| Index: net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java
|
| diff --git a/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java b/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java
|
| index 841a3e5a88fe5fa40621c45ce5a818dd3538b7d6..85608ea60d70973704efba0ebf7ec612806b73ce 100644
|
| --- a/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java
|
| +++ b/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java
|
| @@ -4,8 +4,14 @@
|
|
|
| package org.chromium.net.test;
|
|
|
| -import org.chromium.base.annotations.CalledByNative;
|
| -import org.chromium.base.annotations.JNINamespace;
|
| +import android.content.ComponentName;
|
| +import android.content.Context;
|
| +import android.content.Intent;
|
| +import android.content.ServiceConnection;
|
| +import android.os.IBinder;
|
| +import android.os.RemoteException;
|
| +
|
| +import org.chromium.base.Log;
|
|
|
| import java.io.File;
|
|
|
| @@ -25,17 +31,62 @@ import java.io.File;
|
| * s.shutdownAndWait();
|
| * s.destroy();
|
| */
|
| -@JNINamespace("net::test_server")
|
| public class EmbeddedTestServer {
|
| - private long mNativeEmbeddedTestServer;
|
| -
|
| - /** Create an uninitialized EmbeddedTestServer. */
|
| - public EmbeddedTestServer() {}
|
| + private static final String TAG = "cr_net_test";
|
| +
|
| + private static final String EMBEDDED_TEST_SERVER_SERVICE =
|
| + "org.chromium.net.test.EMBEDDED_TEST_SERVER_SERVICE";
|
| +
|
| + private IEmbeddedTestServerImpl mImpl;
|
| + private ServiceConnection mConn = new ServiceConnection() {
|
| + @Override
|
| + public void onServiceConnected(ComponentName name, IBinder service) {
|
| + synchronized (mImplMonitor) {
|
| + mImpl = IEmbeddedTestServerImpl.Stub.asInterface(service);
|
| + mImplMonitor.notify();
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void onServiceDisconnected(ComponentName name) {
|
| + synchronized (mImplMonitor) {
|
| + mImpl = null;
|
| + mImplMonitor.notify();
|
| + }
|
| + }
|
| + };
|
| +
|
| + private final Object mImplMonitor = new Object();
|
| +
|
| + /**
|
| + * Exception class raised on failure in the EmbeddedTestServer.
|
| + */
|
| + public static final class EmbeddedTestServerFailure extends Error {
|
| + public EmbeddedTestServerFailure(String errorDesc) {
|
| + super(errorDesc);
|
| + }
|
| + }
|
|
|
| - /** Initialize the native EmbeddedTestServer object. */
|
| - public void initializeNative() {
|
| - if (mNativeEmbeddedTestServer == 0) nativeInit();
|
| - assert mNativeEmbeddedTestServer != 0;
|
| + public void initializeNative(Context context) throws InterruptedException {
|
| + Intent intent = new Intent(EMBEDDED_TEST_SERVER_SERVICE);
|
| + intent.setClassName("org.chromium.chrome.test.support",
|
| + "org.chromium.net.test.EmbeddedTestServerService");
|
| + if (!context.bindService(intent, mConn, Context.BIND_AUTO_CREATE)) {
|
| + throw new EmbeddedTestServerFailure(
|
| + "Unable to bind to the EmbeddedTestServer service.");
|
| + }
|
| + synchronized (mImplMonitor) {
|
| + // TODO(jbudorick): Add a fatal timeout here.
|
| + while (mImpl == null) {
|
| + mImplMonitor.wait();
|
| + }
|
| + }
|
| + try {
|
| + mImpl.initializeNative();
|
| + } catch (RemoteException e) {
|
| + Log.e(TAG, "Failed to initialize native server.", e);
|
| + throw new EmbeddedTestServerFailure("Failed to initialize native server.");
|
| + }
|
| }
|
|
|
| /** Serve files from the provided directory.
|
| @@ -43,7 +94,7 @@ public class EmbeddedTestServer {
|
| * @param directory The directory from which files should be served.
|
| */
|
| public void serveFilesFromDirectory(File directory) {
|
| - nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directory.getPath());
|
| + serveFilesFromDirectory(directory.getPath());
|
| }
|
|
|
| /** Serve files from the provided directory.
|
| @@ -51,7 +102,12 @@ public class EmbeddedTestServer {
|
| * @param directoryPath The path of the directory from which files should be served.
|
| */
|
| public void serveFilesFromDirectory(String directoryPath) {
|
| - nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath);
|
| + try {
|
| + mImpl.serveFilesFromDirectory(directoryPath);
|
| + } catch (RemoteException e) {
|
| + throw new EmbeddedTestServerFailure(
|
| + "Failed to start serving files from " + directoryPath + ": " + e.toString());
|
| + }
|
| }
|
|
|
| // TODO(svaldez): Remove once all consumers have switched to start().
|
| @@ -73,7 +129,32 @@ public class EmbeddedTestServer {
|
| * @return Whether the server was successfully initialized.
|
| */
|
| public boolean start() {
|
| - return nativeStart(mNativeEmbeddedTestServer);
|
| + try {
|
| + return mImpl.start();
|
| + } catch (RemoteException e) {
|
| + throw new EmbeddedTestServerFailure("Failed to start server: " + e.toString());
|
| + }
|
| + }
|
| +
|
| + /** Create and initialize a server that serves files from the provided directory.
|
| + *
|
| + * This handles native object initialization, server configuration, and server initialization.
|
| + * On returning, the server is ready for use.
|
| + *
|
| + * @param context The context in which the server is being started.
|
| + * @param directory The directory from which files should be served.
|
| + * @return The created server.
|
| + */
|
| + public static EmbeddedTestServer createAndStartFileServer(Context context, File directory)
|
| + throws InterruptedException {
|
| + EmbeddedTestServer server = new EmbeddedTestServer();
|
| + server.initializeNative(context);
|
| + server.serveFilesFromDirectory(directory);
|
| + if (!server.start()) {
|
| + throw new EmbeddedTestServerFailure(
|
| + "Failed to start serving files from " + directory.getPath());
|
| + }
|
| + return server;
|
| }
|
|
|
| /** Get the full URL for the given relative URL.
|
| @@ -82,7 +163,12 @@ public class EmbeddedTestServer {
|
| * @return The URL as a String.
|
| */
|
| public String getURL(String relativeUrl) {
|
| - return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
|
| + try {
|
| + return mImpl.getURL(relativeUrl);
|
| + } catch (RemoteException e) {
|
| + throw new EmbeddedTestServerFailure(
|
| + "Failed to get URL for " + relativeUrl + ": " + e.toString());
|
| + }
|
| }
|
|
|
| /** Shutdown the server.
|
| @@ -90,33 +176,32 @@ public class EmbeddedTestServer {
|
| * @return Whether the server was successfully shut down.
|
| */
|
| public boolean shutdownAndWaitUntilComplete() {
|
| - return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer);
|
| + try {
|
| + return mImpl.shutdownAndWaitUntilComplete();
|
| + } catch (RemoteException e) {
|
| + throw new EmbeddedTestServerFailure("Failed to shut down: " + e.toString());
|
| + }
|
| }
|
|
|
| /** Destroy the native EmbeddedTestServer object. */
|
| public void destroy() {
|
| - assert mNativeEmbeddedTestServer != 0;
|
| - nativeDestroy(mNativeEmbeddedTestServer);
|
| - assert mNativeEmbeddedTestServer == 0;
|
| - }
|
| -
|
| - @CalledByNative
|
| - private void setNativePtr(long nativePtr) {
|
| - assert mNativeEmbeddedTestServer == 0;
|
| - mNativeEmbeddedTestServer = nativePtr;
|
| + try {
|
| + mImpl.destroy();
|
| + } catch (RemoteException e) {
|
| + throw new EmbeddedTestServerFailure("Failed to destroy native server: " + e.toString());
|
| + }
|
| }
|
|
|
| - @CalledByNative
|
| - private void clearNativePtr() {
|
| - assert mNativeEmbeddedTestServer != 0;
|
| - mNativeEmbeddedTestServer = 0;
|
| + /** Stop and destroy the provided server.
|
| + *
|
| + * This handles stopping the server and destroying the native object.
|
| + *
|
| + * @param server The server to stop and destroy.
|
| + */
|
| + public static void stopAndDestroyServer(EmbeddedTestServer server) {
|
| + if (!server.shutdownAndWaitUntilComplete()) {
|
| + throw new EmbeddedTestServerFailure("Failed to stop server.");
|
| + }
|
| + server.destroy();
|
| }
|
| -
|
| - 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);
|
| }
|
|
|