1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtCore/qstringlistmodel.h>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcomponent.h>
47 #include <QtQml/qqmlcontext.h>
48 #include <QtQml/qqmlexpression.h>
49 #include <QtQml/qqmlincubator.h>
50 #include <QtQuick/private/qquickitem_p.h>
51 #include <QtQuick/private/qquickgridview_p.h>
52 #include <QtQuick/private/qquicktext_p.h>
53 #include <QtQuick/private/qquickvisualitemmodel_p.h>
54 #include <QtQml/private/qquicklistmodel_p.h>
55 #include <QtQml/private/qlistmodelinterface_p.h>
56 #include "../../shared/util.h"
57 #include "../shared/viewtestutil.h"
58 #include "../shared/visualtestutil.h"
59 #include <QtGui/qguiapplication.h>
61 Q_DECLARE_METATYPE(Qt::LayoutDirection)
62 Q_DECLARE_METATYPE(QQuickGridView::Flow)
64 using namespace QQuickViewTestUtil;
65 using namespace QQuickVisualTestUtil;
69 class tst_QQuickGridView : public QQmlDataTest
81 void inserted_more_data();
82 void insertBeforeVisible();
83 void insertBeforeVisible_data();
86 void removed_more_data();
87 void addOrRemoveBeforeVisible();
88 void addOrRemoveBeforeVisible_data();
92 void multipleChanges();
93 void multipleChanges_data();
94 void swapWithFirstItem();
97 void noCurrentIndex();
100 void propertyChanges();
101 void componentChanges();
103 void positionViewAtIndex();
104 void positionViewAtIndex_rightToLeft();
109 void enforceRange_rightToLeft();
111 void manualHighlight();
117 void resizeViewAndRepaint();
118 void changeColumnCount();
119 void indexAt_itemAt_data();
120 void indexAt_itemAt();
124 void onRemove_data();
127 void creationContext();
128 void snapToRow_data();
130 void snapOneRow_data();
135 void unrequestedVisibility();
137 void populateTransitions();
138 void populateTransitions_data();
139 void addTransitions();
140 void addTransitions_data();
141 void moveTransitions();
142 void moveTransitions_data();
143 void removeTransitions();
144 void removeTransitions_data();
145 void displacedTransitions();
146 void displacedTransitions_data();
147 void multipleTransitions();
148 void multipleTransitions_data();
149 void multipleDisplaced();
152 QList<int> toIntList(const QVariantList &list);
153 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
154 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
155 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
158 QQuickView *getView() {
160 if (QString(QTest::currentTestFunction()) != testForView) {
164 m_view->setSource(QUrl());
169 testForView = QTest::currentTestFunction();
170 m_view = createView();
173 void releaseView(QQuickView *view) {
174 Q_ASSERT(view == m_view);
175 m_view->setSource(QUrl());
178 QQuickView *getView() {
181 void releaseView(QQuickView *view) {
190 tst_QQuickGridView::tst_QQuickGridView() : m_view(0)
194 void tst_QQuickGridView::init()
197 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
198 testForView = QString();
205 void tst_QQuickGridView::items()
207 QQuickView *canvas = createView();
210 model.addItem("Fred", "12345");
211 model.addItem("John", "2345");
212 model.addItem("Bob", "54321");
213 model.addItem("Billy", "22345");
214 model.addItem("Sam", "2945");
215 model.addItem("Ben", "04321");
216 model.addItem("Jim", "0780");
218 QQmlContext *ctxt = canvas->rootContext();
219 ctxt->setContextProperty("testModel", &model);
220 ctxt->setContextProperty("testRightToLeft", QVariant(false));
221 ctxt->setContextProperty("testTopToBottom", QVariant(false));
223 canvas->setSource(testFileUrl("gridview1.qml"));
224 qApp->processEvents();
226 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
227 QTRY_VERIFY(gridview != 0);
229 QQuickItem *contentItem = gridview->contentItem();
230 QTRY_VERIFY(contentItem != 0);
232 QTRY_COMPARE(gridview->count(), model.count());
233 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
234 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
236 for (int i = 0; i < model.count(); ++i) {
237 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
238 QTRY_VERIFY(name != 0);
239 QTRY_COMPARE(name->text(), model.name(i));
240 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
241 QTRY_VERIFY(number != 0);
242 QTRY_COMPARE(number->text(), model.number(i));
245 // set an empty model and confirm that items are destroyed
247 ctxt->setContextProperty("testModel", &model2);
249 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
250 QTRY_VERIFY(itemCount == 0);
255 void tst_QQuickGridView::changed()
257 QQuickView *canvas = createView();
260 model.addItem("Fred", "12345");
261 model.addItem("John", "2345");
262 model.addItem("Bob", "54321");
263 model.addItem("Billy", "22345");
264 model.addItem("Sam", "2945");
265 model.addItem("Ben", "04321");
266 model.addItem("Jim", "0780");
268 QQmlContext *ctxt = canvas->rootContext();
269 ctxt->setContextProperty("testModel", &model);
270 ctxt->setContextProperty("testRightToLeft", QVariant(false));
271 ctxt->setContextProperty("testTopToBottom", QVariant(false));
273 canvas->setSource(testFileUrl("gridview1.qml"));
274 qApp->processEvents();
276 QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
277 QTRY_VERIFY(gridview != 0);
279 QQuickItem *contentItem = gridview->contentItem();
280 QTRY_VERIFY(contentItem != 0);
282 model.modifyItem(1, "Will", "9876");
283 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
284 QTRY_VERIFY(name != 0);
285 QTRY_COMPARE(name->text(), model.name(1));
286 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
287 QTRY_VERIFY(number != 0);
288 QTRY_COMPARE(number->text(), model.number(1));
293 void tst_QQuickGridView::inserted()
295 QQuickView *canvas = createView();
299 model.addItem("Fred", "12345");
300 model.addItem("John", "2345");
301 model.addItem("Bob", "54321");
303 QQmlContext *ctxt = canvas->rootContext();
304 ctxt->setContextProperty("testModel", &model);
305 ctxt->setContextProperty("testRightToLeft", QVariant(false));
306 ctxt->setContextProperty("testTopToBottom", QVariant(false));
308 canvas->setSource(testFileUrl("gridview1.qml"));
309 qApp->processEvents();
311 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
312 QTRY_VERIFY(gridview != 0);
314 QQuickItem *contentItem = gridview->contentItem();
315 QTRY_VERIFY(contentItem != 0);
317 model.insertItem(1, "Will", "9876");
319 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
320 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
322 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
323 QTRY_VERIFY(name != 0);
324 QTRY_COMPARE(name->text(), model.name(1));
325 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
326 QTRY_VERIFY(number != 0);
327 QTRY_COMPARE(number->text(), model.number(1));
329 // Checks that onAdd is called
330 int added = canvas->rootObject()->property("added").toInt();
331 QTRY_COMPARE(added, 1);
333 // Confirm items positioned correctly
334 for (int i = 0; i < model.count(); ++i) {
335 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
336 QTRY_COMPARE(item->x(), (i%3)*80.0);
337 QTRY_COMPARE(item->y(), (i/3)*60.0);
340 model.insertItem(0, "Foo", "1111"); // zero index, and current item
342 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
344 name = findItem<QQuickText>(contentItem, "textName", 0);
345 QTRY_VERIFY(name != 0);
346 QTRY_COMPARE(name->text(), model.name(0));
347 number = findItem<QQuickText>(contentItem, "textNumber", 0);
348 QTRY_VERIFY(number != 0);
349 QTRY_COMPARE(number->text(), model.number(0));
351 QTRY_COMPARE(gridview->currentIndex(), 1);
353 // Confirm items positioned correctly
354 for (int i = 0; i < model.count(); ++i) {
355 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
356 QTRY_VERIFY(item->x() == (i%3)*80);
357 QTRY_VERIFY(item->y() == (i/3)*60);
360 for (int i = model.count(); i < 30; ++i)
361 model.insertItem(i, "Hello", QString::number(i));
363 gridview->setContentY(120);
365 // Insert item outside visible area
366 model.insertItem(1, "Hello", "1324");
368 QTRY_VERIFY(gridview->contentY() == 120);
373 void tst_QQuickGridView::inserted_more()
375 QFETCH(qreal, contentYRowOffset);
376 QFETCH(int, insertIndex);
377 QFETCH(int, insertCount);
378 QFETCH(qreal, rowOffsetAfterMove);
381 for (int i = 0; i < 30; i++)
382 model.addItem("Item" + QString::number(i), "");
384 QQuickView *canvas = getView();
385 QQmlContext *ctxt = canvas->rootContext();
386 ctxt->setContextProperty("testModel", &model);
387 ctxt->setContextProperty("testRightToLeft", QVariant(false));
388 ctxt->setContextProperty("testTopToBottom", QVariant(false));
390 canvas->setSource(testFileUrl("gridview1.qml"));
392 qApp->processEvents();
394 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
395 QTRY_VERIFY(gridview != 0);
396 QQuickItem *contentItem = gridview->contentItem();
397 QTRY_VERIFY(contentItem != 0);
399 gridview->setContentY(contentYRowOffset * 60.0);
400 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
402 QList<QPair<QString, QString> > newData;
403 for (int i=0; i<insertCount; i++)
404 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
405 model.insertItems(insertIndex, newData);
406 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
408 // check visibleItems.first() is in correct position
409 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
411 QCOMPARE(item0->y(), 0.0);
413 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
414 int firstVisibleIndex = -1;
415 for (int i=0; i<items.count(); i++) {
416 if (items[i]->y() >= gridview->contentY()) {
417 QQmlExpression e(qmlContext(items[i]), items[i], "index");
418 firstVisibleIndex = e.evaluate().toInt();
422 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
424 // Confirm items positioned correctly and indexes correct
425 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
428 qreal pixelOffset = 60 * rowOffsetAfterMove;
429 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
430 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
431 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
433 QCOMPARE(item->x(), (i%3)*80.0);
434 QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
436 name = findItem<QQuickText>(contentItem, "textName", i);
438 QCOMPARE(name->text(), model.name(i));
439 number = findItem<QQuickText>(contentItem, "textNumber", i);
440 QVERIFY(number != 0);
441 QCOMPARE(number->text(), model.number(i));
447 void tst_QQuickGridView::inserted_more_data()
449 QTest::addColumn<qreal>("contentYRowOffset");
450 QTest::addColumn<int>("insertIndex");
451 QTest::addColumn<int>("insertCount");
452 QTest::addColumn<qreal>("rowOffsetAfterMove");
454 QTest::newRow("add 1, before visible items")
457 << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
458 // new 1st visible item is at 0
460 QTest::newRow("add 2, before visible items")
463 << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
465 QTest::newRow("add 3, before visible items")
468 << -1.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
470 QTest::newRow("add 5, before visible items")
473 << -1.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
474 // grid is rearranged and first visible moves forward within its row
476 QTest::newRow("add 6, before visible items")
479 << -1.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
483 QTest::newRow("add 1, at start of visible, content at start")
488 QTest::newRow("add multiple, at start of visible, content at start")
493 QTest::newRow("add 1, at start of visible, content not at start")
498 QTest::newRow("add multiple, at start of visible, content not at start")
504 QTest::newRow("add 1, at end of visible, content at start")
509 QTest::newRow("add 1, at end of visible, content at start")
514 QTest::newRow("add 1, at end of visible, content not at start")
519 QTest::newRow("add multiple, at end of visible, content not at start")
525 QTest::newRow("add 1, after visible, content at start")
530 QTest::newRow("add 1, after visible, content at start")
535 QTest::newRow("add 1, after visible, content not at start")
540 QTest::newRow("add multiple, after visible, content not at start")
546 void tst_QQuickGridView::insertBeforeVisible()
548 QFETCH(int, insertIndex);
549 QFETCH(int, insertCount);
550 QFETCH(int, cacheBuffer);
553 QQuickView *canvas = getView();
556 for (int i = 0; i < 30; i++)
557 model.addItem("Item" + QString::number(i), "");
559 QQmlContext *ctxt = canvas->rootContext();
560 ctxt->setContextProperty("testModel", &model);
561 ctxt->setContextProperty("testRightToLeft", QVariant(false));
562 ctxt->setContextProperty("testTopToBottom", QVariant(false));
563 canvas->setSource(testFileUrl("gridview1.qml"));
565 qApp->processEvents();
567 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
568 QTRY_VERIFY(gridview != 0);
569 QQuickItem *contentItem = gridview->contentItem();
570 QTRY_VERIFY(contentItem != 0);
572 gridview->setCacheBuffer(cacheBuffer);
573 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
575 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
576 int firstVisibleIndex = 12; // move to an index where the top item is not visible
577 gridview->setContentY(firstVisibleIndex/3 * 60.0);
578 gridview->setCurrentIndex(firstVisibleIndex);
579 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
581 QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
582 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
584 QCOMPARE(item->y(), gridview->contentY());
586 QList<QPair<QString, QString> > newData;
587 for (int i=0; i<insertCount; i++)
588 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
589 model.insertItems(insertIndex, newData);
590 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
592 // now, moving to the top of the view should position the inserted items correctly
593 int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
594 gridview->setCurrentIndex(0);
595 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
596 QTRY_COMPARE(gridview->currentIndex(), 0);
597 QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
599 // Confirm items positioned correctly and indexes correct
600 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
601 for (int i = 0; i < model.count() && i < itemCount; ++i) {
602 item = findItem<QQuickItem>(contentItem, "wrapper", i);
603 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
604 QCOMPARE(item->x(), (i%3)*80.0);
605 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
606 name = findItem<QQuickText>(contentItem, "textName", i);
608 QTRY_COMPARE(name->text(), model.name(i));
614 void tst_QQuickGridView::insertBeforeVisible_data()
616 QTest::addColumn<int>("insertIndex");
617 QTest::addColumn<int>("insertCount");
618 QTest::addColumn<int>("cacheBuffer");
620 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
621 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
622 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
624 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
625 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
626 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
628 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
629 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
630 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
632 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
633 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
634 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
637 void tst_QQuickGridView::removed()
639 QQuickView *canvas = createView();
643 for (int i = 0; i < 40; i++)
644 model.addItem("Item" + QString::number(i), "");
646 QQmlContext *ctxt = canvas->rootContext();
647 ctxt->setContextProperty("testModel", &model);
648 ctxt->setContextProperty("testRightToLeft", QVariant(false));
649 ctxt->setContextProperty("testTopToBottom", QVariant(false));
651 canvas->setSource(testFileUrl("gridview1.qml"));
652 qApp->processEvents();
654 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
655 QTRY_VERIFY(gridview != 0);
656 QQuickItem *contentItem = gridview->contentItem();
657 QTRY_VERIFY(contentItem != 0);
658 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
661 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
663 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
664 QTRY_VERIFY(name != 0);
665 QTRY_COMPARE(name->text(), model.name(1));
666 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
667 QTRY_VERIFY(number != 0);
668 QTRY_COMPARE(number->text(), model.number(1));
671 // Checks that onRemove is called
672 QString removed = canvas->rootObject()->property("removed").toString();
673 QTRY_COMPARE(removed, QString("Item1"));
675 // Confirm items positioned correctly
676 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
677 for (int i = 0; i < model.count() && i < itemCount; ++i) {
678 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
679 if (!item) qWarning() << "Item" << i << "not found";
680 QTRY_VERIFY(item->x() == (i%3)*80);
681 QTRY_VERIFY(item->y() == (i/3)*60);
684 // Remove first item (which is the current item);
686 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
688 name = findItem<QQuickText>(contentItem, "textName", 0);
689 QTRY_VERIFY(name != 0);
690 QTRY_COMPARE(name->text(), model.name(0));
691 number = findItem<QQuickText>(contentItem, "textNumber", 0);
692 QTRY_VERIFY(number != 0);
693 QTRY_COMPARE(number->text(), model.number(0));
696 // Confirm items positioned correctly
697 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
698 for (int i = 0; i < model.count() && i < itemCount; ++i) {
699 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
700 if (!item) qWarning() << "Item" << i << "not found";
701 QTRY_VERIFY(item->x() == (i%3)*80);
702 QTRY_VERIFY(item->y() == (i/3)*60);
705 // Remove items not visible
706 model.removeItem(25);
707 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
709 // Confirm items positioned correctly
710 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
711 for (int i = 0; i < model.count() && i < itemCount; ++i) {
712 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
713 if (!item) qWarning() << "Item" << i << "not found";
714 QTRY_VERIFY(item->x() == (i%3)*80);
715 QTRY_VERIFY(item->y() == (i/3)*60);
718 // Remove items before visible
719 gridview->setContentY(120);
720 gridview->setCurrentIndex(10);
722 // Setting currentIndex above shouldn't cause view to scroll
723 QTRY_COMPARE(gridview->contentY(), 120.0);
726 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
728 // Confirm items positioned correctly
729 for (int i = 6; i < 18; ++i) {
730 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
731 if (!item) qWarning() << "Item" << i << "not found";
732 QTRY_VERIFY(item->x() == (i%3)*80);
733 QTRY_VERIFY(item->y() == (i/3)*60);
736 // Remove currentIndex
737 QQuickItem *oldCurrent = gridview->currentItem();
739 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
741 QTRY_COMPARE(gridview->currentIndex(), 9);
742 QTRY_VERIFY(gridview->currentItem() != oldCurrent);
744 gridview->setContentY(0);
745 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
747 // Confirm items positioned correctly
748 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
749 for (int i = 0; i < model.count() && i < itemCount; ++i) {
750 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
751 QTRY_VERIFY(item->x() == (i%3)*80);
752 QTRY_VERIFY(item->y() == (i/3)*60);
755 // remove item outside current view.
756 gridview->setCurrentIndex(32);
757 gridview->setContentY(240);
759 model.removeItem(30);
760 QTRY_VERIFY(gridview->currentIndex() == 31);
762 // remove current item beyond visible items.
763 gridview->setCurrentIndex(20);
764 gridview->setContentY(0);
765 model.removeItem(20);
767 QTRY_COMPARE(gridview->currentIndex(), 20);
768 QTRY_VERIFY(gridview->currentItem() != 0);
770 // remove item before current, but visible
771 gridview->setCurrentIndex(8);
772 gridview->setContentY(240);
773 oldCurrent = gridview->currentItem();
776 QTRY_COMPARE(gridview->currentIndex(), 7);
777 QTRY_VERIFY(gridview->currentItem() == oldCurrent);
782 void tst_QQuickGridView::removed_more()
784 QFETCH(qreal, contentYRowOffset);
785 QFETCH(int, removeIndex);
786 QFETCH(int, removeCount);
787 QFETCH(qreal, rowOffsetAfterMove);
788 QFETCH(QString, firstVisible);
792 QQuickView *canvas = getView();
795 for (int i = 0; i < 30; i++)
796 model.addItem("Item" + QString::number(i), "");
798 QQmlContext *ctxt = canvas->rootContext();
799 ctxt->setContextProperty("testModel", &model);
800 ctxt->setContextProperty("testRightToLeft", QVariant(false));
801 ctxt->setContextProperty("testTopToBottom", QVariant(false));
802 canvas->setSource(testFileUrl("gridview1.qml"));
804 qApp->processEvents();
806 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
807 QTRY_VERIFY(gridview != 0);
808 QQuickItem *contentItem = gridview->contentItem();
809 QTRY_VERIFY(contentItem != 0);
811 gridview->setContentY(contentYRowOffset * 60.0);
812 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
814 model.removeItems(removeIndex, removeCount);
815 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
818 int firstVisibleIndex = -1;
819 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
820 for (int i=0; i<items.count(); i++) {
821 if (items[i]->y() >= gridview->contentY()) {
822 QQmlExpression e(qmlContext(items[i]), items[i], "index");
823 firstVisibleIndex = e.evaluate().toInt();
824 QQmlExpression en(qmlContext(items[i]), items[i], "name");
825 firstName = en.evaluate().toString();
829 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
830 QCOMPARE(firstName, firstVisible);
832 // Confirm items positioned correctly and indexes correct
833 qreal pixelOffset = 60 * rowOffsetAfterMove;
834 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
835 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
836 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
838 QTRY_COMPARE(item->x(), (i%3)*80.0);
839 QTRY_COMPARE(item->y(), (i/3)*60.0 + pixelOffset);
841 name = findItem<QQuickText>(contentItem, "textName", i);
843 QTRY_COMPARE(name->text(), model.name(i));
844 number = findItem<QQuickText>(contentItem, "textNumber", i);
845 QVERIFY(number != 0);
846 QTRY_COMPARE(number->text(), model.number(i));
852 void tst_QQuickGridView::removed_more_data()
854 QTest::addColumn<qreal>("contentYRowOffset");
855 QTest::addColumn<int>("removeIndex");
856 QTest::addColumn<int>("removeCount");
857 QTest::addColumn<qreal>("rowOffsetAfterMove");
858 QTest::addColumn<QString>("firstVisible");
860 QTest::newRow("remove 1, before visible items")
865 QTest::newRow("remove 1, before visible position")
870 QTest::newRow("remove multiple, all before visible items")
873 << 1.0 << "Item6"; // removed top row, slide down by 1 row
875 QTest::newRow("remove multiple, all before visible items, remove item 0")
878 << 1.0 << "Item7"; // removed top row, slide down by 1 row
880 QTest::newRow("remove multiple rows, all before visible items")
885 QTest::newRow("remove one row before visible, content y not on item border")
888 << 1.0 << "Item6"; // 1 row removed
890 QTest::newRow("remove mix of visible/non-visible")
893 << 1.0 << "Item6"; // 1 row removed
896 // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
897 // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
898 QTest::newRow("remove multiple, mix of items from before and within visible items")
901 << 1.0 << "Item8"; // adjust for the 1 row removed before the visible
903 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
906 << 1.0 * 2 << "Item8"; // adjust for the 2 rows removed before the visible
909 QTest::newRow("remove 1, from start of visible, content at start")
914 QTest::newRow("remove multiple, from start of visible, content at start")
919 QTest::newRow("remove 1, from start of visible, content not at start")
924 QTest::newRow("remove multiple, from start of visible, content not at start")
930 QTest::newRow("remove 1, from middle of visible, content at start")
935 QTest::newRow("remove multiple, from middle of visible, content at start")
940 QTest::newRow("remove 1, from middle of visible, content not at start")
945 QTest::newRow("remove multiple, from middle of visible, content not at start")
951 QTest::newRow("remove 1, after visible, content at start")
956 QTest::newRow("remove multiple, after visible, content at start")
961 QTest::newRow("remove 1, after visible, content not at start")
966 QTest::newRow("remove multiple, after visible, content not at start")
971 QTest::newRow("remove multiple, mix of items from within and after visible items")
977 void tst_QQuickGridView::addOrRemoveBeforeVisible()
979 // QTBUG-21588: ensure re-layout is done on grid after adding or removing
980 // items from before the visible area
983 QFETCH(qreal, newTopContentY);
985 QQuickView *canvas = getView();
989 for (int i = 0; i < 30; i++)
990 model.addItem("Item" + QString::number(i), "");
992 QQmlContext *ctxt = canvas->rootContext();
993 ctxt->setContextProperty("testModel", &model);
994 ctxt->setContextProperty("testRightToLeft", QVariant(false));
995 ctxt->setContextProperty("testTopToBottom", QVariant(false));
996 canvas->setSource(testFileUrl("gridview1.qml"));
998 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
999 QTRY_VERIFY(gridview != 0);
1000 QQuickItem *contentItem = gridview->contentItem();
1001 QTRY_VERIFY(contentItem != 0);
1003 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1004 QTRY_COMPARE(name->text(), QString("Item0"));
1006 gridview->setCurrentIndex(0);
1007 qApp->processEvents();
1008 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1010 // scroll down until item 0 is no longer drawn
1011 // (bug not triggered if we just move using content y, since that doesn't
1012 // refill and change the visible items)
1013 gridview->setCurrentIndex(24);
1014 qApp->processEvents();
1016 QTRY_COMPARE(gridview->currentIndex(), 24);
1017 QTRY_COMPARE(gridview->contentY(), 220.0);
1019 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1020 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible
1023 model.insertItem(0, "New Item", "New Item number");
1024 QTRY_COMPARE(gridview->count(), 31);
1026 model.removeItem(0);
1027 QTRY_COMPARE(gridview->count(), 29);
1030 // scroll back up and item 0 should be gone
1031 gridview->setCurrentIndex(0);
1032 qApp->processEvents();
1033 QTRY_COMPARE(gridview->currentIndex(), 0);
1034 QTRY_COMPARE(gridview->contentY(), newTopContentY);
1036 name = findItem<QQuickText>(contentItem, "textName", 0);
1038 QCOMPARE(name->text(), QString("New Item"));
1040 QCOMPARE(name->text(), QString("Item1"));
1042 // Confirm items positioned correctly
1043 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1044 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1045 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
1046 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1047 QTRY_VERIFY(item->x() == (i%3)*80);
1048 QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1051 releaseView(canvas);
1054 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1056 QTest::addColumn<bool>("doAdd");
1057 QTest::addColumn<qreal>("newTopContentY");
1059 QTest::newRow("add") << true << -60.0;
1060 QTest::newRow("remove") << false << -60.0;
1063 void tst_QQuickGridView::clear()
1065 QQuickView *canvas = createView();
1068 for (int i = 0; i < 30; i++)
1069 model.addItem("Item" + QString::number(i), "");
1071 QQmlContext *ctxt = canvas->rootContext();
1072 ctxt->setContextProperty("testModel", &model);
1073 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1074 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1076 canvas->setSource(testFileUrl("gridview1.qml"));
1078 qApp->processEvents();
1080 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1081 QVERIFY(gridview != 0);
1082 QQuickItem *contentItem = gridview->contentItem();
1083 QVERIFY(contentItem != 0);
1084 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1088 QVERIFY(gridview->count() == 0);
1089 QVERIFY(gridview->currentItem() == 0);
1090 QVERIFY(gridview->contentY() == 0);
1091 QVERIFY(gridview->currentIndex() == -1);
1093 // confirm sanity when adding an item to cleared list
1094 model.addItem("New", "1");
1095 QTRY_COMPARE(gridview->count(), 1);
1096 QVERIFY(gridview->currentItem() != 0);
1097 QVERIFY(gridview->currentIndex() == 0);
1102 void tst_QQuickGridView::moved()
1104 QFETCH(qreal, contentYRowOffset);
1108 QFETCH(qreal, rowOffsetAfterMove);
1112 QQuickView *canvas = getView();
1115 for (int i = 0; i < 30; i++)
1116 model.addItem("Item" + QString::number(i), "");
1118 QQmlContext *ctxt = canvas->rootContext();
1119 ctxt->setContextProperty("testModel", &model);
1120 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1121 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1123 canvas->setSource(testFileUrl("gridview1.qml"));
1125 qApp->processEvents();
1127 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1128 QTRY_VERIFY(gridview != 0);
1129 QQuickItem *contentItem = gridview->contentItem();
1130 QTRY_VERIFY(contentItem != 0);
1131 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1133 QQuickItem *currentItem = gridview->currentItem();
1134 QTRY_VERIFY(currentItem != 0);
1136 if (contentYRowOffset != 0) {
1137 gridview->setContentY(contentYRowOffset * 60.0);
1138 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1141 model.moveItems(from, to, count);
1142 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1144 // Confirm items positioned correctly and indexes correct
1145 int firstVisibleIndex = qCeil(gridview->contentY() / 60.0) * 3;
1146 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1147 qreal pixelOffset = 60 * rowOffsetAfterMove;
1148 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
1149 if (i >= firstVisibleIndex + 18) // index has moved out of view
1151 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1152 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1154 QTRY_COMPARE(item->x(), (i%3)*80.0);
1155 QTRY_COMPARE(item->y(), (i/3)*60.0 + pixelOffset);
1157 name = findItem<QQuickText>(contentItem, "textName", i);
1159 QTRY_COMPARE(name->text(), model.name(i));
1160 number = findItem<QQuickText>(contentItem, "textNumber", i);
1161 QVERIFY(number != 0);
1162 QTRY_COMPARE(number->text(), model.number(i));
1164 // current index should have been updated
1165 if (item == currentItem)
1166 QTRY_COMPARE(gridview->currentIndex(), i);
1169 releaseView(canvas);
1172 void tst_QQuickGridView::moved_data()
1174 QTest::addColumn<qreal>("contentYRowOffset");
1175 QTest::addColumn<int>("from");
1176 QTest::addColumn<int>("to");
1177 QTest::addColumn<int>("count");
1178 QTest::addColumn<qreal>("rowOffsetAfterMove");
1180 // model starts with 30 items, each 80x60, in area 240x320
1181 // 18 items should be visible at a time
1183 // The first visible item should not move upwards and out of the view
1184 // if items are moved/removed before it.
1187 QTest::newRow("move 1 forwards, within visible items")
1192 QTest::newRow("move 1 forwards, from non-visible -> visible")
1197 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1198 << 2.0 // // show 6-23
1202 QTest::newRow("move 1 forwards, from visible -> non-visible")
1207 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1213 QTest::newRow("move 1 backwards, within visible items")
1218 QTest::newRow("move 1 backwards, within visible items (to first index)")
1223 QTest::newRow("move 1 backwards, from non-visible -> visible")
1228 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1233 QTest::newRow("move 1 backwards, from visible -> non-visible")
1236 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1238 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1241 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1244 QTest::newRow("move multiple forwards, within visible items")
1249 QTest::newRow("move multiple backwards, within visible items (move first item)")
1254 QTest::newRow("move multiple forwards, before visible items")
1256 << 3 << 4 << 3 // 3, 4, 5 move to after 6
1257 << 1.0; // row of 3,4,5 has moved down
1259 QTest::newRow("move multiple forwards, from non-visible -> visible")
1262 << 1.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1264 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1267 << 1.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1269 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1272 << 1.0; // top two rows removed, third row is now the first visible
1274 QTest::newRow("move multiple forwards, to bottom of view")
1279 QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1284 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1289 QTest::newRow("move multiple forwards, from visible -> non-visible")
1294 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1300 QTest::newRow("move multiple backwards, within visible items")
1305 QTest::newRow("move multiple backwards, from non-visible -> visible")
1310 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1315 QTest::newRow("move multiple backwards, from visible -> non-visible")
1318 << -1.0; // to minimize movement, items are added above visible area, all items move up by 1 row
1320 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1323 << -1.0; // 16,17,18 move to above item 0, all items move up by 1 row
1326 void tst_QQuickGridView::multipleChanges()
1328 QFETCH(int, startCount);
1329 QFETCH(QList<ListChange>, changes);
1330 QFETCH(int, newCount);
1331 QFETCH(int, newCurrentIndex);
1333 QQuickView *canvas = getView();
1336 for (int i = 0; i < startCount; i++)
1337 model.addItem("Item" + QString::number(i), "");
1339 QQmlContext *ctxt = canvas->rootContext();
1340 ctxt->setContextProperty("testModel", &model);
1341 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1342 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1344 canvas->setSource(testFileUrl("gridview1.qml"));
1346 qApp->processEvents();
1348 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1349 QTRY_VERIFY(gridview != 0);
1350 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1352 for (int i=0; i<changes.count(); i++) {
1353 switch (changes[i].type) {
1354 case ListChange::Inserted:
1356 QList<QPair<QString, QString> > items;
1357 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1358 items << qMakePair(QString("new item " + j), QString::number(j));
1359 model.insertItems(changes[i].index, items);
1362 case ListChange::Removed:
1363 model.removeItems(changes[i].index, changes[i].count);
1364 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1366 case ListChange::Moved:
1367 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1368 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1370 case ListChange::SetCurrent:
1371 gridview->setCurrentIndex(changes[i].index);
1372 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1374 case ListChange::SetContentY:
1375 gridview->setContentY(changes[i].pos);
1376 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1381 QTRY_COMPARE(gridview->count(), newCount);
1382 QCOMPARE(gridview->count(), model.count());
1383 QTRY_COMPARE(gridview->currentIndex(), newCurrentIndex);
1387 QQuickItem *contentItem = gridview->contentItem();
1388 QTRY_VERIFY(contentItem != 0);
1389 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1390 for (int i=0; i < model.count() && i < itemCount; ++i) {
1391 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1392 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1393 name = findItem<QQuickText>(contentItem, "textName", i);
1395 QTRY_COMPARE(name->text(), model.name(i));
1396 number = findItem<QQuickText>(contentItem, "textNumber", i);
1397 QVERIFY(number != 0);
1398 QTRY_COMPARE(number->text(), model.number(i));
1401 releaseView(canvas);
1404 void tst_QQuickGridView::multipleChanges_data()
1406 QTest::addColumn<int>("startCount");
1407 QTest::addColumn<QList<ListChange> >("changes");
1408 QTest::addColumn<int>("newCount");
1409 QTest::addColumn<int>("newCurrentIndex");
1411 QList<ListChange> changes;
1413 for (int i=1; i<30; i++)
1414 changes << ListChange::remove(0);
1415 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1417 changes << ListChange::remove(0);
1418 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1421 changes << ListChange::setCurrent(29);
1422 for (int i=29; i>0; i--)
1423 changes << ListChange::remove(i);
1424 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1426 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1427 << ListChange::remove(0, 1)
1428 << ListChange::insert(0, 1)
1431 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1432 << ListChange::setCurrent(2)
1433 << ListChange::remove(2, 1)
1434 << ListChange::insert(2, 1)
1437 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1438 << ListChange::setCurrent(1)
1439 << ListChange::remove(1, 3)
1440 << ListChange::insert(2, 2)
1443 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1444 << ListChange::setCurrent(2)
1445 << ListChange::remove(1, 3)
1446 << ListChange::move(1, 5, 1)
1449 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1450 << ListChange::setCurrent(5)
1451 << ListChange::remove(4, 3)
1452 << ListChange::move(4, 1, 1)
1456 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1457 << ListChange::insert(0, 2)
1458 << ListChange::insert(0, 4)
1459 << ListChange::insert(0, 6)
1462 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1463 << ListChange::insert(0, 2)
1464 << ListChange::insert(0, 4)
1465 << ListChange::insert(0, 6)
1466 << ListChange::setCurrent(3)
1467 << ListChange::insert(3, 2)
1470 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1471 << ListChange::insert(0, 30)
1472 << ListChange::remove(0, 30)
1475 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1476 << ListChange::insert(1)
1477 << ListChange::setCurrent(1)
1478 << ListChange::remove(1)
1481 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1482 << ListChange::insert(0, 10)
1483 << ListChange::remove(5, 10)
1486 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1487 << ListChange::insert(0, 3)
1488 << ListChange::move(0, 10, 3)
1491 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1492 << ListChange::insert(0, 3)
1493 << ListChange::move(0, 8, 5)
1496 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1497 << ListChange::setCurrent(9)
1498 << ListChange::insert(10, 3)
1499 << ListChange::move(8, 0, 5)
1503 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1504 << ListChange::setCurrent(1)
1505 << ListChange::move(1, 2, 2)
1506 << ListChange::move(2, 1, 2)
1509 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1510 << ListChange::setCurrent(2)
1511 << ListChange::move(1, 2, 3)
1512 << ListChange::move(3, 0, 5)
1515 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1516 << ListChange::setCurrent(5)
1517 << ListChange::move(5, 0, 1)
1518 << ListChange::remove(0)
1521 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1522 << ListChange::setCurrent(5)
1523 << ListChange::move(5, 0, 1)
1524 << ListChange::insert(0)
1527 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1528 << ListChange::setCurrent(1)
1529 << ListChange::move(5, 1, 3)
1530 << ListChange::remove(1, 3)
1533 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1534 << ListChange::setCurrent(5)
1535 << ListChange::move(5, 1, 3)
1536 << ListChange::insert(1, 5)
1539 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1540 << ListChange::setCurrent(3)
1541 << ListChange::move(0, 1, 2)
1542 << ListChange::insert(3, 5)
1546 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1547 << ListChange::insert(0, 5)
1548 << ListChange::setCurrent(-1)
1549 << ListChange::remove(0, 5)
1550 << ListChange::insert(0, 5)
1555 void tst_QQuickGridView::swapWithFirstItem()
1558 QQuickView *canvas = createView();
1561 for (int i = 0; i < 30; i++)
1562 model.addItem("Item" + QString::number(i), "");
1564 QQmlContext *ctxt = canvas->rootContext();
1565 ctxt->setContextProperty("testModel", &model);
1566 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1567 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1569 canvas->setSource(testFileUrl("gridview1.qml"));
1571 qApp->processEvents();
1573 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1574 QTRY_VERIFY(gridview != 0);
1576 // ensure content position is stable
1577 gridview->setContentY(0);
1578 model.moveItem(10, 0);
1579 QTRY_VERIFY(gridview->contentY() == 0);
1584 void tst_QQuickGridView::currentIndex()
1587 for (int i = 0; i < 60; i++)
1588 model.addItem("Item" + QString::number(i), QString::number(i));
1590 QQuickView *canvas = new QQuickView(0);
1591 canvas->setGeometry(0,0,240,320);
1594 QQmlContext *ctxt = canvas->rootContext();
1595 ctxt->setContextProperty("testModel", &model);
1597 QString filename(testFile("gridview-initCurrent.qml"));
1598 canvas->setSource(QUrl::fromLocalFile(filename));
1600 qApp->processEvents();
1602 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1603 QVERIFY(gridview != 0);
1604 QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1606 QQuickItem *contentItem = gridview->contentItem();
1607 QVERIFY(contentItem != 0);
1609 // current item should be third item
1610 QCOMPARE(gridview->currentIndex(), 35);
1611 QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1612 QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1613 QCOMPARE(gridview->contentY(), 400.0);
1615 gridview->moveCurrentIndexRight();
1616 QCOMPARE(gridview->currentIndex(), 36);
1617 gridview->moveCurrentIndexDown();
1618 QCOMPARE(gridview->currentIndex(), 39);
1619 gridview->moveCurrentIndexUp();
1620 QCOMPARE(gridview->currentIndex(), 36);
1621 gridview->moveCurrentIndexLeft();
1622 QCOMPARE(gridview->currentIndex(), 35);
1624 // wait until motion stops
1625 QTRY_VERIFY(gridview->verticalVelocity() == 0.0);
1628 gridview->setCurrentIndex(0);
1629 QCOMPARE(gridview->currentIndex(), 0);
1630 // confirm that the velocity is updated
1631 QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1633 gridview->moveCurrentIndexUp();
1634 QCOMPARE(gridview->currentIndex(), 0);
1636 gridview->moveCurrentIndexLeft();
1637 QCOMPARE(gridview->currentIndex(), 0);
1639 gridview->setCurrentIndex(model.count()-1);
1640 QCOMPARE(gridview->currentIndex(), model.count()-1);
1642 gridview->moveCurrentIndexRight();
1643 QCOMPARE(gridview->currentIndex(), model.count()-1);
1645 gridview->moveCurrentIndexDown();
1646 QCOMPARE(gridview->currentIndex(), model.count()-1);
1649 gridview->setWrapEnabled(true);
1651 gridview->setCurrentIndex(0);
1652 QCOMPARE(gridview->currentIndex(), 0);
1654 gridview->moveCurrentIndexLeft();
1655 QCOMPARE(gridview->currentIndex(), model.count()-1);
1657 qApp->processEvents();
1658 QTRY_COMPARE(gridview->contentY(), 880.0);
1660 gridview->moveCurrentIndexRight();
1661 QCOMPARE(gridview->currentIndex(), 0);
1663 QTRY_COMPARE(gridview->contentY(), 0.0);
1666 // footer should become visible if it is out of view, and then current index moves to the first row
1667 canvas->rootObject()->setProperty("showFooter", true);
1668 QTRY_VERIFY(gridview->footerItem());
1669 gridview->setCurrentIndex(model.count()-3);
1670 QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1671 gridview->setCurrentIndex(model.count()-2);
1672 QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1673 canvas->rootObject()->setProperty("showFooter", false);
1675 // header should become visible if it is out of view, and then current index moves to the last row
1676 canvas->rootObject()->setProperty("showHeader", true);
1677 QTRY_VERIFY(gridview->headerItem());
1678 gridview->setCurrentIndex(3);
1679 QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1680 gridview->setCurrentIndex(1);
1681 QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1682 canvas->rootObject()->setProperty("showHeader", false);
1686 canvas->requestActivateWindow();
1687 QTest::qWaitForWindowShown(canvas);
1688 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1690 gridview->setCurrentIndex(0);
1692 QTest::keyClick(canvas, Qt::Key_Down);
1693 QCOMPARE(gridview->currentIndex(), 3);
1695 QTest::keyClick(canvas, Qt::Key_Up);
1696 QCOMPARE(gridview->currentIndex(), 0);
1698 // hold down Key_Down
1699 for (int i=0; i<(model.count() / 3) - 1; i++) {
1700 QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
1701 QTRY_COMPARE(gridview->currentIndex(), i*3 + 3);
1703 QTest::keyRelease(canvas, Qt::Key_Down);
1704 QTRY_COMPARE(gridview->currentIndex(), 57);
1705 QTRY_COMPARE(gridview->contentY(), 880.0);
1708 for (int i=(model.count() / 3) - 1; i > 0; i--) {
1709 QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
1710 QTRY_COMPARE(gridview->currentIndex(), i*3 - 3);
1712 QTest::keyRelease(canvas, Qt::Key_Up);
1713 QTRY_COMPARE(gridview->currentIndex(), 0);
1714 QTRY_COMPARE(gridview->contentY(), 0.0);
1717 gridview->setFlow(QQuickGridView::TopToBottom);
1719 canvas->requestActivateWindow();
1720 QTest::qWaitForWindowShown(canvas);
1721 QVERIFY(qGuiApp->focusWindow() == canvas);
1722 qApp->processEvents();
1724 QTest::keyClick(canvas, Qt::Key_Right);
1725 QCOMPARE(gridview->currentIndex(), 5);
1727 QTest::keyClick(canvas, Qt::Key_Left);
1728 QCOMPARE(gridview->currentIndex(), 0);
1730 QTest::keyClick(canvas, Qt::Key_Down);
1731 QCOMPARE(gridview->currentIndex(), 1);
1733 QTest::keyClick(canvas, Qt::Key_Up);
1734 QCOMPARE(gridview->currentIndex(), 0);
1736 // hold down Key_Right
1737 for (int i=0; i<(model.count() / 5) - 1; i++) {
1738 QTest::simulateEvent(canvas, true, Qt::Key_Right, Qt::NoModifier, "", true);
1739 QTRY_COMPARE(gridview->currentIndex(), i*5 + 5);
1742 QTest::keyRelease(canvas, Qt::Key_Right);
1743 QTRY_COMPARE(gridview->currentIndex(), 55);
1744 QTRY_COMPARE(gridview->contentX(), 720.0);
1746 // hold down Key_Left
1747 for (int i=(model.count() / 5) - 1; i > 0; i--) {
1748 QTest::simulateEvent(canvas, true, Qt::Key_Left, Qt::NoModifier, "", true);
1749 QTRY_COMPARE(gridview->currentIndex(), i*5 - 5);
1751 QTest::keyRelease(canvas, Qt::Key_Left);
1752 QTRY_COMPARE(gridview->currentIndex(), 0);
1753 QTRY_COMPARE(gridview->contentX(), 0.0);
1756 // turn off auto highlight
1757 gridview->setHighlightFollowsCurrentItem(false);
1758 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1759 QVERIFY(gridview->highlightItem());
1760 qreal hlPosX = gridview->highlightItem()->x();
1761 qreal hlPosY = gridview->highlightItem()->y();
1763 gridview->setCurrentIndex(5);
1764 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1765 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1767 // insert item before currentIndex
1768 gridview->setCurrentIndex(28);
1769 model.insertItem(0, "Foo", "1111");
1770 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1772 // check removing highlight by setting currentIndex to -1;
1773 gridview->setCurrentIndex(-1);
1775 QCOMPARE(gridview->currentIndex(), -1);
1776 QVERIFY(!gridview->highlightItem());
1777 QVERIFY(!gridview->currentItem());
1779 gridview->setHighlightFollowsCurrentItem(true);
1781 gridview->setFlow(QQuickGridView::LeftToRight);
1782 gridview->setLayoutDirection(Qt::RightToLeft);
1784 canvas->requestActivateWindow();
1785 QTest::qWaitForWindowShown(canvas);
1786 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1787 qApp->processEvents();
1789 gridview->setCurrentIndex(35);
1791 QTest::keyClick(canvas, Qt::Key_Right);
1792 QCOMPARE(gridview->currentIndex(), 34);
1794 QTest::keyClick(canvas, Qt::Key_Down);
1795 QCOMPARE(gridview->currentIndex(), 37);
1797 QTest::keyClick(canvas, Qt::Key_Up);
1798 QCOMPARE(gridview->currentIndex(), 34);
1800 QTest::keyClick(canvas, Qt::Key_Left);
1801 QCOMPARE(gridview->currentIndex(), 35);
1804 // turn off auto highlight
1805 gridview->setHighlightFollowsCurrentItem(false);
1806 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1807 QVERIFY(gridview->highlightItem());
1808 hlPosX = gridview->highlightItem()->x();
1809 hlPosY = gridview->highlightItem()->y();
1811 gridview->setCurrentIndex(5);
1812 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1813 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1815 // insert item before currentIndex
1816 gridview->setCurrentIndex(28);
1817 model.insertItem(0, "Foo", "1111");
1818 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1820 // check removing highlight by setting currentIndex to -1;
1821 gridview->setCurrentIndex(-1);
1823 QCOMPARE(gridview->currentIndex(), -1);
1824 QVERIFY(!gridview->highlightItem());
1825 QVERIFY(!gridview->currentItem());
1827 // moving currentItem out of view should make it invisible
1828 gridview->setCurrentIndex(0);
1829 QTRY_VERIFY(gridview->currentItem()->isVisible());
1830 gridview->setContentY(200);
1831 QTRY_VERIFY(!gridview->currentItem()->isVisible());
1836 void tst_QQuickGridView::noCurrentIndex()
1839 for (int i = 0; i < 60; i++)
1840 model.addItem("Item" + QString::number(i), QString::number(i));
1842 QQuickView *canvas = new QQuickView(0);
1843 canvas->setGeometry(0,0,240,320);
1845 QQmlContext *ctxt = canvas->rootContext();
1846 ctxt->setContextProperty("testModel", &model);
1848 QString filename(testFile("gridview-noCurrent.qml"));
1849 canvas->setSource(QUrl::fromLocalFile(filename));
1851 qApp->processEvents();
1853 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1854 QVERIFY(gridview != 0);
1855 QQuickItem *contentItem = gridview->contentItem();
1856 QVERIFY(contentItem != 0);
1857 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1859 // current index should be -1
1860 QCOMPARE(gridview->currentIndex(), -1);
1861 QVERIFY(!gridview->currentItem());
1862 QVERIFY(!gridview->highlightItem());
1863 QCOMPARE(gridview->contentY(), 0.0);
1865 gridview->setCurrentIndex(5);
1866 QCOMPARE(gridview->currentIndex(), 5);
1867 QVERIFY(gridview->currentItem());
1868 QVERIFY(gridview->highlightItem());
1873 void tst_QQuickGridView::changeFlow()
1875 QQuickView *canvas = createView();
1878 for (int i = 0; i < 30; i++)
1879 model.addItem("Item" + QString::number(i), QString::number(i));
1881 QQmlContext *ctxt = canvas->rootContext();
1882 ctxt->setContextProperty("testModel", &model);
1883 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1884 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1886 canvas->setSource(testFileUrl("gridview1.qml"));
1887 qApp->processEvents();
1889 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1890 QTRY_VERIFY(gridview != 0);
1892 QQuickItem *contentItem = gridview->contentItem();
1893 QTRY_VERIFY(contentItem != 0);
1895 // Confirm items positioned correctly and indexes correct
1896 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1897 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1898 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1899 if (!item) qWarning() << "Item" << i << "not found";
1901 QTRY_COMPARE(item->x(), qreal((i%3)*80));
1902 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1903 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1904 QTRY_VERIFY(name != 0);
1905 QTRY_COMPARE(name->text(), model.name(i));
1906 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1907 QTRY_VERIFY(number != 0);
1908 QTRY_COMPARE(number->text(), model.number(i));
1911 ctxt->setContextProperty("testTopToBottom", QVariant(true));
1913 // Confirm items positioned correctly and indexes correct
1914 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1915 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1916 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1917 if (!item) qWarning() << "Item" << i << "not found";
1919 QTRY_COMPARE(item->x(), qreal((i/5)*80));
1920 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1921 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1922 QTRY_VERIFY(name != 0);
1923 QTRY_COMPARE(name->text(), model.name(i));
1924 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1925 QTRY_VERIFY(number != 0);
1926 QTRY_COMPARE(number->text(), model.number(i));
1929 ctxt->setContextProperty("testRightToLeft", QVariant(true));
1931 // Confirm items positioned correctly and indexes correct
1932 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1933 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1934 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1935 if (!item) qWarning() << "Item" << i << "not found";
1937 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
1938 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1939 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1940 QTRY_VERIFY(name != 0);
1941 QTRY_COMPARE(name->text(), model.name(i));
1942 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1943 QTRY_VERIFY(number != 0);
1944 QTRY_COMPARE(number->text(), model.number(i));
1946 gridview->setContentX(100);
1947 QTRY_COMPARE(gridview->contentX(), 100.);
1948 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1949 QTRY_COMPARE(gridview->contentX(), 0.);
1951 // Confirm items positioned correctly and indexes correct
1952 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1953 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1954 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1955 if (!item) qWarning() << "Item" << i << "not found";
1957 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
1958 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1959 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1960 QTRY_VERIFY(name != 0);
1961 QTRY_COMPARE(name->text(), model.name(i));
1962 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1963 QTRY_VERIFY(number != 0);
1964 QTRY_COMPARE(number->text(), model.number(i));
1970 void tst_QQuickGridView::defaultValues()
1973 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
1974 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1976 QTRY_VERIFY(obj != 0);
1977 QTRY_VERIFY(obj->model() == QVariant());
1978 QTRY_VERIFY(obj->delegate() == 0);
1979 QTRY_COMPARE(obj->currentIndex(), -1);
1980 QTRY_VERIFY(obj->currentItem() == 0);
1981 QTRY_COMPARE(obj->count(), 0);
1982 QTRY_VERIFY(obj->highlight() == 0);
1983 QTRY_VERIFY(obj->highlightItem() == 0);
1984 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
1985 QTRY_VERIFY(obj->flow() == 0);
1986 QTRY_COMPARE(obj->isWrapEnabled(), false);
1987 QTRY_COMPARE(obj->cacheBuffer(), 0);
1988 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
1989 QTRY_COMPARE(obj->cellHeight(), qreal(100));
1993 void tst_QQuickGridView::properties()
1996 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
1997 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1999 QTRY_VERIFY(obj != 0);
2000 QTRY_VERIFY(obj->model() != QVariant());
2001 QTRY_VERIFY(obj->delegate() != 0);
2002 QTRY_COMPARE(obj->currentIndex(), 0);
2003 QTRY_VERIFY(obj->currentItem() != 0);
2004 QTRY_COMPARE(obj->count(), 4);
2005 QTRY_VERIFY(obj->highlight() != 0);
2006 QTRY_VERIFY(obj->highlightItem() != 0);
2007 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
2008 QTRY_VERIFY(obj->flow() == 0);
2009 QTRY_COMPARE(obj->isWrapEnabled(), true);
2010 QTRY_COMPARE(obj->cacheBuffer(), 200);
2011 QTRY_COMPARE(obj->cellWidth(), qreal(100));
2012 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2016 void tst_QQuickGridView::propertyChanges()
2018 QQuickView *canvas = createView();
2019 QTRY_VERIFY(canvas);
2020 canvas->setSource(testFileUrl("propertychangestest.qml"));
2022 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2023 QTRY_VERIFY(gridView);
2025 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
2026 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
2027 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
2028 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
2030 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2031 QTRY_COMPARE(gridView->cacheBuffer(), 10);
2032 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
2034 gridView->setWrapEnabled(false);
2035 gridView->setCacheBuffer(3);
2036 gridView->setFlow(QQuickGridView::TopToBottom);
2038 QTRY_COMPARE(gridView->isWrapEnabled(), false);
2039 QTRY_COMPARE(gridView->cacheBuffer(), 3);
2040 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
2042 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2043 QTRY_COMPARE(cacheBufferSpy.count(),1);
2044 QTRY_COMPARE(flowSpy.count(),1);
2046 gridView->setWrapEnabled(false);
2047 gridView->setCacheBuffer(3);
2048 gridView->setFlow(QQuickGridView::TopToBottom);
2050 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2051 QTRY_COMPARE(cacheBufferSpy.count(),1);
2052 QTRY_COMPARE(flowSpy.count(),1);
2054 gridView->setFlow(QQuickGridView::LeftToRight);
2055 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
2057 gridView->setWrapEnabled(true);
2058 gridView->setCacheBuffer(5);
2059 gridView->setLayoutDirection(Qt::RightToLeft);
2061 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2062 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2063 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2065 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2066 QTRY_COMPARE(cacheBufferSpy.count(),2);
2067 QTRY_COMPARE(layoutSpy.count(),1);
2068 QTRY_COMPARE(flowSpy.count(),2);
2070 gridView->setWrapEnabled(true);
2071 gridView->setCacheBuffer(5);
2072 gridView->setLayoutDirection(Qt::RightToLeft);
2074 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2075 QTRY_COMPARE(cacheBufferSpy.count(),2);
2076 QTRY_COMPARE(layoutSpy.count(),1);
2077 QTRY_COMPARE(flowSpy.count(),2);
2079 gridView->setFlow(QQuickGridView::TopToBottom);
2080 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
2081 QTRY_COMPARE(flowSpy.count(),3);
2083 gridView->setFlow(QQuickGridView::TopToBottom);
2084 QTRY_COMPARE(flowSpy.count(),3);
2089 void tst_QQuickGridView::componentChanges()
2091 QQuickView *canvas = createView();
2092 QTRY_VERIFY(canvas);
2093 canvas->setSource(testFileUrl("propertychangestest.qml"));
2095 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2096 QTRY_VERIFY(gridView);
2098 QQmlComponent component(canvas->engine());
2099 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2101 QQmlComponent delegateComponent(canvas->engine());
2102 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2104 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2105 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2106 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2107 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2108 QSignalSpy headerItemSpy(gridView, SIGNAL(headerItemChanged()));
2109 QSignalSpy footerItemSpy(gridView, SIGNAL(footerItemChanged()));
2111 gridView->setHighlight(&component);
2112 gridView->setDelegate(&delegateComponent);
2113 gridView->setHeader(&component);
2114 gridView->setFooter(&component);
2116 QTRY_COMPARE(gridView->highlight(), &component);
2117 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2118 QTRY_COMPARE(gridView->header(), &component);
2119 QTRY_COMPARE(gridView->footer(), &component);
2121 QVERIFY(gridView->headerItem());
2122 QVERIFY(gridView->footerItem());
2124 QTRY_COMPARE(highlightSpy.count(),1);
2125 QTRY_COMPARE(delegateSpy.count(),1);
2126 QTRY_COMPARE(headerSpy.count(),1);
2127 QTRY_COMPARE(footerSpy.count(),1);
2128 QTRY_COMPARE(headerItemSpy.count(),1);
2129 QTRY_COMPARE(footerItemSpy.count(),1);
2131 gridView->setHighlight(&component);
2132 gridView->setDelegate(&delegateComponent);
2133 gridView->setHeader(&component);
2134 gridView->setFooter(&component);
2136 QTRY_COMPARE(highlightSpy.count(),1);
2137 QTRY_COMPARE(delegateSpy.count(),1);
2138 QTRY_COMPARE(headerSpy.count(),1);
2139 QTRY_COMPARE(footerSpy.count(),1);
2140 QTRY_COMPARE(headerItemSpy.count(),1);
2141 QTRY_COMPARE(footerItemSpy.count(),1);
2146 void tst_QQuickGridView::modelChanges()
2148 QQuickView *canvas = createView();
2149 QTRY_VERIFY(canvas);
2150 canvas->setSource(testFileUrl("propertychangestest.qml"));
2152 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2153 QTRY_VERIFY(gridView);
2155 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2156 QTRY_VERIFY(alternateModel);
2157 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2158 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2160 gridView->setModel(modelVariant);
2161 QTRY_COMPARE(gridView->model(), modelVariant);
2162 QTRY_COMPARE(modelSpy.count(),1);
2164 gridView->setModel(modelVariant);
2165 QTRY_COMPARE(modelSpy.count(),1);
2167 gridView->setModel(QVariant());
2168 QTRY_COMPARE(modelSpy.count(),2);
2172 void tst_QQuickGridView::positionViewAtIndex()
2174 QQuickView *canvas = createView();
2177 for (int i = 0; i < 40; i++)
2178 model.addItem("Item" + QString::number(i), "");
2180 QQmlContext *ctxt = canvas->rootContext();
2181 ctxt->setContextProperty("testModel", &model);
2182 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2183 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2185 canvas->setSource(testFileUrl("gridview1.qml"));
2187 qApp->processEvents();
2189 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2190 QTRY_VERIFY(gridview != 0);
2191 QQuickItem *contentItem = gridview->contentItem();
2192 QTRY_VERIFY(contentItem != 0);
2193 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2195 // Confirm items positioned correctly
2196 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2197 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2198 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2199 if (!item) qWarning() << "Item" << i << "not found";
2201 QTRY_COMPARE(item->x(), (i%3)*80.);
2202 QTRY_COMPARE(item->y(), (i/3)*60.);
2205 // Position on a currently visible item
2206 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2207 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2208 QTRY_COMPARE(gridview->contentY(), 60.);
2210 // Confirm items positioned correctly
2211 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2212 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2213 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2214 if (!item) qWarning() << "Item" << i << "not found";
2216 QTRY_COMPARE(item->x(), (i%3)*80.);
2217 QTRY_COMPARE(item->y(), (i/3)*60.);
2220 // Position on an item beyond the visible items
2221 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2222 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2223 QTRY_COMPARE(gridview->contentY(), 420.);
2225 // Confirm items positioned correctly
2226 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2227 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2228 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2229 if (!item) qWarning() << "Item" << i << "not found";
2231 QTRY_COMPARE(item->x(), (i%3)*80.);
2232 QTRY_COMPARE(item->y(), (i/3)*60.);
2235 // Position on an item that would leave empty space if positioned at the top
2236 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2237 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2238 QTRY_COMPARE(gridview->contentY(), 520.);
2240 // Confirm items positioned correctly
2241 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2242 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2243 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2244 if (!item) qWarning() << "Item" << i << "not found";
2246 QTRY_COMPARE(item->x(), (i%3)*80.);
2247 QTRY_COMPARE(item->y(), (i/3)*60.);
2250 // Position at the beginning again
2251 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2252 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2253 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2254 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2255 QTRY_COMPARE(gridview->contentY(), 0.);
2257 // Confirm items positioned correctly
2258 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2259 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2260 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2261 if (!item) qWarning() << "Item" << i << "not found";
2263 QTRY_COMPARE(item->x(), (i%3)*80.);
2264 QTRY_COMPARE(item->y(), (i/3)*60.);
2268 gridview->positionViewAtIndex(30, QQuickGridView::End);
2269 QTRY_COMPARE(gridview->contentY(), 340.);
2271 // Position in Center
2272 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2273 QTRY_COMPARE(gridview->contentY(), 170.);
2275 // Ensure at least partially visible
2276 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2277 QTRY_COMPARE(gridview->contentY(), 170.);
2279 gridview->setContentY(302);
2280 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2281 QTRY_COMPARE(gridview->contentY(), 302.);
2283 gridview->setContentY(360);
2284 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2285 QTRY_COMPARE(gridview->contentY(), 300.);
2287 gridview->setContentY(60);
2288 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2289 QTRY_COMPARE(gridview->contentY(), 60.);
2291 gridview->setContentY(20);
2292 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2293 QTRY_COMPARE(gridview->contentY(), 100.);
2295 // Ensure completely visible
2296 gridview->setContentY(120);
2297 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2298 QTRY_COMPARE(gridview->contentY(), 120.);
2300 gridview->setContentY(302);
2301 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2302 QTRY_COMPARE(gridview->contentY(), 300.);
2304 gridview->setContentY(60);
2305 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2306 QTRY_COMPARE(gridview->contentY(), 100.);
2308 // Test for Top To Bottom layout
2309 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2311 // Confirm items positioned correctly
2312 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2313 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2314 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2315 if (!item) qWarning() << "Item" << i << "not found";
2317 QTRY_COMPARE(item->x(), (i/5)*80.);
2318 QTRY_COMPARE(item->y(), (i%5)*60.);
2322 gridview->positionViewAtIndex(30, QQuickGridView::End);
2323 QTRY_COMPARE(gridview->contentX(), 320.);
2324 QTRY_COMPARE(gridview->contentY(), 0.);
2326 // Position in Center
2327 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2328 QTRY_COMPARE(gridview->contentX(), 160.);
2330 // Ensure at least partially visible
2331 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2332 QTRY_COMPARE(gridview->contentX(), 160.);
2334 gridview->setContentX(170);
2335 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2336 QTRY_COMPARE(gridview->contentX(), 170.);
2338 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2339 QTRY_COMPARE(gridview->contentX(), 320.);
2341 gridview->setContentX(170);
2342 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2343 QTRY_COMPARE(gridview->contentX(), 240.);
2345 // positionViewAtBeginning
2346 gridview->positionViewAtBeginning();
2347 QTRY_COMPARE(gridview->contentX(), 0.);
2349 gridview->setContentX(80);
2350 canvas->rootObject()->setProperty("showHeader", true);
2351 gridview->positionViewAtBeginning();
2352 QTRY_COMPARE(gridview->contentX(), -30.);
2354 // positionViewAtEnd
2355 gridview->positionViewAtEnd();
2356 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2358 gridview->setContentX(80);
2359 canvas->rootObject()->setProperty("showFooter", true);
2360 gridview->positionViewAtEnd();
2361 QTRY_COMPARE(gridview->contentX(), 430.);
2363 // set current item to outside visible view, position at beginning
2364 // and ensure highlight moves to current item
2365 gridview->setCurrentIndex(6);
2366 gridview->positionViewAtBeginning();
2367 QTRY_COMPARE(gridview->contentX(), -30.);
2368 QVERIFY(gridview->highlightItem());
2369 QCOMPARE(gridview->highlightItem()->x(), 80.);
2374 void tst_QQuickGridView::snapping()
2376 QQuickView *canvas = createView();
2379 for (int i = 0; i < 40; i++)
2380 model.addItem("Item" + QString::number(i), "");
2382 QQmlContext *ctxt = canvas->rootContext();
2383 ctxt->setContextProperty("testModel", &model);
2384 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2385 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2387 canvas->setSource(testFileUrl("gridview1.qml"));
2388 qApp->processEvents();
2390 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2391 QTRY_VERIFY(gridview != 0);
2393 gridview->setHeight(220);
2394 QCOMPARE(gridview->height(), 220.);
2396 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2397 QCOMPARE(gridview->contentY(), 80.);
2399 gridview->setContentY(0);
2400 QCOMPARE(gridview->contentY(), 0.);
2402 gridview->setSnapMode(QQuickGridView::SnapToRow);
2403 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2405 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2406 QCOMPARE(gridview->contentY(), 60.);
2408 gridview->positionViewAtIndex(15, QQuickGridView::End);
2409 QCOMPARE(gridview->contentY(), 120.);
2415 void tst_QQuickGridView::mirroring()
2417 QQuickView *canvasA = createView();
2418 canvasA->setSource(testFileUrl("mirroring.qml"));
2419 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2420 QTRY_VERIFY(gridviewA != 0);
2422 QQuickView *canvasB = createView();
2423 canvasB->setSource(testFileUrl("mirroring.qml"));
2424 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2425 QTRY_VERIFY(gridviewA != 0);
2426 qApp->processEvents();
2428 QList<QString> objectNames;
2429 objectNames << "item1" << "item2"; // << "item3"
2431 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2432 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2433 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2436 foreach (const QString objectName, objectNames)
2437 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2439 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2440 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2443 foreach (const QString objectName, objectNames)
2444 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2446 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2447 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2448 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2450 // LTR != LTR+mirror
2451 foreach (const QString objectName, objectNames)
2452 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2454 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2456 // RTL == LTR+mirror
2457 foreach (const QString objectName, objectNames)
2458 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2460 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2462 // RTL != RTL+mirror
2463 foreach (const QString objectName, objectNames)
2464 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2466 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2468 // LTR == RTL+mirror
2469 foreach (const QString objectName, objectNames)
2470 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2476 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2478 QQuickView *canvas = createView();
2481 for (int i = 0; i < 40; i++)
2482 model.addItem("Item" + QString::number(i), "");
2484 QQmlContext *ctxt = canvas->rootContext();
2485 ctxt->setContextProperty("testModel", &model);
2486 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2487 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2489 canvas->setSource(testFileUrl("gridview1.qml"));
2490 qApp->processEvents();
2492 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2493 QTRY_VERIFY(gridview != 0);
2495 QQuickItem *contentItem = gridview->contentItem();
2496 QTRY_VERIFY(contentItem != 0);
2498 // Confirm items positioned correctly
2499 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2500 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2501 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2502 if (!item) qWarning() << "Item" << i << "not found";
2504 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2505 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2508 // Position on a currently visible item
2509 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2510 QTRY_COMPARE(gridview->contentX(), -320.);
2512 // Confirm items positioned correctly
2513 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2514 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2515 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2516 if (!item) qWarning() << "Item" << i << "not found";
2518 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2519 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2522 // Position on an item beyond the visible items
2523 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2524 QTRY_COMPARE(gridview->contentX(), -560.);
2526 // Confirm items positioned correctly
2527 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2528 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2529 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2530 if (!item) qWarning() << "Item" << i << "not found";
2532 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2533 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2536 // Position on an item that would leave empty space if positioned at the top
2537 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2538 QTRY_COMPARE(gridview->contentX(), -640.);
2540 // Confirm items positioned correctly
2541 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2542 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2543 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2544 if (!item) qWarning() << "Item" << i << "not found";
2546 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2547 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2550 // Position at the beginning again
2551 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2552 QTRY_COMPARE(gridview->contentX(), -240.);
2554 // Confirm items positioned correctly
2555 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2556 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2557 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2558 if (!item) qWarning() << "Item" << i << "not found";
2560 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2561 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2565 gridview->positionViewAtIndex(30, QQuickGridView::End);
2566 QTRY_COMPARE(gridview->contentX(), -560.);
2568 // Position in Center
2569 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2570 QTRY_COMPARE(gridview->contentX(), -400.);
2572 // Ensure at least partially visible
2573 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2574 QTRY_COMPARE(gridview->contentX(), -400.);
2576 gridview->setContentX(-555.);
2577 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2578 QTRY_COMPARE(gridview->contentX(), -555.);
2580 gridview->setContentX(-239);
2581 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2582 QTRY_COMPARE(gridview->contentX(), -320.);
2584 gridview->setContentX(-239);
2585 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2586 QTRY_COMPARE(gridview->contentX(), -400.);
2588 gridview->setContentX(-640);
2589 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2590 QTRY_COMPARE(gridview->contentX(), -560.);
2592 // Ensure completely visible
2593 gridview->setContentX(-400);
2594 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2595 QTRY_COMPARE(gridview->contentX(), -400.);
2597 gridview->setContentX(-315);
2598 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2599 QTRY_COMPARE(gridview->contentX(), -320.);
2601 gridview->setContentX(-640);
2602 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2603 QTRY_COMPARE(gridview->contentX(), -560.);
2608 void tst_QQuickGridView::resetModel()
2610 QQuickView *canvas = createView();
2612 QStringList strings;
2613 strings << "one" << "two" << "three";
2614 QStringListModel model(strings);
2616 QQmlContext *ctxt = canvas->rootContext();
2617 ctxt->setContextProperty("testModel", &model);
2619 canvas->setSource(testFileUrl("displaygrid.qml"));
2621 qApp->processEvents();
2623 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2624 QTRY_VERIFY(gridview != 0);
2625 QQuickItem *contentItem = gridview->contentItem();
2626 QTRY_VERIFY(contentItem != 0);
2627 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2629 QTRY_COMPARE(gridview->count(), model.rowCount());
2631 for (int i = 0; i < model.rowCount(); ++i) {
2632 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2633 QTRY_VERIFY(display != 0);
2634 QTRY_COMPARE(display->text(), strings.at(i));
2638 strings << "four" << "five" << "six" << "seven";
2639 model.setStringList(strings);
2641 QTRY_COMPARE(gridview->count(), model.rowCount());
2643 for (int i = 0; i < model.rowCount(); ++i) {
2644 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2645 QTRY_VERIFY(display != 0);
2646 QTRY_COMPARE(display->text(), strings.at(i));
2652 void tst_QQuickGridView::enforceRange()
2654 QQuickView *canvas = createView();
2657 for (int i = 0; i < 30; i++)
2658 model.addItem("Item" + QString::number(i), "");
2660 QQmlContext *ctxt = canvas->rootContext();
2661 ctxt->setContextProperty("testModel", &model);
2662 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2663 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2665 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2667 qApp->processEvents();
2668 QVERIFY(canvas->rootObject() != 0);
2670 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2671 QTRY_VERIFY(gridview != 0);
2673 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2674 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2675 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2676 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2678 QQuickItem *contentItem = gridview->contentItem();
2679 QTRY_VERIFY(contentItem != 0);
2681 // view should be positioned at the top of the range.
2682 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2684 QTRY_COMPARE(gridview->contentY(), -100.0);
2686 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2687 QTRY_VERIFY(name != 0);
2688 QTRY_COMPARE(name->text(), model.name(0));
2689 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2690 QTRY_VERIFY(number != 0);
2691 QTRY_COMPARE(number->text(), model.number(0));
2693 // Check currentIndex is updated when contentItem moves
2694 gridview->setContentY(0);
2695 QTRY_COMPARE(gridview->currentIndex(), 2);
2697 gridview->setCurrentIndex(5);
2698 QTRY_COMPARE(gridview->contentY(), 100.);
2701 for (int i = 0; i < 5; i++)
2702 model2.addItem("Item" + QString::number(i), "");
2704 ctxt->setContextProperty("testModel", &model2);
2705 QCOMPARE(gridview->count(), 5);
2710 void tst_QQuickGridView::enforceRange_rightToLeft()
2712 QQuickView *canvas = createView();
2715 for (int i = 0; i < 30; i++)
2716 model.addItem("Item" + QString::number(i), "");
2718 QQmlContext *ctxt = canvas->rootContext();
2719 ctxt->setContextProperty("testModel", &model);
2720 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2721 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2723 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2724 qApp->processEvents();
2725 QVERIFY(canvas->rootObject() != 0);
2727 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2728 QTRY_VERIFY(gridview != 0);
2730 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2731 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2732 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2734 QQuickItem *contentItem = gridview->contentItem();
2735 QVERIFY(contentItem != 0);
2737 // view should be positioned at the top of the range.
2738 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2740 QTRY_COMPARE(gridview->contentX(), -140.);
2741 QTRY_COMPARE(gridview->contentY(), 0.0);
2743 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2744 QTRY_VERIFY(name != 0);
2745 QTRY_COMPARE(name->text(), model.name(0));
2746 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2747 QTRY_VERIFY(number != 0);
2748 QTRY_COMPARE(number->text(), model.number(0));
2750 // Check currentIndex is updated when contentItem moves
2751 gridview->setContentX(-240);
2752 QTRY_COMPARE(gridview->currentIndex(), 3);
2754 gridview->setCurrentIndex(7);
2755 QTRY_COMPARE(gridview->contentX(), -340.);
2756 QTRY_COMPARE(gridview->contentY(), 0.0);
2759 for (int i = 0; i < 5; i++)
2760 model2.addItem("Item" + QString::number(i), "");
2762 ctxt->setContextProperty("testModel", &model2);
2763 QCOMPARE(gridview->count(), 5);
2768 void tst_QQuickGridView::QTBUG_8456()
2770 QQuickView *canvas = createView();
2772 canvas->setSource(testFileUrl("setindex.qml"));
2773 qApp->processEvents();
2775 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2776 QTRY_VERIFY(gridview != 0);
2778 QTRY_COMPARE(gridview->currentIndex(), 0);
2783 void tst_QQuickGridView::manualHighlight()
2785 QQuickView *canvas = createView();
2787 QString filename(testFile("manual-highlight.qml"));
2788 canvas->setSource(QUrl::fromLocalFile(filename));
2790 qApp->processEvents();
2792 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2793 QTRY_VERIFY(gridview != 0);
2795 QQuickItem *contentItem = gridview->contentItem();
2796 QTRY_VERIFY(contentItem != 0);
2798 QTRY_COMPARE(gridview->currentIndex(), 0);
2799 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2800 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2801 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2803 gridview->setCurrentIndex(2);
2805 QTRY_COMPARE(gridview->currentIndex(), 2);
2806 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2807 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2808 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2810 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
2812 QTRY_COMPARE(gridview->currentIndex(), 2);
2813 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2814 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2815 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2817 gridview->setFlow(QQuickGridView::TopToBottom);
2818 QTRY_COMPARE(gridview->flow(), QQuickGridView::TopToBottom);
2820 gridview->setCurrentIndex(0);
2821 QTRY_COMPARE(gridview->currentIndex(), 0);
2822 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2823 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2824 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2830 void tst_QQuickGridView::footer()
2832 QFETCH(QQuickGridView::Flow, flow);
2833 QFETCH(Qt::LayoutDirection, layoutDirection);
2834 QFETCH(QPointF, initialFooterPos);
2835 QFETCH(QPointF, changedFooterPos);
2836 QFETCH(QPointF, initialContentPos);
2837 QFETCH(QPointF, changedContentPos);
2838 QFETCH(QPointF, firstDelegatePos);
2839 QFETCH(QPointF, resizeContentPos);
2841 QQuickView *canvas = getView();
2845 for (int i = 0; i < 7; i++)
2846 model.addItem("Item" + QString::number(i), "");
2848 QQmlContext *ctxt = canvas->rootContext();
2849 ctxt->setContextProperty("testModel", &model);
2851 canvas->setSource(testFileUrl("footer.qml"));
2852 qApp->processEvents();
2854 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2855 QTRY_VERIFY(gridview != 0);
2856 gridview->setFlow(flow);
2857 gridview->setLayoutDirection(layoutDirection);
2859 QQuickItem *contentItem = gridview->contentItem();
2860 QTRY_VERIFY(contentItem != 0);
2862 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
2865 QVERIFY(footer == gridview->footerItem());
2867 QCOMPARE(footer->pos(), initialFooterPos);
2868 QCOMPARE(footer->width(), 100.);
2869 QCOMPARE(footer->height(), 30.);
2870 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2872 if (flow == QQuickGridView::LeftToRight)
2873 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + footer->height());
2875 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + footer->width());
2877 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2879 QCOMPARE(item->pos(), firstDelegatePos);
2881 if (flow == QQuickGridView::LeftToRight) {
2882 // shrink by one row
2883 model.removeItem(2);
2884 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
2886 // shrink by one column
2887 model.removeItem(2);
2888 model.removeItem(3);
2889 if (layoutDirection == Qt::LeftToRight)
2890 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
2892 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
2898 QPointF posWhenNoItems(0, 0);
2899 if (layoutDirection == Qt::RightToLeft)
2900 posWhenNoItems.setX(flow == QQuickGridView::LeftToRight ? gridview->width() - footer->width() : -footer->width());
2901 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2903 // if header is present, it's at a negative pos, so the footer should not move
2904 canvas->rootObject()->setProperty("showHeader", true);
2905 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
2906 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2907 canvas->rootObject()->setProperty("showHeader", false);
2910 for (int i = 0; i < 30; i++)
2911 model.addItem("Item" + QString::number(i), "");
2913 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
2914 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
2916 QCOMPARE(footerItemSpy.count(), 1);
2918 footer = findItem<QQuickText>(contentItem, "footer");
2920 footer = findItem<QQuickText>(contentItem, "footer2");
2923 QVERIFY(footer == gridview->footerItem());
2925 QCOMPARE(footer->pos(), changedFooterPos);
2926 QCOMPARE(footer->width(), 50.);
2927 QCOMPARE(footer->height(), 20.);
2928 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2930 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2932 QCOMPARE(item->pos(), firstDelegatePos);
2934 gridview->positionViewAtEnd();
2935 footer->setHeight(10);
2936 footer->setWidth(40);
2937 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
2939 releaseView(canvas);
2942 void tst_QQuickGridView::footer_data()
2944 QTest::addColumn<QQuickGridView::Flow>("flow");
2945 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2946 QTest::addColumn<QPointF>("initialFooterPos");
2947 QTest::addColumn<QPointF>("changedFooterPos");
2948 QTest::addColumn<QPointF>("initialContentPos");
2949 QTest::addColumn<QPointF>("changedContentPos");
2950 QTest::addColumn<QPointF>("firstDelegatePos");
2951 QTest::addColumn<QPointF>("resizeContentPos");
2953 // footer1 = 100 x 30
2954 // footer2 = 50 x 20
2957 // view height = 320
2959 // footer below items, bottom left
2960 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
2961 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
2962 << QPointF(0, 10 * 60) // 30 items = 10 rows
2966 << QPointF(0, 10 * 60 - 320 + 10);
2968 // footer below items, bottom right
2969 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
2970 << QPointF(240 - 100, 3 * 60)
2971 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
2974 << QPointF(240 - 80, 0)
2975 << QPointF(0, 10 * 60 - 320 + 10);
2977 // footer to right of items
2978 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
2979 << QPointF(2 * 80, 0) // 2 columns, cell width 80
2980 << QPointF(6 * 80, 0) // 30 items = 6 columns
2984 << QPointF(6 * 80 - 240 + 40, 0);
2986 // footer to left of items
2987 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
2988 << QPointF(-(2 * 80) - 100, 0)
2989 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
2991 << QPointF(-240, 0) // unchanged, footer change doesn't change content pos
2993 << QPointF(-(6 * 80) - 40, 0);
2996 void tst_QQuickGridView::header()
2998 QFETCH(QQuickGridView::Flow, flow);
2999 QFETCH(Qt::LayoutDirection, layoutDirection);
3000 QFETCH(QPointF, initialHeaderPos);
3001 QFETCH(QPointF, changedHeaderPos);
3002 QFETCH(QPointF, initialContentPos);
3003 QFETCH(QPointF, changedContentPos);
3004 QFETCH(QPointF, firstDelegatePos);
3005 QFETCH(QPointF, resizeContentPos);
3008 for (int i = 0; i < 30; i++)
3009 model.addItem("Item" + QString::number(i), "");
3011 QQuickView *canvas = getView();
3012 canvas->rootContext()->setContextProperty("testModel", &model);
3013 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3014 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3015 canvas->setSource(testFileUrl("header.qml"));
3017 qApp->processEvents();
3019 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3020 QTRY_VERIFY(gridview != 0);
3021 gridview->setFlow(flow);
3022 gridview->setLayoutDirection(layoutDirection);
3023 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3025 QQuickItem *contentItem = gridview->contentItem();
3026 QTRY_VERIFY(contentItem != 0);
3028 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3031 QVERIFY(header == gridview->headerItem());
3033 QCOMPARE(header->pos(), initialHeaderPos);
3034 QCOMPARE(header->width(), 100.);
3035 QCOMPARE(header->height(), 30.);
3036 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3038 if (flow == QQuickGridView::LeftToRight)
3039 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + header->height());
3041 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + header->width());
3043 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3045 QCOMPARE(item->pos(), firstDelegatePos);
3048 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3049 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3051 for (int i = 0; i < 30; i++)
3052 model.addItem("Item" + QString::number(i), "");
3054 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
3055 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3057 QCOMPARE(headerItemSpy.count(), 1);
3059 header = findItem<QQuickText>(contentItem, "header");
3061 header = findItem<QQuickText>(contentItem, "header2");
3064 QVERIFY(header == gridview->headerItem());
3066 QCOMPARE(header->pos(), changedHeaderPos);
3067 QCOMPARE(header->width(), 50.);
3068 QCOMPARE(header->height(), 20.);
3069 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3071 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3073 QCOMPARE(item->pos(), firstDelegatePos);
3075 header->setHeight(10);
3076 header->setWidth(40);
3077 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3079 releaseView(canvas);
3082 // QTBUG-21207 header should become visible if view resizes from initial empty size
3085 canvas->rootContext()->setContextProperty("testModel", &model);
3086 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3087 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3088 canvas->setSource(testFileUrl("header.qml"));
3090 qApp->processEvents();
3092 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3093 QTRY_VERIFY(gridview != 0);
3094 gridview->setFlow(flow);
3095 gridview->setLayoutDirection(layoutDirection);
3096 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3098 gridview->setWidth(240);
3099 gridview->setHeight(320);
3100 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3101 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3103 releaseView(canvas);
3106 void tst_QQuickGridView::header_data()
3108 QTest::addColumn<QQuickGridView::Flow>("flow");
3109 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3110 QTest::addColumn<QPointF>("initialHeaderPos");
3111 QTest::addColumn<QPointF>("changedHeaderPos");
3112 QTest::addColumn<QPointF>("initialContentPos");
3113 QTest::addColumn<QPointF>("changedContentPos");
3114 QTest::addColumn<QPointF>("firstDelegatePos");
3115 QTest::addColumn<QPointF>("resizeContentPos");
3117 // header1 = 100 x 30
3118 // header2 = 50 x 20
3122 // header above items, top left
3123 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
3131 // header above items, top right
3132 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
3133 << QPointF(240 - 100, -30)
3134 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3140 // header to left of items
3141 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
3149 // header to right of items
3150 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
3153 << QPointF(-(240 - 100), 0)
3154 << QPointF(-(240 - 50), 0)
3156 << QPointF(-(240 - 40), 0);
3159 class GVAccessor : public QQuickGridView
3162 qreal minY() const { return minYExtent(); }
3163 qreal maxY() const { return maxYExtent(); }
3164 qreal minX() const { return minXExtent(); }
3165 qreal maxX() const { return maxXExtent(); }
3168 void tst_QQuickGridView::headerFooter()
3172 QQuickView *canvas = createView();
3175 QQmlContext *ctxt = canvas->rootContext();
3176 ctxt->setContextProperty("testModel", &model);
3178 canvas->setSource(testFileUrl("headerfooter.qml"));
3179 qApp->processEvents();
3181 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3182 QTRY_VERIFY(gridview != 0);
3184 QQuickItem *contentItem = gridview->contentItem();
3185 QTRY_VERIFY(contentItem != 0);
3187 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3189 QCOMPARE(header->y(), -header->height());
3191 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3193 QCOMPARE(footer->y(), 0.);
3195 QCOMPARE(static_cast<GVAccessor*>(gridview)->minY(), header->height());
3196 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxY(), header->height());
3202 QQuickView *canvas = createView();
3205 QQmlContext *ctxt = canvas->rootContext();
3206 ctxt->setContextProperty("testModel", &model);
3208 canvas->setSource(testFileUrl("headerfooter.qml"));
3209 canvas->rootObject()->setProperty("horizontal", true);
3210 qApp->processEvents();
3212 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3213 QTRY_VERIFY(gridview != 0);
3215 QQuickItem *contentItem = gridview->contentItem();
3216 QTRY_VERIFY(contentItem != 0);
3218 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3220 QCOMPARE(header->x(), -header->width());
3222 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3224 QCOMPARE(footer->x(), 0.);
3226 QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), header->width());
3227 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), header->width());
3233 QQuickView *canvas = createView();
3236 QQmlContext *ctxt = canvas->rootContext();
3237 ctxt->setContextProperty("testModel", &model);
3239 canvas->setSource(testFileUrl("headerfooter.qml"));
3240 canvas->rootObject()->setProperty("horizontal", true);
3241 canvas->rootObject()->setProperty("rtl", true);
3242 qApp->processEvents();
3244 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3245 QTRY_VERIFY(gridview != 0);
3247 QQuickItem *contentItem = gridview->contentItem();
3248 QTRY_VERIFY(contentItem != 0);
3250 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3252 QCOMPARE(header->x(), 0.);
3254 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3256 QCOMPARE(footer->x(), -footer->width());
3258 QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), 240. - header->width());
3259 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), 240. - header->width());
3265 QQuickView *canvas = createView();
3268 for (int i = 0; i < 6; i++)
3269 model.addItem("Item" + QString::number(i), "");
3270 QQmlContext *ctxt = canvas->rootContext();
3271 ctxt->setContextProperty("testModel", &model);
3273 canvas->setSource(testFileUrl("headerfooter.qml"));
3274 qApp->processEvents();
3276 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3277 QTRY_VERIFY(gridview != 0);
3279 QQuickItem *contentItem = gridview->contentItem();
3280 QTRY_VERIFY(contentItem != 0);
3282 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3284 QCOMPARE(header->y(), -header->height());
3286 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3288 QCOMPARE(footer->y(), 80.*2);
3292 header = findItem<QQuickItem>(contentItem, "header");
3294 QCOMPARE(header->y(), -header->height());
3296 footer = findItem<QQuickItem>(contentItem, "footer");
3298 QCOMPARE(footer->y(), 80.*2);
3304 void tst_QQuickGridView::resizeViewAndRepaint()
3306 QQuickView *canvas = createView();
3310 for (int i = 0; i < 40; i++)
3311 model.addItem("Item" + QString::number(i), "");
3313 QQmlContext *ctxt = canvas->rootContext();
3314 ctxt->setContextProperty("testModel", &model);
3315 ctxt->setContextProperty("initialWidth", 240);
3316 ctxt->setContextProperty("initialHeight", 100);
3318 canvas->setSource(testFileUrl("resizeview.qml"));
3320 qApp->processEvents();
3322 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3323 QTRY_VERIFY(gridview != 0);
3324 QQuickItem *contentItem = gridview->contentItem();
3325 QTRY_VERIFY(contentItem != 0);
3326 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3328 // item at index 10 should not be currently visible
3329 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3331 gridview->setHeight(320);
3332 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3334 gridview->setHeight(100);
3335 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3337 // Ensure we handle -ve sizes
3338 gridview->setHeight(-100);
3339 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3341 gridview->setCacheBuffer(120);
3342 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3344 // ensure items in cache become visible
3345 gridview->setHeight(120);
3346 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3348 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3349 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3350 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3351 if (!item) qWarning() << "Item" << i << "not found";
3353 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3354 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3355 QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3358 // ensure items outside view become invisible
3359 gridview->setHeight(60);
3360 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3362 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3363 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3364 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3365 if (!item) qWarning() << "Item" << i << "not found";
3367 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3368 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3369 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3375 void tst_QQuickGridView::changeColumnCount()
3378 for (int i = 0; i < 40; i++)
3379 model.addItem("Item" + QString::number(i), "");
3381 QQuickView *canvas = createView();
3382 QQmlContext *ctxt = canvas->rootContext();
3383 ctxt->setContextProperty("testModel", &model);
3384 ctxt->setContextProperty("initialWidth", 100);
3385 ctxt->setContextProperty("initialHeight", 320);
3386 canvas->setSource(testFileUrl("resizeview.qml"));
3388 qApp->processEvents();
3390 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3391 QTRY_VERIFY(gridview != 0);
3392 QQuickItem *contentItem = gridview->contentItem();
3393 QTRY_VERIFY(contentItem != 0);
3394 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3396 // a single column of 6 items are visible
3397 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3398 QCOMPARE(itemCount, 6);
3399 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3400 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3401 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3402 QCOMPARE(item->x(), 0.0);
3403 QCOMPARE(item->y(), qreal(i*60));
3406 // now 6x3 grid is visible, plus 1 extra below for refill
3407 gridview->setWidth(240);
3408 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3409 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3410 QCOMPARE(itemCount, 6*3 + 1);
3411 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3412 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3413 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3414 QCOMPARE(item->x(), qreal((i%3)*80));
3415 QCOMPARE(item->y(), qreal((i/3)*60));
3418 // back to single column
3419 gridview->setWidth(100);
3420 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3421 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3422 QCOMPARE(itemCount, 6);
3423 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3424 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3425 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3426 QCOMPARE(item->x(), 0.0);
3427 QCOMPARE(item->y(), qreal(i*60));
3433 void tst_QQuickGridView::indexAt_itemAt_data()
3435 QTest::addColumn<qreal>("x");
3436 QTest::addColumn<qreal>("y");
3437 QTest::addColumn<int>("index");
3439 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3440 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3441 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3442 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3443 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3446 void tst_QQuickGridView::indexAt_itemAt()
3452 QQuickView *canvas = getView();
3455 model.addItem("Fred", "12345");
3456 model.addItem("John", "2345");
3457 model.addItem("Bob", "54321");
3458 model.addItem("Billy", "22345");
3459 model.addItem("Sam", "2945");
3460 model.addItem("Ben", "04321");
3461 model.addItem("Jim", "0780");
3463 QQmlContext *ctxt = canvas->rootContext();
3464 ctxt->setContextProperty("testModel", &model);
3465 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3466 ctxt->setContextProperty("testTopToBottom", QVariant(false));
3468 canvas->setSource(testFileUrl("gridview1.qml"));
3469 qApp->processEvents();
3471 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3472 QTRY_VERIFY(gridview != 0);
3474 QQuickItem *contentItem = gridview->contentItem();
3475 QTRY_VERIFY(contentItem != 0);
3477 QTRY_COMPARE(gridview->count(), model.count());
3479 QQuickItem *item = 0;
3481 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3484 QCOMPARE(gridview->indexAt(x, y), index);
3485 QVERIFY(gridview->itemAt(x, y) == item);
3487 releaseView(canvas);
3490 void tst_QQuickGridView::onAdd()
3492 QFETCH(int, initialItemCount);
3493 QFETCH(int, itemsToAdd);
3495 const int delegateWidth = 50;
3496 const int delegateHeight = 100;
3498 QQuickView *canvas = getView();
3499 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3501 // these initial items should not trigger GridView.onAdd
3502 for (int i=0; i<initialItemCount; i++)
3503 model.addItem("dummy value", "dummy value");
3505 QQmlContext *ctxt = canvas->rootContext();
3506 ctxt->setContextProperty("testModel", &model);
3507 ctxt->setContextProperty("delegateWidth", delegateWidth);
3508 ctxt->setContextProperty("delegateHeight", delegateHeight);
3509 canvas->setSource(testFileUrl("attachedSignals.qml"));
3511 QObject *object = canvas->rootObject();
3512 object->setProperty("width", canvas->width());
3513 object->setProperty("height", canvas->height());
3514 qApp->processEvents();
3516 QList<QPair<QString, QString> > items;
3517 for (int i=0; i<itemsToAdd; i++)
3518 items << qMakePair(QString("value %1").arg(i), QString::number(i));
3519 model.addItems(items);
3521 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3522 qApp->processEvents();
3524 QVariantList result = object->property("addedDelegates").toList();
3525 QTRY_COMPARE(result.count(), items.count());
3526 for (int i=0; i<items.count(); i++)
3527 QCOMPARE(result[i].toString(), items[i].first);
3529 releaseView(canvas);
3532 void tst_QQuickGridView::onAdd_data()
3534 QTest::addColumn<int>("initialItemCount");
3535 QTest::addColumn<int>("itemsToAdd");
3537 QTest::newRow("0, add 1") << 0 << 1;
3538 QTest::newRow("0, add 2") << 0 << 2;
3539 QTest::newRow("0, add 10") << 0 << 10;
3541 QTest::newRow("1, add 1") << 1 << 1;
3542 QTest::newRow("1, add 2") << 1 << 2;
3543 QTest::newRow("1, add 10") << 1 << 10;
3545 QTest::newRow("5, add 1") << 5 << 1;
3546 QTest::newRow("5, add 2") << 5 << 2;
3547 QTest::newRow("5, add 10") << 5 << 10;
3550 void tst_QQuickGridView::onRemove()
3552 QFETCH(int, initialItemCount);
3553 QFETCH(int, indexToRemove);
3554 QFETCH(int, removeCount);
3556 const int delegateWidth = 50;
3557 const int delegateHeight = 100;
3559 for (int i=0; i<initialItemCount; i++)
3560 model.addItem(QString("value %1").arg(i), "dummy value");
3562 QQuickView *canvas = getView();
3563 QQmlContext *ctxt = canvas->rootContext();
3564 ctxt->setContextProperty("testModel", &model);
3565 ctxt->setContextProperty("delegateWidth", delegateWidth);
3566 ctxt->setContextProperty("delegateHeight", delegateHeight);
3567 canvas->setSource(testFileUrl("attachedSignals.qml"));
3568 QObject *object = canvas->rootObject();
3570 model.removeItems(indexToRemove, removeCount);
3571 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3572 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
3574 releaseView(canvas);
3577 void tst_QQuickGridView::onRemove_data()
3579 QTest::addColumn<int>("initialItemCount");
3580 QTest::addColumn<int>("indexToRemove");
3581 QTest::addColumn<int>("removeCount");
3583 QTest::newRow("remove first") << 1 << 0 << 1;
3584 QTest::newRow("two items, remove first") << 2 << 0 << 1;
3585 QTest::newRow("two items, remove last") << 2 << 1 << 1;
3586 QTest::newRow("two items, remove all") << 2 << 0 << 2;
3588 QTest::newRow("four items, remove first") << 4 << 0 << 1;
3589 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
3590 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
3591 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
3592 QTest::newRow("four items, remove last") << 4 << 3 << 1;
3593 QTest::newRow("four items, remove all") << 4 << 0 << 4;
3595 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
3596 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
3597 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
3600 void tst_QQuickGridView::columnCount()
3603 canvas.setSource(testFileUrl("gridview4.qml"));
3605 canvas.requestActivateWindow();
3606 QTest::qWaitForWindowShown(&canvas);
3608 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
3610 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
3611 QCOMPARE(view->cellHeight(), qreal(100));
3613 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
3614 QCOMPARE(items.size(), 18);
3615 QCOMPARE(items.at(8)->y(), qreal(0));
3616 QCOMPARE(items.at(9)->y(), qreal(100));
3619 void tst_QQuickGridView::margins()
3622 QQuickView *canvas = createView();
3625 for (int i = 0; i < 40; i++)
3626 model.addItem("Item" + QString::number(i), "");
3628 QQmlContext *ctxt = canvas->rootContext();
3629 ctxt->setContextProperty("testModel", &model);
3630 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3632 canvas->setSource(testFileUrl("margins.qml"));
3634 qApp->processEvents();
3636 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3637 QTRY_VERIFY(gridview != 0);
3638 QQuickItem *contentItem = gridview->contentItem();
3639 QTRY_VERIFY(contentItem != 0);
3640 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3642 QCOMPARE(gridview->contentX(), -30.);
3643 QCOMPARE(gridview->xOrigin(), 0.);
3646 gridview->positionViewAtEnd();
3647 qreal pos = gridview->contentX();
3648 gridview->setContentX(pos + 80);
3649 gridview->returnToBounds();
3650 QTRY_COMPARE(gridview->contentX(), pos + 50);
3652 // remove item before visible and check that left margin is maintained
3653 // and xOrigin is updated
3654 gridview->setContentX(200);
3655 model.removeItems(0, 4);
3657 gridview->setContentX(-50);
3658 gridview->returnToBounds();
3659 QCOMPARE(gridview->xOrigin(), 100.);
3660 QTRY_COMPARE(gridview->contentX(), 70.);
3662 // reduce left margin
3663 gridview->setLeftMargin(20);
3664 QCOMPARE(gridview->xOrigin(), 100.);
3665 QTRY_COMPARE(gridview->contentX(), 80.);
3668 gridview->positionViewAtEnd();
3669 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3670 pos = gridview->contentX();
3671 gridview->setContentX(pos + 80);
3672 gridview->returnToBounds();
3673 QTRY_COMPARE(gridview->contentX(), pos + 50);
3675 // reduce right margin
3676 pos = gridview->contentX();
3677 gridview->setRightMargin(40);
3678 QCOMPARE(gridview->xOrigin(), 0.);
3679 QTRY_COMPARE(gridview->contentX(), pos-10);
3685 QQuickView *canvas = createView();
3689 for (int i = 0; i < 40; i++)
3690 model.addItem("Item" + QString::number(i), "");
3692 QQmlContext *ctxt = canvas->rootContext();
3693 ctxt->setContextProperty("testModel", &model);
3694 ctxt->setContextProperty("testRightToLeft", QVariant(true));
3696 canvas->setSource(testFileUrl("margins.qml"));
3697 qApp->processEvents();
3699 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3700 QTRY_VERIFY(gridview != 0);
3702 QQuickItem *contentItem = gridview->contentItem();
3703 QTRY_VERIFY(contentItem != 0);
3705 QTRY_COMPARE(gridview->contentX(), -240+50.);
3706 QTRY_COMPARE(gridview->xOrigin(), 0.);
3709 gridview->positionViewAtEnd();
3710 qreal pos = gridview->contentX();
3711 gridview->setContentX(pos - 80);
3712 gridview->returnToBounds();
3713 QTRY_COMPARE(gridview->contentX(), pos - 30);
3715 // remove item before visible and check that left margin is maintained
3716 // and xOrigin is updated
3717 gridview->setContentX(-400);
3718 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3719 model.removeItems(0, 4);
3720 QTRY_COMPARE(model.count(), gridview->count());
3721 gridview->setContentX(-240+50);
3722 gridview->returnToBounds();
3723 QCOMPARE(gridview->xOrigin(), -100.);
3724 QTRY_COMPARE(gridview->contentX(), -240-50.);
3726 // reduce right margin
3727 pos = gridview->contentX();
3728 gridview->setRightMargin(40);
3729 QCOMPARE(gridview->xOrigin(), -100.);
3730 QTRY_COMPARE(gridview->contentX(), -240-100 + 40.);
3733 gridview->positionViewAtEnd();
3734 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3735 pos = gridview->contentX();
3736 gridview->setContentX(pos - 80);
3737 gridview->returnToBounds();
3738 QTRY_COMPARE(gridview->contentX(), pos - 30);
3740 // reduce left margin
3741 pos = gridview->contentX();
3742 gridview->setLeftMargin(20);
3743 QCOMPARE(gridview->xOrigin(), 0.);
3744 QTRY_COMPARE(gridview->contentX(), pos+10);
3750 void tst_QQuickGridView::creationContext()
3753 canvas.setGeometry(0,0,240,320);
3754 canvas.setSource(testFileUrl("creationContext.qml"));
3755 qApp->processEvents();
3757 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
3759 QVERIFY(rootItem->property("count").toInt() > 0);
3762 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
3763 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3764 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
3765 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3766 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
3767 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3770 void tst_QQuickGridView::snapToRow_data()
3772 QTest::addColumn<QQuickGridView::Flow>("flow");
3773 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3774 QTest::addColumn<int>("highlightRangeMode");
3775 QTest::addColumn<QPoint>("flickStart");
3776 QTest::addColumn<QPoint>("flickEnd");
3777 QTest::addColumn<qreal>("snapAlignment");
3778 QTest::addColumn<qreal>("endExtent");
3779 QTest::addColumn<qreal>("startExtent");
3781 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3782 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
3784 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3785 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
3787 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3788 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
3790 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3791 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
3793 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3794 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
3796 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3797 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
3800 void tst_QQuickGridView::snapToRow()
3802 QFETCH(QQuickGridView::Flow, flow);
3803 QFETCH(Qt::LayoutDirection, layoutDirection);
3804 QFETCH(int, highlightRangeMode);
3805 QFETCH(QPoint, flickStart);
3806 QFETCH(QPoint, flickEnd);
3807 QFETCH(qreal, snapAlignment);
3808 QFETCH(qreal, endExtent);
3809 QFETCH(qreal, startExtent);
3811 QQuickView *canvas = getView();
3813 canvas->setSource(testFileUrl("snapToRow.qml"));
3815 qApp->processEvents();
3817 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3818 QTRY_VERIFY(gridview != 0);
3820 gridview->setFlow(flow);
3821 gridview->setLayoutDirection(layoutDirection);
3822 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3823 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3825 QQuickItem *contentItem = gridview->contentItem();
3826 QTRY_VERIFY(contentItem != 0);
3828 // confirm that a flick hits an item boundary
3829 flick(canvas, flickStart, flickEnd, 180);
3830 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3831 if (flow == QQuickGridView::LeftToRight)
3832 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
3834 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
3838 flick(canvas, flickStart, flickEnd, 180);
3839 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3840 } while (flow == QQuickGridView::LeftToRight
3841 ? !gridview->isAtYEnd()
3842 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3844 if (flow == QQuickGridView::LeftToRight)
3845 QCOMPARE(gridview->contentY(), endExtent);
3847 QCOMPARE(gridview->contentX(), endExtent);
3851 flick(canvas, flickEnd, flickStart, 180);
3852 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3853 } while (flow == QQuickGridView::LeftToRight
3854 ? !gridview->isAtYBeginning()
3855 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3857 if (flow == QQuickGridView::LeftToRight)
3858 QCOMPARE(gridview->contentY(), startExtent);
3860 QCOMPARE(gridview->contentX(), startExtent);
3862 releaseView(canvas);
3865 void tst_QQuickGridView::snapOneRow_data()
3867 QTest::addColumn<QQuickGridView::Flow>("flow");
3868 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3869 QTest::addColumn<int>("highlightRangeMode");
3870 QTest::addColumn<QPoint>("flickStart");
3871 QTest::addColumn<QPoint>("flickEnd");
3872 QTest::addColumn<qreal>("snapAlignment");
3873 QTest::addColumn<qreal>("endExtent");
3874 QTest::addColumn<qreal>("startExtent");
3876 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3877 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
3879 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3880 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
3882 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3883 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
3885 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3886 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
3888 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3889 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
3891 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3892 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
3895 void tst_QQuickGridView::snapOneRow()
3897 QFETCH(QQuickGridView::Flow, flow);
3898 QFETCH(Qt::LayoutDirection, layoutDirection);
3899 QFETCH(int, highlightRangeMode);
3900 QFETCH(QPoint, flickStart);
3901 QFETCH(QPoint, flickEnd);
3902 QFETCH(qreal, snapAlignment);
3903 QFETCH(qreal, endExtent);
3904 QFETCH(qreal, startExtent);
3906 QQuickView *canvas = getView();
3908 canvas->setSource(testFileUrl("snapOneRow.qml"));
3910 qApp->processEvents();
3912 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3913 QTRY_VERIFY(gridview != 0);
3915 gridview->setFlow(flow);
3916 gridview->setLayoutDirection(layoutDirection);
3917 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3918 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3920 QQuickItem *contentItem = gridview->contentItem();
3921 QTRY_VERIFY(contentItem != 0);
3923 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
3925 // confirm that a flick hits next row boundary
3926 flick(canvas, flickStart, flickEnd, 180);
3927 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3928 if (flow == QQuickGridView::LeftToRight)
3929 QCOMPARE(gridview->contentY(), snapAlignment);
3931 QCOMPARE(gridview->contentX(), snapAlignment);
3933 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3934 QCOMPARE(gridview->currentIndex(), 2);
3935 QCOMPARE(currentIndexSpy.count(), 1);
3940 flick(canvas, flickStart, flickEnd, 180);
3941 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3942 } while (flow == QQuickGridView::LeftToRight
3943 ? !gridview->isAtYEnd()
3944 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3946 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3947 QCOMPARE(gridview->currentIndex(), 6);
3948 QCOMPARE(currentIndexSpy.count(), 3);
3951 if (flow == QQuickGridView::LeftToRight)
3952 QCOMPARE(gridview->contentY(), endExtent);
3954 QCOMPARE(gridview->contentX(), endExtent);
3958 flick(canvas, flickEnd, flickStart, 180);
3959 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3960 } while (flow == QQuickGridView::LeftToRight
3961 ? !gridview->isAtYBeginning()
3962 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3964 if (flow == QQuickGridView::LeftToRight)
3965 QCOMPARE(gridview->contentY(), startExtent);
3967 QCOMPARE(gridview->contentX(), startExtent);
3969 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3970 QCOMPARE(gridview->currentIndex(), 0);
3971 QCOMPARE(currentIndexSpy.count(), 6);
3974 releaseView(canvas);
3978 void tst_QQuickGridView::unaligned()
3980 QQuickView *canvas = createView();
3984 for (int i = 0; i < 10; i++)
3985 model.addItem("Item" + QString::number(i), "");
3987 QQmlContext *ctxt = canvas->rootContext();
3988 ctxt->setContextProperty("testModel", &model);
3990 canvas->setSource(testFileUrl("unaligned.qml"));
3991 qApp->processEvents();
3993 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3994 QVERIFY(gridview != 0);
3996 QQuickItem *contentItem = gridview->contentItem();
3997 QVERIFY(contentItem != 0);
3999 for (int i = 0; i < 10; ++i) {
4000 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4001 if (!item) qWarning() << "Item" << i << "not found";
4003 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4004 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4008 for (int i = 10; i < 18; ++i) {
4009 model.addItem("Item" + QString::number(i), "");
4010 QQuickItem *item = 0;
4011 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4012 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4013 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4017 for (int i = 0; i < 10; ++i) {
4018 model.insertItem(i, "Item" + QString::number(i), "");
4019 QQuickItem *item = 0;
4020 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4021 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4022 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4026 model.removeItems(7, 10);
4027 QTRY_COMPARE(model.count(), gridview->count());
4028 for (int i = 0; i < 18; ++i) {
4029 QQuickItem *item = 0;
4030 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4031 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
4032 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
4038 void tst_QQuickGridView::populateTransitions()
4040 QFETCH(bool, staticallyPopulate);
4041 QFETCH(bool, dynamicallyPopulate);
4042 QFETCH(bool, usePopulateTransition);
4044 QPointF transitionFrom(-50, -50);
4045 QPointF transitionVia(100, 100);
4046 QaimModel model_transitionFrom;
4047 QaimModel model_transitionVia;
4050 if (staticallyPopulate) {
4051 for (int i = 0; i < 30; i++)
4052 model.addItem("item" + QString::number(i), "");
4055 QQuickView *canvas = getView();
4056 canvas->rootContext()->setContextProperty("testModel", &model);
4057 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4058 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4059 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4060 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4061 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4062 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4063 canvas->setSource(testFileUrl("populateTransitions.qml"));
4066 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4068 QQuickItem *contentItem = gridview->contentItem();
4069 QVERIFY(contentItem);
4071 if (staticallyPopulate || dynamicallyPopulate) {
4072 // check the populate transition is run
4073 if (usePopulateTransition) {
4074 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 19);
4076 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4077 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4079 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4081 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4084 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4085 if (usePopulateTransition)
4086 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
4087 for (int i=0; i < model.count() && i < itemCount; ++i) {
4088 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4089 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4090 QCOMPARE(item->x(), (i%3)*80.0);
4091 QCOMPARE(item->y(), (i/3)*60.0);
4092 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4094 QTRY_COMPARE(name->text(), model.name(i));
4097 // add an item and check this is done with add transition, not populate
4098 model.insertItem(0, "another item", "");
4099 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
4100 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(),
4101 (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 19 : 0);
4104 canvas->rootContext()->setContextProperty("testModel", QVariant());
4105 QTRY_COMPARE(gridview->count(), 0);
4106 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4107 gridview->setProperty("countPopulateTransitions", 0);
4108 gridview->setProperty("countAddTransitions", 0);
4110 // set to a valid model and check populate transition is run a second time
4112 for (int i = 0; i < 30; i++)
4113 model.addItem("item" + QString::number(i), "");
4114 canvas->rootContext()->setContextProperty("testModel", &model);
4115 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4117 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
4118 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4120 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4121 if (usePopulateTransition)
4122 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
4123 for (int i=0; i < model.count() && i < itemCount; ++i) {
4124 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4125 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4126 QCOMPARE(item->x(), (i%3)*80.0);
4127 QCOMPARE(item->y(), (i/3)*60.0);
4128 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4130 QTRY_COMPARE(name->text(), model.name(i));
4133 // reset model and check populate transition is run again
4134 gridview->setProperty("countPopulateTransitions", 0);
4135 gridview->setProperty("countAddTransitions", 0);
4137 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
4138 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4140 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4141 if (usePopulateTransition)
4142 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
4143 for (int i=0; i < model.count() && i < itemCount; ++i) {
4144 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4145 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4146 QCOMPARE(item->x(), (i%3)*80.0);
4147 QCOMPARE(item->y(), (i/3)*60.0);
4148 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4150 QTRY_COMPARE(name->text(), model.name(i));
4153 releaseView(canvas);
4156 void tst_QQuickGridView::populateTransitions_data()
4158 QTest::addColumn<bool>("staticallyPopulate");
4159 QTest::addColumn<bool>("dynamicallyPopulate");
4160 QTest::addColumn<bool>("usePopulateTransition");
4162 QTest::newRow("static") << true << false << true;
4163 QTest::newRow("static, no populate") << true << false << false;
4165 QTest::newRow("dynamic") << false << true << true;
4166 QTest::newRow("dynamic, no populate") << false << true << false;
4168 QTest::newRow("empty to start with") << false << false << true;
4169 QTest::newRow("empty to start with, no populate") << false << false << false;
4172 void tst_QQuickGridView::addTransitions()
4174 QFETCH(int, initialItemCount);
4175 QFETCH(bool, shouldAnimateTargets);
4176 QFETCH(qreal, contentYRowOffset);
4177 QFETCH(int, insertionIndex);
4178 QFETCH(int, insertionCount);
4179 QFETCH(ListRange, expectedDisplacedIndexes);
4181 // added items should start here
4182 QPointF targetItems_transitionFrom(-50, -50);
4184 // displaced items should pass through this point
4185 QPointF displacedItems_transitionVia(100, 100);
4188 for (int i = 0; i < initialItemCount; i++)
4189 model.addItem("Original item" + QString::number(i), "");
4190 QaimModel model_targetItems_transitionFrom;
4191 QaimModel model_displacedItems_transitionVia;
4193 QQuickView *canvas = getView();
4194 QQmlContext *ctxt = canvas->rootContext();
4195 ctxt->setContextProperty("testModel", &model);
4196 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4197 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4198 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4199 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4200 canvas->setSource(testFileUrl("addTransitions.qml"));
4203 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4204 QTRY_VERIFY(gridview != 0);
4205 QQuickItem *contentItem = gridview->contentItem();
4206 QVERIFY(contentItem != 0);
4207 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4209 if (contentYRowOffset != 0) {
4210 gridview->setContentY(contentYRowOffset * 60.0);
4211 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4214 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4216 // only target items that will become visible should be animated
4217 QList<QPair<QString, QString> > newData;
4218 QList<QPair<QString, QString> > expectedTargetData;
4219 QList<int> targetIndexes;
4220 if (shouldAnimateTargets) {
4221 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4222 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4224 // last visible item is the first item of the row beneath the view
4225 if (i >= (gridview->contentY() / 60)*3 && i < qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) {
4226 expectedTargetData << newData.last();
4230 QVERIFY(expectedTargetData.count() > 0);
4234 if (!newData.isEmpty()) {
4235 model.insertItems(insertionIndex, newData);
4236 QTRY_COMPARE(model.count(), gridview->count());
4239 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4241 if (shouldAnimateTargets) {
4242 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4243 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4244 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4246 // check the target and displaced items were animated
4247 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4248 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4250 // check attached properties
4251 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4252 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4253 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4254 if (expectedDisplacedIndexes.isValid()) {
4255 // adjust expectedDisplacedIndexes to their final values after the move
4256 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4257 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4258 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4259 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4262 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4263 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4266 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4267 int firstVisibleIndex = -1;
4268 for (int i=0; i<items.count(); i++) {
4269 if (items[i]->y() >= gridview->contentY()) {
4270 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4271 firstVisibleIndex = e.evaluate().toInt();
4275 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4277 // verify all items moved to the correct final positions
4278 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4279 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4280 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4281 QCOMPARE(item->x(), (i%3)*80.0);
4282 QCOMPARE(item->y(), (i/3)*60.0);
4283 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4285 QCOMPARE(name->text(), model.name(i));
4288 releaseView(canvas);
4291 void tst_QQuickGridView::addTransitions_data()
4293 QTest::addColumn<int>("initialItemCount");
4294 QTest::addColumn<qreal>("contentYRowOffset");
4295 QTest::addColumn<bool>("shouldAnimateTargets");
4296 QTest::addColumn<int>("insertionIndex");
4297 QTest::addColumn<int>("insertionCount");
4298 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4300 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4301 QTest::newRow("insert 1, just before start")
4302 << 30 << 1.0 << false
4303 << 0 << 1 << ListRange();
4304 QTest::newRow("insert 1, way before start")
4305 << 30 << 1.0 << false
4306 << 0 << 1 << ListRange();
4307 QTest::newRow("insert multiple, just before start")
4308 << 30 << 1.0 << false
4309 << 0 << 3 << ListRange();
4310 QTest::newRow("insert multiple (< 1 row), just before start")
4311 << 30 << 1.0 << false
4312 << 0 << 2 << ListRange();
4313 QTest::newRow("insert multiple, way before start")
4314 << 30 << 3.0 << false
4315 << 0 << 3 << ListRange();
4317 QTest::newRow("insert 1 at start")
4318 << 30 << 0.0 << true
4319 << 0 << 1 << ListRange(0, 17);
4320 QTest::newRow("insert multiple at start")
4321 << 30 << 0.0 << true
4322 << 0 << 3 << ListRange(0, 17);
4323 QTest::newRow("insert multiple (> 1 row) at start")
4324 << 30 << 0.0 << true
4325 << 0 << 5 << ListRange(0, 17);
4326 QTest::newRow("insert 1 at start, content y not 0")
4327 << 30 << 1.0 << true // first visible is index 3
4328 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4329 QTest::newRow("insert multiple at start, content y not 0")
4330 << 30 << 1.0 << true // first visible is index 3
4331 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4332 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4333 << 30 << 1.0 << true // first visible is index 3
4334 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4336 QTest::newRow("insert 1 at start, to empty grid")
4338 << 0 << 1 << ListRange();
4339 QTest::newRow("insert multiple at start, to empty grid")
4341 << 0 << 3 << ListRange();
4343 QTest::newRow("insert 1 at middle")
4344 << 30 << 0.0 << true
4345 << 7 << 1 << ListRange(7, 17);
4346 QTest::newRow("insert multiple at middle")
4347 << 30 << 0.0 << true
4348 << 7 << 3 << ListRange(7, 17);
4349 QTest::newRow("insert multiple (> 1 row) at middle")
4350 << 30 << 0.0 << true
4351 << 7 << 5 << ListRange(7, 17);
4353 QTest::newRow("insert 1 at bottom")
4354 << 30 << 0.0 << true
4355 << 17 << 1 << ListRange(17, 17);
4356 QTest::newRow("insert multiple at bottom")
4357 << 30 << 0.0 << true
4358 << 17 << 3 << ListRange(17, 17);
4359 QTest::newRow("insert 1 at bottom, content y not 0")
4360 << 30 << 1.0 << true
4361 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4362 QTest::newRow("insert multiple at bottom, content y not 0")
4363 << 30 << 1.0 << true
4364 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4367 // items added after the last visible will not be animated in, since they
4368 // do not appear in the final view
4369 QTest::newRow("insert 1 after end")
4370 << 30 << 0.0 << false
4371 << 18 << 1 << ListRange();
4372 QTest::newRow("insert multiple after end")
4373 << 30 << 0.0 << false
4374 << 18 << 3 << ListRange();
4377 void tst_QQuickGridView::moveTransitions()
4379 QFETCH(int, initialItemCount);
4380 QFETCH(qreal, contentYRowOffset);
4381 QFETCH(qreal, rowOffsetAfterMove);
4382 QFETCH(int, moveFrom);
4383 QFETCH(int, moveTo);
4384 QFETCH(int, moveCount);
4385 QFETCH(ListRange, expectedDisplacedIndexes);
4387 // target and displaced items should pass through these points
4388 QPointF targetItems_transitionVia(-50, 50);
4389 QPointF displacedItems_transitionVia(100, 100);
4392 for (int i = 0; i < initialItemCount; i++)
4393 model.addItem("Original item" + QString::number(i), "");
4394 QaimModel model_targetItems_transitionVia;
4395 QaimModel model_displacedItems_transitionVia;
4397 QQuickView *canvas = getView();
4398 QQmlContext *ctxt = canvas->rootContext();
4399 ctxt->setContextProperty("testModel", &model);
4400 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4401 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4402 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4403 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4404 canvas->setSource(testFileUrl("moveTransitions.qml"));
4407 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4408 QTRY_VERIFY(gridview != 0);
4409 QQuickItem *contentItem = gridview->contentItem();
4410 QVERIFY(contentItem != 0);
4413 if (contentYRowOffset != 0) {
4414 gridview->setContentY(contentYRowOffset * 60.0);
4415 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4418 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4420 // Items moving to *or* from visible positions should be animated.
4421 // Otherwise, they should not be animated.
4422 QList<QPair<QString, QString> > expectedTargetData;
4423 QList<int> targetIndexes;
4424 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4425 int toIndex = moveTo + (i - moveFrom);
4426 int firstVisibleIndex = (gridview->contentY() / 60) * 3;
4427 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4428 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4429 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4430 expectedTargetData << qMakePair(model.name(i), model.number(i));
4434 // ViewTransition.index provides the indices that items are moving to, not from
4435 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4438 model.moveItems(moveFrom, moveTo, moveCount);
4440 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4441 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4442 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4444 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4446 // check the target and displaced items were animated
4447 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4448 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4450 // check attached properties
4451 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4452 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4453 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4454 if (expectedDisplacedIndexes.isValid()) {
4455 // adjust expectedDisplacedIndexes to their final values after the move
4456 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4457 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4458 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4459 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4462 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4463 int firstVisibleIndex = -1;
4464 for (int i=0; i<items.count(); i++) {
4465 if (items[i]->y() >= gridview->contentY()) {
4466 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4467 firstVisibleIndex = e.evaluate().toInt();
4471 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4473 // verify all items moved to the correct final positions
4474 qreal pixelOffset = 60 * rowOffsetAfterMove;
4475 for (int i=firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4476 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4477 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4478 QCOMPARE(item->x(), (i%3)*80.0);
4479 QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
4480 name = findItem<QQuickText>(contentItem, "textName", i);
4482 QTRY_COMPARE(name->text(), model.name(i));
4485 releaseView(canvas);
4488 void tst_QQuickGridView::moveTransitions_data()
4490 QTest::addColumn<int>("initialItemCount");
4491 QTest::addColumn<qreal>("contentYRowOffset");
4492 QTest::addColumn<qreal>("rowOffsetAfterMove");
4493 QTest::addColumn<int>("moveFrom");
4494 QTest::addColumn<int>("moveTo");
4495 QTest::addColumn<int>("moveCount");
4496 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4498 QTest::newRow("move from above view, outside visible items, move 1")
4500 << 1 << 10 << 1 << ListRange(6, 10);
4501 QTest::newRow("move from above view, outside visible items, move 1 (first item)")
4503 << 0 << 10 << 1 << ListRange(6, 10);
4504 QTest::newRow("move from above view, outside visible items, move multiple")
4506 << 1 << 10 << 3 << ListRange(13, 23);
4507 QTest::newRow("move from above view, mix of visible/non-visible")
4509 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4510 QTest::newRow("move from above view, mix of visible/non-visible (move first)")
4512 << 0 << 10 << 6 << ListRange(16, 23);
4514 QTest::newRow("move within view, move 1 down")
4516 << 1 << 10 << 1 << ListRange(2, 10);
4517 QTest::newRow("move within view, move 1 down, move first item")
4519 << 0 << 10 << 1 << ListRange(1, 10);
4520 QTest::newRow("move within view, move 1 down, move first item, contentY not 0")
4522 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
4523 QTest::newRow("move within view, move 1 down, to last item")
4525 << 10 << 17 << 1 << ListRange(11, 17);
4526 QTest::newRow("move within view, move first->last")
4528 << 0 << 17 << 1 << ListRange(1, 17);
4530 QTest::newRow("move within view, move multiple down")
4532 << 1 << 10 << 3 << ListRange(4, 12);
4533 QTest::newRow("move within view, move multiple down, move first item")
4535 << 0 << 10 << 3 << ListRange(3, 12);
4536 QTest::newRow("move within view, move multiple down, move first item, contentY not 0")
4538 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
4539 QTest::newRow("move within view, move multiple down, displace last item")
4541 << 5 << 15 << 3 << ListRange(8, 17);
4542 QTest::newRow("move within view, move multiple down, move first->last")
4544 << 0 << 15 << 3 << ListRange(3, 17);
4546 QTest::newRow("move within view, move 1 up")
4548 << 10 << 1 << 1 << ListRange(1, 9);
4549 QTest::newRow("move within view, move 1 up, move to first index")
4551 << 10 << 0 << 1 << ListRange(0, 9);
4552 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0")
4554 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
4555 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border")
4557 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
4558 QTest::newRow("move within view, move 1 up, move last item")
4560 << 17 << 10 << 1 << ListRange(10, 16);
4561 QTest::newRow("move within view, move 1 up, move last->first")
4563 << 17 << 0 << 1 << ListRange(0, 16);
4565 QTest::newRow("move within view, move multiple up")
4567 << 10 << 1 << 3 << ListRange(1, 9);
4568 QTest::newRow("move within view, move multiple (> 1 row) up")
4570 << 10 << 1 << 5 << ListRange(1, 9);
4571 QTest::newRow("move within view, move multiple up, move to first index")
4573 << 10 << 0 << 3 << ListRange(0, 9);
4574 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0")
4576 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
4577 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border")
4579 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
4580 QTest::newRow("move within view, move multiple up, move last item")
4582 << 15 << 5 << 3 << ListRange(5, 14);
4583 QTest::newRow("move within view, move multiple up, move last->first")
4585 << 15 << 0 << 3 << ListRange(0, 14);
4587 QTest::newRow("move from below view, move 1 up")
4589 << 20 << 5 << 1 << ListRange(5, 17);
4590 QTest::newRow("move from below view, move 1 up, move to top")
4592 << 20 << 0 << 1 << ListRange(0, 17);
4593 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0")
4595 << 25 << 3 << 1 << ListRange(0+3, 17+3);
4596 QTest::newRow("move from below view, move multiple (> 1 row) up")
4598 << 20 << 5 << 5 << ListRange(5, 17);
4599 QTest::newRow("move from below view, move multiple up, move to top")
4601 << 20 << 0 << 3 << ListRange(0, 17);
4602 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0")
4604 << 25 << 3 << 3 << ListRange(0+3, 17+3);
4606 QTest::newRow("move from below view, move 1 up, move to bottom")
4608 << 20 << 17 << 1 << ListRange(17, 17);
4609 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0")
4611 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
4612 QTest::newRow("move from below view, move multiple up, move to to bottom")
4614 << 20 << 17 << 3 << ListRange(17, 17);
4615 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0")
4617 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
4620 void tst_QQuickGridView::removeTransitions()
4622 QFETCH(int, initialItemCount);
4623 QFETCH(bool, shouldAnimateTargets);
4624 QFETCH(qreal, contentYRowOffset);
4625 QFETCH(int, removalIndex);
4626 QFETCH(int, removalCount);
4627 QFETCH(ListRange, expectedDisplacedIndexes);
4629 // added items should end here
4630 QPointF targetItems_transitionTo(-50, -50);
4632 // displaced items should pass through this points
4633 QPointF displacedItems_transitionVia(100, 100);
4636 for (int i = 0; i < initialItemCount; i++)
4637 model.addItem("Original item" + QString::number(i), "");
4638 QaimModel model_targetItems_transitionTo;
4639 QaimModel model_displacedItems_transitionVia;
4641 QQuickView *canvas = getView();
4642 QQmlContext *ctxt = canvas->rootContext();
4643 ctxt->setContextProperty("testModel", &model);
4644 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
4645 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4646 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
4647 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4648 canvas->setSource(testFileUrl("removeTransitions.qml"));
4651 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4652 QTRY_VERIFY(gridview != 0);
4653 QQuickItem *contentItem = gridview->contentItem();
4654 QVERIFY(contentItem != 0);
4655 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4657 if (contentYRowOffset != 0) {
4658 gridview->setContentY(contentYRowOffset * 60.0);
4659 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4662 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4664 // only target items that are visible should be animated
4665 QList<QPair<QString, QString> > expectedTargetData;
4666 QList<int> targetIndexes;
4667 if (shouldAnimateTargets) {
4668 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
4669 int firstVisibleIndex = (gridview->contentY() / 60.0)*3;
4670 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4671 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
4672 expectedTargetData << qMakePair(model.name(i), model.number(i));
4676 QVERIFY(expectedTargetData.count() > 0);
4679 // calculate targetItems and expectedTargets before model changes
4680 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4681 QVariantMap expectedTargets;
4682 for (int i=0; i<targetIndexes.count(); i++)
4683 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
4686 model.removeItems(removalIndex, removalCount);
4687 QTRY_COMPARE(model.count(), gridview->count());
4689 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
4690 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4691 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4692 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4694 // check the target and displaced items were animated
4695 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
4696 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4698 // check attached properties
4699 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
4700 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4701 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4702 if (expectedDisplacedIndexes.isValid()) {
4703 // adjust expectedDisplacedIndexes to their final values after the move
4704 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
4705 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4706 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4707 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4710 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
4711 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4714 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4715 int itemCount = items.count();
4716 int firstVisibleIndex = -1;
4717 for (int i=0; i<items.count(); i++) {
4718 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4719 int index = e.evaluate().toInt();
4720 if (firstVisibleIndex < 0 && items[i]->y() >= gridview->contentY())
4721 firstVisibleIndex = index;
4723 itemCount--; // exclude deleted items
4725 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4727 // verify all items moved to the correct final positions
4728 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4729 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4730 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4731 QCOMPARE(item->x(), (i%3)*80.0);
4732 QCOMPARE(item->y(), gridview->contentY() + ((i-firstVisibleIndex)/3) * 60.0);
4733 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4735 QTRY_COMPARE(name->text(), model.name(i));
4738 releaseView(canvas);
4741 void tst_QQuickGridView::removeTransitions_data()
4743 QTest::addColumn<int>("initialItemCount");
4744 QTest::addColumn<qreal>("contentYRowOffset");
4745 QTest::addColumn<bool>("shouldAnimateTargets");
4746 QTest::addColumn<int>("removalIndex");
4747 QTest::addColumn<int>("removalCount");
4748 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4750 // All items that are visible following the remove operation should be animated.
4751 // Remove targets that are outside of the view should not be animated.
4753 // For a GridView, removing any number of items other than a full row before the start
4754 // should displace all items in the view
4755 QTest::newRow("remove 1 before start")
4756 << 30 << 2.0 << false
4757 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
4758 QTest::newRow("remove 1 row, before start")
4759 << 30 << 2.0 << false
4760 << 3 << 3 << ListRange();
4761 QTest::newRow("remove between 1-2 rows, before start")
4762 << 30 << 2.0 << false
4763 << 0 << 5 << ListRange(6, 25);
4764 QTest::newRow("remove 2 rows, before start")
4765 << 30 << 2.0 << false
4766 << 0 << 6 << ListRange();
4767 QTest::newRow("remove mix of before and after start")
4768 << 30 << 1.0 << true
4769 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
4772 QTest::newRow("remove 1 from start")
4773 << 30 << 0.0 << true
4774 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
4775 QTest::newRow("remove multiple from start")
4776 << 30 << 0.0 << true
4777 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
4778 QTest::newRow("remove 1 from start, content y not 0")
4779 << 30 << 1.0 << true
4780 << 3 << 1 << ListRange(1 + 3, 18 + 3);
4781 QTest::newRow("remove multiple from start, content y not 0")
4782 << 30 << 1.0 << true
4783 << 3 << 3 << ListRange(3 + 3, 20 + 3);
4786 QTest::newRow("remove 1 from middle")
4787 << 30 << 0.0 << true
4788 << 5 << 1 << ListRange(6, 18);
4789 QTest::newRow("remove multiple from middle")
4790 << 30 << 0.0 << true
4791 << 5 << 3 << ListRange(8, 20);
4794 QTest::newRow("remove 1 from bottom")
4795 << 30 << 0.0 << true
4796 << 17 << 1 << ListRange(18, 18);
4797 QTest::newRow("remove multiple (1 row) from bottom")
4798 << 30 << 0.0 << true
4799 << 15 << 3 << ListRange(18, 20);
4800 QTest::newRow("remove multiple (> 1 row) from bottom")
4801 << 30 << 0.0 << true
4802 << 15 << 5 << ListRange(20, 22);
4803 QTest::newRow("remove 1 from bottom, content y not 0")
4804 << 30 << 1.0 << true
4805 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
4806 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
4807 << 30 << 1.0 << true
4808 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
4811 QTest::newRow("remove 1 after end")
4812 << 30 << 0.0 << false
4813 << 18 << 1 << ListRange();
4814 QTest::newRow("remove multiple after end")
4815 << 30 << 0.0 << false
4816 << 18 << 3 << ListRange();
4819 void tst_QQuickGridView::displacedTransitions()
4821 QFETCH(bool, useDisplaced);
4822 QFETCH(bool, displacedEnabled);
4823 QFETCH(bool, useAddDisplaced);
4824 QFETCH(bool, addDisplacedEnabled);
4825 QFETCH(bool, useMoveDisplaced);
4826 QFETCH(bool, moveDisplacedEnabled);
4827 QFETCH(bool, useRemoveDisplaced);
4828 QFETCH(bool, removeDisplacedEnabled);
4829 QFETCH(ListChange, change);
4830 QFETCH(ListRange, expectedDisplacedIndexes);
4833 for (int i = 0; i < 30; i++)
4834 model.addItem("Original item" + QString::number(i), "");
4835 QaimModel model_displaced_transitionVia;
4836 QaimModel model_addDisplaced_transitionVia;
4837 QaimModel model_moveDisplaced_transitionVia;
4838 QaimModel model_removeDisplaced_transitionVia;
4840 QPointF displaced_transitionVia(-50, -100);
4841 QPointF addDisplaced_transitionVia(-150, 100);
4842 QPointF moveDisplaced_transitionVia(50, -100);
4843 QPointF removeDisplaced_transitionVia(150, 100);
4845 QQuickView *canvas = getView();
4846 QQmlContext *ctxt = canvas->rootContext();
4847 ctxt->setContextProperty("testModel", &model);
4848 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
4849 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
4850 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
4851 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
4852 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
4853 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
4854 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
4855 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
4856 ctxt->setContextProperty("useDisplaced", useDisplaced);
4857 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
4858 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
4859 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
4860 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
4861 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
4862 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
4863 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
4864 canvas->setSource(testFileUrl("displacedTransitions.qml"));
4866 qApp->processEvents();
4868 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4869 QTRY_VERIFY(gridview != 0);
4870 QQuickItem *contentItem = gridview->contentItem();
4871 QVERIFY(contentItem != 0);
4872 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4874 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4875 gridview->setProperty("displaceTransitionsDone", false);
4877 switch (change.type) {
4878 case ListChange::Inserted:
4880 QList<QPair<QString, QString> > targetItemData;
4881 for (int i=change.index; i<change.index + change.count; ++i)
4882 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
4883 model.insertItems(change.index, targetItemData);
4884 QTRY_COMPARE(model.count(), gridview->count());
4887 case ListChange::Removed:
4888 model.removeItems(change.index, change.count);
4889 QTRY_COMPARE(model.count(), gridview->count());
4891 case ListChange::Moved:
4892 model.moveItems(change.index, change.to, change.count);
4893 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4895 case ListChange::SetCurrent:
4896 case ListChange::SetContentY:
4900 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
4901 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
4903 if ((useDisplaced && displacedEnabled)
4904 || (useAddDisplaced && addDisplacedEnabled)
4905 || (useMoveDisplaced && moveDisplacedEnabled)
4906 || (useRemoveDisplaced && removeDisplacedEnabled)) {
4907 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
4909 // check the correct number of target items and indexes were received
4910 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
4911 for (int i=0; i<resultTargetIndexes.count(); i++)
4912 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
4913 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
4914 for (int i=0; i<resultTargetItems.count(); i++)
4915 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
4917 QCOMPARE(resultTargetIndexes.count(), 0);
4918 QCOMPARE(resultTargetItems.count(), 0);
4921 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
4922 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
4924 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
4925 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
4926 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
4928 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
4929 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
4930 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
4932 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
4934 if (useDisplaced && displacedEnabled
4935 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
4936 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
4937 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
4938 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
4940 QCOMPARE(model_displaced_transitionVia.count(), 0);
4943 // verify all items moved to the correct final positions
4944 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4945 for (int i=0; i < model.count() && i < items.count(); ++i) {
4946 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4947 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4948 QCOMPARE(item->x(), (i%3)*80.0);
4949 QCOMPARE(item->y(), (i/3)*60.0);
4950 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4952 QTRY_COMPARE(name->text(), model.name(i));
4955 releaseView(canvas);
4958 void tst_QQuickGridView::displacedTransitions_data()
4960 QTest::addColumn<bool>("useDisplaced");
4961 QTest::addColumn<bool>("displacedEnabled");
4962 QTest::addColumn<bool>("useAddDisplaced");
4963 QTest::addColumn<bool>("addDisplacedEnabled");
4964 QTest::addColumn<bool>("useMoveDisplaced");
4965 QTest::addColumn<bool>("moveDisplacedEnabled");
4966 QTest::addColumn<bool>("useRemoveDisplaced");
4967 QTest::addColumn<bool>("removeDisplacedEnabled");
4968 QTest::addColumn<ListChange>("change");
4969 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4971 QTest::newRow("no displaced transitions at all")
4976 << ListChange::insert(0, 1) << ListRange(0, 17);
4978 QTest::newRow("just displaced")
4983 << ListChange::insert(0, 1) << ListRange(0, 17);
4985 QTest::newRow("just displaced (not enabled)")
4990 << ListChange::insert(0, 1) << ListRange(0, 17);
4992 QTest::newRow("displaced + addDisplaced")
4997 << ListChange::insert(0, 1) << ListRange(0, 17);
4999 QTest::newRow("displaced + addDisplaced (not enabled)")
5004 << ListChange::insert(0, 1) << ListRange(0, 17);
5006 QTest::newRow("displaced + moveDisplaced")
5011 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5013 QTest::newRow("displaced + moveDisplaced (not enabled)")
5018 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5020 QTest::newRow("displaced + removeDisplaced")
5025 << ListChange::remove(0, 1) << ListRange(1, 18);
5027 QTest::newRow("displaced + removeDisplaced (not enabled)")
5032 << ListChange::remove(0, 1) << ListRange(1, 18);
5035 QTest::newRow("displaced + add, should use generic displaced for a remove")
5040 << ListChange::remove(0, 1) << ListRange(1, 18);
5043 void tst_QQuickGridView::multipleTransitions()
5045 // Tests that if you interrupt a transition in progress with another action that
5046 // cancels the previous transition, the resulting items are still placed correctly.
5048 QFETCH(int, initialCount);
5049 QFETCH(qreal, contentY);
5050 QFETCH(QList<ListChange>, changes);
5051 QFETCH(bool, enableAddTransitions);
5052 QFETCH(bool, enableMoveTransitions);
5053 QFETCH(bool, enableRemoveTransitions);
5054 QFETCH(bool, rippleAddDisplaced);
5056 // add transitions on the left, moves on the right
5057 QPointF addTargets_transitionFrom(-50, -50);
5058 QPointF addDisplaced_transitionFrom(-50, 50);
5059 QPointF moveTargets_transitionFrom(50, -50);
5060 QPointF moveDisplaced_transitionFrom(50, 50);
5061 QPointF removeTargets_transitionTo(-100, 300);
5062 QPointF removeDisplaced_transitionFrom(100, 300);
5065 for (int i = 0; i < initialCount; i++)
5066 model.addItem("Original item" + QString::number(i), "");
5068 QQuickView *canvas = getView();
5069 QQmlContext *ctxt = canvas->rootContext();
5070 ctxt->setContextProperty("testModel", &model);
5071 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5072 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5073 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5074 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5075 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5076 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5077 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5078 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5079 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5080 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5081 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5083 QTest::qWaitForWindowShown(canvas);
5085 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5086 QTRY_VERIFY(gridview != 0);
5087 QQuickItem *contentItem = gridview->contentItem();
5088 QVERIFY(contentItem != 0);
5089 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5091 if (contentY != 0) {
5092 gridview->setContentY(contentY);
5093 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5096 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5098 for (int i=0; i<changes.count(); i++) {
5099 switch (changes[i].type) {
5100 case ListChange::Inserted:
5102 QList<QPair<QString, QString> > targetItems;
5103 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5104 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5105 model.insertItems(changes[i].index, targetItems);
5106 QTRY_COMPARE(model.count(), gridview->count());
5107 if (i == changes.count() - 1) {
5108 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
5109 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
5111 QTest::qWait(timeBetweenActions);
5115 case ListChange::Removed:
5116 model.removeItems(changes[i].index, changes[i].count);
5117 QTRY_COMPARE(model.count(), gridview->count());
5118 if (i == changes.count() - 1) {
5119 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
5120 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
5122 QTest::qWait(timeBetweenActions);
5125 case ListChange::Moved:
5126 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5127 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5128 if (i == changes.count() - 1) {
5129 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
5130 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
5132 QTest::qWait(timeBetweenActions);
5135 case ListChange::SetCurrent:
5136 gridview->setCurrentIndex(changes[i].index);
5137 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5139 case ListChange::SetContentY:
5140 gridview->setContentY(changes[i].pos);
5141 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5145 QCOMPARE(gridview->count(), model.count());
5147 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5148 int firstVisibleIndex = -1;
5149 for (int i=0; i<items.count(); i++) {
5150 if (items[i]->y() >= contentY) {
5151 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5152 firstVisibleIndex = e.evaluate().toInt();
5156 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5158 // verify all items moved to the correct final positions
5159 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5160 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5161 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5162 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5163 QTRY_COMPARE(item->x(), (i%3)*80.0);
5164 QTRY_COMPARE(item->y(), (i/3)*60.0);
5165 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5167 QTRY_COMPARE(name->text(), model.name(i));
5170 releaseView(canvas);
5173 void tst_QQuickGridView::multipleTransitions_data()
5175 QTest::addColumn<int>("initialCount");
5176 QTest::addColumn<qreal>("contentY");
5177 QTest::addColumn<QList<ListChange> >("changes");
5178 QTest::addColumn<bool>("enableAddTransitions");
5179 QTest::addColumn<bool>("enableMoveTransitions");
5180 QTest::addColumn<bool>("enableRemoveTransitions");
5181 QTest::addColumn<bool>("rippleAddDisplaced");
5183 // the added item and displaced items should move to final dest correctly
5184 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5185 << ListChange::insert(0, 1)
5186 << ListChange::move(0, 3, 1)
5188 << true << true << true << false;
5190 // items affected by the add should change from move to add transition
5191 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5192 << ListChange::move(1, 10, 3)
5193 << ListChange::insert(0, 1)
5195 << true << true << true << false;
5197 // items should be placed correctly if you trigger a transition then refill for that index
5198 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5199 << ListChange::insert(0, 1)
5200 << ListChange::setContentY(160.0)
5201 << ListChange::setContentY(0.0)
5202 << ListChange::insert(0, 1)
5204 << true << true << true << false;
5206 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5207 << ListChange::insert(1, 1)
5208 << ListChange::remove(1, 1)
5210 << true << true << true << true;
5212 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5213 // even if a remove-displace transition is not present to re-animate them
5214 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5215 << ListChange::insert(0, 1)
5216 << ListChange::remove(2, 1)
5218 << true << true << false << false;
5220 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5221 // remove has changed the position of where it will move to
5222 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5223 << ListChange::setContentY(-10.0)
5224 << ListChange::insert(0, 1)
5225 << ListChange::insert(0, 1)
5226 << ListChange::remove(2, 1)
5228 << true << true << false << false;
5231 void tst_QQuickGridView::multipleDisplaced()
5233 // multiple move() operations should only restart displace transitions for items that
5234 // moved from previously set positions, and not those that have moved from their current
5235 // item positions (which may e.g. still be changing from easing bounces in the last transition)
5238 for (int i = 0; i < 30; i++)
5239 model.addItem("Original item" + QString::number(i), "");
5241 QQuickView *canvas = createView();
5242 QQmlContext *ctxt = canvas->rootContext();
5243 ctxt->setContextProperty("testModel", &model);
5244 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5246 QTest::qWaitForWindowShown(canvas);
5248 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5249 QTRY_VERIFY(gridview != 0);
5250 QQuickItem *contentItem = gridview->contentItem();
5251 QVERIFY(contentItem != 0);
5252 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5254 model.moveItems(12, 8, 1);
5255 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5256 model.moveItems(8, 3, 1);
5257 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5259 QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5260 foreach (const QString &name, transitionsStarted.keys()) {
5261 QVERIFY2(transitionsStarted[name] == 1,
5262 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5265 // verify all items moved to the correct final positions
5266 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5267 for (int i=0; i < model.count() && i < items.count(); ++i) {
5268 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5269 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5270 QTRY_COMPARE(item->x(), (i%3)*80.0);
5271 QTRY_COMPARE(item->y(), (i/3)*60.0);
5272 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5274 QTRY_COMPARE(name->text(), model.name(i));
5280 void tst_QQuickGridView::cacheBuffer()
5282 QQuickView *canvas = createView();
5285 for (int i = 0; i < 90; i++)
5286 model.addItem("Item" + QString::number(i), "");
5288 QQmlContext *ctxt = canvas->rootContext();
5289 ctxt->setContextProperty("testModel", &model);
5290 ctxt->setContextProperty("testRightToLeft", QVariant(false));
5291 ctxt->setContextProperty("testTopToBottom", QVariant(false));
5293 canvas->setSource(testFileUrl("gridview1.qml"));
5295 qApp->processEvents();
5297 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5298 QVERIFY(gridview != 0);
5300 QQuickItem *contentItem = gridview->contentItem();
5301 QVERIFY(contentItem != 0);
5302 QVERIFY(gridview->delegate() != 0);
5303 QVERIFY(gridview->model() != 0);
5305 // Confirm items positioned correctly
5306 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5307 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5308 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5309 QTRY_COMPARE(item->x(), (i%3)*80.0);
5310 QTRY_COMPARE(item->y(), (i/3)*60.0);
5313 QQmlIncubationController controller;
5314 canvas->engine()->setIncubationController(&controller);
5316 canvas->rootObject()->setProperty("cacheBuffer", 200);
5317 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5319 // items will be created one at a time
5320 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5321 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5322 QQuickItem *item = 0;
5325 controller.incubateWhile(&b);
5326 item = findItem<QQuickItem>(gridview, "wrapper", i);
5332 controller.incubateWhile(&b);
5335 int newItemCount = 0;
5336 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5338 // Confirm items positioned correctly
5339 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5340 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5342 QTRY_COMPARE(item->x(), (i%3)*80.0);
5343 QTRY_COMPARE(item->y(), (i/3)*60.0);
5346 // move view and confirm items in view are visible immediately and outside are created async
5347 gridview->setContentY(300);
5349 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5350 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5352 QTRY_COMPARE(item->x(), (i%3)*80.0);
5353 QTRY_COMPARE(item->y(), (i/3)*60.0);
5356 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5358 // ensure buffered items are created
5359 for (int i = 34; i < qMin(44,model.count()); ++i) {
5360 QQuickItem *item = 0;
5362 qGuiApp->processEvents(); // allow refill to happen
5364 controller.incubateWhile(&b);
5365 item = findItem<QQuickItem>(gridview, "wrapper", i);
5371 controller.incubateWhile(&b);
5377 void tst_QQuickGridView::asynchronous()
5379 QQuickView *canvas = createView();
5381 QQmlIncubationController controller;
5382 canvas->engine()->setIncubationController(&controller);
5384 canvas->setSource(testFileUrl("asyncloader.qml"));
5386 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5387 QVERIFY(rootObject);
5389 QQuickGridView *gridview = 0;
5392 controller.incubateWhile(&b);
5393 gridview = rootObject->findChild<QQuickGridView*>("view");
5396 // items will be created one at a time
5397 for (int i = 0; i < 12; ++i) {
5398 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5399 QQuickItem *item = 0;
5402 controller.incubateWhile(&b);
5403 item = findItem<QQuickItem>(gridview, "wrapper", i);
5409 controller.incubateWhile(&b);
5412 // verify positioning
5413 QQuickItem *contentItem = gridview->contentItem();
5414 for (int i = 0; i < 12; ++i) {
5415 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5416 if (!item) qWarning() << "Item" << i << "not found";
5417 QVERIFY(item->x() == (i%3)*100);
5418 QVERIFY(item->y() == (i/3)*100);
5424 void tst_QQuickGridView::unrequestedVisibility()
5427 for (int i = 0; i < 30; i++)
5428 model.addItem("Item" + QString::number(i), QString::number(i));
5430 QQuickView *canvas = new QQuickView(0);
5431 canvas->setGeometry(0,0,240,320);
5433 QQmlContext *ctxt = canvas->rootContext();
5434 ctxt->setContextProperty("testModel", &model);
5435 ctxt->setContextProperty("testWrap", QVariant(false));
5437 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5441 qApp->processEvents();
5443 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5444 QTRY_VERIFY(leftview != 0);
5446 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5447 QTRY_VERIFY(rightview != 0);
5449 QQuickItem *leftContent = leftview->contentItem();
5450 QTRY_VERIFY(leftContent != 0);
5452 QQuickItem *rightContent = rightview->contentItem();
5453 QTRY_VERIFY(rightContent != 0);
5455 rightview->setCurrentIndex(12);
5457 QTRY_COMPARE(leftview->contentY(), 0.0);
5458 QTRY_COMPARE(rightview->contentY(), 240.0);
5462 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5463 QCOMPARE(item->isVisible(), true);
5464 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5465 QCOMPARE(item->isVisible(), false);
5467 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5468 QCOMPARE(item->isVisible(), false);
5469 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5470 QCOMPARE(item->isVisible(), true);
5472 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5473 QCOMPARE(item->isVisible(), true);
5474 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5475 QCOMPARE(item->isVisible(), false);
5476 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5477 QCOMPARE(item->isVisible(), false);
5478 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5479 QCOMPARE(item->isVisible(), true);
5481 rightview->setCurrentIndex(0);
5483 QTRY_COMPARE(leftview->contentY(), 0.0);
5484 QTRY_COMPARE(rightview->contentY(), 0.0);
5486 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5487 QCOMPARE(item->isVisible(), true);
5488 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5489 QTRY_COMPARE(item->isVisible(), true);
5491 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5492 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5494 leftview->setCurrentIndex(12);
5496 QTRY_COMPARE(leftview->contentY(), 240.0);
5497 QTRY_COMPARE(rightview->contentY(), 0.0);
5499 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5500 QTRY_COMPARE(item->isVisible(), false);
5501 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5502 QCOMPARE(item->isVisible(), true);
5504 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5505 QCOMPARE(item->isVisible(), true);
5506 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5507 QCOMPARE(item->isVisible(), false);
5509 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5510 QCOMPARE(item->isVisible(), false);
5511 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5512 QCOMPARE(item->isVisible(), true);
5513 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5514 QCOMPARE(item->isVisible(), true);
5515 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5516 QCOMPARE(item->isVisible(), false);
5518 // move a non-visible item into view
5519 model.moveItems(10, 9, 1);
5520 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5522 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5523 QCOMPARE(item->isVisible(), false);
5524 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5525 QCOMPARE(item->isVisible(), true);
5527 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5528 QCOMPARE(item->isVisible(), true);
5529 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5530 QCOMPARE(item->isVisible(), false);
5532 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5533 QCOMPARE(item->isVisible(), false);
5534 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5535 QCOMPARE(item->isVisible(), true);
5536 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5537 QCOMPARE(item->isVisible(), true);
5538 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5539 QCOMPARE(item->isVisible(), false);
5541 // move a visible item out of view
5542 model.moveItems(5, 3, 1);
5543 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5545 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5546 QCOMPARE(item->isVisible(), false);
5547 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5548 QCOMPARE(item->isVisible(), true);
5549 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5550 QCOMPARE(item->isVisible(), true);
5551 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5552 QCOMPARE(item->isVisible(), false);
5554 // move a non-visible item into view
5555 model.moveItems(3, 5, 1);
5556 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5558 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5559 QCOMPARE(item->isVisible(), false);
5560 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5561 QCOMPARE(item->isVisible(), true);
5562 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5563 QCOMPARE(item->isVisible(), true);
5564 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5565 QCOMPARE(item->isVisible(), false);
5567 // move a visible item out of view
5568 model.moveItems(9, 10, 1);
5569 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5571 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5572 QCOMPARE(item->isVisible(), false);
5573 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5574 QCOMPARE(item->isVisible(), true);
5575 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5576 QCOMPARE(item->isVisible(), true);
5577 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5578 QCOMPARE(item->isVisible(), false);
5580 // move a non-visible item into view
5581 model.moveItems(10, 9, 1);
5582 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5584 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5585 QCOMPARE(item->isVisible(), false);
5586 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5587 QCOMPARE(item->isVisible(), true);
5588 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5589 QCOMPARE(item->isVisible(), true);
5590 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5591 QCOMPARE(item->isVisible(), false);
5596 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
5600 for (int i=0; i<list.count(); i++) {
5601 ret << list[i].toInt(&ok);
5603 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
5609 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
5611 for (int i=0; i<indexLists.count(); i++) {
5612 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
5613 if (current != expectedIndexes.toSet())
5614 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
5615 QCOMPARE(current, expectedIndexes.toSet());
5619 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
5621 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
5622 QVERIFY(it.value().type() == QVariant::Int);
5623 QString name = it.key();
5624 int itemIndex = it.value().toInt();
5625 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
5626 if (model.name(itemIndex) != name)
5627 qDebug() << itemIndex;
5628 QCOMPARE(model.name(itemIndex), name);
5630 QCOMPARE(items.count(), expectedIndexes.count());
5633 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
5635 for (int i=0; i<itemLists.count(); i++) {
5636 QVariantList current = itemLists[i].toList();
5637 for (int j=0; j<current.count(); j++) {
5638 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
5639 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
5640 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
5642 QCOMPARE(current.count(), expectedItems.count());
5646 QTEST_MAIN(tst_QQuickGridView)
5648 #include "tst_qquickgridview.moc"