Index: components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/DataPipe.java |
diff --git a/components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/DataPipe.java b/components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/DataPipe.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c36319a897c3d8c2d6392642188de97893118c4b |
--- /dev/null |
+++ b/components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/DataPipe.java |
@@ -0,0 +1,79 @@ |
+// 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.components.devtools_bridge; |
+ |
+import java.nio.ByteBuffer; |
+ |
+/** |
+ * Represents a pair of connected AbstractDataChannel's. Sends to one channel |
+ * come to another and vice versa. |
+ */ |
+public class DataPipe { |
+ private final SignalingThreadMock mSignalingThread = new SignalingThreadMock(); |
+ |
+ private final PairedDataChannel mDC0 = new PairedDataChannel(); |
+ private final PairedDataChannel mDC1 = new PairedDataChannel(); |
+ |
+ public void connect() { |
+ mDC0.setPair(mDC1); |
+ mDC1.setPair(mDC0); |
+ mDC0.open(); |
+ mDC1.open(); |
+ } |
+ |
+ public void disconnect() { |
+ mDC0.setPair(null); |
+ mDC1.setPair(null); |
+ mDC0.close(); |
+ mDC1.close(); |
+ } |
+ |
+ public AbstractDataChannel dataChannel(int index) { |
+ switch (index) { |
+ case 0: |
+ return mDC0; |
+ case 1: |
+ return mDC1; |
+ default: |
+ throw new IllegalArgumentException("index"); |
+ } |
+ } |
+ |
+ public void dispose() { |
+ mDC0.dispose(); |
+ mDC1.dispose(); |
+ mSignalingThread.dispose(); |
+ } |
+ |
+ private class PairedDataChannel extends DataChannelMock { |
+ private PairedDataChannel mPair; |
+ |
+ public PairedDataChannel() { |
+ super(mSignalingThread); |
+ } |
+ |
+ public void setPair(final PairedDataChannel pair) { |
+ mSignalingThread.invoke(new Runnable() { |
+ @Override |
+ public void run() { |
+ mPair = pair; |
+ } |
+ }); |
+ } |
+ |
+ @Override |
+ protected void sendOnSignalingThread(ByteBuffer message) { |
+ assert message.remaining() > 0; |
+ |
+ if (mPair == null) return; |
+ mPair.notifyMessageOnSignalingThread(message); |
+ } |
+ |
+ @Override |
+ protected void disposeSignalingThread() { |
+ // Ignore. Will dispose in DataPipe.dispose. |
+ } |
+ } |
+} |