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

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2012 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "recursivedirmodeltest.h"
0023 
0024 // Local
0025 #include <lib/recursivedirmodel.h>
0026 
0027 // Qt
0028 #include <QDebug>
0029 #include <QTest>
0030 
0031 // KF
0032 #include <KDirModel>
0033 
0034 using namespace Gwenview;
0035 
0036 QTEST_MAIN(RecursiveDirModelTest)
0037 
0038 void RecursiveDirModelTest::testBasic_data()
0039 {
0040     QTest::addColumn<QStringList>("initialFiles");
0041     QTest::addColumn<QStringList>("addedFiles");
0042     QTest::addColumn<QStringList>("removedFiles");
0043 #define NEW_ROW(name, initialFiles, addedFiles, removedFiles) QTest::newRow(name) << (initialFiles) << (addedFiles) << (removedFiles)
0044     NEW_ROW("empty_dir", QStringList(), QStringList() << "new.jpg", QStringList() << "new.jpg");
0045     NEW_ROW("images_only",
0046             QStringList() << "pict01.jpg"
0047                           << "pict02.jpg"
0048                           << "pict03.jpg",
0049             QStringList() << "pict04.jpg",
0050             QStringList() << "pict02.jpg");
0051     NEW_ROW("images_in_two_dirs",
0052             QStringList() << "d1/pict101.jpg"
0053                           << "d1/pict102.jpg"
0054                           << "d2/pict201.jpg",
0055             QStringList() << "d1/pict103.jpg"
0056                           << "d2/pict202.jpg",
0057             QStringList() << "d2/pict202.jpg");
0058     NEW_ROW("images_in_two_dirs_w_same_names",
0059             QStringList() << "d1/a.jpg"
0060                           << "d1/b.jpg"
0061                           << "d2/a.jpg"
0062                           << "d2/b.jpg",
0063             QStringList() << "d3/a.jpg"
0064                           << "d3/b.jpg",
0065             QStringList() << "d1/a.jpg"
0066                           << "d2/a.jpg"
0067                           << "d3/a.jpg");
0068 #undef NEW_ROW
0069 }
0070 
0071 static QList<QUrl> listModelUrls(QAbstractItemModel *model)
0072 {
0073     QList<QUrl> out;
0074     for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
0075         QModelIndex index = model->index(row, 0);
0076         KFileItem item = index.data(KDirModel::FileItemRole).value<KFileItem>();
0077         out << item.url();
0078     }
0079     std::sort(out.begin(), out.end());
0080     return out;
0081 }
0082 
0083 static QList<QUrl> listExpectedUrls(const QDir &dir, const QStringList &files)
0084 {
0085     QList<QUrl> lst;
0086     for (const QString &name : files) {
0087         lst << QUrl::fromLocalFile(dir.absoluteFilePath(name));
0088     }
0089     std::sort(lst.begin(), lst.end());
0090     return lst;
0091 }
0092 
0093 void logLst(const QList<QUrl> &lst)
0094 {
0095     for (const QUrl &url : lst) {
0096         qWarning() << url.fileName();
0097     }
0098 }
0099 
0100 void RecursiveDirModelTest::testBasic()
0101 {
0102     QFETCH(QStringList, initialFiles);
0103     QFETCH(QStringList, addedFiles);
0104     QFETCH(QStringList, removedFiles);
0105     TestUtils::SandBoxDir sandBoxDir;
0106 
0107     RecursiveDirModel model;
0108     TestUtils::TimedEventLoop loop;
0109     connect(&model, &RecursiveDirModel::completed, &loop, &QEventLoop::quit);
0110 
0111     // Test initial files
0112     sandBoxDir.fill(initialFiles);
0113     model.setUrl(QUrl::fromLocalFile(sandBoxDir.absolutePath()));
0114 
0115     QList<QUrl> out, expected;
0116     do {
0117         loop.exec();
0118         out = listModelUrls(&model);
0119         expected = listExpectedUrls(sandBoxDir, initialFiles);
0120     } while (out.size() != expected.size());
0121     QCOMPARE(out, expected);
0122 
0123     // Test adding new files
0124     sandBoxDir.fill(addedFiles);
0125 
0126     do {
0127         loop.exec();
0128         out = listModelUrls(&model);
0129         expected = listExpectedUrls(sandBoxDir, initialFiles + addedFiles);
0130     } while (out.size() != expected.size());
0131     QCOMPARE(out, expected);
0132 
0133 #if 0
0134     /* FIXME: This part of the test is not reliable :/ Sometimes some tests pass,
0135      * sometimes they don't. It feels like KDirLister::itemsDeleted() is not
0136      * always emitted.
0137      */
0138 
0139     // Test removing files
0140     for (const QString &name : qAsConst(removedFiles)) {
0141         bool ok = sandBoxDir.remove(name);
0142         Q_ASSERT(ok);
0143         expected.removeOne(QUrl(sandBoxDir.absoluteFilePath(name)));
0144     }
0145     QTime chrono;
0146     chrono.start();
0147     while (chrono.elapsed() < 2000) {
0148         waitForDeferredDeletes();
0149     }
0150 
0151     out = listModelUrls(&model);
0152     if (out != expected) {
0153         qWarning() << "out:";
0154         logLst(out);
0155         qWarning() << "expected:";
0156         logLst(expected);
0157     }
0158     QCOMPARE(out, expected);
0159 #endif
0160 }
0161 
0162 void RecursiveDirModelTest::testSetNewUrl()
0163 {
0164     TestUtils::SandBoxDir sandBoxDir;
0165     sandBoxDir.fill(QStringList() << "d1/a.jpg"
0166                                   << "d1/b.jpg"
0167                                   << "d1/c.jpg"
0168                                   << "d1/d.jpg"
0169                                   << "d2/e.jpg"
0170                                   << "d2/f.jpg");
0171 
0172     RecursiveDirModel model;
0173     TestUtils::TimedEventLoop loop;
0174     connect(&model, &RecursiveDirModel::completed, &loop, &QEventLoop::quit);
0175 
0176     model.setUrl(QUrl::fromLocalFile(sandBoxDir.absoluteFilePath("d1")));
0177     loop.exec();
0178     QCOMPARE(model.rowCount(QModelIndex()), 4);
0179 
0180     model.setUrl(QUrl::fromLocalFile(sandBoxDir.absoluteFilePath("d2")));
0181     loop.exec();
0182     QCOMPARE(model.rowCount(QModelIndex()), 2);
0183 }
0184 
0185 #include "moc_recursivedirmodeltest.cpp"