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/qquickitem_p.h>
50 #include <QtQuick/private/qquicklistview_p.h>
51 #include <QtQuick/private/qquicktext_p.h>
52 #include <QtQuick/private/qquickvisualitemmodel_p.h>
53 #include <QtQml/private/qquicklistmodel_p.h>
54 #include "../../shared/util.h"
55 #include "../shared/viewtestutil.h"
56 #include "../shared/visualtestutil.h"
57 #include "incrementalmodel.h"
60 Q_DECLARE_METATYPE(Qt::LayoutDirection)
61 Q_DECLARE_METATYPE(QQuickListView::Orientation)
63 using namespace QQuickViewTestUtil;
64 using namespace QQuickVisualTestUtil;
68 class tst_QQuickListView : public QQmlDataTest
76 // Test both QListModelInterface and QAbstractItemModel model types
77 void qListModelInterface_items();
78 void qListModelInterface_package_items();
79 void qAbstractItemModel_items();
81 void qListModelInterface_changed();
82 void qListModelInterface_package_changed();
83 void qAbstractItemModel_changed();
85 void qListModelInterface_inserted();
86 void qListModelInterface_inserted_more();
87 void qListModelInterface_inserted_more_data();
88 void qListModelInterface_package_inserted();
89 void qAbstractItemModel_inserted();
90 void qAbstractItemModel_inserted_more();
91 void qAbstractItemModel_inserted_more_data();
93 void qListModelInterface_removed();
94 void qListModelInterface_removed_more();
95 void qListModelInterface_removed_more_data();
96 void qListModelInterface_package_removed();
97 void qAbstractItemModel_removed();
98 void qAbstractItemModel_removed_more();
99 void qAbstractItemModel_removed_more_data();
101 void qListModelInterface_moved();
102 void qListModelInterface_moved_data();
103 void qListModelInterface_package_moved();
104 void qListModelInterface_package_moved_data();
105 void qAbstractItemModel_moved();
106 void qAbstractItemModel_moved_data();
108 void multipleChanges();
109 void multipleChanges_data();
111 void qListModelInterface_clear();
112 void qListModelInterface_package_clear();
113 void qAbstractItemModel_clear();
115 void insertBeforeVisible();
116 void insertBeforeVisible_data();
117 void swapWithFirstItem();
119 void currentIndex_delayedItemCreation();
120 void currentIndex_delayedItemCreation_data();
122 void noCurrentIndex();
124 void enforceRange_withoutHighlight();
126 void qListModelInterface_sections();
127 void qListModelInterface_package_sections();
128 void qAbstractItemModel_sections();
129 void sectionsPositioning();
130 void sectionsDelegate();
131 void sectionPropertyChange();
133 void positionViewAtIndex();
135 void propertyChanges();
136 void componentChanges();
138 void manualHighlight();
141 void header_delayItemCreation();
146 void resizeViewAndRepaint();
147 void sizeLessThan1();
149 void resizeDelegate();
150 void resizeFirstDelegate();
152 void indexAt_itemAt_data();
153 void indexAt_itemAt();
154 void incrementalModel();
158 void onRemove_data();
160 void test_mirroring();
162 void marginsResize();
163 void marginsResize_data();
164 void creationContext();
165 void snapToItem_data();
167 void snapOneItem_data();
175 void unrequestedVisibility();
177 void populateTransitions();
178 void populateTransitions_data();
179 void addTransitions();
180 void addTransitions_data();
181 void moveTransitions();
182 void moveTransitions_data();
183 void removeTransitions();
184 void removeTransitions_data();
185 void displacedTransitions();
186 void displacedTransitions_data();
187 void multipleTransitions();
188 void multipleTransitions_data();
189 void multipleDisplaced();
191 void flickBeyondBounds();
194 template <class T> void items(const QUrl &source, bool forceLayout);
195 template <class T> void changed(const QUrl &source, bool forceLayout);
196 template <class T> void inserted(const QUrl &source);
197 template <class T> void inserted_more();
198 template <class T> void removed(const QUrl &source, bool animated);
199 template <class T> void removed_more(const QUrl &source);
200 template <class T> void moved(const QUrl &source);
201 template <class T> void clear(const QUrl &source);
202 template <class T> void sections(const QUrl &source);
204 QList<int> toIntList(const QVariantList &list);
205 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
206 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
207 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
209 void inserted_more_data();
210 void removed_more_data();
214 QQuickView *getView() {
216 if (QString(QTest::currentTestFunction()) != testForView) {
220 m_view->setSource(QUrl());
225 testForView = QTest::currentTestFunction();
226 m_view = createView();
229 void releaseView(QQuickView *view) {
230 Q_ASSERT(view == m_view);
231 m_view->setSource(QUrl());
234 QQuickView *getView() {
237 void releaseView(QQuickView *view) {
246 class TestObject : public QObject
250 Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError)
251 Q_PROPERTY(bool animate READ animate NOTIFY changedAnim)
252 Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl)
253 Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer)
256 TestObject(QObject *parent = 0)
257 : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
260 bool error() const { return mError; }
261 void setError(bool err) { mError = err; emit changedError(); }
263 bool animate() const { return mAnimate; }
264 void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
266 bool invalidHighlight() const { return mInvalidHighlight; }
267 void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
269 int cacheBuffer() const { return mCacheBuffer; }
270 void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
276 void changedCacheBuffer();
281 bool mInvalidHighlight;
285 tst_QQuickListView::tst_QQuickListView() : m_view(0)
289 void tst_QQuickListView::init()
292 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
293 testForView = QString();
301 void tst_QQuickListView::items(const QUrl &source, bool forceLayout)
303 QQuickView *canvas = createView();
306 model.addItem("Fred", "12345");
307 model.addItem("John", "2345");
308 model.addItem("Bob", "54321");
310 QQmlContext *ctxt = canvas->rootContext();
311 ctxt->setContextProperty("testModel", &model);
313 TestObject *testObject = new TestObject;
314 ctxt->setContextProperty("testObject", testObject);
316 canvas->setSource(source);
317 qApp->processEvents();
319 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
320 QTRY_VERIFY(listview != 0);
322 QQuickItem *contentItem = listview->contentItem();
323 QTRY_VERIFY(contentItem != 0);
325 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
326 QTRY_VERIFY(testObject->error() == false);
328 QTRY_VERIFY(listview->highlightItem() != 0);
329 QTRY_COMPARE(listview->count(), model.count());
330 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
331 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
333 // current item should be first item
334 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
336 for (int i = 0; i < model.count(); ++i) {
337 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
338 QTRY_VERIFY(name != 0);
339 QTRY_COMPARE(name->text(), model.name(i));
340 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
341 QTRY_VERIFY(number != 0);
342 QTRY_COMPARE(number->text(), model.number(i));
345 // switch to other delegate
346 testObject->setAnimate(true);
347 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
348 QTRY_VERIFY(testObject->error() == false);
349 QTRY_VERIFY(listview->currentItem());
351 // set invalid highlight
352 testObject->setInvalidHighlight(true);
353 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
354 QTRY_VERIFY(testObject->error() == false);
355 QTRY_VERIFY(listview->currentItem());
356 QTRY_VERIFY(listview->highlightItem() == 0);
358 // back to normal highlight
359 testObject->setInvalidHighlight(false);
360 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
361 QTRY_VERIFY(testObject->error() == false);
362 QTRY_VERIFY(listview->currentItem());
363 QTRY_VERIFY(listview->highlightItem() != 0);
365 // set an empty model and confirm that items are destroyed
367 ctxt->setContextProperty("testModel", &model2);
369 // Force a layout, necessary if ListView is completed before VisualDataModel.
371 QCOMPARE(listview->property("count").toInt(), 0);
373 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
374 QTRY_VERIFY(itemCount == 0);
376 QTRY_COMPARE(listview->highlightResizeSpeed(), 1000.0);
377 QTRY_COMPARE(listview->highlightMoveSpeed(), 1000.0);
385 void tst_QQuickListView::changed(const QUrl &source, bool forceLayout)
387 QQuickView *canvas = createView();
390 model.addItem("Fred", "12345");
391 model.addItem("John", "2345");
392 model.addItem("Bob", "54321");
394 QQmlContext *ctxt = canvas->rootContext();
395 ctxt->setContextProperty("testModel", &model);
397 TestObject *testObject = new TestObject;
398 ctxt->setContextProperty("testObject", testObject);
400 canvas->setSource(source);
401 qApp->processEvents();
403 QQuickFlickable *listview = findItem<QQuickFlickable>(canvas->rootObject(), "list");
404 QTRY_VERIFY(listview != 0);
406 QQuickItem *contentItem = listview->contentItem();
407 QTRY_VERIFY(contentItem != 0);
409 // Force a layout, necessary if ListView is completed before VisualDataModel.
411 QCOMPARE(listview->property("count").toInt(), model.count());
413 model.modifyItem(1, "Will", "9876");
414 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
415 QTRY_VERIFY(name != 0);
416 QTRY_COMPARE(name->text(), model.name(1));
417 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
418 QTRY_VERIFY(number != 0);
419 QTRY_COMPARE(number->text(), model.number(1));
426 void tst_QQuickListView::inserted(const QUrl &source)
428 QQuickView *canvas = createView();
432 model.addItem("Fred", "12345");
433 model.addItem("John", "2345");
434 model.addItem("Bob", "54321");
436 QQmlContext *ctxt = canvas->rootContext();
437 ctxt->setContextProperty("testModel", &model);
439 TestObject *testObject = new TestObject;
440 ctxt->setContextProperty("testObject", testObject);
442 canvas->setSource(source);
443 qApp->processEvents();
445 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
446 QTRY_VERIFY(listview != 0);
448 QQuickItem *contentItem = listview->contentItem();
449 QTRY_VERIFY(contentItem != 0);
451 model.insertItem(1, "Will", "9876");
453 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
454 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
456 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
457 QTRY_VERIFY(name != 0);
458 QTRY_COMPARE(name->text(), model.name(1));
459 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
460 QTRY_VERIFY(number != 0);
461 QTRY_COMPARE(number->text(), model.number(1));
463 // Confirm items positioned correctly
464 for (int i = 0; i < model.count(); ++i) {
465 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
466 QTRY_COMPARE(item->y(), i*20.0);
469 model.insertItem(0, "Foo", "1111"); // zero index, and current item
471 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
472 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
474 name = findItem<QQuickText>(contentItem, "textName", 0);
475 QTRY_VERIFY(name != 0);
476 QTRY_COMPARE(name->text(), model.name(0));
477 number = findItem<QQuickText>(contentItem, "textNumber", 0);
478 QTRY_VERIFY(number != 0);
479 QTRY_COMPARE(number->text(), model.number(0));
481 QTRY_COMPARE(listview->currentIndex(), 1);
483 // Confirm items positioned correctly
484 for (int i = 0; i < model.count(); ++i) {
485 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
486 QTRY_COMPARE(item->y(), i*20.0);
489 for (int i = model.count(); i < 30; ++i)
490 model.insertItem(i, "Hello", QString::number(i));
492 listview->setContentY(80);
494 // Insert item outside visible area
495 model.insertItem(1, "Hello", "1324");
497 QTRY_VERIFY(listview->contentY() == 80);
499 // Confirm items positioned correctly
500 for (int i = 5; i < 5+15; ++i) {
501 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
502 if (!item) qWarning() << "Item" << i << "not found";
504 QTRY_COMPARE(item->y(), i*20.0 - 20.0);
507 // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
511 model.insertItem(0, "Hello", "1234");
512 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
514 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
516 QCOMPARE(item->y(), 0.);
517 QTRY_VERIFY(listview->contentY() == 0);
524 void tst_QQuickListView::inserted_more()
526 QFETCH(qreal, contentY);
527 QFETCH(int, insertIndex);
528 QFETCH(int, insertCount);
529 QFETCH(qreal, itemsOffsetAfterMove);
532 for (int i = 0; i < 30; i++)
533 model.addItem("Item" + QString::number(i), "");
535 QQuickView *canvas = getView();
536 QQmlContext *ctxt = canvas->rootContext();
537 ctxt->setContextProperty("testModel", &model);
539 TestObject *testObject = new TestObject;
540 ctxt->setContextProperty("testObject", testObject);
542 canvas->setSource(testFileUrl("listviewtest.qml"));
544 qApp->processEvents();
545 QTest::qWaitForWindowShown(canvas);
547 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
548 QTRY_VERIFY(listview != 0);
549 QQuickItem *contentItem = listview->contentItem();
550 QTRY_VERIFY(contentItem != 0);
552 listview->setContentY(contentY);
553 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
555 QList<QPair<QString, QString> > newData;
556 for (int i=0; i<insertCount; i++)
557 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
558 model.insertItems(insertIndex, newData);
559 QTRY_COMPARE(listview->property("count").toInt(), model.count());
561 // check visibleItems.first() is in correct position
562 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
564 QCOMPARE(item0->y(), itemsOffsetAfterMove);
566 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
567 int firstVisibleIndex = -1;
568 for (int i=0; i<items.count(); i++) {
569 if (items[i]->y() >= contentY) {
570 QQmlExpression e(qmlContext(items[i]), items[i], "index");
571 firstVisibleIndex = e.evaluate().toInt();
575 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
577 // Confirm items positioned correctly and indexes correct
578 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
581 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
582 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
583 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
584 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
585 name = findItem<QQuickText>(contentItem, "textName", i);
587 QTRY_COMPARE(name->text(), model.name(i));
588 number = findItem<QQuickText>(contentItem, "textNumber", i);
589 QVERIFY(number != 0);
590 QTRY_COMPARE(number->text(), model.number(i));
597 void tst_QQuickListView::inserted_more_data()
599 QTest::addColumn<qreal>("contentY");
600 QTest::addColumn<int>("insertIndex");
601 QTest::addColumn<int>("insertCount");
602 QTest::addColumn<qreal>("itemsOffsetAfterMove");
604 QTest::newRow("add 1, before visible items")
607 << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move
609 QTest::newRow("add multiple, before visible")
612 << -20.0 * 3; // again first visible should not move
614 QTest::newRow("add 1, at start of visible, content at start")
619 QTest::newRow("add multiple, start of visible, content at start")
624 QTest::newRow("add 1, at start of visible, content not at start")
629 QTest::newRow("add multiple, at start of visible, content not at start")
635 QTest::newRow("add 1, at end of visible, content at start")
640 QTest::newRow("add 1, at end of visible, content at start")
645 QTest::newRow("add 1, at end of visible, content not at start")
650 QTest::newRow("add multiple, at end of visible, content not at start")
656 QTest::newRow("add 1, after visible, content at start")
661 QTest::newRow("add 1, after visible, content at start")
666 QTest::newRow("add 1, after visible, content not at start")
671 QTest::newRow("add multiple, after visible, content not at start")
677 void tst_QQuickListView::insertBeforeVisible()
679 QFETCH(int, insertIndex);
680 QFETCH(int, insertCount);
681 QFETCH(int, cacheBuffer);
684 QQuickView *canvas = getView();
687 for (int i = 0; i < 30; i++)
688 model.addItem("Item" + QString::number(i), "");
690 QQmlContext *ctxt = canvas->rootContext();
691 ctxt->setContextProperty("testModel", &model);
693 TestObject *testObject = new TestObject;
694 ctxt->setContextProperty("testObject", testObject);
696 canvas->setSource(testFileUrl("listviewtest.qml"));
698 qApp->processEvents();
699 QTest::qWaitForWindowShown(canvas);
701 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
702 QTRY_VERIFY(listview != 0);
703 QQuickItem *contentItem = listview->contentItem();
704 QTRY_VERIFY(contentItem != 0);
706 listview->setCacheBuffer(cacheBuffer);
707 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
709 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
710 int firstVisibleIndex = 20; // move to an index where the top item is not visible
711 listview->setContentY(firstVisibleIndex * 20.0);
712 listview->setCurrentIndex(firstVisibleIndex);
714 qApp->processEvents();
715 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
716 QTRY_COMPARE(listview->currentIndex(), firstVisibleIndex);
717 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
719 QCOMPARE(item->y(), listview->contentY());
721 QList<QPair<QString, QString> > newData;
722 for (int i=0; i<insertCount; i++)
723 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
724 model.insertItems(insertIndex, newData);
725 QTRY_COMPARE(listview->property("count").toInt(), model.count());
727 // now, moving to the top of the view should position the inserted items correctly
728 int itemsOffsetAfterMove = -(insertCount * 20);
729 listview->setCurrentIndex(0);
730 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
731 QTRY_COMPARE(listview->currentIndex(), 0);
732 QTRY_COMPARE(listview->contentY(), 0.0 + itemsOffsetAfterMove);
734 // Confirm items positioned correctly and indexes correct
735 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
736 for (int i = 0; i < model.count() && i < itemCount; ++i) {
737 item = findItem<QQuickItem>(contentItem, "wrapper", i);
738 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
739 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
740 name = findItem<QQuickText>(contentItem, "textName", i);
742 QTRY_COMPARE(name->text(), model.name(i));
749 void tst_QQuickListView::insertBeforeVisible_data()
751 QTest::addColumn<int>("insertIndex");
752 QTest::addColumn<int>("insertCount");
753 QTest::addColumn<int>("cacheBuffer");
755 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
756 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
757 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
759 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
760 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
761 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
763 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 3 << 0;
764 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 3 << 100;
765 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 3 << 500;
767 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 3 << 0;
768 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 3 << 100;
769 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 3 << 500;
773 void tst_QQuickListView::removed(const QUrl &source, bool /* animated */)
775 QQuickView *canvas = createView();
778 for (int i = 0; i < 50; i++)
779 model.addItem("Item" + QString::number(i), "");
781 QQmlContext *ctxt = canvas->rootContext();
782 ctxt->setContextProperty("testModel", &model);
784 TestObject *testObject = new TestObject;
785 ctxt->setContextProperty("testObject", testObject);
787 canvas->setSource(source);
789 qApp->processEvents();
790 QTest::qWaitForWindowShown(canvas);
792 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
793 QTRY_VERIFY(listview != 0);
794 QQuickItem *contentItem = listview->contentItem();
795 QTRY_VERIFY(contentItem != 0);
796 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
799 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
801 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
802 QTRY_VERIFY(name != 0);
803 QTRY_COMPARE(name->text(), model.name(1));
804 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
805 QTRY_VERIFY(number != 0);
806 QTRY_COMPARE(number->text(), model.number(1));
808 // Confirm items positioned correctly
809 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
810 for (int i = 0; i < model.count() && i < itemCount; ++i) {
811 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
812 if (!item) qWarning() << "Item" << i << "not found";
814 QTRY_VERIFY(item->y() == i*20);
817 // Remove first item (which is the current item);
819 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
821 name = findItem<QQuickText>(contentItem, "textName", 0);
822 QTRY_VERIFY(name != 0);
823 QTRY_COMPARE(name->text(), model.name(0));
824 number = findItem<QQuickText>(contentItem, "textNumber", 0);
825 QTRY_VERIFY(number != 0);
826 QTRY_COMPARE(number->text(), model.number(0));
828 // Confirm items positioned correctly
829 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
830 for (int i = 0; i < model.count() && i < itemCount; ++i) {
831 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
832 if (!item) qWarning() << "Item" << i << "not found";
834 QTRY_COMPARE(item->y(),i*20.0);
837 // Remove items not visible
838 model.removeItem(18);
839 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
841 // Confirm items positioned correctly
842 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
843 for (int i = 0; i < model.count() && i < itemCount; ++i) {
844 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
845 if (!item) qWarning() << "Item" << i << "not found";
847 QTRY_COMPARE(item->y(),i*20.0);
850 // Remove items before visible
851 listview->setContentY(80);
852 listview->setCurrentIndex(10);
854 model.removeItem(1); // post: top item will be at 20
855 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
857 // Confirm items positioned correctly
858 for (int i = 2; i < 18; ++i) {
859 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
860 if (!item) qWarning() << "Item" << i << "not found";
862 QTRY_COMPARE(item->y(),20+i*20.0);
865 // Remove current index
866 QTRY_VERIFY(listview->currentIndex() == 9);
867 QQuickItem *oldCurrent = listview->currentItem();
870 QTRY_COMPARE(listview->currentIndex(), 9);
871 QTRY_VERIFY(listview->currentItem() != oldCurrent);
873 listview->setContentY(20); // That's the top now
874 // let transitions settle.
875 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
877 // Confirm items positioned correctly
878 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
879 for (int i = 0; i < model.count() && i < itemCount; ++i) {
880 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
881 if (!item) qWarning() << "Item" << i << "not found";
883 QTRY_COMPARE(item->y(),20+i*20.0);
886 // remove current item beyond visible items.
887 listview->setCurrentIndex(20);
888 listview->setContentY(40);
889 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
891 model.removeItem(20);
892 QTRY_COMPARE(listview->currentIndex(), 20);
893 QTRY_VERIFY(listview->currentItem() != 0);
895 // remove item before current, but visible
896 listview->setCurrentIndex(8);
897 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
898 oldCurrent = listview->currentItem();
901 QTRY_COMPARE(listview->currentIndex(), 7);
902 QTRY_VERIFY(listview->currentItem() == oldCurrent);
904 listview->setContentY(80);
905 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
907 // remove all visible items
908 model.removeItems(1, 18);
909 QTRY_COMPARE(listview->count() , model.count());
911 // Confirm items positioned correctly
912 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
913 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
914 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i+1);
915 if (!item) qWarning() << "Item" << i+1 << "not found";
917 QTRY_COMPARE(item->y(),80+i*20.0);
920 model.removeItems(1, 17);
921 QTRY_COMPARE(listview->count() , model.count());
923 model.removeItems(2, 1);
924 QTRY_COMPARE(listview->count() , model.count());
926 model.addItem("New", "1");
927 QTRY_COMPARE(listview->count() , model.count());
929 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", model.count()-1));
930 QCOMPARE(name->text(), QString("New"));
932 // Add some more items so that we don't run out
934 for (int i = 0; i < 50; i++)
935 model.addItem("Item" + QString::number(i), "");
938 listview->setCurrentIndex(0);
939 listview->setContentY(30);
941 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", 0));
943 // QTBUG-19198 move to end and remove all visible items one at a time.
944 listview->positionViewAtEnd();
945 for (int i = 0; i < 18; ++i)
946 model.removeItems(model.count() - 1, 1);
947 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() > 16);
954 void tst_QQuickListView::removed_more(const QUrl &source)
956 QFETCH(qreal, contentY);
957 QFETCH(int, removeIndex);
958 QFETCH(int, removeCount);
959 QFETCH(qreal, itemsOffsetAfterMove);
963 QQuickView *canvas = getView();
966 for (int i = 0; i < 30; i++)
967 model.addItem("Item" + QString::number(i), "");
969 QQmlContext *ctxt = canvas->rootContext();
970 ctxt->setContextProperty("testModel", &model);
972 TestObject *testObject = new TestObject;
973 ctxt->setContextProperty("testObject", testObject);
975 canvas->setSource(source);
977 qApp->processEvents();
978 QTest::qWaitForWindowShown(canvas);
980 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
981 QTRY_VERIFY(listview != 0);
982 QQuickItem *contentItem = listview->contentItem();
983 QTRY_VERIFY(contentItem != 0);
985 listview->setContentY(contentY);
986 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
988 // wait for refill (after refill, items above the firstVisibleIndex-1 should not be rendered)
989 int firstVisibleIndex = contentY / 20;
990 if (firstVisibleIndex - 2 >= 0)
991 QTRY_VERIFY(!findItem<QQuickText>(contentItem, "textName", firstVisibleIndex - 2));
993 model.removeItems(removeIndex, removeCount);
994 QTRY_COMPARE(listview->property("count").toInt(), model.count());
996 // check visibleItems.first() is in correct position
997 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
999 QCOMPARE(item0->y(), itemsOffsetAfterMove);
1001 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1002 for (int i=0; i<items.count(); i++) {
1003 if (items[i]->y() >= contentY) {
1004 QQmlExpression e(qmlContext(items[i]), items[i], "index");
1005 firstVisibleIndex = e.evaluate().toInt();
1009 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1011 // Confirm items positioned correctly and indexes correct
1012 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1013 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
1014 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1015 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1016 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
1017 name = findItem<QQuickText>(contentItem, "textName", i);
1019 QTRY_COMPARE(name->text(), model.name(i));
1020 number = findItem<QQuickText>(contentItem, "textNumber", i);
1021 QVERIFY(number != 0);
1022 QTRY_COMPARE(number->text(), model.number(i));
1025 releaseView(canvas);
1029 void tst_QQuickListView::removed_more_data()
1031 QTest::addColumn<qreal>("contentY");
1032 QTest::addColumn<int>("removeIndex");
1033 QTest::addColumn<int>("removeCount");
1034 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1036 QTest::newRow("remove 1, before visible items")
1037 << 80.0 // show 4-19
1039 << 20.0; // visible items slide down by 1 item so that first visible does not move
1041 QTest::newRow("remove multiple, all before visible items")
1046 QTest::newRow("remove multiple, all before visible items, remove item 0")
1051 // remove 1,2,3 before the visible pos, 0 moves down to just before the visible pos,
1052 // items 4,5 are removed from view, item 6 slides up to original pos of item 4 (80px)
1053 QTest::newRow("remove multiple, mix of items from before and within visible items")
1056 << 20.0 * 3; // adjust for the 3 items removed before the visible
1058 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1061 << 20.0 * 4; // adjust for the 3 items removed before the visible
1064 QTest::newRow("remove 1, from start of visible, content at start")
1069 QTest::newRow("remove multiple, from start of visible, content at start")
1074 QTest::newRow("remove 1, from start of visible, content not at start")
1075 << 80.0 // show 4-19
1079 QTest::newRow("remove multiple, from start of visible, content not at start")
1080 << 80.0 // show 4-19
1085 QTest::newRow("remove 1, from middle of visible, content at start")
1090 QTest::newRow("remove multiple, from middle of visible, content at start")
1095 QTest::newRow("remove 1, from middle of visible, content not at start")
1096 << 80.0 // show 4-19
1100 QTest::newRow("remove multiple, from middle of visible, content not at start")
1101 << 80.0 // show 4-19
1106 QTest::newRow("remove 1, after visible, content at start")
1111 QTest::newRow("remove multiple, after visible, content at start")
1116 QTest::newRow("remove 1, after visible, content not at middle")
1117 << 80.0 // show 4-19
1121 QTest::newRow("remove multiple, after visible, content not at start")
1122 << 80.0 // show 4-19
1126 QTest::newRow("remove multiple, mix of items from within and after visible items")
1133 void tst_QQuickListView::clear(const QUrl &source)
1135 QQuickView *canvas = createView();
1138 for (int i = 0; i < 30; i++)
1139 model.addItem("Item" + QString::number(i), "");
1141 QQmlContext *ctxt = canvas->rootContext();
1142 ctxt->setContextProperty("testModel", &model);
1144 TestObject *testObject = new TestObject;
1145 ctxt->setContextProperty("testObject", testObject);
1147 canvas->setSource(source);
1149 qApp->processEvents();
1151 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1152 QTRY_VERIFY(listview != 0);
1153 QQuickItem *contentItem = listview->contentItem();
1154 QTRY_VERIFY(contentItem != 0);
1155 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1159 QTRY_VERIFY(listview->count() == 0);
1160 QTRY_VERIFY(listview->currentItem() == 0);
1161 QTRY_VERIFY(listview->contentY() == 0);
1162 QVERIFY(listview->currentIndex() == -1);
1164 // confirm sanity when adding an item to cleared list
1165 model.addItem("New", "1");
1166 QTRY_VERIFY(listview->count() == 1);
1167 QVERIFY(listview->currentItem() != 0);
1168 QVERIFY(listview->currentIndex() == 0);
1175 void tst_QQuickListView::moved(const QUrl &source)
1177 QFETCH(qreal, contentY);
1181 QFETCH(qreal, itemsOffsetAfterMove);
1185 QQuickView *canvas = getView();
1188 for (int i = 0; i < 30; i++)
1189 model.addItem("Item" + QString::number(i), "");
1191 QQmlContext *ctxt = canvas->rootContext();
1192 ctxt->setContextProperty("testModel", &model);
1194 TestObject *testObject = new TestObject;
1195 ctxt->setContextProperty("testObject", testObject);
1197 canvas->setSource(source);
1199 qApp->processEvents();
1200 QTest::qWaitForWindowShown(canvas);
1202 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1203 QTRY_VERIFY(listview != 0);
1204 QQuickItem *contentItem = listview->contentItem();
1205 QTRY_VERIFY(contentItem != 0);
1206 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1208 QQuickItem *currentItem = listview->currentItem();
1209 QTRY_VERIFY(currentItem != 0);
1211 if (contentY != 0) {
1212 listview->setContentY(contentY);
1213 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1216 model.moveItems(from, to, count);
1217 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1219 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1220 int firstVisibleIndex = -1;
1221 for (int i=0; i<items.count(); i++) {
1222 if (items[i]->y() >= contentY) {
1223 QQmlExpression e(qmlContext(items[i]), items[i], "index");
1224 firstVisibleIndex = e.evaluate().toInt();
1228 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1230 // Confirm items positioned correctly and indexes correct
1231 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1232 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
1233 if (i >= firstVisibleIndex + 16) // index has moved out of view
1235 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1236 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1237 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
1238 name = findItem<QQuickText>(contentItem, "textName", i);
1240 QTRY_COMPARE(name->text(), model.name(i));
1241 number = findItem<QQuickText>(contentItem, "textNumber", i);
1242 QVERIFY(number != 0);
1243 QTRY_COMPARE(number->text(), model.number(i));
1245 // current index should have been updated
1246 if (item == currentItem)
1247 QTRY_COMPARE(listview->currentIndex(), i);
1250 releaseView(canvas);
1254 void tst_QQuickListView::moved_data()
1256 QTest::addColumn<qreal>("contentY");
1257 QTest::addColumn<int>("from");
1258 QTest::addColumn<int>("to");
1259 QTest::addColumn<int>("count");
1260 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1262 // model starts with 30 items, each 20px high, in area 320px high
1263 // 16 items should be visible at a time
1264 // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved
1266 QTest::newRow("move 1 forwards, within visible items")
1271 QTest::newRow("move 1 forwards, from non-visible -> visible")
1272 << 80.0 // show 4-19
1274 << 20.0; // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement
1276 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1277 << 80.0 // show 4-19
1279 << 20.0; // first item has moved to below item4, everything drops down by size of 1 item
1281 QTest::newRow("move 1 forwards, from visible -> non-visible")
1286 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1292 QTest::newRow("move 1 backwards, within visible items")
1297 QTest::newRow("move 1 backwards, within visible items (to first index)")
1302 QTest::newRow("move 1 backwards, from non-visible -> visible")
1307 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1312 QTest::newRow("move 1 backwards, from visible -> non-visible")
1313 << 80.0 // show 4-19
1315 << -20.0; // to minimize movement, item 0 moves to -20, and other items do not move
1317 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1318 << 80.0 // show 4-19
1320 << -20.0; // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move
1323 QTest::newRow("move multiple forwards, within visible items")
1328 QTest::newRow("move multiple forwards, before visible items")
1329 << 140.0 // show 7-22
1330 << 4 << 5 << 3 // 4,5,6 move to below 7
1331 << 20.0 * 3; // 4,5,6 moved down
1333 QTest::newRow("move multiple forwards, from non-visible -> visible")
1334 << 80.0 // show 4-19
1336 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1338 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1339 << 80.0 // show 4-19
1341 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1343 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1346 << 20.0; // item 1,2 are removed, item 3 is now first visible
1348 QTest::newRow("move multiple forwards, to bottom of view")
1353 QTest::newRow("move multiple forwards, to bottom of view, first->last")
1358 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1363 QTest::newRow("move multiple forwards, from visible -> non-visible")
1368 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1374 QTest::newRow("move multiple backwards, within visible items")
1379 QTest::newRow("move multiple backwards, within visible items (move first item)")
1384 QTest::newRow("move multiple backwards, from non-visible -> visible")
1389 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1394 QTest::newRow("move multiple backwards, from visible -> non-visible")
1395 << 80.0 // show 4-19
1397 << -20.0 * 3; // to minimize movement, 0 moves by -60, and other items do not move
1399 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1400 << 80.0 // show 4-19
1402 << -20.0 * 3; // to minimize movement, 16,17,18 move to above item 0, and other items do not move
1405 void tst_QQuickListView::multipleChanges()
1407 QFETCH(int, startCount);
1408 QFETCH(QList<ListChange>, changes);
1409 QFETCH(int, newCount);
1410 QFETCH(int, newCurrentIndex);
1412 QQuickView *canvas = getView();
1415 for (int i = 0; i < startCount; i++)
1416 model.addItem("Item" + QString::number(i), "");
1418 QQmlContext *ctxt = canvas->rootContext();
1419 ctxt->setContextProperty("testModel", &model);
1421 TestObject *testObject = new TestObject;
1422 ctxt->setContextProperty("testObject", testObject);
1424 canvas->setSource(testFileUrl("listviewtest.qml"));
1426 qApp->processEvents();
1427 QTest::qWaitForWindowShown(canvas);
1429 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1430 QTRY_VERIFY(listview != 0);
1431 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1433 for (int i=0; i<changes.count(); i++) {
1434 switch (changes[i].type) {
1435 case ListChange::Inserted:
1437 QList<QPair<QString, QString> > items;
1438 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1439 items << qMakePair(QString("new item %1").arg(j), QString::number(j));
1440 model.insertItems(changes[i].index, items);
1441 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1444 case ListChange::Removed:
1445 model.removeItems(changes[i].index, changes[i].count);
1446 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1448 case ListChange::Moved:
1449 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1450 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1452 case ListChange::SetCurrent:
1453 listview->setCurrentIndex(changes[i].index);
1454 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1456 case ListChange::SetContentY:
1457 listview->setContentY(changes[i].pos);
1458 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1463 QTRY_COMPARE(listview->count(), newCount);
1464 QCOMPARE(listview->count(), model.count());
1465 QTRY_COMPARE(listview->currentIndex(), newCurrentIndex);
1469 QQuickItem *contentItem = listview->contentItem();
1470 QTRY_VERIFY(contentItem != 0);
1471 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1472 for (int i=0; i < model.count() && i < itemCount; ++i) {
1473 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1474 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1475 name = findItem<QQuickText>(contentItem, "textName", i);
1477 QTRY_COMPARE(name->text(), model.name(i));
1478 number = findItem<QQuickText>(contentItem, "textNumber", i);
1479 QVERIFY(number != 0);
1480 QTRY_COMPARE(number->text(), model.number(i));
1484 releaseView(canvas);
1487 void tst_QQuickListView::multipleChanges_data()
1489 QTest::addColumn<int>("startCount");
1490 QTest::addColumn<QList<ListChange> >("changes");
1491 QTest::addColumn<int>("newCount");
1492 QTest::addColumn<int>("newCurrentIndex");
1494 QList<ListChange> changes;
1496 for (int i=1; i<30; i++)
1497 changes << ListChange::remove(0);
1498 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1500 changes << ListChange::remove(0);
1501 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1504 changes << ListChange::setCurrent(29);
1505 for (int i=29; i>0; i--)
1506 changes << ListChange::remove(i);
1507 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1509 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1510 << ListChange::remove(0, 1)
1511 << ListChange::insert(0, 1)
1514 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1515 << ListChange::setCurrent(2)
1516 << ListChange::remove(2, 1)
1517 << ListChange::insert(2, 1)
1520 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1521 << ListChange::setCurrent(1)
1522 << ListChange::remove(1, 3)
1523 << ListChange::insert(2, 2)
1526 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1527 << ListChange::setCurrent(2)
1528 << ListChange::remove(1, 3)
1529 << ListChange::move(1, 5, 1)
1532 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1533 << ListChange::setCurrent(5)
1534 << ListChange::remove(4, 3)
1535 << ListChange::move(4, 1, 1)
1539 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1540 << ListChange::insert(0, 2)
1541 << ListChange::insert(0, 4)
1542 << ListChange::insert(0, 6)
1545 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1546 << ListChange::insert(0, 2)
1547 << ListChange::insert(0, 4)
1548 << ListChange::insert(0, 6)
1549 << ListChange::setCurrent(3)
1550 << ListChange::insert(3, 2)
1553 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1554 << ListChange::insert(0, 30)
1555 << ListChange::remove(0, 30)
1558 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1559 << ListChange::insert(1)
1560 << ListChange::setCurrent(1)
1561 << ListChange::remove(1)
1564 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1565 << ListChange::insert(0, 10)
1566 << ListChange::remove(5, 10)
1569 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1570 << ListChange::insert(0, 3)
1571 << ListChange::move(0, 10, 3)
1574 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1575 << ListChange::insert(0, 3)
1576 << ListChange::move(0, 8, 5)
1579 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1580 << ListChange::setCurrent(9)
1581 << ListChange::insert(10, 3)
1582 << ListChange::move(8, 0, 5)
1586 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1587 << ListChange::setCurrent(1)
1588 << ListChange::move(1, 2, 2)
1589 << ListChange::move(2, 1, 2)
1592 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1593 << ListChange::setCurrent(2)
1594 << ListChange::move(1, 2, 3)
1595 << ListChange::move(3, 0, 5)
1598 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1599 << ListChange::setCurrent(5)
1600 << ListChange::move(5, 0, 1)
1601 << ListChange::remove(0)
1604 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1605 << ListChange::setCurrent(5)
1606 << ListChange::move(5, 0, 1)
1607 << ListChange::insert(0)
1610 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1611 << ListChange::setCurrent(1)
1612 << ListChange::move(5, 1, 3)
1613 << ListChange::remove(1, 3)
1616 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1617 << ListChange::setCurrent(5)
1618 << ListChange::move(5, 1, 3)
1619 << ListChange::insert(1, 5)
1622 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1623 << ListChange::setCurrent(3)
1624 << ListChange::move(0, 1, 2)
1625 << ListChange::insert(3, 5)
1629 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1630 << ListChange::insert(0, 5)
1631 << ListChange::setCurrent(-1)
1632 << ListChange::remove(0, 5)
1633 << ListChange::insert(0, 5)
1637 void tst_QQuickListView::swapWithFirstItem()
1639 QQuickView *canvas = createView();
1642 for (int i = 0; i < 30; i++)
1643 model.addItem("Item" + QString::number(i), "");
1645 QQmlContext *ctxt = canvas->rootContext();
1646 ctxt->setContextProperty("testModel", &model);
1648 TestObject *testObject = new TestObject;
1649 ctxt->setContextProperty("testObject", testObject);
1651 canvas->setSource(testFileUrl("listviewtest.qml"));
1653 qApp->processEvents();
1655 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1656 QTRY_VERIFY(listview != 0);
1657 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1659 // ensure content position is stable
1660 listview->setContentY(0);
1661 model.moveItem(1, 0);
1662 QTRY_VERIFY(listview->contentY() == 0);
1668 void tst_QQuickListView::enforceRange()
1670 QQuickView *canvas = createView();
1673 for (int i = 0; i < 30; i++)
1674 model.addItem("Item" + QString::number(i), "");
1676 QQmlContext *ctxt = canvas->rootContext();
1677 ctxt->setContextProperty("testModel", &model);
1679 canvas->setSource(testFileUrl("listview-enforcerange.qml"));
1681 qApp->processEvents();
1683 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1684 QTRY_VERIFY(listview != 0);
1686 QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0);
1687 QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0);
1688 QTRY_COMPARE(listview->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
1689 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1691 QQuickItem *contentItem = listview->contentItem();
1692 QTRY_VERIFY(contentItem != 0);
1694 // view should be positioned at the top of the range.
1695 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
1697 QTRY_COMPARE(listview->contentY(), -100.0);
1699 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1700 QTRY_VERIFY(name != 0);
1701 QTRY_COMPARE(name->text(), model.name(0));
1702 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
1703 QTRY_VERIFY(number != 0);
1704 QTRY_COMPARE(number->text(), model.number(0));
1706 // Check currentIndex is updated when contentItem moves
1707 listview->setContentY(20);
1709 QTRY_COMPARE(listview->currentIndex(), 6);
1712 QmlListModel model2;
1713 for (int i = 0; i < 5; i++)
1714 model2.addItem("Item" + QString::number(i), "");
1716 ctxt->setContextProperty("testModel", &model2);
1717 QCOMPARE(listview->count(), 5);
1722 void tst_QQuickListView::enforceRange_withoutHighlight()
1725 // If no highlight is set but StrictlyEnforceRange is used, the content should still move
1726 // to the correct position (i.e. to the next/previous item, not next/previous section)
1727 // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex()
1729 QQuickView *canvas = createView();
1732 model.addItem("Item 0", "a");
1733 model.addItem("Item 1", "b");
1734 model.addItem("Item 2", "b");
1735 model.addItem("Item 3", "c");
1737 QQmlContext *ctxt = canvas->rootContext();
1738 ctxt->setContextProperty("testModel", &model);
1740 canvas->setSource(testFileUrl("listview-enforcerange-nohighlight.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 qreal expectedPos = -100.0;
1750 expectedPos += 10.0; // scroll past 1st section's delegate (10px height)
1751 QTRY_COMPARE(listview->contentY(), expectedPos);
1753 expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section
1754 QTest::keyClick(canvas, Qt::Key_Down);
1756 QTRY_COMPARE(listview->contentY(), expectedPos);
1758 expectedPos += 20; // scroll past 1st item of 2nd section
1759 QTest::keyClick(canvas, Qt::Key_Down);
1760 QTRY_COMPARE(listview->contentY(), expectedPos);
1762 expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section
1763 QTest::keyClick(canvas, Qt::Key_Down);
1764 QTRY_COMPARE(listview->contentY(), expectedPos);
1769 void tst_QQuickListView::spacing()
1771 QQuickView *canvas = createView();
1774 for (int i = 0; i < 30; i++)
1775 model.addItem("Item" + QString::number(i), "");
1777 QQmlContext *ctxt = canvas->rootContext();
1778 ctxt->setContextProperty("testModel", &model);
1780 TestObject *testObject = new TestObject;
1781 ctxt->setContextProperty("testObject", testObject);
1783 canvas->setSource(testFileUrl("listviewtest.qml"));
1785 qApp->processEvents();
1787 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1788 QTRY_VERIFY(listview != 0);
1790 QQuickItem *contentItem = listview->contentItem();
1791 QTRY_VERIFY(contentItem != 0);
1792 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1794 // Confirm items positioned correctly
1795 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1796 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1797 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1798 if (!item) qWarning() << "Item" << i << "not found";
1800 QTRY_VERIFY(item->y() == i*20);
1803 listview->setSpacing(10);
1804 QTRY_VERIFY(listview->spacing() == 10);
1806 // Confirm items positioned correctly
1807 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 11);
1808 for (int i = 0; i < 11; ++i) {
1809 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1810 if (!item) qWarning() << "Item" << i << "not found";
1812 QTRY_VERIFY(item->y() == i*30);
1815 listview->setSpacing(0);
1817 // Confirm items positioned correctly
1818 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() >= 16);
1819 for (int i = 0; i < 16; ++i) {
1820 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1821 if (!item) qWarning() << "Item" << i << "not found";
1823 QTRY_COMPARE(item->y(), i*20.0);
1830 template <typename T>
1831 void tst_QQuickListView::sections(const QUrl &source)
1833 QQuickView *canvas = createView();
1836 for (int i = 0; i < 30; i++)
1837 model.addItem("Item" + QString::number(i), QString::number(i/5));
1839 QQmlContext *ctxt = canvas->rootContext();
1840 ctxt->setContextProperty("testModel", &model);
1842 canvas->setSource(source);
1844 qApp->processEvents();
1846 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1847 QTRY_VERIFY(listview != 0);
1849 QQuickItem *contentItem = listview->contentItem();
1850 QTRY_VERIFY(contentItem != 0);
1852 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1854 // Confirm items positioned correctly
1855 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1856 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1857 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1859 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20));
1860 QQuickText *next = findItem<QQuickText>(item, "nextSection");
1861 QCOMPARE(next->text().toInt(), (i+1)/5);
1864 QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1866 // Remove section boundary
1867 model.removeItem(5);
1868 QTRY_COMPARE(listview->count(), model.count());
1870 // New section header created
1871 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1873 QTRY_COMPARE(item->height(), 40.0);
1875 model.insertItem(3, "New Item", "0");
1876 QTRY_COMPARE(listview->count(), model.count());
1878 // Section header moved
1879 item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1881 QTRY_COMPARE(item->height(), 20.0);
1883 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1885 QTRY_COMPARE(item->height(), 40.0);
1887 // insert item which will become a section header
1888 model.insertItem(6, "Replace header", "1");
1889 QTRY_COMPARE(listview->count(), model.count());
1891 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1893 QTRY_COMPARE(item->height(), 40.0);
1895 item = findItem<QQuickItem>(contentItem, "wrapper", 7);
1897 QTRY_COMPARE(item->height(), 20.0);
1899 QTRY_COMPARE(listview->currentSection(), QString("0"));
1901 listview->setContentY(140);
1902 QTRY_COMPARE(listview->currentSection(), QString("1"));
1904 QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
1906 listview->setContentY(20);
1907 QTRY_COMPARE(listview->currentSection(), QString("0"));
1909 QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
1911 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
1913 QTRY_COMPARE(item->height(), 20.0);
1915 // check that headers change when item changes
1916 listview->setContentY(0);
1917 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1918 model.modifyItem(0, "changed", "2");
1919 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1921 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
1923 QTRY_COMPARE(item->height(), 40.0);
1928 void tst_QQuickListView::sectionsDelegate()
1930 QSKIP("QTBUG-24395");
1932 QQuickView *canvas = createView();
1935 for (int i = 0; i < 30; i++)
1936 model.addItem("Item" + QString::number(i), QString::number(i/5));
1938 QQmlContext *ctxt = canvas->rootContext();
1939 ctxt->setContextProperty("testModel", &model);
1941 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
1943 qApp->processEvents();
1945 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1946 QTRY_VERIFY(listview != 0);
1948 QQuickItem *contentItem = listview->contentItem();
1949 QTRY_VERIFY(contentItem != 0);
1951 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1953 // Confirm items positioned correctly
1954 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1955 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1956 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1958 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20));
1959 QQuickText *next = findItem<QQuickText>(item, "nextSection");
1960 QCOMPARE(next->text().toInt(), (i+1)/5);
1963 for (int i = 0; i < 3; ++i) {
1964 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
1966 QTRY_COMPARE(item->y(), qreal(i*20*6));
1969 // ensure section header is maintained in view
1970 listview->setCurrentIndex(20);
1971 QTRY_VERIFY(listview->contentY() >= 200.0);
1972 listview->setCurrentIndex(0);
1973 QTRY_COMPARE(listview->contentY(), 0.0);
1976 model.modifyItem(0, "One", "aaa");
1977 model.modifyItem(1, "Two", "aaa");
1978 model.modifyItem(2, "Three", "aaa");
1979 model.modifyItem(3, "Four", "aaa");
1980 model.modifyItem(4, "Five", "aaa");
1981 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1983 for (int i = 0; i < 3; ++i) {
1984 QQuickItem *item = findItem<QQuickItem>(contentItem,
1985 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
1987 QTRY_COMPARE(item->y(), qreal(i*20*6));
1990 // remove section boundary
1991 model.removeItem(5);
1992 QTRY_COMPARE(listview->count(), model.count());
1993 for (int i = 0; i < 3; ++i) {
1994 QQuickItem *item = findItem<QQuickItem>(contentItem,
1995 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2000 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "sect_1");
2001 QCOMPARE(items.count(), 1);
2004 model.modifyItem(0, "One", "aaa");
2005 model.modifyItem(1, "One", "aaa");
2006 model.modifyItem(2, "One", "aaa");
2007 model.modifyItem(3, "Four", "aaa");
2008 model.modifyItem(4, "Four", "aaa");
2009 model.modifyItem(5, "Four", "aaa");
2010 model.modifyItem(6, "Five", "aaa");
2011 model.modifyItem(7, "Five", "aaa");
2012 model.modifyItem(8, "Five", "aaa");
2013 model.modifyItem(9, "Two", "aaa");
2014 model.modifyItem(10, "Two", "aaa");
2015 model.modifyItem(11, "Two", "aaa");
2016 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2017 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_aaa").count(), 1);
2018 canvas->rootObject()->setProperty("sectionProperty", "name");
2019 // ensure view has settled.
2020 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_Four").count(), 1);
2021 for (int i = 0; i < 4; ++i) {
2022 QQuickItem *item = findItem<QQuickItem>(contentItem,
2023 "sect_" + model.name(i*3));
2025 QTRY_COMPARE(item->y(), qreal(i*20*4));
2029 model.removeItems(10, 20);
2030 // ensure view has settled.
2031 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 10);
2032 // Drag view up beyond bounds
2033 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20));
2035 QMouseEvent mv(QEvent::MouseMove, QPoint(20,0), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
2036 QGuiApplication::sendEvent(canvas, &mv);
2039 QMouseEvent mv(QEvent::MouseMove, QPoint(20,-50), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
2040 QGuiApplication::sendEvent(canvas, &mv);
2043 QMouseEvent mv(QEvent::MouseMove, QPoint(20,-200), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
2044 QGuiApplication::sendEvent(canvas, &mv);
2046 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-200));
2047 // view should settle back at 0
2048 QTRY_COMPARE(listview->contentY(), 0.0);
2053 void tst_QQuickListView::sectionsPositioning()
2055 QQuickView *canvas = createView();
2058 for (int i = 0; i < 30; i++)
2059 model.addItem("Item" + QString::number(i), QString::number(i/5));
2061 QQmlContext *ctxt = canvas->rootContext();
2062 ctxt->setContextProperty("testModel", &model);
2064 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2066 qApp->processEvents();
2067 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2069 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2070 QTRY_VERIFY(listview != 0);
2071 QQuickItem *contentItem = listview->contentItem();
2072 QTRY_VERIFY(contentItem != 0);
2073 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2075 for (int i = 0; i < 3; ++i) {
2076 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2078 QTRY_COMPARE(item->y(), qreal(i*20*6));
2081 QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header
2083 QCOMPARE(topItem->y(), 0.);
2085 QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2086 QVERIFY(bottomItem);
2087 QCOMPARE(bottomItem->y(), 300.);
2089 // move down a little and check that section header is at top
2090 listview->setContentY(10);
2091 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2092 QCOMPARE(topItem->y(), 0.);
2094 // push the top header up
2095 listview->setContentY(110);
2096 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2097 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2099 QCOMPARE(topItem->y(), 100.);
2101 QQuickItem *item = findVisibleChild(contentItem, "sect_1");
2103 QCOMPARE(item->y(), 120.);
2105 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2106 QVERIFY(bottomItem);
2107 QCOMPARE(bottomItem->y(), 410.);
2109 // Move past section 0
2110 listview->setContentY(120);
2111 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2112 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2115 // Push section footer down
2116 listview->setContentY(70);
2117 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2118 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2119 QVERIFY(bottomItem);
2120 QCOMPARE(bottomItem->y(), 380.);
2122 // Change current section
2123 listview->setContentY(10);
2124 model.modifyItem(0, "One", "aaa");
2125 model.modifyItem(1, "Two", "aaa");
2126 model.modifyItem(2, "Three", "aaa");
2127 model.modifyItem(3, "Four", "aaa");
2128 model.modifyItem(4, "Five", "aaa");
2129 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2131 QTRY_COMPARE(listview->currentSection(), QString("aaa"));
2133 for (int i = 0; i < 3; ++i) {
2134 QQuickItem *item = findItem<QQuickItem>(contentItem,
2135 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2137 QTRY_COMPARE(item->y(), qreal(i*20*6));
2140 QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header
2141 QCOMPARE(topItem->y(), 10.);
2143 // remove section boundary
2144 listview->setContentY(120);
2145 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2146 model.removeItem(5);
2147 QTRY_COMPARE(listview->count(), model.count());
2148 for (int i = 1; i < 3; ++i) {
2149 QQuickItem *item = findVisibleChild(contentItem,
2150 "sect_" + QString::number(i));
2152 QTRY_COMPARE(item->y(), qreal(i*20*6));
2155 QVERIFY(topItem = findVisibleChild(contentItem, "sect_1"));
2156 QTRY_COMPARE(topItem->y(), 120.);
2158 // Change the next section
2159 listview->setContentY(0);
2160 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2161 bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2162 QVERIFY(bottomItem);
2163 QTRY_COMPARE(bottomItem->y(), 300.);
2165 model.modifyItem(14, "New", "new");
2166 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2168 QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer
2169 QTRY_COMPARE(bottomItem->y(), 300.);
2171 // Turn sticky footer off
2172 listview->setContentY(20);
2173 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2174 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2175 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2176 QCOMPARE(item->y(), 340.);
2178 // Turn sticky header off
2179 listview->setContentY(30);
2180 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2181 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2182 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2183 QCOMPARE(item->y(), 0.);
2185 // if an empty model is set the header/footer should be cleaned up
2186 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2187 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2188 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2189 QmlListModel model1;
2190 ctxt->setContextProperty("testModel", &model1);
2191 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2192 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2194 // clear model - header/footer should be cleaned up
2195 ctxt->setContextProperty("testModel", &model);
2196 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2197 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2199 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2200 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2205 void tst_QQuickListView::sectionPropertyChange()
2207 QQuickView *canvas = createView();
2209 canvas->setSource(testFileUrl("sectionpropertychange.qml"));
2211 qApp->processEvents();
2213 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2214 QTRY_VERIFY(listview != 0);
2216 QQuickItem *contentItem = listview->contentItem();
2217 QTRY_VERIFY(contentItem != 0);
2219 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2221 // Confirm items positioned correctly
2222 for (int i = 0; i < 2; ++i) {
2223 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2225 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2228 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2229 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2231 // Confirm items positioned correctly
2232 for (int i = 0; i < 2; ++i) {
2233 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2235 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2238 QMetaObject::invokeMethod(canvas->rootObject(), "switchGroups");
2239 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2241 // Confirm items positioned correctly
2242 for (int i = 0; i < 2; ++i) {
2243 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2245 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2251 void tst_QQuickListView::currentIndex_delayedItemCreation()
2253 QFETCH(bool, setCurrentToZero);
2255 QQuickView *canvas = getView();
2257 // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2258 // (since the currentItem will have changed and that shares the same index)
2259 canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2261 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2262 qApp->processEvents();
2264 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2265 QTRY_VERIFY(listview != 0);
2266 QQuickItem *contentItem = listview->contentItem();
2267 QTRY_VERIFY(contentItem != 0);
2269 QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2270 QCOMPARE(listview->currentIndex(), 0);
2271 QTRY_COMPARE(spy.count(), 1);
2273 releaseView(canvas);
2276 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2278 QTest::addColumn<bool>("setCurrentToZero");
2280 QTest::newRow("set to 0") << true;
2281 QTest::newRow("don't set to 0") << false;
2284 void tst_QQuickListView::currentIndex()
2287 for (int i = 0; i < 30; i++)
2288 model.addItem("Item" + QString::number(i), QString::number(i));
2290 QQuickView *canvas = new QQuickView(0);
2291 canvas->setGeometry(0,0,240,320);
2293 QQmlContext *ctxt = canvas->rootContext();
2294 ctxt->setContextProperty("testModel", &model);
2295 ctxt->setContextProperty("testWrap", QVariant(false));
2297 QString filename(testFile("listview-initCurrent.qml"));
2298 canvas->setSource(QUrl::fromLocalFile(filename));
2300 qApp->processEvents();
2302 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2303 QTRY_VERIFY(listview != 0);
2304 QQuickItem *contentItem = listview->contentItem();
2305 QTRY_VERIFY(contentItem != 0);
2306 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2308 // current item should be 20th item at startup
2309 // and current item should be in view
2310 QCOMPARE(listview->currentIndex(), 20);
2311 QCOMPARE(listview->contentY(), 100.0);
2312 QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2313 QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2316 listview->setCurrentIndex(0);
2317 QCOMPARE(listview->currentIndex(), 0);
2318 // confirm that the velocity is updated
2319 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2321 listview->incrementCurrentIndex();
2322 QCOMPARE(listview->currentIndex(), 1);
2323 listview->decrementCurrentIndex();
2324 QCOMPARE(listview->currentIndex(), 0);
2326 listview->decrementCurrentIndex();
2327 QCOMPARE(listview->currentIndex(), 0);
2330 ctxt->setContextProperty("testWrap", QVariant(true));
2331 QVERIFY(listview->isWrapEnabled());
2333 listview->decrementCurrentIndex();
2334 QCOMPARE(listview->currentIndex(), model.count()-1);
2336 QTRY_COMPARE(listview->contentY(), 280.0);
2338 listview->incrementCurrentIndex();
2339 QCOMPARE(listview->currentIndex(), 0);
2341 QTRY_COMPARE(listview->contentY(), 0.0);
2344 // footer should become visible if it is out of view, and then current index is set to count-1
2345 canvas->rootObject()->setProperty("showFooter", true);
2346 QTRY_VERIFY(listview->footerItem());
2347 listview->setCurrentIndex(model.count()-2);
2348 QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2349 listview->setCurrentIndex(model.count()-1);
2350 QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2351 canvas->rootObject()->setProperty("showFooter", false);
2353 // header should become visible if it is out of view, and then current index is set to 0
2354 canvas->rootObject()->setProperty("showHeader", true);
2355 QTRY_VERIFY(listview->headerItem());
2356 listview->setCurrentIndex(1);
2357 QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2358 listview->setCurrentIndex(0);
2359 QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2360 canvas->rootObject()->setProperty("showHeader", false);
2365 canvas->requestActivateWindow();
2366 QTest::qWaitForWindowShown(canvas);
2367 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2369 listview->setCurrentIndex(0);
2371 QTest::keyClick(canvas, Qt::Key_Down);
2372 QCOMPARE(listview->currentIndex(), 1);
2374 QTest::keyClick(canvas, Qt::Key_Up);
2375 QCOMPARE(listview->currentIndex(), 0);
2377 // hold down Key_Down
2378 for (int i=0; i<model.count()-1; i++) {
2379 QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
2380 QTRY_COMPARE(listview->currentIndex(), i+1);
2382 QTest::keyRelease(canvas, Qt::Key_Down);
2383 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2384 QTRY_COMPARE(listview->contentY(), 280.0);
2387 for (int i=model.count()-1; i > 0; i--) {
2388 QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
2389 QTRY_COMPARE(listview->currentIndex(), i-1);
2391 QTest::keyRelease(canvas, Qt::Key_Up);
2392 QTRY_COMPARE(listview->currentIndex(), 0);
2393 QTRY_COMPARE(listview->contentY(), 0.0);
2396 // turn off auto highlight
2397 listview->setHighlightFollowsCurrentItem(false);
2398 QVERIFY(listview->highlightFollowsCurrentItem() == false);
2400 QVERIFY(listview->highlightItem());
2401 qreal hlPos = listview->highlightItem()->y();
2403 listview->setCurrentIndex(4);
2404 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2406 // insert item before currentIndex
2407 listview->setCurrentIndex(28);
2408 model.insertItem(0, "Foo", "1111");
2409 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2411 // check removing highlight by setting currentIndex to -1;
2412 listview->setCurrentIndex(-1);
2414 QCOMPARE(listview->currentIndex(), -1);
2415 QVERIFY(!listview->highlightItem());
2416 QVERIFY(!listview->currentItem());
2418 listview->setCurrentIndex(0);
2419 QTRY_VERIFY(listview->currentItem()->isVisible());
2420 listview->setContentY(200);
2421 QTRY_VERIFY(!listview->currentItem()->isVisible());
2426 void tst_QQuickListView::noCurrentIndex()
2429 for (int i = 0; i < 30; i++)
2430 model.addItem("Item" + QString::number(i), QString::number(i));
2432 QQuickView *canvas = new QQuickView(0);
2433 canvas->setGeometry(0,0,240,320);
2435 QQmlContext *ctxt = canvas->rootContext();
2436 ctxt->setContextProperty("testModel", &model);
2438 QString filename(testFile("listview-noCurrent.qml"));
2439 canvas->setSource(QUrl::fromLocalFile(filename));
2441 qApp->processEvents();
2443 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2444 QTRY_VERIFY(listview != 0);
2445 QQuickItem *contentItem = listview->contentItem();
2446 QTRY_VERIFY(contentItem != 0);
2447 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2449 // current index should be -1 at startup
2450 // and we should not have a currentItem or highlightItem
2451 QCOMPARE(listview->currentIndex(), -1);
2452 QCOMPARE(listview->contentY(), 0.0);
2453 QVERIFY(!listview->highlightItem());
2454 QVERIFY(!listview->currentItem());
2456 listview->setCurrentIndex(2);
2457 QCOMPARE(listview->currentIndex(), 2);
2458 QVERIFY(listview->highlightItem());
2459 QVERIFY(listview->currentItem());
2464 void tst_QQuickListView::itemList()
2466 QQuickView *canvas = createView();
2467 canvas->setSource(testFileUrl("itemlist.qml"));
2469 qApp->processEvents();
2471 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2472 QTRY_VERIFY(listview != 0);
2474 QQuickItem *contentItem = listview->contentItem();
2475 QTRY_VERIFY(contentItem != 0);
2477 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2478 QTRY_VERIFY(model != 0);
2480 QTRY_VERIFY(model->count() == 3);
2481 QTRY_COMPARE(listview->currentIndex(), 0);
2483 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2485 QTRY_COMPARE(item->x(), 0.0);
2486 QCOMPARE(item->height(), listview->height());
2488 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2490 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2492 listview->setCurrentIndex(2);
2494 item = findItem<QQuickItem>(contentItem, "item3");
2496 QTRY_COMPARE(item->x(), 480.0);
2498 text = findItem<QQuickText>(contentItem, "text3");
2500 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2505 void tst_QQuickListView::cacheBuffer()
2507 QQuickView *canvas = createView();
2510 for (int i = 0; i < 90; i++)
2511 model.addItem("Item" + QString::number(i), "");
2513 QQmlContext *ctxt = canvas->rootContext();
2514 ctxt->setContextProperty("testModel", &model);
2516 TestObject *testObject = new TestObject;
2517 ctxt->setContextProperty("testObject", testObject);
2519 canvas->setSource(testFileUrl("listviewtest.qml"));
2521 qApp->processEvents();
2523 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2524 QTRY_VERIFY(listview != 0);
2526 QQuickItem *contentItem = listview->contentItem();
2527 QTRY_VERIFY(contentItem != 0);
2528 QTRY_VERIFY(listview->delegate() != 0);
2529 QTRY_VERIFY(listview->model() != 0);
2530 QTRY_VERIFY(listview->highlight() != 0);
2532 // Confirm items positioned correctly
2533 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2534 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2535 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2536 if (!item) qWarning() << "Item" << i << "not found";
2538 QTRY_VERIFY(item->y() == i*20);
2541 QQmlIncubationController controller;
2542 canvas->engine()->setIncubationController(&controller);
2544 testObject->setCacheBuffer(200);
2545 QTRY_VERIFY(listview->cacheBuffer() == 200);
2547 // items will be created one at a time
2548 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2549 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2550 QQuickItem *item = 0;
2553 controller.incubateWhile(&b);
2554 item = findItem<QQuickItem>(listview, "wrapper", i);
2560 controller.incubateWhile(&b);
2563 int newItemCount = 0;
2564 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2566 // Confirm items positioned correctly
2567 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2568 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2569 if (!item) qWarning() << "Item" << i << "not found";
2571 QTRY_VERIFY(item->y() == i*20);
2574 // move view and confirm items in view are visible immediately and outside are created async
2575 listview->setContentY(300);
2577 for (int i = 15; i < 32; ++i) {
2578 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2579 if (!item) qWarning() << "Item" << i << "not found";
2581 QVERIFY(item->y() == i*20);
2584 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2586 // ensure buffered items are created
2587 for (int i = 32; i < qMin(41,model.count()); ++i) {
2588 QQuickItem *item = 0;
2590 qGuiApp->processEvents(); // allow refill to happen
2592 controller.incubateWhile(&b);
2593 item = findItem<QQuickItem>(listview, "wrapper", i);
2599 controller.incubateWhile(&b);
2606 void tst_QQuickListView::positionViewAtIndex()
2608 QQuickView *canvas = createView();
2611 for (int i = 0; i < 40; i++)
2612 model.addItem("Item" + QString::number(i), "");
2614 QQmlContext *ctxt = canvas->rootContext();
2615 ctxt->setContextProperty("testModel", &model);
2617 TestObject *testObject = new TestObject;
2618 ctxt->setContextProperty("testObject", testObject);
2620 canvas->setSource(testFileUrl("listviewtest.qml"));
2621 qApp->processEvents();
2623 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2624 QTRY_VERIFY(listview != 0);
2625 QQuickItem *contentItem = listview->contentItem();
2626 QTRY_VERIFY(contentItem != 0);
2627 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2629 // Confirm items positioned correctly
2630 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2631 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2632 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2633 if (!item) qWarning() << "Item" << i << "not found";
2635 QTRY_COMPARE(item->y(), i*20.);
2638 // Position on a currently visible item
2639 listview->positionViewAtIndex(3, QQuickListView::Beginning);
2640 QTRY_COMPARE(listview->contentY(), 60.);
2642 // Confirm items positioned correctly
2643 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2644 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2645 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2646 if (!item) qWarning() << "Item" << i << "not found";
2648 QTRY_COMPARE(item->y(), i*20.);
2651 // Position on an item beyond the visible items
2652 listview->positionViewAtIndex(22, QQuickListView::Beginning);
2653 QTRY_COMPARE(listview->contentY(), 440.);
2655 // Confirm items positioned correctly
2656 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2657 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2658 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2659 if (!item) qWarning() << "Item" << i << "not found";
2661 QTRY_COMPARE(item->y(), i*20.);
2664 // Position on an item that would leave empty space if positioned at the top
2665 listview->positionViewAtIndex(28, QQuickListView::Beginning);
2666 QTRY_COMPARE(listview->contentY(), 480.);
2668 // Confirm items positioned correctly
2669 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2670 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2671 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2672 if (!item) qWarning() << "Item" << i << "not found";
2674 QTRY_COMPARE(item->y(), i*20.);
2677 // Position at the beginning again
2678 listview->positionViewAtIndex(0, QQuickListView::Beginning);
2679 QTRY_COMPARE(listview->contentY(), 0.);
2681 // Confirm items positioned correctly
2682 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2683 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2684 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2685 if (!item) qWarning() << "Item" << i << "not found";
2687 QTRY_COMPARE(item->y(), i*20.);
2690 // Position at End using last index
2691 listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
2692 QTRY_COMPARE(listview->contentY(), 480.);
2694 // Confirm items positioned correctly
2695 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2696 for (int i = 24; i < model.count(); ++i) {
2697 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2698 if (!item) qWarning() << "Item" << i << "not found";
2700 QTRY_COMPARE(item->y(), i*20.);
2704 listview->positionViewAtIndex(20, QQuickListView::End);
2705 QTRY_COMPARE(listview->contentY(), 100.);
2707 // Position in Center
2708 listview->positionViewAtIndex(15, QQuickListView::Center);
2709 QTRY_COMPARE(listview->contentY(), 150.);
2711 // Ensure at least partially visible
2712 listview->positionViewAtIndex(15, QQuickListView::Visible);
2713 QTRY_COMPARE(listview->contentY(), 150.);
2715 listview->setContentY(302);
2716 listview->positionViewAtIndex(15, QQuickListView::Visible);
2717 QTRY_COMPARE(listview->contentY(), 302.);
2719 listview->setContentY(320);
2720 listview->positionViewAtIndex(15, QQuickListView::Visible);
2721 QTRY_COMPARE(listview->contentY(), 300.);
2723 listview->setContentY(85);
2724 listview->positionViewAtIndex(20, QQuickListView::Visible);
2725 QTRY_COMPARE(listview->contentY(), 85.);
2727 listview->setContentY(75);
2728 listview->positionViewAtIndex(20, QQuickListView::Visible);
2729 QTRY_COMPARE(listview->contentY(), 100.);
2731 // Ensure completely visible
2732 listview->setContentY(120);
2733 listview->positionViewAtIndex(20, QQuickListView::Contain);
2734 QTRY_COMPARE(listview->contentY(), 120.);
2736 listview->setContentY(302);
2737 listview->positionViewAtIndex(15, QQuickListView::Contain);
2738 QTRY_COMPARE(listview->contentY(), 300.);
2740 listview->setContentY(85);
2741 listview->positionViewAtIndex(20, QQuickListView::Contain);
2742 QTRY_COMPARE(listview->contentY(), 100.);
2744 // positionAtBeginnging
2745 listview->positionViewAtBeginning();
2746 QTRY_COMPARE(listview->contentY(), 0.);
2748 listview->setContentY(80);
2749 canvas->rootObject()->setProperty("showHeader", true);
2750 listview->positionViewAtBeginning();
2751 QTRY_COMPARE(listview->contentY(), -30.);
2754 listview->positionViewAtEnd();
2755 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
2757 listview->setContentY(80);
2758 canvas->rootObject()->setProperty("showFooter", true);
2759 listview->positionViewAtEnd();
2760 QTRY_COMPARE(listview->contentY(), 510.);
2762 // set current item to outside visible view, position at beginning
2763 // and ensure highlight moves to current item
2764 listview->setCurrentIndex(1);
2765 listview->positionViewAtBeginning();
2766 QTRY_COMPARE(listview->contentY(), -30.);
2767 QVERIFY(listview->highlightItem());
2768 QCOMPARE(listview->highlightItem()->y(), 20.);
2774 void tst_QQuickListView::resetModel()
2776 QQuickView *canvas = createView();
2778 QStringList strings;
2779 strings << "one" << "two" << "three";
2780 QStringListModel model(strings);
2782 QQmlContext *ctxt = canvas->rootContext();
2783 ctxt->setContextProperty("testModel", &model);
2785 canvas->setSource(testFileUrl("displaylist.qml"));
2787 qApp->processEvents();
2789 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2790 QTRY_VERIFY(listview != 0);
2791 QQuickItem *contentItem = listview->contentItem();
2792 QTRY_VERIFY(contentItem != 0);
2793 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2795 QTRY_COMPARE(listview->count(), model.rowCount());
2797 for (int i = 0; i < model.rowCount(); ++i) {
2798 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2799 QTRY_VERIFY(display != 0);
2800 QTRY_COMPARE(display->text(), strings.at(i));
2804 strings << "four" << "five" << "six" << "seven";
2805 model.setStringList(strings);
2807 QTRY_COMPARE(listview->count(), model.rowCount());
2809 for (int i = 0; i < model.rowCount(); ++i) {
2810 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2811 QTRY_VERIFY(display != 0);
2812 QTRY_COMPARE(display->text(), strings.at(i));
2818 void tst_QQuickListView::propertyChanges()
2820 QQuickView *canvas = createView();
2821 QTRY_VERIFY(canvas);
2822 canvas->setSource(testFileUrl("propertychangestest.qml"));
2824 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
2825 QTRY_VERIFY(listView);
2827 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
2828 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
2829 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
2830 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
2831 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
2832 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
2833 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
2835 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
2836 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
2837 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
2838 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
2839 QTRY_COMPARE(listView->isWrapEnabled(), true);
2840 QTRY_COMPARE(listView->cacheBuffer(), 10);
2841 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
2843 listView->setHighlightFollowsCurrentItem(false);
2844 listView->setPreferredHighlightBegin(1.0);
2845 listView->setPreferredHighlightEnd(1.0);
2846 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
2847 listView->setWrapEnabled(false);
2848 listView->setCacheBuffer(3);
2849 listView->setSnapMode(QQuickListView::SnapOneItem);
2851 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
2852 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
2853 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
2854 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
2855 QTRY_COMPARE(listView->isWrapEnabled(), false);
2856 QTRY_COMPARE(listView->cacheBuffer(), 3);
2857 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
2859 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
2860 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
2861 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
2862 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
2863 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2864 QTRY_COMPARE(cacheBufferSpy.count(),1);
2865 QTRY_COMPARE(snapModeSpy.count(),1);
2867 listView->setHighlightFollowsCurrentItem(false);
2868 listView->setPreferredHighlightBegin(1.0);
2869 listView->setPreferredHighlightEnd(1.0);
2870 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
2871 listView->setWrapEnabled(false);
2872 listView->setCacheBuffer(3);
2873 listView->setSnapMode(QQuickListView::SnapOneItem);
2875 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
2876 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
2877 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
2878 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
2879 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2880 QTRY_COMPARE(cacheBufferSpy.count(),1);
2881 QTRY_COMPARE(snapModeSpy.count(),1);
2886 void tst_QQuickListView::componentChanges()
2888 QQuickView *canvas = createView();
2889 QTRY_VERIFY(canvas);
2890 canvas->setSource(testFileUrl("propertychangestest.qml"));
2892 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
2893 QTRY_VERIFY(listView);
2895 QQmlComponent component(canvas->engine());
2896 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2898 QQmlComponent delegateComponent(canvas->engine());
2899 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2901 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
2902 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
2903 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
2904 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
2906 listView->setHighlight(&component);
2907 listView->setHeader(&component);
2908 listView->setFooter(&component);
2909 listView->setDelegate(&delegateComponent);
2911 QTRY_COMPARE(listView->highlight(), &component);
2912 QTRY_COMPARE(listView->header(), &component);
2913 QTRY_COMPARE(listView->footer(), &component);
2914 QTRY_COMPARE(listView->delegate(), &delegateComponent);
2916 QTRY_COMPARE(highlightSpy.count(),1);
2917 QTRY_COMPARE(delegateSpy.count(),1);
2918 QTRY_COMPARE(headerSpy.count(),1);
2919 QTRY_COMPARE(footerSpy.count(),1);
2921 listView->setHighlight(&component);
2922 listView->setHeader(&component);
2923 listView->setFooter(&component);
2924 listView->setDelegate(&delegateComponent);
2926 QTRY_COMPARE(highlightSpy.count(),1);
2927 QTRY_COMPARE(delegateSpy.count(),1);
2928 QTRY_COMPARE(headerSpy.count(),1);
2929 QTRY_COMPARE(footerSpy.count(),1);
2934 void tst_QQuickListView::modelChanges()
2936 QQuickView *canvas = createView();
2937 QTRY_VERIFY(canvas);
2938 canvas->setSource(testFileUrl("propertychangestest.qml"));
2940 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
2941 QTRY_VERIFY(listView);
2943 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2944 QTRY_VERIFY(alternateModel);
2945 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2946 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
2948 listView->setModel(modelVariant);
2949 QTRY_COMPARE(listView->model(), modelVariant);
2950 QTRY_COMPARE(modelSpy.count(),1);
2952 listView->setModel(modelVariant);
2953 QTRY_COMPARE(modelSpy.count(),1);
2955 listView->setModel(QVariant());
2956 QTRY_COMPARE(modelSpy.count(),2);
2961 void tst_QQuickListView::QTBUG_9791()
2963 QQuickView *canvas = createView();
2965 canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
2966 qApp->processEvents();
2968 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
2969 QTRY_VERIFY(listview != 0);
2971 QQuickItem *contentItem = listview->contentItem();
2972 QTRY_VERIFY(contentItem != 0);
2973 QTRY_VERIFY(listview->delegate() != 0);
2974 QTRY_VERIFY(listview->model() != 0);
2976 QMetaObject::invokeMethod(listview, "fillModel");
2977 qApp->processEvents();
2979 // Confirm items positioned correctly
2980 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
2981 QCOMPARE(itemCount, 3);
2983 for (int i = 0; i < itemCount; ++i) {
2984 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2985 if (!item) qWarning() << "Item" << i << "not found";
2987 QTRY_COMPARE(item->x(), i*300.0);
2990 // check that view is positioned correctly
2991 QTRY_COMPARE(listview->contentX(), 590.0);
2996 void tst_QQuickListView::manualHighlight()
2998 QQuickView *canvas = new QQuickView(0);
2999 canvas->setGeometry(0,0,240,320);
3001 QString filename(testFile("manual-highlight.qml"));
3002 canvas->setSource(QUrl::fromLocalFile(filename));
3004 qApp->processEvents();
3006 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3007 QTRY_VERIFY(listview != 0);
3009 QQuickItem *contentItem = listview->contentItem();
3010 QTRY_VERIFY(contentItem != 0);
3012 QTRY_COMPARE(listview->currentIndex(), 0);
3013 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3014 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3016 listview->setCurrentIndex(2);
3018 QTRY_COMPARE(listview->currentIndex(), 2);
3019 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3020 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3023 listview->positionViewAtIndex(3, QQuickListView::Contain);
3025 QTRY_COMPARE(listview->currentIndex(), 2);
3026 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3027 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3032 void tst_QQuickListView::QTBUG_11105()
3034 QQuickView *canvas = createView();
3037 for (int i = 0; i < 30; i++)
3038 model.addItem("Item" + QString::number(i), "");
3040 QQmlContext *ctxt = canvas->rootContext();
3041 ctxt->setContextProperty("testModel", &model);
3043 TestObject *testObject = new TestObject;
3044 ctxt->setContextProperty("testObject", testObject);
3046 canvas->setSource(testFileUrl("listviewtest.qml"));
3048 qApp->processEvents();
3050 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3051 QTRY_VERIFY(listview != 0);
3052 QQuickItem *contentItem = listview->contentItem();
3053 QTRY_VERIFY(contentItem != 0);
3054 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3056 // Confirm items positioned correctly
3057 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3058 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3059 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3060 if (!item) qWarning() << "Item" << i << "not found";
3062 QTRY_VERIFY(item->y() == i*20);
3065 listview->positionViewAtIndex(20, QQuickListView::Beginning);
3066 QCOMPARE(listview->contentY(), 280.);
3068 QmlListModel model2;
3069 for (int i = 0; i < 5; i++)
3070 model2.addItem("Item" + QString::number(i), "");
3072 ctxt->setContextProperty("testModel", &model2);
3074 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3075 QCOMPARE(itemCount, 5);
3081 void tst_QQuickListView::header()
3083 QFETCH(QQuickListView::Orientation, orientation);
3084 QFETCH(Qt::LayoutDirection, layoutDirection);
3085 QFETCH(QPointF, initialHeaderPos);
3086 QFETCH(QPointF, firstDelegatePos);
3087 QFETCH(QPointF, initialContentPos);
3088 QFETCH(QPointF, changedHeaderPos);
3089 QFETCH(QPointF, changedContentPos);
3090 QFETCH(QPointF, resizeContentPos);
3093 for (int i = 0; i < 30; i++)
3094 model.addItem("Item" + QString::number(i), "");
3096 QQuickView *canvas = getView();
3097 canvas->rootContext()->setContextProperty("testModel", &model);
3098 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3099 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3100 canvas->setSource(testFileUrl("header.qml"));
3102 qApp->processEvents();
3104 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3105 QTRY_VERIFY(listview != 0);
3106 listview->setOrientation(orientation);
3107 listview->setLayoutDirection(layoutDirection);
3109 QQuickItem *contentItem = listview->contentItem();
3110 QTRY_VERIFY(contentItem != 0);
3112 QQuickText *header = 0;
3113 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3114 QVERIFY(header == listview->headerItem());
3116 QCOMPARE(header->width(), 100.);
3117 QCOMPARE(header->height(), 30.);
3118 QCOMPARE(header->pos(), initialHeaderPos);
3119 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3121 if (orientation == QQuickListView::Vertical)
3122 QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3124 QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3126 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3128 QCOMPARE(item->pos(), firstDelegatePos);
3131 QTRY_COMPARE(listview->count(), model.count());
3132 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3134 for (int i = 0; i < 30; i++)
3135 model.addItem("Item" + QString::number(i), "");
3137 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3138 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3140 QCOMPARE(headerItemSpy.count(), 1);
3142 header = findItem<QQuickText>(contentItem, "header");
3144 header = findItem<QQuickText>(contentItem, "header2");
3147 QVERIFY(header == listview->headerItem());
3149 QCOMPARE(header->pos(), changedHeaderPos);
3150 QCOMPARE(header->width(), 50.);
3151 QCOMPARE(header->height(), 20.);
3152 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3154 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3156 QCOMPARE(item->pos(), firstDelegatePos);
3158 releaseView(canvas);
3161 // QTBUG-21207 header should become visible if view resizes from initial empty size
3164 canvas->rootContext()->setContextProperty("testModel", &model);
3165 canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3166 canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3167 canvas->setSource(testFileUrl("header.qml"));
3169 qApp->processEvents();
3171 listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3172 QTRY_VERIFY(listview != 0);
3173 listview->setOrientation(orientation);
3174 listview->setLayoutDirection(layoutDirection);
3175 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3177 listview->setWidth(240);
3178 listview->setHeight(320);
3179 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3180 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3182 releaseView(canvas);
3185 void tst_QQuickListView::header_data()
3187 QTest::addColumn<QQuickListView::Orientation>("orientation");
3188 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3189 QTest::addColumn<QPointF>("initialHeaderPos");
3190 QTest::addColumn<QPointF>("changedHeaderPos");
3191 QTest::addColumn<QPointF>("initialContentPos");
3192 QTest::addColumn<QPointF>("changedContentPos");
3193 QTest::addColumn<QPointF>("firstDelegatePos");
3194 QTest::addColumn<QPointF>("resizeContentPos");
3196 // header1 = 100 x 30
3197 // header2 = 50 x 20
3198 // delegates = 240 x 20
3201 // header above items, top left
3202 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight
3210 // header above items, top right
3211 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft
3219 // header to left of items
3220 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight
3228 // header to right of items
3229 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft
3232 << QPointF(-240 + 100, 0)
3233 << QPointF(-240 + 50, 0)
3235 << QPointF(-240 + 40, 0);
3238 void tst_QQuickListView::header_delayItemCreation()
3240 QQuickView *canvas = createView();
3244 canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3245 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
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 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3256 QCOMPARE(header->y(), -header->height());
3258 QCOMPARE(listview->contentY(), -header->height());
3261 QTRY_COMPARE(header->y(), -header->height());
3266 void tst_QQuickListView::footer()
3268 QFETCH(QQuickListView::Orientation, orientation);
3269 QFETCH(Qt::LayoutDirection, layoutDirection);
3270 QFETCH(QPointF, initialFooterPos);
3271 QFETCH(QPointF, firstDelegatePos);
3272 QFETCH(QPointF, initialContentPos);
3273 QFETCH(QPointF, changedFooterPos);
3274 QFETCH(QPointF, changedContentPos);
3275 QFETCH(QPointF, resizeContentPos);
3277 QQuickView *canvas = getView();
3280 for (int i = 0; i < 3; i++)
3281 model.addItem("Item" + QString::number(i), "");
3283 QQmlContext *ctxt = canvas->rootContext();
3284 ctxt->setContextProperty("testModel", &model);
3286 canvas->setSource(testFileUrl("footer.qml"));
3288 qApp->processEvents();
3290 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3291 QTRY_VERIFY(listview != 0);
3292 listview->setOrientation(orientation);
3293 listview->setLayoutDirection(layoutDirection);
3294 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3296 QQuickItem *contentItem = listview->contentItem();
3297 QTRY_VERIFY(contentItem != 0);
3299 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3302 QVERIFY(footer == listview->footerItem());
3304 QCOMPARE(footer->pos(), initialFooterPos);
3305 QCOMPARE(footer->width(), 100.);
3306 QCOMPARE(footer->height(), 30.);
3307 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3309 if (orientation == QQuickListView::Vertical)
3310 QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3312 QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3314 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3316 QCOMPARE(item->pos(), firstDelegatePos);
3319 model.removeItem(1);
3321 if (orientation == QQuickListView::Vertical) {
3322 QTRY_COMPARE(footer->y(), initialFooterPos.y() - 20); // delegate height = 20
3324 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3325 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3331 QPointF posWhenNoItems(0, 0);
3332 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3333 posWhenNoItems.setX(-100);
3334 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3336 // if header is present, it's at a negative pos, so the footer should not move
3337 canvas->rootObject()->setProperty("showHeader", true);
3338 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3339 canvas->rootObject()->setProperty("showHeader", false);
3342 for (int i = 0; i < 30; i++)
3343 model.addItem("Item" + QString::number(i), "");
3345 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3346 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3348 QCOMPARE(footerItemSpy.count(), 1);
3350 footer = findItem<QQuickText>(contentItem, "footer");
3352 footer = findItem<QQuickText>(contentItem, "footer2");
3355 QVERIFY(footer == listview->footerItem());
3357 QCOMPARE(footer->pos(), changedFooterPos);
3358 QCOMPARE(footer->width(), 50.);
3359 QCOMPARE(footer->height(), 20.);
3360 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3362 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3364 QCOMPARE(item->pos(), firstDelegatePos);
3366 listview->positionViewAtEnd();
3367 footer->setHeight(10);
3368 footer->setWidth(40);
3369 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3371 releaseView(canvas);
3374 void tst_QQuickListView::footer_data()
3376 QTest::addColumn<QQuickListView::Orientation>("orientation");
3377 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3378 QTest::addColumn<QPointF>("initialFooterPos");
3379 QTest::addColumn<QPointF>("changedFooterPos");
3380 QTest::addColumn<QPointF>("initialContentPos");
3381 QTest::addColumn<QPointF>("changedContentPos");
3382 QTest::addColumn<QPointF>("firstDelegatePos");
3383 QTest::addColumn<QPointF>("resizeContentPos");
3385 // footer1 = 100 x 30
3386 // footer2 = 50 x 20
3387 // delegates = 40 x 20
3389 // view height = 320
3391 // footer below items, bottom left
3392 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight
3393 << QPointF(0, 3 * 20)
3394 << QPointF(0, 30 * 20) // added 30 items
3398 << QPointF(0, 30 * 20 - 320 + 10);
3400 // footer below items, bottom right
3401 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft
3402 << QPointF(0, 3 * 20)
3403 << QPointF(0, 30 * 20)
3407 << QPointF(0, 30 * 20 - 320 + 10);
3409 // footer to right of items
3410 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight
3411 << QPointF(40 * 3, 0)
3412 << QPointF(40 * 30, 0)
3416 << QPointF(40 * 30 - 240 + 40, 0);
3418 // footer to left of items
3419 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft
3420 << QPointF(-(40 * 3) - 100, 0)
3421 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3425 << QPointF(-(40 * 30) - 40, 0);
3428 class LVAccessor : public QQuickListView
3431 qreal minY() const { return minYExtent(); }
3432 qreal maxY() const { return maxYExtent(); }
3433 qreal minX() const { return minXExtent(); }
3434 qreal maxX() const { return maxXExtent(); }
3437 void tst_QQuickListView::headerFooter()
3441 QQuickView *canvas = createView();
3444 QQmlContext *ctxt = canvas->rootContext();
3445 ctxt->setContextProperty("testModel", &model);
3447 canvas->setSource(testFileUrl("headerfooter.qml"));
3448 qApp->processEvents();
3450 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3451 QTRY_VERIFY(listview != 0);
3453 QQuickItem *contentItem = listview->contentItem();
3454 QTRY_VERIFY(contentItem != 0);
3456 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3458 QCOMPARE(header->y(), -header->height());
3460 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3462 QCOMPARE(footer->y(), 0.);
3464 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), header->height());
3465 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), header->height());
3471 QQuickView *canvas = createView();
3474 QQmlContext *ctxt = canvas->rootContext();
3475 ctxt->setContextProperty("testModel", &model);
3477 canvas->setSource(testFileUrl("headerfooter.qml"));
3478 canvas->rootObject()->setProperty("horizontal", true);
3479 qApp->processEvents();
3481 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3482 QTRY_VERIFY(listview != 0);
3484 QQuickItem *contentItem = listview->contentItem();
3485 QTRY_VERIFY(contentItem != 0);
3487 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3489 QCOMPARE(header->x(), -header->width());
3491 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3493 QCOMPARE(footer->x(), 0.);
3495 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), header->width());
3496 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), header->width());
3502 QQuickView *canvas = createView();
3505 QQmlContext *ctxt = canvas->rootContext();
3506 ctxt->setContextProperty("testModel", &model);
3508 canvas->setSource(testFileUrl("headerfooter.qml"));
3509 canvas->rootObject()->setProperty("horizontal", true);
3510 canvas->rootObject()->setProperty("rtl", true);
3511 qApp->processEvents();
3513 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3514 QTRY_VERIFY(listview != 0);
3516 QQuickItem *contentItem = listview->contentItem();
3517 QTRY_VERIFY(contentItem != 0);
3519 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3521 QCOMPARE(header->x(), 0.);
3523 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3525 QCOMPARE(footer->x(), -footer->width());
3527 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), 240. - header->width());
3528 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), 240. - header->width());
3534 QQuickView *canvas = createView();
3537 for (int i = 0; i < 4; i++)
3538 model.addItem("Item" + QString::number(i), "");
3539 QQmlContext *ctxt = canvas->rootContext();
3540 ctxt->setContextProperty("testModel", &model);
3542 canvas->setSource(testFileUrl("headerfooter.qml"));
3543 qApp->processEvents();
3545 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3546 QTRY_VERIFY(listview != 0);
3548 QQuickItem *contentItem = listview->contentItem();
3549 QTRY_VERIFY(contentItem != 0);
3551 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3553 QCOMPARE(header->y(), -header->height());
3555 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3557 QCOMPARE(footer->y(), 30.*4);
3561 header = findItem<QQuickItem>(contentItem, "header");
3563 QCOMPARE(header->y(), -header->height());
3565 footer = findItem<QQuickItem>(contentItem, "footer");
3567 QCOMPARE(footer->y(), 30.*4);
3573 void tst_QQuickListView::resizeView()
3575 QQuickView *canvas = createView();
3578 for (int i = 0; i < 40; i++)
3579 model.addItem("Item" + QString::number(i), "");
3581 QQmlContext *ctxt = canvas->rootContext();
3582 ctxt->setContextProperty("testModel", &model);
3584 TestObject *testObject = new TestObject;
3585 ctxt->setContextProperty("testObject", testObject);
3587 canvas->setSource(testFileUrl("listviewtest.qml"));
3589 qApp->processEvents();
3591 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3592 QTRY_VERIFY(listview != 0);
3593 QQuickItem *contentItem = listview->contentItem();
3594 QTRY_VERIFY(contentItem != 0);
3595 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3597 // Confirm items positioned correctly
3598 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3599 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3600 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3601 if (!item) qWarning() << "Item" << i << "not found";
3603 QTRY_COMPARE(item->y(), i*20.);
3606 QVariant heightRatio;
3607 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3608 QCOMPARE(heightRatio.toReal(), 0.4);
3610 listview->setHeight(200);
3611 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3613 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3614 QCOMPARE(heightRatio.toReal(), 0.25);
3616 // Ensure we handle -ve sizes
3617 listview->setHeight(-100);
3618 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
3620 listview->setCacheBuffer(200);
3621 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
3623 // ensure items in cache become visible
3624 listview->setHeight(200);
3625 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
3627 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3628 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3629 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3630 if (!item) qWarning() << "Item" << i << "not found";
3632 QTRY_COMPARE(item->y(), i*20.);
3633 QCOMPARE(item->isVisible(), i < 11); // inside view visible, outside not visible
3636 // ensure items outside view become invisible
3637 listview->setHeight(100);
3638 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
3640 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3641 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3642 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3643 if (!item) qWarning() << "Item" << i << "not found";
3645 QTRY_COMPARE(item->y(), i*20.);
3646 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3653 void tst_QQuickListView::resizeViewAndRepaint()
3655 QQuickView *canvas = createView();
3658 for (int i = 0; i < 40; i++)
3659 model.addItem("Item" + QString::number(i), "");
3661 QQmlContext *ctxt = canvas->rootContext();
3662 ctxt->setContextProperty("testModel", &model);
3663 ctxt->setContextProperty("initialHeight", 100);
3665 canvas->setSource(testFileUrl("resizeview.qml"));
3667 qApp->processEvents();
3669 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3670 QTRY_VERIFY(listview != 0);
3671 QQuickItem *contentItem = listview->contentItem();
3672 QTRY_VERIFY(contentItem != 0);
3673 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3675 // item at index 10 should not be currently visible
3676 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3678 listview->setHeight(320);
3680 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3682 listview->setHeight(100);
3683 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3688 void tst_QQuickListView::sizeLessThan1()
3690 QQuickView *canvas = createView();
3693 for (int i = 0; i < 30; i++)
3694 model.addItem("Item" + QString::number(i), "");
3696 QQmlContext *ctxt = canvas->rootContext();
3697 ctxt->setContextProperty("testModel", &model);
3699 TestObject *testObject = new TestObject;
3700 ctxt->setContextProperty("testObject", testObject);
3702 canvas->setSource(testFileUrl("sizelessthan1.qml"));
3704 qApp->processEvents();
3706 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3707 QTRY_VERIFY(listview != 0);
3708 QQuickItem *contentItem = listview->contentItem();
3709 QTRY_VERIFY(contentItem != 0);
3710 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3712 // Confirm items positioned correctly
3713 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3714 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3715 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3716 if (!item) qWarning() << "Item" << i << "not found";
3718 QTRY_COMPARE(item->y(), i*0.5);
3725 void tst_QQuickListView::QTBUG_14821()
3727 QQuickView *canvas = createView();
3729 canvas->setSource(testFileUrl("qtbug14821.qml"));
3730 qApp->processEvents();
3732 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3733 QVERIFY(listview != 0);
3735 QQuickItem *contentItem = listview->contentItem();
3736 QVERIFY(contentItem != 0);
3738 listview->decrementCurrentIndex();
3739 QCOMPARE(listview->currentIndex(), 99);
3741 listview->incrementCurrentIndex();
3742 QCOMPARE(listview->currentIndex(), 0);
3747 void tst_QQuickListView::resizeDelegate()
3749 QQuickView *canvas = createView();
3751 QStringList strings;
3752 for (int i = 0; i < 30; ++i)
3753 strings << QString::number(i);
3754 QStringListModel model(strings);
3756 QQmlContext *ctxt = canvas->rootContext();
3757 ctxt->setContextProperty("testModel", &model);
3759 canvas->setSource(testFileUrl("displaylist.qml"));
3761 qApp->processEvents();
3763 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3764 QVERIFY(listview != 0);
3765 QQuickItem *contentItem = listview->contentItem();
3766 QVERIFY(contentItem != 0);
3767 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3769 QCOMPARE(listview->count(), model.rowCount());
3771 listview->setCurrentIndex(25);
3772 listview->setContentY(0);
3773 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3775 for (int i = 0; i < 16; ++i) {
3776 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3778 QCOMPARE(item->y(), i*20.0);
3781 QCOMPARE(listview->currentItem()->y(), 500.0);
3782 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
3784 canvas->rootObject()->setProperty("delegateHeight", 30);
3785 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3787 for (int i = 0; i < 11; ++i) {
3788 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3790 QTRY_COMPARE(item->y(), i*30.0);
3793 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
3794 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
3796 listview->setCurrentIndex(1);
3797 listview->positionViewAtIndex(25, QQuickListView::Beginning);
3798 listview->positionViewAtIndex(5, QQuickListView::Beginning);
3799 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3801 for (int i = 5; i < 16; ++i) {
3802 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3804 QCOMPARE(item->y(), i*30.0);
3807 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
3808 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
3810 canvas->rootObject()->setProperty("delegateHeight", 20);
3811 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3813 for (int i = 5; i < 11; ++i) {
3814 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3816 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
3819 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
3820 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
3825 void tst_QQuickListView::resizeFirstDelegate()
3827 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
3828 // and other delegates have height > 0
3830 QQuickView *canvas = createView();
3832 // bug only occurs when all items in the model are visible
3834 for (int i = 0; i < 10; i++)
3835 model.addItem("Item" + QString::number(i), "");
3837 QQmlContext *ctxt = canvas->rootContext();
3838 ctxt->setContextProperty("testModel", &model);
3840 TestObject *testObject = new TestObject;
3841 ctxt->setContextProperty("testObject", testObject);
3843 canvas->setSource(testFileUrl("listviewtest.qml"));
3845 qApp->processEvents();
3847 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3848 QVERIFY(listview != 0);
3849 QQuickItem *contentItem = listview->contentItem();
3850 QVERIFY(contentItem != 0);
3851 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3853 QQuickItem *item = 0;
3854 for (int i = 0; i < model.count(); ++i) {
3855 item = findItem<QQuickItem>(contentItem, "wrapper", i);
3857 QCOMPARE(item->y(), i*20.0);
3860 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3863 // check the content y has not jumped up and down
3864 QCOMPARE(listview->contentY(), 0.0);
3865 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
3867 QCOMPARE(spy.count(), 0);
3869 for (int i = 1; i < model.count(); ++i) {
3870 item = findItem<QQuickItem>(contentItem, "wrapper", i);
3872 QTRY_COMPARE(item->y(), (i-1)*20.0);
3876 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
3877 // list if they follow a zero-sized delegate
3879 for (int i = 0; i < 10; i++)
3880 model.addItem("Item" + QString::number(i), "");
3881 QTRY_COMPARE(listview->count(), model.count());
3883 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
3887 listview->setCurrentIndex(19);
3888 qApp->processEvents();
3889 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3891 // items 0-2 should have been deleted
3892 for (int i=0; i<3; i++) {
3893 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
3900 void tst_QQuickListView::QTBUG_16037()
3902 QQuickView *canvas = createView();
3905 canvas->setSource(testFileUrl("qtbug16037.qml"));
3906 qApp->processEvents();
3908 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
3909 QTRY_VERIFY(listview != 0);
3911 QVERIFY(listview->contentHeight() <= 0.0);
3913 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
3915 QTRY_COMPARE(listview->contentHeight(), 80.0);
3920 void tst_QQuickListView::indexAt_itemAt_data()
3922 QTest::addColumn<qreal>("x");
3923 QTest::addColumn<qreal>("y");
3924 QTest::addColumn<int>("index");
3926 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3927 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
3928 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
3929 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
3930 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
3933 void tst_QQuickListView::indexAt_itemAt()
3939 QQuickView *canvas = getView();
3942 for (int i = 0; i < 30; i++)
3943 model.addItem("Item" + QString::number(i), "");
3945 QQmlContext *ctxt = canvas->rootContext();
3946 ctxt->setContextProperty("testModel", &model);
3948 TestObject *testObject = new TestObject;
3949 ctxt->setContextProperty("testObject", testObject);
3951 canvas->setSource(testFileUrl("listviewtest.qml"));
3953 qApp->processEvents();
3955 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3956 QTRY_VERIFY(listview != 0);
3958 QQuickItem *contentItem = listview->contentItem();
3959 QTRY_VERIFY(contentItem != 0);
3960 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3962 QQuickItem *item = 0;
3964 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3967 QCOMPARE(listview->indexAt(x,y), index);
3968 QVERIFY(listview->itemAt(x,y) == item);
3970 releaseView(canvas);
3974 void tst_QQuickListView::incrementalModel()
3976 QQuickView *canvas = createView();
3978 IncrementalModel model;
3979 QQmlContext *ctxt = canvas->rootContext();
3980 ctxt->setContextProperty("testModel", &model);
3982 canvas->setSource(testFileUrl("displaylist.qml"));
3983 qApp->processEvents();
3985 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3986 QTRY_VERIFY(listview != 0);
3988 QQuickItem *contentItem = listview->contentItem();
3989 QTRY_VERIFY(contentItem != 0);
3991 QTRY_COMPARE(listview->count(), 20);
3993 listview->positionViewAtIndex(10, QQuickListView::Beginning);
3995 QTRY_COMPARE(listview->count(), 25);
4000 void tst_QQuickListView::onAdd()
4002 QFETCH(int, initialItemCount);
4003 QFETCH(int, itemsToAdd);
4005 const int delegateHeight = 10;
4008 // these initial items should not trigger ListView.onAdd
4009 for (int i=0; i<initialItemCount; i++)
4010 model.addItem("dummy value", "dummy value");
4012 QQuickView *canvas = createView();
4013 canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4014 QQmlContext *ctxt = canvas->rootContext();
4015 ctxt->setContextProperty("testModel", &model);
4016 ctxt->setContextProperty("delegateHeight", delegateHeight);
4017 canvas->setSource(testFileUrl("attachedSignals.qml"));
4019 QObject *object = canvas->rootObject();
4020 object->setProperty("width", canvas->width());
4021 object->setProperty("height", canvas->height());
4022 qApp->processEvents();
4024 QList<QPair<QString, QString> > items;
4025 for (int i=0; i<itemsToAdd; i++)
4026 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4027 model.addItems(items);
4028 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4030 QVariantList result = object->property("addedDelegates").toList();
4031 QCOMPARE(result.count(), items.count());
4032 for (int i=0; i<items.count(); i++)
4033 QCOMPARE(result[i].toString(), items[i].first);
4038 void tst_QQuickListView::onAdd_data()
4040 QTest::addColumn<int>("initialItemCount");
4041 QTest::addColumn<int>("itemsToAdd");
4043 QTest::newRow("0, add 1") << 0 << 1;
4044 QTest::newRow("0, add 2") << 0 << 2;
4045 QTest::newRow("0, add 10") << 0 << 10;
4047 QTest::newRow("1, add 1") << 1 << 1;
4048 QTest::newRow("1, add 2") << 1 << 2;
4049 QTest::newRow("1, add 10") << 1 << 10;
4051 QTest::newRow("5, add 1") << 5 << 1;
4052 QTest::newRow("5, add 2") << 5 << 2;
4053 QTest::newRow("5, add 10") << 5 << 10;
4056 void tst_QQuickListView::onRemove()
4058 QFETCH(int, initialItemCount);
4059 QFETCH(int, indexToRemove);
4060 QFETCH(int, removeCount);
4062 const int delegateHeight = 10;
4064 for (int i=0; i<initialItemCount; i++)
4065 model.addItem(QString("value %1").arg(i), "dummy value");
4067 QQuickView *canvas = getView();
4068 QQmlContext *ctxt = canvas->rootContext();
4069 ctxt->setContextProperty("testModel", &model);
4070 ctxt->setContextProperty("delegateHeight", delegateHeight);
4071 canvas->setSource(testFileUrl("attachedSignals.qml"));
4073 QObject *object = canvas->rootObject();
4075 model.removeItems(indexToRemove, removeCount);
4076 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
4078 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4080 releaseView(canvas);
4083 void tst_QQuickListView::onRemove_data()
4085 QTest::addColumn<int>("initialItemCount");
4086 QTest::addColumn<int>("indexToRemove");
4087 QTest::addColumn<int>("removeCount");
4089 QTest::newRow("remove first") << 1 << 0 << 1;
4090 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4091 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4092 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4094 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4095 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4096 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4097 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4098 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4099 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4101 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4102 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4103 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4106 void tst_QQuickListView::rightToLeft()
4108 QQuickView *canvas = createView();
4109 canvas->setGeometry(0,0,640,320);
4110 canvas->setSource(testFileUrl("rightToLeft.qml"));
4112 qApp->processEvents();
4114 QVERIFY(canvas->rootObject() != 0);
4115 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
4116 QTRY_VERIFY(listview != 0);
4118 QQuickItem *contentItem = listview->contentItem();
4119 QTRY_VERIFY(contentItem != 0);
4121 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4123 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4124 QTRY_VERIFY(model != 0);
4126 QTRY_VERIFY(model->count() == 3);
4127 QTRY_COMPARE(listview->currentIndex(), 0);
4129 // initial position at first item, right edge aligned
4130 QCOMPARE(listview->contentX(), -640.);
4132 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4134 QTRY_COMPARE(item->x(), -100.0);
4135 QCOMPARE(item->height(), listview->height());
4137 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4139 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4141 listview->setCurrentIndex(2);
4143 item = findItem<QQuickItem>(contentItem, "item3");
4145 QTRY_COMPARE(item->x(), -540.0);
4147 text = findItem<QQuickText>(contentItem, "text3");
4149 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4151 QCOMPARE(listview->contentX(), -640.);
4153 // Ensure resizing maintains position relative to right edge
4154 qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
4155 QTRY_COMPARE(listview->contentX(), -600.);
4160 void tst_QQuickListView::test_mirroring()
4162 QQuickView *canvasA = createView();
4163 canvasA->setSource(testFileUrl("rightToLeft.qml"));
4164 QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
4165 QTRY_VERIFY(listviewA != 0);
4167 QQuickView *canvasB = createView();
4168 canvasB->setSource(testFileUrl("rightToLeft.qml"));
4169 QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
4170 QTRY_VERIFY(listviewA != 0);
4171 qApp->processEvents();
4173 QList<QString> objectNames;
4174 objectNames << "item1" << "item2"; // << "item3"
4176 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4177 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4178 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4181 foreach (const QString objectName, objectNames)
4182 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4184 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4185 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4188 foreach (const QString objectName, objectNames)
4189 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4191 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4192 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4193 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4195 // LTR != LTR+mirror
4196 foreach (const QString objectName, objectNames)
4197 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4199 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4201 // RTL == LTR+mirror
4202 foreach (const QString objectName, objectNames)
4203 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4205 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4207 // RTL != RTL+mirror
4208 foreach (const QString objectName, objectNames)
4209 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4211 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4213 // LTR == RTL+mirror
4214 foreach (const QString objectName, objectNames)
4215 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4221 void tst_QQuickListView::margins()
4223 QQuickView *canvas = createView();
4226 for (int i = 0; i < 50; i++)
4227 model.addItem("Item" + QString::number(i), "");
4229 QQmlContext *ctxt = canvas->rootContext();
4230 ctxt->setContextProperty("testModel", &model);
4232 canvas->setSource(testFileUrl("margins.qml"));
4234 qApp->processEvents();
4236 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4237 QTRY_VERIFY(listview != 0);
4238 QQuickItem *contentItem = listview->contentItem();
4239 QTRY_VERIFY(contentItem != 0);
4240 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4242 QCOMPARE(listview->contentY(), -30.);
4243 QCOMPARE(listview->yOrigin(), 0.);
4246 listview->positionViewAtEnd();
4247 qreal pos = listview->contentY();
4248 listview->setContentY(pos + 80);
4249 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4250 listview->returnToBounds();
4251 QTRY_COMPARE(listview->contentY(), pos + 50);
4253 // remove item before visible and check that top margin is maintained
4254 // and yOrigin is updated
4255 listview->setContentY(100);
4256 model.removeItem(1);
4257 QTRY_COMPARE(listview->count(), model.count());
4258 listview->setContentY(-50);
4259 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4260 listview->returnToBounds();
4261 QCOMPARE(listview->yOrigin(), 20.);
4262 QTRY_COMPARE(listview->contentY(), -10.);
4264 // reduce top margin
4265 listview->setTopMargin(20);
4266 QCOMPARE(listview->yOrigin(), 20.);
4267 QTRY_COMPARE(listview->contentY(), 0.);
4270 listview->positionViewAtEnd();
4271 pos = listview->contentY();
4272 listview->setContentY(pos + 80);
4273 listview->returnToBounds();
4274 QTRY_COMPARE(listview->contentY(), pos + 50);
4276 // reduce bottom margin
4277 pos = listview->contentY();
4278 listview->setBottomMargin(40);
4279 QCOMPARE(listview->yOrigin(), 20.);
4280 QTRY_COMPARE(listview->contentY(), pos-10);
4286 void tst_QQuickListView::marginsResize()
4288 QFETCH(QQuickListView::Orientation, orientation);
4289 QFETCH(Qt::LayoutDirection, layoutDirection);
4290 QFETCH(qreal, start);
4293 QQuickView *canvas = getView();
4295 canvas->setSource(testFileUrl("margins2.qml"));
4297 qApp->processEvents();
4299 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4300 QTRY_VERIFY(listview != 0);
4302 listview->setOrientation(orientation);
4303 listview->setLayoutDirection(layoutDirection);
4304 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4306 // view is resized after componentCompleted - top margin should still be visible
4307 if (orientation == QQuickListView::Vertical)
4308 QCOMPARE(listview->contentY(), start);
4310 QCOMPARE(listview->contentX(), start);
4312 // move to last index and ensure bottom margin is visible.
4313 listview->setCurrentIndex(19);
4314 if (orientation == QQuickListView::Vertical)
4315 QTRY_COMPARE(listview->contentY(), end);
4317 QTRY_COMPARE(listview->contentX(), end);
4319 // back to top - top margin should be visible.
4320 listview->setCurrentIndex(0);
4321 if (orientation == QQuickListView::Vertical)
4322 QTRY_COMPARE(listview->contentY(), start);
4324 QTRY_COMPARE(listview->contentX(), start);
4326 releaseView(canvas);
4329 void tst_QQuickListView::marginsResize_data()
4331 QTest::addColumn<QQuickListView::Orientation>("orientation");
4332 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4333 QTest::addColumn<qreal>("start");
4334 QTest::addColumn<qreal>("end");
4336 QTest::newRow("vertical") << QQuickListView::Vertical << Qt::LeftToRight << -20.0 << 1020.0;
4337 QTest::newRow("horizontal") << QQuickListView::Horizontal << Qt::LeftToRight << -20.0 << 1020.0;
4338 QTest::newRow("horizontal, rtl") << QQuickListView::Horizontal << Qt::RightToLeft << -180.0 << -1220.0;
4341 void tst_QQuickListView::snapToItem_data()
4343 QTest::addColumn<QQuickListView::Orientation>("orientation");
4344 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4345 QTest::addColumn<int>("highlightRangeMode");
4346 QTest::addColumn<QPoint>("flickStart");
4347 QTest::addColumn<QPoint>("flickEnd");
4348 QTest::addColumn<qreal>("snapAlignment");
4349 QTest::addColumn<qreal>("endExtent");
4350 QTest::addColumn<qreal>("startExtent");
4352 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4353 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4355 QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4356 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4358 QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4359 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4361 QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4362 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4364 QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4365 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4367 QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4368 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4371 void tst_QQuickListView::snapToItem()
4373 QFETCH(QQuickListView::Orientation, orientation);
4374 QFETCH(Qt::LayoutDirection, layoutDirection);
4375 QFETCH(int, highlightRangeMode);
4376 QFETCH(QPoint, flickStart);
4377 QFETCH(QPoint, flickEnd);
4378 QFETCH(qreal, snapAlignment);
4379 QFETCH(qreal, endExtent);
4380 QFETCH(qreal, startExtent);
4382 QQuickView *canvas = getView();
4384 canvas->setSource(testFileUrl("snapToItem.qml"));
4386 qApp->processEvents();
4388 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4389 QTRY_VERIFY(listview != 0);
4391 listview->setOrientation(orientation);
4392 listview->setLayoutDirection(layoutDirection);
4393 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4394 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4396 QQuickItem *contentItem = listview->contentItem();
4397 QTRY_VERIFY(contentItem != 0);
4399 // confirm that a flick hits an item boundary
4400 flick(canvas, flickStart, flickEnd, 180);
4401 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4402 if (orientation == QQuickListView::Vertical)
4403 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4405 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4409 flick(canvas, flickStart, flickEnd, 180);
4410 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4411 } while (orientation == QQuickListView::Vertical
4412 ? !listview->isAtYEnd()
4413 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4415 if (orientation == QQuickListView::Vertical)
4416 QCOMPARE(listview->contentY(), endExtent);
4418 QCOMPARE(listview->contentX(), endExtent);
4422 flick(canvas, flickEnd, flickStart, 180);
4423 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4424 } while (orientation == QQuickListView::Vertical
4425 ? !listview->isAtYBeginning()
4426 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4428 if (orientation == QQuickListView::Vertical)
4429 QCOMPARE(listview->contentY(), startExtent);
4431 QCOMPARE(listview->contentX(), startExtent);
4433 releaseView(canvas);
4436 void tst_QQuickListView::qListModelInterface_items()
4438 items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4441 void tst_QQuickListView::qListModelInterface_package_items()
4443 items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4446 void tst_QQuickListView::qAbstractItemModel_items()
4448 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
4451 void tst_QQuickListView::qListModelInterface_changed()
4453 changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4456 void tst_QQuickListView::qListModelInterface_package_changed()
4458 changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4461 void tst_QQuickListView::qAbstractItemModel_changed()
4463 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
4466 void tst_QQuickListView::qListModelInterface_inserted()
4468 inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
4471 void tst_QQuickListView::qListModelInterface_package_inserted()
4473 inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4476 void tst_QQuickListView::qListModelInterface_inserted_more()
4478 inserted_more<QmlListModel>();
4481 void tst_QQuickListView::qListModelInterface_inserted_more_data()
4483 inserted_more_data();
4486 void tst_QQuickListView::qAbstractItemModel_inserted()
4488 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
4491 void tst_QQuickListView::qAbstractItemModel_inserted_more()
4493 inserted_more<QaimModel>();
4496 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
4498 inserted_more_data();
4501 void tst_QQuickListView::qListModelInterface_removed()
4503 removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4504 removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
4507 void tst_QQuickListView::qListModelInterface_removed_more()
4509 removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
4512 void tst_QQuickListView::qListModelInterface_removed_more_data()
4514 removed_more_data();
4517 void tst_QQuickListView::qListModelInterface_package_removed()
4519 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
4520 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4523 void tst_QQuickListView::qAbstractItemModel_removed()
4525 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
4526 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
4529 void tst_QQuickListView::qAbstractItemModel_removed_more()
4531 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
4534 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
4536 removed_more_data();
4539 void tst_QQuickListView::qListModelInterface_moved()
4541 moved<QmlListModel>(testFileUrl("listviewtest.qml"));
4544 void tst_QQuickListView::qListModelInterface_moved_data()
4549 void tst_QQuickListView::qListModelInterface_package_moved()
4551 moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4554 void tst_QQuickListView::qListModelInterface_package_moved_data()
4559 void tst_QQuickListView::qAbstractItemModel_moved()
4561 moved<QaimModel>(testFileUrl("listviewtest.qml"));
4564 void tst_QQuickListView::qAbstractItemModel_moved_data()
4569 void tst_QQuickListView::qListModelInterface_clear()
4571 clear<QmlListModel>(testFileUrl("listviewtest.qml"));
4574 void tst_QQuickListView::qListModelInterface_package_clear()
4576 clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4579 void tst_QQuickListView::qAbstractItemModel_clear()
4581 clear<QaimModel>(testFileUrl("listviewtest.qml"));
4584 void tst_QQuickListView::qListModelInterface_sections()
4586 sections<QmlListModel>(testFileUrl("listview-sections.qml"));
4589 void tst_QQuickListView::qListModelInterface_package_sections()
4591 sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
4594 void tst_QQuickListView::qAbstractItemModel_sections()
4596 sections<QaimModel>(testFileUrl("listview-sections.qml"));
4599 void tst_QQuickListView::creationContext()
4602 canvas.setGeometry(0,0,240,320);
4603 canvas.setSource(testFileUrl("creationContext.qml"));
4604 qApp->processEvents();
4606 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4608 QVERIFY(rootItem->property("count").toInt() > 0);
4611 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
4612 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4613 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4614 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4615 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4616 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4617 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
4618 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4621 void tst_QQuickListView::QTBUG_21742()
4624 canvas.setGeometry(0,0,200,200);
4625 canvas.setSource(testFileUrl("qtbug-21742.qml"));
4626 qApp->processEvents();
4628 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4630 QCOMPARE(rootItem->property("count").toInt(), 1);
4633 void tst_QQuickListView::asynchronous()
4635 QQuickView *canvas = createView();
4637 QQmlIncubationController controller;
4638 canvas->engine()->setIncubationController(&controller);
4640 canvas->setSource(testFileUrl("asyncloader.qml"));
4642 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
4643 QVERIFY(rootObject);
4645 QQuickListView *listview = 0;
4648 controller.incubateWhile(&b);
4649 listview = rootObject->findChild<QQuickListView*>("view");
4652 // items will be created one at a time
4653 for (int i = 0; i < 8; ++i) {
4654 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
4655 QQuickItem *item = 0;
4658 controller.incubateWhile(&b);
4659 item = findItem<QQuickItem>(listview, "wrapper", i);
4665 controller.incubateWhile(&b);
4668 // verify positioning
4669 QQuickItem *contentItem = listview->contentItem();
4670 for (int i = 0; i < 8; ++i) {
4671 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4672 QTRY_COMPARE(item->y(), i*50.0);
4678 void tst_QQuickListView::snapOneItem_data()
4680 QTest::addColumn<QQuickListView::Orientation>("orientation");
4681 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4682 QTest::addColumn<int>("highlightRangeMode");
4683 QTest::addColumn<QPoint>("flickStart");
4684 QTest::addColumn<QPoint>("flickEnd");
4685 QTest::addColumn<qreal>("snapAlignment");
4686 QTest::addColumn<qreal>("endExtent");
4687 QTest::addColumn<qreal>("startExtent");
4689 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4690 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
4692 QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4693 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
4695 QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4696 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
4698 QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4699 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
4701 QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4702 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
4704 QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4705 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
4708 void tst_QQuickListView::snapOneItem()
4710 QFETCH(QQuickListView::Orientation, orientation);
4711 QFETCH(Qt::LayoutDirection, layoutDirection);
4712 QFETCH(int, highlightRangeMode);
4713 QFETCH(QPoint, flickStart);
4714 QFETCH(QPoint, flickEnd);
4715 QFETCH(qreal, snapAlignment);
4716 QFETCH(qreal, endExtent);
4717 QFETCH(qreal, startExtent);
4720 // This test seems to be unreliable - different test data fails on different runs
4721 QSKIP("QTBUG-24338");
4724 QQuickView *canvas = getView();
4726 canvas->setSource(testFileUrl("snapOneItem.qml"));
4728 qApp->processEvents();
4730 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4731 QTRY_VERIFY(listview != 0);
4733 listview->setOrientation(orientation);
4734 listview->setLayoutDirection(layoutDirection);
4735 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4736 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4738 QQuickItem *contentItem = listview->contentItem();
4739 QTRY_VERIFY(contentItem != 0);
4741 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
4743 // confirm that a flick hits the next item boundary
4744 flick(canvas, flickStart, flickEnd, 180);
4745 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4746 if (orientation == QQuickListView::Vertical)
4747 QCOMPARE(listview->contentY(), snapAlignment);
4749 QCOMPARE(listview->contentX(), snapAlignment);
4751 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4752 QCOMPARE(listview->currentIndex(), 1);
4753 QCOMPARE(currentIndexSpy.count(), 1);
4758 flick(canvas, flickStart, flickEnd, 180);
4759 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4760 } while (orientation == QQuickListView::Vertical
4761 ? !listview->isAtYEnd()
4762 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4764 if (orientation == QQuickListView::Vertical)
4765 QCOMPARE(listview->contentY(), endExtent);
4767 QCOMPARE(listview->contentX(), endExtent);
4769 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4770 QCOMPARE(listview->currentIndex(), 3);
4771 QCOMPARE(currentIndexSpy.count(), 3);
4776 flick(canvas, flickEnd, flickStart, 180);
4777 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4778 } while (orientation == QQuickListView::Vertical
4779 ? !listview->isAtYBeginning()
4780 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4782 if (orientation == QQuickListView::Vertical)
4783 QCOMPARE(listview->contentY(), startExtent);
4785 QCOMPARE(listview->contentX(), startExtent);
4787 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4788 QCOMPARE(listview->currentIndex(), 0);
4789 QCOMPARE(currentIndexSpy.count(), 6);
4792 releaseView(canvas);
4795 void tst_QQuickListView::unrequestedVisibility()
4798 for (int i = 0; i < 30; i++)
4799 model.addItem("Item" + QString::number(i), QString::number(i));
4801 QQuickView *canvas = new QQuickView(0);
4802 canvas->setGeometry(0,0,240,320);
4804 QQmlContext *ctxt = canvas->rootContext();
4805 ctxt->setContextProperty("testModel", &model);
4806 ctxt->setContextProperty("testWrap", QVariant(false));
4808 canvas->setSource(testFileUrl("unrequestedItems.qml"));
4810 qApp->processEvents();
4812 QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
4813 QTRY_VERIFY(leftview != 0);
4815 QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
4816 QTRY_VERIFY(rightview != 0);
4818 QQuickItem *leftContent = leftview->contentItem();
4819 QTRY_VERIFY(leftContent != 0);
4821 QQuickItem *rightContent = rightview->contentItem();
4822 QTRY_VERIFY(rightContent != 0);
4824 rightview->setCurrentIndex(20);
4826 QTRY_COMPARE(leftview->contentY(), 0.0);
4827 QTRY_COMPARE(rightview->contentY(), 100.0);
4831 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4832 QCOMPARE(item->isVisible(), true);
4833 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4834 QCOMPARE(item->isVisible(), false);
4836 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
4837 QCOMPARE(item->isVisible(), false);
4838 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
4839 QCOMPARE(item->isVisible(), true);
4841 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
4842 QCOMPARE(item->isVisible(), true);
4843 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
4844 QCOMPARE(item->isVisible(), false);
4845 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
4846 QCOMPARE(item->isVisible(), false);
4847 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
4848 QCOMPARE(item->isVisible(), true);
4850 rightview->setCurrentIndex(0);
4852 QTRY_COMPARE(leftview->contentY(), 0.0);
4853 QTRY_COMPARE(rightview->contentY(), 0.0);
4855 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4856 QCOMPARE(item->isVisible(), true);
4857 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4858 QTRY_COMPARE(item->isVisible(), true);
4860 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
4861 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
4863 leftview->setCurrentIndex(20);
4865 QTRY_COMPARE(leftview->contentY(), 100.0);
4866 QTRY_COMPARE(rightview->contentY(), 0.0);
4868 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4869 QTRY_COMPARE(item->isVisible(), false);
4870 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4871 QCOMPARE(item->isVisible(), true);
4873 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
4874 QCOMPARE(item->isVisible(), true);
4875 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
4876 QCOMPARE(item->isVisible(), false);
4878 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
4879 QCOMPARE(item->isVisible(), false);
4880 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4881 QCOMPARE(item->isVisible(), true);
4882 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4883 QCOMPARE(item->isVisible(), true);
4884 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4885 QCOMPARE(item->isVisible(), false);
4887 model.moveItems(19, 1, 1);
4888 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4890 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4891 QCOMPARE(item->isVisible(), false);
4892 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4893 QCOMPARE(item->isVisible(), true);
4895 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
4896 QCOMPARE(item->isVisible(), true);
4897 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
4898 QCOMPARE(item->isVisible(), false);
4900 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4901 QCOMPARE(item->isVisible(), false);
4902 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4903 QCOMPARE(item->isVisible(), true);
4904 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4905 QCOMPARE(item->isVisible(), true);
4906 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4907 QCOMPARE(item->isVisible(), false);
4909 model.moveItems(3, 4, 1);
4910 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4912 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4913 QCOMPARE(item->isVisible(), false);
4914 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4915 QCOMPARE(item->isVisible(), true);
4916 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4917 QCOMPARE(item->isVisible(), true);
4918 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4919 QCOMPARE(item->isVisible(), false);
4921 model.moveItems(4, 3, 1);
4922 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4924 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4925 QCOMPARE(item->isVisible(), false);
4926 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4927 QCOMPARE(item->isVisible(), true);
4928 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4929 QCOMPARE(item->isVisible(), true);
4930 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4931 QCOMPARE(item->isVisible(), false);
4933 model.moveItems(16, 17, 1);
4934 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4936 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4937 QCOMPARE(item->isVisible(), false);
4938 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4939 QCOMPARE(item->isVisible(), true);
4940 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4941 QCOMPARE(item->isVisible(), true);
4942 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4943 QCOMPARE(item->isVisible(), false);
4945 model.moveItems(17, 16, 1);
4946 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4948 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4949 QCOMPARE(item->isVisible(), false);
4950 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4951 QCOMPARE(item->isVisible(), true);
4952 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4953 QCOMPARE(item->isVisible(), true);
4954 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4955 QCOMPARE(item->isVisible(), false);
4960 void tst_QQuickListView::populateTransitions()
4962 QFETCH(bool, staticallyPopulate);
4963 QFETCH(bool, dynamicallyPopulate);
4964 QFETCH(bool, usePopulateTransition);
4966 QPointF transitionFrom(-50, -50);
4967 QPointF transitionVia(100, 100);
4968 QaimModel model_transitionFrom;
4969 QaimModel model_transitionVia;
4972 if (staticallyPopulate) {
4973 for (int i = 0; i < 30; i++)
4974 model.addItem("item" + QString::number(i), "");
4977 QQuickView *canvas = getView();
4978 canvas->rootContext()->setContextProperty("testModel", &model);
4979 canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
4980 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4981 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4982 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4983 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4984 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4985 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4986 canvas->setSource(testFileUrl("populateTransitions.qml"));
4988 QTest::qWaitForWindowShown(canvas);
4990 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4992 QQuickItem *contentItem = listview->contentItem();
4993 QVERIFY(contentItem);
4995 if (staticallyPopulate || dynamicallyPopulate) {
4996 // check the populate transition is run
4997 if (usePopulateTransition) {
4998 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 17);
5000 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5001 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5003 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5005 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5008 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5009 if (usePopulateTransition)
5010 QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt());
5011 for (int i=0; i < model.count() && i < itemCount; ++i) {
5012 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5013 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5014 QTRY_COMPARE(item->x(), 0.0);
5015 QTRY_COMPARE(item->y(), i*20.0);
5016 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5018 QTRY_COMPARE(name->text(), model.name(i));
5021 // add an item and check this is done with add trantion, not populate
5022 model.insertItem(0, "another item", "");
5023 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5024 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(),
5025 (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 17 : 0);
5028 canvas->rootContext()->setContextProperty("testModel", QVariant());
5029 QTRY_COMPARE(listview->count(), 0);
5030 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5031 listview->setProperty("countPopulateTransitions", 0);
5032 listview->setProperty("countAddTransitions", 0);
5034 // set to a valid model and check populate transition is run a second time
5036 for (int i = 0; i < 30; i++)
5037 model.addItem("item" + QString::number(i), "");
5038 canvas->rootContext()->setContextProperty("testModel", &model);
5039 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 17 : 0);
5040 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5042 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5043 if (usePopulateTransition)
5044 QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt());
5045 for (int i=0; i < model.count() && i < itemCount; ++i) {
5046 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5047 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5048 QTRY_COMPARE(item->x(), 0.0);
5049 QTRY_COMPARE(item->y(), i*20.0);
5050 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5052 QTRY_COMPARE(name->text(), model.name(i));
5055 // reset model and check populate transition is run again
5056 listview->setProperty("countPopulateTransitions", 0);
5057 listview->setProperty("countAddTransitions", 0);
5059 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 17 : 0);
5060 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5062 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5063 if (usePopulateTransition)
5064 QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt());
5065 for (int i=0; i < model.count() && i < itemCount; ++i) {
5066 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5067 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5068 QTRY_COMPARE(item->x(), 0.0);
5069 QTRY_COMPARE(item->y(), i*20.0);
5070 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5072 QTRY_COMPARE(name->text(), model.name(i));
5075 releaseView(canvas);
5078 void tst_QQuickListView::populateTransitions_data()
5080 QTest::addColumn<bool>("staticallyPopulate");
5081 QTest::addColumn<bool>("dynamicallyPopulate");
5082 QTest::addColumn<bool>("usePopulateTransition");
5084 QTest::newRow("static") << true << false << true;
5085 QTest::newRow("static, no populate") << true << false << false;
5087 QTest::newRow("dynamic") << false << true << true;
5088 QTest::newRow("dynamic, no populate") << false << true << false;
5090 QTest::newRow("empty to start with") << false << false << true;
5091 QTest::newRow("empty to start with, no populate") << false << false << false;
5094 void tst_QQuickListView::addTransitions()
5096 QFETCH(int, initialItemCount);
5097 QFETCH(bool, shouldAnimateTargets);
5098 QFETCH(qreal, contentY);
5099 QFETCH(int, insertionIndex);
5100 QFETCH(int, insertionCount);
5101 QFETCH(ListRange, expectedDisplacedIndexes);
5103 // added items should start here
5104 QPointF targetItems_transitionFrom(-50, -50);
5106 // displaced items should pass through this point
5107 QPointF displacedItems_transitionVia(100, 100);
5110 for (int i = 0; i < initialItemCount; i++)
5111 model.addItem("Original item" + QString::number(i), "");
5112 QaimModel model_targetItems_transitionFrom;
5113 QaimModel model_displacedItems_transitionVia;
5115 QQuickView *canvas = getView();
5116 QQmlContext *ctxt = canvas->rootContext();
5117 TestObject *testObject = new TestObject;
5118 ctxt->setContextProperty("testModel", &model);
5119 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5120 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5121 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5122 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5123 ctxt->setContextProperty("testObject", testObject);
5124 canvas->setSource(testFileUrl("addTransitions.qml"));
5126 QTest::qWaitForWindowShown(canvas);
5128 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5129 QTRY_VERIFY(listview != 0);
5130 QQuickItem *contentItem = listview->contentItem();
5131 QVERIFY(contentItem != 0);
5132 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5134 if (contentY != 0) {
5135 listview->setContentY(contentY);
5136 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5139 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5141 // only target items that will become visible should be animated
5142 QList<QPair<QString, QString> > newData;
5143 QList<QPair<QString, QString> > expectedTargetData;
5144 QList<int> targetIndexes;
5145 if (shouldAnimateTargets) {
5146 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5147 newData << qMakePair(QString("New item %1").arg(i), QString(""));
5149 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
5150 expectedTargetData << newData.last();
5154 QVERIFY(expectedTargetData.count() > 0);
5158 if (!newData.isEmpty()) {
5159 model.insertItems(insertionIndex, newData);
5160 QTRY_COMPARE(model.count(), listview->count());
5163 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5165 if (shouldAnimateTargets) {
5166 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5167 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5168 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5170 // check the target and displaced items were animated
5171 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5172 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5174 // check attached properties
5175 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5176 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5177 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5178 if (expectedDisplacedIndexes.isValid()) {
5179 // adjust expectedDisplacedIndexes to their final values after the move
5180 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5181 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5182 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5183 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5187 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5188 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5191 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5192 int firstVisibleIndex = -1;
5193 int itemCount = items.count();
5194 for (int i=0; i<items.count(); i++) {
5195 if (items[i]->y() >= contentY) {
5196 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5197 firstVisibleIndex = e.evaluate().toInt();
5201 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5203 // verify all items moved to the correct final positions
5204 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5205 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5206 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5207 QTRY_COMPARE(item->y(), i*20.0);
5208 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5210 QTRY_COMPARE(name->text(), model.name(i));
5213 releaseView(canvas);
5217 void tst_QQuickListView::addTransitions_data()
5219 QTest::addColumn<int>("initialItemCount");
5220 QTest::addColumn<qreal>("contentY");
5221 QTest::addColumn<bool>("shouldAnimateTargets");
5222 QTest::addColumn<int>("insertionIndex");
5223 QTest::addColumn<int>("insertionCount");
5224 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5226 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5227 QTest::newRow("insert 1, just before start")
5228 << 30 << 20.0 << false
5229 << 0 << 1 << ListRange();
5230 QTest::newRow("insert 1, way before start")
5231 << 30 << 20.0 << false
5232 << 0 << 1 << ListRange();
5233 QTest::newRow("insert multiple, just before start")
5234 << 30 << 100.0 << false
5235 << 0 << 3 << ListRange();
5236 QTest::newRow("insert multiple, way before start")
5237 << 30 << 100.0 << false
5238 << 0 << 3 << ListRange();
5240 QTest::newRow("insert 1 at start")
5241 << 30 << 0.0 << true
5242 << 0 << 1 << ListRange(0, 15);
5243 QTest::newRow("insert multiple at start")
5244 << 30 << 0.0 << true
5245 << 0 << 3 << ListRange(0, 15);
5246 QTest::newRow("insert 1 at start, content y not 0")
5247 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5248 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5249 QTest::newRow("insert multiple at start, content y not 0")
5250 << 30 << 40.0 << true // first visible is index 2
5251 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5253 QTest::newRow("insert 1 at start, to empty list")
5255 << 0 << 1 << ListRange();
5256 QTest::newRow("insert multiple at start, to empty list")
5258 << 0 << 3 << ListRange();
5260 QTest::newRow("insert 1 at middle")
5261 << 30 << 0.0 << true
5262 << 5 << 1 << ListRange(5, 15);
5263 QTest::newRow("insert multiple at middle")
5264 << 30 << 0.0 << true
5265 << 5 << 3 << ListRange(5, 15);
5267 QTest::newRow("insert 1 at bottom")
5268 << 30 << 0.0 << true
5269 << 15 << 1 << ListRange(15, 15);
5270 QTest::newRow("insert multiple at bottom")
5271 << 30 << 0.0 << true
5272 << 15 << 3 << ListRange(15, 15);
5273 QTest::newRow("insert 1 at bottom, content y not 0")
5274 << 30 << 20.0 * 3 << true
5275 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5276 QTest::newRow("insert multiple at bottom, content y not 0")
5277 << 30 << 20.0 * 3 << true
5278 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5280 // items added after the last visible will not be animated in, since they
5281 // do not appear in the final view
5282 QTest::newRow("insert 1 after end")
5283 << 30 << 0.0 << false
5284 << 17 << 1 << ListRange();
5285 QTest::newRow("insert multiple after end")
5286 << 30 << 0.0 << false
5287 << 17 << 3 << ListRange();
5290 void tst_QQuickListView::moveTransitions()
5292 QFETCH(int, initialItemCount);
5293 QFETCH(qreal, contentY);
5294 QFETCH(qreal, itemsOffsetAfterMove);
5295 QFETCH(int, moveFrom);
5296 QFETCH(int, moveTo);
5297 QFETCH(int, moveCount);
5298 QFETCH(ListRange, expectedDisplacedIndexes);
5300 // target and displaced items should pass through these points
5301 QPointF targetItems_transitionVia(-50, 50);
5302 QPointF displacedItems_transitionVia(100, 100);
5305 for (int i = 0; i < initialItemCount; i++)
5306 model.addItem("Original item" + QString::number(i), "");
5307 QaimModel model_targetItems_transitionVia;
5308 QaimModel model_displacedItems_transitionVia;
5310 QQuickView *canvas = getView();
5311 QQmlContext *ctxt = canvas->rootContext();
5312 TestObject *testObject = new TestObject;
5313 ctxt->setContextProperty("testModel", &model);
5314 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5315 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5316 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5317 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5318 ctxt->setContextProperty("testObject", testObject);
5319 canvas->setSource(testFileUrl("moveTransitions.qml"));
5321 QTest::qWaitForWindowShown(canvas);
5323 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5324 QTRY_VERIFY(listview != 0);
5325 QQuickItem *contentItem = listview->contentItem();
5326 QVERIFY(contentItem != 0);
5329 if (contentY != 0) {
5330 listview->setContentY(contentY);
5331 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5334 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5336 // Items moving to *or* from visible positions should be animated.
5337 // Otherwise, they should not be animated.
5338 QList<QPair<QString, QString> > expectedTargetData;
5339 QList<int> targetIndexes;
5340 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5341 int toIndex = moveTo + (i - moveFrom);
5342 if (i <= (contentY + listview->height()) / 20
5343 || toIndex < (contentY + listview->height()) / 20) {
5344 expectedTargetData << qMakePair(model.name(i), model.number(i));
5348 // ViewTransition.index provides the indices that items are moving to, not from
5349 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5352 model.moveItems(moveFrom, moveTo, moveCount);
5354 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5355 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5356 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5358 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5360 // check the target and displaced items were animated
5361 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5362 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5364 // check attached properties
5365 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5366 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5367 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5368 if (expectedDisplacedIndexes.isValid()) {
5369 // adjust expectedDisplacedIndexes to their final values after the move
5370 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5371 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5372 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5373 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5376 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5377 int firstVisibleIndex = -1;
5378 for (int i=0; i<items.count(); i++) {
5379 if (items[i]->y() >= contentY) {
5380 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5381 firstVisibleIndex = e.evaluate().toInt();
5385 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5387 // verify all items moved to the correct final positions
5388 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5389 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5390 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5391 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5392 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
5393 name = findItem<QQuickText>(contentItem, "textName", i);
5395 QTRY_COMPARE(name->text(), model.name(i));
5398 releaseView(canvas);
5402 void tst_QQuickListView::moveTransitions_data()
5404 QTest::addColumn<int>("initialItemCount");
5405 QTest::addColumn<qreal>("contentY");
5406 QTest::addColumn<qreal>("itemsOffsetAfterMove");
5407 QTest::addColumn<int>("moveFrom");
5408 QTest::addColumn<int>("moveTo");
5409 QTest::addColumn<int>("moveCount");
5410 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5412 // when removing from above the visible, all items shift down depending on how many
5413 // items have been removed from above the visible
5414 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
5415 << 1 << 10 << 1 << ListRange(11, 15+4);
5416 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
5417 << 0 << 10 << 1 << ListRange(11, 15+4);
5418 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
5419 << 1 << 10 << 2 << ListRange(12, 15+4);
5420 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
5421 << 0 << 10 << 3 << ListRange(13, 15+4);
5422 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
5423 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
5424 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
5425 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
5427 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
5428 << 1 << 10 << 1 << ListRange(2, 10);
5429 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
5430 << 0 << 10 << 1 << ListRange(1, 10);
5431 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5432 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
5433 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
5434 << 10 << 15 << 1 << ListRange(11, 15);
5435 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
5436 << 0 << 15 << 1 << ListRange(1, 15);
5438 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
5439 << 1 << 10 << 3 << ListRange(4, 12);
5440 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
5441 << 0 << 10 << 3 << ListRange(3, 12);
5442 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5443 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
5444 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
5445 << 5 << 13 << 3 << ListRange(8, 15);
5446 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
5447 << 0 << 13 << 3 << ListRange(3, 15);
5449 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
5450 << 10 << 1 << 1 << ListRange(1, 9);
5451 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
5452 << 10 << 0 << 1 << ListRange(0, 9);
5453 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5454 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5455 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
5456 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5457 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
5458 << 15 << 10 << 1 << ListRange(10, 14);
5459 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
5460 << 15 << 0 << 1 << ListRange(0, 14);
5462 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
5463 << 10 << 1 << 3 << ListRange(1, 9);
5464 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
5465 << 10 << 0 << 3 << ListRange(0, 9);
5466 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5467 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
5468 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
5469 << 13 << 5 << 3 << ListRange(5, 12);
5470 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
5471 << 13 << 0 << 3 << ListRange(0, 12);
5473 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
5474 << 20 << 0 << 1 << ListRange(0, 15);
5475 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
5476 << 25 << 4 << 1 << ListRange(0+4, 15+4);
5477 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
5478 << 20 << 0 << 3 << ListRange(0, 15);
5479 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
5480 << 25 << 4 << 3 << ListRange(0+4, 15+4);
5482 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
5483 << 20 << 15 << 1 << ListRange(15, 15);
5484 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
5485 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
5486 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
5487 << 20 << 15 << 3 << ListRange(15, 15);
5488 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
5489 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
5492 void tst_QQuickListView::removeTransitions()
5494 QFETCH(int, initialItemCount);
5495 QFETCH(bool, shouldAnimateTargets);
5496 QFETCH(qreal, contentY);
5497 QFETCH(int, removalIndex);
5498 QFETCH(int, removalCount);
5499 QFETCH(ListRange, expectedDisplacedIndexes);
5501 // added items should end here
5502 QPointF targetItems_transitionTo(-50, -50);
5504 // displaced items should pass through this points
5505 QPointF displacedItems_transitionVia(100, 100);
5508 for (int i = 0; i < initialItemCount; i++)
5509 model.addItem("Original item" + QString::number(i), "");
5510 QaimModel model_targetItems_transitionTo;
5511 QaimModel model_displacedItems_transitionVia;
5513 QQuickView *canvas = getView();
5514 QQmlContext *ctxt = canvas->rootContext();
5515 TestObject *testObject = new TestObject;
5516 ctxt->setContextProperty("testModel", &model);
5517 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5518 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5519 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5520 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5521 ctxt->setContextProperty("testObject", testObject);
5522 canvas->setSource(testFileUrl("removeTransitions.qml"));
5524 QTest::qWaitForWindowShown(canvas);
5526 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5527 QTRY_VERIFY(listview != 0);
5528 QQuickItem *contentItem = listview->contentItem();
5529 QVERIFY(contentItem != 0);
5530 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5532 if (contentY != 0) {
5533 listview->setContentY(contentY);
5534 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5537 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5539 // only target items that are visible should be animated
5540 QList<QPair<QString, QString> > expectedTargetData;
5541 QList<int> targetIndexes;
5542 if (shouldAnimateTargets) {
5543 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5544 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
5545 expectedTargetData << qMakePair(model.name(i), model.number(i));
5549 QVERIFY(expectedTargetData.count() > 0);
5552 // calculate targetItems and expectedTargets before model changes
5553 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5554 QVariantMap expectedTargets;
5555 for (int i=0; i<targetIndexes.count(); i++)
5556 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5559 model.removeItems(removalIndex, removalCount);
5560 QTRY_COMPARE(model.count(), listview->count());
5562 if (shouldAnimateTargets) {
5563 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5564 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5565 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5567 // check the target and displaced items were animated
5568 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5569 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5571 // check attached properties
5572 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
5573 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5574 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5575 if (expectedDisplacedIndexes.isValid()) {
5576 // adjust expectedDisplacedIndexes to their final values after the move
5577 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5578 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5579 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5580 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5583 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5584 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5587 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5588 int firstVisibleIndex = -1;
5589 int itemCount = items.count();
5591 for (int i=0; i<items.count(); i++) {
5592 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5593 int index = e.evaluate().toInt();
5594 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
5595 firstVisibleIndex = index;
5597 itemCount--; // exclude deleted items
5599 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5601 // verify all items moved to the correct final positions
5602 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5603 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5604 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5605 QCOMPARE(item->x(), 0.0);
5606 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
5607 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5609 QTRY_COMPARE(name->text(), model.name(i));
5612 releaseView(canvas);
5616 void tst_QQuickListView::removeTransitions_data()
5618 QTest::addColumn<int>("initialItemCount");
5619 QTest::addColumn<qreal>("contentY");
5620 QTest::addColumn<bool>("shouldAnimateTargets");
5621 QTest::addColumn<int>("removalIndex");
5622 QTest::addColumn<int>("removalCount");
5623 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5625 // All items that are visible following the remove operation should be animated.
5626 // Remove targets that are outside of the view should not be animated.
5628 QTest::newRow("remove 1 before start")
5629 << 30 << 20.0 * 3 << false
5630 << 2 << 1 << ListRange();
5631 QTest::newRow("remove multiple, all before start")
5632 << 30 << 20.0 * 3 << false
5633 << 0 << 3 << ListRange();
5634 QTest::newRow("remove mix of before and after start")
5635 << 30 << 20.0 * 3 << true
5636 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
5638 QTest::newRow("remove 1 from start")
5639 << 30 << 0.0 << true
5640 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
5641 QTest::newRow("remove multiple from start")
5642 << 30 << 0.0 << true
5643 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
5644 QTest::newRow("remove 1 from start, content y not 0")
5645 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
5646 << 2 << 1 << ListRange(1 + 2, 16 + 2);
5647 QTest::newRow("remove multiple from start, content y not 0")
5648 << 30 << 20.0 * 2 << true // first visible is index 2
5649 << 2 << 3 << ListRange(3 + 2, 18 + 2);
5651 QTest::newRow("remove 1 from middle")
5652 << 30 << 0.0 << true
5653 << 5 << 1 << ListRange(6, 16);
5654 QTest::newRow("remove multiple from middle")
5655 << 30 << 0.0 << true
5656 << 5 << 3 << ListRange(8, 18);
5659 QTest::newRow("remove 1 from bottom")
5660 << 30 << 0.0 << true
5661 << 15 << 1 << ListRange(16, 16);
5663 // remove 15, 16, 17
5664 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
5665 // the view, and 18 will be animated as the displaced item to replace the last item
5666 QTest::newRow("remove multiple from bottom")
5667 << 30 << 0.0 << true
5668 << 15 << 3 << ListRange(18, 18);
5670 QTest::newRow("remove 1 from bottom, content y not 0")
5671 << 30 << 20.0 * 2 << true
5672 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
5673 QTest::newRow("remove multiple from bottom, content y not 0")
5674 << 30 << 20.0 * 2 << true
5675 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
5678 QTest::newRow("remove 1 after end")
5679 << 30 << 0.0 << false
5680 << 17 << 1 << ListRange();
5681 QTest::newRow("remove multiple after end")
5682 << 30 << 0.0 << false
5683 << 17 << 3 << ListRange();
5686 void tst_QQuickListView::displacedTransitions()
5688 QFETCH(bool, useDisplaced);
5689 QFETCH(bool, displacedEnabled);
5690 QFETCH(bool, useAddDisplaced);
5691 QFETCH(bool, addDisplacedEnabled);
5692 QFETCH(bool, useMoveDisplaced);
5693 QFETCH(bool, moveDisplacedEnabled);
5694 QFETCH(bool, useRemoveDisplaced);
5695 QFETCH(bool, removeDisplacedEnabled);
5696 QFETCH(ListChange, change);
5697 QFETCH(ListRange, expectedDisplacedIndexes);
5700 for (int i = 0; i < 30; i++)
5701 model.addItem("Original item" + QString::number(i), "");
5702 QaimModel model_displaced_transitionVia;
5703 QaimModel model_addDisplaced_transitionVia;
5704 QaimModel model_moveDisplaced_transitionVia;
5705 QaimModel model_removeDisplaced_transitionVia;
5707 QPointF displaced_transitionVia(-50, -100);
5708 QPointF addDisplaced_transitionVia(-150, 100);
5709 QPointF moveDisplaced_transitionVia(50, -100);
5710 QPointF removeDisplaced_transitionVia(150, 100);
5712 QQuickView *canvas = getView();
5713 QQmlContext *ctxt = canvas->rootContext();
5714 TestObject *testObject = new TestObject(canvas);
5715 ctxt->setContextProperty("testModel", &model);
5716 ctxt->setContextProperty("testObject", testObject);
5717 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
5718 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
5719 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
5720 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
5721 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
5722 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
5723 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
5724 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
5725 ctxt->setContextProperty("useDisplaced", useDisplaced);
5726 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
5727 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
5728 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
5729 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
5730 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
5731 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
5732 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
5733 canvas->setSource(testFileUrl("displacedTransitions.qml"));
5735 qApp->processEvents();
5737 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5738 QTRY_VERIFY(listview != 0);
5739 QQuickItem *contentItem = listview->contentItem();
5740 QVERIFY(contentItem != 0);
5741 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5743 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5744 listview->setProperty("displaceTransitionsDone", false);
5746 switch (change.type) {
5747 case ListChange::Inserted:
5749 QList<QPair<QString, QString> > targetItemData;
5750 for (int i=change.index; i<change.index + change.count; ++i)
5751 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
5752 model.insertItems(change.index, targetItemData);
5753 QTRY_COMPARE(model.count(), listview->count());
5756 case ListChange::Removed:
5757 model.removeItems(change.index, change.count);
5758 QTRY_COMPARE(model.count(), listview->count());
5760 case ListChange::Moved:
5761 model.moveItems(change.index, change.to, change.count);
5762 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5764 case ListChange::SetCurrent:
5765 case ListChange::SetContentY:
5769 QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
5770 QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
5772 if ((useDisplaced && displacedEnabled)
5773 || (useAddDisplaced && addDisplacedEnabled)
5774 || (useMoveDisplaced && moveDisplacedEnabled)
5775 || (useRemoveDisplaced && removeDisplacedEnabled)) {
5776 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
5778 // check the correct number of target items and indexes were received
5779 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
5780 for (int i=0; i<resultTargetIndexes.count(); i++)
5781 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
5782 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
5783 for (int i=0; i<resultTargetItems.count(); i++)
5784 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
5786 QCOMPARE(resultTargetIndexes.count(), 0);
5787 QCOMPARE(resultTargetItems.count(), 0);
5790 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
5791 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
5793 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
5794 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
5795 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
5797 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
5798 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
5799 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
5801 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
5803 if (useDisplaced && displacedEnabled
5804 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
5805 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
5806 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
5807 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
5809 QCOMPARE(model_displaced_transitionVia.count(), 0);
5812 // verify all items moved to the correct final positions
5813 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5814 for (int i=0; i < model.count() && i < items.count(); ++i) {
5815 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5816 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5817 QCOMPARE(item->x(), 0.0);
5818 QCOMPARE(item->y(), i * 20.0);
5819 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5821 QTRY_COMPARE(name->text(), model.name(i));
5824 releaseView(canvas);
5827 void tst_QQuickListView::displacedTransitions_data()
5829 QTest::addColumn<bool>("useDisplaced");
5830 QTest::addColumn<bool>("displacedEnabled");
5831 QTest::addColumn<bool>("useAddDisplaced");
5832 QTest::addColumn<bool>("addDisplacedEnabled");
5833 QTest::addColumn<bool>("useMoveDisplaced");
5834 QTest::addColumn<bool>("moveDisplacedEnabled");
5835 QTest::addColumn<bool>("useRemoveDisplaced");
5836 QTest::addColumn<bool>("removeDisplacedEnabled");
5837 QTest::addColumn<ListChange>("change");
5838 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5840 QTest::newRow("no displaced transitions at all")
5845 << ListChange::insert(0, 1) << ListRange(0, 15);
5847 QTest::newRow("just displaced")
5852 << ListChange::insert(0, 1) << ListRange(0, 15);
5854 QTest::newRow("just displaced (not enabled)")
5859 << ListChange::insert(0, 1) << ListRange(0, 15);
5861 QTest::newRow("displaced + addDisplaced")
5866 << ListChange::insert(0, 1) << ListRange(0, 15);
5868 QTest::newRow("displaced + addDisplaced (not enabled)")
5873 << ListChange::insert(0, 1) << ListRange(0, 15);
5875 QTest::newRow("displaced + moveDisplaced")
5880 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5882 QTest::newRow("displaced + moveDisplaced (not enabled)")
5887 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5889 QTest::newRow("displaced + removeDisplaced")
5894 << ListChange::remove(0, 1) << ListRange(1, 16);
5896 QTest::newRow("displaced + removeDisplaced (not enabled)")
5901 << ListChange::remove(0, 1) << ListRange(1, 16);
5904 QTest::newRow("displaced + add, should use generic displaced for a remove")
5909 << ListChange::remove(0, 1) << ListRange(1, 16);
5912 void tst_QQuickListView::multipleTransitions()
5914 // Tests that if you interrupt a transition in progress with another action that
5915 // cancels the previous transition, the resulting items are still placed correctly.
5917 QFETCH(int, initialCount);
5918 QFETCH(qreal, contentY);
5919 QFETCH(QList<ListChange>, changes);
5920 QFETCH(bool, enableAddTransitions);
5921 QFETCH(bool, enableMoveTransitions);
5922 QFETCH(bool, enableRemoveTransitions);
5923 QFETCH(bool, rippleAddDisplaced);
5925 QPointF addTargets_transitionFrom(-50, -50);
5926 QPointF addDisplaced_transitionFrom(-50, 50);
5927 QPointF moveTargets_transitionFrom(50, -50);
5928 QPointF moveDisplaced_transitionFrom(50, 50);
5929 QPointF removeTargets_transitionTo(-100, 300);
5930 QPointF removeDisplaced_transitionFrom(100, 300);
5933 for (int i = 0; i < initialCount; i++)
5934 model.addItem("Original item" + QString::number(i), "");
5936 QQuickView *canvas = getView();
5937 QQmlContext *ctxt = canvas->rootContext();
5938 TestObject *testObject = new TestObject;
5939 ctxt->setContextProperty("testModel", &model);
5940 ctxt->setContextProperty("testObject", testObject);
5941 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5942 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5943 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5944 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5945 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5946 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5947 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5948 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5949 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5950 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5951 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5953 QTest::qWaitForWindowShown(canvas);
5955 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5956 QTRY_VERIFY(listview != 0);
5957 QQuickItem *contentItem = listview->contentItem();
5958 QVERIFY(contentItem != 0);
5959 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5961 if (contentY != 0) {
5962 listview->setContentY(contentY);
5963 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5966 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5968 for (int i=0; i<changes.count(); i++) {
5969 switch (changes[i].type) {
5970 case ListChange::Inserted:
5972 QList<QPair<QString, QString> > targetItems;
5973 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5974 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5975 model.insertItems(changes[i].index, targetItems);
5976 QTRY_COMPARE(model.count(), listview->count());
5977 if (i == changes.count() - 1) {
5978 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
5979 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
5981 QTest::qWait(timeBetweenActions);
5985 case ListChange::Removed:
5986 model.removeItems(changes[i].index, changes[i].count);
5987 QTRY_COMPARE(model.count(), listview->count());
5988 if (i == changes.count() - 1) {
5989 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
5990 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
5992 QTest::qWait(timeBetweenActions);
5995 case ListChange::Moved:
5996 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5997 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5998 if (i == changes.count() - 1) {
5999 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6000 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6002 QTest::qWait(timeBetweenActions);
6005 case ListChange::SetCurrent:
6006 listview->setCurrentIndex(changes[i].index);
6008 case ListChange::SetContentY:
6009 listview->setContentY(changes[i].pos);
6010 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6014 QCOMPARE(listview->count(), model.count());
6016 // verify all items moved to the correct final positions
6017 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6018 for (int i=0; i < model.count() && i < items.count(); ++i) {
6019 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6020 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6021 QTRY_COMPARE(item->x(), 0.0);
6022 QTRY_COMPARE(item->y(), i*20.0);
6023 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6025 QTRY_COMPARE(name->text(), model.name(i));
6028 releaseView(canvas);
6032 void tst_QQuickListView::multipleTransitions_data()
6034 QTest::addColumn<int>("initialCount");
6035 QTest::addColumn<qreal>("contentY");
6036 QTest::addColumn<QList<ListChange> >("changes");
6037 QTest::addColumn<bool>("enableAddTransitions");
6038 QTest::addColumn<bool>("enableMoveTransitions");
6039 QTest::addColumn<bool>("enableRemoveTransitions");
6040 QTest::addColumn<bool>("rippleAddDisplaced");
6042 // the added item and displaced items should move to final dest correctly
6043 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6044 << ListChange::insert(0, 1)
6045 << ListChange::move(0, 3, 1)
6047 << true << true << true << false;
6049 // items affected by the add should change from move to add transition
6050 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6051 << ListChange::move(1, 10, 3)
6052 << ListChange::insert(0, 1)
6054 << true << true << true << false;
6056 // items should be placed correctly if you trigger a transition then refill for that index
6057 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6058 << ListChange::insert(0, 1)
6059 << ListChange::setContentY(80.0)
6060 << ListChange::setContentY(0.0)
6061 << ListChange::insert(0, 1)
6063 << true << true << true << false;
6065 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6066 << ListChange::insert(1, 1)
6067 << ListChange::remove(1, 1)
6069 << true << true << true << true;
6071 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6072 // even if a remove-displace transition is not present to re-animate them
6073 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6074 << ListChange::insert(0, 1)
6075 << ListChange::remove(2, 1)
6077 << true << true << false << false;
6079 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6080 // remove has changed the position of where it will move to
6081 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6082 << ListChange::setContentY(-10.0)
6083 << ListChange::insert(0, 1)
6084 << ListChange::insert(0, 1)
6085 << ListChange::remove(2, 1)
6087 << true << true << false << false;
6090 void tst_QQuickListView::multipleDisplaced()
6092 // multiple move() operations should only restart displace transitions for items that
6093 // moved from previously set positions, and not those that have moved from their current
6094 // item positions (which may e.g. still be changing from easing bounces in the last transition)
6097 for (int i = 0; i < 30; i++)
6098 model.addItem("Original item" + QString::number(i), "");
6100 QQuickView *canvas = getView();
6101 QQmlContext *ctxt = canvas->rootContext();
6102 ctxt->setContextProperty("testModel", &model);
6103 ctxt->setContextProperty("testObject", new TestObject(canvas));
6104 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
6106 QTest::qWaitForWindowShown(canvas);
6108 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6109 QTRY_VERIFY(listview != 0);
6110 QQuickItem *contentItem = listview->contentItem();
6111 QVERIFY(contentItem != 0);
6112 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6114 model.moveItems(12, 8, 1);
6115 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
6116 model.moveItems(8, 3, 1);
6117 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6119 QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6120 foreach (const QString &name, transitionsStarted.keys()) {
6121 QVERIFY2(transitionsStarted[name] == 1,
6122 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6125 // verify all items moved to the correct final positions
6126 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6127 for (int i=0; i < model.count() && i < items.count(); ++i) {
6128 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6129 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6130 QTRY_COMPARE(item->x(), 0.0);
6131 QTRY_COMPARE(item->y(), i*20.0);
6132 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6134 QTRY_COMPARE(name->text(), model.name(i));
6137 releaseView(canvas);
6140 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6144 for (int i=0; i<list.count(); i++) {
6145 ret << list[i].toInt(&ok);
6147 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6153 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6155 for (int i=0; i<indexLists.count(); i++) {
6156 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6157 if (current != expectedIndexes.toSet())
6158 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6159 QCOMPARE(current, expectedIndexes.toSet());
6163 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6165 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6166 QVERIFY(it.value().type() == QVariant::Int);
6167 QString name = it.key();
6168 int itemIndex = it.value().toInt();
6169 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6170 if (model.name(itemIndex) != name)
6171 qDebug() << itemIndex;
6172 QCOMPARE(model.name(itemIndex), name);
6174 QCOMPARE(items.count(), expectedIndexes.count());
6177 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6179 for (int i=0; i<itemLists.count(); i++) {
6180 QVERIFY(itemLists[i].type() == QVariant::List);
6181 QVariantList current = itemLists[i].toList();
6182 for (int j=0; j<current.count(); j++) {
6183 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6184 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6185 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6187 QCOMPARE(current.count(), expectedItems.count());
6191 void tst_QQuickListView::flickBeyondBounds()
6193 QQuickView *canvas = createView();
6195 canvas->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6197 qApp->processEvents();
6199 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
6200 QTRY_VERIFY(listview != 0);
6202 QQuickItem *contentItem = listview->contentItem();
6203 QTRY_VERIFY(contentItem != 0);
6204 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6206 // Flick view up beyond bounds
6207 flick(canvas, QPoint(10, 10), QPoint(10, -1000), 180);
6208 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6210 // We're really testing that we don't get stuck in a loop,
6211 // but also confirm items positioned correctly.
6212 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6213 for (int i = 0; i < 2; ++i) {
6214 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6215 if (!item) qWarning() << "Item" << i << "not found";
6217 QTRY_VERIFY(item->y() == i*45);
6224 QTEST_MAIN(tst_QQuickListView)
6226 #include "tst_qquicklistview.moc"