You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
8.9 KiB
202 lines
8.9 KiB
/******************************************************************** |
|
This file is part of the KDE project. |
|
|
|
Copyright (C) 2014 Martin Gräßlin <mgraesslin@kde.org> |
|
|
|
This program is free software; you can redistribute it and/or modify |
|
it under the terms of the GNU General Public License as published by |
|
the Free Software Foundation; either version 2 of the License, or |
|
(at your option) any later version. |
|
|
|
This program is distributed in the hope that it will be useful, |
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
GNU General Public License for more details. |
|
|
|
You should have received a copy of the GNU General Public License |
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*********************************************************************/ |
|
#include "modeltest.h" |
|
#include "../historymodel.h" |
|
#include "../historystringitem.h" |
|
|
|
#include <QtTest> |
|
Q_DECLARE_METATYPE(HistoryItem*) |
|
|
|
class HistoryModelTest : public QObject |
|
{ |
|
Q_OBJECT |
|
private Q_SLOTS: |
|
void testSetMaxSize(); |
|
void testInsertRemove(); |
|
void testClear(); |
|
void testIndexOf(); |
|
}; |
|
|
|
void HistoryModelTest::testSetMaxSize() |
|
{ |
|
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr)); |
|
QScopedPointer<ModelTest> modelTest(new ModelTest(history.data())); |
|
|
|
QCOMPARE(history->rowCount(), 0); |
|
QCOMPARE(history->maxSize(), 0); |
|
|
|
// insert an item - should still be empty |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo")))); |
|
QCOMPARE(history->rowCount(), 0); |
|
|
|
// now it should insert again |
|
history->setMaxSize(1); |
|
QCOMPARE(history->maxSize(), 1); |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo")))); |
|
QCOMPARE(history->rowCount(), 1); |
|
|
|
// insert another item, foo should get removed |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("bar")))); |
|
QCOMPARE(history->rowCount(), 1); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), QLatin1String("bar")); |
|
|
|
// I don't trust the model, add more items |
|
history->setMaxSize(10); |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo")))); |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foobar")))); |
|
QCOMPARE(history->rowCount(), 3); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), QLatin1String("foobar")); |
|
QCOMPARE(history->data(history->index(1, 0)).toString(), QLatin1String("foo")); |
|
QCOMPARE(history->data(history->index(2, 0)).toString(), QLatin1String("bar")); |
|
|
|
// setting to 0 should clear again |
|
history->setMaxSize(0); |
|
QCOMPARE(history->maxSize(), 0); |
|
QCOMPARE(history->rowCount(), 0); |
|
} |
|
|
|
void HistoryModelTest::testInsertRemove() |
|
{ |
|
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr)); |
|
QScopedPointer<ModelTest> modelTest(new ModelTest(history.data())); |
|
history->setMaxSize(10); |
|
QCOMPARE(history->rowCount(), 0); |
|
|
|
const QString fooText = QStringLiteral("foo"); |
|
const QString barText = QStringLiteral("bar"); |
|
const QString fooBarText = QStringLiteral("foobar"); |
|
const QByteArray fooUuid = QCryptographicHash::hash(fooText.toUtf8(), QCryptographicHash::Sha1); |
|
const QByteArray barUuid = QCryptographicHash::hash(barText.toUtf8(), QCryptographicHash::Sha1); |
|
const QByteArray foobarUuid = QCryptographicHash::hash(fooBarText.toUtf8(), QCryptographicHash::Sha1); |
|
|
|
// let's insert a few items |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooText))); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), fooText); |
|
// QCOMPARE(history->first()->next_uuid(), history->first()->uuid()); |
|
// QCOMPARE(history->first()->previous_uuid(), history->first()->uuid()); |
|
|
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText))); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); |
|
// QCOMPARE(history->first()->next_uuid(), fooUuid); |
|
// QCOMPARE(history->first()->previous_uuid(), fooUuid); |
|
// QCOMPARE(history->find(fooUuid)->next_uuid(), barUuid); |
|
// QCOMPARE(history->find(fooUuid)->previous_uuid(), barUuid); |
|
|
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(fooBarText))); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText); |
|
// QCOMPARE(history->first()->next_uuid(), barUuid); |
|
// QCOMPARE(history->first()->previous_uuid(), fooUuid); |
|
// QCOMPARE(history->find(fooUuid)->next_uuid(), foobarUuid); |
|
// QCOMPARE(history->find(fooUuid)->previous_uuid(), barUuid); |
|
// QCOMPARE(history->find(barUuid)->next_uuid(), fooUuid); |
|
// QCOMPARE(history->find(barUuid)->previous_uuid(), foobarUuid); |
|
|
|
// insert one again - it should be moved to top |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(barText))); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); |
|
// QCOMPARE(history->first()->next_uuid(), foobarUuid); |
|
// QCOMPARE(history->first()->previous_uuid(), fooUuid); |
|
// QCOMPARE(history->find(fooUuid)->next_uuid(), barUuid); |
|
// QCOMPARE(history->find(fooUuid)->previous_uuid(), foobarUuid); |
|
// QCOMPARE(history->find(foobarUuid)->next_uuid(), fooUuid); |
|
// QCOMPARE(history->find(foobarUuid)->previous_uuid(), barUuid); |
|
|
|
// move one to top using the slot |
|
// already on top, shouldn't change anything |
|
history->moveToTop(barUuid); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); |
|
// QCOMPARE(history->first()->next_uuid(), foobarUuid); |
|
// QCOMPARE(history->first()->previous_uuid(), fooUuid); |
|
// QCOMPARE(history->find(fooUuid)->next_uuid(), barUuid); |
|
// QCOMPARE(history->find(fooUuid)->previous_uuid(), foobarUuid); |
|
// QCOMPARE(history->find(foobarUuid)->next_uuid(), fooUuid); |
|
// QCOMPARE(history->find(foobarUuid)->previous_uuid(), barUuid); |
|
|
|
// another one should change, though |
|
history->moveToTop(foobarUuid); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), fooBarText); |
|
// QCOMPARE(history->first()->next_uuid(), barUuid); |
|
// QCOMPARE(history->first()->previous_uuid(), fooUuid); |
|
// QCOMPARE(history->find(fooUuid)->next_uuid(), foobarUuid); |
|
// QCOMPARE(history->find(fooUuid)->previous_uuid(), barUuid); |
|
// QCOMPARE(history->find(barUuid)->next_uuid(), fooUuid); |
|
// QCOMPARE(history->find(barUuid)->previous_uuid(), foobarUuid); |
|
|
|
// remove them again |
|
QVERIFY(history->remove(foobarUuid)); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), barText); |
|
// QCOMPARE(history->first()->next_uuid(), fooUuid); |
|
// QCOMPARE(history->first()->previous_uuid(), fooUuid); |
|
// QCOMPARE(history->find(fooUuid)->next_uuid(), barUuid); |
|
// QCOMPARE(history->find(fooUuid)->previous_uuid(), barUuid); |
|
|
|
QVERIFY(history->remove(barUuid)); |
|
QCOMPARE(history->data(history->index(0, 0)).toString(), fooText); |
|
// QCOMPARE(history->first()->next_uuid(), history->first()->uuid()); |
|
// QCOMPARE(history->first()->previous_uuid(), history->first()->uuid()); |
|
|
|
QVERIFY(history->remove(fooUuid)); |
|
QCOMPARE(history->rowCount(), 0); |
|
} |
|
|
|
void HistoryModelTest::testClear() |
|
{ |
|
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr)); |
|
QScopedPointer<ModelTest> modelTest(new ModelTest(history.data())); |
|
history->setMaxSize(10); |
|
QCOMPARE(history->rowCount(), 0); |
|
|
|
history->clear(); |
|
QCOMPARE(history->rowCount(), 0); |
|
|
|
// insert some items |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo")))); |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("bar")))); |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foobar")))); |
|
history->moveToTop(QCryptographicHash::hash(QByteArrayLiteral("bar"), QCryptographicHash::Sha1)); |
|
QCOMPARE(history->rowCount(), 3); |
|
|
|
// and clear |
|
history->clear(); |
|
QCOMPARE(history->rowCount(), 0); |
|
} |
|
|
|
void HistoryModelTest::testIndexOf() |
|
{ |
|
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr)); |
|
QScopedPointer<ModelTest> modelTest(new ModelTest(history.data())); |
|
history->setMaxSize(10); |
|
QCOMPARE(history->rowCount(), 0); |
|
QVERIFY(!history->indexOf(QByteArrayLiteral("whatever")).isValid()); |
|
QVERIFY(!history->indexOf(QByteArray()).isValid()); |
|
|
|
// insert some items |
|
history->insert(QSharedPointer<HistoryItem>(new HistoryStringItem(QStringLiteral("foo")))); |
|
QVERIFY(!history->indexOf(QByteArrayLiteral("whatever")).isValid()); |
|
QVERIFY(!history->indexOf(QByteArray()).isValid()); |
|
const QByteArray fooUuid = QCryptographicHash::hash(QByteArrayLiteral("foo"), QCryptographicHash::Sha1); |
|
QVERIFY(history->indexOf(fooUuid).isValid()); |
|
QCOMPARE(history->indexOf(fooUuid).data(Qt::UserRole+1).toByteArray(), fooUuid); |
|
|
|
history->clear(); |
|
QVERIFY(!history->indexOf(fooUuid).isValid()); |
|
} |
|
|
|
QTEST_MAIN(HistoryModelTest) |
|
#include "historymodeltest.moc"
|
|
|