tst_qqmlecmascript.cpp 240 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
5 6 7 8 9
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
10 11 12 13 14 15
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 17
**
** In addition, as a special exception, Nokia gives you certain additional
18
** rights. These rights are described in the Nokia Qt LGPL Exception
19 20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
21 22 23 24 25 26 27 28 29 30 31
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
32 33 34 35 36
**
**
**
**
**
37
**
38 39 40
** $QT_END_LICENSE$
**
****************************************************************************/
41
#include <QtTest/QtTest>
42 43 44 45
#include <QtQml/qqmlcomponent.h>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlexpression.h>
#include <QtQml/qqmlcontext.h>
46 47
#include <QtCore/qfileinfo.h>
#include <QtCore/qdebug.h>
48
#include <QtQml/private/qqmlguard_p.h>
49 50
#include <QtCore/qdir.h>
#include <QtCore/qnumeric.h>
51 52
#include <private/qqmlengine_p.h>
#include <private/qqmlvmemetaobject_p.h>
53
#include <private/qv4compiler_p.h>
54 55
#include "testtypes.h"
#include "testhttpserver.h"
56
#include "../../shared/util.h"
57 58 59 60 61 62 63 64

/*
This test covers evaluation of ECMAScript expressions and bindings from within
QML.  This does not include static QML language issues.

Static QML language issues are covered in qmllanguage
*/

65
class tst_qqmlecmascript : public QQmlDataTest
66 67 68
{
    Q_OBJECT
public:
69
    tst_qqmlecmascript() {}
70 71 72 73

private slots:
    void initTestCase();
    void assignBasicTypes();
74 75
    void assignDate_data();
    void assignDate();
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    void idShortcutInvalidates();
    void boolPropertiesEvaluateAsBool();
    void methods();
    void signalAssignment();
    void bindingLoop();
    void basicExpressions();
    void basicExpressions_data();
    void arrayExpressions();
    void contextPropertiesTriggerReeval();
    void objectPropertiesTriggerReeval();
    void deferredProperties();
    void deferredPropertiesErrors();
    void extensionObjects();
    void overrideExtensionProperties();
    void attachedProperties();
    void enums();
    void valueTypeFunctions();
    void constantsOverrideBindings();
    void outerBindingOverridesInnerBinding();
    void aliasPropertyAndBinding();
96
    void aliasPropertyReset();
97 98
    void nonExistentAttachedObject();
    void scope();
99
    void importScope();
100 101
    void signalParameterTypes();
    void objectsCompareAsEqual();
102 103
    void componentCreation_data();
    void componentCreation();
104 105 106 107
    void dynamicCreation_data();
    void dynamicCreation();
    void dynamicDestruction();
    void objectToString();
108
    void objectHasOwnProperty();
109 110
    void selfDeletingBinding();
    void extendedObjectPropertyLookup();
111
    void extendedObjectPropertyLookup2();
112 113 114 115 116 117 118 119
    void scriptErrors();
    void functionErrors();
    void propertyAssignmentErrors();
    void signalTriggeredBindings();
    void listProperties();
    void exceptionClearsOnReeval();
    void exceptionSlotProducesWarning();
    void exceptionBindingProducesWarning();
120
    void compileInvalidBinding();
121 122 123 124 125 126
    void transientErrors();
    void shutdownErrors();
    void compositePropertyType();
    void jsObject();
    void undefinedResetsProperty();
    void listToVariant();
127
    void listAssignment();
128 129 130 131 132 133 134 135
    void multiEngineObject();
    void deletedObject();
    void attachedPropertyScope();
    void scriptConnect();
    void scriptDisconnect();
    void ownership();
    void cppOwnershipReturnValue();
    void ownershipCustomReturnValue();
136 137
    void ownershipRootObject();
    void ownershipConsistency();
138 139 140 141 142 143
    void qlistqobjectMethods();
    void strictlyEquals();
    void compiled();
    void numberAssignment();
    void propertySplicing();
    void signalWithUnknownTypes();
144 145 146 147
    void signalWithJSValueInVariant_data();
    void signalWithJSValueInVariant();
    void signalWithJSValueInVariant_twoEngines_data();
    void signalWithJSValueInVariant_twoEngines();
148 149
    void signalWithQJSValue_data();
    void signalWithQJSValue();
150
    void moduleApi_data();
151
    void moduleApi();
152
    void importScripts_data();
153 154
    void importScripts();
    void scarceResources();
155 156
    void scarceResources_data();
    void scarceResources_other();
157
    void propertyChangeSlots();
158 159 160 161 162 163 164 165 166 167 168
    void propertyVar_data();
    void propertyVar();
    void propertyVarCpp();
    void propertyVarOwnership();
    void propertyVarImplicitOwnership();
    void propertyVarReparent();
    void propertyVarReparentNullContext();
    void propertyVarCircular();
    void propertyVarCircular2();
    void propertyVarInheritance();
    void propertyVarInheritance2();
169
    void elementAssign();
170
    void objectPassThroughSignals();
171
    void objectConversion();
172
    void booleanConversion();
173
    void handleReferenceManagement();
Chris Adams's avatar
Chris Adams committed
174
    void stringArg();
175
    void readonlyDeclaration();
176 177 178
    void sequenceConversionRead();
    void sequenceConversionWrite();
    void sequenceConversionArray();
179
    void sequenceConversionIndexes();
180 181 182
    void sequenceConversionThreads();
    void sequenceConversionBindings();
    void sequenceConversionCopy();
183
    void assignSequenceTypes();
184
    void qtbug_22464();
185
    void qtbug_21580();
186

187 188 189
    void bug1();
    void bug2();
    void dynamicCreationCrash();
190
    void dynamicCreationOwnership();
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    void regExpBug();
    void nullObjectBinding();
    void deletedEngine();
    void libraryScriptAssert();
    void variantsAssignedUndefined();
    void qtbug_9792();
    void qtcreatorbug_1289();
    void noSpuriousWarningsAtShutdown();
    void canAssignNullToQObject();
    void functionAssignment_fromBinding();
    void functionAssignment_fromJS();
    void functionAssignment_fromJS_data();
    void functionAssignmentfromJS_invalid();
    void eval();
    void function();
206
    void functionException();
207 208 209
    void qtbug_10696();
    void qtbug_11606();
    void qtbug_11600();
Aaron Kennedy's avatar
Aaron Kennedy committed
210
    void qtbug_21864();
211
    void qobjectConnectionListExceptionHandling();
212 213 214
    void nonscriptable();
    void deleteLater();
    void in();
215
    void typeOf();
216
    void qtbug_24448();
217 218 219 220 221 222
    void sharedAttachedObject();
    void objectName();
    void writeRemovesBinding();
    void aliasBindingsAssignCorrectly();
    void aliasBindingsOverrideTarget();
    void aliasWritesOverrideBindings();
223
    void aliasToCompositeElement();
224
    void realToInt();
225
    void urlProperty();
226 227
    void urlPropertyWithEncoding();
    void urlListPropertyWithEncoding();
228
    void dynamicString();
229
    void include();
230
    void signalHandlers();
231
    void doubleEvaluate();
232
    void forInLoop();
233 234
    void nonNotifyable();
    void deleteWhileBindingRunning();
235 236 237
    void callQtInvokables();
    void invokableObjectArg();
    void invokableObjectRet();
238
    void qtbug_20344();
239
    void qtbug_22679();
240 241
    void qtbug_22843_data();
    void qtbug_22843();
242
    void rewriteMultiLineStrings();
243 244
    void revisionErrors();
    void revision();
245
    void invokableWithQObjectDerived();
246
    void realTypePrecision();
247

248
    void automaticSemicolon();
249
    void unaryExpression();
250
    void switchStatement();
251 252
    void withStatement();
    void tryStatement();
253

254
private:
255
    static void propertyVarWeakRefCallback(v8::Persistent<v8::Value> object, void* parameter);
256
    QQmlEngine engine;
257 258
};

259
void tst_qqmlecmascript::initTestCase()
260
{
261
    QQmlDataTest::initTestCase();
262
    registerTypes();
263 264 265

    QString dataDir(dataDirectory() + QLatin1Char('/') + QLatin1String("lib"));
    engine.addImportPath(dataDir);
266
}
267

268
void tst_qqmlecmascript::assignBasicTypes()
269 270
{
    {
271
    QQmlComponent component(&engine, testFileUrl("assignBasicTypes.qml"));
272 273 274 275 276 277 278 279 280 281 282 283 284
    MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
    QVERIFY(object != 0);
    QCOMPARE(object->flagProperty(), MyTypeObject::FlagVal1 | MyTypeObject::FlagVal3);
    QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2);
    QCOMPARE(object->stringProperty(), QString("Hello World!"));
    QCOMPARE(object->uintProperty(), uint(10));
    QCOMPARE(object->intProperty(), -19);
    QCOMPARE((float)object->realProperty(), float(23.2));
    QCOMPARE((float)object->doubleProperty(), float(-19.75));
    QCOMPARE((float)object->floatProperty(), float(8.5));
    QCOMPARE(object->colorProperty(), QColor("red"));
    QCOMPARE(object->dateProperty(), QDate(1982, 11, 25));
    QCOMPARE(object->timeProperty(), QTime(11, 11, 32));
285
    QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1), Qt::UTC));
286 287 288 289 290 291 292 293 294 295 296 297 298
    QCOMPARE(object->pointProperty(), QPoint(99,13));
    QCOMPARE(object->pointFProperty(), QPointF(-10.1, 12.3));
    QCOMPARE(object->sizeProperty(), QSize(99, 13));
    QCOMPARE(object->sizeFProperty(), QSizeF(0.1, 0.2));
    QCOMPARE(object->rectProperty(), QRect(9, 7, 100, 200));
    QCOMPARE(object->rectFProperty(), QRectF(1000.1, -10.9, 400, 90.99));
    QCOMPARE(object->boolProperty(), true);
    QCOMPARE(object->variantProperty(), QVariant("Hello World!"));
    QCOMPARE(object->vectorProperty(), QVector3D(10, 1, 2.2));
    QCOMPARE(object->urlProperty(), component.url().resolved(QUrl("main.qml")));
    delete object;
    }
    {
299
    QQmlComponent component(&engine, testFileUrl("assignBasicTypes.2.qml"));
300 301 302 303 304 305 306 307 308 309 310 311 312
    MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
    QVERIFY(object != 0);
    QCOMPARE(object->flagProperty(), MyTypeObject::FlagVal1 | MyTypeObject::FlagVal3);
    QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2);
    QCOMPARE(object->stringProperty(), QString("Hello World!"));
    QCOMPARE(object->uintProperty(), uint(10));
    QCOMPARE(object->intProperty(), -19);
    QCOMPARE((float)object->realProperty(), float(23.2));
    QCOMPARE((float)object->doubleProperty(), float(-19.75));
    QCOMPARE((float)object->floatProperty(), float(8.5));
    QCOMPARE(object->colorProperty(), QColor("red"));
    QCOMPARE(object->dateProperty(), QDate(1982, 11, 25));
    QCOMPARE(object->timeProperty(), QTime(11, 11, 32));
313
    QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1), Qt::UTC));
314 315 316 317 318 319 320 321 322 323 324 325 326 327
    QCOMPARE(object->pointProperty(), QPoint(99,13));
    QCOMPARE(object->pointFProperty(), QPointF(-10.1, 12.3));
    QCOMPARE(object->sizeProperty(), QSize(99, 13));
    QCOMPARE(object->sizeFProperty(), QSizeF(0.1, 0.2));
    QCOMPARE(object->rectProperty(), QRect(9, 7, 100, 200));
    QCOMPARE(object->rectFProperty(), QRectF(1000.1, -10.9, 400, 90.99));
    QCOMPARE(object->boolProperty(), true);
    QCOMPARE(object->variantProperty(), QVariant("Hello World!"));
    QCOMPARE(object->vectorProperty(), QVector3D(10, 1, 2.2));
    QCOMPARE(object->urlProperty(), component.url().resolved(QUrl("main.qml")));
    delete object;
    }
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
void tst_qqmlecmascript::assignDate_data()
{
    QTest::addColumn<QUrl>("source");
    QTest::newRow("Component.onComplete JS") << testFileUrl("assignDate.qml");
    QTest::newRow("Binding JS") << testFileUrl("assignDate.2.qml");
    QTest::newRow("Binding UTC") << testFileUrl("assignDate.3.qml");
    QTest::newRow("Binding JS UTC") << testFileUrl("assignDate.4.qml");
    QTest::newRow("Binding UTC+2") << testFileUrl("assignDate.5.qml");
    QTest::newRow("Binding JS UTC+2 ") << testFileUrl("assignDate.6.qml");
}

void tst_qqmlecmascript::assignDate()
{
    QFETCH(QUrl, source);
    QQmlComponent component(&engine, source);
    QScopedPointer<QObject> obj(component.create());
    MyTypeObject *object = qobject_cast<MyTypeObject *>(obj.data());
    QVERIFY(object != 0);
    QCOMPARE(object->dateProperty(), QDate(1982, 11, 25));
    QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1), Qt::UTC));
}

350
void tst_qqmlecmascript::idShortcutInvalidates()
351 352
{
    {
353
        QQmlComponent component(&engine, testFileUrl("idShortcutInvalidates.qml"));
354 355 356 357 358
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QVERIFY(object->objectProperty() != 0);
        delete object->objectProperty();
        QVERIFY(object->objectProperty() == 0);
359
        delete object;
360 361 362
    }

    {
363
        QQmlComponent component(&engine, testFileUrl("idShortcutInvalidates.1.qml"));
364 365 366 367 368
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QVERIFY(object->objectProperty() != 0);
        delete object->objectProperty();
        QVERIFY(object->objectProperty() == 0);
369
        delete object;
370 371 372
    }
}

373
void tst_qqmlecmascript::boolPropertiesEvaluateAsBool()
374 375
{
    {
376
        QQmlComponent component(&engine, testFileUrl("boolPropertiesEvaluateAsBool.1.qml"));
377 378 379
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->stringProperty(), QLatin1String("pass"));
380
        delete object;
381 382
    }
    {
383
        QQmlComponent component(&engine, testFileUrl("boolPropertiesEvaluateAsBool.2.qml"));
384 385 386
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->stringProperty(), QLatin1String("pass"));
387
        delete object;
388 389 390
    }
}

391
void tst_qqmlecmascript::signalAssignment()
392 393
{
    {
394
        QQmlComponent component(&engine, testFileUrl("signalAssignment.1.qml"));
395 396 397 398 399
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->string(), QString());
        emit object->basicSignal();
        QCOMPARE(object->string(), QString("pass"));
400
        delete object;
401 402 403
    }

    {
404
        QQmlComponent component(&engine, testFileUrl("signalAssignment.2.qml"));
405 406 407 408 409
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->string(), QString());
        emit object->argumentSignal(19, "Hello world!", 10.25, MyQmlObject::EnumValue4, Qt::RightButton);
        QCOMPARE(object->string(), QString("pass 19 Hello world! 10.25 3 2"));
410
        delete object;
411
    }
412 413

    {
414
        QQmlComponent component(&engine, testFileUrl("signalAssignment.3.qml"));
415 416 417 418 419 420 421 422 423 424
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->string(), QString());
        emit object->unnamedArgumentSignal(19, 10.25, "Hello world!");
        QEXPECT_FAIL("", "QTBUG-24481", Continue);
        QCOMPARE(object->string(), QString("pass 19 Hello world!"));
        delete object;
    }

    {
425
        QQmlComponent component(&engine, testFileUrl("signalAssignment.4.qml"));
426 427 428 429 430 431 432
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->string(), QString());
        emit object->signalWithGlobalName(19);
        QCOMPARE(object->string(), QString("pass 5"));
        delete object;
    }
433 434
}

435
void tst_qqmlecmascript::methods()
436 437
{
    {
438
        QQmlComponent component(&engine, testFileUrl("methods.1.qml"));
439 440 441 442 443 444 445
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->methodCalled(), false);
        QCOMPARE(object->methodIntCalled(), false);
        emit object->basicSignal();
        QCOMPARE(object->methodCalled(), true);
        QCOMPARE(object->methodIntCalled(), false);
446
        delete object;
447 448 449
    }

    {
450
        QQmlComponent component(&engine, testFileUrl("methods.2.qml"));
451 452 453 454 455 456 457
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);
        QCOMPARE(object->methodCalled(), false);
        QCOMPARE(object->methodIntCalled(), false);
        emit object->basicSignal();
        QCOMPARE(object->methodCalled(), false);
        QCOMPARE(object->methodIntCalled(), true);
458
        delete object;
459 460 461
    }

    {
462
        QQmlComponent component(&engine, testFileUrl("methods.3.qml"));
463 464 465
        QObject *object = component.create();
        QVERIFY(object != 0);
        QCOMPARE(object->property("test").toInt(), 19);
466
        delete object;
467 468 469
    }

    {
470
        QQmlComponent component(&engine, testFileUrl("methods.4.qml"));
471 472 473 474 475
        QObject *object = component.create();
        QVERIFY(object != 0);
        QCOMPARE(object->property("test").toInt(), 19);
        QCOMPARE(object->property("test2").toInt(), 17);
        QCOMPARE(object->property("test3").toInt(), 16);
476
        delete object;
477 478 479
    }

    {
480
        QQmlComponent component(&engine, testFileUrl("methods.5.qml"));
481 482 483
        QObject *object = component.create();
        QVERIFY(object != 0);
        QCOMPARE(object->property("test").toInt(), 9);
484
        delete object;
485 486 487
    }
}

488
void tst_qqmlecmascript::bindingLoop()
489
{
490
    QQmlComponent component(&engine, testFileUrl("bindingLoop.qml"));
491
    QString warning = component.url().toString() + ":5:9: QML MyQmlObject: Binding loop detected for property \"stringProperty\"";
492 493 494
    QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData());
    QObject *object = component.create();
    QVERIFY(object != 0);
495
    delete object;
496 497
}

498
void tst_qqmlecmascript::basicExpressions_data()
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
{
    QTest::addColumn<QString>("expression");
    QTest::addColumn<QVariant>("result");
    QTest::addColumn<bool>("nest");

    QTest::newRow("Syntax error (self test)") << "{console.log({'a':1'}.a)}" << QVariant() << false;
    QTest::newRow("Context property") << "a" << QVariant(1944) << false;
    QTest::newRow("Context property") << "a" << QVariant(1944) << true;
    QTest::newRow("Context property expression") << "a * 2" << QVariant(3888) << false;
    QTest::newRow("Context property expression") << "a * 2" << QVariant(3888) << true;
    QTest::newRow("Overridden context property") << "b" << QVariant("Milk") << false;
    QTest::newRow("Overridden context property") << "b" << QVariant("Cow") << true;
    QTest::newRow("Object property") << "object.stringProperty" << QVariant("Object1") << false;
    QTest::newRow("Object property") << "object.stringProperty" << QVariant("Object1") << true;
    QTest::newRow("Overridden object property") << "objectOverride.stringProperty" << QVariant("Object2") << false;
    QTest::newRow("Overridden object property") << "objectOverride.stringProperty" << QVariant("Object3") << true;
    QTest::newRow("Default object property") << "horseLegs" << QVariant(4) << false;
    QTest::newRow("Default object property") << "antLegs" << QVariant(6) << false;
    QTest::newRow("Default object property") << "emuLegs" << QVariant(2) << false;
    QTest::newRow("Nested default object property") << "horseLegs" << QVariant(4) << true;
    QTest::newRow("Nested default object property") << "antLegs" << QVariant(7) << true;
    QTest::newRow("Nested default object property") << "emuLegs" << QVariant(2) << true;
    QTest::newRow("Nested default object property") << "humanLegs" << QVariant(2) << true;
    QTest::newRow("Context property override default object property") << "millipedeLegs" << QVariant(100) << true;
}

525
void tst_qqmlecmascript::basicExpressions()
526 527 528 529 530 531 532 533 534 535 536 537 538 539
{
    QFETCH(QString, expression);
    QFETCH(QVariant, result);
    QFETCH(bool, nest);

    MyQmlObject object1;
    MyQmlObject object2;
    MyQmlObject object3;
    MyDefaultObject1 default1;
    MyDefaultObject3 default3;
    object1.setStringProperty("Object1");
    object2.setStringProperty("Object2");
    object3.setStringProperty("Object3");

540 541
    QQmlContext context(engine.rootContext());
    QQmlContext nestedContext(&context);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

    context.setContextObject(&default1);
    context.setContextProperty("a", QVariant(1944));
    context.setContextProperty("b", QVariant("Milk"));
    context.setContextProperty("object", &object1);
    context.setContextProperty("objectOverride", &object2);
    nestedContext.setContextObject(&default3);
    nestedContext.setContextProperty("b", QVariant("Cow"));
    nestedContext.setContextProperty("objectOverride", &object3);
    nestedContext.setContextProperty("millipedeLegs", QVariant(100));

    MyExpression expr(nest?&nestedContext:&context, expression);
    QCOMPARE(expr.evaluate(), result);
}

557
void tst_qqmlecmascript::arrayExpressions()
558 559 560 561 562
{
    QObject obj1;
    QObject obj2;
    QObject obj3;

563
    QQmlContext context(engine.rootContext());
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    context.setContextProperty("a", &obj1);
    context.setContextProperty("b", &obj2);
    context.setContextProperty("c", &obj3);

    MyExpression expr(&context, "[a, b, c, 10]");
    QVariant result = expr.evaluate();
    QCOMPARE(result.userType(), qMetaTypeId<QList<QObject *> >());
    QList<QObject *> list = qvariant_cast<QList<QObject *> >(result);
    QCOMPARE(list.count(), 4);
    QCOMPARE(list.at(0), &obj1);
    QCOMPARE(list.at(1), &obj2);
    QCOMPARE(list.at(2), &obj3);
    QCOMPARE(list.at(3), (QObject *)0);
}

// Tests that modifying a context property will reevaluate expressions
580
void tst_qqmlecmascript::contextPropertiesTriggerReeval()
581
{
582
    QQmlContext context(engine.rootContext());
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    MyQmlObject object1;
    MyQmlObject object2;
    MyQmlObject *object3 = new MyQmlObject;

    object1.setStringProperty("Hello");
    object2.setStringProperty("World");

    context.setContextProperty("testProp", QVariant(1));
    context.setContextProperty("testObj", &object1);
    context.setContextProperty("testObj2", object3);

    { 
        MyExpression expr(&context, "testProp + 1");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant(2));

        context.setContextProperty("testProp", QVariant(2));
        QCOMPARE(expr.changed, true);
        QCOMPARE(expr.evaluate(), QVariant(3));
    }

    { 
        MyExpression expr(&context, "testProp + testProp + testProp");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant(6));

        context.setContextProperty("testProp", QVariant(4));
        QCOMPARE(expr.changed, true);
        QCOMPARE(expr.evaluate(), QVariant(12));
    }

    { 
        MyExpression expr(&context, "testObj.stringProperty");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant("Hello"));

        context.setContextProperty("testObj", &object2);
        QCOMPARE(expr.changed, true);
        QCOMPARE(expr.evaluate(), QVariant("World"));
    }

    { 
        MyExpression expr(&context, "testObj.stringProperty /**/");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant("World"));

        context.setContextProperty("testObj", &object1);
        QCOMPARE(expr.changed, true);
        QCOMPARE(expr.evaluate(), QVariant("Hello"));
    }

    { 
        MyExpression expr(&context, "testObj2");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant::fromValue((QObject *)object3));
    }

640
    delete object3;
641 642
}

643
void tst_qqmlecmascript::objectPropertiesTriggerReeval()
644
{
645
    QQmlContext context(engine.rootContext());
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    MyQmlObject object1;
    MyQmlObject object2;
    MyQmlObject object3;
    context.setContextProperty("testObj", &object1);

    object1.setStringProperty(QLatin1String("Hello"));
    object2.setStringProperty(QLatin1String("Dog"));
    object3.setStringProperty(QLatin1String("Cat"));

    { 
        MyExpression expr(&context, "testObj.stringProperty");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant("Hello"));

        object1.setStringProperty(QLatin1String("World"));
        QCOMPARE(expr.changed, true);
        QCOMPARE(expr.evaluate(), QVariant("World"));
    }

    { 
        MyExpression expr(&context, "testObj.objectProperty.stringProperty");
        QCOMPARE(expr.changed, false);
        QCOMPARE(expr.evaluate(), QVariant());

        object1.setObjectProperty(&object2);
        QCOMPARE(expr.changed, true);
        expr.changed = false;
        QCOMPARE(expr.evaluate(), QVariant("Dog"));

        object1.setObjectProperty(&object3);
        QCOMPARE(expr.changed, true);
        expr.changed = false;
        QCOMPARE(expr.evaluate(), QVariant("Cat"));

        object1.setObjectProperty(0);
        QCOMPARE(expr.changed, true);
        expr.changed = false;
        QCOMPARE(expr.evaluate(), QVariant());

        object1.setObjectProperty(&object3);
        QCOMPARE(expr.changed, true);
        expr.changed = false;
        QCOMPARE(expr.evaluate(), QVariant("Cat"));

        object3.setStringProperty("Donkey");
        QCOMPARE(expr.changed, true);
        expr.changed = false;
        QCOMPARE(expr.evaluate(), QVariant("Donkey"));
    }
}

697
void tst_qqmlecmascript::deferredProperties()
698
{
699
    QQmlComponent component(&engine, testFileUrl("deferredProperties.qml"));
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    MyDeferredObject *object = 
        qobject_cast<MyDeferredObject *>(component.create());
    QVERIFY(object != 0);
    QCOMPARE(object->value(), 0);
    QVERIFY(object->objectProperty() == 0);
    QVERIFY(object->objectProperty2() != 0);
    qmlExecuteDeferred(object);
    QCOMPARE(object->value(), 10);
    QVERIFY(object->objectProperty() != 0);
    MyQmlObject *qmlObject = 
        qobject_cast<MyQmlObject *>(object->objectProperty());
    QVERIFY(qmlObject != 0);
    QCOMPARE(qmlObject->value(), 10);
    object->setValue(19);
    QCOMPARE(qmlObject->value(), 19);
715 716

    delete object;
717 718 719
}

// Check errors on deferred properties are correctly emitted
720
void tst_qqmlecmascript::deferredPropertiesErrors()
721
{
722
    QQmlComponent component(&engine, testFileUrl("deferredPropertiesErrors.qml"));
723 724 725 726 727 728 729
    MyDeferredObject *object = 
        qobject_cast<MyDeferredObject *>(component.create());
    QVERIFY(object != 0);
    QCOMPARE(object->value(), 0);
    QVERIFY(object->objectProperty() == 0);
    QVERIFY(object->objectProperty2() == 0);

730
    QString warning = component.url().toString() + ":6: Unable to assign [undefined] to QObject*";
731 732 733 734 735 736 737
    QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));

    qmlExecuteDeferred(object);

    delete object;
}

738
void tst_qqmlecmascript::extensionObjects()
739
{
740
    QQmlComponent component(&engine, testFileUrl("extensionObjects.qml"));
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
    MyExtendedObject *object = 
        qobject_cast<MyExtendedObject *>(component.create());
    QVERIFY(object != 0);
    QCOMPARE(object->baseProperty(), 13);
    QCOMPARE(object->coreProperty(), 9);
    object->setProperty("extendedProperty", QVariant(11));
    object->setProperty("baseExtendedProperty", QVariant(92));
    QCOMPARE(object->coreProperty(), 11);
    QCOMPARE(object->baseProperty(), 92);

    MyExtendedObject *nested = qobject_cast<MyExtendedObject*>(qvariant_cast<QObject *>(object->property("nested")));
    QVERIFY(nested);
    QCOMPARE(nested->baseProperty(), 13);
    QCOMPARE(nested->coreProperty(), 9);
    nested->setProperty("extendedProperty", QVariant(11));
    nested->setProperty("baseExtendedProperty", QVariant(92));
    QCOMPARE(nested->coreProperty(), 11);
    QCOMPARE(nested->baseProperty(), 92);

760
    delete object;
761 762
}

763
void tst_qqmlecmascript::overrideExtensionProperties()
764
{
765
    QQmlComponent component(&engine, testFileUrl("extensionObjectsPropertyOverride.qml"));
766 767 768 769 770
    OverrideDefaultPropertyObject *object =
        qobject_cast<OverrideDefaultPropertyObject *>(component.create());
    QVERIFY(object != 0);
    QVERIFY(object->secondProperty() != 0);
    QVERIFY(object->firstProperty() == 0);
771 772

    delete object;
773 774
}

775
void tst_qqmlecmascript::attachedProperties()
776 777
{
    {
778
        QQmlComponent component(&engine, testFileUrl("attachedProperty.qml"));
779 780 781 782 783 784
        QObject *object = component.create();
        QVERIFY(object != 0);
        QCOMPARE(object->property("a").toInt(), 19);
        QCOMPARE(object->property("b").toInt(), 19);
        QCOMPARE(object->property("c").toInt(), 19);
        QCOMPARE(object->property("d").toInt(), 19);
785
        delete object;
786 787 788
    }

    {
789
        QQmlComponent component(&engine, testFileUrl("attachedProperty.2.qml"));
790 791 792 793 794 795
        QObject *object = component.create();
        QVERIFY(object != 0);
        QCOMPARE(object->property("a").toInt(), 26);
        QCOMPARE(object->property("b").toInt(), 26);
        QCOMPARE(object->property("c").toInt(), 26);
        QCOMPARE(object->property("d").toInt(), 26);
796 797

        delete object;
798 799 800
    }

    {
801
        QQmlComponent component(&engine, testFileUrl("writeAttachedProperty.qml"));
802 803 804 805 806 807 808 809 810 811
        QObject *object = component.create();
        QVERIFY(object != 0);

        QMetaObject::invokeMethod(object, "writeValue2");

        MyQmlAttachedObject *attached =
            qobject_cast<MyQmlAttachedObject *>(qmlAttachedPropertiesObject<MyQmlObject>(object));
        QVERIFY(attached != 0);

        QCOMPARE(attached->value2(), 9);
812
        delete object;
813 814 815
    }
}

816
void tst_qqmlecmascript::enums()
817 818 819
{
    // Existent enums
    {
820
    QQmlComponent component(&engine, testFileUrl("enums.1.qml"));
821 822 823 824 825 826 827 828 829 830 831 832 833
    QObject *object = component.create();
    QVERIFY(object != 0);

    QCOMPARE(object->property("a").toInt(), 0);
    QCOMPARE(object->property("b").toInt(), 1);
    QCOMPARE(object->property("c").toInt(), 2);
    QCOMPARE(object->property("d").toInt(), 3);
    QCOMPARE(object->property("e").toInt(), 0);
    QCOMPARE(object->property("f").toInt(), 1);
    QCOMPARE(object->property("g").toInt(), 2);
    QCOMPARE(object->property("h").toInt(), 3);
    QCOMPARE(object->property("i").toInt(), 19);
    QCOMPARE(object->property("j").toInt(), 19);
834 835

    delete object;
836 837 838
    }
    // Non-existent enums
    {
839
    QQmlComponent component(&engine, testFileUrl("enums.2.qml"));
840

841 842
    QString warning1 = component.url().toString() + ":5: Unable to assign [undefined] to int";
    QString warning2 = component.url().toString() + ":6: Unable to assign [undefined] to int";
843 844 845 846 847 848 849
    QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
    QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));

    QObject *object = component.create();
    QVERIFY(object != 0);
    QCOMPARE(object->property("a").toInt(), 0);
    QCOMPARE(object->property("b").toInt(), 0);
850

851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    delete object;
    }
    // Enums as literals
    {
    QQmlComponent component(&engine, testFileUrl("enums.3.qml"));
    QObject *object = component.create();
    QVERIFY(object != 0);

    // check the values are what we expect
    QCOMPARE(object->property("a").toInt(), 4);
    QCOMPARE(object->property("b").toInt(), 5);
    QCOMPARE(object->property("c").toInt(), 9);
    QCOMPARE(object->property("d").toInt(), 13);
    QCOMPARE(object->property("e").toInt(), 2);
    QCOMPARE(object->property("f").toInt(), 3);
    QCOMPARE(object->property("h").toInt(), 2);
    QCOMPARE(object->property("i").toInt(), 3);

    // count of change signals
    QCOMPARE(object->property("ac").toInt(), 0);
    QCOMPARE(object->property("bc").toInt(), 0);
    QCOMPARE(object->property("cc").toInt(), 0);
    QCOMPARE(object->property("dc").toInt(), 0);
    QCOMPARE(object->property("ec").toInt(), 0);
    QCOMPARE(object->property("fc").toInt(), 0);
    QCOMPARE(object->property("hc").toInt(), 1); // namespace -> binding
    QCOMPARE(object->property("ic").toInt(), 1); // namespace -> binding

879
    delete object;
880 881 882
    }
}

883
void tst_qqmlecmascript::valueTypeFunctions()
884
{
885
    QQmlComponent component(&engine, testFileUrl("valueTypeFunctions.qml"));
886 887 888 889
    MyTypeObject *obj = qobject_cast<MyTypeObject*>(component.create());
    QVERIFY(obj != 0);
    QCOMPARE(obj->rectProperty(), QRect(0,0,100,100));
    QCOMPARE(obj->rectFProperty(), QRectF(0,0.5,100,99.5));
890 891

    delete obj;
892 893 894 895 896 897
}

/* 
Tests that writing a constant to a property with a binding on it disables the
binding.
*/
898
void tst_qqmlecmascript::constantsOverrideBindings()
899 900 901
{
    // From ECMAScript
    {
902
        QQmlComponent component(&engine, testFileUrl("constantsOverrideBindings.1.qml"));
903 904 905 906 907 908 909 910 911 912 913 914
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);

        QCOMPARE(object->property("c2").toInt(), 0);
        object->setProperty("c1", QVariant(9));
        QCOMPARE(object->property("c2").toInt(), 9);

        emit object->basicSignal();

        QCOMPARE(object->property("c2").toInt(), 13);
        object->setProperty("c1", QVariant(8));
        QCOMPARE(object->property("c2").toInt(), 13);
915 916

        delete object;
917 918 919 920
    }

    // During construction
    {
921
        QQmlComponent component(&engine, testFileUrl("constantsOverrideBindings.2.qml"));
922 923 924 925 926 927 928 929
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);

        QCOMPARE(object->property("c1").toInt(), 0);
        QCOMPARE(object->property("c2").toInt(), 10);
        object->setProperty("c1", QVariant(9));
        QCOMPARE(object->property("c1").toInt(), 9);
        QCOMPARE(object->property("c2").toInt(), 10);
930 931

        delete object;
932 933 934 935 936
    }

#if 0
    // From C++
    {
937
        QQmlComponent component(&engine, testFileUrl("constantsOverrideBindings.3.qml"));
938 939 940 941 942 943 944 945 946 947 948 949
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);

        QCOMPARE(object->property("c2").toInt(), 0);
        object->setProperty("c1", QVariant(9));
        QCOMPARE(object->property("c2").toInt(), 9);

        object->setProperty("c2", QVariant(13));
        QCOMPARE(object->property("c2").toInt(), 13);
        object->setProperty("c1", QVariant(7));
        QCOMPARE(object->property("c1").toInt(), 7);
        QCOMPARE(object->property("c2").toInt(), 13);
950 951

        delete object;
952 953 954 955 956
    }
#endif

    // Using an alias
    {
957
        QQmlComponent component(&engine, testFileUrl("constantsOverrideBindings.4.qml"));
958 959 960 961 962 963 964 965
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);

        QCOMPARE(object->property("c1").toInt(), 0);
        QCOMPARE(object->property("c3").toInt(), 10);
        object->setProperty("c1", QVariant(9));
        QCOMPARE(object->property("c1").toInt(), 9);
        QCOMPARE(object->property("c3").toInt(), 10);
966 967

        delete object;
968 969 970 971 972 973 974
    }
}

/*
Tests that assigning a binding to a property that already has a binding causes
the original binding to be disabled.
*/
975
void tst_qqmlecmascript::outerBindingOverridesInnerBinding()
976
{
977
    QQmlComponent component(&engine, 
978
                           testFileUrl("outerBindingOverridesInnerBinding.qml"));
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
    QVERIFY(object != 0);

    QCOMPARE(object->property("c1").toInt(), 0);
    QCOMPARE(object->property("c2").toInt(), 0);
    QCOMPARE(object->property("c3").toInt(), 0);

    object->setProperty("c1", QVariant(9));
    QCOMPARE(object->property("c1").toInt(), 9);
    QCOMPARE(object->property("c2").toInt(), 0);
    QCOMPARE(object->property("c3").toInt(), 0);

    object->setProperty("c3", QVariant(8));
    QCOMPARE(object->property("c1").toInt(), 9);
    QCOMPARE(object->property("c2").toInt(), 8);
    QCOMPARE(object->property("c3").toInt(), 8);
995 996

    delete object;
997 998 999 1000 1001 1002 1003
}

/*
Access a non-existent attached object.  

Tests for a regression where this used to crash.
*/
1004
void tst_qqmlecmascript::nonExistentAttachedObject()
1005
{
1006
    QQmlComponent component(&engine, testFileUrl("nonExistentAttachedObject.qml"));
1007

1008
    QString warning = component.url().toString() + ":4: Unable to assign [undefined] to QString";
1009 1010 1011 1012
    QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));

    QObject *object = component.create();
    QVERIFY(object != 0);
1013 1014

    delete object;
1015 1016
}

1017
void tst_qqmlecmascript::scope()
1018 1019
{
    {
1020
        QQmlComponent component(&engine, testFileUrl("scope.qml"));
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        QObject *object = component.create();
        QVERIFY(object != 0);

        QCOMPARE(object->property("test1").toInt(), 1);
        QCOMPARE(object->property("test2").toInt(), 2);
        QCOMPARE(object->property("test3").toString(), QString("1Test"));
        QCOMPARE(object->property("test4").toString(), QString("2Test"));
        QCOMPARE(object->property("test5").toInt(), 1);
        QCOMPARE(object->property("test6").toInt(), 1);
        QCOMPARE(object->property("test7").toInt(), 2);
        QCOMPARE(object->property("test8").toInt(), 2);
        QCOMPARE(object->property("test9").toInt(), 1);
        QCOMPARE(object->property("test10").toInt(), 3);
1034 1035

        delete object;
1036 1037 1038
    }

    {
1039
        QQmlComponent component(&engine, testFileUrl("scope.2.qml"));
1040 1041 1042 1043 1044 1045 1046 1047 1048
        QObject *object = component.create();
        QVERIFY(object != 0);

        QCOMPARE(object->property("test1").toInt(), 19);
        QCOMPARE(object->property("test2").toInt(), 19);
        QCOMPARE(object->property("test3").toInt(), 14);
        QCOMPARE(object->property("test4").toInt(), 14);
        QCOMPARE(object->property("test5").toInt(), 24);
        QCOMPARE(object->property("test6").toInt(), 24);
1049 1050

        delete object;
1051 1052 1053
    }

    {
1054
        QQmlComponent component(&engine, testFileUrl("scope.3.qml"));
1055 1056 1057 1058 1059 1060
        QObject *object = component.create();
        QVERIFY(object != 0);

        QCOMPARE(object->property("test1").toBool(), true);
        QCOMPARE(object->property("test2").toBool(), true);
        QCOMPARE(object->property("test3").toBool(), true);
1061 1062

        delete object;
1063 1064 1065 1066
    }

    // Signal argument scope
    {
1067
        QQmlComponent component(&engine, testFileUrl("scope.4.qml"));
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
        MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
        QVERIFY(object != 0);

        QCOMPARE(object->property("test").toInt(), 0);
        QCOMPARE(object->property("test2").toString(), QString());

        emit object->argumentSignal(13, "Argument Scope", 9, MyQmlObject::EnumValue4, Qt::RightButton);

        QCOMPARE(object->property("test").toInt(), 13);
        QCOMPARE(object->property("test2").toString(), QString("Argument Scope"));

        delete object;
    }
1081 1082

    {
1083
        QQmlComponent component(&engine, testFileUrl("scope.5.qml"));
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        QObject *object = component.create();
        QVERIFY(object != 0);

        QCOMPARE(object->property("test1").toBool(), true);
        QCOMPARE(object->property("test2").toBool(), true);

        delete object;
    }

    {
1094
        QQmlComponent component(&engine, testFileUrl("scope.6.qml"));
1095 1096 1097 1098 1099 1100 1101
        QObject *object = component.create();
        QVERIFY(object != 0);

        QCOMPARE(object->property("test").toBool(), true);

        delete object;
    }
1102 1103
}

1104 1105
// In 4.7, non-library javascript files that had no imports shared the imports of their
// importing context
1106
void tst_qqmlecmascript::importScope()
1107
{
1108
    QQmlComponent component(&engine, testFileUrl("importScope.qml"));
1109 1110 1111 1112 1113 1114 1115 1116
    QObject *o = component.create();
    QVERIFY(o != 0);

    QCOMPARE(o->property("test").toInt(), 240);

    delete o;
}

1117 1118
/*
Tests that "any" type passes through a synthesized signal parameter.  This
1119
is essentially a test of QQmlMetaType::copy()
1120
*/
1121
void tst_qqmlecmascript::signalParameterTypes()
1122
{
1123
    QQmlComponent component(&engine, testFileUrl("signalParameterTypes.qml"));
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
    QVERIFY(object != 0);

    emit object->basicSignal();

    QCOMPARE(object->property("intProperty").toInt(), 10);
    QCOMPARE(object->property("realProperty").toReal(), 19.2);
    QVERIFY(object->property("colorProperty").value<QColor>() == QColor(255, 255, 0, 255));
    QVERIFY(object->property("variantProperty") == QVariant::fromValue(QColor(255, 0, 255, 255)));
    QVERIFY(object->property("enumProperty") == MyQmlObject::EnumValue3);
    QVERIFY(object->property("qtEnumProperty") == Qt::LeftButton);
1135 1136

    delete object;
1137 1138 1139 1140 1141
}

/*
Test that two JS objects for the same QObject compare as equal.
*/
1142
void tst_qqmlecmascript::objectsCompareAsEqual()
1143
{
1144
    QQmlComponent component(&engine, testFileUrl("objectsCompareAsEqual.qml"));
1145 1146 1147 1148 1149 1150 1151 1152
    QObject *object = component.create();
    QVERIFY(object != 0);

    QCOMPARE(object->property("test1").toBool(), true);
    QCOMPARE(object->property("test2").toBool(), true);
    QCOMPARE(object->property("test3").toBool(), true);
    QCOMPARE(object->property("test4").toBool(), true);
    QCOMPARE(object->property("test5").toBool(), true);
1153 1154

    delete object;
1155 1156 1157 1158 1159 1160 1161
}

/*
Confirm bindings and alias properties can coexist.

Tests for a regression where the binding would not reevaluate.
*/
1162
void tst_qqmlecmascript::aliasPropertyAndBinding()
1163
{
1164
    QQmlComponent component(&engine, testFileUrl("aliasPropertyAndBinding.qml"));
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    QObject *object = component.create();
    QVERIFY(object != 0);

    QCOMPARE(object->property("c2").toInt(), 3);
    QCOMPARE(object->property("c3").toInt(), 3);

    object->setProperty("c2", QVariant(19));

    QCOMPARE(object->property("c2").toInt(), 19);
    QCOMPARE(object->property("c3").toInt(), 19);
1175 1176

    delete object;
1177 1178
}

1179 1180 1181 1182
/*
Ensure that we can write undefined value to an alias property,
and that the aliased property is reset correctly if possible.
*/
1183
void tst_qqmlecmascript::aliasPropertyReset()
1184 1185 1186 1187
{
    QObject *object = 0;

    // test that a manual write (of undefined) to a resettable aliased property succeeds
1188
    QQmlComponent c1(&engine, testFileUrl("aliasreset/aliasPropertyReset.1.qml"));
1189 1190
    object = c1.create();
    QVERIFY(object != 0);
1191
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() != 0);
1192 1193
    QCOMPARE(object->property("aliasIsUndefined"), QVariant(false));
    QMetaObject::invokeMethod(object, "resetAliased");
1194
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0);
1195 1196 1197 1198
    QCOMPARE(object->property("aliasIsUndefined"), QVariant(true));
    delete object;

    // test that a manual write (of undefined) to a resettable alias property succeeds
1199
    QQmlComponent c2(&engine, testFileUrl("aliasreset/aliasPropertyReset.2.qml"));
1200 1201
    object = c2.create();
    QVERIFY(object != 0);
1202
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() != 0);
1203 1204
    QCOMPARE(object->property("loaderSourceComponentIsUndefined"), QVariant(false));
    QMetaObject::invokeMethod(object, "resetAlias");
1205
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0);
1206 1207 1208 1209
    QCOMPARE(object->property("loaderSourceComponentIsUndefined"), QVariant(true));
    delete object;

    // test that an alias to a bound property works correctly
1210
    QQmlComponent c3(&engine, testFileUrl("aliasreset/aliasPropertyReset.3.qml"));
1211 1212
    object = c3.create();
    QVERIFY(object != 0);
1213
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() != 0);
1214 1215 1216
    QCOMPARE(object->property("loaderOneSourceComponentIsUndefined"), QVariant(false));
    QCOMPARE(object->property("loaderTwoSourceComponentIsUndefined"), QVariant(false));
    QMetaObject::invokeMethod(object, "resetAlias");
1217
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0);
1218 1219 1220 1221 1222 1223
    QCOMPARE(object->property("loaderOneSourceComponentIsUndefined"), QVariant(true));
    QCOMPARE(object->property("loaderTwoSourceComponentIsUndefined"), QVariant(false));
    delete object;

    // test that a manual write (of undefined) to a resettable alias property
    // whose aliased property's object has been deleted, does not crash.
1224
    QQmlComponent c4(&engine, testFileUrl("aliasreset/aliasPropertyReset.4.qml"));
1225 1226
    object = c4.create();
    QVERIFY(object != 0);
1227
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() != 0);
1228 1229 1230
    QObject *loader = object->findChild<QObject*>("loader");
    QVERIFY(loader != 0);
    delete loader;
1231
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0); // deletion should have caused value unset.
1232
    QMetaObject::invokeMethod(object, "resetAlias"); // shouldn't crash.
1233
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0);
1234
    QMetaObject::invokeMethod(object, "setAlias");   // shouldn't crash, and shouldn't change value (since it's no longer referencing anything).
1235
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0);
1236 1237 1238
    delete object;

    // test that binding an alias property to an undefined value works correctly
1239
    QQmlComponent c5(&engine, testFileUrl("aliasreset/aliasPropertyReset.5.qml"));
1240 1241
    object = c5.create();
    QVERIFY(object != 0);
1242
    QVERIFY(object->property("sourceComponentAlias").value<QQmlComponent*>() == 0); // bound to undefined value.
1243 1244 1245
    delete object;

    // test that a manual write (of undefined) to a non-resettable property fails properly
1246
    QUrl url = testFileUrl("aliasreset/aliasPropertyReset.error.1.qml");
1247
    QString warning1 = url.toString() + QLatin1String(":15: Error: Cannot assign [undefined] to int");
1248
    QQmlComponent e1(&engine, url);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    object = e1.create();
    QVERIFY(object != 0);
    QCOMPARE(object->property("intAlias").value<int>(), 12);
    QCOMPARE(object->property("aliasedIntIsUndefined"), QVariant(false));
    QTest::ignoreMessage(QtWarningMsg, warning1.toLatin1().constData());
    QMetaObject::invokeMethod(object, "resetAlias");
    QCOMPARE(object->property("intAlias").value<int>(), 12);
    QCOMPARE(object->property("aliasedIntIsUndefined"), QVariant(false));
    delete object;
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
void tst_qqmlecmascript::componentCreation_data()
{
    QTest::addColumn<QString>("method");
    QTest::addColumn<QString>("creationError");
    QTest::addColumn<QString>("createdParent");

    QTest::newRow("url")
        << "url"
        << ""
        << "";
    QTest::newRow("urlMode")
        << "urlMode"
        << ""
        << "";
    QTest::newRow("urlParent")
        << "urlParent"
        << ""
        << "obj";
    QTest::newRow("urlNullParent")
        << "urlNullParent"
        << ""
        << "null";
    QTest::newRow("urlModeParent")
        << "urlModeParent"
        << ""
        << "obj";
    QTest::newRow("urlModeNullParent")
        << "urlModeNullParent"
        << ""
        << "null";
    QTest::newRow("invalidSecondArg")
        << "invalidSecondArg"
        << ":40: Error: Qt.createComponent(): Invalid arguments"
        << "";
    QTest::newRow("invalidThirdArg")
        << "invalidThirdArg"
        << ":45: Error: Qt.createComponent(): Invalid parent object"
        << "";
    QTest::newRow("invalidMode")
        << "invalidMode"
        << ":50: Error: Qt.createComponent(): Invalid arguments"
        << "";
}

/*
Test using createComponent to dynamically generate a component.
*/
void tst_qqmlecmascript::componentCreation()
{
    QFETCH(QString, method);
    QFETCH(QString, creationError);
    QFETCH(QString, createdParent);

    QUrl testUrl(testFileUrl("componentCreation.qml"));

    if (!creationError.isEmpty()) {
        QString warning = testUrl.toString() + creationError;
        QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData());
    }

    QQmlComponent component(&engine, testUrl);
    MyTypeObject *object = qobject_cast<MyTypeObject*>(component.create());
    QVERIFY(object != 0);

    QMetaObject::invokeMethod(object, method.toUtf8());
    QQmlComponent *created = object->componentProperty();

    if (creationError.isEmpty()) {
        QVERIFY(created);

        QObject *expectedParent;
        if (createdParent.isEmpty()) {
            // For now, the parent should be the engine; this will change for QTBUG-24841
            expectedParent = &engine;
        } else if (createdParent == QLatin1String("obj")) {
            expectedParent = object;
        } else if (createdParent == QLatin1String("null")) {
            expectedParent = 0;
        }
        QCOMPARE(created->parent(), expectedParent);
    }
}

1343
void tst_qqmlecmascript::dynamicCreation_data()
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
{
    QTest::addColumn<QString>("method");
    QTest::addColumn<QString>("createdName");

    QTest::newRow("One") << "createOne" << "objectOne";
    QTest::newRow("Two") << "createTwo" << "objectTwo";
    QTest::newRow("Three") << "createThree" << "objectThree";
}

/*
Test using createQmlObject to dynamically generate an item
Also using createComponent is tested.
*/
1357
void tst_qqmlecmascript::dynamicCreation()
1358 1359 1360 1361
{
    QFETCH(QString, method);
    QFETCH(QString, createdName);

1362
    QQmlComponent component(&engine, testFileUrl("dynamicCreation.qml"));
1363 1364 1365 1366 1367 1368 1369
    MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
    QVERIFY(object != 0);

    QMetaObject::invokeMethod(object, method.toUtf8());
    QObject *created = object->objectProperty();
    QVERIFY(created);
    QCOMPARE(created->objectName(), createdName);
1370 1371

    delete object;
1372 1373 1374 1375 1376
}

/*
   Tests the destroy function
*/
1377
void tst_qqmlecmascript::dynamicDestruction()
1378
{
1379
    {
1380 1381
    QQmlComponent component(&engine, testFileUrl("dynamicDeletion.qml"));
    QQmlGuard<MyQmlObject> object = qobject_cast<MyQmlObject*>(component.create());
1382
    QVERIFY(object != 0);
1383
    QQmlGuard<QObject> createdQmlObject = 0;
1384 1385 1386 1387 1388 1389 1390 1391

    QMetaObject::invokeMethod(object, "create");
    createdQmlObject = object->objectProperty();
    QVERIFY(createdQmlObject);
    QCOMPARE(createdQmlObject->objectName(), QString("emptyObject"));

    QMetaObject::invokeMethod(object, "killOther");
    QVERIFY(createdQmlObject);
Rohan McGovern's avatar
Rohan McGovern committed
1392 1393 1394

    QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
    QCoreApplication::processEvents();
1395 1396 1397 1398
    QVERIFY(createdQmlObject);
    for (int ii = 0; createdQmlObject && ii < 50; ++ii) { // After 5 seconds we should give up
        if (createdQmlObject) {
            QTest::qWait(100);
Rohan McGovern's avatar
Rohan McGovern committed
1399 1400
            QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
            QCoreApplication::processEvents();
1401 1402 1403 1404
        }
    }
    QVERIFY(!createdQmlObject);

1405
    QQmlEngine::setObjectOwnership(object, QQmlEngine::JavaScriptOwnership);
1406 1407
    QMetaObject::invokeMethod(object, "killMe");
    QVERIFY(object);
Rohan McGovern's avatar
Rohan McGovern committed
1408 1409
    QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
    QCoreApplication::processEvents();
1410
    QVERIFY(!object);
1411 1412 1413
    }

    {
1414
    QQmlComponent component(&engine, testFileUrl("dynamicDeletion.2.qml"));
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    QObject *o = component.create();
    QVERIFY(o != 0);

    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) == 0);

    QMetaObject::invokeMethod(o, "create");

    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) != 0);

    QMetaObject::invokeMethod(o, "destroy");

Rohan McGovern's avatar
Rohan McGovern committed
1426 1427
    QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
    QCoreApplication::processEvents();
1428 1429 1430 1431 1432

    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) == 0);

    delete o;
    }
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

    {
    // QTBUG-23451
    QQmlGuard<QObject> createdQmlObject = 0;
    QQmlComponent component(&engine, testFileUrl("dynamicDeletion.3.qml"));
    QObject *o = component.create();
    QVERIFY(o != 0);
    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) == 0);
    QMetaObject::invokeMethod(o, "create");
    createdQmlObject = qvariant_cast<QObject*>(o->property("objectProperty"));
    QVERIFY(createdQmlObject);
    QMetaObject::invokeMethod(o, "destroy");
    QVERIFY(qvariant_cast<bool>(o->property("test")) == false);
    for (int ii = 0; createdQmlObject && ii < 50; ++ii) { // After 5 seconds we should give up
        QTest::qWait(100);
        QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
        QCoreApplication::processEvents();
    }
    QVERIFY(qvariant_cast<QObject*>(o->property("objectProperty")) == 0);
    QVERIFY(qvariant_cast<bool>(o->property("test")) == true);
    delete o;
    }
1455 1456 1457 1458 1459
}

/*
   tests that id.toString() works
*/
1460
void tst_qqmlecmascript::objectToString()
1461
{
1462
    QQmlComponent component(&engine, testFileUrl("qmlToString.qml"));
1463 1464 1465 1466 1467
    MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
    QVERIFY(object != 0);
    QMetaObject::invokeMethod(object, "testToString");
    QVERIFY(object->stringProperty().startsWith("MyQmlObject_QML_"));
    QVERIFY(object->stringProperty().endsWith(", \"objName\")"));
1468 1469

    delete object;