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