summaryrefslogtreecommitdiff
path: root/plugins/autotest/testresultmodel.cpp
diff options
context:
space:
mode:
authorChristian Stenger <christian.stenger@digia.com>2014-10-07 15:51:02 +0200
committerChristian Stenger <christian.stenger@theqtcompany.com>2014-12-04 13:52:15 +0100
commit44db2be195b38316e238eddad80b816ab96b6058 (patch)
tree5bf50c8194761477910a852776fcc7e3098eb259 /plugins/autotest/testresultmodel.cpp
parent2f4139e5d313902683a3a4a98f9ef012afff8734 (diff)
downloadqt-creator-44db2be195b38316e238eddad80b816ab96b6058.tar.gz
Provide basic test runner and output pane
Diffstat (limited to 'plugins/autotest/testresultmodel.cpp')
-rw-r--r--plugins/autotest/testresultmodel.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/plugins/autotest/testresultmodel.cpp b/plugins/autotest/testresultmodel.cpp
new file mode 100644
index 0000000000..1867987aa1
--- /dev/null
+++ b/plugins/autotest/testresultmodel.cpp
@@ -0,0 +1,163 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use contact form at http://qt.digia.com
+**
+** 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 Digia.
+**
+** If you have questions regarding the use of this file, please use
+** contact form at http://qt.digia.com
+**
+****************************************************************************/
+
+#include "testresultmodel.h"
+
+#include <QDebug>
+#include <QFontMetrics>
+#include <QIcon>
+
+namespace Autotest {
+namespace Internal {
+
+TestResultModel::TestResultModel(QObject *parent) :
+ QAbstractItemModel(parent),
+ m_widthOfLineNumber(0),
+ m_maxWidthOfFileName(0),
+ m_lastMaxWidthIndex(0)
+{
+}
+
+TestResultModel::~TestResultModel()
+{
+ m_testResults.clear();
+}
+
+QModelIndex TestResultModel::index(int row, int column, const QModelIndex &parent) const
+{
+ if (parent.isValid())
+ return QModelIndex();
+ return createIndex(row, column);
+}
+
+QModelIndex TestResultModel::parent(const QModelIndex &) const
+{
+ return QModelIndex();
+}
+
+int TestResultModel::rowCount(const QModelIndex &parent) const
+{
+ return parent.isValid() ? 0 : m_testResults.size();
+}
+
+int TestResultModel::columnCount(const QModelIndex &parent) const
+{
+ return parent.isValid() ? 0 : 1;
+}
+
+static QIcon testResultIcon(ResultType result) {
+ static QIcon icons[8] = {
+ QIcon(QLatin1String(":/images/pass.png")),
+ QIcon(QLatin1String(":/images/fail.png")),
+ QIcon(QLatin1String(":/images/xfail.png")),
+ QIcon(QLatin1String(":/images/xpass.png")),
+ QIcon(QLatin1String(":/images/skip.png")),
+ QIcon(QLatin1String(":/images/debug.png")),
+ QIcon(QLatin1String(":/images/warn.png")),
+ QIcon(QLatin1String(":/images/fatal.png")),
+ };
+
+ if (result < 0 || result >= MESSAGE_INTERNAL)
+ return QIcon();
+ return icons[result];
+}
+
+QVariant TestResultModel::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid() || index.row() >= m_testResults.count() || index.column() != 0)
+ return QVariant();
+ if (role == Qt::DisplayRole) {
+ const TestResult &tr = m_testResults.at(index.row());
+ switch (tr.result()) {
+ case ResultType::PASS:
+ case ResultType::FAIL:
+ case ResultType::EXPECTED_FAIL:
+ case ResultType::UNEXPECTED_PASS:
+ case ResultType::SKIP:
+ return QString::fromLatin1("%1::%2 (%3) - %4").arg(tr.className(), tr.testCase(),
+ tr.dataTag(), tr.fileName());
+ default:
+ return tr.description();
+ }
+ }
+ if (role == Qt::DecorationRole) {
+ const TestResult &tr = m_testResults.at(index.row());
+ return testResultIcon(tr.result());
+ }
+
+ return QVariant();
+}
+
+void TestResultModel::addTestResult(const TestResult &testResult)
+{
+ beginInsertRows(QModelIndex(), m_testResults.size(), m_testResults.size());
+ m_testResults.append(testResult);
+ endInsertRows();
+}
+
+void TestResultModel::clearTestResults()
+{
+ if (m_testResults.size() == 0)
+ return;
+ beginRemoveRows(QModelIndex(), 0, m_testResults.size() - 1);
+ m_testResults.clear();
+ endRemoveRows();
+}
+
+TestResult TestResultModel::testResult(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return TestResult(QString(), QString());
+ return m_testResults.at(index.row());
+}
+
+int TestResultModel::maxWidthOfFileName(const QFont &font)
+{
+ int count = m_testResults.size();
+ if (count == 0)
+ return 0;
+ if (m_maxWidthOfFileName > 0 && font == m_measurementFont && m_lastMaxWidthIndex == count - 1)
+ return m_maxWidthOfFileName;
+
+ QFontMetrics fm(font);
+ m_measurementFont = font;
+
+ for (int i = m_lastMaxWidthIndex; i < count; ++i) {
+ QString filename = m_testResults.at(i).fileName();
+ const int pos = filename.lastIndexOf(QLatin1Char('/'));
+ if (pos != -1)
+ filename = filename.mid(pos +1);
+
+ m_maxWidthOfFileName = qMax(m_maxWidthOfFileName, fm.width(filename));
+ }
+ m_lastMaxWidthIndex = count - 1;
+ return m_maxWidthOfFileName;
+}
+
+int TestResultModel::maxWidthOfLineNumber(const QFont &font)
+{
+ if (m_widthOfLineNumber == 0 || font != m_measurementFont) {
+ QFontMetrics fm(font);
+ m_measurementFont = font;
+ m_widthOfLineNumber = fm.width(QLatin1String("88888"));
+ }
+ return m_widthOfLineNumber;
+}
+
+} // namespace Internal
+} // namespace Autotest