Commit 842ab2a5 authored by Christian Stenger's avatar Christian Stenger Committed by Christian Stenger
Browse files

AutoTest: Move functionality from model to item classes



Another preparation for having a consistent interface later on.

Change-Id: I573e45101016048de916f4d321b326a24efbe4ee
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent cdf16ced
......@@ -25,6 +25,9 @@
#pragma once
#include <cpptools/cppmodelmanager.h>
#include <cpptools/projectpart.h>
#include <QStringList>
namespace Autotest {
......@@ -63,6 +66,20 @@ public:
static const QByteArrayList valid = {"QUICK_TEST_MAIN", "QUICK_TEST_OPENGL_MAIN"};
return valid.contains(macro);
}
static QString getCMakeDisplayNameIfNecessary(const QString &filePath, const QString &proFile)
{
static const QString CMAKE_LISTS = QLatin1String("CMakeLists.txt");
if (!proFile.endsWith(CMAKE_LISTS))
return QString();
const QList<CppTools::ProjectPart::Ptr> &projectParts
= CppTools::CppModelManager::instance()->projectPart(filePath);
if (projectParts.size())
return projectParts.first()->displayName;
return QString();
}
};
} // namespace Internal
......
......@@ -107,12 +107,9 @@ void TestNavigationWidget::contextMenuEvent(QContextMenuEvent *event)
const QModelIndex index = list.first();
QRect rect(m_view->visualRect(index));
if (rect.contains(event->pos())) {
// do not provide this menu entry for unnamed Quick Tests as it makes no sense
int type = index.data(TypeRole).toInt();
const QString &unnamed = tr(Constants::UNNAMED_QUICKTESTS);
if ((type == TestTreeItem::TestFunctionOrSet && index.parent().data().toString() != unnamed)
|| (type == TestTreeItem::TestCase && index.data().toString() != unnamed)
|| (type == TestTreeItem::TestDataTag)) {
TestTreeItem *item = static_cast<TestTreeItem *>(
m_model->itemForIndex(m_sortFilterModel->mapToSource(index)));
if (item->canProvideTestConfiguration()) {
runThisTest = new QAction(tr("Run This Test"), &menu);
runThisTest->setEnabled(enabled);
connect(runThisTest, &QAction::triggered,
......@@ -256,13 +253,10 @@ void TestNavigationWidget::onRunThisTestTriggered()
return;
TestTreeItem *item = static_cast<TestTreeItem *>(sourceIndex.internalPointer());
if (item->type() == TestTreeItem::TestCase || item->type() == TestTreeItem::TestFunctionOrSet
|| item->type() == TestTreeItem::TestDataTag) {
if (TestConfiguration *configuration = m_model->getTestConfiguration(item)) {
TestRunner *runner = TestRunner::instance();
runner->setSelectedTests( {configuration} );
runner->prepareToRunTests();
}
if (TestConfiguration *configuration = m_model->getTestConfiguration(item)) {
TestRunner *runner = TestRunner::instance();
runner->setSelectedTests( {configuration} );
runner->prepareToRunTests();
}
}
......
This diff is collapsed.
......@@ -48,6 +48,7 @@ class AutoTestTreeItem;
class QuickTestTreeItem;
class GoogleTestTreeItem;
struct TestParseResult;
class TestConfiguration;
class TestTreeItem : public Utils::TreeItem
{
......@@ -84,8 +85,8 @@ public:
unsigned column() const { return m_column; }
QString proFile() const { return m_proFile; }
void setProFile(const QString &proFile) { m_proFile = proFile; }
void setChecked(const Qt::CheckState checked);
Qt::CheckState checked() const;
virtual void setChecked(const Qt::CheckState checked);
virtual Qt::CheckState checked() const;
Type type() const { return m_type; }
void markForRemoval(bool mark);
void markForRemovalRecursively(bool mark);
......@@ -98,12 +99,10 @@ public:
TestTreeItem *findChildByFile(const QString &filePath);
TestTreeItem *findChildByNameAndFile(const QString &name, const QString &filePath);
virtual AutoTestTreeItem *asAutoTestTreeItem() { return 0; }
virtual QuickTestTreeItem *asQuickTestTreeItem() { return 0; }
virtual GoogleTestTreeItem *asGoogleTestTreeItem() { return 0; }
virtual const AutoTestTreeItem *asAutoTestTreeItem() const { return 0; }
virtual const QuickTestTreeItem *asQuickTestTreeItem() const { return 0; }
virtual const GoogleTestTreeItem *asGoogleTestTreeItem() const { return 0; }
virtual bool canProvideTestConfiguration() const { return false; }
virtual TestConfiguration *testConfiguration() const { return 0; }
virtual QList<TestConfiguration *> getAllTestConfigurations() const;
virtual QList<TestConfiguration *> getSelectedTestConfigurations() const;
protected:
bool modifyFilePath(const QString &filePath);
......@@ -139,15 +138,18 @@ public:
AutoTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type) {}
virtual AutoTestTreeItem *asAutoTestTreeItem() override { return this; }
virtual const AutoTestTreeItem *asAutoTestTreeItem() const override { return this; }
static AutoTestTreeItem *createTestItem(const TestParseResult &result);
static AutoTestTreeItem *createFunctionItem(const QString &functionName,
const TestCodeLocationAndType &location,
const TestCodeLocationList &dataTags);
static AutoTestTreeItem *createDataTagItem(const QString &fileName,
const TestCodeLocationAndType &location);
QVariant data(int column, int role) const override;
bool canProvideTestConfiguration() const override;
TestConfiguration *testConfiguration() const override;
QList<TestConfiguration *> getAllTestConfigurations() const override;
QList<TestConfiguration *> getSelectedTestConfigurations() const override;
};
class QuickTestTreeItem : public TestTreeItem
......@@ -156,15 +158,21 @@ public:
QuickTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type) {}
virtual QuickTestTreeItem *asQuickTestTreeItem() override { return this; }
virtual const QuickTestTreeItem *asQuickTestTreeItem() const override { return this; }
static QuickTestTreeItem *createTestItem(const TestParseResult &result);
static QuickTestTreeItem *createFunctionItem(const QString &functionName,
const TestCodeLocationAndType &location);
static QuickTestTreeItem *createUnnamedQuickTestItem(const TestParseResult &result);
static QuickTestTreeItem *createUnnamedQuickFunctionItem(const QString &functionName,
const TestParseResult &result);
QVariant data(int column, int role) const override;
bool canProvideTestConfiguration() const override;
TestConfiguration *testConfiguration() const override;
QList<TestConfiguration *> getAllTestConfigurations() const override;
QList<TestConfiguration *> getSelectedTestConfigurations() const override;
private:
TestTreeItem *unnamedQuickTests() const;
};
class GoogleTestTreeItem : public TestTreeItem
......@@ -184,14 +192,15 @@ public:
GoogleTestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root) : TestTreeItem(name, filePath, type), m_state(Enabled) {}
virtual GoogleTestTreeItem *asGoogleTestTreeItem() override { return this; }
virtual const GoogleTestTreeItem *asGoogleTestTreeItem() const override { return this; }
static GoogleTestTreeItem *createTestItem(const TestParseResult &result);
static GoogleTestTreeItem *createTestSetItem(const TestParseResult &result,
const TestCodeLocationAndType &location);
QVariant data(int column, int role) const override;
bool canProvideTestConfiguration() const override { return type() != Root; }
TestConfiguration *testConfiguration() const override;
QList<TestConfiguration *> getAllTestConfigurations() const override;
QList<TestConfiguration *> getSelectedTestConfigurations() const override;
void setStates(TestStates states) { m_state = states; }
void setState(TestState state) { m_state |= state; }
......
......@@ -31,15 +31,10 @@
#include "testtreemodel.h"
#include <cpptools/cppmodelmanager.h>
#include <cpptools/projectpart.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <texteditor/texteditor.h>
#include <utils/qtcassert.h>
namespace Autotest {
......@@ -190,396 +185,29 @@ bool TestTreeModel::hasTests() const
|| m_googleTestRootItem->childCount() > 0;
}
static QString getCMakeDisplayNameIfNecessary(const QString &filePath, const QString &proFile)
{
static const QString CMAKE_LISTS = QLatin1String("CMakeLists.txt");
if (!proFile.endsWith(CMAKE_LISTS))
return QString();
const QList<CppTools::ProjectPart::Ptr> &projectParts
= CppTools::CppModelManager::instance()->projectPart(filePath);
if (projectParts.size())
return projectParts.first()->displayName;
return QString();
}
// used as key inside getAllTestCases()/getSelectedTestCases() for Google Tests
class ProFileWithDisplayName
{
public:
ProFileWithDisplayName(const QString &file, const QString &name)
: proFile(file), displayName(name) {}
QString proFile;
QString displayName;
bool operator==(const ProFileWithDisplayName &rhs) const
{
return proFile == rhs.proFile && displayName == rhs.displayName;
}
};
// needed as ProFileWithDisplayName is used as key inside a QHash
bool operator<(const ProFileWithDisplayName &lhs, const ProFileWithDisplayName &rhs)
{
return lhs.proFile == rhs.proFile ? lhs.displayName < rhs.displayName
: lhs.proFile < rhs.proFile;
}
// needed as ProFileWithDisplayName is used as a key inside a QHash
uint qHash(const ProFileWithDisplayName &lhs)
{
return ::qHash(lhs.proFile) ^ ::qHash(lhs.displayName);
}
QList<TestConfiguration *> TestTreeModel::getAllTestCases() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project)
return result;
// get all Auto Tests
for (int row = 0, count = m_autoTestRootItem->childCount(); row < count; ++row) {
const TestTreeItem *child = m_autoTestRootItem->childItem(row);
TestConfiguration *tc = new TestConfiguration(child->name(), QStringList(),
child->childCount());
tc->setProFile(child->proFile());
tc->setProject(project);
tc->setDisplayName(getCMakeDisplayNameIfNecessary(child->filePath(), child->proFile()));
result << tc;
}
// get all Quick Tests
QHash<QString, int> foundProFiles;
for (int row = 0, count = m_quickTestRootItem->childCount(); row < count; ++row) {
const TestTreeItem *child = m_quickTestRootItem->childItem(row);
// unnamed Quick Tests must be handled separately
if (child->name().isEmpty()) {
for (int childRow = 0, ccount = child->childCount(); childRow < ccount; ++ childRow) {
const TestTreeItem *grandChild = child->childItem(childRow);
const QString &proFile = grandChild->proFile();
foundProFiles.insert(proFile, foundProFiles[proFile] + 1);
}
continue;
}
// named Quick Test
const QString &proFile = child->proFile();
foundProFiles.insert(proFile, foundProFiles[proFile] + child->childCount());
}
// create TestConfiguration for each project file
{
QHash<QString, int>::ConstIterator it = foundProFiles.begin();
QHash<QString, int>::ConstIterator end = foundProFiles.end();
for ( ; it != end; ++it) {
TestConfiguration *tc = new TestConfiguration(QString(), QStringList(), it.value());
tc->setProFile(it.key());
tc->setProject(project);
result << tc;
}
}
foundProFiles.clear();
// get all Google Tests
QHash<ProFileWithDisplayName, int> proFilesWithTestSets;
for (int row = 0, count = m_googleTestRootItem->childCount(); row < count; ++row) {
const GoogleTestTreeItem *child = m_googleTestRootItem->childItem(row)->asGoogleTestTreeItem();
const int grandChildCount = child->childCount();
for (int grandChildRow = 0; grandChildRow < grandChildCount; ++grandChildRow) {
const TestTreeItem *grandChild = child->childItem(grandChildRow);
if (grandChild->checked() == Qt::Checked) {
ProFileWithDisplayName key(grandChild->proFile(),
getCMakeDisplayNameIfNecessary(grandChild->filePath(),
grandChild->proFile()));
proFilesWithTestSets.insert(key, proFilesWithTestSets[key] + 1);
}
}
}
{
QHash<ProFileWithDisplayName, int>::ConstIterator it = proFilesWithTestSets.begin();
QHash<ProFileWithDisplayName, int>::ConstIterator end = proFilesWithTestSets.end();
for ( ; it != end; ++it) {
const ProFileWithDisplayName &key = it.key();
TestConfiguration *tc = new TestConfiguration(QString(), QStringList(), it.value());
tc->setTestType(TestTypeGTest);
tc->setProFile(key.proFile);
tc->setDisplayName(key.displayName);
tc->setProject(project);
result << tc;
}
}
result.append(m_autoTestRootItem->getAllTestConfigurations());
result.append(m_quickTestRootItem->getAllTestConfigurations());
result.append(m_googleTestRootItem->getAllTestConfigurations());
return result;
}
static QString gtestFilter(GoogleTestTreeItem::TestStates states)
{
if ((states & GoogleTestTreeItem::Parameterized) && (states & GoogleTestTreeItem::Typed))
return QLatin1String("*/%1/*.%2");
if (states & GoogleTestTreeItem::Parameterized)
return QLatin1String("*/%1.%2/*");
if (states & GoogleTestTreeItem::Typed)
return QLatin1String("%1/*.%2");
return QLatin1String("%1.%2");
}
QList<TestConfiguration *> TestTreeModel::getSelectedTests() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project)
return result;
TestConfiguration *testConfiguration = 0;
for (int row = 0, count = m_autoTestRootItem->childCount(); row < count; ++row) {
const TestTreeItem *child = m_autoTestRootItem->childItem(row);
switch (child->checked()) {
case Qt::Unchecked:
continue;
case Qt::Checked:
testConfiguration = new TestConfiguration(child->name(), QStringList(), child->childCount());
testConfiguration->setProFile(child->proFile());
testConfiguration->setProject(project);
testConfiguration->setDisplayName(getCMakeDisplayNameIfNecessary(child->filePath(),
child->proFile()));
result << testConfiguration;
continue;
case Qt::PartiallyChecked:
default:
const QString childName = child->name();
int grandChildCount = child->childCount();
QStringList testCases;
for (int grandChildRow = 0; grandChildRow < grandChildCount; ++grandChildRow) {
const TestTreeItem *grandChild = child->childItem(grandChildRow);
if (grandChild->checked() == Qt::Checked)
testCases << grandChild->name();
}
testConfiguration = new TestConfiguration(childName, testCases);
testConfiguration->setProFile(child->proFile());
testConfiguration->setProject(project);
testConfiguration->setDisplayName(getCMakeDisplayNameIfNecessary(child->filePath(),
child->proFile()));
result << testConfiguration;
}
}
// Quick Tests must be handled differently - need the calling cpp file to use this in
// addProjectInformation() - additionally this must be unique to not execute the same executable
// on and on and on...
// TODO: do this later on for Auto Tests as well to support strange setups? or redo the model
QHash<QString, TestConfiguration *> foundProFiles;
if (TestTreeItem *unnamed = unnamedQuickTests()) {
for (int childRow = 0, ccount = unnamed->childCount(); childRow < ccount; ++ childRow) {
const TestTreeItem *grandChild = unnamed->childItem(childRow);
const QString &proFile = grandChild->proFile();
if (foundProFiles.contains(proFile)) {
QTC_ASSERT(testConfiguration,
qWarning() << "Illegal state (unnamed Quick Test listed as named)";
return QList<TestConfiguration *>());
foundProFiles[proFile]->setTestCaseCount(testConfiguration->testCaseCount() + 1);
} else {
testConfiguration = new TestConfiguration(QString(), QStringList());
testConfiguration->setTestCaseCount(1);
testConfiguration->setUnnamedOnly(true);
testConfiguration->setProFile(proFile);
testConfiguration->setProject(project);
foundProFiles.insert(proFile, testConfiguration);
}
}
}
for (int row = 0, count = m_quickTestRootItem->childCount(); row < count; ++row) {
const TestTreeItem *child = m_quickTestRootItem->childItem(row);
// unnamed Quick Tests have been handled separately already
if (child->name().isEmpty())
continue;
// named Quick Tests
switch (child->checked()) {
case Qt::Unchecked:
continue;
case Qt::Checked:
case Qt::PartiallyChecked:
default:
QStringList testFunctions;
int grandChildCount = child->childCount();
for (int grandChildRow = 0; grandChildRow < grandChildCount; ++grandChildRow) {
const TestTreeItem *grandChild = child->childItem(grandChildRow);
if (grandChild->type() != TestTreeItem::TestFunctionOrSet)
continue;
testFunctions << child->name() + QLatin1String("::") + grandChild->name();
}
TestConfiguration *tc;
if (foundProFiles.contains(child->proFile())) {
tc = foundProFiles[child->proFile()];
QStringList oldFunctions(tc->testCases());
// if oldFunctions.size() is 0 this test configuration is used for at least one
// unnamed test case
if (oldFunctions.size() == 0) {
tc->setTestCaseCount(tc->testCaseCount() + testFunctions.size());
tc->setUnnamedOnly(false);
} else {
oldFunctions << testFunctions;
tc->setTestCases(oldFunctions);
}
} else {
tc = new TestConfiguration(QString(), testFunctions);
tc->setProFile(child->proFile());
tc->setProject(project);
foundProFiles.insert(child->proFile(), tc);
}
break;
}
}
{
QHash<QString, TestConfiguration *>::ConstIterator it = foundProFiles.begin();
QHash<QString, TestConfiguration *>::ConstIterator end = foundProFiles.end();
for ( ; it != end; ++it) {
TestConfiguration *config = it.value();
if (!config->unnamedOnly())
result << config;
else
delete config;
}
}
// get selected Google Tests
QHash<ProFileWithDisplayName, QStringList> proFilesWithCheckedTestSets;
for (int row = 0, count = m_googleTestRootItem->childCount(); row < count; ++row) {
const auto child = m_googleTestRootItem->childItem(row)->asGoogleTestTreeItem();
if (child->checked() == Qt::Unchecked)
continue;
int grandChildCount = child->childCount();
for (int grandChildRow = 0; grandChildRow < grandChildCount; ++grandChildRow) {
const TestTreeItem *grandChild = child->childItem(grandChildRow);
if (grandChild->checked() == Qt::Checked) {
ProFileWithDisplayName key(grandChild->proFile(),
getCMakeDisplayNameIfNecessary(grandChild->filePath(),
grandChild->proFile()));
proFilesWithCheckedTestSets[key].append(
gtestFilter(child->state()).arg(child->name()).arg(grandChild->name()));
}
}
}
{
QHash<ProFileWithDisplayName, QStringList>::ConstIterator it = proFilesWithCheckedTestSets.begin();
QHash<ProFileWithDisplayName, QStringList>::ConstIterator end = proFilesWithCheckedTestSets.end();
for ( ; it != end; ++it) {
const ProFileWithDisplayName &key = it.key();
TestConfiguration *tc = new TestConfiguration(QString(), it.value());
tc->setTestType(TestTypeGTest);
tc->setProFile(key.proFile);
tc->setDisplayName(key.displayName);
tc->setProject(project);
result << tc;
}
}
result.append(m_autoTestRootItem->getSelectedTestConfigurations());
result.append(m_quickTestRootItem->getSelectedTestConfigurations());
result.append(m_googleTestRootItem->getSelectedTestConfigurations());
return result;
}
TestConfiguration *TestTreeModel::getTestConfiguration(const TestTreeItem *item) const
{
QTC_ASSERT(item != 0, return 0);
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
QTC_ASSERT(project, return 0);
TestConfiguration *config = 0;
switch (item->type()) {
case TestTreeItem::TestCase: {
if (item->asQuickTestTreeItem()) {
// Quick Test TestCase
QStringList testFunctions;
for (int row = 0, count = item->childCount(); row < count; ++row) {
testFunctions << item->name() + QLatin1String("::")
+ item->childItem(row)->name();
}
config = new TestConfiguration(QString(), testFunctions);
config->setProFile(item->proFile());
config->setProject(project);
} else if (item->asAutoTestTreeItem()) {
// normal auto test
config = new TestConfiguration(item->name(), QStringList(), item->childCount());
config->setProFile(item->proFile());
config->setProject(project);
config->setDisplayName(getCMakeDisplayNameIfNecessary(item->filePath(),
item->proFile()));
} else if (auto gtestItem = item->asGoogleTestTreeItem()) {
const QString &testSpecifier
= gtestFilter(gtestItem->state()).arg(item->name()).arg(QLatin1Char('*'));
if (int childCount = item->childCount()) {
config = new TestConfiguration(QString(), QStringList(testSpecifier));
config->setTestCaseCount(childCount);
config->setProFile(item->proFile());
config->setProject(project);
// item has no filePath set - so take it of the first children
config->setDisplayName(getCMakeDisplayNameIfNecessary(
item->childItem(0)->filePath(), item->proFile()));
config->setTestType(TestTypeGTest);
}
}
break;
}
case TestTreeItem::TestFunctionOrSet: {
TestTreeItem *parent = item->parentItem();
if (parent->asQuickTestTreeItem()) {
// it's a Quick Test function of a named TestCase
QStringList testFunction(parent->name() + QLatin1String("::") + item->name());
config = new TestConfiguration(QString(), testFunction);
config->setProFile(parent->proFile());
config->setProject(project);
} else if (parent->asAutoTestTreeItem()){
// normal auto test
config = new TestConfiguration(parent->name(), QStringList() << item->name());
config->setProFile(parent->proFile());
config->setProject(project);
config->setDisplayName(getCMakeDisplayNameIfNecessary(item->filePath(),
parent->proFile()));
} else if (auto gtestParent = parent->asGoogleTestTreeItem()) {
const QString &testSpecifier
= gtestFilter(gtestParent->state()).arg(parent->name()).arg(item->name());
config = new TestConfiguration(QString(), QStringList(testSpecifier));
config->setProFile(item->proFile());
config->setProject(project);
config->setDisplayName(getCMakeDisplayNameIfNecessary(item->filePath(),
parent->proFile()));
config->setTestType(TestTypeGTest);
}
break;
}
case TestTreeItem::TestDataTag: {
const TestTreeItem *function = item->parentItem();
const TestTreeItem *parent = function ? function->parentItem() : 0;
if (!parent)
return 0;
const QString functionWithTag = function->name() + QLatin1Char(':') + item->name();
config = new TestConfiguration(parent->name(), QStringList() << functionWithTag);
config->setProFile(parent->proFile());
config->setProject(project);
config->setDisplayName(getCMakeDisplayNameIfNecessary(item->filePath(), parent->proFile()));
break;
}
// not supported items
default:
return 0;
}
return config;
return item->testConfiguration();