Add MockFileSource and MockView for testing the ResourceLoader

- MockFileSource
  Will load pre-defined resources from the filesystem. It supports
  passing a string, that when it matches an URL, will simulate a failure.

- MockView
  It is just a no-op to not get in the way when testing
  resource loading.

Few more additions:

- style.json
  Specially crafted style that will look always for the same tile,
  glyph range, etc at the "test/fixtures/resources/" path. No matter
  where you look (or at what zoom), you always get the same stuff.

- vector.pbf
  This taken from a part of Taipei that will request several glyph
  ranges.
This commit is contained in:
Thiago Marcos P. Santos
2015-05-18 13:32:56 +03:00
parent ee9b172152
commit 6ef6591a52
10 changed files with 138 additions and 0 deletions

BIN
test/fixtures/resources/glyphs.pbf vendored Normal file

Binary file not shown.

1
test/fixtures/resources/source.json vendored Normal file

File diff suppressed because one or more lines are too long

1
test/fixtures/resources/sprite.json vendored Normal file

File diff suppressed because one or more lines are too long

BIN
test/fixtures/resources/sprite.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

31
test/fixtures/resources/style.json vendored Normal file
View File

@@ -0,0 +1,31 @@
{
"version": 1,
"name": "Test",
"sources": {
"compositedsource": {
"url": "test/fixtures/resources/source.json",
"type": "vector"
}
},
"sprite": "test/fixtures/resources/sprite",
"glyphs": "test/fixtures/resources/glyphs.pbf",
"layers": [{
"id": "road",
"type": "symbol",
"source": "compositedsource",
"source-layer": "road_label",
"layout": {
"text-font": "Open Sans Regular, Arial Unicode MS Regular",
"text-field": "{name_en}"
}
}, {
"id": "poi",
"type": "symbol",
"source": "compositedsource",
"source-layer": "poi_label",
"layout": {
"text-font": "Open Sans Regular, Arial Unicode MS Regular",
"icon-image": "{maki}_icon"
}
}]
}

BIN
test/fixtures/resources/vector.pbf vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,47 @@
#include "mock_file_source.hpp"
#include <mbgl/storage/request.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/thread.hpp>
namespace mbgl {
class MockFileSource::Impl {
public:
Impl(uv_loop_t*, const std::string& matchFail) : matchFail_(matchFail) {}
void handleRequest(Request* req) const;
private:
std::string matchFail_;
};
void MockFileSource::Impl::handleRequest(Request* req) const {
const Resource& resource = req->resource;
std::shared_ptr<Response> response = std::make_shared<Response>();
if (matchFail_.empty() || resource.url.find(matchFail_) == std::string::npos) {
response->status = Response::Status::Successful;
response->data = util::read_file(resource.url.c_str());
} else {
response->message = "Failed by the test case";
}
req->notify(response);
}
MockFileSource::MockFileSource(const std::string& matchFail)
: thread_(util::make_unique<util::Thread<Impl>>("FileSource", util::ThreadPriority::Low, matchFail)) {
}
Request* MockFileSource::request(const Resource& resource, uv_loop_t* loop, Callback callback) {
Request* req = new Request(resource, loop, std::move(callback));
thread_->invoke(&Impl::handleRequest, req);
return req;
}
void MockFileSource::cancel(Request*) {
}
}

View File

@@ -0,0 +1,33 @@
#ifndef TEST_RESOURCES_MOCK_FILE_SOURCE
#define TEST_RESOURCES_MOCK_FILE_SOURCE
#include <mbgl/storage/file_source.hpp>
#include <string>
namespace mbgl {
namespace util {
template <typename T> class Thread;
}
// This mock FileSource will read data from the disk and will fail
// the request if the URL matches the 'matchFail' string.
class MockFileSource : public FileSource {
public:
class Impl;
MockFileSource(const std::string& matchFail);
~MockFileSource() override = default;
// FileSource implementation.
Request* request(const Resource&, uv_loop_t*, Callback) override;
void cancel(Request*) override;
private:
const std::unique_ptr<util::Thread<Impl>> thread_;
};
}
#endif

View File

@@ -0,0 +1,21 @@
#ifndef TEST_RESOURCES_MOCK_VIEW
#define TEST_RESOURCES_MOCK_VIEW
#include <mbgl/map/view.hpp>
namespace mbgl {
class MockView : public View {
public:
MockView() = default;
// View implementation.
void activate() override {};
void deactivate() override {};
void notify() override {};
void invalidate(std::function<void()>) override {};
};
}
#endif

View File

@@ -59,6 +59,10 @@
'miscellaneous/variant.cpp',
'miscellaneous/worker.cpp',
'resources/mock_file_source.cpp',
'resources/mock_file_source.hpp',
'resources/mock_view.hpp',
'storage/storage.hpp',
'storage/storage.cpp',
'storage/cache_response.cpp',