From 20615a2c15a3b7e0a002d98146d25f29c8631ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 21 Dec 2016 12:56:34 +0100 Subject: [android] improve test runner by allowing selective execution of tests --- platform/android/src/test/Main.java | 4 +-- platform/android/src/test/main.jni.cpp | 53 +++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 16 deletions(-) (limited to 'platform/android/src/test') diff --git a/platform/android/src/test/Main.java b/platform/android/src/test/Main.java index 2abcf2bfdb..b6f540f666 100644 --- a/platform/android/src/test/Main.java +++ b/platform/android/src/test/Main.java @@ -1,13 +1,13 @@ public class Main { - public native void runAllTests(); + public native void runAllTests(String[] args); public static void main(String[] args) throws Exception { //Load the tests System.loadLibrary("mbgl-test"); //Run the tests - new Main().runAllTests(); + new Main().runAllTests(args); //Exit explicitly otherwise dalvikvm won't quit System.exit(0); diff --git a/platform/android/src/test/main.jni.cpp b/platform/android/src/test/main.jni.cpp index d37b908202..f79d7671cb 100644 --- a/platform/android/src/test/main.jni.cpp +++ b/platform/android/src/test/main.jni.cpp @@ -2,25 +2,51 @@ #include #include +#include #include #include +#include + #pragma clang diagnostic ignored "-Wunused-parameter" #define MAKE_NATIVE_METHOD(name, sig) jni::MakeNativeMethod( #name, sig ) namespace { -/** - * JNI Bound to Main#runAllTests() - */ -void runAllTests(JNIEnv *env, jni::jobject* obj) { - mbgl::Log::Info(mbgl::Event::JNI, "Starting tests"); - mbgl::runTests(0, nullptr); -} +// Main class (entry point for tests from dalvikvm) +struct Main { + static constexpr auto Name() { + return "Main"; + } -} + /** + * JNI Bound to Main#runAllTests() + */ + static void runAllTests(jni::JNIEnv& env, jni::Object
, jni::Array args) { + mbgl::Log::Warning(mbgl::Event::JNI, "Starting tests"); + + // We need to create a copy of the argv data since Java-internals are stored in UTF-16. + std::vector data; + // Add a fake first argument to align indices. Google Test expects the first argument to + // start at index 1; index 0 is the name of the executable. + data.push_back("main.jar"); + const int argc = args.Length(env); + for (auto i = 0; i < argc; i++) { + data.emplace_back(jni::Make(env, args.Get(env, i))); + } + + // Create an array of char pointers that point back to the data array. + std::vector argv; + for (const auto& arg : data) { + argv.push_back(arg.data()); + } + mbgl::runTests(argv.size(), const_cast(argv.data())); + } +}; + +} // namespace // JNI Bindings to stub the android.util.Log implementation @@ -47,20 +73,19 @@ static jint logger_entry_max_payload_native(JNIEnv* env, jni::jobject* clazz) { // Main entry point extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { - //Load the main library jni bindings + // Load the main library jni bindings mbgl::Log::Info(mbgl::Event::JNI, "Registering main JNI Methods"); mbgl::android::registerNatives(vm); - //Load the test library jni bindings + // Load the test library jni bindings mbgl::Log::Info(mbgl::Event::JNI, "Registering test JNI Methods"); jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6); - //Main class (entry point for tests from dalvikvm) - struct Main { static constexpr auto Name() { return "Main"; } }; - jni::RegisterNatives(env, jni::Class
::Find(env), MAKE_NATIVE_METHOD(runAllTests, "()V")); + jni::RegisterNatives(env, jni::Class
::Find(env), + jni::MakeNativeMethod("runAllTests")); - //Bindings for system classes + // Bindings for system classes struct Log { static constexpr auto Name() { return "android/util/Log"; } }; try { jni::RegisterNatives(env, jni::Class::Find(env), -- cgit v1.2.1