|
11 | 11 | #include <mbgl/util/run_loop.hpp>
|
12 | 12 | #include <mbgl/util/texture_pool.hpp>
|
13 | 13 | #include <mbgl/util/thread.hpp>
|
14 |
| - |
15 |
| -#include <regex> |
| 14 | +#include <mbgl/util/string.hpp> |
16 | 15 |
|
17 | 16 | using namespace mbgl;
|
18 | 17 |
|
19 |
| -namespace { |
20 |
| - |
21 |
| -class MockMapContext : public Style::Observer { |
| 18 | +class StyleObserver : public Style::Observer { |
22 | 19 | public:
|
23 |
| - MockMapContext(View& view, |
24 |
| - FileSource& fileSource, |
25 |
| - const std::function<void(std::exception_ptr error)>& callback) |
26 |
| - : data_(MapMode::Still, GLContextMode::Unique, view.getPixelRatio()), |
27 |
| - transform_(view, ConstrainMode::HeightOnly), |
28 |
| - callback_(callback) { |
29 |
| - util::ThreadContext::setFileSource(&fileSource); |
30 |
| - |
31 |
| - transform_.resize({{ 1000, 1000 }}); |
32 |
| - transform_.setLatLngZoom({0, 0}, 16); |
33 |
| - |
34 |
| - const std::string style = util::read_file("test/fixtures/resources/style.json"); |
35 |
| - style_ = std::make_unique<Style>(data_); |
36 |
| - style_->setJSON(style, ""); |
37 |
| - style_->setObserver(this); |
| 20 | + void onTileDataChanged() override { |
| 21 | + tileDataChanged(); |
38 | 22 | }
|
39 | 23 |
|
40 |
| - ~MockMapContext() { |
41 |
| - cleanup(); |
| 24 | + void onResourceLoadingFailed(std::exception_ptr error) override { |
| 25 | + resourceLoadingFailed(error); |
42 | 26 | }
|
43 | 27 |
|
44 |
| - void cleanup() { |
45 |
| - style_.reset(); |
46 |
| - } |
| 28 | + std::function<void ()> tileDataChanged = [] {}; |
| 29 | + std::function<void (std::exception_ptr)> resourceLoadingFailed = [] (auto) {}; |
| 30 | +}; |
47 | 31 |
|
48 |
| - void update() { |
49 |
| - const auto now = Clock::now(); |
| 32 | +std::string resourceErrorString(MockFileSource::Type type, const std::string& param) { |
| 33 | + Log::setObserver(std::make_unique<Log::NullObserver>()); // Squelch logging. |
50 | 34 |
|
51 |
| - data_.setAnimationTime(now); |
52 |
| - transform_.updateTransitions(now); |
| 35 | + util::RunLoop loop; |
53 | 36 |
|
54 |
| - style_->cascade(); |
55 |
| - style_->recalculate(16); |
56 |
| - style_->update(transform_.getState(), texturePool_); |
57 |
| - } |
| 37 | + util::ThreadContext context("Map", util::ThreadType::Map, util::ThreadPriority::Regular); |
| 38 | + util::ThreadContext::Set(&context); |
58 | 39 |
|
59 |
| - // Style::Observer implementation. |
60 |
| - void onTileDataChanged() override { |
61 |
| - update(); |
| 40 | + MockFileSource fileSource(type, param); |
| 41 | + util::ThreadContext::setFileSource(&fileSource); |
62 | 42 |
|
63 |
| - if (style_->isLoaded()) { |
64 |
| - callback_(nullptr); |
65 |
| - } |
66 |
| - }; |
| 43 | + MapData data(MapMode::Still, GLContextMode::Unique, 1.0); |
| 44 | + MockView view; |
| 45 | + Transform transform(view, ConstrainMode::HeightOnly); |
| 46 | + TexturePool texturePool; |
| 47 | + Style style(data); |
67 | 48 |
|
68 |
| - void onResourceLoadingFailed(std::exception_ptr error) override { |
69 |
| - callback_(error); |
70 |
| - } |
| 49 | + StyleObserver observer; |
| 50 | + std::string result; |
71 | 51 |
|
72 |
| -private: |
73 |
| - MapData data_; |
74 |
| - Transform transform_; |
75 |
| - TexturePool texturePool_; |
| 52 | + observer.tileDataChanged = [&] () { |
| 53 | + // Prompt tile loading after sources load. |
| 54 | + style.update(transform.getState(), texturePool); |
76 | 55 |
|
77 |
| - std::unique_ptr<Style> style_; |
| 56 | + if (style.isLoaded()) { |
| 57 | + loop.stop(); |
| 58 | + } |
| 59 | + }; |
78 | 60 |
|
79 |
| - std::function<void(std::exception_ptr error)> callback_; |
80 |
| -}; |
| 61 | + observer.resourceLoadingFailed = [&] (std::exception_ptr error) { |
| 62 | + result = util::toString(error); |
| 63 | + loop.stop(); |
| 64 | + }; |
81 | 65 |
|
82 |
| -void runTestCase(MockFileSource::Type type, |
83 |
| - const std::string& param, |
84 |
| - const std::string& message) { |
85 |
| - util::RunLoop loop; |
| 66 | + transform.resize({{ 512, 512 }}); |
| 67 | + transform.setLatLngZoom({0, 0}, 0); |
86 | 68 |
|
87 |
| - MockView view; |
88 |
| - MockFileSource fileSource(type, param); |
| 69 | + style.setObserver(&observer); |
| 70 | + style.setJSON(util::read_file("test/fixtures/resources/style.json"), ""); |
| 71 | + style.cascade(); |
| 72 | + style.recalculate(16); |
89 | 73 |
|
90 |
| - FixtureLogObserver* log = new FixtureLogObserver(); |
91 |
| - Log::setObserver(std::unique_ptr<Log::Observer>(log)); |
| 74 | + loop.run(); |
92 | 75 |
|
93 |
| - auto callback = [type, &loop, ¶m](std::exception_ptr error) { |
94 |
| - if (type == MockFileSource::Success) { |
95 |
| - EXPECT_TRUE(error == nullptr); |
96 |
| - } else { |
97 |
| - EXPECT_TRUE(error != nullptr); |
98 |
| - } |
| 76 | + return result; |
| 77 | +} |
99 | 78 |
|
100 |
| - try { |
101 |
| - if (error) { |
102 |
| - std::rethrow_exception(error); |
103 |
| - } |
104 |
| - } catch (const util::GlyphRangeLoadingException&) { |
105 |
| - EXPECT_EQ(param, "glyphs.pbf"); |
106 |
| - } catch (const util::SourceLoadingException&) { |
107 |
| - EXPECT_TRUE(param == "source_raster.json" || param == "source_vector.json"); |
108 |
| - } catch (const util::SpriteLoadingException&) { |
109 |
| - EXPECT_TRUE(param == "sprite.png" || param == "sprite.json"); |
110 |
| - } catch (const util::TileLoadingException&) { |
111 |
| - EXPECT_TRUE(param == "raster.png" || param == "vector.pbf"); |
112 |
| - } catch (const std::exception&) { |
113 |
| - EXPECT_TRUE(false) << "Unhandled exception."; |
114 |
| - } |
| 79 | +TEST(ResourceLoading, Success) { |
| 80 | + EXPECT_EQ(resourceErrorString(MockFileSource::Success, ""), ""); |
| 81 | +} |
115 | 82 |
|
116 |
| - loop.stop(); |
117 |
| - }; |
| 83 | +TEST(ResourceLoading, RasterSourceFail) { |
| 84 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "source_raster.json"), |
| 85 | + "Failed to load [test/fixtures/resources/source_raster.json]: Failed by the test case"); |
| 86 | +} |
118 | 87 |
|
119 |
| - std::unique_ptr<util::Thread<MockMapContext>> context( |
120 |
| - std::make_unique<util::Thread<MockMapContext>>( |
121 |
| - util::ThreadContext{"Map", util::ThreadType::Map, util::ThreadPriority::Regular}, view, fileSource, callback)); |
| 88 | +TEST(ResourceLoading, VectorSourceFail) { |
| 89 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "source_vector.json"), |
| 90 | + "Failed to load [test/fixtures/resources/source_vector.json]: Failed by the test case"); |
| 91 | +} |
122 | 92 |
|
123 |
| - loop.run(); |
| 93 | +TEST(ResourceLoading, SpriteJSONFail) { |
| 94 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "sprite.json"), |
| 95 | + "Failed to load [test/fixtures/resources/sprite.json]: Failed by the test case"); |
| 96 | +} |
124 | 97 |
|
125 |
| - // Needed because it will make the Map thread |
126 |
| - // join and cease logging after this point. |
127 |
| - context->invoke(&MockMapContext::cleanup); |
128 |
| - context.reset(); |
| 98 | +TEST(ResourceLoading, SpriteImageFail) { |
| 99 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "sprite.png"), |
| 100 | + "Failed to load [test/fixtures/resources/sprite.png]: Failed by the test case"); |
| 101 | +} |
129 | 102 |
|
130 |
| - uint32_t match = 0; |
131 |
| - std::vector<FixtureLogObserver::LogMessage> logMessages = log->unchecked(); |
| 103 | +TEST(ResourceLoading, RasterTileFail) { |
| 104 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "raster.png"), |
| 105 | + "Failed to load [test/fixtures/resources/raster.png]: Failed by the test case"); |
| 106 | +} |
132 | 107 |
|
133 |
| - for (auto& logMessage : logMessages) { |
134 |
| - if (std::regex_match(logMessage.msg, std::regex(message))) { |
135 |
| - match++; |
136 |
| - } |
137 |
| - } |
| 108 | +TEST(ResourceLoading, VectorTileFail) { |
| 109 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "vector.pbf"), |
| 110 | + "Failed to load [test/fixtures/resources/vector.pbf]: Failed by the test case"); |
| 111 | +} |
138 | 112 |
|
139 |
| - if (type == MockFileSource::Success) { |
140 |
| - EXPECT_EQ(match, 0u); |
141 |
| - } else { |
142 |
| - EXPECT_GT(match, 0u); |
143 |
| - } |
| 113 | +TEST(ResourceLoading, GlyphsFail) { |
| 114 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestFail, "glyphs.pbf"), |
| 115 | + "Failed to load [test/fixtures/resources/glyphs.pbf]: Failed by the test case"); |
144 | 116 | }
|
145 | 117 |
|
146 |
| -} // namespace |
| 118 | +TEST(ResourceLoading, RasterSourceCorrupt) { |
| 119 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "source_raster.json"), |
| 120 | + "Failed to parse [test/fixtures/resources/source_raster.json]: 0 - Invalid value."); |
| 121 | +} |
147 | 122 |
|
148 |
| -class ResourceLoading : public ::testing::TestWithParam<std::pair<std::string, std::string>> { |
149 |
| -}; |
| 123 | +TEST(ResourceLoading, VectorSourceCorrupt) { |
| 124 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "source_vector.json"), |
| 125 | + "Failed to parse [test/fixtures/resources/source_vector.json]: 0 - Invalid value."); |
| 126 | +} |
150 | 127 |
|
151 |
| -TEST_P(ResourceLoading, Success) { |
152 |
| - runTestCase(MockFileSource::Success, GetParam().first, std::string()); |
| 128 | +TEST(ResourceLoading, SpriteJSONCorrupt) { |
| 129 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "sprite.json"), |
| 130 | + "Failed to parse JSON: Invalid value. at offset 0"); |
153 | 131 | }
|
154 | 132 |
|
155 |
| -TEST_P(ResourceLoading, RequestFail) { |
156 |
| - std::stringstream message; |
157 |
| - message << "Failed to load \\[test\\/fixtures\\/resources\\/" << GetParam().first << "\\]\\: Failed by the test case"; |
| 133 | +TEST(ResourceLoading, SpriteImageCorrupt) { |
| 134 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "sprite.png"), |
| 135 | + "Could not parse sprite image"); |
| 136 | +} |
158 | 137 |
|
159 |
| - runTestCase(MockFileSource::RequestFail, GetParam().first, message.str()); |
| 138 | +TEST(ResourceLoading, RasterTileCorrupt) { |
| 139 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "raster.png"), |
| 140 | + "Failed to parse [0/0/0]: error parsing raster image"); |
160 | 141 | }
|
161 | 142 |
|
162 |
| -TEST_P(ResourceLoading, RequestWithCorruptedData) { |
163 |
| - runTestCase(MockFileSource::RequestWithCorruptedData, GetParam().first, GetParam().second); |
| 143 | +TEST(ResourceLoading, VectorTileCorrupt) { |
| 144 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "vector.pbf"), |
| 145 | + "Failed to parse [0/0/0]: pbf unknown field type exception"); |
164 | 146 | }
|
165 | 147 |
|
166 |
| -INSTANTIATE_TEST_CASE_P(Style, ResourceLoading, |
167 |
| - ::testing::Values( |
168 |
| - std::make_pair("source_raster.json", "Failed to parse \\[test\\/fixtures\\/resources\\/source_raster.json\\]: 0 - Invalid value."), |
169 |
| - std::make_pair("source_vector.json", "Failed to parse \\[test\\/fixtures\\/resources\\/source_vector.json\\]: 0 - Invalid value."), |
170 |
| - std::make_pair("sprite.json", "Failed to parse JSON: Invalid value. at offset 0"), |
171 |
| - std::make_pair("sprite.png", "Could not parse sprite image"), |
172 |
| - std::make_pair("raster.png", "Failed to parse \\[17\\/6553(4|5|6|7)\\/6553(4|5|6|7)\\]\\: error parsing raster image"), |
173 |
| - std::make_pair("vector.pbf", "Failed to parse \\[1(5|6)\\/1638(3|4)\\/1638(3|4)\\]\\: pbf unknown field type exception"), |
174 |
| - std::make_pair("glyphs.pbf", "Failed to parse \\[test\\/fixtures\\/resources\\/glyphs.pbf\\]: pbf unknown field type exception"))); |
| 148 | +TEST(ResourceLoading, GlyphsCorrupt) { |
| 149 | + EXPECT_EQ(resourceErrorString(MockFileSource::RequestWithCorruptedData, "glyphs.pbf"), |
| 150 | + "Failed to parse [test/fixtures/resources/glyphs.pbf]: pbf unknown field type exception"); |
| 151 | +} |
0 commit comments