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.
5 package org.chromium.components.devtools_bridge;
7 import android.content.Intent;
8 import android.net.LocalServerSocket;
9 import android.net.LocalSocket;
10 import android.os.IBinder;
11 import android.test.ServiceTestCase;
12 import android.test.suitebuilder.annotation.SmallTest;
14 import junit.framework.Assert;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.Future;
20 * Tests for {@link DevToolsBridgeServer}
22 public class DevToolsBridgeServerTest extends ServiceTestCase<LocalBindingTestService> {
23 private static final String REQUEST = "Request";
24 private static final String RESPONSE = "Response";
25 private static final String SESSION_ID = "SESSION_ID";
26 private static final String CLIENT_SOCKET_NAME =
27 DevToolsBridgeServerTest.class.getName() + ".CLIENT_SOCKET_NAME";
28 private SessionDependencyFactory mFactory;
30 public DevToolsBridgeServerTest() {
31 super(LocalBindingTestService.class);
34 private void invokeOnServiceThread(Runnable runnable) {
35 getService().invokeOnServiceThread(runnable);
38 private <T> T invokeOnServiceThread(Callable<T> callable) throws Exception {
39 return getService().invokeOnServiceThread(callable);
43 protected void setUp() throws Exception {
46 mFactory = new SessionDependencyFactory();
50 protected void tearDown() throws Exception {
51 invokeOnServiceThread(new Runnable() {
62 public void testBind() throws Exception {
63 Assert.assertNotNull(bindService().getReceiver());
67 public void testRequestResponse() throws Exception {
68 LocalBindingTestService.LocalBinder binder = bindService();
69 LocalServerSocket serverListeningSocket = new LocalServerSocket(binder.socketName());
70 ClientSessionTestingHost clientSession = new ClientSessionTestingHost(
71 mFactory, binder.getReceiver(), binder.createExecutor(),
72 SESSION_ID, CLIENT_SOCKET_NAME);
73 clientSession.start();
75 Future<String> response = TestUtils.asyncRequest(CLIENT_SOCKET_NAME, REQUEST);
76 LocalSocket serverSocket = serverListeningSocket.accept();
77 String request = TestUtils.readAll(serverSocket);
78 Assert.assertEquals(REQUEST, request);
79 TestUtils.writeAndShutdown(serverSocket, RESPONSE);
80 Assert.assertEquals(RESPONSE, response.get());
82 clientSession.dispose();
85 private LocalBindingTestService.LocalBinder bindService() throws Exception {
86 IBinder binder = invokeOnServiceThread(new Callable<IBinder>() {
88 public IBinder call() {
89 return bindService(new Intent(getContext(), LocalBindingTestService.class));
92 return (LocalBindingTestService.LocalBinder) binder;