Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / android / javatests / src / org / chromium / mojo / bindings / BindingsTestUtils.java
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.MojoException;
10 import org.chromium.mojo.system.Pair;
11
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17  * Utility class for bindings tests.
18  */
19 public class BindingsTestUtils {
20
21     /**
22      * {@link MessageReceiver} that records any message it receives.
23      */
24     public static class RecordingMessageReceiver extends SideEffectFreeCloseable
25             implements MessageReceiver {
26
27         public final List<Message> messages = new ArrayList<Message>();
28
29         /**
30          * @see MessageReceiver#accept(Message)
31          */
32         @Override
33         public boolean accept(Message message) {
34             messages.add(message);
35             return true;
36         }
37     }
38
39     /**
40      * {@link MessageReceiverWithResponder} that records any message it receives.
41      */
42     public static class RecordingMessageReceiverWithResponder extends RecordingMessageReceiver
43             implements MessageReceiverWithResponder {
44
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
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
63         private MojoException mLastMojoException = null;
64
65         /**
66          * @see ConnectionErrorHandler#onConnectionError(MojoException)
67          */
68         @Override
69         public void onConnectionError(MojoException e) {
70             mLastMojoException = e;
71         }
72
73         /**
74          * Returns the last recorded exception.
75          */
76         public MojoException getLastMojoException() {
77             return mLastMojoException;
78         }
79
80     }
81
82     /**
83      * Creates a new valid {@link Message}. The message will have a valid header.
84      */
85     public static Message newRandomMessage(int size) {
86         assert size > 16;
87         ByteBuffer message = TestUtils.newRandomBuffer(size);
88         int[] headerAsInts = { 16, 2, 0, 0 };
89         for (int i = 0; i < 4; ++i) {
90             message.putInt(4 * i, headerAsInts[i]);
91         }
92         message.position(0);
93         return new Message(message, new ArrayList<Handle>());
94     }
95 }