summaryrefslogtreecommitdiff
path: root/test/text/glyph_loader.test.cpp
blob: e06fbcb314d01cddaab0c2b55d1264df363bf172 (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
#include <mbgl/test/util.hpp>
#include <mbgl/test/stub_file_source.hpp>

#include <mbgl/text/glyph_manager.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/logging.hpp>

using namespace mbgl;

class StubGlyphManagerObserver : public GlyphManagerObserver {
public:
    void onGlyphsLoaded(const FontStack& fontStack, const GlyphRange& glyphRange) override {
        if (glyphsLoaded) glyphsLoaded(fontStack, glyphRange);
    }

    void onGlyphsError(const FontStack& fontStack,
                       const GlyphRange& glyphRange,
                       std::exception_ptr error,
                       const EventSeverity) override {
        if (glyphsError)
            glyphsError(fontStack, glyphRange, error);
    }

    std::function<void (const FontStack&, const GlyphRange&)> glyphsLoaded;
    std::function<void (const FontStack&, const GlyphRange&, std::exception_ptr)> glyphsError;
};

class StubGlyphRequestor : public GlyphRequestor {
public:
    void onGlyphsAvailable(GlyphMap glyphs) override {
        if (glyphsAvailable) glyphsAvailable(std::move(glyphs));
    }

    std::function<void (GlyphMap)> glyphsAvailable;
};

class GlyphManagerTest {
public:
    util::RunLoop loop;
    StubFileSource fileSource;
    StubGlyphManagerObserver observer;
    StubGlyphRequestor requestor;
    GlyphManager glyphManager { fileSource };

    void run(const std::string& url, GlyphDependencies dependencies) {
        // Squelch logging.
        Log::setObserver(std::make_unique<Log::NullObserver>());

        glyphManager.setURL(url);
        glyphManager.setObserver(&observer);
        glyphManager.getGlyphs(requestor, std::move(dependencies));

        loop.run();
    }

    void end() {
        loop.stop();
    }
};

TEST(GlyphManager, LoadingSuccess) {
    GlyphManagerTest test;

    test.fileSource.glyphsResponse = [&] (const Resource& resource) {
        EXPECT_EQ(ResourceKind::Glyphs, resource.kind);
        Response response;
        response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf"));
        return response;
    };

    test.observer.glyphsError = [&] (const FontStack&, const GlyphRange&, std::exception_ptr) {
        FAIL();
        test.end();
    };

    test.observer.glyphsLoaded = [&] (const FontStack& fontStack, const GlyphRange& range) {
        ASSERT_EQ(fontStack, FontStack {{"Test Stack"}});
        ASSERT_EQ(range, GlyphRange(0, 255));
    };

    test.requestor.glyphsAvailable = [&] (GlyphMap glyphs) {
        const auto& testPositions = glyphs.at({{"Test Stack"}});

        ASSERT_EQ(testPositions.size(), 3u);
        ASSERT_EQ(testPositions.count(u'a'), 1u);
        ASSERT_EQ(testPositions.count(u'å'), 1u);
        ASSERT_EQ(testPositions.count(u' '), 1u);
        ASSERT_TRUE(bool(testPositions.at(u' ')));

        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        GlyphDependencies {
            {{{"Test Stack"}}, {u'a', u'å', u' '}}
        });
}

TEST(GlyphManager, LoadingFail) {
    GlyphManagerTest test;

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

    test.observer.glyphsError = [&] (const FontStack& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) {
        EXPECT_EQ(fontStack, FontStack({"Test Stack"}));
        EXPECT_EQ(glyphRange, GlyphRange(0, 255));

        EXPECT_TRUE(error != nullptr);
        EXPECT_EQ(util::toString(error), "Failed by the test case");

        test.end();
    };

    test.requestor.glyphsAvailable = [&] (GlyphMap) {
        FAIL();
        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        GlyphDependencies {
            {{{"Test Stack"}}, {u'a', u'å'}}
        });
}

TEST(GlyphManager, LoadingCorrupted) {
    GlyphManagerTest test;

    test.fileSource.glyphsResponse = [&] (const Resource&) {
        Response response;
        response.data = std::make_unique<std::string>("CORRUPTED");
        return response;
    };

    test.observer.glyphsError = [&] (const FontStack& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) {
        EXPECT_EQ(fontStack, FontStack({"Test Stack"}));
        EXPECT_EQ(glyphRange, GlyphRange(0, 255));

        EXPECT_TRUE(error != nullptr);
        EXPECT_EQ(util::toString(error), "unknown pbf field type exception");

        test.end();
    };

    test.requestor.glyphsAvailable = [&] (GlyphMap) {
        FAIL();
        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        GlyphDependencies {
            {{{"Test Stack"}}, {u'a', u'å'}}
        });
}

TEST(GlyphManager, LoadingCancel) {
    GlyphManagerTest test;

    test.fileSource.glyphsResponse = [&] (const Resource&) {
        test.end();
        return optional<Response>();
    };

    test.observer.glyphsLoaded = [&] (const FontStack&, const GlyphRange&) {
        FAIL() << "Should never be called";
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        GlyphDependencies {
            {{{"Test Stack"}}, {u'a', u'å'}}
        });
}

TEST(GlyphManager, LoadingInvalid) {
    GlyphManagerTest test;

    test.fileSource.glyphsResponse = [&] (const Resource& resource) {
        EXPECT_EQ(ResourceKind::Glyphs, resource.kind);
        Response response;
        response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/fake_glyphs-0-255.pbf"));
        return response;
    };

    test.observer.glyphsError = [&] (const FontStack&, const GlyphRange&, std::exception_ptr) {
        FAIL();
        test.end();
    };

    test.observer.glyphsLoaded = [&] (const FontStack& fontStack, const GlyphRange& range) {
        ASSERT_EQ(fontStack, FontStack {{"Test Stack"}});
        ASSERT_EQ(range, GlyphRange(0, 255));
    };

    test.requestor.glyphsAvailable = [&] (GlyphMap glyphs) {
        const auto& testPositions = glyphs.at({{"Test Stack"}});

        ASSERT_EQ(testPositions.size(), 2u);
        ASSERT_FALSE(bool(testPositions.at(u'A')));
        ASSERT_TRUE(bool(testPositions.at(u'E')));

        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        GlyphDependencies {
            {{{"Test Stack"}}, {u'A', u'E'}}
        });
}