summaryrefslogtreecommitdiff
path: root/platform/android/src/test/test_runner.cpp
blob: ec315fe27cfb8aa05319c6c0a9bf9463e42eb06f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <mbgl/test.hpp>
#include "test_runner_common.hpp"

#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";

    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;
        }
    }
}