• 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 org.chromium.mojo.TestUtils;
8 import org.chromium.mojo.system.Handle;
9 import org.chromium.mojo.system.MessagePipeHandle;
10 import org.chromium.mojo.system.MojoException;
11 import org.chromium.mojo.system.Pair;
12 import org.chromium.mojo.system.impl.CoreImpl;
13 
14 import java.io.Closeable;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.List;
18 
19 /**
20  * Utility class for bindings tests.
21  */
22 public class BindingsTestUtils {
23     /**
24      * {@link MessageReceiver} that records any message it receives.
25      */
26     public static class RecordingMessageReceiver
27             extends SideEffectFreeCloseable implements MessageReceiver {
28         public final List<Message> messages = new ArrayList<Message>();
29 
30         /**
31          * @see MessageReceiver#accept(Message)
32          */
33         @Override
accept(Message message)34         public boolean accept(Message message) {
35             messages.add(message);
36             return true;
37         }
38     }
39 
40     /**
41      * {@link MessageReceiverWithResponder} that records any message it receives.
42      */
43     public static class RecordingMessageReceiverWithResponder
44             extends RecordingMessageReceiver implements MessageReceiverWithResponder {
45         public final List<Pair<Message, MessageReceiver>> messagesWithReceivers =
46                 new ArrayList<Pair<Message, MessageReceiver>>();
47 
48         /**
49          * @see MessageReceiverWithResponder#acceptWithResponder(Message, MessageReceiver)
50          */
51         @Override
acceptWithResponder(Message message, MessageReceiver responder)52         public boolean acceptWithResponder(Message message, MessageReceiver responder) {
53             messagesWithReceivers.add(Pair.create(message, responder));
54             return true;
55         }
56     }
57 
58     /**
59      * {@link ConnectionErrorHandler} that records any error it received.
60      */
61     public static class CapturingErrorHandler implements ConnectionErrorHandler {
62         private MojoException mLastMojoException = null;
63 
64         /**
65          * @see ConnectionErrorHandler#onConnectionError(MojoException)
66          */
67         @Override
onConnectionError(MojoException e)68         public void onConnectionError(MojoException e) {
69             mLastMojoException = e;
70         }
71 
72         /**
73          * Returns the last recorded exception.
74          */
getLastMojoException()75         public MojoException getLastMojoException() {
76             return mLastMojoException;
77         }
78     }
79 
80     /**
81      * Creates a new valid {@link Message}. The message will have a valid header.
82      */
newRandomMessage(int size)83     public static Message newRandomMessage(int size) {
84         assert size > 16;
85         ByteBuffer message = TestUtils.newRandomBuffer(size);
86         int[] headerAsInts = {16, 2, 0, 0};
87         for (int i = 0; i < 4; ++i) {
88             message.putInt(4 * i, headerAsInts[i]);
89         }
90         message.position(0);
91         return new Message(message, new ArrayList<Handle>());
92     }
93 
newProxyOverPipe( Interface.Manager<I, P> manager, I impl, List<Closeable> toClose)94     public static <I extends Interface, P extends Interface.Proxy> P newProxyOverPipe(
95             Interface.Manager<I, P> manager, I impl, List<Closeable> toClose) {
96         Pair<MessagePipeHandle, MessagePipeHandle> handles =
97                 CoreImpl.getInstance().createMessagePipe(null);
98         P proxy = manager.attachProxy(handles.first, 0);
99         toClose.add(proxy);
100         manager.bind(impl, handles.second);
101         return proxy;
102     }
103 
structsEqual(Struct s1, Struct s2)104     public static boolean structsEqual(Struct s1, Struct s2) {
105         if (s1 == s2) return true;
106         if (s1 == null || s2 == null) return false;
107         return s1.serialize(null).getData().equals(s2.serialize(null).getData());
108     }
109 }
110