diff options
author | Mitch Curtis <mitch.curtis@theqtcompany.com> | 2015-04-08 10:04:25 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-04-08 14:32:41 +0000 |
commit | 360fcf3a99d84b038326d05f8ea8718b40bff2c0 (patch) | |
tree | cf1bd655ff9aa7c439cc7281105066def24a880c /tests/auto/quick/qquickitem | |
parent | 1a4719dd0d6c565fa97e6962c3686123ec02fd00 (diff) |
Return the correct type from Item::mapToItem/Item::mapFromItem.
Previously we were returning a JavaScript object with x/y/width/height
properties, instead of a point/rect. This meant that the type couldn't
be converted to a point/rect because we don't support duck typing,
where we would deduce the type based on the properties.
One example of a broken use case that this patch fixes is when QML is
unable to convert the return type to a point in a property declaration:
property point p: mouseArea.mapToItem(child, mouseArea.mouseX, mouseArea.mouseY)
Another is using the result of the function to pass to another function:
child.contains(mouseArea.mapToItem(child, mouseArea.mouseX, mouseArea.mouseY))
Change-Id: I3ce82f10175f904dd02c8af6b5e42cee14b2ebb2
Task-number: QTBUG-41452
Reviewed-by: Liang Qi <liang.qi@theqtcompany.com>
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'tests/auto/quick/qquickitem')
-rw-r--r-- | tests/auto/quick/qquickitem/data/contains.qml | 26 | ||||
-rw-r--r-- | tests/auto/quick/qquickitem/tst_qquickitem.cpp | 45 |
2 files changed, 71 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickitem/data/contains.qml b/tests/auto/quick/qquickitem/data/contains.qml new file mode 100644 index 0000000000..72fd8bdc91 --- /dev/null +++ b/tests/auto/quick/qquickitem/data/contains.qml @@ -0,0 +1,26 @@ +import QtQuick 2.3 + +Item { + id: root + width: 300 + height: 300 + + function childContainsViaMapToItem(x, y) { + var childLocalPos = root.mapToItem(child, x, y); + return child.contains(childLocalPos); + } + + function childContainsViaMapFromItem(x, y) { + var childLocalPos = child.mapFromItem(root, x, y); + return child.contains(childLocalPos); + } + + Item { + id: child + x: 50 + y: 50 + width: 50 + height: 50 + } +} + diff --git a/tests/auto/quick/qquickitem/tst_qquickitem.cpp b/tests/auto/quick/qquickitem/tst_qquickitem.cpp index 832cbab971..c79af91747 100644 --- a/tests/auto/quick/qquickitem/tst_qquickitem.cpp +++ b/tests/auto/quick/qquickitem/tst_qquickitem.cpp @@ -169,6 +169,9 @@ private slots: void objectChildTransform(); + void contains_data(); + void contains(); + private: enum PaintOrderOp { @@ -1927,6 +1930,48 @@ void tst_qquickitem::objectChildTransform() // Shouldn't crash. } +void tst_qquickitem::contains_data() +{ + QTest::addColumn<int>("x"); + QTest::addColumn<int>("y"); + QTest::addColumn<bool>("contains"); + + QTest::newRow("(0, 0) = false") << 0 << 0 << false; + QTest::newRow("(50, 0) = false") << 50 << 0 << false; + QTest::newRow("(0, 50) = false") << 0 << 50 << false; + QTest::newRow("(50, 50) = true") << 50 << 50 << true; + QTest::newRow("(100, 100) = true") << 100 << 100 << true; + QTest::newRow("(150, 150) = false") << 150 << 150 << false; +} + +void tst_qquickitem::contains() +{ + // Tests that contains works, but also checks that mapToItem/mapFromItem + // return the correct type (point or rect, not a JS object with those properties), + // as this is a common combination of calls. + + QFETCH(int, x); + QFETCH(int, y); + QFETCH(bool, contains); + + QQuickView view; + view.setSource(testFileUrl("contains.qml")); + + QQuickItem *root = qobject_cast<QQuickItem*>(view.rootObject()); + QVERIFY(root); + + QVariant result = false; + QVERIFY(QMetaObject::invokeMethod(root, "childContainsViaMapToItem", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, qreal(x)), Q_ARG(QVariant, qreal(y)))); + QCOMPARE(result.toBool(), contains); + + result = false; + QVERIFY(QMetaObject::invokeMethod(root, "childContainsViaMapFromItem", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, qreal(x)), Q_ARG(QVariant, qreal(y)))); + QCOMPARE(result.toBool(), contains); +} + + QTEST_MAIN(tst_qquickitem) #include "tst_qquickitem.moc" |