~aleteoryx/muditaos

muditaos/module-apps/tests/windows/test-WindowsPopupQueue.cpp -rw-r--r-- 3.3 KiB
a405cad6Aleteoryx trim readme 6 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "WindowsPopupFilter.hpp"
#include <catch2/catch.hpp>
#include <module-apps/apps-common/WindowsPopupQueue.hpp>

gui::popup::Filter filter;

TEST_CASE("WindowsPopupQueue creation")
{
    auto wp = app::WindowsPopupQueue();

    std::optional<gui::popup::Request> r = wp.popRequest(filter);
    REQUIRE(not r);
}

TEST_CASE("WindowsPopupQueue push")
{
    using namespace gui::popup;
    auto wp = app::WindowsPopupQueue();
    Blueprint blueprint;
    SECTION("single")
    {
        Request request(
            gui::popup::ID::Alarm, std::make_unique<gui::PopupRequestParams>(gui::popup::ID::Alarm), blueprint);
        wp.pushRequest(std::move(request));
        wp.popRequest(filter);
        auto r = wp.popRequest(filter);
        REQUIRE(not r);
    }

    /// we should be able to have multiple same requests stored on the popups queue
    SECTION("multiple")
    {
        const auto count = 3;
        auto request_gen = [blueprint]() {
            return Request(
                gui::popup::ID::Alarm, std::make_unique<gui::PopupRequestParams>(gui::popup::ID::Alarm), blueprint);
        };
        Request request[count] = {request_gen(), request_gen(), request_gen()};
        for (auto &val : request) {
            wp.pushRequest(std::move(val));
        }
        for (auto i = count; i != 0; --i) {
            auto r = wp.popRequest(filter);
            REQUIRE(r);
        }
    }
}

class NoneFilter : public gui::popup::Filter
{
    bool isPermitted(const gui::PopupRequestParams & /*params*/) const override
    {
        return false;
    }
};

TEST_CASE("WindowsPopupQueue filter")
{
    using namespace gui::popup;
    auto wp = app::WindowsPopupQueue();
    Blueprint blueprint;
    auto noneFilter = NoneFilter();
    Request request(gui::popup::ID::Alarm, std::make_unique<gui::PopupRequestParams>(gui::popup::ID::Alarm), blueprint);
    wp.pushRequest(std::move(request));
    {
        auto r = wp.popRequest(noneFilter);
        REQUIRE(not r);
    }
    {
        auto r = wp.popRequest(filter);
        REQUIRE(r);
    }
    {
        auto r = wp.popRequest(noneFilter);
        REQUIRE(not r);
    }
    {
        auto r = wp.popRequest(filter);
        REQUIRE(not r);
    }
}

TEST_CASE("WindowsPopupRequest - handle empty request foo")
{
    using namespace gui::popup;
    SECTION("no blueprint")
    {
        Blueprint blueprint;
        Request request(
            gui::popup::ID::Alarm, std::make_unique<gui::PopupRequestParams>(gui::popup::ID::Alarm), blueprint);
        REQUIRE_THROWS_AS(request.handle(), std::bad_function_call);
    }

    SECTION("failing blueprint")
    {
        Blueprint blueprint = [](gui::popup::ID, std::unique_ptr<gui::PopupRequestParams> &) -> bool { return false; };
        Request request(
            gui::popup::ID::Alarm, std::make_unique<gui::PopupRequestParams>(gui::popup::ID::Alarm), blueprint);
        REQUIRE(not request.handle());
    }

    SECTION("successful blueprint")
    {
        Blueprint blueprint = [](gui::popup::ID, std::unique_ptr<gui::PopupRequestParams> &) -> bool { return true; };
        Request request(
            gui::popup::ID::Alarm, std::make_unique<gui::PopupRequestParams>(gui::popup::ID::Alarm), blueprint);
        REQUIRE(request.handle());
    }
}