QmlProfiler: Add test for debug messages model

Change-Id: Ie6872646c09a89a417379a3e1f551526234ab26a
Reviewed-by: Christian Kandeler <christian.kandeler@theqtcompany.com>
This commit is contained in:
Ulf Hermann
2016-05-30 11:59:32 +02:00
parent 1fc9bcc025
commit 652846c5c7
7 changed files with 236 additions and 0 deletions

View File

@@ -97,3 +97,7 @@ RESOURCES += \
FORMS += \
qmlprofilerconfigwidget.ui
equals(TEST, 1) {
include(tests/tests.pri)
}

View File

@@ -71,4 +71,13 @@ QtcPlugin {
prefix: "qml/"
files: ["qmlprofiler.qrc"]
}
Group {
name: "Unit tests"
condition: project.testsEnabled
prefix: "tests/"
files: [
"debugmessagesmodel_test.cpp", "debugmessagesmodel_test.h",
]
}
}

View File

@@ -29,6 +29,10 @@
#include "qmlprofilertool.h"
#include "qmlprofilertimelinemodel.h"
#ifdef WITH_TESTS
#include "tests/debugmessagesmodel_test.h"
#endif
#include <extensionsystem/pluginmanager.h>
#include <utils/hostosinfo.h>
@@ -72,5 +76,14 @@ QmlProfilerSettings *QmlProfilerPlugin::globalSettings()
return qmlProfilerGlobalSettings();
}
QList<QObject *> QmlProfiler::Internal::QmlProfilerPlugin::createTestObjects() const
{
QList<QObject *> tests;
#ifdef WITH_TESTS
tests << new DebugMessagesModelTest;
#endif
return tests;
}
} // namespace Internal
} // namespace QmlProfiler

View File

@@ -46,6 +46,8 @@ public:
static bool debugOutput;
static QmlProfilerSettings *globalSettings();
QList<QObject *> createTestObjects() const override;
};
} // namespace Internal

View File

@@ -0,0 +1,145 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "debugmessagesmodel_test.h"
#include <QtTest>
namespace QmlProfiler {
namespace Internal {
DebugMessagesModelTest::DebugMessagesModelTest(QObject *parent) :
QObject(parent), manager(nullptr), model(&manager)
{
}
void DebugMessagesModelTest::initTestCase()
{
manager.startAcquiring();
QmlEvent event;
QmlEventType type;
event.setTypeIndex(-1);
type.location.filename = QLatin1String("somefile.js");
type.displayName.clear();
type.data.clear();
type.message = DebugMessage;
type.rangeType = MaximumRangeType;
for (int i = 0; i < 10; ++i) {
event.setTimestamp(i);
event.setString(QString::fromLatin1("message %1").arg(i));
type.location.line = i;
type.location.column = 10 - i;
type.detailType = i % (QtMsgType::QtInfoMsg + 1);
event.setTypeIndex(manager.qmlModel()->addEventType(type));
manager.qmlModel()->addEvent(event);
}
manager.acquiringDone();
QCOMPARE(manager.state(), QmlProfilerModelManager::Done);
}
void DebugMessagesModelTest::testTypeId()
{
// Each event should have a different type and they should be the only ones
for (int i = 0; i < 10; ++i)
QCOMPARE(model.typeId(i), i);
}
void DebugMessagesModelTest::testColor()
{
// TimelineModel::colorBySelectionId ...
for (int i = 0; i < 10; ++i) {
QCOMPARE(model.color(i),
QColor::fromHsl((i % (QtMsgType::QtInfoMsg + 1) * 25) % 360, 150, 166));
}
}
static const char *messageTypes[] = {
QT_TRANSLATE_NOOP("DebugMessagesModel", "Debug Message"),
QT_TRANSLATE_NOOP("DebugMessagesModel", "Warning Message"),
QT_TRANSLATE_NOOP("DebugMessagesModel", "Critical Message"),
QT_TRANSLATE_NOOP("DebugMessagesModel", "Fatal Message"),
QT_TRANSLATE_NOOP("DebugMessagesModel", "Info Message"),
};
void DebugMessagesModelTest::testLabels()
{
QVariantList labels = model.labels();
for (int i = 0; i <= QtMsgType::QtInfoMsg; ++i) {
QVariantMap element = labels[i].toMap();
QCOMPARE(element[QLatin1String("description")].toString(), model.tr(messageTypes[i]));
QCOMPARE(element[QLatin1String("id")].toInt(), i);
}
}
void DebugMessagesModelTest::testDetails()
{
for (int i = 0; i < 10; ++i) {
QVariantMap details = model.details(i);
QCOMPARE(details.value(QLatin1String("displayName")).toString(),
model.tr(messageTypes[i % (QtMsgType::QtInfoMsg + 1)]));
QCOMPARE(details.value(model.tr("Timestamp")).toString(),
QmlProfilerDataModel::formatTime(i));
QCOMPARE(details.value(model.tr("Message")).toString(),
QString::fromLatin1("message %1").arg(i));
QCOMPARE(details.value(model.tr("Location")).toString(),
QString::fromLatin1("somefile.js:%1").arg(i));
}
}
void DebugMessagesModelTest::testExpandedRow()
{
for (int i = 0; i < 10; ++i)
QCOMPARE(model.expandedRow(i), (i % (QtMsgType::QtInfoMsg + 1) + 1));
}
void DebugMessagesModelTest::testCollapsedRow()
{
for (int i = 0; i < 10; ++i)
QCOMPARE(model.collapsedRow(i), 1);
}
void DebugMessagesModelTest::testLocation()
{
QVariantMap expected;
expected[QLatin1String("file")] = QLatin1String("somefile.js");
for (int i = 0; i < 10; ++i) {
expected[QLatin1String("line")] = i;
expected[QLatin1String("column")] = 10 - i;
QCOMPARE(model.location(i), expected);
}
}
void DebugMessagesModelTest::cleanupTestCase()
{
model.clear();
QCOMPARE(model.count(), 0);
QCOMPARE(model.expandedRowCount(), 1);
QCOMPARE(model.collapsedRowCount(), 1);
}
} // namespace Internal
} // namespace QmlProfiler

View File

@@ -0,0 +1,58 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <qmlprofiler/debugmessagesmodel.h>
#include <qmlprofiler/qmlprofilermodelmanager.h>
#include <QObject>
namespace QmlProfiler {
namespace Internal {
class DebugMessagesModelTest : public QObject
{
Q_OBJECT
public:
DebugMessagesModelTest(QObject *parent = 0);
private slots:
void initTestCase();
void testTypeId();
void testColor();
void testLabels();
void testDetails();
void testExpandedRow();
void testCollapsedRow();
void testLocation();
void cleanupTestCase();
private:
QmlProfilerModelManager manager;
DebugMessagesModel model;
};
} // namespace Internal
} // namespace QmlProfiler

View File

@@ -0,0 +1,5 @@
SOURCES += \
$$PWD/debugmessagesmodel_test.cpp
HEADERS += \
$$PWD/debugmessagesmodel_test.h