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 // Test both QListModelInterface and QAbstractItemModel model types
78 void qListModelInterface_items();
79 void qListModelInterface_package_items();
80 void qAbstractItemModel_items();
82 void qListModelInterface_changed();
83 void qListModelInterface_package_changed();
84 void qAbstractItemModel_changed();
86 void qListModelInterface_inserted();
87 void qListModelInterface_inserted_more();
88 void qListModelInterface_inserted_more_data();
89 void qListModelInterface_package_inserted();
90 void qAbstractItemModel_inserted();
91 void qAbstractItemModel_inserted_more();
92 void qAbstractItemModel_inserted_more_data();
93 void qAbstractItemModel_inserted_more_bottomToTop();
94 void qAbstractItemModel_inserted_more_bottomToTop_data();
96 void qListModelInterface_removed();
97 void qListModelInterface_removed_more();
98 void qListModelInterface_removed_more_data();
99 void qListModelInterface_package_removed();
100 void qAbstractItemModel_removed();
101 void qAbstractItemModel_removed_more();
102 void qAbstractItemModel_removed_more_data();
103 void qAbstractItemModel_removed_more_bottomToTop();
104 void qAbstractItemModel_removed_more_bottomToTop_data();
106 void qListModelInterface_moved();
107 void qListModelInterface_moved_data();
108 void qListModelInterface_package_moved();
109 void qListModelInterface_package_moved_data();
110 void qAbstractItemModel_moved();
111 void qAbstractItemModel_moved_data();
112 void qAbstractItemModel_moved_bottomToTop();
113 void qAbstractItemModel_moved_bottomToTop_data();
115 void multipleChanges_condensed() { multipleChanges(true); }
116 void multipleChanges_condensed_data() { multipleChanges_data(); }
117 void multipleChanges_uncondensed() { multipleChanges(false); }
118 void multipleChanges_uncondensed_data() { multipleChanges_data(); }
120 void qListModelInterface_clear();
121 void qListModelInterface_package_clear();
122 void qAbstractItemModel_clear();
123 void qAbstractItemModel_clear_bottomToTop();
125 void insertBeforeVisible();
126 void insertBeforeVisible_data();
127 void swapWithFirstItem();
129 void itemListFlicker();
130 void currentIndex_delayedItemCreation();
131 void currentIndex_delayedItemCreation_data();
133 void noCurrentIndex();
134 void keyNavigation();
135 void keyNavigation_data();
137 void enforceRange_withoutHighlight();
139 void qListModelInterface_sections();
140 void qListModelInterface_package_sections();
141 void qAbstractItemModel_sections();
142 void sectionsPositioning();
143 void sectionsDelegate();
144 void sectionsDragOutsideBounds_data();
145 void sectionsDragOutsideBounds();
146 void sectionPropertyChange();
148 void positionViewAtIndex();
150 void propertyChanges();
151 void componentChanges();
153 void manualHighlight();
154 void initialZValues();
157 void header_delayItemCreation();
162 void resetModel_headerFooter();
164 void resizeViewAndRepaint();
165 void sizeLessThan1();
167 void resizeDelegate();
168 void resizeFirstDelegate();
169 void repositionResizedDelegate();
170 void repositionResizedDelegate_data();
172 void indexAt_itemAt_data();
173 void indexAt_itemAt();
174 void incrementalModel();
178 void onRemove_data();
180 void test_mirroring();
182 void marginsResize();
183 void marginsResize_data();
184 void creationContext();
185 void snapToItem_data();
187 void snapOneItem_data();
195 void unrequestedVisibility();
197 void populateTransitions();
198 void populateTransitions_data();
199 void addTransitions();
200 void addTransitions_data();
201 void moveTransitions();
202 void moveTransitions_data();
203 void removeTransitions();
204 void removeTransitions_data();
205 void displacedTransitions();
206 void displacedTransitions_data();
207 void multipleTransitions();
208 void multipleTransitions_data();
209 void multipleDisplaced();
211 void flickBeyondBounds();
212 void destroyItemOnCreation();
214 void parentBinding();
217 template <class T> void items(const QUrl &source, bool forceLayout);
218 template <class T> void changed(const QUrl &source, bool forceLayout);
219 template <class T> void inserted(const QUrl &source);
220 template <class T> void inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
221 template <class T> void removed(const QUrl &source, bool animated);
222 template <class T> void removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
223 template <class T> void moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
224 template <class T> void clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
225 template <class T> void sections(const QUrl &source);
227 void multipleChanges(bool condensed);
228 void multipleChanges_data();
230 QList<int> toIntList(const QVariantList &list);
231 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
232 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
233 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
235 void inserted_more_data();
236 void removed_more_data();
240 QQuickView *getView() {
242 if (QString(QTest::currentTestFunction()) != testForView) {
246 m_view->setSource(QUrl());
251 testForView = QTest::currentTestFunction();
252 m_view = createView();
255 void releaseView(QQuickView *view) {
256 Q_ASSERT(view == m_view);
257 m_view->setSource(QUrl());
260 QQuickView *getView() {
263 void releaseView(QQuickView *view) {
268 static void errorMsgHandler(QtMsgType, const char *)
275 static int m_errorCount;
278 int tst_QQuickListView::m_errorCount = 0;
280 class TestObject : public QObject
284 Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError)
285 Q_PROPERTY(bool animate READ animate NOTIFY changedAnim)
286 Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl)
287 Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer)
290 TestObject(QObject *parent = 0)
291 : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
294 bool error() const { return mError; }
295 void setError(bool err) { mError = err; emit changedError(); }
297 bool animate() const { return mAnimate; }
298 void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
300 bool invalidHighlight() const { return mInvalidHighlight; }
301 void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
303 int cacheBuffer() const { return mCacheBuffer; }
304 void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
310 void changedCacheBuffer();
315 bool mInvalidHighlight;
319 tst_QQuickListView::tst_QQuickListView() : m_view(0)
323 void tst_QQuickListView::init()
326 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
327 testForView = QString();
335 void tst_QQuickListView::items(const QUrl &source, bool forceLayout)
337 QQuickView *canvas = createView();
340 model.addItem("Fred", "12345");
341 model.addItem("John", "2345");
342 model.addItem("Bob", "54321");
344 QQmlContext *ctxt = canvas->rootContext();
345 ctxt->setContextProperty("testModel", &model);
347 TestObject *testObject = new TestObject;
348 ctxt->setContextProperty("testObject", testObject);
350 canvas->setSource(source);
351 qApp->processEvents();
353 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
354 QTRY_VERIFY(listview != 0);
356 QQuickItem *contentItem = listview->contentItem();
357 QTRY_VERIFY(contentItem != 0);
359 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
360 QTRY_VERIFY(testObject->error() == false);
362 QTRY_VERIFY(listview->highlightItem() != 0);
363 QTRY_COMPARE(listview->count(), model.count());
364 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
365 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
367 // current item should be first item
368 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
370 for (int i = 0; i < model.count(); ++i) {
371 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
372 QTRY_VERIFY(name != 0);
373 QTRY_COMPARE(name->text(), model.name(i));
374 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
375 QTRY_VERIFY(number != 0);
376 QTRY_COMPARE(number->text(), model.number(i));
379 // switch to other delegate
380 testObject->setAnimate(true);
381 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
382 QTRY_VERIFY(testObject->error() == false);
383 QTRY_VERIFY(listview->currentItem());
385 // set invalid highlight
386 testObject->setInvalidHighlight(true);
387 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
388 QTRY_VERIFY(testObject->error() == false);
389 QTRY_VERIFY(listview->currentItem());
390 QTRY_VERIFY(listview->highlightItem() == 0);
392 // back to normal highlight
393 testObject->setInvalidHighlight(false);
394 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
395 QTRY_VERIFY(testObject->error() == false);
396 QTRY_VERIFY(listview->currentItem());
397 QTRY_VERIFY(listview->highlightItem() != 0);
399 // set an empty model and confirm that items are destroyed
401 ctxt->setContextProperty("testModel", &model2);
403 // Force a layout, necessary if ListView is completed before VisualDataModel.
405 QCOMPARE(listview->property("count").toInt(), 0);
407 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
408 QTRY_VERIFY(itemCount == 0);
410 QTRY_COMPARE(listview->highlightResizeSpeed(), 1000.0);
411 QTRY_COMPARE(listview->highlightMoveSpeed(), 1000.0);
419 void tst_QQuickListView::changed(const QUrl &source, bool forceLayout)
421 QQuickView *canvas = createView();
424 model.addItem("Fred", "12345");
425 model.addItem("John", "2345");
426 model.addItem("Bob", "54321");
428 QQmlContext *ctxt = canvas->rootContext();
429 ctxt->setContextProperty("testModel", &model);
431 TestObject *testObject = new TestObject;
432 ctxt->setContextProperty("testObject", testObject);
434 canvas->setSource(source);
435 qApp->processEvents();
437 QQuickFlickable *listview = findItem<QQuickFlickable>(canvas->rootObject(), "list");
438 QTRY_VERIFY(listview != 0);
440 QQuickItem *contentItem = listview->contentItem();
441 QTRY_VERIFY(contentItem != 0);
443 // Force a layout, necessary if ListView is completed before VisualDataModel.
445 QCOMPARE(listview->property("count").toInt(), model.count());
447 model.modifyItem(1, "Will", "9876");
448 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
449 QTRY_VERIFY(name != 0);
450 QTRY_COMPARE(name->text(), model.name(1));
451 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
452 QTRY_VERIFY(number != 0);
453 QTRY_COMPARE(number->text(), model.number(1));
460 void tst_QQuickListView::inserted(const QUrl &source)
462 QQuickView *canvas = createView();
466 model.addItem("Fred", "12345");
467 model.addItem("John", "2345");
468 model.addItem("Bob", "54321");
470 QQmlContext *ctxt = canvas->rootContext();
471 ctxt->setContextProperty("testModel", &model);
473 TestObject *testObject = new TestObject;
474 ctxt->setContextProperty("testObject", testObject);
476 canvas->setSource(source);
477 qApp->processEvents();
479 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
480 QTRY_VERIFY(listview != 0);
482 QQuickItem *contentItem = listview->contentItem();
483 QTRY_VERIFY(contentItem != 0);
485 model.insertItem(1, "Will", "9876");
487 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
488 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
490 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
491 QTRY_VERIFY(name != 0);
492 QTRY_COMPARE(name->text(), model.name(1));
493 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
494 QTRY_VERIFY(number != 0);
495 QTRY_COMPARE(number->text(), model.number(1));
497 // Confirm items positioned correctly
498 for (int i = 0; i < model.count(); ++i) {
499 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
500 QTRY_COMPARE(item->y(), i*20.0);
503 model.insertItem(0, "Foo", "1111"); // zero index, and current item
505 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
506 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
508 name = findItem<QQuickText>(contentItem, "textName", 0);
509 QTRY_VERIFY(name != 0);
510 QTRY_COMPARE(name->text(), model.name(0));
511 number = findItem<QQuickText>(contentItem, "textNumber", 0);
512 QTRY_VERIFY(number != 0);
513 QTRY_COMPARE(number->text(), model.number(0));
515 QTRY_COMPARE(listview->currentIndex(), 1);
517 // Confirm items positioned correctly
518 for (int i = 0; i < model.count(); ++i) {
519 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
520 QTRY_COMPARE(item->y(), i*20.0);
523 for (int i = model.count(); i < 30; ++i)
524 model.insertItem(i, "Hello", QString::number(i));
526 listview->setContentY(80);
528 // Insert item outside visible area
529 model.insertItem(1, "Hello", "1324");
531 QTRY_VERIFY(listview->contentY() == 80);
533 // Confirm items positioned correctly
534 for (int i = 5; i < 5+15; ++i) {
535 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
536 if (!item) qWarning() << "Item" << i << "not found";
538 QTRY_COMPARE(item->y(), i*20.0 - 20.0);
541 // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
545 model.insertItem(0, "Hello", "1234");
546 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
548 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
550 QCOMPARE(item->y(), 0.);
551 QTRY_VERIFY(listview->contentY() == 0);
558 void tst_QQuickListView::inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
560 QFETCH(qreal, contentY);
561 QFETCH(int, insertIndex);
562 QFETCH(int, insertCount);
563 QFETCH(qreal, itemsOffsetAfterMove);
566 for (int i = 0; i < 30; i++)
567 model.addItem("Item" + QString::number(i), "");
569 QQuickView *canvas = getView();
570 QQmlContext *ctxt = canvas->rootContext();
571 ctxt->setContextProperty("testModel", &model);
573 TestObject *testObject = new TestObject;
574 ctxt->setContextProperty("testObject", testObject);
576 canvas->setSource(testFileUrl("listviewtest.qml"));
578 qApp->processEvents();
579 QTest::qWaitForWindowShown(canvas);
581 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
582 QTRY_VERIFY(listview != 0);
583 QQuickItem *contentItem = listview->contentItem();
584 QTRY_VERIFY(contentItem != 0);
586 bool waitForPolish = (contentY != 0);
587 if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
588 listview->setVerticalLayoutDirection(verticalLayoutDirection);
589 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
590 contentY = -listview->height() - contentY;
592 listview->setContentY(contentY);
594 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
596 QList<QPair<QString, QString> > newData;
597 for (int i=0; i<insertCount; i++)
598 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
599 model.insertItems(insertIndex, newData);
600 QTRY_COMPARE(listview->property("count").toInt(), model.count());
602 // check visibleItems.first() is in correct position
603 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
605 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
606 QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
608 QCOMPARE(item0->y(), itemsOffsetAfterMove);
610 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
611 int firstVisibleIndex = -1;
612 for (int i=0; i<items.count(); i++) {
613 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
614 if (item && !QQuickItemPrivate::get(item)->culled) {
615 firstVisibleIndex = i;
619 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
621 // Confirm items positioned correctly and indexes correct
624 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
625 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
626 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
627 qreal pos = i*20.0 + itemsOffsetAfterMove;
628 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
629 pos = -item0->height() - pos;
630 QTRY_COMPARE(item->y(), pos);
631 name = findItem<QQuickText>(contentItem, "textName", i);
633 QTRY_COMPARE(name->text(), model.name(i));
634 number = findItem<QQuickText>(contentItem, "textNumber", i);
635 QVERIFY(number != 0);
636 QTRY_COMPARE(number->text(), model.number(i));
643 void tst_QQuickListView::inserted_more_data()
645 QTest::addColumn<qreal>("contentY");
646 QTest::addColumn<int>("insertIndex");
647 QTest::addColumn<int>("insertCount");
648 QTest::addColumn<qreal>("itemsOffsetAfterMove");
650 QTest::newRow("add 1, before visible items")
653 << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move
655 QTest::newRow("add multiple, before visible")
658 << -20.0 * 3; // again first visible should not move
660 QTest::newRow("add 1, at start of visible, content at start")
665 QTest::newRow("add multiple, start of visible, content at start")
670 QTest::newRow("add 1, at start of visible, content not at start")
675 QTest::newRow("add multiple, at start of visible, content not at start")
681 QTest::newRow("add 1, at end of visible, content at start")
686 QTest::newRow("add 1, at end of visible, content at start")
691 QTest::newRow("add 1, at end of visible, content not at start")
696 QTest::newRow("add multiple, at end of visible, content not at start")
702 QTest::newRow("add 1, after visible, content at start")
707 QTest::newRow("add 1, after visible, content at start")
712 QTest::newRow("add 1, after visible, content not at start")
717 QTest::newRow("add multiple, after visible, content not at start")
723 void tst_QQuickListView::insertBeforeVisible()
725 QFETCH(int, insertIndex);
726 QFETCH(int, insertCount);
727 QFETCH(int, cacheBuffer);
730 QQuickView *canvas = getView();
733 for (int i = 0; i < 30; i++)
734 model.addItem("Item" + QString::number(i), "");
736 QQmlContext *ctxt = canvas->rootContext();
737 ctxt->setContextProperty("testModel", &model);
739 TestObject *testObject = new TestObject;
740 ctxt->setContextProperty("testObject", testObject);
742 canvas->setSource(testFileUrl("listviewtest.qml"));
744 qApp->processEvents();
745 QTest::qWaitForWindowShown(canvas);
747 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
748 QTRY_VERIFY(listview != 0);
749 QQuickItem *contentItem = listview->contentItem();
750 QTRY_VERIFY(contentItem != 0);
752 listview->setCacheBuffer(cacheBuffer);
753 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
755 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
756 int firstVisibleIndex = 20; // move to an index where the top item is not visible
757 listview->setContentY(firstVisibleIndex * 20.0);
758 listview->setCurrentIndex(firstVisibleIndex);
760 qApp->processEvents();
761 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
762 QTRY_COMPARE(listview->currentIndex(), firstVisibleIndex);
763 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
765 QCOMPARE(item->y(), listview->contentY());
767 QList<QPair<QString, QString> > newData;
768 for (int i=0; i<insertCount; i++)
769 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
770 model.insertItems(insertIndex, newData);
771 QTRY_COMPARE(listview->property("count").toInt(), model.count());
773 // now, moving to the top of the view should position the inserted items correctly
774 int itemsOffsetAfterMove = -(insertCount * 20);
775 listview->setCurrentIndex(0);
776 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
777 QTRY_COMPARE(listview->currentIndex(), 0);
778 QTRY_COMPARE(listview->contentY(), 0.0 + itemsOffsetAfterMove);
780 // Confirm items positioned correctly and indexes correct
781 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
782 for (int i = 0; i < model.count() && i < itemCount; ++i) {
783 item = findItem<QQuickItem>(contentItem, "wrapper", i);
784 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
785 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
786 name = findItem<QQuickText>(contentItem, "textName", i);
788 QTRY_COMPARE(name->text(), model.name(i));
795 void tst_QQuickListView::insertBeforeVisible_data()
797 QTest::addColumn<int>("insertIndex");
798 QTest::addColumn<int>("insertCount");
799 QTest::addColumn<int>("cacheBuffer");
801 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
802 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
803 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
805 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
806 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
807 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
809 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 3 << 0;
810 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 3 << 100;
811 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 3 << 500;
813 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 3 << 0;
814 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 3 << 100;
815 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 3 << 500;
819 void tst_QQuickListView::removed(const QUrl &source, bool /* animated */)
821 QQuickView *canvas = createView();
824 for (int i = 0; i < 50; i++)
825 model.addItem("Item" + QString::number(i), "");
827 QQmlContext *ctxt = canvas->rootContext();
828 ctxt->setContextProperty("testModel", &model);
830 TestObject *testObject = new TestObject;
831 ctxt->setContextProperty("testObject", testObject);
833 canvas->setSource(source);
835 qApp->processEvents();
836 QTest::qWaitForWindowShown(canvas);
838 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
839 QTRY_VERIFY(listview != 0);
840 QQuickItem *contentItem = listview->contentItem();
841 QTRY_VERIFY(contentItem != 0);
842 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
845 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
847 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
848 QTRY_VERIFY(name != 0);
849 QTRY_COMPARE(name->text(), model.name(1));
850 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
851 QTRY_VERIFY(number != 0);
852 QTRY_COMPARE(number->text(), model.number(1));
854 // Confirm items positioned correctly
855 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
856 for (int i = 0; i < model.count() && i < itemCount; ++i) {
857 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
858 if (!item) qWarning() << "Item" << i << "not found";
860 QTRY_VERIFY(item->y() == i*20);
863 // Remove first item (which is the current item);
865 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
867 name = findItem<QQuickText>(contentItem, "textName", 0);
868 QTRY_VERIFY(name != 0);
869 QTRY_COMPARE(name->text(), model.name(0));
870 number = findItem<QQuickText>(contentItem, "textNumber", 0);
871 QTRY_VERIFY(number != 0);
872 QTRY_COMPARE(number->text(), model.number(0));
874 // Confirm items positioned correctly
875 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
876 for (int i = 0; i < model.count() && i < itemCount; ++i) {
877 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
878 if (!item) qWarning() << "Item" << i << "not found";
880 QTRY_COMPARE(item->y(),i*20.0);
883 // Remove items not visible
884 model.removeItem(18);
885 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
887 // Confirm items positioned correctly
888 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
889 for (int i = 0; i < model.count() && i < itemCount; ++i) {
890 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
891 if (!item) qWarning() << "Item" << i << "not found";
893 QTRY_COMPARE(item->y(),i*20.0);
896 // Remove items before visible
897 listview->setContentY(80);
898 listview->setCurrentIndex(10);
900 model.removeItem(1); // post: top item will be at 20
901 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
903 // Confirm items positioned correctly
904 for (int i = 2; i < 18; ++i) {
905 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
906 if (!item) qWarning() << "Item" << i << "not found";
908 QTRY_COMPARE(item->y(),20+i*20.0);
911 // Remove current index
912 QTRY_VERIFY(listview->currentIndex() == 9);
913 QQuickItem *oldCurrent = listview->currentItem();
916 QTRY_COMPARE(listview->currentIndex(), 9);
917 QTRY_VERIFY(listview->currentItem() != oldCurrent);
919 listview->setContentY(20); // That's the top now
920 // let transitions settle.
921 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
923 // Confirm items positioned correctly
924 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
925 for (int i = 0; i < model.count() && i < itemCount; ++i) {
926 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
927 if (!item) qWarning() << "Item" << i << "not found";
929 QTRY_COMPARE(item->y(),20+i*20.0);
932 // remove current item beyond visible items.
933 listview->setCurrentIndex(20);
934 listview->setContentY(40);
935 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
937 model.removeItem(20);
938 QTRY_COMPARE(listview->currentIndex(), 20);
939 QTRY_VERIFY(listview->currentItem() != 0);
941 // remove item before current, but visible
942 listview->setCurrentIndex(8);
943 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
944 oldCurrent = listview->currentItem();
947 QTRY_COMPARE(listview->currentIndex(), 7);
948 QTRY_VERIFY(listview->currentItem() == oldCurrent);
950 listview->setContentY(80);
951 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
953 // remove all visible items
954 model.removeItems(1, 18);
955 QTRY_COMPARE(listview->count() , model.count());
957 // Confirm items positioned correctly
958 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
959 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
960 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i+1);
961 if (!item) qWarning() << "Item" << i+1 << "not found";
963 QTRY_COMPARE(item->y(),80+i*20.0);
966 model.removeItems(1, 17);
967 QTRY_COMPARE(listview->count() , model.count());
969 model.removeItems(2, 1);
970 QTRY_COMPARE(listview->count() , model.count());
972 model.addItem("New", "1");
973 QTRY_COMPARE(listview->count() , model.count());
975 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", model.count()-1));
976 QCOMPARE(name->text(), QString("New"));
978 // Add some more items so that we don't run out
980 for (int i = 0; i < 50; i++)
981 model.addItem("Item" + QString::number(i), "");
984 listview->setCurrentIndex(0);
985 listview->setContentY(30);
987 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", 0));
989 // QTBUG-19198 move to end and remove all visible items one at a time.
990 listview->positionViewAtEnd();
991 for (int i = 0; i < 18; ++i)
992 model.removeItems(model.count() - 1, 1);
993 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() > 16);
1000 void tst_QQuickListView::removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1002 QFETCH(qreal, contentY);
1003 QFETCH(int, removeIndex);
1004 QFETCH(int, removeCount);
1005 QFETCH(qreal, itemsOffsetAfterMove);
1007 QQuickView *canvas = getView();
1010 for (int i = 0; i < 30; i++)
1011 model.addItem("Item" + QString::number(i), "");
1013 QQmlContext *ctxt = canvas->rootContext();
1014 ctxt->setContextProperty("testModel", &model);
1016 TestObject *testObject = new TestObject;
1017 ctxt->setContextProperty("testObject", testObject);
1019 canvas->setSource(source);
1021 qApp->processEvents();
1022 QTest::qWaitForWindowShown(canvas);
1024 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1025 QTRY_VERIFY(listview != 0);
1026 QQuickItem *contentItem = listview->contentItem();
1027 QTRY_VERIFY(contentItem != 0);
1029 bool waitForPolish = (contentY != 0);
1030 if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
1031 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1032 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1033 contentY = -listview->height() - contentY;
1035 listview->setContentY(contentY);
1037 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1039 model.removeItems(removeIndex, removeCount);
1040 QTRY_COMPARE(listview->property("count").toInt(), model.count());
1042 // check visibleItems.first() is in correct position
1043 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
1046 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1047 QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
1049 QCOMPARE(item0->y(), itemsOffsetAfterMove);
1051 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1052 int firstVisibleIndex = -1;
1053 for (int i=0; i<items.count(); i++) {
1054 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1055 if (item && delegateVisible(item)) {
1056 firstVisibleIndex = i;
1060 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1062 // Confirm items positioned correctly and indexes correct
1065 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1066 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1067 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1068 qreal pos = i*20.0 + itemsOffsetAfterMove;
1069 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1070 pos = -item0->height() - pos;
1071 QTRY_COMPARE(item->y(), pos);
1072 name = findItem<QQuickText>(contentItem, "textName", i);
1074 QTRY_COMPARE(name->text(), model.name(i));
1075 number = findItem<QQuickText>(contentItem, "textNumber", i);
1076 QVERIFY(number != 0);
1077 QTRY_COMPARE(number->text(), model.number(i));
1080 releaseView(canvas);
1084 void tst_QQuickListView::removed_more_data()
1086 QTest::addColumn<qreal>("contentY");
1087 QTest::addColumn<int>("removeIndex");
1088 QTest::addColumn<int>("removeCount");
1089 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1091 QTest::newRow("remove 1, before visible items")
1092 << 80.0 // show 4-19
1094 << 20.0; // visible items slide down by 1 item so that first visible does not move
1096 QTest::newRow("remove multiple, all before visible items")
1101 QTest::newRow("remove multiple, all before visible items, remove item 0")
1106 // remove 1,2,3 before the visible pos, 0 moves down to just before the visible pos,
1107 // items 4,5 are removed from view, item 6 slides up to original pos of item 4 (80px)
1108 QTest::newRow("remove multiple, mix of items from before and within visible items")
1111 << 20.0 * 3; // adjust for the 3 items removed before the visible
1113 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1116 << 20.0 * 4; // adjust for the 3 items removed before the visible
1119 QTest::newRow("remove 1, from start of visible, content at start")
1124 QTest::newRow("remove multiple, from start of visible, content at start")
1129 QTest::newRow("remove 1, from start of visible, content not at start")
1130 << 80.0 // show 4-19
1134 QTest::newRow("remove multiple, from start of visible, content not at start")
1135 << 80.0 // show 4-19
1140 QTest::newRow("remove 1, from middle of visible, content at start")
1145 QTest::newRow("remove multiple, from middle of visible, content at start")
1150 QTest::newRow("remove 1, from middle of visible, content not at start")
1151 << 80.0 // show 4-19
1155 QTest::newRow("remove multiple, from middle of visible, content not at start")
1156 << 80.0 // show 4-19
1161 QTest::newRow("remove 1, after visible, content at start")
1166 QTest::newRow("remove multiple, after visible, content at start")
1171 QTest::newRow("remove 1, after visible, content not at middle")
1172 << 80.0 // show 4-19
1176 QTest::newRow("remove multiple, after visible, content not at start")
1177 << 80.0 // show 4-19
1181 QTest::newRow("remove multiple, mix of items from within and after visible items")
1188 void tst_QQuickListView::clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1190 QQuickView *canvas = createView();
1193 for (int i = 0; i < 30; i++)
1194 model.addItem("Item" + QString::number(i), "");
1196 QQmlContext *ctxt = canvas->rootContext();
1197 ctxt->setContextProperty("testModel", &model);
1199 TestObject *testObject = new TestObject;
1200 ctxt->setContextProperty("testObject", testObject);
1202 canvas->setSource(source);
1204 qApp->processEvents();
1206 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1207 QTRY_VERIFY(listview != 0);
1208 QQuickItem *contentItem = listview->contentItem();
1209 QTRY_VERIFY(contentItem != 0);
1211 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1212 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1216 QTRY_COMPARE(findItems<QQuickListView>(contentItem, "wrapper").count(), 0);
1217 QTRY_VERIFY(listview->count() == 0);
1218 QTRY_VERIFY(listview->currentItem() == 0);
1219 if (verticalLayoutDirection == QQuickItemView::TopToBottom)
1220 QTRY_COMPARE(listview->contentY(), 0.0);
1222 QTRY_COMPARE(listview->contentY(), -listview->height());
1223 QVERIFY(listview->currentIndex() == -1);
1225 QCOMPARE(listview->contentHeight(), 0.0);
1227 // confirm sanity when adding an item to cleared list
1228 model.addItem("New", "1");
1229 QTRY_VERIFY(listview->count() == 1);
1230 QVERIFY(listview->currentItem() != 0);
1231 QVERIFY(listview->currentIndex() == 0);
1238 void tst_QQuickListView::moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1240 QFETCH(qreal, contentY);
1244 QFETCH(qreal, itemsOffsetAfterMove);
1248 QQuickView *canvas = getView();
1251 for (int i = 0; i < 30; i++)
1252 model.addItem("Item" + QString::number(i), "");
1254 QQmlContext *ctxt = canvas->rootContext();
1255 ctxt->setContextProperty("testModel", &model);
1257 TestObject *testObject = new TestObject;
1258 ctxt->setContextProperty("testObject", testObject);
1260 canvas->setSource(source);
1262 qApp->processEvents();
1263 QTest::qWaitForWindowShown(canvas);
1265 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1266 QTRY_VERIFY(listview != 0);
1267 QQuickItem *contentItem = listview->contentItem();
1268 QTRY_VERIFY(contentItem != 0);
1270 // always need to wait for view to be painted before the first move()
1271 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1273 bool waitForPolish = (contentY != 0);
1274 if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
1275 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1276 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1277 contentY = -listview->height() - contentY;
1279 listview->setContentY(contentY);
1281 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1283 model.moveItems(from, to, count);
1284 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1286 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1287 int firstVisibleIndex = -1;
1288 for (int i=0; i<items.count(); i++) {
1289 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1290 if (item && delegateVisible(item)) {
1291 firstVisibleIndex = i;
1295 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1297 // Confirm items positioned correctly and indexes correct
1298 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1299 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1300 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1301 qreal pos = i*20.0 + itemsOffsetAfterMove;
1302 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1303 pos = -item->height() - pos;
1304 QTRY_COMPARE(item->y(), pos);
1305 name = findItem<QQuickText>(contentItem, "textName", i);
1307 QTRY_COMPARE(name->text(), model.name(i));
1308 number = findItem<QQuickText>(contentItem, "textNumber", i);
1309 QVERIFY(number != 0);
1310 QTRY_COMPARE(number->text(), model.number(i));
1312 // current index should have been updated
1313 if (item == listview->currentItem())
1314 QTRY_COMPARE(listview->currentIndex(), i);
1317 releaseView(canvas);
1321 void tst_QQuickListView::moved_data()
1323 QTest::addColumn<qreal>("contentY");
1324 QTest::addColumn<int>("from");
1325 QTest::addColumn<int>("to");
1326 QTest::addColumn<int>("count");
1327 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1329 // model starts with 30 items, each 20px high, in area 320px high
1330 // 16 items should be visible at a time
1331 // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved
1333 QTest::newRow("move 1 forwards, within visible items")
1338 QTest::newRow("move 1 forwards, from non-visible -> visible")
1339 << 80.0 // show 4-19
1341 << 20.0; // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement
1343 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1344 << 80.0 // show 4-19
1346 << 20.0; // first item has moved to below item4, everything drops down by size of 1 item
1348 QTest::newRow("move 1 forwards, from visible -> non-visible")
1353 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1359 QTest::newRow("move 1 backwards, within visible items")
1364 QTest::newRow("move 1 backwards, within visible items (to first index)")
1369 QTest::newRow("move 1 backwards, from non-visible -> visible")
1374 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1379 QTest::newRow("move 1 backwards, from visible -> non-visible")
1380 << 80.0 // show 4-19
1382 << -20.0; // to minimize movement, item 0 moves to -20, and other items do not move
1384 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1385 << 80.0 // show 4-19
1387 << -20.0; // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move
1390 QTest::newRow("move multiple forwards, within visible items")
1395 QTest::newRow("move multiple forwards, before visible items")
1396 << 140.0 // show 7-22
1397 << 4 << 5 << 3 // 4,5,6 move to below 7
1398 << 20.0 * 3; // 4,5,6 moved down
1400 QTest::newRow("move multiple forwards, from non-visible -> visible")
1401 << 80.0 // show 4-19
1403 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1405 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1406 << 80.0 // show 4-19
1408 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1410 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1413 << 20.0; // item 1,2 are removed, item 3 is now first visible
1415 QTest::newRow("move multiple forwards, to bottom of view")
1420 QTest::newRow("move multiple forwards, to bottom of view, first->last")
1425 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1430 QTest::newRow("move multiple forwards, from visible -> non-visible")
1435 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1441 QTest::newRow("move multiple backwards, within visible items")
1446 QTest::newRow("move multiple backwards, within visible items (move first item)")
1451 QTest::newRow("move multiple backwards, from non-visible -> visible")
1456 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1461 QTest::newRow("move multiple backwards, from visible -> non-visible")
1462 << 80.0 // show 4-19
1464 << -20.0 * 3; // to minimize movement, 0 moves by -60, and other items do not move
1466 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1467 << 80.0 // show 4-19
1469 << -20.0 * 3; // to minimize movement, 16,17,18 move to above item 0, and other items do not move
1472 void tst_QQuickListView::multipleChanges(bool condensed)
1474 QFETCH(int, startCount);
1475 QFETCH(QList<ListChange>, changes);
1476 QFETCH(int, newCount);
1477 QFETCH(int, newCurrentIndex);
1479 QQuickView *canvas = getView();
1482 for (int i = 0; i < startCount; i++)
1483 model.addItem("Item" + QString::number(i), "");
1485 QQmlContext *ctxt = canvas->rootContext();
1486 ctxt->setContextProperty("testModel", &model);
1488 TestObject *testObject = new TestObject;
1489 ctxt->setContextProperty("testObject", testObject);
1491 canvas->setSource(testFileUrl("listviewtest.qml"));
1493 qApp->processEvents();
1494 QTest::qWaitForWindowShown(canvas);
1496 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1497 QTRY_VERIFY(listview != 0);
1498 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1500 for (int i=0; i<changes.count(); i++) {
1501 switch (changes[i].type) {
1502 case ListChange::Inserted:
1504 QList<QPair<QString, QString> > items;
1505 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1506 items << qMakePair(QString("new item %1").arg(j), QString::number(j));
1507 model.insertItems(changes[i].index, items);
1510 case ListChange::Removed:
1511 model.removeItems(changes[i].index, changes[i].count);
1513 case ListChange::Moved:
1514 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1516 case ListChange::SetCurrent:
1517 listview->setCurrentIndex(changes[i].index);
1519 case ListChange::SetContentY:
1520 listview->setContentY(changes[i].pos);
1526 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1529 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1531 QCOMPARE(listview->count(), newCount);
1532 QCOMPARE(listview->count(), model.count());
1533 QCOMPARE(listview->currentIndex(), newCurrentIndex);
1537 QQuickItem *contentItem = listview->contentItem();
1538 QTRY_VERIFY(contentItem != 0);
1539 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1540 for (int i=0; i < model.count() && i < itemCount; ++i) {
1541 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1542 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1543 name = findItem<QQuickText>(contentItem, "textName", i);
1545 QTRY_COMPARE(name->text(), model.name(i));
1546 number = findItem<QQuickText>(contentItem, "textNumber", i);
1547 QVERIFY(number != 0);
1548 QTRY_COMPARE(number->text(), model.number(i));
1552 releaseView(canvas);
1555 void tst_QQuickListView::multipleChanges_data()
1557 QTest::addColumn<int>("startCount");
1558 QTest::addColumn<QList<ListChange> >("changes");
1559 QTest::addColumn<int>("newCount");
1560 QTest::addColumn<int>("newCurrentIndex");
1562 QList<ListChange> changes;
1564 for (int i=1; i<30; i++)
1565 changes << ListChange::remove(0);
1566 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1568 changes << ListChange::remove(0);
1569 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1572 changes << ListChange::setCurrent(29);
1573 for (int i=29; i>0; i--)
1574 changes << ListChange::remove(i);
1575 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1577 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1578 << ListChange::remove(0, 1)
1579 << ListChange::insert(0, 1)
1582 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1583 << ListChange::setCurrent(2)
1584 << ListChange::remove(2, 1)
1585 << ListChange::insert(2, 1)
1588 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1589 << ListChange::setCurrent(1)
1590 << ListChange::remove(1, 3)
1591 << ListChange::insert(2, 2)
1594 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1595 << ListChange::setCurrent(2)
1596 << ListChange::remove(1, 3)
1597 << ListChange::move(1, 5, 1)
1600 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1601 << ListChange::setCurrent(5)
1602 << ListChange::remove(4, 3)
1603 << ListChange::move(4, 1, 1)
1607 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1608 << ListChange::insert(0, 2)
1609 << ListChange::insert(0, 4)
1610 << ListChange::insert(0, 6)
1613 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1614 << ListChange::insert(0, 2)
1615 << ListChange::insert(0, 4)
1616 << ListChange::insert(0, 6)
1617 << ListChange::setCurrent(3)
1618 << ListChange::insert(3, 2)
1621 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1622 << ListChange::insert(0, 30)
1623 << ListChange::remove(0, 30)
1626 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1627 << ListChange::insert(1)
1628 << ListChange::setCurrent(1)
1629 << ListChange::remove(1)
1632 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1633 << ListChange::insert(0, 10)
1634 << ListChange::remove(5, 10)
1637 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1638 << ListChange::insert(0, 3)
1639 << ListChange::move(0, 10, 3)
1642 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1643 << ListChange::insert(0, 3)
1644 << ListChange::move(0, 8, 5)
1647 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1648 << ListChange::setCurrent(9)
1649 << ListChange::insert(10, 3)
1650 << ListChange::move(8, 0, 5)
1654 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1655 << ListChange::setCurrent(1)
1656 << ListChange::move(1, 2, 2)
1657 << ListChange::move(2, 1, 2)
1660 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1661 << ListChange::setCurrent(2)
1662 << ListChange::move(1, 2, 3)
1663 << ListChange::move(3, 0, 5)
1666 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1667 << ListChange::setCurrent(5)
1668 << ListChange::move(5, 0, 1)
1669 << ListChange::remove(0)
1672 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1673 << ListChange::setCurrent(5)
1674 << ListChange::move(5, 0, 1)
1675 << ListChange::insert(0)
1678 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1679 << ListChange::setCurrent(1)
1680 << ListChange::move(5, 1, 3)
1681 << ListChange::remove(1, 3)
1684 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1685 << ListChange::setCurrent(5)
1686 << ListChange::move(5, 1, 3)
1687 << ListChange::insert(1, 5)
1690 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1691 << ListChange::setCurrent(3)
1692 << ListChange::move(0, 1, 2)
1693 << ListChange::insert(3, 5)
1696 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1697 << ListChange::insert(0, 5)
1698 << ListChange::setCurrent(-1)
1699 << ListChange::remove(0, 5)
1700 << ListChange::insert(0, 5)
1703 QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1704 << ListChange::remove(20, 5)
1705 << ListChange::setContentY(20)
1708 QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1709 << ListChange::insert(9, 5)
1710 << ListChange::setContentY(20)
1713 QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1714 << ListChange::move(15, 8, 3)
1715 << ListChange::setContentY(0)
1718 QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1719 << ListChange::setContentY(20)
1720 << ListChange::remove(0, 30)
1721 << ListChange::insert(0, 2)
1722 << ListChange::setContentY(0)
1726 void tst_QQuickListView::swapWithFirstItem()
1728 QQuickView *canvas = createView();
1731 for (int i = 0; i < 30; i++)
1732 model.addItem("Item" + QString::number(i), "");
1734 QQmlContext *ctxt = canvas->rootContext();
1735 ctxt->setContextProperty("testModel", &model);
1737 TestObject *testObject = new TestObject;
1738 ctxt->setContextProperty("testObject", testObject);
1740 canvas->setSource(testFileUrl("listviewtest.qml"));
1742 qApp->processEvents();
1744 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1745 QTRY_VERIFY(listview != 0);
1746 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1748 // ensure content position is stable
1749 listview->setContentY(0);
1750 model.moveItem(1, 0);
1751 QTRY_VERIFY(listview->contentY() == 0);
1757 void tst_QQuickListView::enforceRange()
1759 QQuickView *canvas = createView();
1762 for (int i = 0; i < 30; i++)
1763 model.addItem("Item" + QString::number(i), "");
1765 QQmlContext *ctxt = canvas->rootContext();
1766 ctxt->setContextProperty("testModel", &model);
1768 canvas->setSource(testFileUrl("listview-enforcerange.qml"));
1770 qApp->processEvents();
1772 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1773 QTRY_VERIFY(listview != 0);
1775 QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0);
1776 QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0);
1777 QTRY_COMPARE(listview->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
1778 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1780 QQuickItem *contentItem = listview->contentItem();
1781 QTRY_VERIFY(contentItem != 0);
1783 // view should be positioned at the top of the range.
1784 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
1786 QTRY_COMPARE(listview->contentY(), -100.0);
1788 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1789 QTRY_VERIFY(name != 0);
1790 QTRY_COMPARE(name->text(), model.name(0));
1791 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
1792 QTRY_VERIFY(number != 0);
1793 QTRY_COMPARE(number->text(), model.number(0));
1795 // Check currentIndex is updated when contentItem moves
1796 listview->setContentY(20);
1798 QTRY_COMPARE(listview->currentIndex(), 6);
1801 QmlListModel model2;
1802 for (int i = 0; i < 5; i++)
1803 model2.addItem("Item" + QString::number(i), "");
1805 ctxt->setContextProperty("testModel", &model2);
1806 QCOMPARE(listview->count(), 5);
1811 void tst_QQuickListView::enforceRange_withoutHighlight()
1814 // If no highlight is set but StrictlyEnforceRange is used, the content should still move
1815 // to the correct position (i.e. to the next/previous item, not next/previous section)
1816 // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex()
1818 QQuickView *canvas = createView();
1821 model.addItem("Item 0", "a");
1822 model.addItem("Item 1", "b");
1823 model.addItem("Item 2", "b");
1824 model.addItem("Item 3", "c");
1826 QQmlContext *ctxt = canvas->rootContext();
1827 ctxt->setContextProperty("testModel", &model);
1829 canvas->setSource(testFileUrl("listview-enforcerange-nohighlight.qml"));
1831 qApp->processEvents();
1833 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1834 QTRY_VERIFY(listview != 0);
1835 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1837 qreal expectedPos = -100.0;
1839 expectedPos += 10.0; // scroll past 1st section's delegate (10px height)
1840 QTRY_COMPARE(listview->contentY(), expectedPos);
1842 expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section
1843 QTest::keyClick(canvas, Qt::Key_Down);
1845 QTRY_COMPARE(listview->contentY(), expectedPos);
1847 expectedPos += 20; // scroll past 1st item of 2nd section
1848 QTest::keyClick(canvas, Qt::Key_Down);
1849 QTRY_COMPARE(listview->contentY(), expectedPos);
1851 expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section
1852 QTest::keyClick(canvas, Qt::Key_Down);
1853 QTRY_COMPARE(listview->contentY(), expectedPos);
1858 void tst_QQuickListView::spacing()
1860 QQuickView *canvas = createView();
1863 for (int i = 0; i < 30; i++)
1864 model.addItem("Item" + QString::number(i), "");
1866 QQmlContext *ctxt = canvas->rootContext();
1867 ctxt->setContextProperty("testModel", &model);
1869 TestObject *testObject = new TestObject;
1870 ctxt->setContextProperty("testObject", testObject);
1872 canvas->setSource(testFileUrl("listviewtest.qml"));
1874 qApp->processEvents();
1876 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1877 QTRY_VERIFY(listview != 0);
1879 QQuickItem *contentItem = listview->contentItem();
1880 QTRY_VERIFY(contentItem != 0);
1881 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1883 // Confirm items positioned correctly
1884 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1885 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1886 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1887 if (!item) qWarning() << "Item" << i << "not found";
1889 QTRY_VERIFY(item->y() == i*20);
1892 listview->setSpacing(10);
1893 QTRY_VERIFY(listview->spacing() == 10);
1895 // Confirm items positioned correctly
1896 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 11);
1897 for (int i = 0; i < 11; ++i) {
1898 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1899 if (!item) qWarning() << "Item" << i << "not found";
1901 QTRY_VERIFY(item->y() == i*30);
1904 listview->setSpacing(0);
1906 // Confirm items positioned correctly
1907 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() >= 16);
1908 for (int i = 0; i < 16; ++i) {
1909 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1910 if (!item) qWarning() << "Item" << i << "not found";
1912 QTRY_COMPARE(item->y(), i*20.0);
1919 template <typename T>
1920 void tst_QQuickListView::sections(const QUrl &source)
1922 QQuickView *canvas = createView();
1925 for (int i = 0; i < 30; i++)
1926 model.addItem("Item" + QString::number(i), QString::number(i/5));
1928 QQmlContext *ctxt = canvas->rootContext();
1929 ctxt->setContextProperty("testModel", &model);
1931 canvas->setSource(source);
1933 qApp->processEvents();
1935 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1936 QTRY_VERIFY(listview != 0);
1938 QQuickItem *contentItem = listview->contentItem();
1939 QTRY_VERIFY(contentItem != 0);
1941 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1943 // Confirm items positioned correctly
1944 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1945 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1946 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1948 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20));
1949 QQuickText *next = findItem<QQuickText>(item, "nextSection");
1950 QCOMPARE(next->text().toInt(), (i+1)/5);
1953 QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1955 // Remove section boundary
1956 model.removeItem(5);
1957 QTRY_COMPARE(listview->count(), model.count());
1959 // New section header created
1960 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1962 QTRY_COMPARE(item->height(), 40.0);
1964 model.insertItem(3, "New Item", "0");
1965 QTRY_COMPARE(listview->count(), model.count());
1967 // Section header moved
1968 item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1970 QTRY_COMPARE(item->height(), 20.0);
1972 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1974 QTRY_COMPARE(item->height(), 40.0);
1976 // insert item which will become a section header
1977 model.insertItem(6, "Replace header", "1");
1978 QTRY_COMPARE(listview->count(), model.count());
1980 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1982 QTRY_COMPARE(item->height(), 40.0);
1984 item = findItem<QQuickItem>(contentItem, "wrapper", 7);
1986 QTRY_COMPARE(item->height(), 20.0);
1988 QTRY_COMPARE(listview->currentSection(), QString("0"));
1990 listview->setContentY(140);
1991 QTRY_COMPARE(listview->currentSection(), QString("1"));
1993 QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
1995 listview->setContentY(20);
1996 QTRY_COMPARE(listview->currentSection(), QString("0"));
1998 QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
2000 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2002 QTRY_COMPARE(item->height(), 20.0);
2004 // check that headers change when item changes
2005 listview->setContentY(0);
2006 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2007 model.modifyItem(0, "changed", "2");
2008 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2010 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2012 QTRY_COMPARE(item->height(), 40.0);
2017 void tst_QQuickListView::sectionsDelegate()
2019 QSKIP("QTBUG-24395");
2021 QQuickView *canvas = createView();
2024 for (int i = 0; i < 30; i++)
2025 model.addItem("Item" + QString::number(i), QString::number(i/5));
2027 QQmlContext *ctxt = canvas->rootContext();
2028 ctxt->setContextProperty("testModel", &model);
2030 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2032 qApp->processEvents();
2034 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2035 QTRY_VERIFY(listview != 0);
2037 QQuickItem *contentItem = listview->contentItem();
2038 QTRY_VERIFY(contentItem != 0);
2040 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2042 // Confirm items positioned correctly
2043 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2044 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2045 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2047 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20));
2048 QQuickText *next = findItem<QQuickText>(item, "nextSection");
2049 QCOMPARE(next->text().toInt(), (i+1)/5);
2052 for (int i = 0; i < 3; ++i) {
2053 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2055 QTRY_COMPARE(item->y(), qreal(i*20*6));
2058 // ensure section header is maintained in view
2059 listview->setCurrentIndex(20);
2060 QTRY_VERIFY(listview->contentY() >= 200.0);
2061 listview->setCurrentIndex(0);
2062 QTRY_COMPARE(listview->contentY(), 0.0);
2065 model.modifyItem(0, "One", "aaa");
2066 model.modifyItem(1, "Two", "aaa");
2067 model.modifyItem(2, "Three", "aaa");
2068 model.modifyItem(3, "Four", "aaa");
2069 model.modifyItem(4, "Five", "aaa");
2070 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2072 for (int i = 0; i < 3; ++i) {
2073 QQuickItem *item = findItem<QQuickItem>(contentItem,
2074 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2076 QTRY_COMPARE(item->y(), qreal(i*20*6));
2079 // remove section boundary
2080 model.removeItem(5);
2081 QTRY_COMPARE(listview->count(), model.count());
2082 for (int i = 0; i < 3; ++i) {
2083 QQuickItem *item = findItem<QQuickItem>(contentItem,
2084 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2089 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "sect_1");
2090 QCOMPARE(items.count(), 1);
2093 model.modifyItem(0, "One", "aaa");
2094 model.modifyItem(1, "One", "aaa");
2095 model.modifyItem(2, "One", "aaa");
2096 model.modifyItem(3, "Four", "aaa");
2097 model.modifyItem(4, "Four", "aaa");
2098 model.modifyItem(5, "Four", "aaa");
2099 model.modifyItem(6, "Five", "aaa");
2100 model.modifyItem(7, "Five", "aaa");
2101 model.modifyItem(8, "Five", "aaa");
2102 model.modifyItem(9, "Two", "aaa");
2103 model.modifyItem(10, "Two", "aaa");
2104 model.modifyItem(11, "Two", "aaa");
2105 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2106 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_aaa").count(), 1);
2107 canvas->rootObject()->setProperty("sectionProperty", "name");
2108 // ensure view has settled.
2109 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_Four").count(), 1);
2110 for (int i = 0; i < 4; ++i) {
2111 QQuickItem *item = findItem<QQuickItem>(contentItem,
2112 "sect_" + model.name(i*3));
2114 QTRY_COMPARE(item->y(), qreal(i*20*4));
2120 void tst_QQuickListView::sectionsDragOutsideBounds_data()
2122 QTest::addColumn<int>("distance");
2123 QTest::addColumn<int>("cacheBuffer");
2125 QTest::newRow("500, no cache buffer") << 500 << 0;
2126 QTest::newRow("1000, no cache buffer") << 1000 << 0;
2127 QTest::newRow("500, cache buffer") << 500 << 320;
2128 QTest::newRow("1000, cache buffer") << 1000 << 320;
2131 void tst_QQuickListView::sectionsDragOutsideBounds()
2133 QFETCH(int, distance);
2134 QFETCH(int, cacheBuffer);
2136 QQuickView *canvas = getView();
2139 for (int i = 0; i < 10; i++)
2140 model.addItem("Item" + QString::number(i), QString::number(i/5));
2142 QQmlContext *ctxt = canvas->rootContext();
2143 ctxt->setContextProperty("testModel", &model);
2145 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2147 qApp->processEvents();
2149 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2150 QTRY_VERIFY(listview != 0);
2151 listview->setCacheBuffer(cacheBuffer);
2153 QQuickItem *contentItem = listview->contentItem();
2154 QTRY_VERIFY(contentItem != 0);
2156 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2159 // Drag view up beyond bounds
2160 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20));
2161 QTest::mouseMove(canvas, QPoint(20,0));
2162 QTest::mouseMove(canvas, QPoint(20,-50));
2163 QTest::mouseMove(canvas, QPoint(20,-distance));
2164 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-distance));
2165 // view should settle back at 0
2166 QTRY_COMPARE(listview->contentY(), 0.0);
2168 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,0));
2169 QTest::mouseMove(canvas, QPoint(20,20));
2170 QTest::mouseMove(canvas, QPoint(20,70));
2171 QTest::mouseMove(canvas, QPoint(20,distance));
2173 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,distance));
2174 // view should settle back at 0
2175 QTRY_COMPARE(listview->contentY(), 0.0);
2177 releaseView(canvas);
2180 void tst_QQuickListView::sectionsPositioning()
2182 QQuickView *canvas = createView();
2185 for (int i = 0; i < 30; i++)
2186 model.addItem("Item" + QString::number(i), QString::number(i/5));
2188 QQmlContext *ctxt = canvas->rootContext();
2189 ctxt->setContextProperty("testModel", &model);
2191 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2193 qApp->processEvents();
2194 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2196 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2197 QTRY_VERIFY(listview != 0);
2198 QQuickItem *contentItem = listview->contentItem();
2199 QTRY_VERIFY(contentItem != 0);
2200 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2202 for (int i = 0; i < 3; ++i) {
2203 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2205 QTRY_COMPARE(item->y(), qreal(i*20*6));
2208 QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header
2210 QCOMPARE(topItem->y(), 0.);
2212 QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2213 QVERIFY(bottomItem);
2214 QCOMPARE(bottomItem->y(), 300.);
2216 // move down a little and check that section header is at top
2217 listview->setContentY(10);
2218 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2219 QCOMPARE(topItem->y(), 0.);
2221 // push the top header up
2222 listview->setContentY(110);
2223 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2224 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2226 QCOMPARE(topItem->y(), 100.);
2228 QQuickItem *item = findVisibleChild(contentItem, "sect_1");
2230 QCOMPARE(item->y(), 120.);
2232 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2233 QVERIFY(bottomItem);
2234 QCOMPARE(bottomItem->y(), 410.);
2236 // Move past section 0
2237 listview->setContentY(120);
2238 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2239 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2242 // Push section footer down
2243 listview->setContentY(70);
2244 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2245 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2246 QVERIFY(bottomItem);
2247 QCOMPARE(bottomItem->y(), 380.);
2249 // Change current section, and verify case insensitive comparison
2250 listview->setContentY(10);
2251 model.modifyItem(0, "One", "aaa");
2252 model.modifyItem(1, "Two", "AAA");
2253 model.modifyItem(2, "Three", "aAa");
2254 model.modifyItem(3, "Four", "aaA");
2255 model.modifyItem(4, "Five", "Aaa");
2256 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2258 QTRY_COMPARE(listview->currentSection(), QString("aaa"));
2260 for (int i = 0; i < 3; ++i) {
2261 QQuickItem *item = findItem<QQuickItem>(contentItem,
2262 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2264 QTRY_COMPARE(item->y(), qreal(i*20*6));
2267 QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header
2268 QCOMPARE(topItem->y(), 10.);
2270 // remove section boundary
2271 listview->setContentY(120);
2272 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2273 model.removeItem(5);
2274 QTRY_COMPARE(listview->count(), model.count());
2275 for (int i = 1; i < 3; ++i) {
2276 QQuickItem *item = findVisibleChild(contentItem,
2277 "sect_" + QString::number(i));
2279 QTRY_COMPARE(item->y(), qreal(i*20*6));
2282 QVERIFY(topItem = findVisibleChild(contentItem, "sect_1"));
2283 QTRY_COMPARE(topItem->y(), 120.);
2285 // Change the next section
2286 listview->setContentY(0);
2287 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2288 bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2289 QVERIFY(bottomItem);
2290 QTRY_COMPARE(bottomItem->y(), 300.);
2292 model.modifyItem(14, "New", "new");
2293 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2295 QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer
2296 QTRY_COMPARE(bottomItem->y(), 300.);
2298 // Turn sticky footer off
2299 listview->setContentY(20);
2300 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2301 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2302 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2303 QCOMPARE(item->y(), 340.);
2305 // Turn sticky header off
2306 listview->setContentY(30);
2307 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2308 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2309 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2310 QCOMPARE(item->y(), 0.);
2312 // if an empty model is set the header/footer should be cleaned up
2313 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2314 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2315 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2316 QmlListModel model1;
2317 ctxt->setContextProperty("testModel", &model1);
2318 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2319 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2321 // clear model - header/footer should be cleaned up
2322 ctxt->setContextProperty("testModel", &model);
2323 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2324 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2326 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2327 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2332 void tst_QQuickListView::sectionPropertyChange()
2334 QQuickView *canvas = createView();
2336 canvas->setSource(testFileUrl("sectionpropertychange.qml"));
2338 qApp->processEvents();
2340 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2341 QTRY_VERIFY(listview != 0);
2343 QQuickItem *contentItem = listview->contentItem();
2344 QTRY_VERIFY(contentItem != 0);
2346 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2348 // Confirm items positioned correctly
2349 for (int i = 0; i < 2; ++i) {
2350 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2352 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2355 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2356 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2358 // Confirm items positioned correctly
2359 for (int i = 0; i < 2; ++i) {
2360 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2362 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2365 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2366 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2368 // Confirm items positioned correctly
2369 for (int i = 0; i < 2; ++i) {
2370 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2372 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2378 void tst_QQuickListView::currentIndex_delayedItemCreation()
2380 QFETCH(bool, setCurrentToZero);
2382 QQuickView *canvas = getView();
2384 // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2385 // (since the currentItem will have changed and that shares the same index)
2386 canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2388 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2389 qApp->processEvents();
2391 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2392 QTRY_VERIFY(listview != 0);
2393 QQuickItem *contentItem = listview->contentItem();
2394 QTRY_VERIFY(contentItem != 0);
2396 QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2397 QCOMPARE(listview->currentIndex(), 0);
2398 QTRY_COMPARE(spy.count(), 1);
2400 releaseView(canvas);
2403 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2405 QTest::addColumn<bool>("setCurrentToZero");
2407 QTest::newRow("set to 0") << true;
2408 QTest::newRow("don't set to 0") << false;
2411 void tst_QQuickListView::currentIndex()
2414 for (int i = 0; i < 30; i++)
2415 model.addItem("Item" + QString::number(i), QString::number(i));
2417 QQuickView *canvas = new QQuickView(0);
2418 canvas->setGeometry(0,0,240,320);
2420 QQmlContext *ctxt = canvas->rootContext();
2421 ctxt->setContextProperty("testModel", &model);
2422 ctxt->setContextProperty("testWrap", QVariant(false));
2424 QString filename(testFile("listview-initCurrent.qml"));
2425 canvas->setSource(QUrl::fromLocalFile(filename));
2427 qApp->processEvents();
2429 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2430 QTRY_VERIFY(listview != 0);
2431 QQuickItem *contentItem = listview->contentItem();
2432 QTRY_VERIFY(contentItem != 0);
2433 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2435 // current item should be 20th item at startup
2436 // and current item should be in view
2437 QCOMPARE(listview->currentIndex(), 20);
2438 QCOMPARE(listview->contentY(), 100.0);
2439 QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2440 QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2442 listview->setCurrentIndex(0);
2443 QCOMPARE(listview->currentIndex(), 0);
2444 // confirm that the velocity is updated
2445 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2447 // footer should become visible if it is out of view, and then current index is set to count-1
2448 canvas->rootObject()->setProperty("showFooter", true);
2449 QTRY_VERIFY(listview->footerItem());
2450 listview->setCurrentIndex(model.count()-2);
2451 QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2452 listview->setCurrentIndex(model.count()-1);
2453 QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2454 canvas->rootObject()->setProperty("showFooter", false);
2456 // header should become visible if it is out of view, and then current index is set to 0
2457 canvas->rootObject()->setProperty("showHeader", true);
2458 QTRY_VERIFY(listview->headerItem());
2459 listview->setCurrentIndex(1);
2460 QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2461 listview->setCurrentIndex(0);
2462 QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2463 canvas->rootObject()->setProperty("showHeader", false);
2465 // turn off auto highlight
2466 listview->setHighlightFollowsCurrentItem(false);
2467 QVERIFY(listview->highlightFollowsCurrentItem() == false);
2469 QVERIFY(listview->highlightItem());
2470 qreal hlPos = listview->highlightItem()->y();
2472 listview->setCurrentIndex(4);
2473 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2475 // insert item before currentIndex
2476 listview->setCurrentIndex(28);
2477 model.insertItem(0, "Foo", "1111");
2478 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2480 // check removing highlight by setting currentIndex to -1;
2481 listview->setCurrentIndex(-1);
2483 QCOMPARE(listview->currentIndex(), -1);
2484 QVERIFY(!listview->highlightItem());
2485 QVERIFY(!listview->currentItem());
2487 // moving currentItem out of view should make it invisible
2488 listview->setCurrentIndex(0);
2489 QTRY_VERIFY(delegateVisible(listview->currentItem()));
2490 listview->setContentY(200);
2491 QTRY_VERIFY(!delegateVisible(listview->currentItem()));
2496 void tst_QQuickListView::noCurrentIndex()
2499 for (int i = 0; i < 30; i++)
2500 model.addItem("Item" + QString::number(i), QString::number(i));
2502 QQuickView *canvas = new QQuickView(0);
2503 canvas->setGeometry(0,0,240,320);
2505 QQmlContext *ctxt = canvas->rootContext();
2506 ctxt->setContextProperty("testModel", &model);
2508 QString filename(testFile("listview-noCurrent.qml"));
2509 canvas->setSource(QUrl::fromLocalFile(filename));
2511 qApp->processEvents();
2513 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2514 QTRY_VERIFY(listview != 0);
2515 QQuickItem *contentItem = listview->contentItem();
2516 QTRY_VERIFY(contentItem != 0);
2517 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2519 // current index should be -1 at startup
2520 // and we should not have a currentItem or highlightItem
2521 QCOMPARE(listview->currentIndex(), -1);
2522 QCOMPARE(listview->contentY(), 0.0);
2523 QVERIFY(!listview->highlightItem());
2524 QVERIFY(!listview->currentItem());
2526 listview->setCurrentIndex(2);
2527 QCOMPARE(listview->currentIndex(), 2);
2528 QVERIFY(listview->highlightItem());
2529 QVERIFY(listview->currentItem());
2534 void tst_QQuickListView::keyNavigation()
2536 QFETCH(QQuickListView::Orientation, orientation);
2537 QFETCH(Qt::LayoutDirection, layoutDirection);
2538 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
2539 QFETCH(Qt::Key, forwardsKey);
2540 QFETCH(Qt::Key, backwardsKey);
2541 QFETCH(QPointF, contentPosAtFirstItem);
2542 QFETCH(QPointF, contentPosAtLastItem);
2545 for (int i = 0; i < 30; i++)
2546 model.addItem("Item" + QString::number(i), "");
2548 QQuickView *canvas = getView();
2549 TestObject *testObject = new TestObject;
2550 canvas->rootContext()->setContextProperty("testModel", &model);
2551 canvas->rootContext()->setContextProperty("testObject", testObject);
2552 canvas->setSource(testFileUrl("listviewtest.qml"));
2554 qApp->processEvents();
2556 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2557 QTRY_VERIFY(listview != 0);
2559 listview->setOrientation(orientation);
2560 listview->setLayoutDirection(layoutDirection);
2561 listview->setVerticalLayoutDirection(verticalLayoutDirection);
2562 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2564 canvas->requestActivateWindow();
2565 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2566 QCOMPARE(listview->currentIndex(), 0);
2568 QTest::keyClick(canvas, forwardsKey);
2569 QCOMPARE(listview->currentIndex(), 1);
2571 QTest::keyClick(canvas, backwardsKey);
2572 QCOMPARE(listview->currentIndex(), 0);
2574 // hold down a key to go forwards
2575 for (int i=0; i<model.count()-1; i++) {
2576 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
2577 QTRY_COMPARE(listview->currentIndex(), i+1);
2579 QTest::keyRelease(canvas, forwardsKey);
2580 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2581 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2582 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2584 // hold down a key to go backwards
2585 for (int i=model.count()-1; i > 0; i--) {
2586 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2587 QTRY_COMPARE(listview->currentIndex(), i-1);
2589 QTest::keyRelease(canvas, backwardsKey);
2590 QTRY_COMPARE(listview->currentIndex(), 0);
2591 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2592 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2595 QVERIFY(!listview->isWrapEnabled());
2596 listview->incrementCurrentIndex();
2597 QCOMPARE(listview->currentIndex(), 1);
2598 listview->decrementCurrentIndex();
2599 QCOMPARE(listview->currentIndex(), 0);
2601 listview->decrementCurrentIndex();
2602 QCOMPARE(listview->currentIndex(), 0);
2605 listview->setWrapEnabled(true);
2606 QVERIFY(listview->isWrapEnabled());
2608 listview->decrementCurrentIndex();
2609 QCOMPARE(listview->currentIndex(), model.count()-1);
2610 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2611 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2613 listview->incrementCurrentIndex();
2614 QCOMPARE(listview->currentIndex(), 0);
2615 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2616 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2618 releaseView(canvas);
2622 void tst_QQuickListView::keyNavigation_data()
2624 QTest::addColumn<QQuickListView::Orientation>("orientation");
2625 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2626 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2627 QTest::addColumn<Qt::Key>("forwardsKey");
2628 QTest::addColumn<Qt::Key>("backwardsKey");
2629 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2630 QTest::addColumn<QPointF>("contentPosAtLastItem");
2632 QTest::newRow("Vertical, TopToBottom")
2633 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2634 << Qt::Key_Down << Qt::Key_Up
2636 << QPointF(0, 30*20 - 320);
2638 QTest::newRow("Vertical, BottomToTop")
2639 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2640 << Qt::Key_Up << Qt::Key_Down
2642 << QPointF(0, -(30 * 20));
2644 QTest::newRow("Horizontal, LeftToRight")
2645 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2646 << Qt::Key_Right << Qt::Key_Left
2648 << QPointF(30*240 - 240, 0);
2650 QTest::newRow("Horizontal, RightToLeft")
2651 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2652 << Qt::Key_Left << Qt::Key_Right
2654 << QPointF(-(30 * 240), 0);
2657 void tst_QQuickListView::itemList()
2659 QQuickView *canvas = createView();
2660 canvas->setSource(testFileUrl("itemlist.qml"));
2662 qApp->processEvents();
2664 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2665 QTRY_VERIFY(listview != 0);
2667 QQuickItem *contentItem = listview->contentItem();
2668 QTRY_VERIFY(contentItem != 0);
2670 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2671 QTRY_VERIFY(model != 0);
2673 QTRY_VERIFY(model->count() == 3);
2674 QTRY_COMPARE(listview->currentIndex(), 0);
2676 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2678 QTRY_COMPARE(item->x(), 0.0);
2679 QCOMPARE(item->height(), listview->height());
2681 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2683 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2685 listview->setCurrentIndex(2);
2687 item = findItem<QQuickItem>(contentItem, "item3");
2689 QTRY_COMPARE(item->x(), 480.0);
2691 text = findItem<QQuickText>(contentItem, "text3");
2693 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2698 void tst_QQuickListView::itemListFlicker()
2700 QQuickView *canvas = createView();
2701 canvas->setSource(testFileUrl("itemlist-flicker.qml"));
2703 qApp->processEvents();
2705 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2706 QTRY_VERIFY(listview != 0);
2708 QQuickItem *contentItem = listview->contentItem();
2709 QTRY_VERIFY(contentItem != 0);
2711 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2712 QTRY_VERIFY(model != 0);
2714 QTRY_VERIFY(model->count() == 3);
2715 QTRY_COMPARE(listview->currentIndex(), 0);
2719 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2720 QVERIFY(delegateVisible(item));
2721 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2722 QVERIFY(delegateVisible(item));
2723 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2724 QVERIFY(delegateVisible(item));
2726 listview->setCurrentIndex(1);
2728 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2729 QVERIFY(delegateVisible(item));
2730 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2731 QVERIFY(delegateVisible(item));
2732 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2733 QVERIFY(delegateVisible(item));
2735 listview->setCurrentIndex(2);
2737 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2738 QVERIFY(delegateVisible(item));
2739 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2740 QVERIFY(delegateVisible(item));
2741 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2742 QVERIFY(delegateVisible(item));
2747 void tst_QQuickListView::cacheBuffer()
2749 QQuickView *canvas = createView();
2752 for (int i = 0; i < 90; i++)
2753 model.addItem("Item" + QString::number(i), "");
2755 QQmlContext *ctxt = canvas->rootContext();
2756 ctxt->setContextProperty("testModel", &model);
2758 TestObject *testObject = new TestObject;
2759 ctxt->setContextProperty("testObject", testObject);
2761 canvas->setSource(testFileUrl("listviewtest.qml"));
2763 qApp->processEvents();
2765 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2766 QTRY_VERIFY(listview != 0);
2768 QQuickItem *contentItem = listview->contentItem();
2769 QTRY_VERIFY(contentItem != 0);
2770 QTRY_VERIFY(listview->delegate() != 0);
2771 QTRY_VERIFY(listview->model() != 0);
2772 QTRY_VERIFY(listview->highlight() != 0);
2774 // Confirm items positioned correctly
2775 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2776 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2777 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2778 if (!item) qWarning() << "Item" << i << "not found";
2780 QTRY_VERIFY(item->y() == i*20);
2783 QQmlIncubationController controller;
2784 canvas->engine()->setIncubationController(&controller);
2786 testObject->setCacheBuffer(200);
2787 QTRY_VERIFY(listview->cacheBuffer() == 200);
2789 // items will be created one at a time
2790 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2791 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2792 QQuickItem *item = 0;
2795 controller.incubateWhile(&b);
2796 item = findItem<QQuickItem>(listview, "wrapper", i);
2802 controller.incubateWhile(&b);
2805 int newItemCount = 0;
2806 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2808 // Confirm items positioned correctly
2809 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2810 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2811 if (!item) qWarning() << "Item" << i << "not found";
2813 QTRY_VERIFY(item->y() == i*20);
2816 // move view and confirm items in view are visible immediately and outside are created async
2817 listview->setContentY(300);
2819 for (int i = 15; i < 32; ++i) {
2820 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2821 if (!item) qWarning() << "Item" << i << "not found";
2823 QVERIFY(item->y() == i*20);
2826 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2828 // ensure buffered items are created
2829 for (int i = 32; i < qMin(41,model.count()); ++i) {
2830 QQuickItem *item = 0;
2832 qGuiApp->processEvents(); // allow refill to happen
2834 controller.incubateWhile(&b);
2835 item = findItem<QQuickItem>(listview, "wrapper", i);
2841 controller.incubateWhile(&b);
2848 void tst_QQuickListView::positionViewAtIndex()
2850 QQuickView *canvas = createView();
2853 for (int i = 0; i < 40; i++)
2854 model.addItem("Item" + QString::number(i), "");
2856 QQmlContext *ctxt = canvas->rootContext();
2857 ctxt->setContextProperty("testModel", &model);
2859 TestObject *testObject = new TestObject;
2860 ctxt->setContextProperty("testObject", testObject);
2862 canvas->setSource(testFileUrl("listviewtest.qml"));
2863 qApp->processEvents();
2865 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2866 QTRY_VERIFY(listview != 0);
2867 QQuickItem *contentItem = listview->contentItem();
2868 QTRY_VERIFY(contentItem != 0);
2869 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
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_COMPARE(item->y(), i*20.);
2880 // Position on a currently visible item
2881 listview->positionViewAtIndex(3, QQuickListView::Beginning);
2882 QTRY_COMPARE(listview->contentY(), 60.);
2884 // Confirm items positioned correctly
2885 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2886 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2887 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2888 if (!item) qWarning() << "Item" << i << "not found";
2890 QTRY_COMPARE(item->y(), i*20.);
2893 // Position on an item beyond the visible items
2894 listview->positionViewAtIndex(22, QQuickListView::Beginning);
2895 QTRY_COMPARE(listview->contentY(), 440.);
2897 // Confirm items positioned correctly
2898 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2899 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2900 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2901 if (!item) qWarning() << "Item" << i << "not found";
2903 QTRY_COMPARE(item->y(), i*20.);
2906 // Position on an item that would leave empty space if positioned at the top
2907 listview->positionViewAtIndex(28, QQuickListView::Beginning);
2908 QTRY_COMPARE(listview->contentY(), 480.);
2910 // Confirm items positioned correctly
2911 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2912 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2913 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2914 if (!item) qWarning() << "Item" << i << "not found";
2916 QTRY_COMPARE(item->y(), i*20.);
2919 // Position at the beginning again
2920 listview->positionViewAtIndex(0, QQuickListView::Beginning);
2921 QTRY_COMPARE(listview->contentY(), 0.);
2923 // Confirm items positioned correctly
2924 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2925 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2926 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2927 if (!item) qWarning() << "Item" << i << "not found";
2929 QTRY_COMPARE(item->y(), i*20.);
2932 // Position at End using last index
2933 listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
2934 QTRY_COMPARE(listview->contentY(), 480.);
2936 // Confirm items positioned correctly
2937 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2938 for (int i = 24; i < model.count(); ++i) {
2939 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2940 if (!item) qWarning() << "Item" << i << "not found";
2942 QTRY_COMPARE(item->y(), i*20.);
2946 listview->positionViewAtIndex(20, QQuickListView::End);
2947 QTRY_COMPARE(listview->contentY(), 100.);
2949 // Position in Center
2950 listview->positionViewAtIndex(15, QQuickListView::Center);
2951 QTRY_COMPARE(listview->contentY(), 150.);
2953 // Ensure at least partially visible
2954 listview->positionViewAtIndex(15, QQuickListView::Visible);
2955 QTRY_COMPARE(listview->contentY(), 150.);
2957 listview->setContentY(302);
2958 listview->positionViewAtIndex(15, QQuickListView::Visible);
2959 QTRY_COMPARE(listview->contentY(), 302.);
2961 listview->setContentY(320);
2962 listview->positionViewAtIndex(15, QQuickListView::Visible);
2963 QTRY_COMPARE(listview->contentY(), 300.);
2965 listview->setContentY(85);
2966 listview->positionViewAtIndex(20, QQuickListView::Visible);
2967 QTRY_COMPARE(listview->contentY(), 85.);
2969 listview->setContentY(75);
2970 listview->positionViewAtIndex(20, QQuickListView::Visible);
2971 QTRY_COMPARE(listview->contentY(), 100.);
2973 // Ensure completely visible
2974 listview->setContentY(120);
2975 listview->positionViewAtIndex(20, QQuickListView::Contain);
2976 QTRY_COMPARE(listview->contentY(), 120.);
2978 listview->setContentY(302);
2979 listview->positionViewAtIndex(15, QQuickListView::Contain);
2980 QTRY_COMPARE(listview->contentY(), 300.);
2982 listview->setContentY(85);
2983 listview->positionViewAtIndex(20, QQuickListView::Contain);
2984 QTRY_COMPARE(listview->contentY(), 100.);
2986 // positionAtBeginnging
2987 listview->positionViewAtBeginning();
2988 QTRY_COMPARE(listview->contentY(), 0.);
2990 listview->setContentY(80);
2991 canvas->rootObject()->setProperty("showHeader", true);
2992 listview->positionViewAtBeginning();
2993 QTRY_COMPARE(listview->contentY(), -30.);
2996 listview->positionViewAtEnd();
2997 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
2999 listview->setContentY(80);
3000 canvas->rootObject()->setProperty("showFooter", true);
3001 listview->positionViewAtEnd();
3002 QTRY_COMPARE(listview->contentY(), 510.);
3004 // set current item to outside visible view, position at beginning
3005 // and ensure highlight moves to current item
3006 listview->setCurrentIndex(1);
3007 listview->positionViewAtBeginning();
3008 QTRY_COMPARE(listview->contentY(), -30.);
3009 QVERIFY(listview->highlightItem());
3010 QCOMPARE(listview->highlightItem()->y(), 20.);
3016 void tst_QQuickListView::resetModel()
3018 QQuickView *canvas = createView();
3020 QStringList strings;
3021 strings << "one" << "two" << "three";
3022 QStringListModel model(strings);
3024 QQmlContext *ctxt = canvas->rootContext();
3025 ctxt->setContextProperty("testModel", &model);
3027 canvas->setSource(testFileUrl("displaylist.qml"));
3029 qApp->processEvents();
3031 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3032 QTRY_VERIFY(listview != 0);
3033 QQuickItem *contentItem = listview->contentItem();
3034 QTRY_VERIFY(contentItem != 0);
3035 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3037 QTRY_COMPARE(listview->count(), model.rowCount());
3039 for (int i = 0; i < model.rowCount(); ++i) {
3040 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3041 QTRY_VERIFY(display != 0);
3042 QTRY_COMPARE(display->text(), strings.at(i));
3046 strings << "four" << "five" << "six" << "seven";
3047 model.setStringList(strings);
3049 QTRY_COMPARE(listview->count(), model.rowCount());
3051 for (int i = 0; i < model.rowCount(); ++i) {
3052 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3053 QTRY_VERIFY(display != 0);
3054 QTRY_COMPARE(display->text(), strings.at(i));
3060 void tst_QQuickListView::propertyChanges()
3062 QQuickView *canvas = createView();
3063 QTRY_VERIFY(canvas);
3064 canvas->setSource(testFileUrl("propertychangestest.qml"));
3066 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3067 QTRY_VERIFY(listView);
3069 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3070 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3071 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3072 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3073 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3074 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3075 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3077 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3078 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3079 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3080 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3081 QTRY_COMPARE(listView->isWrapEnabled(), true);
3082 QTRY_COMPARE(listView->cacheBuffer(), 10);
3083 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3085 listView->setHighlightFollowsCurrentItem(false);
3086 listView->setPreferredHighlightBegin(1.0);
3087 listView->setPreferredHighlightEnd(1.0);
3088 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3089 listView->setWrapEnabled(false);
3090 listView->setCacheBuffer(3);
3091 listView->setSnapMode(QQuickListView::SnapOneItem);
3093 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3094 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3095 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3096 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3097 QTRY_COMPARE(listView->isWrapEnabled(), false);
3098 QTRY_COMPARE(listView->cacheBuffer(), 3);
3099 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3101 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3102 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3103 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3104 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3105 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3106 QTRY_COMPARE(cacheBufferSpy.count(),1);
3107 QTRY_COMPARE(snapModeSpy.count(),1);
3109 listView->setHighlightFollowsCurrentItem(false);
3110 listView->setPreferredHighlightBegin(1.0);
3111 listView->setPreferredHighlightEnd(1.0);
3112 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3113 listView->setWrapEnabled(false);
3114 listView->setCacheBuffer(3);
3115 listView->setSnapMode(QQuickListView::SnapOneItem);
3117 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3118 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3119 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3120 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3121 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3122 QTRY_COMPARE(cacheBufferSpy.count(),1);
3123 QTRY_COMPARE(snapModeSpy.count(),1);
3128 void tst_QQuickListView::componentChanges()
3130 QQuickView *canvas = createView();
3131 QTRY_VERIFY(canvas);
3132 canvas->setSource(testFileUrl("propertychangestest.qml"));
3134 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3135 QTRY_VERIFY(listView);
3137 QQmlComponent component(canvas->engine());
3138 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3140 QQmlComponent delegateComponent(canvas->engine());
3141 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3143 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3144 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3145 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3146 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3148 listView->setHighlight(&component);
3149 listView->setHeader(&component);
3150 listView->setFooter(&component);
3151 listView->setDelegate(&delegateComponent);
3153 QTRY_COMPARE(listView->highlight(), &component);
3154 QTRY_COMPARE(listView->header(), &component);
3155 QTRY_COMPARE(listView->footer(), &component);
3156 QTRY_COMPARE(listView->delegate(), &delegateComponent);
3158 QTRY_COMPARE(highlightSpy.count(),1);
3159 QTRY_COMPARE(delegateSpy.count(),1);
3160 QTRY_COMPARE(headerSpy.count(),1);
3161 QTRY_COMPARE(footerSpy.count(),1);
3163 listView->setHighlight(&component);
3164 listView->setHeader(&component);
3165 listView->setFooter(&component);
3166 listView->setDelegate(&delegateComponent);
3168 QTRY_COMPARE(highlightSpy.count(),1);
3169 QTRY_COMPARE(delegateSpy.count(),1);
3170 QTRY_COMPARE(headerSpy.count(),1);
3171 QTRY_COMPARE(footerSpy.count(),1);
3176 void tst_QQuickListView::modelChanges()
3178 QQuickView *canvas = createView();
3179 QTRY_VERIFY(canvas);
3180 canvas->setSource(testFileUrl("propertychangestest.qml"));
3182 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
3183 QTRY_VERIFY(listView);
3185 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
3186 QTRY_VERIFY(alternateModel);
3187 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3188 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3190 listView->setModel(modelVariant);
3191 QTRY_COMPARE(listView->model(), modelVariant);
3192 QTRY_COMPARE(modelSpy.count(),1);
3194 listView->setModel(modelVariant);
3195 QTRY_COMPARE(modelSpy.count(),1);
3197 listView->setModel(QVariant());
3198 QTRY_COMPARE(modelSpy.count(),2);
3203 void tst_QQuickListView::QTBUG_9791()
3205 QQuickView *canvas = createView();
3207 canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
3208 qApp->processEvents();
3210 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3211 QTRY_VERIFY(listview != 0);
3213 QQuickItem *contentItem = listview->contentItem();
3214 QTRY_VERIFY(contentItem != 0);
3215 QTRY_VERIFY(listview->delegate() != 0);
3216 QTRY_VERIFY(listview->model() != 0);
3218 QMetaObject::invokeMethod(listview, "fillModel");
3219 qApp->processEvents();
3221 // Confirm items positioned correctly
3222 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3223 QCOMPARE(itemCount, 3);
3225 for (int i = 0; i < itemCount; ++i) {
3226 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3227 if (!item) qWarning() << "Item" << i << "not found";
3229 QTRY_COMPARE(item->x(), i*300.0);
3232 // check that view is positioned correctly
3233 QTRY_COMPARE(listview->contentX(), 590.0);
3238 void tst_QQuickListView::manualHighlight()
3240 QQuickView *canvas = new QQuickView(0);
3241 canvas->setGeometry(0,0,240,320);
3243 QString filename(testFile("manual-highlight.qml"));
3244 canvas->setSource(QUrl::fromLocalFile(filename));
3246 qApp->processEvents();
3248 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3249 QTRY_VERIFY(listview != 0);
3251 QQuickItem *contentItem = listview->contentItem();
3252 QTRY_VERIFY(contentItem != 0);
3254 QTRY_COMPARE(listview->currentIndex(), 0);
3255 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3256 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3258 listview->setCurrentIndex(2);
3260 QTRY_COMPARE(listview->currentIndex(), 2);
3261 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3262 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3265 listview->positionViewAtIndex(3, QQuickListView::Contain);
3267 QTRY_COMPARE(listview->currentIndex(), 2);
3268 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3269 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3274 void tst_QQuickListView::QTBUG_11105()
3276 QQuickView *canvas = createView();
3279 for (int i = 0; i < 30; i++)
3280 model.addItem("Item" + QString::number(i), "");
3282 QQmlContext *ctxt = canvas->rootContext();
3283 ctxt->setContextProperty("testModel", &model);
3285 TestObject *testObject = new TestObject;
3286 ctxt->setContextProperty("testObject", testObject);
3288 canvas->setSource(testFileUrl("listviewtest.qml"));
3290 qApp->processEvents();
3292 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3293 QTRY_VERIFY(listview != 0);
3294 QQuickItem *contentItem = listview->contentItem();
3295 QTRY_VERIFY(contentItem != 0);
3296 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3298 // Confirm items positioned correctly
3299 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3300 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3301 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3302 if (!item) qWarning() << "Item" << i << "not found";
3304 QTRY_VERIFY(item->y() == i*20);
3307 listview->positionViewAtIndex(20, QQuickListView::Beginning);
3308 QCOMPARE(listview->contentY(), 280.);
3310 QmlListModel model2;
3311 for (int i = 0; i < 5; i++)
3312 model2.addItem("Item" + QString::number(i), "");
3314 ctxt->setContextProperty("testModel", &model2);
3316 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3317 QCOMPARE(itemCount, 5);
3323 void tst_QQuickListView::initialZValues()
3325 QQuickView *canvas = createView();
3326 canvas->setSource(testFileUrl("initialZValues.qml"));
3327 qApp->processEvents();
3329 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3330 QTRY_VERIFY(listview != 0);
3331 QQuickItem *contentItem = listview->contentItem();
3332 QTRY_VERIFY(contentItem != 0);
3334 QVERIFY(listview->headerItem());
3335 QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3337 QVERIFY(listview->footerItem());
3338 QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3343 void tst_QQuickListView::header()
3345 QFETCH(QQuickListView::Orientation, orientation);
3346 QFETCH(Qt::LayoutDirection, layoutDirection);
3347 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3348 QFETCH(QPointF, initialHeaderPos);
3349 QFETCH(QPointF, changedHeaderPos);
3350 QFETCH(QPointF, initialContentPos);
3351 QFETCH(QPointF, changedContentPos);
3352 QFETCH(QPointF, firstDelegatePos);
3353 QFETCH(QPointF, resizeContentPos);
3356 for (int i = 0; i < 30; i++)
3357 model.addItem("Item" + QString::number(i), "");
3359 QQuickView *canvas = getView();
3360 canvas->rootContext()->setContextProperty("testModel", &model);
3361 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3362 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3363 canvas->setSource(testFileUrl("header.qml"));
3365 qApp->processEvents();
3367 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3368 QTRY_VERIFY(listview != 0);
3369 listview->setOrientation(orientation);
3370 listview->setLayoutDirection(layoutDirection);
3371 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3372 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3374 QQuickItem *contentItem = listview->contentItem();
3375 QTRY_VERIFY(contentItem != 0);
3377 QQuickText *header = 0;
3378 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3379 QVERIFY(header == listview->headerItem());
3381 QCOMPARE(header->width(), 100.);
3382 QCOMPARE(header->height(), 30.);
3383 QCOMPARE(header->pos(), initialHeaderPos);
3384 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3386 if (orientation == QQuickListView::Vertical)
3387 QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3389 QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3391 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3393 QCOMPARE(item->pos(), firstDelegatePos);
3396 QTRY_COMPARE(listview->count(), model.count());
3397 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3398 if (orientation == QQuickListView::Vertical)
3399 QCOMPARE(listview->contentHeight(), header->height());
3401 QCOMPARE(listview->contentWidth(), header->width());
3403 for (int i = 0; i < 30; i++)
3404 model.addItem("Item" + QString::number(i), "");
3406 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3407 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3409 QCOMPARE(headerItemSpy.count(), 1);
3411 header = findItem<QQuickText>(contentItem, "header");
3413 header = findItem<QQuickText>(contentItem, "header2");
3416 QVERIFY(header == listview->headerItem());
3418 QCOMPARE(header->pos(), changedHeaderPos);
3419 QCOMPARE(header->width(), 50.);
3420 QCOMPARE(header->height(), 20.);
3421 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3423 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3425 QCOMPARE(item->pos(), firstDelegatePos);
3427 listview->positionViewAtBeginning();
3428 header->setHeight(10);
3429 header->setWidth(40);
3430 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3432 releaseView(canvas);
3435 // QTBUG-21207 header should become visible if view resizes from initial empty size
3438 canvas->rootContext()->setContextProperty("testModel", &model);
3439 canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3440 canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3441 canvas->setSource(testFileUrl("header.qml"));
3443 qApp->processEvents();
3445 listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3446 QTRY_VERIFY(listview != 0);
3447 listview->setOrientation(orientation);
3448 listview->setLayoutDirection(layoutDirection);
3449 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3450 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3452 listview->setWidth(240);
3453 listview->setHeight(320);
3454 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3455 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3457 releaseView(canvas);
3460 void tst_QQuickListView::header_data()
3462 QTest::addColumn<QQuickListView::Orientation>("orientation");
3463 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3464 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3465 QTest::addColumn<QPointF>("initialHeaderPos");
3466 QTest::addColumn<QPointF>("changedHeaderPos");
3467 QTest::addColumn<QPointF>("initialContentPos");
3468 QTest::addColumn<QPointF>("changedContentPos");
3469 QTest::addColumn<QPointF>("firstDelegatePos");
3470 QTest::addColumn<QPointF>("resizeContentPos");
3472 // header1 = 100 x 30
3473 // header2 = 50 x 20
3474 // delegates = 240 x 30
3477 // header above items, top left
3478 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3486 // header above items, top right
3487 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3495 // header to left of items
3496 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3504 // header to right of items
3505 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3508 << QPointF(-240 + 100, 0)
3509 << QPointF(-240 + 50, 0)
3511 << QPointF(-240 + 40, 0);
3513 // header below items
3514 QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3517 << QPointF(0, -320 + 30)
3518 << QPointF(0, -320 + 20)
3520 << QPointF(0, -320 + 10);
3523 void tst_QQuickListView::header_delayItemCreation()
3525 QQuickView *canvas = createView();
3529 canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3530 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3531 qApp->processEvents();
3533 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3534 QTRY_VERIFY(listview != 0);
3536 QQuickItem *contentItem = listview->contentItem();
3537 QTRY_VERIFY(contentItem != 0);
3539 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3541 QCOMPARE(header->y(), -header->height());
3543 QCOMPARE(listview->contentY(), -header->height());
3546 QTRY_COMPARE(header->y(), -header->height());
3551 void tst_QQuickListView::footer()
3553 QFETCH(QQuickListView::Orientation, orientation);
3554 QFETCH(Qt::LayoutDirection, layoutDirection);
3555 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3556 QFETCH(QPointF, initialFooterPos);
3557 QFETCH(QPointF, firstDelegatePos);
3558 QFETCH(QPointF, initialContentPos);
3559 QFETCH(QPointF, changedFooterPos);
3560 QFETCH(QPointF, changedContentPos);
3561 QFETCH(QPointF, resizeContentPos);
3563 QQuickView *canvas = getView();
3566 for (int i = 0; i < 3; i++)
3567 model.addItem("Item" + QString::number(i), "");
3569 QQmlContext *ctxt = canvas->rootContext();
3570 ctxt->setContextProperty("testModel", &model);
3572 canvas->setSource(testFileUrl("footer.qml"));
3574 qApp->processEvents();
3576 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3577 QTRY_VERIFY(listview != 0);
3578 listview->setOrientation(orientation);
3579 listview->setLayoutDirection(layoutDirection);
3580 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3581 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3583 QQuickItem *contentItem = listview->contentItem();
3584 QTRY_VERIFY(contentItem != 0);
3586 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3589 QVERIFY(footer == listview->footerItem());
3591 QCOMPARE(footer->pos(), initialFooterPos);
3592 QCOMPARE(footer->width(), 100.);
3593 QCOMPARE(footer->height(), 30.);
3594 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3596 if (orientation == QQuickListView::Vertical)
3597 QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3599 QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3601 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3603 QCOMPARE(item->pos(), firstDelegatePos);
3606 model.removeItem(1);
3608 if (orientation == QQuickListView::Vertical) {
3609 QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3610 initialFooterPos.y() - 20 : initialFooterPos.y() + 20); // delegate width = 40
3612 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3613 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3618 if (orientation == QQuickListView::Vertical)
3619 QTRY_COMPARE(listview->contentHeight(), footer->height());
3621 QTRY_COMPARE(listview->contentWidth(), footer->width());
3623 QPointF posWhenNoItems(0, 0);
3624 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3625 posWhenNoItems.setX(-100);
3626 else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3627 posWhenNoItems.setY(-30);
3628 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3630 // if header is present, it's at a negative pos, so the footer should not move
3631 canvas->rootObject()->setProperty("showHeader", true);
3632 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3633 canvas->rootObject()->setProperty("showHeader", false);
3636 for (int i = 0; i < 30; i++)
3637 model.addItem("Item" + QString::number(i), "");
3639 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3640 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3642 QCOMPARE(footerItemSpy.count(), 1);
3644 footer = findItem<QQuickText>(contentItem, "footer");
3646 footer = findItem<QQuickText>(contentItem, "footer2");
3649 QVERIFY(footer == listview->footerItem());
3651 QCOMPARE(footer->pos(), changedFooterPos);
3652 QCOMPARE(footer->width(), 50.);
3653 QCOMPARE(footer->height(), 20.);
3654 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3656 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3658 QCOMPARE(item->pos(), firstDelegatePos);
3660 listview->positionViewAtEnd();
3661 footer->setHeight(10);
3662 footer->setWidth(40);
3663 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3665 releaseView(canvas);
3668 void tst_QQuickListView::footer_data()
3670 QTest::addColumn<QQuickListView::Orientation>("orientation");
3671 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3672 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3673 QTest::addColumn<QPointF>("initialFooterPos");
3674 QTest::addColumn<QPointF>("changedFooterPos");
3675 QTest::addColumn<QPointF>("initialContentPos");
3676 QTest::addColumn<QPointF>("changedContentPos");
3677 QTest::addColumn<QPointF>("firstDelegatePos");
3678 QTest::addColumn<QPointF>("resizeContentPos");
3680 // footer1 = 100 x 30
3681 // footer2 = 50 x 20
3682 // delegates = 40 x 20
3684 // view height = 320
3686 // footer below items, bottom left
3687 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3688 << QPointF(0, 3 * 20)
3689 << QPointF(0, 30 * 20) // added 30 items
3693 << QPointF(0, 30 * 20 - 320 + 10);
3695 // footer below items, bottom right
3696 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3697 << QPointF(0, 3 * 20)
3698 << QPointF(0, 30 * 20)
3702 << QPointF(0, 30 * 20 - 320 + 10);
3704 // footer to right of items
3705 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3706 << QPointF(40 * 3, 0)
3707 << QPointF(40 * 30, 0)
3711 << QPointF(40 * 30 - 240 + 40, 0);
3713 // footer to left of items
3714 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3715 << QPointF(-(40 * 3) - 100, 0)
3716 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3720 << QPointF(-(40 * 30) - 40, 0);
3722 // footer above items
3723 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3724 << QPointF(0, -(3 * 20) - 30)
3725 << QPointF(0, -(30 * 20) - 20)
3729 << QPointF(0, -(30 * 20) - 10);
3732 class LVAccessor : public QQuickListView
3735 qreal minY() const { return minYExtent(); }
3736 qreal maxY() const { return maxYExtent(); }
3737 qreal minX() const { return minXExtent(); }
3738 qreal maxX() const { return maxXExtent(); }
3742 void tst_QQuickListView::extents()
3744 QFETCH(QQuickListView::Orientation, orientation);
3745 QFETCH(Qt::LayoutDirection, layoutDirection);
3746 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3747 QFETCH(QPointF, headerPos);
3748 QFETCH(QPointF, footerPos);
3749 QFETCH(QPointF, minPos);
3750 QFETCH(QPointF, maxPos);
3751 QFETCH(QPointF, origin_empty);
3752 QFETCH(QPointF, origin_nonEmpty);
3754 QQuickView *canvas = getView();
3757 QQmlContext *ctxt = canvas->rootContext();
3758 ctxt->setContextProperty("testModel", &model);
3759 canvas->setSource(testFileUrl("headerfooter.qml"));
3761 qApp->processEvents();
3763 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3764 QTRY_VERIFY(listview != 0);
3765 listview->setOrientation(orientation);
3766 listview->setLayoutDirection(layoutDirection);
3767 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3768 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3770 QQuickItem *contentItem = listview->contentItem();
3771 QTRY_VERIFY(contentItem != 0);
3773 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3775 QCOMPARE(header->pos(), headerPos);
3777 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3779 QCOMPARE(footer->pos(), footerPos);
3781 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3782 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3783 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3784 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3786 QCOMPARE(listview->originX(), origin_empty.x());
3787 QCOMPARE(listview->originY(), origin_empty.y());
3788 for (int i=0; i<30; i++)
3789 model.addItem("Item" + QString::number(i), "");
3790 QTRY_COMPARE(listview->count(), model.count());
3791 QCOMPARE(listview->originX(), origin_nonEmpty.x());
3792 QCOMPARE(listview->originY(), origin_nonEmpty.y());
3794 releaseView(canvas);
3797 void tst_QQuickListView::extents_data()
3799 QTest::addColumn<QQuickListView::Orientation>("orientation");
3800 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3801 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3802 QTest::addColumn<QPointF>("headerPos");
3803 QTest::addColumn<QPointF>("footerPos");
3804 QTest::addColumn<QPointF>("minPos");
3805 QTest::addColumn<QPointF>("maxPos");
3806 QTest::addColumn<QPointF>("origin_empty");
3807 QTest::addColumn<QPointF>("origin_nonEmpty");
3809 // header is 240x20 (or 20x320 in Horizontal orientation)
3810 // footer is 240x30 (or 30x320 in Horizontal orientation)
3812 QTest::newRow("Vertical, TopToBottom")
3813 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3814 << QPointF(0, -20) << QPointF(0, 0)
3815 << QPointF(0, 20) << QPointF(240, 20)
3816 << QPointF(0, -20) << QPointF(0, -20);
3818 QTest::newRow("Vertical, BottomToTop")
3819 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3820 << QPointF(0, 0) << QPointF(0, -30)
3821 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3822 << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3824 QTest::newRow("Horizontal, LeftToRight")
3825 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3826 << QPointF(-20, 0) << QPointF(0, 0)
3827 << QPointF(20, 0) << QPointF(20, 320)
3828 << QPointF(-20, 0) << QPointF(-20, 0);
3830 QTest::newRow("Horizontal, RightToLeft")
3831 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3832 << QPointF(0, 0) << QPointF(-30, 0)
3833 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3834 << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3837 void tst_QQuickListView::resetModel_headerFooter()
3839 // Resetting a model shouldn't crash in views with header/footer
3841 QQuickView *canvas = createView();
3844 for (int i = 0; i < 4; i++)
3845 model.addItem("Item" + QString::number(i), "");
3846 QQmlContext *ctxt = canvas->rootContext();
3847 ctxt->setContextProperty("testModel", &model);
3849 canvas->setSource(testFileUrl("headerfooter.qml"));
3850 qApp->processEvents();
3852 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3853 QTRY_VERIFY(listview != 0);
3855 QQuickItem *contentItem = listview->contentItem();
3856 QTRY_VERIFY(contentItem != 0);
3858 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3860 QCOMPARE(header->y(), -header->height());
3862 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3864 QCOMPARE(footer->y(), 30.*4);
3868 header = findItem<QQuickItem>(contentItem, "header");
3870 QCOMPARE(header->y(), -header->height());
3872 footer = findItem<QQuickItem>(contentItem, "footer");
3874 QCOMPARE(footer->y(), 30.*4);
3879 void tst_QQuickListView::resizeView()
3881 QQuickView *canvas = createView();
3884 for (int i = 0; i < 40; i++)
3885 model.addItem("Item" + QString::number(i), "");
3887 QQmlContext *ctxt = canvas->rootContext();
3888 ctxt->setContextProperty("testModel", &model);
3890 TestObject *testObject = new TestObject;
3891 ctxt->setContextProperty("testObject", testObject);
3893 canvas->setSource(testFileUrl("listviewtest.qml"));
3895 qApp->processEvents();
3897 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3898 QTRY_VERIFY(listview != 0);
3899 QQuickItem *contentItem = listview->contentItem();
3900 QTRY_VERIFY(contentItem != 0);
3901 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3903 // Confirm items positioned correctly
3904 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3905 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3906 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3907 if (!item) qWarning() << "Item" << i << "not found";
3909 QTRY_COMPARE(item->y(), i*20.);
3912 QVariant heightRatio;
3913 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3914 QCOMPARE(heightRatio.toReal(), 0.4);
3916 listview->setHeight(200);
3917 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3919 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3920 QCOMPARE(heightRatio.toReal(), 0.25);
3922 // Ensure we handle -ve sizes
3923 listview->setHeight(-100);
3924 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
3926 listview->setCacheBuffer(200);
3927 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
3929 // ensure items in cache become visible
3930 listview->setHeight(200);
3931 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
3933 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3934 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3935 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3936 if (!item) qWarning() << "Item" << i << "not found";
3938 QTRY_COMPARE(item->y(), i*20.);
3939 QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
3942 // ensure items outside view become invisible
3943 listview->setHeight(100);
3944 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
3946 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3947 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3948 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3949 if (!item) qWarning() << "Item" << i << "not found";
3951 QTRY_COMPARE(item->y(), i*20.);
3952 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
3959 void tst_QQuickListView::resizeViewAndRepaint()
3961 QQuickView *canvas = createView();
3964 for (int i = 0; i < 40; i++)
3965 model.addItem("Item" + QString::number(i), "");
3967 QQmlContext *ctxt = canvas->rootContext();
3968 ctxt->setContextProperty("testModel", &model);
3969 ctxt->setContextProperty("initialHeight", 100);
3971 canvas->setSource(testFileUrl("resizeview.qml"));
3973 qApp->processEvents();
3975 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3976 QTRY_VERIFY(listview != 0);
3977 QQuickItem *contentItem = listview->contentItem();
3978 QTRY_VERIFY(contentItem != 0);
3979 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3981 // item at index 10 should not be currently visible
3982 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3984 listview->setHeight(320);
3986 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3988 listview->setHeight(100);
3989 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3994 void tst_QQuickListView::sizeLessThan1()
3996 QQuickView *canvas = createView();
3999 for (int i = 0; i < 30; i++)
4000 model.addItem("Item" + QString::number(i), "");
4002 QQmlContext *ctxt = canvas->rootContext();
4003 ctxt->setContextProperty("testModel", &model);
4005 TestObject *testObject = new TestObject;
4006 ctxt->setContextProperty("testObject", testObject);
4008 canvas->setSource(testFileUrl("sizelessthan1.qml"));
4010 qApp->processEvents();
4012 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4013 QTRY_VERIFY(listview != 0);
4014 QQuickItem *contentItem = listview->contentItem();
4015 QTRY_VERIFY(contentItem != 0);
4016 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4018 // Confirm items positioned correctly
4019 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4020 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4021 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4022 if (!item) qWarning() << "Item" << i << "not found";
4024 QTRY_COMPARE(item->y(), i*0.5);
4031 void tst_QQuickListView::QTBUG_14821()
4033 QQuickView *canvas = createView();
4035 canvas->setSource(testFileUrl("qtbug14821.qml"));
4036 qApp->processEvents();
4038 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4039 QVERIFY(listview != 0);
4041 QQuickItem *contentItem = listview->contentItem();
4042 QVERIFY(contentItem != 0);
4044 listview->decrementCurrentIndex();
4045 QCOMPARE(listview->currentIndex(), 99);
4047 listview->incrementCurrentIndex();
4048 QCOMPARE(listview->currentIndex(), 0);
4053 void tst_QQuickListView::resizeDelegate()
4055 QQuickView *canvas = createView();
4057 QStringList strings;
4058 for (int i = 0; i < 30; ++i)
4059 strings << QString::number(i);
4060 QStringListModel model(strings);
4062 QQmlContext *ctxt = canvas->rootContext();
4063 ctxt->setContextProperty("testModel", &model);
4065 canvas->setSource(testFileUrl("displaylist.qml"));
4067 qApp->processEvents();
4069 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4070 QVERIFY(listview != 0);
4071 QQuickItem *contentItem = listview->contentItem();
4072 QVERIFY(contentItem != 0);
4073 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4075 QCOMPARE(listview->count(), model.rowCount());
4077 listview->setCurrentIndex(25);
4078 listview->setContentY(0);
4079 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4081 for (int i = 0; i < 16; ++i) {
4082 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4084 QCOMPARE(item->y(), i*20.0);
4087 QCOMPARE(listview->currentItem()->y(), 500.0);
4088 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4090 canvas->rootObject()->setProperty("delegateHeight", 30);
4091 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4093 for (int i = 0; i < 11; ++i) {
4094 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4096 QTRY_COMPARE(item->y(), i*30.0);
4099 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4100 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4102 listview->setCurrentIndex(1);
4103 listview->positionViewAtIndex(25, QQuickListView::Beginning);
4104 listview->positionViewAtIndex(5, QQuickListView::Beginning);
4105 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4107 for (int i = 5; i < 16; ++i) {
4108 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4110 QCOMPARE(item->y(), i*30.0);
4113 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4114 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4116 canvas->rootObject()->setProperty("delegateHeight", 20);
4117 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4119 for (int i = 5; i < 11; ++i) {
4120 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4122 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4125 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4126 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4131 void tst_QQuickListView::resizeFirstDelegate()
4133 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4134 // and other delegates have height > 0
4136 QQuickView *canvas = createView();
4138 // bug only occurs when all items in the model are visible
4140 for (int i = 0; i < 10; i++)
4141 model.addItem("Item" + QString::number(i), "");
4143 QQmlContext *ctxt = canvas->rootContext();
4144 ctxt->setContextProperty("testModel", &model);
4146 TestObject *testObject = new TestObject;
4147 ctxt->setContextProperty("testObject", testObject);
4149 canvas->setSource(testFileUrl("listviewtest.qml"));
4151 qApp->processEvents();
4153 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4154 QVERIFY(listview != 0);
4155 QQuickItem *contentItem = listview->contentItem();
4156 QVERIFY(contentItem != 0);
4157 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4159 QQuickItem *item = 0;
4160 for (int i = 0; i < model.count(); ++i) {
4161 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4163 QCOMPARE(item->y(), i*20.0);
4166 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4169 // check the content y has not jumped up and down
4170 QCOMPARE(listview->contentY(), 0.0);
4171 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4173 QCOMPARE(spy.count(), 0);
4175 for (int i = 1; i < model.count(); ++i) {
4176 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4178 QTRY_COMPARE(item->y(), (i-1)*20.0);
4182 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4183 // list if they follow a zero-sized delegate
4185 for (int i = 0; i < 10; i++)
4186 model.addItem("Item" + QString::number(i), "");
4187 QTRY_COMPARE(listview->count(), model.count());
4189 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4193 listview->setCurrentIndex(19);
4194 qApp->processEvents();
4195 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4197 // items 0-2 should have been deleted
4198 for (int i=0; i<3; i++) {
4199 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4206 void tst_QQuickListView::repositionResizedDelegate()
4208 QFETCH(QQuickListView::Orientation, orientation);
4209 QFETCH(Qt::LayoutDirection, layoutDirection);
4210 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4211 QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4212 QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4213 QFETCH(QRectF, origPositionerRect);
4214 QFETCH(QRectF, resizedPositionerRect);
4216 QQuickView *canvas = getView();
4217 QQmlContext *ctxt = canvas->rootContext();
4218 ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4219 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4220 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4221 canvas->setSource(testFileUrl("repositionResizedDelegate.qml"));
4223 qApp->processEvents();
4225 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
4226 QTRY_VERIFY(listview != 0);
4227 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4229 QQuickItem *positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4230 QVERIFY(positioner);
4231 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4232 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4233 QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4234 int prevSpyCount = 0;
4236 // When an item is resized while it is partially visible, it should resize in the
4237 // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4238 // the item should also be re-positioned so its end position stays the same.
4240 listview->setContentX(contentPos_itemFirstHalfVisible.x());
4241 listview->setContentY(contentPos_itemFirstHalfVisible.y());
4242 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4243 prevSpyCount = spy.count();
4244 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4245 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4246 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4247 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4248 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4249 QCOMPARE(spy.count(), prevSpyCount);
4251 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "decrementRepeater"));
4252 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4253 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4254 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4255 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4257 listview->setContentX(contentPos_itemSecondHalfVisible.x());
4258 listview->setContentY(contentPos_itemSecondHalfVisible.y());
4259 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4260 prevSpyCount = spy.count();
4262 QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "incrementRepeater"));
4263 positioner = findItem<QQuickItem>(canvas->rootObject(), "positioner");
4264 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4265 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4266 QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4267 QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4268 qApp->processEvents();
4269 QCOMPARE(spy.count(), prevSpyCount);
4271 releaseView(canvas);
4274 void tst_QQuickListView::repositionResizedDelegate_data()
4276 QTest::addColumn<QQuickListView::Orientation>("orientation");
4277 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4278 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4279 QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4280 QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4281 QTest::addColumn<QRectF>("origPositionerRect");
4282 QTest::addColumn<QRectF>("resizedPositionerRect");
4284 QTest::newRow("vertical")
4285 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4286 << QPointF(0, 60) << QPointF(0, 200 + 60)
4287 << QRectF(0, 200, 120, 120)
4288 << QRectF(0, 200, 120, 120 * 2);
4290 QTest::newRow("vertical, BottomToTop")
4291 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4292 << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4293 << QRectF(0, -200 - 120, 120, 120)
4294 << QRectF(0, -200 - 120*2, 120, 120 * 2);
4296 QTest::newRow("horizontal")
4297 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4298 << QPointF(60, 0) << QPointF(260, 0)
4299 << QRectF(200, 0, 120, 120)
4300 << QRectF(200, 0, 120 * 2, 120);
4302 QTest::newRow("horizontal, rtl")
4303 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4304 << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4305 << QRectF(-200 - 120, 0, 120, 120)
4306 << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4309 void tst_QQuickListView::QTBUG_16037()
4311 QQuickView *canvas = createView();
4314 canvas->setSource(testFileUrl("qtbug16037.qml"));
4315 qApp->processEvents();
4317 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4318 QTRY_VERIFY(listview != 0);
4320 QVERIFY(listview->contentHeight() <= 0.0);
4322 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
4324 QTRY_COMPARE(listview->contentHeight(), 80.0);
4329 void tst_QQuickListView::indexAt_itemAt_data()
4331 QTest::addColumn<qreal>("x");
4332 QTest::addColumn<qreal>("y");
4333 QTest::addColumn<int>("index");
4335 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4336 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4337 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4338 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4339 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4342 void tst_QQuickListView::indexAt_itemAt()
4348 QQuickView *canvas = getView();
4351 for (int i = 0; i < 30; i++)
4352 model.addItem("Item" + QString::number(i), "");
4354 QQmlContext *ctxt = canvas->rootContext();
4355 ctxt->setContextProperty("testModel", &model);
4357 TestObject *testObject = new TestObject;
4358 ctxt->setContextProperty("testObject", testObject);
4360 canvas->setSource(testFileUrl("listviewtest.qml"));
4362 qApp->processEvents();
4364 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4365 QTRY_VERIFY(listview != 0);
4367 QQuickItem *contentItem = listview->contentItem();
4368 QTRY_VERIFY(contentItem != 0);
4369 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4371 QQuickItem *item = 0;
4373 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4376 QCOMPARE(listview->indexAt(x,y), index);
4377 QVERIFY(listview->itemAt(x,y) == item);
4379 releaseView(canvas);
4383 void tst_QQuickListView::incrementalModel()
4385 QQuickView *canvas = createView();
4387 IncrementalModel model;
4388 QQmlContext *ctxt = canvas->rootContext();
4389 ctxt->setContextProperty("testModel", &model);
4391 canvas->setSource(testFileUrl("displaylist.qml"));
4392 qApp->processEvents();
4394 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4395 QTRY_VERIFY(listview != 0);
4397 QQuickItem *contentItem = listview->contentItem();
4398 QTRY_VERIFY(contentItem != 0);
4400 QTRY_COMPARE(listview->count(), 20);
4402 listview->positionViewAtIndex(10, QQuickListView::Beginning);
4404 QTRY_COMPARE(listview->count(), 25);
4409 void tst_QQuickListView::onAdd()
4411 QFETCH(int, initialItemCount);
4412 QFETCH(int, itemsToAdd);
4414 const int delegateHeight = 10;
4417 // these initial items should not trigger ListView.onAdd
4418 for (int i=0; i<initialItemCount; i++)
4419 model.addItem("dummy value", "dummy value");
4421 QQuickView *canvas = createView();
4422 canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4423 QQmlContext *ctxt = canvas->rootContext();
4424 ctxt->setContextProperty("testModel", &model);
4425 ctxt->setContextProperty("delegateHeight", delegateHeight);
4426 canvas->setSource(testFileUrl("attachedSignals.qml"));
4428 QObject *object = canvas->rootObject();
4429 object->setProperty("width", canvas->width());
4430 object->setProperty("height", canvas->height());
4431 qApp->processEvents();
4433 QList<QPair<QString, QString> > items;
4434 for (int i=0; i<itemsToAdd; i++)
4435 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4436 model.addItems(items);
4437 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4439 QVariantList result = object->property("addedDelegates").toList();
4440 QCOMPARE(result.count(), items.count());
4441 for (int i=0; i<items.count(); i++)
4442 QCOMPARE(result[i].toString(), items[i].first);
4447 void tst_QQuickListView::onAdd_data()
4449 QTest::addColumn<int>("initialItemCount");
4450 QTest::addColumn<int>("itemsToAdd");
4452 QTest::newRow("0, add 1") << 0 << 1;
4453 QTest::newRow("0, add 2") << 0 << 2;
4454 QTest::newRow("0, add 10") << 0 << 10;
4456 QTest::newRow("1, add 1") << 1 << 1;
4457 QTest::newRow("1, add 2") << 1 << 2;
4458 QTest::newRow("1, add 10") << 1 << 10;
4460 QTest::newRow("5, add 1") << 5 << 1;
4461 QTest::newRow("5, add 2") << 5 << 2;
4462 QTest::newRow("5, add 10") << 5 << 10;
4465 void tst_QQuickListView::onRemove()
4467 QFETCH(int, initialItemCount);
4468 QFETCH(int, indexToRemove);
4469 QFETCH(int, removeCount);
4471 const int delegateHeight = 10;
4473 for (int i=0; i<initialItemCount; i++)
4474 model.addItem(QString("value %1").arg(i), "dummy value");
4476 QQuickView *canvas = getView();
4477 QQmlContext *ctxt = canvas->rootContext();
4478 ctxt->setContextProperty("testModel", &model);
4479 ctxt->setContextProperty("delegateHeight", delegateHeight);
4480 canvas->setSource(testFileUrl("attachedSignals.qml"));
4482 QObject *object = canvas->rootObject();
4484 model.removeItems(indexToRemove, removeCount);
4485 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4487 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4489 releaseView(canvas);
4492 void tst_QQuickListView::onRemove_data()
4494 QTest::addColumn<int>("initialItemCount");
4495 QTest::addColumn<int>("indexToRemove");
4496 QTest::addColumn<int>("removeCount");
4498 QTest::newRow("remove first") << 1 << 0 << 1;
4499 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4500 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4501 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4503 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4504 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4505 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4506 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4507 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4508 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4510 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4511 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4512 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4515 void tst_QQuickListView::rightToLeft()
4517 QQuickView *canvas = createView();
4518 canvas->setGeometry(0,0,640,320);
4519 canvas->setSource(testFileUrl("rightToLeft.qml"));
4521 qApp->processEvents();
4523 QVERIFY(canvas->rootObject() != 0);
4524 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4525 QTRY_VERIFY(listview != 0);
4527 QQuickItem *contentItem = listview->contentItem();
4528 QTRY_VERIFY(contentItem != 0);
4530 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4532 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4533 QTRY_VERIFY(model != 0);
4535 QTRY_VERIFY(model->count() == 3);
4536 QTRY_COMPARE(listview->currentIndex(), 0);
4538 // initial position at first item, right edge aligned
4539 QCOMPARE(listview->contentX(), -640.);
4541 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4543 QTRY_COMPARE(item->x(), -100.0);
4544 QCOMPARE(item->height(), listview->height());
4546 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4548 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4550 listview->setCurrentIndex(2);
4552 item = findItem<QQuickItem>(contentItem, "item3");
4554 QTRY_COMPARE(item->x(), -540.0);
4556 text = findItem<QQuickText>(contentItem, "text3");
4558 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4560 QCOMPARE(listview->contentX(), -640.);
4562 // Ensure resizing maintains position relative to right edge
4563 qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4564 QTRY_COMPARE(listview->contentX(), -600.);
4569 void tst_QQuickListView::test_mirroring()
4571 QQuickView *canvasA = createView();
4572 canvasA->setSource(testFileUrl("rightToLeft.qml"));
4573 QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4574 QTRY_VERIFY(listviewA != 0);
4576 QQuickView *canvasB = createView();
4577 canvasB->setSource(testFileUrl("rightToLeft.qml"));
4578 QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4579 QTRY_VERIFY(listviewA != 0);
4580 qApp->processEvents();
4582 QList<QString> objectNames;
4583 objectNames << "item1" << "item2"; // << "item3"
4585 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4586 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4587 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4590 foreach (const QString objectName, objectNames)
4591 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4593 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4594 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4597 foreach (const QString objectName, objectNames)
4598 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4600 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4601 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4602 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4604 // LTR != LTR+mirror
4605 foreach (const QString objectName, objectNames)
4606 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4608 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4610 // RTL == LTR+mirror
4611 foreach (const QString objectName, objectNames)
4612 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4614 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4616 // RTL != RTL+mirror
4617 foreach (const QString objectName, objectNames)
4618 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4620 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4622 // LTR == RTL+mirror
4623 foreach (const QString objectName, objectNames)
4624 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4630 void tst_QQuickListView::margins()
4632 QQuickView *canvas = createView();
4635 for (int i = 0; i < 50; i++)
4636 model.addItem("Item" + QString::number(i), "");
4638 QQmlContext *ctxt = canvas->rootContext();
4639 ctxt->setContextProperty("testModel", &model);
4641 canvas->setSource(testFileUrl("margins.qml"));
4643 qApp->processEvents();
4645 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4646 QTRY_VERIFY(listview != 0);
4647 QQuickItem *contentItem = listview->contentItem();
4648 QTRY_VERIFY(contentItem != 0);
4649 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4651 QCOMPARE(listview->contentY(), -30.);
4652 QCOMPARE(listview->originY(), 0.);
4655 listview->positionViewAtEnd();
4656 qreal pos = listview->contentY();
4657 listview->setContentY(pos + 80);
4658 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4659 listview->returnToBounds();
4660 QTRY_COMPARE(listview->contentY(), pos + 50);
4662 // remove item before visible and check that top margin is maintained
4663 // and originY is updated
4664 listview->setContentY(100);
4665 model.removeItem(1);
4666 QTRY_COMPARE(listview->count(), model.count());
4667 listview->setContentY(-50);
4668 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4669 listview->returnToBounds();
4670 QCOMPARE(listview->originY(), 20.);
4671 QTRY_COMPARE(listview->contentY(), -10.);
4673 // reduce top margin
4674 listview->setTopMargin(20);
4675 QCOMPARE(listview->originY(), 20.);
4676 QTRY_COMPARE(listview->contentY(), 0.);
4679 listview->positionViewAtEnd();
4680 pos = listview->contentY();
4681 listview->setContentY(pos + 80);
4682 listview->returnToBounds();
4683 QTRY_COMPARE(listview->contentY(), pos + 50);
4685 // reduce bottom margin
4686 pos = listview->contentY();
4687 listview->setBottomMargin(40);
4688 QCOMPARE(listview->originY(), 20.);
4689 QTRY_COMPARE(listview->contentY(), pos-10);
4695 void tst_QQuickListView::marginsResize()
4697 QFETCH(QQuickListView::Orientation, orientation);
4698 QFETCH(Qt::LayoutDirection, layoutDirection);
4699 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4700 QFETCH(qreal, start);
4703 QPoint flickStart(20, 20);
4704 QPoint flickEnd(20, 20);
4705 if (orientation == QQuickListView::Vertical)
4706 flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4708 flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4710 QQuickView *canvas = getView();
4712 canvas->setSource(testFileUrl("margins2.qml"));
4714 qApp->processEvents();
4716 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4717 QTRY_VERIFY(listview != 0);
4719 listview->setOrientation(orientation);
4720 listview->setLayoutDirection(layoutDirection);
4721 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4722 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4724 // view is resized after componentCompleted - top margin should still be visible
4725 if (orientation == QQuickListView::Vertical)
4726 QCOMPARE(listview->contentY(), start);
4728 QCOMPARE(listview->contentX(), start);
4730 // move to last index and ensure bottom margin is visible.
4731 listview->setCurrentIndex(19);
4732 if (orientation == QQuickListView::Vertical)
4733 QTRY_COMPARE(listview->contentY(), end);
4735 QTRY_COMPARE(listview->contentX(), end);
4737 // flick past the end and check content pos still settles on correct extents
4738 flick(canvas, flickStart, flickEnd, 180);
4739 QTRY_VERIFY(listview->isMoving() == false);
4740 if (orientation == QQuickListView::Vertical)
4741 QTRY_COMPARE(listview->contentY(), end);
4743 QTRY_COMPARE(listview->contentX(), end);
4745 // back to top - top margin should be visible.
4746 listview->setCurrentIndex(0);
4747 if (orientation == QQuickListView::Vertical)
4748 QTRY_COMPARE(listview->contentY(), start);
4750 QTRY_COMPARE(listview->contentX(), start);
4752 // flick past the beginning and check content pos still settles on correct extents
4753 flick(canvas, flickEnd, flickStart, 180);
4754 QTRY_VERIFY(listview->isMoving() == false);
4755 if (orientation == QQuickListView::Vertical)
4756 QTRY_COMPARE(listview->contentY(), start);
4758 QTRY_COMPARE(listview->contentX(), start);
4760 releaseView(canvas);
4763 void tst_QQuickListView::marginsResize_data()
4765 QTest::addColumn<QQuickListView::Orientation>("orientation");
4766 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4767 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4768 QTest::addColumn<qreal>("start");
4769 QTest::addColumn<qreal>("end");
4771 // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4773 QTest::newRow("vertical")
4774 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4777 QTest::newRow("vertical, BottomToTop")
4778 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4779 << -180.0 << -1240.0;
4781 QTest::newRow("horizontal")
4782 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4785 QTest::newRow("horizontal, rtl")
4786 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4787 << -180.0 << -1240.0;
4790 void tst_QQuickListView::snapToItem_data()
4792 QTest::addColumn<QQuickListView::Orientation>("orientation");
4793 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4794 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4795 QTest::addColumn<int>("highlightRangeMode");
4796 QTest::addColumn<QPoint>("flickStart");
4797 QTest::addColumn<QPoint>("flickEnd");
4798 QTest::addColumn<qreal>("snapAlignment");
4799 QTest::addColumn<qreal>("endExtent");
4800 QTest::addColumn<qreal>("startExtent");
4802 QTest::newRow("vertical, top to bottom")
4803 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4804 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4806 QTest::newRow("vertical, bottom to top")
4807 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4808 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4810 QTest::newRow("horizontal, left to right")
4811 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4812 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4814 QTest::newRow("horizontal, right to left")
4815 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4816 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4818 QTest::newRow("vertical, top to bottom, enforce range")
4819 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4820 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4822 QTest::newRow("vertical, bottom to top, enforce range")
4823 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4824 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4826 QTest::newRow("horizontal, left to right, enforce range")
4827 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4828 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4830 QTest::newRow("horizontal, right to left, enforce range")
4831 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4832 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4835 void tst_QQuickListView::snapToItem()
4837 QFETCH(QQuickListView::Orientation, orientation);
4838 QFETCH(Qt::LayoutDirection, layoutDirection);
4839 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4840 QFETCH(int, highlightRangeMode);
4841 QFETCH(QPoint, flickStart);
4842 QFETCH(QPoint, flickEnd);
4843 QFETCH(qreal, snapAlignment);
4844 QFETCH(qreal, endExtent);
4845 QFETCH(qreal, startExtent);
4847 QQuickView *canvas = getView();
4849 canvas->setSource(testFileUrl("snapToItem.qml"));
4851 qApp->processEvents();
4853 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4854 QTRY_VERIFY(listview != 0);
4856 listview->setOrientation(orientation);
4857 listview->setLayoutDirection(layoutDirection);
4858 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4859 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4860 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4862 QQuickItem *contentItem = listview->contentItem();
4863 QTRY_VERIFY(contentItem != 0);
4865 // confirm that a flick hits an item boundary
4866 flick(canvas, flickStart, flickEnd, 180);
4867 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4868 if (orientation == QQuickListView::Vertical)
4869 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4871 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4875 flick(canvas, flickStart, flickEnd, 180);
4876 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4877 } while (orientation == QQuickListView::Vertical
4878 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4879 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4881 if (orientation == QQuickListView::Vertical)
4882 QCOMPARE(listview->contentY(), endExtent);
4884 QCOMPARE(listview->contentX(), endExtent);
4888 flick(canvas, flickEnd, flickStart, 180);
4889 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4890 } while (orientation == QQuickListView::Vertical
4891 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
4892 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4894 if (orientation == QQuickListView::Vertical)
4895 QCOMPARE(listview->contentY(), startExtent);
4897 QCOMPARE(listview->contentX(), startExtent);
4899 releaseView(canvas);
4902 void tst_QQuickListView::qListModelInterface_items()
4904 items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4907 void tst_QQuickListView::qListModelInterface_package_items()
4909 items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4912 void tst_QQuickListView::qAbstractItemModel_items()
4914 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
4917 void tst_QQuickListView::qListModelInterface_changed()
4919 changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4922 void tst_QQuickListView::qListModelInterface_package_changed()
4924 changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4927 void tst_QQuickListView::qAbstractItemModel_changed()
4929 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
4932 void tst_QQuickListView::qListModelInterface_inserted()
4934 inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
4937 void tst_QQuickListView::qListModelInterface_package_inserted()
4939 inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4942 void tst_QQuickListView::qListModelInterface_inserted_more()
4944 inserted_more<QmlListModel>();
4947 void tst_QQuickListView::qListModelInterface_inserted_more_data()
4949 inserted_more_data();
4952 void tst_QQuickListView::qAbstractItemModel_inserted()
4954 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
4957 void tst_QQuickListView::qAbstractItemModel_inserted_more()
4959 inserted_more<QaimModel>();
4962 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
4964 inserted_more_data();
4967 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
4969 inserted_more<QaimModel>(QQuickItemView::BottomToTop);
4972 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
4974 inserted_more_data();
4977 void tst_QQuickListView::qListModelInterface_removed()
4979 removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4980 removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
4983 void tst_QQuickListView::qListModelInterface_removed_more()
4985 removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
4988 void tst_QQuickListView::qListModelInterface_removed_more_data()
4990 removed_more_data();
4993 void tst_QQuickListView::qListModelInterface_package_removed()
4995 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
4996 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4999 void tst_QQuickListView::qAbstractItemModel_removed()
5001 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5002 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5005 void tst_QQuickListView::qAbstractItemModel_removed_more()
5007 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5010 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5012 removed_more_data();
5015 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5017 removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5020 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5022 removed_more_data();
5025 void tst_QQuickListView::qListModelInterface_moved()
5027 moved<QmlListModel>(testFileUrl("listviewtest.qml"));
5030 void tst_QQuickListView::qListModelInterface_moved_data()
5035 void tst_QQuickListView::qListModelInterface_package_moved()
5037 moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5040 void tst_QQuickListView::qListModelInterface_package_moved_data()
5045 void tst_QQuickListView::qAbstractItemModel_moved()
5047 moved<QaimModel>(testFileUrl("listviewtest.qml"));
5050 void tst_QQuickListView::qAbstractItemModel_moved_data()
5055 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5057 moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5060 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5065 void tst_QQuickListView::qListModelInterface_clear()
5067 clear<QmlListModel>(testFileUrl("listviewtest.qml"));
5070 void tst_QQuickListView::qListModelInterface_package_clear()
5072 clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
5075 void tst_QQuickListView::qAbstractItemModel_clear()
5077 clear<QaimModel>(testFileUrl("listviewtest.qml"));
5080 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5082 clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5085 void tst_QQuickListView::qListModelInterface_sections()
5087 sections<QmlListModel>(testFileUrl("listview-sections.qml"));
5090 void tst_QQuickListView::qListModelInterface_package_sections()
5092 sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
5095 void tst_QQuickListView::qAbstractItemModel_sections()
5097 sections<QaimModel>(testFileUrl("listview-sections.qml"));
5100 void tst_QQuickListView::creationContext()
5103 canvas.setGeometry(0,0,240,320);
5104 canvas.setSource(testFileUrl("creationContext.qml"));
5105 qApp->processEvents();
5107 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5109 QVERIFY(rootItem->property("count").toInt() > 0);
5112 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5113 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5114 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5115 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5116 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5117 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5118 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5119 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5122 void tst_QQuickListView::QTBUG_21742()
5125 canvas.setGeometry(0,0,200,200);
5126 canvas.setSource(testFileUrl("qtbug-21742.qml"));
5127 qApp->processEvents();
5129 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
5131 QCOMPARE(rootItem->property("count").toInt(), 1);
5134 void tst_QQuickListView::asynchronous()
5136 QQuickView *canvas = createView();
5138 QQmlIncubationController controller;
5139 canvas->engine()->setIncubationController(&controller);
5141 canvas->setSource(testFileUrl("asyncloader.qml"));
5143 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5144 QVERIFY(rootObject);
5146 QQuickListView *listview = 0;
5149 controller.incubateWhile(&b);
5150 listview = rootObject->findChild<QQuickListView*>("view");
5153 // items will be created one at a time
5154 for (int i = 0; i < 8; ++i) {
5155 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5156 QQuickItem *item = 0;
5159 controller.incubateWhile(&b);
5160 item = findItem<QQuickItem>(listview, "wrapper", i);
5166 controller.incubateWhile(&b);
5169 // verify positioning
5170 QQuickItem *contentItem = listview->contentItem();
5171 for (int i = 0; i < 8; ++i) {
5172 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5173 QTRY_COMPARE(item->y(), i*50.0);
5179 void tst_QQuickListView::snapOneItem_data()
5181 QTest::addColumn<QQuickListView::Orientation>("orientation");
5182 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5183 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5184 QTest::addColumn<int>("highlightRangeMode");
5185 QTest::addColumn<QPoint>("flickStart");
5186 QTest::addColumn<QPoint>("flickEnd");
5187 QTest::addColumn<qreal>("snapAlignment");
5188 QTest::addColumn<qreal>("endExtent");
5189 QTest::addColumn<qreal>("startExtent");
5191 QTest::newRow("vertical, top to bottom")
5192 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5193 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5195 QTest::newRow("vertical, bottom to top")
5196 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5197 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5199 QTest::newRow("horizontal, left to right")
5200 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5201 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5203 QTest::newRow("horizontal, right to left")
5204 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5205 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5207 QTest::newRow("vertical, top to bottom, enforce range")
5208 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5209 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5211 QTest::newRow("vertical, bottom to top, enforce range")
5212 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5213 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5215 QTest::newRow("horizontal, left to right, enforce range")
5216 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5217 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5219 QTest::newRow("horizontal, right to left, enforce range")
5220 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5221 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5224 void tst_QQuickListView::snapOneItem()
5226 QFETCH(QQuickListView::Orientation, orientation);
5227 QFETCH(Qt::LayoutDirection, layoutDirection);
5228 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5229 QFETCH(int, highlightRangeMode);
5230 QFETCH(QPoint, flickStart);
5231 QFETCH(QPoint, flickEnd);
5232 QFETCH(qreal, snapAlignment);
5233 QFETCH(qreal, endExtent);
5234 QFETCH(qreal, startExtent);
5237 // This test seems to be unreliable - different test data fails on different runs
5238 QSKIP("QTBUG-24338");
5241 QQuickView *canvas = getView();
5243 canvas->setSource(testFileUrl("snapOneItem.qml"));
5245 qApp->processEvents();
5247 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5248 QTRY_VERIFY(listview != 0);
5250 listview->setOrientation(orientation);
5251 listview->setLayoutDirection(layoutDirection);
5252 listview->setVerticalLayoutDirection(verticalLayoutDirection);
5253 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5254 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5256 QQuickItem *contentItem = listview->contentItem();
5257 QTRY_VERIFY(contentItem != 0);
5259 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5261 // confirm that a flick hits the next item boundary
5262 flick(canvas, flickStart, flickEnd, 180);
5263 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5264 if (orientation == QQuickListView::Vertical)
5265 QCOMPARE(listview->contentY(), snapAlignment);
5267 QCOMPARE(listview->contentX(), snapAlignment);
5269 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5270 QCOMPARE(listview->currentIndex(), 1);
5271 QCOMPARE(currentIndexSpy.count(), 1);
5276 flick(canvas, flickStart, flickEnd, 180);
5277 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5278 } while (orientation == QQuickListView::Vertical
5279 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5280 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5282 if (orientation == QQuickListView::Vertical)
5283 QCOMPARE(listview->contentY(), endExtent);
5285 QCOMPARE(listview->contentX(), endExtent);
5287 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5288 QCOMPARE(listview->currentIndex(), 3);
5289 QCOMPARE(currentIndexSpy.count(), 3);
5294 flick(canvas, flickEnd, flickStart, 180);
5295 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5296 } while (orientation == QQuickListView::Vertical
5297 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5298 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5300 if (orientation == QQuickListView::Vertical)
5301 QCOMPARE(listview->contentY(), startExtent);
5303 QCOMPARE(listview->contentX(), startExtent);
5305 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5306 QCOMPARE(listview->currentIndex(), 0);
5307 QCOMPARE(currentIndexSpy.count(), 6);
5310 releaseView(canvas);
5313 void tst_QQuickListView::unrequestedVisibility()
5316 for (int i = 0; i < 30; i++)
5317 model.addItem("Item" + QString::number(i), QString::number(i));
5319 QQuickView *canvas = new QQuickView(0);
5320 canvas->setGeometry(0,0,240,320);
5322 QQmlContext *ctxt = canvas->rootContext();
5323 ctxt->setContextProperty("testModel", &model);
5324 ctxt->setContextProperty("testWrap", QVariant(false));
5326 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5328 qApp->processEvents();
5330 QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
5331 QTRY_VERIFY(leftview != 0);
5333 QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
5334 QTRY_VERIFY(rightview != 0);
5336 QQuickItem *leftContent = leftview->contentItem();
5337 QTRY_VERIFY(leftContent != 0);
5339 QQuickItem *rightContent = rightview->contentItem();
5340 QTRY_VERIFY(rightContent != 0);
5342 rightview->setCurrentIndex(20);
5344 QTRY_COMPARE(leftview->contentY(), 0.0);
5345 QTRY_COMPARE(rightview->contentY(), 100.0);
5349 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5350 QCOMPARE(delegateVisible(item), true);
5351 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5352 QCOMPARE(delegateVisible(item), false);
5354 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5355 QCOMPARE(delegateVisible(item), false);
5356 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5357 QCOMPARE(delegateVisible(item), true);
5359 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5360 QCOMPARE(delegateVisible(item), true);
5361 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5362 QCOMPARE(delegateVisible(item), false);
5363 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5364 QCOMPARE(delegateVisible(item), false);
5365 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5366 QCOMPARE(delegateVisible(item), true);
5368 rightview->setCurrentIndex(0);
5370 QTRY_COMPARE(leftview->contentY(), 0.0);
5371 QTRY_COMPARE(rightview->contentY(), 0.0);
5373 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5374 QCOMPARE(delegateVisible(item), true);
5375 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5376 QTRY_COMPARE(delegateVisible(item), true);
5378 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5379 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5381 leftview->setCurrentIndex(20);
5383 QTRY_COMPARE(leftview->contentY(), 100.0);
5384 QTRY_COMPARE(rightview->contentY(), 0.0);
5386 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5387 QTRY_COMPARE(delegateVisible(item), false);
5388 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5389 QCOMPARE(delegateVisible(item), true);
5391 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5392 QCOMPARE(delegateVisible(item), true);
5393 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5394 QCOMPARE(delegateVisible(item), false);
5396 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5397 QCOMPARE(delegateVisible(item), false);
5398 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5399 QCOMPARE(delegateVisible(item), true);
5400 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5401 QCOMPARE(delegateVisible(item), true);
5402 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5403 QCOMPARE(delegateVisible(item), false);
5405 model.moveItems(19, 1, 1);
5406 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5408 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5409 QCOMPARE(delegateVisible(item), false);
5410 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5411 QCOMPARE(delegateVisible(item), true);
5413 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5414 QCOMPARE(delegateVisible(item), true);
5415 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5416 QCOMPARE(delegateVisible(item), false);
5418 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5419 QCOMPARE(delegateVisible(item), false);
5420 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5421 QCOMPARE(delegateVisible(item), true);
5422 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5423 QCOMPARE(delegateVisible(item), true);
5424 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5425 QCOMPARE(delegateVisible(item), false);
5427 model.moveItems(3, 4, 1);
5428 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5430 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5431 QCOMPARE(delegateVisible(item), false);
5432 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5433 QCOMPARE(delegateVisible(item), true);
5434 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5435 QCOMPARE(delegateVisible(item), true);
5436 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5437 QCOMPARE(delegateVisible(item), false);
5439 model.moveItems(4, 3, 1);
5440 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5442 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5443 QCOMPARE(delegateVisible(item), false);
5444 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5445 QCOMPARE(delegateVisible(item), true);
5446 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5447 QCOMPARE(delegateVisible(item), true);
5448 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5449 QCOMPARE(delegateVisible(item), false);
5451 model.moveItems(16, 17, 1);
5452 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5454 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5455 QCOMPARE(delegateVisible(item), false);
5456 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5457 QCOMPARE(delegateVisible(item), true);
5458 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5459 QCOMPARE(delegateVisible(item), true);
5460 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5461 QCOMPARE(delegateVisible(item), false);
5463 model.moveItems(17, 16, 1);
5464 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5466 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5467 QCOMPARE(delegateVisible(item), false);
5468 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5469 QCOMPARE(delegateVisible(item), true);
5470 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5471 QCOMPARE(delegateVisible(item), true);
5472 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5473 QCOMPARE(delegateVisible(item), false);
5478 void tst_QQuickListView::populateTransitions()
5480 QFETCH(bool, staticallyPopulate);
5481 QFETCH(bool, dynamicallyPopulate);
5482 QFETCH(bool, usePopulateTransition);
5484 QPointF transitionFrom(-50, -50);
5485 QPointF transitionVia(100, 100);
5486 QaimModel model_transitionFrom;
5487 QaimModel model_transitionVia;
5490 if (staticallyPopulate) {
5491 for (int i = 0; i < 30; i++)
5492 model.addItem("item" + QString::number(i), "");
5495 QQuickView *canvas = getView();
5496 canvas->rootContext()->setContextProperty("testModel", &model);
5497 canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
5498 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5499 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5500 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5501 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
5502 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5503 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5504 canvas->setSource(testFileUrl("populateTransitions.qml"));
5506 QTest::qWaitForWindowShown(canvas);
5508 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5510 QQuickItem *contentItem = listview->contentItem();
5511 QVERIFY(contentItem);
5513 if (staticallyPopulate && usePopulateTransition) {
5514 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5515 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5516 } else if (dynamicallyPopulate) {
5517 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5518 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5520 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5521 QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5522 QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5525 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5526 for (int i=0; i < model.count() && i < itemCount; ++i) {
5527 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5528 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5529 QTRY_COMPARE(item->x(), 0.0);
5530 QTRY_COMPARE(item->y(), i*20.0);
5531 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5533 QTRY_COMPARE(name->text(), model.name(i));
5536 listview->setProperty("countPopulateTransitions", 0);
5537 listview->setProperty("countAddTransitions", 0);
5539 // add an item and check this is done with add transition, not populate
5540 model.insertItem(0, "another item", "");
5541 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5542 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5545 canvas->rootContext()->setContextProperty("testModel", QVariant());
5546 QTRY_COMPARE(listview->count(), 0);
5547 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5548 listview->setProperty("countPopulateTransitions", 0);
5549 listview->setProperty("countAddTransitions", 0);
5551 // set to a valid model and check populate transition is run a second time
5553 for (int i = 0; i < 30; i++)
5554 model.addItem("item" + QString::number(i), "");
5555 canvas->rootContext()->setContextProperty("testModel", &model);
5556 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5557 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5559 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5560 for (int i=0; i < model.count() && i < itemCount; ++i) {
5561 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5562 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5563 QTRY_COMPARE(item->x(), 0.0);
5564 QTRY_COMPARE(item->y(), i*20.0);
5565 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5567 QTRY_COMPARE(name->text(), model.name(i));
5570 // reset model and check populate transition is run again
5571 listview->setProperty("countPopulateTransitions", 0);
5572 listview->setProperty("countAddTransitions", 0);
5574 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5575 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5577 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5578 for (int i=0; i < model.count() && i < itemCount; ++i) {
5579 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5580 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5581 QTRY_COMPARE(item->x(), 0.0);
5582 QTRY_COMPARE(item->y(), i*20.0);
5583 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5585 QTRY_COMPARE(name->text(), model.name(i));
5588 releaseView(canvas);
5591 void tst_QQuickListView::populateTransitions_data()
5593 QTest::addColumn<bool>("staticallyPopulate");
5594 QTest::addColumn<bool>("dynamicallyPopulate");
5595 QTest::addColumn<bool>("usePopulateTransition");
5597 QTest::newRow("static") << true << false << true;
5598 QTest::newRow("static, no populate") << true << false << false;
5600 QTest::newRow("dynamic") << false << true << true;
5601 QTest::newRow("dynamic, no populate") << false << true << false;
5603 QTest::newRow("empty to start with") << false << false << true;
5604 QTest::newRow("empty to start with, no populate") << false << false << false;
5607 void tst_QQuickListView::addTransitions()
5609 QFETCH(int, initialItemCount);
5610 QFETCH(bool, shouldAnimateTargets);
5611 QFETCH(qreal, contentY);
5612 QFETCH(int, insertionIndex);
5613 QFETCH(int, insertionCount);
5614 QFETCH(ListRange, expectedDisplacedIndexes);
5616 // added items should start here
5617 QPointF targetItems_transitionFrom(-50, -50);
5619 // displaced items should pass through this point
5620 QPointF displacedItems_transitionVia(100, 100);
5623 for (int i = 0; i < initialItemCount; i++)
5624 model.addItem("Original item" + QString::number(i), "");
5625 QaimModel model_targetItems_transitionFrom;
5626 QaimModel model_displacedItems_transitionVia;
5628 QQuickView *canvas = getView();
5629 QQmlContext *ctxt = canvas->rootContext();
5630 TestObject *testObject = new TestObject;
5631 ctxt->setContextProperty("testModel", &model);
5632 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5633 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5634 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5635 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5636 ctxt->setContextProperty("testObject", testObject);
5637 canvas->setSource(testFileUrl("addTransitions.qml"));
5639 QTest::qWaitForWindowShown(canvas);
5641 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5642 QTRY_VERIFY(listview != 0);
5643 QQuickItem *contentItem = listview->contentItem();
5644 QVERIFY(contentItem != 0);
5645 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5647 if (contentY != 0) {
5648 listview->setContentY(contentY);
5649 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5652 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5654 // only target items that will become visible should be animated
5655 QList<QPair<QString, QString> > newData;
5656 QList<QPair<QString, QString> > expectedTargetData;
5657 QList<int> targetIndexes;
5658 if (shouldAnimateTargets) {
5659 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5660 newData << qMakePair(QString("New item %1").arg(i), QString(""));
5662 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
5663 expectedTargetData << newData.last();
5667 QVERIFY(expectedTargetData.count() > 0);
5671 if (!newData.isEmpty()) {
5672 model.insertItems(insertionIndex, newData);
5673 QTRY_COMPARE(model.count(), listview->count());
5676 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5678 if (shouldAnimateTargets) {
5679 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5680 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5681 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5683 // check the target and displaced items were animated
5684 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5685 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5687 // check attached properties
5688 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5689 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5690 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5691 if (expectedDisplacedIndexes.isValid()) {
5692 // adjust expectedDisplacedIndexes to their final values after the move
5693 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5694 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5695 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5696 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5700 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5701 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5704 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5705 int firstVisibleIndex = -1;
5706 int itemCount = items.count();
5707 for (int i=0; i<items.count(); i++) {
5708 if (items[i]->y() >= contentY) {
5709 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5710 firstVisibleIndex = e.evaluate().toInt();
5714 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5716 // verify all items moved to the correct final positions
5717 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5718 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5719 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5720 QTRY_COMPARE(item->y(), i*20.0);
5721 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5723 QTRY_COMPARE(name->text(), model.name(i));
5726 releaseView(canvas);
5730 void tst_QQuickListView::addTransitions_data()
5732 QTest::addColumn<int>("initialItemCount");
5733 QTest::addColumn<qreal>("contentY");
5734 QTest::addColumn<bool>("shouldAnimateTargets");
5735 QTest::addColumn<int>("insertionIndex");
5736 QTest::addColumn<int>("insertionCount");
5737 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5739 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5740 QTest::newRow("insert 1, just before start")
5741 << 30 << 20.0 << false
5742 << 0 << 1 << ListRange();
5743 QTest::newRow("insert 1, way before start")
5744 << 30 << 20.0 << false
5745 << 0 << 1 << ListRange();
5746 QTest::newRow("insert multiple, just before start")
5747 << 30 << 100.0 << false
5748 << 0 << 3 << ListRange();
5749 QTest::newRow("insert multiple, way before start")
5750 << 30 << 100.0 << false
5751 << 0 << 3 << ListRange();
5753 QTest::newRow("insert 1 at start")
5754 << 30 << 0.0 << true
5755 << 0 << 1 << ListRange(0, 15);
5756 QTest::newRow("insert multiple at start")
5757 << 30 << 0.0 << true
5758 << 0 << 3 << ListRange(0, 15);
5759 QTest::newRow("insert 1 at start, content y not 0")
5760 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5761 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5762 QTest::newRow("insert multiple at start, content y not 0")
5763 << 30 << 40.0 << true // first visible is index 2
5764 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5766 QTest::newRow("insert 1 at start, to empty list")
5768 << 0 << 1 << ListRange();
5769 QTest::newRow("insert multiple at start, to empty list")
5771 << 0 << 3 << ListRange();
5773 QTest::newRow("insert 1 at middle")
5774 << 30 << 0.0 << true
5775 << 5 << 1 << ListRange(5, 15);
5776 QTest::newRow("insert multiple at middle")
5777 << 30 << 0.0 << true
5778 << 5 << 3 << ListRange(5, 15);
5780 QTest::newRow("insert 1 at bottom")
5781 << 30 << 0.0 << true
5782 << 15 << 1 << ListRange(15, 15);
5783 QTest::newRow("insert multiple at bottom")
5784 << 30 << 0.0 << true
5785 << 15 << 3 << ListRange(15, 15);
5786 QTest::newRow("insert 1 at bottom, content y not 0")
5787 << 30 << 20.0 * 3 << true
5788 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5789 QTest::newRow("insert multiple at bottom, content y not 0")
5790 << 30 << 20.0 * 3 << true
5791 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5793 // items added after the last visible will not be animated in, since they
5794 // do not appear in the final view
5795 QTest::newRow("insert 1 after end")
5796 << 30 << 0.0 << false
5797 << 17 << 1 << ListRange();
5798 QTest::newRow("insert multiple after end")
5799 << 30 << 0.0 << false
5800 << 17 << 3 << ListRange();
5803 void tst_QQuickListView::moveTransitions()
5805 QFETCH(int, initialItemCount);
5806 QFETCH(qreal, contentY);
5807 QFETCH(qreal, itemsOffsetAfterMove);
5808 QFETCH(int, moveFrom);
5809 QFETCH(int, moveTo);
5810 QFETCH(int, moveCount);
5811 QFETCH(ListRange, expectedDisplacedIndexes);
5813 // target and displaced items should pass through these points
5814 QPointF targetItems_transitionVia(-50, 50);
5815 QPointF displacedItems_transitionVia(100, 100);
5818 for (int i = 0; i < initialItemCount; i++)
5819 model.addItem("Original item" + QString::number(i), "");
5820 QaimModel model_targetItems_transitionVia;
5821 QaimModel model_displacedItems_transitionVia;
5823 QQuickView *canvas = getView();
5824 QQmlContext *ctxt = canvas->rootContext();
5825 TestObject *testObject = new TestObject;
5826 ctxt->setContextProperty("testModel", &model);
5827 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5828 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5829 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5830 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5831 ctxt->setContextProperty("testObject", testObject);
5832 canvas->setSource(testFileUrl("moveTransitions.qml"));
5834 QTest::qWaitForWindowShown(canvas);
5836 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5837 QTRY_VERIFY(listview != 0);
5838 QQuickItem *contentItem = listview->contentItem();
5839 QVERIFY(contentItem != 0);
5842 if (contentY != 0) {
5843 listview->setContentY(contentY);
5844 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5847 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5849 // Items moving to *or* from visible positions should be animated.
5850 // Otherwise, they should not be animated.
5851 QList<QPair<QString, QString> > expectedTargetData;
5852 QList<int> targetIndexes;
5853 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5854 int toIndex = moveTo + (i - moveFrom);
5855 if (i <= (contentY + listview->height()) / 20
5856 || toIndex < (contentY + listview->height()) / 20) {
5857 expectedTargetData << qMakePair(model.name(i), model.number(i));
5861 // ViewTransition.index provides the indices that items are moving to, not from
5862 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5865 model.moveItems(moveFrom, moveTo, moveCount);
5867 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5868 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5869 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5871 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5873 // check the target and displaced items were animated
5874 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5875 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5877 // check attached properties
5878 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5879 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5880 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5881 if (expectedDisplacedIndexes.isValid()) {
5882 // adjust expectedDisplacedIndexes to their final values after the move
5883 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5884 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5885 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5886 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5889 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5890 int firstVisibleIndex = -1;
5891 for (int i=0; i<items.count(); i++) {
5892 if (items[i]->y() >= contentY) {
5893 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5894 firstVisibleIndex = e.evaluate().toInt();
5898 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5900 // verify all items moved to the correct final positions
5901 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5902 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5903 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5904 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5905 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
5906 name = findItem<QQuickText>(contentItem, "textName", i);
5908 QTRY_COMPARE(name->text(), model.name(i));
5911 releaseView(canvas);
5915 void tst_QQuickListView::moveTransitions_data()
5917 QTest::addColumn<int>("initialItemCount");
5918 QTest::addColumn<qreal>("contentY");
5919 QTest::addColumn<qreal>("itemsOffsetAfterMove");
5920 QTest::addColumn<int>("moveFrom");
5921 QTest::addColumn<int>("moveTo");
5922 QTest::addColumn<int>("moveCount");
5923 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5925 // when removing from above the visible, all items shift down depending on how many
5926 // items have been removed from above the visible
5927 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
5928 << 1 << 10 << 1 << ListRange(11, 15+4);
5929 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
5930 << 0 << 10 << 1 << ListRange(11, 15+4);
5931 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
5932 << 1 << 10 << 2 << ListRange(12, 15+4);
5933 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
5934 << 0 << 10 << 3 << ListRange(13, 15+4);
5935 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
5936 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
5937 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
5938 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
5940 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
5941 << 1 << 10 << 1 << ListRange(2, 10);
5942 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
5943 << 0 << 10 << 1 << ListRange(1, 10);
5944 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5945 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
5946 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
5947 << 10 << 15 << 1 << ListRange(11, 15);
5948 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
5949 << 0 << 15 << 1 << ListRange(1, 15);
5951 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
5952 << 1 << 10 << 3 << ListRange(4, 12);
5953 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
5954 << 0 << 10 << 3 << ListRange(3, 12);
5955 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5956 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
5957 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
5958 << 5 << 13 << 3 << ListRange(8, 15);
5959 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
5960 << 0 << 13 << 3 << ListRange(3, 15);
5962 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
5963 << 10 << 1 << 1 << ListRange(1, 9);
5964 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
5965 << 10 << 0 << 1 << ListRange(0, 9);
5966 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5967 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5968 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
5969 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5970 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
5971 << 15 << 10 << 1 << ListRange(10, 14);
5972 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
5973 << 15 << 0 << 1 << ListRange(0, 14);
5975 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
5976 << 10 << 1 << 3 << ListRange(1, 9);
5977 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
5978 << 10 << 0 << 3 << ListRange(0, 9);
5979 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5980 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
5981 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
5982 << 13 << 5 << 3 << ListRange(5, 12);
5983 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
5984 << 13 << 0 << 3 << ListRange(0, 12);
5986 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
5987 << 20 << 0 << 1 << ListRange(0, 15);
5988 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
5989 << 25 << 4 << 1 << ListRange(0+4, 15+4);
5990 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
5991 << 20 << 0 << 3 << ListRange(0, 15);
5992 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
5993 << 25 << 4 << 3 << ListRange(0+4, 15+4);
5995 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
5996 << 20 << 15 << 1 << ListRange(15, 15);
5997 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
5998 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
5999 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6000 << 20 << 15 << 3 << ListRange(15, 15);
6001 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6002 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6005 void tst_QQuickListView::removeTransitions()
6007 QFETCH(int, initialItemCount);
6008 QFETCH(bool, shouldAnimateTargets);
6009 QFETCH(qreal, contentY);
6010 QFETCH(int, removalIndex);
6011 QFETCH(int, removalCount);
6012 QFETCH(ListRange, expectedDisplacedIndexes);
6014 // added items should end here
6015 QPointF targetItems_transitionTo(-50, -50);
6017 // displaced items should pass through this points
6018 QPointF displacedItems_transitionVia(100, 100);
6021 for (int i = 0; i < initialItemCount; i++)
6022 model.addItem("Original item" + QString::number(i), "");
6023 QaimModel model_targetItems_transitionTo;
6024 QaimModel model_displacedItems_transitionVia;
6026 QQuickView *canvas = getView();
6027 QQmlContext *ctxt = canvas->rootContext();
6028 TestObject *testObject = new TestObject;
6029 ctxt->setContextProperty("testModel", &model);
6030 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6031 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6032 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6033 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6034 ctxt->setContextProperty("testObject", testObject);
6035 canvas->setSource(testFileUrl("removeTransitions.qml"));
6037 QTest::qWaitForWindowShown(canvas);
6039 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6040 QTRY_VERIFY(listview != 0);
6041 QQuickItem *contentItem = listview->contentItem();
6042 QVERIFY(contentItem != 0);
6043 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6045 if (contentY != 0) {
6046 listview->setContentY(contentY);
6047 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6050 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6052 // only target items that are visible should be animated
6053 QList<QPair<QString, QString> > expectedTargetData;
6054 QList<int> targetIndexes;
6055 if (shouldAnimateTargets) {
6056 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6057 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6058 expectedTargetData << qMakePair(model.name(i), model.number(i));
6062 QVERIFY(expectedTargetData.count() > 0);
6065 // calculate targetItems and expectedTargets before model changes
6066 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6067 QVariantMap expectedTargets;
6068 for (int i=0; i<targetIndexes.count(); i++)
6069 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6072 model.removeItems(removalIndex, removalCount);
6073 QTRY_COMPARE(model.count(), listview->count());
6075 if (shouldAnimateTargets) {
6076 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6077 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6078 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6080 // check the target and displaced items were animated
6081 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6082 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6084 // check attached properties
6085 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6086 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6087 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6088 if (expectedDisplacedIndexes.isValid()) {
6089 // adjust expectedDisplacedIndexes to their final values after the move
6090 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6091 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6092 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6093 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6096 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6097 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6100 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6101 int firstVisibleIndex = -1;
6102 int itemCount = items.count();
6104 for (int i=0; i<items.count(); i++) {
6105 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6106 int index = e.evaluate().toInt();
6107 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6108 firstVisibleIndex = index;
6110 itemCount--; // exclude deleted items
6112 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6114 // verify all items moved to the correct final positions
6115 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6116 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6117 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6118 QCOMPARE(item->x(), 0.0);
6119 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6120 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6122 QTRY_COMPARE(name->text(), model.name(i));
6125 releaseView(canvas);
6129 void tst_QQuickListView::removeTransitions_data()
6131 QTest::addColumn<int>("initialItemCount");
6132 QTest::addColumn<qreal>("contentY");
6133 QTest::addColumn<bool>("shouldAnimateTargets");
6134 QTest::addColumn<int>("removalIndex");
6135 QTest::addColumn<int>("removalCount");
6136 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6138 // All items that are visible following the remove operation should be animated.
6139 // Remove targets that are outside of the view should not be animated.
6141 QTest::newRow("remove 1 before start")
6142 << 30 << 20.0 * 3 << false
6143 << 2 << 1 << ListRange();
6144 QTest::newRow("remove multiple, all before start")
6145 << 30 << 20.0 * 3 << false
6146 << 0 << 3 << ListRange();
6147 QTest::newRow("remove mix of before and after start")
6148 << 30 << 20.0 * 3 << true
6149 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
6151 QTest::newRow("remove 1 from start")
6152 << 30 << 0.0 << true
6153 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
6154 QTest::newRow("remove multiple from start")
6155 << 30 << 0.0 << true
6156 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
6157 QTest::newRow("remove 1 from start, content y not 0")
6158 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
6159 << 2 << 1 << ListRange(1 + 2, 16 + 2);
6160 QTest::newRow("remove multiple from start, content y not 0")
6161 << 30 << 20.0 * 2 << true // first visible is index 2
6162 << 2 << 3 << ListRange(3 + 2, 18 + 2);
6164 QTest::newRow("remove 1 from middle")
6165 << 30 << 0.0 << true
6166 << 5 << 1 << ListRange(6, 16);
6167 QTest::newRow("remove multiple from middle")
6168 << 30 << 0.0 << true
6169 << 5 << 3 << ListRange(8, 18);
6172 QTest::newRow("remove 1 from bottom")
6173 << 30 << 0.0 << true
6174 << 15 << 1 << ListRange(16, 16);
6176 // remove 15, 16, 17
6177 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6178 // the view, and 18 will be animated as the displaced item to replace the last item
6179 QTest::newRow("remove multiple from bottom")
6180 << 30 << 0.0 << true
6181 << 15 << 3 << ListRange(18, 18);
6183 QTest::newRow("remove 1 from bottom, content y not 0")
6184 << 30 << 20.0 * 2 << true
6185 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6186 QTest::newRow("remove multiple from bottom, content y not 0")
6187 << 30 << 20.0 * 2 << true
6188 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6191 QTest::newRow("remove 1 after end")
6192 << 30 << 0.0 << false
6193 << 17 << 1 << ListRange();
6194 QTest::newRow("remove multiple after end")
6195 << 30 << 0.0 << false
6196 << 17 << 3 << ListRange();
6199 void tst_QQuickListView::displacedTransitions()
6201 QFETCH(bool, useDisplaced);
6202 QFETCH(bool, displacedEnabled);
6203 QFETCH(bool, useAddDisplaced);
6204 QFETCH(bool, addDisplacedEnabled);
6205 QFETCH(bool, useMoveDisplaced);
6206 QFETCH(bool, moveDisplacedEnabled);
6207 QFETCH(bool, useRemoveDisplaced);
6208 QFETCH(bool, removeDisplacedEnabled);
6209 QFETCH(ListChange, change);
6210 QFETCH(ListRange, expectedDisplacedIndexes);
6213 for (int i = 0; i < 30; i++)
6214 model.addItem("Original item" + QString::number(i), "");
6215 QaimModel model_displaced_transitionVia;
6216 QaimModel model_addDisplaced_transitionVia;
6217 QaimModel model_moveDisplaced_transitionVia;
6218 QaimModel model_removeDisplaced_transitionVia;
6220 QPointF displaced_transitionVia(-50, -100);
6221 QPointF addDisplaced_transitionVia(-150, 100);
6222 QPointF moveDisplaced_transitionVia(50, -100);
6223 QPointF removeDisplaced_transitionVia(150, 100);
6225 QQuickView *canvas = getView();
6226 QQmlContext *ctxt = canvas->rootContext();
6227 TestObject *testObject = new TestObject(canvas);
6228 ctxt->setContextProperty("testModel", &model);
6229 ctxt->setContextProperty("testObject", testObject);
6230 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6231 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6232 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6233 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6234 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6235 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6236 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6237 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6238 ctxt->setContextProperty("useDisplaced", useDisplaced);
6239 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6240 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6241 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6242 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6243 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6244 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6245 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6246 canvas->setSource(testFileUrl("displacedTransitions.qml"));
6248 qApp->processEvents();
6250 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6251 QTRY_VERIFY(listview != 0);
6252 QQuickItem *contentItem = listview->contentItem();
6253 QVERIFY(contentItem != 0);
6254 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6256 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6257 listview->setProperty("displaceTransitionsDone", false);
6259 switch (change.type) {
6260 case ListChange::Inserted:
6262 QList<QPair<QString, QString> > targetItemData;
6263 for (int i=change.index; i<change.index + change.count; ++i)
6264 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6265 model.insertItems(change.index, targetItemData);
6266 QTRY_COMPARE(model.count(), listview->count());
6269 case ListChange::Removed:
6270 model.removeItems(change.index, change.count);
6271 QTRY_COMPARE(model.count(), listview->count());
6273 case ListChange::Moved:
6274 model.moveItems(change.index, change.to, change.count);
6275 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6277 case ListChange::SetCurrent:
6278 case ListChange::SetContentY:
6279 case ListChange::Polish:
6283 QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6284 QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6286 if ((useDisplaced && displacedEnabled)
6287 || (useAddDisplaced && addDisplacedEnabled)
6288 || (useMoveDisplaced && moveDisplacedEnabled)
6289 || (useRemoveDisplaced && removeDisplacedEnabled)) {
6290 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6292 // check the correct number of target items and indexes were received
6293 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6294 for (int i=0; i<resultTargetIndexes.count(); i++)
6295 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6296 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6297 for (int i=0; i<resultTargetItems.count(); i++)
6298 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6300 QCOMPARE(resultTargetIndexes.count(), 0);
6301 QCOMPARE(resultTargetItems.count(), 0);
6304 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6305 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6307 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6308 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6309 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6311 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6312 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6313 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6315 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6317 if (useDisplaced && displacedEnabled
6318 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6319 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6320 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6321 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6323 QCOMPARE(model_displaced_transitionVia.count(), 0);
6326 // verify all items moved to the correct final positions
6327 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6328 for (int i=0; i < model.count() && i < items.count(); ++i) {
6329 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6330 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6331 QCOMPARE(item->x(), 0.0);
6332 QCOMPARE(item->y(), i * 20.0);
6333 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6335 QTRY_COMPARE(name->text(), model.name(i));
6338 releaseView(canvas);
6341 void tst_QQuickListView::displacedTransitions_data()
6343 QTest::addColumn<bool>("useDisplaced");
6344 QTest::addColumn<bool>("displacedEnabled");
6345 QTest::addColumn<bool>("useAddDisplaced");
6346 QTest::addColumn<bool>("addDisplacedEnabled");
6347 QTest::addColumn<bool>("useMoveDisplaced");
6348 QTest::addColumn<bool>("moveDisplacedEnabled");
6349 QTest::addColumn<bool>("useRemoveDisplaced");
6350 QTest::addColumn<bool>("removeDisplacedEnabled");
6351 QTest::addColumn<ListChange>("change");
6352 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6354 QTest::newRow("no displaced transitions at all")
6359 << ListChange::insert(0, 1) << ListRange(0, 15);
6361 QTest::newRow("just displaced")
6366 << ListChange::insert(0, 1) << ListRange(0, 15);
6368 QTest::newRow("just displaced (not enabled)")
6373 << ListChange::insert(0, 1) << ListRange(0, 15);
6375 QTest::newRow("displaced + addDisplaced")
6380 << ListChange::insert(0, 1) << ListRange(0, 15);
6382 QTest::newRow("displaced + addDisplaced (not enabled)")
6387 << ListChange::insert(0, 1) << ListRange(0, 15);
6389 QTest::newRow("displaced + moveDisplaced")
6394 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6396 QTest::newRow("displaced + moveDisplaced (not enabled)")
6401 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6403 QTest::newRow("displaced + removeDisplaced")
6408 << ListChange::remove(0, 1) << ListRange(1, 16);
6410 QTest::newRow("displaced + removeDisplaced (not enabled)")
6415 << ListChange::remove(0, 1) << ListRange(1, 16);
6418 QTest::newRow("displaced + add, should use generic displaced for a remove")
6423 << ListChange::remove(0, 1) << ListRange(1, 16);
6426 void tst_QQuickListView::multipleTransitions()
6428 // Tests that if you interrupt a transition in progress with another action that
6429 // cancels the previous transition, the resulting items are still placed correctly.
6431 QFETCH(int, initialCount);
6432 QFETCH(qreal, contentY);
6433 QFETCH(QList<ListChange>, changes);
6434 QFETCH(bool, enableAddTransitions);
6435 QFETCH(bool, enableMoveTransitions);
6436 QFETCH(bool, enableRemoveTransitions);
6437 QFETCH(bool, rippleAddDisplaced);
6439 QPointF addTargets_transitionFrom(-50, -50);
6440 QPointF addDisplaced_transitionFrom(-50, 50);
6441 QPointF moveTargets_transitionFrom(50, -50);
6442 QPointF moveDisplaced_transitionFrom(50, 50);
6443 QPointF removeTargets_transitionTo(-100, 300);
6444 QPointF removeDisplaced_transitionFrom(100, 300);
6447 for (int i = 0; i < initialCount; i++)
6448 model.addItem("Original item" + QString::number(i), "");
6450 QQuickView *canvas = getView();
6451 QQmlContext *ctxt = canvas->rootContext();
6452 TestObject *testObject = new TestObject;
6453 ctxt->setContextProperty("testModel", &model);
6454 ctxt->setContextProperty("testObject", testObject);
6455 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6456 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6457 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6458 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6459 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6460 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6461 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6462 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6463 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6464 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6465 canvas->setSource(testFileUrl("multipleTransitions.qml"));
6467 QTest::qWaitForWindowShown(canvas);
6469 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6470 QTRY_VERIFY(listview != 0);
6471 QQuickItem *contentItem = listview->contentItem();
6472 QVERIFY(contentItem != 0);
6473 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6475 if (contentY != 0) {
6476 listview->setContentY(contentY);
6477 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6480 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
6482 for (int i=0; i<changes.count(); i++) {
6483 switch (changes[i].type) {
6484 case ListChange::Inserted:
6486 QList<QPair<QString, QString> > targetItems;
6487 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6488 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6489 model.insertItems(changes[i].index, targetItems);
6490 QTRY_COMPARE(model.count(), listview->count());
6491 if (i == changes.count() - 1) {
6492 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6493 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6495 QTest::qWait(timeBetweenActions);
6499 case ListChange::Removed:
6500 model.removeItems(changes[i].index, changes[i].count);
6501 QTRY_COMPARE(model.count(), listview->count());
6502 if (i == changes.count() - 1) {
6503 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6504 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6506 QTest::qWait(timeBetweenActions);
6509 case ListChange::Moved:
6510 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6511 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6512 if (i == changes.count() - 1) {
6513 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6514 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6516 QTest::qWait(timeBetweenActions);
6519 case ListChange::SetCurrent:
6520 listview->setCurrentIndex(changes[i].index);
6522 case ListChange::SetContentY:
6523 listview->setContentY(changes[i].pos);
6524 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6526 case ListChange::Polish:
6530 QCOMPARE(listview->count(), model.count());
6532 // verify all items moved to the correct final positions
6533 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6534 for (int i=0; i < model.count() && i < items.count(); ++i) {
6535 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6536 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6537 QTRY_COMPARE(item->x(), 0.0);
6538 QTRY_COMPARE(item->y(), i*20.0);
6539 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6541 QTRY_COMPARE(name->text(), model.name(i));
6544 releaseView(canvas);
6548 void tst_QQuickListView::multipleTransitions_data()
6550 QTest::addColumn<int>("initialCount");
6551 QTest::addColumn<qreal>("contentY");
6552 QTest::addColumn<QList<ListChange> >("changes");
6553 QTest::addColumn<bool>("enableAddTransitions");
6554 QTest::addColumn<bool>("enableMoveTransitions");
6555 QTest::addColumn<bool>("enableRemoveTransitions");
6556 QTest::addColumn<bool>("rippleAddDisplaced");
6558 // the added item and displaced items should move to final dest correctly
6559 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6560 << ListChange::insert(0, 1)
6561 << ListChange::move(0, 3, 1)
6563 << true << true << true << false;
6565 // items affected by the add should change from move to add transition
6566 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6567 << ListChange::move(1, 10, 3)
6568 << ListChange::insert(0, 1)
6570 << true << true << true << false;
6572 // items should be placed correctly if you trigger a transition then refill for that index
6573 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6574 << ListChange::insert(0, 1)
6575 << ListChange::setContentY(80.0)
6576 << ListChange::setContentY(0.0)
6577 << ListChange::insert(0, 1)
6579 << true << true << true << false;
6581 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6582 << ListChange::insert(1, 1)
6583 << ListChange::remove(1, 1)
6585 << true << true << true << true;
6587 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6588 // even if a remove-displace transition is not present to re-animate them
6589 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6590 << ListChange::insert(0, 1)
6591 << ListChange::remove(2, 1)
6593 << true << true << false << false;
6595 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6596 // remove has changed the position of where it will move to
6597 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6598 << ListChange::setContentY(-10.0)
6599 << ListChange::insert(0, 1)
6600 << ListChange::insert(0, 1)
6601 << ListChange::remove(2, 1)
6603 << true << true << false << false;
6606 void tst_QQuickListView::multipleDisplaced()
6608 // multiple move() operations should only restart displace transitions for items that
6609 // moved from previously set positions, and not those that have moved from their current
6610 // item positions (which may e.g. still be changing from easing bounces in the last transition)
6613 for (int i = 0; i < 30; i++)
6614 model.addItem("Original item" + QString::number(i), "");
6616 QQuickView *canvas = getView();
6617 QQmlContext *ctxt = canvas->rootContext();
6618 ctxt->setContextProperty("testModel", &model);
6619 ctxt->setContextProperty("testObject", new TestObject(canvas));
6620 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6622 QTest::qWaitForWindowShown(canvas);
6624 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6625 QTRY_VERIFY(listview != 0);
6626 QQuickItem *contentItem = listview->contentItem();
6627 QVERIFY(contentItem != 0);
6628 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6630 model.moveItems(12, 8, 1);
6631 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6632 model.moveItems(8, 3, 1);
6633 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6635 QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6636 foreach (const QString &name, transitionsStarted.keys()) {
6637 QVERIFY2(transitionsStarted[name] == 1,
6638 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6641 // verify all items moved to the correct final positions
6642 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6643 for (int i=0; i < model.count() && i < items.count(); ++i) {
6644 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6645 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6646 QTRY_COMPARE(item->x(), 0.0);
6647 QTRY_COMPARE(item->y(), i*20.0);
6648 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6650 QTRY_COMPARE(name->text(), model.name(i));
6653 releaseView(canvas);
6656 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6660 for (int i=0; i<list.count(); i++) {
6661 ret << list[i].toInt(&ok);
6663 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6669 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6671 for (int i=0; i<indexLists.count(); i++) {
6672 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6673 if (current != expectedIndexes.toSet())
6674 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6675 QCOMPARE(current, expectedIndexes.toSet());
6679 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6681 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6682 QVERIFY(it.value().type() == QVariant::Int);
6683 QString name = it.key();
6684 int itemIndex = it.value().toInt();
6685 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6686 if (model.name(itemIndex) != name)
6687 qDebug() << itemIndex;
6688 QCOMPARE(model.name(itemIndex), name);
6690 QCOMPARE(items.count(), expectedIndexes.count());
6693 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6695 for (int i=0; i<itemLists.count(); i++) {
6696 QVERIFY(itemLists[i].type() == QVariant::List);
6697 QVariantList current = itemLists[i].toList();
6698 for (int j=0; j<current.count(); j++) {
6699 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6700 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6701 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6703 QCOMPARE(current.count(), expectedItems.count());
6707 void tst_QQuickListView::flickBeyondBounds()
6709 QQuickView *canvas = createView();
6711 canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6713 qApp->processEvents();
6715 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6716 QTRY_VERIFY(listview != 0);
6718 QQuickItem *contentItem = listview->contentItem();
6719 QTRY_VERIFY(contentItem != 0);
6720 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6722 // Flick view up beyond bounds
6723 flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6724 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6726 // We're really testing that we don't get stuck in a loop,
6727 // but also confirm items positioned correctly.
6728 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6729 for (int i = 0; i < 2; ++i) {
6730 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6731 if (!item) qWarning() << "Item" << i << "not found";
6733 QTRY_VERIFY(item->y() == i*45);
6739 void tst_QQuickListView::destroyItemOnCreation()
6742 QQuickView *canvas = createView();
6743 canvas->rootContext()->setContextProperty("testModel", &model);
6745 canvas->setSource(testFileUrl("destroyItemOnCreation.qml"));
6747 qApp->processEvents();
6749 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6750 QVERIFY(listview != 0);
6752 QQuickItem *contentItem = listview->contentItem();
6753 QVERIFY(contentItem != 0);
6754 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6756 QCOMPARE(canvas->rootObject()->property("createdIndex").toInt(), -1);
6757 model.addItem("new item", "");
6758 QTRY_COMPARE(canvas->rootObject()->property("createdIndex").toInt(), 0);
6760 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6761 QCOMPARE(model.count(), 0);
6766 void tst_QQuickListView::parentBinding()
6768 QQuickView *canvas = createView();
6771 QtMsgHandler old = qInstallMsgHandler(errorMsgHandler);
6773 canvas->setSource(testFileUrl("parentBinding.qml"));
6775 qApp->processEvents();
6777 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
6778 QVERIFY(listview != 0);
6780 QQuickItem *contentItem = listview->contentItem();
6781 QVERIFY(contentItem != 0);
6782 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6784 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6786 QCOMPARE(item->width(), listview->width());
6787 QCOMPARE(item->height(), listview->height()/12);
6789 // there should be no transient binding error
6790 QVERIFY(!m_errorCount);
6792 qInstallMsgHandler(old);
6797 QTEST_MAIN(tst_QQuickListView)
6799 #include "tst_qquicklistview.moc"