diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-02-18 12:41:09 +0100 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-03-22 11:56:01 -0700 |
commit | db5ac4785fdc02b4e233201bb3c6f55270e3c65d (patch) | |
tree | 0c730d62e471d211924c486e1eeadf5efb305eaf /test/fixtures | |
parent | 61920071cd221d0d0627e01893185f0f19b55a98 (diff) | |
download | qtlocation-mapboxgl-db5ac4785fdc02b4e233201bb3c6f55270e3c65d.tar.gz |
[test] rearrange test files so they're not in the fixtures folder
Diffstat (limited to 'test/fixtures')
-rw-r--r-- | test/fixtures/fixture_log_observer.cpp | 105 | ||||
-rw-r--r-- | test/fixtures/fixture_log_observer.hpp | 71 | ||||
-rw-r--r-- | test/fixtures/main.cpp | 6 | ||||
-rw-r--r-- | test/fixtures/mock_view.hpp | 29 | ||||
-rw-r--r-- | test/fixtures/stub_file_source.cpp | 69 | ||||
-rw-r--r-- | test/fixtures/stub_file_source.hpp | 45 | ||||
-rw-r--r-- | test/fixtures/stub_style_observer.hpp | 67 | ||||
-rw-r--r-- | test/fixtures/util.cpp | 125 | ||||
-rw-r--r-- | test/fixtures/util.hpp | 44 |
9 files changed, 0 insertions, 561 deletions
diff --git a/test/fixtures/fixture_log_observer.cpp b/test/fixtures/fixture_log_observer.cpp deleted file mode 100644 index a6e89e2e79..0000000000 --- a/test/fixtures/fixture_log_observer.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "fixture_log_observer.hpp" -#include "../fixtures/util.hpp" - -namespace mbgl { - -FixtureLog::Message::Message(EventSeverity severity_, - Event event_, - int64_t code_, - const std::string& msg_) - : severity(severity_), event(event_), code(code_), msg(msg_) { -} - -bool FixtureLog::Message::operator==(const Message& rhs) const { - return severity == rhs.severity && event == rhs.event && code == rhs.code && msg == rhs.msg; -} - -FixtureLog::Message::Message() : severity(), event(), code(), msg() { -} - -FixtureLog::Observer::Observer(FixtureLog* log_) : log(log_) { -} - -FixtureLog::Observer::~Observer() { - if (log) { - log->observer = nullptr; - } - std::cerr << unchecked(); -} - -bool FixtureLog::Observer::onRecord(EventSeverity severity, - Event event, - int64_t code, - const std::string& msg) { - std::lock_guard<std::mutex> lock(messagesMutex); - - messages.emplace_back(severity, event, code, msg); - - return true; -} - -bool FixtureLog::Observer::empty() const { - std::lock_guard<std::mutex> lock(messagesMutex); - - return messages.empty(); -} - -size_t FixtureLog::Observer::count(const Message& message) const { - std::lock_guard<std::mutex> lock(messagesMutex); - - size_t message_count = 0; - for (const auto& msg : messages) { - if (msg == message) { - message_count++; - msg.checked = true; - } - } - return message_count; -} - -FixtureLog::FixtureLog() : observer(new FixtureLogObserver(this)) { - Log::setObserver(std::unique_ptr<Log::Observer>(observer)); -} - -bool FixtureLog::empty() const { - return observer ? observer->empty() : true; -} - -size_t FixtureLog::count(const FixtureLog::Message& message) const { - return observer ? observer->count(message) : 0; -} - -FixtureLog::~FixtureLog() { - if (observer) { - Log::removeObserver(); - } -} - -std::vector<FixtureLog::Message> FixtureLogObserver::unchecked() const { - std::lock_guard<std::mutex> lock(messagesMutex); - - std::vector<Message> unchecked_messages; - for (const auto& msg : messages) { - if (!msg.checked) { - unchecked_messages.push_back(msg); - msg.checked = true; - } - } - return unchecked_messages; -} - -::std::ostream& operator<<(::std::ostream& os, const std::vector<FixtureLog::Message>& messages) { - for (const auto& message : messages) { - os << "- " << message; - } - return os; -} - -::std::ostream& operator<<(::std::ostream& os, const FixtureLog::Message& message) { - os << "[\"" << message.severity << "\", \"" << message.event << "\""; - os << ", " << message.code; - os << ", \"" << message.msg << "\""; - return os << "]" << std::endl; -} - -} // namespace mbgl diff --git a/test/fixtures/fixture_log_observer.hpp b/test/fixtures/fixture_log_observer.hpp deleted file mode 100644 index f2ccb5cb58..0000000000 --- a/test/fixtures/fixture_log_observer.hpp +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef MBGL_TEST_FIXTURE_LOG_OBSERVER -#define MBGL_TEST_FIXTURE_LOG_OBSERVER - -#include <mbgl/platform/log.hpp> - -#include <vector> -#include <cstdarg> -#include <mutex> -#include <iostream> - -namespace mbgl { - -class FixtureLog { -public: - struct Message { - Message(EventSeverity severity_, Event event_, int64_t code_, const std::string &msg_); - Message(); - - bool operator==(const Message& rhs) const; - - const EventSeverity severity; - const Event event; - const int64_t code; - const std::string msg; - - mutable bool checked = false; - }; - - class Observer : public Log::Observer { - public: - using LogMessage = Message; - - Observer(FixtureLog* log = nullptr); - ~Observer(); - - // Log::Observer implementation - virtual bool onRecord(EventSeverity severity, - Event event, - int64_t code, - const std::string& msg) override; - - bool empty() const; - size_t count(const Message& message) const; - std::vector<Message> unchecked() const; - - private: - FixtureLog* log; - std::vector<Message> messages; - mutable std::mutex messagesMutex; - }; - - FixtureLog(); - - bool empty() const; - size_t count(const Message& message) const; - - ~FixtureLog(); - -private: - Observer* observer; -}; - -::std::ostream &operator<<(::std::ostream &os, - const std::vector<FixtureLog::Observer::LogMessage> &messages); -::std::ostream &operator<<(::std::ostream &os, const FixtureLog::Observer::LogMessage &message); - -using FixtureLogObserver = FixtureLog::Observer; - -} // namespace mbgl - -#endif diff --git a/test/fixtures/main.cpp b/test/fixtures/main.cpp deleted file mode 100644 index f7b2a6e92f..0000000000 --- a/test/fixtures/main.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "util.hpp" - -GTEST_API_ int main(int argc, char *argv[]) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/test/fixtures/mock_view.hpp b/test/fixtures/mock_view.hpp deleted file mode 100644 index e608545da5..0000000000 --- a/test/fixtures/mock_view.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef TEST_FIXTURES_MOCK_VIEW -#define TEST_FIXTURES_MOCK_VIEW - -#include <mbgl/map/view.hpp> - -#include <array> - -namespace mbgl { - -class MockView : public View { -public: - MockView() = default; - - // View implementation. - float getPixelRatio() const override { return 1; } - std::array<uint16_t, 2> getSize() const override { return {{ 0, 0 }}; } - std::array<uint16_t, 2> getFramebufferSize() const override { return {{ 0, 0 }}; } - - void activate() override {}; - void deactivate() override {}; - void notify() override {}; - void invalidate() override {} - void beforeRender() override {} - void afterRender() override {} -}; - -} - -#endif diff --git a/test/fixtures/stub_file_source.cpp b/test/fixtures/stub_file_source.cpp deleted file mode 100644 index b41eded084..0000000000 --- a/test/fixtures/stub_file_source.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "stub_file_source.hpp" - -namespace mbgl { - -using namespace std::chrono_literals; - -class StubFileRequest : public FileRequest { -public: - StubFileRequest(StubFileSource& fileSource_) - : fileSource(fileSource_) { - } - - ~StubFileRequest() { - fileSource.pending.erase(this); - } - - StubFileSource& fileSource; -}; - -StubFileSource::StubFileSource() { - timer.start(10ms, 10ms, [this] { - // Explicit copy to avoid iterator invalidation if ~StubFileRequest gets called within the loop. - auto pending_ = pending; - for (auto& pair : pending_) { - optional<Response> res = std::get<1>(pair.second)(std::get<0>(pair.second)); - if (res) { - std::get<2>(pair.second)(*res); - } - } - }); -} - -StubFileSource::~StubFileSource() = default; - -std::unique_ptr<FileRequest> StubFileSource::request(const Resource& resource, Callback callback) { - auto req = std::make_unique<StubFileRequest>(*this); - pending.emplace(req.get(), std::make_tuple(resource, response, callback)); - return std::move(req); -} - -optional<Response> StubFileSource::defaultResponse(const Resource& resource) { - switch (resource.kind) { - case Resource::Kind::Style: - if (!styleResponse) throw std::runtime_error("unexpected style request"); - return styleResponse(resource); - case Resource::Kind::Source: - if (!sourceResponse) throw std::runtime_error("unexpected source request"); - return sourceResponse(resource); - case Resource::Kind::Tile: - if (!tileResponse) throw std::runtime_error("unexpected tile request"); - return tileResponse(resource); - case Resource::Kind::Glyphs: - if (!glyphsResponse) throw std::runtime_error("unexpected glyphs request"); - return glyphsResponse(resource); - case Resource::Kind::SpriteJSON: - if (!spriteJSONResponse) throw std::runtime_error("unexpected sprite JSON request"); - return spriteJSONResponse(resource); - case Resource::Kind::SpriteImage: - if (!spriteImageResponse) throw std::runtime_error("unexpected sprite image request"); - return spriteImageResponse(resource); - case Resource::Kind::Unknown: - throw std::runtime_error("unknown resource type"); - } - - // The above switch is exhaustive, but placate GCC nonetheless: - return Response(); -} - -} // namespace mbgl diff --git a/test/fixtures/stub_file_source.hpp b/test/fixtures/stub_file_source.hpp deleted file mode 100644 index dbb584fdcc..0000000000 --- a/test/fixtures/stub_file_source.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef TEST_RESOURCES_STUB_FILE_SOURCE -#define TEST_RESOURCES_STUB_FILE_SOURCE - -#include <mbgl/storage/file_source.hpp> -#include <mbgl/util/timer.hpp> - -#include <unordered_map> - -namespace mbgl { - -class StubFileSource : public FileSource { -public: - StubFileSource(); - ~StubFileSource() override; - - std::unique_ptr<FileRequest> request(const Resource&, Callback) override; - - using ResponseFunction = std::function<optional<Response> (const Resource&)>; - - // You can set the response callback on a global level by assigning this callback: - ResponseFunction response = [this] (const Resource& resource) { - return defaultResponse(resource); - }; - - // Or set per-kind responses by setting these callbacks: - ResponseFunction styleResponse; - ResponseFunction sourceResponse; - ResponseFunction tileResponse; - ResponseFunction glyphsResponse; - ResponseFunction spriteJSONResponse; - ResponseFunction spriteImageResponse; - -private: - friend class StubFileRequest; - - // The default behavior is to throw if no per-kind callback has been set. - optional<Response> defaultResponse(const Resource&); - - std::unordered_map<FileRequest*, std::tuple<Resource, ResponseFunction, Callback>> pending; - util::Timer timer; -}; - -} - -#endif diff --git a/test/fixtures/stub_style_observer.hpp b/test/fixtures/stub_style_observer.hpp deleted file mode 100644 index 7236ca74f5..0000000000 --- a/test/fixtures/stub_style_observer.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef MBGL_TEST_STUB_STYLE_OBSERVER -#define MBGL_TEST_STUB_STYLE_OBSERVER - -#include <mbgl/style/style.hpp> - -namespace mbgl { - -/** - * An implementation of Style::Observer that forwards all methods to dynamically-settable lambas. - */ -class StubStyleObserver : public Style::Observer { -public: - void onGlyphsLoaded(const std::string& fontStack, const GlyphRange& glyphRange) override { - if (glyphsLoaded) glyphsLoaded(fontStack, glyphRange); - } - - void onGlyphsError(const std::string& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) override { - if (glyphsError) glyphsError(fontStack, glyphRange, error); - } - - void onSpriteLoaded() override { - if (spriteLoaded) spriteLoaded(); - } - - void onSpriteError(std::exception_ptr error) override { - if (spriteError) spriteError(error); - } - - void onSourceLoaded(Source& source) override { - if (sourceLoaded) sourceLoaded(source); - } - - void onSourceError(Source& source, std::exception_ptr error) override { - if (sourceError) sourceError(source, error); - } - - void onTileLoaded(Source& source, const TileID& tileID, bool isNewTile) override { - if (tileLoaded) tileLoaded(source, tileID, isNewTile); - } - - void onTileError(Source& source, const TileID& tileID, std::exception_ptr error) override { - if (tileError) tileError(source, tileID, error); - } - - void onResourceLoaded() override { - if (resourceLoaded) resourceLoaded(); - }; - - void onResourceError(std::exception_ptr error) override { - if (resourceError) resourceError(error); - }; - - std::function<void (const std::string& fontStack, const GlyphRange&)> glyphsLoaded; - std::function<void (const std::string& fontStack, const GlyphRange&, std::exception_ptr)> glyphsError; - std::function<void ()> spriteLoaded; - std::function<void (std::exception_ptr)> spriteError; - std::function<void (Source&)> sourceLoaded; - std::function<void (Source&, std::exception_ptr)> sourceError; - std::function<void (Source&, const TileID&, bool isNewTile)> tileLoaded; - std::function<void (Source&, const TileID&, std::exception_ptr)> tileError; - std::function<void ()> resourceLoaded; - std::function<void (std::exception_ptr)> resourceError; -}; - -} // namespace mbgl - -#endif diff --git a/test/fixtures/util.cpp b/test/fixtures/util.cpp deleted file mode 100644 index 0829fe72d8..0000000000 --- a/test/fixtures/util.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include "util.hpp" - -#include <mbgl/map/map.hpp> -#include <mbgl/platform/log.hpp> -#include <mbgl/util/image.hpp> -#include <mbgl/util/io.hpp> -#include <mbgl/util/chrono.hpp> - -#include <mapbox/pixelmatch.hpp> - -#include <csignal> -#include <future> - -#include <unistd.h> - -namespace mbgl { -namespace test { - -Server::Server(const char* executable) { - int input[2]; - int output[2]; - - if (pipe(input)) { - throw std::runtime_error("Cannot create server input pipe"); - } - if (pipe(output)) { - throw std::runtime_error("Cannot create server output pipe"); - } - - // Store the parent => child pipe so that we can close it in the destructor. - fd = input[1]; - - pid_t pid = fork(); - if (pid < 0) { - Log::Error(Event::Setup, "Cannot create server process"); - exit(1); - } else if (pid == 0) { - // This is the child process. - - // Connect the parent => child pipe to stdin. - while ((dup2(input[0], STDIN_FILENO) == -1) && (errno == EINTR)) {} - close(input[0]); - close(input[1]); - - // Move the child => parent side of the pipe to stdout. - while ((dup2(output[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {} - close(output[1]); - close(output[0]); - - // Launch the actual server process. - int ret = execl(executable, executable, nullptr); - - // This call should not return. In case execl failed, we exit anyway. - if (ret < 0) { - Log::Error(Event::Setup, "Failed to start server: %s", strerror(errno)); - } - exit(0); - } else { - // This is the parent process. - - // Close the unneeded sides of the pipes. - close(output[1]); - close(input[0]); - - // Wait until the server process sends at least 2 bytes or closes the handle. - char buffer[2]; - ssize_t bytes, total = 0; - while (total < 2 && (bytes = read(output[0], buffer + total, 2 - total)) != 0) { - total += bytes; - } - - // Close child => parent pipe. - close(output[0]); - - // Check signature - if (total != 2 || strncmp(buffer, "OK", 2) != 0) { - throw std::runtime_error("Failed to start server: Invalid signature"); - } - } -} - -Server::~Server() { - if (fd > 0) { - close(fd); - } -} - -PremultipliedImage render(Map& map) { - std::promise<PremultipliedImage> promise; - map.renderStill([&](std::exception_ptr, PremultipliedImage&& image) { - promise.set_value(std::move(image)); - }); - return promise.get_future().get(); -} - -void checkImage(const std::string& base, - const PremultipliedImage& actual, - double imageThreshold, - double pixelThreshold) { - if (getenv("UPDATE")) { - util::write_file(base + "/expected.png", encodePNG(actual)); - return; - } - - PremultipliedImage expected = decodeImage(util::read_file(base + "/expected.png")); - PremultipliedImage diff { expected.width, expected.height }; - - ASSERT_EQ(expected.width, actual.width); - ASSERT_EQ(expected.height, actual.height); - - double pixels = mapbox::pixelmatch(actual.data.get(), - expected.data.get(), - expected.width, - expected.height, - diff.data.get(), - pixelThreshold); - - EXPECT_LE(pixels / (expected.width * expected.height), imageThreshold); - - util::write_file(base + "/actual.png", encodePNG(actual)); - util::write_file(base + "/diff.png", encodePNG(diff)); -} - -} // namespace test -} // namespace mbgl diff --git a/test/fixtures/util.hpp b/test/fixtures/util.hpp deleted file mode 100644 index 911f2073b5..0000000000 --- a/test/fixtures/util.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef MBGL_TEST_UTIL -#define MBGL_TEST_UTIL - -#include <mbgl/util/image.hpp> -#include <mbgl/util/chrono.hpp> - -#include <cstdint> - -#include <gtest/gtest.h> - -#define SCOPED_TEST(name) \ - static class name { \ - bool completed = false; \ - public: \ - void finish() { EXPECT_FALSE(completed) << #name " was already completed."; completed = true; } \ - ~name() { if (!completed) ADD_FAILURE() << #name " didn't complete."; } \ - } name; - -namespace mbgl { - -class Map; - -namespace test { - -class Server { -public: - Server(const char* executable); - ~Server(); - -private: - int fd = -1; -}; - -PremultipliedImage render(Map&); - -void checkImage(const std::string& base, - const PremultipliedImage& actual, - double imageThreshold = 0, - double pixelThreshold = 0); - -} -} - -#endif |