AutoTest: Move functionality from model to item classes

Another preparation for having a consistent interface later on.

Change-Id: I573e45101016048de916f4d321b326a24efbe4ee
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
Christian Stenger
2016-04-08 14:58:23 +02:00
committed by Christian Stenger
parent cdf16ced62
commit 842ab2a551
5 changed files with 616 additions and 454 deletions

View File

@@ -24,17 +24,18 @@
****************************************************************************/
#include "autotestconstants.h"
#include "autotest_utils.h"
#include "testconfiguration.h"
#include "testtreeitem.h"
#include "testtreemodel.h"
#include <cplusplus/Icons.h>
#include <projectexplorer/session.h>
#include <texteditor/texteditor.h>
#include <utils/qtcassert.h>
#include <QIcon>
#include <texteditor/texteditor.h>
#include <cplusplus/Icons.h>
namespace Autotest {
namespace Internal {
@@ -69,51 +70,21 @@ QVariant TestTreeItem::data(int /*column*/, int role) const
case Qt::DisplayRole:
if (m_type == Root && childCount() == 0)
return QString(m_name + QCoreApplication::translate("TestTreeItem", " (none)"));
else if (m_name.isEmpty())
return QCoreApplication::translate("TestTreeItem", Constants::UNNAMED_QUICKTESTS);
else
return m_name;
case Qt::ToolTipRole:
if (m_type == TestCase && m_name.isEmpty()) {
return QCoreApplication::translate("TestTreeItem","<p>Give all test cases a name to ensure correct behavior "
"when running test cases and to be able to select them.</p>");
}
return m_filePath;
case Qt::DecorationRole:
return testTreeIcon(m_type);
case Qt::CheckStateRole:
switch (m_type) {
case Root:
case TestDataFunction:
case TestSpecialFunction:
case TestDataTag:
return QVariant();
case TestCase:
return m_name.isEmpty() ? QVariant() : checked();
case TestFunctionOrSet:
if (parentItem() && parentItem()->name().isEmpty())
return QVariant();
return checked();
default:
return checked();
}
return QVariant();
case LinkRole: {
QVariant itemLink;
itemLink.setValue(TextEditor::TextEditorWidget::Link(m_filePath, m_line, m_column));
return itemLink;
}
case ItalicRole:
switch (m_type) {
case TestDataFunction:
case TestSpecialFunction:
return true;
case TestCase:
return m_name.isEmpty();
case TestFunctionOrSet:
return parentItem() ? parentItem()->name().isEmpty() : false;
default:
return false;
}
return false;
case TypeRole:
return m_type;
}
@@ -197,14 +168,9 @@ Qt::CheckState TestTreeItem::checked() const
case TestCase:
case TestFunctionOrSet:
return m_checked;
case TestDataFunction:
case TestSpecialFunction:
return Qt::Unchecked;
default:
if (parent())
return parentItem()->m_checked;
return Qt::Unchecked;
}
return Qt::Unchecked;
}
void TestTreeItem::markForRemoval(bool mark)
@@ -250,6 +216,16 @@ TestTreeItem *TestTreeItem::findChildByNameAndFile(const QString &name, const QS
});
}
QList<TestConfiguration *> TestTreeItem::getAllTestConfigurations() const
{
return QList<TestConfiguration *>();
}
QList<TestConfiguration *> TestTreeItem::getSelectedTestConfigurations() const
{
return QList<TestConfiguration *>();
}
void TestTreeItem::revalidateCheckState()
{
if (childCount() == 0)
@@ -343,6 +319,152 @@ AutoTestTreeItem *AutoTestTreeItem::createDataTagItem(const QString &fileName,
return tagItem;
}
QVariant AutoTestTreeItem::data(int column, int role) const
{
switch (role) {
case Qt::CheckStateRole:
switch (type()) {
case Root:
case TestDataFunction:
case TestSpecialFunction:
case TestDataTag:
return QVariant();
default:
return checked();
}
case ItalicRole:
switch (type()) {
case TestDataFunction:
case TestSpecialFunction:
return true;
default:
return false;
}
}
return TestTreeItem::data(column, role);
}
bool AutoTestTreeItem::canProvideTestConfiguration() const
{
switch (type()) {
case TestCase:
case TestFunctionOrSet:
case TestDataTag:
return true;
default:
return false;
}
}
TestConfiguration *AutoTestTreeItem::testConfiguration() const
{
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
QTC_ASSERT(project, return 0);
TestConfiguration *config = 0;
switch (type()) {
case TestCase:
config = new TestConfiguration(name(), QStringList(), childCount());
config->setProFile(proFile());
config->setProject(project);
config->setDisplayName(TestUtils::getCMakeDisplayNameIfNecessary(filePath(), proFile()));
break;
case TestFunctionOrSet: {
TestTreeItem *parent = parentItem();
config = new TestConfiguration(parent->name(), QStringList() << name());
config->setProFile(parent->proFile());
config->setProject(project);
config->setDisplayName(
TestUtils::getCMakeDisplayNameIfNecessary(filePath(), parent->proFile()));
break;
}
case TestDataTag:{
const TestTreeItem *function = parentItem();
const TestTreeItem *parent = function ? function->parentItem() : 0;
if (!parent)
return 0;
const QString functionWithTag = function->name() + QLatin1Char(':') + name();
config = new TestConfiguration(parent->name(), QStringList() << functionWithTag);
config->setProFile(parent->proFile());
config->setProject(project);
config->setDisplayName(TestUtils::getCMakeDisplayNameIfNecessary(filePath(),
parent->proFile()));
break;
}
default:
return 0;
}
return config;
}
QList<TestConfiguration *> AutoTestTreeItem::getAllTestConfigurations() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project || type() != Root)
return result;
for (int row = 0, count = childCount(); row < count; ++row) {
const TestTreeItem *child = childItem(row);
TestConfiguration *tc = new TestConfiguration(child->name(), QStringList(),
child->childCount());
tc->setProFile(child->proFile());
tc->setProject(project);
tc->setDisplayName(TestUtils::getCMakeDisplayNameIfNecessary(child->filePath(),
child->proFile()));
result << tc;
}
return result;
}
QList<TestConfiguration *> AutoTestTreeItem::getSelectedTestConfigurations() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project || type() != Root)
return result;
TestConfiguration *testConfiguration = 0;
for (int row = 0, count = childCount(); row < count; ++row) {
const TestTreeItem *child = 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(
TestUtils::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(
TestUtils::getCMakeDisplayNameIfNecessary(child->filePath(), child->proFile()));
result << testConfiguration;
}
}
return result;
}
QuickTestTreeItem *QuickTestTreeItem::createTestItem(const TestParseResult &result)
{
QuickTestTreeItem *item = new QuickTestTreeItem(result.testCaseName, result.fileName, TestCase);
@@ -382,6 +504,238 @@ QuickTestTreeItem *QuickTestTreeItem::createUnnamedQuickFunctionItem(const QStri
return item;
}
QVariant QuickTestTreeItem::data(int column, int role) const
{
switch (role) {
case Qt::DisplayRole:
if (type() == TestCase && name().isEmpty())
return QObject::tr(Constants::UNNAMED_QUICKTESTS);
break;
case Qt::ToolTipRole:
if (type() == TestCase && name().isEmpty())
return QObject::tr("<p>Give all test cases a name to ensure correct behavior "
"when running test cases and to be able to select them.</p>");
break;
case Qt::CheckStateRole:
switch (type()) {
case Root:
case TestDataFunction:
case TestSpecialFunction:
case TestDataTag:
return QVariant();
case TestCase:
return name().isEmpty() ? QVariant() : checked();
case TestFunctionOrSet:
return (parentItem() && !parentItem()->name().isEmpty()) ? checked() : QVariant();
default:
return checked();
}
case ItalicRole:
switch (type()) {
case TestDataFunction:
case TestSpecialFunction:
return true;
case TestCase:
return name().isEmpty();
case TestFunctionOrSet:
return parentItem() ? parentItem()->name().isEmpty() : false;
default:
return false;
}
default:
break;
}
return TestTreeItem::data(column, role);
}
bool QuickTestTreeItem::canProvideTestConfiguration() const
{
switch (type()) {
case TestCase:
return !name().isEmpty();
case TestFunctionOrSet:
return !parentItem()->name().isEmpty();
default:
return false;
}
}
TestConfiguration *QuickTestTreeItem::testConfiguration() const
{
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
QTC_ASSERT(project, return 0);
TestConfiguration *config = 0;
switch (type()) {
case TestCase: {
QStringList testFunctions;
for (int row = 0, count = childCount(); row < count; ++row)
testFunctions << name() + QLatin1String("::") + childItem(row)->name();
config = new TestConfiguration(QString(), testFunctions);
config->setProFile(proFile());
config->setProject(project);
break;
}
case TestFunctionOrSet: {
TestTreeItem *parent = parentItem();
QStringList testFunction(parent->name() + QLatin1String("::") + name());
config = new TestConfiguration(QString(), testFunction);
config->setProFile(parent->proFile());
config->setProject(project);
break;
}
default:
return 0;
}
return config;
}
QList<TestConfiguration *> QuickTestTreeItem::getAllTestConfigurations() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project || type() != Root)
return result;
QHash<QString, int> foundProFiles;
for (int row = 0, count = childCount(); row < count; ++row) {
const TestTreeItem *child = 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;
}
return result;
}
QList<TestConfiguration *> QuickTestTreeItem::getSelectedTestConfigurations() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project || type() != Root)
return result;
TestConfiguration *tc = 0;
QHash<QString, TestConfiguration *> foundProFiles;
// unnamed Quick Tests must be handled first
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(tc,
qWarning() << "Illegal state (unnamed Quick Test listed as named)";
return QList<TestConfiguration *>());
foundProFiles[proFile]->setTestCaseCount(tc->testCaseCount() + 1);
} else {
tc = new TestConfiguration(QString(), QStringList());
tc->setTestCaseCount(1);
tc->setUnnamedOnly(true);
tc->setProFile(proFile);
tc->setProject(project);
foundProFiles.insert(proFile, tc);
}
}
}
for (int row = 0, count = childCount(); row < count; ++row) {
const TestTreeItem *child = 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() != TestFunctionOrSet)
continue;
testFunctions << child->name() + QLatin1String("::") + grandChild->name();
}
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;
}
return result;
}
TestTreeItem *QuickTestTreeItem::unnamedQuickTests() const
{
if (type() != Root)
return 0;
for (int row = 0, count = childCount(); row < count; ++row) {
TestTreeItem *child = childItem(row);
if (child->name().isEmpty())
return child;
}
return 0;
}
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");
}
GoogleTestTreeItem *GoogleTestTreeItem::createTestItem(const TestParseResult &result)
{
GoogleTestTreeItem *item = new GoogleTestTreeItem(result.testCaseName, QString(), TestCase);
@@ -414,12 +768,22 @@ QVariant GoogleTestTreeItem::data(int column, int role) const
switch (role) {
case Qt::DisplayRole: {
if (type() == TestTreeItem::Root)
return TestTreeItem::data(column, role);
break;
const QString &displayName = (m_state & GoogleTestTreeItem::Disabled)
? name().mid(9) : name();
return QVariant(displayName + nameSuffix());
}
case Qt::CheckStateRole:
switch (type()) {
case TestCase:
case TestFunctionOrSet:
return checked();
default:
return QVariant();
}
case ItalicRole:
return false;
case StateRole:
return (int)m_state;
default:
@@ -428,6 +792,156 @@ QVariant GoogleTestTreeItem::data(int column, int role) const
return TestTreeItem::data(column, role);
}
TestConfiguration *GoogleTestTreeItem::testConfiguration() const
{
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
QTC_ASSERT(project, return 0);
TestConfiguration *config = 0;
switch (type()) {
case TestCase: {
const QString &testSpecifier = gtestFilter(state()).arg(name()).arg(QLatin1Char('*'));
if (int count = childCount()) {
config = new TestConfiguration(QString(), QStringList(testSpecifier));
config->setTestCaseCount(count);
config->setProFile(proFile());
config->setProject(project);
// item has no filePath set - so take it of the first children
config->setDisplayName(
TestUtils::getCMakeDisplayNameIfNecessary(childItem(0)->filePath(), proFile()));
config->setTestType(TestTypeGTest);
}
break;
}
case TestFunctionOrSet: {
GoogleTestTreeItem *parent = static_cast<GoogleTestTreeItem *>(parentItem());
if (parent)
return 0;
const QString &testSpecifier = gtestFilter(parent->state()).arg(parent->name()).arg(name());
config = new TestConfiguration(QString(), QStringList(testSpecifier));
config->setProFile(proFile());
config->setProject(project);
config->setDisplayName(
TestUtils::getCMakeDisplayNameIfNecessary(filePath(), parent->proFile()));
config->setTestType(TestTypeGTest);
break;
}
default:
return 0;
}
return config;
}
// 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 *> GoogleTestTreeItem::getAllTestConfigurations() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project || type() != Root)
return result;
QHash<ProFileWithDisplayName, int> proFilesWithTestSets;
for (int row = 0, count = childCount(); row < count; ++row) {
const GoogleTestTreeItem *child = static_cast<const GoogleTestTreeItem *>(childItem(row));
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(),
TestUtils::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;
}
return result;
}
QList<TestConfiguration *> GoogleTestTreeItem::getSelectedTestConfigurations() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project || type() != Root)
return result;
QHash<ProFileWithDisplayName, QStringList> proFilesWithCheckedTestSets;
for (int row = 0, count = childCount(); row < count; ++row) {
const GoogleTestTreeItem *child = static_cast<const GoogleTestTreeItem *>(childItem(row));
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(),
TestUtils::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;
}
return result;
}
bool GoogleTestTreeItem::modifyTestSetContent(const QString &fileName,
const TestCodeLocationAndType &location)
{
@@ -445,7 +959,7 @@ TestTreeItem *GoogleTestTreeItem::findChildByNameStateAndFile(const QString &nam
const QString &proFile)
{
return findChildBy([name, state, proFile](const TestTreeItem *other) -> bool {
GoogleTestTreeItem *gtestItem = const_cast<TestTreeItem *>(other)->asGoogleTestTreeItem();
const GoogleTestTreeItem *gtestItem = static_cast<const GoogleTestTreeItem *>(other);
return other->proFile() == proFile
&& other->name() == name
&& gtestItem->state() == state;