File indexing completed on 2024-05-19 04:41:29
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 <QAction> 0009 #include <QTabWidget> 0010 0011 #include "../problemsview.h" 0012 0013 #include <tests/testcore.h> 0014 #include <tests/autotestshell.h> 0015 0016 #include <interfaces/ilanguagecontroller.h> 0017 #include <shell/problemmodelset.h> 0018 #include <shell/problemmodel.h> 0019 #include <shell/problem.h> 0020 0021 using namespace KDevelop; 0022 0023 class TestProblemsView : public QObject 0024 { 0025 Q_OBJECT 0026 private Q_SLOTS: 0027 void initTestCase(); 0028 void cleanupTestCase(); 0029 0030 void testLoad(); 0031 void testAddModel(); 0032 void testSwitchTab(); 0033 void testRemoveModel(); 0034 void testAddRemoveProblems(); 0035 void testSetProblems(); 0036 0037 private: 0038 QTabWidget* tabWidget(); 0039 0040 QScopedPointer<ProblemsView> m_view; 0041 }; 0042 0043 void TestProblemsView::initTestCase() 0044 { 0045 AutoTestShell::init(); 0046 TestCore::initialize(Core::NoUi); 0047 0048 ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); 0049 auto* model = new ProblemModel(pms); 0050 IProblem::Ptr p(new DetectedProblem()); 0051 model->addProblem(p); 0052 pms->addModel(QStringLiteral("MODEL1_ID"), QStringLiteral("MODEL1"), model); 0053 0054 m_view.reset(new ProblemsView()); 0055 } 0056 0057 void TestProblemsView::cleanupTestCase() 0058 { 0059 TestCore::shutdown(); 0060 } 0061 0062 void TestProblemsView::testLoad() 0063 { 0064 m_view->load(); 0065 0066 // Check that the initial model's tab shows up 0067 QTabWidget* tab = tabWidget(); 0068 QVERIFY(tab); 0069 QCOMPARE(tab->count(), 1); 0070 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL1 (1)")); 0071 } 0072 0073 void TestProblemsView::testAddModel() 0074 { 0075 ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); 0076 pms->addModel(QStringLiteral("MODEL2_ID"), QStringLiteral("MODEL2"), new ProblemModel(pms)); 0077 0078 QTabWidget* tab = tabWidget(); 0079 QVERIFY(tab); 0080 QCOMPARE(tab->count(), 2); 0081 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL1 (1)")); 0082 QCOMPARE(tab->tabText(1), QStringLiteral("MODEL2 (0)")); 0083 } 0084 0085 QVector<bool> visibilites(const QList<QAction*>& actions) 0086 { 0087 QVector<bool> visibilites; 0088 for (auto action : actions) { 0089 visibilites << action->isVisible(); 0090 } 0091 return visibilites; 0092 } 0093 0094 void TestProblemsView::testSwitchTab() 0095 { 0096 QTabWidget* tab = tabWidget(); 0097 QVERIFY(tab); 0098 0099 // Check that the current widget's actions are in the toolbar 0100 QWidget* oldWidget = tab->currentWidget(); 0101 QVERIFY(oldWidget); 0102 const auto oldVisibilites = visibilites(m_view->actions()); 0103 0104 tab->setCurrentIndex(1); 0105 0106 // Check that the new widget's actions are in the toolbar 0107 QWidget* newWidget = tab->currentWidget(); 0108 QVERIFY(newWidget); 0109 QVERIFY(newWidget != oldWidget); 0110 const auto newVisibilites = visibilites(m_view->actions()); 0111 QCOMPARE(oldVisibilites, newVisibilites); 0112 } 0113 0114 void TestProblemsView::testRemoveModel() 0115 { 0116 // Remove the model 0117 ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); 0118 ProblemModel* model = pms->findModel(QStringLiteral("MODEL1_ID")); 0119 QVERIFY(model); 0120 pms->removeModel(QStringLiteral("MODEL1_ID")); 0121 delete model; 0122 model = nullptr; 0123 0124 // Now let's see if the view has been updated! 0125 QTabWidget* tab = tabWidget(); 0126 QVERIFY(tab); 0127 QCOMPARE(tab->count(), 1); 0128 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)")); 0129 } 0130 0131 void TestProblemsView::testAddRemoveProblems() 0132 { 0133 ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); 0134 ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID")); 0135 QVERIFY(model); 0136 0137 QTabWidget* tab = tabWidget(); 0138 QVERIFY(tab); 0139 0140 // Make sure there are no problems right now 0141 model->clearProblems(); 0142 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)")); 0143 0144 // Let's add some problems 0145 int c = 0; 0146 for (int i = 0; i < 3; i++) { 0147 IProblem::Ptr p(new DetectedProblem()); 0148 model->addProblem(p); 0149 c++; 0150 0151 // Check if the view has noticed the addition 0152 QString label = QStringLiteral("MODEL2 (%1)").arg(c); 0153 QCOMPARE(tab->tabText(0), label); 0154 } 0155 0156 // Clear the problems 0157 model->clearProblems(); 0158 0159 // Check if the view has noticed the clear 0160 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)")); 0161 } 0162 0163 void TestProblemsView::testSetProblems() 0164 { 0165 ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet(); 0166 ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID")); 0167 QVERIFY(model); 0168 0169 QTabWidget* tab = tabWidget(); 0170 QVERIFY(tab); 0171 0172 // Make sure there are no problems right now 0173 model->clearProblems(); 0174 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)")); 0175 0176 // Build a problem vector and set the problems 0177 QVector<IProblem::Ptr> problems; 0178 for (int i = 0; i < 3; i++) { 0179 IProblem::Ptr p(new DetectedProblem()); 0180 problems.push_back(p); 0181 } 0182 model->setProblems(problems); 0183 0184 // Check if the view has noticed 0185 QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (3)")); 0186 } 0187 0188 ////////////////////////////////////////////////////////////////////////////////////////////////////////// 0189 0190 QTabWidget* TestProblemsView::tabWidget() 0191 { 0192 auto* tab = m_view->findChild<QTabWidget*>(); 0193 return tab; 0194 } 0195 0196 QTEST_MAIN(TestProblemsView) 0197 0198 #include "test_problemsview.moc"