OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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.test; | |
12 | |
13 import java.util.concurrent.CountDownLatch; | |
14 import java.util.concurrent.TimeUnit; | |
15 | |
16 import org.appspot.apprtc.util.LooperExecutor; | |
17 | |
18 import android.test.InstrumentationTestCase; | |
19 import android.test.suitebuilder.annotation.SmallTest; | |
20 import android.util.Log; | |
21 | |
22 public class LooperExecutorTest extends InstrumentationTestCase { | |
23 private static final String TAG = "LooperTest"; | |
24 private static final int WAIT_TIMEOUT = 5000; | |
25 | |
26 @SmallTest | |
27 public void testLooperExecutor() throws InterruptedException { | |
28 Log.d(TAG, "testLooperExecutor"); | |
29 final int counter[] = new int[1]; | |
30 final int expectedCounter = 10; | |
31 final CountDownLatch looperDone = new CountDownLatch(1); | |
32 | |
33 Runnable counterIncRunnable = new Runnable() { | |
34 @Override | |
35 public void run() { | |
36 counter[0]++; | |
37 Log.d(TAG, "Run " + counter[0]); | |
38 } | |
39 }; | |
40 LooperExecutor executor = new LooperExecutor(); | |
41 | |
42 // Try to execute a counter increment task before starting an executor. | |
43 executor.execute(counterIncRunnable); | |
44 | |
45 // Start the executor and run expected amount of counter increment task. | |
46 executor.requestStart(); | |
47 for (int i = 0; i < expectedCounter; i++) { | |
48 executor.execute(counterIncRunnable); | |
49 } | |
50 executor.execute(new Runnable() { | |
51 @Override | |
52 public void run() { | |
53 looperDone.countDown(); | |
54 } | |
55 }); | |
56 executor.requestStop(); | |
57 | |
58 // Try to execute a task after stopping the executor. | |
59 executor.execute(counterIncRunnable); | |
60 | |
61 // Wait for final looper task and make sure the counter increment task | |
62 // is executed expected amount of times. | |
63 looperDone.await(WAIT_TIMEOUT, TimeUnit.MILLISECONDS); | |
64 assertTrue (looperDone.getCount() == 0); | |
65 assertTrue (counter[0] == expectedCounter); | |
66 | |
67 Log.d(TAG, "testLooperExecutor done"); | |
68 } | |
69 } | |
OLD | NEW |