summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-07-13 15:35:02 +0300
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-07-18 10:45:12 +0200
commit9ebf727f56819f259a7a0771b1ed58b14c3adea3 (patch)
tree0fa580307dda6cbc63674c5b8b351455fe94a318 /test
parenta5a0558bde5d67617b6f305179063cd4e0ac329e (diff)
downloadqtlocation-mapboxgl-9ebf727f56819f259a7a0771b1ed58b14c3adea3.tar.gz
[core] BackendScope prevent double (de-)activation
- Guards against duplicate activations by checking wether the backend of the prior scope is the same as the current ones - Makes sure that only the most outer backend scope deactivates by tracking activation state
Diffstat (limited to 'test')
-rw-r--r--test/renderer/backend_scope.test.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/test/renderer/backend_scope.test.cpp b/test/renderer/backend_scope.test.cpp
new file mode 100644
index 0000000000..a8c92bb31f
--- /dev/null
+++ b/test/renderer/backend_scope.test.cpp
@@ -0,0 +1,111 @@
+#include <mbgl/test/util.hpp>
+
+#include <mbgl/renderer/renderer_backend.hpp>
+#include <mbgl/renderer/backend_scope.hpp>
+
+#include <functional>
+
+using namespace mbgl;
+
+class StubRendererBackend: public RendererBackend {
+public:
+
+ void activate() override {
+ if (activateFunction) activateFunction();
+ }
+
+ void deactivate() override {
+ if (deactivateFunction) deactivateFunction();
+ }
+
+ void updateAssumedState() override {
+ if (updateAssumedStateFunction) updateAssumedStateFunction();
+ }
+
+ gl::ProcAddress initializeExtension(const char* ext) override {
+ if (initializeExtensionFunction) {
+ return initializeExtensionFunction(ext);
+ } else {
+ return {};
+ }
+ }
+
+ std::function<void ()> activateFunction;
+ std::function<void ()> deactivateFunction;
+ std::function<void ()> updateAssumedStateFunction;
+ std::function<gl::ProcAddress (const char*)> initializeExtensionFunction;
+};
+
+// A scope should activate on construction
+// and deactivate on descruction (going out
+// of scope)
+TEST(BackendScope, SingleScope) {
+ bool activated;
+ bool deactivated;
+
+ StubRendererBackend backend;
+ backend.activateFunction = [&] { activated = true; };
+ backend.deactivateFunction = [&] { deactivated = true; };
+
+ {
+ BackendScope test { backend };
+ }
+
+ ASSERT_TRUE(activated);
+ ASSERT_TRUE(deactivated);
+}
+
+// With nested scopes, only the outer scope
+// should activate/deactivate
+TEST(BackendScope, NestedScopes) {
+ int activated = 0;
+ int deactivated = 0;
+
+ StubRendererBackend backend;
+ backend.activateFunction = [&] { activated++; };
+ backend.deactivateFunction = [&] { deactivated++; };
+
+ {
+ BackendScope outer { backend };
+ ASSERT_EQ(1, activated);
+ {
+ BackendScope inner { backend };
+ ASSERT_EQ(1, activated);
+ }
+ ASSERT_EQ(0, deactivated);
+ }
+
+ ASSERT_EQ(1, deactivated);
+}
+
+// With chained scopes, where scopes have
+// different backends, the scopes should each
+// activate the scope on entering and de-activating
+// on leaving the scope
+TEST(BackendScope, ChainedScopes) {
+ bool activatedA = false;
+ bool activatedB = false;
+
+ StubRendererBackend backendA;
+ backendA.activateFunction = [&] { activatedA = true; };
+ backendA.deactivateFunction = [&] { activatedA = false; };
+
+ StubRendererBackend backendB;
+ backendB.activateFunction = [&] { activatedB = true; };
+ backendB.deactivateFunction = [&] { activatedB = false; };
+
+ {
+ BackendScope scopeA { backendA };
+ ASSERT_TRUE(activatedA);
+ {
+ BackendScope scopeB { backendB };
+ ASSERT_FALSE(activatedA);
+ ASSERT_TRUE(activatedB);
+ }
+ ASSERT_FALSE(activatedB);
+ ASSERT_TRUE(activatedA);
+ }
+
+ ASSERT_FALSE(activatedA);
+ ASSERT_FALSE(activatedB);
+}