7dba3a60b38039950cf3a3cc026ac259a9721d28
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquicklistview / tst_qquicklistview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QtCore/QStringListModel>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcontext.h>
47 #include <QtQml/qqmlexpression.h>
48 #include <QtQml/qqmlincubator.h>
49 #include <QtQuick/private/qquicklistview_p.h>
50 #include <QtQuick/private/qquicktext_p.h>
51 #include <QtQuick/private/qquickvisualitemmodel_p.h>
52 #include <QtQml/private/qquicklistmodel_p.h>
53 #include "../../shared/util.h"
54 #include "../shared/viewtestutil.h"
55 #include "../shared/visualtestutil.h"
56 #include "incrementalmodel.h"
57 #include <math.h>
58
59 Q_DECLARE_METATYPE(Qt::LayoutDirection)
60 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
61 Q_DECLARE_METATYPE(QQuickListView::Orientation)
62 Q_DECLARE_METATYPE(Qt::Key)
63
64 using namespace QQuickViewTestUtil;
65 using namespace QQuickVisualTestUtil;
66
67 #define SHARE_VIEWS
68
69 class tst_QQuickListView : public QQmlDataTest
70 {
71     Q_OBJECT
72 public:
73     tst_QQuickListView();
74
75 private slots:
76     void init();
77     void cleanupTestCase();
78     // Test both QListModelInterface and QAbstractItemModel model types
79     void qListModelInterface_items();
80     void qListModelInterface_package_items();
81     void qAbstractItemModel_items();
82
83     void qListModelInterface_changed();
84     void qListModelInterface_package_changed();
85     void qAbstractItemModel_changed();
86
87     void qListModelInterface_inserted();
88     void qListModelInterface_inserted_more();
89     void qListModelInterface_inserted_more_data();
90     void qListModelInterface_package_inserted();
91     void qAbstractItemModel_inserted();
92     void qAbstractItemModel_inserted_more();
93     void qAbstractItemModel_inserted_more_data();
94     void qAbstractItemModel_inserted_more_bottomToTop();
95     void qAbstractItemModel_inserted_more_bottomToTop_data();
96
97     void qListModelInterface_removed();
98     void qListModelInterface_removed_more();
99     void qListModelInterface_removed_more_data();
100     void qListModelInterface_package_removed();
101     void qAbstractItemModel_removed();
102     void qAbstractItemModel_removed_more();
103     void qAbstractItemModel_removed_more_data();
104     void qAbstractItemModel_removed_more_bottomToTop();
105     void qAbstractItemModel_removed_more_bottomToTop_data();
106
107     void qListModelInterface_moved();
108     void qListModelInterface_moved_data();
109     void qListModelInterface_package_moved();
110     void qListModelInterface_package_moved_data();
111     void qAbstractItemModel_moved();
112     void qAbstractItemModel_moved_data();
113     void qAbstractItemModel_moved_bottomToTop();
114     void qAbstractItemModel_moved_bottomToTop_data();
115
116     void multipleChanges_condensed() { multipleChanges(true); }
117     void multipleChanges_condensed_data() { multipleChanges_data(); }
118     void multipleChanges_uncondensed() { multipleChanges(false); }
119     void multipleChanges_uncondensed_data() { multipleChanges_data(); }
120
121     void qListModelInterface_clear();
122     void qListModelInterface_package_clear();
123     void qAbstractItemModel_clear();
124     void qAbstractItemModel_clear_bottomToTop();
125
126     void insertBeforeVisible();
127     void insertBeforeVisible_data();
128     void swapWithFirstItem();
129     void itemList();
130     void itemListFlicker();
131     void currentIndex_delayedItemCreation();
132     void currentIndex_delayedItemCreation_data();
133     void currentIndex();
134     void noCurrentIndex();
135     void keyNavigation();
136     void keyNavigation_data();
137     void enforceRange();
138     void enforceRange_withoutHighlight();
139     void spacing();
140     void qListModelInterface_sections();
141     void qListModelInterface_package_sections();
142     void qAbstractItemModel_sections();
143     void sectionsPositioning();
144     void sectionsDelegate();
145     void sectionsDragOutsideBounds_data();
146     void sectionsDragOutsideBounds();
147     void sectionsDelegate_headerVisibility();
148     void sectionPropertyChange();
149     void sectionDelegateChange();
150     void cacheBuffer();
151     void positionViewAtIndex();
152     void resetModel();
153     void propertyChanges();
154     void componentChanges();
155     void modelChanges();
156     void manualHighlight();
157     void initialZValues();
158     void header();
159     void header_data();
160     void header_delayItemCreation();
161     void footer();
162     void footer_data();
163     void extents();
164     void extents_data();
165     void resetModel_headerFooter();
166     void resizeView();
167     void resizeViewAndRepaint();
168     void sizeLessThan1();
169     void QTBUG_14821();
170     void resizeDelegate();
171     void resizeFirstDelegate();
172     void repositionResizedDelegate();
173     void repositionResizedDelegate_data();
174     void QTBUG_16037();
175     void indexAt_itemAt_data();
176     void indexAt_itemAt();
177     void incrementalModel();
178     void onAdd();
179     void onAdd_data();
180     void onRemove();
181     void onRemove_data();
182     void rightToLeft();
183     void test_mirroring();
184     void margins();
185     void marginsResize();
186     void marginsResize_data();
187     void creationContext();
188     void snapToItem_data();
189     void snapToItem();
190     void snapOneItem_data();
191     void snapOneItem();
192
193     void QTBUG_9791();
194     void QTBUG_11105();
195     void QTBUG_21742();
196
197     void asynchronous();
198     void unrequestedVisibility();
199
200     void populateTransitions();
201     void populateTransitions_data();
202     void addTransitions();
203     void addTransitions_data();
204     void moveTransitions();
205     void moveTransitions_data();
206     void removeTransitions();
207     void removeTransitions_data();
208     void displacedTransitions();
209     void displacedTransitions_data();
210     void multipleTransitions();
211     void multipleTransitions_data();
212     void multipleDisplaced();
213
214     void flickBeyondBounds();
215     void destroyItemOnCreation();
216
217     void parentBinding();
218
219 private:
220     template <class T> void items(const QUrl &source, bool forceLayout);
221     template <class T> void changed(const QUrl &source, bool forceLayout);
222     template <class T> void inserted(const QUrl &source);
223     template <class T> void inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
224     template <class T> void removed(const QUrl &source, bool animated);
225     template <class T> void removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
226     template <class T> void moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
227     template <class T> void clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
228     template <class T> void sections(const QUrl &source);
229
230     void multipleChanges(bool condensed);
231     void multipleChanges_data();
232
233     QList<int> toIntList(const QVariantList &list);
234     void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
235     void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
236     void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
237
238     void inserted_more_data();
239     void removed_more_data();
240     void moved_data();
241
242 #ifdef SHARE_VIEWS
243     QQuickView *getView() {
244         if (m_view) {
245             if (QString(QTest::currentTestFunction()) != testForView) {
246                 delete m_view;
247                 m_view = 0;
248             } else {
249                 m_view->setSource(QUrl());
250                 return m_view;
251             }
252         }
253
254         testForView = QTest::currentTestFunction();
255         m_view = createView();
256         return m_view;
257     }
258     void releaseView(QQuickView *view) {
259         Q_ASSERT(view == m_view);
260         m_view->setSource(QUrl());
261     }
262 #else
263     QQuickView *getView() {
264         return createView();
265     }
266     void releaseView(QQuickView *view) {
267         delete view;
268     }
269 #endif
270
271     static void errorMsgHandler(QtMsgType, const char *)
272     {
273         ++m_errorCount;
274     }
275
276     QQuickView *m_view;
277     QString testForView;
278     static int m_errorCount;
279 };
280
281 int tst_QQuickListView::m_errorCount = 0;
282
283 class TestObject : public QObject
284 {
285     Q_OBJECT
286
287     Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError)
288     Q_PROPERTY(bool animate READ animate NOTIFY changedAnim)
289     Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl)
290     Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer)
291
292 public:
293     TestObject(QObject *parent = 0)
294         : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
295         , mCacheBuffer(0) {}
296
297     bool error() const { return mError; }
298     void setError(bool err) { mError = err; emit changedError(); }
299
300     bool animate() const { return mAnimate; }
301     void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
302
303     bool invalidHighlight() const { return mInvalidHighlight; }
304     void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
305
306     int cacheBuffer() const { return mCacheBuffer; }
307     void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
308
309 signals:
310     void changedError();
311     void changedAnim();
312     void changedHl();
313     void changedCacheBuffer();
314
315 public:
316     bool mError;
317     bool mAnimate;
318     bool mInvalidHighlight;
319     int mCacheBuffer;
320 };
321
322 tst_QQuickListView::tst_QQuickListView() : m_view(0)
323 {
324 }
325
326 void tst_QQuickListView::init()
327 {
328 #ifdef SHARE_VIEWS
329     if (m_view && QString(QTest::currentTestFunction()) != testForView) {
330         testForView = QString();
331         delete m_view;
332         m_view = 0;
333     }
334 #endif
335 }
336
337 void tst_QQuickListView::cleanupTestCase()
338 {
339 #ifdef SHARE_VIEWS
340     testForView = QString();
341     delete m_view;
342     m_view = 0;
343 #endif
344 }
345
346 template <class T>
347 void tst_QQuickListView::items(const QUrl &source, bool forceLayout)
348 {
349     QQuickView *canvas = createView();
350
351     T model;
352     model.addItem("Fred", "12345");
353     model.addItem("John", "2345");
354     model.addItem("Bob", "54321");
355
356     QQmlContext *ctxt = canvas->rootContext();
357     ctxt->setContextProperty("testModel", &model);
358
359     TestObject *testObject = new TestObject;
360     ctxt->setContextProperty("testObject", testObject);
361
362     canvas->setSource(source);
363     qApp->processEvents();
364
365     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
366     QTRY_VERIFY(listview != 0);
367
368     QQuickItem *contentItem = listview->contentItem();
369     QTRY_VERIFY(contentItem != 0);
370
371     QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
372     QTRY_VERIFY(testObject->error() == false);
373
374     QTRY_VERIFY(listview->highlightItem() != 0);
375     QTRY_COMPARE(listview->count(), model.count());
376     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
377     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
378
379     // current item should be first item
380     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
381
382     for (int i = 0; i < model.count(); ++i) {
383         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
384         QTRY_VERIFY(name != 0);
385         QTRY_COMPARE(name->text(), model.name(i));
386         QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
387         QTRY_VERIFY(number != 0);
388         QTRY_COMPARE(number->text(), model.number(i));
389     }
390
391     // switch to other delegate
392     testObject->setAnimate(true);
393     QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
394     QTRY_VERIFY(testObject->error() == false);
395     QTRY_VERIFY(listview->currentItem());
396
397     // set invalid highlight
398     testObject->setInvalidHighlight(true);
399     QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
400     QTRY_VERIFY(testObject->error() == false);
401     QTRY_VERIFY(listview->currentItem());
402     QTRY_VERIFY(listview->highlightItem() == 0);
403
404     // back to normal highlight
405     testObject->setInvalidHighlight(false);
406     QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
407     QTRY_VERIFY(testObject->error() == false);
408     QTRY_VERIFY(listview->currentItem());
409     QTRY_VERIFY(listview->highlightItem() != 0);
410
411     // set an empty model and confirm that items are destroyed
412     T model2;
413     ctxt->setContextProperty("testModel", &model2);
414
415     // Force a layout, necessary if ListView is completed before VisualDataModel.
416     if (forceLayout)
417         QCOMPARE(listview->property("count").toInt(), 0);
418
419     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
420     QTRY_VERIFY(itemCount == 0);
421
422     QTRY_COMPARE(listview->highlightResizeVelocity(), 1000.0);
423     QTRY_COMPARE(listview->highlightMoveVelocity(), 1000.0);
424
425     delete canvas;
426     delete testObject;
427 }
428
429
430 template <class T>
431 void tst_QQuickListView::changed(const QUrl &source, bool forceLayout)
432 {
433     QQuickView *canvas = createView();
434
435     T model;
436     model.addItem("Fred", "12345");
437     model.addItem("John", "2345");
438     model.addItem("Bob", "54321");
439
440     QQmlContext *ctxt = canvas->rootContext();
441     ctxt->setContextProperty("testModel", &model);
442
443     TestObject *testObject = new TestObject;
444     ctxt->setContextProperty("testObject", testObject);
445
446     canvas->setSource(source);
447     qApp->processEvents();
448
449     QQuickFlickable *listview = findItem<QQuickFlickable>(canvas->rootObject(), "list");
450     QTRY_VERIFY(listview != 0);
451
452     QQuickItem *contentItem = listview->contentItem();
453     QTRY_VERIFY(contentItem != 0);
454
455     // Force a layout, necessary if ListView is completed before VisualDataModel.
456     if (forceLayout)
457         QCOMPARE(listview->property("count").toInt(), model.count());
458
459     model.modifyItem(1, "Will", "9876");
460     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
461     QTRY_VERIFY(name != 0);
462     QTRY_COMPARE(name->text(), model.name(1));
463     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
464     QTRY_VERIFY(number != 0);
465     QTRY_COMPARE(number->text(), model.number(1));
466
467     delete canvas;
468     delete testObject;
469 }
470
471 template <class T>
472 void tst_QQuickListView::inserted(const QUrl &source)
473 {
474     QQuickView *canvas = createView();
475     canvas->show();
476
477     T model;
478     model.addItem("Fred", "12345");
479     model.addItem("John", "2345");
480     model.addItem("Bob", "54321");
481
482     QQmlContext *ctxt = canvas->rootContext();
483     ctxt->setContextProperty("testModel", &model);
484
485     TestObject *testObject = new TestObject;
486     ctxt->setContextProperty("testObject", testObject);
487
488     canvas->setSource(source);
489     qApp->processEvents();
490
491     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
492     QTRY_VERIFY(listview != 0);
493
494     QQuickItem *contentItem = listview->contentItem();
495     QTRY_VERIFY(contentItem != 0);
496
497     model.insertItem(1, "Will", "9876");
498
499     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
500     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
501
502     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
503     QTRY_VERIFY(name != 0);
504     QTRY_COMPARE(name->text(), model.name(1));
505     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
506     QTRY_VERIFY(number != 0);
507     QTRY_COMPARE(number->text(), model.number(1));
508
509     // Confirm items positioned correctly
510     for (int i = 0; i < model.count(); ++i) {
511         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
512         QTRY_COMPARE(item->y(), i*20.0);
513     }
514
515     model.insertItem(0, "Foo", "1111"); // zero index, and current item
516
517     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
518     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
519
520     name = findItem<QQuickText>(contentItem, "textName", 0);
521     QTRY_VERIFY(name != 0);
522     QTRY_COMPARE(name->text(), model.name(0));
523     number = findItem<QQuickText>(contentItem, "textNumber", 0);
524     QTRY_VERIFY(number != 0);
525     QTRY_COMPARE(number->text(), model.number(0));
526
527     QTRY_COMPARE(listview->currentIndex(), 1);
528
529     // Confirm items positioned correctly
530     for (int i = 0; i < model.count(); ++i) {
531         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
532         QTRY_COMPARE(item->y(), i*20.0);
533     }
534
535     for (int i = model.count(); i < 30; ++i)
536         model.insertItem(i, "Hello", QString::number(i));
537
538     listview->setContentY(80);
539
540     // Insert item outside visible area
541     model.insertItem(1, "Hello", "1324");
542
543     QTRY_VERIFY(listview->contentY() == 80);
544
545     // Confirm items positioned correctly
546     for (int i = 5; i < 5+15; ++i) {
547         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
548         if (!item) qWarning() << "Item" << i << "not found";
549         QTRY_VERIFY(item);
550         QTRY_COMPARE(item->y(), i*20.0 - 20.0);
551     }
552
553 //    QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
554
555     // QTBUG-19675
556     model.clear();
557     model.insertItem(0, "Hello", "1234");
558     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
559
560     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
561     QVERIFY(item);
562     QCOMPARE(item->y(), 0.);
563     QTRY_VERIFY(listview->contentY() == 0);
564
565     delete canvas;
566     delete testObject;
567 }
568
569 template <class T>
570 void tst_QQuickListView::inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
571 {
572     QFETCH(qreal, contentY);
573     QFETCH(int, insertIndex);
574     QFETCH(int, insertCount);
575     QFETCH(qreal, itemsOffsetAfterMove);
576
577     T model;
578     for (int i = 0; i < 30; i++)
579         model.addItem("Item" + QString::number(i), "");
580
581     QQuickView *canvas = getView();
582     QQmlContext *ctxt = canvas->rootContext();
583     ctxt->setContextProperty("testModel", &model);
584
585     TestObject *testObject = new TestObject;
586     ctxt->setContextProperty("testObject", testObject);
587
588     canvas->setSource(testFileUrl("listviewtest.qml"));
589     canvas->show();
590     qApp->processEvents();
591     QTest::qWaitForWindowShown(canvas);
592
593     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
594     QTRY_VERIFY(listview != 0);
595     QQuickItem *contentItem = listview->contentItem();
596     QTRY_VERIFY(contentItem != 0);
597
598     bool waitForPolish = (contentY != 0);
599     if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
600         listview->setVerticalLayoutDirection(verticalLayoutDirection);
601         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
602         contentY = -listview->height() - contentY;
603     }
604     listview->setContentY(contentY);
605     if (waitForPolish)
606         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
607
608     QList<QPair<QString, QString> > newData;
609     for (int i=0; i<insertCount; i++)
610         newData << qMakePair(QString("value %1").arg(i), QString::number(i));
611     model.insertItems(insertIndex, newData);
612     QTRY_COMPARE(listview->property("count").toInt(), model.count());
613
614     // check visibleItems.first() is in correct position
615     QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
616     QVERIFY(item0);
617     if (verticalLayoutDirection == QQuickItemView::BottomToTop)
618         QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
619     else
620         QCOMPARE(item0->y(), itemsOffsetAfterMove);
621
622     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
623     int firstVisibleIndex = -1;
624     for (int i=0; i<items.count(); i++) {
625         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
626         if (item && !QQuickItemPrivate::get(item)->culled) {
627             firstVisibleIndex = i;
628             break;
629         }
630     }
631     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
632
633     // Confirm items positioned correctly and indexes correct
634     QQuickText *name;
635     QQuickText *number;
636     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
637         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
638         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
639         qreal pos = i*20.0 + itemsOffsetAfterMove;
640         if (verticalLayoutDirection == QQuickItemView::BottomToTop)
641             pos = -item0->height() - pos;
642         QTRY_COMPARE(item->y(), pos);
643         name = findItem<QQuickText>(contentItem, "textName", i);
644         QVERIFY(name != 0);
645         QTRY_COMPARE(name->text(), model.name(i));
646         number = findItem<QQuickText>(contentItem, "textNumber", i);
647         QVERIFY(number != 0);
648         QTRY_COMPARE(number->text(), model.number(i));
649     }
650
651     releaseView(canvas);
652     delete testObject;
653 }
654
655 void tst_QQuickListView::inserted_more_data()
656 {
657     QTest::addColumn<qreal>("contentY");
658     QTest::addColumn<int>("insertIndex");
659     QTest::addColumn<int>("insertCount");
660     QTest::addColumn<qreal>("itemsOffsetAfterMove");
661
662     QTest::newRow("add 1, before visible items")
663             << 80.0     // show 4-19
664             << 3 << 1
665             << -20.0;   // insert above first visible i.e. 0 is at -20, first visible should not move
666
667     QTest::newRow("add multiple, before visible")
668             << 80.0     // show 4-19
669             << 3 << 3
670             << -20.0 * 3;   // again first visible should not move
671
672     QTest::newRow("add 1, at start of visible, content at start")
673             << 0.0
674             << 0 << 1
675             << 0.0;
676
677     QTest::newRow("add multiple, start of visible, content at start")
678             << 0.0
679             << 0 << 3
680             << 0.0;
681
682     QTest::newRow("add 1, at start of visible, content not at start")
683             << 80.0     // show 4-19
684             << 4 << 1
685             << 0.0;
686
687     QTest::newRow("add multiple, at start of visible, content not at start")
688             << 80.0     // show 4-19
689             << 4 << 3
690             << 0.0;
691
692
693     QTest::newRow("add 1, at end of visible, content at start")
694             << 0.0
695             << 15 << 1
696             << 0.0;
697
698     QTest::newRow("add 1, at end of visible, content at start")
699             << 0.0
700             << 15 << 3
701             << 0.0;
702
703     QTest::newRow("add 1, at end of visible, content not at start")
704             << 80.0     // show 4-19
705             << 19 << 1
706             << 0.0;
707
708     QTest::newRow("add multiple, at end of visible, content not at start")
709             << 80.0     // show 4-19
710             << 19 << 3
711             << 0.0;
712
713
714     QTest::newRow("add 1, after visible, content at start")
715             << 0.0
716             << 16 << 1
717             << 0.0;
718
719     QTest::newRow("add 1, after visible, content at start")
720             << 0.0
721             << 16 << 3
722             << 0.0;
723
724     QTest::newRow("add 1, after visible, content not at start")
725             << 80.0     // show 4-19
726             << 20 << 1
727             << 0.0;
728
729     QTest::newRow("add multiple, after visible, content not at start")
730             << 80.0     // show 4-19
731             << 20 << 3
732             << 0.0;
733 }
734
735 void tst_QQuickListView::insertBeforeVisible()
736 {
737     QFETCH(int, insertIndex);
738     QFETCH(int, insertCount);
739     QFETCH(int, cacheBuffer);
740
741     QQuickText *name;
742     QQuickView *canvas = getView();
743
744     QmlListModel model;
745     for (int i = 0; i < 30; i++)
746         model.addItem("Item" + QString::number(i), "");
747
748     QQmlContext *ctxt = canvas->rootContext();
749     ctxt->setContextProperty("testModel", &model);
750
751     TestObject *testObject = new TestObject;
752     ctxt->setContextProperty("testObject", testObject);
753
754     canvas->setSource(testFileUrl("listviewtest.qml"));
755     canvas->show();
756     qApp->processEvents();
757     QTest::qWaitForWindowShown(canvas);
758
759     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
760     QTRY_VERIFY(listview != 0);
761     QQuickItem *contentItem = listview->contentItem();
762     QTRY_VERIFY(contentItem != 0);
763
764     listview->setCacheBuffer(cacheBuffer);
765     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
766
767     // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
768     int firstVisibleIndex = 20;     // move to an index where the top item is not visible
769     listview->setContentY(firstVisibleIndex * 20.0);
770     listview->setCurrentIndex(firstVisibleIndex);
771
772     qApp->processEvents();
773     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
774     QTRY_COMPARE(listview->currentIndex(), firstVisibleIndex);
775     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
776     QVERIFY(item);
777     QCOMPARE(item->y(), listview->contentY());
778
779     QList<QPair<QString, QString> > newData;
780     for (int i=0; i<insertCount; i++)
781         newData << qMakePair(QString("value %1").arg(i), QString::number(i));
782     model.insertItems(insertIndex, newData);
783     QTRY_COMPARE(listview->property("count").toInt(), model.count());
784
785     // now, moving to the top of the view should position the inserted items correctly
786     int itemsOffsetAfterMove = -(insertCount * 20);
787     listview->setCurrentIndex(0);
788     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
789     QTRY_COMPARE(listview->currentIndex(), 0);
790     QTRY_COMPARE(listview->contentY(), 0.0 + itemsOffsetAfterMove);
791
792     // Confirm items positioned correctly and indexes correct
793     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
794     for (int i = 0; i < model.count() && i < itemCount; ++i) {
795         item = findItem<QQuickItem>(contentItem, "wrapper", i);
796         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
797         QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
798         name = findItem<QQuickText>(contentItem, "textName", i);
799         QVERIFY(name != 0);
800         QTRY_COMPARE(name->text(), model.name(i));
801     }
802
803     releaseView(canvas);
804     delete testObject;
805 }
806
807 void tst_QQuickListView::insertBeforeVisible_data()
808 {
809     QTest::addColumn<int>("insertIndex");
810     QTest::addColumn<int>("insertCount");
811     QTest::addColumn<int>("cacheBuffer");
812
813     QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
814     QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
815     QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
816
817     QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
818     QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
819     QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
820
821     QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 3 << 0;
822     QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 3 << 100;
823     QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 3 << 500;
824
825     QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 3 << 0;
826     QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 3 << 100;
827     QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 3 << 500;
828 }
829
830 template <class T>
831 void tst_QQuickListView::removed(const QUrl &source, bool /* animated */)
832 {
833     QQuickView *canvas = createView();
834
835     T model;
836     for (int i = 0; i < 50; i++)
837         model.addItem("Item" + QString::number(i), "");
838
839     QQmlContext *ctxt = canvas->rootContext();
840     ctxt->setContextProperty("testModel", &model);
841
842     TestObject *testObject = new TestObject;
843     ctxt->setContextProperty("testObject", testObject);
844
845     canvas->setSource(source);
846     canvas->show();
847     qApp->processEvents();
848     QTest::qWaitForWindowShown(canvas);
849
850     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
851     QTRY_VERIFY(listview != 0);
852     QQuickItem *contentItem = listview->contentItem();
853     QTRY_VERIFY(contentItem != 0);
854     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
855
856     model.removeItem(1);
857     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
858
859     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
860     QTRY_VERIFY(name != 0);
861     QTRY_COMPARE(name->text(), model.name(1));
862     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
863     QTRY_VERIFY(number != 0);
864     QTRY_COMPARE(number->text(), model.number(1));
865
866     // Confirm items positioned correctly
867     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
868     for (int i = 0; i < model.count() && i < itemCount; ++i) {
869         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
870         if (!item) qWarning() << "Item" << i << "not found";
871         QTRY_VERIFY(item);
872         QTRY_VERIFY(item->y() == i*20);
873     }
874
875     // Remove first item (which is the current item);
876     model.removeItem(0);
877     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
878
879     name = findItem<QQuickText>(contentItem, "textName", 0);
880     QTRY_VERIFY(name != 0);
881     QTRY_COMPARE(name->text(), model.name(0));
882     number = findItem<QQuickText>(contentItem, "textNumber", 0);
883     QTRY_VERIFY(number != 0);
884     QTRY_COMPARE(number->text(), model.number(0));
885
886     // Confirm items positioned correctly
887     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
888     for (int i = 0; i < model.count() && i < itemCount; ++i) {
889         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
890         if (!item) qWarning() << "Item" << i << "not found";
891         QTRY_VERIFY(item);
892         QTRY_COMPARE(item->y(),i*20.0);
893     }
894
895     // Remove items not visible
896     model.removeItem(18);
897     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
898
899     // Confirm items positioned correctly
900     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
901     for (int i = 0; i < model.count() && i < itemCount; ++i) {
902         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
903         if (!item) qWarning() << "Item" << i << "not found";
904         QTRY_VERIFY(item);
905         QTRY_COMPARE(item->y(),i*20.0);
906     }
907
908     // Remove items before visible
909     listview->setContentY(80);
910     listview->setCurrentIndex(10);
911
912     model.removeItem(1); // post: top item will be at 20
913     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
914
915     // Confirm items positioned correctly
916     for (int i = 2; i < 18; ++i) {
917         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
918         if (!item) qWarning() << "Item" << i << "not found";
919         QTRY_VERIFY(item);
920         QTRY_COMPARE(item->y(),20+i*20.0);
921     }
922
923     // Remove current index
924     QTRY_VERIFY(listview->currentIndex() == 9);
925     QQuickItem *oldCurrent = listview->currentItem();
926     model.removeItem(9);
927
928     QTRY_COMPARE(listview->currentIndex(), 9);
929     QTRY_VERIFY(listview->currentItem() != oldCurrent);
930
931     listview->setContentY(20); // That's the top now
932     // let transitions settle.
933     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
934
935     // Confirm items positioned correctly
936     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
937     for (int i = 0; i < model.count() && i < itemCount; ++i) {
938         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
939         if (!item) qWarning() << "Item" << i << "not found";
940         QTRY_VERIFY(item);
941         QTRY_COMPARE(item->y(),20+i*20.0);
942     }
943
944     // remove current item beyond visible items.
945     listview->setCurrentIndex(20);
946     listview->setContentY(40);
947     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
948
949     model.removeItem(20);
950     QTRY_COMPARE(listview->currentIndex(), 20);
951     QTRY_VERIFY(listview->currentItem() != 0);
952
953     // remove item before current, but visible
954     listview->setCurrentIndex(8);
955     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
956     oldCurrent = listview->currentItem();
957     model.removeItem(6);
958
959     QTRY_COMPARE(listview->currentIndex(), 7);
960     QTRY_VERIFY(listview->currentItem() == oldCurrent);
961
962     listview->setContentY(80);
963     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
964
965     // remove all visible items
966     model.removeItems(1, 18);
967     QTRY_COMPARE(listview->count() , model.count());
968
969     // Confirm items positioned correctly
970     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
971     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
972         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i+1);
973         if (!item) qWarning() << "Item" << i+1 << "not found";
974         QTRY_VERIFY(item);
975         QTRY_COMPARE(item->y(),80+i*20.0);
976     }
977
978     model.removeItems(1, 17);
979     QTRY_COMPARE(listview->count() , model.count());
980
981     model.removeItems(2, 1);
982     QTRY_COMPARE(listview->count() , model.count());
983
984     model.addItem("New", "1");
985     QTRY_COMPARE(listview->count() , model.count());
986
987     QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", model.count()-1));
988     QCOMPARE(name->text(), QString("New"));
989
990     // Add some more items so that we don't run out
991     model.clear();
992     for (int i = 0; i < 50; i++)
993         model.addItem("Item" + QString::number(i), "");
994
995     // QTBUG-QTBUG-20575
996     listview->setCurrentIndex(0);
997     listview->setContentY(30);
998     model.removeItem(0);
999     QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", 0));
1000
1001     // QTBUG-19198 move to end and remove all visible items one at a time.
1002     listview->positionViewAtEnd();
1003     for (int i = 0; i < 18; ++i)
1004         model.removeItems(model.count() - 1, 1);
1005     QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() > 16);
1006
1007     delete canvas;
1008     delete testObject;
1009 }
1010
1011 template <class T>
1012 void tst_QQuickListView::removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1013 {
1014     QFETCH(qreal, contentY);
1015     QFETCH(int, removeIndex);
1016     QFETCH(int, removeCount);
1017     QFETCH(qreal, itemsOffsetAfterMove);
1018
1019     QQuickView *canvas = getView();
1020
1021     T model;
1022     for (int i = 0; i < 30; i++)
1023         model.addItem("Item" + QString::number(i), "");
1024
1025     QQmlContext *ctxt = canvas->rootContext();
1026     ctxt->setContextProperty("testModel", &model);
1027
1028     TestObject *testObject = new TestObject;
1029     ctxt->setContextProperty("testObject", testObject);
1030
1031     canvas->setSource(source);
1032     canvas->show();
1033     qApp->processEvents();
1034     QTest::qWaitForWindowShown(canvas);
1035
1036     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1037     QTRY_VERIFY(listview != 0);
1038     QQuickItem *contentItem = listview->contentItem();
1039     QTRY_VERIFY(contentItem != 0);
1040
1041     bool waitForPolish = (contentY != 0);
1042     if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
1043         listview->setVerticalLayoutDirection(verticalLayoutDirection);
1044         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1045         contentY = -listview->height() - contentY;
1046     }
1047     listview->setContentY(contentY);
1048     if (waitForPolish)
1049         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1050
1051     model.removeItems(removeIndex, removeCount);
1052     QTRY_COMPARE(listview->property("count").toInt(), model.count());
1053
1054     // check visibleItems.first() is in correct position
1055     QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
1056     QVERIFY(item0);
1057     QVERIFY(item0);
1058     if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1059         QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
1060     else
1061         QCOMPARE(item0->y(), itemsOffsetAfterMove);
1062
1063     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1064     int firstVisibleIndex = -1;
1065     for (int i=0; i<items.count(); i++) {
1066         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1067         if (item && delegateVisible(item)) {
1068             firstVisibleIndex = i;
1069             break;
1070         }
1071     }
1072     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1073
1074     // Confirm items positioned correctly and indexes correct
1075     QQuickText *name;
1076     QQuickText *number;
1077     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1078         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1079         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1080         qreal pos = i*20.0 + itemsOffsetAfterMove;
1081         if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1082             pos = -item0->height() - pos;
1083         QTRY_COMPARE(item->y(), pos);
1084         name = findItem<QQuickText>(contentItem, "textName", i);
1085         QVERIFY(name != 0);
1086         QTRY_COMPARE(name->text(), model.name(i));
1087         number = findItem<QQuickText>(contentItem, "textNumber", i);
1088         QVERIFY(number != 0);
1089         QTRY_COMPARE(number->text(), model.number(i));
1090     }
1091
1092     releaseView(canvas);
1093     delete testObject;
1094 }
1095
1096 void tst_QQuickListView::removed_more_data()
1097 {
1098     QTest::addColumn<qreal>("contentY");
1099     QTest::addColumn<int>("removeIndex");
1100     QTest::addColumn<int>("removeCount");
1101     QTest::addColumn<qreal>("itemsOffsetAfterMove");
1102
1103     QTest::newRow("remove 1, before visible items")
1104             << 80.0     // show 4-19
1105             << 3 << 1
1106             << 20.0;   // visible items slide down by 1 item so that first visible does not move
1107
1108     QTest::newRow("remove multiple, all before visible items")
1109             << 80.0
1110             << 1 << 3
1111             << 20.0 * 3;
1112
1113     QTest::newRow("remove multiple, all before visible items, remove item 0")
1114             << 80.0
1115             << 0 << 4
1116             << 20.0 * 4;
1117
1118     // remove 1,2,3 before the visible pos, 0 moves down to just before the visible pos,
1119     // items 4,5 are removed from view, item 6 slides up to original pos of item 4 (80px)
1120     QTest::newRow("remove multiple, mix of items from before and within visible items")
1121             << 80.0
1122             << 1 << 5
1123             << 20.0 * 3;    // adjust for the 3 items removed before the visible
1124
1125     QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1126             << 80.0
1127             << 0 << 6
1128             << 20.0 * 4;    // adjust for the 3 items removed before the visible
1129
1130
1131     QTest::newRow("remove 1, from start of visible, content at start")
1132             << 0.0
1133             << 0 << 1
1134             << 0.0;
1135
1136     QTest::newRow("remove multiple, from start of visible, content at start")
1137             << 0.0
1138             << 0 << 3
1139             << 0.0;
1140
1141     QTest::newRow("remove 1, from start of visible, content not at start")
1142             << 80.0     // show 4-19
1143             << 4 << 1
1144             << 0.0;
1145
1146     QTest::newRow("remove multiple, from start of visible, content not at start")
1147             << 80.0     // show 4-19
1148             << 4 << 3
1149             << 0.0;
1150
1151
1152     QTest::newRow("remove 1, from middle of visible, content at start")
1153             << 0.0
1154             << 10 << 1
1155             << 0.0;
1156
1157     QTest::newRow("remove multiple, from middle of visible, content at start")
1158             << 0.0
1159             << 10 << 5
1160             << 0.0;
1161
1162     QTest::newRow("remove 1, from middle of visible, content not at start")
1163             << 80.0     // show 4-19
1164             << 10 << 1
1165             << 0.0;
1166
1167     QTest::newRow("remove multiple, from middle of visible, content not at start")
1168             << 80.0     // show 4-19
1169             << 10 << 5
1170             << 0.0;
1171
1172
1173     QTest::newRow("remove 1, after visible, content at start")
1174             << 0.0
1175             << 16 << 1
1176             << 0.0;
1177
1178     QTest::newRow("remove multiple, after visible, content at start")
1179             << 0.0
1180             << 16 << 5
1181             << 0.0;
1182
1183     QTest::newRow("remove 1, after visible, content not at middle")
1184             << 80.0     // show 4-19
1185             << 16+4 << 1
1186             << 0.0;
1187
1188     QTest::newRow("remove multiple, after visible, content not at start")
1189             << 80.0     // show 4-19
1190             << 16+4 << 5
1191             << 0.0;
1192
1193     QTest::newRow("remove multiple, mix of items from within and after visible items")
1194             << 80.0
1195             << 18 << 5
1196             << 0.0;
1197 }
1198
1199 template <class T>
1200 void tst_QQuickListView::clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1201 {
1202     QQuickView *canvas = createView();
1203
1204     T model;
1205     for (int i = 0; i < 30; i++)
1206         model.addItem("Item" + QString::number(i), "");
1207
1208     QQmlContext *ctxt = canvas->rootContext();
1209     ctxt->setContextProperty("testModel", &model);
1210
1211     TestObject *testObject = new TestObject;
1212     ctxt->setContextProperty("testObject", testObject);
1213
1214     canvas->setSource(source);
1215     canvas->show();
1216     qApp->processEvents();
1217
1218     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1219     QTRY_VERIFY(listview != 0);
1220     QQuickItem *contentItem = listview->contentItem();
1221     QTRY_VERIFY(contentItem != 0);
1222
1223     listview->setVerticalLayoutDirection(verticalLayoutDirection);
1224     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1225
1226     model.clear();
1227
1228     QTRY_COMPARE(findItems<QQuickListView>(contentItem, "wrapper").count(), 0);
1229     QTRY_VERIFY(listview->count() == 0);
1230     QTRY_VERIFY(listview->currentItem() == 0);
1231     if (verticalLayoutDirection == QQuickItemView::TopToBottom)
1232         QTRY_COMPARE(listview->contentY(), 0.0);
1233     else
1234         QTRY_COMPARE(listview->contentY(), -listview->height());
1235     QVERIFY(listview->currentIndex() == -1);
1236
1237     QCOMPARE(listview->contentHeight(), 0.0);
1238
1239     // confirm sanity when adding an item to cleared list
1240     model.addItem("New", "1");
1241     QTRY_VERIFY(listview->count() == 1);
1242     QVERIFY(listview->currentItem() != 0);
1243     QVERIFY(listview->currentIndex() == 0);
1244
1245     delete canvas;
1246     delete testObject;
1247 }
1248
1249 template <class T>
1250 void tst_QQuickListView::moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1251 {
1252     QFETCH(qreal, contentY);
1253     QFETCH(int, from);
1254     QFETCH(int, to);
1255     QFETCH(int, count);
1256     QFETCH(qreal, itemsOffsetAfterMove);
1257
1258     QQuickText *name;
1259     QQuickText *number;
1260     QQuickView *canvas = getView();
1261
1262     T model;
1263     for (int i = 0; i < 30; i++)
1264         model.addItem("Item" + QString::number(i), "");
1265
1266     QQmlContext *ctxt = canvas->rootContext();
1267     ctxt->setContextProperty("testModel", &model);
1268
1269     TestObject *testObject = new TestObject;
1270     ctxt->setContextProperty("testObject", testObject);
1271
1272     canvas->setSource(source);
1273     canvas->show();
1274     qApp->processEvents();
1275     QTest::qWaitForWindowShown(canvas);
1276
1277     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1278     QTRY_VERIFY(listview != 0);
1279     QQuickItem *contentItem = listview->contentItem();
1280     QTRY_VERIFY(contentItem != 0);
1281
1282     // always need to wait for view to be painted before the first move()
1283     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1284
1285     bool waitForPolish = (contentY != 0);
1286     if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
1287         listview->setVerticalLayoutDirection(verticalLayoutDirection);
1288         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1289         contentY = -listview->height() - contentY;
1290     }
1291     listview->setContentY(contentY);
1292     if (waitForPolish)
1293         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1294
1295     model.moveItems(from, to, count);
1296     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1297
1298     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1299     int firstVisibleIndex = -1;
1300     for (int i=0; i<items.count(); i++) {
1301         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1302         if (item && delegateVisible(item)) {
1303             firstVisibleIndex = i;
1304             break;
1305         }
1306     }
1307     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1308
1309     // Confirm items positioned correctly and indexes correct
1310     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1311         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1312         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1313         qreal pos = i*20.0 + itemsOffsetAfterMove;
1314         if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1315             pos = -item->height() - pos;
1316         QTRY_COMPARE(item->y(), pos);
1317         name = findItem<QQuickText>(contentItem, "textName", i);
1318         QVERIFY(name != 0);
1319         QTRY_COMPARE(name->text(), model.name(i));
1320         number = findItem<QQuickText>(contentItem, "textNumber", i);
1321         QVERIFY(number != 0);
1322         QTRY_COMPARE(number->text(), model.number(i));
1323
1324         // current index should have been updated
1325         if (item == listview->currentItem())
1326             QTRY_COMPARE(listview->currentIndex(), i);
1327     }
1328
1329     releaseView(canvas);
1330     delete testObject;
1331 }
1332
1333 void tst_QQuickListView::moved_data()
1334 {
1335     QTest::addColumn<qreal>("contentY");
1336     QTest::addColumn<int>("from");
1337     QTest::addColumn<int>("to");
1338     QTest::addColumn<int>("count");
1339     QTest::addColumn<qreal>("itemsOffsetAfterMove");
1340
1341     // model starts with 30 items, each 20px high, in area 320px high
1342     // 16 items should be visible at a time
1343     // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved
1344
1345     QTest::newRow("move 1 forwards, within visible items")
1346             << 0.0
1347             << 1 << 4 << 1
1348             << 0.0;
1349
1350     QTest::newRow("move 1 forwards, from non-visible -> visible")
1351             << 80.0     // show 4-19
1352             << 1 << 18 << 1
1353             << 20.0;    // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement
1354
1355     QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1356             << 80.0     // show 4-19
1357             << 0 << 4 << 1
1358             << 20.0;    // first item has moved to below item4, everything drops down by size of 1 item
1359
1360     QTest::newRow("move 1 forwards, from visible -> non-visible")
1361             << 0.0
1362             << 1 << 16 << 1
1363             << 0.0;
1364
1365     QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1366             << 0.0
1367             << 0 << 16 << 1
1368             << 0.0;
1369
1370
1371     QTest::newRow("move 1 backwards, within visible items")
1372             << 0.0
1373             << 4 << 1 << 1
1374             << 0.0;
1375
1376     QTest::newRow("move 1 backwards, within visible items (to first index)")
1377             << 0.0
1378             << 4 << 0 << 1
1379             << 0.0;
1380
1381     QTest::newRow("move 1 backwards, from non-visible -> visible")
1382             << 0.0
1383             << 20 << 4 << 1
1384             << 0.0;
1385
1386     QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1387             << 0.0
1388             << 29 << 15 << 1
1389             << 0.0;
1390
1391     QTest::newRow("move 1 backwards, from visible -> non-visible")
1392             << 80.0     // show 4-19
1393             << 16 << 1 << 1
1394             << -20.0;   // to minimize movement, item 0 moves to -20, and other items do not move
1395
1396     QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1397             << 80.0     // show 4-19
1398             << 16 << 0 << 1
1399             << -20.0;   // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move
1400
1401
1402     QTest::newRow("move multiple forwards, within visible items")
1403             << 0.0
1404             << 0 << 5 << 3
1405             << 0.0;
1406
1407     QTest::newRow("move multiple forwards, before visible items")
1408             << 140.0     // show 7-22
1409             << 4 << 5 << 3      // 4,5,6 move to below 7
1410             << 20.0 * 3;      // 4,5,6 moved down
1411
1412     QTest::newRow("move multiple forwards, from non-visible -> visible")
1413             << 80.0     // show 4-19
1414             << 1 << 5 << 3
1415             << 20.0 * 3;    // moving 3 from above the content y should adjust y positions accordingly
1416
1417     QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1418             << 80.0     // show 4-19
1419             << 0 << 5 << 3
1420             << 20.0 * 3;        // moving 3 from above the content y should adjust y positions accordingly
1421
1422     QTest::newRow("move multiple forwards, mix of non-visible/visible")
1423             << 40.0
1424             << 1 << 16 << 2
1425             << 20.0;    // item 1,2 are removed, item 3 is now first visible
1426
1427     QTest::newRow("move multiple forwards, to bottom of view")
1428             << 0.0
1429             << 5 << 13 << 3
1430             << 0.0;
1431
1432     QTest::newRow("move multiple forwards, to bottom of view, first->last")
1433             << 0.0
1434             << 0 << 13 << 3
1435             << 0.0;
1436
1437     QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1438             << 80.0
1439             << 5+4 << 13+4 << 3
1440             << 0.0;
1441
1442     QTest::newRow("move multiple forwards, from visible -> non-visible")
1443             << 0.0
1444             << 1 << 16 << 3
1445             << 0.0;
1446
1447     QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1448             << 0.0
1449             << 0 << 16 << 3
1450             << 0.0;
1451
1452
1453     QTest::newRow("move multiple backwards, within visible items")
1454             << 0.0
1455             << 4 << 1 << 3
1456             << 0.0;
1457
1458     QTest::newRow("move multiple backwards, within visible items (move first item)")
1459             << 0.0
1460             << 10 << 0 << 3
1461             << 0.0;
1462
1463     QTest::newRow("move multiple backwards, from non-visible -> visible")
1464             << 0.0
1465             << 20 << 4 << 3
1466             << 0.0;
1467
1468     QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1469             << 0.0
1470             << 27 << 10 << 3
1471             << 0.0;
1472
1473     QTest::newRow("move multiple backwards, from visible -> non-visible")
1474             << 80.0     // show 4-19
1475             << 16 << 1 << 3
1476             << -20.0 * 3;   // to minimize movement, 0 moves by -60, and other items do not move
1477
1478     QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1479             << 80.0     // show 4-19
1480             << 16 << 0 << 3
1481             << -20.0 * 3;   // to minimize movement, 16,17,18 move to above item 0, and other items do not move
1482 }
1483
1484 void tst_QQuickListView::multipleChanges(bool condensed)
1485 {
1486     QFETCH(int, startCount);
1487     QFETCH(QList<ListChange>, changes);
1488     QFETCH(int, newCount);
1489     QFETCH(int, newCurrentIndex);
1490
1491     QQuickView *canvas = getView();
1492
1493     QmlListModel model;
1494     for (int i = 0; i < startCount; i++)
1495         model.addItem("Item" + QString::number(i), "");
1496
1497     QQmlContext *ctxt = canvas->rootContext();
1498     ctxt->setContextProperty("testModel", &model);
1499
1500     TestObject *testObject = new TestObject;
1501     ctxt->setContextProperty("testObject", testObject);
1502
1503     canvas->setSource(testFileUrl("listviewtest.qml"));
1504     canvas->show();
1505     qApp->processEvents();
1506     QTest::qWaitForWindowShown(canvas);
1507
1508     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1509     QTRY_VERIFY(listview != 0);
1510     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1511
1512     for (int i=0; i<changes.count(); i++) {
1513         switch (changes[i].type) {
1514             case ListChange::Inserted:
1515             {
1516                 QList<QPair<QString, QString> > items;
1517                 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1518                     items << qMakePair(QString("new item %1").arg(j), QString::number(j));
1519                 model.insertItems(changes[i].index, items);
1520                 break;
1521             }
1522             case ListChange::Removed:
1523                 model.removeItems(changes[i].index, changes[i].count);
1524                 break;
1525             case ListChange::Moved:
1526                 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1527                 break;
1528             case ListChange::SetCurrent:
1529                 listview->setCurrentIndex(changes[i].index);
1530                 break;
1531             case ListChange::SetContentY:
1532                 listview->setContentY(changes[i].pos);
1533                 break;
1534             default:
1535                 continue;
1536         }
1537         if (!condensed) {
1538             QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1539         }
1540     }
1541     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1542
1543     QCOMPARE(listview->count(), newCount);
1544     QCOMPARE(listview->count(), model.count());
1545     QCOMPARE(listview->currentIndex(), newCurrentIndex);
1546
1547     QQuickText *name;
1548     QQuickText *number;
1549     QQuickItem *contentItem = listview->contentItem();
1550     QTRY_VERIFY(contentItem != 0);
1551     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1552     for (int i=0; i < model.count() && i < itemCount; ++i) {
1553         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1554         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1555         name = findItem<QQuickText>(contentItem, "textName", i);
1556         QVERIFY(name != 0);
1557         QTRY_COMPARE(name->text(), model.name(i));
1558         number = findItem<QQuickText>(contentItem, "textNumber", i);
1559         QVERIFY(number != 0);
1560         QTRY_COMPARE(number->text(), model.number(i));
1561     }
1562
1563     delete testObject;
1564     releaseView(canvas);
1565 }
1566
1567 void tst_QQuickListView::multipleChanges_data()
1568 {
1569     QTest::addColumn<int>("startCount");
1570     QTest::addColumn<QList<ListChange> >("changes");
1571     QTest::addColumn<int>("newCount");
1572     QTest::addColumn<int>("newCurrentIndex");
1573
1574     QList<ListChange> changes;
1575
1576     for (int i=1; i<30; i++)
1577         changes << ListChange::remove(0);
1578     QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1579
1580     changes << ListChange::remove(0);
1581     QTest::newRow("remove all") << 30 << changes << 0 << -1;
1582
1583     changes.clear();
1584     changes << ListChange::setCurrent(29);
1585     for (int i=29; i>0; i--)
1586         changes << ListChange::remove(i);
1587     QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1588
1589     QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1590             << ListChange::remove(0, 1)
1591             << ListChange::insert(0, 1)
1592             ) << 10 << 1;
1593
1594     QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1595             << ListChange::setCurrent(2)
1596             << ListChange::remove(2, 1)
1597             << ListChange::insert(2, 1)
1598             ) << 10 << 3;
1599
1600     QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1601             << ListChange::setCurrent(1)
1602             << ListChange::remove(1, 3)
1603             << ListChange::insert(2, 2)
1604             ) << 9 << 1;
1605
1606     QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1607             << ListChange::setCurrent(2)
1608             << ListChange::remove(1, 3)
1609             << ListChange::move(1, 5, 1)
1610             ) << 7 << 5;
1611
1612     QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1613             << ListChange::setCurrent(5)
1614             << ListChange::remove(4, 3)
1615             << ListChange::move(4, 1, 1)
1616             ) << 7 << 1;
1617
1618
1619     QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1620             << ListChange::insert(0, 2)
1621             << ListChange::insert(0, 4)
1622             << ListChange::insert(0, 6)
1623             ) << 12 << 10;
1624
1625     QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1626             << ListChange::insert(0, 2)
1627             << ListChange::insert(0, 4)
1628             << ListChange::insert(0, 6)
1629             << ListChange::setCurrent(3)
1630             << ListChange::insert(3, 2)
1631             ) << 14 << 5;
1632
1633     QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1634             << ListChange::insert(0, 30)
1635             << ListChange::remove(0, 30)
1636             ) << 0 << -1;
1637
1638     QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1639             << ListChange::insert(1)
1640             << ListChange::setCurrent(1)
1641             << ListChange::remove(1)
1642             ) << 30 << 1;
1643
1644     QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1645             << ListChange::insert(0, 10)
1646             << ListChange::remove(5, 10)
1647             ) << 10 << 5;
1648
1649     QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1650             << ListChange::insert(0, 3)
1651             << ListChange::move(0, 10, 3)
1652             ) << 13 << 0;
1653
1654     QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1655             << ListChange::insert(0, 3)
1656             << ListChange::move(0, 8, 5)
1657             ) << 13 << 11;
1658
1659     QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1660             << ListChange::setCurrent(9)
1661             << ListChange::insert(10, 3)
1662             << ListChange::move(8, 0, 5)
1663             ) << 13 << 1;
1664
1665
1666     QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1667             << ListChange::setCurrent(1)
1668             << ListChange::move(1, 2, 2)
1669             << ListChange::move(2, 1, 2)
1670             ) << 10 << 1;
1671
1672     QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1673             << ListChange::setCurrent(2)
1674             << ListChange::move(1, 2, 3)
1675             << ListChange::move(3, 0, 5)
1676             ) << 10 << 0;
1677
1678     QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1679             << ListChange::setCurrent(5)
1680             << ListChange::move(5, 0, 1)
1681             << ListChange::remove(0)
1682             ) << 9 << 0;
1683
1684     QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1685             << ListChange::setCurrent(5)
1686             << ListChange::move(5, 0, 1)
1687             << ListChange::insert(0)
1688             ) << 11 << 1;
1689
1690     QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1691             << ListChange::setCurrent(1)
1692             << ListChange::move(5, 1, 3)
1693             << ListChange::remove(1, 3)
1694             ) << 7 << 1;
1695
1696     QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1697             << ListChange::setCurrent(5)
1698             << ListChange::move(5, 1, 3)
1699             << ListChange::insert(1, 5)
1700             ) << 15 << 6;
1701
1702     QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1703             << ListChange::setCurrent(3)
1704             << ListChange::move(0, 1, 2)
1705             << ListChange::insert(3, 5)
1706             ) << 15 << 8;
1707
1708     QTest::newRow("clear current") << 0 << (QList<ListChange>()
1709             << ListChange::insert(0, 5)
1710             << ListChange::setCurrent(-1)
1711             << ListChange::remove(0, 5)
1712             << ListChange::insert(0, 5)
1713             ) << 5 << -1;
1714
1715     QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1716             << ListChange::remove(20, 5)
1717             << ListChange::setContentY(20)
1718             ) << 25 << 0;
1719
1720     QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1721             << ListChange::insert(9, 5)
1722             << ListChange::setContentY(20)
1723             ) << 15 << 0;
1724
1725     QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1726             << ListChange::move(15, 8, 3)
1727             << ListChange::setContentY(0)
1728             ) << 20 << 0;
1729
1730     QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1731             << ListChange::setContentY(20)
1732             << ListChange::remove(0, 30)
1733             << ListChange::insert(0, 2)
1734             << ListChange::setContentY(0)
1735             ) << 2 << 0;
1736 }
1737
1738 void tst_QQuickListView::swapWithFirstItem()
1739 {
1740     QQuickView *canvas = createView();
1741
1742     QmlListModel model;
1743     for (int i = 0; i < 30; i++)
1744         model.addItem("Item" + QString::number(i), "");
1745
1746     QQmlContext *ctxt = canvas->rootContext();
1747     ctxt->setContextProperty("testModel", &model);
1748
1749     TestObject *testObject = new TestObject;
1750     ctxt->setContextProperty("testObject", testObject);
1751
1752     canvas->setSource(testFileUrl("listviewtest.qml"));
1753     canvas->show();
1754     qApp->processEvents();
1755
1756     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1757     QTRY_VERIFY(listview != 0);
1758     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1759
1760     // ensure content position is stable
1761     listview->setContentY(0);
1762     model.moveItem(1, 0);
1763     QTRY_VERIFY(listview->contentY() == 0);
1764
1765     delete testObject;
1766     delete canvas;
1767 }
1768
1769 void tst_QQuickListView::enforceRange()
1770 {
1771     QQuickView *canvas = createView();
1772
1773     QmlListModel model;
1774     for (int i = 0; i < 30; i++)
1775         model.addItem("Item" + QString::number(i), "");
1776
1777     QQmlContext *ctxt = canvas->rootContext();
1778     ctxt->setContextProperty("testModel", &model);
1779
1780     canvas->setSource(testFileUrl("listview-enforcerange.qml"));
1781     canvas->show();
1782     qApp->processEvents();
1783
1784     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1785     QTRY_VERIFY(listview != 0);
1786
1787     QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0);
1788     QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0);
1789     QTRY_COMPARE(listview->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
1790     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1791
1792     QQuickItem *contentItem = listview->contentItem();
1793     QTRY_VERIFY(contentItem != 0);
1794
1795     // view should be positioned at the top of the range.
1796     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
1797     QTRY_VERIFY(item);
1798     QTRY_COMPARE(listview->contentY(), -100.0);
1799
1800     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1801     QTRY_VERIFY(name != 0);
1802     QTRY_COMPARE(name->text(), model.name(0));
1803     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
1804     QTRY_VERIFY(number != 0);
1805     QTRY_COMPARE(number->text(), model.number(0));
1806
1807     // Check currentIndex is updated when contentItem moves
1808     listview->setContentY(20);
1809
1810     QTRY_COMPARE(listview->currentIndex(), 6);
1811
1812     // change model
1813     QmlListModel model2;
1814     for (int i = 0; i < 5; i++)
1815         model2.addItem("Item" + QString::number(i), "");
1816
1817     ctxt->setContextProperty("testModel", &model2);
1818     QCOMPARE(listview->count(), 5);
1819
1820     delete canvas;
1821 }
1822
1823 void tst_QQuickListView::enforceRange_withoutHighlight()
1824 {
1825     // QTBUG-20287
1826     // If no highlight is set but StrictlyEnforceRange is used, the content should still move
1827     // to the correct position (i.e. to the next/previous item, not next/previous section)
1828     // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex()
1829
1830     QQuickView *canvas = createView();
1831
1832     QmlListModel model;
1833     model.addItem("Item 0", "a");
1834     model.addItem("Item 1", "b");
1835     model.addItem("Item 2", "b");
1836     model.addItem("Item 3", "c");
1837
1838     QQmlContext *ctxt = canvas->rootContext();
1839     ctxt->setContextProperty("testModel", &model);
1840
1841     canvas->setSource(testFileUrl("listview-enforcerange-nohighlight.qml"));
1842     canvas->show();
1843     qApp->processEvents();
1844
1845     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1846     QTRY_VERIFY(listview != 0);
1847     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1848
1849     qreal expectedPos = -100.0;
1850
1851     expectedPos += 10.0;    // scroll past 1st section's delegate (10px height)
1852     QTRY_COMPARE(listview->contentY(), expectedPos);
1853
1854     expectedPos += 20 + 10;     // scroll past 1st section and section delegate of 2nd section
1855     QTest::keyClick(canvas, Qt::Key_Down);
1856
1857     QTRY_COMPARE(listview->contentY(), expectedPos);
1858
1859     expectedPos += 20;     // scroll past 1st item of 2nd section
1860     QTest::keyClick(canvas, Qt::Key_Down);
1861     QTRY_COMPARE(listview->contentY(), expectedPos);
1862
1863     expectedPos += 20 + 10;     // scroll past 2nd item of 2nd section and section delegate of 3rd section
1864     QTest::keyClick(canvas, Qt::Key_Down);
1865     QTRY_COMPARE(listview->contentY(), expectedPos);
1866
1867     delete canvas;
1868 }
1869
1870 void tst_QQuickListView::spacing()
1871 {
1872     QQuickView *canvas = createView();
1873
1874     QmlListModel model;
1875     for (int i = 0; i < 30; i++)
1876         model.addItem("Item" + QString::number(i), "");
1877
1878     QQmlContext *ctxt = canvas->rootContext();
1879     ctxt->setContextProperty("testModel", &model);
1880
1881     TestObject *testObject = new TestObject;
1882     ctxt->setContextProperty("testObject", testObject);
1883
1884     canvas->setSource(testFileUrl("listviewtest.qml"));
1885     canvas->show();
1886     qApp->processEvents();
1887
1888     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1889     QTRY_VERIFY(listview != 0);
1890
1891     QQuickItem *contentItem = listview->contentItem();
1892     QTRY_VERIFY(contentItem != 0);
1893     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1894
1895     // Confirm items positioned correctly
1896     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1897     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1898         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1899         if (!item) qWarning() << "Item" << i << "not found";
1900         QTRY_VERIFY(item);
1901         QTRY_VERIFY(item->y() == i*20);
1902     }
1903
1904     listview->setSpacing(10);
1905     QTRY_VERIFY(listview->spacing() == 10);
1906
1907     // Confirm items positioned correctly
1908     QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 11);
1909     for (int i = 0; i < 11; ++i) {
1910         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1911         if (!item) qWarning() << "Item" << i << "not found";
1912         QTRY_VERIFY(item);
1913         QTRY_VERIFY(item->y() == i*30);
1914     }
1915
1916     listview->setSpacing(0);
1917
1918     // Confirm items positioned correctly
1919     QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() >= 16);
1920     for (int i = 0; i < 16; ++i) {
1921         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1922         if (!item) qWarning() << "Item" << i << "not found";
1923         QTRY_VERIFY(item);
1924         QTRY_COMPARE(item->y(), i*20.0);
1925     }
1926
1927     delete canvas;
1928     delete testObject;
1929 }
1930
1931 template <typename T>
1932 void tst_QQuickListView::sections(const QUrl &source)
1933 {
1934     QQuickView *canvas = createView();
1935
1936     T model;
1937     for (int i = 0; i < 30; i++)
1938         model.addItem("Item" + QString::number(i), QString::number(i/5));
1939
1940     QQmlContext *ctxt = canvas->rootContext();
1941     ctxt->setContextProperty("testModel", &model);
1942
1943     canvas->setSource(source);
1944     canvas->show();
1945     qApp->processEvents();
1946
1947     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1948     QTRY_VERIFY(listview != 0);
1949
1950     QQuickItem *contentItem = listview->contentItem();
1951     QTRY_VERIFY(contentItem != 0);
1952
1953     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1954
1955     // Confirm items positioned correctly
1956     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1957     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1958         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1959         QVERIFY(item);
1960         QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20));
1961         QQuickText *next = findItem<QQuickText>(item, "nextSection");
1962         QCOMPARE(next->text().toInt(), (i+1)/5);
1963     }
1964
1965     QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1966
1967     // Remove section boundary
1968     model.removeItem(5);
1969     QTRY_COMPARE(listview->count(), model.count());
1970
1971     // New section header created
1972     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1973     QTRY_VERIFY(item);
1974     QTRY_COMPARE(item->height(), 40.0);
1975
1976     model.insertItem(3, "New Item", "0");
1977     QTRY_COMPARE(listview->count(), model.count());
1978
1979     // Section header moved
1980     item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1981     QTRY_VERIFY(item);
1982     QTRY_COMPARE(item->height(), 20.0);
1983
1984     item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1985     QTRY_VERIFY(item);
1986     QTRY_COMPARE(item->height(), 40.0);
1987
1988     // insert item which will become a section header
1989     model.insertItem(6, "Replace header", "1");
1990     QTRY_COMPARE(listview->count(), model.count());
1991
1992     item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1993     QTRY_VERIFY(item);
1994     QTRY_COMPARE(item->height(), 40.0);
1995
1996     item = findItem<QQuickItem>(contentItem, "wrapper", 7);
1997     QTRY_VERIFY(item);
1998     QTRY_COMPARE(item->height(), 20.0);
1999
2000     QTRY_COMPARE(listview->currentSection(), QString("0"));
2001
2002     listview->setContentY(140);
2003     QTRY_COMPARE(listview->currentSection(), QString("1"));
2004
2005     QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
2006
2007     listview->setContentY(20);
2008     QTRY_COMPARE(listview->currentSection(), QString("0"));
2009
2010     QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
2011
2012     item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2013     QTRY_VERIFY(item);
2014     QTRY_COMPARE(item->height(), 20.0);
2015
2016     // check that headers change when item changes
2017     listview->setContentY(0);
2018     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2019     model.modifyItem(0, "changed", "2");
2020     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2021
2022     item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2023     QTRY_VERIFY(item);
2024     QTRY_COMPARE(item->height(), 40.0);
2025
2026     delete canvas;
2027 }
2028
2029 void tst_QQuickListView::sectionsDelegate()
2030 {
2031     QQuickView *canvas = createView();
2032
2033     QmlListModel model;
2034     for (int i = 0; i < 30; i++)
2035         model.addItem("Item" + QString::number(i), QString::number(i/5));
2036
2037     QQmlContext *ctxt = canvas->rootContext();
2038     ctxt->setContextProperty("testModel", &model);
2039
2040     canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2041     canvas->show();
2042     qApp->processEvents();
2043
2044     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2045     QTRY_VERIFY(listview != 0);
2046
2047     QQuickItem *contentItem = listview->contentItem();
2048     QTRY_VERIFY(contentItem != 0);
2049
2050     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2051
2052     // Confirm items positioned correctly
2053     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2054     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2055         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2056         QTRY_VERIFY(item);
2057         QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20));
2058         QQuickText *next = findItem<QQuickText>(item, "nextSection");
2059         QCOMPARE(next->text().toInt(), (i+1)/5);
2060     }
2061
2062     for (int i = 0; i < 3; ++i) {
2063         QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2064         QVERIFY(item);
2065         QTRY_COMPARE(item->y(), qreal(i*20*6));
2066     }
2067
2068     // change section
2069     model.modifyItem(0, "One", "aaa");
2070     model.modifyItem(1, "Two", "aaa");
2071     model.modifyItem(2, "Three", "aaa");
2072     model.modifyItem(3, "Four", "aaa");
2073     model.modifyItem(4, "Five", "aaa");
2074     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2075
2076     for (int i = 0; i < 3; ++i) {
2077         QQuickItem *item = findItem<QQuickItem>(contentItem,
2078                 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2079         QVERIFY(item);
2080         QTRY_COMPARE(item->y(), qreal(i*20*6));
2081     }
2082
2083     // remove section boundary
2084     model.removeItem(5);
2085     QTRY_COMPARE(listview->count(), model.count());
2086     for (int i = 0; i < 3; ++i) {
2087         QQuickItem *item = findItem<QQuickItem>(contentItem,
2088                 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2089         QVERIFY(item);
2090     }
2091
2092     // QTBUG-17606
2093     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "sect_1");
2094     QCOMPARE(items.count(), 1);
2095
2096     // QTBUG-17759
2097     model.modifyItem(0, "One", "aaa");
2098     model.modifyItem(1, "One", "aaa");
2099     model.modifyItem(2, "One", "aaa");
2100     model.modifyItem(3, "Four", "aaa");
2101     model.modifyItem(4, "Four", "aaa");
2102     model.modifyItem(5, "Four", "aaa");
2103     model.modifyItem(6, "Five", "aaa");
2104     model.modifyItem(7, "Five", "aaa");
2105     model.modifyItem(8, "Five", "aaa");
2106     model.modifyItem(9, "Two", "aaa");
2107     model.modifyItem(10, "Two", "aaa");
2108     model.modifyItem(11, "Two", "aaa");
2109     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2110     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_aaa").count(), 1);
2111     canvas->rootObject()->setProperty("sectionProperty", "name");
2112     // ensure view has settled.
2113     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_Four").count(), 1);
2114     for (int i = 0; i < 4; ++i) {
2115         QQuickItem *item = findItem<QQuickItem>(contentItem,
2116                 "sect_" + model.name(i*3));
2117         QVERIFY(item);
2118         QTRY_COMPARE(item->y(), qreal(i*20*4));
2119     }
2120
2121     delete canvas;
2122 }
2123
2124 void tst_QQuickListView::sectionsDragOutsideBounds_data()
2125 {
2126     QTest::addColumn<int>("distance");
2127     QTest::addColumn<int>("cacheBuffer");
2128
2129     QTest::newRow("500, no cache buffer") << 500 << 0;
2130     QTest::newRow("1000, no cache buffer") << 1000 << 0;
2131     QTest::newRow("500, cache buffer") << 500 << 320;
2132     QTest::newRow("1000, cache buffer") << 1000 << 320;
2133 }
2134
2135 void tst_QQuickListView::sectionsDragOutsideBounds()
2136 {
2137     QFETCH(int, distance);
2138     QFETCH(int, cacheBuffer);
2139
2140     QQuickView *canvas = getView();
2141
2142     QmlListModel model;
2143     for (int i = 0; i < 10; i++)
2144         model.addItem("Item" + QString::number(i), QString::number(i/5));
2145
2146     QQmlContext *ctxt = canvas->rootContext();
2147     ctxt->setContextProperty("testModel", &model);
2148
2149     canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2150     canvas->show();
2151     qApp->processEvents();
2152
2153     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2154     QTRY_VERIFY(listview != 0);
2155     listview->setCacheBuffer(cacheBuffer);
2156
2157     QQuickItem *contentItem = listview->contentItem();
2158     QTRY_VERIFY(contentItem != 0);
2159
2160     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2161
2162     // QTBUG-17769
2163     // Drag view up beyond bounds
2164     QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20));
2165     QTest::mouseMove(canvas, QPoint(20,0));
2166     QTest::mouseMove(canvas, QPoint(20,-50));
2167     QTest::mouseMove(canvas, QPoint(20,-distance));
2168     QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-distance));
2169     // view should settle back at 0
2170     QTRY_COMPARE(listview->contentY(), 0.0);
2171
2172     QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,0));
2173     QTest::mouseMove(canvas, QPoint(20,20));
2174     QTest::mouseMove(canvas, QPoint(20,70));
2175     QTest::mouseMove(canvas, QPoint(20,distance));
2176
2177     QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,distance));
2178     // view should settle back at 0
2179     QTRY_COMPARE(listview->contentY(), 0.0);
2180
2181     releaseView(canvas);
2182 }
2183
2184 void tst_QQuickListView::sectionsDelegate_headerVisibility()
2185 {
2186     QSKIP("QTBUG-24395");
2187
2188     QQuickView *canvas = createView();
2189
2190     QmlListModel model;
2191     for (int i = 0; i < 30; i++)
2192         model.addItem("Item" + QString::number(i), QString::number(i/5));
2193
2194     canvas->rootContext()->setContextProperty("testModel", &model);
2195     canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2196     canvas->show();
2197     qApp->processEvents();
2198
2199     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2200     QTRY_VERIFY(listview != 0);
2201
2202     QQuickItem *contentItem = listview->contentItem();
2203     QTRY_VERIFY(contentItem != 0);
2204     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2205
2206     // ensure section header is maintained in view
2207     listview->setCurrentIndex(20);
2208     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2209     QTRY_COMPARE(listview->contentY(), 200.0);
2210     QTRY_VERIFY(listview->isMoving() == false);
2211     listview->setCurrentIndex(0);
2212     QTRY_COMPARE(listview->contentY(), 0.0);
2213
2214     delete canvas;
2215 }
2216
2217 void tst_QQuickListView::sectionsPositioning()
2218 {
2219     QQuickView *canvas = createView();
2220
2221     QmlListModel model;
2222     for (int i = 0; i < 30; i++)
2223         model.addItem("Item" + QString::number(i), QString::number(i/5));
2224
2225     QQmlContext *ctxt = canvas->rootContext();
2226     ctxt->setContextProperty("testModel", &model);
2227
2228     canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2229     canvas->show();
2230     qApp->processEvents();
2231     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2232
2233     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2234     QTRY_VERIFY(listview != 0);
2235     QQuickItem *contentItem = listview->contentItem();
2236     QTRY_VERIFY(contentItem != 0);
2237     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2238
2239     for (int i = 0; i < 3; ++i) {
2240         QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2241         QVERIFY(item);
2242         QTRY_COMPARE(item->y(), qreal(i*20*6));
2243     }
2244
2245     QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header
2246     QVERIFY(topItem);
2247     QCOMPARE(topItem->y(), 0.);
2248
2249     QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2250     QVERIFY(bottomItem);
2251     QCOMPARE(bottomItem->y(), 300.);
2252
2253     // move down a little and check that section header is at top
2254     listview->setContentY(10);
2255     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2256     QCOMPARE(topItem->y(), 0.);
2257
2258     // push the top header up
2259     listview->setContentY(110);
2260     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2261     topItem = findVisibleChild(contentItem, "sect_0"); // section header
2262     QVERIFY(topItem);
2263     QCOMPARE(topItem->y(), 100.);
2264
2265     QQuickItem *item = findVisibleChild(contentItem, "sect_1");
2266     QVERIFY(item);
2267     QCOMPARE(item->y(), 120.);
2268
2269     bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2270     QVERIFY(bottomItem);
2271     QCOMPARE(bottomItem->y(), 410.);
2272
2273     // Move past section 0
2274     listview->setContentY(120);
2275     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2276     topItem = findVisibleChild(contentItem, "sect_0"); // section header
2277     QVERIFY(!topItem);
2278
2279     // Push section footer down
2280     listview->setContentY(70);
2281     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2282     bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2283     QVERIFY(bottomItem);
2284     QCOMPARE(bottomItem->y(), 380.);
2285
2286     // Change current section, and verify case insensitive comparison
2287     listview->setContentY(10);
2288     model.modifyItem(0, "One", "aaa");
2289     model.modifyItem(1, "Two", "AAA");
2290     model.modifyItem(2, "Three", "aAa");
2291     model.modifyItem(3, "Four", "aaA");
2292     model.modifyItem(4, "Five", "Aaa");
2293     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2294
2295     QTRY_COMPARE(listview->currentSection(), QString("aaa"));
2296
2297     for (int i = 0; i < 3; ++i) {
2298         QQuickItem *item = findItem<QQuickItem>(contentItem,
2299                 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2300         QVERIFY(item);
2301         QTRY_COMPARE(item->y(), qreal(i*20*6));
2302     }
2303
2304     QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header
2305     QCOMPARE(topItem->y(), 10.);
2306
2307     // remove section boundary
2308     listview->setContentY(120);
2309     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2310     model.removeItem(5);
2311     QTRY_COMPARE(listview->count(), model.count());
2312     for (int i = 1; i < 3; ++i) {
2313         QQuickItem *item = findVisibleChild(contentItem,
2314                 "sect_" + QString::number(i));
2315         QVERIFY(item);
2316         QTRY_COMPARE(item->y(), qreal(i*20*6));
2317     }
2318
2319     QVERIFY(topItem = findVisibleChild(contentItem, "sect_1"));
2320     QTRY_COMPARE(topItem->y(), 120.);
2321
2322     // Change the next section
2323     listview->setContentY(0);
2324     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2325     bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2326     QVERIFY(bottomItem);
2327     QTRY_COMPARE(bottomItem->y(), 300.);
2328
2329     model.modifyItem(14, "New", "new");
2330     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2331
2332     QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer
2333     QTRY_COMPARE(bottomItem->y(), 300.);
2334
2335     // Turn sticky footer off
2336     listview->setContentY(20);
2337     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2338     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2339     QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2340     QCOMPARE(item->y(), 340.);
2341
2342     // Turn sticky header off
2343     listview->setContentY(30);
2344     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2345     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2346     QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2347     QCOMPARE(item->y(), 0.);
2348
2349     // if an empty model is set the header/footer should be cleaned up
2350     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2351     QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2352     QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2353     QmlListModel model1;
2354     ctxt->setContextProperty("testModel", &model1);
2355     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2356     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2357
2358     // clear model - header/footer should be cleaned up
2359     ctxt->setContextProperty("testModel", &model);
2360     QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2361     QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2362     model.clear();
2363     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2364     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2365
2366     delete canvas;
2367 }
2368
2369 void tst_QQuickListView::sectionPropertyChange()
2370 {
2371     QQuickView *canvas = createView();
2372
2373     canvas->setSource(testFileUrl("sectionpropertychange.qml"));
2374     canvas->show();
2375     qApp->processEvents();
2376
2377     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2378     QTRY_VERIFY(listview != 0);
2379
2380     QQuickItem *contentItem = listview->contentItem();
2381     QTRY_VERIFY(contentItem != 0);
2382
2383     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2384
2385     // Confirm items positioned correctly
2386     for (int i = 0; i < 2; ++i) {
2387         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2388         QTRY_VERIFY(item);
2389         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2390     }
2391
2392     QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2393     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2394
2395     // Confirm items positioned correctly
2396     for (int i = 0; i < 2; ++i) {
2397         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2398         QTRY_VERIFY(item);
2399         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2400     }
2401
2402     QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2403     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2404
2405     // Confirm items positioned correctly
2406     for (int i = 0; i < 2; ++i) {
2407         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2408         QTRY_VERIFY(item);
2409         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2410     }
2411
2412     QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2413     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2414
2415     // Confirm items positioned correctly
2416     for (int i = 0; i < 2; ++i) {
2417         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2418         QTRY_VERIFY(item);
2419         QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2420     }
2421
2422     QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2423     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2424
2425     // Confirm items positioned correctly
2426     for (int i = 0; i < 2; ++i) {
2427         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2428         QTRY_VERIFY(item);
2429         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2430     }
2431
2432     delete canvas;
2433 }
2434
2435 void tst_QQuickListView::sectionDelegateChange()
2436 {
2437     QQuickView *canvas = createView();
2438
2439     canvas->setSource(testFileUrl("sectiondelegatechange.qml"));
2440     canvas->show();
2441     qApp->processEvents();
2442
2443     QQuickListView *listview = qobject_cast<QQuickListView *>(canvas->rootObject());
2444     QVERIFY(listview != 0);
2445
2446     QQuickItem *contentItem = listview->contentItem();
2447     QVERIFY(contentItem != 0);
2448
2449     QQUICK_VERIFY_POLISH(listview);
2450
2451     QVERIFY(findItems<QQuickItem>(contentItem, "section1").count() > 0);
2452     QCOMPARE(findItems<QQuickItem>(contentItem, "section2").count(), 0);
2453
2454     for (int i = 0; i < 3; ++i) {
2455         QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2456         QTRY_VERIFY(item);
2457         QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2458     }
2459
2460     QMetaObject::invokeMethod(canvas->rootObject(), "switchDelegates");
2461     QQUICK_VERIFY_POLISH(listview);
2462
2463     QCOMPARE(findItems<QQuickItem>(contentItem, "section1").count(), 0);
2464     QVERIFY(findItems<QQuickItem>(contentItem, "section2").count() > 0);
2465
2466     for (int i = 0; i < 3; ++i) {
2467         QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2468         QVERIFY(item);
2469         QTRY_COMPARE(item->y(), qreal(50. + i*75.));
2470     }
2471
2472     delete canvas;
2473 }
2474
2475 void tst_QQuickListView::currentIndex_delayedItemCreation()
2476 {
2477     QFETCH(bool, setCurrentToZero);
2478
2479     QQuickView *canvas = getView();
2480
2481     // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2482     // (since the currentItem will have changed and that shares the same index)
2483     canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2484
2485     canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2486     qApp->processEvents();
2487
2488     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2489     QTRY_VERIFY(listview != 0);
2490     QQuickItem *contentItem = listview->contentItem();
2491     QTRY_VERIFY(contentItem != 0);
2492
2493     QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2494     QCOMPARE(listview->currentIndex(), 0);
2495     QTRY_COMPARE(spy.count(), 1);
2496
2497     releaseView(canvas);
2498 }
2499
2500 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2501 {
2502     QTest::addColumn<bool>("setCurrentToZero");
2503
2504     QTest::newRow("set to 0") << true;
2505     QTest::newRow("don't set to 0") << false;
2506 }
2507
2508 void tst_QQuickListView::currentIndex()
2509 {
2510     QmlListModel model;
2511     for (int i = 0; i < 30; i++)
2512         model.addItem("Item" + QString::number(i), QString::number(i));
2513
2514     QQuickView *canvas = new QQuickView(0);
2515     canvas->setGeometry(0,0,240,320);
2516
2517     QQmlContext *ctxt = canvas->rootContext();
2518     ctxt->setContextProperty("testModel", &model);
2519     ctxt->setContextProperty("testWrap", QVariant(false));
2520
2521     QString filename(testFile("listview-initCurrent.qml"));
2522     canvas->setSource(QUrl::fromLocalFile(filename));
2523     canvas->show();
2524     qApp->processEvents();
2525
2526     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2527     QTRY_VERIFY(listview != 0);
2528     QQuickItem *contentItem = listview->contentItem();
2529     QTRY_VERIFY(contentItem != 0);
2530     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2531
2532     // current item should be 20th item at startup
2533     // and current item should be in view
2534     QCOMPARE(listview->currentIndex(), 20);
2535     QCOMPARE(listview->contentY(), 100.0);
2536     QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2537     QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2538
2539     listview->setCurrentIndex(0);
2540     QCOMPARE(listview->currentIndex(), 0);
2541     // confirm that the velocity is updated
2542     QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2543
2544     // footer should become visible if it is out of view, and then current index is set to count-1
2545     canvas->rootObject()->setProperty("showFooter", true);
2546     QTRY_VERIFY(listview->footerItem());
2547     listview->setCurrentIndex(model.count()-2);
2548     QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2549     listview->setCurrentIndex(model.count()-1);
2550     QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2551     canvas->rootObject()->setProperty("showFooter", false);
2552
2553     // header should become visible if it is out of view, and then current index is set to 0
2554     canvas->rootObject()->setProperty("showHeader", true);
2555     QTRY_VERIFY(listview->headerItem());
2556     listview->setCurrentIndex(1);
2557     QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2558     listview->setCurrentIndex(0);
2559     QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2560     canvas->rootObject()->setProperty("showHeader", false);
2561
2562     // turn off auto highlight
2563     listview->setHighlightFollowsCurrentItem(false);
2564     QVERIFY(listview->highlightFollowsCurrentItem() == false);
2565
2566     QVERIFY(listview->highlightItem());
2567     qreal hlPos = listview->highlightItem()->y();
2568
2569     listview->setCurrentIndex(4);
2570     QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2571
2572     // insert item before currentIndex
2573     listview->setCurrentIndex(28);
2574     model.insertItem(0, "Foo", "1111");
2575     QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2576
2577     // check removing highlight by setting currentIndex to -1;
2578     listview->setCurrentIndex(-1);
2579
2580     QCOMPARE(listview->currentIndex(), -1);
2581     QVERIFY(!listview->highlightItem());
2582     QVERIFY(!listview->currentItem());
2583
2584     // moving currentItem out of view should make it invisible
2585     listview->setCurrentIndex(0);
2586     QTRY_VERIFY(delegateVisible(listview->currentItem()));
2587     listview->setContentY(200);
2588     QTRY_VERIFY(!delegateVisible(listview->currentItem()));
2589
2590     delete canvas;
2591 }
2592
2593 void tst_QQuickListView::noCurrentIndex()
2594 {
2595     QmlListModel model;
2596     for (int i = 0; i < 30; i++)
2597         model.addItem("Item" + QString::number(i), QString::number(i));
2598
2599     QQuickView *canvas = new QQuickView(0);
2600     canvas->setGeometry(0,0,240,320);
2601
2602     QQmlContext *ctxt = canvas->rootContext();
2603     ctxt->setContextProperty("testModel", &model);
2604
2605     QString filename(testFile("listview-noCurrent.qml"));
2606     canvas->setSource(QUrl::fromLocalFile(filename));
2607     canvas->show();
2608     qApp->processEvents();
2609
2610     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2611     QTRY_VERIFY(listview != 0);
2612     QQuickItem *contentItem = listview->contentItem();
2613     QTRY_VERIFY(contentItem != 0);
2614     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2615
2616     // current index should be -1 at startup
2617     // and we should not have a currentItem or highlightItem
2618     QCOMPARE(listview->currentIndex(), -1);
2619     QCOMPARE(listview->contentY(), 0.0);
2620     QVERIFY(!listview->highlightItem());
2621     QVERIFY(!listview->currentItem());
2622
2623     listview->setCurrentIndex(2);
2624     QCOMPARE(listview->currentIndex(), 2);
2625     QVERIFY(listview->highlightItem());
2626     QVERIFY(listview->currentItem());
2627
2628     delete canvas;
2629 }
2630
2631 void tst_QQuickListView::keyNavigation()
2632 {
2633     QFETCH(QQuickListView::Orientation, orientation);
2634     QFETCH(Qt::LayoutDirection, layoutDirection);
2635     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
2636     QFETCH(Qt::Key, forwardsKey);
2637     QFETCH(Qt::Key, backwardsKey);
2638     QFETCH(QPointF, contentPosAtFirstItem);
2639     QFETCH(QPointF, contentPosAtLastItem);
2640
2641     QmlListModel model;
2642     for (int i = 0; i < 30; i++)
2643         model.addItem("Item" + QString::number(i), "");
2644
2645     QQuickView *canvas = getView();
2646     TestObject *testObject = new TestObject;
2647     canvas->rootContext()->setContextProperty("testModel", &model);
2648     canvas->rootContext()->setContextProperty("testObject", testObject);
2649     canvas->setSource(testFileUrl("listviewtest.qml"));
2650     canvas->show();
2651     qApp->processEvents();
2652
2653     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2654     QTRY_VERIFY(listview != 0);
2655
2656     listview->setOrientation(orientation);
2657     listview->setLayoutDirection(layoutDirection);
2658     listview->setVerticalLayoutDirection(verticalLayoutDirection);
2659     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2660
2661     canvas->requestActivateWindow();
2662     QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2663     QCOMPARE(listview->currentIndex(), 0);
2664
2665     QTest::keyClick(canvas, forwardsKey);
2666     QCOMPARE(listview->currentIndex(), 1);
2667
2668     QTest::keyClick(canvas, backwardsKey);
2669     QCOMPARE(listview->currentIndex(), 0);
2670
2671     // hold down a key to go forwards
2672     for (int i=0; i<model.count()-1; i++) {
2673         QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
2674         QTRY_COMPARE(listview->currentIndex(), i+1);
2675     }
2676     QTest::keyRelease(canvas, forwardsKey);
2677     QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2678     QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2679     QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2680
2681     // hold down a key to go backwards
2682     for (int i=model.count()-1; i > 0; i--) {
2683         QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2684         QTRY_COMPARE(listview->currentIndex(), i-1);
2685     }
2686     QTest::keyRelease(canvas, backwardsKey);
2687     QTRY_COMPARE(listview->currentIndex(), 0);
2688     QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2689     QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2690
2691     // no wrap
2692     QVERIFY(!listview->isWrapEnabled());
2693     listview->incrementCurrentIndex();
2694     QCOMPARE(listview->currentIndex(), 1);
2695     listview->decrementCurrentIndex();
2696     QCOMPARE(listview->currentIndex(), 0);
2697
2698     listview->decrementCurrentIndex();
2699     QCOMPARE(listview->currentIndex(), 0);
2700
2701     // with wrap
2702     listview->setWrapEnabled(true);
2703     QVERIFY(listview->isWrapEnabled());
2704
2705     listview->decrementCurrentIndex();
2706     QCOMPARE(listview->currentIndex(), model.count()-1);
2707     QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2708     QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2709
2710     listview->incrementCurrentIndex();
2711     QCOMPARE(listview->currentIndex(), 0);
2712     QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2713     QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2714
2715     releaseView(canvas);
2716     delete testObject;
2717 }
2718
2719 void tst_QQuickListView::keyNavigation_data()
2720 {
2721     QTest::addColumn<QQuickListView::Orientation>("orientation");
2722     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2723     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2724     QTest::addColumn<Qt::Key>("forwardsKey");
2725     QTest::addColumn<Qt::Key>("backwardsKey");
2726     QTest::addColumn<QPointF>("contentPosAtFirstItem");
2727     QTest::addColumn<QPointF>("contentPosAtLastItem");
2728
2729     QTest::newRow("Vertical, TopToBottom")
2730             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2731             << Qt::Key_Down << Qt::Key_Up
2732             << QPointF(0, 0)
2733             << QPointF(0, 30*20 - 320);
2734
2735     QTest::newRow("Vertical, BottomToTop")
2736             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2737             << Qt::Key_Up << Qt::Key_Down
2738             << QPointF(0, -320)
2739             << QPointF(0, -(30 * 20));
2740
2741     QTest::newRow("Horizontal, LeftToRight")
2742             << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2743             << Qt::Key_Right << Qt::Key_Left
2744             << QPointF(0, 0)
2745             << QPointF(30*240 - 240, 0);
2746
2747     QTest::newRow("Horizontal, RightToLeft")
2748             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2749             << Qt::Key_Left << Qt::Key_Right
2750             << QPointF(-240, 0)
2751             << QPointF(-(30 * 240), 0);
2752 }
2753
2754 void tst_QQuickListView::itemList()
2755 {
2756     QQuickView *canvas = createView();
2757     canvas->setSource(testFileUrl("itemlist.qml"));
2758     canvas->show();
2759     qApp->processEvents();
2760
2761     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2762     QTRY_VERIFY(listview != 0);
2763
2764     QQuickItem *contentItem = listview->contentItem();
2765     QTRY_VERIFY(contentItem != 0);
2766
2767     QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2768     QTRY_VERIFY(model != 0);
2769
2770     QTRY_VERIFY(model->count() == 3);
2771     QTRY_COMPARE(listview->currentIndex(), 0);
2772
2773     QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2774     QTRY_VERIFY(item);
2775     QTRY_COMPARE(item->x(), 0.0);
2776     QCOMPARE(item->height(), listview->height());
2777
2778     QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2779     QTRY_VERIFY(text);
2780     QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2781
2782     listview->setCurrentIndex(2);
2783
2784     item = findItem<QQuickItem>(contentItem, "item3");
2785     QTRY_VERIFY(item);
2786     QTRY_COMPARE(item->x(), 480.0);
2787
2788     text = findItem<QQuickText>(contentItem, "text3");
2789     QTRY_VERIFY(text);
2790     QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2791
2792     delete canvas;
2793 }
2794
2795 void tst_QQuickListView::itemListFlicker()
2796 {
2797     QQuickView *canvas = createView();
2798     canvas->setSource(testFileUrl("itemlist-flicker.qml"));
2799     canvas->show();
2800     qApp->processEvents();
2801
2802     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2803     QTRY_VERIFY(listview != 0);
2804
2805     QQuickItem *contentItem = listview->contentItem();
2806     QTRY_VERIFY(contentItem != 0);
2807
2808     QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2809     QTRY_VERIFY(model != 0);
2810
2811     QTRY_VERIFY(model->count() == 3);
2812     QTRY_COMPARE(listview->currentIndex(), 0);
2813
2814     QQuickItem *item;
2815
2816     QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2817     QVERIFY(delegateVisible(item));
2818     QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2819     QVERIFY(delegateVisible(item));
2820     QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2821     QVERIFY(delegateVisible(item));
2822
2823     listview->setCurrentIndex(1);
2824
2825     QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2826     QVERIFY(delegateVisible(item));
2827     QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2828     QVERIFY(delegateVisible(item));
2829     QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2830     QVERIFY(delegateVisible(item));
2831
2832     listview->setCurrentIndex(2);
2833
2834     QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2835     QVERIFY(delegateVisible(item));
2836     QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2837     QVERIFY(delegateVisible(item));
2838     QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2839     QVERIFY(delegateVisible(item));
2840
2841     delete canvas;
2842 }
2843
2844 void tst_QQuickListView::cacheBuffer()
2845 {
2846     QQuickView *canvas = createView();
2847
2848     QmlListModel model;
2849     for (int i = 0; i < 90; i++)
2850         model.addItem("Item" + QString::number(i), "");
2851
2852     QQmlContext *ctxt = canvas->rootContext();
2853     ctxt->setContextProperty("testModel", &model);
2854
2855     TestObject *testObject = new TestObject;
2856     ctxt->setContextProperty("testObject", testObject);
2857
2858     canvas->setSource(testFileUrl("listviewtest.qml"));
2859     canvas->show();
2860     qApp->processEvents();
2861
2862     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2863     QTRY_VERIFY(listview != 0);
2864
2865     QQuickItem *contentItem = listview->contentItem();
2866     QTRY_VERIFY(contentItem != 0);
2867     QTRY_VERIFY(listview->delegate() != 0);
2868     QTRY_VERIFY(listview->model() != 0);
2869     QTRY_VERIFY(listview->highlight() != 0);
2870
2871     // Confirm items positioned correctly
2872     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2873     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2874         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2875         if (!item) qWarning() << "Item" << i << "not found";
2876         QTRY_VERIFY(item);
2877         QTRY_VERIFY(item->y() == i*20);
2878     }
2879
2880     QQmlIncubationController controller;
2881     canvas->engine()->setIncubationController(&controller);
2882
2883     testObject->setCacheBuffer(200);
2884     QTRY_VERIFY(listview->cacheBuffer() == 200);
2885
2886     // items will be created one at a time
2887     for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2888         QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2889         QQuickItem *item = 0;
2890         while (!item) {
2891             bool b = false;
2892             controller.incubateWhile(&b);
2893             item = findItem<QQuickItem>(listview, "wrapper", i);
2894         }
2895     }
2896
2897     {
2898         bool b = true;
2899         controller.incubateWhile(&b);
2900     }
2901
2902     int newItemCount = 0;
2903     newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2904
2905     // Confirm items positioned correctly
2906     for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2907         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2908         if (!item) qWarning() << "Item" << i << "not found";
2909         QTRY_VERIFY(item);
2910         QTRY_VERIFY(item->y() == i*20);
2911     }
2912
2913     // move view and confirm items in view are visible immediately and outside are created async
2914     listview->setContentY(300);
2915
2916     for (int i = 15; i < 32; ++i) {
2917         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2918         if (!item) qWarning() << "Item" << i << "not found";
2919         QVERIFY(item);
2920         QVERIFY(item->y() == i*20);
2921     }
2922
2923     QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2924
2925     // ensure buffered items are created
2926     for (int i = 32; i < qMin(41,model.count()); ++i) {
2927         QQuickItem *item = 0;
2928         while (!item) {
2929             qGuiApp->processEvents(); // allow refill to happen
2930             bool b = false;
2931             controller.incubateWhile(&b);
2932             item = findItem<QQuickItem>(listview, "wrapper", i);
2933         }
2934     }
2935
2936     {
2937         bool b = true;
2938         controller.incubateWhile(&b);
2939     }
2940
2941     delete canvas;
2942     delete testObject;
2943 }
2944
2945 void tst_QQuickListView::positionViewAtIndex()
2946 {
2947     QQuickView *canvas = createView();
2948
2949     QmlListModel model;
2950     for (int i = 0; i < 40; i++)
2951         model.addItem("Item" + QString::number(i), "");
2952
2953     QQmlContext *ctxt = canvas->rootContext();
2954     ctxt->setContextProperty("testModel", &model);
2955
2956     TestObject *testObject = new TestObject;
2957     ctxt->setContextProperty("testObject", testObject);
2958     canvas->show();
2959     canvas->setSource(testFileUrl("listviewtest.qml"));
2960     qApp->processEvents();
2961
2962     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2963     QTRY_VERIFY(listview != 0);
2964     QQuickItem *contentItem = listview->contentItem();
2965     QTRY_VERIFY(contentItem != 0);
2966     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2967
2968     // Confirm items positioned correctly
2969     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2970     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2971         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2972         if (!item) qWarning() << "Item" << i << "not found";
2973         QTRY_VERIFY(item);
2974         QTRY_COMPARE(item->y(), i*20.);
2975     }
2976
2977     // Position on a currently visible item
2978     listview->positionViewAtIndex(3, QQuickListView::Beginning);
2979     QTRY_COMPARE(listview->contentY(), 60.);
2980
2981     // Confirm items positioned correctly
2982     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2983     for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2984         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2985         if (!item) qWarning() << "Item" << i << "not found";
2986         QTRY_VERIFY(item);
2987         QTRY_COMPARE(item->y(), i*20.);
2988     }
2989
2990     // Position on an item beyond the visible items
2991     listview->positionViewAtIndex(22, QQuickListView::Beginning);
2992     QTRY_COMPARE(listview->contentY(), 440.);
2993
2994     // Confirm items positioned correctly
2995     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2996     for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2997         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2998         if (!item) qWarning() << "Item" << i << "not found";
2999         QTRY_VERIFY(item);
3000         QTRY_COMPARE(item->y(), i*20.);
3001     }
3002
3003     // Position on an item that would leave empty space if positioned at the top
3004     listview->positionViewAtIndex(28, QQuickListView::Beginning);
3005     QTRY_COMPARE(listview->contentY(), 480.);
3006
3007     // Confirm items positioned correctly
3008     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3009     for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
3010         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3011         if (!item) qWarning() << "Item" << i << "not found";
3012         QTRY_VERIFY(item);
3013         QTRY_COMPARE(item->y(), i*20.);
3014     }
3015
3016     // Position at the beginning again
3017     listview->positionViewAtIndex(0, QQuickListView::Beginning);
3018     QTRY_COMPARE(listview->contentY(), 0.);
3019
3020     // Confirm items positioned correctly
3021     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3022     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
3023         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3024         if (!item) qWarning() << "Item" << i << "not found";
3025         QTRY_VERIFY(item);
3026         QTRY_COMPARE(item->y(), i*20.);
3027     }
3028
3029     // Position at End using last index
3030     listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
3031     QTRY_COMPARE(listview->contentY(), 480.);
3032
3033     // Confirm items positioned correctly
3034     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3035     for (int i = 24; i < model.count(); ++i) {
3036         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3037         if (!item) qWarning() << "Item" << i << "not found";
3038         QTRY_VERIFY(item);
3039         QTRY_COMPARE(item->y(), i*20.);
3040     }
3041
3042     // Position at End
3043     listview->positionViewAtIndex(20, QQuickListView::End);
3044     QTRY_COMPARE(listview->contentY(), 100.);
3045
3046     // Position in Center
3047     listview->positionViewAtIndex(15, QQuickListView::Center);
3048     QTRY_COMPARE(listview->contentY(), 150.);
3049
3050     // Ensure at least partially visible
3051     listview->positionViewAtIndex(15, QQuickListView::Visible);
3052     QTRY_COMPARE(listview->contentY(), 150.);
3053
3054     listview->setContentY(302);
3055     listview->positionViewAtIndex(15, QQuickListView::Visible);
3056     QTRY_COMPARE(listview->contentY(), 302.);
3057
3058     listview->setContentY(320);
3059     listview->positionViewAtIndex(15, QQuickListView::Visible);
3060     QTRY_COMPARE(listview->contentY(), 300.);
3061
3062     listview->setContentY(85);
3063     listview->positionViewAtIndex(20, QQuickListView::Visible);
3064     QTRY_COMPARE(listview->contentY(), 85.);
3065
3066     listview->setContentY(75);
3067     listview->positionViewAtIndex(20, QQuickListView::Visible);
3068     QTRY_COMPARE(listview->contentY(), 100.);
3069
3070     // Ensure completely visible
3071     listview->setContentY(120);
3072     listview->positionViewAtIndex(20, QQuickListView::Contain);
3073     QTRY_COMPARE(listview->contentY(), 120.);
3074
3075     listview->setContentY(302);
3076     listview->positionViewAtIndex(15, QQuickListView::Contain);
3077     QTRY_COMPARE(listview->contentY(), 300.);
3078
3079     listview->setContentY(85);
3080     listview->positionViewAtIndex(20, QQuickListView::Contain);
3081     QTRY_COMPARE(listview->contentY(), 100.);
3082
3083     // positionAtBeginnging
3084     listview->positionViewAtBeginning();
3085     QTRY_COMPARE(listview->contentY(), 0.);
3086
3087     listview->setContentY(80);
3088     canvas->rootObject()->setProperty("showHeader", true);
3089     listview->positionViewAtBeginning();
3090     QTRY_COMPARE(listview->contentY(), -30.);
3091
3092     // positionAtEnd
3093     listview->positionViewAtEnd();
3094     QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
3095
3096     listview->setContentY(80);
3097     canvas->rootObject()->setProperty("showFooter", true);
3098     listview->positionViewAtEnd();
3099     QTRY_COMPARE(listview->contentY(), 510.);
3100
3101     // set current item to outside visible view, position at beginning
3102     // and ensure highlight moves to current item
3103     listview->setCurrentIndex(1);
3104     listview->positionViewAtBeginning();
3105     QTRY_COMPARE(listview->contentY(), -30.);
3106     QVERIFY(listview->highlightItem());
3107     QCOMPARE(listview->highlightItem()->y(), 20.);
3108
3109     delete canvas;
3110     delete testObject;
3111 }
3112
3113 void tst_QQuickListView::resetModel()
3114 {
3115     QQuickView *canvas = createView();
3116
3117     QStringList strings;
3118     strings << "one" << "two" << "three";
3119     QStringListModel model(strings);
3120
3121     QQmlContext *ctxt = canvas->rootContext();
3122     ctxt->setContextProperty("testModel", &model);
3123
3124     canvas->setSource(testFileUrl("displaylist.qml"));
3125     canvas->show();
3126     qApp->processEvents();
3127
3128     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3129     QTRY_VERIFY(listview != 0);
3130     QQuickItem *contentItem = listview->contentItem();
3131     QTRY_VERIFY(contentItem != 0);
3132     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3133
3134     QTRY_COMPARE(listview->count(), model.rowCount());
3135
3136     for (int i = 0; i < model.rowCount(); ++i) {
3137         QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3138         QTRY_VERIFY(display != 0);
3139         QTRY_COMPARE(display->text(), strings.at(i));
3140     }
3141
3142     strings.clear();
3143     strings << "four" << "five" << "six" << "seven";
3144     model.setStringList(strings);
3145
3146     QTRY_COMPARE(listview->count(), model.rowCount());
3147
3148     for (int i = 0; i < model.rowCount(); ++i) {
3149         QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3150         QTRY_VERIFY(display != 0);
3151         QTRY_COMPARE(display->text(), strings.at(i));
3152     }
3153
3154     delete canvas;
3155 }
3156
3157 void tst_QQuickListView::propertyChanges()
3158 {
3159     QQuickView *canvas = createView();
3160     QTRY_VERIFY(canvas);
3161     canvas->setSource(testFileUrl("propertychangestest.qml"));
3162
3163     QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3164     QTRY_VERIFY(listView);
3165
3166     QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3167     QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3168     QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3169     QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3170     QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3171     QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3172     QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3173
3174     QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3175     QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3176     QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3177     QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3178     QTRY_COMPARE(listView->isWrapEnabled(), true);
3179     QTRY_COMPARE(listView->cacheBuffer(), 10);
3180     QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3181
3182     listView->setHighlightFollowsCurrentItem(false);
3183     listView->setPreferredHighlightBegin(1.0);
3184     listView->setPreferredHighlightEnd(1.0);
3185     listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3186     listView->setWrapEnabled(false);
3187     listView->setCacheBuffer(3);
3188     listView->setSnapMode(QQuickListView::SnapOneItem);
3189
3190     QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3191     QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3192     QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3193     QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3194     QTRY_COMPARE(listView->isWrapEnabled(), false);
3195     QTRY_COMPARE(listView->cacheBuffer(), 3);
3196     QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3197
3198     QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3199     QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3200     QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3201     QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3202     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3203     QTRY_COMPARE(cacheBufferSpy.count(),1);
3204     QTRY_COMPARE(snapModeSpy.count(),1);
3205
3206     listView->setHighlightFollowsCurrentItem(false);
3207     listView->setPreferredHighlightBegin(1.0);
3208     listView->setPreferredHighlightEnd(1.0);
3209     listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3210     listView->setWrapEnabled(false);
3211     listView->setCacheBuffer(3);
3212     listView->setSnapMode(QQuickListView::SnapOneItem);
3213
3214     QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3215     QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3216     QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3217     QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3218     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3219     QTRY_COMPARE(cacheBufferSpy.count(),1);
3220     QTRY_COMPARE(snapModeSpy.count(),1);
3221
3222     delete canvas;
3223 }
3224
3225 void tst_QQuickListView::componentChanges()
3226 {
3227     QQuickView *canvas = createView();
3228     QTRY_VERIFY(canvas);
3229     canvas->setSource(testFileUrl("propertychangestest.qml"));
3230
3231     QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3232     QTRY_VERIFY(listView);
3233
3234     QQmlComponent component(canvas->engine());
3235     component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3236
3237     QQmlComponent delegateComponent(canvas->engine());
3238     delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3239
3240     QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3241     QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3242     QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3243     QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3244
3245     listView->setHighlight(&component);
3246     listView->setHeader(&component);
3247     listView->setFooter(&component);
3248     listView->setDelegate(&delegateComponent);
3249
3250     QTRY_COMPARE(listView->highlight(), &component);
3251     QTRY_COMPARE(listView->header(), &component);
3252     QTRY_COMPARE(listView->footer(), &component);
3253     QTRY_COMPARE(listView->delegate(), &delegateComponent);
3254
3255     QTRY_COMPARE(highlightSpy.count(),1);
3256     QTRY_COMPARE(delegateSpy.count(),1);
3257     QTRY_COMPARE(headerSpy.count(),1);
3258     QTRY_COMPARE(footerSpy.count(),1);
3259
3260     listView->setHighlight(&component);
3261     listView->setHeader(&component);
3262     listView->setFooter(&component);
3263     listView->setDelegate(&delegateComponent);
3264
3265     QTRY_COMPARE(highlightSpy.count(),1);
3266     QTRY_COMPARE(delegateSpy.count(),1);
3267     QTRY_COMPARE(headerSpy.count(),1);
3268     QTRY_COMPARE(footerSpy.count(),1);
3269
3270     delete canvas;
3271 }
3272
3273 void tst_QQuickListView::modelChanges()
3274 {
3275     QQuickView *canvas = createView();
3276     QTRY_VERIFY(canvas);
3277     canvas->setSource(testFileUrl("propertychangestest.qml"));
3278
3279     QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3280     QTRY_VERIFY(listView);
3281
3282     QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
3283     QTRY_VERIFY(alternateModel);
3284     QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3285     QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3286
3287     listView->setModel(modelVariant);
3288     QTRY_COMPARE(listView->model(), modelVariant);
3289     QTRY_COMPARE(modelSpy.count(),1);
3290
3291     listView->setModel(modelVariant);
3292     QTRY_COMPARE(modelSpy.count(),1);
3293
3294     listView->setModel(QVariant());
3295     QTRY_COMPARE(modelSpy.count(),2);
3296
3297     delete canvas;
3298 }
3299
3300 void tst_QQuickListView::QTBUG_9791()
3301 {
3302     QQuickView *canvas = createView();
3303
3304     canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
3305     qApp->processEvents();
3306
3307     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3308     QTRY_VERIFY(listview != 0);
3309
3310     QQuickItem *contentItem = listview->contentItem();
3311     QTRY_VERIFY(contentItem != 0);
3312     QTRY_VERIFY(listview->delegate() != 0);
3313     QTRY_VERIFY(listview->model() != 0);
3314
3315     QMetaObject::invokeMethod(listview, "fillModel");
3316     qApp->processEvents();
3317
3318     // Confirm items positioned correctly
3319     int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3320     QCOMPARE(itemCount, 3);
3321
3322     for (int i = 0; i < itemCount; ++i) {
3323         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3324         if (!item) qWarning() << "Item" << i << "not found";
3325         QTRY_VERIFY(item);
3326         QTRY_COMPARE(item->x(), i*300.0);
3327     }
3328
3329     // check that view is positioned correctly
3330     QTRY_COMPARE(listview->contentX(), 590.0);
3331
3332     delete canvas;
3333 }
3334
3335 void tst_QQuickListView::manualHighlight()
3336 {
3337     QQuickView *canvas = new QQuickView(0);
3338     canvas->setGeometry(0,0,240,320);
3339
3340     QString filename(testFile("manual-highlight.qml"));
3341     canvas->setSource(QUrl::fromLocalFile(filename));
3342
3343     qApp->processEvents();
3344
3345     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3346     QTRY_VERIFY(listview != 0);
3347
3348     QQuickItem *contentItem = listview->contentItem();
3349     QTRY_VERIFY(contentItem != 0);
3350
3351     QTRY_COMPARE(listview->currentIndex(), 0);
3352     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3353     QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3354
3355     listview->setCurrentIndex(2);
3356
3357     QTRY_COMPARE(listview->currentIndex(), 2);
3358     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3359     QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3360
3361     // QTBUG-15972
3362     listview->positionViewAtIndex(3, QQuickListView::Contain);
3363
3364     QTRY_COMPARE(listview->currentIndex(), 2);
3365     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3366     QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3367
3368     delete canvas;
3369 }
3370
3371 void tst_QQuickListView::QTBUG_11105()
3372 {
3373     QQuickView *canvas = createView();
3374
3375     QmlListModel model;
3376     for (int i = 0; i < 30; i++)
3377         model.addItem("Item" + QString::number(i), "");
3378
3379     QQmlContext *ctxt = canvas->rootContext();
3380     ctxt->setContextProperty("testModel", &model);
3381
3382     TestObject *testObject = new TestObject;
3383     ctxt->setContextProperty("testObject", testObject);
3384
3385     canvas->setSource(testFileUrl("listviewtest.qml"));
3386     canvas->show();
3387     qApp->processEvents();
3388
3389     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3390     QTRY_VERIFY(listview != 0);
3391     QQuickItem *contentItem = listview->contentItem();
3392     QTRY_VERIFY(contentItem != 0);
3393     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3394
3395     // Confirm items positioned correctly
3396     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3397     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3398         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3399         if (!item) qWarning() << "Item" << i << "not found";
3400         QTRY_VERIFY(item);
3401         QTRY_VERIFY(item->y() == i*20);
3402     }
3403
3404     listview->positionViewAtIndex(20, QQuickListView::Beginning);
3405     QCOMPARE(listview->contentY(), 280.);
3406
3407     QmlListModel model2;
3408     for (int i = 0; i < 5; i++)
3409         model2.addItem("Item" + QString::number(i), "");
3410
3411     ctxt->setContextProperty("testModel", &model2);
3412
3413     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3414     QCOMPARE(itemCount, 5);
3415
3416     delete canvas;
3417     delete testObject;
3418 }
3419
3420 void tst_QQuickListView::initialZValues()
3421 {
3422     QQuickView *canvas = createView();
3423     canvas->setSource(testFileUrl("initialZValues.qml"));
3424     qApp->processEvents();
3425
3426     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3427     QTRY_VERIFY(listview != 0);
3428     QQuickItem *contentItem = listview->contentItem();
3429     QTRY_VERIFY(contentItem != 0);
3430
3431     QVERIFY(listview->headerItem());
3432     QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3433
3434     QVERIFY(listview->footerItem());
3435     QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3436
3437     delete canvas;
3438 }
3439
3440 void tst_QQuickListView::header()
3441 {
3442     QFETCH(QQuickListView::Orientation, orientation);
3443     QFETCH(Qt::LayoutDirection, layoutDirection);
3444     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3445     QFETCH(QPointF, initialHeaderPos);
3446     QFETCH(QPointF, changedHeaderPos);
3447     QFETCH(QPointF, initialContentPos);
3448     QFETCH(QPointF, changedContentPos);
3449     QFETCH(QPointF, firstDelegatePos);
3450     QFETCH(QPointF, resizeContentPos);
3451
3452     QmlListModel model;
3453     for (int i = 0; i < 30; i++)
3454         model.addItem("Item" + QString::number(i), "");
3455
3456     QQuickView *canvas = getView();
3457     canvas->rootContext()->setContextProperty("testModel", &model);
3458     canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3459     canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3460     canvas->setSource(testFileUrl("header.qml"));
3461     canvas->show();
3462     qApp->processEvents();
3463
3464     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3465     QTRY_VERIFY(listview != 0);
3466     listview->setOrientation(orientation);
3467     listview->setLayoutDirection(layoutDirection);
3468     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3469     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3470
3471     QQuickItem *contentItem = listview->contentItem();
3472     QTRY_VERIFY(contentItem != 0);
3473
3474     QQuickText *header = 0;
3475     QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3476     QVERIFY(header == listview->headerItem());
3477
3478     QCOMPARE(header->width(), 100.);
3479     QCOMPARE(header->height(), 30.);
3480     QCOMPARE(header->pos(), initialHeaderPos);
3481     QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3482
3483     if (orientation == QQuickListView::Vertical)
3484         QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3485     else
3486         QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3487
3488     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3489     QVERIFY(item);
3490     QCOMPARE(item->pos(), firstDelegatePos);
3491
3492     model.clear();
3493     QTRY_COMPARE(listview->count(), model.count());
3494     QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3495     if (orientation == QQuickListView::Vertical)
3496         QCOMPARE(listview->contentHeight(), header->height());
3497     else
3498         QCOMPARE(listview->contentWidth(), header->width());
3499
3500     for (int i = 0; i < 30; i++)
3501         model.addItem("Item" + QString::number(i), "");
3502
3503     QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3504     QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3505
3506     QCOMPARE(headerItemSpy.count(), 1);
3507
3508     header = findItem<QQuickText>(contentItem, "header");
3509     QVERIFY(!header);
3510     header = findItem<QQuickText>(contentItem, "header2");
3511     QVERIFY(header);
3512
3513     QVERIFY(header == listview->headerItem());
3514
3515     QCOMPARE(header->pos(), changedHeaderPos);
3516     QCOMPARE(header->width(), 50.);
3517     QCOMPARE(header->height(), 20.);
3518     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3519
3520     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3521     QVERIFY(item);
3522     QCOMPARE(item->pos(), firstDelegatePos);
3523
3524     listview->positionViewAtBeginning();
3525     header->setHeight(10);
3526     header->setWidth(40);
3527     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3528
3529     releaseView(canvas);
3530
3531
3532     // QTBUG-21207 header should become visible if view resizes from initial empty size
3533
3534     canvas = getView();
3535     canvas->rootContext()->setContextProperty("testModel", &model);
3536     canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3537     canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3538     canvas->setSource(testFileUrl("header.qml"));
3539     canvas->show();
3540     qApp->processEvents();
3541
3542     listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3543     QTRY_VERIFY(listview != 0);
3544     listview->setOrientation(orientation);
3545     listview->setLayoutDirection(layoutDirection);
3546     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3547     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3548
3549     listview->setWidth(240);
3550     listview->setHeight(320);
3551     QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3552     QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3553
3554     releaseView(canvas);
3555 }
3556
3557 void tst_QQuickListView::header_data()
3558 {
3559     QTest::addColumn<QQuickListView::Orientation>("orientation");
3560     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3561     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3562     QTest::addColumn<QPointF>("initialHeaderPos");
3563     QTest::addColumn<QPointF>("changedHeaderPos");
3564     QTest::addColumn<QPointF>("initialContentPos");
3565     QTest::addColumn<QPointF>("changedContentPos");
3566     QTest::addColumn<QPointF>("firstDelegatePos");
3567     QTest::addColumn<QPointF>("resizeContentPos");
3568
3569     // header1 = 100 x 30
3570     // header2 = 50 x 20
3571     // delegates = 240 x 30
3572     // view width = 240
3573
3574     // header above items, top left
3575     QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3576         << QPointF(0, -30)
3577         << QPointF(0, -20)
3578         << QPointF(0, -30)
3579         << QPointF(0, -20)
3580         << QPointF(0, 0)
3581         << QPointF(0, -10);
3582
3583     // header above items, top right
3584     QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3585         << QPointF(0, -30)
3586         << QPointF(0, -20)
3587         << QPointF(0, -30)
3588         << QPointF(0, -20)
3589         << QPointF(0, 0)
3590         << QPointF(0, -10);
3591
3592     // header to left of items
3593     QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3594         << QPointF(-100, 0)
3595         << QPointF(-50, 0)
3596         << QPointF(-100, 0)
3597         << QPointF(-50, 0)
3598         << QPointF(0, 0)
3599         << QPointF(-40, 0);
3600
3601     // header to right of items
3602     QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3603         << QPointF(0, 0)
3604         << QPointF(0, 0)
3605         << QPointF(-240 + 100, 0)
3606         << QPointF(-240 + 50, 0)
3607         << QPointF(-240, 0)
3608         << QPointF(-240 + 40, 0);
3609
3610     // header below items
3611     QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3612         << QPointF(0, 0)
3613         << QPointF(0, 0)
3614         << QPointF(0, -320 + 30)
3615         << QPointF(0, -320 + 20)
3616         << QPointF(0, -30)
3617         << QPointF(0, -320 + 10);
3618 }
3619
3620 void tst_QQuickListView::header_delayItemCreation()
3621 {
3622     QQuickView *canvas = createView();
3623
3624     QmlListModel model;
3625
3626     canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3627     canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3628     qApp->processEvents();
3629
3630     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3631     QTRY_VERIFY(listview != 0);
3632
3633     QQuickItem *contentItem = listview->contentItem();
3634     QTRY_VERIFY(contentItem != 0);
3635
3636     QQuickText *header = findItem<QQuickText>(contentItem, "header");
3637     QVERIFY(header);
3638     QCOMPARE(header->y(), -header->height());
3639
3640     QCOMPARE(listview->contentY(), -header->height());
3641
3642     model.clear();
3643     QTRY_COMPARE(header->y(), -header->height());
3644
3645     delete canvas;
3646 }
3647
3648 void tst_QQuickListView::footer()
3649 {
3650     QFETCH(QQuickListView::Orientation, orientation);
3651     QFETCH(Qt::LayoutDirection, layoutDirection);
3652     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3653     QFETCH(QPointF, initialFooterPos);
3654     QFETCH(QPointF, firstDelegatePos);
3655     QFETCH(QPointF, initialContentPos);
3656     QFETCH(QPointF, changedFooterPos);
3657     QFETCH(QPointF, changedContentPos);
3658     QFETCH(QPointF, resizeContentPos);
3659
3660     QQuickView *canvas = getView();
3661
3662     QmlListModel model;
3663     for (int i = 0; i < 3; i++)
3664         model.addItem("Item" + QString::number(i), "");
3665
3666     QQmlContext *ctxt = canvas->rootContext();
3667     ctxt->setContextProperty("testModel", &model);
3668
3669     canvas->setSource(testFileUrl("footer.qml"));
3670     canvas->show();
3671     qApp->processEvents();
3672
3673     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3674     QTRY_VERIFY(listview != 0);
3675     listview->setOrientation(orientation);
3676     listview->setLayoutDirection(layoutDirection);
3677     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3678     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3679
3680     QQuickItem *contentItem = listview->contentItem();
3681     QTRY_VERIFY(contentItem != 0);
3682
3683     QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3684     QVERIFY(footer);
3685
3686     QVERIFY(footer == listview->footerItem());
3687
3688     QCOMPARE(footer->pos(), initialFooterPos);
3689     QCOMPARE(footer->width(), 100.);
3690     QCOMPARE(footer->height(), 30.);
3691     QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3692
3693     if (orientation == QQuickListView::Vertical)
3694         QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3695     else
3696         QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3697
3698     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3699     QVERIFY(item);
3700     QCOMPARE(item->pos(), firstDelegatePos);
3701
3702     // remove one item
3703     model.removeItem(1);
3704
3705     if (orientation == QQuickListView::Vertical) {
3706         QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3707                 initialFooterPos.y() - 20 : initialFooterPos.y() + 20);  // delegate width = 40
3708     } else {
3709         QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3710                 initialFooterPos.x() - 40 : initialFooterPos.x() + 40);  // delegate width = 40
3711     }
3712
3713     // remove all items
3714     model.clear();
3715     if (orientation == QQuickListView::Vertical)
3716         QTRY_COMPARE(listview->contentHeight(), footer->height());
3717     else
3718         QTRY_COMPARE(listview->contentWidth(), footer->width());
3719
3720     QPointF posWhenNoItems(0, 0);
3721     if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3722         posWhenNoItems.setX(-100);
3723     else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3724         posWhenNoItems.setY(-30);
3725     QTRY_COMPARE(footer->pos(), posWhenNoItems);
3726
3727     // if header is present, it's at a negative pos, so the footer should not move
3728     canvas->rootObject()->setProperty("showHeader", true);
3729     QTRY_COMPARE(footer->pos(), posWhenNoItems);
3730     canvas->rootObject()->setProperty("showHeader", false);
3731
3732     // add 30 items
3733     for (int i = 0; i < 30; i++)
3734         model.addItem("Item" + QString::number(i), "");
3735
3736     QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3737     QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3738
3739     QCOMPARE(footerItemSpy.count(), 1);
3740
3741     footer = findItem<QQuickText>(contentItem, "footer");
3742     QVERIFY(!footer);
3743     footer = findItem<QQuickText>(contentItem, "footer2");
3744     QVERIFY(footer);
3745
3746     QVERIFY(footer == listview->footerItem());
3747
3748     QCOMPARE(footer->pos(), changedFooterPos);
3749     QCOMPARE(footer->width(), 50.);
3750     QCOMPARE(footer->height(), 20.);
3751     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3752
3753     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3754     QVERIFY(item);
3755     QCOMPARE(item->pos(), firstDelegatePos);
3756
3757     listview->positionViewAtEnd();
3758     footer->setHeight(10);
3759     footer->setWidth(40);
3760     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3761
3762     releaseView(canvas);
3763 }
3764
3765 void tst_QQuickListView::footer_data()
3766 {
3767     QTest::addColumn<QQuickListView::Orientation>("orientation");
3768     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3769     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3770     QTest::addColumn<QPointF>("initialFooterPos");
3771     QTest::addColumn<QPointF>("changedFooterPos");
3772     QTest::addColumn<QPointF>("initialContentPos");
3773     QTest::addColumn<QPointF>("changedContentPos");
3774     QTest::addColumn<QPointF>("firstDelegatePos");
3775     QTest::addColumn<QPointF>("resizeContentPos");
3776
3777     // footer1 = 100 x 30
3778     // footer2 = 50 x 20
3779     // delegates = 40 x 20
3780     // view width = 240
3781     // view height = 320
3782
3783     // footer below items, bottom left
3784     QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3785         << QPointF(0, 3 * 20)
3786         << QPointF(0, 30 * 20)  // added 30 items
3787         << QPointF(0, 0)
3788         << QPointF(0, 0)
3789         << QPointF(0, 0)
3790         << QPointF(0, 30 * 20 - 320 + 10);
3791
3792     // footer below items, bottom right
3793     QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3794         << QPointF(0, 3 * 20)
3795         << QPointF(0, 30 * 20)
3796         << QPointF(0, 0)
3797         << QPointF(0, 0)
3798         << QPointF(0, 0)
3799         << QPointF(0, 30 * 20 - 320 + 10);
3800
3801     // footer to right of items
3802     QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3803         << QPointF(40 * 3, 0)
3804         << QPointF(40 * 30, 0)
3805         << QPointF(0, 0)
3806         << QPointF(0, 0)
3807         << QPointF(0, 0)
3808         << QPointF(40 * 30 - 240 + 40, 0);
3809
3810     // footer to left of items
3811     QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3812         << QPointF(-(40 * 3) - 100, 0)
3813         << QPointF(-(40 * 30) - 50, 0)     // 50 = new footer width
3814         << QPointF(-240, 0)
3815         << QPointF(-240, 0)
3816         << QPointF(-40, 0)
3817         << QPointF(-(40 * 30) - 40, 0);
3818
3819     // footer above items
3820     QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3821         << QPointF(0, -(3 * 20) - 30)
3822         << QPointF(0, -(30 * 20) - 20)
3823         << QPointF(0, -320)
3824         << QPointF(0, -320)
3825         << QPointF(0, -20)
3826         << QPointF(0, -(30 * 20) - 10);
3827 }
3828
3829 class LVAccessor : public QQuickListView
3830 {
3831 public:
3832     qreal minY() const { return minYExtent(); }
3833     qreal maxY() const { return maxYExtent(); }
3834     qreal minX() const { return minXExtent(); }
3835     qreal maxX() const { return maxXExtent(); }
3836 };
3837
3838
3839 void tst_QQuickListView::extents()
3840 {
3841     QFETCH(QQuickListView::Orientation, orientation);
3842     QFETCH(Qt::LayoutDirection, layoutDirection);
3843     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3844     QFETCH(QPointF, headerPos);
3845     QFETCH(QPointF, footerPos);
3846     QFETCH(QPointF, minPos);
3847     QFETCH(QPointF, maxPos);
3848     QFETCH(QPointF, origin_empty);
3849     QFETCH(QPointF, origin_nonEmpty);
3850
3851     QQuickView *canvas = getView();
3852
3853     QmlListModel model;
3854     QQmlContext *ctxt = canvas->rootContext();
3855     ctxt->setContextProperty("testModel", &model);
3856     canvas->setSource(testFileUrl("headerfooter.qml"));
3857     canvas->show();
3858     qApp->processEvents();
3859
3860     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3861     QTRY_VERIFY(listview != 0);
3862     listview->setOrientation(orientation);
3863     listview->setLayoutDirection(layoutDirection);
3864     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3865     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3866
3867     QQuickItem *contentItem = listview->contentItem();
3868     QTRY_VERIFY(contentItem != 0);
3869
3870     QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3871     QVERIFY(header);
3872     QCOMPARE(header->pos(), headerPos);
3873
3874     QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3875     QVERIFY(footer);
3876     QCOMPARE(footer->pos(), footerPos);
3877
3878     QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3879     QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3880     QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3881     QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3882
3883     QCOMPARE(listview->originX(), origin_empty.x());
3884     QCOMPARE(listview->originY(), origin_empty.y());
3885     for (int i=0; i<30; i++)
3886         model.addItem("Item" + QString::number(i), "");
3887     QTRY_COMPARE(listview->count(), model.count());
3888     QCOMPARE(listview->originX(), origin_nonEmpty.x());
3889     QCOMPARE(listview->originY(), origin_nonEmpty.y());
3890
3891     releaseView(canvas);
3892 }
3893
3894 void tst_QQuickListView::extents_data()
3895 {
3896     QTest::addColumn<QQuickListView::Orientation>("orientation");
3897     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3898     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3899     QTest::addColumn<QPointF>("headerPos");
3900     QTest::addColumn<QPointF>("footerPos");
3901     QTest::addColumn<QPointF>("minPos");
3902     QTest::addColumn<QPointF>("maxPos");
3903     QTest::addColumn<QPointF>("origin_empty");
3904     QTest::addColumn<QPointF>("origin_nonEmpty");
3905
3906     // header is 240x20 (or 20x320 in Horizontal orientation)
3907     // footer is 240x30 (or 30x320 in Horizontal orientation)
3908
3909     QTest::newRow("Vertical, TopToBottom")
3910             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3911             << QPointF(0, -20) << QPointF(0, 0)
3912             << QPointF(0, 20) << QPointF(240, 20)
3913             << QPointF(0, -20) << QPointF(0, -20);
3914
3915     QTest::newRow("Vertical, BottomToTop")
3916             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3917             << QPointF(0, 0) << QPointF(0, -30)
3918             << QPointF(0, 320 - 20) << QPointF(240, 320 - 20)  // content flow is reversed
3919             << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3920
3921     QTest::newRow("Horizontal, LeftToRight")
3922             << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3923             << QPointF(-20, 0) << QPointF(0, 0)
3924             << QPointF(20, 0) << QPointF(20, 320)
3925             << QPointF(-20, 0) << QPointF(-20, 0);
3926
3927     QTest::newRow("Horizontal, RightToLeft")
3928             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3929             << QPointF(0, 0) << QPointF(-30, 0)
3930             << QPointF(240 - 20, 0) << QPointF(240 - 20, 320)  // content flow is reversed
3931             << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3932 }
3933
3934 void tst_QQuickListView::resetModel_headerFooter()
3935 {
3936     // Resetting a model shouldn't crash in views with header/footer
3937
3938     QQuickView *canvas = createView();
3939
3940     QaimModel model;
3941     for (int i = 0; i < 4; i++)
3942         model.addItem("Item" + QString::number(i), "");
3943     QQmlContext *ctxt = canvas->rootContext();
3944     ctxt->setContextProperty("testModel", &model);
3945
3946     canvas->setSource(testFileUrl("headerfooter.qml"));
3947     qApp->processEvents();
3948
3949     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3950     QTRY_VERIFY(listview != 0);
3951
3952     QQuickItem *contentItem = listview->contentItem();
3953     QTRY_VERIFY(contentItem != 0);
3954
3955     QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3956     QVERIFY(header);
3957     QCOMPARE(header->y(), -header->height());
3958
3959     QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3960     QVERIFY(footer);
3961     QCOMPARE(footer->y(), 30.*4);
3962
3963     model.reset();
3964
3965     header = findItem<QQuickItem>(contentItem, "header");
3966     QVERIFY(header);
3967     QCOMPARE(header->y(), -header->height());
3968
3969     footer = findItem<QQuickItem>(contentItem, "footer");
3970     QVERIFY(footer);
3971     QCOMPARE(footer->y(), 30.*4);
3972
3973     delete canvas;
3974 }
3975
3976 void tst_QQuickListView::resizeView()
3977 {
3978     QQuickView *canvas = createView();
3979
3980     QmlListModel model;
3981     for (int i = 0; i < 40; i++)
3982         model.addItem("Item" + QString::number(i), "");
3983
3984     QQmlContext *ctxt = canvas->rootContext();
3985     ctxt->setContextProperty("testModel", &model);
3986
3987     TestObject *testObject = new TestObject;
3988     ctxt->setContextProperty("testObject", testObject);
3989
3990     canvas->setSource(testFileUrl("listviewtest.qml"));
3991     canvas->show();
3992     qApp->processEvents();
3993
3994     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3995     QTRY_VERIFY(listview != 0);
3996     QQuickItem *contentItem = listview->contentItem();
3997     QTRY_VERIFY(contentItem != 0);
3998     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3999
4000     // Confirm items positioned correctly
4001     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4002     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4003         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4004         if (!item) qWarning() << "Item" << i << "not found";
4005         QTRY_VERIFY(item);
4006         QTRY_COMPARE(item->y(), i*20.);
4007     }
4008
4009     QVariant heightRatio;
4010     QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4011     QCOMPARE(heightRatio.toReal(), 0.4);
4012
4013     listview->setHeight(200);
4014     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4015
4016     QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4017     QCOMPARE(heightRatio.toReal(), 0.25);
4018
4019     // Ensure we handle -ve sizes
4020     listview->setHeight(-100);
4021     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
4022
4023     listview->setCacheBuffer(200);
4024     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
4025
4026     // ensure items in cache become visible
4027     listview->setHeight(200);
4028     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
4029
4030     itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4031     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4032         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4033         if (!item) qWarning() << "Item" << i << "not found";
4034         QTRY_VERIFY(item);
4035         QTRY_COMPARE(item->y(), i*20.);
4036         QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
4037     }
4038
4039     // ensure items outside view become invisible
4040     listview->setHeight(100);
4041     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
4042
4043     itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4044     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4045         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4046         if (!item) qWarning() << "Item" << i << "not found";
4047         QTRY_VERIFY(item);
4048         QTRY_COMPARE(item->y(), i*20.);
4049         QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
4050     }
4051
4052     delete canvas;
4053     delete testObject;
4054 }
4055
4056 void tst_QQuickListView::resizeViewAndRepaint()
4057 {
4058     QQuickView *canvas = createView();
4059
4060     QmlListModel model;
4061     for (int i = 0; i < 40; i++)
4062         model.addItem("Item" + QString::number(i), "");
4063
4064     QQmlContext *ctxt = canvas->rootContext();
4065     ctxt->setContextProperty("testModel", &model);
4066     ctxt->setContextProperty("initialHeight", 100);
4067
4068     canvas->setSource(testFileUrl("resizeview.qml"));
4069     canvas->show();
4070     qApp->processEvents();
4071
4072     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4073     QTRY_VERIFY(listview != 0);
4074     QQuickItem *contentItem = listview->contentItem();
4075     QTRY_VERIFY(contentItem != 0);
4076     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4077
4078     // item at index 10 should not be currently visible
4079     QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4080
4081     listview->setHeight(320);
4082
4083     QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
4084
4085     listview->setHeight(100);
4086     QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4087
4088     delete canvas;
4089 }
4090
4091 void tst_QQuickListView::sizeLessThan1()
4092 {
4093     QQuickView *canvas = createView();
4094
4095     QmlListModel model;
4096     for (int i = 0; i < 30; i++)
4097         model.addItem("Item" + QString::number(i), "");
4098
4099     QQmlContext *ctxt = canvas->rootContext();
4100     ctxt->setContextProperty("testModel", &model);
4101
4102     TestObject *testObject = new TestObject;
4103     ctxt->setContextProperty("testObject", testObject);
4104
4105     canvas->setSource(testFileUrl("sizelessthan1.qml"));
4106     canvas->show();
4107     qApp->processEvents();
4108
4109     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4110     QTRY_VERIFY(listview != 0);
4111     QQuickItem *contentItem = listview->contentItem();
4112     QTRY_VERIFY(contentItem != 0);
4113     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4114
4115     // Confirm items positioned correctly
4116     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4117     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4118         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4119         if (!item) qWarning() << "Item" << i << "not found";
4120         QTRY_VERIFY(item);
4121         QTRY_COMPARE(item->y(), i*0.5);
4122     }
4123
4124     delete canvas;
4125     delete testObject;
4126 }
4127
4128 void tst_QQuickListView::QTBUG_14821()
4129 {
4130     QQuickView *canvas = createView();
4131
4132     canvas->setSource(testFileUrl("qtbug14821.qml"));
4133     qApp->processEvents();
4134
4135     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4136     QVERIFY(listview != 0);
4137
4138     QQuickItem *contentItem = listview->contentItem();
4139     QVERIFY(contentItem != 0);
4140
4141     listview->decrementCurrentIndex();
4142     QCOMPARE(listview->currentIndex(), 99);
4143
4144     listview->incrementCurrentIndex();
4145     QCOMPARE(listview->currentIndex(), 0);
4146
4147     delete canvas;
4148 }
4149
4150 void tst_QQuickListView::resizeDelegate()
4151 {
4152     QQuickView *canvas = createView();
4153
4154     QStringList strings;
4155     for (int i = 0; i < 30; ++i)
4156         strings << QString::number(i);
4157     QStringListModel model(strings);
4158
4159     QQmlContext *ctxt = canvas->rootContext();
4160     ctxt->setContextProperty("testModel", &model);
4161
4162     canvas->setSource(testFileUrl("displaylist.qml"));
4163     canvas->show();
4164     qApp->processEvents();
4165
4166     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4167     QVERIFY(listview != 0);
4168     QQuickItem *contentItem = listview->contentItem();
4169     QVERIFY(contentItem != 0);
4170     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4171
4172     QCOMPARE(listview->count(), model.rowCount());
4173
4174     listview->setCurrentIndex(25);
4175     listview->setContentY(0);
4176     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4177
4178     for (int i = 0; i < 16; ++i) {
4179         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4180         QVERIFY(item != 0);
4181         QCOMPARE(item->y(), i*20.0);
4182     }
4183
4184     QCOMPARE(listview->currentItem()->y(), 500.0);
4185     QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4186
4187     canvas->rootObject()->setProperty("delegateHeight", 30);
4188     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4189
4190     for (int i = 0; i < 11; ++i) {
4191         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4192         QVERIFY(item != 0);
4193         QTRY_COMPARE(item->y(), i*30.0);
4194     }
4195
4196     QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4197     QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4198
4199     listview->setCurrentIndex(1);
4200     listview->positionViewAtIndex(25, QQuickListView::Beginning);
4201     listview->positionViewAtIndex(5, QQuickListView::Beginning);
4202     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4203
4204     for (int i = 5; i < 16; ++i) {
4205         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4206         QVERIFY(item != 0);
4207         QCOMPARE(item->y(), i*30.0);
4208     }
4209
4210     QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4211     QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4212
4213     canvas->rootObject()->setProperty("delegateHeight", 20);
4214     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4215
4216     for (int i = 5; i < 11; ++i) {
4217         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4218         QVERIFY(item != 0);
4219         QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4220     }
4221
4222     QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4223     QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4224
4225     delete canvas;
4226 }
4227
4228 void tst_QQuickListView::resizeFirstDelegate()
4229 {
4230     // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4231     // and other delegates have height > 0
4232
4233     QQuickView *canvas = createView();
4234
4235     // bug only occurs when all items in the model are visible
4236     QmlListModel model;
4237     for (int i = 0; i < 10; i++)
4238         model.addItem("Item" + QString::number(i), "");
4239
4240     QQmlContext *ctxt = canvas->rootContext();
4241     ctxt->setContextProperty("testModel", &model);
4242
4243     TestObject *testObject = new TestObject;
4244     ctxt->setContextProperty("testObject", testObject);
4245
4246     canvas->setSource(testFileUrl("listviewtest.qml"));
4247     canvas->show();
4248     qApp->processEvents();
4249
4250     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4251     QVERIFY(listview != 0);
4252     QQuickItem *contentItem = listview->contentItem();
4253     QVERIFY(contentItem != 0);
4254     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4255
4256     QQuickItem *item = 0;
4257     for (int i = 0; i < model.count(); ++i) {
4258         item = findItem<QQuickItem>(contentItem, "wrapper", i);
4259         QVERIFY(item != 0);
4260         QCOMPARE(item->y(), i*20.0);
4261     }
4262
4263     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4264     item->setHeight(0);
4265
4266     // check the content y has not jumped up and down
4267     QCOMPARE(listview->contentY(), 0.0);
4268     QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4269     QTest::qWait(100);
4270     QCOMPARE(spy.count(), 0);
4271
4272     for (int i = 1; i < model.count(); ++i) {
4273         item = findItem<QQuickItem>(contentItem, "wrapper", i);
4274         QVERIFY(item != 0);
4275         QTRY_COMPARE(item->y(), (i-1)*20.0);
4276     }
4277
4278
4279     // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4280     // list if they follow a zero-sized delegate
4281
4282     for (int i = 0; i < 10; i++)
4283         model.addItem("Item" + QString::number(i), "");
4284     QTRY_COMPARE(listview->count(), model.count());
4285
4286     item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4287     QVERIFY(item);
4288     item->setHeight(0);
4289
4290     listview->setCurrentIndex(19);
4291     qApp->processEvents();
4292     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4293
4294     // items 0-2 should have been deleted
4295     for (int i=0; i<3; i++) {
4296         QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4297     }
4298
4299     delete testObject;
4300     delete canvas;
4301 }
4302
4303 void tst_QQuickListView::repositionResizedDelegate()
4304 {
4305     QFETCH(QQuickListView::Orientation, orientation);
4306     QFETCH(Qt::LayoutDirection, layoutDirection);
4307     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4308     QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4309     QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4310     QFETCH(QRectF, origPositionerRect);
4311     QFETCH(QRectF, resizedPositionerRect);
4312
4313     QQuickView *canvas = getView();
4314     QQmlContext *ctxt = canvas->rootContext();
4315     ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4316     ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4317     ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4318     canvas->setSource(testFileUrl("repositionResizedDelegate.qml"));
4319     canvas->show();
4320     qApp->processEvents();
4321
4322     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4323     QTRY_VERIFY(listview != 0);
4324     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4325
4326     QQuickItem *positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4327     QVERIFY(positioner);
4328     QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4329     QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4330     QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4331     int prevSpyCount = 0;
4332
4333     // When an item is resized while it is partially visible, it should resize in the
4334     // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4335     // the item should also be re-positioned so its end position stays the same.
4336
4337     listview->setContentX(contentPos_itemFirstHalfVisible.x());
4338     listview->setContentY(contentPos_itemFirstHalfVisible.y());
4339     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4340     prevSpyCount = spy.count();
4341     QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4342     QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4343     QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4344     QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4345     QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4346     QCOMPARE(spy.count(), prevSpyCount);
4347
4348     QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "decrementRepeater"));
4349     QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4350     QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4351     QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4352     QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4353
4354     listview->setContentX(contentPos_itemSecondHalfVisible.x());
4355     listview->setContentY(contentPos_itemSecondHalfVisible.y());
4356     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4357     prevSpyCount = spy.count();
4358
4359     QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4360     positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4361     QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4362     QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4363     QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4364     QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4365     qApp->processEvents();
4366     QCOMPARE(spy.count(), prevSpyCount);
4367
4368     releaseView(canvas);
4369 }
4370
4371 void tst_QQuickListView::repositionResizedDelegate_data()
4372 {
4373     QTest::addColumn<QQuickListView::Orientation>("orientation");
4374     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4375     QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4376     QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4377     QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4378     QTest::addColumn<QRectF>("origPositionerRect");
4379     QTest::addColumn<QRectF>("resizedPositionerRect");
4380
4381     QTest::newRow("vertical")
4382             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4383             << QPointF(0, 60) << QPointF(0, 200 + 60)
4384             << QRectF(0, 200, 120, 120)
4385             << QRectF(0, 200, 120, 120 * 2);
4386
4387     QTest::newRow("vertical, BottomToTop")
4388             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4389             << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4390             << QRectF(0, -200 - 120, 120, 120)
4391             << QRectF(0, -200 - 120*2, 120, 120 * 2);
4392
4393     QTest::newRow("horizontal")
4394             << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4395             << QPointF(60, 0) << QPointF(260, 0)
4396             << QRectF(200, 0, 120, 120)
4397             << QRectF(200, 0, 120 * 2, 120);
4398
4399     QTest::newRow("horizontal, rtl")
4400             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4401             << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4402             << QRectF(-200 - 120, 0, 120, 120)
4403             << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4404 }
4405
4406 void tst_QQuickListView::QTBUG_16037()
4407 {
4408     QQuickView *canvas = createView();
4409     canvas->show();
4410
4411     canvas->setSource(testFileUrl("qtbug16037.qml"));
4412     qApp->processEvents();
4413
4414     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4415     QTRY_VERIFY(listview != 0);
4416
4417     QVERIFY(listview->contentHeight() <= 0.0);
4418
4419     QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
4420
4421     QTRY_COMPARE(listview->contentHeight(), 80.0);
4422
4423     delete canvas;
4424 }
4425
4426 void tst_QQuickListView::indexAt_itemAt_data()
4427 {
4428     QTest::addColumn<qreal>("x");
4429     QTest::addColumn<qreal>("y");
4430     QTest::addColumn<int>("index");
4431
4432     QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4433     QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4434     QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4435     QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4436     QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4437 }
4438
4439 void tst_QQuickListView::indexAt_itemAt()
4440 {
4441     QFETCH(qreal, x);
4442     QFETCH(qreal, y);
4443     QFETCH(int, index);
4444
4445     QQuickView *canvas = getView();
4446
4447     QmlListModel model;
4448     for (int i = 0; i < 30; i++)
4449         model.addItem("Item" + QString::number(i), "");
4450
4451     QQmlContext *ctxt = canvas->rootContext();
4452     ctxt->setContextProperty("testModel", &model);
4453
4454     TestObject *testObject = new TestObject;
4455     ctxt->setContextProperty("testObject", testObject);
4456
4457     canvas->setSource(testFileUrl("listviewtest.qml"));
4458     canvas->show();
4459     qApp->processEvents();
4460
4461     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4462     QTRY_VERIFY(listview != 0);
4463
4464     QQuickItem *contentItem = listview->contentItem();
4465     QTRY_VERIFY(contentItem != 0);
4466     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4467
4468     QQuickItem *item = 0;
4469     if (index >= 0) {
4470         item = findItem<QQuickItem>(contentItem, "wrapper", index);
4471         QVERIFY(item);
4472     }
4473     QCOMPARE(listview->indexAt(x,y), index);
4474     QVERIFY(listview->itemAt(x,y) == item);
4475
4476     releaseView(canvas);
4477     delete testObject;
4478 }
4479
4480 void tst_QQuickListView::incrementalModel()
4481 {
4482     QQuickView *canvas = createView();
4483
4484     IncrementalModel model;
4485     QQmlContext *ctxt = canvas->rootContext();
4486     ctxt->setContextProperty("testModel", &model);
4487
4488     canvas->setSource(testFileUrl("displaylist.qml"));
4489     qApp->processEvents();
4490
4491     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4492     QTRY_VERIFY(listview != 0);
4493
4494     QQuickItem *contentItem = listview->contentItem();
4495     QTRY_VERIFY(contentItem != 0);
4496
4497     QTRY_COMPARE(listview->count(), 20);
4498
4499     listview->positionViewAtIndex(10, QQuickListView::Beginning);
4500
4501     QTRY_COMPARE(listview->count(), 25);
4502
4503     delete canvas;
4504 }
4505
4506 void tst_QQuickListView::onAdd()
4507 {
4508     QFETCH(int, initialItemCount);
4509     QFETCH(int, itemsToAdd);
4510
4511     const int delegateHeight = 10;
4512     QaimModel model;
4513
4514     // these initial items should not trigger ListView.onAdd
4515     for (int i=0; i<initialItemCount; i++)
4516         model.addItem("dummy value", "dummy value");
4517
4518     QQuickView *canvas = createView();
4519     canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4520     QQmlContext *ctxt = canvas->rootContext();
4521     ctxt->setContextProperty("testModel", &model);
4522     ctxt->setContextProperty("delegateHeight", delegateHeight);
4523     canvas->setSource(testFileUrl("attachedSignals.qml"));
4524
4525     QObject *object = canvas->rootObject();
4526     object->setProperty("width", canvas->width());
4527     object->setProperty("height", canvas->height());
4528     qApp->processEvents();
4529
4530     QList<QPair<QString, QString> > items;
4531     for (int i=0; i<itemsToAdd; i++)
4532         items << qMakePair(QString("value %1").arg(i), QString::number(i));
4533     model.addItems(items);
4534     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4535
4536     QVariantList result = object->property("addedDelegates").toList();
4537     QCOMPARE(result.count(), items.count());
4538     for (int i=0; i<items.count(); i++)
4539         QCOMPARE(result[i].toString(), items[i].first);
4540
4541     delete canvas;
4542 }
4543
4544 void tst_QQuickListView::onAdd_data()
4545 {
4546     QTest::addColumn<int>("initialItemCount");
4547     QTest::addColumn<int>("itemsToAdd");
4548
4549     QTest::newRow("0, add 1") << 0 << 1;
4550     QTest::newRow("0, add 2") << 0 << 2;
4551     QTest::newRow("0, add 10") << 0 << 10;
4552
4553     QTest::newRow("1, add 1") << 1 << 1;
4554     QTest::newRow("1, add 2") << 1 << 2;
4555     QTest::newRow("1, add 10") << 1 << 10;
4556
4557     QTest::newRow("5, add 1") << 5 << 1;
4558     QTest::newRow("5, add 2") << 5 << 2;
4559     QTest::newRow("5, add 10") << 5 << 10;
4560 }
4561
4562 void tst_QQuickListView::onRemove()
4563 {
4564     QFETCH(int, initialItemCount);
4565     QFETCH(int, indexToRemove);
4566     QFETCH(int, removeCount);
4567
4568     const int delegateHeight = 10;
4569     QaimModel model;
4570     for (int i=0; i<initialItemCount; i++)
4571         model.addItem(QString("value %1").arg(i), "dummy value");
4572
4573     QQuickView *canvas = getView();
4574     QQmlContext *ctxt = canvas->rootContext();
4575     ctxt->setContextProperty("testModel", &model);
4576     ctxt->setContextProperty("delegateHeight", delegateHeight);
4577     canvas->setSource(testFileUrl("attachedSignals.qml"));
4578
4579     QObject *object = canvas->rootObject();
4580
4581     model.removeItems(indexToRemove, removeCount);
4582     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4583
4584     QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4585
4586     releaseView(canvas);
4587 }
4588
4589 void tst_QQuickListView::onRemove_data()
4590 {
4591     QTest::addColumn<int>("initialItemCount");
4592     QTest::addColumn<int>("indexToRemove");
4593     QTest::addColumn<int>("removeCount");
4594
4595     QTest::newRow("remove first") << 1 << 0 << 1;
4596     QTest::newRow("two items, remove first") << 2 << 0 << 1;
4597     QTest::newRow("two items, remove last") << 2 << 1 << 1;
4598     QTest::newRow("two items, remove all") << 2 << 0 << 2;
4599
4600     QTest::newRow("four items, remove first") << 4 << 0 << 1;
4601     QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4602     QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4603     QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4604     QTest::newRow("four items, remove last") << 4 << 3 << 1;
4605     QTest::newRow("four items, remove all") << 4 << 0 << 4;
4606
4607     QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4608     QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4609     QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4610 }
4611
4612 void tst_QQuickListView::rightToLeft()
4613 {
4614     QQuickView *canvas = createView();
4615     canvas->setGeometry(0,0,640,320);
4616     canvas->setSource(testFileUrl("rightToLeft.qml"));
4617     canvas->show();
4618     qApp->processEvents();
4619
4620     QVERIFY(canvas->rootObject() != 0);
4621     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4622     QTRY_VERIFY(listview != 0);
4623
4624     QQuickItem *contentItem = listview->contentItem();
4625     QTRY_VERIFY(contentItem != 0);
4626
4627     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4628
4629     QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4630     QTRY_VERIFY(model != 0);
4631
4632     QTRY_VERIFY(model->count() == 3);
4633     QTRY_COMPARE(listview->currentIndex(), 0);
4634
4635     // initial position at first item, right edge aligned
4636     QCOMPARE(listview->contentX(), -640.);
4637
4638     QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4639     QTRY_VERIFY(item);
4640     QTRY_COMPARE(item->x(), -100.0);
4641     QCOMPARE(item->height(), listview->height());
4642
4643     QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4644     QTRY_VERIFY(text);
4645     QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4646
4647     listview->setCurrentIndex(2);
4648
4649     item = findItem<QQuickItem>(contentItem, "item3");
4650     QTRY_VERIFY(item);
4651     QTRY_COMPARE(item->x(), -540.0);
4652
4653     text = findItem<QQuickText>(contentItem, "text3");
4654     QTRY_VERIFY(text);
4655     QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4656
4657     QCOMPARE(listview->contentX(), -640.);
4658
4659     // Ensure resizing maintains position relative to right edge
4660     qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4661     QTRY_COMPARE(listview->contentX(), -600.);
4662
4663     delete canvas;
4664 }
4665
4666 void tst_QQuickListView::test_mirroring()
4667 {
4668     QQuickView *canvasA = createView();
4669     canvasA->setSource(testFileUrl("rightToLeft.qml"));
4670     QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4671     QTRY_VERIFY(listviewA != 0);
4672
4673     QQuickView *canvasB = createView();
4674     canvasB->setSource(testFileUrl("rightToLeft.qml"));
4675     QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4676     QTRY_VERIFY(listviewA != 0);
4677     qApp->processEvents();
4678
4679     QList<QString> objectNames;
4680     objectNames << "item1" << "item2"; // << "item3"
4681
4682     listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4683     listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4684     QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4685
4686     // LTR != RTL
4687     foreach (const QString objectName, objectNames)
4688         QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4689
4690     listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4691     listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4692
4693     // LTR == LTR
4694     foreach (const QString objectName, objectNames)
4695         QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4696
4697     QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4698     QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4699     QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4700
4701     // LTR != LTR+mirror
4702     foreach (const QString objectName, objectNames)
4703         QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4704
4705     listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4706
4707     // RTL == LTR+mirror
4708     foreach (const QString objectName, objectNames)
4709         QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4710
4711     listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4712
4713     // RTL != RTL+mirror
4714     foreach (const QString objectName, objectNames)
4715         QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4716
4717     listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4718
4719     // LTR == RTL+mirror
4720     foreach (const QString objectName, objectNames)
4721         QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4722
4723     delete canvasA;
4724     delete canvasB;
4725 }
4726
4727 void tst_QQuickListView::margins()
4728 {
4729     QQuickView *canvas = createView();
4730
4731     QaimModel model;
4732     for (int i = 0; i < 50; i++)
4733         model.addItem("Item" + QString::number(i), "");
4734
4735     QQmlContext *ctxt = canvas->rootContext();
4736     ctxt->setContextProperty("testModel", &model);
4737
4738     canvas->setSource(testFileUrl("margins.qml"));
4739     canvas->show();
4740     qApp->processEvents();
4741
4742     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4743     QTRY_VERIFY(listview != 0);
4744     QQuickItem *contentItem = listview->contentItem();
4745     QTRY_VERIFY(contentItem != 0);
4746     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4747
4748     QCOMPARE(listview->contentY(), -30.);
4749     QCOMPARE(listview->originY(), 0.);
4750
4751     // check end bound
4752     listview->positionViewAtEnd();
4753     qreal pos = listview->contentY();
4754     listview->setContentY(pos + 80);
4755     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4756     listview->returnToBounds();
4757     QTRY_COMPARE(listview->contentY(), pos + 50);
4758
4759     // remove item before visible and check that top margin is maintained
4760     // and originY is updated
4761     listview->setContentY(100);
4762     model.removeItem(1);
4763     QTRY_COMPARE(listview->count(), model.count());
4764     listview->setContentY(-50);
4765     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4766     listview->returnToBounds();
4767     QCOMPARE(listview->originY(), 20.);
4768     QTRY_COMPARE(listview->contentY(), -10.);
4769
4770     // reduce top margin
4771     listview->setTopMargin(20);
4772     QCOMPARE(listview->originY(), 20.);
4773     QTRY_COMPARE(listview->contentY(), 0.);
4774
4775     // check end bound
4776     listview->positionViewAtEnd();
4777     pos = listview->contentY();
4778     listview->setContentY(pos + 80);
4779     listview->returnToBounds();
4780     QTRY_COMPARE(listview->contentY(), pos + 50);
4781
4782     // reduce bottom margin
4783     pos = listview->contentY();
4784     listview->setBottomMargin(40);
4785     QCOMPARE(listview->originY(), 20.);
4786     QTRY_COMPARE(listview->contentY(), pos-10);
4787
4788     delete canvas;
4789 }
4790
4791 // QTBUG-24028
4792 void tst_QQuickListView::marginsResize()
4793 {
4794     QFETCH(QQuickListView::Orientation, orientation);
4795     QFETCH(Qt::LayoutDirection, layoutDirection);
4796     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4797     QFETCH(qreal, start);
4798     QFETCH(qreal, end);
4799
4800     QPoint flickStart(20, 20);
4801     QPoint flickEnd(20, 20);
4802     if (orientation == QQuickListView::Vertical)
4803         flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4804     else
4805         flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4806
4807     QQuickView *canvas = getView();
4808
4809     canvas->setSource(testFileUrl("margins2.qml"));
4810     canvas->show();
4811     qApp->processEvents();
4812
4813     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4814     QTRY_VERIFY(listview != 0);
4815
4816     listview->setOrientation(orientation);
4817     listview->setLayoutDirection(layoutDirection);
4818     listview->setVerticalLayoutDirection(verticalLayoutDirection);
4819     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4820
4821     // view is resized after componentCompleted - top margin should still be visible
4822     if (orientation == QQuickListView::Vertical)
4823         QCOMPARE(listview->contentY(), start);
4824     else
4825         QCOMPARE(listview->contentX(), start);
4826
4827     // move to last index and ensure bottom margin is visible.
4828     listview->setCurrentIndex(19);
4829     if (orientation == QQuickListView::Vertical)
4830         QTRY_COMPARE(listview->contentY(), end);
4831     else
4832         QTRY_COMPARE(listview->contentX(), end);
4833
4834     // flick past the end and check content pos still settles on correct extents
4835     flick(canvas, flickStart, flickEnd, 180);
4836     QTRY_VERIFY(listview->isMoving() == false);
4837     if (orientation == QQuickListView::Vertical)
4838         QTRY_COMPARE(listview->contentY(), end);
4839     else
4840         QTRY_COMPARE(listview->contentX(), end);
4841
4842     // back to top - top margin should be visible.
4843     listview->setCurrentIndex(0);
4844     if (orientation == QQuickListView::Vertical)
4845         QTRY_COMPARE(listview->contentY(), start);
4846     else
4847         QTRY_COMPARE(listview->contentX(), start);
4848
4849     // flick past the beginning and check content pos still settles on correct extents
4850     flick(canvas, flickEnd, flickStart, 180);
4851     QTRY_VERIFY(listview->isMoving() == false);
4852     if (orientation == QQuickListView::Vertical)
4853         QTRY_COMPARE(listview->contentY(), start);
4854     else
4855         QTRY_COMPARE(listview->contentX(), start);
4856
4857     releaseView(canvas);
4858 }
4859
4860 void tst_QQuickListView::marginsResize_data()
4861 {
4862     QTest::addColumn<QQuickListView::Orientation>("orientation");
4863     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4864     QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4865     QTest::addColumn<qreal>("start");
4866     QTest::addColumn<qreal>("end");
4867
4868     // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4869
4870     QTest::newRow("vertical")
4871             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4872             << -40.0 << 1020.0;
4873
4874     QTest::newRow("vertical, BottomToTop")
4875             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4876             << -180.0 << -1240.0;
4877
4878     QTest::newRow("horizontal")
4879             << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4880             << -40.0 << 1020.0;
4881
4882     QTest::newRow("horizontal, rtl")
4883             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4884             << -180.0 << -1240.0;
4885 }
4886
4887 void tst_QQuickListView::snapToItem_data()
4888 {
4889     QTest::addColumn<QQuickListView::Orientation>("orientation");
4890     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4891     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4892     QTest::addColumn<int>("highlightRangeMode");
4893     QTest::addColumn<QPoint>("flickStart");
4894     QTest::addColumn<QPoint>("flickEnd");
4895     QTest::addColumn<qreal>("snapAlignment");
4896     QTest::addColumn<qreal>("endExtent");
4897     QTest::addColumn<qreal>("startExtent");
4898
4899     QTest::newRow("vertical, top to bottom")
4900         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4901         << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4902
4903     QTest::newRow("vertical, bottom to top")
4904         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4905         << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4906
4907     QTest::newRow("horizontal, left to right")
4908         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4909         << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4910
4911     QTest::newRow("horizontal, right to left")
4912         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4913         << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4914
4915     QTest::newRow("vertical, top to bottom, enforce range")
4916         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4917         << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4918
4919     QTest::newRow("vertical, bottom to top, enforce range")
4920         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4921         << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4922
4923     QTest::newRow("horizontal, left to right, enforce range")
4924         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4925         << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4926
4927     QTest::newRow("horizontal, right to left, enforce range")
4928         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4929         << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4930 }
4931
4932 void tst_QQuickListView::snapToItem()
4933 {
4934     QFETCH(QQuickListView::Orientation, orientation);
4935     QFETCH(Qt::LayoutDirection, layoutDirection);
4936     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4937     QFETCH(int, highlightRangeMode);
4938     QFETCH(QPoint, flickStart);
4939     QFETCH(QPoint, flickEnd);
4940     QFETCH(qreal, snapAlignment);
4941     QFETCH(qreal, endExtent);
4942     QFETCH(qreal, startExtent);
4943
4944     QQuickView *canvas = getView();
4945
4946     canvas->setSource(testFileUrl("snapToItem.qml"));
4947     canvas->show();
4948     qApp->processEvents();
4949
4950     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4951     QTRY_VERIFY(listview != 0);
4952
4953     listview->setOrientation(orientation);
4954     listview->setLayoutDirection(layoutDirection);
4955     listview->setVerticalLayoutDirection(verticalLayoutDirection);
4956     listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4957     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4958
4959     QQuickItem *contentItem = listview->contentItem();
4960     QTRY_VERIFY(contentItem != 0);
4961
4962     // confirm that a flick hits an item boundary
4963     flick(canvas, flickStart, flickEnd, 180);
4964     QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4965     if (orientation == QQuickListView::Vertical)
4966         QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4967     else
4968         QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4969
4970     // flick to end
4971     do {
4972         flick(canvas, flickStart, flickEnd, 180);
4973         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4974     } while (orientation == QQuickListView::Vertical
4975            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4976            : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4977
4978     if (orientation == QQuickListView::Vertical)
4979         QCOMPARE(listview->contentY(), endExtent);
4980     else
4981         QCOMPARE(listview->contentX(), endExtent);
4982
4983     // flick to start
4984     do {
4985         flick(canvas, flickEnd, flickStart, 180);
4986         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4987     } while (orientation == QQuickListView::Vertical
4988            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
4989            : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4990
4991     if (orientation == QQuickListView::Vertical)
4992         QCOMPARE(listview->contentY(), startExtent);
4993     else
4994         QCOMPARE(listview->contentX(), startExtent);
4995
4996     releaseView(canvas);
4997 }
4998
4999 void tst_QQuickListView::qListModelInterface_items()
5000 {
5001     items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5002 }
5003
5004 void tst_QQuickListView::qListModelInterface_package_items()
5005 {
5006     items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5007 }
5008
5009 void tst_QQuickListView::qAbstractItemModel_items()
5010 {
5011     items<QaimModel>(testFileUrl("listviewtest.qml"), false);
5012 }
5013
5014 void tst_QQuickListView::qListModelInterface_changed()
5015 {
5016     changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5017 }
5018
5019 void tst_QQuickListView::qListModelInterface_package_changed()
5020 {
5021     changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5022 }
5023
5024 void tst_QQuickListView::qAbstractItemModel_changed()
5025 {
5026     changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5027 }
5028
5029 void tst_QQuickListView::qListModelInterface_inserted()
5030 {
5031     inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
5032 }
5033
5034 void tst_QQuickListView::qListModelInterface_package_inserted()
5035 {
5036     inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5037 }
5038
5039 void tst_QQuickListView::qListModelInterface_inserted_more()
5040 {
5041     inserted_more<QmlListModel>();
5042 }
5043
5044 void tst_QQuickListView::qListModelInterface_inserted_more_data()
5045 {
5046     inserted_more_data();
5047 }
5048
5049 void tst_QQuickListView::qAbstractItemModel_inserted()
5050 {
5051     inserted<QaimModel>(testFileUrl("listviewtest.qml"));
5052 }
5053
5054 void tst_QQuickListView::qAbstractItemModel_inserted_more()
5055 {
5056     inserted_more<QaimModel>();
5057 }
5058
5059 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
5060 {
5061     inserted_more_data();
5062 }
5063
5064 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
5065 {
5066     inserted_more<QaimModel>(QQuickItemView::BottomToTop);
5067 }
5068
5069 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
5070 {
5071     inserted_more_data();
5072 }
5073
5074 void tst_QQuickListView::qListModelInterface_removed()
5075 {
5076     removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5077     removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
5078 }
5079
5080 void tst_QQuickListView::qListModelInterface_removed_more()
5081 {
5082     removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
5083 }
5084
5085 void tst_QQuickListView::qListModelInterface_removed_more_data()
5086 {
5087     removed_more_data();
5088 }
5089
5090 void tst_QQuickListView::qListModelInterface_package_removed()
5091 {
5092     removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
5093     removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5094 }
5095
5096 void tst_QQuickListView::qAbstractItemModel_removed()
5097 {
5098     removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5099     removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5100 }
5101
5102 void tst_QQuickListView::qAbstractItemModel_removed_more()
5103 {
5104     removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5105 }
5106
5107 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5108 {
5109     removed_more_data();
5110 }
5111
5112 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5113 {
5114     removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5115 }
5116
5117 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5118 {
5119     removed_more_data();
5120 }
5121
5122 void tst_QQuickListView::qListModelInterface_moved()
5123 {
5124     moved<QmlListModel>(testFileUrl("listviewtest.qml"));
5125 }
5126
5127 void tst_QQuickListView::qListModelInterface_moved_data()
5128 {
5129     moved_data();
5130 }
5131
5132 void tst_QQuickListView::qListModelInterface_package_moved()
5133 {
5134     moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5135 }
5136
5137 void tst_QQuickListView::qListModelInterface_package_moved_data()
5138 {
5139     moved_data();
5140 }
5141
5142 void tst_QQuickListView::qAbstractItemModel_moved()
5143 {
5144     moved<QaimModel>(testFileUrl("listviewtest.qml"));
5145 }
5146
5147 void tst_QQuickListView::qAbstractItemModel_moved_data()
5148 {
5149     moved_data();
5150 }
5151
5152 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5153 {
5154     moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5155 }
5156
5157 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5158 {
5159     moved_data();
5160 }
5161
5162 void tst_QQuickListView::qListModelInterface_clear()
5163 {
5164     clear<QmlListModel>(testFileUrl("listviewtest.qml"));
5165 }
5166
5167 void tst_QQuickListView::qListModelInterface_package_clear()
5168 {
5169     clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5170 }
5171
5172 void tst_QQuickListView::qAbstractItemModel_clear()
5173 {
5174     clear<QaimModel>(testFileUrl("listviewtest.qml"));
5175 }
5176
5177 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5178 {
5179     clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5180 }
5181
5182 void tst_QQuickListView::qListModelInterface_sections()
5183 {
5184     sections<QmlListModel>(testFileUrl("listview-sections.qml"));
5185 }
5186
5187 void tst_QQuickListView::qListModelInterface_package_sections()
5188 {
5189     sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
5190 }
5191
5192 void tst_QQuickListView::qAbstractItemModel_sections()
5193 {
5194     sections<QaimModel>(testFileUrl("listview-sections.qml"));
5195 }
5196
5197 void tst_QQuickListView::creationContext()
5198 {
5199     QQuickView canvas;
5200     canvas.setGeometry(0,0,240,320);
5201     canvas.setSource(testFileUrl("creationContext.qml"));
5202     qApp->processEvents();
5203
5204     QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5205     QVERIFY(rootItem);
5206     QVERIFY(rootItem->property("count").toInt() > 0);
5207
5208     QQuickItem *item;
5209     QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5210     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5211     QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5212     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5213     QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5214     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5215     QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5216     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5217 }
5218
5219 void tst_QQuickListView::QTBUG_21742()
5220 {
5221     QQuickView canvas;
5222     canvas.setGeometry(0,0,200,200);
5223     canvas.setSource(testFileUrl("qtbug-21742.qml"));
5224     qApp->processEvents();
5225
5226     QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5227     QVERIFY(rootItem);
5228     QCOMPARE(rootItem->property("count").toInt(), 1);
5229 }
5230
5231 void tst_QQuickListView::asynchronous()
5232 {
5233     QQuickView *canvas = createView();
5234     canvas->show();
5235     QQmlIncubationController controller;
5236     canvas->engine()->setIncubationController(&controller);
5237
5238     canvas->setSource(testFileUrl("asyncloader.qml"));
5239
5240     QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5241     QVERIFY(rootObject);
5242
5243     QQuickListView *listview = 0;
5244     while (!listview) {
5245         bool b = false;
5246         controller.incubateWhile(&b);
5247         listview = rootObject->findChild<QQuickListView*>("view");
5248     }
5249
5250     // items will be created one at a time
5251     for (int i = 0; i < 8; ++i) {
5252         QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5253         QQuickItem *item = 0;
5254         while (!item) {
5255             bool b = false;
5256             controller.incubateWhile(&b);
5257             item = findItem<QQuickItem>(listview, "wrapper", i);
5258         }
5259     }
5260
5261     {
5262         bool b = true;
5263         controller.incubateWhile(&b);
5264     }
5265
5266     // verify positioning
5267     QQuickItem *contentItem = listview->contentItem();
5268     for (int i = 0; i < 8; ++i) {
5269         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5270         QTRY_COMPARE(item->y(), i*50.0);
5271     }
5272
5273     delete canvas;
5274 }
5275
5276 void tst_QQuickListView::snapOneItem_data()
5277 {
5278     QTest::addColumn<QQuickListView::Orientation>("orientation");
5279     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5280     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5281     QTest::addColumn<int>("highlightRangeMode");
5282     QTest::addColumn<QPoint>("flickStart");
5283     QTest::addColumn<QPoint>("flickEnd");
5284     QTest::addColumn<qreal>("snapAlignment");
5285     QTest::addColumn<qreal>("endExtent");
5286     QTest::addColumn<qreal>("startExtent");
5287
5288     QTest::newRow("vertical, top to bottom")
5289         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5290         << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5291
5292     QTest::newRow("vertical, bottom to top")
5293         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5294         << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5295
5296     QTest::newRow("horizontal, left to right")
5297         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5298         << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5299
5300     QTest::newRow("horizontal, right to left")
5301         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5302         << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5303
5304     QTest::newRow("vertical, top to bottom, enforce range")
5305         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5306         << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5307
5308     QTest::newRow("vertical, bottom to top, enforce range")
5309         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5310         << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5311
5312     QTest::newRow("horizontal, left to right, enforce range")
5313         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5314         << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5315
5316     QTest::newRow("horizontal, right to left, enforce range")
5317         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5318         << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5319 }
5320
5321 void tst_QQuickListView::snapOneItem()
5322 {
5323     QFETCH(QQuickListView::Orientation, orientation);
5324     QFETCH(Qt::LayoutDirection, layoutDirection);
5325     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5326     QFETCH(int, highlightRangeMode);
5327     QFETCH(QPoint, flickStart);
5328     QFETCH(QPoint, flickEnd);
5329     QFETCH(qreal, snapAlignment);
5330     QFETCH(qreal, endExtent);
5331     QFETCH(qreal, startExtent);
5332
5333 #ifdef Q_OS_MAC
5334     // This test seems to be unreliable - different test data fails on different runs
5335     QSKIP("QTBUG-24338");
5336 #endif
5337
5338     QQuickView *canvas = getView();
5339
5340     canvas->setSource(testFileUrl("snapOneItem.qml"));
5341     canvas->show();
5342     qApp->processEvents();
5343
5344     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5345     QTRY_VERIFY(listview != 0);
5346
5347     listview->setOrientation(orientation);
5348     listview->setLayoutDirection(layoutDirection);
5349     listview->setVerticalLayoutDirection(verticalLayoutDirection);
5350     listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5351     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5352
5353     QQuickItem *contentItem = listview->contentItem();
5354     QTRY_VERIFY(contentItem != 0);
5355
5356     QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5357
5358     // confirm that a flick hits the next item boundary
5359     flick(canvas, flickStart, flickEnd, 180);
5360     QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5361     if (orientation == QQuickListView::Vertical)
5362         QCOMPARE(listview->contentY(), snapAlignment);
5363     else
5364         QCOMPARE(listview->contentX(), snapAlignment);
5365
5366     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5367         QCOMPARE(listview->currentIndex(), 1);
5368         QCOMPARE(currentIndexSpy.count(), 1);
5369     }
5370
5371     // flick to end
5372     do {
5373         flick(canvas, flickStart, flickEnd, 180);
5374         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5375     } while (orientation == QQuickListView::Vertical
5376            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5377            : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5378
5379     if (orientation == QQuickListView::Vertical)
5380         QCOMPARE(listview->contentY(), endExtent);
5381     else
5382         QCOMPARE(listview->contentX(), endExtent);
5383
5384     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5385         QCOMPARE(listview->currentIndex(), 3);
5386         QCOMPARE(currentIndexSpy.count(), 3);
5387     }
5388
5389     // flick to start
5390     do {
5391         flick(canvas, flickEnd, flickStart, 180);
5392         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5393     } while (orientation == QQuickListView::Vertical
5394            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5395            : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5396
5397     if (orientation == QQuickListView::Vertical)
5398         QCOMPARE(listview->contentY(), startExtent);
5399     else
5400         QCOMPARE(listview->contentX(), startExtent);
5401
5402     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5403         QCOMPARE(listview->currentIndex(), 0);
5404         QCOMPARE(currentIndexSpy.count(), 6);
5405     }
5406
5407     releaseView(canvas);
5408 }
5409
5410 void tst_QQuickListView::unrequestedVisibility()
5411 {
5412     QmlListModel model;
5413     for (int i = 0; i < 30; i++)
5414         model.addItem("Item" + QString::number(i), QString::number(i));
5415
5416     QQuickView *canvas = new QQuickView(0);
5417     canvas->setGeometry(0,0,240,320);
5418
5419     QQmlContext *ctxt = canvas->rootContext();
5420     ctxt->setContextProperty("testModel", &model);
5421     ctxt->setContextProperty("testWrap", QVariant(false));
5422
5423     canvas->setSource(testFileUrl("unrequestedItems.qml"));
5424     canvas->show();
5425     qApp->processEvents();
5426
5427     QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
5428     QTRY_VERIFY(leftview != 0);
5429
5430     QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
5431     QTRY_VERIFY(rightview != 0);
5432
5433     QQuickItem *leftContent = leftview->contentItem();
5434     QTRY_VERIFY(leftContent != 0);
5435
5436     QQuickItem *rightContent = rightview->contentItem();
5437     QTRY_VERIFY(rightContent != 0);
5438
5439     rightview->setCurrentIndex(20);
5440
5441     QTRY_COMPARE(leftview->contentY(), 0.0);
5442     QTRY_COMPARE(rightview->contentY(), 100.0);
5443
5444     QQuickItem *item;
5445
5446     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5447     QCOMPARE(delegateVisible(item), true);
5448     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5449     QCOMPARE(delegateVisible(item), false);
5450
5451     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5452     QCOMPARE(delegateVisible(item), false);
5453     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5454     QCOMPARE(delegateVisible(item), true);
5455
5456     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5457     QCOMPARE(delegateVisible(item), true);
5458     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5459     QCOMPARE(delegateVisible(item), false);
5460     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5461     QCOMPARE(delegateVisible(item), false);
5462     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5463     QCOMPARE(delegateVisible(item), true);
5464
5465     rightview->setCurrentIndex(0);
5466
5467     QTRY_COMPARE(leftview->contentY(), 0.0);
5468     QTRY_COMPARE(rightview->contentY(), 0.0);
5469
5470     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5471     QCOMPARE(delegateVisible(item), true);
5472     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5473     QTRY_COMPARE(delegateVisible(item), true);
5474
5475     QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5476     QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5477
5478     leftview->setCurrentIndex(20);
5479
5480     QTRY_COMPARE(leftview->contentY(), 100.0);
5481     QTRY_COMPARE(rightview->contentY(), 0.0);
5482
5483     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5484     QTRY_COMPARE(delegateVisible(item), false);
5485     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5486     QCOMPARE(delegateVisible(item), true);
5487
5488     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5489     QCOMPARE(delegateVisible(item), true);
5490     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5491     QCOMPARE(delegateVisible(item), false);
5492
5493     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5494     QCOMPARE(delegateVisible(item), false);
5495     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5496     QCOMPARE(delegateVisible(item), true);
5497     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5498     QCOMPARE(delegateVisible(item), true);
5499     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5500     QCOMPARE(delegateVisible(item), false);
5501
5502     model.moveItems(19, 1, 1);
5503     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5504
5505     QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5506     QCOMPARE(delegateVisible(item), false);
5507     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5508     QCOMPARE(delegateVisible(item), true);
5509
5510     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5511     QCOMPARE(delegateVisible(item), true);
5512     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5513     QCOMPARE(delegateVisible(item), false);
5514
5515     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5516     QCOMPARE(delegateVisible(item), false);
5517     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5518     QCOMPARE(delegateVisible(item), true);
5519     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5520     QCOMPARE(delegateVisible(item), true);
5521     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5522     QCOMPARE(delegateVisible(item), false);
5523
5524     model.moveItems(3, 4, 1);
5525     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5526
5527     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5528     QCOMPARE(delegateVisible(item), false);
5529     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5530     QCOMPARE(delegateVisible(item), true);
5531     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5532     QCOMPARE(delegateVisible(item), true);
5533     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5534     QCOMPARE(delegateVisible(item), false);
5535
5536     model.moveItems(4, 3, 1);
5537     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5538
5539     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5540     QCOMPARE(delegateVisible(item), false);
5541     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5542     QCOMPARE(delegateVisible(item), true);
5543     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5544     QCOMPARE(delegateVisible(item), true);
5545     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5546     QCOMPARE(delegateVisible(item), false);
5547
5548     model.moveItems(16, 17, 1);
5549     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5550
5551     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5552     QCOMPARE(delegateVisible(item), false);
5553     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5554     QCOMPARE(delegateVisible(item), true);
5555     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5556     QCOMPARE(delegateVisible(item), true);
5557     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5558     QCOMPARE(delegateVisible(item), false);
5559
5560     model.moveItems(17, 16, 1);
5561     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5562
5563     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5564     QCOMPARE(delegateVisible(item), false);
5565     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5566     QCOMPARE(delegateVisible(item), true);
5567     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5568     QCOMPARE(delegateVisible(item), true);
5569     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5570     QCOMPARE(delegateVisible(item), false);
5571
5572     delete canvas;
5573 }
5574
5575 void tst_QQuickListView::populateTransitions()
5576 {
5577     QFETCH(bool, staticallyPopulate);
5578     QFETCH(bool, dynamicallyPopulate);
5579     QFETCH(bool, usePopulateTransition);
5580
5581     QPointF transitionFrom(-50, -50);
5582     QPointF transitionVia(100, 100);
5583     QaimModel model_transitionFrom;
5584     QaimModel model_transitionVia;
5585
5586     QaimModel model;
5587     if (staticallyPopulate) {
5588         for (int i = 0; i < 30; i++)
5589             model.addItem("item" + QString::number(i), "");
5590     }
5591
5592     QQuickView *canvas = getView();
5593     canvas->rootContext()->setContextProperty("testModel", &model);
5594     canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
5595     canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5596     canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5597     canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5598     canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
5599     canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5600     canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5601     canvas->setSource(testFileUrl("populateTransitions.qml"));
5602     canvas->show();
5603     QTest::qWaitForWindowShown(canvas);
5604
5605     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5606     QVERIFY(listview);
5607     QQuickItem *contentItem = listview->contentItem();
5608     QVERIFY(contentItem);
5609
5610     if (staticallyPopulate && usePopulateTransition) {
5611         QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5612         QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5613     } else if (dynamicallyPopulate) {
5614         QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5615         QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5616     } else {
5617         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5618         QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5619         QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5620     }
5621
5622     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5623     for (int i=0; i < model.count() && i < itemCount; ++i) {
5624         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5625         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5626         QTRY_COMPARE(item->x(), 0.0);
5627         QTRY_COMPARE(item->y(), i*20.0);
5628         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5629         QVERIFY(name != 0);
5630         QTRY_COMPARE(name->text(), model.name(i));
5631     }
5632
5633     listview->setProperty("countPopulateTransitions", 0);
5634     listview->setProperty("countAddTransitions", 0);
5635
5636     // add an item and check this is done with add transition, not populate
5637     model.insertItem(0, "another item", "");
5638     QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5639     QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5640
5641     // clear the model
5642     canvas->rootContext()->setContextProperty("testModel", QVariant());
5643     QTRY_COMPARE(listview->count(), 0);
5644     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5645     listview->setProperty("countPopulateTransitions", 0);
5646     listview->setProperty("countAddTransitions", 0);
5647
5648     // set to a valid model and check populate transition is run a second time
5649     model.clear();
5650     for (int i = 0; i < 30; i++)
5651         model.addItem("item" + QString::number(i), "");
5652     canvas->rootContext()->setContextProperty("testModel", &model);
5653     QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5654     QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5655
5656     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5657     for (int i=0; i < model.count() && i < itemCount; ++i) {
5658         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5659         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5660         QTRY_COMPARE(item->x(), 0.0);
5661         QTRY_COMPARE(item->y(), i*20.0);
5662         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5663         QVERIFY(name != 0);
5664         QTRY_COMPARE(name->text(), model.name(i));
5665     }
5666
5667     // reset model and check populate transition is run again
5668     listview->setProperty("countPopulateTransitions", 0);
5669     listview->setProperty("countAddTransitions", 0);
5670     model.reset();
5671     QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5672     QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5673
5674     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5675     for (int i=0; i < model.count() && i < itemCount; ++i) {
5676         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5677         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5678         QTRY_COMPARE(item->x(), 0.0);
5679         QTRY_COMPARE(item->y(), i*20.0);
5680         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5681         QVERIFY(name != 0);
5682         QTRY_COMPARE(name->text(), model.name(i));
5683     }
5684
5685     releaseView(canvas);
5686 }
5687
5688 void tst_QQuickListView::populateTransitions_data()
5689 {
5690     QTest::addColumn<bool>("staticallyPopulate");
5691     QTest::addColumn<bool>("dynamicallyPopulate");
5692     QTest::addColumn<bool>("usePopulateTransition");
5693
5694     QTest::newRow("static") << true << false << true;
5695     QTest::newRow("static, no populate") << true << false << false;
5696
5697     QTest::newRow("dynamic") << false << true << true;
5698     QTest::newRow("dynamic, no populate") << false << true << false;
5699
5700     QTest::newRow("empty to start with") << false << false << true;
5701     QTest::newRow("empty to start with, no populate") << false << false << false;
5702 }
5703
5704 void tst_QQuickListView::addTransitions()
5705 {
5706     QFETCH(int, initialItemCount);
5707     QFETCH(bool, shouldAnimateTargets);
5708     QFETCH(qreal, contentY);
5709     QFETCH(int, insertionIndex);
5710     QFETCH(int, insertionCount);
5711     QFETCH(ListRange, expectedDisplacedIndexes);
5712
5713     // added items should start here
5714     QPointF targetItems_transitionFrom(-50, -50);
5715
5716     // displaced items should pass through this point
5717     QPointF displacedItems_transitionVia(100, 100);
5718
5719     QaimModel model;
5720     for (int i = 0; i < initialItemCount; i++)
5721         model.addItem("Original item" + QString::number(i), "");
5722     QaimModel model_targetItems_transitionFrom;
5723     QaimModel model_displacedItems_transitionVia;
5724
5725     QQuickView *canvas = getView();
5726     QQmlContext *ctxt = canvas->rootContext();
5727     TestObject *testObject = new TestObject;
5728     ctxt->setContextProperty("testModel", &model);
5729     ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5730     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5731     ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5732     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5733     ctxt->setContextProperty("testObject", testObject);
5734     canvas->setSource(testFileUrl("addTransitions.qml"));
5735     canvas->show();
5736     QTest::qWaitForWindowShown(canvas);
5737
5738     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5739     QTRY_VERIFY(listview != 0);
5740     QQuickItem *contentItem = listview->contentItem();
5741     QVERIFY(contentItem != 0);
5742     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5743
5744     if (contentY != 0) {
5745         listview->setContentY(contentY);
5746         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5747     }
5748
5749     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5750
5751     // only target items that will become visible should be animated
5752     QList<QPair<QString, QString> > newData;
5753     QList<QPair<QString, QString> > expectedTargetData;
5754     QList<int> targetIndexes;
5755     if (shouldAnimateTargets) {
5756         for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5757             newData << qMakePair(QString("New item %1").arg(i), QString(""));
5758
5759             if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {  // only grab visible items
5760                 expectedTargetData << newData.last();
5761                 targetIndexes << i;
5762             }
5763         }
5764         QVERIFY(expectedTargetData.count() > 0);
5765     }
5766
5767     // start animation
5768     if (!newData.isEmpty()) {
5769         model.insertItems(insertionIndex, newData);
5770         QTRY_COMPARE(model.count(), listview->count());
5771     }
5772
5773     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5774
5775     if (shouldAnimateTargets) {
5776         QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5777         QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5778                      expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5779
5780         // check the target and displaced items were animated
5781         model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5782         model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5783
5784         // check attached properties
5785         matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5786         matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5787         matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5788         if (expectedDisplacedIndexes.isValid()) {
5789             // adjust expectedDisplacedIndexes to their final values after the move
5790             QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5791             matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5792             matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5793             matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5794         }
5795
5796     } else {
5797         QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5798         QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5799     }
5800
5801     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5802     int firstVisibleIndex = -1;
5803     int itemCount = items.count();
5804     for (int i=0; i<items.count(); i++) {
5805         if (items[i]->y() >= contentY) {
5806             QQmlExpression e(qmlContext(items[i]), items[i], "index");
5807             firstVisibleIndex = e.evaluate().toInt();
5808             break;
5809         }
5810     }
5811     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5812
5813     // verify all items moved to the correct final positions
5814     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5815         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5816         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5817         QTRY_COMPARE(item->y(), i*20.0);
5818         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5819         QVERIFY(name != 0);
5820         QTRY_COMPARE(name->text(), model.name(i));
5821     }
5822
5823     releaseView(canvas);
5824     delete testObject;
5825 }
5826
5827 void tst_QQuickListView::addTransitions_data()
5828 {
5829     QTest::addColumn<int>("initialItemCount");
5830     QTest::addColumn<qreal>("contentY");
5831     QTest::addColumn<bool>("shouldAnimateTargets");
5832     QTest::addColumn<int>("insertionIndex");
5833     QTest::addColumn<int>("insertionCount");
5834     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5835
5836     // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5837     QTest::newRow("insert 1, just before start")
5838             << 30 << 20.0 << false
5839             << 0 << 1 << ListRange();
5840     QTest::newRow("insert 1, way before start")
5841             << 30 << 20.0 << false
5842             << 0 << 1 << ListRange();
5843     QTest::newRow("insert multiple, just before start")
5844             << 30 << 100.0 << false
5845             << 0 << 3 << ListRange();
5846     QTest::newRow("insert multiple, way before start")
5847             << 30 << 100.0 << false
5848             << 0 << 3 << ListRange();
5849
5850     QTest::newRow("insert 1 at start")
5851             << 30 << 0.0 << true
5852             << 0 << 1 << ListRange(0, 15);
5853     QTest::newRow("insert multiple at start")
5854             << 30 << 0.0 << true
5855             << 0 << 3 << ListRange(0, 15);
5856     QTest::newRow("insert 1 at start, content y not 0")
5857             << 30 << 40.0 << true  // first visible is index 2, so translate the displaced indexes by 2
5858             << 2 << 1 << ListRange(0 + 2, 15 + 2);
5859     QTest::newRow("insert multiple at start, content y not 0")
5860             << 30 << 40.0 << true    // first visible is index 2
5861             << 2 << 3 << ListRange(0 + 2, 15 + 2);
5862
5863     QTest::newRow("insert 1 at start, to empty list")
5864             << 0 << 0.0 << true
5865             << 0 << 1 << ListRange();
5866     QTest::newRow("insert multiple at start, to empty list")
5867             << 0 << 0.0 << true
5868             << 0 << 3 << ListRange();
5869
5870     QTest::newRow("insert 1 at middle")
5871             << 30 << 0.0 << true
5872             << 5 << 1 << ListRange(5, 15);
5873     QTest::newRow("insert multiple at middle")
5874             << 30 << 0.0 << true
5875             << 5 << 3 << ListRange(5, 15);
5876
5877     QTest::newRow("insert 1 at bottom")
5878             << 30 << 0.0 << true
5879             << 15 << 1 << ListRange(15, 15);
5880     QTest::newRow("insert multiple at bottom")
5881             << 30 << 0.0 << true
5882             << 15 << 3 << ListRange(15, 15);
5883     QTest::newRow("insert 1 at bottom, content y not 0")
5884             << 30 << 20.0 * 3 << true
5885             << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5886     QTest::newRow("insert multiple at bottom, content y not 0")
5887             << 30 << 20.0 * 3 << true
5888             << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5889
5890     // items added after the last visible will not be animated in, since they
5891     // do not appear in the final view
5892     QTest::newRow("insert 1 after end")
5893             << 30 << 0.0 << false
5894             << 17 << 1 << ListRange();
5895     QTest::newRow("insert multiple after end")
5896             << 30 << 0.0 << false
5897             << 17 << 3 << ListRange();
5898 }
5899
5900 void tst_QQuickListView::moveTransitions()
5901 {
5902     QFETCH(int, initialItemCount);
5903     QFETCH(qreal, contentY);
5904     QFETCH(qreal, itemsOffsetAfterMove);
5905     QFETCH(int, moveFrom);
5906     QFETCH(int, moveTo);
5907     QFETCH(int, moveCount);
5908     QFETCH(ListRange, expectedDisplacedIndexes);
5909
5910     // target and displaced items should pass through these points
5911     QPointF targetItems_transitionVia(-50, 50);
5912     QPointF displacedItems_transitionVia(100, 100);
5913
5914     QaimModel model;
5915     for (int i = 0; i < initialItemCount; i++)
5916         model.addItem("Original item" + QString::number(i), "");
5917     QaimModel model_targetItems_transitionVia;
5918     QaimModel model_displacedItems_transitionVia;
5919
5920     QQuickView *canvas = getView();
5921     QQmlContext *ctxt = canvas->rootContext();
5922     TestObject *testObject = new TestObject;
5923     ctxt->setContextProperty("testModel", &model);
5924     ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5925     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5926     ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5927     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5928     ctxt->setContextProperty("testObject", testObject);
5929     canvas->setSource(testFileUrl("moveTransitions.qml"));
5930     canvas->show();
5931     QTest::qWaitForWindowShown(canvas);
5932
5933     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5934     QTRY_VERIFY(listview != 0);
5935     QQuickItem *contentItem = listview->contentItem();
5936     QVERIFY(contentItem != 0);
5937     QQuickText *name;
5938
5939     if (contentY != 0) {
5940         listview->setContentY(contentY);
5941         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5942     }
5943
5944     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5945
5946     // Items moving to *or* from visible positions should be animated.
5947     // Otherwise, they should not be animated.
5948     QList<QPair<QString, QString> > expectedTargetData;
5949     QList<int> targetIndexes;
5950     for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5951         int toIndex = moveTo + (i - moveFrom);
5952         if (i <= (contentY + listview->height()) / 20
5953                 || toIndex < (contentY + listview->height()) / 20) {
5954             expectedTargetData << qMakePair(model.name(i), model.number(i));
5955             targetIndexes << i;
5956         }
5957     }
5958     // ViewTransition.index provides the indices that items are moving to, not from
5959     targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5960
5961     // start animation
5962     model.moveItems(moveFrom, moveTo, moveCount);
5963
5964     QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5965     QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5966                  expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5967
5968     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5969
5970     // check the target and displaced items were animated
5971     model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5972     model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5973
5974     // check attached properties
5975     matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5976     matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5977     matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5978     if (expectedDisplacedIndexes.isValid()) {
5979         // adjust expectedDisplacedIndexes to their final values after the move
5980         QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5981         matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5982         matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5983         matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5984     }
5985
5986     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5987     int firstVisibleIndex = -1;
5988     for (int i=0; i<items.count(); i++) {
5989         if (items[i]->y() >= contentY) {
5990             QQmlExpression e(qmlContext(items[i]), items[i], "index");
5991             firstVisibleIndex = e.evaluate().toInt();
5992             break;
5993         }
5994     }
5995     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5996
5997     // verify all items moved to the correct final positions
5998     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5999     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6000         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6001         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6002         QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
6003         name = findItem<QQuickText>(contentItem, "textName", i);
6004         QVERIFY(name != 0);
6005         QTRY_COMPARE(name->text(), model.name(i));
6006     }
6007
6008     releaseView(canvas);
6009     delete testObject;
6010 }
6011
6012 void tst_QQuickListView::moveTransitions_data()
6013 {
6014     QTest::addColumn<int>("initialItemCount");
6015     QTest::addColumn<qreal>("contentY");
6016     QTest::addColumn<qreal>("itemsOffsetAfterMove");
6017     QTest::addColumn<int>("moveFrom");
6018     QTest::addColumn<int>("moveTo");
6019     QTest::addColumn<int>("moveCount");
6020     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6021
6022     // when removing from above the visible, all items shift down depending on how many
6023     // items have been removed from above the visible
6024     QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
6025             << 1 << 10 << 1 << ListRange(11, 15+4);
6026     QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
6027             << 0 << 10 << 1 << ListRange(11, 15+4);
6028     QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
6029             << 1 << 10 << 2 << ListRange(12, 15+4);
6030     QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
6031             << 0 << 10 << 3 << ListRange(13, 15+4);
6032     QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
6033             << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
6034     QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
6035             << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
6036
6037     QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
6038             << 1 << 10 << 1 << ListRange(2, 10);
6039     QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
6040             << 0 << 10 << 1 << ListRange(1, 10);
6041     QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6042             << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
6043     QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
6044             << 10 << 15 << 1 << ListRange(11, 15);
6045     QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
6046             << 0 << 15 << 1 << ListRange(1, 15);
6047
6048     QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
6049             << 1 << 10 << 3 << ListRange(4, 12);
6050     QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
6051             << 0 << 10 << 3 << ListRange(3, 12);
6052     QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6053             << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
6054     QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
6055             << 5 << 13 << 3 << ListRange(8, 15);
6056     QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
6057             << 0 << 13 << 3 << ListRange(3, 15);
6058
6059     QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
6060             << 10 << 1 << 1 << ListRange(1, 9);
6061     QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
6062             << 10 << 0 << 1 << ListRange(0, 9);
6063     QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6064             << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6065     QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
6066             << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6067     QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
6068             << 15 << 10 << 1 << ListRange(10, 14);
6069     QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
6070             << 15 << 0 << 1 << ListRange(0, 14);
6071
6072     QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
6073             << 10 << 1 << 3 << ListRange(1, 9);
6074     QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
6075             << 10 << 0 << 3 << ListRange(0, 9);
6076     QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6077             << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
6078     QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
6079             << 13 << 5 << 3 << ListRange(5, 12);
6080     QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
6081             << 13 << 0 << 3 << ListRange(0, 12);
6082
6083     QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
6084             << 20 << 0 << 1 << ListRange(0, 15);
6085     QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6086             << 25 << 4 << 1 << ListRange(0+4, 15+4);
6087     QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
6088             << 20 << 0 << 3 << ListRange(0, 15);
6089     QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6090             << 25 << 4 << 3 << ListRange(0+4, 15+4);
6091
6092     QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
6093             << 20 << 15 << 1 << ListRange(15, 15);
6094     QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6095             << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
6096     QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6097             << 20 << 15 << 3 << ListRange(15, 15);
6098     QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6099             << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6100 }
6101
6102 void tst_QQuickListView::removeTransitions()
6103 {
6104     QFETCH(int, initialItemCount);
6105     QFETCH(bool, shouldAnimateTargets);
6106     QFETCH(qreal, contentY);
6107     QFETCH(int, removalIndex);
6108     QFETCH(int, removalCount);
6109     QFETCH(ListRange, expectedDisplacedIndexes);
6110
6111     // added items should end here
6112     QPointF targetItems_transitionTo(-50, -50);
6113
6114     // displaced items should pass through this points
6115     QPointF displacedItems_transitionVia(100, 100);
6116
6117     QaimModel model;
6118     for (int i = 0; i < initialItemCount; i++)
6119         model.addItem("Original item" + QString::number(i), "");
6120     QaimModel model_targetItems_transitionTo;
6121     QaimModel model_displacedItems_transitionVia;
6122
6123     QQuickView *canvas = getView();
6124     QQmlContext *ctxt = canvas->rootContext();
6125     TestObject *testObject = new TestObject;
6126     ctxt->setContextProperty("testModel", &model);
6127     ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6128     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6129     ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6130     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6131     ctxt->setContextProperty("testObject", testObject);
6132     canvas->setSource(testFileUrl("removeTransitions.qml"));
6133     canvas->show();
6134     QTest::qWaitForWindowShown(canvas);
6135
6136     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6137     QTRY_VERIFY(listview != 0);
6138     QQuickItem *contentItem = listview->contentItem();
6139     QVERIFY(contentItem != 0);
6140     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6141
6142     if (contentY != 0) {
6143         listview->setContentY(contentY);
6144         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6145     }
6146
6147     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6148
6149     // only target items that are visible should be animated
6150     QList<QPair<QString, QString> > expectedTargetData;
6151     QList<int> targetIndexes;
6152     if (shouldAnimateTargets) {
6153         for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6154             if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6155                 expectedTargetData << qMakePair(model.name(i), model.number(i));
6156                 targetIndexes << i;
6157             }
6158         }
6159         QVERIFY(expectedTargetData.count() > 0);
6160     }
6161
6162     // calculate targetItems and expectedTargets before model changes
6163     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6164     QVariantMap expectedTargets;
6165     for (int i=0; i<targetIndexes.count(); i++)
6166         expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6167
6168     // start animation
6169     model.removeItems(removalIndex, removalCount);
6170     QTRY_COMPARE(model.count(), listview->count());
6171
6172     if (shouldAnimateTargets) {
6173         QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6174         QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6175                      expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6176
6177         // check the target and displaced items were animated
6178         model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6179         model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6180
6181         // check attached properties
6182         QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6183         matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6184         matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6185         if (expectedDisplacedIndexes.isValid()) {
6186             // adjust expectedDisplacedIndexes to their final values after the move
6187             QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6188             matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6189             matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6190             matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6191         }
6192     } else {
6193         QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6194         QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6195     }
6196
6197     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6198     int firstVisibleIndex = -1;
6199     int itemCount = items.count();
6200
6201     for (int i=0; i<items.count(); i++) {
6202         QQmlExpression e(qmlContext(items[i]), items[i], "index");
6203         int index = e.evaluate().toInt();
6204         if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6205             firstVisibleIndex = index;
6206         if (index < 0)
6207             itemCount--;    // exclude deleted items
6208     }
6209     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6210
6211     // verify all items moved to the correct final positions
6212     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6213         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6214         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6215         QCOMPARE(item->x(), 0.0);
6216         QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6217         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6218         QVERIFY(name != 0);
6219         QTRY_COMPARE(name->text(), model.name(i));
6220     }
6221
6222     releaseView(canvas);
6223     delete testObject;
6224 }
6225
6226 void tst_QQuickListView::removeTransitions_data()
6227 {
6228     QTest::addColumn<int>("initialItemCount");
6229     QTest::addColumn<qreal>("contentY");
6230     QTest::addColumn<bool>("shouldAnimateTargets");
6231     QTest::addColumn<int>("removalIndex");
6232     QTest::addColumn<int>("removalCount");
6233     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6234
6235     // All items that are visible following the remove operation should be animated.
6236     // Remove targets that are outside of the view should not be animated.
6237
6238     QTest::newRow("remove 1 before start")
6239             << 30 << 20.0 * 3 << false
6240             << 2 << 1 << ListRange();
6241     QTest::newRow("remove multiple, all before start")
6242             << 30 << 20.0 * 3 << false
6243             << 0 << 3 << ListRange();
6244     QTest::newRow("remove mix of before and after start")
6245             << 30 << 20.0 * 3 << true
6246             << 2 << 3 << ListRange(5, 20);  // 5-20 are visible after the remove
6247
6248     QTest::newRow("remove 1 from start")
6249             << 30 << 0.0 << true
6250             << 0 << 1 << ListRange(1, 16);  // 1-16 are visible after the remove
6251     QTest::newRow("remove multiple from start")
6252             << 30 << 0.0 << true
6253             << 0 << 3 << ListRange(3, 18);  // 3-18 are visible after the remove
6254     QTest::newRow("remove 1 from start, content y not 0")
6255             << 30 << 20.0 * 2 << true  // first visible is index 2, so translate the displaced indexes by 2
6256             << 2 << 1 << ListRange(1 + 2, 16 + 2);
6257     QTest::newRow("remove multiple from start, content y not 0")
6258             << 30 << 20.0 * 2 << true    // first visible is index 2
6259             << 2 << 3 << ListRange(3 + 2, 18 + 2);
6260
6261     QTest::newRow("remove 1 from middle")
6262             << 30 << 0.0 << true
6263             << 5 << 1 << ListRange(6, 16);
6264     QTest::newRow("remove multiple from middle")
6265             << 30 << 0.0 << true
6266             << 5 << 3 << ListRange(8, 18);
6267
6268
6269     QTest::newRow("remove 1 from bottom")
6270             << 30 << 0.0 << true
6271             << 15 << 1 << ListRange(16, 16);
6272
6273     // remove 15, 16, 17
6274     // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6275     // the view, and 18 will be animated as the displaced item to replace the last item
6276     QTest::newRow("remove multiple from bottom")
6277             << 30 << 0.0 << true
6278             << 15 << 3 << ListRange(18, 18);
6279
6280     QTest::newRow("remove 1 from bottom, content y not 0")
6281             << 30 << 20.0 * 2 << true
6282             << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6283     QTest::newRow("remove multiple from bottom, content y not 0")
6284             << 30 << 20.0 * 2 << true
6285             << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6286
6287
6288     QTest::newRow("remove 1 after end")
6289             << 30 << 0.0 << false
6290             << 17 << 1 << ListRange();
6291     QTest::newRow("remove multiple after end")
6292             << 30 << 0.0 << false
6293             << 17 << 3 << ListRange();
6294 }
6295
6296 void tst_QQuickListView::displacedTransitions()
6297 {
6298     QFETCH(bool, useDisplaced);
6299     QFETCH(bool, displacedEnabled);
6300     QFETCH(bool, useAddDisplaced);
6301     QFETCH(bool, addDisplacedEnabled);
6302     QFETCH(bool, useMoveDisplaced);
6303     QFETCH(bool, moveDisplacedEnabled);
6304     QFETCH(bool, useRemoveDisplaced);
6305     QFETCH(bool, removeDisplacedEnabled);
6306     QFETCH(ListChange, change);
6307     QFETCH(ListRange, expectedDisplacedIndexes);
6308
6309     QaimModel model;
6310     for (int i = 0; i < 30; i++)
6311         model.addItem("Original item" + QString::number(i), "");
6312     QaimModel model_displaced_transitionVia;
6313     QaimModel model_addDisplaced_transitionVia;
6314     QaimModel model_moveDisplaced_transitionVia;
6315     QaimModel model_removeDisplaced_transitionVia;
6316
6317     QPointF displaced_transitionVia(-50, -100);
6318     QPointF addDisplaced_transitionVia(-150, 100);
6319     QPointF moveDisplaced_transitionVia(50, -100);
6320     QPointF removeDisplaced_transitionVia(150, 100);
6321
6322     QQuickView *canvas = getView();
6323     QQmlContext *ctxt = canvas->rootContext();
6324     TestObject *testObject = new TestObject(canvas);
6325     ctxt->setContextProperty("testModel", &model);
6326     ctxt->setContextProperty("testObject", testObject);
6327     ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6328     ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6329     ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6330     ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6331     ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6332     ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6333     ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6334     ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6335     ctxt->setContextProperty("useDisplaced", useDisplaced);
6336     ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6337     ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6338     ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6339     ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6340     ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6341     ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6342     ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6343     canvas->setSource(testFileUrl("displacedTransitions.qml"));
6344     canvas->show();
6345     qApp->processEvents();
6346
6347     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6348     QTRY_VERIFY(listview != 0);
6349     QQuickItem *contentItem = listview->contentItem();
6350     QVERIFY(contentItem != 0);
6351     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6352
6353     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6354     listview->setProperty("displaceTransitionsDone", false);
6355
6356     switch (change.type) {
6357         case ListChange::Inserted:
6358         {
6359             QList<QPair<QString, QString> > targetItemData;
6360             for (int i=change.index; i<change.index + change.count; ++i)
6361                 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6362             model.insertItems(change.index, targetItemData);
6363             QTRY_COMPARE(model.count(), listview->count());
6364             break;
6365         }
6366         case ListChange::Removed:
6367             model.removeItems(change.index, change.count);
6368             QTRY_COMPARE(model.count(), listview->count());
6369             break;
6370         case ListChange::Moved:
6371             model.moveItems(change.index, change.to, change.count);
6372             QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6373             break;
6374         case ListChange::SetCurrent:
6375         case ListChange::SetContentY:
6376         case ListChange::Polish:
6377             break;
6378     }
6379
6380     QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6381     QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6382
6383     if ((useDisplaced && displacedEnabled)
6384             || (useAddDisplaced && addDisplacedEnabled)
6385             || (useMoveDisplaced && moveDisplacedEnabled)
6386             || (useRemoveDisplaced && removeDisplacedEnabled)) {
6387         QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6388
6389         // check the correct number of target items and indexes were received
6390         QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6391         for (int i=0; i<resultTargetIndexes.count(); i++)
6392             QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6393         QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6394         for (int i=0; i<resultTargetItems.count(); i++)
6395             QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6396     } else {
6397         QCOMPARE(resultTargetIndexes.count(), 0);
6398         QCOMPARE(resultTargetItems.count(), 0);
6399     }
6400
6401     if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6402         model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6403     else
6404         QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6405     if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6406         model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6407     else
6408         QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6409     if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6410         model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6411     else
6412         QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6413
6414     if (useDisplaced && displacedEnabled
6415             && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6416                  || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6417                  || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6418         model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6419     } else {
6420         QCOMPARE(model_displaced_transitionVia.count(), 0);
6421     }
6422
6423     // verify all items moved to the correct final positions
6424     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6425     for (int i=0; i < model.count() && i < items.count(); ++i) {
6426         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6427         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6428         QCOMPARE(item->x(), 0.0);
6429         QCOMPARE(item->y(), i * 20.0);
6430         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6431         QVERIFY(name != 0);
6432         QTRY_COMPARE(name->text(), model.name(i));
6433     }
6434
6435     releaseView(canvas);
6436 }
6437
6438 void tst_QQuickListView::displacedTransitions_data()
6439 {
6440     QTest::addColumn<bool>("useDisplaced");
6441     QTest::addColumn<bool>("displacedEnabled");
6442     QTest::addColumn<bool>("useAddDisplaced");
6443     QTest::addColumn<bool>("addDisplacedEnabled");
6444     QTest::addColumn<bool>("useMoveDisplaced");
6445     QTest::addColumn<bool>("moveDisplacedEnabled");
6446     QTest::addColumn<bool>("useRemoveDisplaced");
6447     QTest::addColumn<bool>("removeDisplacedEnabled");
6448     QTest::addColumn<ListChange>("change");
6449     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6450
6451     QTest::newRow("no displaced transitions at all")
6452             << false << false
6453             << false << false
6454             << false << false
6455             << false << false
6456             << ListChange::insert(0, 1) << ListRange(0, 15);
6457
6458     QTest::newRow("just displaced")
6459             << true << true
6460             << false << false
6461             << false << false
6462             << false << false
6463             << ListChange::insert(0, 1) << ListRange(0, 15);
6464
6465     QTest::newRow("just displaced (not enabled)")
6466             << true << false
6467             << false << false
6468             << false << false
6469             << false << false
6470             << ListChange::insert(0, 1) << ListRange(0, 15);
6471
6472     QTest::newRow("displaced + addDisplaced")
6473             << true << true
6474             << true << true
6475             << false << false
6476             << false << false
6477             << ListChange::insert(0, 1) << ListRange(0, 15);
6478
6479     QTest::newRow("displaced + addDisplaced (not enabled)")
6480             << true << true
6481             << true << false
6482             << false << false
6483             << false << false
6484             << ListChange::insert(0, 1) << ListRange(0, 15);
6485
6486     QTest::newRow("displaced + moveDisplaced")
6487             << true << true
6488             << false << false
6489             << true << true
6490             << false << false
6491             << ListChange::move(0, 10, 1) << ListRange(1, 10);
6492
6493     QTest::newRow("displaced + moveDisplaced (not enabled)")
6494             << true << true
6495             << false << false
6496             << true << false
6497             << false << false
6498             << ListChange::move(0, 10, 1) << ListRange(1, 10);
6499
6500     QTest::newRow("displaced + removeDisplaced")
6501             << true << true
6502             << false << false
6503             << false << false
6504             << true << true
6505             << ListChange::remove(0, 1) << ListRange(1, 16);
6506
6507     QTest::newRow("displaced + removeDisplaced (not enabled)")
6508             << true << true
6509             << false << false
6510             << false << false
6511             << true << false
6512             << ListChange::remove(0, 1) << ListRange(1, 16);
6513
6514
6515     QTest::newRow("displaced + add, should use generic displaced for a remove")
6516             << true << true
6517             << true << true
6518             << false << false
6519             << true << false
6520             << ListChange::remove(0, 1) << ListRange(1, 16);
6521 }
6522
6523 void tst_QQuickListView::multipleTransitions()
6524 {
6525     // Tests that if you interrupt a transition in progress with another action that
6526     // cancels the previous transition, the resulting items are still placed correctly.
6527
6528     QFETCH(int, initialCount);
6529     QFETCH(qreal, contentY);
6530     QFETCH(QList<ListChange>, changes);
6531     QFETCH(bool, enableAddTransitions);
6532     QFETCH(bool, enableMoveTransitions);
6533     QFETCH(bool, enableRemoveTransitions);
6534     QFETCH(bool, rippleAddDisplaced);
6535
6536     QPointF addTargets_transitionFrom(-50, -50);
6537     QPointF addDisplaced_transitionFrom(-50, 50);
6538     QPointF moveTargets_transitionFrom(50, -50);
6539     QPointF moveDisplaced_transitionFrom(50, 50);
6540     QPointF removeTargets_transitionTo(-100, 300);
6541     QPointF removeDisplaced_transitionFrom(100, 300);
6542
6543     QmlListModel model;
6544     for (int i = 0; i < initialCount; i++)
6545         model.addItem("Original item" + QString::number(i), "");
6546
6547     QQuickView *canvas = getView();
6548     QQmlContext *ctxt = canvas->rootContext();
6549     TestObject *testObject = new TestObject;
6550     ctxt->setContextProperty("testModel", &model);
6551     ctxt->setContextProperty("testObject", testObject);
6552     ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6553     ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6554     ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6555     ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6556     ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6557     ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6558     ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6559     ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6560     ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6561     ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6562     canvas->setSource(testFileUrl("multipleTransitions.qml"));
6563     canvas->show();
6564     QTest::qWaitForWindowShown(canvas);
6565
6566     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6567     QTRY_VERIFY(listview != 0);
6568     QQuickItem *contentItem = listview->contentItem();
6569     QVERIFY(contentItem != 0);
6570     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6571
6572     if (contentY != 0) {
6573         listview->setContentY(contentY);
6574         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6575     }
6576
6577     int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
6578
6579     for (int i=0; i<changes.count(); i++) {
6580         switch (changes[i].type) {
6581             case ListChange::Inserted:
6582             {
6583                 QList<QPair<QString, QString> > targetItems;
6584                 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6585                     targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6586                 model.insertItems(changes[i].index, targetItems);
6587                 QTRY_COMPARE(model.count(), listview->count());
6588                 if (i == changes.count() - 1) {
6589                     QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6590                     QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6591                 } else {
6592                     QTest::qWait(timeBetweenActions);
6593                 }
6594                 break;
6595             }
6596             case ListChange::Removed:
6597                 model.removeItems(changes[i].index, changes[i].count);
6598                 QTRY_COMPARE(model.count(), listview->count());
6599                 if (i == changes.count() - 1) {
6600                     QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6601                     QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6602                 } else {
6603                     QTest::qWait(timeBetweenActions);
6604                 }
6605                 break;
6606             case ListChange::Moved:
6607                 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6608                 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6609                 if (i == changes.count() - 1) {
6610                     QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6611                     QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6612                 } else {
6613                     QTest::qWait(timeBetweenActions);
6614                 }
6615                 break;
6616             case ListChange::SetCurrent:
6617                 listview->setCurrentIndex(changes[i].index);
6618                 break;
6619             case ListChange::SetContentY:
6620                 listview->setContentY(changes[i].pos);
6621                 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6622                 break;
6623             case ListChange::Polish:
6624                 break;
6625         }
6626     }
6627     QCOMPARE(listview->count(), model.count());
6628
6629     // verify all items moved to the correct final positions
6630     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6631     for (int i=0; i < model.count() && i < items.count(); ++i) {
6632         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6633         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6634         QTRY_COMPARE(item->x(), 0.0);
6635         QTRY_COMPARE(item->y(), i*20.0);
6636         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6637         QVERIFY(name != 0);
6638         QTRY_COMPARE(name->text(), model.name(i));
6639     }
6640
6641     releaseView(canvas);
6642     delete testObject;
6643 }
6644
6645 void tst_QQuickListView::multipleTransitions_data()
6646 {
6647     QTest::addColumn<int>("initialCount");
6648     QTest::addColumn<qreal>("contentY");
6649     QTest::addColumn<QList<ListChange> >("changes");
6650     QTest::addColumn<bool>("enableAddTransitions");
6651     QTest::addColumn<bool>("enableMoveTransitions");
6652     QTest::addColumn<bool>("enableRemoveTransitions");
6653     QTest::addColumn<bool>("rippleAddDisplaced");
6654
6655     // the added item and displaced items should move to final dest correctly
6656     QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6657             << ListChange::insert(0, 1)
6658             << ListChange::move(0, 3, 1)
6659             )
6660             << true << true << true << false;
6661
6662     // items affected by the add should change from move to add transition
6663     QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6664             << ListChange::move(1, 10, 3)
6665             << ListChange::insert(0, 1)
6666             )
6667             << true << true << true << false;
6668
6669     // items should be placed correctly if you trigger a transition then refill for that index
6670     QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6671             << ListChange::insert(0, 1)
6672             << ListChange::setContentY(80.0)
6673             << ListChange::setContentY(0.0)
6674             << ListChange::insert(0, 1)
6675             )
6676             << true << true << true << false;
6677
6678     QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6679             << ListChange::insert(1, 1)
6680             << ListChange::remove(1, 1)
6681             )
6682             << true << true << true << true;
6683
6684     // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6685     // even if a remove-displace transition is not present to re-animate them
6686     QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6687             << ListChange::insert(0, 1)
6688             << ListChange::remove(2, 1)
6689             )
6690             << true << true << false << false;
6691
6692     // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6693     // remove has changed the position of where it will move to
6694     QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6695             << ListChange::setContentY(-10.0)
6696             << ListChange::insert(0, 1)
6697             << ListChange::insert(0, 1)
6698             << ListChange::remove(2, 1)
6699             )
6700             << true << true << false << false;
6701 }
6702
6703 void tst_QQuickListView::multipleDisplaced()
6704 {
6705     // multiple move() operations should only restart displace transitions for items that
6706     // moved from previously set positions, and not those that have moved from their current
6707     // item positions (which may e.g. still be changing from easing bounces in the last transition)
6708
6709     QmlListModel model;
6710     for (int i = 0; i < 30; i++)
6711         model.addItem("Original item" + QString::number(i), "");
6712
6713     QQuickView *canvas = getView();
6714     QQmlContext *ctxt = canvas->rootContext();
6715     ctxt->setContextProperty("testModel", &model);
6716     ctxt->setContextProperty("testObject", new TestObject(canvas));
6717     canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6718     canvas->show();
6719     QTest::qWaitForWindowShown(canvas);
6720
6721     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6722     QTRY_VERIFY(listview != 0);
6723     QQuickItem *contentItem = listview->contentItem();
6724     QVERIFY(contentItem != 0);
6725     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6726
6727     model.moveItems(12, 8, 1);
6728     QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6729     model.moveItems(8, 3, 1);
6730     QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6731
6732     QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6733     foreach (const QString &name, transitionsStarted.keys()) {
6734         QVERIFY2(transitionsStarted[name] == 1,
6735                  QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6736     }
6737
6738     // verify all items moved to the correct final positions
6739     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6740     for (int i=0; i < model.count() && i < items.count(); ++i) {
6741         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6742         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6743         QTRY_COMPARE(item->x(), 0.0);
6744         QTRY_COMPARE(item->y(), i*20.0);
6745         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6746         QVERIFY(name != 0);
6747         QTRY_COMPARE(name->text(), model.name(i));
6748     }
6749
6750     releaseView(canvas);
6751 }
6752
6753 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6754 {
6755     QList<int> ret;
6756     bool ok = true;
6757     for (int i=0; i<list.count(); i++) {
6758         ret << list[i].toInt(&ok);
6759         if (!ok)
6760             qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6761     }
6762
6763     return ret;
6764 }
6765
6766 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6767 {
6768     for (int i=0; i<indexLists.count(); i++) {
6769         QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6770         if (current != expectedIndexes.toSet())
6771             qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6772         QCOMPARE(current, expectedIndexes.toSet());
6773     }
6774 }
6775
6776 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6777 {
6778     for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6779         QVERIFY(it.value().type() == QVariant::Int);
6780         QString name = it.key();
6781         int itemIndex = it.value().toInt();
6782         QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6783         if (model.name(itemIndex) != name)
6784             qDebug() << itemIndex;
6785         QCOMPARE(model.name(itemIndex), name);
6786     }
6787     QCOMPARE(items.count(), expectedIndexes.count());
6788 }
6789
6790 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6791 {
6792     for (int i=0; i<itemLists.count(); i++) {
6793         QVERIFY(itemLists[i].type() == QVariant::List);
6794         QVariantList current = itemLists[i].toList();
6795         for (int j=0; j<current.count(); j++) {
6796             QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6797             QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6798             QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6799         }
6800         QCOMPARE(current.count(), expectedItems.count());
6801     }
6802 }
6803
6804 void tst_QQuickListView::flickBeyondBounds()
6805 {
6806     QQuickView *canvas = createView();
6807
6808     canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6809     canvas->show();
6810     qApp->processEvents();
6811
6812     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6813     QTRY_VERIFY(listview != 0);
6814
6815     QQuickItem *contentItem = listview->contentItem();
6816     QTRY_VERIFY(contentItem != 0);
6817     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6818
6819     // Flick view up beyond bounds
6820     flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6821     QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6822
6823     // We're really testing that we don't get stuck in a loop,
6824     // but also confirm items positioned correctly.
6825     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6826     for (int i = 0; i < 2; ++i) {
6827         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6828         if (!item) qWarning() << "Item" << i << "not found";
6829         QTRY_VERIFY(item);
6830         QTRY_VERIFY(item->y() == i*45);
6831     }
6832
6833     delete canvas;
6834 }
6835
6836 void tst_QQuickListView::destroyItemOnCreation()
6837 {
6838     QmlListModel model;
6839     QQuickView *canvas = createView();
6840     canvas->rootContext()->setContextProperty("testModel", &model);
6841
6842     canvas->setSource(testFileUrl("destroyItemOnCreation.qml"));
6843     canvas->show();
6844     qApp->processEvents();
6845
6846     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6847     QVERIFY(listview != 0);
6848
6849     QQuickItem *contentItem = listview->contentItem();
6850     QVERIFY(contentItem != 0);
6851     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6852
6853     QCOMPARE(canvas->rootObject()->property("createdIndex").toInt(), -1);
6854     model.addItem("new item", "");
6855     QTRY_COMPARE(canvas->rootObject()->property("createdIndex").toInt(), 0);
6856
6857     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6858     QCOMPARE(model.count(), 0);
6859
6860     delete canvas;
6861 }
6862
6863 void tst_QQuickListView::parentBinding()
6864 {
6865     QQuickView *canvas = createView();
6866
6867     m_errorCount = 0;
6868     QtMsgHandler old = qInstallMsgHandler(errorMsgHandler);
6869
6870     canvas->setSource(testFileUrl("parentBinding.qml"));
6871     canvas->show();
6872     qApp->processEvents();
6873
6874     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
6875     QVERIFY(listview != 0);
6876
6877     QQuickItem *contentItem = listview->contentItem();
6878     QVERIFY(contentItem != 0);
6879     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6880
6881     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6882     QVERIFY(item);
6883     QCOMPARE(item->width(), listview->width());
6884     QCOMPARE(item->height(), listview->height()/12);
6885
6886     // there should be no transient binding error
6887     QVERIFY(!m_errorCount);
6888
6889     qInstallMsgHandler(old);
6890
6891     delete canvas;
6892 }
6893
6894 QTEST_MAIN(tst_QQuickListView)
6895
6896 #include "tst_qquicklistview.moc"
6897
6898