Index: mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java |
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ab8f960865850ab335b1b71dc68569e364bb58d7 |
--- /dev/null |
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java |
@@ -0,0 +1,86 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.mojo.bindings; |
+ |
+import android.test.suitebuilder.annotation.SmallTest; |
+ |
+import org.chromium.mojo.MojoTestCase; |
+import org.chromium.mojo.system.impl.CoreImpl; |
+ |
+import java.util.ArrayList; |
+import java.util.List; |
+import java.util.concurrent.Executor; |
+import java.util.concurrent.Executors; |
+import java.util.concurrent.ScheduledExecutorService; |
+ |
+/** |
+ * Testing the executor factory. |
+ */ |
+public class ExecutorFactoryTest extends MojoTestCase { |
+ |
+ private static final long RUN_LOOP_TIMEOUT_MS = 50; |
+ private static final ScheduledExecutorService WORKER = |
+ Executors.newSingleThreadScheduledExecutor(); |
+ |
+ private Executor mExecutor; |
+ private List<Thread> mThreadContainer; |
+ |
+ /** |
+ * @see MojoTestCase#setUp() |
+ */ |
+ @Override |
+ protected void setUp() throws Exception { |
+ super.setUp(); |
+ mExecutor = ExecutorFactory.getExecutorForCurrentThread(CoreImpl.getInstance()); |
+ mThreadContainer = new ArrayList<Thread>(); |
+ } |
+ |
+ /** |
+ * Testing the {@link Executor} when called from the executor thread. |
+ */ |
+ @SmallTest |
+ public void testExecutorOnCurrentThread() { |
+ mExecutor.execute(new Runnable() { |
+ @Override |
+ public void run() { |
+ mThreadContainer.add(Thread.currentThread()); |
+ } |
+ }); |
+ assertEquals(0, mThreadContainer.size()); |
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS); |
+ assertEquals(1, mThreadContainer.size()); |
+ assertEquals(Thread.currentThread(), mThreadContainer.get(0)); |
+ } |
+ |
+ /** |
+ * Testing the {@link Executor} when called from another thread. |
+ */ |
+ @SmallTest |
+ public void testExecutorOnOtherThread() throws InterruptedException { |
+ final Object lock = new Object(); |
+ synchronized (lock) { |
+ WORKER.execute(new Runnable() { |
+ @Override |
+ public void run() { |
+ synchronized (lock) { |
+ mExecutor.execute(new Runnable() { |
+ |
+ @Override |
+ public void run() { |
+ mThreadContainer.add(Thread.currentThread()); |
+ } |
+ }); |
+ lock.notify(); |
+ } |
+ } |
+ }); |
+ lock.wait(); |
+ } |
+ assertEquals(0, mThreadContainer.size()); |
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS); |
+ assertEquals(1, mThreadContainer.size()); |
+ assertEquals(Thread.currentThread(), mThreadContainer.get(0)); |
+ } |
+} |