1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/*
SPDX-FileCopyrightText: 2011 Andrey Batyiev <batyiev@gmail.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "test_models.h"
#include <QTest>
#include <vcs/models/vcsfilechangesmodel.h>
#include <tests/autotestshell.h>
#include <tests/testcore.h>
using namespace KDevelop;
void TestModels::initTestCase()
{
AutoTestShell::init({QStringLiteral("dummy")});
TestCore::initialize();
}
void TestModels::cleanupTestCase()
{
TestCore::shutdown();
}
void TestModels::testVcsFileChangesModel()
{
const auto indexForUrl = [](const VcsFileChangesModel* model, const QUrl& url) {
return model->match(model->index(0, 0), VcsFileChangesModel::UrlRole,
url, 1, Qt::MatchExactly).value(0);
};
const auto statusInfo = [](const QModelIndex& idx) {
return idx.data(VcsFileChangesModel::VcsStatusInfoRole).value<VcsStatusInfo>();
};
QScopedPointer<VcsFileChangesModel> model(new VcsFileChangesModel);
// Newly created model should be empty
QVERIFY(model->rowCount() == 0);
// Pull some files into
QUrl filenames[] = {
QUrl::fromLocalFile(QStringLiteral("foo")),
QUrl::fromLocalFile(QStringLiteral("bar")),
QUrl::fromLocalFile(QStringLiteral("pew")),
QUrl::fromLocalFile(QStringLiteral("trash"))
};
VcsStatusInfo::State states[] = {VcsStatusInfo::ItemAdded, VcsStatusInfo::ItemModified, VcsStatusInfo::ItemDeleted, VcsStatusInfo::ItemUpToDate};
VcsStatusInfo status;
for(int i = 0; i < 3; i++) {
status.setUrl(filenames[i]);
status.setState(states[i]);
model->updateState(status);
QCOMPARE(model->rowCount(), (i+1));
}
// Pulling up-to-date file doesn't change anything
{
status.setUrl(filenames[3]);
status.setState(states[3]);
model->updateState(status);
QCOMPARE(model->rowCount(), 3);
}
// Check that all OK
for(int i = 0; i < 3; i++) {
QModelIndex idx = indexForUrl(model.data(), filenames[i]);
QVERIFY(idx.isValid());
VcsStatusInfo info = statusInfo(idx);
QVERIFY(info.url().isValid());
QCOMPARE(info.url(), filenames[i]);
QCOMPARE(info.state(), states[i]);
}
// Pull it all again = nothing changed
for(int i = 0; i < 3; i++) {
status.setUrl(filenames[i]);
status.setState(states[i]);
model->updateState(status);
QCOMPARE(model->rowCount(), 3);
}
// Check that all OK
for(int i = 0; i < 3; i++) {
QModelIndex item = indexForUrl(model.data(), filenames[i]);
QVERIFY(item.isValid());
VcsStatusInfo info = statusInfo(item);
QCOMPARE(info.url(), filenames[i]);
QCOMPARE(info.state(), states[i]);
}
// Remove one file
{
states[1] = VcsStatusInfo::ItemUpToDate;
status.setUrl(filenames[1]);
status.setState(states[1]);
model->updateState(status);
QCOMPARE(model->rowCount(), 2);
}
// Check them all
for(int i = 0; i < 3; i++) {
if(states[i] != VcsStatusInfo::ItemUpToDate && states[i] != VcsStatusInfo::ItemUnknown) {
QModelIndex item = indexForUrl(model.data(), filenames[i]);
QVERIFY(item.isValid());
VcsStatusInfo info = statusInfo(item);
QCOMPARE(info.url(), filenames[i]);
QCOMPARE(info.state(), states[i]);
}
}
// Delete them all
model->removeRows(0, model->rowCount());
QCOMPARE(model->rowCount(), 0);
// Pull it all again
for(int i = 0; i < 3; i++) {
status.setUrl(filenames[i]);
status.setState(states[i]);
model->updateState(status);
}
QCOMPARE(model->rowCount(), 2);
}
QTEST_MAIN(TestModels)
|