aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/autotestplugin.cpp
blob: c74ad31d277e8a8af90a99ab881561fdeac9f932 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// 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 "autotestplugin.h"

#include "autotestconstants.h"
#include "autotesticons.h"
#include "autotesttr.h"
#include "projectsettingswidget.h"
#include "testcodeparser.h"
#include "testframeworkmanager.h"
#include "testnavigationwidget.h"
#include "testprojectsettings.h"
#include "testresultspane.h"
#include "testrunner.h"
#include "testsettingspage.h"
#include "testtreeitem.h"
#include "testtreemodel.h"

#include "boost/boosttestframework.h"
#include "catch/catchtestframework.h"
#include "ctest/ctesttool.h"
#include "gtest/gtestframework.h"
#include "qtest/datataglocatorfilter.h"
#include "qtest/qttestframework.h"
#include "quick/quicktestframework.h"

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/progressmanager/progressmanager.h>

#include <cplusplus/CppDocument.h>
#include <cplusplus/LookupContext.h>
#include <cplusplus/Overview.h>

#include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppmodelmanager.h>

#include <extensionsystem/iplugin.h>
#include <extensionsystem/pluginmanager.h>

#include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorericons.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/target.h>

#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>

#include <utils/algorithm.h>
#include <utils/processinterface.h>
#include <utils/textutils.h>
#include <utils/utilsicons.h>

#include <QAction>
#include <QList>
#include <QMainWindow>
#include <QMap>
#include <QMenu>
#include <QMessageBox>
#include <QTextCursor>

#ifdef WITH_TESTS
#include "autotestunittests.h"
#include "loadprojectscenario.h"
#endif

using namespace Core;
using namespace Utils;

namespace Autotest::Internal {

class AutotestPluginPrivate final : public QObject
{
    Q_OBJECT
public:
    AutotestPluginPrivate();
    ~AutotestPluginPrivate() final;

    TestResultsPane *m_resultsPane = nullptr;
    QMap<QString, ChoicePair> m_runconfigCache;

    void initializeMenuEntries();
    void onRunAllTriggered(TestRunMode mode);
    void onRunSelectedTriggered(TestRunMode mode);
    void onRunFailedTriggered();
    void onRunFileTriggered();
    void onRunUnderCursorTriggered(TestRunMode mode);
    void onDisableTemporarily(bool disable);

    TestCodeParser m_testCodeParser;
    TestTreeModel m_testTreeModel{&m_testCodeParser};
    TestRunner m_testRunner;
    DataTagLocatorFilter m_dataTagLocatorFilter;
#ifdef WITH_TESTS
    LoadProjectScenario m_loadProjectScenario{&m_testTreeModel};
#endif
};

static AutotestPluginPrivate *dd = nullptr;
static QHash<ProjectExplorer::Project *, TestProjectSettings *> s_projectSettings;

AutotestPluginPrivate::AutotestPluginPrivate()
{
    dd = this; // Needed as the code below access it via the static plugin interface
    initializeMenuEntries();
    TestFrameworkManager::registerTestFramework(&theQtTestFramework());
    TestFrameworkManager::registerTestFramework(&theQuickTestFramework());
    TestFrameworkManager::registerTestFramework(&theGTestFramework());
    TestFrameworkManager::registerTestFramework(&theBoostTestFramework());
    TestFrameworkManager::registerTestFramework(&theCatchFramework());

    TestFrameworkManager::registerTestTool(&theCTestTool());

    m_resultsPane = TestResultsPane::instance();

    setupAutotestProjectPanel();

    TestFrameworkManager::activateFrameworksAndToolsFromSettings();
    m_testTreeModel.synchronizeTestFrameworks();
    m_testTreeModel.synchronizeTestTools();

    auto sessionManager = ProjectExplorer::ProjectManager::instance();
    connect(sessionManager, &ProjectExplorer::ProjectManager::startupProjectChanged,
            this, [this] { m_runconfigCache.clear(); });

    connect(sessionManager, &ProjectExplorer::ProjectManager::aboutToRemoveProject,
            this, [](ProjectExplorer::Project *project) {
        const auto it = s_projectSettings.constFind(project);
        if (it != s_projectSettings.constEnd()) {
            delete it.value();
            s_projectSettings.erase(it);
        }
    });
}

AutotestPluginPrivate::~AutotestPluginPrivate()
{
    if (!s_projectSettings.isEmpty()) {
        qDeleteAll(s_projectSettings);
        s_projectSettings.clear();
    }

    delete m_resultsPane;
}

TestProjectSettings *projectSettings(ProjectExplorer::Project *project)
{
    auto &settings = s_projectSettings[project];
    if (!settings)
        settings = new TestProjectSettings(project);

    return settings;
}

void AutotestPluginPrivate::initializeMenuEntries()
{
    const Id menuId = Constants::MENU_ID;

    MenuBuilder(menuId)
        .setTitle(Tr::tr("&Tests"))
        .setOnAllDisabledBehavior(ActionContainer::Show)
        .addToContainer(Core::Constants::M_TOOLS);

    ActionBuilder(this, Constants::ACTION_RUN_ALL_ID)
        .setText(Tr::tr("Run &All Tests"))
        .setIcon(Utils::Icons::RUN_SMALL.icon())
        .setToolTip(Tr::tr("Run All Tests"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+A"), Tr::tr("Alt+Shift+T,Alt+A"))
        .addToContainer(menuId)
        .setEnabled(false)
        .addOnTriggered(this, [this] { onRunAllTriggered(TestRunMode::Run); });

    ActionBuilder(this, Constants::ACTION_RUN_ALL_NODEPLOY_ID)
        .setText(Tr::tr("Run All Tests Without Deployment"))
        .setIcon(Utils::Icons::RUN_SMALL.icon())
        .setToolTip(Tr::tr("Run All Tests Without Deployment"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+E"), Tr::tr("Alt+Shift+T,Alt+E"))
        .addToContainer(menuId)
        .setEnabled(false)
        .addOnTriggered(this, [this] { onRunAllTriggered(TestRunMode::RunWithoutDeploy); });

    ActionBuilder(this, Constants::ACTION_RUN_SELECTED_ID)
        .setText(Tr::tr("&Run Selected Tests"))
        .setIcon(Utils::Icons::RUN_SELECTED.icon())
        .setToolTip(Tr::tr("Run Selected Tests"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+R"), Tr::tr("Alt+Shift+T,Alt+R"))
        .addToContainer(menuId)
        .setEnabled(false)
        .addOnTriggered(this, [this] { onRunSelectedTriggered(TestRunMode::Run); });

    ActionBuilder(this, Constants::ACTION_RUN_SELECTED_NODEPLOY_ID)
        .setText(Tr::tr("&Run Selected Tests Without Deployment"))
        .setIcon(Utils::Icons::RUN_SELECTED.icon())
        .setToolTip(Tr::tr("Run Selected Tests Without Deployment"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+W"), Tr::tr("Alt+Shift+T,Alt+W"))
        .addToContainer(menuId)
        .setEnabled(false)
        .addOnTriggered(this, [this] { onRunSelectedTriggered(TestRunMode::RunWithoutDeploy); });

    ActionBuilder(this, Constants::ACTION_RUN_FAILED_ID)
        .setText(Tr::tr("Run &Failed Tests"))
        .setIcon(Icons::RUN_FAILED.icon())
        .setToolTip(Tr::tr("Run Failed Tests"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+F"), Tr::tr("Alt+Shift+T,Alt+F"))
        .addToContainer(menuId)
        .setEnabled(false)
        .addOnTriggered(this, [this] { onRunFailedTriggered(); });

    ActionBuilder(this, Constants::ACTION_RUN_FILE_ID)
        .setText(Tr::tr("Run Tests for &Current File"))
        .setIcon(Utils::Icons::RUN_FILE.icon())
        .setToolTip(Tr::tr("Run Tests for Current File"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+C"), Tr::tr("Alt+Shift+T,Alt+C"))
        .addToContainer(menuId)
        .setEnabled(false)
        .addOnTriggered(this, [this] { onRunFileTriggered(); });

    ActionBuilder(this, Constants::ACTION_DISABLE_TMP)
        .setText(Tr::tr("Disable Temporarily"))
        .setToolTip(Tr::tr("Disable scanning and other actions until explicitly rescanning, "
                           "re-enabling, or restarting Qt Creator."))
        .setCheckable(true)
        .addToContainer(menuId)
        .addOnTriggered(this, [this](bool on) { onDisableTemporarily(on); });

    ActionBuilder(this, Constants::ACTION_SCAN_ID)
        .setText(Tr::tr("Re&scan Tests"))
        .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+S"), Tr::tr("Alt+Shift+T,Alt+S"))
        .addToContainer(menuId)
        .addOnTriggered(this, [] {
            if (dd->m_testCodeParser.state() == TestCodeParser::DisabledTemporarily)
                dd->onDisableTemporarily(false);  // Rescan Test should explicitly re-enable
            else
                dd->m_testCodeParser.updateTestTree();
        });

    using namespace ProjectExplorer;
    connect(BuildManager::instance(), &BuildManager::buildStateChanged,
            this, &updateMenuItemsEnabledState);
    connect(BuildManager::instance(), &BuildManager::buildQueueFinished,
            this, &updateMenuItemsEnabledState);
    connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::runActionsUpdated,
            this, &updateMenuItemsEnabledState);
    connect(&dd->m_testTreeModel, &TestTreeModel::testTreeModelChanged,
            this, &updateMenuItemsEnabledState);
}

void AutotestPluginPrivate::onRunAllTriggered(TestRunMode mode)
{
    m_testRunner.runTests(mode, m_testTreeModel.getAllTestCases());
}

void AutotestPluginPrivate::onRunSelectedTriggered(TestRunMode mode)
{
    m_testRunner.runTests(mode, m_testTreeModel.getSelectedTests());
}

void AutotestPluginPrivate::onRunFailedTriggered()
{
    const QList<ITestConfiguration *> failed = m_testTreeModel.getFailedTests();
    if (failed.isEmpty()) // the framework might not be able to provide them
        return;
    m_testRunner.runTests(TestRunMode::Run, failed);
}

void AutotestPluginPrivate::onRunFileTriggered()
{
    const IDocument *document = EditorManager::currentDocument();
    if (!document)
        return;

    const FilePath &fileName = document->filePath();
    if (fileName.isEmpty())
        return;

    const QList<ITestConfiguration *> tests = m_testTreeModel.getTestsForFile(fileName);
    if (tests.isEmpty())
        return;

    m_testRunner.runTests(TestRunMode::Run, tests);
}

static QList<ITestConfiguration *> testItemsToTestConfigurations(const QList<ITestTreeItem *> &items,
                                                                TestRunMode mode)
{
    QList<ITestConfiguration *> configs;
    for (const ITestTreeItem * item : items) {
        if (ITestConfiguration *currentConfig = item->asConfiguration(mode))
            configs << currentConfig;
    }
    return configs;
}

void AutotestPluginPrivate::onRunUnderCursorTriggered(TestRunMode mode)
{
    TextEditor::BaseTextEditor *currentEditor = TextEditor::BaseTextEditor::currentTextEditor();
    QTC_ASSERT(currentEditor && currentEditor->textDocument(), return);
    const int line = currentEditor->currentLine();
    const FilePath filePath = currentEditor->textDocument()->filePath();

    const CPlusPlus::Snapshot snapshot = CppEditor::CppModelManager::snapshot();
    const CPlusPlus::Document::Ptr doc = snapshot.document(filePath);
    if (doc.isNull()) // not part of C++ snapshot
        return;

    CPlusPlus::Scope *scope = doc->scopeAt(line, currentEditor->currentColumn());
    QTextCursor cursor = currentEditor->editorWidget()->textCursor();
    cursor.select(QTextCursor::WordUnderCursor);
    const QString text = cursor.selectedText();

    while (scope && scope->asBlock())
        scope = scope->enclosingScope();
    if (scope && scope->asFunction()) { // class, namespace for further stuff?
        const QList<const CPlusPlus::Name *> fullName
                = CPlusPlus::LookupContext::fullyQualifiedName(scope);
        const QString funcName = CPlusPlus::Overview().prettyName(fullName);
        const TestFrameworks active = activeTestFrameworks();
        for (auto framework : active) {
            const QStringList testName = framework->testNameForSymbolName(funcName);
            if (!testName.size())
                continue;
            TestTreeItem *it = framework->rootNode()->findTestByNameAndFile(testName, filePath);
            if (it) {
                const QList<ITestConfiguration *> testsToRun
                        = testItemsToTestConfigurations({ it }, mode);
                if (!testsToRun.isEmpty()) {
                    m_testRunner.runTests(mode, testsToRun);
                    return;
                }
            }
        }
    }

    // general approach
    if (text.isEmpty())
        return; // Do not trigger when no name under cursor

    const QList<ITestTreeItem *> testsItems = m_testTreeModel.testItemsByName(text);
    if (testsItems.isEmpty())
        return; // Wrong location triggered

    // check whether we have been triggered on a test function definition
    QList<ITestTreeItem *> filteredItems = Utils::filtered(testsItems, [&](ITestTreeItem *it){
        return it->line() == line && it->filePath() == filePath;
    });

    if (filteredItems.size() == 0 && testsItems.size() > 1) {
        CPlusPlus::Scope *scope = doc->scopeAt(line, currentEditor->currentColumn());
        if (scope->asClass()) {
            const QList<const CPlusPlus::Name *> fullName
                    = CPlusPlus::LookupContext::fullyQualifiedName(scope);
            const QString className = CPlusPlus::Overview().prettyName(fullName);

            filteredItems = Utils::filtered(testsItems,
                                            [&text, &className](ITestTreeItem *it){
                return it->name() == text
                        && static_cast<ITestTreeItem *>(it->parent())->name() == className;
            });
        }
    }
    if ((filteredItems.size() != 1 && testsItems.size() > 1)
            && (mode == TestRunMode::Debug || mode == TestRunMode::DebugWithoutDeploy)) {
        MessageManager::writeFlashing(Tr::tr("Cannot debug multiple tests at once."));
        return;
    }
    const QList<ITestConfiguration *> testsToRun = testItemsToTestConfigurations(
                filteredItems.size() == 1 ? filteredItems : testsItems, mode);

    if (testsToRun.isEmpty()) {
        MessageManager::writeFlashing(Tr::tr("Selected test was not found (%1).").arg(text));
        return;
    }

    m_testRunner.runTests(mode, testsToRun);
}

void AutotestPluginPrivate::onDisableTemporarily(bool disable)
{
    if (disable) {
        // cancel running parse
        m_testCodeParser.aboutToShutdown(false);
        // clear model
        m_testTreeModel.removeAllTestItems();
        m_testTreeModel.removeAllTestToolItems();
        updateMenuItemsEnabledState();
    } else {
        // re-enable
        m_testCodeParser.setState(TestCodeParser::Idle);
        // trigger scan
        m_testCodeParser.updateTestTree();
    }
}

TestFrameworks activeTestFrameworks()
{
    ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
    TestFrameworks sorted;
    if (!project || projectSettings(project)->useGlobalSettings()) {
        sorted = Utils::filtered(TestFrameworkManager::registeredFrameworks(),
                                 &ITestFramework::active);
    } else { // we've got custom project settings
        const TestProjectSettings *settings = projectSettings(project);
        const QHash<ITestFramework *, bool> active = settings->activeFrameworks();
        sorted = Utils::filtered(TestFrameworkManager::registeredFrameworks(),
                                 [active](ITestFramework *framework) {
            return active.value(framework, false);
        });
    }
    return sorted;
}

void updateMenuItemsEnabledState()
{
    const ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
    const ProjectExplorer::Target *target = project ? project->activeTarget() : nullptr;
    const bool disabled = dd->m_testCodeParser.state() == TestCodeParser::DisabledTemporarily;
    const bool canScan = disabled || (!dd->m_testRunner.isTestRunning()
                                      && dd->m_testCodeParser.state() == TestCodeParser::Idle);
    const bool hasTests = dd->m_testTreeModel.hasTests();
    // avoid expensive call to PE::canRunStartupProject() - limit to minimum necessary checks
    const bool canRun = !disabled && hasTests && canScan
            && project && !project->needsConfiguration()
            && target && target->activeRunConfiguration()
            && !ProjectExplorer::BuildManager::isBuilding();
    const bool canRunFailed = canRun && dd->m_testTreeModel.hasFailedTests();

    ActionManager::command(Constants::ACTION_RUN_ALL_ID)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_SELECTED_ID)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_ALL_NODEPLOY_ID)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_SELECTED_NODEPLOY_ID)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_FAILED_ID)->action()->setEnabled(canRunFailed);
    ActionManager::command(Constants::ACTION_RUN_FILE_ID)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_SCAN_ID)->action()->setEnabled(canScan);

    ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT);
    if (!contextMenu)
        return; // When no context menu, actions do not exists

    ActionManager::command(Constants::ACTION_RUN_UCURSOR)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_UCURSOR_NODEPLOY)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_DBG_UCURSOR)->action()->setEnabled(canRun);
    ActionManager::command(Constants::ACTION_RUN_DBG_UCURSOR_NODEPLOY)->action()->setEnabled(canRun);
}

void cacheRunConfigChoice(const QString &buildTargetKey, const ChoicePair &choice)
{
    if (dd)
        dd->m_runconfigCache.insert(buildTargetKey, choice);
}

ChoicePair cachedChoiceFor(const QString &buildTargetKey)
{
    return dd ? dd->m_runconfigCache.value(buildTargetKey) : ChoicePair();
}

void clearChoiceCache()
{
    if (dd)
        dd->m_runconfigCache.clear();
}

void popupResultsPane()
{
    if (dd)
        dd->m_resultsPane->popup(Core::IOutputPane::NoModeSwitch);
}

QString wildcardPatternFromString(const QString &original)
{
    QString pattern = original;
    pattern.replace('\\', "\\\\");
    pattern.replace('.', "\\.");
    pattern.replace('^', "\\^").replace('$', "\\$");
    pattern.replace('(', "\\(").replace(')', "\\)");
    pattern.replace('[', "\\[").replace(']', "\\]");
    pattern.replace('{', "\\{").replace('}', "\\}");
    pattern.replace('+', "\\+");
    pattern.replace('*', ".*");
    pattern.replace('?', '.');
    return pattern;
}

bool ChoicePair::matches(const ProjectExplorer::RunConfiguration *rc) const
{
    return rc && rc->displayName() == displayName && rc->runnable().command.executable() == executable;
}

// AutotestPlugin

class AutotestPlugin final : public ExtensionSystem::IPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "AutoTest.json")

public:
    AutotestPlugin()
    {
        // needed to be used in QueuedConnection connects
        qRegisterMetaType<TestResult>();
        qRegisterMetaType<TestTreeItem *>();
        qRegisterMetaType<TestCodeLocationAndType>();
        // warm up meta type system to be able to read Qt::CheckState with persistent settings
        qRegisterMetaType<Qt::CheckState>();

        setupTestNavigationWidgetFactory();
    }

    ~AutotestPlugin() final
    {
        delete dd;
        dd = nullptr;
    }

    void initialize() final
    {
        setupTestSettingsPage();

        dd = new AutotestPluginPrivate;
    #ifdef WITH_TESTS
        ExtensionSystem::PluginManager::registerScenario("TestModelManagerInterface",
                       [] { return dd->m_loadProjectScenario(); });

        addTestCreator(createAutotestUnitTests);
    #endif
    }

    void extensionsInitialized()
    {
        ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT);
        if (!contextMenu) // if QC is started without CppEditor plugin
            return;

        const Id menuId = "Autotest.TestUnderCursor";
        ActionContainer * const runTestMenu = ActionManager::createMenu(menuId);
        runTestMenu->menu()->setTitle(Tr::tr("Run Test Under Cursor"));
        contextMenu->addSeparator();
        contextMenu->addMenu(runTestMenu);
        contextMenu->addSeparator();

        ActionBuilder(this, Constants::ACTION_RUN_UCURSOR)
            .setText(Tr::tr("&Run Test"))
            .setEnabled(false)
            .setIcon(Utils::Icons::RUN_SMALL.icon())
            .addToContainer(menuId)
            .addOnTriggered([] { dd->onRunUnderCursorTriggered(TestRunMode::Run); });

        ActionBuilder(this, Constants::ACTION_RUN_UCURSOR_NODEPLOY)
            .setText(Tr::tr("Run Test Without Deployment"))
            .setIcon(Utils::Icons::RUN_SMALL.icon())
            .setEnabled(false)
            .addToContainer(menuId)
            .addOnTriggered([] { dd->onRunUnderCursorTriggered(TestRunMode::RunWithoutDeploy); });

        ActionBuilder(this, Constants::ACTION_RUN_DBG_UCURSOR)
            .setText(Tr::tr("&Debug Test"))
            .setIcon(ProjectExplorer::Icons::DEBUG_START_SMALL.icon())
            .setEnabled(false)
            .addToContainer(menuId)
            .addOnTriggered([] { dd->onRunUnderCursorTriggered(TestRunMode::Debug); });

        ActionBuilder(this, Constants::ACTION_RUN_DBG_UCURSOR_NODEPLOY)
            .setText(Tr::tr("Debug Test Without Deployment"))
            .setIcon(ProjectExplorer::Icons::DEBUG_START_SMALL.icon())
            .setEnabled(false)
            .addToContainer(menuId)
            .addOnTriggered([] { dd->onRunUnderCursorTriggered(TestRunMode::DebugWithoutDeploy); });
    }

    ShutdownFlag aboutToShutdown() final
    {
        dd->m_testCodeParser.aboutToShutdown(true);
        dd->m_testTreeModel.disconnect();
        return SynchronousShutdown;
    }
};

} // Autotest::Internal

#include "autotestplugin.moc"