diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-01 08:56:54 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-01 08:56:54 +0000 |
commit | 7eed2ec6c797a1e1556ceb954cfe52f8876ef816 (patch) | |
tree | eb34bfe702553a83e3a916f8a1c950a137a9f6a8 /mojo/android/javatests | |
parent | dd8fa9056d20c93c07ec3ff1ff19bec9f4a81921 (diff) | |
download | chromium_src-7eed2ec6c797a1e1556ceb954cfe52f8876ef816.zip chromium_src-7eed2ec6c797a1e1556ceb954cfe52f8876ef816.tar.gz chromium_src-7eed2ec6c797a1e1556ceb954cfe52f8876ef816.tar.bz2 |
Add Message and MessageReceiver classes.
Those classes are utility class to handle mojo messages.
R=viettrungluu@chromium.org,rmcilroy@chromium.org
Review URL: https://codereview.chromium.org/311363003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@280785 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/android/javatests')
-rw-r--r-- | mojo/android/javatests/src/org/chromium/mojo/TestUtils.java | 30 | ||||
-rw-r--r-- | mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java | 123 |
2 files changed, 153 insertions, 0 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java b/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java new file mode 100644 index 0000000..ad13ca9 --- /dev/null +++ b/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java @@ -0,0 +1,30 @@ +// 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; + +import java.nio.ByteBuffer; +import java.util.Random; + +/** + * Utilities methods for tests. + */ +public final class TestUtils { + + private static final Random RANDOM = new Random(); + + /** + * Returns a new direct ByteBuffer of the given size with random (but reproducible) data. + */ + public static ByteBuffer newRandomBuffer(int size) { + byte bytes[] = new byte[size]; + RANDOM.setSeed(size); + RANDOM.nextBytes(bytes); + ByteBuffer data = ByteBuffer.allocateDirect(size); + data.put(bytes); + data.flip(); + return data; + } + +} diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java new file mode 100644 index 0000000..ee58979 --- /dev/null +++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java @@ -0,0 +1,123 @@ +// 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.TestUtils; +import org.chromium.mojo.system.Core; +import org.chromium.mojo.system.DataPipe; +import org.chromium.mojo.system.Handle; +import org.chromium.mojo.system.MessagePipeHandle; +import org.chromium.mojo.system.MojoException; +import org.chromium.mojo.system.MojoResult; +import org.chromium.mojo.system.Pair; +import org.chromium.mojo.system.impl.CoreImpl; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Testing {@link Message}. + */ +public class MessageTest extends MojoTestCase { + + static class RecordingMessageReceiver implements MessageReceiver { + + public final List<Message> messages = new ArrayList<Message>(); + + /** + * @see MessageReceiver#accept(Message) + */ + @Override + public boolean accept(Message message) { + messages.add(message); + return true; + } + } + + private static final int DATA_SIZE = 1024; + + private ByteBuffer mData; + private Pair<MessagePipeHandle, MessagePipeHandle> mHandles; + private List<Handle> mHandlesToSend = new ArrayList<Handle>(); + private List<Handle> mHandlesToClose = new ArrayList<Handle>(); + private RecordingMessageReceiver mMessageReceiver; + + /** + * @see org.chromium.mojo.MojoTestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + Core core = CoreImpl.getInstance(); + mData = TestUtils.newRandomBuffer(DATA_SIZE); + mMessageReceiver = new RecordingMessageReceiver(); + mHandles = core.createMessagePipe(); + Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> datapipe = core.createDataPipe(null); + mHandlesToSend.addAll(Arrays.asList(datapipe.first, datapipe.second)); + mHandlesToClose.addAll(Arrays.asList(mHandles.first, mHandles.second)); + mHandlesToClose.addAll(mHandlesToSend); + } + + /** + * @see org.chromium.mojo.MojoTestCase#tearDown() + */ + @Override + protected void tearDown() throws Exception { + for (Handle handle : mHandlesToClose) { + handle.close(); + } + super.tearDown(); + } + + /** + * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} + */ + @SmallTest + public void testReadAndDispatchMessage() { + mHandles.first.writeMessage(mData, mHandlesToSend, MessagePipeHandle.WriteFlags.NONE); + assertEquals(MojoResult.OK, + Message.readAndDispatchMessage(mHandles.second, mMessageReceiver)); + assertEquals(1, mMessageReceiver.messages.size()); + Message message = mMessageReceiver.messages.get(0); + mHandlesToClose.addAll(message.handles); + assertEquals(mData, message.buffer); + assertEquals(2, message.handles.size()); + for (Handle handle : message.handles) { + assertTrue(handle.isValid()); + } + } + + /** + * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} with no + * message available. + */ + @SmallTest + public void testReadAndDispatchMessageOnEmptyHandle() { + assertEquals(MojoResult.SHOULD_WAIT, + Message.readAndDispatchMessage(mHandles.second, mMessageReceiver)); + assertEquals(0, mMessageReceiver.messages.size()); + } + + /** + * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} on closed + * handle. + */ + @SmallTest + public void testReadAndDispatchMessageOnClosedHandle() { + mHandles.first.close(); + try { + Message.readAndDispatchMessage(mHandles.second, mMessageReceiver); + fail("MojoException should have been thrown"); + } catch (MojoException expected) { + assertEquals(MojoResult.FAILED_PRECONDITION, expected.getMojoResult()); + } + assertEquals(0, mMessageReceiver.messages.size()); + } +} |