1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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"
59 Q_DECLARE_METATYPE(Qt::LayoutDirection)
60 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
61 Q_DECLARE_METATYPE(QQuickListView::Orientation)
62 Q_DECLARE_METATYPE(Qt::Key)
64 using namespace QQuickViewTestUtil;
65 using namespace QQuickVisualTestUtil;
69 class tst_QQuickListView : public QQmlDataTest
77 void cleanupTestCase();
78 // Test both QListModelInterface and QAbstractItemModel model types
79 void qListModelInterface_items();
80 void qListModelInterface_package_items();
81 void qAbstractItemModel_items();
83 void qListModelInterface_changed();
84 void qListModelInterface_package_changed();
85 void qAbstractItemModel_changed();
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();
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();
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();
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(); }
121 void qListModelInterface_clear();
122 void qListModelInterface_package_clear();
123 void qAbstractItemModel_clear();
124 void qAbstractItemModel_clear_bottomToTop();
126 void insertBeforeVisible();
127 void insertBeforeVisible_data();
128 void swapWithFirstItem();
130 void itemListFlicker();
131 void currentIndex_delayedItemCreation();
132 void currentIndex_delayedItemCreation_data();
134 void noCurrentIndex();
135 void keyNavigation();
136 void keyNavigation_data();
138 void enforceRange_withoutHighlight();
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();
151 void positionViewAtIndex();
153 void propertyChanges();
154 void componentChanges();
156 void manualHighlight();
157 void initialZValues();
160 void header_delayItemCreation();
165 void resetModel_headerFooter();
167 void resizeViewAndRepaint();
168 void sizeLessThan1();
170 void resizeDelegate();
171 void resizeFirstDelegate();
172 void repositionResizedDelegate();
173 void repositionResizedDelegate_data();
175 void indexAt_itemAt_data();
176 void indexAt_itemAt();
177 void incrementalModel();
181 void onRemove_data();
183 void test_mirroring();
185 void marginsResize();
186 void marginsResize_data();
187 void creationContext();
188 void snapToItem_data();
190 void snapOneItem_data();
198 void unrequestedVisibility();
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();
214 void flickBeyondBounds();
215 void destroyItemOnCreation();
217 void parentBinding();
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);
230 void multipleChanges(bool condensed);
231 void multipleChanges_data();
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);
238 void inserted_more_data();
239 void removed_more_data();
243 QQuickView *getView() {
245 if (QString(QTest::currentTestFunction()) != testForView) {
249 m_view->setSource(QUrl());
254 testForView = QTest::currentTestFunction();
255 m_view = createView();
258 void releaseView(QQuickView *view) {
259 Q_ASSERT(view == m_view);
260 m_view->setSource(QUrl());
263 QQuickView *getView() {
266 void releaseView(QQuickView *view) {
271 static void errorMsgHandler(QtMsgType, const char *)
278 static int m_errorCount;
281 int tst_QQuickListView::m_errorCount = 0;
283 class TestObject : public QObject
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)
293 TestObject(QObject *parent = 0)
294 : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
297 bool error() const { return mError; }
298 void setError(bool err) { mError = err; emit changedError(); }
300 bool animate() const { return mAnimate; }
301 void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
303 bool invalidHighlight() const { return mInvalidHighlight; }
304 void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
306 int cacheBuffer() const { return mCacheBuffer; }
307 void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
313 void changedCacheBuffer();
318 bool mInvalidHighlight;
322 tst_QQuickListView::tst_QQuickListView() : m_view(0)
326 void tst_QQuickListView::init()
329 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
330 testForView = QString();
337 void tst_QQuickListView::cleanupTestCase()
340 testForView = QString();
347 void tst_QQuickListView::items(const QUrl &source, bool forceLayout)
349 QQuickView *canvas = createView();
352 model.addItem("Fred", "12345");
353 model.addItem("John", "2345");
354 model.addItem("Bob", "54321");
356 QQmlContext *ctxt = canvas->rootContext();
357 ctxt->setContextProperty("testModel", &model);
359 TestObject *testObject = new TestObject;
360 ctxt->setContextProperty("testObject", testObject);
362 canvas->setSource(source);
363 qApp->processEvents();
365 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
366 QTRY_VERIFY(listview != 0);
368 QQuickItem *contentItem = listview->contentItem();
369 QTRY_VERIFY(contentItem != 0);
371 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
372 QTRY_VERIFY(testObject->error() == false);
374 QTRY_VERIFY(listview->highlightItem() != 0);
375 QTRY_COMPARE(listview->count(), model.count());
376 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
377 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
379 // current item should be first item
380 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
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));
391 // switch to other delegate
392 testObject->setAnimate(true);
393 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
394 QTRY_VERIFY(testObject->error() == false);
395 QTRY_VERIFY(listview->currentItem());
397 // set invalid highlight
398 testObject->setInvalidHighlight(true);
399 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
400 QTRY_VERIFY(testObject->error() == false);
401 QTRY_VERIFY(listview->currentItem());
402 QTRY_VERIFY(listview->highlightItem() == 0);
404 // back to normal highlight
405 testObject->setInvalidHighlight(false);
406 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
407 QTRY_VERIFY(testObject->error() == false);
408 QTRY_VERIFY(listview->currentItem());
409 QTRY_VERIFY(listview->highlightItem() != 0);
411 // set an empty model and confirm that items are destroyed
413 ctxt->setContextProperty("testModel", &model2);
415 // Force a layout, necessary if ListView is completed before VisualDataModel.
417 QCOMPARE(listview->property("count").toInt(), 0);
419 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
420 QTRY_VERIFY(itemCount == 0);
422 QTRY_COMPARE(listview->highlightResizeVelocity(), 1000.0);
423 QTRY_COMPARE(listview->highlightMoveVelocity(), 1000.0);
431 void tst_QQuickListView::changed(const QUrl &source, bool forceLayout)
433 QQuickView *canvas = createView();
436 model.addItem("Fred", "12345");
437 model.addItem("John", "2345");
438 model.addItem("Bob", "54321");
440 QQmlContext *ctxt = canvas->rootContext();
441 ctxt->setContextProperty("testModel", &model);
443 TestObject *testObject = new TestObject;
444 ctxt->setContextProperty("testObject", testObject);
446 canvas->setSource(source);
447 qApp->processEvents();
449 QQuickFlickable *listview = findItem<QQuickFlickable>(canvas->rootObject(), "list");
450 QTRY_VERIFY(listview != 0);
452 QQuickItem *contentItem = listview->contentItem();
453 QTRY_VERIFY(contentItem != 0);
455 // Force a layout, necessary if ListView is completed before VisualDataModel.
457 QCOMPARE(listview->property("count").toInt(), model.count());
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));
472 void tst_QQuickListView::inserted(const QUrl &source)
474 QQuickView *canvas = createView();
478 model.addItem("Fred", "12345");
479 model.addItem("John", "2345");
480 model.addItem("Bob", "54321");
482 QQmlContext *ctxt = canvas->rootContext();
483 ctxt->setContextProperty("testModel", &model);
485 TestObject *testObject = new TestObject;
486 ctxt->setContextProperty("testObject", testObject);
488 canvas->setSource(source);
489 qApp->processEvents();
491 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
492 QTRY_VERIFY(listview != 0);
494 QQuickItem *contentItem = listview->contentItem();
495 QTRY_VERIFY(contentItem != 0);
497 model.insertItem(1, "Will", "9876");
499 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
500 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
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));
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);
515 model.insertItem(0, "Foo", "1111"); // zero index, and current item
517 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
518 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
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));
527 QTRY_COMPARE(listview->currentIndex(), 1);
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);
535 for (int i = model.count(); i < 30; ++i)
536 model.insertItem(i, "Hello", QString::number(i));
538 listview->setContentY(80);
540 // Insert item outside visible area
541 model.insertItem(1, "Hello", "1324");
543 QTRY_VERIFY(listview->contentY() == 80);
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";
550 QTRY_COMPARE(item->y(), i*20.0 - 20.0);
553 // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
557 model.insertItem(0, "Hello", "1234");
558 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
560 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
562 QCOMPARE(item->y(), 0.);
563 QTRY_VERIFY(listview->contentY() == 0);
570 void tst_QQuickListView::inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
572 QFETCH(qreal, contentY);
573 QFETCH(int, insertIndex);
574 QFETCH(int, insertCount);
575 QFETCH(qreal, itemsOffsetAfterMove);
578 for (int i = 0; i < 30; i++)
579 model.addItem("Item" + QString::number(i), "");
581 QQuickView *canvas = getView();
582 QQmlContext *ctxt = canvas->rootContext();
583 ctxt->setContextProperty("testModel", &model);
585 TestObject *testObject = new TestObject;
586 ctxt->setContextProperty("testObject", testObject);
588 canvas->setSource(testFileUrl("listviewtest.qml"));
590 qApp->processEvents();
591 QTest::qWaitForWindowShown(canvas);
593 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
594 QTRY_VERIFY(listview != 0);
595 QQuickItem *contentItem = listview->contentItem();
596 QTRY_VERIFY(contentItem != 0);
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;
604 listview->setContentY(contentY);
606 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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());
614 // check visibleItems.first() is in correct position
615 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
617 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
618 QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
620 QCOMPARE(item0->y(), itemsOffsetAfterMove);
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;
631 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
633 // Confirm items positioned correctly and indexes correct
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);
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));
655 void tst_QQuickListView::inserted_more_data()
657 QTest::addColumn<qreal>("contentY");
658 QTest::addColumn<int>("insertIndex");
659 QTest::addColumn<int>("insertCount");
660 QTest::addColumn<qreal>("itemsOffsetAfterMove");
662 QTest::newRow("add 1, before visible items")
665 << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move
667 QTest::newRow("add multiple, before visible")
670 << -20.0 * 3; // again first visible should not move
672 QTest::newRow("add 1, at start of visible, content at start")
677 QTest::newRow("add multiple, start of visible, content at start")
682 QTest::newRow("add 1, at start of visible, content not at start")
687 QTest::newRow("add multiple, at start of visible, content not at start")
693 QTest::newRow("add 1, at end of visible, content at start")
698 QTest::newRow("add 1, at end of visible, content at start")
703 QTest::newRow("add 1, at end of visible, content not at start")
708 QTest::newRow("add multiple, at end of visible, content not at start")
714 QTest::newRow("add 1, after visible, content at start")
719 QTest::newRow("add 1, after visible, content at start")
724 QTest::newRow("add 1, after visible, content not at start")
729 QTest::newRow("add multiple, after visible, content not at start")
735 void tst_QQuickListView::insertBeforeVisible()
737 QFETCH(int, insertIndex);
738 QFETCH(int, insertCount);
739 QFETCH(int, cacheBuffer);
742 QQuickView *canvas = getView();
745 for (int i = 0; i < 30; i++)
746 model.addItem("Item" + QString::number(i), "");
748 QQmlContext *ctxt = canvas->rootContext();
749 ctxt->setContextProperty("testModel", &model);
751 TestObject *testObject = new TestObject;
752 ctxt->setContextProperty("testObject", testObject);
754 canvas->setSource(testFileUrl("listviewtest.qml"));
756 qApp->processEvents();
757 QTest::qWaitForWindowShown(canvas);
759 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
760 QTRY_VERIFY(listview != 0);
761 QQuickItem *contentItem = listview->contentItem();
762 QTRY_VERIFY(contentItem != 0);
764 listview->setCacheBuffer(cacheBuffer);
765 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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);
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);
777 QCOMPARE(item->y(), listview->contentY());
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());
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);
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);
800 QTRY_COMPARE(name->text(), model.name(i));
807 void tst_QQuickListView::insertBeforeVisible_data()
809 QTest::addColumn<int>("insertIndex");
810 QTest::addColumn<int>("insertCount");
811 QTest::addColumn<int>("cacheBuffer");
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;
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;
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;
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;
831 void tst_QQuickListView::removed(const QUrl &source, bool /* animated */)
833 QQuickView *canvas = createView();
836 for (int i = 0; i < 50; i++)
837 model.addItem("Item" + QString::number(i), "");
839 QQmlContext *ctxt = canvas->rootContext();
840 ctxt->setContextProperty("testModel", &model);
842 TestObject *testObject = new TestObject;
843 ctxt->setContextProperty("testObject", testObject);
845 canvas->setSource(source);
847 qApp->processEvents();
848 QTest::qWaitForWindowShown(canvas);
850 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
851 QTRY_VERIFY(listview != 0);
852 QQuickItem *contentItem = listview->contentItem();
853 QTRY_VERIFY(contentItem != 0);
854 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
857 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
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));
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";
872 QTRY_VERIFY(item->y() == i*20);
875 // Remove first item (which is the current item);
877 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
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));
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";
892 QTRY_COMPARE(item->y(),i*20.0);
895 // Remove items not visible
896 model.removeItem(18);
897 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
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";
905 QTRY_COMPARE(item->y(),i*20.0);
908 // Remove items before visible
909 listview->setContentY(80);
910 listview->setCurrentIndex(10);
912 model.removeItem(1); // post: top item will be at 20
913 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
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";
920 QTRY_COMPARE(item->y(),20+i*20.0);
923 // Remove current index
924 QTRY_VERIFY(listview->currentIndex() == 9);
925 QQuickItem *oldCurrent = listview->currentItem();
928 QTRY_COMPARE(listview->currentIndex(), 9);
929 QTRY_VERIFY(listview->currentItem() != oldCurrent);
931 listview->setContentY(20); // That's the top now
932 // let transitions settle.
933 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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";
941 QTRY_COMPARE(item->y(),20+i*20.0);
944 // remove current item beyond visible items.
945 listview->setCurrentIndex(20);
946 listview->setContentY(40);
947 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
949 model.removeItem(20);
950 QTRY_COMPARE(listview->currentIndex(), 20);
951 QTRY_VERIFY(listview->currentItem() != 0);
953 // remove item before current, but visible
954 listview->setCurrentIndex(8);
955 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
956 oldCurrent = listview->currentItem();
959 QTRY_COMPARE(listview->currentIndex(), 7);
960 QTRY_VERIFY(listview->currentItem() == oldCurrent);
962 listview->setContentY(80);
963 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
965 // remove all visible items
966 model.removeItems(1, 18);
967 QTRY_COMPARE(listview->count() , model.count());
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";
975 QTRY_COMPARE(item->y(),80+i*20.0);
978 model.removeItems(1, 17);
979 QTRY_COMPARE(listview->count() , model.count());
981 model.removeItems(2, 1);
982 QTRY_COMPARE(listview->count() , model.count());
984 model.addItem("New", "1");
985 QTRY_COMPARE(listview->count() , model.count());
987 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", model.count()-1));
988 QCOMPARE(name->text(), QString("New"));
990 // Add some more items so that we don't run out
992 for (int i = 0; i < 50; i++)
993 model.addItem("Item" + QString::number(i), "");
996 listview->setCurrentIndex(0);
997 listview->setContentY(30);
999 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", 0));
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);
1012 void tst_QQuickListView::removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1014 QFETCH(qreal, contentY);
1015 QFETCH(int, removeIndex);
1016 QFETCH(int, removeCount);
1017 QFETCH(qreal, itemsOffsetAfterMove);
1019 QQuickView *canvas = getView();
1022 for (int i = 0; i < 30; i++)
1023 model.addItem("Item" + QString::number(i), "");
1025 QQmlContext *ctxt = canvas->rootContext();
1026 ctxt->setContextProperty("testModel", &model);
1028 TestObject *testObject = new TestObject;
1029 ctxt->setContextProperty("testObject", testObject);
1031 canvas->setSource(source);
1033 qApp->processEvents();
1034 QTest::qWaitForWindowShown(canvas);
1036 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1037 QTRY_VERIFY(listview != 0);
1038 QQuickItem *contentItem = listview->contentItem();
1039 QTRY_VERIFY(contentItem != 0);
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;
1047 listview->setContentY(contentY);
1049 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1051 model.removeItems(removeIndex, removeCount);
1052 QTRY_COMPARE(listview->property("count").toInt(), model.count());
1054 // check visibleItems.first() is in correct position
1055 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
1058 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1059 QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
1061 QCOMPARE(item0->y(), itemsOffsetAfterMove);
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;
1072 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1074 // Confirm items positioned correctly and indexes correct
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);
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));
1092 releaseView(canvas);
1096 void tst_QQuickListView::removed_more_data()
1098 QTest::addColumn<qreal>("contentY");
1099 QTest::addColumn<int>("removeIndex");
1100 QTest::addColumn<int>("removeCount");
1101 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1103 QTest::newRow("remove 1, before visible items")
1104 << 80.0 // show 4-19
1106 << 20.0; // visible items slide down by 1 item so that first visible does not move
1108 QTest::newRow("remove multiple, all before visible items")
1113 QTest::newRow("remove multiple, all before visible items, remove item 0")
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")
1123 << 20.0 * 3; // adjust for the 3 items removed before the visible
1125 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1128 << 20.0 * 4; // adjust for the 3 items removed before the visible
1131 QTest::newRow("remove 1, from start of visible, content at start")
1136 QTest::newRow("remove multiple, from start of visible, content at start")
1141 QTest::newRow("remove 1, from start of visible, content not at start")
1142 << 80.0 // show 4-19
1146 QTest::newRow("remove multiple, from start of visible, content not at start")
1147 << 80.0 // show 4-19
1152 QTest::newRow("remove 1, from middle of visible, content at start")
1157 QTest::newRow("remove multiple, from middle of visible, content at start")
1162 QTest::newRow("remove 1, from middle of visible, content not at start")
1163 << 80.0 // show 4-19
1167 QTest::newRow("remove multiple, from middle of visible, content not at start")
1168 << 80.0 // show 4-19
1173 QTest::newRow("remove 1, after visible, content at start")
1178 QTest::newRow("remove multiple, after visible, content at start")
1183 QTest::newRow("remove 1, after visible, content not at middle")
1184 << 80.0 // show 4-19
1188 QTest::newRow("remove multiple, after visible, content not at start")
1189 << 80.0 // show 4-19
1193 QTest::newRow("remove multiple, mix of items from within and after visible items")
1200 void tst_QQuickListView::clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1202 QQuickView *canvas = createView();
1205 for (int i = 0; i < 30; i++)
1206 model.addItem("Item" + QString::number(i), "");
1208 QQmlContext *ctxt = canvas->rootContext();
1209 ctxt->setContextProperty("testModel", &model);
1211 TestObject *testObject = new TestObject;
1212 ctxt->setContextProperty("testObject", testObject);
1214 canvas->setSource(source);
1216 qApp->processEvents();
1218 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1219 QTRY_VERIFY(listview != 0);
1220 QQuickItem *contentItem = listview->contentItem();
1221 QTRY_VERIFY(contentItem != 0);
1223 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1224 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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);
1234 QTRY_COMPARE(listview->contentY(), -listview->height());
1235 QVERIFY(listview->currentIndex() == -1);
1237 QCOMPARE(listview->contentHeight(), 0.0);
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);
1250 void tst_QQuickListView::moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1252 QFETCH(qreal, contentY);
1256 QFETCH(qreal, itemsOffsetAfterMove);
1260 QQuickView *canvas = getView();
1263 for (int i = 0; i < 30; i++)
1264 model.addItem("Item" + QString::number(i), "");
1266 QQmlContext *ctxt = canvas->rootContext();
1267 ctxt->setContextProperty("testModel", &model);
1269 TestObject *testObject = new TestObject;
1270 ctxt->setContextProperty("testObject", testObject);
1272 canvas->setSource(source);
1274 qApp->processEvents();
1275 QTest::qWaitForWindowShown(canvas);
1277 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1278 QTRY_VERIFY(listview != 0);
1279 QQuickItem *contentItem = listview->contentItem();
1280 QTRY_VERIFY(contentItem != 0);
1282 // always need to wait for view to be painted before the first move()
1283 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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;
1291 listview->setContentY(contentY);
1293 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1295 model.moveItems(from, to, count);
1296 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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;
1307 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
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);
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));
1324 // current index should have been updated
1325 if (item == listview->currentItem())
1326 QTRY_COMPARE(listview->currentIndex(), i);
1329 releaseView(canvas);
1333 void tst_QQuickListView::moved_data()
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");
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
1345 QTest::newRow("move 1 forwards, within visible items")
1350 QTest::newRow("move 1 forwards, from non-visible -> visible")
1351 << 80.0 // show 4-19
1353 << 20.0; // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement
1355 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1356 << 80.0 // show 4-19
1358 << 20.0; // first item has moved to below item4, everything drops down by size of 1 item
1360 QTest::newRow("move 1 forwards, from visible -> non-visible")
1365 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1371 QTest::newRow("move 1 backwards, within visible items")
1376 QTest::newRow("move 1 backwards, within visible items (to first index)")
1381 QTest::newRow("move 1 backwards, from non-visible -> visible")
1386 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1391 QTest::newRow("move 1 backwards, from visible -> non-visible")
1392 << 80.0 // show 4-19
1394 << -20.0; // to minimize movement, item 0 moves to -20, and other items do not move
1396 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1397 << 80.0 // show 4-19
1399 << -20.0; // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move
1402 QTest::newRow("move multiple forwards, within visible items")
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
1412 QTest::newRow("move multiple forwards, from non-visible -> visible")
1413 << 80.0 // show 4-19
1415 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1417 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1418 << 80.0 // show 4-19
1420 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1422 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1425 << 20.0; // item 1,2 are removed, item 3 is now first visible
1427 QTest::newRow("move multiple forwards, to bottom of view")
1432 QTest::newRow("move multiple forwards, to bottom of view, first->last")
1437 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1442 QTest::newRow("move multiple forwards, from visible -> non-visible")
1447 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1453 QTest::newRow("move multiple backwards, within visible items")
1458 QTest::newRow("move multiple backwards, within visible items (move first item)")
1463 QTest::newRow("move multiple backwards, from non-visible -> visible")
1468 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1473 QTest::newRow("move multiple backwards, from visible -> non-visible")
1474 << 80.0 // show 4-19
1476 << -20.0 * 3; // to minimize movement, 0 moves by -60, and other items do not move
1478 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1479 << 80.0 // show 4-19
1481 << -20.0 * 3; // to minimize movement, 16,17,18 move to above item 0, and other items do not move
1484 void tst_QQuickListView::multipleChanges(bool condensed)
1486 QFETCH(int, startCount);
1487 QFETCH(QList<ListChange>, changes);
1488 QFETCH(int, newCount);
1489 QFETCH(int, newCurrentIndex);
1491 QQuickView *canvas = getView();
1494 for (int i = 0; i < startCount; i++)
1495 model.addItem("Item" + QString::number(i), "");
1497 QQmlContext *ctxt = canvas->rootContext();
1498 ctxt->setContextProperty("testModel", &model);
1500 TestObject *testObject = new TestObject;
1501 ctxt->setContextProperty("testObject", testObject);
1503 canvas->setSource(testFileUrl("listviewtest.qml"));
1505 qApp->processEvents();
1506 QTest::qWaitForWindowShown(canvas);
1508 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1509 QTRY_VERIFY(listview != 0);
1510 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1512 for (int i=0; i<changes.count(); i++) {
1513 switch (changes[i].type) {
1514 case ListChange::Inserted:
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);
1522 case ListChange::Removed:
1523 model.removeItems(changes[i].index, changes[i].count);
1525 case ListChange::Moved:
1526 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1528 case ListChange::SetCurrent:
1529 listview->setCurrentIndex(changes[i].index);
1531 case ListChange::SetContentY:
1532 listview->setContentY(changes[i].pos);
1538 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1541 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1543 QCOMPARE(listview->count(), newCount);
1544 QCOMPARE(listview->count(), model.count());
1545 QCOMPARE(listview->currentIndex(), newCurrentIndex);
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);
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));
1564 releaseView(canvas);
1567 void tst_QQuickListView::multipleChanges_data()
1569 QTest::addColumn<int>("startCount");
1570 QTest::addColumn<QList<ListChange> >("changes");
1571 QTest::addColumn<int>("newCount");
1572 QTest::addColumn<int>("newCurrentIndex");
1574 QList<ListChange> changes;
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;
1580 changes << ListChange::remove(0);
1581 QTest::newRow("remove all") << 30 << changes << 0 << -1;
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;
1589 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1590 << ListChange::remove(0, 1)
1591 << ListChange::insert(0, 1)
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)
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)
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)
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)
1619 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1620 << ListChange::insert(0, 2)
1621 << ListChange::insert(0, 4)
1622 << ListChange::insert(0, 6)
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)
1633 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1634 << ListChange::insert(0, 30)
1635 << ListChange::remove(0, 30)
1638 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1639 << ListChange::insert(1)
1640 << ListChange::setCurrent(1)
1641 << ListChange::remove(1)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
1715 QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1716 << ListChange::remove(20, 5)
1717 << ListChange::setContentY(20)
1720 QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1721 << ListChange::insert(9, 5)
1722 << ListChange::setContentY(20)
1725 QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1726 << ListChange::move(15, 8, 3)
1727 << ListChange::setContentY(0)
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)
1738 void tst_QQuickListView::swapWithFirstItem()
1740 QQuickView *canvas = createView();
1743 for (int i = 0; i < 30; i++)
1744 model.addItem("Item" + QString::number(i), "");
1746 QQmlContext *ctxt = canvas->rootContext();
1747 ctxt->setContextProperty("testModel", &model);
1749 TestObject *testObject = new TestObject;
1750 ctxt->setContextProperty("testObject", testObject);
1752 canvas->setSource(testFileUrl("listviewtest.qml"));
1754 qApp->processEvents();
1756 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1757 QTRY_VERIFY(listview != 0);
1758 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1760 // ensure content position is stable
1761 listview->setContentY(0);
1762 model.moveItem(1, 0);
1763 QTRY_VERIFY(listview->contentY() == 0);
1769 void tst_QQuickListView::enforceRange()
1771 QQuickView *canvas = createView();
1774 for (int i = 0; i < 30; i++)
1775 model.addItem("Item" + QString::number(i), "");
1777 QQmlContext *ctxt = canvas->rootContext();
1778 ctxt->setContextProperty("testModel", &model);
1780 canvas->setSource(testFileUrl("listview-enforcerange.qml"));
1782 qApp->processEvents();
1784 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1785 QTRY_VERIFY(listview != 0);
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);
1792 QQuickItem *contentItem = listview->contentItem();
1793 QTRY_VERIFY(contentItem != 0);
1795 // view should be positioned at the top of the range.
1796 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
1798 QTRY_COMPARE(listview->contentY(), -100.0);
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));
1807 // Check currentIndex is updated when contentItem moves
1808 listview->setContentY(20);
1810 QTRY_COMPARE(listview->currentIndex(), 6);
1813 QmlListModel model2;
1814 for (int i = 0; i < 5; i++)
1815 model2.addItem("Item" + QString::number(i), "");
1817 ctxt->setContextProperty("testModel", &model2);
1818 QCOMPARE(listview->count(), 5);
1823 void tst_QQuickListView::enforceRange_withoutHighlight()
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()
1830 QQuickView *canvas = createView();
1833 model.addItem("Item 0", "a");
1834 model.addItem("Item 1", "b");
1835 model.addItem("Item 2", "b");
1836 model.addItem("Item 3", "c");
1838 QQmlContext *ctxt = canvas->rootContext();
1839 ctxt->setContextProperty("testModel", &model);
1841 canvas->setSource(testFileUrl("listview-enforcerange-nohighlight.qml"));
1843 QTest::qWaitForWindowExposed(canvas);
1845 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1846 QTRY_VERIFY(listview != 0);
1847 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1849 qreal expectedPos = -100.0;
1851 expectedPos += 10.0; // scroll past 1st section's delegate (10px height)
1852 QTRY_COMPARE(listview->contentY(), expectedPos);
1854 expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section
1855 QTest::keyClick(canvas, Qt::Key_Down);
1857 QTRY_COMPARE(listview->contentY(), expectedPos);
1859 expectedPos += 20; // scroll past 1st item of 2nd section
1860 QTest::keyClick(canvas, Qt::Key_Down);
1861 QTRY_COMPARE(listview->contentY(), expectedPos);
1863 expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section
1864 QTest::keyClick(canvas, Qt::Key_Down);
1865 QTRY_COMPARE(listview->contentY(), expectedPos);
1870 void tst_QQuickListView::spacing()
1872 QQuickView *canvas = createView();
1875 for (int i = 0; i < 30; i++)
1876 model.addItem("Item" + QString::number(i), "");
1878 QQmlContext *ctxt = canvas->rootContext();
1879 ctxt->setContextProperty("testModel", &model);
1881 TestObject *testObject = new TestObject;
1882 ctxt->setContextProperty("testObject", testObject);
1884 canvas->setSource(testFileUrl("listviewtest.qml"));
1886 qApp->processEvents();
1888 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1889 QTRY_VERIFY(listview != 0);
1891 QQuickItem *contentItem = listview->contentItem();
1892 QTRY_VERIFY(contentItem != 0);
1893 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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";
1901 QTRY_VERIFY(item->y() == i*20);
1904 listview->setSpacing(10);
1905 QTRY_VERIFY(listview->spacing() == 10);
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";
1913 QTRY_VERIFY(item->y() == i*30);
1916 listview->setSpacing(0);
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";
1924 QTRY_COMPARE(item->y(), i*20.0);
1931 template <typename T>
1932 void tst_QQuickListView::sections(const QUrl &source)
1934 QQuickView *canvas = createView();
1937 for (int i = 0; i < 30; i++)
1938 model.addItem("Item" + QString::number(i), QString::number(i/5));
1940 QQmlContext *ctxt = canvas->rootContext();
1941 ctxt->setContextProperty("testModel", &model);
1943 canvas->setSource(source);
1945 qApp->processEvents();
1947 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1948 QTRY_VERIFY(listview != 0);
1950 QQuickItem *contentItem = listview->contentItem();
1951 QTRY_VERIFY(contentItem != 0);
1953 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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);
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);
1965 QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1967 // Remove section boundary
1968 model.removeItem(5);
1969 QTRY_COMPARE(listview->count(), model.count());
1971 // New section header created
1972 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1974 QTRY_COMPARE(item->height(), 40.0);
1976 model.insertItem(3, "New Item", "0");
1977 QTRY_COMPARE(listview->count(), model.count());
1979 // Section header moved
1980 item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1982 QTRY_COMPARE(item->height(), 20.0);
1984 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1986 QTRY_COMPARE(item->height(), 40.0);
1988 // insert item which will become a section header
1989 model.insertItem(6, "Replace header", "1");
1990 QTRY_COMPARE(listview->count(), model.count());
1992 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1994 QTRY_COMPARE(item->height(), 40.0);
1996 item = findItem<QQuickItem>(contentItem, "wrapper", 7);
1998 QTRY_COMPARE(item->height(), 20.0);
2000 QTRY_COMPARE(listview->currentSection(), QString("0"));
2002 listview->setContentY(140);
2003 QTRY_COMPARE(listview->currentSection(), QString("1"));
2005 QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
2007 listview->setContentY(20);
2008 QTRY_COMPARE(listview->currentSection(), QString("0"));
2010 QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
2012 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2014 QTRY_COMPARE(item->height(), 20.0);
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);
2022 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2024 QTRY_COMPARE(item->height(), 40.0);
2029 void tst_QQuickListView::sectionsDelegate()
2031 QQuickView *canvas = createView();
2034 for (int i = 0; i < 30; i++)
2035 model.addItem("Item" + QString::number(i), QString::number(i/5));
2037 QQmlContext *ctxt = canvas->rootContext();
2038 ctxt->setContextProperty("testModel", &model);
2040 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2042 qApp->processEvents();
2044 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2045 QTRY_VERIFY(listview != 0);
2047 QQuickItem *contentItem = listview->contentItem();
2048 QTRY_VERIFY(contentItem != 0);
2050 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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);
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);
2062 for (int i = 0; i < 3; ++i) {
2063 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2065 QTRY_COMPARE(item->y(), qreal(i*20*6));
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);
2076 for (int i = 0; i < 3; ++i) {
2077 QQuickItem *item = findItem<QQuickItem>(contentItem,
2078 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2080 QTRY_COMPARE(item->y(), qreal(i*20*6));
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)));
2093 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "sect_1");
2094 QCOMPARE(items.count(), 1);
2097 model.modifyItem(0, "One", "aaa");
2098 model.modifyItem(1, "One", "aaa");
2099 model.modifyItem(2, "One", "aaa");
2100 model.modifyItem(3, "Four", "aaa");
2101 model.modifyItem(4, "Four", "aaa");
2102 model.modifyItem(5, "Four", "aaa");
2103 model.modifyItem(6, "Five", "aaa");
2104 model.modifyItem(7, "Five", "aaa");
2105 model.modifyItem(8, "Five", "aaa");
2106 model.modifyItem(9, "Two", "aaa");
2107 model.modifyItem(10, "Two", "aaa");
2108 model.modifyItem(11, "Two", "aaa");
2109 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2110 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_aaa").count(), 1);
2111 canvas->rootObject()->setProperty("sectionProperty", "name");
2112 // ensure view has settled.
2113 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_Four").count(), 1);
2114 for (int i = 0; i < 4; ++i) {
2115 QQuickItem *item = findItem<QQuickItem>(contentItem,
2116 "sect_" + model.name(i*3));
2118 QTRY_COMPARE(item->y(), qreal(i*20*4));
2124 void tst_QQuickListView::sectionsDragOutsideBounds_data()
2126 QTest::addColumn<int>("distance");
2127 QTest::addColumn<int>("cacheBuffer");
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;
2135 void tst_QQuickListView::sectionsDragOutsideBounds()
2137 QFETCH(int, distance);
2138 QFETCH(int, cacheBuffer);
2140 QQuickView *canvas = getView();
2143 for (int i = 0; i < 10; i++)
2144 model.addItem("Item" + QString::number(i), QString::number(i/5));
2146 QQmlContext *ctxt = canvas->rootContext();
2147 ctxt->setContextProperty("testModel", &model);
2149 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2151 qApp->processEvents();
2153 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2154 QTRY_VERIFY(listview != 0);
2155 listview->setCacheBuffer(cacheBuffer);
2157 QQuickItem *contentItem = listview->contentItem();
2158 QTRY_VERIFY(contentItem != 0);
2160 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2163 // Drag view up beyond bounds
2164 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20));
2165 QTest::mouseMove(canvas, QPoint(20,0));
2166 QTest::mouseMove(canvas, QPoint(20,-50));
2167 QTest::mouseMove(canvas, QPoint(20,-distance));
2168 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-distance));
2169 // view should settle back at 0
2170 QTRY_COMPARE(listview->contentY(), 0.0);
2172 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,0));
2173 QTest::mouseMove(canvas, QPoint(20,20));
2174 QTest::mouseMove(canvas, QPoint(20,70));
2175 QTest::mouseMove(canvas, QPoint(20,distance));
2177 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,distance));
2178 // view should settle back at 0
2179 QTRY_COMPARE(listview->contentY(), 0.0);
2181 releaseView(canvas);
2184 void tst_QQuickListView::sectionsDelegate_headerVisibility()
2186 QSKIP("QTBUG-24395");
2188 QQuickView *canvas = createView();
2191 for (int i = 0; i < 30; i++)
2192 model.addItem("Item" + QString::number(i), QString::number(i/5));
2194 canvas->rootContext()->setContextProperty("testModel", &model);
2195 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2197 qApp->processEvents();
2199 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2200 QTRY_VERIFY(listview != 0);
2202 QQuickItem *contentItem = listview->contentItem();
2203 QTRY_VERIFY(contentItem != 0);
2204 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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);
2217 void tst_QQuickListView::sectionsPositioning()
2219 QQuickView *canvas = createView();
2222 for (int i = 0; i < 30; i++)
2223 model.addItem("Item" + QString::number(i), QString::number(i/5));
2225 QQmlContext *ctxt = canvas->rootContext();
2226 ctxt->setContextProperty("testModel", &model);
2228 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2230 qApp->processEvents();
2231 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2233 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2234 QTRY_VERIFY(listview != 0);
2235 QQuickItem *contentItem = listview->contentItem();
2236 QTRY_VERIFY(contentItem != 0);
2237 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2239 for (int i = 0; i < 3; ++i) {
2240 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2242 QTRY_COMPARE(item->y(), qreal(i*20*6));
2245 QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header
2247 QCOMPARE(topItem->y(), 0.);
2249 QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2250 QVERIFY(bottomItem);
2251 QCOMPARE(bottomItem->y(), 300.);
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.);
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
2263 QCOMPARE(topItem->y(), 100.);
2265 QQuickItem *item = findVisibleChild(contentItem, "sect_1");
2267 QCOMPARE(item->y(), 120.);
2269 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2270 QVERIFY(bottomItem);
2271 QCOMPARE(bottomItem->y(), 410.);
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
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.);
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);
2295 QTRY_COMPARE(listview->currentSection(), QString("aaa"));
2297 for (int i = 0; i < 3; ++i) {
2298 QQuickItem *item = findItem<QQuickItem>(contentItem,
2299 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2301 QTRY_COMPARE(item->y(), qreal(i*20*6));
2304 QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header
2305 QCOMPARE(topItem->y(), 10.);
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));
2316 QTRY_COMPARE(item->y(), qreal(i*20*6));
2319 QVERIFY(topItem = findVisibleChild(contentItem, "sect_1"));
2320 QTRY_COMPARE(topItem->y(), 120.);
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.);
2329 model.modifyItem(14, "New", "new");
2330 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2332 QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer
2333 QTRY_COMPARE(bottomItem->y(), 300.);
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);
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.);
2349 // Turn sticky footer off
2350 listview->setContentY(20);
2351 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2352 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2353 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2354 QCOMPARE(item->y(), 340.);
2356 // Turn sticky header off
2357 listview->setContentY(30);
2358 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2359 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2360 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2361 QCOMPARE(item->y(), 0.);
2363 // if an empty model is set the header/footer should be cleaned up
2364 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2365 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2366 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2367 QmlListModel model1;
2368 ctxt->setContextProperty("testModel", &model1);
2369 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2370 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
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
2377 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2378 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2383 void tst_QQuickListView::sectionPropertyChange()
2385 QQuickView *canvas = createView();
2387 canvas->setSource(testFileUrl("sectionpropertychange.qml"));
2389 qApp->processEvents();
2391 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2392 QTRY_VERIFY(listview != 0);
2394 QQuickItem *contentItem = listview->contentItem();
2395 QTRY_VERIFY(contentItem != 0);
2397 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2399 // Confirm items positioned correctly
2400 for (int i = 0; i < 2; ++i) {
2401 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2403 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2406 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2407 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2409 // Confirm items positioned correctly
2410 for (int i = 0; i < 2; ++i) {
2411 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2413 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2416 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2417 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2419 // Confirm items positioned correctly
2420 for (int i = 0; i < 2; ++i) {
2421 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2423 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2426 QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2427 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2429 // Confirm items positioned correctly
2430 for (int i = 0; i < 2; ++i) {
2431 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2433 QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2436 QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2437 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2439 // Confirm items positioned correctly
2440 for (int i = 0; i < 2; ++i) {
2441 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2443 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2449 void tst_QQuickListView::sectionDelegateChange()
2451 QQuickView *canvas = createView();
2453 canvas->setSource(testFileUrl("sectiondelegatechange.qml"));
2455 qApp->processEvents();
2457 QQuickListView *listview = qobject_cast<QQuickListView *>(canvas->rootObject());
2458 QVERIFY(listview != 0);
2460 QQuickItem *contentItem = listview->contentItem();
2461 QVERIFY(contentItem != 0);
2463 QQUICK_VERIFY_POLISH(listview);
2465 QVERIFY(findItems<QQuickItem>(contentItem, "section1").count() > 0);
2466 QCOMPARE(findItems<QQuickItem>(contentItem, "section2").count(), 0);
2468 for (int i = 0; i < 3; ++i) {
2469 QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2471 QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2474 QMetaObject::invokeMethod(canvas->rootObject(), "switchDelegates");
2475 QQUICK_VERIFY_POLISH(listview);
2477 QCOMPARE(findItems<QQuickItem>(contentItem, "section1").count(), 0);
2478 QVERIFY(findItems<QQuickItem>(contentItem, "section2").count() > 0);
2480 for (int i = 0; i < 3; ++i) {
2481 QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2483 QTRY_COMPARE(item->y(), qreal(50. + i*75.));
2489 void tst_QQuickListView::currentIndex_delayedItemCreation()
2491 QFETCH(bool, setCurrentToZero);
2493 QQuickView *canvas = getView();
2495 // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2496 // (since the currentItem will have changed and that shares the same index)
2497 canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2499 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2500 qApp->processEvents();
2502 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2503 QTRY_VERIFY(listview != 0);
2504 QQuickItem *contentItem = listview->contentItem();
2505 QTRY_VERIFY(contentItem != 0);
2507 QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2508 QCOMPARE(listview->currentIndex(), 0);
2509 QTRY_COMPARE(spy.count(), 1);
2511 releaseView(canvas);
2514 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2516 QTest::addColumn<bool>("setCurrentToZero");
2518 QTest::newRow("set to 0") << true;
2519 QTest::newRow("don't set to 0") << false;
2522 void tst_QQuickListView::currentIndex()
2525 for (int i = 0; i < 30; i++)
2526 model.addItem("Item" + QString::number(i), QString::number(i));
2528 QQuickView *canvas = new QQuickView(0);
2529 canvas->setGeometry(0,0,240,320);
2531 QQmlContext *ctxt = canvas->rootContext();
2532 ctxt->setContextProperty("testModel", &model);
2533 ctxt->setContextProperty("testWrap", QVariant(false));
2535 QString filename(testFile("listview-initCurrent.qml"));
2536 canvas->setSource(QUrl::fromLocalFile(filename));
2538 qApp->processEvents();
2540 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2541 QTRY_VERIFY(listview != 0);
2542 QQuickItem *contentItem = listview->contentItem();
2543 QTRY_VERIFY(contentItem != 0);
2544 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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());
2553 listview->setCurrentIndex(0);
2554 QCOMPARE(listview->currentIndex(), 0);
2555 // confirm that the velocity is updated
2556 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2558 // footer should become visible if it is out of view, and then current index is set to count-1
2559 canvas->rootObject()->setProperty("showFooter", true);
2560 QTRY_VERIFY(listview->footerItem());
2561 listview->setCurrentIndex(model.count()-2);
2562 QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2563 listview->setCurrentIndex(model.count()-1);
2564 QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2565 canvas->rootObject()->setProperty("showFooter", false);
2567 // header should become visible if it is out of view, and then current index is set to 0
2568 canvas->rootObject()->setProperty("showHeader", true);
2569 QTRY_VERIFY(listview->headerItem());
2570 listview->setCurrentIndex(1);
2571 QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2572 listview->setCurrentIndex(0);
2573 QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2574 canvas->rootObject()->setProperty("showHeader", false);
2576 // turn off auto highlight
2577 listview->setHighlightFollowsCurrentItem(false);
2578 QVERIFY(listview->highlightFollowsCurrentItem() == false);
2580 QVERIFY(listview->highlightItem());
2581 qreal hlPos = listview->highlightItem()->y();
2583 listview->setCurrentIndex(4);
2584 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2586 // insert item before currentIndex
2587 listview->setCurrentIndex(28);
2588 model.insertItem(0, "Foo", "1111");
2589 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2591 // check removing highlight by setting currentIndex to -1;
2592 listview->setCurrentIndex(-1);
2594 QCOMPARE(listview->currentIndex(), -1);
2595 QVERIFY(!listview->highlightItem());
2596 QVERIFY(!listview->currentItem());
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()));
2607 void tst_QQuickListView::noCurrentIndex()
2610 for (int i = 0; i < 30; i++)
2611 model.addItem("Item" + QString::number(i), QString::number(i));
2613 QQuickView *canvas = new QQuickView(0);
2614 canvas->setGeometry(0,0,240,320);
2616 QQmlContext *ctxt = canvas->rootContext();
2617 ctxt->setContextProperty("testModel", &model);
2619 QString filename(testFile("listview-noCurrent.qml"));
2620 canvas->setSource(QUrl::fromLocalFile(filename));
2622 qApp->processEvents();
2624 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2625 QTRY_VERIFY(listview != 0);
2626 QQuickItem *contentItem = listview->contentItem();
2627 QTRY_VERIFY(contentItem != 0);
2628 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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());
2637 listview->setCurrentIndex(2);
2638 QCOMPARE(listview->currentIndex(), 2);
2639 QVERIFY(listview->highlightItem());
2640 QVERIFY(listview->currentItem());
2645 void tst_QQuickListView::keyNavigation()
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);
2656 for (int i = 0; i < 30; i++)
2657 model.addItem("Item" + QString::number(i), "");
2659 QQuickView *canvas = getView();
2660 TestObject *testObject = new TestObject;
2661 canvas->rootContext()->setContextProperty("testModel", &model);
2662 canvas->rootContext()->setContextProperty("testObject", testObject);
2663 canvas->setSource(testFileUrl("listviewtest.qml"));
2665 QTest::qWaitForWindowActive(canvas);
2667 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2668 QTRY_VERIFY(listview != 0);
2670 listview->setOrientation(orientation);
2671 listview->setLayoutDirection(layoutDirection);
2672 listview->setVerticalLayoutDirection(verticalLayoutDirection);
2673 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2675 canvas->requestActivateWindow();
2676 QTest::qWaitForWindowActive(canvas);
2677 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2678 QCOMPARE(listview->currentIndex(), 0);
2680 QTest::keyClick(canvas, forwardsKey);
2681 QCOMPARE(listview->currentIndex(), 1);
2683 QTest::keyClick(canvas, backwardsKey);
2684 QCOMPARE(listview->currentIndex(), 0);
2686 // hold down a key to go forwards
2687 for (int i=0; i<model.count()-1; i++) {
2688 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
2689 QTRY_COMPARE(listview->currentIndex(), i+1);
2691 QTest::keyRelease(canvas, forwardsKey);
2692 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2693 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2694 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2696 // hold down a key to go backwards
2697 for (int i=model.count()-1; i > 0; i--) {
2698 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2699 QTRY_COMPARE(listview->currentIndex(), i-1);
2701 QTest::keyRelease(canvas, backwardsKey);
2702 QTRY_COMPARE(listview->currentIndex(), 0);
2703 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2704 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2707 QVERIFY(!listview->isWrapEnabled());
2708 listview->incrementCurrentIndex();
2709 QCOMPARE(listview->currentIndex(), 1);
2710 listview->decrementCurrentIndex();
2711 QCOMPARE(listview->currentIndex(), 0);
2713 listview->decrementCurrentIndex();
2714 QCOMPARE(listview->currentIndex(), 0);
2717 listview->setWrapEnabled(true);
2718 QVERIFY(listview->isWrapEnabled());
2720 listview->decrementCurrentIndex();
2721 QCOMPARE(listview->currentIndex(), model.count()-1);
2722 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2723 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2725 listview->incrementCurrentIndex();
2726 QCOMPARE(listview->currentIndex(), 0);
2727 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2728 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2730 releaseView(canvas);
2734 void tst_QQuickListView::keyNavigation_data()
2736 QTest::addColumn<QQuickListView::Orientation>("orientation");
2737 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2738 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2739 QTest::addColumn<Qt::Key>("forwardsKey");
2740 QTest::addColumn<Qt::Key>("backwardsKey");
2741 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2742 QTest::addColumn<QPointF>("contentPosAtLastItem");
2744 QTest::newRow("Vertical, TopToBottom")
2745 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2746 << Qt::Key_Down << Qt::Key_Up
2748 << QPointF(0, 30*20 - 320);
2750 QTest::newRow("Vertical, BottomToTop")
2751 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2752 << Qt::Key_Up << Qt::Key_Down
2754 << QPointF(0, -(30 * 20));
2756 QTest::newRow("Horizontal, LeftToRight")
2757 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2758 << Qt::Key_Right << Qt::Key_Left
2760 << QPointF(30*240 - 240, 0);
2762 QTest::newRow("Horizontal, RightToLeft")
2763 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2764 << Qt::Key_Left << Qt::Key_Right
2766 << QPointF(-(30 * 240), 0);
2769 void tst_QQuickListView::itemList()
2771 QQuickView *canvas = createView();
2772 canvas->setSource(testFileUrl("itemlist.qml"));
2774 qApp->processEvents();
2776 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2777 QTRY_VERIFY(listview != 0);
2779 QQuickItem *contentItem = listview->contentItem();
2780 QTRY_VERIFY(contentItem != 0);
2782 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2783 QTRY_VERIFY(model != 0);
2785 QTRY_VERIFY(model->count() == 3);
2786 QTRY_COMPARE(listview->currentIndex(), 0);
2788 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2790 QTRY_COMPARE(item->x(), 0.0);
2791 QCOMPARE(item->height(), listview->height());
2793 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2795 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2797 listview->setCurrentIndex(2);
2799 item = findItem<QQuickItem>(contentItem, "item3");
2801 QTRY_COMPARE(item->x(), 480.0);
2803 text = findItem<QQuickText>(contentItem, "text3");
2805 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2810 void tst_QQuickListView::itemListFlicker()
2812 QQuickView *canvas = createView();
2813 canvas->setSource(testFileUrl("itemlist-flicker.qml"));
2815 qApp->processEvents();
2817 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2818 QTRY_VERIFY(listview != 0);
2820 QQuickItem *contentItem = listview->contentItem();
2821 QTRY_VERIFY(contentItem != 0);
2823 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2824 QTRY_VERIFY(model != 0);
2826 QTRY_VERIFY(model->count() == 3);
2827 QTRY_COMPARE(listview->currentIndex(), 0);
2831 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2832 QVERIFY(delegateVisible(item));
2833 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2834 QVERIFY(delegateVisible(item));
2835 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2836 QVERIFY(delegateVisible(item));
2838 listview->setCurrentIndex(1);
2840 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2841 QVERIFY(delegateVisible(item));
2842 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2843 QVERIFY(delegateVisible(item));
2844 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2845 QVERIFY(delegateVisible(item));
2847 listview->setCurrentIndex(2);
2849 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2850 QVERIFY(delegateVisible(item));
2851 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2852 QVERIFY(delegateVisible(item));
2853 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2854 QVERIFY(delegateVisible(item));
2859 void tst_QQuickListView::cacheBuffer()
2861 QQuickView *canvas = createView();
2864 for (int i = 0; i < 90; i++)
2865 model.addItem("Item" + QString::number(i), "");
2867 QQmlContext *ctxt = canvas->rootContext();
2868 ctxt->setContextProperty("testModel", &model);
2870 TestObject *testObject = new TestObject;
2871 ctxt->setContextProperty("testObject", testObject);
2873 canvas->setSource(testFileUrl("listviewtest.qml"));
2875 qApp->processEvents();
2877 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2878 QTRY_VERIFY(listview != 0);
2880 QQuickItem *contentItem = listview->contentItem();
2881 QTRY_VERIFY(contentItem != 0);
2882 QTRY_VERIFY(listview->delegate() != 0);
2883 QTRY_VERIFY(listview->model() != 0);
2884 QTRY_VERIFY(listview->highlight() != 0);
2886 // Confirm items positioned correctly
2887 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2888 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2889 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2890 if (!item) qWarning() << "Item" << i << "not found";
2892 QTRY_VERIFY(item->y() == i*20);
2895 QQmlIncubationController controller;
2896 canvas->engine()->setIncubationController(&controller);
2898 testObject->setCacheBuffer(200);
2899 QTRY_VERIFY(listview->cacheBuffer() == 200);
2901 // items will be created one at a time
2902 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2903 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2904 QQuickItem *item = 0;
2907 controller.incubateWhile(&b);
2908 item = findItem<QQuickItem>(listview, "wrapper", i);
2914 controller.incubateWhile(&b);
2917 int newItemCount = 0;
2918 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2920 // Confirm items positioned correctly
2921 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2922 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2923 if (!item) qWarning() << "Item" << i << "not found";
2925 QTRY_VERIFY(item->y() == i*20);
2928 // move view and confirm items in view are visible immediately and outside are created async
2929 listview->setContentY(300);
2931 for (int i = 15; i < 32; ++i) {
2932 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2933 if (!item) qWarning() << "Item" << i << "not found";
2935 QVERIFY(item->y() == i*20);
2938 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2940 // ensure buffered items are created
2941 for (int i = 32; i < qMin(41,model.count()); ++i) {
2942 QQuickItem *item = 0;
2944 qGuiApp->processEvents(); // allow refill to happen
2946 controller.incubateWhile(&b);
2947 item = findItem<QQuickItem>(listview, "wrapper", i);
2953 controller.incubateWhile(&b);
2960 void tst_QQuickListView::positionViewAtIndex()
2962 QQuickView *canvas = createView();
2965 for (int i = 0; i < 40; i++)
2966 model.addItem("Item" + QString::number(i), "");
2968 QQmlContext *ctxt = canvas->rootContext();
2969 ctxt->setContextProperty("testModel", &model);
2971 TestObject *testObject = new TestObject;
2972 ctxt->setContextProperty("testObject", testObject);
2974 canvas->setSource(testFileUrl("listviewtest.qml"));
2975 qApp->processEvents();
2977 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2978 QTRY_VERIFY(listview != 0);
2979 QQuickItem *contentItem = listview->contentItem();
2980 QTRY_VERIFY(contentItem != 0);
2981 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2983 // Confirm items positioned correctly
2984 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2985 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2986 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2987 if (!item) qWarning() << "Item" << i << "not found";
2989 QTRY_COMPARE(item->y(), i*20.);
2992 // Position on a currently visible item
2993 listview->positionViewAtIndex(3, QQuickListView::Beginning);
2994 QTRY_COMPARE(listview->contentY(), 60.);
2996 // Confirm items positioned correctly
2997 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2998 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2999 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3000 if (!item) qWarning() << "Item" << i << "not found";
3002 QTRY_COMPARE(item->y(), i*20.);
3005 // Position on an item beyond the visible items
3006 listview->positionViewAtIndex(22, QQuickListView::Beginning);
3007 QTRY_COMPARE(listview->contentY(), 440.);
3009 // Confirm items positioned correctly
3010 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3011 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
3012 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3013 if (!item) qWarning() << "Item" << i << "not found";
3015 QTRY_COMPARE(item->y(), i*20.);
3018 // Position on an item that would leave empty space if positioned at the top
3019 listview->positionViewAtIndex(28, QQuickListView::Beginning);
3020 QTRY_COMPARE(listview->contentY(), 480.);
3022 // Confirm items positioned correctly
3023 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3024 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
3025 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3026 if (!item) qWarning() << "Item" << i << "not found";
3028 QTRY_COMPARE(item->y(), i*20.);
3031 // Position at the beginning again
3032 listview->positionViewAtIndex(0, QQuickListView::Beginning);
3033 QTRY_COMPARE(listview->contentY(), 0.);
3035 // Confirm items positioned correctly
3036 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3037 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
3038 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3039 if (!item) qWarning() << "Item" << i << "not found";
3041 QTRY_COMPARE(item->y(), i*20.);
3044 // Position at End using last index
3045 listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
3046 QTRY_COMPARE(listview->contentY(), 480.);
3048 // Confirm items positioned correctly
3049 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3050 for (int i = 24; i < model.count(); ++i) {
3051 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3052 if (!item) qWarning() << "Item" << i << "not found";
3054 QTRY_COMPARE(item->y(), i*20.);
3058 listview->positionViewAtIndex(20, QQuickListView::End);
3059 QTRY_COMPARE(listview->contentY(), 100.);
3061 // Position in Center
3062 listview->positionViewAtIndex(15, QQuickListView::Center);
3063 QTRY_COMPARE(listview->contentY(), 150.);
3065 // Ensure at least partially visible
3066 listview->positionViewAtIndex(15, QQuickListView::Visible);
3067 QTRY_COMPARE(listview->contentY(), 150.);
3069 listview->setContentY(302);
3070 listview->positionViewAtIndex(15, QQuickListView::Visible);
3071 QTRY_COMPARE(listview->contentY(), 302.);
3073 listview->setContentY(320);
3074 listview->positionViewAtIndex(15, QQuickListView::Visible);
3075 QTRY_COMPARE(listview->contentY(), 300.);
3077 listview->setContentY(85);
3078 listview->positionViewAtIndex(20, QQuickListView::Visible);
3079 QTRY_COMPARE(listview->contentY(), 85.);
3081 listview->setContentY(75);
3082 listview->positionViewAtIndex(20, QQuickListView::Visible);
3083 QTRY_COMPARE(listview->contentY(), 100.);
3085 // Ensure completely visible
3086 listview->setContentY(120);
3087 listview->positionViewAtIndex(20, QQuickListView::Contain);
3088 QTRY_COMPARE(listview->contentY(), 120.);
3090 listview->setContentY(302);
3091 listview->positionViewAtIndex(15, QQuickListView::Contain);
3092 QTRY_COMPARE(listview->contentY(), 300.);
3094 listview->setContentY(85);
3095 listview->positionViewAtIndex(20, QQuickListView::Contain);
3096 QTRY_COMPARE(listview->contentY(), 100.);
3098 // positionAtBeginnging
3099 listview->positionViewAtBeginning();
3100 QTRY_COMPARE(listview->contentY(), 0.);
3102 listview->setContentY(80);
3103 canvas->rootObject()->setProperty("showHeader", true);
3104 listview->positionViewAtBeginning();
3105 QTRY_COMPARE(listview->contentY(), -30.);
3108 listview->positionViewAtEnd();
3109 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
3111 listview->setContentY(80);
3112 canvas->rootObject()->setProperty("showFooter", true);
3113 listview->positionViewAtEnd();
3114 QTRY_COMPARE(listview->contentY(), 510.);
3116 // set current item to outside visible view, position at beginning
3117 // and ensure highlight moves to current item
3118 listview->setCurrentIndex(1);
3119 listview->positionViewAtBeginning();
3120 QTRY_COMPARE(listview->contentY(), -30.);
3121 QVERIFY(listview->highlightItem());
3122 QCOMPARE(listview->highlightItem()->y(), 20.);
3128 void tst_QQuickListView::resetModel()
3130 QQuickView *canvas = createView();
3132 QStringList strings;
3133 strings << "one" << "two" << "three";
3134 QStringListModel model(strings);
3136 QQmlContext *ctxt = canvas->rootContext();
3137 ctxt->setContextProperty("testModel", &model);
3139 canvas->setSource(testFileUrl("displaylist.qml"));
3141 qApp->processEvents();
3143 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3144 QTRY_VERIFY(listview != 0);
3145 QQuickItem *contentItem = listview->contentItem();
3146 QTRY_VERIFY(contentItem != 0);
3147 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3149 QTRY_COMPARE(listview->count(), model.rowCount());
3151 for (int i = 0; i < model.rowCount(); ++i) {
3152 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3153 QTRY_VERIFY(display != 0);
3154 QTRY_COMPARE(display->text(), strings.at(i));
3158 strings << "four" << "five" << "six" << "seven";
3159 model.setStringList(strings);
3161 QTRY_COMPARE(listview->count(), model.rowCount());
3163 for (int i = 0; i < model.rowCount(); ++i) {
3164 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3165 QTRY_VERIFY(display != 0);
3166 QTRY_COMPARE(display->text(), strings.at(i));
3172 void tst_QQuickListView::propertyChanges()
3174 QQuickView *canvas = createView();
3175 QTRY_VERIFY(canvas);
3176 canvas->setSource(testFileUrl("propertychangestest.qml"));
3178 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3179 QTRY_VERIFY(listView);
3181 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3182 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3183 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3184 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3185 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3186 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3187 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3189 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3190 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3191 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3192 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3193 QTRY_COMPARE(listView->isWrapEnabled(), true);
3194 QTRY_COMPARE(listView->cacheBuffer(), 10);
3195 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3197 listView->setHighlightFollowsCurrentItem(false);
3198 listView->setPreferredHighlightBegin(1.0);
3199 listView->setPreferredHighlightEnd(1.0);
3200 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3201 listView->setWrapEnabled(false);
3202 listView->setCacheBuffer(3);
3203 listView->setSnapMode(QQuickListView::SnapOneItem);
3205 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3206 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3207 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3208 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3209 QTRY_COMPARE(listView->isWrapEnabled(), false);
3210 QTRY_COMPARE(listView->cacheBuffer(), 3);
3211 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3213 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3214 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3215 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3216 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3217 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3218 QTRY_COMPARE(cacheBufferSpy.count(),1);
3219 QTRY_COMPARE(snapModeSpy.count(),1);
3221 listView->setHighlightFollowsCurrentItem(false);
3222 listView->setPreferredHighlightBegin(1.0);
3223 listView->setPreferredHighlightEnd(1.0);
3224 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3225 listView->setWrapEnabled(false);
3226 listView->setCacheBuffer(3);
3227 listView->setSnapMode(QQuickListView::SnapOneItem);
3229 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3230 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3231 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3232 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3233 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3234 QTRY_COMPARE(cacheBufferSpy.count(),1);
3235 QTRY_COMPARE(snapModeSpy.count(),1);
3240 void tst_QQuickListView::componentChanges()
3242 QQuickView *canvas = createView();
3243 QTRY_VERIFY(canvas);
3244 canvas->setSource(testFileUrl("propertychangestest.qml"));
3246 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3247 QTRY_VERIFY(listView);
3249 QQmlComponent component(canvas->engine());
3250 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3252 QQmlComponent delegateComponent(canvas->engine());
3253 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3255 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3256 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3257 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3258 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3260 listView->setHighlight(&component);
3261 listView->setHeader(&component);
3262 listView->setFooter(&component);
3263 listView->setDelegate(&delegateComponent);
3265 QTRY_COMPARE(listView->highlight(), &component);
3266 QTRY_COMPARE(listView->header(), &component);
3267 QTRY_COMPARE(listView->footer(), &component);
3268 QTRY_COMPARE(listView->delegate(), &delegateComponent);
3270 QTRY_COMPARE(highlightSpy.count(),1);
3271 QTRY_COMPARE(delegateSpy.count(),1);
3272 QTRY_COMPARE(headerSpy.count(),1);
3273 QTRY_COMPARE(footerSpy.count(),1);
3275 listView->setHighlight(&component);
3276 listView->setHeader(&component);
3277 listView->setFooter(&component);
3278 listView->setDelegate(&delegateComponent);
3280 QTRY_COMPARE(highlightSpy.count(),1);
3281 QTRY_COMPARE(delegateSpy.count(),1);
3282 QTRY_COMPARE(headerSpy.count(),1);
3283 QTRY_COMPARE(footerSpy.count(),1);
3288 void tst_QQuickListView::modelChanges()
3290 QQuickView *canvas = createView();
3291 QTRY_VERIFY(canvas);
3292 canvas->setSource(testFileUrl("propertychangestest.qml"));
3294 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3295 QTRY_VERIFY(listView);
3297 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
3298 QTRY_VERIFY(alternateModel);
3299 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3300 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3302 listView->setModel(modelVariant);
3303 QTRY_COMPARE(listView->model(), modelVariant);
3304 QTRY_COMPARE(modelSpy.count(),1);
3306 listView->setModel(modelVariant);
3307 QTRY_COMPARE(modelSpy.count(),1);
3309 listView->setModel(QVariant());
3310 QTRY_COMPARE(modelSpy.count(),2);
3315 void tst_QQuickListView::QTBUG_9791()
3317 QQuickView *canvas = createView();
3319 canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
3320 qApp->processEvents();
3322 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3323 QTRY_VERIFY(listview != 0);
3325 QQuickItem *contentItem = listview->contentItem();
3326 QTRY_VERIFY(contentItem != 0);
3327 QTRY_VERIFY(listview->delegate() != 0);
3328 QTRY_VERIFY(listview->model() != 0);
3330 QMetaObject::invokeMethod(listview, "fillModel");
3331 qApp->processEvents();
3333 // Confirm items positioned correctly
3334 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3335 QCOMPARE(itemCount, 3);
3337 for (int i = 0; i < itemCount; ++i) {
3338 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3339 if (!item) qWarning() << "Item" << i << "not found";
3341 QTRY_COMPARE(item->x(), i*300.0);
3344 // check that view is positioned correctly
3345 QTRY_COMPARE(listview->contentX(), 590.0);
3350 void tst_QQuickListView::manualHighlight()
3352 QQuickView *canvas = new QQuickView(0);
3353 canvas->setGeometry(0,0,240,320);
3355 QString filename(testFile("manual-highlight.qml"));
3356 canvas->setSource(QUrl::fromLocalFile(filename));
3358 qApp->processEvents();
3360 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3361 QTRY_VERIFY(listview != 0);
3363 QQuickItem *contentItem = listview->contentItem();
3364 QTRY_VERIFY(contentItem != 0);
3366 QTRY_COMPARE(listview->currentIndex(), 0);
3367 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3368 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3370 listview->setCurrentIndex(2);
3372 QTRY_COMPARE(listview->currentIndex(), 2);
3373 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3374 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3377 listview->positionViewAtIndex(3, QQuickListView::Contain);
3379 QTRY_COMPARE(listview->currentIndex(), 2);
3380 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3381 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3386 void tst_QQuickListView::QTBUG_11105()
3388 QQuickView *canvas = createView();
3391 for (int i = 0; i < 30; i++)
3392 model.addItem("Item" + QString::number(i), "");
3394 QQmlContext *ctxt = canvas->rootContext();
3395 ctxt->setContextProperty("testModel", &model);
3397 TestObject *testObject = new TestObject;
3398 ctxt->setContextProperty("testObject", testObject);
3400 canvas->setSource(testFileUrl("listviewtest.qml"));
3402 qApp->processEvents();
3404 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3405 QTRY_VERIFY(listview != 0);
3406 QQuickItem *contentItem = listview->contentItem();
3407 QTRY_VERIFY(contentItem != 0);
3408 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3410 // Confirm items positioned correctly
3411 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3412 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3413 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3414 if (!item) qWarning() << "Item" << i << "not found";
3416 QTRY_VERIFY(item->y() == i*20);
3419 listview->positionViewAtIndex(20, QQuickListView::Beginning);
3420 QCOMPARE(listview->contentY(), 280.);
3422 QmlListModel model2;
3423 for (int i = 0; i < 5; i++)
3424 model2.addItem("Item" + QString::number(i), "");
3426 ctxt->setContextProperty("testModel", &model2);
3428 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3429 QCOMPARE(itemCount, 5);
3435 void tst_QQuickListView::initialZValues()
3437 QQuickView *canvas = createView();
3438 canvas->setSource(testFileUrl("initialZValues.qml"));
3439 qApp->processEvents();
3441 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3442 QTRY_VERIFY(listview != 0);
3443 QQuickItem *contentItem = listview->contentItem();
3444 QTRY_VERIFY(contentItem != 0);
3446 QVERIFY(listview->headerItem());
3447 QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3449 QVERIFY(listview->footerItem());
3450 QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3455 void tst_QQuickListView::header()
3457 QFETCH(QQuickListView::Orientation, orientation);
3458 QFETCH(Qt::LayoutDirection, layoutDirection);
3459 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3460 QFETCH(QPointF, initialHeaderPos);
3461 QFETCH(QPointF, changedHeaderPos);
3462 QFETCH(QPointF, initialContentPos);
3463 QFETCH(QPointF, changedContentPos);
3464 QFETCH(QPointF, firstDelegatePos);
3465 QFETCH(QPointF, resizeContentPos);
3468 for (int i = 0; i < 30; i++)
3469 model.addItem("Item" + QString::number(i), "");
3471 QQuickView *canvas = getView();
3472 canvas->rootContext()->setContextProperty("testModel", &model);
3473 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3474 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3475 canvas->setSource(testFileUrl("header.qml"));
3477 qApp->processEvents();
3479 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3480 QTRY_VERIFY(listview != 0);
3481 listview->setOrientation(orientation);
3482 listview->setLayoutDirection(layoutDirection);
3483 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3484 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3486 QQuickItem *contentItem = listview->contentItem();
3487 QTRY_VERIFY(contentItem != 0);
3489 QQuickText *header = 0;
3490 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3491 QVERIFY(header == listview->headerItem());
3493 QCOMPARE(header->width(), 100.);
3494 QCOMPARE(header->height(), 30.);
3495 QCOMPARE(header->pos(), initialHeaderPos);
3496 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3498 if (orientation == QQuickListView::Vertical)
3499 QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3501 QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3503 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3505 QCOMPARE(item->pos(), firstDelegatePos);
3508 QTRY_COMPARE(listview->count(), model.count());
3509 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3510 if (orientation == QQuickListView::Vertical)
3511 QCOMPARE(listview->contentHeight(), header->height());
3513 QCOMPARE(listview->contentWidth(), header->width());
3515 for (int i = 0; i < 30; i++)
3516 model.addItem("Item" + QString::number(i), "");
3518 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3519 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3521 QCOMPARE(headerItemSpy.count(), 1);
3523 header = findItem<QQuickText>(contentItem, "header");
3525 header = findItem<QQuickText>(contentItem, "header2");
3528 QVERIFY(header == listview->headerItem());
3530 QCOMPARE(header->pos(), changedHeaderPos);
3531 QCOMPARE(header->width(), 50.);
3532 QCOMPARE(header->height(), 20.);
3533 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3535 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3537 QCOMPARE(item->pos(), firstDelegatePos);
3539 listview->positionViewAtBeginning();
3540 header->setHeight(10);
3541 header->setWidth(40);
3542 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3544 releaseView(canvas);
3547 // QTBUG-21207 header should become visible if view resizes from initial empty size
3550 canvas->rootContext()->setContextProperty("testModel", &model);
3551 canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3552 canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3553 canvas->setSource(testFileUrl("header.qml"));
3555 qApp->processEvents();
3557 listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3558 QTRY_VERIFY(listview != 0);
3559 listview->setOrientation(orientation);
3560 listview->setLayoutDirection(layoutDirection);
3561 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3562 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3564 listview->setWidth(240);
3565 listview->setHeight(320);
3566 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3567 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3569 releaseView(canvas);
3572 void tst_QQuickListView::header_data()
3574 QTest::addColumn<QQuickListView::Orientation>("orientation");
3575 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3576 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3577 QTest::addColumn<QPointF>("initialHeaderPos");
3578 QTest::addColumn<QPointF>("changedHeaderPos");
3579 QTest::addColumn<QPointF>("initialContentPos");
3580 QTest::addColumn<QPointF>("changedContentPos");
3581 QTest::addColumn<QPointF>("firstDelegatePos");
3582 QTest::addColumn<QPointF>("resizeContentPos");
3584 // header1 = 100 x 30
3585 // header2 = 50 x 20
3586 // delegates = 240 x 30
3589 // header above items, top left
3590 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3598 // header above items, top right
3599 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3607 // header to left of items
3608 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3616 // header to right of items
3617 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3620 << QPointF(-240 + 100, 0)
3621 << QPointF(-240 + 50, 0)
3623 << QPointF(-240 + 40, 0);
3625 // header below items
3626 QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3629 << QPointF(0, -320 + 30)
3630 << QPointF(0, -320 + 20)
3632 << QPointF(0, -320 + 10);
3635 void tst_QQuickListView::header_delayItemCreation()
3637 QQuickView *canvas = createView();
3641 canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3642 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3643 qApp->processEvents();
3645 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3646 QTRY_VERIFY(listview != 0);
3648 QQuickItem *contentItem = listview->contentItem();
3649 QTRY_VERIFY(contentItem != 0);
3651 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3653 QCOMPARE(header->y(), -header->height());
3655 QCOMPARE(listview->contentY(), -header->height());
3658 QTRY_COMPARE(header->y(), -header->height());
3663 void tst_QQuickListView::footer()
3665 QFETCH(QQuickListView::Orientation, orientation);
3666 QFETCH(Qt::LayoutDirection, layoutDirection);
3667 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3668 QFETCH(QPointF, initialFooterPos);
3669 QFETCH(QPointF, firstDelegatePos);
3670 QFETCH(QPointF, initialContentPos);
3671 QFETCH(QPointF, changedFooterPos);
3672 QFETCH(QPointF, changedContentPos);
3673 QFETCH(QPointF, resizeContentPos);
3675 QQuickView *canvas = getView();
3678 for (int i = 0; i < 3; i++)
3679 model.addItem("Item" + QString::number(i), "");
3681 QQmlContext *ctxt = canvas->rootContext();
3682 ctxt->setContextProperty("testModel", &model);
3684 canvas->setSource(testFileUrl("footer.qml"));
3686 qApp->processEvents();
3688 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3689 QTRY_VERIFY(listview != 0);
3690 listview->setOrientation(orientation);
3691 listview->setLayoutDirection(layoutDirection);
3692 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3693 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3695 QQuickItem *contentItem = listview->contentItem();
3696 QTRY_VERIFY(contentItem != 0);
3698 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3701 QVERIFY(footer == listview->footerItem());
3703 QCOMPARE(footer->pos(), initialFooterPos);
3704 QCOMPARE(footer->width(), 100.);
3705 QCOMPARE(footer->height(), 30.);
3706 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3708 if (orientation == QQuickListView::Vertical)
3709 QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3711 QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3713 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3715 QCOMPARE(item->pos(), firstDelegatePos);
3718 model.removeItem(1);
3720 if (orientation == QQuickListView::Vertical) {
3721 QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3722 initialFooterPos.y() - 20 : initialFooterPos.y() + 20); // delegate width = 40
3724 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3725 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3730 if (orientation == QQuickListView::Vertical)
3731 QTRY_COMPARE(listview->contentHeight(), footer->height());
3733 QTRY_COMPARE(listview->contentWidth(), footer->width());
3735 QPointF posWhenNoItems(0, 0);
3736 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3737 posWhenNoItems.setX(-100);
3738 else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3739 posWhenNoItems.setY(-30);
3740 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3742 // if header is present, it's at a negative pos, so the footer should not move
3743 canvas->rootObject()->setProperty("showHeader", true);
3744 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3745 canvas->rootObject()->setProperty("showHeader", false);
3748 for (int i = 0; i < 30; i++)
3749 model.addItem("Item" + QString::number(i), "");
3751 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3752 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3754 QCOMPARE(footerItemSpy.count(), 1);
3756 footer = findItem<QQuickText>(contentItem, "footer");
3758 footer = findItem<QQuickText>(contentItem, "footer2");
3761 QVERIFY(footer == listview->footerItem());
3763 QCOMPARE(footer->pos(), changedFooterPos);
3764 QCOMPARE(footer->width(), 50.);
3765 QCOMPARE(footer->height(), 20.);
3766 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3768 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3770 QCOMPARE(item->pos(), firstDelegatePos);
3772 listview->positionViewAtEnd();
3773 footer->setHeight(10);
3774 footer->setWidth(40);
3775 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3777 releaseView(canvas);
3780 void tst_QQuickListView::footer_data()
3782 QTest::addColumn<QQuickListView::Orientation>("orientation");
3783 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3784 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3785 QTest::addColumn<QPointF>("initialFooterPos");
3786 QTest::addColumn<QPointF>("changedFooterPos");
3787 QTest::addColumn<QPointF>("initialContentPos");
3788 QTest::addColumn<QPointF>("changedContentPos");
3789 QTest::addColumn<QPointF>("firstDelegatePos");
3790 QTest::addColumn<QPointF>("resizeContentPos");
3792 // footer1 = 100 x 30
3793 // footer2 = 50 x 20
3794 // delegates = 40 x 20
3796 // view height = 320
3798 // footer below items, bottom left
3799 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3800 << QPointF(0, 3 * 20)
3801 << QPointF(0, 30 * 20) // added 30 items
3805 << QPointF(0, 30 * 20 - 320 + 10);
3807 // footer below items, bottom right
3808 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3809 << QPointF(0, 3 * 20)
3810 << QPointF(0, 30 * 20)
3814 << QPointF(0, 30 * 20 - 320 + 10);
3816 // footer to right of items
3817 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3818 << QPointF(40 * 3, 0)
3819 << QPointF(40 * 30, 0)
3823 << QPointF(40 * 30 - 240 + 40, 0);
3825 // footer to left of items
3826 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3827 << QPointF(-(40 * 3) - 100, 0)
3828 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3832 << QPointF(-(40 * 30) - 40, 0);
3834 // footer above items
3835 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3836 << QPointF(0, -(3 * 20) - 30)
3837 << QPointF(0, -(30 * 20) - 20)
3841 << QPointF(0, -(30 * 20) - 10);
3844 class LVAccessor : public QQuickListView
3847 qreal minY() const { return minYExtent(); }
3848 qreal maxY() const { return maxYExtent(); }
3849 qreal minX() const { return minXExtent(); }
3850 qreal maxX() const { return maxXExtent(); }
3854 void tst_QQuickListView::extents()
3856 QFETCH(QQuickListView::Orientation, orientation);
3857 QFETCH(Qt::LayoutDirection, layoutDirection);
3858 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3859 QFETCH(QPointF, headerPos);
3860 QFETCH(QPointF, footerPos);
3861 QFETCH(QPointF, minPos);
3862 QFETCH(QPointF, maxPos);
3863 QFETCH(QPointF, origin_empty);
3864 QFETCH(QPointF, origin_nonEmpty);
3866 QQuickView *canvas = getView();
3869 QQmlContext *ctxt = canvas->rootContext();
3870 ctxt->setContextProperty("testModel", &model);
3871 canvas->setSource(testFileUrl("headerfooter.qml"));
3873 qApp->processEvents();
3875 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3876 QTRY_VERIFY(listview != 0);
3877 listview->setOrientation(orientation);
3878 listview->setLayoutDirection(layoutDirection);
3879 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3880 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3882 QQuickItem *contentItem = listview->contentItem();
3883 QTRY_VERIFY(contentItem != 0);
3885 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3887 QCOMPARE(header->pos(), headerPos);
3889 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3891 QCOMPARE(footer->pos(), footerPos);
3893 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3894 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3895 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3896 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3898 QCOMPARE(listview->originX(), origin_empty.x());
3899 QCOMPARE(listview->originY(), origin_empty.y());
3900 for (int i=0; i<30; i++)
3901 model.addItem("Item" + QString::number(i), "");
3902 QTRY_COMPARE(listview->count(), model.count());
3903 QCOMPARE(listview->originX(), origin_nonEmpty.x());
3904 QCOMPARE(listview->originY(), origin_nonEmpty.y());
3906 releaseView(canvas);
3909 void tst_QQuickListView::extents_data()
3911 QTest::addColumn<QQuickListView::Orientation>("orientation");
3912 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3913 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3914 QTest::addColumn<QPointF>("headerPos");
3915 QTest::addColumn<QPointF>("footerPos");
3916 QTest::addColumn<QPointF>("minPos");
3917 QTest::addColumn<QPointF>("maxPos");
3918 QTest::addColumn<QPointF>("origin_empty");
3919 QTest::addColumn<QPointF>("origin_nonEmpty");
3921 // header is 240x20 (or 20x320 in Horizontal orientation)
3922 // footer is 240x30 (or 30x320 in Horizontal orientation)
3924 QTest::newRow("Vertical, TopToBottom")
3925 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3926 << QPointF(0, -20) << QPointF(0, 0)
3927 << QPointF(0, 20) << QPointF(240, 20)
3928 << QPointF(0, -20) << QPointF(0, -20);
3930 QTest::newRow("Vertical, BottomToTop")
3931 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3932 << QPointF(0, 0) << QPointF(0, -30)
3933 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3934 << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3936 QTest::newRow("Horizontal, LeftToRight")
3937 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3938 << QPointF(-20, 0) << QPointF(0, 0)
3939 << QPointF(20, 0) << QPointF(20, 320)
3940 << QPointF(-20, 0) << QPointF(-20, 0);
3942 QTest::newRow("Horizontal, RightToLeft")
3943 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3944 << QPointF(0, 0) << QPointF(-30, 0)
3945 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3946 << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3949 void tst_QQuickListView::resetModel_headerFooter()
3951 // Resetting a model shouldn't crash in views with header/footer
3953 QQuickView *canvas = createView();
3956 for (int i = 0; i < 4; i++)
3957 model.addItem("Item" + QString::number(i), "");
3958 QQmlContext *ctxt = canvas->rootContext();
3959 ctxt->setContextProperty("testModel", &model);
3961 canvas->setSource(testFileUrl("headerfooter.qml"));
3962 qApp->processEvents();
3964 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3965 QTRY_VERIFY(listview != 0);
3967 QQuickItem *contentItem = listview->contentItem();
3968 QTRY_VERIFY(contentItem != 0);
3970 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3972 QCOMPARE(header->y(), -header->height());
3974 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3976 QCOMPARE(footer->y(), 30.*4);
3980 header = findItem<QQuickItem>(contentItem, "header");
3982 QCOMPARE(header->y(), -header->height());
3984 footer = findItem<QQuickItem>(contentItem, "footer");
3986 QCOMPARE(footer->y(), 30.*4);
3991 void tst_QQuickListView::resizeView()
3993 QQuickView *canvas = createView();
3996 for (int i = 0; i < 40; i++)
3997 model.addItem("Item" + QString::number(i), "");
3999 QQmlContext *ctxt = canvas->rootContext();
4000 ctxt->setContextProperty("testModel", &model);
4002 TestObject *testObject = new TestObject;
4003 ctxt->setContextProperty("testObject", testObject);
4005 canvas->setSource(testFileUrl("listviewtest.qml"));
4007 qApp->processEvents();
4009 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4010 QTRY_VERIFY(listview != 0);
4011 QQuickItem *contentItem = listview->contentItem();
4012 QTRY_VERIFY(contentItem != 0);
4013 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4015 // Confirm items positioned correctly
4016 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4017 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4018 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4019 if (!item) qWarning() << "Item" << i << "not found";
4021 QTRY_COMPARE(item->y(), i*20.);
4024 QVariant heightRatio;
4025 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4026 QCOMPARE(heightRatio.toReal(), 0.4);
4028 listview->setHeight(200);
4029 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4031 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4032 QCOMPARE(heightRatio.toReal(), 0.25);
4034 // Ensure we handle -ve sizes
4035 listview->setHeight(-100);
4036 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
4038 listview->setCacheBuffer(200);
4039 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
4041 // ensure items in cache become visible
4042 listview->setHeight(200);
4043 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
4045 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4046 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4047 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4048 if (!item) qWarning() << "Item" << i << "not found";
4050 QTRY_COMPARE(item->y(), i*20.);
4051 QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
4054 // ensure items outside view become invisible
4055 listview->setHeight(100);
4056 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
4058 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4059 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4060 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4061 if (!item) qWarning() << "Item" << i << "not found";
4063 QTRY_COMPARE(item->y(), i*20.);
4064 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
4071 void tst_QQuickListView::resizeViewAndRepaint()
4073 QQuickView *canvas = createView();
4076 for (int i = 0; i < 40; i++)
4077 model.addItem("Item" + QString::number(i), "");
4079 QQmlContext *ctxt = canvas->rootContext();
4080 ctxt->setContextProperty("testModel", &model);
4081 ctxt->setContextProperty("initialHeight", 100);
4083 canvas->setSource(testFileUrl("resizeview.qml"));
4085 qApp->processEvents();
4087 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4088 QTRY_VERIFY(listview != 0);
4089 QQuickItem *contentItem = listview->contentItem();
4090 QTRY_VERIFY(contentItem != 0);
4091 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4093 // item at index 10 should not be currently visible
4094 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4096 listview->setHeight(320);
4098 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
4100 listview->setHeight(100);
4101 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4106 void tst_QQuickListView::sizeLessThan1()
4108 QQuickView *canvas = createView();
4111 for (int i = 0; i < 30; i++)
4112 model.addItem("Item" + QString::number(i), "");
4114 QQmlContext *ctxt = canvas->rootContext();
4115 ctxt->setContextProperty("testModel", &model);
4117 TestObject *testObject = new TestObject;
4118 ctxt->setContextProperty("testObject", testObject);
4120 canvas->setSource(testFileUrl("sizelessthan1.qml"));
4122 qApp->processEvents();
4124 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4125 QTRY_VERIFY(listview != 0);
4126 QQuickItem *contentItem = listview->contentItem();
4127 QTRY_VERIFY(contentItem != 0);
4128 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4130 // Confirm items positioned correctly
4131 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4132 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4133 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4134 if (!item) qWarning() << "Item" << i << "not found";
4136 QTRY_COMPARE(item->y(), i*0.5);
4143 void tst_QQuickListView::QTBUG_14821()
4145 QQuickView *canvas = createView();
4147 canvas->setSource(testFileUrl("qtbug14821.qml"));
4148 qApp->processEvents();
4150 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4151 QVERIFY(listview != 0);
4153 QQuickItem *contentItem = listview->contentItem();
4154 QVERIFY(contentItem != 0);
4156 listview->decrementCurrentIndex();
4157 QCOMPARE(listview->currentIndex(), 99);
4159 listview->incrementCurrentIndex();
4160 QCOMPARE(listview->currentIndex(), 0);
4165 void tst_QQuickListView::resizeDelegate()
4167 QQuickView *canvas = createView();
4169 QStringList strings;
4170 for (int i = 0; i < 30; ++i)
4171 strings << QString::number(i);
4172 QStringListModel model(strings);
4174 QQmlContext *ctxt = canvas->rootContext();
4175 ctxt->setContextProperty("testModel", &model);
4177 canvas->setSource(testFileUrl("displaylist.qml"));
4179 qApp->processEvents();
4181 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4182 QVERIFY(listview != 0);
4183 QQuickItem *contentItem = listview->contentItem();
4184 QVERIFY(contentItem != 0);
4185 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4187 QCOMPARE(listview->count(), model.rowCount());
4189 listview->setCurrentIndex(25);
4190 listview->setContentY(0);
4191 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4193 for (int i = 0; i < 16; ++i) {
4194 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4196 QCOMPARE(item->y(), i*20.0);
4199 QCOMPARE(listview->currentItem()->y(), 500.0);
4200 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4202 canvas->rootObject()->setProperty("delegateHeight", 30);
4203 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4205 for (int i = 0; i < 11; ++i) {
4206 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4208 QTRY_COMPARE(item->y(), i*30.0);
4211 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4212 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4214 listview->setCurrentIndex(1);
4215 listview->positionViewAtIndex(25, QQuickListView::Beginning);
4216 listview->positionViewAtIndex(5, QQuickListView::Beginning);
4217 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4219 for (int i = 5; i < 16; ++i) {
4220 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4222 QCOMPARE(item->y(), i*30.0);
4225 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4226 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4228 canvas->rootObject()->setProperty("delegateHeight", 20);
4229 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4231 for (int i = 5; i < 11; ++i) {
4232 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4234 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4237 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4238 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4243 void tst_QQuickListView::resizeFirstDelegate()
4245 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4246 // and other delegates have height > 0
4248 QQuickView *canvas = createView();
4250 // bug only occurs when all items in the model are visible
4252 for (int i = 0; i < 10; i++)
4253 model.addItem("Item" + QString::number(i), "");
4255 QQmlContext *ctxt = canvas->rootContext();
4256 ctxt->setContextProperty("testModel", &model);
4258 TestObject *testObject = new TestObject;
4259 ctxt->setContextProperty("testObject", testObject);
4261 canvas->setSource(testFileUrl("listviewtest.qml"));
4263 qApp->processEvents();
4265 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4266 QVERIFY(listview != 0);
4267 QQuickItem *contentItem = listview->contentItem();
4268 QVERIFY(contentItem != 0);
4269 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4271 QQuickItem *item = 0;
4272 for (int i = 0; i < model.count(); ++i) {
4273 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4275 QCOMPARE(item->y(), i*20.0);
4278 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4281 // check the content y has not jumped up and down
4282 QCOMPARE(listview->contentY(), 0.0);
4283 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4285 QCOMPARE(spy.count(), 0);
4287 for (int i = 1; i < model.count(); ++i) {
4288 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4290 QTRY_COMPARE(item->y(), (i-1)*20.0);
4294 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4295 // list if they follow a zero-sized delegate
4297 for (int i = 0; i < 10; i++)
4298 model.addItem("Item" + QString::number(i), "");
4299 QTRY_COMPARE(listview->count(), model.count());
4301 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4305 listview->setCurrentIndex(19);
4306 qApp->processEvents();
4307 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4309 // items 0-2 should have been deleted
4310 for (int i=0; i<3; i++) {
4311 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4318 void tst_QQuickListView::repositionResizedDelegate()
4320 QFETCH(QQuickListView::Orientation, orientation);
4321 QFETCH(Qt::LayoutDirection, layoutDirection);
4322 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4323 QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4324 QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4325 QFETCH(QRectF, origPositionerRect);
4326 QFETCH(QRectF, resizedPositionerRect);
4328 QQuickView *canvas = getView();
4329 QQmlContext *ctxt = canvas->rootContext();
4330 ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4331 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4332 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4333 canvas->setSource(testFileUrl("repositionResizedDelegate.qml"));
4335 qApp->processEvents();
4337 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4338 QTRY_VERIFY(listview != 0);
4339 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4341 QQuickItem *positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4342 QVERIFY(positioner);
4343 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4344 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4345 QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4346 int prevSpyCount = 0;
4348 // When an item is resized while it is partially visible, it should resize in the
4349 // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4350 // the item should also be re-positioned so its end position stays the same.
4352 listview->setContentX(contentPos_itemFirstHalfVisible.x());
4353 listview->setContentY(contentPos_itemFirstHalfVisible.y());
4354 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4355 prevSpyCount = spy.count();
4356 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4357 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4358 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4359 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4360 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4361 QCOMPARE(spy.count(), prevSpyCount);
4363 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "decrementRepeater"));
4364 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4365 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4366 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4367 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4369 listview->setContentX(contentPos_itemSecondHalfVisible.x());
4370 listview->setContentY(contentPos_itemSecondHalfVisible.y());
4371 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4372 prevSpyCount = spy.count();
4374 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4375 positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4376 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4377 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4378 QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4379 QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4380 qApp->processEvents();
4381 QCOMPARE(spy.count(), prevSpyCount);
4383 releaseView(canvas);
4386 void tst_QQuickListView::repositionResizedDelegate_data()
4388 QTest::addColumn<QQuickListView::Orientation>("orientation");
4389 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4390 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4391 QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4392 QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4393 QTest::addColumn<QRectF>("origPositionerRect");
4394 QTest::addColumn<QRectF>("resizedPositionerRect");
4396 QTest::newRow("vertical")
4397 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4398 << QPointF(0, 60) << QPointF(0, 200 + 60)
4399 << QRectF(0, 200, 120, 120)
4400 << QRectF(0, 200, 120, 120 * 2);
4402 QTest::newRow("vertical, BottomToTop")
4403 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4404 << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4405 << QRectF(0, -200 - 120, 120, 120)
4406 << QRectF(0, -200 - 120*2, 120, 120 * 2);
4408 QTest::newRow("horizontal")
4409 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4410 << QPointF(60, 0) << QPointF(260, 0)
4411 << QRectF(200, 0, 120, 120)
4412 << QRectF(200, 0, 120 * 2, 120);
4414 QTest::newRow("horizontal, rtl")
4415 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4416 << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4417 << QRectF(-200 - 120, 0, 120, 120)
4418 << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4421 void tst_QQuickListView::QTBUG_16037()
4423 QQuickView *canvas = createView();
4426 canvas->setSource(testFileUrl("qtbug16037.qml"));
4427 qApp->processEvents();
4429 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4430 QTRY_VERIFY(listview != 0);
4432 QVERIFY(listview->contentHeight() <= 0.0);
4434 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
4436 QTRY_COMPARE(listview->contentHeight(), 80.0);
4441 void tst_QQuickListView::indexAt_itemAt_data()
4443 QTest::addColumn<qreal>("x");
4444 QTest::addColumn<qreal>("y");
4445 QTest::addColumn<int>("index");
4447 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4448 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4449 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4450 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4451 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4454 void tst_QQuickListView::indexAt_itemAt()
4460 QQuickView *canvas = getView();
4463 for (int i = 0; i < 30; i++)
4464 model.addItem("Item" + QString::number(i), "");
4466 QQmlContext *ctxt = canvas->rootContext();
4467 ctxt->setContextProperty("testModel", &model);
4469 TestObject *testObject = new TestObject;
4470 ctxt->setContextProperty("testObject", testObject);
4472 canvas->setSource(testFileUrl("listviewtest.qml"));
4474 qApp->processEvents();
4476 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4477 QTRY_VERIFY(listview != 0);
4479 QQuickItem *contentItem = listview->contentItem();
4480 QTRY_VERIFY(contentItem != 0);
4481 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4483 QQuickItem *item = 0;
4485 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4488 QCOMPARE(listview->indexAt(x,y), index);
4489 QVERIFY(listview->itemAt(x,y) == item);
4491 releaseView(canvas);
4495 void tst_QQuickListView::incrementalModel()
4497 QQuickView *canvas = createView();
4499 IncrementalModel model;
4500 QQmlContext *ctxt = canvas->rootContext();
4501 ctxt->setContextProperty("testModel", &model);
4503 canvas->setSource(testFileUrl("displaylist.qml"));
4504 qApp->processEvents();
4506 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4507 QTRY_VERIFY(listview != 0);
4509 QQuickItem *contentItem = listview->contentItem();
4510 QTRY_VERIFY(contentItem != 0);
4512 QTRY_COMPARE(listview->count(), 20);
4514 listview->positionViewAtIndex(10, QQuickListView::Beginning);
4516 QTRY_COMPARE(listview->count(), 25);
4521 void tst_QQuickListView::onAdd()
4523 QFETCH(int, initialItemCount);
4524 QFETCH(int, itemsToAdd);
4526 const int delegateHeight = 10;
4529 // these initial items should not trigger ListView.onAdd
4530 for (int i=0; i<initialItemCount; i++)
4531 model.addItem("dummy value", "dummy value");
4533 QQuickView *canvas = createView();
4534 canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4535 QQmlContext *ctxt = canvas->rootContext();
4536 ctxt->setContextProperty("testModel", &model);
4537 ctxt->setContextProperty("delegateHeight", delegateHeight);
4538 canvas->setSource(testFileUrl("attachedSignals.qml"));
4540 QObject *object = canvas->rootObject();
4541 object->setProperty("width", canvas->width());
4542 object->setProperty("height", canvas->height());
4543 qApp->processEvents();
4545 QList<QPair<QString, QString> > items;
4546 for (int i=0; i<itemsToAdd; i++)
4547 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4548 model.addItems(items);
4549 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4551 QVariantList result = object->property("addedDelegates").toList();
4552 QCOMPARE(result.count(), items.count());
4553 for (int i=0; i<items.count(); i++)
4554 QCOMPARE(result[i].toString(), items[i].first);
4559 void tst_QQuickListView::onAdd_data()
4561 QTest::addColumn<int>("initialItemCount");
4562 QTest::addColumn<int>("itemsToAdd");
4564 QTest::newRow("0, add 1") << 0 << 1;
4565 QTest::newRow("0, add 2") << 0 << 2;
4566 QTest::newRow("0, add 10") << 0 << 10;
4568 QTest::newRow("1, add 1") << 1 << 1;
4569 QTest::newRow("1, add 2") << 1 << 2;
4570 QTest::newRow("1, add 10") << 1 << 10;
4572 QTest::newRow("5, add 1") << 5 << 1;
4573 QTest::newRow("5, add 2") << 5 << 2;
4574 QTest::newRow("5, add 10") << 5 << 10;
4577 void tst_QQuickListView::onRemove()
4579 QFETCH(int, initialItemCount);
4580 QFETCH(int, indexToRemove);
4581 QFETCH(int, removeCount);
4583 const int delegateHeight = 10;
4585 for (int i=0; i<initialItemCount; i++)
4586 model.addItem(QString("value %1").arg(i), "dummy value");
4588 QQuickView *canvas = getView();
4589 QQmlContext *ctxt = canvas->rootContext();
4590 ctxt->setContextProperty("testModel", &model);
4591 ctxt->setContextProperty("delegateHeight", delegateHeight);
4592 canvas->setSource(testFileUrl("attachedSignals.qml"));
4594 QObject *object = canvas->rootObject();
4596 model.removeItems(indexToRemove, removeCount);
4597 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4599 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4601 releaseView(canvas);
4604 void tst_QQuickListView::onRemove_data()
4606 QTest::addColumn<int>("initialItemCount");
4607 QTest::addColumn<int>("indexToRemove");
4608 QTest::addColumn<int>("removeCount");
4610 QTest::newRow("remove first") << 1 << 0 << 1;
4611 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4612 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4613 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4615 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4616 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4617 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4618 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4619 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4620 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4622 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4623 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4624 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4627 void tst_QQuickListView::rightToLeft()
4629 QQuickView *canvas = createView();
4630 canvas->setGeometry(0,0,640,320);
4631 canvas->setSource(testFileUrl("rightToLeft.qml"));
4633 qApp->processEvents();
4635 QVERIFY(canvas->rootObject() != 0);
4636 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4637 QTRY_VERIFY(listview != 0);
4639 QQuickItem *contentItem = listview->contentItem();
4640 QTRY_VERIFY(contentItem != 0);
4642 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4644 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4645 QTRY_VERIFY(model != 0);
4647 QTRY_VERIFY(model->count() == 3);
4648 QTRY_COMPARE(listview->currentIndex(), 0);
4650 // initial position at first item, right edge aligned
4651 QCOMPARE(listview->contentX(), -640.);
4653 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4655 QTRY_COMPARE(item->x(), -100.0);
4656 QCOMPARE(item->height(), listview->height());
4658 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4660 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4662 listview->setCurrentIndex(2);
4664 item = findItem<QQuickItem>(contentItem, "item3");
4666 QTRY_COMPARE(item->x(), -540.0);
4668 text = findItem<QQuickText>(contentItem, "text3");
4670 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4672 QCOMPARE(listview->contentX(), -640.);
4674 // Ensure resizing maintains position relative to right edge
4675 qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4676 QTRY_COMPARE(listview->contentX(), -600.);
4681 void tst_QQuickListView::test_mirroring()
4683 QQuickView *canvasA = createView();
4684 canvasA->setSource(testFileUrl("rightToLeft.qml"));
4685 QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4686 QTRY_VERIFY(listviewA != 0);
4688 QQuickView *canvasB = createView();
4689 canvasB->setSource(testFileUrl("rightToLeft.qml"));
4690 QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4691 QTRY_VERIFY(listviewA != 0);
4692 qApp->processEvents();
4694 QList<QString> objectNames;
4695 objectNames << "item1" << "item2"; // << "item3"
4697 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4698 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4699 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4702 foreach (const QString objectName, objectNames)
4703 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4705 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4706 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4709 foreach (const QString objectName, objectNames)
4710 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4712 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4713 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4714 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4716 // LTR != LTR+mirror
4717 foreach (const QString objectName, objectNames)
4718 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4720 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4722 // RTL == LTR+mirror
4723 foreach (const QString objectName, objectNames)
4724 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4726 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4728 // RTL != RTL+mirror
4729 foreach (const QString objectName, objectNames)
4730 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4732 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4734 // LTR == RTL+mirror
4735 foreach (const QString objectName, objectNames)
4736 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4742 void tst_QQuickListView::margins()
4744 QQuickView *canvas = createView();
4747 for (int i = 0; i < 50; i++)
4748 model.addItem("Item" + QString::number(i), "");
4750 QQmlContext *ctxt = canvas->rootContext();
4751 ctxt->setContextProperty("testModel", &model);
4753 canvas->setSource(testFileUrl("margins.qml"));
4755 qApp->processEvents();
4757 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4758 QTRY_VERIFY(listview != 0);
4759 QQuickItem *contentItem = listview->contentItem();
4760 QTRY_VERIFY(contentItem != 0);
4761 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4763 QCOMPARE(listview->contentY(), -30.);
4764 QCOMPARE(listview->originY(), 0.);
4767 listview->positionViewAtEnd();
4768 qreal pos = listview->contentY();
4769 listview->setContentY(pos + 80);
4770 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4771 listview->returnToBounds();
4772 QTRY_COMPARE(listview->contentY(), pos + 50);
4774 // remove item before visible and check that top margin is maintained
4775 // and originY is updated
4776 listview->setContentY(100);
4777 model.removeItem(1);
4778 QTRY_COMPARE(listview->count(), model.count());
4779 listview->setContentY(-50);
4780 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4781 listview->returnToBounds();
4782 QCOMPARE(listview->originY(), 20.);
4783 QTRY_COMPARE(listview->contentY(), -10.);
4785 // reduce top margin
4786 listview->setTopMargin(20);
4787 QCOMPARE(listview->originY(), 20.);
4788 QTRY_COMPARE(listview->contentY(), 0.);
4791 listview->positionViewAtEnd();
4792 pos = listview->contentY();
4793 listview->setContentY(pos + 80);
4794 listview->returnToBounds();
4795 QTRY_COMPARE(listview->contentY(), pos + 50);
4797 // reduce bottom margin
4798 pos = listview->contentY();
4799 listview->setBottomMargin(40);
4800 QCOMPARE(listview->originY(), 20.);
4801 QTRY_COMPARE(listview->contentY(), pos-10);
4807 void tst_QQuickListView::marginsResize()
4809 QFETCH(QQuickListView::Orientation, orientation);
4810 QFETCH(Qt::LayoutDirection, layoutDirection);
4811 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4812 QFETCH(qreal, start);
4815 QPoint flickStart(20, 20);
4816 QPoint flickEnd(20, 20);
4817 if (orientation == QQuickListView::Vertical)
4818 flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4820 flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4822 QQuickView *canvas = getView();
4824 canvas->setSource(testFileUrl("margins2.qml"));
4826 qApp->processEvents();
4828 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4829 QTRY_VERIFY(listview != 0);
4831 listview->setOrientation(orientation);
4832 listview->setLayoutDirection(layoutDirection);
4833 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4834 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4836 // view is resized after componentCompleted - top margin should still be visible
4837 if (orientation == QQuickListView::Vertical)
4838 QCOMPARE(listview->contentY(), start);
4840 QCOMPARE(listview->contentX(), start);
4842 // move to last index and ensure bottom margin is visible.
4843 listview->setCurrentIndex(19);
4844 if (orientation == QQuickListView::Vertical)
4845 QTRY_COMPARE(listview->contentY(), end);
4847 QTRY_COMPARE(listview->contentX(), end);
4849 // flick past the end and check content pos still settles on correct extents
4850 flick(canvas, flickStart, flickEnd, 180);
4851 QTRY_VERIFY(listview->isMoving() == false);
4852 if (orientation == QQuickListView::Vertical)
4853 QTRY_COMPARE(listview->contentY(), end);
4855 QTRY_COMPARE(listview->contentX(), end);
4857 // back to top - top margin should be visible.
4858 listview->setCurrentIndex(0);
4859 if (orientation == QQuickListView::Vertical)
4860 QTRY_COMPARE(listview->contentY(), start);
4862 QTRY_COMPARE(listview->contentX(), start);
4864 // flick past the beginning and check content pos still settles on correct extents
4865 flick(canvas, flickEnd, flickStart, 180);
4866 QTRY_VERIFY(listview->isMoving() == false);
4867 if (orientation == QQuickListView::Vertical)
4868 QTRY_COMPARE(listview->contentY(), start);
4870 QTRY_COMPARE(listview->contentX(), start);
4872 releaseView(canvas);
4875 void tst_QQuickListView::marginsResize_data()
4877 QTest::addColumn<QQuickListView::Orientation>("orientation");
4878 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4879 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4880 QTest::addColumn<qreal>("start");
4881 QTest::addColumn<qreal>("end");
4883 // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4885 QTest::newRow("vertical")
4886 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4889 QTest::newRow("vertical, BottomToTop")
4890 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4891 << -180.0 << -1240.0;
4893 QTest::newRow("horizontal")
4894 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4897 QTest::newRow("horizontal, rtl")
4898 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4899 << -180.0 << -1240.0;
4902 void tst_QQuickListView::snapToItem_data()
4904 QTest::addColumn<QQuickListView::Orientation>("orientation");
4905 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4906 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4907 QTest::addColumn<int>("highlightRangeMode");
4908 QTest::addColumn<QPoint>("flickStart");
4909 QTest::addColumn<QPoint>("flickEnd");
4910 QTest::addColumn<qreal>("snapAlignment");
4911 QTest::addColumn<qreal>("endExtent");
4912 QTest::addColumn<qreal>("startExtent");
4914 QTest::newRow("vertical, top to bottom")
4915 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4916 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4918 QTest::newRow("vertical, bottom to top")
4919 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4920 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4922 QTest::newRow("horizontal, left to right")
4923 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4924 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4926 QTest::newRow("horizontal, right to left")
4927 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4928 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4930 QTest::newRow("vertical, top to bottom, enforce range")
4931 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4932 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4934 QTest::newRow("vertical, bottom to top, enforce range")
4935 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4936 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4938 QTest::newRow("horizontal, left to right, enforce range")
4939 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4940 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4942 QTest::newRow("horizontal, right to left, enforce range")
4943 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4944 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4947 void tst_QQuickListView::snapToItem()
4949 QFETCH(QQuickListView::Orientation, orientation);
4950 QFETCH(Qt::LayoutDirection, layoutDirection);
4951 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4952 QFETCH(int, highlightRangeMode);
4953 QFETCH(QPoint, flickStart);
4954 QFETCH(QPoint, flickEnd);
4955 QFETCH(qreal, snapAlignment);
4956 QFETCH(qreal, endExtent);
4957 QFETCH(qreal, startExtent);
4959 QQuickView *canvas = getView();
4961 canvas->setSource(testFileUrl("snapToItem.qml"));
4963 qApp->processEvents();
4965 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4966 QTRY_VERIFY(listview != 0);
4968 listview->setOrientation(orientation);
4969 listview->setLayoutDirection(layoutDirection);
4970 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4971 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4972 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4974 QQuickItem *contentItem = listview->contentItem();
4975 QTRY_VERIFY(contentItem != 0);
4977 // confirm that a flick hits an item boundary
4978 flick(canvas, flickStart, flickEnd, 180);
4979 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4980 if (orientation == QQuickListView::Vertical)
4981 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4983 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4987 flick(canvas, flickStart, flickEnd, 180);
4988 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4989 } while (orientation == QQuickListView::Vertical
4990 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4991 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4993 if (orientation == QQuickListView::Vertical)
4994 QCOMPARE(listview->contentY(), endExtent);
4996 QCOMPARE(listview->contentX(), endExtent);
5000 flick(canvas, flickEnd, flickStart, 180);
5001 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5002 } while (orientation == QQuickListView::Vertical
5003 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5004 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5006 if (orientation == QQuickListView::Vertical)
5007 QCOMPARE(listview->contentY(), startExtent);
5009 QCOMPARE(listview->contentX(), startExtent);
5011 releaseView(canvas);
5014 void tst_QQuickListView::qListModelInterface_items()
5016 items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5019 void tst_QQuickListView::qListModelInterface_package_items()
5021 items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5024 void tst_QQuickListView::qAbstractItemModel_items()
5026 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
5029 void tst_QQuickListView::qListModelInterface_changed()
5031 changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5034 void tst_QQuickListView::qListModelInterface_package_changed()
5036 changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5039 void tst_QQuickListView::qAbstractItemModel_changed()
5041 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5044 void tst_QQuickListView::qListModelInterface_inserted()
5046 inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
5049 void tst_QQuickListView::qListModelInterface_package_inserted()
5051 inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5054 void tst_QQuickListView::qListModelInterface_inserted_more()
5056 inserted_more<QmlListModel>();
5059 void tst_QQuickListView::qListModelInterface_inserted_more_data()
5061 inserted_more_data();
5064 void tst_QQuickListView::qAbstractItemModel_inserted()
5066 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
5069 void tst_QQuickListView::qAbstractItemModel_inserted_more()
5071 inserted_more<QaimModel>();
5074 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
5076 inserted_more_data();
5079 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
5081 inserted_more<QaimModel>(QQuickItemView::BottomToTop);
5084 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
5086 inserted_more_data();
5089 void tst_QQuickListView::qListModelInterface_removed()
5091 removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5092 removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
5095 void tst_QQuickListView::qListModelInterface_removed_more()
5097 removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
5100 void tst_QQuickListView::qListModelInterface_removed_more_data()
5102 removed_more_data();
5105 void tst_QQuickListView::qListModelInterface_package_removed()
5107 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
5108 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5111 void tst_QQuickListView::qAbstractItemModel_removed()
5113 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5114 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5117 void tst_QQuickListView::qAbstractItemModel_removed_more()
5119 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5122 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5124 removed_more_data();
5127 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5129 removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5132 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5134 removed_more_data();
5137 void tst_QQuickListView::qListModelInterface_moved()
5139 moved<QmlListModel>(testFileUrl("listviewtest.qml"));
5142 void tst_QQuickListView::qListModelInterface_moved_data()
5147 void tst_QQuickListView::qListModelInterface_package_moved()
5149 moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5152 void tst_QQuickListView::qListModelInterface_package_moved_data()
5157 void tst_QQuickListView::qAbstractItemModel_moved()
5159 moved<QaimModel>(testFileUrl("listviewtest.qml"));
5162 void tst_QQuickListView::qAbstractItemModel_moved_data()
5167 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5169 moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5172 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5177 void tst_QQuickListView::qListModelInterface_clear()
5179 clear<QmlListModel>(testFileUrl("listviewtest.qml"));
5182 void tst_QQuickListView::qListModelInterface_package_clear()
5184 clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5187 void tst_QQuickListView::qAbstractItemModel_clear()
5189 clear<QaimModel>(testFileUrl("listviewtest.qml"));
5192 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5194 clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5197 void tst_QQuickListView::qListModelInterface_sections()
5199 sections<QmlListModel>(testFileUrl("listview-sections.qml"));
5202 void tst_QQuickListView::qListModelInterface_package_sections()
5204 sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
5207 void tst_QQuickListView::qAbstractItemModel_sections()
5209 sections<QaimModel>(testFileUrl("listview-sections.qml"));
5212 void tst_QQuickListView::creationContext()
5215 canvas.setGeometry(0,0,240,320);
5216 canvas.setSource(testFileUrl("creationContext.qml"));
5217 qApp->processEvents();
5219 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5221 QVERIFY(rootItem->property("count").toInt() > 0);
5224 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5225 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5226 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5227 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5228 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5229 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5230 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5231 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5234 void tst_QQuickListView::QTBUG_21742()
5237 canvas.setGeometry(0,0,200,200);
5238 canvas.setSource(testFileUrl("qtbug-21742.qml"));
5239 qApp->processEvents();
5241 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5243 QCOMPARE(rootItem->property("count").toInt(), 1);
5246 void tst_QQuickListView::asynchronous()
5248 QQuickView *canvas = createView();
5250 QQmlIncubationController controller;
5251 canvas->engine()->setIncubationController(&controller);
5253 canvas->setSource(testFileUrl("asyncloader.qml"));
5255 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5256 QVERIFY(rootObject);
5258 QQuickListView *listview = 0;
5261 controller.incubateWhile(&b);
5262 listview = rootObject->findChild<QQuickListView*>("view");
5265 // items will be created one at a time
5266 for (int i = 0; i < 8; ++i) {
5267 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5268 QQuickItem *item = 0;
5271 controller.incubateWhile(&b);
5272 item = findItem<QQuickItem>(listview, "wrapper", i);
5278 controller.incubateWhile(&b);
5281 // verify positioning
5282 QQuickItem *contentItem = listview->contentItem();
5283 for (int i = 0; i < 8; ++i) {
5284 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5285 QTRY_COMPARE(item->y(), i*50.0);
5291 void tst_QQuickListView::snapOneItem_data()
5293 QTest::addColumn<QQuickListView::Orientation>("orientation");
5294 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5295 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5296 QTest::addColumn<int>("highlightRangeMode");
5297 QTest::addColumn<QPoint>("flickStart");
5298 QTest::addColumn<QPoint>("flickEnd");
5299 QTest::addColumn<qreal>("snapAlignment");
5300 QTest::addColumn<qreal>("endExtent");
5301 QTest::addColumn<qreal>("startExtent");
5303 QTest::newRow("vertical, top to bottom")
5304 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5305 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5307 QTest::newRow("vertical, bottom to top")
5308 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5309 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5311 QTest::newRow("horizontal, left to right")
5312 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5313 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5315 QTest::newRow("horizontal, right to left")
5316 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5317 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5319 QTest::newRow("vertical, top to bottom, enforce range")
5320 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5321 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5323 QTest::newRow("vertical, bottom to top, enforce range")
5324 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5325 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5327 QTest::newRow("horizontal, left to right, enforce range")
5328 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5329 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5331 QTest::newRow("horizontal, right to left, enforce range")
5332 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5333 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5336 void tst_QQuickListView::snapOneItem()
5338 QFETCH(QQuickListView::Orientation, orientation);
5339 QFETCH(Qt::LayoutDirection, layoutDirection);
5340 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5341 QFETCH(int, highlightRangeMode);
5342 QFETCH(QPoint, flickStart);
5343 QFETCH(QPoint, flickEnd);
5344 QFETCH(qreal, snapAlignment);
5345 QFETCH(qreal, endExtent);
5346 QFETCH(qreal, startExtent);
5349 // This test seems to be unreliable - different test data fails on different runs
5350 QSKIP("QTBUG-24338");
5353 QQuickView *canvas = getView();
5355 canvas->setSource(testFileUrl("snapOneItem.qml"));
5357 qApp->processEvents();
5359 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5360 QTRY_VERIFY(listview != 0);
5362 listview->setOrientation(orientation);
5363 listview->setLayoutDirection(layoutDirection);
5364 listview->setVerticalLayoutDirection(verticalLayoutDirection);
5365 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5366 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5368 QQuickItem *contentItem = listview->contentItem();
5369 QTRY_VERIFY(contentItem != 0);
5371 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5373 // confirm that a flick hits the next item boundary
5374 flick(canvas, flickStart, flickEnd, 180);
5375 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5376 if (orientation == QQuickListView::Vertical)
5377 QCOMPARE(listview->contentY(), snapAlignment);
5379 QCOMPARE(listview->contentX(), snapAlignment);
5381 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5382 QCOMPARE(listview->currentIndex(), 1);
5383 QCOMPARE(currentIndexSpy.count(), 1);
5388 flick(canvas, flickStart, flickEnd, 180);
5389 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5390 } while (orientation == QQuickListView::Vertical
5391 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5392 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5394 if (orientation == QQuickListView::Vertical)
5395 QCOMPARE(listview->contentY(), endExtent);
5397 QCOMPARE(listview->contentX(), endExtent);
5399 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5400 QCOMPARE(listview->currentIndex(), 3);
5401 QCOMPARE(currentIndexSpy.count(), 3);
5406 flick(canvas, flickEnd, flickStart, 180);
5407 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5408 } while (orientation == QQuickListView::Vertical
5409 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5410 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5412 if (orientation == QQuickListView::Vertical)
5413 QCOMPARE(listview->contentY(), startExtent);
5415 QCOMPARE(listview->contentX(), startExtent);
5417 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5418 QCOMPARE(listview->currentIndex(), 0);
5419 QCOMPARE(currentIndexSpy.count(), 6);
5422 releaseView(canvas);
5425 void tst_QQuickListView::unrequestedVisibility()
5428 for (int i = 0; i < 30; i++)
5429 model.addItem("Item" + QString::number(i), QString::number(i));
5431 QQuickView *canvas = new QQuickView(0);
5432 canvas->setGeometry(0,0,240,320);
5434 QQmlContext *ctxt = canvas->rootContext();
5435 ctxt->setContextProperty("testModel", &model);
5436 ctxt->setContextProperty("testWrap", QVariant(false));
5438 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5440 qApp->processEvents();
5442 QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
5443 QTRY_VERIFY(leftview != 0);
5445 QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
5446 QTRY_VERIFY(rightview != 0);
5448 QQuickItem *leftContent = leftview->contentItem();
5449 QTRY_VERIFY(leftContent != 0);
5451 QQuickItem *rightContent = rightview->contentItem();
5452 QTRY_VERIFY(rightContent != 0);
5454 rightview->setCurrentIndex(20);
5456 QTRY_COMPARE(leftview->contentY(), 0.0);
5457 QTRY_COMPARE(rightview->contentY(), 100.0);
5461 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5462 QCOMPARE(delegateVisible(item), true);
5463 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5464 QCOMPARE(delegateVisible(item), false);
5466 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5467 QCOMPARE(delegateVisible(item), false);
5468 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5469 QCOMPARE(delegateVisible(item), true);
5471 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5472 QCOMPARE(delegateVisible(item), true);
5473 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5474 QCOMPARE(delegateVisible(item), false);
5475 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5476 QCOMPARE(delegateVisible(item), false);
5477 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5478 QCOMPARE(delegateVisible(item), true);
5480 rightview->setCurrentIndex(0);
5482 QTRY_COMPARE(leftview->contentY(), 0.0);
5483 QTRY_COMPARE(rightview->contentY(), 0.0);
5485 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5486 QCOMPARE(delegateVisible(item), true);
5487 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5488 QTRY_COMPARE(delegateVisible(item), true);
5490 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5491 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5493 leftview->setCurrentIndex(20);
5495 QTRY_COMPARE(leftview->contentY(), 100.0);
5496 QTRY_COMPARE(rightview->contentY(), 0.0);
5498 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5499 QTRY_COMPARE(delegateVisible(item), false);
5500 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5501 QCOMPARE(delegateVisible(item), true);
5503 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5504 QCOMPARE(delegateVisible(item), true);
5505 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5506 QCOMPARE(delegateVisible(item), false);
5508 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5509 QCOMPARE(delegateVisible(item), false);
5510 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5511 QCOMPARE(delegateVisible(item), true);
5512 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5513 QCOMPARE(delegateVisible(item), true);
5514 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5515 QCOMPARE(delegateVisible(item), false);
5517 model.moveItems(19, 1, 1);
5518 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5520 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5521 QCOMPARE(delegateVisible(item), false);
5522 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5523 QCOMPARE(delegateVisible(item), true);
5525 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5526 QCOMPARE(delegateVisible(item), true);
5527 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5528 QCOMPARE(delegateVisible(item), false);
5530 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5531 QCOMPARE(delegateVisible(item), false);
5532 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5533 QCOMPARE(delegateVisible(item), true);
5534 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5535 QCOMPARE(delegateVisible(item), true);
5536 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5537 QCOMPARE(delegateVisible(item), false);
5539 model.moveItems(3, 4, 1);
5540 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5542 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5543 QCOMPARE(delegateVisible(item), false);
5544 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5545 QCOMPARE(delegateVisible(item), true);
5546 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5547 QCOMPARE(delegateVisible(item), true);
5548 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5549 QCOMPARE(delegateVisible(item), false);
5551 model.moveItems(4, 3, 1);
5552 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5554 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5555 QCOMPARE(delegateVisible(item), false);
5556 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5557 QCOMPARE(delegateVisible(item), true);
5558 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5559 QCOMPARE(delegateVisible(item), true);
5560 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5561 QCOMPARE(delegateVisible(item), false);
5563 model.moveItems(16, 17, 1);
5564 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5566 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5567 QCOMPARE(delegateVisible(item), false);
5568 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5569 QCOMPARE(delegateVisible(item), true);
5570 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5571 QCOMPARE(delegateVisible(item), true);
5572 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5573 QCOMPARE(delegateVisible(item), false);
5575 model.moveItems(17, 16, 1);
5576 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5578 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5579 QCOMPARE(delegateVisible(item), false);
5580 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5581 QCOMPARE(delegateVisible(item), true);
5582 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5583 QCOMPARE(delegateVisible(item), true);
5584 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5585 QCOMPARE(delegateVisible(item), false);
5590 void tst_QQuickListView::populateTransitions()
5592 QFETCH(bool, staticallyPopulate);
5593 QFETCH(bool, dynamicallyPopulate);
5594 QFETCH(bool, usePopulateTransition);
5596 QPointF transitionFrom(-50, -50);
5597 QPointF transitionVia(100, 100);
5598 QaimModel model_transitionFrom;
5599 QaimModel model_transitionVia;
5602 if (staticallyPopulate) {
5603 for (int i = 0; i < 30; i++)
5604 model.addItem("item" + QString::number(i), "");
5607 QQuickView *canvas = getView();
5608 canvas->rootContext()->setContextProperty("testModel", &model);
5609 canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
5610 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5611 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5612 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5613 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
5614 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5615 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5616 canvas->setSource(testFileUrl("populateTransitions.qml"));
5618 QTest::qWaitForWindowShown(canvas);
5620 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5622 QQuickItem *contentItem = listview->contentItem();
5623 QVERIFY(contentItem);
5625 if (staticallyPopulate && usePopulateTransition) {
5626 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5627 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5628 } else if (dynamicallyPopulate) {
5629 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5630 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5632 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5633 QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5634 QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5637 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5638 for (int i=0; i < model.count() && i < itemCount; ++i) {
5639 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5640 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5641 QTRY_COMPARE(item->x(), 0.0);
5642 QTRY_COMPARE(item->y(), i*20.0);
5643 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5645 QTRY_COMPARE(name->text(), model.name(i));
5648 listview->setProperty("countPopulateTransitions", 0);
5649 listview->setProperty("countAddTransitions", 0);
5651 // add an item and check this is done with add transition, not populate
5652 model.insertItem(0, "another item", "");
5653 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5654 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5657 canvas->rootContext()->setContextProperty("testModel", QVariant());
5658 QTRY_COMPARE(listview->count(), 0);
5659 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5660 listview->setProperty("countPopulateTransitions", 0);
5661 listview->setProperty("countAddTransitions", 0);
5663 // set to a valid model and check populate transition is run a second time
5665 for (int i = 0; i < 30; i++)
5666 model.addItem("item" + QString::number(i), "");
5667 canvas->rootContext()->setContextProperty("testModel", &model);
5668 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5669 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5671 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5672 for (int i=0; i < model.count() && i < itemCount; ++i) {
5673 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5674 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5675 QTRY_COMPARE(item->x(), 0.0);
5676 QTRY_COMPARE(item->y(), i*20.0);
5677 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5679 QTRY_COMPARE(name->text(), model.name(i));
5682 // reset model and check populate transition is run again
5683 listview->setProperty("countPopulateTransitions", 0);
5684 listview->setProperty("countAddTransitions", 0);
5686 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5687 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5689 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5690 for (int i=0; i < model.count() && i < itemCount; ++i) {
5691 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5692 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5693 QTRY_COMPARE(item->x(), 0.0);
5694 QTRY_COMPARE(item->y(), i*20.0);
5695 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5697 QTRY_COMPARE(name->text(), model.name(i));
5700 releaseView(canvas);
5703 void tst_QQuickListView::populateTransitions_data()
5705 QTest::addColumn<bool>("staticallyPopulate");
5706 QTest::addColumn<bool>("dynamicallyPopulate");
5707 QTest::addColumn<bool>("usePopulateTransition");
5709 QTest::newRow("static") << true << false << true;
5710 QTest::newRow("static, no populate") << true << false << false;
5712 QTest::newRow("dynamic") << false << true << true;
5713 QTest::newRow("dynamic, no populate") << false << true << false;
5715 QTest::newRow("empty to start with") << false << false << true;
5716 QTest::newRow("empty to start with, no populate") << false << false << false;
5719 void tst_QQuickListView::addTransitions()
5721 QFETCH(int, initialItemCount);
5722 QFETCH(bool, shouldAnimateTargets);
5723 QFETCH(qreal, contentY);
5724 QFETCH(int, insertionIndex);
5725 QFETCH(int, insertionCount);
5726 QFETCH(ListRange, expectedDisplacedIndexes);
5728 // added items should start here
5729 QPointF targetItems_transitionFrom(-50, -50);
5731 // displaced items should pass through this point
5732 QPointF displacedItems_transitionVia(100, 100);
5735 for (int i = 0; i < initialItemCount; i++)
5736 model.addItem("Original item" + QString::number(i), "");
5737 QaimModel model_targetItems_transitionFrom;
5738 QaimModel model_displacedItems_transitionVia;
5740 QQuickView *canvas = getView();
5741 QQmlContext *ctxt = canvas->rootContext();
5742 TestObject *testObject = new TestObject;
5743 ctxt->setContextProperty("testModel", &model);
5744 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5745 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5746 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5747 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5748 ctxt->setContextProperty("testObject", testObject);
5749 canvas->setSource(testFileUrl("addTransitions.qml"));
5751 QTest::qWaitForWindowShown(canvas);
5753 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5754 QTRY_VERIFY(listview != 0);
5755 QQuickItem *contentItem = listview->contentItem();
5756 QVERIFY(contentItem != 0);
5757 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5759 if (contentY != 0) {
5760 listview->setContentY(contentY);
5761 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5764 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5766 // only target items that will become visible should be animated
5767 QList<QPair<QString, QString> > newData;
5768 QList<QPair<QString, QString> > expectedTargetData;
5769 QList<int> targetIndexes;
5770 if (shouldAnimateTargets) {
5771 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5772 newData << qMakePair(QString("New item %1").arg(i), QString(""));
5774 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
5775 expectedTargetData << newData.last();
5779 QVERIFY(expectedTargetData.count() > 0);
5783 if (!newData.isEmpty()) {
5784 model.insertItems(insertionIndex, newData);
5785 QTRY_COMPARE(model.count(), listview->count());
5788 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5790 if (shouldAnimateTargets) {
5791 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5792 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5793 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5795 // check the target and displaced items were animated
5796 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5797 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5799 // check attached properties
5800 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5801 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5802 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5803 if (expectedDisplacedIndexes.isValid()) {
5804 // adjust expectedDisplacedIndexes to their final values after the move
5805 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5806 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5807 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5808 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5812 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5813 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5816 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5817 int firstVisibleIndex = -1;
5818 int itemCount = items.count();
5819 for (int i=0; i<items.count(); i++) {
5820 if (items[i]->y() >= contentY) {
5821 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5822 firstVisibleIndex = e.evaluate().toInt();
5826 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5828 // verify all items moved to the correct final positions
5829 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5830 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5831 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5832 QTRY_COMPARE(item->y(), i*20.0);
5833 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5835 QTRY_COMPARE(name->text(), model.name(i));
5838 releaseView(canvas);
5842 void tst_QQuickListView::addTransitions_data()
5844 QTest::addColumn<int>("initialItemCount");
5845 QTest::addColumn<qreal>("contentY");
5846 QTest::addColumn<bool>("shouldAnimateTargets");
5847 QTest::addColumn<int>("insertionIndex");
5848 QTest::addColumn<int>("insertionCount");
5849 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5851 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5852 QTest::newRow("insert 1, just before start")
5853 << 30 << 20.0 << false
5854 << 0 << 1 << ListRange();
5855 QTest::newRow("insert 1, way before start")
5856 << 30 << 20.0 << false
5857 << 0 << 1 << ListRange();
5858 QTest::newRow("insert multiple, just before start")
5859 << 30 << 100.0 << false
5860 << 0 << 3 << ListRange();
5861 QTest::newRow("insert multiple, way before start")
5862 << 30 << 100.0 << false
5863 << 0 << 3 << ListRange();
5865 QTest::newRow("insert 1 at start")
5866 << 30 << 0.0 << true
5867 << 0 << 1 << ListRange(0, 15);
5868 QTest::newRow("insert multiple at start")
5869 << 30 << 0.0 << true
5870 << 0 << 3 << ListRange(0, 15);
5871 QTest::newRow("insert 1 at start, content y not 0")
5872 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5873 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5874 QTest::newRow("insert multiple at start, content y not 0")
5875 << 30 << 40.0 << true // first visible is index 2
5876 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5878 QTest::newRow("insert 1 at start, to empty list")
5880 << 0 << 1 << ListRange();
5881 QTest::newRow("insert multiple at start, to empty list")
5883 << 0 << 3 << ListRange();
5885 QTest::newRow("insert 1 at middle")
5886 << 30 << 0.0 << true
5887 << 5 << 1 << ListRange(5, 15);
5888 QTest::newRow("insert multiple at middle")
5889 << 30 << 0.0 << true
5890 << 5 << 3 << ListRange(5, 15);
5892 QTest::newRow("insert 1 at bottom")
5893 << 30 << 0.0 << true
5894 << 15 << 1 << ListRange(15, 15);
5895 QTest::newRow("insert multiple at bottom")
5896 << 30 << 0.0 << true
5897 << 15 << 3 << ListRange(15, 15);
5898 QTest::newRow("insert 1 at bottom, content y not 0")
5899 << 30 << 20.0 * 3 << true
5900 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5901 QTest::newRow("insert multiple at bottom, content y not 0")
5902 << 30 << 20.0 * 3 << true
5903 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5905 // items added after the last visible will not be animated in, since they
5906 // do not appear in the final view
5907 QTest::newRow("insert 1 after end")
5908 << 30 << 0.0 << false
5909 << 17 << 1 << ListRange();
5910 QTest::newRow("insert multiple after end")
5911 << 30 << 0.0 << false
5912 << 17 << 3 << ListRange();
5915 void tst_QQuickListView::moveTransitions()
5917 QFETCH(int, initialItemCount);
5918 QFETCH(qreal, contentY);
5919 QFETCH(qreal, itemsOffsetAfterMove);
5920 QFETCH(int, moveFrom);
5921 QFETCH(int, moveTo);
5922 QFETCH(int, moveCount);
5923 QFETCH(ListRange, expectedDisplacedIndexes);
5925 // target and displaced items should pass through these points
5926 QPointF targetItems_transitionVia(-50, 50);
5927 QPointF displacedItems_transitionVia(100, 100);
5930 for (int i = 0; i < initialItemCount; i++)
5931 model.addItem("Original item" + QString::number(i), "");
5932 QaimModel model_targetItems_transitionVia;
5933 QaimModel model_displacedItems_transitionVia;
5935 QQuickView *canvas = getView();
5936 QQmlContext *ctxt = canvas->rootContext();
5937 TestObject *testObject = new TestObject;
5938 ctxt->setContextProperty("testModel", &model);
5939 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5940 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5941 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5942 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5943 ctxt->setContextProperty("testObject", testObject);
5944 canvas->setSource(testFileUrl("moveTransitions.qml"));
5946 QTest::qWaitForWindowShown(canvas);
5948 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5949 QTRY_VERIFY(listview != 0);
5950 QQuickItem *contentItem = listview->contentItem();
5951 QVERIFY(contentItem != 0);
5954 if (contentY != 0) {
5955 listview->setContentY(contentY);
5956 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5959 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5961 // Items moving to *or* from visible positions should be animated.
5962 // Otherwise, they should not be animated.
5963 QList<QPair<QString, QString> > expectedTargetData;
5964 QList<int> targetIndexes;
5965 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5966 int toIndex = moveTo + (i - moveFrom);
5967 if (i <= (contentY + listview->height()) / 20
5968 || toIndex < (contentY + listview->height()) / 20) {
5969 expectedTargetData << qMakePair(model.name(i), model.number(i));
5973 // ViewTransition.index provides the indices that items are moving to, not from
5974 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5977 model.moveItems(moveFrom, moveTo, moveCount);
5979 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5980 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5981 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5983 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5985 // check the target and displaced items were animated
5986 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5987 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5989 // check attached properties
5990 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5991 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5992 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5993 if (expectedDisplacedIndexes.isValid()) {
5994 // adjust expectedDisplacedIndexes to their final values after the move
5995 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5996 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5997 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5998 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6001 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6002 int firstVisibleIndex = -1;
6003 for (int i=0; i<items.count(); i++) {
6004 if (items[i]->y() >= contentY) {
6005 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6006 firstVisibleIndex = e.evaluate().toInt();
6010 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6012 // verify all items moved to the correct final positions
6013 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
6014 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6015 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6016 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6017 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
6018 name = findItem<QQuickText>(contentItem, "textName", i);
6020 QTRY_COMPARE(name->text(), model.name(i));
6023 releaseView(canvas);
6027 void tst_QQuickListView::moveTransitions_data()
6029 QTest::addColumn<int>("initialItemCount");
6030 QTest::addColumn<qreal>("contentY");
6031 QTest::addColumn<qreal>("itemsOffsetAfterMove");
6032 QTest::addColumn<int>("moveFrom");
6033 QTest::addColumn<int>("moveTo");
6034 QTest::addColumn<int>("moveCount");
6035 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6037 // when removing from above the visible, all items shift down depending on how many
6038 // items have been removed from above the visible
6039 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
6040 << 1 << 10 << 1 << ListRange(11, 15+4);
6041 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
6042 << 0 << 10 << 1 << ListRange(11, 15+4);
6043 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
6044 << 1 << 10 << 2 << ListRange(12, 15+4);
6045 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
6046 << 0 << 10 << 3 << ListRange(13, 15+4);
6047 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
6048 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
6049 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
6050 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
6052 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
6053 << 1 << 10 << 1 << ListRange(2, 10);
6054 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
6055 << 0 << 10 << 1 << ListRange(1, 10);
6056 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6057 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
6058 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
6059 << 10 << 15 << 1 << ListRange(11, 15);
6060 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
6061 << 0 << 15 << 1 << ListRange(1, 15);
6063 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
6064 << 1 << 10 << 3 << ListRange(4, 12);
6065 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
6066 << 0 << 10 << 3 << ListRange(3, 12);
6067 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6068 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
6069 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
6070 << 5 << 13 << 3 << ListRange(8, 15);
6071 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
6072 << 0 << 13 << 3 << ListRange(3, 15);
6074 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
6075 << 10 << 1 << 1 << ListRange(1, 9);
6076 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
6077 << 10 << 0 << 1 << ListRange(0, 9);
6078 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6079 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6080 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
6081 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6082 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
6083 << 15 << 10 << 1 << ListRange(10, 14);
6084 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
6085 << 15 << 0 << 1 << ListRange(0, 14);
6087 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
6088 << 10 << 1 << 3 << ListRange(1, 9);
6089 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
6090 << 10 << 0 << 3 << ListRange(0, 9);
6091 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6092 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
6093 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
6094 << 13 << 5 << 3 << ListRange(5, 12);
6095 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
6096 << 13 << 0 << 3 << ListRange(0, 12);
6098 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
6099 << 20 << 0 << 1 << ListRange(0, 15);
6100 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6101 << 25 << 4 << 1 << ListRange(0+4, 15+4);
6102 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
6103 << 20 << 0 << 3 << ListRange(0, 15);
6104 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6105 << 25 << 4 << 3 << ListRange(0+4, 15+4);
6107 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
6108 << 20 << 15 << 1 << ListRange(15, 15);
6109 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6110 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
6111 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6112 << 20 << 15 << 3 << ListRange(15, 15);
6113 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6114 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6117 void tst_QQuickListView::removeTransitions()
6119 QFETCH(int, initialItemCount);
6120 QFETCH(bool, shouldAnimateTargets);
6121 QFETCH(qreal, contentY);
6122 QFETCH(int, removalIndex);
6123 QFETCH(int, removalCount);
6124 QFETCH(ListRange, expectedDisplacedIndexes);
6126 // added items should end here
6127 QPointF targetItems_transitionTo(-50, -50);
6129 // displaced items should pass through this points
6130 QPointF displacedItems_transitionVia(100, 100);
6133 for (int i = 0; i < initialItemCount; i++)
6134 model.addItem("Original item" + QString::number(i), "");
6135 QaimModel model_targetItems_transitionTo;
6136 QaimModel model_displacedItems_transitionVia;
6138 QQuickView *canvas = getView();
6139 QQmlContext *ctxt = canvas->rootContext();
6140 TestObject *testObject = new TestObject;
6141 ctxt->setContextProperty("testModel", &model);
6142 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6143 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6144 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6145 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6146 ctxt->setContextProperty("testObject", testObject);
6147 canvas->setSource(testFileUrl("removeTransitions.qml"));
6149 QTest::qWaitForWindowShown(canvas);
6151 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6152 QTRY_VERIFY(listview != 0);
6153 QQuickItem *contentItem = listview->contentItem();
6154 QVERIFY(contentItem != 0);
6155 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6157 if (contentY != 0) {
6158 listview->setContentY(contentY);
6159 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6162 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6164 // only target items that are visible should be animated
6165 QList<QPair<QString, QString> > expectedTargetData;
6166 QList<int> targetIndexes;
6167 if (shouldAnimateTargets) {
6168 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6169 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6170 expectedTargetData << qMakePair(model.name(i), model.number(i));
6174 QVERIFY(expectedTargetData.count() > 0);
6177 // calculate targetItems and expectedTargets before model changes
6178 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6179 QVariantMap expectedTargets;
6180 for (int i=0; i<targetIndexes.count(); i++)
6181 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6184 model.removeItems(removalIndex, removalCount);
6185 QTRY_COMPARE(model.count(), listview->count());
6187 if (shouldAnimateTargets) {
6188 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6189 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6190 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6192 // check the target and displaced items were animated
6193 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6194 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6196 // check attached properties
6197 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6198 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6199 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6200 if (expectedDisplacedIndexes.isValid()) {
6201 // adjust expectedDisplacedIndexes to their final values after the move
6202 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6203 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6204 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6205 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6208 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6209 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6212 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6213 int firstVisibleIndex = -1;
6214 int itemCount = items.count();
6216 for (int i=0; i<items.count(); i++) {
6217 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6218 int index = e.evaluate().toInt();
6219 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6220 firstVisibleIndex = index;
6222 itemCount--; // exclude deleted items
6224 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6226 // verify all items moved to the correct final positions
6227 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6228 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6229 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6230 QCOMPARE(item->x(), 0.0);
6231 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6232 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6234 QTRY_COMPARE(name->text(), model.name(i));
6237 releaseView(canvas);
6241 void tst_QQuickListView::removeTransitions_data()
6243 QTest::addColumn<int>("initialItemCount");
6244 QTest::addColumn<qreal>("contentY");
6245 QTest::addColumn<bool>("shouldAnimateTargets");
6246 QTest::addColumn<int>("removalIndex");
6247 QTest::addColumn<int>("removalCount");
6248 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6250 // All items that are visible following the remove operation should be animated.
6251 // Remove targets that are outside of the view should not be animated.
6253 QTest::newRow("remove 1 before start")
6254 << 30 << 20.0 * 3 << false
6255 << 2 << 1 << ListRange();
6256 QTest::newRow("remove multiple, all before start")
6257 << 30 << 20.0 * 3 << false
6258 << 0 << 3 << ListRange();
6259 QTest::newRow("remove mix of before and after start")
6260 << 30 << 20.0 * 3 << true
6261 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
6263 QTest::newRow("remove 1 from start")
6264 << 30 << 0.0 << true
6265 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
6266 QTest::newRow("remove multiple from start")
6267 << 30 << 0.0 << true
6268 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
6269 QTest::newRow("remove 1 from start, content y not 0")
6270 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
6271 << 2 << 1 << ListRange(1 + 2, 16 + 2);
6272 QTest::newRow("remove multiple from start, content y not 0")
6273 << 30 << 20.0 * 2 << true // first visible is index 2
6274 << 2 << 3 << ListRange(3 + 2, 18 + 2);
6276 QTest::newRow("remove 1 from middle")
6277 << 30 << 0.0 << true
6278 << 5 << 1 << ListRange(6, 16);
6279 QTest::newRow("remove multiple from middle")
6280 << 30 << 0.0 << true
6281 << 5 << 3 << ListRange(8, 18);
6284 QTest::newRow("remove 1 from bottom")
6285 << 30 << 0.0 << true
6286 << 15 << 1 << ListRange(16, 16);
6288 // remove 15, 16, 17
6289 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6290 // the view, and 18 will be animated as the displaced item to replace the last item
6291 QTest::newRow("remove multiple from bottom")
6292 << 30 << 0.0 << true
6293 << 15 << 3 << ListRange(18, 18);
6295 QTest::newRow("remove 1 from bottom, content y not 0")
6296 << 30 << 20.0 * 2 << true
6297 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6298 QTest::newRow("remove multiple from bottom, content y not 0")
6299 << 30 << 20.0 * 2 << true
6300 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6303 QTest::newRow("remove 1 after end")
6304 << 30 << 0.0 << false
6305 << 17 << 1 << ListRange();
6306 QTest::newRow("remove multiple after end")
6307 << 30 << 0.0 << false
6308 << 17 << 3 << ListRange();
6311 void tst_QQuickListView::displacedTransitions()
6313 QFETCH(bool, useDisplaced);
6314 QFETCH(bool, displacedEnabled);
6315 QFETCH(bool, useAddDisplaced);
6316 QFETCH(bool, addDisplacedEnabled);
6317 QFETCH(bool, useMoveDisplaced);
6318 QFETCH(bool, moveDisplacedEnabled);
6319 QFETCH(bool, useRemoveDisplaced);
6320 QFETCH(bool, removeDisplacedEnabled);
6321 QFETCH(ListChange, change);
6322 QFETCH(ListRange, expectedDisplacedIndexes);
6325 for (int i = 0; i < 30; i++)
6326 model.addItem("Original item" + QString::number(i), "");
6327 QaimModel model_displaced_transitionVia;
6328 QaimModel model_addDisplaced_transitionVia;
6329 QaimModel model_moveDisplaced_transitionVia;
6330 QaimModel model_removeDisplaced_transitionVia;
6332 QPointF displaced_transitionVia(-50, -100);
6333 QPointF addDisplaced_transitionVia(-150, 100);
6334 QPointF moveDisplaced_transitionVia(50, -100);
6335 QPointF removeDisplaced_transitionVia(150, 100);
6337 QQuickView *canvas = getView();
6338 QQmlContext *ctxt = canvas->rootContext();
6339 TestObject *testObject = new TestObject(canvas);
6340 ctxt->setContextProperty("testModel", &model);
6341 ctxt->setContextProperty("testObject", testObject);
6342 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6343 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6344 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6345 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6346 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6347 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6348 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6349 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6350 ctxt->setContextProperty("useDisplaced", useDisplaced);
6351 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6352 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6353 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6354 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6355 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6356 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6357 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6358 canvas->setSource(testFileUrl("displacedTransitions.qml"));
6360 qApp->processEvents();
6362 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6363 QTRY_VERIFY(listview != 0);
6364 QQuickItem *contentItem = listview->contentItem();
6365 QVERIFY(contentItem != 0);
6366 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6368 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6369 listview->setProperty("displaceTransitionsDone", false);
6371 switch (change.type) {
6372 case ListChange::Inserted:
6374 QList<QPair<QString, QString> > targetItemData;
6375 for (int i=change.index; i<change.index + change.count; ++i)
6376 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6377 model.insertItems(change.index, targetItemData);
6378 QTRY_COMPARE(model.count(), listview->count());
6381 case ListChange::Removed:
6382 model.removeItems(change.index, change.count);
6383 QTRY_COMPARE(model.count(), listview->count());
6385 case ListChange::Moved:
6386 model.moveItems(change.index, change.to, change.count);
6387 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6389 case ListChange::SetCurrent:
6390 case ListChange::SetContentY:
6391 case ListChange::Polish:
6395 QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6396 QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6398 if ((useDisplaced && displacedEnabled)
6399 || (useAddDisplaced && addDisplacedEnabled)
6400 || (useMoveDisplaced && moveDisplacedEnabled)
6401 || (useRemoveDisplaced && removeDisplacedEnabled)) {
6402 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6404 // check the correct number of target items and indexes were received
6405 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6406 for (int i=0; i<resultTargetIndexes.count(); i++)
6407 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6408 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6409 for (int i=0; i<resultTargetItems.count(); i++)
6410 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6412 QCOMPARE(resultTargetIndexes.count(), 0);
6413 QCOMPARE(resultTargetItems.count(), 0);
6416 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6417 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6419 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6420 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6421 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6423 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6424 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6425 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6427 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6429 if (useDisplaced && displacedEnabled
6430 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6431 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6432 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6433 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6435 QCOMPARE(model_displaced_transitionVia.count(), 0);
6438 // verify all items moved to the correct final positions
6439 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6440 for (int i=0; i < model.count() && i < items.count(); ++i) {
6441 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6442 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6443 QCOMPARE(item->x(), 0.0);
6444 QCOMPARE(item->y(), i * 20.0);
6445 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6447 QTRY_COMPARE(name->text(), model.name(i));
6450 releaseView(canvas);
6453 void tst_QQuickListView::displacedTransitions_data()
6455 QTest::addColumn<bool>("useDisplaced");
6456 QTest::addColumn<bool>("displacedEnabled");
6457 QTest::addColumn<bool>("useAddDisplaced");
6458 QTest::addColumn<bool>("addDisplacedEnabled");
6459 QTest::addColumn<bool>("useMoveDisplaced");
6460 QTest::addColumn<bool>("moveDisplacedEnabled");
6461 QTest::addColumn<bool>("useRemoveDisplaced");
6462 QTest::addColumn<bool>("removeDisplacedEnabled");
6463 QTest::addColumn<ListChange>("change");
6464 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6466 QTest::newRow("no displaced transitions at all")
6471 << ListChange::insert(0, 1) << ListRange(0, 15);
6473 QTest::newRow("just displaced")
6478 << ListChange::insert(0, 1) << ListRange(0, 15);
6480 QTest::newRow("just displaced (not enabled)")
6485 << ListChange::insert(0, 1) << ListRange(0, 15);
6487 QTest::newRow("displaced + addDisplaced")
6492 << ListChange::insert(0, 1) << ListRange(0, 15);
6494 QTest::newRow("displaced + addDisplaced (not enabled)")
6499 << ListChange::insert(0, 1) << ListRange(0, 15);
6501 QTest::newRow("displaced + moveDisplaced")
6506 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6508 QTest::newRow("displaced + moveDisplaced (not enabled)")
6513 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6515 QTest::newRow("displaced + removeDisplaced")
6520 << ListChange::remove(0, 1) << ListRange(1, 16);
6522 QTest::newRow("displaced + removeDisplaced (not enabled)")
6527 << ListChange::remove(0, 1) << ListRange(1, 16);
6530 QTest::newRow("displaced + add, should use generic displaced for a remove")
6535 << ListChange::remove(0, 1) << ListRange(1, 16);
6538 void tst_QQuickListView::multipleTransitions()
6540 // Tests that if you interrupt a transition in progress with another action that
6541 // cancels the previous transition, the resulting items are still placed correctly.
6543 QFETCH(int, initialCount);
6544 QFETCH(qreal, contentY);
6545 QFETCH(QList<ListChange>, changes);
6546 QFETCH(bool, enableAddTransitions);
6547 QFETCH(bool, enableMoveTransitions);
6548 QFETCH(bool, enableRemoveTransitions);
6549 QFETCH(bool, rippleAddDisplaced);
6551 QPointF addTargets_transitionFrom(-50, -50);
6552 QPointF addDisplaced_transitionFrom(-50, 50);
6553 QPointF moveTargets_transitionFrom(50, -50);
6554 QPointF moveDisplaced_transitionFrom(50, 50);
6555 QPointF removeTargets_transitionTo(-100, 300);
6556 QPointF removeDisplaced_transitionFrom(100, 300);
6559 for (int i = 0; i < initialCount; i++)
6560 model.addItem("Original item" + QString::number(i), "");
6562 QQuickView *canvas = getView();
6563 QQmlContext *ctxt = canvas->rootContext();
6564 TestObject *testObject = new TestObject;
6565 ctxt->setContextProperty("testModel", &model);
6566 ctxt->setContextProperty("testObject", testObject);
6567 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6568 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6569 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6570 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6571 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6572 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6573 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6574 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6575 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6576 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6577 canvas->setSource(testFileUrl("multipleTransitions.qml"));
6579 QTest::qWaitForWindowShown(canvas);
6581 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6582 QTRY_VERIFY(listview != 0);
6583 QQuickItem *contentItem = listview->contentItem();
6584 QVERIFY(contentItem != 0);
6585 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6587 if (contentY != 0) {
6588 listview->setContentY(contentY);
6589 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6592 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
6594 for (int i=0; i<changes.count(); i++) {
6595 switch (changes[i].type) {
6596 case ListChange::Inserted:
6598 QList<QPair<QString, QString> > targetItems;
6599 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6600 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6601 model.insertItems(changes[i].index, targetItems);
6602 QTRY_COMPARE(model.count(), listview->count());
6603 if (i == changes.count() - 1) {
6604 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6605 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6607 QTest::qWait(timeBetweenActions);
6611 case ListChange::Removed:
6612 model.removeItems(changes[i].index, changes[i].count);
6613 QTRY_COMPARE(model.count(), listview->count());
6614 if (i == changes.count() - 1) {
6615 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6616 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6618 QTest::qWait(timeBetweenActions);
6621 case ListChange::Moved:
6622 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6623 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6624 if (i == changes.count() - 1) {
6625 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6626 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6628 QTest::qWait(timeBetweenActions);
6631 case ListChange::SetCurrent:
6632 listview->setCurrentIndex(changes[i].index);
6634 case ListChange::SetContentY:
6635 listview->setContentY(changes[i].pos);
6636 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6638 case ListChange::Polish:
6642 QCOMPARE(listview->count(), model.count());
6644 // verify all items moved to the correct final positions
6645 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6646 for (int i=0; i < model.count() && i < items.count(); ++i) {
6647 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6648 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6649 QTRY_COMPARE(item->x(), 0.0);
6650 QTRY_COMPARE(item->y(), i*20.0);
6651 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6653 QTRY_COMPARE(name->text(), model.name(i));
6656 releaseView(canvas);
6660 void tst_QQuickListView::multipleTransitions_data()
6662 QTest::addColumn<int>("initialCount");
6663 QTest::addColumn<qreal>("contentY");
6664 QTest::addColumn<QList<ListChange> >("changes");
6665 QTest::addColumn<bool>("enableAddTransitions");
6666 QTest::addColumn<bool>("enableMoveTransitions");
6667 QTest::addColumn<bool>("enableRemoveTransitions");
6668 QTest::addColumn<bool>("rippleAddDisplaced");
6670 // the added item and displaced items should move to final dest correctly
6671 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6672 << ListChange::insert(0, 1)
6673 << ListChange::move(0, 3, 1)
6675 << true << true << true << false;
6677 // items affected by the add should change from move to add transition
6678 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6679 << ListChange::move(1, 10, 3)
6680 << ListChange::insert(0, 1)
6682 << true << true << true << false;
6684 // items should be placed correctly if you trigger a transition then refill for that index
6685 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6686 << ListChange::insert(0, 1)
6687 << ListChange::setContentY(80.0)
6688 << ListChange::setContentY(0.0)
6689 << ListChange::insert(0, 1)
6691 << true << true << true << false;
6693 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6694 << ListChange::insert(1, 1)
6695 << ListChange::remove(1, 1)
6697 << true << true << true << true;
6699 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6700 // even if a remove-displace transition is not present to re-animate them
6701 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6702 << ListChange::insert(0, 1)
6703 << ListChange::remove(2, 1)
6705 << true << true << false << false;
6707 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6708 // remove has changed the position of where it will move to
6709 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6710 << ListChange::setContentY(-10.0)
6711 << ListChange::insert(0, 1)
6712 << ListChange::insert(0, 1)
6713 << ListChange::remove(2, 1)
6715 << true << true << false << false;
6718 void tst_QQuickListView::multipleDisplaced()
6720 // multiple move() operations should only restart displace transitions for items that
6721 // moved from previously set positions, and not those that have moved from their current
6722 // item positions (which may e.g. still be changing from easing bounces in the last transition)
6725 for (int i = 0; i < 30; i++)
6726 model.addItem("Original item" + QString::number(i), "");
6728 QQuickView *canvas = getView();
6729 QQmlContext *ctxt = canvas->rootContext();
6730 ctxt->setContextProperty("testModel", &model);
6731 ctxt->setContextProperty("testObject", new TestObject(canvas));
6732 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6734 QTest::qWaitForWindowShown(canvas);
6736 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6737 QTRY_VERIFY(listview != 0);
6738 QQuickItem *contentItem = listview->contentItem();
6739 QVERIFY(contentItem != 0);
6740 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6742 model.moveItems(12, 8, 1);
6743 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6744 model.moveItems(8, 3, 1);
6745 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6747 QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6748 foreach (const QString &name, transitionsStarted.keys()) {
6749 QVERIFY2(transitionsStarted[name] == 1,
6750 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6753 // verify all items moved to the correct final positions
6754 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6755 for (int i=0; i < model.count() && i < items.count(); ++i) {
6756 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6757 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6758 QTRY_COMPARE(item->x(), 0.0);
6759 QTRY_COMPARE(item->y(), i*20.0);
6760 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6762 QTRY_COMPARE(name->text(), model.name(i));
6765 releaseView(canvas);
6768 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6772 for (int i=0; i<list.count(); i++) {
6773 ret << list[i].toInt(&ok);
6775 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6781 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6783 for (int i=0; i<indexLists.count(); i++) {
6784 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6785 if (current != expectedIndexes.toSet())
6786 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6787 QCOMPARE(current, expectedIndexes.toSet());
6791 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6793 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6794 QVERIFY(it.value().type() == QVariant::Int);
6795 QString name = it.key();
6796 int itemIndex = it.value().toInt();
6797 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6798 if (model.name(itemIndex) != name)
6799 qDebug() << itemIndex;
6800 QCOMPARE(model.name(itemIndex), name);
6802 QCOMPARE(items.count(), expectedIndexes.count());
6805 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6807 for (int i=0; i<itemLists.count(); i++) {
6808 QVERIFY(itemLists[i].type() == QVariant::List);
6809 QVariantList current = itemLists[i].toList();
6810 for (int j=0; j<current.count(); j++) {
6811 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6812 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6813 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6815 QCOMPARE(current.count(), expectedItems.count());
6819 void tst_QQuickListView::flickBeyondBounds()
6821 QQuickView *canvas = createView();
6823 canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6825 qApp->processEvents();
6827 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6828 QTRY_VERIFY(listview != 0);
6830 QQuickItem *contentItem = listview->contentItem();
6831 QTRY_VERIFY(contentItem != 0);
6832 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6834 // Flick view up beyond bounds
6835 flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6836 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6838 // We're really testing that we don't get stuck in a loop,
6839 // but also confirm items positioned correctly.
6840 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6841 for (int i = 0; i < 2; ++i) {
6842 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6843 if (!item) qWarning() << "Item" << i << "not found";
6845 QTRY_VERIFY(item->y() == i*45);
6851 void tst_QQuickListView::destroyItemOnCreation()
6854 QQuickView *canvas = createView();
6855 canvas->rootContext()->setContextProperty("testModel", &model);
6857 canvas->setSource(testFileUrl("destroyItemOnCreation.qml"));
6859 qApp->processEvents();
6861 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6862 QVERIFY(listview != 0);
6864 QQuickItem *contentItem = listview->contentItem();
6865 QVERIFY(contentItem != 0);
6866 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6868 QCOMPARE(canvas->rootObject()->property("createdIndex").toInt(), -1);
6869 model.addItem("new item", "");
6870 QTRY_COMPARE(canvas->rootObject()->property("createdIndex").toInt(), 0);
6872 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6873 QCOMPARE(model.count(), 0);
6878 void tst_QQuickListView::parentBinding()
6880 QQuickView *canvas = createView();
6883 QtMsgHandler old = qInstallMsgHandler(errorMsgHandler);
6885 canvas->setSource(testFileUrl("parentBinding.qml"));
6887 QTest::qWaitForWindowExposed(canvas);
6889 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
6890 QVERIFY(listview != 0);
6892 QQuickItem *contentItem = listview->contentItem();
6893 QVERIFY(contentItem != 0);
6894 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6896 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6898 QCOMPARE(item->width(), listview->width());
6899 QCOMPARE(item->height(), listview->height()/12);
6901 // there should be no transient binding error
6902 QVERIFY(!m_errorCount);
6904 qInstallMsgHandler(old);
6909 QTEST_MAIN(tst_QQuickListView)
6911 #include "tst_qquicklistview.moc"