File indexing completed on 2024-05-12 04:38:21
0001 /* 0002 SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <QTest> 0008 #include <QSignalSpy> 0009 #include <QVector> 0010 #include "shell/problemmodelset.h" 0011 #include "shell/problemmodel.h" 0012 #include "tests/testcore.h" 0013 #include "tests/autotestshell.h" 0014 0015 using namespace KDevelop; 0016 0017 namespace 0018 { 0019 0020 const int testModelCount = 3; 0021 0022 const QString testModelIds[] = { 0023 QStringLiteral("MODEL1_ID"), 0024 QStringLiteral("MODEL2_ID"), 0025 QStringLiteral("MODEL3_ID") 0026 }; 0027 0028 const QString testModelNames[] = { 0029 QStringLiteral("MODEL1"), 0030 QStringLiteral("MODEL2"), 0031 QStringLiteral("MODEL3") 0032 }; 0033 0034 struct TestModelData 0035 { 0036 QString id; 0037 QString name; 0038 ProblemModel* model; 0039 }; 0040 0041 } 0042 0043 class TestProblemModelSet : public QObject 0044 { 0045 Q_OBJECT 0046 private Q_SLOTS: 0047 void initTestCase(); 0048 void cleanupTestCase(); 0049 0050 void testAddModel(); 0051 void testFindModel(); 0052 void testModelListing(); 0053 void testRemoveModel(); 0054 0055 private: 0056 QScopedPointer<ProblemModelSet> m_set; 0057 QVector<TestModelData> m_testData; 0058 0059 }; 0060 0061 void TestProblemModelSet::initTestCase() 0062 { 0063 AutoTestShell::init(); 0064 TestCore::initialize(Core::NoUi); 0065 0066 m_set.reset(new ProblemModelSet()); 0067 0068 for (int i = 0; i < testModelCount; i++) { 0069 m_testData.push_back(TestModelData({testModelIds[i], testModelNames[i], new ProblemModel(nullptr)})); 0070 } 0071 } 0072 0073 void TestProblemModelSet::cleanupTestCase() 0074 { 0075 for (auto& d : qAsConst(m_testData)) { 0076 delete d.model; 0077 } 0078 0079 m_testData.clear(); 0080 0081 TestCore::shutdown(); 0082 } 0083 0084 void TestProblemModelSet::testAddModel() 0085 { 0086 QSignalSpy spy(m_set.data(), &ProblemModelSet::added); 0087 0088 int c = 0; 0089 for (int i = 0; i < testModelCount; i++) { 0090 m_set->addModel(m_testData[i].id, m_testData[i].name, m_testData[i].model); 0091 c++; 0092 QCOMPARE(spy.count(), c); 0093 QCOMPARE(m_set->models().count(), c); 0094 } 0095 0096 } 0097 0098 void TestProblemModelSet::testFindModel() 0099 { 0100 ProblemModel *model = nullptr; 0101 for (int i = 0; i < testModelCount; i++) { 0102 model = m_set->findModel(m_testData[i].id); 0103 0104 QVERIFY(model); 0105 QVERIFY(model == m_testData[i].model); 0106 } 0107 0108 model = m_set->findModel(QString()); 0109 QVERIFY(model == nullptr); 0110 0111 model = m_set->findModel(QStringLiteral("RandomName")); 0112 QVERIFY(model == nullptr); 0113 } 0114 0115 void TestProblemModelSet::testModelListing() 0116 { 0117 QVector<ModelData> models = m_set->models(); 0118 QCOMPARE(models.size(), testModelCount); 0119 0120 for (int i = 0; i < testModelCount; i++) { 0121 QCOMPARE(models[i].name, m_testData[i].name); 0122 QCOMPARE(models[i].model, m_testData[i].model); 0123 } 0124 } 0125 0126 void TestProblemModelSet::testRemoveModel() 0127 { 0128 QSignalSpy spy(m_set.data(), &ProblemModelSet::removed); 0129 0130 int c = 0; 0131 for (const TestModelData& data : qAsConst(m_testData)) { 0132 m_set->removeModel(data.id); 0133 c++; 0134 0135 QCOMPARE(spy.count(), c); 0136 QVERIFY(testModelCount >= c); 0137 QCOMPARE(m_set->models().count(), testModelCount - c); 0138 } 0139 0140 0141 0142 } 0143 0144 QTEST_MAIN(TestProblemModelSet) 0145 0146 #include "test_problemmodelset.moc"