From 5e7e2d23e7a75a08d65ca670591594f4c7be2138 Mon Sep 17 00:00:00 2001 From: Juha Alanen Date: Thu, 19 Dec 2019 15:26:18 +0200 Subject: [android] Share common code between test runners --- platform/android/src/test/benchmark_runner.cpp | 238 +------------------- platform/android/src/test/collator_test_stub.cpp | 34 +++ .../android/src/test/number_format_test_stub.cpp | 15 ++ platform/android/src/test/render_test_collator.cpp | 34 --- .../android/src/test/render_test_number_format.cpp | 15 -- platform/android/src/test/render_test_runner.cpp | 239 +-------------------- platform/android/src/test/test_runner_common.cpp | 224 +++++++++++++++++++ platform/android/src/test/test_runner_common.hpp | 23 ++ 8 files changed, 302 insertions(+), 520 deletions(-) create mode 100644 platform/android/src/test/collator_test_stub.cpp create mode 100644 platform/android/src/test/number_format_test_stub.cpp delete mode 100644 platform/android/src/test/render_test_collator.cpp delete mode 100644 platform/android/src/test/render_test_number_format.cpp create mode 100644 platform/android/src/test/test_runner_common.cpp create mode 100644 platform/android/src/test/test_runner_common.hpp (limited to 'platform') diff --git a/platform/android/src/test/benchmark_runner.cpp b/platform/android/src/test/benchmark_runner.cpp index eef829eac3..694825f995 100644 --- a/platform/android/src/test/benchmark_runner.cpp +++ b/platform/android/src/test/benchmark_runner.cpp @@ -1,243 +1,11 @@ -#include #include -#include - -#include -#include - -#include "jni.hpp" +#include "test_runner_common.hpp" #include -#include -#include #include -namespace mbgl { - -namespace { - -int severityToPriority(EventSeverity severity) { - switch (severity) { - case EventSeverity::Debug: - return ANDROID_LOG_DEBUG; - case EventSeverity::Info: - return ANDROID_LOG_INFO; - case EventSeverity::Warning: - return ANDROID_LOG_WARN; - case EventSeverity::Error: - return ANDROID_LOG_ERROR; - default: - return ANDROID_LOG_VERBOSE; - } -} - -} // namespace - -void Log::platformRecord(EventSeverity severity, const std::string& msg) { - __android_log_print(severityToPriority(severity), "mbgl", "%s", msg.c_str()); -} - -} // namespace mbgl - -namespace { - -template -class JavaWrapper { -public: - JavaWrapper(JNIEnv* env_, T obj_) : env(env_), obj(obj_) {} - ~JavaWrapper() { - env->DeleteLocalRef(obj); - env = nullptr; - obj = NULL; - } - T& get() { return obj; } - -private: - JavaWrapper() = delete; - JNIEnv* env; - T obj; -}; - -void changeState(JNIEnv* env, struct android_app* app, bool result) { - jobject nativeActivity = app->activity->clazz; - jclass acl = env->GetObjectClass(nativeActivity); - jmethodID getClassLoader = env->GetMethodID(acl, "getClassLoader", "()Ljava/lang/ClassLoader;"); - jobject cls = env->CallObjectMethod(nativeActivity, getClassLoader); - JavaWrapper classLoader(env, env->FindClass("java/lang/ClassLoader")); - jmethodID findClass = env->GetMethodID(classLoader.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); - JavaWrapper strClassName(env, env->NewStringUTF("android.app.TestState")); - jclass testStateClass = static_cast(env->CallObjectMethod(cls, findClass, strClassName.get())); - if (testStateClass != NULL) { - jfieldID id0 = env->GetStaticFieldID(testStateClass, "testResult", "Z"); - env->SetStaticBooleanField(testStateClass, id0, result); - jfieldID id = env->GetStaticFieldID(testStateClass, "running", "Z"); - env->SetStaticBooleanField(testStateClass, id, false); - } -} - -bool copyFile(JNIEnv* env, - AAssetManager* assetManager, - const std::string& filePath, - const std::string& destinationPath, - const std::string& fileName) { - JavaWrapper fileClass(env, env->FindClass("java/io/File")); - jmethodID fileCtor = env->GetMethodID(fileClass.get(), "", "(Ljava/lang/String;Ljava/lang/String;)V"); - jmethodID fileExists = env->GetMethodID(fileClass.get(), "exists", "()Z"); - - JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); - JavaWrapper file(env, env->NewStringUTF(fileName.c_str())); - JavaWrapper fileToCopy(env, env->NewObject(fileClass.get(), fileCtor, destination.get(), file.get())); - - bool stateOk = true; - if (env->CallBooleanMethod(fileToCopy.get(), fileExists)) { - mbgl::Log::Warning(mbgl::Event::General, "File '%s' already exists", filePath.c_str()); - } else { - std::unique_ptr> fileAsset( - AAssetManager_open(assetManager, fileName.c_str(), AASSET_MODE_BUFFER), - [](AAsset* asset) { AAsset_close(asset); }); - if (fileAsset == nullptr) { - mbgl::Log::Warning(mbgl::Event::General, "Failed to open asset file %s", fileName.c_str()); - return false; - } - const void* fileData = AAsset_getBuffer(fileAsset.get()); - const off_t fileLen = AAsset_getLength(fileAsset.get()); - - std::unique_ptr> newFile(std::fopen(filePath.c_str(), "w+"), - [](FILE* file) { std::fclose(file); }); - stateOk = newFile != nullptr; - - if (!stateOk) { - mbgl::Log::Warning(mbgl::Event::General, "Failed to create new file entry %s", fileName.c_str()); - } else { - auto res = static_cast(std::fwrite(fileData, sizeof(char), fileLen, newFile.get())); - if (fileLen != res) { - mbgl::Log::Warning( - mbgl::Event::General, "Failed to generate file entry %s from assets", fileName.c_str()); - } - } - } - return stateOk; -} - -std::string jstringToStdString(JNIEnv* env, jstring jStr) { - if (!jStr) { - return std::string(); - } - - JavaWrapper stringClass(env, env->GetObjectClass(jStr)); - const jmethodID getBytes = env->GetMethodID(stringClass.get(), "getBytes", "(Ljava/lang/String;)[B"); - JavaWrapper stringJbytes( - env, - static_cast( - env->CallObjectMethod(jStr, getBytes, JavaWrapper(env, env->NewStringUTF("UTF-8")).get()))); - - size_t length = static_cast(env->GetArrayLength(stringJbytes.get())); - jbyte* pBytes = env->GetByteArrayElements(stringJbytes.get(), NULL); - - std::string ret = std::string(reinterpret_cast(pBytes), length); - env->ReleaseByteArrayElements(stringJbytes.get(), pBytes, JNI_ABORT); - - return ret; -} - -void unZipFile(JNIEnv* env, const std::string& zipFilePath, const std::string& destinationPath) { - JavaWrapper fileClassWrapper(env, env->FindClass("java/io/File")); - auto fileClass = fileClassWrapper.get(); - jmethodID fileCtor = env->GetMethodID(fileClass, "", "(Ljava/lang/String;Ljava/lang/String;)V"); - jmethodID fileExists = env->GetMethodID(fileClass, "exists", "()Z"); - jmethodID fileIsDirectory = env->GetMethodID(fileClass, "isDirectory", "()Z"); - jmethodID deleteFile = env->GetMethodID(fileClass, "delete", "()Z"); - jmethodID createNewFile = env->GetMethodID(fileClass, "createNewFile", "()Z"); - jmethodID fileGetName = env->GetMethodID(fileClass, "getName", "()Ljava/lang/String;"); - - JavaWrapper fileInputStreamWrapper(env, env->FindClass("java/io/FileInputStream")); - auto fileInputStream = fileInputStreamWrapper.get(); - jmethodID finCtor = env->GetMethodID(fileInputStream, "", "(Ljava/lang/String;)V"); - - JavaWrapper fileOutputStreamWrapper(env, env->FindClass("java/io/FileOutputStream")); - auto fileOutputStream = fileOutputStreamWrapper.get(); - jmethodID foutCtor = env->GetMethodID(fileOutputStream, "", "(Ljava/io/File;)V"); - jmethodID foutClose = env->GetMethodID(fileOutputStream, "close", "()V"); - jmethodID foutWrite = env->GetMethodID(fileOutputStream, "write", "([BII)V"); - - JavaWrapper zipInputStreamWrapper(env, env->FindClass("java/util/zip/ZipInputStream")); - auto zipInputStream = zipInputStreamWrapper.get(); - jmethodID zinCtor = env->GetMethodID(zipInputStream, "", "(Ljava/io/InputStream;)V"); - jmethodID zinGetNextEntry = env->GetMethodID(zipInputStream, "getNextEntry", "()Ljava/util/zip/ZipEntry;"); - jmethodID zinRead = env->GetMethodID(zipInputStream, "read", "([B)I"); - jmethodID zinCloseEntry = env->GetMethodID(zipInputStream, "closeEntry", "()V"); - - JavaWrapper zipEntryWrapper(env, env->FindClass("java/util/zip/ZipEntry")); - auto zipEntry = zipEntryWrapper.get(); - jmethodID zipGetName = env->GetMethodID(zipEntry, "getName", "()Ljava/lang/String;"); - jmethodID zipIsDirectory = env->GetMethodID(zipEntry, "isDirectory", "()Z"); - - // Unzip the resource folder to destination path - JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); - JavaWrapper zipFile(env, env->NewStringUTF(zipFilePath.c_str())); - JavaWrapper fileIn(env, env->NewObject(fileInputStream, finCtor, zipFile.get())); - JavaWrapper zipIn(env, env->NewObject(zipInputStream, zinCtor, fileIn.get())); - - while (true) { - JavaWrapper obj(env, env->CallObjectMethod(zipIn.get(), zinGetNextEntry)); - jobject zEntry = obj.get(); - if (zEntry == NULL) { - break; - } - jstring dir = static_cast(env->CallObjectMethod(zEntry, zipGetName)); - std::string name = jstringToStdString(env, dir); - bool isDir = env->CallBooleanMethod(zEntry, zipIsDirectory); - - JavaWrapper fileHandle(env, env->NewObject(fileClass, fileCtor, destination.get(), dir)); - jobject& f = fileHandle.get(); - std::string fileName = jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); - - if (isDir) { - if (!(env->CallBooleanMethod(f, fileIsDirectory))) { - jmethodID mkdirs = env->GetMethodID(fileClass, "mkdirs", "()Z"); - bool success = (env->CallBooleanMethod(f, mkdirs)); - std::string fileNameStr = - jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); - - if (!success) { - mbgl::Log::Warning( - mbgl::Event::General, "Failed to create folder entry %s from zip", fileNameStr.c_str()); - } - } - } else if (!(env->CallBooleanMethod(f, fileExists))) { - bool success = env->CallBooleanMethod(f, createNewFile); - std::string fileNameStr = - jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); - - if (!success) { - mbgl::Log::Warning( - mbgl::Event::General, "Failed to create folder entry %s from zip", fileNameStr.c_str()); - continue; - } - - JavaWrapper fout(env, env->NewObject(fileOutputStream, foutCtor, f)); - JavaWrapper jBuff(env, env->NewByteArray(2048)); - - int count; - while ((count = env->CallIntMethod(zipIn.get(), zinRead, jBuff.get())) != -1) { - env->CallVoidMethod(fout.get(), foutWrite, jBuff.get(), 0, count); - } - - env->CallVoidMethod(zipIn.get(), zinCloseEntry); - env->CallVoidMethod(fout.get(), foutClose); - } - } - - JavaWrapper fileToDelete(env, env->NewObject(fileClass, fileCtor, destination.get(), zipFile.get())); - if (env->CallBooleanMethod(fileToDelete.get(), fileExists)) { - jboolean success = (env->CallBooleanMethod(fileToDelete.get(), deleteFile)); - if (!success) { - mbgl::Log::Warning(mbgl::Event::General, "Failed to delete file entry %s", zipFilePath.c_str()); - } - } -} - -} // namespace +using namespace mbgl; +using namespace mbgl::android; bool running = false; bool done = false; diff --git a/platform/android/src/test/collator_test_stub.cpp b/platform/android/src/test/collator_test_stub.cpp new file mode 100644 index 0000000000..7c83318979 --- /dev/null +++ b/platform/android/src/test/collator_test_stub.cpp @@ -0,0 +1,34 @@ +#include + +#include + +namespace mbgl { +namespace platform { + +class Collator::Impl { +public: + Impl(optional) {} + + bool operator==(const Impl&) const { return true; } + + int compare(const std::string&, const std::string&) const { return 0; } + + std::string resolvedLocale() const { return ""; } +}; + +Collator::Collator(bool, bool, optional locale_) : impl(std::make_shared(std::move(locale_))) {} + +int Collator::compare(const std::string& lhs, const std::string& rhs) const { + return impl->compare(lhs, rhs); +} + +bool Collator::operator==(const Collator& other) const { + return *impl == *(other.impl); +} + +std::string Collator::resolvedLocale() const { + return impl->resolvedLocale(); +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/android/src/test/number_format_test_stub.cpp b/platform/android/src/test/number_format_test_stub.cpp new file mode 100644 index 0000000000..57710c8558 --- /dev/null +++ b/platform/android/src/test/number_format_test_stub.cpp @@ -0,0 +1,15 @@ +#include + +namespace mbgl { +namespace platform { + +std::string formatNumber(double /*number*/, + const std::string& /*localeId */, + const std::string& /*currency*/, + uint8_t /*minFractionDigits*/, + uint8_t /*maxFractionDigits*/) { + return ""; +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/android/src/test/render_test_collator.cpp b/platform/android/src/test/render_test_collator.cpp deleted file mode 100644 index 7c83318979..0000000000 --- a/platform/android/src/test/render_test_collator.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include - -#include - -namespace mbgl { -namespace platform { - -class Collator::Impl { -public: - Impl(optional) {} - - bool operator==(const Impl&) const { return true; } - - int compare(const std::string&, const std::string&) const { return 0; } - - std::string resolvedLocale() const { return ""; } -}; - -Collator::Collator(bool, bool, optional locale_) : impl(std::make_shared(std::move(locale_))) {} - -int Collator::compare(const std::string& lhs, const std::string& rhs) const { - return impl->compare(lhs, rhs); -} - -bool Collator::operator==(const Collator& other) const { - return *impl == *(other.impl); -} - -std::string Collator::resolvedLocale() const { - return impl->resolvedLocale(); -} - -} // namespace platform -} // namespace mbgl diff --git a/platform/android/src/test/render_test_number_format.cpp b/platform/android/src/test/render_test_number_format.cpp deleted file mode 100644 index 57710c8558..0000000000 --- a/platform/android/src/test/render_test_number_format.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include - -namespace mbgl { -namespace platform { - -std::string formatNumber(double /*number*/, - const std::string& /*localeId */, - const std::string& /*currency*/, - uint8_t /*minFractionDigits*/, - uint8_t /*maxFractionDigits*/) { - return ""; -} - -} // namespace platform -} // namespace mbgl diff --git a/platform/android/src/test/render_test_runner.cpp b/platform/android/src/test/render_test_runner.cpp index fe6148f951..8d9c9e4838 100644 --- a/platform/android/src/test/render_test_runner.cpp +++ b/platform/android/src/test/render_test_runner.cpp @@ -1,243 +1,10 @@ -#include #include -#include +#include "test_runner_common.hpp" -#include -#include - -#include "jni.hpp" - -#include #include -#include -#include - -namespace mbgl { - -namespace { - -int severityToPriority(EventSeverity severity) { - switch (severity) { - case EventSeverity::Debug: - return ANDROID_LOG_DEBUG; - case EventSeverity::Info: - return ANDROID_LOG_INFO; - case EventSeverity::Warning: - return ANDROID_LOG_WARN; - case EventSeverity::Error: - return ANDROID_LOG_ERROR; - default: - return ANDROID_LOG_VERBOSE; - } -} - -} // namespace - -void Log::platformRecord(EventSeverity severity, const std::string& msg) { - __android_log_print(severityToPriority(severity), "mbgl", "%s", msg.c_str()); -} - -} // namespace mbgl - -namespace { - -template -class JavaWrapper { -public: - JavaWrapper(JNIEnv* env_, T obj_) : env(env_), obj(obj_) {} - ~JavaWrapper() { - env->DeleteLocalRef(obj); - env = nullptr; - obj = NULL; - } - T& get() { return obj; } - -private: - JavaWrapper() = delete; - JNIEnv* env; - T obj; -}; - -std::string jstringToStdString(JNIEnv* env, jstring jStr) { - if (!jStr) { - return std::string(); - } - - JavaWrapper stringClass(env, env->GetObjectClass(jStr)); - const jmethodID getBytes = env->GetMethodID(stringClass.get(), "getBytes", "(Ljava/lang/String;)[B"); - JavaWrapper stringJbytes( - env, - static_cast( - env->CallObjectMethod(jStr, getBytes, JavaWrapper(env, env->NewStringUTF("UTF-8")).get()))); - - size_t length = static_cast(env->GetArrayLength(stringJbytes.get())); - jbyte* pBytes = env->GetByteArrayElements(stringJbytes.get(), NULL); - - std::string ret = std::string(reinterpret_cast(pBytes), length); - env->ReleaseByteArrayElements(stringJbytes.get(), pBytes, JNI_ABORT); - - return ret; -} - -void changeState(JNIEnv* env, struct android_app* app, bool result) { - jobject nativeActivity = app->activity->clazz; - jclass acl = env->GetObjectClass(nativeActivity); - jmethodID getClassLoader = env->GetMethodID(acl, "getClassLoader", "()Ljava/lang/ClassLoader;"); - jobject cls = env->CallObjectMethod(nativeActivity, getClassLoader); - JavaWrapper classLoader(env, env->FindClass("java/lang/ClassLoader")); - jmethodID findClass = env->GetMethodID(classLoader.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); - JavaWrapper strClassName(env, env->NewStringUTF("android.app.TestState")); - jclass testStateClass = static_cast(env->CallObjectMethod(cls, findClass, strClassName.get())); - if (testStateClass != NULL) { - jfieldID id0 = env->GetStaticFieldID(testStateClass, "testResult", "Z"); - env->SetStaticBooleanField(testStateClass, id0, result); - jfieldID id = env->GetStaticFieldID(testStateClass, "running", "Z"); - env->SetStaticBooleanField(testStateClass, id, false); - } -} - -bool copyFile(JNIEnv* env, - AAssetManager* assetManager, - const std::string& filePath, - const std::string& destinationPath, - const std::string& fileName) { - JavaWrapper fileClass(env, env->FindClass("java/io/File")); - jmethodID fileCtor = env->GetMethodID(fileClass.get(), "", "(Ljava/lang/String;Ljava/lang/String;)V"); - jmethodID fileExists = env->GetMethodID(fileClass.get(), "exists", "()Z"); - - JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); - JavaWrapper file(env, env->NewStringUTF(fileName.c_str())); - JavaWrapper fileToCopy(env, env->NewObject(fileClass.get(), fileCtor, destination.get(), file.get())); - - bool stateOk = true; - if (env->CallBooleanMethod(fileToCopy.get(), fileExists)) { - mbgl::Log::Warning(mbgl::Event::General, "File '%s' already exists", filePath.c_str()); - } else { - std::unique_ptr> fileAsset( - AAssetManager_open(assetManager, fileName.c_str(), AASSET_MODE_BUFFER), - [](AAsset* asset) { AAsset_close(asset); }); - if (fileAsset == nullptr) { - mbgl::Log::Warning(mbgl::Event::General, "Failed to open asset file %s", fileName.c_str()); - return false; - } - const void* fileData = AAsset_getBuffer(fileAsset.get()); - const off_t fileLen = AAsset_getLength(fileAsset.get()); - - std::unique_ptr> newFile(std::fopen(filePath.c_str(), "w+"), - [](FILE* file) { std::fclose(file); }); - stateOk = newFile != nullptr; - - if (!stateOk) { - mbgl::Log::Warning(mbgl::Event::General, "Failed to create new file entry %s", fileName.c_str()); - } else { - auto res = static_cast(std::fwrite(fileData, sizeof(char), fileLen, newFile.get())); - if (fileLen != res) { - mbgl::Log::Warning( - mbgl::Event::General, "Failed to generate file entry %s from assets", fileName.c_str()); - } - } - } - return stateOk; -} - -void unZipFile(JNIEnv* env, const std::string& zipFilePath, const std::string& destinationPath) { - JavaWrapper fileClassWrapper(env, env->FindClass("java/io/File")); - auto fileClass = fileClassWrapper.get(); - jmethodID fileCtor = env->GetMethodID(fileClass, "", "(Ljava/lang/String;Ljava/lang/String;)V"); - jmethodID fileExists = env->GetMethodID(fileClass, "exists", "()Z"); - jmethodID fileIsDirectory = env->GetMethodID(fileClass, "isDirectory", "()Z"); - jmethodID deleteFile = env->GetMethodID(fileClass, "delete", "()Z"); - jmethodID createNewFile = env->GetMethodID(fileClass, "createNewFile", "()Z"); - jmethodID fileGetName = env->GetMethodID(fileClass, "getName", "()Ljava/lang/String;"); - - JavaWrapper fileInputStreamWrapper(env, env->FindClass("java/io/FileInputStream")); - auto fileInputStream = fileInputStreamWrapper.get(); - jmethodID finCtor = env->GetMethodID(fileInputStream, "", "(Ljava/lang/String;)V"); - - JavaWrapper fileOutputStreamWrapper(env, env->FindClass("java/io/FileOutputStream")); - auto fileOutputStream = fileOutputStreamWrapper.get(); - jmethodID foutCtor = env->GetMethodID(fileOutputStream, "", "(Ljava/io/File;)V"); - jmethodID foutClose = env->GetMethodID(fileOutputStream, "close", "()V"); - jmethodID foutWrite = env->GetMethodID(fileOutputStream, "write", "([BII)V"); - - JavaWrapper zipInputStreamWrapper(env, env->FindClass("java/util/zip/ZipInputStream")); - auto zipInputStream = zipInputStreamWrapper.get(); - jmethodID zinCtor = env->GetMethodID(zipInputStream, "", "(Ljava/io/InputStream;)V"); - jmethodID zinGetNextEntry = env->GetMethodID(zipInputStream, "getNextEntry", "()Ljava/util/zip/ZipEntry;"); - jmethodID zinRead = env->GetMethodID(zipInputStream, "read", "([B)I"); - jmethodID zinCloseEntry = env->GetMethodID(zipInputStream, "closeEntry", "()V"); - - JavaWrapper zipEntryWrapper(env, env->FindClass("java/util/zip/ZipEntry")); - auto zipEntry = zipEntryWrapper.get(); - jmethodID zipGetName = env->GetMethodID(zipEntry, "getName", "()Ljava/lang/String;"); - jmethodID zipIsDirectory = env->GetMethodID(zipEntry, "isDirectory", "()Z"); - - // Unzip the resource folder to destination path - JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); - JavaWrapper zipFile(env, env->NewStringUTF(zipFilePath.c_str())); - JavaWrapper fileIn(env, env->NewObject(fileInputStream, finCtor, zipFile.get())); - JavaWrapper zipIn(env, env->NewObject(zipInputStream, zinCtor, fileIn.get())); - - while (true) { - JavaWrapper obj(env, env->CallObjectMethod(zipIn.get(), zinGetNextEntry)); - jobject zEntry = obj.get(); - if (zEntry == NULL) { - break; - } - jstring dir = static_cast(env->CallObjectMethod(zEntry, zipGetName)); - std::string name = jstringToStdString(env, dir); - bool isDir = env->CallBooleanMethod(zEntry, zipIsDirectory); - - JavaWrapper fileHandle(env, env->NewObject(fileClass, fileCtor, destination.get(), dir)); - jobject& f = fileHandle.get(); - std::string fileName = jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); - - if (isDir) { - if (!(env->CallBooleanMethod(f, fileIsDirectory))) { - jmethodID mkdirs = env->GetMethodID(fileClass, "mkdirs", "()Z"); - bool success = (env->CallBooleanMethod(f, mkdirs)); - std::string fileNameStr = - jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); - - if (!success) { - mbgl::Log::Warning( - mbgl::Event::General, "Failed to create folder entry %s from zip", fileNameStr.c_str()); - } - } - } else if (!(env->CallBooleanMethod(f, fileExists))) { - bool success = env->CallBooleanMethod(f, createNewFile); - std::string fileNameStr = - jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); - - if (!success) { - mbgl::Log::Warning( - mbgl::Event::General, "Failed to create folder entry %s from zip", fileNameStr.c_str()); - continue; - } - - JavaWrapper fout(env, env->NewObject(fileOutputStream, foutCtor, f)); - JavaWrapper jBuff(env, env->NewByteArray(2048)); - - int count; - while ((count = env->CallIntMethod(zipIn.get(), zinRead, jBuff.get())) != -1) { - env->CallVoidMethod(fout.get(), foutWrite, jBuff.get(), 0, count); - } - - env->CallVoidMethod(zipIn.get(), zinCloseEntry); - env->CallVoidMethod(fout.get(), foutClose); - } - } - - JavaWrapper fileToDelete(env, env->NewObject(fileClass, fileCtor, destination.get(), zipFile.get())); - if (env->CallBooleanMethod(fileToDelete.get(), fileExists)) { - jboolean success = (env->CallBooleanMethod(fileToDelete.get(), deleteFile)); - if (!success) { - mbgl::Log::Warning(mbgl::Event::General, "Failed to delete file entry %s", zipFilePath.c_str()); - } - } -} -} // namespace +using namespace mbgl; +using namespace mbgl::android; void android_main(struct android_app* app) { mbgl::android::theJVM = app->activity->vm; diff --git a/platform/android/src/test/test_runner_common.cpp b/platform/android/src/test/test_runner_common.cpp new file mode 100644 index 0000000000..9d17f72d0b --- /dev/null +++ b/platform/android/src/test/test_runner_common.cpp @@ -0,0 +1,224 @@ +#include "test_runner_common.hpp" +#include + +namespace mbgl { + +void Log::platformRecord(EventSeverity severity, const std::string& msg) { + __android_log_print(android::severityToPriority(severity), "mbgl", "%s", msg.c_str()); +} + +namespace android { + +int severityToPriority(EventSeverity severity) { + switch (severity) { + case EventSeverity::Debug: + return ANDROID_LOG_DEBUG; + case EventSeverity::Info: + return ANDROID_LOG_INFO; + case EventSeverity::Warning: + return ANDROID_LOG_WARN; + case EventSeverity::Error: + return ANDROID_LOG_ERROR; + default: + return ANDROID_LOG_VERBOSE; + } +} + +template +class JavaWrapper { +public: + JavaWrapper(JNIEnv* env_, T obj_) : env(env_), obj(obj_) {} + ~JavaWrapper() { + env->DeleteLocalRef(obj); + env = nullptr; + obj = NULL; + } + T& get() { return obj; } + +private: + JavaWrapper() = delete; + JNIEnv* env; + T obj; +}; + +std::string jstringToStdString(JNIEnv* env, jstring jStr) { + if (!jStr) { + return std::string(); + } + + JavaWrapper stringClass(env, env->GetObjectClass(jStr)); + const jmethodID getBytes = env->GetMethodID(stringClass.get(), "getBytes", "(Ljava/lang/String;)[B"); + JavaWrapper stringJbytes( + env, + static_cast( + env->CallObjectMethod(jStr, getBytes, JavaWrapper(env, env->NewStringUTF("UTF-8")).get()))); + + size_t length = static_cast(env->GetArrayLength(stringJbytes.get())); + jbyte* pBytes = env->GetByteArrayElements(stringJbytes.get(), NULL); + + std::string ret = std::string(reinterpret_cast(pBytes), length); + env->ReleaseByteArrayElements(stringJbytes.get(), pBytes, JNI_ABORT); + + return ret; +} + +void changeState(JNIEnv* env, struct android_app* app, bool result) { + jobject nativeActivity = app->activity->clazz; + jclass acl = env->GetObjectClass(nativeActivity); + jmethodID getClassLoader = env->GetMethodID(acl, "getClassLoader", "()Ljava/lang/ClassLoader;"); + jobject cls = env->CallObjectMethod(nativeActivity, getClassLoader); + JavaWrapper classLoader(env, env->FindClass("java/lang/ClassLoader")); + jmethodID findClass = env->GetMethodID(classLoader.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); + JavaWrapper strClassName(env, env->NewStringUTF("android.app.TestState")); + jclass testStateClass = static_cast(env->CallObjectMethod(cls, findClass, strClassName.get())); + if (testStateClass != NULL) { + jfieldID id0 = env->GetStaticFieldID(testStateClass, "testResult", "Z"); + env->SetStaticBooleanField(testStateClass, id0, result); + jfieldID id = env->GetStaticFieldID(testStateClass, "running", "Z"); + env->SetStaticBooleanField(testStateClass, id, false); + } +} + +bool copyFile(JNIEnv* env, + AAssetManager* assetManager, + const std::string& filePath, + const std::string& destinationPath, + const std::string& fileName) { + JavaWrapper fileClass(env, env->FindClass("java/io/File")); + jmethodID fileCtor = env->GetMethodID(fileClass.get(), "", "(Ljava/lang/String;Ljava/lang/String;)V"); + jmethodID fileExists = env->GetMethodID(fileClass.get(), "exists", "()Z"); + + JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); + JavaWrapper file(env, env->NewStringUTF(fileName.c_str())); + JavaWrapper fileToCopy(env, env->NewObject(fileClass.get(), fileCtor, destination.get(), file.get())); + + bool stateOk = true; + if (env->CallBooleanMethod(fileToCopy.get(), fileExists)) { + mbgl::Log::Warning(mbgl::Event::General, "File '%s' already exists", filePath.c_str()); + } else { + std::unique_ptr> fileAsset( + AAssetManager_open(assetManager, fileName.c_str(), AASSET_MODE_BUFFER), + [](AAsset* asset) { AAsset_close(asset); }); + if (fileAsset == nullptr) { + mbgl::Log::Warning(mbgl::Event::General, "Failed to open asset file %s", fileName.c_str()); + return false; + } + const void* fileData = AAsset_getBuffer(fileAsset.get()); + const off_t fileLen = AAsset_getLength(fileAsset.get()); + + std::unique_ptr> newFile(std::fopen(filePath.c_str(), "w+"), + [](FILE* file) { std::fclose(file); }); + stateOk = newFile != nullptr; + + if (!stateOk) { + mbgl::Log::Warning(mbgl::Event::General, "Failed to create new file entry %s", fileName.c_str()); + } else { + auto res = static_cast(std::fwrite(fileData, sizeof(char), fileLen, newFile.get())); + if (fileLen != res) { + mbgl::Log::Warning( + mbgl::Event::General, "Failed to generate file entry %s from assets", fileName.c_str()); + } + } + } + return stateOk; +} + +void unZipFile(JNIEnv* env, const std::string& zipFilePath, const std::string& destinationPath) { + JavaWrapper fileClassWrapper(env, env->FindClass("java/io/File")); + auto fileClass = fileClassWrapper.get(); + jmethodID fileCtor = env->GetMethodID(fileClass, "", "(Ljava/lang/String;Ljava/lang/String;)V"); + jmethodID fileExists = env->GetMethodID(fileClass, "exists", "()Z"); + jmethodID fileIsDirectory = env->GetMethodID(fileClass, "isDirectory", "()Z"); + jmethodID deleteFile = env->GetMethodID(fileClass, "delete", "()Z"); + jmethodID createNewFile = env->GetMethodID(fileClass, "createNewFile", "()Z"); + jmethodID fileGetName = env->GetMethodID(fileClass, "getName", "()Ljava/lang/String;"); + + JavaWrapper fileInputStreamWrapper(env, env->FindClass("java/io/FileInputStream")); + auto fileInputStream = fileInputStreamWrapper.get(); + jmethodID finCtor = env->GetMethodID(fileInputStream, "", "(Ljava/lang/String;)V"); + + JavaWrapper fileOutputStreamWrapper(env, env->FindClass("java/io/FileOutputStream")); + auto fileOutputStream = fileOutputStreamWrapper.get(); + jmethodID foutCtor = env->GetMethodID(fileOutputStream, "", "(Ljava/io/File;)V"); + jmethodID foutClose = env->GetMethodID(fileOutputStream, "close", "()V"); + jmethodID foutWrite = env->GetMethodID(fileOutputStream, "write", "([BII)V"); + + JavaWrapper zipInputStreamWrapper(env, env->FindClass("java/util/zip/ZipInputStream")); + auto zipInputStream = zipInputStreamWrapper.get(); + jmethodID zinCtor = env->GetMethodID(zipInputStream, "", "(Ljava/io/InputStream;)V"); + jmethodID zinGetNextEntry = env->GetMethodID(zipInputStream, "getNextEntry", "()Ljava/util/zip/ZipEntry;"); + jmethodID zinRead = env->GetMethodID(zipInputStream, "read", "([B)I"); + jmethodID zinCloseEntry = env->GetMethodID(zipInputStream, "closeEntry", "()V"); + + JavaWrapper zipEntryWrapper(env, env->FindClass("java/util/zip/ZipEntry")); + auto zipEntry = zipEntryWrapper.get(); + jmethodID zipGetName = env->GetMethodID(zipEntry, "getName", "()Ljava/lang/String;"); + jmethodID zipIsDirectory = env->GetMethodID(zipEntry, "isDirectory", "()Z"); + + // Unzip the resource folder to destination path + JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); + JavaWrapper zipFile(env, env->NewStringUTF(zipFilePath.c_str())); + JavaWrapper fileIn(env, env->NewObject(fileInputStream, finCtor, zipFile.get())); + JavaWrapper zipIn(env, env->NewObject(zipInputStream, zinCtor, fileIn.get())); + + while (true) { + JavaWrapper obj(env, env->CallObjectMethod(zipIn.get(), zinGetNextEntry)); + jobject zEntry = obj.get(); + if (zEntry == NULL) { + break; + } + jstring dir = static_cast(env->CallObjectMethod(zEntry, zipGetName)); + std::string name = jstringToStdString(env, dir); + bool isDir = env->CallBooleanMethod(zEntry, zipIsDirectory); + + JavaWrapper fileHandle(env, env->NewObject(fileClass, fileCtor, destination.get(), dir)); + jobject& f = fileHandle.get(); + std::string fileName = jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); + + if (isDir) { + if (!(env->CallBooleanMethod(f, fileIsDirectory))) { + jmethodID mkdirs = env->GetMethodID(fileClass, "mkdirs", "()Z"); + bool success = (env->CallBooleanMethod(f, mkdirs)); + std::string fileNameStr = + jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); + + if (!success) { + mbgl::Log::Warning( + mbgl::Event::General, "Failed to create folder entry %s from zip", fileNameStr.c_str()); + } + } + } else if (!(env->CallBooleanMethod(f, fileExists))) { + bool success = env->CallBooleanMethod(f, createNewFile); + std::string fileNameStr = + jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); + + if (!success) { + mbgl::Log::Warning( + mbgl::Event::General, "Failed to create folder entry %s from zip", fileNameStr.c_str()); + continue; + } + + JavaWrapper fout(env, env->NewObject(fileOutputStream, foutCtor, f)); + JavaWrapper jBuff(env, env->NewByteArray(2048)); + + int count; + while ((count = env->CallIntMethod(zipIn.get(), zinRead, jBuff.get())) != -1) { + env->CallVoidMethod(fout.get(), foutWrite, jBuff.get(), 0, count); + } + + env->CallVoidMethod(zipIn.get(), zinCloseEntry); + env->CallVoidMethod(fout.get(), foutClose); + } + } + + JavaWrapper fileToDelete(env, env->NewObject(fileClass, fileCtor, destination.get(), zipFile.get())); + if (env->CallBooleanMethod(fileToDelete.get(), fileExists)) { + jboolean success = (env->CallBooleanMethod(fileToDelete.get(), deleteFile)); + if (!success) { + mbgl::Log::Warning(mbgl::Event::General, "Failed to delete file entry %s", zipFilePath.c_str()); + } + } +} + +} // namespace android +} // namespace mbgl diff --git a/platform/android/src/test/test_runner_common.hpp b/platform/android/src/test/test_runner_common.hpp new file mode 100644 index 0000000000..4be59e9178 --- /dev/null +++ b/platform/android/src/test/test_runner_common.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include + +#include "jni.hpp" + +namespace mbgl { +namespace android { + +int severityToPriority(EventSeverity severity); +void changeState(JNIEnv* env, struct android_app* app, bool result); +bool copyFile(JNIEnv* env, + AAssetManager* assetManager, + const std::string& filePath, + const std::string& destinationPath, + const std::string& fileName); + +void unZipFile(JNIEnv* env, const std::string& zipFilePath, const std::string& destinationPath); + +} // namespace android +} // namespace mbgl -- cgit v1.2.1