e0c923e8905bf4cd9da4dfba1dca3f21bf373bec
[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     QTest::qWaitForWindowExposed(canvas);
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     // delegate size increase should push section footer down
2336     listview->setContentY(70);
2337     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2338     QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_3")); // section footer
2339     QTRY_COMPARE(bottomItem->y(), 370.);
2340     QQuickItem *inlineSection = findVisibleChild(contentItem, "sect_new");
2341     item = findItem<QQuickItem>(contentItem, "wrapper", 13);
2342     QVERIFY(item);
2343     item->setHeight(40.);
2344     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2345     QTRY_COMPARE(bottomItem->y(), 380.);
2346     QCOMPARE(inlineSection->y(), 360.);
2347     item->setHeight(20.);
2348
2349     // Turn sticky footer off
2350     listview->setContentY(20);
2351     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2352     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2353     QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2354     QCOMPARE(item->y(), 340.);
2355
2356     // Turn sticky header off
2357     listview->setContentY(30);
2358     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2359     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2360     QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2361     QCOMPARE(item->y(), 0.);
2362
2363     // if an empty model is set the header/footer should be cleaned up
2364     canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2365     QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2366     QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2367     QmlListModel model1;
2368     ctxt->setContextProperty("testModel", &model1);
2369     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2370     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2371
2372     // clear model - header/footer should be cleaned up
2373     ctxt->setContextProperty("testModel", &model);
2374     QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2375     QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2376     model.clear();
2377     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2378     QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2379
2380     delete canvas;
2381 }
2382
2383 void tst_QQuickListView::sectionPropertyChange()
2384 {
2385     QQuickView *canvas = createView();
2386
2387     canvas->setSource(testFileUrl("sectionpropertychange.qml"));
2388     canvas->show();
2389     qApp->processEvents();
2390
2391     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2392     QTRY_VERIFY(listview != 0);
2393
2394     QQuickItem *contentItem = listview->contentItem();
2395     QTRY_VERIFY(contentItem != 0);
2396
2397     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2398
2399     // Confirm items positioned correctly
2400     for (int i = 0; i < 2; ++i) {
2401         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2402         QTRY_VERIFY(item);
2403         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2404     }
2405
2406     QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2407     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2408
2409     // Confirm items positioned correctly
2410     for (int i = 0; i < 2; ++i) {
2411         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2412         QTRY_VERIFY(item);
2413         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2414     }
2415
2416     QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2417     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2418
2419     // Confirm items positioned correctly
2420     for (int i = 0; i < 2; ++i) {
2421         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2422         QTRY_VERIFY(item);
2423         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2424     }
2425
2426     QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2427     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2428
2429     // Confirm items positioned correctly
2430     for (int i = 0; i < 2; ++i) {
2431         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2432         QTRY_VERIFY(item);
2433         QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2434     }
2435
2436     QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2437     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2438
2439     // Confirm items positioned correctly
2440     for (int i = 0; i < 2; ++i) {
2441         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2442         QTRY_VERIFY(item);
2443         QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2444     }
2445
2446     delete canvas;
2447 }
2448
2449 void tst_QQuickListView::sectionDelegateChange()
2450 {
2451     QQuickView *canvas = createView();
2452
2453     canvas->setSource(testFileUrl("sectiondelegatechange.qml"));
2454     canvas->show();
2455     qApp->processEvents();
2456
2457     QQuickListView *listview = qobject_cast<QQuickListView *>(canvas->rootObject());
2458     QVERIFY(listview != 0);
2459
2460     QQuickItem *contentItem = listview->contentItem();
2461     QVERIFY(contentItem != 0);
2462
2463     QQUICK_VERIFY_POLISH(listview);
2464
2465     QVERIFY(findItems<QQuickItem>(contentItem, "section1").count() > 0);
2466     QCOMPARE(findItems<QQuickItem>(contentItem, "section2").count(), 0);
2467
2468     for (int i = 0; i < 3; ++i) {
2469         QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2470         QTRY_VERIFY(item);
2471         QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2472     }
2473
2474     QMetaObject::invokeMethod(canvas->rootObject(), "switchDelegates");
2475     QQUICK_VERIFY_POLISH(listview);
2476
2477     QCOMPARE(findItems<QQuickItem>(contentItem, "section1").count(), 0);
2478     QVERIFY(findItems<QQuickItem>(contentItem, "section2").count() > 0);
2479
2480     for (int i = 0; i < 3; ++i) {
2481         QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2482         QVERIFY(item);
2483         QTRY_COMPARE(item->y(), qreal(50. + i*75.));
2484     }
2485
2486     delete canvas;
2487 }
2488
2489 void tst_QQuickListView::currentIndex_delayedItemCreation()
2490 {
2491     QFETCH(bool, setCurrentToZero);
2492
2493     QQuickView *canvas = getView();
2494
2495     // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2496     // (since the currentItem will have changed and that shares the same index)
2497     canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2498
2499     canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2500     qApp->processEvents();
2501
2502     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2503     QTRY_VERIFY(listview != 0);
2504     QQuickItem *contentItem = listview->contentItem();
2505     QTRY_VERIFY(contentItem != 0);
2506
2507     QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2508     QCOMPARE(listview->currentIndex(), 0);
2509     QTRY_COMPARE(spy.count(), 1);
2510
2511     releaseView(canvas);
2512 }
2513
2514 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2515 {
2516     QTest::addColumn<bool>("setCurrentToZero");
2517
2518     QTest::newRow("set to 0") << true;
2519     QTest::newRow("don't set to 0") << false;
2520 }
2521
2522 void tst_QQuickListView::currentIndex()
2523 {
2524     QmlListModel model;
2525     for (int i = 0; i < 30; i++)
2526         model.addItem("Item" + QString::number(i), QString::number(i));
2527
2528     QQuickView *canvas = new QQuickView(0);
2529     canvas->setGeometry(0,0,240,320);
2530
2531     QQmlContext *ctxt = canvas->rootContext();
2532     ctxt->setContextProperty("testModel", &model);
2533     ctxt->setContextProperty("testWrap", QVariant(false));
2534
2535     QString filename(testFile("listview-initCurrent.qml"));
2536     canvas->setSource(QUrl::fromLocalFile(filename));
2537     canvas->show();
2538     qApp->processEvents();
2539
2540     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2541     QTRY_VERIFY(listview != 0);
2542     QQuickItem *contentItem = listview->contentItem();
2543     QTRY_VERIFY(contentItem != 0);
2544     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2545
2546     // current item should be 20th item at startup
2547     // and current item should be in view
2548     QCOMPARE(listview->currentIndex(), 20);
2549     QCOMPARE(listview->contentY(), 100.0);
2550     QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2551     QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2552
2553     listview->setCurrentIndex(0);
2554     QCOMPARE(listview->currentIndex(), 0);
2555     // confirm that the velocity is updated
2556     QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2557
2558     // footer should become visible if it is out of view, and then current index is set to count-1
2559     canvas->rootObject()->setProperty("showFooter", true);
2560     QTRY_VERIFY(listview->footerItem());
2561     listview->setCurrentIndex(model.count()-2);
2562     QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2563     listview->setCurrentIndex(model.count()-1);
2564     QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2565     canvas->rootObject()->setProperty("showFooter", false);
2566
2567     // header should become visible if it is out of view, and then current index is set to 0
2568     canvas->rootObject()->setProperty("showHeader", true);
2569     QTRY_VERIFY(listview->headerItem());
2570     listview->setCurrentIndex(1);
2571     QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2572     listview->setCurrentIndex(0);
2573     QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2574     canvas->rootObject()->setProperty("showHeader", false);
2575
2576     // turn off auto highlight
2577     listview->setHighlightFollowsCurrentItem(false);
2578     QVERIFY(listview->highlightFollowsCurrentItem() == false);
2579
2580     QVERIFY(listview->highlightItem());
2581     qreal hlPos = listview->highlightItem()->y();
2582
2583     listview->setCurrentIndex(4);
2584     QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2585
2586     // insert item before currentIndex
2587     listview->setCurrentIndex(28);
2588     model.insertItem(0, "Foo", "1111");
2589     QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2590
2591     // check removing highlight by setting currentIndex to -1;
2592     listview->setCurrentIndex(-1);
2593
2594     QCOMPARE(listview->currentIndex(), -1);
2595     QVERIFY(!listview->highlightItem());
2596     QVERIFY(!listview->currentItem());
2597
2598     // moving currentItem out of view should make it invisible
2599     listview->setCurrentIndex(0);
2600     QTRY_VERIFY(delegateVisible(listview->currentItem()));
2601     listview->setContentY(200);
2602     QTRY_VERIFY(!delegateVisible(listview->currentItem()));
2603
2604     delete canvas;
2605 }
2606
2607 void tst_QQuickListView::noCurrentIndex()
2608 {
2609     QmlListModel model;
2610     for (int i = 0; i < 30; i++)
2611         model.addItem("Item" + QString::number(i), QString::number(i));
2612
2613     QQuickView *canvas = new QQuickView(0);
2614     canvas->setGeometry(0,0,240,320);
2615
2616     QQmlContext *ctxt = canvas->rootContext();
2617     ctxt->setContextProperty("testModel", &model);
2618
2619     QString filename(testFile("listview-noCurrent.qml"));
2620     canvas->setSource(QUrl::fromLocalFile(filename));
2621     canvas->show();
2622     qApp->processEvents();
2623
2624     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2625     QTRY_VERIFY(listview != 0);
2626     QQuickItem *contentItem = listview->contentItem();
2627     QTRY_VERIFY(contentItem != 0);
2628     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2629
2630     // current index should be -1 at startup
2631     // and we should not have a currentItem or highlightItem
2632     QCOMPARE(listview->currentIndex(), -1);
2633     QCOMPARE(listview->contentY(), 0.0);
2634     QVERIFY(!listview->highlightItem());
2635     QVERIFY(!listview->currentItem());
2636
2637     listview->setCurrentIndex(2);
2638     QCOMPARE(listview->currentIndex(), 2);
2639     QVERIFY(listview->highlightItem());
2640     QVERIFY(listview->currentItem());
2641
2642     delete canvas;
2643 }
2644
2645 void tst_QQuickListView::keyNavigation()
2646 {
2647     QFETCH(QQuickListView::Orientation, orientation);
2648     QFETCH(Qt::LayoutDirection, layoutDirection);
2649     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
2650     QFETCH(Qt::Key, forwardsKey);
2651     QFETCH(Qt::Key, backwardsKey);
2652     QFETCH(QPointF, contentPosAtFirstItem);
2653     QFETCH(QPointF, contentPosAtLastItem);
2654
2655     QmlListModel model;
2656     for (int i = 0; i < 30; i++)
2657         model.addItem("Item" + QString::number(i), "");
2658
2659     QQuickView *canvas = getView();
2660     TestObject *testObject = new TestObject;
2661     canvas->rootContext()->setContextProperty("testModel", &model);
2662     canvas->rootContext()->setContextProperty("testObject", testObject);
2663     canvas->setSource(testFileUrl("listviewtest.qml"));
2664     canvas->show();
2665     QTest::qWaitForWindowActive(canvas);
2666
2667     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2668     QTRY_VERIFY(listview != 0);
2669
2670     listview->setOrientation(orientation);
2671     listview->setLayoutDirection(layoutDirection);
2672     listview->setVerticalLayoutDirection(verticalLayoutDirection);
2673     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2674
2675     canvas->requestActivateWindow();
2676     QTest::qWaitForWindowActive(canvas);
2677     QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2678     QCOMPARE(listview->currentIndex(), 0);
2679
2680     QTest::keyClick(canvas, forwardsKey);
2681     QCOMPARE(listview->currentIndex(), 1);
2682
2683     QTest::keyClick(canvas, backwardsKey);
2684     QCOMPARE(listview->currentIndex(), 0);
2685
2686     // hold down a key to go forwards
2687     for (int i=0; i<model.count()-1; i++) {
2688         QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
2689         QTRY_COMPARE(listview->currentIndex(), i+1);
2690     }
2691     QTest::keyRelease(canvas, forwardsKey);
2692     QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2693     QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2694     QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2695
2696     // hold down a key to go backwards
2697     for (int i=model.count()-1; i > 0; i--) {
2698         QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2699         QTRY_COMPARE(listview->currentIndex(), i-1);
2700     }
2701     QTest::keyRelease(canvas, backwardsKey);
2702     QTRY_COMPARE(listview->currentIndex(), 0);
2703     QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2704     QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2705
2706     // no wrap
2707     QVERIFY(!listview->isWrapEnabled());
2708     listview->incrementCurrentIndex();
2709     QCOMPARE(listview->currentIndex(), 1);
2710     listview->decrementCurrentIndex();
2711     QCOMPARE(listview->currentIndex(), 0);
2712
2713     listview->decrementCurrentIndex();
2714     QCOMPARE(listview->currentIndex(), 0);
2715
2716     // with wrap
2717     listview->setWrapEnabled(true);
2718     QVERIFY(listview->isWrapEnabled());
2719
2720     listview->decrementCurrentIndex();
2721     QCOMPARE(listview->currentIndex(), model.count()-1);
2722     QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2723     QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2724
2725     listview->incrementCurrentIndex();
2726     QCOMPARE(listview->currentIndex(), 0);
2727     QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2728     QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2729
2730     releaseView(canvas);
2731     delete testObject;
2732 }
2733
2734 void tst_QQuickListView::keyNavigation_data()
2735 {
2736     QTest::addColumn<QQuickListView::Orientation>("orientation");
2737     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2738     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2739     QTest::addColumn<Qt::Key>("forwardsKey");
2740     QTest::addColumn<Qt::Key>("backwardsKey");
2741     QTest::addColumn<QPointF>("contentPosAtFirstItem");
2742     QTest::addColumn<QPointF>("contentPosAtLastItem");
2743
2744     QTest::newRow("Vertical, TopToBottom")
2745             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2746             << Qt::Key_Down << Qt::Key_Up
2747             << QPointF(0, 0)
2748             << QPointF(0, 30*20 - 320);
2749
2750     QTest::newRow("Vertical, BottomToTop")
2751             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2752             << Qt::Key_Up << Qt::Key_Down
2753             << QPointF(0, -320)
2754             << QPointF(0, -(30 * 20));
2755
2756     QTest::newRow("Horizontal, LeftToRight")
2757             << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2758             << Qt::Key_Right << Qt::Key_Left
2759             << QPointF(0, 0)
2760             << QPointF(30*240 - 240, 0);
2761
2762     QTest::newRow("Horizontal, RightToLeft")
2763             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2764             << Qt::Key_Left << Qt::Key_Right
2765             << QPointF(-240, 0)
2766             << QPointF(-(30 * 240), 0);
2767 }
2768
2769 void tst_QQuickListView::itemList()
2770 {
2771     QQuickView *canvas = createView();
2772     canvas->setSource(testFileUrl("itemlist.qml"));
2773     canvas->show();
2774     qApp->processEvents();
2775
2776     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2777     QTRY_VERIFY(listview != 0);
2778
2779     QQuickItem *contentItem = listview->contentItem();
2780     QTRY_VERIFY(contentItem != 0);
2781
2782     QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2783     QTRY_VERIFY(model != 0);
2784
2785     QTRY_VERIFY(model->count() == 3);
2786     QTRY_COMPARE(listview->currentIndex(), 0);
2787
2788     QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2789     QTRY_VERIFY(item);
2790     QTRY_COMPARE(item->x(), 0.0);
2791     QCOMPARE(item->height(), listview->height());
2792
2793     QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2794     QTRY_VERIFY(text);
2795     QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2796
2797     listview->setCurrentIndex(2);
2798
2799     item = findItem<QQuickItem>(contentItem, "item3");
2800     QTRY_VERIFY(item);
2801     QTRY_COMPARE(item->x(), 480.0);
2802
2803     text = findItem<QQuickText>(contentItem, "text3");
2804     QTRY_VERIFY(text);
2805     QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2806
2807     delete canvas;
2808 }
2809
2810 void tst_QQuickListView::itemListFlicker()
2811 {
2812     QQuickView *canvas = createView();
2813     canvas->setSource(testFileUrl("itemlist-flicker.qml"));
2814     canvas->show();
2815     qApp->processEvents();
2816
2817     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2818     QTRY_VERIFY(listview != 0);
2819
2820     QQuickItem *contentItem = listview->contentItem();
2821     QTRY_VERIFY(contentItem != 0);
2822
2823     QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2824     QTRY_VERIFY(model != 0);
2825
2826     QTRY_VERIFY(model->count() == 3);
2827     QTRY_COMPARE(listview->currentIndex(), 0);
2828
2829     QQuickItem *item;
2830
2831     QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2832     QVERIFY(delegateVisible(item));
2833     QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2834     QVERIFY(delegateVisible(item));
2835     QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2836     QVERIFY(delegateVisible(item));
2837
2838     listview->setCurrentIndex(1);
2839
2840     QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2841     QVERIFY(delegateVisible(item));
2842     QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2843     QVERIFY(delegateVisible(item));
2844     QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2845     QVERIFY(delegateVisible(item));
2846
2847     listview->setCurrentIndex(2);
2848
2849     QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2850     QVERIFY(delegateVisible(item));
2851     QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2852     QVERIFY(delegateVisible(item));
2853     QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2854     QVERIFY(delegateVisible(item));
2855
2856     delete canvas;
2857 }
2858
2859 void tst_QQuickListView::cacheBuffer()
2860 {
2861     QQuickView *canvas = createView();
2862
2863     QmlListModel model;
2864     for (int i = 0; i < 90; i++)
2865         model.addItem("Item" + QString::number(i), "");
2866
2867     QQmlContext *ctxt = canvas->rootContext();
2868     ctxt->setContextProperty("testModel", &model);
2869
2870     TestObject *testObject = new TestObject;
2871     ctxt->setContextProperty("testObject", testObject);
2872
2873     canvas->setSource(testFileUrl("listviewtest.qml"));
2874     canvas->show();
2875     qApp->processEvents();
2876
2877     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2878     QTRY_VERIFY(listview != 0);
2879
2880     QQuickItem *contentItem = listview->contentItem();
2881     QTRY_VERIFY(contentItem != 0);
2882     QTRY_VERIFY(listview->delegate() != 0);
2883     QTRY_VERIFY(listview->model() != 0);
2884     QTRY_VERIFY(listview->highlight() != 0);
2885
2886     // Confirm items positioned correctly
2887     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2888     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2889         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2890         if (!item) qWarning() << "Item" << i << "not found";
2891         QTRY_VERIFY(item);
2892         QTRY_VERIFY(item->y() == i*20);
2893     }
2894
2895     QQmlIncubationController controller;
2896     canvas->engine()->setIncubationController(&controller);
2897
2898     testObject->setCacheBuffer(200);
2899     QTRY_VERIFY(listview->cacheBuffer() == 200);
2900
2901     // items will be created one at a time
2902     for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2903         QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2904         QQuickItem *item = 0;
2905         while (!item) {
2906             bool b = false;
2907             controller.incubateWhile(&b);
2908             item = findItem<QQuickItem>(listview, "wrapper", i);
2909         }
2910     }
2911
2912     {
2913         bool b = true;
2914         controller.incubateWhile(&b);
2915     }
2916
2917     int newItemCount = 0;
2918     newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2919
2920     // Confirm items positioned correctly
2921     for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2922         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2923         if (!item) qWarning() << "Item" << i << "not found";
2924         QTRY_VERIFY(item);
2925         QTRY_VERIFY(item->y() == i*20);
2926     }
2927
2928     // move view and confirm items in view are visible immediately and outside are created async
2929     listview->setContentY(300);
2930
2931     for (int i = 15; i < 32; ++i) {
2932         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2933         if (!item) qWarning() << "Item" << i << "not found";
2934         QVERIFY(item);
2935         QVERIFY(item->y() == i*20);
2936     }
2937
2938     QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2939
2940     // ensure buffered items are created
2941     for (int i = 32; i < qMin(41,model.count()); ++i) {
2942         QQuickItem *item = 0;
2943         while (!item) {
2944             qGuiApp->processEvents(); // allow refill to happen
2945             bool b = false;
2946             controller.incubateWhile(&b);
2947             item = findItem<QQuickItem>(listview, "wrapper", i);
2948         }
2949     }
2950
2951     {
2952         bool b = true;
2953         controller.incubateWhile(&b);
2954     }
2955
2956     delete canvas;
2957     delete testObject;
2958 }
2959
2960 void tst_QQuickListView::positionViewAtIndex()
2961 {
2962     QQuickView *canvas = createView();
2963
2964     QmlListModel model;
2965     for (int i = 0; i < 40; i++)
2966         model.addItem("Item" + QString::number(i), "");
2967
2968     QQmlContext *ctxt = canvas->rootContext();
2969     ctxt->setContextProperty("testModel", &model);
2970
2971     TestObject *testObject = new TestObject;
2972     ctxt->setContextProperty("testObject", testObject);
2973     canvas->show();
2974     canvas->setSource(testFileUrl("listviewtest.qml"));
2975     qApp->processEvents();
2976
2977     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2978     QTRY_VERIFY(listview != 0);
2979     QQuickItem *contentItem = listview->contentItem();
2980     QTRY_VERIFY(contentItem != 0);
2981     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2982
2983     // Confirm items positioned correctly
2984     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2985     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2986         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2987         if (!item) qWarning() << "Item" << i << "not found";
2988         QTRY_VERIFY(item);
2989         QTRY_COMPARE(item->y(), i*20.);
2990     }
2991
2992     // Position on a currently visible item
2993     listview->positionViewAtIndex(3, QQuickListView::Beginning);
2994     QTRY_COMPARE(listview->contentY(), 60.);
2995
2996     // Confirm items positioned correctly
2997     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2998     for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2999         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3000         if (!item) qWarning() << "Item" << i << "not found";
3001         QTRY_VERIFY(item);
3002         QTRY_COMPARE(item->y(), i*20.);
3003     }
3004
3005     // Position on an item beyond the visible items
3006     listview->positionViewAtIndex(22, QQuickListView::Beginning);
3007     QTRY_COMPARE(listview->contentY(), 440.);
3008
3009     // Confirm items positioned correctly
3010     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3011     for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
3012         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3013         if (!item) qWarning() << "Item" << i << "not found";
3014         QTRY_VERIFY(item);
3015         QTRY_COMPARE(item->y(), i*20.);
3016     }
3017
3018     // Position on an item that would leave empty space if positioned at the top
3019     listview->positionViewAtIndex(28, QQuickListView::Beginning);
3020     QTRY_COMPARE(listview->contentY(), 480.);
3021
3022     // Confirm items positioned correctly
3023     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3024     for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
3025         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3026         if (!item) qWarning() << "Item" << i << "not found";
3027         QTRY_VERIFY(item);
3028         QTRY_COMPARE(item->y(), i*20.);
3029     }
3030
3031     // Position at the beginning again
3032     listview->positionViewAtIndex(0, QQuickListView::Beginning);
3033     QTRY_COMPARE(listview->contentY(), 0.);
3034
3035     // Confirm items positioned correctly
3036     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3037     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
3038         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3039         if (!item) qWarning() << "Item" << i << "not found";
3040         QTRY_VERIFY(item);
3041         QTRY_COMPARE(item->y(), i*20.);
3042     }
3043
3044     // Position at End using last index
3045     listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
3046     QTRY_COMPARE(listview->contentY(), 480.);
3047
3048     // Confirm items positioned correctly
3049     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3050     for (int i = 24; i < model.count(); ++i) {
3051         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3052         if (!item) qWarning() << "Item" << i << "not found";
3053         QTRY_VERIFY(item);
3054         QTRY_COMPARE(item->y(), i*20.);
3055     }
3056
3057     // Position at End
3058     listview->positionViewAtIndex(20, QQuickListView::End);
3059     QTRY_COMPARE(listview->contentY(), 100.);
3060
3061     // Position in Center
3062     listview->positionViewAtIndex(15, QQuickListView::Center);
3063     QTRY_COMPARE(listview->contentY(), 150.);
3064
3065     // Ensure at least partially visible
3066     listview->positionViewAtIndex(15, QQuickListView::Visible);
3067     QTRY_COMPARE(listview->contentY(), 150.);
3068
3069     listview->setContentY(302);
3070     listview->positionViewAtIndex(15, QQuickListView::Visible);
3071     QTRY_COMPARE(listview->contentY(), 302.);
3072
3073     listview->setContentY(320);
3074     listview->positionViewAtIndex(15, QQuickListView::Visible);
3075     QTRY_COMPARE(listview->contentY(), 300.);
3076
3077     listview->setContentY(85);
3078     listview->positionViewAtIndex(20, QQuickListView::Visible);
3079     QTRY_COMPARE(listview->contentY(), 85.);
3080
3081     listview->setContentY(75);
3082     listview->positionViewAtIndex(20, QQuickListView::Visible);
3083     QTRY_COMPARE(listview->contentY(), 100.);
3084
3085     // Ensure completely visible
3086     listview->setContentY(120);
3087     listview->positionViewAtIndex(20, QQuickListView::Contain);
3088     QTRY_COMPARE(listview->contentY(), 120.);
3089
3090     listview->setContentY(302);
3091     listview->positionViewAtIndex(15, QQuickListView::Contain);
3092     QTRY_COMPARE(listview->contentY(), 300.);
3093
3094     listview->setContentY(85);
3095     listview->positionViewAtIndex(20, QQuickListView::Contain);
3096     QTRY_COMPARE(listview->contentY(), 100.);
3097
3098     // positionAtBeginnging
3099     listview->positionViewAtBeginning();
3100     QTRY_COMPARE(listview->contentY(), 0.);
3101
3102     listview->setContentY(80);
3103     canvas->rootObject()->setProperty("showHeader", true);
3104     listview->positionViewAtBeginning();
3105     QTRY_COMPARE(listview->contentY(), -30.);
3106
3107     // positionAtEnd
3108     listview->positionViewAtEnd();
3109     QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
3110
3111     listview->setContentY(80);
3112     canvas->rootObject()->setProperty("showFooter", true);
3113     listview->positionViewAtEnd();
3114     QTRY_COMPARE(listview->contentY(), 510.);
3115
3116     // set current item to outside visible view, position at beginning
3117     // and ensure highlight moves to current item
3118     listview->setCurrentIndex(1);
3119     listview->positionViewAtBeginning();
3120     QTRY_COMPARE(listview->contentY(), -30.);
3121     QVERIFY(listview->highlightItem());
3122     QCOMPARE(listview->highlightItem()->y(), 20.);
3123
3124     delete canvas;
3125     delete testObject;
3126 }
3127
3128 void tst_QQuickListView::resetModel()
3129 {
3130     QQuickView *canvas = createView();
3131
3132     QStringList strings;
3133     strings << "one" << "two" << "three";
3134     QStringListModel model(strings);
3135
3136     QQmlContext *ctxt = canvas->rootContext();
3137     ctxt->setContextProperty("testModel", &model);
3138
3139     canvas->setSource(testFileUrl("displaylist.qml"));
3140     canvas->show();
3141     qApp->processEvents();
3142
3143     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3144     QTRY_VERIFY(listview != 0);
3145     QQuickItem *contentItem = listview->contentItem();
3146     QTRY_VERIFY(contentItem != 0);
3147     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3148
3149     QTRY_COMPARE(listview->count(), model.rowCount());
3150
3151     for (int i = 0; i < model.rowCount(); ++i) {
3152         QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3153         QTRY_VERIFY(display != 0);
3154         QTRY_COMPARE(display->text(), strings.at(i));
3155     }
3156
3157     strings.clear();
3158     strings << "four" << "five" << "six" << "seven";
3159     model.setStringList(strings);
3160
3161     QTRY_COMPARE(listview->count(), model.rowCount());
3162
3163     for (int i = 0; i < model.rowCount(); ++i) {
3164         QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3165         QTRY_VERIFY(display != 0);
3166         QTRY_COMPARE(display->text(), strings.at(i));
3167     }
3168
3169     delete canvas;
3170 }
3171
3172 void tst_QQuickListView::propertyChanges()
3173 {
3174     QQuickView *canvas = createView();
3175     QTRY_VERIFY(canvas);
3176     canvas->setSource(testFileUrl("propertychangestest.qml"));
3177
3178     QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3179     QTRY_VERIFY(listView);
3180
3181     QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3182     QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3183     QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3184     QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3185     QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3186     QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3187     QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3188
3189     QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3190     QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3191     QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3192     QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3193     QTRY_COMPARE(listView->isWrapEnabled(), true);
3194     QTRY_COMPARE(listView->cacheBuffer(), 10);
3195     QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3196
3197     listView->setHighlightFollowsCurrentItem(false);
3198     listView->setPreferredHighlightBegin(1.0);
3199     listView->setPreferredHighlightEnd(1.0);
3200     listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3201     listView->setWrapEnabled(false);
3202     listView->setCacheBuffer(3);
3203     listView->setSnapMode(QQuickListView::SnapOneItem);
3204
3205     QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3206     QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3207     QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3208     QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3209     QTRY_COMPARE(listView->isWrapEnabled(), false);
3210     QTRY_COMPARE(listView->cacheBuffer(), 3);
3211     QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3212
3213     QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3214     QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3215     QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3216     QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3217     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3218     QTRY_COMPARE(cacheBufferSpy.count(),1);
3219     QTRY_COMPARE(snapModeSpy.count(),1);
3220
3221     listView->setHighlightFollowsCurrentItem(false);
3222     listView->setPreferredHighlightBegin(1.0);
3223     listView->setPreferredHighlightEnd(1.0);
3224     listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3225     listView->setWrapEnabled(false);
3226     listView->setCacheBuffer(3);
3227     listView->setSnapMode(QQuickListView::SnapOneItem);
3228
3229     QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3230     QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3231     QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3232     QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3233     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3234     QTRY_COMPARE(cacheBufferSpy.count(),1);
3235     QTRY_COMPARE(snapModeSpy.count(),1);
3236
3237     delete canvas;
3238 }
3239
3240 void tst_QQuickListView::componentChanges()
3241 {
3242     QQuickView *canvas = createView();
3243     QTRY_VERIFY(canvas);
3244     canvas->setSource(testFileUrl("propertychangestest.qml"));
3245
3246     QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3247     QTRY_VERIFY(listView);
3248
3249     QQmlComponent component(canvas->engine());
3250     component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3251
3252     QQmlComponent delegateComponent(canvas->engine());
3253     delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3254
3255     QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3256     QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3257     QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3258     QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3259
3260     listView->setHighlight(&component);
3261     listView->setHeader(&component);
3262     listView->setFooter(&component);
3263     listView->setDelegate(&delegateComponent);
3264
3265     QTRY_COMPARE(listView->highlight(), &component);
3266     QTRY_COMPARE(listView->header(), &component);
3267     QTRY_COMPARE(listView->footer(), &component);
3268     QTRY_COMPARE(listView->delegate(), &delegateComponent);
3269
3270     QTRY_COMPARE(highlightSpy.count(),1);
3271     QTRY_COMPARE(delegateSpy.count(),1);
3272     QTRY_COMPARE(headerSpy.count(),1);
3273     QTRY_COMPARE(footerSpy.count(),1);
3274
3275     listView->setHighlight(&component);
3276     listView->setHeader(&component);
3277     listView->setFooter(&component);
3278     listView->setDelegate(&delegateComponent);
3279
3280     QTRY_COMPARE(highlightSpy.count(),1);
3281     QTRY_COMPARE(delegateSpy.count(),1);
3282     QTRY_COMPARE(headerSpy.count(),1);
3283     QTRY_COMPARE(footerSpy.count(),1);
3284
3285     delete canvas;
3286 }
3287
3288 void tst_QQuickListView::modelChanges()
3289 {
3290     QQuickView *canvas = createView();
3291     QTRY_VERIFY(canvas);
3292     canvas->setSource(testFileUrl("propertychangestest.qml"));
3293
3294     QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3295     QTRY_VERIFY(listView);
3296
3297     QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
3298     QTRY_VERIFY(alternateModel);
3299     QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3300     QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3301
3302     listView->setModel(modelVariant);
3303     QTRY_COMPARE(listView->model(), modelVariant);
3304     QTRY_COMPARE(modelSpy.count(),1);
3305
3306     listView->setModel(modelVariant);
3307     QTRY_COMPARE(modelSpy.count(),1);
3308
3309     listView->setModel(QVariant());
3310     QTRY_COMPARE(modelSpy.count(),2);
3311
3312     delete canvas;
3313 }
3314
3315 void tst_QQuickListView::QTBUG_9791()
3316 {
3317     QQuickView *canvas = createView();
3318
3319     canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
3320     qApp->processEvents();
3321
3322     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3323     QTRY_VERIFY(listview != 0);
3324
3325     QQuickItem *contentItem = listview->contentItem();
3326     QTRY_VERIFY(contentItem != 0);
3327     QTRY_VERIFY(listview->delegate() != 0);
3328     QTRY_VERIFY(listview->model() != 0);
3329
3330     QMetaObject::invokeMethod(listview, "fillModel");
3331     qApp->processEvents();
3332
3333     // Confirm items positioned correctly
3334     int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3335     QCOMPARE(itemCount, 3);
3336
3337     for (int i = 0; i < itemCount; ++i) {
3338         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3339         if (!item) qWarning() << "Item" << i << "not found";
3340         QTRY_VERIFY(item);
3341         QTRY_COMPARE(item->x(), i*300.0);
3342     }
3343
3344     // check that view is positioned correctly
3345     QTRY_COMPARE(listview->contentX(), 590.0);
3346
3347     delete canvas;
3348 }
3349
3350 void tst_QQuickListView::manualHighlight()
3351 {
3352     QQuickView *canvas = new QQuickView(0);
3353     canvas->setGeometry(0,0,240,320);
3354
3355     QString filename(testFile("manual-highlight.qml"));
3356     canvas->setSource(QUrl::fromLocalFile(filename));
3357
3358     qApp->processEvents();
3359
3360     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3361     QTRY_VERIFY(listview != 0);
3362
3363     QQuickItem *contentItem = listview->contentItem();
3364     QTRY_VERIFY(contentItem != 0);
3365
3366     QTRY_COMPARE(listview->currentIndex(), 0);
3367     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3368     QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3369
3370     listview->setCurrentIndex(2);
3371
3372     QTRY_COMPARE(listview->currentIndex(), 2);
3373     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3374     QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3375
3376     // QTBUG-15972
3377     listview->positionViewAtIndex(3, QQuickListView::Contain);
3378
3379     QTRY_COMPARE(listview->currentIndex(), 2);
3380     QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3381     QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3382
3383     delete canvas;
3384 }
3385
3386 void tst_QQuickListView::QTBUG_11105()
3387 {
3388     QQuickView *canvas = createView();
3389
3390     QmlListModel model;
3391     for (int i = 0; i < 30; i++)
3392         model.addItem("Item" + QString::number(i), "");
3393
3394     QQmlContext *ctxt = canvas->rootContext();
3395     ctxt->setContextProperty("testModel", &model);
3396
3397     TestObject *testObject = new TestObject;
3398     ctxt->setContextProperty("testObject", testObject);
3399
3400     canvas->setSource(testFileUrl("listviewtest.qml"));
3401     canvas->show();
3402     qApp->processEvents();
3403
3404     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3405     QTRY_VERIFY(listview != 0);
3406     QQuickItem *contentItem = listview->contentItem();
3407     QTRY_VERIFY(contentItem != 0);
3408     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3409
3410     // Confirm items positioned correctly
3411     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3412     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3413         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3414         if (!item) qWarning() << "Item" << i << "not found";
3415         QTRY_VERIFY(item);
3416         QTRY_VERIFY(item->y() == i*20);
3417     }
3418
3419     listview->positionViewAtIndex(20, QQuickListView::Beginning);
3420     QCOMPARE(listview->contentY(), 280.);
3421
3422     QmlListModel model2;
3423     for (int i = 0; i < 5; i++)
3424         model2.addItem("Item" + QString::number(i), "");
3425
3426     ctxt->setContextProperty("testModel", &model2);
3427
3428     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3429     QCOMPARE(itemCount, 5);
3430
3431     delete canvas;
3432     delete testObject;
3433 }
3434
3435 void tst_QQuickListView::initialZValues()
3436 {
3437     QQuickView *canvas = createView();
3438     canvas->setSource(testFileUrl("initialZValues.qml"));
3439     qApp->processEvents();
3440
3441     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3442     QTRY_VERIFY(listview != 0);
3443     QQuickItem *contentItem = listview->contentItem();
3444     QTRY_VERIFY(contentItem != 0);
3445
3446     QVERIFY(listview->headerItem());
3447     QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3448
3449     QVERIFY(listview->footerItem());
3450     QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3451
3452     delete canvas;
3453 }
3454
3455 void tst_QQuickListView::header()
3456 {
3457     QFETCH(QQuickListView::Orientation, orientation);
3458     QFETCH(Qt::LayoutDirection, layoutDirection);
3459     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3460     QFETCH(QPointF, initialHeaderPos);
3461     QFETCH(QPointF, changedHeaderPos);
3462     QFETCH(QPointF, initialContentPos);
3463     QFETCH(QPointF, changedContentPos);
3464     QFETCH(QPointF, firstDelegatePos);
3465     QFETCH(QPointF, resizeContentPos);
3466
3467     QmlListModel model;
3468     for (int i = 0; i < 30; i++)
3469         model.addItem("Item" + QString::number(i), "");
3470
3471     QQuickView *canvas = getView();
3472     canvas->rootContext()->setContextProperty("testModel", &model);
3473     canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3474     canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3475     canvas->setSource(testFileUrl("header.qml"));
3476     canvas->show();
3477     qApp->processEvents();
3478
3479     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3480     QTRY_VERIFY(listview != 0);
3481     listview->setOrientation(orientation);
3482     listview->setLayoutDirection(layoutDirection);
3483     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3484     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3485
3486     QQuickItem *contentItem = listview->contentItem();
3487     QTRY_VERIFY(contentItem != 0);
3488
3489     QQuickText *header = 0;
3490     QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3491     QVERIFY(header == listview->headerItem());
3492
3493     QCOMPARE(header->width(), 100.);
3494     QCOMPARE(header->height(), 30.);
3495     QCOMPARE(header->pos(), initialHeaderPos);
3496     QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3497
3498     if (orientation == QQuickListView::Vertical)
3499         QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3500     else
3501         QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3502
3503     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3504     QVERIFY(item);
3505     QCOMPARE(item->pos(), firstDelegatePos);
3506
3507     model.clear();
3508     QTRY_COMPARE(listview->count(), model.count());
3509     QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3510     if (orientation == QQuickListView::Vertical)
3511         QCOMPARE(listview->contentHeight(), header->height());
3512     else
3513         QCOMPARE(listview->contentWidth(), header->width());
3514
3515     for (int i = 0; i < 30; i++)
3516         model.addItem("Item" + QString::number(i), "");
3517
3518     QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3519     QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3520
3521     QCOMPARE(headerItemSpy.count(), 1);
3522
3523     header = findItem<QQuickText>(contentItem, "header");
3524     QVERIFY(!header);
3525     header = findItem<QQuickText>(contentItem, "header2");
3526     QVERIFY(header);
3527
3528     QVERIFY(header == listview->headerItem());
3529
3530     QCOMPARE(header->pos(), changedHeaderPos);
3531     QCOMPARE(header->width(), 50.);
3532     QCOMPARE(header->height(), 20.);
3533     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3534
3535     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3536     QVERIFY(item);
3537     QCOMPARE(item->pos(), firstDelegatePos);
3538
3539     listview->positionViewAtBeginning();
3540     header->setHeight(10);
3541     header->setWidth(40);
3542     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3543
3544     releaseView(canvas);
3545
3546
3547     // QTBUG-21207 header should become visible if view resizes from initial empty size
3548
3549     canvas = getView();
3550     canvas->rootContext()->setContextProperty("testModel", &model);
3551     canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3552     canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3553     canvas->setSource(testFileUrl("header.qml"));
3554     canvas->show();
3555     qApp->processEvents();
3556
3557     listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3558     QTRY_VERIFY(listview != 0);
3559     listview->setOrientation(orientation);
3560     listview->setLayoutDirection(layoutDirection);
3561     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3562     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3563
3564     listview->setWidth(240);
3565     listview->setHeight(320);
3566     QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3567     QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3568
3569     releaseView(canvas);
3570 }
3571
3572 void tst_QQuickListView::header_data()
3573 {
3574     QTest::addColumn<QQuickListView::Orientation>("orientation");
3575     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3576     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3577     QTest::addColumn<QPointF>("initialHeaderPos");
3578     QTest::addColumn<QPointF>("changedHeaderPos");
3579     QTest::addColumn<QPointF>("initialContentPos");
3580     QTest::addColumn<QPointF>("changedContentPos");
3581     QTest::addColumn<QPointF>("firstDelegatePos");
3582     QTest::addColumn<QPointF>("resizeContentPos");
3583
3584     // header1 = 100 x 30
3585     // header2 = 50 x 20
3586     // delegates = 240 x 30
3587     // view width = 240
3588
3589     // header above items, top left
3590     QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3591         << QPointF(0, -30)
3592         << QPointF(0, -20)
3593         << QPointF(0, -30)
3594         << QPointF(0, -20)
3595         << QPointF(0, 0)
3596         << QPointF(0, -10);
3597
3598     // header above items, top right
3599     QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3600         << QPointF(0, -30)
3601         << QPointF(0, -20)
3602         << QPointF(0, -30)
3603         << QPointF(0, -20)
3604         << QPointF(0, 0)
3605         << QPointF(0, -10);
3606
3607     // header to left of items
3608     QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3609         << QPointF(-100, 0)
3610         << QPointF(-50, 0)
3611         << QPointF(-100, 0)
3612         << QPointF(-50, 0)
3613         << QPointF(0, 0)
3614         << QPointF(-40, 0);
3615
3616     // header to right of items
3617     QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3618         << QPointF(0, 0)
3619         << QPointF(0, 0)
3620         << QPointF(-240 + 100, 0)
3621         << QPointF(-240 + 50, 0)
3622         << QPointF(-240, 0)
3623         << QPointF(-240 + 40, 0);
3624
3625     // header below items
3626     QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3627         << QPointF(0, 0)
3628         << QPointF(0, 0)
3629         << QPointF(0, -320 + 30)
3630         << QPointF(0, -320 + 20)
3631         << QPointF(0, -30)
3632         << QPointF(0, -320 + 10);
3633 }
3634
3635 void tst_QQuickListView::header_delayItemCreation()
3636 {
3637     QQuickView *canvas = createView();
3638
3639     QmlListModel model;
3640
3641     canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3642     canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3643     qApp->processEvents();
3644
3645     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3646     QTRY_VERIFY(listview != 0);
3647
3648     QQuickItem *contentItem = listview->contentItem();
3649     QTRY_VERIFY(contentItem != 0);
3650
3651     QQuickText *header = findItem<QQuickText>(contentItem, "header");
3652     QVERIFY(header);
3653     QCOMPARE(header->y(), -header->height());
3654
3655     QCOMPARE(listview->contentY(), -header->height());
3656
3657     model.clear();
3658     QTRY_COMPARE(header->y(), -header->height());
3659
3660     delete canvas;
3661 }
3662
3663 void tst_QQuickListView::footer()
3664 {
3665     QFETCH(QQuickListView::Orientation, orientation);
3666     QFETCH(Qt::LayoutDirection, layoutDirection);
3667     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3668     QFETCH(QPointF, initialFooterPos);
3669     QFETCH(QPointF, firstDelegatePos);
3670     QFETCH(QPointF, initialContentPos);
3671     QFETCH(QPointF, changedFooterPos);
3672     QFETCH(QPointF, changedContentPos);
3673     QFETCH(QPointF, resizeContentPos);
3674
3675     QQuickView *canvas = getView();
3676
3677     QmlListModel model;
3678     for (int i = 0; i < 3; i++)
3679         model.addItem("Item" + QString::number(i), "");
3680
3681     QQmlContext *ctxt = canvas->rootContext();
3682     ctxt->setContextProperty("testModel", &model);
3683
3684     canvas->setSource(testFileUrl("footer.qml"));
3685     canvas->show();
3686     qApp->processEvents();
3687
3688     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3689     QTRY_VERIFY(listview != 0);
3690     listview->setOrientation(orientation);
3691     listview->setLayoutDirection(layoutDirection);
3692     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3693     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3694
3695     QQuickItem *contentItem = listview->contentItem();
3696     QTRY_VERIFY(contentItem != 0);
3697
3698     QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3699     QVERIFY(footer);
3700
3701     QVERIFY(footer == listview->footerItem());
3702
3703     QCOMPARE(footer->pos(), initialFooterPos);
3704     QCOMPARE(footer->width(), 100.);
3705     QCOMPARE(footer->height(), 30.);
3706     QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3707
3708     if (orientation == QQuickListView::Vertical)
3709         QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3710     else
3711         QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3712
3713     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3714     QVERIFY(item);
3715     QCOMPARE(item->pos(), firstDelegatePos);
3716
3717     // remove one item
3718     model.removeItem(1);
3719
3720     if (orientation == QQuickListView::Vertical) {
3721         QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3722                 initialFooterPos.y() - 20 : initialFooterPos.y() + 20);  // delegate width = 40
3723     } else {
3724         QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3725                 initialFooterPos.x() - 40 : initialFooterPos.x() + 40);  // delegate width = 40
3726     }
3727
3728     // remove all items
3729     model.clear();
3730     if (orientation == QQuickListView::Vertical)
3731         QTRY_COMPARE(listview->contentHeight(), footer->height());
3732     else
3733         QTRY_COMPARE(listview->contentWidth(), footer->width());
3734
3735     QPointF posWhenNoItems(0, 0);
3736     if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3737         posWhenNoItems.setX(-100);
3738     else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3739         posWhenNoItems.setY(-30);
3740     QTRY_COMPARE(footer->pos(), posWhenNoItems);
3741
3742     // if header is present, it's at a negative pos, so the footer should not move
3743     canvas->rootObject()->setProperty("showHeader", true);
3744     QTRY_COMPARE(footer->pos(), posWhenNoItems);
3745     canvas->rootObject()->setProperty("showHeader", false);
3746
3747     // add 30 items
3748     for (int i = 0; i < 30; i++)
3749         model.addItem("Item" + QString::number(i), "");
3750
3751     QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3752     QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3753
3754     QCOMPARE(footerItemSpy.count(), 1);
3755
3756     footer = findItem<QQuickText>(contentItem, "footer");
3757     QVERIFY(!footer);
3758     footer = findItem<QQuickText>(contentItem, "footer2");
3759     QVERIFY(footer);
3760
3761     QVERIFY(footer == listview->footerItem());
3762
3763     QCOMPARE(footer->pos(), changedFooterPos);
3764     QCOMPARE(footer->width(), 50.);
3765     QCOMPARE(footer->height(), 20.);
3766     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3767
3768     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3769     QVERIFY(item);
3770     QCOMPARE(item->pos(), firstDelegatePos);
3771
3772     listview->positionViewAtEnd();
3773     footer->setHeight(10);
3774     footer->setWidth(40);
3775     QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3776
3777     releaseView(canvas);
3778 }
3779
3780 void tst_QQuickListView::footer_data()
3781 {
3782     QTest::addColumn<QQuickListView::Orientation>("orientation");
3783     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3784     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3785     QTest::addColumn<QPointF>("initialFooterPos");
3786     QTest::addColumn<QPointF>("changedFooterPos");
3787     QTest::addColumn<QPointF>("initialContentPos");
3788     QTest::addColumn<QPointF>("changedContentPos");
3789     QTest::addColumn<QPointF>("firstDelegatePos");
3790     QTest::addColumn<QPointF>("resizeContentPos");
3791
3792     // footer1 = 100 x 30
3793     // footer2 = 50 x 20
3794     // delegates = 40 x 20
3795     // view width = 240
3796     // view height = 320
3797
3798     // footer below items, bottom left
3799     QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3800         << QPointF(0, 3 * 20)
3801         << QPointF(0, 30 * 20)  // added 30 items
3802         << QPointF(0, 0)
3803         << QPointF(0, 0)
3804         << QPointF(0, 0)
3805         << QPointF(0, 30 * 20 - 320 + 10);
3806
3807     // footer below items, bottom right
3808     QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3809         << QPointF(0, 3 * 20)
3810         << QPointF(0, 30 * 20)
3811         << QPointF(0, 0)
3812         << QPointF(0, 0)
3813         << QPointF(0, 0)
3814         << QPointF(0, 30 * 20 - 320 + 10);
3815
3816     // footer to right of items
3817     QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3818         << QPointF(40 * 3, 0)
3819         << QPointF(40 * 30, 0)
3820         << QPointF(0, 0)
3821         << QPointF(0, 0)
3822         << QPointF(0, 0)
3823         << QPointF(40 * 30 - 240 + 40, 0);
3824
3825     // footer to left of items
3826     QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3827         << QPointF(-(40 * 3) - 100, 0)
3828         << QPointF(-(40 * 30) - 50, 0)     // 50 = new footer width
3829         << QPointF(-240, 0)
3830         << QPointF(-240, 0)
3831         << QPointF(-40, 0)
3832         << QPointF(-(40 * 30) - 40, 0);
3833
3834     // footer above items
3835     QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3836         << QPointF(0, -(3 * 20) - 30)
3837         << QPointF(0, -(30 * 20) - 20)
3838         << QPointF(0, -320)
3839         << QPointF(0, -320)
3840         << QPointF(0, -20)
3841         << QPointF(0, -(30 * 20) - 10);
3842 }
3843
3844 class LVAccessor : public QQuickListView
3845 {
3846 public:
3847     qreal minY() const { return minYExtent(); }
3848     qreal maxY() const { return maxYExtent(); }
3849     qreal minX() const { return minXExtent(); }
3850     qreal maxX() const { return maxXExtent(); }
3851 };
3852
3853
3854 void tst_QQuickListView::extents()
3855 {
3856     QFETCH(QQuickListView::Orientation, orientation);
3857     QFETCH(Qt::LayoutDirection, layoutDirection);
3858     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3859     QFETCH(QPointF, headerPos);
3860     QFETCH(QPointF, footerPos);
3861     QFETCH(QPointF, minPos);
3862     QFETCH(QPointF, maxPos);
3863     QFETCH(QPointF, origin_empty);
3864     QFETCH(QPointF, origin_nonEmpty);
3865
3866     QQuickView *canvas = getView();
3867
3868     QmlListModel model;
3869     QQmlContext *ctxt = canvas->rootContext();
3870     ctxt->setContextProperty("testModel", &model);
3871     canvas->setSource(testFileUrl("headerfooter.qml"));
3872     canvas->show();
3873     qApp->processEvents();
3874
3875     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3876     QTRY_VERIFY(listview != 0);
3877     listview->setOrientation(orientation);
3878     listview->setLayoutDirection(layoutDirection);
3879     listview->setVerticalLayoutDirection(verticalLayoutDirection);
3880     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3881
3882     QQuickItem *contentItem = listview->contentItem();
3883     QTRY_VERIFY(contentItem != 0);
3884
3885     QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3886     QVERIFY(header);
3887     QCOMPARE(header->pos(), headerPos);
3888
3889     QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3890     QVERIFY(footer);
3891     QCOMPARE(footer->pos(), footerPos);
3892
3893     QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3894     QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3895     QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3896     QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3897
3898     QCOMPARE(listview->originX(), origin_empty.x());
3899     QCOMPARE(listview->originY(), origin_empty.y());
3900     for (int i=0; i<30; i++)
3901         model.addItem("Item" + QString::number(i), "");
3902     QTRY_COMPARE(listview->count(), model.count());
3903     QCOMPARE(listview->originX(), origin_nonEmpty.x());
3904     QCOMPARE(listview->originY(), origin_nonEmpty.y());
3905
3906     releaseView(canvas);
3907 }
3908
3909 void tst_QQuickListView::extents_data()
3910 {
3911     QTest::addColumn<QQuickListView::Orientation>("orientation");
3912     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3913     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3914     QTest::addColumn<QPointF>("headerPos");
3915     QTest::addColumn<QPointF>("footerPos");
3916     QTest::addColumn<QPointF>("minPos");
3917     QTest::addColumn<QPointF>("maxPos");
3918     QTest::addColumn<QPointF>("origin_empty");
3919     QTest::addColumn<QPointF>("origin_nonEmpty");
3920
3921     // header is 240x20 (or 20x320 in Horizontal orientation)
3922     // footer is 240x30 (or 30x320 in Horizontal orientation)
3923
3924     QTest::newRow("Vertical, TopToBottom")
3925             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3926             << QPointF(0, -20) << QPointF(0, 0)
3927             << QPointF(0, 20) << QPointF(240, 20)
3928             << QPointF(0, -20) << QPointF(0, -20);
3929
3930     QTest::newRow("Vertical, BottomToTop")
3931             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3932             << QPointF(0, 0) << QPointF(0, -30)
3933             << QPointF(0, 320 - 20) << QPointF(240, 320 - 20)  // content flow is reversed
3934             << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3935
3936     QTest::newRow("Horizontal, LeftToRight")
3937             << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3938             << QPointF(-20, 0) << QPointF(0, 0)
3939             << QPointF(20, 0) << QPointF(20, 320)
3940             << QPointF(-20, 0) << QPointF(-20, 0);
3941
3942     QTest::newRow("Horizontal, RightToLeft")
3943             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3944             << QPointF(0, 0) << QPointF(-30, 0)
3945             << QPointF(240 - 20, 0) << QPointF(240 - 20, 320)  // content flow is reversed
3946             << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3947 }
3948
3949 void tst_QQuickListView::resetModel_headerFooter()
3950 {
3951     // Resetting a model shouldn't crash in views with header/footer
3952
3953     QQuickView *canvas = createView();
3954
3955     QaimModel model;
3956     for (int i = 0; i < 4; i++)
3957         model.addItem("Item" + QString::number(i), "");
3958     QQmlContext *ctxt = canvas->rootContext();
3959     ctxt->setContextProperty("testModel", &model);
3960
3961     canvas->setSource(testFileUrl("headerfooter.qml"));
3962     qApp->processEvents();
3963
3964     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3965     QTRY_VERIFY(listview != 0);
3966
3967     QQuickItem *contentItem = listview->contentItem();
3968     QTRY_VERIFY(contentItem != 0);
3969
3970     QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3971     QVERIFY(header);
3972     QCOMPARE(header->y(), -header->height());
3973
3974     QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3975     QVERIFY(footer);
3976     QCOMPARE(footer->y(), 30.*4);
3977
3978     model.reset();
3979
3980     header = findItem<QQuickItem>(contentItem, "header");
3981     QVERIFY(header);
3982     QCOMPARE(header->y(), -header->height());
3983
3984     footer = findItem<QQuickItem>(contentItem, "footer");
3985     QVERIFY(footer);
3986     QCOMPARE(footer->y(), 30.*4);
3987
3988     delete canvas;
3989 }
3990
3991 void tst_QQuickListView::resizeView()
3992 {
3993     QQuickView *canvas = createView();
3994
3995     QmlListModel model;
3996     for (int i = 0; i < 40; i++)
3997         model.addItem("Item" + QString::number(i), "");
3998
3999     QQmlContext *ctxt = canvas->rootContext();
4000     ctxt->setContextProperty("testModel", &model);
4001
4002     TestObject *testObject = new TestObject;
4003     ctxt->setContextProperty("testObject", testObject);
4004
4005     canvas->setSource(testFileUrl("listviewtest.qml"));
4006     canvas->show();
4007     qApp->processEvents();
4008
4009     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4010     QTRY_VERIFY(listview != 0);
4011     QQuickItem *contentItem = listview->contentItem();
4012     QTRY_VERIFY(contentItem != 0);
4013     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4014
4015     // Confirm items positioned correctly
4016     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4017     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4018         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4019         if (!item) qWarning() << "Item" << i << "not found";
4020         QTRY_VERIFY(item);
4021         QTRY_COMPARE(item->y(), i*20.);
4022     }
4023
4024     QVariant heightRatio;
4025     QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4026     QCOMPARE(heightRatio.toReal(), 0.4);
4027
4028     listview->setHeight(200);
4029     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4030
4031     QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4032     QCOMPARE(heightRatio.toReal(), 0.25);
4033
4034     // Ensure we handle -ve sizes
4035     listview->setHeight(-100);
4036     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
4037
4038     listview->setCacheBuffer(200);
4039     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
4040
4041     // ensure items in cache become visible
4042     listview->setHeight(200);
4043     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
4044
4045     itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4046     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4047         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4048         if (!item) qWarning() << "Item" << i << "not found";
4049         QTRY_VERIFY(item);
4050         QTRY_COMPARE(item->y(), i*20.);
4051         QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
4052     }
4053
4054     // ensure items outside view become invisible
4055     listview->setHeight(100);
4056     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
4057
4058     itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4059     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4060         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4061         if (!item) qWarning() << "Item" << i << "not found";
4062         QTRY_VERIFY(item);
4063         QTRY_COMPARE(item->y(), i*20.);
4064         QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
4065     }
4066
4067     delete canvas;
4068     delete testObject;
4069 }
4070
4071 void tst_QQuickListView::resizeViewAndRepaint()
4072 {
4073     QQuickView *canvas = createView();
4074
4075     QmlListModel model;
4076     for (int i = 0; i < 40; i++)
4077         model.addItem("Item" + QString::number(i), "");
4078
4079     QQmlContext *ctxt = canvas->rootContext();
4080     ctxt->setContextProperty("testModel", &model);
4081     ctxt->setContextProperty("initialHeight", 100);
4082
4083     canvas->setSource(testFileUrl("resizeview.qml"));
4084     canvas->show();
4085     qApp->processEvents();
4086
4087     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4088     QTRY_VERIFY(listview != 0);
4089     QQuickItem *contentItem = listview->contentItem();
4090     QTRY_VERIFY(contentItem != 0);
4091     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4092
4093     // item at index 10 should not be currently visible
4094     QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4095
4096     listview->setHeight(320);
4097
4098     QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
4099
4100     listview->setHeight(100);
4101     QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4102
4103     delete canvas;
4104 }
4105
4106 void tst_QQuickListView::sizeLessThan1()
4107 {
4108     QQuickView *canvas = createView();
4109
4110     QmlListModel model;
4111     for (int i = 0; i < 30; i++)
4112         model.addItem("Item" + QString::number(i), "");
4113
4114     QQmlContext *ctxt = canvas->rootContext();
4115     ctxt->setContextProperty("testModel", &model);
4116
4117     TestObject *testObject = new TestObject;
4118     ctxt->setContextProperty("testObject", testObject);
4119
4120     canvas->setSource(testFileUrl("sizelessthan1.qml"));
4121     canvas->show();
4122     qApp->processEvents();
4123
4124     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4125     QTRY_VERIFY(listview != 0);
4126     QQuickItem *contentItem = listview->contentItem();
4127     QTRY_VERIFY(contentItem != 0);
4128     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4129
4130     // Confirm items positioned correctly
4131     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4132     for (int i = 0; i < model.count() && i < itemCount; ++i) {
4133         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4134         if (!item) qWarning() << "Item" << i << "not found";
4135         QTRY_VERIFY(item);
4136         QTRY_COMPARE(item->y(), i*0.5);
4137     }
4138
4139     delete canvas;
4140     delete testObject;
4141 }
4142
4143 void tst_QQuickListView::QTBUG_14821()
4144 {
4145     QQuickView *canvas = createView();
4146
4147     canvas->setSource(testFileUrl("qtbug14821.qml"));
4148     qApp->processEvents();
4149
4150     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4151     QVERIFY(listview != 0);
4152
4153     QQuickItem *contentItem = listview->contentItem();
4154     QVERIFY(contentItem != 0);
4155
4156     listview->decrementCurrentIndex();
4157     QCOMPARE(listview->currentIndex(), 99);
4158
4159     listview->incrementCurrentIndex();
4160     QCOMPARE(listview->currentIndex(), 0);
4161
4162     delete canvas;
4163 }
4164
4165 void tst_QQuickListView::resizeDelegate()
4166 {
4167     QQuickView *canvas = createView();
4168
4169     QStringList strings;
4170     for (int i = 0; i < 30; ++i)
4171         strings << QString::number(i);
4172     QStringListModel model(strings);
4173
4174     QQmlContext *ctxt = canvas->rootContext();
4175     ctxt->setContextProperty("testModel", &model);
4176
4177     canvas->setSource(testFileUrl("displaylist.qml"));
4178     canvas->show();
4179     qApp->processEvents();
4180
4181     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4182     QVERIFY(listview != 0);
4183     QQuickItem *contentItem = listview->contentItem();
4184     QVERIFY(contentItem != 0);
4185     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4186
4187     QCOMPARE(listview->count(), model.rowCount());
4188
4189     listview->setCurrentIndex(25);
4190     listview->setContentY(0);
4191     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4192
4193     for (int i = 0; i < 16; ++i) {
4194         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4195         QVERIFY(item != 0);
4196         QCOMPARE(item->y(), i*20.0);
4197     }
4198
4199     QCOMPARE(listview->currentItem()->y(), 500.0);
4200     QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4201
4202     canvas->rootObject()->setProperty("delegateHeight", 30);
4203     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4204
4205     for (int i = 0; i < 11; ++i) {
4206         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4207         QVERIFY(item != 0);
4208         QTRY_COMPARE(item->y(), i*30.0);
4209     }
4210
4211     QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4212     QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4213
4214     listview->setCurrentIndex(1);
4215     listview->positionViewAtIndex(25, QQuickListView::Beginning);
4216     listview->positionViewAtIndex(5, QQuickListView::Beginning);
4217     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4218
4219     for (int i = 5; i < 16; ++i) {
4220         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4221         QVERIFY(item != 0);
4222         QCOMPARE(item->y(), i*30.0);
4223     }
4224
4225     QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4226     QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4227
4228     canvas->rootObject()->setProperty("delegateHeight", 20);
4229     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4230
4231     for (int i = 5; i < 11; ++i) {
4232         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4233         QVERIFY(item != 0);
4234         QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4235     }
4236
4237     QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4238     QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4239
4240     delete canvas;
4241 }
4242
4243 void tst_QQuickListView::resizeFirstDelegate()
4244 {
4245     // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4246     // and other delegates have height > 0
4247
4248     QQuickView *canvas = createView();
4249
4250     // bug only occurs when all items in the model are visible
4251     QmlListModel model;
4252     for (int i = 0; i < 10; i++)
4253         model.addItem("Item" + QString::number(i), "");
4254
4255     QQmlContext *ctxt = canvas->rootContext();
4256     ctxt->setContextProperty("testModel", &model);
4257
4258     TestObject *testObject = new TestObject;
4259     ctxt->setContextProperty("testObject", testObject);
4260
4261     canvas->setSource(testFileUrl("listviewtest.qml"));
4262     canvas->show();
4263     qApp->processEvents();
4264
4265     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4266     QVERIFY(listview != 0);
4267     QQuickItem *contentItem = listview->contentItem();
4268     QVERIFY(contentItem != 0);
4269     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4270
4271     QQuickItem *item = 0;
4272     for (int i = 0; i < model.count(); ++i) {
4273         item = findItem<QQuickItem>(contentItem, "wrapper", i);
4274         QVERIFY(item != 0);
4275         QCOMPARE(item->y(), i*20.0);
4276     }
4277
4278     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4279     item->setHeight(0);
4280
4281     // check the content y has not jumped up and down
4282     QCOMPARE(listview->contentY(), 0.0);
4283     QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4284     QTest::qWait(100);
4285     QCOMPARE(spy.count(), 0);
4286
4287     for (int i = 1; i < model.count(); ++i) {
4288         item = findItem<QQuickItem>(contentItem, "wrapper", i);
4289         QVERIFY(item != 0);
4290         QTRY_COMPARE(item->y(), (i-1)*20.0);
4291     }
4292
4293
4294     // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4295     // list if they follow a zero-sized delegate
4296
4297     for (int i = 0; i < 10; i++)
4298         model.addItem("Item" + QString::number(i), "");
4299     QTRY_COMPARE(listview->count(), model.count());
4300
4301     item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4302     QVERIFY(item);
4303     item->setHeight(0);
4304
4305     listview->setCurrentIndex(19);
4306     qApp->processEvents();
4307     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4308
4309     // items 0-2 should have been deleted
4310     for (int i=0; i<3; i++) {
4311         QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4312     }
4313
4314     delete testObject;
4315     delete canvas;
4316 }
4317
4318 void tst_QQuickListView::repositionResizedDelegate()
4319 {
4320     QFETCH(QQuickListView::Orientation, orientation);
4321     QFETCH(Qt::LayoutDirection, layoutDirection);
4322     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4323     QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4324     QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4325     QFETCH(QRectF, origPositionerRect);
4326     QFETCH(QRectF, resizedPositionerRect);
4327
4328     QQuickView *canvas = getView();
4329     QQmlContext *ctxt = canvas->rootContext();
4330     ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4331     ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4332     ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4333     canvas->setSource(testFileUrl("repositionResizedDelegate.qml"));
4334     canvas->show();
4335     qApp->processEvents();
4336
4337     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4338     QTRY_VERIFY(listview != 0);
4339     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4340
4341     QQuickItem *positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4342     QVERIFY(positioner);
4343     QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4344     QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4345     QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4346     int prevSpyCount = 0;
4347
4348     // When an item is resized while it is partially visible, it should resize in the
4349     // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4350     // the item should also be re-positioned so its end position stays the same.
4351
4352     listview->setContentX(contentPos_itemFirstHalfVisible.x());
4353     listview->setContentY(contentPos_itemFirstHalfVisible.y());
4354     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4355     prevSpyCount = spy.count();
4356     QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4357     QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4358     QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4359     QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4360     QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4361     QCOMPARE(spy.count(), prevSpyCount);
4362
4363     QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "decrementRepeater"));
4364     QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4365     QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4366     QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4367     QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4368
4369     listview->setContentX(contentPos_itemSecondHalfVisible.x());
4370     listview->setContentY(contentPos_itemSecondHalfVisible.y());
4371     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4372     prevSpyCount = spy.count();
4373
4374     QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4375     positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4376     QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4377     QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4378     QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4379     QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4380     qApp->processEvents();
4381     QCOMPARE(spy.count(), prevSpyCount);
4382
4383     releaseView(canvas);
4384 }
4385
4386 void tst_QQuickListView::repositionResizedDelegate_data()
4387 {
4388     QTest::addColumn<QQuickListView::Orientation>("orientation");
4389     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4390     QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4391     QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4392     QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4393     QTest::addColumn<QRectF>("origPositionerRect");
4394     QTest::addColumn<QRectF>("resizedPositionerRect");
4395
4396     QTest::newRow("vertical")
4397             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4398             << QPointF(0, 60) << QPointF(0, 200 + 60)
4399             << QRectF(0, 200, 120, 120)
4400             << QRectF(0, 200, 120, 120 * 2);
4401
4402     QTest::newRow("vertical, BottomToTop")
4403             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4404             << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4405             << QRectF(0, -200 - 120, 120, 120)
4406             << QRectF(0, -200 - 120*2, 120, 120 * 2);
4407
4408     QTest::newRow("horizontal")
4409             << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4410             << QPointF(60, 0) << QPointF(260, 0)
4411             << QRectF(200, 0, 120, 120)
4412             << QRectF(200, 0, 120 * 2, 120);
4413
4414     QTest::newRow("horizontal, rtl")
4415             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4416             << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4417             << QRectF(-200 - 120, 0, 120, 120)
4418             << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4419 }
4420
4421 void tst_QQuickListView::QTBUG_16037()
4422 {
4423     QQuickView *canvas = createView();
4424     canvas->show();
4425
4426     canvas->setSource(testFileUrl("qtbug16037.qml"));
4427     qApp->processEvents();
4428
4429     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4430     QTRY_VERIFY(listview != 0);
4431
4432     QVERIFY(listview->contentHeight() <= 0.0);
4433
4434     QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
4435
4436     QTRY_COMPARE(listview->contentHeight(), 80.0);
4437
4438     delete canvas;
4439 }
4440
4441 void tst_QQuickListView::indexAt_itemAt_data()
4442 {
4443     QTest::addColumn<qreal>("x");
4444     QTest::addColumn<qreal>("y");
4445     QTest::addColumn<int>("index");
4446
4447     QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4448     QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4449     QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4450     QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4451     QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4452 }
4453
4454 void tst_QQuickListView::indexAt_itemAt()
4455 {
4456     QFETCH(qreal, x);
4457     QFETCH(qreal, y);
4458     QFETCH(int, index);
4459
4460     QQuickView *canvas = getView();
4461
4462     QmlListModel model;
4463     for (int i = 0; i < 30; i++)
4464         model.addItem("Item" + QString::number(i), "");
4465
4466     QQmlContext *ctxt = canvas->rootContext();
4467     ctxt->setContextProperty("testModel", &model);
4468
4469     TestObject *testObject = new TestObject;
4470     ctxt->setContextProperty("testObject", testObject);
4471
4472     canvas->setSource(testFileUrl("listviewtest.qml"));
4473     canvas->show();
4474     qApp->processEvents();
4475
4476     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4477     QTRY_VERIFY(listview != 0);
4478
4479     QQuickItem *contentItem = listview->contentItem();
4480     QTRY_VERIFY(contentItem != 0);
4481     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4482
4483     QQuickItem *item = 0;
4484     if (index >= 0) {
4485         item = findItem<QQuickItem>(contentItem, "wrapper", index);
4486         QVERIFY(item);
4487     }
4488     QCOMPARE(listview->indexAt(x,y), index);
4489     QVERIFY(listview->itemAt(x,y) == item);
4490
4491     releaseView(canvas);
4492     delete testObject;
4493 }
4494
4495 void tst_QQuickListView::incrementalModel()
4496 {
4497     QQuickView *canvas = createView();
4498
4499     IncrementalModel model;
4500     QQmlContext *ctxt = canvas->rootContext();
4501     ctxt->setContextProperty("testModel", &model);
4502
4503     canvas->setSource(testFileUrl("displaylist.qml"));
4504     qApp->processEvents();
4505
4506     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4507     QTRY_VERIFY(listview != 0);
4508
4509     QQuickItem *contentItem = listview->contentItem();
4510     QTRY_VERIFY(contentItem != 0);
4511
4512     QTRY_COMPARE(listview->count(), 20);
4513
4514     listview->positionViewAtIndex(10, QQuickListView::Beginning);
4515
4516     QTRY_COMPARE(listview->count(), 25);
4517
4518     delete canvas;
4519 }
4520
4521 void tst_QQuickListView::onAdd()
4522 {
4523     QFETCH(int, initialItemCount);
4524     QFETCH(int, itemsToAdd);
4525
4526     const int delegateHeight = 10;
4527     QaimModel model;
4528
4529     // these initial items should not trigger ListView.onAdd
4530     for (int i=0; i<initialItemCount; i++)
4531         model.addItem("dummy value", "dummy value");
4532
4533     QQuickView *canvas = createView();
4534     canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4535     QQmlContext *ctxt = canvas->rootContext();
4536     ctxt->setContextProperty("testModel", &model);
4537     ctxt->setContextProperty("delegateHeight", delegateHeight);
4538     canvas->setSource(testFileUrl("attachedSignals.qml"));
4539
4540     QObject *object = canvas->rootObject();
4541     object->setProperty("width", canvas->width());
4542     object->setProperty("height", canvas->height());
4543     qApp->processEvents();
4544
4545     QList<QPair<QString, QString> > items;
4546     for (int i=0; i<itemsToAdd; i++)
4547         items << qMakePair(QString("value %1").arg(i), QString::number(i));
4548     model.addItems(items);
4549     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4550
4551     QVariantList result = object->property("addedDelegates").toList();
4552     QCOMPARE(result.count(), items.count());
4553     for (int i=0; i<items.count(); i++)
4554         QCOMPARE(result[i].toString(), items[i].first);
4555
4556     delete canvas;
4557 }
4558
4559 void tst_QQuickListView::onAdd_data()
4560 {
4561     QTest::addColumn<int>("initialItemCount");
4562     QTest::addColumn<int>("itemsToAdd");
4563
4564     QTest::newRow("0, add 1") << 0 << 1;
4565     QTest::newRow("0, add 2") << 0 << 2;
4566     QTest::newRow("0, add 10") << 0 << 10;
4567
4568     QTest::newRow("1, add 1") << 1 << 1;
4569     QTest::newRow("1, add 2") << 1 << 2;
4570     QTest::newRow("1, add 10") << 1 << 10;
4571
4572     QTest::newRow("5, add 1") << 5 << 1;
4573     QTest::newRow("5, add 2") << 5 << 2;
4574     QTest::newRow("5, add 10") << 5 << 10;
4575 }
4576
4577 void tst_QQuickListView::onRemove()
4578 {
4579     QFETCH(int, initialItemCount);
4580     QFETCH(int, indexToRemove);
4581     QFETCH(int, removeCount);
4582
4583     const int delegateHeight = 10;
4584     QaimModel model;
4585     for (int i=0; i<initialItemCount; i++)
4586         model.addItem(QString("value %1").arg(i), "dummy value");
4587
4588     QQuickView *canvas = getView();
4589     QQmlContext *ctxt = canvas->rootContext();
4590     ctxt->setContextProperty("testModel", &model);
4591     ctxt->setContextProperty("delegateHeight", delegateHeight);
4592     canvas->setSource(testFileUrl("attachedSignals.qml"));
4593
4594     QObject *object = canvas->rootObject();
4595
4596     model.removeItems(indexToRemove, removeCount);
4597     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4598
4599     QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4600
4601     releaseView(canvas);
4602 }
4603
4604 void tst_QQuickListView::onRemove_data()
4605 {
4606     QTest::addColumn<int>("initialItemCount");
4607     QTest::addColumn<int>("indexToRemove");
4608     QTest::addColumn<int>("removeCount");
4609
4610     QTest::newRow("remove first") << 1 << 0 << 1;
4611     QTest::newRow("two items, remove first") << 2 << 0 << 1;
4612     QTest::newRow("two items, remove last") << 2 << 1 << 1;
4613     QTest::newRow("two items, remove all") << 2 << 0 << 2;
4614
4615     QTest::newRow("four items, remove first") << 4 << 0 << 1;
4616     QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4617     QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4618     QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4619     QTest::newRow("four items, remove last") << 4 << 3 << 1;
4620     QTest::newRow("four items, remove all") << 4 << 0 << 4;
4621
4622     QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4623     QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4624     QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4625 }
4626
4627 void tst_QQuickListView::rightToLeft()
4628 {
4629     QQuickView *canvas = createView();
4630     canvas->setGeometry(0,0,640,320);
4631     canvas->setSource(testFileUrl("rightToLeft.qml"));
4632     canvas->show();
4633     qApp->processEvents();
4634
4635     QVERIFY(canvas->rootObject() != 0);
4636     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4637     QTRY_VERIFY(listview != 0);
4638
4639     QQuickItem *contentItem = listview->contentItem();
4640     QTRY_VERIFY(contentItem != 0);
4641
4642     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4643
4644     QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4645     QTRY_VERIFY(model != 0);
4646
4647     QTRY_VERIFY(model->count() == 3);
4648     QTRY_COMPARE(listview->currentIndex(), 0);
4649
4650     // initial position at first item, right edge aligned
4651     QCOMPARE(listview->contentX(), -640.);
4652
4653     QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4654     QTRY_VERIFY(item);
4655     QTRY_COMPARE(item->x(), -100.0);
4656     QCOMPARE(item->height(), listview->height());
4657
4658     QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4659     QTRY_VERIFY(text);
4660     QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4661
4662     listview->setCurrentIndex(2);
4663
4664     item = findItem<QQuickItem>(contentItem, "item3");
4665     QTRY_VERIFY(item);
4666     QTRY_COMPARE(item->x(), -540.0);
4667
4668     text = findItem<QQuickText>(contentItem, "text3");
4669     QTRY_VERIFY(text);
4670     QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4671
4672     QCOMPARE(listview->contentX(), -640.);
4673
4674     // Ensure resizing maintains position relative to right edge
4675     qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4676     QTRY_COMPARE(listview->contentX(), -600.);
4677
4678     delete canvas;
4679 }
4680
4681 void tst_QQuickListView::test_mirroring()
4682 {
4683     QQuickView *canvasA = createView();
4684     canvasA->setSource(testFileUrl("rightToLeft.qml"));
4685     QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4686     QTRY_VERIFY(listviewA != 0);
4687
4688     QQuickView *canvasB = createView();
4689     canvasB->setSource(testFileUrl("rightToLeft.qml"));
4690     QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4691     QTRY_VERIFY(listviewA != 0);
4692     qApp->processEvents();
4693
4694     QList<QString> objectNames;
4695     objectNames << "item1" << "item2"; // << "item3"
4696
4697     listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4698     listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4699     QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4700
4701     // LTR != RTL
4702     foreach (const QString objectName, objectNames)
4703         QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4704
4705     listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4706     listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4707
4708     // LTR == LTR
4709     foreach (const QString objectName, objectNames)
4710         QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4711
4712     QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4713     QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4714     QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4715
4716     // LTR != LTR+mirror
4717     foreach (const QString objectName, objectNames)
4718         QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4719
4720     listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4721
4722     // RTL == LTR+mirror
4723     foreach (const QString objectName, objectNames)
4724         QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4725
4726     listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4727
4728     // RTL != RTL+mirror
4729     foreach (const QString objectName, objectNames)
4730         QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4731
4732     listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4733
4734     // LTR == RTL+mirror
4735     foreach (const QString objectName, objectNames)
4736         QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4737
4738     delete canvasA;
4739     delete canvasB;
4740 }
4741
4742 void tst_QQuickListView::margins()
4743 {
4744     QQuickView *canvas = createView();
4745
4746     QaimModel model;
4747     for (int i = 0; i < 50; i++)
4748         model.addItem("Item" + QString::number(i), "");
4749
4750     QQmlContext *ctxt = canvas->rootContext();
4751     ctxt->setContextProperty("testModel", &model);
4752
4753     canvas->setSource(testFileUrl("margins.qml"));
4754     canvas->show();
4755     qApp->processEvents();
4756
4757     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4758     QTRY_VERIFY(listview != 0);
4759     QQuickItem *contentItem = listview->contentItem();
4760     QTRY_VERIFY(contentItem != 0);
4761     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4762
4763     QCOMPARE(listview->contentY(), -30.);
4764     QCOMPARE(listview->originY(), 0.);
4765
4766     // check end bound
4767     listview->positionViewAtEnd();
4768     qreal pos = listview->contentY();
4769     listview->setContentY(pos + 80);
4770     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4771     listview->returnToBounds();
4772     QTRY_COMPARE(listview->contentY(), pos + 50);
4773
4774     // remove item before visible and check that top margin is maintained
4775     // and originY is updated
4776     listview->setContentY(100);
4777     model.removeItem(1);
4778     QTRY_COMPARE(listview->count(), model.count());
4779     listview->setContentY(-50);
4780     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4781     listview->returnToBounds();
4782     QCOMPARE(listview->originY(), 20.);
4783     QTRY_COMPARE(listview->contentY(), -10.);
4784
4785     // reduce top margin
4786     listview->setTopMargin(20);
4787     QCOMPARE(listview->originY(), 20.);
4788     QTRY_COMPARE(listview->contentY(), 0.);
4789
4790     // check end bound
4791     listview->positionViewAtEnd();
4792     pos = listview->contentY();
4793     listview->setContentY(pos + 80);
4794     listview->returnToBounds();
4795     QTRY_COMPARE(listview->contentY(), pos + 50);
4796
4797     // reduce bottom margin
4798     pos = listview->contentY();
4799     listview->setBottomMargin(40);
4800     QCOMPARE(listview->originY(), 20.);
4801     QTRY_COMPARE(listview->contentY(), pos-10);
4802
4803     delete canvas;
4804 }
4805
4806 // QTBUG-24028
4807 void tst_QQuickListView::marginsResize()
4808 {
4809     QFETCH(QQuickListView::Orientation, orientation);
4810     QFETCH(Qt::LayoutDirection, layoutDirection);
4811     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4812     QFETCH(qreal, start);
4813     QFETCH(qreal, end);
4814
4815     QPoint flickStart(20, 20);
4816     QPoint flickEnd(20, 20);
4817     if (orientation == QQuickListView::Vertical)
4818         flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4819     else
4820         flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4821
4822     QQuickView *canvas = getView();
4823
4824     canvas->setSource(testFileUrl("margins2.qml"));
4825     canvas->show();
4826     qApp->processEvents();
4827
4828     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4829     QTRY_VERIFY(listview != 0);
4830
4831     listview->setOrientation(orientation);
4832     listview->setLayoutDirection(layoutDirection);
4833     listview->setVerticalLayoutDirection(verticalLayoutDirection);
4834     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4835
4836     // view is resized after componentCompleted - top margin should still be visible
4837     if (orientation == QQuickListView::Vertical)
4838         QCOMPARE(listview->contentY(), start);
4839     else
4840         QCOMPARE(listview->contentX(), start);
4841
4842     // move to last index and ensure bottom margin is visible.
4843     listview->setCurrentIndex(19);
4844     if (orientation == QQuickListView::Vertical)
4845         QTRY_COMPARE(listview->contentY(), end);
4846     else
4847         QTRY_COMPARE(listview->contentX(), end);
4848
4849     // flick past the end and check content pos still settles on correct extents
4850     flick(canvas, flickStart, flickEnd, 180);
4851     QTRY_VERIFY(listview->isMoving() == false);
4852     if (orientation == QQuickListView::Vertical)
4853         QTRY_COMPARE(listview->contentY(), end);
4854     else
4855         QTRY_COMPARE(listview->contentX(), end);
4856
4857     // back to top - top margin should be visible.
4858     listview->setCurrentIndex(0);
4859     if (orientation == QQuickListView::Vertical)
4860         QTRY_COMPARE(listview->contentY(), start);
4861     else
4862         QTRY_COMPARE(listview->contentX(), start);
4863
4864     // flick past the beginning and check content pos still settles on correct extents
4865     flick(canvas, flickEnd, flickStart, 180);
4866     QTRY_VERIFY(listview->isMoving() == false);
4867     if (orientation == QQuickListView::Vertical)
4868         QTRY_COMPARE(listview->contentY(), start);
4869     else
4870         QTRY_COMPARE(listview->contentX(), start);
4871
4872     releaseView(canvas);
4873 }
4874
4875 void tst_QQuickListView::marginsResize_data()
4876 {
4877     QTest::addColumn<QQuickListView::Orientation>("orientation");
4878     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4879     QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4880     QTest::addColumn<qreal>("start");
4881     QTest::addColumn<qreal>("end");
4882
4883     // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4884
4885     QTest::newRow("vertical")
4886             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4887             << -40.0 << 1020.0;
4888
4889     QTest::newRow("vertical, BottomToTop")
4890             << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4891             << -180.0 << -1240.0;
4892
4893     QTest::newRow("horizontal")
4894             << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4895             << -40.0 << 1020.0;
4896
4897     QTest::newRow("horizontal, rtl")
4898             << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4899             << -180.0 << -1240.0;
4900 }
4901
4902 void tst_QQuickListView::snapToItem_data()
4903 {
4904     QTest::addColumn<QQuickListView::Orientation>("orientation");
4905     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4906     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4907     QTest::addColumn<int>("highlightRangeMode");
4908     QTest::addColumn<QPoint>("flickStart");
4909     QTest::addColumn<QPoint>("flickEnd");
4910     QTest::addColumn<qreal>("snapAlignment");
4911     QTest::addColumn<qreal>("endExtent");
4912     QTest::addColumn<qreal>("startExtent");
4913
4914     QTest::newRow("vertical, top to bottom")
4915         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4916         << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4917
4918     QTest::newRow("vertical, bottom to top")
4919         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4920         << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4921
4922     QTest::newRow("horizontal, left to right")
4923         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4924         << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4925
4926     QTest::newRow("horizontal, right to left")
4927         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4928         << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4929
4930     QTest::newRow("vertical, top to bottom, enforce range")
4931         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4932         << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4933
4934     QTest::newRow("vertical, bottom to top, enforce range")
4935         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4936         << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4937
4938     QTest::newRow("horizontal, left to right, enforce range")
4939         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4940         << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4941
4942     QTest::newRow("horizontal, right to left, enforce range")
4943         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4944         << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4945 }
4946
4947 void tst_QQuickListView::snapToItem()
4948 {
4949     QFETCH(QQuickListView::Orientation, orientation);
4950     QFETCH(Qt::LayoutDirection, layoutDirection);
4951     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4952     QFETCH(int, highlightRangeMode);
4953     QFETCH(QPoint, flickStart);
4954     QFETCH(QPoint, flickEnd);
4955     QFETCH(qreal, snapAlignment);
4956     QFETCH(qreal, endExtent);
4957     QFETCH(qreal, startExtent);
4958
4959     QQuickView *canvas = getView();
4960
4961     canvas->setSource(testFileUrl("snapToItem.qml"));
4962     canvas->show();
4963     qApp->processEvents();
4964
4965     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4966     QTRY_VERIFY(listview != 0);
4967
4968     listview->setOrientation(orientation);
4969     listview->setLayoutDirection(layoutDirection);
4970     listview->setVerticalLayoutDirection(verticalLayoutDirection);
4971     listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4972     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4973
4974     QQuickItem *contentItem = listview->contentItem();
4975     QTRY_VERIFY(contentItem != 0);
4976
4977     // confirm that a flick hits an item boundary
4978     flick(canvas, flickStart, flickEnd, 180);
4979     QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4980     if (orientation == QQuickListView::Vertical)
4981         QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4982     else
4983         QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4984
4985     // flick to end
4986     do {
4987         flick(canvas, flickStart, flickEnd, 180);
4988         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4989     } while (orientation == QQuickListView::Vertical
4990            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4991            : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4992
4993     if (orientation == QQuickListView::Vertical)
4994         QCOMPARE(listview->contentY(), endExtent);
4995     else
4996         QCOMPARE(listview->contentX(), endExtent);
4997
4998     // flick to start
4999     do {
5000         flick(canvas, flickEnd, flickStart, 180);
5001         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5002     } while (orientation == QQuickListView::Vertical
5003            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5004            : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5005
5006     if (orientation == QQuickListView::Vertical)
5007         QCOMPARE(listview->contentY(), startExtent);
5008     else
5009         QCOMPARE(listview->contentX(), startExtent);
5010
5011     releaseView(canvas);
5012 }
5013
5014 void tst_QQuickListView::qListModelInterface_items()
5015 {
5016     items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5017 }
5018
5019 void tst_QQuickListView::qListModelInterface_package_items()
5020 {
5021     items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5022 }
5023
5024 void tst_QQuickListView::qAbstractItemModel_items()
5025 {
5026     items<QaimModel>(testFileUrl("listviewtest.qml"), false);
5027 }
5028
5029 void tst_QQuickListView::qListModelInterface_changed()
5030 {
5031     changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5032 }
5033
5034 void tst_QQuickListView::qListModelInterface_package_changed()
5035 {
5036     changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5037 }
5038
5039 void tst_QQuickListView::qAbstractItemModel_changed()
5040 {
5041     changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5042 }
5043
5044 void tst_QQuickListView::qListModelInterface_inserted()
5045 {
5046     inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
5047 }
5048
5049 void tst_QQuickListView::qListModelInterface_package_inserted()
5050 {
5051     inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5052 }
5053
5054 void tst_QQuickListView::qListModelInterface_inserted_more()
5055 {
5056     inserted_more<QmlListModel>();
5057 }
5058
5059 void tst_QQuickListView::qListModelInterface_inserted_more_data()
5060 {
5061     inserted_more_data();
5062 }
5063
5064 void tst_QQuickListView::qAbstractItemModel_inserted()
5065 {
5066     inserted<QaimModel>(testFileUrl("listviewtest.qml"));
5067 }
5068
5069 void tst_QQuickListView::qAbstractItemModel_inserted_more()
5070 {
5071     inserted_more<QaimModel>();
5072 }
5073
5074 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
5075 {
5076     inserted_more_data();
5077 }
5078
5079 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
5080 {
5081     inserted_more<QaimModel>(QQuickItemView::BottomToTop);
5082 }
5083
5084 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
5085 {
5086     inserted_more_data();
5087 }
5088
5089 void tst_QQuickListView::qListModelInterface_removed()
5090 {
5091     removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5092     removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
5093 }
5094
5095 void tst_QQuickListView::qListModelInterface_removed_more()
5096 {
5097     removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
5098 }
5099
5100 void tst_QQuickListView::qListModelInterface_removed_more_data()
5101 {
5102     removed_more_data();
5103 }
5104
5105 void tst_QQuickListView::qListModelInterface_package_removed()
5106 {
5107     removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
5108     removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5109 }
5110
5111 void tst_QQuickListView::qAbstractItemModel_removed()
5112 {
5113     removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5114     removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5115 }
5116
5117 void tst_QQuickListView::qAbstractItemModel_removed_more()
5118 {
5119     removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5120 }
5121
5122 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5123 {
5124     removed_more_data();
5125 }
5126
5127 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5128 {
5129     removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5130 }
5131
5132 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5133 {
5134     removed_more_data();
5135 }
5136
5137 void tst_QQuickListView::qListModelInterface_moved()
5138 {
5139     moved<QmlListModel>(testFileUrl("listviewtest.qml"));
5140 }
5141
5142 void tst_QQuickListView::qListModelInterface_moved_data()
5143 {
5144     moved_data();
5145 }
5146
5147 void tst_QQuickListView::qListModelInterface_package_moved()
5148 {
5149     moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5150 }
5151
5152 void tst_QQuickListView::qListModelInterface_package_moved_data()
5153 {
5154     moved_data();
5155 }
5156
5157 void tst_QQuickListView::qAbstractItemModel_moved()
5158 {
5159     moved<QaimModel>(testFileUrl("listviewtest.qml"));
5160 }
5161
5162 void tst_QQuickListView::qAbstractItemModel_moved_data()
5163 {
5164     moved_data();
5165 }
5166
5167 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5168 {
5169     moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5170 }
5171
5172 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5173 {
5174     moved_data();
5175 }
5176
5177 void tst_QQuickListView::qListModelInterface_clear()
5178 {
5179     clear<QmlListModel>(testFileUrl("listviewtest.qml"));
5180 }
5181
5182 void tst_QQuickListView::qListModelInterface_package_clear()
5183 {
5184     clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5185 }
5186
5187 void tst_QQuickListView::qAbstractItemModel_clear()
5188 {
5189     clear<QaimModel>(testFileUrl("listviewtest.qml"));
5190 }
5191
5192 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5193 {
5194     clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5195 }
5196
5197 void tst_QQuickListView::qListModelInterface_sections()
5198 {
5199     sections<QmlListModel>(testFileUrl("listview-sections.qml"));
5200 }
5201
5202 void tst_QQuickListView::qListModelInterface_package_sections()
5203 {
5204     sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
5205 }
5206
5207 void tst_QQuickListView::qAbstractItemModel_sections()
5208 {
5209     sections<QaimModel>(testFileUrl("listview-sections.qml"));
5210 }
5211
5212 void tst_QQuickListView::creationContext()
5213 {
5214     QQuickView canvas;
5215     canvas.setGeometry(0,0,240,320);
5216     canvas.setSource(testFileUrl("creationContext.qml"));
5217     qApp->processEvents();
5218
5219     QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5220     QVERIFY(rootItem);
5221     QVERIFY(rootItem->property("count").toInt() > 0);
5222
5223     QQuickItem *item;
5224     QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5225     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5226     QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5227     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5228     QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5229     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5230     QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5231     QCOMPARE(item->property("text").toString(), QString("Hello!"));
5232 }
5233
5234 void tst_QQuickListView::QTBUG_21742()
5235 {
5236     QQuickView canvas;
5237     canvas.setGeometry(0,0,200,200);
5238     canvas.setSource(testFileUrl("qtbug-21742.qml"));
5239     qApp->processEvents();
5240
5241     QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5242     QVERIFY(rootItem);
5243     QCOMPARE(rootItem->property("count").toInt(), 1);
5244 }
5245
5246 void tst_QQuickListView::asynchronous()
5247 {
5248     QQuickView *canvas = createView();
5249     canvas->show();
5250     QQmlIncubationController controller;
5251     canvas->engine()->setIncubationController(&controller);
5252
5253     canvas->setSource(testFileUrl("asyncloader.qml"));
5254
5255     QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5256     QVERIFY(rootObject);
5257
5258     QQuickListView *listview = 0;
5259     while (!listview) {
5260         bool b = false;
5261         controller.incubateWhile(&b);
5262         listview = rootObject->findChild<QQuickListView*>("view");
5263     }
5264
5265     // items will be created one at a time
5266     for (int i = 0; i < 8; ++i) {
5267         QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5268         QQuickItem *item = 0;
5269         while (!item) {
5270             bool b = false;
5271             controller.incubateWhile(&b);
5272             item = findItem<QQuickItem>(listview, "wrapper", i);
5273         }
5274     }
5275
5276     {
5277         bool b = true;
5278         controller.incubateWhile(&b);
5279     }
5280
5281     // verify positioning
5282     QQuickItem *contentItem = listview->contentItem();
5283     for (int i = 0; i < 8; ++i) {
5284         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5285         QTRY_COMPARE(item->y(), i*50.0);
5286     }
5287
5288     delete canvas;
5289 }
5290
5291 void tst_QQuickListView::snapOneItem_data()
5292 {
5293     QTest::addColumn<QQuickListView::Orientation>("orientation");
5294     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5295     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5296     QTest::addColumn<int>("highlightRangeMode");
5297     QTest::addColumn<QPoint>("flickStart");
5298     QTest::addColumn<QPoint>("flickEnd");
5299     QTest::addColumn<qreal>("snapAlignment");
5300     QTest::addColumn<qreal>("endExtent");
5301     QTest::addColumn<qreal>("startExtent");
5302
5303     QTest::newRow("vertical, top to bottom")
5304         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5305         << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5306
5307     QTest::newRow("vertical, bottom to top")
5308         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5309         << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5310
5311     QTest::newRow("horizontal, left to right")
5312         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5313         << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5314
5315     QTest::newRow("horizontal, right to left")
5316         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5317         << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5318
5319     QTest::newRow("vertical, top to bottom, enforce range")
5320         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5321         << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5322
5323     QTest::newRow("vertical, bottom to top, enforce range")
5324         << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5325         << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5326
5327     QTest::newRow("horizontal, left to right, enforce range")
5328         << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5329         << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5330
5331     QTest::newRow("horizontal, right to left, enforce range")
5332         << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5333         << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5334 }
5335
5336 void tst_QQuickListView::snapOneItem()
5337 {
5338     QFETCH(QQuickListView::Orientation, orientation);
5339     QFETCH(Qt::LayoutDirection, layoutDirection);
5340     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5341     QFETCH(int, highlightRangeMode);
5342     QFETCH(QPoint, flickStart);
5343     QFETCH(QPoint, flickEnd);
5344     QFETCH(qreal, snapAlignment);
5345     QFETCH(qreal, endExtent);
5346     QFETCH(qreal, startExtent);
5347
5348 #ifdef Q_OS_MAC
5349     // This test seems to be unreliable - different test data fails on different runs
5350     QSKIP("QTBUG-24338");
5351 #endif
5352
5353     QQuickView *canvas = getView();
5354
5355     canvas->setSource(testFileUrl("snapOneItem.qml"));
5356     canvas->show();
5357     qApp->processEvents();
5358
5359     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5360     QTRY_VERIFY(listview != 0);
5361
5362     listview->setOrientation(orientation);
5363     listview->setLayoutDirection(layoutDirection);
5364     listview->setVerticalLayoutDirection(verticalLayoutDirection);
5365     listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5366     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5367
5368     QQuickItem *contentItem = listview->contentItem();
5369     QTRY_VERIFY(contentItem != 0);
5370
5371     QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5372
5373     // confirm that a flick hits the next item boundary
5374     flick(canvas, flickStart, flickEnd, 180);
5375     QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5376     if (orientation == QQuickListView::Vertical)
5377         QCOMPARE(listview->contentY(), snapAlignment);
5378     else
5379         QCOMPARE(listview->contentX(), snapAlignment);
5380
5381     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5382         QCOMPARE(listview->currentIndex(), 1);
5383         QCOMPARE(currentIndexSpy.count(), 1);
5384     }
5385
5386     // flick to end
5387     do {
5388         flick(canvas, flickStart, flickEnd, 180);
5389         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5390     } while (orientation == QQuickListView::Vertical
5391            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5392            : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5393
5394     if (orientation == QQuickListView::Vertical)
5395         QCOMPARE(listview->contentY(), endExtent);
5396     else
5397         QCOMPARE(listview->contentX(), endExtent);
5398
5399     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5400         QCOMPARE(listview->currentIndex(), 3);
5401         QCOMPARE(currentIndexSpy.count(), 3);
5402     }
5403
5404     // flick to start
5405     do {
5406         flick(canvas, flickEnd, flickStart, 180);
5407         QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5408     } while (orientation == QQuickListView::Vertical
5409            ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5410            : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5411
5412     if (orientation == QQuickListView::Vertical)
5413         QCOMPARE(listview->contentY(), startExtent);
5414     else
5415         QCOMPARE(listview->contentX(), startExtent);
5416
5417     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5418         QCOMPARE(listview->currentIndex(), 0);
5419         QCOMPARE(currentIndexSpy.count(), 6);
5420     }
5421
5422     releaseView(canvas);
5423 }
5424
5425 void tst_QQuickListView::unrequestedVisibility()
5426 {
5427     QmlListModel model;
5428     for (int i = 0; i < 30; i++)
5429         model.addItem("Item" + QString::number(i), QString::number(i));
5430
5431     QQuickView *canvas = new QQuickView(0);
5432     canvas->setGeometry(0,0,240,320);
5433
5434     QQmlContext *ctxt = canvas->rootContext();
5435     ctxt->setContextProperty("testModel", &model);
5436     ctxt->setContextProperty("testWrap", QVariant(false));
5437
5438     canvas->setSource(testFileUrl("unrequestedItems.qml"));
5439     canvas->show();
5440     qApp->processEvents();
5441
5442     QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
5443     QTRY_VERIFY(leftview != 0);
5444
5445     QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
5446     QTRY_VERIFY(rightview != 0);
5447
5448     QQuickItem *leftContent = leftview->contentItem();
5449     QTRY_VERIFY(leftContent != 0);
5450
5451     QQuickItem *rightContent = rightview->contentItem();
5452     QTRY_VERIFY(rightContent != 0);
5453
5454     rightview->setCurrentIndex(20);
5455
5456     QTRY_COMPARE(leftview->contentY(), 0.0);
5457     QTRY_COMPARE(rightview->contentY(), 100.0);
5458
5459     QQuickItem *item;
5460
5461     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5462     QCOMPARE(delegateVisible(item), true);
5463     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5464     QCOMPARE(delegateVisible(item), false);
5465
5466     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5467     QCOMPARE(delegateVisible(item), false);
5468     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5469     QCOMPARE(delegateVisible(item), true);
5470
5471     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5472     QCOMPARE(delegateVisible(item), true);
5473     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5474     QCOMPARE(delegateVisible(item), false);
5475     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5476     QCOMPARE(delegateVisible(item), false);
5477     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5478     QCOMPARE(delegateVisible(item), true);
5479
5480     rightview->setCurrentIndex(0);
5481
5482     QTRY_COMPARE(leftview->contentY(), 0.0);
5483     QTRY_COMPARE(rightview->contentY(), 0.0);
5484
5485     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5486     QCOMPARE(delegateVisible(item), true);
5487     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5488     QTRY_COMPARE(delegateVisible(item), true);
5489
5490     QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5491     QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5492
5493     leftview->setCurrentIndex(20);
5494
5495     QTRY_COMPARE(leftview->contentY(), 100.0);
5496     QTRY_COMPARE(rightview->contentY(), 0.0);
5497
5498     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5499     QTRY_COMPARE(delegateVisible(item), false);
5500     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5501     QCOMPARE(delegateVisible(item), true);
5502
5503     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5504     QCOMPARE(delegateVisible(item), true);
5505     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5506     QCOMPARE(delegateVisible(item), false);
5507
5508     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5509     QCOMPARE(delegateVisible(item), false);
5510     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5511     QCOMPARE(delegateVisible(item), true);
5512     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5513     QCOMPARE(delegateVisible(item), true);
5514     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5515     QCOMPARE(delegateVisible(item), false);
5516
5517     model.moveItems(19, 1, 1);
5518     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5519
5520     QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5521     QCOMPARE(delegateVisible(item), false);
5522     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5523     QCOMPARE(delegateVisible(item), true);
5524
5525     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5526     QCOMPARE(delegateVisible(item), true);
5527     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5528     QCOMPARE(delegateVisible(item), false);
5529
5530     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5531     QCOMPARE(delegateVisible(item), false);
5532     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5533     QCOMPARE(delegateVisible(item), true);
5534     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5535     QCOMPARE(delegateVisible(item), true);
5536     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5537     QCOMPARE(delegateVisible(item), false);
5538
5539     model.moveItems(3, 4, 1);
5540     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5541
5542     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5543     QCOMPARE(delegateVisible(item), false);
5544     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5545     QCOMPARE(delegateVisible(item), true);
5546     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5547     QCOMPARE(delegateVisible(item), true);
5548     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5549     QCOMPARE(delegateVisible(item), false);
5550
5551     model.moveItems(4, 3, 1);
5552     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5553
5554     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5555     QCOMPARE(delegateVisible(item), false);
5556     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5557     QCOMPARE(delegateVisible(item), true);
5558     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5559     QCOMPARE(delegateVisible(item), true);
5560     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5561     QCOMPARE(delegateVisible(item), false);
5562
5563     model.moveItems(16, 17, 1);
5564     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5565
5566     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5567     QCOMPARE(delegateVisible(item), false);
5568     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5569     QCOMPARE(delegateVisible(item), true);
5570     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5571     QCOMPARE(delegateVisible(item), true);
5572     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5573     QCOMPARE(delegateVisible(item), false);
5574
5575     model.moveItems(17, 16, 1);
5576     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5577
5578     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5579     QCOMPARE(delegateVisible(item), false);
5580     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5581     QCOMPARE(delegateVisible(item), true);
5582     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5583     QCOMPARE(delegateVisible(item), true);
5584     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5585     QCOMPARE(delegateVisible(item), false);
5586
5587     delete canvas;
5588 }
5589
5590 void tst_QQuickListView::populateTransitions()
5591 {
5592     QFETCH(bool, staticallyPopulate);
5593     QFETCH(bool, dynamicallyPopulate);
5594     QFETCH(bool, usePopulateTransition);
5595
5596     QPointF transitionFrom(-50, -50);
5597     QPointF transitionVia(100, 100);
5598     QaimModel model_transitionFrom;
5599     QaimModel model_transitionVia;
5600
5601     QaimModel model;
5602     if (staticallyPopulate) {
5603         for (int i = 0; i < 30; i++)
5604             model.addItem("item" + QString::number(i), "");
5605     }
5606
5607     QQuickView *canvas = getView();
5608     canvas->rootContext()->setContextProperty("testModel", &model);
5609     canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
5610     canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5611     canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5612     canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5613     canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
5614     canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5615     canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5616     canvas->setSource(testFileUrl("populateTransitions.qml"));
5617     canvas->show();
5618     QTest::qWaitForWindowShown(canvas);
5619
5620     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5621     QVERIFY(listview);
5622     QQuickItem *contentItem = listview->contentItem();
5623     QVERIFY(contentItem);
5624
5625     if (staticallyPopulate && usePopulateTransition) {
5626         QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5627         QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5628     } else if (dynamicallyPopulate) {
5629         QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5630         QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5631     } else {
5632         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5633         QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5634         QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5635     }
5636
5637     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5638     for (int i=0; i < model.count() && i < itemCount; ++i) {
5639         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5640         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5641         QTRY_COMPARE(item->x(), 0.0);
5642         QTRY_COMPARE(item->y(), i*20.0);
5643         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5644         QVERIFY(name != 0);
5645         QTRY_COMPARE(name->text(), model.name(i));
5646     }
5647
5648     listview->setProperty("countPopulateTransitions", 0);
5649     listview->setProperty("countAddTransitions", 0);
5650
5651     // add an item and check this is done with add transition, not populate
5652     model.insertItem(0, "another item", "");
5653     QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5654     QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5655
5656     // clear the model
5657     canvas->rootContext()->setContextProperty("testModel", QVariant());
5658     QTRY_COMPARE(listview->count(), 0);
5659     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5660     listview->setProperty("countPopulateTransitions", 0);
5661     listview->setProperty("countAddTransitions", 0);
5662
5663     // set to a valid model and check populate transition is run a second time
5664     model.clear();
5665     for (int i = 0; i < 30; i++)
5666         model.addItem("item" + QString::number(i), "");
5667     canvas->rootContext()->setContextProperty("testModel", &model);
5668     QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5669     QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5670
5671     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5672     for (int i=0; i < model.count() && i < itemCount; ++i) {
5673         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5674         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5675         QTRY_COMPARE(item->x(), 0.0);
5676         QTRY_COMPARE(item->y(), i*20.0);
5677         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5678         QVERIFY(name != 0);
5679         QTRY_COMPARE(name->text(), model.name(i));
5680     }
5681
5682     // reset model and check populate transition is run again
5683     listview->setProperty("countPopulateTransitions", 0);
5684     listview->setProperty("countAddTransitions", 0);
5685     model.reset();
5686     QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5687     QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5688
5689     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5690     for (int i=0; i < model.count() && i < itemCount; ++i) {
5691         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5692         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5693         QTRY_COMPARE(item->x(), 0.0);
5694         QTRY_COMPARE(item->y(), i*20.0);
5695         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5696         QVERIFY(name != 0);
5697         QTRY_COMPARE(name->text(), model.name(i));
5698     }
5699
5700     releaseView(canvas);
5701 }
5702
5703 void tst_QQuickListView::populateTransitions_data()
5704 {
5705     QTest::addColumn<bool>("staticallyPopulate");
5706     QTest::addColumn<bool>("dynamicallyPopulate");
5707     QTest::addColumn<bool>("usePopulateTransition");
5708
5709     QTest::newRow("static") << true << false << true;
5710     QTest::newRow("static, no populate") << true << false << false;
5711
5712     QTest::newRow("dynamic") << false << true << true;
5713     QTest::newRow("dynamic, no populate") << false << true << false;
5714
5715     QTest::newRow("empty to start with") << false << false << true;
5716     QTest::newRow("empty to start with, no populate") << false << false << false;
5717 }
5718
5719 void tst_QQuickListView::addTransitions()
5720 {
5721     QFETCH(int, initialItemCount);
5722     QFETCH(bool, shouldAnimateTargets);
5723     QFETCH(qreal, contentY);
5724     QFETCH(int, insertionIndex);
5725     QFETCH(int, insertionCount);
5726     QFETCH(ListRange, expectedDisplacedIndexes);
5727
5728     // added items should start here
5729     QPointF targetItems_transitionFrom(-50, -50);
5730
5731     // displaced items should pass through this point
5732     QPointF displacedItems_transitionVia(100, 100);
5733
5734     QaimModel model;
5735     for (int i = 0; i < initialItemCount; i++)
5736         model.addItem("Original item" + QString::number(i), "");
5737     QaimModel model_targetItems_transitionFrom;
5738     QaimModel model_displacedItems_transitionVia;
5739
5740     QQuickView *canvas = getView();
5741     QQmlContext *ctxt = canvas->rootContext();
5742     TestObject *testObject = new TestObject;
5743     ctxt->setContextProperty("testModel", &model);
5744     ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5745     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5746     ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5747     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5748     ctxt->setContextProperty("testObject", testObject);
5749     canvas->setSource(testFileUrl("addTransitions.qml"));
5750     canvas->show();
5751     QTest::qWaitForWindowShown(canvas);
5752
5753     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5754     QTRY_VERIFY(listview != 0);
5755     QQuickItem *contentItem = listview->contentItem();
5756     QVERIFY(contentItem != 0);
5757     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5758
5759     if (contentY != 0) {
5760         listview->setContentY(contentY);
5761         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5762     }
5763
5764     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5765
5766     // only target items that will become visible should be animated
5767     QList<QPair<QString, QString> > newData;
5768     QList<QPair<QString, QString> > expectedTargetData;
5769     QList<int> targetIndexes;
5770     if (shouldAnimateTargets) {
5771         for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5772             newData << qMakePair(QString("New item %1").arg(i), QString(""));
5773
5774             if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {  // only grab visible items
5775                 expectedTargetData << newData.last();
5776                 targetIndexes << i;
5777             }
5778         }
5779         QVERIFY(expectedTargetData.count() > 0);
5780     }
5781
5782     // start animation
5783     if (!newData.isEmpty()) {
5784         model.insertItems(insertionIndex, newData);
5785         QTRY_COMPARE(model.count(), listview->count());
5786     }
5787
5788     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5789
5790     if (shouldAnimateTargets) {
5791         QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5792         QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5793                      expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5794
5795         // check the target and displaced items were animated
5796         model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5797         model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5798
5799         // check attached properties
5800         matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5801         matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5802         matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5803         if (expectedDisplacedIndexes.isValid()) {
5804             // adjust expectedDisplacedIndexes to their final values after the move
5805             QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5806             matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5807             matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5808             matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5809         }
5810
5811     } else {
5812         QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5813         QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5814     }
5815
5816     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5817     int firstVisibleIndex = -1;
5818     int itemCount = items.count();
5819     for (int i=0; i<items.count(); i++) {
5820         if (items[i]->y() >= contentY) {
5821             QQmlExpression e(qmlContext(items[i]), items[i], "index");
5822             firstVisibleIndex = e.evaluate().toInt();
5823             break;
5824         }
5825     }
5826     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5827
5828     // verify all items moved to the correct final positions
5829     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5830         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5831         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5832         QTRY_COMPARE(item->y(), i*20.0);
5833         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5834         QVERIFY(name != 0);
5835         QTRY_COMPARE(name->text(), model.name(i));
5836     }
5837
5838     releaseView(canvas);
5839     delete testObject;
5840 }
5841
5842 void tst_QQuickListView::addTransitions_data()
5843 {
5844     QTest::addColumn<int>("initialItemCount");
5845     QTest::addColumn<qreal>("contentY");
5846     QTest::addColumn<bool>("shouldAnimateTargets");
5847     QTest::addColumn<int>("insertionIndex");
5848     QTest::addColumn<int>("insertionCount");
5849     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5850
5851     // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5852     QTest::newRow("insert 1, just before start")
5853             << 30 << 20.0 << false
5854             << 0 << 1 << ListRange();
5855     QTest::newRow("insert 1, way before start")
5856             << 30 << 20.0 << false
5857             << 0 << 1 << ListRange();
5858     QTest::newRow("insert multiple, just before start")
5859             << 30 << 100.0 << false
5860             << 0 << 3 << ListRange();
5861     QTest::newRow("insert multiple, way before start")
5862             << 30 << 100.0 << false
5863             << 0 << 3 << ListRange();
5864
5865     QTest::newRow("insert 1 at start")
5866             << 30 << 0.0 << true
5867             << 0 << 1 << ListRange(0, 15);
5868     QTest::newRow("insert multiple at start")
5869             << 30 << 0.0 << true
5870             << 0 << 3 << ListRange(0, 15);
5871     QTest::newRow("insert 1 at start, content y not 0")
5872             << 30 << 40.0 << true  // first visible is index 2, so translate the displaced indexes by 2
5873             << 2 << 1 << ListRange(0 + 2, 15 + 2);
5874     QTest::newRow("insert multiple at start, content y not 0")
5875             << 30 << 40.0 << true    // first visible is index 2
5876             << 2 << 3 << ListRange(0 + 2, 15 + 2);
5877
5878     QTest::newRow("insert 1 at start, to empty list")
5879             << 0 << 0.0 << true
5880             << 0 << 1 << ListRange();
5881     QTest::newRow("insert multiple at start, to empty list")
5882             << 0 << 0.0 << true
5883             << 0 << 3 << ListRange();
5884
5885     QTest::newRow("insert 1 at middle")
5886             << 30 << 0.0 << true
5887             << 5 << 1 << ListRange(5, 15);
5888     QTest::newRow("insert multiple at middle")
5889             << 30 << 0.0 << true
5890             << 5 << 3 << ListRange(5, 15);
5891
5892     QTest::newRow("insert 1 at bottom")
5893             << 30 << 0.0 << true
5894             << 15 << 1 << ListRange(15, 15);
5895     QTest::newRow("insert multiple at bottom")
5896             << 30 << 0.0 << true
5897             << 15 << 3 << ListRange(15, 15);
5898     QTest::newRow("insert 1 at bottom, content y not 0")
5899             << 30 << 20.0 * 3 << true
5900             << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5901     QTest::newRow("insert multiple at bottom, content y not 0")
5902             << 30 << 20.0 * 3 << true
5903             << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5904
5905     // items added after the last visible will not be animated in, since they
5906     // do not appear in the final view
5907     QTest::newRow("insert 1 after end")
5908             << 30 << 0.0 << false
5909             << 17 << 1 << ListRange();
5910     QTest::newRow("insert multiple after end")
5911             << 30 << 0.0 << false
5912             << 17 << 3 << ListRange();
5913 }
5914
5915 void tst_QQuickListView::moveTransitions()
5916 {
5917     QFETCH(int, initialItemCount);
5918     QFETCH(qreal, contentY);
5919     QFETCH(qreal, itemsOffsetAfterMove);
5920     QFETCH(int, moveFrom);
5921     QFETCH(int, moveTo);
5922     QFETCH(int, moveCount);
5923     QFETCH(ListRange, expectedDisplacedIndexes);
5924
5925     // target and displaced items should pass through these points
5926     QPointF targetItems_transitionVia(-50, 50);
5927     QPointF displacedItems_transitionVia(100, 100);
5928
5929     QaimModel model;
5930     for (int i = 0; i < initialItemCount; i++)
5931         model.addItem("Original item" + QString::number(i), "");
5932     QaimModel model_targetItems_transitionVia;
5933     QaimModel model_displacedItems_transitionVia;
5934
5935     QQuickView *canvas = getView();
5936     QQmlContext *ctxt = canvas->rootContext();
5937     TestObject *testObject = new TestObject;
5938     ctxt->setContextProperty("testModel", &model);
5939     ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5940     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5941     ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5942     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5943     ctxt->setContextProperty("testObject", testObject);
5944     canvas->setSource(testFileUrl("moveTransitions.qml"));
5945     canvas->show();
5946     QTest::qWaitForWindowShown(canvas);
5947
5948     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5949     QTRY_VERIFY(listview != 0);
5950     QQuickItem *contentItem = listview->contentItem();
5951     QVERIFY(contentItem != 0);
5952     QQuickText *name;
5953
5954     if (contentY != 0) {
5955         listview->setContentY(contentY);
5956         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5957     }
5958
5959     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5960
5961     // Items moving to *or* from visible positions should be animated.
5962     // Otherwise, they should not be animated.
5963     QList<QPair<QString, QString> > expectedTargetData;
5964     QList<int> targetIndexes;
5965     for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5966         int toIndex = moveTo + (i - moveFrom);
5967         if (i <= (contentY + listview->height()) / 20
5968                 || toIndex < (contentY + listview->height()) / 20) {
5969             expectedTargetData << qMakePair(model.name(i), model.number(i));
5970             targetIndexes << i;
5971         }
5972     }
5973     // ViewTransition.index provides the indices that items are moving to, not from
5974     targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5975
5976     // start animation
5977     model.moveItems(moveFrom, moveTo, moveCount);
5978
5979     QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5980     QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5981                  expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5982
5983     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5984
5985     // check the target and displaced items were animated
5986     model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5987     model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5988
5989     // check attached properties
5990     matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5991     matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5992     matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5993     if (expectedDisplacedIndexes.isValid()) {
5994         // adjust expectedDisplacedIndexes to their final values after the move
5995         QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5996         matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5997         matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5998         matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5999     }
6000
6001     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6002     int firstVisibleIndex = -1;
6003     for (int i=0; i<items.count(); i++) {
6004         if (items[i]->y() >= contentY) {
6005             QQmlExpression e(qmlContext(items[i]), items[i], "index");
6006             firstVisibleIndex = e.evaluate().toInt();
6007             break;
6008         }
6009     }
6010     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6011
6012     // verify all items moved to the correct final positions
6013     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
6014     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6015         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6016         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6017         QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
6018         name = findItem<QQuickText>(contentItem, "textName", i);
6019         QVERIFY(name != 0);
6020         QTRY_COMPARE(name->text(), model.name(i));
6021     }
6022
6023     releaseView(canvas);
6024     delete testObject;
6025 }
6026
6027 void tst_QQuickListView::moveTransitions_data()
6028 {
6029     QTest::addColumn<int>("initialItemCount");
6030     QTest::addColumn<qreal>("contentY");
6031     QTest::addColumn<qreal>("itemsOffsetAfterMove");
6032     QTest::addColumn<int>("moveFrom");
6033     QTest::addColumn<int>("moveTo");
6034     QTest::addColumn<int>("moveCount");
6035     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6036
6037     // when removing from above the visible, all items shift down depending on how many
6038     // items have been removed from above the visible
6039     QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
6040             << 1 << 10 << 1 << ListRange(11, 15+4);
6041     QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
6042             << 0 << 10 << 1 << ListRange(11, 15+4);
6043     QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
6044             << 1 << 10 << 2 << ListRange(12, 15+4);
6045     QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
6046             << 0 << 10 << 3 << ListRange(13, 15+4);
6047     QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
6048             << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
6049     QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
6050             << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
6051
6052     QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
6053             << 1 << 10 << 1 << ListRange(2, 10);
6054     QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
6055             << 0 << 10 << 1 << ListRange(1, 10);
6056     QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6057             << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
6058     QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
6059             << 10 << 15 << 1 << ListRange(11, 15);
6060     QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
6061             << 0 << 15 << 1 << ListRange(1, 15);
6062
6063     QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
6064             << 1 << 10 << 3 << ListRange(4, 12);
6065     QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
6066             << 0 << 10 << 3 << ListRange(3, 12);
6067     QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6068             << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
6069     QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
6070             << 5 << 13 << 3 << ListRange(8, 15);
6071     QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
6072             << 0 << 13 << 3 << ListRange(3, 15);
6073
6074     QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
6075             << 10 << 1 << 1 << ListRange(1, 9);
6076     QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
6077             << 10 << 0 << 1 << ListRange(0, 9);
6078     QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6079             << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6080     QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
6081             << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6082     QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
6083             << 15 << 10 << 1 << ListRange(10, 14);
6084     QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
6085             << 15 << 0 << 1 << ListRange(0, 14);
6086
6087     QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
6088             << 10 << 1 << 3 << ListRange(1, 9);
6089     QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
6090             << 10 << 0 << 3 << ListRange(0, 9);
6091     QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6092             << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
6093     QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
6094             << 13 << 5 << 3 << ListRange(5, 12);
6095     QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
6096             << 13 << 0 << 3 << ListRange(0, 12);
6097
6098     QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
6099             << 20 << 0 << 1 << ListRange(0, 15);
6100     QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6101             << 25 << 4 << 1 << ListRange(0+4, 15+4);
6102     QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
6103             << 20 << 0 << 3 << ListRange(0, 15);
6104     QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6105             << 25 << 4 << 3 << ListRange(0+4, 15+4);
6106
6107     QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
6108             << 20 << 15 << 1 << ListRange(15, 15);
6109     QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6110             << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
6111     QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6112             << 20 << 15 << 3 << ListRange(15, 15);
6113     QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6114             << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6115 }
6116
6117 void tst_QQuickListView::removeTransitions()
6118 {
6119     QFETCH(int, initialItemCount);
6120     QFETCH(bool, shouldAnimateTargets);
6121     QFETCH(qreal, contentY);
6122     QFETCH(int, removalIndex);
6123     QFETCH(int, removalCount);
6124     QFETCH(ListRange, expectedDisplacedIndexes);
6125
6126     // added items should end here
6127     QPointF targetItems_transitionTo(-50, -50);
6128
6129     // displaced items should pass through this points
6130     QPointF displacedItems_transitionVia(100, 100);
6131
6132     QaimModel model;
6133     for (int i = 0; i < initialItemCount; i++)
6134         model.addItem("Original item" + QString::number(i), "");
6135     QaimModel model_targetItems_transitionTo;
6136     QaimModel model_displacedItems_transitionVia;
6137
6138     QQuickView *canvas = getView();
6139     QQmlContext *ctxt = canvas->rootContext();
6140     TestObject *testObject = new TestObject;
6141     ctxt->setContextProperty("testModel", &model);
6142     ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6143     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6144     ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6145     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6146     ctxt->setContextProperty("testObject", testObject);
6147     canvas->setSource(testFileUrl("removeTransitions.qml"));
6148     canvas->show();
6149     QTest::qWaitForWindowShown(canvas);
6150
6151     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6152     QTRY_VERIFY(listview != 0);
6153     QQuickItem *contentItem = listview->contentItem();
6154     QVERIFY(contentItem != 0);
6155     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6156
6157     if (contentY != 0) {
6158         listview->setContentY(contentY);
6159         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6160     }
6161
6162     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6163
6164     // only target items that are visible should be animated
6165     QList<QPair<QString, QString> > expectedTargetData;
6166     QList<int> targetIndexes;
6167     if (shouldAnimateTargets) {
6168         for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6169             if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6170                 expectedTargetData << qMakePair(model.name(i), model.number(i));
6171                 targetIndexes << i;
6172             }
6173         }
6174         QVERIFY(expectedTargetData.count() > 0);
6175     }
6176
6177     // calculate targetItems and expectedTargets before model changes
6178     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6179     QVariantMap expectedTargets;
6180     for (int i=0; i<targetIndexes.count(); i++)
6181         expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6182
6183     // start animation
6184     model.removeItems(removalIndex, removalCount);
6185     QTRY_COMPARE(model.count(), listview->count());
6186
6187     if (shouldAnimateTargets) {
6188         QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6189         QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6190                      expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6191
6192         // check the target and displaced items were animated
6193         model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6194         model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6195
6196         // check attached properties
6197         QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6198         matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6199         matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6200         if (expectedDisplacedIndexes.isValid()) {
6201             // adjust expectedDisplacedIndexes to their final values after the move
6202             QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6203             matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6204             matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6205             matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6206         }
6207     } else {
6208         QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6209         QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6210     }
6211
6212     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6213     int firstVisibleIndex = -1;
6214     int itemCount = items.count();
6215
6216     for (int i=0; i<items.count(); i++) {
6217         QQmlExpression e(qmlContext(items[i]), items[i], "index");
6218         int index = e.evaluate().toInt();
6219         if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6220             firstVisibleIndex = index;
6221         if (index < 0)
6222             itemCount--;    // exclude deleted items
6223     }
6224     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6225
6226     // verify all items moved to the correct final positions
6227     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6228         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6229         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6230         QCOMPARE(item->x(), 0.0);
6231         QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6232         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6233         QVERIFY(name != 0);
6234         QTRY_COMPARE(name->text(), model.name(i));
6235     }
6236
6237     releaseView(canvas);
6238     delete testObject;
6239 }
6240
6241 void tst_QQuickListView::removeTransitions_data()
6242 {
6243     QTest::addColumn<int>("initialItemCount");
6244     QTest::addColumn<qreal>("contentY");
6245     QTest::addColumn<bool>("shouldAnimateTargets");
6246     QTest::addColumn<int>("removalIndex");
6247     QTest::addColumn<int>("removalCount");
6248     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6249
6250     // All items that are visible following the remove operation should be animated.
6251     // Remove targets that are outside of the view should not be animated.
6252
6253     QTest::newRow("remove 1 before start")
6254             << 30 << 20.0 * 3 << false
6255             << 2 << 1 << ListRange();
6256     QTest::newRow("remove multiple, all before start")
6257             << 30 << 20.0 * 3 << false
6258             << 0 << 3 << ListRange();
6259     QTest::newRow("remove mix of before and after start")
6260             << 30 << 20.0 * 3 << true
6261             << 2 << 3 << ListRange(5, 20);  // 5-20 are visible after the remove
6262
6263     QTest::newRow("remove 1 from start")
6264             << 30 << 0.0 << true
6265             << 0 << 1 << ListRange(1, 16);  // 1-16 are visible after the remove
6266     QTest::newRow("remove multiple from start")
6267             << 30 << 0.0 << true
6268             << 0 << 3 << ListRange(3, 18);  // 3-18 are visible after the remove
6269     QTest::newRow("remove 1 from start, content y not 0")
6270             << 30 << 20.0 * 2 << true  // first visible is index 2, so translate the displaced indexes by 2
6271             << 2 << 1 << ListRange(1 + 2, 16 + 2);
6272     QTest::newRow("remove multiple from start, content y not 0")
6273             << 30 << 20.0 * 2 << true    // first visible is index 2
6274             << 2 << 3 << ListRange(3 + 2, 18 + 2);
6275
6276     QTest::newRow("remove 1 from middle")
6277             << 30 << 0.0 << true
6278             << 5 << 1 << ListRange(6, 16);
6279     QTest::newRow("remove multiple from middle")
6280             << 30 << 0.0 << true
6281             << 5 << 3 << ListRange(8, 18);
6282
6283
6284     QTest::newRow("remove 1 from bottom")
6285             << 30 << 0.0 << true
6286             << 15 << 1 << ListRange(16, 16);
6287
6288     // remove 15, 16, 17
6289     // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6290     // the view, and 18 will be animated as the displaced item to replace the last item
6291     QTest::newRow("remove multiple from bottom")
6292             << 30 << 0.0 << true
6293             << 15 << 3 << ListRange(18, 18);
6294
6295     QTest::newRow("remove 1 from bottom, content y not 0")
6296             << 30 << 20.0 * 2 << true
6297             << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6298     QTest::newRow("remove multiple from bottom, content y not 0")
6299             << 30 << 20.0 * 2 << true
6300             << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6301
6302
6303     QTest::newRow("remove 1 after end")
6304             << 30 << 0.0 << false
6305             << 17 << 1 << ListRange();
6306     QTest::newRow("remove multiple after end")
6307             << 30 << 0.0 << false
6308             << 17 << 3 << ListRange();
6309 }
6310
6311 void tst_QQuickListView::displacedTransitions()
6312 {
6313     QFETCH(bool, useDisplaced);
6314     QFETCH(bool, displacedEnabled);
6315     QFETCH(bool, useAddDisplaced);
6316     QFETCH(bool, addDisplacedEnabled);
6317     QFETCH(bool, useMoveDisplaced);
6318     QFETCH(bool, moveDisplacedEnabled);
6319     QFETCH(bool, useRemoveDisplaced);
6320     QFETCH(bool, removeDisplacedEnabled);
6321     QFETCH(ListChange, change);
6322     QFETCH(ListRange, expectedDisplacedIndexes);
6323
6324     QaimModel model;
6325     for (int i = 0; i < 30; i++)
6326         model.addItem("Original item" + QString::number(i), "");
6327     QaimModel model_displaced_transitionVia;
6328     QaimModel model_addDisplaced_transitionVia;
6329     QaimModel model_moveDisplaced_transitionVia;
6330     QaimModel model_removeDisplaced_transitionVia;
6331
6332     QPointF displaced_transitionVia(-50, -100);
6333     QPointF addDisplaced_transitionVia(-150, 100);
6334     QPointF moveDisplaced_transitionVia(50, -100);
6335     QPointF removeDisplaced_transitionVia(150, 100);
6336
6337     QQuickView *canvas = getView();
6338     QQmlContext *ctxt = canvas->rootContext();
6339     TestObject *testObject = new TestObject(canvas);
6340     ctxt->setContextProperty("testModel", &model);
6341     ctxt->setContextProperty("testObject", testObject);
6342     ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6343     ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6344     ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6345     ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6346     ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6347     ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6348     ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6349     ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6350     ctxt->setContextProperty("useDisplaced", useDisplaced);
6351     ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6352     ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6353     ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6354     ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6355     ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6356     ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6357     ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6358     canvas->setSource(testFileUrl("displacedTransitions.qml"));
6359     canvas->show();
6360     qApp->processEvents();
6361
6362     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6363     QTRY_VERIFY(listview != 0);
6364     QQuickItem *contentItem = listview->contentItem();
6365     QVERIFY(contentItem != 0);
6366     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6367
6368     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6369     listview->setProperty("displaceTransitionsDone", false);
6370
6371     switch (change.type) {
6372         case ListChange::Inserted:
6373         {
6374             QList<QPair<QString, QString> > targetItemData;
6375             for (int i=change.index; i<change.index + change.count; ++i)
6376                 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6377             model.insertItems(change.index, targetItemData);
6378             QTRY_COMPARE(model.count(), listview->count());
6379             break;
6380         }
6381         case ListChange::Removed:
6382             model.removeItems(change.index, change.count);
6383             QTRY_COMPARE(model.count(), listview->count());
6384             break;
6385         case ListChange::Moved:
6386             model.moveItems(change.index, change.to, change.count);
6387             QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6388             break;
6389         case ListChange::SetCurrent:
6390         case ListChange::SetContentY:
6391         case ListChange::Polish:
6392             break;
6393     }
6394
6395     QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6396     QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6397
6398     if ((useDisplaced && displacedEnabled)
6399             || (useAddDisplaced && addDisplacedEnabled)
6400             || (useMoveDisplaced && moveDisplacedEnabled)
6401             || (useRemoveDisplaced && removeDisplacedEnabled)) {
6402         QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6403
6404         // check the correct number of target items and indexes were received
6405         QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6406         for (int i=0; i<resultTargetIndexes.count(); i++)
6407             QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6408         QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6409         for (int i=0; i<resultTargetItems.count(); i++)
6410             QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6411     } else {
6412         QCOMPARE(resultTargetIndexes.count(), 0);
6413         QCOMPARE(resultTargetItems.count(), 0);
6414     }
6415
6416     if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6417         model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6418     else
6419         QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6420     if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6421         model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6422     else
6423         QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6424     if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6425         model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6426     else
6427         QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6428
6429     if (useDisplaced && displacedEnabled
6430             && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6431                  || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6432                  || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6433         model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6434     } else {
6435         QCOMPARE(model_displaced_transitionVia.count(), 0);
6436     }
6437
6438     // verify all items moved to the correct final positions
6439     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6440     for (int i=0; i < model.count() && i < items.count(); ++i) {
6441         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6442         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6443         QCOMPARE(item->x(), 0.0);
6444         QCOMPARE(item->y(), i * 20.0);
6445         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6446         QVERIFY(name != 0);
6447         QTRY_COMPARE(name->text(), model.name(i));
6448     }
6449
6450     releaseView(canvas);
6451 }
6452
6453 void tst_QQuickListView::displacedTransitions_data()
6454 {
6455     QTest::addColumn<bool>("useDisplaced");
6456     QTest::addColumn<bool>("displacedEnabled");
6457     QTest::addColumn<bool>("useAddDisplaced");
6458     QTest::addColumn<bool>("addDisplacedEnabled");
6459     QTest::addColumn<bool>("useMoveDisplaced");
6460     QTest::addColumn<bool>("moveDisplacedEnabled");
6461     QTest::addColumn<bool>("useRemoveDisplaced");
6462     QTest::addColumn<bool>("removeDisplacedEnabled");
6463     QTest::addColumn<ListChange>("change");
6464     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6465
6466     QTest::newRow("no displaced transitions at all")
6467             << false << false
6468             << false << false
6469             << false << false
6470             << false << false
6471             << ListChange::insert(0, 1) << ListRange(0, 15);
6472
6473     QTest::newRow("just displaced")
6474             << true << true
6475             << false << false
6476             << false << false
6477             << false << false
6478             << ListChange::insert(0, 1) << ListRange(0, 15);
6479
6480     QTest::newRow("just displaced (not enabled)")
6481             << true << false
6482             << false << false
6483             << false << false
6484             << false << false
6485             << ListChange::insert(0, 1) << ListRange(0, 15);
6486
6487     QTest::newRow("displaced + addDisplaced")
6488             << true << true
6489             << true << true
6490             << false << false
6491             << false << false
6492             << ListChange::insert(0, 1) << ListRange(0, 15);
6493
6494     QTest::newRow("displaced + addDisplaced (not enabled)")
6495             << true << true
6496             << true << false
6497             << false << false
6498             << false << false
6499             << ListChange::insert(0, 1) << ListRange(0, 15);
6500
6501     QTest::newRow("displaced + moveDisplaced")
6502             << true << true
6503             << false << false
6504             << true << true
6505             << false << false
6506             << ListChange::move(0, 10, 1) << ListRange(1, 10);
6507
6508     QTest::newRow("displaced + moveDisplaced (not enabled)")
6509             << true << true
6510             << false << false
6511             << true << false
6512             << false << false
6513             << ListChange::move(0, 10, 1) << ListRange(1, 10);
6514
6515     QTest::newRow("displaced + removeDisplaced")
6516             << true << true
6517             << false << false
6518             << false << false
6519             << true << true
6520             << ListChange::remove(0, 1) << ListRange(1, 16);
6521
6522     QTest::newRow("displaced + removeDisplaced (not enabled)")
6523             << true << true
6524             << false << false
6525             << false << false
6526             << true << false
6527             << ListChange::remove(0, 1) << ListRange(1, 16);
6528
6529
6530     QTest::newRow("displaced + add, should use generic displaced for a remove")
6531             << true << true
6532             << true << true
6533             << false << false
6534             << true << false
6535             << ListChange::remove(0, 1) << ListRange(1, 16);
6536 }
6537
6538 void tst_QQuickListView::multipleTransitions()
6539 {
6540     // Tests that if you interrupt a transition in progress with another action that
6541     // cancels the previous transition, the resulting items are still placed correctly.
6542
6543     QFETCH(int, initialCount);
6544     QFETCH(qreal, contentY);
6545     QFETCH(QList<ListChange>, changes);
6546     QFETCH(bool, enableAddTransitions);
6547     QFETCH(bool, enableMoveTransitions);
6548     QFETCH(bool, enableRemoveTransitions);
6549     QFETCH(bool, rippleAddDisplaced);
6550
6551     QPointF addTargets_transitionFrom(-50, -50);
6552     QPointF addDisplaced_transitionFrom(-50, 50);
6553     QPointF moveTargets_transitionFrom(50, -50);
6554     QPointF moveDisplaced_transitionFrom(50, 50);
6555     QPointF removeTargets_transitionTo(-100, 300);
6556     QPointF removeDisplaced_transitionFrom(100, 300);
6557
6558     QmlListModel model;
6559     for (int i = 0; i < initialCount; i++)
6560         model.addItem("Original item" + QString::number(i), "");
6561
6562     QQuickView *canvas = getView();
6563     QQmlContext *ctxt = canvas->rootContext();
6564     TestObject *testObject = new TestObject;
6565     ctxt->setContextProperty("testModel", &model);
6566     ctxt->setContextProperty("testObject", testObject);
6567     ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6568     ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6569     ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6570     ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6571     ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6572     ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6573     ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6574     ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6575     ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6576     ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6577     canvas->setSource(testFileUrl("multipleTransitions.qml"));
6578     canvas->show();
6579     QTest::qWaitForWindowShown(canvas);
6580
6581     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6582     QTRY_VERIFY(listview != 0);
6583     QQuickItem *contentItem = listview->contentItem();
6584     QVERIFY(contentItem != 0);
6585     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6586
6587     if (contentY != 0) {
6588         listview->setContentY(contentY);
6589         QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6590     }
6591
6592     int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
6593
6594     for (int i=0; i<changes.count(); i++) {
6595         switch (changes[i].type) {
6596             case ListChange::Inserted:
6597             {
6598                 QList<QPair<QString, QString> > targetItems;
6599                 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6600                     targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6601                 model.insertItems(changes[i].index, targetItems);
6602                 QTRY_COMPARE(model.count(), listview->count());
6603                 if (i == changes.count() - 1) {
6604                     QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6605                     QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6606                 } else {
6607                     QTest::qWait(timeBetweenActions);
6608                 }
6609                 break;
6610             }
6611             case ListChange::Removed:
6612                 model.removeItems(changes[i].index, changes[i].count);
6613                 QTRY_COMPARE(model.count(), listview->count());
6614                 if (i == changes.count() - 1) {
6615                     QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6616                     QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6617                 } else {
6618                     QTest::qWait(timeBetweenActions);
6619                 }
6620                 break;
6621             case ListChange::Moved:
6622                 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6623                 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6624                 if (i == changes.count() - 1) {
6625                     QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6626                     QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6627                 } else {
6628                     QTest::qWait(timeBetweenActions);
6629                 }
6630                 break;
6631             case ListChange::SetCurrent:
6632                 listview->setCurrentIndex(changes[i].index);
6633                 break;
6634             case ListChange::SetContentY:
6635                 listview->setContentY(changes[i].pos);
6636                 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6637                 break;
6638             case ListChange::Polish:
6639                 break;
6640         }
6641     }
6642     QCOMPARE(listview->count(), model.count());
6643
6644     // verify all items moved to the correct final positions
6645     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6646     for (int i=0; i < model.count() && i < items.count(); ++i) {
6647         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6648         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6649         QTRY_COMPARE(item->x(), 0.0);
6650         QTRY_COMPARE(item->y(), i*20.0);
6651         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6652         QVERIFY(name != 0);
6653         QTRY_COMPARE(name->text(), model.name(i));
6654     }
6655
6656     releaseView(canvas);
6657     delete testObject;
6658 }
6659
6660 void tst_QQuickListView::multipleTransitions_data()
6661 {
6662     QTest::addColumn<int>("initialCount");
6663     QTest::addColumn<qreal>("contentY");
6664     QTest::addColumn<QList<ListChange> >("changes");
6665     QTest::addColumn<bool>("enableAddTransitions");
6666     QTest::addColumn<bool>("enableMoveTransitions");
6667     QTest::addColumn<bool>("enableRemoveTransitions");
6668     QTest::addColumn<bool>("rippleAddDisplaced");
6669
6670     // the added item and displaced items should move to final dest correctly
6671     QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6672             << ListChange::insert(0, 1)
6673             << ListChange::move(0, 3, 1)
6674             )
6675             << true << true << true << false;
6676
6677     // items affected by the add should change from move to add transition
6678     QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6679             << ListChange::move(1, 10, 3)
6680             << ListChange::insert(0, 1)
6681             )
6682             << true << true << true << false;
6683
6684     // items should be placed correctly if you trigger a transition then refill for that index
6685     QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6686             << ListChange::insert(0, 1)
6687             << ListChange::setContentY(80.0)
6688             << ListChange::setContentY(0.0)
6689             << ListChange::insert(0, 1)
6690             )
6691             << true << true << true << false;
6692
6693     QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6694             << ListChange::insert(1, 1)
6695             << ListChange::remove(1, 1)
6696             )
6697             << true << true << true << true;
6698
6699     // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6700     // even if a remove-displace transition is not present to re-animate them
6701     QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6702             << ListChange::insert(0, 1)
6703             << ListChange::remove(2, 1)
6704             )
6705             << true << true << false << false;
6706
6707     // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6708     // remove has changed the position of where it will move to
6709     QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6710             << ListChange::setContentY(-10.0)
6711             << ListChange::insert(0, 1)
6712             << ListChange::insert(0, 1)
6713             << ListChange::remove(2, 1)
6714             )
6715             << true << true << false << false;
6716 }
6717
6718 void tst_QQuickListView::multipleDisplaced()
6719 {
6720     // multiple move() operations should only restart displace transitions for items that
6721     // moved from previously set positions, and not those that have moved from their current
6722     // item positions (which may e.g. still be changing from easing bounces in the last transition)
6723
6724     QmlListModel model;
6725     for (int i = 0; i < 30; i++)
6726         model.addItem("Original item" + QString::number(i), "");
6727
6728     QQuickView *canvas = getView();
6729     QQmlContext *ctxt = canvas->rootContext();
6730     ctxt->setContextProperty("testModel", &model);
6731     ctxt->setContextProperty("testObject", new TestObject(canvas));
6732     canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6733     canvas->show();
6734     QTest::qWaitForWindowShown(canvas);
6735
6736     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6737     QTRY_VERIFY(listview != 0);
6738     QQuickItem *contentItem = listview->contentItem();
6739     QVERIFY(contentItem != 0);
6740     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6741
6742     model.moveItems(12, 8, 1);
6743     QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6744     model.moveItems(8, 3, 1);
6745     QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6746
6747     QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6748     foreach (const QString &name, transitionsStarted.keys()) {
6749         QVERIFY2(transitionsStarted[name] == 1,
6750                  QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6751     }
6752
6753     // verify all items moved to the correct final positions
6754     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6755     for (int i=0; i < model.count() && i < items.count(); ++i) {
6756         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6757         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6758         QTRY_COMPARE(item->x(), 0.0);
6759         QTRY_COMPARE(item->y(), i*20.0);
6760         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6761         QVERIFY(name != 0);
6762         QTRY_COMPARE(name->text(), model.name(i));
6763     }
6764
6765     releaseView(canvas);
6766 }
6767
6768 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6769 {
6770     QList<int> ret;
6771     bool ok = true;
6772     for (int i=0; i<list.count(); i++) {
6773         ret << list[i].toInt(&ok);
6774         if (!ok)
6775             qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6776     }
6777
6778     return ret;
6779 }
6780
6781 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6782 {
6783     for (int i=0; i<indexLists.count(); i++) {
6784         QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6785         if (current != expectedIndexes.toSet())
6786             qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6787         QCOMPARE(current, expectedIndexes.toSet());
6788     }
6789 }
6790
6791 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6792 {
6793     for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6794         QVERIFY(it.value().type() == QVariant::Int);
6795         QString name = it.key();
6796         int itemIndex = it.value().toInt();
6797         QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6798         if (model.name(itemIndex) != name)
6799             qDebug() << itemIndex;
6800         QCOMPARE(model.name(itemIndex), name);
6801     }
6802     QCOMPARE(items.count(), expectedIndexes.count());
6803 }
6804
6805 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6806 {
6807     for (int i=0; i<itemLists.count(); i++) {
6808         QVERIFY(itemLists[i].type() == QVariant::List);
6809         QVariantList current = itemLists[i].toList();
6810         for (int j=0; j<current.count(); j++) {
6811             QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6812             QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6813             QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6814         }
6815         QCOMPARE(current.count(), expectedItems.count());
6816     }
6817 }
6818
6819 void tst_QQuickListView::flickBeyondBounds()
6820 {
6821     QQuickView *canvas = createView();
6822
6823     canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6824     canvas->show();
6825     qApp->processEvents();
6826
6827     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6828     QTRY_VERIFY(listview != 0);
6829
6830     QQuickItem *contentItem = listview->contentItem();
6831     QTRY_VERIFY(contentItem != 0);
6832     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6833
6834     // Flick view up beyond bounds
6835     flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6836     QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6837
6838     // We're really testing that we don't get stuck in a loop,
6839     // but also confirm items positioned correctly.
6840     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6841     for (int i = 0; i < 2; ++i) {
6842         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6843         if (!item) qWarning() << "Item" << i << "not found";
6844         QTRY_VERIFY(item);
6845         QTRY_VERIFY(item->y() == i*45);
6846     }
6847
6848     delete canvas;
6849 }
6850
6851 void tst_QQuickListView::destroyItemOnCreation()
6852 {
6853     QmlListModel model;
6854     QQuickView *canvas = createView();
6855     canvas->rootContext()->setContextProperty("testModel", &model);
6856
6857     canvas->setSource(testFileUrl("destroyItemOnCreation.qml"));
6858     canvas->show();
6859     qApp->processEvents();
6860
6861     QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6862     QVERIFY(listview != 0);
6863
6864     QQuickItem *contentItem = listview->contentItem();
6865     QVERIFY(contentItem != 0);
6866     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6867
6868     QCOMPARE(canvas->rootObject()->property("createdIndex").toInt(), -1);
6869     model.addItem("new item", "");
6870     QTRY_COMPARE(canvas->rootObject()->property("createdIndex").toInt(), 0);
6871
6872     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6873     QCOMPARE(model.count(), 0);
6874
6875     delete canvas;
6876 }
6877
6878 void tst_QQuickListView::parentBinding()
6879 {
6880     QQuickView *canvas = createView();
6881
6882     m_errorCount = 0;
6883     QtMsgHandler old = qInstallMsgHandler(errorMsgHandler);
6884
6885     canvas->setSource(testFileUrl("parentBinding.qml"));
6886     canvas->show();
6887     QTest::qWaitForWindowExposed(canvas);
6888
6889     QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
6890     QVERIFY(listview != 0);
6891
6892     QQuickItem *contentItem = listview->contentItem();
6893     QVERIFY(contentItem != 0);
6894     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6895
6896     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6897     QVERIFY(item);
6898     QCOMPARE(item->width(), listview->width());
6899     QCOMPARE(item->height(), listview->height()/12);
6900
6901     // there should be no transient binding error
6902     QVERIFY(!m_errorCount);
6903
6904     qInstallMsgHandler(old);
6905
6906     delete canvas;
6907 }
6908
6909 QTEST_MAIN(tst_QQuickListView)
6910
6911 #include "tst_qquicklistview.moc"
6912
6913