diff options
-rw-r--r-- | src/imports/testlib/TestCase.qml | 32 | ||||
-rw-r--r-- | tests/auto/qmltest/selftests/tst_findChild.qml | 37 |
2 files changed, 66 insertions, 3 deletions
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index 8d9f6822f4..700c9112eb 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -633,17 +633,45 @@ Item { \since 5.4 \qmlmethod QtObject TestCase::findChild(parent, objectName) - Returns the first child of \a parent with \a objectName, - or \c null if no such item exists. Children are searched recursively. + Returns the first child of \a parent with \a objectName, or \c null if + no such item exists. Both visual and non-visual children are searched + recursively, with visual children being searched first. \code compare(findChild(item, "childObject"), expectedChildObject); \endcode */ function findChild(parent, objectName) { + // First, search the visual item hierarchy. + var child = qtest_findVisualChild(parent, objectName); + if (child) + return child; + + // If it's not a visual child, it might be a QObject child. return qtest_results.findChild(parent, objectName); } + /*! \internal */ + function qtest_findVisualChild(parent, objectName) { + if (!parent || parent.children === undefined) + return null; + + for (var i = 0; i < parent.children.length; ++i) { + // Is this direct child of ours the child we're after? + var child = parent.children[i]; + if (child.objectName === objectName) + return child; + } + + for (i = 0; i < parent.children.length; ++i) { + // Try the direct child's children. + child = qtest_findVisualChild(parent.children[i], objectName); + if (child) + return child; + } + return null; + } + /*! \qmlmethod TestCase::tryCompare(obj, property, expected, timeout = 5000, message = "") diff --git a/tests/auto/qmltest/selftests/tst_findChild.qml b/tests/auto/qmltest/selftests/tst_findChild.qml index 1ed4c94423..69bcb390e9 100644 --- a/tests/auto/qmltest/selftests/tst_findChild.qml +++ b/tests/auto/qmltest/selftests/tst_findChild.qml @@ -82,6 +82,33 @@ TestCase { objectName: "nestedChildItem2" } + Loader { + id: loader + + sourceComponent: Item { + id: loaderItem + objectName: "loaderItem" + + Item { + objectName: "nestedLoaderItem" + } + + Repeater { + model: 5 + delegate: Item { + objectName: "repeaterItem" + index + } + } + + ListView { + model: 5 + delegate: Item { + objectName: "listViewItem" + index + } + } + } + } + function test_findChild() { compare(findChild(null, ""), null); compare(findChild(undefined, ""), null); @@ -99,6 +126,14 @@ TestCase { var mostDirectChild = duplicateNestedChildItem2Component.createObject(nestedChildItem0); compare(nestedChildItem0.children.length, 2); - compare(findChild(nestedChildrenItem, "nestedChildItem2"), mostDirectChild); + compare(findChild(nestedChildrenItem, "nestedChildItem2"), mostDirectChild, + "Dynamically created nested child items are found"); + + compare(findChild(loader, "loaderItem"), loader.item); + verify(findChild(loader, "nestedLoaderItem")); + + // These don't make their delegate items QObject children, only visual. + verify(findChild(loader, "repeaterItem0")); + verify(findChild(loader, "listViewItem0")); } } |