• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.mojo.bindings;
6 
7 import android.test.suitebuilder.annotation.SmallTest;
8 
9 import org.chromium.mojo.MojoTestCase;
10 import org.chromium.mojo.bindings.BindingsTestUtils.CapturingErrorHandler;
11 import org.chromium.mojo.bindings.BindingsTestUtils.RecordingMessageReceiver;
12 import org.chromium.mojo.system.Core;
13 import org.chromium.mojo.system.Handle;
14 import org.chromium.mojo.system.MessagePipeHandle;
15 import org.chromium.mojo.system.MojoResult;
16 import org.chromium.mojo.system.Pair;
17 import org.chromium.mojo.system.ResultAnd;
18 import org.chromium.mojo.system.impl.CoreImpl;
19 
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 
23 /**
24  * Testing the {@link Connector} class.
25  */
26 public class ConnectorTest extends MojoTestCase {
27 
28     private static final int DATA_LENGTH = 1024;
29 
30     private MessagePipeHandle mHandle;
31     private Connector mConnector;
32     private Message mTestMessage;
33     private RecordingMessageReceiver mReceiver;
34     private CapturingErrorHandler mErrorHandler;
35 
36     /**
37      * @see MojoTestCase#setUp()
38      */
39     @Override
setUp()40     protected void setUp() throws Exception {
41         super.setUp();
42         Core core = CoreImpl.getInstance();
43         Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(
44                 new MessagePipeHandle.CreateOptions());
45         mHandle = handles.first;
46         mConnector = new Connector(handles.second);
47         mReceiver = new RecordingMessageReceiver();
48         mConnector.setIncomingMessageReceiver(mReceiver);
49         mErrorHandler = new CapturingErrorHandler();
50         mConnector.setErrorHandler(mErrorHandler);
51         mConnector.start();
52         mTestMessage = BindingsTestUtils.newRandomMessage(DATA_LENGTH);
53         assertNull(mErrorHandler.getLastMojoException());
54         assertEquals(0, mReceiver.messages.size());
55     }
56 
57     /**
58      * @see MojoTestCase#tearDown()
59      */
60     @Override
tearDown()61     protected void tearDown() throws Exception {
62         mConnector.close();
63         mHandle.close();
64         super.tearDown();
65     }
66 
67     /**
68      * Test sending a message through a {@link Connector}.
69      */
70     @SmallTest
testSendingMessage()71     public void testSendingMessage() {
72         mConnector.accept(mTestMessage);
73         assertNull(mErrorHandler.getLastMojoException());
74         ByteBuffer received = ByteBuffer.allocateDirect(DATA_LENGTH);
75         ResultAnd<MessagePipeHandle.ReadMessageResult> result =
76                 mHandle.readMessage(received, 0, MessagePipeHandle.ReadFlags.NONE);
77         assertEquals(MojoResult.OK, result.getMojoResult());
78         assertEquals(DATA_LENGTH, result.getValue().getMessageSize());
79         assertEquals(mTestMessage.getData(), received);
80     }
81 
82     /**
83      * Test receiving a message through a {@link Connector}
84      */
85     @SmallTest
testReceivingMessage()86     public void testReceivingMessage() {
87         mHandle.writeMessage(mTestMessage.getData(), new ArrayList<Handle>(),
88                 MessagePipeHandle.WriteFlags.NONE);
89         runLoopUntilIdle();
90         assertNull(mErrorHandler.getLastMojoException());
91         assertEquals(1, mReceiver.messages.size());
92         Message received = mReceiver.messages.get(0);
93         assertEquals(0, received.getHandles().size());
94         assertEquals(mTestMessage.getData(), received.getData());
95     }
96 
97     /**
98      * Test receiving an error through a {@link Connector}.
99      */
100     @SmallTest
testErrors()101     public void testErrors() {
102         mHandle.close();
103         runLoopUntilIdle();
104         assertNotNull(mErrorHandler.getLastMojoException());
105         assertEquals(MojoResult.FAILED_PRECONDITION,
106                 mErrorHandler.getLastMojoException().getMojoResult());
107     }
108 }
109