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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2010-06-21
 * Description : Test for RG tag model.
 *
 * SPDX-FileCopyrightText: 2010 by Michael G. Hansen <mike at mghansen dot de>
 * SPDX-FileCopyrightText: 2010 by Gabriel Voicu <ping dot gabi at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "rgtagmodel_utest.h"

// Qt includes

#include <QUrl>

// Local includes

#include "digikam_debug.h"
#include "simpletreemodel.h"
#include "rgtagmodel.h"
#include "modeltest.h"

using namespace Digikam;

/**
 * @brief Dummy test that does nothing
 */
void TestRGTagModel::testNoOp()
{
}

/**
 * @brief Create an RGTagModel, but leave it empty
 */
void TestRGTagModel::testModelEmpty()
{
    SimpleTreeModel* const treeModel = new SimpleTreeModel(1, this);
    new ModelTest(treeModel, this);

    RGTagModel* const tagModel = new RGTagModel(treeModel, this);
    new ModelTest(tagModel, this);
}

void TestRGTagModel::testModel2()
{
    SimpleTreeModel* const treeModel = new SimpleTreeModel(1, this);
    new ModelTest(treeModel, this);

    // add some items before the tagModel is created:
    SimpleTreeModel::Item* const treeItem1 = treeModel->addItem();
    const QPersistentModelIndex treeItem1Index = treeModel->itemToIndex(treeItem1);
    SimpleTreeModel::Item* const treeItem11 = treeModel->addItem(treeItem1);<--- Variable 'treeItem11' can be declared as pointer to const
    const QPersistentModelIndex treeItem11Index = treeModel->itemToIndex(treeItem11);

    SimpleTreeModel::Item* const treeItem2 = treeModel->addItem();<--- Variable 'treeItem2' can be declared as pointer to const
    const QPersistentModelIndex treeItem2Index = treeModel->itemToIndex(treeItem2);

    RGTagModel* const tagModel = new RGTagModel(treeModel, this);
    // modeltest will be created at the end of this function to make sure it does not influence the result!

    // first, verify the row and column counts:
    QCOMPARE(tagModel->rowCount(QModelIndex()), 2);
    QCOMPARE(tagModel->columnCount(QModelIndex()), 1);

    // now test toSourceIndex:
    const QModelIndex tagItem1Index = tagModel->index(0, 0);
    Q_ASSERT(tagItem1Index.isValid());
    QCOMPARE(tagModel->rowCount(tagItem1Index), 1);
    QCOMPARE(tagModel->columnCount(tagItem1Index), 1);

    const QModelIndex tagItem1IndexSource = tagModel->toSourceIndex(tagItem1Index);
    Q_ASSERT(tagItem1IndexSource.isValid());
    Q_ASSERT(tagItem1IndexSource==treeItem1Index);

    const QModelIndex tagItem2Index = tagModel->index(1, 0);
    Q_ASSERT(tagItem2Index.isValid());
    QCOMPARE(tagModel->rowCount(tagItem2Index), 0);
    QCOMPARE(tagModel->columnCount(tagItem2Index), 1);

    const QModelIndex tagItem2IndexSource = tagModel->toSourceIndex(tagItem2Index);
    Q_ASSERT(tagItem2IndexSource.isValid());
    Q_ASSERT(tagItem2IndexSource==treeItem2Index);

    const QModelIndex tagItem11Index = tagModel->index(0, 0, tagItem1Index);
    Q_ASSERT(tagItem11Index.isValid());
    QCOMPARE(tagModel->rowCount(tagItem11Index), 0);
    QCOMPARE(tagModel->columnCount(tagItem11Index), 1);

    const QModelIndex tagItem11IndexSource = tagModel->toSourceIndex(tagItem11Index);
    Q_ASSERT(tagItem11IndexSource.isValid());
    Q_ASSERT(tagItem11IndexSource==treeItem11Index);

    // add modeltest as the last test:
    new ModelTest(tagModel, this);
}

void TestRGTagModel::testModel3()
{
    SimpleTreeModel* const treeModel = new SimpleTreeModel(1, this);
    new ModelTest(treeModel, this);

    // add some items before the tagModel is created:
    SimpleTreeModel::Item* const treeItem1 = treeModel->addItem();
    const QPersistentModelIndex treeItem1Index = treeModel->itemToIndex(treeItem1);
    SimpleTreeModel::Item* const treeItem11 = treeModel->addItem(treeItem1);<--- Variable 'treeItem11' can be declared as pointer to const
    const QPersistentModelIndex treeItem11Index = treeModel->itemToIndex(treeItem11);

    SimpleTreeModel::Item* const treeItem2 = treeModel->addItem();<--- Variable 'treeItem2' can be declared as pointer to const
    const QPersistentModelIndex treeItem2Index = treeModel->itemToIndex(treeItem2);

    RGTagModel* const tagModel = new RGTagModel(treeModel, this);
    // modeltest will be created at the end of this function to make sure it does not influence the result!

    // first, verify the row and column counts:
    QCOMPARE(tagModel->rowCount(QModelIndex()), 2);
    QCOMPARE(tagModel->columnCount(QModelIndex()), 1);

    // now test toSourceIndex:
    //const QModelIndex tagItem1Index = tagModel->index(0, 0);

    // now add a new item to the source model, before the existing item:
    SimpleTreeModel::Item* const treeItem11a = treeModel->addItem(treeItem1, 0);<--- Variable 'treeItem11a' can be declared as pointer to const
    const QPersistentModelIndex treeItem11aIndex = treeModel->itemToIndex(treeItem11a);
    QCOMPARE(treeItem11Index.row(), 1);
    QCOMPARE(treeItem11aIndex.row(), 0);

    // now add a new item to the source model, this time in the middle:
    SimpleTreeModel::Item* const treeItem11b = treeModel->addItem(treeItem1, 1);<--- Variable 'treeItem11b' can be declared as pointer to const
    const QPersistentModelIndex treeItem11bIndex = treeModel->itemToIndex(treeItem11b);
    QCOMPARE(treeItem11Index.row(), 2);
    QCOMPARE(treeItem11aIndex.row(), 0);
    QCOMPARE(treeItem11bIndex.row(), 1);

    // add modeltest as the last test:
    new ModelTest(tagModel, this);
}

void TestRGTagModel::testModel1()
{
    SimpleTreeModel* const treeModel = new SimpleTreeModel(1, this);
    new ModelTest(treeModel, this);

    // add some items before the tagModel is created:
    SimpleTreeModel::Item* const treeItem1 = treeModel->addItem();
    QPersistentModelIndex treeItem1Index = treeModel->itemToIndex(treeItem1);
    SimpleTreeModel::Item* const treeItem11 = treeModel->addItem(treeItem1);<--- Variable 'treeItem11' can be declared as pointer to const
    QPersistentModelIndex treeItem11Index = treeModel->itemToIndex(treeItem11);

    RGTagModel* const tagModel = new RGTagModel(treeModel, this);<--- Variable 'tagModel' can be declared as pointer to const
    // TODO: make sure the ModelTest does not find any errors, currently it does find errors ;-)
    //new ModelTest(tagModel, this);

    // simple tests
    Q_ASSERT(tagModel->rowCount()==treeModel->rowCount());

    const QPersistentModelIndex tagItem1Index = tagModel->fromSourceIndex(treeItem1Index);
    Q_ASSERT(tagItem1Index.isValid());
    qCDebug(DIGIKAM_TESTS_LOG)<<tagItem1Index;

    Q_ASSERT(tagModel->rowCount(tagItem1Index)==treeModel->rowCount(treeItem1Index));


    // make sure the tagModel handles items inserted after it was created
    // - both top level
    SimpleTreeModel::Item* const treeItem2 = treeModel->addItem();
    QPersistentModelIndex treeItem2Index = treeModel->itemToIndex(treeItem2);
    Q_ASSERT(tagModel->rowCount()==treeModel->rowCount());
    const QPersistentModelIndex tagItem2Index = tagModel->fromSourceIndex(treeItem2Index);

    // - and sub items:
    SimpleTreeModel::Item* const treeItem21 = treeModel->addItem(treeItem2);<--- Variable 'treeItem21' can be declared as pointer to const
    Q_ASSERT(tagItem2Index.isValid());

    Q_ASSERT(tagModel->rowCount(tagItem2Index)==treeModel->rowCount(treeItem2Index));

    const QPersistentModelIndex tagItem11Index = tagModel->fromSourceIndex(treeItem11Index);
    Q_ASSERT(tagItem11Index.isValid());

    QPersistentModelIndex treeItem21Index = treeModel->itemToIndex(treeItem21);
    const QPersistentModelIndex tagItem21Index = tagModel->fromSourceIndex(treeItem21Index);
    Q_ASSERT(tagItem21Index.isValid());

    // now make sure we can descend:
    const QModelIndex ti1 = tagModel->index(0, 0);
    Q_ASSERT(ti1.isValid());
    Q_ASSERT(ti1 == tagItem1Index);

    // descends level 1 row 0
    const QModelIndex ti11 = tagModel->index(0, 0, ti1);
    Q_ASSERT(ti11.isValid());
    Q_ASSERT(ti11 == tagItem11Index);

    qCDebug(DIGIKAM_TESTS_LOG)<<"----------------------_";

    // descends level 0 row 1
    const QModelIndex ti2 = tagModel->index(1, 0);
    Q_ASSERT(ti2.isValid());
    Q_ASSERT(ti2 == tagItem2Index);

    // descends level 1 row 0
    QModelIndex ti21 = tagModel->index(0, 0, ti2);
    Q_ASSERT(ti21.isValid());
    Q_ASSERT(ti21 == tagItem21Index);

    //checks invalid index
    const QModelIndex ti111 = tagModel->index(0, 0, ti11);
    Q_ASSERT(!ti111.isValid());

    //checks parent of tagItem1
    const QModelIndex parent_ti1 = tagModel->parent(ti1);
    Q_ASSERT(!parent_ti1.isValid());

    //checks parent of tagItem11
    const QModelIndex parent_ti11 = tagModel->parent(ti11);
    Q_ASSERT(parent_ti11 == tagItem1Index);

    //checks parent of tagItem2
    const QModelIndex parent_ti2 = tagModel->parent(ti2);
    Q_ASSERT(!parent_ti2.isValid());

    const QModelIndex parent_ti21 = tagModel->parent(ti21);
    Q_ASSERT(parent_ti21.isValid());
}

void TestRGTagModel::testModelSpacerTags()
{
    SimpleTreeModel* const treeModel = new SimpleTreeModel(1, this);
    new ModelTest(treeModel, this);

    // add some items before the tagModel is created:
    SimpleTreeModel::Item* const treeItem1 = treeModel->addItem();
    QPersistentModelIndex treeItem1Index = treeModel->itemToIndex(treeItem1);
    treeItem1->data = QLatin1String("oldChildren");

    SimpleTreeModel::Item* const treeItem11 = treeModel->addItem(treeItem1);<--- Variable 'treeItem11' can be declared as pointer to const
    QPersistentModelIndex treeItem11Index = treeModel->itemToIndex(treeItem11);

    RGTagModel* const tagModel = new RGTagModel(treeModel, this);
    // TODO: make sure the ModelTest does not find any errors, currently it does find errors ;-)
    new ModelTest(tagModel, this);

    const QPersistentModelIndex tagItem11Index = tagModel->fromSourceIndex(treeItem11Index);
    Q_ASSERT(tagItem11Index.isValid());

    qCDebug(DIGIKAM_TESTS_LOG)<<"Worked before adding spacers";

    //insert spacer below ti21
    tagModel->addSpacerTag(QModelIndex(), QLatin1String("{Country}"));
    tagModel->addNewTag(QModelIndex(), QLatin1String("New Tag"), QString());

    qCDebug(DIGIKAM_TESTS_LOG)<<"Added the spacers.";

    const QModelIndex index11 = tagModel->index(0, 0);
    const QModelIndex index12 = tagModel->index(1, 0);
    const QModelIndex index13 = tagModel->index(2, 0);

    qCDebug(DIGIKAM_TESTS_LOG)<<tagModel->data(index11, Qt::DisplayRole);
    qCDebug(DIGIKAM_TESTS_LOG)<<tagModel->data(index12, Qt::DisplayRole);
    qCDebug(DIGIKAM_TESTS_LOG)<<tagModel->data(index13, Qt::DisplayRole);
//  qCDebug(DIGIKAM_TESTS_LOG)<<tagModel->data(2, 0, QModelIndex());

/*
    qCDebug(DIGIKAM_TESTS_LOG)<<"VERIFY IF NEW TAG EXISTS:";
    QModelIndex ti211Spacer = tagModel->index(0, 0, ti21);
    Q_ASSERT(ti211Spacer.isValid());
*/

}

QTEST_GUILESS_MAIN(TestRGTagModel)

#include "moc_rgtagmodel_utest.cpp"