summaryrefslogtreecommitdiff
path: root/test/map/map.cpp
blob: 6ecbc52e5fc99d30d8adbbac5c33a73ef4074bae (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
#include <mbgl/test/util.hpp>
#include <mbgl/test/stub_file_source.hpp>
#include <mbgl/test/fake_file_source.hpp>
#include <mbgl/test/fixture_log_observer.hpp>

#include <mbgl/map/map.hpp>
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/util/color.hpp>

using namespace mbgl;
using namespace mbgl::style;
using namespace std::literals::string_literals;

struct MapTest {
    util::RunLoop runLoop;
    std::shared_ptr<HeadlessDisplay> display { std::make_shared<mbgl::HeadlessDisplay>() };
    HeadlessView view { display, 1 };
    StubFileSource fileSource;
};

TEST(Map, Offline) {
    MapTest test;
    DefaultFileSource fileSource(":memory:", ".");

    auto expiredItem = [] (const std::string& path) {
        Response response;
        response.data = std::make_shared<std::string>(util::read_file("test/fixtures/map/offline/"s + path));
        response.expires = Timestamp{ Seconds(0) };
        return response;
    };

    const std::string prefix = "http://127.0.0.1:3000/";
    fileSource.put(Resource::style(prefix + "style.json"), expiredItem("style.json"));
    fileSource.put(Resource::source(prefix + "streets.json"), expiredItem("streets.json"));
    fileSource.put(Resource::spriteJSON(prefix + "sprite", 1.0), expiredItem("sprite.json"));
    fileSource.put(Resource::spriteImage(prefix + "sprite", 1.0), expiredItem("sprite.png"));
    fileSource.put(Resource::tile(prefix + "{z}-{x}-{y}.vector.pbf", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), expiredItem("0-0-0.vector.pbf"));
    fileSource.put(Resource::glyphs(prefix + "{fontstack}/{range}.pbf", {{"Helvetica"}}, {0, 255}), expiredItem("glyph.pbf"));
    NetworkStatus::Set(NetworkStatus::Status::Offline);

    Map map(test.view, fileSource, MapMode::Still);
    map.setStyleURL(prefix + "style.json");

    test::checkImage("test/fixtures/map/offline",
                     test::render(map),
                     0.0015,
                     0.1);

    NetworkStatus::Set(NetworkStatus::Status::Online);
}

TEST(Map, SetStyleInvalidJSON) {
    MapTest test;

    Log::setObserver(std::make_unique<FixtureLogObserver>());

    bool fail = false;
    test.view.setMapChangeCallback([&](MapChange change) {
        if (change == mbgl::MapChangeDidFailLoadingMap) {
            fail = true;
        }
    });

    {
        Map map(test.view, test.fileSource, MapMode::Still);
        map.setStyleJSON("invalid");
    }

    EXPECT_TRUE(fail);

    auto observer = Log::removeObserver();
    auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
    EXPECT_EQ(1u, flo->count({ EventSeverity::Error, Event::ParseStyle, -1,
        "Failed to parse style: 0 - Invalid value." }));
    auto unchecked = flo->unchecked();
    EXPECT_TRUE(unchecked.empty()) << unchecked;
}

TEST(Map, SetStyleInvalidURL) {
    MapTest test;

    test.fileSource.styleResponse = [] (const Resource&) {
        Response response;
        response.error = std::make_unique<Response::Error>(
            Response::Error::Reason::Other,
            "Failed by the test case");
        return response;
    };

    test.view.setMapChangeCallback([&](MapChange change) {
        if (change == mbgl::MapChangeDidFailLoadingMap) {
            test.runLoop.stop();
        }
    });

    Map map(test.view, test.fileSource, MapMode::Still);
    map.setStyleURL("mapbox://bar");

    test.runLoop.run();
}

TEST(Map, DoubleStyleLoad) {
    MapTest test;

    Map map(test.view, test.fileSource, MapMode::Still);
    map.setStyleJSON("");
    map.setStyleJSON("");
}

TEST(Map, StyleFresh) {
    // The map should not revalidate fresh styles.

    MapTest test;
    FakeFileSource fileSource;

    Map map(test.view, fileSource, MapMode::Still);
    map.setStyleURL("mapbox://styles/test");
    EXPECT_EQ(1u, fileSource.requests.size());

    Response response;
    response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json"));
    response.expires = Timestamp::max();

    fileSource.respond(Resource::Style, response);
    EXPECT_EQ(0u, fileSource.requests.size());
}

TEST(Map, StyleExpired) {
    // The map should allow expired styles to be revalidated, so long as no mutations are made.

    using namespace std::chrono_literals;

    MapTest test;
    FakeFileSource fileSource;

    Map map(test.view, fileSource, MapMode::Still);
    map.setStyleURL("mapbox://styles/test");
    EXPECT_EQ(1u, fileSource.requests.size());

    Response response;
    response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json"));
    response.expires = util::now() - 1h;

    fileSource.respond(Resource::Style, response);
    EXPECT_EQ(1u, fileSource.requests.size());

    map.addLayer(std::make_unique<style::BackgroundLayer>("bg"));
    EXPECT_EQ(1u, fileSource.requests.size());

    fileSource.respond(Resource::Style, response);
    EXPECT_EQ(0u, fileSource.requests.size());
    EXPECT_NE(nullptr, map.getLayer("bg"));
}

TEST(Map, StyleExpiredWithAnnotations) {
    // Adding an annotation should not prevent revalidation of an expired style.

    using namespace std::chrono_literals;

    MapTest test;
    FakeFileSource fileSource;

    Map map(test.view, fileSource, MapMode::Still);
    map.setStyleURL("mapbox://styles/test");
    EXPECT_EQ(1u, fileSource.requests.size());

    Response response;
    response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json"));
    response.expires = util::now() - 1h;

    fileSource.respond(Resource::Style, response);
    EXPECT_EQ(1u, fileSource.requests.size());

    map.addAnnotation(LineAnnotation { LineString<double> {{ { 0, 0 }, { 10, 10 } }} });
    EXPECT_EQ(1u, fileSource.requests.size());

    fileSource.respond(Resource::Style, response);
    EXPECT_EQ(1u, fileSource.requests.size());
}

TEST(Map, StyleEarlyMutation) {
    // An early mutation should not prevent the initial style load.

    MapTest test;
    FakeFileSource fileSource;

    Map map(test.view, fileSource, MapMode::Still);
    map.setStyleURL("mapbox://styles/test");
    map.addLayer(std::make_unique<style::BackgroundLayer>("bg"));

    Response response;
    response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/water.json"));
    fileSource.respond(Resource::Style, response);

    EXPECT_EQ(0u, fileSource.requests.size());
    EXPECT_NE(nullptr, map.getLayer("water"));
}

TEST(Map, StyleLoadedSignal) {
    MapTest test;
    Map map(test.view, test.fileSource, MapMode::Still);
    
    // The map should emit a signal on style loaded
    bool emitted = false;
    test.view.setMapChangeCallback([&](MapChange change) {
        if (change == mbgl::MapChangeDidFinishLoadingStyle) {
            emitted = true;
        }
    });
    map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
    EXPECT_TRUE(emitted);
    
    // But not when the style couldn't be parsed
    emitted = false;
    map.setStyleJSON("invalid");
    EXPECT_FALSE(emitted);
}

TEST(Map, AddLayer) {
    MapTest test;

    Map map(test.view, test.fileSource, MapMode::Still);
    map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));

    auto layer = std::make_unique<BackgroundLayer>("background");
    layer->setBackgroundColor({{ 1, 0, 0, 1 }});
    map.addLayer(std::move(layer));

    test::checkImage("test/fixtures/map/add_layer", test::render(map));
}

TEST(Map, RemoveLayer) {
    MapTest test;

    Map map(test.view, test.fileSource, MapMode::Still);
    map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));

    auto layer = std::make_unique<BackgroundLayer>("background");
    layer->setBackgroundColor({{ 1, 0, 0, 1 }});
    map.addLayer(std::move(layer));
    map.removeLayer("background");

    test::checkImage("test/fixtures/map/remove_layer", test::render(map));
}

TEST(Map, Classes) {
    MapTest test;

    Map map(test.view, test.fileSource, MapMode::Still);
    map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));

    EXPECT_FALSE(map.getTransitionOptions().duration);

    auto duration = mbgl::Duration(mbgl::Milliseconds(300));
    map.setTransitionOptions({ duration });
    EXPECT_EQ(map.getTransitionOptions().duration, duration);

    map.addClass("test");
    EXPECT_TRUE(map.hasClass("test"));

    map.removeClass("test");
    EXPECT_TRUE(map.getClasses().empty());

    std::vector<std::string> classes = { "foo", "bar" };
    map.setClasses(classes);
    EXPECT_FALSE(map.hasClass("test"));
    EXPECT_TRUE(map.hasClass("foo"));
    EXPECT_TRUE(map.hasClass("bar"));

    // Does nothing - same style JSON.
    map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
    EXPECT_TRUE(map.hasClass("foo"));
    EXPECT_EQ(map.getTransitionOptions().duration, duration);

    map.setStyleJSON(util::read_file("test/fixtures/api/water.json"));
    EXPECT_TRUE(map.getClasses().empty());
    EXPECT_FALSE(map.getTransitionOptions().duration);
}