~aleteoryx/muditaos

muditaos/module-gui/test/test-catch-text/test-gui-MultiTextLine.cpp -rw-r--r-- 5.4 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
// 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 "InitializedFontManager.hpp"

#include <widgets/text/Text.hpp>
#include <widgets/text/core/lines/MultiTextLine.hpp>

#include <mock/buildTextDocument.hpp>
#include <mock/multi-line-string.hpp>
#include <mock/BlockFactory.hpp>

#include <catch2/catch.hpp>

const auto maxWidth = std::numeric_limits<unsigned int>().max();

TEST_CASE("TextLine - ctor")
{
    using namespace gui;

    SECTION("no document")
    {
        Text text;
        auto cursor = new TextCursor(&text);
        auto line   = MultiTextLine(*cursor, maxWidth);
        REQUIRE(line.length() == 0);
    }

    SECTION("first line of multiline - all fits in")
    {
        auto texts            = mockup::lineStrings(3);
        auto [document, font] = mockup::buildMultilineTestDocument(texts);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, font);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() > 0);
        REQUIRE(line.length() == texts.front().length());
    }

    SECTION("oneline line - all fits in")
    {
        auto test_text        = mockup::multiWordString(5);
        auto [document, font] = mockup::buildOnelineTestDocument(test_text);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, font);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() > 0);
        REQUIRE(line.length() == test_text.length());
    }
}

TEST_CASE("MultiTextLine - non fitting text")
{
    using namespace gui;

    SECTION("text > one line to show")
    {
        auto test_text        = mockup::multiWordString(5);
        auto [document, font] = mockup::buildOnelineTestDocument(test_text);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, font);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() != 0);
        REQUIRE(line.length() <= test_text.length());
    }
}

TEST_CASE("MultiTextLine - multiple styles text")
{
    using namespace gui;
    mockup::fontManager();

    auto getTextLen = [](auto blocks) -> unsigned int {
        unsigned int ret = 0;
        for (auto el : blocks) {
            ret += el.length();
        }
        return ret;
    };

    SECTION("multi - style text, one paragraph")
    {
        auto testblock = mockup::getBlock(mockup::BlockFactory::Type::Block0);
        auto document  = TextDocument(testblock);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, nullptr);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.width() > 0);
        REQUIRE(line.length() == getTextLen(testblock));
    }

    SECTION("multi - style text, two paragraphs, first paragraph")
    {
        auto testblock = mockup::getBlock(mockup::BlockFactory::Type::Block0);
        auto block1    = mockup::getBlock(mockup::BlockFactory::Type::Block1);
        auto block0    = mockup::getBlock(mockup::BlockFactory::Type::Block0);

        testblock.insert(testblock.end(), block1.begin(), block1.end());
        auto document = TextDocument(testblock);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, nullptr);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() == getTextLen(block0));
    }

    SECTION("multi - style text, two paragraphs, second paragraph")
    {

        auto testblock = mockup::getBlock(mockup::BlockFactory::Type::Block0);
        auto block1    = mockup::getBlock(mockup::BlockFactory::Type::Block1);
        auto block0    = mockup::getBlock(mockup::BlockFactory::Type::Block0);

        testblock.insert(testblock.end(), block1.begin(), block1.end());
        auto document = TextDocument(testblock);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, block0.size(), nullptr);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() == getTextLen(block1));
    }

    SECTION("multi - empty text, end newline")
    {
        auto testblock = mockup::getBlock(mockup::BlockFactory::Type::NoneBlock0);
        auto document  = TextDocument(testblock);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, nullptr);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() == 0);
        REQUIRE(line.width() == 0);
    }

    SECTION("multi - empty text, end none")
    {
        auto testblock = mockup::getBlock(mockup::BlockFactory::Type::NoneBlock1);
        auto document  = TextDocument(testblock);

        auto cursor = std::make_unique<BlockCursor>(&document, 0, 0, nullptr);
        auto line   = gui::MultiTextLine(*cursor, maxWidth);

        REQUIRE(line.length() == 0);
        REQUIRE(line.width() == 0);
    }
}

TEST_CASE("MultiTextLine - elements sizes checkup")
{
    using namespace gui;

    auto testblock = mockup::getBlock(mockup::BlockFactory::Type::Block0);
    auto document  = TextDocument(testblock);

    auto cursor    = std::make_unique<BlockCursor>(&document, 0, 0, nullptr);
    auto text_line = MultiTextLine(*cursor, maxWidth);

    REQUIRE(text_line.length() > 0);
    const Item *it = nullptr;
    for (unsigned int cnt = 0; cnt < text_line.count(); ++cnt) {
        it = text_line.getElement(cnt);
        REQUIRE(it != nullptr);
        REQUIRE(it->getWidth() > 0);
        REQUIRE(it->getHeight() > 0);
    }
}