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

Unified 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, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServerImpl.java
diff --git a/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServerImpl.java b/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServerImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..9eb9cb3afe762d80093c1c677297f22748edd99a
--- /dev/null
+++ b/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServerImpl.java
@@ -0,0 +1,182 @@
+// Copyright 2015 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.net.test;
+
+import android.os.Handler;
+import android.os.HandlerThread;
+
+import org.chromium.base.Log;
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+import org.chromium.base.test.library_loader.TestLibraryLoader;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Java bindings for the native embedded test server.
+ *
+ * An example use:
+ * EmbeddedTestServer s = new EmbeddedTestServer();
+ * s.initializeNative();
+ * s.serveFilesFromDirectory("/path/to/my/directory");
+ * if (!s.start()) {
+ * throw new SomeKindOfException("Unable to initialize EmbeddedTestServer.");
+ * }
+ *
+ * // serve requests...
+ *
+ * s.shutdownAndWait();
+ * s.destroy();
+ */
+@JNINamespace("net::test_server")
+public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
+ private static final String TAG = "cr_net_test";
+
+ private static AtomicInteger sCount = new AtomicInteger();
+
+ private Handler mHandler;
+ private HandlerThread mHandlerThread;
+ private long mNativeEmbeddedTestServer;
+
+ /** Create an uninitialized EmbeddedTestServer. */
+ public EmbeddedTestServerImpl() {}
+
+ private <V> V runOnHandlerThread(Callable<V> c) {
+ FutureTask<V> t = new FutureTask<V>(c);
+ mHandler.post(t);
+ try {
+ return t.get();
+ } catch (ExecutionException e) {
+ Log.e(TAG, "Exception raised from native EmbeddedTestServer", e);
+ } catch (InterruptedException e) {
+ Log.e(TAG, "Interrupted while waiting for native EmbeddedTestServer", e);
+ return null;
+ }
+ return null;
+ }
+
+ /** Initialize the native EmbeddedTestServer object. */
+ public void initializeNative() {
+ mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndIncrement());
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
+ mHandlerThread.start();
+ mHandler = new Handler(mHandlerThread.getLooper());
+
+ runOnHandlerThread(new Callable<Void>() {
+ @Override
+ public Void call() {
+ Log.i(TAG, "initializeNative thread ID: %d", Thread.currentThread().getId());
+ TestLibraryLoader.loadLibraries();
+ if (mNativeEmbeddedTestServer == 0) nativeInit();
+ assert mNativeEmbeddedTestServer != 0;
+ return null;
+ }
+ });
+ }
+
+ /** Serve files from the provided directory.
+ *
+ * @param directoryPath The path of the directory from which files should be served.
+ */
+ public void serveFilesFromDirectory(final String directoryPath) {
+ runOnHandlerThread(new Callable<Void>() {
+ @Override
+ public Void call() {
+ Log.i(TAG, "serveFilesFromDirectory thread ID: %d", Thread.currentThread().getId());
+ nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath);
+ return null;
+ }
+ });
+ }
+
+ /** Starts the server.
+ *
+ * Note that this should be called after handlers are set up, including any relevant calls
+ * serveFilesFromDirectory.
+ *
+ * @return Whether the server was successfully started.
+ */
+ public boolean start() {
+ return runOnHandlerThread(new Callable<Boolean>() {
+ @Override
+ public Boolean call() {
+ Log.i(TAG, "start thread ID: %d", Thread.currentThread().getId());
+ return nativeStart(mNativeEmbeddedTestServer);
+ }
+ });
+ }
+
+ /** Get the full URL for the given relative URL.
+ *
+ * @param relativeUrl The relative URL for which a full URL should be returned.
+ * @return The URL as a String.
+ */
+ public String getURL(final String relativeUrl) {
+ return runOnHandlerThread(new Callable<String>() {
+ @Override
+ public String call() {
+ Log.i(TAG, "getURL thread ID: %d", Thread.currentThread().getId());
+ return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
+ }
+ });
+ }
+
+ /** Shut down the server.
+ *
+ * @return Whether the server was successfully shut down.
+ */
+ public boolean shutdownAndWaitUntilComplete() {
+ return runOnHandlerThread(new Callable<Boolean>() {
+ @Override
+ public Boolean call() {
+ Log.i(TAG, "shutdownAndWaitUntilComplete thread ID: %d",
+ Thread.currentThread().getId());
+ return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer);
+ }
+ });
+ }
+
+ /** Destroy the native EmbeddedTestServer object. */
+ public void destroy() {
+ runOnHandlerThread(new Callable<Void>() {
+ @Override
+ public Void call() {
+ Log.i(TAG, "destroy thread ID: %d", Thread.currentThread().getId());
+ assert mNativeEmbeddedTestServer != 0;
+ nativeDestroy(mNativeEmbeddedTestServer);
+ assert mNativeEmbeddedTestServer == 0;
+ return null;
+ }
+ });
+
+ mHandlerThread.quitSafely();
+ try {
+ mHandlerThread.join();
+ } catch (InterruptedException e) {
+ }
+ }
+
+ @CalledByNative
+ private void setNativePtr(long nativePtr) {
+ assert mNativeEmbeddedTestServer == 0;
+ mNativeEmbeddedTestServer = nativePtr;
+ }
+
+ @CalledByNative
+ private void clearNativePtr() {
+ assert mNativeEmbeddedTestServer != 0;
+ mNativeEmbeddedTestServer = 0;
+ }
+
+ private native void nativeInit();
+ private native void nativeDestroy(long nativeEmbeddedTestServerAndroid);
+ private native boolean nativeStart(long nativeEmbeddedTestServerAndroid);
+ private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbeddedTestServerAndroid);
+ private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, String relativeUrl);
+ private native void nativeServeFilesFromDirectory(
+ long nativeEmbeddedTestServerAndroid, String directoryPath);
+}

Powered by Google App Engine
This is Rietveld 408576698