summaryrefslogtreecommitdiff
path: root/test/sprite/sprite_loader.test.cpp
blob: 3691572265f6ce56e03ce312dad064e849d6c556 (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
#include <mbgl/test/util.hpp>
#include <mbgl/test/fixture_log_observer.hpp>
#include <mbgl/test/stub_file_source.hpp>

#include <mbgl/sprite/sprite_loader.hpp>
#include <mbgl/sprite/sprite_loader_observer.hpp>
#include <mbgl/sprite/sprite_parser.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/util/string.hpp>

#include <utility>

using namespace mbgl;
using namespace mbgl::style;

class StubSpriteLoaderObserver : public SpriteLoaderObserver {
public:
    void onSpriteLoaded(std::vector<std::unique_ptr<style::Image>>&& images) override {
        if (spriteLoaded) spriteLoaded(std::move(images));
    }

    void onSpriteError(std::exception_ptr error) override {
        if (spriteError) spriteError(error);
    }

    std::function<void (std::vector<std::unique_ptr<style::Image>>&&)> spriteLoaded;
    std::function<void (std::exception_ptr)> spriteError;
};

class SpriteLoaderTest {
public:
    SpriteLoaderTest() = default;

    util::RunLoop loop;
    StubFileSource fileSource;
    StubSpriteLoaderObserver observer;
    ThreadPool threadPool { 1 };
    SpriteLoader spriteLoader{ 1 };

    void run() {
        // Squelch logging.
        Log::setObserver(std::make_unique<Log::NullObserver>());

        spriteLoader.setObserver(&observer);
        spriteLoader.load("test/fixtures/resources/sprite", threadPool, fileSource);

        loop.run();
    }

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

Response successfulSpriteImageResponse(const Resource& resource) {
    EXPECT_EQ("test/fixtures/resources/sprite.png", resource.url);
    Response response;
    response.data = std::make_unique<std::string>(util::read_file(resource.url));
    return response;
}

Response successfulSpriteJSONResponse(const Resource& resource) {
    EXPECT_EQ("test/fixtures/resources/sprite.json", resource.url);
    Response response;
    response.data = std::make_unique<std::string>(util::read_file(resource.url));
    return response;
}

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

Response corruptSpriteResponse(const Resource&) {
    Response response;
    response.data = std::make_unique<std::string>("CORRUPT");
    return response;
}

TEST(SpriteLoader, LoadingSuccess) {
    SpriteLoaderTest test;

    test.fileSource.spriteImageResponse = successfulSpriteImageResponse;
    test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse;

    test.observer.spriteError = [&] (std::exception_ptr error) {
        FAIL() << util::toString(error);
        test.end();
    };

    test.observer.spriteLoaded = [&] (std::vector<std::unique_ptr<style::Image>>&& images) {
        EXPECT_EQ(images.size(), 367u);
        test.end();
    };

    test.run();
}

TEST(SpriteLoader, JSONLoadingFail) {
    SpriteLoaderTest test;

    test.fileSource.spriteImageResponse = successfulSpriteImageResponse;
    test.fileSource.spriteJSONResponse = failedSpriteResponse;

    test.observer.spriteError = [&] (std::exception_ptr error) {
        EXPECT_TRUE(error != nullptr);
        EXPECT_EQ("Failed by the test case", util::toString(error));
        test.end();
    };

    test.run();
}

TEST(SpriteLoader, ImageLoadingFail) {
    SpriteLoaderTest test;

    test.fileSource.spriteImageResponse = failedSpriteResponse;
    test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse;

    test.observer.spriteError = [&] (std::exception_ptr error) {
        EXPECT_TRUE(error != nullptr);
        EXPECT_EQ("Failed by the test case", util::toString(error));
        test.end();
    };

    test.run();
}

TEST(SpriteLoader, JSONLoadingCorrupted) {
    SpriteLoaderTest test;

    test.fileSource.spriteImageResponse = successfulSpriteImageResponse;
    test.fileSource.spriteJSONResponse = corruptSpriteResponse;

    test.observer.spriteError = [&] (std::exception_ptr error) {
        EXPECT_TRUE(error != nullptr);
        EXPECT_EQ("Failed to parse JSON: Invalid value. at offset 0", util::toString(error));
        test.end();
    };

    test.run();
}

TEST(SpriteLoader, ImageLoadingCorrupted) {
    SpriteLoaderTest test;

    test.fileSource.spriteImageResponse = corruptSpriteResponse;
    test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse;

    test.observer.spriteError = [&] (std::exception_ptr error) {
        EXPECT_TRUE(error != nullptr);
        // Not asserting on platform-specific error text.
        test.end();
    };

    test.run();
}

TEST(SpriteLoader, LoadingCancel) {
    SpriteLoaderTest test;

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

    test.observer.spriteLoaded = [&] (const std::vector<std::unique_ptr<style::Image>>&) {
        FAIL() << "Should never be called";
    };

    test.run();
}