Support better boolean conversion semantics
authorAaron Kennedy <aaron.kennedy@nokia.com>
Mon, 25 Jul 2011 03:35:03 +0000 (13:35 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 25 Jul 2011 04:08:56 +0000 (06:08 +0200)
Task-number: QTBUG-20242

Change-Id: Ie678f6189a8060de600b5394fbaaaef49be274c6
Reviewed-on: http://codereview.qt.nokia.com/2061
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>
src/declarative/qml/v8/qv8engine.cpp
tests/auto/declarative/qdeclarativeecmascript/data/booleanConversion.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp

index a6af68d..b8a1bc4 100644 (file)
@@ -174,6 +174,9 @@ QVariant QV8Engine::toVariant(v8::Handle<v8::Value> value, int typeHint)
     if (value.IsEmpty()) 
         return QVariant();
 
+    if (typeHint == QVariant::Bool)
+        return QVariant(value->BooleanValue());
+
     if (value->IsObject()) {
         QV8ObjectResource *r = (QV8ObjectResource *)value->ToObject()->GetExternalResource();
         if (r) {
diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/booleanConversion.qml b/tests/auto/declarative/qdeclarativeecmascript/data/booleanConversion.qml
new file mode 100644 (file)
index 0000000..a7e08ec
--- /dev/null
@@ -0,0 +1,28 @@
+import QtQuick 1.0
+
+QtObject {
+    id: root
+
+    property bool test_true1: false
+    property bool test_true2: false
+    property bool test_true3: false
+    property bool test_true4: false
+    property bool test_true5: false
+
+    property bool test_false1: true
+    property bool test_false2: true
+    property bool test_false3: true
+
+
+    Component.onCompleted: {
+        test_true1 = 11
+        test_true2 = "Hello"
+        test_true3 = root
+        test_true4 = { a: 10, b: 11 }
+        test_true5 = true
+
+        test_false1 = 0
+        test_false2 = null
+        test_false3 = false
+    }
+}
index aafae1e..d73da28 100644 (file)
@@ -150,6 +150,7 @@ private slots:
     void propertyChangeSlots();
     void elementAssign();
     void objectPassThroughSignals();
+    void booleanConversion();
 
     void bug1();
     void bug2();
@@ -2938,6 +2939,27 @@ void tst_qdeclarativeecmascript::objectPassThroughSignals()
     delete object;
 }
 
+// QTBUG-20242
+void tst_qdeclarativeecmascript::booleanConversion()
+{
+    QDeclarativeComponent component(&engine, TEST_FILE("booleanConversion.qml"));
+
+    QObject *object = component.create();
+    QVERIFY(object != 0);
+
+    QCOMPARE(object->property("test_true1").toBool(), true);
+    QCOMPARE(object->property("test_true2").toBool(), true);
+    QCOMPARE(object->property("test_true3").toBool(), true);
+    QCOMPARE(object->property("test_true4").toBool(), true);
+    QCOMPARE(object->property("test_true5").toBool(), true);
+
+    QCOMPARE(object->property("test_false1").toBool(), false);
+    QCOMPARE(object->property("test_false2").toBool(), false);
+    QCOMPARE(object->property("test_false3").toBool(), false);
+
+    delete object;
+}
+
 // Test that assigning a null object works 
 // Regressed with: df1788b4dbbb2826ae63f26bdf166342595343f4
 void tst_qdeclarativeecmascript::nullObjectBinding()