summaryrefslogtreecommitdiff
path: root/test/style/glyph_store.cpp
blob: 8e7518c33fcef562a80caa082e454c4420186c3f (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
#include "../fixtures/fixture_log_observer.hpp"
#include "../fixtures/mock_file_source.hpp"
#include "../fixtures/util.hpp"

#include <mbgl/text/font_stack.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/thread.hpp>
#include <utility>

using namespace mbgl;

using GlyphStoreTestCallback = std::function<void(GlyphStore*, std::exception_ptr)>;

struct GlyphStoreParams {
    const std::string url;
    const std::string stack;
    const std::set<GlyphRange> ranges;
};

class GlyphStoreThread : public GlyphStore::Observer {
public:
    GlyphStoreThread(FileSource* fileSource, GlyphStoreTestCallback callback) : callback_(std::move(callback)) {
        util::ThreadContext::setFileSource(fileSource);
    }

    void loadGlyphStore(const GlyphStoreParams& params) {
        glyphStore_.reset(new GlyphStore());

        glyphStore_->setObserver(this);
        glyphStore_->setURL(params.url);

        ASSERT_FALSE(glyphStore_->hasGlyphRanges(params.stack, params.ranges));
    }

    void unloadGlyphStore() {
        glyphStore_->setObserver(nullptr);
        glyphStore_.reset();
    }

    void onGlyphsLoaded(const std::string&, const GlyphRange&) override {
        callback_(glyphStore_.get(), nullptr);
    }

    void onGlyphsError(const std::string&, const GlyphRange&, std::exception_ptr error) override {
        callback_(glyphStore_.get(), error);
    }

private:
    std::unique_ptr<GlyphStore> glyphStore_;
    GlyphStoreTestCallback callback_;
};

class GlyphStoreTest : public testing::Test {
protected:
    void runTest(const GlyphStoreParams& params, FileSource* fileSource, GlyphStoreTestCallback callback) {
        util::RunLoop loop;

        async_ = std::make_unique<util::AsyncTask>([&]{ loop.stop(); });
        async_->unref();

        const util::ThreadContext context = {"Map", util::ThreadType::Map, util::ThreadPriority::Regular};

        util::Thread<GlyphStoreThread> tester(context, fileSource, callback);
        tester.invoke(&GlyphStoreThread::loadGlyphStore, params);

        loop.run();

        tester.invoke(&GlyphStoreThread::unloadGlyphStore);
    }

    void stopTest() {
        testDone = true;
        async_->send();
    }

    bool isDone() const {
        return testDone;
    }

private:
    bool testDone = false;

    std::unique_ptr<util::AsyncTask> async_;
};

TEST_F(GlyphStoreTest, LoadingSuccess) {
    GlyphStoreParams params = {
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}, {256, 511}}
    };

    auto callback = [this, &params](GlyphStore* store, std::exception_ptr error) {
        ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map));

        // We need to check if the test is over because checking
        // if the GlyphStore has glyphs below will cause more requests
        // to happen and we don't want this endless loop.
        if (isDone()) {
            return;
        }

        ASSERT_EQ(error, nullptr);

        if (!store->hasGlyphRanges(params.stack, params.ranges)) {
            return;
        }

        ASSERT_FALSE(store->hasGlyphRanges("Foobar", params.ranges));
        ASSERT_FALSE(store->hasGlyphRanges("Foobar", {{512, 767}}));
        ASSERT_FALSE(store->hasGlyphRanges("Test Stack",  {{512, 767}}));

        auto fontStack = store->getFontStack(params.stack);
        ASSERT_FALSE(fontStack->getMetrics().empty());
        ASSERT_FALSE(fontStack->getSDFs().empty());

        stopTest();
    };

    MockFileSource fileSource(MockFileSource::Success, "");
    runTest(params, &fileSource, callback);
}

TEST_F(GlyphStoreTest, LoadingFail) {
    GlyphStoreParams params = {
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}, {256, 511}}
    };

    auto callback = [this, &params](GlyphStore* store, std::exception_ptr error) {
        ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map));

        if (isDone()) {
            return;
        }

        ASSERT_TRUE(error != nullptr);

        auto fontStack = store->getFontStack(params.stack);
        ASSERT_TRUE(fontStack->getMetrics().empty());
        ASSERT_TRUE(fontStack->getSDFs().empty());

        for (const auto& range : params.ranges) {
            ASSERT_FALSE(store->hasGlyphRanges(params.stack, {range}));
        }

        ASSERT_FALSE(store->hasGlyphRanges(params.stack, params.ranges));
        ASSERT_FALSE(store->hasGlyphRanges("Foobar", params.ranges));
        ASSERT_FALSE(store->hasGlyphRanges("Foobar", {{512, 767}}));

        stopTest();
    };

    MockFileSource fileSource(MockFileSource::RequestFail, "glyphs.pbf");
    runTest(params, &fileSource, callback);
}

TEST_F(GlyphStoreTest, LoadingCorrupted) {
    GlyphStoreParams params = {
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}, {256, 511}}
    };

    auto callback = [this, &params](GlyphStore* store, std::exception_ptr error) {
        ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map));

        if (isDone()) {
            return;
        }

        ASSERT_TRUE(error != nullptr);

        auto fontStack = store->getFontStack(params.stack);
        ASSERT_TRUE(fontStack->getMetrics().empty());
        ASSERT_TRUE(fontStack->getSDFs().empty());

        for (const auto& range : params.ranges) {
            ASSERT_FALSE(store->hasGlyphRanges(params.stack, {range}));
        }

        ASSERT_FALSE(store->hasGlyphRanges(params.stack, params.ranges));
        ASSERT_FALSE(store->hasGlyphRanges("Foobar", params.ranges));
        ASSERT_FALSE(store->hasGlyphRanges("Foobar", {{512, 767}}));

        stopTest();
    };

    MockFileSource fileSource(MockFileSource::RequestWithCorruptedData, "glyphs.pbf");
    runTest(params, &fileSource, callback);
}

TEST_F(GlyphStoreTest, LoadingCancel) {
    GlyphStoreParams params = {
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}, {256, 511}}
    };

    auto callback = [this](GlyphStore*, std::exception_ptr) {
        FAIL() << "Should never be called";
    };

    MockFileSource fileSource(MockFileSource::SuccessWithDelay, "glyphs.pbf");
    fileSource.setOnRequestDelayedCallback([this]{
        stopTest();
    });
    runTest(params, &fileSource, callback);
}

TEST_F(GlyphStoreTest, InvalidURL) {
    GlyphStoreParams params = {
        "foo bar",
        "Test Stack",
        {{0, 255}, {256, 511}}
    };

    auto callback = [this, &params](GlyphStore* store, std::exception_ptr error) {
        ASSERT_TRUE(error != nullptr);

        auto fontStack = store->getFontStack(params.stack);
        ASSERT_TRUE(fontStack->getMetrics().empty());
        ASSERT_TRUE(fontStack->getSDFs().empty());

        stopTest();
    };

    MockFileSource fileSource(MockFileSource::Success, "");
    runTest(params, &fileSource, callback);
}