From ddce00b3ea35618c343094fb9792489edaa21308 Mon Sep 17 00:00:00 2001
From: Mitch Curtis <mitch.curtis@digia.com>
Date: Thu, 18 Sep 2014 10:29:00 +0200
Subject: [PATCH] QTBUG-36698 auto test.

Change-Id: Iafa2f68831e7ceb4edb5024a556383c9d5b3a8ee
---
 .../sectionsCollapsedWithCurrentLabelAtStart.qml   | 78 ++++++++++++++++++++++
 .../quick/qquicklistview/tst_qquicklistview.cpp    | 37 ++++++++++
 2 files changed, 115 insertions(+)
 create mode 100644 tests/auto/quick/qquicklistview/data/sectionsCollapsedWithCurrentLabelAtStart.qml

diff --git a/tests/auto/quick/qquicklistview/data/sectionsCollapsedWithCurrentLabelAtStart.qml b/tests/auto/quick/qquicklistview/data/sectionsCollapsedWithCurrentLabelAtStart.qml
new file mode 100644
index 0000000..3ac73ef
--- /dev/null
+++ b/tests/auto/quick/qquicklistview/data/sectionsCollapsedWithCurrentLabelAtStart.qml
@@ -0,0 +1,78 @@
+import QtQuick 2.0
+
+Rectangle {
+    width: 300
+    height: 300
+
+    function createModel() {
+        for (var i = 0; i < 10; ++i)
+            animalsModel.append({name: i + " Parrot", size: "Small", visibility: true});
+        for (i = 10; i < 20; ++i)
+            animalsModel.append({name: i + " Cat", size: "Medium", visibility: true});
+        for (i = 20; i < 30; ++i)
+            animalsModel.append({name: i + " Elephant", size: "Large", visibility: true});
+    }
+
+    Component.onCompleted: createModel()
+
+    ListModel {
+        id: animalsModel
+    }
+
+    Component {
+        id: sectionHeading
+
+        Rectangle {
+            width: listView.width
+            height: collapsed ? 30 : 50
+            color: collapsed ? "blue" : "lightsteelblue"
+            objectName: section + "SectionDelegate"
+            property bool collapsed: false
+
+            Text {
+                anchors.fill: parent
+                text: section
+                horizontalAlignment: Text.AlignHCenter
+                verticalAlignment: Text.AlignVCenter
+                font.bold: true
+            }
+
+            MouseArea {
+                anchors.fill: parent
+                onClicked: {
+                    print(section, "clicked")
+                    collapsed = !collapsed
+                    for (var i = 0; i < animalsModel.count; i++) {
+                        var item = animalsModel.get(i)
+                        if (item.size === section) {
+                            item.visibility = !collapsed
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    ListView {
+        id: listView
+        objectName: "list"
+        anchors.fill: parent
+        model: animalsModel
+
+        delegate: Text {
+            text: name
+            height: 50
+            width: listView.width
+            horizontalAlignment: Text.AlignHCenter
+            verticalAlignment: Text.AlignVCenter
+            visible: visibility
+            onVisibleChanged: {
+                visible ? height = 60 : height = 0
+            }
+        }
+        section.property: "size"
+        section.criteria: ViewSection.FullString
+        section.delegate: sectionHeading
+        section.labelPositioning: ViewSection.InlineLabels | ViewSection.CurrentLabelAtStart
+    }
+}
diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp
index 1e901ea..f2116c9 100644
--- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp
+++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp
@@ -134,6 +134,7 @@ private slots:
     void sectionsDelegate_headerVisibility();
     void sectionPropertyChange();
     void sectionDelegateChange();
+    void sectionsCollapsedWithCurrentLabelAtStart();
     void cacheBuffer();
     void positionViewAtBeginningEnd();
     void positionViewAtIndex();
@@ -2538,6 +2539,42 @@ void tst_QQuickListView::sectionDelegateChange()
     delete window;
 }
 
+void tst_QQuickListView::sectionsCollapsedWithCurrentLabelAtStart()
+{
+    QQuickView *window = createView();
+    window->setSource(testFileUrl("sectionsCollapsedWithCurrentLabelAtStart.qml"));
+    window->show();
+    QVERIFY(QTest::qWaitForWindowExposed(window));
+
+    QQuickListView *listView = findItem<QQuickListView>(window->rootObject(), "list");
+    QVERIFY(listView != 0);
+
+    QQuickItem *contentItem = listView->contentItem();
+    QVERIFY(contentItem != 0);
+
+    listView->setContentY(listView->contentHeight() - listView->height());
+    // Moving to the end of the list instantly doesn't reproduce the bug, so flick.
+    flick(window, QPoint(window->width() / 2, window->height() * 0.25), QPoint(window->width() / 2, window->height() * 0.35), 100);
+    QTRY_VERIFY(!listView->isMoving());
+    // The bug isn't visible until we click on the section header.
+    const QColor steelBlue = QColor(176, 196, 222);
+    QCOMPARE(QColor(window->grabWindow().pixel(0, 0)), steelBlue);
+
+    QQuickItem *largeSection = findItem<QQuickItem>(listView, "LargeSectionDelegate");
+    QVERIFY(largeSection);
+
+    const QPointF sectionCenter = QPointF(largeSection->width() / 2, largeSection->height() / 2);
+    QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, sectionCenter.toPoint());
+    QCOMPARE(QColor(window->grabWindow().pixel(0, 0)), QColor(Qt::blue));
+
+    QQuickItem *mediumSection = findItem<QQuickItem>(listView, "MediumSectionDelegate");
+    QVERIFY(mediumSection);
+    // This is the bug - a different section delegate is changed when it should be the Large delegate.
+    QCOMPARE(mediumSection->property("color").value<QColor>(), steelBlue);
+
+    QCOMPARE(largeSection->property("color").value<QColor>(), QColor(Qt::blue));
+}
+
 void tst_QQuickListView::currentIndex_delayedItemCreation()
 {
     QFETCH(bool, setCurrentToZero);
-- 
1.9.1

