DataChannelMock.java revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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 5package org.chromium.components.devtools_bridge; 6 7import junit.framework.Assert; 8 9import java.nio.ByteBuffer; 10import java.util.concurrent.LinkedBlockingQueue; 11 12/** 13 * Mock of AbstractDataChannel tests. Also base class for DataPipe channels. 14 */ 15public class DataChannelMock extends AbstractDataChannel { 16 private final SignalingThreadMock mSignalingThread; 17 private Observer mObserver; 18 private boolean mOpen = false; 19 private final LinkedBlockingQueue<ByteBuffer> mQueue = new LinkedBlockingQueue<ByteBuffer>(); 20 21 // |signalingThread| will be disposed in the |dispose| method. If successor needs 22 // to control it's lifetime it must override |disposeSignalingThread| and not to invoke super's 23 // implementation. 24 protected DataChannelMock(SignalingThreadMock signalingThread) { 25 mSignalingThread = signalingThread; 26 } 27 28 public DataChannelMock() { 29 this(new SignalingThreadMock()); 30 } 31 32 public void open() { 33 onStateChange(AbstractDataChannel.State.OPEN); 34 } 35 36 @Override 37 public void close() { 38 onStateChange(AbstractDataChannel.State.CLOSED); 39 } 40 41 private void onStateChange(final State state) { 42 mSignalingThread.invoke(new Runnable() { 43 @Override 44 public void run() { 45 mObserver.onStateChange(state); 46 } 47 }); 48 } 49 50 // Sends onMessage to the observer. 51 public void notifyMessage(ByteBuffer data) { 52 final byte[] bytes = toByteArray(data); 53 mSignalingThread.invoke(new Runnable() { 54 @Override 55 public void run() { 56 notifyMessageOnSignalingThread(ByteBuffer.wrap(bytes)); 57 } 58 }); 59 } 60 61 protected void notifyMessageOnSignalingThread(ByteBuffer buffer) { 62 mObserver.onMessage(buffer); 63 } 64 65 // Blocks until message received. Removes it from the queue and returns. 66 public ByteBuffer receive() { 67 try { 68 return mQueue.take(); 69 } catch (InterruptedException e) { 70 throw new RuntimeException(e); 71 } 72 } 73 74 @Override 75 public void registerObserver(final Observer observer) { 76 mSignalingThread.invoke(new Runnable() { 77 @Override 78 public void run() { 79 Assert.assertNull(mObserver); 80 mObserver = observer; 81 Assert.assertNotNull(mObserver); 82 } 83 }); 84 } 85 86 @Override 87 public void unregisterObserver() { 88 mSignalingThread.invoke(new Runnable() { 89 @Override 90 public void run() { 91 Assert.assertNotNull(mObserver); 92 mObserver = null; 93 } 94 }); 95 } 96 97 private byte[] toByteArray(ByteBuffer data) { 98 final byte[] result = new byte[data.remaining()]; 99 data.get(result); 100 return result; 101 } 102 103 @Override 104 public void send(ByteBuffer message, MessageType type) { 105 final byte[] data = toByteArray(message); 106 assert data.length > 0; 107 mSignalingThread.post(new Runnable() { 108 @Override 109 public void run() { 110 sendOnSignalingThread(ByteBuffer.wrap(data)); 111 android.util.Log.d("DataChannelMock", "Packet sent."); 112 } 113 }); 114 } 115 116 protected void sendOnSignalingThread(ByteBuffer message) { 117 boolean success = mQueue.offer(message); 118 assert success; 119 } 120 121 @Override 122 public void dispose() { 123 mSignalingThread.invoke(new Runnable() { 124 @Override 125 public void run() { 126 Assert.assertNull(mObserver); 127 } 128 }); 129 disposeSignalingThread(); 130 } 131 132 protected void disposeSignalingThread() { 133 mSignalingThread.dispose(); 134 } 135} 136