summaryrefslogtreecommitdiff
path: root/test/headless.cpp
blob: 29db9500d4b6f5569b964109dd4c1d21ab1ec7e2 (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
#include "gtest/gtest.h"

#include <mbgl/map/map.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/std.hpp>

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/default/caching_http_file_source.hpp>

#include "./fixtures/fixture_log.hpp"

#include <dirent.h>
#include <signal.h>
#include <libgen.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif

std::string base_directory;

namespace mbgl {
namespace platform {

std::string defaultCacheDatabase() {
    // Disables the cache.
    return "";
}
}
}

class ServerEnvironment : public ::testing::Environment {
public:
    virtual void SetUp() {
        pid = fork();
        if (pid < 0) {
            throw std::runtime_error("Cannot create web server");
        } else if (pid == 0) {
#ifdef __linux__
            prctl(PR_SET_PDEATHSIG, SIGHUP);
#endif
            const auto executable = base_directory + "bin/server.py";
            const char *port = "2900";
            char *arg[] = { const_cast<char *>(executable.c_str()), const_cast<char *>(port), nullptr };
            int ret = execv(executable.c_str(), arg);
            // This call should not return. In case execve failed, we exit anyway.
            if (ret < 0) {
                fprintf(stderr, "Failed to start server: %s\n", strerror(errno));
            }
            exit(0);
        } else {
            display = std::make_shared<mbgl::HeadlessDisplay>();
        }
    }
    virtual void TearDown() {
        ASSERT_TRUE(pid);
        kill(pid, SIGHUP);
    }

    std::shared_ptr<mbgl::HeadlessDisplay> display;

private:
    pid_t pid = 0;
};


ServerEnvironment* env = nullptr;


GTEST_API_ int main(int argc, char *argv[]) {
    // Note: glibc's dirname() **modifies** the argument and can't handle static strings.
    std::string file { __FILE__ }; file = dirname(const_cast<char *>(file.c_str()));
    if (file[0] == '/') {
        // If __FILE__ is an absolute path, we don't have to guess from the argv 0.
        base_directory = file + "/suite/";
    } else {
        std::string argv0 { argv[0] }; argv0 = dirname(const_cast<char *>(argv0.c_str()));
        base_directory = argv0 + "/" + file + "/suite/";
    }

    testing::InitGoogleTest(&argc, argv);
    env = new ServerEnvironment();
    ::testing::AddGlobalTestEnvironment(env);
    return RUN_ALL_TESTS();
}

void rewriteLocalScheme(rapidjson::Value &value, rapidjson::Document::AllocatorType &allocator) {
    ASSERT_TRUE(value.IsString());
    auto string = std::string { value.GetString(),value.GetStringLength() };
    if (string.compare(0, 8, "local://") == 0) {
        string.replace(0, 8, "http://127.0.0.1:2900/");
        value.SetString(string.data(), string.size(), allocator);
    }
}

class HeadlessTest : public ::testing::TestWithParam<std::string> {};

TEST_P(HeadlessTest, render) {
    using namespace mbgl;

    const std::string& base = GetParam();

    std::string style = util::read_file(base_directory + "tests/" + base + "/style.json");
    std::string info = util::read_file(base_directory + "tests/" + base + "/info.json");

    // Parse style.
    rapidjson::Document styleDoc;
    styleDoc.Parse<0>((const char *const)style.c_str());
    ASSERT_FALSE(styleDoc.HasParseError());
    ASSERT_TRUE(styleDoc.IsObject());

    // Rewrite "local://" to "http://127.0.0.1:2900/".
    if (styleDoc.HasMember("sprite")) {
        rewriteLocalScheme(styleDoc["sprite"], styleDoc.GetAllocator());
    }

    if (styleDoc.HasMember("glyphs")) {
        rewriteLocalScheme(styleDoc["glyphs"], styleDoc.GetAllocator());
    }

    if (styleDoc.HasMember("sources")) {
        auto &sources = styleDoc["sources"];
        ASSERT_TRUE(sources.IsObject());
        for (auto source = sources.MemberBegin(), end = sources.MemberEnd(); source != end; source++) {
            if (source->value.HasMember("tiles")) {
                auto &tiles = source->value["tiles"];
                ASSERT_TRUE(tiles.IsArray());
                for (rapidjson::SizeType i = 0; i < tiles.Size(); i++) {
                    rewriteLocalScheme(tiles[i], styleDoc.GetAllocator());
                }
            }

            if (source->value.HasMember("url")) {
                rewriteLocalScheme(source->value["url"], styleDoc.GetAllocator());
            }
        }
    }

    // Convert the JSON object back into a stringified version.
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    styleDoc.Accept(writer);
    style = buffer.GetString();

    // Parse settings.
    rapidjson::Document infoDoc;
    infoDoc.Parse<0>((const char *const)info.c_str());
    ASSERT_FALSE(infoDoc.HasParseError());
    ASSERT_TRUE(infoDoc.IsObject());

    Log::Set<FixtureLogBackend>();

    for (auto it = infoDoc.MemberBegin(), end = infoDoc.MemberEnd(); it != end; it++) {
        const std::string name {
            it->name.GetString(), it->name.GetStringLength()
        };
        const rapidjson::Value& value = it->value;
        ASSERT_TRUE(value.IsObject());
        if (value.HasMember("center")) ASSERT_TRUE(value["center"].IsArray());

        const std::string actual_image = base_directory + "tests/" + base + "/" + name +  "/actual.png";

        const double zoom = value.HasMember("zoom") ? value["zoom"].GetDouble() : 0;
        const double bearing = value.HasMember("bearing") ? value["bearing"].GetDouble() : 0;
        const double latitude = value.HasMember("center") ? value["center"][rapidjson::SizeType(0)].GetDouble() : 0;
        const double longitude = value.HasMember("center") ? value["center"][rapidjson::SizeType(1)].GetDouble() : 0;
        const unsigned int width = value.HasMember("width") ? value["width"].GetUint() : 512;
        const unsigned int height = value.HasMember("height") ? value["height"].GetUint() : 512;
        const unsigned int pixelRatio = value.HasMember("pixelRatio") ? value["pixelRatio"].GetUint() : 1;

        std::vector<std::string> classes;
        if (value.HasMember("classes")) {
            const rapidjson::Value& js_classes = value["classes"];
            ASSERT_TRUE(js_classes.IsArray());
            for (rapidjson::SizeType i = 0; i < js_classes.Size(); i++) {
                const rapidjson::Value& js_class = js_classes[i];
                ASSERT_TRUE(js_class.IsString());
                classes.push_back({ js_class.GetString(), js_class.GetStringLength() });
            }
        }

        HeadlessView view(env->display);
        CachingHTTPFileSource fileSource(platform::defaultCacheDatabase());
        Map map(view, fileSource);

        map.setStyleJSON(style, base_directory);
        map.setAppliedClasses(classes);

        view.resize(width, height, pixelRatio);
        map.resize(width, height, pixelRatio);
        map.setLonLatZoom(longitude, latitude, zoom);
        map.setBearing(bearing);


        // Run the loop. It will terminate when we don't have any further listeners.
        map.run();

        const unsigned int w = width * pixelRatio;
        const unsigned int h = height * pixelRatio;

        auto pixels = view.readPixels();

        const std::string image = util::compress_png(w, h, pixels.get());
        util::write_file(actual_image, image);
    }
}

INSTANTIATE_TEST_CASE_P(Headless, HeadlessTest, ::testing::ValuesIn([] {
    std::vector<std::string> names;

    DIR *dir = opendir((base_directory + "tests").c_str());
    if (dir == nullptr) {
        return names;
    }

    for (dirent *dp = nullptr; (dp = readdir(dir)) != nullptr;) {
        const std::string name = dp->d_name;
        if (name != "." && name != ".." && name != "index.html") {
            names.push_back(name);
        }
    }

    closedir(dir);

    return names;
}()));