Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qqmlqt / tst_qqmlqt.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <private/qqmlengine_p.h>
42
43 #include <qtest.h>
44 #include <QDebug>
45 #include <QQmlEngine>
46 #include <QFontDatabase>
47 #include <QFileInfo>
48 #include <QQmlComponent>
49 #include <QDesktopServices>
50 #include <QDir>
51 #include <QCryptographicHash>
52 #include <QtQuick/QQuickItem>
53 #include <QSignalSpy>
54 #include <QVector2D>
55 #include <QVector3D>
56 #include <QVector4D>
57 #include <QQuaternion>
58 #include <QMatrix4x4>
59 #include <QFont>
60 #include "../../shared/util.h"
61
62 class tst_qqmlqt : public QQmlDataTest
63 {
64     Q_OBJECT
65 public:
66     tst_qqmlqt() {}
67
68 private slots:
69     void enums();
70     void rgba();
71     void hsla();
72     void colorEqual();
73     void rect();
74     void point();
75     void size();
76     void vector2d();
77     void vector3d();
78     void vector4d();
79     void quaternion();
80     void matrix4x4();
81     void font();
82     void lighter();
83     void darker();
84     void tint();
85     void openUrlExternally();
86     void openUrlExternally_pragmaLibrary();
87     void md5();
88     void createComponent();
89     void createComponent_pragmaLibrary();
90     void createQmlObject();
91     void dateTimeConversion();
92     void dateTimeFormatting();
93     void dateTimeFormatting_data();
94     void dateTimeFormattingVariants();
95     void dateTimeFormattingVariants_data();
96     void isQtObject();
97     void btoa();
98     void atob();
99     void fontFamilies();
100     void quit();
101     void resolvedUrl();
102
103 private:
104     QQmlEngine engine;
105 };
106
107 void tst_qqmlqt::enums()
108 {
109     QQmlComponent component(&engine, testFileUrl("enums.qml"));
110     QObject *object = component.create();
111     QVERIFY(object != 0);
112
113     QCOMPARE(object->property("test1").toInt(), (int)Qt::Key_Escape);
114     QCOMPARE(object->property("test2").toInt(), (int)Qt::DescendingOrder);
115     QCOMPARE(object->property("test3").toInt(), (int)Qt::ElideMiddle);
116     QCOMPARE(object->property("test4").toInt(), (int)Qt::AlignRight);
117
118     delete object;
119 }
120
121 void tst_qqmlqt::rgba()
122 {
123     QQmlComponent component(&engine, testFileUrl("rgba.qml"));
124
125     QString warning1 = component.url().toString() + ":6: Error: Qt.rgba(): Invalid arguments";
126     QString warning2 = component.url().toString() + ":7: Error: Qt.rgba(): Invalid arguments";
127     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
128     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
129
130     QObject *object = component.create();
131     QVERIFY(object != 0);
132
133
134     QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(1, 0, 0, 0.8));
135     QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor::fromRgbF(1, 0.5, 0.3, 1));
136     QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor());
137     QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor());
138     QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor::fromRgbF(1, 1, 1, 1));
139     QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor::fromRgbF(0, 0, 0, 0));
140
141     delete object;
142 }
143
144 void tst_qqmlqt::hsla()
145 {
146     QQmlComponent component(&engine, testFileUrl("hsla.qml"));
147
148     QString warning1 = component.url().toString() + ":6: Error: Qt.hsla(): Invalid arguments";
149     QString warning2 = component.url().toString() + ":7: Error: Qt.hsla(): Invalid arguments";
150     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
151     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
152
153     QObject *object = component.create();
154     QVERIFY(object != 0);
155
156     QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromHslF(1, 0, 0, 0.8));
157     QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor::fromHslF(1, 0.5, 0.3, 1));
158     QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor());
159     QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor());
160     QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor::fromHslF(1, 1, 1, 1));
161     QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor::fromHslF(0, 0, 0, 0));
162
163     delete object;
164 }
165
166 void tst_qqmlqt::colorEqual()
167 {
168     QQmlComponent component(&engine, testFileUrl("colorEqual.qml"));
169
170     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":6: Error: Qt.colorEqual(): Invalid arguments"));
171     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":7: Error: Qt.colorEqual(): Invalid arguments"));
172     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":9: Error: Qt.colorEqual(): Invalid color name"));
173     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":10: Error: Qt.colorEqual(): Invalid color name"));
174     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":12: Error: Qt.colorEqual(): Invalid arguments"));
175     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":13: Error: Qt.colorEqual(): Invalid arguments"));
176     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":17: Error: Qt.colorEqual(): Invalid arguments"));
177     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":18: Error: Qt.colorEqual(): Invalid arguments"));
178     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":34: Error: Qt.colorEqual(): Invalid color name"));
179     QTest::ignoreMessage(QtWarningMsg, qPrintable(component.url().toString() + ":35: Error: Qt.colorEqual(): Invalid color name"));
180
181     QObject *object = component.create();
182     QVERIFY(object != 0);
183
184     QCOMPARE(object->property("test1a").toBool(), false);
185     QCOMPARE(object->property("test1b").toBool(), false);
186     QCOMPARE(object->property("test1c").toBool(), false);
187     QCOMPARE(object->property("test1d").toBool(), false);
188     QCOMPARE(object->property("test1e").toBool(), false);
189     QCOMPARE(object->property("test1f").toBool(), false);
190     QCOMPARE(object->property("test1g").toBool(), false);
191     QCOMPARE(object->property("test1h").toBool(), false);
192
193     QCOMPARE(object->property("test2a").toBool(), true);
194     QCOMPARE(object->property("test2b").toBool(), true);
195     QCOMPARE(object->property("test2c").toBool(), true);
196     QCOMPARE(object->property("test2d").toBool(), true);
197     QCOMPARE(object->property("test2e").toBool(), true);
198     QCOMPARE(object->property("test2f").toBool(), true);
199     QCOMPARE(object->property("test2g").toBool(), true);
200     QCOMPARE(object->property("test2h").toBool(), true);
201     QCOMPARE(object->property("test2i").toBool(), false);
202     QCOMPARE(object->property("test2j").toBool(), false);
203     QCOMPARE(object->property("test2k").toBool(), false);
204     QCOMPARE(object->property("test2l").toBool(), false);
205     QCOMPARE(object->property("test2m").toBool(), false);
206     QCOMPARE(object->property("test2n").toBool(), false);
207
208     QCOMPARE(object->property("test3a").toBool(), true);
209     QCOMPARE(object->property("test3b").toBool(), true);
210     QCOMPARE(object->property("test3c").toBool(), true);
211     QCOMPARE(object->property("test3d").toBool(), true);
212     QCOMPARE(object->property("test3e").toBool(), true);
213     QCOMPARE(object->property("test3f").toBool(), true);
214     QCOMPARE(object->property("test3g").toBool(), false);
215     QCOMPARE(object->property("test3h").toBool(), false);
216     QCOMPARE(object->property("test3i").toBool(), true);
217     QCOMPARE(object->property("test3j").toBool(), true);
218     QCOMPARE(object->property("test3k").toBool(), true);
219     QCOMPARE(object->property("test3l").toBool(), true);
220     QCOMPARE(object->property("test3m").toBool(), true);
221     QCOMPARE(object->property("test3n").toBool(), true);
222
223     QCOMPARE(object->property("test4a").toBool(), true);
224     QCOMPARE(object->property("test4b").toBool(), true);
225     QCOMPARE(object->property("test4c").toBool(), false);
226     QCOMPARE(object->property("test4d").toBool(), false);
227     QCOMPARE(object->property("test4e").toBool(), false);
228     QCOMPARE(object->property("test4f").toBool(), false);
229     QCOMPARE(object->property("test4g").toBool(), false);
230     QCOMPARE(object->property("test4h").toBool(), false);
231     QCOMPARE(object->property("test4i").toBool(), false);
232     QCOMPARE(object->property("test4j").toBool(), false);
233
234     QCOMPARE(object->property("test5a").toBool(), true);
235     QCOMPARE(object->property("test5b").toBool(), true);
236     QCOMPARE(object->property("test5c").toBool(), true);
237     QCOMPARE(object->property("test5d").toBool(), false);
238     QCOMPARE(object->property("test5e").toBool(), false);
239
240     QCOMPARE(object->property("test6a").toBool(), true);
241     QCOMPARE(object->property("test6b").toBool(), true);
242     QCOMPARE(object->property("test6c").toBool(), true);
243     QCOMPARE(object->property("test6d").toBool(), false);
244     QCOMPARE(object->property("test6e").toBool(), false);
245
246     delete object;
247 }
248
249 void tst_qqmlqt::rect()
250 {
251     QQmlComponent component(&engine, testFileUrl("rect.qml"));
252
253     QString warning1 = component.url().toString() + ":6: Error: Qt.rect(): Invalid arguments";
254     QString warning2 = component.url().toString() + ":7: Error: Qt.rect(): Invalid arguments";
255     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
256     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
257
258     QObject *object = component.create();
259     QVERIFY(object != 0);
260
261     QCOMPARE(qvariant_cast<QRectF>(object->property("test1")), QRectF(10, 13, 100, 109));
262     QCOMPARE(qvariant_cast<QRectF>(object->property("test2")), QRectF(-10, 13, 100, 109.6));
263     QCOMPARE(qvariant_cast<QRectF>(object->property("test3")), QRectF());
264     QCOMPARE(qvariant_cast<QRectF>(object->property("test4")), QRectF());
265     QCOMPARE(qvariant_cast<QRectF>(object->property("test5")), QRectF(10, 13, 100, -109));
266
267     delete object;
268 }
269
270 void tst_qqmlqt::point()
271 {
272     QQmlComponent component(&engine, testFileUrl("point.qml"));
273
274     QString warning1 = component.url().toString() + ":6: Error: Qt.point(): Invalid arguments";
275     QString warning2 = component.url().toString() + ":7: Error: Qt.point(): Invalid arguments";
276     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
277     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
278
279     QObject *object = component.create();
280     QVERIFY(object != 0);
281
282     QCOMPARE(qvariant_cast<QPointF>(object->property("test1")), QPointF(19, 34));
283     QCOMPARE(qvariant_cast<QPointF>(object->property("test2")), QPointF(-3, 109.2));
284     QCOMPARE(qvariant_cast<QPointF>(object->property("test3")), QPointF());
285     QCOMPARE(qvariant_cast<QPointF>(object->property("test4")), QPointF());
286
287     delete object;
288 }
289
290 void tst_qqmlqt::size()
291 {
292     QQmlComponent component(&engine, testFileUrl("size.qml"));
293
294     QString warning1 = component.url().toString() + ":7: Error: Qt.size(): Invalid arguments";
295     QString warning2 = component.url().toString() + ":8: Error: Qt.size(): Invalid arguments";
296     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
297     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
298
299     QObject *object = component.create();
300     QVERIFY(object != 0);
301
302     QCOMPARE(qvariant_cast<QSizeF>(object->property("test1")), QSizeF(19, 34));
303     QCOMPARE(qvariant_cast<QSizeF>(object->property("test2")), QSizeF(3, 109.2));
304     QCOMPARE(qvariant_cast<QSizeF>(object->property("test3")), QSizeF(-3, 10));
305     QCOMPARE(qvariant_cast<QSizeF>(object->property("test4")), QSizeF());
306     QCOMPARE(qvariant_cast<QSizeF>(object->property("test5")), QSizeF());
307
308     delete object;
309 }
310
311 void tst_qqmlqt::vector2d()
312 {
313     QQmlComponent component(&engine, testFileUrl("vector2.qml"));
314
315     QString warning1 = component.url().toString() + ":6: Error: Qt.vector2d(): Invalid arguments";
316     QString warning2 = component.url().toString() + ":7: Error: Qt.vector2d(): Invalid arguments";
317     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
318     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
319
320     QObject *object = component.create();
321     QVERIFY(object != 0);
322
323     QCOMPARE(qvariant_cast<QVector2D>(object->property("test1")), QVector2D(1, 0.9));
324     QCOMPARE(qvariant_cast<QVector2D>(object->property("test2")), QVector2D(102, -982.1));
325     QCOMPARE(qvariant_cast<QVector2D>(object->property("test3")), QVector2D());
326     QCOMPARE(qvariant_cast<QVector2D>(object->property("test4")), QVector2D());
327
328     delete object;
329 }
330
331 void tst_qqmlqt::vector3d()
332 {
333     QQmlComponent component(&engine, testFileUrl("vector.qml"));
334
335     QString warning1 = component.url().toString() + ":6: Error: Qt.vector3d(): Invalid arguments";
336     QString warning2 = component.url().toString() + ":7: Error: Qt.vector3d(): Invalid arguments";
337     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
338     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
339
340     QObject *object = component.create();
341     QVERIFY(object != 0);
342
343     QCOMPARE(qvariant_cast<QVector3D>(object->property("test1")), QVector3D(1, 0, 0.9));
344     QCOMPARE(qvariant_cast<QVector3D>(object->property("test2")), QVector3D(102, -10, -982.1));
345     QCOMPARE(qvariant_cast<QVector3D>(object->property("test3")), QVector3D());
346     QCOMPARE(qvariant_cast<QVector3D>(object->property("test4")), QVector3D());
347
348     delete object;
349 }
350
351 void tst_qqmlqt::vector4d()
352 {
353     QQmlComponent component(&engine, testFileUrl("vector4.qml"));
354
355     QString warning1 = component.url().toString() + ":6: Error: Qt.vector4d(): Invalid arguments";
356     QString warning2 = component.url().toString() + ":7: Error: Qt.vector4d(): Invalid arguments";
357     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
358     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
359
360     QObject *object = component.create();
361     QVERIFY(object != 0);
362
363     QCOMPARE(qvariant_cast<QVector4D>(object->property("test1")), QVector4D(1, 0, 0.9, 0.6));
364     QCOMPARE(qvariant_cast<QVector4D>(object->property("test2")), QVector4D(102, -10, -982.1, 10));
365     QCOMPARE(qvariant_cast<QVector4D>(object->property("test3")), QVector4D());
366     QCOMPARE(qvariant_cast<QVector4D>(object->property("test4")), QVector4D());
367
368     delete object;
369 }
370
371 void tst_qqmlqt::quaternion()
372 {
373     QQmlComponent component(&engine, testFileUrl("quaternion.qml"));
374
375     QString warning1 = component.url().toString() + ":6: Error: Qt.quaternion(): Invalid arguments";
376     QString warning2 = component.url().toString() + ":7: Error: Qt.quaternion(): Invalid arguments";
377     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
378     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
379
380     QObject *object = component.create();
381     QVERIFY(object != 0);
382
383     QCOMPARE(qvariant_cast<QQuaternion>(object->property("test1")), QQuaternion(2, 17, 0.9, 0.6));
384     QCOMPARE(qvariant_cast<QQuaternion>(object->property("test2")), QQuaternion(102, -10, -982.1, 10));
385     QCOMPARE(qvariant_cast<QQuaternion>(object->property("test3")), QQuaternion());
386     QCOMPARE(qvariant_cast<QQuaternion>(object->property("test4")), QQuaternion());
387
388     delete object;
389 }
390
391 void tst_qqmlqt::matrix4x4()
392 {
393     QQmlComponent component(&engine, testFileUrl("matrix4x4.qml"));
394
395     QString warning1 = component.url().toString() + ":6: Error: Qt.matrix4x4(): Invalid arguments";
396     QString warning2 = component.url().toString() + ":7: Error: Qt.matrix4x4(): Invalid argument: not a valid matrix4x4 values array";
397     QString warning3 = component.url().toString() + ":8: Error: Qt.matrix4x4(): Invalid argument: not a valid matrix4x4 values array";
398     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
399     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
400     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning3));
401
402     QObject *object = component.create();
403     QVERIFY(object != 0);
404
405     QCOMPARE(qvariant_cast<QMatrix4x4>(object->property("test1")), QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
406     QCOMPARE(qvariant_cast<QMatrix4x4>(object->property("test2")), QMatrix4x4(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4));
407     QCOMPARE(qvariant_cast<QMatrix4x4>(object->property("test3")), QMatrix4x4());
408     QCOMPARE(qvariant_cast<QMatrix4x4>(object->property("test4")), QMatrix4x4());
409     QCOMPARE(qvariant_cast<QMatrix4x4>(object->property("test5")), QMatrix4x4());
410
411     delete object;
412 }
413
414 void tst_qqmlqt::font()
415 {
416     QQmlComponent component(&engine, testFileUrl("font.qml"));
417
418     QString warning1 = component.url().toString() + ":6: Error: Qt.font(): Invalid arguments";
419     QString warning2 = component.url().toString() + ":7: Error: Qt.font(): Invalid argument: no valid font subproperties specified";
420     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
421     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
422
423     QObject *object = component.create();
424     QVERIFY(object != 0);
425
426     QCOMPARE(qvariant_cast<QFont>(object->property("test1")), QFont("Arial", 22));
427     QCOMPARE(qvariant_cast<QFont>(object->property("test2")), QFont("Arial", 20, QFont::DemiBold, true));
428     QCOMPARE(qvariant_cast<QFont>(object->property("test3")), QFont());
429     QCOMPARE(qvariant_cast<QFont>(object->property("test4")), QFont());
430
431     delete object;
432 }
433
434 void tst_qqmlqt::lighter()
435 {
436     QQmlComponent component(&engine, testFileUrl("lighter.qml"));
437
438     QString warning1 = component.url().toString() + ":5: Error: Qt.lighter(): Invalid arguments";
439     QString warning2 = component.url().toString() + ":10: Error: Qt.lighter(): Invalid arguments";
440     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
441     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
442
443     QObject *object = component.create();
444     QVERIFY(object != 0);
445
446     QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(1, 0.8, 0.3).lighter());
447     QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor());
448     QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor::fromRgbF(1, 0.8, 0.3).lighter(180));
449     QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor("red").lighter());
450     QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor());
451     QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor());
452
453     delete object;
454 }
455
456 void tst_qqmlqt::darker()
457 {
458     QQmlComponent component(&engine, testFileUrl("darker.qml"));
459
460     QString warning1 = component.url().toString() + ":5: Error: Qt.darker(): Invalid arguments";
461     QString warning2 = component.url().toString() + ":10: Error: Qt.darker(): Invalid arguments";
462     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
463     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
464
465     QObject *object = component.create();
466     QVERIFY(object != 0);
467
468     QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(1, 0.8, 0.3).darker());
469     QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor());
470     QCOMPARE(qvariant_cast<QColor>(object->property("test3")), QColor::fromRgbF(1, 0.8, 0.3).darker(280));
471     QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor("red").darker());
472     QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor());
473     QCOMPARE(qvariant_cast<QColor>(object->property("test6")), QColor());
474
475     delete object;
476 }
477
478 void tst_qqmlqt::tint()
479 {
480     QQmlComponent component(&engine, testFileUrl("tint.qml"));
481
482     QString warning1 = component.url().toString() + ":7: Error: Qt.tint(): Invalid arguments";
483     QString warning2 = component.url().toString() + ":8: Error: Qt.tint(): Invalid arguments";
484
485     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
486     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
487
488     QObject *object = component.create();
489     QVERIFY(object != 0);
490
491     QCOMPARE(qvariant_cast<QColor>(object->property("test1")), QColor::fromRgbF(0, 0, 1));
492     QCOMPARE(qvariant_cast<QColor>(object->property("test2")), QColor::fromRgbF(1, 0, 0));
493     QColor test3 = qvariant_cast<QColor>(object->property("test3"));
494     QCOMPARE(test3.rgba(), 0xFF7F0080);
495     QCOMPARE(qvariant_cast<QColor>(object->property("test4")), QColor());
496     QCOMPARE(qvariant_cast<QColor>(object->property("test5")), QColor());
497
498     delete object;
499 }
500
501 class MyUrlHandler : public QObject
502 {
503     Q_OBJECT
504 public:
505     MyUrlHandler() : called(0) { }
506     int called;
507     QUrl last;
508
509 public slots:
510     void noteCall(const QUrl &url) { called++; last = url; }
511 };
512
513 void tst_qqmlqt::openUrlExternally()
514 {
515     MyUrlHandler handler;
516
517     QDesktopServices::setUrlHandler("test", &handler, "noteCall");
518     QDesktopServices::setUrlHandler("file", &handler, "noteCall");
519
520     QQmlComponent component(&engine, testFileUrl("openUrlExternally.qml"));
521     QObject *object = component.create();
522     QVERIFY(object != 0);
523     QCOMPARE(handler.called,1);
524     QCOMPARE(handler.last, QUrl("test:url"));
525
526     object->setProperty("testFile", true);
527
528     QCOMPARE(handler.called,2);
529     QCOMPARE(handler.last, testFileUrl("test.html"));
530
531     QDesktopServices::unsetUrlHandler("test");
532     QDesktopServices::unsetUrlHandler("file");
533 }
534
535 void tst_qqmlqt::openUrlExternally_pragmaLibrary()
536 {
537     MyUrlHandler handler;
538
539     QDesktopServices::setUrlHandler("test", &handler, "noteCall");
540     QDesktopServices::setUrlHandler("file", &handler, "noteCall");
541
542     QQmlComponent component(&engine, testFileUrl("openUrlExternally_lib.qml"));
543     QObject *object = component.create();
544     QVERIFY(object != 0);
545     QCOMPARE(handler.called,1);
546     QCOMPARE(handler.last, QUrl("test:url"));
547
548     object->setProperty("testFile", true);
549
550     QCOMPARE(handler.called,2);
551     QCOMPARE(handler.last, testFileUrl("test.html"));
552
553     QDesktopServices::unsetUrlHandler("test");
554     QDesktopServices::unsetUrlHandler("file");
555 }
556
557 void tst_qqmlqt::md5()
558 {
559     QQmlComponent component(&engine, testFileUrl("md5.qml"));
560
561     QString warning1 = component.url().toString() + ":4: Error: Qt.md5(): Invalid arguments";
562     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
563
564     QObject *object = component.create();
565     QVERIFY(object != 0);
566
567     QCOMPARE(object->property("test2").toString(), QLatin1String(QCryptographicHash::hash("Hello World", QCryptographicHash::Md5).toHex()));
568
569     delete object;
570 }
571
572 void tst_qqmlqt::createComponent()
573 {
574     {
575     QQmlComponent component(&engine, testFileUrl("createComponent.qml"));
576
577     QString warning1 = component.url().toString() + ":9: Error: Qt.createComponent(): Invalid arguments";
578     QString warning2 = component.url().toString() + ":10: Error: Qt.createComponent(): Invalid arguments";
579     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
580     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
581
582     QObject *object = component.create();
583     QVERIFY(object != 0);
584
585     QCOMPARE(object->property("absoluteUrl").toString(), QString("http://www.example.com/test.qml"));
586     QCOMPARE(object->property("relativeUrl").toString(), testFileUrl("createComponentData.qml").toString());
587
588     QTRY_VERIFY(object->property("asyncResult").toBool());
589
590     delete object;
591     }
592
593     // simultaneous sync and async compilation
594     {
595     QQmlComponent component(&engine, testFileUrl("createComponent.2.qml"));
596     QObject *object = component.create();
597     QVERIFY(object != 0);
598     QTRY_VERIFY(object->property("success").toBool());
599     delete object;
600     }
601 }
602
603 void tst_qqmlqt::createComponent_pragmaLibrary()
604 {
605     // Currently, just loading createComponent_lib.qml causes crash on some platforms
606     QQmlComponent component(&engine, testFileUrl("createComponent_lib.qml"));
607     QObject *object = component.create();
608     QVERIFY(object != 0);
609     QCOMPARE(object->property("status").toInt(), int(QQmlComponent::Ready));
610     QCOMPARE(object->property("readValue").toInt(), int(1913));
611     delete object;
612 }
613
614 void tst_qqmlqt::createQmlObject()
615 {
616     QQmlComponent component(&engine, testFileUrl("createQmlObject.qml"));
617
618     QString warning1 = component.url().toString() + ":7: Error: Qt.createQmlObject(): Invalid arguments";
619     QString warning2 = component.url().toString()+ ":10: Error: Qt.createQmlObject(): failed to create object: \n    " + testFileUrl("inline").toString() + ":2:10: Blah is not a type";
620     QString warning3 = component.url().toString()+ ":11: Error: Qt.createQmlObject(): failed to create object: \n    " + testFileUrl("main.qml").toString() + ":4:14: Duplicate property name";
621     QString warning4 = component.url().toString()+ ":9: Error: Qt.createQmlObject(): Missing parent object";
622     QString warning5 = component.url().toString()+ ":8: Error: Qt.createQmlObject(): Invalid arguments";
623     QString warning6 = "RunTimeError:  Qt.createQmlObject(): failed to create object: \n    " + testFileUrl("inline").toString() + ":3: Cannot assign object type QObject with no default method";
624     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
625     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
626     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning3));
627     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning4));
628     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning5));
629     QTest::ignoreMessage(QtDebugMsg, qPrintable(warning6));
630
631     QObject *object = component.create();
632     QVERIFY(object != 0);
633
634     QCOMPARE(object->property("emptyArg").toBool(), true);
635     QCOMPARE(object->property("success").toBool(), true);
636
637     QQuickItem *item = qobject_cast<QQuickItem *>(object);
638     QVERIFY(item != 0);
639     QVERIFY(item->childItems().count() == 1);
640
641     delete object;
642 }
643
644
645 void tst_qqmlqt::dateTimeConversion()
646 {
647     QDate date(2008,12,24);
648     QTime time(14,15,38,200);
649     QDateTime dateTime(date, time);
650     //Note that when converting Date to QDateTime they can argue over historical DST data when converting to local time.
651     //Tests should use UTC or recent dates.
652     QDateTime dateTime2(QDate(2852,12,31), QTime(23,59,59,500));
653     QDateTime dateTime3(QDate(2000,1,1), QTime(0,0,0,0));
654     QDateTime dateTime4(QDate(2001,2,2), QTime(0,0,0,0));
655     QDateTime dateTime5(QDate(1999,1,1), QTime(2,3,4,0));
656     QDateTime dateTime6(QDate(2008,2,24), QTime(14,15,38,200));
657     QDateTime dateTime7(QDate(1970,1,1), QTime(0,0,0,0), Qt::UTC);
658     QDateTime dateTime8(QDate(1586,2,2), QTime(0,0,0,0), Qt::UTC);
659     QDateTime dateTime9(QDate(955,1,1), QTime(0,0,0,0), Qt::UTC);
660     QDateTime dateTime10(QDate(113,2,24), QTime(14,15,38,200), Qt::UTC);
661
662     QQmlEngine eng;
663     QQmlComponent component(&eng, testFileUrl("dateTimeConversion.qml"));
664     QObject *obj = component.create();
665
666     QCOMPARE(obj->property("qdate").toDate(), date);
667     QCOMPARE(obj->property("qtime").toTime(), time);
668     QCOMPARE(obj->property("qdatetime").toDateTime(), dateTime);
669     QCOMPARE(obj->property("qdatetime2").toDateTime(), dateTime2);
670     QCOMPARE(obj->property("qdatetime3").toDateTime(), dateTime3);
671     QCOMPARE(obj->property("qdatetime4").toDateTime(), dateTime4);
672     QCOMPARE(obj->property("qdatetime5").toDateTime(), dateTime5);
673     QCOMPARE(obj->property("qdatetime6").toDateTime(), dateTime6);
674     QCOMPARE(obj->property("qdatetime7").toDateTime(), dateTime7);
675     QCOMPARE(obj->property("qdatetime8").toDateTime(), dateTime8);
676     QCOMPARE(obj->property("qdatetime9").toDateTime(), dateTime9);
677     QCOMPARE(obj->property("qdatetime10").toDateTime(), dateTime10);
678 }
679
680 void tst_qqmlqt::dateTimeFormatting()
681 {
682     QFETCH(QString, method);
683     QFETCH(QStringList, inputProperties);
684     QFETCH(QStringList, expectedResults);
685
686     QDate date(2008,12,24);
687     QTime time(14,15,38,200);
688     QDateTime dateTime(date, time);
689
690     QQmlEngine eng;
691
692     eng.rootContext()->setContextProperty("qdate", date);
693     eng.rootContext()->setContextProperty("qtime", time);
694     eng.rootContext()->setContextProperty("qdatetime", dateTime);
695
696     QQmlComponent component(&eng, testFileUrl("formatting.qml"));
697
698     QStringList warnings;
699     warnings << component.url().toString() + ":37: Error: Qt.formatDate(): Invalid date format"
700         << component.url().toString() + ":36: Error: Qt.formatDate(): Invalid arguments"
701         << component.url().toString() + ":40: Error: Qt.formatTime(): Invalid time format"
702         << component.url().toString() + ":39: Error: Qt.formatTime(): Invalid arguments"
703         << component.url().toString() + ":43: Error: Qt.formatDateTime(): Invalid datetime format"
704         << component.url().toString() + ":42: Error: Qt.formatDateTime(): Invalid arguments";
705
706     foreach (const QString &warning, warnings)
707         QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
708
709     QObject *object = component.create();
710     QVERIFY2(component.errorString().isEmpty(), qPrintable(component.errorString()));
711     QVERIFY(object != 0);
712
713     QVERIFY(inputProperties.count() > 0);
714     QVariant result;
715     foreach(const QString &prop, inputProperties) {
716         QVERIFY(QMetaObject::invokeMethod(object, method.toUtf8().constData(),
717                 Q_RETURN_ARG(QVariant, result),
718                 Q_ARG(QVariant, prop)));
719         QStringList output = result.toStringList();
720         QCOMPARE(output.size(), expectedResults.size());
721         for (int i=0; i<output.count(); i++)
722             QCOMPARE(output[i], expectedResults[i]);
723     }
724
725     delete object;
726 }
727
728 void tst_qqmlqt::dateTimeFormatting_data()
729 {
730     QTest::addColumn<QString>("method");
731     QTest::addColumn<QStringList>("inputProperties");
732     QTest::addColumn<QStringList>("expectedResults");
733
734     QDate date(2008,12,24);
735     QTime time(14,15,38,200);
736     QDateTime dateTime(date, time);
737
738     QTest::newRow("formatDate")
739         << "formatDate"
740         << (QStringList() << "dateFromString" << "jsdate" << "qdate" << "qdatetime")
741         << (QStringList() << date.toString(Qt::DefaultLocaleShortDate)
742                           << date.toString(Qt::DefaultLocaleLongDate)
743                           << date.toString("ddd MMMM d yy"));
744
745     QTest::newRow("formatTime")
746         << "formatTime"
747         << (QStringList() << "jsdate" << "qtime" << "qdatetime")
748         << (QStringList() << time.toString(Qt::DefaultLocaleShortDate)
749                           << time.toString(Qt::DefaultLocaleLongDate)
750                           << time.toString("H:m:s a")
751                           << time.toString("hh:mm:ss.zzz"));
752
753     QTest::newRow("formatDateTime")
754         << "formatDateTime"
755         << (QStringList() << "jsdate" << "qdatetime")
756         << (QStringList() << dateTime.toString(Qt::DefaultLocaleShortDate)
757                           << dateTime.toString(Qt::DefaultLocaleLongDate)
758                           << dateTime.toString("M/d/yy H:m:s a"));
759 }
760
761 void tst_qqmlqt::dateTimeFormattingVariants()
762 {
763     QFETCH(QString, method);
764     QFETCH(QVariant, variant);
765     QFETCH(QStringList, expectedResults);
766
767     QQmlEngine eng;
768     eng.rootContext()->setContextProperty("qvariant", variant);
769     QQmlComponent component(&eng, testFileUrl("formatting.qml"));
770
771     QStringList warnings;
772     warnings << component.url().toString() + ":37: Error: Qt.formatDate(): Invalid date format"
773         << component.url().toString() + ":36: Error: Qt.formatDate(): Invalid arguments"
774         << component.url().toString() + ":40: Error: Qt.formatTime(): Invalid time format"
775         << component.url().toString() + ":39: Error: Qt.formatTime(): Invalid arguments"
776         << component.url().toString() + ":43: Error: Qt.formatDateTime(): Invalid datetime format"
777         << component.url().toString() + ":42: Error: Qt.formatDateTime(): Invalid arguments";
778
779     foreach (const QString &warning, warnings)
780         QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
781
782     QObject *object = component.create();
783     QVERIFY2(component.errorString().isEmpty(), qPrintable(component.errorString()));
784     QVERIFY(object != 0);
785
786     QVariant result;
787     QVERIFY(QMetaObject::invokeMethod(object, method.toUtf8().constData(),
788             Q_RETURN_ARG(QVariant, result),
789             Q_ARG(QVariant, QString(QLatin1String("qvariant")))));
790     QStringList output = result.toStringList();
791     QCOMPARE(output, expectedResults);
792
793     delete object;
794 }
795
796 void tst_qqmlqt::dateTimeFormattingVariants_data()
797 {
798     QTest::addColumn<QString>("method");
799     QTest::addColumn<QVariant>("variant");
800     QTest::addColumn<QStringList>("expectedResults");
801
802     QDateTime temporary;
803
804     QTime time(11, 16, 39, 755);
805     temporary = QDateTime(QDate(1970,1,1), time);
806     QTest::newRow("formatDate, qtime") << "formatDate" << QVariant::fromValue(time) << (QStringList() << temporary.date().toString(Qt::DefaultLocaleShortDate) << temporary.date().toString(Qt::DefaultLocaleLongDate) << temporary.date().toString("ddd MMMM d yy"));
807     QTest::newRow("formatDateTime, qtime") << "formatDateTime" << QVariant::fromValue(time) << (QStringList() << temporary.toString(Qt::DefaultLocaleShortDate) << temporary.toString(Qt::DefaultLocaleLongDate) << temporary.toString("M/d/yy H:m:s a"));
808     QTest::newRow("formatTime, qtime") << "formatTime" << QVariant::fromValue(time) << (QStringList() << temporary.time().toString(Qt::DefaultLocaleShortDate) << temporary.time().toString(Qt::DefaultLocaleLongDate) << temporary.time().toString("H:m:s a") << temporary.time().toString("hh:mm:ss.zzz"));
809
810     QDate date(2011,5,31);
811     temporary = QDateTime(date);
812     QTest::newRow("formatDate, qdate") << "formatDate" << QVariant::fromValue(date) << (QStringList() << temporary.date().toString(Qt::DefaultLocaleShortDate) << temporary.date().toString(Qt::DefaultLocaleLongDate) << temporary.date().toString("ddd MMMM d yy"));
813     QTest::newRow("formatDateTime, qdate") << "formatDateTime" << QVariant::fromValue(date) << (QStringList() << temporary.toString(Qt::DefaultLocaleShortDate) << temporary.toString(Qt::DefaultLocaleLongDate) << temporary.toString("M/d/yy H:m:s a"));
814     QTest::newRow("formatTime, qdate") << "formatTime" << QVariant::fromValue(date) << (QStringList() << temporary.time().toString(Qt::DefaultLocaleShortDate) << temporary.time().toString(Qt::DefaultLocaleLongDate) << temporary.time().toString("H:m:s a") << temporary.time().toString("hh:mm:ss.zzz"));
815
816     QDateTime dateTime(date, time);
817     temporary = dateTime;
818     QTest::newRow("formatDate, qdatetime") << "formatDate" << QVariant::fromValue(dateTime) << (QStringList() << temporary.date().toString(Qt::DefaultLocaleShortDate) << temporary.date().toString(Qt::DefaultLocaleLongDate) << temporary.date().toString("ddd MMMM d yy"));
819     QTest::newRow("formatDateTime, qdatetime") << "formatDateTime" << QVariant::fromValue(dateTime) << (QStringList() << temporary.toString(Qt::DefaultLocaleShortDate) << temporary.toString(Qt::DefaultLocaleLongDate) << temporary.toString("M/d/yy H:m:s a"));
820     QTest::newRow("formatTime, qdatetime") << "formatTime" << QVariant::fromValue(dateTime) << (QStringList() << temporary.time().toString(Qt::DefaultLocaleShortDate) << temporary.time().toString(Qt::DefaultLocaleLongDate) << temporary.time().toString("H:m:s a") << temporary.time().toString("hh:mm:ss.zzz"));
821
822     QString string(QLatin1String("2011/05/31 11:16:39.755"));
823     temporary = QDateTime::fromString(string, "yyyy/MM/dd HH:mm:ss.zzz");
824     QTest::newRow("formatDate, qstring") << "formatDate" << QVariant::fromValue(string) << (QStringList() << temporary.date().toString(Qt::DefaultLocaleShortDate) << temporary.date().toString(Qt::DefaultLocaleLongDate) << temporary.date().toString("ddd MMMM d yy"));
825     QTest::newRow("formatDateTime, qstring") << "formatDateTime" << QVariant::fromValue(string) << (QStringList() << temporary.toString(Qt::DefaultLocaleShortDate) << temporary.toString(Qt::DefaultLocaleLongDate) << temporary.toString("M/d/yy H:m:s a"));
826     QTest::newRow("formatTime, qstring") << "formatTime" << QVariant::fromValue(string) << (QStringList() << temporary.time().toString(Qt::DefaultLocaleShortDate) << temporary.time().toString(Qt::DefaultLocaleLongDate) << temporary.time().toString("H:m:s a") << temporary.time().toString("hh:mm:ss.zzz"));
827
828     QColor color(Qt::red);
829     temporary = QVariant::fromValue(color).toDateTime();
830     QTest::newRow("formatDate, qcolor") << "formatDate" << QVariant::fromValue(color) << (QStringList() << temporary.date().toString(Qt::DefaultLocaleShortDate) << temporary.date().toString(Qt::DefaultLocaleLongDate) << temporary.date().toString("ddd MMMM d yy"));
831     QTest::newRow("formatDateTime, qcolor") << "formatDateTime" << QVariant::fromValue(color) << (QStringList() << temporary.toString(Qt::DefaultLocaleShortDate) << temporary.toString(Qt::DefaultLocaleLongDate) << temporary.toString("M/d/yy H:m:s a"));
832     QTest::newRow("formatTime, qcolor") << "formatTime" << QVariant::fromValue(color) << (QStringList() << temporary.time().toString(Qt::DefaultLocaleShortDate) << temporary.time().toString(Qt::DefaultLocaleLongDate) << temporary.time().toString("H:m:s a") << temporary.time().toString("hh:mm:ss.zzz"));
833
834     int integer(4);
835     temporary = QVariant::fromValue(integer).toDateTime();
836     QTest::newRow("formatDate, int") << "formatDate" << QVariant::fromValue(integer) << (QStringList() << temporary.date().toString(Qt::DefaultLocaleShortDate) << temporary.date().toString(Qt::DefaultLocaleLongDate) << temporary.date().toString("ddd MMMM d yy"));
837     QTest::newRow("formatDateTime, int") << "formatDateTime" << QVariant::fromValue(integer) << (QStringList() << temporary.toString(Qt::DefaultLocaleShortDate) << temporary.toString(Qt::DefaultLocaleLongDate) << temporary.toString("M/d/yy H:m:s a"));
838     QTest::newRow("formatTime, int") << "formatTime" << QVariant::fromValue(integer) << (QStringList() << temporary.time().toString(Qt::DefaultLocaleShortDate) << temporary.time().toString(Qt::DefaultLocaleLongDate) << temporary.time().toString("H:m:s a") << temporary.time().toString("hh:mm:ss.zzz"));
839 }
840
841 void tst_qqmlqt::isQtObject()
842 {
843     QQmlComponent component(&engine, testFileUrl("isQtObject.qml"));
844     QObject *object = component.create();
845     QVERIFY(object != 0);
846
847     QCOMPARE(object->property("test1").toBool(), true);
848     QCOMPARE(object->property("test2").toBool(), false);
849     QCOMPARE(object->property("test3").toBool(), false);
850     QCOMPARE(object->property("test4").toBool(), false);
851     QCOMPARE(object->property("test5").toBool(), false);
852
853     delete object;
854 }
855
856 void tst_qqmlqt::btoa()
857 {
858     QQmlComponent component(&engine, testFileUrl("btoa.qml"));
859
860     QString warning1 = component.url().toString() + ":4: Error: Qt.btoa(): Invalid arguments";
861     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
862
863     QObject *object = component.create();
864     QVERIFY(object != 0);
865
866     QCOMPARE(object->property("test2").toString(), QString("SGVsbG8gd29ybGQh"));
867
868     delete object;
869 }
870
871 void tst_qqmlqt::atob()
872 {
873     QQmlComponent component(&engine, testFileUrl("atob.qml"));
874
875     QString warning1 = component.url().toString() + ":4: Error: Qt.atob(): Invalid arguments";
876     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
877
878     QObject *object = component.create();
879     QVERIFY(object != 0);
880
881     QCOMPARE(object->property("test2").toString(), QString("Hello world!"));
882
883     delete object;
884 }
885
886 void tst_qqmlqt::fontFamilies()
887 {
888     QQmlComponent component(&engine, testFileUrl("fontFamilies.qml"));
889
890     QString warning1 = component.url().toString() + ":4: Error: Qt.fontFamilies(): Invalid arguments";
891     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
892
893     QObject *object = component.create();
894     QVERIFY(object != 0);
895
896     QFontDatabase database;
897     QCOMPARE(object->property("test2"), QVariant::fromValue(database.families()));
898
899     delete object;
900 }
901
902 void tst_qqmlqt::quit()
903 {
904     QQmlComponent component(&engine, testFileUrl("quit.qml"));
905
906     QSignalSpy spy(&engine, SIGNAL(quit()));
907     QObject *object = component.create();
908     QVERIFY(object != 0);
909     QCOMPARE(spy.count(), 1);
910
911     delete object;
912 }
913
914 void tst_qqmlqt::resolvedUrl()
915 {
916     QQmlComponent component(&engine, testFileUrl("resolvedUrl.qml"));
917
918     QObject *object = component.create();
919     QVERIFY(object != 0);
920
921     QCOMPARE(object->property("result").toString(), component.url().toString());
922     QCOMPARE(object->property("isString").toBool(), true);
923
924     delete object;
925 }
926
927 QTEST_MAIN(tst_qqmlqt)
928
929 #include "tst_qqmlqt.moc"