File: treetest.cpp

package info (click to toggle)
kdenlive 25.12.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 125,912 kB
  • sloc: cpp: 206,648; xml: 11,857; python: 1,139; ansic: 1,054; javascript: 578; sh: 389; makefile: 15
file content (253 lines) | stat: -rw-r--r-- 10,587 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2022 Eric Jiang
    SPDX-FileCopyrightText: 2017-2019 Nicolas Carion <french.ebook.lover@gmail.com>
    SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "catch.hpp"
#include "test_utils.hpp"
// test specific headers
#include <QString>
#include <cmath>
#include <iostream>
#include <tuple>
#include <unordered_set>

#include "abstractmodel/abstracttreemodel.hpp"
#include "abstractmodel/treeitem.hpp"
#include "effects/effectlist/model/effecttreemodel.hpp"
#include "effects/effectlist/model/effectfilter.hpp"

TEST_CASE("Basic tree testing", "[TreeModel]")
{
    auto model = AbstractTreeModel::construct();

    REQUIRE(KdenliveTests::checkModelConsistency(model));
    REQUIRE(model->rowCount() == 0);

    SECTION("Item creation Test")
    {
        auto item = TreeItem::construct(QList<QVariant>{QStringLiteral("test")}, model, false);
        int id = item->getId();
        REQUIRE(item->depth() == 0);
        REQUIRE(KdenliveTests::checkModelConsistency(model));

        // check that a valid Id has been assigned
        REQUIRE(id != -1);

        // check that the item is not yet registered (not valid parent)
        REQUIRE(KdenliveTests::modelSize(model) == 1);

        // Assign this to a parent
        model->getRoot()->appendChild(item);
        REQUIRE(KdenliveTests::checkModelConsistency(model));
        // Now the item should be registered, we query it
        REQUIRE(KdenliveTests::modelSize(model) == 2);
        REQUIRE(model->getItemById(id) == item);
        REQUIRE(item->depth() == 1);
        REQUIRE(model->rowCount() == 1);
        REQUIRE(model->rowCount(model->getIndexFromItem(item)) == 0);

        // Retrieve data member
        REQUIRE(model->data(model->getIndexFromItem(item), 0) == QStringLiteral("test"));

        // Try joint creation / assignation
        auto item2 = item->appendChild(QList<QVariant>{QStringLiteral("test2")});
        auto state = [&]() {
            REQUIRE(KdenliveTests::checkModelConsistency(model));
            REQUIRE(item->depth() == 1);
            REQUIRE(item2->depth() == 2);
            REQUIRE(model->rowCount() == 1);
            REQUIRE(item->row() == 0);
            REQUIRE(item2->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item2), 0) == QStringLiteral("test2"));
            REQUIRE(model->rowCount(model->getIndexFromItem(item2)) == 0);
        };
        state();
        REQUIRE(model->rowCount(model->getIndexFromItem(item)) == 1);
        REQUIRE(KdenliveTests::modelSize(model) == 3);

        // Add a second child to item to check if everything collapses
        auto item3 = item->appendChild(QList<QVariant>{QStringLiteral("test3")});
        state();
        REQUIRE(model->rowCount(model->getIndexFromItem(item3)) == 0);
        REQUIRE(model->rowCount(model->getIndexFromItem(item)) == 2);
        REQUIRE(KdenliveTests::modelSize(model) == 4);
        REQUIRE(item3->depth() == 2);
        REQUIRE(item3->row() == 1);
        REQUIRE(model->data(model->getIndexFromItem(item3), 0) == QStringLiteral("test3"));
    }

    SECTION("Invalid moves")
    {
        auto item = model->getRoot()->appendChild(QList<QVariant>{QStringLiteral("test")});
        auto state = [&]() {
            REQUIRE(KdenliveTests::checkModelConsistency(model));
            REQUIRE(model->rowCount() == 1);
            REQUIRE(item->depth() == 1);
            REQUIRE(item->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item), 0) == QStringLiteral("test"));
        };
        state();
        REQUIRE(model->rowCount(model->getIndexFromItem(item)) == 0);

        // Try to move the root
        REQUIRE_FALSE(item->appendChild(model->getRoot()));
        state();
        REQUIRE(model->rowCount(model->getIndexFromItem(item)) == 0);

        auto item2 = item->appendChild(QList<QVariant>{QStringLiteral("test2")});
        auto item3 = item2->appendChild(QList<QVariant>{QStringLiteral("test3")});
        auto item4 = item3->appendChild(QList<QVariant>{QStringLiteral("test4")});
        auto state2 = [&]() {
            state();
            REQUIRE(item2->depth() == 2);
            REQUIRE(item2->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item2), 0) == QStringLiteral("test2"));
            REQUIRE(item3->depth() == 3);
            REQUIRE(item3->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item3), 0) == QStringLiteral("test3"));
            REQUIRE(item4->depth() == 4);
            REQUIRE(item4->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item4), 0) == QStringLiteral("test4"));
        };
        state2();

        // Try to make a loop
        REQUIRE_FALSE(item->changeParent(item3));
        state2();
        REQUIRE_FALSE(item->changeParent(item4));
        state2();

        // Try to append a child that already have a parent
        REQUIRE_FALSE(item->appendChild(item4));
        state2();

        // valid move
        REQUIRE(item4->changeParent(item2));
        REQUIRE(KdenliveTests::checkModelConsistency(model));
    }

    SECTION("Deregistration tests")
    {
        // we construct a non trivial structure
        auto item = model->getRoot()->appendChild(QList<QVariant>{QStringLiteral("test")});
        auto item2 = item->appendChild(QList<QVariant>{QStringLiteral("test2")});
        auto item3 = item2->appendChild(QList<QVariant>{QStringLiteral("test3")});
        auto item4 = item3->appendChild(QList<QVariant>{QStringLiteral("test4")});
        auto item5 = item2->appendChild(QList<QVariant>{QStringLiteral("test5")});
        auto state = [&]() {
            REQUIRE(KdenliveTests::checkModelConsistency(model));
            REQUIRE(model->rowCount() == 1);
            REQUIRE(item->depth() == 1);
            REQUIRE(item->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item), 0) == QStringLiteral("test"));
            REQUIRE(model->rowCount(model->getIndexFromItem(item)) == 1);
            REQUIRE(item2->depth() == 2);
            REQUIRE(item2->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item2), 0) == QStringLiteral("test2"));
            REQUIRE(model->rowCount(model->getIndexFromItem(item2)) == 2);
            REQUIRE(item3->depth() == 3);
            REQUIRE(item3->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item3), 0) == QStringLiteral("test3"));
            REQUIRE(model->rowCount(model->getIndexFromItem(item3)) == 1);
            REQUIRE(item4->depth() == 4);
            REQUIRE(item4->row() == 0);
            REQUIRE(model->data(model->getIndexFromItem(item4), 0) == QStringLiteral("test4"));
            REQUIRE(model->rowCount(model->getIndexFromItem(item4)) == 0);
            REQUIRE(item5->depth() == 3);
            REQUIRE(item5->row() == 1);
            REQUIRE(model->data(model->getIndexFromItem(item5), 0) == QStringLiteral("test5"));
            REQUIRE(model->rowCount(model->getIndexFromItem(item5)) == 0);
            REQUIRE(KdenliveTests::modelSize(model) == 6);
            REQUIRE(item->isInModel());
            REQUIRE(item2->isInModel());
            REQUIRE(item3->isInModel());
            REQUIRE(item4->isInModel());
            REQUIRE(item5->isInModel());
        };
        state();

        // deregister the topmost item, should also deregister its children
        item->changeParent(std::shared_ptr<TreeItem>());
        REQUIRE(KdenliveTests::modelSize(model) == 1);
        REQUIRE(model->rowCount() == 0);
        REQUIRE(!item->isInModel());
        REQUIRE(!item2->isInModel());
        REQUIRE(!item3->isInModel());
        REQUIRE(!item4->isInModel());
        REQUIRE(!item5->isInModel());

        // reinsert
        REQUIRE(model->getRoot()->appendChild(item));
        state();

        item2->removeChild(item5);
        REQUIRE(!item5->isInModel());
        REQUIRE(model->rowCount(model->getIndexFromItem(item2)) == 1);
        REQUIRE(KdenliveTests::modelSize(model) == 5);

        // reinsert
        REQUIRE(item5->changeParent(item2));
        state();
    }
}

// Tests the logic for matching the user-supplied search string against the list
// of items. The actual logic is in AssetFilter but since it's an abstract
// class, we test EffectFilter instead.
TEST_CASE("Effect filter text-matching logic")
{
    auto model = EffectTreeModel::construct("", nullptr);
    EffectFilter filter{nullptr};
    // rootData copied from effecttreemodel.cpp
    QList<QVariant> rootData{"Name", "ID", "Type", "isFav"};

    SECTION("Handles basic alphanum search")
    {
        auto item = TreeItem::construct(rootData, model, true);
        item->setData(AssetTreeModel::IdCol, "Hello World");
        item->setData(AssetTreeModel::NameCol, "This is K-denlive");

        filter.setFilterName(true, "wORL"); // codespell:ignore worl
        CHECK(KdenliveTests::effectFilterName(filter, item) == true);

        // should not search across spaces
        filter.setFilterName(true, "Thisis");
        CHECK(KdenliveTests::effectFilterName(filter, item) == false);

        // should ignore punctuation
        filter.setFilterName(true, "Kden");
        CHECK(KdenliveTests::effectFilterName(filter, item) == true);
    }

    // Tests for bug 432699 where filtering the effects list in Chinese fails
    // because the filter only works for ascii alphanum characters.
    SECTION("Handles Chinese/Japanese search")
    {
        auto item = TreeItem::construct(rootData, model, true);
        item->setData(AssetTreeModel::IdCol, "静音");
        item->setData(AssetTreeModel::NameCol, "ミュート");

        filter.setFilterName(true, "音");
        CHECK(KdenliveTests::effectFilterName(filter, item) == true);
        filter.setFilterName(true, "默");
        CHECK(KdenliveTests::effectFilterName(filter, item) == false);

        // should ignore punctuation
        filter.setFilterName(true, "ミュ");
        CHECK(KdenliveTests::effectFilterName(filter, item) == true);
    }

    SECTION("Ignores diacritics")
    {
        auto item = TreeItem::construct(rootData, model, true);
        item->setData(AssetTreeModel::IdCol, "rgb parade");
        item->setData(AssetTreeModel::NameCol, "Défilé RVB");

        // should be able to search with and without diacritics
        filter.setFilterName(true, "defile");
        CHECK(KdenliveTests::effectFilterName(filter, item) == true);
        filter.setFilterName(true, "ilé");
        CHECK(KdenliveTests::effectFilterName(filter, item) == true);
    }
}