diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-20 15:36:34 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-20 15:36:34 +0000 |
commit | 7d9d82e1b08b031b24451d6cb7f035d0cc3f7365 (patch) | |
tree | e879929a8d6574a0d5c7f1b2fc862f053d263295 /mojo/android/javatests | |
parent | 07177bac1c498f5f3e8307058dd8a36ca02b1756 (diff) | |
download | chromium_src-7d9d82e1b08b031b24451d6cb7f035d0cc3f7365.zip chromium_src-7d9d82e1b08b031b24451d6cb7f035d0cc3f7365.tar.gz chromium_src-7d9d82e1b08b031b24451d6cb7f035d0cc3f7365.tar.bz2 |
Add AsyncWaiter implementation to java API.
Review URL: https://codereview.chromium.org/288993002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271660 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/android/javatests')
-rw-r--r-- | mojo/android/javatests/core_test.cc | 35 | ||||
-rw-r--r-- | mojo/android/javatests/src/org/chromium/mojo/system/CoreTest.java | 307 |
2 files changed, 342 insertions, 0 deletions
diff --git a/mojo/android/javatests/core_test.cc b/mojo/android/javatests/core_test.cc index 3d4032f..38aa6c4 100644 --- a/mojo/android/javatests/core_test.cc +++ b/mojo/android/javatests/core_test.cc @@ -6,7 +6,22 @@ #include "base/android/jni_android.h" #include "base/android/scoped_java_ref.h" +#include "base/bind.h" +#include "base/logging.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "base/test/test_support_android.h" #include "jni/CoreTest_jni.h" +#include "mojo/public/cpp/environment/environment.h" + +namespace { + +struct TestEnvironment { + mojo::Environment environment; + base::MessageLoopForUI message_loop; +}; + +} // namespace namespace mojo { namespace android { @@ -16,6 +31,26 @@ static void InitApplicationContext(JNIEnv* env, jobject context) { base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); base::android::InitApplicationContext(env, scoped_context); + base::InitAndroidTestMessageLoop(); +} + +static jlong SetupTestEnvironment(JNIEnv* env, jobject jcaller) { + return reinterpret_cast<intptr_t>(new TestEnvironment()); +} + +static void TearDownTestEnvironment(JNIEnv* env, + jobject jcaller, + jlong test_environment) { + delete reinterpret_cast<TestEnvironment*>(test_environment); +} + +static void RunLoop(JNIEnv* env, jobject jcaller, jlong timeout_ms) { + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::MessageLoop::QuitClosure(), + base::TimeDelta::FromMilliseconds(timeout_ms)); + base::RunLoop run_loop; + run_loop.Run(); } bool RegisterCoreTest(JNIEnv* env) { 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 bf941b0..b534752 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,8 @@ import android.test.suitebuilder.annotation.SmallTest; import org.chromium.base.JNINamespace; import org.chromium.base.library_loader.LibraryLoader; +import org.chromium.mojo.system.AsyncWaiter.Callback; +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,16 +35,31 @@ import java.util.concurrent.TimeUnit; @JNINamespace("mojo::android") public class CoreTest extends InstrumentationTestCase { + private static final long RUN_LOOP_TIMEOUT_MS = 5; + private static final ScheduledExecutorService WORKER = Executors.newSingleThreadScheduledExecutor(); + private long mTestEnvironmentPointer; + /** * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() throws Exception { + super.setUp(); LibraryLoader.ensureInitialized(); nativeInitApplicationContext(getInstrumentation().getTargetContext()); + mTestEnvironmentPointer = nativeSetupTestEnvironment(); + } + + /** + * @see android.test.InstrumentationTestCase#tearDown() + */ + @Override + protected void tearDown() throws Exception { + nativeTearDownTestEnvironment(mTestEnvironmentPointer); + super.tearDown(); } /** @@ -518,5 +535,295 @@ public class CoreTest extends InstrumentationTestCase { } } + private static class AsyncWaiterResult implements Callback { + private int mResult = Integer.MIN_VALUE; + private MojoException mException = null; + + /** + * @see Callback#onResult(int) + */ + @Override + public void onResult(int result) { + this.mResult = result; + } + + /** + * @see Callback#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 testAsyncWaiterCorrectResult() { + 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()); + + 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(RUN_LOOP_TIMEOUT_MS); + assertNull(asyncWaiterResult.getException()); + assertEquals(MojoResult.OK, asyncWaiterResult.getResult()); + } finally { + handles.first.close(); + handles.second.close(); + } + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterClosingPeerHandle() { + Core core = CoreImpl.getInstance(); + + // Closing the peer handle. + Pair<MessagePipeHandle, MessagePipeHandle> 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_TIMEOUT_MS); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + handles.second.close(); + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + assertNull(asyncWaiterResult.getException()); + assertEquals(MojoResult.FAILED_PRECONDITION, asyncWaiterResult.getResult()); + } finally { + handles.first.close(); + handles.second.close(); + } + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterClosingWaitingHandle() { + Core core = CoreImpl.getInstance(); + + // Closing the peer handle. + Pair<MessagePipeHandle, MessagePipeHandle> 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_TIMEOUT_MS); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + handles.first.close(); + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + // TODO(qsr) Re-enable when MojoWaitMany handles it correctly. + // assertNull(asyncWaiterResult.getException()); + // assertEquals(MojoResult.CANCELLED, asyncWaiterResult.getResult()); + } finally { + handles.first.close(); + handles.second.close(); + } + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterWaitingOnInvalidHandle() { + Core core = CoreImpl.getInstance(); + + // Closing the peer handle. + Pair<MessagePipeHandle, MessagePipeHandle> 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_TIMEOUT_MS); + assertNotNull(asyncWaiterResult.getException()); + assertEquals(MojoResult.INVALID_ARGUMENT, + asyncWaiterResult.getException().getMojoResult()); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + } finally { + handles.first.close(); + handles.second.close(); + } + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterWaitingOnDefaultInvalidHandle() { + Core core = CoreImpl.getInstance(); + + final AsyncWaiterResult asyncWaiterResult = new AsyncWaiterResult(); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + core.getDefaultAsyncWaiter().asyncWait(new InvalidHandle(), + WaitFlags.none().setReadable(true), Core.DEADLINE_INFINITE, asyncWaiterResult); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + assertNotNull(asyncWaiterResult.getException()); + assertEquals(MojoResult.INVALID_ARGUMENT, + asyncWaiterResult.getException().getMojoResult()); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterWaitingWithTimeout() { + Core core = CoreImpl.getInstance(); + + // Closing the peer handle. + Pair<MessagePipeHandle, MessagePipeHandle> 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_TIMEOUT_MS, asyncWaiterResult); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + nativeRunLoop(10 * RUN_LOOP_TIMEOUT_MS); + assertNull(asyncWaiterResult.getException()); + assertEquals(MojoResult.DEADLINE_EXCEEDED, asyncWaiterResult.getResult()); + } finally { + handles.first.close(); + handles.second.close(); + } + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterCancelWaiting() { + Core core = CoreImpl.getInstance(); + + // Closing the peer handle. + Pair<MessagePipeHandle, MessagePipeHandle> 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_TIMEOUT_MS); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + cancellable.cancel(); + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + + handles.second.writeMessage(ByteBuffer.allocateDirect(1), null, + MessagePipeHandle.WriteFlags.none()); + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); + assertEquals(null, asyncWaiterResult.getException()); + } finally { + handles.first.close(); + handles.second.close(); + } + } + + /** + * Testing core {@link AsyncWaiter} implementation. + */ + @SmallTest + public void testAsyncWaiterImmediateCancelOnInvalidHandle() { + Core core = CoreImpl.getInstance(); + + // Closing the peer handle. + Pair<MessagePipeHandle, MessagePipeHandle> 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_TIMEOUT_MS); + 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); } |