Chromium Code Reviews| Index: components/devtools_bridge/android/javatests/src/org/chromium/components/devtools_bridge/LocalTunnelBridgeTest.java |
| diff --git a/components/devtools_bridge/android/javatests/src/org/chromium/components/devtools_bridge/LocalTunnelBridgeTest.java b/components/devtools_bridge/android/javatests/src/org/chromium/components/devtools_bridge/LocalTunnelBridgeTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..25b2f2656fb4ce4ca43e301c5b465a5096c13fcc |
| --- /dev/null |
| +++ b/components/devtools_bridge/android/javatests/src/org/chromium/components/devtools_bridge/LocalTunnelBridgeTest.java |
| @@ -0,0 +1,107 @@ |
| +// 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 android.net.LocalServerSocket; |
| +import android.net.LocalSocket; |
| +import android.test.InstrumentationTestCase; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import junit.framework.Assert; |
| + |
| +import java.io.IOException; |
| +import java.util.concurrent.Future; |
| + |
| +/** |
| + * Tests for {@link SocketTunnelBridge} |
| + */ |
| +public class LocalTunnelBridgeTest extends InstrumentationTestCase { |
| + private static final String REQUEST = "Request"; |
| + private static final String RESPONSE = "Response"; |
| + |
| + private static final String SERVER_SOCKET_NAME = |
| + "org.chromium.components.devtools_bridge.LocalTunnelBridgeTest.SERVER_SOCKET"; |
| + private static final String CLIENT_SOCKET_NAME = |
| + "org.chromium.components.devtools_bridge.LocalTunnelBridgeTest.CLIENT_SOCKET"; |
| + |
| + private LocalTunnelBridge mBridge; |
| + private LocalServerSocket mServerSocket; |
| + |
| + @Override |
| + public void setUp() throws IOException { |
| + mServerSocket = new LocalServerSocket(SERVER_SOCKET_NAME); |
| + } |
| + |
| + private void startBridge() throws IOException { |
| + startBridge(SERVER_SOCKET_NAME); |
| + } |
| + |
| + private void startBridge(String serverSocketName) throws IOException { |
| + Assert.assertNull(mBridge); |
| + mBridge = new LocalTunnelBridge(serverSocketName, CLIENT_SOCKET_NAME); |
| + mBridge.start(); |
| + } |
| + |
| + @Override |
| + public void tearDown() throws IOException { |
| + if (mBridge != null) |
| + mBridge.dispose(); |
|
mnaganov (inactive)
2014/09/15 16:35:22
Should also reset the reference just in case.
SeRya
2014/09/15 18:16:21
Done.
|
| + mServerSocket.close(); |
| + } |
| + |
| + @SmallTest |
| + public void testStartStop() throws Exception { |
| + startBridge(); |
| + mBridge.stop(); |
| + } |
| + |
| + @SmallTest |
| + public void testRequestResponse() throws Exception { |
| + startBridge(); |
| + |
| + Future<String> response = TestUtils.asyncRequest(CLIENT_SOCKET_NAME, REQUEST); |
| + |
| + LocalSocket socket = mServerSocket.accept(); |
| + String request = TestUtils.readAll(socket); |
| + TestUtils.writeAndShutdown(socket, RESPONSE); |
| + |
| + Assert.assertEquals(REQUEST, request); |
| + |
| + Assert.assertEquals(RESPONSE, response.get()); |
| + socket.close(); |
| + |
| + mBridge.stop(); |
| + } |
| + |
| + @SmallTest |
| + public void testRequestFailure1() throws Exception { |
| + startBridge(); |
| + |
| + Future<String> response = TestUtils.asyncRequest(CLIENT_SOCKET_NAME, REQUEST); |
| + LocalSocket socket = mServerSocket.accept(); |
| + int firstByte = socket.getInputStream().read(); |
| + |
| + Assert.assertEquals((int) REQUEST.charAt(0), firstByte); |
| + |
| + socket.close(); |
| + |
| + Assert.assertEquals("", response.get()); |
| + |
| + mBridge.waitAllConnectionsClosed(); |
| + mBridge.stop(); |
| + } |
| + |
| + @SmallTest |
| + public void testRequestFailure2() throws Exception { |
| + startBridge("jdwp-control"); // Android system socket will reject connection. |
| + |
| + Future<String> response = TestUtils.asyncRequest(CLIENT_SOCKET_NAME, REQUEST); |
| + |
| + Assert.assertEquals("", response.get()); |
| + |
| + mBridge.waitAllConnectionsClosed(); |
| + mBridge.stop(); |
| + } |
| +} |