summaryrefslogtreecommitdiff
path: root/test/renderer/backend_scope.test.cpp
blob: e78c895ac8261215ac389b554df4a519059b3025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <mbgl/test/util.hpp>

#include <mbgl/renderer/renderer_backend.hpp>
#include <mbgl/gfx/backend_scope.hpp>

#include <functional>

using namespace mbgl;

class StubRendererBackend: public RendererBackend {
public:
    void bind() override {
    }

    mbgl::Size getFramebufferSize() const override {
        return mbgl::Size{};
    }

    void activate() override {
        if (activateFunction) activateFunction();
    }

    void deactivate() override {
        if (deactivateFunction) deactivateFunction();
    }

    void updateAssumedState() override {
        if (updateAssumedStateFunction) updateAssumedStateFunction();
    }

    gl::ProcAddress getExtensionFunctionPointer(const char*) override {
        return {};
    }

    std::function<void ()> activateFunction;
    std::function<void ()> deactivateFunction;
    std::function<void ()> updateAssumedStateFunction;
};

// 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; };

    {
        gfx::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++; };

    {
        gfx::BackendScope outer { backend };
        ASSERT_EQ(1, activated);
        {
            gfx::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; };

    {
        gfx::BackendScope scopeA { backendA };
        ASSERT_TRUE(activatedA);
        {
            gfx::BackendScope scopeB { backendB };
            ASSERT_FALSE(activatedA);
            ASSERT_TRUE(activatedB);
        }
        ASSERT_FALSE(activatedB);
        ASSERT_TRUE(activatedA);
    }

    ASSERT_FALSE(activatedA);
    ASSERT_FALSE(activatedB);
}