From ce9b9692109f16b9173cb527a5673e03cd933407 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 5 Dec 2011 10:59:29 +1000 Subject: [PATCH] Add tests for snapOneItem/snapOneRow snapMode Change-Id: Ia7e8fe6a7da48d8f67914d90377c6fad32ecb415 Reviewed-by: Andrew den Exter Reviewed-by: Martin Jones --- .../qtquick2/qquickgridview/data/snapOneRow.qml | 49 +++++++++ .../qtquick2/qquickgridview/tst_qquickgridview.cpp | 114 +++++++++++++++++++++ .../qtquick2/qquicklistview/data/snapOneItem.qml | 49 +++++++++ .../qtquick2/qquicklistview/tst_qquicklistview.cpp | 113 ++++++++++++++++++++ 4 files changed, 325 insertions(+) create mode 100644 tests/auto/qtquick2/qquickgridview/data/snapOneRow.qml create mode 100644 tests/auto/qtquick2/qquicklistview/data/snapOneItem.qml diff --git a/tests/auto/qtquick2/qquickgridview/data/snapOneRow.qml b/tests/auto/qtquick2/qquickgridview/data/snapOneRow.qml new file mode 100644 index 0000000..3d32d75 --- /dev/null +++ b/tests/auto/qtquick2/qquickgridview/data/snapOneRow.qml @@ -0,0 +1,49 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 240 + height: 240 + color: "#ffffff" + + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + height: 120 + width: 120 + Column { + Text { + text: index + } + Text { + text: wrapper.x + ", " + wrapper.y + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "transparent" + } + } + GridView { + id: grid + objectName: "grid" + anchors.fill: parent + cellWidth: 120 + cellHeight: 120 + preferredHighlightBegin: 20 + preferredHighlightEnd: 140 + snapMode: GridView.SnapOneRow + layoutDirection: Qt.RightToLeft + flow: GridView.TopToBottom + highlightRangeMode: GridView.StrictlyEnforceRange + highlight: Rectangle { width: 120; height: 120; color: "yellow" } + model: 10 + delegate: myDelegate + } + + Text { + anchors.right: parent.right + anchors.bottom: parent.bottom + text: grid.contentX + ", " + grid.contentY + } +} diff --git a/tests/auto/qtquick2/qquickgridview/tst_qquickgridview.cpp b/tests/auto/qtquick2/qquickgridview/tst_qquickgridview.cpp index cc0f7b7..241e2e9 100644 --- a/tests/auto/qtquick2/qquickgridview/tst_qquickgridview.cpp +++ b/tests/auto/qtquick2/qquickgridview/tst_qquickgridview.cpp @@ -115,6 +115,8 @@ private slots: void creationContext(); void snapToRow_data(); void snapToRow(); + void snapOneRow_data(); + void snapOneRow(); void unaligned(); void cacheBuffer(); void asynchronous(); @@ -3400,6 +3402,118 @@ void tst_QQuickGridView::snapToRow() delete canvas; } +void tst_QQuickGridView::snapOneRow_data() +{ + QTest::addColumn("flow"); + QTest::addColumn("layoutDirection"); + QTest::addColumn("highlightRangeMode"); + QTest::addColumn("flickStart"); + QTest::addColumn("flickEnd"); + QTest::addColumn("snapAlignment"); + QTest::addColumn("endExtent"); + QTest::addColumn("startExtent"); + + QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) + << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 360.0 << 0.0; + + QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) + << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 360.0 << 0.0; + + QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange) + << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -360.0 - 240.0 << -240.0; + + QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 460.0 << -20.0; + + QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 460.0 << -20.0; + + QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -360.0 - 240.0 - 100.0 << -220.0; +} + +void tst_QQuickGridView::snapOneRow() +{ + QFETCH(QQuickGridView::Flow, flow); + QFETCH(Qt::LayoutDirection, layoutDirection); + QFETCH(int, highlightRangeMode); + QFETCH(QPoint, flickStart); + QFETCH(QPoint, flickEnd); + QFETCH(qreal, snapAlignment); + QFETCH(qreal, endExtent); + QFETCH(qreal, startExtent); + + QQuickView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(TESTDATA("snapOneRow.qml"))); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + gridview->setFlow(flow); + gridview->setLayoutDirection(layoutDirection); + gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode)); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged())); + + // confirm that a flick hits next row boundary + flick(canvas, flickStart, flickEnd, 180); + QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops + if (flow == QQuickGridView::LeftToRight) + QCOMPARE(gridview->contentY(), snapAlignment); + else + QCOMPARE(gridview->contentX(), snapAlignment); + + if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { + QCOMPARE(gridview->currentIndex(), 2); + QCOMPARE(currentIndexSpy.count(), 1); + } + + // flick to end + do { + flick(canvas, flickStart, flickEnd, 180); + QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops + } while (flow == QQuickGridView::LeftToRight + ? !gridview->isAtYEnd() + : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning()); + + if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { + QCOMPARE(gridview->currentIndex(), 8); + QCOMPARE(currentIndexSpy.count(), 4); + } + + if (flow == QQuickGridView::LeftToRight) + QCOMPARE(gridview->contentY(), endExtent); + else + QCOMPARE(gridview->contentX(), endExtent); + + // flick to start + do { + flick(canvas, flickEnd, flickStart, 180); + QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops + } while (flow == QQuickGridView::LeftToRight + ? !gridview->isAtYBeginning() + : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd()); + + if (flow == QQuickGridView::LeftToRight) + QCOMPARE(gridview->contentY(), startExtent); + else + QCOMPARE(gridview->contentX(), startExtent); + + if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { + QCOMPARE(gridview->currentIndex(), 0); + QCOMPARE(currentIndexSpy.count(), 8); + } + + delete canvas; +} + + void tst_QQuickGridView::unaligned() { QQuickView *canvas = createView(); diff --git a/tests/auto/qtquick2/qquicklistview/data/snapOneItem.qml b/tests/auto/qtquick2/qquicklistview/data/snapOneItem.qml new file mode 100644 index 0000000..d67d804 --- /dev/null +++ b/tests/auto/qtquick2/qquicklistview/data/snapOneItem.qml @@ -0,0 +1,49 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 240 + height: 240 + color: "#ffffff" + + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + height: 200 + width: 200 + Column { + Text { + text: index + } + Text { + text: wrapper.x + ", " + wrapper.y + } + } + color: ListView.isCurrentItem ? "lightsteelblue" : "transparent" + } + } + ListView { + id: list + objectName: "list" + anchors.fill: parent + preferredHighlightBegin: 20 + preferredHighlightEnd: 220 + snapMode: ListView.SnapOneItem + orientation: ListView.Horizontal + layoutDirection: Qt.RightToLeft + highlightRangeMode: ListView.StrictlyEnforceRange +// highlightRangeMode: ListView.NoHighlightRange + highlight: Rectangle { width: 200; height: 200; color: "yellow" } + flickDeceleration: 200 // encourages long flick + model: 4 + delegate: myDelegate + } + + Text { + anchors.right: parent.right + anchors.bottom: parent.bottom + text: list.contentX + ", " + list.contentY + } +} diff --git a/tests/auto/qtquick2/qquicklistview/tst_qquicklistview.cpp b/tests/auto/qtquick2/qquicklistview/tst_qquicklistview.cpp index 780033b..ea10523 100644 --- a/tests/auto/qtquick2/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/qtquick2/qquicklistview/tst_qquicklistview.cpp @@ -143,6 +143,8 @@ private slots: void creationContext(); void snapToItem_data(); void snapToItem(); + void snapOneItem_data(); + void snapOneItem(); void QTBUG_9791(); void QTBUG_11105(); @@ -4299,6 +4301,117 @@ void tst_QQuickListView::asynchronous() delete canvas; } +void tst_QQuickListView::snapOneItem_data() +{ + QTest::addColumn("orientation"); + QTest::addColumn("layoutDirection"); + QTest::addColumn("highlightRangeMode"); + QTest::addColumn("flickStart"); + QTest::addColumn("flickEnd"); + QTest::addColumn("snapAlignment"); + QTest::addColumn("endExtent"); + QTest::addColumn("startExtent"); + + QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) + << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0; + + QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) + << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0; + + QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange) + << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0; + + QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0; + + QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0; + + QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0; +} + +void tst_QQuickListView::snapOneItem() +{ + QFETCH(QQuickListView::Orientation, orientation); + QFETCH(Qt::LayoutDirection, layoutDirection); + QFETCH(int, highlightRangeMode); + QFETCH(QPoint, flickStart); + QFETCH(QPoint, flickEnd); + QFETCH(qreal, snapAlignment); + QFETCH(qreal, endExtent); + QFETCH(qreal, startExtent); + + QQuickView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(TESTDATA("snapOneItem.qml"))); + canvas->show(); + qApp->processEvents(); + + QQuickListView *listview = findItem(canvas->rootObject(), "list"); + QTRY_VERIFY(listview != 0); + + listview->setOrientation(orientation); + listview->setLayoutDirection(layoutDirection); + listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode)); + + QQuickItem *contentItem = listview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged())); + + // confirm that a flick hits the next item boundary + flick(canvas, flickStart, flickEnd, 180); + QTRY_VERIFY(listview->isMoving() == false); // wait until it stops + if (orientation == QQuickListView::Vertical) + QCOMPARE(listview->contentY(), snapAlignment); + else + QCOMPARE(listview->contentX(), snapAlignment); + + if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { + QCOMPARE(listview->currentIndex(), 1); + QCOMPARE(currentIndexSpy.count(), 1); + } + + // flick to end + do { + flick(canvas, flickStart, flickEnd, 180); + QTRY_VERIFY(listview->isMoving() == false); // wait until it stops + } while (orientation == QQuickListView::Vertical + ? !listview->isAtYEnd() + : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning()); + + if (orientation == QQuickListView::Vertical) + QCOMPARE(listview->contentY(), endExtent); + else + QCOMPARE(listview->contentX(), endExtent); + + if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { + QCOMPARE(listview->currentIndex(), 3); + QCOMPARE(currentIndexSpy.count(), 3); + } + + // flick to start + do { + flick(canvas, flickEnd, flickStart, 180); + QTRY_VERIFY(listview->isMoving() == false); // wait until it stops + } while (orientation == QQuickListView::Vertical + ? !listview->isAtYBeginning() + : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd()); + + if (orientation == QQuickListView::Vertical) + QCOMPARE(listview->contentY(), startExtent); + else + QCOMPARE(listview->contentX(), startExtent); + + if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { + QCOMPARE(listview->currentIndex(), 0); + QCOMPARE(currentIndexSpy.count(), 6); + } + + delete canvas; +} + QQuickItem *tst_QQuickListView::findVisibleChild(QQuickItem *parent, const QString &objectName) { QQuickItem *item = 0; -- 2.7.4