OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.mojo.bindings; | 5 package org.chromium.mojo.bindings; |
6 | 6 |
7 import org.chromium.mojo.TestUtils; | 7 import org.chromium.mojo.TestUtils; |
8 import org.chromium.mojo.system.Handle; | 8 import org.chromium.mojo.system.Handle; |
9 import org.chromium.mojo.system.MojoException; | 9 import org.chromium.mojo.system.MojoException; |
10 import org.chromium.mojo.system.Pair; | 10 import org.chromium.mojo.system.Pair; |
11 | 11 |
12 import java.nio.ByteBuffer; | 12 import java.nio.ByteBuffer; |
13 import java.util.ArrayList; | 13 import java.util.ArrayList; |
14 import java.util.List; | 14 import java.util.List; |
15 | 15 |
16 /** | 16 /** |
17 * Utility class for bindings tests. | 17 * Utility class for bindings tests. |
18 */ | 18 */ |
19 public class BindingsTestUtils { | 19 public class BindingsTestUtils { |
20 | 20 |
21 /** | 21 /** |
22 * {@link MessageReceiver} that records any message it receives. | 22 * {@link MessageReceiver} that records any message it receives. |
23 */ | 23 */ |
24 public static class RecordingMessageReceiver extends SideEffectFreeCloseable | 24 public static class RecordingMessageReceiver implements MessageReceiver { |
25 implements MessageReceiver { | |
26 | 25 |
27 public final List<MessageWithHeader> messages = new ArrayList<MessageWit
hHeader>(); | 26 public final List<MessageWithHeader> messages = new ArrayList<MessageWit
hHeader>(); |
28 | 27 |
29 /** | 28 /** |
30 * @see MessageReceiver#accept(MessageWithHeader) | 29 * @see MessageReceiver#accept(MessageWithHeader) |
31 */ | 30 */ |
32 @Override | 31 @Override |
33 public boolean accept(MessageWithHeader message) { | 32 public boolean accept(MessageWithHeader message) { |
34 messages.add(message); | 33 messages.add(message); |
35 return true; | 34 return true; |
(...skipping 18 matching lines...) Expand all Loading... |
54 messagesWithReceivers.add(Pair.create(message, responder)); | 53 messagesWithReceivers.add(Pair.create(message, responder)); |
55 return true; | 54 return true; |
56 } | 55 } |
57 } | 56 } |
58 | 57 |
59 /** | 58 /** |
60 * {@link ConnectionErrorHandler} that records any error it received. | 59 * {@link ConnectionErrorHandler} that records any error it received. |
61 */ | 60 */ |
62 public static class CapturingErrorHandler implements ConnectionErrorHandler
{ | 61 public static class CapturingErrorHandler implements ConnectionErrorHandler
{ |
63 | 62 |
64 private MojoException mLastMojoException = null; | 63 public MojoException exception = null; |
65 | 64 |
66 /** | 65 /** |
67 * @see ConnectionErrorHandler#onConnectionError(MojoException) | 66 * @see ConnectionErrorHandler#onConnectionError(MojoException) |
68 */ | 67 */ |
69 @Override | 68 @Override |
70 public void onConnectionError(MojoException e) { | 69 public void onConnectionError(MojoException e) { |
71 mLastMojoException = e; | 70 exception = e; |
72 } | 71 } |
73 | |
74 /** | |
75 * Returns the last recorded exception. | |
76 */ | |
77 public MojoException getLastMojoException() { | |
78 return mLastMojoException; | |
79 } | |
80 | |
81 } | 72 } |
82 | 73 |
83 /** | 74 /** |
84 * Creates a new valid {@link MessageWithHeader}. | 75 * Creates a new valid {@link MessageWithHeader}. |
85 */ | 76 */ |
86 public static MessageWithHeader newRandomMessageWithHeader(int size) { | 77 public static MessageWithHeader newRandomMessageWithHeader(int size) { |
87 assert size > 16; | 78 assert size > 16; |
88 ByteBuffer message = TestUtils.newRandomBuffer(size); | 79 ByteBuffer message = TestUtils.newRandomBuffer(size); |
89 int[] headerAsInts = { 16, 2, 0, 0 }; | 80 int[] headerAsInts = { 16, 2, 0, 0 }; |
90 for (int i = 0; i < 4; ++i) { | 81 for (int i = 0; i < 4; ++i) { |
91 message.putInt(4 * i, headerAsInts[i]); | 82 message.putInt(4 * i, headerAsInts[i]); |
92 } | 83 } |
93 message.position(0); | 84 message.position(0); |
94 return new MessageWithHeader(new Message(message, new ArrayList<Handle>(
))); | 85 return new MessageWithHeader(new Message(message, new ArrayList<Handle>(
))); |
95 } | 86 } |
96 } | 87 } |
OLD | NEW |