Index: webrtc/examples/androidjunit/src/org/appspot/apprtc/util/LooperExecutorTest.java |
diff --git a/webrtc/examples/androidjunit/src/org/appspot/apprtc/util/LooperExecutorTest.java b/webrtc/examples/androidjunit/src/org/appspot/apprtc/util/LooperExecutorTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..02d5f9363cb592415a044f3c339d75c1f9863cb2 |
--- /dev/null |
+++ b/webrtc/examples/androidjunit/src/org/appspot/apprtc/util/LooperExecutorTest.java |
@@ -0,0 +1,106 @@ |
+/* |
+ * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+package org.appspot.apprtc.util; |
+ |
+import org.junit.After; |
+import org.junit.Before; |
+import org.junit.Test; |
+import org.junit.runner.RunWith; |
+import org.mockito.Mock; |
+import org.mockito.MockitoAnnotations; |
+import org.robolectric.RobolectricTestRunner; |
+import org.robolectric.annotation.Config; |
+import org.robolectric.shadows.ShadowLooper; |
+ |
+import static org.mockito.Mockito.times; |
+import static org.mockito.Mockito.verify; |
+import static org.mockito.Mockito.verifyNoMoreInteractions; |
+import static org.robolectric.Robolectric.shadowOf; |
+ |
+@RunWith(RobolectricTestRunner.class) |
+@Config(manifest = Config.NONE) |
+public class LooperExecutorTest { |
+ private final static int RUN_TIMES = 10; |
+ |
+ @Mock private Runnable mockRunnable; |
+ private LooperExecutor executor; |
+ |
+ @Before |
+ public void setUp() { |
+ MockitoAnnotations.initMocks(this); |
+ executor = new LooperExecutor(); |
+ } |
+ |
+ @After |
+ public void tearDown() { |
+ executor.requestStop(); |
+ ShadowLooper shadowLooper = getShadowLooper(); |
+ shadowLooper.idle(); |
+ } |
+ |
+ @Test |
+ public void testExecute() { |
+ executor.requestStart(); |
+ ShadowLooper shadowLooper = getShadowLooper(); |
+ |
+ for (int i = 0; i < RUN_TIMES; i++) { |
+ executor.execute(mockRunnable); |
+ } |
+ |
+ verifyNoMoreInteractions(mockRunnable); |
+ shadowLooper.idle(); |
magjed_webrtc
2016/05/19 12:17:13
What happens when shadowLooper.idle() is called? A
sakal
2016/05/19 13:04:38
That is correct. Done.
|
+ verify(mockRunnable, times(RUN_TIMES)).run(); |
+ } |
+ |
+ @Test |
+ public void testExecuteBeforeStart() { |
magjed_webrtc
2016/05/19 12:17:13
Comment that calls to execute() before requestStar
sakal
2016/05/19 13:04:38
Done.
|
+ executor.execute(mockRunnable); |
+ verifyNoMoreInteractions(mockRunnable); |
+ |
+ executor.requestStart(); |
+ ShadowLooper shadowLooper = getShadowLooper(); |
+ shadowLooper.idle(); |
+ |
+ verifyNoMoreInteractions(mockRunnable); |
magjed_webrtc
2016/05/19 12:17:13
Isn't enough with one verifyNoMoreInteractions(moc
sakal
2016/05/19 13:04:38
Done.
|
+ } |
+ |
+ @Test |
+ public void testExecuteAfterStop() { |
magjed_webrtc
2016/05/19 12:17:13
Comment that calls to execute() after requestStop(
sakal
2016/05/19 13:04:38
Done.
|
+ executor.requestStart(); |
+ ShadowLooper shadowLooper = getShadowLooper(); |
+ executor.requestStop(); |
+ |
+ executor.execute(mockRunnable); |
+ verifyNoMoreInteractions(mockRunnable); |
+ shadowLooper.idle(); |
+ |
+ verifyNoMoreInteractions(mockRunnable); |
+ } |
+ |
+ @Test |
+ public void testMultipleStarts() { |
magjed_webrtc
2016/05/19 12:17:13
Comment that extra requestStart() calls are ok and
sakal
2016/05/19 13:04:38
Done.
|
+ executor.requestStart(); |
+ testExecute(); |
+ } |
+ |
+ @Test |
+ public void testMultipleStops() { |
+ executor.requestStart(); |
+ executor.requestStop(); |
+ executor.requestStop(); |
+ ShadowLooper shadowLooper = getShadowLooper(); |
+ shadowLooper.idle(); |
+ } |
+ |
+ private ShadowLooper getShadowLooper() { |
+ return shadowOf(executor.getHandler().getLooper()); |
+ } |
+} |