aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/testsettingspage.cpp
blob: f74716b8e2050fca902ed3e3ab3120b6d08745db (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only 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/dialogs/ioptionspage.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 <QHeaderView>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QTreeWidget>

using namespace Utils;

namespace Autotest::Internal {

class TestSettingsWidget : public Core::IOptionsPageWidget
{
public:
    TestSettingsWidget();

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

    QTreeWidget *m_frameworkTreeWidget;
    InfoLabel *m_frameworksWarn;
};

TestSettingsWidget::TestSettingsWidget()
{
    auto scanThreadLabel = new QLabel(Tr::tr("Scan threads:"));
    scanThreadLabel->setToolTip("Number of worker threads used when scanning for tests.");

    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 InfoLabel;
    m_frameworksWarn->setVisible(false);
    m_frameworksWarn->setElideMode(Qt::ElideNone);
    m_frameworksWarn->setType(InfoLabel::Warning);

    using namespace 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(&clearChoiceCache, this)
    };

    TestSettings &s = Internal::testSettings();
    Group generalGroup {
        title(Tr::tr("General")),
        Column {
            Row { scanThreadLabel, s.scanThreadLimit, st },
            s.omitInternalMsg,
            s.omitRunConfigWarn,
            s.limitResultOutput,
            Row { s.limitResultDescription, s.resultDescriptionMaxSize, st },
            s.popupOnStart,
            s.popupOnFinish,
            Row { Space(20), s.popupOnFail },
            s.autoScroll,
            s.displayApplication,
            s.processArgs,
            Row { Tr::tr("Automatically run"), s.runAfterBuild, st },
            Row { s.useTimeout, s.timeout, 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);

    populateFrameworksListWidget(s.frameworks, s.tools);

    setOnApply([this] {
        TestSettings &s = Internal::testSettings();

        NonAspectSettings tmp;
        testSettings(tmp);
        testToolsSettings(tmp);

        const QList<Utils::Id> changedIds = Utils::filtered(tmp.frameworksGrouping.keys(),
           [&tmp, &s](Utils::Id id) {
            return tmp.frameworksGrouping[id] != s.frameworksGrouping[id];
        });

        testSettings(s);
        testToolsSettings(s);
        s.toSettings();

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

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

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

    setOnCancel([] { Internal::testSettings().cancel(); });
}

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

void TestSettingsWidget::populateFrameworksListWidget(const QHash<Id, bool> &frameworks,
                                                      const QHash<Id, bool> &testTools)
{
    const TestFrameworks &registered = TestFrameworkManager::registeredFrameworks();
    m_frameworkTreeWidget->clear();
    for (const ITestFramework *framework : registered) {
        const 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 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(NonAspectSettings &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 Id id = 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(NonAspectSettings &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 Id id = 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

class TestSettingsPage final : public Core::IOptionsPage
{
public:
    TestSettingsPage()
    {
        setId(Constants::AUTOTEST_SETTINGS_ID);
        setDisplayName(Tr::tr("General"));
        setCategory(Constants::AUTOTEST_SETTINGS_CATEGORY);
        setDisplayCategory(Tr::tr("Testing"));
        setCategoryIconPath(":/autotest/images/settingscategory_autotest.png");
        setWidgetCreator([] { return new TestSettingsWidget; });
    }
};

void setupTestSettingsPage()
{
    static TestSettingsPage theTestSettingsPage;
}

} // Autotest::Internal