diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-04-07 18:55:48 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-04-11 11:00:34 -0700 |
commit | 1c7c5af69df3edc19ba48697d350de087b9243cf (patch) | |
tree | c9538a61d974634db4aa6f3ae20d4ff9078ad75e /test/src | |
parent | c507b8a693d91dea1b0018f9105553128a245146 (diff) | |
download | qtlocation-mapboxgl-1c7c5af69df3edc19ba48697d350de087b9243cf.tar.gz |
[ios] Run core unit tests in CI
Diffstat (limited to 'test/src')
-rw-r--r-- | test/src/main.cpp | 7 | ||||
-rw-r--r-- | test/src/main.mm | 8 | ||||
-rw-r--r-- | test/src/mbgl/test/fixture_log_observer.cpp (renamed from test/src/fixture_log_observer.cpp) | 0 | ||||
-rw-r--r-- | test/src/mbgl/test/fixture_log_observer.hpp | 71 | ||||
-rw-r--r-- | test/src/mbgl/test/mock_view.hpp | 29 | ||||
-rw-r--r-- | test/src/mbgl/test/stub_file_source.cpp (renamed from test/src/stub_file_source.cpp) | 0 | ||||
-rw-r--r-- | test/src/mbgl/test/stub_file_source.hpp | 45 | ||||
-rw-r--r-- | test/src/mbgl/test/stub_style_observer.hpp | 67 | ||||
-rw-r--r-- | test/src/mbgl/test/test.cpp | 11 | ||||
-rw-r--r-- | test/src/mbgl/test/util.cpp (renamed from test/src/util.cpp) | 0 | ||||
-rw-r--r-- | test/src/mbgl/test/util.hpp | 80 |
11 files changed, 309 insertions, 9 deletions
diff --git a/test/src/main.cpp b/test/src/main.cpp index f2d2944ec7..a481dc5dc3 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -1,6 +1,5 @@ -#include <mbgl/test/util.hpp> +#include <mbgl/test.hpp> -GTEST_API_ int main(int argc, char *argv[]) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); +int main(int argc, char *argv[]) { + return mbgl::runTests(argc, argv); } diff --git a/test/src/main.mm b/test/src/main.mm index abfa62972b..12d5de4d5a 100644 --- a/test/src/main.mm +++ b/test/src/main.mm @@ -1,10 +1,8 @@ -#import <Foundation/Foundation.h> +#include <mbgl/test.hpp> -#include <mbgl/test/util.hpp> +#import <Foundation/Foundation.h> int main(int argc, char* argv[]) { [[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] bundlePath]]; - - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + return mbgl::runTests(argc, argv); } diff --git a/test/src/fixture_log_observer.cpp b/test/src/mbgl/test/fixture_log_observer.cpp index 302fdc7081..302fdc7081 100644 --- a/test/src/fixture_log_observer.cpp +++ b/test/src/mbgl/test/fixture_log_observer.cpp diff --git a/test/src/mbgl/test/fixture_log_observer.hpp b/test/src/mbgl/test/fixture_log_observer.hpp new file mode 100644 index 0000000000..f2ccb5cb58 --- /dev/null +++ b/test/src/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/src/mbgl/test/mock_view.hpp b/test/src/mbgl/test/mock_view.hpp new file mode 100644 index 0000000000..e608545da5 --- /dev/null +++ b/test/src/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/src/stub_file_source.cpp b/test/src/mbgl/test/stub_file_source.cpp index 89ccb3b335..89ccb3b335 100644 --- a/test/src/stub_file_source.cpp +++ b/test/src/mbgl/test/stub_file_source.cpp diff --git a/test/src/mbgl/test/stub_file_source.hpp b/test/src/mbgl/test/stub_file_source.hpp new file mode 100644 index 0000000000..477f72ed42 --- /dev/null +++ b/test/src/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<AsyncRequest> 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<AsyncRequest*, std::tuple<Resource, ResponseFunction, Callback>> pending; + util::Timer timer; +}; + +} + +#endif diff --git a/test/src/mbgl/test/stub_style_observer.hpp b/test/src/mbgl/test/stub_style_observer.hpp new file mode 100644 index 0000000000..7236ca74f5 --- /dev/null +++ b/test/src/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/src/mbgl/test/test.cpp b/test/src/mbgl/test/test.cpp new file mode 100644 index 0000000000..cbc6cfb102 --- /dev/null +++ b/test/src/mbgl/test/test.cpp @@ -0,0 +1,11 @@ +#include <mbgl/test.hpp> +#include <gtest/gtest.h> + +namespace mbgl { + +int runTests(int argc, char *argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + +} diff --git a/test/src/util.cpp b/test/src/mbgl/test/util.cpp index ca2282a4b5..ca2282a4b5 100644 --- a/test/src/util.cpp +++ b/test/src/mbgl/test/util.cpp diff --git a/test/src/mbgl/test/util.hpp b/test/src/mbgl/test/util.hpp new file mode 100644 index 0000000000..f778679cb8 --- /dev/null +++ b/test/src/mbgl/test/util.hpp @@ -0,0 +1,80 @@ +#ifndef MBGL_TEST_UTIL +#define MBGL_TEST_UTIL + +#ifdef __APPLE__ +#include <TargetConditionals.h> +#endif + +#if TARGET_OS_IOS +#define TEST_READ_ONLY 1 +#define TEST_HAS_SERVER 0 +#else +#define TEST_READ_ONLY 0 +#define TEST_HAS_SERVER 1 +#endif + +#if TARGET_OS_SIMULATOR +#define TEST_IS_SIMULATOR 1 +#else +#define TEST_IS_SIMULATOR 0 +#endif + +#if !TEST_IS_SIMULATOR +#define TEST_REQUIRES_ACCURATE_TIMING(name) name +#else +#define TEST_REQUIRES_ACCURATE_TIMING(name) DISABLED_ ## name +#endif + +#if !TEST_READ_ONLY +#define TEST_REQUIRES_WRITE(name) name +#else +#define TEST_REQUIRES_WRITE(name) DISABLED_ ## name +#endif + +#if TEST_HAS_SERVER +#define TEST_REQUIRES_SERVER(name) name +#else +#define TEST_REQUIRES_SERVER(name) DISABLED_ ## name +#endif + +#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 |