1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtCore/QStringListModel>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcontext.h>
47 #include <QtQml/qqmlexpression.h>
48 #include <QtQml/qqmlincubator.h>
49 #include <QtQuick/private/qquicklistview_p.h>
50 #include <QtQuick/private/qquicktext_p.h>
51 #include <QtQuick/private/qquickvisualitemmodel_p.h>
52 #include <QtQml/private/qquicklistmodel_p.h>
53 #include "../../shared/util.h"
54 #include "../shared/viewtestutil.h"
55 #include "../shared/visualtestutil.h"
56 #include "incrementalmodel.h"
59 Q_DECLARE_METATYPE(Qt::LayoutDirection)
60 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
61 Q_DECLARE_METATYPE(QQuickItemView::PositionMode)
62 Q_DECLARE_METATYPE(QQuickListView::Orientation)
63 Q_DECLARE_METATYPE(Qt::Key)
65 using namespace QQuickViewTestUtil;
66 using namespace QQuickVisualTestUtil;
70 class tst_QQuickListView : public QQmlDataTest
78 void cleanupTestCase();
79 // Test QAbstractItemModel model types
80 void qAbstractItemModel_package_items();
81 void qAbstractItemModel_items();
83 void qAbstractItemModel_package_changed();
84 void qAbstractItemModel_changed();
86 void qAbstractItemModel_package_inserted();
87 void qAbstractItemModel_inserted();
88 void qAbstractItemModel_inserted_more();
89 void qAbstractItemModel_inserted_more_data();
90 void qAbstractItemModel_inserted_more_bottomToTop();
91 void qAbstractItemModel_inserted_more_bottomToTop_data();
93 void qAbstractItemModel_package_removed();
94 void qAbstractItemModel_removed();
95 void qAbstractItemModel_removed_more();
96 void qAbstractItemModel_removed_more_data();
97 void qAbstractItemModel_removed_more_bottomToTop();
98 void qAbstractItemModel_removed_more_bottomToTop_data();
100 void qAbstractItemModel_package_moved();
101 void qAbstractItemModel_package_moved_data();
102 void qAbstractItemModel_moved();
103 void qAbstractItemModel_moved_data();
104 void qAbstractItemModel_moved_bottomToTop();
105 void qAbstractItemModel_moved_bottomToTop_data();
107 void multipleChanges_condensed() { multipleChanges(true); }
108 void multipleChanges_condensed_data() { multipleChanges_data(); }
109 void multipleChanges_uncondensed() { multipleChanges(false); }
110 void multipleChanges_uncondensed_data() { multipleChanges_data(); }
112 void qAbstractItemModel_package_clear();
113 void qAbstractItemModel_clear();
114 void qAbstractItemModel_clear_bottomToTop();
116 void insertBeforeVisible();
117 void insertBeforeVisible_data();
118 void swapWithFirstItem();
120 void itemListFlicker();
121 void currentIndex_delayedItemCreation();
122 void currentIndex_delayedItemCreation_data();
124 void noCurrentIndex();
125 void keyNavigation();
126 void keyNavigation_data();
128 void enforceRange_withoutHighlight();
130 void qAbstractItemModel_package_sections();
131 void qAbstractItemModel_sections();
132 void sectionsPositioning();
133 void sectionsDelegate();
134 void sectionsDragOutsideBounds_data();
135 void sectionsDragOutsideBounds();
136 void sectionsDelegate_headerVisibility();
137 void sectionPropertyChange();
138 void sectionDelegateChange();
140 void positionViewAtBeginningEnd();
141 void positionViewAtIndex();
142 void positionViewAtIndex_data();
144 void propertyChanges();
145 void componentChanges();
147 void manualHighlight();
148 void initialZValues();
151 void header_delayItemCreation();
156 void resetModel_headerFooter();
158 void resizeViewAndRepaint();
159 void sizeLessThan1();
161 void resizeDelegate();
162 void resizeFirstDelegate();
163 void repositionResizedDelegate();
164 void repositionResizedDelegate_data();
166 void indexAt_itemAt_data();
167 void indexAt_itemAt();
168 void incrementalModel();
172 void onRemove_data();
174 void test_mirroring();
176 void marginsResize();
177 void marginsResize_data();
178 void creationContext();
179 void snapToItem_data();
181 void snapOneItem_data();
189 void unrequestedVisibility();
191 void populateTransitions();
192 void populateTransitions_data();
193 void addTransitions();
194 void addTransitions_data();
195 void moveTransitions();
196 void moveTransitions_data();
197 void removeTransitions();
198 void removeTransitions_data();
199 void displacedTransitions();
200 void displacedTransitions_data();
201 void multipleTransitions();
202 void multipleTransitions_data();
203 void multipleDisplaced();
205 void flickBeyondBounds();
206 void destroyItemOnCreation();
208 void parentBinding();
211 template <class T> void items(const QUrl &source, bool forceLayout);
212 template <class T> void changed(const QUrl &source, bool forceLayout);
213 template <class T> void inserted(const QUrl &source);
214 template <class T> void inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
215 template <class T> void removed(const QUrl &source, bool animated);
216 template <class T> void removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
217 template <class T> void moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
218 template <class T> void clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection = QQuickItemView::TopToBottom);
219 template <class T> void sections(const QUrl &source);
221 void multipleChanges(bool condensed);
222 void multipleChanges_data();
224 QList<int> toIntList(const QVariantList &list);
225 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
226 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
227 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
229 void inserted_more_data();
230 void removed_more_data();
234 QQuickView *getView() {
236 if (QString(QTest::currentTestFunction()) != testForView) {
240 m_view->setSource(QUrl());
245 testForView = QTest::currentTestFunction();
246 m_view = createView();
249 void releaseView(QQuickView *view) {
250 Q_ASSERT(view == m_view);
251 m_view->setSource(QUrl());
254 QQuickView *getView() {
257 void releaseView(QQuickView *view) {
262 static void errorMsgHandler(QtMsgType, const QMessageLogContext &, const QString &)
269 static int m_errorCount;
272 int tst_QQuickListView::m_errorCount = 0;
274 class TestObject : public QObject
278 Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError)
279 Q_PROPERTY(bool animate READ animate NOTIFY changedAnim)
280 Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl)
281 Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer)
284 TestObject(QObject *parent = 0)
285 : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
288 bool error() const { return mError; }
289 void setError(bool err) { mError = err; emit changedError(); }
291 bool animate() const { return mAnimate; }
292 void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
294 bool invalidHighlight() const { return mInvalidHighlight; }
295 void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
297 int cacheBuffer() const { return mCacheBuffer; }
298 void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
304 void changedCacheBuffer();
309 bool mInvalidHighlight;
313 tst_QQuickListView::tst_QQuickListView() : m_view(0)
317 void tst_QQuickListView::init()
320 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
321 testForView = QString();
328 void tst_QQuickListView::cleanupTestCase()
331 testForView = QString();
338 void tst_QQuickListView::items(const QUrl &source, bool forceLayout)
340 QQuickView *window = createView();
343 model.addItem("Fred", "12345");
344 model.addItem("John", "2345");
345 model.addItem("Bob", "54321");
347 QQmlContext *ctxt = window->rootContext();
348 ctxt->setContextProperty("testModel", &model);
350 TestObject *testObject = new TestObject;
351 ctxt->setContextProperty("testObject", testObject);
353 window->setSource(source);
354 qApp->processEvents();
356 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
357 QTRY_VERIFY(listview != 0);
359 QQuickItem *contentItem = listview->contentItem();
360 QTRY_VERIFY(contentItem != 0);
362 QMetaObject::invokeMethod(window->rootObject(), "checkProperties");
363 QTRY_VERIFY(testObject->error() == false);
365 QTRY_VERIFY(listview->highlightItem() != 0);
366 QTRY_COMPARE(listview->count(), model.count());
367 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
368 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
370 // current item should be first item
371 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
373 for (int i = 0; i < model.count(); ++i) {
374 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
375 QTRY_VERIFY(name != 0);
376 QTRY_COMPARE(name->text(), model.name(i));
377 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
378 QTRY_VERIFY(number != 0);
379 QTRY_COMPARE(number->text(), model.number(i));
382 // switch to other delegate
383 testObject->setAnimate(true);
384 QMetaObject::invokeMethod(window->rootObject(), "checkProperties");
385 QTRY_VERIFY(testObject->error() == false);
386 QTRY_VERIFY(listview->currentItem());
388 // set invalid highlight
389 testObject->setInvalidHighlight(true);
390 QMetaObject::invokeMethod(window->rootObject(), "checkProperties");
391 QTRY_VERIFY(testObject->error() == false);
392 QTRY_VERIFY(listview->currentItem());
393 QTRY_VERIFY(listview->highlightItem() == 0);
395 // back to normal highlight
396 testObject->setInvalidHighlight(false);
397 QMetaObject::invokeMethod(window->rootObject(), "checkProperties");
398 QTRY_VERIFY(testObject->error() == false);
399 QTRY_VERIFY(listview->currentItem());
400 QTRY_VERIFY(listview->highlightItem() != 0);
402 // set an empty model and confirm that items are destroyed
404 ctxt->setContextProperty("testModel", &model2);
406 // Force a layout, necessary if ListView is completed before VisualDataModel.
408 QCOMPARE(listview->property("count").toInt(), 0);
410 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
411 QTRY_VERIFY(itemCount == 0);
413 QTRY_COMPARE(listview->highlightResizeVelocity(), 1000.0);
414 QTRY_COMPARE(listview->highlightMoveVelocity(), 1000.0);
422 void tst_QQuickListView::changed(const QUrl &source, bool forceLayout)
424 QQuickView *window = createView();
427 model.addItem("Fred", "12345");
428 model.addItem("John", "2345");
429 model.addItem("Bob", "54321");
431 QQmlContext *ctxt = window->rootContext();
432 ctxt->setContextProperty("testModel", &model);
434 TestObject *testObject = new TestObject;
435 ctxt->setContextProperty("testObject", testObject);
437 window->setSource(source);
438 qApp->processEvents();
440 QQuickFlickable *listview = findItem<QQuickFlickable>(window->rootObject(), "list");
441 QTRY_VERIFY(listview != 0);
443 QQuickItem *contentItem = listview->contentItem();
444 QTRY_VERIFY(contentItem != 0);
446 // Force a layout, necessary if ListView is completed before VisualDataModel.
448 QCOMPARE(listview->property("count").toInt(), model.count());
450 model.modifyItem(1, "Will", "9876");
451 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
452 QTRY_VERIFY(name != 0);
453 QTRY_COMPARE(name->text(), model.name(1));
454 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
455 QTRY_VERIFY(number != 0);
456 QTRY_COMPARE(number->text(), model.number(1));
463 void tst_QQuickListView::inserted(const QUrl &source)
465 QQuickView *window = createView();
469 model.addItem("Fred", "12345");
470 model.addItem("John", "2345");
471 model.addItem("Bob", "54321");
473 QQmlContext *ctxt = window->rootContext();
474 ctxt->setContextProperty("testModel", &model);
476 TestObject *testObject = new TestObject;
477 ctxt->setContextProperty("testObject", testObject);
479 window->setSource(source);
480 qApp->processEvents();
482 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
483 QTRY_VERIFY(listview != 0);
485 QQuickItem *contentItem = listview->contentItem();
486 QTRY_VERIFY(contentItem != 0);
488 model.insertItem(1, "Will", "9876");
490 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
491 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
493 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
494 QTRY_VERIFY(name != 0);
495 QTRY_COMPARE(name->text(), model.name(1));
496 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
497 QTRY_VERIFY(number != 0);
498 QTRY_COMPARE(number->text(), model.number(1));
500 // Confirm items positioned correctly
501 for (int i = 0; i < model.count(); ++i) {
502 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
503 QTRY_COMPARE(item->y(), i*20.0);
506 model.insertItem(0, "Foo", "1111"); // zero index, and current item
508 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
509 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
511 name = findItem<QQuickText>(contentItem, "textName", 0);
512 QTRY_VERIFY(name != 0);
513 QTRY_COMPARE(name->text(), model.name(0));
514 number = findItem<QQuickText>(contentItem, "textNumber", 0);
515 QTRY_VERIFY(number != 0);
516 QTRY_COMPARE(number->text(), model.number(0));
518 QTRY_COMPARE(listview->currentIndex(), 1);
520 // Confirm items positioned correctly
521 for (int i = 0; i < model.count(); ++i) {
522 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
523 QTRY_COMPARE(item->y(), i*20.0);
526 for (int i = model.count(); i < 30; ++i)
527 model.insertItem(i, "Hello", QString::number(i));
529 listview->setContentY(80);
531 // Insert item outside visible area
532 model.insertItem(1, "Hello", "1324");
534 QTRY_VERIFY(listview->contentY() == 80);
536 // Confirm items positioned correctly
537 for (int i = 5; i < 5+15; ++i) {
538 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
539 if (!item) qWarning() << "Item" << i << "not found";
541 QTRY_COMPARE(item->y(), i*20.0 - 20.0);
544 // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
548 model.insertItem(0, "Hello", "1234");
549 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
551 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
553 QCOMPARE(item->y(), 0.);
554 QTRY_VERIFY(listview->contentY() == 0);
561 void tst_QQuickListView::inserted_more(QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
563 QFETCH(qreal, contentY);
564 QFETCH(int, insertIndex);
565 QFETCH(int, insertCount);
566 QFETCH(qreal, itemsOffsetAfterMove);
569 for (int i = 0; i < 30; i++)
570 model.addItem("Item" + QString::number(i), "");
572 QQuickView *window = getView();
573 QQmlContext *ctxt = window->rootContext();
574 ctxt->setContextProperty("testModel", &model);
576 TestObject *testObject = new TestObject;
577 ctxt->setContextProperty("testObject", testObject);
579 window->setSource(testFileUrl("listviewtest.qml"));
581 QVERIFY(QTest::qWaitForWindowExposed(window));
583 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
584 QTRY_VERIFY(listview != 0);
585 QQuickItem *contentItem = listview->contentItem();
586 QTRY_VERIFY(contentItem != 0);
588 bool waitForPolish = (contentY != 0);
589 if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
590 listview->setVerticalLayoutDirection(verticalLayoutDirection);
591 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
592 contentY = -listview->height() - contentY;
594 listview->setContentY(contentY);
596 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
598 QList<QPair<QString, QString> > newData;
599 for (int i=0; i<insertCount; i++)
600 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
601 model.insertItems(insertIndex, newData);
602 QTRY_COMPARE(listview->property("count").toInt(), model.count());
604 // check visibleItems.first() is in correct position
605 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
607 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
608 QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
610 QCOMPARE(item0->y(), itemsOffsetAfterMove);
612 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
613 int firstVisibleIndex = -1;
614 for (int i=0; i<items.count(); i++) {
615 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
616 if (item && !QQuickItemPrivate::get(item)->culled) {
617 firstVisibleIndex = i;
621 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
623 // Confirm items positioned correctly and indexes correct
626 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
627 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
628 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
629 qreal pos = i*20.0 + itemsOffsetAfterMove;
630 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
631 pos = -item0->height() - pos;
632 QTRY_COMPARE(item->y(), pos);
633 name = findItem<QQuickText>(contentItem, "textName", i);
635 QTRY_COMPARE(name->text(), model.name(i));
636 number = findItem<QQuickText>(contentItem, "textNumber", i);
637 QVERIFY(number != 0);
638 QTRY_COMPARE(number->text(), model.number(i));
645 void tst_QQuickListView::inserted_more_data()
647 QTest::addColumn<qreal>("contentY");
648 QTest::addColumn<int>("insertIndex");
649 QTest::addColumn<int>("insertCount");
650 QTest::addColumn<qreal>("itemsOffsetAfterMove");
652 QTest::newRow("add 1, before visible items")
655 << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move
657 QTest::newRow("add multiple, before visible")
660 << -20.0 * 3; // again first visible should not move
662 QTest::newRow("add 1, at start of visible, content at start")
667 QTest::newRow("add multiple, start of visible, content at start")
672 QTest::newRow("add 1, at start of visible, content not at start")
677 QTest::newRow("add multiple, at start of visible, content not at start")
683 QTest::newRow("add 1, at end of visible, content at start")
688 QTest::newRow("add 1, at end of visible, content at start")
693 QTest::newRow("add 1, at end of visible, content not at start")
698 QTest::newRow("add multiple, at end of visible, content not at start")
704 QTest::newRow("add 1, after visible, content at start")
709 QTest::newRow("add 1, after visible, content at start")
714 QTest::newRow("add 1, after visible, content not at start")
719 QTest::newRow("add multiple, after visible, content not at start")
725 void tst_QQuickListView::insertBeforeVisible()
727 QFETCH(int, insertIndex);
728 QFETCH(int, insertCount);
729 QFETCH(int, cacheBuffer);
732 QQuickView *window = getView();
735 for (int i = 0; i < 30; i++)
736 model.addItem("Item" + QString::number(i), "");
738 QQmlContext *ctxt = window->rootContext();
739 ctxt->setContextProperty("testModel", &model);
741 TestObject *testObject = new TestObject;
742 ctxt->setContextProperty("testObject", testObject);
744 window->setSource(testFileUrl("listviewtest.qml"));
746 QVERIFY(QTest::qWaitForWindowExposed(window));
748 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
749 QTRY_VERIFY(listview != 0);
750 QQuickItem *contentItem = listview->contentItem();
751 QTRY_VERIFY(contentItem != 0);
753 listview->setCacheBuffer(cacheBuffer);
754 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
756 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
757 int firstVisibleIndex = 20; // move to an index where the top item is not visible
758 listview->setContentY(firstVisibleIndex * 20.0);
759 listview->setCurrentIndex(firstVisibleIndex);
761 qApp->processEvents();
762 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
763 QTRY_COMPARE(listview->currentIndex(), firstVisibleIndex);
764 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
766 QCOMPARE(item->y(), listview->contentY());
768 QList<QPair<QString, QString> > newData;
769 for (int i=0; i<insertCount; i++)
770 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
771 model.insertItems(insertIndex, newData);
772 QTRY_COMPARE(listview->property("count").toInt(), model.count());
774 // now, moving to the top of the view should position the inserted items correctly
775 int itemsOffsetAfterMove = -(insertCount * 20);
776 listview->setCurrentIndex(0);
777 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
778 QTRY_COMPARE(listview->currentIndex(), 0);
779 QTRY_COMPARE(listview->contentY(), 0.0 + itemsOffsetAfterMove);
781 // Confirm items positioned correctly and indexes correct
782 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
783 for (int i = 0; i < model.count() && i < itemCount; ++i) {
784 item = findItem<QQuickItem>(contentItem, "wrapper", i);
785 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
786 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
787 name = findItem<QQuickText>(contentItem, "textName", i);
789 QTRY_COMPARE(name->text(), model.name(i));
796 void tst_QQuickListView::insertBeforeVisible_data()
798 QTest::addColumn<int>("insertIndex");
799 QTest::addColumn<int>("insertCount");
800 QTest::addColumn<int>("cacheBuffer");
802 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
803 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
804 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
806 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
807 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
808 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
810 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 3 << 0;
811 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 3 << 100;
812 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 3 << 500;
814 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 3 << 0;
815 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 3 << 100;
816 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 3 << 500;
820 void tst_QQuickListView::removed(const QUrl &source, bool /* animated */)
822 QQuickView *window = createView();
825 for (int i = 0; i < 50; i++)
826 model.addItem("Item" + QString::number(i), "");
828 QQmlContext *ctxt = window->rootContext();
829 ctxt->setContextProperty("testModel", &model);
831 TestObject *testObject = new TestObject;
832 ctxt->setContextProperty("testObject", testObject);
834 window->setSource(source);
836 QVERIFY(QTest::qWaitForWindowExposed(window));
838 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
839 QTRY_VERIFY(listview != 0);
840 QQuickItem *contentItem = listview->contentItem();
841 QTRY_VERIFY(contentItem != 0);
842 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
845 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
847 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
848 QTRY_VERIFY(name != 0);
849 QTRY_COMPARE(name->text(), model.name(1));
850 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
851 QTRY_VERIFY(number != 0);
852 QTRY_COMPARE(number->text(), model.number(1));
854 // Confirm items positioned correctly
855 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
856 for (int i = 0; i < model.count() && i < itemCount; ++i) {
857 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
858 if (!item) qWarning() << "Item" << i << "not found";
860 QTRY_VERIFY(item->y() == i*20);
863 // Remove first item (which is the current item);
865 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
867 name = findItem<QQuickText>(contentItem, "textName", 0);
868 QTRY_VERIFY(name != 0);
869 QTRY_COMPARE(name->text(), model.name(0));
870 number = findItem<QQuickText>(contentItem, "textNumber", 0);
871 QTRY_VERIFY(number != 0);
872 QTRY_COMPARE(number->text(), model.number(0));
874 // Confirm items positioned correctly
875 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
876 for (int i = 0; i < model.count() && i < itemCount; ++i) {
877 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
878 if (!item) qWarning() << "Item" << i << "not found";
880 QTRY_COMPARE(item->y(),i*20.0);
883 // Remove items not visible
884 model.removeItem(18);
885 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
887 // Confirm items positioned correctly
888 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
889 for (int i = 0; i < model.count() && i < itemCount; ++i) {
890 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
891 if (!item) qWarning() << "Item" << i << "not found";
893 QTRY_COMPARE(item->y(),i*20.0);
896 // Remove items before visible
897 listview->setContentY(80);
898 listview->setCurrentIndex(10);
900 model.removeItem(1); // post: top item will be at 20
901 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
903 // Confirm items positioned correctly
904 for (int i = 2; i < 18; ++i) {
905 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
906 if (!item) qWarning() << "Item" << i << "not found";
908 QTRY_COMPARE(item->y(),20+i*20.0);
911 // Remove current index
912 QTRY_VERIFY(listview->currentIndex() == 9);
913 QQuickItem *oldCurrent = listview->currentItem();
916 QTRY_COMPARE(listview->currentIndex(), 9);
917 QTRY_VERIFY(listview->currentItem() != oldCurrent);
919 listview->setContentY(20); // That's the top now
920 // let transitions settle.
921 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
923 // Confirm items positioned correctly
924 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
925 for (int i = 0; i < model.count() && i < itemCount; ++i) {
926 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
927 if (!item) qWarning() << "Item" << i << "not found";
929 QTRY_COMPARE(item->y(),20+i*20.0);
932 // remove current item beyond visible items.
933 listview->setCurrentIndex(20);
934 listview->setContentY(40);
935 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
937 model.removeItem(20);
938 QTRY_COMPARE(listview->currentIndex(), 20);
939 QTRY_VERIFY(listview->currentItem() != 0);
941 // remove item before current, but visible
942 listview->setCurrentIndex(8);
943 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
944 oldCurrent = listview->currentItem();
947 QTRY_COMPARE(listview->currentIndex(), 7);
948 QTRY_VERIFY(listview->currentItem() == oldCurrent);
950 listview->setContentY(80);
951 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
953 // remove all visible items
954 model.removeItems(1, 18);
955 QTRY_COMPARE(listview->count() , model.count());
957 // Confirm items positioned correctly
958 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
959 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
960 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i+1);
961 if (!item) qWarning() << "Item" << i+1 << "not found";
963 QTRY_COMPARE(item->y(),80+i*20.0);
966 model.removeItems(1, 17);
967 QTRY_COMPARE(listview->count() , model.count());
969 model.removeItems(2, 1);
970 QTRY_COMPARE(listview->count() , model.count());
972 model.addItem("New", "1");
973 QTRY_COMPARE(listview->count() , model.count());
975 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", model.count()-1));
976 QCOMPARE(name->text(), QString("New"));
978 // Add some more items so that we don't run out
980 for (int i = 0; i < 50; i++)
981 model.addItem("Item" + QString::number(i), "");
984 listview->setCurrentIndex(0);
985 listview->setContentY(30);
987 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", 0));
989 // QTBUG-19198 move to end and remove all visible items one at a time.
990 listview->positionViewAtEnd();
991 for (int i = 0; i < 18; ++i)
992 model.removeItems(model.count() - 1, 1);
993 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() > 16);
1000 void tst_QQuickListView::removed_more(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1002 QFETCH(qreal, contentY);
1003 QFETCH(int, removeIndex);
1004 QFETCH(int, removeCount);
1005 QFETCH(qreal, itemsOffsetAfterMove);
1007 QQuickView *window = getView();
1010 for (int i = 0; i < 30; i++)
1011 model.addItem("Item" + QString::number(i), "");
1013 QQmlContext *ctxt = window->rootContext();
1014 ctxt->setContextProperty("testModel", &model);
1016 TestObject *testObject = new TestObject;
1017 ctxt->setContextProperty("testObject", testObject);
1019 window->setSource(source);
1021 QVERIFY(QTest::qWaitForWindowExposed(window));
1023 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1024 QTRY_VERIFY(listview != 0);
1025 QQuickItem *contentItem = listview->contentItem();
1026 QTRY_VERIFY(contentItem != 0);
1028 bool waitForPolish = (contentY != 0);
1029 if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
1030 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1031 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1032 contentY = -listview->height() - contentY;
1034 listview->setContentY(contentY);
1036 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1038 model.removeItems(removeIndex, removeCount);
1039 QTRY_COMPARE(listview->property("count").toInt(), model.count());
1041 // check visibleItems.first() is in correct position
1042 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
1045 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1046 QCOMPARE(item0->y(), -item0->height() - itemsOffsetAfterMove);
1048 QCOMPARE(item0->y(), itemsOffsetAfterMove);
1050 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1051 int firstVisibleIndex = -1;
1052 for (int i=0; i<items.count(); i++) {
1053 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1054 if (item && delegateVisible(item)) {
1055 firstVisibleIndex = i;
1059 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1061 // Confirm items positioned correctly and indexes correct
1064 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1065 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1066 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1067 qreal pos = i*20.0 + itemsOffsetAfterMove;
1068 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1069 pos = -item0->height() - pos;
1070 QTRY_COMPARE(item->y(), pos);
1071 name = findItem<QQuickText>(contentItem, "textName", i);
1073 QTRY_COMPARE(name->text(), model.name(i));
1074 number = findItem<QQuickText>(contentItem, "textNumber", i);
1075 QVERIFY(number != 0);
1076 QTRY_COMPARE(number->text(), model.number(i));
1079 releaseView(window);
1083 void tst_QQuickListView::removed_more_data()
1085 QTest::addColumn<qreal>("contentY");
1086 QTest::addColumn<int>("removeIndex");
1087 QTest::addColumn<int>("removeCount");
1088 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1090 QTest::newRow("remove 1, before visible items")
1091 << 80.0 // show 4-19
1093 << 20.0; // visible items slide down by 1 item so that first visible does not move
1095 QTest::newRow("remove multiple, all before visible items")
1100 QTest::newRow("remove multiple, all before visible items, remove item 0")
1105 // remove 1,2,3 before the visible pos, 0 moves down to just before the visible pos,
1106 // items 4,5 are removed from view, item 6 slides up to original pos of item 4 (80px)
1107 QTest::newRow("remove multiple, mix of items from before and within visible items")
1110 << 20.0 * 3; // adjust for the 3 items removed before the visible
1112 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1115 << 20.0 * 4; // adjust for the 3 items removed before the visible
1118 QTest::newRow("remove 1, from start of visible, content at start")
1123 QTest::newRow("remove multiple, from start of visible, content at start")
1128 QTest::newRow("remove 1, from start of visible, content not at start")
1129 << 80.0 // show 4-19
1133 QTest::newRow("remove multiple, from start of visible, content not at start")
1134 << 80.0 // show 4-19
1139 QTest::newRow("remove 1, from middle of visible, content at start")
1144 QTest::newRow("remove multiple, from middle of visible, content at start")
1149 QTest::newRow("remove 1, from middle of visible, content not at start")
1150 << 80.0 // show 4-19
1154 QTest::newRow("remove multiple, from middle of visible, content not at start")
1155 << 80.0 // show 4-19
1160 QTest::newRow("remove 1, after visible, content at start")
1165 QTest::newRow("remove multiple, after visible, content at start")
1170 QTest::newRow("remove 1, after visible, content not at middle")
1171 << 80.0 // show 4-19
1175 QTest::newRow("remove multiple, after visible, content not at start")
1176 << 80.0 // show 4-19
1180 QTest::newRow("remove multiple, mix of items from within and after visible items")
1187 void tst_QQuickListView::clear(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1189 QQuickView *window = createView();
1192 for (int i = 0; i < 30; i++)
1193 model.addItem("Item" + QString::number(i), "");
1195 QQmlContext *ctxt = window->rootContext();
1196 ctxt->setContextProperty("testModel", &model);
1198 TestObject *testObject = new TestObject;
1199 ctxt->setContextProperty("testObject", testObject);
1201 window->setSource(source);
1203 qApp->processEvents();
1205 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1206 QTRY_VERIFY(listview != 0);
1207 QQuickItem *contentItem = listview->contentItem();
1208 QTRY_VERIFY(contentItem != 0);
1210 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1211 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1215 QTRY_COMPARE(findItems<QQuickListView>(contentItem, "wrapper").count(), 0);
1216 QTRY_VERIFY(listview->count() == 0);
1217 QTRY_VERIFY(listview->currentItem() == 0);
1218 if (verticalLayoutDirection == QQuickItemView::TopToBottom)
1219 QTRY_COMPARE(listview->contentY(), 0.0);
1221 QTRY_COMPARE(listview->contentY(), -listview->height());
1222 QVERIFY(listview->currentIndex() == -1);
1224 QCOMPARE(listview->contentHeight(), 0.0);
1226 // confirm sanity when adding an item to cleared list
1227 model.addItem("New", "1");
1228 QTRY_VERIFY(listview->count() == 1);
1229 QVERIFY(listview->currentItem() != 0);
1230 QVERIFY(listview->currentIndex() == 0);
1237 void tst_QQuickListView::moved(const QUrl &source, QQuickItemView::VerticalLayoutDirection verticalLayoutDirection)
1239 QFETCH(qreal, contentY);
1243 QFETCH(qreal, itemsOffsetAfterMove);
1247 QQuickView *window = getView();
1250 for (int i = 0; i < 30; i++)
1251 model.addItem("Item" + QString::number(i), "");
1253 QQmlContext *ctxt = window->rootContext();
1254 ctxt->setContextProperty("testModel", &model);
1256 TestObject *testObject = new TestObject;
1257 ctxt->setContextProperty("testObject", testObject);
1259 window->setSource(source);
1261 QVERIFY(QTest::qWaitForWindowExposed(window));
1263 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1264 QTRY_VERIFY(listview != 0);
1265 QQuickItem *contentItem = listview->contentItem();
1266 QTRY_VERIFY(contentItem != 0);
1268 // always need to wait for view to be painted before the first move()
1269 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1271 bool waitForPolish = (contentY != 0);
1272 if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
1273 listview->setVerticalLayoutDirection(verticalLayoutDirection);
1274 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1275 contentY = -listview->height() - contentY;
1277 listview->setContentY(contentY);
1279 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1281 model.moveItems(from, to, count);
1282 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1284 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1285 int firstVisibleIndex = -1;
1286 for (int i=0; i<items.count(); i++) {
1287 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1288 if (item && delegateVisible(item)) {
1289 firstVisibleIndex = i;
1293 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1295 // Confirm items positioned correctly and indexes correct
1296 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1297 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1298 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1299 qreal pos = i*20.0 + itemsOffsetAfterMove;
1300 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
1301 pos = -item->height() - pos;
1302 QTRY_COMPARE(item->y(), pos);
1303 name = findItem<QQuickText>(contentItem, "textName", i);
1305 QTRY_COMPARE(name->text(), model.name(i));
1306 number = findItem<QQuickText>(contentItem, "textNumber", i);
1307 QVERIFY(number != 0);
1308 QTRY_COMPARE(number->text(), model.number(i));
1310 // current index should have been updated
1311 if (item == listview->currentItem())
1312 QTRY_COMPARE(listview->currentIndex(), i);
1315 releaseView(window);
1319 void tst_QQuickListView::moved_data()
1321 QTest::addColumn<qreal>("contentY");
1322 QTest::addColumn<int>("from");
1323 QTest::addColumn<int>("to");
1324 QTest::addColumn<int>("count");
1325 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1327 // model starts with 30 items, each 20px high, in area 320px high
1328 // 16 items should be visible at a time
1329 // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved
1331 QTest::newRow("move 1 forwards, within visible items")
1336 QTest::newRow("move 1 forwards, from non-visible -> visible")
1337 << 80.0 // show 4-19
1339 << 20.0; // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement
1341 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1342 << 80.0 // show 4-19
1344 << 20.0; // first item has moved to below item4, everything drops down by size of 1 item
1346 QTest::newRow("move 1 forwards, from visible -> non-visible")
1351 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1357 QTest::newRow("move 1 backwards, within visible items")
1362 QTest::newRow("move 1 backwards, within visible items (to first index)")
1367 QTest::newRow("move 1 backwards, from non-visible -> visible")
1372 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1377 QTest::newRow("move 1 backwards, from visible -> non-visible")
1378 << 80.0 // show 4-19
1380 << -20.0; // to minimize movement, item 0 moves to -20, and other items do not move
1382 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1383 << 80.0 // show 4-19
1385 << -20.0; // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move
1388 QTest::newRow("move multiple forwards, within visible items")
1393 QTest::newRow("move multiple forwards, before visible items")
1394 << 140.0 // show 7-22
1395 << 4 << 5 << 3 // 4,5,6 move to below 7
1396 << 20.0 * 3; // 4,5,6 moved down
1398 QTest::newRow("move multiple forwards, from non-visible -> visible")
1399 << 80.0 // show 4-19
1401 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1403 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1404 << 80.0 // show 4-19
1406 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1408 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1411 << 20.0; // item 1,2 are removed, item 3 is now first visible
1413 QTest::newRow("move multiple forwards, to bottom of view")
1418 QTest::newRow("move multiple forwards, to bottom of view, first->last")
1423 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1428 QTest::newRow("move multiple forwards, from visible -> non-visible")
1433 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1439 QTest::newRow("move multiple backwards, within visible items")
1444 QTest::newRow("move multiple backwards, within visible items (move first item)")
1449 QTest::newRow("move multiple backwards, from non-visible -> visible")
1454 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1459 QTest::newRow("move multiple backwards, from visible -> non-visible")
1460 << 80.0 // show 4-19
1462 << -20.0 * 3; // to minimize movement, 0 moves by -60, and other items do not move
1464 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1465 << 80.0 // show 4-19
1467 << -20.0 * 3; // to minimize movement, 16,17,18 move to above item 0, and other items do not move
1470 void tst_QQuickListView::multipleChanges(bool condensed)
1472 QFETCH(int, startCount);
1473 QFETCH(QList<ListChange>, changes);
1474 QFETCH(int, newCount);
1475 QFETCH(int, newCurrentIndex);
1477 QQuickView *window = getView();
1480 for (int i = 0; i < startCount; i++)
1481 model.addItem("Item" + QString::number(i), "");
1483 QQmlContext *ctxt = window->rootContext();
1484 ctxt->setContextProperty("testModel", &model);
1486 TestObject *testObject = new TestObject;
1487 ctxt->setContextProperty("testObject", testObject);
1489 window->setSource(testFileUrl("listviewtest.qml"));
1491 QVERIFY(QTest::qWaitForWindowExposed(window));
1493 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1494 QTRY_VERIFY(listview != 0);
1495 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1497 for (int i=0; i<changes.count(); i++) {
1498 switch (changes[i].type) {
1499 case ListChange::Inserted:
1501 QList<QPair<QString, QString> > items;
1502 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1503 items << qMakePair(QString("new item %1").arg(j), QString::number(j));
1504 model.insertItems(changes[i].index, items);
1507 case ListChange::Removed:
1508 model.removeItems(changes[i].index, changes[i].count);
1510 case ListChange::Moved:
1511 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1513 case ListChange::SetCurrent:
1514 listview->setCurrentIndex(changes[i].index);
1516 case ListChange::SetContentY:
1517 listview->setContentY(changes[i].pos);
1523 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1526 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1528 QCOMPARE(listview->count(), newCount);
1529 QCOMPARE(listview->count(), model.count());
1530 QCOMPARE(listview->currentIndex(), newCurrentIndex);
1534 QQuickItem *contentItem = listview->contentItem();
1535 QTRY_VERIFY(contentItem != 0);
1536 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1537 for (int i=0; i < model.count() && i < itemCount; ++i) {
1538 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1539 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1540 name = findItem<QQuickText>(contentItem, "textName", i);
1542 QTRY_COMPARE(name->text(), model.name(i));
1543 number = findItem<QQuickText>(contentItem, "textNumber", i);
1544 QVERIFY(number != 0);
1545 QTRY_COMPARE(number->text(), model.number(i));
1549 releaseView(window);
1552 void tst_QQuickListView::multipleChanges_data()
1554 QTest::addColumn<int>("startCount");
1555 QTest::addColumn<QList<ListChange> >("changes");
1556 QTest::addColumn<int>("newCount");
1557 QTest::addColumn<int>("newCurrentIndex");
1559 QList<ListChange> changes;
1561 for (int i=1; i<30; i++)
1562 changes << ListChange::remove(0);
1563 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1565 changes << ListChange::remove(0);
1566 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1569 changes << ListChange::setCurrent(29);
1570 for (int i=29; i>0; i--)
1571 changes << ListChange::remove(i);
1572 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1574 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1575 << ListChange::remove(0, 1)
1576 << ListChange::insert(0, 1)
1579 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1580 << ListChange::setCurrent(2)
1581 << ListChange::remove(2, 1)
1582 << ListChange::insert(2, 1)
1585 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1586 << ListChange::setCurrent(1)
1587 << ListChange::remove(1, 3)
1588 << ListChange::insert(2, 2)
1591 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1592 << ListChange::setCurrent(2)
1593 << ListChange::remove(1, 3)
1594 << ListChange::move(1, 5, 1)
1597 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1598 << ListChange::setCurrent(5)
1599 << ListChange::remove(4, 3)
1600 << ListChange::move(4, 1, 1)
1604 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1605 << ListChange::insert(0, 2)
1606 << ListChange::insert(0, 4)
1607 << ListChange::insert(0, 6)
1610 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1611 << ListChange::insert(0, 2)
1612 << ListChange::insert(0, 4)
1613 << ListChange::insert(0, 6)
1614 << ListChange::setCurrent(3)
1615 << ListChange::insert(3, 2)
1618 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1619 << ListChange::insert(0, 30)
1620 << ListChange::remove(0, 30)
1623 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1624 << ListChange::insert(1)
1625 << ListChange::setCurrent(1)
1626 << ListChange::remove(1)
1629 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1630 << ListChange::insert(0, 10)
1631 << ListChange::remove(5, 10)
1634 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1635 << ListChange::insert(0, 3)
1636 << ListChange::move(0, 10, 3)
1639 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1640 << ListChange::insert(0, 3)
1641 << ListChange::move(0, 8, 5)
1644 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1645 << ListChange::setCurrent(9)
1646 << ListChange::insert(10, 3)
1647 << ListChange::move(8, 0, 5)
1651 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1652 << ListChange::setCurrent(1)
1653 << ListChange::move(1, 2, 2)
1654 << ListChange::move(2, 1, 2)
1657 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1658 << ListChange::setCurrent(2)
1659 << ListChange::move(1, 2, 3)
1660 << ListChange::move(3, 0, 5)
1663 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1664 << ListChange::setCurrent(5)
1665 << ListChange::move(5, 0, 1)
1666 << ListChange::remove(0)
1669 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1670 << ListChange::setCurrent(5)
1671 << ListChange::move(5, 0, 1)
1672 << ListChange::insert(0)
1675 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1676 << ListChange::setCurrent(1)
1677 << ListChange::move(5, 1, 3)
1678 << ListChange::remove(1, 3)
1681 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1682 << ListChange::setCurrent(5)
1683 << ListChange::move(5, 1, 3)
1684 << ListChange::insert(1, 5)
1687 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1688 << ListChange::setCurrent(3)
1689 << ListChange::move(0, 1, 2)
1690 << ListChange::insert(3, 5)
1693 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1694 << ListChange::insert(0, 5)
1695 << ListChange::setCurrent(-1)
1696 << ListChange::remove(0, 5)
1697 << ListChange::insert(0, 5)
1700 QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1701 << ListChange::remove(20, 5)
1702 << ListChange::setContentY(20)
1705 QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1706 << ListChange::insert(9, 5)
1707 << ListChange::setContentY(20)
1710 QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1711 << ListChange::move(15, 8, 3)
1712 << ListChange::setContentY(0)
1715 QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1716 << ListChange::setContentY(20)
1717 << ListChange::remove(0, 30)
1718 << ListChange::insert(0, 2)
1719 << ListChange::setContentY(0)
1723 void tst_QQuickListView::swapWithFirstItem()
1725 QQuickView *window = createView();
1728 for (int i = 0; i < 30; i++)
1729 model.addItem("Item" + QString::number(i), "");
1731 QQmlContext *ctxt = window->rootContext();
1732 ctxt->setContextProperty("testModel", &model);
1734 TestObject *testObject = new TestObject;
1735 ctxt->setContextProperty("testObject", testObject);
1737 window->setSource(testFileUrl("listviewtest.qml"));
1739 qApp->processEvents();
1741 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1742 QTRY_VERIFY(listview != 0);
1743 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1745 // ensure content position is stable
1746 listview->setContentY(0);
1747 model.moveItem(1, 0);
1748 QTRY_VERIFY(listview->contentY() == 0);
1754 void tst_QQuickListView::enforceRange()
1756 QQuickView *window = createView();
1759 for (int i = 0; i < 30; i++)
1760 model.addItem("Item" + QString::number(i), "");
1762 QQmlContext *ctxt = window->rootContext();
1763 ctxt->setContextProperty("testModel", &model);
1765 window->setSource(testFileUrl("listview-enforcerange.qml"));
1767 qApp->processEvents();
1769 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1770 QTRY_VERIFY(listview != 0);
1772 QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0);
1773 QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0);
1774 QTRY_COMPARE(listview->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
1775 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1777 QQuickItem *contentItem = listview->contentItem();
1778 QTRY_VERIFY(contentItem != 0);
1780 // view should be positioned at the top of the range.
1781 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
1783 QTRY_COMPARE(listview->contentY(), -100.0);
1785 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1786 QTRY_VERIFY(name != 0);
1787 QTRY_COMPARE(name->text(), model.name(0));
1788 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
1789 QTRY_VERIFY(number != 0);
1790 QTRY_COMPARE(number->text(), model.number(0));
1792 // Check currentIndex is updated when contentItem moves
1793 listview->setContentY(20);
1795 QTRY_COMPARE(listview->currentIndex(), 6);
1799 for (int i = 0; i < 5; i++)
1800 model2.addItem("Item" + QString::number(i), "");
1802 ctxt->setContextProperty("testModel", &model2);
1803 QCOMPARE(listview->count(), 5);
1808 void tst_QQuickListView::enforceRange_withoutHighlight()
1811 // If no highlight is set but StrictlyEnforceRange is used, the content should still move
1812 // to the correct position (i.e. to the next/previous item, not next/previous section)
1813 // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex()
1815 QQuickView *window = createView();
1818 model.addItem("Item 0", "a");
1819 model.addItem("Item 1", "b");
1820 model.addItem("Item 2", "b");
1821 model.addItem("Item 3", "c");
1823 QQmlContext *ctxt = window->rootContext();
1824 ctxt->setContextProperty("testModel", &model);
1826 window->setSource(testFileUrl("listview-enforcerange-nohighlight.qml"));
1828 QTest::qWaitForWindowExposed(window);
1830 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1831 QTRY_VERIFY(listview != 0);
1832 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1834 qreal expectedPos = -100.0;
1836 expectedPos += 10.0; // scroll past 1st section's delegate (10px height)
1837 QTRY_COMPARE(listview->contentY(), expectedPos);
1839 expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section
1840 QTest::keyClick(window, Qt::Key_Down);
1842 QTRY_COMPARE(listview->contentY(), expectedPos);
1844 expectedPos += 20; // scroll past 1st item of 2nd section
1845 QTest::keyClick(window, Qt::Key_Down);
1846 QTRY_COMPARE(listview->contentY(), expectedPos);
1848 expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section
1849 QTest::keyClick(window, Qt::Key_Down);
1850 QTRY_COMPARE(listview->contentY(), expectedPos);
1855 void tst_QQuickListView::spacing()
1857 QQuickView *window = createView();
1860 for (int i = 0; i < 30; i++)
1861 model.addItem("Item" + QString::number(i), "");
1863 QQmlContext *ctxt = window->rootContext();
1864 ctxt->setContextProperty("testModel", &model);
1866 TestObject *testObject = new TestObject;
1867 ctxt->setContextProperty("testObject", testObject);
1869 window->setSource(testFileUrl("listviewtest.qml"));
1871 qApp->processEvents();
1873 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1874 QTRY_VERIFY(listview != 0);
1876 QQuickItem *contentItem = listview->contentItem();
1877 QTRY_VERIFY(contentItem != 0);
1878 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1880 // Confirm items positioned correctly
1881 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1882 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1883 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1884 if (!item) qWarning() << "Item" << i << "not found";
1886 QTRY_VERIFY(item->y() == i*20);
1889 listview->setSpacing(10);
1890 QTRY_VERIFY(listview->spacing() == 10);
1892 // Confirm items positioned correctly
1893 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 11);
1894 for (int i = 0; i < 11; ++i) {
1895 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1896 if (!item) qWarning() << "Item" << i << "not found";
1898 QTRY_VERIFY(item->y() == i*30);
1901 listview->setSpacing(0);
1903 // Confirm items positioned correctly
1904 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() >= 16);
1905 for (int i = 0; i < 16; ++i) {
1906 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1907 if (!item) qWarning() << "Item" << i << "not found";
1909 QTRY_COMPARE(item->y(), i*20.0);
1916 template <typename T>
1917 void tst_QQuickListView::sections(const QUrl &source)
1919 QQuickView *window = createView();
1922 for (int i = 0; i < 30; i++)
1923 model.addItem("Item" + QString::number(i), QString::number(i/5));
1925 QQmlContext *ctxt = window->rootContext();
1926 ctxt->setContextProperty("testModel", &model);
1928 window->setSource(source);
1930 qApp->processEvents();
1932 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
1933 QTRY_VERIFY(listview != 0);
1935 QQuickItem *contentItem = listview->contentItem();
1936 QTRY_VERIFY(contentItem != 0);
1938 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1940 // Confirm items positioned correctly
1941 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1942 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1943 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1945 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20));
1946 QQuickText *next = findItem<QQuickText>(item, "nextSection");
1947 QCOMPARE(next->text().toInt(), (i+1)/5);
1950 QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1952 // Remove section boundary
1953 model.removeItem(5);
1954 QTRY_COMPARE(listview->count(), model.count());
1956 // New section header created
1957 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1959 QTRY_COMPARE(item->height(), 40.0);
1961 model.insertItem(3, "New Item", "0");
1962 QTRY_COMPARE(listview->count(), model.count());
1964 // Section header moved
1965 item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1967 QTRY_COMPARE(item->height(), 20.0);
1969 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1971 QTRY_COMPARE(item->height(), 40.0);
1973 // insert item which will become a section header
1974 model.insertItem(6, "Replace header", "1");
1975 QTRY_COMPARE(listview->count(), model.count());
1977 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1979 QTRY_COMPARE(item->height(), 40.0);
1981 item = findItem<QQuickItem>(contentItem, "wrapper", 7);
1983 QTRY_COMPARE(item->height(), 20.0);
1985 QTRY_COMPARE(listview->currentSection(), QString("0"));
1987 listview->setContentY(140);
1988 QTRY_COMPARE(listview->currentSection(), QString("1"));
1990 QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
1992 listview->setContentY(20);
1993 QTRY_COMPARE(listview->currentSection(), QString("0"));
1995 QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
1997 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
1999 QTRY_COMPARE(item->height(), 20.0);
2001 // check that headers change when item changes
2002 listview->setContentY(0);
2003 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2004 model.modifyItem(0, "changed", "2");
2005 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2007 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
2009 QTRY_COMPARE(item->height(), 40.0);
2014 void tst_QQuickListView::sectionsDelegate()
2016 QQuickView *window = createView();
2019 for (int i = 0; i < 30; i++)
2020 model.addItem("Item" + QString::number(i), QString::number(i/5));
2022 QQmlContext *ctxt = window->rootContext();
2023 ctxt->setContextProperty("testModel", &model);
2025 window->setSource(testFileUrl("listview-sections_delegate.qml"));
2027 qApp->processEvents();
2029 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2030 QTRY_VERIFY(listview != 0);
2032 QQuickItem *contentItem = listview->contentItem();
2033 QTRY_VERIFY(contentItem != 0);
2035 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2037 // Confirm items positioned correctly
2038 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2039 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2040 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2042 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20));
2043 QQuickText *next = findItem<QQuickText>(item, "nextSection");
2044 QCOMPARE(next->text().toInt(), (i+1)/5);
2047 for (int i = 0; i < 3; ++i) {
2048 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2050 QTRY_COMPARE(item->y(), qreal(i*20*6));
2054 model.modifyItem(0, "One", "aaa");
2055 model.modifyItem(1, "Two", "aaa");
2056 model.modifyItem(2, "Three", "aaa");
2057 model.modifyItem(3, "Four", "aaa");
2058 model.modifyItem(4, "Five", "aaa");
2059 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2061 for (int i = 0; i < 3; ++i) {
2062 QQuickItem *item = findItem<QQuickItem>(contentItem,
2063 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2065 QTRY_COMPARE(item->y(), qreal(i*20*6));
2068 // remove section boundary
2069 model.removeItem(5);
2070 QTRY_COMPARE(listview->count(), model.count());
2071 for (int i = 0; i < 3; ++i) {
2072 QQuickItem *item = findItem<QQuickItem>(contentItem,
2073 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2078 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "sect_1");
2079 QCOMPARE(items.count(), 1);
2082 model.modifyItem(0, "One", "aaa");
2083 model.modifyItem(1, "One", "aaa");
2084 model.modifyItem(2, "One", "aaa");
2085 model.modifyItem(3, "Four", "aaa");
2086 model.modifyItem(4, "Four", "aaa");
2087 model.modifyItem(5, "Four", "aaa");
2088 model.modifyItem(6, "Five", "aaa");
2089 model.modifyItem(7, "Five", "aaa");
2090 model.modifyItem(8, "Five", "aaa");
2091 model.modifyItem(9, "Two", "aaa");
2092 model.modifyItem(10, "Two", "aaa");
2093 model.modifyItem(11, "Two", "aaa");
2094 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2095 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_aaa").count(), 1);
2096 window->rootObject()->setProperty("sectionProperty", "name");
2097 // ensure view has settled.
2098 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_Four").count(), 1);
2099 for (int i = 0; i < 4; ++i) {
2100 QQuickItem *item = findItem<QQuickItem>(contentItem,
2101 "sect_" + model.name(i*3));
2103 QTRY_COMPARE(item->y(), qreal(i*20*4));
2109 void tst_QQuickListView::sectionsDragOutsideBounds_data()
2111 QTest::addColumn<int>("distance");
2112 QTest::addColumn<int>("cacheBuffer");
2114 QTest::newRow("500, no cache buffer") << 500 << 0;
2115 QTest::newRow("1000, no cache buffer") << 1000 << 0;
2116 QTest::newRow("500, cache buffer") << 500 << 320;
2117 QTest::newRow("1000, cache buffer") << 1000 << 320;
2120 void tst_QQuickListView::sectionsDragOutsideBounds()
2122 QFETCH(int, distance);
2123 QFETCH(int, cacheBuffer);
2125 QQuickView *window = getView();
2128 for (int i = 0; i < 10; i++)
2129 model.addItem("Item" + QString::number(i), QString::number(i/5));
2131 QQmlContext *ctxt = window->rootContext();
2132 ctxt->setContextProperty("testModel", &model);
2134 window->setSource(testFileUrl("listview-sections_delegate.qml"));
2136 qApp->processEvents();
2138 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2139 QTRY_VERIFY(listview != 0);
2140 listview->setCacheBuffer(cacheBuffer);
2142 QQuickItem *contentItem = listview->contentItem();
2143 QTRY_VERIFY(contentItem != 0);
2145 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2148 // Drag view up beyond bounds
2149 QTest::mousePress(window, Qt::LeftButton, 0, QPoint(20,20));
2150 QTest::mouseMove(window, QPoint(20,0));
2151 QTest::mouseMove(window, QPoint(20,-50));
2152 QTest::mouseMove(window, QPoint(20,-distance));
2153 QTest::mouseRelease(window, Qt::LeftButton, 0, QPoint(20,-distance));
2154 // view should settle back at 0
2155 QTRY_COMPARE(listview->contentY(), 0.0);
2157 QTest::mousePress(window, Qt::LeftButton, 0, QPoint(20,0));
2158 QTest::mouseMove(window, QPoint(20,20));
2159 QTest::mouseMove(window, QPoint(20,70));
2160 QTest::mouseMove(window, QPoint(20,distance));
2162 QTest::mouseRelease(window, Qt::LeftButton, 0, QPoint(20,distance));
2163 // view should settle back at 0
2164 QTRY_COMPARE(listview->contentY(), 0.0);
2166 releaseView(window);
2169 void tst_QQuickListView::sectionsDelegate_headerVisibility()
2171 QSKIP("QTBUG-24395");
2173 QQuickView *window = createView();
2176 for (int i = 0; i < 30; i++)
2177 model.addItem("Item" + QString::number(i), QString::number(i/5));
2179 window->rootContext()->setContextProperty("testModel", &model);
2180 window->setSource(testFileUrl("listview-sections_delegate.qml"));
2182 window->requestActivateWindow();
2183 QTest::qWaitForWindowActive(window);
2185 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2186 QTRY_VERIFY(listview != 0);
2188 QQuickItem *contentItem = listview->contentItem();
2189 QTRY_VERIFY(contentItem != 0);
2190 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2192 // ensure section header is maintained in view
2193 listview->setCurrentIndex(20);
2194 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2195 QTRY_VERIFY(qFuzzyCompare(listview->contentY(), 200.0));
2196 QTRY_VERIFY(listview->isMoving() == false);
2197 listview->setCurrentIndex(0);
2198 QTRY_VERIFY(qFuzzyIsNull(listview->contentY()));
2203 void tst_QQuickListView::sectionsPositioning()
2205 QQuickView *window = createView();
2208 for (int i = 0; i < 30; i++)
2209 model.addItem("Item" + QString::number(i), QString::number(i/5));
2211 QQmlContext *ctxt = window->rootContext();
2212 ctxt->setContextProperty("testModel", &model);
2214 window->setSource(testFileUrl("listview-sections_delegate.qml"));
2216 qApp->processEvents();
2217 window->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2219 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2220 QTRY_VERIFY(listview != 0);
2221 QQuickItem *contentItem = listview->contentItem();
2222 QTRY_VERIFY(contentItem != 0);
2223 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2225 for (int i = 0; i < 3; ++i) {
2226 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2228 QTRY_COMPARE(item->y(), qreal(i*20*6));
2231 QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header
2233 QCOMPARE(topItem->y(), 0.);
2235 QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2236 QVERIFY(bottomItem);
2237 QCOMPARE(bottomItem->y(), 300.);
2239 // move down a little and check that section header is at top
2240 listview->setContentY(10);
2241 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2242 QCOMPARE(topItem->y(), 0.);
2244 // push the top header up
2245 listview->setContentY(110);
2246 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2247 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2249 QCOMPARE(topItem->y(), 100.);
2251 QQuickItem *item = findVisibleChild(contentItem, "sect_1");
2253 QCOMPARE(item->y(), 120.);
2255 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2256 QVERIFY(bottomItem);
2257 QCOMPARE(bottomItem->y(), 410.);
2259 // Move past section 0
2260 listview->setContentY(120);
2261 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2262 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2265 // Push section footer down
2266 listview->setContentY(70);
2267 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2268 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2269 QVERIFY(bottomItem);
2270 QCOMPARE(bottomItem->y(), 380.);
2272 // Change current section, and verify case insensitive comparison
2273 listview->setContentY(10);
2274 model.modifyItem(0, "One", "aaa");
2275 model.modifyItem(1, "Two", "AAA");
2276 model.modifyItem(2, "Three", "aAa");
2277 model.modifyItem(3, "Four", "aaA");
2278 model.modifyItem(4, "Five", "Aaa");
2279 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2281 QTRY_COMPARE(listview->currentSection(), QString("aaa"));
2283 for (int i = 0; i < 3; ++i) {
2284 QQuickItem *item = findItem<QQuickItem>(contentItem,
2285 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2287 QTRY_COMPARE(item->y(), qreal(i*20*6));
2290 QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header
2291 QCOMPARE(topItem->y(), 10.);
2293 // remove section boundary
2294 listview->setContentY(120);
2295 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2296 model.removeItem(5);
2297 QTRY_COMPARE(listview->count(), model.count());
2298 for (int i = 1; i < 3; ++i) {
2299 QQuickItem *item = findVisibleChild(contentItem,
2300 "sect_" + QString::number(i));
2302 QTRY_COMPARE(item->y(), qreal(i*20*6));
2305 QVERIFY(topItem = findVisibleChild(contentItem, "sect_1"));
2306 QTRY_COMPARE(topItem->y(), 120.);
2308 // Change the next section
2309 listview->setContentY(0);
2310 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2311 bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2312 QVERIFY(bottomItem);
2313 QTRY_COMPARE(bottomItem->y(), 300.);
2315 model.modifyItem(14, "New", "new");
2316 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2318 QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer
2319 QTRY_COMPARE(bottomItem->y(), 300.);
2321 // delegate size increase should push section footer down
2322 listview->setContentY(70);
2323 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2324 QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_3")); // section footer
2325 QTRY_COMPARE(bottomItem->y(), 370.);
2326 QQuickItem *inlineSection = findVisibleChild(contentItem, "sect_new");
2327 item = findItem<QQuickItem>(contentItem, "wrapper", 13);
2329 item->setHeight(40.);
2330 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2331 QTRY_COMPARE(bottomItem->y(), 380.);
2332 QCOMPARE(inlineSection->y(), 360.);
2333 item->setHeight(20.);
2335 // Turn sticky footer off
2336 listview->setContentY(20);
2337 window->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2338 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2339 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2340 QCOMPARE(item->y(), 340.);
2342 // Turn sticky header off
2343 listview->setContentY(30);
2344 window->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2345 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2346 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2347 QCOMPARE(item->y(), 0.);
2349 // if an empty model is set the header/footer should be cleaned up
2350 window->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2351 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2352 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2354 ctxt->setContextProperty("testModel", &model1);
2355 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2356 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2358 // clear model - header/footer should be cleaned up
2359 ctxt->setContextProperty("testModel", &model);
2360 QTRY_VERIFY(findVisibleChild(contentItem, "sect_aaa")); // section header
2361 QTRY_VERIFY(findVisibleChild(contentItem, "sect_new")); // section footer
2363 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_aaa")); // section header
2364 QTRY_VERIFY(!findVisibleChild(contentItem, "sect_new")); // section footer
2369 void tst_QQuickListView::sectionPropertyChange()
2371 QQuickView *window = createView();
2373 window->setSource(testFileUrl("sectionpropertychange.qml"));
2375 qApp->processEvents();
2377 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2378 QTRY_VERIFY(listview != 0);
2380 QQuickItem *contentItem = listview->contentItem();
2381 QTRY_VERIFY(contentItem != 0);
2383 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2385 // Confirm items positioned correctly
2386 for (int i = 0; i < 2; ++i) {
2387 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2389 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2392 QMetaObject::invokeMethod(window->rootObject(), "switchGroups");
2393 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2395 // Confirm items positioned correctly
2396 for (int i = 0; i < 2; ++i) {
2397 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2399 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2402 QMetaObject::invokeMethod(window->rootObject(), "switchGroups");
2403 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2405 // Confirm items positioned correctly
2406 for (int i = 0; i < 2; ++i) {
2407 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2409 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2412 QMetaObject::invokeMethod(window->rootObject(), "switchGrouped");
2413 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2415 // Confirm items positioned correctly
2416 for (int i = 0; i < 2; ++i) {
2417 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2419 QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2422 QMetaObject::invokeMethod(window->rootObject(), "switchGrouped");
2423 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2425 // Confirm items positioned correctly
2426 for (int i = 0; i < 2; ++i) {
2427 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2429 QTRY_COMPARE(item->y(), qreal(25. + i*75.));
2435 void tst_QQuickListView::sectionDelegateChange()
2437 QQuickView *window = createView();
2439 window->setSource(testFileUrl("sectiondelegatechange.qml"));
2441 qApp->processEvents();
2443 QQuickListView *listview = qobject_cast<QQuickListView *>(window->rootObject());
2444 QVERIFY(listview != 0);
2446 QQuickItem *contentItem = listview->contentItem();
2447 QVERIFY(contentItem != 0);
2449 QQUICK_VERIFY_POLISH(listview);
2451 QVERIFY(findItems<QQuickItem>(contentItem, "section1").count() > 0);
2452 QCOMPARE(findItems<QQuickItem>(contentItem, "section2").count(), 0);
2454 for (int i = 0; i < 3; ++i) {
2455 QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2457 QTRY_COMPARE(item->y(), qreal(25. + i*50.));
2460 QMetaObject::invokeMethod(window->rootObject(), "switchDelegates");
2461 QQUICK_VERIFY_POLISH(listview);
2463 QCOMPARE(findItems<QQuickItem>(contentItem, "section1").count(), 0);
2464 QVERIFY(findItems<QQuickItem>(contentItem, "section2").count() > 0);
2466 for (int i = 0; i < 3; ++i) {
2467 QQuickItem *item = findItem<QQuickItem>(contentItem, "item", i);
2469 QTRY_COMPARE(item->y(), qreal(50. + i*75.));
2475 void tst_QQuickListView::currentIndex_delayedItemCreation()
2477 QFETCH(bool, setCurrentToZero);
2479 QQuickView *window = getView();
2481 // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2482 // (since the currentItem will have changed and that shares the same index)
2483 window->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2485 window->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2486 qApp->processEvents();
2488 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2489 QTRY_VERIFY(listview != 0);
2490 QQuickItem *contentItem = listview->contentItem();
2491 QTRY_VERIFY(contentItem != 0);
2493 QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2494 QCOMPARE(listview->currentIndex(), 0);
2495 QTRY_COMPARE(spy.count(), 1);
2497 releaseView(window);
2500 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2502 QTest::addColumn<bool>("setCurrentToZero");
2504 QTest::newRow("set to 0") << true;
2505 QTest::newRow("don't set to 0") << false;
2508 void tst_QQuickListView::currentIndex()
2510 QaimModel initModel;
2512 for (int i = 0; i < 30; i++)
2513 initModel.addItem("Item" + QString::number(i), QString::number(i));
2515 QQuickView *window = new QQuickView(0);
2516 window->setGeometry(0,0,240,320);
2518 QQmlContext *ctxt = window->rootContext();
2519 ctxt->setContextProperty("testModel", &initModel);
2520 ctxt->setContextProperty("testWrap", QVariant(false));
2522 QString filename(testFile("listview-initCurrent.qml"));
2523 window->setSource(QUrl::fromLocalFile(filename));
2525 qApp->processEvents();
2527 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2528 QTRY_VERIFY(listview != 0);
2529 QQuickItem *contentItem = listview->contentItem();
2530 QTRY_VERIFY(contentItem != 0);
2531 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2533 // currentIndex is initialized to 20
2534 // currentItem should be in view
2535 QCOMPARE(listview->currentIndex(), 20);
2536 QCOMPARE(listview->contentY(), 100.0);
2537 QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2538 QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2540 // changing model should reset currentIndex to 0
2542 for (int i = 0; i < 30; i++)
2543 model.addItem("Item" + QString::number(i), QString::number(i));
2544 ctxt->setContextProperty("testModel", &model);
2546 QCOMPARE(listview->currentIndex(), 0);
2547 QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2549 // confirm that the velocity is updated
2550 listview->setCurrentIndex(20);
2551 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2552 listview->setCurrentIndex(0);
2553 QTRY_VERIFY(listview->verticalVelocity() == 0.0);
2555 // footer should become visible if it is out of view, and then current index is set to count-1
2556 window->rootObject()->setProperty("showFooter", true);
2557 QTRY_VERIFY(listview->footerItem());
2558 listview->setCurrentIndex(model.count()-2);
2559 QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2560 listview->setCurrentIndex(model.count()-1);
2561 QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2562 window->rootObject()->setProperty("showFooter", false);
2564 // header should become visible if it is out of view, and then current index is set to 0
2565 window->rootObject()->setProperty("showHeader", true);
2566 QTRY_VERIFY(listview->headerItem());
2567 listview->setCurrentIndex(1);
2568 QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2569 listview->setCurrentIndex(0);
2570 QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2571 window->rootObject()->setProperty("showHeader", false);
2573 // turn off auto highlight
2574 listview->setHighlightFollowsCurrentItem(false);
2575 QVERIFY(listview->highlightFollowsCurrentItem() == false);
2577 QVERIFY(listview->highlightItem());
2578 qreal hlPos = listview->highlightItem()->y();
2580 listview->setCurrentIndex(4);
2581 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2583 // insert item before currentIndex
2584 listview->setCurrentIndex(28);
2585 model.insertItem(0, "Foo", "1111");
2586 QTRY_COMPARE(window->rootObject()->property("current").toInt(), 29);
2588 // check removing highlight by setting currentIndex to -1;
2589 listview->setCurrentIndex(-1);
2591 QCOMPARE(listview->currentIndex(), -1);
2592 QVERIFY(!listview->highlightItem());
2593 QVERIFY(!listview->currentItem());
2595 // moving currentItem out of view should make it invisible
2596 listview->setCurrentIndex(0);
2597 QTRY_VERIFY(delegateVisible(listview->currentItem()));
2598 listview->setContentY(200);
2599 QTRY_VERIFY(!delegateVisible(listview->currentItem()));
2604 void tst_QQuickListView::noCurrentIndex()
2607 for (int i = 0; i < 30; i++)
2608 model.addItem("Item" + QString::number(i), QString::number(i));
2610 QQuickView *window = new QQuickView(0);
2611 window->setGeometry(0,0,240,320);
2613 QQmlContext *ctxt = window->rootContext();
2614 ctxt->setContextProperty("testModel", &model);
2616 QString filename(testFile("listview-noCurrent.qml"));
2617 window->setSource(QUrl::fromLocalFile(filename));
2619 qApp->processEvents();
2621 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2622 QTRY_VERIFY(listview != 0);
2623 QQuickItem *contentItem = listview->contentItem();
2624 QTRY_VERIFY(contentItem != 0);
2625 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2627 // current index should be -1 at startup
2628 // and we should not have a currentItem or highlightItem
2629 QCOMPARE(listview->currentIndex(), -1);
2630 QCOMPARE(listview->contentY(), 0.0);
2631 QVERIFY(!listview->highlightItem());
2632 QVERIFY(!listview->currentItem());
2634 listview->setCurrentIndex(2);
2635 QCOMPARE(listview->currentIndex(), 2);
2636 QVERIFY(listview->highlightItem());
2637 QVERIFY(listview->currentItem());
2642 void tst_QQuickListView::keyNavigation()
2644 QFETCH(QQuickListView::Orientation, orientation);
2645 QFETCH(Qt::LayoutDirection, layoutDirection);
2646 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
2647 QFETCH(Qt::Key, forwardsKey);
2648 QFETCH(Qt::Key, backwardsKey);
2649 QFETCH(QPointF, contentPosAtFirstItem);
2650 QFETCH(QPointF, contentPosAtLastItem);
2653 for (int i = 0; i < 30; i++)
2654 model.addItem("Item" + QString::number(i), "");
2656 QQuickView *window = getView();
2657 TestObject *testObject = new TestObject;
2658 window->rootContext()->setContextProperty("testModel", &model);
2659 window->rootContext()->setContextProperty("testObject", testObject);
2660 window->setSource(testFileUrl("listviewtest.qml"));
2662 QTest::qWaitForWindowActive(window);
2664 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2665 QTRY_VERIFY(listview != 0);
2667 listview->setOrientation(orientation);
2668 listview->setLayoutDirection(layoutDirection);
2669 listview->setVerticalLayoutDirection(verticalLayoutDirection);
2670 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2672 window->requestActivateWindow();
2673 QTest::qWaitForWindowActive(window);
2674 QTRY_VERIFY(qGuiApp->focusWindow() == window);
2676 QTest::keyClick(window, forwardsKey);
2677 QCOMPARE(listview->currentIndex(), 1);
2679 QTest::keyClick(window, backwardsKey);
2680 QCOMPARE(listview->currentIndex(), 0);
2682 // hold down a key to go forwards
2683 for (int i=0; i<model.count()-1; i++) {
2684 QTest::simulateEvent(window, true, forwardsKey, Qt::NoModifier, "", true);
2685 QTRY_COMPARE(listview->currentIndex(), i+1);
2687 QTest::keyRelease(window, forwardsKey);
2688 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2689 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2690 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2692 // hold down a key to go backwards
2693 for (int i=model.count()-1; i > 0; i--) {
2694 QTest::simulateEvent(window, true, backwardsKey, Qt::NoModifier, "", true);
2695 QTRY_COMPARE(listview->currentIndex(), i-1);
2697 QTest::keyRelease(window, backwardsKey);
2698 QTRY_COMPARE(listview->currentIndex(), 0);
2699 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2700 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2703 QVERIFY(!listview->isWrapEnabled());
2704 listview->incrementCurrentIndex();
2705 QCOMPARE(listview->currentIndex(), 1);
2706 listview->decrementCurrentIndex();
2707 QCOMPARE(listview->currentIndex(), 0);
2709 listview->decrementCurrentIndex();
2710 QCOMPARE(listview->currentIndex(), 0);
2713 listview->setWrapEnabled(true);
2714 QVERIFY(listview->isWrapEnabled());
2716 listview->decrementCurrentIndex();
2717 QCOMPARE(listview->currentIndex(), model.count()-1);
2718 QTRY_COMPARE(listview->contentX(), contentPosAtLastItem.x());
2719 QTRY_COMPARE(listview->contentY(), contentPosAtLastItem.y());
2721 listview->incrementCurrentIndex();
2722 QCOMPARE(listview->currentIndex(), 0);
2723 QTRY_COMPARE(listview->contentX(), contentPosAtFirstItem.x());
2724 QTRY_COMPARE(listview->contentY(), contentPosAtFirstItem.y());
2726 releaseView(window);
2730 void tst_QQuickListView::keyNavigation_data()
2732 QTest::addColumn<QQuickListView::Orientation>("orientation");
2733 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2734 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2735 QTest::addColumn<Qt::Key>("forwardsKey");
2736 QTest::addColumn<Qt::Key>("backwardsKey");
2737 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2738 QTest::addColumn<QPointF>("contentPosAtLastItem");
2740 QTest::newRow("Vertical, TopToBottom")
2741 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
2742 << Qt::Key_Down << Qt::Key_Up
2744 << QPointF(0, 30*20 - 320);
2746 QTest::newRow("Vertical, BottomToTop")
2747 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
2748 << Qt::Key_Up << Qt::Key_Down
2750 << QPointF(0, -(30 * 20));
2752 QTest::newRow("Horizontal, LeftToRight")
2753 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
2754 << Qt::Key_Right << Qt::Key_Left
2756 << QPointF(30*240 - 240, 0);
2758 QTest::newRow("Horizontal, RightToLeft")
2759 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
2760 << Qt::Key_Left << Qt::Key_Right
2762 << QPointF(-(30 * 240), 0);
2765 void tst_QQuickListView::itemList()
2767 QQuickView *window = createView();
2768 window->setSource(testFileUrl("itemlist.qml"));
2770 qApp->processEvents();
2772 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "view");
2773 QTRY_VERIFY(listview != 0);
2775 QQuickItem *contentItem = listview->contentItem();
2776 QTRY_VERIFY(contentItem != 0);
2778 QQuickVisualItemModel *model = window->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2779 QTRY_VERIFY(model != 0);
2781 QTRY_VERIFY(model->count() == 3);
2782 QTRY_COMPARE(listview->currentIndex(), 0);
2784 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2786 QTRY_COMPARE(item->x(), 0.0);
2787 QCOMPARE(item->height(), listview->height());
2789 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2791 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2793 listview->setCurrentIndex(2);
2795 item = findItem<QQuickItem>(contentItem, "item3");
2797 QTRY_COMPARE(item->x(), 480.0);
2799 text = findItem<QQuickText>(contentItem, "text3");
2801 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2806 void tst_QQuickListView::itemListFlicker()
2808 QQuickView *window = createView();
2809 window->setSource(testFileUrl("itemlist-flicker.qml"));
2811 qApp->processEvents();
2813 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "view");
2814 QTRY_VERIFY(listview != 0);
2816 QQuickItem *contentItem = listview->contentItem();
2817 QTRY_VERIFY(contentItem != 0);
2819 QQuickVisualItemModel *model = window->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2820 QTRY_VERIFY(model != 0);
2822 QTRY_VERIFY(model->count() == 3);
2823 QTRY_COMPARE(listview->currentIndex(), 0);
2827 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2828 QVERIFY(delegateVisible(item));
2829 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2830 QVERIFY(delegateVisible(item));
2831 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2832 QVERIFY(delegateVisible(item));
2834 listview->setCurrentIndex(1);
2836 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2837 QVERIFY(delegateVisible(item));
2838 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2839 QVERIFY(delegateVisible(item));
2840 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2841 QVERIFY(delegateVisible(item));
2843 listview->setCurrentIndex(2);
2845 QVERIFY(item = findItem<QQuickItem>(contentItem, "item1"));
2846 QVERIFY(delegateVisible(item));
2847 QVERIFY(item = findItem<QQuickItem>(contentItem, "item2"));
2848 QVERIFY(delegateVisible(item));
2849 QVERIFY(item = findItem<QQuickItem>(contentItem, "item3"));
2850 QVERIFY(delegateVisible(item));
2855 void tst_QQuickListView::cacheBuffer()
2857 QQuickView *window = createView();
2860 for (int i = 0; i < 90; i++)
2861 model.addItem("Item" + QString::number(i), "");
2863 QQmlContext *ctxt = window->rootContext();
2864 ctxt->setContextProperty("testModel", &model);
2866 TestObject *testObject = new TestObject;
2867 ctxt->setContextProperty("testObject", testObject);
2869 window->setSource(testFileUrl("listviewtest.qml"));
2871 qApp->processEvents();
2873 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2874 QTRY_VERIFY(listview != 0);
2876 QQuickItem *contentItem = listview->contentItem();
2877 QTRY_VERIFY(contentItem != 0);
2878 QTRY_VERIFY(listview->delegate() != 0);
2879 QTRY_VERIFY(listview->model() != 0);
2880 QTRY_VERIFY(listview->highlight() != 0);
2882 // Confirm items positioned correctly
2883 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2884 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2885 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2886 if (!item) qWarning() << "Item" << i << "not found";
2888 QTRY_VERIFY(item->y() == i*20);
2891 QQmlIncubationController controller;
2892 window->engine()->setIncubationController(&controller);
2894 testObject->setCacheBuffer(200);
2895 QTRY_VERIFY(listview->cacheBuffer() == 200);
2897 // items will be created one at a time
2898 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2899 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2900 QQuickItem *item = 0;
2903 controller.incubateWhile(&b);
2904 item = findItem<QQuickItem>(listview, "wrapper", i);
2910 controller.incubateWhile(&b);
2913 int newItemCount = 0;
2914 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2916 // Confirm items positioned correctly
2917 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2918 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2919 if (!item) qWarning() << "Item" << i << "not found";
2921 QTRY_VERIFY(item->y() == i*20);
2924 // move view and confirm items in view are visible immediately and outside are created async
2925 listview->setContentY(300);
2927 for (int i = 15; i < 32; ++i) {
2928 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2929 if (!item) qWarning() << "Item" << i << "not found";
2931 QVERIFY(item->y() == i*20);
2934 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2936 // ensure buffered items are created
2937 for (int i = 32; i < qMin(41,model.count()); ++i) {
2938 QQuickItem *item = 0;
2940 qGuiApp->processEvents(); // allow refill to happen
2942 controller.incubateWhile(&b);
2943 item = findItem<QQuickItem>(listview, "wrapper", i);
2949 controller.incubateWhile(&b);
2956 void tst_QQuickListView::positionViewAtBeginningEnd()
2958 QQuickView *window = createView();
2961 for (int i = 0; i < 40; i++)
2962 model.addItem("Item" + QString::number(i), "");
2964 QQmlContext *ctxt = window->rootContext();
2965 ctxt->setContextProperty("testModel", &model);
2967 TestObject *testObject = new TestObject;
2968 ctxt->setContextProperty("testObject", testObject);
2970 window->setSource(testFileUrl("listviewtest.qml"));
2971 qApp->processEvents();
2973 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
2974 QTRY_VERIFY(listview != 0);
2975 QQuickItem *contentItem = listview->contentItem();
2976 QTRY_VERIFY(contentItem != 0);
2977 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2979 listview->setContentY(100);
2981 // positionAtBeginnging
2982 listview->positionViewAtBeginning();
2983 QTRY_COMPARE(listview->contentY(), 0.);
2985 listview->setContentY(80);
2986 window->rootObject()->setProperty("showHeader", true);
2987 listview->positionViewAtBeginning();
2988 QTRY_COMPARE(listview->contentY(), -30.);
2991 listview->positionViewAtEnd();
2992 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
2994 listview->setContentY(80);
2995 window->rootObject()->setProperty("showFooter", true);
2996 listview->positionViewAtEnd();
2997 QTRY_COMPARE(listview->contentY(), 510.);
2999 // set current item to outside visible view, position at beginning
3000 // and ensure highlight moves to current item
3001 listview->setCurrentIndex(1);
3002 listview->positionViewAtBeginning();
3003 QTRY_COMPARE(listview->contentY(), -30.);
3004 QVERIFY(listview->highlightItem());
3005 QCOMPARE(listview->highlightItem()->y(), 20.);
3011 void tst_QQuickListView::positionViewAtIndex()
3013 QFETCH(bool, enforceRange);
3014 QFETCH(qreal, initContentY);
3016 QFETCH(QQuickListView::PositionMode, mode);
3017 QFETCH(qreal, contentY);
3019 QQuickView *window = getView();
3022 for (int i = 0; i < 40; i++)
3023 model.addItem("Item" + QString::number(i), "");
3025 QQmlContext *ctxt = window->rootContext();
3026 ctxt->setContextProperty("testModel", &model);
3028 TestObject *testObject = new TestObject;
3029 ctxt->setContextProperty("testObject", testObject);
3031 window->setSource(testFileUrl("listviewtest.qml"));
3032 qApp->processEvents();
3034 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3035 QTRY_VERIFY(listview != 0);
3036 QQuickItem *contentItem = listview->contentItem();
3037 QTRY_VERIFY(contentItem != 0);
3038 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3040 window->rootObject()->setProperty("enforceRange", enforceRange);
3041 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3043 listview->setContentY(initContentY);
3045 listview->positionViewAtIndex(index, mode);
3046 QTRY_COMPARE(listview->contentY(), contentY);
3048 // Confirm items positioned correctly
3049 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3050 for (int i = index; i < model.count() && i < itemCount-index-1; ++i) {
3051 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3052 if (!item) qWarning() << "Item" << i << "not found";
3054 QTRY_COMPARE(item->y(), i*20.);
3057 releaseView(window);
3060 void tst_QQuickListView::positionViewAtIndex_data()
3062 QTest::addColumn<bool>("enforceRange");
3063 QTest::addColumn<qreal>("initContentY");
3064 QTest::addColumn<int>("index");
3065 QTest::addColumn<QQuickListView::PositionMode>("mode");
3066 QTest::addColumn<qreal>("contentY");
3068 QTest::newRow("no range, 3 at Beginning") << false << 0. << 3 << QQuickListView::Beginning << 60.;
3069 QTest::newRow("no range, 3 at End") << false << 0. << 3 << QQuickListView::End << 0.;
3070 QTest::newRow("no range, 22 at Beginning") << false << 0. << 22 << QQuickListView::Beginning << 440.;
3071 // Position on an item that would leave empty space if positioned at the top
3072 QTest::newRow("no range, 28 at Beginning") << false << 0. << 28 << QQuickListView::Beginning << 480.;
3073 // Position at End using last index
3074 QTest::newRow("no range, last at End") << false << 0. << 39 << QQuickListView::End << 480.;
3076 QTest::newRow("no range, 20 at End") << false << 0. << 20 << QQuickListView::End << 100.;
3077 // Position in Center
3078 QTest::newRow("no range, 15 at Center") << false << 0. << 15 << QQuickListView::Center << 150.;
3079 // Ensure at least partially visible
3080 QTest::newRow("no range, 15 visible => Visible") << false << 150. << 15 << QQuickListView::Visible << 150.;
3081 QTest::newRow("no range, 15 partially visible => Visible") << false << 302. << 15 << QQuickListView::Visible << 302.;
3082 QTest::newRow("no range, 15 before visible => Visible") << false << 320. << 15 << QQuickListView::Visible << 300.;
3083 QTest::newRow("no range, 20 visible => Visible") << false << 85. << 20 << QQuickListView::Visible << 85.;
3084 QTest::newRow("no range, 20 before visible => Visible") << false << 75. << 20 << QQuickListView::Visible << 100.;
3085 QTest::newRow("no range, 20 after visible => Visible") << false << 480. << 20 << QQuickListView::Visible << 400.;
3086 // Ensure completely visible
3087 QTest::newRow("no range, 20 visible => Contain") << false << 120. << 20 << QQuickListView::Contain << 120.;
3088 QTest::newRow("no range, 15 partially visible => Contain") << false << 302. << 15 << QQuickListView::Contain << 300.;
3089 QTest::newRow("no range, 20 partially visible => Contain") << false << 85. << 20 << QQuickListView::Contain << 100.;
3091 QTest::newRow("strict range, 3 at End") << true << 0. << 3 << QQuickListView::End << -120.;
3092 QTest::newRow("strict range, 38 at Beginning") << true << 0. << 38 << QQuickListView::Beginning << 660.;
3093 QTest::newRow("strict range, 15 at Center") << true << 0. << 15 << QQuickListView::Center << 140.;
3094 QTest::newRow("strict range, 3 at SnapPosition") << true << 0. << 3 << QQuickListView::SnapPosition << -60.;
3095 QTest::newRow("strict range, 10 at SnapPosition") << true << 0. << 10 << QQuickListView::SnapPosition << 80.;
3096 QTest::newRow("strict range, 38 at SnapPosition") << true << 0. << 38 << QQuickListView::SnapPosition << 640.;
3099 void tst_QQuickListView::resetModel()
3101 QQuickView *window = createView();
3103 QStringList strings;
3104 strings << "one" << "two" << "three";
3105 QStringListModel model(strings);
3107 QQmlContext *ctxt = window->rootContext();
3108 ctxt->setContextProperty("testModel", &model);
3110 window->setSource(testFileUrl("displaylist.qml"));
3112 qApp->processEvents();
3114 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3115 QTRY_VERIFY(listview != 0);
3116 QQuickItem *contentItem = listview->contentItem();
3117 QTRY_VERIFY(contentItem != 0);
3118 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3120 QTRY_COMPARE(listview->count(), model.rowCount());
3122 for (int i = 0; i < model.rowCount(); ++i) {
3123 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3124 QTRY_VERIFY(display != 0);
3125 QTRY_COMPARE(display->text(), strings.at(i));
3129 strings << "four" << "five" << "six" << "seven";
3130 model.setStringList(strings);
3132 QTRY_COMPARE(listview->count(), model.rowCount());
3134 for (int i = 0; i < model.rowCount(); ++i) {
3135 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
3136 QTRY_VERIFY(display != 0);
3137 QTRY_COMPARE(display->text(), strings.at(i));
3143 void tst_QQuickListView::propertyChanges()
3145 QQuickView *window = createView();
3146 QTRY_VERIFY(window);
3147 window->setSource(testFileUrl("propertychangestest.qml"));
3149 QQuickListView *listView = window->rootObject()->findChild<QQuickListView*>("listView");
3150 QTRY_VERIFY(listView);
3152 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
3153 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
3154 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
3155 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
3156 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
3157 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
3158 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
3160 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
3161 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
3162 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
3163 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
3164 QTRY_COMPARE(listView->isWrapEnabled(), true);
3165 QTRY_COMPARE(listView->cacheBuffer(), 10);
3166 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
3168 listView->setHighlightFollowsCurrentItem(false);
3169 listView->setPreferredHighlightBegin(1.0);
3170 listView->setPreferredHighlightEnd(1.0);
3171 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3172 listView->setWrapEnabled(false);
3173 listView->setCacheBuffer(3);
3174 listView->setSnapMode(QQuickListView::SnapOneItem);
3176 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
3177 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
3178 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
3179 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
3180 QTRY_COMPARE(listView->isWrapEnabled(), false);
3181 QTRY_COMPARE(listView->cacheBuffer(), 3);
3182 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
3184 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3185 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3186 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3187 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3188 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3189 QTRY_COMPARE(cacheBufferSpy.count(),1);
3190 QTRY_COMPARE(snapModeSpy.count(),1);
3192 listView->setHighlightFollowsCurrentItem(false);
3193 listView->setPreferredHighlightBegin(1.0);
3194 listView->setPreferredHighlightEnd(1.0);
3195 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
3196 listView->setWrapEnabled(false);
3197 listView->setCacheBuffer(3);
3198 listView->setSnapMode(QQuickListView::SnapOneItem);
3200 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
3201 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
3202 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
3203 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
3204 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
3205 QTRY_COMPARE(cacheBufferSpy.count(),1);
3206 QTRY_COMPARE(snapModeSpy.count(),1);
3211 void tst_QQuickListView::componentChanges()
3213 QQuickView *window = createView();
3214 QTRY_VERIFY(window);
3215 window->setSource(testFileUrl("propertychangestest.qml"));
3217 QQuickListView *listView = window->rootObject()->findChild<QQuickListView*>("listView");
3218 QTRY_VERIFY(listView);
3220 QQmlComponent component(window->engine());
3221 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
3223 QQmlComponent delegateComponent(window->engine());
3224 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
3226 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
3227 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
3228 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
3229 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
3231 listView->setHighlight(&component);
3232 listView->setHeader(&component);
3233 listView->setFooter(&component);
3234 listView->setDelegate(&delegateComponent);
3236 QTRY_COMPARE(listView->highlight(), &component);
3237 QTRY_COMPARE(listView->header(), &component);
3238 QTRY_COMPARE(listView->footer(), &component);
3239 QTRY_COMPARE(listView->delegate(), &delegateComponent);
3241 QTRY_COMPARE(highlightSpy.count(),1);
3242 QTRY_COMPARE(delegateSpy.count(),1);
3243 QTRY_COMPARE(headerSpy.count(),1);
3244 QTRY_COMPARE(footerSpy.count(),1);
3246 listView->setHighlight(&component);
3247 listView->setHeader(&component);
3248 listView->setFooter(&component);
3249 listView->setDelegate(&delegateComponent);
3251 QTRY_COMPARE(highlightSpy.count(),1);
3252 QTRY_COMPARE(delegateSpy.count(),1);
3253 QTRY_COMPARE(headerSpy.count(),1);
3254 QTRY_COMPARE(footerSpy.count(),1);
3259 void tst_QQuickListView::modelChanges()
3261 QQuickView *window = createView();
3262 QTRY_VERIFY(window);
3263 window->setSource(testFileUrl("propertychangestest.qml"));
3265 QQuickListView *listView = window->rootObject()->findChild<QQuickListView*>("listView");
3266 QTRY_VERIFY(listView);
3268 QQuickListModel *alternateModel = window->rootObject()->findChild<QQuickListModel*>("alternateModel");
3269 QTRY_VERIFY(alternateModel);
3270 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
3271 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
3273 listView->setModel(modelVariant);
3274 QTRY_COMPARE(listView->model(), modelVariant);
3275 QTRY_COMPARE(modelSpy.count(),1);
3277 listView->setModel(modelVariant);
3278 QTRY_COMPARE(modelSpy.count(),1);
3280 listView->setModel(QVariant());
3281 QTRY_COMPARE(modelSpy.count(),2);
3286 void tst_QQuickListView::QTBUG_9791()
3288 QQuickView *window = createView();
3290 window->setSource(testFileUrl("strictlyenforcerange.qml"));
3291 qApp->processEvents();
3293 QQuickListView *listview = qobject_cast<QQuickListView*>(window->rootObject());
3294 QTRY_VERIFY(listview != 0);
3296 QQuickItem *contentItem = listview->contentItem();
3297 QTRY_VERIFY(contentItem != 0);
3298 QTRY_VERIFY(listview->delegate() != 0);
3299 QTRY_VERIFY(listview->model() != 0);
3301 QMetaObject::invokeMethod(listview, "fillModel");
3302 qApp->processEvents();
3304 // Confirm items positioned correctly
3305 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3306 QCOMPARE(itemCount, 3);
3308 for (int i = 0; i < itemCount; ++i) {
3309 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3310 if (!item) qWarning() << "Item" << i << "not found";
3312 QTRY_COMPARE(item->x(), i*300.0);
3315 // check that view is positioned correctly
3316 QTRY_COMPARE(listview->contentX(), 590.0);
3321 void tst_QQuickListView::manualHighlight()
3323 QQuickView *window = new QQuickView(0);
3324 window->setGeometry(0,0,240,320);
3326 QString filename(testFile("manual-highlight.qml"));
3327 window->setSource(QUrl::fromLocalFile(filename));
3329 qApp->processEvents();
3331 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3332 QTRY_VERIFY(listview != 0);
3334 QQuickItem *contentItem = listview->contentItem();
3335 QTRY_VERIFY(contentItem != 0);
3337 QTRY_COMPARE(listview->currentIndex(), 0);
3338 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3339 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3341 listview->setCurrentIndex(2);
3343 QTRY_COMPARE(listview->currentIndex(), 2);
3344 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3345 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3348 listview->positionViewAtIndex(3, QQuickListView::Contain);
3350 QTRY_COMPARE(listview->currentIndex(), 2);
3351 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3352 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
3357 void tst_QQuickListView::QTBUG_11105()
3359 QQuickView *window = createView();
3362 for (int i = 0; i < 30; i++)
3363 model.addItem("Item" + QString::number(i), "");
3365 QQmlContext *ctxt = window->rootContext();
3366 ctxt->setContextProperty("testModel", &model);
3368 TestObject *testObject = new TestObject;
3369 ctxt->setContextProperty("testObject", testObject);
3371 window->setSource(testFileUrl("listviewtest.qml"));
3373 qApp->processEvents();
3375 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3376 QTRY_VERIFY(listview != 0);
3377 QQuickItem *contentItem = listview->contentItem();
3378 QTRY_VERIFY(contentItem != 0);
3379 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3381 // Confirm items positioned correctly
3382 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3383 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3384 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3385 if (!item) qWarning() << "Item" << i << "not found";
3387 QTRY_VERIFY(item->y() == i*20);
3390 listview->positionViewAtIndex(20, QQuickListView::Beginning);
3391 QCOMPARE(listview->contentY(), 280.);
3394 for (int i = 0; i < 5; i++)
3395 model2.addItem("Item" + QString::number(i), "");
3397 ctxt->setContextProperty("testModel", &model2);
3399 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3400 QCOMPARE(itemCount, 5);
3406 void tst_QQuickListView::initialZValues()
3408 QQuickView *window = createView();
3409 window->setSource(testFileUrl("initialZValues.qml"));
3410 qApp->processEvents();
3412 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3413 QTRY_VERIFY(listview != 0);
3414 QQuickItem *contentItem = listview->contentItem();
3415 QTRY_VERIFY(contentItem != 0);
3417 QVERIFY(listview->headerItem());
3418 QTRY_COMPARE(listview->headerItem()->z(), listview->property("initialZ").toReal());
3420 QVERIFY(listview->footerItem());
3421 QTRY_COMPARE(listview->footerItem()->z(), listview->property("initialZ").toReal());
3426 void tst_QQuickListView::header()
3428 QFETCH(QQuickListView::Orientation, orientation);
3429 QFETCH(Qt::LayoutDirection, layoutDirection);
3430 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3431 QFETCH(QPointF, initialHeaderPos);
3432 QFETCH(QPointF, changedHeaderPos);
3433 QFETCH(QPointF, initialContentPos);
3434 QFETCH(QPointF, changedContentPos);
3435 QFETCH(QPointF, firstDelegatePos);
3436 QFETCH(QPointF, resizeContentPos);
3439 for (int i = 0; i < 30; i++)
3440 model.addItem("Item" + QString::number(i), "");
3442 QQuickView *window = getView();
3443 window->rootContext()->setContextProperty("testModel", &model);
3444 window->rootContext()->setContextProperty("initialViewWidth", 240);
3445 window->rootContext()->setContextProperty("initialViewHeight", 320);
3446 window->setSource(testFileUrl("header.qml"));
3448 qApp->processEvents();
3450 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3451 QTRY_VERIFY(listview != 0);
3452 listview->setOrientation(orientation);
3453 listview->setLayoutDirection(layoutDirection);
3454 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3455 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3457 QQuickItem *contentItem = listview->contentItem();
3458 QTRY_VERIFY(contentItem != 0);
3460 QQuickText *header = 0;
3461 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
3462 QVERIFY(header == listview->headerItem());
3464 QCOMPARE(header->width(), 100.);
3465 QCOMPARE(header->height(), 30.);
3466 QCOMPARE(header->pos(), initialHeaderPos);
3467 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3469 if (orientation == QQuickListView::Vertical)
3470 QCOMPARE(listview->contentHeight(), model.count() * 30. + header->height());
3472 QCOMPARE(listview->contentWidth(), model.count() * 240. + header->width());
3474 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3476 QCOMPARE(item->pos(), firstDelegatePos);
3479 QTRY_COMPARE(listview->count(), model.count());
3480 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3481 if (orientation == QQuickListView::Vertical)
3482 QCOMPARE(listview->contentHeight(), header->height());
3484 QCOMPARE(listview->contentWidth(), header->width());
3486 for (int i = 0; i < 30; i++)
3487 model.addItem("Item" + QString::number(i), "");
3489 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3490 QMetaObject::invokeMethod(window->rootObject(), "changeHeader");
3492 QCOMPARE(headerItemSpy.count(), 1);
3494 header = findItem<QQuickText>(contentItem, "header");
3496 header = findItem<QQuickText>(contentItem, "header2");
3499 QVERIFY(header == listview->headerItem());
3501 QCOMPARE(header->pos(), changedHeaderPos);
3502 QCOMPARE(header->width(), 50.);
3503 QCOMPARE(header->height(), 20.);
3504 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3506 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3508 QCOMPARE(item->pos(), firstDelegatePos);
3510 listview->positionViewAtBeginning();
3511 header->setHeight(10);
3512 header->setWidth(40);
3513 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3515 releaseView(window);
3518 // QTBUG-21207 header should become visible if view resizes from initial empty size
3521 window->rootContext()->setContextProperty("testModel", &model);
3522 window->rootContext()->setContextProperty("initialViewWidth", 0.0);
3523 window->rootContext()->setContextProperty("initialViewHeight", 0.0);
3524 window->setSource(testFileUrl("header.qml"));
3526 qApp->processEvents();
3528 listview = findItem<QQuickListView>(window->rootObject(), "list");
3529 QTRY_VERIFY(listview != 0);
3530 listview->setOrientation(orientation);
3531 listview->setLayoutDirection(layoutDirection);
3532 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3533 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3535 listview->setWidth(240);
3536 listview->setHeight(320);
3537 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3538 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3540 releaseView(window);
3543 void tst_QQuickListView::header_data()
3545 QTest::addColumn<QQuickListView::Orientation>("orientation");
3546 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3547 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3548 QTest::addColumn<QPointF>("initialHeaderPos");
3549 QTest::addColumn<QPointF>("changedHeaderPos");
3550 QTest::addColumn<QPointF>("initialContentPos");
3551 QTest::addColumn<QPointF>("changedContentPos");
3552 QTest::addColumn<QPointF>("firstDelegatePos");
3553 QTest::addColumn<QPointF>("resizeContentPos");
3555 // header1 = 100 x 30
3556 // header2 = 50 x 20
3557 // delegates = 240 x 30
3560 // header above items, top left
3561 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3569 // header above items, top right
3570 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3578 // header to left of items
3579 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3587 // header to right of items
3588 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3591 << QPointF(-240 + 100, 0)
3592 << QPointF(-240 + 50, 0)
3594 << QPointF(-240 + 40, 0);
3596 // header below items
3597 QTest::newRow("vertical, bottom to top") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3600 << QPointF(0, -320 + 30)
3601 << QPointF(0, -320 + 20)
3603 << QPointF(0, -320 + 10);
3606 void tst_QQuickListView::header_delayItemCreation()
3608 QQuickView *window = createView();
3612 window->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3613 window->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3614 qApp->processEvents();
3616 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3617 QTRY_VERIFY(listview != 0);
3619 QQuickItem *contentItem = listview->contentItem();
3620 QTRY_VERIFY(contentItem != 0);
3622 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3624 QCOMPARE(header->y(), -header->height());
3626 QCOMPARE(listview->contentY(), -header->height());
3629 QTRY_COMPARE(header->y(), -header->height());
3634 void tst_QQuickListView::footer()
3636 QFETCH(QQuickListView::Orientation, orientation);
3637 QFETCH(Qt::LayoutDirection, layoutDirection);
3638 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3639 QFETCH(QPointF, initialFooterPos);
3640 QFETCH(QPointF, firstDelegatePos);
3641 QFETCH(QPointF, initialContentPos);
3642 QFETCH(QPointF, changedFooterPos);
3643 QFETCH(QPointF, changedContentPos);
3644 QFETCH(QPointF, resizeContentPos);
3646 QQuickView *window = getView();
3649 for (int i = 0; i < 3; i++)
3650 model.addItem("Item" + QString::number(i), "");
3652 QQmlContext *ctxt = window->rootContext();
3653 ctxt->setContextProperty("testModel", &model);
3655 window->setSource(testFileUrl("footer.qml"));
3657 qApp->processEvents();
3659 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3660 QTRY_VERIFY(listview != 0);
3661 listview->setOrientation(orientation);
3662 listview->setLayoutDirection(layoutDirection);
3663 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3664 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3666 QQuickItem *contentItem = listview->contentItem();
3667 QTRY_VERIFY(contentItem != 0);
3669 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3672 QVERIFY(footer == listview->footerItem());
3674 QCOMPARE(footer->pos(), initialFooterPos);
3675 QCOMPARE(footer->width(), 100.);
3676 QCOMPARE(footer->height(), 30.);
3677 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3679 if (orientation == QQuickListView::Vertical)
3680 QCOMPARE(listview->contentHeight(), model.count() * 20. + footer->height());
3682 QCOMPARE(listview->contentWidth(), model.count() * 40. + footer->width());
3684 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3686 QCOMPARE(item->pos(), firstDelegatePos);
3689 model.removeItem(1);
3691 if (orientation == QQuickListView::Vertical) {
3692 QTRY_COMPARE(footer->y(), verticalLayoutDirection == QQuickItemView::TopToBottom ?
3693 initialFooterPos.y() - 20 : initialFooterPos.y() + 20); // delegate width = 40
3695 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3696 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3701 if (orientation == QQuickListView::Vertical)
3702 QTRY_COMPARE(listview->contentHeight(), footer->height());
3704 QTRY_COMPARE(listview->contentWidth(), footer->width());
3706 QPointF posWhenNoItems(0, 0);
3707 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3708 posWhenNoItems.setX(-100);
3709 else if (orientation == QQuickListView::Vertical && verticalLayoutDirection == QQuickItemView::BottomToTop)
3710 posWhenNoItems.setY(-30);
3711 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3713 // if header is present, it's at a negative pos, so the footer should not move
3714 window->rootObject()->setProperty("showHeader", true);
3715 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3716 window->rootObject()->setProperty("showHeader", false);
3719 for (int i = 0; i < 30; i++)
3720 model.addItem("Item" + QString::number(i), "");
3722 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3723 QMetaObject::invokeMethod(window->rootObject(), "changeFooter");
3725 QCOMPARE(footerItemSpy.count(), 1);
3727 footer = findItem<QQuickText>(contentItem, "footer");
3729 footer = findItem<QQuickText>(contentItem, "footer2");
3732 QVERIFY(footer == listview->footerItem());
3734 QCOMPARE(footer->pos(), changedFooterPos);
3735 QCOMPARE(footer->width(), 50.);
3736 QCOMPARE(footer->height(), 20.);
3737 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3739 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3741 QCOMPARE(item->pos(), firstDelegatePos);
3743 listview->positionViewAtEnd();
3744 footer->setHeight(10);
3745 footer->setWidth(40);
3746 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3748 releaseView(window);
3751 void tst_QQuickListView::footer_data()
3753 QTest::addColumn<QQuickListView::Orientation>("orientation");
3754 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3755 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3756 QTest::addColumn<QPointF>("initialFooterPos");
3757 QTest::addColumn<QPointF>("changedFooterPos");
3758 QTest::addColumn<QPointF>("initialContentPos");
3759 QTest::addColumn<QPointF>("changedContentPos");
3760 QTest::addColumn<QPointF>("firstDelegatePos");
3761 QTest::addColumn<QPointF>("resizeContentPos");
3763 // footer1 = 100 x 30
3764 // footer2 = 50 x 20
3765 // delegates = 40 x 20
3767 // view height = 320
3769 // footer below items, bottom left
3770 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3771 << QPointF(0, 3 * 20)
3772 << QPointF(0, 30 * 20) // added 30 items
3776 << QPointF(0, 30 * 20 - 320 + 10);
3778 // footer below items, bottom right
3779 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QQuickItemView::TopToBottom
3780 << QPointF(0, 3 * 20)
3781 << QPointF(0, 30 * 20)
3785 << QPointF(0, 30 * 20 - 320 + 10);
3787 // footer to right of items
3788 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3789 << QPointF(40 * 3, 0)
3790 << QPointF(40 * 30, 0)
3794 << QPointF(40 * 30 - 240 + 40, 0);
3796 // footer to left of items
3797 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3798 << QPointF(-(40 * 3) - 100, 0)
3799 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3803 << QPointF(-(40 * 30) - 40, 0);
3805 // footer above items
3806 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3807 << QPointF(0, -(3 * 20) - 30)
3808 << QPointF(0, -(30 * 20) - 20)
3812 << QPointF(0, -(30 * 20) - 10);
3815 class LVAccessor : public QQuickListView
3818 qreal minY() const { return minYExtent(); }
3819 qreal maxY() const { return maxYExtent(); }
3820 qreal minX() const { return minXExtent(); }
3821 qreal maxX() const { return maxXExtent(); }
3825 void tst_QQuickListView::extents()
3827 QFETCH(QQuickListView::Orientation, orientation);
3828 QFETCH(Qt::LayoutDirection, layoutDirection);
3829 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3830 QFETCH(QPointF, headerPos);
3831 QFETCH(QPointF, footerPos);
3832 QFETCH(QPointF, minPos);
3833 QFETCH(QPointF, maxPos);
3834 QFETCH(QPointF, origin_empty);
3835 QFETCH(QPointF, origin_nonEmpty);
3837 QQuickView *window = getView();
3840 QQmlContext *ctxt = window->rootContext();
3842 ctxt->setContextProperty("testModel", &model);
3843 window->setSource(testFileUrl("headerfooter.qml"));
3845 qApp->processEvents();
3847 QQuickListView *listview = qobject_cast<QQuickListView*>(window->rootObject());
3848 QTRY_VERIFY(listview != 0);
3849 listview->setOrientation(orientation);
3850 listview->setLayoutDirection(layoutDirection);
3851 listview->setVerticalLayoutDirection(verticalLayoutDirection);
3852 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3854 QQuickItem *contentItem = listview->contentItem();
3855 QTRY_VERIFY(contentItem != 0);
3857 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3859 QCOMPARE(header->pos(), headerPos);
3861 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3863 QCOMPARE(footer->pos(), footerPos);
3865 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), minPos.x());
3866 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), minPos.y());
3867 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), maxPos.x());
3868 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), maxPos.y());
3870 QCOMPARE(listview->originX(), origin_empty.x());
3871 QCOMPARE(listview->originY(), origin_empty.y());
3872 for (int i=0; i<30; i++)
3873 model.addItem("Item" + QString::number(i), "");
3874 QTRY_COMPARE(listview->count(), model.count());
3875 QCOMPARE(listview->originX(), origin_nonEmpty.x());
3876 QCOMPARE(listview->originY(), origin_nonEmpty.y());
3878 releaseView(window);
3881 void tst_QQuickListView::extents_data()
3883 QTest::addColumn<QQuickListView::Orientation>("orientation");
3884 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3885 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3886 QTest::addColumn<QPointF>("headerPos");
3887 QTest::addColumn<QPointF>("footerPos");
3888 QTest::addColumn<QPointF>("minPos");
3889 QTest::addColumn<QPointF>("maxPos");
3890 QTest::addColumn<QPointF>("origin_empty");
3891 QTest::addColumn<QPointF>("origin_nonEmpty");
3893 // header is 240x20 (or 20x320 in Horizontal orientation)
3894 // footer is 240x30 (or 30x320 in Horizontal orientation)
3896 QTest::newRow("Vertical, TopToBottom")
3897 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
3898 << QPointF(0, -20) << QPointF(0, 0)
3899 << QPointF(0, 20) << QPointF(240, 20)
3900 << QPointF(0, -20) << QPointF(0, -20);
3902 QTest::newRow("Vertical, BottomToTop")
3903 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
3904 << QPointF(0, 0) << QPointF(0, -30)
3905 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3906 << QPointF(0, -30) << QPointF(0, (-30.0 * 30) - 30);
3908 QTest::newRow("Horizontal, LeftToRight")
3909 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom
3910 << QPointF(-20, 0) << QPointF(0, 0)
3911 << QPointF(20, 0) << QPointF(20, 320)
3912 << QPointF(-20, 0) << QPointF(-20, 0);
3914 QTest::newRow("Horizontal, RightToLeft")
3915 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
3916 << QPointF(0, 0) << QPointF(-30, 0)
3917 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3918 << QPointF(-30, 0) << QPointF((-240.0 * 30) - 30, 0);
3921 void tst_QQuickListView::resetModel_headerFooter()
3923 // Resetting a model shouldn't crash in views with header/footer
3925 QQuickView *window = createView();
3928 for (int i = 0; i < 4; i++)
3929 model.addItem("Item" + QString::number(i), "");
3930 QQmlContext *ctxt = window->rootContext();
3931 ctxt->setContextProperty("testModel", &model);
3933 window->setSource(testFileUrl("headerfooter.qml"));
3934 qApp->processEvents();
3936 QQuickListView *listview = qobject_cast<QQuickListView*>(window->rootObject());
3937 QTRY_VERIFY(listview != 0);
3939 QQuickItem *contentItem = listview->contentItem();
3940 QTRY_VERIFY(contentItem != 0);
3942 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3944 QCOMPARE(header->y(), -header->height());
3946 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3948 QCOMPARE(footer->y(), 30.*4);
3952 header = findItem<QQuickItem>(contentItem, "header");
3954 QCOMPARE(header->y(), -header->height());
3956 footer = findItem<QQuickItem>(contentItem, "footer");
3958 QCOMPARE(footer->y(), 30.*4);
3963 void tst_QQuickListView::resizeView()
3965 QQuickView *window = createView();
3968 for (int i = 0; i < 40; i++)
3969 model.addItem("Item" + QString::number(i), "");
3971 QQmlContext *ctxt = window->rootContext();
3972 ctxt->setContextProperty("testModel", &model);
3974 TestObject *testObject = new TestObject;
3975 ctxt->setContextProperty("testObject", testObject);
3977 window->setSource(testFileUrl("listviewtest.qml"));
3979 qApp->processEvents();
3981 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
3982 QTRY_VERIFY(listview != 0);
3983 QQuickItem *contentItem = listview->contentItem();
3984 QTRY_VERIFY(contentItem != 0);
3985 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3987 // Confirm items positioned correctly
3988 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3989 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3990 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3991 if (!item) qWarning() << "Item" << i << "not found";
3993 QTRY_COMPARE(item->y(), i*20.);
3996 QVariant heightRatio;
3997 QMetaObject::invokeMethod(window->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3998 QCOMPARE(heightRatio.toReal(), 0.4);
4000 listview->setHeight(200);
4001 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4003 QMetaObject::invokeMethod(window->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
4004 QCOMPARE(heightRatio.toReal(), 0.25);
4006 // Ensure we handle -ve sizes
4007 listview->setHeight(-100);
4008 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
4010 listview->setCacheBuffer(200);
4011 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
4013 // ensure items in cache become visible
4014 listview->setHeight(200);
4015 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
4017 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4018 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4019 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4020 if (!item) qWarning() << "Item" << i << "not found";
4022 QTRY_COMPARE(item->y(), i*20.);
4023 QCOMPARE(delegateVisible(item), i < 11); // inside view visible, outside not visible
4026 // ensure items outside view become invisible
4027 listview->setHeight(100);
4028 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
4030 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4031 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4032 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4033 if (!item) qWarning() << "Item" << i << "not found";
4035 QTRY_COMPARE(item->y(), i*20.);
4036 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
4043 void tst_QQuickListView::resizeViewAndRepaint()
4045 QQuickView *window = createView();
4048 for (int i = 0; i < 40; i++)
4049 model.addItem("Item" + QString::number(i), "");
4051 QQmlContext *ctxt = window->rootContext();
4052 ctxt->setContextProperty("testModel", &model);
4053 ctxt->setContextProperty("initialHeight", 100);
4055 window->setSource(testFileUrl("resizeview.qml"));
4057 qApp->processEvents();
4059 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4060 QTRY_VERIFY(listview != 0);
4061 QQuickItem *contentItem = listview->contentItem();
4062 QTRY_VERIFY(contentItem != 0);
4063 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4065 // item at index 10 should not be currently visible
4066 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4068 listview->setHeight(320);
4070 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
4072 listview->setHeight(100);
4073 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
4078 void tst_QQuickListView::sizeLessThan1()
4080 QQuickView *window = createView();
4083 for (int i = 0; i < 30; i++)
4084 model.addItem("Item" + QString::number(i), "");
4086 QQmlContext *ctxt = window->rootContext();
4087 ctxt->setContextProperty("testModel", &model);
4089 TestObject *testObject = new TestObject;
4090 ctxt->setContextProperty("testObject", testObject);
4092 window->setSource(testFileUrl("sizelessthan1.qml"));
4094 qApp->processEvents();
4096 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4097 QTRY_VERIFY(listview != 0);
4098 QQuickItem *contentItem = listview->contentItem();
4099 QTRY_VERIFY(contentItem != 0);
4100 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4102 // Confirm items positioned correctly
4103 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4104 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4105 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4106 if (!item) qWarning() << "Item" << i << "not found";
4108 QTRY_COMPARE(item->y(), i*0.5);
4115 void tst_QQuickListView::QTBUG_14821()
4117 QQuickView *window = createView();
4119 window->setSource(testFileUrl("qtbug14821.qml"));
4120 qApp->processEvents();
4122 QQuickListView *listview = qobject_cast<QQuickListView*>(window->rootObject());
4123 QVERIFY(listview != 0);
4125 QQuickItem *contentItem = listview->contentItem();
4126 QVERIFY(contentItem != 0);
4128 listview->decrementCurrentIndex();
4129 QCOMPARE(listview->currentIndex(), 99);
4131 listview->incrementCurrentIndex();
4132 QCOMPARE(listview->currentIndex(), 0);
4137 void tst_QQuickListView::resizeDelegate()
4139 QQuickView *window = createView();
4141 QStringList strings;
4142 for (int i = 0; i < 30; ++i)
4143 strings << QString::number(i);
4144 QStringListModel model(strings);
4146 QQmlContext *ctxt = window->rootContext();
4147 ctxt->setContextProperty("testModel", &model);
4149 window->setSource(testFileUrl("displaylist.qml"));
4151 qApp->processEvents();
4153 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4154 QVERIFY(listview != 0);
4155 QQuickItem *contentItem = listview->contentItem();
4156 QVERIFY(contentItem != 0);
4157 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4159 QCOMPARE(listview->count(), model.rowCount());
4161 listview->setCurrentIndex(25);
4162 listview->setContentY(0);
4163 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4165 for (int i = 0; i < 16; ++i) {
4166 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4168 QCOMPARE(item->y(), i*20.0);
4171 QCOMPARE(listview->currentItem()->y(), 500.0);
4172 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
4174 window->rootObject()->setProperty("delegateHeight", 30);
4175 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4177 for (int i = 0; i < 11; ++i) {
4178 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4180 QTRY_COMPARE(item->y(), i*30.0);
4183 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
4184 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
4186 listview->setCurrentIndex(1);
4187 listview->positionViewAtIndex(25, QQuickListView::Beginning);
4188 listview->positionViewAtIndex(5, QQuickListView::Beginning);
4189 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4191 for (int i = 5; i < 16; ++i) {
4192 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4194 QCOMPARE(item->y(), i*30.0);
4197 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
4198 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
4200 window->rootObject()->setProperty("delegateHeight", 20);
4201 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4203 for (int i = 5; i < 11; ++i) {
4204 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4206 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
4209 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
4210 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
4215 void tst_QQuickListView::resizeFirstDelegate()
4217 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
4218 // and other delegates have height > 0
4220 QQuickView *window = createView();
4222 // bug only occurs when all items in the model are visible
4224 for (int i = 0; i < 10; i++)
4225 model.addItem("Item" + QString::number(i), "");
4227 QQmlContext *ctxt = window->rootContext();
4228 ctxt->setContextProperty("testModel", &model);
4230 TestObject *testObject = new TestObject;
4231 ctxt->setContextProperty("testObject", testObject);
4233 window->setSource(testFileUrl("listviewtest.qml"));
4235 qApp->processEvents();
4237 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4238 QVERIFY(listview != 0);
4239 QQuickItem *contentItem = listview->contentItem();
4240 QVERIFY(contentItem != 0);
4241 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4243 QQuickItem *item = 0;
4244 for (int i = 0; i < model.count(); ++i) {
4245 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4247 QCOMPARE(item->y(), i*20.0);
4250 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
4253 // check the content y has not jumped up and down
4254 QCOMPARE(listview->contentY(), 0.0);
4255 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
4257 QCOMPARE(spy.count(), 0);
4259 for (int i = 1; i < model.count(); ++i) {
4260 item = findItem<QQuickItem>(contentItem, "wrapper", i);
4262 QTRY_COMPARE(item->y(), (i-1)*20.0);
4266 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
4267 // list if they follow a zero-sized delegate
4269 for (int i = 0; i < 10; i++)
4270 model.addItem("Item" + QString::number(i), "");
4271 QTRY_COMPARE(listview->count(), model.count());
4273 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
4277 listview->setCurrentIndex(19);
4278 qApp->processEvents();
4279 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4281 // items 0-2 should have been deleted
4282 for (int i=0; i<3; i++) {
4283 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
4290 void tst_QQuickListView::repositionResizedDelegate()
4292 QFETCH(QQuickListView::Orientation, orientation);
4293 QFETCH(Qt::LayoutDirection, layoutDirection);
4294 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4295 QFETCH(QPointF, contentPos_itemFirstHalfVisible);
4296 QFETCH(QPointF, contentPos_itemSecondHalfVisible);
4297 QFETCH(QRectF, origPositionerRect);
4298 QFETCH(QRectF, resizedPositionerRect);
4300 QQuickView *window = getView();
4301 QQmlContext *ctxt = window->rootContext();
4302 ctxt->setContextProperty("testHorizontal", orientation == QQuickListView::Horizontal);
4303 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
4304 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickListView::BottomToTop);
4305 window->setSource(testFileUrl("repositionResizedDelegate.qml"));
4307 qApp->processEvents();
4309 QQuickListView *listview = qobject_cast<QQuickListView*>(window->rootObject());
4310 QTRY_VERIFY(listview != 0);
4311 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4313 QQuickItem *positioner = findItem<QQuickItem>(window->rootObject(), "positioner");
4314 QVERIFY(positioner);
4315 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4316 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4317 QSignalSpy spy(listview, orientation == QQuickListView::Vertical ? SIGNAL(contentYChanged()) : SIGNAL(contentXChanged()));
4318 int prevSpyCount = 0;
4320 // When an item is resized while it is partially visible, it should resize in the
4321 // direction of the content flow. If a RightToLeft or BottomToTop layout is used,
4322 // the item should also be re-positioned so its end position stays the same.
4324 listview->setContentX(contentPos_itemFirstHalfVisible.x());
4325 listview->setContentY(contentPos_itemFirstHalfVisible.y());
4326 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4327 prevSpyCount = spy.count();
4328 QVERIFY(QMetaObject::invokeMethod(window->rootObject(), "incrementRepeater"));
4329 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4330 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4331 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4332 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4333 QCOMPARE(spy.count(), prevSpyCount);
4335 QVERIFY(QMetaObject::invokeMethod(window->rootObject(), "decrementRepeater"));
4336 QTRY_COMPARE(positioner->boundingRect().size(), origPositionerRect.size());
4337 QTRY_COMPARE(positioner->pos(), origPositionerRect.topLeft());
4338 QCOMPARE(listview->contentX(), contentPos_itemFirstHalfVisible.x());
4339 QCOMPARE(listview->contentY(), contentPos_itemFirstHalfVisible.y());
4341 listview->setContentX(contentPos_itemSecondHalfVisible.x());
4342 listview->setContentY(contentPos_itemSecondHalfVisible.y());
4343 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4344 prevSpyCount = spy.count();
4346 QVERIFY(QMetaObject::invokeMethod(window->rootObject(), "incrementRepeater"));
4347 positioner = findItem<QQuickItem>(window->rootObject(), "positioner");
4348 QTRY_COMPARE(positioner->boundingRect().size(), resizedPositionerRect.size());
4349 QTRY_COMPARE(positioner->pos(), resizedPositionerRect.topLeft());
4350 QCOMPARE(listview->contentX(), contentPos_itemSecondHalfVisible.x());
4351 QCOMPARE(listview->contentY(), contentPos_itemSecondHalfVisible.y());
4352 qApp->processEvents();
4353 QCOMPARE(spy.count(), prevSpyCount);
4355 releaseView(window);
4358 void tst_QQuickListView::repositionResizedDelegate_data()
4360 QTest::addColumn<QQuickListView::Orientation>("orientation");
4361 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4362 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4363 QTest::addColumn<QPointF>("contentPos_itemFirstHalfVisible");
4364 QTest::addColumn<QPointF>("contentPos_itemSecondHalfVisible");
4365 QTest::addColumn<QRectF>("origPositionerRect");
4366 QTest::addColumn<QRectF>("resizedPositionerRect");
4368 QTest::newRow("vertical")
4369 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4370 << QPointF(0, 60) << QPointF(0, 200 + 60)
4371 << QRectF(0, 200, 120, 120)
4372 << QRectF(0, 200, 120, 120 * 2);
4374 QTest::newRow("vertical, BottomToTop")
4375 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4376 << QPointF(0, -200 - 60) << QPointF(0, -200 - 260)
4377 << QRectF(0, -200 - 120, 120, 120)
4378 << QRectF(0, -200 - 120*2, 120, 120 * 2);
4380 QTest::newRow("horizontal")
4381 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4382 << QPointF(60, 0) << QPointF(260, 0)
4383 << QRectF(200, 0, 120, 120)
4384 << QRectF(200, 0, 120 * 2, 120);
4386 QTest::newRow("horizontal, rtl")
4387 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4388 << QPointF(-200 - 60, 0) << QPointF(-200 - 260, 0)
4389 << QRectF(-200 - 120, 0, 120, 120)
4390 << QRectF(-200 - 120 * 2, 0, 120 * 2, 120);
4393 void tst_QQuickListView::QTBUG_16037()
4395 QQuickView *window = createView();
4398 window->setSource(testFileUrl("qtbug16037.qml"));
4399 qApp->processEvents();
4401 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "listview");
4402 QTRY_VERIFY(listview != 0);
4404 QVERIFY(listview->contentHeight() <= 0.0);
4406 QMetaObject::invokeMethod(window->rootObject(), "setModel");
4408 QTRY_COMPARE(listview->contentHeight(), 80.0);
4413 void tst_QQuickListView::indexAt_itemAt_data()
4415 QTest::addColumn<qreal>("x");
4416 QTest::addColumn<qreal>("y");
4417 QTest::addColumn<int>("index");
4419 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
4420 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
4421 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
4422 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
4423 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
4426 void tst_QQuickListView::indexAt_itemAt()
4432 QQuickView *window = getView();
4435 for (int i = 0; i < 30; i++)
4436 model.addItem("Item" + QString::number(i), "");
4438 QQmlContext *ctxt = window->rootContext();
4439 ctxt->setContextProperty("testModel", &model);
4441 TestObject *testObject = new TestObject;
4442 ctxt->setContextProperty("testObject", testObject);
4444 window->setSource(testFileUrl("listviewtest.qml"));
4446 qApp->processEvents();
4448 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4449 QTRY_VERIFY(listview != 0);
4451 QQuickItem *contentItem = listview->contentItem();
4452 QTRY_VERIFY(contentItem != 0);
4453 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4455 QQuickItem *item = 0;
4457 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4460 QCOMPARE(listview->indexAt(x,y), index);
4461 QVERIFY(listview->itemAt(x,y) == item);
4463 releaseView(window);
4467 void tst_QQuickListView::incrementalModel()
4469 QQuickView *window = createView();
4471 IncrementalModel model;
4472 QQmlContext *ctxt = window->rootContext();
4473 ctxt->setContextProperty("testModel", &model);
4475 window->setSource(testFileUrl("displaylist.qml"));
4476 qApp->processEvents();
4478 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4479 QTRY_VERIFY(listview != 0);
4481 QQuickItem *contentItem = listview->contentItem();
4482 QTRY_VERIFY(contentItem != 0);
4484 QTRY_COMPARE(listview->count(), 20);
4486 listview->positionViewAtIndex(10, QQuickListView::Beginning);
4488 QTRY_COMPARE(listview->count(), 25);
4493 void tst_QQuickListView::onAdd()
4495 QFETCH(int, initialItemCount);
4496 QFETCH(int, itemsToAdd);
4498 const int delegateHeight = 10;
4501 // these initial items should not trigger ListView.onAdd
4502 for (int i=0; i<initialItemCount; i++)
4503 model.addItem("dummy value", "dummy value");
4505 QQuickView *window = createView();
4506 window->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
4507 QQmlContext *ctxt = window->rootContext();
4508 ctxt->setContextProperty("testModel", &model);
4509 ctxt->setContextProperty("delegateHeight", delegateHeight);
4510 window->setSource(testFileUrl("attachedSignals.qml"));
4512 QObject *object = window->rootObject();
4513 object->setProperty("width", window->width());
4514 object->setProperty("height", window->height());
4515 qApp->processEvents();
4517 QList<QPair<QString, QString> > items;
4518 for (int i=0; i<itemsToAdd; i++)
4519 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4520 model.addItems(items);
4521 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
4523 QVariantList result = object->property("addedDelegates").toList();
4524 QCOMPARE(result.count(), items.count());
4525 for (int i=0; i<items.count(); i++)
4526 QCOMPARE(result[i].toString(), items[i].first);
4531 void tst_QQuickListView::onAdd_data()
4533 QTest::addColumn<int>("initialItemCount");
4534 QTest::addColumn<int>("itemsToAdd");
4536 QTest::newRow("0, add 1") << 0 << 1;
4537 QTest::newRow("0, add 2") << 0 << 2;
4538 QTest::newRow("0, add 10") << 0 << 10;
4540 QTest::newRow("1, add 1") << 1 << 1;
4541 QTest::newRow("1, add 2") << 1 << 2;
4542 QTest::newRow("1, add 10") << 1 << 10;
4544 QTest::newRow("5, add 1") << 5 << 1;
4545 QTest::newRow("5, add 2") << 5 << 2;
4546 QTest::newRow("5, add 10") << 5 << 10;
4549 void tst_QQuickListView::onRemove()
4551 QFETCH(int, initialItemCount);
4552 QFETCH(int, indexToRemove);
4553 QFETCH(int, removeCount);
4555 const int delegateHeight = 10;
4557 for (int i=0; i<initialItemCount; i++)
4558 model.addItem(QString("value %1").arg(i), "dummy value");
4560 QQuickView *window = getView();
4561 QQmlContext *ctxt = window->rootContext();
4562 ctxt->setContextProperty("testModel", &model);
4563 ctxt->setContextProperty("delegateHeight", delegateHeight);
4564 window->setSource(testFileUrl("attachedSignals.qml"));
4566 QObject *object = window->rootObject();
4568 model.removeItems(indexToRemove, removeCount);
4569 QTRY_COMPARE(window->rootObject()->property("count").toInt(), model.count());
4571 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4573 releaseView(window);
4576 void tst_QQuickListView::onRemove_data()
4578 QTest::addColumn<int>("initialItemCount");
4579 QTest::addColumn<int>("indexToRemove");
4580 QTest::addColumn<int>("removeCount");
4582 QTest::newRow("remove first") << 1 << 0 << 1;
4583 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4584 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4585 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4587 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4588 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4589 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4590 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4591 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4592 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4594 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4595 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4596 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4599 void tst_QQuickListView::rightToLeft()
4601 QQuickView *window = createView();
4602 window->setGeometry(0,0,640,320);
4603 window->setSource(testFileUrl("rightToLeft.qml"));
4605 qApp->processEvents();
4607 QVERIFY(window->rootObject() != 0);
4608 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "view");
4609 QTRY_VERIFY(listview != 0);
4611 QQuickItem *contentItem = listview->contentItem();
4612 QTRY_VERIFY(contentItem != 0);
4614 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4616 QQuickVisualItemModel *model = window->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
4617 QTRY_VERIFY(model != 0);
4619 QTRY_VERIFY(model->count() == 3);
4620 QTRY_COMPARE(listview->currentIndex(), 0);
4622 // initial position at first item, right edge aligned
4623 QCOMPARE(listview->contentX(), -640.);
4625 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
4627 QTRY_COMPARE(item->x(), -100.0);
4628 QCOMPARE(item->height(), listview->height());
4630 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
4632 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
4634 listview->setCurrentIndex(2);
4636 item = findItem<QQuickItem>(contentItem, "item3");
4638 QTRY_COMPARE(item->x(), -540.0);
4640 text = findItem<QQuickText>(contentItem, "text3");
4642 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
4644 QCOMPARE(listview->contentX(), -640.);
4646 // Ensure resizing maintains position relative to right edge
4647 qobject_cast<QQuickItem*>(window->rootObject())->setWidth(600);
4648 QTRY_COMPARE(listview->contentX(), -600.);
4653 void tst_QQuickListView::test_mirroring()
4655 QQuickView *windowA = createView();
4656 windowA->setSource(testFileUrl("rightToLeft.qml"));
4657 QQuickListView *listviewA = findItem<QQuickListView>(windowA->rootObject(), "view");
4658 QTRY_VERIFY(listviewA != 0);
4660 QQuickView *windowB = createView();
4661 windowB->setSource(testFileUrl("rightToLeft.qml"));
4662 QQuickListView *listviewB = findItem<QQuickListView>(windowB->rootObject(), "view");
4663 QTRY_VERIFY(listviewA != 0);
4664 qApp->processEvents();
4666 QList<QString> objectNames;
4667 objectNames << "item1" << "item2"; // << "item3"
4669 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4670 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4671 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4674 foreach (const QString objectName, objectNames)
4675 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4677 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4678 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4681 foreach (const QString objectName, objectNames)
4682 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4684 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4685 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4686 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4688 // LTR != LTR+mirror
4689 foreach (const QString objectName, objectNames)
4690 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4692 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4694 // RTL == LTR+mirror
4695 foreach (const QString objectName, objectNames)
4696 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4698 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4700 // RTL != RTL+mirror
4701 foreach (const QString objectName, objectNames)
4702 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4704 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4706 // LTR == RTL+mirror
4707 foreach (const QString objectName, objectNames)
4708 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4714 void tst_QQuickListView::margins()
4716 QQuickView *window = createView();
4719 for (int i = 0; i < 50; i++)
4720 model.addItem("Item" + QString::number(i), "");
4722 QQmlContext *ctxt = window->rootContext();
4723 ctxt->setContextProperty("testModel", &model);
4725 window->setSource(testFileUrl("margins.qml"));
4727 qApp->processEvents();
4729 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4730 QTRY_VERIFY(listview != 0);
4731 QQuickItem *contentItem = listview->contentItem();
4732 QTRY_VERIFY(contentItem != 0);
4733 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4735 QCOMPARE(listview->contentY(), -30.);
4736 QCOMPARE(listview->originY(), 0.);
4739 listview->positionViewAtEnd();
4740 qreal pos = listview->contentY();
4741 listview->setContentY(pos + 80);
4742 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4743 listview->returnToBounds();
4744 QTRY_COMPARE(listview->contentY(), pos + 50);
4746 // remove item before visible and check that top margin is maintained
4747 // and originY is updated
4748 listview->setContentY(100);
4749 model.removeItem(1);
4750 QTRY_COMPARE(listview->count(), model.count());
4751 listview->setContentY(-50);
4752 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4753 listview->returnToBounds();
4754 QCOMPARE(listview->originY(), 20.);
4755 QTRY_COMPARE(listview->contentY(), -10.);
4757 // reduce top margin
4758 listview->setTopMargin(20);
4759 QCOMPARE(listview->originY(), 20.);
4760 QTRY_COMPARE(listview->contentY(), 0.);
4763 listview->positionViewAtEnd();
4764 pos = listview->contentY();
4765 listview->setContentY(pos + 80);
4766 listview->returnToBounds();
4767 QTRY_COMPARE(listview->contentY(), pos + 50);
4769 // reduce bottom margin
4770 pos = listview->contentY();
4771 listview->setBottomMargin(40);
4772 QCOMPARE(listview->originY(), 20.);
4773 QTRY_COMPARE(listview->contentY(), pos-10);
4779 void tst_QQuickListView::marginsResize()
4781 QFETCH(QQuickListView::Orientation, orientation);
4782 QFETCH(Qt::LayoutDirection, layoutDirection);
4783 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4784 QFETCH(qreal, start);
4787 QPoint flickStart(20, 20);
4788 QPoint flickEnd(20, 20);
4789 if (orientation == QQuickListView::Vertical)
4790 flickStart.ry() += (verticalLayoutDirection == QQuickItemView::TopToBottom) ? 180 : -180;
4792 flickStart.rx() += (layoutDirection == Qt::LeftToRight) ? 180 : -180;
4794 QQuickView *window = getView();
4796 window->setSource(testFileUrl("margins2.qml"));
4798 qApp->processEvents();
4800 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "listview");
4801 QTRY_VERIFY(listview != 0);
4803 listview->setOrientation(orientation);
4804 listview->setLayoutDirection(layoutDirection);
4805 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4806 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4808 // view is resized after componentCompleted - top margin should still be visible
4809 if (orientation == QQuickListView::Vertical)
4810 QCOMPARE(listview->contentY(), start);
4812 QCOMPARE(listview->contentX(), start);
4814 // move to last index and ensure bottom margin is visible.
4815 listview->setCurrentIndex(19);
4816 if (orientation == QQuickListView::Vertical)
4817 QTRY_COMPARE(listview->contentY(), end);
4819 QTRY_COMPARE(listview->contentX(), end);
4821 // flick past the end and check content pos still settles on correct extents
4822 flick(window, flickStart, flickEnd, 180);
4823 QTRY_VERIFY(listview->isMoving() == false);
4824 if (orientation == QQuickListView::Vertical)
4825 QTRY_COMPARE(listview->contentY(), end);
4827 QTRY_COMPARE(listview->contentX(), end);
4829 // back to top - top margin should be visible.
4830 listview->setCurrentIndex(0);
4831 if (orientation == QQuickListView::Vertical)
4832 QTRY_COMPARE(listview->contentY(), start);
4834 QTRY_COMPARE(listview->contentX(), start);
4836 // flick past the beginning and check content pos still settles on correct extents
4837 flick(window, flickEnd, flickStart, 180);
4838 QTRY_VERIFY(listview->isMoving() == false);
4839 if (orientation == QQuickListView::Vertical)
4840 QTRY_COMPARE(listview->contentY(), start);
4842 QTRY_COMPARE(listview->contentX(), start);
4844 releaseView(window);
4847 void tst_QQuickListView::marginsResize_data()
4849 QTest::addColumn<QQuickListView::Orientation>("orientation");
4850 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4851 QTest::addColumn<QQuickListView::VerticalLayoutDirection>("verticalLayoutDirection");
4852 QTest::addColumn<qreal>("start");
4853 QTest::addColumn<qreal>("end");
4855 // in Right to Left mode, leftMargin still means leftMargin - it doesn't reverse to mean rightMargin
4857 QTest::newRow("vertical")
4858 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom
4861 QTest::newRow("vertical, BottomToTop")
4862 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop
4863 << -180.0 << -1240.0;
4865 QTest::newRow("horizontal")
4866 << QQuickListView::Horizontal<< Qt::LeftToRight << QQuickItemView::TopToBottom
4869 QTest::newRow("horizontal, rtl")
4870 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom
4871 << -180.0 << -1240.0;
4874 void tst_QQuickListView::snapToItem_data()
4876 QTest::addColumn<QQuickListView::Orientation>("orientation");
4877 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4878 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
4879 QTest::addColumn<int>("highlightRangeMode");
4880 QTest::addColumn<QPoint>("flickStart");
4881 QTest::addColumn<QPoint>("flickEnd");
4882 QTest::addColumn<qreal>("snapAlignment");
4883 QTest::addColumn<qreal>("endExtent");
4884 QTest::addColumn<qreal>("startExtent");
4886 QTest::newRow("vertical, top to bottom")
4887 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4888 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4890 QTest::newRow("vertical, bottom to top")
4891 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
4892 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 << -240.0;
4894 QTest::newRow("horizontal, left to right")
4895 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4896 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 560.0 << 0.0;
4898 QTest::newRow("horizontal, right to left")
4899 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
4900 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 << -240.0;
4902 QTest::newRow("vertical, top to bottom, enforce range")
4903 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4904 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4906 QTest::newRow("vertical, bottom to top, enforce range")
4907 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
4908 << QPoint(20, 20) << QPoint(20, 200) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4910 QTest::newRow("horizontal, left to right, enforce range")
4911 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4912 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 700.0 << -20.0;
4914 QTest::newRow("horizontal, right to left, enforce range")
4915 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
4916 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -560.0 - 240.0 - 140.0 << -220.0;
4919 void tst_QQuickListView::snapToItem()
4921 QFETCH(QQuickListView::Orientation, orientation);
4922 QFETCH(Qt::LayoutDirection, layoutDirection);
4923 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
4924 QFETCH(int, highlightRangeMode);
4925 QFETCH(QPoint, flickStart);
4926 QFETCH(QPoint, flickEnd);
4927 QFETCH(qreal, snapAlignment);
4928 QFETCH(qreal, endExtent);
4929 QFETCH(qreal, startExtent);
4931 QQuickView *window = getView();
4933 window->setSource(testFileUrl("snapToItem.qml"));
4935 qApp->processEvents();
4937 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
4938 QTRY_VERIFY(listview != 0);
4940 listview->setOrientation(orientation);
4941 listview->setLayoutDirection(layoutDirection);
4942 listview->setVerticalLayoutDirection(verticalLayoutDirection);
4943 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4944 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4946 QQuickItem *contentItem = listview->contentItem();
4947 QTRY_VERIFY(contentItem != 0);
4949 // confirm that a flick hits an item boundary
4950 flick(window, flickStart, flickEnd, 180);
4951 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4952 if (orientation == QQuickListView::Vertical)
4953 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4955 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4959 flick(window, flickStart, flickEnd, 180);
4960 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4961 } while (orientation == QQuickListView::Vertical
4962 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
4963 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4965 if (orientation == QQuickListView::Vertical)
4966 QCOMPARE(listview->contentY(), endExtent);
4968 QCOMPARE(listview->contentX(), endExtent);
4972 flick(window, flickEnd, flickStart, 180);
4973 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4974 } while (orientation == QQuickListView::Vertical
4975 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
4976 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4978 if (orientation == QQuickListView::Vertical)
4979 QCOMPARE(listview->contentY(), startExtent);
4981 QCOMPARE(listview->contentX(), startExtent);
4983 releaseView(window);
4986 void tst_QQuickListView::qAbstractItemModel_package_items()
4988 items<QaimModel>(testFileUrl("listviewtest-package.qml"), true);
4991 void tst_QQuickListView::qAbstractItemModel_items()
4993 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
4996 void tst_QQuickListView::qAbstractItemModel_package_changed()
4998 changed<QaimModel>(testFileUrl("listviewtest-package.qml"), true);
5001 void tst_QQuickListView::qAbstractItemModel_changed()
5003 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5006 void tst_QQuickListView::qAbstractItemModel_package_inserted()
5008 inserted<QaimModel>(testFileUrl("listviewtest-package.qml"));
5011 void tst_QQuickListView::qAbstractItemModel_inserted()
5013 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
5016 void tst_QQuickListView::qAbstractItemModel_inserted_more()
5018 inserted_more<QaimModel>();
5021 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
5023 inserted_more_data();
5026 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop()
5028 inserted_more<QaimModel>(QQuickItemView::BottomToTop);
5031 void tst_QQuickListView::qAbstractItemModel_inserted_more_bottomToTop_data()
5033 inserted_more_data();
5036 void tst_QQuickListView::qAbstractItemModel_package_removed()
5038 removed<QaimModel>(testFileUrl("listviewtest-package.qml"), false);
5039 removed<QaimModel>(testFileUrl("listviewtest-package.qml"), true);
5042 void tst_QQuickListView::qAbstractItemModel_removed()
5044 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
5045 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
5048 void tst_QQuickListView::qAbstractItemModel_removed_more()
5050 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
5053 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
5055 removed_more_data();
5058 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop()
5060 removed_more<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5063 void tst_QQuickListView::qAbstractItemModel_removed_more_bottomToTop_data()
5065 removed_more_data();
5068 void tst_QQuickListView::qAbstractItemModel_package_moved()
5070 moved<QaimModel>(testFileUrl("listviewtest-package.qml"));
5073 void tst_QQuickListView::qAbstractItemModel_package_moved_data()
5078 void tst_QQuickListView::qAbstractItemModel_moved()
5080 moved<QaimModel>(testFileUrl("listviewtest.qml"));
5083 void tst_QQuickListView::qAbstractItemModel_moved_data()
5088 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop()
5090 moved<QaimModel>(testFileUrl("listviewtest-package.qml"), QQuickItemView::BottomToTop);
5093 void tst_QQuickListView::qAbstractItemModel_moved_bottomToTop_data()
5098 void tst_QQuickListView::qAbstractItemModel_package_clear()
5100 clear<QaimModel>(testFileUrl("listviewtest-package.qml"));
5103 void tst_QQuickListView::qAbstractItemModel_clear()
5105 clear<QaimModel>(testFileUrl("listviewtest.qml"));
5108 void tst_QQuickListView::qAbstractItemModel_clear_bottomToTop()
5110 clear<QaimModel>(testFileUrl("listviewtest.qml"), QQuickItemView::BottomToTop);
5113 void tst_QQuickListView::qAbstractItemModel_package_sections()
5115 sections<QaimModel>(testFileUrl("listview-sections-package.qml"));
5118 void tst_QQuickListView::qAbstractItemModel_sections()
5120 sections<QaimModel>(testFileUrl("listview-sections.qml"));
5123 void tst_QQuickListView::creationContext()
5126 window.setGeometry(0,0,240,320);
5127 window.setSource(testFileUrl("creationContext.qml"));
5128 qApp->processEvents();
5130 QQuickItem *rootItem = qobject_cast<QQuickItem *>(window.rootObject());
5132 QVERIFY(rootItem->property("count").toInt() > 0);
5135 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
5136 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5137 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
5138 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5139 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
5140 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5141 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
5142 QCOMPARE(item->property("text").toString(), QString("Hello!"));
5145 void tst_QQuickListView::QTBUG_21742()
5148 window.setGeometry(0,0,200,200);
5149 window.setSource(testFileUrl("qtbug-21742.qml"));
5150 qApp->processEvents();
5152 QQuickItem *rootItem = qobject_cast<QQuickItem *>(window.rootObject());
5154 QCOMPARE(rootItem->property("count").toInt(), 1);
5157 void tst_QQuickListView::asynchronous()
5159 QQuickView *window = createView();
5161 QQmlIncubationController controller;
5162 window->engine()->setIncubationController(&controller);
5164 window->setSource(testFileUrl("asyncloader.qml"));
5166 QQuickItem *rootObject = qobject_cast<QQuickItem*>(window->rootObject());
5167 QVERIFY(rootObject);
5169 QQuickListView *listview = 0;
5172 controller.incubateWhile(&b);
5173 listview = rootObject->findChild<QQuickListView*>("view");
5176 // items will be created one at a time
5177 for (int i = 0; i < 8; ++i) {
5178 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
5179 QQuickItem *item = 0;
5182 controller.incubateWhile(&b);
5183 item = findItem<QQuickItem>(listview, "wrapper", i);
5189 controller.incubateWhile(&b);
5192 // verify positioning
5193 QQuickItem *contentItem = listview->contentItem();
5194 for (int i = 0; i < 8; ++i) {
5195 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5196 QTRY_COMPARE(item->y(), i*50.0);
5202 void tst_QQuickListView::snapOneItem_data()
5204 QTest::addColumn<QQuickListView::Orientation>("orientation");
5205 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
5206 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
5207 QTest::addColumn<int>("highlightRangeMode");
5208 QTest::addColumn<QPoint>("flickStart");
5209 QTest::addColumn<QPoint>("flickEnd");
5210 QTest::addColumn<qreal>("snapAlignment");
5211 QTest::addColumn<qreal>("endExtent");
5212 QTest::addColumn<qreal>("startExtent");
5214 QTest::newRow("vertical, top to bottom")
5215 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5216 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5218 QTest::newRow("vertical, bottom to top")
5219 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::NoHighlightRange)
5220 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -560.0 - 240.0 << -240.0;
5222 QTest::newRow("horizontal, left to right")
5223 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5224 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
5226 QTest::newRow("horizontal, right to left")
5227 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::NoHighlightRange)
5228 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
5230 QTest::newRow("vertical, top to bottom, enforce range")
5231 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5232 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5234 QTest::newRow("vertical, bottom to top, enforce range")
5235 << QQuickListView::Vertical << Qt::LeftToRight << QQuickItemView::BottomToTop << int(QQuickItemView::StrictlyEnforceRange)
5236 << QPoint(20, 20) << QPoint(20, 200) << -420.0 << -580.0 - 240.0 << -220.0;
5238 QTest::newRow("horizontal, left to right, enforce range")
5239 << QQuickListView::Horizontal << Qt::LeftToRight << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5240 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
5242 QTest::newRow("horizontal, right to left, enforce range")
5243 << QQuickListView::Horizontal << Qt::RightToLeft << QQuickItemView::TopToBottom << int(QQuickItemView::StrictlyEnforceRange)
5244 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
5247 void tst_QQuickListView::snapOneItem()
5249 QFETCH(QQuickListView::Orientation, orientation);
5250 QFETCH(Qt::LayoutDirection, layoutDirection);
5251 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
5252 QFETCH(int, highlightRangeMode);
5253 QFETCH(QPoint, flickStart);
5254 QFETCH(QPoint, flickEnd);
5255 QFETCH(qreal, snapAlignment);
5256 QFETCH(qreal, endExtent);
5257 QFETCH(qreal, startExtent);
5259 QQuickView *window = getView();
5261 window->setSource(testFileUrl("snapOneItem.qml"));
5263 qApp->processEvents();
5265 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
5266 QTRY_VERIFY(listview != 0);
5268 listview->setOrientation(orientation);
5269 listview->setLayoutDirection(layoutDirection);
5270 listview->setVerticalLayoutDirection(verticalLayoutDirection);
5271 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
5272 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5274 QQuickItem *contentItem = listview->contentItem();
5275 QTRY_VERIFY(contentItem != 0);
5277 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
5279 // confirm that a flick hits the next item boundary
5280 flick(window, flickStart, flickEnd, 180);
5281 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5282 if (orientation == QQuickListView::Vertical)
5283 QCOMPARE(listview->contentY(), snapAlignment);
5285 QCOMPARE(listview->contentX(), snapAlignment);
5287 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5288 QCOMPARE(listview->currentIndex(), 1);
5289 QCOMPARE(currentIndexSpy.count(), 1);
5294 flick(window, flickStart, flickEnd, 180);
5295 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5296 } while (orientation == QQuickListView::Vertical
5297 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYEnd() : !listview->isAtYBeginning()
5298 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
5300 if (orientation == QQuickListView::Vertical)
5301 QCOMPARE(listview->contentY(), endExtent);
5303 QCOMPARE(listview->contentX(), endExtent);
5305 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5306 QCOMPARE(listview->currentIndex(), 3);
5307 QCOMPARE(currentIndexSpy.count(), 3);
5312 flick(window, flickEnd, flickStart, 180);
5313 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
5314 } while (orientation == QQuickListView::Vertical
5315 ? verticalLayoutDirection == QQuickItemView::TopToBottom ? !listview->isAtYBeginning() : !listview->isAtYEnd()
5316 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
5318 if (orientation == QQuickListView::Vertical)
5319 QCOMPARE(listview->contentY(), startExtent);
5321 QCOMPARE(listview->contentX(), startExtent);
5323 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
5324 QCOMPARE(listview->currentIndex(), 0);
5325 QCOMPARE(currentIndexSpy.count(), 6);
5328 releaseView(window);
5331 void tst_QQuickListView::unrequestedVisibility()
5334 for (int i = 0; i < 30; i++)
5335 model.addItem("Item" + QString::number(i), QString::number(i));
5337 QQuickView *window = new QQuickView(0);
5338 window->setGeometry(0,0,240,320);
5340 QQmlContext *ctxt = window->rootContext();
5341 ctxt->setContextProperty("testModel", &model);
5342 ctxt->setContextProperty("testWrap", QVariant(false));
5344 window->setSource(testFileUrl("unrequestedItems.qml"));
5346 qApp->processEvents();
5348 QQuickListView *leftview = findItem<QQuickListView>(window->rootObject(), "leftList");
5349 QTRY_VERIFY(leftview != 0);
5351 QQuickListView *rightview = findItem<QQuickListView>(window->rootObject(), "rightList");
5352 QTRY_VERIFY(rightview != 0);
5354 QQuickItem *leftContent = leftview->contentItem();
5355 QTRY_VERIFY(leftContent != 0);
5357 QQuickItem *rightContent = rightview->contentItem();
5358 QTRY_VERIFY(rightContent != 0);
5360 rightview->setCurrentIndex(20);
5362 QTRY_COMPARE(leftview->contentY(), 0.0);
5363 QTRY_COMPARE(rightview->contentY(), 100.0);
5367 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5368 QCOMPARE(delegateVisible(item), true);
5369 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5370 QCOMPARE(delegateVisible(item), false);
5372 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5373 QCOMPARE(delegateVisible(item), false);
5374 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5375 QCOMPARE(delegateVisible(item), true);
5377 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
5378 QCOMPARE(delegateVisible(item), true);
5379 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
5380 QCOMPARE(delegateVisible(item), false);
5381 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5382 QCOMPARE(delegateVisible(item), false);
5383 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5384 QCOMPARE(delegateVisible(item), true);
5386 rightview->setCurrentIndex(0);
5388 QTRY_COMPARE(leftview->contentY(), 0.0);
5389 QTRY_COMPARE(rightview->contentY(), 0.0);
5391 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5392 QCOMPARE(delegateVisible(item), true);
5393 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5394 QTRY_COMPARE(delegateVisible(item), true);
5396 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
5397 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
5399 leftview->setCurrentIndex(20);
5401 QTRY_COMPARE(leftview->contentY(), 100.0);
5402 QTRY_COMPARE(rightview->contentY(), 0.0);
5404 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5405 QTRY_COMPARE(delegateVisible(item), false);
5406 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5407 QCOMPARE(delegateVisible(item), true);
5409 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5410 QCOMPARE(delegateVisible(item), true);
5411 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5412 QCOMPARE(delegateVisible(item), false);
5414 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5415 QCOMPARE(delegateVisible(item), false);
5416 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5417 QCOMPARE(delegateVisible(item), true);
5418 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5419 QCOMPARE(delegateVisible(item), true);
5420 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5421 QCOMPARE(delegateVisible(item), false);
5423 model.moveItems(19, 1, 1);
5424 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5426 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5427 QCOMPARE(delegateVisible(item), false);
5428 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5429 QCOMPARE(delegateVisible(item), true);
5431 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
5432 QCOMPARE(delegateVisible(item), true);
5433 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
5434 QCOMPARE(delegateVisible(item), false);
5436 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5437 QCOMPARE(delegateVisible(item), false);
5438 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5439 QCOMPARE(delegateVisible(item), true);
5440 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5441 QCOMPARE(delegateVisible(item), true);
5442 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5443 QCOMPARE(delegateVisible(item), false);
5445 model.moveItems(3, 4, 1);
5446 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5448 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5449 QCOMPARE(delegateVisible(item), false);
5450 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5451 QCOMPARE(delegateVisible(item), true);
5452 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5453 QCOMPARE(delegateVisible(item), true);
5454 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5455 QCOMPARE(delegateVisible(item), false);
5457 model.moveItems(4, 3, 1);
5458 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5460 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5461 QCOMPARE(delegateVisible(item), false);
5462 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5463 QCOMPARE(delegateVisible(item), true);
5464 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5465 QCOMPARE(delegateVisible(item), true);
5466 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5467 QCOMPARE(delegateVisible(item), false);
5469 model.moveItems(16, 17, 1);
5470 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5472 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5473 QCOMPARE(delegateVisible(item), false);
5474 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5475 QCOMPARE(delegateVisible(item), true);
5476 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5477 QCOMPARE(delegateVisible(item), true);
5478 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5479 QCOMPARE(delegateVisible(item), false);
5481 model.moveItems(17, 16, 1);
5482 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5484 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
5485 QCOMPARE(delegateVisible(item), false);
5486 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5487 QCOMPARE(delegateVisible(item), true);
5488 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
5489 QCOMPARE(delegateVisible(item), true);
5490 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
5491 QCOMPARE(delegateVisible(item), false);
5496 void tst_QQuickListView::populateTransitions()
5498 QFETCH(bool, staticallyPopulate);
5499 QFETCH(bool, dynamicallyPopulate);
5500 QFETCH(bool, usePopulateTransition);
5502 QPointF transitionFrom(-50, -50);
5503 QPointF transitionVia(100, 100);
5504 QaimModel model_transitionFrom;
5505 QaimModel model_transitionVia;
5508 if (staticallyPopulate) {
5509 for (int i = 0; i < 30; i++)
5510 model.addItem("item" + QString::number(i), "");
5513 QQuickView *window = getView();
5514 window->rootContext()->setContextProperty("testModel", &model);
5515 window->rootContext()->setContextProperty("testObject", new TestObject(window->rootContext()));
5516 window->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
5517 window->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
5518 window->rootContext()->setContextProperty("transitionFrom", transitionFrom);
5519 window->rootContext()->setContextProperty("transitionVia", transitionVia);
5520 window->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
5521 window->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
5522 window->setSource(testFileUrl("populateTransitions.qml"));
5524 QVERIFY(QTest::qWaitForWindowExposed(window));
5526 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
5528 QQuickItem *contentItem = listview->contentItem();
5529 QVERIFY(contentItem);
5531 if (staticallyPopulate && usePopulateTransition) {
5532 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 16);
5533 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5534 } else if (dynamicallyPopulate) {
5535 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5536 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 16);
5538 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5539 QCOMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5540 QCOMPARE(listview->property("countAddTransitions").toInt(), 0);
5543 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5544 for (int i=0; i < model.count() && i < itemCount; ++i) {
5545 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5546 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5547 QTRY_COMPARE(item->x(), 0.0);
5548 QTRY_COMPARE(item->y(), i*20.0);
5549 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5551 QTRY_COMPARE(name->text(), model.name(i));
5554 listview->setProperty("countPopulateTransitions", 0);
5555 listview->setProperty("countAddTransitions", 0);
5557 // add an item and check this is done with add transition, not populate
5558 model.insertItem(0, "another item", "");
5559 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
5560 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
5563 window->rootContext()->setContextProperty("testModel", QVariant());
5564 QTRY_COMPARE(listview->count(), 0);
5565 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
5566 listview->setProperty("countPopulateTransitions", 0);
5567 listview->setProperty("countAddTransitions", 0);
5569 // set to a valid model and check populate transition is run a second time
5571 for (int i = 0; i < 30; i++)
5572 model.addItem("item" + QString::number(i), "");
5573 window->rootContext()->setContextProperty("testModel", &model);
5574 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5575 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5577 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5578 for (int i=0; i < model.count() && i < itemCount; ++i) {
5579 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5580 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5581 QTRY_COMPARE(item->x(), 0.0);
5582 QTRY_COMPARE(item->y(), i*20.0);
5583 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5585 QTRY_COMPARE(name->text(), model.name(i));
5588 // reset model and check populate transition is run again
5589 listview->setProperty("countPopulateTransitions", 0);
5590 listview->setProperty("countAddTransitions", 0);
5592 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 16 : 0);
5593 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
5595 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5596 for (int i=0; i < model.count() && i < itemCount; ++i) {
5597 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5598 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5599 QTRY_COMPARE(item->x(), 0.0);
5600 QTRY_COMPARE(item->y(), i*20.0);
5601 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5603 QTRY_COMPARE(name->text(), model.name(i));
5606 releaseView(window);
5609 void tst_QQuickListView::populateTransitions_data()
5611 QTest::addColumn<bool>("staticallyPopulate");
5612 QTest::addColumn<bool>("dynamicallyPopulate");
5613 QTest::addColumn<bool>("usePopulateTransition");
5615 QTest::newRow("static") << true << false << true;
5616 QTest::newRow("static, no populate") << true << false << false;
5618 QTest::newRow("dynamic") << false << true << true;
5619 QTest::newRow("dynamic, no populate") << false << true << false;
5621 QTest::newRow("empty to start with") << false << false << true;
5622 QTest::newRow("empty to start with, no populate") << false << false << false;
5625 void tst_QQuickListView::addTransitions()
5627 QFETCH(int, initialItemCount);
5628 QFETCH(bool, shouldAnimateTargets);
5629 QFETCH(qreal, contentY);
5630 QFETCH(int, insertionIndex);
5631 QFETCH(int, insertionCount);
5632 QFETCH(ListRange, expectedDisplacedIndexes);
5634 // added items should start here
5635 QPointF targetItems_transitionFrom(-50, -50);
5637 // displaced items should pass through this point
5638 QPointF displacedItems_transitionVia(100, 100);
5641 for (int i = 0; i < initialItemCount; i++)
5642 model.addItem("Original item" + QString::number(i), "");
5643 QaimModel model_targetItems_transitionFrom;
5644 QaimModel model_displacedItems_transitionVia;
5646 QQuickView *window = getView();
5647 QQmlContext *ctxt = window->rootContext();
5648 TestObject *testObject = new TestObject;
5649 ctxt->setContextProperty("testModel", &model);
5650 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
5651 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5652 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
5653 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5654 ctxt->setContextProperty("testObject", testObject);
5655 window->setSource(testFileUrl("addTransitions.qml"));
5657 QVERIFY(QTest::qWaitForWindowExposed(window));
5659 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
5660 QTRY_VERIFY(listview != 0);
5661 QQuickItem *contentItem = listview->contentItem();
5662 QVERIFY(contentItem != 0);
5663 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5665 if (contentY != 0) {
5666 listview->setContentY(contentY);
5667 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5670 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5672 // only target items that will become visible should be animated
5673 QList<QPair<QString, QString> > newData;
5674 QList<QPair<QString, QString> > expectedTargetData;
5675 QList<int> targetIndexes;
5676 if (shouldAnimateTargets) {
5677 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
5678 newData << qMakePair(QString("New item %1").arg(i), QString(""));
5680 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
5681 expectedTargetData << newData.last();
5685 QVERIFY(expectedTargetData.count() > 0);
5689 if (!newData.isEmpty()) {
5690 model.insertItems(insertionIndex, newData);
5691 QTRY_COMPARE(model.count(), listview->count());
5694 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5696 if (shouldAnimateTargets) {
5697 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5698 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5699 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5701 // check the target and displaced items were animated
5702 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5703 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5705 // check attached properties
5706 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5707 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5708 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5709 if (expectedDisplacedIndexes.isValid()) {
5710 // adjust expectedDisplacedIndexes to their final values after the move
5711 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5712 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5713 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5714 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5718 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5719 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5722 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5723 int firstVisibleIndex = -1;
5724 int itemCount = items.count();
5725 for (int i=0; i<items.count(); i++) {
5726 if (items[i]->y() >= contentY) {
5727 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5728 firstVisibleIndex = e.evaluate().toInt();
5732 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5734 // verify all items moved to the correct final positions
5735 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5736 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5737 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5738 QTRY_COMPARE(item->y(), i*20.0);
5739 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5741 QTRY_COMPARE(name->text(), model.name(i));
5744 releaseView(window);
5748 void tst_QQuickListView::addTransitions_data()
5750 QTest::addColumn<int>("initialItemCount");
5751 QTest::addColumn<qreal>("contentY");
5752 QTest::addColumn<bool>("shouldAnimateTargets");
5753 QTest::addColumn<int>("insertionIndex");
5754 QTest::addColumn<int>("insertionCount");
5755 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5757 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5758 QTest::newRow("insert 1, just before start")
5759 << 30 << 20.0 << false
5760 << 0 << 1 << ListRange();
5761 QTest::newRow("insert 1, way before start")
5762 << 30 << 20.0 << false
5763 << 0 << 1 << ListRange();
5764 QTest::newRow("insert multiple, just before start")
5765 << 30 << 100.0 << false
5766 << 0 << 3 << ListRange();
5767 QTest::newRow("insert multiple, way before start")
5768 << 30 << 100.0 << false
5769 << 0 << 3 << ListRange();
5771 QTest::newRow("insert 1 at start")
5772 << 30 << 0.0 << true
5773 << 0 << 1 << ListRange(0, 15);
5774 QTest::newRow("insert multiple at start")
5775 << 30 << 0.0 << true
5776 << 0 << 3 << ListRange(0, 15);
5777 QTest::newRow("insert 1 at start, content y not 0")
5778 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5779 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5780 QTest::newRow("insert multiple at start, content y not 0")
5781 << 30 << 40.0 << true // first visible is index 2
5782 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5784 QTest::newRow("insert 1 at start, to empty list")
5786 << 0 << 1 << ListRange();
5787 QTest::newRow("insert multiple at start, to empty list")
5789 << 0 << 3 << ListRange();
5791 QTest::newRow("insert 1 at middle")
5792 << 30 << 0.0 << true
5793 << 5 << 1 << ListRange(5, 15);
5794 QTest::newRow("insert multiple at middle")
5795 << 30 << 0.0 << true
5796 << 5 << 3 << ListRange(5, 15);
5798 QTest::newRow("insert 1 at bottom")
5799 << 30 << 0.0 << true
5800 << 15 << 1 << ListRange(15, 15);
5801 QTest::newRow("insert multiple at bottom")
5802 << 30 << 0.0 << true
5803 << 15 << 3 << ListRange(15, 15);
5804 QTest::newRow("insert 1 at bottom, content y not 0")
5805 << 30 << 20.0 * 3 << true
5806 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5807 QTest::newRow("insert multiple at bottom, content y not 0")
5808 << 30 << 20.0 * 3 << true
5809 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5811 // items added after the last visible will not be animated in, since they
5812 // do not appear in the final view
5813 QTest::newRow("insert 1 after end")
5814 << 30 << 0.0 << false
5815 << 17 << 1 << ListRange();
5816 QTest::newRow("insert multiple after end")
5817 << 30 << 0.0 << false
5818 << 17 << 3 << ListRange();
5821 void tst_QQuickListView::moveTransitions()
5823 QFETCH(int, initialItemCount);
5824 QFETCH(qreal, contentY);
5825 QFETCH(qreal, itemsOffsetAfterMove);
5826 QFETCH(int, moveFrom);
5827 QFETCH(int, moveTo);
5828 QFETCH(int, moveCount);
5829 QFETCH(ListRange, expectedDisplacedIndexes);
5831 // target and displaced items should pass through these points
5832 QPointF targetItems_transitionVia(-50, 50);
5833 QPointF displacedItems_transitionVia(100, 100);
5836 for (int i = 0; i < initialItemCount; i++)
5837 model.addItem("Original item" + QString::number(i), "");
5838 QaimModel model_targetItems_transitionVia;
5839 QaimModel model_displacedItems_transitionVia;
5841 QQuickView *window = getView();
5842 QQmlContext *ctxt = window->rootContext();
5843 TestObject *testObject = new TestObject;
5844 ctxt->setContextProperty("testModel", &model);
5845 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5846 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5847 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5848 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5849 ctxt->setContextProperty("testObject", testObject);
5850 window->setSource(testFileUrl("moveTransitions.qml"));
5852 QVERIFY(QTest::qWaitForWindowExposed(window));
5854 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
5855 QTRY_VERIFY(listview != 0);
5856 QQuickItem *contentItem = listview->contentItem();
5857 QVERIFY(contentItem != 0);
5860 if (contentY != 0) {
5861 listview->setContentY(contentY);
5862 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5865 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5867 // Items moving to *or* from visible positions should be animated.
5868 // Otherwise, they should not be animated.
5869 QList<QPair<QString, QString> > expectedTargetData;
5870 QList<int> targetIndexes;
5871 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5872 int toIndex = moveTo + (i - moveFrom);
5873 if (i <= (contentY + listview->height()) / 20
5874 || toIndex < (contentY + listview->height()) / 20) {
5875 expectedTargetData << qMakePair(model.name(i), model.number(i));
5879 // ViewTransition.index provides the indices that items are moving to, not from
5880 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5883 model.moveItems(moveFrom, moveTo, moveCount);
5885 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5886 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5887 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5889 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5891 // check the target and displaced items were animated
5892 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5893 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5895 // check attached properties
5896 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5897 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5898 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5899 if (expectedDisplacedIndexes.isValid()) {
5900 // adjust expectedDisplacedIndexes to their final values after the move
5901 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5902 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5903 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5904 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5907 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5908 int firstVisibleIndex = -1;
5909 for (int i=0; i<items.count(); i++) {
5910 if (items[i]->y() >= contentY) {
5911 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5912 firstVisibleIndex = e.evaluate().toInt();
5916 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5918 // verify all items moved to the correct final positions
5919 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5920 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5921 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5922 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5923 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
5924 name = findItem<QQuickText>(contentItem, "textName", i);
5926 QTRY_COMPARE(name->text(), model.name(i));
5929 releaseView(window);
5933 void tst_QQuickListView::moveTransitions_data()
5935 QTest::addColumn<int>("initialItemCount");
5936 QTest::addColumn<qreal>("contentY");
5937 QTest::addColumn<qreal>("itemsOffsetAfterMove");
5938 QTest::addColumn<int>("moveFrom");
5939 QTest::addColumn<int>("moveTo");
5940 QTest::addColumn<int>("moveCount");
5941 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5943 // when removing from above the visible, all items shift down depending on how many
5944 // items have been removed from above the visible
5945 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
5946 << 1 << 10 << 1 << ListRange(11, 15+4);
5947 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
5948 << 0 << 10 << 1 << ListRange(11, 15+4);
5949 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
5950 << 1 << 10 << 2 << ListRange(12, 15+4);
5951 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
5952 << 0 << 10 << 3 << ListRange(13, 15+4);
5953 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
5954 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
5955 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
5956 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
5958 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
5959 << 1 << 10 << 1 << ListRange(2, 10);
5960 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
5961 << 0 << 10 << 1 << ListRange(1, 10);
5962 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5963 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
5964 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
5965 << 10 << 15 << 1 << ListRange(11, 15);
5966 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
5967 << 0 << 15 << 1 << ListRange(1, 15);
5969 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
5970 << 1 << 10 << 3 << ListRange(4, 12);
5971 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
5972 << 0 << 10 << 3 << ListRange(3, 12);
5973 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5974 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
5975 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
5976 << 5 << 13 << 3 << ListRange(8, 15);
5977 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
5978 << 0 << 13 << 3 << ListRange(3, 15);
5980 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
5981 << 10 << 1 << 1 << ListRange(1, 9);
5982 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
5983 << 10 << 0 << 1 << ListRange(0, 9);
5984 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5985 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5986 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
5987 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5988 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
5989 << 15 << 10 << 1 << ListRange(10, 14);
5990 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
5991 << 15 << 0 << 1 << ListRange(0, 14);
5993 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
5994 << 10 << 1 << 3 << ListRange(1, 9);
5995 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
5996 << 10 << 0 << 3 << ListRange(0, 9);
5997 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5998 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
5999 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
6000 << 13 << 5 << 3 << ListRange(5, 12);
6001 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
6002 << 13 << 0 << 3 << ListRange(0, 12);
6004 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
6005 << 20 << 0 << 1 << ListRange(0, 15);
6006 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6007 << 25 << 4 << 1 << ListRange(0+4, 15+4);
6008 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
6009 << 20 << 0 << 3 << ListRange(0, 15);
6010 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
6011 << 25 << 4 << 3 << ListRange(0+4, 15+4);
6013 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
6014 << 20 << 15 << 1 << ListRange(15, 15);
6015 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6016 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
6017 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
6018 << 20 << 15 << 3 << ListRange(15, 15);
6019 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
6020 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
6023 void tst_QQuickListView::removeTransitions()
6025 QFETCH(int, initialItemCount);
6026 QFETCH(bool, shouldAnimateTargets);
6027 QFETCH(qreal, contentY);
6028 QFETCH(int, removalIndex);
6029 QFETCH(int, removalCount);
6030 QFETCH(ListRange, expectedDisplacedIndexes);
6032 // added items should end here
6033 QPointF targetItems_transitionTo(-50, -50);
6035 // displaced items should pass through this points
6036 QPointF displacedItems_transitionVia(100, 100);
6039 for (int i = 0; i < initialItemCount; i++)
6040 model.addItem("Original item" + QString::number(i), "");
6041 QaimModel model_targetItems_transitionTo;
6042 QaimModel model_displacedItems_transitionVia;
6044 QQuickView *window = getView();
6045 QQmlContext *ctxt = window->rootContext();
6046 TestObject *testObject = new TestObject;
6047 ctxt->setContextProperty("testModel", &model);
6048 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
6049 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
6050 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
6051 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
6052 ctxt->setContextProperty("testObject", testObject);
6053 window->setSource(testFileUrl("removeTransitions.qml"));
6055 QVERIFY(QTest::qWaitForWindowExposed(window));
6057 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
6058 QTRY_VERIFY(listview != 0);
6059 QQuickItem *contentItem = listview->contentItem();
6060 QVERIFY(contentItem != 0);
6061 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6063 if (contentY != 0) {
6064 listview->setContentY(contentY);
6065 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6068 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6070 // only target items that are visible should be animated
6071 QList<QPair<QString, QString> > expectedTargetData;
6072 QList<int> targetIndexes;
6073 if (shouldAnimateTargets) {
6074 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
6075 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
6076 expectedTargetData << qMakePair(model.name(i), model.number(i));
6080 QVERIFY(expectedTargetData.count() > 0);
6083 // calculate targetItems and expectedTargets before model changes
6084 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
6085 QVariantMap expectedTargets;
6086 for (int i=0; i<targetIndexes.count(); i++)
6087 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
6090 model.removeItems(removalIndex, removalCount);
6091 QTRY_COMPARE(model.count(), listview->count());
6093 if (shouldAnimateTargets) {
6094 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
6095 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
6096 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
6098 // check the target and displaced items were animated
6099 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
6100 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
6102 // check attached properties
6103 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
6104 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
6105 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
6106 if (expectedDisplacedIndexes.isValid()) {
6107 // adjust expectedDisplacedIndexes to their final values after the move
6108 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
6109 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
6110 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
6111 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
6114 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
6115 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
6118 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6119 int firstVisibleIndex = -1;
6120 int itemCount = items.count();
6122 for (int i=0; i<items.count(); i++) {
6123 QQmlExpression e(qmlContext(items[i]), items[i], "index");
6124 int index = e.evaluate().toInt();
6125 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
6126 firstVisibleIndex = index;
6128 itemCount--; // exclude deleted items
6130 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
6132 // verify all items moved to the correct final positions
6133 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
6134 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6135 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6136 QCOMPARE(item->x(), 0.0);
6137 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
6138 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6140 QTRY_COMPARE(name->text(), model.name(i));
6143 releaseView(window);
6147 void tst_QQuickListView::removeTransitions_data()
6149 QTest::addColumn<int>("initialItemCount");
6150 QTest::addColumn<qreal>("contentY");
6151 QTest::addColumn<bool>("shouldAnimateTargets");
6152 QTest::addColumn<int>("removalIndex");
6153 QTest::addColumn<int>("removalCount");
6154 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6156 // All items that are visible following the remove operation should be animated.
6157 // Remove targets that are outside of the view should not be animated.
6159 QTest::newRow("remove 1 before start")
6160 << 30 << 20.0 * 3 << false
6161 << 2 << 1 << ListRange();
6162 QTest::newRow("remove multiple, all before start")
6163 << 30 << 20.0 * 3 << false
6164 << 0 << 3 << ListRange();
6165 QTest::newRow("remove mix of before and after start")
6166 << 30 << 20.0 * 3 << true
6167 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
6169 QTest::newRow("remove 1 from start")
6170 << 30 << 0.0 << true
6171 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
6172 QTest::newRow("remove multiple from start")
6173 << 30 << 0.0 << true
6174 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
6175 QTest::newRow("remove 1 from start, content y not 0")
6176 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
6177 << 2 << 1 << ListRange(1 + 2, 16 + 2);
6178 QTest::newRow("remove multiple from start, content y not 0")
6179 << 30 << 20.0 * 2 << true // first visible is index 2
6180 << 2 << 3 << ListRange(3 + 2, 18 + 2);
6182 QTest::newRow("remove 1 from middle")
6183 << 30 << 0.0 << true
6184 << 5 << 1 << ListRange(6, 16);
6185 QTest::newRow("remove multiple from middle")
6186 << 30 << 0.0 << true
6187 << 5 << 3 << ListRange(8, 18);
6190 QTest::newRow("remove 1 from bottom")
6191 << 30 << 0.0 << true
6192 << 15 << 1 << ListRange(16, 16);
6194 // remove 15, 16, 17
6195 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
6196 // the view, and 18 will be animated as the displaced item to replace the last item
6197 QTest::newRow("remove multiple from bottom")
6198 << 30 << 0.0 << true
6199 << 15 << 3 << ListRange(18, 18);
6201 QTest::newRow("remove 1 from bottom, content y not 0")
6202 << 30 << 20.0 * 2 << true
6203 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
6204 QTest::newRow("remove multiple from bottom, content y not 0")
6205 << 30 << 20.0 * 2 << true
6206 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
6209 QTest::newRow("remove 1 after end")
6210 << 30 << 0.0 << false
6211 << 17 << 1 << ListRange();
6212 QTest::newRow("remove multiple after end")
6213 << 30 << 0.0 << false
6214 << 17 << 3 << ListRange();
6217 void tst_QQuickListView::displacedTransitions()
6219 QFETCH(bool, useDisplaced);
6220 QFETCH(bool, displacedEnabled);
6221 QFETCH(bool, useAddDisplaced);
6222 QFETCH(bool, addDisplacedEnabled);
6223 QFETCH(bool, useMoveDisplaced);
6224 QFETCH(bool, moveDisplacedEnabled);
6225 QFETCH(bool, useRemoveDisplaced);
6226 QFETCH(bool, removeDisplacedEnabled);
6227 QFETCH(ListChange, change);
6228 QFETCH(ListRange, expectedDisplacedIndexes);
6231 for (int i = 0; i < 30; i++)
6232 model.addItem("Original item" + QString::number(i), "");
6233 QaimModel model_displaced_transitionVia;
6234 QaimModel model_addDisplaced_transitionVia;
6235 QaimModel model_moveDisplaced_transitionVia;
6236 QaimModel model_removeDisplaced_transitionVia;
6238 QPointF displaced_transitionVia(-50, -100);
6239 QPointF addDisplaced_transitionVia(-150, 100);
6240 QPointF moveDisplaced_transitionVia(50, -100);
6241 QPointF removeDisplaced_transitionVia(150, 100);
6243 QQuickView *window = getView();
6244 QQmlContext *ctxt = window->rootContext();
6245 TestObject *testObject = new TestObject(window);
6246 ctxt->setContextProperty("testModel", &model);
6247 ctxt->setContextProperty("testObject", testObject);
6248 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
6249 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
6250 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
6251 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
6252 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
6253 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
6254 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
6255 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
6256 ctxt->setContextProperty("useDisplaced", useDisplaced);
6257 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
6258 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
6259 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
6260 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
6261 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
6262 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
6263 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
6264 window->setSource(testFileUrl("displacedTransitions.qml"));
6266 qApp->processEvents();
6268 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
6269 QTRY_VERIFY(listview != 0);
6270 QQuickItem *contentItem = listview->contentItem();
6271 QVERIFY(contentItem != 0);
6272 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6274 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
6275 listview->setProperty("displaceTransitionsDone", false);
6277 switch (change.type) {
6278 case ListChange::Inserted:
6280 QList<QPair<QString, QString> > targetItemData;
6281 for (int i=change.index; i<change.index + change.count; ++i)
6282 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
6283 model.insertItems(change.index, targetItemData);
6284 QTRY_COMPARE(model.count(), listview->count());
6287 case ListChange::Removed:
6288 model.removeItems(change.index, change.count);
6289 QTRY_COMPARE(model.count(), listview->count());
6291 case ListChange::Moved:
6292 model.moveItems(change.index, change.to, change.count);
6293 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6295 case ListChange::SetCurrent:
6296 case ListChange::SetContentY:
6297 case ListChange::Polish:
6301 QVariantList resultTargetIndexes = listview->property("displacedTargetIndexes").toList();
6302 QVariantList resultTargetItems = listview->property("displacedTargetItems").toList();
6304 if ((useDisplaced && displacedEnabled)
6305 || (useAddDisplaced && addDisplacedEnabled)
6306 || (useMoveDisplaced && moveDisplacedEnabled)
6307 || (useRemoveDisplaced && removeDisplacedEnabled)) {
6308 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6310 // check the correct number of target items and indexes were received
6311 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
6312 for (int i=0; i<resultTargetIndexes.count(); i++)
6313 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
6314 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
6315 for (int i=0; i<resultTargetItems.count(); i++)
6316 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
6318 QCOMPARE(resultTargetIndexes.count(), 0);
6319 QCOMPARE(resultTargetItems.count(), 0);
6322 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
6323 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
6325 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
6326 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
6327 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
6329 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
6330 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
6331 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
6333 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
6335 if (useDisplaced && displacedEnabled
6336 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
6337 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
6338 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
6339 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
6341 QCOMPARE(model_displaced_transitionVia.count(), 0);
6344 // verify all items moved to the correct final positions
6345 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6346 for (int i=0; i < model.count() && i < items.count(); ++i) {
6347 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6348 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6349 QCOMPARE(item->x(), 0.0);
6350 QCOMPARE(item->y(), i * 20.0);
6351 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6353 QTRY_COMPARE(name->text(), model.name(i));
6356 releaseView(window);
6359 void tst_QQuickListView::displacedTransitions_data()
6361 QTest::addColumn<bool>("useDisplaced");
6362 QTest::addColumn<bool>("displacedEnabled");
6363 QTest::addColumn<bool>("useAddDisplaced");
6364 QTest::addColumn<bool>("addDisplacedEnabled");
6365 QTest::addColumn<bool>("useMoveDisplaced");
6366 QTest::addColumn<bool>("moveDisplacedEnabled");
6367 QTest::addColumn<bool>("useRemoveDisplaced");
6368 QTest::addColumn<bool>("removeDisplacedEnabled");
6369 QTest::addColumn<ListChange>("change");
6370 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
6372 QTest::newRow("no displaced transitions at all")
6377 << ListChange::insert(0, 1) << ListRange(0, 15);
6379 QTest::newRow("just displaced")
6384 << ListChange::insert(0, 1) << ListRange(0, 15);
6386 QTest::newRow("just displaced (not enabled)")
6391 << ListChange::insert(0, 1) << ListRange(0, 15);
6393 QTest::newRow("displaced + addDisplaced")
6398 << ListChange::insert(0, 1) << ListRange(0, 15);
6400 QTest::newRow("displaced + addDisplaced (not enabled)")
6405 << ListChange::insert(0, 1) << ListRange(0, 15);
6407 QTest::newRow("displaced + moveDisplaced")
6412 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6414 QTest::newRow("displaced + moveDisplaced (not enabled)")
6419 << ListChange::move(0, 10, 1) << ListRange(1, 10);
6421 QTest::newRow("displaced + removeDisplaced")
6426 << ListChange::remove(0, 1) << ListRange(1, 16);
6428 QTest::newRow("displaced + removeDisplaced (not enabled)")
6433 << ListChange::remove(0, 1) << ListRange(1, 16);
6436 QTest::newRow("displaced + add, should use generic displaced for a remove")
6441 << ListChange::remove(0, 1) << ListRange(1, 16);
6444 void tst_QQuickListView::multipleTransitions()
6446 // Tests that if you interrupt a transition in progress with another action that
6447 // cancels the previous transition, the resulting items are still placed correctly.
6449 QFETCH(int, initialCount);
6450 QFETCH(qreal, contentY);
6451 QFETCH(QList<ListChange>, changes);
6452 QFETCH(bool, enableAddTransitions);
6453 QFETCH(bool, enableMoveTransitions);
6454 QFETCH(bool, enableRemoveTransitions);
6455 QFETCH(bool, rippleAddDisplaced);
6457 QPointF addTargets_transitionFrom(-50, -50);
6458 QPointF addDisplaced_transitionFrom(-50, 50);
6459 QPointF moveTargets_transitionFrom(50, -50);
6460 QPointF moveDisplaced_transitionFrom(50, 50);
6461 QPointF removeTargets_transitionTo(-100, 300);
6462 QPointF removeDisplaced_transitionFrom(100, 300);
6465 for (int i = 0; i < initialCount; i++)
6466 model.addItem("Original item" + QString::number(i), "");
6468 QQuickView *window = getView();
6469 QQmlContext *ctxt = window->rootContext();
6470 TestObject *testObject = new TestObject;
6471 ctxt->setContextProperty("testModel", &model);
6472 ctxt->setContextProperty("testObject", testObject);
6473 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
6474 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
6475 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
6476 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
6477 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
6478 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
6479 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
6480 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
6481 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
6482 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
6483 window->setSource(testFileUrl("multipleTransitions.qml"));
6485 QVERIFY(QTest::qWaitForWindowExposed(window));
6487 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
6488 QTRY_VERIFY(listview != 0);
6489 QQuickItem *contentItem = listview->contentItem();
6490 QVERIFY(contentItem != 0);
6491 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6493 if (contentY != 0) {
6494 listview->setContentY(contentY);
6495 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6498 int timeBetweenActions = window->rootObject()->property("timeBetweenActions").toInt();
6500 for (int i=0; i<changes.count(); i++) {
6501 switch (changes[i].type) {
6502 case ListChange::Inserted:
6504 QList<QPair<QString, QString> > targetItems;
6505 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
6506 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
6507 model.insertItems(changes[i].index, targetItems);
6508 QTRY_COMPARE(model.count(), listview->count());
6509 if (i == changes.count() - 1) {
6510 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
6511 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
6513 QTest::qWait(timeBetweenActions);
6517 case ListChange::Removed:
6518 model.removeItems(changes[i].index, changes[i].count);
6519 QTRY_COMPARE(model.count(), listview->count());
6520 if (i == changes.count() - 1) {
6521 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
6522 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
6524 QTest::qWait(timeBetweenActions);
6527 case ListChange::Moved:
6528 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
6529 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6530 if (i == changes.count() - 1) {
6531 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
6532 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
6534 QTest::qWait(timeBetweenActions);
6537 case ListChange::SetCurrent:
6538 listview->setCurrentIndex(changes[i].index);
6540 case ListChange::SetContentY:
6541 listview->setContentY(changes[i].pos);
6542 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6544 case ListChange::Polish:
6548 QCOMPARE(listview->count(), model.count());
6550 // verify all items moved to the correct final positions
6551 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6552 for (int i=0; i < model.count() && i < items.count(); ++i) {
6553 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6554 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6555 QTRY_COMPARE(item->x(), 0.0);
6556 QTRY_COMPARE(item->y(), i*20.0);
6557 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6559 QTRY_COMPARE(name->text(), model.name(i));
6562 releaseView(window);
6566 void tst_QQuickListView::multipleTransitions_data()
6568 QTest::addColumn<int>("initialCount");
6569 QTest::addColumn<qreal>("contentY");
6570 QTest::addColumn<QList<ListChange> >("changes");
6571 QTest::addColumn<bool>("enableAddTransitions");
6572 QTest::addColumn<bool>("enableMoveTransitions");
6573 QTest::addColumn<bool>("enableRemoveTransitions");
6574 QTest::addColumn<bool>("rippleAddDisplaced");
6576 // the added item and displaced items should move to final dest correctly
6577 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
6578 << ListChange::insert(0, 1)
6579 << ListChange::move(0, 3, 1)
6581 << true << true << true << false;
6583 // items affected by the add should change from move to add transition
6584 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
6585 << ListChange::move(1, 10, 3)
6586 << ListChange::insert(0, 1)
6588 << true << true << true << false;
6590 // items should be placed correctly if you trigger a transition then refill for that index
6591 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
6592 << ListChange::insert(0, 1)
6593 << ListChange::setContentY(80.0)
6594 << ListChange::setContentY(0.0)
6595 << ListChange::insert(0, 1)
6597 << true << true << true << false;
6599 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
6600 << ListChange::insert(1, 1)
6601 << ListChange::remove(1, 1)
6603 << true << true << true << true;
6605 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
6606 // even if a remove-displace transition is not present to re-animate them
6607 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6608 << ListChange::insert(0, 1)
6609 << ListChange::remove(2, 1)
6611 << true << true << false << false;
6613 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
6614 // remove has changed the position of where it will move to
6615 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
6616 << ListChange::setContentY(-10.0)
6617 << ListChange::insert(0, 1)
6618 << ListChange::insert(0, 1)
6619 << ListChange::remove(2, 1)
6621 << true << true << false << false;
6624 void tst_QQuickListView::multipleDisplaced()
6626 // multiple move() operations should only restart displace transitions for items that
6627 // moved from previously set positions, and not those that have moved from their current
6628 // item positions (which may e.g. still be changing from easing bounces in the last transition)
6631 for (int i = 0; i < 30; i++)
6632 model.addItem("Original item" + QString::number(i), "");
6634 QQuickView *window = getView();
6635 QQmlContext *ctxt = window->rootContext();
6636 ctxt->setContextProperty("testModel", &model);
6637 ctxt->setContextProperty("testObject", new TestObject(window));
6638 window->setSource(testFileUrl("multipleDisplaced.qml"));
6640 QVERIFY(QTest::qWaitForWindowExposed(window));
6642 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
6643 QTRY_VERIFY(listview != 0);
6644 QQuickItem *contentItem = listview->contentItem();
6645 QVERIFY(contentItem != 0);
6646 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6648 model.moveItems(12, 8, 1);
6649 QTest::qWait(window->rootObject()->property("duration").toInt() / 2);
6650 model.moveItems(8, 3, 1);
6651 QTRY_VERIFY(listview->property("displaceTransitionsDone").toBool());
6653 QVariantMap transitionsStarted = listview->property("displaceTransitionsStarted").toMap();
6654 foreach (const QString &name, transitionsStarted.keys()) {
6655 QVERIFY2(transitionsStarted[name] == 1,
6656 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
6659 // verify all items moved to the correct final positions
6660 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
6661 for (int i=0; i < model.count() && i < items.count(); ++i) {
6662 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6663 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
6664 QTRY_COMPARE(item->x(), 0.0);
6665 QTRY_COMPARE(item->y(), i*20.0);
6666 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
6668 QTRY_COMPARE(name->text(), model.name(i));
6671 releaseView(window);
6674 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
6678 for (int i=0; i<list.count(); i++) {
6679 ret << list[i].toInt(&ok);
6681 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
6687 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6689 for (int i=0; i<indexLists.count(); i++) {
6690 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6691 if (current != expectedIndexes.toSet())
6692 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6693 QCOMPARE(current, expectedIndexes.toSet());
6697 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6699 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6700 QVERIFY(it.value().type() == QVariant::Int);
6701 QString name = it.key();
6702 int itemIndex = it.value().toInt();
6703 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6704 if (model.name(itemIndex) != name)
6705 qDebug() << itemIndex;
6706 QCOMPARE(model.name(itemIndex), name);
6708 QCOMPARE(items.count(), expectedIndexes.count());
6711 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6713 for (int i=0; i<itemLists.count(); i++) {
6714 QVERIFY(itemLists[i].type() == QVariant::List);
6715 QVariantList current = itemLists[i].toList();
6716 for (int j=0; j<current.count(); j++) {
6717 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6718 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6719 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6721 QCOMPARE(current.count(), expectedItems.count());
6725 void tst_QQuickListView::flickBeyondBounds()
6727 QQuickView *window = createView();
6729 window->setSource(testFileUrl("flickBeyondBoundsBug.qml"));
6731 qApp->processEvents();
6733 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
6734 QTRY_VERIFY(listview != 0);
6736 QQuickItem *contentItem = listview->contentItem();
6737 QTRY_VERIFY(contentItem != 0);
6738 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6740 // Flick view up beyond bounds
6741 flick(window, QPoint(10, 10), QPoint(10, -1000), 180);
6742 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 0);
6744 // We're really testing that we don't get stuck in a loop,
6745 // but also confirm items positioned correctly.
6746 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 2);
6747 for (int i = 0; i < 2; ++i) {
6748 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
6749 if (!item) qWarning() << "Item" << i << "not found";
6751 QTRY_VERIFY(item->y() == i*45);
6757 void tst_QQuickListView::destroyItemOnCreation()
6760 QQuickView *window = createView();
6761 window->rootContext()->setContextProperty("testModel", &model);
6763 window->setSource(testFileUrl("destroyItemOnCreation.qml"));
6765 qApp->processEvents();
6767 QQuickListView *listview = findItem<QQuickListView>(window->rootObject(), "list");
6768 QVERIFY(listview != 0);
6770 QQuickItem *contentItem = listview->contentItem();
6771 QVERIFY(contentItem != 0);
6772 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6774 QCOMPARE(window->rootObject()->property("createdIndex").toInt(), -1);
6775 model.addItem("new item", "");
6776 QTRY_COMPARE(window->rootObject()->property("createdIndex").toInt(), 0);
6778 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
6779 QCOMPARE(model.count(), 0);
6784 void tst_QQuickListView::parentBinding()
6786 QQuickView *window = createView();
6789 QtMessageHandler old = qInstallMessageHandler(errorMsgHandler);
6791 window->setSource(testFileUrl("parentBinding.qml"));
6793 QTest::qWaitForWindowExposed(window);
6795 QQuickListView *listview = qobject_cast<QQuickListView*>(window->rootObject());
6796 QVERIFY(listview != 0);
6798 QQuickItem *contentItem = listview->contentItem();
6799 QVERIFY(contentItem != 0);
6800 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
6802 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
6804 QCOMPARE(item->width(), listview->width());
6805 QCOMPARE(item->height(), listview->height()/12);
6807 // there should be no transient binding error
6808 QVERIFY(!m_errorCount);
6810 qInstallMessageHandler(old);
6815 QTEST_MAIN(tst_QQuickListView)
6817 #include "tst_qquicklistview.moc"