summaryrefslogtreecommitdiffstats
path: root/net/test
diff options
context:
space:
mode:
authorjbudorick <jbudorick@chromium.org>2015-06-25 08:21:54 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-25 15:22:26 +0000
commitd58755d3bb7ead19d8ad909e4455b14857fe6d10 (patch)
tree4367efc337f1c604f9397ced5e59e7212eb34226 /net/test
parent6245bc32334b8bc1fe4d48b61dbbc461920ddb2b (diff)
downloadchromium_src-d58755d3bb7ead19d8ad909e4455b14857fe6d10.zip
chromium_src-d58755d3bb7ead19d8ad909e4455b14857fe6d10.tar.gz
chromium_src-d58755d3bb7ead19d8ad909e4455b14857fe6d10.tar.bz2
[Android] Add a Java version of net::test_server::EmbeddedTestServer.
Notably, this CL does not: - convert ChromeInstrumentationTestRunner's TestHttpServer to use this - convert cronet's NativeTestServer to use this Those can be done separately. BUG=488192 Review URL: https://codereview.chromium.org/1174613002 Cr-Commit-Position: refs/heads/master@{#336159}
Diffstat (limited to 'net/test')
-rw-r--r--net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java111
-rw-r--r--net/test/android/net_test_jni_onload.cc40
-rw-r--r--net/test/android/net_test_jni_onload.h19
-rw-r--r--net/test/embedded_test_server/android/embedded_test_server_android.cc71
-rw-r--r--net/test/embedded_test_server/android/embedded_test_server_android.h51
-rw-r--r--net/test/embedded_test_server/embedded_test_server.h2
6 files changed, 293 insertions, 1 deletions
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
new file mode 100644
index 0000000..06ab79e
--- /dev/null
+++ b/net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java
@@ -0,0 +1,111 @@
+// 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 org.chromium.base.CalledByNative;
+import org.chromium.base.JNINamespace;
+
+import java.io.File;
+
+/**
+ * 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.initializeAndWaitUntilReady()) {
+ * throw new SomeKindOfException("Unable to initialize EmbeddedTestServer.");
+ * }
+ *
+ * // serve requests...
+ *
+ * s.shutdownAndWait();
+ * s.destroy();
+ */
+@JNINamespace("net::test_server")
+public class EmbeddedTestServer {
+ private long mNativeEmbeddedTestServer;
+
+ /** Create an uninitialized EmbeddedTestServer. */
+ public EmbeddedTestServer() {}
+
+ /** Initialize the native EmbeddedTestServer object. */
+ public void initializeNative() {
+ if (mNativeEmbeddedTestServer == 0) nativeInit();
+ assert mNativeEmbeddedTestServer != 0;
+ }
+
+ /** Serve files from the provided directory.
+ *
+ * @param directory The directory from which files should be served.
+ */
+ public void serveFilesFromDirectory(File directory) {
+ nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directory.getPath());
+ }
+
+ /** Serve files from the provided directory.
+ *
+ * @param directoryPath The path of the directory from which files should be served.
+ */
+ public void serveFilesFromDirectory(String directoryPath) {
+ nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath);
+ }
+
+ /** Initialize the server.
+ *
+ * Note that this should be called after handlers are set up, including any relevant calls
+ * serveFilesFromDirectory.
+ *
+ * @return Whether the server was successfully initialized.
+ */
+ public boolean initializeAndWaitUntilReady() {
+ return nativeInitializeAndWaitUntilReady(mNativeEmbeddedTestServer);
+ }
+
+ /** Get the full URL for the given relative URL.
+ *
+ * @param relativeUrl The relative URL for which a full URL will be obtained.
+ * @return The URL as a String.
+ */
+ public String getURL(String relativeUrl) {
+ return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
+ }
+
+ /** Shutdown the server.
+ *
+ * @return Whether the server was successfully shut down.
+ */
+ public boolean shutdownAndWaitUntilComplete() {
+ return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer);
+ }
+
+ /** 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;
+ }
+
+ @CalledByNative
+ private void clearNativePtr() {
+ assert mNativeEmbeddedTestServer != 0;
+ mNativeEmbeddedTestServer = 0;
+ }
+
+ private native void nativeInit();
+ private native void nativeDestroy(long nativeEmbeddedTestServerAndroid);
+ private native boolean nativeInitializeAndWaitUntilReady(long nativeEmbeddedTestServerAndroid);
+ private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbeddedTestServerAndroid);
+ private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, String relativeUrl);
+ private native void nativeServeFilesFromDirectory(
+ long nativeEmbeddedTestServerAndroid, String directoryPath);
+}
diff --git a/net/test/android/net_test_jni_onload.cc b/net/test/android/net_test_jni_onload.cc
new file mode 100644
index 0000000..ac54b1f
--- /dev/null
+++ b/net/test/android/net_test_jni_onload.cc
@@ -0,0 +1,40 @@
+// 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.
+
+#include "net/test/android/net_test_jni_onload.h"
+
+#include "base/android/base_jni_onload.h"
+#include "base/bind.h"
+#include "net/test/embedded_test_server/android/embedded_test_server_android.h"
+
+namespace net {
+namespace test {
+
+namespace {
+
+bool RegisterJNI(JNIEnv* env) {
+ return net::test_server::EmbeddedTestServerAndroid::
+ RegisterEmbeddedTestServerAndroid(env);
+}
+
+bool Init() {
+ return true;
+}
+
+} // namesapce
+
+bool OnJNIOnLoadRegisterJNI(JavaVM* vm) {
+ std::vector<base::android::RegisterCallback> register_callbacks;
+ register_callbacks.push_back(base::Bind(&RegisterJNI));
+ return base::android::OnJNIOnLoadRegisterJNI(vm, register_callbacks);
+}
+
+bool OnJNIOnLoadInit() {
+ std::vector<base::android::InitCallback> init_callbacks;
+ init_callbacks.push_back(base::Bind(&Init));
+ return base::android::OnJNIOnLoadInit(init_callbacks);
+}
+
+} // namespace test
+} // namespace net
diff --git a/net/test/android/net_test_jni_onload.h b/net/test/android/net_test_jni_onload.h
new file mode 100644
index 0000000..67d139a
--- /dev/null
+++ b/net/test/android/net_test_jni_onload.h
@@ -0,0 +1,19 @@
+// 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.
+
+#ifndef NET_TEST_ANDROID_NET_TEST_JNI_ONLOAD_H_
+#define NET_TEST_ANDROID_NET_TEST_JNI_ONLOAD_H_
+
+#include <jni.h>
+
+namespace net {
+namespace test {
+
+bool OnJNIOnLoadRegisterJNI(JavaVM* vm);
+bool OnJNIOnLoadInit();
+
+} // namespace test
+} // namespace net
+
+#endif // NET_TEST_ANDROID_NET_TEST_JNI_ONLOAD_H_
diff --git a/net/test/embedded_test_server/android/embedded_test_server_android.cc b/net/test/embedded_test_server/android/embedded_test_server_android.cc
new file mode 100644
index 0000000..7cca9d9
--- /dev/null
+++ b/net/test/embedded_test_server/android/embedded_test_server_android.cc
@@ -0,0 +1,71 @@
+// 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.
+
+#include "net/test/embedded_test_server/android/embedded_test_server_android.h"
+
+#include "base/android/jni_string.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/bind.h"
+#include "base/files/file_path.h"
+#include "base/trace_event/trace_event.h"
+#include "net/test/jni/EmbeddedTestServer_jni.h"
+
+namespace net {
+namespace test_server {
+
+EmbeddedTestServerAndroid::EmbeddedTestServerAndroid(JNIEnv* env, jobject jobj)
+ : weak_java_server_(env, jobj), test_server_() {
+ Java_EmbeddedTestServer_setNativePtr(env, jobj,
+ reinterpret_cast<intptr_t>(this));
+}
+
+EmbeddedTestServerAndroid::~EmbeddedTestServerAndroid() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ Java_EmbeddedTestServer_clearNativePtr(env, weak_java_server_.get(env).obj());
+}
+
+jboolean EmbeddedTestServerAndroid::InitializeAndWaitUntilReady(JNIEnv* env,
+ jobject jobj) {
+ return test_server_.InitializeAndWaitUntilReady();
+}
+
+jboolean EmbeddedTestServerAndroid::ShutdownAndWaitUntilComplete(JNIEnv* env,
+ jobject jobj) {
+ return test_server_.ShutdownAndWaitUntilComplete();
+}
+
+base::android::ScopedJavaLocalRef<jstring> EmbeddedTestServerAndroid::GetURL(
+ JNIEnv* env,
+ jobject jobj,
+ jstring jrelative_url) const {
+ const GURL gurl(test_server_.GetURL(
+ base::android::ConvertJavaStringToUTF8(env, jrelative_url)));
+ return base::android::ConvertUTF8ToJavaString(env, gurl.spec());
+}
+
+void EmbeddedTestServerAndroid::ServeFilesFromDirectory(
+ JNIEnv* env,
+ jobject jobj,
+ jstring jdirectory_path) {
+ const base::FilePath directory(
+ base::android::ConvertJavaStringToUTF8(env, jdirectory_path));
+ test_server_.ServeFilesFromDirectory(directory);
+}
+
+void EmbeddedTestServerAndroid::Destroy(JNIEnv* env, jobject jobj) {
+ delete this;
+}
+
+static void Init(JNIEnv* env, jobject jobj) {
+ TRACE_EVENT0("native", "EmbeddedTestServerAndroid::Init");
+ new EmbeddedTestServerAndroid(env, jobj);
+}
+
+// static
+bool EmbeddedTestServerAndroid::RegisterEmbeddedTestServerAndroid(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace test_server
+} // namespace net
diff --git a/net/test/embedded_test_server/android/embedded_test_server_android.h b/net/test/embedded_test_server/android/embedded_test_server_android.h
new file mode 100644
index 0000000..fb03e37
--- /dev/null
+++ b/net/test/embedded_test_server/android/embedded_test_server_android.h
@@ -0,0 +1,51 @@
+// 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.
+
+#ifndef NET_TEST_EMBEDDED_TEST_SERVER_ANDROID_EMBEDDED_TEST_SERVER_ANDROID_H_
+#define NET_TEST_EMBEDDED_TEST_SERVER_ANDROID_EMBEDDED_TEST_SERVER_ANDROID_H_
+
+#include <jni.h>
+
+#include "base/android/jni_weak_ref.h"
+#include "base/android/scoped_java_ref.h"
+#include "net/test/embedded_test_server/embedded_test_server.h"
+#include "net/test/embedded_test_server/http_request.h"
+#include "net/test/embedded_test_server/http_response.h"
+
+namespace net {
+namespace test_server {
+
+// The C++ side of the Java EmbeddedTestServer.
+class EmbeddedTestServerAndroid {
+ public:
+ EmbeddedTestServerAndroid(JNIEnv* env, jobject obj);
+ ~EmbeddedTestServerAndroid();
+
+ void Destroy(JNIEnv* env, jobject obj);
+
+ jboolean InitializeAndWaitUntilReady(JNIEnv* env, jobject jobj);
+
+ jboolean ShutdownAndWaitUntilComplete(JNIEnv* env, jobject jobj);
+
+ base::android::ScopedJavaLocalRef<jstring>
+ GetURL(JNIEnv* jenv, jobject jobj, jstring jrelative_url) const;
+
+ void ServeFilesFromDirectory(JNIEnv* env,
+ jobject jobj,
+ jstring jdirectory_path);
+
+ static bool RegisterEmbeddedTestServerAndroid(JNIEnv* env);
+
+ private:
+ JavaObjectWeakGlobalRef weak_java_server_;
+
+ EmbeddedTestServer test_server_;
+
+ DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServerAndroid);
+};
+
+} // namespace test_server
+} // namespace net
+
+#endif // NET_TEST_EMBEDDED_TEST_SERVER_ANDROID_EMBEDDED_TEST_SERVER_ANDROID_H_
diff --git a/net/test/embedded_test_server/embedded_test_server.h b/net/test/embedded_test_server/embedded_test_server.h
index a49f95c..5051826 100644
--- a/net/test/embedded_test_server/embedded_test_server.h
+++ b/net/test/embedded_test_server/embedded_test_server.h
@@ -206,7 +206,7 @@ class EmbeddedTestServer : public StreamListenSocket::Delegate {
DISALLOW_COPY_AND_ASSIGN(EmbeddedTestServer);
};
-} // namespace test_servers
+} // namespace test_server
} // namespace net
#endif // NET_TEST_EMBEDDED_TEST_SERVER_EMBEDDED_TEST_SERVER_H_