Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / devtools_bridge / android / javatests / src / org / chromium / components / devtools_bridge / DevToolsBridgeServerTest.java
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
5 package org.chromium.components.devtools_bridge;
6
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;
13
14 import junit.framework.Assert;
15
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.Future;
18
19 /**
20  * Tests for {@link DevToolsBridgeServer}
21  */
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;
29
30     public DevToolsBridgeServerTest() {
31         super(LocalBindingTestService.class);
32     }
33
34     private void invokeOnServiceThread(Runnable runnable) {
35         getService().invokeOnServiceThread(runnable);
36     }
37
38     private <T> T invokeOnServiceThread(Callable<T> callable) throws Exception {
39         return getService().invokeOnServiceThread(callable);
40     }
41
42     @Override
43     protected void setUp() throws Exception {
44         super.setUp();
45         setupService();
46         mFactory = new SessionDependencyFactory();
47     }
48
49     @Override
50     protected void tearDown() throws Exception {
51         invokeOnServiceThread(new Runnable() {
52             @Override
53             public void run() {
54                 shutdownService();
55             }
56         });
57         mFactory.dispose();
58         super.tearDown();
59     }
60
61     @SmallTest
62     public void testBind() throws Exception {
63         Assert.assertNotNull(bindService().getReceiver());
64     }
65
66     @SmallTest
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();
74
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());
81
82         clientSession.dispose();
83     }
84
85     private LocalBindingTestService.LocalBinder bindService() throws Exception {
86         IBinder binder = invokeOnServiceThread(new Callable<IBinder>() {
87             @Override
88             public IBinder call() {
89                 return bindService(new Intent(getContext(), LocalBindingTestService.class));
90             }
91         });
92         return (LocalBindingTestService.LocalBinder) binder;
93     }
94 }