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

Unified Diff: net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java

Issue 1526183002: Revert of [Android] Add ChromiumNetTestSupport.apk for the java EmbeddedTestServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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/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 ef4410482409cc66f36e8399f9c36a8ed82695ec..9f974ce9a2a8978e8db5fed2cc5d4d5829e487df 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,18 +4,13 @@
package org.chromium.net.test;
-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 org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
import java.io.File;
-/** A simple file server for java tests.
+/**
+ * Java bindings for the native embedded test server.
*
* An example use:
* EmbeddedTestServer s = new EmbeddedTestServer();
@@ -26,76 +21,21 @@
* }
*
* // serve requests...
- * s.getURL("/foo/bar.txt");
*
* s.shutdownAndWait();
* s.destroy();
- *
- * Note that this runs net::test_server::EmbeddedTestServer in a service in a separate APK.
*/
+@JNINamespace("net::test_server")
public class EmbeddedTestServer {
- private static final String TAG = "cr_TestServer";
+ private long mNativeEmbeddedTestServer;
- private static final String EMBEDDED_TEST_SERVER_SERVICE =
- "org.chromium.net.test.EMBEDDED_TEST_SERVER_SERVICE";
- private static final long SERVICE_CONNECTION_WAIT_INTERVAL_MS = 5000;
+ /** Create an uninitialized EmbeddedTestServer. */
+ public EmbeddedTestServer() {}
- 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);
- }
-
- public EmbeddedTestServerFailure(String errorDesc, Throwable cause) {
- super(errorDesc, cause);
- }
- }
-
- public void initializeNative(Context context) throws InterruptedException {
- Intent intent = new Intent(EMBEDDED_TEST_SERVER_SERVICE);
- intent.setClassName(
- "org.chromium.net.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) {
- Log.i(TAG, "Waiting for EmbeddedTestServer service connection.");
- while (mImpl == null) {
- mImplMonitor.wait(SERVICE_CONNECTION_WAIT_INTERVAL_MS);
- Log.i(TAG, "Still waiting for EmbeddedTestServer service connection.");
- }
- Log.i(TAG, "EmbeddedTestServer service connected.");
- try {
- mImpl.initializeNative();
- } catch (RemoteException e) {
- Log.e(TAG, "Failed to initialize native server.", e);
- throw new EmbeddedTestServerFailure("Failed to initialize native server.");
- }
- }
+ /** Initialize the native EmbeddedTestServer object. */
+ public void initializeNative() {
+ if (mNativeEmbeddedTestServer == 0) nativeInit();
+ assert mNativeEmbeddedTestServer != 0;
}
/** Serve files from the provided directory.
@@ -103,7 +43,7 @@
* @param directory The directory from which files should be served.
*/
public void serveFilesFromDirectory(File directory) {
- serveFilesFromDirectory(directory.getPath());
+ nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directory.getPath());
}
/** Serve files from the provided directory.
@@ -111,21 +51,7 @@
* @param directoryPath The path of the directory from which files should be served.
*/
public void serveFilesFromDirectory(String directoryPath) {
- try {
- synchronized (mImplMonitor) {
- checkServiceLocked();
- mImpl.serveFilesFromDirectory(directoryPath);
- }
- } catch (RemoteException e) {
- throw new EmbeddedTestServerFailure(
- "Failed to start serving files from " + directoryPath + ": " + e.toString());
- }
- }
-
- private void checkServiceLocked() {
- if (mImpl == null) {
- throw new EmbeddedTestServerFailure("Service disconnected.");
- }
+ nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath);
}
/** Starts the server.
@@ -136,35 +62,7 @@
* @return Whether the server was successfully initialized.
*/
public boolean start() {
- try {
- synchronized (mImplMonitor) {
- checkServiceLocked();
- return mImpl.start();
- }
- } catch (RemoteException e) {
- throw new EmbeddedTestServerFailure("Failed to start server.", e);
- }
- }
-
- /** 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;
+ return nativeStart(mNativeEmbeddedTestServer);
}
/** Get the full URL for the given relative URL.
@@ -173,14 +71,7 @@
* @return The URL as a String.
*/
public String getURL(String relativeUrl) {
- try {
- synchronized (mImplMonitor) {
- checkServiceLocked();
- return mImpl.getURL(relativeUrl);
- }
- } catch (RemoteException e) {
- throw new EmbeddedTestServerFailure("Failed to get URL for " + relativeUrl, e);
- }
+ return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
}
/** Shutdown the server.
@@ -188,40 +79,33 @@
* @return Whether the server was successfully shut down.
*/
public boolean shutdownAndWaitUntilComplete() {
- try {
- synchronized (mImplMonitor) {
- checkServiceLocked();
- return mImpl.shutdownAndWaitUntilComplete();
- }
- } catch (RemoteException e) {
- throw new EmbeddedTestServerFailure("Failed to shut down.", e);
- }
+ return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer);
}
/** Destroy the native EmbeddedTestServer object. */
- public void destroy(Context context) {
- try {
- synchronized (mImplMonitor) {
- checkServiceLocked();
- mImpl.destroy();
- }
- } catch (RemoteException e) {
- throw new EmbeddedTestServerFailure("Failed to destroy native server.", e);
- } finally {
- context.unbindService(mConn);
- }
+ public void destroy() {
+ assert mNativeEmbeddedTestServer != 0;
+ nativeDestroy(mNativeEmbeddedTestServer);
+ assert 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, Context context) {
- if (!server.shutdownAndWaitUntilComplete()) {
- throw new EmbeddedTestServerFailure("Failed to stop server.");
- }
- server.destroy(context);
+ @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