File indexing completed on 2024-10-13 05:09:05

0001 /* This file is part of Kairo Timer
0002 
0003    SPDX-FileCopyrightText: 2016 (c) Kevin Ottens <ervin@kde.org>
0004 
0005    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 
0007 */
0008 
0009 #include <QtTest>
0010 
0011 #include <memory>
0012 
0013 #include "circuitlistmodel.h"
0014 
0015 static_assert(std::is_base_of<QAbstractItemModel, CircuitListModel>::value, "Should be an item model");
0016 
0017 class CircuitListModelTest : public QObject
0018 {
0019     Q_OBJECT
0020 private:
0021     QDir storageDir() const
0022     {
0023         auto path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
0024         return {path};
0025     }
0026 
0027     void writeInFile(const QString &fileName, const QString &fileContent)
0028     {
0029         auto dir = storageDir();
0030         auto file = std::make_unique<QFile>(dir.path() + '/' + fileName);
0031         if (file->open(QFile::WriteOnly)) {
0032             auto stream = std::make_unique<QTextStream>(file.get());
0033             *stream << fileContent;
0034         }
0035         QTest::qWait(20);
0036     }
0037 
0038     void deleteFile(const QString &fileName)
0039     {
0040         QFile::remove(storageDir().path() + '/' + fileName);
0041         QTest::qWait(20);
0042     }
0043 
0044 private slots:
0045     void initTestCase()
0046     {
0047         QCoreApplication::setApplicationName("circuitlistmodeltest");
0048         QStandardPaths::setTestModeEnabled(true);
0049         cleanupTestCase();
0050     }
0051 
0052     void cleanupTestCase()
0053     {
0054         auto dir = storageDir();
0055         if (dir.exists()) {
0056             dir.removeRecursively();
0057             dir.rmpath("..");
0058         }
0059     }
0060 
0061     void shouldHaveDefaultStateAndCreateStorageDir()
0062     {
0063         // GIVEN
0064         auto dir = storageDir();
0065         QVERIFY(!dir.exists());
0066         auto model = std::make_unique<CircuitListModel>();
0067 
0068         // THEN
0069         QVERIFY(dir.exists());
0070         QCOMPARE(model->path(), dir.path());
0071         QCOMPARE(model->rowCount(), 0);
0072         QCOMPARE(model->roleNames().value(Qt::DisplayRole), QByteArray("display"));
0073         QCOMPARE(model->roleNames().value(CircuitListModel::FilePathRole), QByteArray("filePath"));
0074     }
0075 
0076     void shouldListCircuitsInStorageDir()
0077     {
0078         // GIVEN
0079         auto model = std::make_unique<CircuitListModel>();
0080         auto spy = std::make_unique<QSignalSpy>(model.get(), &CircuitListModel::modelReset);
0081 
0082         // WHEN
0083         writeInFile("foo.kairo", "---\nname: Foo Circuit\n---\n[]\n");
0084 
0085         // THEN
0086         QCOMPARE(spy->size(), 1);
0087         QCOMPARE(model->path(), storageDir().path());
0088         QCOMPARE(model->rowCount(), 1);
0089         QCOMPARE(model->rowCount(model->index(0, 0)), 0);
0090         QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Foo Circuit"));
0091         QCOMPARE(model->data(model->index(0, 0), CircuitListModel::FilePathRole).toString(),
0092                  storageDir().path() + '/' + "foo.kairo");
0093         spy->clear();
0094 
0095         // WHEN
0096         writeInFile("ignored.json", "---\nname: Ignored file (not .kairo)\n---\n[]\n");
0097 
0098         // THEN
0099         QCOMPARE(spy->size(), 0);
0100         QCOMPARE(model->path(), storageDir().path());
0101         QCOMPARE(model->rowCount(), 1);
0102         QCOMPARE(model->rowCount(model->index(0, 0)), 0);
0103         QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Foo Circuit"));
0104         QCOMPARE(model->data(model->index(0, 0), CircuitListModel::FilePathRole).toString(),
0105                  storageDir().path() + '/' + "foo.kairo");
0106         spy->clear();
0107 
0108         // WHEN
0109         writeInFile("bar.kairo", "---\nname: ZBar\n---\n[]\n");
0110 
0111         // THEN
0112         QCOMPARE(spy->size(), 1);
0113         QCOMPARE(model->path(), storageDir().path());
0114         QCOMPARE(model->rowCount(), 2);
0115         QCOMPARE(model->rowCount(model->index(0, 0)), 0);
0116         QCOMPARE(model->rowCount(model->index(1, 0)), 0);
0117         QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Foo Circuit"));
0118         QCOMPARE(model->data(model->index(0, 0), CircuitListModel::FilePathRole).toString(),
0119                  storageDir().path() + '/' + "foo.kairo");
0120         QCOMPARE(model->data(model->index(1, 0)).toString(), QString("ZBar"));
0121         QCOMPARE(model->data(model->index(1, 0), CircuitListModel::FilePathRole).toString(),
0122                  storageDir().path() + '/' + "bar.kairo");
0123         spy->clear();
0124 
0125         // WHEN
0126         writeInFile("zab.kairo", "---\nname: Baz Circuit\n---\n[]\n");
0127 
0128         // THEN
0129         QCOMPARE(spy->size(), 1);
0130         QCOMPARE(model->path(), storageDir().path());
0131         QCOMPARE(model->rowCount(), 3);
0132         QCOMPARE(model->rowCount(model->index(0, 0)), 0);
0133         QCOMPARE(model->rowCount(model->index(1, 0)), 0);
0134         QCOMPARE(model->rowCount(model->index(2, 0)), 0);
0135         QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Baz Circuit"));
0136         QCOMPARE(model->data(model->index(0, 0), CircuitListModel::FilePathRole).toString(),
0137                  storageDir().path() + '/' + "zab.kairo");
0138         QCOMPARE(model->data(model->index(1, 0)).toString(), QString("Foo Circuit"));
0139         QCOMPARE(model->data(model->index(1, 0), CircuitListModel::FilePathRole).toString(),
0140                  storageDir().path() + '/' + "foo.kairo");
0141         QCOMPARE(model->data(model->index(2, 0)).toString(), QString("ZBar"));
0142         QCOMPARE(model->data(model->index(2, 0), CircuitListModel::FilePathRole).toString(),
0143                  storageDir().path() + '/' + "bar.kairo");
0144         spy->clear();
0145 
0146         // WHEN
0147         deleteFile("foo.kairo");
0148 
0149         // THEN
0150         QCOMPARE(spy->size(), 1);
0151         QCOMPARE(model->path(), storageDir().path());
0152         QCOMPARE(model->rowCount(), 2);
0153         QCOMPARE(model->rowCount(model->index(0, 0)), 0);
0154         QCOMPARE(model->rowCount(model->index(1, 0)), 0);
0155         QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Baz Circuit"));
0156         QCOMPARE(model->data(model->index(0, 0), CircuitListModel::FilePathRole).toString(),
0157                  storageDir().path() + '/' + "zab.kairo");
0158         QCOMPARE(model->data(model->index(1, 0)).toString(), QString("ZBar"));
0159         QCOMPARE(model->data(model->index(1, 0), CircuitListModel::FilePathRole).toString(),
0160                  storageDir().path() + '/' + "bar.kairo");
0161         spy->clear();
0162 
0163         // WHEN
0164         deleteFile("bar.kairo");
0165 
0166         // THEN
0167         QCOMPARE(spy->size(), 1);
0168         QCOMPARE(model->path(), storageDir().path());
0169         QCOMPARE(model->rowCount(), 1);
0170         QCOMPARE(model->rowCount(model->index(0, 0)), 0);
0171         QCOMPARE(model->data(model->index(0, 0)).toString(), QString("Baz Circuit"));
0172         QCOMPARE(model->data(model->index(0, 0), CircuitListModel::FilePathRole).toString(),
0173                  storageDir().path() + '/' + "zab.kairo");
0174         spy->clear();
0175 
0176         // WHEN
0177         deleteFile("zab.kairo");
0178 
0179         // THEN
0180         QCOMPARE(spy->size(), 1);
0181         QCOMPARE(model->path(), storageDir().path());
0182         QCOMPARE(model->rowCount(), 0);
0183         spy->clear();
0184     }
0185 
0186     void shouldLoadCircuits()
0187     {
0188         // GIVEN
0189         auto model = std::make_unique<CircuitListModel>();
0190         writeInFile("foo.kairo", "---\nname: Foo Circuit\n---\n[{\"text\": \"foo\"},{\"text\": \"foo\", \"duration\": 800}]\n");
0191         writeInFile("bar.kairo", "---\nname: Bar Circuit\n---\n[{\"text\": \"bar\", \"duration\": 4000},{\"text\": \"bar\"}]\n");
0192 
0193         // WHEN
0194         auto circuit = model->loadCircuit(0);
0195 
0196         // THEN
0197         auto expected = CircuitModel{"Bar Circuit",{{"bar", 4000}, {"bar"}}};
0198         QCOMPARE(circuit, expected);
0199 
0200         // WHEN
0201         circuit = model->loadCircuit(1);
0202 
0203         // THEN
0204         expected = CircuitModel{"Foo Circuit",{{"foo"}, {"foo", 800}}};
0205         QCOMPARE(circuit, expected);
0206     }
0207 };
0208 
0209 QTEST_MAIN(CircuitListModelTest)
0210 
0211 #include "circuitlistmodeltest.moc"