1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtGui/QStringListModel>
44 #include <QtDeclarative/qsgview.h>
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativecontext.h>
47 #include <QtDeclarative/qdeclarativeexpression.h>
48 #include <QtDeclarative/private/qsgitem_p.h>
49 #include <QtDeclarative/private/qsglistview_p.h>
50 #include <QtDeclarative/private/qsgtext_p.h>
51 #include <QtDeclarative/private/qsgvisualitemmodel_p.h>
52 #include <QtDeclarative/private/qdeclarativelistmodel_p.h>
53 #include <QtDeclarative/private/qlistmodelinterface_p.h>
54 #include "../../../shared/util.h"
55 #include "incrementalmodel.h"
56 #include <QtOpenGL/QGLShaderProgram>
59 // In Symbian OS test data is located in applications private dir
63 Q_DECLARE_METATYPE(Qt::LayoutDirection)
64 Q_DECLARE_METATYPE(QSGListView::Orientation)
66 class tst_QSGListView : public QObject
74 void cleanupTestCase();
75 // Test both QListModelInterface and QAbstractItemModel model types
76 void qListModelInterface_items();
77 void qAbstractItemModel_items();
79 void qListModelInterface_changed();
80 void qAbstractItemModel_changed();
82 void qListModelInterface_inserted();
83 void qAbstractItemModel_inserted();
85 void qListModelInterface_removed();
86 void qAbstractItemModel_removed();
88 void qListModelInterface_moved();
89 void qListModelInterface_moved_data();
90 void qAbstractItemModel_moved();
91 void qAbstractItemModel_moved_data();
93 void qListModelInterface_clear();
94 void qAbstractItemModel_clear();
96 void swapWithFirstItem();
99 void noCurrentIndex();
101 void enforceRange_withoutHighlight();
104 void sectionsDelegate();
106 void positionViewAtIndex();
108 void propertyChanges();
109 void componentChanges();
112 void manualHighlight();
116 void header_delayItemCreation();
121 void sizeLessThan1();
123 void resizeDelegate();
124 void resizeFirstDelegate();
127 void incrementalModel();
131 void onRemove_data();
133 void test_mirroring();
136 template <class T> void items();
137 template <class T> void changed();
138 template <class T> void inserted();
139 template <class T> void removed(bool animated);
140 template <class T> void moved();
141 template <class T> void clear();
142 QSGView *createView();
144 T *findItem(QSGItem *parent, const QString &id, int index=-1);
146 QList<T*> findItems(QSGItem *parent, const QString &objectName);
147 void dumpTree(QSGItem *parent, int depth = 0);
152 void tst_QSGListView::initTestCase()
155 if (!QGLShaderProgram::hasOpenGLShaderPrograms(canvas.context()))
156 QSKIP("QSGListView needs OpenGL 2.0", SkipAll);
159 void tst_QSGListView::cleanupTestCase()
163 class TestObject : public QObject
167 Q_PROPERTY(bool error READ error WRITE setError NOTIFY changedError)
168 Q_PROPERTY(bool animate READ animate NOTIFY changedAnim)
169 Q_PROPERTY(bool invalidHighlight READ invalidHighlight NOTIFY changedHl)
170 Q_PROPERTY(int cacheBuffer READ cacheBuffer NOTIFY changedCacheBuffer)
173 TestObject(QObject *parent = 0)
174 : QObject(parent), mError(true), mAnimate(false), mInvalidHighlight(false)
177 bool error() const { return mError; }
178 void setError(bool err) { mError = err; emit changedError(); }
180 bool animate() const { return mAnimate; }
181 void setAnimate(bool anim) { mAnimate = anim; emit changedAnim(); }
183 bool invalidHighlight() const { return mInvalidHighlight; }
184 void setInvalidHighlight(bool invalid) { mInvalidHighlight = invalid; emit changedHl(); }
186 int cacheBuffer() const { return mCacheBuffer; }
187 void setCacheBuffer(int buffer) { mCacheBuffer = buffer; emit changedCacheBuffer(); }
193 void changedCacheBuffer();
198 bool mInvalidHighlight;
203 void tst_qsglistview_move(int from, int to, int n, T *items)
206 // Only move forwards - flip if backwards moving
214 items->move(from, to);
218 typename T::ConstIterator it=items->begin(); it += from+n;
219 for (; i<to-from; ++i,++it)
220 replaced.append(*it);
222 it=items->begin(); it += from;
223 for (; i<n; ++i,++it)
224 replaced.append(*it);
225 typename T::ConstIterator f=replaced.begin();
226 typename T::Iterator t=items->begin(); t += from;
227 for (; f != replaced.end(); ++f, ++t)
232 class TestModel : public QListModelInterface
236 TestModel(QObject *parent = 0) : QListModelInterface(parent) {}
239 enum Roles { Name, Number };
241 QString name(int index) const { return list.at(index).first; }
242 QString number(int index) const { return list.at(index).second; }
244 int count() const { return list.count(); }
246 QList<int> roles() const { return QList<int>() << Name << Number; }
247 QString toString(int role) const {
258 QVariant data(int index, int role) const
261 return list.at(index).first;
263 return list.at(index).second;
266 QHash<int, QVariant> data(int index, const QList<int> &roles) const {
267 QHash<int,QVariant> returnHash;
269 for (int i = 0; i < roles.size(); ++i) {
270 int role = roles.at(i);
274 info = list.at(index).first;
277 info = list.at(index).second;
282 returnHash.insert(role, info);
287 void addItem(const QString &name, const QString &number) {
288 list.append(QPair<QString,QString>(name, number));
289 emit itemsInserted(list.count()-1, 1);
292 void insertItem(int index, const QString &name, const QString &number) {
293 list.insert(index, QPair<QString,QString>(name, number));
294 emit itemsInserted(index, 1);
297 void removeItem(int index) {
298 list.removeAt(index);
299 emit itemsRemoved(index, 1);
302 void removeItems(int index, int count) {
305 list.removeAt(index);
306 emit itemsRemoved(index, count);
309 void moveItem(int from, int to) {
311 emit itemsMoved(from, to, 1);
314 void moveItems(int from, int to, int count) {
315 tst_qsglistview_move(from, to, count, &list);
316 emit itemsMoved(from, to, count);
319 void modifyItem(int index, const QString &name, const QString &number) {
320 list[index] = QPair<QString,QString>(name, number);
321 emit itemsChanged(index, 1, roles());
325 int count = list.count();
327 emit itemsRemoved(0, count);
331 QList<QPair<QString,QString> > list;
335 class TestModel2 : public QAbstractListModel
338 enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 };
340 TestModel2(QObject *parent=0) : QAbstractListModel(parent) {
341 QHash<int, QByteArray> roles;
342 roles[Name] = "name";
343 roles[Number] = "number";
347 int rowCount(const QModelIndex &parent=QModelIndex()) const { Q_UNUSED(parent); return list.count(); }
348 QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
351 rv = list.at(index.row()).first;
352 else if (role == Number)
353 rv = list.at(index.row()).second;
358 int count() const { return rowCount(); }
359 QString name(int index) const { return list.at(index).first; }
360 QString number(int index) const { return list.at(index).second; }
362 void addItem(const QString &name, const QString &number) {
363 emit beginInsertRows(QModelIndex(), list.count(), list.count());
364 list.append(QPair<QString,QString>(name, number));
365 emit endInsertRows();
368 void addItems(const QList<QPair<QString, QString> > &items) {
369 emit beginInsertRows(QModelIndex(), list.count(), list.count()+items.count()-1);
370 for (int i=0; i<items.count(); i++)
371 list.append(QPair<QString,QString>(items[i].first, items[i].second));
372 emit endInsertRows();
375 void insertItem(int index, const QString &name, const QString &number) {
376 emit beginInsertRows(QModelIndex(), index, index);
377 list.insert(index, QPair<QString,QString>(name, number));
378 emit endInsertRows();
381 void removeItem(int index) {
382 emit beginRemoveRows(QModelIndex(), index, index);
383 list.removeAt(index);
384 emit endRemoveRows();
387 void removeItems(int index, int count) {
388 emit beginRemoveRows(QModelIndex(), index, index+count-1);
390 list.removeAt(index);
391 emit endRemoveRows();
394 void moveItem(int from, int to) {
395 emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
400 void moveItems(int from, int to, int count) {
401 emit beginMoveRows(QModelIndex(), from, from+count-1, QModelIndex(), to > from ? to+count : to);
402 tst_qsglistview_move(from, to, count, &list);
406 void modifyItem(int idx, const QString &name, const QString &number) {
407 list[idx] = QPair<QString,QString>(name, number);
408 emit dataChanged(index(idx,0), index(idx,0));
412 int count = list.count();
413 emit beginRemoveRows(QModelIndex(), 0, count-1);
415 emit endRemoveRows();
419 QList<QPair<QString,QString> > list;
422 tst_QSGListView::tst_QSGListView()
427 void tst_QSGListView::items()
429 QSGView *canvas = createView();
432 model.addItem("Fred", "12345");
433 model.addItem("John", "2345");
434 model.addItem("Bob", "54321");
436 QDeclarativeContext *ctxt = canvas->rootContext();
437 ctxt->setContextProperty("testModel", &model);
439 TestObject *testObject = new TestObject;
440 ctxt->setContextProperty("testObject", testObject);
442 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
443 qApp->processEvents();
445 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
446 QTRY_VERIFY(listview != 0);
448 QSGItem *contentItem = listview->contentItem();
449 QTRY_VERIFY(contentItem != 0);
451 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
452 QTRY_VERIFY(testObject->error() == false);
454 QTRY_VERIFY(listview->highlightItem() != 0);
455 QTRY_COMPARE(listview->count(), model.count());
456 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
457 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
459 // current item should be first item
460 QTRY_COMPARE(listview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 0));
462 for (int i = 0; i < model.count(); ++i) {
463 QSGText *name = findItem<QSGText>(contentItem, "textName", i);
464 QTRY_VERIFY(name != 0);
465 QTRY_COMPARE(name->text(), model.name(i));
466 QSGText *number = findItem<QSGText>(contentItem, "textNumber", i);
467 QTRY_VERIFY(number != 0);
468 QTRY_COMPARE(number->text(), model.number(i));
471 // switch to other delegate
472 testObject->setAnimate(true);
473 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
474 QTRY_VERIFY(testObject->error() == false);
475 QTRY_VERIFY(listview->currentItem());
477 // set invalid highlight
478 testObject->setInvalidHighlight(true);
479 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
480 QTRY_VERIFY(testObject->error() == false);
481 QTRY_VERIFY(listview->currentItem());
482 QTRY_VERIFY(listview->highlightItem() == 0);
484 // back to normal highlight
485 testObject->setInvalidHighlight(false);
486 QMetaObject::invokeMethod(canvas->rootObject(), "checkProperties");
487 QTRY_VERIFY(testObject->error() == false);
488 QTRY_VERIFY(listview->currentItem());
489 QTRY_VERIFY(listview->highlightItem() != 0);
491 // set an empty model and confirm that items are destroyed
493 ctxt->setContextProperty("testModel", &model2);
495 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
496 QTRY_VERIFY(itemCount == 0);
498 QTRY_COMPARE(listview->highlightResizeSpeed(), 1000.0);
499 QTRY_COMPARE(listview->highlightMoveSpeed(), 1000.0);
507 void tst_QSGListView::changed()
509 QSGView *canvas = createView();
512 model.addItem("Fred", "12345");
513 model.addItem("John", "2345");
514 model.addItem("Bob", "54321");
516 QDeclarativeContext *ctxt = canvas->rootContext();
517 ctxt->setContextProperty("testModel", &model);
519 TestObject *testObject = new TestObject;
520 ctxt->setContextProperty("testObject", testObject);
522 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
523 qApp->processEvents();
525 QSGFlickable *listview = findItem<QSGFlickable>(canvas->rootObject(), "list");
526 QTRY_VERIFY(listview != 0);
528 QSGItem *contentItem = listview->contentItem();
529 QTRY_VERIFY(contentItem != 0);
531 model.modifyItem(1, "Will", "9876");
532 QSGText *name = findItem<QSGText>(contentItem, "textName", 1);
533 QTRY_VERIFY(name != 0);
534 QTRY_COMPARE(name->text(), model.name(1));
535 QSGText *number = findItem<QSGText>(contentItem, "textNumber", 1);
536 QTRY_VERIFY(number != 0);
537 QTRY_COMPARE(number->text(), model.number(1));
544 void tst_QSGListView::inserted()
546 QSGView *canvas = createView();
549 model.addItem("Fred", "12345");
550 model.addItem("John", "2345");
551 model.addItem("Bob", "54321");
553 QDeclarativeContext *ctxt = canvas->rootContext();
554 ctxt->setContextProperty("testModel", &model);
556 TestObject *testObject = new TestObject;
557 ctxt->setContextProperty("testObject", testObject);
559 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
560 qApp->processEvents();
562 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
563 QTRY_VERIFY(listview != 0);
565 QSGItem *contentItem = listview->contentItem();
566 QTRY_VERIFY(contentItem != 0);
568 model.insertItem(1, "Will", "9876");
570 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
572 QSGText *name = findItem<QSGText>(contentItem, "textName", 1);
573 QTRY_VERIFY(name != 0);
574 QTRY_COMPARE(name->text(), model.name(1));
575 QSGText *number = findItem<QSGText>(contentItem, "textNumber", 1);
576 QTRY_VERIFY(number != 0);
577 QTRY_COMPARE(number->text(), model.number(1));
579 // Confirm items positioned correctly
580 for (int i = 0; i < model.count(); ++i) {
581 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
582 QTRY_COMPARE(item->y(), i*20.0);
585 model.insertItem(0, "Foo", "1111"); // zero index, and current item
587 QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count());
588 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
590 name = findItem<QSGText>(contentItem, "textName", 0);
591 QTRY_VERIFY(name != 0);
592 QTRY_COMPARE(name->text(), model.name(0));
593 number = findItem<QSGText>(contentItem, "textNumber", 0);
594 QTRY_VERIFY(number != 0);
595 QTRY_COMPARE(number->text(), model.number(0));
597 QTRY_COMPARE(listview->currentIndex(), 1);
599 // Confirm items positioned correctly
600 for (int i = 0; i < model.count(); ++i) {
601 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
602 QTRY_COMPARE(item->y(), i*20.0);
605 for (int i = model.count(); i < 30; ++i)
606 model.insertItem(i, "Hello", QString::number(i));
608 listview->setContentY(80);
610 // Insert item outside visible area
611 model.insertItem(1, "Hello", "1324");
613 QTRY_VERIFY(listview->contentY() == 80);
615 // Confirm items positioned correctly
616 for (int i = 5; i < 5+15; ++i) {
617 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
618 if (!item) qWarning() << "Item" << i << "not found";
620 QTRY_COMPARE(item->y(), i*20.0 - 20.0);
623 // QTRY_COMPARE(listview->contentItemHeight(), model.count() * 20.0);
627 model.insertItem(0, "Hello", "1234");
628 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
630 QCOMPARE(item->y(), 0.);
631 QVERIFY(listview->contentY() == 0);
638 void tst_QSGListView::removed(bool animated)
640 QSGView *canvas = createView();
643 for (int i = 0; i < 50; i++)
644 model.addItem("Item" + QString::number(i), "");
646 QDeclarativeContext *ctxt = canvas->rootContext();
647 ctxt->setContextProperty("testModel", &model);
649 TestObject *testObject = new TestObject;
650 ctxt->setContextProperty("testObject", testObject);
652 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
654 qApp->processEvents();
656 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
657 QTRY_VERIFY(listview != 0);
659 QSGItem *contentItem = listview->contentItem();
660 QTRY_VERIFY(contentItem != 0);
663 QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count());
665 QSGText *name = findItem<QSGText>(contentItem, "textName", 1);
666 QTRY_VERIFY(name != 0);
667 QTRY_COMPARE(name->text(), model.name(1));
668 QSGText *number = findItem<QSGText>(contentItem, "textNumber", 1);
669 QTRY_VERIFY(number != 0);
670 QTRY_COMPARE(number->text(), model.number(1));
672 // Confirm items positioned correctly
673 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
674 for (int i = 0; i < model.count() && i < itemCount; ++i) {
675 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
676 if (!item) qWarning() << "Item" << i << "not found";
678 QTRY_VERIFY(item->y() == i*20);
681 // Remove first item (which is the current item);
682 model.removeItem(0); // post: top item starts at 20
686 name = findItem<QSGText>(contentItem, "textName", 0);
687 QTRY_VERIFY(name != 0);
688 QTRY_COMPARE(name->text(), model.name(0));
689 number = findItem<QSGText>(contentItem, "textNumber", 0);
690 QTRY_VERIFY(number != 0);
691 QTRY_COMPARE(number->text(), model.number(0));
693 // Confirm items positioned correctly
694 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
695 for (int i = 0; i < model.count() && i < itemCount; ++i) {
696 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
697 if (!item) qWarning() << "Item" << i << "not found";
699 QTRY_COMPARE(item->y(),i*20.0 + 20.0);
702 // Remove items not visible
703 model.removeItem(18);
704 qApp->processEvents();
706 // Confirm items positioned correctly
707 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
708 for (int i = 0; i < model.count() && i < itemCount; ++i) {
709 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
710 if (!item) qWarning() << "Item" << i << "not found";
712 QTRY_COMPARE(item->y(),i*20.0+20.0);
715 // Remove items before visible
716 listview->setContentY(80);
717 listview->setCurrentIndex(10);
719 model.removeItem(1); // post: top item will be at 40
720 qApp->processEvents();
722 // Confirm items positioned correctly
723 for (int i = 2; i < 18; ++i) {
724 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
725 if (!item) qWarning() << "Item" << i << "not found";
727 QTRY_COMPARE(item->y(),40+i*20.0);
730 // Remove current index
731 QTRY_VERIFY(listview->currentIndex() == 9);
732 QSGItem *oldCurrent = listview->currentItem();
735 QTRY_COMPARE(listview->currentIndex(), 9);
736 QTRY_VERIFY(listview->currentItem() != oldCurrent);
738 listview->setContentY(40); // That's the top now
739 // let transitions settle.
742 // Confirm items positioned correctly
743 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
744 for (int i = 0; i < model.count() && i < itemCount; ++i) {
745 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
746 if (!item) qWarning() << "Item" << i << "not found";
748 QTRY_COMPARE(item->y(),40+i*20.0);
751 // remove current item beyond visible items.
752 listview->setCurrentIndex(20);
753 listview->setContentY(40);
754 model.removeItem(20);
756 QTRY_COMPARE(listview->currentIndex(), 20);
757 QTRY_VERIFY(listview->currentItem() != 0);
759 // remove item before current, but visible
760 listview->setCurrentIndex(8);
761 oldCurrent = listview->currentItem();
764 QTRY_COMPARE(listview->currentIndex(), 7);
765 QTRY_VERIFY(listview->currentItem() == oldCurrent);
767 listview->setContentY(80);
770 // remove all visible items
771 model.removeItems(1, 18);
774 // Confirm items positioned correctly
775 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
776 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
777 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i+2);
778 if (!item) qWarning() << "Item" << i+2 << "not found";
780 QTRY_COMPARE(item->y(),80+i*20.0);
783 model.removeItems(1, 17);
784 // QTest::qWait(300);
786 model.removeItems(2, 1);
787 model.addItem("New", "1");
789 QTRY_VERIFY(name = findItem<QSGText>(contentItem, "textName", model.count()-1));
790 QCOMPARE(name->text(), QString("New"));
792 // Add some more items so that we don't run out
794 for (int i = 0; i < 50; i++)
795 model.addItem("Item" + QString::number(i), "");
798 listview->setCurrentIndex(0);
799 listview->setContentY(30);
801 QTRY_VERIFY(name = findItem<QSGText>(contentItem, "textName", 0));
803 // QTBUG-19198 move to end and remove all visible items one at a time.
804 listview->positionViewAtEnd();
805 for (int i = 0; i < 18; ++i)
806 model.removeItems(model.count() - 1, 1);
807 QTRY_VERIFY(findItems<QSGItem>(contentItem, "wrapper").count() > 16);
814 void tst_QSGListView::clear()
816 QSGView *canvas = createView();
819 for (int i = 0; i < 30; i++)
820 model.addItem("Item" + QString::number(i), "");
822 QDeclarativeContext *ctxt = canvas->rootContext();
823 ctxt->setContextProperty("testModel", &model);
825 TestObject *testObject = new TestObject;
826 ctxt->setContextProperty("testObject", testObject);
828 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
829 qApp->processEvents();
831 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
832 QTRY_VERIFY(listview != 0);
834 QSGItem *contentItem = listview->contentItem();
835 QTRY_VERIFY(contentItem != 0);
839 QTRY_VERIFY(listview->count() == 0);
840 QTRY_VERIFY(listview->currentItem() == 0);
841 QTRY_VERIFY(listview->contentY() == 0);
842 QVERIFY(listview->currentIndex() == -1);
844 // confirm sanity when adding an item to cleared list
845 model.addItem("New", "1");
846 QTRY_VERIFY(listview->count() == 1);
847 QVERIFY(listview->currentItem() != 0);
848 QVERIFY(listview->currentIndex() == 0);
855 void tst_QSGListView::moved()
857 QFETCH(qreal, contentY);
861 QFETCH(qreal, itemsOffsetAfterMove);
865 QSGView *canvas = createView();
869 for (int i = 0; i < 30; i++)
870 model.addItem("Item" + QString::number(i), "");
872 QDeclarativeContext *ctxt = canvas->rootContext();
873 ctxt->setContextProperty("testModel", &model);
875 TestObject *testObject = new TestObject;
876 ctxt->setContextProperty("testObject", testObject);
878 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
879 qApp->processEvents();
881 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
882 QTRY_VERIFY(listview != 0);
884 QSGItem *contentItem = listview->contentItem();
885 QTRY_VERIFY(contentItem != 0);
887 QSGItem *currentItem = listview->currentItem();
888 QTRY_VERIFY(currentItem != 0);
890 listview->setContentY(contentY);
891 model.moveItems(from, to, count);
892 qApp->processEvents();
894 // Confirm items positioned correctly and indexes correct
895 int firstVisibleIndex = qCeil(contentY / 20.0);
896 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
898 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
899 if (i >= firstVisibleIndex + 16) // index has moved out of view
901 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
902 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
903 QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
904 name = findItem<QSGText>(contentItem, "textName", i);
906 QTRY_COMPARE(name->text(), model.name(i));
907 number = findItem<QSGText>(contentItem, "textNumber", i);
908 QVERIFY(number != 0);
909 QTRY_COMPARE(number->text(), model.number(i));
911 // current index should have been updated
912 if (item == currentItem)
913 QTRY_COMPARE(listview->currentIndex(), i);
920 void tst_QSGListView::moved_data()
922 QTest::addColumn<qreal>("contentY");
923 QTest::addColumn<int>("from");
924 QTest::addColumn<int>("to");
925 QTest::addColumn<int>("count");
926 QTest::addColumn<qreal>("itemsOffsetAfterMove");
928 // model starts with 30 items, each 20px high, in area 320px high
929 // 16 items should be visible at a time
930 // itemsOffsetAfterMove should be > 0 whenever items above the visible pos have moved
932 QTest::newRow("move 1 forwards, within visible items")
937 QTest::newRow("move 1 forwards, from non-visible -> visible")
942 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
947 QTest::newRow("move 1 forwards, from visible -> non-visible")
952 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
958 QTest::newRow("move 1 backwards, within visible items")
963 QTest::newRow("move 1 backwards, from non-visible -> visible")
968 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
973 QTest::newRow("move 1 backwards, from visible -> non-visible")
976 << 20.0 * 15; // this results in a forward movement that removes 15 items
978 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
981 << 20.0 * 16; // everything should move to after item 16
984 QTest::newRow("move multiple forwards, within visible items")
989 QTest::newRow("move multiple forwards, from non-visible -> visible")
992 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
994 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
997 << 20.0 * 3; // moving 3 from above the content y should adjust y positions accordingly
999 QTest::newRow("move multiple forwards, from visible -> non-visible")
1004 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1010 QTest::newRow("move multiple backwards, within visible items")
1015 QTest::newRow("move multiple backwards, from non-visible -> visible")
1020 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1025 QTest::newRow("move multiple backwards, from visible -> non-visible")
1026 << 80.0 // show 4-19
1028 << 20.0 * 15; // this results in a forward movement that removes 15 items
1030 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1031 << 80.0 // show 4-19
1036 void tst_QSGListView::swapWithFirstItem()
1038 QSGView *canvas = createView();
1042 for (int i = 0; i < 30; i++)
1043 model.addItem("Item" + QString::number(i), "");
1045 QDeclarativeContext *ctxt = canvas->rootContext();
1046 ctxt->setContextProperty("testModel", &model);
1048 TestObject *testObject = new TestObject;
1049 ctxt->setContextProperty("testObject", testObject);
1051 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
1052 qApp->processEvents();
1054 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1055 QTRY_VERIFY(listview != 0);
1057 // ensure content position is stable
1058 listview->setContentY(0);
1059 model.moveItem(1, 0);
1060 QTRY_VERIFY(listview->contentY() == 0);
1066 void tst_QSGListView::enforceRange()
1068 QSGView *canvas = createView();
1071 for (int i = 0; i < 30; i++)
1072 model.addItem("Item" + QString::number(i), "");
1074 QDeclarativeContext *ctxt = canvas->rootContext();
1075 ctxt->setContextProperty("testModel", &model);
1077 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listview-enforcerange.qml"));
1078 qApp->processEvents();
1080 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1081 QTRY_VERIFY(listview != 0);
1083 QTRY_COMPARE(listview->preferredHighlightBegin(), 100.0);
1084 QTRY_COMPARE(listview->preferredHighlightEnd(), 100.0);
1085 QTRY_COMPARE(listview->highlightRangeMode(), QSGListView::StrictlyEnforceRange);
1087 QSGItem *contentItem = listview->contentItem();
1088 QTRY_VERIFY(contentItem != 0);
1090 // view should be positioned at the top of the range.
1091 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
1093 QTRY_COMPARE(listview->contentY(), -100.0);
1095 QSGText *name = findItem<QSGText>(contentItem, "textName", 0);
1096 QTRY_VERIFY(name != 0);
1097 QTRY_COMPARE(name->text(), model.name(0));
1098 QSGText *number = findItem<QSGText>(contentItem, "textNumber", 0);
1099 QTRY_VERIFY(number != 0);
1100 QTRY_COMPARE(number->text(), model.number(0));
1102 // Check currentIndex is updated when contentItem moves
1103 listview->setContentY(20);
1105 QTRY_COMPARE(listview->currentIndex(), 6);
1109 for (int i = 0; i < 5; i++)
1110 model2.addItem("Item" + QString::number(i), "");
1112 ctxt->setContextProperty("testModel", &model2);
1113 QCOMPARE(listview->count(), 5);
1118 void tst_QSGListView::enforceRange_withoutHighlight()
1121 // If no highlight is set but StrictlyEnforceRange is used, the content should still move
1122 // to the correct position (i.e. to the next/previous item, not next/previous section)
1123 // when moving up/down via incrementCurrentIndex() and decrementCurrentIndex()
1125 QSGView *canvas = createView();
1129 model.addItem("Item 0", "a");
1130 model.addItem("Item 1", "b");
1131 model.addItem("Item 2", "b");
1132 model.addItem("Item 3", "c");
1134 QDeclarativeContext *ctxt = canvas->rootContext();
1135 ctxt->setContextProperty("testModel", &model);
1137 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listview-enforcerange-nohighlight.qml"));
1138 qApp->processEvents();
1140 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1141 QTRY_VERIFY(listview != 0);
1143 qreal expectedPos = -100.0;
1145 expectedPos += 10.0; // scroll past 1st section's delegate (10px height)
1146 QTRY_COMPARE(listview->contentY(), expectedPos);
1148 expectedPos += 20 + 10; // scroll past 1st section and section delegate of 2nd section
1149 QTest::keyClick(canvas, Qt::Key_Down);
1150 QTRY_COMPARE(listview->contentY(), expectedPos);
1152 expectedPos += 20; // scroll past 1st item of 2nd section
1153 QTest::keyClick(canvas, Qt::Key_Down);
1154 QTRY_COMPARE(listview->contentY(), expectedPos);
1156 expectedPos += 20 + 10; // scroll past 2nd item of 2nd section and section delegate of 3rd section
1157 QTest::keyClick(canvas, Qt::Key_Down);
1158 QTRY_COMPARE(listview->contentY(), expectedPos);
1163 void tst_QSGListView::spacing()
1165 QSGView *canvas = createView();
1168 for (int i = 0; i < 30; i++)
1169 model.addItem("Item" + QString::number(i), "");
1171 QDeclarativeContext *ctxt = canvas->rootContext();
1172 ctxt->setContextProperty("testModel", &model);
1174 TestObject *testObject = new TestObject;
1175 ctxt->setContextProperty("testObject", testObject);
1177 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
1178 qApp->processEvents();
1180 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1181 QTRY_VERIFY(listview != 0);
1183 QSGItem *contentItem = listview->contentItem();
1184 QTRY_VERIFY(contentItem != 0);
1186 // Confirm items positioned correctly
1187 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1188 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1189 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1190 if (!item) qWarning() << "Item" << i << "not found";
1192 QTRY_VERIFY(item->y() == i*20);
1195 listview->setSpacing(10);
1196 QTRY_VERIFY(listview->spacing() == 10);
1198 // Confirm items positioned correctly
1199 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1200 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1201 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1202 if (!item) qWarning() << "Item" << i << "not found";
1204 QTRY_VERIFY(item->y() == i*30);
1207 listview->setSpacing(0);
1209 // Confirm items positioned correctly
1210 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1211 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1212 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1213 if (!item) qWarning() << "Item" << i << "not found";
1215 QTRY_COMPARE(item->y(), i*20.0);
1222 void tst_QSGListView::sections()
1224 QSGView *canvas = createView();
1227 for (int i = 0; i < 30; i++)
1228 model.addItem("Item" + QString::number(i), QString::number(i/5));
1230 QDeclarativeContext *ctxt = canvas->rootContext();
1231 ctxt->setContextProperty("testModel", &model);
1233 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listview-sections.qml"));
1234 qApp->processEvents();
1236 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1237 QTRY_VERIFY(listview != 0);
1239 QSGItem *contentItem = listview->contentItem();
1240 QTRY_VERIFY(contentItem != 0);
1242 // Confirm items positioned correctly
1243 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1244 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1245 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1247 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20));
1248 QSGText *next = findItem<QSGText>(item, "nextSection");
1249 QCOMPARE(next->text().toInt(), (i+1)/5);
1252 QSignalSpy currentSectionChangedSpy(listview, SIGNAL(currentSectionChanged()));
1254 // Remove section boundary
1255 model.removeItem(5);
1257 // New section header created
1258 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 5);
1260 QTRY_COMPARE(item->height(), 40.0);
1262 model.insertItem(3, "New Item", "0");
1264 // Section header moved
1265 item = findItem<QSGItem>(contentItem, "wrapper", 5);
1267 QTRY_COMPARE(item->height(), 20.0);
1269 item = findItem<QSGItem>(contentItem, "wrapper", 6);
1271 QTRY_COMPARE(item->height(), 40.0);
1273 // insert item which will become a section header
1274 model.insertItem(6, "Replace header", "1");
1276 item = findItem<QSGItem>(contentItem, "wrapper", 6);
1278 QTRY_COMPARE(item->height(), 40.0);
1280 item = findItem<QSGItem>(contentItem, "wrapper", 7);
1282 QTRY_COMPARE(item->height(), 20.0);
1284 QTRY_COMPARE(listview->currentSection(), QString("0"));
1286 listview->setContentY(140);
1287 QTRY_COMPARE(listview->currentSection(), QString("1"));
1289 QTRY_COMPARE(currentSectionChangedSpy.count(), 1);
1291 listview->setContentY(20);
1292 QTRY_COMPARE(listview->currentSection(), QString("0"));
1294 QTRY_COMPARE(currentSectionChangedSpy.count(), 2);
1296 item = findItem<QSGItem>(contentItem, "wrapper", 1);
1298 QTRY_COMPARE(item->height(), 20.0);
1300 // check that headers change when item changes
1301 listview->setContentY(0);
1302 model.modifyItem(0, "changed", "2");
1304 item = findItem<QSGItem>(contentItem, "wrapper", 1);
1306 QTRY_COMPARE(item->height(), 40.0);
1311 void tst_QSGListView::sectionsDelegate()
1313 QSGView *canvas = createView();
1316 for (int i = 0; i < 30; i++)
1317 model.addItem("Item" + QString::number(i), QString::number(i/5));
1319 QDeclarativeContext *ctxt = canvas->rootContext();
1320 ctxt->setContextProperty("testModel", &model);
1322 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listview-sections_delegate.qml"));
1323 qApp->processEvents();
1325 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1326 QTRY_VERIFY(listview != 0);
1328 QSGItem *contentItem = listview->contentItem();
1329 QTRY_VERIFY(contentItem != 0);
1331 // Confirm items positioned correctly
1332 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1333 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1334 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1336 QTRY_COMPARE(item->y(), qreal(i*20 + ((i+5)/5) * 20));
1337 QSGText *next = findItem<QSGText>(item, "nextSection");
1338 QCOMPARE(next->text().toInt(), (i+1)/5);
1341 for (int i = 0; i < 3; ++i) {
1342 QSGItem *item = findItem<QSGItem>(contentItem, "sect_" + QString::number(i));
1344 QTRY_COMPARE(item->y(), qreal(i*20*6));
1347 model.modifyItem(0, "One", "aaa");
1348 model.modifyItem(1, "Two", "aaa");
1349 model.modifyItem(2, "Three", "aaa");
1350 model.modifyItem(3, "Four", "aaa");
1351 model.modifyItem(4, "Five", "aaa");
1353 for (int i = 0; i < 3; ++i) {
1354 QSGItem *item = findItem<QSGItem>(contentItem,
1355 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
1357 QTRY_COMPARE(item->y(), qreal(i*20*6));
1360 // remove section boundary
1361 model.removeItem(5);
1362 qApp->processEvents();
1363 for (int i = 0; i < 3; ++i) {
1364 QSGItem *item = findItem<QSGItem>(contentItem,
1365 "sect_" + (i == 0 ? QString("aaa") : QString::number(i)));
1370 QList<QSGItem*> items = findItems<QSGItem>(contentItem, "sect_1");
1371 QCOMPARE(items.count(), 1);
1374 model.modifyItem(0, "One", "aaa");
1375 model.modifyItem(1, "One", "aaa");
1376 model.modifyItem(2, "One", "aaa");
1377 model.modifyItem(3, "Four", "aaa");
1378 model.modifyItem(4, "Four", "aaa");
1379 model.modifyItem(5, "Four", "aaa");
1380 model.modifyItem(6, "Five", "aaa");
1381 model.modifyItem(7, "Five", "aaa");
1382 model.modifyItem(8, "Five", "aaa");
1383 model.modifyItem(9, "Two", "aaa");
1384 model.modifyItem(10, "Two", "aaa");
1385 model.modifyItem(11, "Two", "aaa");
1386 QTRY_COMPARE(findItems<QSGItem>(contentItem, "sect_aaa").count(), 1);
1387 canvas->rootObject()->setProperty("sectionProperty", "name");
1388 // ensure view has settled.
1389 QTRY_COMPARE(findItems<QSGItem>(contentItem, "sect_Four").count(), 1);
1390 for (int i = 0; i < 4; ++i) {
1391 QSGItem *item = findItem<QSGItem>(contentItem,
1392 "sect_" + model.name(i*3));
1394 QTRY_COMPARE(item->y(), qreal(i*20*4));
1398 model.removeItems(10, 20);
1399 // ensure view has settled.
1400 QTRY_COMPARE(findItems<QSGItem>(contentItem, "wrapper").count(), 10);
1401 // Drag view up beyond bounds
1402 QTest::mousePress(canvas, Qt::LeftButton, 0, QPoint(20,20));
1404 QMouseEvent mv(QEvent::MouseMove, QPoint(20,0), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
1405 QApplication::sendEvent(canvas, &mv);
1408 QMouseEvent mv(QEvent::MouseMove, QPoint(20,-50), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
1409 QApplication::sendEvent(canvas, &mv);
1412 QMouseEvent mv(QEvent::MouseMove, QPoint(20,-200), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
1413 QApplication::sendEvent(canvas, &mv);
1415 QTest::mouseRelease(canvas, Qt::LeftButton, 0, QPoint(20,-200));
1416 // view should settle back at 0
1417 QTRY_COMPARE(listview->contentY(), 0.0);
1422 void tst_QSGListView::currentIndex()
1425 for (int i = 0; i < 30; i++)
1426 model.addItem("Item" + QString::number(i), QString::number(i));
1428 QSGView *canvas = new QSGView(0);
1429 canvas->setFixedSize(240,320);
1431 QDeclarativeContext *ctxt = canvas->rootContext();
1432 ctxt->setContextProperty("testModel", &model);
1433 ctxt->setContextProperty("testWrap", QVariant(false));
1435 QString filename(SRCDIR "/data/listview-initCurrent.qml");
1436 canvas->setSource(QUrl::fromLocalFile(filename));
1438 qApp->processEvents();
1440 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1441 QTRY_VERIFY(listview != 0);
1443 QSGItem *contentItem = listview->contentItem();
1444 QTRY_VERIFY(contentItem != 0);
1446 // current item should be 20th item at startup
1447 // and current item should be in view
1448 QCOMPARE(listview->currentIndex(), 20);
1449 QCOMPARE(listview->contentY(), 100.0);
1450 QCOMPARE(listview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 20));
1451 QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
1454 listview->setCurrentIndex(0);
1455 QCOMPARE(listview->currentIndex(), 0);
1456 // confirm that the velocity is updated
1457 QTRY_VERIFY(listview->verticalVelocity() != 0.0);
1459 listview->incrementCurrentIndex();
1460 QCOMPARE(listview->currentIndex(), 1);
1461 listview->decrementCurrentIndex();
1462 QCOMPARE(listview->currentIndex(), 0);
1464 listview->decrementCurrentIndex();
1465 QCOMPARE(listview->currentIndex(), 0);
1468 ctxt->setContextProperty("testWrap", QVariant(true));
1469 QVERIFY(listview->isWrapEnabled());
1471 listview->decrementCurrentIndex();
1472 QCOMPARE(listview->currentIndex(), model.count()-1);
1474 QTRY_COMPARE(listview->contentY(), 280.0);
1476 listview->incrementCurrentIndex();
1477 QCOMPARE(listview->currentIndex(), 0);
1479 QTRY_COMPARE(listview->contentY(), 0.0);
1483 qApp->setActiveWindow(canvas);
1485 // to be safe and avoid failing setFocus with window managers
1486 qt_x11_wait_for_window_manager(canvas);
1488 QTRY_VERIFY(canvas->hasFocus());
1489 qApp->processEvents();
1491 QTest::keyClick(canvas, Qt::Key_Down);
1492 QCOMPARE(listview->currentIndex(), 1);
1494 QTest::keyClick(canvas, Qt::Key_Up);
1495 QCOMPARE(listview->currentIndex(), 0);
1497 // hold down Key_Down
1498 for (int i=0; i<model.count()-1; i++) {
1499 QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
1500 QTRY_COMPARE(listview->currentIndex(), i+1);
1502 QTest::keyRelease(canvas, Qt::Key_Down);
1503 QTRY_COMPARE(listview->currentIndex(), model.count()-1);
1504 QTRY_COMPARE(listview->contentY(), 280.0);
1507 for (int i=model.count()-1; i > 0; i--) {
1508 QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
1509 QTRY_COMPARE(listview->currentIndex(), i-1);
1511 QTest::keyRelease(canvas, Qt::Key_Up);
1512 QTRY_COMPARE(listview->currentIndex(), 0);
1513 QTRY_COMPARE(listview->contentY(), 0.0);
1516 // turn off auto highlight
1517 listview->setHighlightFollowsCurrentItem(false);
1518 QVERIFY(listview->highlightFollowsCurrentItem() == false);
1520 QVERIFY(listview->highlightItem());
1521 qreal hlPos = listview->highlightItem()->y();
1523 listview->setCurrentIndex(4);
1524 QTRY_COMPARE(listview->highlightItem()->y(), hlPos);
1526 // insert item before currentIndex
1527 listview->setCurrentIndex(28);
1528 model.insertItem(0, "Foo", "1111");
1529 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1531 // check removing highlight by setting currentIndex to -1;
1532 listview->setCurrentIndex(-1);
1534 QCOMPARE(listview->currentIndex(), -1);
1535 QVERIFY(!listview->highlightItem());
1536 QVERIFY(!listview->currentItem());
1541 void tst_QSGListView::noCurrentIndex()
1544 for (int i = 0; i < 30; i++)
1545 model.addItem("Item" + QString::number(i), QString::number(i));
1547 QSGView *canvas = new QSGView(0);
1548 canvas->setFixedSize(240,320);
1550 QDeclarativeContext *ctxt = canvas->rootContext();
1551 ctxt->setContextProperty("testModel", &model);
1553 QString filename(SRCDIR "/data/listview-noCurrent.qml");
1554 canvas->setSource(QUrl::fromLocalFile(filename));
1556 qApp->processEvents();
1558 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1559 QTRY_VERIFY(listview != 0);
1561 QSGItem *contentItem = listview->contentItem();
1562 QTRY_VERIFY(contentItem != 0);
1564 // current index should be -1 at startup
1565 // and we should not have a currentItem or highlightItem
1566 QCOMPARE(listview->currentIndex(), -1);
1567 QCOMPARE(listview->contentY(), 0.0);
1568 QVERIFY(!listview->highlightItem());
1569 QVERIFY(!listview->currentItem());
1571 listview->setCurrentIndex(2);
1572 QCOMPARE(listview->currentIndex(), 2);
1573 QVERIFY(listview->highlightItem());
1574 QVERIFY(listview->currentItem());
1579 void tst_QSGListView::itemList()
1581 QSGView *canvas = createView();
1583 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/itemlist.qml"));
1584 qApp->processEvents();
1586 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "view");
1587 QTRY_VERIFY(listview != 0);
1589 QSGItem *contentItem = listview->contentItem();
1590 QTRY_VERIFY(contentItem != 0);
1592 QSGVisualItemModel *model = canvas->rootObject()->findChild<QSGVisualItemModel*>("itemModel");
1593 QTRY_VERIFY(model != 0);
1595 QTRY_VERIFY(model->count() == 3);
1596 QTRY_COMPARE(listview->currentIndex(), 0);
1598 QSGItem *item = findItem<QSGItem>(contentItem, "item1");
1600 QTRY_COMPARE(item->x(), 0.0);
1601 QCOMPARE(item->height(), listview->height());
1603 QSGText *text = findItem<QSGText>(contentItem, "text1");
1605 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
1607 listview->setCurrentIndex(2);
1609 item = findItem<QSGItem>(contentItem, "item3");
1611 QTRY_COMPARE(item->x(), 480.0);
1613 text = findItem<QSGText>(contentItem, "text3");
1615 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
1620 void tst_QSGListView::cacheBuffer()
1622 QSGView *canvas = createView();
1625 for (int i = 0; i < 30; i++)
1626 model.addItem("Item" + QString::number(i), "");
1628 QDeclarativeContext *ctxt = canvas->rootContext();
1629 ctxt->setContextProperty("testModel", &model);
1631 TestObject *testObject = new TestObject;
1632 ctxt->setContextProperty("testObject", testObject);
1634 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
1635 qApp->processEvents();
1637 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1638 QTRY_VERIFY(listview != 0);
1640 QSGItem *contentItem = listview->contentItem();
1641 QTRY_VERIFY(contentItem != 0);
1642 QTRY_VERIFY(listview->delegate() != 0);
1643 QTRY_VERIFY(listview->model() != 0);
1644 QTRY_VERIFY(listview->highlight() != 0);
1646 // Confirm items positioned correctly
1647 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1648 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1649 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1650 if (!item) qWarning() << "Item" << i << "not found";
1652 QTRY_VERIFY(item->y() == i*20);
1655 testObject->setCacheBuffer(400);
1656 QTRY_VERIFY(listview->cacheBuffer() == 400);
1658 int newItemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1659 QTRY_VERIFY(newItemCount > itemCount);
1661 // Confirm items positioned correctly
1662 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
1663 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1664 if (!item) qWarning() << "Item" << i << "not found";
1666 QTRY_VERIFY(item->y() == i*20);
1673 void tst_QSGListView::positionViewAtIndex()
1675 QSGView *canvas = createView();
1678 for (int i = 0; i < 40; i++)
1679 model.addItem("Item" + QString::number(i), "");
1681 QDeclarativeContext *ctxt = canvas->rootContext();
1682 ctxt->setContextProperty("testModel", &model);
1684 TestObject *testObject = new TestObject;
1685 ctxt->setContextProperty("testObject", testObject);
1687 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
1688 qApp->processEvents();
1690 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1691 QTRY_VERIFY(listview != 0);
1693 QSGItem *contentItem = listview->contentItem();
1694 QTRY_VERIFY(contentItem != 0);
1696 // Confirm items positioned correctly
1697 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1698 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1699 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1700 if (!item) qWarning() << "Item" << i << "not found";
1702 QTRY_COMPARE(item->y(), i*20.);
1705 // Position on a currently visible item
1706 listview->positionViewAtIndex(3, QSGListView::Beginning);
1707 QTRY_COMPARE(listview->contentY(), 60.);
1709 // Confirm items positioned correctly
1710 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1711 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
1712 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1713 if (!item) qWarning() << "Item" << i << "not found";
1715 QTRY_COMPARE(item->y(), i*20.);
1718 // Position on an item beyond the visible items
1719 listview->positionViewAtIndex(22, QSGListView::Beginning);
1720 QTRY_COMPARE(listview->contentY(), 440.);
1722 // Confirm items positioned correctly
1723 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1724 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
1725 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1726 if (!item) qWarning() << "Item" << i << "not found";
1728 QTRY_COMPARE(item->y(), i*20.);
1731 // Position on an item that would leave empty space if positioned at the top
1732 listview->positionViewAtIndex(28, QSGListView::Beginning);
1733 QTRY_COMPARE(listview->contentY(), 480.);
1735 // Confirm items positioned correctly
1736 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1737 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
1738 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1739 if (!item) qWarning() << "Item" << i << "not found";
1741 QTRY_COMPARE(item->y(), i*20.);
1744 // Position at the beginning again
1745 listview->positionViewAtIndex(0, QSGListView::Beginning);
1746 QTRY_COMPARE(listview->contentY(), 0.);
1748 // Confirm items positioned correctly
1749 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1750 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
1751 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1752 if (!item) qWarning() << "Item" << i << "not found";
1754 QTRY_COMPARE(item->y(), i*20.);
1757 // Position at End using last index
1758 listview->positionViewAtIndex(model.count()-1, QSGListView::End);
1759 QTRY_COMPARE(listview->contentY(), 480.);
1761 // Confirm items positioned correctly
1762 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1763 for (int i = 24; i < model.count(); ++i) {
1764 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1765 if (!item) qWarning() << "Item" << i << "not found";
1767 QTRY_COMPARE(item->y(), i*20.);
1771 listview->positionViewAtIndex(20, QSGListView::End);
1772 QTRY_COMPARE(listview->contentY(), 100.);
1774 // Position in Center
1775 listview->positionViewAtIndex(15, QSGListView::Center);
1776 QTRY_COMPARE(listview->contentY(), 150.);
1778 // Ensure at least partially visible
1779 listview->positionViewAtIndex(15, QSGListView::Visible);
1780 QTRY_COMPARE(listview->contentY(), 150.);
1782 listview->setContentY(302);
1783 listview->positionViewAtIndex(15, QSGListView::Visible);
1784 QTRY_COMPARE(listview->contentY(), 302.);
1786 listview->setContentY(320);
1787 listview->positionViewAtIndex(15, QSGListView::Visible);
1788 QTRY_COMPARE(listview->contentY(), 300.);
1790 listview->setContentY(85);
1791 listview->positionViewAtIndex(20, QSGListView::Visible);
1792 QTRY_COMPARE(listview->contentY(), 85.);
1794 listview->setContentY(75);
1795 listview->positionViewAtIndex(20, QSGListView::Visible);
1796 QTRY_COMPARE(listview->contentY(), 100.);
1798 // Ensure completely visible
1799 listview->setContentY(120);
1800 listview->positionViewAtIndex(20, QSGListView::Contain);
1801 QTRY_COMPARE(listview->contentY(), 120.);
1803 listview->setContentY(302);
1804 listview->positionViewAtIndex(15, QSGListView::Contain);
1805 QTRY_COMPARE(listview->contentY(), 300.);
1807 listview->setContentY(85);
1808 listview->positionViewAtIndex(20, QSGListView::Contain);
1809 QTRY_COMPARE(listview->contentY(), 100.);
1811 // positionAtBeginnging
1812 listview->positionViewAtBeginning();
1813 QTRY_COMPARE(listview->contentY(), 0.);
1815 listview->setContentY(80);
1816 canvas->rootObject()->setProperty("showHeader", true);
1817 listview->positionViewAtBeginning();
1818 QTRY_COMPARE(listview->contentY(), -30.);
1821 listview->positionViewAtEnd();
1822 QTRY_COMPARE(listview->contentY(), 480.); // 40*20 - 320
1824 listview->setContentY(80);
1825 canvas->rootObject()->setProperty("showFooter", true);
1826 listview->positionViewAtEnd();
1827 QTRY_COMPARE(listview->contentY(), 510.);
1829 // set current item to outside visible view, position at beginning
1830 // and ensure highlight moves to current item
1831 listview->setCurrentIndex(1);
1832 listview->positionViewAtBeginning();
1833 QTRY_COMPARE(listview->contentY(), -30.);
1834 QVERIFY(listview->highlightItem());
1835 QCOMPARE(listview->highlightItem()->y(), 20.);
1841 void tst_QSGListView::resetModel()
1843 QSGView *canvas = createView();
1845 QStringList strings;
1846 strings << "one" << "two" << "three";
1847 QStringListModel model(strings);
1849 QDeclarativeContext *ctxt = canvas->rootContext();
1850 ctxt->setContextProperty("testModel", &model);
1852 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/displaylist.qml"));
1853 qApp->processEvents();
1855 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
1856 QTRY_VERIFY(listview != 0);
1858 QSGItem *contentItem = listview->contentItem();
1859 QTRY_VERIFY(contentItem != 0);
1861 QTRY_COMPARE(listview->count(), model.rowCount());
1863 for (int i = 0; i < model.rowCount(); ++i) {
1864 QSGText *display = findItem<QSGText>(contentItem, "displayText", i);
1865 QTRY_VERIFY(display != 0);
1866 QTRY_COMPARE(display->text(), strings.at(i));
1870 strings << "four" << "five" << "six" << "seven";
1871 model.setStringList(strings);
1873 QTRY_COMPARE(listview->count(), model.rowCount());
1875 for (int i = 0; i < model.rowCount(); ++i) {
1876 QSGText *display = findItem<QSGText>(contentItem, "displayText", i);
1877 QTRY_VERIFY(display != 0);
1878 QTRY_COMPARE(display->text(), strings.at(i));
1884 void tst_QSGListView::propertyChanges()
1886 QSGView *canvas = createView();
1887 QTRY_VERIFY(canvas);
1888 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/propertychangestest.qml"));
1890 QSGListView *listView = canvas->rootObject()->findChild<QSGListView*>("listView");
1891 QTRY_VERIFY(listView);
1893 QSignalSpy highlightFollowsCurrentItemSpy(listView, SIGNAL(highlightFollowsCurrentItemChanged()));
1894 QSignalSpy preferredHighlightBeginSpy(listView, SIGNAL(preferredHighlightBeginChanged()));
1895 QSignalSpy preferredHighlightEndSpy(listView, SIGNAL(preferredHighlightEndChanged()));
1896 QSignalSpy highlightRangeModeSpy(listView, SIGNAL(highlightRangeModeChanged()));
1897 QSignalSpy keyNavigationWrapsSpy(listView, SIGNAL(keyNavigationWrapsChanged()));
1898 QSignalSpy cacheBufferSpy(listView, SIGNAL(cacheBufferChanged()));
1899 QSignalSpy snapModeSpy(listView, SIGNAL(snapModeChanged()));
1901 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), true);
1902 QTRY_COMPARE(listView->preferredHighlightBegin(), 0.0);
1903 QTRY_COMPARE(listView->preferredHighlightEnd(), 0.0);
1904 QTRY_COMPARE(listView->highlightRangeMode(), QSGListView::ApplyRange);
1905 QTRY_COMPARE(listView->isWrapEnabled(), true);
1906 QTRY_COMPARE(listView->cacheBuffer(), 10);
1907 QTRY_COMPARE(listView->snapMode(), QSGListView::SnapToItem);
1909 listView->setHighlightFollowsCurrentItem(false);
1910 listView->setPreferredHighlightBegin(1.0);
1911 listView->setPreferredHighlightEnd(1.0);
1912 listView->setHighlightRangeMode(QSGListView::StrictlyEnforceRange);
1913 listView->setWrapEnabled(false);
1914 listView->setCacheBuffer(3);
1915 listView->setSnapMode(QSGListView::SnapOneItem);
1917 QTRY_COMPARE(listView->highlightFollowsCurrentItem(), false);
1918 QTRY_COMPARE(listView->preferredHighlightBegin(), 1.0);
1919 QTRY_COMPARE(listView->preferredHighlightEnd(), 1.0);
1920 QTRY_COMPARE(listView->highlightRangeMode(), QSGListView::StrictlyEnforceRange);
1921 QTRY_COMPARE(listView->isWrapEnabled(), false);
1922 QTRY_COMPARE(listView->cacheBuffer(), 3);
1923 QTRY_COMPARE(listView->snapMode(), QSGListView::SnapOneItem);
1925 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
1926 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
1927 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
1928 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
1929 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1930 QTRY_COMPARE(cacheBufferSpy.count(),1);
1931 QTRY_COMPARE(snapModeSpy.count(),1);
1933 listView->setHighlightFollowsCurrentItem(false);
1934 listView->setPreferredHighlightBegin(1.0);
1935 listView->setPreferredHighlightEnd(1.0);
1936 listView->setHighlightRangeMode(QSGListView::StrictlyEnforceRange);
1937 listView->setWrapEnabled(false);
1938 listView->setCacheBuffer(3);
1939 listView->setSnapMode(QSGListView::SnapOneItem);
1941 QTRY_COMPARE(highlightFollowsCurrentItemSpy.count(),1);
1942 QTRY_COMPARE(preferredHighlightBeginSpy.count(),1);
1943 QTRY_COMPARE(preferredHighlightEndSpy.count(),1);
1944 QTRY_COMPARE(highlightRangeModeSpy.count(),1);
1945 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1946 QTRY_COMPARE(cacheBufferSpy.count(),1);
1947 QTRY_COMPARE(snapModeSpy.count(),1);
1952 void tst_QSGListView::componentChanges()
1954 QSGView *canvas = createView();
1955 QTRY_VERIFY(canvas);
1956 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/propertychangestest.qml"));
1958 QSGListView *listView = canvas->rootObject()->findChild<QSGListView*>("listView");
1959 QTRY_VERIFY(listView);
1961 QDeclarativeComponent component(canvas->engine());
1962 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
1964 QDeclarativeComponent delegateComponent(canvas->engine());
1965 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
1967 QSignalSpy highlightSpy(listView, SIGNAL(highlightChanged()));
1968 QSignalSpy delegateSpy(listView, SIGNAL(delegateChanged()));
1969 QSignalSpy headerSpy(listView, SIGNAL(headerChanged()));
1970 QSignalSpy footerSpy(listView, SIGNAL(footerChanged()));
1972 listView->setHighlight(&component);
1973 listView->setHeader(&component);
1974 listView->setFooter(&component);
1975 listView->setDelegate(&delegateComponent);
1977 QTRY_COMPARE(listView->highlight(), &component);
1978 QTRY_COMPARE(listView->header(), &component);
1979 QTRY_COMPARE(listView->footer(), &component);
1980 QTRY_COMPARE(listView->delegate(), &delegateComponent);
1982 QTRY_COMPARE(highlightSpy.count(),1);
1983 QTRY_COMPARE(delegateSpy.count(),1);
1984 QTRY_COMPARE(headerSpy.count(),1);
1985 QTRY_COMPARE(footerSpy.count(),1);
1987 listView->setHighlight(&component);
1988 listView->setHeader(&component);
1989 listView->setFooter(&component);
1990 listView->setDelegate(&delegateComponent);
1992 QTRY_COMPARE(highlightSpy.count(),1);
1993 QTRY_COMPARE(delegateSpy.count(),1);
1994 QTRY_COMPARE(headerSpy.count(),1);
1995 QTRY_COMPARE(footerSpy.count(),1);
2000 void tst_QSGListView::modelChanges()
2002 QSGView *canvas = createView();
2003 QTRY_VERIFY(canvas);
2004 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/propertychangestest.qml"));
2006 QSGListView *listView = canvas->rootObject()->findChild<QSGListView*>("listView");
2007 QTRY_VERIFY(listView);
2009 QDeclarativeListModel *alternateModel = canvas->rootObject()->findChild<QDeclarativeListModel*>("alternateModel");
2010 QTRY_VERIFY(alternateModel);
2011 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2012 QSignalSpy modelSpy(listView, SIGNAL(modelChanged()));
2014 listView->setModel(modelVariant);
2015 QTRY_COMPARE(listView->model(), modelVariant);
2016 QTRY_COMPARE(modelSpy.count(),1);
2018 listView->setModel(modelVariant);
2019 QTRY_COMPARE(modelSpy.count(),1);
2021 listView->setModel(QVariant());
2022 QTRY_COMPARE(modelSpy.count(),2);
2027 void tst_QSGListView::QTBUG_9791()
2029 QSGView *canvas = createView();
2031 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/strictlyenforcerange.qml"));
2032 qApp->processEvents();
2034 QSGListView *listview = qobject_cast<QSGListView*>(canvas->rootObject());
2035 QTRY_VERIFY(listview != 0);
2037 QSGItem *contentItem = listview->contentItem();
2038 QTRY_VERIFY(contentItem != 0);
2039 QTRY_VERIFY(listview->delegate() != 0);
2040 QTRY_VERIFY(listview->model() != 0);
2042 QMetaObject::invokeMethod(listview, "fillModel");
2043 qApp->processEvents();
2045 // Confirm items positioned correctly
2046 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
2047 QCOMPARE(itemCount, 3);
2049 for (int i = 0; i < itemCount; ++i) {
2050 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2051 if (!item) qWarning() << "Item" << i << "not found";
2053 QTRY_COMPARE(item->x(), i*300.0);
2056 // check that view is positioned correctly
2057 QTRY_COMPARE(listview->contentX(), 590.0);
2062 void tst_QSGListView::manualHighlight()
2064 QSGView *canvas = new QSGView(0);
2065 canvas->setFixedSize(240,320);
2067 QString filename(SRCDIR "/data/manual-highlight.qml");
2068 canvas->setSource(QUrl::fromLocalFile(filename));
2070 qApp->processEvents();
2072 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2073 QTRY_VERIFY(listview != 0);
2075 QSGItem *contentItem = listview->contentItem();
2076 QTRY_VERIFY(contentItem != 0);
2078 QTRY_COMPARE(listview->currentIndex(), 0);
2079 QTRY_COMPARE(listview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 0));
2080 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
2082 listview->setCurrentIndex(2);
2084 QTRY_COMPARE(listview->currentIndex(), 2);
2085 QTRY_COMPARE(listview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 2));
2086 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
2089 listview->positionViewAtIndex(3, QSGListView::Contain);
2091 QTRY_COMPARE(listview->currentIndex(), 2);
2092 QTRY_COMPARE(listview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 2));
2093 QTRY_COMPARE(listview->highlightItem()->y() - 5, listview->currentItem()->y());
2098 void tst_QSGListView::QTBUG_11105()
2100 QSGView *canvas = createView();
2103 for (int i = 0; i < 30; i++)
2104 model.addItem("Item" + QString::number(i), "");
2106 QDeclarativeContext *ctxt = canvas->rootContext();
2107 ctxt->setContextProperty("testModel", &model);
2109 TestObject *testObject = new TestObject;
2110 ctxt->setContextProperty("testObject", testObject);
2112 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
2113 qApp->processEvents();
2115 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2116 QTRY_VERIFY(listview != 0);
2118 QSGItem *contentItem = listview->contentItem();
2119 QTRY_VERIFY(contentItem != 0);
2121 // Confirm items positioned correctly
2122 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
2123 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2124 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2125 if (!item) qWarning() << "Item" << i << "not found";
2127 QTRY_VERIFY(item->y() == i*20);
2130 listview->positionViewAtIndex(20, QSGListView::Beginning);
2131 QCOMPARE(listview->contentY(), 280.);
2134 for (int i = 0; i < 5; i++)
2135 model2.addItem("Item" + QString::number(i), "");
2137 ctxt->setContextProperty("testModel", &model2);
2139 itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
2140 QCOMPARE(itemCount, 5);
2146 void tst_QSGListView::header()
2148 QFETCH(QSGListView::Orientation, orientation);
2149 QFETCH(Qt::LayoutDirection, layoutDirection);
2150 QFETCH(QPointF, initialHeaderPos);
2151 QFETCH(QPointF, firstDelegatePos);
2152 QFETCH(QPointF, initialContentPos);
2153 QFETCH(QPointF, changedHeaderPos);
2154 QFETCH(QPointF, changedContentPos);
2156 QSGView *canvas = createView();
2159 for (int i = 0; i < 30; i++)
2160 model.addItem("Item" + QString::number(i), "");
2162 QDeclarativeContext *ctxt = canvas->rootContext();
2163 ctxt->setContextProperty("testModel", &model);
2165 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/header.qml"));
2166 qApp->processEvents();
2168 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2169 QTRY_VERIFY(listview != 0);
2170 listview->setOrientation(orientation);
2171 listview->setLayoutDirection(layoutDirection);
2173 QSGItem *contentItem = listview->contentItem();
2174 QTRY_VERIFY(contentItem != 0);
2176 QSGText *header = findItem<QSGText>(contentItem, "header");
2179 QVERIFY(header == listview->headerItem());
2181 QCOMPARE(header->width(), 100.);
2182 QCOMPARE(header->height(), 30.);
2183 QCOMPARE(header->pos(), initialHeaderPos);
2184 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
2186 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
2188 QCOMPARE(item->pos(), firstDelegatePos);
2191 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
2193 for (int i = 0; i < 30; i++)
2194 model.addItem("Item" + QString::number(i), "");
2196 QSignalSpy headerItemSpy(listview, SIGNAL(headerItemChanged()));
2197 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
2199 QCOMPARE(headerItemSpy.count(), 1);
2201 header = findItem<QSGText>(contentItem, "header");
2203 header = findItem<QSGText>(contentItem, "header2");
2206 QVERIFY(header == listview->headerItem());
2208 QCOMPARE(header->pos(), changedHeaderPos);
2209 QCOMPARE(header->width(), 50.);
2210 QCOMPARE(header->height(), 20.);
2211 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
2212 QCOMPARE(item->pos(), firstDelegatePos);
2217 void tst_QSGListView::header_data()
2219 QTest::addColumn<QSGListView::Orientation>("orientation");
2220 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2221 QTest::addColumn<QPointF>("initialHeaderPos");
2222 QTest::addColumn<QPointF>("changedHeaderPos");
2223 QTest::addColumn<QPointF>("initialContentPos");
2224 QTest::addColumn<QPointF>("changedContentPos");
2225 QTest::addColumn<QPointF>("firstDelegatePos");
2227 // header1 = 100 x 30
2228 // header2 = 50 x 20
2229 // delegates = 240 x 20
2232 // header above items, top left
2233 QTest::newRow("vertical, left to right") << QSGListView::Vertical << Qt::LeftToRight
2240 // header above items, top right
2241 QTest::newRow("vertical, layout right to left") << QSGListView::Vertical << Qt::RightToLeft
2248 // header to left of items
2249 QTest::newRow("horizontal, layout left to right") << QSGListView::Horizontal << Qt::LeftToRight
2256 // header to right of items
2257 QTest::newRow("horizontal, layout right to left") << QSGListView::Horizontal << Qt::RightToLeft
2260 << QPointF(-240 + 100, 0)
2261 << QPointF(-240 + 50, 0)
2262 << QPointF(-240, 0);
2265 void tst_QSGListView::header_delayItemCreation()
2267 QSGView *canvas = createView();
2271 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/header1.qml"));
2272 qApp->processEvents();
2274 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2275 QTRY_VERIFY(listview != 0);
2277 QSGItem *contentItem = listview->contentItem();
2278 QTRY_VERIFY(contentItem != 0);
2280 QSGText *header = findItem<QSGText>(contentItem, "header");
2282 QCOMPARE(header->y(), -header->height());
2284 QCOMPARE(listview->contentY(), -header->height());
2287 QTRY_COMPARE(header->y(), -header->height());
2292 void tst_QSGListView::footer()
2294 QFETCH(QSGListView::Orientation, orientation);
2295 QFETCH(Qt::LayoutDirection, layoutDirection);
2296 QFETCH(QPointF, initialFooterPos);
2297 QFETCH(QPointF, firstDelegatePos);
2298 QFETCH(QPointF, initialContentPos);
2299 QFETCH(QPointF, changedFooterPos);
2300 QFETCH(QPointF, changedContentPos);
2302 QSGView *canvas = createView();
2305 for (int i = 0; i < 3; i++)
2306 model.addItem("Item" + QString::number(i), "");
2308 QDeclarativeContext *ctxt = canvas->rootContext();
2309 ctxt->setContextProperty("testModel", &model);
2311 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/footer.qml"));
2313 qApp->processEvents();
2315 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2316 QTRY_VERIFY(listview != 0);
2317 listview->setOrientation(orientation);
2318 listview->setLayoutDirection(layoutDirection);
2320 QSGItem *contentItem = listview->contentItem();
2321 QTRY_VERIFY(contentItem != 0);
2323 QSGText *footer = findItem<QSGText>(contentItem, "footer");
2326 QVERIFY(footer == listview->footerItem());
2328 QCOMPARE(footer->pos(), initialFooterPos);
2329 QCOMPARE(footer->width(), 100.);
2330 QCOMPARE(footer->height(), 30.);
2331 QCOMPARE(QPointF(listview->contentX(), listview->contentY()), initialContentPos);
2333 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
2335 QCOMPARE(item->pos(), firstDelegatePos);
2338 model.removeItem(1);
2340 if (orientation == QSGListView::Vertical) {
2341 QTRY_COMPARE(footer->y(), initialFooterPos.y() - 20); // delegate height = 20
2343 QTRY_COMPARE(footer->x(), layoutDirection == Qt::LeftToRight ?
2344 initialFooterPos.x() - 40 : initialFooterPos.x() + 40); // delegate width = 40
2350 QPointF posWhenNoItems(0, 0);
2351 if (orientation == QSGListView::Horizontal && layoutDirection == Qt::RightToLeft)
2352 posWhenNoItems.setX(-100);
2353 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2355 // if header is present, it's at a negative pos, so the footer should not move
2356 canvas->rootObject()->setProperty("showHeader", true);
2357 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2358 canvas->rootObject()->setProperty("showHeader", false);
2361 for (int i = 0; i < 30; i++)
2362 model.addItem("Item" + QString::number(i), "");
2364 QSignalSpy footerItemSpy(listview, SIGNAL(footerItemChanged()));
2365 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
2367 QCOMPARE(footerItemSpy.count(), 1);
2369 footer = findItem<QSGText>(contentItem, "footer");
2371 footer = findItem<QSGText>(contentItem, "footer2");
2374 QVERIFY(footer == listview->footerItem());
2376 QCOMPARE(footer->pos(), changedFooterPos);
2377 QCOMPARE(footer->width(), 50.);
2378 QCOMPARE(footer->height(), 20.);
2379 QTRY_COMPARE(QPointF(listview->contentX(), listview->contentY()), changedContentPos);
2381 item = findItem<QSGItem>(contentItem, "wrapper", 0);
2383 QCOMPARE(item->pos(), firstDelegatePos);
2388 void tst_QSGListView::footer_data()
2390 QTest::addColumn<QSGListView::Orientation>("orientation");
2391 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2392 QTest::addColumn<QPointF>("initialFooterPos");
2393 QTest::addColumn<QPointF>("changedFooterPos");
2394 QTest::addColumn<QPointF>("initialContentPos");
2395 QTest::addColumn<QPointF>("changedContentPos");
2396 QTest::addColumn<QPointF>("firstDelegatePos");
2398 // footer1 = 100 x 30
2399 // footer2 = 100 x 20
2400 // delegates = 40 x 20
2403 // footer below items, bottom left
2404 QTest::newRow("vertical, layout left to right") << QSGListView::Vertical << Qt::LeftToRight
2405 << QPointF(0, 3 * 20)
2406 << QPointF(0, 30 * 20) // added 30 items
2411 // footer below items, bottom right
2412 QTest::newRow("vertical, layout right to left") << QSGListView::Vertical << Qt::RightToLeft
2413 << QPointF(0, 3 * 20)
2414 << QPointF(0, 30 * 20)
2419 // footer to right of items
2420 QTest::newRow("horizontal, layout left to right") << QSGListView::Horizontal << Qt::LeftToRight
2421 << QPointF(40 * 3, 0)
2422 << QPointF(40 * 30, 0)
2427 // footer to left of items
2428 QTest::newRow("horizontal, layout right to left") << QSGListView::Horizontal << Qt::RightToLeft
2429 << QPointF(-(40 * 3) - 100, 0)
2430 << QPointF(-(40 * 30) - 50, 0) // 50 = new footer width
2436 class LVAccessor : public QSGListView
2439 qreal minY() const { return minYExtent(); }
2440 qreal maxY() const { return maxYExtent(); }
2441 qreal minX() const { return minXExtent(); }
2442 qreal maxX() const { return maxXExtent(); }
2445 void tst_QSGListView::headerFooter()
2449 QSGView *canvas = createView();
2452 QDeclarativeContext *ctxt = canvas->rootContext();
2453 ctxt->setContextProperty("testModel", &model);
2455 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/headerfooter.qml"));
2456 qApp->processEvents();
2458 QSGListView *listview = qobject_cast<QSGListView*>(canvas->rootObject());
2459 QTRY_VERIFY(listview != 0);
2461 QSGItem *contentItem = listview->contentItem();
2462 QTRY_VERIFY(contentItem != 0);
2464 QSGItem *header = findItem<QSGItem>(contentItem, "header");
2466 QCOMPARE(header->y(), -header->height());
2468 QSGItem *footer = findItem<QSGItem>(contentItem, "footer");
2470 QCOMPARE(footer->y(), 0.);
2472 QVERIFY(static_cast<LVAccessor*>(listview)->minY() == 0);
2473 QVERIFY(static_cast<LVAccessor*>(listview)->maxY() == 0);
2479 QSGView *canvas = createView();
2482 QDeclarativeContext *ctxt = canvas->rootContext();
2483 ctxt->setContextProperty("testModel", &model);
2485 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/headerfooter.qml"));
2486 canvas->rootObject()->setProperty("horizontal", true);
2487 qApp->processEvents();
2489 QSGListView *listview = qobject_cast<QSGListView*>(canvas->rootObject());
2490 QTRY_VERIFY(listview != 0);
2492 QSGItem *contentItem = listview->contentItem();
2493 QTRY_VERIFY(contentItem != 0);
2495 QSGItem *header = findItem<QSGItem>(contentItem, "header");
2497 QCOMPARE(header->x(), -header->width());
2499 QSGItem *footer = findItem<QSGItem>(contentItem, "footer");
2501 QCOMPARE(footer->x(), 0.);
2503 QVERIFY(static_cast<LVAccessor*>(listview)->minX() == 0);
2504 QVERIFY(static_cast<LVAccessor*>(listview)->maxX() == 0);
2510 QSGView *canvas = createView();
2513 QDeclarativeContext *ctxt = canvas->rootContext();
2514 ctxt->setContextProperty("testModel", &model);
2516 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/headerfooter.qml"));
2517 canvas->rootObject()->setProperty("horizontal", true);
2518 canvas->rootObject()->setProperty("rtl", true);
2519 qApp->processEvents();
2521 QSGListView *listview = qobject_cast<QSGListView*>(canvas->rootObject());
2522 QTRY_VERIFY(listview != 0);
2524 QSGItem *contentItem = listview->contentItem();
2525 QTRY_VERIFY(contentItem != 0);
2527 QSGItem *header = findItem<QSGItem>(contentItem, "header");
2529 QCOMPARE(header->x(), 0.);
2531 QSGItem *footer = findItem<QSGItem>(contentItem, "footer");
2533 QCOMPARE(footer->x(), -footer->width());
2535 QCOMPARE(static_cast<LVAccessor*>(listview)->minX(), 240.);
2536 QCOMPARE(static_cast<LVAccessor*>(listview)->maxX(), 240.);
2542 void tst_QSGListView::resizeView()
2544 QSGView *canvas = createView();
2547 for (int i = 0; i < 40; i++)
2548 model.addItem("Item" + QString::number(i), "");
2550 QDeclarativeContext *ctxt = canvas->rootContext();
2551 ctxt->setContextProperty("testModel", &model);
2553 TestObject *testObject = new TestObject;
2554 ctxt->setContextProperty("testObject", testObject);
2556 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
2557 qApp->processEvents();
2559 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2560 QTRY_VERIFY(listview != 0);
2562 QSGItem *contentItem = listview->contentItem();
2563 QTRY_VERIFY(contentItem != 0);
2565 // Confirm items positioned correctly
2566 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
2567 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2568 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2569 if (!item) qWarning() << "Item" << i << "not found";
2571 QTRY_COMPARE(item->y(), i*20.);
2574 QVariant heightRatio;
2575 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
2576 QCOMPARE(heightRatio.toReal(), 0.4);
2578 listview->setHeight(200);
2580 QMetaObject::invokeMethod(canvas->rootObject(), "heightRatio", Q_RETURN_ARG(QVariant, heightRatio));
2581 QCOMPARE(heightRatio.toReal(), 0.25);
2587 void tst_QSGListView::sizeLessThan1()
2589 QSGView *canvas = createView();
2592 for (int i = 0; i < 30; i++)
2593 model.addItem("Item" + QString::number(i), "");
2595 QDeclarativeContext *ctxt = canvas->rootContext();
2596 ctxt->setContextProperty("testModel", &model);
2598 TestObject *testObject = new TestObject;
2599 ctxt->setContextProperty("testObject", testObject);
2601 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/sizelessthan1.qml"));
2602 qApp->processEvents();
2604 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2605 QTRY_VERIFY(listview != 0);
2607 QSGItem *contentItem = listview->contentItem();
2608 QTRY_VERIFY(contentItem != 0);
2610 // Confirm items positioned correctly
2611 int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
2612 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2613 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2614 if (!item) qWarning() << "Item" << i << "not found";
2616 QTRY_COMPARE(item->y(), i*0.5);
2623 void tst_QSGListView::QTBUG_14821()
2625 QSGView *canvas = createView();
2627 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/qtbug14821.qml"));
2628 qApp->processEvents();
2630 QSGListView *listview = qobject_cast<QSGListView*>(canvas->rootObject());
2631 QVERIFY(listview != 0);
2633 QSGItem *contentItem = listview->contentItem();
2634 QVERIFY(contentItem != 0);
2636 listview->decrementCurrentIndex();
2637 QCOMPARE(listview->currentIndex(), 99);
2639 listview->incrementCurrentIndex();
2640 QCOMPARE(listview->currentIndex(), 0);
2645 void tst_QSGListView::resizeDelegate()
2647 QSGView *canvas = createView();
2650 QStringList strings;
2651 for (int i = 0; i < 30; ++i)
2652 strings << QString::number(i);
2653 QStringListModel model(strings);
2655 QDeclarativeContext *ctxt = canvas->rootContext();
2656 ctxt->setContextProperty("testModel", &model);
2658 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/displaylist.qml"));
2659 qApp->processEvents();
2661 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2662 QVERIFY(listview != 0);
2664 QSGItem *contentItem = listview->contentItem();
2665 QVERIFY(contentItem != 0);
2667 QCOMPARE(listview->count(), model.rowCount());
2669 listview->setCurrentIndex(25);
2670 listview->setContentY(0);
2672 for (int i = 0; i < 16; ++i) {
2673 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2675 QCOMPARE(item->y(), i*20.0);
2678 QCOMPARE(listview->currentItem()->y(), 500.0);
2679 QTRY_COMPARE(listview->highlightItem()->y(), 500.0);
2681 canvas->rootObject()->setProperty("delegateHeight", 30);
2684 for (int i = 0; i < 11; ++i) {
2685 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2687 QTRY_COMPARE(item->y(), i*30.0);
2690 QTRY_COMPARE(listview->currentItem()->y(), 750.0);
2691 QTRY_COMPARE(listview->highlightItem()->y(), 750.0);
2693 listview->setCurrentIndex(1);
2694 listview->positionViewAtIndex(25, QSGListView::Beginning);
2695 listview->positionViewAtIndex(5, QSGListView::Beginning);
2697 for (int i = 5; i < 16; ++i) {
2698 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2700 QCOMPARE(item->y(), i*30.0);
2703 QTRY_COMPARE(listview->currentItem()->y(), 30.0);
2704 QTRY_COMPARE(listview->highlightItem()->y(), 30.0);
2706 canvas->rootObject()->setProperty("delegateHeight", 20);
2709 for (int i = 5; i < 11; ++i) {
2710 QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
2712 QTRY_COMPARE(item->y(), 150 + (i-5)*20.0);
2715 QTRY_COMPARE(listview->currentItem()->y(), 70.0);
2716 QTRY_COMPARE(listview->highlightItem()->y(), 70.0);
2721 void tst_QSGListView::resizeFirstDelegate()
2723 // QTBUG-20712: Content Y jumps constantly if first delegate height == 0
2724 // and other delegates have height > 0
2726 QSGView *canvas = createView();
2729 // bug only occurs when all items in the model are visible
2731 for (int i = 0; i < 10; i++)
2732 model.addItem("Item" + QString::number(i), "");
2734 QDeclarativeContext *ctxt = canvas->rootContext();
2735 ctxt->setContextProperty("testModel", &model);
2737 TestObject *testObject = new TestObject;
2738 ctxt->setContextProperty("testObject", testObject);
2740 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
2741 qApp->processEvents();
2743 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2744 QVERIFY(listview != 0);
2746 QSGItem *contentItem = listview->contentItem();
2747 QVERIFY(contentItem != 0);
2750 for (int i = 0; i < model.count(); ++i) {
2751 item = findItem<QSGItem>(contentItem, "wrapper", i);
2753 QCOMPARE(item->y(), i*20.0);
2756 item = findItem<QSGItem>(contentItem, "wrapper", 0);
2759 // check the content y has not jumped up and down
2760 QCOMPARE(listview->contentY(), 0.0);
2761 QSignalSpy spy(listview, SIGNAL(contentYChanged()));
2763 QCOMPARE(spy.count(), 0);
2765 for (int i = 1; i < model.count(); ++i) {
2766 item = findItem<QSGItem>(contentItem, "wrapper", i);
2768 QTRY_COMPARE(item->y(), (i-1)*20.0);
2775 void tst_QSGListView::QTBUG_16037()
2777 QSGView *canvas = createView();
2780 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/qtbug16037.qml"));
2781 qApp->processEvents();
2783 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "listview");
2784 QTRY_VERIFY(listview != 0);
2786 QVERIFY(listview->contentHeight() <= 0.0);
2788 QMetaObject::invokeMethod(canvas->rootObject(), "setModel");
2790 QTRY_COMPARE(listview->contentHeight(), 80.0);
2795 void tst_QSGListView::indexAt()
2797 QSGView *canvas = createView();
2800 for (int i = 0; i < 30; i++)
2801 model.addItem("Item" + QString::number(i), "");
2803 QDeclarativeContext *ctxt = canvas->rootContext();
2804 ctxt->setContextProperty("testModel", &model);
2806 TestObject *testObject = new TestObject;
2807 ctxt->setContextProperty("testObject", testObject);
2809 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/listviewtest.qml"));
2810 qApp->processEvents();
2812 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2813 QTRY_VERIFY(listview != 0);
2815 QSGItem *contentItem = listview->contentItem();
2816 QTRY_VERIFY(contentItem != 0);
2818 QCOMPARE(listview->indexAt(0,0), 0);
2819 QCOMPARE(listview->indexAt(0,19), 0);
2820 QCOMPARE(listview->indexAt(239,19), 0);
2821 QCOMPARE(listview->indexAt(0,20), 1);
2822 QCOMPARE(listview->indexAt(240,20), -1);
2828 void tst_QSGListView::incrementalModel()
2830 QSGView *canvas = createView();
2832 IncrementalModel model;
2833 QDeclarativeContext *ctxt = canvas->rootContext();
2834 ctxt->setContextProperty("testModel", &model);
2836 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/displaylist.qml"));
2837 qApp->processEvents();
2839 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
2840 QTRY_VERIFY(listview != 0);
2842 QSGItem *contentItem = listview->contentItem();
2843 QTRY_VERIFY(contentItem != 0);
2845 QTRY_COMPARE(listview->count(), 20);
2847 listview->positionViewAtIndex(10, QSGListView::Beginning);
2849 QTRY_COMPARE(listview->count(), 25);
2854 void tst_QSGListView::onAdd()
2856 QFETCH(int, initialItemCount);
2857 QFETCH(int, itemsToAdd);
2859 const int delegateHeight = 10;
2862 // these initial items should not trigger ListView.onAdd
2863 for (int i=0; i<initialItemCount; i++)
2864 model.addItem("dummy value", "dummy value");
2866 QSGView *canvas = createView();
2867 canvas->setFixedSize(200, delegateHeight * (initialItemCount + itemsToAdd));
2868 QDeclarativeContext *ctxt = canvas->rootContext();
2869 ctxt->setContextProperty("testModel", &model);
2870 ctxt->setContextProperty("delegateHeight", delegateHeight);
2871 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/attachedSignals.qml"));
2873 QObject *object = canvas->rootObject();
2874 object->setProperty("width", canvas->width());
2875 object->setProperty("height", canvas->height());
2876 qApp->processEvents();
2878 QList<QPair<QString, QString> > items;
2879 for (int i=0; i<itemsToAdd; i++)
2880 items << qMakePair(QString("value %1").arg(i), QString::number(i));
2881 model.addItems(items);
2883 qApp->processEvents();
2885 QVariantList result = object->property("addedDelegates").toList();
2886 QCOMPARE(result.count(), items.count());
2887 for (int i=0; i<items.count(); i++)
2888 QCOMPARE(result[i].toString(), items[i].first);
2893 void tst_QSGListView::onAdd_data()
2895 QTest::addColumn<int>("initialItemCount");
2896 QTest::addColumn<int>("itemsToAdd");
2898 QTest::newRow("0, add 1") << 0 << 1;
2899 QTest::newRow("0, add 2") << 0 << 2;
2900 QTest::newRow("0, add 10") << 0 << 10;
2902 QTest::newRow("1, add 1") << 1 << 1;
2903 QTest::newRow("1, add 2") << 1 << 2;
2904 QTest::newRow("1, add 10") << 1 << 10;
2906 QTest::newRow("5, add 1") << 5 << 1;
2907 QTest::newRow("5, add 2") << 5 << 2;
2908 QTest::newRow("5, add 10") << 5 << 10;
2911 void tst_QSGListView::onRemove()
2913 QFETCH(int, initialItemCount);
2914 QFETCH(int, indexToRemove);
2915 QFETCH(int, removeCount);
2917 const int delegateHeight = 10;
2919 for (int i=0; i<initialItemCount; i++)
2920 model.addItem(QString("value %1").arg(i), "dummy value");
2922 QSGView *canvas = createView();
2923 QDeclarativeContext *ctxt = canvas->rootContext();
2924 ctxt->setContextProperty("testModel", &model);
2925 ctxt->setContextProperty("delegateHeight", delegateHeight);
2926 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/attachedSignals.qml"));
2927 QObject *object = canvas->rootObject();
2929 qApp->processEvents();
2931 model.removeItems(indexToRemove, removeCount);
2932 qApp->processEvents();
2933 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
2938 void tst_QSGListView::onRemove_data()
2940 QTest::addColumn<int>("initialItemCount");
2941 QTest::addColumn<int>("indexToRemove");
2942 QTest::addColumn<int>("removeCount");
2944 QTest::newRow("remove first") << 1 << 0 << 1;
2945 QTest::newRow("two items, remove first") << 2 << 0 << 1;
2946 QTest::newRow("two items, remove last") << 2 << 1 << 1;
2947 QTest::newRow("two items, remove all") << 2 << 0 << 2;
2949 QTest::newRow("four items, remove first") << 4 << 0 << 1;
2950 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
2951 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
2952 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
2953 QTest::newRow("four items, remove last") << 4 << 3 << 1;
2954 QTest::newRow("four items, remove all") << 4 << 0 << 4;
2956 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
2957 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
2958 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
2961 void tst_QSGListView::rightToLeft()
2963 QSGView *canvas = createView();
2964 canvas->setFixedSize(640,320);
2965 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml"));
2966 qApp->processEvents();
2968 QVERIFY(canvas->rootObject() != 0);
2969 QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "view");
2970 QTRY_VERIFY(listview != 0);
2972 QSGItem *contentItem = listview->contentItem();
2973 QTRY_VERIFY(contentItem != 0);
2975 QSGVisualItemModel *model = canvas->rootObject()->findChild<QSGVisualItemModel*>("itemModel");
2976 QTRY_VERIFY(model != 0);
2978 QTRY_VERIFY(model->count() == 3);
2979 QTRY_COMPARE(listview->currentIndex(), 0);
2981 // initial position at first item, right edge aligned
2982 QCOMPARE(listview->contentX(), -640.);
2984 QSGItem *item = findItem<QSGItem>(contentItem, "item1");
2986 QTRY_COMPARE(item->x(), -100.0);
2987 QCOMPARE(item->height(), listview->height());
2989 QSGText *text = findItem<QSGText>(contentItem, "text1");
2991 QTRY_COMPARE(text->text(), QLatin1String("index: 0"));
2993 listview->setCurrentIndex(2);
2995 item = findItem<QSGItem>(contentItem, "item3");
2997 QTRY_COMPARE(item->x(), -540.0);
2999 text = findItem<QSGText>(contentItem, "text3");
3001 QTRY_COMPARE(text->text(), QLatin1String("index: 2"));
3003 QCOMPARE(listview->contentX(), -640.);
3005 // Ensure resizing maintains position relative to right edge
3006 qobject_cast<QSGItem*>(canvas->rootObject())->setWidth(600);
3007 QTRY_COMPARE(listview->contentX(), -600.);
3012 void tst_QSGListView::test_mirroring()
3014 QSGView *canvasA = createView();
3015 canvasA->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml"));
3016 QSGListView *listviewA = findItem<QSGListView>(canvasA->rootObject(), "view");
3017 QTRY_VERIFY(listviewA != 0);
3019 QSGView *canvasB = createView();
3020 canvasB->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml"));
3021 QSGListView *listviewB = findItem<QSGListView>(canvasB->rootObject(), "view");
3022 QTRY_VERIFY(listviewA != 0);
3023 qApp->processEvents();
3025 QList<QString> objectNames;
3026 objectNames << "item1" << "item2"; // << "item3"
3028 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
3029 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
3030 QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection());
3033 foreach(const QString objectName, objectNames)
3034 QVERIFY(findItem<QSGItem>(listviewA, objectName)->x() != findItem<QSGItem>(listviewB, objectName)->x());
3036 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
3037 listviewB->setProperty("layoutDirection", Qt::LeftToRight);
3040 foreach(const QString objectName, objectNames)
3041 QCOMPARE(findItem<QSGItem>(listviewA, objectName)->x(), findItem<QSGItem>(listviewB, objectName)->x());
3043 QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection());
3044 QSGItemPrivate::get(listviewB)->setLayoutMirror(true);
3045 QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection());
3047 // LTR != LTR+mirror
3048 foreach(const QString objectName, objectNames)
3049 QVERIFY(findItem<QSGItem>(listviewA, objectName)->x() != findItem<QSGItem>(listviewB, objectName)->x());
3051 listviewA->setProperty("layoutDirection", Qt::RightToLeft);
3053 // RTL == LTR+mirror
3054 foreach(const QString objectName, objectNames)
3055 QCOMPARE(findItem<QSGItem>(listviewA, objectName)->x(), findItem<QSGItem>(listviewB, objectName)->x());
3057 listviewB->setProperty("layoutDirection", Qt::RightToLeft);
3059 // RTL != RTL+mirror
3060 foreach(const QString objectName, objectNames)
3061 QVERIFY(findItem<QSGItem>(listviewA, objectName)->x() != findItem<QSGItem>(listviewB, objectName)->x());
3063 listviewA->setProperty("layoutDirection", Qt::LeftToRight);
3065 // LTR == RTL+mirror
3066 foreach(const QString objectName, objectNames)
3067 QCOMPARE(findItem<QSGItem>(listviewA, objectName)->x(), findItem<QSGItem>(listviewB, objectName)->x());
3073 void tst_QSGListView::qListModelInterface_items()
3078 void tst_QSGListView::qAbstractItemModel_items()
3080 items<TestModel2>();
3083 void tst_QSGListView::qListModelInterface_changed()
3085 changed<TestModel>();
3088 void tst_QSGListView::qAbstractItemModel_changed()
3090 changed<TestModel2>();
3093 void tst_QSGListView::qListModelInterface_inserted()
3095 inserted<TestModel>();
3098 void tst_QSGListView::qAbstractItemModel_inserted()
3100 inserted<TestModel2>();
3103 void tst_QSGListView::qListModelInterface_removed()
3105 removed<TestModel>(false);
3106 removed<TestModel>(true);
3109 void tst_QSGListView::qAbstractItemModel_removed()
3111 removed<TestModel2>(false);
3112 removed<TestModel2>(true);
3115 void tst_QSGListView::qListModelInterface_moved()
3120 void tst_QSGListView::qListModelInterface_moved_data()
3125 void tst_QSGListView::qAbstractItemModel_moved()
3127 moved<TestModel2>();
3130 void tst_QSGListView::qAbstractItemModel_moved_data()
3135 void tst_QSGListView::qListModelInterface_clear()
3140 void tst_QSGListView::qAbstractItemModel_clear()
3142 clear<TestModel2>();
3145 QSGView *tst_QSGListView::createView()
3147 QSGView *canvas = new QSGView(0);
3148 canvas->setFixedSize(240,320);
3154 Find an item with the specified objectName. If index is supplied then the
3155 item must also evaluate the {index} expression equal to index
3157 template<typename T>
3158 T *tst_QSGListView::findItem(QSGItem *parent, const QString &objectName, int index)
3160 const QMetaObject &mo = T::staticMetaObject;
3161 //qDebug() << parent->childItems().count() << "children";
3162 for (int i = 0; i < parent->childItems().count(); ++i) {
3163 QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
3166 //qDebug() << "try" << item;
3167 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
3169 QDeclarativeExpression e(qmlContext(item), item, "index");
3170 if (e.evaluate().toInt() == index)
3171 return static_cast<T*>(item);
3173 return static_cast<T*>(item);
3176 item = findItem<T>(item, objectName, index);
3178 return static_cast<T*>(item);
3184 template<typename T>
3185 QList<T*> tst_QSGListView::findItems(QSGItem *parent, const QString &objectName)
3188 const QMetaObject &mo = T::staticMetaObject;
3189 //qDebug() << parent->childItems().count() << "children";
3190 for (int i = 0; i < parent->childItems().count(); ++i) {
3191 QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
3192 if(!item || !item->isVisible())
3194 //qDebug() << "try" << item;
3195 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName))
3196 items.append(static_cast<T*>(item));
3197 items += findItems<T>(item, objectName);
3203 void tst_QSGListView::dumpTree(QSGItem *parent, int depth)
3205 static QString padding(" ");
3206 for (int i = 0; i < parent->childItems().count(); ++i) {
3207 QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
3210 qDebug() << padding.left(depth*2) << item;
3211 dumpTree(item, depth+1);
3216 QTEST_MAIN(tst_QSGListView)
3218 #include "tst_qsglistview.moc"