File indexing completed on 2025-01-05 03:58:09

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-06-21
0007  * Description : Test for SimpleTreeModel.
0008  *
0009  * SPDX-FileCopyrightText: 2010      by Michael G. Hansen <mike at mghansen dot de>
0010  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "simpletreemodel_utest.h"
0017 
0018 // Qt includes
0019 
0020 #include <QUrl>
0021 
0022 // local includes
0023 
0024 #include "digikam_debug.h"
0025 #include "simpletreemodel.h"
0026 #include "modeltest.h"
0027 
0028 using namespace Digikam;
0029 
0030 /**
0031  * Description : Dummy test that does nothing
0032  */
0033 void TestSimpleTreeModel::testNoOp()
0034 {
0035 }
0036 
0037 void TestSimpleTreeModel::testModel1()
0038 {
0039     SimpleTreeModel* const treeModel       = new SimpleTreeModel(1, this);
0040 
0041     new ModelTest(treeModel, this);
0042 
0043     Q_ASSERT(!treeModel->index(0, 0).isValid());
0044     Q_ASSERT(treeModel->indexToItem(QModelIndex()) == treeModel->rootItem());
0045     Q_ASSERT(!treeModel->itemToIndex(treeModel->rootItem()).isValid());
0046     Q_ASSERT(!treeModel->itemToIndex(nullptr).isValid());
0047     Q_ASSERT(!treeModel->parent(QModelIndex()).isValid());
0048 
0049     // ---
0050 
0051     SimpleTreeModel::Item* const item1     = treeModel->addItem();
0052 
0053     Q_ASSERT(item1                              != nullptr);
0054 
0055     const QPersistentModelIndex item1Index = treeModel->itemToIndex(item1);
0056 
0057     Q_ASSERT(item1Index.isValid());
0058     Q_ASSERT(treeModel->indexToItem(item1Index) == item1);
0059     Q_ASSERT(!treeModel->parent(item1Index).isValid());
0060 
0061     // ---
0062 
0063     SimpleTreeModel::Item* const item2      = treeModel->addItem();
0064 
0065     Q_ASSERT(item2                              != nullptr);
0066 
0067     const QModelIndex item2Index            = treeModel->itemToIndex(item2);
0068 
0069     Q_ASSERT(item2Index.isValid());
0070     Q_ASSERT(treeModel->indexToItem(item2Index) == item2);
0071     Q_ASSERT(!treeModel->parent(item2Index).isValid());
0072 
0073     // ---
0074 
0075     SimpleTreeModel::Item* const item21      = treeModel->addItem(item2);
0076 
0077     Q_ASSERT(item21                              != nullptr);
0078 
0079     const QModelIndex item21Index            = treeModel->itemToIndex(item21);
0080 
0081     Q_ASSERT(item21Index.isValid());
0082     Q_ASSERT(treeModel->indexToItem(item21Index) == item21);
0083     Q_ASSERT(treeModel->parent(item21Index)      == item2Index);
0084     Q_ASSERT(treeModel->index(0, 0, item2Index)  == item21Index);
0085 
0086     // just make sure another modeltest will test things for consistency in case a signal went missing
0087 
0088     new ModelTest(treeModel, this);
0089 
0090     Q_ASSERT(treeModel->rootItem()                                 == treeModel->indexToItem(QModelIndex()));
0091     Q_ASSERT(treeModel->indexToItem(treeModel->itemToIndex(item1)) == item1);
0092     Q_ASSERT(treeModel->hasIndex(0, 0)                             == true);
0093 
0094     QModelIndex topIndex                     = treeModel->index(0, 0, QModelIndex());
0095 
0096     if (treeModel->rowCount(topIndex) > 0)
0097     {
0098         QModelIndex childIndex = treeModel->index(0, 0, topIndex);
0099         qCDebug(DIGIKAM_TESTS_LOG) << childIndex;
0100         qCDebug(DIGIKAM_TESTS_LOG) << treeModel->parent(childIndex);
0101 
0102         Q_ASSERT(treeModel->parent(childIndex) == topIndex);
0103     }
0104 
0105     // add another few items
0106 
0107     {
0108         SimpleTreeModel::Item* const item22 = treeModel->addItem(item2, 0);
0109 
0110         Q_ASSERT(item22                              != nullptr);
0111 
0112         const QModelIndex item22Index       = treeModel->itemToIndex(item22);
0113 
0114         Q_ASSERT(item22Index.isValid());
0115         Q_ASSERT(treeModel->indexToItem(item22Index) == item22);
0116         Q_ASSERT(treeModel->parent(item22Index)      == item2Index);
0117         Q_ASSERT(treeModel->index(0, 0, item2Index)  == item22Index);
0118         Q_ASSERT(item22Index.row()                   == 0);
0119     }
0120 
0121     // add another few items
0122 
0123     {
0124         SimpleTreeModel::Item* const item23 = treeModel->addItem(item2, 1);
0125 
0126         Q_ASSERT(item23                              != nullptr);
0127 
0128         const QModelIndex item23Index       = treeModel->itemToIndex(item23);
0129 
0130         Q_ASSERT(item23Index.isValid());
0131         Q_ASSERT(treeModel->indexToItem(item23Index) == item23);
0132         Q_ASSERT(treeModel->parent(item23Index)      == item2Index);
0133         Q_ASSERT(treeModel->index(1, 0, item2Index)  == item23Index);
0134         Q_ASSERT(item23Index.row()                   == 1);
0135     }
0136 
0137     new ModelTest(treeModel, this);
0138 }
0139 
0140 QTEST_GUILESS_MAIN(TestSimpleTreeModel)
0141 
0142 #include "moc_simpletreemodel_utest.cpp"