summaryrefslogtreecommitdiff
path: root/plugins/autotest/testresultspane.cpp
blob: d2425ce80b7c5955031a55cfe64599cadec06700 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at
** http://www.qt.io/contact-us
**
** This file is part of the Qt Creator Enterprise Auto Test Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include "autotestplugin.h"
#include "testresultspane.h"
#include "testresultmodel.h"
#include "testresultdelegate.h"
#include "testrunner.h"
#include "testsettings.h"
#include "testtreemodel.h"

#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icontext.h>

#include <texteditor/texteditor.h>

#include <utils/itemviews.h>
#include <utils/theme/theme.h>

#include <QDebug>
#include <QHBoxLayout>
#include <QMenu>
#include <QPlainTextEdit>
#include <QTabWidget>
#include <QToolButton>
#include <QVBoxLayout>

namespace Autotest {
namespace Internal {

TestResultsPane::TestResultsPane(QObject *parent) :
    Core::IOutputPane(parent),
    m_context(new Core::IContext(this)),
    m_wasVisibleBefore(false)
{
    m_outputPane = new QTabWidget;
    m_outputPane->setDocumentMode(true);

    m_outputWidget = new QWidget;
    QVBoxLayout *outputLayout = new QVBoxLayout;
    outputLayout->setMargin(0);
    outputLayout->setSpacing(0);
    m_outputWidget->setLayout(outputLayout);

    QPalette pal;
    pal.setColor(QPalette::Window,
                 Utils::creatorTheme()->color(Utils::Theme::InfoBarBackground));
    pal.setColor(QPalette::WindowText,
                 Utils::creatorTheme()->color(Utils::Theme::InfoBarText));
    m_summaryWidget = new QFrame;
    m_summaryWidget->setPalette(pal);
    m_summaryWidget->setAutoFillBackground(true);
    QHBoxLayout *layout = new QHBoxLayout;
    layout->setMargin(6);
    m_summaryWidget->setLayout(layout);
    m_summaryLabel = new QLabel;
    m_summaryLabel->setPalette(pal);
    layout->addWidget(m_summaryLabel);
    m_summaryWidget->setVisible(false);

    outputLayout->addWidget(m_summaryWidget);

    m_treeView = new Utils::TreeView(m_outputWidget);
    m_treeView->setHeaderHidden(true);
    m_treeView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    m_model = new TestResultModel(this);
    m_filterModel = new TestResultFilterModel(m_model, this);
    m_filterModel->setDynamicSortFilter(true);
    m_treeView->setModel(m_filterModel);
    TestResultDelegate *trd = new TestResultDelegate(this);
    m_treeView->setItemDelegate(trd);

    outputLayout->addWidget(m_treeView);

    createToolButtons();

    m_runnerServerLog = new QPlainTextEdit;
    m_runnerServerLog->setMaximumBlockCount(10000);

    m_outputPane->addTab(m_outputWidget, tr("Test Results"));
    m_outputPane->addTab(m_runnerServerLog, tr("Runner/Server Log"));

    connect(m_outputPane, &QTabWidget::currentChanged, [this] () {
        navigateStateChanged();
    });
    connect(m_treeView, &Utils::TreeView::activated, this, &TestResultsPane::onItemActivated);
    connect(m_treeView->selectionModel(), &QItemSelectionModel::currentChanged,
            trd, &TestResultDelegate::currentChanged);
    connect(TestRunner::instance(), &TestRunner::testRunStarted,
            this, &TestResultsPane::onTestRunStarted);
    connect(TestRunner::instance(), &TestRunner::testRunFinished,
            this, &TestResultsPane::onTestRunFinished);
}

void TestResultsPane::createToolButtons()
{
    m_runAll = new QToolButton(m_treeView);
    m_runAll->setIcon(QIcon(QLatin1String(":/images/run.png")));
    m_runAll->setToolTip(tr("Run All Tests"));
    m_runAll->setEnabled(false);
    connect(m_runAll, &QToolButton::clicked, this, &TestResultsPane::onRunAllTriggered);

    m_runSelected = new QToolButton(m_treeView);
    m_runSelected->setIcon(QIcon(QLatin1String(":/images/runselected.png")));
    m_runSelected->setToolTip(tr("Run Selected Tests"));
    m_runSelected->setEnabled(false);
    connect(m_runSelected, &QToolButton::clicked, this, &TestResultsPane::onRunSelectedTriggered);

    m_stopTestRun = new QToolButton(m_treeView);
    m_stopTestRun->setIcon(QIcon(QLatin1String(":/images/stop.png")));
    m_stopTestRun->setToolTip(tr("Stop Test Run"));
    m_stopTestRun->setEnabled(false);
    connect(m_stopTestRun, &QToolButton::clicked, TestRunner::instance(), &TestRunner::requestStopTestRun);

    m_filterButton = new QToolButton(m_treeView);
    m_filterButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_FILTER)));
    m_filterButton->setToolTip(tr("Filter Test Results"));
    m_filterButton->setProperty("noArrow", true);
    m_filterButton->setAutoRaise(true);
    m_filterButton->setPopupMode(QToolButton::InstantPopup);
    m_filterMenu = new QMenu(m_filterButton);
    initializeFilterMenu();
    connect(m_filterMenu, &QMenu::triggered, this, &TestResultsPane::filterMenuTriggered);
    m_filterButton->setMenu(m_filterMenu);
}

static TestResultsPane *m_instance = 0;

TestResultsPane *TestResultsPane::instance()
{
    if (!m_instance)
        m_instance = new TestResultsPane;
    return m_instance;
}

TestResultsPane::~TestResultsPane()
{
    delete m_treeView;
    m_instance = 0;
}

void TestResultsPane::addTestResult(const TestResult &result)
{
    m_model->addTestResult(result);
    if (!m_treeView->isVisible())
        popup(Core::IOutputPane::NoModeSwitch);
    flash();
    navigateStateChanged();
}

void TestResultsPane::addLogoutput(const QString &output)
{
    m_runnerServerLog->appendPlainText(output);
}

void TestResultsPane::updateSquishSummaryLabel()
{
    const int passes = m_model->resultTypeCount(Result::SQUISH_PASS)
            + m_model->resultTypeCount(Result::SQUISH_EXPECTED_FAIL);
    const int fails = m_model->resultTypeCount(Result::SQUISH_FAIL)
            + m_model->resultTypeCount(Result::SQUISH_UNEXPECTED_PASS);

    const QString labelText = tr("<p><b>Test summary:</b>&nbsp;&nbsp; %1 passes, %2 fails, "
                                 "%3 fatals, %4 errors, %5 warnings.</p>")
            .arg(passes).arg(fails).arg(m_model->resultTypeCount(Result::SQUISH_FATAL))
            .arg(m_model->resultTypeCount(Result::SQUISH_ERROR))
            .arg(m_model->resultTypeCount(Result::SQUISH_WARN));

    m_summaryLabel->setText(labelText);
    m_summaryWidget->setVisible(true);
}

QWidget *TestResultsPane::outputWidget(QWidget *parent)
{
    if (m_outputPane) {
        m_outputPane->setParent(parent);
    } else {
        qDebug() << "This should not happen...";
    }
    return m_outputPane;
}

QList<QWidget *> TestResultsPane::toolBarWidgets() const
{
    return QList<QWidget *>() << m_runAll << m_runSelected << m_stopTestRun << m_filterButton;
}

QString TestResultsPane::displayName() const
{
    return tr("Test Results");
}

int TestResultsPane::priorityInStatusBar() const
{
    return -666;
}

void TestResultsPane::clearContents()
{
    if (m_outputPane->currentIndex() == 0) {
        m_filterModel->clearTestResults();
        navigateStateChanged();
        m_summaryWidget->setVisible(false);
    } else if (m_outputPane->currentIndex() == 1) {
        m_runnerServerLog->clear();
    }
}

void TestResultsPane::visibilityChanged(bool visible)
{
    if (visible) {
        if (m_wasVisibleBefore)
            return;
        connect(TestTreeModel::instance(), &TestTreeModel::testTreeModelChanged,
                this, &TestResultsPane::onTestTreeModelChanged);
        m_wasVisibleBefore = true;
        TestTreeModel::instance()->enableParsing();
    } else {
        if (!m_wasVisibleBefore)
            return;
        disconnect(TestTreeModel::instance(), &TestTreeModel::testTreeModelChanged,
                   this, &TestResultsPane::onTestTreeModelChanged);
        m_wasVisibleBefore = false;
        TestTreeModel::instance()->disableParsing();
    }
}

void TestResultsPane::setFocus()
{
}

bool TestResultsPane::hasFocus() const
{
    return m_treeView->hasFocus() || m_runnerServerLog->hasFocus();
}

bool TestResultsPane::canFocus() const
{
    return true;
}

bool TestResultsPane::canNavigate() const
{
    return m_outputPane->currentIndex() == 0; // only support navigation for test results
}

bool TestResultsPane::canNext() const
{
    return m_filterModel->hasResults();
}

bool TestResultsPane::canPrevious() const
{
    return m_filterModel->hasResults();
}

void TestResultsPane::goToNext()
{
    if (!canNext())
        return;

    QModelIndex currentIndex = m_treeView->currentIndex();
    if (currentIndex.isValid()) {
        int row = currentIndex.row() + 1;
        if (row == m_filterModel->rowCount(QModelIndex()))
            row = 0;
        currentIndex = m_filterModel->index(row, 0, QModelIndex());
    } else {
        currentIndex = m_filterModel->index(0, 0, QModelIndex());
    }
    m_treeView->setCurrentIndex(currentIndex);
    onItemActivated(currentIndex);
}

void TestResultsPane::goToPrev()
{
    if (!canPrevious())
        return;

    QModelIndex currentIndex = m_treeView->currentIndex();
    if (currentIndex.isValid()) {
        int row = currentIndex.row() - 1;
        if (row < 0)
            row = m_filterModel->rowCount(QModelIndex()) - 1;
        currentIndex = m_filterModel->index(row, 0, QModelIndex());
    } else {
        currentIndex = m_filterModel->index(m_filterModel->rowCount(QModelIndex()) - 1, 0, QModelIndex());
    }
    m_treeView->setCurrentIndex(currentIndex);
    onItemActivated(currentIndex);
}

void TestResultsPane::onItemActivated(const QModelIndex &index)
{
    if (!index.isValid())
        return;

    const TestResult tr = m_filterModel->testResult(index);
    if (!tr.fileName().isEmpty())
        Core::EditorManager::openEditorAt(tr.fileName(), tr.line(), 0);
}

void TestResultsPane::onRunAllTriggered()
{
    TestRunner *runner = TestRunner::instance();
    runner->setSelectedTests(TestTreeModel::instance()->getAllTestCases());
    runner->runTests();
}

void TestResultsPane::onRunSelectedTriggered()
{
    TestRunner *runner = TestRunner::instance();
    runner->setSelectedTests(TestTreeModel::instance()->getSelectedTests());
    runner->runTests();
}

void TestResultsPane::initializeFilterMenu()
{
    const bool omitIntern = AutotestPlugin::instance()->qtestSettings()->omitInternalMssg;
    // FilterModel has all messages enabled by default
    if (omitIntern)
        m_filterModel->toggleTestResultType(Result::MESSAGE_INTERNAL);

    QMap<Result::Type, QString> textAndType;
    textAndType.insert(Result::PASS, tr("Pass"));
    textAndType.insert(Result::FAIL, tr("Fail"));
    textAndType.insert(Result::EXPECTED_FAIL, tr("Expected Fail"));
    textAndType.insert(Result::UNEXPECTED_PASS, tr("Unexpected Pass"));
    textAndType.insert(Result::SKIP, tr("Skip"));
    textAndType.insert(Result::BENCHMARK, tr("Benchmarks"));
    textAndType.insert(Result::MESSAGE_DEBUG, tr("Debug Messages"));
    textAndType.insert(Result::MESSAGE_WARN, tr("Warning Messages"));
    textAndType.insert(Result::MESSAGE_INTERNAL, tr("Internal Messages"));
    textAndType.insert(Result::SQUISH_LOG, tr("Log Messages"));
    foreach (Result::Type result, textAndType.keys()) {
        QAction *action = new QAction(m_filterMenu);
        action->setText(textAndType.value(result));
        action->setCheckable(true);
        action->setChecked(result != Result::MESSAGE_INTERNAL || !omitIntern);
        action->setData(result);
        m_filterMenu->addAction(action);
    }
    m_filterMenu->addSeparator();
    QAction *action = new QAction(m_filterMenu);
    action->setText(tr("Check All Filters"));
    action->setCheckable(false);
    m_filterMenu->addAction(action);
    connect(action, &QAction::triggered, this, &TestResultsPane::enableAllFilter);
}

void TestResultsPane::updateSummaryLabel()
{
    QString labelText = QString::fromLatin1("<p><b>Test summary:</b>&nbsp;&nbsp; %1 %2, %3 %4")
            .arg(QString::number(m_model->resultTypeCount(Result::PASS)), tr("passes"),
                 QString::number(m_model->resultTypeCount(Result::FAIL)), tr("fails"));
    int count = m_model->resultTypeCount(Result::UNEXPECTED_PASS);
    if (count)
        labelText.append(QString::fromLatin1(", %1 %2")
                         .arg(QString::number(count), tr("unexpected passes")));
    count = m_model->resultTypeCount(Result::EXPECTED_FAIL);
    if (count)
        labelText.append(QString::fromLatin1(", %1 %2")
                         .arg(QString::number(count), tr("expected fails")));
    count = m_model->resultTypeCount(Result::MESSAGE_FATAL);
    if (count)
        labelText.append(QString::fromLatin1(", %1 %2")
                         .arg(QString::number(count), tr("fatals")));
    count = m_model->resultTypeCount(Result::BLACKLISTED_FAIL)
            + m_model->resultTypeCount(Result::BLACKLISTED_PASS);
    if (count)
        labelText.append(QString::fromLatin1(", %1 %2")
                         .arg(QString::number(count), tr("blacklisted")));

    labelText.append(QLatin1String(".</p>"));
    m_summaryLabel->setText(labelText);
}

void TestResultsPane::enableAllFilter()
{
    foreach (QAction *action, m_filterMenu->actions()) {
        if (action->isCheckable())
            action->setChecked(true);
    }
    m_filterModel->enableAllResultTypes();
}

void TestResultsPane::filterMenuTriggered(QAction *action)
{
    m_filterModel->toggleTestResultType(TestResult::toResultType(action->data().value<int>()));
    navigateStateChanged();
}

void TestResultsPane::onTestRunStarted()
{
    m_stopTestRun->setEnabled(true);
    m_runAll->setEnabled(false);
    m_runSelected->setEnabled(false);
    m_summaryWidget->setVisible(false);
}

void TestResultsPane::onTestRunFinished()
{
    m_stopTestRun->setEnabled(false);
    m_runAll->setEnabled(true);
    m_runSelected->setEnabled(true);
    updateSummaryLabel();
    m_summaryWidget->setVisible(true);
    m_model->removeCurrentTestMessage();
}

void TestResultsPane::onTestTreeModelChanged()
{
    const bool enable = TestTreeModel::instance()->hasTests();
    m_runAll->setEnabled(enable);
    m_runSelected->setEnabled(enable);
}

} // namespace Internal
} // namespace Autotest