From 6e973e890a5b3e3fdde82e0fc62c0987a5917573 Mon Sep 17 00:00:00 2001 From: zmiao Date: Tue, 19 Nov 2019 20:45:30 +0200 Subject: [render-test] Wrap test data inside RenderTestRunner App + Add a new test app (#15887) * [render-test] Wrap test resources inside app * [render-test] Add test app * fix test app failure * [render-test]add callback + add javaObjectWrapper --- .gitignore | 10 + next/platform/android/android.cmake | 41 ++++ .../com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt | 8 +- platform/android/src/test/render_test_runner.cpp | 269 +++++++++++++++++++-- platform/default/src/mbgl/render-test/main.cpp | 2 +- platform/ios/test/MGLMapViewPitchTests.m | 2 +- render-test/android-manifest.json | 7 +- render-test/android/README.md | 8 +- render-test/android/app/build.gradle | 4 + .../java/android/app/NativeActivityTest.java | 29 +++ .../androidTest/java/android/app/TestState.java | 5 + .../android/app/src/main/AndroidManifest.xml | 2 +- render-test/android/app/src/main/assets/to_zip.txt | 15 ++ render-test/android/render_test_setup.sh | 42 ---- render-test/include/mbgl/render_test.hpp | 5 +- render-test/manifest_parser.cpp | 25 +- render-test/manifest_parser.hpp | 3 + render-test/render_test.cpp | 8 +- render-test/runner.cpp | 2 +- src/mbgl/map/transform.cpp | 76 +++--- src/mbgl/map/transform_state.cpp | 54 +++-- src/mbgl/map/transform_state.hpp | 8 +- 22 files changed, 487 insertions(+), 138 deletions(-) create mode 100644 render-test/android/app/src/androidTest/java/android/app/NativeActivityTest.java create mode 100644 render-test/android/app/src/androidTest/java/android/app/TestState.java create mode 100644 render-test/android/app/src/main/assets/to_zip.txt delete mode 100755 render-test/android/render_test_setup.sh diff --git a/.gitignore b/.gitignore index 538870edf7..d81b8c9727 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,16 @@ test/fixtures/storage/assets.zip buck-out .buckd +# Android RenderTestRunner +render-test/android/.* +render-test/android/app/build/ +render-test/android/app/src/main/assets/data.zip +render-test/android/local.properties +render-test/android/app/.* +render-test/android/package.json +render-test/android/app/app.iml +render-test/android/android.iml + # Generated file from npm/yarn package-lock.json diff --git a/next/platform/android/android.cmake b/next/platform/android/android.cmake index 06183291db..e086f8b75b 100644 --- a/next/platform/android/android.cmake +++ b/next/platform/android/android.cmake @@ -338,6 +338,47 @@ target_link_libraries( mbgl-render-test ) +add_custom_command( + TARGET mbgl-render-test-runner PRE_BUILD + COMMAND + ${CMAKE_COMMAND} + -E + copy + ${MBGL_ROOT}/render-test/android-manifest.json + ${MBGL_ROOT}/android-manifest.json + COMMAND + ${CMAKE_COMMAND} + -E + copy + ${MBGL_ROOT}/platform/node/test/ignores.json + ${MBGL_ROOT}/ignores/ignores.json + COMMAND + ${CMAKE_COMMAND} + -E + copy + ${MBGL_ROOT}/render-test/linux-ignores.json + ${MBGL_ROOT}/ignores/linux-ignores.json + COMMAND + ${CMAKE_COMMAND} + -E + tar + "cf" + "render-test/android/app/src/main/assets/data.zip" + --format=zip + --files-from=render-test/android/app/src/main/assets/to_zip.txt + COMMAND + ${CMAKE_COMMAND} + -E + remove_directory + ${MBGL_ROOT}/ignores + COMMAND + ${CMAKE_COMMAND} + -E + remove + ${MBGL_ROOT}/android-manifest.json + WORKING_DIRECTORY ${MBGL_ROOT} +) + # Android has no concept of MinSizeRel on android.toolchain.cmake and provides configurations tuned for binary size. We can push it a bit # more with code folding and LTO. set_target_properties(example-custom-layer PROPERTIES LINK_FLAGS_RELEASE "-fuse-ld=gold -O2 -flto -Wl,--icf=safe") diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt index dc313b5f64..ebb8ef4e4c 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt @@ -75,7 +75,7 @@ class NativeMapViewTest : AppCenter() { val expected = BEARING_TEST nativeMapView.setBearing(expected, 0) val actual = nativeMapView.bearing - assertEquals("Bearing should match", expected, actual, DELTA) + assertEquals("Bearing should match", expected, actual, 0.00001) } @Test @@ -123,7 +123,7 @@ class NativeMapViewTest : AppCenter() { val expected = PITCH_TEST nativeMapView.setPitch(expected, 0) val actual = nativeMapView.pitch - assertEquals("Pitch should match", expected, actual, DELTA) + assertEquals("Pitch should match", expected, actual, 0.00001) } @Test @@ -167,8 +167,8 @@ class NativeMapViewTest : AppCenter() { val actual = nativeMapView.cameraPosition assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, DELTA) assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, DELTA) - assertEquals("Bearing should match", expected.bearing, actual.bearing, DELTA) - assertEquals("Pitch should match", expected.tilt, actual.tilt, DELTA) + assertEquals("Bearing should match", expected.bearing, actual.bearing, 0.00001) + assertEquals("Pitch should match", expected.tilt, actual.tilt, 0.00001) assertEquals("Zoom should match", expected.zoom, actual.zoom, DELTA) Assert.assertArrayEquals(expected.padding, actual.padding, DELTA) } diff --git a/platform/android/src/test/render_test_runner.cpp b/platform/android/src/test/render_test_runner.cpp index d4554aa9de..98197ce1cc 100644 --- a/platform/android/src/test/render_test_runner.cpp +++ b/platform/android/src/test/render_test_runner.cpp @@ -1,15 +1,17 @@ #include #include +#include + +#include +#include + #include "jni.hpp" -#include "logger.hpp" +#include +#include #include #include -#include - -#include - namespace mbgl { namespace { @@ -18,16 +20,12 @@ 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; } @@ -41,17 +39,252 @@ void Log::platformRecord(EventSeverity severity, const std::string& msg) { } // 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) { + 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 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 fileName = + jstringToStdString(env, static_cast(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(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 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 + void android_main(struct android_app* app) { mbgl::android::theJVM = app->activity->vm; - JNIEnv* env; + JNIEnv* env = nullptr; app->activity->vm->AttachCurrentThread(&env, NULL); - std::vector arguments = {"mbgl-render-test-runner", "-p", "/sdcard/render-test/android-manifest.json"}; - std::vector argv; - for (const auto& arg : arguments) { - argv.push_back((char*)arg.data()); + 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); + + std::string configFile = storagePath + "/android-manifest.json"; + std::vector arguments = {"mbgl-render-test-runner", "-p", configFile}; + std::vector argv; + for (const auto& arg : arguments) { + argv.push_back((char*)arg.data()); + } + argv.push_back(nullptr); + + int finishedTestCount = 0; + std::function testStatus = [&]() { + ALooper_pollAll(0, &outFd, &outEvents, reinterpret_cast(&source)); + + if (source != nullptr) { + source->process(app, source); + } + + mbgl::Log::Info(mbgl::Event::General, "Current finished tests number is '%d' ", ++finishedTestCount); + }; + + mbgl::runRenderTests(argv.size() - 1, argv.data(), testStatus); + mbgl::Log::Info(mbgl::Event::General, "All tests are finished!"); + changeState(env, app); + } + while (true) { + ALooper_pollAll(0, &outFd, &outEvents, reinterpret_cast(&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; + } } - argv.push_back(nullptr); - (void)mbgl::runRenderTests(argv.size() - 1, argv.data()); - app->activity->vm->DetachCurrentThread(); -} \ No newline at end of file +} diff --git a/platform/default/src/mbgl/render-test/main.cpp b/platform/default/src/mbgl/render-test/main.cpp index 9b22b20e00..aed0e01d39 100644 --- a/platform/default/src/mbgl/render-test/main.cpp +++ b/platform/default/src/mbgl/render-test/main.cpp @@ -1,5 +1,5 @@ #include int main(int argc, char *argv[]) { - return mbgl::runRenderTests(argc, argv); + return mbgl::runRenderTests(argc, argv, []() {}); } diff --git a/platform/ios/test/MGLMapViewPitchTests.m b/platform/ios/test/MGLMapViewPitchTests.m index fa657eb994..0317c1f8ff 100644 --- a/platform/ios/test/MGLMapViewPitchTests.m +++ b/platform/ios/test/MGLMapViewPitchTests.m @@ -153,7 +153,7 @@ // Set the map camera to a pitched state, perhaps from a previous gesture or camera movement. self.mapView.camera = [self.mapView cameraByTiltingToPitch:initialTilt]; - XCTAssertEqual(self.mapView.camera.pitch, initialTilt, @"Tilt should initially be set to %.f°.", initialTilt); + XCTAssertEqualWithAccuracy(self.mapView.camera.pitch, initialTilt, 10e-6, @"Tilt should initially be set to %.f°.", initialTilt); // Initialize a tilt gesture. MockUIPanGestureRecognizer *gesture = [[MockUIPanGestureRecognizer alloc] initWithTarget:self.mapView action:nil]; diff --git a/render-test/android-manifest.json b/render-test/android-manifest.json index 56223d4753..7aa9eb26bc 100644 --- a/render-test/android-manifest.json +++ b/render-test/android-manifest.json @@ -1,7 +1,8 @@ { "base_test_path":"mapbox-gl-js/test/integration", - "expectation_paths":["render-test/expected/render-tests"], - "ignore_paths":["platform/node/test/ignores.json", "render-test/linux-ignores.json", "render-test/tests/should-fail.json"], + "expectation_paths":[], + "ignore_paths":["ignores/ignores.json", "ignores/linux-ignores.json"], "vendor_path":"vendor", - "asset_path": "mapbox-gl-js/test/integration" + "asset_path": "mapbox-gl-js/test/integration", + "result_path": "/sdcard/" } \ No newline at end of file diff --git a/render-test/android/README.md b/render-test/android/README.md index 270c970fce..4037419012 100644 --- a/render-test/android/README.md +++ b/render-test/android/README.md @@ -1,9 +1,3 @@ # RenderTestRunner -This app is a purely native application, with no Java source code, that can run **mbgl-render-test-runner** on android devices. - - -## Setup the test environment -- Run render_test_setup.sh so that all the necessary test resources are pushed to the device. - -- Switch on storage permission of the app so that it can read/write data on SD card. \ No newline at end of file +This app is a purely native application, with no Java source code, that can run **mbgl-render-test-runner** on android devices. \ No newline at end of file diff --git a/render-test/android/app/build.gradle b/render-test/android/app/build.gradle index 60609e3ba2..682af85dcf 100644 --- a/render-test/android/app/build.gradle +++ b/render-test/android/app/build.gradle @@ -14,6 +14,7 @@ android { targets 'mbgl-render-test-runner' } } + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } externalNativeBuild { cmake { @@ -26,4 +27,7 @@ android { dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + androidTestImplementation 'androidx.test.ext:junit:1.1.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-beta01' + androidTestImplementation 'androidx.test:rules:1.2.0-beta01' } diff --git a/render-test/android/app/src/androidTest/java/android/app/NativeActivityTest.java b/render-test/android/app/src/androidTest/java/android/app/NativeActivityTest.java new file mode 100644 index 0000000000..3d8c333902 --- /dev/null +++ b/render-test/android/app/src/androidTest/java/android/app/NativeActivityTest.java @@ -0,0 +1,29 @@ +package android.app; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; +import androidx.test.rule.ActivityTestRule; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import android.util.Log; + +@LargeTest +@RunWith(AndroidJUnit4.class) +public class NativeActivityTest { + + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(NativeActivity.class, false, false); + + @Test(timeout = 1200000L) + public void runRenderTests() throws Exception { + Log.v("Test", "Start the test"); + mActivityTestRule.launchActivity(null); + while (TestState.running) { + Log.v("Test", "Test is running"); + Thread.sleep(1000L); + } + Log.v("Test", "End the test"); + } +} \ No newline at end of file diff --git a/render-test/android/app/src/androidTest/java/android/app/TestState.java b/render-test/android/app/src/androidTest/java/android/app/TestState.java new file mode 100644 index 0000000000..44a0653fb6 --- /dev/null +++ b/render-test/android/app/src/androidTest/java/android/app/TestState.java @@ -0,0 +1,5 @@ +package android.app; + +public class TestState { + static boolean running = true; +} \ No newline at end of file diff --git a/render-test/android/app/src/main/AndroidManifest.xml b/render-test/android/app/src/main/AndroidManifest.xml index 6c7af7ed8f..8df48ef97f 100644 --- a/render-test/android/app/src/main/AndroidManifest.xml +++ b/render-test/android/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ android:fullBackupContent="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" - android:hasCode="false"> + android:hasCode="true"> + namespace mbgl { -int runRenderTests(int argc, char* argv[]); +int runRenderTests(int argc, char* argv[], std::function); } // namespace mbgl diff --git a/render-test/manifest_parser.cpp b/render-test/manifest_parser.cpp index 3f1f9f3866..6bba33754a 100644 --- a/render-test/manifest_parser.cpp +++ b/render-test/manifest_parser.cpp @@ -34,9 +34,15 @@ const std::vector>& Manifest::getIgnores() c const std::string& Manifest::getTestRootPath() const { return testRootPath; } +const std::string& Manifest::getAssetPath() const { + return assetPath; +} const std::string& Manifest::getManifestPath() const { return manifestPath; } +const std::string& Manifest::getResultPath() const { + return resultPath; +} void Manifest::doShuffle(uint32_t seed) { std::seed_seq sequence{seed}; @@ -239,7 +245,7 @@ mbgl::filesystem::path getValidPath(const std::string& manifestPath, const std:: result = BasePath / result; } if (mbgl::filesystem::exists(result)) { - return result; + return result.lexically_normal(); } mbgl::Log::Warning(mbgl::Event::General, "Invalid path is provoided inside the manifest file: %s", path.c_str()); return mbgl::filesystem::path{}; @@ -285,6 +291,18 @@ mbgl::optional ManifestParser::parseManifest(const std::string& manife return mbgl::nullopt; } } + if (document.HasMember("result_path")) { + const auto& resultPathValue = document["result_path"]; + if (!resultPathValue.IsString()) { + mbgl::Log::Warning( + mbgl::Event::General, "Invalid assetPath is provoided inside the manifest file: %s", filePath.c_str()); + return mbgl::nullopt; + } + manifest.resultPath = (getValidPath(manifest.manifestPath, resultPathValue.GetString()) / "").string(); + if (manifest.resultPath.empty()) { + return mbgl::nullopt; + } + } mbgl::filesystem::path baseTestPath; if (document.HasMember("base_test_path")) { const auto& testPathValue = document["base_test_path"]; @@ -366,6 +384,11 @@ mbgl::optional ManifestParser::parseManifest(const std::string& manife if (manifest.manifestPath.back() == '/') { manifest.manifestPath.pop_back(); } + if (manifest.resultPath.empty()) { + manifest.resultPath = manifest.manifestPath; + } else if (manifest.resultPath.back() == '/') { + manifest.resultPath.pop_back(); + } std::vector paths; for (const auto& id : testNames) { diff --git a/render-test/manifest_parser.hpp b/render-test/manifest_parser.hpp index bc5adf1091..c4672fb4c5 100644 --- a/render-test/manifest_parser.hpp +++ b/render-test/manifest_parser.hpp @@ -17,7 +17,9 @@ public: const std::vector>& getIgnores() const; const std::vector& getTestPaths() const; const std::string& getTestRootPath() const; + const std::string& getAssetPath() const; const std::string& getManifestPath() const; + const std::string& getResultPath() const; void doShuffle(uint32_t seed); std::string localizeURL(const std::string& url) const; @@ -43,6 +45,7 @@ private: std::string testRootPath; std::string vendorPath; std::string assetPath; + std::string resultPath; std::vector> ignores; std::vector testPaths; }; diff --git a/render-test/render_test.cpp b/render-test/render_test.cpp index 38d6c15f3f..5753065a7e 100644 --- a/render-test/render_test.cpp +++ b/render-test/render_test.cpp @@ -100,7 +100,7 @@ ArgumentsTuple parseArguments(int argc, char** argv) { } // namespace namespace mbgl { -int runRenderTests(int argc, char** argv) { +int runRenderTests(int argc, char** argv, std::function testStatus) { bool recycleMap; bool shuffle; uint32_t seed; @@ -198,10 +198,12 @@ int runRenderTests(int argc, char** argv) { } metadatas.push_back(std::move(metadata)); + if (testStatus) { + testStatus(); + } } - const auto& testRootPath = manifest.getManifestPath(); const auto resultPath = - testRootPath + "/" + (testNames.empty() ? "render-tests" : testNames.front()) + "_index.html"; + manifest.getResultPath() + "/" + (testNames.empty() ? "render-tests" : testNames.front()) + "_index.html"; std::string resultsHTML = createResultPage(stats, metadatas, shuffle, seed); mbgl::util::write_file(resultPath, resultsHTML); diff --git a/render-test/runner.cpp b/render-test/runner.cpp index 0389b83575..ce76bda157 100644 --- a/render-test/runner.cpp +++ b/render-test/runner.cpp @@ -596,7 +596,7 @@ bool TestRunner::runOperations(const std::string& key, TestMetadata& metadata, R std::string imagePath = operationArray[2].GetString(); imagePath.erase(std::remove(imagePath.begin(), imagePath.end(), '"'), imagePath.end()); - const mbgl::filesystem::path filePath = mbgl::filesystem::path(manifest.getTestRootPath()) / imagePath; + const mbgl::filesystem::path filePath = (mbgl::filesystem::path(manifest.getAssetPath()) / imagePath); mbgl::optional maybeImage = mbgl::util::readFile(filePath.string()); if (!maybeImage) { diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index c9d19509f4..1bda870ce5 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -56,6 +56,7 @@ void Transform::resize(const Size size) { observer.onCameraWillChange(MapObserver::CameraChangeMode::Immediate); state.setSize(size); + state.constrain(); observer.onCameraDidChange(MapObserver::CameraChangeMode::Immediate); } @@ -192,7 +193,6 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima // Minimize rotation by taking the shorter path around the circle. bearing = _normalizeAngle(bearing, state.getBearing()); state.setBearing(_normalizeAngle(state.getBearing(), bearing)); - const double startZoom = state.scaleZoom(state.getScale()); const double startBearing = state.getBearing(); const double startPitch = state.getPitch(); @@ -290,41 +290,45 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima state.setRotating(bearing != startBearing); const EdgeInsets startEdgeInsets = state.getEdgeInsets(); - startTransition(camera, animation, [=](double k) { - /// s: The distance traveled along the flight path, measured in - /// ρ-screenfuls. - double s = k * S; - double us = k == 1.0 ? 1.0 : u(s); + startTransition( + camera, + animation, + [=](double k) { + /// s: The distance traveled along the flight path, measured in + /// ρ-screenfuls. + double s = k * S; + double us = k == 1.0 ? 1.0 : u(s); + + // Calculate the current point and zoom level along the flight path. + Point framePoint = util::interpolate(startPoint, endPoint, us); + double frameZoom = + linearZoomInterpolation ? util::interpolate(startZoom, zoom, k) : startZoom + state.scaleZoom(1 / w(s)); + + // Zoom can be NaN if size is empty. + if (std::isnan(frameZoom)) { + frameZoom = zoom; + } - // Calculate the current point and zoom level along the flight path. - Point framePoint = util::interpolate(startPoint, endPoint, us); - double frameZoom = linearZoomInterpolation ? util::interpolate(startZoom, zoom, k) - : startZoom + state.scaleZoom(1 / w(s)); + // Convert to geographic coordinates and set the new viewpoint. + LatLng frameLatLng = Projection::unproject(framePoint, startScale); + state.setLatLngZoom(frameLatLng, frameZoom); - // Zoom can be NaN if size is empty. - if (std::isnan(frameZoom)) { - frameZoom = zoom; - } - - // Convert to geographic coordinates and set the new viewpoint. - LatLng frameLatLng = Projection::unproject(framePoint, startScale); - state.setLatLngZoom(frameLatLng, frameZoom); - - if (bearing != startBearing) { - state.setBearing(util::wrap(util::interpolate(startBearing, bearing, k), -M_PI, M_PI)); - } - if (padding != startEdgeInsets) { - // Interpolate edge insets - state.setEdgeInsets({util::interpolate(startEdgeInsets.top(), padding.top(), k), - util::interpolate(startEdgeInsets.left(), padding.left(), k), - util::interpolate(startEdgeInsets.bottom(), padding.bottom(), k), - util::interpolate(startEdgeInsets.right(), padding.right(), k)}); - } - auto maxPitch = getMaxPitchForEdgeInsets(state.getEdgeInsets()); - if (pitch != startPitch || maxPitch < startPitch) { - state.setPitch(std::min(maxPitch, util::interpolate(startPitch, pitch, k))); - } - }, duration); + if (bearing != startBearing) { + state.setBearing(util::wrap(util::interpolate(startBearing, bearing, k), -M_PI, M_PI)); + } + if (padding != startEdgeInsets) { + // Interpolate edge insets + state.setEdgeInsets({util::interpolate(startEdgeInsets.top(), padding.top(), k), + util::interpolate(startEdgeInsets.left(), padding.left(), k), + util::interpolate(startEdgeInsets.bottom(), padding.bottom(), k), + util::interpolate(startEdgeInsets.right(), padding.right(), k)}); + } + auto maxPitch = getMaxPitchForEdgeInsets(state.getEdgeInsets()); + if (pitch != startPitch || maxPitch < startPitch) { + state.setPitch(std::min(maxPitch, util::interpolate(startPitch, pitch, k))); + } + }, + duration); } #pragma mark - Position @@ -400,6 +404,7 @@ double Transform::getPitch() const { void Transform::setNorthOrientation(NorthOrientation orientation) { state.setNorthOrientation(orientation); + state.constrain(); } NorthOrientation Transform::getNorthOrientation() const { @@ -410,6 +415,7 @@ NorthOrientation Transform::getNorthOrientation() const { void Transform::setConstrainMode(mbgl::ConstrainMode mode) { state.setConstrainMode(mode); + state.constrain(); } ConstrainMode Transform::getConstrainMode() const { @@ -432,6 +438,7 @@ void Transform::setProjectionMode(const ProjectionMode& options) { state.setAxonometric(options.axonometric.value_or(state.getAxonometric())); state.setXSkew(options.xSkew.value_or(state.getXSkew())); state.setYSkew(options.ySkew.value_or(state.getYSkew())); + state.updateMatrix(); } ProjectionMode Transform::getProjectionMode() const { @@ -499,6 +506,7 @@ void Transform::startTransition(const CameraOptions& camera, animation.transitionFinishFn(); } observer.onCameraDidChange(isAnimated ? MapObserver::CameraChangeMode::Animated : MapObserver::CameraChangeMode::Immediate); + state.updateMatrix(); }; if (!isAnimated) { diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 5c2ae6abad..ccbde3c349 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -108,17 +108,21 @@ void TransformState::updateMatrix() { if (size.isEmpty()) { return; } - coordiMatrix = coordinatePointMatrix(); - mat4 mat = coordiMatrix; + if (matrixUpdated) return; + coordMatrix = coordinatePointMatrix(); + mat4 mat = coordMatrix; bool err = matrix::invert(invertedMatrix, mat); if (err) throw std::runtime_error("failed to invert coordinatePointMatrix"); + matrixUpdated = true; } void TransformState::setSize(const Size& size_) { - size = size_; - constrain(); + if (size != size_) { + size = size_; + matrixUpdated = false; + } updateMatrix(); } @@ -134,8 +138,11 @@ NorthOrientation TransformState::getNorthOrientation() const { } void TransformState::setNorthOrientation(const NorthOrientation& val) { - orientation = val; - constrain(); + if (orientation != val) { + orientation = val; + matrixUpdated = false; + } + updateMatrix(); } double TransformState::getNorthOrientationAngle() const { @@ -157,9 +164,13 @@ ConstrainMode TransformState::getConstrainMode() const { } void TransformState::setConstrainMode(const ConstrainMode& val) { - constrainMode = val; - constrain(); + if (constrainMode != val) { + constrainMode = val; + matrixUpdated = false; + } + updateMatrix(); } + #pragma mark - ViewportMode ViewportMode TransformState::getViewportMode() const { @@ -182,8 +193,9 @@ CameraOptions TransformState::getCameraOptions(optional padding) con void TransformState::setEdgeInsets(const EdgeInsets& val) { if (edgeInsets != val) { edgeInsets = val; - updateMatrix(); + matrixUpdated = false; } + updateMatrix(); } #pragma mark - Position @@ -267,8 +279,9 @@ float TransformState::getBearing() const { void TransformState::setBearing(float val) { if (bearing != val) { bearing = val; - updateMatrix(); + matrixUpdated = false; } + updateMatrix(); } float TransformState::getFieldOfView() const { @@ -286,22 +299,25 @@ float TransformState::getPitch() const { void TransformState::setPitch(float val) { if (pitch != val) { pitch = val; - updateMatrix(); + matrixUpdated = false; } + updateMatrix(); } void TransformState::setXSkew(double val) { if (xSkew != val) { xSkew = val; - updateMatrix(); + matrixUpdated = false; } + updateMatrix(); } void TransformState::setYSkew(double val) { if (ySkew != val) { ySkew = val; - updateMatrix(); + matrixUpdated = false; } + updateMatrix(); } #pragma mark - State @@ -343,8 +359,8 @@ ScreenCoordinate TransformState::latLngToScreenCoordinate(const LatLng& latLng) vec4 p; Point pt = Projection::project(latLng, scale) / util::tileSize; vec4 c = {{ pt.x, pt.y, 0, 1 }}; - matrix::transformMat4(p, c, coordiMatrix); - return { p[0] / p[3], size.height - p[1] / p[3] }; + matrix::transformMat4(p, c, coordMatrix); + return {p[0] / p[3], size.height - p[1] / p[3]}; } TileCoordinate TransformState::screenCoordinateToTileCoordinate(const ScreenCoordinate& point, uint8_t atZoom) const { @@ -403,7 +419,6 @@ mat4 TransformState::getPixelMatrix() const { return m; } - #pragma mark - (private helper functions) bool TransformState::rotatedNorth() const { @@ -413,8 +428,10 @@ bool TransformState::rotatedNorth() const { void TransformState::constrain() { constrain(scale, x, y); + matrixUpdated = false; updateMatrix(); } + void TransformState::constrain(double& scale_, double& x_, double& y_) const { if (constrainMode == ConstrainMode::None) { return; @@ -463,10 +480,11 @@ void TransformState::setLatLngZoom(const LatLng& latLng, double zoom) { 0.5 * Cc * std::log((1 + f) / (1 - f)), }; setScalePoint(newScale, point); + matrixUpdated = false; updateMatrix(); } -void TransformState::setScalePoint(const double newScale, const ScreenCoordinate &point) { +void TransformState::setScalePoint(const double newScale, const ScreenCoordinate& point) { double constrainedScale = newScale; ScreenCoordinate constrainedPoint = point; constrain(constrainedScale, constrainedPoint.x, constrainedPoint.y); @@ -499,7 +517,7 @@ float TransformState::maxPitchScaleFactor() const { Point pt = Projection::project(latLng, scale) / util::tileSize; vec4 p = {{ pt.x, pt.y, 0, 1 }}; vec4 topPoint; - matrix::transformMat4(topPoint, p, coordiMatrix); + matrix::transformMat4(topPoint, p, coordMatrix); return topPoint[3] / getCameraToCenterDistance(); } diff --git a/src/mbgl/map/transform_state.hpp b/src/mbgl/map/transform_state.hpp index 8e2b4b273c..a82f59a9b3 100644 --- a/src/mbgl/map/transform_state.hpp +++ b/src/mbgl/map/transform_state.hpp @@ -114,10 +114,11 @@ public: void setLatLngZoom(const LatLng& latLng, double zoom); -private: + void constrain(); void updateMatrix(); + +private: bool rotatedNorth() const; - void constrain(); void constrain(double& scale, double& x, double& y) const; // Viewport center offset, from [size.width / 2, size.height / 2], defined @@ -173,7 +174,8 @@ private: double Bc = Projection::worldSize(scale) / util::DEGREES_MAX; double Cc = Projection::worldSize(scale) / util::M2PI; - mat4 coordiMatrix; + bool matrixUpdated{false}; + mat4 coordMatrix; mat4 invertedMatrix; }; -- cgit v1.2.1