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 // Turn sticky footer off
2336 listview->setContentY(20);
2337 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2338 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2339 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2340 QCOMPARE(item->y(), 340.);
2342 // Turn sticky header off
2343 listview->setContentY(30);
2344 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2345 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2346 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2347 QCOMPARE(item->y(), 0.);
2349 // if an empty model is set the header/footer should be cleaned up
2350 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2351 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2352 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2353 QmlListModel model1;
2354 ctxt->setContextProperty("testModel", &model1);
2355 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2356 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2358 // clear model - header/footer should be cleaned up
2359 ctxt->setContextProperty("testModel", &model);
2360 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2361 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2363 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2364 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2369 void tst_QQuickListView::sectionPropertyChange()
2371 QQuickView *canvas = createView();
2373 canvas->setSource(testFileUrl("sectionpropertychange.qml"));
2375 qApp->processEvents();
2377 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2378 QTRY_VERIFY(listview != 0);
2380 QQuickItem *contentItem = listview->contentItem();
2381 QTRY_VERIFY(contentItem != 0);
2383 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2385 // Confirm items positioned correctly
2386 for (int i = 0; i < 2; ++i) {
2387 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2389 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2392 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2393 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2395 // Confirm items positioned correctly
2396 for (int i = 0; i < 2; ++i) {
2397 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2399 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2402 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2403 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2405 // Confirm items positioned correctly
2406 for (int i = 0; i < 2; ++i) {
2407 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2409 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2412 QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2413 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2415 // Confirm items positioned correctly
2416 for (int i = 0; i < 2; ++i) {
2417 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2419 QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2422 QMetaObject::invokeMethod(canvas->rootObject(), "switchGrouped");
2423 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2425 // Confirm items positioned correctly
2426 for (int i = 0; i < 2; ++i) {
2427 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2429 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2435 void tst_QQuickListView::sectionDelegateChange()
2437 QQuickView *canvas = createView();
2439 canvas->setSource(testFileUrl("sectiondelegatechange.qml"));
2441 qApp->processEvents();
2443 QQuickListView *listview = qobject_cast<QQuickListView *>(canvas->rootObject());
2444 QVERIFY(listview != 0);
2446 QQuickItem *contentItem = listview->contentItem();
2447 QVERIFY(contentItem != 0);
2449 QQUICK_VERIFY_POLISH(listview);
2451 QVERIFY(findItems<QQuickItem>(contentItem, "section1").count() > 0);
2452 QCOMPARE(findItems<QQuickItem>(contentItem, "section2").count(), 0);
2454 for (int i = 0; i < 3; ++i) {
2455 QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2457 QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2460 QMetaObject::invokeMethod(canvas->rootObject(), "switchDelegates");
2461 QQUICK_VERIFY_POLISH(listview);
2463 QCOMPARE(findItems<QQuickItem>(contentItem, "section1").count(), 0);
2464 QVERIFY(findItems<QQuickItem>(contentItem, "section2").count() > 0);
2466 for (int i = 0; i < 3; ++i) {
2467 QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2469 QTRY_COMPARE(item->y(), qreal(50. + i*75.));
2475 void tst_QQuickListView::currentIndex_delayedItemCreation()
2477 QFETCH(bool, setCurrentToZero);
2479 QQuickView *canvas = getView();
2481 // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2482 // (since the currentItem will have changed and that shares the same index)
2483 canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2485 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2486 qApp->processEvents();
2488 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2489 QTRY_VERIFY(listview != 0);
2490 QQuickItem *contentItem = listview->contentItem();
2491 QTRY_VERIFY(contentItem != 0);
2493 QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2494 QCOMPARE(listview->currentIndex(), 0);
2495 QTRY_COMPARE(spy.count(), 1);
2497 releaseView(canvas);
2500 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2502 QTest::addColumn<bool>("setCurrentToZero");
2504 QTest::newRow("set to 0") << true;
2505 QTest::newRow("don't set to 0") << false;
2508 void tst_QQuickListView::currentIndex()
2511 for (int i = 0; i < 30; i++)
2512 model.addItem("Item" + QString::number(i), QString::number(i));
2514 QQuickView *canvas = new QQuickView(0);
2515 canvas->setGeometry(0,0,240,320);
2517 QQmlContext *ctxt = canvas->rootContext();
2518 ctxt->setContextProperty("testModel", &model);
2519 ctxt->setContextProperty("testWrap", QVariant(false));
2521 QString filename(testFile("listview-initCurrent.qml"));
2522 canvas->setSource(QUrl::fromLocalFile(filename));
2524 qApp->processEvents();
2526 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2527 QTRY_VERIFY(listview != 0);
2528 QQuickItem *contentItem = listview->contentItem();
2529 QTRY_VERIFY(contentItem != 0);
2530 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2532 // current item should be 20th item at startup
2533 // and current item should be in view
2534 QCOMPARE(listview->currentIndex(), 20);
2535 QCOMPARE(listview->contentY(), 100.0);
2536 QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2537 QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2539 listview->setCurrentIndex(0);
2540 QCOMPARE(listview->currentIndex(), 0);
2541 // confirm that the velocity is updated
2542 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2544 // footer should become visible if it is out of view, and then current index is set to count-1
2545 canvas->rootObject()->setProperty("showFooter", true);
2546 QTRY_VERIFY(listview->footerItem());
2547 listview->setCurrentIndex(model.count()-2);
2548 QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2549 listview->setCurrentIndex(model.count()-1);
2550 QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2551 canvas->rootObject()->setProperty("showFooter", false);
2553 // header should become visible if it is out of view, and then current index is set to 0
2554 canvas->rootObject()->setProperty("showHeader", true);
2555 QTRY_VERIFY(listview->headerItem());
2556 listview->setCurrentIndex(1);
2557 QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2558 listview->setCurrentIndex(0);
2559 QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2560 canvas->rootObject()->setProperty("showHeader", false);
2562 // turn off auto highlight
2563 listview->setHighlightFollowsCurrentItem(false);
2564 QVERIFY(listview->highlightFollowsCurrentItem() == false);
2566 QVERIFY(listview->highlightItem());
2567 qreal hlPos = listview->highlightItem()->y();
2569 listview->setCurrentIndex(4);
2570 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2572 // insert item before currentIndex
2573 listview->setCurrentIndex(28);
2574 model.insertItem(0, "Foo", "1111");
2575 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2577 // check removing highlight by setting currentIndex to -1;
2578 listview->setCurrentIndex(-1);
2580 QCOMPARE(listview->currentIndex(), -1);
2581 QVERIFY(!listview->highlightItem());
2582 QVERIFY(!listview->currentItem());
2584 // moving currentItem out of view should make it invisible
2585 listview->setCurrentIndex(0);
2586 QTRY_VERIFY(delegateVisible(listview->currentItem()));
2587 listview->setContentY(200);
2588 QTRY_VERIFY(!delegateVisible(listview->currentItem()));
2593 void tst_QQuickListView::noCurrentIndex()
2596 for (int i = 0; i < 30; i++)
2597 model.addItem("Item" + QString::number(i), QString::number(i));
2599 QQuickView *canvas = new QQuickView(0);
2600 canvas->setGeometry(0,0,240,320);
2602 QQmlContext *ctxt = canvas->rootContext();
2603 ctxt->setContextProperty("testModel", &model);
2605 QString filename(testFile("listview-noCurrent.qml"));
2606 canvas->setSource(QUrl::fromLocalFile(filename));
2608 qApp->processEvents();
2610 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2611 QTRY_VERIFY(listview != 0);
2612 QQuickItem *contentItem = listview->contentItem();
2613 QTRY_VERIFY(contentItem != 0);
2614 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2616 // current index should be -1 at startup
2617 // and we should not have a currentItem or highlightItem
2618 QCOMPARE(listview->currentIndex(), -1);
2619 QCOMPARE(listview->contentY(), 0.0);
2620 QVERIFY(!listview->highlightItem());
2621 QVERIFY(!listview->currentItem());
2623 listview->setCurrentIndex(2);
2624 QCOMPARE(listview->currentIndex(), 2);
2625 QVERIFY(listview->highlightItem());
2626 QVERIFY(listview->currentItem());
2631 void tst_QQuickListView::keyNavigation()
2633 QFETCH(QQuickListView::Orientation, orientation);
2634 QFETCH(Qt::LayoutDirection, layoutDirection);
2635 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
2636 QFETCH(Qt::Key, forwardsKey);
2637 QFETCH(Qt::Key, backwardsKey);
2638 QFETCH(QPointF, contentPosAtFirstItem);
2639 QFETCH(QPointF, contentPosAtLastItem);
2642 for (int i = 0; i < 30; i++)
2643 model.addItem("Item" + QString::number(i), "");
2645 QQuickView *canvas = getView();
2646 TestObject *testObject = new TestObject;
2647 canvas->rootContext()->setContextProperty("testModel", &model);
2648 canvas->rootContext()->setContextProperty("testObject", testObject);
2649 canvas->setSource(testFileUrl("listviewtest.qml"));
2651 qApp->processEvents();
2653 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2654 QTRY_VERIFY(listview != 0);
2656 listview->setOrientation(orientation);
2657 listview->setLayoutDirection(layoutDirection);
2658 listview->setVerticalLayoutDirection(verticalLayoutDirection);
2659 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2661 canvas->requestActivateWindow();
2662 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2663 QCOMPARE(listview->currentIndex(), 0);
2665 QTest::keyClick(canvas, forwardsKey);
2666 QCOMPARE(listview->currentIndex(), 1);
2668 QTest::keyClick(canvas, backwardsKey);
2669 QCOMPARE(listview->currentIndex(), 0);
2671 // hold down a key to go forwards
2672 for (int i=0; i<model.count()-1; i++) {
2673 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
2674 QTRY_COMPARE(listview->currentIndex(), i+1);
2676 QTest::keyRelease(canvas, forwardsKey);
2677 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2678 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2679 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2681 // hold down a key to go backwards
2682 for (int i=model.count()-1; i > 0; i--) {
2683 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2684 QTRY_COMPARE(listview->currentIndex(), i-1);
2686 QTest::keyRelease(canvas, backwardsKey);
2687 QTRY_COMPARE(listview->currentIndex(), 0);
2688 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2689 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2692 QVERIFY(!listview->isWrapEnabled());
2693 listview->incrementCurrentIndex();
2694 QCOMPARE(listview->currentIndex(), 1);
2695 listview->decrementCurrentIndex();
2696 QCOMPARE(listview->currentIndex(), 0);
2698 listview->decrementCurrentIndex();
2699 QCOMPARE(listview->currentIndex(), 0);
2702 listview->setWrapEnabled(true);
2703 QVERIFY(listview->isWrapEnabled());
2705 listview->decrementCurrentIndex();
2706 QCOMPARE(listview->currentIndex(), model.count()-1);
2707 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2708 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2710 listview->incrementCurrentIndex();
2711 QCOMPARE(listview->currentIndex(), 0);
2712 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2713 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2715 releaseView(canvas);
2719 void tst_QQuickListView::keyNavigation_data()
2721 QTest::addColumn<QQuickListView::Orientation>("orientation");
2722 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2723 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2724 QTest::addColumn<Qt::Key>("forwardsKey");
2725 QTest::addColumn<Qt::Key>("backwardsKey");
2726 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2727 QTest::addColumn<QPointF>("contentPosAtLastItem");
2729 QTest::newRow("Vertical, TopToBottom")
2730 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2731 << Qt::Key_Down << Qt::Key_Up
2733 << QPointF(0, 30*20 - 320);
2735 QTest::newRow("Vertical, BottomToTop")
2736 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2737 << Qt::Key_Up << Qt::Key_Down
2739 << QPointF(0, -(30 * 20));
2741 QTest::newRow("Horizontal, LeftToRight")
2742 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2743 << Qt::Key_Right << Qt::Key_Left
2745 << QPointF(30*240 - 240, 0);
2747 QTest::newRow("Horizontal, RightToLeft")
2748 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2749 << Qt::Key_Left << Qt::Key_Right
2751 << QPointF(-(30 * 240), 0);
2754 void tst_QQuickListView::itemList()
2756 QQuickView *canvas = createView();
2757 canvas->setSource(testFileUrl("itemlist.qml"));
2759 qApp->processEvents();
2761 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2762 QTRY_VERIFY(listview != 0);
2764 QQuickItem *contentItem = listview->contentItem();
2765 QTRY_VERIFY(contentItem != 0);
2767 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2768 QTRY_VERIFY(model != 0);
2770 QTRY_VERIFY(model->count() == 3);
2771 QTRY_COMPARE(listview->currentIndex(), 0);
2773 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2775 QTRY_COMPARE(item->x(), 0.0);
2776 QCOMPARE(item->height(), listview->height());
2778 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2780 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2782 listview->setCurrentIndex(2);
2784 item = findItem<QQuickItem>(contentItem, "item3");
2786 QTRY_COMPARE(item->x(), 480.0);
2788 text = findItem<QQuickText>(contentItem, "text3");
2790 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2795 void tst_QQuickListView::itemListFlicker()
2797 QQuickView *canvas = createView();
2798 canvas->setSource(testFileUrl("itemlist-flicker.qml"));
2800 qApp->processEvents();
2802 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2803 QTRY_VERIFY(listview != 0);
2805 QQuickItem *contentItem = listview->contentItem();
2806 QTRY_VERIFY(contentItem != 0);
2808 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2809 QTRY_VERIFY(model != 0);
2811 QTRY_VERIFY(model->count() == 3);
2812 QTRY_COMPARE(listview->currentIndex(), 0);
2816 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2817 QVERIFY(delegateVisible(item));
2818 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2819 QVERIFY(delegateVisible(item));
2820 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2821 QVERIFY(delegateVisible(item));
2823 listview->setCurrentIndex(1);
2825 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2826 QVERIFY(delegateVisible(item));
2827 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2828 QVERIFY(delegateVisible(item));
2829 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2830 QVERIFY(delegateVisible(item));
2832 listview->setCurrentIndex(2);
2834 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2835 QVERIFY(delegateVisible(item));
2836 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2837 QVERIFY(delegateVisible(item));
2838 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2839 QVERIFY(delegateVisible(item));
2844 void tst_QQuickListView::cacheBuffer()
2846 QQuickView *canvas = createView();
2849 for (int i = 0; i < 90; i++)
2850 model.addItem("Item" + QString::number(i), "");
2852 QQmlContext *ctxt = canvas->rootContext();
2853 ctxt->setContextProperty("testModel", &model);
2855 TestObject *testObject = new TestObject;
2856 ctxt->setContextProperty("testObject", testObject);
2858 canvas->setSource(testFileUrl("listviewtest.qml"));
2860 qApp->processEvents();
2862 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2863 QTRY_VERIFY(listview != 0);
2865 QQuickItem *contentItem = listview->contentItem();
2866 QTRY_VERIFY(contentItem != 0);
2867 QTRY_VERIFY(listview->delegate() != 0);
2868 QTRY_VERIFY(listview->model() != 0);
2869 QTRY_VERIFY(listview->highlight() != 0);
2871 // Confirm items positioned correctly
2872 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2873 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2874 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2875 if (!item) qWarning() << "Item" << i << "not found";
2877 QTRY_VERIFY(item->y() == i*20);
2880 QQmlIncubationController controller;
2881 canvas->engine()->setIncubationController(&controller);
2883 testObject->setCacheBuffer(200);
2884 QTRY_VERIFY(listview->cacheBuffer() == 200);
2886 // items will be created one at a time
2887 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2888 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2889 QQuickItem *item = 0;
2892 controller.incubateWhile(&b);
2893 item = findItem<QQuickItem>(listview, "wrapper", i);
2899 controller.incubateWhile(&b);
2902 int newItemCount = 0;
2903 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2905 // Confirm items positioned correctly
2906 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2907 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2908 if (!item) qWarning() << "Item" << i << "not found";
2910 QTRY_VERIFY(item->y() == i*20);
2913 // move view and confirm items in view are visible immediately and outside are created async
2914 listview->setContentY(300);
2916 for (int i = 15; i < 32; ++i) {
2917 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2918 if (!item) qWarning() << "Item" << i << "not found";
2920 QVERIFY(item->y() == i*20);
2923 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2925 // ensure buffered items are created
2926 for (int i = 32; i < qMin(41,model.count()); ++i) {
2927 QQuickItem *item = 0;
2929 qGuiApp->processEvents(); // allow refill to happen
2931 controller.incubateWhile(&b);
2932 item = findItem<QQuickItem>(listview, "wrapper", i);
2938 controller.incubateWhile(&b);
2945 void tst_QQuickListView::positionViewAtIndex()
2947 QQuickView *canvas = createView();
2950 for (int i = 0; i < 40; i++)
2951 model.addItem("Item" + QString::number(i), "");
2953 QQmlContext *ctxt = canvas->rootContext();
2954 ctxt->setContextProperty("testModel", &model);
2956 TestObject *testObject = new TestObject;
2957 ctxt->setContextProperty("testObject", testObject);
2959 canvas->setSource(testFileUrl("listviewtest.qml"));
2960 qApp->processEvents();
2962 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2963 QTRY_VERIFY(listview != 0);
2964 QQuickItem *contentItem = listview->contentItem();
2965 QTRY_VERIFY(contentItem != 0);
2966 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2968 // Confirm items positioned correctly
2969 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2970 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2971 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2972 if (!item) qWarning() << "Item" << i << "not found";
2974 QTRY_COMPARE(item->y(), i*20.);
2977 // Position on a currently visible item
2978 listview->positionViewAtIndex(3, QQuickListView::Beginning);
2979 QTRY_COMPARE(listview->contentY(), 60.);
2981 // Confirm items positioned correctly
2982 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2983 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2984 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2985 if (!item) qWarning() << "Item" << i << "not found";
2987 QTRY_COMPARE(item->y(), i*20.);
2990 // Position on an item beyond the visible items
2991 listview->positionViewAtIndex(22, QQuickListView::Beginning);
2992 QTRY_COMPARE(listview->contentY(), 440.);
2994 // Confirm items positioned correctly
2995 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2996 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2997 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2998 if (!item) qWarning() << "Item" << i << "not found";
3000 QTRY_COMPARE(item->y(), i*20.);
3003 // Position on an item that would leave empty space if positioned at the top
3004 listview->positionViewAtIndex(28, QQuickListView::Beginning);
3005 QTRY_COMPARE(listview->contentY(), 480.);
3007 // Confirm items positioned correctly
3008 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3009 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
3010 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3011 if (!item) qWarning() << "Item" << i << "not found";
3013 QTRY_COMPARE(item->y(), i*20.);
3016 // Position at the beginning again
3017 listview->positionViewAtIndex(0, QQuickListView::Beginning);
3018 QTRY_COMPARE(listview->contentY(), 0.);
3020 // Confirm items positioned correctly
3021 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3022 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
3023 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3024 if (!item) qWarning() << "Item" << i << "not found";
3026 QTRY_COMPARE(item->y(), i*20.);
3029 // Position at End using last index
3030 listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
3031 QTRY_COMPARE(listview->contentY(), 480.);
3033 // Confirm items positioned correctly
3034 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3035 for (int i = 24; i < model.count(); ++i) {
3036 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3037 if (!item) qWarning() << "Item" << i << "not found";
3039 QTRY_COMPARE(item->y(), i*20.);
3043 listview->positionViewAtIndex(20, QQuickListView::End);
3044 QTRY_COMPARE(listview->contentY(), 100.);
3046 // Position in Center
3047 listview->positionViewAtIndex(15, QQuickListView::Center);
3048 QTRY_COMPARE(listview->contentY(), 150.);
3050 // Ensure at least partially visible
3051 listview->positionViewAtIndex(15, QQuickListView::Visible);
3052 QTRY_COMPARE(listview->contentY(), 150.);
3054 listview->setContentY(302);
3055 listview->positionViewAtIndex(15, QQuickListView::Visible);
3056 QTRY_COMPARE(listview->contentY(), 302.);
3058 listview->setContentY(320);
3059 listview->positionViewAtIndex(15, QQuickListView::Visible);
3060 QTRY_COMPARE(listview->contentY(), 300.);
3062 listview->setContentY(85);
3063 listview->positionViewAtIndex(20, QQuickListView::Visible);
3064 QTRY_COMPARE(listview->contentY(), 85.);
3066 listview->setContentY(75);
3067 listview->positionViewAtIndex(20, QQuickListView::Visible);
3068 QTRY_COMPARE(listview->contentY(), 100.);
3070 // Ensure completely visible
3071 listview->setContentY(120);
3072 listview->positionViewAtIndex(20, QQuickListView::Contain);
3073 QTRY_COMPARE(listview->contentY(), 120.);
3075 listview->setContentY(302);
3076 listview->positionViewAtIndex(15, QQuickListView::Contain);
3077 QTRY_COMPARE(listview->contentY(), 300.);
3079 listview->setContentY(85);
3080 listview->positionViewAtIndex(20, QQuickListView::Contain);
3081 QTRY_COMPARE(listview->contentY(), 100.);
3083 // positionAtBeginnging
3084 listview->positionViewAtBeginning();
3085 QTRY_COMPARE(listview->contentY(), 0.);
3087 listview->setContentY(80);
3088 canvas->rootObject()->setProperty("showHeader", true);
3089 listview->positionViewAtBeginning();
3090 QTRY_COMPARE(listview->contentY(), -30.);
3093 listview->positionViewAtEnd();
3094 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
3096 listview->setContentY(80);
3097 canvas->rootObject()->setProperty("showFooter", true);
3098 listview->positionViewAtEnd();
3099 QTRY_COMPARE(listview->contentY(), 510.);
3101 // set current item to outside visible view, position at beginning
3102 // and ensure highlight moves to current item
3103 listview->setCurrentIndex(1);
3104 listview->positionViewAtBeginning();
3105 QTRY_COMPARE(listview->contentY(), -30.);
3106 QVERIFY(listview->highlightItem());
3107 QCOMPARE(listview->highlightItem()->y(), 20.);
3113 void tst_QQuickListView::resetModel()
3115 QQuickView *canvas = createView();
3117 QStringList strings;
3118 strings << "one" << "two" << "three";
3119 QStringListModel model(strings);
3121 QQmlContext *ctxt = canvas->rootContext();
3122 ctxt->setContextProperty("testModel", &model);
3124 canvas->setSource(testFileUrl("displaylist.qml"));
3126 qApp->processEvents();
3128 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3129 QTRY_VERIFY(listview != 0);
3130 QQuickItem *contentItem = listview->contentItem();
3131 QTRY_VERIFY(contentItem != 0);
3132 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3134 QTRY_COMPARE(listview->count(), model.rowCount());
3136 for (int i = 0; i < model.rowCount(); ++i) {
3137 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3138 QTRY_VERIFY(display != 0);
3139 QTRY_COMPARE(display->text(), strings.at(i));
3143 strings << "four" << "five" << "six" << "seven";
3144 model.setStringList(strings);
3146 QTRY_COMPARE(listview->count(), model.rowCount());
3148 for (int i = 0; i < model.rowCount(); ++i) {
3149 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3150 QTRY_VERIFY(display != 0);
3151 QTRY_COMPARE(display->text(), strings.at(i));
3157 void tst_QQuickListView::propertyChanges()
3159 QQuickView *canvas = createView();
3160 QTRY_VERIFY(canvas);
3161 canvas->setSource(testFileUrl("propertychangestest.qml"));
3163 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3164 QTRY_VERIFY(listView);
3166 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3167 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3168 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3169 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3170 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3171 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3172 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3174 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3175 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3176 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3177 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3178 QTRY_COMPARE(listView->isWrapEnabled(), true);
3179 QTRY_COMPARE(listView->cacheBuffer(), 10);
3180 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3182 listView->setHighlightFollowsCurrentItem(false);
3183 listView->setPreferredHighlightBegin(1.0);
3184 listView->setPreferredHighlightEnd(1.0);
3185 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3186 listView->setWrapEnabled(false);
3187 listView->setCacheBuffer(3);
3188 listView->setSnapMode(QQuickListView::SnapOneItem);
3190 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3191 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3192 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3193 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3194 QTRY_COMPARE(listView->isWrapEnabled(), false);
3195 QTRY_COMPARE(listView->cacheBuffer(), 3);
3196 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3198 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3199 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3200 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3201 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3202 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3203 QTRY_COMPARE(cacheBufferSpy.count(),1);
3204 QTRY_COMPARE(snapModeSpy.count(),1);
3206 listView->setHighlightFollowsCurrentItem(false);
3207 listView->setPreferredHighlightBegin(1.0);
3208 listView->setPreferredHighlightEnd(1.0);
3209 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3210 listView->setWrapEnabled(false);
3211 listView->setCacheBuffer(3);
3212 listView->setSnapMode(QQuickListView::SnapOneItem);
3214 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3215 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3216 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3217 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3218 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3219 QTRY_COMPARE(cacheBufferSpy.count(),1);
3220 QTRY_COMPARE(snapModeSpy.count(),1);
3225 void tst_QQuickListView::componentChanges()
3227 QQuickView *canvas = createView();
3228 QTRY_VERIFY(canvas);
3229 canvas->setSource(testFileUrl("propertychangestest.qml"));
3231 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3232 QTRY_VERIFY(listView);
3234 QQmlComponent component(canvas->engine());
3235 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3237 QQmlComponent delegateComponent(canvas->engine());
3238 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3240 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3241 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3242 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3243 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3245 listView->setHighlight(&component);
3246 listView->setHeader(&component);
3247 listView->setFooter(&component);
3248 listView->setDelegate(&delegateComponent);
3250 QTRY_COMPARE(listView->highlight(), &component);
3251 QTRY_COMPARE(listView->header(), &component);
3252 QTRY_COMPARE(listView->footer(), &component);
3253 QTRY_COMPARE(listView->delegate(), &delegateComponent);
3255 QTRY_COMPARE(highlightSpy.count(),1);
3256 QTRY_COMPARE(delegateSpy.count(),1);
3257 QTRY_COMPARE(headerSpy.count(),1);
3258 QTRY_COMPARE(footerSpy.count(),1);
3260 listView->setHighlight(&component);
3261 listView->setHeader(&component);
3262 listView->setFooter(&component);
3263 listView->setDelegate(&delegateComponent);
3265 QTRY_COMPARE(highlightSpy.count(),1);
3266 QTRY_COMPARE(delegateSpy.count(),1);
3267 QTRY_COMPARE(headerSpy.count(),1);
3268 QTRY_COMPARE(footerSpy.count(),1);
3273 void tst_QQuickListView::modelChanges()
3275 QQuickView *canvas = createView();
3276 QTRY_VERIFY(canvas);
3277 canvas->setSource(testFileUrl("propertychangestest.qml"));
3279 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3280 QTRY_VERIFY(listView);
3282 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
3283 QTRY_VERIFY(alternateModel);
3284 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3285 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3287 listView->setModel(modelVariant);
3288 QTRY_COMPARE(listView->model(), modelVariant);
3289 QTRY_COMPARE(modelSpy.count(),1);
3291 listView->setModel(modelVariant);
3292 QTRY_COMPARE(modelSpy.count(),1);
3294 listView->setModel(QVariant());
3295 QTRY_COMPARE(modelSpy.count(),2);
3300 void tst_QQuickListView::QTBUG_9791()
3302 QQuickView *canvas = createView();
3304 canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
3305 qApp->processEvents();
3307 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3308 QTRY_VERIFY(listview != 0);
3310 QQuickItem *contentItem = listview->contentItem();
3311 QTRY_VERIFY(contentItem != 0);
3312 QTRY_VERIFY(listview->delegate() != 0);
3313 QTRY_VERIFY(listview->model() != 0);
3315 QMetaObject::invokeMethod(listview, "fillModel");
3316 qApp->processEvents();
3318 // Confirm items positioned correctly
3319 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3320 QCOMPARE(itemCount, 3);
3322 for (int i = 0; i < itemCount; ++i) {
3323 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3324 if (!item) qWarning() << "Item" << i << "not found";
3326 QTRY_COMPARE(item->x(), i*300.0);
3329 // check that view is positioned correctly
3330 QTRY_COMPARE(listview->contentX(), 590.0);
3335 void tst_QQuickListView::manualHighlight()
3337 QQuickView *canvas = new QQuickView(0);
3338 canvas->setGeometry(0,0,240,320);
3340 QString filename(testFile("manual-highlight.qml"));
3341 canvas->setSource(QUrl::fromLocalFile(filename));
3343 qApp->processEvents();
3345 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3346 QTRY_VERIFY(listview != 0);
3348 QQuickItem *contentItem = listview->contentItem();
3349 QTRY_VERIFY(contentItem != 0);
3351 QTRY_COMPARE(listview->currentIndex(), 0);
3352 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3353 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3355 listview->setCurrentIndex(2);
3357 QTRY_COMPARE(listview->currentIndex(), 2);
3358 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3359 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3362 listview->positionViewAtIndex(3, QQuickListView::Contain);
3364 QTRY_COMPARE(listview->currentIndex(), 2);
3365 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3366 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3371 void tst_QQuickListView::QTBUG_11105()
3373 QQuickView *canvas = createView();
3376 for (int i = 0; i < 30; i++)
3377 model.addItem("Item" + QString::number(i), "");
3379 QQmlContext *ctxt = canvas->rootContext();
3380 ctxt->setContextProperty("testModel", &model);
3382 TestObject *testObject = new TestObject;
3383 ctxt->setContextProperty("testObject", testObject);
3385 canvas->setSource(testFileUrl("listviewtest.qml"));
3387 qApp->processEvents();
3389 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3390 QTRY_VERIFY(listview != 0);
3391 QQuickItem *contentItem = listview->contentItem();
3392 QTRY_VERIFY(contentItem != 0);
3393 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3395 // Confirm items positioned correctly
3396 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3397 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3398 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3399 if (!item) qWarning() << "Item" << i << "not found";
3401 QTRY_VERIFY(item->y() == i*20);
3404 listview->positionViewAtIndex(20, QQuickListView::Beginning);
3405 QCOMPARE(listview->contentY(), 280.);
3407 QmlListModel model2;
3408 for (int i = 0; i < 5; i++)
3409 model2.addItem("Item" + QString::number(i), "");
3411 ctxt->setContextProperty("testModel", &model2);
3413 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3414 QCOMPARE(itemCount, 5);
3420 void tst_QQuickListView::initialZValues()
3422 QQuickView *canvas = createView();
3423 canvas->setSource(testFileUrl("initialZValues.qml"));
3424 qApp->processEvents();
3426 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3427 QTRY_VERIFY(listview != 0);
3428 QQuickItem *contentItem = listview->contentItem();
3429 QTRY_VERIFY(contentItem != 0);
3431 QVERIFY(listview->headerItem());
3432 QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3434 QVERIFY(listview->footerItem());
3435 QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3440 void tst_QQuickListView::header()
3442 QFETCH(QQuickListView::Orientation, orientation);
3443 QFETCH(Qt::LayoutDirection, layoutDirection);
3444 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3445 QFETCH(QPointF, initialHeaderPos);
3446 QFETCH(QPointF, changedHeaderPos);
3447 QFETCH(QPointF, initialContentPos);
3448 QFETCH(QPointF, changedContentPos);
3449 QFETCH(QPointF, firstDelegatePos);
3450 QFETCH(QPointF, resizeContentPos);
3453 for (int i = 0; i < 30; i++)
3454 model.addItem("Item" + QString::number(i), "");
3456 QQuickView *canvas = getView();
3457 canvas->rootContext()->setContextProperty("testModel", &model);
3458 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3459 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3460 canvas->setSource(testFileUrl("header.qml"));
3462 qApp->processEvents();
3464 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3465 QTRY_VERIFY(listview != 0);
3466 listview->setOrientation(orientation);
3467 listview->setLayoutDirection(layoutDirection);
3468 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3469 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3471 QQuickItem *contentItem = listview->contentItem();
3472 QTRY_VERIFY(contentItem != 0);
3474 QQuickText *header = 0;
3475 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3476 QVERIFY(header == listview->headerItem());
3478 QCOMPARE(header->width(), 100.);
3479 QCOMPARE(header->height(), 30.);
3480 QCOMPARE(header->pos(), initialHeaderPos);
3481 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3483 if (orientation == QQuickListView::Vertical)
3484 QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3486 QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3488 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3490 QCOMPARE(item->pos(), firstDelegatePos);
3493 QTRY_COMPARE(listview->count(), model.count());
3494 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3495 if (orientation == QQuickListView::Vertical)
3496 QCOMPARE(listview->contentHeight(), header->height());
3498 QCOMPARE(listview->contentWidth(), header->width());
3500 for (int i = 0; i < 30; i++)
3501 model.addItem("Item" + QString::number(i), "");
3503 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3504 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3506 QCOMPARE(headerItemSpy.count(), 1);
3508 header = findItem<QQuickText>(contentItem, "header");
3510 header = findItem<QQuickText>(contentItem, "header2");
3513 QVERIFY(header == listview->headerItem());
3515 QCOMPARE(header->pos(), changedHeaderPos);
3516 QCOMPARE(header->width(), 50.);
3517 QCOMPARE(header->height(), 20.);
3518 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3520 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3522 QCOMPARE(item->pos(), firstDelegatePos);
3524 listview->positionViewAtBeginning();
3525 header->setHeight(10);
3526 header->setWidth(40);
3527 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3529 releaseView(canvas);
3532 // QTBUG-21207 header should become visible if view resizes from initial empty size
3535 canvas->rootContext()->setContextProperty("testModel", &model);
3536 canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3537 canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3538 canvas->setSource(testFileUrl("header.qml"));
3540 qApp->processEvents();
3542 listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3543 QTRY_VERIFY(listview != 0);
3544 listview->setOrientation(orientation);
3545 listview->setLayoutDirection(layoutDirection);
3546 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3547 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3549 listview->setWidth(240);
3550 listview->setHeight(320);
3551 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3552 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3554 releaseView(canvas);
3557 void tst_QQuickListView::header_data()
3559 QTest::addColumn<QQuickListView::Orientation>("orientation");
3560 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3561 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3562 QTest::addColumn<QPointF>("initialHeaderPos");
3563 QTest::addColumn<QPointF>("changedHeaderPos");
3564 QTest::addColumn<QPointF>("initialContentPos");
3565 QTest::addColumn<QPointF>("changedContentPos");
3566 QTest::addColumn<QPointF>("firstDelegatePos");
3567 QTest::addColumn<QPointF>("resizeContentPos");
3569 // header1 = 100 x 30
3570 // header2 = 50 x 20
3571 // delegates = 240 x 30
3574 // header above items, top left
3575 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3583 // header above items, top right
3584 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3592 // header to left of items
3593 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3601 // header to right of items
3602 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3605 << QPointF(-240 + 100, 0)
3606 << QPointF(-240 + 50, 0)
3608 << QPointF(-240 + 40, 0);
3610 // header below items
3611 QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3614 << QPointF(0, -320 + 30)
3615 << QPointF(0, -320 + 20)
3617 << QPointF(0, -320 + 10);
3620 void tst_QQuickListView::header_delayItemCreation()
3622 QQuickView *canvas = createView();
3626 canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3627 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3628 qApp->processEvents();
3630 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3631 QTRY_VERIFY(listview != 0);
3633 QQuickItem *contentItem = listview->contentItem();
3634 QTRY_VERIFY(contentItem != 0);
3636 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3638 QCOMPARE(header->y(), -header->height());
3640 QCOMPARE(listview->contentY(), -header->height());
3643 QTRY_COMPARE(header->y(), -header->height());
3648 void tst_QQuickListView::footer()
3650 QFETCH(QQuickListView::Orientation, orientation);
3651 QFETCH(Qt::LayoutDirection, layoutDirection);
3652 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3653 QFETCH(QPointF, initialFooterPos);
3654 QFETCH(QPointF, firstDelegatePos);
3655 QFETCH(QPointF, initialContentPos);
3656 QFETCH(QPointF, changedFooterPos);
3657 QFETCH(QPointF, changedContentPos);
3658 QFETCH(QPointF, resizeContentPos);
3660 QQuickView *canvas = getView();
3663 for (int i = 0; i < 3; i++)
3664 model.addItem("Item" + QString::number(i), "");
3666 QQmlContext *ctxt = canvas->rootContext();
3667 ctxt->setContextProperty("testModel", &model);
3669 canvas->setSource(testFileUrl("footer.qml"));
3671 qApp->processEvents();
3673 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3674 QTRY_VERIFY(listview != 0);
3675 listview->setOrientation(orientation);
3676 listview->setLayoutDirection(layoutDirection);
3677 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3678 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3680 QQuickItem *contentItem = listview->contentItem();
3681 QTRY_VERIFY(contentItem != 0);
3683 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3686 QVERIFY(footer == listview->footerItem());
3688 QCOMPARE(footer->pos(), initialFooterPos);
3689 QCOMPARE(footer->width(), 100.);
3690 QCOMPARE(footer->height(), 30.);
3691 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3693 if (orientation == QQuickListView::Vertical)
3694 QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3696 QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3698 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3700 QCOMPARE(item->pos(), firstDelegatePos);
3703 model.removeItem(1);
3705 if (orientation == QQuickListView::Vertical) {
3706 QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3707 initialFooterPos.y() - 20 : initialFooterPos.y() + 20); // delegate width = 40
3709 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3710 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3715 if (orientation == QQuickListView::Vertical)
3716 QTRY_COMPARE(listview->contentHeight(), footer->height());
3718 QTRY_COMPARE(listview->contentWidth(), footer->width());
3720 QPointF posWhenNoItems(0, 0);
3721 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3722 posWhenNoItems.setX(-100);
3723 else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3724 posWhenNoItems.setY(-30);
3725 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3727 // if header is present, it's at a negative pos, so the footer should not move
3728 canvas->rootObject()->setProperty("showHeader", true);
3729 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3730 canvas->rootObject()->setProperty("showHeader", false);
3733 for (int i = 0; i < 30; i++)
3734 model.addItem("Item" + QString::number(i), "");
3736 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3737 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3739 QCOMPARE(footerItemSpy.count(), 1);
3741 footer = findItem<QQuickText>(contentItem, "footer");
3743 footer = findItem<QQuickText>(contentItem, "footer2");
3746 QVERIFY(footer == listview->footerItem());
3748 QCOMPARE(footer->pos(), changedFooterPos);
3749 QCOMPARE(footer->width(), 50.);
3750 QCOMPARE(footer->height(), 20.);
3751 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3753 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3755 QCOMPARE(item->pos(), firstDelegatePos);
3757 listview->positionViewAtEnd();
3758 footer->setHeight(10);
3759 footer->setWidth(40);
3760 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3762 releaseView(canvas);
3765 void tst_QQuickListView::footer_data()
3767 QTest::addColumn<QQuickListView::Orientation>("orientation");
3768 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3769 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3770 QTest::addColumn<QPointF>("initialFooterPos");
3771 QTest::addColumn<QPointF>("changedFooterPos");
3772 QTest::addColumn<QPointF>("initialContentPos");
3773 QTest::addColumn<QPointF>("changedContentPos");
3774 QTest::addColumn<QPointF>("firstDelegatePos");
3775 QTest::addColumn<QPointF>("resizeContentPos");
3777 // footer1 = 100 x 30
3778 // footer2 = 50 x 20
3779 // delegates = 40 x 20
3781 // view height = 320
3783 // footer below items, bottom left
3784 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3785 << QPointF(0, 3 * 20)
3786 << QPointF(0, 30 * 20) // added 30 items
3790 << QPointF(0, 30 * 20 - 320 + 10);
3792 // footer below items, bottom right
3793 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3794 << QPointF(0, 3 * 20)
3795 << QPointF(0, 30 * 20)
3799 << QPointF(0, 30 * 20 - 320 + 10);
3801 // footer to right of items
3802 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3803 << QPointF(40 * 3, 0)
3804 << QPointF(40 * 30, 0)
3808 << QPointF(40 * 30 - 240 + 40, 0);
3810 // footer to left of items
3811 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3812 << QPointF(-(40 * 3) - 100, 0)
3813 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3817 << QPointF(-(40 * 30) - 40, 0);
3819 // footer above items
3820 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3821 << QPointF(0, -(3 * 20) - 30)
3822 << QPointF(0, -(30 * 20) - 20)
3826 << QPointF(0, -(30 * 20) - 10);
3829 class LVAccessor : public QQuickListView
3832 qreal minY() const { return minYExtent(); }
3833 qreal maxY() const { return maxYExtent(); }
3834 qreal minX() const { return minXExtent(); }
3835 qreal maxX() const { return maxXExtent(); }
3839 void tst_QQuickListView::extents()
3841 QFETCH(QQuickListView::Orientation, orientation);
3842 QFETCH(Qt::LayoutDirection, layoutDirection);
3843 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3844 QFETCH(QPointF, headerPos);
3845 QFETCH(QPointF, footerPos);
3846 QFETCH(QPointF, minPos);
3847 QFETCH(QPointF, maxPos);
3848 QFETCH(QPointF, origin_empty);
3849 QFETCH(QPointF, origin_nonEmpty);
3851 QQuickView *canvas = getView();
3854 QQmlContext *ctxt = canvas->rootContext();
3855 ctxt->setContextProperty("testModel", &model);
3856 canvas->setSource(testFileUrl("headerfooter.qml"));
3858 qApp->processEvents();
3860 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3861 QTRY_VERIFY(listview != 0);
3862 listview->setOrientation(orientation);
3863 listview->setLayoutDirection(layoutDirection);
3864 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3865 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3867 QQuickItem *contentItem = listview->contentItem();
3868 QTRY_VERIFY(contentItem != 0);
3870 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3872 QCOMPARE(header->pos(), headerPos);
3874 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3876 QCOMPARE(footer->pos(), footerPos);
3878 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3879 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3880 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3881 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3883 QCOMPARE(listview->originX(), origin_empty.x());
3884 QCOMPARE(listview->originY(), origin_empty.y());
3885 for (int i=0; i<30; i++)
3886 model.addItem("Item" + QString::number(i), "");
3887 QTRY_COMPARE(listview->count(), model.count());
3888 QCOMPARE(listview->originX(), origin_nonEmpty.x());
3889 QCOMPARE(listview->originY(), origin_nonEmpty.y());
3891 releaseView(canvas);
3894 void tst_QQuickListView::extents_data()
3896 QTest::addColumn<QQuickListView::Orientation>("orientation");
3897 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3898 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3899 QTest::addColumn<QPointF>("headerPos");
3900 QTest::addColumn<QPointF>("footerPos");
3901 QTest::addColumn<QPointF>("minPos");
3902 QTest::addColumn<QPointF>("maxPos");
3903 QTest::addColumn<QPointF>("origin_empty");
3904 QTest::addColumn<QPointF>("origin_nonEmpty");
3906 // header is 240x20 (or 20x320 in Horizontal orientation)
3907 // footer is 240x30 (or 30x320 in Horizontal orientation)
3909 QTest::newRow("Vertical, TopToBottom")
3910 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3911 << QPointF(0, -20) << QPointF(0, 0)
3912 << QPointF(0, 20) << QPointF(240, 20)
3913 << QPointF(0, -20) << QPointF(0, -20);
3915 QTest::newRow("Vertical, BottomToTop")
3916 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3917 << QPointF(0, 0) << QPointF(0, -30)
3918 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3919 << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3921 QTest::newRow("Horizontal, LeftToRight")
3922 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3923 << QPointF(-20, 0) << QPointF(0, 0)
3924 << QPointF(20, 0) << QPointF(20, 320)
3925 << QPointF(-20, 0) << QPointF(-20, 0);
3927 QTest::newRow("Horizontal, RightToLeft")
3928 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3929 << QPointF(0, 0) << QPointF(-30, 0)
3930 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3931 << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3934 void tst_QQuickListView::resetModel_headerFooter()
3936 // Resetting a model shouldn't crash in views with header/footer
3938 QQuickView *canvas = createView();
3941 for (int i = 0; i < 4; i++)
3942 model.addItem("Item" + QString::number(i), "");
3943 QQmlContext *ctxt = canvas->rootContext();
3944 ctxt->setContextProperty("testModel", &model);
3946 canvas->setSource(testFileUrl("headerfooter.qml"));
3947 qApp->processEvents();
3949 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3950 QTRY_VERIFY(listview != 0);
3952 QQuickItem *contentItem = listview->contentItem();
3953 QTRY_VERIFY(contentItem != 0);
3955 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3957 QCOMPARE(header->y(), -header->height());
3959 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3961 QCOMPARE(footer->y(), 30.*4);
3965 header = findItem<QQuickItem>(contentItem, "header");
3967 QCOMPARE(header->y(), -header->height());
3969 footer = findItem<QQuickItem>(contentItem, "footer");
3971 QCOMPARE(footer->y(), 30.*4);
3976 void tst_QQuickListView::resizeView()
3978 QQuickView *canvas = createView();
3981 for (int i = 0; i < 40; i++)
3982 model.addItem("Item" + QString::number(i), "");
3984 QQmlContext *ctxt = canvas->rootContext();
3985 ctxt->setContextProperty("testModel", &model);
3987 TestObject *testObject = new TestObject;
3988 ctxt->setContextProperty("testObject", testObject);
3990 canvas->setSource(testFileUrl("listviewtest.qml"));
3992 qApp->processEvents();
3994 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3995 QTRY_VERIFY(listview != 0);
3996 QQuickItem *contentItem = listview->contentItem();
3997 QTRY_VERIFY(contentItem != 0);
3998 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4000 // Confirm items positioned correctly
4001 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4002 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4003 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4004 if (!item) qWarning() << "Item" << i << "not found";
4006 QTRY_COMPARE(item->y(), i*20.);
4009 QVariant heightRatio;
4010 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4011 QCOMPARE(heightRatio.toReal(), 0.4);
4013 listview->setHeight(200);
4014 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4016 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4017 QCOMPARE(heightRatio.toReal(), 0.25);
4019 // Ensure we handle -ve sizes
4020 listview->setHeight(-100);
4021 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
4023 listview->setCacheBuffer(200);
4024 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
4026 // ensure items in cache become visible
4027 listview->setHeight(200);
4028 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
4030 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4031 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4032 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4033 if (!item) qWarning() << "Item" << i << "not found";
4035 QTRY_COMPARE(item->y(), i*20.);
4036 QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
4039 // ensure items outside view become invisible
4040 listview->setHeight(100);
4041 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
4043 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4044 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4045 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4046 if (!item) qWarning() << "Item" << i << "not found";
4048 QTRY_COMPARE(item->y(), i*20.);
4049 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
4056 void tst_QQuickListView::resizeViewAndRepaint()
4058 QQuickView *canvas = createView();
4061 for (int i = 0; i < 40; i++)
4062 model.addItem("Item" + QString::number(i), "");
4064 QQmlContext *ctxt = canvas->rootContext();
4065 ctxt->setContextProperty("testModel", &model);
4066 ctxt->setContextProperty("initialHeight", 100);
4068 canvas->setSource(testFileUrl("resizeview.qml"));
4070 qApp->processEvents();
4072 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4073 QTRY_VERIFY(listview != 0);
4074 QQuickItem *contentItem = listview->contentItem();
4075 QTRY_VERIFY(contentItem != 0);
4076 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4078 // item at index 10 should not be currently visible
4079 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4081 listview->setHeight(320);
4083 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
4085 listview->setHeight(100);
4086 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4091 void tst_QQuickListView::sizeLessThan1()
4093 QQuickView *canvas = createView();
4096 for (int i = 0; i < 30; i++)
4097 model.addItem("Item" + QString::number(i), "");
4099 QQmlContext *ctxt = canvas->rootContext();
4100 ctxt->setContextProperty("testModel", &model);
4102 TestObject *testObject = new TestObject;
4103 ctxt->setContextProperty("testObject", testObject);
4105 canvas->setSource(testFileUrl("sizelessthan1.qml"));
4107 qApp->processEvents();
4109 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4110 QTRY_VERIFY(listview != 0);
4111 QQuickItem *contentItem = listview->contentItem();
4112 QTRY_VERIFY(contentItem != 0);
4113 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4115 // Confirm items positioned correctly
4116 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4117 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4118 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4119 if (!item) qWarning() << "Item" << i << "not found";
4121 QTRY_COMPARE(item->y(), i*0.5);
4128 void tst_QQuickListView::QTBUG_14821()
4130 QQuickView *canvas = createView();
4132 canvas->setSource(testFileUrl("qtbug14821.qml"));
4133 qApp->processEvents();
4135 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4136 QVERIFY(listview != 0);
4138 QQuickItem *contentItem = listview->contentItem();
4139 QVERIFY(contentItem != 0);
4141 listview->decrementCurrentIndex();
4142 QCOMPARE(listview->currentIndex(), 99);
4144 listview->incrementCurrentIndex();
4145 QCOMPARE(listview->currentIndex(), 0);
4150 void tst_QQuickListView::resizeDelegate()
4152 QQuickView *canvas = createView();
4154 QStringList strings;
4155 for (int i = 0; i < 30; ++i)
4156 strings << QString::number(i);
4157 QStringListModel model(strings);
4159 QQmlContext *ctxt = canvas->rootContext();
4160 ctxt->setContextProperty("testModel", &model);
4162 canvas->setSource(testFileUrl("displaylist.qml"));
4164 qApp->processEvents();
4166 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4167 QVERIFY(listview != 0);
4168 QQuickItem *contentItem = listview->contentItem();
4169 QVERIFY(contentItem != 0);
4170 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4172 QCOMPARE(listview->count(), model.rowCount());
4174 listview->setCurrentIndex(25);
4175 listview->setContentY(0);
4176 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4178 for (int i = 0; i < 16; ++i) {
4179 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4181 QCOMPARE(item->y(), i*20.0);
4184 QCOMPARE(listview->currentItem()->y(), 500.0);
4185 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4187 canvas->rootObject()->setProperty("delegateHeight", 30);
4188 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4190 for (int i = 0; i < 11; ++i) {
4191 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4193 QTRY_COMPARE(item->y(), i*30.0);
4196 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4197 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4199 listview->setCurrentIndex(1);
4200 listview->positionViewAtIndex(25, QQuickListView::Beginning);
4201 listview->positionViewAtIndex(5, QQuickListView::Beginning);
4202 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4204 for (int i = 5; i < 16; ++i) {
4205 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4207 QCOMPARE(item->y(), i*30.0);
4210 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4211 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4213 canvas->rootObject()->setProperty("delegateHeight", 20);
4214 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4216 for (int i = 5; i < 11; ++i) {
4217 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4219 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4222 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4223 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4228 void tst_QQuickListView::resizeFirstDelegate()
4230 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4231 // and other delegates have height > 0
4233 QQuickView *canvas = createView();
4235 // bug only occurs when all items in the model are visible
4237 for (int i = 0; i < 10; i++)
4238 model.addItem("Item" + QString::number(i), "");
4240 QQmlContext *ctxt = canvas->rootContext();
4241 ctxt->setContextProperty("testModel", &model);
4243 TestObject *testObject = new TestObject;
4244 ctxt->setContextProperty("testObject", testObject);
4246 canvas->setSource(testFileUrl("listviewtest.qml"));
4248 qApp->processEvents();
4250 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4251 QVERIFY(listview != 0);
4252 QQuickItem *contentItem = listview->contentItem();
4253 QVERIFY(contentItem != 0);
4254 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4256 QQuickItem *item = 0;
4257 for (int i = 0; i < model.count(); ++i) {
4258 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4260 QCOMPARE(item->y(), i*20.0);
4263 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4266 // check the content y has not jumped up and down
4267 QCOMPARE(listview->contentY(), 0.0);
4268 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4270 QCOMPARE(spy.count(), 0);
4272 for (int i = 1; i < model.count(); ++i) {
4273 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4275 QTRY_COMPARE(item->y(), (i-1)*20.0);
4279 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4280 // list if they follow a zero-sized delegate
4282 for (int i = 0; i < 10; i++)
4283 model.addItem("Item" + QString::number(i), "");
4284 QTRY_COMPARE(listview->count(), model.count());
4286 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4290 listview->setCurrentIndex(19);
4291 qApp->processEvents();
4292 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4294 // items 0-2 should have been deleted
4295 for (int i=0; i<3; i++) {
4296 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4303 void tst_QQuickListView::repositionResizedDelegate()
4305 QFETCH(QQuickListView::Orientation, orientation);
4306 QFETCH(Qt::LayoutDirection, layoutDirection);
4307 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4308 QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4309 QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4310 QFETCH(QRectF, origPositionerRect);
4311 QFETCH(QRectF, resizedPositionerRect);
4313 QQuickView *canvas = getView();
4314 QQmlContext *ctxt = canvas->rootContext();
4315 ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4316 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4317 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4318 canvas->setSource(testFileUrl("repositionResizedDelegate.qml"));
4320 qApp->processEvents();
4322 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4323 QTRY_VERIFY(listview != 0);
4324 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4326 QQuickItem *positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4327 QVERIFY(positioner);
4328 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4329 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4330 QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4331 int prevSpyCount = 0;
4333 // When an item is resized while it is partially visible, it should resize in the
4334 // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4335 // the item should also be re-positioned so its end position stays the same.
4337 listview->setContentX(contentPos_itemFirstHalfVisible.x());
4338 listview->setContentY(contentPos_itemFirstHalfVisible.y());
4339 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4340 prevSpyCount = spy.count();
4341 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4342 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4343 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4344 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4345 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4346 QCOMPARE(spy.count(), prevSpyCount);
4348 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "decrementRepeater"));
4349 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4350 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4351 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4352 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4354 listview->setContentX(contentPos_itemSecondHalfVisible.x());
4355 listview->setContentY(contentPos_itemSecondHalfVisible.y());
4356 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4357 prevSpyCount = spy.count();
4359 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4360 positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4361 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4362 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4363 QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4364 QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4365 qApp->processEvents();
4366 QCOMPARE(spy.count(), prevSpyCount);
4368 releaseView(canvas);
4371 void tst_QQuickListView::repositionResizedDelegate_data()
4373 QTest::addColumn<QQuickListView::Orientation>("orientation");
4374 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4375 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4376 QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4377 QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4378 QTest::addColumn<QRectF>("origPositionerRect");
4379 QTest::addColumn<QRectF>("resizedPositionerRect");
4381 QTest::newRow("vertical")
4382 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4383 << QPointF(0, 60) << QPointF(0, 200 + 60)
4384 << QRectF(0, 200, 120, 120)
4385 << QRectF(0, 200, 120, 120 * 2);
4387 QTest::newRow("vertical, BottomToTop")
4388 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4389 << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4390 << QRectF(0, -200 - 120, 120, 120)
4391 << QRectF(0, -200 - 120*2, 120, 120 * 2);
4393 QTest::newRow("horizontal")
4394 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4395 << QPointF(60, 0) << QPointF(260, 0)
4396 << QRectF(200, 0, 120, 120)
4397 << QRectF(200, 0, 120 * 2, 120);
4399 QTest::newRow("horizontal, rtl")
4400 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4401 << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4402 << QRectF(-200 - 120, 0, 120, 120)
4403 << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4406 void tst_QQuickListView::QTBUG_16037()
4408 QQuickView *canvas = createView();
4411 canvas->setSource(testFileUrl("qtbug16037.qml"));
4412 qApp->processEvents();
4414 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4415 QTRY_VERIFY(listview != 0);
4417 QVERIFY(listview->contentHeight() <= 0.0);
4419 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
4421 QTRY_COMPARE(listview->contentHeight(), 80.0);
4426 void tst_QQuickListView::indexAt_itemAt_data()
4428 QTest::addColumn<qreal>("x");
4429 QTest::addColumn<qreal>("y");
4430 QTest::addColumn<int>("index");
4432 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4433 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4434 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4435 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4436 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4439 void tst_QQuickListView::indexAt_itemAt()
4445 QQuickView *canvas = getView();
4448 for (int i = 0; i < 30; i++)
4449 model.addItem("Item" + QString::number(i), "");
4451 QQmlContext *ctxt = canvas->rootContext();
4452 ctxt->setContextProperty("testModel", &model);
4454 TestObject *testObject = new TestObject;
4455 ctxt->setContextProperty("testObject", testObject);
4457 canvas->setSource(testFileUrl("listviewtest.qml"));
4459 qApp->processEvents();
4461 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4462 QTRY_VERIFY(listview != 0);
4464 QQuickItem *contentItem = listview->contentItem();
4465 QTRY_VERIFY(contentItem != 0);
4466 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4468 QQuickItem *item = 0;
4470 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4473 QCOMPARE(listview->indexAt(x,y), index);
4474 QVERIFY(listview->itemAt(x,y) == item);
4476 releaseView(canvas);
4480 void tst_QQuickListView::incrementalModel()
4482 QQuickView *canvas = createView();
4484 IncrementalModel model;
4485 QQmlContext *ctxt = canvas->rootContext();
4486 ctxt->setContextProperty("testModel", &model);
4488 canvas->setSource(testFileUrl("displaylist.qml"));
4489 qApp->processEvents();
4491 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4492 QTRY_VERIFY(listview != 0);
4494 QQuickItem *contentItem = listview->contentItem();
4495 QTRY_VERIFY(contentItem != 0);
4497 QTRY_COMPARE(listview->count(), 20);
4499 listview->positionViewAtIndex(10, QQuickListView::Beginning);
4501 QTRY_COMPARE(listview->count(), 25);
4506 void tst_QQuickListView::onAdd()
4508 QFETCH(int, initialItemCount);
4509 QFETCH(int, itemsToAdd);
4511 const int delegateHeight = 10;
4514 // these initial items should not trigger ListView.onAdd
4515 for (int i=0; i<initialItemCount; i++)
4516 model.addItem("dummy value", "dummy value");
4518 QQuickView *canvas = createView();
4519 canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4520 QQmlContext *ctxt = canvas->rootContext();
4521 ctxt->setContextProperty("testModel", &model);
4522 ctxt->setContextProperty("delegateHeight", delegateHeight);
4523 canvas->setSource(testFileUrl("attachedSignals.qml"));
4525 QObject *object = canvas->rootObject();
4526 object->setProperty("width", canvas->width());
4527 object->setProperty("height", canvas->height());
4528 qApp->processEvents();
4530 QList<QPair<QString, QString> > items;
4531 for (int i=0; i<itemsToAdd; i++)
4532 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4533 model.addItems(items);
4534 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4536 QVariantList result = object->property("addedDelegates").toList();
4537 QCOMPARE(result.count(), items.count());
4538 for (int i=0; i<items.count(); i++)
4539 QCOMPARE(result[i].toString(), items[i].first);
4544 void tst_QQuickListView::onAdd_data()
4546 QTest::addColumn<int>("initialItemCount");
4547 QTest::addColumn<int>("itemsToAdd");
4549 QTest::newRow("0, add 1") << 0 << 1;
4550 QTest::newRow("0, add 2") << 0 << 2;
4551 QTest::newRow("0, add 10") << 0 << 10;
4553 QTest::newRow("1, add 1") << 1 << 1;
4554 QTest::newRow("1, add 2") << 1 << 2;
4555 QTest::newRow("1, add 10") << 1 << 10;
4557 QTest::newRow("5, add 1") << 5 << 1;
4558 QTest::newRow("5, add 2") << 5 << 2;
4559 QTest::newRow("5, add 10") << 5 << 10;
4562 void tst_QQuickListView::onRemove()
4564 QFETCH(int, initialItemCount);
4565 QFETCH(int, indexToRemove);
4566 QFETCH(int, removeCount);
4568 const int delegateHeight = 10;
4570 for (int i=0; i<initialItemCount; i++)
4571 model.addItem(QString("value %1").arg(i), "dummy value");
4573 QQuickView *canvas = getView();
4574 QQmlContext *ctxt = canvas->rootContext();
4575 ctxt->setContextProperty("testModel", &model);
4576 ctxt->setContextProperty("delegateHeight", delegateHeight);
4577 canvas->setSource(testFileUrl("attachedSignals.qml"));
4579 QObject *object = canvas->rootObject();
4581 model.removeItems(indexToRemove, removeCount);
4582 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4584 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4586 releaseView(canvas);
4589 void tst_QQuickListView::onRemove_data()
4591 QTest::addColumn<int>("initialItemCount");
4592 QTest::addColumn<int>("indexToRemove");
4593 QTest::addColumn<int>("removeCount");
4595 QTest::newRow("remove first") << 1 << 0 << 1;
4596 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4597 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4598 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4600 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4601 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4602 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4603 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4604 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4605 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4607 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4608 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4609 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4612 void tst_QQuickListView::rightToLeft()
4614 QQuickView *canvas = createView();
4615 canvas->setGeometry(0,0,640,320);
4616 canvas->setSource(testFileUrl("rightToLeft.qml"));
4618 qApp->processEvents();
4620 QVERIFY(canvas->rootObject() != 0);
4621 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4622 QTRY_VERIFY(listview != 0);
4624 QQuickItem *contentItem = listview->contentItem();
4625 QTRY_VERIFY(contentItem != 0);
4627 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4629 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4630 QTRY_VERIFY(model != 0);
4632 QTRY_VERIFY(model->count() == 3);
4633 QTRY_COMPARE(listview->currentIndex(), 0);
4635 // initial position at first item, right edge aligned
4636 QCOMPARE(listview->contentX(), -640.);
4638 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4640 QTRY_COMPARE(item->x(), -100.0);
4641 QCOMPARE(item->height(), listview->height());
4643 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4645 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4647 listview->setCurrentIndex(2);
4649 item = findItem<QQuickItem>(contentItem, "item3");
4651 QTRY_COMPARE(item->x(), -540.0);
4653 text = findItem<QQuickText>(contentItem, "text3");
4655 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4657 QCOMPARE(listview->contentX(), -640.);
4659 // Ensure resizing maintains position relative to right edge
4660 qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4661 QTRY_COMPARE(listview->contentX(), -600.);
4666 void tst_QQuickListView::test_mirroring()
4668 QQuickView *canvasA = createView();
4669 canvasA->setSource(testFileUrl("rightToLeft.qml"));
4670 QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4671 QTRY_VERIFY(listviewA != 0);
4673 QQuickView *canvasB = createView();
4674 canvasB->setSource(testFileUrl("rightToLeft.qml"));
4675 QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4676 QTRY_VERIFY(listviewA != 0);
4677 qApp->processEvents();
4679 QList<QString> objectNames;
4680 objectNames << "item1" << "item2"; // << "item3"
4682 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4683 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4684 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4687 foreach (const QString objectName, objectNames)
4688 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4690 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4691 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4694 foreach (const QString objectName, objectNames)
4695 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4697 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4698 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4699 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4701 // LTR != LTR+mirror
4702 foreach (const QString objectName, objectNames)
4703 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4705 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4707 // RTL == LTR+mirror
4708 foreach (const QString objectName, objectNames)
4709 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4711 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4713 // RTL != RTL+mirror
4714 foreach (const QString objectName, objectNames)
4715 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4717 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4719 // LTR == RTL+mirror
4720 foreach (const QString objectName, objectNames)
4721 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4727 void tst_QQuickListView::margins()
4729 QQuickView *canvas = createView();
4732 for (int i = 0; i < 50; i++)
4733 model.addItem("Item" + QString::number(i), "");
4735 QQmlContext *ctxt = canvas->rootContext();
4736 ctxt->setContextProperty("testModel", &model);
4738 canvas->setSource(testFileUrl("margins.qml"));
4740 qApp->processEvents();
4742 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4743 QTRY_VERIFY(listview != 0);
4744 QQuickItem *contentItem = listview->contentItem();
4745 QTRY_VERIFY(contentItem != 0);
4746 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4748 QCOMPARE(listview->contentY(), -30.);
4749 QCOMPARE(listview->originY(), 0.);
4752 listview->positionViewAtEnd();
4753 qreal pos = listview->contentY();
4754 listview->setContentY(pos + 80);
4755 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4756 listview->returnToBounds();
4757 QTRY_COMPARE(listview->contentY(), pos + 50);
4759 // remove item before visible and check that top margin is maintained
4760 // and originY is updated
4761 listview->setContentY(100);
4762 model.removeItem(1);
4763 QTRY_COMPARE(listview->count(), model.count());
4764 listview->setContentY(-50);
4765 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4766 listview->returnToBounds();
4767 QCOMPARE(listview->originY(), 20.);
4768 QTRY_COMPARE(listview->contentY(), -10.);
4770 // reduce top margin
4771 listview->setTopMargin(20);
4772 QCOMPARE(listview->originY(), 20.);
4773 QTRY_COMPARE(listview->contentY(), 0.);
4776 listview->positionViewAtEnd();
4777 pos = listview->contentY();
4778 listview->setContentY(pos + 80);
4779 listview->returnToBounds();
4780 QTRY_COMPARE(listview->contentY(), pos + 50);
4782 // reduce bottom margin
4783 pos = listview->contentY();
4784 listview->setBottomMargin(40);
4785 QCOMPARE(listview->originY(), 20.);
4786 QTRY_COMPARE(listview->contentY(), pos-10);
4792 void tst_QQuickListView::marginsResize()
4794 QFETCH(QQuickListView::Orientation, orientation);
4795 QFETCH(Qt::LayoutDirection, layoutDirection);
4796 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4797 QFETCH(qreal, start);
4800 QPoint flickStart(20, 20);
4801 QPoint flickEnd(20, 20);
4802 if (orientation == QQuickListView::Vertical)
4803 flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4805 flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4807 QQuickView *canvas = getView();
4809 canvas->setSource(testFileUrl("margins2.qml"));
4811 qApp->processEvents();
4813 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4814 QTRY_VERIFY(listview != 0);
4816 listview->setOrientation(orientation);
4817 listview->setLayoutDirection(layoutDirection);
4818 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4819 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4821 // view is resized after componentCompleted - top margin should still be visible
4822 if (orientation == QQuickListView::Vertical)
4823 QCOMPARE(listview->contentY(), start);
4825 QCOMPARE(listview->contentX(), start);
4827 // move to last index and ensure bottom margin is visible.
4828 listview->setCurrentIndex(19);
4829 if (orientation == QQuickListView::Vertical)
4830 QTRY_COMPARE(listview->contentY(), end);
4832 QTRY_COMPARE(listview->contentX(), end);
4834 // flick past the end and check content pos still settles on correct extents
4835 flick(canvas, flickStart, flickEnd, 180);
4836 QTRY_VERIFY(listview->isMoving() == false);
4837 if (orientation == QQuickListView::Vertical)
4838 QTRY_COMPARE(listview->contentY(), end);
4840 QTRY_COMPARE(listview->contentX(), end);
4842 // back to top - top margin should be visible.
4843 listview->setCurrentIndex(0);
4844 if (orientation == QQuickListView::Vertical)
4845 QTRY_COMPARE(listview->contentY(), start);
4847 QTRY_COMPARE(listview->contentX(), start);
4849 // flick past the beginning and check content pos still settles on correct extents
4850 flick(canvas, flickEnd, flickStart, 180);
4851 QTRY_VERIFY(listview->isMoving() == false);
4852 if (orientation == QQuickListView::Vertical)
4853 QTRY_COMPARE(listview->contentY(), start);
4855 QTRY_COMPARE(listview->contentX(), start);
4857 releaseView(canvas);
4860 void tst_QQuickListView::marginsResize_data()
4862 QTest::addColumn<QQuickListView::Orientation>("orientation");
4863 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4864 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4865 QTest::addColumn<qreal>("start");
4866 QTest::addColumn<qreal>("end");
4868 // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4870 QTest::newRow("vertical")
4871 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4874 QTest::newRow("vertical, BottomToTop")
4875 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4876 << -180.0 << -1240.0;
4878 QTest::newRow("horizontal")
4879 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4882 QTest::newRow("horizontal, rtl")
4883 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4884 << -180.0 << -1240.0;
4887 void tst_QQuickListView::snapToItem_data()
4889 QTest::addColumn<QQuickListView::Orientation>("orientation");
4890 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4891 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4892 QTest::addColumn<int>("highlightRangeMode");
4893 QTest::addColumn<QPoint>("flickStart");
4894 QTest::addColumn<QPoint>("flickEnd");
4895 QTest::addColumn<qreal>("snapAlignment");
4896 QTest::addColumn<qreal>("endExtent");
4897 QTest::addColumn<qreal>("startExtent");
4899 QTest::newRow("vertical, top to bottom")
4900 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4901 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4903 QTest::newRow("vertical, bottom to top")
4904 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4905 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4907 QTest::newRow("horizontal, left to right")
4908 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4909 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4911 QTest::newRow("horizontal, right to left")
4912 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4913 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4915 QTest::newRow("vertical, top to bottom, enforce range")
4916 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4917 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4919 QTest::newRow("vertical, bottom to top, enforce range")
4920 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4921 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4923 QTest::newRow("horizontal, left to right, enforce range")
4924 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4925 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4927 QTest::newRow("horizontal, right to left, enforce range")
4928 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4929 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4932 void tst_QQuickListView::snapToItem()
4934 QFETCH(QQuickListView::Orientation, orientation);
4935 QFETCH(Qt::LayoutDirection, layoutDirection);
4936 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4937 QFETCH(int, highlightRangeMode);
4938 QFETCH(QPoint, flickStart);
4939 QFETCH(QPoint, flickEnd);
4940 QFETCH(qreal, snapAlignment);
4941 QFETCH(qreal, endExtent);
4942 QFETCH(qreal, startExtent);
4944 QQuickView *canvas = getView();
4946 canvas->setSource(testFileUrl("snapToItem.qml"));
4948 qApp->processEvents();
4950 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4951 QTRY_VERIFY(listview != 0);
4953 listview->setOrientation(orientation);
4954 listview->setLayoutDirection(layoutDirection);
4955 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4956 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4957 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4959 QQuickItem *contentItem = listview->contentItem();
4960 QTRY_VERIFY(contentItem != 0);
4962 // confirm that a flick hits an item boundary
4963 flick(canvas, flickStart, flickEnd, 180);
4964 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4965 if (orientation == QQuickListView::Vertical)
4966 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4968 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4972 flick(canvas, flickStart, flickEnd, 180);
4973 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4974 } while (orientation == QQuickListView::Vertical
4975 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4976 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4978 if (orientation == QQuickListView::Vertical)
4979 QCOMPARE(listview->contentY(), endExtent);
4981 QCOMPARE(listview->contentX(), endExtent);
4985 flick(canvas, flickEnd, flickStart, 180);
4986 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4987 } while (orientation == QQuickListView::Vertical
4988 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
4989 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4991 if (orientation == QQuickListView::Vertical)
4992 QCOMPARE(listview->contentY(), startExtent);
4994 QCOMPARE(listview->contentX(), startExtent);
4996 releaseView(canvas);
4999 void tst_QQuickListView::qListModelInterface_items()
5001 items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5004 void tst_QQuickListView::qListModelInterface_package_items()
5006 items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5009 void tst_QQuickListView::qAbstractItemModel_items()
5011 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
5014 void tst_QQuickListView::qListModelInterface_changed()
5016 changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5019 void tst_QQuickListView::qListModelInterface_package_changed()
5021 changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5024 void tst_QQuickListView::qAbstractItemModel_changed()
5026 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5029 void tst_QQuickListView::qListModelInterface_inserted()
5031 inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
5034 void tst_QQuickListView::qListModelInterface_package_inserted()
5036 inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5039 void tst_QQuickListView::qListModelInterface_inserted_more()
5041 inserted_more<QmlListModel>();
5044 void tst_QQuickListView::qListModelInterface_inserted_more_data()
5046 inserted_more_data();
5049 void tst_QQuickListView::qAbstractItemModel_inserted()
5051 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
5054 void tst_QQuickListView::qAbstractItemModel_inserted_more()
5056 inserted_more<QaimModel>();
5059 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
5061 inserted_more_data();
5064 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
5066 inserted_more<QaimModel>(QQuickItemView::BottomToTop);
5069 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
5071 inserted_more_data();
5074 void tst_QQuickListView::qListModelInterface_removed()
5076 removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
5077 removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
5080 void tst_QQuickListView::qListModelInterface_removed_more()
5082 removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
5085 void tst_QQuickListView::qListModelInterface_removed_more_data()
5087 removed_more_data();
5090 void tst_QQuickListView::qListModelInterface_package_removed()
5092 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
5093 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
5096 void tst_QQuickListView::qAbstractItemModel_removed()
5098 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5099 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5102 void tst_QQuickListView::qAbstractItemModel_removed_more()
5104 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5107 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5109 removed_more_data();
5112 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5114 removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5117 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5119 removed_more_data();
5122 void tst_QQuickListView::qListModelInterface_moved()
5124 moved<QmlListModel>(testFileUrl("listviewtest.qml"));
5127 void tst_QQuickListView::qListModelInterface_moved_data()
5132 void tst_QQuickListView::qListModelInterface_package_moved()
5134 moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5137 void tst_QQuickListView::qListModelInterface_package_moved_data()
5142 void tst_QQuickListView::qAbstractItemModel_moved()
5144 moved<QaimModel>(testFileUrl("listviewtest.qml"));
5147 void tst_QQuickListView::qAbstractItemModel_moved_data()
5152 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5154 moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5157 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5162 void tst_QQuickListView::qListModelInterface_clear()
5164 clear<QmlListModel>(testFileUrl("listviewtest.qml"));
5167 void tst_QQuickListView::qListModelInterface_package_clear()
5169 clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5172 void tst_QQuickListView::qAbstractItemModel_clear()
5174 clear<QaimModel>(testFileUrl("listviewtest.qml"));
5177 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5179 clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5182 void tst_QQuickListView::qListModelInterface_sections()
5184 sections<QmlListModel>(testFileUrl("listview-sections.qml"));
5187 void tst_QQuickListView::qListModelInterface_package_sections()
5189 sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
5192 void tst_QQuickListView::qAbstractItemModel_sections()
5194 sections<QaimModel>(testFileUrl("listview-sections.qml"));
5197 void tst_QQuickListView::creationContext()
5200 canvas.setGeometry(0,0,240,320);
5201 canvas.setSource(testFileUrl("creationContext.qml"));
5202 qApp->processEvents();
5204 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5206 QVERIFY(rootItem->property("count").toInt() > 0);
5209 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5210 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5211 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5212 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5213 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5214 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5215 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5216 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5219 void tst_QQuickListView::QTBUG_21742()
5222 canvas.setGeometry(0,0,200,200);
5223 canvas.setSource(testFileUrl("qtbug-21742.qml"));
5224 qApp->processEvents();
5226 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5228 QCOMPARE(rootItem->property("count").toInt(), 1);
5231 void tst_QQuickListView::asynchronous()
5233 QQuickView *canvas = createView();
5235 QQmlIncubationController controller;
5236 canvas->engine()->setIncubationController(&controller);
5238 canvas->setSource(testFileUrl("asyncloader.qml"));
5240 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5241 QVERIFY(rootObject);
5243 QQuickListView *listview = 0;
5246 controller.incubateWhile(&b);
5247 listview = rootObject->findChild<QQuickListView*>("view");
5250 // items will be created one at a time
5251 for (int i = 0; i < 8; ++i) {
5252 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5253 QQuickItem *item = 0;
5256 controller.incubateWhile(&b);
5257 item = findItem<QQuickItem>(listview, "wrapper", i);
5263 controller.incubateWhile(&b);
5266 // verify positioning
5267 QQuickItem *contentItem = listview->contentItem();
5268 for (int i = 0; i < 8; ++i) {
5269 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5270 QTRY_COMPARE(item->y(), i*50.0);
5276 void tst_QQuickListView::snapOneItem_data()
5278 QTest::addColumn<QQuickListView::Orientation>("orientation");
5279 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5280 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5281 QTest::addColumn<int>("highlightRangeMode");
5282 QTest::addColumn<QPoint>("flickStart");
5283 QTest::addColumn<QPoint>("flickEnd");
5284 QTest::addColumn<qreal>("snapAlignment");
5285 QTest::addColumn<qreal>("endExtent");
5286 QTest::addColumn<qreal>("startExtent");
5288 QTest::newRow("vertical, top to bottom")
5289 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5290 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5292 QTest::newRow("vertical, bottom to top")
5293 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5294 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5296 QTest::newRow("horizontal, left to right")
5297 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5298 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5300 QTest::newRow("horizontal, right to left")
5301 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5302 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5304 QTest::newRow("vertical, top to bottom, enforce range")
5305 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5306 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5308 QTest::newRow("vertical, bottom to top, enforce range")
5309 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5310 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5312 QTest::newRow("horizontal, left to right, enforce range")
5313 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5314 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5316 QTest::newRow("horizontal, right to left, enforce range")
5317 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5318 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5321 void tst_QQuickListView::snapOneItem()
5323 QFETCH(QQuickListView::Orientation, orientation);
5324 QFETCH(Qt::LayoutDirection, layoutDirection);
5325 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5326 QFETCH(int, highlightRangeMode);
5327 QFETCH(QPoint, flickStart);
5328 QFETCH(QPoint, flickEnd);
5329 QFETCH(qreal, snapAlignment);
5330 QFETCH(qreal, endExtent);
5331 QFETCH(qreal, startExtent);
5334 // This test seems to be unreliable - different test data fails on different runs
5335 QSKIP("QTBUG-24338");
5338 QQuickView *canvas = getView();
5340 canvas->setSource(testFileUrl("snapOneItem.qml"));
5342 qApp->processEvents();
5344 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5345 QTRY_VERIFY(listview != 0);
5347 listview->setOrientation(orientation);
5348 listview->setLayoutDirection(layoutDirection);
5349 listview->setVerticalLayoutDirection(verticalLayoutDirection);
5350 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5351 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5353 QQuickItem *contentItem = listview->contentItem();
5354 QTRY_VERIFY(contentItem != 0);
5356 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5358 // confirm that a flick hits the next item boundary
5359 flick(canvas, flickStart, flickEnd, 180);
5360 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5361 if (orientation == QQuickListView::Vertical)
5362 QCOMPARE(listview->contentY(), snapAlignment);
5364 QCOMPARE(listview->contentX(), snapAlignment);
5366 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5367 QCOMPARE(listview->currentIndex(), 1);
5368 QCOMPARE(currentIndexSpy.count(), 1);
5373 flick(canvas, flickStart, flickEnd, 180);
5374 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5375 } while (orientation == QQuickListView::Vertical
5376 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5377 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5379 if (orientation == QQuickListView::Vertical)
5380 QCOMPARE(listview->contentY(), endExtent);
5382 QCOMPARE(listview->contentX(), endExtent);
5384 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5385 QCOMPARE(listview->currentIndex(), 3);
5386 QCOMPARE(currentIndexSpy.count(), 3);
5391 flick(canvas, flickEnd, flickStart, 180);
5392 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5393 } while (orientation == QQuickListView::Vertical
5394 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5395 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5397 if (orientation == QQuickListView::Vertical)
5398 QCOMPARE(listview->contentY(), startExtent);
5400 QCOMPARE(listview->contentX(), startExtent);
5402 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5403 QCOMPARE(listview->currentIndex(), 0);
5404 QCOMPARE(currentIndexSpy.count(), 6);
5407 releaseView(canvas);
5410 void tst_QQuickListView::unrequestedVisibility()
5413 for (int i = 0; i < 30; i++)
5414 model.addItem("Item" + QString::number(i), QString::number(i));
5416 QQuickView *canvas = new QQuickView(0);
5417 canvas->setGeometry(0,0,240,320);
5419 QQmlContext *ctxt = canvas->rootContext();
5420 ctxt->setContextProperty("testModel", &model);
5421 ctxt->setContextProperty("testWrap", QVariant(false));
5423 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5425 qApp->processEvents();
5427 QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
5428 QTRY_VERIFY(leftview != 0);
5430 QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
5431 QTRY_VERIFY(rightview != 0);
5433 QQuickItem *leftContent = leftview->contentItem();
5434 QTRY_VERIFY(leftContent != 0);
5436 QQuickItem *rightContent = rightview->contentItem();
5437 QTRY_VERIFY(rightContent != 0);
5439 rightview->setCurrentIndex(20);
5441 QTRY_COMPARE(leftview->contentY(), 0.0);
5442 QTRY_COMPARE(rightview->contentY(), 100.0);
5446 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5447 QCOMPARE(delegateVisible(item), true);
5448 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5449 QCOMPARE(delegateVisible(item), false);
5451 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5452 QCOMPARE(delegateVisible(item), false);
5453 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5454 QCOMPARE(delegateVisible(item), true);
5456 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5457 QCOMPARE(delegateVisible(item), true);
5458 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5459 QCOMPARE(delegateVisible(item), false);
5460 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5461 QCOMPARE(delegateVisible(item), false);
5462 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5463 QCOMPARE(delegateVisible(item), true);
5465 rightview->setCurrentIndex(0);
5467 QTRY_COMPARE(leftview->contentY(), 0.0);
5468 QTRY_COMPARE(rightview->contentY(), 0.0);
5470 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5471 QCOMPARE(delegateVisible(item), true);
5472 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5473 QTRY_COMPARE(delegateVisible(item), true);
5475 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5476 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5478 leftview->setCurrentIndex(20);
5480 QTRY_COMPARE(leftview->contentY(), 100.0);
5481 QTRY_COMPARE(rightview->contentY(), 0.0);
5483 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5484 QTRY_COMPARE(delegateVisible(item), false);
5485 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5486 QCOMPARE(delegateVisible(item), true);
5488 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5489 QCOMPARE(delegateVisible(item), true);
5490 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5491 QCOMPARE(delegateVisible(item), false);
5493 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5494 QCOMPARE(delegateVisible(item), false);
5495 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5496 QCOMPARE(delegateVisible(item), true);
5497 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5498 QCOMPARE(delegateVisible(item), true);
5499 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5500 QCOMPARE(delegateVisible(item), false);
5502 model.moveItems(19, 1, 1);
5503 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5505 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5506 QCOMPARE(delegateVisible(item), false);
5507 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5508 QCOMPARE(delegateVisible(item), true);
5510 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5511 QCOMPARE(delegateVisible(item), true);
5512 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5513 QCOMPARE(delegateVisible(item), false);
5515 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5516 QCOMPARE(delegateVisible(item), false);
5517 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5518 QCOMPARE(delegateVisible(item), true);
5519 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5520 QCOMPARE(delegateVisible(item), true);
5521 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5522 QCOMPARE(delegateVisible(item), false);
5524 model.moveItems(3, 4, 1);
5525 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5527 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5528 QCOMPARE(delegateVisible(item), false);
5529 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5530 QCOMPARE(delegateVisible(item), true);
5531 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5532 QCOMPARE(delegateVisible(item), true);
5533 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5534 QCOMPARE(delegateVisible(item), false);
5536 model.moveItems(4, 3, 1);
5537 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5539 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5540 QCOMPARE(delegateVisible(item), false);
5541 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5542 QCOMPARE(delegateVisible(item), true);
5543 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5544 QCOMPARE(delegateVisible(item), true);
5545 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5546 QCOMPARE(delegateVisible(item), false);
5548 model.moveItems(16, 17, 1);
5549 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5551 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5552 QCOMPARE(delegateVisible(item), false);
5553 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5554 QCOMPARE(delegateVisible(item), true);
5555 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5556 QCOMPARE(delegateVisible(item), true);
5557 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5558 QCOMPARE(delegateVisible(item), false);
5560 model.moveItems(17, 16, 1);
5561 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5563 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5564 QCOMPARE(delegateVisible(item), false);
5565 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5566 QCOMPARE(delegateVisible(item), true);
5567 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5568 QCOMPARE(delegateVisible(item), true);
5569 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5570 QCOMPARE(delegateVisible(item), false);
5575 void tst_QQuickListView::populateTransitions()
5577 QFETCH(bool, staticallyPopulate);
5578 QFETCH(bool, dynamicallyPopulate);
5579 QFETCH(bool, usePopulateTransition);
5581 QPointF transitionFrom(-50, -50);
5582 QPointF transitionVia(100, 100);
5583 QaimModel model_transitionFrom;
5584 QaimModel model_transitionVia;
5587 if (staticallyPopulate) {
5588 for (int i = 0; i < 30; i++)
5589 model.addItem("item" + QString::number(i), "");
5592 QQuickView *canvas = getView();
5593 canvas->rootContext()->setContextProperty("testModel", &model);
5594 canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
5595 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5596 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5597 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5598 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
5599 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5600 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5601 canvas->setSource(testFileUrl("populateTransitions.qml"));
5603 QTest::qWaitForWindowShown(canvas);
5605 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5607 QQuickItem *contentItem = listview->contentItem();
5608 QVERIFY(contentItem);
5610 if (staticallyPopulate && usePopulateTransition) {
5611 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5612 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5613 } else if (dynamicallyPopulate) {
5614 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5615 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5617 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5618 QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5619 QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5622 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5623 for (int i=0; i < model.count() && i < itemCount; ++i) {
5624 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5625 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5626 QTRY_COMPARE(item->x(), 0.0);
5627 QTRY_COMPARE(item->y(), i*20.0);
5628 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5630 QTRY_COMPARE(name->text(), model.name(i));
5633 listview->setProperty("countPopulateTransitions", 0);
5634 listview->setProperty("countAddTransitions", 0);
5636 // add an item and check this is done with add transition, not populate
5637 model.insertItem(0, "another item", "");
5638 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5639 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5642 canvas->rootContext()->setContextProperty("testModel", QVariant());
5643 QTRY_COMPARE(listview->count(), 0);
5644 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5645 listview->setProperty("countPopulateTransitions", 0);
5646 listview->setProperty("countAddTransitions", 0);
5648 // set to a valid model and check populate transition is run a second time
5650 for (int i = 0; i < 30; i++)
5651 model.addItem("item" + QString::number(i), "");
5652 canvas->rootContext()->setContextProperty("testModel", &model);
5653 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5654 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5656 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5657 for (int i=0; i < model.count() && i < itemCount; ++i) {
5658 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5659 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5660 QTRY_COMPARE(item->x(), 0.0);
5661 QTRY_COMPARE(item->y(), i*20.0);
5662 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5664 QTRY_COMPARE(name->text(), model.name(i));
5667 // reset model and check populate transition is run again
5668 listview->setProperty("countPopulateTransitions", 0);
5669 listview->setProperty("countAddTransitions", 0);
5671 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5672 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5674 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5675 for (int i=0; i < model.count() && i < itemCount; ++i) {
5676 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5677 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5678 QTRY_COMPARE(item->x(), 0.0);
5679 QTRY_COMPARE(item->y(), i*20.0);
5680 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5682 QTRY_COMPARE(name->text(), model.name(i));
5685 releaseView(canvas);
5688 void tst_QQuickListView::populateTransitions_data()
5690 QTest::addColumn<bool>("staticallyPopulate");
5691 QTest::addColumn<bool>("dynamicallyPopulate");
5692 QTest::addColumn<bool>("usePopulateTransition");
5694 QTest::newRow("static") << true << false << true;
5695 QTest::newRow("static, no populate") << true << false << false;
5697 QTest::newRow("dynamic") << false << true << true;
5698 QTest::newRow("dynamic, no populate") << false << true << false;
5700 QTest::newRow("empty to start with") << false << false << true;
5701 QTest::newRow("empty to start with, no populate") << false << false << false;
5704 void tst_QQuickListView::addTransitions()
5706 QFETCH(int, initialItemCount);
5707 QFETCH(bool, shouldAnimateTargets);
5708 QFETCH(qreal, contentY);
5709 QFETCH(int, insertionIndex);
5710 QFETCH(int, insertionCount);
5711 QFETCH(ListRange, expectedDisplacedIndexes);
5713 // added items should start here
5714 QPointF targetItems_transitionFrom(-50, -50);
5716 // displaced items should pass through this point
5717 QPointF displacedItems_transitionVia(100, 100);
5720 for (int i = 0; i < initialItemCount; i++)
5721 model.addItem("Original item" + QString::number(i), "");
5722 QaimModel model_targetItems_transitionFrom;
5723 QaimModel model_displacedItems_transitionVia;
5725 QQuickView *canvas = getView();
5726 QQmlContext *ctxt = canvas->rootContext();
5727 TestObject *testObject = new TestObject;
5728 ctxt->setContextProperty("testModel", &model);
5729 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5730 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5731 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5732 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5733 ctxt->setContextProperty("testObject", testObject);
5734 canvas->setSource(testFileUrl("addTransitions.qml"));
5736 QTest::qWaitForWindowShown(canvas);
5738 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5739 QTRY_VERIFY(listview != 0);
5740 QQuickItem *contentItem = listview->contentItem();
5741 QVERIFY(contentItem != 0);
5742 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5744 if (contentY != 0) {
5745 listview->setContentY(contentY);
5746 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5749 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5751 // only target items that will become visible should be animated
5752 QList<QPair<QString, QString> > newData;
5753 QList<QPair<QString, QString> > expectedTargetData;
5754 QList<int> targetIndexes;
5755 if (shouldAnimateTargets) {
5756 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5757 newData << qMakePair(QString("New item %1").arg(i), QString(""));
5759 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
5760 expectedTargetData << newData.last();
5764 QVERIFY(expectedTargetData.count() > 0);
5768 if (!newData.isEmpty()) {
5769 model.insertItems(insertionIndex, newData);
5770 QTRY_COMPARE(model.count(), listview->count());
5773 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5775 if (shouldAnimateTargets) {
5776 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5777 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5778 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5780 // check the target and displaced items were animated
5781 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5782 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5784 // check attached properties
5785 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5786 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5787 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5788 if (expectedDisplacedIndexes.isValid()) {
5789 // adjust expectedDisplacedIndexes to their final values after the move
5790 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5791 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5792 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5793 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5797 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5798 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5801 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5802 int firstVisibleIndex = -1;
5803 int itemCount = items.count();
5804 for (int i=0; i<items.count(); i++) {
5805 if (items[i]->y() >= contentY) {
5806 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5807 firstVisibleIndex = e.evaluate().toInt();
5811 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5813 // verify all items moved to the correct final positions
5814 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5815 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5816 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5817 QTRY_COMPARE(item->y(), i*20.0);
5818 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5820 QTRY_COMPARE(name->text(), model.name(i));
5823 releaseView(canvas);
5827 void tst_QQuickListView::addTransitions_data()
5829 QTest::addColumn<int>("initialItemCount");
5830 QTest::addColumn<qreal>("contentY");
5831 QTest::addColumn<bool>("shouldAnimateTargets");
5832 QTest::addColumn<int>("insertionIndex");
5833 QTest::addColumn<int>("insertionCount");
5834 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5836 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5837 QTest::newRow("insert 1, just before start")
5838 << 30 << 20.0 << false
5839 << 0 << 1 << ListRange();
5840 QTest::newRow("insert 1, way before start")
5841 << 30 << 20.0 << false
5842 << 0 << 1 << ListRange();
5843 QTest::newRow("insert multiple, just before start")
5844 << 30 << 100.0 << false
5845 << 0 << 3 << ListRange();
5846 QTest::newRow("insert multiple, way before start")
5847 << 30 << 100.0 << false
5848 << 0 << 3 << ListRange();
5850 QTest::newRow("insert 1 at start")
5851 << 30 << 0.0 << true
5852 << 0 << 1 << ListRange(0, 15);
5853 QTest::newRow("insert multiple at start")
5854 << 30 << 0.0 << true
5855 << 0 << 3 << ListRange(0, 15);
5856 QTest::newRow("insert 1 at start, content y not 0")
5857 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5858 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5859 QTest::newRow("insert multiple at start, content y not 0")
5860 << 30 << 40.0 << true // first visible is index 2
5861 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5863 QTest::newRow("insert 1 at start, to empty list")
5865 << 0 << 1 << ListRange();
5866 QTest::newRow("insert multiple at start, to empty list")
5868 << 0 << 3 << ListRange();
5870 QTest::newRow("insert 1 at middle")
5871 << 30 << 0.0 << true
5872 << 5 << 1 << ListRange(5, 15);
5873 QTest::newRow("insert multiple at middle")
5874 << 30 << 0.0 << true
5875 << 5 << 3 << ListRange(5, 15);
5877 QTest::newRow("insert 1 at bottom")
5878 << 30 << 0.0 << true
5879 << 15 << 1 << ListRange(15, 15);
5880 QTest::newRow("insert multiple at bottom")
5881 << 30 << 0.0 << true
5882 << 15 << 3 << ListRange(15, 15);
5883 QTest::newRow("insert 1 at bottom, content y not 0")
5884 << 30 << 20.0 * 3 << true
5885 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5886 QTest::newRow("insert multiple at bottom, content y not 0")
5887 << 30 << 20.0 * 3 << true
5888 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5890 // items added after the last visible will not be animated in, since they
5891 // do not appear in the final view
5892 QTest::newRow("insert 1 after end")
5893 << 30 << 0.0 << false
5894 << 17 << 1 << ListRange();
5895 QTest::newRow("insert multiple after end")
5896 << 30 << 0.0 << false
5897 << 17 << 3 << ListRange();
5900 void tst_QQuickListView::moveTransitions()
5902 QFETCH(int, initialItemCount);
5903 QFETCH(qreal, contentY);
5904 QFETCH(qreal, itemsOffsetAfterMove);
5905 QFETCH(int, moveFrom);
5906 QFETCH(int, moveTo);
5907 QFETCH(int, moveCount);
5908 QFETCH(ListRange, expectedDisplacedIndexes);
5910 // target and displaced items should pass through these points
5911 QPointF targetItems_transitionVia(-50, 50);
5912 QPointF displacedItems_transitionVia(100, 100);
5915 for (int i = 0; i < initialItemCount; i++)
5916 model.addItem("Original item" + QString::number(i), "");
5917 QaimModel model_targetItems_transitionVia;
5918 QaimModel model_displacedItems_transitionVia;
5920 QQuickView *canvas = getView();
5921 QQmlContext *ctxt = canvas->rootContext();
5922 TestObject *testObject = new TestObject;
5923 ctxt->setContextProperty("testModel", &model);
5924 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5925 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5926 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5927 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5928 ctxt->setContextProperty("testObject", testObject);
5929 canvas->setSource(testFileUrl("moveTransitions.qml"));
5931 QTest::qWaitForWindowShown(canvas);
5933 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5934 QTRY_VERIFY(listview != 0);
5935 QQuickItem *contentItem = listview->contentItem();
5936 QVERIFY(contentItem != 0);
5939 if (contentY != 0) {
5940 listview->setContentY(contentY);
5941 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5944 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5946 // Items moving to *or* from visible positions should be animated.
5947 // Otherwise, they should not be animated.
5948 QList<QPair<QString, QString> > expectedTargetData;
5949 QList<int> targetIndexes;
5950 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5951 int toIndex = moveTo + (i - moveFrom);
5952 if (i <= (contentY + listview->height()) / 20
5953 || toIndex < (contentY + listview->height()) / 20) {
5954 expectedTargetData << qMakePair(model.name(i), model.number(i));
5958 // ViewTransition.index provides the indices that items are moving to, not from
5959 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5962 model.moveItems(moveFrom, moveTo, moveCount);
5964 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5965 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5966 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5968 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5970 // check the target and displaced items were animated
5971 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5972 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5974 // check attached properties
5975 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5976 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5977 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5978 if (expectedDisplacedIndexes.isValid()) {
5979 // adjust expectedDisplacedIndexes to their final values after the move
5980 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5981 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5982 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5983 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5986 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5987 int firstVisibleIndex = -1;
5988 for (int i=0; i<items.count(); i++) {
5989 if (items[i]->y() >= contentY) {
5990 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5991 firstVisibleIndex = e.evaluate().toInt();
5995 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5997 // verify all items moved to the correct final positions
5998 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5999 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6000 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6001 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6002 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
6003 name = findItem<QQuickText>(contentItem, "textName", i);
6005 QTRY_COMPARE(name->text(), model.name(i));
6008 releaseView(canvas);
6012 void tst_QQuickListView::moveTransitions_data()
6014 QTest::addColumn<int>("initialItemCount");
6015 QTest::addColumn<qreal>("contentY");
6016 QTest::addColumn<qreal>("itemsOffsetAfterMove");
6017 QTest::addColumn<int>("moveFrom");
6018 QTest::addColumn<int>("moveTo");
6019 QTest::addColumn<int>("moveCount");
6020 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6022 // when removing from above the visible, all items shift down depending on how many
6023 // items have been removed from above the visible
6024 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
6025 << 1 << 10 << 1 << ListRange(11, 15+4);
6026 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
6027 << 0 << 10 << 1 << ListRange(11, 15+4);
6028 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
6029 << 1 << 10 << 2 << ListRange(12, 15+4);
6030 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
6031 << 0 << 10 << 3 << ListRange(13, 15+4);
6032 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
6033 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
6034 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
6035 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
6037 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
6038 << 1 << 10 << 1 << ListRange(2, 10);
6039 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
6040 << 0 << 10 << 1 << ListRange(1, 10);
6041 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6042 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
6043 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
6044 << 10 << 15 << 1 << ListRange(11, 15);
6045 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
6046 << 0 << 15 << 1 << ListRange(1, 15);
6048 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
6049 << 1 << 10 << 3 << ListRange(4, 12);
6050 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
6051 << 0 << 10 << 3 << ListRange(3, 12);
6052 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
6053 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
6054 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
6055 << 5 << 13 << 3 << ListRange(8, 15);
6056 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
6057 << 0 << 13 << 3 << ListRange(3, 15);
6059 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
6060 << 10 << 1 << 1 << ListRange(1, 9);
6061 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
6062 << 10 << 0 << 1 << ListRange(0, 9);
6063 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6064 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6065 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
6066 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
6067 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
6068 << 15 << 10 << 1 << ListRange(10, 14);
6069 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
6070 << 15 << 0 << 1 << ListRange(0, 14);
6072 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
6073 << 10 << 1 << 3 << ListRange(1, 9);
6074 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
6075 << 10 << 0 << 3 << ListRange(0, 9);
6076 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
6077 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
6078 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
6079 << 13 << 5 << 3 << ListRange(5, 12);
6080 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
6081 << 13 << 0 << 3 << ListRange(0, 12);
6083 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
6084 << 20 << 0 << 1 << ListRange(0, 15);
6085 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6086 << 25 << 4 << 1 << ListRange(0+4, 15+4);
6087 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
6088 << 20 << 0 << 3 << ListRange(0, 15);
6089 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6090 << 25 << 4 << 3 << ListRange(0+4, 15+4);
6092 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
6093 << 20 << 15 << 1 << ListRange(15, 15);
6094 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6095 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
6096 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6097 << 20 << 15 << 3 << ListRange(15, 15);
6098 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6099 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6102 void tst_QQuickListView::removeTransitions()
6104 QFETCH(int, initialItemCount);
6105 QFETCH(bool, shouldAnimateTargets);
6106 QFETCH(qreal, contentY);
6107 QFETCH(int, removalIndex);
6108 QFETCH(int, removalCount);
6109 QFETCH(ListRange, expectedDisplacedIndexes);
6111 // added items should end here
6112 QPointF targetItems_transitionTo(-50, -50);
6114 // displaced items should pass through this points
6115 QPointF displacedItems_transitionVia(100, 100);
6118 for (int i = 0; i < initialItemCount; i++)
6119 model.addItem("Original item" + QString::number(i), "");
6120 QaimModel model_targetItems_transitionTo;
6121 QaimModel model_displacedItems_transitionVia;
6123 QQuickView *canvas = getView();
6124 QQmlContext *ctxt = canvas->rootContext();
6125 TestObject *testObject = new TestObject;
6126 ctxt->setContextProperty("testModel", &model);
6127 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6128 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6129 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6130 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6131 ctxt->setContextProperty("testObject", testObject);
6132 canvas->setSource(testFileUrl("removeTransitions.qml"));
6134 QTest::qWaitForWindowShown(canvas);
6136 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6137 QTRY_VERIFY(listview != 0);
6138 QQuickItem *contentItem = listview->contentItem();
6139 QVERIFY(contentItem != 0);
6140 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6142 if (contentY != 0) {
6143 listview->setContentY(contentY);
6144 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6147 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6149 // only target items that are visible should be animated
6150 QList<QPair<QString, QString> > expectedTargetData;
6151 QList<int> targetIndexes;
6152 if (shouldAnimateTargets) {
6153 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6154 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6155 expectedTargetData << qMakePair(model.name(i), model.number(i));
6159 QVERIFY(expectedTargetData.count() > 0);
6162 // calculate targetItems and expectedTargets before model changes
6163 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6164 QVariantMap expectedTargets;
6165 for (int i=0; i<targetIndexes.count(); i++)
6166 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6169 model.removeItems(removalIndex, removalCount);
6170 QTRY_COMPARE(model.count(), listview->count());
6172 if (shouldAnimateTargets) {
6173 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6174 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6175 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6177 // check the target and displaced items were animated
6178 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6179 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6181 // check attached properties
6182 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6183 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6184 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6185 if (expectedDisplacedIndexes.isValid()) {
6186 // adjust expectedDisplacedIndexes to their final values after the move
6187 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6188 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6189 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6190 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6193 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6194 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6197 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6198 int firstVisibleIndex = -1;
6199 int itemCount = items.count();
6201 for (int i=0; i<items.count(); i++) {
6202 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6203 int index = e.evaluate().toInt();
6204 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6205 firstVisibleIndex = index;
6207 itemCount--; // exclude deleted items
6209 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6211 // verify all items moved to the correct final positions
6212 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6213 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6214 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6215 QCOMPARE(item->x(), 0.0);
6216 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6217 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6219 QTRY_COMPARE(name->text(), model.name(i));
6222 releaseView(canvas);
6226 void tst_QQuickListView::removeTransitions_data()
6228 QTest::addColumn<int>("initialItemCount");
6229 QTest::addColumn<qreal>("contentY");
6230 QTest::addColumn<bool>("shouldAnimateTargets");
6231 QTest::addColumn<int>("removalIndex");
6232 QTest::addColumn<int>("removalCount");
6233 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6235 // All items that are visible following the remove operation should be animated.
6236 // Remove targets that are outside of the view should not be animated.
6238 QTest::newRow("remove 1 before start")
6239 << 30 << 20.0 * 3 << false
6240 << 2 << 1 << ListRange();
6241 QTest::newRow("remove multiple, all before start")
6242 << 30 << 20.0 * 3 << false
6243 << 0 << 3 << ListRange();
6244 QTest::newRow("remove mix of before and after start")
6245 << 30 << 20.0 * 3 << true
6246 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
6248 QTest::newRow("remove 1 from start")
6249 << 30 << 0.0 << true
6250 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
6251 QTest::newRow("remove multiple from start")
6252 << 30 << 0.0 << true
6253 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
6254 QTest::newRow("remove 1 from start, content y not 0")
6255 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
6256 << 2 << 1 << ListRange(1 + 2, 16 + 2);
6257 QTest::newRow("remove multiple from start, content y not 0")
6258 << 30 << 20.0 * 2 << true // first visible is index 2
6259 << 2 << 3 << ListRange(3 + 2, 18 + 2);
6261 QTest::newRow("remove 1 from middle")
6262 << 30 << 0.0 << true
6263 << 5 << 1 << ListRange(6, 16);
6264 QTest::newRow("remove multiple from middle")
6265 << 30 << 0.0 << true
6266 << 5 << 3 << ListRange(8, 18);
6269 QTest::newRow("remove 1 from bottom")
6270 << 30 << 0.0 << true
6271 << 15 << 1 << ListRange(16, 16);
6273 // remove 15, 16, 17
6274 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6275 // the view, and 18 will be animated as the displaced item to replace the last item
6276 QTest::newRow("remove multiple from bottom")
6277 << 30 << 0.0 << true
6278 << 15 << 3 << ListRange(18, 18);
6280 QTest::newRow("remove 1 from bottom, content y not 0")
6281 << 30 << 20.0 * 2 << true
6282 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6283 QTest::newRow("remove multiple from bottom, content y not 0")
6284 << 30 << 20.0 * 2 << true
6285 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6288 QTest::newRow("remove 1 after end")
6289 << 30 << 0.0 << false
6290 << 17 << 1 << ListRange();
6291 QTest::newRow("remove multiple after end")
6292 << 30 << 0.0 << false
6293 << 17 << 3 << ListRange();
6296 void tst_QQuickListView::displacedTransitions()
6298 QFETCH(bool, useDisplaced);
6299 QFETCH(bool, displacedEnabled);
6300 QFETCH(bool, useAddDisplaced);
6301 QFETCH(bool, addDisplacedEnabled);
6302 QFETCH(bool, useMoveDisplaced);
6303 QFETCH(bool, moveDisplacedEnabled);
6304 QFETCH(bool, useRemoveDisplaced);
6305 QFETCH(bool, removeDisplacedEnabled);
6306 QFETCH(ListChange, change);
6307 QFETCH(ListRange, expectedDisplacedIndexes);
6310 for (int i = 0; i < 30; i++)
6311 model.addItem("Original item" + QString::number(i), "");
6312 QaimModel model_displaced_transitionVia;
6313 QaimModel model_addDisplaced_transitionVia;
6314 QaimModel model_moveDisplaced_transitionVia;
6315 QaimModel model_removeDisplaced_transitionVia;
6317 QPointF displaced_transitionVia(-50, -100);
6318 QPointF addDisplaced_transitionVia(-150, 100);
6319 QPointF moveDisplaced_transitionVia(50, -100);
6320 QPointF removeDisplaced_transitionVia(150, 100);
6322 QQuickView *canvas = getView();
6323 QQmlContext *ctxt = canvas->rootContext();
6324 TestObject *testObject = new TestObject(canvas);
6325 ctxt->setContextProperty("testModel", &model);
6326 ctxt->setContextProperty("testObject", testObject);
6327 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6328 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6329 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6330 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6331 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6332 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6333 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6334 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6335 ctxt->setContextProperty("useDisplaced", useDisplaced);
6336 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6337 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6338 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6339 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6340 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6341 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6342 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6343 canvas->setSource(testFileUrl("displacedTransitions.qml"));
6345 qApp->processEvents();
6347 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6348 QTRY_VERIFY(listview != 0);
6349 QQuickItem *contentItem = listview->contentItem();
6350 QVERIFY(contentItem != 0);
6351 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6353 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6354 listview->setProperty("displaceTransitionsDone", false);
6356 switch (change.type) {
6357 case ListChange::Inserted:
6359 QList<QPair<QString, QString> > targetItemData;
6360 for (int i=change.index; i<change.index + change.count; ++i)
6361 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6362 model.insertItems(change.index, targetItemData);
6363 QTRY_COMPARE(model.count(), listview->count());
6366 case ListChange::Removed:
6367 model.removeItems(change.index, change.count);
6368 QTRY_COMPARE(model.count(), listview->count());
6370 case ListChange::Moved:
6371 model.moveItems(change.index, change.to, change.count);
6372 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6374 case ListChange::SetCurrent:
6375 case ListChange::SetContentY:
6376 case ListChange::Polish:
6380 QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6381 QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6383 if ((useDisplaced && displacedEnabled)
6384 || (useAddDisplaced && addDisplacedEnabled)
6385 || (useMoveDisplaced && moveDisplacedEnabled)
6386 || (useRemoveDisplaced && removeDisplacedEnabled)) {
6387 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6389 // check the correct number of target items and indexes were received
6390 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6391 for (int i=0; i<resultTargetIndexes.count(); i++)
6392 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6393 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6394 for (int i=0; i<resultTargetItems.count(); i++)
6395 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6397 QCOMPARE(resultTargetIndexes.count(), 0);
6398 QCOMPARE(resultTargetItems.count(), 0);
6401 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6402 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6404 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6405 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6406 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6408 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6409 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6410 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6412 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6414 if (useDisplaced && displacedEnabled
6415 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6416 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6417 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6418 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6420 QCOMPARE(model_displaced_transitionVia.count(), 0);
6423 // verify all items moved to the correct final positions
6424 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6425 for (int i=0; i < model.count() && i < items.count(); ++i) {
6426 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6427 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6428 QCOMPARE(item->x(), 0.0);
6429 QCOMPARE(item->y(), i * 20.0);
6430 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6432 QTRY_COMPARE(name->text(), model.name(i));
6435 releaseView(canvas);
6438 void tst_QQuickListView::displacedTransitions_data()
6440 QTest::addColumn<bool>("useDisplaced");
6441 QTest::addColumn<bool>("displacedEnabled");
6442 QTest::addColumn<bool>("useAddDisplaced");
6443 QTest::addColumn<bool>("addDisplacedEnabled");
6444 QTest::addColumn<bool>("useMoveDisplaced");
6445 QTest::addColumn<bool>("moveDisplacedEnabled");
6446 QTest::addColumn<bool>("useRemoveDisplaced");
6447 QTest::addColumn<bool>("removeDisplacedEnabled");
6448 QTest::addColumn<ListChange>("change");
6449 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6451 QTest::newRow("no displaced transitions at all")
6456 << ListChange::insert(0, 1) << ListRange(0, 15);
6458 QTest::newRow("just displaced")
6463 << ListChange::insert(0, 1) << ListRange(0, 15);
6465 QTest::newRow("just displaced (not enabled)")
6470 << ListChange::insert(0, 1) << ListRange(0, 15);
6472 QTest::newRow("displaced + addDisplaced")
6477 << ListChange::insert(0, 1) << ListRange(0, 15);
6479 QTest::newRow("displaced + addDisplaced (not enabled)")
6484 << ListChange::insert(0, 1) << ListRange(0, 15);
6486 QTest::newRow("displaced + moveDisplaced")
6491 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6493 QTest::newRow("displaced + moveDisplaced (not enabled)")
6498 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6500 QTest::newRow("displaced + removeDisplaced")
6505 << ListChange::remove(0, 1) << ListRange(1, 16);
6507 QTest::newRow("displaced + removeDisplaced (not enabled)")
6512 << ListChange::remove(0, 1) << ListRange(1, 16);
6515 QTest::newRow("displaced + add, should use generic displaced for a remove")
6520 << ListChange::remove(0, 1) << ListRange(1, 16);
6523 void tst_QQuickListView::multipleTransitions()
6525 // Tests that if you interrupt a transition in progress with another action that
6526 // cancels the previous transition, the resulting items are still placed correctly.
6528 QFETCH(int, initialCount);
6529 QFETCH(qreal, contentY);
6530 QFETCH(QList<ListChange>, changes);
6531 QFETCH(bool, enableAddTransitions);
6532 QFETCH(bool, enableMoveTransitions);
6533 QFETCH(bool, enableRemoveTransitions);
6534 QFETCH(bool, rippleAddDisplaced);
6536 QPointF addTargets_transitionFrom(-50, -50);
6537 QPointF addDisplaced_transitionFrom(-50, 50);
6538 QPointF moveTargets_transitionFrom(50, -50);
6539 QPointF moveDisplaced_transitionFrom(50, 50);
6540 QPointF removeTargets_transitionTo(-100, 300);
6541 QPointF removeDisplaced_transitionFrom(100, 300);
6544 for (int i = 0; i < initialCount; i++)
6545 model.addItem("Original item" + QString::number(i), "");
6547 QQuickView *canvas = getView();
6548 QQmlContext *ctxt = canvas->rootContext();
6549 TestObject *testObject = new TestObject;
6550 ctxt->setContextProperty("testModel", &model);
6551 ctxt->setContextProperty("testObject", testObject);
6552 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6553 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6554 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6555 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6556 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6557 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6558 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6559 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6560 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6561 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6562 canvas->setSource(testFileUrl("multipleTransitions.qml"));
6564 QTest::qWaitForWindowShown(canvas);
6566 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6567 QTRY_VERIFY(listview != 0);
6568 QQuickItem *contentItem = listview->contentItem();
6569 QVERIFY(contentItem != 0);
6570 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6572 if (contentY != 0) {
6573 listview->setContentY(contentY);
6574 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6577 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
6579 for (int i=0; i<changes.count(); i++) {
6580 switch (changes[i].type) {
6581 case ListChange::Inserted:
6583 QList<QPair<QString, QString> > targetItems;
6584 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6585 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6586 model.insertItems(changes[i].index, targetItems);
6587 QTRY_COMPARE(model.count(), listview->count());
6588 if (i == changes.count() - 1) {
6589 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6590 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6592 QTest::qWait(timeBetweenActions);
6596 case ListChange::Removed:
6597 model.removeItems(changes[i].index, changes[i].count);
6598 QTRY_COMPARE(model.count(), listview->count());
6599 if (i == changes.count() - 1) {
6600 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6601 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6603 QTest::qWait(timeBetweenActions);
6606 case ListChange::Moved:
6607 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6608 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6609 if (i == changes.count() - 1) {
6610 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6611 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6613 QTest::qWait(timeBetweenActions);
6616 case ListChange::SetCurrent:
6617 listview->setCurrentIndex(changes[i].index);
6619 case ListChange::SetContentY:
6620 listview->setContentY(changes[i].pos);
6621 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6623 case ListChange::Polish:
6627 QCOMPARE(listview->count(), model.count());
6629 // verify all items moved to the correct final positions
6630 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6631 for (int i=0; i < model.count() && i < items.count(); ++i) {
6632 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6633 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6634 QTRY_COMPARE(item->x(), 0.0);
6635 QTRY_COMPARE(item->y(), i*20.0);
6636 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6638 QTRY_COMPARE(name->text(), model.name(i));
6641 releaseView(canvas);
6645 void tst_QQuickListView::multipleTransitions_data()
6647 QTest::addColumn<int>("initialCount");
6648 QTest::addColumn<qreal>("contentY");
6649 QTest::addColumn<QList<ListChange> >("changes");
6650 QTest::addColumn<bool>("enableAddTransitions");
6651 QTest::addColumn<bool>("enableMoveTransitions");
6652 QTest::addColumn<bool>("enableRemoveTransitions");
6653 QTest::addColumn<bool>("rippleAddDisplaced");
6655 // the added item and displaced items should move to final dest correctly
6656 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6657 << ListChange::insert(0, 1)
6658 << ListChange::move(0, 3, 1)
6660 << true << true << true << false;
6662 // items affected by the add should change from move to add transition
6663 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6664 << ListChange::move(1, 10, 3)
6665 << ListChange::insert(0, 1)
6667 << true << true << true << false;
6669 // items should be placed correctly if you trigger a transition then refill for that index
6670 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6671 << ListChange::insert(0, 1)
6672 << ListChange::setContentY(80.0)
6673 << ListChange::setContentY(0.0)
6674 << ListChange::insert(0, 1)
6676 << true << true << true << false;
6678 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6679 << ListChange::insert(1, 1)
6680 << ListChange::remove(1, 1)
6682 << true << true << true << true;
6684 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6685 // even if a remove-displace transition is not present to re-animate them
6686 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6687 << ListChange::insert(0, 1)
6688 << ListChange::remove(2, 1)
6690 << true << true << false << false;
6692 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6693 // remove has changed the position of where it will move to
6694 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6695 << ListChange::setContentY(-10.0)
6696 << ListChange::insert(0, 1)
6697 << ListChange::insert(0, 1)
6698 << ListChange::remove(2, 1)
6700 << true << true << false << false;
6703 void tst_QQuickListView::multipleDisplaced()
6705 // multiple move() operations should only restart displace transitions for items that
6706 // moved from previously set positions, and not those that have moved from their current
6707 // item positions (which may e.g. still be changing from easing bounces in the last transition)
6710 for (int i = 0; i < 30; i++)
6711 model.addItem("Original item" + QString::number(i), "");
6713 QQuickView *canvas = getView();
6714 QQmlContext *ctxt = canvas->rootContext();
6715 ctxt->setContextProperty("testModel", &model);
6716 ctxt->setContextProperty("testObject", new TestObject(canvas));
6717 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6719 QTest::qWaitForWindowShown(canvas);
6721 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6722 QTRY_VERIFY(listview != 0);
6723 QQuickItem *contentItem = listview->contentItem();
6724 QVERIFY(contentItem != 0);
6725 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6727 model.moveItems(12, 8, 1);
6728 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6729 model.moveItems(8, 3, 1);
6730 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6732 QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6733 foreach (const QString &name, transitionsStarted.keys()) {
6734 QVERIFY2(transitionsStarted[name] == 1,
6735 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6738 // verify all items moved to the correct final positions
6739 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6740 for (int i=0; i < model.count() && i < items.count(); ++i) {
6741 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6742 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6743 QTRY_COMPARE(item->x(), 0.0);
6744 QTRY_COMPARE(item->y(), i*20.0);
6745 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6747 QTRY_COMPARE(name->text(), model.name(i));
6750 releaseView(canvas);
6753 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6757 for (int i=0; i<list.count(); i++) {
6758 ret << list[i].toInt(&ok);
6760 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6766 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6768 for (int i=0; i<indexLists.count(); i++) {
6769 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6770 if (current != expectedIndexes.toSet())
6771 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6772 QCOMPARE(current, expectedIndexes.toSet());
6776 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6778 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6779 QVERIFY(it.value().type() == QVariant::Int);
6780 QString name = it.key();
6781 int itemIndex = it.value().toInt();
6782 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6783 if (model.name(itemIndex) != name)
6784 qDebug() << itemIndex;
6785 QCOMPARE(model.name(itemIndex), name);
6787 QCOMPARE(items.count(), expectedIndexes.count());
6790 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6792 for (int i=0; i<itemLists.count(); i++) {
6793 QVERIFY(itemLists[i].type() == QVariant::List);
6794 QVariantList current = itemLists[i].toList();
6795 for (int j=0; j<current.count(); j++) {
6796 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6797 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6798 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6800 QCOMPARE(current.count(), expectedItems.count());
6804 void tst_QQuickListView::flickBeyondBounds()
6806 QQuickView *canvas = createView();
6808 canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6810 qApp->processEvents();
6812 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6813 QTRY_VERIFY(listview != 0);
6815 QQuickItem *contentItem = listview->contentItem();
6816 QTRY_VERIFY(contentItem != 0);
6817 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6819 // Flick view up beyond bounds
6820 flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6821 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6823 // We're really testing that we don't get stuck in a loop,
6824 // but also confirm items positioned correctly.
6825 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6826 for (int i = 0; i < 2; ++i) {
6827 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6828 if (!item) qWarning() << "Item" << i << "not found";
6830 QTRY_VERIFY(item->y() == i*45);
6836 void tst_QQuickListView::destroyItemOnCreation()
6839 QQuickView *canvas = createView();
6840 canvas->rootContext()->setContextProperty("testModel", &model);
6842 canvas->setSource(testFileUrl("destroyItemOnCreation.qml"));
6844 qApp->processEvents();
6846 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6847 QVERIFY(listview != 0);
6849 QQuickItem *contentItem = listview->contentItem();
6850 QVERIFY(contentItem != 0);
6851 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6853 QCOMPARE(canvas->rootObject()->property("createdIndex").toInt(), -1);
6854 model.addItem("new item", "");
6855 QTRY_COMPARE(canvas->rootObject()->property("createdIndex").toInt(), 0);
6857 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6858 QCOMPARE(model.count(), 0);
6863 void tst_QQuickListView::parentBinding()
6865 QQuickView *canvas = createView();
6868 QtMsgHandler old = qInstallMsgHandler(errorMsgHandler);
6870 canvas->setSource(testFileUrl("parentBinding.qml"));
6872 qApp->processEvents();
6874 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
6875 QVERIFY(listview != 0);
6877 QQuickItem *contentItem = listview->contentItem();
6878 QVERIFY(contentItem != 0);
6879 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6881 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6883 QCOMPARE(item->width(), listview->width());
6884 QCOMPARE(item->height(), listview->height()/12);
6886 // there should be no transient binding error
6887 QVERIFY(!m_errorCount);
6889 qInstallMsgHandler(old);
6894 QTEST_MAIN(tst_QQuickListView)
6896 #include "tst_qquicklistview.moc"