Debian Codegraph @

src/imports/Test/plugin/uctestcase.cpp

raw
1 /*
2 * Copyright 2013-2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Christian Dywan <[email protected]>
17 */
18
19 #include "uctestcase.h"
20
21 #include <stdlib.h>
22
23 #include <QtCore/QDir>
24 #include <QtCore/QDebug>
25 #include <QtQml/QQmlEngine>
26 #include <QtQuick/QQuickItem>
27 #include <QtTest>
28
29 Q_DECLARE_METATYPE(QList<QQmlError>)
30
31 /*!
32 * \ingroup lomiri
33 * \brief LomiriTestCase is the C++ pendant to the QML LomiriTestCase.
34 */
35 LomiriTestCase::LomiriTestCase(const QString& file, ResizeMode resize, bool assertOnFailure, QWindow* parent) : QQuickView(parent)
36 {
37 // Create a dummy touch device for tests.
38 QTest::createTouchDevice();
39
40 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
41 QString modules(LOMIRI_QML_IMPORT_PATH);
42 Q_ASSERT(QDir(modules).exists());
43 QString modulePath(QDir(modules).absolutePath());
44 engine()->addImportPath(modulePath);
45 #endif
46
47 qRegisterMetaType<QList <QQmlError> >();
48 m_spy = new QSignalSpy(engine(), SIGNAL(warnings(QList<QQmlError>)));
49 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
50 m_spy->setParent(this);
51 #endif
52
53 Q_ASSERT(!file.isEmpty());
54 setResizeMode(resize);
55 setSource(QUrl::fromLocalFile(file));
56 if (assertOnFailure) {
57 Q_ASSERT(status() == QQuickView::Ready);
58 Q_ASSERT(rootObject());
59 }
60 if (rootObject()) {
61 show();
62 Q_ASSERT(QTest::qWaitForWindowExposed(this));
63 }
64 }
65
66 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
67 LomiriTestCase::~LomiriTestCase()
68 {
69 if (m_spy) {
70 delete m_spy;
71 }
72 }
73 #endif
74
75 /*!
76 * The number of all warnings from the point of loading the first line of QML code.
77 */
78 int LomiriTestCase::warnings() const
79 {
80 return m_spy->count();
81 }
82
83 /*!
84 * Ignore an expected warning message created using qmlWarning.
85 */
86 void LomiriTestCase::ignoreWarning(const QString& fileName, uint line,
87 uint column, const QString& message, uint occurences)
88 {
89 for (uint i = 0; i < occurences; i++) {
90 QString url(QUrl::fromLocalFile(QFileInfo(fileName).absoluteFilePath()).toEncoded());
91 QString warning(QString("%1:%2:%3: %4").arg(url).arg(line).arg(column).arg(message));
92 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
93 }
94 }
95
96 /*!
97 * Same as previous but without column number
98 */
99 void LomiriTestCase::ignoreWarning(const QString& fileName, uint line,
100 const QString& message, uint occurences)
101 {
102 for (uint i = 0; i < occurences; i++) {
103 QString url(QUrl::fromLocalFile(QFileInfo(fileName).absoluteFilePath()).toEncoded());
104 QString warning(QString("%1:%2 %3").arg(url).arg(line).arg(message));
105 QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
106 }
107 }