~aleteoryx/muditaos

ref: 6f257d38dbd42ac6aa54258e640571b6cbe003ad muditaos/module-gui/test/test-catch/test-gui.cpp -rw-r--r-- 5.1 KiB
6f257d38 — Marcin Lyda [MOS-303] Add playback path gain params to json 3 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
146
147
148
149
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/// These are random tests what could be salvaged from old tests

#include "mock/InitializedFontManager.hpp"

#include <memory>
#include <functional>
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <catch2/catch.hpp>

#include <log/log.hpp>
#include <utf8/UTF8.hpp>

#include <module-gui/gui/core/ImageManager.hpp>
#include <module-gui/gui/core/BoundingBox.hpp>
#include <module-gui/gui/widgets/text/Label.hpp>
#include <module-gui/gui/widgets/BoxLayout.hpp>
#include <module-gui/gui/widgets/Image.hpp>

#include <mock/TestWindow.hpp>

using namespace std;

TEST_CASE("Test BoundingBox intersect")
{
    gui::BoundingBox result;
    REQUIRE(gui::BoundingBox::intersect(gui::BoundingBox(0, 0, 15, 15), gui::BoundingBox(15, 0, 15, 15), result) ==
            false);
    REQUIRE(gui::BoundingBox::intersect(gui::BoundingBox(0, 0, 15, 15), gui::BoundingBox(0, 15, 15, 15), result) ==
            false);
    REQUIRE(gui::BoundingBox::intersect(gui::BoundingBox(0, 0, 15, 15), gui::BoundingBox(-15, 0, 15, 15), result) ==
            false);
    REQUIRE(gui::BoundingBox::intersect(gui::BoundingBox(0, 0, 15, 15), gui::BoundingBox(0, -15, 15, 15), result) ==
            false);
    REQUIRE(gui::BoundingBox::intersect(gui::BoundingBox(0, 0, 15, 15), gui::BoundingBox(14, 0, 15, 15), result) ==
            true);
}

/// note that fontmanager is `global` so loading it now will make it available in next steps
/// which is why it makes next steps work on possibly empty element (fontmanager)
/// tbh - there should allways be fallback to some memory stored font in our FontManager
TEST_CASE("Are fonts loaded")
{
    auto &fontmanager = gui::FontManager::getInstance();
    // check getInstance - getting even default font will result in nullptr
    // this is because no fonts are loaded
    REQUIRE(fontmanager.getFont() == nullptr);
    // now initialize, from where is it taken? nobody knows from this foo
    fontmanager.init("sys/current/assets");
    // check if there is at least default font
    REQUIRE(fontmanager.getFont() != nullptr);
}

TEST_CASE("Draw window with labels")
{
    mockup::fontManager(); // To load fonts
    auto win = new gui::TestWindow("MAIN");
    win->setSize(480, 600);

    // add label with time
    auto label = new gui::Label(win, 20, 80, 440, 120, "TOP LEFT");
    label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top));
    label->setRadius(5);
    label->setFont("gt_pressura_bold_65");

    label = new gui::Label(win, 20, 280, 440, 120, "2xCENTER#####");
    label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
    label->setRadius(10);
    label->setPenWidth(5);
    label->setFont("gt_pressura_bold_65");

    label = new gui::Label(win, 20, 480, 440, 120, "BOTTOM RIGHT");
    label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Bottom));
    label->setRadius(15);
    label->setTextEllipsisType(gui::TextEllipsis::Right);
    label->setFont("gt_pressura_bold_65");

    // vector with draw commands
    // context for drawing commands
    auto commandsList = win->buildDrawList();

    delete win;
}

TEST_CASE("Draw window with box layouts")
{
    mockup::fontManager();
    auto win_unique = make_unique<gui::TestWindow>("MAIN"); // make root gui element used unique to auto clean it
    auto win        = win_unique.get();                     // get pointer to root element for ctros sake
    win->setSize(480, 600);

    gui::HBox *hBox = new gui::HBox(win, 50, 50, 380, 500);

    gui::Rect *maxW1 = new gui::Rect();
    maxW1->setFillColor(gui::Color(5, 0));
    maxW1->setFilled(true);
    maxW1->setMaximumSize(50, 300);

    gui::Label *maxW4 = new gui::Label(nullptr);
    maxW4->setText("Top Left corner");
    maxW4->setTextEllipsisType(gui::TextEllipsis::Right);
    maxW4->setMaximumSize(275, 60);

    gui::Rect *maxW2 = new gui::Rect();
    maxW2->setFillColor(gui::Color(8, 0));
    maxW2->setFilled(true);
    maxW2->setMaximumSize(35, 300);

    gui::Rect *maxW3 = new gui::Rect();
    maxW3->setFillColor(gui::Color(11, 0));
    maxW3->setFilled(true);
    maxW3->setMaximumSize(30, 300);

    hBox->addWidget(maxW1);
    hBox->addWidget(maxW4);
    hBox->addWidget(maxW2);

    gui::VBox *vBox = new gui::VBox(hBox, 10, 155, 460, 600 - 160);

    gui::Rect *maxH1 = new gui::Rect();
    maxH1->setMaximumSize(10, 80);

    gui::Rect *maxH2 = new gui::Rect();
    maxH2->setMaximumSize(15, 300);

    gui::Rect *maxH3 = new gui::Rect();
    maxH3->setMaximumSize(30, 300);

    gui::Label *maxH4 = new gui::Label(nullptr);
    maxH4->setText("Hello Mudita");
    maxH4->setRadius(20);
    maxH4->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
    maxH4->setMaximumSize(75, 60);

    vBox->addWidget(maxH1);
    vBox->addWidget(maxH2);
    vBox->addWidget(maxH4);
    vBox->addWidget(maxH3);

    hBox->addWidget(maxW3);
}