1 /*
2     SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include <QTest>
8 #include <QAction>
9 #include <QTabWidget>
10 
11 #include "../problemsview.h"
12 
13 #include <tests/testcore.h>
14 #include <tests/autotestshell.h>
15 
16 #include <interfaces/ilanguagecontroller.h>
17 #include <shell/problemmodelset.h>
18 #include <shell/problemmodel.h>
19 #include <shell/problem.h>
20 
21 using namespace KDevelop;
22 
23 class TestProblemsView : public QObject
24 {
25     Q_OBJECT
26 private Q_SLOTS:
27     void initTestCase();
28     void cleanupTestCase();
29 
30     void testLoad();
31     void testAddModel();
32     void testSwitchTab();
33     void testRemoveModel();
34     void testAddRemoveProblems();
35     void testSetProblems();
36 
37 private:
38     QTabWidget* tabWidget();
39 
40     QScopedPointer<ProblemsView> m_view;
41 };
42 
initTestCase()43 void TestProblemsView::initTestCase()
44 {
45     AutoTestShell::init();
46     TestCore::initialize(Core::NoUi);
47 
48     ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
49     auto* model = new ProblemModel(pms);
50     IProblem::Ptr p(new DetectedProblem());
51     model->addProblem(p);
52     pms->addModel(QStringLiteral("MODEL1_ID"), QStringLiteral("MODEL1"), model);
53 
54     m_view.reset(new ProblemsView());
55 }
56 
cleanupTestCase()57 void TestProblemsView::cleanupTestCase()
58 {
59     TestCore::shutdown();
60 }
61 
testLoad()62 void TestProblemsView::testLoad()
63 {
64     m_view->load();
65 
66     // Check that the initial model's tab shows up
67     QTabWidget* tab = tabWidget();
68     QVERIFY(tab);
69     QCOMPARE(tab->count(), 1);
70     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL1 (1)"));
71 }
72 
testAddModel()73 void TestProblemsView::testAddModel()
74 {
75     ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
76     pms->addModel(QStringLiteral("MODEL2_ID"), QStringLiteral("MODEL2"), new ProblemModel(pms));
77 
78     QTabWidget* tab = tabWidget();
79     QVERIFY(tab);
80     QCOMPARE(tab->count(), 2);
81     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL1 (1)"));
82     QCOMPARE(tab->tabText(1), QStringLiteral("MODEL2 (0)"));
83 }
84 
visibilites(const QList<QAction * > & actions)85 QVector<bool> visibilites(const QList<QAction*>& actions)
86 {
87     QVector<bool> visibilites;
88     for (auto action : actions) {
89         visibilites << action->isVisible();
90     }
91     return visibilites;
92 }
93 
testSwitchTab()94 void TestProblemsView::testSwitchTab()
95 {
96     QTabWidget* tab = tabWidget();
97     QVERIFY(tab);
98 
99     // Check that the current widget's actions are in the toolbar
100     QWidget* oldWidget = tab->currentWidget();
101     QVERIFY(oldWidget);
102     const auto oldVisibilites = visibilites(m_view->actions());
103 
104     tab->setCurrentIndex(1);
105 
106     // Check that the new widget's actions are in the toolbar
107     QWidget* newWidget = tab->currentWidget();
108     QVERIFY(newWidget);
109     QVERIFY(newWidget != oldWidget);
110     const auto newVisibilites = visibilites(m_view->actions());
111     QCOMPARE(oldVisibilites, newVisibilites);
112 }
113 
testRemoveModel()114 void TestProblemsView::testRemoveModel()
115 {
116     // Remove the model
117     ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
118     ProblemModel* model = pms->findModel(QStringLiteral("MODEL1_ID"));
119     QVERIFY(model);
120     pms->removeModel(QStringLiteral("MODEL1_ID"));
121     delete model;
122     model = nullptr;
123 
124     // Now let's see if the view has been updated!
125     QTabWidget* tab = tabWidget();
126     QVERIFY(tab);
127     QCOMPARE(tab->count(), 1);
128     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
129 }
130 
testAddRemoveProblems()131 void TestProblemsView::testAddRemoveProblems()
132 {
133     ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
134     ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID"));
135     QVERIFY(model);
136 
137     QTabWidget* tab = tabWidget();
138     QVERIFY(tab);
139 
140     // Make sure there are no problems right now
141     model->clearProblems();
142     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
143 
144     // Let's add some problems
145     int c = 0;
146     for (int i = 0; i < 3; i++) {
147         IProblem::Ptr p(new DetectedProblem());
148         model->addProblem(p);
149         c++;
150 
151         // Check if the view has noticed the addition
152         QString label = QStringLiteral("MODEL2 (%1)").arg(c);
153         QCOMPARE(tab->tabText(0), label);
154     }
155 
156     // Clear the problems
157     model->clearProblems();
158 
159     // Check if the view has noticed the clear
160     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
161 }
162 
testSetProblems()163 void TestProblemsView::testSetProblems()
164 {
165     ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
166     ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID"));
167     QVERIFY(model);
168 
169     QTabWidget* tab = tabWidget();
170     QVERIFY(tab);
171 
172     // Make sure there are no problems right now
173     model->clearProblems();
174     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
175 
176     // Build a problem vector and set the problems
177     QVector<IProblem::Ptr> problems;
178     for (int i = 0; i < 3; i++) {
179         IProblem::Ptr p(new DetectedProblem());
180         problems.push_back(p);
181     }
182     model->setProblems(problems);
183 
184     // Check if the view has noticed
185     QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (3)"));
186 }
187 
188 //////////////////////////////////////////////////////////////////////////////////////////////////////////
189 
tabWidget()190 QTabWidget* TestProblemsView::tabWidget()
191 {
192     auto* tab = m_view->findChild<QTabWidget*>();
193     return tab;
194 }
195 
196 QTEST_MAIN(TestProblemsView)
197 
198 #include "test_problemsview.moc"
199