aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-02-24 13:31:20 +0100
committerIvan Solovev <ivan.solovev@qt.io>2021-03-10 15:17:57 +0100
commitea8f8e09007f4acb5530b1966bc5b2afab609064 (patch)
tree03b2c9ff8defd65a45daefe516ecc51501b528d7 /tests/auto/qml
parentc42d558dc9ff89d452546412ee88a16ae1e324e4 (diff)
Introduce XmlListModel to QtDeclarative
XmlListModel was previously a part of QtXmlPatterns, which would not be a part of Qt 6. The idea of this commit is to move a simplified version of XmlListModel to QtDeclarative, so that it could be used at least in the examples of different Qt modules. Unlike the old implementation, this version does not have an XPath support. This results in a reduced feature set - the user can't build complicated XPath queries to populate model roles. Now the user can select an xml element and, optionally, an attribute, which will be used to extract the data. [ChangeLog][XmlListModel] Introduce an XmlListModel QML model to create read-only models from XML data. This is a simplified version of a model from QtXmlPatterns, which would no longer be a part of Qt 6. This model supports only simple slash-separated paths and, optionally, one attribute for each element. Task-number: QTBUG-89817 Change-Id: I4186587dc1445dd981ac92b4ce104434236a32b9 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r--tests/auto/qml/CMakeLists.txt3
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/CMakeLists.txt27
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/attributes.qml11
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/attributes.xml14
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/elementErrors.qml9
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/empty.xml0
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/malformedAttribute.xml28
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/malformedData.qml10
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/malformedTagNestedLevel.xml28
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/malformedTagTopLevel.xml28
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/model.qml11
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/model.xml54
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/model2.xml14
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/nestedElements.qml11
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/nestedElements.xml76
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/propertychanges.qml10
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/proxyCrash.qml8
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/recipes.qml10
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/recipes.xml90
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/roleCrash.qml10
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/threading.qml8
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/data/unique.qml8
-rw-r--r--tests/auto/qml/qqmlxmllistmodel/tst_qqmlxmllistmodel.cpp710
23 files changed, 1178 insertions, 0 deletions
diff --git a/tests/auto/qml/CMakeLists.txt b/tests/auto/qml/CMakeLists.txt
index 58520d92a0..c0b26c09b4 100644
--- a/tests/auto/qml/CMakeLists.txt
+++ b/tests/auto/qml/CMakeLists.txt
@@ -99,6 +99,9 @@ if(QT_FEATURE_private_tests)
add_subdirectory(bindingdependencyapi)
add_subdirectory(v4misc)
add_subdirectory(qqmldelegatemodel) # special case
+ if (QT_FEATURE_qml_xmllistmodel)
+ add_subdirectory(qqmlxmllistmodel)
+ endif()
endif()
if(NOT CMAKE_CROSSCOMPILING)
add_subdirectory(qmltyperegistrar)
diff --git a/tests/auto/qml/qqmlxmllistmodel/CMakeLists.txt b/tests/auto/qml/qqmlxmllistmodel/CMakeLists.txt
new file mode 100644
index 0000000000..3748661ce2
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/CMakeLists.txt
@@ -0,0 +1,27 @@
+file(GLOB_RECURSE test_data_glob
+ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
+ data/*)
+list(APPEND test_data ${test_data_glob})
+
+qt_internal_add_test(tst_qqmlxmllistmodel
+ SOURCES
+ ../../shared/util.cpp ../../shared/util.h
+ tst_qqmlxmllistmodel.cpp
+ INCLUDE_DIRECTORIES
+ ../../shared
+ LIBRARIES
+ Qt::Core
+ Qt::Qml
+ Qt::QmlXmlListModelPrivate
+ TESTDATA ${test_data}
+)
+
+qt_internal_extend_target(tst_qqmlxmllistmodel CONDITION ANDROID OR IOS
+ DEFINES
+ QT_QMLTEST_DATADIR=\\\":/data\\\"
+)
+
+qt_internal_extend_target(tst_qqmlxmllistmodel CONDITION NOT ANDROID AND NOT IOS
+ DEFINES
+ QT_QMLTEST_DATADIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}/data\\\"
+)
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/attributes.qml b/tests/auto/qml/qqmlxmllistmodel/data/attributes.qml
new file mode 100644
index 0000000000..df44c7862b
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/attributes.qml
@@ -0,0 +1,11 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "attributes.xml"
+ query: "/Pets/Pet"
+
+ XmlListModelRole { name: "name"; elementName: ""; attributeName: "name" }
+ XmlListModelRole { name: "type"; elementName: "info"; attributeName: "type" }
+ XmlListModelRole { name: "age"; elementName: "info"; attributeName: "age" }
+ XmlListModelRole { name: "size"; elementName: "info"; attributeName: "size" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/attributes.xml b/tests/auto/qml/qqmlxmllistmodel/data/attributes.xml
new file mode 100644
index 0000000000..a10b77c7dc
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/attributes.xml
@@ -0,0 +1,14 @@
+<Pets>
+ <Pet name="Polly">
+ <info type="Parrot" age="12" size="Small"/>
+ </Pet>
+ <Pet name="Penny">
+ <info type="Turtle" age="4" size="Small"/>
+ </Pet>
+ <Pet name="Spot">
+ <info type="Dog" age="9" size="Medium"/>
+ </Pet>
+ <Pet name="Tiny">
+ <info type="Elephant" age="15" size="Large"/>
+ </Pet>
+</Pets>
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/elementErrors.qml b/tests/auto/qml/qqmlxmllistmodel/data/elementErrors.qml
new file mode 100644
index 0000000000..559aab1ef4
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/elementErrors.qml
@@ -0,0 +1,9 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "model.xml"
+ query: "/Pets/Pet"
+ XmlListModelRole { name: "name"; elementName: "/name" } // starts with '/'
+ XmlListModelRole { name: "age"; elementName: "age/" } // ends with '/'
+ XmlListModelRole { name: "type"; elementName: "some//element" } // contains "//"
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/empty.xml b/tests/auto/qml/qqmlxmllistmodel/data/empty.xml
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/empty.xml
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/malformedAttribute.xml b/tests/auto/qml/qqmlxmllistmodel/data/malformedAttribute.xml
new file mode 100644
index 0000000000..6e4c0e3637
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/malformedAttribute.xml
@@ -0,0 +1,28 @@
+<Pets>
+ <Pet>
+ <name>Polly</name>
+ <completely>
+ <useless>
+ <hierarchy/>
+ </useless>
+ </completely>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <another>
+ <nested>
+ <tag/>
+ </nested>
+ </another>
+ <useful>
+ <tags>
+ <type>Parrot</type>
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age 1value="12"/>
+ </other>
+ <size>Small</size>
+ </some>
+ </Pet>
+</Pets> \ No newline at end of file
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/malformedData.qml b/tests/auto/qml/qqmlxmllistmodel/data/malformedData.qml
new file mode 100644
index 0000000000..092eaa0cdf
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/malformedData.qml
@@ -0,0 +1,10 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ query: "/Pets/Pet"
+
+ XmlListModelRole { name: "name"; elementName: "name" }
+ XmlListModelRole { name: "type"; elementName: "some/other/useful/tags/type" }
+ XmlListModelRole { name: "age"; elementName: "some/other/age"; attributeName: "value" }
+ XmlListModelRole { name: "size"; elementName: "some/size" }
+} \ No newline at end of file
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/malformedTagNestedLevel.xml b/tests/auto/qml/qqmlxmllistmodel/data/malformedTagNestedLevel.xml
new file mode 100644
index 0000000000..d0332e2924
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/malformedTagNestedLevel.xml
@@ -0,0 +1,28 @@
+<Pets>
+ <Pet>
+ <name>Polly</name>
+ <completely>
+ <useless>
+ <hierarchy/>
+ </useless>
+ </completely>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <another>
+ <nested>
+ <tag/>
+ </nested>
+ </another>
+ <useful>
+ <tags>
+ <type>Parrot
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age value="12"/>
+ </other>
+ <size>Small</size>
+ </some>
+ </Pet>
+</Pets> \ No newline at end of file
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/malformedTagTopLevel.xml b/tests/auto/qml/qqmlxmllistmodel/data/malformedTagTopLevel.xml
new file mode 100644
index 0000000000..b499db4b51
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/malformedTagTopLevel.xml
@@ -0,0 +1,28 @@
+<Pets>
+ <Pet>
+ <name>Polly</na>
+ <completely>
+ <useless>
+ <hierarchy/>
+ </useless>
+ </completely>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <another>
+ <nested>
+ <tag/>
+ </nested>
+ </another>
+ <useful>
+ <tags>
+ <type>Parrot</type>
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age value="12"/>
+ </other>
+ <size>Small</size>
+ </some>
+ </Pet>
+</Pets> \ No newline at end of file
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/model.qml b/tests/auto/qml/qqmlxmllistmodel/data/model.qml
new file mode 100644
index 0000000000..6634725d4c
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/model.qml
@@ -0,0 +1,11 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "model.xml"
+ query: "/Pets/Pet"
+
+ XmlListModelRole { name: "name"; elementName: "name" }
+ XmlListModelRole { name: "type"; elementName: "type" }
+ XmlListModelRole { name: "age"; elementName: "age" }
+ XmlListModelRole { name: "size"; elementName: "size" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/model.xml b/tests/auto/qml/qqmlxmllistmodel/data/model.xml
new file mode 100644
index 0000000000..40cd6d0432
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/model.xml
@@ -0,0 +1,54 @@
+<Pets>
+ <Pet>
+ <name>Polly</name>
+ <type>Parrot</type>
+ <age>12</age>
+ <size>Small</size>
+ </Pet>
+ <Pet>
+ <name>Penny</name>
+ <type>Turtle</type>
+ <age>4</age>
+ <size>Small</size>
+ </Pet>
+ <Pet>
+ <name>Warren</name>
+ <type>Rabbit</type>
+ <age>2</age>
+ <size>Small</size>
+ </Pet>
+ <Pet>
+ <name>Spot</name>
+ <type>Dog</type>
+ <age>9</age>
+ <size>Medium</size>
+ </Pet>
+ <Pet>
+ <name>Whiskers</name>
+ <type>Cat</type>
+ <age>2</age>
+ <size>Medium</size>
+ </Pet>
+ <Pet>
+ <name>Joey</name>
+ <type>Kangaroo</type>
+ <age>1</age>
+ </Pet>
+ <Pet>
+ <name>Kimba</name>
+ <type>Bunny</type>
+ <age>65</age>
+ <size>Large</size>
+ </Pet>
+ <Pet>
+ <name>Rover</name>
+ <type>Dog</type>
+ <size>Large</size>
+ </Pet>
+ <Pet>
+ <name>Tiny</name>
+ <type>Elephant</type>
+ <age>15</age>
+ <size>Large</size>
+ </Pet>
+</Pets>
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/model2.xml b/tests/auto/qml/qqmlxmllistmodel/data/model2.xml
new file mode 100644
index 0000000000..dab2ec6dc0
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/model2.xml
@@ -0,0 +1,14 @@
+<Pets>
+ <Pet>
+ <name>Polly</name>
+ <type>Parrot</type>
+ <age>12</age>
+ <size>Small</size>
+ </Pet>
+ <Pet>
+ <name>Penny</name>
+ <type>Turtle</type>
+ <age>4</age>
+ <size>Small</size>
+ </Pet>
+</Pets>
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/nestedElements.qml b/tests/auto/qml/qqmlxmllistmodel/data/nestedElements.qml
new file mode 100644
index 0000000000..2106ac3f77
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/nestedElements.qml
@@ -0,0 +1,11 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "nestedElements.xml"
+ query: "/Pets/Pet"
+
+ XmlListModelRole { name: "name"; elementName: "name" }
+ XmlListModelRole { name: "type"; elementName: "some/other/useful/tags/type" }
+ XmlListModelRole { name: "age"; elementName: "some/other/age"; attributeName: "value" }
+ XmlListModelRole { name: "size"; elementName: "some/size" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/nestedElements.xml b/tests/auto/qml/qqmlxmllistmodel/data/nestedElements.xml
new file mode 100644
index 0000000000..8ea904920e
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/nestedElements.xml
@@ -0,0 +1,76 @@
+<Pets>
+ <Pet>
+ <name>Polly</name>
+ <completely>
+ <useless>
+ <hierarchy/>
+ </useless>
+ </completely>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <another>
+ <nested>
+ <tag/>
+ </nested>
+ </another>
+ <useful>
+ <tags>
+ <type>Parrot</type>
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age value="12"/>
+ </other>
+ <size>Small</size>
+ </some>
+ </Pet>
+ <Pet>
+ <name>Penny</name>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <useful>
+ <tags>
+ <type>Turtle</type>
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age value="4"/>
+ </other>
+ <size>Small</size>
+ </some>
+ </Pet>
+ <Pet>
+ <name>Spot</name>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <useful>
+ <tags>
+ <type>Dog</type>
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age value="9"/>
+ </other>
+ <size>Medium</size>
+ </some>
+ </Pet>
+ <Pet>
+ <name>Tiny</name>
+ <some>
+ <other>
+ <type>incorrect type tag</type>
+ <useful>
+ <tags>
+ <type>Elephant</type>
+ <size>incorrect size tag</size>
+ </tags>
+ </useful>
+ <age value="15"/>
+ </other>
+ <size>Large</size>
+ </some>
+ </Pet>
+</Pets>
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/propertychanges.qml b/tests/auto/qml/qqmlxmllistmodel/data/propertychanges.qml
new file mode 100644
index 0000000000..3b3e77d726
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/propertychanges.qml
@@ -0,0 +1,10 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "model.xml"
+ query: "/Pets/Pet"
+ XmlListModelRole { objectName: "role"; name: "name"; elementName: "name" }
+ XmlListModelRole { name: "type"; elementName: "type" }
+ XmlListModelRole { name: "age"; elementName: "age" }
+ XmlListModelRole { name: "size"; elementName: "size" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/proxyCrash.qml b/tests/auto/qml/qqmlxmllistmodel/data/proxyCrash.qml
new file mode 100644
index 0000000000..9233975b19
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/proxyCrash.qml
@@ -0,0 +1,8 @@
+import QtQml.XmlListModel
+import SortFilterProxyModel 1.0
+
+SortFilterProxyModel {
+ source: XmlListModel {
+ XmlListModelRole { }
+ }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/recipes.qml b/tests/auto/qml/qqmlxmllistmodel/data/recipes.qml
new file mode 100644
index 0000000000..18cd8c301a
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/recipes.qml
@@ -0,0 +1,10 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "recipes.xml"
+ query: "/recipes/recipe"
+ XmlListModelRole { name: "title"; elementName: ""; attributeName: "title" }
+ XmlListModelRole { name: "picture"; elementName: "picture" }
+ XmlListModelRole { name: "ingredients"; elementName: "ingredients" }
+ XmlListModelRole { name: "preparation"; elementName: "method" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/recipes.xml b/tests/auto/qml/qqmlxmllistmodel/data/recipes.xml
new file mode 100644
index 0000000000..d71de60710
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/recipes.xml
@@ -0,0 +1,90 @@
+<recipes>
+ <recipe title="Pancakes">
+ <picture>content/pics/pancakes.jpg</picture>
+ <ingredients><![CDATA[<html>
+ <ul>
+ <li> 1 cup (150g) self-raising flour
+ <li> 1 tbs caster sugar
+ <li> 3/4 cup (185ml) milk
+ <li> 1 egg
+ </ul>
+ </html>
+ ]]></ingredients>
+ <method><![CDATA[<html>
+ <ol>
+ <li> Sift flour and sugar together into a bowl. Add a pinch of salt.
+ <li> Beat milk and egg together, then add to dry ingredients. Beat until smooth.
+ <li> Pour mixture into a pan on medium heat and cook until bubbles appear on the surface.
+ <li> Turn over and cook other side until golden.
+ </ol>
+ </html>
+ ]]></method>
+ </recipe>
+ <recipe title="Fruit Salad">
+ <picture>content/pics/fruit-salad.jpg</picture>
+ <ingredients><![CDATA[* Seasonal Fruit]]></ingredients>
+ <method><![CDATA[* Chop fruit and place in a bowl.]]></method>
+ </recipe>
+ <recipe title="Vegetable Soup">
+ <picture>content/pics/vegetable-soup.jpg</picture>
+ <ingredients><![CDATA[<html>
+ <ul>
+ <li> 1 onion
+ <li> 1 turnip
+ <li> 1 potato
+ <li> 1 carrot
+ <li> 1 head of celery
+ <li> 1 1/2 litres of water
+ </ul>
+ </html>
+ ]]></ingredients>
+ <method><![CDATA[<html>
+ <ol>
+ <li> Chop vegetables.
+ <li> Boil in water until vegetables soften.
+ <li> Season with salt and pepper to taste.
+ </ol>
+ </html>
+ ]]></method>
+ </recipe>
+ <recipe title="Hamburger">
+ <picture>content/pics/hamburger.jpg</picture>
+ <ingredients><![CDATA[<html>
+ <ul>
+ <li> 500g minced beef
+ <li> Seasoning
+ <li> lettuce, tomato, onion, cheese
+ <li> 1 hamburger bun for each burger
+ </ul>
+ </html>
+ ]]></ingredients>
+ <method><![CDATA[<html>
+ <ol>
+ <li> Mix the beef, together with seasoning, in a food processor.
+ <li> Shape the beef into burgers.
+ <li> Grill the burgers for about 5 mins on each side (until cooked through)
+ <li> Serve each burger on a bun with ketchup, cheese, lettuce, tomato and onion.
+ </ol>
+ </html>
+ ]]></method>
+ </recipe>
+ <recipe title="Lemonade">
+ <picture>content/pics/lemonade.jpg</picture>
+ <ingredients><![CDATA[<html>
+ <ul>
+ <li> 1 cup Lemon Juice
+ <li> 1 cup Sugar
+ <li> 6 Cups of Water (2 cups warm water, 4 cups cold water)
+ </ul>
+ </html>
+ ]]></ingredients>
+ <method><![CDATA[<html>
+ <ol>
+ <li> Pour 2 cups of warm water into a pitcher and stir in sugar until it dissolves.
+ <li> Pour in lemon juice, stir again, and add 4 cups of cold water.
+ <li> Chill or serve over ice cubes.
+ </ol>
+ </html>
+ ]]></method>
+ </recipe>
+</recipes>
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/roleCrash.qml b/tests/auto/qml/qqmlxmllistmodel/data/roleCrash.qml
new file mode 100644
index 0000000000..85cb692f03
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/roleCrash.qml
@@ -0,0 +1,10 @@
+import QtQuick 2.0
+import QtQml.XmlListModel
+
+XmlListModel {
+ id: model
+ XmlListModelRole {}
+ Component.onCompleted: {
+ model.roles = 0
+ }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/threading.qml b/tests/auto/qml/qqmlxmllistmodel/data/threading.qml
new file mode 100644
index 0000000000..43672d01ba
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/threading.qml
@@ -0,0 +1,8 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ query: "/data/item"
+ XmlListModelRole { name: "name"; elementName: "name" }
+ XmlListModelRole { name: "age"; elementName: "age" }
+ XmlListModelRole { name: "sport"; elementName: "sport" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/data/unique.qml b/tests/auto/qml/qqmlxmllistmodel/data/unique.qml
new file mode 100644
index 0000000000..1289c7de93
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/data/unique.qml
@@ -0,0 +1,8 @@
+import QtQml.XmlListModel
+
+XmlListModel {
+ source: "model.xml"
+ query: "/Pets/Pet"
+ XmlListModelRole { name: "name"; elementName: "name" }
+ XmlListModelRole { name: "name"; elementName: "type" }
+}
diff --git a/tests/auto/qml/qqmlxmllistmodel/tst_qqmlxmllistmodel.cpp b/tests/auto/qml/qqmlxmllistmodel/tst_qqmlxmllistmodel.cpp
new file mode 100644
index 0000000000..947b64a5cb
--- /dev/null
+++ b/tests/auto/qml/qqmlxmllistmodel/tst_qqmlxmllistmodel.cpp
@@ -0,0 +1,710 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QtTest>
+#include <QSignalSpy>
+#include <QMetaObject>
+#include <QSortFilterProxyModel>
+#include <QFile>
+#include <QTimer>
+#include <QTemporaryFile>
+#include <QNetworkRequest>
+#include <QNetworkAccessManager>
+#include <QtQmlXmlListModel/private/qqmlxmllistmodel_p.h>
+#include "../../shared/util.h"
+
+typedef QList<QVariantList> QQmlXmlModelData;
+
+Q_DECLARE_METATYPE(QQmlXmlModelData)
+Q_DECLARE_METATYPE(QQmlXmlListModel::Status)
+
+class tst_QQmlXmlListModel : public QQmlDataTest
+
+{
+ Q_OBJECT
+public:
+ tst_QQmlXmlListModel() { }
+
+private slots:
+ void initTestCase() override
+ {
+ QQmlDataTest::initTestCase();
+ qRegisterMetaType<QQmlXmlListModel::Status>();
+ }
+
+ void buildModel();
+ void cdata();
+ void attributes();
+ void roles();
+ void elementErrors();
+ void uniqueRoleNames();
+ void headers();
+ void source();
+ void source_data();
+ void data();
+ void reload();
+ void threading();
+ void threading_data();
+ void propertyChanges();
+ void nestedElements();
+ void malformedData();
+ void malformedData_data();
+
+ void roleCrash();
+ void proxyCrash();
+
+private:
+ QString errorString(QAbstractItemModel *model)
+ {
+ QString ret;
+ QMetaObject::invokeMethod(model, "errorString", Q_RETURN_ARG(QString, ret));
+ return ret;
+ }
+
+ QString makeItemXmlAndData(const QString &data, QQmlXmlModelData *modelData = 0) const
+ {
+ if (modelData)
+ modelData->clear();
+ QString xml;
+
+ if (!data.isEmpty()) {
+ const QStringList items = data.split(QLatin1Char(';'));
+ for (const QString &item : items) {
+ if (item.isEmpty())
+ continue;
+ QVariantList variants;
+ xml += QLatin1String("<item>");
+ const QStringList fields = item.split(QLatin1Char(','));
+ for (const QString &field : fields) {
+ QStringList values = field.split(QLatin1Char('='));
+ if (values.count() != 2) {
+ qWarning() << "makeItemXmlAndData: invalid field:" << field;
+ continue;
+ }
+ xml += QString("<%1>%2</%1>").arg(values[0], values[1]);
+ if (!modelData)
+ continue;
+ bool isNum = false;
+ int number = values[1].toInt(&isNum);
+ if (isNum)
+ variants << number;
+ else
+ variants << values[1];
+ }
+ xml += QLatin1String("</item>");
+ if (modelData)
+ modelData->append(variants);
+ }
+ }
+
+ QString decl = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>";
+ return decl + QLatin1String("<data>") + xml + QLatin1String("</data>");
+ }
+
+ QQmlEngine engine;
+};
+
+class ScopedFile
+{
+public:
+ ScopedFile(const QUrl &url, const QByteArray &data) : m_fileUrl(url)
+ {
+ m_file.setFileName(url.toLocalFile());
+ m_created = m_file.open(QIODevice::WriteOnly | QIODevice::Truncate);
+ if (m_created) {
+ const auto written = m_file.write(data);
+ m_created = written == data.size();
+ m_file.close();
+ }
+ }
+ ~ScopedFile() { QFile::remove(m_file.fileName()); }
+
+ bool isCreated() const { return m_created; }
+ QUrl fileUrl() const { return m_fileUrl; }
+
+private:
+ QFile m_file;
+ const QUrl m_fileUrl;
+ bool m_created = false;
+};
+
+class CustomNetworkAccessManagerFactory : public QObject, public QQmlNetworkAccessManagerFactory
+{
+ Q_OBJECT
+public:
+ QVariantMap lastSentHeaders;
+
+protected:
+ QNetworkAccessManager *create(QObject *parent) override;
+};
+
+class CustomNetworkAccessManager : public QNetworkAccessManager
+{
+ Q_OBJECT
+public:
+ CustomNetworkAccessManager(CustomNetworkAccessManagerFactory *factory, QObject *parent)
+ : QNetworkAccessManager(parent), m_factory(factory)
+ {
+ }
+
+protected:
+ QNetworkReply *createRequest(Operation op, const QNetworkRequest &req,
+ QIODevice *outgoingData = 0) override
+ {
+ if (m_factory) {
+ QVariantMap map;
+ const auto rawHeaderList = req.rawHeaderList();
+ for (const QString &header : rawHeaderList)
+ map[header] = req.rawHeader(header.toUtf8());
+ m_factory->lastSentHeaders = map;
+ }
+ return QNetworkAccessManager::createRequest(op, req, outgoingData);
+ }
+
+ QPointer<CustomNetworkAccessManagerFactory> m_factory;
+};
+
+QNetworkAccessManager *CustomNetworkAccessManagerFactory::create(QObject *parent)
+{
+ return new CustomNetworkAccessManager(this, parent);
+}
+
+void tst_QQmlXmlListModel::buildModel()
+{
+ QQmlComponent component(&engine, testFileUrl("model.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 9);
+
+ QModelIndex index = model->index(3, 0);
+ QCOMPARE(model->data(index, Qt::UserRole).toString(), QLatin1String("Spot"));
+ QCOMPARE(model->data(index, Qt::UserRole + 1).toString(), QLatin1String("Dog"));
+ QCOMPARE(model->data(index, Qt::UserRole + 2).toInt(), 9);
+ QCOMPARE(model->data(index, Qt::UserRole + 3).toString(), QLatin1String("Medium"));
+}
+
+void tst_QQmlXmlListModel::cdata()
+{
+ QQmlComponent component(&engine, testFileUrl("recipes.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 5);
+
+ QVERIFY(model->data(model->index(2, 0), Qt::UserRole + 2)
+ .toString()
+ .startsWith(QLatin1String("<html>")));
+}
+
+void tst_QQmlXmlListModel::attributes()
+{
+ QQmlComponent component(&engine, testFileUrl("attributes.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 4);
+
+ const QList<QVariantList> desiredResults = { QVariantList { "Polly", "Parrot", 12, "Small" },
+ QVariantList { "Penny", "Turtle", 4, "Small" },
+ QVariantList { "Spot", "Dog", 9, "Medium" },
+ QVariantList { "Tiny", "Elephant", 15, "Large" } };
+
+ QVERIFY(model->rowCount() == desiredResults.size());
+
+ for (qsizetype idx = 0; idx < model->rowCount(); ++idx) {
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole).toString(),
+ desiredResults.at(idx).at(0).toString());
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole + 1).toString(),
+ desiredResults.at(idx).at(1).toString());
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole + 2).toInt(),
+ desiredResults.at(idx).at(2).toInt());
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole + 3).toString(),
+ desiredResults.at(idx).at(3).toString());
+ }
+}
+
+void tst_QQmlXmlListModel::roles()
+{
+ QQmlComponent component(&engine, testFileUrl("model.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 9);
+
+ QHash<int, QByteArray> roleNames = model->roleNames();
+ QCOMPARE(roleNames.count(), 4);
+ QVERIFY(roleNames.key("name", -1) >= 0);
+ QVERIFY(roleNames.key("type", -1) >= 0);
+ QVERIFY(roleNames.key("age", -1) >= 0);
+ QVERIFY(roleNames.key("size", -1) >= 0);
+
+ QSet<int> roles;
+ roles.insert(roleNames.key("name"));
+ roles.insert(roleNames.key("type"));
+ roles.insert(roleNames.key("age"));
+ roles.insert(roleNames.key("size"));
+ QCOMPARE(roles.count(), 4);
+}
+
+void tst_QQmlXmlListModel::elementErrors()
+{
+ QQmlComponent component(&engine, testFileUrl("elementErrors.qml"));
+ QTest::ignoreMessage(QtWarningMsg,
+ (testFileUrl("elementErrors.qml").toString()
+ + ":6:5: QML XmlListModelRole: An xml element must not start with '/'")
+ .toUtf8()
+ .constData());
+ QTest::ignoreMessage(QtWarningMsg,
+ (testFileUrl("elementErrors.qml").toString()
+ + ":7:5: QML XmlListModelRole: An xml element must not end with '/'")
+ .toUtf8()
+ .constData());
+ QTest::ignoreMessage(QtWarningMsg,
+ (testFileUrl("elementErrors.qml").toString()
+ + ":8:5: QML XmlListModelRole: An xml element must not contain \"//\"")
+ .toUtf8()
+ .constData());
+
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 9);
+
+ QModelIndex index = model->index(3, 0);
+ QCOMPARE(model->data(index, Qt::UserRole).toString(), QString());
+ QCOMPARE(model->data(index, Qt::UserRole + 1).toString(), QString());
+ QCOMPARE(model->data(index, Qt::UserRole + 2).toString(), QString());
+}
+
+void tst_QQmlXmlListModel::uniqueRoleNames()
+{
+ QQmlComponent component(&engine, testFileUrl("unique.qml"));
+ QTest::ignoreMessage(QtWarningMsg,
+ (testFileUrl("unique.qml").toString()
+ + ":7:5: QML XmlListModelRole: \"name\" duplicates a previous role name "
+ "and will be disabled.")
+ .toUtf8()
+ .constData());
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 9);
+
+ QHash<int, QByteArray> roleNames = model->roleNames();
+ QCOMPARE(roleNames.count(), 1);
+}
+
+void tst_QQmlXmlListModel::headers()
+{
+ // ensure the QNetworkAccessManagers created for this test are immediately deleted
+ QQmlEngine qmlEng;
+
+ CustomNetworkAccessManagerFactory factory;
+ qmlEng.setNetworkAccessManagerFactory(&factory);
+
+ QQmlComponent component(&qmlEng, testFileUrl("model.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")),
+ QQmlXmlListModel::Ready);
+
+ // It doesn't do a network request for a local file
+ QCOMPARE(factory.lastSentHeaders.count(), 0);
+
+ model->setProperty("source", QUrl("http://localhost/filethatdoesnotexist.xml"));
+ QTRY_COMPARE_WITH_TIMEOUT(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")),
+ QQmlXmlListModel::Error, 10000);
+
+ QVariantMap expectedHeaders;
+ expectedHeaders["Accept"] = "application/xml,*/*";
+
+ QCOMPARE(factory.lastSentHeaders.count(), expectedHeaders.count());
+ for (auto it = expectedHeaders.cbegin(), end = expectedHeaders.cend(); it != end; ++it) {
+ QVERIFY(factory.lastSentHeaders.contains(it.key()));
+ QCOMPARE(factory.lastSentHeaders[it.key()].toString(), it.value().toString());
+ }
+}
+
+void tst_QQmlXmlListModel::source()
+{
+ QFETCH(QUrl, source);
+ QFETCH(int, count);
+ QFETCH(QQmlXmlListModel::Status, status);
+
+ QQmlComponent component(&engine, testFileUrl("model.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QSignalSpy spy(model.get(), SIGNAL(statusChanged(QQmlXmlListModel::Status)));
+
+ QVERIFY(errorString(model.get()).isEmpty());
+ QCOMPARE(model->property("progress").toDouble(), qreal(1.0));
+ QCOMPARE(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")),
+ QQmlXmlListModel::Loading);
+ QTRY_COMPARE(spy.count(), 1);
+ spy.clear();
+ QCOMPARE(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")),
+ QQmlXmlListModel::Ready);
+ QVERIFY(errorString(model.get()).isEmpty());
+ QCOMPARE(model->property("progress").toDouble(), qreal(1.0));
+ QCOMPARE(model->rowCount(), 9);
+
+ model->setProperty("source", source);
+ if (model->property("source").toString().isEmpty())
+ QCOMPARE(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")),
+ QQmlXmlListModel::Null);
+ QCOMPARE(model->property("progress").toDouble(), qreal(source.isLocalFile() ? 1.0 : 0.0));
+ QTRY_COMPARE(spy.count(), 1);
+ spy.clear();
+ QCOMPARE(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")),
+ QQmlXmlListModel::Loading);
+ QVERIFY(errorString(model.get()).isEmpty());
+
+ QEventLoop loop;
+ QTimer timer;
+ timer.setSingleShot(true);
+ connect(model.get(), SIGNAL(statusChanged(QQmlXmlListModel::Status)), &loop, SLOT(quit()));
+ connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
+ timer.start(20000);
+ loop.exec();
+
+ if (spy.count() == 0 && status != QQmlXmlListModel::Ready) {
+ qWarning("QQmlXmlListModel invalid source test timed out");
+ } else {
+ QCOMPARE(spy.count(), 1);
+ spy.clear();
+ }
+
+ QCOMPARE(qvariant_cast<QQmlXmlListModel::Status>(model->property("status")), status);
+ QCOMPARE(model->rowCount(), count);
+
+ if (status == QQmlXmlListModel::Ready)
+ QCOMPARE(model->property("progress").toDouble(), qreal(1.0));
+
+ QCOMPARE(errorString(model.get()).isEmpty(), status == QQmlXmlListModel::Ready);
+}
+
+void tst_QQmlXmlListModel::source_data()
+{
+ QTest::addColumn<QUrl>("source");
+ QTest::addColumn<int>("count");
+ QTest::addColumn<QQmlXmlListModel::Status>("status");
+
+ QTest::newRow("valid") << testFileUrl("model2.xml") << 2 << QQmlXmlListModel::Ready;
+ QTest::newRow("invalid") << QUrl("http://blah.blah/blah.xml") << 0 << QQmlXmlListModel::Error;
+
+ // empty file
+ QTemporaryFile *temp = new QTemporaryFile(this);
+ if (temp->open())
+ QTest::newRow("empty file")
+ << QUrl::fromLocalFile(temp->fileName()) << 0 << QQmlXmlListModel::Ready;
+ temp->close();
+}
+
+void tst_QQmlXmlListModel::data()
+{
+ QQmlComponent component(&engine, testFileUrl("model.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+
+ for (int i = 0; i < 9; i++) {
+ QModelIndex index = model->index(i, 0);
+ for (int j = 0; j < model->roleNames().count(); j++) {
+ QCOMPARE(model->data(index, j), QVariant());
+ }
+ }
+ QTRY_COMPARE(model->rowCount(), 9);
+}
+
+void tst_QQmlXmlListModel::reload()
+{
+ // If no keys are used, the model should be rebuilt from scratch when
+ // reload() is called.
+
+ QQmlComponent component(&engine, testFileUrl("model.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 9);
+
+ QSignalSpy spyInsert(model.get(), SIGNAL(rowsInserted(QModelIndex, int, int)));
+ QSignalSpy spyRemove(model.get(), SIGNAL(rowsRemoved(QModelIndex, int, int)));
+ QSignalSpy spyCount(model.get(), SIGNAL(countChanged()));
+ // reload multiple times to test the xml query aborting
+ QMetaObject::invokeMethod(model.get(), "reload");
+ QMetaObject::invokeMethod(model.get(), "reload");
+ QCoreApplication::processEvents();
+ QMetaObject::invokeMethod(model.get(), "reload");
+ QMetaObject::invokeMethod(model.get(), "reload");
+ QTRY_COMPARE(spyCount.count(), 0);
+ QTRY_COMPARE(spyInsert.count(), 1);
+ QTRY_COMPARE(spyRemove.count(), 1);
+
+ QCOMPARE(spyInsert[0][1].toInt(), 0);
+ QCOMPARE(spyInsert[0][2].toInt(), 8);
+
+ QCOMPARE(spyRemove[0][1].toInt(), 0);
+ QCOMPARE(spyRemove[0][2].toInt(), 8);
+}
+
+void tst_QQmlXmlListModel::threading()
+{
+ QFETCH(int, xmlDataCount);
+
+ QQmlComponent component(&engine, testFileUrl("threading.qml"));
+
+ QScopedPointer<QAbstractItemModel> m1(qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(m1 != nullptr);
+ QScopedPointer<QAbstractItemModel> m2(qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(m2 != nullptr);
+ QScopedPointer<QAbstractItemModel> m3(qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(m3 != nullptr);
+
+ for (int dataCount = 0; dataCount < xmlDataCount; ++dataCount) {
+ QString data1, data2, data3;
+ for (int i = 0; i < dataCount; ++i) {
+ data1 += "name=A" + QString::number(i) + ",age=1" + QString::number(i)
+ + ",sport=Football;";
+ data2 += "name=B" + QString::number(i) + ",age=2" + QString::number(i)
+ + ",sport=Athletics;";
+ data3 += "name=C" + QString::number(i) + ",age=3" + QString::number(i)
+ + ",sport=Curling;";
+ }
+
+ ScopedFile f1(testFileUrl("file1.xml"), makeItemXmlAndData(data1).toLatin1());
+ ScopedFile f2(testFileUrl("file2.xml"), makeItemXmlAndData(data2).toLatin1());
+ ScopedFile f3(testFileUrl("file3.xml"), makeItemXmlAndData(data3).toLatin1());
+ QVERIFY(f1.isCreated() && f2.isCreated() && f3.isCreated());
+
+ m1->setProperty("source", f1.fileUrl());
+ m2->setProperty("source", f2.fileUrl());
+ m3->setProperty("source", f3.fileUrl());
+ QCoreApplication::processEvents();
+
+ QTRY_VERIFY(m1->rowCount() == dataCount && m2->rowCount() == dataCount
+ && m3->rowCount() == dataCount);
+
+ for (int i = 0; i < dataCount; ++i) {
+ QModelIndex index = m1->index(i, 0);
+ QList<int> roles = m1->roleNames().keys();
+ std::sort(roles.begin(), roles.end());
+ QCOMPARE(m1->data(index, roles.at(0)).toString(),
+ QLatin1Char('A') + QString::number(i));
+ QCOMPARE(m1->data(index, roles.at(1)).toString(),
+ QLatin1Char('1') + QString::number(i));
+ QCOMPARE(m1->data(index, roles.at(2)).toString(), QString("Football"));
+
+ index = m2->index(i, 0);
+ roles = m2->roleNames().keys();
+ std::sort(roles.begin(), roles.end());
+ QCOMPARE(m2->data(index, roles.at(0)).toString(),
+ QLatin1Char('B') + QString::number(i));
+ QCOMPARE(m2->data(index, roles.at(1)).toString(),
+ QLatin1Char('2') + QString::number(i));
+ QCOMPARE(m2->data(index, roles.at(2)).toString(), QString("Athletics"));
+
+ index = m3->index(i, 0);
+ roles = m3->roleNames().keys();
+ std::sort(roles.begin(), roles.end());
+ QCOMPARE(m3->data(index, roles.at(0)).toString(),
+ QLatin1Char('C') + QString::number(i));
+ QCOMPARE(m3->data(index, roles.at(1)).toString(),
+ QLatin1Char('3') + QString::number(i));
+ QCOMPARE(m3->data(index, roles.at(2)).toString(), QString("Curling"));
+ }
+
+ // clear sources, so that we could reuse same file names later
+ m1->setProperty("source", QUrl());
+ m2->setProperty("source", QUrl());
+ m3->setProperty("source", QUrl());
+ }
+}
+
+void tst_QQmlXmlListModel::threading_data()
+{
+ QTest::addColumn<int>("xmlDataCount");
+
+ QTest::newRow("1") << 1;
+ QTest::newRow("2") << 2;
+ QTest::newRow("10") << 10;
+}
+
+void tst_QQmlXmlListModel::propertyChanges()
+{
+ QQmlComponent component(&engine, testFileUrl("propertychanges.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 9);
+
+ QObject *role = model->findChild<QObject *>("role");
+ QVERIFY(role);
+
+ QSignalSpy nameSpy(role, SIGNAL(nameChanged()));
+ QSignalSpy elementSpy(role, SIGNAL(elementNameChanged()));
+
+ role->setProperty("name", "size");
+ role->setProperty("elementName", "size");
+
+ QCOMPARE(role->property("name").toString(), QString("size"));
+ QCOMPARE(role->property("elementName").toString(), QString("size"));
+
+ QCOMPARE(nameSpy.count(), 1);
+ QCOMPARE(elementSpy.count(), 1);
+
+ role->setProperty("name", "size");
+ role->setProperty("elementName", "size");
+
+ QCOMPARE(nameSpy.count(), 1);
+ QCOMPARE(elementSpy.count(), 1);
+
+ QSignalSpy sourceSpy(model.get(), SIGNAL(sourceChanged()));
+ QSignalSpy modelQuerySpy(model.get(), SIGNAL(queryChanged()));
+
+ model->setProperty("source", QUrl("model2.xml"));
+ model->setProperty("query", "/Pets");
+
+ QCOMPARE(model->property("source").toUrl(), QUrl("model2.xml"));
+ QCOMPARE(model->property("query").toString(), QString("/Pets"));
+
+ QTRY_COMPARE(model->rowCount(), 1);
+
+ QCOMPARE(sourceSpy.count(), 1);
+ QCOMPARE(modelQuerySpy.count(), 1);
+
+ model->setProperty("source", QUrl("model2.xml"));
+ model->setProperty("query", "/Pets");
+
+ QCOMPARE(sourceSpy.count(), 1);
+ QCOMPARE(modelQuerySpy.count(), 1);
+
+ QTRY_COMPARE(model->rowCount(), 1);
+}
+
+void tst_QQmlXmlListModel::nestedElements()
+{
+ QQmlComponent component(&engine, testFileUrl("nestedElements.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ QTRY_COMPARE(model->rowCount(), 4);
+
+ const QList<QVariantList> desiredResults = { QVariantList { "Polly", "Parrot", 12, "Small" },
+ QVariantList { "Penny", "Turtle", 4, "Small" },
+ QVariantList { "Spot", "Dog", 9, "Medium" },
+ QVariantList { "Tiny", "Elephant", 15, "Large" } };
+
+ QVERIFY(model->rowCount() == desiredResults.size());
+
+ for (qsizetype idx = 0; idx < model->rowCount(); ++idx) {
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole).toString(),
+ desiredResults.at(idx).at(0).toString());
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole + 1).toString(),
+ desiredResults.at(idx).at(1).toString());
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole + 2).toInt(),
+ desiredResults.at(idx).at(2).toInt());
+ QCOMPARE(model->data(model->index(idx, 0), Qt::UserRole + 3).toString(),
+ desiredResults.at(idx).at(3).toString());
+ }
+}
+
+void tst_QQmlXmlListModel::malformedData()
+{
+ QFETCH(QUrl, fileName);
+ QFETCH(QString, errorMessage);
+
+ // In this test we check that malformed xml document would not cause
+ // infinite loop while parsing, and that the errors will be reported.
+
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ (testFileUrl("malformedData.qml").toString() + errorMessage).toUtf8().constData());
+
+ QQmlComponent component(&engine, testFileUrl("malformedData.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+ model->setProperty("source", fileName);
+ QTRY_VERIFY(model->rowCount() != 0);
+}
+
+void tst_QQmlXmlListModel::malformedData_data()
+{
+ QTest::addColumn<QUrl>("fileName");
+ QTest::addColumn<QString>("errorMessage");
+
+ QTest::addRow("tag mismatch top level")
+ << testFileUrl("malformedTagTopLevel.xml")
+ << QStringLiteral(
+ ":3:1: QML XmlListModel: Query error: \"Opening and ending tag mismatch.\"");
+ QTest::addRow("missing tag nested level")
+ << testFileUrl("malformedTagNestedLevel.xml")
+ << QStringLiteral(":3:1: QML XmlListModel: Query error: \"Expected character data.\"");
+ QTest::addRow("invalid attribute name")
+ << testFileUrl("malformedAttribute.xml")
+ << QStringLiteral(":3:1: QML XmlListModel: Query error: \"Expected '>' or '/', but got "
+ "'[0-9]'.\"");
+}
+
+void tst_QQmlXmlListModel::roleCrash()
+{
+ // don't crash
+ QQmlComponent component(&engine, testFileUrl("roleCrash.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+}
+
+class SortFilterProxyModel : public QSortFilterProxyModel
+{
+ Q_OBJECT
+ Q_PROPERTY(QObject *source READ source WRITE setSource)
+
+public:
+ SortFilterProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) { sort(0); }
+ QObject *source() const { return sourceModel(); }
+ void setSource(QObject *source) { setSourceModel(qobject_cast<QAbstractItemModel *>(source)); }
+};
+
+void tst_QQmlXmlListModel::proxyCrash()
+{
+ qmlRegisterType<SortFilterProxyModel>("SortFilterProxyModel", 1, 0, "SortFilterProxyModel");
+
+ // don't crash
+ QQmlComponent component(&engine, testFileUrl("proxyCrash.qml"));
+ QScopedPointer<QAbstractItemModel> model(
+ qobject_cast<QAbstractItemModel *>(component.create()));
+ QVERIFY(model != nullptr);
+}
+
+QTEST_MAIN(tst_QQmlXmlListModel)
+
+#include "tst_qqmlxmllistmodel.moc"