aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/testsettingspage.cpp
blob: 2533ed4b7f3b89d577fb15f369f6bbb7cafb1a84 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "testsettingspage.h"

#include "autotestconstants.h"
#include "autotestplugin.h"
#include "autotesttr.h"
#include "testframeworkmanager.h"
#include "testsettings.h"
#include "testtreemodel.h"

#include <coreplugin/icore.h>
#include <utils/algorithm.h>
#include <utils/id.h>
#include <utils/infolabel.h>
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>
#include <utils/utilsicons.h>

#include <QCheckBox>
#include <QComboBox>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QSpinBox>
#include <QTreeWidget>
#include <QVBoxLayout>
#include <QWidget>

namespace Autotest {
namespace Internal {

class TestSettingsWidget : public QWidget
{
public:
    explicit TestSettingsWidget(QWidget *parent = nullptr);

    void setSettings(const TestSettings &settings);
    TestSettings settings() const;

private:
    void populateFrameworksListWidget(const QHash<Utils::Id, bool> &frameworks,
                                      const QHash<Utils::Id, bool> &testTools);
    void testSettings(TestSettings &settings) const;
    void testToolsSettings(TestSettings &settings) const;
    void onFrameworkItemChanged();

    QCheckBox *m_omitInternalMsgCB;
    QCheckBox *m_omitRunConfigWarnCB;
    QCheckBox *m_limitResultOutputCB;
    QCheckBox *m_limitResultDescriptionCb;
    QSpinBox *m_limitResultDescriptionSpinBox;
    QCheckBox *m_openResultsOnStartCB;
    QCheckBox *m_openResultsOnFinishCB;
    QCheckBox *m_openResultsOnFailCB;
    QCheckBox *m_autoScrollCB;
    QCheckBox *m_displayAppCB;
    QCheckBox *m_processArgsCB;
    QComboBox *m_runAfterBuildCB;
    QSpinBox *m_timeoutSpin;
    QTreeWidget *m_frameworkTreeWidget;
    Utils::InfoLabel *m_frameworksWarn;
};

TestSettingsWidget::TestSettingsWidget(QWidget *parent)
    : QWidget(parent)
{
    resize(586, 469);

    m_omitInternalMsgCB = new QCheckBox(Tr::tr("Omit internal messages"));
    m_omitInternalMsgCB->setChecked(true);
    m_omitInternalMsgCB->setToolTip(Tr::tr("Hides internal messages by default. "
        "You can still enable them by using the test results filter."));

    m_omitRunConfigWarnCB = new QCheckBox(Tr::tr("Omit run configuration warnings"));
    m_omitRunConfigWarnCB->setToolTip(Tr::tr("Hides warnings related to a deduced run configuration."));

    m_limitResultOutputCB = new QCheckBox(Tr::tr("Limit result output"));
    m_limitResultOutputCB->setChecked(true);
    m_limitResultOutputCB->setToolTip(Tr::tr("Limits result output to 100000 characters."));

    m_limitResultDescriptionCb = new QCheckBox(Tr::tr("Limit result description:"));
    m_limitResultDescriptionCb->setToolTip(
        Tr::tr("Limit number of lines shown in test result tooltip and description."));

    m_limitResultDescriptionSpinBox = new QSpinBox;
    m_limitResultDescriptionSpinBox->setEnabled(false);
    m_limitResultDescriptionSpinBox->setMinimum(1);
    m_limitResultDescriptionSpinBox->setMaximum(1000000);
    m_limitResultDescriptionSpinBox->setValue(10);

    m_openResultsOnStartCB = new QCheckBox(Tr::tr("Open results when tests start"));
    m_openResultsOnStartCB->setToolTip(
        Tr::tr("Displays test results automatically when tests are started."));

    m_openResultsOnFinishCB = new QCheckBox(Tr::tr("Open results when tests finish"));
    m_openResultsOnFinishCB->setChecked(true);
    m_openResultsOnFinishCB->setToolTip(
        Tr::tr("Displays test results automatically when tests are finished."));

    m_openResultsOnFailCB = new QCheckBox(Tr::tr("Only for unsuccessful test runs"));
    m_openResultsOnFailCB->setToolTip(
        Tr::tr("Displays test results only if the test run contains failed, fatal or unexpectedly passed tests."));

    m_autoScrollCB = new QCheckBox(Tr::tr("Automatically scroll results"));
    m_autoScrollCB->setChecked(true);
    m_autoScrollCB->setToolTip(Tr::tr("Automatically scrolls down when new items are added and scrollbar is at bottom."));

    m_displayAppCB = new QCheckBox(Tr::tr("Group results by application"));

    m_processArgsCB = new QCheckBox(Tr::tr("Process arguments"));
    m_processArgsCB->setToolTip(
        Tr::tr("Allow passing arguments specified on the respective run configuration.\n"
           "Warning: this is an experimental feature and might lead to failing to execute the test executable."));

    m_runAfterBuildCB = new QComboBox;
    m_runAfterBuildCB->setToolTip(Tr::tr("Runs chosen tests automatically if a build succeeded."));
    m_runAfterBuildCB->addItem(Tr::tr("None"));
    m_runAfterBuildCB->addItem(Tr::tr("All"));
    m_runAfterBuildCB->addItem(Tr::tr("Selected"));

    auto timeoutLabel = new QLabel(Tr::tr("Timeout:"));
    timeoutLabel->setToolTip(Tr::tr("Timeout used when executing each test case."));

    m_timeoutSpin = new QSpinBox;
    m_timeoutSpin->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_timeoutSpin->setRange(5, 36000);
    m_timeoutSpin->setValue(60);
    m_timeoutSpin->setSuffix(Tr::tr(" s"));
    m_timeoutSpin->setToolTip(
        Tr::tr("Timeout used when executing test cases. This will apply "
               "for each test case on its own, not the whole project."));

    m_frameworkTreeWidget = new QTreeWidget;
    m_frameworkTreeWidget->setRootIsDecorated(false);
    m_frameworkTreeWidget->setHeaderHidden(false);
    m_frameworkTreeWidget->setColumnCount(2);
    m_frameworkTreeWidget->header()->setDefaultSectionSize(150);
    m_frameworkTreeWidget->setToolTip(Tr::tr("Selects the test frameworks to be handled by the AutoTest plugin."));

    QTreeWidgetItem *item = m_frameworkTreeWidget->headerItem();
    item->setText(0, Tr::tr("Framework"));
    item->setToolTip(0, Tr::tr("Selects the test frameworks to be handled by the AutoTest plugin."));
    item->setText(1, Tr::tr("Group"));
    item->setToolTip(1, Tr::tr("Enables grouping of test cases."));

    m_frameworksWarn = new Utils::InfoLabel;
    m_frameworksWarn->setVisible(false);
    m_frameworksWarn->setElideMode(Qt::ElideNone);
    m_frameworksWarn->setType(Utils::InfoLabel::Warning);

    using namespace Utils::Layouting;

    PushButton resetChoicesButton {
        text(Tr::tr("Reset Cached Choices")),
        tooltip(Tr::tr("Clear all cached choices of run configurations for "
                       "tests where the executable could not be deduced.")),
        onClicked([] { AutotestPlugin::clearChoiceCache(); }, this)
    };

    Group generalGroup {
        title(Tr::tr("General")),
        Column {
            m_omitInternalMsgCB,
            m_omitRunConfigWarnCB,
            m_limitResultOutputCB,
            Row { m_limitResultDescriptionCb, m_limitResultDescriptionSpinBox, st },
            m_openResultsOnStartCB,
            m_openResultsOnFinishCB,
            Row { Space(20), m_openResultsOnFailCB },
            m_autoScrollCB,
            m_displayAppCB,
            m_processArgsCB,
            Row { Tr::tr("Automatically run"), m_runAfterBuildCB, st },
            Row { timeoutLabel, m_timeoutSpin, st },
            Row { resetChoicesButton, st }
         }
    };

    Group activeFrameworks {
        title(Tr::tr("Active Test Frameworks")),
        Column {
            m_frameworkTreeWidget,
            m_frameworksWarn,
        }
    };

    Column {
        Row {
            Column { generalGroup, st },
            Column { activeFrameworks, st }
        },
        st
    }.attachTo(this);

    connect(m_frameworkTreeWidget, &QTreeWidget::itemChanged,
            this, &TestSettingsWidget::onFrameworkItemChanged);
    connect(m_openResultsOnFinishCB, &QCheckBox::toggled,
            m_openResultsOnFailCB, &QCheckBox::setEnabled);
    connect(m_limitResultDescriptionCb, &QCheckBox::toggled,
            m_limitResultDescriptionSpinBox, &QSpinBox::setEnabled);
}

void TestSettingsWidget::setSettings(const TestSettings &settings)
{
    m_timeoutSpin->setValue(settings.timeout / 1000); // we store milliseconds
    m_omitInternalMsgCB->setChecked(settings.omitInternalMssg);
    m_omitRunConfigWarnCB->setChecked(settings.omitRunConfigWarn);
    m_limitResultOutputCB->setChecked(settings.limitResultOutput);
    m_limitResultDescriptionCb->setChecked(settings.limitResultDescription);
    m_limitResultDescriptionSpinBox->setEnabled(settings.limitResultDescription);
    m_limitResultDescriptionSpinBox->setValue(settings.resultDescriptionMaxSize);
    m_autoScrollCB->setChecked(settings.autoScroll);
    m_processArgsCB->setChecked(settings.processArgs);
    m_displayAppCB->setChecked(settings.displayApplication);
    m_openResultsOnStartCB->setChecked(settings.popupOnStart);
    m_openResultsOnFinishCB->setChecked(settings.popupOnFinish);
    m_openResultsOnFailCB->setChecked(settings.popupOnFail);
    m_runAfterBuildCB->setCurrentIndex(int(settings.runAfterBuild));
    populateFrameworksListWidget(settings.frameworks, settings.tools);
}

TestSettings TestSettingsWidget::settings() const
{
    TestSettings result;
    result.timeout = m_timeoutSpin->value() * 1000; // we display seconds
    result.omitInternalMssg = m_omitInternalMsgCB->isChecked();
    result.omitRunConfigWarn = m_omitRunConfigWarnCB->isChecked();
    result.limitResultOutput = m_limitResultOutputCB->isChecked();
    result.limitResultDescription = m_limitResultDescriptionCb->isChecked();
    result.resultDescriptionMaxSize = m_limitResultDescriptionSpinBox->value();
    result.autoScroll = m_autoScrollCB->isChecked();
    result.processArgs = m_processArgsCB->isChecked();
    result.displayApplication = m_displayAppCB->isChecked();
    result.popupOnStart = m_openResultsOnStartCB->isChecked();
    result.popupOnFinish = m_openResultsOnFinishCB->isChecked();
    result.popupOnFail = m_openResultsOnFailCB->isChecked();
    result.runAfterBuild = RunAfterBuildMode(m_runAfterBuildCB->currentIndex());
    testSettings(result);
    testToolsSettings(result);
    return result;
}

enum TestBaseInfo
{
    BaseId = Qt::UserRole,
    BaseType
};

void TestSettingsWidget::populateFrameworksListWidget(const QHash<Utils::Id, bool> &frameworks,
                                                      const QHash<Utils::Id, bool> &testTools)
{
    const TestFrameworks &registered = TestFrameworkManager::registeredFrameworks();
    m_frameworkTreeWidget->clear();
    for (const ITestFramework *framework : registered) {
        const Utils::Id id = framework->id();
        auto item = new QTreeWidgetItem(m_frameworkTreeWidget, {framework->displayName()});
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
        item->setCheckState(0, frameworks.value(id) ? Qt::Checked : Qt::Unchecked);
        item->setData(0, BaseId, id.toSetting());
        item->setData(0, BaseType, ITestBase::Framework);
        item->setData(1, Qt::CheckStateRole, framework->grouping() ? Qt::Checked : Qt::Unchecked);
        item->setToolTip(0, Tr::tr("Enable or disable test frameworks to be handled by the "
                                   "AutoTest plugin."));
        QString toolTip = framework->groupingToolTip();
        if (toolTip.isEmpty())
            toolTip = Tr::tr("Enable or disable grouping of test cases by folder.");
        item->setToolTip(1, toolTip);
    }
    // ...and now the test tools
    const TestTools &registeredTools = TestFrameworkManager::registeredTestTools();
    for (const ITestTool *testTool : registeredTools) {
        const Utils::Id id = testTool->id();
        auto item = new QTreeWidgetItem(m_frameworkTreeWidget, {testTool->displayName()});
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
        item->setCheckState(0, testTools.value(id) ? Qt::Checked : Qt::Unchecked);
        item->setData(0, BaseId, id.toSetting());
        item->setData(0, BaseType, ITestBase::Tool);
    }
}

void TestSettingsWidget::testSettings(TestSettings &settings) const
{
    const QAbstractItemModel *model = m_frameworkTreeWidget->model();
    QTC_ASSERT(model, return);
    const int itemCount = TestFrameworkManager::registeredFrameworks().size();
    QTC_ASSERT(itemCount <= model->rowCount(), return);
    for (int row = 0; row < itemCount; ++row) {
        QModelIndex idx = model->index(row, 0);
        const Utils::Id id = Utils::Id::fromSetting(idx.data(BaseId));
        settings.frameworks.insert(id, idx.data(Qt::CheckStateRole) == Qt::Checked);
        idx = model->index(row, 1);
        settings.frameworksGrouping.insert(id, idx.data(Qt::CheckStateRole) == Qt::Checked);
    }
}

void TestSettingsWidget::testToolsSettings(TestSettings &settings) const
{
    const QAbstractItemModel *model = m_frameworkTreeWidget->model();
    QTC_ASSERT(model, return);
    // frameworks are listed before tools
    int row = TestFrameworkManager::registeredFrameworks().size();
    const int end = model->rowCount();
    QTC_ASSERT(row <= end, return);
    for ( ; row < end; ++row) {
        const QModelIndex idx = model->index(row, 0);
        const Utils::Id id = Utils::Id::fromSetting(idx.data(BaseId));
        settings.tools.insert(id, idx.data(Qt::CheckStateRole) == Qt::Checked);
    }
}

void TestSettingsWidget::onFrameworkItemChanged()
{
    bool atLeastOneEnabled = false;
    int mixed = ITestBase::None;
    if (QAbstractItemModel *model = m_frameworkTreeWidget->model()) {
        for (int row = 0, count = model->rowCount(); row < count; ++row) {
            const QModelIndex idx = model->index(row, 0);
            if (idx.data(Qt::CheckStateRole) == Qt::Checked) {
                atLeastOneEnabled = true;
                mixed |= idx.data(BaseType).toInt();
            }
        }
    }

    if (!atLeastOneEnabled || (mixed == (ITestBase::Framework | ITestBase::Tool))) {
        if (!atLeastOneEnabled) {
            m_frameworksWarn->setText(Tr::tr("No active test frameworks or tools."));
            m_frameworksWarn->setToolTip(Tr::tr("You will not be able to use the AutoTest plugin "
                                                "without having at least one active test framework."));
        } else {
            m_frameworksWarn->setText(Tr::tr("Mixing test frameworks and test tools."));
            m_frameworksWarn->setToolTip(Tr::tr("Mixing test frameworks and test tools can lead "
                                                "to duplicating run information when using "
                                                "\"Run All Tests\", for example."));
        }
    }
    m_frameworksWarn->setVisible(!atLeastOneEnabled
                                    || (mixed == (ITestBase::Framework | ITestBase::Tool)));
}

TestSettingsPage::TestSettingsPage(TestSettings *settings)
    : m_settings(settings)
{
    setId(Constants::AUTOTEST_SETTINGS_ID);
    setDisplayName(Tr::tr("General"));
    setCategory(Constants::AUTOTEST_SETTINGS_CATEGORY);
    setDisplayCategory(Tr::tr("Testing"));
    setCategoryIconPath(":/autotest/images/settingscategory_autotest.png");
}

QWidget *TestSettingsPage::widget()
{
    if (!m_widget) {
        m_widget = new TestSettingsWidget;
        m_widget->setSettings(*m_settings);
    }
    return m_widget;
}

void TestSettingsPage::apply()
{
    if (!m_widget) // page was not shown at all
        return;
    const TestSettings newSettings = m_widget->settings();
    const QList<Utils::Id> changedIds = Utils::filtered(newSettings.frameworksGrouping.keys(),
                                                       [newSettings, this] (const Utils::Id &id) {
        return newSettings.frameworksGrouping[id] != m_settings->frameworksGrouping[id];
    });
    *m_settings = newSettings;
    m_settings->toSettings(Core::ICore::settings());

    for (ITestFramework *framework : TestFrameworkManager::registeredFrameworks()) {
        framework->setActive(m_settings->frameworks.value(framework->id(), false));
        framework->setGrouping(m_settings->frameworksGrouping.value(framework->id(), false));
    }

    for (ITestTool *testTool : TestFrameworkManager::registeredTestTools())
        testTool->setActive(m_settings->tools.value(testTool->id(), false));

    TestTreeModel::instance()->synchronizeTestFrameworks();
    TestTreeModel::instance()->synchronizeTestTools();
    if (!changedIds.isEmpty())
        TestTreeModel::instance()->rebuild(changedIds);
}

} // namespace Internal
} // namespace Autotest