File indexing completed on 2024-05-12 04:19:58

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2009 Aurélien Gâteau <agateau@kde.org>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (at your option) any later version.
0009 
0010 This program is distributed in the hope that it will be useful,
0011 but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 GNU General Public License for more details.
0014 
0015 You should have received a copy of the GNU General Public License
0016 along with this program; if not, write to the Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018 
0019 */
0020 #include "historymodeltest.h"
0021 
0022 // Qt
0023 #include <QDir>
0024 #include <QTemporaryDir>
0025 #include <QTest>
0026 
0027 // KF
0028 #include <KFilePlacesModel>
0029 
0030 // Local
0031 #include "../lib/historymodel.h"
0032 
0033 QTEST_MAIN(HistoryModelTest)
0034 
0035 using namespace Gwenview;
0036 
0037 void testModel(const HistoryModel &model, const QUrl &u1, const QUrl &u2)
0038 {
0039     QModelIndex index;
0040     QUrl url;
0041     QCOMPARE(model.rowCount(), 2);
0042 
0043     index = model.index(0, 0);
0044     QVERIFY(index.isValid());
0045     url = model.data(index, KFilePlacesModel::UrlRole).toUrl();
0046     QCOMPARE(url, u1);
0047 
0048     index = model.index(1, 0);
0049     QVERIFY(index.isValid());
0050     url = model.data(index, KFilePlacesModel::UrlRole).toUrl();
0051     QCOMPARE(url, u2);
0052 }
0053 
0054 void HistoryModelTest::testAddUrl()
0055 {
0056     QUrl u1 = QUrl::fromLocalFile("/home");
0057     QDateTime d1 = QDateTime::fromString("2008-02-03T12:34:56", Qt::ISODate);
0058     QUrl u2 = QUrl::fromLocalFile("/root");
0059     QDateTime d2 = QDateTime::fromString("2009-01-29T23:01:47", Qt::ISODate);
0060     QTemporaryDir dir;
0061     {
0062         HistoryModel model(nullptr, dir.path());
0063         model.addUrl(u1, d1);
0064         model.addUrl(u2, d2);
0065         testModel(model, u2, u1);
0066     }
0067 
0068     HistoryModel model(nullptr, dir.path());
0069     testModel(model, u2, u1);
0070 
0071     // Make u1 the most recent
0072     QDateTime d3 = QDateTime::fromString("2009-03-24T22:42:15", Qt::ISODate);
0073     model.addUrl(u1, d3);
0074     testModel(model, u1, u2);
0075 }
0076 
0077 void HistoryModelTest::testGarbageCollect()
0078 {
0079     QUrl u1 = QUrl::fromLocalFile("/home");
0080     QDateTime d1 = QDateTime::fromString("2008-02-03T12:34:56", Qt::ISODate);
0081     QUrl u2 = QUrl::fromLocalFile("/root");
0082     QDateTime d2 = QDateTime::fromString("2009-01-29T23:01:47", Qt::ISODate);
0083     QUrl u3 = QUrl::fromLocalFile("/usr");
0084     QDateTime d3 = QDateTime::fromString("2009-03-24T22:42:15", Qt::ISODate);
0085 
0086     QTemporaryDir dir;
0087     {
0088         HistoryModel model(nullptr, dir.path(), 2);
0089         model.addUrl(u1, d1);
0090         model.addUrl(u2, d2);
0091         testModel(model, u2, u1);
0092         model.addUrl(u3, d3);
0093     }
0094 
0095     // Create a model with a larger history so that if garbage collecting fails
0096     // to remove the collected url, the size of the model won't pass
0097     // testModel()
0098     HistoryModel model(nullptr, dir.path(), 10);
0099     testModel(model, u3, u2);
0100 }
0101 
0102 void HistoryModelTest::testRemoveRows()
0103 {
0104     QUrl u1 = QUrl::fromLocalFile("/home");
0105     QDateTime d1 = QDateTime::fromString("2008-02-03T12:34:56", Qt::ISODate);
0106     QUrl u2 = QUrl::fromLocalFile("/root");
0107     QDateTime d2 = QDateTime::fromString("2009-01-29T23:01:47", Qt::ISODate);
0108 
0109     QTemporaryDir dir;
0110     HistoryModel model(nullptr, dir.path(), 2);
0111     model.addUrl(u1, d1);
0112     model.addUrl(u2, d2);
0113     model.removeRows(0, 1);
0114     QCOMPARE(model.rowCount(), 1);
0115     QDir qDir(dir.path());
0116     QCOMPARE(qDir.entryList(QDir::Files | QDir::NoDotAndDotDot).count(), 1);
0117 }
0118 
0119 #include "moc_historymodeltest.cpp"