autotestplugin.cpp 6.45 KB
Newer Older
Christian Stenger's avatar
Christian Stenger committed
1
2
/****************************************************************************
**
Christian Stenger's avatar
Christian Stenger committed
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Christian Stenger's avatar
Christian Stenger committed
5
**
Christian Stenger's avatar
Christian Stenger committed
6
** This file is part of Qt Creator.
Christian Stenger's avatar
Christian Stenger committed
7
**
Christian Stenger's avatar
Christian Stenger committed
8
9
10
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
Christian Stenger's avatar
Christian Stenger committed
11
** Software or, alternatively, in accordance with the terms contained in
Christian Stenger's avatar
Christian Stenger committed
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Christian Stenger's avatar
Christian Stenger committed
15
**
Christian Stenger's avatar
Christian Stenger committed
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Christian Stenger's avatar
Christian Stenger committed
23
24
25
26
27
**
****************************************************************************/

#include "autotestplugin.h"
#include "autotestconstants.h"
Christian Stenger's avatar
Christian Stenger committed
28
#include "testcodeparser.h"
29
#include "testframeworkmanager.h"
30
#include "testrunner.h"
31
32
#include "testsettings.h"
#include "testsettingspage.h"
33
#include "testtreeitem.h"
Christian Stenger's avatar
Christian Stenger committed
34
35
#include "testtreeview.h"
#include "testtreemodel.h"
36
#include "testresultspane.h"
37
#include "testnavigationwidget.h"
Christian Stenger's avatar
Christian Stenger committed
38

39
40
41
42
#include "qtest/qttestframework.h"
#include "quick/quicktestframework.h"
#include "gtest/gtestframework.h"

Christian Stenger's avatar
Christian Stenger committed
43
44
45
46
47
48
49
#include <coreplugin/icore.h>
#include <coreplugin/icontext.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h>

50
51
#include <extensionsystem/pluginmanager.h>

Christian Stenger's avatar
Christian Stenger committed
52
53
54
55
56
57
58
#include <QAction>
#include <QMessageBox>
#include <QMainWindow>
#include <QMenu>

#include <QtPlugin>

59
60
61
62
#ifdef WITH_TESTS
#include "autotestunittests.h"
#endif

Christian Stenger's avatar
Christian Stenger committed
63
using namespace Autotest::Internal;
64
using namespace Core;
Christian Stenger's avatar
Christian Stenger committed
65

66
67
static AutotestPlugin *m_instance = 0;

Christian Stenger's avatar
Christian Stenger committed
68
AutotestPlugin::AutotestPlugin()
69
    : m_settings(new TestSettings)
Christian Stenger's avatar
Christian Stenger committed
70
{
71
72
    // needed to be used in QueuedConnection connects
    qRegisterMetaType<TestResult>();
73
    qRegisterMetaType<TestTreeItem *>();
74
75
    qRegisterMetaType<TestCodeLocationAndType>();

76
    m_instance = this;
Christian Stenger's avatar
Christian Stenger committed
77
78
79
80
}

AutotestPlugin::~AutotestPlugin()
{
81
    delete m_frameworkManager;
Christian Stenger's avatar
Christian Stenger committed
82
83
}

84
85
86
87
88
AutotestPlugin *AutotestPlugin::instance()
{
    return m_instance;
}

Tim Jenssen's avatar
Tim Jenssen committed
89
90
91
92
93
QSharedPointer<TestSettings> AutotestPlugin::settings() const
{
    return m_settings;
}

94
95
void AutotestPlugin::initializeMenuEntries()
{
96
    ActionContainer *menu = ActionManager::createMenu(Constants::MENU_ID);
97
    menu->menu()->setTitle(tr("&Tests"));
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

    QAction *action = new QAction(tr("Run &All Tests"), this);
    Command *command = ActionManager::registerAction(action, Constants::ACTION_RUN_ALL_ID);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+T,Alt+A")));
    connect(action, &QAction::triggered,
            this, &AutotestPlugin::onRunAllTriggered);
    menu->addAction(command);

    action = new QAction(tr("&Run Selected Tests"), this);
    command = ActionManager::registerAction(action, Constants::ACTION_RUN_SELECTED_ID);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+T,Alt+R")));
    connect(action, &QAction::triggered,
            this, &AutotestPlugin::onRunSelectedTriggered);
    menu->addAction(command);

    action = new QAction(tr("Re&scan Tests"), this);
    command = ActionManager::registerAction(action, Constants::ACTION_SCAN_ID);
Christian Stenger's avatar
Christian Stenger committed
115
116
117
    command->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+T,Alt+S")));
    connect(action, &QAction::triggered,
            TestTreeModel::instance()->parser(), &TestCodeParser::updateTestTree);
118
    menu->addAction(command);
119

120
121
122
123
    ActionContainer *toolsMenu = ActionManager::actionContainer(Core::Constants::M_TOOLS);
    toolsMenu->addMenu(menu);
    connect(toolsMenu->menu(), &QMenu::aboutToShow,
            this, &AutotestPlugin::updateMenuItemsEnabledState);
124
125
}

Christian Stenger's avatar
Christian Stenger committed
126
127
128
129
130
bool AutotestPlugin::initialize(const QStringList &arguments, QString *errorString)
{
    Q_UNUSED(arguments)
    Q_UNUSED(errorString)

131
    initializeMenuEntries();
Christian Stenger's avatar
Christian Stenger committed
132

133
134
135
136
137
    m_frameworkManager = TestFrameworkManager::instance();
    m_frameworkManager->registerTestFramework(new QtTestFramework);
    m_frameworkManager->registerTestFramework(new QuickTestFramework);
    m_frameworkManager->registerTestFramework(new GTestFramework);

138
    m_frameworkManager->synchronizeSettings(ICore::settings());
139
    addAutoReleasedObject(new TestSettingsPage(m_settings));
140
    addAutoReleasedObject(new TestNavigationWidgetFactory);
141
    addAutoReleasedObject(TestResultsPane::instance());
Christian Stenger's avatar
Christian Stenger committed
142

143
144
    if (m_settings->alwaysParse)
        TestTreeModel::instance()->enableParsingFromSettings();
145
146
    m_frameworkManager->activateFrameworksFromSettings(m_settings);
    TestTreeModel::instance()->syncTestFrameworks();
147

Christian Stenger's avatar
Christian Stenger committed
148
149
150
151
152
153
154
155
156
    return true;
}

void AutotestPlugin::extensionsInitialized()
{
}

ExtensionSystem::IPlugin::ShutdownFlag AutotestPlugin::aboutToShutdown()
{
157
    TestTreeModel::instance()->parser()->aboutToShutdown();
Christian Stenger's avatar
Christian Stenger committed
158
159
160
    return SynchronousShutdown;
}

161
162
163
164
165
void AutotestPlugin::onRunAllTriggered()
{
    TestRunner *runner = TestRunner::instance();
    TestTreeModel *model = TestTreeModel::instance();
    runner->setSelectedTests(model->getAllTestCases());
166
    runner->prepareToRunTests(TestRunner::Run);
167
168
169
170
171
172
173
}

void AutotestPlugin::onRunSelectedTriggered()
{
    TestRunner *runner = TestRunner::instance();
    TestTreeModel *model = TestTreeModel::instance();
    runner->setSelectedTests(model->getSelectedTests());
174
    runner->prepareToRunTests(TestRunner::Run);
175
176
177
178
}

void AutotestPlugin::updateMenuItemsEnabledState()
{
179
180
    const bool enabled = !TestRunner::instance()->isTestRunning()
            && TestTreeModel::instance()->parser()->state() == TestCodeParser::Idle;
181
182
183
184
185
186
187
    const bool hasTests = TestTreeModel::instance()->hasTests();

    ActionManager::command(Constants::ACTION_RUN_ALL_ID)->action()->setEnabled(enabled && hasTests);
    ActionManager::command(Constants::ACTION_RUN_SELECTED_ID)->action()->setEnabled(enabled && hasTests);
    ActionManager::command(Constants::ACTION_SCAN_ID)->action()->setEnabled(enabled);
}

188
189
190
191
192
193
194
195
QList<QObject *> AutotestPlugin::createTestObjects() const
{
    QList<QObject *> tests;
#ifdef WITH_TESTS
    tests << new AutoTestUnitTests(TestTreeModel::instance());
#endif
    return tests;
}