diff options
author | Juha Alanen <juha.alanen@mapbox.com> | 2019-12-19 15:55:39 +0200 |
---|---|---|
committer | Juha Alanen <juha.alanen@mapbox.com> | 2020-01-10 17:19:30 +0200 |
commit | 0b3942572e5fc7be575ff15c82a9e31d508609f7 (patch) | |
tree | d3ef09c2117a03bee3b85f5e62a5b9a10c285a9c /platform/android | |
parent | f24e4ca0689c023cf5499c4a78ba7a20b3e9a4c0 (diff) | |
download | qtlocation-mapboxgl-0b3942572e5fc7be575ff15c82a9e31d508609f7.tar.gz |
[test] Run unit tests as NativeActivity application on Android
Diffstat (limited to 'platform/android')
-rw-r--r-- | platform/android/src/test/test_runner.cpp | 75 |
1 files changed, 70 insertions, 5 deletions
diff --git a/platform/android/src/test/test_runner.cpp b/platform/android/src/test/test_runner.cpp index 008e3c5748..ec315fe27c 100644 --- a/platform/android/src/test/test_runner.cpp +++ b/platform/android/src/test/test_runner.cpp @@ -1,10 +1,75 @@ -#include "runtime.hpp" #include <mbgl/test.hpp> +#include "test_runner_common.hpp" -int main(int argc, char *argv[]) { - if (!mbgl::android::initRuntime(argc, argv)) { - return 1; +#include <unistd.h> +#include <thread> + +using namespace mbgl; +using namespace mbgl::android; + +std::atomic<bool> running{true}; +std::once_flag done; +ALooper* looper = NULL; + +void runner() { + std::vector<std::string> arguments = {"mbgl-test-runner", "--gtest_output=xml:/sdcard/test/results/results.xml"}; + std::vector<char*> argv; + for (const auto& arg : arguments) { + argv.push_back((char*)arg.data()); } + argv.push_back(nullptr); + + mbgl::Log::Info(mbgl::Event::General, "Start TestRunner"); + int status = mbgl::runTests(argv.size(), argv.data()); + mbgl::Log::Info(mbgl::Event::General, "TestRunner finished with status: '%d'", status); + running = false; + ALooper_wake(looper); +} + +void android_main(struct android_app* app) { + mbgl::android::theJVM = app->activity->vm; + JNIEnv* env = nullptr; + std::thread runnerThread; + app->activity->vm->AttachCurrentThread(&env, NULL); + looper = ALooper_forThread(); + + std::string storagePath(app->activity->internalDataPath); + std::string zipFile = storagePath + "/data.zip"; - mbgl::runTests(argc, argv); + if (copyFile(env, app->activity->assetManager, zipFile, storagePath, "data.zip")) { + if (chdir("/sdcard")) { + mbgl::Log::Error(mbgl::Event::General, "Failed to change the directory to /sdcard"); + changeState(env, app, false); + } else { + unZipFile(env, zipFile, "/sdcard/"); + runnerThread = std::thread(runner); + } + } else { + mbgl::Log::Error(mbgl::Event::General, "Failed to copy zip file '%s' to external storage", zipFile.c_str()); + changeState(env, app, false); + } + + int outFd, outEvents; + struct android_poll_source* source = nullptr; + + while (true) { + ALooper_pollAll(-1, &outFd, &outEvents, reinterpret_cast<void**>(&source)); + if (source != nullptr) { + source->process(app, source); + } + + if (!running) { + std::call_once(done, [&] { + mbgl::Log::Info(mbgl::Event::General, "TestRunner done"); + runnerThread.join(); + changeState(env, app, true); + }); + } + + if (app->destroyRequested != 0) { + app->activity->vm->DetachCurrentThread(); + mbgl::Log::Info(mbgl::Event::General, "Close the App!"); + return; + } + } } |