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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <QTest>
#include <QAction>
#include <QTabWidget>
#include "../problemsview.h"
#include <tests/testcore.h>
#include <tests/autotestshell.h>
#include <interfaces/ilanguagecontroller.h>
#include <shell/problemmodelset.h>
#include <shell/problemmodel.h>
#include <shell/problem.h>
using namespace KDevelop;
class TestProblemsView : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void testLoad();
void testAddModel();
void testSwitchTab();
void testRemoveModel();
void testAddRemoveProblems();
void testSetProblems();
private:
QTabWidget* tabWidget();
QScopedPointer<ProblemsView> m_view;
};
void TestProblemsView::initTestCase()
{
AutoTestShell::init();
TestCore::initialize(Core::NoUi);
ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
auto* model = new ProblemModel(pms);
IProblem::Ptr p(new DetectedProblem());
model->addProblem(p);
pms->addModel(QStringLiteral("MODEL1_ID"), QStringLiteral("MODEL1"), model);
m_view.reset(new ProblemsView());
}
void TestProblemsView::cleanupTestCase()
{
TestCore::shutdown();
}
void TestProblemsView::testLoad()
{
m_view->load();
// Check that the initial model's tab shows up
QTabWidget* tab = tabWidget();
QVERIFY(tab);
QCOMPARE(tab->count(), 1);
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL1 (1)"));
}
void TestProblemsView::testAddModel()
{
ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
pms->addModel(QStringLiteral("MODEL2_ID"), QStringLiteral("MODEL2"), new ProblemModel(pms));
QTabWidget* tab = tabWidget();
QVERIFY(tab);
QCOMPARE(tab->count(), 2);
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL1 (1)"));
QCOMPARE(tab->tabText(1), QStringLiteral("MODEL2 (0)"));
}
QVector<bool> visibilites(const QList<QAction*>& actions)
{
QVector<bool> visibilites;
for (auto action : actions) {
visibilites << action->isVisible();
}
return visibilites;
}
void TestProblemsView::testSwitchTab()
{
QTabWidget* tab = tabWidget();
QVERIFY(tab);
// Check that the current widget's actions are in the toolbar
QWidget* oldWidget = tab->currentWidget();
QVERIFY(oldWidget);
const auto oldVisibilites = visibilites(m_view->actions());
tab->setCurrentIndex(1);
// Check that the new widget's actions are in the toolbar
QWidget* newWidget = tab->currentWidget();
QVERIFY(newWidget);
QVERIFY(newWidget != oldWidget);
const auto newVisibilites = visibilites(m_view->actions());
QCOMPARE(oldVisibilites, newVisibilites);
}
void TestProblemsView::testRemoveModel()
{
// Remove the model
ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
ProblemModel* model = pms->findModel(QStringLiteral("MODEL1_ID"));
QVERIFY(model);
pms->removeModel(QStringLiteral("MODEL1_ID"));
delete model;
model = nullptr;
// Now let's see if the view has been updated!
QTabWidget* tab = tabWidget();
QVERIFY(tab);
QCOMPARE(tab->count(), 1);
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
}
void TestProblemsView::testAddRemoveProblems()
{
ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID"));
QVERIFY(model);
QTabWidget* tab = tabWidget();
QVERIFY(tab);
// Make sure there are no problems right now
model->clearProblems();
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
// Let's add some problems
int c = 0;
for (int i = 0; i < 3; i++) {
IProblem::Ptr p(new DetectedProblem());
model->addProblem(p);
c++;
// Check if the view has noticed the addition
QString label = QStringLiteral("MODEL2 (%1)").arg(c);
QCOMPARE(tab->tabText(0), label);
}
// Clear the problems
model->clearProblems();
// Check if the view has noticed the clear
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
}
void TestProblemsView::testSetProblems()
{
ProblemModelSet* pms = ICore::self()->languageController()->problemModelSet();
ProblemModel* model = pms->findModel(QStringLiteral("MODEL2_ID"));
QVERIFY(model);
QTabWidget* tab = tabWidget();
QVERIFY(tab);
// Make sure there are no problems right now
model->clearProblems();
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (0)"));
// Build a problem vector and set the problems
QVector<IProblem::Ptr> problems;
for (int i = 0; i < 3; i++) {
IProblem::Ptr p(new DetectedProblem());
problems.push_back(p);
}
model->setProblems(problems);
// Check if the view has noticed
QCOMPARE(tab->tabText(0), QStringLiteral("MODEL2 (3)"));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
QTabWidget* TestProblemsView::tabWidget()
{
auto* tab = m_view->findChild<QTabWidget*>();
return tab;
}
QTEST_MAIN(TestProblemsView)
#include "test_problemsview.moc"
|