/**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/ ** ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** GNU Lesser General Public License Usage ** This file may be used under the terms of the GNU Lesser General Public ** License version 2.1 as published by the Free Software Foundation and ** appearing in the file LICENSE.LGPL included in the packaging of this ** file. Please review the following information to ensure the GNU Lesser ** General Public License version 2.1 requirements will be met: ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional ** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU General ** Public License version 3.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of this ** file. Please review the following information to ensure the GNU General ** Public License version 3.0 requirements will be met: ** http://www.gnu.org/copyleft/gpl.html. ** ** Other Usage ** Alternatively, this file may be used in accordance with the terms and ** conditions contained in a signed written agreement between you and Nokia. ** ** ** ** ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include "../../shared/util.h" #include "../shared/viewtestutil.h" #include "../shared/visualtestutil.h" #include "incrementalmodel.h" #include Q_DECLARE_METATYPE(Qt::LayoutDirection) Q_DECLARE_METATYPE(QQuickListView::Orientation) using namespace QQuickViewTestUtil; using namespace QQuickVisualTestUtil; class tst_QQuickListView : public QDeclarativeDataTest { Q_OBJECT public: tst_QQuickListView(); private slots: // Test both QListModelInterface and QAbstractItemModel model types void qListModelInterface_items(); void qListModelInterface_package_items(); void qAbstractItemModel_items(); void qListModelInterface_changed(); void qListModelInterface_package_changed(); void qAbstractItemModel_changed(); void qListModelInterface_inserted(); void qListModelInterface_inserted_more(); void qListModelInterface_inserted_more_data(); void qListModelInterface_package_inserted(); void qAbstractItemModel_inserted(); void qAbstractItemModel_inserted_more(); void qAbstractItemModel_inserted_more_data(); void qListModelInterface_removed(); void qListModelInterface_removed_more(); void qListModelInterface_removed_more_data(); void qListModelInterface_package_removed(); void qAbstractItemModel_removed(); void qAbstractItemModel_removed_more(); void qAbstractItemModel_removed_more_data(); void qListModelInterface_moved(); void qListModelInterface_moved_data(); void qListModelInterface_package_moved(); void qListModelInterface_package_moved_data(); void qAbstractItemModel_moved(); void qAbstractItemModel_moved_data(); void multipleChanges(); void multipleChanges_data(); void qListModelInterface_clear(); void qListModelInterface_package_clear(); void qAbstractItemModel_clear(); void insertBeforeVisible(); void insertBeforeVisible_data(); void swapWithFirstItem(); void itemList(); void currentIndex_delayedItemCreation(); void currentIndex_delayedItemCreation_data(); void currentIndex(); void noCurrentIndex(); void enforceRange(); void enforceRange_withoutHighlight(); void spacing(); void qListModelInterface_sections(); void qListModelInterface_package_sections(); void qAbstractItemModel_sections(); void sectionsPositioning(); void sectionsDelegate(); void cacheBuffer(); void positionViewAtIndex(); void resetModel(); void propertyChanges(); void componentChanges(); void modelChanges(); void manualHighlight(); void header(); void header_data(); void header_delayItemCreation(); void footer(); void footer_data(); void headerFooter(); void resizeView(); void resizeViewAndRepaint(); void sizeLessThan1(); void QTBUG_14821(); void resizeDelegate(); void resizeFirstDelegate(); void QTBUG_16037(); void indexAt_itemAt_data(); void indexAt_itemAt(); void incrementalModel(); void onAdd(); void onAdd_data(); void onRemove(); void onRemove_data(); void rightToLeft(); void test_mirroring(); void margins(); void marginsResize(); void marginsResize_data(); void creationContext(); void snapToItem_data(); void snapToItem(); void snapOneItem_data(); void snapOneItem(); void QTBUG_9791(); void QTBUG_11105(); void QTBUG_21742(); void asynchronous(); void unrequestedVisibility(); void populateTransitions(); void populateTransitions_data(); void addTransitions(); void addTransitions_data(); void moveTransitions(); void moveTransitions_data(); void removeTransitions(); void removeTransitions_data(); void multipleTransitions(); void multipleTransitions_data(); private: template void items(const QUrl &source, bool forceLayout); template void changed(const QUrl &source, bool forceLayout); template void inserted(const QUrl &source); template void inserted_more(); template void removed(const QUrl &source, bool animated); template void removed_more(const QUrl &source); template void moved(const QUrl &source); template void clear(const QUrl &source); template void sections(const QUrl &source); QList toIntList(const QVariantList &list); void matchIndexLists(const QVariantList &indexLists, const QList &expectedIndexes); void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList &expectedIndexes); void matchItemLists(const QVariantList &itemLists, const QList &expectedItems); void inserted_more_data(); void removed_more_data(); void moved_data(); }; class TestObject : public QObject { Q_OBJECT Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError) Q_PROPERTY(bool animate READ animate NOTIFY changedAnim) Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl) Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer) public: TestObject(QObject *parent = 0) : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false) , mCacheBuffer(0) {} bool error() const { return mError; } void setError(bool err) { mError = err; emit changedError(); } bool animate() const { return mAnimate; } void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); } bool invalidHighlight() const { return mInvalidHighlight; } void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); } int cacheBuffer() const { return mCacheBuffer; } void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); } signals: void changedError(); void changedAnim(); void changedHl(); void changedCacheBuffer(); public: bool mError; bool mAnimate; bool mInvalidHighlight; int mCacheBuffer; }; tst_QQuickListView::tst_QQuickListView() { } template void tst_QQuickListView::items(const QUrl &source, bool forceLayout) { QQuickView *canvas = createView(); T model; model.addItem("Fred", "12345"); model.addItem("John", "2345"); model.addItem("Bob", "54321"); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties"); QTRY_VERIFY(testObject->error() == false); QTRY_VERIFY(listview->highlightItem() != 0); QTRY_COMPARE(listview->count(), model.count()); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item // current item should be first item QTRY_COMPARE(listview->currentItem(), findItem(contentItem, "wrapper", 0)); for (int i = 0; i < model.count(); ++i) { QQuickText *name = findItem(contentItem, "textName", i); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); QQuickText *number = findItem(contentItem, "textNumber", i); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(i)); } // switch to other delegate testObject->setAnimate(true); QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties"); QTRY_VERIFY(testObject->error() == false); QTRY_VERIFY(listview->currentItem()); // set invalid highlight testObject->setInvalidHighlight(true); QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties"); QTRY_VERIFY(testObject->error() == false); QTRY_VERIFY(listview->currentItem()); QTRY_VERIFY(listview->highlightItem() == 0); // back to normal highlight testObject->setInvalidHighlight(false); QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties"); QTRY_VERIFY(testObject->error() == false); QTRY_VERIFY(listview->currentItem()); QTRY_VERIFY(listview->highlightItem() != 0); // set an empty model and confirm that items are destroyed T model2; ctxt->setContextProperty("testModel", &model2); // Force a layout, necessary if ListView is completed before VisualDataModel. if (forceLayout) QCOMPARE(listview->property("count").toInt(), 0); int itemCount = findItems(contentItem, "wrapper").count(); QTRY_VERIFY(itemCount == 0); QTRY_COMPARE(listview->highlightResizeSpeed(), 1000.0); QTRY_COMPARE(listview->highlightMoveSpeed(), 1000.0); delete canvas; delete testObject; } template void tst_QQuickListView::changed(const QUrl &source, bool forceLayout) { QQuickView *canvas = createView(); T model; model.addItem("Fred", "12345"); model.addItem("John", "2345"); model.addItem("Bob", "54321"); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); qApp->processEvents(); QQuickFlickable *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); // Force a layout, necessary if ListView is completed before VisualDataModel. if (forceLayout) QCOMPARE(listview->property("count").toInt(), model.count()); model.modifyItem(1, "Will", "9876"); QQuickText *name = findItem(contentItem, "textName", 1); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(1)); QQuickText *number = findItem(contentItem, "textNumber", 1); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(1)); delete canvas; delete testObject; } template void tst_QQuickListView::inserted(const QUrl &source) { QQuickView *canvas = createView(); canvas->show(); T model; model.addItem("Fred", "12345"); model.addItem("John", "2345"); model.addItem("Bob", "54321"); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); model.insertItem(1, "Will", "9876"); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item QQuickText *name = findItem(contentItem, "textName", 1); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(1)); QQuickText *number = findItem(contentItem, "textNumber", 1); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(1)); // Confirm items positioned correctly for (int i = 0; i < model.count(); ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QTRY_COMPARE(item->y(), i*20.0); } model.insertItem(0, "Foo", "1111"); // zero index, and current item QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item name = findItem(contentItem, "textName", 0); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(0)); number = findItem(contentItem, "textNumber", 0); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(0)); QTRY_COMPARE(listview->currentIndex(), 1); // Confirm items positioned correctly for (int i = 0; i < model.count(); ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QTRY_COMPARE(item->y(), i*20.0); } for (int i = model.count(); i < 30; ++i) model.insertItem(i, "Hello", QString::number(i)); listview->setContentY(80); // Insert item outside visible area model.insertItem(1, "Hello", "1324"); QTRY_VERIFY(listview->contentY() == 80); // Confirm items positioned correctly for (int i = 5; i < 5+15; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.0 - 20.0); } // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0); // QTBUG-19675 model.clear(); model.insertItem(0, "Hello", "1234"); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QQuickItem *item = findItem(contentItem, "wrapper", 0); QVERIFY(item); QCOMPARE(item->y(), 0.); QTRY_VERIFY(listview->contentY() == 0); delete canvas; delete testObject; } template void tst_QQuickListView::inserted_more() { QFETCH(qreal, contentY); QFETCH(int, insertIndex); QFETCH(int, insertCount); QFETCH(qreal, itemsOffsetAfterMove); T model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QQuickView *canvas = createView(); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); listview->setContentY(contentY); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QList > newData; for (int i=0; iproperty("count").toInt(), model.count()); // check visibleItems.first() is in correct position QQuickItem *item0 = findItem(contentItem, "wrapper", 0); QVERIFY(item0); QCOMPARE(item0->y(), itemsOffsetAfterMove); QList items = findItems(contentItem, "wrapper"); int firstVisibleIndex = -1; for (int i=0; iy() >= contentY) { QDeclarativeExpression e(qmlContext(items[i]), items[i], "index"); firstVisibleIndex = e.evaluate().toInt(); break; } } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // Confirm items positioned correctly and indexes correct int itemCount = findItems(contentItem, "wrapper").count(); QQuickText *name; QQuickText *number; for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove); name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); number = findItem(contentItem, "textNumber", i); QVERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(i)); } delete canvas; delete testObject; } void tst_QQuickListView::inserted_more_data() { QTest::addColumn("contentY"); QTest::addColumn("insertIndex"); QTest::addColumn("insertCount"); QTest::addColumn("itemsOffsetAfterMove"); QTest::newRow("add 1, before visible items") << 80.0 // show 4-19 << 3 << 1 << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move QTest::newRow("add multiple, before visible") << 80.0 // show 4-19 << 3 << 3 << -20.0 * 3; // again first visible should not move QTest::newRow("add 1, at start of visible, content at start") << 0.0 << 0 << 1 << 0.0; QTest::newRow("add multiple, start of visible, content at start") << 0.0 << 0 << 3 << 0.0; QTest::newRow("add 1, at start of visible, content not at start") << 80.0 // show 4-19 << 4 << 1 << 0.0; QTest::newRow("add multiple, at start of visible, content not at start") << 80.0 // show 4-19 << 4 << 3 << 0.0; QTest::newRow("add 1, at end of visible, content at start") << 0.0 << 15 << 1 << 0.0; QTest::newRow("add 1, at end of visible, content at start") << 0.0 << 15 << 3 << 0.0; QTest::newRow("add 1, at end of visible, content not at start") << 80.0 // show 4-19 << 19 << 1 << 0.0; QTest::newRow("add multiple, at end of visible, content not at start") << 80.0 // show 4-19 << 19 << 3 << 0.0; QTest::newRow("add 1, after visible, content at start") << 0.0 << 16 << 1 << 0.0; QTest::newRow("add 1, after visible, content at start") << 0.0 << 16 << 3 << 0.0; QTest::newRow("add 1, after visible, content not at start") << 80.0 // show 4-19 << 20 << 1 << 0.0; QTest::newRow("add multiple, after visible, content not at start") << 80.0 // show 4-19 << 20 << 3 << 0.0; } void tst_QQuickListView::insertBeforeVisible() { QFETCH(int, insertIndex); QFETCH(int, insertCount); QFETCH(int, cacheBuffer); QQuickText *name; QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); listview->setCacheBuffer(cacheBuffer); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // trigger a refill (not just setting contentY) so that the visibleItems grid is updated int firstVisibleIndex = 20; // move to an index where the top item is not visible listview->setContentY(firstVisibleIndex * 20.0); listview->setCurrentIndex(firstVisibleIndex); qApp->processEvents(); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_COMPARE(listview->currentIndex(), firstVisibleIndex); QQuickItem *item = findItem(contentItem, "wrapper", firstVisibleIndex); QVERIFY(item); QCOMPARE(item->y(), listview->contentY()); QList > newData; for (int i=0; iproperty("count").toInt(), model.count()); // now, moving to the top of the view should position the inserted items correctly int itemsOffsetAfterMove = -(insertCount * 20); listview->setCurrentIndex(0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_COMPARE(listview->currentIndex(), 0); QTRY_COMPARE(listview->contentY(), 0.0 + itemsOffsetAfterMove); // Confirm items positioned correctly and indexes correct int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove); name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } delete canvas; delete testObject; } void tst_QQuickListView::insertBeforeVisible_data() { QTest::addColumn("insertIndex"); QTest::addColumn("insertCount"); QTest::addColumn("cacheBuffer"); QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0; QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100; QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500; QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0; QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100; QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500; QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 3 << 0; QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 3 << 100; QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 3 << 500; QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 3 << 0; QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 3 << 100; QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 3 << 500; } template void tst_QQuickListView::removed(const QUrl &source, bool /* animated */) { QQuickView *canvas = createView(); T model; for (int i = 0; i < 50; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); model.removeItem(1); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QQuickText *name = findItem(contentItem, "textName", 1); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(1)); QQuickText *number = findItem(contentItem, "textNumber", 1); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(1)); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_VERIFY(item->y() == i*20); } // Remove first item (which is the current item); model.removeItem(0); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); name = findItem(contentItem, "textName", 0); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(0)); number = findItem(contentItem, "textNumber", 0); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(0)); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(),i*20.0); } // Remove items not visible model.removeItem(18); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(),i*20.0); } // Remove items before visible listview->setContentY(80); listview->setCurrentIndex(10); model.removeItem(1); // post: top item will be at 20 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); // Confirm items positioned correctly for (int i = 2; i < 18; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(),20+i*20.0); } // Remove current index QTRY_VERIFY(listview->currentIndex() == 9); QQuickItem *oldCurrent = listview->currentItem(); model.removeItem(9); QTRY_COMPARE(listview->currentIndex(), 9); QTRY_VERIFY(listview->currentItem() != oldCurrent); listview->setContentY(20); // That's the top now // let transitions settle. QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTest::qWait(300); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(),20+i*20.0); } // remove current item beyond visible items. listview->setCurrentIndex(20); listview->setContentY(40); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); model.removeItem(20); QTRY_COMPARE(listview->currentIndex(), 20); QTRY_VERIFY(listview->currentItem() != 0); // remove item before current, but visible listview->setCurrentIndex(8); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); oldCurrent = listview->currentItem(); model.removeItem(6); QTRY_COMPARE(listview->currentIndex(), 7); QTRY_VERIFY(listview->currentItem() == oldCurrent); listview->setContentY(80); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTest::qWait(300); // remove all visible items model.removeItems(1, 18); QTRY_COMPARE(listview->count() , model.count()); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount-1; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i+1); if (!item) qWarning() << "Item" << i+1 << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(),80+i*20.0); } model.removeItems(1, 17); QTRY_COMPARE(listview->count() , model.count()); model.removeItems(2, 1); QTRY_COMPARE(listview->count() , model.count()); model.addItem("New", "1"); QTRY_COMPARE(listview->count() , model.count()); QTRY_VERIFY(name = findItem(contentItem, "textName", model.count()-1)); QCOMPARE(name->text(), QString("New")); // Add some more items so that we don't run out model.clear(); for (int i = 0; i < 50; i++) model.addItem("Item" + QString::number(i), ""); // QTBUG-QTBUG-20575 listview->setCurrentIndex(0); listview->setContentY(30); model.removeItem(0); QTRY_VERIFY(name = findItem(contentItem, "textName", 0)); // QTBUG-19198 move to end and remove all visible items one at a time. listview->positionViewAtEnd(); for (int i = 0; i < 18; ++i) model.removeItems(model.count() - 1, 1); QTRY_VERIFY(findItems(contentItem, "wrapper").count() > 16); delete canvas; delete testObject; } template void tst_QQuickListView::removed_more(const QUrl &source) { QFETCH(qreal, contentY); QFETCH(int, removeIndex); QFETCH(int, removeCount); QFETCH(qreal, itemsOffsetAfterMove); QQuickText *name; QQuickText *number; QQuickView *canvas = createView(); T model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); listview->setContentY(contentY); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // wait for refill (after refill, items above the firstVisibleIndex-1 should not be rendered) int firstVisibleIndex = contentY / 20; if (firstVisibleIndex - 2 >= 0) QTRY_VERIFY(!findItem(contentItem, "textName", firstVisibleIndex - 2)); model.removeItems(removeIndex, removeCount); QTRY_COMPARE(listview->property("count").toInt(), model.count()); // check visibleItems.first() is in correct position QQuickItem *item0 = findItem(contentItem, "wrapper", 0); QVERIFY(item0); QCOMPARE(item0->y(), itemsOffsetAfterMove); QList items = findItems(contentItem, "wrapper"); for (int i=0; iy() >= contentY) { QDeclarativeExpression e(qmlContext(items[i]), items[i], "index"); firstVisibleIndex = e.evaluate().toInt(); break; } } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // Confirm items positioned correctly and indexes correct int itemCount = findItems(contentItem, "wrapper").count(); for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove); name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); number = findItem(contentItem, "textNumber", i); QVERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(i)); } delete canvas; delete testObject; } void tst_QQuickListView::removed_more_data() { QTest::addColumn("contentY"); QTest::addColumn("removeIndex"); QTest::addColumn("removeCount"); QTest::addColumn("itemsOffsetAfterMove"); QTest::newRow("remove 1, before visible items") << 80.0 // show 4-19 << 3 << 1 << 20.0; // visible items slide down by 1 item so that first visible does not move QTest::newRow("remove multiple, all before visible items") << 80.0 << 1 << 3 << 20.0 * 3; QTest::newRow("remove multiple, all before visible items, remove item 0") << 80.0 << 0 << 4 << 20.0 * 4; // remove 1,2,3 before the visible pos, 0 moves down to just before the visible pos, // items 4,5 are removed from view, item 6 slides up to original pos of item 4 (80px) QTest::newRow("remove multiple, mix of items from before and within visible items") << 80.0 << 1 << 5 << 20.0 * 3; // adjust for the 3 items removed before the visible QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0") << 80.0 << 0 << 6 << 20.0 * 4; // adjust for the 3 items removed before the visible QTest::newRow("remove 1, from start of visible, content at start") << 0.0 << 0 << 1 << 0.0; QTest::newRow("remove multiple, from start of visible, content at start") << 0.0 << 0 << 3 << 0.0; QTest::newRow("remove 1, from start of visible, content not at start") << 80.0 // show 4-19 << 4 << 1 << 0.0; QTest::newRow("remove multiple, from start of visible, content not at start") << 80.0 // show 4-19 << 4 << 3 << 0.0; QTest::newRow("remove 1, from middle of visible, content at start") << 0.0 << 10 << 1 << 0.0; QTest::newRow("remove multiple, from middle of visible, content at start") << 0.0 << 10 << 5 << 0.0; QTest::newRow("remove 1, from middle of visible, content not at start") << 80.0 // show 4-19 << 10 << 1 << 0.0; QTest::newRow("remove multiple, from middle of visible, content not at start") << 80.0 // show 4-19 << 10 << 5 << 0.0; QTest::newRow("remove 1, after visible, content at start") << 0.0 << 16 << 1 << 0.0; QTest::newRow("remove multiple, after visible, content at start") << 0.0 << 16 << 5 << 0.0; QTest::newRow("remove 1, after visible, content not at middle") << 80.0 // show 4-19 << 16+4 << 1 << 0.0; QTest::newRow("remove multiple, after visible, content not at start") << 80.0 // show 4-19 << 16+4 << 5 << 0.0; QTest::newRow("remove multiple, mix of items from within and after visible items") << 80.0 << 18 << 5 << 0.0; } template void tst_QQuickListView::clear(const QUrl &source) { QQuickView *canvas = createView(); T model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); model.clear(); QTRY_VERIFY(listview->count() == 0); QTRY_VERIFY(listview->currentItem() == 0); QTRY_VERIFY(listview->contentY() == 0); QVERIFY(listview->currentIndex() == -1); // confirm sanity when adding an item to cleared list model.addItem("New", "1"); QTRY_VERIFY(listview->count() == 1); QVERIFY(listview->currentItem() != 0); QVERIFY(listview->currentIndex() == 0); delete canvas; delete testObject; } template void tst_QQuickListView::moved(const QUrl &source) { QFETCH(qreal, contentY); QFETCH(int, from); QFETCH(int, to); QFETCH(int, count); QFETCH(qreal, itemsOffsetAfterMove); QQuickText *name; QQuickText *number; QQuickView *canvas = createView(); T model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(source); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *currentItem = listview->currentItem(); QTRY_VERIFY(currentItem != 0); if (contentY != 0) { listview->setContentY(contentY); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); } model.moveItems(from, to, count); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QList items = findItems(contentItem, "wrapper"); int firstVisibleIndex = -1; for (int i=0; iy() >= contentY) { QDeclarativeExpression e(qmlContext(items[i]), items[i], "index"); firstVisibleIndex = e.evaluate().toInt(); break; } } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // Confirm items positioned correctly and indexes correct int itemCount = findItems(contentItem, "wrapper").count(); for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) { if (i >= firstVisibleIndex + 16) // index has moved out of view continue; QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove); name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); number = findItem(contentItem, "textNumber", i); QVERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(i)); // current index should have been updated if (item == currentItem) QTRY_COMPARE(listview->currentIndex(), i); } delete canvas; delete testObject; } void tst_QQuickListView::moved_data() { QTest::addColumn("contentY"); QTest::addColumn("from"); QTest::addColumn("to"); QTest::addColumn("count"); QTest::addColumn("itemsOffsetAfterMove"); // model starts with 30 items, each 20px high, in area 320px high // 16 items should be visible at a time // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved QTest::newRow("move 1 forwards, within visible items") << 0.0 << 1 << 4 << 1 << 0.0; QTest::newRow("move 1 forwards, from non-visible -> visible") << 80.0 // show 4-19 << 1 << 18 << 1 << 20.0; // removed 1 item above the first visible, so item 0 should drop down by 1 to minimize movement QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)") << 80.0 // show 4-19 << 0 << 4 << 1 << 20.0; // first item has moved to below item4, everything drops down by size of 1 item QTest::newRow("move 1 forwards, from visible -> non-visible") << 0.0 << 1 << 16 << 1 << 0.0; QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)") << 0.0 << 0 << 16 << 1 << 0.0; QTest::newRow("move 1 backwards, within visible items") << 0.0 << 4 << 1 << 1 << 0.0; QTest::newRow("move 1 backwards, within visible items (to first index)") << 0.0 << 4 << 0 << 1 << 0.0; QTest::newRow("move 1 backwards, from non-visible -> visible") << 0.0 << 20 << 4 << 1 << 0.0; QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)") << 0.0 << 29 << 15 << 1 << 0.0; QTest::newRow("move 1 backwards, from visible -> non-visible") << 80.0 // show 4-19 << 16 << 1 << 1 << -20.0; // to minimize movement, item 0 moves to -20, and other items do not move QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)") << 80.0 // show 4-19 << 16 << 0 << 1 << -20.0; // to minimize movement, item 16 (now at 0) moves to -20, and other items do not move QTest::newRow("move multiple forwards, within visible items") << 0.0 << 0 << 5 << 3 << 0.0; QTest::newRow("move multiple forwards, before visible items") << 140.0 // show 7-22 << 4 << 5 << 3 // 4,5,6 move to below 7 << 20.0 * 3; // 4,5,6 moved down QTest::newRow("move multiple forwards, from non-visible -> visible") << 80.0 // show 4-19 << 1 << 5 << 3 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)") << 80.0 // show 4-19 << 0 << 5 << 3 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly QTest::newRow("move multiple forwards, mix of non-visible/visible") << 40.0 << 1 << 16 << 2 << 20.0; // item 1,2 are removed, item 3 is now first visible QTest::newRow("move multiple forwards, to bottom of view") << 0.0 << 5 << 13 << 3 << 0.0; QTest::newRow("move multiple forwards, to bottom of view, first->last") << 0.0 << 0 << 13 << 3 << 0.0; QTest::newRow("move multiple forwards, to bottom of view, content y not 0") << 80.0 << 5+4 << 13+4 << 3 << 0.0; QTest::newRow("move multiple forwards, from visible -> non-visible") << 0.0 << 1 << 16 << 3 << 0.0; QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)") << 0.0 << 0 << 16 << 3 << 0.0; QTest::newRow("move multiple backwards, within visible items") << 0.0 << 4 << 1 << 3 << 0.0; QTest::newRow("move multiple backwards, within visible items (move first item)") << 0.0 << 10 << 0 << 3 << 0.0; QTest::newRow("move multiple backwards, from non-visible -> visible") << 0.0 << 20 << 4 << 3 << 0.0; QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)") << 0.0 << 27 << 10 << 3 << 0.0; QTest::newRow("move multiple backwards, from visible -> non-visible") << 80.0 // show 4-19 << 16 << 1 << 3 << -20.0 * 3; // to minimize movement, 0 moves by -60, and other items do not move QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)") << 80.0 // show 4-19 << 16 << 0 << 3 << -20.0 * 3; // to minimize movement, 16,17,18 move to above item 0, and other items do not move } void tst_QQuickListView::multipleChanges() { QFETCH(int, startCount); QFETCH(QList, changes); QFETCH(int, newCount); QFETCH(int, newCurrentIndex); QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < startCount; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i=0; i > items; for (int j=changes[i].index; jpolishScheduled, false); break; } case ListChange::Removed: model.removeItems(changes[i].index, changes[i].count); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); break; case ListChange::Moved: model.moveItems(changes[i].index, changes[i].to, changes[i].count); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); break; case ListChange::SetCurrent: listview->setCurrentIndex(changes[i].index); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); break; case ListChange::SetContentY: listview->setContentY(changes[i].pos); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); break; } } QTRY_COMPARE(listview->count(), newCount); QCOMPARE(listview->count(), model.count()); QTRY_COMPARE(listview->currentIndex(), newCurrentIndex); QQuickText *name; QQuickText *number; QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); int itemCount = findItems(contentItem, "wrapper").count(); for (int i=0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); number = findItem(contentItem, "textNumber", i); QVERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(i)); } delete testObject; delete canvas; } void tst_QQuickListView::multipleChanges_data() { QTest::addColumn("startCount"); QTest::addColumn >("changes"); QTest::addColumn("newCount"); QTest::addColumn("newCurrentIndex"); QList changes; for (int i=1; i<30; i++) changes << ListChange::remove(0); QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0; changes << ListChange::remove(0); QTest::newRow("remove all") << 30 << changes << 0 << -1; changes.clear(); changes << ListChange::setCurrent(29); for (int i=29; i>0; i--) changes << ListChange::remove(i); QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0; QTest::newRow("remove then insert at 0") << 10 << (QList() << ListChange::remove(0, 1) << ListChange::insert(0, 1) ) << 10 << 1; QTest::newRow("remove then insert at non-zero index") << 10 << (QList() << ListChange::setCurrent(2) << ListChange::remove(2, 1) << ListChange::insert(2, 1) ) << 10 << 3; QTest::newRow("remove current then insert below it") << 10 << (QList() << ListChange::setCurrent(1) << ListChange::remove(1, 3) << ListChange::insert(2, 2) ) << 9 << 1; QTest::newRow("remove current index then move it down") << 10 << (QList() << ListChange::setCurrent(2) << ListChange::remove(1, 3) << ListChange::move(1, 5, 1) ) << 7 << 5; QTest::newRow("remove current index then move it up") << 10 << (QList() << ListChange::setCurrent(5) << ListChange::remove(4, 3) << ListChange::move(4, 1, 1) ) << 7 << 1; QTest::newRow("insert multiple times") << 0 << (QList() << ListChange::insert(0, 2) << ListChange::insert(0, 4) << ListChange::insert(0, 6) ) << 12 << 10; QTest::newRow("insert multiple times with current index changes") << 0 << (QList() << ListChange::insert(0, 2) << ListChange::insert(0, 4) << ListChange::insert(0, 6) << ListChange::setCurrent(3) << ListChange::insert(3, 2) ) << 14 << 5; QTest::newRow("insert and remove all") << 0 << (QList() << ListChange::insert(0, 30) << ListChange::remove(0, 30) ) << 0 << -1; QTest::newRow("insert and remove current") << 30 << (QList() << ListChange::insert(1) << ListChange::setCurrent(1) << ListChange::remove(1) ) << 30 << 1; QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList() << ListChange::insert(0, 10) << ListChange::remove(5, 10) ) << 10 << 5; QTest::newRow("insert multiple, then move new items to end") << 10 << (QList() << ListChange::insert(0, 3) << ListChange::move(0, 10, 3) ) << 13 << 0; QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList() << ListChange::insert(0, 3) << ListChange::move(0, 8, 5) ) << 13 << 11; QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList() << ListChange::setCurrent(9) << ListChange::insert(10, 3) << ListChange::move(8, 0, 5) ) << 13 << 1; QTest::newRow("move back and forth to same index") << 10 << (QList() << ListChange::setCurrent(1) << ListChange::move(1, 2, 2) << ListChange::move(2, 1, 2) ) << 10 << 1; QTest::newRow("move forwards then back") << 10 << (QList() << ListChange::setCurrent(2) << ListChange::move(1, 2, 3) << ListChange::move(3, 0, 5) ) << 10 << 0; QTest::newRow("move current, then remove it") << 10 << (QList() << ListChange::setCurrent(5) << ListChange::move(5, 0, 1) << ListChange::remove(0) ) << 9 << 0; QTest::newRow("move current, then insert before it") << 10 << (QList() << ListChange::setCurrent(5) << ListChange::move(5, 0, 1) << ListChange::insert(0) ) << 11 << 1; QTest::newRow("move multiple, then remove them") << 10 << (QList() << ListChange::setCurrent(1) << ListChange::move(5, 1, 3) << ListChange::remove(1, 3) ) << 7 << 1; QTest::newRow("move multiple, then insert before them") << 10 << (QList() << ListChange::setCurrent(5) << ListChange::move(5, 1, 3) << ListChange::insert(1, 5) ) << 15 << 6; QTest::newRow("move multiple, then insert after them") << 10 << (QList() << ListChange::setCurrent(3) << ListChange::move(0, 1, 2) << ListChange::insert(3, 5) ) << 15 << 8; QTest::newRow("clear current") << 0 << (QList() << ListChange::insert(0, 5) << ListChange::setCurrent(-1) << ListChange::remove(0, 5) << ListChange::insert(0, 5) ) << 5 << -1; } void tst_QQuickListView::swapWithFirstItem() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // ensure content position is stable listview->setContentY(0); model.moveItem(1, 0); QTRY_VERIFY(listview->contentY() == 0); delete testObject; delete canvas; } void tst_QQuickListView::enforceRange() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("listview-enforcerange.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0); QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0); QTRY_COMPARE(listview->highlightRangeMode(), QQuickListView::StrictlyEnforceRange); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); // view should be positioned at the top of the range. QQuickItem *item = findItem(contentItem, "wrapper", 0); QTRY_VERIFY(item); QTRY_COMPARE(listview->contentY(), -100.0); QQuickText *name = findItem(contentItem, "textName", 0); QTRY_VERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(0)); QQuickText *number = findItem(contentItem, "textNumber", 0); QTRY_VERIFY(number != 0); QTRY_COMPARE(number->text(), model.number(0)); // Check currentIndex is updated when contentItem moves listview->setContentY(20); QTRY_COMPARE(listview->currentIndex(), 6); // change model QmlListModel model2; for (int i = 0; i < 5; i++) model2.addItem("Item" + QString::number(i), ""); ctxt->setContextProperty("testModel", &model2); QCOMPARE(listview->count(), 5); delete canvas; } void tst_QQuickListView::enforceRange_withoutHighlight() { // QTBUG-20287 // If no highlight is set but StrictlyEnforceRange is used, the content should still move // to the correct position (i.e. to the next/previous item, not next/previous section) // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex() QQuickView *canvas = createView(); QmlListModel model; model.addItem("Item 0", "a"); model.addItem("Item 1", "b"); model.addItem("Item 2", "b"); model.addItem("Item 3", "c"); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("listview-enforcerange-nohighlight.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); qreal expectedPos = -100.0; expectedPos += 10.0; // scroll past 1st section's delegate (10px height) QTRY_COMPARE(listview->contentY(), expectedPos); expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section QTest::keyClick(canvas, Qt::Key_Down); QTRY_COMPARE(listview->contentY(), expectedPos); expectedPos += 20; // scroll past 1st item of 2nd section QTest::keyClick(canvas, Qt::Key_Down); QTRY_COMPARE(listview->contentY(), expectedPos); expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section QTest::keyClick(canvas, Qt::Key_Down); QTRY_COMPARE(listview->contentY(), expectedPos); delete canvas; } void tst_QQuickListView::spacing() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_VERIFY(item->y() == i*20); } listview->setSpacing(10); QTRY_VERIFY(listview->spacing() == 10); // Confirm items positioned correctly QTRY_VERIFY(findItems(contentItem, "wrapper").count() == 11); for (int i = 0; i < 11; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_VERIFY(item->y() == i*30); } listview->setSpacing(0); // Confirm items positioned correctly QTRY_VERIFY(findItems(contentItem, "wrapper").count() >= 16); for (int i = 0; i < 16; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.0); } delete canvas; delete testObject; } template void tst_QQuickListView::sections(const QUrl &source) { QQuickView *canvas = createView(); T model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), QString::number(i/5)); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(source); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QTRY_VERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20)); QQuickText *next = findItem(item, "nextSection"); QCOMPARE(next->text().toInt(), (i+1)/5); } QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged())); // Remove section boundary model.removeItem(5); QTRY_COMPARE(listview->count(), model.count()); // New section header created QQuickItem *item = findItem(contentItem, "wrapper", 5); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 40.0); model.insertItem(3, "New Item", "0"); QTRY_COMPARE(listview->count(), model.count()); // Section header moved item = findItem(contentItem, "wrapper", 5); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 20.0); item = findItem(contentItem, "wrapper", 6); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 40.0); // insert item which will become a section header model.insertItem(6, "Replace header", "1"); QTRY_COMPARE(listview->count(), model.count()); item = findItem(contentItem, "wrapper", 6); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 40.0); item = findItem(contentItem, "wrapper", 7); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 20.0); QTRY_COMPARE(listview->currentSection(), QString("0")); listview->setContentY(140); QTRY_COMPARE(listview->currentSection(), QString("1")); QTRY_COMPARE(currentSectionChangedSpy.count(), 1); listview->setContentY(20); QTRY_COMPARE(listview->currentSection(), QString("0")); QTRY_COMPARE(currentSectionChangedSpy.count(), 2); item = findItem(contentItem, "wrapper", 1); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 20.0); // check that headers change when item changes listview->setContentY(0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); model.modifyItem(0, "changed", "2"); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); item = findItem(contentItem, "wrapper", 1); QTRY_VERIFY(item); QTRY_COMPARE(item->height(), 40.0); delete canvas; } void tst_QQuickListView::sectionsDelegate() { QSKIP("QTBUG-24395"); QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), QString::number(i/5)); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("listview-sections_delegate.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QTRY_VERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20)); QQuickText *next = findItem(item, "nextSection"); QCOMPARE(next->text().toInt(), (i+1)/5); } for (int i = 0; i < 3; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + QString::number(i)); QVERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20*6)); } // ensure section header is maintained in view listview->setCurrentIndex(20); QTRY_VERIFY(listview->contentY() >= 200.0); listview->setCurrentIndex(0); QTRY_COMPARE(listview->contentY(), 0.0); // change section model.modifyItem(0, "One", "aaa"); model.modifyItem(1, "Two", "aaa"); model.modifyItem(2, "Three", "aaa"); model.modifyItem(3, "Four", "aaa"); model.modifyItem(4, "Five", "aaa"); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i = 0; i < 3; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + (i == 0 ? QString("aaa") : QString::number(i))); QVERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20*6)); } // remove section boundary model.removeItem(5); QTRY_COMPARE(listview->count(), model.count()); for (int i = 0; i < 3; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + (i == 0 ? QString("aaa") : QString::number(i))); QVERIFY(item); } // QTBUG-17606 QList items = findItems(contentItem, "sect_1"); QCOMPARE(items.count(), 1); // QTBUG-17759 model.modifyItem(0, "One", "aaa"); model.modifyItem(1, "One", "aaa"); model.modifyItem(2, "One", "aaa"); model.modifyItem(3, "Four", "aaa"); model.modifyItem(4, "Four", "aaa"); model.modifyItem(5, "Four", "aaa"); model.modifyItem(6, "Five", "aaa"); model.modifyItem(7, "Five", "aaa"); model.modifyItem(8, "Five", "aaa"); model.modifyItem(9, "Two", "aaa"); model.modifyItem(10, "Two", "aaa"); model.modifyItem(11, "Two", "aaa"); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_COMPARE(findItems(contentItem, "sect_aaa").count(), 1); canvas->rootObject()->setProperty("sectionProperty", "name"); // ensure view has settled. QTRY_COMPARE(findItems(contentItem, "sect_Four").count(), 1); for (int i = 0; i < 4; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + model.name(i*3)); QVERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20*4)); } // QTBUG-17769 model.removeItems(10, 20); // ensure view has settled. QTRY_COMPARE(findItems(contentItem, "wrapper").count(), 10); // Drag view up beyond bounds QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20)); { QMouseEvent mv(QEvent::MouseMove, QPoint(20,0), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier); QGuiApplication::sendEvent(canvas, &mv); } { QMouseEvent mv(QEvent::MouseMove, QPoint(20,-50), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier); QGuiApplication::sendEvent(canvas, &mv); } { QMouseEvent mv(QEvent::MouseMove, QPoint(20,-200), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier); QGuiApplication::sendEvent(canvas, &mv); } QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-200)); // view should settle back at 0 QTRY_COMPARE(listview->contentY(), 0.0); delete canvas; } void tst_QQuickListView::sectionsPositioning() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), QString::number(i/5)); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("listview-sections_delegate.qml")); canvas->show(); qApp->processEvents(); canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart | QQuickViewSection::NextLabelAtEnd))); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i = 0; i < 3; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + QString::number(i)); QVERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20*6)); } QQuickItem *topItem = findVisibleChild(contentItem, "sect_0"); // section header QVERIFY(topItem); QCOMPARE(topItem->y(), 0.); QQuickItem *bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer QVERIFY(bottomItem); QCOMPARE(bottomItem->y(), 300.); // move down a little and check that section header is at top listview->setContentY(10); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QCOMPARE(topItem->y(), 0.); // push the top header up listview->setContentY(110); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); topItem = findVisibleChild(contentItem, "sect_0"); // section header QVERIFY(topItem); QCOMPARE(topItem->y(), 100.); QQuickItem *item = findVisibleChild(contentItem, "sect_1"); QVERIFY(item); QCOMPARE(item->y(), 120.); bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer QVERIFY(bottomItem); QCOMPARE(bottomItem->y(), 410.); // Move past section 0 listview->setContentY(120); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); topItem = findVisibleChild(contentItem, "sect_0"); // section header QVERIFY(!topItem); // Push section footer down listview->setContentY(70); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); bottomItem = findVisibleChild(contentItem, "sect_4"); // section footer QVERIFY(bottomItem); QCOMPARE(bottomItem->y(), 380.); // Change current section listview->setContentY(10); model.modifyItem(0, "One", "aaa"); model.modifyItem(1, "Two", "aaa"); model.modifyItem(2, "Three", "aaa"); model.modifyItem(3, "Four", "aaa"); model.modifyItem(4, "Five", "aaa"); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_COMPARE(listview->currentSection(), QString("aaa")); for (int i = 0; i < 3; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + (i == 0 ? QString("aaa") : QString::number(i))); QVERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20*6)); } QTRY_VERIFY(topItem = findVisibleChild(contentItem, "sect_aaa")); // section header QCOMPARE(topItem->y(), 10.); // remove section boundary listview->setContentY(120); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); model.removeItem(5); QTRY_COMPARE(listview->count(), model.count()); for (int i = 0; i < 3; ++i) { QQuickItem *item = findItem(contentItem, "sect_" + (i == 0 ? QString("aaa") : QString::number(i))); QVERIFY(item); QTRY_COMPARE(item->y(), qreal(i*20*6)); } QVERIFY(topItem = findVisibleChild(contentItem, "sect_1")); QTRY_COMPARE(topItem->y(), 120.); // Change the next section listview->setContentY(0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); bottomItem = findVisibleChild(contentItem, "sect_3"); // section footer QVERIFY(bottomItem); QTRY_COMPARE(bottomItem->y(), 300.); model.modifyItem(14, "New", "new"); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_VERIFY(bottomItem = findVisibleChild(contentItem, "sect_new")); // section footer QTRY_COMPARE(bottomItem->y(), 300.); // Turn sticky footer off listview->setContentY(20); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels | QQuickViewSection::CurrentLabelAtStart))); QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_new")); // inline label restored QCOMPARE(item->y(), 340.); // Turn sticky header off listview->setContentY(30); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); canvas->rootObject()->setProperty("sectionPositioning", QVariant(int(QQuickViewSection::InlineLabels))); QTRY_VERIFY(item = findVisibleChild(contentItem, "sect_aaa")); // inline label restored QCOMPARE(item->y(), 0.); delete canvas; } void tst_QQuickListView::currentIndex_delayedItemCreation() { QFETCH(bool, setCurrentToZero); QQuickView *canvas = createView(); // test currentIndexChanged() is emitted even if currentIndex = 0 on start up // (since the currentItem will have changed and that shares the same index) canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero); canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml")); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QSignalSpy spy(listview, SIGNAL(currentItemChanged())); QCOMPARE(listview->currentIndex(), 0); QTRY_COMPARE(spy.count(), 1); delete canvas; } void tst_QQuickListView::currentIndex_delayedItemCreation_data() { QTest::addColumn("setCurrentToZero"); QTest::newRow("set to 0") << true; QTest::newRow("don't set to 0") << false; } void tst_QQuickListView::currentIndex() { QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), QString::number(i)); QQuickView *canvas = new QQuickView(0); canvas->setGeometry(0,0,240,320); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("testWrap", QVariant(false)); QString filename(testFile("listview-initCurrent.qml")); canvas->setSource(QUrl::fromLocalFile(filename)); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // current item should be 20th item at startup // and current item should be in view QCOMPARE(listview->currentIndex(), 20); QCOMPARE(listview->contentY(), 100.0); QCOMPARE(listview->currentItem(), findItem(contentItem, "wrapper", 20)); QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y()); // no wrap listview->setCurrentIndex(0); QCOMPARE(listview->currentIndex(), 0); // confirm that the velocity is updated QTRY_VERIFY(listview->verticalVelocity() != 0.0); listview->incrementCurrentIndex(); QCOMPARE(listview->currentIndex(), 1); listview->decrementCurrentIndex(); QCOMPARE(listview->currentIndex(), 0); listview->decrementCurrentIndex(); QCOMPARE(listview->currentIndex(), 0); // with wrap ctxt->setContextProperty("testWrap", QVariant(true)); QVERIFY(listview->isWrapEnabled()); listview->decrementCurrentIndex(); QCOMPARE(listview->currentIndex(), model.count()-1); QTRY_COMPARE(listview->contentY(), 280.0); listview->incrementCurrentIndex(); QCOMPARE(listview->currentIndex(), 0); QTRY_COMPARE(listview->contentY(), 0.0); // footer should become visible if it is out of view, and then current index is set to count-1 canvas->rootObject()->setProperty("showFooter", true); QTRY_VERIFY(listview->footerItem()); listview->setCurrentIndex(model.count()-2); QTRY_VERIFY(listview->footerItem()->y() > listview->contentY() + listview->height()); listview->setCurrentIndex(model.count()-1); QTRY_COMPARE(listview->contentY() + listview->height(), (20.0 * model.count()) + listview->footerItem()->height()); canvas->rootObject()->setProperty("showFooter", false); // header should become visible if it is out of view, and then current index is set to 0 canvas->rootObject()->setProperty("showHeader", true); QTRY_VERIFY(listview->headerItem()); listview->setCurrentIndex(1); QTRY_VERIFY(listview->headerItem()->y() + listview->headerItem()->height() < listview->contentY()); listview->setCurrentIndex(0); QTRY_COMPARE(listview->contentY(), -listview->headerItem()->height()); canvas->rootObject()->setProperty("showHeader", false); // Test keys canvas->show(); canvas->requestActivateWindow(); QTest::qWaitForWindowShown(canvas); QTRY_VERIFY(qGuiApp->focusWindow() == canvas); listview->setCurrentIndex(0); QTest::keyClick(canvas, Qt::Key_Down); QCOMPARE(listview->currentIndex(), 1); QTest::keyClick(canvas, Qt::Key_Up); QCOMPARE(listview->currentIndex(), 0); // hold down Key_Down for (int i=0; icurrentIndex(), i+1); } QTest::keyRelease(canvas, Qt::Key_Down); QTRY_COMPARE(listview->currentIndex(), model.count()-1); QTRY_COMPARE(listview->contentY(), 280.0); // hold down Key_Up for (int i=model.count()-1; i > 0; i--) { QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true); QTRY_COMPARE(listview->currentIndex(), i-1); } QTest::keyRelease(canvas, Qt::Key_Up); QTRY_COMPARE(listview->currentIndex(), 0); QTRY_COMPARE(listview->contentY(), 0.0); // turn off auto highlight listview->setHighlightFollowsCurrentItem(false); QVERIFY(listview->highlightFollowsCurrentItem() == false); QVERIFY(listview->highlightItem()); qreal hlPos = listview->highlightItem()->y(); listview->setCurrentIndex(4); QTRY_COMPARE(listview->highlightItem()->y(), hlPos); // insert item before currentIndex listview->setCurrentIndex(28); model.insertItem(0, "Foo", "1111"); QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29); // check removing highlight by setting currentIndex to -1; listview->setCurrentIndex(-1); QCOMPARE(listview->currentIndex(), -1); QVERIFY(!listview->highlightItem()); QVERIFY(!listview->currentItem()); delete canvas; } void tst_QQuickListView::noCurrentIndex() { QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), QString::number(i)); QQuickView *canvas = new QQuickView(0); canvas->setGeometry(0,0,240,320); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); QString filename(testFile("listview-noCurrent.qml")); canvas->setSource(QUrl::fromLocalFile(filename)); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // current index should be -1 at startup // and we should not have a currentItem or highlightItem QCOMPARE(listview->currentIndex(), -1); QCOMPARE(listview->contentY(), 0.0); QVERIFY(!listview->highlightItem()); QVERIFY(!listview->currentItem()); listview->setCurrentIndex(2); QCOMPARE(listview->currentIndex(), 2); QVERIFY(listview->highlightItem()); QVERIFY(listview->currentItem()); delete canvas; } void tst_QQuickListView::itemList() { QQuickView *canvas = createView(); canvas->setSource(testFileUrl("itemlist.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "view"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickVisualItemModel *model = canvas->rootObject()->findChild("itemModel"); QTRY_VERIFY(model != 0); QTRY_VERIFY(model->count() == 3); QTRY_COMPARE(listview->currentIndex(), 0); QQuickItem *item = findItem(contentItem, "item1"); QTRY_VERIFY(item); QTRY_COMPARE(item->x(), 0.0); QCOMPARE(item->height(), listview->height()); QQuickText *text = findItem(contentItem, "text1"); QTRY_VERIFY(text); QTRY_COMPARE(text->text(), QLatin1String("index: 0")); listview->setCurrentIndex(2); item = findItem(contentItem, "item3"); QTRY_VERIFY(item); QTRY_COMPARE(item->x(), 480.0); text = findItem(contentItem, "text3"); QTRY_VERIFY(text); QTRY_COMPARE(text->text(), QLatin1String("index: 2")); delete canvas; } void tst_QQuickListView::cacheBuffer() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 90; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_VERIFY(listview->delegate() != 0); QTRY_VERIFY(listview->model() != 0); QTRY_VERIFY(listview->highlight() != 0); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_VERIFY(item->y() == i*20); } QDeclarativeIncubationController controller; canvas->engine()->setIncubationController(&controller); testObject->setCacheBuffer(200); QTRY_VERIFY(listview->cacheBuffer() == 200); // items will be created one at a time for (int i = itemCount; i < qMin(itemCount+10,model.count()); ++i) { QVERIFY(findItem(listview, "wrapper", i) == 0); QQuickItem *item = 0; while (!item) { bool b = false; controller.incubateWhile(&b); item = findItem(listview, "wrapper", i); } } { bool b = true; controller.incubateWhile(&b); } int newItemCount = 0; newItemCount = findItems(contentItem, "wrapper").count(); // Confirm items positioned correctly for (int i = 0; i < model.count() && i < newItemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_VERIFY(item->y() == i*20); } // move view and confirm items in view are visible immediately and outside are created async listview->setContentY(300); for (int i = 15; i < 32; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QVERIFY(item); QVERIFY(item->y() == i*20); } QVERIFY(findItem(listview, "wrapper", 32) == 0); // ensure buffered items are created for (int i = 32; i < qMin(41,model.count()); ++i) { QQuickItem *item = 0; while (!item) { qGuiApp->processEvents(); // allow refill to happen bool b = false; controller.incubateWhile(&b); item = findItem(listview, "wrapper", i); } } { bool b = true; controller.incubateWhile(&b); } delete canvas; delete testObject; } void tst_QQuickListView::positionViewAtIndex() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 40; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->show(); canvas->setSource(testFileUrl("listviewtest.qml")); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } // Position on a currently visible item listview->positionViewAtIndex(3, QQuickListView::Beginning); QTRY_COMPARE(listview->contentY(), 60.); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } // Position on an item beyond the visible items listview->positionViewAtIndex(22, QQuickListView::Beginning); QTRY_COMPARE(listview->contentY(), 440.); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } // Position on an item that would leave empty space if positioned at the top listview->positionViewAtIndex(28, QQuickListView::Beginning); QTRY_COMPARE(listview->contentY(), 480.); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } // Position at the beginning again listview->positionViewAtIndex(0, QQuickListView::Beginning); QTRY_COMPARE(listview->contentY(), 0.); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount-1; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } // Position at End using last index listview->positionViewAtIndex(model.count()-1, QQuickListView::End); QTRY_COMPARE(listview->contentY(), 480.); // Confirm items positioned correctly itemCount = findItems(contentItem, "wrapper").count(); for (int i = 24; i < model.count(); ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } // Position at End listview->positionViewAtIndex(20, QQuickListView::End); QTRY_COMPARE(listview->contentY(), 100.); // Position in Center listview->positionViewAtIndex(15, QQuickListView::Center); QTRY_COMPARE(listview->contentY(), 150.); // Ensure at least partially visible listview->positionViewAtIndex(15, QQuickListView::Visible); QTRY_COMPARE(listview->contentY(), 150.); listview->setContentY(302); listview->positionViewAtIndex(15, QQuickListView::Visible); QTRY_COMPARE(listview->contentY(), 302.); listview->setContentY(320); listview->positionViewAtIndex(15, QQuickListView::Visible); QTRY_COMPARE(listview->contentY(), 300.); listview->setContentY(85); listview->positionViewAtIndex(20, QQuickListView::Visible); QTRY_COMPARE(listview->contentY(), 85.); listview->setContentY(75); listview->positionViewAtIndex(20, QQuickListView::Visible); QTRY_COMPARE(listview->contentY(), 100.); // Ensure completely visible listview->setContentY(120); listview->positionViewAtIndex(20, QQuickListView::Contain); QTRY_COMPARE(listview->contentY(), 120.); listview->setContentY(302); listview->positionViewAtIndex(15, QQuickListView::Contain); QTRY_COMPARE(listview->contentY(), 300.); listview->setContentY(85); listview->positionViewAtIndex(20, QQuickListView::Contain); QTRY_COMPARE(listview->contentY(), 100.); // positionAtBeginnging listview->positionViewAtBeginning(); QTRY_COMPARE(listview->contentY(), 0.); listview->setContentY(80); canvas->rootObject()->setProperty("showHeader", true); listview->positionViewAtBeginning(); QTRY_COMPARE(listview->contentY(), -30.); // positionAtEnd listview->positionViewAtEnd(); QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320 listview->setContentY(80); canvas->rootObject()->setProperty("showFooter", true); listview->positionViewAtEnd(); QTRY_COMPARE(listview->contentY(), 510.); // set current item to outside visible view, position at beginning // and ensure highlight moves to current item listview->setCurrentIndex(1); listview->positionViewAtBeginning(); QTRY_COMPARE(listview->contentY(), -30.); QVERIFY(listview->highlightItem()); QCOMPARE(listview->highlightItem()->y(), 20.); delete canvas; delete testObject; } void tst_QQuickListView::resetModel() { QQuickView *canvas = createView(); QStringList strings; strings << "one" << "two" << "three"; QStringListModel model(strings); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("displaylist.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_COMPARE(listview->count(), model.rowCount()); for (int i = 0; i < model.rowCount(); ++i) { QQuickText *display = findItem(contentItem, "displayText", i); QTRY_VERIFY(display != 0); QTRY_COMPARE(display->text(), strings.at(i)); } strings.clear(); strings << "four" << "five" << "six" << "seven"; model.setStringList(strings); QTRY_COMPARE(listview->count(), model.rowCount()); for (int i = 0; i < model.rowCount(); ++i) { QQuickText *display = findItem(contentItem, "displayText", i); QTRY_VERIFY(display != 0); QTRY_COMPARE(display->text(), strings.at(i)); } delete canvas; } void tst_QQuickListView::propertyChanges() { QQuickView *canvas = createView(); QTRY_VERIFY(canvas); canvas->setSource(testFileUrl("propertychangestest.qml")); QQuickListView *listView = canvas->rootObject()->findChild("listView"); QTRY_VERIFY(listView); QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged())); QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged())); QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged())); QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged())); QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged())); QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged())); QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged())); QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true); QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0); QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0); QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::ApplyRange); QTRY_COMPARE(listView->isWrapEnabled(), true); QTRY_COMPARE(listView->cacheBuffer(), 10); QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapToItem); listView->setHighlightFollowsCurrentItem(false); listView->setPreferredHighlightBegin(1.0); listView->setPreferredHighlightEnd(1.0); listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange); listView->setWrapEnabled(false); listView->setCacheBuffer(3); listView->setSnapMode(QQuickListView::SnapOneItem); QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false); QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0); QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0); QTRY_COMPARE(listView->highlightRangeMode(), QQuickListView::StrictlyEnforceRange); QTRY_COMPARE(listView->isWrapEnabled(), false); QTRY_COMPARE(listView->cacheBuffer(), 3); QTRY_COMPARE(listView->snapMode(), QQuickListView::SnapOneItem); QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1); QTRY_COMPARE(preferredHighlightBeginSpy.count(),1); QTRY_COMPARE(preferredHighlightEndSpy.count(),1); QTRY_COMPARE(highlightRangeModeSpy.count(),1); QTRY_COMPARE(keyNavigationWrapsSpy.count(),1); QTRY_COMPARE(cacheBufferSpy.count(),1); QTRY_COMPARE(snapModeSpy.count(),1); listView->setHighlightFollowsCurrentItem(false); listView->setPreferredHighlightBegin(1.0); listView->setPreferredHighlightEnd(1.0); listView->setHighlightRangeMode(QQuickListView::StrictlyEnforceRange); listView->setWrapEnabled(false); listView->setCacheBuffer(3); listView->setSnapMode(QQuickListView::SnapOneItem); QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1); QTRY_COMPARE(preferredHighlightBeginSpy.count(),1); QTRY_COMPARE(preferredHighlightEndSpy.count(),1); QTRY_COMPARE(highlightRangeModeSpy.count(),1); QTRY_COMPARE(keyNavigationWrapsSpy.count(),1); QTRY_COMPARE(cacheBufferSpy.count(),1); QTRY_COMPARE(snapModeSpy.count(),1); delete canvas; } void tst_QQuickListView::componentChanges() { QQuickView *canvas = createView(); QTRY_VERIFY(canvas); canvas->setSource(testFileUrl("propertychangestest.qml")); QQuickListView *listView = canvas->rootObject()->findChild("listView"); QTRY_VERIFY(listView); QDeclarativeComponent component(canvas->engine()); component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile("")); QDeclarativeComponent delegateComponent(canvas->engine()); delegateComponent.setData("import QtQuick 2.0; Text { text: 'Name: ' + name }", QUrl::fromLocalFile("")); QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged())); QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged())); QSignalSpy headerSpy(listView, SIGNAL(headerChanged())); QSignalSpy footerSpy(listView, SIGNAL(footerChanged())); listView->setHighlight(&component); listView->setHeader(&component); listView->setFooter(&component); listView->setDelegate(&delegateComponent); QTRY_COMPARE(listView->highlight(), &component); QTRY_COMPARE(listView->header(), &component); QTRY_COMPARE(listView->footer(), &component); QTRY_COMPARE(listView->delegate(), &delegateComponent); QTRY_COMPARE(highlightSpy.count(),1); QTRY_COMPARE(delegateSpy.count(),1); QTRY_COMPARE(headerSpy.count(),1); QTRY_COMPARE(footerSpy.count(),1); listView->setHighlight(&component); listView->setHeader(&component); listView->setFooter(&component); listView->setDelegate(&delegateComponent); QTRY_COMPARE(highlightSpy.count(),1); QTRY_COMPARE(delegateSpy.count(),1); QTRY_COMPARE(headerSpy.count(),1); QTRY_COMPARE(footerSpy.count(),1); delete canvas; } void tst_QQuickListView::modelChanges() { QQuickView *canvas = createView(); QTRY_VERIFY(canvas); canvas->setSource(testFileUrl("propertychangestest.qml")); QQuickListView *listView = canvas->rootObject()->findChild("listView"); QTRY_VERIFY(listView); QDeclarativeListModel *alternateModel = canvas->rootObject()->findChild("alternateModel"); QTRY_VERIFY(alternateModel); QVariant modelVariant = QVariant::fromValue(alternateModel); QSignalSpy modelSpy(listView, SIGNAL(modelChanged())); listView->setModel(modelVariant); QTRY_COMPARE(listView->model(), modelVariant); QTRY_COMPARE(modelSpy.count(),1); listView->setModel(modelVariant); QTRY_COMPARE(modelSpy.count(),1); listView->setModel(QVariant()); QTRY_COMPARE(modelSpy.count(),2); delete canvas; } void tst_QQuickListView::QTBUG_9791() { QQuickView *canvas = createView(); canvas->setSource(testFileUrl("strictlyenforcerange.qml")); qApp->processEvents(); QQuickListView *listview = qobject_cast(canvas->rootObject()); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_VERIFY(listview->delegate() != 0); QTRY_VERIFY(listview->model() != 0); QMetaObject::invokeMethod(listview, "fillModel"); qApp->processEvents(); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper", false).count(); QCOMPARE(itemCount, 3); for (int i = 0; i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->x(), i*300.0); } // check that view is positioned correctly QTRY_COMPARE(listview->contentX(), 590.0); delete canvas; } void tst_QQuickListView::manualHighlight() { QQuickView *canvas = new QQuickView(0); canvas->setGeometry(0,0,240,320); QString filename(testFile("manual-highlight.qml")); canvas->setSource(QUrl::fromLocalFile(filename)); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(listview->currentIndex(), 0); QTRY_COMPARE(listview->currentItem(), findItem(contentItem, "wrapper", 0)); QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y()); listview->setCurrentIndex(2); QTRY_COMPARE(listview->currentIndex(), 2); QTRY_COMPARE(listview->currentItem(), findItem(contentItem, "wrapper", 2)); QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y()); // QTBUG-15972 listview->positionViewAtIndex(3, QQuickListView::Contain); QTRY_COMPARE(listview->currentIndex(), 2); QTRY_COMPARE(listview->currentItem(), findItem(contentItem, "wrapper", 2)); QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y()); delete canvas; } void tst_QQuickListView::QTBUG_11105() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_VERIFY(item->y() == i*20); } listview->positionViewAtIndex(20, QQuickListView::Beginning); QCOMPARE(listview->contentY(), 280.); QmlListModel model2; for (int i = 0; i < 5; i++) model2.addItem("Item" + QString::number(i), ""); ctxt->setContextProperty("testModel", &model2); itemCount = findItems(contentItem, "wrapper").count(); QCOMPARE(itemCount, 5); delete canvas; delete testObject; } void tst_QQuickListView::header() { QFETCH(QQuickListView::Orientation, orientation); QFETCH(Qt::LayoutDirection, layoutDirection); QFETCH(QPointF, initialHeaderPos); QFETCH(QPointF, firstDelegatePos); QFETCH(QPointF, initialContentPos); QFETCH(QPointF, changedHeaderPos); QFETCH(QPointF, changedContentPos); QFETCH(QPointF, resizeContentPos); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QQuickView *canvas = createView(); canvas->rootContext()->setContextProperty("testModel", &model); canvas->rootContext()->setContextProperty("initialViewWidth", 240); canvas->rootContext()->setContextProperty("initialViewHeight", 320); canvas->setSource(testFileUrl("header.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); listview->setOrientation(orientation); listview->setLayoutDirection(layoutDirection); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickText *header = 0; QTRY_VERIFY(header = findItem(contentItem, "header")); QVERIFY(header == listview->headerItem()); QCOMPARE(header->width(), 100.); QCOMPARE(header->height(), 30.); QCOMPARE(header->pos(), initialHeaderPos); QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos); QQuickItem *item = findItem(contentItem, "wrapper", 0); QVERIFY(item); QCOMPARE(item->pos(), firstDelegatePos); model.clear(); QTRY_COMPARE(listview->count(), model.count()); QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged())); QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader"); QCOMPARE(headerItemSpy.count(), 1); header = findItem(contentItem, "header"); QVERIFY(!header); header = findItem(contentItem, "header2"); QVERIFY(header); QVERIFY(header == listview->headerItem()); QCOMPARE(header->pos(), changedHeaderPos); QCOMPARE(header->width(), 50.); QCOMPARE(header->height(), 20.); QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos); item = findItem(contentItem, "wrapper", 0); QVERIFY(item); QCOMPARE(item->pos(), firstDelegatePos); delete canvas; // QTBUG-21207 header should become visible if view resizes from initial empty size canvas = createView(); canvas->rootContext()->setContextProperty("testModel", &model); canvas->rootContext()->setContextProperty("initialViewWidth", 0.0); canvas->rootContext()->setContextProperty("initialViewHeight", 0.0); canvas->setSource(testFileUrl("header.qml")); canvas->show(); qApp->processEvents(); listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); listview->setOrientation(orientation); listview->setLayoutDirection(layoutDirection); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); listview->setWidth(240); listview->setHeight(320); QTRY_COMPARE(listview->headerItem()->pos(), initialHeaderPos); QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos); delete canvas; } void tst_QQuickListView::header_data() { QTest::addColumn("orientation"); QTest::addColumn("layoutDirection"); QTest::addColumn("initialHeaderPos"); QTest::addColumn("changedHeaderPos"); QTest::addColumn("initialContentPos"); QTest::addColumn("changedContentPos"); QTest::addColumn("firstDelegatePos"); QTest::addColumn("resizeContentPos"); // header1 = 100 x 30 // header2 = 50 x 20 // delegates = 240 x 20 // view width = 240 // header above items, top left QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << QPointF(0, -30) << QPointF(0, -20) << QPointF(0, -30) << QPointF(0, -20) << QPointF(0, 0) << QPointF(0, -10); // header above items, top right QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QPointF(0, -30) << QPointF(0, -20) << QPointF(0, -30) << QPointF(0, -20) << QPointF(0, 0) << QPointF(0, -10); // header to left of items QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QPointF(-100, 0) << QPointF(-50, 0) << QPointF(-100, 0) << QPointF(-50, 0) << QPointF(0, 0) << QPointF(-40, 0); // header to right of items QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QPointF(0, 0) << QPointF(0, 0) << QPointF(-240 + 100, 0) << QPointF(-240 + 50, 0) << QPointF(-240, 0) << QPointF(-240 + 40, 0); } void tst_QQuickListView::header_delayItemCreation() { QQuickView *canvas = createView(); QmlListModel model; canvas->rootContext()->setContextProperty("setCurrentToZero", QVariant(false)); canvas->setSource(testFileUrl("fillModelOnComponentCompleted.qml")); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickText *header = findItem(contentItem, "header"); QVERIFY(header); QCOMPARE(header->y(), -header->height()); QCOMPARE(listview->contentY(), -header->height()); model.clear(); QTRY_COMPARE(header->y(), -header->height()); delete canvas; } void tst_QQuickListView::footer() { QFETCH(QQuickListView::Orientation, orientation); QFETCH(Qt::LayoutDirection, layoutDirection); QFETCH(QPointF, initialFooterPos); QFETCH(QPointF, firstDelegatePos); QFETCH(QPointF, initialContentPos); QFETCH(QPointF, changedFooterPos); QFETCH(QPointF, changedContentPos); QFETCH(QPointF, resizeContentPos); QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 3; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("footer.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); listview->setOrientation(orientation); listview->setLayoutDirection(layoutDirection); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickText *footer = findItem(contentItem, "footer"); QVERIFY(footer); QVERIFY(footer == listview->footerItem()); QCOMPARE(footer->pos(), initialFooterPos); QCOMPARE(footer->width(), 100.); QCOMPARE(footer->height(), 30.); QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos); QQuickItem *item = findItem(contentItem, "wrapper", 0); QVERIFY(item); QCOMPARE(item->pos(), firstDelegatePos); // remove one item model.removeItem(1); if (orientation == QQuickListView::Vertical) { QTRY_COMPARE(footer->y(), initialFooterPos.y() - 20); // delegate height = 20 } else { QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ? initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40 } // remove all items model.clear(); QPointF posWhenNoItems(0, 0); if (orientation == QQuickListView::Horizontal && layoutDirection == Qt::RightToLeft) posWhenNoItems.setX(-100); QTRY_COMPARE(footer->pos(), posWhenNoItems); // if header is present, it's at a negative pos, so the footer should not move canvas->rootObject()->setProperty("showHeader", true); QTRY_COMPARE(footer->pos(), posWhenNoItems); canvas->rootObject()->setProperty("showHeader", false); // add 30 items for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged())); QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter"); QCOMPARE(footerItemSpy.count(), 1); footer = findItem(contentItem, "footer"); QVERIFY(!footer); footer = findItem(contentItem, "footer2"); QVERIFY(footer); QVERIFY(footer == listview->footerItem()); QCOMPARE(footer->pos(), changedFooterPos); QCOMPARE(footer->width(), 50.); QCOMPARE(footer->height(), 20.); QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos); item = findItem(contentItem, "wrapper", 0); QVERIFY(item); QCOMPARE(item->pos(), firstDelegatePos); listview->positionViewAtEnd(); footer->setHeight(10); footer->setWidth(40); QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), resizeContentPos); delete canvas; } void tst_QQuickListView::footer_data() { QTest::addColumn("orientation"); QTest::addColumn("layoutDirection"); QTest::addColumn("initialFooterPos"); QTest::addColumn("changedFooterPos"); QTest::addColumn("initialContentPos"); QTest::addColumn("changedContentPos"); QTest::addColumn("firstDelegatePos"); QTest::addColumn("resizeContentPos"); // footer1 = 100 x 30 // footer2 = 50 x 20 // delegates = 40 x 20 // view width = 240 // view height = 320 // footer below items, bottom left QTest::newRow("vertical, layout left to right") << QQuickListView::Vertical << Qt::LeftToRight << QPointF(0, 3 * 20) << QPointF(0, 30 * 20) // added 30 items << QPointF(0, 0) << QPointF(0, 0) << QPointF(0, 0) << QPointF(0, 30 * 20 - 320 + 10); // footer below items, bottom right QTest::newRow("vertical, layout right to left") << QQuickListView::Vertical << Qt::RightToLeft << QPointF(0, 3 * 20) << QPointF(0, 30 * 20) << QPointF(0, 0) << QPointF(0, 0) << QPointF(0, 0) << QPointF(0, 30 * 20 - 320 + 10); // footer to right of items QTest::newRow("horizontal, layout left to right") << QQuickListView::Horizontal << Qt::LeftToRight << QPointF(40 * 3, 0) << QPointF(40 * 30, 0) << QPointF(0, 0) << QPointF(0, 0) << QPointF(0, 0) << QPointF(40 * 30 - 240 + 40, 0); // footer to left of items QTest::newRow("horizontal, layout right to left") << QQuickListView::Horizontal << Qt::RightToLeft << QPointF(-(40 * 3) - 100, 0) << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width << QPointF(-240, 0) << QPointF(-240, 0) << QPointF(-40, 0) << QPointF(-(40 * 30) - 40, 0); } class LVAccessor : public QQuickListView { public: qreal minY() const { return minYExtent(); } qreal maxY() const { return maxYExtent(); } qreal minX() const { return minXExtent(); } qreal maxX() const { return maxXExtent(); } }; void tst_QQuickListView::headerFooter() { { // Vertical QQuickView *canvas = createView(); QmlListModel model; QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("headerfooter.qml")); qApp->processEvents(); QQuickListView *listview = qobject_cast(canvas->rootObject()); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickItem *header = findItem(contentItem, "header"); QVERIFY(header); QCOMPARE(header->y(), -header->height()); QQuickItem *footer = findItem(contentItem, "footer"); QVERIFY(footer); QCOMPARE(footer->y(), 0.); QCOMPARE(static_cast(listview)->minY(), header->height()); QCOMPARE(static_cast(listview)->maxY(), header->height()); delete canvas; } { // Horizontal QQuickView *canvas = createView(); QmlListModel model; QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("headerfooter.qml")); canvas->rootObject()->setProperty("horizontal", true); qApp->processEvents(); QQuickListView *listview = qobject_cast(canvas->rootObject()); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickItem *header = findItem(contentItem, "header"); QVERIFY(header); QCOMPARE(header->x(), -header->width()); QQuickItem *footer = findItem(contentItem, "footer"); QVERIFY(footer); QCOMPARE(footer->x(), 0.); QCOMPARE(static_cast(listview)->minX(), header->width()); QCOMPARE(static_cast(listview)->maxX(), header->width()); delete canvas; } { // Horizontal RTL QQuickView *canvas = createView(); QmlListModel model; QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("headerfooter.qml")); canvas->rootObject()->setProperty("horizontal", true); canvas->rootObject()->setProperty("rtl", true); qApp->processEvents(); QQuickListView *listview = qobject_cast(canvas->rootObject()); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QQuickItem *header = findItem(contentItem, "header"); QVERIFY(header); QCOMPARE(header->x(), 0.); QQuickItem *footer = findItem(contentItem, "footer"); QVERIFY(footer); QCOMPARE(footer->x(), -footer->width()); QCOMPARE(static_cast(listview)->minX(), 240. - header->width()); QCOMPARE(static_cast(listview)->maxX(), 240. - header->width()); delete canvas; } } void tst_QQuickListView::resizeView() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 40; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); } QVariant heightRatio; QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio)); QCOMPARE(heightRatio.toReal(), 0.4); listview->setHeight(200); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio)); QCOMPARE(heightRatio.toReal(), 0.25); // Ensure we handle -ve sizes listview->setHeight(-100); QTRY_COMPARE(findItems(contentItem, "wrapper", false).count(), 1); listview->setCacheBuffer(200); QTRY_COMPARE(findItems(contentItem, "wrapper", false).count(), 11); // ensure items in cache become visible listview->setHeight(200); QTRY_COMPARE(findItems(contentItem, "wrapper", false).count(), 21); itemCount = findItems(contentItem, "wrapper", false).count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); QCOMPARE(item->isVisible(), i < 11); // inside view visible, outside not visible } // ensure items outside view become invisible listview->setHeight(100); QTRY_COMPARE(findItems(contentItem, "wrapper", false).count(), 16); itemCount = findItems(contentItem, "wrapper", false).count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*20.); QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible } delete canvas; delete testObject; } void tst_QQuickListView::resizeViewAndRepaint() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 40; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("initialHeight", 100); canvas->setSource(testFileUrl("resizeview.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // item at index 10 should not be currently visible QVERIFY(!findItem(contentItem, "wrapper", 10)); listview->setHeight(320); QTRY_VERIFY(findItem(contentItem, "wrapper", 10)); listview->setHeight(100); QTRY_VERIFY(!findItem(contentItem, "wrapper", 10)); delete canvas; } void tst_QQuickListView::sizeLessThan1() { QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("sizelessthan1.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // Confirm items positioned correctly int itemCount = findItems(contentItem, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); if (!item) qWarning() << "Item" << i << "not found"; QTRY_VERIFY(item); QTRY_COMPARE(item->y(), i*0.5); } delete canvas; delete testObject; } void tst_QQuickListView::QTBUG_14821() { QQuickView *canvas = createView(); canvas->setSource(testFileUrl("qtbug14821.qml")); qApp->processEvents(); QQuickListView *listview = qobject_cast(canvas->rootObject()); QVERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); listview->decrementCurrentIndex(); QCOMPARE(listview->currentIndex(), 99); listview->incrementCurrentIndex(); QCOMPARE(listview->currentIndex(), 0); delete canvas; } void tst_QQuickListView::resizeDelegate() { QQuickView *canvas = createView(); QStringList strings; for (int i = 0; i < 30; ++i) strings << QString::number(i); QStringListModel model(strings); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("displaylist.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QVERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QCOMPARE(listview->count(), model.rowCount()); listview->setCurrentIndex(25); listview->setContentY(0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i = 0; i < 16; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY(item != 0); QCOMPARE(item->y(), i*20.0); } QCOMPARE(listview->currentItem()->y(), 500.0); QTRY_COMPARE(listview->highlightItem()->y(), 500.0); canvas->rootObject()->setProperty("delegateHeight", 30); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i = 0; i < 11; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY(item != 0); QTRY_COMPARE(item->y(), i*30.0); } QTRY_COMPARE(listview->currentItem()->y(), 750.0); QTRY_COMPARE(listview->highlightItem()->y(), 750.0); listview->setCurrentIndex(1); listview->positionViewAtIndex(25, QQuickListView::Beginning); listview->positionViewAtIndex(5, QQuickListView::Beginning); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i = 5; i < 16; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY(item != 0); QCOMPARE(item->y(), i*30.0); } QTRY_COMPARE(listview->currentItem()->y(), 30.0); QTRY_COMPARE(listview->highlightItem()->y(), 30.0); canvas->rootObject()->setProperty("delegateHeight", 20); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); for (int i = 5; i < 11; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY(item != 0); QTRY_COMPARE(item->y(), 150 + (i-5)*20.0); } QTRY_COMPARE(listview->currentItem()->y(), 70.0); QTRY_COMPARE(listview->highlightItem()->y(), 70.0); delete canvas; } void tst_QQuickListView::resizeFirstDelegate() { // QTBUG-20712: Content Y jumps constantly if first delegate height == 0 // and other delegates have height > 0 QQuickView *canvas = createView(); // bug only occurs when all items in the model are visible QmlListModel model; for (int i = 0; i < 10; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QVERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *item = 0; for (int i = 0; i < model.count(); ++i) { item = findItem(contentItem, "wrapper", i); QVERIFY(item != 0); QCOMPARE(item->y(), i*20.0); } item = findItem(contentItem, "wrapper", 0); item->setHeight(0); // check the content y has not jumped up and down QCOMPARE(listview->contentY(), 0.0); QSignalSpy spy(listview, SIGNAL(contentYChanged())); QTest::qWait(100); QCOMPARE(spy.count(), 0); for (int i = 1; i < model.count(); ++i) { item = findItem(contentItem, "wrapper", i); QVERIFY(item != 0); QTRY_COMPARE(item->y(), (i-1)*20.0); } // QTBUG-22014: refill doesn't clear items scrolling off the top of the // list if they follow a zero-sized delegate for (int i = 0; i < 10; i++) model.addItem("Item" + QString::number(i), ""); QTRY_COMPARE(listview->count(), model.count()); item = findItem(contentItem, "wrapper", 1); QVERIFY(item); item->setHeight(0); listview->setCurrentIndex(19); qApp->processEvents(); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // items 0-2 should have been deleted for (int i=0; i<3; i++) { QTRY_VERIFY(!findItem(contentItem, "wrapper", i)); } delete testObject; delete canvas; } void tst_QQuickListView::QTBUG_16037() { QQuickView *canvas = createView(); canvas->show(); canvas->setSource(testFileUrl("qtbug16037.qml")); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "listview"); QTRY_VERIFY(listview != 0); QVERIFY(listview->contentHeight() <= 0.0); QMetaObject::invokeMethod(canvas->rootObject(), "setModel"); QTRY_COMPARE(listview->contentHeight(), 80.0); delete canvas; } void tst_QQuickListView::indexAt_itemAt_data() { QTest::addColumn("x"); QTest::addColumn("y"); QTest::addColumn("index"); QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0; QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0; QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0; QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1; QTest::newRow("No Item - 240, 20") << 240. << 20. << -1; } void tst_QQuickListView::indexAt_itemAt() { QFETCH(qreal, x); QFETCH(qreal, y); QFETCH(int, index); QQuickView *canvas = createView(); QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("listviewtest.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *item = 0; if (index >= 0) { item = findItem(contentItem, "wrapper", index); QVERIFY(item); } QCOMPARE(listview->indexAt(x,y), index); QVERIFY(listview->itemAt(x,y) == item); delete canvas; delete testObject; } void tst_QQuickListView::incrementalModel() { QQuickView *canvas = createView(); IncrementalModel model; QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("displaylist.qml")); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(listview->count(), 20); listview->positionViewAtIndex(10, QQuickListView::Beginning); QTRY_COMPARE(listview->count(), 25); delete canvas; } void tst_QQuickListView::onAdd() { QFETCH(int, initialItemCount); QFETCH(int, itemsToAdd); const int delegateHeight = 10; QaimModel model; // these initial items should not trigger ListView.onAdd for (int i=0; isetGeometry(0,0,200, delegateHeight * (initialItemCount + itemsToAdd)); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("delegateHeight", delegateHeight); canvas->setSource(testFileUrl("attachedSignals.qml")); QObject *object = canvas->rootObject(); object->setProperty("width", canvas->width()); object->setProperty("height", canvas->height()); qApp->processEvents(); QList > items; for (int i=0; irootObject()->property("count").toInt(), model.count()); QVariantList result = object->property("addedDelegates").toList(); QCOMPARE(result.count(), items.count()); for (int i=0; i("initialItemCount"); QTest::addColumn("itemsToAdd"); QTest::newRow("0, add 1") << 0 << 1; QTest::newRow("0, add 2") << 0 << 2; QTest::newRow("0, add 10") << 0 << 10; QTest::newRow("1, add 1") << 1 << 1; QTest::newRow("1, add 2") << 1 << 2; QTest::newRow("1, add 10") << 1 << 10; QTest::newRow("5, add 1") << 5 << 1; QTest::newRow("5, add 2") << 5 << 2; QTest::newRow("5, add 10") << 5 << 10; } void tst_QQuickListView::onRemove() { QFETCH(int, initialItemCount); QFETCH(int, indexToRemove); QFETCH(int, removeCount); const int delegateHeight = 10; QaimModel model; for (int i=0; irootContext(); ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("delegateHeight", delegateHeight); canvas->setSource(testFileUrl("attachedSignals.qml")); QObject *object = canvas->rootObject(); model.removeItems(indexToRemove, removeCount); QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount)); delete canvas; } void tst_QQuickListView::onRemove_data() { QTest::addColumn("initialItemCount"); QTest::addColumn("indexToRemove"); QTest::addColumn("removeCount"); QTest::newRow("remove first") << 1 << 0 << 1; QTest::newRow("two items, remove first") << 2 << 0 << 1; QTest::newRow("two items, remove last") << 2 << 1 << 1; QTest::newRow("two items, remove all") << 2 << 0 << 2; QTest::newRow("four items, remove first") << 4 << 0 << 1; QTest::newRow("four items, remove 0-2") << 4 << 0 << 2; QTest::newRow("four items, remove 1-3") << 4 << 1 << 2; QTest::newRow("four items, remove 2-4") << 4 << 2 << 2; QTest::newRow("four items, remove last") << 4 << 3 << 1; QTest::newRow("four items, remove all") << 4 << 0 << 4; QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8; QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5; QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6; } void tst_QQuickListView::rightToLeft() { QQuickView *canvas = createView(); canvas->setGeometry(0,0,640,320); canvas->setSource(testFileUrl("rightToLeft.qml")); canvas->show(); qApp->processEvents(); QVERIFY(canvas->rootObject() != 0); QQuickListView *listview = findItem(canvas->rootObject(), "view"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickVisualItemModel *model = canvas->rootObject()->findChild("itemModel"); QTRY_VERIFY(model != 0); QTRY_VERIFY(model->count() == 3); QTRY_COMPARE(listview->currentIndex(), 0); // initial position at first item, right edge aligned QCOMPARE(listview->contentX(), -640.); QQuickItem *item = findItem(contentItem, "item1"); QTRY_VERIFY(item); QTRY_COMPARE(item->x(), -100.0); QCOMPARE(item->height(), listview->height()); QQuickText *text = findItem(contentItem, "text1"); QTRY_VERIFY(text); QTRY_COMPARE(text->text(), QLatin1String("index: 0")); listview->setCurrentIndex(2); item = findItem(contentItem, "item3"); QTRY_VERIFY(item); QTRY_COMPARE(item->x(), -540.0); text = findItem(contentItem, "text3"); QTRY_VERIFY(text); QTRY_COMPARE(text->text(), QLatin1String("index: 2")); QCOMPARE(listview->contentX(), -640.); // Ensure resizing maintains position relative to right edge qobject_cast(canvas->rootObject())->setWidth(600); QTRY_COMPARE(listview->contentX(), -600.); delete canvas; } void tst_QQuickListView::test_mirroring() { QQuickView *canvasA = createView(); canvasA->setSource(testFileUrl("rightToLeft.qml")); QQuickListView *listviewA = findItem(canvasA->rootObject(), "view"); QTRY_VERIFY(listviewA != 0); QQuickView *canvasB = createView(); canvasB->setSource(testFileUrl("rightToLeft.qml")); QQuickListView *listviewB = findItem(canvasB->rootObject(), "view"); QTRY_VERIFY(listviewA != 0); qApp->processEvents(); QList objectNames; objectNames << "item1" << "item2"; // << "item3" listviewA->setProperty("layoutDirection", Qt::LeftToRight); listviewB->setProperty("layoutDirection", Qt::RightToLeft); QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection()); // LTR != RTL foreach (const QString objectName, objectNames) QVERIFY(findItem(listviewA, objectName)->x() != findItem(listviewB, objectName)->x()); listviewA->setProperty("layoutDirection", Qt::LeftToRight); listviewB->setProperty("layoutDirection", Qt::LeftToRight); // LTR == LTR foreach (const QString objectName, objectNames) QCOMPARE(findItem(listviewA, objectName)->x(), findItem(listviewB, objectName)->x()); QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection()); QQuickItemPrivate::get(listviewB)->setLayoutMirror(true); QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection()); // LTR != LTR+mirror foreach (const QString objectName, objectNames) QVERIFY(findItem(listviewA, objectName)->x() != findItem(listviewB, objectName)->x()); listviewA->setProperty("layoutDirection", Qt::RightToLeft); // RTL == LTR+mirror foreach (const QString objectName, objectNames) QCOMPARE(findItem(listviewA, objectName)->x(), findItem(listviewB, objectName)->x()); listviewB->setProperty("layoutDirection", Qt::RightToLeft); // RTL != RTL+mirror foreach (const QString objectName, objectNames) QVERIFY(findItem(listviewA, objectName)->x() != findItem(listviewB, objectName)->x()); listviewA->setProperty("layoutDirection", Qt::LeftToRight); // LTR == RTL+mirror foreach (const QString objectName, objectNames) QCOMPARE(findItem(listviewA, objectName)->x(), findItem(listviewB, objectName)->x()); delete canvasA; delete canvasB; } void tst_QQuickListView::margins() { QQuickView *canvas = createView(); QaimModel model; for (int i = 0; i < 50; i++) model.addItem("Item" + QString::number(i), ""); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); canvas->setSource(testFileUrl("margins.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QCOMPARE(listview->contentY(), -30.); QCOMPARE(listview->yOrigin(), 0.); // check end bound listview->positionViewAtEnd(); qreal pos = listview->contentY(); listview->setContentY(pos + 80); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); listview->returnToBounds(); QTRY_COMPARE(listview->contentY(), pos + 50); // remove item before visible and check that top margin is maintained // and yOrigin is updated listview->setContentY(100); model.removeItem(1); QTRY_COMPARE(listview->count(), model.count()); listview->setContentY(-50); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); listview->returnToBounds(); QCOMPARE(listview->yOrigin(), 20.); QTRY_COMPARE(listview->contentY(), -10.); // reduce top margin listview->setTopMargin(20); QCOMPARE(listview->yOrigin(), 20.); QTRY_COMPARE(listview->contentY(), 0.); // check end bound listview->positionViewAtEnd(); pos = listview->contentY(); listview->setContentY(pos + 80); listview->returnToBounds(); QTRY_COMPARE(listview->contentY(), pos + 50); // reduce bottom margin pos = listview->contentY(); listview->setBottomMargin(40); QCOMPARE(listview->yOrigin(), 20.); QTRY_COMPARE(listview->contentY(), pos-10); delete canvas; } // QTBUG-24028 void tst_QQuickListView::marginsResize() { QFETCH(QQuickListView::Orientation, orientation); QFETCH(Qt::LayoutDirection, layoutDirection); QFETCH(qreal, start); QFETCH(qreal, end); QQuickView *canvas = createView(); canvas->setSource(testFileUrl("margins2.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "listview"); QTRY_VERIFY(listview != 0); listview->setOrientation(orientation); listview->setLayoutDirection(layoutDirection); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); // view is resized after componentCompleted - top margin should still be visible if (orientation == QQuickListView::Vertical) QCOMPARE(listview->contentY(), start); else QCOMPARE(listview->contentX(), start); // move to last index and ensure bottom margin is visible. listview->setCurrentIndex(19); if (orientation == QQuickListView::Vertical) QTRY_COMPARE(listview->contentY(), end); else QTRY_COMPARE(listview->contentX(), end); // back to top - top margin should be visible. listview->setCurrentIndex(0); if (orientation == QQuickListView::Vertical) QTRY_COMPARE(listview->contentY(), start); else QTRY_COMPARE(listview->contentX(), start); delete canvas; } void tst_QQuickListView::marginsResize_data() { QTest::addColumn("orientation"); QTest::addColumn("layoutDirection"); QTest::addColumn("start"); QTest::addColumn("end"); QTest::newRow("vertical") << QQuickListView::Vertical << Qt::LeftToRight << -20.0 << 1020.0; QTest::newRow("horizontal") << QQuickListView::Horizontal << Qt::LeftToRight << -20.0 << 1020.0; QTest::newRow("horizontal, rtl") << QQuickListView::Horizontal << Qt::RightToLeft << -180.0 << -1220.0; } void tst_QQuickListView::snapToItem_data() { QTest::addColumn("orientation"); QTest::addColumn("layoutDirection"); QTest::addColumn("highlightRangeMode"); QTest::addColumn("flickStart"); QTest::addColumn("flickEnd"); QTest::addColumn("snapAlignment"); QTest::addColumn("endExtent"); QTest::addColumn("startExtent"); QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0; QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0; QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange) << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 << -240.0; QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0; QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0; QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange) << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 - 140.0 << -220.0; } void tst_QQuickListView::snapToItem() { QFETCH(QQuickListView::Orientation, orientation); QFETCH(Qt::LayoutDirection, layoutDirection); QFETCH(int, highlightRangeMode); QFETCH(QPoint, flickStart); QFETCH(QPoint, flickEnd); QFETCH(qreal, snapAlignment); QFETCH(qreal, endExtent); QFETCH(qreal, startExtent); QQuickView *canvas = createView(); canvas->setSource(testFileUrl("snapToItem.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); listview->setOrientation(orientation); listview->setLayoutDirection(layoutDirection); listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode)); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); // confirm that a flick hits an item boundary flick(canvas, flickStart, flickEnd, 180); QTRY_VERIFY(listview->isMoving() == false); // wait until it stops if (orientation == QQuickListView::Vertical) QCOMPARE(qreal(fmod(listview->contentY(),80.0)), snapAlignment); else QCOMPARE(qreal(fmod(listview->contentX(),80.0)), snapAlignment); // flick to end do { flick(canvas, flickStart, flickEnd, 180); QTRY_VERIFY(listview->isMoving() == false); // wait until it stops } while (orientation == QQuickListView::Vertical ? !listview->isAtYEnd() : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning()); if (orientation == QQuickListView::Vertical) QCOMPARE(listview->contentY(), endExtent); else QCOMPARE(listview->contentX(), endExtent); // flick to start do { flick(canvas, flickEnd, flickStart, 180); QTRY_VERIFY(listview->isMoving() == false); // wait until it stops } while (orientation == QQuickListView::Vertical ? !listview->isAtYBeginning() : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd()); if (orientation == QQuickListView::Vertical) QCOMPARE(listview->contentY(), startExtent); else QCOMPARE(listview->contentX(), startExtent); delete canvas; } void tst_QQuickListView::qListModelInterface_items() { items(testFileUrl("listviewtest.qml"), false); } void tst_QQuickListView::qListModelInterface_package_items() { items(testFileUrl("listviewtest-package.qml"), true); } void tst_QQuickListView::qAbstractItemModel_items() { items(testFileUrl("listviewtest.qml"), false); } void tst_QQuickListView::qListModelInterface_changed() { changed(testFileUrl("listviewtest.qml"), false); } void tst_QQuickListView::qListModelInterface_package_changed() { changed(testFileUrl("listviewtest-package.qml"), true); } void tst_QQuickListView::qAbstractItemModel_changed() { changed(testFileUrl("listviewtest.qml"), false); } void tst_QQuickListView::qListModelInterface_inserted() { inserted(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qListModelInterface_package_inserted() { inserted(testFileUrl("listviewtest-package.qml")); } void tst_QQuickListView::qListModelInterface_inserted_more() { inserted_more(); } void tst_QQuickListView::qListModelInterface_inserted_more_data() { inserted_more_data(); } void tst_QQuickListView::qAbstractItemModel_inserted() { inserted(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qAbstractItemModel_inserted_more() { inserted_more(); } void tst_QQuickListView::qAbstractItemModel_inserted_more_data() { inserted_more_data(); } void tst_QQuickListView::qListModelInterface_removed() { removed(testFileUrl("listviewtest.qml"), false); removed(testFileUrl("listviewtest.qml"), true); } void tst_QQuickListView::qListModelInterface_removed_more() { removed_more(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qListModelInterface_removed_more_data() { removed_more_data(); } void tst_QQuickListView::qListModelInterface_package_removed() { removed(testFileUrl("listviewtest-package.qml"), false); removed(testFileUrl("listviewtest-package.qml"), true); } void tst_QQuickListView::qAbstractItemModel_removed() { removed(testFileUrl("listviewtest.qml"), false); removed(testFileUrl("listviewtest.qml"), true); } void tst_QQuickListView::qAbstractItemModel_removed_more() { removed_more(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qAbstractItemModel_removed_more_data() { removed_more_data(); } void tst_QQuickListView::qListModelInterface_moved() { moved(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qListModelInterface_moved_data() { moved_data(); } void tst_QQuickListView::qListModelInterface_package_moved() { moved(testFileUrl("listviewtest-package.qml")); } void tst_QQuickListView::qListModelInterface_package_moved_data() { moved_data(); } void tst_QQuickListView::qAbstractItemModel_moved() { moved(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qAbstractItemModel_moved_data() { moved_data(); } void tst_QQuickListView::qListModelInterface_clear() { clear(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qListModelInterface_package_clear() { clear(testFileUrl("listviewtest-package.qml")); } void tst_QQuickListView::qAbstractItemModel_clear() { clear(testFileUrl("listviewtest.qml")); } void tst_QQuickListView::qListModelInterface_sections() { sections(testFileUrl("listview-sections.qml")); } void tst_QQuickListView::qListModelInterface_package_sections() { sections(testFileUrl("listview-sections-package.qml")); } void tst_QQuickListView::qAbstractItemModel_sections() { sections(testFileUrl("listview-sections.qml")); } void tst_QQuickListView::creationContext() { QQuickView canvas; canvas.setGeometry(0,0,240,320); canvas.setSource(testFileUrl("creationContext.qml")); qApp->processEvents(); QQuickItem *rootItem = qobject_cast(canvas.rootObject()); QVERIFY(rootItem); QVERIFY(rootItem->property("count").toInt() > 0); QQuickItem *item; QVERIFY(item = rootItem->findChild("listItem")); QCOMPARE(item->property("text").toString(), QString("Hello!")); QVERIFY(item = rootItem->findChild("header")); QCOMPARE(item->property("text").toString(), QString("Hello!")); QVERIFY(item = rootItem->findChild("footer")); QCOMPARE(item->property("text").toString(), QString("Hello!")); QVERIFY(item = rootItem->findChild("section")); QCOMPARE(item->property("text").toString(), QString("Hello!")); } void tst_QQuickListView::QTBUG_21742() { QQuickView canvas; canvas.setGeometry(0,0,200,200); canvas.setSource(testFileUrl("qtbug-21742.qml")); qApp->processEvents(); QQuickItem *rootItem = qobject_cast(canvas.rootObject()); QVERIFY(rootItem); QCOMPARE(rootItem->property("count").toInt(), 1); } void tst_QQuickListView::asynchronous() { QQuickView *canvas = createView(); canvas->show(); QDeclarativeIncubationController controller; canvas->engine()->setIncubationController(&controller); canvas->setSource(testFileUrl("asyncloader.qml")); QQuickItem *rootObject = qobject_cast(canvas->rootObject()); QVERIFY(rootObject); QQuickListView *listview = 0; while (!listview) { bool b = false; controller.incubateWhile(&b); listview = rootObject->findChild("view"); } // items will be created one at a time for (int i = 0; i < 8; ++i) { QVERIFY(findItem(listview, "wrapper", i) == 0); QQuickItem *item = 0; while (!item) { bool b = false; controller.incubateWhile(&b); item = findItem(listview, "wrapper", i); } } { bool b = true; controller.incubateWhile(&b); } // verify positioning QQuickItem *contentItem = listview->contentItem(); for (int i = 0; i < 8; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QTRY_COMPARE(item->y(), i*50.0); } delete canvas; } void tst_QQuickListView::snapOneItem_data() { QTest::addColumn("orientation"); QTest::addColumn("layoutDirection"); QTest::addColumn("highlightRangeMode"); QTest::addColumn("flickStart"); QTest::addColumn("flickEnd"); QTest::addColumn("snapAlignment"); QTest::addColumn("endExtent"); QTest::addColumn("startExtent"); QTest::newRow("vertical, left to right") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 560.0 << 0.0; QTest::newRow("horizontal, left to right") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange) << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 560.0 << 0.0; QTest::newRow("horizontal, right to left") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange) << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -560.0 - 240.0 << -240.0; QTest::newRow("vertical, left to right, enforce range") << QQuickListView::Vertical << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) << QPoint(20, 200) << QPoint(20, 20) << 180.0 << 580.0 << -20.0; QTest::newRow("horizontal, left to right, enforce range") << QQuickListView::Horizontal << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange) << QPoint(200, 20) << QPoint(20, 20) << 180.0 << 580.0 << -20.0; QTest::newRow("horizontal, right to left, enforce range") << QQuickListView::Horizontal << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange) << QPoint(20, 20) << QPoint(200, 20) << -420.0 << -580.0 - 240.0 << -220.0; } void tst_QQuickListView::snapOneItem() { QFETCH(QQuickListView::Orientation, orientation); QFETCH(Qt::LayoutDirection, layoutDirection); QFETCH(int, highlightRangeMode); QFETCH(QPoint, flickStart); QFETCH(QPoint, flickEnd); QFETCH(qreal, snapAlignment); QFETCH(qreal, endExtent); QFETCH(qreal, startExtent); #ifdef Q_OS_MAC // This test seems to be unreliable - different test data fails on different runs QSKIP("QTBUG-24338"); #endif QQuickView *canvas = createView(); canvas->setSource(testFileUrl("snapOneItem.qml")); canvas->show(); qApp->processEvents(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); listview->setOrientation(orientation); listview->setLayoutDirection(layoutDirection); listview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode)); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QQuickItem *contentItem = listview->contentItem(); QTRY_VERIFY(contentItem != 0); QSignalSpy currentIndexSpy(listview, SIGNAL(currentIndexChanged())); // confirm that a flick hits the next item boundary flick(canvas, flickStart, flickEnd, 180); QTRY_VERIFY(listview->isMoving() == false); // wait until it stops if (orientation == QQuickListView::Vertical) QCOMPARE(listview->contentY(), snapAlignment); else QCOMPARE(listview->contentX(), snapAlignment); if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { QCOMPARE(listview->currentIndex(), 1); QCOMPARE(currentIndexSpy.count(), 1); } // flick to end do { flick(canvas, flickStart, flickEnd, 180); QTRY_VERIFY(listview->isMoving() == false); // wait until it stops } while (orientation == QQuickListView::Vertical ? !listview->isAtYEnd() : layoutDirection == Qt::LeftToRight ? !listview->isAtXEnd() : !listview->isAtXBeginning()); if (orientation == QQuickListView::Vertical) QCOMPARE(listview->contentY(), endExtent); else QCOMPARE(listview->contentX(), endExtent); if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { QCOMPARE(listview->currentIndex(), 3); QCOMPARE(currentIndexSpy.count(), 3); } // flick to start do { flick(canvas, flickEnd, flickStart, 180); QTRY_VERIFY(listview->isMoving() == false); // wait until it stops } while (orientation == QQuickListView::Vertical ? !listview->isAtYBeginning() : layoutDirection == Qt::LeftToRight ? !listview->isAtXBeginning() : !listview->isAtXEnd()); if (orientation == QQuickListView::Vertical) QCOMPARE(listview->contentY(), startExtent); else QCOMPARE(listview->contentX(), startExtent); if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) { QCOMPARE(listview->currentIndex(), 0); QCOMPARE(currentIndexSpy.count(), 6); } delete canvas; } void tst_QQuickListView::unrequestedVisibility() { QmlListModel model; for (int i = 0; i < 30; i++) model.addItem("Item" + QString::number(i), QString::number(i)); QQuickView *canvas = new QQuickView(0); canvas->setGeometry(0,0,240,320); QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("testWrap", QVariant(false)); canvas->setSource(testFileUrl("unrequestedItems.qml")); canvas->show(); qApp->processEvents(); QQuickListView *leftview = findItem(canvas->rootObject(), "leftList"); QTRY_VERIFY(leftview != 0); QQuickListView *rightview = findItem(canvas->rootObject(), "rightList"); QTRY_VERIFY(rightview != 0); QQuickItem *leftContent = leftview->contentItem(); QTRY_VERIFY(leftContent != 0); QQuickItem *rightContent = rightview->contentItem(); QTRY_VERIFY(rightContent != 0); rightview->setCurrentIndex(20); QTRY_COMPARE(leftview->contentY(), 0.0); QTRY_COMPARE(rightview->contentY(), 100.0); QQuickItem *item; QVERIFY(item = findItem(leftContent, "wrapper", 1)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 1)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 19)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(rightContent, "wrapper", 19)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(leftContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(leftContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(rightContent, "wrapper", 3)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(rightContent, "wrapper", 4)); QCOMPARE(item->isVisible(), true); rightview->setCurrentIndex(0); QTRY_COMPARE(leftview->contentY(), 0.0); QTRY_COMPARE(rightview->contentY(), 0.0); QVERIFY(item = findItem(leftContent, "wrapper", 1)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 1)); QTRY_COMPARE(item->isVisible(), true); QVERIFY(!findItem(leftContent, "wrapper", 19)); QVERIFY(!findItem(rightContent, "wrapper", 19)); leftview->setCurrentIndex(20); QTRY_COMPARE(leftview->contentY(), 100.0); QTRY_COMPARE(rightview->contentY(), 0.0); QVERIFY(item = findItem(leftContent, "wrapper", 1)); QTRY_COMPARE(item->isVisible(), false); QVERIFY(item = findItem(rightContent, "wrapper", 1)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(leftContent, "wrapper", 19)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 19)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 3)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 4)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); model.moveItems(19, 1, 1); QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); QTRY_VERIFY(item = findItem(leftContent, "wrapper", 1)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(rightContent, "wrapper", 1)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(leftContent, "wrapper", 19)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 19)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 4)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 5)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); model.moveItems(3, 4, 1); QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); QVERIFY(item = findItem(leftContent, "wrapper", 4)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 5)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); model.moveItems(4, 3, 1); QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); QVERIFY(item = findItem(leftContent, "wrapper", 4)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 5)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); model.moveItems(16, 17, 1); QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); QVERIFY(item = findItem(leftContent, "wrapper", 4)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 5)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); model.moveItems(17, 16, 1); QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false); QVERIFY(item = findItem(leftContent, "wrapper", 4)); QCOMPARE(item->isVisible(), false); QVERIFY(item = findItem(leftContent, "wrapper", 5)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 16)); QCOMPARE(item->isVisible(), true); QVERIFY(item = findItem(rightContent, "wrapper", 17)); QCOMPARE(item->isVisible(), false); delete canvas; } void tst_QQuickListView::populateTransitions() { QFETCH(bool, staticallyPopulate); QFETCH(bool, dynamicallyPopulate); QFETCH(bool, usePopulateTransition); QPointF transitionFrom(-50, -50); QPointF transitionVia(100, 100); QaimModel model_transitionFrom; QaimModel model_transitionVia; QaimModel model; if (staticallyPopulate) { for (int i = 0; i < 30; i++) model.addItem("item" + QString::number(i), ""); } QQuickView *canvas = createView(); canvas->rootContext()->setContextProperty("testModel", &model); canvas->rootContext()->setContextProperty("testObject", new TestObject(canvas->rootContext())); canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition); canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate); canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom); canvas->rootContext()->setContextProperty("transitionVia", transitionVia); canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom); canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia); canvas->setSource(testFileUrl("populateTransitions.qml")); canvas->show(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QVERIFY(listview); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem); if (staticallyPopulate || dynamicallyPopulate) { // check the populate transition is run if (usePopulateTransition) { QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 17); } else { QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), 0); } QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0); } else { QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); } int itemCount = findItems(contentItem, "wrapper").count(); if (usePopulateTransition) QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt()); for (int i=0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->x(), 0.0); QTRY_COMPARE(item->y(), i*20.0); QQuickText *name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } // add an item and check this is done with add trantion, not populate model.insertItem(0, "another item", ""); QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 1); QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 17 : 0); // clear the model canvas->rootContext()->setContextProperty("testModel", QVariant()); QTRY_COMPARE(listview->count(), 0); QTRY_COMPARE(findItems(contentItem, "wrapper").count(), 0); listview->setProperty("countPopulateTransitions", 0); listview->setProperty("countAddTransitions", 0); // set to a valid model and check populate transition is run a second time model.clear(); for (int i = 0; i < 30; i++) model.addItem("item" + QString::number(i), ""); canvas->rootContext()->setContextProperty("testModel", &model); QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 17 : 0); QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0); itemCount = findItems(contentItem, "wrapper").count(); if (usePopulateTransition) QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt()); for (int i=0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->x(), 0.0); QTRY_COMPARE(item->y(), i*20.0); QQuickText *name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } // reset model and check populate transition is run again listview->setProperty("countPopulateTransitions", 0); listview->setProperty("countAddTransitions", 0); model.reset(); QTRY_COMPARE(listview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 17 : 0); QTRY_COMPARE(listview->property("countAddTransitions").toInt(), 0); itemCount = findItems(contentItem, "wrapper").count(); if (usePopulateTransition) QCOMPARE(itemCount, listview->property("countPopulateTransitions").toInt()); for (int i=0; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->x(), 0.0); QTRY_COMPARE(item->y(), i*20.0); QQuickText *name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } delete canvas; } void tst_QQuickListView::populateTransitions_data() { QTest::addColumn("staticallyPopulate"); QTest::addColumn("dynamicallyPopulate"); QTest::addColumn("usePopulateTransition"); QTest::newRow("static") << true << false << true; QTest::newRow("static, no populate") << true << false << false; QTest::newRow("dynamic") << false << true << true; QTest::newRow("dynamic, no populate") << false << true << false; QTest::newRow("empty to start with") << false << false << true; QTest::newRow("empty to start with, no populate") << false << false << false; } void tst_QQuickListView::addTransitions() { QFETCH(int, initialItemCount); QFETCH(bool, shouldAnimateTargets); QFETCH(qreal, contentY); QFETCH(int, insertionIndex); QFETCH(int, insertionCount); QFETCH(ListRange, expectedDisplacedIndexes); // added items should start here QPointF targetItems_transitionFrom(-50, -50); // displaced items should pass through this point QPointF displacedItems_transitionVia(100, 100); QaimModel model; for (int i = 0; i < initialItemCount; i++) model.addItem("Original item" + QString::number(i), ""); QaimModel model_targetItems_transitionFrom; QaimModel model_displacedItems_transitionVia; QQuickView *canvas = createView(); QDeclarativeContext *ctxt = canvas->rootContext(); TestObject *testObject = new TestObject; ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom); ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia); ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom); ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia); ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("addTransitions.qml")); canvas->show(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); if (contentY != 0) { listview->setContentY(contentY); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); } QList > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model); // only target items that will become visible should be animated QList > newData; QList > expectedTargetData; QList targetIndexes; if (shouldAnimateTargets) { for (int i=insertionIndex; i= contentY / 20 && i < (contentY + listview->height()) / 20) { // only grab visible items expectedTargetData << newData.last(); targetIndexes << i; } } QVERIFY(expectedTargetData.count() > 0); } // start animation if (!newData.isEmpty()) { model.insertItems(insertionIndex, newData); QTRY_COMPARE(model.count(), listview->count()); } QList targetItems = findItems(contentItem, "wrapper", targetIndexes); if (shouldAnimateTargets) { QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count()); QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(), expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0); // check the target and displaced items were animated model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos"); model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim"); // check attached properties matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes); matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes); matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems); if (expectedDisplacedIndexes.isValid()) { // adjust expectedDisplacedIndexes to their final values after the move QList displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount); matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes); matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes); matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems); } } else { QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0); QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0); } QList items = findItems(contentItem, "wrapper"); int firstVisibleIndex = -1; int itemCount = items.count(); for (int i=0; iy() >= contentY) { QDeclarativeExpression e(qmlContext(items[i]), items[i], "index"); firstVisibleIndex = e.evaluate().toInt(); break; } } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // verify all items moved to the correct final positions for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->y(), i*20.0); QQuickText *name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } delete canvas; delete testObject; } void tst_QQuickListView::addTransitions_data() { QTest::addColumn("initialItemCount"); QTest::addColumn("contentY"); QTest::addColumn("shouldAnimateTargets"); QTest::addColumn("insertionIndex"); QTest::addColumn("insertionCount"); QTest::addColumn("expectedDisplacedIndexes"); // if inserting before visible index, items should not appear or animate in, even if there are > 1 new items QTest::newRow("insert 1, just before start") << 30 << 20.0 << false << 0 << 1 << ListRange(); QTest::newRow("insert 1, way before start") << 30 << 20.0 << false << 0 << 1 << ListRange(); QTest::newRow("insert multiple, just before start") << 30 << 100.0 << false << 0 << 3 << ListRange(); QTest::newRow("insert multiple, way before start") << 30 << 100.0 << false << 0 << 3 << ListRange(); QTest::newRow("insert 1 at start") << 30 << 0.0 << true << 0 << 1 << ListRange(0, 15); QTest::newRow("insert multiple at start") << 30 << 0.0 << true << 0 << 3 << ListRange(0, 15); QTest::newRow("insert 1 at start, content y not 0") << 30 << 40.0 << true // first visible is index 2, so translate the displaced indexes by 2 << 2 << 1 << ListRange(0 + 2, 15 + 2); QTest::newRow("insert multiple at start, content y not 0") << 30 << 40.0 << true // first visible is index 2 << 2 << 3 << ListRange(0 + 2, 15 + 2); QTest::newRow("insert 1 at start, to empty list") << 0 << 0.0 << true << 0 << 1 << ListRange(); QTest::newRow("insert multiple at start, to empty list") << 0 << 0.0 << true << 0 << 3 << ListRange(); QTest::newRow("insert 1 at middle") << 30 << 0.0 << true << 5 << 1 << ListRange(5, 15); QTest::newRow("insert multiple at middle") << 30 << 0.0 << true << 5 << 3 << ListRange(5, 15); QTest::newRow("insert 1 at bottom") << 30 << 0.0 << true << 15 << 1 << ListRange(15, 15); QTest::newRow("insert multiple at bottom") << 30 << 0.0 << true << 15 << 3 << ListRange(15, 15); QTest::newRow("insert 1 at bottom, content y not 0") << 30 << 20.0 * 3 << true << 15 + 3 << 1 << ListRange(15 + 3, 15 + 3); QTest::newRow("insert multiple at bottom, content y not 0") << 30 << 20.0 * 3 << true << 15 + 3 << 3 << ListRange(15 + 3, 15 + 3); // items added after the last visible will not be animated in, since they // do not appear in the final view QTest::newRow("insert 1 after end") << 30 << 0.0 << false << 17 << 1 << ListRange(); QTest::newRow("insert multiple after end") << 30 << 0.0 << false << 17 << 3 << ListRange(); } void tst_QQuickListView::moveTransitions() { QFETCH(int, initialItemCount); QFETCH(qreal, contentY); QFETCH(qreal, itemsOffsetAfterMove); QFETCH(int, moveFrom); QFETCH(int, moveTo); QFETCH(int, moveCount); QFETCH(ListRange, expectedDisplacedIndexes); // target and displaced items should pass through these points QPointF targetItems_transitionVia(-50, 50); QPointF displacedItems_transitionVia(100, 100); QaimModel model; for (int i = 0; i < initialItemCount; i++) model.addItem("Original item" + QString::number(i), ""); QaimModel model_targetItems_transitionVia; QaimModel model_displacedItems_transitionVia; QQuickView *canvas = createView(); QDeclarativeContext *ctxt = canvas->rootContext(); TestObject *testObject = new TestObject; ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia); ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia); ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia); ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia); ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("moveTransitions.qml")); canvas->show(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); QQuickText *name; if (contentY != 0) { listview->setContentY(contentY); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); } QList > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model); // Items moving to *or* from visible positions should be animated. // Otherwise, they should not be animated. QList > expectedTargetData; QList targetIndexes; for (int i=moveFrom; iheight()) / 20 || toIndex < (contentY + listview->height()) / 20) { expectedTargetData << qMakePair(model.name(i), model.number(i)); targetIndexes << i; } } // ViewTransition.index provides the indices that items are moving to, not from targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount); // start animation model.moveItems(moveFrom, moveTo, moveCount); QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count()); QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(), expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0); QList targetItems = findItems(contentItem, "wrapper", targetIndexes); // check the target and displaced items were animated model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos"); model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim"); // check attached properties matchItemsAndIndexes(listview->property("targetTrans_items").toMap(), model, targetIndexes); matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes); matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems); if (expectedDisplacedIndexes.isValid()) { // adjust expectedDisplacedIndexes to their final values after the move QList displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount); matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes); matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes); matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems); } QList items = findItems(contentItem, "wrapper"); int firstVisibleIndex = -1; for (int i=0; iy() >= contentY) { QDeclarativeExpression e(qmlContext(items[i]), items[i], "index"); firstVisibleIndex = e.evaluate().toInt(); break; } } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // verify all items moved to the correct final positions int itemCount = findItems(contentItem, "wrapper").count(); for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove); name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } delete canvas; delete testObject; } void tst_QQuickListView::moveTransitions_data() { QTest::addColumn("initialItemCount"); QTest::addColumn("contentY"); QTest::addColumn("itemsOffsetAfterMove"); QTest::addColumn("moveFrom"); QTest::addColumn("moveTo"); QTest::addColumn("moveCount"); QTest::addColumn("expectedDisplacedIndexes"); // when removing from above the visible, all items shift down depending on how many // items have been removed from above the visible QTest::newRow("move from above view, outside visible items, move 1") << 30 << 4*20.0 << 20.0 << 1 << 10 << 1 << ListRange(11, 15+4); QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 4*20.0 << 20.0 << 0 << 10 << 1 << ListRange(11, 15+4); QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 4*20.0 << 2*20.0 << 1 << 10 << 2 << ListRange(12, 15+4); QTest::newRow("move from above view, outside visible items, move multiple (first item)") << 30 << 4*20.0 << 3*20.0 << 0 << 10 << 3 << ListRange(13, 15+4); QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 4*20.0 << 3*20.0 << 1 << 10 << 5 << ListRange(6, 14) + ListRange(15, 15+4); QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 4*20.0 << 4*20.0 << 0 << 10 << 5 << ListRange(5, 14) + ListRange(15, 15+4); QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0 << 1 << 10 << 1 << ListRange(2, 10); QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0 << 0 << 10 << 1 << ListRange(1, 10); QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0 << 0+4 << 10+4 << 1 << ListRange(1+4, 10+4); QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0 << 10 << 15 << 1 << ListRange(11, 15); QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0 << 0 << 15 << 1 << ListRange(1, 15); QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0 << 1 << 10 << 3 << ListRange(4, 12); QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0 << 0 << 10 << 3 << ListRange(3, 12); QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 4*20.0 << 0.0 << 0+4 << 10+4 << 3 << ListRange(3+4, 12+4); QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0 << 5 << 13 << 3 << ListRange(8, 15); QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0 << 0 << 13 << 3 << ListRange(3, 15); QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0 << 10 << 1 << 1 << ListRange(1, 9); QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0 << 10 << 0 << 1 << ListRange(0, 9); QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4); QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 4*20.0 - 10 << 0.0 << 10+4 << 0+4 << 1 << ListRange(0+4, 9+4); QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0 << 15 << 10 << 1 << ListRange(10, 14); QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0 << 15 << 0 << 1 << ListRange(0, 14); QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0 << 10 << 1 << 3 << ListRange(1, 9); QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0 << 10 << 0 << 3 << ListRange(0, 9); QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 4*20.0 << 0.0 << 10+4 << 0+4 << 3 << ListRange(0+4, 9+4); QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0 << 13 << 5 << 3 << ListRange(5, 12); QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0 << 13 << 0 << 3 << ListRange(0, 12); QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0 << 20 << 0 << 1 << ListRange(0, 15); QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0 << 25 << 4 << 1 << ListRange(0+4, 15+4); QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0 << 20 << 0 << 3 << ListRange(0, 15); QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 4*20.0 << 0.0 << 25 << 4 << 3 << ListRange(0+4, 15+4); QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0 << 20 << 15 << 1 << ListRange(15, 15); QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0 << 25 << 15+4 << 1 << ListRange(15+4, 15+4); QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0 << 20 << 15 << 3 << ListRange(15, 15); QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 4*20.0 << 0.0 << 25 << 15+4 << 3 << ListRange(15+4, 15+4); } void tst_QQuickListView::removeTransitions() { QFETCH(int, initialItemCount); QFETCH(bool, shouldAnimateTargets); QFETCH(qreal, contentY); QFETCH(int, removalIndex); QFETCH(int, removalCount); QFETCH(ListRange, expectedDisplacedIndexes); // added items should end here QPointF targetItems_transitionTo(-50, -50); // displaced items should pass through this points QPointF displacedItems_transitionVia(100, 100); QaimModel model; for (int i = 0; i < initialItemCount; i++) model.addItem("Original item" + QString::number(i), ""); QaimModel model_targetItems_transitionTo; QaimModel model_displacedItems_transitionVia; QQuickView *canvas = createView(); QDeclarativeContext *ctxt = canvas->rootContext(); TestObject *testObject = new TestObject; ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo); ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia); ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo); ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia); ctxt->setContextProperty("testObject", testObject); canvas->setSource(testFileUrl("removeTransitions.qml")); canvas->show(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); if (contentY != 0) { listview->setContentY(contentY); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); } QList > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model); // only target items that are visible should be animated QList > expectedTargetData; QList targetIndexes; if (shouldAnimateTargets) { for (int i=removalIndex; i= contentY / 20 && i < (contentY + listview->height()) / 20) { expectedTargetData << qMakePair(model.name(i), model.number(i)); targetIndexes << i; } } QVERIFY(expectedTargetData.count() > 0); } // calculate targetItems and expectedTargets before model changes QList targetItems = findItems(contentItem, "wrapper", targetIndexes); QVariantMap expectedTargets; for (int i=0; icount()); if (shouldAnimateTargets) { QTRY_COMPARE(listview->property("targetTransitionsDone").toInt(), expectedTargetData.count()); QTRY_COMPARE(listview->property("displaceTransitionsDone").toInt(), expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0); // check the target and displaced items were animated model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos"); model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim"); // check attached properties QCOMPARE(listview->property("targetTrans_items").toMap(), expectedTargets); matchIndexLists(listview->property("targetTrans_targetIndexes").toList(), targetIndexes); matchItemLists(listview->property("targetTrans_targetItems").toList(), targetItems); if (expectedDisplacedIndexes.isValid()) { // adjust expectedDisplacedIndexes to their final values after the move QList displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount); matchItemsAndIndexes(listview->property("displacedTrans_items").toMap(), model, displacedIndexes); matchIndexLists(listview->property("displacedTrans_targetIndexes").toList(), targetIndexes); matchItemLists(listview->property("displacedTrans_targetItems").toList(), targetItems); } } else { QTRY_COMPARE(model_targetItems_transitionTo.count(), 0); QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0); } QList items = findItems(contentItem, "wrapper"); int firstVisibleIndex = -1; int itemCount = items.count(); for (int i=0; iy() >= contentY) firstVisibleIndex = index; if (index < 0) itemCount--; // exclude deleted items } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // verify all items moved to the correct final positions for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QCOMPARE(item->x(), 0.0); QCOMPARE(item->y(), contentY + (i-firstVisibleIndex) * 20.0); QQuickText *name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } delete canvas; delete testObject; } void tst_QQuickListView::removeTransitions_data() { QTest::addColumn("initialItemCount"); QTest::addColumn("contentY"); QTest::addColumn("shouldAnimateTargets"); QTest::addColumn("removalIndex"); QTest::addColumn("removalCount"); QTest::addColumn("expectedDisplacedIndexes"); // All items that are visible following the remove operation should be animated. // Remove targets that are outside of the view should not be animated. QTest::newRow("remove 1 before start") << 30 << 20.0 * 3 << false << 2 << 1 << ListRange(); QTest::newRow("remove multiple, all before start") << 30 << 20.0 * 3 << false << 0 << 3 << ListRange(); QTest::newRow("remove mix of before and after start") << 30 << 20.0 * 3 << true << 2 << 3 << ListRange(5, 20); // 5-20 are visible after the remove QTest::newRow("remove 1 from start") << 30 << 0.0 << true << 0 << 1 << ListRange(1, 16); // 1-16 are visible after the remove QTest::newRow("remove multiple from start") << 30 << 0.0 << true << 0 << 3 << ListRange(3, 18); // 3-18 are visible after the remove QTest::newRow("remove 1 from start, content y not 0") << 30 << 20.0 * 2 << true // first visible is index 2, so translate the displaced indexes by 2 << 2 << 1 << ListRange(1 + 2, 16 + 2); QTest::newRow("remove multiple from start, content y not 0") << 30 << 20.0 * 2 << true // first visible is index 2 << 2 << 3 << ListRange(3 + 2, 18 + 2); QTest::newRow("remove 1 from middle") << 30 << 0.0 << true << 5 << 1 << ListRange(6, 16); QTest::newRow("remove multiple from middle") << 30 << 0.0 << true << 5 << 3 << ListRange(8, 18); QTest::newRow("remove 1 from bottom") << 30 << 0.0 << true << 15 << 1 << ListRange(16, 16); // remove 15, 16, 17 // 15 will animate as the target item, 16 & 17 won't be animated since they are outside // the view, and 18 will be animated as the displaced item to replace the last item QTest::newRow("remove multiple from bottom") << 30 << 0.0 << true << 15 << 3 << ListRange(18, 18); QTest::newRow("remove 1 from bottom, content y not 0") << 30 << 20.0 * 2 << true << 15 + 2 << 1 << ListRange(16 + 2, 16 + 2); QTest::newRow("remove multiple from bottom, content y not 0") << 30 << 20.0 * 2 << true << 15 + 2 << 3 << ListRange(18 + 2, 18 + 2); QTest::newRow("remove 1 after end") << 30 << 0.0 << false << 17 << 1 << ListRange(); QTest::newRow("remove multiple after end") << 30 << 0.0 << false << 17 << 3 << ListRange(); } void tst_QQuickListView::multipleTransitions() { // Tests that if you interrupt a transition in progress with another action that // cancels the previous transition, the resulting items are still placed correctly. QFETCH(int, initialCount); QFETCH(qreal, contentY); QFETCH(QList, changes); // add transitions on the left, moves on the right QPointF addTargets_transitionFrom(-50, -50); QPointF addDisplaced_transitionFrom(-50, 50); QPointF moveTargets_transitionFrom(50, -50); QPointF moveDisplaced_transitionFrom(50, 50); QmlListModel model; for (int i = 0; i < initialCount; i++) model.addItem("Original item" + QString::number(i), ""); QQuickView *canvas = createView(); QDeclarativeContext *ctxt = canvas->rootContext(); TestObject *testObject = new TestObject; ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("testObject", testObject); ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom); ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom); ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom); ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom); canvas->setSource(testFileUrl("multipleTransitions.qml")); canvas->show(); QQuickListView *listview = findItem(canvas->rootObject(), "list"); QTRY_VERIFY(listview != 0); QQuickItem *contentItem = listview->contentItem(); QVERIFY(contentItem != 0); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt(); QList > targetItems; for (int i=0; icount()); QTRY_VERIFY(listview->property("runningAddTargets").toBool()); QTRY_VERIFY(listview->property("runningAddDisplaced").toBool()); if (i == changes.count() - 1) { QTRY_VERIFY(!listview->property("runningAddTargets").toBool()); QTRY_VERIFY(!listview->property("runningAddDisplaced").toBool()); } else { QTest::qWait(timeBetweenActions); } break; } case ListChange::Removed: for (int j=changes[i].index; jcount()); QTRY_VERIFY(listview->property("runningRemoveTargets").toBool()); QTRY_VERIFY(listview->property("runningRemoveDisplaced").toBool()); if (i == changes.count() - 1) { QTRY_VERIFY(!listview->property("runningRemoveTargets").toBool()); QTRY_VERIFY(!listview->property("runningRemoveDisplaced").toBool()); } else { QTest::qWait(timeBetweenActions); } break; case ListChange::Moved: for (int j=changes[i].index; jproperty("runningMoveTargets").toBool()); QTRY_VERIFY(listview->property("runningMoveDisplaced").toBool()); if (i == changes.count() - 1) { QTRY_VERIFY(!listview->property("runningMoveTargets").toBool()); QTRY_VERIFY(!listview->property("runningMoveDisplaced").toBool()); } else { QTest::qWait(timeBetweenActions); } break; case ListChange::SetCurrent: listview->setCurrentIndex(changes[i].index); break; case ListChange::SetContentY: listview->setContentY(changes[i].pos); QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false); break; } } QCOMPARE(listview->count(), model.count()); QList items = findItems(contentItem, "wrapper"); int firstVisibleIndex = -1; for (int i=0; iy() >= contentY) { QDeclarativeExpression e(qmlContext(items[i]), items[i], "index"); firstVisibleIndex = e.evaluate().toInt(); break; } } QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex)); // verify all items moved to the correct final positions int itemCount = findItems(contentItem, "wrapper").count(); for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) { QQuickItem *item = findItem(contentItem, "wrapper", i); QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i))); QTRY_COMPARE(item->x(), 0.0); QTRY_COMPARE(item->y(), i*20.0); QQuickText *name = findItem(contentItem, "textName", i); QVERIFY(name != 0); QTRY_COMPARE(name->text(), model.name(i)); } delete canvas; delete testObject; } void tst_QQuickListView::multipleTransitions_data() { QTest::addColumn("initialCount"); QTest::addColumn("contentY"); QTest::addColumn >("changes"); // the added item and displaced items should move to final dest correctly QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList() << ListChange::insert(0, 1) << ListChange::move(0, 3, 1) ); // items affected by the add should change from move to add transition QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList() << ListChange::move(1, 10, 3) << ListChange::insert(0, 1) ); // items should be placed correctly if you trigger a transition then refill for that index QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList() << ListChange::insert(0, 1) << ListChange::setContentY(80.0) << ListChange::setContentY(0.0) << ListChange::insert(0, 1) ); } QList tst_QQuickListView::toIntList(const QVariantList &list) { QList ret; bool ok = true; for (int i=0; i &expectedIndexes) { for (int i=0; i current = indexLists[i].value >().toSet(); if (current != expectedIndexes.toSet()) qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes; QCOMPARE(current, expectedIndexes.toSet()); } } void tst_QQuickListView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList &expectedIndexes) { for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) { QVERIFY(it.value().type() == QVariant::Int); QString name = it.key(); int itemIndex = it.value().toInt(); QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex))); if (model.name(itemIndex) != name) qDebug() << itemIndex; QCOMPARE(model.name(itemIndex), name); } QCOMPARE(items.count(), expectedIndexes.count()); } void tst_QQuickListView::matchItemLists(const QVariantList &itemLists, const QList &expectedItems) { for (int i=0; i(current[j].value()); QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j))); QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j))); } QCOMPARE(current.count(), expectedItems.count()); } } QTEST_MAIN(tst_QQuickListView) #include "tst_qquicklistview.moc"