~aleteoryx/muditaos

ref: 48d31b876cf6fed438676e4fa73b6e16b1f6b3a7 muditaos/module-services/service-appmgr/tests/test-ActionsRegistry.cpp -rw-r--r-- 4.8 KiB
48d31b87 — tomaszkrosnowski [EGD-6311] Audio settings windows 4 years ago
                                                                                
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <module-services/service-appmgr/service-appmgr/model/ActionsRegistry.hpp>

using namespace app::manager;

TEST_CASE("Given actions registry when initialise it incorrectly then verify")
{
    REQUIRE_THROWS(ActionsRegistry{nullptr});
}

TEST_CASE("Given empty actions registry when verify its state then no pending actions")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return true; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Given actions registry when enqueue then check pending action")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return true; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(registry.hasPendingAction());
    REQUIRE(registry.getPendingAction()->actionId == actions::Launch);
}

TEST_CASE("Given actions registry when enqueue then check if action was processed")
{
    bool handled            = false;
    auto nextActionCallback = [&handled](ActionEntry & /*entry*/) {
        handled = true;
        return true;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(handled);
}

TEST_CASE("Given actions registry when finished queued action then no pending actions")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return true; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();
    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Given actions registry when enqueue multiple actions sequentially then count handled actions")
{
    int counter             = 0;
    auto nextActionCallback = [&counter](ActionEntry & /*entry*/) {
        ++counter;
        return true;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();

    registry.enqueue(ActionEntry{actions::Launch});
    registry.finished();

    REQUIRE(counter == 3);
}

TEST_CASE("Given actions registry when enqueue multiple actions at once then count handled actions")
{
    int counter             = 0;
    auto nextActionCallback = [&counter](ActionEntry & /*entry*/) {
        ++counter;
        return true;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(counter == 1);
    REQUIRE(registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::Launch});
    registry.enqueue(ActionEntry{actions::Launch});

    registry.finished(); // Finished processing the 1st action
    REQUIRE(counter == 2);
    REQUIRE(registry.hasPendingAction());

    registry.finished(); // Finished processing the 2nd action
    REQUIRE(counter == 3);
    REQUIRE(registry.hasPendingAction());

    registry.finished(); // Finished processing the 3rd action
    REQUIRE(!registry.hasPendingAction());
}

TEST_CASE("Given actions registry when enqueue multiple actions at once then wait for them to expire.")
{
    auto nextActionCallback = [](ActionEntry & /*entry*/) { return true; };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::Launch, {}, std::chrono::steady_clock::now() - std::chrono::seconds{29}});
    registry.enqueue(ActionEntry{actions::Launch, {}, std::chrono::steady_clock::now() - std::chrono::seconds{30}});

    registry.finished(); // Finished processing the 1st action
    REQUIRE(registry.hasPendingAction());

    registry.finished();                   // Finished processing the 2nd action
    REQUIRE(!registry.hasPendingAction()); // 3rd action has expired.
}

TEST_CASE("Given actions registry when enqueue multiple actions at once then process the specific ones only.")
{
    int counter             = 0;
    auto nextActionCallback = [&counter](ActionEntry &entry) {
        if (entry.actionId == actions::UserAction) {
            ++counter;
            return true;
        }
        return false;
    };
    ActionsRegistry registry{std::move(nextActionCallback)};

    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(!registry.hasPendingAction());

    registry.enqueue(ActionEntry{actions::UserAction});
    registry.enqueue(ActionEntry{actions::Launch});
    REQUIRE(counter == 1);

    registry.finished();
    REQUIRE(counter == 1);
    REQUIRE(!registry.hasPendingAction());
}