diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-05-14 04:12:29 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-05-14 04:12:29 (GMT) |
commit | b1008e6a4f660866265083f6f4bd321a85cff377 (patch) | |
tree | b3b485c1172240d88560691a5988bc97a4c9af0b /tests/auto | |
parent | 8ac395b4293433d3d98e4e8d9825f86d6a351d69 (diff) | |
download | Qt-b1008e6a4f660866265083f6f4bd321a85cff377.zip Qt-b1008e6a4f660866265083f6f4bd321a85cff377.tar.gz Qt-b1008e6a4f660866265083f6f4bd321a85cff377.tar.bz2 |
Add some expression tests
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/declarative/qmlbindengine/tst_qmlbindengine.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/auto/declarative/qmlbindengine/tst_qmlbindengine.cpp b/tests/auto/declarative/qmlbindengine/tst_qmlbindengine.cpp index 251bce4..72c8953 100644 --- a/tests/auto/declarative/qmlbindengine/tst_qmlbindengine.cpp +++ b/tests/auto/declarative/qmlbindengine/tst_qmlbindengine.cpp @@ -80,6 +80,8 @@ private slots: void methods(); void signalAssignment(); void bindingLoop(); + void basicExpressions(); + void basicExpressions_data(); void contextPropertiesTriggerReeval(); private: @@ -170,6 +172,99 @@ public: bool changed; }; + +class MyDefaultObject1 : public QObject +{ + Q_OBJECT + Q_PROPERTY(int horseLegs READ horseLegs); + Q_PROPERTY(int antLegs READ antLegs); +public: + int horseLegs() const { return 4; } + int antLegs() const { return 6; } +}; + +class MyDefaultObject2 : public QObject +{ + Q_OBJECT + Q_PROPERTY(int antLegs READ antLegs); + Q_PROPERTY(int emuLegs READ emuLegs); +public: + int antLegs() const { return 5; } // Had an accident + int emuLegs() const { return 2; } +}; + +class MyDefaultObject3 : public QObject +{ + Q_OBJECT + Q_PROPERTY(int antLegs READ antLegs); + Q_PROPERTY(int humanLegs READ humanLegs); +public: + int antLegs() const { return 7; } // Mutant + int humanLegs() const { return 2; } + int millipedeLegs() const { return 1000; } +}; + +void tst_qmlbindengine::basicExpressions_data() +{ + QTest::addColumn<QString>("expression"); + QTest::addColumn<QVariant>("result"); + QTest::addColumn<bool>("nest"); + + 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; +} + +void tst_qmlbindengine::basicExpressions() +{ + QFETCH(QString, expression); + QFETCH(QVariant, result); + QFETCH(bool, nest); + + MyQmlObject object1; + MyQmlObject object2; + MyQmlObject object3; + MyDefaultObject1 default1; + MyDefaultObject2 default2; + MyDefaultObject3 default3; + object1.setStringProperty("Object1"); + object2.setStringProperty("Object2"); + object3.setStringProperty("Object3"); + + QmlContext context(engine.rootContext()); + QmlContext nestedContext(&context); + + context.addDefaultObject(&default1); + context.addDefaultObject(&default2); + context.setContextProperty("a", QVariant(1944)); + context.setContextProperty("b", QVariant("Milk")); + context.setContextProperty("object", &object1); + context.setContextProperty("objectOverride", &object2); + nestedContext.addDefaultObject(&default3); + nestedContext.setContextProperty("b", QVariant("Cow")); + nestedContext.setContextProperty("objectOverride", &object3); + nestedContext.setContextProperty("millipedeLegs", QVariant(100)); + + MyExpression expr(nest?&nestedContext:&context, expression); + QCOMPARE(expr.value(), result); +} + +// Tests that modifying a context property will reevaluate expressions void tst_qmlbindengine::contextPropertiesTriggerReeval() { QmlContext context(engine.rootContext()); |