diff options
author | Juha Alanen <juha.alanen@mapbox.com> | 2019-10-02 13:26:39 +0300 |
---|---|---|
committer | Juha Alanen <19551460+jmalanen@users.noreply.github.com> | 2019-10-07 10:58:46 +0300 |
commit | 13801f1b80e21e128b851caa58a9f4bbd2421844 (patch) | |
tree | 9198929d4701cb32c72458168eff49b76a2c18d5 /render-test | |
parent | 18a426d74808605d7bb333a56de0cb5493094f20 (diff) | |
download | qtlocation-mapboxgl-13801f1b80e21e128b851caa58a9f4bbd2421844.tar.gz |
[render-test] Add support for query tests
Diffstat (limited to 'render-test')
-rw-r--r-- | render-test/expected/render-tests/debug/collision-lines-overscaled/expected.png (renamed from render-test/expected/debug/collision-lines-overscaled/expected.png) | bin | 7774 -> 7774 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/debug/collision-lines-pitched/expected.png (renamed from render-test/expected/debug/collision-lines-pitched/expected.png) | bin | 165500 -> 165500 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/debug/collision-lines/expected.png (renamed from render-test/expected/debug/collision-lines/expected.png) | bin | 196454 -> 196454 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/debug/collision-pitched-wrapped/expected.png (renamed from render-test/expected/debug/collision-pitched-wrapped/expected.png) | bin | 69725 -> 69725 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/symbol-visibility/visible/expected.png (renamed from render-test/expected/symbol-visibility/visible/expected.png) | bin | 14729 -> 14729 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/text-pitch-alignment/auto-text-rotation-alignment-map/expected.png (renamed from render-test/expected/text-pitch-alignment/auto-text-rotation-alignment-map/expected.png) | bin | 26157 -> 26157 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/text-pitch-alignment/map-text-rotation-alignment-map/expected.png (renamed from render-test/expected/text-pitch-alignment/map-text-rotation-alignment-map/expected.png) | bin | 26157 -> 26157 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/text-pitch-alignment/viewport-text-rotation-alignment-map/expected.png (renamed from render-test/expected/text-pitch-alignment/viewport-text-rotation-alignment-map/expected.png) | bin | 26866 -> 26866 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/text-variable-anchor/pitched-rotated-debug/expected.png (renamed from render-test/expected/text-variable-anchor/pitched-rotated-debug/expected.png) | bin | 43135 -> 43135 bytes | |||
-rw-r--r-- | render-test/expected/render-tests/text-variable-anchor/rotated-offset/expected.png (renamed from render-test/expected/text-variable-anchor/rotated-offset/expected.png) | bin | 35553 -> 35553 bytes | |||
-rw-r--r-- | render-test/main.cpp | 2 | ||||
-rw-r--r-- | render-test/metadata.hpp | 7 | ||||
-rw-r--r-- | render-test/parser.cpp | 134 | ||||
-rw-r--r-- | render-test/parser.hpp | 3 | ||||
-rw-r--r-- | render-test/runner.cpp | 254 | ||||
-rw-r--r-- | render-test/runner.hpp | 5 |
16 files changed, 387 insertions, 18 deletions
diff --git a/render-test/expected/debug/collision-lines-overscaled/expected.png b/render-test/expected/render-tests/debug/collision-lines-overscaled/expected.png Binary files differindex 38eb0d2da6..38eb0d2da6 100644 --- a/render-test/expected/debug/collision-lines-overscaled/expected.png +++ b/render-test/expected/render-tests/debug/collision-lines-overscaled/expected.png diff --git a/render-test/expected/debug/collision-lines-pitched/expected.png b/render-test/expected/render-tests/debug/collision-lines-pitched/expected.png Binary files differindex 416d7d5715..416d7d5715 100644 --- a/render-test/expected/debug/collision-lines-pitched/expected.png +++ b/render-test/expected/render-tests/debug/collision-lines-pitched/expected.png diff --git a/render-test/expected/debug/collision-lines/expected.png b/render-test/expected/render-tests/debug/collision-lines/expected.png Binary files differindex 3f4790a585..3f4790a585 100644 --- a/render-test/expected/debug/collision-lines/expected.png +++ b/render-test/expected/render-tests/debug/collision-lines/expected.png diff --git a/render-test/expected/debug/collision-pitched-wrapped/expected.png b/render-test/expected/render-tests/debug/collision-pitched-wrapped/expected.png Binary files differindex 9b718c09c0..9b718c09c0 100644 --- a/render-test/expected/debug/collision-pitched-wrapped/expected.png +++ b/render-test/expected/render-tests/debug/collision-pitched-wrapped/expected.png diff --git a/render-test/expected/symbol-visibility/visible/expected.png b/render-test/expected/render-tests/symbol-visibility/visible/expected.png Binary files differindex 8da157772a..8da157772a 100644 --- a/render-test/expected/symbol-visibility/visible/expected.png +++ b/render-test/expected/render-tests/symbol-visibility/visible/expected.png diff --git a/render-test/expected/text-pitch-alignment/auto-text-rotation-alignment-map/expected.png b/render-test/expected/render-tests/text-pitch-alignment/auto-text-rotation-alignment-map/expected.png Binary files differindex cd690ca152..cd690ca152 100644 --- a/render-test/expected/text-pitch-alignment/auto-text-rotation-alignment-map/expected.png +++ b/render-test/expected/render-tests/text-pitch-alignment/auto-text-rotation-alignment-map/expected.png diff --git a/render-test/expected/text-pitch-alignment/map-text-rotation-alignment-map/expected.png b/render-test/expected/render-tests/text-pitch-alignment/map-text-rotation-alignment-map/expected.png Binary files differindex cd690ca152..cd690ca152 100644 --- a/render-test/expected/text-pitch-alignment/map-text-rotation-alignment-map/expected.png +++ b/render-test/expected/render-tests/text-pitch-alignment/map-text-rotation-alignment-map/expected.png diff --git a/render-test/expected/text-pitch-alignment/viewport-text-rotation-alignment-map/expected.png b/render-test/expected/render-tests/text-pitch-alignment/viewport-text-rotation-alignment-map/expected.png Binary files differindex 764d4a0b24..764d4a0b24 100644 --- a/render-test/expected/text-pitch-alignment/viewport-text-rotation-alignment-map/expected.png +++ b/render-test/expected/render-tests/text-pitch-alignment/viewport-text-rotation-alignment-map/expected.png diff --git a/render-test/expected/text-variable-anchor/pitched-rotated-debug/expected.png b/render-test/expected/render-tests/text-variable-anchor/pitched-rotated-debug/expected.png Binary files differindex 4e3d012844..4e3d012844 100644 --- a/render-test/expected/text-variable-anchor/pitched-rotated-debug/expected.png +++ b/render-test/expected/render-tests/text-variable-anchor/pitched-rotated-debug/expected.png diff --git a/render-test/expected/text-variable-anchor/rotated-offset/expected.png b/render-test/expected/render-tests/text-variable-anchor/rotated-offset/expected.png Binary files differindex 13690d147c..13690d147c 100644 --- a/render-test/expected/text-variable-anchor/rotated-offset/expected.png +++ b/render-test/expected/render-tests/text-variable-anchor/rotated-offset/expected.png diff --git a/render-test/main.cpp b/render-test/main.cpp index fcdbe3ab55..6f5e2449a9 100644 --- a/render-test/main.cpp +++ b/render-test/main.cpp @@ -81,7 +81,7 @@ int main(int argc, char** argv) { bool shouldIgnore = false; std::string ignoreReason; - const std::string ignoreName = "render-tests/" + id; + const std::string ignoreName = id; const auto it = std::find_if(ignores.cbegin(), ignores.cend(), [&ignoreName](auto pair) { return pair.first == ignoreName; }); if (it != ignores.end()) { shouldIgnore = true; diff --git a/render-test/metadata.hpp b/render-test/metadata.hpp index d23a0fb296..d25b81c7ab 100644 --- a/render-test/metadata.hpp +++ b/render-test/metadata.hpp @@ -1,9 +1,11 @@ #pragma once +#include <mbgl/util/geo.hpp> #include <mbgl/util/rapidjson.hpp> #include <mbgl/util/size.hpp> #include <mbgl/map/mode.hpp> +#include <mbgl/renderer/query.hpp> #include "filesystem.hpp" @@ -50,6 +52,7 @@ struct TestMetadata { TestPaths paths; mbgl::JSDocument document; + bool renderTest = true; mbgl::Size size{ 512u, 512u }; float pixelRatio = 1.0f; @@ -61,6 +64,9 @@ struct TestMetadata { bool axonometric = false; double xSkew = 0.0; double ySkew = 1.0; + mbgl::ScreenCoordinate queryGeometry{0u, 0u}; + mbgl::ScreenBox queryGeometryBox{{0u, 0u}, {0u, 0u}}; + mbgl::RenderedQueryOptions queryOptions; // TODO uint32_t fadeDuration = 0; @@ -72,6 +78,7 @@ struct TestMetadata { std::string color; std::string actual; + std::string actualJson; std::string expected; std::string diff; diff --git a/render-test/parser.cpp b/render-test/parser.cpp index 9b462dee72..802c3c7f55 100644 --- a/render-test/parser.cpp +++ b/render-test/parser.cpp @@ -5,9 +5,14 @@ #include <args.hxx> +#include <rapidjson/prettywriter.h> #include <rapidjson/stringbuffer.h> #include <rapidjson/writer.h> +#include <mapbox/geojson_impl.hpp> +#include <mbgl/style/conversion/filter.hpp> +#include <mbgl/style/conversion/json.hpp> + #include <boost/archive/iterators/base64_from_binary.hpp> #include <boost/archive/iterators/insert_linebreaks.hpp> #include <boost/archive/iterators/transform_width.hpp> @@ -179,8 +184,67 @@ TestPaths makeTestPaths(mbgl::filesystem::path stylePath) { }; } +void writeJSON(rapidjson::PrettyWriter<rapidjson::StringBuffer>& writer, const mbgl::Value& value) { + value.match([&writer](const mbgl::NullValue&) { writer.Null(); }, + [&writer](bool b) { writer.Bool(b); }, + [&writer](uint64_t u) { writer.Uint64(u); }, + [&writer](int64_t i) { writer.Int64(i); }, + [&writer](double d) { d == std::floor(d) ? writer.Int64(d) : writer.Double(d); }, + [&writer](const std::string& s) { writer.String(s); }, + [&writer](const std::vector<mbgl::Value>& arr) { + writer.StartArray(); + for (const auto& item : arr) { + writeJSON(writer, item); + } + writer.EndArray(); + }, + [&writer](const std::unordered_map<std::string, mbgl::Value>& obj) { + writer.StartObject(); + std::map<std::string, mbgl::Value> sorted(obj.begin(), obj.end()); + for (const auto& entry : sorted) { + writer.Key(entry.first.c_str()); + writeJSON(writer, entry.second); + } + writer.EndObject(); + }); +} + } // namespace +std::string toJSON(const mbgl::Value& value, unsigned indent, bool singleLine) { + rapidjson::StringBuffer buffer; + rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); + if (singleLine) { + writer.SetFormatOptions(rapidjson::kFormatSingleLineArray); + } + writer.SetIndent(' ', indent); + writeJSON(writer, value); + return buffer.GetString(); +} + +std::string toJSON(const std::vector<mbgl::Feature>& features, unsigned indent, bool singleLine) { + rapidjson::CrtAllocator allocator; + rapidjson::StringBuffer buffer; + rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); + if (singleLine) { + writer.SetFormatOptions(rapidjson::kFormatSingleLineArray); + } + writer.SetIndent(' ', indent); + writer.StartArray(); + for (size_t i = 0; i < features.size(); ++i) { + auto result = mapbox::geojson::convert(features[i], allocator); + + result.AddMember("source", features[i].source, allocator); + if (!features[i].sourceLayer.empty()) { + result.AddMember("sourceLayer", features[i].sourceLayer, allocator); + } + result.AddMember("state", mapbox::geojson::to_value{allocator}(features[i].state), allocator); + result.Accept(writer); + } + writer.EndArray(); + return buffer.GetString(); +} + JSONReply readJson(const mbgl::filesystem::path& jsonPath) { auto maybeJSON = mbgl::util::readFile(jsonPath); if (!maybeJSON) { @@ -228,7 +292,7 @@ std::string serializeMetrics(const TestMetrics& metrics) { } std::vector<std::string> readExpectedEntries(const mbgl::filesystem::path& base) { - static const std::regex regex(".*expected.*.png"); + static const std::regex regex(".*expected.*.png|.*expected.*.json"); std::vector<std::string> expectedImages; for (const auto& entry : mbgl::filesystem::directory_iterator(base)) { @@ -476,6 +540,45 @@ TestMetadata parseTestMetadata(const TestPaths& paths) { metadata.ySkew = testValue["skew"][1].GetDouble(); } + if (testValue.HasMember("queryGeometry")) { + assert(testValue["queryGeometry"].IsArray()); + if (testValue["queryGeometry"][0].IsNumber() && testValue["queryGeometry"][1].IsNumber()) { + metadata.queryGeometry.x = testValue["queryGeometry"][0].GetDouble(); + metadata.queryGeometry.y = testValue["queryGeometry"][1].GetDouble(); + } else if (testValue["queryGeometry"][0].IsArray() && testValue["queryGeometry"][1].IsArray()) { + metadata.queryGeometryBox.min.x = testValue["queryGeometry"][0][0].GetDouble(); + metadata.queryGeometryBox.min.y = testValue["queryGeometry"][0][1].GetDouble(); + metadata.queryGeometryBox.max.x = testValue["queryGeometry"][1][0].GetDouble(); + metadata.queryGeometryBox.max.y = testValue["queryGeometry"][1][1].GetDouble(); + } + metadata.renderTest = false; + } + + if (testValue.HasMember("queryOptions")) { + assert(testValue["queryOptions"].IsObject()); + + if (testValue["queryOptions"].HasMember("layers")) { + assert(testValue["queryOptions"]["layers"].IsArray()); + auto layersArray = testValue["queryOptions"]["layers"].GetArray(); + std::vector<std::string> layersVec; + for (uint32_t i = 0; i < layersArray.Size(); i++) { + layersVec.emplace_back(testValue["queryOptions"]["layers"][i].GetString()); + } + metadata.queryOptions.layerIDs = layersVec; + } + + using namespace mbgl::style; + using namespace mbgl::style::conversion; + if (testValue["queryOptions"].HasMember("filter")) { + assert(testValue["queryOptions"]["filter"].IsArray()); + auto& filterVal = testValue["queryOptions"]["filter"]; + Error error; + mbgl::optional<Filter> converted = convert<Filter>(filterVal, error); + assert(converted); + metadata.queryOptions.filter = std::move(*converted); + } + } + // TODO: fadeDuration // TODO: addFakeCanvas @@ -499,20 +602,31 @@ std::string createResultItem(const TestMetadata& metadata, bool hasFailedTests) html.append("<div class=\"test " + metadata.status + (shouldHide ? " hide" : "") + "\">\n"); html.append(R"(<h2><span class="label" style="background: )" + metadata.color + "\">" + metadata.status + "</span> " + metadata.id + "</h2>\n"); if (metadata.status != "errored") { - html.append("<img width=" + mbgl::util::toString(metadata.size.width)); - html.append(" height=" + mbgl::util::toString(metadata.size.height)); - html.append(" src=\"data:image/png;base64," + encodeBase64(metadata.actual) + "\""); - html.append(" data-alt-src=\"data:image/png;base64," + encodeBase64(metadata.expected) + "\">\n"); - - html.append("<img width=" + mbgl::util::toString(metadata.size.width)); - html.append(" height=" + mbgl::util::toString(metadata.size.height)); - html.append(" src=\"data:image/png;base64," + encodeBase64(metadata.diff) + "\">\n"); + if (metadata.renderTest) { + html.append("<img width=" + mbgl::util::toString(metadata.size.width)); + html.append(" height=" + mbgl::util::toString(metadata.size.height)); + html.append(" src=\"data:image/png;base64," + encodeBase64(metadata.actual) + "\""); + html.append(" data-alt-src=\"data:image/png;base64," + encodeBase64(metadata.expected) + "\">\n"); + + html.append("<img width=" + mbgl::util::toString(metadata.size.width)); + html.append(" height=" + mbgl::util::toString(metadata.size.height)); + html.append(" src=\"data:image/png;base64," + encodeBase64(metadata.diff) + "\">\n"); + } else { + html.append("<img width=" + mbgl::util::toString(metadata.size.width)); + html.append(" height=" + mbgl::util::toString(metadata.size.height)); + html.append(" src=\"data:image/png;base64," + encodeBase64(metadata.actual) + "\">\n"); + } } else { assert(!metadata.errorMessage.empty()); html.append("<p style=\"color: red\"><strong>Error:</strong> " + metadata.errorMessage + "</p>\n"); } if (metadata.difference != 0.0) { - html.append("<p class=\"diff\"><strong>Diff:</strong> " + mbgl::util::toString(metadata.difference) + "</p>\n"); + if (metadata.renderTest) { + html.append("<p class=\"diff\"><strong>Diff:</strong> " + mbgl::util::toString(metadata.difference) + + "</p>\n"); + } else { + html.append("<p class=\"diff\"><strong>Diff:</strong> " + metadata.diff + "</p>\n"); + } } html.append("</div>\n"); diff --git a/render-test/parser.hpp b/render-test/parser.hpp index 94fb212944..afa281ad30 100644 --- a/render-test/parser.hpp +++ b/render-test/parser.hpp @@ -31,5 +31,8 @@ std::string createResultPage(const TestStatistics&, const std::vector<TestMetada std::string localizeURL(const std::string& url); +std::string toJSON(const mbgl::Value& value, unsigned indent, bool singleLine); +std::string toJSON(const std::vector<mbgl::Feature>& features, unsigned indent, bool singleLine); + void localizeSourceURLs(mbgl::JSValue& root, mbgl::JSDocument& document); void localizeStyleURLs(mbgl::JSValue& root, mbgl::JSDocument& document);
\ No newline at end of file diff --git a/render-test/runner.cpp b/render-test/runner.cpp index fa7a86e948..868e1a9a32 100644 --- a/render-test/runner.cpp +++ b/render-test/runner.cpp @@ -30,10 +30,11 @@ #include <utility> #include <sstream> +using namespace mbgl; + // static const std::string& TestRunner::getBasePath() { - const static std::string result = - std::string(TEST_RUNNER_ROOT_PATH).append("/mapbox-gl-js/test/integration/render-tests"); + const static std::string result = std::string(TEST_RUNNER_ROOT_PATH).append("/mapbox-gl-js/test/integration"); return result; } @@ -46,7 +47,236 @@ const std::vector<std::string>& TestRunner::getPlatformExpectationsPaths() { return result; } -bool TestRunner::checkResults(mbgl::PremultipliedImage&& actualImage, TestMetadata& metadata) { +// Strip precision for numbers, so that we can compare evaluated results with fixtures. +// Copied from JS expression harness. +Value stripPrecision(const Value& value) { + const double decimalSigFigs = 6; + if (auto num = numericValue<double>(value)) { + if (*num == 0) { + return *num; + } + + const double multiplier = std::pow(10, std::max(0.0, decimalSigFigs - std::ceil(std::log10(std::fabs(*num))))); + + // We strip precision twice in a row here to avoid cases where + // stripping an already stripped number will modify its value + // due to bad floating point precision luck + // eg `Math.floor(8.16598 * 100000) / 100000` -> 8.16597 + const double firstStrip = std::floor(*num * multiplier) / multiplier; + return std::floor(firstStrip * multiplier) / multiplier; + } + + if (value.getArray()) { + std::vector<Value> stripped; + const auto& vec = *value.getArray(); + stripped.reserve(vec.size()); + for (const auto& val : vec) { + stripped.emplace_back(stripPrecision(val)); + } + return stripped; + } else if (value.getObject()) { + std::unordered_map<std::string, Value> stripped; + const auto& map = *value.getObject(); + for (const auto& pair : map) { + stripped.emplace(pair.first, stripPrecision(pair.second)); + } + return stripped; + } + + return value; +} + +bool deepEqual(const Value& a, const Value& b) { + const auto& anum = numericValue<double>(a); + const auto& bnum = numericValue<double>(b); + if (anum && bnum) { + return stripPrecision(*anum) == stripPrecision(*bnum); + } + + if (a.which() != b.which()) { + return false; + } + + if (a.getArray() && b.getArray()) { + const auto& avec = *a.getArray(); + const auto& bvec = *b.getArray(); + if (avec.size() != bvec.size()) { + return false; + } + for (std::size_t i = 0; i < avec.size(); ++i) { + if (!deepEqual(avec[i], bvec[i])) { + return false; + } + } + return true; + } + + if (a.getObject() && b.getObject()) { + const auto& amap = *a.getObject(); + const auto& bmap = *b.getObject(); + if (amap.size() != bmap.size()) { + return false; + } + for (const auto& pair : amap) { + auto it = bmap.find(pair.first); + if (it == bmap.end()) { + return false; + } + if (!deepEqual(pair.second, it->second)) { + return false; + } + } + return true; + } + + if (a == b) { + return true; + } + + if (a.getString() && b.getString()) { + const auto& strA = *a.getString(); + const auto& strB = *b.getString(); + if (strA == strB) { + return true; + } + + try { + double numA = std::stod(strA); + double numB = std::stod(strB); + return stripPrecision(numA) == stripPrecision(numB); + } catch (...) { + } + } + + return false; +} + +std::vector<std::string> tokenize(std::string str) { + std::vector<std::string> tokens; + std::regex re("\n"); + std::copy(std::regex_token_iterator<std::string::iterator>(str.begin(), str.end(), re, -1), + std::regex_token_iterator<std::string::iterator>(), + std::back_inserter(tokens)); + return tokens; +} + +std::string simpleDiff(const Value& result, const Value& expected) { + std::vector<std::string> resultTokens{tokenize(toJSON(result, 2, false))}; + std::vector<std::string> expectedTokens{tokenize(toJSON(expected, 2, false))}; + std::size_t maxLength = std::max(resultTokens.size(), expectedTokens.size()); + std::ostringstream diff; + + diff << "<pre>" << std::endl; + const auto flush = + [](const std::vector<std::string>& vec, std::size_t pos, std::ostringstream& out, std::string separator) { + for (std::size_t j = pos; j < vec.size(); ++j) { + out << separator << vec[j] << std::endl; + } + }; + + for (std::size_t i = 0; i < maxLength; ++i) { + if (resultTokens.size() <= i) { + flush(expectedTokens, i, diff, "-"); + break; + } + + if (expectedTokens.size() <= i) { + flush(resultTokens, i, diff, "+"); + break; + } + + if (!deepEqual(resultTokens[i], expectedTokens[i])) { + diff << "<b>" + << "-" << expectedTokens[i] << "</b>" << std::endl; + diff << "<b>" + << "+" << resultTokens[i] << "</b>" << std::endl; + } else { + diff << resultTokens[i] << std::endl; + } + } + diff << "</pre>" << std::endl; + return diff.str(); +} + +bool TestRunner::checkQueryTestResults(mbgl::PremultipliedImage&& actualImage, + std::vector<mbgl::Feature>&& features, + TestMetadata& metadata) { + const std::string& base = metadata.paths.defaultExpectations(); + const std::vector<mbgl::filesystem::path>& expectations = metadata.paths.expectations; + + metadata.actual = mbgl::encodePNG(actualImage); + + if (actualImage.size.isEmpty()) { + metadata.errorMessage = "Invalid size for actual image"; + return false; + } + + metadata.actualJson = toJSON(features, 2, false); + + if (metadata.actualJson.empty()) { + metadata.errorMessage = "Invalid size for actual JSON"; + return false; + } + +#if !TEST_READ_ONLY + if (getenv("UPDATE_PLATFORM")) { + mbgl::filesystem::create_directories(expectations.back()); + mbgl::util::write_file(expectations.back().string() + "/expected.json", metadata.actualJson); + return true; + } else if (getenv("UPDATE_DEFAULT")) { + mbgl::util::write_file(base + "/expected.json", metadata.actualJson); + return true; + } + + mbgl::util::write_file(base + "/actual.json", metadata.actualJson); +#endif + + std::vector<std::string> expectedJsonPaths; + mbgl::filesystem::path expectedMetricsPath; + for (auto rit = expectations.rbegin(); rit != expectations.rend(); ++rit) { + if (mbgl::filesystem::exists(*rit)) { + expectedJsonPaths = readExpectedEntries(*rit); + if (!expectedJsonPaths.empty()) break; + } + } + + if (expectedJsonPaths.empty()) { + metadata.errorMessage = "Failed to find expectations for: " + metadata.paths.stylePath.string(); + return false; + } + + for (const auto& entry : expectedJsonPaths) { + auto maybeExpectedJson = readJson(entry); + if (maybeExpectedJson.is<mbgl::JSDocument>()) { + auto& expected = maybeExpectedJson.get<mbgl::JSDocument>(); + + mbgl::JSDocument actual; + actual.Parse<0>(metadata.actualJson); + if (actual.HasParseError()) { + metadata.errorMessage = "Error parsing actual JSON for: " + metadata.paths.stylePath.string(); + return false; + } + + auto actualVal = mapbox::geojson::convert<mapbox::geojson::value>(actual); + auto expectedVal = mapbox::geojson::convert<mapbox::geojson::value>(expected); + bool equal = deepEqual(actualVal, expectedVal); + + metadata.difference = !equal; + if (equal) { + metadata.diff = "Match"; + } else { + metadata.diff = simpleDiff(actualVal, expectedVal); + } + } else { + metadata.errorMessage = "Failed to load expected JSON " + entry; + return false; + } + } + + return true; +} + +bool TestRunner::checkRenderTestResults(mbgl::PremultipliedImage&& actualImage, TestMetadata& metadata) { const std::string& base = metadata.paths.defaultExpectations(); const std::vector<mbgl::filesystem::path>& expectations = metadata.paths.expectations; @@ -308,13 +538,13 @@ bool TestRunner::runOperations(const std::string& key, TestMetadata& metadata) { assert(operationArray[1].IsNumber()); map.jumpTo(mbgl::CameraOptions().withBearing(operationArray[1].GetDouble())); - // setPitch + // setPitch } else if (operationArray[0].GetString() == setPitchOp) { assert(operationArray.Size() >= 2u); assert(operationArray[1].IsNumber()); map.jumpTo(mbgl::CameraOptions().withPitch(operationArray[1].GetDouble())); - // setFilter + // setFilter } else if (operationArray[0].GetString() == setFilterOp) { assert(operationArray.Size() >= 3u); assert(operationArray[1].IsString()); @@ -665,7 +895,19 @@ bool TestRunner::run(TestMetadata& metadata) { return false; } - return checkResults(std::move(image), metadata); + if (metadata.renderTest) { + return checkRenderTestResults(std::move(image), metadata); + } else { + std::vector<mbgl::Feature> features; + assert(metadata.document["metadata"]["test"]["queryGeometry"].IsArray()); + if (metadata.document["metadata"]["test"]["queryGeometry"][0].IsNumber() && + metadata.document["metadata"]["test"]["queryGeometry"][1].IsNumber()) { + features = frontend.getRenderer()->queryRenderedFeatures(metadata.queryGeometry, metadata.queryOptions); + } else { + features = frontend.getRenderer()->queryRenderedFeatures(metadata.queryGeometryBox, metadata.queryOptions); + } + return checkQueryTestResults(std::move(image), std::move(features), metadata); + } } void TestRunner::reset() { diff --git a/render-test/runner.hpp b/render-test/runner.hpp index fdea65e104..d8e5275f61 100644 --- a/render-test/runner.hpp +++ b/render-test/runner.hpp @@ -21,7 +21,10 @@ public: private: bool runOperations(const std::string& key, TestMetadata&); - bool checkResults(mbgl::PremultipliedImage&& image, TestMetadata&); + bool checkQueryTestResults(mbgl::PremultipliedImage&& actualImage, + std::vector<mbgl::Feature>&& features, + TestMetadata&); + bool checkRenderTestResults(mbgl::PremultipliedImage&& image, TestMetadata&); struct Impl { Impl(const TestMetadata&); |