aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmllanguage
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@digia.com>2014-04-11 12:16:44 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-04-12 08:03:16 +0200
commitff0857541d5d391c7c03cce5893b41dd9b35e7fa (patch)
tree0d20dba80e8aa3e541a7d51b135c8a6b65b60019 /tests/auto/qml/qqmllanguage
parentde11a78c625b0865f8cd3d0cfe446cba2bdb719e (diff)
Fix assignments of null strings in QML and JavaScript
Assigning "" to a string based property would exhibit inconsistent behavior: * When assigned as a literal, it would assign an empty string * When assigned from JavaScript, it would assign a null string When the assignment was done _to_ a QByteArray property, it would hit the case of calling QVariant::convert where the incoming variant is either an empty or a null string and the target is a QByteArray. For historical reasons - as documented - QVariant::convert will return false when the incoming variant is a null variant. In V8 assignment from JavaScript would produce an empty string and thus hit the "succesfull" conversion code path in QVariant to convert to a seemingly empty QByteArray. With v4 a null string would result in a failed conversion and spurious warnings as seen in the reported task. This patch ensures that we consistently map "" to a null string, when it comes from JavaScript or QML as a literal string. We now also detect the situation of trying to convert a (valid) null variant to another target type. Task-number: QTBUG-37197 Change-Id: I68f9031262fdd287d69a38d5468fb38a20441d7b Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
Diffstat (limited to 'tests/auto/qml/qqmllanguage')
-rw-r--r--tests/auto/qml/qqmllanguage/data/assignNullStrings.qml9
-rw-r--r--tests/auto/qml/qqmllanguage/testtypes.h11
-rw-r--r--tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp14
3 files changed, 34 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmllanguage/data/assignNullStrings.qml b/tests/auto/qml/qqmllanguage/data/assignNullStrings.qml
new file mode 100644
index 0000000000..5e1c3a9b03
--- /dev/null
+++ b/tests/auto/qml/qqmllanguage/data/assignNullStrings.qml
@@ -0,0 +1,9 @@
+import Test 1.0
+MyTypeObject {
+ stringProperty: ""
+ byteArrayProperty: ""
+ function assignNullStringsFromJs() {
+ stringProperty = ""
+ byteArrayProperty = ""
+ }
+}
diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h
index 6a6b2eba45..1c13a2e21c 100644
--- a/tests/auto/qml/qqmllanguage/testtypes.h
+++ b/tests/auto/qml/qqmllanguage/testtypes.h
@@ -236,6 +236,7 @@ class MyTypeObject : public QObject
Q_PROPERTY(MyMirroredEnum mirroredEnumProperty READ mirroredEnumProperty WRITE setMirroredEnumProperty NOTIFY mirroredEnumPropertyChanged)
Q_PROPERTY(MyEnumContainer::RelatedEnum relatedEnumProperty READ relatedEnumProperty WRITE setRelatedEnumProperty)
Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringPropertyChanged)
+ Q_PROPERTY(QByteArray byteArrayProperty READ byteArrayProperty WRITE setByteArrayProperty NOTIFY byteArrayPropertyChanged)
Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty NOTIFY uintPropertyChanged)
Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
Q_PROPERTY(qreal realProperty READ realProperty WRITE setRealProperty NOTIFY realPropertyChanged)
@@ -357,6 +358,15 @@ public:
emit stringPropertyChanged();
}
+ QByteArray byteArrayPropertyValue;
+ QByteArray byteArrayProperty() const {
+ return byteArrayPropertyValue;
+ }
+ void setByteArrayProperty(const QByteArray &v) {
+ byteArrayPropertyValue = v;
+ emit byteArrayPropertyChanged();
+ }
+
uint uintPropertyValue;
uint uintProperty() const {
return uintPropertyValue;
@@ -570,6 +580,7 @@ signals:
void qtEnumPropertyChanged();
void mirroredEnumPropertyChanged();
void stringPropertyChanged();
+ void byteArrayPropertyChanged();
void uintPropertyChanged();
void intPropertyChanged();
void realPropertyChanged();
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
index 55d07e78da..87f811cbbc 100644
--- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
+++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
@@ -110,6 +110,7 @@ private slots:
void assignLiteralToVariant();
void assignLiteralToVar();
void assignLiteralToJSValue();
+ void assignNullStrings();
void bindJSValueToVar();
void bindJSValueToVariant();
void bindJSValueToType();
@@ -873,6 +874,19 @@ void tst_qqmllanguage::assignLiteralToJSValue()
}
}
+void tst_qqmllanguage::assignNullStrings()
+{
+ QQmlComponent component(&engine, testFileUrl("assignNullStrings.qml"));
+ VERIFY_ERRORS(0);
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+ QVERIFY(object->stringProperty().isNull());
+ QVERIFY(object->byteArrayProperty().isNull());
+ QMetaObject::invokeMethod(object, "assignNullStringsFromJs", Qt::DirectConnection);
+ QVERIFY(object->stringProperty().isNull());
+ QVERIFY(object->byteArrayProperty().isNull());
+}
+
void tst_qqmllanguage::bindJSValueToVar()
{
QQmlComponent component(&engine, testFileUrl("assignLiteralToJSValue.qml"));