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

#include <mbgl/text/font_stack.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/platform/log.hpp>

using namespace mbgl;

class GlyphStoreTest {
public:
    GlyphStoreTest(MockFileSource::Type type, const std::string& resource)
        : fileSource(type, resource) {}

    util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
    util::RunLoop loop;
    MockFileSource fileSource;
    StubStyleObserver observer;
    GlyphStore glyphStore;

    void run(const std::string& url, const std::string& fontStack, const std::set<GlyphRange>& glyphRanges) {
        // Squelch logging.
        Log::setObserver(std::make_unique<Log::NullObserver>());

        util::ThreadContext::Set(&context);
        util::ThreadContext::setFileSource(&fileSource);

        glyphStore.setObserver(&observer);
        glyphStore.setURL(url);
        glyphStore.hasGlyphRanges(fontStack, glyphRanges);

        loop.run();
    }

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

TEST(GlyphStore, LoadingSuccess) {
    GlyphStoreTest test(MockFileSource::Success, "");

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

    test.observer.glyphsLoaded = [&] (const std::string&, const GlyphRange&) {
        if (!test.glyphStore.hasGlyphRanges("Test Stack", {{0, 255}, {256, 511}}))
            return;

        auto fontStack = test.glyphStore.getFontStack("Test Stack");
        ASSERT_FALSE(fontStack->getSDFs().empty());

        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}, {256, 511}});
}

TEST(GlyphStore, LoadingFail) {
    GlyphStoreTest test(MockFileSource::RequestFail, "glyphs.pbf");

    test.observer.glyphsError = [&] (const std::string& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) {
        ASSERT_TRUE(error != nullptr);
        ASSERT_EQ(fontStack, "Test Stack");
        ASSERT_EQ(glyphRange, GlyphRange(0, 255));

        auto stack = test.glyphStore.getFontStack("Test Stack");
        ASSERT_TRUE(stack->getSDFs().empty());
        ASSERT_FALSE(test.glyphStore.hasGlyphRanges("Test Stack", {{0, 255}}));

        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}});
}

TEST(GlyphStore, LoadingCorrupted) {
    GlyphStoreTest test(MockFileSource::RequestWithCorruptedData, "glyphs.pbf");

    test.observer.glyphsError = [&] (const std::string& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) {
        ASSERT_TRUE(error != nullptr);
        ASSERT_EQ(fontStack, "Test Stack");
        ASSERT_EQ(glyphRange, GlyphRange(0, 255));

        auto stack = test.glyphStore.getFontStack("Test Stack");
        ASSERT_TRUE(stack->getSDFs().empty());
        ASSERT_FALSE(test.glyphStore.hasGlyphRanges("Test Stack", {{0, 255}}));

        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}});
}

TEST(GlyphStore, LoadingCancel) {
    GlyphStoreTest test(MockFileSource::Success, "glyphs.pbf");

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

    test.fileSource.requestEnqueuedCallback = [&]{
        test.end();
    };

    test.run(
        "test/fixtures/resources/glyphs.pbf",
        "Test Stack",
        {{0, 255}});
}

TEST(GlyphStore, InvalidURL) {
    GlyphStoreTest test(MockFileSource::Success, "");

    test.observer.glyphsError = [&] (const std::string&, const GlyphRange&, std::exception_ptr error) {
        ASSERT_TRUE(error != nullptr);

        auto stack = test.glyphStore.getFontStack("Test Stack");
        ASSERT_TRUE(stack->getSDFs().empty());

        test.end();
    };

    test.run(
        "foo bar",
        "Test Stack",
        {{0, 255}});
}