aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlinfo/tst_qqmlinfo.cpp
blob: ada3f9e37b13d9d0d7ff2c650ce479bca743ea83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <qtest.h>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QTimer>
#include <QQmlContext>
#include <qqmlinfo.h>
#include "../../shared/util.h"

class tst_qqmlinfo : public QQmlDataTest
{
    Q_OBJECT
public:
    tst_qqmlinfo() {}

private slots:
    void qmlObject();
    void nestedQmlObject();
    void nestedComponent();
    void nonQmlObject();
    void nullObject();
    void nonQmlContextedObject();
    void types();
    void chaining();
    void messageTypes();

private:
    QQmlEngine engine;
};

void tst_qqmlinfo::qmlObject()
{
    QQmlComponent component(&engine, testFileUrl("qmlObject.qml"));

    QObject *object = component.create();
    QVERIFY(object != nullptr);

    QString message = component.url().toString() + ":3:1: QML QtObject: Test Message";
    QTest::ignoreMessage(QtInfoMsg, qPrintable(message));
    qmlInfo(object) << "Test Message";

    QObject *nested = qvariant_cast<QObject *>(object->property("nested"));
    QVERIFY(nested != nullptr);

    message = component.url().toString() + ":6:13: QML QtObject: Second Test Message";
    QTest::ignoreMessage(QtInfoMsg, qPrintable(message));
    qmlInfo(nested) << "Second Test Message";
}

void tst_qqmlinfo::nestedQmlObject()
{
    QQmlComponent component(&engine, testFileUrl("nestedQmlObject.qml"));

    QObject *object = component.create();
    QVERIFY(object != nullptr);

    QObject *nested = qvariant_cast<QObject *>(object->property("nested"));
    QVERIFY(nested != nullptr);
    QObject *nested2 = qvariant_cast<QObject *>(object->property("nested2"));
    QVERIFY(nested2 != nullptr);

    QString message = component.url().toString() + ":5:13: QML NestedObject: Outer Object";
    QTest::ignoreMessage(QtInfoMsg, qPrintable(message));
    qmlInfo(nested) << "Outer Object";

    message = testFileUrl("NestedObject.qml").toString() + ":6:14: QML QtObject: Inner Object";
    QTest::ignoreMessage(QtInfoMsg, qPrintable(message));
    qmlInfo(nested2) << "Inner Object";
}

void tst_qqmlinfo::nestedComponent()
{
    QQmlComponent component(&engine, testFileUrl("NestedComponent.qml"));

    QObject *object = component.create();
    QVERIFY(object != nullptr);

    QObject *nested = qvariant_cast<QObject *>(object->property("nested"));
    QVERIFY(nested != nullptr);
    QObject *nested2 = qvariant_cast<QObject *>(object->property("nested2"));
    QVERIFY(nested2 != nullptr);

    QString message = component.url().toString() + ":10:9: QML NestedObject: Complex Object";
    QTest::ignoreMessage(QtInfoMsg, qPrintable(message));
    qmlInfo(nested) << "Complex Object";

    message = component.url().toString() + ":16:9: QML Image: Simple Object";
    QTest::ignoreMessage(QtInfoMsg, qPrintable(message));
    qmlInfo(nested2) << "Simple Object";
}

void tst_qqmlinfo::nonQmlObject()
{
    QObject object;
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: QML QtObject: Test Message");
    qmlInfo(&object) << "Test Message";

    QTimer nonQmlObject;
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: QML QTimer: Test Message");
    qmlInfo(&nonQmlObject) << "Test Message";
}

void tst_qqmlinfo::nullObject()
{
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: Null Object Test Message");
    qmlInfo(nullptr) << "Null Object Test Message";
}

void tst_qqmlinfo::nonQmlContextedObject()
{
    QObject object;
    QQmlContext context(&engine);
    QQmlEngine::setContextForObject(&object, &context);
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: QML QtObject: Test Message");
    qmlInfo(&object) << "Test Message";
}

void tst_qqmlinfo::types()
{
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: false");
    qmlInfo(nullptr) << false;

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: 1.1");
    qmlInfo(nullptr) << 1.1;

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: 1.2");
    qmlInfo(nullptr) << 1.2f;

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: 15");
    qmlInfo(nullptr) << 15;

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: 'b'");
    qmlInfo(nullptr) << QChar('b');

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: \"Qt\"");
    qmlInfo(nullptr) << QByteArray("Qt");

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: true");
    qmlInfo(nullptr) << bool(true);

    //### do we actually want QUrl to show up in the output?
    //### why the extra space at the end?
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: QUrl(\"http://www.qt-project.org\") ");
    qmlInfo(nullptr) << QUrl("http://www.qt-project.org");

    //### should this be quoted?
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: hello");
    qmlInfo(nullptr) << QLatin1String("hello");

    //### should this be quoted?
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: World");
    QString str("Hello World");
    QStringRef ref(&str, 6, 5);
    qmlInfo(nullptr) << ref;

    //### should this be quoted?
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: Quick");
    qmlInfo(nullptr) << QString ("Quick");
}

void tst_qqmlinfo::chaining()
{
    QString str("Hello World");
    QStringRef ref(&str, 6, 5);
    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: false 1.1 1.2 15 hello 'b' World \"Qt\" true Quick QUrl(\"http://www.qt-project.org\") ");
    qmlInfo(nullptr) << false << ' '
               << 1.1 << ' '
               << 1.2f << ' '
               << 15 << ' '
               << QLatin1String("hello") << ' '
               << QChar('b') << ' '
               << ref << ' '
               << QByteArray("Qt") << ' '
               << bool(true) << ' '
               << QString ("Quick") << ' '
               << QUrl("http://www.qt-project.org");
}

// Ensure that messages of different types are sent with the correct QtMsgType.
void tst_qqmlinfo::messageTypes()
{
    QTest::ignoreMessage(QtDebugMsg, "<Unknown File>: debug");
    qmlDebug(nullptr) << QLatin1String("debug");

    QTest::ignoreMessage(QtInfoMsg, "<Unknown File>: info");
    qmlInfo(nullptr) << QLatin1String("info");

    QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: warning");
    qmlWarning(nullptr) << QLatin1String("warning");
}

QTEST_MAIN(tst_qqmlinfo)

#include "tst_qqmlinfo.moc"