Files
qt-creator/plugins/autotest/autotestplugin.cpp

194 lines
5.9 KiB
C++
Raw Normal View History

2014-10-07 12:30:54 +02:00
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
2014-10-07 12:30:54 +02:00
** All rights reserved.
** For any questions to The Qt Company, please use contact form at
** http://www.qt.io/contact-us
2014-10-07 12:30:54 +02:00
**
** This file is part of the Qt Creator Enterprise Auto Test Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
2014-10-07 12:30:54 +02:00
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
2014-10-07 12:30:54 +02:00
**
****************************************************************************/
#include "autotestplugin.h"
#include "autotestconstants.h"
#include "testcodeparser.h"
#include "testrunner.h"
#include "testsettings.h"
#include "testsettingspage.h"
#include "testtreeitem.h"
2014-10-07 12:30:54 +02:00
#include "testtreeview.h"
#include "testtreemodel.h"
#include "testresultspane.h"
#include "testnavigationwidget.h"
2014-10-07 12:30:54 +02:00
#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>
#include <extensionsystem/pluginmanager.h>
#include <licensechecker/licensecheckerplugin.h>
2014-10-07 12:30:54 +02:00
#include <QAction>
#include <QMessageBox>
#include <QMainWindow>
#include <QMenu>
#include <QtPlugin>
#ifdef WITH_TESTS
#include "autotestunittests.h"
#endif
2014-10-07 12:30:54 +02:00
using namespace Autotest::Internal;
using namespace Core;
2014-10-07 12:30:54 +02:00
static AutotestPlugin *m_instance = 0;
2014-10-07 12:30:54 +02:00
AutotestPlugin::AutotestPlugin()
: m_settings(new TestSettings)
2014-10-07 12:30:54 +02:00
{
// needed to be used in QueuedConnection connects
qRegisterMetaType<TestResult>();
qRegisterMetaType<TestTreeItem>();
qRegisterMetaType<TestCodeLocationAndType>();
qRegisterMetaType<TestTreeModel::Type>();
m_instance = this;
2014-10-07 12:30:54 +02:00
}
AutotestPlugin::~AutotestPlugin()
{
// Delete members
TestTreeModel *model = TestTreeModel::instance();
delete model;
TestRunner *runner = TestRunner::instance();
delete runner;
2014-10-07 12:30:54 +02:00
}
AutotestPlugin *AutotestPlugin::instance()
{
return m_instance;
}
QSharedPointer<TestSettings> AutotestPlugin::settings() const
{
return m_settings;
}
bool AutotestPlugin::checkLicense()
{
LicenseChecker::LicenseCheckerPlugin *licenseChecker
= ExtensionSystem::PluginManager::getObject<LicenseChecker::LicenseCheckerPlugin>();
if (!licenseChecker || !licenseChecker->hasValidLicense()) {
qWarning() << "Invalid license, disabling Qt Creator Enterprise Auto Test Add-on.";
return false;
} else if (!licenseChecker->enterpriseFeatures())
return false;
return true;
}
void AutotestPlugin::initializeMenuEntries()
{
ActionContainer *menu = ActionManager::createMenu(Constants::MENU_ID);
menu->menu()->setTitle(tr("Tests"));
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);
command->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+T,Alt+S")));
connect(action, &QAction::triggered,
TestTreeModel::instance()->parser(), &TestCodeParser::updateTestTree);
menu->addAction(command);
ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
connect(menu->menu(), &QMenu::aboutToShow, this, &AutotestPlugin::updateMenuItemsEnabledState);
}
2014-10-07 12:30:54 +02:00
bool AutotestPlugin::initialize(const QStringList &arguments, QString *errorString)
{
Q_UNUSED(arguments)
Q_UNUSED(errorString)
if (!checkLicense())
return true;
initializeMenuEntries();
2014-10-07 12:30:54 +02:00
m_settings->fromSettings(ICore::settings());
addAutoReleasedObject(new TestSettingsPage(m_settings));
addAutoReleasedObject(new TestNavigationWidgetFactory);
addAutoReleasedObject(TestResultsPane::instance());
2014-10-07 12:30:54 +02:00
return true;
}
void AutotestPlugin::extensionsInitialized()
{
}
ExtensionSystem::IPlugin::ShutdownFlag AutotestPlugin::aboutToShutdown()
{
return SynchronousShutdown;
}
void AutotestPlugin::onRunAllTriggered()
{
TestRunner *runner = TestRunner::instance();
TestTreeModel *model = TestTreeModel::instance();
runner->setSelectedTests(model->getAllTestCases());
runner->runTests();
}
void AutotestPlugin::onRunSelectedTriggered()
{
TestRunner *runner = TestRunner::instance();
TestTreeModel *model = TestTreeModel::instance();
runner->setSelectedTests(model->getSelectedTests());
runner->runTests();
}
void AutotestPlugin::updateMenuItemsEnabledState()
{
const bool enabled = !TestRunner::instance()->isTestRunning();
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);
}
QList<QObject *> AutotestPlugin::createTestObjects() const
{
QList<QObject *> tests;
#ifdef WITH_TESTS
tests << new AutoTestUnitTests(TestTreeModel::instance());
#endif
return tests;
}