1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
41 #include <private/qqmlengine_p.h>
46 #include <QFontDatabase>
48 #include <QQmlComponent>
49 #include <QDesktopServices>
51 #include <QCryptographicHash>
52 #include <QtQuick/QQuickItem>
57 #include <QQuaternion>
60 #include "../../shared/util.h"
62 class tst_qqmlqt : public QQmlDataTest
85 void openUrlExternally();
86 void openUrlExternally_pragmaLibrary();
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();
107 void tst_qqmlqt::enums()
109 QQmlComponent component(&engine, testFileUrl("enums.qml"));
110 QObject *object = component.create();
111 QVERIFY(object != 0);
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);
121 void tst_qqmlqt::rgba()
123 QQmlComponent component(&engine, testFileUrl("rgba.qml"));
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));
130 QObject *object = component.create();
131 QVERIFY(object != 0);
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));
144 void tst_qqmlqt::hsla()
146 QQmlComponent component(&engine, testFileUrl("hsla.qml"));
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));
153 QObject *object = component.create();
154 QVERIFY(object != 0);
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));
166 void tst_qqmlqt::colorEqual()
168 QQmlComponent component(&engine, testFileUrl("colorEqual.qml"));
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"));
181 QObject *object = component.create();
182 QVERIFY(object != 0);
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);
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);
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);
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);
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);
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);
249 void tst_qqmlqt::rect()
251 QQmlComponent component(&engine, testFileUrl("rect.qml"));
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));
258 QObject *object = component.create();
259 QVERIFY(object != 0);
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));
270 void tst_qqmlqt::point()
272 QQmlComponent component(&engine, testFileUrl("point.qml"));
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));
279 QObject *object = component.create();
280 QVERIFY(object != 0);
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());
290 void tst_qqmlqt::size()
292 QQmlComponent component(&engine, testFileUrl("size.qml"));
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));
299 QObject *object = component.create();
300 QVERIFY(object != 0);
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());
311 void tst_qqmlqt::vector2d()
313 QQmlComponent component(&engine, testFileUrl("vector2.qml"));
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));
320 QObject *object = component.create();
321 QVERIFY(object != 0);
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());
331 void tst_qqmlqt::vector3d()
333 QQmlComponent component(&engine, testFileUrl("vector.qml"));
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));
340 QObject *object = component.create();
341 QVERIFY(object != 0);
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());
351 void tst_qqmlqt::vector4d()
353 QQmlComponent component(&engine, testFileUrl("vector4.qml"));
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));
360 QObject *object = component.create();
361 QVERIFY(object != 0);
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());
371 void tst_qqmlqt::quaternion()
373 QQmlComponent component(&engine, testFileUrl("quaternion.qml"));
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));
380 QObject *object = component.create();
381 QVERIFY(object != 0);
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());
391 void tst_qqmlqt::matrix4x4()
393 QQmlComponent component(&engine, testFileUrl("matrix4x4.qml"));
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));
402 QObject *object = component.create();
403 QVERIFY(object != 0);
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());
414 void tst_qqmlqt::font()
416 QQmlComponent component(&engine, testFileUrl("font.qml"));
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));
423 QObject *object = component.create();
424 QVERIFY(object != 0);
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());
434 void tst_qqmlqt::lighter()
436 QQmlComponent component(&engine, testFileUrl("lighter.qml"));
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));
443 QObject *object = component.create();
444 QVERIFY(object != 0);
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());
456 void tst_qqmlqt::darker()
458 QQmlComponent component(&engine, testFileUrl("darker.qml"));
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));
465 QObject *object = component.create();
466 QVERIFY(object != 0);
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());
478 void tst_qqmlqt::tint()
480 QQmlComponent component(&engine, testFileUrl("tint.qml"));
482 QString warning1 = component.url().toString() + ":7: Error: Qt.tint(): Invalid arguments";
483 QString warning2 = component.url().toString() + ":8: Error: Qt.tint(): Invalid arguments";
485 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
486 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
488 QObject *object = component.create();
489 QVERIFY(object != 0);
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());
501 class MyUrlHandler : public QObject
505 MyUrlHandler() : called(0) { }
510 void noteCall(const QUrl &url) { called++; last = url; }
513 void tst_qqmlqt::openUrlExternally()
515 MyUrlHandler handler;
517 QDesktopServices::setUrlHandler("test", &handler, "noteCall");
518 QDesktopServices::setUrlHandler("file", &handler, "noteCall");
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"));
526 object->setProperty("testFile", true);
528 QCOMPARE(handler.called,2);
529 QCOMPARE(handler.last, testFileUrl("test.html"));
531 QDesktopServices::unsetUrlHandler("test");
532 QDesktopServices::unsetUrlHandler("file");
535 void tst_qqmlqt::openUrlExternally_pragmaLibrary()
537 MyUrlHandler handler;
539 QDesktopServices::setUrlHandler("test", &handler, "noteCall");
540 QDesktopServices::setUrlHandler("file", &handler, "noteCall");
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"));
548 object->setProperty("testFile", true);
550 QCOMPARE(handler.called,2);
551 QCOMPARE(handler.last, testFileUrl("test.html"));
553 QDesktopServices::unsetUrlHandler("test");
554 QDesktopServices::unsetUrlHandler("file");
557 void tst_qqmlqt::md5()
559 QQmlComponent component(&engine, testFileUrl("md5.qml"));
561 QString warning1 = component.url().toString() + ":4: Error: Qt.md5(): Invalid arguments";
562 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
564 QObject *object = component.create();
565 QVERIFY(object != 0);
567 QCOMPARE(object->property("test2").toString(), QLatin1String(QCryptographicHash::hash("Hello World", QCryptographicHash::Md5).toHex()));
572 void tst_qqmlqt::createComponent()
575 QQmlComponent component(&engine, testFileUrl("createComponent.qml"));
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));
582 QObject *object = component.create();
583 QVERIFY(object != 0);
585 QCOMPARE(object->property("absoluteUrl").toString(), QString("http://www.example.com/test.qml"));
586 QCOMPARE(object->property("relativeUrl").toString(), testFileUrl("createComponentData.qml").toString());
588 QTRY_VERIFY(object->property("asyncResult").toBool());
593 // simultaneous sync and async compilation
595 QQmlComponent component(&engine, testFileUrl("createComponent.2.qml"));
596 QObject *object = component.create();
597 QVERIFY(object != 0);
598 QTRY_VERIFY(object->property("success").toBool());
603 void tst_qqmlqt::createComponent_pragmaLibrary()
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));
614 void tst_qqmlqt::createQmlObject()
616 QQmlComponent component(&engine, testFileUrl("createQmlObject.qml"));
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));
631 QObject *object = component.create();
632 QVERIFY(object != 0);
634 QCOMPARE(object->property("emptyArg").toBool(), true);
635 QCOMPARE(object->property("success").toBool(), true);
637 QQuickItem *item = qobject_cast<QQuickItem *>(object);
639 QVERIFY(item->childItems().count() == 1);
645 void tst_qqmlqt::dateTimeConversion()
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);
663 QQmlComponent component(&eng, testFileUrl("dateTimeConversion.qml"));
664 QObject *obj = component.create();
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);
680 void tst_qqmlqt::dateTimeFormatting()
682 QFETCH(QString, method);
683 QFETCH(QStringList, inputProperties);
684 QFETCH(QStringList, expectedResults);
686 QDate date(2008,12,24);
687 QTime time(14,15,38,200);
688 QDateTime dateTime(date, time);
692 eng.rootContext()->setContextProperty("qdate", date);
693 eng.rootContext()->setContextProperty("qtime", time);
694 eng.rootContext()->setContextProperty("qdatetime", dateTime);
696 QQmlComponent component(&eng, testFileUrl("formatting.qml"));
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";
706 foreach (const QString &warning, warnings)
707 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
709 QObject *object = component.create();
710 QVERIFY2(component.errorString().isEmpty(), qPrintable(component.errorString()));
711 QVERIFY(object != 0);
713 QVERIFY(inputProperties.count() > 0);
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]);
728 void tst_qqmlqt::dateTimeFormatting_data()
730 QTest::addColumn<QString>("method");
731 QTest::addColumn<QStringList>("inputProperties");
732 QTest::addColumn<QStringList>("expectedResults");
734 QDate date(2008,12,24);
735 QTime time(14,15,38,200);
736 QDateTime dateTime(date, time);
738 QTest::newRow("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"));
745 QTest::newRow("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"));
753 QTest::newRow("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"));
761 void tst_qqmlqt::dateTimeFormattingVariants()
763 QFETCH(QString, method);
764 QFETCH(QVariant, variant);
765 QFETCH(QStringList, expectedResults);
768 eng.rootContext()->setContextProperty("qvariant", variant);
769 QQmlComponent component(&eng, testFileUrl("formatting.qml"));
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";
779 foreach (const QString &warning, warnings)
780 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
782 QObject *object = component.create();
783 QVERIFY2(component.errorString().isEmpty(), qPrintable(component.errorString()));
784 QVERIFY(object != 0);
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);
796 void tst_qqmlqt::dateTimeFormattingVariants_data()
798 QTest::addColumn<QString>("method");
799 QTest::addColumn<QVariant>("variant");
800 QTest::addColumn<QStringList>("expectedResults");
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"));
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"));
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"));
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"));
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"));
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"));
841 void tst_qqmlqt::isQtObject()
843 QQmlComponent component(&engine, testFileUrl("isQtObject.qml"));
844 QObject *object = component.create();
845 QVERIFY(object != 0);
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);
856 void tst_qqmlqt::btoa()
858 QQmlComponent component(&engine, testFileUrl("btoa.qml"));
860 QString warning1 = component.url().toString() + ":4: Error: Qt.btoa(): Invalid arguments";
861 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
863 QObject *object = component.create();
864 QVERIFY(object != 0);
866 QCOMPARE(object->property("test2").toString(), QString("SGVsbG8gd29ybGQh"));
871 void tst_qqmlqt::atob()
873 QQmlComponent component(&engine, testFileUrl("atob.qml"));
875 QString warning1 = component.url().toString() + ":4: Error: Qt.atob(): Invalid arguments";
876 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
878 QObject *object = component.create();
879 QVERIFY(object != 0);
881 QCOMPARE(object->property("test2").toString(), QString("Hello world!"));
886 void tst_qqmlqt::fontFamilies()
888 QQmlComponent component(&engine, testFileUrl("fontFamilies.qml"));
890 QString warning1 = component.url().toString() + ":4: Error: Qt.fontFamilies(): Invalid arguments";
891 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
893 QObject *object = component.create();
894 QVERIFY(object != 0);
896 QFontDatabase database;
897 QCOMPARE(object->property("test2"), QVariant::fromValue(database.families()));
902 void tst_qqmlqt::quit()
904 QQmlComponent component(&engine, testFileUrl("quit.qml"));
906 QSignalSpy spy(&engine, SIGNAL(quit()));
907 QObject *object = component.create();
908 QVERIFY(object != 0);
909 QCOMPARE(spy.count(), 1);
914 void tst_qqmlqt::resolvedUrl()
916 QQmlComponent component(&engine, testFileUrl("resolvedUrl.qml"));
918 QObject *object = component.create();
919 QVERIFY(object != 0);
921 QCOMPARE(object->property("result").toString(), component.url().toString());
922 QCOMPARE(object->property("isString").toBool(), true);
927 QTEST_MAIN(tst_qqmlqt)
929 #include "tst_qqmlqt.moc"