OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 package org.appspot.apprtc.util; | |
12 | |
13 import static org.mockito.Mockito.times; | |
14 import static org.mockito.Mockito.verify; | |
15 import static org.mockito.Mockito.verifyNoMoreInteractions; | |
16 import static org.robolectric.Robolectric.shadowOf; | |
17 | |
18 import org.junit.After; | |
19 import org.junit.Before; | |
20 import org.junit.Test; | |
21 import org.junit.runner.RunWith; | |
22 import org.mockito.Mock; | |
23 import org.mockito.MockitoAnnotations; | |
24 import org.robolectric.RobolectricTestRunner; | |
25 import org.robolectric.annotation.Config; | |
26 import org.robolectric.shadows.ShadowLooper; | |
27 | |
28 @RunWith(RobolectricTestRunner.class) | |
29 @Config(manifest = Config.NONE) | |
30 public class LooperExecutorTest { | |
31 private final static int RUN_TIMES = 10; | |
32 | |
33 @Mock private Runnable mockRunnable; | |
34 private LooperExecutor executor; | |
35 | |
36 @Before | |
37 public void setUp() { | |
38 MockitoAnnotations.initMocks(this); | |
39 executor = new LooperExecutor(); | |
40 } | |
41 | |
42 @After | |
43 public void tearDown() { | |
44 executor.requestStop(); | |
45 executePendingRunnables(); | |
46 } | |
47 | |
48 @Test | |
49 public void testExecute() { | |
50 executor.requestStart(); | |
51 | |
52 for (int i = 0; i < RUN_TIMES; i++) { | |
53 executor.execute(mockRunnable); | |
54 } | |
55 | |
56 verifyNoMoreInteractions(mockRunnable); | |
57 executePendingRunnables(); | |
58 verify(mockRunnable, times(RUN_TIMES)).run(); | |
59 } | |
60 | |
61 /** | |
62 * Test that runnables executed before requestStart are ignored. | |
63 */ | |
64 @Test | |
65 public void testExecuteBeforeStart() { | |
66 executor.execute(mockRunnable); | |
67 | |
68 executor.requestStart(); | |
69 executePendingRunnables(); | |
70 | |
71 verifyNoMoreInteractions(mockRunnable); | |
72 } | |
73 | |
74 /** | |
75 * Test that runnables executed after requestStop are not executed. | |
76 */ | |
77 @Test | |
78 public void testExecuteAfterStop() { | |
79 executor.requestStart(); | |
80 executor.requestStop(); | |
81 | |
82 executor.execute(mockRunnable); | |
83 executePendingRunnables(); | |
84 | |
85 verifyNoMoreInteractions(mockRunnable); | |
86 } | |
87 | |
88 /** | |
89 * Test multiple requestStart calls are just ignored. | |
90 */ | |
91 @Test | |
92 public void testMultipleStarts() { | |
93 executor.requestStart(); | |
94 testExecute(); | |
95 } | |
96 | |
97 /** | |
98 * Test multiple requestStop calls are just ignored. | |
99 */ | |
100 @Test | |
101 public void testMultipleStops() { | |
102 executor.requestStart(); | |
103 executor.requestStop(); | |
104 executor.requestStop(); | |
105 executePendingRunnables(); | |
106 } | |
107 | |
108 /** | |
109 * Calls ShadowLooper's idle method in order to execute pending runnables. | |
110 */ | |
111 private void executePendingRunnables() { | |
112 ShadowLooper shadowLooper = getShadowLooper(); | |
113 shadowLooper.idle(); | |
114 } | |
115 | |
116 /** | |
117 * Get ShadowLooper of the executor thread. | |
118 */ | |
119 private ShadowLooper getShadowLooper() { | |
120 return shadowOf(executor.getHandler().getLooper()); | |
121 } | |
122 } | |
OLD | NEW |