Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeinfo / tst_qdeclarativeinfo.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
45 #include <QPushButton>
46 #include <QDeclarativeContext>
47 #include <qdeclarativeinfo.h>
48
49 #ifdef Q_OS_SYMBIAN
50 // In Symbian OS test data is located in applications private dir
51 #define SRCDIR "."
52 #endif
53
54 class tst_qdeclarativeinfo : public QObject
55 {
56     Q_OBJECT
57 public:
58     tst_qdeclarativeinfo() {}
59
60 private slots:
61     void qmlObject();
62     void nestedQmlObject();
63     void nonQmlObject();
64     void nullObject();
65     void nonQmlContextedObject();
66     void types();
67     void chaining();
68
69 private:
70     QDeclarativeEngine engine;
71 };
72
73 inline QUrl TEST_FILE(const QString &filename)
74 {
75     return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
76 }
77
78 void tst_qdeclarativeinfo::qmlObject()
79 {
80     QDeclarativeComponent component(&engine, TEST_FILE("qmlObject.qml"));
81
82     QObject *object = component.create();
83     QVERIFY(object != 0);
84
85     QString message = component.url().toString() + ":3:1: QML QObject_QML_0: Test Message";
86     QTest::ignoreMessage(QtWarningMsg, qPrintable(message));
87     qmlInfo(object) << "Test Message";
88
89     QObject *nested = qvariant_cast<QObject *>(object->property("nested"));
90     QVERIFY(nested != 0);
91
92     message = component.url().toString() + ":6:13: QML QtObject: Second Test Message";
93     QTest::ignoreMessage(QtWarningMsg, qPrintable(message));
94     qmlInfo(nested) << "Second Test Message";
95 }
96
97 void tst_qdeclarativeinfo::nestedQmlObject()
98 {
99     QDeclarativeComponent component(&engine, TEST_FILE("nestedQmlObject.qml"));
100
101     QObject *object = component.create();
102     QVERIFY(object != 0);
103
104     QObject *nested = qvariant_cast<QObject *>(object->property("nested"));
105     QVERIFY(nested != 0);
106     QObject *nested2 = qvariant_cast<QObject *>(object->property("nested2"));
107     QVERIFY(nested2 != 0);
108
109     QString message = component.url().toString() + ":5:13: QML NestedObject: Outer Object";
110     QTest::ignoreMessage(QtWarningMsg, qPrintable(message));
111     qmlInfo(nested) << "Outer Object";
112
113     message = TEST_FILE("NestedObject.qml").toString() + ":6:14: QML QtObject: Inner Object";
114     QTest::ignoreMessage(QtWarningMsg, qPrintable(message));
115     qmlInfo(nested2) << "Inner Object";
116 }
117
118 void tst_qdeclarativeinfo::nonQmlObject()
119 {
120     QObject object;
121     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML QtObject: Test Message");
122     qmlInfo(&object) << "Test Message";
123
124     QPushButton pbObject;
125     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML QPushButton: Test Message");
126     qmlInfo(&pbObject) << "Test Message";
127 }
128
129 void tst_qdeclarativeinfo::nullObject()
130 {
131     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: Null Object Test Message");
132     qmlInfo(0) << "Null Object Test Message";
133 }
134
135 void tst_qdeclarativeinfo::nonQmlContextedObject()
136 {
137     QObject object;
138     QDeclarativeContext context(&engine);
139     QDeclarativeEngine::setContextForObject(&object, &context);
140     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML QtObject: Test Message");
141     qmlInfo(&object) << "Test Message";
142 }
143
144 void tst_qdeclarativeinfo::types()
145 {
146     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: false");
147     qmlInfo(0) << false;
148
149     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: 1.1");
150     qmlInfo(0) << 1.1;
151
152     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: 1.2");
153     qmlInfo(0) << 1.2f;
154
155     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: 15");
156     qmlInfo(0) << 15;
157
158     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: 'b'");
159     qmlInfo(0) << QChar('b');
160
161     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: \"Qt\"");
162     qmlInfo(0) << QByteArray("Qt");
163
164     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: true");
165     qmlInfo(0) << QBool(true);
166
167     //### do we actually want QUrl to show up in the output?
168     //### why the extra space at the end?
169     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QUrl(\"http://qt.nokia.com\") ");
170     qmlInfo(0) << QUrl("http://qt.nokia.com");
171
172     //### should this be quoted?
173     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: hello");
174     qmlInfo(0) << QLatin1String("hello");
175
176     //### should this be quoted?
177     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: World");
178     QString str("Hello World");
179     QStringRef ref(&str, 6, 5);
180     qmlInfo(0) << ref;
181
182     //### should this be quoted?
183     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: Quick");
184     qmlInfo(0) << QString ("Quick");
185 }
186
187 void tst_qdeclarativeinfo::chaining()
188 {
189     //### should more of these be automatically inserting spaces?
190     QString str("Hello World");
191     QStringRef ref(&str, 6, 5);
192     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: false 1.1 1.2 15 hello 'b' QUrl(\"http://qt.nokia.com\") World \"Qt\" true Quick ");
193     qmlInfo(0) << false << ' '
194                << 1.1 << ' '
195                << 1.2f << ' '
196                << 15 << ' '
197                << QLatin1String("hello") << ' '
198                << QChar('b') << ' '
199                << QUrl("http://qt.nokia.com")
200                << ref
201                << QByteArray("Qt")
202                << QBool(true)
203                << QString ("Quick");
204 }
205
206 QTEST_MAIN(tst_qdeclarativeinfo)
207
208 #include "tst_qdeclarativeinfo.moc"