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..2ca126be7e3073947d5c788e05aa8b7a16425f73 |
--- /dev/null |
+++ b/webrtc/examples/androidjunit/src/org/appspot/apprtc/util/LooperExecutorTest.java |
@@ -0,0 +1,122 @@ |
+/* |
+ * 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(); |
+ executePendingRunnables(); |
+ } |
+ |
+ @Test |
+ public void testExecute() { |
+ executor.requestStart(); |
+ |
+ for (int i = 0; i < RUN_TIMES; i++) { |
+ executor.execute(mockRunnable); |
+ } |
+ |
+ verifyNoMoreInteractions(mockRunnable); |
+ executePendingRunnables(); |
+ verify(mockRunnable, times(RUN_TIMES)).run(); |
+ } |
+ |
+ /** |
+ * Test that runnables executed before requestStart are ignored. |
+ */ |
+ @Test |
+ public void testExecuteBeforeStart() { |
+ executor.execute(mockRunnable); |
+ |
+ executor.requestStart(); |
+ executePendingRunnables(); |
+ |
+ verifyNoMoreInteractions(mockRunnable); |
magjed_webrtc
2016/05/20 07:50:52
Can you move this to the top of the function?
magjed_webrtc
2016/05/20 07:57:04
Discussed offline. I misunderstood how verifyNoMor
sakal
2016/05/20 07:57:32
Function checks that there haven't been any unexpe
|
+ } |
+ |
+ /** |
+ * Test that runnables executed after requestStop are not executed. |
+ */ |
+ @Test |
+ public void testExecuteAfterStop() { |
+ executor.requestStart(); |
+ executor.requestStop(); |
+ |
+ executor.execute(mockRunnable); |
+ executePendingRunnables(); |
+ |
+ verifyNoMoreInteractions(mockRunnable); |
magjed_webrtc
2016/05/20 07:50:52
Can you move this to the top of the function?
sakal
2016/05/20 07:57:32
ditto.
|
+ } |
+ |
+ /** |
+ * Test multiple requestStart calls are just ignored. |
+ */ |
+ @Test |
+ public void testMultipleStarts() { |
+ executor.requestStart(); |
+ testExecute(); |
+ } |
+ |
+ /** |
+ * Test multiple requestStop calls are just ignored. |
+ */ |
+ @Test |
+ public void testMultipleStops() { |
+ executor.requestStart(); |
+ executor.requestStop(); |
+ executor.requestStop(); |
+ executePendingRunnables(); |
+ } |
+ |
+ /** |
+ * Calls ShadowLooper's idle method in order to execute pending runnables. |
+ */ |
+ private void executePendingRunnables() { |
+ ShadowLooper shadowLooper = getShadowLooper(); |
+ shadowLooper.idle(); |
+ } |
+ |
+ /** |
+ * Get ShadowLooper of the executor thread |
+ */ |
+ private ShadowLooper getShadowLooper() { |
+ return shadowOf(executor.getHandler().getLooper()); |
+ } |
+} |