File indexing completed on 2024-05-12 17:09:48

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "../historymodel.h"
0008 #include "../historyimageitem.h"
0009 #include "../historystringitem.h"
0010 #include "../historyurlitem.h"
0011 
0012 #include <QAbstractItemModelTester>
0013 #include <QtTest>
0014 
0015 class HistoryModelTest : public QObject
0016 {
0017     Q_OBJECT
0018 private Q_SLOTS:
0019     void testSetMaxSize();
0020     void testInsertRemove();
0021     void testClear();
0022     void testIndexOf();
0023     void testType_data();
0024     void testType();
0025 };
0026 
0027 void HistoryModelTest::testSetMaxSize()
0028 {
0029     std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
0030     std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
0031 
0032     QCOMPARE(history->rowCount(), 0);
0033     QCOMPARE(history->maxSize(), 0);
0034 
0035     // insert an item - should still be empty
0036     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo"))));
0037     QCOMPARE(history->rowCount(), 0);
0038 
0039     // now it should insert again
0040     history->setMaxSize(1);
0041     QCOMPARE(history->maxSize(), 1);
0042     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo"))));
0043     QCOMPARE(history->rowCount(), 1);
0044 
0045     // insert another item, foo should get removed
0046     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("bar"))));
0047     QCOMPARE(history->rowCount(), 1);
0048     QCOMPARE(history->data(history->index(0, 0)).toString(), QLatin1String("bar"));
0049 
0050     // I don't trust the model, add more items
0051     history->setMaxSize(10);
0052     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo"))));
0053     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foobar"))));
0054     QCOMPARE(history->rowCount(), 3);
0055     QCOMPARE(history->data(history->index(0, 0)).toString(), QLatin1String("foobar"));
0056     QCOMPARE(history->data(history->index(1, 0)).toString(), QLatin1String("foo"));
0057     QCOMPARE(history->data(history->index(2, 0)).toString(), QLatin1String("bar"));
0058 
0059     // setting to 0 should clear again
0060     history->setMaxSize(0);
0061     QCOMPARE(history->maxSize(), 0);
0062     QCOMPARE(history->rowCount(), 0);
0063 }
0064 
0065 void HistoryModelTest::testInsertRemove()
0066 {
0067     std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
0068     std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
0069     history->setMaxSize(10);
0070     QCOMPARE(history->rowCount(), 0);
0071 
0072     const QString fooText = QStringLiteral("foo");
0073     const QString barText = QStringLiteral("bar");
0074     const QString fooBarText = QStringLiteral("foobar");
0075     const QByteArray fooUuid = QCryptographicHash::hash(fooText.toUtf8(), QCryptographicHash::Sha1);
0076     const QByteArray barUuid = QCryptographicHash::hash(barText.toUtf8(), QCryptographicHash::Sha1);
0077     const QByteArray foobarUuid = QCryptographicHash::hash(fooBarText.toUtf8(), QCryptographicHash::Sha1);
0078 
0079     // let's insert a few items
0080     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooText)));
0081     QModelIndex index = history->index(0, 0);
0082     QCOMPARE(index.data().toString(), fooText);
0083     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
0084     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
0085 
0086     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText)));
0087     QCOMPARE(index.data().toString(), barText);
0088     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
0089     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
0090     index = history->indexOf(fooUuid);
0091     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
0092     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
0093 
0094     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooBarText)));
0095     QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText);
0096     index = history->index(0, 0);
0097     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
0098     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
0099     index = history->indexOf(fooUuid);
0100     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
0101     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
0102     index = history->indexOf(barUuid);
0103     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
0104     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
0105 
0106     // insert one again - it should be moved to top
0107     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText)));
0108     QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
0109     index = history->index(0, 0);
0110     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
0111     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
0112     index = history->indexOf(fooUuid);
0113     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
0114     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
0115     index = history->indexOf(foobarUuid);
0116     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
0117     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
0118 
0119     // move one to top using the slot
0120     // already on top, shouldn't change anything
0121     history->moveToTop(barUuid);
0122     QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
0123     index = history->index(0, 0);
0124     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
0125     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
0126     index = history->indexOf(fooUuid);
0127     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
0128     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
0129     index = history->indexOf(foobarUuid);
0130     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
0131     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
0132 
0133     // another one should change, though
0134     history->moveToTop(foobarUuid);
0135     QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText);
0136     index = history->index(0, 0);
0137     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
0138     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
0139     index = history->indexOf(fooUuid);
0140     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), foobarUuid);
0141     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
0142     index = history->indexOf(barUuid);
0143     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
0144     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), foobarUuid);
0145 
0146     // remove them again
0147     QVERIFY(history->remove(foobarUuid));
0148     QCOMPARE(history->data(history->index(0, 0)).toString(), barText);
0149     index = history->index(0, 0);
0150     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), fooUuid);
0151     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), fooUuid);
0152     index = history->indexOf(fooUuid);
0153     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), barUuid);
0154     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), barUuid);
0155 
0156     QVERIFY(history->remove(barUuid));
0157     QCOMPARE(history->data(history->index(0, 0)).toString(), fooText);
0158     index = history->index(0, 0);
0159     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->next_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
0160     QCOMPARE(index.data(HistoryModel::HistoryItemConstPtrRole).value<HistoryItemConstPtr>()->previous_uuid(), index.data(HistoryModel::UuidRole).toByteArray());
0161 
0162     QVERIFY(history->remove(fooUuid));
0163     QCOMPARE(history->rowCount(), 0);
0164 }
0165 
0166 void HistoryModelTest::testClear()
0167 {
0168     std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
0169     std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
0170     history->setMaxSize(10);
0171     QCOMPARE(history->rowCount(), 0);
0172 
0173     history->clear();
0174     QCOMPARE(history->rowCount(), 0);
0175 
0176     // insert some items
0177     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo"))));
0178     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("bar"))));
0179     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foobar"))));
0180     history->moveToTop(QCryptographicHash::hash(QByteArrayLiteral("bar"), QCryptographicHash::Sha1));
0181     QCOMPARE(history->rowCount(), 3);
0182 
0183     // and clear
0184     history->clear();
0185     QCOMPARE(history->rowCount(), 0);
0186 }
0187 
0188 void HistoryModelTest::testIndexOf()
0189 {
0190     std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
0191     std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
0192     history->setMaxSize(10);
0193     QCOMPARE(history->rowCount(), 0);
0194     QVERIFY(!history->indexOf(QByteArrayLiteral("whatever")).isValid());
0195     QVERIFY(!history->indexOf(QByteArray()).isValid());
0196 
0197     // insert some items
0198     history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo"))));
0199     QVERIFY(!history->indexOf(QByteArrayLiteral("whatever")).isValid());
0200     QVERIFY(!history->indexOf(QByteArray()).isValid());
0201     const QByteArray fooUuid = QCryptographicHash::hash(QByteArrayLiteral("foo"), QCryptographicHash::Sha1);
0202     QVERIFY(history->indexOf(fooUuid).isValid());
0203     QCOMPARE(history->indexOf(fooUuid).data(HistoryModel::UuidRole).toByteArray(), fooUuid);
0204 
0205     history->clear();
0206     QVERIFY(!history->indexOf(fooUuid).isValid());
0207 }
0208 
0209 void HistoryModelTest::testType_data()
0210 {
0211     QTest::addColumn<HistoryItem *>("item");
0212     QTest::addColumn<HistoryItemType>("expectedType");
0213 
0214     HistoryItem *item = new HistoryStringItem(QStringLiteral("foo"));
0215     QTest::newRow("text") << item << HistoryItemType::Text;
0216     item = new HistoryImageItem(QImage());
0217     QTest::newRow("image") << item << HistoryItemType::Image;
0218     item = new HistoryURLItem(QList<QUrl>(), KUrlMimeData::MetaDataMap(), false);
0219     QTest::newRow("url") << item << HistoryItemType::Url;
0220 }
0221 
0222 void HistoryModelTest::testType()
0223 {
0224     std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
0225     std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
0226     history->setMaxSize(10);
0227     QCOMPARE(history->rowCount(), 0);
0228 
0229     QFETCH(HistoryItem *, item);
0230     QFETCH(HistoryItemType, expectedType);
0231     history->insert(QSharedPointer<HistoryItem>(item));
0232     QCOMPARE(history->index(0).data(HistoryModel::TypeRole).value<HistoryItemType>(), expectedType);
0233 }
0234 
0235 QTEST_MAIN(HistoryModelTest)
0236 #include "historymodeltest.moc"