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 qApp->processEvents();
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 qApp->processEvents();
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 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2677 QCOMPARE(listview->currentIndex(), 0);
2679 QTest::keyClick(canvas, forwardsKey);
2680 QCOMPARE(listview->currentIndex(), 1);
2682 QTest::keyClick(canvas, backwardsKey);
2683 QCOMPARE(listview->currentIndex(), 0);
2685 // hold down a key to go forwards
2686 for (int i=0; i<model.count()-1; i++) {
2687 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
2688 QTRY_COMPARE(listview->currentIndex(), i+1);
2690 QTest::keyRelease(canvas, forwardsKey);
2691 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2692 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2693 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2695 // hold down a key to go backwards
2696 for (int i=model.count()-1; i > 0; i--) {
2697 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2698 QTRY_COMPARE(listview->currentIndex(), i-1);
2700 QTest::keyRelease(canvas, backwardsKey);
2701 QTRY_COMPARE(listview->currentIndex(), 0);
2702 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2703 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2706 QVERIFY(!listview->isWrapEnabled());
2707 listview->incrementCurrentIndex();
2708 QCOMPARE(listview->currentIndex(), 1);
2709 listview->decrementCurrentIndex();
2710 QCOMPARE(listview->currentIndex(), 0);
2712 listview->decrementCurrentIndex();
2713 QCOMPARE(listview->currentIndex(), 0);
2716 listview->setWrapEnabled(true);
2717 QVERIFY(listview->isWrapEnabled());
2719 listview->decrementCurrentIndex();
2720 QCOMPARE(listview->currentIndex(), model.count()-1);
2721 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2722 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2724 listview->incrementCurrentIndex();
2725 QCOMPARE(listview->currentIndex(), 0);
2726 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2727 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2729 releaseView(canvas);
2733 void tst_QQuickListView::keyNavigation_data()
2735 QTest::addColumn<QQuickListView::Orientation>("orientation");
2736 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2737 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2738 QTest::addColumn<Qt::Key>("forwardsKey");
2739 QTest::addColumn<Qt::Key>("backwardsKey");
2740 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2741 QTest::addColumn<QPointF>("contentPosAtLastItem");
2743 QTest::newRow("Vertical, TopToBottom")
2744 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2745 << Qt::Key_Down << Qt::Key_Up
2747 << QPointF(0, 30*20 - 320);
2749 QTest::newRow("Vertical, BottomToTop")
2750 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2751 << Qt::Key_Up << Qt::Key_Down
2753 << QPointF(0, -(30 * 20));
2755 QTest::newRow("Horizontal, LeftToRight")
2756 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2757 << Qt::Key_Right << Qt::Key_Left
2759 << QPointF(30*240 - 240, 0);
2761 QTest::newRow("Horizontal, RightToLeft")
2762 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2763 << Qt::Key_Left << Qt::Key_Right
2765 << QPointF(-(30 * 240), 0);
2768 void tst_QQuickListView::itemList()
2770 QQuickView *canvas = createView();
2771 canvas->setSource(testFileUrl("itemlist.qml"));
2773 qApp->processEvents();
2775 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2776 QTRY_VERIFY(listview != 0);
2778 QQuickItem *contentItem = listview->contentItem();
2779 QTRY_VERIFY(contentItem != 0);
2781 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2782 QTRY_VERIFY(model != 0);
2784 QTRY_VERIFY(model->count() == 3);
2785 QTRY_COMPARE(listview->currentIndex(), 0);
2787 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2789 QTRY_COMPARE(item->x(), 0.0);
2790 QCOMPARE(item->height(), listview->height());
2792 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2794 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2796 listview->setCurrentIndex(2);
2798 item = findItem<QQuickItem>(contentItem, "item3");
2800 QTRY_COMPARE(item->x(), 480.0);
2802 text = findItem<QQuickText>(contentItem, "text3");
2804 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2809 void tst_QQuickListView::itemListFlicker()
2811 QQuickView *canvas = createView();
2812 canvas->setSource(testFileUrl("itemlist-flicker.qml"));
2814 qApp->processEvents();
2816 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2817 QTRY_VERIFY(listview != 0);
2819 QQuickItem *contentItem = listview->contentItem();
2820 QTRY_VERIFY(contentItem != 0);
2822 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2823 QTRY_VERIFY(model != 0);
2825 QTRY_VERIFY(model->count() == 3);
2826 QTRY_COMPARE(listview->currentIndex(), 0);
2830 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2831 QVERIFY(delegateVisible(item));
2832 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2833 QVERIFY(delegateVisible(item));
2834 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2835 QVERIFY(delegateVisible(item));
2837 listview->setCurrentIndex(1);
2839 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2840 QVERIFY(delegateVisible(item));
2841 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2842 QVERIFY(delegateVisible(item));
2843 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2844 QVERIFY(delegateVisible(item));
2846 listview->setCurrentIndex(2);
2848 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2849 QVERIFY(delegateVisible(item));
2850 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2851 QVERIFY(delegateVisible(item));
2852 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2853 QVERIFY(delegateVisible(item));
2858 void tst_QQuickListView::cacheBuffer()
2860 QQuickView *canvas = createView();
2863 for (int i = 0; i < 90; i++)
2864 model.addItem("Item" + QString::number(i), "");
2866 QQmlContext *ctxt = canvas->rootContext();
2867 ctxt->setContextProperty("testModel", &model);
2869 TestObject *testObject = new TestObject;
2870 ctxt->setContextProperty("testObject", testObject);
2872 canvas->setSource(testFileUrl("listviewtest.qml"));
2874 qApp->processEvents();
2876 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2877 QTRY_VERIFY(listview != 0);
2879 QQuickItem *contentItem = listview->contentItem();
2880 QTRY_VERIFY(contentItem != 0);
2881 QTRY_VERIFY(listview->delegate() != 0);
2882 QTRY_VERIFY(listview->model() != 0);
2883 QTRY_VERIFY(listview->highlight() != 0);
2885 // Confirm items positioned correctly
2886 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2887 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2888 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2889 if (!item) qWarning() << "Item" << i << "not found";
2891 QTRY_VERIFY(item->y() == i*20);
2894 QQmlIncubationController controller;
2895 canvas->engine()->setIncubationController(&controller);
2897 testObject->setCacheBuffer(200);
2898 QTRY_VERIFY(listview->cacheBuffer() == 200);
2900 // items will be created one at a time
2901 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2902 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2903 QQuickItem *item = 0;
2906 controller.incubateWhile(&b);
2907 item = findItem<QQuickItem>(listview, "wrapper", i);
2913 controller.incubateWhile(&b);
2916 int newItemCount = 0;
2917 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2919 // Confirm items positioned correctly
2920 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2921 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2922 if (!item) qWarning() << "Item" << i << "not found";
2924 QTRY_VERIFY(item->y() == i*20);
2927 // move view and confirm items in view are visible immediately and outside are created async
2928 listview->setContentY(300);
2930 for (int i = 15; i < 32; ++i) {
2931 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2932 if (!item) qWarning() << "Item" << i << "not found";
2934 QVERIFY(item->y() == i*20);
2937 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2939 // ensure buffered items are created
2940 for (int i = 32; i < qMin(41,model.count()); ++i) {
2941 QQuickItem *item = 0;
2943 qGuiApp->processEvents(); // allow refill to happen
2945 controller.incubateWhile(&b);
2946 item = findItem<QQuickItem>(listview, "wrapper", i);
2952 controller.incubateWhile(&b);
2959 void tst_QQuickListView::positionViewAtIndex()
2961 QQuickView *canvas = createView();
2964 for (int i = 0; i < 40; i++)
2965 model.addItem("Item" + QString::number(i), "");
2967 QQmlContext *ctxt = canvas->rootContext();
2968 ctxt->setContextProperty("testModel", &model);
2970 TestObject *testObject = new TestObject;
2971 ctxt->setContextProperty("testObject", testObject);
2973 canvas->setSource(testFileUrl("listviewtest.qml"));
2974 qApp->processEvents();
2976 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2977 QTRY_VERIFY(listview != 0);
2978 QQuickItem *contentItem = listview->contentItem();
2979 QTRY_VERIFY(contentItem != 0);
2980 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2982 // Confirm items positioned correctly
2983 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2984 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2985 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2986 if (!item) qWarning() << "Item" << i << "not found";
2988 QTRY_COMPARE(item->y(), i*20.);
2991 // Position on a currently visible item
2992 listview->positionViewAtIndex(3, QQuickListView::Beginning);
2993 QTRY_COMPARE(listview->contentY(), 60.);
2995 // Confirm items positioned correctly
2996 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2997 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2998 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2999 if (!item) qWarning() << "Item" << i << "not found";
3001 QTRY_COMPARE(item->y(), i*20.);
3004 // Position on an item beyond the visible items
3005 listview->positionViewAtIndex(22, QQuickListView::Beginning);
3006 QTRY_COMPARE(listview->contentY(), 440.);
3008 // Confirm items positioned correctly
3009 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3010 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
3011 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3012 if (!item) qWarning() << "Item" << i << "not found";
3014 QTRY_COMPARE(item->y(), i*20.);
3017 // Position on an item that would leave empty space if positioned at the top
3018 listview->positionViewAtIndex(28, QQuickListView::Beginning);
3019 QTRY_COMPARE(listview->contentY(), 480.);
3021 // Confirm items positioned correctly
3022 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3023 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
3024 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3025 if (!item) qWarning() << "Item" << i << "not found";
3027 QTRY_COMPARE(item->y(), i*20.);
3030 // Position at the beginning again
3031 listview->positionViewAtIndex(0, QQuickListView::Beginning);
3032 QTRY_COMPARE(listview->contentY(), 0.);
3034 // Confirm items positioned correctly
3035 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3036 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
3037 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3038 if (!item) qWarning() << "Item" << i << "not found";
3040 QTRY_COMPARE(item->y(), i*20.);
3043 // Position at End using last index
3044 listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
3045 QTRY_COMPARE(listview->contentY(), 480.);
3047 // Confirm items positioned correctly
3048 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3049 for (int i = 24; i < model.count(); ++i) {
3050 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3051 if (!item) qWarning() << "Item" << i << "not found";
3053 QTRY_COMPARE(item->y(), i*20.);
3057 listview->positionViewAtIndex(20, QQuickListView::End);
3058 QTRY_COMPARE(listview->contentY(), 100.);
3060 // Position in Center
3061 listview->positionViewAtIndex(15, QQuickListView::Center);
3062 QTRY_COMPARE(listview->contentY(), 150.);
3064 // Ensure at least partially visible
3065 listview->positionViewAtIndex(15, QQuickListView::Visible);
3066 QTRY_COMPARE(listview->contentY(), 150.);
3068 listview->setContentY(302);
3069 listview->positionViewAtIndex(15, QQuickListView::Visible);
3070 QTRY_COMPARE(listview->contentY(), 302.);
3072 listview->setContentY(320);
3073 listview->positionViewAtIndex(15, QQuickListView::Visible);
3074 QTRY_COMPARE(listview->contentY(), 300.);
3076 listview->setContentY(85);
3077 listview->positionViewAtIndex(20, QQuickListView::Visible);
3078 QTRY_COMPARE(listview->contentY(), 85.);
3080 listview->setContentY(75);
3081 listview->positionViewAtIndex(20, QQuickListView::Visible);
3082 QTRY_COMPARE(listview->contentY(), 100.);
3084 // Ensure completely visible
3085 listview->setContentY(120);
3086 listview->positionViewAtIndex(20, QQuickListView::Contain);
3087 QTRY_COMPARE(listview->contentY(), 120.);
3089 listview->setContentY(302);
3090 listview->positionViewAtIndex(15, QQuickListView::Contain);
3091 QTRY_COMPARE(listview->contentY(), 300.);
3093 listview->setContentY(85);
3094 listview->positionViewAtIndex(20, QQuickListView::Contain);
3095 QTRY_COMPARE(listview->contentY(), 100.);
3097 // positionAtBeginnging
3098 listview->positionViewAtBeginning();
3099 QTRY_COMPARE(listview->contentY(), 0.);
3101 listview->setContentY(80);
3102 canvas->rootObject()->setProperty("showHeader", true);
3103 listview->positionViewAtBeginning();
3104 QTRY_COMPARE(listview->contentY(), -30.);
3107 listview->positionViewAtEnd();
3108 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
3110 listview->setContentY(80);
3111 canvas->rootObject()->setProperty("showFooter", true);
3112 listview->positionViewAtEnd();
3113 QTRY_COMPARE(listview->contentY(), 510.);
3115 // set current item to outside visible view, position at beginning
3116 // and ensure highlight moves to current item
3117 listview->setCurrentIndex(1);
3118 listview->positionViewAtBeginning();
3119 QTRY_COMPARE(listview->contentY(), -30.);
3120 QVERIFY(listview->highlightItem());
3121 QCOMPARE(listview->highlightItem()->y(), 20.);
3127 void tst_QQuickListView::resetModel()
3129 QQuickView *canvas = createView();
3131 QStringList strings;
3132 strings << "one" << "two" << "three";
3133 QStringListModel model(strings);
3135 QQmlContext *ctxt = canvas->rootContext();
3136 ctxt->setContextProperty("testModel", &model);
3138 canvas->setSource(testFileUrl("displaylist.qml"));
3140 qApp->processEvents();
3142 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3143 QTRY_VERIFY(listview != 0);
3144 QQuickItem *contentItem = listview->contentItem();
3145 QTRY_VERIFY(contentItem != 0);
3146 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3148 QTRY_COMPARE(listview->count(), model.rowCount());
3150 for (int i = 0; i < model.rowCount(); ++i) {
3151 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3152 QTRY_VERIFY(display != 0);
3153 QTRY_COMPARE(display->text(), strings.at(i));
3157 strings << "four" << "five" << "six" << "seven";
3158 model.setStringList(strings);
3160 QTRY_COMPARE(listview->count(), model.rowCount());
3162 for (int i = 0; i < model.rowCount(); ++i) {
3163 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3164 QTRY_VERIFY(display != 0);
3165 QTRY_COMPARE(display->text(), strings.at(i));
3171 void tst_QQuickListView::propertyChanges()
3173 QQuickView *canvas = createView();
3174 QTRY_VERIFY(canvas);
3175 canvas->setSource(testFileUrl("propertychangestest.qml"));
3177 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3178 QTRY_VERIFY(listView);
3180 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3181 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3182 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3183 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3184 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3185 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3186 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3188 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3189 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3190 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3191 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3192 QTRY_COMPARE(listView->isWrapEnabled(), true);
3193 QTRY_COMPARE(listView->cacheBuffer(), 10);
3194 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3196 listView->setHighlightFollowsCurrentItem(false);
3197 listView->setPreferredHighlightBegin(1.0);
3198 listView->setPreferredHighlightEnd(1.0);
3199 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3200 listView->setWrapEnabled(false);
3201 listView->setCacheBuffer(3);
3202 listView->setSnapMode(QQuickListView::SnapOneItem);
3204 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3205 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3206 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3207 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3208 QTRY_COMPARE(listView->isWrapEnabled(), false);
3209 QTRY_COMPARE(listView->cacheBuffer(), 3);
3210 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3212 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3213 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3214 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3215 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3216 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3217 QTRY_COMPARE(cacheBufferSpy.count(),1);
3218 QTRY_COMPARE(snapModeSpy.count(),1);
3220 listView->setHighlightFollowsCurrentItem(false);
3221 listView->setPreferredHighlightBegin(1.0);
3222 listView->setPreferredHighlightEnd(1.0);
3223 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3224 listView->setWrapEnabled(false);
3225 listView->setCacheBuffer(3);
3226 listView->setSnapMode(QQuickListView::SnapOneItem);
3228 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3229 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3230 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3231 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3232 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3233 QTRY_COMPARE(cacheBufferSpy.count(),1);
3234 QTRY_COMPARE(snapModeSpy.count(),1);
3239 void tst_QQuickListView::componentChanges()
3241 QQuickView *canvas = createView();
3242 QTRY_VERIFY(canvas);
3243 canvas->setSource(testFileUrl("propertychangestest.qml"));
3245 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3246 QTRY_VERIFY(listView);
3248 QQmlComponent component(canvas->engine());
3249 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3251 QQmlComponent delegateComponent(canvas->engine());
3252 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3254 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3255 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3256 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3257 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3259 listView->setHighlight(&component);
3260 listView->setHeader(&component);
3261 listView->setFooter(&component);
3262 listView->setDelegate(&delegateComponent);
3264 QTRY_COMPARE(listView->highlight(), &component);
3265 QTRY_COMPARE(listView->header(), &component);
3266 QTRY_COMPARE(listView->footer(), &component);
3267 QTRY_COMPARE(listView->delegate(), &delegateComponent);
3269 QTRY_COMPARE(highlightSpy.count(),1);
3270 QTRY_COMPARE(delegateSpy.count(),1);
3271 QTRY_COMPARE(headerSpy.count(),1);
3272 QTRY_COMPARE(footerSpy.count(),1);
3274 listView->setHighlight(&component);
3275 listView->setHeader(&component);
3276 listView->setFooter(&component);
3277 listView->setDelegate(&delegateComponent);
3279 QTRY_COMPARE(highlightSpy.count(),1);
3280 QTRY_COMPARE(delegateSpy.count(),1);
3281 QTRY_COMPARE(headerSpy.count(),1);
3282 QTRY_COMPARE(footerSpy.count(),1);
3287 void tst_QQuickListView::modelChanges()
3289 QQuickView *canvas = createView();
3290 QTRY_VERIFY(canvas);
3291 canvas->setSource(testFileUrl("propertychangestest.qml"));
3293 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3294 QTRY_VERIFY(listView);
3296 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
3297 QTRY_VERIFY(alternateModel);
3298 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3299 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3301 listView->setModel(modelVariant);
3302 QTRY_COMPARE(listView->model(), modelVariant);
3303 QTRY_COMPARE(modelSpy.count(),1);
3305 listView->setModel(modelVariant);
3306 QTRY_COMPARE(modelSpy.count(),1);
3308 listView->setModel(QVariant());
3309 QTRY_COMPARE(modelSpy.count(),2);
3314 void tst_QQuickListView::QTBUG_9791()
3316 QQuickView *canvas = createView();
3318 canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
3319 qApp->processEvents();
3321 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3322 QTRY_VERIFY(listview != 0);
3324 QQuickItem *contentItem = listview->contentItem();
3325 QTRY_VERIFY(contentItem != 0);
3326 QTRY_VERIFY(listview->delegate() != 0);
3327 QTRY_VERIFY(listview->model() != 0);
3329 QMetaObject::invokeMethod(listview, "fillModel");
3330 qApp->processEvents();
3332 // Confirm items positioned correctly
3333 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3334 QCOMPARE(itemCount, 3);
3336 for (int i = 0; i < itemCount; ++i) {
3337 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3338 if (!item) qWarning() << "Item" << i << "not found";
3340 QTRY_COMPARE(item->x(), i*300.0);
3343 // check that view is positioned correctly
3344 QTRY_COMPARE(listview->contentX(), 590.0);
3349 void tst_QQuickListView::manualHighlight()
3351 QQuickView *canvas = new QQuickView(0);
3352 canvas->setGeometry(0,0,240,320);
3354 QString filename(testFile("manual-highlight.qml"));
3355 canvas->setSource(QUrl::fromLocalFile(filename));
3357 qApp->processEvents();
3359 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3360 QTRY_VERIFY(listview != 0);
3362 QQuickItem *contentItem = listview->contentItem();
3363 QTRY_VERIFY(contentItem != 0);
3365 QTRY_COMPARE(listview->currentIndex(), 0);
3366 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3367 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3369 listview->setCurrentIndex(2);
3371 QTRY_COMPARE(listview->currentIndex(), 2);
3372 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3373 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3376 listview->positionViewAtIndex(3, QQuickListView::Contain);
3378 QTRY_COMPARE(listview->currentIndex(), 2);
3379 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3380 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3385 void tst_QQuickListView::QTBUG_11105()
3387 QQuickView *canvas = createView();
3390 for (int i = 0; i < 30; i++)
3391 model.addItem("Item" + QString::number(i), "");
3393 QQmlContext *ctxt = canvas->rootContext();
3394 ctxt->setContextProperty("testModel", &model);
3396 TestObject *testObject = new TestObject;
3397 ctxt->setContextProperty("testObject", testObject);
3399 canvas->setSource(testFileUrl("listviewtest.qml"));
3401 qApp->processEvents();
3403 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3404 QTRY_VERIFY(listview != 0);
3405 QQuickItem *contentItem = listview->contentItem();
3406 QTRY_VERIFY(contentItem != 0);
3407 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3409 // Confirm items positioned correctly
3410 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3411 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3412 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3413 if (!item) qWarning() << "Item" << i << "not found";
3415 QTRY_VERIFY(item->y() == i*20);
3418 listview->positionViewAtIndex(20, QQuickListView::Beginning);
3419 QCOMPARE(listview->contentY(), 280.);
3421 QmlListModel model2;
3422 for (int i = 0; i < 5; i++)
3423 model2.addItem("Item" + QString::number(i), "");
3425 ctxt->setContextProperty("testModel", &model2);
3427 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3428 QCOMPARE(itemCount, 5);
3434 void tst_QQuickListView::initialZValues()
3436 QQuickView *canvas = createView();
3437 canvas->setSource(testFileUrl("initialZValues.qml"));
3438 qApp->processEvents();
3440 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3441 QTRY_VERIFY(listview != 0);
3442 QQuickItem *contentItem = listview->contentItem();
3443 QTRY_VERIFY(contentItem != 0);
3445 QVERIFY(listview->headerItem());
3446 QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3448 QVERIFY(listview->footerItem());
3449 QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3454 void tst_QQuickListView::header()
3456 QFETCH(QQuickListView::Orientation, orientation);
3457 QFETCH(Qt::LayoutDirection, layoutDirection);
3458 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3459 QFETCH(QPointF, initialHeaderPos);
3460 QFETCH(QPointF, changedHeaderPos);
3461 QFETCH(QPointF, initialContentPos);
3462 QFETCH(QPointF, changedContentPos);
3463 QFETCH(QPointF, firstDelegatePos);
3464 QFETCH(QPointF, resizeContentPos);
3467 for (int i = 0; i < 30; i++)
3468 model.addItem("Item" + QString::number(i), "");
3470 QQuickView *canvas = getView();
3471 canvas->rootContext()->setContextProperty("testModel", &model);
3472 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3473 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3474 canvas->setSource(testFileUrl("header.qml"));
3476 qApp->processEvents();
3478 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3479 QTRY_VERIFY(listview != 0);
3480 listview->setOrientation(orientation);
3481 listview->setLayoutDirection(layoutDirection);
3482 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3483 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3485 QQuickItem *contentItem = listview->contentItem();
3486 QTRY_VERIFY(contentItem != 0);
3488 QQuickText *header = 0;
3489 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3490 QVERIFY(header == listview->headerItem());
3492 QCOMPARE(header->width(), 100.);
3493 QCOMPARE(header->height(), 30.);
3494 QCOMPARE(header->pos(), initialHeaderPos);
3495 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3497 if (orientation == QQuickListView::Vertical)
3498 QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3500 QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3502 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3504 QCOMPARE(item->pos(), firstDelegatePos);
3507 QTRY_COMPARE(listview->count(), model.count());
3508 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3509 if (orientation == QQuickListView::Vertical)
3510 QCOMPARE(listview->contentHeight(), header->height());
3512 QCOMPARE(listview->contentWidth(), header->width());
3514 for (int i = 0; i < 30; i++)
3515 model.addItem("Item" + QString::number(i), "");
3517 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3518 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3520 QCOMPARE(headerItemSpy.count(), 1);
3522 header = findItem<QQuickText>(contentItem, "header");
3524 header = findItem<QQuickText>(contentItem, "header2");
3527 QVERIFY(header == listview->headerItem());
3529 QCOMPARE(header->pos(), changedHeaderPos);
3530 QCOMPARE(header->width(), 50.);
3531 QCOMPARE(header->height(), 20.);
3532 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3534 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3536 QCOMPARE(item->pos(), firstDelegatePos);
3538 listview->positionViewAtBeginning();
3539 header->setHeight(10);
3540 header->setWidth(40);
3541 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3543 releaseView(canvas);
3546 // QTBUG-21207 header should become visible if view resizes from initial empty size
3549 canvas->rootContext()->setContextProperty("testModel", &model);
3550 canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3551 canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3552 canvas->setSource(testFileUrl("header.qml"));
3554 qApp->processEvents();
3556 listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3557 QTRY_VERIFY(listview != 0);
3558 listview->setOrientation(orientation);
3559 listview->setLayoutDirection(layoutDirection);
3560 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3561 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3563 listview->setWidth(240);
3564 listview->setHeight(320);
3565 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3566 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3568 releaseView(canvas);
3571 void tst_QQuickListView::header_data()
3573 QTest::addColumn<QQuickListView::Orientation>("orientation");
3574 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3575 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3576 QTest::addColumn<QPointF>("initialHeaderPos");
3577 QTest::addColumn<QPointF>("changedHeaderPos");
3578 QTest::addColumn<QPointF>("initialContentPos");
3579 QTest::addColumn<QPointF>("changedContentPos");
3580 QTest::addColumn<QPointF>("firstDelegatePos");
3581 QTest::addColumn<QPointF>("resizeContentPos");
3583 // header1 = 100 x 30
3584 // header2 = 50 x 20
3585 // delegates = 240 x 30
3588 // header above items, top left
3589 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3597 // header above items, top right
3598 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3606 // header to left of items
3607 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3615 // header to right of items
3616 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3619 << QPointF(-240 + 100, 0)
3620 << QPointF(-240 + 50, 0)
3622 << QPointF(-240 + 40, 0);
3624 // header below items
3625 QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3628 << QPointF(0, -320 + 30)
3629 << QPointF(0, -320 + 20)
3631 << QPointF(0, -320 + 10);
3634 void tst_QQuickListView::header_delayItemCreation()
3636 QQuickView *canvas = createView();
3640 canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3641 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3642 qApp->processEvents();
3644 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3645 QTRY_VERIFY(listview != 0);
3647 QQuickItem *contentItem = listview->contentItem();
3648 QTRY_VERIFY(contentItem != 0);
3650 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3652 QCOMPARE(header->y(), -header->height());
3654 QCOMPARE(listview->contentY(), -header->height());
3657 QTRY_COMPARE(header->y(), -header->height());
3662 void tst_QQuickListView::footer()
3664 QFETCH(QQuickListView::Orientation, orientation);
3665 QFETCH(Qt::LayoutDirection, layoutDirection);
3666 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3667 QFETCH(QPointF, initialFooterPos);
3668 QFETCH(QPointF, firstDelegatePos);
3669 QFETCH(QPointF, initialContentPos);
3670 QFETCH(QPointF, changedFooterPos);
3671 QFETCH(QPointF, changedContentPos);
3672 QFETCH(QPointF, resizeContentPos);
3674 QQuickView *canvas = getView();
3677 for (int i = 0; i < 3; i++)
3678 model.addItem("Item" + QString::number(i), "");
3680 QQmlContext *ctxt = canvas->rootContext();
3681 ctxt->setContextProperty("testModel", &model);
3683 canvas->setSource(testFileUrl("footer.qml"));
3685 qApp->processEvents();
3687 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3688 QTRY_VERIFY(listview != 0);
3689 listview->setOrientation(orientation);
3690 listview->setLayoutDirection(layoutDirection);
3691 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3692 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3694 QQuickItem *contentItem = listview->contentItem();
3695 QTRY_VERIFY(contentItem != 0);
3697 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3700 QVERIFY(footer == listview->footerItem());
3702 QCOMPARE(footer->pos(), initialFooterPos);
3703 QCOMPARE(footer->width(), 100.);
3704 QCOMPARE(footer->height(), 30.);
3705 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3707 if (orientation == QQuickListView::Vertical)
3708 QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3710 QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3712 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3714 QCOMPARE(item->pos(), firstDelegatePos);
3717 model.removeItem(1);
3719 if (orientation == QQuickListView::Vertical) {
3720 QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3721 initialFooterPos.y() - 20 : initialFooterPos.y() + 20); // delegate width = 40
3723 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3724 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3729 if (orientation == QQuickListView::Vertical)
3730 QTRY_COMPARE(listview->contentHeight(), footer->height());
3732 QTRY_COMPARE(listview->contentWidth(), footer->width());
3734 QPointF posWhenNoItems(0, 0);
3735 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3736 posWhenNoItems.setX(-100);
3737 else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3738 posWhenNoItems.setY(-30);
3739 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3741 // if header is present, it's at a negative pos, so the footer should not move
3742 canvas->rootObject()->setProperty("showHeader", true);
3743 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3744 canvas->rootObject()->setProperty("showHeader", false);
3747 for (int i = 0; i < 30; i++)
3748 model.addItem("Item" + QString::number(i), "");
3750 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3751 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3753 QCOMPARE(footerItemSpy.count(), 1);
3755 footer = findItem<QQuickText>(contentItem, "footer");
3757 footer = findItem<QQuickText>(contentItem, "footer2");
3760 QVERIFY(footer == listview->footerItem());
3762 QCOMPARE(footer->pos(), changedFooterPos);
3763 QCOMPARE(footer->width(), 50.);
3764 QCOMPARE(footer->height(), 20.);
3765 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3767 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3769 QCOMPARE(item->pos(), firstDelegatePos);
3771 listview->positionViewAtEnd();
3772 footer->setHeight(10);
3773 footer->setWidth(40);
3774 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3776 releaseView(canvas);
3779 void tst_QQuickListView::footer_data()
3781 QTest::addColumn<QQuickListView::Orientation>("orientation");
3782 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3783 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3784 QTest::addColumn<QPointF>("initialFooterPos");
3785 QTest::addColumn<QPointF>("changedFooterPos");
3786 QTest::addColumn<QPointF>("initialContentPos");
3787 QTest::addColumn<QPointF>("changedContentPos");
3788 QTest::addColumn<QPointF>("firstDelegatePos");
3789 QTest::addColumn<QPointF>("resizeContentPos");
3791 // footer1 = 100 x 30
3792 // footer2 = 50 x 20
3793 // delegates = 40 x 20
3795 // view height = 320
3797 // footer below items, bottom left
3798 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3799 << QPointF(0, 3 * 20)
3800 << QPointF(0, 30 * 20) // added 30 items
3804 << QPointF(0, 30 * 20 - 320 + 10);
3806 // footer below items, bottom right
3807 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3808 << QPointF(0, 3 * 20)
3809 << QPointF(0, 30 * 20)
3813 << QPointF(0, 30 * 20 - 320 + 10);
3815 // footer to right of items
3816 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3817 << QPointF(40 * 3, 0)
3818 << QPointF(40 * 30, 0)
3822 << QPointF(40 * 30 - 240 + 40, 0);
3824 // footer to left of items
3825 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3826 << QPointF(-(40 * 3) - 100, 0)
3827 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3831 << QPointF(-(40 * 30) - 40, 0);
3833 // footer above items
3834 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3835 << QPointF(0, -(3 * 20) - 30)
3836 << QPointF(0, -(30 * 20) - 20)
3840 << QPointF(0, -(30 * 20) - 10);
3843 class LVAccessor : public QQuickListView
3846 qreal minY() const { return minYExtent(); }
3847 qreal maxY() const { return maxYExtent(); }
3848 qreal minX() const { return minXExtent(); }
3849 qreal maxX() const { return maxXExtent(); }
3853 void tst_QQuickListView::extents()
3855 QFETCH(QQuickListView::Orientation, orientation);
3856 QFETCH(Qt::LayoutDirection, layoutDirection);
3857 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3858 QFETCH(QPointF, headerPos);
3859 QFETCH(QPointF, footerPos);
3860 QFETCH(QPointF, minPos);
3861 QFETCH(QPointF, maxPos);
3862 QFETCH(QPointF, origin_empty);
3863 QFETCH(QPointF, origin_nonEmpty);
3865 QQuickView *canvas = getView();
3868 QQmlContext *ctxt = canvas->rootContext();
3869 ctxt->setContextProperty("testModel", &model);
3870 canvas->setSource(testFileUrl("headerfooter.qml"));
3872 qApp->processEvents();
3874 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3875 QTRY_VERIFY(listview != 0);
3876 listview->setOrientation(orientation);
3877 listview->setLayoutDirection(layoutDirection);
3878 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3879 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3881 QQuickItem *contentItem = listview->contentItem();
3882 QTRY_VERIFY(contentItem != 0);
3884 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3886 QCOMPARE(header->pos(), headerPos);
3888 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3890 QCOMPARE(footer->pos(), footerPos);
3892 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3893 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3894 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3895 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3897 QCOMPARE(listview->originX(), origin_empty.x());
3898 QCOMPARE(listview->originY(), origin_empty.y());
3899 for (int i=0; i<30; i++)
3900 model.addItem("Item" + QString::number(i), "");
3901 QTRY_COMPARE(listview->count(), model.count());
3902 QCOMPARE(listview->originX(), origin_nonEmpty.x());
3903 QCOMPARE(listview->originY(), origin_nonEmpty.y());
3905 releaseView(canvas);
3908 void tst_QQuickListView::extents_data()
3910 QTest::addColumn<QQuickListView::Orientation>("orientation");
3911 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3912 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3913 QTest::addColumn<QPointF>("headerPos");
3914 QTest::addColumn<QPointF>("footerPos");
3915 QTest::addColumn<QPointF>("minPos");
3916 QTest::addColumn<QPointF>("maxPos");
3917 QTest::addColumn<QPointF>("origin_empty");
3918 QTest::addColumn<QPointF>("origin_nonEmpty");
3920 // header is 240x20 (or 20x320 in Horizontal orientation)
3921 // footer is 240x30 (or 30x320 in Horizontal orientation)
3923 QTest::newRow("Vertical, TopToBottom")
3924 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3925 << QPointF(0, -20) << QPointF(0, 0)
3926 << QPointF(0, 20) << QPointF(240, 20)
3927 << QPointF(0, -20) << QPointF(0, -20);
3929 QTest::newRow("Vertical, BottomToTop")
3930 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3931 << QPointF(0, 0) << QPointF(0, -30)
3932 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3933 << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3935 QTest::newRow("Horizontal, LeftToRight")
3936 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3937 << QPointF(-20, 0) << QPointF(0, 0)
3938 << QPointF(20, 0) << QPointF(20, 320)
3939 << QPointF(-20, 0) << QPointF(-20, 0);
3941 QTest::newRow("Horizontal, RightToLeft")
3942 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3943 << QPointF(0, 0) << QPointF(-30, 0)
3944 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3945 << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3948 void tst_QQuickListView::resetModel_headerFooter()
3950 // Resetting a model shouldn't crash in views with header/footer
3952 QQuickView *canvas = createView();
3955 for (int i = 0; i < 4; i++)
3956 model.addItem("Item" + QString::number(i), "");
3957 QQmlContext *ctxt = canvas->rootContext();
3958 ctxt->setContextProperty("testModel", &model);
3960 canvas->setSource(testFileUrl("headerfooter.qml"));
3961 qApp->processEvents();
3963 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3964 QTRY_VERIFY(listview != 0);
3966 QQuickItem *contentItem = listview->contentItem();
3967 QTRY_VERIFY(contentItem != 0);
3969 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3971 QCOMPARE(header->y(), -header->height());
3973 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3975 QCOMPARE(footer->y(), 30.*4);
3979 header = findItem<QQuickItem>(contentItem, "header");
3981 QCOMPARE(header->y(), -header->height());
3983 footer = findItem<QQuickItem>(contentItem, "footer");
3985 QCOMPARE(footer->y(), 30.*4);
3990 void tst_QQuickListView::resizeView()
3992 QQuickView *canvas = createView();
3995 for (int i = 0; i < 40; i++)
3996 model.addItem("Item" + QString::number(i), "");
3998 QQmlContext *ctxt = canvas->rootContext();
3999 ctxt->setContextProperty("testModel", &model);
4001 TestObject *testObject = new TestObject;
4002 ctxt->setContextProperty("testObject", testObject);
4004 canvas->setSource(testFileUrl("listviewtest.qml"));
4006 qApp->processEvents();
4008 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4009 QTRY_VERIFY(listview != 0);
4010 QQuickItem *contentItem = listview->contentItem();
4011 QTRY_VERIFY(contentItem != 0);
4012 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4014 // Confirm items positioned correctly
4015 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4016 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4017 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4018 if (!item) qWarning() << "Item" << i << "not found";
4020 QTRY_COMPARE(item->y(), i*20.);
4023 QVariant heightRatio;
4024 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4025 QCOMPARE(heightRatio.toReal(), 0.4);
4027 listview->setHeight(200);
4028 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4030 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4031 QCOMPARE(heightRatio.toReal(), 0.25);
4033 // Ensure we handle -ve sizes
4034 listview->setHeight(-100);
4035 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
4037 listview->setCacheBuffer(200);
4038 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
4040 // ensure items in cache become visible
4041 listview->setHeight(200);
4042 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
4044 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4045 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4046 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4047 if (!item) qWarning() << "Item" << i << "not found";
4049 QTRY_COMPARE(item->y(), i*20.);
4050 QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
4053 // ensure items outside view become invisible
4054 listview->setHeight(100);
4055 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
4057 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4058 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4059 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4060 if (!item) qWarning() << "Item" << i << "not found";
4062 QTRY_COMPARE(item->y(), i*20.);
4063 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
4070 void tst_QQuickListView::resizeViewAndRepaint()
4072 QQuickView *canvas = createView();
4075 for (int i = 0; i < 40; i++)
4076 model.addItem("Item" + QString::number(i), "");
4078 QQmlContext *ctxt = canvas->rootContext();
4079 ctxt->setContextProperty("testModel", &model);
4080 ctxt->setContextProperty("initialHeight", 100);
4082 canvas->setSource(testFileUrl("resizeview.qml"));
4084 qApp->processEvents();
4086 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4087 QTRY_VERIFY(listview != 0);
4088 QQuickItem *contentItem = listview->contentItem();
4089 QTRY_VERIFY(contentItem != 0);
4090 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4092 // item at index 10 should not be currently visible
4093 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4095 listview->setHeight(320);
4097 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
4099 listview->setHeight(100);
4100 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4105 void tst_QQuickListView::sizeLessThan1()
4107 QQuickView *canvas = createView();
4110 for (int i = 0; i < 30; i++)
4111 model.addItem("Item" + QString::number(i), "");
4113 QQmlContext *ctxt = canvas->rootContext();
4114 ctxt->setContextProperty("testModel", &model);
4116 TestObject *testObject = new TestObject;
4117 ctxt->setContextProperty("testObject", testObject);
4119 canvas->setSource(testFileUrl("sizelessthan1.qml"));
4121 qApp->processEvents();
4123 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4124 QTRY_VERIFY(listview != 0);
4125 QQuickItem *contentItem = listview->contentItem();
4126 QTRY_VERIFY(contentItem != 0);
4127 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4129 // Confirm items positioned correctly
4130 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4131 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4132 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4133 if (!item) qWarning() << "Item" << i << "not found";
4135 QTRY_COMPARE(item->y(), i*0.5);
4142 void tst_QQuickListView::QTBUG_14821()
4144 QQuickView *canvas = createView();
4146 canvas->setSource(testFileUrl("qtbug14821.qml"));
4147 qApp->processEvents();
4149 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4150 QVERIFY(listview != 0);
4152 QQuickItem *contentItem = listview->contentItem();
4153 QVERIFY(contentItem != 0);
4155 listview->decrementCurrentIndex();
4156 QCOMPARE(listview->currentIndex(), 99);
4158 listview->incrementCurrentIndex();
4159 QCOMPARE(listview->currentIndex(), 0);
4164 void tst_QQuickListView::resizeDelegate()
4166 QQuickView *canvas = createView();
4168 QStringList strings;
4169 for (int i = 0; i < 30; ++i)
4170 strings << QString::number(i);
4171 QStringListModel model(strings);
4173 QQmlContext *ctxt = canvas->rootContext();
4174 ctxt->setContextProperty("testModel", &model);
4176 canvas->setSource(testFileUrl("displaylist.qml"));
4178 qApp->processEvents();
4180 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4181 QVERIFY(listview != 0);
4182 QQuickItem *contentItem = listview->contentItem();
4183 QVERIFY(contentItem != 0);
4184 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4186 QCOMPARE(listview->count(), model.rowCount());
4188 listview->setCurrentIndex(25);
4189 listview->setContentY(0);
4190 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4192 for (int i = 0; i < 16; ++i) {
4193 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4195 QCOMPARE(item->y(), i*20.0);
4198 QCOMPARE(listview->currentItem()->y(), 500.0);
4199 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4201 canvas->rootObject()->setProperty("delegateHeight", 30);
4202 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4204 for (int i = 0; i < 11; ++i) {
4205 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4207 QTRY_COMPARE(item->y(), i*30.0);
4210 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4211 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4213 listview->setCurrentIndex(1);
4214 listview->positionViewAtIndex(25, QQuickListView::Beginning);
4215 listview->positionViewAtIndex(5, QQuickListView::Beginning);
4216 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4218 for (int i = 5; i < 16; ++i) {
4219 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4221 QCOMPARE(item->y(), i*30.0);
4224 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4225 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4227 canvas->rootObject()->setProperty("delegateHeight", 20);
4228 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4230 for (int i = 5; i < 11; ++i) {
4231 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4233 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4236 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4237 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4242 void tst_QQuickListView::resizeFirstDelegate()
4244 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4245 // and other delegates have height > 0
4247 QQuickView *canvas = createView();
4249 // bug only occurs when all items in the model are visible
4251 for (int i = 0; i < 10; i++)
4252 model.addItem("Item" + QString::number(i), "");
4254 QQmlContext *ctxt = canvas->rootContext();
4255 ctxt->setContextProperty("testModel", &model);
4257 TestObject *testObject = new TestObject;
4258 ctxt->setContextProperty("testObject", testObject);
4260 canvas->setSource(testFileUrl("listviewtest.qml"));
4262 qApp->processEvents();
4264 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4265 QVERIFY(listview != 0);
4266 QQuickItem *contentItem = listview->contentItem();
4267 QVERIFY(contentItem != 0);
4268 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4270 QQuickItem *item = 0;
4271 for (int i = 0; i < model.count(); ++i) {
4272 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4274 QCOMPARE(item->y(), i*20.0);
4277 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4280 // check the content y has not jumped up and down
4281 QCOMPARE(listview->contentY(), 0.0);
4282 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4284 QCOMPARE(spy.count(), 0);
4286 for (int i = 1; i < model.count(); ++i) {
4287 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4289 QTRY_COMPARE(item->y(), (i-1)*20.0);
4293 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4294 // list if they follow a zero-sized delegate
4296 for (int i = 0; i < 10; i++)
4297 model.addItem("Item" + QString::number(i), "");
4298 QTRY_COMPARE(listview->count(), model.count());
4300 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4304 listview->setCurrentIndex(19);
4305 qApp->processEvents();
4306 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4308 // items 0-2 should have been deleted
4309 for (int i=0; i<3; i++) {
4310 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4317 void tst_QQuickListView::repositionResizedDelegate()
4319 QFETCH(QQuickListView::Orientation, orientation);
4320 QFETCH(Qt::LayoutDirection, layoutDirection);
4321 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4322 QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4323 QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4324 QFETCH(QRectF, origPositionerRect);
4325 QFETCH(QRectF, resizedPositionerRect);
4327 QQuickView *canvas = getView();
4328 QQmlContext *ctxt = canvas->rootContext();
4329 ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4330 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4331 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4332 canvas->setSource(testFileUrl("repositionResizedDelegate.qml"));
4334 qApp->processEvents();
4336 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4337 QTRY_VERIFY(listview != 0);
4338 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4340 QQuickItem *positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4341 QVERIFY(positioner);
4342 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4343 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4344 QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4345 int prevSpyCount = 0;
4347 // When an item is resized while it is partially visible, it should resize in the
4348 // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4349 // the item should also be re-positioned so its end position stays the same.
4351 listview->setContentX(contentPos_itemFirstHalfVisible.x());
4352 listview->setContentY(contentPos_itemFirstHalfVisible.y());
4353 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4354 prevSpyCount = spy.count();
4355 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4356 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4357 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4358 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4359 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4360 QCOMPARE(spy.count(), prevSpyCount);
4362 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "decrementRepeater"));
4363 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4364 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4365 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4366 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4368 listview->setContentX(contentPos_itemSecondHalfVisible.x());
4369 listview->setContentY(contentPos_itemSecondHalfVisible.y());
4370 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4371 prevSpyCount = spy.count();
4373 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4374 positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4375 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4376 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4377 QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4378 QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4379 qApp->processEvents();
4380 QCOMPARE(spy.count(), prevSpyCount);
4382 releaseView(canvas);
4385 void tst_QQuickListView::repositionResizedDelegate_data()
4387 QTest::addColumn<QQuickListView::Orientation>("orientation");
4388 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4389 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4390 QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4391 QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4392 QTest::addColumn<QRectF>("origPositionerRect");
4393 QTest::addColumn<QRectF>("resizedPositionerRect");
4395 QTest::newRow("vertical")
4396 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4397 << QPointF(0, 60) << QPointF(0, 200 + 60)
4398 << QRectF(0, 200, 120, 120)
4399 << QRectF(0, 200, 120, 120 * 2);
4401 QTest::newRow("vertical, BottomToTop")
4402 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4403 << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4404 << QRectF(0, -200 - 120, 120, 120)
4405 << QRectF(0, -200 - 120*2, 120, 120 * 2);
4407 QTest::newRow("horizontal")
4408 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4409 << QPointF(60, 0) << QPointF(260, 0)
4410 << QRectF(200, 0, 120, 120)
4411 << QRectF(200, 0, 120 * 2, 120);
4413 QTest::newRow("horizontal, rtl")
4414 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4415 << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4416 << QRectF(-200 - 120, 0, 120, 120)
4417 << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4420 void tst_QQuickListView::QTBUG_16037()
4422 QQuickView *canvas = createView();
4425 canvas->setSource(testFileUrl("qtbug16037.qml"));
4426 qApp->processEvents();
4428 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4429 QTRY_VERIFY(listview != 0);
4431 QVERIFY(listview->contentHeight() <= 0.0);
4433 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
4435 QTRY_COMPARE(listview->contentHeight(), 80.0);
4440 void tst_QQuickListView::indexAt_itemAt_data()
4442 QTest::addColumn<qreal>("x");
4443 QTest::addColumn<qreal>("y");
4444 QTest::addColumn<int>("index");
4446 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4447 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4448 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4449 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4450 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4453 void tst_QQuickListView::indexAt_itemAt()
4459 QQuickView *canvas = getView();
4462 for (int i = 0; i < 30; i++)
4463 model.addItem("Item" + QString::number(i), "");
4465 QQmlContext *ctxt = canvas->rootContext();
4466 ctxt->setContextProperty("testModel", &model);
4468 TestObject *testObject = new TestObject;
4469 ctxt->setContextProperty("testObject", testObject);
4471 canvas->setSource(testFileUrl("listviewtest.qml"));
4473 qApp->processEvents();
4475 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4476 QTRY_VERIFY(listview != 0);
4478 QQuickItem *contentItem = listview->contentItem();
4479 QTRY_VERIFY(contentItem != 0);
4480 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4482 QQuickItem *item = 0;
4484 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4487 QCOMPARE(listview->indexAt(x,y), index);
4488 QVERIFY(listview->itemAt(x,y) == item);
4490 releaseView(canvas);
4494 void tst_QQuickListView::incrementalModel()
4496 QQuickView *canvas = createView();
4498 IncrementalModel model;
4499 QQmlContext *ctxt = canvas->rootContext();
4500 ctxt->setContextProperty("testModel", &model);
4502 canvas->setSource(testFileUrl("displaylist.qml"));
4503 qApp->processEvents();
4505 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4506 QTRY_VERIFY(listview != 0);
4508 QQuickItem *contentItem = listview->contentItem();
4509 QTRY_VERIFY(contentItem != 0);
4511 QTRY_COMPARE(listview->count(), 20);
4513 listview->positionViewAtIndex(10, QQuickListView::Beginning);
4515 QTRY_COMPARE(listview->count(), 25);
4520 void tst_QQuickListView::onAdd()
4522 QFETCH(int, initialItemCount);
4523 QFETCH(int, itemsToAdd);
4525 const int delegateHeight = 10;
4528 // these initial items should not trigger ListView.onAdd
4529 for (int i=0; i<initialItemCount; i++)
4530 model.addItem("dummy value", "dummy value");
4532 QQuickView *canvas = createView();
4533 canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4534 QQmlContext *ctxt = canvas->rootContext();
4535 ctxt->setContextProperty("testModel", &model);
4536 ctxt->setContextProperty("delegateHeight", delegateHeight);
4537 canvas->setSource(testFileUrl("attachedSignals.qml"));
4539 QObject *object = canvas->rootObject();
4540 object->setProperty("width", canvas->width());
4541 object->setProperty("height", canvas->height());
4542 qApp->processEvents();
4544 QList<QPair<QString, QString> > items;
4545 for (int i=0; i<itemsToAdd; i++)
4546 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4547 model.addItems(items);
4548 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4550 QVariantList result = object->property("addedDelegates").toList();
4551 QCOMPARE(result.count(), items.count());
4552 for (int i=0; i<items.count(); i++)
4553 QCOMPARE(result[i].toString(), items[i].first);
4558 void tst_QQuickListView::onAdd_data()
4560 QTest::addColumn<int>("initialItemCount");
4561 QTest::addColumn<int>("itemsToAdd");
4563 QTest::newRow("0, add 1") << 0 << 1;
4564 QTest::newRow("0, add 2") << 0 << 2;
4565 QTest::newRow("0, add 10") << 0 << 10;
4567 QTest::newRow("1, add 1") << 1 << 1;
4568 QTest::newRow("1, add 2") << 1 << 2;
4569 QTest::newRow("1, add 10") << 1 << 10;
4571 QTest::newRow("5, add 1") << 5 << 1;
4572 QTest::newRow("5, add 2") << 5 << 2;
4573 QTest::newRow("5, add 10") << 5 << 10;
4576 void tst_QQuickListView::onRemove()
4578 QFETCH(int, initialItemCount);
4579 QFETCH(int, indexToRemove);
4580 QFETCH(int, removeCount);
4582 const int delegateHeight = 10;
4584 for (int i=0; i<initialItemCount; i++)
4585 model.addItem(QString("value %1").arg(i), "dummy value");
4587 QQuickView *canvas = getView();
4588 QQmlContext *ctxt = canvas->rootContext();
4589 ctxt->setContextProperty("testModel", &model);
4590 ctxt->setContextProperty("delegateHeight", delegateHeight);
4591 canvas->setSource(testFileUrl("attachedSignals.qml"));
4593 QObject *object = canvas->rootObject();
4595 model.removeItems(indexToRemove, removeCount);
4596 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4598 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4600 releaseView(canvas);
4603 void tst_QQuickListView::onRemove_data()
4605 QTest::addColumn<int>("initialItemCount");
4606 QTest::addColumn<int>("indexToRemove");
4607 QTest::addColumn<int>("removeCount");
4609 QTest::newRow("remove first") << 1 << 0 << 1;
4610 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4611 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4612 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4614 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4615 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4616 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4617 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4618 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4619 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4621 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4622 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4623 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4626 void tst_QQuickListView::rightToLeft()
4628 QQuickView *canvas = createView();
4629 canvas->setGeometry(0,0,640,320);
4630 canvas->setSource(testFileUrl("rightToLeft.qml"));
4632 qApp->processEvents();
4634 QVERIFY(canvas->rootObject() != 0);
4635 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4636 QTRY_VERIFY(listview != 0);
4638 QQuickItem *contentItem = listview->contentItem();
4639 QTRY_VERIFY(contentItem != 0);
4641 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4643 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4644 QTRY_VERIFY(model != 0);
4646 QTRY_VERIFY(model->count() == 3);
4647 QTRY_COMPARE(listview->currentIndex(), 0);
4649 // initial position at first item, right edge aligned
4650 QCOMPARE(listview->contentX(), -640.);
4652 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4654 QTRY_COMPARE(item->x(), -100.0);
4655 QCOMPARE(item->height(), listview->height());
4657 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4659 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4661 listview->setCurrentIndex(2);
4663 item = findItem<QQuickItem>(contentItem, "item3");
4665 QTRY_COMPARE(item->x(), -540.0);
4667 text = findItem<QQuickText>(contentItem, "text3");
4669 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4671 QCOMPARE(listview->contentX(), -640.);
4673 // Ensure resizing maintains position relative to right edge
4674 qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4675 QTRY_COMPARE(listview->contentX(), -600.);
4680 void tst_QQuickListView::test_mirroring()
4682 QQuickView *canvasA = createView();
4683 canvasA->setSource(testFileUrl("rightToLeft.qml"));
4684 QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4685 QTRY_VERIFY(listviewA != 0);
4687 QQuickView *canvasB = createView();
4688 canvasB->setSource(testFileUrl("rightToLeft.qml"));
4689 QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4690 QTRY_VERIFY(listviewA != 0);
4691 qApp->processEvents();
4693 QList<QString> objectNames;
4694 objectNames << "item1" << "item2"; // << "item3"
4696 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4697 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4698 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4701 foreach (const QString objectName, objectNames)
4702 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4704 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4705 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4708 foreach (const QString objectName, objectNames)
4709 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4711 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4712 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4713 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4715 // LTR != LTR+mirror
4716 foreach (const QString objectName, objectNames)
4717 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4719 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4721 // RTL == LTR+mirror
4722 foreach (const QString objectName, objectNames)
4723 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4725 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4727 // RTL != RTL+mirror
4728 foreach (const QString objectName, objectNames)
4729 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4731 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4733 // LTR == RTL+mirror
4734 foreach (const QString objectName, objectNames)
4735 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4741 void tst_QQuickListView::margins()
4743 QQuickView *canvas = createView();
4746 for (int i = 0; i < 50; i++)
4747 model.addItem("Item" + QString::number(i), "");
4749 QQmlContext *ctxt = canvas->rootContext();
4750 ctxt->setContextProperty("testModel", &model);
4752 canvas->setSource(testFileUrl("margins.qml"));
4754 qApp->processEvents();
4756 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4757 QTRY_VERIFY(listview != 0);
4758 QQuickItem *contentItem = listview->contentItem();
4759 QTRY_VERIFY(contentItem != 0);
4760 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4762 QCOMPARE(listview->contentY(), -30.);
4763 QCOMPARE(listview->originY(), 0.);
4766 listview->positionViewAtEnd();
4767 qreal pos = listview->contentY();
4768 listview->setContentY(pos + 80);
4769 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4770 listview->returnToBounds();
4771 QTRY_COMPARE(listview->contentY(), pos + 50);
4773 // remove item before visible and check that top margin is maintained
4774 // and originY is updated
4775 listview->setContentY(100);
4776 model.removeItem(1);
4777 QTRY_COMPARE(listview->count(), model.count());
4778 listview->setContentY(-50);
4779 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4780 listview->returnToBounds();
4781 QCOMPARE(listview->originY(), 20.);
4782 QTRY_COMPARE(listview->contentY(), -10.);
4784 // reduce top margin
4785 listview->setTopMargin(20);
4786 QCOMPARE(listview->originY(), 20.);
4787 QTRY_COMPARE(listview->contentY(), 0.);
4790 listview->positionViewAtEnd();
4791 pos = listview->contentY();
4792 listview->setContentY(pos + 80);
4793 listview->returnToBounds();
4794 QTRY_COMPARE(listview->contentY(), pos + 50);
4796 // reduce bottom margin
4797 pos = listview->contentY();
4798 listview->setBottomMargin(40);
4799 QCOMPARE(listview->originY(), 20.);
4800 QTRY_COMPARE(listview->contentY(), pos-10);
4806 void tst_QQuickListView::marginsResize()
4808 QFETCH(QQuickListView::Orientation, orientation);
4809 QFETCH(Qt::LayoutDirection, layoutDirection);
4810 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4811 QFETCH(qreal, start);
4814 QPoint flickStart(20, 20);
4815 QPoint flickEnd(20, 20);
4816 if (orientation == QQuickListView::Vertical)
4817 flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4819 flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4821 QQuickView *canvas = getView();
4823 canvas->setSource(testFileUrl("margins2.qml"));
4825 qApp->processEvents();
4827 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4828 QTRY_VERIFY(listview != 0);
4830 listview->setOrientation(orientation);
4831 listview->setLayoutDirection(layoutDirection);
4832 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4833 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4835 // view is resized after componentCompleted - top margin should still be visible
4836 if (orientation == QQuickListView::Vertical)
4837 QCOMPARE(listview->contentY(), start);
4839 QCOMPARE(listview->contentX(), start);
4841 // move to last index and ensure bottom margin is visible.
4842 listview->setCurrentIndex(19);
4843 if (orientation == QQuickListView::Vertical)
4844 QTRY_COMPARE(listview->contentY(), end);
4846 QTRY_COMPARE(listview->contentX(), end);
4848 // flick past the end and check content pos still settles on correct extents
4849 flick(canvas, flickStart, flickEnd, 180);
4850 QTRY_VERIFY(listview->isMoving() == false);
4851 if (orientation == QQuickListView::Vertical)
4852 QTRY_COMPARE(listview->contentY(), end);
4854 QTRY_COMPARE(listview->contentX(), end);
4856 // back to top - top margin should be visible.
4857 listview->setCurrentIndex(0);
4858 if (orientation == QQuickListView::Vertical)
4859 QTRY_COMPARE(listview->contentY(), start);
4861 QTRY_COMPARE(listview->contentX(), start);
4863 // flick past the beginning and check content pos still settles on correct extents
4864 flick(canvas, flickEnd, flickStart, 180);
4865 QTRY_VERIFY(listview->isMoving() == false);
4866 if (orientation == QQuickListView::Vertical)
4867 QTRY_COMPARE(listview->contentY(), start);
4869 QTRY_COMPARE(listview->contentX(), start);
4871 releaseView(canvas);
4874 void tst_QQuickListView::marginsResize_data()
4876 QTest::addColumn<QQuickListView::Orientation>("orientation");
4877 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4878 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4879 QTest::addColumn<qreal>("start");
4880 QTest::addColumn<qreal>("end");
4882 // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4884 QTest::newRow("vertical")
4885 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4888 QTest::newRow("vertical, BottomToTop")
4889 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4890 << -180.0 << -1240.0;
4892 QTest::newRow("horizontal")
4893 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4896 QTest::newRow("horizontal, rtl")
4897 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4898 << -180.0 << -1240.0;
4901 void tst_QQuickListView::snapToItem_data()
4903 QTest::addColumn<QQuickListView::Orientation>("orientation");
4904 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4905 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4906 QTest::addColumn<int>("highlightRangeMode");
4907 QTest::addColumn<QPoint>("flickStart");
4908 QTest::addColumn<QPoint>("flickEnd");
4909 QTest::addColumn<qreal>("snapAlignment");
4910 QTest::addColumn<qreal>("endExtent");
4911 QTest::addColumn<qreal>("startExtent");
4913 QTest::newRow("vertical, top to bottom")
4914 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4915 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4917 QTest::newRow("vertical, bottom to top")
4918 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4919 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4921 QTest::newRow("horizontal, left to right")
4922 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4923 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4925 QTest::newRow("horizontal, right to left")
4926 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4927 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4929 QTest::newRow("vertical, top to bottom, enforce range")
4930 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4931 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4933 QTest::newRow("vertical, bottom to top, enforce range")
4934 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4935 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4937 QTest::newRow("horizontal, left to right, enforce range")
4938 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4939 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4941 QTest::newRow("horizontal, right to left, enforce range")
4942 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4943 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4946 void tst_QQuickListView::snapToItem()
4948 QFETCH(QQuickListView::Orientation, orientation);
4949 QFETCH(Qt::LayoutDirection, layoutDirection);
4950 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4951 QFETCH(int, highlightRangeMode);
4952 QFETCH(QPoint, flickStart);
4953 QFETCH(QPoint, flickEnd);
4954 QFETCH(qreal, snapAlignment);
4955 QFETCH(qreal, endExtent);
4956 QFETCH(qreal, startExtent);
4958 QQuickView *canvas = getView();
4960 canvas->setSource(testFileUrl("snapToItem.qml"));
4962 qApp->processEvents();
4964 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4965 QTRY_VERIFY(listview != 0);
4967 listview->setOrientation(orientation);
4968 listview->setLayoutDirection(layoutDirection);
4969 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4970 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4971 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4973 QQuickItem *contentItem = listview->contentItem();
4974 QTRY_VERIFY(contentItem != 0);
4976 // confirm that a flick hits an item boundary
4977 flick(canvas, flickStart, flickEnd, 180);
4978 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4979 if (orientation == QQuickListView::Vertical)
4980 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4982 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4986 flick(canvas, flickStart, flickEnd, 180);
4987 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4988 } while (orientation == QQuickListView::Vertical
4989 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4990 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4992 if (orientation == QQuickListView::Vertical)
4993 QCOMPARE(listview->contentY(), endExtent);
4995 QCOMPARE(listview->contentX(), endExtent);
4999 flick(canvas, flickEnd, flickStart, 180);
5000 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5001 } while (orientation == QQuickListView::Vertical
5002 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5003 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5005 if (orientation == QQuickListView::Vertical)
5006 QCOMPARE(listview->contentY(), startExtent);
5008 QCOMPARE(listview->contentX(), startExtent);
5010 releaseView(canvas);
5013 void tst_QQuickListView::qListModelInterface_items()
5015 items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5018 void tst_QQuickListView::qListModelInterface_package_items()
5020 items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5023 void tst_QQuickListView::qAbstractItemModel_items()
5025 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
5028 void tst_QQuickListView::qListModelInterface_changed()
5030 changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5033 void tst_QQuickListView::qListModelInterface_package_changed()
5035 changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5038 void tst_QQuickListView::qAbstractItemModel_changed()
5040 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5043 void tst_QQuickListView::qListModelInterface_inserted()
5045 inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
5048 void tst_QQuickListView::qListModelInterface_package_inserted()
5050 inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5053 void tst_QQuickListView::qListModelInterface_inserted_more()
5055 inserted_more<QmlListModel>();
5058 void tst_QQuickListView::qListModelInterface_inserted_more_data()
5060 inserted_more_data();
5063 void tst_QQuickListView::qAbstractItemModel_inserted()
5065 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
5068 void tst_QQuickListView::qAbstractItemModel_inserted_more()
5070 inserted_more<QaimModel>();
5073 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
5075 inserted_more_data();
5078 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
5080 inserted_more<QaimModel>(QQuickItemView::BottomToTop);
5083 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
5085 inserted_more_data();
5088 void tst_QQuickListView::qListModelInterface_removed()
5090 removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5091 removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
5094 void tst_QQuickListView::qListModelInterface_removed_more()
5096 removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
5099 void tst_QQuickListView::qListModelInterface_removed_more_data()
5101 removed_more_data();
5104 void tst_QQuickListView::qListModelInterface_package_removed()
5106 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
5107 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5110 void tst_QQuickListView::qAbstractItemModel_removed()
5112 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5113 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5116 void tst_QQuickListView::qAbstractItemModel_removed_more()
5118 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5121 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5123 removed_more_data();
5126 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5128 removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5131 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5133 removed_more_data();
5136 void tst_QQuickListView::qListModelInterface_moved()
5138 moved<QmlListModel>(testFileUrl("listviewtest.qml"));
5141 void tst_QQuickListView::qListModelInterface_moved_data()
5146 void tst_QQuickListView::qListModelInterface_package_moved()
5148 moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5151 void tst_QQuickListView::qListModelInterface_package_moved_data()
5156 void tst_QQuickListView::qAbstractItemModel_moved()
5158 moved<QaimModel>(testFileUrl("listviewtest.qml"));
5161 void tst_QQuickListView::qAbstractItemModel_moved_data()
5166 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5168 moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5171 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5176 void tst_QQuickListView::qListModelInterface_clear()
5178 clear<QmlListModel>(testFileUrl("listviewtest.qml"));
5181 void tst_QQuickListView::qListModelInterface_package_clear()
5183 clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5186 void tst_QQuickListView::qAbstractItemModel_clear()
5188 clear<QaimModel>(testFileUrl("listviewtest.qml"));
5191 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5193 clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5196 void tst_QQuickListView::qListModelInterface_sections()
5198 sections<QmlListModel>(testFileUrl("listview-sections.qml"));
5201 void tst_QQuickListView::qListModelInterface_package_sections()
5203 sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
5206 void tst_QQuickListView::qAbstractItemModel_sections()
5208 sections<QaimModel>(testFileUrl("listview-sections.qml"));
5211 void tst_QQuickListView::creationContext()
5214 canvas.setGeometry(0,0,240,320);
5215 canvas.setSource(testFileUrl("creationContext.qml"));
5216 qApp->processEvents();
5218 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5220 QVERIFY(rootItem->property("count").toInt() > 0);
5223 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5224 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5225 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5226 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5227 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5228 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5229 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5230 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5233 void tst_QQuickListView::QTBUG_21742()
5236 canvas.setGeometry(0,0,200,200);
5237 canvas.setSource(testFileUrl("qtbug-21742.qml"));
5238 qApp->processEvents();
5240 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5242 QCOMPARE(rootItem->property("count").toInt(), 1);
5245 void tst_QQuickListView::asynchronous()
5247 QQuickView *canvas = createView();
5249 QQmlIncubationController controller;
5250 canvas->engine()->setIncubationController(&controller);
5252 canvas->setSource(testFileUrl("asyncloader.qml"));
5254 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5255 QVERIFY(rootObject);
5257 QQuickListView *listview = 0;
5260 controller.incubateWhile(&b);
5261 listview = rootObject->findChild<QQuickListView*>("view");
5264 // items will be created one at a time
5265 for (int i = 0; i < 8; ++i) {
5266 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5267 QQuickItem *item = 0;
5270 controller.incubateWhile(&b);
5271 item = findItem<QQuickItem>(listview, "wrapper", i);
5277 controller.incubateWhile(&b);
5280 // verify positioning
5281 QQuickItem *contentItem = listview->contentItem();
5282 for (int i = 0; i < 8; ++i) {
5283 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5284 QTRY_COMPARE(item->y(), i*50.0);
5290 void tst_QQuickListView::snapOneItem_data()
5292 QTest::addColumn<QQuickListView::Orientation>("orientation");
5293 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5294 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5295 QTest::addColumn<int>("highlightRangeMode");
5296 QTest::addColumn<QPoint>("flickStart");
5297 QTest::addColumn<QPoint>("flickEnd");
5298 QTest::addColumn<qreal>("snapAlignment");
5299 QTest::addColumn<qreal>("endExtent");
5300 QTest::addColumn<qreal>("startExtent");
5302 QTest::newRow("vertical, top to bottom")
5303 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5304 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5306 QTest::newRow("vertical, bottom to top")
5307 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5308 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5310 QTest::newRow("horizontal, left to right")
5311 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5312 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5314 QTest::newRow("horizontal, right to left")
5315 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5316 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5318 QTest::newRow("vertical, top to bottom, enforce range")
5319 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5320 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5322 QTest::newRow("vertical, bottom to top, enforce range")
5323 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5324 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5326 QTest::newRow("horizontal, left to right, enforce range")
5327 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5328 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5330 QTest::newRow("horizontal, right to left, enforce range")
5331 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5332 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5335 void tst_QQuickListView::snapOneItem()
5337 QFETCH(QQuickListView::Orientation, orientation);
5338 QFETCH(Qt::LayoutDirection, layoutDirection);
5339 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5340 QFETCH(int, highlightRangeMode);
5341 QFETCH(QPoint, flickStart);
5342 QFETCH(QPoint, flickEnd);
5343 QFETCH(qreal, snapAlignment);
5344 QFETCH(qreal, endExtent);
5345 QFETCH(qreal, startExtent);
5348 // This test seems to be unreliable - different test data fails on different runs
5349 QSKIP("QTBUG-24338");
5352 QQuickView *canvas = getView();
5354 canvas->setSource(testFileUrl("snapOneItem.qml"));
5356 qApp->processEvents();
5358 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5359 QTRY_VERIFY(listview != 0);
5361 listview->setOrientation(orientation);
5362 listview->setLayoutDirection(layoutDirection);
5363 listview->setVerticalLayoutDirection(verticalLayoutDirection);
5364 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5365 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5367 QQuickItem *contentItem = listview->contentItem();
5368 QTRY_VERIFY(contentItem != 0);
5370 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5372 // confirm that a flick hits the next item boundary
5373 flick(canvas, flickStart, flickEnd, 180);
5374 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5375 if (orientation == QQuickListView::Vertical)
5376 QCOMPARE(listview->contentY(), snapAlignment);
5378 QCOMPARE(listview->contentX(), snapAlignment);
5380 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5381 QCOMPARE(listview->currentIndex(), 1);
5382 QCOMPARE(currentIndexSpy.count(), 1);
5387 flick(canvas, flickStart, flickEnd, 180);
5388 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5389 } while (orientation == QQuickListView::Vertical
5390 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5391 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5393 if (orientation == QQuickListView::Vertical)
5394 QCOMPARE(listview->contentY(), endExtent);
5396 QCOMPARE(listview->contentX(), endExtent);
5398 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5399 QCOMPARE(listview->currentIndex(), 3);
5400 QCOMPARE(currentIndexSpy.count(), 3);
5405 flick(canvas, flickEnd, flickStart, 180);
5406 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5407 } while (orientation == QQuickListView::Vertical
5408 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5409 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5411 if (orientation == QQuickListView::Vertical)
5412 QCOMPARE(listview->contentY(), startExtent);
5414 QCOMPARE(listview->contentX(), startExtent);
5416 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5417 QCOMPARE(listview->currentIndex(), 0);
5418 QCOMPARE(currentIndexSpy.count(), 6);
5421 releaseView(canvas);
5424 void tst_QQuickListView::unrequestedVisibility()
5427 for (int i = 0; i < 30; i++)
5428 model.addItem("Item" + QString::number(i), QString::number(i));
5430 QQuickView *canvas = new QQuickView(0);
5431 canvas->setGeometry(0,0,240,320);
5433 QQmlContext *ctxt = canvas->rootContext();
5434 ctxt->setContextProperty("testModel", &model);
5435 ctxt->setContextProperty("testWrap", QVariant(false));
5437 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5439 qApp->processEvents();
5441 QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
5442 QTRY_VERIFY(leftview != 0);
5444 QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
5445 QTRY_VERIFY(rightview != 0);
5447 QQuickItem *leftContent = leftview->contentItem();
5448 QTRY_VERIFY(leftContent != 0);
5450 QQuickItem *rightContent = rightview->contentItem();
5451 QTRY_VERIFY(rightContent != 0);
5453 rightview->setCurrentIndex(20);
5455 QTRY_COMPARE(leftview->contentY(), 0.0);
5456 QTRY_COMPARE(rightview->contentY(), 100.0);
5460 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5461 QCOMPARE(delegateVisible(item), true);
5462 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5463 QCOMPARE(delegateVisible(item), false);
5465 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5466 QCOMPARE(delegateVisible(item), false);
5467 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5468 QCOMPARE(delegateVisible(item), true);
5470 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5471 QCOMPARE(delegateVisible(item), true);
5472 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5473 QCOMPARE(delegateVisible(item), false);
5474 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5475 QCOMPARE(delegateVisible(item), false);
5476 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5477 QCOMPARE(delegateVisible(item), true);
5479 rightview->setCurrentIndex(0);
5481 QTRY_COMPARE(leftview->contentY(), 0.0);
5482 QTRY_COMPARE(rightview->contentY(), 0.0);
5484 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5485 QCOMPARE(delegateVisible(item), true);
5486 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5487 QTRY_COMPARE(delegateVisible(item), true);
5489 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5490 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5492 leftview->setCurrentIndex(20);
5494 QTRY_COMPARE(leftview->contentY(), 100.0);
5495 QTRY_COMPARE(rightview->contentY(), 0.0);
5497 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5498 QTRY_COMPARE(delegateVisible(item), false);
5499 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5500 QCOMPARE(delegateVisible(item), true);
5502 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5503 QCOMPARE(delegateVisible(item), true);
5504 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5505 QCOMPARE(delegateVisible(item), false);
5507 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5508 QCOMPARE(delegateVisible(item), false);
5509 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5510 QCOMPARE(delegateVisible(item), true);
5511 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5512 QCOMPARE(delegateVisible(item), true);
5513 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5514 QCOMPARE(delegateVisible(item), false);
5516 model.moveItems(19, 1, 1);
5517 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5519 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5520 QCOMPARE(delegateVisible(item), false);
5521 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5522 QCOMPARE(delegateVisible(item), true);
5524 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5525 QCOMPARE(delegateVisible(item), true);
5526 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5527 QCOMPARE(delegateVisible(item), false);
5529 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5530 QCOMPARE(delegateVisible(item), false);
5531 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5532 QCOMPARE(delegateVisible(item), true);
5533 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5534 QCOMPARE(delegateVisible(item), true);
5535 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5536 QCOMPARE(delegateVisible(item), false);
5538 model.moveItems(3, 4, 1);
5539 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5541 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5542 QCOMPARE(delegateVisible(item), false);
5543 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5544 QCOMPARE(delegateVisible(item), true);
5545 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5546 QCOMPARE(delegateVisible(item), true);
5547 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5548 QCOMPARE(delegateVisible(item), false);
5550 model.moveItems(4, 3, 1);
5551 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5553 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5554 QCOMPARE(delegateVisible(item), false);
5555 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5556 QCOMPARE(delegateVisible(item), true);
5557 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5558 QCOMPARE(delegateVisible(item), true);
5559 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5560 QCOMPARE(delegateVisible(item), false);
5562 model.moveItems(16, 17, 1);
5563 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5565 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5566 QCOMPARE(delegateVisible(item), false);
5567 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5568 QCOMPARE(delegateVisible(item), true);
5569 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5570 QCOMPARE(delegateVisible(item), true);
5571 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5572 QCOMPARE(delegateVisible(item), false);
5574 model.moveItems(17, 16, 1);
5575 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5577 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5578 QCOMPARE(delegateVisible(item), false);
5579 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5580 QCOMPARE(delegateVisible(item), true);
5581 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5582 QCOMPARE(delegateVisible(item), true);
5583 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5584 QCOMPARE(delegateVisible(item), false);
5589 void tst_QQuickListView::populateTransitions()
5591 QFETCH(bool, staticallyPopulate);
5592 QFETCH(bool, dynamicallyPopulate);
5593 QFETCH(bool, usePopulateTransition);
5595 QPointF transitionFrom(-50, -50);
5596 QPointF transitionVia(100, 100);
5597 QaimModel model_transitionFrom;
5598 QaimModel model_transitionVia;
5601 if (staticallyPopulate) {
5602 for (int i = 0; i < 30; i++)
5603 model.addItem("item" + QString::number(i), "");
5606 QQuickView *canvas = getView();
5607 canvas->rootContext()->setContextProperty("testModel", &model);
5608 canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
5609 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5610 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5611 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5612 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
5613 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5614 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5615 canvas->setSource(testFileUrl("populateTransitions.qml"));
5617 QTest::qWaitForWindowShown(canvas);
5619 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5621 QQuickItem *contentItem = listview->contentItem();
5622 QVERIFY(contentItem);
5624 if (staticallyPopulate && usePopulateTransition) {
5625 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5626 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5627 } else if (dynamicallyPopulate) {
5628 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5629 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5631 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5632 QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5633 QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5636 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5637 for (int i=0; i < model.count() && i < itemCount; ++i) {
5638 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5639 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5640 QTRY_COMPARE(item->x(), 0.0);
5641 QTRY_COMPARE(item->y(), i*20.0);
5642 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5644 QTRY_COMPARE(name->text(), model.name(i));
5647 listview->setProperty("countPopulateTransitions", 0);
5648 listview->setProperty("countAddTransitions", 0);
5650 // add an item and check this is done with add transition, not populate
5651 model.insertItem(0, "another item", "");
5652 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5653 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5656 canvas->rootContext()->setContextProperty("testModel", QVariant());
5657 QTRY_COMPARE(listview->count(), 0);
5658 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5659 listview->setProperty("countPopulateTransitions", 0);
5660 listview->setProperty("countAddTransitions", 0);
5662 // set to a valid model and check populate transition is run a second time
5664 for (int i = 0; i < 30; i++)
5665 model.addItem("item" + QString::number(i), "");
5666 canvas->rootContext()->setContextProperty("testModel", &model);
5667 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5668 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5670 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5671 for (int i=0; i < model.count() && i < itemCount; ++i) {
5672 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5673 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5674 QTRY_COMPARE(item->x(), 0.0);
5675 QTRY_COMPARE(item->y(), i*20.0);
5676 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5678 QTRY_COMPARE(name->text(), model.name(i));
5681 // reset model and check populate transition is run again
5682 listview->setProperty("countPopulateTransitions", 0);
5683 listview->setProperty("countAddTransitions", 0);
5685 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5686 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5688 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5689 for (int i=0; i < model.count() && i < itemCount; ++i) {
5690 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5691 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5692 QTRY_COMPARE(item->x(), 0.0);
5693 QTRY_COMPARE(item->y(), i*20.0);
5694 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5696 QTRY_COMPARE(name->text(), model.name(i));
5699 releaseView(canvas);
5702 void tst_QQuickListView::populateTransitions_data()
5704 QTest::addColumn<bool>("staticallyPopulate");
5705 QTest::addColumn<bool>("dynamicallyPopulate");
5706 QTest::addColumn<bool>("usePopulateTransition");
5708 QTest::newRow("static") << true << false << true;
5709 QTest::newRow("static, no populate") << true << false << false;
5711 QTest::newRow("dynamic") << false << true << true;
5712 QTest::newRow("dynamic, no populate") << false << true << false;
5714 QTest::newRow("empty to start with") << false << false << true;
5715 QTest::newRow("empty to start with, no populate") << false << false << false;
5718 void tst_QQuickListView::addTransitions()
5720 QFETCH(int, initialItemCount);
5721 QFETCH(bool, shouldAnimateTargets);
5722 QFETCH(qreal, contentY);
5723 QFETCH(int, insertionIndex);
5724 QFETCH(int, insertionCount);
5725 QFETCH(ListRange, expectedDisplacedIndexes);
5727 // added items should start here
5728 QPointF targetItems_transitionFrom(-50, -50);
5730 // displaced items should pass through this point
5731 QPointF displacedItems_transitionVia(100, 100);
5734 for (int i = 0; i < initialItemCount; i++)
5735 model.addItem("Original item" + QString::number(i), "");
5736 QaimModel model_targetItems_transitionFrom;
5737 QaimModel model_displacedItems_transitionVia;
5739 QQuickView *canvas = getView();
5740 QQmlContext *ctxt = canvas->rootContext();
5741 TestObject *testObject = new TestObject;
5742 ctxt->setContextProperty("testModel", &model);
5743 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5744 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5745 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5746 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5747 ctxt->setContextProperty("testObject", testObject);
5748 canvas->setSource(testFileUrl("addTransitions.qml"));
5750 QTest::qWaitForWindowShown(canvas);
5752 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5753 QTRY_VERIFY(listview != 0);
5754 QQuickItem *contentItem = listview->contentItem();
5755 QVERIFY(contentItem != 0);
5756 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5758 if (contentY != 0) {
5759 listview->setContentY(contentY);
5760 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5763 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5765 // only target items that will become visible should be animated
5766 QList<QPair<QString, QString> > newData;
5767 QList<QPair<QString, QString> > expectedTargetData;
5768 QList<int> targetIndexes;
5769 if (shouldAnimateTargets) {
5770 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5771 newData << qMakePair(QString("New item %1").arg(i), QString(""));
5773 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
5774 expectedTargetData << newData.last();
5778 QVERIFY(expectedTargetData.count() > 0);
5782 if (!newData.isEmpty()) {
5783 model.insertItems(insertionIndex, newData);
5784 QTRY_COMPARE(model.count(), listview->count());
5787 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5789 if (shouldAnimateTargets) {
5790 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5791 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5792 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5794 // check the target and displaced items were animated
5795 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5796 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5798 // check attached properties
5799 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5800 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5801 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5802 if (expectedDisplacedIndexes.isValid()) {
5803 // adjust expectedDisplacedIndexes to their final values after the move
5804 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5805 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5806 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5807 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5811 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5812 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5815 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5816 int firstVisibleIndex = -1;
5817 int itemCount = items.count();
5818 for (int i=0; i<items.count(); i++) {
5819 if (items[i]->y() >= contentY) {
5820 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5821 firstVisibleIndex = e.evaluate().toInt();
5825 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5827 // verify all items moved to the correct final positions
5828 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5829 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5830 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5831 QTRY_COMPARE(item->y(), i*20.0);
5832 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5834 QTRY_COMPARE(name->text(), model.name(i));
5837 releaseView(canvas);
5841 void tst_QQuickListView::addTransitions_data()
5843 QTest::addColumn<int>("initialItemCount");
5844 QTest::addColumn<qreal>("contentY");
5845 QTest::addColumn<bool>("shouldAnimateTargets");
5846 QTest::addColumn<int>("insertionIndex");
5847 QTest::addColumn<int>("insertionCount");
5848 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5850 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5851 QTest::newRow("insert 1, just before start")
5852 << 30 << 20.0 << false
5853 << 0 << 1 << ListRange();
5854 QTest::newRow("insert 1, way before start")
5855 << 30 << 20.0 << false
5856 << 0 << 1 << ListRange();
5857 QTest::newRow("insert multiple, just before start")
5858 << 30 << 100.0 << false
5859 << 0 << 3 << ListRange();
5860 QTest::newRow("insert multiple, way before start")
5861 << 30 << 100.0 << false
5862 << 0 << 3 << ListRange();
5864 QTest::newRow("insert 1 at start")
5865 << 30 << 0.0 << true
5866 << 0 << 1 << ListRange(0, 15);
5867 QTest::newRow("insert multiple at start")
5868 << 30 << 0.0 << true
5869 << 0 << 3 << ListRange(0, 15);
5870 QTest::newRow("insert 1 at start, content y not 0")
5871 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5872 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5873 QTest::newRow("insert multiple at start, content y not 0")
5874 << 30 << 40.0 << true // first visible is index 2
5875 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5877 QTest::newRow("insert 1 at start, to empty list")
5879 << 0 << 1 << ListRange();
5880 QTest::newRow("insert multiple at start, to empty list")
5882 << 0 << 3 << ListRange();
5884 QTest::newRow("insert 1 at middle")
5885 << 30 << 0.0 << true
5886 << 5 << 1 << ListRange(5, 15);
5887 QTest::newRow("insert multiple at middle")
5888 << 30 << 0.0 << true
5889 << 5 << 3 << ListRange(5, 15);
5891 QTest::newRow("insert 1 at bottom")
5892 << 30 << 0.0 << true
5893 << 15 << 1 << ListRange(15, 15);
5894 QTest::newRow("insert multiple at bottom")
5895 << 30 << 0.0 << true
5896 << 15 << 3 << ListRange(15, 15);
5897 QTest::newRow("insert 1 at bottom, content y not 0")
5898 << 30 << 20.0 * 3 << true
5899 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5900 QTest::newRow("insert multiple at bottom, content y not 0")
5901 << 30 << 20.0 * 3 << true
5902 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5904 // items added after the last visible will not be animated in, since they
5905 // do not appear in the final view
5906 QTest::newRow("insert 1 after end")
5907 << 30 << 0.0 << false
5908 << 17 << 1 << ListRange();
5909 QTest::newRow("insert multiple after end")
5910 << 30 << 0.0 << false
5911 << 17 << 3 << ListRange();
5914 void tst_QQuickListView::moveTransitions()
5916 QFETCH(int, initialItemCount);
5917 QFETCH(qreal, contentY);
5918 QFETCH(qreal, itemsOffsetAfterMove);
5919 QFETCH(int, moveFrom);
5920 QFETCH(int, moveTo);
5921 QFETCH(int, moveCount);
5922 QFETCH(ListRange, expectedDisplacedIndexes);
5924 // target and displaced items should pass through these points
5925 QPointF targetItems_transitionVia(-50, 50);
5926 QPointF displacedItems_transitionVia(100, 100);
5929 for (int i = 0; i < initialItemCount; i++)
5930 model.addItem("Original item" + QString::number(i), "");
5931 QaimModel model_targetItems_transitionVia;
5932 QaimModel model_displacedItems_transitionVia;
5934 QQuickView *canvas = getView();
5935 QQmlContext *ctxt = canvas->rootContext();
5936 TestObject *testObject = new TestObject;
5937 ctxt->setContextProperty("testModel", &model);
5938 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5939 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5940 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5941 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5942 ctxt->setContextProperty("testObject", testObject);
5943 canvas->setSource(testFileUrl("moveTransitions.qml"));
5945 QTest::qWaitForWindowShown(canvas);
5947 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5948 QTRY_VERIFY(listview != 0);
5949 QQuickItem *contentItem = listview->contentItem();
5950 QVERIFY(contentItem != 0);
5953 if (contentY != 0) {
5954 listview->setContentY(contentY);
5955 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5958 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5960 // Items moving to *or* from visible positions should be animated.
5961 // Otherwise, they should not be animated.
5962 QList<QPair<QString, QString> > expectedTargetData;
5963 QList<int> targetIndexes;
5964 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5965 int toIndex = moveTo + (i - moveFrom);
5966 if (i <= (contentY + listview->height()) / 20
5967 || toIndex < (contentY + listview->height()) / 20) {
5968 expectedTargetData << qMakePair(model.name(i), model.number(i));
5972 // ViewTransition.index provides the indices that items are moving to, not from
5973 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5976 model.moveItems(moveFrom, moveTo, moveCount);
5978 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5979 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5980 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5982 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5984 // check the target and displaced items were animated
5985 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5986 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5988 // check attached properties
5989 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5990 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5991 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5992 if (expectedDisplacedIndexes.isValid()) {
5993 // adjust expectedDisplacedIndexes to their final values after the move
5994 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5995 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5996 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5997 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6000 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6001 int firstVisibleIndex = -1;
6002 for (int i=0; i<items.count(); i++) {
6003 if (items[i]->y() >= contentY) {
6004 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6005 firstVisibleIndex = e.evaluate().toInt();
6009 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6011 // verify all items moved to the correct final positions
6012 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
6013 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6014 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6015 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6016 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
6017 name = findItem<QQuickText>(contentItem, "textName", i);
6019 QTRY_COMPARE(name->text(), model.name(i));
6022 releaseView(canvas);
6026 void tst_QQuickListView::moveTransitions_data()
6028 QTest::addColumn<int>("initialItemCount");
6029 QTest::addColumn<qreal>("contentY");
6030 QTest::addColumn<qreal>("itemsOffsetAfterMove");
6031 QTest::addColumn<int>("moveFrom");
6032 QTest::addColumn<int>("moveTo");
6033 QTest::addColumn<int>("moveCount");
6034 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6036 // when removing from above the visible, all items shift down depending on how many
6037 // items have been removed from above the visible
6038 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
6039 << 1 << 10 << 1 << ListRange(11, 15+4);
6040 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
6041 << 0 << 10 << 1 << ListRange(11, 15+4);
6042 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
6043 << 1 << 10 << 2 << ListRange(12, 15+4);
6044 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
6045 << 0 << 10 << 3 << ListRange(13, 15+4);
6046 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
6047 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
6048 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
6049 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
6051 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
6052 << 1 << 10 << 1 << ListRange(2, 10);
6053 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
6054 << 0 << 10 << 1 << ListRange(1, 10);
6055 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6056 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
6057 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
6058 << 10 << 15 << 1 << ListRange(11, 15);
6059 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
6060 << 0 << 15 << 1 << ListRange(1, 15);
6062 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
6063 << 1 << 10 << 3 << ListRange(4, 12);
6064 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
6065 << 0 << 10 << 3 << ListRange(3, 12);
6066 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6067 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
6068 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
6069 << 5 << 13 << 3 << ListRange(8, 15);
6070 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
6071 << 0 << 13 << 3 << ListRange(3, 15);
6073 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
6074 << 10 << 1 << 1 << ListRange(1, 9);
6075 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
6076 << 10 << 0 << 1 << ListRange(0, 9);
6077 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6078 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6079 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
6080 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6081 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
6082 << 15 << 10 << 1 << ListRange(10, 14);
6083 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
6084 << 15 << 0 << 1 << ListRange(0, 14);
6086 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
6087 << 10 << 1 << 3 << ListRange(1, 9);
6088 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
6089 << 10 << 0 << 3 << ListRange(0, 9);
6090 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6091 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
6092 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
6093 << 13 << 5 << 3 << ListRange(5, 12);
6094 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
6095 << 13 << 0 << 3 << ListRange(0, 12);
6097 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
6098 << 20 << 0 << 1 << ListRange(0, 15);
6099 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6100 << 25 << 4 << 1 << ListRange(0+4, 15+4);
6101 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
6102 << 20 << 0 << 3 << ListRange(0, 15);
6103 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6104 << 25 << 4 << 3 << ListRange(0+4, 15+4);
6106 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
6107 << 20 << 15 << 1 << ListRange(15, 15);
6108 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6109 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
6110 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6111 << 20 << 15 << 3 << ListRange(15, 15);
6112 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6113 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6116 void tst_QQuickListView::removeTransitions()
6118 QFETCH(int, initialItemCount);
6119 QFETCH(bool, shouldAnimateTargets);
6120 QFETCH(qreal, contentY);
6121 QFETCH(int, removalIndex);
6122 QFETCH(int, removalCount);
6123 QFETCH(ListRange, expectedDisplacedIndexes);
6125 // added items should end here
6126 QPointF targetItems_transitionTo(-50, -50);
6128 // displaced items should pass through this points
6129 QPointF displacedItems_transitionVia(100, 100);
6132 for (int i = 0; i < initialItemCount; i++)
6133 model.addItem("Original item" + QString::number(i), "");
6134 QaimModel model_targetItems_transitionTo;
6135 QaimModel model_displacedItems_transitionVia;
6137 QQuickView *canvas = getView();
6138 QQmlContext *ctxt = canvas->rootContext();
6139 TestObject *testObject = new TestObject;
6140 ctxt->setContextProperty("testModel", &model);
6141 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6142 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6143 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6144 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6145 ctxt->setContextProperty("testObject", testObject);
6146 canvas->setSource(testFileUrl("removeTransitions.qml"));
6148 QTest::qWaitForWindowShown(canvas);
6150 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6151 QTRY_VERIFY(listview != 0);
6152 QQuickItem *contentItem = listview->contentItem();
6153 QVERIFY(contentItem != 0);
6154 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6156 if (contentY != 0) {
6157 listview->setContentY(contentY);
6158 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6161 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6163 // only target items that are visible should be animated
6164 QList<QPair<QString, QString> > expectedTargetData;
6165 QList<int> targetIndexes;
6166 if (shouldAnimateTargets) {
6167 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6168 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6169 expectedTargetData << qMakePair(model.name(i), model.number(i));
6173 QVERIFY(expectedTargetData.count() > 0);
6176 // calculate targetItems and expectedTargets before model changes
6177 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6178 QVariantMap expectedTargets;
6179 for (int i=0; i<targetIndexes.count(); i++)
6180 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6183 model.removeItems(removalIndex, removalCount);
6184 QTRY_COMPARE(model.count(), listview->count());
6186 if (shouldAnimateTargets) {
6187 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6188 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6189 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6191 // check the target and displaced items were animated
6192 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6193 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6195 // check attached properties
6196 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6197 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6198 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6199 if (expectedDisplacedIndexes.isValid()) {
6200 // adjust expectedDisplacedIndexes to their final values after the move
6201 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6202 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6203 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6204 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6207 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6208 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6211 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6212 int firstVisibleIndex = -1;
6213 int itemCount = items.count();
6215 for (int i=0; i<items.count(); i++) {
6216 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6217 int index = e.evaluate().toInt();
6218 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6219 firstVisibleIndex = index;
6221 itemCount--; // exclude deleted items
6223 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6225 // verify all items moved to the correct final positions
6226 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6227 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6228 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6229 QCOMPARE(item->x(), 0.0);
6230 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6231 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6233 QTRY_COMPARE(name->text(), model.name(i));
6236 releaseView(canvas);
6240 void tst_QQuickListView::removeTransitions_data()
6242 QTest::addColumn<int>("initialItemCount");
6243 QTest::addColumn<qreal>("contentY");
6244 QTest::addColumn<bool>("shouldAnimateTargets");
6245 QTest::addColumn<int>("removalIndex");
6246 QTest::addColumn<int>("removalCount");
6247 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6249 // All items that are visible following the remove operation should be animated.
6250 // Remove targets that are outside of the view should not be animated.
6252 QTest::newRow("remove 1 before start")
6253 << 30 << 20.0 * 3 << false
6254 << 2 << 1 << ListRange();
6255 QTest::newRow("remove multiple, all before start")
6256 << 30 << 20.0 * 3 << false
6257 << 0 << 3 << ListRange();
6258 QTest::newRow("remove mix of before and after start")
6259 << 30 << 20.0 * 3 << true
6260 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
6262 QTest::newRow("remove 1 from start")
6263 << 30 << 0.0 << true
6264 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
6265 QTest::newRow("remove multiple from start")
6266 << 30 << 0.0 << true
6267 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
6268 QTest::newRow("remove 1 from start, content y not 0")
6269 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
6270 << 2 << 1 << ListRange(1 + 2, 16 + 2);
6271 QTest::newRow("remove multiple from start, content y not 0")
6272 << 30 << 20.0 * 2 << true // first visible is index 2
6273 << 2 << 3 << ListRange(3 + 2, 18 + 2);
6275 QTest::newRow("remove 1 from middle")
6276 << 30 << 0.0 << true
6277 << 5 << 1 << ListRange(6, 16);
6278 QTest::newRow("remove multiple from middle")
6279 << 30 << 0.0 << true
6280 << 5 << 3 << ListRange(8, 18);
6283 QTest::newRow("remove 1 from bottom")
6284 << 30 << 0.0 << true
6285 << 15 << 1 << ListRange(16, 16);
6287 // remove 15, 16, 17
6288 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6289 // the view, and 18 will be animated as the displaced item to replace the last item
6290 QTest::newRow("remove multiple from bottom")
6291 << 30 << 0.0 << true
6292 << 15 << 3 << ListRange(18, 18);
6294 QTest::newRow("remove 1 from bottom, content y not 0")
6295 << 30 << 20.0 * 2 << true
6296 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6297 QTest::newRow("remove multiple from bottom, content y not 0")
6298 << 30 << 20.0 * 2 << true
6299 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6302 QTest::newRow("remove 1 after end")
6303 << 30 << 0.0 << false
6304 << 17 << 1 << ListRange();
6305 QTest::newRow("remove multiple after end")
6306 << 30 << 0.0 << false
6307 << 17 << 3 << ListRange();
6310 void tst_QQuickListView::displacedTransitions()
6312 QFETCH(bool, useDisplaced);
6313 QFETCH(bool, displacedEnabled);
6314 QFETCH(bool, useAddDisplaced);
6315 QFETCH(bool, addDisplacedEnabled);
6316 QFETCH(bool, useMoveDisplaced);
6317 QFETCH(bool, moveDisplacedEnabled);
6318 QFETCH(bool, useRemoveDisplaced);
6319 QFETCH(bool, removeDisplacedEnabled);
6320 QFETCH(ListChange, change);
6321 QFETCH(ListRange, expectedDisplacedIndexes);
6324 for (int i = 0; i < 30; i++)
6325 model.addItem("Original item" + QString::number(i), "");
6326 QaimModel model_displaced_transitionVia;
6327 QaimModel model_addDisplaced_transitionVia;
6328 QaimModel model_moveDisplaced_transitionVia;
6329 QaimModel model_removeDisplaced_transitionVia;
6331 QPointF displaced_transitionVia(-50, -100);
6332 QPointF addDisplaced_transitionVia(-150, 100);
6333 QPointF moveDisplaced_transitionVia(50, -100);
6334 QPointF removeDisplaced_transitionVia(150, 100);
6336 QQuickView *canvas = getView();
6337 QQmlContext *ctxt = canvas->rootContext();
6338 TestObject *testObject = new TestObject(canvas);
6339 ctxt->setContextProperty("testModel", &model);
6340 ctxt->setContextProperty("testObject", testObject);
6341 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6342 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6343 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6344 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6345 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6346 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6347 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6348 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6349 ctxt->setContextProperty("useDisplaced", useDisplaced);
6350 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6351 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6352 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6353 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6354 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6355 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6356 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6357 canvas->setSource(testFileUrl("displacedTransitions.qml"));
6359 qApp->processEvents();
6361 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6362 QTRY_VERIFY(listview != 0);
6363 QQuickItem *contentItem = listview->contentItem();
6364 QVERIFY(contentItem != 0);
6365 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6367 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6368 listview->setProperty("displaceTransitionsDone", false);
6370 switch (change.type) {
6371 case ListChange::Inserted:
6373 QList<QPair<QString, QString> > targetItemData;
6374 for (int i=change.index; i<change.index + change.count; ++i)
6375 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6376 model.insertItems(change.index, targetItemData);
6377 QTRY_COMPARE(model.count(), listview->count());
6380 case ListChange::Removed:
6381 model.removeItems(change.index, change.count);
6382 QTRY_COMPARE(model.count(), listview->count());
6384 case ListChange::Moved:
6385 model.moveItems(change.index, change.to, change.count);
6386 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6388 case ListChange::SetCurrent:
6389 case ListChange::SetContentY:
6390 case ListChange::Polish:
6394 QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6395 QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6397 if ((useDisplaced && displacedEnabled)
6398 || (useAddDisplaced && addDisplacedEnabled)
6399 || (useMoveDisplaced && moveDisplacedEnabled)
6400 || (useRemoveDisplaced && removeDisplacedEnabled)) {
6401 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6403 // check the correct number of target items and indexes were received
6404 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6405 for (int i=0; i<resultTargetIndexes.count(); i++)
6406 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6407 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6408 for (int i=0; i<resultTargetItems.count(); i++)
6409 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6411 QCOMPARE(resultTargetIndexes.count(), 0);
6412 QCOMPARE(resultTargetItems.count(), 0);
6415 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6416 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6418 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6419 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6420 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6422 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6423 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6424 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6426 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6428 if (useDisplaced && displacedEnabled
6429 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6430 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6431 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6432 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6434 QCOMPARE(model_displaced_transitionVia.count(), 0);
6437 // verify all items moved to the correct final positions
6438 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6439 for (int i=0; i < model.count() && i < items.count(); ++i) {
6440 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6441 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6442 QCOMPARE(item->x(), 0.0);
6443 QCOMPARE(item->y(), i * 20.0);
6444 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6446 QTRY_COMPARE(name->text(), model.name(i));
6449 releaseView(canvas);
6452 void tst_QQuickListView::displacedTransitions_data()
6454 QTest::addColumn<bool>("useDisplaced");
6455 QTest::addColumn<bool>("displacedEnabled");
6456 QTest::addColumn<bool>("useAddDisplaced");
6457 QTest::addColumn<bool>("addDisplacedEnabled");
6458 QTest::addColumn<bool>("useMoveDisplaced");
6459 QTest::addColumn<bool>("moveDisplacedEnabled");
6460 QTest::addColumn<bool>("useRemoveDisplaced");
6461 QTest::addColumn<bool>("removeDisplacedEnabled");
6462 QTest::addColumn<ListChange>("change");
6463 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6465 QTest::newRow("no displaced transitions at all")
6470 << ListChange::insert(0, 1) << ListRange(0, 15);
6472 QTest::newRow("just displaced")
6477 << ListChange::insert(0, 1) << ListRange(0, 15);
6479 QTest::newRow("just displaced (not enabled)")
6484 << ListChange::insert(0, 1) << ListRange(0, 15);
6486 QTest::newRow("displaced + addDisplaced")
6491 << ListChange::insert(0, 1) << ListRange(0, 15);
6493 QTest::newRow("displaced + addDisplaced (not enabled)")
6498 << ListChange::insert(0, 1) << ListRange(0, 15);
6500 QTest::newRow("displaced + moveDisplaced")
6505 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6507 QTest::newRow("displaced + moveDisplaced (not enabled)")
6512 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6514 QTest::newRow("displaced + removeDisplaced")
6519 << ListChange::remove(0, 1) << ListRange(1, 16);
6521 QTest::newRow("displaced + removeDisplaced (not enabled)")
6526 << ListChange::remove(0, 1) << ListRange(1, 16);
6529 QTest::newRow("displaced + add, should use generic displaced for a remove")
6534 << ListChange::remove(0, 1) << ListRange(1, 16);
6537 void tst_QQuickListView::multipleTransitions()
6539 // Tests that if you interrupt a transition in progress with another action that
6540 // cancels the previous transition, the resulting items are still placed correctly.
6542 QFETCH(int, initialCount);
6543 QFETCH(qreal, contentY);
6544 QFETCH(QList<ListChange>, changes);
6545 QFETCH(bool, enableAddTransitions);
6546 QFETCH(bool, enableMoveTransitions);
6547 QFETCH(bool, enableRemoveTransitions);
6548 QFETCH(bool, rippleAddDisplaced);
6550 QPointF addTargets_transitionFrom(-50, -50);
6551 QPointF addDisplaced_transitionFrom(-50, 50);
6552 QPointF moveTargets_transitionFrom(50, -50);
6553 QPointF moveDisplaced_transitionFrom(50, 50);
6554 QPointF removeTargets_transitionTo(-100, 300);
6555 QPointF removeDisplaced_transitionFrom(100, 300);
6558 for (int i = 0; i < initialCount; i++)
6559 model.addItem("Original item" + QString::number(i), "");
6561 QQuickView *canvas = getView();
6562 QQmlContext *ctxt = canvas->rootContext();
6563 TestObject *testObject = new TestObject;
6564 ctxt->setContextProperty("testModel", &model);
6565 ctxt->setContextProperty("testObject", testObject);
6566 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6567 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6568 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6569 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6570 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6571 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6572 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6573 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6574 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6575 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6576 canvas->setSource(testFileUrl("multipleTransitions.qml"));
6578 QTest::qWaitForWindowShown(canvas);
6580 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6581 QTRY_VERIFY(listview != 0);
6582 QQuickItem *contentItem = listview->contentItem();
6583 QVERIFY(contentItem != 0);
6584 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6586 if (contentY != 0) {
6587 listview->setContentY(contentY);
6588 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6591 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
6593 for (int i=0; i<changes.count(); i++) {
6594 switch (changes[i].type) {
6595 case ListChange::Inserted:
6597 QList<QPair<QString, QString> > targetItems;
6598 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6599 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6600 model.insertItems(changes[i].index, targetItems);
6601 QTRY_COMPARE(model.count(), listview->count());
6602 if (i == changes.count() - 1) {
6603 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6604 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6606 QTest::qWait(timeBetweenActions);
6610 case ListChange::Removed:
6611 model.removeItems(changes[i].index, changes[i].count);
6612 QTRY_COMPARE(model.count(), listview->count());
6613 if (i == changes.count() - 1) {
6614 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6615 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6617 QTest::qWait(timeBetweenActions);
6620 case ListChange::Moved:
6621 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6622 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6623 if (i == changes.count() - 1) {
6624 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6625 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6627 QTest::qWait(timeBetweenActions);
6630 case ListChange::SetCurrent:
6631 listview->setCurrentIndex(changes[i].index);
6633 case ListChange::SetContentY:
6634 listview->setContentY(changes[i].pos);
6635 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6637 case ListChange::Polish:
6641 QCOMPARE(listview->count(), model.count());
6643 // verify all items moved to the correct final positions
6644 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6645 for (int i=0; i < model.count() && i < items.count(); ++i) {
6646 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6647 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6648 QTRY_COMPARE(item->x(), 0.0);
6649 QTRY_COMPARE(item->y(), i*20.0);
6650 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6652 QTRY_COMPARE(name->text(), model.name(i));
6655 releaseView(canvas);
6659 void tst_QQuickListView::multipleTransitions_data()
6661 QTest::addColumn<int>("initialCount");
6662 QTest::addColumn<qreal>("contentY");
6663 QTest::addColumn<QList<ListChange> >("changes");
6664 QTest::addColumn<bool>("enableAddTransitions");
6665 QTest::addColumn<bool>("enableMoveTransitions");
6666 QTest::addColumn<bool>("enableRemoveTransitions");
6667 QTest::addColumn<bool>("rippleAddDisplaced");
6669 // the added item and displaced items should move to final dest correctly
6670 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6671 << ListChange::insert(0, 1)
6672 << ListChange::move(0, 3, 1)
6674 << true << true << true << false;
6676 // items affected by the add should change from move to add transition
6677 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6678 << ListChange::move(1, 10, 3)
6679 << ListChange::insert(0, 1)
6681 << true << true << true << false;
6683 // items should be placed correctly if you trigger a transition then refill for that index
6684 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6685 << ListChange::insert(0, 1)
6686 << ListChange::setContentY(80.0)
6687 << ListChange::setContentY(0.0)
6688 << ListChange::insert(0, 1)
6690 << true << true << true << false;
6692 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6693 << ListChange::insert(1, 1)
6694 << ListChange::remove(1, 1)
6696 << true << true << true << true;
6698 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6699 // even if a remove-displace transition is not present to re-animate them
6700 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6701 << ListChange::insert(0, 1)
6702 << ListChange::remove(2, 1)
6704 << true << true << false << false;
6706 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6707 // remove has changed the position of where it will move to
6708 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6709 << ListChange::setContentY(-10.0)
6710 << ListChange::insert(0, 1)
6711 << ListChange::insert(0, 1)
6712 << ListChange::remove(2, 1)
6714 << true << true << false << false;
6717 void tst_QQuickListView::multipleDisplaced()
6719 // multiple move() operations should only restart displace transitions for items that
6720 // moved from previously set positions, and not those that have moved from their current
6721 // item positions (which may e.g. still be changing from easing bounces in the last transition)
6724 for (int i = 0; i < 30; i++)
6725 model.addItem("Original item" + QString::number(i), "");
6727 QQuickView *canvas = getView();
6728 QQmlContext *ctxt = canvas->rootContext();
6729 ctxt->setContextProperty("testModel", &model);
6730 ctxt->setContextProperty("testObject", new TestObject(canvas));
6731 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6733 QTest::qWaitForWindowShown(canvas);
6735 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6736 QTRY_VERIFY(listview != 0);
6737 QQuickItem *contentItem = listview->contentItem();
6738 QVERIFY(contentItem != 0);
6739 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6741 model.moveItems(12, 8, 1);
6742 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6743 model.moveItems(8, 3, 1);
6744 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6746 QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6747 foreach (const QString &name, transitionsStarted.keys()) {
6748 QVERIFY2(transitionsStarted[name] == 1,
6749 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6752 // verify all items moved to the correct final positions
6753 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6754 for (int i=0; i < model.count() && i < items.count(); ++i) {
6755 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6756 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6757 QTRY_COMPARE(item->x(), 0.0);
6758 QTRY_COMPARE(item->y(), i*20.0);
6759 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6761 QTRY_COMPARE(name->text(), model.name(i));
6764 releaseView(canvas);
6767 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6771 for (int i=0; i<list.count(); i++) {
6772 ret << list[i].toInt(&ok);
6774 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6780 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6782 for (int i=0; i<indexLists.count(); i++) {
6783 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6784 if (current != expectedIndexes.toSet())
6785 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6786 QCOMPARE(current, expectedIndexes.toSet());
6790 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6792 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6793 QVERIFY(it.value().type() == QVariant::Int);
6794 QString name = it.key();
6795 int itemIndex = it.value().toInt();
6796 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6797 if (model.name(itemIndex) != name)
6798 qDebug() << itemIndex;
6799 QCOMPARE(model.name(itemIndex), name);
6801 QCOMPARE(items.count(), expectedIndexes.count());
6804 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6806 for (int i=0; i<itemLists.count(); i++) {
6807 QVERIFY(itemLists[i].type() == QVariant::List);
6808 QVariantList current = itemLists[i].toList();
6809 for (int j=0; j<current.count(); j++) {
6810 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6811 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6812 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6814 QCOMPARE(current.count(), expectedItems.count());
6818 void tst_QQuickListView::flickBeyondBounds()
6820 QQuickView *canvas = createView();
6822 canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6824 qApp->processEvents();
6826 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6827 QTRY_VERIFY(listview != 0);
6829 QQuickItem *contentItem = listview->contentItem();
6830 QTRY_VERIFY(contentItem != 0);
6831 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6833 // Flick view up beyond bounds
6834 flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6835 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6837 // We're really testing that we don't get stuck in a loop,
6838 // but also confirm items positioned correctly.
6839 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6840 for (int i = 0; i < 2; ++i) {
6841 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6842 if (!item) qWarning() << "Item" << i << "not found";
6844 QTRY_VERIFY(item->y() == i*45);
6850 void tst_QQuickListView::destroyItemOnCreation()
6853 QQuickView *canvas = createView();
6854 canvas->rootContext()->setContextProperty("testModel", &model);
6856 canvas->setSource(testFileUrl("destroyItemOnCreation.qml"));
6858 qApp->processEvents();
6860 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6861 QVERIFY(listview != 0);
6863 QQuickItem *contentItem = listview->contentItem();
6864 QVERIFY(contentItem != 0);
6865 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6867 QCOMPARE(canvas->rootObject()->property("createdIndex").toInt(), -1);
6868 model.addItem("new item", "");
6869 QTRY_COMPARE(canvas->rootObject()->property("createdIndex").toInt(), 0);
6871 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6872 QCOMPARE(model.count(), 0);
6877 void tst_QQuickListView::parentBinding()
6879 QQuickView *canvas = createView();
6882 QtMsgHandler old = qInstallMsgHandler(errorMsgHandler);
6884 canvas->setSource(testFileUrl("parentBinding.qml"));
6886 qApp->processEvents();
6888 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
6889 QVERIFY(listview != 0);
6891 QQuickItem *contentItem = listview->contentItem();
6892 QVERIFY(contentItem != 0);
6893 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6895 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6897 QCOMPARE(item->width(), listview->width());
6898 QCOMPARE(item->height(), listview->height()/12);
6900 // there should be no transient binding error
6901 QVERIFY(!m_errorCount);
6903 qInstallMsgHandler(old);
6908 QTEST_MAIN(tst_QQuickListView)
6910 #include "tst_qquicklistview.moc"