summaryrefslogtreecommitdiff
path: root/test/include
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-02-18 12:41:09 +0100
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-03-22 11:56:01 -0700
commitdb5ac4785fdc02b4e233201bb3c6f55270e3c65d (patch)
tree0c730d62e471d211924c486e1eeadf5efb305eaf /test/include
parent61920071cd221d0d0627e01893185f0f19b55a98 (diff)
downloadqtlocation-mapboxgl-db5ac4785fdc02b4e233201bb3c6f55270e3c65d.tar.gz
[test] rearrange test files so they're not in the fixtures folder
Diffstat (limited to 'test/include')
-rw-r--r--test/include/mbgl/test/fixture_log_observer.hpp71
-rw-r--r--test/include/mbgl/test/mock_view.hpp29
-rw-r--r--test/include/mbgl/test/stub_file_source.hpp45
-rw-r--r--test/include/mbgl/test/stub_style_observer.hpp67
-rw-r--r--test/include/mbgl/test/util.hpp44
5 files changed, 256 insertions, 0 deletions
diff --git a/test/include/mbgl/test/fixture_log_observer.hpp b/test/include/mbgl/test/fixture_log_observer.hpp
new file mode 100644
index 0000000000..f2ccb5cb58
--- /dev/null
+++ b/test/include/mbgl/test/fixture_log_observer.hpp
@@ -0,0 +1,71 @@
+#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/include/mbgl/test/mock_view.hpp b/test/include/mbgl/test/mock_view.hpp
new file mode 100644
index 0000000000..e608545da5
--- /dev/null
+++ b/test/include/mbgl/test/mock_view.hpp
@@ -0,0 +1,29 @@
+#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/include/mbgl/test/stub_file_source.hpp b/test/include/mbgl/test/stub_file_source.hpp
new file mode 100644
index 0000000000..dbb584fdcc
--- /dev/null
+++ b/test/include/mbgl/test/stub_file_source.hpp
@@ -0,0 +1,45 @@
+#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/include/mbgl/test/stub_style_observer.hpp b/test/include/mbgl/test/stub_style_observer.hpp
new file mode 100644
index 0000000000..7236ca74f5
--- /dev/null
+++ b/test/include/mbgl/test/stub_style_observer.hpp
@@ -0,0 +1,67 @@
+#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/include/mbgl/test/util.hpp b/test/include/mbgl/test/util.hpp
new file mode 100644
index 0000000000..911f2073b5
--- /dev/null
+++ b/test/include/mbgl/test/util.hpp
@@ -0,0 +1,44 @@
+#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