diff options
author | jbudorick <jbudorick@chromium.org> | 2015-06-25 08:21:54 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-25 15:22:26 +0000 |
commit | d58755d3bb7ead19d8ad909e4455b14857fe6d10 (patch) | |
tree | 4367efc337f1c604f9397ced5e59e7212eb34226 /net/test/android | |
parent | 6245bc32334b8bc1fe4d48b61dbbc461920ddb2b (diff) | |
download | chromium_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/android')
-rw-r--r-- | net/test/android/javatests/src/org/chromium/net/test/EmbeddedTestServer.java | 111 | ||||
-rw-r--r-- | net/test/android/net_test_jni_onload.cc | 40 | ||||
-rw-r--r-- | net/test/android/net_test_jni_onload.h | 19 |
3 files changed, 170 insertions, 0 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_ |