~aleteoryx/muditaos

muditaos/module-gui/test/test-google/test-gui-dom-dump.cpp -rw-r--r-- 1.9 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
// 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 "gtest/gtest.h"

#include "gui/dom/Item2JsonSerializer.hpp"

#include "Item.hpp"
#include "Label.hpp"
#include "Text.hpp"
#include "FontManager.hpp"

#include <purefs/filesystem_paths.hpp>

class Item2JsonSerializerTester : public ::testing::Test
{
  protected:
    static constexpr auto testTextValue1 = "Some text 1";
    static constexpr auto testTextValue2 = "Some text 2";
    Item2JsonSerializerTester()
    {
        auto &fm = gui::FontManager::getInstance();
        fm.init(purefs::dir::getAssetsDirPath());

        auto text = new gui::Text(nullptr, 0, 0, 0, 0);
        text->setText(testTextValue1);

        root.addWidget(text);
        root.addWidget(new gui::Label(nullptr, 0, 0, 0, 0, testTextValue2));

        serializer.traverse(root);
        std::stringstream ss;
        serializer.dump(ss);
        serializedItem = ss.str();
    }
    gui::Item2JsonSerializer serializer;

  public:
    gui::Item root;
    std::string serializedItem;
};

TEST_F(Item2JsonSerializerTester, ChildrenCountTest)
{
    constexpr auto expectedRootChildrenCountPhrase  = "\"ChildrenCount\": 2";
    constexpr auto expectedChildChildrenCountPhrase = "\"ChildrenCount\": 0";

    const auto rootPhrasePos = serializedItem.find(expectedRootChildrenCountPhrase);
    ASSERT_NE(rootPhrasePos, std::string::npos);

    const auto firstChildPhrasePos = serializedItem.find(expectedChildChildrenCountPhrase);
    ASSERT_NE(rootPhrasePos, std::string::npos);

    const auto secondChildPhrasePos = serializedItem.find(expectedChildChildrenCountPhrase, firstChildPhrasePos);
    ASSERT_NE(secondChildPhrasePos, std::string::npos);
}

TEST_F(Item2JsonSerializerTester, TextValueTest)
{
    ASSERT_NE(serializedItem.find(testTextValue1), std::string::npos);
    ASSERT_NE(serializedItem.find(testTextValue2), std::string::npos);
}