Index: mojo/android/javatests/src/org/chromium/mojo/system/CoreTest.java |
diff --git a/mojo/android/javatests/src/org/chromium/mojo/system/CoreTest.java b/mojo/android/javatests/src/org/chromium/mojo/system/CoreTest.java |
index bf941b03d25d9ee108bd776ae6ccb8ae8ba5f924..73f0b7f1ea408e245fd21fb335c100a4416c9431 100644 |
--- a/mojo/android/javatests/src/org/chromium/mojo/system/CoreTest.java |
+++ b/mojo/android/javatests/src/org/chromium/mojo/system/CoreTest.java |
@@ -10,6 +10,7 @@ import android.test.suitebuilder.annotation.SmallTest; |
import org.chromium.base.JNINamespace; |
import org.chromium.base.library_loader.LibraryLoader; |
+import org.chromium.mojo.system.AsyncWaiter.Cancellable; |
import org.chromium.mojo.system.Core.WaitFlags; |
import org.chromium.mojo.system.Core.WaitManyResult; |
import org.chromium.mojo.system.MessagePipeHandle.ReadFlags; |
@@ -33,9 +34,13 @@ import java.util.concurrent.TimeUnit; |
@JNINamespace("mojo::android") |
public class CoreTest extends InstrumentationTestCase { |
+ private static final long RUN_LOOP_DELAY = 50; |
rmcilroy
2014/05/15 22:36:51
RUN_LOOP_TIMEOUT_MS
qsr
2014/05/19 12:12:09
Done.
|
+ |
private static final ScheduledExecutorService WORKER = |
Executors.newSingleThreadScheduledExecutor(); |
+ private long mTestEnvironmentPointer; |
+ |
/** |
* @see junit.framework.TestCase#setUp() |
*/ |
@@ -43,6 +48,15 @@ public class CoreTest extends InstrumentationTestCase { |
protected void setUp() throws Exception { |
LibraryLoader.ensureInitialized(); |
nativeInitApplicationContext(getInstrumentation().getTargetContext()); |
+ mTestEnvironmentPointer = nativeSetupTestEnvironment(); |
+ } |
+ |
+ /** |
+ * @see android.test.InstrumentationTestCase#tearDown() |
+ */ |
+ @Override |
+ protected void tearDown() throws Exception { |
+ nativeTearDownTestEnvironment(mTestEnvironmentPointer); |
} |
/** |
@@ -518,5 +532,207 @@ public class CoreTest extends InstrumentationTestCase { |
} |
} |
+ private static class AsyncWaiterResult implements AsyncWaiterCallback { |
+ private int mResult = Integer.MIN_VALUE; |
+ private MojoException mException = null; |
+ |
+ /** |
+ * @see AsyncWaiterCallback#onResult(int) |
+ */ |
+ @Override |
+ public void onResult(int result) { |
+ this.mResult = result; |
+ } |
+ |
+ /** |
+ * @see AsyncWaiterCallback#onError(MojoException) |
+ */ |
+ @Override |
+ public void onError(MojoException exception) { |
+ this.mException = exception; |
+ } |
+ |
+ /** |
+ * @return the result |
+ */ |
+ public int getResult() { |
+ return mResult; |
+ } |
+ |
+ /** |
+ * @return the exception |
+ */ |
+ public MojoException getException() { |
+ return mException; |
+ } |
+ |
+ } |
+ |
+ /** |
+ * Testing core {@link AsyncWaiter} implementation. |
+ */ |
+ @SmallTest |
+ public void testAsyncWaiter() { |
+ Core core = CoreImpl.getInstance(); |
+ |
+ // Checking a correct result. |
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
rmcilroy
2014/05/15 22:36:51
nit - would probably be clearer with newlines betw
qsr
2014/05/19 12:12:09
Done.
|
+ core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ handles.second.writeMessage(ByteBuffer.allocateDirect(1), null, |
+ MessagePipeHandle.WriteFlags.none()); |
+ nativeRunLoop(50); |
rmcilroy
2014/05/15 22:36:51
Use RUN_LOOP_TIMEOUT_MS throughout.
qsr
2014/05/19 12:12:09
Done.
|
+ assertNull(asyncWaiterResult.getException()); |
+ assertEquals(MojoResult.OK, asyncWaiterResult.getResult()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ // Closing the peer handle. |
bulach
2014/05/15 19:32:58
nit: you may want to split it into separate test c
rmcilroy
2014/05/15 22:36:51
+1 (and for each of the other test case).
qsr
2014/05/19 12:12:09
Done.
|
+ handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ nativeRunLoop(50); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ handles.second.close(); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertNull(asyncWaiterResult.getException()); |
+ assertEquals(MojoResult.FAILED_PRECONDITION, asyncWaiterResult.getResult()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ // Closing the handle itself. |
+ handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ handles.first.close(); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ // TODO(qsr) Re-enable when MojoWaitMany handles it correctly. |
+ // assertNull(asyncWaiterResult.getException()); |
+ // assertEquals(MojoResult.CANCELLED, asyncWaiterResult.getResult()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ // Waiting on an invalid handle. |
+ handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ handles.first.close(); |
+ core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertNotNull(asyncWaiterResult.getException()); |
+ assertEquals(MojoResult.INVALID_ARGUMENT, |
+ asyncWaiterResult.getException().getMojoResult()); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ // Waiting with a timeout. |
+ handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), RUN_LOOP_DELAY / 4, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertNull(asyncWaiterResult.getException()); |
+ assertEquals(MojoResult.DEADLINE_EXCEEDED, asyncWaiterResult.getResult()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ // Canceling a wait. |
+ handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ Cancellable cancellable = core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ cancellable.cancel(); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ handles.second.writeMessage(ByteBuffer.allocateDirect(1), null, |
+ MessagePipeHandle.WriteFlags.none()); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ // Immediately canceling a wait on an invalid handle.. |
+ handles = core.createMessagePipe(); |
+ try { |
+ final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); |
+ handles.first.close(); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ Cancellable cancellable = core.getDefaultAsyncWaiter().asyncWait(handles.first, |
+ WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ cancellable.cancel(); |
+ nativeRunLoop(RUN_LOOP_DELAY); |
+ assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); |
+ assertEquals(null, asyncWaiterResult.getException()); |
+ } finally { |
+ handles.first.close(); |
+ handles.second.close(); |
+ } |
+ |
+ } |
+ |
private native void nativeInitApplicationContext(Context context); |
+ |
+ private native long nativeSetupTestEnvironment(); |
+ |
+ private native void nativeTearDownTestEnvironment(long testEnvironment); |
+ |
+ private native void nativeRunLoop(long timeoutMS); |
} |