~aleteoryx/muditaos

muditaos/module-gui/test/test-catch-text/test-gui-TextInLineCursor.cpp -rw-r--r-- 9.2 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
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
// 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 <widgets/text/TextFixedSize.hpp>
#include "InitializedFontManager.hpp"

#include <catch2/catch.hpp>

namespace gui
{
    class TestText : public TextFixedSize
    {
      public:
        [[nodiscard]] auto getLine()
        {
            return lines->getLine();
        }

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

        auto removeNChars(unsigned int n)
        {
            for (unsigned int i = 0; i < n; i++) {
                removeChar();
            }
        }

        [[nodiscard]] auto checkOneLine() -> bool
        {
            return lines->size() == 1;
        }

        auto checkCursorDocumentBegin()
        {
            return cursor->atBegin();
        }

        auto checkCursorDocumentEnd()
        {
            return cursor->atEnd();
        }

        auto getLineText()
        {
            return getLine()->getText(0);
        }

        bool checkEllipsis(TextEllipsis ellipsis)
        {
            auto leftCondition =
                getLineText().substr(0, UTF8(text::ellipsis_signs).length()) == UTF8(text::ellipsis_signs);
            auto rightCondition =
                getLineText().substr(getLineText().length() - UTF8(text::ellipsis_signs).length(),
                                     UTF8(text::ellipsis_signs).length()) == UTF8(text::ellipsis_signs);

            switch (ellipsis) {
            case TextEllipsis::Left:
                return leftCondition && !rightCondition;
            case TextEllipsis::Right:
                return !leftCondition && rightCondition;
            case TextEllipsis::Both:
                return leftCondition && rightCondition;
            default:
                return false;
            }
        }
    };
} // namespace gui

TEST_CASE("TextInLineCursor - navigation test with ellipsis")
{
    using namespace gui;

    std::string testStringLine = "Test Long Long Long Long Line "; // 30 sings
    unsigned int lineLength    = 10;

    SECTION("Space for 10 sings - navigation from document begin")
    {
        mockup::fontManager();
        auto text = std::make_unique<gui::TestText>();
        text->setSize(120, 30);
        text->setTextType(gui::TextType::SingleLine);
        text->setCursorStartPosition(gui::CursorStartPosition::DocumentBegin);

        text->setText(testStringLine);

        REQUIRE(text->checkOneLine());
        REQUIRE(text->getLine()->length() == lineLength);
        REQUIRE(text->getLine()->getText(0) == testStringLine.substr(0, lineLength));
        REQUIRE(text->checkCursorDocumentBegin());

        // Move cursor at line ending -> content should be the same
        text->moveCursor(gui::NavigationDirection::RIGHT, lineLength);
        REQUIRE(text->getLineText() == testStringLine.substr(0, lineLength));

        // Move cursor one sign more and check content
        text->moveCursor(gui::NavigationDirection::RIGHT, 1);
        REQUIRE(text->getLineText() == testStringLine.substr(1, lineLength));

        // Move cursor 5 signs more and check content
        text->moveCursor(gui::NavigationDirection::RIGHT, 5);
        REQUIRE(text->getLineText() == testStringLine.substr(6, lineLength));

        // Move cursor to document end;
        text->moveCursor(gui::NavigationDirection::RIGHT, text->getText().length());
        REQUIRE(text->getLineText() ==
                testStringLine.substr(text->getText().length() - lineLength - 1, text->getText().length()));
        REQUIRE(text->checkCursorDocumentEnd());

        // Move cursor back for text length - cursor should be at document begin.
        text->moveCursor(gui::NavigationDirection::LEFT, text->getText().length());
        REQUIRE(text->getLineText() == testStringLine.substr(0, lineLength));
        REQUIRE(text->checkCursorDocumentBegin());
    }

    SECTION("Space for 10 sings - navigation from document end")
    {
        mockup::fontManager();
        auto text = std::make_unique<gui::TestText>();
        text->setSize(120, 30);
        text->setTextType(gui::TextType::SingleLine);
        text->setCursorStartPosition(gui::CursorStartPosition::DocumentEnd);

        text->setText(testStringLine);

        REQUIRE(text->checkOneLine());
        REQUIRE(text->getLine()->length() == lineLength + 1);
        REQUIRE(text->getLine()->getText(0) ==
                testStringLine.substr(text->getText().length() - lineLength - 1, lineLength + 1));
        REQUIRE(text->checkCursorDocumentEnd());

        // Move cursor at line begin -> content should be the same
        text->moveCursor(gui::NavigationDirection::LEFT, lineLength + 1);
        REQUIRE(text->getLine()->getText(0) ==
                testStringLine.substr(text->getText().length() - lineLength - 1, lineLength + 1));

        // Move cursor one sign more and check content
        text->moveCursor(gui::NavigationDirection::LEFT, 1);
        REQUIRE(text->getLineText() ==
                testStringLine.substr(text->getText().length() - lineLength - 2, lineLength + 1));

        // Move cursor back for text length - cursor should be at document begin.
        text->moveCursor(gui::NavigationDirection::LEFT, text->getText().length());
        REQUIRE(text->getLineText() == testStringLine.substr(0, lineLength));
        REQUIRE(text->checkCursorDocumentBegin());
    }
}

TEST_CASE("TextInLineCursor - navigation test without ellipsis")
{
    using namespace gui;

    std::string testStringLine = "Test Long Long Long Long Line "; // 30 sings
    unsigned int lineLength    = 10;

    SECTION("Space for 10 sings - navigation with left ellipsis")
    {
        mockup::fontManager();
        auto text = std::make_unique<gui::TestText>();
        text->setSize(120, 30);
        text->setTextType(gui::TextType::SingleLine);
        text->setCursorStartPosition(gui::CursorStartPosition::DocumentBegin);
        text->setTextEllipsisType(gui::TextEllipsis::Left);

        text->setText(testStringLine);

        REQUIRE(text->checkOneLine());
        REQUIRE(text->getLine()->length() == lineLength);
        REQUIRE(text->getLine()->getText(0) == testStringLine.substr(0, lineLength));
        REQUIRE(text->checkCursorDocumentBegin());

        // Move cursor at line ending -> content should be the same
        text->moveCursor(gui::NavigationDirection::RIGHT, lineLength);
        REQUIRE(text->getLineText() == testStringLine.substr(0, lineLength));

        // Move cursor at text ending -> left ellipsis should appear
        text->moveCursor(gui::NavigationDirection::RIGHT, text->getText().length());
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Left));

        // Move cursor at text beginning -> no ellipsis
        text->moveCursor(gui::NavigationDirection::LEFT, text->getText().length());
        REQUIRE(!text->checkEllipsis(gui::TextEllipsis::Left));
    }

    SECTION("Space for 10 sings - navigation with right ellipsis")
    {
        mockup::fontManager();
        auto text = std::make_unique<gui::TestText>();
        text->setSize(120, 30);
        text->setTextType(gui::TextType::SingleLine);
        text->setCursorStartPosition(gui::CursorStartPosition::DocumentBegin);
        text->setTextEllipsisType(gui::TextEllipsis::Right);

        text->setText(testStringLine);

        REQUIRE(text->checkOneLine());
        REQUIRE(text->checkCursorDocumentBegin());
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Right));

        // Move cursor at text end -> no ellipsis
        text->moveCursor(gui::NavigationDirection::RIGHT, text->getText().length());
        REQUIRE(!text->checkEllipsis(gui::TextEllipsis::Right));

        // Move cursor at text beginning -> right ellipsis should appear
        text->moveCursor(gui::NavigationDirection::LEFT, text->getText().length());
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Right));
    }

    SECTION("Space for 10 sings - navigation with both ellipsis")
    {
        mockup::fontManager();
        auto text = std::make_unique<gui::TestText>();
        text->setSize(120, 30);
        text->setTextType(gui::TextType::SingleLine);
        text->setCursorStartPosition(gui::CursorStartPosition::DocumentBegin);
        text->setTextEllipsisType(gui::TextEllipsis::Both);

        text->setText(testStringLine);

        REQUIRE(text->checkOneLine());
        REQUIRE(text->checkCursorDocumentBegin());
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Right));
        REQUIRE(!text->checkEllipsis(gui::TextEllipsis::Both));

        // Move cursor at middle -> both ellipsis
        text->moveCursor(gui::NavigationDirection::RIGHT, lineLength);
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Both));

        // Move cursor at text end -> left ellipsis should appear
        text->moveCursor(gui::NavigationDirection::RIGHT, text->getText().length());
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Left));
        REQUIRE(!text->checkEllipsis(gui::TextEllipsis::Both));

        // Move cursor at text beginning -> right ellipsis should appear
        text->moveCursor(gui::NavigationDirection::LEFT, text->getText().length());
        REQUIRE(text->checkEllipsis(gui::TextEllipsis::Right));
        REQUIRE(!text->checkEllipsis(gui::TextEllipsis::Both));
    }
}