File indexing completed on 2024-06-16 04:24:04

0001 /*
0002     SPDX-FileCopyrightText: 2011 Andrey Batyiev <batyiev@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "test_models.h"
0008 
0009 #include <QTest>
0010 
0011 #include <vcs/models/vcsfilechangesmodel.h>
0012 #include <tests/autotestshell.h>
0013 #include <tests/testcore.h>
0014 
0015 using namespace KDevelop;
0016 
0017 void TestModels::initTestCase()
0018 {
0019     AutoTestShell::init({QStringLiteral("dummy")});
0020     TestCore::initialize();
0021 }
0022 
0023 void TestModels::cleanupTestCase()
0024 {
0025     TestCore::shutdown();
0026 }
0027 
0028 void TestModels::testVcsFileChangesModel()
0029 {
0030     const auto indexForUrl = [](const VcsFileChangesModel* model, const QUrl& url) {
0031         return model->match(model->index(0, 0), VcsFileChangesModel::UrlRole,
0032                             url, 1, Qt::MatchExactly).value(0);
0033     };
0034     const auto statusInfo = [](const QModelIndex& idx) {
0035         return idx.data(VcsFileChangesModel::VcsStatusInfoRole).value<VcsStatusInfo>();
0036     };
0037 
0038     QScopedPointer<VcsFileChangesModel> model(new VcsFileChangesModel);
0039 
0040     // Newly created model should be empty
0041     QVERIFY(model->rowCount() == 0);
0042 
0043     // Pull some files into
0044     QUrl filenames[] = {
0045         QUrl::fromLocalFile(QStringLiteral("foo")),
0046         QUrl::fromLocalFile(QStringLiteral("bar")),
0047         QUrl::fromLocalFile(QStringLiteral("pew")),
0048         QUrl::fromLocalFile(QStringLiteral("trash"))
0049     };
0050     VcsStatusInfo::State states[] = {VcsStatusInfo::ItemAdded, VcsStatusInfo::ItemModified, VcsStatusInfo::ItemDeleted, VcsStatusInfo::ItemUpToDate};
0051     VcsStatusInfo status;
0052 
0053     for(int i = 0; i < 3; i++) {
0054         status.setUrl(filenames[i]);
0055         status.setState(states[i]);
0056         model->updateState(status);
0057         QCOMPARE(model->rowCount(), (i+1));
0058     }
0059 
0060     // Pulling up-to-date file doesn't change anything
0061     {
0062         status.setUrl(filenames[3]);
0063         status.setState(states[3]);
0064         model->updateState(status);
0065         QCOMPARE(model->rowCount(), 3);
0066     }
0067 
0068     // Check that all OK
0069     for(int i = 0; i < 3; i++) {
0070         QModelIndex idx = indexForUrl(model.data(), filenames[i]);
0071         QVERIFY(idx.isValid());
0072         VcsStatusInfo info = statusInfo(idx);
0073         QVERIFY(info.url().isValid());
0074         QCOMPARE(info.url(), filenames[i]);
0075         QCOMPARE(info.state(), states[i]);
0076     }
0077 
0078     // Pull it all again = nothing changed
0079     for(int i = 0; i < 3; i++) {
0080         status.setUrl(filenames[i]);
0081         status.setState(states[i]);
0082         model->updateState(status);
0083         QCOMPARE(model->rowCount(), 3);
0084     }
0085 
0086     // Check that all OK
0087     for(int i = 0; i < 3; i++) {
0088         QModelIndex item = indexForUrl(model.data(), filenames[i]);
0089         QVERIFY(item.isValid());
0090         VcsStatusInfo info = statusInfo(item);
0091         QCOMPARE(info.url(), filenames[i]);
0092         QCOMPARE(info.state(), states[i]);
0093     }
0094 
0095     // Remove one file
0096     {
0097         states[1] = VcsStatusInfo::ItemUpToDate;
0098         status.setUrl(filenames[1]);
0099         status.setState(states[1]);
0100         model->updateState(status);
0101         QCOMPARE(model->rowCount(), 2);
0102     }
0103 
0104     // Check them all
0105     for(int i = 0; i < 3; i++) {
0106         if(states[i] != VcsStatusInfo::ItemUpToDate && states[i] != VcsStatusInfo::ItemUnknown) {
0107             QModelIndex item = indexForUrl(model.data(), filenames[i]);
0108             QVERIFY(item.isValid());
0109             VcsStatusInfo info = statusInfo(item);
0110             QCOMPARE(info.url(), filenames[i]);
0111             QCOMPARE(info.state(), states[i]);
0112         }
0113     }
0114 
0115     // Delete them all
0116     model->removeRows(0, model->rowCount());
0117     QCOMPARE(model->rowCount(), 0);
0118 
0119     // Pull it all again
0120     for(int i = 0; i < 3; i++) {
0121         status.setUrl(filenames[i]);
0122         status.setState(states[i]);
0123         model->updateState(status);
0124     }
0125     QCOMPARE(model->rowCount(), 2);
0126 }
0127 
0128 QTEST_MAIN(TestModels)
0129 
0130 #include "moc_test_models.cpp"