summaryrefslogtreecommitdiff
path: root/platform/android/src/test/render_test_runner.cpp
blob: 0d2400ac74edf6fa5b20ad32d7ad48a68c2a8b69 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <android_native_app_glue.h>
#include <mbgl/render_test.hpp>
#include <mbgl/util/logging.hpp>

#include <android/asset_manager.h>
#include <android/log.h>

#include "jni.hpp"

#include <cstdio>
#include <functional>
#include <string>
#include <vector>

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 T>
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<jclass> stringClass(env, env->GetObjectClass(jStr));
    const jmethodID getBytes = env->GetMethodID(stringClass.get(), "getBytes", "(Ljava/lang/String;)[B");
    JavaWrapper<jbyteArray> stringJbytes(
        env,
        static_cast<jbyteArray>(
            env->CallObjectMethod(jStr, getBytes, JavaWrapper<jstring>(env, env->NewStringUTF("UTF-8")).get())));

    size_t length = static_cast<size_t>(env->GetArrayLength(stringJbytes.get()));
    jbyte* pBytes = env->GetByteArrayElements(stringJbytes.get(), NULL);

    std::string ret = std::string(reinterpret_cast<char*>(pBytes), length);
    env->ReleaseByteArrayElements(stringJbytes.get(), pBytes, JNI_ABORT);

    return ret;
}

void changeState(JNIEnv* env, struct android_app* app) {
    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<jclass> classLoader(env, env->FindClass("java/lang/ClassLoader"));
    jmethodID findClass = env->GetMethodID(classLoader.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
    JavaWrapper<jstring> strClassName(env, env->NewStringUTF("android.app.TestState"));
    jclass testStateClass = static_cast<jclass>(env->CallObjectMethod(cls, findClass, strClassName.get()));
    if (testStateClass != NULL) {
        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<jclass> fileClass(env, env->FindClass("java/io/File"));
    jmethodID fileCtor = env->GetMethodID(fileClass.get(), "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
    jmethodID fileExists = env->GetMethodID(fileClass.get(), "exists", "()Z");

    JavaWrapper<jstring> destination(env, env->NewStringUTF(destinationPath.c_str()));
    JavaWrapper<jstring> file(env, env->NewStringUTF(fileName.c_str()));
    JavaWrapper<jobject> 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<AAsset, std::function<void(AAsset*)>> 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<FILE, std::function<void(FILE*)>> 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<off_t>(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<jclass> fileClassWrapper(env, env->FindClass("java/io/File"));
    auto fileClass = fileClassWrapper.get();
    jmethodID fileCtor = env->GetMethodID(fileClass, "<init>", "(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<jclass> fileInputStreamWrapper(env, env->FindClass("java/io/FileInputStream"));
    auto fileInputStream = fileInputStreamWrapper.get();
    jmethodID finCtor = env->GetMethodID(fileInputStream, "<init>", "(Ljava/lang/String;)V");

    JavaWrapper<jclass> fileOutputStreamWrapper(env, env->FindClass("java/io/FileOutputStream"));
    auto fileOutputStream = fileOutputStreamWrapper.get();
    jmethodID foutCtor = env->GetMethodID(fileOutputStream, "<init>", "(Ljava/io/File;)V");
    jmethodID foutClose = env->GetMethodID(fileOutputStream, "close", "()V");
    jmethodID foutWrite = env->GetMethodID(fileOutputStream, "write", "([BII)V");

    JavaWrapper<jclass> zipInputStreamWrapper(env, env->FindClass("java/util/zip/ZipInputStream"));
    auto zipInputStream = zipInputStreamWrapper.get();
    jmethodID zinCtor = env->GetMethodID(zipInputStream, "<init>", "(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<jclass> 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<jstring> destination(env, env->NewStringUTF(destinationPath.c_str()));
    JavaWrapper<jstring> zipFile(env, env->NewStringUTF(zipFilePath.c_str()));
    JavaWrapper<jobject> fileIn(env, env->NewObject(fileInputStream, finCtor, zipFile.get()));
    JavaWrapper<jobject> zipIn(env, env->NewObject(zipInputStream, zinCtor, fileIn.get()));

    while (true) {
        JavaWrapper<jobject> obj(env, env->CallObjectMethod(zipIn.get(), zinGetNextEntry));
        jobject zEntry = obj.get();
        if (zEntry == NULL) {
            break;
        }
        jstring dir = static_cast<jstring>(env->CallObjectMethod(zEntry, zipGetName));
        std::string name = jstringToStdString(env, dir);
        bool isDir = env->CallBooleanMethod(zEntry, zipIsDirectory);

        JavaWrapper<jobject> fileHandle(env, env->NewObject(fileClass, fileCtor, destination.get(), dir));
        jobject& f = fileHandle.get();
        std::string fileName = jstringToStdString(env, static_cast<jstring>(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 fileName =
                    jstringToStdString(env, static_cast<jstring>(env->CallObjectMethod(f, fileGetName)));

                if (!success) {
                    mbgl::Log::Warning(
                        mbgl::Event::General, "Failed to create folder entry %s from zip", fileName.c_str());
                }
            }
        } else if (!(env->CallBooleanMethod(f, fileExists))) {
            bool success = env->CallBooleanMethod(f, createNewFile);
            std::string fileName = jstringToStdString(env, static_cast<jstring>(env->CallObjectMethod(f, fileGetName)));

            if (!success) {
                mbgl::Log::Warning(mbgl::Event::General, "Failed to create folder entry %s from zip", fileName.c_str());
                continue;
            }

            JavaWrapper<jobject> fout(env, env->NewObject(fileOutputStream, foutCtor, f));
            JavaWrapper<jbyteArray> 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<jobject> 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

void android_main(struct android_app* app) {
    mbgl::android::theJVM = app->activity->vm;
    JNIEnv* env = nullptr;
    app->activity->vm->AttachCurrentThread(&env, NULL);

    const char* storage_chars = app->activity->internalDataPath;
    std::string storagePath(storage_chars);
    std::string zipFile = storagePath + "/data.zip";

    int outFd, outEvents;
    struct android_poll_source* source = nullptr;
    if (!copyFile(env, app->activity->assetManager, zipFile, storagePath, "data.zip")) {
        mbgl::Log::Error(
            mbgl::Event::General, "Failed to copy zip File '%s' to external storage for upzipping", zipFile.c_str());
    } else {
        unZipFile(env, zipFile, storagePath);

        auto runTestWithManifest = [&storagePath, &app, &outFd, &outEvents, &source](
                                       const std::string manifest, const std::string filter = {}) -> bool {
            const std::string configFile = storagePath + manifest;
            std::vector<std::string> arguments = {"mbgl-render-test-runner", "-p", configFile, filter};
            std::vector<char*> argv;
            for (const auto& arg : arguments) {
                argv.push_back(const_cast<char*>(arg.data()));
            }
            argv.push_back(nullptr);

            int finishedTestCount = 0;
            std::function<void()> testStatus = [&]() {
                ALooper_pollAll(0, &outFd, &outEvents, reinterpret_cast<void**>(&source));

                if (source != nullptr) {
                    source->process(app, source);
                }

                mbgl::Log::Info(mbgl::Event::General, "Current finished tests number is '%d' ", ++finishedTestCount);
            };
            mbgl::Log::Info(
                mbgl::Event::General, "Start running RenderTestRunner with manifest: '%s'", manifest.c_str());
            bool result = mbgl::runRenderTests(argv.size() - 1, argv.data(), testStatus) == 0;
            mbgl::Log::Info(mbgl::Event::General, "End running RenderTestRunner with manifest: '%s'", manifest.c_str());
            return result;
        };
        runTestWithManifest("/android-manifest-probe-network-gfx.json");
        runTestWithManifest("/android-manifest-probe-memory.json", "--filter=real-world");
        mbgl::Log::Info(mbgl::Event::General, "All tests are finished!");
        changeState(env, app);
    }
    while (true) {
        ALooper_pollAll(0, &outFd, &outEvents, reinterpret_cast<void**>(&source));

        if (source != nullptr) {
            source->process(app, source);
        }
        if (app->destroyRequested != 0) {
            app->activity->vm->DetachCurrentThread();
            mbgl::Log::Info(mbgl::Event::General, "Close the App!");
            return;
        }
    }
}