Commit 87a6f003 authored by Christian Stenger's avatar Christian Stenger Committed by Christian Stenger

AutoTest: Split off TestResult...

...to avoid dragging around members that are not used in sub-classes.

Change-Id: I3e30b403491b1ae9219a0b461c3034cb854701a6
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent b58a10df
...@@ -270,29 +270,29 @@ void QtTestOutputReader::processOutput() ...@@ -270,29 +270,29 @@ void QtTestOutputReader::processOutput()
const QStringRef currentTag = m_xmlReader.name(); const QStringRef currentTag = m_xmlReader.name();
if (currentTag == QStringLiteral("TestFunction")) { if (currentTag == QStringLiteral("TestFunction")) {
if (!m_duration.isEmpty()) { if (!m_duration.isEmpty()) {
TestResultPtr testResult = TestResultPtr(new QTestResult(m_className)); QTestResult *testResult = new QTestResult(m_className);
testResult->setTestCase(m_testCase); testResult->setFunctionName(m_testCase);
testResult->setResult(Result::MessageInternal); testResult->setResult(Result::MessageInternal);
testResult->setDescription(tr("Execution took %1 ms.").arg(m_duration)); testResult->setDescription(tr("Execution took %1 ms.").arg(m_duration));
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
} }
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1); m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} else if (currentTag == QStringLiteral("TestCase")) { } else if (currentTag == QStringLiteral("TestCase")) {
TestResultPtr testResult = TestResultPtr(new QTestResult(m_className)); QTestResult *testResult = new QTestResult(m_className);
testResult->setResult(Result::MessageTestCaseEnd); testResult->setResult(Result::MessageTestCaseEnd);
testResult->setDescription( testResult->setDescription(
m_duration.isEmpty() ? tr("Test finished.") m_duration.isEmpty() ? tr("Test finished.")
: tr("Test execution took %1 ms.").arg(m_duration)); : tr("Test execution took %1 ms.").arg(m_duration));
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
} else if (validEndTags.contains(currentTag.toString())) { } else if (validEndTags.contains(currentTag.toString())) {
TestResultPtr testResult = TestResultPtr(new QTestResult(m_className)); QTestResult *testResult = new QTestResult(m_className);
testResult->setTestCase(m_testCase); testResult->setFunctionName(m_testCase);
testResult->setDataTag(m_dataTag); testResult->setDataTag(m_dataTag);
testResult->setResult(m_result); testResult->setResult(m_result);
testResult->setFileName(m_file); testResult->setFileName(m_file);
testResult->setLine(m_lineNumber); testResult->setLine(m_lineNumber);
testResult->setDescription(m_description); testResult->setDescription(m_description);
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
} }
break; break;
} }
...@@ -367,11 +367,11 @@ void GTestOutputReader::processOutput() ...@@ -367,11 +367,11 @@ void GTestOutputReader::processOutput()
} }
if (testEnds.exactMatch(line)) { if (testEnds.exactMatch(line)) {
TestResultPtr testResult = TestResultPtr(new GTestResult(m_currentTestName)); GTestResult *testResult = new GTestResult(m_currentTestName);
testResult->setTestCase(m_currentTestSet); testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::MessageTestCaseEnd); testResult->setResult(Result::MessageTestCaseEnd);
testResult->setDescription(tr("Test execution took %1").arg(testEnds.cap(2))); testResult->setDescription(tr("Test execution took %1").arg(testEnds.cap(2)));
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
m_currentTestName.clear(); m_currentTestName.clear();
m_currentTestSet.clear(); m_currentTestSet.clear();
} else if (newTestStarts.exactMatch(line)) { } else if (newTestStarts.exactMatch(line)) {
...@@ -394,21 +394,21 @@ void GTestOutputReader::processOutput() ...@@ -394,21 +394,21 @@ void GTestOutputReader::processOutput()
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(testResult);
m_description.clear(); m_description.clear();
} else if (testSetSuccess.exactMatch(line)) { } else if (testSetSuccess.exactMatch(line)) {
TestResultPtr testResult = TestResultPtr(new GTestResult(m_currentTestName)); GTestResult *testResult = new GTestResult(m_currentTestName);
testResult->setTestCase(m_currentTestSet); testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::Pass); testResult->setResult(Result::Pass);
testResult->setDescription(m_description); testResult->setDescription(m_description);
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
m_description.clear(); m_description.clear();
testResult = TestResultPtr(new GTestResult(m_currentTestName)); testResult = new GTestResult(m_currentTestName);
testResult->setTestCase(m_currentTestSet); testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::MessageInternal); testResult->setResult(Result::MessageInternal);
testResult->setDescription(tr("Execution took %1.").arg(testSetSuccess.cap(2))); testResult->setDescription(tr("Execution took %1.").arg(testSetSuccess.cap(2)));
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1); m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} else if (testSetFail.exactMatch(line)) { } else if (testSetFail.exactMatch(line)) {
TestResultPtr testResult = TestResultPtr(new GTestResult(m_currentTestName)); GTestResult *testResult = new GTestResult(m_currentTestName);
testResult->setTestCase(m_currentTestSet); testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::Fail); testResult->setResult(Result::Fail);
m_description.chop(1); m_description.chop(1);
testResult->setDescription(m_description); testResult->setDescription(m_description);
...@@ -423,13 +423,13 @@ void GTestOutputReader::processOutput() ...@@ -423,13 +423,13 @@ void GTestOutputReader::processOutput()
break; break;
} }
} }
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
m_description.clear(); m_description.clear();
testResult = TestResultPtr(new GTestResult(m_currentTestName)); testResult = new GTestResult(m_currentTestName);
testResult->setTestCase(m_currentTestSet); testResult->setTestSetName(m_currentTestSet);
testResult->setResult(Result::MessageInternal); testResult->setResult(Result::MessageInternal);
testResult->setDescription(tr("Execution took %1.").arg(testSetFail.cap(2))); testResult->setDescription(tr("Execution took %1.").arg(testSetFail.cap(2)));
m_futureInterface.reportResult(testResult); m_futureInterface.reportResult(TestResultPtr(testResult));
m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1); m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
} }
} }
......
...@@ -41,14 +41,16 @@ TestResult::TestResult() ...@@ -41,14 +41,16 @@ TestResult::TestResult()
{ {
} }
TestResult::TestResult(const QString &className) TestResult::TestResult(const QString &name)
: m_class(className) : m_name(name)
, m_result(Result::Invalid)
, m_line(0)
, m_type(TestTypeQt)
{ {
} }
const QString TestResult::outputString(bool selected) const
{
return selected ? m_description : m_description.split(QLatin1Char('\n')).first();
}
Result::Type TestResult::resultFromString(const QString &resultString) Result::Type TestResult::resultFromString(const QString &resultString)
{ {
if (resultString == QLatin1String("pass")) if (resultString == QLatin1String("pass"))
...@@ -146,23 +148,71 @@ QColor TestResult::colorForType(const Result::Type type) ...@@ -146,23 +148,71 @@ QColor TestResult::colorForType(const Result::Type type)
} }
} }
bool operator==(const TestResult &t1, const TestResult &t2) QTestResult::QTestResult(const QString &className)
: TestResult(className)
{ {
return t1.className() == t2.className()
&& t1.testCase() == t2.testCase()
&& t1.dataTag() == t2.dataTag()
&& t1.result() == t2.result();
} }
QTestResult::QTestResult(const QString &className) const QString QTestResult::outputString(bool selected) const
: TestResult(className) {
const QString &desc = description();
const QString &className = name();
QString output;
switch (result()) {
case Result::Pass:
case Result::Fail:
case Result::ExpectedFail:
case Result::UnexpectedPass:
case Result::BlacklistedFail:
case Result::BlacklistedPass:
output = className + QLatin1String("::") + m_function;
if (!m_dataTag.isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(m_dataTag));
if (selected && !desc.isEmpty()) {
output.append(QLatin1Char('\n')).append(desc);
}
break;
case Result::Benchmark:
output = className + QLatin1String("::") + m_function;
if (!m_dataTag.isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(m_dataTag));
if (!desc.isEmpty()) {
int breakPos = desc.indexOf(QLatin1Char('('));
output.append(QLatin1String(": ")).append(desc.left(breakPos));
if (selected)
output.append(QLatin1Char('\n')).append(desc.mid(breakPos));
}
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
}
return output;
}
GTestResult::GTestResult(const QString &name)
: TestResult(name)
{ {
} }
GTestResult::GTestResult(const QString &className) const QString GTestResult::outputString(bool selected) const
: TestResult(className)
{ {
setTestType(TestTypeGTest); const QString &desc = description();
QString output;
switch (result()) {
case Result::Pass:
case Result::Fail:
output = m_testSetName;
if (selected && !desc.isEmpty())
output.append(QLatin1Char('\n')).append(desc);
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
}
return output;
} }
} // namespace Internal } // namespace Internal
......
...@@ -67,25 +67,22 @@ enum Type { ...@@ -67,25 +67,22 @@ enum Type {
class TestResult class TestResult
{ {
public: public:
TestResult(); explicit TestResult();
TestResult(const QString &className); explicit TestResult(const QString &name);
virtual ~TestResult() {}
QString className() const { return m_class; } virtual const QString outputString(bool selected) const;
QString testCase() const { return m_case; }
QString dataTag() const { return m_dataTag; } QString name() const { return m_name; }
Result::Type result() const { return m_result; } Result::Type result() const { return m_result; }
QString description() const { return m_description; } QString description() const { return m_description; }
QString fileName() const { return m_file; } QString fileName() const { return m_file; }
int line() const { return m_line; } int line() const { return m_line; }
TestType type() const { return m_type; }
void setDescription(const QString &description) { m_description = description; } void setDescription(const QString &description) { m_description = description; }
void setFileName(const QString &fileName) { m_file = fileName; } void setFileName(const QString &fileName) { m_file = fileName; }
void setLine(int line) { m_line = line; } void setLine(int line) { m_line = line; }
void setResult(Result::Type type) { m_result = type; } void setResult(Result::Type type) { m_result = type; }
void setTestCase(const QString &testCase) { m_case = testCase; }
void setDataTag(const QString &dataTag) { m_dataTag = dataTag; }
void setTestType(TestType type) { m_type = type; }
static Result::Type resultFromString(const QString &resultString); static Result::Type resultFromString(const QString &resultString);
static Result::Type toResultType(int rt); static Result::Type toResultType(int rt);
...@@ -93,15 +90,11 @@ public: ...@@ -93,15 +90,11 @@ public:
static QColor colorForType(const Result::Type type); static QColor colorForType(const Result::Type type);
private: private:
QString m_class; QString m_name;
QString m_case; Result::Type m_result = Result::Invalid;
QString m_dataTag;
Result::Type m_result;
QString m_description; QString m_description;
QString m_file; QString m_file;
int m_line; int m_line = 0;
TestType m_type;
// environment?
}; };
using TestResultPtr = QSharedPointer<TestResult>; using TestResultPtr = QSharedPointer<TestResult>;
...@@ -115,16 +108,28 @@ public: ...@@ -115,16 +108,28 @@ public:
class QTestResult : public TestResult class QTestResult : public TestResult
{ {
public: public:
QTestResult(const QString &className = QString()); explicit QTestResult(const QString &className = QString());
const QString outputString(bool selected) const override;
void setFunctionName(const QString &functionName) { m_function = functionName; }
void setDataTag(const QString &dataTag) { m_dataTag = dataTag; }
private:
QString m_function;
QString m_dataTag;
}; };
class GTestResult : public TestResult class GTestResult : public TestResult
{ {
public: public:
GTestResult(const QString &className = QString()); explicit GTestResult(const QString &name = QString());
}; const QString outputString(bool selected) const override;
bool operator==(const TestResult &t1, const TestResult &t2); void setTestSetName(const QString &testSetName) { m_testSetName = testSetName; }
private:
QString m_testSetName;
};
} // namespace Internal } // namespace Internal
} // namespace Autotest } // namespace Autotest
......
...@@ -43,46 +43,6 @@ TestResultDelegate::TestResultDelegate(QObject *parent) ...@@ -43,46 +43,6 @@ TestResultDelegate::TestResultDelegate(QObject *parent)
{ {
} }
QString TestResultDelegate::outputString(const TestResult &testResult, bool selected)
{
const QString desc = testResult.description();
QString output;
switch (testResult.result()) {
case Result::Pass:
case Result::Fail:
case Result::ExpectedFail:
case Result::UnexpectedPass:
case Result::BlacklistedFail:
case Result::BlacklistedPass:
if (testResult.type() == TestTypeQt)
output = testResult.className() + QLatin1String("::") + testResult.testCase();
else // TestTypeGTest
output = testResult.testCase();
if (!testResult.dataTag().isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(testResult.dataTag()));
if (selected && !desc.isEmpty()) {
output.append(QLatin1Char('\n')).append(desc);
}
break;
case Result::Benchmark:
output = testResult.className() + QLatin1String("::") + testResult.testCase();
if (!testResult.dataTag().isEmpty())
output.append(QString::fromLatin1(" (%1)").arg(testResult.dataTag()));
if (!desc.isEmpty()) {
int breakPos = desc.indexOf(QLatin1Char('('));
output.append(QLatin1String(": ")).append(desc.left(breakPos));
if (selected)
output.append(QLatin1Char('\n')).append(desc.mid(breakPos));
}
break;
default:
output = desc;
if (!selected)
output = output.split(QLatin1Char('\n')).first();
}
return output;
}
void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
...@@ -110,7 +70,8 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op ...@@ -110,7 +70,8 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model()); TestResultFilterModel *resultFilterModel = static_cast<TestResultFilterModel *>(view->model());
LayoutPositions positions(opt, resultFilterModel); LayoutPositions positions(opt, resultFilterModel);
const TestResult &testResult = resultFilterModel->testResult(index); const TestResult *testResult = resultFilterModel->testResult(index);
QTC_ASSERT(testResult, painter->restore();return);
// draw the indicator by ourself as we paint across it with the delegate // draw the indicator by ourself as we paint across it with the delegate
QStyleOptionViewItem indicatorOpt = option; QStyleOptionViewItem indicatorOpt = option;
...@@ -122,17 +83,17 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op ...@@ -122,17 +83,17 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
painter->drawPixmap(positions.left(), positions.top(), painter->drawPixmap(positions.left(), positions.top(),
icon.pixmap(positions.iconSize(), positions.iconSize())); icon.pixmap(positions.iconSize(), positions.iconSize()));
QString typeStr = TestResult::resultToString(testResult.result()); QString typeStr = TestResult::resultToString(testResult->result());
if (selected) { if (selected) {
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr); painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
} else { } else {
QPen tmp = painter->pen(); QPen tmp = painter->pen();
painter->setPen(TestResult::colorForType(testResult.result())); painter->setPen(TestResult::colorForType(testResult->result()));
painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr); painter->drawText(positions.typeAreaLeft(), positions.top() + fm.ascent(), typeStr);
painter->setPen(tmp); painter->setPen(tmp);
} }
QString output = outputString(testResult, selected); QString output = testResult->outputString(selected);
if (selected) { if (selected) {
output.replace(QLatin1Char('\n'), QChar::LineSeparator); output.replace(QLatin1Char('\n'), QChar::LineSeparator);
...@@ -151,7 +112,7 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op ...@@ -151,7 +112,7 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
fm.elidedText(output.left(2000), Qt::ElideRight, positions.textAreaWidth())); fm.elidedText(output.left(2000), Qt::ElideRight, positions.textAreaWidth()));
} }
QString file = testResult.fileName(); QString file = testResult->fileName();
const int pos = file.lastIndexOf(QLatin1Char('/')); const int pos = file.lastIndexOf(QLatin1Char('/'));
if (pos != -1) if (pos != -1)
file = file.mid(pos + 1); file = file.mid(pos + 1);
...@@ -160,8 +121,8 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op ...@@ -160,8 +121,8 @@ void TestResultDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
painter->drawText(positions.fileAreaLeft(), positions.top() + fm.ascent(), file); painter->drawText(positions.fileAreaLeft(), positions.top() + fm.ascent(), file);
if (testResult.line()) { if (testResult->line()) {
QString line = QString::number(testResult.line()); QString line = QString::number(testResult->line());
painter->setClipRect(positions.lineArea()); painter->setClipRect(positions.lineArea());
painter->drawText(positions.lineAreaLeft(), positions.top() + fm.ascent(), line); painter->drawText(positions.lineAreaLeft(), positions.top() + fm.ascent(), line);
} }
...@@ -190,9 +151,9 @@ QSize TestResultDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo ...@@ -190,9 +151,9 @@ QSize TestResultDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
s.setWidth(opt.rect.width()); s.setWidth(opt.rect.width());
if (selected) { if (selected) {
const TestResult &testResult = resultFilterModel->testResult(index); const TestResult *testResult = resultFilterModel->testResult(index);
QTC_ASSERT(testResult, return QSize());
QString output = outputString(testResult, selected); QString output = testResult->outputString(selected);
output.replace(QLatin1Char('\n'), QChar::LineSeparator); output.replace(QLatin1Char('\n'), QChar::LineSeparator);
if (AutotestPlugin::instance()->settings()->limitResultOutput if (AutotestPlugin::instance()->settings()->limitResultOutput
......
...@@ -42,8 +42,6 @@ public: ...@@ -42,8 +42,6 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
static QString outputString(const TestResult &testResult, bool selected);
public slots: public slots:
void currentChanged(const QModelIndex &current, const QModelIndex &previous); void currentChanged(const QModelIndex &current, const QModelIndex &previous);
......
...@@ -81,8 +81,7 @@ QVariant TestResultItem::data(int column, int role) const ...@@ -81,8 +81,7 @@ QVariant TestResultItem::data(int column, int role) const
case Qt::DecorationRole: case Qt::DecorationRole:
return m_testResult ? testResultIcon(m_testResult->result()) : QVariant(); return m_testResult ? testResultIcon(m_testResult->result()) : QVariant();
case Qt::DisplayRole: case Qt::DisplayRole:
return m_testResult ? TestResultDelegate::outputString(*m_testResult.data(), true) return m_testResult ? m_testResult->outputString(true) : QVariant();
: QVariant();
default: default:
return Utils::TreeItem::data(column, role); return Utils::TreeItem::data(column, role);
} }
...@@ -175,11 +174,11 @@ void TestResultModel::addTestResult(const TestResultPtr &testResult, bool autoEx ...@@ -175,11 +174,11 @@ void TestResultModel::addTestResult(const TestResultPtr &testResult, bool autoEx
TestResultItem *newItem = new TestResultItem(testResult); TestResultItem *newItem = new TestResultItem(testResult);
// FIXME this might be totally wrong... we need some more unique information! // FIXME this might be totally wrong... we need some more unique information!
if (!testResult->className().isEmpty()) { if (!testResult->name().isEmpty()) {
for (int row = lastRow; row >= 0; --row) { for (int row = lastRow; row >= 0; --row) {
TestResultItem *current = static_cast<TestResultItem *>(topLevelItems.at(row)); TestResultItem *current = static_cast<TestResultItem *>(topLevelItems.at(row));
const TestResult *result = current->testResult(); const TestResult *result = current->testResult();
if (result && result->className() == testResult->className()) { if (result && result->name() == testResult->name()) {
current->appendChild(newItem); current->appendChild(newItem);
if (autoExpand) if (autoExpand)
current->expand(); current->expand();
...@@ -226,12 +225,12 @@ void TestResultModel::clearTestResults() ...@@ -226,12 +225,12 @@ void TestResultModel::clearTestResults()
m_widthOfLineNumber = 0; m_widthOfLineNumber = 0;
} }
TestResult TestResultModel::testResult(const QModelIndex &idx) const TestResult *TestResultModel::testResult(const QModelIndex &idx)
{ {
if (idx.isValid()) if (idx.isValid())
return *(static_cast<TestResultItem *>(itemForIndex(idx))->testResult()); return static_cast<TestResultItem *>(itemForIndex(idx))->testResult();
return TestResult(); return 0;
} }
int TestResultModel::maxWidthOfFileName(const QFont