~aleteoryx/muditaos

ref: 9ef454085e4cca8a1f6c3c270cf460f899c9ee23 muditaos/module-gui/test/test-catch/test-gui-callbacks.cpp -rw-r--r-- 3.7 KiB
9ef45408 — Lefucjusz [MOS-1064] Fix no input language selected for French/Spanish 1 year, 9 months 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>
#include <module-gui/gui/widgets/Item.hpp>
#include <mock/TestWindow.hpp>
#include <gui/input/InputEvent.hpp>

// there is strict order on which callbacks should be called
TEST_CASE("gui::Item on input flow test")
{
    bool success = false;
    auto item    = gui::Item();

    SECTION("gui item onInput callback called")
    {
        success            = false;
        item.inputCallback = [&success](gui::Item &, const gui::InputEvent &inputEvent) -> bool {
            success = true;
            return true;
        };
        // trigger callback
        item.onInput(gui::InputEvent({}));
        // test callback called
        REQUIRE(success == true);
    }

    SECTION("gui item resize called")
    {
        success                       = false;
        item.dimensionChangedCallback = [&success](gui::Item &, gui::BoundingBox data) -> bool {
            success = true;
            return true;
        };
        // trigger callback
        item.setSize(item.getWidth(), item.getHeight());
        // test callback called
        REQUIRE(success == true);
    }
}

#include <module-gui/gui/widgets/text/Label.hpp>
#include "mock/InitializedFontManager.hpp"

TEST_CASE("gui::Window on input flow test")
{
    auto win     = gui::TestWindow("Callback test window");
    bool success = false;
    SECTION("gui item onInput callback called")
    {
        success           = false;
        win.inputCallback = [&success](gui::Item &, const gui::InputEvent &inputEvent) -> bool {
            success = true;
            return true;
        };
        // trigger callback
        win.onInput(gui::InputEvent({}));
        // test callback called
        REQUIRE(success == true);
    }

    SECTION("gui item - no input handling, no navigation")
    {
        win.inputCallback = nullptr;
        win.onInput(gui::InputEvent({}));
        REQUIRE(success == false);
    }

    mockup::fontManager();
    auto l1 = new gui::Label(&win, 0, 0, 0, 0, "Test 1");
    auto l2 = new gui::Label(&win, 0, 0, 0, 0, "Test 2");

    SECTION("gui item - no input handling with navigation")
    {
        bool focus_acquired_l1 = false;
        bool focus_acquired_l2 = false;
        bool l1_input_handled  = false;
        l1->setNavigationItem({gui::NavigationDirection::DOWN}, l2);
        l1->focusChangedCallback = [&focus_acquired_l1](gui::Item &it) -> bool {
            if (it.focus) {
                focus_acquired_l1 = true;
            }
            return true;
        };

        l1->inputCallback = [&l1_input_handled](gui::Item &, const gui::InputEvent &inputEvent) -> bool {
            if (inputEvent.is(gui::KeyCode::KEY_TORCH)) {
                l1_input_handled = true;
                return true;
            }
            return false;
        };

        l2->setNavigationItem({gui::NavigationDirection::DOWN}, l1);
        l2->focusChangedCallback = [&focus_acquired_l2](gui::Item &it) -> bool {
            if (it.focus) {
                focus_acquired_l2 = true;
            }
            return true;
        };

        win.setFocusItem(l1);
        focus_acquired_l2 = false;
        win.onInput(gui::InputEvent({}, gui::InputEvent::State::KeyReleasedShort, gui::KeyCode::KEY_DOWN));
        REQUIRE(focus_acquired_l2);

        focus_acquired_l1 = false;
        win.onInput(gui::InputEvent({}, gui::InputEvent::State::KeyReleasedShort, gui::KeyCode::KEY_DOWN));
        REQUIRE(focus_acquired_l1);

        win.onInput(gui::InputEvent({}, gui::InputEvent::State::KeyReleasedShort, gui::KeyCode::KEY_TORCH));
        REQUIRE(l1_input_handled);
    }
}