~aleteoryx/muditaos

ref: sign_test muditaos/module-gui/test/test-google/test-gui-listview.cpp -rw-r--r-- 17.8 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "gtest/gtest.h"

#include "TestListView.hpp"

#include <gui/input/InputEvent.hpp>
#include <gui/widgets/ListView.hpp>
#include <test/mock/TestListViewProvider.hpp>

#include <log/log.hpp>

class ListViewTesting : public ::testing::Test
{
  protected:
    void SetUp() override
    {
        testProvider = std::make_shared<gui::TestListViewProvider>();
        testListView = new TestListView(
            nullptr, testStyle::list_x, testStyle::list_y, testStyle::list_w, testStyle::list_h, nullptr);

        ASSERT_EQ(0, testListView->currentPageSize) << "List should be empty";

        testListView->setProvider(testProvider);
        testListView->rebuildList();
        testListView->focus = true;
    }

    void TearDown() override
    {
        delete testListView;
    }

    void moveNTimes(unsigned int n, gui::listview::Direction direction)
    {
        auto key = gui::KeyCode::KEY_UP;

        if (direction == gui::listview::Direction::Top)
            key = gui::KeyCode::KEY_UP;
        if (direction == gui::listview::Direction::Bottom)
            key = gui::KeyCode::KEY_DOWN;

        for (unsigned int i = 0; i < n; i++) {
            testListView->onInput(gui::InputEvent({}, gui::InputEvent::State::keyReleasedShort, key));
        }
    }

    std::shared_ptr<gui::TestListViewProvider> testProvider = nullptr;
    TestListView *testListView                              = nullptr;
};

TEST_F(ListViewTesting, Constructor_Destructor_Test)
{
    // Check that there are no memory leaks - done by fixture setup and teardown.
}

TEST_F(ListViewTesting, Fill_List_And_Item_Magin_Test)
{
    // Assign list to provider and request data - item margins set to 0, element height at 100, list height 600
    testListView->provider->requestRecords(0, 10);

    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list";

    testProvider->testItemMargins = gui::Margins(0, 50, 0, 0);
    testListView->provider->requestRecords(0, 10);

    ASSERT_EQ(4, testListView->currentPageSize) << "Change span size to 50 so there will be space for 4 elements";
}

TEST_F(ListViewTesting, Not_Equal_Items_Test)
{
    // Assign list to provider and request data - item margins set to 0, elements height set to 100,200,300,400... ,
    // list height 600
    testProvider->notEqualItems = true;
    testListView->provider->requestRecords(0, 10);

    ASSERT_EQ(3, testListView->currentPageSize) << "3 elements should fit into list (100+200+300)";

    moveNTimes(testListView->currentPageSize, gui::listview::Direction::Bottom);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate bottom by page size - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(1, testListView->currentPageSize) << "1 elements should fit into list (400)";

    moveNTimes(1, gui::listview::Direction::Top);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate top by one - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(3, testListView->currentPageSize) << "3 elements should fit into list (100+200+300)";
}

TEST_F(ListViewTesting, List_Clear_Test)
{
    // Assign list to provider and request data - item margins set to 0, element height at 100, list height 600
    testListView->provider->requestRecords(0, 10);

    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 0";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";

    // Change pages
    moveNTimes(6, gui::listview::Direction::Bottom);
    moveNTimes(1, gui::listview::Direction::Top);

    // Clear list and request same data as before -> page should refresh with same data.
    testListView->reset();
    testProvider->refreshList();

    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 0";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";
}

TEST_F(ListViewTesting, Scroll_Test)
{
    testListView->provider->requestRecords(0, 10);
    ASSERT_TRUE(testListView->scroll->shouldShowScroll(testListView->currentPageSize, testListView->elementsCount))
        << "10 provider elements, 100 h each, list 600 - scroll yes.";
    ASSERT_TRUE(testListView->scroll->visible) << "10 provider elements, 100 h each, list 600 - scroll yes";

    testProvider->testItemCount = 5;
    testListView->rebuildList();
    testListView->provider->requestRecords(0, 10);
    ASSERT_FALSE(testListView->scroll->shouldShowScroll(testListView->currentPageSize, testListView->elementsCount))
        << "set provider elements to 5, 100 h each, list 600 -  scroll no.";
    ASSERT_FALSE(testListView->scroll->visible) << "set provider elements to 5, 100 h each, list 600 -  scroll no";
}

TEST_F(ListViewTesting, Navigate_Test)
{
    // 10 provider elements, 100 h each, list 600.
    testListView->provider->requestRecords(0, 10);
    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 0";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";

    moveNTimes(1, gui::listview::Direction::Top);
    ASSERT_TRUE(testListView->listBorderReached)
        << "Navigate top by 1 - page should not change as it is TopDown list - but list border reached";
    testListView->listBorderReached = false;
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 0";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";

    moveNTimes(6, gui::listview::Direction::Bottom);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate bottom by 6 - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(4, testListView->currentPageSize) << "4 elements should be displayed (10 - 6)";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 6";
    ASSERT_EQ(9, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 9";
    ASSERT_EQ(gui::listview::Direction::Bottom, testListView->direction) << "List Direction should be Bottom";

    moveNTimes(1, gui::listview::Direction::Top);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate top by one - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should be displayed";
    ASSERT_EQ(5, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 5";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";
    ASSERT_EQ(gui::listview::Direction::Top, testListView->direction) << "List Direction should be Top";

    moveNTimes(1, gui::listview::Direction::Bottom);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate bot by one - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(4, testListView->currentPageSize) << "4 elements should be displayed (10 - 6)";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 5";
    ASSERT_EQ(9, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";
    ASSERT_EQ(gui::listview::Direction::Bottom, testListView->direction) << "List Direction should be Bottom";

    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 6 has focus";
    moveNTimes(2, gui::listview::Direction::Bottom);
    ASSERT_FALSE(testListView->listBorderReached) << "Navigate bot by 2 - page should not change";
    ASSERT_EQ(8, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 8 has focus";
    ASSERT_EQ(4, testListView->currentPageSize) << "4 elements should be displayed (10 - 6)";
    ASSERT_NE(gui::listview::Direction::Top, testListView->direction) << "list direction not changed should be Top";

    ASSERT_EQ(8, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 8 has focus";

    moveNTimes(2, gui::listview::Direction::Top);
    ASSERT_FALSE(testListView->listBorderReached) << "Navigate top by 2 - page should not change";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 6 has focus";
    ASSERT_EQ(4, testListView->currentPageSize) << "4 elements should be displayed (10 - 6)";
    ASSERT_NE(gui::listview::Direction::Top, testListView->direction) << "list direction not changed should be Top";
}

TEST_F(ListViewTesting, Continuous_Type_Test)
{
    // set list type to Continuous
    testListView->setBoundaries(gui::Boundaries::Continuous);

    // 10 provider elements, 100 h each, list 600 -> 6 elements on page.
    testListView->provider->requestRecords(0, 10);

    moveNTimes(1, gui::listview::Direction::Top);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate top by one - page should change to last page";
    testListView->listBorderReached = false;
    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should be displayed";

    ASSERT_EQ(9, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 9";
    ASSERT_EQ(3, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 3 (9 - 6)";
    ASSERT_EQ(gui::listview::Direction::Top, testListView->direction) << "List Direction should be Top";

    moveNTimes(testListView->currentPageSize, gui::listview::Direction::Top);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate top by page size - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should be displayed";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 0";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 6";
    ASSERT_EQ(gui::listview::Direction::Bottom, testListView->direction) << "List Direction should be Bottom";

    moveNTimes(1, gui::listview::Direction::Bottom);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate bot by one - page should change";
    testListView->listBorderReached = false;
    ASSERT_EQ(4, testListView->currentPageSize) << "6 elements should be displayed";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 5";
    ASSERT_EQ(9, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";
    ASSERT_EQ(gui::listview::Direction::Bottom, testListView->direction) << "List Direction should be Bottom";

    moveNTimes(4, gui::listview::Direction::Bottom);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate bot by 4 - page should change to first page";
    testListView->listBorderReached = false;
    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should be displayed";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->children.front())->ID)
        << "First element ID should be 5";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->children.back())->ID)
        << "Last element ID should be 0";
    ASSERT_EQ(gui::listview::Direction::Bottom, testListView->direction) << "List Direction should be Bottom";
}

TEST_F(ListViewTesting, Data_Deletion_Test)
{
    // Internal data -> do not delete
    testProvider->dataSource = gui::TestListViewDataSource::Internal;
    testListView->provider->requestRecords(0, 10);

    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";

    // Save pointers to first and last elements
    auto pointerToFirst = dynamic_cast<gui::TestListItem *>(testListView->body->children.front());
    auto pointerToLast  = dynamic_cast<gui::TestListItem *>(testListView->body->children.back());

    // Clear list
    testListView->reset();
    ASSERT_EQ(0, testListView->body->children.size()) << "List should be empty";
    testProvider->refreshList();

    // Check if pointers match
    ASSERT_EQ(pointerToFirst, dynamic_cast<gui::TestListItem *>(testListView->body->children.front()))
        << "Pointer to first element should match";
    ASSERT_EQ(pointerToLast, dynamic_cast<gui::TestListItem *>(testListView->body->children.back()))
        << "Pointer to last element should match";

    // External data -> delete
    testProvider->dataSource = gui::TestListViewDataSource::External;
    testListView->provider->requestRecords(0, 10);

    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";

    // Save pointers to first and last elements
    pointerToFirst = dynamic_cast<gui::TestListItem *>(testListView->body->children.front());
    pointerToLast  = dynamic_cast<gui::TestListItem *>(testListView->body->children.back());

    // Clear list
    testListView->reset();
    ASSERT_EQ(0, testListView->body->children.size()) << "List should be empty";
    testProvider->refreshList();

    // Check if pointers don't match
    ASSERT_NE(pointerToFirst, dynamic_cast<gui::TestListItem *>(testListView->body->children.front()))
        << "Pointer to first element should match";
    ASSERT_NE(pointerToLast, dynamic_cast<gui::TestListItem *>(testListView->body->children.back()))
        << "Pointer to last element should match";
}

TEST_F(ListViewTesting, Rebuild_Type_Test)
{
    // Internal data -> do not delete
    testProvider->dataSource = gui::TestListViewDataSource::Internal;

    // Do full list rebuild
    testListView->rebuildList(gui::listview::RebuildType::Full);

    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";
    ASSERT_EQ(0, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 0 has focus";

    moveNTimes(3, gui::listview::Direction::Bottom);
    ASSERT_FALSE(testListView->listBorderReached) << "Navigate bot by 3 - page should not change";
    ASSERT_EQ(3, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 3 has focus";

    auto pointerToFocusedElement = dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem());

    // Do in place rebuild
    testListView->rebuildList(gui::listview::RebuildType::InPlace);

    // Check if focused item did not change
    ASSERT_EQ(3, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 3 has focus";
    ASSERT_EQ(pointerToFocusedElement, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem()))
        << "Focused item should be same";

    // Do on offset rebuild on 8 element
    testListView->rebuildList(gui::listview::RebuildType::OnOffset, 8);

    // Page should change and 2 elements should fit (10 - 8)
    ASSERT_EQ(2, testListView->currentPageSize) << "2 elements should fit into list.";
    ASSERT_EQ(8, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 8 has focus";

    moveNTimes(2, gui::listview::Direction::Top);
    ASSERT_TRUE(testListView->listBorderReached) << "Navigate Top by 3 - page should change";
    ASSERT_EQ(6, testListView->currentPageSize) << "6 elements should fit into list.";
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 5 has focus";

    pointerToFocusedElement = dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem());

    // Do in place rebuild
    testListView->rebuildList(gui::listview::RebuildType::InPlace);

    // Check if focused item did not change
    ASSERT_EQ(6, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 6 has focus";
    ASSERT_EQ(pointerToFocusedElement, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem()))
        << "Focused item should be same";

    // Do full list rebuild
    testListView->rebuildList(gui::listview::RebuildType::Full);

    // Do on page element rebuild on index in Page scope
    testListView->rebuildList(gui::listview::RebuildType::OnPageElement, 3);

    // Check if focused item did not change
    ASSERT_EQ(3, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 3 has focus";

    // Do on page element rebuild in Page scope
    testListView->rebuildList(gui::listview::RebuildType::OnPageElement, 1);

    // Check if focused item did not change
    ASSERT_EQ(1, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 1 has focus";

    // Do on page element rebuild on index outside Page scope. Page should change and proper element should be focused.
    testListView->rebuildList(gui::listview::RebuildType::OnPageElement, 8);

    // Check if focused item did not change
    ASSERT_EQ(8, dynamic_cast<gui::TestListItem *>(testListView->body->getFocusItem())->ID)
        << "Check if item 8 has focus";
}