diff options
Diffstat (limited to 'tests/auto/quick/qquickgridview')
31 files changed, 6684 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickgridview/data/ComponentView.qml b/tests/auto/quick/qquickgridview/data/ComponentView.qml new file mode 100644 index 0000000000..12ab6c92d1 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/ComponentView.qml @@ -0,0 +1,14 @@ +import QtQuick 2.0 + +GridView { + id: view + + property string title + + width: 100; height: 100; + + model: 1 + delegate: Text { objectName: "listItem"; text: view.title } + header: Text { objectName: "header"; text: view.title } + footer: Text { objectName: "footer"; text: view.title } +} diff --git a/tests/auto/quick/qquickgridview/data/addTransitions.qml b/tests/auto/quick/qquickgridview/data/addTransitions.qml new file mode 100644 index 0000000000..3f47cbd7b7 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/addTransitions.qml @@ -0,0 +1,129 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 550 + height: 600 + + property int duration: 10 + property int count: grid.count + + Component { + id: myDelegate + + Rectangle { + id: wrapper + + property string nameData: name + + objectName: "wrapper" + width: 80 + height: 60 + border.width: 1 + Column { + Text { text: index } + Text { + text: wrapper.x + ", " + wrapper.y + } + Text { + id: textName + objectName: "textName" + text: name + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + + onXChanged: checkPos() + onYChanged: checkPos() + + function checkPos() { + if (Qt.point(x, y) == targetItems_transitionFrom) + model_targetItems_transitionFrom.addItem(name, "") + if (Qt.point(x, y) == displacedItems_transitionVia) + model_displacedItems_transitionVia.addItem(name, "") + } + } + } + + GridView { + id: grid + + property int targetTransitionsDone + property int displaceTransitionsDone + + property var targetTrans_items: new Object() + property var targetTrans_targetIndexes: new Array() + property var targetTrans_targetItems: new Array() + + property var displacedTrans_items: new Object() + property var displacedTrans_targetIndexes: new Array() + property var displacedTrans_targetItems: new Array() + + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + anchors.centerIn: parent + model: testModel + delegate: myDelegate + + // for QQmlListProperty types + function copyList(propList) { + var temp = new Array() + for (var i=0; i<propList.length; i++) + temp.push(propList[i]) + return temp + } + + add: Transition { + id: targetTransition + + SequentialAnimation { + ScriptAction { + script: { + grid.targetTrans_items[targetTransition.ViewTransition.item.nameData] = targetTransition.ViewTransition.index + grid.targetTrans_targetIndexes.push(targetTransition.ViewTransition.targetIndexes) + grid.targetTrans_targetItems.push(grid.copyList(targetTransition.ViewTransition.targetItems)) + } + } + ParallelAnimation { + NumberAnimation { properties: "x"; from: targetItems_transitionFrom.x; duration: root.duration } + NumberAnimation { properties: "y"; from: targetItems_transitionFrom.y; duration: root.duration } + } + + ScriptAction { script: grid.targetTransitionsDone += 1 } + } + } + + addDisplaced: Transition { + id: displaced + + SequentialAnimation { + ScriptAction { + script: { + grid.displacedTrans_items[displaced.ViewTransition.item.nameData] = displaced.ViewTransition.index + grid.displacedTrans_targetIndexes.push(displaced.ViewTransition.targetIndexes) + grid.displacedTrans_targetItems.push(grid.copyList(displaced.ViewTransition.targetItems)) + } + } + ParallelAnimation { + NumberAnimation { properties: "x"; duration: root.duration; to: displacedItems_transitionVia.x } + NumberAnimation { properties: "y"; duration: root.duration; to: displacedItems_transitionVia.y } + } + NumberAnimation { properties: "x,y"; duration: root.duration } + + ScriptAction { script: grid.displaceTransitionsDone += 1 } + } + + } + } + + Rectangle { + anchors.fill: grid + color: "lightsteelblue" + opacity: 0.2 + } +} + + diff --git a/tests/auto/quick/qquickgridview/data/asyncloader.qml b/tests/auto/quick/qquickgridview/data/asyncloader.qml new file mode 100644 index 0000000000..ab66f20a1e --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/asyncloader.qml @@ -0,0 +1,36 @@ + +import QtQuick 2.0 + +Rectangle { + id: root + width: 300; height: 400 + color: "#2200FF00" + + Loader { + asynchronous: true + sourceComponent: viewComp + anchors.fill: parent + } + + Component { + id: viewComp + GridView { + objectName: "view" + width: 300; height: 400 + model: 40 + delegate: aDelegate + + highlight: Rectangle { color: "lightsteelblue" } + } + } + // The delegate for each list + Component { + id: aDelegate + Item { + objectName: "wrapper" + width: 100 + height: 100 + Text { text: 'Index: ' + index } + } + } +} diff --git a/tests/auto/quick/qquickgridview/data/attachedSignals.qml b/tests/auto/quick/qquickgridview/data/attachedSignals.qml new file mode 100644 index 0000000000..73c10d8caf --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/attachedSignals.qml @@ -0,0 +1,27 @@ +import QtQuick 2.0 + +GridView { + id: view + width: 240; height: 320 + + property variant addedDelegates: [] + property int removedDelegateCount + + model: testModel + + cellWidth: delegateWidth; cellHeight: delegateHeight + + delegate: Rectangle { + width: delegateWidth; height: delegateHeight + border.width: 1 + GridView.onAdd: { + var obj = GridView.view.addedDelegates + obj.push(model.name) + GridView.view.addedDelegates = obj + } + GridView.onRemove: { + view.removedDelegateCount += 1 + } + } +} + diff --git a/tests/auto/quick/qquickgridview/data/creationContext.qml b/tests/auto/quick/qquickgridview/data/creationContext.qml new file mode 100644 index 0000000000..79a682788b --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/creationContext.qml @@ -0,0 +1,5 @@ +import QtQuick 2.0 + +ComponentView { + title: "Hello!" +} diff --git a/tests/auto/quick/qquickgridview/data/displaygrid.qml b/tests/auto/quick/qquickgridview/data/displaygrid.qml new file mode 100644 index 0000000000..1da4fe50ac --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/displaygrid.qml @@ -0,0 +1,39 @@ +import QtQuick 2.0 + +Rectangle { + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.color: "blue" + Text { + text: index + } + Text { + y: 20 + id: displayText + objectName: "displayText" + text: display + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + ] + GridView { + id: grid + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + model: testModel + delegate: myDelegate + } +} diff --git a/tests/auto/quick/qquickgridview/data/footer.qml b/tests/auto/quick/qquickgridview/data/footer.qml new file mode 100644 index 0000000000..9083f9f57c --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/footer.qml @@ -0,0 +1,48 @@ +import QtQuick 2.0 + +Rectangle { + property bool showHeader: false + + function changeFooter() { + grid.footer = footer2 + } + width: 240 + height: 320 + color: "#ffffff" + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.color: "blue" + Text { + text: index + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + Component { + id: headerComponent + Text { objectName: "header"; text: "Header " + x + "," + y; width: 100; height: 30 } + } + + GridView { + id: grid + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + model: testModel + delegate: myDelegate + header: parent.showHeader ? headerComponent : null + footer: Text { objectName: "footer"; text: "Footer " + x + "," + y; width: 100; height: 30 } + } + + Component { + id: footer2 + Text { objectName: "footer2"; text: "Footer 2" + x + "," + y; width: 50; height: 20 } + } +} diff --git a/tests/auto/quick/qquickgridview/data/gridview-enforcerange.qml b/tests/auto/quick/qquickgridview/data/gridview-enforcerange.qml new file mode 100644 index 0000000000..2bfe7da78e --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview-enforcerange.qml @@ -0,0 +1,58 @@ +import QtQuick 2.0 + +Rectangle { + width: 240 + height: 320 + color: "#ffffff" + Component { + id: myDelegate + Item { + id: wrapper + objectName: "wrapper" + height: 100 + width: 100 + Text { + text: index + } + Text { + y: 25 + id: textName + objectName: "textName" + text: name + } + Text { + y: 50 + id: textNumber + objectName: "textNumber" + text: number + } + Text { + y: 75 + text: wrapper.y + } + } + } + + Component { + id: myHighlight + Rectangle { + color: "lightsteelblue" + } + } + + GridView { + id: grid + objectName: "grid" + width: 240 + height: 320 + model: testModel + delegate: myDelegate + highlight: myHighlight + flow: (testTopToBottom == true) ? GridView.TopToBottom : GridView.LeftToRight + layoutDirection: (testRightToLeft == true) ? Qt.RightToLeft : Qt.LeftToRight + preferredHighlightBegin: 100 + preferredHighlightEnd: 100 + highlightRangeMode: "StrictlyEnforceRange" + focus: true + } +} diff --git a/tests/auto/quick/qquickgridview/data/gridview-initCurrent.qml b/tests/auto/quick/qquickgridview/data/gridview-initCurrent.qml new file mode 100644 index 0000000000..624f639962 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview-initCurrent.qml @@ -0,0 +1,66 @@ +import QtQuick 2.0 + +Rectangle { + id: root + + property int current: grid.currentIndex + property bool showHeader: false + property bool showFooter: false + + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.color: "blue" + Text { + text: index + } + Text { + x: 40 + text: wrapper.x + ", " + wrapper.y + } + Text { + y: 20 + id: textName + objectName: "textName" + text: name + } + Text { + y: 40 + id: textNumber + objectName: "textNumber" + text: number + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + ] + + Component { + id: headerFooter + Rectangle { height: 30; width: 240; color: "blue" } + } + + GridView { + id: grid + objectName: "grid" + focus: true + width: 240 + height: 320 + currentIndex: 35 + cellWidth: 80 + cellHeight: 60 + delegate: myDelegate + highlightMoveDuration: 400 + model: testModel + header: root.showHeader ? headerFooter : null + footer: root.showFooter ? headerFooter : null + } +} diff --git a/tests/auto/quick/qquickgridview/data/gridview-noCurrent.qml b/tests/auto/quick/qquickgridview/data/gridview-noCurrent.qml new file mode 100644 index 0000000000..600716e2d4 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview-noCurrent.qml @@ -0,0 +1,52 @@ +import QtQuick 2.0 + +Rectangle { + property int current: grid.currentIndex + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.color: "blue" + Text { + text: index + } + Text { + x: 40 + text: wrapper.x + ", " + wrapper.y + } + Text { + y: 20 + id: textName + objectName: "textName" + text: name + } + Text { + y: 40 + id: textNumber + objectName: "textNumber" + text: number + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + ] + GridView { + id: grid + objectName: "grid" + focus: true + width: 240 + height: 320 + currentIndex: -1 + cellWidth: 80 + cellHeight: 60 + delegate: myDelegate + model: testModel + } +} diff --git a/tests/auto/quick/qquickgridview/data/gridview1.qml b/tests/auto/quick/qquickgridview/data/gridview1.qml new file mode 100644 index 0000000000..4bf6f0b952 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview1.qml @@ -0,0 +1,69 @@ +import QtQuick 2.0 + +Rectangle { + id: root + property int count: grid.count + property bool showHeader: false + property bool showFooter: false + property real cacheBuffer: 0 + property int added: -1 + property variant removed + + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.color: "blue" + property string name: model.name + Text { + text: index + } + Text { + x: 40 + text: wrapper.x + ", " + wrapper.y + } + Text { + y: 20 + id: textName + objectName: "textName" + text: name + } + Text { + y: 40 + id: textNumber + objectName: "textNumber" + text: number + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + GridView.onAdd: root.added = index + GridView.onRemove: root.removed = name + } + }, + Component { + id: headerFooter + Rectangle { width: 30; height: 320; color: "blue" } + } + ] + GridView { + id: grid + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + flow: (testTopToBottom == false) ? GridView.LeftToRight : GridView.TopToBottom + layoutDirection: (testRightToLeft == true) ? Qt.RightToLeft : Qt.LeftToRight + model: testModel + delegate: myDelegate + header: root.showHeader ? headerFooter : null + footer: root.showFooter ? headerFooter : null + cacheBuffer: root.cacheBuffer + } +} diff --git a/tests/auto/quick/qquickgridview/data/gridview2.qml b/tests/auto/quick/qquickgridview/data/gridview2.qml new file mode 100644 index 0000000000..5fb45a1613 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview2.qml @@ -0,0 +1,26 @@ +import QtQuick 2.0 + +GridView { + anchors.fill: parent + width: 320; height: 200 + cellWidth: 100; cellHeight: 100; cacheBuffer: 200; focus: true + keyNavigationWraps: true; highlightFollowsCurrentItem: false + + model: ListModel { + id: appModel + ListElement { lColor: "red" } + ListElement { lColor: "yellow" } + ListElement { lColor: "green" } + ListElement { lColor: "blue" } + } + + delegate: Item { + width: 100; height: 100 + Rectangle { + color: lColor; x: 4; y: 4 + width: 92; height: 92 + } + } + + highlight: Rectangle { width: 100; height: 100; color: "black" } +} diff --git a/tests/auto/quick/qquickgridview/data/gridview3.qml b/tests/auto/quick/qquickgridview/data/gridview3.qml new file mode 100644 index 0000000000..a8c1c5a0f7 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview3.qml @@ -0,0 +1,6 @@ +import QtQuick 2.0 + +GridView { + anchors.fill: parent + width: 320; height: 200 +} diff --git a/tests/auto/quick/qquickgridview/data/gridview4.qml b/tests/auto/quick/qquickgridview/data/gridview4.qml new file mode 100644 index 0000000000..eed3a2bdb1 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/gridview4.qml @@ -0,0 +1,11 @@ +import QtQuick 2.0 + +GridView { + width: 405 + height: 200 + cellWidth: width/9 + cellHeight: height/2 + + model: 18 + delegate: Rectangle { objectName: "delegate"; width: 10; height: 10; color: "green" } +} diff --git a/tests/auto/quick/qquickgridview/data/header.qml b/tests/auto/quick/qquickgridview/data/header.qml new file mode 100644 index 0000000000..648e2a2298 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/header.qml @@ -0,0 +1,40 @@ +import QtQuick 2.0 + +Rectangle { + function changeHeader() { + grid.header = header2 + } + width: 240 + height: 320 + color: "#ffffff" + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.color: "blue" + Text { + text: index + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + GridView { + id: grid + objectName: "grid" + width: initialViewWidth + height: initialViewHeight + cellWidth: 80 + cellHeight: 60 + model: testModel + delegate: myDelegate + header: Text { objectName: "header"; text: "Header " + x + "," + y; width: 100; height: 30 } + } + + Component { + id: header2 + Text { objectName: "header2"; text: "Header 2 " + x + "," + y; width: 50; height: 20 } + } +} diff --git a/tests/auto/quick/qquickgridview/data/manual-highlight.qml b/tests/auto/quick/qquickgridview/data/manual-highlight.qml new file mode 100644 index 0000000000..c2f1d20fb1 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/manual-highlight.qml @@ -0,0 +1,48 @@ +import QtQuick 2.0 + +Item { + + ListModel { + id: model + ListElement { + name: "Bill Smith" + number: "555 3264" + } + ListElement { + name: "John Brown" + number: "555 8426" + } + ListElement { + name: "Sam Wise" + number: "555 0473" + } + ListElement { + name: "Bob Brown" + number: "555 5845" + } + } + + Component { + id: highlight + Rectangle { + objectName: "highlight" + width: 80; height: 80 + color: "lightsteelblue"; radius: 5 + y: grid.currentItem.y+5 + x: grid.currentItem.x+5 + } + } + + GridView { + id: grid + objectName: "grid" + anchors.fill: parent + model: model + delegate: Text { objectName: "wrapper"; text: name; width: 80; height: 80 } + + highlight: highlight + highlightFollowsCurrentItem: false + focus: true + } + +} diff --git a/tests/auto/quick/qquickgridview/data/margins.qml b/tests/auto/quick/qquickgridview/data/margins.qml new file mode 100644 index 0000000000..d369658a91 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/margins.qml @@ -0,0 +1,55 @@ +import QtQuick 2.0 + +Rectangle { + id: root + + width: 240 + height: 320 + color: "#ffffff" + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 100 + height: 80 + border.color: "blue" + property string name: model.name + Text { + text: index + } + Text { + x: 40 + text: wrapper.x + ", " + wrapper.y + } + Text { + y: 20 + id: textName + objectName: "textName" + text: name + } + Text { + y: 40 + id: textNumber + objectName: "textNumber" + text: number + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + GridView { + id: grid + objectName: "grid" + width: 240 + height: 320 + cellWidth: 100 + cellHeight: 80 + leftMargin: 30 + rightMargin: 50 + flow: GridView.TopToBottom + layoutDirection: (testRightToLeft == true) ? Qt.RightToLeft : Qt.LeftToRight + model: testModel + delegate: myDelegate + } + Text { anchors.bottom: parent.bottom; text: grid.contentX } +} diff --git a/tests/auto/quick/qquickgridview/data/mirroring.qml b/tests/auto/quick/qquickgridview/data/mirroring.qml new file mode 100644 index 0000000000..b9aff501c1 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/mirroring.qml @@ -0,0 +1,43 @@ +// This example demonstrates how item positioning +// changes in right-to-left layout direction + +import QtQuick 2.0 + +Rectangle { + color: "lightgray" + width: 340 + height: 370 + + VisualItemModel { + id: itemModel + objectName: "itemModel" + Rectangle { + objectName: "item1" + height: 110; width: 120; color: "#FFFEF0" + Text { objectName: "text1"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + Rectangle { + objectName: "item2" + height: 130; width: 150; color: "#F0FFF7" + Text { objectName: "text2"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + Rectangle { + objectName: "item3" + height: 170; width: 190; color: "#F4F0FF" + Text { objectName: "text3"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + } + + GridView { + id: view + objectName: "view" + cellWidth: 190 + cellHeight: 170 + anchors.fill: parent + anchors.bottomMargin: 30 + model: itemModel + highlightRangeMode: "StrictlyEnforceRange" + flow: GridView.TopToBottom + flickDeceleration: 2000 + } +} diff --git a/tests/auto/quick/qquickgridview/data/moveTransitions.qml b/tests/auto/quick/qquickgridview/data/moveTransitions.qml new file mode 100644 index 0000000000..a91f5a3295 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/moveTransitions.qml @@ -0,0 +1,143 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 500 + height: 600 + + property int duration: 10 + property int count: grid.count + + Component { + id: myDelegate + Rectangle { + id: wrapper + + property string nameData: name + + objectName: "wrapper" + width: 80 + height: 60 + border.width: 1 + Column { + Text { text: index } + Text { + text: wrapper.x + ", " + wrapper.y + } + Text { + id: textName + objectName: "textName" + text: name + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + + onXChanged: checkPos() + onYChanged: checkPos() + + function checkPos() { + if (Qt.point(x, y) == targetItems_transitionVia) + model_targetItems_transitionVia.addItem(name, "") + if (Qt.point(x, y) == displacedItems_transitionVia) + model_displacedItems_transitionVia.addItem(name, "") + } + } + } + + GridView { + id: grid + + property int targetTransitionsDone + property int displaceTransitionsDone + + property var targetTrans_items: new Object() + property var targetTrans_targetIndexes: new Array() + property var targetTrans_targetItems: new Array() + + property var displacedTrans_items: new Object() + property var displacedTrans_targetIndexes: new Array() + property var displacedTrans_targetItems: new Array() + + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + anchors.centerIn: parent + model: testModel + delegate: myDelegate + + // for QQmlListProperty types + function copyList(propList) { + var temp = new Array() + for (var i=0; i<propList.length; i++) + temp.push(propList[i]) + return temp + } + + move: Transition { + id: targetTransition + + SequentialAnimation { + ScriptAction { + script: { + grid.targetTrans_items[targetTransition.ViewTransition.item.nameData] = targetTransition.ViewTransition.index + grid.targetTrans_targetIndexes.push(targetTransition.ViewTransition.targetIndexes) + grid.targetTrans_targetItems.push(grid.copyList(targetTransition.ViewTransition.targetItems)) + } + } + ParallelAnimation { + NumberAnimation { properties: "x"; to: targetItems_transitionVia.x; duration: root.duration } + NumberAnimation { properties: "y"; to: targetItems_transitionVia.y; duration: root.duration } + } + + NumberAnimation { properties: "x,y"; duration: root.duration } + + ScriptAction { script: grid.targetTransitionsDone += 1 } + } + } + + moveDisplaced: Transition { + id: displaced + + SequentialAnimation { + ScriptAction { + script: { + grid.displacedTrans_items[displaced.ViewTransition.item.nameData] = displaced.ViewTransition.index + grid.displacedTrans_targetIndexes.push(displaced.ViewTransition.targetIndexes) + grid.displacedTrans_targetItems.push(grid.copyList(displaced.ViewTransition.targetItems)) + } + } + ParallelAnimation { + NumberAnimation { + properties: "x"; duration: root.duration + to: displacedItems_transitionVia.x + } + NumberAnimation { + properties: "y"; duration: root.duration + to: displacedItems_transitionVia.y + } + } + NumberAnimation { properties: "x,y"; duration: root.duration } + + ScriptAction { script: grid.displaceTransitionsDone += 1 } + } + + } + } + + Rectangle { + anchors.fill: grid + color: "lightsteelblue" + opacity: 0.2 + } + + Rectangle { + anchors.bottom: parent.bottom + width: 20; height: 20 + color: "white" + NumberAnimation on x { loops: Animation.Infinite; from: 0; to: 300; duration: 10000 } + } +} + + diff --git a/tests/auto/quick/qquickgridview/data/multipleTransitions.qml b/tests/auto/quick/qquickgridview/data/multipleTransitions.qml new file mode 100644 index 0000000000..45b86e22cf --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/multipleTransitions.qml @@ -0,0 +1,123 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 500 + height: 600 + + // time to pause between each add, remove, etc. + // (obviously, must be less than 'duration' value to actually test that + // interrupting transitions will still produce the correct result) + property int timeBetweenActions: duration / 2 + + property int duration: 100 + + property int count: grid.count + + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.width: 1 + Column { + Text { text: index } + Text { + text: wrapper.x + ", " + wrapper.y + } + Text { + id: textName + objectName: "textName" + text: name + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + } + } + + GridView { + id: grid + + property bool populateDone + + property bool runningAddTargets: false + property bool runningAddDisplaced: false + property bool runningMoveTargets: false + property bool runningMoveDisplaced: false + + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + anchors.centerIn: parent + model: testModel + delegate: myDelegate + + add: Transition { + id: addTargets + SequentialAnimation { + ScriptAction { script: grid.runningAddTargets = true } + ParallelAnimation { + NumberAnimation { properties: "x"; from: addTargets_transitionFrom.x; duration: root.duration } + NumberAnimation { properties: "y"; from: addTargets_transitionFrom.y; duration: root.duration } + } + ScriptAction { script: grid.runningAddTargets = false } + } + } + + addDisplaced: Transition { + id: addDisplaced + SequentialAnimation { + ScriptAction { script: grid.runningAddDisplaced = true } + ParallelAnimation { + NumberAnimation { properties: "x"; from: addDisplaced_transitionFrom.x; duration: root.duration } + NumberAnimation { properties: "y"; from: addDisplaced_transitionFrom.y; duration: root.duration } + } + ScriptAction { script: grid.runningAddDisplaced = false } + } + } + + move: Transition { + id: moveTargets + SequentialAnimation { + ScriptAction { script: grid.runningMoveTargets = true } + ParallelAnimation { + NumberAnimation { properties: "x"; from: moveTargets_transitionFrom.x; duration: root.duration } + NumberAnimation { properties: "y"; from: moveTargets_transitionFrom.y; duration: root.duration } + } + ScriptAction { script: grid.runningMoveTargets = false } + } + } + + moveDisplaced: Transition { + id: moveDisplaced + SequentialAnimation { + ScriptAction { script: grid.runningMoveDisplaced = true } + ParallelAnimation { + NumberAnimation { properties: "x"; from: moveDisplaced_transitionFrom.x; duration: root.duration } + NumberAnimation { properties: "y"; from: moveDisplaced_transitionFrom.y; duration: root.duration } + } + ScriptAction { script: grid.runningMoveDisplaced = false } + } + } + } + + Rectangle { + anchors.fill: grid + color: "lightsteelblue" + opacity: 0.2 + } + + Rectangle { + anchors.bottom: parent.bottom + width: 20; height: 20 + color: "white" + NumberAnimation on x { loops: Animation.Infinite; from: 0; to: 300; duration: 100000 } + } +} + + + diff --git a/tests/auto/quick/qquickgridview/data/populateTransitions.qml b/tests/auto/quick/qquickgridview/data/populateTransitions.qml new file mode 100644 index 0000000000..c12d5ac39d --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/populateTransitions.qml @@ -0,0 +1,103 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 500 + height: 600 + + property int duration: 10 + property int count: grid.count + + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + width: 80 + height: 60 + border.width: 1 + Column { + Text { text: index } + Text { + text: wrapper.x + ", " + wrapper.y + } + Text { + id: textName + objectName: "textName" + text: name + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + + onXChanged: checkPos() + onYChanged: checkPos() + + function checkPos() { + if (Qt.point(x, y) == transitionFrom) + model_transitionFrom.addItem(name, "") + if (Qt.point(x, y) == transitionVia) + model_transitionVia.addItem(name, "") + } + } + } + + GridView { + id: grid + + property int countPopulateTransitions + property int countAddTransitions + + objectName: "grid" + focus: true + anchors.centerIn: parent + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + model: testModel + delegate: myDelegate + + populate: usePopulateTransition ? popTransition : null + + add: Transition { + SequentialAnimation { + ScriptAction { script: grid.countAddTransitions += 1 } + NumberAnimation { properties: "x,y"; duration: root.duration } + } + } + } + + Transition { + id: popTransition + SequentialAnimation { + ParallelAnimation { + NumberAnimation { properties: "x"; from: transitionFrom.x; to: transitionVia.x; duration: root.duration } + NumberAnimation { properties: "y"; from: transitionFrom.y; to: transitionVia.y; duration: root.duration } + } + NumberAnimation { properties: "x,y"; duration: root.duration } + ScriptAction { script: grid.countPopulateTransitions += 1 } + } + } + + Rectangle { + anchors.fill: grid + color: "lightsteelblue" + opacity: 0.2 + } + + Component.onCompleted: { + if (dynamicallyPopulate) { + for (var i=0; i<30; i++) + testModel.addItem("item " + i, "") + } + } + + Rectangle { + anchors.bottom: parent.bottom + width: 20; height: 20 + color: "white" + NumberAnimation on x { loops: Animation.Infinite; from: 0; to: 300; duration: 100000 } + } +} + + diff --git a/tests/auto/quick/qquickgridview/data/propertychangestest.qml b/tests/auto/quick/qquickgridview/data/propertychangestest.qml new file mode 100644 index 0000000000..97efbe78f5 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/propertychangestest.qml @@ -0,0 +1,69 @@ +import QtQuick 2.0 + +Rectangle { + width: 360; height: 120; color: "white" + Component { + id: delegate + Item { + id: wrapper + width: 180; height: 40; + Column { + x: 5; y: 5 + Text { text: '<b>Name:</b> ' + name } + Text { text: '<b>Number:</b> ' + number } + } + } + } + Component { + id: highlightRed + Rectangle { + color: "red" + radius: 10 + opacity: 0.5 + } + } + GridView { + cellWidth:180 + cellHeight:40 + objectName: "gridView" + anchors.fill: parent + model: listModel + delegate: delegate + highlight: highlightRed + focus: true + keyNavigationWraps: true + cacheBuffer: 10 + flow: GridView.LeftToRight + } + + data:[ + ListModel { + id: listModel + ListElement { + name: "Bill Smith" + number: "555 3264" + } + ListElement { + name: "John Brown" + number: "555 8426" + } + ListElement { + name: "Sam Wise" + number: "555 0473" + } + }, + ListModel { + objectName: "alternateModel" + ListElement { + name: "Jack" + number: "555 8426" + } + ListElement { + name: "Mary" + number: "555 3264" + } + } + ] +} + + diff --git a/tests/auto/quick/qquickgridview/data/removeTransitions.qml b/tests/auto/quick/qquickgridview/data/removeTransitions.qml new file mode 100644 index 0000000000..3e82cf7f96 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/removeTransitions.qml @@ -0,0 +1,146 @@ +import QtQuick 2.0 + +Rectangle { + id: root + width: 500 + height: 600 + + property int duration: 10 + property int count: grid.count + + Component { + id: myDelegate + Rectangle { + id: wrapper + + property string nameData: name + + objectName: "wrapper" + width: 80 + height: 60 + border.width: 1 + Column { + Text { text: index } + Text { + text: wrapper.x + ", " + wrapper.y + } + Text { + id: textName + objectName: "textName" + text: name + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "white" + + onXChanged: checkPos() + onYChanged: checkPos() + + function checkPos() { + if (Qt.point(x, y) == targetItems_transitionTo) { + model_targetItems_transitionTo.addItem(nameData, "") // name is invalid once model removes the item + } + if (Qt.point(x, y) == displacedItems_transitionVia) { + model_displacedItems_transitionVia.addItem(name, "") + } + } + } + } + + GridView { + id: grid + + property int targetTransitionsDone + property int displaceTransitionsDone + + property var targetTrans_items: new Object() + property var targetTrans_targetIndexes: new Array() + property var targetTrans_targetItems: new Array() + + property var displacedTrans_items: new Object() + property var displacedTrans_targetIndexes: new Array() + property var displacedTrans_targetItems: new Array() + + objectName: "grid" + width: 240 + height: 320 + cellWidth: 80 + cellHeight: 60 + anchors.centerIn: parent + model: testModel + delegate: myDelegate + + // for QQmlListProperty types + function copyList(propList) { + var temp = new Array() + for (var i=0; i<propList.length; i++) + temp.push(propList[i]) + return temp + } + + remove: Transition { + id: targetTransition + + SequentialAnimation { + ScriptAction { + script: { + grid.targetTrans_items[targetTransition.ViewTransition.item.nameData] = targetTransition.ViewTransition.index + grid.targetTrans_targetIndexes.push(targetTransition.ViewTransition.targetIndexes) + grid.targetTrans_targetItems.push(grid.copyList(targetTransition.ViewTransition.targetItems)) + } + } + ParallelAnimation { + NumberAnimation { properties: "x"; to: targetItems_transitionTo.x; duration: root.duration } + NumberAnimation { properties: "y"; to: targetItems_transitionTo.y; duration: root.duration } + } + ScriptAction { script: grid.targetTransitionsDone += 1 } + + // delay deleting this item so that it stays valid for the tests + // (this doesn't delay the test itself) + PauseAnimation { duration: 10000 } + } + } + + removeDisplaced: Transition { + id: displaced + + SequentialAnimation { + ScriptAction { + script: { + grid.displacedTrans_items[displaced.ViewTransition.item.nameData] = displaced.ViewTransition.index + grid.displacedTrans_targetIndexes.push(displaced.ViewTransition.targetIndexes) + grid.displacedTrans_targetItems.push(grid.copyList(displaced.ViewTransition.targetItems)) + } + } + ParallelAnimation { + NumberAnimation { + properties: "x"; duration: root.duration + to: displacedItems_transitionVia.x + } + NumberAnimation { + properties: "y"; duration: root.duration + to: displacedItems_transitionVia.y + } + } + NumberAnimation { properties: "x,y"; duration: root.duration } + + ScriptAction { script: grid.displaceTransitionsDone += 1 } + } + + } + } + + Rectangle { + anchors.fill: grid + color: "lightsteelblue" + opacity: 0.2 + } + + Rectangle { + anchors.bottom: parent.bottom + width: 20; height: 20 + color: "white" + NumberAnimation on x { loops: Animation.Infinite; from: 0; to: 300; duration: 10000 } + } +} + + diff --git a/tests/auto/quick/qquickgridview/data/resizeview.qml b/tests/auto/quick/qquickgridview/data/resizeview.qml new file mode 100644 index 0000000000..130a0defc1 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/resizeview.qml @@ -0,0 +1,25 @@ +import QtQuick 2.0 + +Rectangle { + id: root + + width: 240 + height: 320 + + GridView { + id: grid + objectName: "grid" + width: initialWidth + height: initialHeight + cellWidth: 80 + cellHeight: 60 + model: testModel + delegate: Rectangle { + objectName: "wrapper" + width: 80 + height: 60 + border.width: 1 + } + } +} + diff --git a/tests/auto/quick/qquickgridview/data/setindex.qml b/tests/auto/quick/qquickgridview/data/setindex.qml new file mode 100644 index 0000000000..ef80f3a2fb --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/setindex.qml @@ -0,0 +1,29 @@ +import QtQuick 2.0 + +Rectangle { + width: 200 + height: 200 + Component { + id: appDelegate + + Item { + id : wrapper + function startupFunction() { + if (index == 5) view.currentIndex = index; + } + Component.onCompleted: startupFunction(); + width: 30; height: 30 + Text { text: index } + } + } + + GridView { + id: view + objectName: "grid" + anchors.fill: parent + cellWidth: 30; cellHeight: 30 + model: 35 + delegate: appDelegate + focus: true + } +} diff --git a/tests/auto/quick/qquickgridview/data/snapOneRow.qml b/tests/auto/quick/qquickgridview/data/snapOneRow.qml new file mode 100644 index 0000000000..3d32d75c45 --- /dev/null +++ b/tests/auto/quick/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/quick/qquickgridview/data/snapToRow.qml b/tests/auto/quick/qquickgridview/data/snapToRow.qml new file mode 100644 index 0000000000..f079a048f0 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/snapToRow.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: 80 + width: 80 + Column { + Text { + text: index + } + Text { + text: wrapper.x + ", " + wrapper.y + } + } + color: GridView.isCurrentItem ? "lightsteelblue" : "transparent" + } + } + GridView { + id: grid + objectName: "grid" + anchors.fill: parent + cellWidth: 80 + cellHeight: 80 + preferredHighlightBegin: 20 + preferredHighlightEnd: 100 + snapMode: GridView.SnapToRow + layoutDirection: Qt.RightToLeft + flow: GridView.TopToBottom + highlightRangeMode: GridView.StrictlyEnforceRange + highlight: Rectangle { width: 80; height: 80; color: "yellow" } + model: 54 + delegate: myDelegate + } + + Text { + anchors.right: parent.right + anchors.bottom: parent.bottom + text: grid.contentX + ", " + grid.contentY + } +} diff --git a/tests/auto/quick/qquickgridview/data/unaligned.qml b/tests/auto/quick/qquickgridview/data/unaligned.qml new file mode 100644 index 0000000000..445400e8b4 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/unaligned.qml @@ -0,0 +1,15 @@ +import QtQuick 2.0 + +GridView { + width: 400 + height: 200 + cellWidth: width/9 + cellHeight: height/2 + + model: testModel + delegate: Rectangle { + objectName: "wrapper"; width: 10; height: 10; color: "green" + Text { text: index } + } +} + diff --git a/tests/auto/quick/qquickgridview/data/unrequestedItems.qml b/tests/auto/quick/qquickgridview/data/unrequestedItems.qml new file mode 100644 index 0000000000..79f845fd25 --- /dev/null +++ b/tests/auto/quick/qquickgridview/data/unrequestedItems.qml @@ -0,0 +1,71 @@ +import QtQuick 2.0 + +Item { + width: 240 + height: 320 + + Component { + id: myDelegate + + Package { + Rectangle { + id: leftWrapper + objectName: "wrapper" + Package.name: "left" + height: 80 + width: 60 + Column { + Text { text: index } + Text { text: name } + Text { text: leftWrapper.x + ", " + leftWrapper.y } + } + color: ListView.isCurrentItem ? "lightsteelblue" : "white" + } + Rectangle { + id: rightWrapper + objectName: "wrapper" + Package.name: "right" + height: 80 + width: 60 + Column { + Text { text: index } + Text { text: name } + Text { text: rightWrapper.x + ", " + rightWrapper.y } + } + color: ListView.isCurrentItem ? "lightsteelblue" : "white" + } + } + + } + + VisualDataModel { + id: visualModel + + delegate: myDelegate + model: testModel + } + + GridView { + id: leftList + objectName: "leftGrid" + anchors { + left: parent.left; top: parent.top; + right: parent.horizontalCenter; bottom: parent.bottom + } + model: visualModel.parts.left + cellWidth: 60 + cellHeight: 80 + } + + GridView { + id: rightList + objectName: "rightGrid" + anchors { + left: parent.horizontalCenter; top: parent.top; + right: parent.right; bottom: parent.bottom + } + model: visualModel.parts.right + cellWidth: 60 + cellHeight: 80 + } +} diff --git a/tests/auto/quick/qquickgridview/qquickgridview.pro b/tests/auto/quick/qquickgridview/qquickgridview.pro new file mode 100644 index 0000000000..cabf4396b4 --- /dev/null +++ b/tests/auto/quick/qquickgridview/qquickgridview.pro @@ -0,0 +1,15 @@ +CONFIG += testcase +TARGET = tst_qquickgridview +macx:CONFIG -= app_bundle + +SOURCES += tst_qquickgridview.cpp + +include (../../shared/util.pri) +include (../shared/util.pri) + +testDataFiles.files = data +testDataFiles.path = . +DEPLOYMENT += testDataFiles + +CONFIG += parallel_test +QT += core-private gui-private v8-private qml-private quick-private opengl-private testlib widgets diff --git a/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp b/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp new file mode 100644 index 0000000000..d156ed0957 --- /dev/null +++ b/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp @@ -0,0 +1,5075 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtCore/qstringlistmodel.h> +#include <QtQuick/qquickview.h> +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcomponent.h> +#include <QtQml/qqmlcontext.h> +#include <QtQml/qqmlexpression.h> +#include <QtQml/qqmlincubator.h> +#include <QtQuick/private/qquickitem_p.h> +#include <QtQuick/private/qquickgridview_p.h> +#include <QtQuick/private/qquicktext_p.h> +#include <QtQuick/private/qquickvisualitemmodel_p.h> +#include <QtQml/private/qquicklistmodel_p.h> +#include <QtQml/private/qlistmodelinterface_p.h> +#include "../../shared/util.h" +#include "../shared/viewtestutil.h" +#include "../shared/visualtestutil.h" +#include <QtGui/qguiapplication.h> + +Q_DECLARE_METATYPE(Qt::LayoutDirection) +Q_DECLARE_METATYPE(QQuickGridView::Flow) + +using namespace QQuickViewTestUtil; +using namespace QQuickVisualTestUtil; + +class tst_QQuickGridView : public QQmlDataTest +{ + Q_OBJECT +public: + tst_QQuickGridView(); + +private slots: + void items(); + void changed(); + void inserted(); + void inserted_more(); + void inserted_more_data(); + void insertBeforeVisible(); + void insertBeforeVisible_data(); + void removed(); + void removed_more(); + void removed_more_data(); + void addOrRemoveBeforeVisible(); + void addOrRemoveBeforeVisible_data(); + void clear(); + void moved(); + void moved_data(); + void multipleChanges(); + void multipleChanges_data(); + void swapWithFirstItem(); + void changeFlow(); + void currentIndex(); + void noCurrentIndex(); + void defaultValues(); + void properties(); + void propertyChanges(); + void componentChanges(); + void modelChanges(); + void positionViewAtIndex(); + void positionViewAtIndex_rightToLeft(); + void mirroring(); + void snapping(); + void resetModel(); + void enforceRange(); + void enforceRange_rightToLeft(); + void QTBUG_8456(); + void manualHighlight(); + void footer(); + void footer_data(); + void header(); + void header_data(); + void resizeViewAndRepaint(); + void changeColumnCount(); + void indexAt_itemAt_data(); + void indexAt_itemAt(); + void onAdd(); + void onAdd_data(); + void onRemove(); + void onRemove_data(); + void columnCount(); + void margins(); + void creationContext(); + void snapToRow_data(); + void snapToRow(); + void snapOneRow_data(); + void snapOneRow(); + void unaligned(); + void cacheBuffer(); + void asynchronous(); + void unrequestedVisibility(); + + void populateTransitions(); + void populateTransitions_data(); + void addTransitions(); + void addTransitions_data(); + void moveTransitions(); + void moveTransitions_data(); + void removeTransitions(); + void removeTransitions_data(); + void multipleTransitions(); + void multipleTransitions_data(); + +private: + QList<int> toIntList(const QVariantList &list); + void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes); + void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes); + void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems); +}; + +tst_QQuickGridView::tst_QQuickGridView() +{ +} + +void tst_QQuickGridView::items() +{ + QQuickView *canvas = createView(); + + QaimModel model; + model.addItem("Fred", "12345"); + model.addItem("John", "2345"); + model.addItem("Bob", "54321"); + model.addItem("Billy", "22345"); + model.addItem("Sam", "2945"); + model.addItem("Ben", "04321"); + model.addItem("Jim", "0780"); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QTRY_COMPARE(gridview->count(), model.count()); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item + + for (int i = 0; i < model.count(); ++i) { + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + + // set an empty model and confirm that items are destroyed + QaimModel model2; + ctxt->setContextProperty("testModel", &model2); + + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + QTRY_VERIFY(itemCount == 0); + + delete canvas; +} + +void tst_QQuickGridView::changed() +{ + QQuickView *canvas = createView(); + + QaimModel model; + model.addItem("Fred", "12345"); + model.addItem("John", "2345"); + model.addItem("Bob", "54321"); + model.addItem("Billy", "22345"); + model.addItem("Sam", "2945"); + model.addItem("Ben", "04321"); + model.addItem("Jim", "0780"); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + model.modifyItem(1, "Will", "9876"); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(1)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(1)); + + delete canvas; +} + +void tst_QQuickGridView::inserted() +{ + QQuickView *canvas = createView(); + canvas->show(); + + QaimModel model; + model.addItem("Fred", "12345"); + model.addItem("John", "2345"); + model.addItem("Bob", "54321"); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + model.insertItem(1, "Will", "9876"); + + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item + + QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(1)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(1)); + + // Checks that onAdd is called + int added = canvas->rootObject()->property("added").toInt(); + QTRY_COMPARE(added, 1); + + // Confirm items positioned correctly + for (int i = 0; i < model.count(); ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QTRY_COMPARE(item->x(), (i%3)*80.0); + QTRY_COMPARE(item->y(), (i/3)*60.0); + } + + model.insertItem(0, "Foo", "1111"); // zero index, and current item + + QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item + + name = findItem<QQuickText>(contentItem, "textName", 0); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(0)); + number = findItem<QQuickText>(contentItem, "textNumber", 0); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(0)); + + QTRY_COMPARE(gridview->currentIndex(), 1); + + // Confirm items positioned correctly + for (int i = 0; i < model.count(); ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60); + } + + for (int i = model.count(); i < 30; ++i) + model.insertItem(i, "Hello", QString::number(i)); + + gridview->setContentY(120); + + // Insert item outside visible area + model.insertItem(1, "Hello", "1324"); + + QTRY_VERIFY(gridview->contentY() == 120); + + delete canvas; +} + +void tst_QQuickGridView::inserted_more() +{ + QFETCH(qreal, contentY); + QFETCH(int, insertIndex); + QFETCH(int, insertCount); + QFETCH(qreal, itemsOffsetAfterMove); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + gridview->setContentY(contentY); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QList<QPair<QString, QString> > newData; + for (int i=0; i<insertCount; i++) + newData << qMakePair(QString("value %1").arg(i), QString::number(i)); + model.insertItems(insertIndex, newData); + QTRY_COMPARE(gridview->property("count").toInt(), model.count()); + + // check visibleItems.first() is in correct position + QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0); + QVERIFY(item0); + QCOMPARE(item0->y(), 0.0); + + QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper"); + int firstVisibleIndex = -1; + for (int i=0; i<items.count(); i++) { + if (items[i]->y() >= contentY) { + QQmlExpression e(qmlContext(items[i]), items[i], "index"); + firstVisibleIndex = e.evaluate().toInt(); + break; + } + } + QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); + + // Confirm items positioned correctly and indexes correct + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + QQuickText *name; + QQuickText *number; + for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove); + + name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(i)); + number = findItem<QQuickText>(contentItem, "textNumber", i); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::inserted_more_data() +{ + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<int>("insertIndex"); + QTest::addColumn<int>("insertCount"); + QTest::addColumn<qreal>("itemsOffsetAfterMove"); + + QTest::newRow("add 1, before visible items") + << 120.0 // show 6-23 + << 5 << 1 + << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row + // new 1st visible item is at 0 + + QTest::newRow("add 2, before visible items") + << 120.0 // show 6-23 + << 5 << 2 + << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row + + QTest::newRow("add 3, before visible items") + << 120.0 // show 6-23 + << 5 << 3 + << -60.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move + + QTest::newRow("add 5, before visible items") + << 120.0 // show 6-23 + << 5 << 5 + << -60.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos, + // grid is rearranged and first visible moves forward within its row + + QTest::newRow("add 6, before visible items") + << 120.0 // show 6-23 + << 5 << 6 + << -60.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move + + + + QTest::newRow("add 1, at start of visible, content at start") + << 0.0 + << 0 << 1 + << 0.0; + + QTest::newRow("add multiple, at start of visible, content at start") + << 0.0 + << 0 << 3 + << 0.0; + + QTest::newRow("add 1, at start of visible, content not at start") + << 120.0 // show 6-23 + << 6 << 1 + << 0.0; + + QTest::newRow("add multiple, at start of visible, content not at start") + << 120.0 // show 6-23 + << 6 << 3 + << 0.0; + + + QTest::newRow("add 1, at end of visible, content at start") + << 0.0 + << 17 << 1 + << 0.0; + + QTest::newRow("add 1, at end of visible, content at start") + << 0.0 + << 17 << 3 + << 0.0; + + QTest::newRow("add 1, at end of visible, content not at start") + << 120.0 // show 6-23 + << 23 << 1 + << 0.0; + + QTest::newRow("add multiple, at end of visible, content not at start") + << 120.0 // show 6-23 + << 23 << 3 + << 0.0; + + + QTest::newRow("add 1, after visible, content at start") + << 0.0 + << 20 << 1 + << 0.0; + + QTest::newRow("add 1, after visible, content at start") + << 0.0 + << 20 << 3 + << 0.0; + + QTest::newRow("add 1, after visible, content not at start") + << 120.0 // show 6-23 + << 24 << 1 + << 0.0; + + QTest::newRow("add multiple, after visible, content not at start") + << 120.0 // show 6-23 + << 24 << 3 + << 0.0; +} + +void tst_QQuickGridView::insertBeforeVisible() +{ + QFETCH(int, insertIndex); + QFETCH(int, insertCount); + QFETCH(int, cacheBuffer); + + QQuickText *name; + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + gridview->setCacheBuffer(cacheBuffer); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // trigger a refill (not just setting contentY) so that the visibleItems grid is updated + int firstVisibleIndex = 12; // move to an index where the top item is not visible + gridview->setContentY(firstVisibleIndex/3 * 60.0); + gridview->setCurrentIndex(firstVisibleIndex); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex); + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex); + QVERIFY(item); + QCOMPARE(item->y(), gridview->contentY()); + + QList<QPair<QString, QString> > newData; + for (int i=0; i<insertCount; i++) + newData << qMakePair(QString("value %1").arg(i), QString::number(i)); + model.insertItems(insertIndex, newData); + QTRY_COMPARE(gridview->property("count").toInt(), model.count()); + + // now, moving to the top of the view should position the inserted items correctly + int itemsOffsetAfterMove = (insertCount / 3) * -60.0; + gridview->setCurrentIndex(0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + QTRY_COMPARE(gridview->currentIndex(), 0); + QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove); + + // Confirm items positioned correctly and indexes correct + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove); + name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::insertBeforeVisible_data() +{ + QTest::addColumn<int>("insertIndex"); + QTest::addColumn<int>("insertCount"); + QTest::addColumn<int>("cacheBuffer"); + + QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0; + QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100; + QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500; + + QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0; + QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100; + QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500; + + QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0; + QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100; + QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500; + + QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0; + QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100; + QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500; +} + +void tst_QQuickGridView::removed() +{ + QQuickView *canvas = createView(); + canvas->show(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + model.removeItem(1); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + + QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(1)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(1)); + + + // Checks that onRemove is called + QString removed = canvas->rootObject()->property("removed").toString(); + QTRY_COMPARE(removed, QString("Item1")); + + // Confirm items positioned correctly + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60); + } + + // Remove first item (which is the current item); + model.removeItem(0); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + + name = findItem<QQuickText>(contentItem, "textName", 0); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(0)); + number = findItem<QQuickText>(contentItem, "textNumber", 0); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(0)); + + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60); + } + + // Remove items not visible + model.removeItem(25); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60); + } + + // Remove items before visible + gridview->setContentY(120); + gridview->setCurrentIndex(10); + + // Setting currentIndex above shouldn't cause view to scroll + QTRY_COMPARE(gridview->contentY(), 120.0); + + model.removeItem(1); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + + // Confirm items positioned correctly + for (int i = 6; i < 18; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60); + } + + // Remove currentIndex + QQuickItem *oldCurrent = gridview->currentItem(); + model.removeItem(9); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); + + QTRY_COMPARE(gridview->currentIndex(), 9); + QTRY_VERIFY(gridview->currentItem() != oldCurrent); + + gridview->setContentY(0); + // let transitions settle. + QTest::qWait(300); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60); + } + + // remove item outside current view. + gridview->setCurrentIndex(32); + gridview->setContentY(240); + + model.removeItem(30); + QTRY_VERIFY(gridview->currentIndex() == 31); + + // remove current item beyond visible items. + gridview->setCurrentIndex(20); + gridview->setContentY(0); + model.removeItem(20); + + QTRY_COMPARE(gridview->currentIndex(), 20); + QTRY_VERIFY(gridview->currentItem() != 0); + + // remove item before current, but visible + gridview->setCurrentIndex(8); + gridview->setContentY(240); + oldCurrent = gridview->currentItem(); + model.removeItem(6); + + QTRY_COMPARE(gridview->currentIndex(), 7); + QTRY_VERIFY(gridview->currentItem() == oldCurrent); + + delete canvas; +} + +void tst_QQuickGridView::removed_more() +{ + QFETCH(qreal, contentY); + QFETCH(int, removeIndex); + QFETCH(int, removeCount); + QFETCH(qreal, itemsOffsetAfterMove); + QFETCH(QString, firstVisible); + + QQuickText *name; + QQuickText *number; + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + gridview->setContentY(contentY); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + model.removeItems(removeIndex, removeCount); + QTRY_COMPARE(gridview->property("count").toInt(), model.count()); + + QString firstName; + int firstVisibleIndex = -1; + QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper"); + for (int i=0; i<items.count(); i++) { + if (items[i]->y() >= contentY) { + QQmlExpression e(qmlContext(items[i]), items[i], "index"); + firstVisibleIndex = e.evaluate().toInt(); + QQmlExpression en(qmlContext(items[i]), items[i], "name"); + firstName = en.evaluate().toString(); + break; + } + } + QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); + QCOMPARE(firstName, firstVisible); + + // Confirm items positioned correctly and indexes correct + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + + QTRY_COMPARE(item->x(), (i%3)*80.0); + QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove); + + name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + number = findItem<QQuickText>(contentItem, "textNumber", i); + QVERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::removed_more_data() +{ + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<int>("removeIndex"); + QTest::addColumn<int>("removeCount"); + QTest::addColumn<qreal>("itemsOffsetAfterMove"); + QTest::addColumn<QString>("firstVisible"); + + QTest::newRow("remove 1, before visible items") + << 120.0 // show 6-23 + << 2 << 1 + << 0.0 << "Item7"; + + QTest::newRow("remove 1, before visible position") + << 120.0 // show 6-23 + << 3 << 1 + << 0.0 << "Item7"; + + QTest::newRow("remove multiple, all before visible items") + << 120.0 + << 1 << 3 + << 60.0 << "Item6"; // removed top row, slide down by 1 row + + QTest::newRow("remove multiple, all before visible items, remove item 0") + << 120.0 + << 0 << 4 + << 60.0 << "Item7"; // removed top row, slide down by 1 row + + QTest::newRow("remove multiple rows, all before visible items") + << 240.0 // show 12-29 + << 1 << 7 + << 120.0 << "Item13"; + + QTest::newRow("remove one row before visible, content y not on item border") + << 100.0 + << 0 << 3 + << 60.0 << "Item6"; // 1 row removed + + QTest::newRow("remove mix of visible/non-visible") + << 120.0 // show 6-23 + << 2 << 3 + << 60.0 << "Item6"; // 1 row removed + + + // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos, + // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px) + QTest::newRow("remove multiple, mix of items from before and within visible items") + << 120.0 + << 3 << 5 + << 60.0 << "Item8"; // adjust for the 1 row removed before the visible + + QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0") + << 120.0 + << 0 << 8 + << 60.0 * 2 << "Item8"; // adjust for the 2 rows removed before the visible + + + QTest::newRow("remove 1, from start of visible, content at start") + << 0.0 + << 0 << 1 + << 0.0 << "Item1"; + + QTest::newRow("remove multiple, from start of visible, content at start") + << 0.0 + << 0 << 3 + << 0.0 << "Item3"; + + QTest::newRow("remove 1, from start of visible, content not at start") + << 120.0 // show 6-23 + << 4 << 1 + << 0.0 << "Item7"; + + QTest::newRow("remove multiple, from start of visible, content not at start") + << 120.0 // show 6-23 + << 4 << 3 + << 0.0 << "Item9"; + + + QTest::newRow("remove 1, from middle of visible, content at start") + << 0.0 + << 10 << 1 + << 0.0 << "Item0"; + + QTest::newRow("remove multiple, from middle of visible, content at start") + << 0.0 + << 10 << 5 + << 0.0 << "Item0"; + + QTest::newRow("remove 1, from middle of visible, content not at start") + << 120.0 // show 6-23 + << 10 << 1 + << 0.0 << "Item6"; + + QTest::newRow("remove multiple, from middle of visible, content not at start") + << 120.0 // show 6-23 + << 10 << 5 + << 0.0 << "Item6"; + + + QTest::newRow("remove 1, after visible, content at start") + << 0.0 + << 16 << 1 + << 0.0 << "Item0"; + + QTest::newRow("remove multiple, after visible, content at start") + << 0.0 + << 16 << 5 + << 0.0 << "Item0"; + + QTest::newRow("remove 1, after visible, content not at start") + << 120.0 // show 6-23 + << 16+4 << 1 + << 0.0 << "Item6"; + + QTest::newRow("remove multiple, after visible, content not at start") + << 120.0 // show 6-23 + << 16+4 << 5 + << 0.0 << "Item6"; + + QTest::newRow("remove multiple, mix of items from within and after visible items") + << 120.0 // show 6-23 + << 20 << 5 + << 0.0 << "Item6"; +} + +void tst_QQuickGridView::addOrRemoveBeforeVisible() +{ + // QTBUG-21588: ensure re-layout is done on grid after adding or removing + // items from before the visible area + + QFETCH(bool, doAdd); + QFETCH(qreal, newTopContentY); + + QQuickView *canvas = createView(); + canvas->show(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + canvas->setSource(testFileUrl("gridview1.qml")); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0); + QTRY_COMPARE(name->text(), QString("Item0")); + + gridview->setCurrentIndex(0); + qApp->processEvents(); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // scroll down until item 0 is no longer drawn + // (bug not triggered if we just move using content y, since that doesn't + // refill and change the visible items) + gridview->setCurrentIndex(24); + qApp->processEvents(); + + QTRY_COMPARE(gridview->currentIndex(), 24); + QTRY_COMPARE(gridview->contentY(), 220.0); + + QTest::qWait(100); // wait for refill to complete + QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible + + if (doAdd) { + model.insertItem(0, "New Item", "New Item number"); + QTRY_COMPARE(gridview->count(), 31); + } else { + model.removeItem(0); + QTRY_COMPARE(gridview->count(), 29); + } + + // scroll back up and item 0 should be gone + gridview->setCurrentIndex(0); + qApp->processEvents(); + QTRY_COMPARE(gridview->currentIndex(), 0); + QTRY_COMPARE(gridview->contentY(), newTopContentY); + + name = findItem<QQuickText>(contentItem, "textName", 0); + if (doAdd) + QCOMPARE(name->text(), QString("New Item")); + else + QCOMPARE(name->text(), QString("Item1")); + + // Confirm items positioned correctly + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i)); + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QTRY_VERIFY(item->x() == (i%3)*80); + QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY); + } + + delete canvas; +} + +void tst_QQuickGridView::addOrRemoveBeforeVisible_data() +{ + QTest::addColumn<bool>("doAdd"); + QTest::addColumn<qreal>("newTopContentY"); + + QTest::newRow("add") << true << -60.0; + QTest::newRow("remove") << false << -60.0; +} + +void tst_QQuickGridView::clear() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QVERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + model.clear(); + + QVERIFY(gridview->count() == 0); + QVERIFY(gridview->currentItem() == 0); + QVERIFY(gridview->contentY() == 0); + QVERIFY(gridview->currentIndex() == -1); + + // confirm sanity when adding an item to cleared list + model.addItem("New", "1"); + QTRY_COMPARE(gridview->count(), 1); + QVERIFY(gridview->currentItem() != 0); + QVERIFY(gridview->currentIndex() == 0); + + delete canvas; +} + +void tst_QQuickGridView::moved() +{ + QFETCH(qreal, contentY); + QFETCH(int, from); + QFETCH(int, to); + QFETCH(int, count); + QFETCH(qreal, itemsOffsetAfterMove); + + QQuickText *name; + QQuickText *number; + QScopedPointer<QQuickView> canvas(createView()); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QQuickItem *currentItem = gridview->currentItem(); + QTRY_VERIFY(currentItem != 0); + + if (contentY != 0) { + gridview->setContentY(contentY); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + } + + model.moveItems(from, to, count); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // Confirm items positioned correctly and indexes correct + int firstVisibleIndex = qCeil(contentY / 60.0) * 3; + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + if (i >= firstVisibleIndex + 18) // index has moved out of view + continue; + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + + QTRY_COMPARE(item->x(), (i%3)*80.0); + QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove); + + name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + number = findItem<QQuickText>(contentItem, "textNumber", i); + QVERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + + // current index should have been updated + if (item == currentItem) + QTRY_COMPARE(gridview->currentIndex(), i); + } +} + +void tst_QQuickGridView::moved_data() +{ + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<int>("from"); + QTest::addColumn<int>("to"); + QTest::addColumn<int>("count"); + QTest::addColumn<qreal>("itemsOffsetAfterMove"); + + // model starts with 30 items, each 80x60, in area 240x320 + // 18 items should be visible at a time + + // The first visible item should not move upwards and out of the view + // if items are moved/removed before it. + + + QTest::newRow("move 1 forwards, within visible items") + << 0.0 + << 1 << 8 << 1 + << 0.0; + + QTest::newRow("move 1 forwards, from non-visible -> visible") + << 120.0 // show 6-23 + << 1 << 23 << 1 + << 0.0; + + QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)") + << 120.0 // // show 6-23 + << 0 << 6 << 1 + << 0.0; + + QTest::newRow("move 1 forwards, from visible -> non-visible") + << 0.0 + << 1 << 20 << 1 + << 0.0; + + QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)") + << 0.0 + << 0 << 20 << 1 + << 0.0; + + + QTest::newRow("move 1 backwards, within visible items") + << 0.0 + << 10 << 5 << 1 + << 0.0; + + QTest::newRow("move 1 backwards, within visible items (to first index)") + << 0.0 + << 10 << 0 << 1 + << 0.0; + + QTest::newRow("move 1 backwards, from non-visible -> visible") + << 0.0 + << 28 << 8 << 1 + << 0.0; + + QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)") + << 0.0 + << 29 << 14 << 1 + << 0.0; + + QTest::newRow("move 1 backwards, from visible -> non-visible") + << 120.0 // show 6-23 + << 7 << 1 << 1 + << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move + + QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)") + << 120.0 // show 6-23 + << 7 << 0 << 1 + << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move + + + QTest::newRow("move multiple forwards, within visible items") + << 0.0 + << 0 << 5 << 3 + << 0.0; + + QTest::newRow("move multiple backwards, within visible items (move first item)") + << 0.0 + << 10 << 0 << 3 + << 0.0; + + QTest::newRow("move multiple forwards, before visible items") + << 120.0 // show 6-23 + << 3 << 4 << 3 // 3, 4, 5 move to after 6 + << 60.0; // row of 3,4,5 has moved down + + QTest::newRow("move multiple forwards, from non-visible -> visible") + << 120.0 // show 6-23 + << 1 << 6 << 3 + << 60.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is + + QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)") + << 120.0 // show 6-23 + << 0 << 6 << 3 + << 60.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row + + QTest::newRow("move multiple forwards, mix of non-visible/visible") + << 120.0 + << 3 << 16 << 6 + << 60.0; // top two rows removed, third row is now the first visible + + QTest::newRow("move multiple forwards, to bottom of view") + << 0.0 + << 5 << 13 << 5 + << 0.0; + + QTest::newRow("move multiple forwards, to bottom of view, first row -> last") + << 0.0 + << 0 << 15 << 3 + << 0.0; + + QTest::newRow("move multiple forwards, to bottom of view, content y not 0") + << 120.0 + << 5+4 << 13+4 << 5 + << 0.0; + + QTest::newRow("move multiple forwards, from visible -> non-visible") + << 0.0 + << 1 << 16 << 3 + << 0.0; + + QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)") + << 0.0 + << 0 << 16 << 3 + << 0.0; + + + QTest::newRow("move multiple backwards, within visible items") + << 0.0 + << 4 << 1 << 3 + << 0.0; + + QTest::newRow("move multiple backwards, from non-visible -> visible") + << 0.0 + << 20 << 4 << 3 + << 0.0; + + QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)") + << 0.0 + << 27 << 10 << 3 + << 0.0; + + QTest::newRow("move multiple backwards, from visible -> non-visible") + << 120.0 // show 6-23 + << 16 << 1 << 3 + << -60.0; // to minimize movement, items are added above visible area, all items move up by 1 row + + QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)") + << 120.0 // show 6-23 + << 16 << 0 << 3 + << -60.0; // 16,17,18 move to above item 0, all items move up by 1 row +} + +void tst_QQuickGridView::multipleChanges() +{ + QFETCH(int, startCount); + QFETCH(QList<ListChange>, changes); + QFETCH(int, newCount); + QFETCH(int, newCurrentIndex); + + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < startCount; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + for (int i=0; i<changes.count(); i++) { + switch (changes[i].type) { + case ListChange::Inserted: + { + QList<QPair<QString, QString> > items; + for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j) + items << qMakePair(QString("new item " + j), QString::number(j)); + model.insertItems(changes[i].index, items); + break; + } + case ListChange::Removed: + model.removeItems(changes[i].index, changes[i].count); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + break; + case ListChange::Moved: + model.moveItems(changes[i].index, changes[i].to, changes[i].count); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + break; + case ListChange::SetCurrent: + gridview->setCurrentIndex(changes[i].index); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + break; + case ListChange::SetContentY: + gridview->setContentY(changes[i].pos); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + break; + } + } + + QTRY_COMPARE(gridview->count(), newCount); + QCOMPARE(gridview->count(), model.count()); + QTRY_COMPARE(gridview->currentIndex(), newCurrentIndex); + + QQuickText *name; + QQuickText *number; + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i=0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + number = findItem<QQuickText>(contentItem, "textNumber", i); + QVERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::multipleChanges_data() +{ + QTest::addColumn<int>("startCount"); + QTest::addColumn<QList<ListChange> >("changes"); + QTest::addColumn<int>("newCount"); + QTest::addColumn<int>("newCurrentIndex"); + + QList<ListChange> changes; + + for (int i=1; i<30; i++) + changes << ListChange::remove(0); + QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0; + + changes << ListChange::remove(0); + QTest::newRow("remove all") << 30 << changes << 0 << -1; + + changes.clear(); + changes << ListChange::setCurrent(29); + for (int i=29; i>0; i--) + changes << ListChange::remove(i); + QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0; + + QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>() + << ListChange::remove(0, 1) + << ListChange::insert(0, 1) + ) << 10 << 1; + + QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>() + << ListChange::setCurrent(2) + << ListChange::remove(2, 1) + << ListChange::insert(2, 1) + ) << 10 << 3; + + QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>() + << ListChange::setCurrent(1) + << ListChange::remove(1, 3) + << ListChange::insert(2, 2) + ) << 9 << 1; + + QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>() + << ListChange::setCurrent(2) + << ListChange::remove(1, 3) + << ListChange::move(1, 5, 1) + ) << 7 << 5; + + QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>() + << ListChange::setCurrent(5) + << ListChange::remove(4, 3) + << ListChange::move(4, 1, 1) + ) << 7 << 1; + + + QTest::newRow("insert multiple times") << 0 << (QList<ListChange>() + << ListChange::insert(0, 2) + << ListChange::insert(0, 4) + << ListChange::insert(0, 6) + ) << 12 << 10; + + QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>() + << ListChange::insert(0, 2) + << ListChange::insert(0, 4) + << ListChange::insert(0, 6) + << ListChange::setCurrent(3) + << ListChange::insert(3, 2) + ) << 14 << 5; + + QTest::newRow("insert and remove all") << 0 << (QList<ListChange>() + << ListChange::insert(0, 30) + << ListChange::remove(0, 30) + ) << 0 << -1; + + QTest::newRow("insert and remove current") << 30 << (QList<ListChange>() + << ListChange::insert(1) + << ListChange::setCurrent(1) + << ListChange::remove(1) + ) << 30 << 1; + + QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>() + << ListChange::insert(0, 10) + << ListChange::remove(5, 10) + ) << 10 << 5; + + QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>() + << ListChange::insert(0, 3) + << ListChange::move(0, 10, 3) + ) << 13 << 0; + + QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>() + << ListChange::insert(0, 3) + << ListChange::move(0, 8, 5) + ) << 13 << 11; + + QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>() + << ListChange::setCurrent(9) + << ListChange::insert(10, 3) + << ListChange::move(8, 0, 5) + ) << 13 << 1; + + + QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>() + << ListChange::setCurrent(1) + << ListChange::move(1, 2, 2) + << ListChange::move(2, 1, 2) + ) << 10 << 1; + + QTest::newRow("move forwards then back") << 10 << (QList<ListChange>() + << ListChange::setCurrent(2) + << ListChange::move(1, 2, 3) + << ListChange::move(3, 0, 5) + ) << 10 << 0; + + QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>() + << ListChange::setCurrent(5) + << ListChange::move(5, 0, 1) + << ListChange::remove(0) + ) << 9 << 0; + + QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>() + << ListChange::setCurrent(5) + << ListChange::move(5, 0, 1) + << ListChange::insert(0) + ) << 11 << 1; + + QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>() + << ListChange::setCurrent(1) + << ListChange::move(5, 1, 3) + << ListChange::remove(1, 3) + ) << 7 << 1; + + QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>() + << ListChange::setCurrent(5) + << ListChange::move(5, 1, 3) + << ListChange::insert(1, 5) + ) << 15 << 6; + + QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>() + << ListChange::setCurrent(3) + << ListChange::move(0, 1, 2) + << ListChange::insert(3, 5) + ) << 15 << 8; + + + QTest::newRow("clear current") << 0 << (QList<ListChange>() + << ListChange::insert(0, 5) + << ListChange::setCurrent(-1) + << ListChange::remove(0, 5) + << ListChange::insert(0, 5) + ) << 5 << -1; +} + + +void tst_QQuickGridView::swapWithFirstItem() +{ + // QTBUG_9697 + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + // ensure content position is stable + gridview->setContentY(0); + model.moveItem(10, 0); + QTRY_VERIFY(gridview->contentY() == 0); + + delete canvas; +} + +void tst_QQuickGridView::currentIndex() +{ + QaimModel model; + for (int i = 0; i < 60; i++) + model.addItem("Item" + QString::number(i), QString::number(i)); + + QQuickView *canvas = new QQuickView(0); + canvas->setGeometry(0,0,240,320); + canvas->show(); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + QString filename(testFile("gridview-initCurrent.qml")); + canvas->setSource(QUrl::fromLocalFile(filename)); + + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QVERIFY(gridview != 0); + QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled); + + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + + // current item should be third item + QCOMPARE(gridview->currentIndex(), 35); + QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35)); + QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y()); + QCOMPARE(gridview->contentY(), 400.0); + + gridview->moveCurrentIndexRight(); + QCOMPARE(gridview->currentIndex(), 36); + gridview->moveCurrentIndexDown(); + QCOMPARE(gridview->currentIndex(), 39); + gridview->moveCurrentIndexUp(); + QCOMPARE(gridview->currentIndex(), 36); + gridview->moveCurrentIndexLeft(); + QCOMPARE(gridview->currentIndex(), 35); + + // wait until motion stops + QTRY_VERIFY(gridview->verticalVelocity() == 0.0); + + // no wrap + gridview->setCurrentIndex(0); + QCOMPARE(gridview->currentIndex(), 0); + // confirm that the velocity is updated + QTRY_VERIFY(gridview->verticalVelocity() != 0.0); + + gridview->moveCurrentIndexUp(); + QCOMPARE(gridview->currentIndex(), 0); + + gridview->moveCurrentIndexLeft(); + QCOMPARE(gridview->currentIndex(), 0); + + gridview->setCurrentIndex(model.count()-1); + QCOMPARE(gridview->currentIndex(), model.count()-1); + + gridview->moveCurrentIndexRight(); + QCOMPARE(gridview->currentIndex(), model.count()-1); + + gridview->moveCurrentIndexDown(); + QCOMPARE(gridview->currentIndex(), model.count()-1); + + // with wrap + gridview->setWrapEnabled(true); + + gridview->setCurrentIndex(0); + QCOMPARE(gridview->currentIndex(), 0); + + gridview->moveCurrentIndexLeft(); + QCOMPARE(gridview->currentIndex(), model.count()-1); + + qApp->processEvents(); + QTRY_COMPARE(gridview->contentY(), 880.0); + + gridview->moveCurrentIndexRight(); + QCOMPARE(gridview->currentIndex(), 0); + + QTRY_COMPARE(gridview->contentY(), 0.0); + + + // footer should become visible if it is out of view, and then current index moves to the first row + canvas->rootObject()->setProperty("showFooter", true); + QTRY_VERIFY(gridview->footerItem()); + gridview->setCurrentIndex(model.count()-3); + QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height()); + gridview->setCurrentIndex(model.count()-2); + QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height()); + canvas->rootObject()->setProperty("showFooter", false); + + // header should become visible if it is out of view, and then current index moves to the last row + canvas->rootObject()->setProperty("showHeader", true); + QTRY_VERIFY(gridview->headerItem()); + gridview->setCurrentIndex(3); + QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY()); + gridview->setCurrentIndex(1); + QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height()); + canvas->rootObject()->setProperty("showHeader", false); + + + // Test keys + canvas->requestActivateWindow(); + QTest::qWaitForWindowShown(canvas); + QTRY_VERIFY(qGuiApp->focusWindow() == canvas); + + gridview->setCurrentIndex(0); + + QTest::keyClick(canvas, Qt::Key_Down); + QCOMPARE(gridview->currentIndex(), 3); + + QTest::keyClick(canvas, Qt::Key_Up); + QCOMPARE(gridview->currentIndex(), 0); + + // hold down Key_Down + for (int i=0; i<(model.count() / 3) - 1; i++) { + QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true); + QTRY_COMPARE(gridview->currentIndex(), i*3 + 3); + } + QTest::keyRelease(canvas, Qt::Key_Down); + QTRY_COMPARE(gridview->currentIndex(), 57); + QTRY_COMPARE(gridview->contentY(), 880.0); + + // hold down Key_Up + for (int i=(model.count() / 3) - 1; i > 0; i--) { + QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true); + QTRY_COMPARE(gridview->currentIndex(), i*3 - 3); + } + QTest::keyRelease(canvas, Qt::Key_Up); + QTRY_COMPARE(gridview->currentIndex(), 0); + QTRY_COMPARE(gridview->contentY(), 0.0); + + + gridview->setFlow(QQuickGridView::TopToBottom); + + canvas->requestActivateWindow(); + QTest::qWaitForWindowShown(canvas); + QVERIFY(qGuiApp->focusWindow() == canvas); + qApp->processEvents(); + + QTest::keyClick(canvas, Qt::Key_Right); + QCOMPARE(gridview->currentIndex(), 5); + + QTest::keyClick(canvas, Qt::Key_Left); + QCOMPARE(gridview->currentIndex(), 0); + + QTest::keyClick(canvas, Qt::Key_Down); + QCOMPARE(gridview->currentIndex(), 1); + + QTest::keyClick(canvas, Qt::Key_Up); + QCOMPARE(gridview->currentIndex(), 0); + + // hold down Key_Right + for (int i=0; i<(model.count() / 5) - 1; i++) { + QTest::simulateEvent(canvas, true, Qt::Key_Right, Qt::NoModifier, "", true); + QTRY_COMPARE(gridview->currentIndex(), i*5 + 5); + } + + QTest::keyRelease(canvas, Qt::Key_Right); + QTRY_COMPARE(gridview->currentIndex(), 55); + QTRY_COMPARE(gridview->contentX(), 720.0); + + // hold down Key_Left + for (int i=(model.count() / 5) - 1; i > 0; i--) { + QTest::simulateEvent(canvas, true, Qt::Key_Left, Qt::NoModifier, "", true); + QTRY_COMPARE(gridview->currentIndex(), i*5 - 5); + } + QTest::keyRelease(canvas, Qt::Key_Left); + QTRY_COMPARE(gridview->currentIndex(), 0); + QTRY_COMPARE(gridview->contentX(), 0.0); + + + // turn off auto highlight + gridview->setHighlightFollowsCurrentItem(false); + QVERIFY(gridview->highlightFollowsCurrentItem() == false); + QVERIFY(gridview->highlightItem()); + qreal hlPosX = gridview->highlightItem()->x(); + qreal hlPosY = gridview->highlightItem()->y(); + + gridview->setCurrentIndex(5); + QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX); + QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY); + + // insert item before currentIndex + gridview->setCurrentIndex(28); + model.insertItem(0, "Foo", "1111"); + QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29); + + // check removing highlight by setting currentIndex to -1; + gridview->setCurrentIndex(-1); + + QCOMPARE(gridview->currentIndex(), -1); + QVERIFY(!gridview->highlightItem()); + QVERIFY(!gridview->currentItem()); + + gridview->setHighlightFollowsCurrentItem(true); + + gridview->setFlow(QQuickGridView::LeftToRight); + gridview->setLayoutDirection(Qt::RightToLeft); + + canvas->requestActivateWindow(); + QTest::qWaitForWindowShown(canvas); + QTRY_VERIFY(qGuiApp->focusWindow() == canvas); + qApp->processEvents(); + + gridview->setCurrentIndex(35); + + QTest::keyClick(canvas, Qt::Key_Right); + QCOMPARE(gridview->currentIndex(), 34); + + QTest::keyClick(canvas, Qt::Key_Down); + QCOMPARE(gridview->currentIndex(), 37); + + QTest::keyClick(canvas, Qt::Key_Up); + QCOMPARE(gridview->currentIndex(), 34); + + QTest::keyClick(canvas, Qt::Key_Left); + QCOMPARE(gridview->currentIndex(), 35); + + + // turn off auto highlight + gridview->setHighlightFollowsCurrentItem(false); + QVERIFY(gridview->highlightFollowsCurrentItem() == false); + QVERIFY(gridview->highlightItem()); + hlPosX = gridview->highlightItem()->x(); + hlPosY = gridview->highlightItem()->y(); + + gridview->setCurrentIndex(5); + QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX); + QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY); + + // insert item before currentIndex + gridview->setCurrentIndex(28); + model.insertItem(0, "Foo", "1111"); + QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29); + + // check removing highlight by setting currentIndex to -1; + gridview->setCurrentIndex(-1); + + QCOMPARE(gridview->currentIndex(), -1); + QVERIFY(!gridview->highlightItem()); + QVERIFY(!gridview->currentItem()); + + delete canvas; +} + +void tst_QQuickGridView::noCurrentIndex() +{ + QaimModel model; + for (int i = 0; i < 60; i++) + model.addItem("Item" + QString::number(i), QString::number(i)); + + QQuickView *canvas = new QQuickView(0); + canvas->setGeometry(0,0,240,320); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + QString filename(testFile("gridview-noCurrent.qml")); + canvas->setSource(QUrl::fromLocalFile(filename)); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QVERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // current index should be -1 + QCOMPARE(gridview->currentIndex(), -1); + QVERIFY(!gridview->currentItem()); + QVERIFY(!gridview->highlightItem()); + QCOMPARE(gridview->contentY(), 0.0); + + gridview->setCurrentIndex(5); + QCOMPARE(gridview->currentIndex(), 5); + QVERIFY(gridview->currentItem()); + QVERIFY(gridview->highlightItem()); + + delete canvas; +} + +void tst_QQuickGridView::changeFlow() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), QString::number(i)); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + // Confirm items positioned correctly and indexes correct + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal((i%3)*80)); + QTRY_COMPARE(item->y(), qreal((i/3)*60)); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + + ctxt->setContextProperty("testTopToBottom", QVariant(true)); + + // Confirm items positioned correctly and indexes correct + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal((i/5)*80)); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + + // Confirm items positioned correctly and indexes correct + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + gridview->setContentX(100); + QTRY_COMPARE(gridview->contentX(), 100.); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + QTRY_COMPARE(gridview->contentX(), 0.); + + // Confirm items positioned correctly and indexes correct + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80)); + QTRY_COMPARE(item->y(), qreal((i/3)*60)); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::defaultValues() +{ + QQmlEngine engine; + QQmlComponent c(&engine, testFileUrl("gridview3.qml")); + QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create()); + + QTRY_VERIFY(obj != 0); + QTRY_VERIFY(obj->model() == QVariant()); + QTRY_VERIFY(obj->delegate() == 0); + QTRY_COMPARE(obj->currentIndex(), -1); + QTRY_VERIFY(obj->currentItem() == 0); + QTRY_COMPARE(obj->count(), 0); + QTRY_VERIFY(obj->highlight() == 0); + QTRY_VERIFY(obj->highlightItem() == 0); + QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true); + QTRY_VERIFY(obj->flow() == 0); + QTRY_COMPARE(obj->isWrapEnabled(), false); + QTRY_COMPARE(obj->cacheBuffer(), 0); + QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default? + QTRY_COMPARE(obj->cellHeight(), qreal(100)); + delete obj; +} + +void tst_QQuickGridView::properties() +{ + QQmlEngine engine; + QQmlComponent c(&engine, testFileUrl("gridview2.qml")); + QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create()); + + QTRY_VERIFY(obj != 0); + QTRY_VERIFY(obj->model() != QVariant()); + QTRY_VERIFY(obj->delegate() != 0); + QTRY_COMPARE(obj->currentIndex(), 0); + QTRY_VERIFY(obj->currentItem() != 0); + QTRY_COMPARE(obj->count(), 4); + QTRY_VERIFY(obj->highlight() != 0); + QTRY_VERIFY(obj->highlightItem() != 0); + QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false); + QTRY_VERIFY(obj->flow() == 0); + QTRY_COMPARE(obj->isWrapEnabled(), true); + QTRY_COMPARE(obj->cacheBuffer(), 200); + QTRY_COMPARE(obj->cellWidth(), qreal(100)); + QTRY_COMPARE(obj->cellHeight(), qreal(100)); + delete obj; +} + +void tst_QQuickGridView::propertyChanges() +{ + QQuickView *canvas = createView(); + QTRY_VERIFY(canvas); + canvas->setSource(testFileUrl("propertychangestest.qml")); + + QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView"); + QTRY_VERIFY(gridView); + + QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged())); + QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged())); + QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged())); + QSignalSpy flowSpy(gridView, SIGNAL(flowChanged())); + + QTRY_COMPARE(gridView->isWrapEnabled(), true); + QTRY_COMPARE(gridView->cacheBuffer(), 10); + QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight); + + gridView->setWrapEnabled(false); + gridView->setCacheBuffer(3); + gridView->setFlow(QQuickGridView::TopToBottom); + + QTRY_COMPARE(gridView->isWrapEnabled(), false); + QTRY_COMPARE(gridView->cacheBuffer(), 3); + QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom); + + QTRY_COMPARE(keyNavigationWrapsSpy.count(),1); + QTRY_COMPARE(cacheBufferSpy.count(),1); + QTRY_COMPARE(flowSpy.count(),1); + + gridView->setWrapEnabled(false); + gridView->setCacheBuffer(3); + gridView->setFlow(QQuickGridView::TopToBottom); + + QTRY_COMPARE(keyNavigationWrapsSpy.count(),1); + QTRY_COMPARE(cacheBufferSpy.count(),1); + QTRY_COMPARE(flowSpy.count(),1); + + gridView->setFlow(QQuickGridView::LeftToRight); + QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight); + + gridView->setWrapEnabled(true); + gridView->setCacheBuffer(5); + gridView->setLayoutDirection(Qt::RightToLeft); + + QTRY_COMPARE(gridView->isWrapEnabled(), true); + QTRY_COMPARE(gridView->cacheBuffer(), 5); + QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft); + + QTRY_COMPARE(keyNavigationWrapsSpy.count(),2); + QTRY_COMPARE(cacheBufferSpy.count(),2); + QTRY_COMPARE(layoutSpy.count(),1); + QTRY_COMPARE(flowSpy.count(),2); + + gridView->setWrapEnabled(true); + gridView->setCacheBuffer(5); + gridView->setLayoutDirection(Qt::RightToLeft); + + QTRY_COMPARE(keyNavigationWrapsSpy.count(),2); + QTRY_COMPARE(cacheBufferSpy.count(),2); + QTRY_COMPARE(layoutSpy.count(),1); + QTRY_COMPARE(flowSpy.count(),2); + + gridView->setFlow(QQuickGridView::TopToBottom); + QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom); + QTRY_COMPARE(flowSpy.count(),3); + + gridView->setFlow(QQuickGridView::TopToBottom); + QTRY_COMPARE(flowSpy.count(),3); + + delete canvas; +} + +void tst_QQuickGridView::componentChanges() +{ + QQuickView *canvas = createView(); + QTRY_VERIFY(canvas); + canvas->setSource(testFileUrl("propertychangestest.qml")); + + QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView"); + QTRY_VERIFY(gridView); + + QQmlComponent component(canvas->engine()); + component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile("")); + + QQmlComponent delegateComponent(canvas->engine()); + delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile("")); + + QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged())); + QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged())); + QSignalSpy headerSpy(gridView, SIGNAL(headerChanged())); + QSignalSpy footerSpy(gridView, SIGNAL(footerChanged())); + + gridView->setHighlight(&component); + gridView->setDelegate(&delegateComponent); + gridView->setHeader(&component); + gridView->setFooter(&component); + + QTRY_COMPARE(gridView->highlight(), &component); + QTRY_COMPARE(gridView->delegate(), &delegateComponent); + QTRY_COMPARE(gridView->header(), &component); + QTRY_COMPARE(gridView->footer(), &component); + + QTRY_COMPARE(highlightSpy.count(),1); + QTRY_COMPARE(delegateSpy.count(),1); + QTRY_COMPARE(headerSpy.count(),1); + QTRY_COMPARE(footerSpy.count(),1); + + gridView->setHighlight(&component); + gridView->setDelegate(&delegateComponent); + gridView->setHeader(&component); + gridView->setFooter(&component); + + QTRY_COMPARE(highlightSpy.count(),1); + QTRY_COMPARE(delegateSpy.count(),1); + QTRY_COMPARE(headerSpy.count(),1); + QTRY_COMPARE(footerSpy.count(),1); + + delete canvas; +} + +void tst_QQuickGridView::modelChanges() +{ + QQuickView *canvas = createView(); + QTRY_VERIFY(canvas); + canvas->setSource(testFileUrl("propertychangestest.qml")); + + QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView"); + QTRY_VERIFY(gridView); + + QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel"); + QTRY_VERIFY(alternateModel); + QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel); + QSignalSpy modelSpy(gridView, SIGNAL(modelChanged())); + + gridView->setModel(modelVariant); + QTRY_COMPARE(gridView->model(), modelVariant); + QTRY_COMPARE(modelSpy.count(),1); + + gridView->setModel(modelVariant); + QTRY_COMPARE(modelSpy.count(),1); + + gridView->setModel(QVariant()); + QTRY_COMPARE(modelSpy.count(),2); + delete canvas; +} + +void tst_QQuickGridView::positionViewAtIndex() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // Confirm items positioned correctly + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.); + QTRY_COMPARE(item->y(), (i/3)*60.); + } + + // Position on a currently visible item + gridview->positionViewAtIndex(4, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->indexAt(120, 90), 4); + QTRY_COMPARE(gridview->contentY(), 60.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.); + QTRY_COMPARE(item->y(), (i/3)*60.); + } + + // Position on an item beyond the visible items + gridview->positionViewAtIndex(21, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->indexAt(40, 450), 21); + QTRY_COMPARE(gridview->contentY(), 420.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.); + QTRY_COMPARE(item->y(), (i/3)*60.); + } + + // Position on an item that would leave empty space if positioned at the top + gridview->positionViewAtIndex(31, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->indexAt(120, 630), 31); + QTRY_COMPARE(gridview->contentY(), 520.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.); + QTRY_COMPARE(item->y(), (i/3)*60.); + } + + // Position at the beginning again + gridview->positionViewAtIndex(0, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->indexAt(0, 0), 0); + QTRY_COMPARE(gridview->indexAt(40, 30), 0); + QTRY_COMPARE(gridview->indexAt(80, 60), 4); + QTRY_COMPARE(gridview->contentY(), 0.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.); + QTRY_COMPARE(item->y(), (i/3)*60.); + } + + // Position at End + gridview->positionViewAtIndex(30, QQuickGridView::End); + QTRY_COMPARE(gridview->contentY(), 340.); + + // Position in Center + gridview->positionViewAtIndex(15, QQuickGridView::Center); + QTRY_COMPARE(gridview->contentY(), 170.); + + // Ensure at least partially visible + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentY(), 170.); + + gridview->setContentY(302); + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentY(), 302.); + + gridview->setContentY(360); + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentY(), 300.); + + gridview->setContentY(60); + gridview->positionViewAtIndex(20, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentY(), 60.); + + gridview->setContentY(20); + gridview->positionViewAtIndex(20, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentY(), 100.); + + // Ensure completely visible + gridview->setContentY(120); + gridview->positionViewAtIndex(20, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentY(), 120.); + + gridview->setContentY(302); + gridview->positionViewAtIndex(15, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentY(), 300.); + + gridview->setContentY(60); + gridview->positionViewAtIndex(20, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentY(), 100.); + + // Test for Top To Bottom layout + ctxt->setContextProperty("testTopToBottom", QVariant(true)); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), (i/5)*80.); + QTRY_COMPARE(item->y(), (i%5)*60.); + } + + // Position at End + gridview->positionViewAtIndex(30, QQuickGridView::End); + QTRY_COMPARE(gridview->contentX(), 320.); + QTRY_COMPARE(gridview->contentY(), 0.); + + // Position in Center + gridview->positionViewAtIndex(15, QQuickGridView::Center); + QTRY_COMPARE(gridview->contentX(), 160.); + + // Ensure at least partially visible + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), 160.); + + gridview->setContentX(170); + gridview->positionViewAtIndex(25, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), 170.); + + gridview->positionViewAtIndex(30, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), 320.); + + gridview->setContentX(170); + gridview->positionViewAtIndex(25, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentX(), 240.); + + // positionViewAtBeginning + gridview->positionViewAtBeginning(); + QTRY_COMPARE(gridview->contentX(), 0.); + + gridview->setContentX(80); + canvas->rootObject()->setProperty("showHeader", true); + gridview->positionViewAtBeginning(); + QTRY_COMPARE(gridview->contentX(), -30.); + + // positionViewAtEnd + gridview->positionViewAtEnd(); + QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns) + + gridview->setContentX(80); + canvas->rootObject()->setProperty("showFooter", true); + gridview->positionViewAtEnd(); + QTRY_COMPARE(gridview->contentX(), 430.); + + // set current item to outside visible view, position at beginning + // and ensure highlight moves to current item + gridview->setCurrentIndex(6); + gridview->positionViewAtBeginning(); + QTRY_COMPARE(gridview->contentX(), -30.); + QVERIFY(gridview->highlightItem()); + QCOMPARE(gridview->highlightItem()->x(), 80.); + + delete canvas; +} + +void tst_QQuickGridView::snapping() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + gridview->setHeight(220); + QCOMPARE(gridview->height(), 220.); + + gridview->positionViewAtIndex(12, QQuickGridView::Visible); + QCOMPARE(gridview->contentY(), 80.); + + gridview->setContentY(0); + QCOMPARE(gridview->contentY(), 0.); + + gridview->setSnapMode(QQuickGridView::SnapToRow); + QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow); + + gridview->positionViewAtIndex(12, QQuickGridView::Visible); + QCOMPARE(gridview->contentY(), 60.); + + gridview->positionViewAtIndex(15, QQuickGridView::End); + QCOMPARE(gridview->contentY(), 120.); + + delete canvas; + +} + +void tst_QQuickGridView::mirroring() +{ + QQuickView *canvasA = createView(); + canvasA->setSource(testFileUrl("mirroring.qml")); + QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view"); + QTRY_VERIFY(gridviewA != 0); + + QQuickView *canvasB = createView(); + canvasB->setSource(testFileUrl("mirroring.qml")); + QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view"); + QTRY_VERIFY(gridviewA != 0); + qApp->processEvents(); + + QList<QString> objectNames; + objectNames << "item1" << "item2"; // << "item3" + + gridviewA->setProperty("layoutDirection", Qt::LeftToRight); + gridviewB->setProperty("layoutDirection", Qt::RightToLeft); + QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection()); + + // LTR != RTL + foreach (const QString objectName, objectNames) + QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x()); + + gridviewA->setProperty("layoutDirection", Qt::LeftToRight); + gridviewB->setProperty("layoutDirection", Qt::LeftToRight); + + // LTR == LTR + foreach (const QString objectName, objectNames) + QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x()); + + QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection()); + QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true); + QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection()); + + // LTR != LTR+mirror + foreach (const QString objectName, objectNames) + QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x()); + + gridviewA->setProperty("layoutDirection", Qt::RightToLeft); + + // RTL == LTR+mirror + foreach (const QString objectName, objectNames) + QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x()); + + gridviewB->setProperty("layoutDirection", Qt::RightToLeft); + + // RTL != RTL+mirror + foreach (const QString objectName, objectNames) + QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x()); + + gridviewA->setProperty("layoutDirection", Qt::LeftToRight); + + // LTR == RTL+mirror + foreach (const QString objectName, objectNames) + QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x()); + + delete canvasA; + delete canvasB; +} + +void tst_QQuickGridView::positionViewAtIndex_rightToLeft() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testTopToBottom", QVariant(true)); + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + // Confirm items positioned correctly + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position on a currently visible item + gridview->positionViewAtIndex(6, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -320.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position on an item beyond the visible items + gridview->positionViewAtIndex(21, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -560.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position on an item that would leave empty space if positioned at the top + gridview->positionViewAtIndex(31, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -640.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position at the beginning again + gridview->positionViewAtIndex(0, QQuickGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -240.); + + // Confirm items positioned correctly + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position at End + gridview->positionViewAtIndex(30, QQuickGridView::End); + QTRY_COMPARE(gridview->contentX(), -560.); + + // Position in Center + gridview->positionViewAtIndex(15, QQuickGridView::Center); + QTRY_COMPARE(gridview->contentX(), -400.); + + // Ensure at least partially visible + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -400.); + + gridview->setContentX(-555.); + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -555.); + + gridview->setContentX(-239); + gridview->positionViewAtIndex(15, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -320.); + + gridview->setContentX(-239); + gridview->positionViewAtIndex(20, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -400.); + + gridview->setContentX(-640); + gridview->positionViewAtIndex(20, QQuickGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -560.); + + // Ensure completely visible + gridview->setContentX(-400); + gridview->positionViewAtIndex(20, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentX(), -400.); + + gridview->setContentX(-315); + gridview->positionViewAtIndex(15, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentX(), -320.); + + gridview->setContentX(-640); + gridview->positionViewAtIndex(20, QQuickGridView::Contain); + QTRY_COMPARE(gridview->contentX(), -560.); + + delete canvas; +} + +void tst_QQuickGridView::resetModel() +{ + QQuickView *canvas = createView(); + + QStringList strings; + strings << "one" << "two" << "three"; + QStringListModel model(strings); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->setSource(testFileUrl("displaygrid.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QTRY_COMPARE(gridview->count(), model.rowCount()); + + for (int i = 0; i < model.rowCount(); ++i) { + QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i); + QTRY_VERIFY(display != 0); + QTRY_COMPARE(display->text(), strings.at(i)); + } + + strings.clear(); + strings << "four" << "five" << "six" << "seven"; + model.setStringList(strings); + + QTRY_COMPARE(gridview->count(), model.rowCount()); + + for (int i = 0; i < model.rowCount(); ++i) { + QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i); + QTRY_VERIFY(display != 0); + QTRY_COMPARE(display->text(), strings.at(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::enforceRange() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview-enforcerange.qml")); + canvas->show(); + qApp->processEvents(); + QVERIFY(canvas->rootObject() != 0); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0); + QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0); + QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + // view should be positioned at the top of the range. + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0); + QTRY_VERIFY(item); + QTRY_COMPARE(gridview->contentY(), -100.0); + + QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(0)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(0)); + + // Check currentIndex is updated when contentItem moves + gridview->setContentY(0); + QTRY_COMPARE(gridview->currentIndex(), 2); + + gridview->setCurrentIndex(5); + QTRY_COMPARE(gridview->contentY(), 100.); + + QaimModel model2; + for (int i = 0; i < 5; i++) + model2.addItem("Item" + QString::number(i), ""); + + ctxt->setContextProperty("testModel", &model2); + QCOMPARE(gridview->count(), 5); + + delete canvas; +} + +void tst_QQuickGridView::enforceRange_rightToLeft() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + ctxt->setContextProperty("testTopToBottom", QVariant(true)); + + canvas->setSource(testFileUrl("gridview-enforcerange.qml")); + qApp->processEvents(); + QVERIFY(canvas->rootObject() != 0); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QCOMPARE(gridview->preferredHighlightBegin(), 100.0); + QCOMPARE(gridview->preferredHighlightEnd(), 100.0); + QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange); + + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + + // view should be positioned at the top of the range. + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0); + QVERIFY(item); + QTRY_COMPARE(gridview->contentX(), -140.); + QTRY_COMPARE(gridview->contentY(), 0.0); + + QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(0)); + QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(0)); + + // Check currentIndex is updated when contentItem moves + gridview->setContentX(-240); + QTRY_COMPARE(gridview->currentIndex(), 3); + + gridview->setCurrentIndex(7); + QTRY_COMPARE(gridview->contentX(), -340.); + QTRY_COMPARE(gridview->contentY(), 0.0); + + QaimModel model2; + for (int i = 0; i < 5; i++) + model2.addItem("Item" + QString::number(i), ""); + + ctxt->setContextProperty("testModel", &model2); + QCOMPARE(gridview->count(), 5); + + delete canvas; +} + +void tst_QQuickGridView::QTBUG_8456() +{ + QQuickView *canvas = createView(); + + canvas->setSource(testFileUrl("setindex.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QTRY_COMPARE(gridview->currentIndex(), 0); + + delete canvas; +} + +void tst_QQuickGridView::manualHighlight() +{ + QQuickView *canvas = createView(); + + QString filename(testFile("manual-highlight.qml")); + canvas->setSource(QUrl::fromLocalFile(filename)); + + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QTRY_COMPARE(gridview->currentIndex(), 0); + QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0)); + QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y()); + QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x()); + + gridview->setCurrentIndex(2); + + QTRY_COMPARE(gridview->currentIndex(), 2); + QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2)); + QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y()); + QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x()); + + gridview->positionViewAtIndex(8, QQuickGridView::Contain); + + QTRY_COMPARE(gridview->currentIndex(), 2); + QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2)); + QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y()); + QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x()); + + gridview->setFlow(QQuickGridView::TopToBottom); + QTRY_COMPARE(gridview->flow(), QQuickGridView::TopToBottom); + + gridview->setCurrentIndex(0); + QTRY_COMPARE(gridview->currentIndex(), 0); + QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0)); + QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y()); + QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x()); + + delete canvas; +} + + +void tst_QQuickGridView::footer() +{ + QFETCH(QQuickGridView::Flow, flow); + QFETCH(Qt::LayoutDirection, layoutDirection); + QFETCH(QPointF, initialFooterPos); + QFETCH(QPointF, changedFooterPos); + QFETCH(QPointF, initialContentPos); + QFETCH(QPointF, changedContentPos); + QFETCH(QPointF, firstDelegatePos); + QFETCH(QPointF, resizeContentPos); + + QQuickView *canvas = createView(); + canvas->show(); + + QaimModel model; + for (int i = 0; i < 7; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->setSource(testFileUrl("footer.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + gridview->setFlow(flow); + gridview->setLayoutDirection(layoutDirection); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QQuickText *footer = findItem<QQuickText>(contentItem, "footer"); + QVERIFY(footer); + + QVERIFY(footer == gridview->footerItem()); + + QCOMPARE(footer->pos(), initialFooterPos); + QCOMPARE(footer->width(), 100.); + QCOMPARE(footer->height(), 30.); + QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos); + + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0); + QVERIFY(item); + QCOMPARE(item->pos(), firstDelegatePos); + + if (flow == QQuickGridView::LeftToRight) { + // shrink by one row + model.removeItem(2); + QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight()); + } else { + // shrink by one column + model.removeItem(2); + model.removeItem(3); + if (layoutDirection == Qt::LeftToRight) + QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth()); + else + QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth()); + } + + // remove all items + model.clear(); + + QPointF posWhenNoItems(0, 0); + if (layoutDirection == Qt::RightToLeft) + posWhenNoItems.setX(flow == QQuickGridView::LeftToRight ? gridview->width() - footer->width() : -footer->width()); + QTRY_COMPARE(footer->pos(), posWhenNoItems); + + // if header is present, it's at a negative pos, so the footer should not move + canvas->rootObject()->setProperty("showHeader", true); + QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0); + QTRY_COMPARE(footer->pos(), posWhenNoItems); + canvas->rootObject()->setProperty("showHeader", false); + + // add 30 items + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged())); + QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter"); + + QCOMPARE(footerItemSpy.count(), 1); + + footer = findItem<QQuickText>(contentItem, "footer"); + QVERIFY(!footer); + footer = findItem<QQuickText>(contentItem, "footer2"); + QVERIFY(footer); + + QVERIFY(footer == gridview->footerItem()); + + QCOMPARE(footer->pos(), changedFooterPos); + QCOMPARE(footer->width(), 50.); + QCOMPARE(footer->height(), 20.); + QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos); + + item = findItem<QQuickItem>(contentItem, "wrapper", 0); + QVERIFY(item); + QCOMPARE(item->pos(), firstDelegatePos); + + gridview->positionViewAtEnd(); + footer->setHeight(10); + footer->setWidth(40); + QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos); + + delete canvas; +} + +void tst_QQuickGridView::footer_data() +{ + QTest::addColumn<QQuickGridView::Flow>("flow"); + QTest::addColumn<Qt::LayoutDirection>("layoutDirection"); + QTest::addColumn<QPointF>("initialFooterPos"); + QTest::addColumn<QPointF>("changedFooterPos"); + QTest::addColumn<QPointF>("initialContentPos"); + QTest::addColumn<QPointF>("changedContentPos"); + QTest::addColumn<QPointF>("firstDelegatePos"); + QTest::addColumn<QPointF>("resizeContentPos"); + + // footer1 = 100 x 30 + // footer2 = 50 x 20 + // cells = 80 * 60 + // view width = 240 + // view height = 320 + + // footer below items, bottom left + QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight + << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60) + << QPointF(0, 10 * 60) // 30 items = 10 rows + << QPointF(0, 0) + << QPointF(0, 0) + << QPointF(0, 0) + << QPointF(0, 10 * 60 - 320 + 10); + + // footer below items, bottom right + QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft + << QPointF(240 - 100, 3 * 60) + << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers + << QPointF(0, 0) + << QPointF(0, 0) + << QPointF(240 - 80, 0) + << QPointF(0, 10 * 60 - 320 + 10); + + // footer to right of items + QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight + << QPointF(2 * 80, 0) // 2 columns, cell width 80 + << QPointF(6 * 80, 0) // 30 items = 6 columns + << QPointF(0, 0) + << QPointF(0, 0) + << QPointF(0, 0) + << QPointF(6 * 80 - 240 + 40, 0); + + // footer to left of items + QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft + << QPointF(-(2 * 80) - 100, 0) + << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width + << QPointF(-240, 0) + << QPointF(-240, 0) // unchanged, footer change doesn't change content pos + << QPointF(-80, 0) + << QPointF(-(6 * 80) - 40, 0); +} + +void tst_QQuickGridView::header() +{ + QFETCH(QQuickGridView::Flow, flow); + QFETCH(Qt::LayoutDirection, layoutDirection); + QFETCH(QPointF, initialHeaderPos); + QFETCH(QPointF, changedHeaderPos); + QFETCH(QPointF, initialContentPos); + QFETCH(QPointF, changedContentPos); + QFETCH(QPointF, firstDelegatePos); + QFETCH(QPointF, resizeContentPos); + + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QQuickView *canvas = createView(); + canvas->rootContext()->setContextProperty("testModel", &model); + canvas->rootContext()->setContextProperty("initialViewWidth", 240); + canvas->rootContext()->setContextProperty("initialViewHeight", 320); + canvas->setSource(testFileUrl("header.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + gridview->setFlow(flow); + gridview->setLayoutDirection(layoutDirection); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QQuickText *header = findItem<QQuickText>(contentItem, "header"); + QVERIFY(header); + + QVERIFY(header == gridview->headerItem()); + + QCOMPARE(header->pos(), initialHeaderPos); + QCOMPARE(header->width(), 100.); + QCOMPARE(header->height(), 30.); + QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos); + + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0); + QVERIFY(item); + QCOMPARE(item->pos(), firstDelegatePos); + + model.clear(); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is + + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged())); + QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader"); + + QCOMPARE(headerItemSpy.count(), 1); + + header = findItem<QQuickText>(contentItem, "header"); + QVERIFY(!header); + header = findItem<QQuickText>(contentItem, "header2"); + QVERIFY(header); + + QVERIFY(header == gridview->headerItem()); + + QCOMPARE(header->pos(), changedHeaderPos); + QCOMPARE(header->width(), 50.); + QCOMPARE(header->height(), 20.); + QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos); + + item = findItem<QQuickItem>(contentItem, "wrapper", 0); + QVERIFY(item); + QCOMPARE(item->pos(), firstDelegatePos); + + header->setHeight(10); + header->setWidth(40); + QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos); + + delete canvas; + + + // QTBUG-21207 header should become visible if view resizes from initial empty size + + canvas = createView(); + canvas->rootContext()->setContextProperty("testModel", &model); + canvas->rootContext()->setContextProperty("initialViewWidth", 240); + canvas->rootContext()->setContextProperty("initialViewHeight", 320); + canvas->setSource(testFileUrl("header.qml")); + canvas->show(); + qApp->processEvents(); + + gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + gridview->setFlow(flow); + gridview->setLayoutDirection(layoutDirection); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + gridview->setWidth(240); + gridview->setHeight(320); + QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos); + QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos); + + delete canvas; +} + +void tst_QQuickGridView::header_data() +{ + QTest::addColumn<QQuickGridView::Flow>("flow"); + QTest::addColumn<Qt::LayoutDirection>("layoutDirection"); + QTest::addColumn<QPointF>("initialHeaderPos"); + QTest::addColumn<QPointF>("changedHeaderPos"); + QTest::addColumn<QPointF>("initialContentPos"); + QTest::addColumn<QPointF>("changedContentPos"); + QTest::addColumn<QPointF>("firstDelegatePos"); + QTest::addColumn<QPointF>("resizeContentPos"); + + // header1 = 100 x 30 + // header2 = 50 x 20 + // cells = 80 x 60 + // view width = 240 + + // header above items, top left + QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight + << QPointF(0, -30) + << QPointF(0, -20) + << QPointF(0, -30) + << QPointF(0, -20) + << QPointF(0, 0) + << QPointF(0, -10); + + // header above items, top right + QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft + << QPointF(240 - 100, -30) + << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers + << QPointF(0, -30) + << QPointF(0, -20) + << QPointF(160, 0) + << QPointF(0, -10); + + // header to left of items + QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight + << QPointF(-100, 0) + << QPointF(-50, 0) + << QPointF(-100, 0) + << QPointF(-50, 0) + << QPointF(0, 0) + << QPointF(-40, 0); + + // header to right of items + QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft + << QPointF(0, 0) + << QPointF(0, 0) + << QPointF(-(240 - 100), 0) + << QPointF(-(240 - 50), 0) + << QPointF(-80, 0) + << QPointF(-(240 - 40), 0); +} + +void tst_QQuickGridView::resizeViewAndRepaint() +{ + QQuickView *canvas = createView(); + canvas->show(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("initialWidth", 240); + ctxt->setContextProperty("initialHeight", 100); + + canvas->setSource(testFileUrl("resizeview.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // item at index 10 should not be currently visible + QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10)); + + gridview->setHeight(320); + QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10)); + + gridview->setHeight(100); + QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10)); + + // Ensure we handle -ve sizes + gridview->setHeight(-100); + QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3); + + gridview->setCacheBuffer(120); + QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9); + + // ensure items in cache become visible + gridview->setHeight(120); + QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15); + + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal((i%3)*80)); + QTRY_COMPARE(item->y(), qreal((i/3)*60)); + QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible + } + + // ensure items outside view become invisible + gridview->setHeight(60); + QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12); + + itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal((i%3)*80)); + QTRY_COMPARE(item->y(), qreal((i/3)*60)); + QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible + } + + delete canvas; +} + +void tst_QQuickGridView::changeColumnCount() +{ + QmlListModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("initialWidth", 100); + ctxt->setContextProperty("initialHeight", 320); + canvas->setSource(testFileUrl("resizeview.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + // a single column of 6 items are visible + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + QCOMPARE(itemCount, 6); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), 0.0); + QCOMPARE(item->y(), qreal(i*60)); + } + + // now 6x3 grid is visible, plus 1 extra below for refill + gridview->setWidth(240); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + QCOMPARE(itemCount, 6*3 + 1); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), qreal((i%3)*80)); + QCOMPARE(item->y(), qreal((i/3)*60)); + } + + // back to single column + gridview->setWidth(100); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + QCOMPARE(itemCount, 6); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), 0.0); + QCOMPARE(item->y(), qreal(i*60)); + } + + delete canvas; +} + +void tst_QQuickGridView::indexAt_itemAt_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<int>("index"); + + QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0; + QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0; + QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1; + QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3; + QTest::newRow("No Item - 240, 0") << 240. << 0. << -1; +} + +void tst_QQuickGridView::indexAt_itemAt() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(int, index); + + QQuickView *canvas = createView(); + + QaimModel model; + model.addItem("Fred", "12345"); + model.addItem("John", "2345"); + model.addItem("Bob", "54321"); + model.addItem("Billy", "22345"); + model.addItem("Sam", "2945"); + model.addItem("Ben", "04321"); + model.addItem("Jim", "0780"); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QTRY_COMPARE(gridview->count(), model.count()); + + QQuickItem *item = 0; + if (index >= 0) { + item = findItem<QQuickItem>(contentItem, "wrapper", index); + QVERIFY(item); + } + QCOMPARE(gridview->indexAt(x, y), index); + QVERIFY(gridview->itemAt(x, y) == item); + + delete canvas; +} + +void tst_QQuickGridView::onAdd() +{ + QFETCH(int, initialItemCount); + QFETCH(int, itemsToAdd); + + const int delegateWidth = 50; + const int delegateHeight = 100; + QaimModel model; + QQuickView *canvas = createView(); + canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit + + // these initial items should not trigger GridView.onAdd + for (int i=0; i<initialItemCount; i++) + model.addItem("dummy value", "dummy value"); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("delegateWidth", delegateWidth); + ctxt->setContextProperty("delegateHeight", delegateHeight); + canvas->setSource(testFileUrl("attachedSignals.qml")); + + QObject *object = canvas->rootObject(); + object->setProperty("width", canvas->width()); + object->setProperty("height", canvas->height()); + qApp->processEvents(); + + QList<QPair<QString, QString> > items; + for (int i=0; i<itemsToAdd; i++) + items << qMakePair(QString("value %1").arg(i), QString::number(i)); + model.addItems(items); + + QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count()); + qApp->processEvents(); + + QVariantList result = object->property("addedDelegates").toList(); + QTRY_COMPARE(result.count(), items.count()); + for (int i=0; i<items.count(); i++) + QCOMPARE(result[i].toString(), items[i].first); + + delete canvas; +} + +void tst_QQuickGridView::onAdd_data() +{ + QTest::addColumn<int>("initialItemCount"); + QTest::addColumn<int>("itemsToAdd"); + + QTest::newRow("0, add 1") << 0 << 1; + QTest::newRow("0, add 2") << 0 << 2; + QTest::newRow("0, add 10") << 0 << 10; + + QTest::newRow("1, add 1") << 1 << 1; + QTest::newRow("1, add 2") << 1 << 2; + QTest::newRow("1, add 10") << 1 << 10; + + QTest::newRow("5, add 1") << 5 << 1; + QTest::newRow("5, add 2") << 5 << 2; + QTest::newRow("5, add 10") << 5 << 10; +} + +void tst_QQuickGridView::onRemove() +{ + QFETCH(int, initialItemCount); + QFETCH(int, indexToRemove); + QFETCH(int, removeCount); + + const int delegateWidth = 50; + const int delegateHeight = 100; + QaimModel model; + for (int i=0; i<initialItemCount; i++) + model.addItem(QString("value %1").arg(i), "dummy value"); + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("delegateWidth", delegateWidth); + ctxt->setContextProperty("delegateHeight", delegateHeight); + canvas->setSource(testFileUrl("attachedSignals.qml")); + QObject *object = canvas->rootObject(); + + model.removeItems(indexToRemove, removeCount); + QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count()); + QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount)); + + delete canvas; +} + +void tst_QQuickGridView::onRemove_data() +{ + QTest::addColumn<int>("initialItemCount"); + QTest::addColumn<int>("indexToRemove"); + QTest::addColumn<int>("removeCount"); + + QTest::newRow("remove first") << 1 << 0 << 1; + QTest::newRow("two items, remove first") << 2 << 0 << 1; + QTest::newRow("two items, remove last") << 2 << 1 << 1; + QTest::newRow("two items, remove all") << 2 << 0 << 2; + + QTest::newRow("four items, remove first") << 4 << 0 << 1; + QTest::newRow("four items, remove 0-2") << 4 << 0 << 2; + QTest::newRow("four items, remove 1-3") << 4 << 1 << 2; + QTest::newRow("four items, remove 2-4") << 4 << 2 << 2; + QTest::newRow("four items, remove last") << 4 << 3 << 1; + QTest::newRow("four items, remove all") << 4 << 0 << 4; + + QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8; + QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5; + QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6; +} + +void tst_QQuickGridView::columnCount() +{ + QQuickView canvas; + canvas.setSource(testFileUrl("gridview4.qml")); + canvas.show(); + canvas.requestActivateWindow(); + QTest::qWaitForWindowShown(&canvas); + + QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject()); + + QCOMPARE(view->cellWidth(), qreal(405)/qreal(9)); + QCOMPARE(view->cellHeight(), qreal(100)); + + QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate"); + QCOMPARE(items.size(), 18); + QCOMPARE(items.at(8)->y(), qreal(0)); + QCOMPARE(items.at(9)->y(), qreal(100)); +} + +void tst_QQuickGridView::margins() +{ + { + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + + canvas->setSource(testFileUrl("margins.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QCOMPARE(gridview->contentX(), -30.); + QCOMPARE(gridview->xOrigin(), 0.); + + // check end bound + gridview->positionViewAtEnd(); + qreal pos = gridview->contentX(); + gridview->setContentX(pos + 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos + 50); + + // remove item before visible and check that left margin is maintained + // and xOrigin is updated + gridview->setContentX(200); + model.removeItems(0, 4); + QTest::qWait(100); + gridview->setContentX(-50); + gridview->returnToBounds(); + QCOMPARE(gridview->xOrigin(), 100.); + QTRY_COMPARE(gridview->contentX(), 70.); + + // reduce left margin + gridview->setLeftMargin(20); + QCOMPARE(gridview->xOrigin(), 100.); + QTRY_COMPARE(gridview->contentX(), 80.); + + // check end bound + gridview->positionViewAtEnd(); + QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin + pos = gridview->contentX(); + gridview->setContentX(pos + 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos + 50); + + // reduce right margin + pos = gridview->contentX(); + gridview->setRightMargin(40); + QCOMPARE(gridview->xOrigin(), 0.); + QTRY_COMPARE(gridview->contentX(), pos-10); + + delete canvas; + } + { + //RTL + QQuickView *canvas = createView(); + canvas->show(); + + QaimModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + + canvas->setSource(testFileUrl("margins.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QCOMPARE(gridview->contentX(), -240+30.); + QCOMPARE(gridview->xOrigin(), 0.); + + // check end bound + gridview->positionViewAtEnd(); + qreal pos = gridview->contentX(); + gridview->setContentX(pos - 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos - 50); + + // remove item before visible and check that left margin is maintained + // and xOrigin is updated + gridview->setContentX(-400); + model.removeItems(0, 4); + QTest::qWait(100); + gridview->setContentX(-240+50); + gridview->returnToBounds(); + QCOMPARE(gridview->xOrigin(), -100.); + QTRY_COMPARE(gridview->contentX(), -240-70.); + + // reduce left margin (i.e. right side due to RTL) + pos = gridview->contentX(); + gridview->setLeftMargin(20); + QCOMPARE(gridview->xOrigin(), -100.); + QTRY_COMPARE(gridview->contentX(), -240-80.); + + // check end bound + gridview->positionViewAtEnd(); + QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin + pos = gridview->contentX(); + gridview->setContentX(pos - 80); + gridview->returnToBounds(); + QTRY_COMPARE(gridview->contentX(), pos - 50); + + // reduce right margin (i.e. left side due to RTL) + pos = gridview->contentX(); + gridview->setRightMargin(40); + QCOMPARE(gridview->xOrigin(), 0.); + QTRY_COMPARE(gridview->contentX(), pos+10); + + delete canvas; + } +} + +void tst_QQuickGridView::creationContext() +{ + QQuickView canvas; + canvas.setGeometry(0,0,240,320); + canvas.setSource(testFileUrl("creationContext.qml")); + qApp->processEvents(); + + QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject()); + QVERIFY(rootItem); + QVERIFY(rootItem->property("count").toInt() > 0); + + QQuickItem *item; + QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild<QQuickItem *>("header")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild<QQuickItem *>("footer")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); +} + +void tst_QQuickGridView::snapToRow_data() +{ + QTest::addColumn<QQuickGridView::Flow>("flow"); + QTest::addColumn<Qt::LayoutDirection>("layoutDirection"); + QTest::addColumn<int>("highlightRangeMode"); + QTest::addColumn<QPoint>("flickStart"); + QTest::addColumn<QPoint>("flickEnd"); + QTest::addColumn<qreal>("snapAlignment"); + QTest::addColumn<qreal>("endExtent"); + QTest::addColumn<qreal>("startExtent"); + + QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) + << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0; + + QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) + << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0; + + QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange) + << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.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) << 60.0 << 1340.0 << -20.0; + + QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0; + + QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange) + << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 - 140.0 << -220.0; +} + +void tst_QQuickGridView::snapToRow() +{ + 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(testFileUrl("snapToRow.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + gridview->setFlow(flow); + gridview->setLayoutDirection(layoutDirection); + gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode)); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QQuickItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + // confirm that a flick hits an item boundary + flick(canvas, flickStart, flickEnd, 180); + QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops + if (flow == QQuickGridView::LeftToRight) + QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment); + else + QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment); + + // 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 (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); + + delete canvas; +} + +void tst_QQuickGridView::snapOneRow_data() +{ + QTest::addColumn<QQuickGridView::Flow>("flow"); + QTest::addColumn<Qt::LayoutDirection>("layoutDirection"); + QTest::addColumn<int>("highlightRangeMode"); + QTest::addColumn<QPoint>("flickStart"); + QTest::addColumn<QPoint>("flickEnd"); + QTest::addColumn<qreal>("snapAlignment"); + QTest::addColumn<qreal>("endExtent"); + QTest::addColumn<qreal>("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(testFileUrl("snapOneRow.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + gridview->setFlow(flow); + gridview->setLayoutDirection(layoutDirection); + gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode)); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + 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(); + canvas->show(); + + QaimModel model; + for (int i = 0; i < 10; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->setSource(testFileUrl("unaligned.qml")); + qApp->processEvents(); + + QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject()); + QVERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + + for (int i = 0; i < 10; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth())); + QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight())); + } + + // appending + for (int i = 10; i < 18; ++i) { + model.addItem("Item" + QString::number(i), ""); + QQuickItem *item = 0; + QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i)); + QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth())); + QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight())); + } + + // inserting + for (int i = 0; i < 10; ++i) { + model.insertItem(i, "Item" + QString::number(i), ""); + QQuickItem *item = 0; + QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i)); + QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth())); + QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight())); + } + + // removing + model.removeItems(7, 10); + QTRY_COMPARE(model.count(), gridview->count()); + for (int i = 0; i < 18; ++i) { + QQuickItem *item = 0; + QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i)); + QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth()); + QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight()); + } + + delete canvas; +} + +void tst_QQuickGridView::populateTransitions() +{ + QFETCH(bool, staticallyPopulate); + QFETCH(bool, dynamicallyPopulate); + QFETCH(bool, usePopulateTransition); + + QPointF transitionFrom(-50, -50); + QPointF transitionVia(100, 100); + QaimModel model_transitionFrom; + QaimModel model_transitionVia; + + QaimModel model; + if (staticallyPopulate) { + for (int i = 0; i < 30; i++) + model.addItem("item" + QString::number(i), ""); + } + + QQuickView *canvas = createView(); + canvas->rootContext()->setContextProperty("testModel", &model); + canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition); + canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate); + canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom); + canvas->rootContext()->setContextProperty("transitionVia", transitionVia); + canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom); + canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia); + canvas->setSource(testFileUrl("populateTransitions.qml")); + canvas->show(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QVERIFY(gridview); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem); + + if (staticallyPopulate || dynamicallyPopulate) { + // check the populate transition is run + if (usePopulateTransition) { + QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 19); + } else { + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0); + } + QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0); + } else { + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + } + + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + if (usePopulateTransition) + QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt()); + for (int i=0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + // add an item and check this is done with add transition, not populate + model.insertItem(0, "another item", ""); + QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1); + QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), + (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 19 : 0); + + // clear the model + canvas->rootContext()->setContextProperty("testModel", QVariant()); + QTRY_COMPARE(gridview->count(), 0); + QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0); + gridview->setProperty("countPopulateTransitions", 0); + gridview->setProperty("countAddTransitions", 0); + + // set to a valid model and check populate transition is run a second time + model.clear(); + for (int i = 0; i < 30; i++) + model.addItem("item" + QString::number(i), ""); + canvas->rootContext()->setContextProperty("testModel", &model); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0); + QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0); + + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + if (usePopulateTransition) + QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt()); + for (int i=0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + // reset model and check populate transition is run again + gridview->setProperty("countPopulateTransitions", 0); + gridview->setProperty("countAddTransitions", 0); + model.reset(); + QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0); + QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0); + + itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + if (usePopulateTransition) + QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt()); + for (int i=0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::populateTransitions_data() +{ + QTest::addColumn<bool>("staticallyPopulate"); + QTest::addColumn<bool>("dynamicallyPopulate"); + QTest::addColumn<bool>("usePopulateTransition"); + + QTest::newRow("static") << true << false << true; + QTest::newRow("static, no populate") << true << false << false; + + QTest::newRow("dynamic") << false << true << true; + QTest::newRow("dynamic, no populate") << false << true << false; + + QTest::newRow("empty to start with") << false << false << true; + QTest::newRow("empty to start with, no populate") << false << false << false; +} + +void tst_QQuickGridView::addTransitions() +{ + QFETCH(int, initialItemCount); + QFETCH(bool, shouldAnimateTargets); + QFETCH(qreal, contentY); + QFETCH(int, insertionIndex); + QFETCH(int, insertionCount); + QFETCH(ListRange, expectedDisplacedIndexes); + + // added items should start here + QPointF targetItems_transitionFrom(-50, -50); + + // displaced items should pass through this point + QPointF displacedItems_transitionVia(100, 100); + + QaimModel model; + for (int i = 0; i < initialItemCount; i++) + model.addItem("Original item" + QString::number(i), ""); + QaimModel model_targetItems_transitionFrom; + QaimModel model_displacedItems_transitionVia; + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom); + ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia); + ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom); + ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia); + canvas->setSource(testFileUrl("addTransitions.qml")); + canvas->show(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + if (contentY != 0) { + gridview->setContentY(contentY); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + } + + QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model); + + // only target items that will become visible should be animated + QList<QPair<QString, QString> > newData; + QList<QPair<QString, QString> > expectedTargetData; + QList<int> targetIndexes; + if (shouldAnimateTargets) { + for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) { + newData << qMakePair(QString("New item %1").arg(i), QString("")); + + // last visible item is the first item of the row beneath the view + if (i >= (contentY / 60)*3 && i < qCeil((contentY + gridview->height()) / 60.0)*3) { + expectedTargetData << newData.last(); + targetIndexes << i; + } + } + QVERIFY(expectedTargetData.count() > 0); + } + + // start animation + if (!newData.isEmpty()) { + model.insertItems(insertionIndex, newData); + QTRY_COMPARE(model.count(), gridview->count()); + } + + QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes); + + if (shouldAnimateTargets) { + QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count()); + QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(), + expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0); + + // check the target and displaced items were animated + model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos"); + model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim"); + + // check attached properties + matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes); + matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes); + matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems); + if (expectedDisplacedIndexes.isValid()) { + // adjust expectedDisplacedIndexes to their final values after the move + QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount); + matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes); + matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes); + matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems); + } + } else { + QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0); + QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0); + } + + QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper"); + int firstVisibleIndex = -1; + for (int i=0; i<items.count(); i++) { + if (items[i]->y() >= contentY) { + QQmlExpression e(qmlContext(items[i]), items[i], "index"); + firstVisibleIndex = e.evaluate().toInt(); + break; + } + } + QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); + + // verify all items moved to the correct final positions + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::addTransitions_data() +{ + QTest::addColumn<int>("initialItemCount"); + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<bool>("shouldAnimateTargets"); + QTest::addColumn<int>("insertionIndex"); + QTest::addColumn<int>("insertionCount"); + QTest::addColumn<ListRange>("expectedDisplacedIndexes"); + + // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items + QTest::newRow("insert 1, just before start") + << 30 << 20.0 << false + << 0 << 1 << ListRange(); + QTest::newRow("insert 1, way before start") + << 30 << 20.0 << false + << 0 << 1 << ListRange(); + QTest::newRow("insert multiple, just before start") + << 30 << 100.0 << false + << 0 << 3 << ListRange(); + QTest::newRow("insert multiple (< 1 row), just before start") + << 30 << 100.0 << false + << 0 << 2 << ListRange(); + QTest::newRow("insert multiple, way before start") + << 30 << 100.0 << false + << 0 << 3 << ListRange(); + + QTest::newRow("insert 1 at start") + << 30 << 0.0 << true + << 0 << 1 << ListRange(0, 17); + QTest::newRow("insert multiple at start") + << 30 << 0.0 << true + << 0 << 3 << ListRange(0, 17); + QTest::newRow("insert multiple (> 1 row) at start") + << 30 << 0.0 << true + << 0 << 5 << ListRange(0, 17); + QTest::newRow("insert 1 at start, content y not 0") + << 30 << 60.0 << true // first visible is index 3 + << 3 << 1 << ListRange(0 + 3, 17 + 3); + QTest::newRow("insert multiple at start, content y not 0") + << 30 << 60.0 << true // first visible is index 3 + << 3 << 3 << ListRange(0 + 3, 17 + 3); + QTest::newRow("insert multiple (> 1 row) at start, content y not 0") + << 30 << 60.0 << true // first visible is index 3 + << 3 << 5 << ListRange(0 + 3, 17 + 3); + + QTest::newRow("insert 1 at start, to empty grid") + << 0 << 0.0 << true + << 0 << 1 << ListRange(); + QTest::newRow("insert multiple at start, to empty grid") + << 0 << 0.0 << true + << 0 << 3 << ListRange(); + + QTest::newRow("insert 1 at middle") + << 30 << 0.0 << true + << 7 << 1 << ListRange(7, 17); + QTest::newRow("insert multiple at middle") + << 30 << 0.0 << true + << 7 << 3 << ListRange(7, 17); + QTest::newRow("insert multiple (> 1 row) at middle") + << 30 << 0.0 << true + << 7 << 5 << ListRange(7, 17); + + QTest::newRow("insert 1 at bottom") + << 30 << 0.0 << true + << 17 << 1 << ListRange(17, 17); + QTest::newRow("insert multiple at bottom") + << 30 << 0.0 << true + << 17 << 3 << ListRange(17, 17); + QTest::newRow("insert 1 at bottom, content y not 0") + << 30 << 20.0 * 3 << true + << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3); + QTest::newRow("insert multiple at bottom, content y not 0") + << 30 << 20.0 * 3 << true + << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3); + + + // items added after the last visible will not be animated in, since they + // do not appear in the final view + QTest::newRow("insert 1 after end") + << 30 << 0.0 << false + << 18 << 1 << ListRange(); + QTest::newRow("insert multiple after end") + << 30 << 0.0 << false + << 18 << 3 << ListRange(); +} + +void tst_QQuickGridView::moveTransitions() +{ + QFETCH(int, initialItemCount); + QFETCH(qreal, contentY); + QFETCH(qreal, itemsOffsetAfterMove); + QFETCH(int, moveFrom); + QFETCH(int, moveTo); + QFETCH(int, moveCount); + QFETCH(ListRange, expectedDisplacedIndexes); + + // target and displaced items should pass through these points + QPointF targetItems_transitionVia(-50, 50); + QPointF displacedItems_transitionVia(100, 100); + + QaimModel model; + for (int i = 0; i < initialItemCount; i++) + model.addItem("Original item" + QString::number(i), ""); + QaimModel model_targetItems_transitionVia; + QaimModel model_displacedItems_transitionVia; + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia); + ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia); + ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia); + ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia); + canvas->setSource(testFileUrl("moveTransitions.qml")); + canvas->show(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QQuickText *name; + + if (contentY != 0) { + gridview->setContentY(contentY); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + } + + QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model); + + // Items moving to *or* from visible positions should be animated. + // Otherwise, they should not be animated. + QList<QPair<QString, QString> > expectedTargetData; + QList<int> targetIndexes; + for (int i=moveFrom; i<moveFrom+moveCount; i++) { + int toIndex = moveTo + (i - moveFrom); + int firstVisibleIndex = (contentY / 60) * 3; + int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1; + if ((i >= firstVisibleIndex && i <= lastVisibleIndex) + || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) { + expectedTargetData << qMakePair(model.name(i), model.number(i)); + targetIndexes << i; + } + } + // ViewTransition.index provides the indices that items are moving to, not from + targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount); + + // start animation + model.moveItems(moveFrom, moveTo, moveCount); + + QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count()); + QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(), + expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0); + + QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes); + + // check the target and displaced items were animated + model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos"); + model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim"); + + // check attached properties + matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes); + matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes); + matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems); + if (expectedDisplacedIndexes.isValid()) { + // adjust expectedDisplacedIndexes to their final values after the move + QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount); + matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes); + matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes); + matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems); + } + + QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper"); + int firstVisibleIndex = -1; + for (int i=0; i<items.count(); i++) { + if (items[i]->y() >= contentY) { + QQmlExpression e(qmlContext(items[i]), items[i], "index"); + firstVisibleIndex = e.evaluate().toInt(); + break; + } + } + QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); + + // verify all items moved to the correct final positions + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove); + name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::moveTransitions_data() +{ + QTest::addColumn<int>("initialItemCount"); + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<qreal>("itemsOffsetAfterMove"); + QTest::addColumn<int>("moveFrom"); + QTest::addColumn<int>("moveTo"); + QTest::addColumn<int>("moveCount"); + QTest::addColumn<ListRange>("expectedDisplacedIndexes"); + + QTest::newRow("move from above view, outside visible items, move 1") << 30 << 120.0 << 0.0 + << 1 << 10 << 1 << ListRange(6, 10); + QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 120.0 << 0.0 + << 0 << 10 << 1 << ListRange(6, 10); + QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 120.0 << 60.0 + << 1 << 10 << 3 << ListRange(13, 23); + QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 120.0 << 60.0 + << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23)); + QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 120.0 << 120.0 + << 0 << 10 << 6 << ListRange(16, 23); + + QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0 + << 1 << 10 << 1 << ListRange(2, 10); + QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0 + << 0 << 10 << 1 << ListRange(1, 10); + QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 120.0 << 0.0 + << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6); + QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0 + << 10 << 17 << 1 << ListRange(11, 17); + QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0 + << 0 << 17 << 1 << ListRange(1, 17); + + QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0 + << 1 << 10 << 3 << ListRange(4, 12); + QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0 + << 0 << 10 << 3 << ListRange(3, 12); + QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 60.0 << 0.0 + << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3); + QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0 + << 5 << 15 << 3 << ListRange(8, 17); + QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0 + << 0 << 15 << 3 << ListRange(3, 17); + + QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0 + << 10 << 1 << 1 << ListRange(1, 9); + QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0 + << 10 << 0 << 1 << ListRange(0, 9); + QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 120.0 << 0.0 + << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6); + QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 80.0 << 0.0 + << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3); + QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0 + << 17 << 10 << 1 << ListRange(10, 16); + QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0 + << 17 << 0 << 1 << ListRange(0, 16); + + QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0 + << 10 << 1 << 3 << ListRange(1, 9); + QTest::newRow("move within view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0 + << 10 << 1 << 5 << ListRange(1, 9); + QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0 + << 10 << 0 << 3 << ListRange(0, 9); + QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 60.0 << 0.0 + << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3); + QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border") << 30 << 80.0 << 0.0 + << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3); + QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0 + << 15 << 5 << 3 << ListRange(5, 14); + QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0 + << 15 << 0 << 3 << ListRange(0, 14); + + QTest::newRow("move from below view, move 1 up") << 30 << 0.0 << 0.0 + << 20 << 5 << 1 << ListRange(5, 17); + QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0 + << 20 << 0 << 1 << ListRange(0, 17); + QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 60.0 << 0.0 + << 25 << 3 << 1 << ListRange(0+3, 17+3); + QTest::newRow("move from below view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0 + << 20 << 5 << 5 << ListRange(5, 17); + QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0 + << 20 << 0 << 3 << ListRange(0, 17); + QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 60.0 << 0.0 + << 25 << 3 << 3 << ListRange(0+3, 17+3); + + QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0 + << 20 << 17 << 1 << ListRange(17, 17); + QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0 + << 25 << 17+3 << 1 << ListRange(17+3, 17+3); + QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0 + << 20 << 17 << 3 << ListRange(17, 17); + QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0 + << 25 << 17+3 << 3 << ListRange(17+3, 17+3); +} + +void tst_QQuickGridView::removeTransitions() +{ + QFETCH(int, initialItemCount); + QFETCH(bool, shouldAnimateTargets); + QFETCH(qreal, contentY); + QFETCH(int, removalIndex); + QFETCH(int, removalCount); + QFETCH(ListRange, expectedDisplacedIndexes); + + // added items should end here + QPointF targetItems_transitionTo(-50, -50); + + // displaced items should pass through this points + QPointF displacedItems_transitionVia(100, 100); + + QaimModel model; + for (int i = 0; i < initialItemCount; i++) + model.addItem("Original item" + QString::number(i), ""); + QaimModel model_targetItems_transitionTo; + QaimModel model_displacedItems_transitionVia; + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo); + ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia); + ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo); + ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia); + canvas->setSource(testFileUrl("removeTransitions.qml")); + canvas->show(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + if (contentY != 0) { + gridview->setContentY(contentY); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + } + + QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model); + + // only target items that are visible should be animated + QList<QPair<QString, QString> > expectedTargetData; + QList<int> targetIndexes; + if (shouldAnimateTargets) { + for (int i=removalIndex; i<removalIndex+removalCount; i++) { + int firstVisibleIndex = (contentY / 60.0)*3; + int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1; + if (i >= firstVisibleIndex && i <= lastVisibleIndex) { + expectedTargetData << qMakePair(model.name(i), model.number(i)); + targetIndexes << i; + } + } + QVERIFY(expectedTargetData.count() > 0); + } + + // calculate targetItems and expectedTargets before model changes + QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes); + QVariantMap expectedTargets; + for (int i=0; i<targetIndexes.count(); i++) + expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i]; + + // start animation + model.removeItems(removalIndex, removalCount); + QTRY_COMPARE(model.count(), gridview->count()); + + if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) { + QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count()); + QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(), + expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0); + + // check the target and displaced items were animated + model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos"); + model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim"); + + // check attached properties + QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets); + matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes); + matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems); + if (expectedDisplacedIndexes.isValid()) { + // adjust expectedDisplacedIndexes to their final values after the move + QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount); + matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes); + matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes); + matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems); + } + } else { + QTRY_COMPARE(model_targetItems_transitionTo.count(), 0); + QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0); + } + + QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper"); + int itemCount = items.count(); + int firstVisibleIndex = -1; + for (int i=0; i<items.count(); i++) { + QQmlExpression e(qmlContext(items[i]), items[i], "index"); + int index = e.evaluate().toInt(); + if (firstVisibleIndex < 0 && items[i]->y() >= contentY) + firstVisibleIndex = index; + else if (index < 0) + itemCount--; // exclude deleted items + } + QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); + + // verify all items moved to the correct final positions + for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), contentY + ((i-firstVisibleIndex)/3) * 60.0); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::removeTransitions_data() +{ + QTest::addColumn<int>("initialItemCount"); + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<bool>("shouldAnimateTargets"); + QTest::addColumn<int>("removalIndex"); + QTest::addColumn<int>("removalCount"); + QTest::addColumn<ListRange>("expectedDisplacedIndexes"); + + // All items that are visible following the remove operation should be animated. + // Remove targets that are outside of the view should not be animated. + + // For a GridView, removing any number of items other than a full row before the start + // should displace all items in the view + QTest::newRow("remove 1 before start") + << 30 << 120.0 << false + << 2 << 1 << ListRange(6, 24); // 6-24 are displaced + QTest::newRow("remove 1 row, before start") + << 30 << 120.0 << false + << 3 << 3 << ListRange(); + QTest::newRow("remove between 1-2 rows, before start") + << 30 << 120.0 << false + << 0 << 5 << ListRange(6, 25); + QTest::newRow("remove 2 rows, before start") + << 30 << 120.0 << false + << 0 << 6 << ListRange(); + QTest::newRow("remove mix of before and after start") + << 30 << 60.0 << true + << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view + + + QTest::newRow("remove 1 from start") + << 30 << 0.0 << true + << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view + QTest::newRow("remove multiple from start") + << 30 << 0.0 << true + << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view + QTest::newRow("remove 1 from start, content y not 0") + << 30 << 60.0 << true + << 3 << 1 << ListRange(1 + 3, 18 + 3); + QTest::newRow("remove multiple from start, content y not 0") + << 30 << 60.0 << true + << 3 << 3 << ListRange(3 + 3, 20 + 3); + + + QTest::newRow("remove 1 from middle") + << 30 << 0.0 << true + << 5 << 1 << ListRange(6, 18); + QTest::newRow("remove multiple from middle") + << 30 << 0.0 << true + << 5 << 3 << ListRange(8, 20); + + + QTest::newRow("remove 1 from bottom") + << 30 << 0.0 << true + << 17 << 1 << ListRange(18, 18); + QTest::newRow("remove multiple (1 row) from bottom") + << 30 << 0.0 << true + << 15 << 3 << ListRange(18, 20); + QTest::newRow("remove multiple (> 1 row) from bottom") + << 30 << 0.0 << true + << 15 << 5 << ListRange(20, 22); + QTest::newRow("remove 1 from bottom, content y not 0") + << 30 << 60.0 << true + << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3); + QTest::newRow("remove multiple (1 row) from bottom, content y not 0") + << 30 << 60.0 << true + << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3); + + + QTest::newRow("remove 1 after end") + << 30 << 0.0 << false + << 18 << 1 << ListRange(); + QTest::newRow("remove multiple after end") + << 30 << 0.0 << false + << 18 << 3 << ListRange(); +} + +void tst_QQuickGridView::multipleTransitions() +{ + // Tests that if you interrupt a transition in progress with another action that + // cancels the previous transition, the resulting items are still placed correctly. + + QFETCH(int, initialCount); + QFETCH(qreal, contentY); + QFETCH(QList<ListChange>, changes); + + // add transitions on the left, moves on the right + QPointF addTargets_transitionFrom(-50, -50); + QPointF addDisplaced_transitionFrom(-50, 50); + QPointF moveTargets_transitionFrom(50, -50); + QPointF moveDisplaced_transitionFrom(50, 50); + + QmlListModel model; + for (int i = 0; i < initialCount; i++) + model.addItem("Original item" + QString::number(i), ""); + + QQuickView *canvas = createView(); + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom); + ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom); + ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom); + ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom); + canvas->setSource(testFileUrl("multipleTransitions.qml")); + canvas->show(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + + int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt(); + + QList<QPair<QString, QString> > targetItems; + for (int i=0; i<changes.count(); i++) { + switch (changes[i].type) { + case ListChange::Inserted: + { + for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j) + targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j)); + model.insertItems(changes[i].index, targetItems); + QTRY_COMPARE(model.count(), gridview->count()); + QTRY_VERIFY(gridview->property("runningAddTargets").toBool()); + QTRY_VERIFY(gridview->property("runningAddDisplaced").toBool()); + if (i == changes.count() - 1) { + QTRY_VERIFY(!gridview->property("runningAddTargets").toBool()); + QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool()); + } else { + QTest::qWait(timeBetweenActions); + } + break; + } + case ListChange::Removed: + for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j) + targetItems << qMakePair(model.name(i), model.number(i)); + model.removeItems(changes[i].index, changes[i].count); + QTRY_COMPARE(model.count(), gridview->count()); + QTRY_VERIFY(gridview->property("runningRemoveTargets").toBool()); + QTRY_VERIFY(gridview->property("runningRemoveDisplaced").toBool()); + if (i == changes.count() - 1) { + QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool()); + QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool()); + } else { + QTest::qWait(timeBetweenActions); + } + break; + case ListChange::Moved: + for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j) + targetItems << qMakePair(model.name(i), model.number(i)); + model.moveItems(changes[i].index, changes[i].to, changes[i].count); + QTRY_VERIFY(gridview->property("runningMoveTargets").toBool()); + QTRY_VERIFY(gridview->property("runningMoveDisplaced").toBool()); + if (i == changes.count() - 1) { + QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool()); + QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool()); + } else { + QTest::qWait(timeBetweenActions); + } + break; + case ListChange::SetCurrent: + gridview->setCurrentIndex(changes[i].index); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + break; + case ListChange::SetContentY: + gridview->setContentY(changes[i].pos); + QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false); + break; + } + } + QCOMPARE(gridview->count(), model.count()); + + QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper"); + int firstVisibleIndex = -1; + for (int i=0; i<items.count(); i++) { + if (items[i]->y() >= contentY) { + QQmlExpression e(qmlContext(items[i]), items[i], "index"); + firstVisibleIndex = e.evaluate().toInt(); + break; + } + } + QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); + + // verify all items moved to the correct final positions + int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count(); + for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); + QCOMPARE(item->x(), (i%3)*80.0); + QCOMPARE(item->y(), (i/3)*60.0); + QQuickText *name = findItem<QQuickText>(contentItem, "textName", i); + QVERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + } + + delete canvas; +} + +void tst_QQuickGridView::multipleTransitions_data() +{ + QTest::addColumn<int>("initialCount"); + QTest::addColumn<qreal>("contentY"); + QTest::addColumn<QList<ListChange> >("changes"); + + // the added item and displaced items should move to final dest correctly + QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>() + << ListChange::insert(0, 1) + << ListChange::move(0, 3, 1) + ); + + // items affected by the add should change from move to add transition + QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>() + << ListChange::move(1, 10, 3) + << ListChange::insert(0, 1) + ); + + // items should be placed correctly if you trigger a transition then refill for that index + QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>() + << ListChange::insert(0, 1) + << ListChange::setContentY(160.0) + << ListChange::setContentY(0.0) + << ListChange::insert(0, 1) + ); +} + +void tst_QQuickGridView::cacheBuffer() +{ + QQuickView *canvas = createView(); + + QaimModel model; + for (int i = 0; i < 90; i++) + model.addItem("Item" + QString::number(i), ""); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + + canvas->setSource(testFileUrl("gridview1.qml")); + canvas->show(); + qApp->processEvents(); + + QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid"); + QVERIFY(gridview != 0); + + QQuickItem *contentItem = gridview->contentItem(); + QVERIFY(contentItem != 0); + QVERIFY(gridview->delegate() != 0); + QVERIFY(gridview->model() != 0); + + // Confirm items positioned correctly + int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QTRY_COMPARE(item->x(), (i%3)*80.0); + QTRY_COMPARE(item->y(), (i/3)*60.0); + } + + QQmlIncubationController controller; + canvas->engine()->setIncubationController(&controller); + + canvas->rootObject()->setProperty("cacheBuffer", 200); + QTRY_VERIFY(gridview->cacheBuffer() == 200); + + // items will be created one at a time + for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) { + QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0); + QQuickItem *item = 0; + while (!item) { + bool b = false; + controller.incubateWhile(&b); + item = findItem<QQuickItem>(gridview, "wrapper", i); + } + } + + { + bool b = true; + controller.incubateWhile(&b); + } + + int newItemCount = 0; + newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count(); + + // Confirm items positioned correctly + for (int i = 0; i < model.count() && i < newItemCount; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.0); + QTRY_COMPARE(item->y(), (i/3)*60.0); + } + + // move view and confirm items in view are visible immediately and outside are created async + gridview->setContentY(300); + + for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + QVERIFY(item); + QTRY_COMPARE(item->x(), (i%3)*80.0); + QTRY_COMPARE(item->y(), (i/3)*60.0); + } + + QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0); + + // ensure buffered items are created + for (int i = 34; i < qMin(44,model.count()); ++i) { + QQuickItem *item = 0; + while (!item) { + qGuiApp->processEvents(); // allow refill to happen + bool b = false; + controller.incubateWhile(&b); + item = findItem<QQuickItem>(gridview, "wrapper", i); + } + } + + { + bool b = true; + controller.incubateWhile(&b); + } + + delete canvas; +} + +void tst_QQuickGridView::asynchronous() +{ + QQuickView *canvas = createView(); + canvas->show(); + QQmlIncubationController controller; + canvas->engine()->setIncubationController(&controller); + + canvas->setSource(testFile("asyncloader.qml")); + + QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject()); + QVERIFY(rootObject); + + QQuickGridView *gridview = 0; + while (!gridview) { + bool b = false; + controller.incubateWhile(&b); + gridview = rootObject->findChild<QQuickGridView*>("view"); + } + + // items will be created one at a time + for (int i = 0; i < 12; ++i) { + QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0); + QQuickItem *item = 0; + while (!item) { + bool b = false; + controller.incubateWhile(&b); + item = findItem<QQuickItem>(gridview, "wrapper", i); + } + } + + { + bool b = true; + controller.incubateWhile(&b); + } + + // verify positioning + QQuickItem *contentItem = gridview->contentItem(); + for (int i = 0; i < 12; ++i) { + QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item->x() == (i%3)*100); + QVERIFY(item->y() == (i/3)*100); + } + + delete canvas; +} + +void tst_QQuickGridView::unrequestedVisibility() +{ + QaimModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), QString::number(i)); + + QQuickView *canvas = new QQuickView(0); + canvas->setGeometry(0,0,240,320); + + QQmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testWrap", QVariant(false)); + + canvas->setSource(testFileUrl("unrequestedItems.qml")); + + canvas->show(); + + qApp->processEvents(); + + QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid"); + QTRY_VERIFY(leftview != 0); + + QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid"); + QTRY_VERIFY(rightview != 0); + + QQuickItem *leftContent = leftview->contentItem(); + QTRY_VERIFY(leftContent != 0); + + QQuickItem *rightContent = rightview->contentItem(); + QTRY_VERIFY(rightContent != 0); + + rightview->setCurrentIndex(12); + + QTRY_COMPARE(leftview->contentY(), 0.0); + QTRY_COMPARE(rightview->contentY(), 240.0); + + QQuickItem *item; + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1)); + QCOMPARE(item->isVisible(), false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11)); + QCOMPARE(item->isVisible(), true); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4)); + QCOMPARE(item->isVisible(), true); + + rightview->setCurrentIndex(0); + + QTRY_COMPARE(leftview->contentY(), 0.0); + QTRY_COMPARE(rightview->contentY(), 0.0); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1)); + QTRY_COMPARE(item->isVisible(), true); + + QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11)); + QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11)); + + leftview->setCurrentIndex(12); + + QTRY_COMPARE(leftview->contentY(), 240.0); + QTRY_COMPARE(rightview->contentY(), 0.0); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1)); + QTRY_COMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1)); + QCOMPARE(item->isVisible(), true); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11)); + QCOMPARE(item->isVisible(), false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + + // move a non-visible item into view + model.moveItems(10, 9, 1); + QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); + + QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1)); + QCOMPARE(item->isVisible(), true); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11)); + QCOMPARE(item->isVisible(), false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + + // move a visible item out of view + model.moveItems(5, 3, 1); + QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + + // move a non-visible item into view + model.moveItems(3, 5, 1); + QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + + // move a visible item out of view + model.moveItems(9, 10, 1); + QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + + // move a non-visible item into view + model.moveItems(10, 9, 1); + QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); + + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3)); + QCOMPARE(item->isVisible(), false); + QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9)); + QCOMPARE(item->isVisible(), true); + QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10)); + QCOMPARE(item->isVisible(), false); + + delete canvas; +} + +QList<int> tst_QQuickGridView::toIntList(const QVariantList &list) +{ + QList<int> ret; + bool ok = true; + for (int i=0; i<list.count(); i++) { + ret << list[i].toInt(&ok); + if (!ok) + qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i]; + } + + return ret; +} + +void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes) +{ + for (int i=0; i<indexLists.count(); i++) { + QSet<int> current = indexLists[i].value<QList<int> >().toSet(); + if (current != expectedIndexes.toSet()) + qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes; + QCOMPARE(current, expectedIndexes.toSet()); + } +} + +void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes) +{ + for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) { + QVERIFY(it.value().type() == QVariant::Int); + QString name = it.key(); + int itemIndex = it.value().toInt(); + QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex))); + if (model.name(itemIndex) != name) + qDebug() << itemIndex; + QCOMPARE(model.name(itemIndex), name); + } + QCOMPARE(items.count(), expectedIndexes.count()); +} + +void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems) +{ + for (int i=0; i<itemLists.count(); i++) { + QVariantList current = itemLists[i].toList(); + for (int j=0; j<current.count(); j++) { + QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>()); + QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j))); + QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j))); + } + QCOMPARE(current.count(), expectedItems.count()); + } +} + +QTEST_MAIN(tst_QQuickGridView) + +#include "tst_qquickgridview.moc" + |