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

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: +landmine for GN 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 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..edec8bec672a73f2d1f14f26a30647bacf2d4ced
--- /dev/null
+++ b/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServerImpl.java
@@ -0,0 +1,170 @@
+// 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 running a net::test_server::EmbeddedTestServer.
+ *
+ * This should not be used directly. Use {@link EmbeddedTestServer} instead.
+ */
+@JNINamespace("net::test_server")
+public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
+ private static final String TAG = "cr_TestServer";
+
+ 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;
+ }
+
+ /** Initialize the native EmbeddedTestServer object. */
+ @Override
+ public void initializeNative() {
+ TestLibraryLoader.loadLibraries();
+
+ mHandlerThread = new HandlerThread("EmbeddedTestServer" + sCount.getAndIncrement());
+ mHandlerThread.start();
+ mHandler = new Handler(mHandlerThread.getLooper());
+
+ runOnHandlerThread(new Callable<Void>() {
+ @Override
+ public Void call() {
+ if (mNativeEmbeddedTestServer == 0) nativeInit();
+ assert mNativeEmbeddedTestServer != 0;
+ 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.
+ */
+ @Override
+ public boolean start() {
+ return runOnHandlerThread(new Callable<Boolean>() {
+ @Override
+ public Boolean call() {
+ return nativeStart(mNativeEmbeddedTestServer);
+ }
+ });
+ }
+
+ /** Serve files from the provided directory.
+ *
+ * @param directoryPath The path of the directory from which files should be served.
+ */
+ @Override
+ public void serveFilesFromDirectory(final String directoryPath) {
+ runOnHandlerThread(new Callable<Void>() {
+ @Override
+ public Void call() {
+ nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath);
+ return null;
+ }
+ });
+ }
+
+ /** 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.
+ */
+ @Override
+ public String getURL(final String relativeUrl) {
+ return runOnHandlerThread(new Callable<String>() {
+ @Override
+ public String call() {
+ return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
+ }
+ });
+ }
+
+ /** Shut down the server.
+ *
+ * @return Whether the server was successfully shut down.
+ */
+ @Override
+ public boolean shutdownAndWaitUntilComplete() {
+ return runOnHandlerThread(new Callable<Boolean>() {
+ @Override
+ public Boolean call() {
+ return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer);
+ }
+ });
+ }
+
+ /** Destroy the native EmbeddedTestServer object. */
+ @Override
+ public void destroy() {
+ runOnHandlerThread(new Callable<Void>() {
+ @Override
+ public Void call() {
+ 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