summaryrefslogtreecommitdiff
path: root/tests/auto/qml/qmldesigner/wizard/stylemodel-test.cpp
blob: 72cf7c135f5b7f6fea93ea89ce738808fc8cf403 (plain)
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "test-utilities.h"
#include "stylemodel.h"

using namespace StudioWelcome;

namespace {

std::unique_ptr<QStandardItemModel> createBackendModel(const QList<QString> &itemNames)
{
    auto model = std::make_unique<QStandardItemModel>();

    for (const QString &name : itemNames)
        model->appendRow(new QStandardItem{name});

    return model;
}

QList<QString> displayValues(const QAbstractListModel *model)
{
    QList<QString> rows;
    for (int rowIndex = 0; rowIndex < model->rowCount(); ++rowIndex) {
        auto data = model->data(model->index(rowIndex, 0), Qt::DisplayRole);
        rows.push_back(data.value<QString>());
    }
    return rows;
}

} // namespace

class QdsStyleModel : public ::testing::Test
{
protected:
    StyleModel *aStyleModel(const QStringList &items)
    {
        m_styleModel = std::make_unique<StyleModel>();
        m_backendModel = createBackendModel(items);
        m_styleModel->setBackendModel(m_backendModel.get());
        return m_styleModel.get();
    }

private:
    std::unique_ptr<StyleModel> m_styleModel;
    std::unique_ptr<QStandardItemModel> m_backendModel;
};

/******************* TESTS *******************/

TEST_F(QdsStyleModel, emptyFilterReturnsAllItems)
{
    auto model = aStyleModel({"a", "b", "c", "d"});

    model->filter("");

    ASSERT_THAT(displayValues(model), ElementsAreArray({"a", "b", "c", "d"}));
}

TEST_F(QdsStyleModel, unknownFilterReturnsEmpty)
{
    auto model = aStyleModel({"a", "b", "c", "d"});

    model->filter("Unknown Filter");

    ASSERT_THAT(displayValues(model), IsEmpty());
}

TEST_F(QdsStyleModel, filterAllOnEmptyList)
{
    auto model = aStyleModel({});

    model->filter("all");

    ASSERT_THAT(displayValues(model), IsEmpty());
}

TEST_F(QdsStyleModel, filterLightOnEmptyList)
{
    auto model = aStyleModel({});

    model->filter("light");

    ASSERT_THAT(displayValues(model), IsEmpty());
}

TEST_F(QdsStyleModel, filterDarkOnEmptyList)
{
    auto model = aStyleModel({});

    model->filter("dark");

    ASSERT_THAT(displayValues(model), IsEmpty());
}

TEST_F(QdsStyleModel, filterAll)
{
    auto model = aStyleModel({"item a", "b", "item c", "item-d", "e"});

    model->filter("all");

    ASSERT_THAT(displayValues(model), ElementsAreArray({"item a", "b", "item c", "item-d", "e"}));
}

TEST_F(QdsStyleModel, filterLight)
{
    auto model = aStyleModel({"Fusion", "Material Light", "Material Dark", "Universal Light"});

    model->filter("light");

    ASSERT_THAT(displayValues(model), ElementsAreArray({"Material Light", "Universal Light"}));
}

TEST_F(QdsStyleModel, filterDark)
{
    auto model = aStyleModel({"Fusion", "Material Light", "Material Dark", "Universal Light"});

    model->filter("dark");

    ASSERT_THAT(displayValues(model), ElementsAreArray({"Material Dark"}));
}

TEST_F(QdsStyleModel, filterLightCaseInsensitive)
{
    auto model = aStyleModel({"Fusion", "Material Light", "Material Dark", "Universal Light"});

    model->filter("LIGHT");

    ASSERT_THAT(displayValues(model), ElementsAreArray({"Material Light", "Universal Light"}));
}

TEST_F(QdsStyleModel, filterDarkCaseInsensitive)
{
    auto model = aStyleModel({"Fusion", "Material Light", "Material Dark", "Universal Light"});

    model->filter("DARK");

    ASSERT_THAT(displayValues(model), ElementsAreArray({"Material Dark"}));
}