Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServerImpl.java

Issue 1465383003: [Android] Add ChromiumNetTestSupport.apk for the java EmbeddedTestServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: agrieve comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.net.test;
6
7 import android.os.Handler;
8 import android.os.HandlerThread;
9
10 import org.chromium.base.Log;
11 import org.chromium.base.annotations.CalledByNative;
12 import org.chromium.base.annotations.JNINamespace;
13 import org.chromium.base.test.library_loader.TestLibraryLoader;
14
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.FutureTask;
18 import java.util.concurrent.atomic.AtomicInteger;
19
20 /**
21 * Java bindings for the native embedded test server.
22 *
23 * An example use:
24 * EmbeddedTestServer s = new EmbeddedTestServer();
25 * s.initializeNative();
26 * s.serveFilesFromDirectory("/path/to/my/directory");
27 * if (!s.start()) {
28 * throw new SomeKindOfException("Unable to initialize EmbeddedTestServer. ");
29 * }
30 *
31 * // serve requests...
32 *
33 * s.shutdownAndWait();
34 * s.destroy();
35 */
36 @JNINamespace("net::test_server")
37 public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
38 private static final String TAG = "cr_net_test";
39
40 private static AtomicInteger sCount = new AtomicInteger();
41
42 private Handler mHandler;
43 private HandlerThread mHandlerThread;
44 private long mNativeEmbeddedTestServer;
45
46 /** Create an uninitialized EmbeddedTestServer. */
47 public EmbeddedTestServerImpl() {}
48
49 private <V> V runOnHandlerThread(Callable<V> c) {
50 FutureTask<V> t = new FutureTask<V>(c);
51 mHandler.post(t);
52 try {
53 return t.get();
54 } catch (ExecutionException e) {
55 Log.e(TAG, "Exception raised from native EmbeddedTestServer", e);
56 } catch (InterruptedException e) {
57 Log.e(TAG, "Interrupted while waiting for native EmbeddedTestServer" , e);
58 return null;
59 }
60 return null;
61 }
62
63 /** Initialize the native EmbeddedTestServer object. */
64 public void initializeNative() {
65 mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndI ncrement());
Yaron 2015/11/24 19:06:28 Wondering why you need a dedicated handler thread?
jbudorick 2015/11/25 02:43:40 The embedded test server checks what thread it's o
66 mHandlerThread.start();
67 mHandler = new Handler(mHandlerThread.getLooper());
68
69 runOnHandlerThread(new Callable<Void>() {
70 @Override
71 public Void call() {
72 Log.i(TAG, "initializeNative thread ID: %d", Thread.currentThrea d().getId());
73 TestLibraryLoader.loadLibraries();
74 if (mNativeEmbeddedTestServer == 0) nativeInit();
75 assert mNativeEmbeddedTestServer != 0;
76 return null;
77 }
78 });
79 }
80
81 /** Serve files from the provided directory.
82 *
83 * @param directoryPath The path of the directory from which files should b e served.
84 */
85 public void serveFilesFromDirectory(final String directoryPath) {
86 runOnHandlerThread(new Callable<Void>() {
87 @Override
88 public Void call() {
89 Log.i(TAG, "serveFilesFromDirectory thread ID: %d", Thread.curre ntThread().getId());
90 nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directo ryPath);
91 return null;
92 }
93 });
94 }
95
96 /** Starts the server.
97 *
98 * Note that this should be called after handlers are set up, including any relevant calls
99 * serveFilesFromDirectory.
100 *
101 * @return Whether the server was successfully started.
102 */
103 public boolean start() {
104 return runOnHandlerThread(new Callable<Boolean>() {
105 @Override
106 public Boolean call() {
107 Log.i(TAG, "start thread ID: %d", Thread.currentThread().getId() );
108 return nativeStart(mNativeEmbeddedTestServer);
109 }
110 });
111 }
112
113 /** Get the full URL for the given relative URL.
114 *
115 * @param relativeUrl The relative URL for which a full URL should be retur ned.
116 * @return The URL as a String.
117 */
118 public String getURL(final String relativeUrl) {
119 return runOnHandlerThread(new Callable<String>() {
120 @Override
121 public String call() {
122 Log.i(TAG, "getURL thread ID: %d", Thread.currentThread().getId( ));
123 return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
124 }
125 });
126 }
127
128 /** Shut down the server.
129 *
130 * @return Whether the server was successfully shut down.
131 */
132 public boolean shutdownAndWaitUntilComplete() {
133 return runOnHandlerThread(new Callable<Boolean>() {
134 @Override
135 public Boolean call() {
136 Log.i(TAG, "shutdownAndWaitUntilComplete thread ID: %d",
137 Thread.currentThread().getId());
138 return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestSer ver);
139 }
140 });
141 }
142
143 /** Destroy the native EmbeddedTestServer object. */
144 public void destroy() {
145 runOnHandlerThread(new Callable<Void>() {
146 @Override
147 public Void call() {
148 Log.i(TAG, "destroy thread ID: %d", Thread.currentThread().getId ());
149 assert mNativeEmbeddedTestServer != 0;
150 nativeDestroy(mNativeEmbeddedTestServer);
151 assert mNativeEmbeddedTestServer == 0;
152 return null;
153 }
154 });
155
156 mHandlerThread.quitSafely();
157 try {
158 mHandlerThread.join();
159 } catch (InterruptedException e) {
160 }
161 }
162
163 @CalledByNative
164 private void setNativePtr(long nativePtr) {
165 assert mNativeEmbeddedTestServer == 0;
166 mNativeEmbeddedTestServer = nativePtr;
167 }
168
169 @CalledByNative
170 private void clearNativePtr() {
171 assert mNativeEmbeddedTestServer != 0;
172 mNativeEmbeddedTestServer = 0;
173 }
174
175 private native void nativeInit();
176 private native void nativeDestroy(long nativeEmbeddedTestServerAndroid);
177 private native boolean nativeStart(long nativeEmbeddedTestServerAndroid);
178 private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbedde dTestServerAndroid);
179 private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, Str ing relativeUrl);
180 private native void nativeServeFilesFromDirectory(
181 long nativeEmbeddedTestServerAndroid, String directoryPath);
182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698