1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtCore/QStringListModel>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcontext.h>
47 #include <QtQml/qqmlexpression.h>
48 #include <QtQml/qqmlincubator.h>
49 #include <QtQuick/private/qquickitem_p.h>
50 #include <QtQuick/private/qquicklistview_p.h>
51 #include <QtQuick/private/qquicktext_p.h>
52 #include <QtQuick/private/qquickvisualitemmodel_p.h>
53 #include <QtQml/private/qquicklistmodel_p.h>
54 #include <QtQuick/private/qquickchangeset_p.h>
55 #include "../../shared/util.h"
56 #include "../shared/viewtestutil.h"
57 #include "../shared/visualtestutil.h"
58 #include "incrementalmodel.h"
61 Q_DECLARE_METATYPE(Qt::LayoutDirection)
62 Q_DECLARE_METATYPE(QQuickListView::Orientation)
64 using namespace QQuickViewTestUtil;
65 using namespace QQuickVisualTestUtil;
67 class tst_QQuickListView : public QQmlDataTest
74 // Test both QListModelInterface and QAbstractItemModel model types
75 void qListModelInterface_items();
76 void qListModelInterface_package_items();
77 void qAbstractItemModel_items();
79 void qListModelInterface_changed();
80 void qListModelInterface_package_changed();
81 void qAbstractItemModel_changed();
83 void qListModelInterface_inserted();
84 void qListModelInterface_inserted_more();
85 void qListModelInterface_inserted_more_data();
86 void qListModelInterface_package_inserted();
87 void qAbstractItemModel_inserted();
88 void qAbstractItemModel_inserted_more();
89 void qAbstractItemModel_inserted_more_data();
91 void qListModelInterface_removed();
92 void qListModelInterface_removed_more();
93 void qListModelInterface_removed_more_data();
94 void qListModelInterface_package_removed();
95 void qAbstractItemModel_removed();
96 void qAbstractItemModel_removed_more();
97 void qAbstractItemModel_removed_more_data();
99 void qListModelInterface_moved();
100 void qListModelInterface_moved_data();
101 void qListModelInterface_package_moved();
102 void qListModelInterface_package_moved_data();
103 void qAbstractItemModel_moved();
104 void qAbstractItemModel_moved_data();
106 void multipleChanges();
107 void multipleChanges_data();
109 void qListModelInterface_clear();
110 void qListModelInterface_package_clear();
111 void qAbstractItemModel_clear();
113 void insertBeforeVisible();
114 void insertBeforeVisible_data();
115 void swapWithFirstItem();
117 void currentIndex_delayedItemCreation();
118 void currentIndex_delayedItemCreation_data();
120 void noCurrentIndex();
122 void enforceRange_withoutHighlight();
124 void qListModelInterface_sections();
125 void qListModelInterface_package_sections();
126 void qAbstractItemModel_sections();
127 void sectionsPositioning();
128 void sectionsDelegate();
130 void positionViewAtIndex();
132 void propertyChanges();
133 void componentChanges();
135 void manualHighlight();
138 void header_delayItemCreation();
143 void resizeViewAndRepaint();
144 void sizeLessThan1();
146 void resizeDelegate();
147 void resizeFirstDelegate();
149 void indexAt_itemAt_data();
150 void indexAt_itemAt();
151 void incrementalModel();
155 void onRemove_data();
157 void test_mirroring();
159 void marginsResize();
160 void marginsResize_data();
161 void creationContext();
162 void snapToItem_data();
164 void snapOneItem_data();
172 void unrequestedVisibility();
174 void populateTransitions();
175 void populateTransitions_data();
176 void addTransitions();
177 void addTransitions_data();
178 void moveTransitions();
179 void moveTransitions_data();
180 void removeTransitions();
181 void removeTransitions_data();
182 void multipleTransitions();
183 void multipleTransitions_data();
186 template <class T> void items(const QUrl &source, bool forceLayout);
187 template <class T> void changed(const QUrl &source, bool forceLayout);
188 template <class T> void inserted(const QUrl &source);
189 template <class T> void inserted_more();
190 template <class T> void removed(const QUrl &source, bool animated);
191 template <class T> void removed_more(const QUrl &source);
192 template <class T> void moved(const QUrl &source);
193 template <class T> void clear(const QUrl &source);
194 template <class T> void sections(const QUrl &source);
196 QList<int> toIntList(const QVariantList &list);
197 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
198 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
199 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
201 void inserted_more_data();
202 void removed_more_data();
206 class TestObject : public QObject
210 Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError)
211 Q_PROPERTY(bool animate READ animate NOTIFY changedAnim)
212 Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl)
213 Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer)
216 TestObject(QObject *parent = 0)
217 : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
220 bool error() const { return mError; }
221 void setError(bool err) { mError = err; emit changedError(); }
223 bool animate() const { return mAnimate; }
224 void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
226 bool invalidHighlight() const { return mInvalidHighlight; }
227 void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
229 int cacheBuffer() const { return mCacheBuffer; }
230 void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
236 void changedCacheBuffer();
241 bool mInvalidHighlight;
245 tst_QQuickListView::tst_QQuickListView()
250 void tst_QQuickListView::items(const QUrl &source, bool forceLayout)
252 QQuickView *canvas = createView();
255 model.addItem("Fred", "12345");
256 model.addItem("John", "2345");
257 model.addItem("Bob", "54321");
259 QQmlContext *ctxt = canvas->rootContext();
260 ctxt->setContextProperty("testModel", &model);
262 TestObject *testObject = new TestObject;
263 ctxt->setContextProperty("testObject", testObject);
265 canvas->setSource(source);
266 qApp->processEvents();
268 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
269 QTRY_VERIFY(listview != 0);
271 QQuickItem *contentItem = listview->contentItem();
272 QTRY_VERIFY(contentItem != 0);
274 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
275 QTRY_VERIFY(testObject->error() == false);
277 QTRY_VERIFY(listview->highlightItem() != 0);
278 QTRY_COMPARE(listview->count(), model.count());
279 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
280 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
282 // current item should be first item
283 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
285 for (int i = 0; i < model.count(); ++i) {
286 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
287 QTRY_VERIFY(name != 0);
288 QTRY_COMPARE(name->text(), model.name(i));
289 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
290 QTRY_VERIFY(number != 0);
291 QTRY_COMPARE(number->text(), model.number(i));
294 // switch to other delegate
295 testObject->setAnimate(true);
296 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
297 QTRY_VERIFY(testObject->error() == false);
298 QTRY_VERIFY(listview->currentItem());
300 // set invalid highlight
301 testObject->setInvalidHighlight(true);
302 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
303 QTRY_VERIFY(testObject->error() == false);
304 QTRY_VERIFY(listview->currentItem());
305 QTRY_VERIFY(listview->highlightItem() == 0);
307 // back to normal highlight
308 testObject->setInvalidHighlight(false);
309 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
310 QTRY_VERIFY(testObject->error() == false);
311 QTRY_VERIFY(listview->currentItem());
312 QTRY_VERIFY(listview->highlightItem() != 0);
314 // set an empty model and confirm that items are destroyed
316 ctxt->setContextProperty("testModel", &model2);
318 // Force a layout, necessary if ListView is completed before VisualDataModel.
320 QCOMPARE(listview->property("count").toInt(), 0);
322 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
323 QTRY_VERIFY(itemCount == 0);
325 QTRY_COMPARE(listview->highlightResizeSpeed(), 1000.0);
326 QTRY_COMPARE(listview->highlightMoveSpeed(), 1000.0);
334 void tst_QQuickListView::changed(const QUrl &source, bool forceLayout)
336 QQuickView *canvas = createView();
339 model.addItem("Fred", "12345");
340 model.addItem("John", "2345");
341 model.addItem("Bob", "54321");
343 QQmlContext *ctxt = canvas->rootContext();
344 ctxt->setContextProperty("testModel", &model);
346 TestObject *testObject = new TestObject;
347 ctxt->setContextProperty("testObject", testObject);
349 canvas->setSource(source);
350 qApp->processEvents();
352 QQuickFlickable *listview = findItem<QQuickFlickable>(canvas->rootObject(), "list");
353 QTRY_VERIFY(listview != 0);
355 QQuickItem *contentItem = listview->contentItem();
356 QTRY_VERIFY(contentItem != 0);
358 // Force a layout, necessary if ListView is completed before VisualDataModel.
360 QCOMPARE(listview->property("count").toInt(), model.count());
362 model.modifyItem(1, "Will", "9876");
363 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
364 QTRY_VERIFY(name != 0);
365 QTRY_COMPARE(name->text(), model.name(1));
366 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
367 QTRY_VERIFY(number != 0);
368 QTRY_COMPARE(number->text(), model.number(1));
375 void tst_QQuickListView::inserted(const QUrl &source)
377 QQuickView *canvas = createView();
381 model.addItem("Fred", "12345");
382 model.addItem("John", "2345");
383 model.addItem("Bob", "54321");
385 QQmlContext *ctxt = canvas->rootContext();
386 ctxt->setContextProperty("testModel", &model);
388 TestObject *testObject = new TestObject;
389 ctxt->setContextProperty("testObject", testObject);
391 canvas->setSource(source);
392 qApp->processEvents();
394 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
395 QTRY_VERIFY(listview != 0);
397 QQuickItem *contentItem = listview->contentItem();
398 QTRY_VERIFY(contentItem != 0);
400 model.insertItem(1, "Will", "9876");
402 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
403 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
405 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
406 QTRY_VERIFY(name != 0);
407 QTRY_COMPARE(name->text(), model.name(1));
408 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
409 QTRY_VERIFY(number != 0);
410 QTRY_COMPARE(number->text(), model.number(1));
412 // Confirm items positioned correctly
413 for (int i = 0; i < model.count(); ++i) {
414 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
415 QTRY_COMPARE(item->y(), i*20.0);
418 model.insertItem(0, "Foo", "1111"); // zero index, and current item
420 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
421 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
423 name = findItem<QQuickText>(contentItem, "textName", 0);
424 QTRY_VERIFY(name != 0);
425 QTRY_COMPARE(name->text(), model.name(0));
426 number = findItem<QQuickText>(contentItem, "textNumber", 0);
427 QTRY_VERIFY(number != 0);
428 QTRY_COMPARE(number->text(), model.number(0));
430 QTRY_COMPARE(listview->currentIndex(), 1);
432 // Confirm items positioned correctly
433 for (int i = 0; i < model.count(); ++i) {
434 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
435 QTRY_COMPARE(item->y(), i*20.0);
438 for (int i = model.count(); i < 30; ++i)
439 model.insertItem(i, "Hello", QString::number(i));
441 listview->setContentY(80);
443 // Insert item outside visible area
444 model.insertItem(1, "Hello", "1324");
446 QTRY_VERIFY(listview->contentY() == 80);
448 // Confirm items positioned correctly
449 for (int i = 5; i < 5+15; ++i) {
450 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
451 if (!item) qWarning() << "Item" << i << "not found";
453 QTRY_COMPARE(item->y(), i*20.0 - 20.0);
456 // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
460 model.insertItem(0, "Hello", "1234");
461 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
463 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
465 QCOMPARE(item->y(), 0.);
466 QTRY_VERIFY(listview->contentY() == 0);
473 void tst_QQuickListView::inserted_more()
475 QFETCH(qreal, contentY);
476 QFETCH(int, insertIndex);
477 QFETCH(int, insertCount);
478 QFETCH(qreal, itemsOffsetAfterMove);
481 for (int i = 0; i < 30; i++)
482 model.addItem("Item" + QString::number(i), "");
484 QQuickView *canvas = createView();
485 QQmlContext *ctxt = canvas->rootContext();
486 ctxt->setContextProperty("testModel", &model);
488 TestObject *testObject = new TestObject;
489 ctxt->setContextProperty("testObject", testObject);
491 canvas->setSource(testFileUrl("listviewtest.qml"));
493 qApp->processEvents();
495 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
496 QTRY_VERIFY(listview != 0);
497 QQuickItem *contentItem = listview->contentItem();
498 QTRY_VERIFY(contentItem != 0);
500 listview->setContentY(contentY);
501 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
503 QList<QPair<QString, QString> > newData;
504 for (int i=0; i<insertCount; i++)
505 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
506 model.insertItems(insertIndex, newData);
507 QTRY_COMPARE(listview->property("count").toInt(), model.count());
509 // check visibleItems.first() is in correct position
510 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
512 QCOMPARE(item0->y(), itemsOffsetAfterMove);
514 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
515 int firstVisibleIndex = -1;
516 for (int i=0; i<items.count(); i++) {
517 if (items[i]->y() >= contentY) {
518 QQmlExpression e(qmlContext(items[i]), items[i], "index");
519 firstVisibleIndex = e.evaluate().toInt();
523 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
525 // Confirm items positioned correctly and indexes correct
526 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
529 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
530 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
531 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
532 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
533 name = findItem<QQuickText>(contentItem, "textName", i);
535 QTRY_COMPARE(name->text(), model.name(i));
536 number = findItem<QQuickText>(contentItem, "textNumber", i);
537 QVERIFY(number != 0);
538 QTRY_COMPARE(number->text(), model.number(i));
545 void tst_QQuickListView::inserted_more_data()
547 QTest::addColumn<qreal>("contentY");
548 QTest::addColumn<int>("insertIndex");
549 QTest::addColumn<int>("insertCount");
550 QTest::addColumn<qreal>("itemsOffsetAfterMove");
552 QTest::newRow("add 1, before visible items")
555 << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move
557 QTest::newRow("add multiple, before visible")
560 << -20.0 * 3; // again first visible should not move
562 QTest::newRow("add 1, at start of visible, content at start")
567 QTest::newRow("add multiple, start of visible, content at start")
572 QTest::newRow("add 1, at start of visible, content not at start")
577 QTest::newRow("add multiple, at start of visible, content not at start")
583 QTest::newRow("add 1, at end of visible, content at start")
588 QTest::newRow("add 1, at end of visible, content at start")
593 QTest::newRow("add 1, at end of visible, content not at start")
598 QTest::newRow("add multiple, at end of visible, content not at start")
604 QTest::newRow("add 1, after visible, content at start")
609 QTest::newRow("add 1, after visible, content at start")
614 QTest::newRow("add 1, after visible, content not at start")
619 QTest::newRow("add multiple, after visible, content not at start")
625 void tst_QQuickListView::insertBeforeVisible()
627 QFETCH(int, insertIndex);
628 QFETCH(int, insertCount);
629 QFETCH(int, cacheBuffer);
632 QQuickView *canvas = createView();
635 for (int i = 0; i < 30; i++)
636 model.addItem("Item" + QString::number(i), "");
638 QQmlContext *ctxt = canvas->rootContext();
639 ctxt->setContextProperty("testModel", &model);
641 TestObject *testObject = new TestObject;
642 ctxt->setContextProperty("testObject", testObject);
644 canvas->setSource(testFileUrl("listviewtest.qml"));
646 qApp->processEvents();
648 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
649 QTRY_VERIFY(listview != 0);
650 QQuickItem *contentItem = listview->contentItem();
651 QTRY_VERIFY(contentItem != 0);
653 listview->setCacheBuffer(cacheBuffer);
654 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
656 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
657 int firstVisibleIndex = 20; // move to an index where the top item is not visible
658 listview->setContentY(firstVisibleIndex * 20.0);
659 listview->setCurrentIndex(firstVisibleIndex);
661 qApp->processEvents();
662 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
663 QTRY_COMPARE(listview->currentIndex(), firstVisibleIndex);
664 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
666 QCOMPARE(item->y(), listview->contentY());
668 QList<QPair<QString, QString> > newData;
669 for (int i=0; i<insertCount; i++)
670 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
671 model.insertItems(insertIndex, newData);
672 QTRY_COMPARE(listview->property("count").toInt(), model.count());
674 // now, moving to the top of the view should position the inserted items correctly
675 int itemsOffsetAfterMove = -(insertCount * 20);
676 listview->setCurrentIndex(0);
677 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
678 QTRY_COMPARE(listview->currentIndex(), 0);
679 QTRY_COMPARE(listview->contentY(), 0.0 + itemsOffsetAfterMove);
681 // Confirm items positioned correctly and indexes correct
682 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
683 for (int i = 0; i < model.count() && i < itemCount; ++i) {
684 item = findItem<QQuickItem>(contentItem, "wrapper", i);
685 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
686 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
687 name = findItem<QQuickText>(contentItem, "textName", i);
689 QTRY_COMPARE(name->text(), model.name(i));
696 void tst_QQuickListView::insertBeforeVisible_data()
698 QTest::addColumn<int>("insertIndex");
699 QTest::addColumn<int>("insertCount");
700 QTest::addColumn<int>("cacheBuffer");
702 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
703 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
704 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
706 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
707 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
708 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
710 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 3 << 0;
711 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 3 << 100;
712 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 3 << 500;
714 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 3 << 0;
715 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 3 << 100;
716 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 3 << 500;
720 void tst_QQuickListView::removed(const QUrl &source, bool /* animated */)
722 QQuickView *canvas = createView();
725 for (int i = 0; i < 50; i++)
726 model.addItem("Item" + QString::number(i), "");
728 QQmlContext *ctxt = canvas->rootContext();
729 ctxt->setContextProperty("testModel", &model);
731 TestObject *testObject = new TestObject;
732 ctxt->setContextProperty("testObject", testObject);
734 canvas->setSource(source);
736 qApp->processEvents();
738 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
739 QTRY_VERIFY(listview != 0);
740 QQuickItem *contentItem = listview->contentItem();
741 QTRY_VERIFY(contentItem != 0);
742 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
745 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
747 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
748 QTRY_VERIFY(name != 0);
749 QTRY_COMPARE(name->text(), model.name(1));
750 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
751 QTRY_VERIFY(number != 0);
752 QTRY_COMPARE(number->text(), model.number(1));
754 // Confirm items positioned correctly
755 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
756 for (int i = 0; i < model.count() && i < itemCount; ++i) {
757 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
758 if (!item) qWarning() << "Item" << i << "not found";
760 QTRY_VERIFY(item->y() == i*20);
763 // Remove first item (which is the current item);
765 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
767 name = findItem<QQuickText>(contentItem, "textName", 0);
768 QTRY_VERIFY(name != 0);
769 QTRY_COMPARE(name->text(), model.name(0));
770 number = findItem<QQuickText>(contentItem, "textNumber", 0);
771 QTRY_VERIFY(number != 0);
772 QTRY_COMPARE(number->text(), model.number(0));
774 // Confirm items positioned correctly
775 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
776 for (int i = 0; i < model.count() && i < itemCount; ++i) {
777 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
778 if (!item) qWarning() << "Item" << i << "not found";
780 QTRY_COMPARE(item->y(),i*20.0);
783 // Remove items not visible
784 model.removeItem(18);
785 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
787 // Confirm items positioned correctly
788 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
789 for (int i = 0; i < model.count() && i < itemCount; ++i) {
790 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
791 if (!item) qWarning() << "Item" << i << "not found";
793 QTRY_COMPARE(item->y(),i*20.0);
796 // Remove items before visible
797 listview->setContentY(80);
798 listview->setCurrentIndex(10);
800 model.removeItem(1); // post: top item will be at 20
801 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
803 // Confirm items positioned correctly
804 for (int i = 2; i < 18; ++i) {
805 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
806 if (!item) qWarning() << "Item" << i << "not found";
808 QTRY_COMPARE(item->y(),20+i*20.0);
811 // Remove current index
812 QTRY_VERIFY(listview->currentIndex() == 9);
813 QQuickItem *oldCurrent = listview->currentItem();
816 QTRY_COMPARE(listview->currentIndex(), 9);
817 QTRY_VERIFY(listview->currentItem() != oldCurrent);
819 listview->setContentY(20); // That's the top now
820 // let transitions settle.
821 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
824 // Confirm items positioned correctly
825 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
826 for (int i = 0; i < model.count() && i < itemCount; ++i) {
827 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
828 if (!item) qWarning() << "Item" << i << "not found";
830 QTRY_COMPARE(item->y(),20+i*20.0);
833 // remove current item beyond visible items.
834 listview->setCurrentIndex(20);
835 listview->setContentY(40);
836 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
838 model.removeItem(20);
839 QTRY_COMPARE(listview->currentIndex(), 20);
840 QTRY_VERIFY(listview->currentItem() != 0);
842 // remove item before current, but visible
843 listview->setCurrentIndex(8);
844 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
845 oldCurrent = listview->currentItem();
848 QTRY_COMPARE(listview->currentIndex(), 7);
849 QTRY_VERIFY(listview->currentItem() == oldCurrent);
851 listview->setContentY(80);
852 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
855 // remove all visible items
856 model.removeItems(1, 18);
857 QTRY_COMPARE(listview->count() , model.count());
859 // Confirm items positioned correctly
860 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
861 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
862 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i+1);
863 if (!item) qWarning() << "Item" << i+1 << "not found";
865 QTRY_COMPARE(item->y(),80+i*20.0);
868 model.removeItems(1, 17);
869 QTRY_COMPARE(listview->count() , model.count());
871 model.removeItems(2, 1);
872 QTRY_COMPARE(listview->count() , model.count());
874 model.addItem("New", "1");
875 QTRY_COMPARE(listview->count() , model.count());
877 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", model.count()-1));
878 QCOMPARE(name->text(), QString("New"));
880 // Add some more items so that we don't run out
882 for (int i = 0; i < 50; i++)
883 model.addItem("Item" + QString::number(i), "");
886 listview->setCurrentIndex(0);
887 listview->setContentY(30);
889 QTRY_VERIFY(name = findItem<QQuickText>(contentItem, "textName", 0));
891 // QTBUG-19198 move to end and remove all visible items one at a time.
892 listview->positionViewAtEnd();
893 for (int i = 0; i < 18; ++i)
894 model.removeItems(model.count() - 1, 1);
895 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() > 16);
902 void tst_QQuickListView::removed_more(const QUrl &source)
904 QFETCH(qreal, contentY);
905 QFETCH(int, removeIndex);
906 QFETCH(int, removeCount);
907 QFETCH(qreal, itemsOffsetAfterMove);
911 QQuickView *canvas = createView();
914 for (int i = 0; i < 30; i++)
915 model.addItem("Item" + QString::number(i), "");
917 QQmlContext *ctxt = canvas->rootContext();
918 ctxt->setContextProperty("testModel", &model);
920 TestObject *testObject = new TestObject;
921 ctxt->setContextProperty("testObject", testObject);
923 canvas->setSource(source);
925 qApp->processEvents();
927 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
928 QTRY_VERIFY(listview != 0);
929 QQuickItem *contentItem = listview->contentItem();
930 QTRY_VERIFY(contentItem != 0);
932 listview->setContentY(contentY);
933 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
935 // wait for refill (after refill, items above the firstVisibleIndex-1 should not be rendered)
936 int firstVisibleIndex = contentY / 20;
937 if (firstVisibleIndex - 2 >= 0)
938 QTRY_VERIFY(!findItem<QQuickText>(contentItem, "textName", firstVisibleIndex - 2));
940 model.removeItems(removeIndex, removeCount);
941 QTRY_COMPARE(listview->property("count").toInt(), model.count());
943 // check visibleItems.first() is in correct position
944 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
946 QCOMPARE(item0->y(), itemsOffsetAfterMove);
948 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
949 for (int i=0; i<items.count(); i++) {
950 if (items[i]->y() >= contentY) {
951 QQmlExpression e(qmlContext(items[i]), items[i], "index");
952 firstVisibleIndex = e.evaluate().toInt();
956 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
958 // Confirm items positioned correctly and indexes correct
959 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
960 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
961 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
962 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
963 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
964 name = findItem<QQuickText>(contentItem, "textName", i);
966 QTRY_COMPARE(name->text(), model.name(i));
967 number = findItem<QQuickText>(contentItem, "textNumber", i);
968 QVERIFY(number != 0);
969 QTRY_COMPARE(number->text(), model.number(i));
976 void tst_QQuickListView::removed_more_data()
978 QTest::addColumn<qreal>("contentY");
979 QTest::addColumn<int>("removeIndex");
980 QTest::addColumn<int>("removeCount");
981 QTest::addColumn<qreal>("itemsOffsetAfterMove");
983 QTest::newRow("remove 1, before visible items")
986 << 20.0; // visible items slide down by 1 item so that first visible does not move
988 QTest::newRow("remove multiple, all before visible items")
993 QTest::newRow("remove multiple, all before visible items, remove item 0")
998 // remove 1,2,3 before the visible pos, 0 moves down to just before the visible pos,
999 // items 4,5 are removed from view, item 6 slides up to original pos of item 4 (80px)
1000 QTest::newRow("remove multiple, mix of items from before and within visible items")
1003 << 20.0 * 3; // adjust for the 3 items removed before the visible
1005 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1008 << 20.0 * 4; // adjust for the 3 items removed before the visible
1011 QTest::newRow("remove 1, from start of visible, content at start")
1016 QTest::newRow("remove multiple, from start of visible, content at start")
1021 QTest::newRow("remove 1, from start of visible, content not at start")
1022 << 80.0 // show 4-19
1026 QTest::newRow("remove multiple, from start of visible, content not at start")
1027 << 80.0 // show 4-19
1032 QTest::newRow("remove 1, from middle of visible, content at start")
1037 QTest::newRow("remove multiple, from middle of visible, content at start")
1042 QTest::newRow("remove 1, from middle of visible, content not at start")
1043 << 80.0 // show 4-19
1047 QTest::newRow("remove multiple, from middle of visible, content not at start")
1048 << 80.0 // show 4-19
1053 QTest::newRow("remove 1, after visible, content at start")
1058 QTest::newRow("remove multiple, after visible, content at start")
1063 QTest::newRow("remove 1, after visible, content not at middle")
1064 << 80.0 // show 4-19
1068 QTest::newRow("remove multiple, after visible, content not at start")
1069 << 80.0 // show 4-19
1073 QTest::newRow("remove multiple, mix of items from within and after visible items")
1080 void tst_QQuickListView::clear(const QUrl &source)
1082 QQuickView *canvas = createView();
1085 for (int i = 0; i < 30; i++)
1086 model.addItem("Item" + QString::number(i), "");
1088 QQmlContext *ctxt = canvas->rootContext();
1089 ctxt->setContextProperty("testModel", &model);
1091 TestObject *testObject = new TestObject;
1092 ctxt->setContextProperty("testObject", testObject);
1094 canvas->setSource(source);
1096 qApp->processEvents();
1098 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1099 QTRY_VERIFY(listview != 0);
1100 QQuickItem *contentItem = listview->contentItem();
1101 QTRY_VERIFY(contentItem != 0);
1102 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1106 QTRY_VERIFY(listview->count() == 0);
1107 QTRY_VERIFY(listview->currentItem() == 0);
1108 QTRY_VERIFY(listview->contentY() == 0);
1109 QVERIFY(listview->currentIndex() == -1);
1111 // confirm sanity when adding an item to cleared list
1112 model.addItem("New", "1");
1113 QTRY_VERIFY(listview->count() == 1);
1114 QVERIFY(listview->currentItem() != 0);
1115 QVERIFY(listview->currentIndex() == 0);
1122 void tst_QQuickListView::moved(const QUrl &source)
1124 QFETCH(qreal, contentY);
1128 QFETCH(qreal, itemsOffsetAfterMove);
1132 QQuickView *canvas = createView();
1135 for (int i = 0; i < 30; i++)
1136 model.addItem("Item" + QString::number(i), "");
1138 QQmlContext *ctxt = canvas->rootContext();
1139 ctxt->setContextProperty("testModel", &model);
1141 TestObject *testObject = new TestObject;
1142 ctxt->setContextProperty("testObject", testObject);
1144 canvas->setSource(source);
1146 qApp->processEvents();
1148 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1149 QTRY_VERIFY(listview != 0);
1150 QQuickItem *contentItem = listview->contentItem();
1151 QTRY_VERIFY(contentItem != 0);
1152 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1154 QQuickItem *currentItem = listview->currentItem();
1155 QTRY_VERIFY(currentItem != 0);
1157 if (contentY != 0) {
1158 listview->setContentY(contentY);
1159 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1162 model.moveItems(from, to, count);
1163 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1165 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1166 int firstVisibleIndex = -1;
1167 for (int i=0; i<items.count(); i++) {
1168 if (items[i]->y() >= contentY) {
1169 QQmlExpression e(qmlContext(items[i]), items[i], "index");
1170 firstVisibleIndex = e.evaluate().toInt();
1174 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1176 // Confirm items positioned correctly and indexes correct
1177 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1178 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
1179 if (i >= firstVisibleIndex + 16) // index has moved out of view
1181 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1182 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1183 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
1184 name = findItem<QQuickText>(contentItem, "textName", i);
1186 QTRY_COMPARE(name->text(), model.name(i));
1187 number = findItem<QQuickText>(contentItem, "textNumber", i);
1188 QVERIFY(number != 0);
1189 QTRY_COMPARE(number->text(), model.number(i));
1191 // current index should have been updated
1192 if (item == currentItem)
1193 QTRY_COMPARE(listview->currentIndex(), i);
1200 void tst_QQuickListView::moved_data()
1202 QTest::addColumn<qreal>("contentY");
1203 QTest::addColumn<int>("from");
1204 QTest::addColumn<int>("to");
1205 QTest::addColumn<int>("count");
1206 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1208 // model starts with 30 items, each 20px high, in area 320px high
1209 // 16 items should be visible at a time
1210 // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved
1212 QTest::newRow("move 1 forwards, within visible items")
1217 QTest::newRow("move 1 forwards, from non-visible -> visible")
1218 << 80.0 // show 4-19
1220 << 20.0; // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement
1222 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1223 << 80.0 // show 4-19
1225 << 20.0; // first item has moved to below item4, everything drops down by size of 1 item
1227 QTest::newRow("move 1 forwards, from visible -> non-visible")
1232 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1238 QTest::newRow("move 1 backwards, within visible items")
1243 QTest::newRow("move 1 backwards, within visible items (to first index)")
1248 QTest::newRow("move 1 backwards, from non-visible -> visible")
1253 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1258 QTest::newRow("move 1 backwards, from visible -> non-visible")
1259 << 80.0 // show 4-19
1261 << -20.0; // to minimize movement, item 0 moves to -20, and other items do not move
1263 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1264 << 80.0 // show 4-19
1266 << -20.0; // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move
1269 QTest::newRow("move multiple forwards, within visible items")
1274 QTest::newRow("move multiple forwards, before visible items")
1275 << 140.0 // show 7-22
1276 << 4 << 5 << 3 // 4,5,6 move to below 7
1277 << 20.0 * 3; // 4,5,6 moved down
1279 QTest::newRow("move multiple forwards, from non-visible -> visible")
1280 << 80.0 // show 4-19
1282 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1284 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1285 << 80.0 // show 4-19
1287 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
1289 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1292 << 20.0; // item 1,2 are removed, item 3 is now first visible
1294 QTest::newRow("move multiple forwards, to bottom of view")
1299 QTest::newRow("move multiple forwards, to bottom of view, first->last")
1304 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1309 QTest::newRow("move multiple forwards, from visible -> non-visible")
1314 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1320 QTest::newRow("move multiple backwards, within visible items")
1325 QTest::newRow("move multiple backwards, within visible items (move first item)")
1330 QTest::newRow("move multiple backwards, from non-visible -> visible")
1335 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1340 QTest::newRow("move multiple backwards, from visible -> non-visible")
1341 << 80.0 // show 4-19
1343 << -20.0 * 3; // to minimize movement, 0 moves by -60, and other items do not move
1345 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1346 << 80.0 // show 4-19
1348 << -20.0 * 3; // to minimize movement, 16,17,18 move to above item 0, and other items do not move
1351 void tst_QQuickListView::multipleChanges()
1353 QFETCH(int, startCount);
1354 QFETCH(QList<ListChange>, changes);
1355 QFETCH(int, newCount);
1356 QFETCH(int, newCurrentIndex);
1358 QQuickView *canvas = createView();
1361 for (int i = 0; i < startCount; i++)
1362 model.addItem("Item" + QString::number(i), "");
1364 QQmlContext *ctxt = canvas->rootContext();
1365 ctxt->setContextProperty("testModel", &model);
1367 TestObject *testObject = new TestObject;
1368 ctxt->setContextProperty("testObject", testObject);
1370 canvas->setSource(testFileUrl("listviewtest.qml"));
1372 qApp->processEvents();
1374 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1375 QTRY_VERIFY(listview != 0);
1376 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1378 for (int i=0; i<changes.count(); i++) {
1379 switch (changes[i].type) {
1380 case ListChange::Inserted:
1382 QList<QPair<QString, QString> > items;
1383 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1384 items << qMakePair(QString("new item %1").arg(j), QString::number(j));
1385 model.insertItems(changes[i].index, items);
1386 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1389 case ListChange::Removed:
1390 model.removeItems(changes[i].index, changes[i].count);
1391 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1393 case ListChange::Moved:
1394 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1395 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1397 case ListChange::SetCurrent:
1398 listview->setCurrentIndex(changes[i].index);
1399 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1401 case ListChange::SetContentY:
1402 listview->setContentY(changes[i].pos);
1403 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1408 QTRY_COMPARE(listview->count(), newCount);
1409 QCOMPARE(listview->count(), model.count());
1410 QTRY_COMPARE(listview->currentIndex(), newCurrentIndex);
1414 QQuickItem *contentItem = listview->contentItem();
1415 QTRY_VERIFY(contentItem != 0);
1416 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1417 for (int i=0; i < model.count() && i < itemCount; ++i) {
1418 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1419 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1420 name = findItem<QQuickText>(contentItem, "textName", i);
1422 QTRY_COMPARE(name->text(), model.name(i));
1423 number = findItem<QQuickText>(contentItem, "textNumber", i);
1424 QVERIFY(number != 0);
1425 QTRY_COMPARE(number->text(), model.number(i));
1432 void tst_QQuickListView::multipleChanges_data()
1434 QTest::addColumn<int>("startCount");
1435 QTest::addColumn<QList<ListChange> >("changes");
1436 QTest::addColumn<int>("newCount");
1437 QTest::addColumn<int>("newCurrentIndex");
1439 QList<ListChange> changes;
1441 for (int i=1; i<30; i++)
1442 changes << ListChange::remove(0);
1443 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1445 changes << ListChange::remove(0);
1446 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1449 changes << ListChange::setCurrent(29);
1450 for (int i=29; i>0; i--)
1451 changes << ListChange::remove(i);
1452 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1454 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1455 << ListChange::remove(0, 1)
1456 << ListChange::insert(0, 1)
1459 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1460 << ListChange::setCurrent(2)
1461 << ListChange::remove(2, 1)
1462 << ListChange::insert(2, 1)
1465 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1466 << ListChange::setCurrent(1)
1467 << ListChange::remove(1, 3)
1468 << ListChange::insert(2, 2)
1471 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1472 << ListChange::setCurrent(2)
1473 << ListChange::remove(1, 3)
1474 << ListChange::move(1, 5, 1)
1477 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1478 << ListChange::setCurrent(5)
1479 << ListChange::remove(4, 3)
1480 << ListChange::move(4, 1, 1)
1484 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1485 << ListChange::insert(0, 2)
1486 << ListChange::insert(0, 4)
1487 << ListChange::insert(0, 6)
1490 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1491 << ListChange::insert(0, 2)
1492 << ListChange::insert(0, 4)
1493 << ListChange::insert(0, 6)
1494 << ListChange::setCurrent(3)
1495 << ListChange::insert(3, 2)
1498 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1499 << ListChange::insert(0, 30)
1500 << ListChange::remove(0, 30)
1503 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1504 << ListChange::insert(1)
1505 << ListChange::setCurrent(1)
1506 << ListChange::remove(1)
1509 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1510 << ListChange::insert(0, 10)
1511 << ListChange::remove(5, 10)
1514 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1515 << ListChange::insert(0, 3)
1516 << ListChange::move(0, 10, 3)
1519 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1520 << ListChange::insert(0, 3)
1521 << ListChange::move(0, 8, 5)
1524 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1525 << ListChange::setCurrent(9)
1526 << ListChange::insert(10, 3)
1527 << ListChange::move(8, 0, 5)
1531 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1532 << ListChange::setCurrent(1)
1533 << ListChange::move(1, 2, 2)
1534 << ListChange::move(2, 1, 2)
1537 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1538 << ListChange::setCurrent(2)
1539 << ListChange::move(1, 2, 3)
1540 << ListChange::move(3, 0, 5)
1543 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1544 << ListChange::setCurrent(5)
1545 << ListChange::move(5, 0, 1)
1546 << ListChange::remove(0)
1549 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1550 << ListChange::setCurrent(5)
1551 << ListChange::move(5, 0, 1)
1552 << ListChange::insert(0)
1555 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1556 << ListChange::setCurrent(1)
1557 << ListChange::move(5, 1, 3)
1558 << ListChange::remove(1, 3)
1561 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1562 << ListChange::setCurrent(5)
1563 << ListChange::move(5, 1, 3)
1564 << ListChange::insert(1, 5)
1567 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1568 << ListChange::setCurrent(3)
1569 << ListChange::move(0, 1, 2)
1570 << ListChange::insert(3, 5)
1574 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1575 << ListChange::insert(0, 5)
1576 << ListChange::setCurrent(-1)
1577 << ListChange::remove(0, 5)
1578 << ListChange::insert(0, 5)
1582 void tst_QQuickListView::swapWithFirstItem()
1584 QQuickView *canvas = createView();
1587 for (int i = 0; i < 30; i++)
1588 model.addItem("Item" + QString::number(i), "");
1590 QQmlContext *ctxt = canvas->rootContext();
1591 ctxt->setContextProperty("testModel", &model);
1593 TestObject *testObject = new TestObject;
1594 ctxt->setContextProperty("testObject", testObject);
1596 canvas->setSource(testFileUrl("listviewtest.qml"));
1598 qApp->processEvents();
1600 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1601 QTRY_VERIFY(listview != 0);
1602 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1604 // ensure content position is stable
1605 listview->setContentY(0);
1606 model.moveItem(1, 0);
1607 QTRY_VERIFY(listview->contentY() == 0);
1613 void tst_QQuickListView::enforceRange()
1615 QQuickView *canvas = createView();
1618 for (int i = 0; i < 30; i++)
1619 model.addItem("Item" + QString::number(i), "");
1621 QQmlContext *ctxt = canvas->rootContext();
1622 ctxt->setContextProperty("testModel", &model);
1624 canvas->setSource(testFileUrl("listview-enforcerange.qml"));
1626 qApp->processEvents();
1628 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1629 QTRY_VERIFY(listview != 0);
1631 QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0);
1632 QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0);
1633 QTRY_COMPARE(listview->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
1634 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1636 QQuickItem *contentItem = listview->contentItem();
1637 QTRY_VERIFY(contentItem != 0);
1639 // view should be positioned at the top of the range.
1640 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
1642 QTRY_COMPARE(listview->contentY(), -100.0);
1644 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1645 QTRY_VERIFY(name != 0);
1646 QTRY_COMPARE(name->text(), model.name(0));
1647 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
1648 QTRY_VERIFY(number != 0);
1649 QTRY_COMPARE(number->text(), model.number(0));
1651 // Check currentIndex is updated when contentItem moves
1652 listview->setContentY(20);
1654 QTRY_COMPARE(listview->currentIndex(), 6);
1657 QmlListModel model2;
1658 for (int i = 0; i < 5; i++)
1659 model2.addItem("Item" + QString::number(i), "");
1661 ctxt->setContextProperty("testModel", &model2);
1662 QCOMPARE(listview->count(), 5);
1667 void tst_QQuickListView::enforceRange_withoutHighlight()
1670 // If no highlight is set but StrictlyEnforceRange is used, the content should still move
1671 // to the correct position (i.e. to the next/previous item, not next/previous section)
1672 // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex()
1674 QQuickView *canvas = createView();
1677 model.addItem("Item 0", "a");
1678 model.addItem("Item 1", "b");
1679 model.addItem("Item 2", "b");
1680 model.addItem("Item 3", "c");
1682 QQmlContext *ctxt = canvas->rootContext();
1683 ctxt->setContextProperty("testModel", &model);
1685 canvas->setSource(testFileUrl("listview-enforcerange-nohighlight.qml"));
1687 qApp->processEvents();
1689 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1690 QTRY_VERIFY(listview != 0);
1691 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1693 qreal expectedPos = -100.0;
1695 expectedPos += 10.0; // scroll past 1st section's delegate (10px height)
1696 QTRY_COMPARE(listview->contentY(), expectedPos);
1698 expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section
1699 QTest::keyClick(canvas, Qt::Key_Down);
1701 QTRY_COMPARE(listview->contentY(), expectedPos);
1703 expectedPos += 20; // scroll past 1st item of 2nd section
1704 QTest::keyClick(canvas, Qt::Key_Down);
1705 QTRY_COMPARE(listview->contentY(), expectedPos);
1707 expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section
1708 QTest::keyClick(canvas, Qt::Key_Down);
1709 QTRY_COMPARE(listview->contentY(), expectedPos);
1714 void tst_QQuickListView::spacing()
1716 QQuickView *canvas = createView();
1719 for (int i = 0; i < 30; i++)
1720 model.addItem("Item" + QString::number(i), "");
1722 QQmlContext *ctxt = canvas->rootContext();
1723 ctxt->setContextProperty("testModel", &model);
1725 TestObject *testObject = new TestObject;
1726 ctxt->setContextProperty("testObject", testObject);
1728 canvas->setSource(testFileUrl("listviewtest.qml"));
1730 qApp->processEvents();
1732 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1733 QTRY_VERIFY(listview != 0);
1735 QQuickItem *contentItem = listview->contentItem();
1736 QTRY_VERIFY(contentItem != 0);
1737 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1739 // Confirm items positioned correctly
1740 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1741 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1742 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1743 if (!item) qWarning() << "Item" << i << "not found";
1745 QTRY_VERIFY(item->y() == i*20);
1748 listview->setSpacing(10);
1749 QTRY_VERIFY(listview->spacing() == 10);
1751 // Confirm items positioned correctly
1752 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() == 11);
1753 for (int i = 0; i < 11; ++i) {
1754 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1755 if (!item) qWarning() << "Item" << i << "not found";
1757 QTRY_VERIFY(item->y() == i*30);
1760 listview->setSpacing(0);
1762 // Confirm items positioned correctly
1763 QTRY_VERIFY(findItems<QQuickItem>(contentItem, "wrapper").count() >= 16);
1764 for (int i = 0; i < 16; ++i) {
1765 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1766 if (!item) qWarning() << "Item" << i << "not found";
1768 QTRY_COMPARE(item->y(), i*20.0);
1775 template <typename T>
1776 void tst_QQuickListView::sections(const QUrl &source)
1778 QQuickView *canvas = createView();
1781 for (int i = 0; i < 30; i++)
1782 model.addItem("Item" + QString::number(i), QString::number(i/5));
1784 QQmlContext *ctxt = canvas->rootContext();
1785 ctxt->setContextProperty("testModel", &model);
1787 canvas->setSource(source);
1789 qApp->processEvents();
1791 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1792 QTRY_VERIFY(listview != 0);
1794 QQuickItem *contentItem = listview->contentItem();
1795 QTRY_VERIFY(contentItem != 0);
1797 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1799 // Confirm items positioned correctly
1800 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1801 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1802 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1804 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20));
1805 QQuickText *next = findItem<QQuickText>(item, "nextSection");
1806 QCOMPARE(next->text().toInt(), (i+1)/5);
1809 QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1811 // Remove section boundary
1812 model.removeItem(5);
1813 QTRY_COMPARE(listview->count(), model.count());
1815 // New section header created
1816 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1818 QTRY_COMPARE(item->height(), 40.0);
1820 model.insertItem(3, "New Item", "0");
1821 QTRY_COMPARE(listview->count(), model.count());
1823 // Section header moved
1824 item = findItem<QQuickItem>(contentItem, "wrapper", 5);
1826 QTRY_COMPARE(item->height(), 20.0);
1828 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1830 QTRY_COMPARE(item->height(), 40.0);
1832 // insert item which will become a section header
1833 model.insertItem(6, "Replace header", "1");
1834 QTRY_COMPARE(listview->count(), model.count());
1836 item = findItem<QQuickItem>(contentItem, "wrapper", 6);
1838 QTRY_COMPARE(item->height(), 40.0);
1840 item = findItem<QQuickItem>(contentItem, "wrapper", 7);
1842 QTRY_COMPARE(item->height(), 20.0);
1844 QTRY_COMPARE(listview->currentSection(), QString("0"));
1846 listview->setContentY(140);
1847 QTRY_COMPARE(listview->currentSection(), QString("1"));
1849 QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
1851 listview->setContentY(20);
1852 QTRY_COMPARE(listview->currentSection(), QString("0"));
1854 QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
1856 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
1858 QTRY_COMPARE(item->height(), 20.0);
1860 // check that headers change when item changes
1861 listview->setContentY(0);
1862 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1863 model.modifyItem(0, "changed", "2");
1864 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1866 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
1868 QTRY_COMPARE(item->height(), 40.0);
1873 void tst_QQuickListView::sectionsDelegate()
1875 QSKIP("QTBUG-24395");
1877 QQuickView *canvas = createView();
1880 for (int i = 0; i < 30; i++)
1881 model.addItem("Item" + QString::number(i), QString::number(i/5));
1883 QQmlContext *ctxt = canvas->rootContext();
1884 ctxt->setContextProperty("testModel", &model);
1886 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
1888 qApp->processEvents();
1890 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
1891 QTRY_VERIFY(listview != 0);
1893 QQuickItem *contentItem = listview->contentItem();
1894 QTRY_VERIFY(contentItem != 0);
1896 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1898 // Confirm items positioned correctly
1899 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1900 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1901 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1903 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20));
1904 QQuickText *next = findItem<QQuickText>(item, "nextSection");
1905 QCOMPARE(next->text().toInt(), (i+1)/5);
1908 for (int i = 0; i < 3; ++i) {
1909 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
1911 QTRY_COMPARE(item->y(), qreal(i*20*6));
1914 // ensure section header is maintained in view
1915 listview->setCurrentIndex(20);
1916 QTRY_VERIFY(listview->contentY() >= 200.0);
1917 listview->setCurrentIndex(0);
1918 QTRY_COMPARE(listview->contentY(), 0.0);
1921 model.modifyItem(0, "One", "aaa");
1922 model.modifyItem(1, "Two", "aaa");
1923 model.modifyItem(2, "Three", "aaa");
1924 model.modifyItem(3, "Four", "aaa");
1925 model.modifyItem(4, "Five", "aaa");
1926 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1928 for (int i = 0; i < 3; ++i) {
1929 QQuickItem *item = findItem<QQuickItem>(contentItem,
1930 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
1932 QTRY_COMPARE(item->y(), qreal(i*20*6));
1935 // remove section boundary
1936 model.removeItem(5);
1937 QTRY_COMPARE(listview->count(), model.count());
1938 for (int i = 0; i < 3; ++i) {
1939 QQuickItem *item = findItem<QQuickItem>(contentItem,
1940 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
1945 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "sect_1");
1946 QCOMPARE(items.count(), 1);
1949 model.modifyItem(0, "One", "aaa");
1950 model.modifyItem(1, "One", "aaa");
1951 model.modifyItem(2, "One", "aaa");
1952 model.modifyItem(3, "Four", "aaa");
1953 model.modifyItem(4, "Four", "aaa");
1954 model.modifyItem(5, "Four", "aaa");
1955 model.modifyItem(6, "Five", "aaa");
1956 model.modifyItem(7, "Five", "aaa");
1957 model.modifyItem(8, "Five", "aaa");
1958 model.modifyItem(9, "Two", "aaa");
1959 model.modifyItem(10, "Two", "aaa");
1960 model.modifyItem(11, "Two", "aaa");
1961 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
1962 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_aaa").count(), 1);
1963 canvas->rootObject()->setProperty("sectionProperty", "name");
1964 // ensure view has settled.
1965 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "sect_Four").count(), 1);
1966 for (int i = 0; i < 4; ++i) {
1967 QQuickItem *item = findItem<QQuickItem>(contentItem,
1968 "sect_" + model.name(i*3));
1970 QTRY_COMPARE(item->y(), qreal(i*20*4));
1974 model.removeItems(10, 20);
1975 // ensure view has settled.
1976 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 10);
1977 // Drag view up beyond bounds
1978 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20));
1980 QMouseEvent mv(QEvent::MouseMove, QPoint(20,0), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
1981 QGuiApplication::sendEvent(canvas, &mv);
1984 QMouseEvent mv(QEvent::MouseMove, QPoint(20,-50), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
1985 QGuiApplication::sendEvent(canvas, &mv);
1988 QMouseEvent mv(QEvent::MouseMove, QPoint(20,-200), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
1989 QGuiApplication::sendEvent(canvas, &mv);
1991 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-200));
1992 // view should settle back at 0
1993 QTRY_COMPARE(listview->contentY(), 0.0);
1998 void tst_QQuickListView::sectionsPositioning()
2000 QQuickView *canvas = createView();
2003 for (int i = 0; i < 30; i++)
2004 model.addItem("Item" + QString::number(i), QString::number(i/5));
2006 QQmlContext *ctxt = canvas->rootContext();
2007 ctxt->setContextProperty("testModel", &model);
2009 canvas->setSource(testFileUrl("listview-sections_delegate.qml"));
2011 qApp->processEvents();
2012 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd)));
2014 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2015 QTRY_VERIFY(listview != 0);
2016 QQuickItem *contentItem = listview->contentItem();
2017 QTRY_VERIFY(contentItem != 0);
2018 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2020 for (int i = 0; i < 3; ++i) {
2021 QQuickItem *item = findItem<QQuickItem>(contentItem, "sect_" + QString::number(i));
2023 QTRY_COMPARE(item->y(), qreal(i*20*6));
2026 QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header
2028 QCOMPARE(topItem->y(), 0.);
2030 QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2031 QVERIFY(bottomItem);
2032 QCOMPARE(bottomItem->y(), 300.);
2034 // move down a little and check that section header is at top
2035 listview->setContentY(10);
2036 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2037 QCOMPARE(topItem->y(), 0.);
2039 // push the top header up
2040 listview->setContentY(110);
2041 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2042 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2044 QCOMPARE(topItem->y(), 100.);
2046 QQuickItem *item = findVisibleChild(contentItem, "sect_1");
2048 QCOMPARE(item->y(), 120.);
2050 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2051 QVERIFY(bottomItem);
2052 QCOMPARE(bottomItem->y(), 410.);
2054 // Move past section 0
2055 listview->setContentY(120);
2056 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2057 topItem = findVisibleChild(contentItem, "sect_0"); // section header
2060 // Push section footer down
2061 listview->setContentY(70);
2062 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2063 bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer
2064 QVERIFY(bottomItem);
2065 QCOMPARE(bottomItem->y(), 380.);
2067 // Change current section
2068 listview->setContentY(10);
2069 model.modifyItem(0, "One", "aaa");
2070 model.modifyItem(1, "Two", "aaa");
2071 model.modifyItem(2, "Three", "aaa");
2072 model.modifyItem(3, "Four", "aaa");
2073 model.modifyItem(4, "Five", "aaa");
2074 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2076 QTRY_COMPARE(listview->currentSection(), QString("aaa"));
2078 for (int i = 0; i < 3; ++i) {
2079 QQuickItem *item = findItem<QQuickItem>(contentItem,
2080 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2082 QTRY_COMPARE(item->y(), qreal(i*20*6));
2085 QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header
2086 QCOMPARE(topItem->y(), 10.);
2088 // remove section boundary
2089 listview->setContentY(120);
2090 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2091 model.removeItem(5);
2092 QTRY_COMPARE(listview->count(), model.count());
2093 for (int i = 0; i < 3; ++i) {
2094 QQuickItem *item = findItem<QQuickItem>(contentItem,
2095 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
2097 QTRY_COMPARE(item->y(), qreal(i*20*6));
2100 QVERIFY(topItem = findVisibleChild(contentItem, "sect_1"));
2101 QTRY_COMPARE(topItem->y(), 120.);
2103 // Change the next section
2104 listview->setContentY(0);
2105 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2106 bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer
2107 QVERIFY(bottomItem);
2108 QTRY_COMPARE(bottomItem->y(), 300.);
2110 model.modifyItem(14, "New", "new");
2111 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2113 QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer
2114 QTRY_COMPARE(bottomItem->y(), 300.);
2116 // Turn sticky footer off
2117 listview->setContentY(20);
2118 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2119 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart)));
2120 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored
2121 QCOMPARE(item->y(), 340.);
2123 // Turn sticky header off
2124 listview->setContentY(30);
2125 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2126 canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels)));
2127 QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored
2128 QCOMPARE(item->y(), 0.);
2133 void tst_QQuickListView::currentIndex_delayedItemCreation()
2135 QFETCH(bool, setCurrentToZero);
2137 QQuickView *canvas = createView();
2139 // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
2140 // (since the currentItem will have changed and that shares the same index)
2141 canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
2143 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
2144 qApp->processEvents();
2146 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2147 QTRY_VERIFY(listview != 0);
2148 QQuickItem *contentItem = listview->contentItem();
2149 QTRY_VERIFY(contentItem != 0);
2151 QSignalSpy spy(listview, SIGNAL(currentItemChanged()));
2152 QCOMPARE(listview->currentIndex(), 0);
2153 QTRY_COMPARE(spy.count(), 1);
2158 void tst_QQuickListView::currentIndex_delayedItemCreation_data()
2160 QTest::addColumn<bool>("setCurrentToZero");
2162 QTest::newRow("set to 0") << true;
2163 QTest::newRow("don't set to 0") << false;
2166 void tst_QQuickListView::currentIndex()
2169 for (int i = 0; i < 30; i++)
2170 model.addItem("Item" + QString::number(i), QString::number(i));
2172 QQuickView *canvas = new QQuickView(0);
2173 canvas->setGeometry(0,0,240,320);
2175 QQmlContext *ctxt = canvas->rootContext();
2176 ctxt->setContextProperty("testModel", &model);
2177 ctxt->setContextProperty("testWrap", QVariant(false));
2179 QString filename(testFile("listview-initCurrent.qml"));
2180 canvas->setSource(QUrl::fromLocalFile(filename));
2182 qApp->processEvents();
2184 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2185 QTRY_VERIFY(listview != 0);
2186 QQuickItem *contentItem = listview->contentItem();
2187 QTRY_VERIFY(contentItem != 0);
2188 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2190 // current item should be 20th item at startup
2191 // and current item should be in view
2192 QCOMPARE(listview->currentIndex(), 20);
2193 QCOMPARE(listview->contentY(), 100.0);
2194 QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
2195 QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
2198 listview->setCurrentIndex(0);
2199 QCOMPARE(listview->currentIndex(), 0);
2200 // confirm that the velocity is updated
2201 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
2203 listview->incrementCurrentIndex();
2204 QCOMPARE(listview->currentIndex(), 1);
2205 listview->decrementCurrentIndex();
2206 QCOMPARE(listview->currentIndex(), 0);
2208 listview->decrementCurrentIndex();
2209 QCOMPARE(listview->currentIndex(), 0);
2212 ctxt->setContextProperty("testWrap", QVariant(true));
2213 QVERIFY(listview->isWrapEnabled());
2215 listview->decrementCurrentIndex();
2216 QCOMPARE(listview->currentIndex(), model.count()-1);
2218 QTRY_COMPARE(listview->contentY(), 280.0);
2220 listview->incrementCurrentIndex();
2221 QCOMPARE(listview->currentIndex(), 0);
2223 QTRY_COMPARE(listview->contentY(), 0.0);
2226 // footer should become visible if it is out of view, and then current index is set to count-1
2227 canvas->rootObject()->setProperty("showFooter", true);
2228 QTRY_VERIFY(listview->footerItem());
2229 listview->setCurrentIndex(model.count()-2);
2230 QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height());
2231 listview->setCurrentIndex(model.count()-1);
2232 QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height());
2233 canvas->rootObject()->setProperty("showFooter", false);
2235 // header should become visible if it is out of view, and then current index is set to 0
2236 canvas->rootObject()->setProperty("showHeader", true);
2237 QTRY_VERIFY(listview->headerItem());
2238 listview->setCurrentIndex(1);
2239 QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY());
2240 listview->setCurrentIndex(0);
2241 QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height());
2242 canvas->rootObject()->setProperty("showHeader", false);
2247 canvas->requestActivateWindow();
2248 QTest::qWaitForWindowShown(canvas);
2249 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
2251 listview->setCurrentIndex(0);
2253 QTest::keyClick(canvas, Qt::Key_Down);
2254 QCOMPARE(listview->currentIndex(), 1);
2256 QTest::keyClick(canvas, Qt::Key_Up);
2257 QCOMPARE(listview->currentIndex(), 0);
2259 // hold down Key_Down
2260 for (int i=0; i<model.count()-1; i++) {
2261 QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
2262 QTRY_COMPARE(listview->currentIndex(), i+1);
2264 QTest::keyRelease(canvas, Qt::Key_Down);
2265 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
2266 QTRY_COMPARE(listview->contentY(), 280.0);
2269 for (int i=model.count()-1; i > 0; i--) {
2270 QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
2271 QTRY_COMPARE(listview->currentIndex(), i-1);
2273 QTest::keyRelease(canvas, Qt::Key_Up);
2274 QTRY_COMPARE(listview->currentIndex(), 0);
2275 QTRY_COMPARE(listview->contentY(), 0.0);
2278 // turn off auto highlight
2279 listview->setHighlightFollowsCurrentItem(false);
2280 QVERIFY(listview->highlightFollowsCurrentItem() == false);
2282 QVERIFY(listview->highlightItem());
2283 qreal hlPos = listview->highlightItem()->y();
2285 listview->setCurrentIndex(4);
2286 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
2288 // insert item before currentIndex
2289 listview->setCurrentIndex(28);
2290 model.insertItem(0, "Foo", "1111");
2291 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
2293 // check removing highlight by setting currentIndex to -1;
2294 listview->setCurrentIndex(-1);
2296 QCOMPARE(listview->currentIndex(), -1);
2297 QVERIFY(!listview->highlightItem());
2298 QVERIFY(!listview->currentItem());
2303 void tst_QQuickListView::noCurrentIndex()
2306 for (int i = 0; i < 30; i++)
2307 model.addItem("Item" + QString::number(i), QString::number(i));
2309 QQuickView *canvas = new QQuickView(0);
2310 canvas->setGeometry(0,0,240,320);
2312 QQmlContext *ctxt = canvas->rootContext();
2313 ctxt->setContextProperty("testModel", &model);
2315 QString filename(testFile("listview-noCurrent.qml"));
2316 canvas->setSource(QUrl::fromLocalFile(filename));
2318 qApp->processEvents();
2320 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2321 QTRY_VERIFY(listview != 0);
2322 QQuickItem *contentItem = listview->contentItem();
2323 QTRY_VERIFY(contentItem != 0);
2324 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2326 // current index should be -1 at startup
2327 // and we should not have a currentItem or highlightItem
2328 QCOMPARE(listview->currentIndex(), -1);
2329 QCOMPARE(listview->contentY(), 0.0);
2330 QVERIFY(!listview->highlightItem());
2331 QVERIFY(!listview->currentItem());
2333 listview->setCurrentIndex(2);
2334 QCOMPARE(listview->currentIndex(), 2);
2335 QVERIFY(listview->highlightItem());
2336 QVERIFY(listview->currentItem());
2341 void tst_QQuickListView::itemList()
2343 QQuickView *canvas = createView();
2344 canvas->setSource(testFileUrl("itemlist.qml"));
2346 qApp->processEvents();
2348 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
2349 QTRY_VERIFY(listview != 0);
2351 QQuickItem *contentItem = listview->contentItem();
2352 QTRY_VERIFY(contentItem != 0);
2354 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
2355 QTRY_VERIFY(model != 0);
2357 QTRY_VERIFY(model->count() == 3);
2358 QTRY_COMPARE(listview->currentIndex(), 0);
2360 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
2362 QTRY_COMPARE(item->x(), 0.0);
2363 QCOMPARE(item->height(), listview->height());
2365 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
2367 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2369 listview->setCurrentIndex(2);
2371 item = findItem<QQuickItem>(contentItem, "item3");
2373 QTRY_COMPARE(item->x(), 480.0);
2375 text = findItem<QQuickText>(contentItem, "text3");
2377 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
2382 void tst_QQuickListView::cacheBuffer()
2384 QQuickView *canvas = createView();
2387 for (int i = 0; i < 90; i++)
2388 model.addItem("Item" + QString::number(i), "");
2390 QQmlContext *ctxt = canvas->rootContext();
2391 ctxt->setContextProperty("testModel", &model);
2393 TestObject *testObject = new TestObject;
2394 ctxt->setContextProperty("testObject", testObject);
2396 canvas->setSource(testFileUrl("listviewtest.qml"));
2398 qApp->processEvents();
2400 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2401 QTRY_VERIFY(listview != 0);
2403 QQuickItem *contentItem = listview->contentItem();
2404 QTRY_VERIFY(contentItem != 0);
2405 QTRY_VERIFY(listview->delegate() != 0);
2406 QTRY_VERIFY(listview->model() != 0);
2407 QTRY_VERIFY(listview->highlight() != 0);
2409 // Confirm items positioned correctly
2410 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2411 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2412 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2413 if (!item) qWarning() << "Item" << i << "not found";
2415 QTRY_VERIFY(item->y() == i*20);
2418 QQmlIncubationController controller;
2419 canvas->engine()->setIncubationController(&controller);
2421 testObject->setCacheBuffer(200);
2422 QTRY_VERIFY(listview->cacheBuffer() == 200);
2424 // items will be created one at a time
2425 for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) {
2426 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
2427 QQuickItem *item = 0;
2430 controller.incubateWhile(&b);
2431 item = findItem<QQuickItem>(listview, "wrapper", i);
2437 controller.incubateWhile(&b);
2440 int newItemCount = 0;
2441 newItemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2443 // Confirm items positioned correctly
2444 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
2445 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2446 if (!item) qWarning() << "Item" << i << "not found";
2448 QTRY_VERIFY(item->y() == i*20);
2451 // move view and confirm items in view are visible immediately and outside are created async
2452 listview->setContentY(300);
2454 for (int i = 15; i < 32; ++i) {
2455 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2456 if (!item) qWarning() << "Item" << i << "not found";
2458 QVERIFY(item->y() == i*20);
2461 QVERIFY(findItem<QQuickItem>(listview, "wrapper", 32) == 0);
2463 // ensure buffered items are created
2464 for (int i = 32; i < qMin(41,model.count()); ++i) {
2465 QQuickItem *item = 0;
2467 qGuiApp->processEvents(); // allow refill to happen
2469 controller.incubateWhile(&b);
2470 item = findItem<QQuickItem>(listview, "wrapper", i);
2476 controller.incubateWhile(&b);
2483 void tst_QQuickListView::positionViewAtIndex()
2485 QQuickView *canvas = createView();
2488 for (int i = 0; i < 40; i++)
2489 model.addItem("Item" + QString::number(i), "");
2491 QQmlContext *ctxt = canvas->rootContext();
2492 ctxt->setContextProperty("testModel", &model);
2494 TestObject *testObject = new TestObject;
2495 ctxt->setContextProperty("testObject", testObject);
2497 canvas->setSource(testFileUrl("listviewtest.qml"));
2498 qApp->processEvents();
2500 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2501 QTRY_VERIFY(listview != 0);
2502 QQuickItem *contentItem = listview->contentItem();
2503 QTRY_VERIFY(contentItem != 0);
2504 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2506 // Confirm items positioned correctly
2507 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2508 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2509 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2510 if (!item) qWarning() << "Item" << i << "not found";
2512 QTRY_COMPARE(item->y(), i*20.);
2515 // Position on a currently visible item
2516 listview->positionViewAtIndex(3, QQuickListView::Beginning);
2517 QTRY_COMPARE(listview->contentY(), 60.);
2519 // Confirm items positioned correctly
2520 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2521 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2522 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2523 if (!item) qWarning() << "Item" << i << "not found";
2525 QTRY_COMPARE(item->y(), i*20.);
2528 // Position on an item beyond the visible items
2529 listview->positionViewAtIndex(22, QQuickListView::Beginning);
2530 QTRY_COMPARE(listview->contentY(), 440.);
2532 // Confirm items positioned correctly
2533 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2534 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2535 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2536 if (!item) qWarning() << "Item" << i << "not found";
2538 QTRY_COMPARE(item->y(), i*20.);
2541 // Position on an item that would leave empty space if positioned at the top
2542 listview->positionViewAtIndex(28, QQuickListView::Beginning);
2543 QTRY_COMPARE(listview->contentY(), 480.);
2545 // Confirm items positioned correctly
2546 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2547 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2548 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2549 if (!item) qWarning() << "Item" << i << "not found";
2551 QTRY_COMPARE(item->y(), i*20.);
2554 // Position at the beginning again
2555 listview->positionViewAtIndex(0, QQuickListView::Beginning);
2556 QTRY_COMPARE(listview->contentY(), 0.);
2558 // Confirm items positioned correctly
2559 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2560 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2561 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2562 if (!item) qWarning() << "Item" << i << "not found";
2564 QTRY_COMPARE(item->y(), i*20.);
2567 // Position at End using last index
2568 listview->positionViewAtIndex(model.count()-1, QQuickListView::End);
2569 QTRY_COMPARE(listview->contentY(), 480.);
2571 // Confirm items positioned correctly
2572 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2573 for (int i = 24; i < model.count(); ++i) {
2574 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2575 if (!item) qWarning() << "Item" << i << "not found";
2577 QTRY_COMPARE(item->y(), i*20.);
2581 listview->positionViewAtIndex(20, QQuickListView::End);
2582 QTRY_COMPARE(listview->contentY(), 100.);
2584 // Position in Center
2585 listview->positionViewAtIndex(15, QQuickListView::Center);
2586 QTRY_COMPARE(listview->contentY(), 150.);
2588 // Ensure at least partially visible
2589 listview->positionViewAtIndex(15, QQuickListView::Visible);
2590 QTRY_COMPARE(listview->contentY(), 150.);
2592 listview->setContentY(302);
2593 listview->positionViewAtIndex(15, QQuickListView::Visible);
2594 QTRY_COMPARE(listview->contentY(), 302.);
2596 listview->setContentY(320);
2597 listview->positionViewAtIndex(15, QQuickListView::Visible);
2598 QTRY_COMPARE(listview->contentY(), 300.);
2600 listview->setContentY(85);
2601 listview->positionViewAtIndex(20, QQuickListView::Visible);
2602 QTRY_COMPARE(listview->contentY(), 85.);
2604 listview->setContentY(75);
2605 listview->positionViewAtIndex(20, QQuickListView::Visible);
2606 QTRY_COMPARE(listview->contentY(), 100.);
2608 // Ensure completely visible
2609 listview->setContentY(120);
2610 listview->positionViewAtIndex(20, QQuickListView::Contain);
2611 QTRY_COMPARE(listview->contentY(), 120.);
2613 listview->setContentY(302);
2614 listview->positionViewAtIndex(15, QQuickListView::Contain);
2615 QTRY_COMPARE(listview->contentY(), 300.);
2617 listview->setContentY(85);
2618 listview->positionViewAtIndex(20, QQuickListView::Contain);
2619 QTRY_COMPARE(listview->contentY(), 100.);
2621 // positionAtBeginnging
2622 listview->positionViewAtBeginning();
2623 QTRY_COMPARE(listview->contentY(), 0.);
2625 listview->setContentY(80);
2626 canvas->rootObject()->setProperty("showHeader", true);
2627 listview->positionViewAtBeginning();
2628 QTRY_COMPARE(listview->contentY(), -30.);
2631 listview->positionViewAtEnd();
2632 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
2634 listview->setContentY(80);
2635 canvas->rootObject()->setProperty("showFooter", true);
2636 listview->positionViewAtEnd();
2637 QTRY_COMPARE(listview->contentY(), 510.);
2639 // set current item to outside visible view, position at beginning
2640 // and ensure highlight moves to current item
2641 listview->setCurrentIndex(1);
2642 listview->positionViewAtBeginning();
2643 QTRY_COMPARE(listview->contentY(), -30.);
2644 QVERIFY(listview->highlightItem());
2645 QCOMPARE(listview->highlightItem()->y(), 20.);
2651 void tst_QQuickListView::resetModel()
2653 QQuickView *canvas = createView();
2655 QStringList strings;
2656 strings << "one" << "two" << "three";
2657 QStringListModel model(strings);
2659 QQmlContext *ctxt = canvas->rootContext();
2660 ctxt->setContextProperty("testModel", &model);
2662 canvas->setSource(testFileUrl("displaylist.qml"));
2664 qApp->processEvents();
2666 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2667 QTRY_VERIFY(listview != 0);
2668 QQuickItem *contentItem = listview->contentItem();
2669 QTRY_VERIFY(contentItem != 0);
2670 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2672 QTRY_COMPARE(listview->count(), model.rowCount());
2674 for (int i = 0; i < model.rowCount(); ++i) {
2675 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2676 QTRY_VERIFY(display != 0);
2677 QTRY_COMPARE(display->text(), strings.at(i));
2681 strings << "four" << "five" << "six" << "seven";
2682 model.setStringList(strings);
2684 QTRY_COMPARE(listview->count(), model.rowCount());
2686 for (int i = 0; i < model.rowCount(); ++i) {
2687 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2688 QTRY_VERIFY(display != 0);
2689 QTRY_COMPARE(display->text(), strings.at(i));
2695 void tst_QQuickListView::propertyChanges()
2697 QQuickView *canvas = createView();
2698 QTRY_VERIFY(canvas);
2699 canvas->setSource(testFileUrl("propertychangestest.qml"));
2701 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
2702 QTRY_VERIFY(listView);
2704 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
2705 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
2706 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
2707 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
2708 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
2709 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
2710 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
2712 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
2713 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
2714 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
2715 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange);
2716 QTRY_COMPARE(listView->isWrapEnabled(), true);
2717 QTRY_COMPARE(listView->cacheBuffer(), 10);
2718 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem);
2720 listView->setHighlightFollowsCurrentItem(false);
2721 listView->setPreferredHighlightBegin(1.0);
2722 listView->setPreferredHighlightEnd(1.0);
2723 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
2724 listView->setWrapEnabled(false);
2725 listView->setCacheBuffer(3);
2726 listView->setSnapMode(QQuickListView::SnapOneItem);
2728 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
2729 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
2730 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
2731 QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange);
2732 QTRY_COMPARE(listView->isWrapEnabled(), false);
2733 QTRY_COMPARE(listView->cacheBuffer(), 3);
2734 QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem);
2736 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
2737 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
2738 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
2739 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
2740 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2741 QTRY_COMPARE(cacheBufferSpy.count(),1);
2742 QTRY_COMPARE(snapModeSpy.count(),1);
2744 listView->setHighlightFollowsCurrentItem(false);
2745 listView->setPreferredHighlightBegin(1.0);
2746 listView->setPreferredHighlightEnd(1.0);
2747 listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange);
2748 listView->setWrapEnabled(false);
2749 listView->setCacheBuffer(3);
2750 listView->setSnapMode(QQuickListView::SnapOneItem);
2752 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
2753 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
2754 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
2755 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
2756 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2757 QTRY_COMPARE(cacheBufferSpy.count(),1);
2758 QTRY_COMPARE(snapModeSpy.count(),1);
2763 void tst_QQuickListView::componentChanges()
2765 QQuickView *canvas = createView();
2766 QTRY_VERIFY(canvas);
2767 canvas->setSource(testFileUrl("propertychangestest.qml"));
2769 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
2770 QTRY_VERIFY(listView);
2772 QQmlComponent component(canvas->engine());
2773 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2775 QQmlComponent delegateComponent(canvas->engine());
2776 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2778 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
2779 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
2780 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
2781 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
2783 listView->setHighlight(&component);
2784 listView->setHeader(&component);
2785 listView->setFooter(&component);
2786 listView->setDelegate(&delegateComponent);
2788 QTRY_COMPARE(listView->highlight(), &component);
2789 QTRY_COMPARE(listView->header(), &component);
2790 QTRY_COMPARE(listView->footer(), &component);
2791 QTRY_COMPARE(listView->delegate(), &delegateComponent);
2793 QTRY_COMPARE(highlightSpy.count(),1);
2794 QTRY_COMPARE(delegateSpy.count(),1);
2795 QTRY_COMPARE(headerSpy.count(),1);
2796 QTRY_COMPARE(footerSpy.count(),1);
2798 listView->setHighlight(&component);
2799 listView->setHeader(&component);
2800 listView->setFooter(&component);
2801 listView->setDelegate(&delegateComponent);
2803 QTRY_COMPARE(highlightSpy.count(),1);
2804 QTRY_COMPARE(delegateSpy.count(),1);
2805 QTRY_COMPARE(headerSpy.count(),1);
2806 QTRY_COMPARE(footerSpy.count(),1);
2811 void tst_QQuickListView::modelChanges()
2813 QQuickView *canvas = createView();
2814 QTRY_VERIFY(canvas);
2815 canvas->setSource(testFileUrl("propertychangestest.qml"));
2817 QQuickListView *listView = canvas->rootObject()->findChild<QQuickListView*>("listView");
2818 QTRY_VERIFY(listView);
2820 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2821 QTRY_VERIFY(alternateModel);
2822 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2823 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
2825 listView->setModel(modelVariant);
2826 QTRY_COMPARE(listView->model(), modelVariant);
2827 QTRY_COMPARE(modelSpy.count(),1);
2829 listView->setModel(modelVariant);
2830 QTRY_COMPARE(modelSpy.count(),1);
2832 listView->setModel(QVariant());
2833 QTRY_COMPARE(modelSpy.count(),2);
2838 void tst_QQuickListView::QTBUG_9791()
2840 QQuickView *canvas = createView();
2842 canvas->setSource(testFileUrl("strictlyenforcerange.qml"));
2843 qApp->processEvents();
2845 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
2846 QTRY_VERIFY(listview != 0);
2848 QQuickItem *contentItem = listview->contentItem();
2849 QTRY_VERIFY(contentItem != 0);
2850 QTRY_VERIFY(listview->delegate() != 0);
2851 QTRY_VERIFY(listview->model() != 0);
2853 QMetaObject::invokeMethod(listview, "fillModel");
2854 qApp->processEvents();
2856 // Confirm items positioned correctly
2857 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
2858 QCOMPARE(itemCount, 3);
2860 for (int i = 0; i < itemCount; ++i) {
2861 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2862 if (!item) qWarning() << "Item" << i << "not found";
2864 QTRY_COMPARE(item->x(), i*300.0);
2867 // check that view is positioned correctly
2868 QTRY_COMPARE(listview->contentX(), 590.0);
2873 void tst_QQuickListView::manualHighlight()
2875 QQuickView *canvas = new QQuickView(0);
2876 canvas->setGeometry(0,0,240,320);
2878 QString filename(testFile("manual-highlight.qml"));
2879 canvas->setSource(QUrl::fromLocalFile(filename));
2881 qApp->processEvents();
2883 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2884 QTRY_VERIFY(listview != 0);
2886 QQuickItem *contentItem = listview->contentItem();
2887 QTRY_VERIFY(contentItem != 0);
2889 QTRY_COMPARE(listview->currentIndex(), 0);
2890 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2891 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
2893 listview->setCurrentIndex(2);
2895 QTRY_COMPARE(listview->currentIndex(), 2);
2896 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2897 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
2900 listview->positionViewAtIndex(3, QQuickListView::Contain);
2902 QTRY_COMPARE(listview->currentIndex(), 2);
2903 QTRY_COMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2904 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
2909 void tst_QQuickListView::QTBUG_11105()
2911 QQuickView *canvas = createView();
2914 for (int i = 0; i < 30; i++)
2915 model.addItem("Item" + QString::number(i), "");
2917 QQmlContext *ctxt = canvas->rootContext();
2918 ctxt->setContextProperty("testModel", &model);
2920 TestObject *testObject = new TestObject;
2921 ctxt->setContextProperty("testObject", testObject);
2923 canvas->setSource(testFileUrl("listviewtest.qml"));
2925 qApp->processEvents();
2927 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2928 QTRY_VERIFY(listview != 0);
2929 QQuickItem *contentItem = listview->contentItem();
2930 QTRY_VERIFY(contentItem != 0);
2931 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
2933 // Confirm items positioned correctly
2934 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2935 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2936 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2937 if (!item) qWarning() << "Item" << i << "not found";
2939 QTRY_VERIFY(item->y() == i*20);
2942 listview->positionViewAtIndex(20, QQuickListView::Beginning);
2943 QCOMPARE(listview->contentY(), 280.);
2945 QmlListModel model2;
2946 for (int i = 0; i < 5; i++)
2947 model2.addItem("Item" + QString::number(i), "");
2949 ctxt->setContextProperty("testModel", &model2);
2951 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2952 QCOMPARE(itemCount, 5);
2958 void tst_QQuickListView::header()
2960 QFETCH(QQuickListView::Orientation, orientation);
2961 QFETCH(Qt::LayoutDirection, layoutDirection);
2962 QFETCH(QPointF, initialHeaderPos);
2963 QFETCH(QPointF, firstDelegatePos);
2964 QFETCH(QPointF, initialContentPos);
2965 QFETCH(QPointF, changedHeaderPos);
2966 QFETCH(QPointF, changedContentPos);
2967 QFETCH(QPointF, resizeContentPos);
2970 for (int i = 0; i < 30; i++)
2971 model.addItem("Item" + QString::number(i), "");
2973 QQuickView *canvas = createView();
2974 canvas->rootContext()->setContextProperty("testModel", &model);
2975 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
2976 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
2977 canvas->setSource(testFileUrl("header.qml"));
2979 qApp->processEvents();
2981 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
2982 QTRY_VERIFY(listview != 0);
2983 listview->setOrientation(orientation);
2984 listview->setLayoutDirection(layoutDirection);
2986 QQuickItem *contentItem = listview->contentItem();
2987 QTRY_VERIFY(contentItem != 0);
2989 QQuickText *header = 0;
2990 QTRY_VERIFY(header = findItem<QQuickText>(contentItem, "header"));
2991 QVERIFY(header == listview->headerItem());
2993 QCOMPARE(header->width(), 100.);
2994 QCOMPARE(header->height(), 30.);
2995 QCOMPARE(header->pos(), initialHeaderPos);
2996 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
2998 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3000 QCOMPARE(item->pos(), firstDelegatePos);
3003 QTRY_COMPARE(listview->count(), model.count());
3004 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3006 for (int i = 0; i < 30; i++)
3007 model.addItem("Item" + QString::number(i), "");
3009 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
3010 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3012 QCOMPARE(headerItemSpy.count(), 1);
3014 header = findItem<QQuickText>(contentItem, "header");
3016 header = findItem<QQuickText>(contentItem, "header2");
3019 QVERIFY(header == listview->headerItem());
3021 QCOMPARE(header->pos(), changedHeaderPos);
3022 QCOMPARE(header->width(), 50.);
3023 QCOMPARE(header->height(), 20.);
3024 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3026 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3028 QCOMPARE(item->pos(), firstDelegatePos);
3033 // QTBUG-21207 header should become visible if view resizes from initial empty size
3035 canvas = createView();
3036 canvas->rootContext()->setContextProperty("testModel", &model);
3037 canvas->rootContext()->setContextProperty("initialViewWidth", 0.0);
3038 canvas->rootContext()->setContextProperty("initialViewHeight", 0.0);
3039 canvas->setSource(testFileUrl("header.qml"));
3041 qApp->processEvents();
3043 listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3044 QTRY_VERIFY(listview != 0);
3045 listview->setOrientation(orientation);
3046 listview->setLayoutDirection(layoutDirection);
3047 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3049 listview->setWidth(240);
3050 listview->setHeight(320);
3051 QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos);
3052 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3058 void tst_QQuickListView::header_data()
3060 QTest::addColumn<QQuickListView::Orientation>("orientation");
3061 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3062 QTest::addColumn<QPointF>("initialHeaderPos");
3063 QTest::addColumn<QPointF>("changedHeaderPos");
3064 QTest::addColumn<QPointF>("initialContentPos");
3065 QTest::addColumn<QPointF>("changedContentPos");
3066 QTest::addColumn<QPointF>("firstDelegatePos");
3067 QTest::addColumn<QPointF>("resizeContentPos");
3069 // header1 = 100 x 30
3070 // header2 = 50 x 20
3071 // delegates = 240 x 20
3074 // header above items, top left
3075 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight
3083 // header above items, top right
3084 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft
3092 // header to left of items
3093 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight
3101 // header to right of items
3102 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft
3105 << QPointF(-240 + 100, 0)
3106 << QPointF(-240 + 50, 0)
3108 << QPointF(-240 + 40, 0);
3111 void tst_QQuickListView::header_delayItemCreation()
3113 QQuickView *canvas = createView();
3117 canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false));
3118 canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml"));
3119 qApp->processEvents();
3121 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3122 QTRY_VERIFY(listview != 0);
3124 QQuickItem *contentItem = listview->contentItem();
3125 QTRY_VERIFY(contentItem != 0);
3127 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3129 QCOMPARE(header->y(), -header->height());
3131 QCOMPARE(listview->contentY(), -header->height());
3134 QTRY_COMPARE(header->y(), -header->height());
3139 void tst_QQuickListView::footer()
3141 QFETCH(QQuickListView::Orientation, orientation);
3142 QFETCH(Qt::LayoutDirection, layoutDirection);
3143 QFETCH(QPointF, initialFooterPos);
3144 QFETCH(QPointF, firstDelegatePos);
3145 QFETCH(QPointF, initialContentPos);
3146 QFETCH(QPointF, changedFooterPos);
3147 QFETCH(QPointF, changedContentPos);
3148 QFETCH(QPointF, resizeContentPos);
3150 QQuickView *canvas = createView();
3153 for (int i = 0; i < 3; i++)
3154 model.addItem("Item" + QString::number(i), "");
3156 QQmlContext *ctxt = canvas->rootContext();
3157 ctxt->setContextProperty("testModel", &model);
3159 canvas->setSource(testFileUrl("footer.qml"));
3161 qApp->processEvents();
3163 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3164 QTRY_VERIFY(listview != 0);
3165 listview->setOrientation(orientation);
3166 listview->setLayoutDirection(layoutDirection);
3167 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3169 QQuickItem *contentItem = listview->contentItem();
3170 QTRY_VERIFY(contentItem != 0);
3172 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3175 QVERIFY(footer == listview->footerItem());
3177 QCOMPARE(footer->pos(), initialFooterPos);
3178 QCOMPARE(footer->width(), 100.);
3179 QCOMPARE(footer->height(), 30.);
3180 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
3182 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3184 QCOMPARE(item->pos(), firstDelegatePos);
3187 model.removeItem(1);
3189 if (orientation == QQuickListView::Vertical) {
3190 QTRY_COMPARE(footer->y(), initialFooterPos.y() - 20); // delegate height = 20
3192 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
3193 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
3199 QPointF posWhenNoItems(0, 0);
3200 if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft)
3201 posWhenNoItems.setX(-100);
3202 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3204 // if header is present, it's at a negative pos, so the footer should not move
3205 canvas->rootObject()->setProperty("showHeader", true);
3206 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3207 canvas->rootObject()->setProperty("showHeader", false);
3210 for (int i = 0; i < 30; i++)
3211 model.addItem("Item" + QString::number(i), "");
3213 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
3214 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3216 QCOMPARE(footerItemSpy.count(), 1);
3218 footer = findItem<QQuickText>(contentItem, "footer");
3220 footer = findItem<QQuickText>(contentItem, "footer2");
3223 QVERIFY(footer == listview->footerItem());
3225 QCOMPARE(footer->pos(), changedFooterPos);
3226 QCOMPARE(footer->width(), 50.);
3227 QCOMPARE(footer->height(), 20.);
3228 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
3230 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3232 QCOMPARE(item->pos(), firstDelegatePos);
3234 listview->positionViewAtEnd();
3235 footer->setHeight(10);
3236 footer->setWidth(40);
3237 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos);
3242 void tst_QQuickListView::footer_data()
3244 QTest::addColumn<QQuickListView::Orientation>("orientation");
3245 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3246 QTest::addColumn<QPointF>("initialFooterPos");
3247 QTest::addColumn<QPointF>("changedFooterPos");
3248 QTest::addColumn<QPointF>("initialContentPos");
3249 QTest::addColumn<QPointF>("changedContentPos");
3250 QTest::addColumn<QPointF>("firstDelegatePos");
3251 QTest::addColumn<QPointF>("resizeContentPos");
3253 // footer1 = 100 x 30
3254 // footer2 = 50 x 20
3255 // delegates = 40 x 20
3257 // view height = 320
3259 // footer below items, bottom left
3260 QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight
3261 << QPointF(0, 3 * 20)
3262 << QPointF(0, 30 * 20) // added 30 items
3266 << QPointF(0, 30 * 20 - 320 + 10);
3268 // footer below items, bottom right
3269 QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft
3270 << QPointF(0, 3 * 20)
3271 << QPointF(0, 30 * 20)
3275 << QPointF(0, 30 * 20 - 320 + 10);
3277 // footer to right of items
3278 QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight
3279 << QPointF(40 * 3, 0)
3280 << QPointF(40 * 30, 0)
3284 << QPointF(40 * 30 - 240 + 40, 0);
3286 // footer to left of items
3287 QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft
3288 << QPointF(-(40 * 3) - 100, 0)
3289 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
3293 << QPointF(-(40 * 30) - 40, 0);
3296 class LVAccessor : public QQuickListView
3299 qreal minY() const { return minYExtent(); }
3300 qreal maxY() const { return maxYExtent(); }
3301 qreal minX() const { return minXExtent(); }
3302 qreal maxX() const { return maxXExtent(); }
3305 void tst_QQuickListView::headerFooter()
3309 QQuickView *canvas = createView();
3312 QQmlContext *ctxt = canvas->rootContext();
3313 ctxt->setContextProperty("testModel", &model);
3315 canvas->setSource(testFileUrl("headerfooter.qml"));
3316 qApp->processEvents();
3318 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3319 QTRY_VERIFY(listview != 0);
3321 QQuickItem *contentItem = listview->contentItem();
3322 QTRY_VERIFY(contentItem != 0);
3324 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3326 QCOMPARE(header->y(), -header->height());
3328 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3330 QCOMPARE(footer->y(), 0.);
3332 QCOMPARE(static_cast<LVAccessor*>(listview)->minY(), header->height());
3333 QCOMPARE(static_cast<LVAccessor*>(listview)->maxY(), header->height());
3339 QQuickView *canvas = createView();
3342 QQmlContext *ctxt = canvas->rootContext();
3343 ctxt->setContextProperty("testModel", &model);
3345 canvas->setSource(testFileUrl("headerfooter.qml"));
3346 canvas->rootObject()->setProperty("horizontal", true);
3347 qApp->processEvents();
3349 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3350 QTRY_VERIFY(listview != 0);
3352 QQuickItem *contentItem = listview->contentItem();
3353 QTRY_VERIFY(contentItem != 0);
3355 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3357 QCOMPARE(header->x(), -header->width());
3359 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3361 QCOMPARE(footer->x(), 0.);
3363 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), header->width());
3364 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), header->width());
3370 QQuickView *canvas = createView();
3373 QQmlContext *ctxt = canvas->rootContext();
3374 ctxt->setContextProperty("testModel", &model);
3376 canvas->setSource(testFileUrl("headerfooter.qml"));
3377 canvas->rootObject()->setProperty("horizontal", true);
3378 canvas->rootObject()->setProperty("rtl", true);
3379 qApp->processEvents();
3381 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3382 QTRY_VERIFY(listview != 0);
3384 QQuickItem *contentItem = listview->contentItem();
3385 QTRY_VERIFY(contentItem != 0);
3387 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3389 QCOMPARE(header->x(), 0.);
3391 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3393 QCOMPARE(footer->x(), -footer->width());
3395 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), 240. - header->width());
3396 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), 240. - header->width());
3402 void tst_QQuickListView::resizeView()
3404 QQuickView *canvas = createView();
3407 for (int i = 0; i < 40; i++)
3408 model.addItem("Item" + QString::number(i), "");
3410 QQmlContext *ctxt = canvas->rootContext();
3411 ctxt->setContextProperty("testModel", &model);
3413 TestObject *testObject = new TestObject;
3414 ctxt->setContextProperty("testObject", testObject);
3416 canvas->setSource(testFileUrl("listviewtest.qml"));
3418 qApp->processEvents();
3420 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3421 QTRY_VERIFY(listview != 0);
3422 QQuickItem *contentItem = listview->contentItem();
3423 QTRY_VERIFY(contentItem != 0);
3424 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3426 // Confirm items positioned correctly
3427 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3428 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3429 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3430 if (!item) qWarning() << "Item" << i << "not found";
3432 QTRY_COMPARE(item->y(), i*20.);
3435 QVariant heightRatio;
3436 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3437 QCOMPARE(heightRatio.toReal(), 0.4);
3439 listview->setHeight(200);
3440 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3442 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
3443 QCOMPARE(heightRatio.toReal(), 0.25);
3445 // Ensure we handle -ve sizes
3446 listview->setHeight(-100);
3447 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 1);
3449 listview->setCacheBuffer(200);
3450 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 11);
3452 // ensure items in cache become visible
3453 listview->setHeight(200);
3454 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 21);
3456 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3457 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3458 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3459 if (!item) qWarning() << "Item" << i << "not found";
3461 QTRY_COMPARE(item->y(), i*20.);
3462 QCOMPARE(item->isVisible(), i < 11); // inside view visible, outside not visible
3465 // ensure items outside view become invisible
3466 listview->setHeight(100);
3467 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 16);
3469 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3470 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3471 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3472 if (!item) qWarning() << "Item" << i << "not found";
3474 QTRY_COMPARE(item->y(), i*20.);
3475 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3482 void tst_QQuickListView::resizeViewAndRepaint()
3484 QQuickView *canvas = createView();
3487 for (int i = 0; i < 40; i++)
3488 model.addItem("Item" + QString::number(i), "");
3490 QQmlContext *ctxt = canvas->rootContext();
3491 ctxt->setContextProperty("testModel", &model);
3492 ctxt->setContextProperty("initialHeight", 100);
3494 canvas->setSource(testFileUrl("resizeview.qml"));
3496 qApp->processEvents();
3498 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3499 QTRY_VERIFY(listview != 0);
3500 QQuickItem *contentItem = listview->contentItem();
3501 QTRY_VERIFY(contentItem != 0);
3502 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3504 // item at index 10 should not be currently visible
3505 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3507 listview->setHeight(320);
3509 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3511 listview->setHeight(100);
3512 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3517 void tst_QQuickListView::sizeLessThan1()
3519 QQuickView *canvas = createView();
3522 for (int i = 0; i < 30; i++)
3523 model.addItem("Item" + QString::number(i), "");
3525 QQmlContext *ctxt = canvas->rootContext();
3526 ctxt->setContextProperty("testModel", &model);
3528 TestObject *testObject = new TestObject;
3529 ctxt->setContextProperty("testObject", testObject);
3531 canvas->setSource(testFileUrl("sizelessthan1.qml"));
3533 qApp->processEvents();
3535 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3536 QTRY_VERIFY(listview != 0);
3537 QQuickItem *contentItem = listview->contentItem();
3538 QTRY_VERIFY(contentItem != 0);
3539 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3541 // Confirm items positioned correctly
3542 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3543 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3544 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3545 if (!item) qWarning() << "Item" << i << "not found";
3547 QTRY_COMPARE(item->y(), i*0.5);
3554 void tst_QQuickListView::QTBUG_14821()
3556 QQuickView *canvas = createView();
3558 canvas->setSource(testFileUrl("qtbug14821.qml"));
3559 qApp->processEvents();
3561 QQuickListView *listview = qobject_cast<QQuickListView*>(canvas->rootObject());
3562 QVERIFY(listview != 0);
3564 QQuickItem *contentItem = listview->contentItem();
3565 QVERIFY(contentItem != 0);
3567 listview->decrementCurrentIndex();
3568 QCOMPARE(listview->currentIndex(), 99);
3570 listview->incrementCurrentIndex();
3571 QCOMPARE(listview->currentIndex(), 0);
3576 void tst_QQuickListView::resizeDelegate()
3578 QQuickView *canvas = createView();
3580 QStringList strings;
3581 for (int i = 0; i < 30; ++i)
3582 strings << QString::number(i);
3583 QStringListModel model(strings);
3585 QQmlContext *ctxt = canvas->rootContext();
3586 ctxt->setContextProperty("testModel", &model);
3588 canvas->setSource(testFileUrl("displaylist.qml"));
3590 qApp->processEvents();
3592 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3593 QVERIFY(listview != 0);
3594 QQuickItem *contentItem = listview->contentItem();
3595 QVERIFY(contentItem != 0);
3596 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3598 QCOMPARE(listview->count(), model.rowCount());
3600 listview->setCurrentIndex(25);
3601 listview->setContentY(0);
3602 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3604 for (int i = 0; i < 16; ++i) {
3605 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3607 QCOMPARE(item->y(), i*20.0);
3610 QCOMPARE(listview->currentItem()->y(), 500.0);
3611 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
3613 canvas->rootObject()->setProperty("delegateHeight", 30);
3614 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3616 for (int i = 0; i < 11; ++i) {
3617 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3619 QTRY_COMPARE(item->y(), i*30.0);
3622 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
3623 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
3625 listview->setCurrentIndex(1);
3626 listview->positionViewAtIndex(25, QQuickListView::Beginning);
3627 listview->positionViewAtIndex(5, QQuickListView::Beginning);
3628 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3630 for (int i = 5; i < 16; ++i) {
3631 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3633 QCOMPARE(item->y(), i*30.0);
3636 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
3637 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
3639 canvas->rootObject()->setProperty("delegateHeight", 20);
3640 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3642 for (int i = 5; i < 11; ++i) {
3643 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3645 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
3648 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
3649 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
3654 void tst_QQuickListView::resizeFirstDelegate()
3656 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
3657 // and other delegates have height > 0
3659 QQuickView *canvas = createView();
3661 // bug only occurs when all items in the model are visible
3663 for (int i = 0; i < 10; i++)
3664 model.addItem("Item" + QString::number(i), "");
3666 QQmlContext *ctxt = canvas->rootContext();
3667 ctxt->setContextProperty("testModel", &model);
3669 TestObject *testObject = new TestObject;
3670 ctxt->setContextProperty("testObject", testObject);
3672 canvas->setSource(testFileUrl("listviewtest.qml"));
3674 qApp->processEvents();
3676 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3677 QVERIFY(listview != 0);
3678 QQuickItem *contentItem = listview->contentItem();
3679 QVERIFY(contentItem != 0);
3680 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3682 QQuickItem *item = 0;
3683 for (int i = 0; i < model.count(); ++i) {
3684 item = findItem<QQuickItem>(contentItem, "wrapper", i);
3686 QCOMPARE(item->y(), i*20.0);
3689 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3692 // check the content y has not jumped up and down
3693 QCOMPARE(listview->contentY(), 0.0);
3694 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
3696 QCOMPARE(spy.count(), 0);
3698 for (int i = 1; i < model.count(); ++i) {
3699 item = findItem<QQuickItem>(contentItem, "wrapper", i);
3701 QTRY_COMPARE(item->y(), (i-1)*20.0);
3705 // QTBUG-22014: refill doesn't clear items scrolling off the top of the
3706 // list if they follow a zero-sized delegate
3708 for (int i = 0; i < 10; i++)
3709 model.addItem("Item" + QString::number(i), "");
3710 QTRY_COMPARE(listview->count(), model.count());
3712 item = findItem<QQuickItem>(contentItem, "wrapper", 1);
3716 listview->setCurrentIndex(19);
3717 qApp->processEvents();
3718 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3720 // items 0-2 should have been deleted
3721 for (int i=0; i<3; i++) {
3722 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", i));
3729 void tst_QQuickListView::QTBUG_16037()
3731 QQuickView *canvas = createView();
3734 canvas->setSource(testFileUrl("qtbug16037.qml"));
3735 qApp->processEvents();
3737 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
3738 QTRY_VERIFY(listview != 0);
3740 QVERIFY(listview->contentHeight() <= 0.0);
3742 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
3744 QTRY_COMPARE(listview->contentHeight(), 80.0);
3749 void tst_QQuickListView::indexAt_itemAt_data()
3751 QTest::addColumn<qreal>("x");
3752 QTest::addColumn<qreal>("y");
3753 QTest::addColumn<int>("index");
3755 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3756 QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
3757 QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
3758 QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
3759 QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
3762 void tst_QQuickListView::indexAt_itemAt()
3768 QQuickView *canvas = createView();
3771 for (int i = 0; i < 30; i++)
3772 model.addItem("Item" + QString::number(i), "");
3774 QQmlContext *ctxt = canvas->rootContext();
3775 ctxt->setContextProperty("testModel", &model);
3777 TestObject *testObject = new TestObject;
3778 ctxt->setContextProperty("testObject", testObject);
3780 canvas->setSource(testFileUrl("listviewtest.qml"));
3782 qApp->processEvents();
3784 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3785 QTRY_VERIFY(listview != 0);
3787 QQuickItem *contentItem = listview->contentItem();
3788 QTRY_VERIFY(contentItem != 0);
3789 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3791 QQuickItem *item = 0;
3793 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3796 QCOMPARE(listview->indexAt(x,y), index);
3797 QVERIFY(listview->itemAt(x,y) == item);
3803 void tst_QQuickListView::incrementalModel()
3805 QQuickView *canvas = createView();
3807 IncrementalModel model;
3808 QQmlContext *ctxt = canvas->rootContext();
3809 ctxt->setContextProperty("testModel", &model);
3811 canvas->setSource(testFileUrl("displaylist.qml"));
3812 qApp->processEvents();
3814 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
3815 QTRY_VERIFY(listview != 0);
3817 QQuickItem *contentItem = listview->contentItem();
3818 QTRY_VERIFY(contentItem != 0);
3820 QTRY_COMPARE(listview->count(), 20);
3822 listview->positionViewAtIndex(10, QQuickListView::Beginning);
3824 QTRY_COMPARE(listview->count(), 25);
3829 void tst_QQuickListView::onAdd()
3831 QFETCH(int, initialItemCount);
3832 QFETCH(int, itemsToAdd);
3834 const int delegateHeight = 10;
3837 // these initial items should not trigger ListView.onAdd
3838 for (int i=0; i<initialItemCount; i++)
3839 model.addItem("dummy value", "dummy value");
3841 QQuickView *canvas = createView();
3842 canvas->setGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd));
3843 QQmlContext *ctxt = canvas->rootContext();
3844 ctxt->setContextProperty("testModel", &model);
3845 ctxt->setContextProperty("delegateHeight", delegateHeight);
3846 canvas->setSource(testFileUrl("attachedSignals.qml"));
3848 QObject *object = canvas->rootObject();
3849 object->setProperty("width", canvas->width());
3850 object->setProperty("height", canvas->height());
3851 qApp->processEvents();
3853 QList<QPair<QString, QString> > items;
3854 for (int i=0; i<itemsToAdd; i++)
3855 items << qMakePair(QString("value %1").arg(i), QString::number(i));
3856 model.addItems(items);
3857 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
3859 QVariantList result = object->property("addedDelegates").toList();
3860 QCOMPARE(result.count(), items.count());
3861 for (int i=0; i<items.count(); i++)
3862 QCOMPARE(result[i].toString(), items[i].first);
3867 void tst_QQuickListView::onAdd_data()
3869 QTest::addColumn<int>("initialItemCount");
3870 QTest::addColumn<int>("itemsToAdd");
3872 QTest::newRow("0, add 1") << 0 << 1;
3873 QTest::newRow("0, add 2") << 0 << 2;
3874 QTest::newRow("0, add 10") << 0 << 10;
3876 QTest::newRow("1, add 1") << 1 << 1;
3877 QTest::newRow("1, add 2") << 1 << 2;
3878 QTest::newRow("1, add 10") << 1 << 10;
3880 QTest::newRow("5, add 1") << 5 << 1;
3881 QTest::newRow("5, add 2") << 5 << 2;
3882 QTest::newRow("5, add 10") << 5 << 10;
3885 void tst_QQuickListView::onRemove()
3887 QFETCH(int, initialItemCount);
3888 QFETCH(int, indexToRemove);
3889 QFETCH(int, removeCount);
3891 const int delegateHeight = 10;
3893 for (int i=0; i<initialItemCount; i++)
3894 model.addItem(QString("value %1").arg(i), "dummy value");
3896 QQuickView *canvas = createView();
3897 QQmlContext *ctxt = canvas->rootContext();
3898 ctxt->setContextProperty("testModel", &model);
3899 ctxt->setContextProperty("delegateHeight", delegateHeight);
3900 canvas->setSource(testFileUrl("attachedSignals.qml"));
3902 QObject *object = canvas->rootObject();
3904 model.removeItems(indexToRemove, removeCount);
3905 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
3907 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
3912 void tst_QQuickListView::onRemove_data()
3914 QTest::addColumn<int>("initialItemCount");
3915 QTest::addColumn<int>("indexToRemove");
3916 QTest::addColumn<int>("removeCount");
3918 QTest::newRow("remove first") << 1 << 0 << 1;
3919 QTest::newRow("two items, remove first") << 2 << 0 << 1;
3920 QTest::newRow("two items, remove last") << 2 << 1 << 1;
3921 QTest::newRow("two items, remove all") << 2 << 0 << 2;
3923 QTest::newRow("four items, remove first") << 4 << 0 << 1;
3924 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
3925 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
3926 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
3927 QTest::newRow("four items, remove last") << 4 << 3 << 1;
3928 QTest::newRow("four items, remove all") << 4 << 0 << 4;
3930 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
3931 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
3932 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
3935 void tst_QQuickListView::rightToLeft()
3937 QQuickView *canvas = createView();
3938 canvas->setGeometry(0,0,640,320);
3939 canvas->setSource(testFileUrl("rightToLeft.qml"));
3941 qApp->processEvents();
3943 QVERIFY(canvas->rootObject() != 0);
3944 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "view");
3945 QTRY_VERIFY(listview != 0);
3947 QQuickItem *contentItem = listview->contentItem();
3948 QTRY_VERIFY(contentItem != 0);
3950 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
3952 QQuickVisualItemModel *model = canvas->rootObject()->findChild<QQuickVisualItemModel*>("itemModel");
3953 QTRY_VERIFY(model != 0);
3955 QTRY_VERIFY(model->count() == 3);
3956 QTRY_COMPARE(listview->currentIndex(), 0);
3958 // initial position at first item, right edge aligned
3959 QCOMPARE(listview->contentX(), -640.);
3961 QQuickItem *item = findItem<QQuickItem>(contentItem, "item1");
3963 QTRY_COMPARE(item->x(), -100.0);
3964 QCOMPARE(item->height(), listview->height());
3966 QQuickText *text = findItem<QQuickText>(contentItem, "text1");
3968 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
3970 listview->setCurrentIndex(2);
3972 item = findItem<QQuickItem>(contentItem, "item3");
3974 QTRY_COMPARE(item->x(), -540.0);
3976 text = findItem<QQuickText>(contentItem, "text3");
3978 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
3980 QCOMPARE(listview->contentX(), -640.);
3982 // Ensure resizing maintains position relative to right edge
3983 qobject_cast<QQuickItem*>(canvas->rootObject())->setWidth(600);
3984 QTRY_COMPARE(listview->contentX(), -600.);
3989 void tst_QQuickListView::test_mirroring()
3991 QQuickView *canvasA = createView();
3992 canvasA->setSource(testFileUrl("rightToLeft.qml"));
3993 QQuickListView *listviewA = findItem<QQuickListView>(canvasA->rootObject(), "view");
3994 QTRY_VERIFY(listviewA != 0);
3996 QQuickView *canvasB = createView();
3997 canvasB->setSource(testFileUrl("rightToLeft.qml"));
3998 QQuickListView *listviewB = findItem<QQuickListView>(canvasB->rootObject(), "view");
3999 QTRY_VERIFY(listviewA != 0);
4000 qApp->processEvents();
4002 QList<QString> objectNames;
4003 objectNames << "item1" << "item2"; // << "item3"
4005 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4006 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4007 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
4010 foreach (const QString objectName, objectNames)
4011 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4013 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4014 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
4017 foreach (const QString objectName, objectNames)
4018 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4020 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
4021 QQuickItemPrivate::get(listviewB)->setLayoutMirror(true);
4022 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
4024 // LTR != LTR+mirror
4025 foreach (const QString objectName, objectNames)
4026 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4028 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
4030 // RTL == LTR+mirror
4031 foreach (const QString objectName, objectNames)
4032 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4034 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
4036 // RTL != RTL+mirror
4037 foreach (const QString objectName, objectNames)
4038 QVERIFY(findItem<QQuickItem>(listviewA, objectName)->x() != findItem<QQuickItem>(listviewB, objectName)->x());
4040 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
4042 // LTR == RTL+mirror
4043 foreach (const QString objectName, objectNames)
4044 QCOMPARE(findItem<QQuickItem>(listviewA, objectName)->x(), findItem<QQuickItem>(listviewB, objectName)->x());
4050 void tst_QQuickListView::margins()
4052 QQuickView *canvas = createView();
4055 for (int i = 0; i < 50; i++)
4056 model.addItem("Item" + QString::number(i), "");
4058 QQmlContext *ctxt = canvas->rootContext();
4059 ctxt->setContextProperty("testModel", &model);
4061 canvas->setSource(testFileUrl("margins.qml"));
4063 qApp->processEvents();
4065 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4066 QTRY_VERIFY(listview != 0);
4067 QQuickItem *contentItem = listview->contentItem();
4068 QTRY_VERIFY(contentItem != 0);
4069 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4071 QCOMPARE(listview->contentY(), -30.);
4072 QCOMPARE(listview->yOrigin(), 0.);
4075 listview->positionViewAtEnd();
4076 qreal pos = listview->contentY();
4077 listview->setContentY(pos + 80);
4078 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4079 listview->returnToBounds();
4080 QTRY_COMPARE(listview->contentY(), pos + 50);
4082 // remove item before visible and check that top margin is maintained
4083 // and yOrigin is updated
4084 listview->setContentY(100);
4085 model.removeItem(1);
4086 QTRY_COMPARE(listview->count(), model.count());
4087 listview->setContentY(-50);
4088 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4089 listview->returnToBounds();
4090 QCOMPARE(listview->yOrigin(), 20.);
4091 QTRY_COMPARE(listview->contentY(), -10.);
4093 // reduce top margin
4094 listview->setTopMargin(20);
4095 QCOMPARE(listview->yOrigin(), 20.);
4096 QTRY_COMPARE(listview->contentY(), 0.);
4099 listview->positionViewAtEnd();
4100 pos = listview->contentY();
4101 listview->setContentY(pos + 80);
4102 listview->returnToBounds();
4103 QTRY_COMPARE(listview->contentY(), pos + 50);
4105 // reduce bottom margin
4106 pos = listview->contentY();
4107 listview->setBottomMargin(40);
4108 QCOMPARE(listview->yOrigin(), 20.);
4109 QTRY_COMPARE(listview->contentY(), pos-10);
4115 void tst_QQuickListView::marginsResize()
4117 QFETCH(QQuickListView::Orientation, orientation);
4118 QFETCH(Qt::LayoutDirection, layoutDirection);
4119 QFETCH(qreal, start);
4122 QQuickView *canvas = createView();
4124 canvas->setSource(testFileUrl("margins2.qml"));
4126 qApp->processEvents();
4128 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "listview");
4129 QTRY_VERIFY(listview != 0);
4131 listview->setOrientation(orientation);
4132 listview->setLayoutDirection(layoutDirection);
4133 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4135 // view is resized after componentCompleted - top margin should still be visible
4136 if (orientation == QQuickListView::Vertical)
4137 QCOMPARE(listview->contentY(), start);
4139 QCOMPARE(listview->contentX(), start);
4141 // move to last index and ensure bottom margin is visible.
4142 listview->setCurrentIndex(19);
4143 if (orientation == QQuickListView::Vertical)
4144 QTRY_COMPARE(listview->contentY(), end);
4146 QTRY_COMPARE(listview->contentX(), end);
4148 // back to top - top margin should be visible.
4149 listview->setCurrentIndex(0);
4150 if (orientation == QQuickListView::Vertical)
4151 QTRY_COMPARE(listview->contentY(), start);
4153 QTRY_COMPARE(listview->contentX(), start);
4158 void tst_QQuickListView::marginsResize_data()
4160 QTest::addColumn<QQuickListView::Orientation>("orientation");
4161 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4162 QTest::addColumn<qreal>("start");
4163 QTest::addColumn<qreal>("end");
4165 QTest::newRow("vertical") << QQuickListView::Vertical << Qt::LeftToRight << -20.0 << 1020.0;
4166 QTest::newRow("horizontal") << QQuickListView::Horizontal << Qt::LeftToRight << -20.0 << 1020.0;
4167 QTest::newRow("horizontal, rtl") << QQuickListView::Horizontal << Qt::RightToLeft << -180.0 << -1220.0;
4170 void tst_QQuickListView::snapToItem_data()
4172 QTest::addColumn<QQuickListView::Orientation>("orientation");
4173 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4174 QTest::addColumn<int>("highlightRangeMode");
4175 QTest::addColumn<QPoint>("flickStart");
4176 QTest::addColumn<QPoint>("flickEnd");
4177 QTest::addColumn<qreal>("snapAlignment");
4178 QTest::addColumn<qreal>("endExtent");
4179 QTest::addColumn<qreal>("startExtent");
4181 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4182 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0;
4184 QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4185 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0;
4187 QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4188 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 << -240.0;
4190 QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4191 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0;
4193 QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4194 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0;
4196 QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4197 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 - 140.0 << -220.0;
4200 void tst_QQuickListView::snapToItem()
4202 QFETCH(QQuickListView::Orientation, orientation);
4203 QFETCH(Qt::LayoutDirection, layoutDirection);
4204 QFETCH(int, highlightRangeMode);
4205 QFETCH(QPoint, flickStart);
4206 QFETCH(QPoint, flickEnd);
4207 QFETCH(qreal, snapAlignment);
4208 QFETCH(qreal, endExtent);
4209 QFETCH(qreal, startExtent);
4211 QQuickView *canvas = createView();
4213 canvas->setSource(testFileUrl("snapToItem.qml"));
4215 qApp->processEvents();
4217 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4218 QTRY_VERIFY(listview != 0);
4220 listview->setOrientation(orientation);
4221 listview->setLayoutDirection(layoutDirection);
4222 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4223 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4225 QQuickItem *contentItem = listview->contentItem();
4226 QTRY_VERIFY(contentItem != 0);
4228 // confirm that a flick hits an item boundary
4229 flick(canvas, flickStart, flickEnd, 180);
4230 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4231 if (orientation == QQuickListView::Vertical)
4232 QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment);
4234 QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment);
4238 flick(canvas, flickStart, flickEnd, 180);
4239 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4240 } while (orientation == QQuickListView::Vertical
4241 ? !listview->isAtYEnd()
4242 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4244 if (orientation == QQuickListView::Vertical)
4245 QCOMPARE(listview->contentY(), endExtent);
4247 QCOMPARE(listview->contentX(), endExtent);
4251 flick(canvas, flickEnd, flickStart, 180);
4252 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4253 } while (orientation == QQuickListView::Vertical
4254 ? !listview->isAtYBeginning()
4255 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4257 if (orientation == QQuickListView::Vertical)
4258 QCOMPARE(listview->contentY(), startExtent);
4260 QCOMPARE(listview->contentX(), startExtent);
4265 void tst_QQuickListView::qListModelInterface_items()
4267 items<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4270 void tst_QQuickListView::qListModelInterface_package_items()
4272 items<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4275 void tst_QQuickListView::qAbstractItemModel_items()
4277 items<QaimModel>(testFileUrl("listviewtest.qml"), false);
4280 void tst_QQuickListView::qListModelInterface_changed()
4282 changed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4285 void tst_QQuickListView::qListModelInterface_package_changed()
4287 changed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4290 void tst_QQuickListView::qAbstractItemModel_changed()
4292 changed<QaimModel>(testFileUrl("listviewtest.qml"), false);
4295 void tst_QQuickListView::qListModelInterface_inserted()
4297 inserted<QmlListModel>(testFileUrl("listviewtest.qml"));
4300 void tst_QQuickListView::qListModelInterface_package_inserted()
4302 inserted<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4305 void tst_QQuickListView::qListModelInterface_inserted_more()
4307 inserted_more<QmlListModel>();
4310 void tst_QQuickListView::qListModelInterface_inserted_more_data()
4312 inserted_more_data();
4315 void tst_QQuickListView::qAbstractItemModel_inserted()
4317 inserted<QaimModel>(testFileUrl("listviewtest.qml"));
4320 void tst_QQuickListView::qAbstractItemModel_inserted_more()
4322 inserted_more<QaimModel>();
4325 void tst_QQuickListView::qAbstractItemModel_inserted_more_data()
4327 inserted_more_data();
4330 void tst_QQuickListView::qListModelInterface_removed()
4332 removed<QmlListModel>(testFileUrl("listviewtest.qml"), false);
4333 removed<QmlListModel>(testFileUrl("listviewtest.qml"), true);
4336 void tst_QQuickListView::qListModelInterface_removed_more()
4338 removed_more<QmlListModel>(testFileUrl("listviewtest.qml"));
4341 void tst_QQuickListView::qListModelInterface_removed_more_data()
4343 removed_more_data();
4346 void tst_QQuickListView::qListModelInterface_package_removed()
4348 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), false);
4349 removed<QmlListModel>(testFileUrl("listviewtest-package.qml"), true);
4352 void tst_QQuickListView::qAbstractItemModel_removed()
4354 removed<QaimModel>(testFileUrl("listviewtest.qml"), false);
4355 removed<QaimModel>(testFileUrl("listviewtest.qml"), true);
4358 void tst_QQuickListView::qAbstractItemModel_removed_more()
4360 removed_more<QaimModel>(testFileUrl("listviewtest.qml"));
4363 void tst_QQuickListView::qAbstractItemModel_removed_more_data()
4365 removed_more_data();
4368 void tst_QQuickListView::qListModelInterface_moved()
4370 moved<QmlListModel>(testFileUrl("listviewtest.qml"));
4373 void tst_QQuickListView::qListModelInterface_moved_data()
4378 void tst_QQuickListView::qListModelInterface_package_moved()
4380 moved<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4383 void tst_QQuickListView::qListModelInterface_package_moved_data()
4388 void tst_QQuickListView::qAbstractItemModel_moved()
4390 moved<QaimModel>(testFileUrl("listviewtest.qml"));
4393 void tst_QQuickListView::qAbstractItemModel_moved_data()
4398 void tst_QQuickListView::qListModelInterface_clear()
4400 clear<QmlListModel>(testFileUrl("listviewtest.qml"));
4403 void tst_QQuickListView::qListModelInterface_package_clear()
4405 clear<QmlListModel>(testFileUrl("listviewtest-package.qml"));
4408 void tst_QQuickListView::qAbstractItemModel_clear()
4410 clear<QaimModel>(testFileUrl("listviewtest.qml"));
4413 void tst_QQuickListView::qListModelInterface_sections()
4415 sections<QmlListModel>(testFileUrl("listview-sections.qml"));
4418 void tst_QQuickListView::qListModelInterface_package_sections()
4420 sections<QmlListModel>(testFileUrl("listview-sections-package.qml"));
4423 void tst_QQuickListView::qAbstractItemModel_sections()
4425 sections<QaimModel>(testFileUrl("listview-sections.qml"));
4428 void tst_QQuickListView::creationContext()
4431 canvas.setGeometry(0,0,240,320);
4432 canvas.setSource(testFileUrl("creationContext.qml"));
4433 qApp->processEvents();
4435 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4437 QVERIFY(rootItem->property("count").toInt() > 0);
4440 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
4441 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4442 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4443 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4444 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4445 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4446 QVERIFY(item = rootItem->findChild<QQuickItem *>("section"));
4447 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4450 void tst_QQuickListView::QTBUG_21742()
4453 canvas.setGeometry(0,0,200,200);
4454 canvas.setSource(testFileUrl("qtbug-21742.qml"));
4455 qApp->processEvents();
4457 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4459 QCOMPARE(rootItem->property("count").toInt(), 1);
4462 void tst_QQuickListView::asynchronous()
4464 QQuickView *canvas = createView();
4466 QQmlIncubationController controller;
4467 canvas->engine()->setIncubationController(&controller);
4469 canvas->setSource(testFileUrl("asyncloader.qml"));
4471 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
4472 QVERIFY(rootObject);
4474 QQuickListView *listview = 0;
4477 controller.incubateWhile(&b);
4478 listview = rootObject->findChild<QQuickListView*>("view");
4481 // items will be created one at a time
4482 for (int i = 0; i < 8; ++i) {
4483 QVERIFY(findItem<QQuickItem>(listview, "wrapper", i) == 0);
4484 QQuickItem *item = 0;
4487 controller.incubateWhile(&b);
4488 item = findItem<QQuickItem>(listview, "wrapper", i);
4494 controller.incubateWhile(&b);
4497 // verify positioning
4498 QQuickItem *contentItem = listview->contentItem();
4499 for (int i = 0; i < 8; ++i) {
4500 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4501 QTRY_COMPARE(item->y(), i*50.0);
4507 void tst_QQuickListView::snapOneItem_data()
4509 QTest::addColumn<QQuickListView::Orientation>("orientation");
4510 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4511 QTest::addColumn<int>("highlightRangeMode");
4512 QTest::addColumn<QPoint>("flickStart");
4513 QTest::addColumn<QPoint>("flickEnd");
4514 QTest::addColumn<qreal>("snapAlignment");
4515 QTest::addColumn<qreal>("endExtent");
4516 QTest::addColumn<qreal>("startExtent");
4518 QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4519 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
4521 QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4522 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0;
4524 QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4525 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0;
4527 QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4528 << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
4530 QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4531 << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0;
4533 QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4534 << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0;
4537 void tst_QQuickListView::snapOneItem()
4539 QFETCH(QQuickListView::Orientation, orientation);
4540 QFETCH(Qt::LayoutDirection, layoutDirection);
4541 QFETCH(int, highlightRangeMode);
4542 QFETCH(QPoint, flickStart);
4543 QFETCH(QPoint, flickEnd);
4544 QFETCH(qreal, snapAlignment);
4545 QFETCH(qreal, endExtent);
4546 QFETCH(qreal, startExtent);
4549 // This test seems to be unreliable - different test data fails on different runs
4550 QSKIP("QTBUG-24338");
4553 QQuickView *canvas = createView();
4555 canvas->setSource(testFileUrl("snapOneItem.qml"));
4557 qApp->processEvents();
4559 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4560 QTRY_VERIFY(listview != 0);
4562 listview->setOrientation(orientation);
4563 listview->setLayoutDirection(layoutDirection);
4564 listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4565 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4567 QQuickItem *contentItem = listview->contentItem();
4568 QTRY_VERIFY(contentItem != 0);
4570 QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged()));
4572 // confirm that a flick hits the next item boundary
4573 flick(canvas, flickStart, flickEnd, 180);
4574 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4575 if (orientation == QQuickListView::Vertical)
4576 QCOMPARE(listview->contentY(), snapAlignment);
4578 QCOMPARE(listview->contentX(), snapAlignment);
4580 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4581 QCOMPARE(listview->currentIndex(), 1);
4582 QCOMPARE(currentIndexSpy.count(), 1);
4587 flick(canvas, flickStart, flickEnd, 180);
4588 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4589 } while (orientation == QQuickListView::Vertical
4590 ? !listview->isAtYEnd()
4591 : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning());
4593 if (orientation == QQuickListView::Vertical)
4594 QCOMPARE(listview->contentY(), endExtent);
4596 QCOMPARE(listview->contentX(), endExtent);
4598 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4599 QCOMPARE(listview->currentIndex(), 3);
4600 QCOMPARE(currentIndexSpy.count(), 3);
4605 flick(canvas, flickEnd, flickStart, 180);
4606 QTRY_VERIFY(listview->isMoving() == false); // wait until it stops
4607 } while (orientation == QQuickListView::Vertical
4608 ? !listview->isAtYBeginning()
4609 : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd());
4611 if (orientation == QQuickListView::Vertical)
4612 QCOMPARE(listview->contentY(), startExtent);
4614 QCOMPARE(listview->contentX(), startExtent);
4616 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4617 QCOMPARE(listview->currentIndex(), 0);
4618 QCOMPARE(currentIndexSpy.count(), 6);
4624 void tst_QQuickListView::unrequestedVisibility()
4627 for (int i = 0; i < 30; i++)
4628 model.addItem("Item" + QString::number(i), QString::number(i));
4630 QQuickView *canvas = new QQuickView(0);
4631 canvas->setGeometry(0,0,240,320);
4633 QQmlContext *ctxt = canvas->rootContext();
4634 ctxt->setContextProperty("testModel", &model);
4635 ctxt->setContextProperty("testWrap", QVariant(false));
4637 canvas->setSource(testFileUrl("unrequestedItems.qml"));
4639 qApp->processEvents();
4641 QQuickListView *leftview = findItem<QQuickListView>(canvas->rootObject(), "leftList");
4642 QTRY_VERIFY(leftview != 0);
4644 QQuickListView *rightview = findItem<QQuickListView>(canvas->rootObject(), "rightList");
4645 QTRY_VERIFY(rightview != 0);
4647 QQuickItem *leftContent = leftview->contentItem();
4648 QTRY_VERIFY(leftContent != 0);
4650 QQuickItem *rightContent = rightview->contentItem();
4651 QTRY_VERIFY(rightContent != 0);
4653 rightview->setCurrentIndex(20);
4655 QTRY_COMPARE(leftview->contentY(), 0.0);
4656 QTRY_COMPARE(rightview->contentY(), 100.0);
4660 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4661 QCOMPARE(item->isVisible(), true);
4662 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4663 QCOMPARE(item->isVisible(), false);
4665 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
4666 QCOMPARE(item->isVisible(), false);
4667 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
4668 QCOMPARE(item->isVisible(), true);
4670 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 16));
4671 QCOMPARE(item->isVisible(), true);
4672 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 17));
4673 QCOMPARE(item->isVisible(), false);
4674 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
4675 QCOMPARE(item->isVisible(), false);
4676 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
4677 QCOMPARE(item->isVisible(), true);
4679 rightview->setCurrentIndex(0);
4681 QTRY_COMPARE(leftview->contentY(), 0.0);
4682 QTRY_COMPARE(rightview->contentY(), 0.0);
4684 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4685 QCOMPARE(item->isVisible(), true);
4686 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4687 QTRY_COMPARE(item->isVisible(), true);
4689 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 19));
4690 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 19));
4692 leftview->setCurrentIndex(20);
4694 QTRY_COMPARE(leftview->contentY(), 100.0);
4695 QTRY_COMPARE(rightview->contentY(), 0.0);
4697 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4698 QTRY_COMPARE(item->isVisible(), false);
4699 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4700 QCOMPARE(item->isVisible(), true);
4702 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
4703 QCOMPARE(item->isVisible(), true);
4704 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
4705 QCOMPARE(item->isVisible(), false);
4707 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
4708 QCOMPARE(item->isVisible(), false);
4709 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4710 QCOMPARE(item->isVisible(), true);
4711 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4712 QCOMPARE(item->isVisible(), true);
4713 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4714 QCOMPARE(item->isVisible(), false);
4716 model.moveItems(19, 1, 1);
4717 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4719 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
4720 QCOMPARE(item->isVisible(), false);
4721 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
4722 QCOMPARE(item->isVisible(), true);
4724 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 19));
4725 QCOMPARE(item->isVisible(), true);
4726 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 19));
4727 QCOMPARE(item->isVisible(), false);
4729 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4730 QCOMPARE(item->isVisible(), false);
4731 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4732 QCOMPARE(item->isVisible(), true);
4733 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4734 QCOMPARE(item->isVisible(), true);
4735 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4736 QCOMPARE(item->isVisible(), false);
4738 model.moveItems(3, 4, 1);
4739 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4741 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4742 QCOMPARE(item->isVisible(), false);
4743 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4744 QCOMPARE(item->isVisible(), true);
4745 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4746 QCOMPARE(item->isVisible(), true);
4747 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4748 QCOMPARE(item->isVisible(), false);
4750 model.moveItems(4, 3, 1);
4751 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4753 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4754 QCOMPARE(item->isVisible(), false);
4755 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4756 QCOMPARE(item->isVisible(), true);
4757 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4758 QCOMPARE(item->isVisible(), true);
4759 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4760 QCOMPARE(item->isVisible(), false);
4762 model.moveItems(16, 17, 1);
4763 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4765 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4766 QCOMPARE(item->isVisible(), false);
4767 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4768 QCOMPARE(item->isVisible(), true);
4769 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4770 QCOMPARE(item->isVisible(), true);
4771 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4772 QCOMPARE(item->isVisible(), false);
4774 model.moveItems(17, 16, 1);
4775 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
4777 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 4));
4778 QCOMPARE(item->isVisible(), false);
4779 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
4780 QCOMPARE(item->isVisible(), true);
4781 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 16));
4782 QCOMPARE(item->isVisible(), true);
4783 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 17));
4784 QCOMPARE(item->isVisible(), false);
4789 void tst_QQuickListView::populateTransitions()
4791 QFETCH(bool, staticallyPopulate);
4792 QFETCH(bool, dynamicallyPopulate);
4793 QFETCH(bool, usePopulateTransition);
4795 QPointF transitionFrom(-50, -50);
4796 QPointF transitionVia(100, 100);
4797 QaimModel model_transitionFrom;
4798 QaimModel model_transitionVia;
4801 if (staticallyPopulate) {
4802 for (int i = 0; i < 30; i++)
4803 model.addItem("item" + QString::number(i), "");
4806 QQuickView *canvas = createView();
4807 canvas->rootContext()->setContextProperty("testModel", &model);
4808 canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext()));
4809 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4810 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4811 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4812 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4813 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4814 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4815 canvas->setSource(testFileUrl("populateTransitions.qml"));
4818 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4820 QQuickItem *contentItem = listview->contentItem();
4821 QVERIFY(contentItem);
4823 if (staticallyPopulate || dynamicallyPopulate) {
4824 // check the populate transition is run
4825 if (usePopulateTransition) {
4826 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 17);
4828 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4829 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0);
4831 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
4833 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4836 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4837 if (usePopulateTransition)
4838 QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt());
4839 for (int i=0; i < model.count() && i < itemCount; ++i) {
4840 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4841 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4842 QTRY_COMPARE(item->x(), 0.0);
4843 QTRY_COMPARE(item->y(), i*20.0);
4844 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4846 QTRY_COMPARE(name->text(), model.name(i));
4849 // add an item and check this is done with add trantion, not populate
4850 model.insertItem(0, "another item", "");
4851 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1);
4852 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(),
4853 (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 17 : 0);
4856 canvas->rootContext()->setContextProperty("testModel", QVariant());
4857 QTRY_COMPARE(listview->count(), 0);
4858 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4859 listview->setProperty("countPopulateTransitions", 0);
4860 listview->setProperty("countAddTransitions", 0);
4862 // set to a valid model and check populate transition is run a second time
4864 for (int i = 0; i < 30; i++)
4865 model.addItem("item" + QString::number(i), "");
4866 canvas->rootContext()->setContextProperty("testModel", &model);
4867 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 17 : 0);
4868 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
4870 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4871 if (usePopulateTransition)
4872 QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt());
4873 for (int i=0; i < model.count() && i < itemCount; ++i) {
4874 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4875 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4876 QTRY_COMPARE(item->x(), 0.0);
4877 QTRY_COMPARE(item->y(), i*20.0);
4878 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4880 QTRY_COMPARE(name->text(), model.name(i));
4883 // reset model and check populate transition is run again
4884 listview->setProperty("countPopulateTransitions", 0);
4885 listview->setProperty("countAddTransitions", 0);
4887 QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 17 : 0);
4888 QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0);
4890 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4891 if (usePopulateTransition)
4892 QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt());
4893 for (int i=0; i < model.count() && i < itemCount; ++i) {
4894 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4895 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4896 QTRY_COMPARE(item->x(), 0.0);
4897 QTRY_COMPARE(item->y(), i*20.0);
4898 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4900 QTRY_COMPARE(name->text(), model.name(i));
4906 void tst_QQuickListView::populateTransitions_data()
4908 QTest::addColumn<bool>("staticallyPopulate");
4909 QTest::addColumn<bool>("dynamicallyPopulate");
4910 QTest::addColumn<bool>("usePopulateTransition");
4912 QTest::newRow("static") << true << false << true;
4913 QTest::newRow("static, no populate") << true << false << false;
4915 QTest::newRow("dynamic") << false << true << true;
4916 QTest::newRow("dynamic, no populate") << false << true << false;
4918 QTest::newRow("empty to start with") << false << false << true;
4919 QTest::newRow("empty to start with, no populate") << false << false << false;
4922 void tst_QQuickListView::addTransitions()
4924 QFETCH(int, initialItemCount);
4925 QFETCH(bool, shouldAnimateTargets);
4926 QFETCH(qreal, contentY);
4927 QFETCH(int, insertionIndex);
4928 QFETCH(int, insertionCount);
4929 QFETCH(ListRange, expectedDisplacedIndexes);
4931 // added items should start here
4932 QPointF targetItems_transitionFrom(-50, -50);
4934 // displaced items should pass through this point
4935 QPointF displacedItems_transitionVia(100, 100);
4938 for (int i = 0; i < initialItemCount; i++)
4939 model.addItem("Original item" + QString::number(i), "");
4940 QaimModel model_targetItems_transitionFrom;
4941 QaimModel model_displacedItems_transitionVia;
4943 QQuickView *canvas = createView();
4944 QQmlContext *ctxt = canvas->rootContext();
4945 TestObject *testObject = new TestObject;
4946 ctxt->setContextProperty("testModel", &model);
4947 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4948 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4949 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4950 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4951 ctxt->setContextProperty("testObject", testObject);
4952 canvas->setSource(testFileUrl("addTransitions.qml"));
4955 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
4956 QTRY_VERIFY(listview != 0);
4957 QQuickItem *contentItem = listview->contentItem();
4958 QVERIFY(contentItem != 0);
4959 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4961 if (contentY != 0) {
4962 listview->setContentY(contentY);
4963 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
4966 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4968 // only target items that will become visible should be animated
4969 QList<QPair<QString, QString> > newData;
4970 QList<QPair<QString, QString> > expectedTargetData;
4971 QList<int> targetIndexes;
4972 if (shouldAnimateTargets) {
4973 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4974 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4976 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items
4977 expectedTargetData << newData.last();
4981 QVERIFY(expectedTargetData.count() > 0);
4985 if (!newData.isEmpty()) {
4986 model.insertItems(insertionIndex, newData);
4987 QTRY_COMPARE(model.count(), listview->count());
4990 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4992 if (shouldAnimateTargets) {
4993 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4994 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
4995 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4997 // check the target and displaced items were animated
4998 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4999 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5001 // check attached properties
5002 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5003 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5004 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5005 if (expectedDisplacedIndexes.isValid()) {
5006 // adjust expectedDisplacedIndexes to their final values after the move
5007 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
5008 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5009 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5010 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5014 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
5015 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5018 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5019 int firstVisibleIndex = -1;
5020 int itemCount = items.count();
5021 for (int i=0; i<items.count(); i++) {
5022 if (items[i]->y() >= contentY) {
5023 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5024 firstVisibleIndex = e.evaluate().toInt();
5028 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5030 // verify all items moved to the correct final positions
5031 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5032 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5033 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5034 QTRY_COMPARE(item->y(), i*20.0);
5035 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5037 QTRY_COMPARE(name->text(), model.name(i));
5044 void tst_QQuickListView::addTransitions_data()
5046 QTest::addColumn<int>("initialItemCount");
5047 QTest::addColumn<qreal>("contentY");
5048 QTest::addColumn<bool>("shouldAnimateTargets");
5049 QTest::addColumn<int>("insertionIndex");
5050 QTest::addColumn<int>("insertionCount");
5051 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5053 // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items
5054 QTest::newRow("insert 1, just before start")
5055 << 30 << 20.0 << false
5056 << 0 << 1 << ListRange();
5057 QTest::newRow("insert 1, way before start")
5058 << 30 << 20.0 << false
5059 << 0 << 1 << ListRange();
5060 QTest::newRow("insert multiple, just before start")
5061 << 30 << 100.0 << false
5062 << 0 << 3 << ListRange();
5063 QTest::newRow("insert multiple, way before start")
5064 << 30 << 100.0 << false
5065 << 0 << 3 << ListRange();
5067 QTest::newRow("insert 1 at start")
5068 << 30 << 0.0 << true
5069 << 0 << 1 << ListRange(0, 15);
5070 QTest::newRow("insert multiple at start")
5071 << 30 << 0.0 << true
5072 << 0 << 3 << ListRange(0, 15);
5073 QTest::newRow("insert 1 at start, content y not 0")
5074 << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2
5075 << 2 << 1 << ListRange(0 + 2, 15 + 2);
5076 QTest::newRow("insert multiple at start, content y not 0")
5077 << 30 << 40.0 << true // first visible is index 2
5078 << 2 << 3 << ListRange(0 + 2, 15 + 2);
5080 QTest::newRow("insert 1 at start, to empty list")
5082 << 0 << 1 << ListRange();
5083 QTest::newRow("insert multiple at start, to empty list")
5085 << 0 << 3 << ListRange();
5087 QTest::newRow("insert 1 at middle")
5088 << 30 << 0.0 << true
5089 << 5 << 1 << ListRange(5, 15);
5090 QTest::newRow("insert multiple at middle")
5091 << 30 << 0.0 << true
5092 << 5 << 3 << ListRange(5, 15);
5094 QTest::newRow("insert 1 at bottom")
5095 << 30 << 0.0 << true
5096 << 15 << 1 << ListRange(15, 15);
5097 QTest::newRow("insert multiple at bottom")
5098 << 30 << 0.0 << true
5099 << 15 << 3 << ListRange(15, 15);
5100 QTest::newRow("insert 1 at bottom, content y not 0")
5101 << 30 << 20.0 * 3 << true
5102 << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3);
5103 QTest::newRow("insert multiple at bottom, content y not 0")
5104 << 30 << 20.0 * 3 << true
5105 << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3);
5107 // items added after the last visible will not be animated in, since they
5108 // do not appear in the final view
5109 QTest::newRow("insert 1 after end")
5110 << 30 << 0.0 << false
5111 << 17 << 1 << ListRange();
5112 QTest::newRow("insert multiple after end")
5113 << 30 << 0.0 << false
5114 << 17 << 3 << ListRange();
5117 void tst_QQuickListView::moveTransitions()
5119 QFETCH(int, initialItemCount);
5120 QFETCH(qreal, contentY);
5121 QFETCH(qreal, itemsOffsetAfterMove);
5122 QFETCH(int, moveFrom);
5123 QFETCH(int, moveTo);
5124 QFETCH(int, moveCount);
5125 QFETCH(ListRange, expectedDisplacedIndexes);
5127 // target and displaced items should pass through these points
5128 QPointF targetItems_transitionVia(-50, 50);
5129 QPointF displacedItems_transitionVia(100, 100);
5132 for (int i = 0; i < initialItemCount; i++)
5133 model.addItem("Original item" + QString::number(i), "");
5134 QaimModel model_targetItems_transitionVia;
5135 QaimModel model_displacedItems_transitionVia;
5137 QQuickView *canvas = createView();
5138 QQmlContext *ctxt = canvas->rootContext();
5139 TestObject *testObject = new TestObject;
5140 ctxt->setContextProperty("testModel", &model);
5141 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
5142 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5143 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
5144 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5145 ctxt->setContextProperty("testObject", testObject);
5146 canvas->setSource(testFileUrl("moveTransitions.qml"));
5149 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5150 QTRY_VERIFY(listview != 0);
5151 QQuickItem *contentItem = listview->contentItem();
5152 QVERIFY(contentItem != 0);
5155 if (contentY != 0) {
5156 listview->setContentY(contentY);
5157 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5160 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5162 // Items moving to *or* from visible positions should be animated.
5163 // Otherwise, they should not be animated.
5164 QList<QPair<QString, QString> > expectedTargetData;
5165 QList<int> targetIndexes;
5166 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
5167 int toIndex = moveTo + (i - moveFrom);
5168 if (i <= (contentY + listview->height()) / 20
5169 || toIndex < (contentY + listview->height()) / 20) {
5170 expectedTargetData << qMakePair(model.name(i), model.number(i));
5174 // ViewTransition.index provides the indices that items are moving to, not from
5175 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
5178 model.moveItems(moveFrom, moveTo, moveCount);
5180 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5181 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5182 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5184 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5186 // check the target and displaced items were animated
5187 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
5188 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5190 // check attached properties
5191 matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes);
5192 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5193 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5194 if (expectedDisplacedIndexes.isValid()) {
5195 // adjust expectedDisplacedIndexes to their final values after the move
5196 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
5197 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5198 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5199 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5202 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5203 int firstVisibleIndex = -1;
5204 for (int i=0; i<items.count(); i++) {
5205 if (items[i]->y() >= contentY) {
5206 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5207 firstVisibleIndex = e.evaluate().toInt();
5211 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5213 // verify all items moved to the correct final positions
5214 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5215 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5216 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5217 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5218 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
5219 name = findItem<QQuickText>(contentItem, "textName", i);
5221 QTRY_COMPARE(name->text(), model.name(i));
5228 void tst_QQuickListView::moveTransitions_data()
5230 QTest::addColumn<int>("initialItemCount");
5231 QTest::addColumn<qreal>("contentY");
5232 QTest::addColumn<qreal>("itemsOffsetAfterMove");
5233 QTest::addColumn<int>("moveFrom");
5234 QTest::addColumn<int>("moveTo");
5235 QTest::addColumn<int>("moveCount");
5236 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5238 // when removing from above the visible, all items shift down depending on how many
5239 // items have been removed from above the visible
5240 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0
5241 << 1 << 10 << 1 << ListRange(11, 15+4);
5242 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0
5243 << 0 << 10 << 1 << ListRange(11, 15+4);
5244 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0
5245 << 1 << 10 << 2 << ListRange(12, 15+4);
5246 QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0
5247 << 0 << 10 << 3 << ListRange(13, 15+4);
5248 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0
5249 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4);
5250 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0
5251 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4);
5253 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
5254 << 1 << 10 << 1 << ListRange(2, 10);
5255 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
5256 << 0 << 10 << 1 << ListRange(1, 10);
5257 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5258 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4);
5259 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
5260 << 10 << 15 << 1 << ListRange(11, 15);
5261 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
5262 << 0 << 15 << 1 << ListRange(1, 15);
5264 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
5265 << 1 << 10 << 3 << ListRange(4, 12);
5266 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
5267 << 0 << 10 << 3 << ListRange(3, 12);
5268 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0
5269 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4);
5270 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
5271 << 5 << 13 << 3 << ListRange(8, 15);
5272 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
5273 << 0 << 13 << 3 << ListRange(3, 15);
5275 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
5276 << 10 << 1 << 1 << ListRange(1, 9);
5277 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
5278 << 10 << 0 << 1 << ListRange(0, 9);
5279 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5280 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5281 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0
5282 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4);
5283 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
5284 << 15 << 10 << 1 << ListRange(10, 14);
5285 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
5286 << 15 << 0 << 1 << ListRange(0, 14);
5288 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
5289 << 10 << 1 << 3 << ListRange(1, 9);
5290 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
5291 << 10 << 0 << 3 << ListRange(0, 9);
5292 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0
5293 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4);
5294 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
5295 << 13 << 5 << 3 << ListRange(5, 12);
5296 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
5297 << 13 << 0 << 3 << ListRange(0, 12);
5299 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
5300 << 20 << 0 << 1 << ListRange(0, 15);
5301 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
5302 << 25 << 4 << 1 << ListRange(0+4, 15+4);
5303 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
5304 << 20 << 0 << 3 << ListRange(0, 15);
5305 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0
5306 << 25 << 4 << 3 << ListRange(0+4, 15+4);
5308 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
5309 << 20 << 15 << 1 << ListRange(15, 15);
5310 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
5311 << 25 << 15+4 << 1 << ListRange(15+4, 15+4);
5312 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
5313 << 20 << 15 << 3 << ListRange(15, 15);
5314 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0
5315 << 25 << 15+4 << 3 << ListRange(15+4, 15+4);
5318 void tst_QQuickListView::removeTransitions()
5320 QFETCH(int, initialItemCount);
5321 QFETCH(bool, shouldAnimateTargets);
5322 QFETCH(qreal, contentY);
5323 QFETCH(int, removalIndex);
5324 QFETCH(int, removalCount);
5325 QFETCH(ListRange, expectedDisplacedIndexes);
5327 // added items should end here
5328 QPointF targetItems_transitionTo(-50, -50);
5330 // displaced items should pass through this points
5331 QPointF displacedItems_transitionVia(100, 100);
5334 for (int i = 0; i < initialItemCount; i++)
5335 model.addItem("Original item" + QString::number(i), "");
5336 QaimModel model_targetItems_transitionTo;
5337 QaimModel model_displacedItems_transitionVia;
5339 QQuickView *canvas = createView();
5340 QQmlContext *ctxt = canvas->rootContext();
5341 TestObject *testObject = new TestObject;
5342 ctxt->setContextProperty("testModel", &model);
5343 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5344 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5345 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5346 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5347 ctxt->setContextProperty("testObject", testObject);
5348 canvas->setSource(testFileUrl("removeTransitions.qml"));
5351 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5352 QTRY_VERIFY(listview != 0);
5353 QQuickItem *contentItem = listview->contentItem();
5354 QVERIFY(contentItem != 0);
5355 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5357 if (contentY != 0) {
5358 listview->setContentY(contentY);
5359 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5362 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5364 // only target items that are visible should be animated
5365 QList<QPair<QString, QString> > expectedTargetData;
5366 QList<int> targetIndexes;
5367 if (shouldAnimateTargets) {
5368 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5369 if (i >= contentY / 20 && i < (contentY + listview->height()) / 20) {
5370 expectedTargetData << qMakePair(model.name(i), model.number(i));
5374 QVERIFY(expectedTargetData.count() > 0);
5377 // calculate targetItems and expectedTargets before model changes
5378 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5379 QVariantMap expectedTargets;
5380 for (int i=0; i<targetIndexes.count(); i++)
5381 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5384 model.removeItems(removalIndex, removalCount);
5385 QTRY_COMPARE(model.count(), listview->count());
5387 if (shouldAnimateTargets) {
5388 QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5389 QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(),
5390 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5392 // check the target and displaced items were animated
5393 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5394 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5396 // check attached properties
5397 QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets);
5398 matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5399 matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems);
5400 if (expectedDisplacedIndexes.isValid()) {
5401 // adjust expectedDisplacedIndexes to their final values after the move
5402 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5403 matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5404 matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5405 matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems);
5408 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5409 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5412 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5413 int firstVisibleIndex = -1;
5414 int itemCount = items.count();
5416 for (int i=0; i<items.count(); i++) {
5417 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5418 int index = e.evaluate().toInt();
5419 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
5420 firstVisibleIndex = index;
5422 itemCount--; // exclude deleted items
5424 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5426 // verify all items moved to the correct final positions
5427 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5428 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5429 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5430 QCOMPARE(item->x(), 0.0);
5431 QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0);
5432 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5434 QTRY_COMPARE(name->text(), model.name(i));
5441 void tst_QQuickListView::removeTransitions_data()
5443 QTest::addColumn<int>("initialItemCount");
5444 QTest::addColumn<qreal>("contentY");
5445 QTest::addColumn<bool>("shouldAnimateTargets");
5446 QTest::addColumn<int>("removalIndex");
5447 QTest::addColumn<int>("removalCount");
5448 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5450 // All items that are visible following the remove operation should be animated.
5451 // Remove targets that are outside of the view should not be animated.
5453 QTest::newRow("remove 1 before start")
5454 << 30 << 20.0 * 3 << false
5455 << 2 << 1 << ListRange();
5456 QTest::newRow("remove multiple, all before start")
5457 << 30 << 20.0 * 3 << false
5458 << 0 << 3 << ListRange();
5459 QTest::newRow("remove mix of before and after start")
5460 << 30 << 20.0 * 3 << true
5461 << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove
5463 QTest::newRow("remove 1 from start")
5464 << 30 << 0.0 << true
5465 << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove
5466 QTest::newRow("remove multiple from start")
5467 << 30 << 0.0 << true
5468 << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove
5469 QTest::newRow("remove 1 from start, content y not 0")
5470 << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2
5471 << 2 << 1 << ListRange(1 + 2, 16 + 2);
5472 QTest::newRow("remove multiple from start, content y not 0")
5473 << 30 << 20.0 * 2 << true // first visible is index 2
5474 << 2 << 3 << ListRange(3 + 2, 18 + 2);
5476 QTest::newRow("remove 1 from middle")
5477 << 30 << 0.0 << true
5478 << 5 << 1 << ListRange(6, 16);
5479 QTest::newRow("remove multiple from middle")
5480 << 30 << 0.0 << true
5481 << 5 << 3 << ListRange(8, 18);
5484 QTest::newRow("remove 1 from bottom")
5485 << 30 << 0.0 << true
5486 << 15 << 1 << ListRange(16, 16);
5488 // remove 15, 16, 17
5489 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside
5490 // the view, and 18 will be animated as the displaced item to replace the last item
5491 QTest::newRow("remove multiple from bottom")
5492 << 30 << 0.0 << true
5493 << 15 << 3 << ListRange(18, 18);
5495 QTest::newRow("remove 1 from bottom, content y not 0")
5496 << 30 << 20.0 * 2 << true
5497 << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2);
5498 QTest::newRow("remove multiple from bottom, content y not 0")
5499 << 30 << 20.0 * 2 << true
5500 << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2);
5503 QTest::newRow("remove 1 after end")
5504 << 30 << 0.0 << false
5505 << 17 << 1 << ListRange();
5506 QTest::newRow("remove multiple after end")
5507 << 30 << 0.0 << false
5508 << 17 << 3 << ListRange();
5511 void tst_QQuickListView::multipleTransitions()
5513 // Tests that if you interrupt a transition in progress with another action that
5514 // cancels the previous transition, the resulting items are still placed correctly.
5516 QFETCH(int, initialCount);
5517 QFETCH(qreal, contentY);
5518 QFETCH(QList<ListChange>, changes);
5520 // add transitions on the left, moves on the right
5521 QPointF addTargets_transitionFrom(-50, -50);
5522 QPointF addDisplaced_transitionFrom(-50, 50);
5523 QPointF moveTargets_transitionFrom(50, -50);
5524 QPointF moveDisplaced_transitionFrom(50, 50);
5527 for (int i = 0; i < initialCount; i++)
5528 model.addItem("Original item" + QString::number(i), "");
5530 QQuickView *canvas = createView();
5531 QQmlContext *ctxt = canvas->rootContext();
5532 TestObject *testObject = new TestObject;
5533 ctxt->setContextProperty("testModel", &model);
5534 ctxt->setContextProperty("testObject", testObject);
5535 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5536 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5537 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5538 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5539 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5542 QQuickListView *listview = findItem<QQuickListView>(canvas->rootObject(), "list");
5543 QTRY_VERIFY(listview != 0);
5544 QQuickItem *contentItem = listview->contentItem();
5545 QVERIFY(contentItem != 0);
5546 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5548 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5550 QList<QPair<QString, QString> > targetItems;
5551 for (int i=0; i<changes.count(); i++) {
5552 switch (changes[i].type) {
5553 case ListChange::Inserted:
5555 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5556 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5557 model.insertItems(changes[i].index, targetItems);
5558 QTRY_COMPARE(model.count(), listview->count());
5559 QTRY_VERIFY(listview->property("runningAddTargets").toBool());
5560 QTRY_VERIFY(listview->property("runningAddDisplaced").toBool());
5561 if (i == changes.count() - 1) {
5562 QTRY_VERIFY(!listview->property("runningAddTargets").toBool());
5563 QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool());
5565 QTest::qWait(timeBetweenActions);
5569 case ListChange::Removed:
5570 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5571 targetItems << qMakePair(model.name(i), model.number(i));
5572 model.removeItems(changes[i].index, changes[i].count);
5573 QTRY_COMPARE(model.count(), listview->count());
5574 QTRY_VERIFY(listview->property("runningRemoveTargets").toBool());
5575 QTRY_VERIFY(listview->property("runningRemoveDisplaced").toBool());
5576 if (i == changes.count() - 1) {
5577 QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool());
5578 QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool());
5580 QTest::qWait(timeBetweenActions);
5583 case ListChange::Moved:
5584 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5585 targetItems << qMakePair(model.name(i), model.number(i));
5586 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5587 QTRY_VERIFY(listview->property("runningMoveTargets").toBool());
5588 QTRY_VERIFY(listview->property("runningMoveDisplaced").toBool());
5589 if (i == changes.count() - 1) {
5590 QTRY_VERIFY(!listview->property("runningMoveTargets").toBool());
5591 QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool());
5593 QTest::qWait(timeBetweenActions);
5596 case ListChange::SetCurrent:
5597 listview->setCurrentIndex(changes[i].index);
5599 case ListChange::SetContentY:
5600 listview->setContentY(changes[i].pos);
5601 QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
5605 QCOMPARE(listview->count(), model.count());
5607 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5608 int firstVisibleIndex = -1;
5609 for (int i=0; i<items.count(); i++) {
5610 if (items[i]->y() >= contentY) {
5611 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5612 firstVisibleIndex = e.evaluate().toInt();
5616 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5618 // verify all items moved to the correct final positions
5619 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5620 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5621 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5622 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5623 QTRY_COMPARE(item->x(), 0.0);
5624 QTRY_COMPARE(item->y(), i*20.0);
5625 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5627 QTRY_COMPARE(name->text(), model.name(i));
5634 void tst_QQuickListView::multipleTransitions_data()
5636 QTest::addColumn<int>("initialCount");
5637 QTest::addColumn<qreal>("contentY");
5638 QTest::addColumn<QList<ListChange> >("changes");
5640 // the added item and displaced items should move to final dest correctly
5641 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5642 << ListChange::insert(0, 1)
5643 << ListChange::move(0, 3, 1)
5646 // items affected by the add should change from move to add transition
5647 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5648 << ListChange::move(1, 10, 3)
5649 << ListChange::insert(0, 1)
5652 // items should be placed correctly if you trigger a transition then refill for that index
5653 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5654 << ListChange::insert(0, 1)
5655 << ListChange::setContentY(80.0)
5656 << ListChange::setContentY(0.0)
5657 << ListChange::insert(0, 1)
5661 QList<int> tst_QQuickListView::toIntList(const QVariantList &list)
5665 for (int i=0; i<list.count(); i++) {
5666 ret << list[i].toInt(&ok);
5668 qWarning() << "tst_QQuickListView::toIntList(): not a number:" << list[i];
5674 void tst_QQuickListView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
5676 for (int i=0; i<indexLists.count(); i++) {
5677 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
5678 if (current != expectedIndexes.toSet())
5679 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
5680 QCOMPARE(current, expectedIndexes.toSet());
5684 void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
5686 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
5687 QVERIFY(it.value().type() == QVariant::Int);
5688 QString name = it.key();
5689 int itemIndex = it.value().toInt();
5690 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
5691 if (model.name(itemIndex) != name)
5692 qDebug() << itemIndex;
5693 QCOMPARE(model.name(itemIndex), name);
5695 QCOMPARE(items.count(), expectedIndexes.count());
5698 void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
5700 for (int i=0; i<itemLists.count(); i++) {
5701 QVERIFY(itemLists[i].type() == QVariant::List);
5702 QVariantList current = itemLists[i].toList();
5703 for (int j=0; j<current.count(); j++) {
5704 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
5705 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
5706 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
5708 QCOMPARE(current.count(), expectedItems.count());
5713 QTEST_MAIN(tst_QQuickListView)
5715 #include "tst_qquicklistview.moc"