~aleteoryx/muditaos

ref: b4814861b02051c9b402627577eec80aeb258830 muditaos/module-gui/test/test-catch-text/test-gui-Text.cpp -rw-r--r-- 8.9 KiB
b4814861 — Roman Kubiak [EGD-4318] enable service desktop (#973) 5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "BoundingBox.hpp"
#include "BoxLayout.hpp"
#include "Common.hpp"
#include "InitializedFontManager.hpp"
#include "TextParse.hpp"
#include "i18/i18.hpp"
#include "mock/InitializedFontManager.hpp"
#include <catch2/catch.hpp>
#include <limits>

#include <module-gui/gui/widgets/Text.hpp>
#include <mock/multi-line-string.hpp>
#include <algorithm>
#include <mock/BlockFactory.hpp>
#include <RawFont.hpp>

TEST_CASE("Text ctor")
{
    using namespace gui;
    mockup::fontManager();

    SECTION("none")
    {
        auto text = Text();
        REQUIRE(text.getText() == "");
    }

    SECTION("empty 0")
    {
        auto text = Text(nullptr, 0, 0, 0, 0);
        REQUIRE(text.getText() == "");
    }

    SECTION("one line")
    {
        auto testtext = "text0 text1 text2";
        auto text     = Text(nullptr, 0, 0, 0, 0, testtext);
        REQUIRE(text.getText() == testtext);
    }
}

namespace gui
{
    class TestText : public Text
    {
      public:
        unsigned int linesSize()
        {
            return lines->get().size();
        }

        void drawLines() override
        {
            Text::drawLines();
            return;
        }

        auto &linesGet()
        {
            return lines->get();
        }

        [[nodiscard]] auto *getInputMode()
        {
            return mode;
        }

        auto moveCursor(NavigationDirection direction, unsigned int n)
        {
            cursor->TextCursor::moveCursor(direction, n);
        }

        [[nodiscard]] auto getCursorPos()
        {
            return cursor->getPosOnScreen();
        }
    };
} // namespace gui

TEST_CASE("Text drawLines")
{

    using namespace gui;

    auto &fontmanager = mockup::fontManager();

    SECTION("one line")
    {
        auto words_count = 3;
        auto testline    = mockup::multiWordString(words_count);
        auto text        = TestText();
        text.setSize(400, 100);
        text.setText(testline);
        text.drawLines();
        REQUIRE(text.linesSize() == 1);
    }

    SECTION("all multiline visible with newline end")
    {
        unsigned int lines_count = 4;
        auto testline            = mockup::multiLineString(lines_count);
        auto text                = TestText();
        text.setSize(300, 500);
        text.setText(std::make_unique<TextDocument>(
            textToTextBlocks(testline, fontmanager.getFont(0), TextBlock::End::Newline)));

        text.drawLines();
        // Extra one line for empty newline at end
        REQUIRE(text.linesSize() == lines_count + 1);
    }
}

TEST_CASE("Text buildDrawList")
{
    using namespace gui;

    auto &fontmanager = mockup::fontManager();
    auto lines_count  = 10;
    auto testline     = mockup::multiLineString(lines_count);
    auto font         = fontmanager.getFont(0);
    auto text         = TestText();
    text.setSize(3000, 3000);
    text.setText(std::make_unique<TextDocument>(textToTextBlocks(testline, font, TextBlock::End::Newline)));

    SECTION("check every element in every line if it's in parent and has non zero sizes apart from last empty line "
            "from newline")
    {
        REQUIRE(text.linesGet().size() > 0);
        for (auto &line : text.linesGet()) {
            if (&line == &text.linesGet().back()) {
                REQUIRE(line.length() == 0);
            }
            else {
                REQUIRE(line.length() > 0);
            }
        }
    }
}

TEST_CASE("handle input mode ABC/abc/1234")
{
    utils::localize.SetDisplayLanguage(utils::Lang::En); /// needed to load input mode
    auto &fontmanager = mockup::fontManager();
    auto font         = fontmanager.getFont(0);
    auto text         = gui::TestText();
    auto modes        = {InputMode::ABC, InputMode::abc, InputMode::digit};
    auto str          = text.getText();
    auto next_mode    = gui::InputEvent({}, gui::InputEvent::State::keyReleasedShort, gui::KeyCode::KEY_AST);
    auto key_2        = gui::InputEvent(
        {
            RawKey::State::Released,
            bsp::KeyCodes::NumericKey2,
        },
        gui::InputEvent::State::keyReleasedShort);
    text.setInputMode(new InputMode(modes));
    text.setFont(font);

    SECTION("ABC -> abc")
    {
        REQUIRE(text.getInputMode()->is(InputMode::ABC));
        text.onInput(key_2);
        str += "A";
        REQUIRE(str == text.getText());

        text.onInput(next_mode);
        REQUIRE(text.getInputMode()->is(InputMode::abc));
        text.onInput(key_2);
        str += "a";
        REQUIRE(!(str == text.getText())); // not equal because its `key 2 double click..., A was changed to a`
    }

    SECTION("ABC -> abc")
    {
        auto time_long_enough_to_not_be_multipress = 1000;
        text.onInput(next_mode);
        key_2.key.time_release += time_long_enough_to_not_be_multipress;
        REQUIRE(text.getInputMode()->is(InputMode::abc));
        text.onInput(key_2);
        key_2.key.time_release += time_long_enough_to_not_be_multipress;
        str += "a";
        REQUIRE(str == text.getText());

        text.onInput(key_2);
        str += "a";
        REQUIRE(str == text.getText());
    }

    SECTION("ABC -> abc -> digit")
    {
        text.onInput(next_mode);
        text.onInput(next_mode);
        REQUIRE(text.getInputMode()->is(InputMode::digit));
        text.onInput(key_2);
        str += "2";
        REQUIRE(str == text.getText());
    }
}

TEST_CASE("handle longpress for digit in ABC mode")
{
    auto text  = gui::TestText();
    auto str   = text.getText() + "1";
    auto key_1 = gui::InputEvent({}, gui::InputEvent::State::keyReleasedLong, gui::KeyCode::KEY_1);
    text.setInputMode(new InputMode({InputMode::ABC}));
    text.onInput(key_1);
    REQUIRE(str == text.getText());
}

TEST_CASE("handle text expand")
{
    mockup::fontManager();
    using namespace gui;
    Length w         = 10;
    Length h         = 100;
    BoxLayout layout = BoxLayout(nullptr, 0, 0, w, h);
    auto text        = new gui::TestText();
    layout.addWidget(text);
    text->setMaximumSize(w, h);
    REQUIRE(text->area() != BoundingBox{0, 0, 0, 0});
}

TEST_CASE("handle newline")
{
    mockup::fontManager();
    using namespace gui;
    Length w         = 10;
    Length h         = 100;
    BoxLayout layout = BoxLayout(nullptr, 0, 0, w, h);
    auto text        = new gui::TestText();
    layout.addWidget(text);
    text->setMaximumSize(w, h);
    text->addText("\n");
    REQUIRE(text->getText() == "\n");
    text->addText("text");
    REQUIRE(text->getText() == "\ntext");
    text->addText("\n");
    REQUIRE(text->getText() == "\ntext\n");
}

TEST_CASE("handle text block - moved cursor to end")
{
    std::string test_text = "text";
    std::string newline   = "\n";
    auto text             = gui::TestText();
    text.addText(gui::TextBlock(test_text, mockup::fontManager().getFont(0), gui::TextBlock::End::None));
    REQUIRE(text.getText() == test_text);
    test_text = test_text + newline;
    text.addText(newline);
    REQUIRE(text.getText() == test_text);
}

TEST_CASE("Text backup and restore tests")
{
    std::string testStringOneLine   = "Test String ";
    std::string testStringTwoLines  = "Test String 1 \n Test String 2";
    std::string overwriteTestString = "Overwrite test String";

    SECTION("Backup one line text with moved cursor, overwrite text and restore")
    {
        mockup::fontManager();
        auto text = new gui::TestText();

        text->addText(testStringOneLine);

        unsigned int cursorMoveN = 2;
        text->moveCursor(gui::NavigationDirection::LEFT, cursorMoveN);

        auto backup = text->backupText();

        REQUIRE(backup.document.getText() == text->getText());
        REQUIRE(backup.document.getText().length() == text->getText().length());
        REQUIRE(backup.cursorPos == text->getCursorPos());

        text->setText(overwriteTestString);

        REQUIRE(text->getText() != testStringOneLine);

        text->restoreFrom(backup);

        REQUIRE(text->getText() == testStringOneLine);
        REQUIRE(text->getCursorPos() == testStringOneLine.length() - cursorMoveN);
    }

    SECTION("Backup two line text with moved cursor, overwrite text and restore")
    {
        mockup::fontManager();
        auto text = new gui::TestText();

        text->addText(testStringTwoLines);

        unsigned int cursorMoveN = 10;
        text->moveCursor(gui::NavigationDirection::LEFT, cursorMoveN);

        auto backup = text->backupText();

        REQUIRE(backup.document.getText() == text->getText());
        REQUIRE(backup.document.getText().length() == text->getText().length());
        REQUIRE(backup.cursorPos == text->getCursorPos());

        text->setText(overwriteTestString);

        REQUIRE(text->getText() != testStringOneLine);

        text->restoreFrom(backup);

        REQUIRE(text->getText() == testStringTwoLines);
        REQUIRE(text->getCursorPos() == testStringTwoLines.length() - cursorMoveN);
    }
}