diff options
author | Pawel Rutka <prutka13@gmail.com> | 2018-02-16 17:57:37 +0100 |
---|---|---|
committer | pawelrutka <prutka13@gmail.com> | 2018-03-16 10:50:21 +0000 |
commit | c8f1da095d22097ed7a8ef0fc84b4ceb7a752cb4 (patch) | |
tree | cdb47337b1f2a1e635cc0215eaf53b9e53d2910e /src | |
parent | 190c5083b7fe449ee1848ff8173931e6eb269ae5 (diff) | |
download | qt-creator-c8f1da095d22097ed7a8ef0fc84b4ceb7a752cb4.tar.gz |
AutoTest: Add possibility to trigger test run from source
Change-Id: Iceed69747de64d76f34451d41f719c8dbdd81e44
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/autotest/autotest.qbs | 1 | ||||
-rw-r--r-- | src/plugins/autotest/autotest_dependencies.pri | 3 | ||||
-rw-r--r-- | src/plugins/autotest/autotestconstants.h | 3 | ||||
-rw-r--r-- | src/plugins/autotest/autotestplugin.cpp | 67 | ||||
-rw-r--r-- | src/plugins/autotest/autotestplugin.h | 2 | ||||
-rw-r--r-- | src/plugins/autotest/testrunner.cpp | 14 | ||||
-rw-r--r-- | src/plugins/autotest/testtreeitem.cpp | 14 | ||||
-rw-r--r-- | src/plugins/autotest/testtreeitem.h | 4 | ||||
-rw-r--r-- | src/plugins/autotest/testtreemodel.cpp | 39 | ||||
-rw-r--r-- | src/plugins/autotest/testtreemodel.h | 3 |
10 files changed, 132 insertions, 18 deletions
diff --git a/src/plugins/autotest/autotest.qbs b/src/plugins/autotest/autotest.qbs index 4b8c13f257..62c5d9fd8b 100644 --- a/src/plugins/autotest/autotest.qbs +++ b/src/plugins/autotest/autotest.qbs @@ -11,6 +11,7 @@ QtcPlugin { Depends { name: "QmlJSTools" } Depends { name: "Utils" } Depends { name: "Debugger" } + Depends { name: "TextEditor" } pluginTestDepends: [ "QbsProjectManager", diff --git a/src/plugins/autotest/autotest_dependencies.pri b/src/plugins/autotest/autotest_dependencies.pri index 1580d58e09..8c8a6cc126 100644 --- a/src/plugins/autotest/autotest_dependencies.pri +++ b/src/plugins/autotest/autotest_dependencies.pri @@ -5,7 +5,8 @@ QTC_PLUGIN_DEPENDS += \ projectexplorer \ cpptools \ qmljstools \ - debugger + debugger \ + texteditor QTC_LIB_DEPENDS += \ cplusplus \ diff --git a/src/plugins/autotest/autotestconstants.h b/src/plugins/autotest/autotestconstants.h index 5a1accdde7..19dde06e97 100644 --- a/src/plugins/autotest/autotestconstants.h +++ b/src/plugins/autotest/autotestconstants.h @@ -33,6 +33,8 @@ namespace Constants { const char ACTION_SCAN_ID[] = "AutoTest.ScanAction"; const char ACTION_RUN_ALL_ID[] = "AutoTest.RunAll"; const char ACTION_RUN_SELECTED_ID[] = "AutoTest.RunSelected"; +const char ACTION_RUN_UCURSOR[] = "AutoTest.RunUnderCursor"; +const char ACTION_RUN_DBG_UCURSOR[] = "AutoTest.RunDebugUnderCursor"; const char MENU_ID[] = "AutoTest.Menu"; const char AUTOTEST_ID[] = "AutoTest.ATP"; const char AUTOTEST_CONTEXT[] = "Auto Tests"; @@ -41,7 +43,6 @@ const char TASK_PARSE[] = "AutoTest.Task.Parse"; const char AUTOTEST_SETTINGS_CATEGORY[] = "ZY.Tests"; const char AUTOTEST_SETTINGS_TR[] = QT_TRANSLATE_NOOP("AutoTest", "Testing"); const char FRAMEWORK_PREFIX[] = "AutoTest.Framework."; - const char SETTINGSPAGE_PREFIX[] = "A.AutoTest."; const char SETTINGSGROUP[] = "Autotest"; } // namespace Constants diff --git a/src/plugins/autotest/autotestplugin.cpp b/src/plugins/autotest/autotestplugin.cpp index 1d850f1240..6d6957c83c 100644 --- a/src/plugins/autotest/autotestplugin.cpp +++ b/src/plugins/autotest/autotestplugin.cpp @@ -47,16 +47,22 @@ #include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/command.h> #include <coreplugin/coreconstants.h> +#include <coreplugin/messagemanager.h> +#include <cppeditor/cppeditorconstants.h> #include <extensionsystem/pluginmanager.h> #include <projectexplorer/buildmanager.h> #include <projectexplorer/projectexplorer.h> +#include <projectexplorer/projectexplorericons.h> +#include <texteditor/texteditor.h> +#include <utils/textutils.h> #include <utils/utilsicons.h> #include <QAction> +#include <QList> #include <QMessageBox> #include <QMainWindow> #include <QMenu> - +#include <QTextCursor> #include <QtPlugin> #ifdef WITH_TESTS @@ -164,6 +170,29 @@ bool AutotestPlugin::initialize(const QStringList &arguments, QString *errorStri void AutotestPlugin::extensionsInitialized() { + ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT); + QTC_ASSERT(contextMenu, return); + + QAction *action = new QAction(tr("&Run Test Under Cursor"), this); + action->setEnabled(false); + action->setIcon(Utils::Icons::RUN_SMALL_TOOLBAR.icon()); + + Command *command = ActionManager::registerAction(action, Constants::ACTION_RUN_UCURSOR); + connect(action, &QAction::triggered, std::bind(&AutotestPlugin::onRunUnderCursorTriggered, this, + TestRunMode::Run)); + contextMenu->addSeparator(); + contextMenu->addAction(command); + + action = new QAction(tr("&Debug Test Under Cursor"), this);; + action->setEnabled(false); + action->setIcon(ProjectExplorer::Icons::DEBUG_START_SMALL.icon()); + + + command = ActionManager::registerAction(action, Constants::ACTION_RUN_DBG_UCURSOR); + connect(action, &QAction::triggered, std::bind(&AutotestPlugin::onRunUnderCursorTriggered, this, + TestRunMode::Debug)); + contextMenu->addAction(command); + contextMenu->addSeparator(); } ExtensionSystem::IPlugin::ShutdownFlag AutotestPlugin::aboutToShutdown() @@ -188,6 +217,35 @@ void AutotestPlugin::onRunSelectedTriggered() runner->prepareToRunTests(TestRunMode::Run); } +void AutotestPlugin::onRunUnderCursorTriggered(TestRunMode mode) +{ + QTextCursor cursor = Utils::Text::wordStartCursor( + TextEditor::BaseTextEditor::currentTextEditor()->editorWidget()->textCursor()); + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + const QString text = cursor.selectedText(); + if (text.isEmpty()) + return; // Do not trigger when no name under cursor + + const QList<TestTreeItem *> testsItems = TestTreeModel::instance()->testItemsByName(text); + if (testsItems.isEmpty()) + return; // Wrong location triggered + + QList<TestConfiguration *> testsToRun; + for (const TestTreeItem * item : testsItems){ + if (TestConfiguration *cfg = item->asConfiguration(mode)) + testsToRun << cfg; + } + + if (testsToRun.isEmpty()) { + MessageManager::write(tr("Selected test was not found (%1).").arg(text), MessageManager::Flash); + return; + } + + auto runner = TestRunner::instance(); + runner->setSelectedTests(testsToRun); + runner->prepareToRunTests(mode); +} + void AutotestPlugin::updateMenuItemsEnabledState() { const bool canScan = !TestRunner::instance()->isTestRunning() @@ -200,6 +258,13 @@ void AutotestPlugin::updateMenuItemsEnabledState() ActionManager::command(Constants::ACTION_RUN_ALL_ID)->action()->setEnabled(canRun); ActionManager::command(Constants::ACTION_RUN_SELECTED_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_DBG_UCURSOR)->action()->setEnabled(canRun); } QList<QObject *> AutotestPlugin::createTestObjects() const diff --git a/src/plugins/autotest/autotestplugin.h b/src/plugins/autotest/autotestplugin.h index a1f0d3b83e..cbd85937eb 100644 --- a/src/plugins/autotest/autotestplugin.h +++ b/src/plugins/autotest/autotestplugin.h @@ -37,6 +37,7 @@ class TestNavigationWidgetFactory; class TestResultsPane; struct TestSettings; class TestSettingsPage; +enum class TestRunMode; class AutotestPlugin : public ExtensionSystem::IPlugin { @@ -59,6 +60,7 @@ private: void initializeMenuEntries(); void onRunAllTriggered(); void onRunSelectedTriggered(); + void onRunUnderCursorTriggered(TestRunMode mode); QList<QObject *> createTestObjects() const override; const QSharedPointer<TestSettings> m_settings; TestFrameworkManager *m_frameworkManager = nullptr; diff --git a/src/plugins/autotest/testrunner.cpp b/src/plugins/autotest/testrunner.cpp index cdc6dd592b..2b3121e47c 100644 --- a/src/plugins/autotest/testrunner.cpp +++ b/src/plugins/autotest/testrunner.cpp @@ -110,19 +110,7 @@ void TestRunner::setSelectedTests(const QList<TestConfiguration *> &selected) void TestRunner::runTest(TestRunMode mode, const TestTreeItem *item) { - TestConfiguration *configuration; - switch (mode) { - case TestRunMode::Run: - case TestRunMode::RunWithoutDeploy: - configuration = item->testConfiguration(); - break; - case TestRunMode::Debug: - case TestRunMode::DebugWithoutDeploy: - configuration = item->debugConfiguration(); - break; - default: - configuration = nullptr; - } + TestConfiguration *configuration = item->asConfiguration(mode); if (configuration) { setSelectedTests({configuration}); diff --git a/src/plugins/autotest/testtreeitem.cpp b/src/plugins/autotest/testtreeitem.cpp index 362465e923..a308ee628c 100644 --- a/src/plugins/autotest/testtreeitem.cpp +++ b/src/plugins/autotest/testtreeitem.cpp @@ -236,6 +236,20 @@ TestTreeItem *TestTreeItem::findChildByNameAndFile(const QString &name, const QS }); } +TestConfiguration *TestTreeItem::asConfiguration(TestRunMode mode) const +{ + switch (mode) { + case TestRunMode::Run: + case TestRunMode::RunWithoutDeploy: + return testConfiguration(); + case TestRunMode::Debug: + case TestRunMode::DebugWithoutDeploy: + return debugConfiguration(); + default: + return nullptr; + } +} + QList<TestConfiguration *> TestTreeItem::getAllTestConfigurations() const { return QList<TestConfiguration *>(); diff --git a/src/plugins/autotest/testtreeitem.h b/src/plugins/autotest/testtreeitem.h index 012104f91f..6e8c7de87e 100644 --- a/src/plugins/autotest/testtreeitem.h +++ b/src/plugins/autotest/testtreeitem.h @@ -48,10 +48,11 @@ namespace Internal { class TestParseResult; class TestConfiguration; - +enum class TestRunMode; class TestTreeItem : public Utils::TreeItem { public: + enum Type { Root, @@ -108,6 +109,7 @@ public: virtual bool canProvideDebugConfiguration() const { return false; } virtual TestConfiguration *testConfiguration() const { return 0; } virtual TestConfiguration *debugConfiguration() const { return 0; } + TestConfiguration *asConfiguration(TestRunMode mode) const; virtual QList<TestConfiguration *> getAllTestConfigurations() const; virtual QList<TestConfiguration *> getSelectedTestConfigurations() const; virtual bool lessThan(const TestTreeItem *other, SortMode mode) const; diff --git a/src/plugins/autotest/testtreemodel.cpp b/src/plugins/autotest/testtreemodel.cpp index f189c6472d..805398bc9f 100644 --- a/src/plugins/autotest/testtreemodel.cpp +++ b/src/plugins/autotest/testtreemodel.cpp @@ -158,6 +158,45 @@ QList<TestConfiguration *> TestTreeModel::getSelectedTests() const return result; } +QList<TestTreeItem *> TestTreeModel::testItemsByName(TestTreeItem *root, const QString &testName) +{ + QList<TestTreeItem *> result; + + for (int row = 0, count = root->childCount(); row < count; ++row){ + TestTreeItem *node = root->childItem(row); + + if (node->type() == TestTreeItem::TestCase) { + if (node->name() == testName) { + result << node; + continue; // prioritize Tests over TestCases + } + + TestTreeItem *testCase = node->findChildBy([testName](const TestTreeItem *it) { + QTC_ASSERT(it, return false); + return it->type() == TestTreeItem::TestFunctionOrSet && it->name() == testName; + }); // collect only actual tests, not special functions like init, cleanup etc, + + if (!testCase) + continue; + + result << testCase; + } else { + result << testItemsByName(node, testName); + } + } + + return result; +} + +QList<TestTreeItem *> TestTreeModel::testItemsByName(const QString &testName) +{ + QList<TestTreeItem *> result; + for (Utils::TreeItem *frameworkRoot : *rootItem()) + result << testItemsByName(static_cast<TestTreeItem *>(frameworkRoot), testName); + + return result; +} + void TestTreeModel::syncTestFrameworks() { // remove all currently registered diff --git a/src/plugins/autotest/testtreemodel.h b/src/plugins/autotest/testtreemodel.h index 58c9b279aa..95298953ed 100644 --- a/src/plugins/autotest/testtreemodel.h +++ b/src/plugins/autotest/testtreemodel.h @@ -54,7 +54,7 @@ public: bool hasTests() const; QList<TestConfiguration *> getAllTestCases() const; QList<TestConfiguration *> getSelectedTests() const; - + QList<TestTreeItem *> testItemsByName(const QString &testName); void syncTestFrameworks(); void rebuild(const QList<Core::Id> &frameworkIds); @@ -91,6 +91,7 @@ private: void revalidateCheckState(TestTreeItem *item); explicit TestTreeModel(QObject *parent = 0); void setupParsingConnections(); + QList<TestTreeItem *> testItemsByName(TestTreeItem *root, const QString &testName); TestCodeParser *m_parser; }; |