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;
67 class tst_QQuickGridView : public QQmlDataTest
78 void inserted_more_data();
79 void insertBeforeVisible();
80 void insertBeforeVisible_data();
83 void removed_more_data();
84 void addOrRemoveBeforeVisible();
85 void addOrRemoveBeforeVisible_data();
89 void multipleChanges();
90 void multipleChanges_data();
91 void swapWithFirstItem();
94 void noCurrentIndex();
97 void propertyChanges();
98 void componentChanges();
100 void positionViewAtIndex();
101 void positionViewAtIndex_rightToLeft();
106 void enforceRange_rightToLeft();
108 void manualHighlight();
113 void resizeViewAndRepaint();
114 void changeColumnCount();
115 void indexAt_itemAt_data();
116 void indexAt_itemAt();
120 void onRemove_data();
123 void creationContext();
124 void snapToRow_data();
126 void snapOneRow_data();
131 void unrequestedVisibility();
133 void populateTransitions();
134 void populateTransitions_data();
135 void addTransitions();
136 void addTransitions_data();
137 void moveTransitions();
138 void moveTransitions_data();
139 void removeTransitions();
140 void removeTransitions_data();
141 void displacedTransitions();
142 void displacedTransitions_data();
143 void multipleTransitions();
144 void multipleTransitions_data();
147 QList<int> toIntList(const QVariantList &list);
148 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
149 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
150 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
153 tst_QQuickGridView::tst_QQuickGridView()
157 void tst_QQuickGridView::items()
159 QQuickView *canvas = createView();
162 model.addItem("Fred", "12345");
163 model.addItem("John", "2345");
164 model.addItem("Bob", "54321");
165 model.addItem("Billy", "22345");
166 model.addItem("Sam", "2945");
167 model.addItem("Ben", "04321");
168 model.addItem("Jim", "0780");
170 QQmlContext *ctxt = canvas->rootContext();
171 ctxt->setContextProperty("testModel", &model);
172 ctxt->setContextProperty("testRightToLeft", QVariant(false));
173 ctxt->setContextProperty("testTopToBottom", QVariant(false));
175 canvas->setSource(testFileUrl("gridview1.qml"));
176 qApp->processEvents();
178 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
179 QTRY_VERIFY(gridview != 0);
181 QQuickItem *contentItem = gridview->contentItem();
182 QTRY_VERIFY(contentItem != 0);
184 QTRY_COMPARE(gridview->count(), model.count());
185 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
186 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
188 for (int i = 0; i < model.count(); ++i) {
189 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
190 QTRY_VERIFY(name != 0);
191 QTRY_COMPARE(name->text(), model.name(i));
192 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
193 QTRY_VERIFY(number != 0);
194 QTRY_COMPARE(number->text(), model.number(i));
197 // set an empty model and confirm that items are destroyed
199 ctxt->setContextProperty("testModel", &model2);
201 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
202 QTRY_VERIFY(itemCount == 0);
207 void tst_QQuickGridView::changed()
209 QQuickView *canvas = createView();
212 model.addItem("Fred", "12345");
213 model.addItem("John", "2345");
214 model.addItem("Bob", "54321");
215 model.addItem("Billy", "22345");
216 model.addItem("Sam", "2945");
217 model.addItem("Ben", "04321");
218 model.addItem("Jim", "0780");
220 QQmlContext *ctxt = canvas->rootContext();
221 ctxt->setContextProperty("testModel", &model);
222 ctxt->setContextProperty("testRightToLeft", QVariant(false));
223 ctxt->setContextProperty("testTopToBottom", QVariant(false));
225 canvas->setSource(testFileUrl("gridview1.qml"));
226 qApp->processEvents();
228 QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
229 QTRY_VERIFY(gridview != 0);
231 QQuickItem *contentItem = gridview->contentItem();
232 QTRY_VERIFY(contentItem != 0);
234 model.modifyItem(1, "Will", "9876");
235 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
236 QTRY_VERIFY(name != 0);
237 QTRY_COMPARE(name->text(), model.name(1));
238 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
239 QTRY_VERIFY(number != 0);
240 QTRY_COMPARE(number->text(), model.number(1));
245 void tst_QQuickGridView::inserted()
247 QQuickView *canvas = createView();
251 model.addItem("Fred", "12345");
252 model.addItem("John", "2345");
253 model.addItem("Bob", "54321");
255 QQmlContext *ctxt = canvas->rootContext();
256 ctxt->setContextProperty("testModel", &model);
257 ctxt->setContextProperty("testRightToLeft", QVariant(false));
258 ctxt->setContextProperty("testTopToBottom", QVariant(false));
260 canvas->setSource(testFileUrl("gridview1.qml"));
261 qApp->processEvents();
263 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
264 QTRY_VERIFY(gridview != 0);
266 QQuickItem *contentItem = gridview->contentItem();
267 QTRY_VERIFY(contentItem != 0);
269 model.insertItem(1, "Will", "9876");
271 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
272 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
274 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
275 QTRY_VERIFY(name != 0);
276 QTRY_COMPARE(name->text(), model.name(1));
277 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
278 QTRY_VERIFY(number != 0);
279 QTRY_COMPARE(number->text(), model.number(1));
281 // Checks that onAdd is called
282 int added = canvas->rootObject()->property("added").toInt();
283 QTRY_COMPARE(added, 1);
285 // Confirm items positioned correctly
286 for (int i = 0; i < model.count(); ++i) {
287 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
288 QTRY_COMPARE(item->x(), (i%3)*80.0);
289 QTRY_COMPARE(item->y(), (i/3)*60.0);
292 model.insertItem(0, "Foo", "1111"); // zero index, and current item
294 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
296 name = findItem<QQuickText>(contentItem, "textName", 0);
297 QTRY_VERIFY(name != 0);
298 QTRY_COMPARE(name->text(), model.name(0));
299 number = findItem<QQuickText>(contentItem, "textNumber", 0);
300 QTRY_VERIFY(number != 0);
301 QTRY_COMPARE(number->text(), model.number(0));
303 QTRY_COMPARE(gridview->currentIndex(), 1);
305 // Confirm items positioned correctly
306 for (int i = 0; i < model.count(); ++i) {
307 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
308 QTRY_VERIFY(item->x() == (i%3)*80);
309 QTRY_VERIFY(item->y() == (i/3)*60);
312 for (int i = model.count(); i < 30; ++i)
313 model.insertItem(i, "Hello", QString::number(i));
315 gridview->setContentY(120);
317 // Insert item outside visible area
318 model.insertItem(1, "Hello", "1324");
320 QTRY_VERIFY(gridview->contentY() == 120);
325 void tst_QQuickGridView::inserted_more()
327 QFETCH(qreal, contentY);
328 QFETCH(int, insertIndex);
329 QFETCH(int, insertCount);
330 QFETCH(qreal, itemsOffsetAfterMove);
333 for (int i = 0; i < 30; i++)
334 model.addItem("Item" + QString::number(i), "");
336 QQuickView *canvas = createView();
337 QQmlContext *ctxt = canvas->rootContext();
338 ctxt->setContextProperty("testModel", &model);
339 ctxt->setContextProperty("testRightToLeft", QVariant(false));
340 ctxt->setContextProperty("testTopToBottom", QVariant(false));
342 canvas->setSource(testFileUrl("gridview1.qml"));
344 qApp->processEvents();
346 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
347 QTRY_VERIFY(gridview != 0);
348 QQuickItem *contentItem = gridview->contentItem();
349 QTRY_VERIFY(contentItem != 0);
351 gridview->setContentY(contentY);
352 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
354 QList<QPair<QString, QString> > newData;
355 for (int i=0; i<insertCount; i++)
356 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
357 model.insertItems(insertIndex, newData);
358 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
360 // check visibleItems.first() is in correct position
361 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
363 QCOMPARE(item0->y(), 0.0);
365 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
366 int firstVisibleIndex = -1;
367 for (int i=0; i<items.count(); i++) {
368 if (items[i]->y() >= contentY) {
369 QQmlExpression e(qmlContext(items[i]), items[i], "index");
370 firstVisibleIndex = e.evaluate().toInt();
374 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
376 // Confirm items positioned correctly and indexes correct
377 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
380 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
381 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
382 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
384 QCOMPARE(item->x(), (i%3)*80.0);
385 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
387 name = findItem<QQuickText>(contentItem, "textName", i);
389 QCOMPARE(name->text(), model.name(i));
390 number = findItem<QQuickText>(contentItem, "textNumber", i);
391 QVERIFY(number != 0);
392 QCOMPARE(number->text(), model.number(i));
398 void tst_QQuickGridView::inserted_more_data()
400 QTest::addColumn<qreal>("contentY");
401 QTest::addColumn<int>("insertIndex");
402 QTest::addColumn<int>("insertCount");
403 QTest::addColumn<qreal>("itemsOffsetAfterMove");
405 QTest::newRow("add 1, before visible items")
406 << 120.0 // show 6-23
408 << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
409 // new 1st visible item is at 0
411 QTest::newRow("add 2, before visible items")
412 << 120.0 // show 6-23
414 << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
416 QTest::newRow("add 3, before visible items")
417 << 120.0 // show 6-23
419 << -60.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
421 QTest::newRow("add 5, before visible items")
422 << 120.0 // show 6-23
424 << -60.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
425 // grid is rearranged and first visible moves forward within its row
427 QTest::newRow("add 6, before visible items")
428 << 120.0 // show 6-23
430 << -60.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
434 QTest::newRow("add 1, at start of visible, content at start")
439 QTest::newRow("add multiple, at start of visible, content at start")
444 QTest::newRow("add 1, at start of visible, content not at start")
445 << 120.0 // show 6-23
449 QTest::newRow("add multiple, at start of visible, content not at start")
450 << 120.0 // show 6-23
455 QTest::newRow("add 1, at end of visible, content at start")
460 QTest::newRow("add 1, at end of visible, content at start")
465 QTest::newRow("add 1, at end of visible, content not at start")
466 << 120.0 // show 6-23
470 QTest::newRow("add multiple, at end of visible, content not at start")
471 << 120.0 // show 6-23
476 QTest::newRow("add 1, after visible, content at start")
481 QTest::newRow("add 1, after visible, content at start")
486 QTest::newRow("add 1, after visible, content not at start")
487 << 120.0 // show 6-23
491 QTest::newRow("add multiple, after visible, content not at start")
492 << 120.0 // show 6-23
497 void tst_QQuickGridView::insertBeforeVisible()
499 QFETCH(int, insertIndex);
500 QFETCH(int, insertCount);
501 QFETCH(int, cacheBuffer);
504 QQuickView *canvas = createView();
507 for (int i = 0; i < 30; i++)
508 model.addItem("Item" + QString::number(i), "");
510 QQmlContext *ctxt = canvas->rootContext();
511 ctxt->setContextProperty("testModel", &model);
512 ctxt->setContextProperty("testRightToLeft", QVariant(false));
513 ctxt->setContextProperty("testTopToBottom", QVariant(false));
514 canvas->setSource(testFileUrl("gridview1.qml"));
516 qApp->processEvents();
518 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
519 QTRY_VERIFY(gridview != 0);
520 QQuickItem *contentItem = gridview->contentItem();
521 QTRY_VERIFY(contentItem != 0);
523 gridview->setCacheBuffer(cacheBuffer);
524 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
526 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
527 int firstVisibleIndex = 12; // move to an index where the top item is not visible
528 gridview->setContentY(firstVisibleIndex/3 * 60.0);
529 gridview->setCurrentIndex(firstVisibleIndex);
530 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
532 QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
533 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
535 QCOMPARE(item->y(), gridview->contentY());
537 QList<QPair<QString, QString> > newData;
538 for (int i=0; i<insertCount; i++)
539 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
540 model.insertItems(insertIndex, newData);
541 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
543 // now, moving to the top of the view should position the inserted items correctly
544 int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
545 gridview->setCurrentIndex(0);
546 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
547 QTRY_COMPARE(gridview->currentIndex(), 0);
548 QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
550 // Confirm items positioned correctly and indexes correct
551 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
552 for (int i = 0; i < model.count() && i < itemCount; ++i) {
553 item = findItem<QQuickItem>(contentItem, "wrapper", i);
554 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
555 QCOMPARE(item->x(), (i%3)*80.0);
556 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
557 name = findItem<QQuickText>(contentItem, "textName", i);
559 QTRY_COMPARE(name->text(), model.name(i));
565 void tst_QQuickGridView::insertBeforeVisible_data()
567 QTest::addColumn<int>("insertIndex");
568 QTest::addColumn<int>("insertCount");
569 QTest::addColumn<int>("cacheBuffer");
571 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
572 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
573 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
575 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
576 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
577 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
579 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
580 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
581 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
583 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
584 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
585 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
588 void tst_QQuickGridView::removed()
590 QQuickView *canvas = createView();
594 for (int i = 0; i < 40; i++)
595 model.addItem("Item" + QString::number(i), "");
597 QQmlContext *ctxt = canvas->rootContext();
598 ctxt->setContextProperty("testModel", &model);
599 ctxt->setContextProperty("testRightToLeft", QVariant(false));
600 ctxt->setContextProperty("testTopToBottom", QVariant(false));
602 canvas->setSource(testFileUrl("gridview1.qml"));
603 qApp->processEvents();
605 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
606 QTRY_VERIFY(gridview != 0);
607 QQuickItem *contentItem = gridview->contentItem();
608 QTRY_VERIFY(contentItem != 0);
609 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
612 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
614 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
615 QTRY_VERIFY(name != 0);
616 QTRY_COMPARE(name->text(), model.name(1));
617 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
618 QTRY_VERIFY(number != 0);
619 QTRY_COMPARE(number->text(), model.number(1));
622 // Checks that onRemove is called
623 QString removed = canvas->rootObject()->property("removed").toString();
624 QTRY_COMPARE(removed, QString("Item1"));
626 // Confirm items positioned correctly
627 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
628 for (int i = 0; i < model.count() && i < itemCount; ++i) {
629 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
630 if (!item) qWarning() << "Item" << i << "not found";
631 QTRY_VERIFY(item->x() == (i%3)*80);
632 QTRY_VERIFY(item->y() == (i/3)*60);
635 // Remove first item (which is the current item);
637 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
639 name = findItem<QQuickText>(contentItem, "textName", 0);
640 QTRY_VERIFY(name != 0);
641 QTRY_COMPARE(name->text(), model.name(0));
642 number = findItem<QQuickText>(contentItem, "textNumber", 0);
643 QTRY_VERIFY(number != 0);
644 QTRY_COMPARE(number->text(), model.number(0));
647 // Confirm items positioned correctly
648 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
649 for (int i = 0; i < model.count() && i < itemCount; ++i) {
650 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
651 if (!item) qWarning() << "Item" << i << "not found";
652 QTRY_VERIFY(item->x() == (i%3)*80);
653 QTRY_VERIFY(item->y() == (i/3)*60);
656 // Remove items not visible
657 model.removeItem(25);
658 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
660 // Confirm items positioned correctly
661 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
662 for (int i = 0; i < model.count() && i < itemCount; ++i) {
663 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
664 if (!item) qWarning() << "Item" << i << "not found";
665 QTRY_VERIFY(item->x() == (i%3)*80);
666 QTRY_VERIFY(item->y() == (i/3)*60);
669 // Remove items before visible
670 gridview->setContentY(120);
671 gridview->setCurrentIndex(10);
673 // Setting currentIndex above shouldn't cause view to scroll
674 QTRY_COMPARE(gridview->contentY(), 120.0);
677 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
679 // Confirm items positioned correctly
680 for (int i = 6; i < 18; ++i) {
681 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
682 if (!item) qWarning() << "Item" << i << "not found";
683 QTRY_VERIFY(item->x() == (i%3)*80);
684 QTRY_VERIFY(item->y() == (i/3)*60);
687 // Remove currentIndex
688 QQuickItem *oldCurrent = gridview->currentItem();
690 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
692 QTRY_COMPARE(gridview->currentIndex(), 9);
693 QTRY_VERIFY(gridview->currentItem() != oldCurrent);
695 gridview->setContentY(0);
696 // let transitions settle.
699 // Confirm items positioned correctly
700 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
701 for (int i = 0; i < model.count() && i < itemCount; ++i) {
702 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
703 QTRY_VERIFY(item->x() == (i%3)*80);
704 QTRY_VERIFY(item->y() == (i/3)*60);
707 // remove item outside current view.
708 gridview->setCurrentIndex(32);
709 gridview->setContentY(240);
711 model.removeItem(30);
712 QTRY_VERIFY(gridview->currentIndex() == 31);
714 // remove current item beyond visible items.
715 gridview->setCurrentIndex(20);
716 gridview->setContentY(0);
717 model.removeItem(20);
719 QTRY_COMPARE(gridview->currentIndex(), 20);
720 QTRY_VERIFY(gridview->currentItem() != 0);
722 // remove item before current, but visible
723 gridview->setCurrentIndex(8);
724 gridview->setContentY(240);
725 oldCurrent = gridview->currentItem();
728 QTRY_COMPARE(gridview->currentIndex(), 7);
729 QTRY_VERIFY(gridview->currentItem() == oldCurrent);
734 void tst_QQuickGridView::removed_more()
736 QFETCH(qreal, contentY);
737 QFETCH(int, removeIndex);
738 QFETCH(int, removeCount);
739 QFETCH(qreal, itemsOffsetAfterMove);
740 QFETCH(QString, firstVisible);
744 QQuickView *canvas = createView();
747 for (int i = 0; i < 30; i++)
748 model.addItem("Item" + QString::number(i), "");
750 QQmlContext *ctxt = canvas->rootContext();
751 ctxt->setContextProperty("testModel", &model);
752 ctxt->setContextProperty("testRightToLeft", QVariant(false));
753 ctxt->setContextProperty("testTopToBottom", QVariant(false));
754 canvas->setSource(testFileUrl("gridview1.qml"));
756 qApp->processEvents();
758 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
759 QTRY_VERIFY(gridview != 0);
760 QQuickItem *contentItem = gridview->contentItem();
761 QTRY_VERIFY(contentItem != 0);
763 gridview->setContentY(contentY);
764 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
766 model.removeItems(removeIndex, removeCount);
767 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
770 int firstVisibleIndex = -1;
771 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
772 for (int i=0; i<items.count(); i++) {
773 if (items[i]->y() >= contentY) {
774 QQmlExpression e(qmlContext(items[i]), items[i], "index");
775 firstVisibleIndex = e.evaluate().toInt();
776 QQmlExpression en(qmlContext(items[i]), items[i], "name");
777 firstName = en.evaluate().toString();
781 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
782 QCOMPARE(firstName, firstVisible);
784 // Confirm items positioned correctly and indexes correct
785 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
786 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
787 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
788 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
790 QTRY_COMPARE(item->x(), (i%3)*80.0);
791 QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
793 name = findItem<QQuickText>(contentItem, "textName", i);
795 QTRY_COMPARE(name->text(), model.name(i));
796 number = findItem<QQuickText>(contentItem, "textNumber", i);
797 QVERIFY(number != 0);
798 QTRY_COMPARE(number->text(), model.number(i));
804 void tst_QQuickGridView::removed_more_data()
806 QTest::addColumn<qreal>("contentY");
807 QTest::addColumn<int>("removeIndex");
808 QTest::addColumn<int>("removeCount");
809 QTest::addColumn<qreal>("itemsOffsetAfterMove");
810 QTest::addColumn<QString>("firstVisible");
812 QTest::newRow("remove 1, before visible items")
813 << 120.0 // show 6-23
817 QTest::newRow("remove 1, before visible position")
818 << 120.0 // show 6-23
822 QTest::newRow("remove multiple, all before visible items")
825 << 60.0 << "Item6"; // removed top row, slide down by 1 row
827 QTest::newRow("remove multiple, all before visible items, remove item 0")
830 << 60.0 << "Item7"; // removed top row, slide down by 1 row
832 QTest::newRow("remove multiple rows, all before visible items")
833 << 240.0 // show 12-29
835 << 120.0 << "Item13";
837 QTest::newRow("remove one row before visible, content y not on item border")
840 << 60.0 << "Item6"; // 1 row removed
842 QTest::newRow("remove mix of visible/non-visible")
843 << 120.0 // show 6-23
845 << 60.0 << "Item6"; // 1 row removed
848 // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
849 // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
850 QTest::newRow("remove multiple, mix of items from before and within visible items")
853 << 60.0 << "Item8"; // adjust for the 1 row removed before the visible
855 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
858 << 60.0 * 2 << "Item8"; // adjust for the 2 rows removed before the visible
861 QTest::newRow("remove 1, from start of visible, content at start")
866 QTest::newRow("remove multiple, from start of visible, content at start")
871 QTest::newRow("remove 1, from start of visible, content not at start")
872 << 120.0 // show 6-23
876 QTest::newRow("remove multiple, from start of visible, content not at start")
877 << 120.0 // show 6-23
882 QTest::newRow("remove 1, from middle of visible, content at start")
887 QTest::newRow("remove multiple, from middle of visible, content at start")
892 QTest::newRow("remove 1, from middle of visible, content not at start")
893 << 120.0 // show 6-23
897 QTest::newRow("remove multiple, from middle of visible, content not at start")
898 << 120.0 // show 6-23
903 QTest::newRow("remove 1, after visible, content at start")
908 QTest::newRow("remove multiple, after visible, content at start")
913 QTest::newRow("remove 1, after visible, content not at start")
914 << 120.0 // show 6-23
918 QTest::newRow("remove multiple, after visible, content not at start")
919 << 120.0 // show 6-23
923 QTest::newRow("remove multiple, mix of items from within and after visible items")
924 << 120.0 // show 6-23
929 void tst_QQuickGridView::addOrRemoveBeforeVisible()
931 // QTBUG-21588: ensure re-layout is done on grid after adding or removing
932 // items from before the visible area
935 QFETCH(qreal, newTopContentY);
937 QQuickView *canvas = createView();
941 for (int i = 0; i < 30; i++)
942 model.addItem("Item" + QString::number(i), "");
944 QQmlContext *ctxt = canvas->rootContext();
945 ctxt->setContextProperty("testModel", &model);
946 ctxt->setContextProperty("testRightToLeft", QVariant(false));
947 ctxt->setContextProperty("testTopToBottom", QVariant(false));
948 canvas->setSource(testFileUrl("gridview1.qml"));
950 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
951 QTRY_VERIFY(gridview != 0);
952 QQuickItem *contentItem = gridview->contentItem();
953 QTRY_VERIFY(contentItem != 0);
955 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
956 QTRY_COMPARE(name->text(), QString("Item0"));
958 gridview->setCurrentIndex(0);
959 qApp->processEvents();
960 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
962 // scroll down until item 0 is no longer drawn
963 // (bug not triggered if we just move using content y, since that doesn't
964 // refill and change the visible items)
965 gridview->setCurrentIndex(24);
966 qApp->processEvents();
968 QTRY_COMPARE(gridview->currentIndex(), 24);
969 QTRY_COMPARE(gridview->contentY(), 220.0);
971 QTest::qWait(100); // wait for refill to complete
972 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible
975 model.insertItem(0, "New Item", "New Item number");
976 QTRY_COMPARE(gridview->count(), 31);
979 QTRY_COMPARE(gridview->count(), 29);
982 // scroll back up and item 0 should be gone
983 gridview->setCurrentIndex(0);
984 qApp->processEvents();
985 QTRY_COMPARE(gridview->currentIndex(), 0);
986 QTRY_COMPARE(gridview->contentY(), newTopContentY);
988 name = findItem<QQuickText>(contentItem, "textName", 0);
990 QCOMPARE(name->text(), QString("New Item"));
992 QCOMPARE(name->text(), QString("Item1"));
994 // Confirm items positioned correctly
995 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
996 for (int i = 0; i < model.count() && i < itemCount; ++i) {
997 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
998 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
999 QTRY_VERIFY(item->x() == (i%3)*80);
1000 QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1006 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1008 QTest::addColumn<bool>("doAdd");
1009 QTest::addColumn<qreal>("newTopContentY");
1011 QTest::newRow("add") << true << -60.0;
1012 QTest::newRow("remove") << false << -60.0;
1015 void tst_QQuickGridView::clear()
1017 QQuickView *canvas = createView();
1020 for (int i = 0; i < 30; i++)
1021 model.addItem("Item" + QString::number(i), "");
1023 QQmlContext *ctxt = canvas->rootContext();
1024 ctxt->setContextProperty("testModel", &model);
1025 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1026 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1028 canvas->setSource(testFileUrl("gridview1.qml"));
1030 qApp->processEvents();
1032 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1033 QVERIFY(gridview != 0);
1034 QQuickItem *contentItem = gridview->contentItem();
1035 QVERIFY(contentItem != 0);
1036 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1040 QVERIFY(gridview->count() == 0);
1041 QVERIFY(gridview->currentItem() == 0);
1042 QVERIFY(gridview->contentY() == 0);
1043 QVERIFY(gridview->currentIndex() == -1);
1045 // confirm sanity when adding an item to cleared list
1046 model.addItem("New", "1");
1047 QTRY_COMPARE(gridview->count(), 1);
1048 QVERIFY(gridview->currentItem() != 0);
1049 QVERIFY(gridview->currentIndex() == 0);
1054 void tst_QQuickGridView::moved()
1056 QFETCH(qreal, contentY);
1060 QFETCH(qreal, itemsOffsetAfterMove);
1064 QScopedPointer<QQuickView> canvas(createView());
1067 for (int i = 0; i < 30; i++)
1068 model.addItem("Item" + QString::number(i), "");
1070 QQmlContext *ctxt = canvas->rootContext();
1071 ctxt->setContextProperty("testModel", &model);
1072 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1073 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1075 canvas->setSource(testFileUrl("gridview1.qml"));
1077 qApp->processEvents();
1079 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1080 QTRY_VERIFY(gridview != 0);
1081 QQuickItem *contentItem = gridview->contentItem();
1082 QTRY_VERIFY(contentItem != 0);
1083 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1085 QQuickItem *currentItem = gridview->currentItem();
1086 QTRY_VERIFY(currentItem != 0);
1088 if (contentY != 0) {
1089 gridview->setContentY(contentY);
1090 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1093 model.moveItems(from, to, count);
1094 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1096 // Confirm items positioned correctly and indexes correct
1097 int firstVisibleIndex = qCeil(contentY / 60.0) * 3;
1098 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1099 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
1100 if (i >= firstVisibleIndex + 18) // index has moved out of view
1102 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1103 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1105 QTRY_COMPARE(item->x(), (i%3)*80.0);
1106 QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
1108 name = findItem<QQuickText>(contentItem, "textName", i);
1110 QTRY_COMPARE(name->text(), model.name(i));
1111 number = findItem<QQuickText>(contentItem, "textNumber", i);
1112 QVERIFY(number != 0);
1113 QTRY_COMPARE(number->text(), model.number(i));
1115 // current index should have been updated
1116 if (item == currentItem)
1117 QTRY_COMPARE(gridview->currentIndex(), i);
1121 void tst_QQuickGridView::moved_data()
1123 QTest::addColumn<qreal>("contentY");
1124 QTest::addColumn<int>("from");
1125 QTest::addColumn<int>("to");
1126 QTest::addColumn<int>("count");
1127 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1129 // model starts with 30 items, each 80x60, in area 240x320
1130 // 18 items should be visible at a time
1132 // The first visible item should not move upwards and out of the view
1133 // if items are moved/removed before it.
1136 QTest::newRow("move 1 forwards, within visible items")
1141 QTest::newRow("move 1 forwards, from non-visible -> visible")
1142 << 120.0 // show 6-23
1146 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1147 << 120.0 // // show 6-23
1151 QTest::newRow("move 1 forwards, from visible -> non-visible")
1156 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1162 QTest::newRow("move 1 backwards, within visible items")
1167 QTest::newRow("move 1 backwards, within visible items (to first index)")
1172 QTest::newRow("move 1 backwards, from non-visible -> visible")
1177 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1182 QTest::newRow("move 1 backwards, from visible -> non-visible")
1183 << 120.0 // show 6-23
1185 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1187 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1188 << 120.0 // show 6-23
1190 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1193 QTest::newRow("move multiple forwards, within visible items")
1198 QTest::newRow("move multiple backwards, within visible items (move first item)")
1203 QTest::newRow("move multiple forwards, before visible items")
1204 << 120.0 // show 6-23
1205 << 3 << 4 << 3 // 3, 4, 5 move to after 6
1206 << 60.0; // row of 3,4,5 has moved down
1208 QTest::newRow("move multiple forwards, from non-visible -> visible")
1209 << 120.0 // show 6-23
1211 << 60.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1213 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1214 << 120.0 // show 6-23
1216 << 60.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1218 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1221 << 60.0; // top two rows removed, third row is now the first visible
1223 QTest::newRow("move multiple forwards, to bottom of view")
1228 QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1233 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1238 QTest::newRow("move multiple forwards, from visible -> non-visible")
1243 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1249 QTest::newRow("move multiple backwards, within visible items")
1254 QTest::newRow("move multiple backwards, from non-visible -> visible")
1259 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1264 QTest::newRow("move multiple backwards, from visible -> non-visible")
1265 << 120.0 // show 6-23
1267 << -60.0; // to minimize movement, items are added above visible area, all items move up by 1 row
1269 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1270 << 120.0 // show 6-23
1272 << -60.0; // 16,17,18 move to above item 0, all items move up by 1 row
1275 void tst_QQuickGridView::multipleChanges()
1277 QFETCH(int, startCount);
1278 QFETCH(QList<ListChange>, changes);
1279 QFETCH(int, newCount);
1280 QFETCH(int, newCurrentIndex);
1282 QQuickView *canvas = createView();
1285 for (int i = 0; i < startCount; i++)
1286 model.addItem("Item" + QString::number(i), "");
1288 QQmlContext *ctxt = canvas->rootContext();
1289 ctxt->setContextProperty("testModel", &model);
1290 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1291 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1293 canvas->setSource(testFileUrl("gridview1.qml"));
1295 qApp->processEvents();
1297 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1298 QTRY_VERIFY(gridview != 0);
1299 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1301 for (int i=0; i<changes.count(); i++) {
1302 switch (changes[i].type) {
1303 case ListChange::Inserted:
1305 QList<QPair<QString, QString> > items;
1306 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1307 items << qMakePair(QString("new item " + j), QString::number(j));
1308 model.insertItems(changes[i].index, items);
1311 case ListChange::Removed:
1312 model.removeItems(changes[i].index, changes[i].count);
1313 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1315 case ListChange::Moved:
1316 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1317 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1319 case ListChange::SetCurrent:
1320 gridview->setCurrentIndex(changes[i].index);
1321 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1323 case ListChange::SetContentY:
1324 gridview->setContentY(changes[i].pos);
1325 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1330 QTRY_COMPARE(gridview->count(), newCount);
1331 QCOMPARE(gridview->count(), model.count());
1332 QTRY_COMPARE(gridview->currentIndex(), newCurrentIndex);
1336 QQuickItem *contentItem = gridview->contentItem();
1337 QTRY_VERIFY(contentItem != 0);
1338 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1339 for (int i=0; i < model.count() && i < itemCount; ++i) {
1340 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1341 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1342 name = findItem<QQuickText>(contentItem, "textName", i);
1344 QTRY_COMPARE(name->text(), model.name(i));
1345 number = findItem<QQuickText>(contentItem, "textNumber", i);
1346 QVERIFY(number != 0);
1347 QTRY_COMPARE(number->text(), model.number(i));
1353 void tst_QQuickGridView::multipleChanges_data()
1355 QTest::addColumn<int>("startCount");
1356 QTest::addColumn<QList<ListChange> >("changes");
1357 QTest::addColumn<int>("newCount");
1358 QTest::addColumn<int>("newCurrentIndex");
1360 QList<ListChange> changes;
1362 for (int i=1; i<30; i++)
1363 changes << ListChange::remove(0);
1364 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1366 changes << ListChange::remove(0);
1367 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1370 changes << ListChange::setCurrent(29);
1371 for (int i=29; i>0; i--)
1372 changes << ListChange::remove(i);
1373 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1375 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1376 << ListChange::remove(0, 1)
1377 << ListChange::insert(0, 1)
1380 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1381 << ListChange::setCurrent(2)
1382 << ListChange::remove(2, 1)
1383 << ListChange::insert(2, 1)
1386 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1387 << ListChange::setCurrent(1)
1388 << ListChange::remove(1, 3)
1389 << ListChange::insert(2, 2)
1392 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1393 << ListChange::setCurrent(2)
1394 << ListChange::remove(1, 3)
1395 << ListChange::move(1, 5, 1)
1398 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1399 << ListChange::setCurrent(5)
1400 << ListChange::remove(4, 3)
1401 << ListChange::move(4, 1, 1)
1405 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1406 << ListChange::insert(0, 2)
1407 << ListChange::insert(0, 4)
1408 << ListChange::insert(0, 6)
1411 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1412 << ListChange::insert(0, 2)
1413 << ListChange::insert(0, 4)
1414 << ListChange::insert(0, 6)
1415 << ListChange::setCurrent(3)
1416 << ListChange::insert(3, 2)
1419 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1420 << ListChange::insert(0, 30)
1421 << ListChange::remove(0, 30)
1424 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1425 << ListChange::insert(1)
1426 << ListChange::setCurrent(1)
1427 << ListChange::remove(1)
1430 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1431 << ListChange::insert(0, 10)
1432 << ListChange::remove(5, 10)
1435 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1436 << ListChange::insert(0, 3)
1437 << ListChange::move(0, 10, 3)
1440 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1441 << ListChange::insert(0, 3)
1442 << ListChange::move(0, 8, 5)
1445 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1446 << ListChange::setCurrent(9)
1447 << ListChange::insert(10, 3)
1448 << ListChange::move(8, 0, 5)
1452 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1453 << ListChange::setCurrent(1)
1454 << ListChange::move(1, 2, 2)
1455 << ListChange::move(2, 1, 2)
1458 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1459 << ListChange::setCurrent(2)
1460 << ListChange::move(1, 2, 3)
1461 << ListChange::move(3, 0, 5)
1464 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1465 << ListChange::setCurrent(5)
1466 << ListChange::move(5, 0, 1)
1467 << ListChange::remove(0)
1470 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1471 << ListChange::setCurrent(5)
1472 << ListChange::move(5, 0, 1)
1473 << ListChange::insert(0)
1476 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1477 << ListChange::setCurrent(1)
1478 << ListChange::move(5, 1, 3)
1479 << ListChange::remove(1, 3)
1482 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1483 << ListChange::setCurrent(5)
1484 << ListChange::move(5, 1, 3)
1485 << ListChange::insert(1, 5)
1488 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1489 << ListChange::setCurrent(3)
1490 << ListChange::move(0, 1, 2)
1491 << ListChange::insert(3, 5)
1495 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1496 << ListChange::insert(0, 5)
1497 << ListChange::setCurrent(-1)
1498 << ListChange::remove(0, 5)
1499 << ListChange::insert(0, 5)
1504 void tst_QQuickGridView::swapWithFirstItem()
1507 QQuickView *canvas = createView();
1510 for (int i = 0; i < 30; i++)
1511 model.addItem("Item" + QString::number(i), "");
1513 QQmlContext *ctxt = canvas->rootContext();
1514 ctxt->setContextProperty("testModel", &model);
1515 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1516 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1518 canvas->setSource(testFileUrl("gridview1.qml"));
1520 qApp->processEvents();
1522 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1523 QTRY_VERIFY(gridview != 0);
1525 // ensure content position is stable
1526 gridview->setContentY(0);
1527 model.moveItem(10, 0);
1528 QTRY_VERIFY(gridview->contentY() == 0);
1533 void tst_QQuickGridView::currentIndex()
1536 for (int i = 0; i < 60; i++)
1537 model.addItem("Item" + QString::number(i), QString::number(i));
1539 QQuickView *canvas = new QQuickView(0);
1540 canvas->setGeometry(0,0,240,320);
1543 QQmlContext *ctxt = canvas->rootContext();
1544 ctxt->setContextProperty("testModel", &model);
1546 QString filename(testFile("gridview-initCurrent.qml"));
1547 canvas->setSource(QUrl::fromLocalFile(filename));
1549 qApp->processEvents();
1551 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1552 QVERIFY(gridview != 0);
1553 QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1555 QQuickItem *contentItem = gridview->contentItem();
1556 QVERIFY(contentItem != 0);
1558 // current item should be third item
1559 QCOMPARE(gridview->currentIndex(), 35);
1560 QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1561 QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1562 QCOMPARE(gridview->contentY(), 400.0);
1564 gridview->moveCurrentIndexRight();
1565 QCOMPARE(gridview->currentIndex(), 36);
1566 gridview->moveCurrentIndexDown();
1567 QCOMPARE(gridview->currentIndex(), 39);
1568 gridview->moveCurrentIndexUp();
1569 QCOMPARE(gridview->currentIndex(), 36);
1570 gridview->moveCurrentIndexLeft();
1571 QCOMPARE(gridview->currentIndex(), 35);
1573 // wait until motion stops
1574 QTRY_VERIFY(gridview->verticalVelocity() == 0.0);
1577 gridview->setCurrentIndex(0);
1578 QCOMPARE(gridview->currentIndex(), 0);
1579 // confirm that the velocity is updated
1580 QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1582 gridview->moveCurrentIndexUp();
1583 QCOMPARE(gridview->currentIndex(), 0);
1585 gridview->moveCurrentIndexLeft();
1586 QCOMPARE(gridview->currentIndex(), 0);
1588 gridview->setCurrentIndex(model.count()-1);
1589 QCOMPARE(gridview->currentIndex(), model.count()-1);
1591 gridview->moveCurrentIndexRight();
1592 QCOMPARE(gridview->currentIndex(), model.count()-1);
1594 gridview->moveCurrentIndexDown();
1595 QCOMPARE(gridview->currentIndex(), model.count()-1);
1598 gridview->setWrapEnabled(true);
1600 gridview->setCurrentIndex(0);
1601 QCOMPARE(gridview->currentIndex(), 0);
1603 gridview->moveCurrentIndexLeft();
1604 QCOMPARE(gridview->currentIndex(), model.count()-1);
1606 qApp->processEvents();
1607 QTRY_COMPARE(gridview->contentY(), 880.0);
1609 gridview->moveCurrentIndexRight();
1610 QCOMPARE(gridview->currentIndex(), 0);
1612 QTRY_COMPARE(gridview->contentY(), 0.0);
1615 // footer should become visible if it is out of view, and then current index moves to the first row
1616 canvas->rootObject()->setProperty("showFooter", true);
1617 QTRY_VERIFY(gridview->footerItem());
1618 gridview->setCurrentIndex(model.count()-3);
1619 QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1620 gridview->setCurrentIndex(model.count()-2);
1621 QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1622 canvas->rootObject()->setProperty("showFooter", false);
1624 // header should become visible if it is out of view, and then current index moves to the last row
1625 canvas->rootObject()->setProperty("showHeader", true);
1626 QTRY_VERIFY(gridview->headerItem());
1627 gridview->setCurrentIndex(3);
1628 QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1629 gridview->setCurrentIndex(1);
1630 QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1631 canvas->rootObject()->setProperty("showHeader", false);
1635 canvas->requestActivateWindow();
1636 QTest::qWaitForWindowShown(canvas);
1637 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1639 gridview->setCurrentIndex(0);
1641 QTest::keyClick(canvas, Qt::Key_Down);
1642 QCOMPARE(gridview->currentIndex(), 3);
1644 QTest::keyClick(canvas, Qt::Key_Up);
1645 QCOMPARE(gridview->currentIndex(), 0);
1647 // hold down Key_Down
1648 for (int i=0; i<(model.count() / 3) - 1; i++) {
1649 QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
1650 QTRY_COMPARE(gridview->currentIndex(), i*3 + 3);
1652 QTest::keyRelease(canvas, Qt::Key_Down);
1653 QTRY_COMPARE(gridview->currentIndex(), 57);
1654 QTRY_COMPARE(gridview->contentY(), 880.0);
1657 for (int i=(model.count() / 3) - 1; i > 0; i--) {
1658 QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
1659 QTRY_COMPARE(gridview->currentIndex(), i*3 - 3);
1661 QTest::keyRelease(canvas, Qt::Key_Up);
1662 QTRY_COMPARE(gridview->currentIndex(), 0);
1663 QTRY_COMPARE(gridview->contentY(), 0.0);
1666 gridview->setFlow(QQuickGridView::TopToBottom);
1668 canvas->requestActivateWindow();
1669 QTest::qWaitForWindowShown(canvas);
1670 QVERIFY(qGuiApp->focusWindow() == canvas);
1671 qApp->processEvents();
1673 QTest::keyClick(canvas, Qt::Key_Right);
1674 QCOMPARE(gridview->currentIndex(), 5);
1676 QTest::keyClick(canvas, Qt::Key_Left);
1677 QCOMPARE(gridview->currentIndex(), 0);
1679 QTest::keyClick(canvas, Qt::Key_Down);
1680 QCOMPARE(gridview->currentIndex(), 1);
1682 QTest::keyClick(canvas, Qt::Key_Up);
1683 QCOMPARE(gridview->currentIndex(), 0);
1685 // hold down Key_Right
1686 for (int i=0; i<(model.count() / 5) - 1; i++) {
1687 QTest::simulateEvent(canvas, true, Qt::Key_Right, Qt::NoModifier, "", true);
1688 QTRY_COMPARE(gridview->currentIndex(), i*5 + 5);
1691 QTest::keyRelease(canvas, Qt::Key_Right);
1692 QTRY_COMPARE(gridview->currentIndex(), 55);
1693 QTRY_COMPARE(gridview->contentX(), 720.0);
1695 // hold down Key_Left
1696 for (int i=(model.count() / 5) - 1; i > 0; i--) {
1697 QTest::simulateEvent(canvas, true, Qt::Key_Left, Qt::NoModifier, "", true);
1698 QTRY_COMPARE(gridview->currentIndex(), i*5 - 5);
1700 QTest::keyRelease(canvas, Qt::Key_Left);
1701 QTRY_COMPARE(gridview->currentIndex(), 0);
1702 QTRY_COMPARE(gridview->contentX(), 0.0);
1705 // turn off auto highlight
1706 gridview->setHighlightFollowsCurrentItem(false);
1707 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1708 QVERIFY(gridview->highlightItem());
1709 qreal hlPosX = gridview->highlightItem()->x();
1710 qreal hlPosY = gridview->highlightItem()->y();
1712 gridview->setCurrentIndex(5);
1713 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1714 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1716 // insert item before currentIndex
1717 gridview->setCurrentIndex(28);
1718 model.insertItem(0, "Foo", "1111");
1719 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1721 // check removing highlight by setting currentIndex to -1;
1722 gridview->setCurrentIndex(-1);
1724 QCOMPARE(gridview->currentIndex(), -1);
1725 QVERIFY(!gridview->highlightItem());
1726 QVERIFY(!gridview->currentItem());
1728 gridview->setHighlightFollowsCurrentItem(true);
1730 gridview->setFlow(QQuickGridView::LeftToRight);
1731 gridview->setLayoutDirection(Qt::RightToLeft);
1733 canvas->requestActivateWindow();
1734 QTest::qWaitForWindowShown(canvas);
1735 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1736 qApp->processEvents();
1738 gridview->setCurrentIndex(35);
1740 QTest::keyClick(canvas, Qt::Key_Right);
1741 QCOMPARE(gridview->currentIndex(), 34);
1743 QTest::keyClick(canvas, Qt::Key_Down);
1744 QCOMPARE(gridview->currentIndex(), 37);
1746 QTest::keyClick(canvas, Qt::Key_Up);
1747 QCOMPARE(gridview->currentIndex(), 34);
1749 QTest::keyClick(canvas, Qt::Key_Left);
1750 QCOMPARE(gridview->currentIndex(), 35);
1753 // turn off auto highlight
1754 gridview->setHighlightFollowsCurrentItem(false);
1755 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1756 QVERIFY(gridview->highlightItem());
1757 hlPosX = gridview->highlightItem()->x();
1758 hlPosY = gridview->highlightItem()->y();
1760 gridview->setCurrentIndex(5);
1761 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1762 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1764 // insert item before currentIndex
1765 gridview->setCurrentIndex(28);
1766 model.insertItem(0, "Foo", "1111");
1767 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1769 // check removing highlight by setting currentIndex to -1;
1770 gridview->setCurrentIndex(-1);
1772 QCOMPARE(gridview->currentIndex(), -1);
1773 QVERIFY(!gridview->highlightItem());
1774 QVERIFY(!gridview->currentItem());
1779 void tst_QQuickGridView::noCurrentIndex()
1782 for (int i = 0; i < 60; i++)
1783 model.addItem("Item" + QString::number(i), QString::number(i));
1785 QQuickView *canvas = new QQuickView(0);
1786 canvas->setGeometry(0,0,240,320);
1788 QQmlContext *ctxt = canvas->rootContext();
1789 ctxt->setContextProperty("testModel", &model);
1791 QString filename(testFile("gridview-noCurrent.qml"));
1792 canvas->setSource(QUrl::fromLocalFile(filename));
1794 qApp->processEvents();
1796 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1797 QVERIFY(gridview != 0);
1798 QQuickItem *contentItem = gridview->contentItem();
1799 QVERIFY(contentItem != 0);
1800 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1802 // current index should be -1
1803 QCOMPARE(gridview->currentIndex(), -1);
1804 QVERIFY(!gridview->currentItem());
1805 QVERIFY(!gridview->highlightItem());
1806 QCOMPARE(gridview->contentY(), 0.0);
1808 gridview->setCurrentIndex(5);
1809 QCOMPARE(gridview->currentIndex(), 5);
1810 QVERIFY(gridview->currentItem());
1811 QVERIFY(gridview->highlightItem());
1816 void tst_QQuickGridView::changeFlow()
1818 QQuickView *canvas = createView();
1821 for (int i = 0; i < 30; i++)
1822 model.addItem("Item" + QString::number(i), QString::number(i));
1824 QQmlContext *ctxt = canvas->rootContext();
1825 ctxt->setContextProperty("testModel", &model);
1826 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1827 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1829 canvas->setSource(testFileUrl("gridview1.qml"));
1830 qApp->processEvents();
1832 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1833 QTRY_VERIFY(gridview != 0);
1835 QQuickItem *contentItem = gridview->contentItem();
1836 QTRY_VERIFY(contentItem != 0);
1838 // Confirm items positioned correctly and indexes correct
1839 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1840 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1841 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1842 if (!item) qWarning() << "Item" << i << "not found";
1844 QTRY_COMPARE(item->x(), qreal((i%3)*80));
1845 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1846 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1847 QTRY_VERIFY(name != 0);
1848 QTRY_COMPARE(name->text(), model.name(i));
1849 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1850 QTRY_VERIFY(number != 0);
1851 QTRY_COMPARE(number->text(), model.number(i));
1854 ctxt->setContextProperty("testTopToBottom", QVariant(true));
1856 // Confirm items positioned correctly and indexes correct
1857 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1858 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1859 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1860 if (!item) qWarning() << "Item" << i << "not found";
1862 QTRY_COMPARE(item->x(), qreal((i/5)*80));
1863 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1864 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1865 QTRY_VERIFY(name != 0);
1866 QTRY_COMPARE(name->text(), model.name(i));
1867 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1868 QTRY_VERIFY(number != 0);
1869 QTRY_COMPARE(number->text(), model.number(i));
1872 ctxt->setContextProperty("testRightToLeft", QVariant(true));
1874 // Confirm items positioned correctly and indexes correct
1875 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1876 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1877 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1878 if (!item) qWarning() << "Item" << i << "not found";
1880 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
1881 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1882 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1883 QTRY_VERIFY(name != 0);
1884 QTRY_COMPARE(name->text(), model.name(i));
1885 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1886 QTRY_VERIFY(number != 0);
1887 QTRY_COMPARE(number->text(), model.number(i));
1889 gridview->setContentX(100);
1890 QTRY_COMPARE(gridview->contentX(), 100.);
1891 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1892 QTRY_COMPARE(gridview->contentX(), 0.);
1894 // Confirm items positioned correctly and indexes correct
1895 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1896 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1897 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1898 if (!item) qWarning() << "Item" << i << "not found";
1900 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
1901 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1902 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1903 QTRY_VERIFY(name != 0);
1904 QTRY_COMPARE(name->text(), model.name(i));
1905 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1906 QTRY_VERIFY(number != 0);
1907 QTRY_COMPARE(number->text(), model.number(i));
1913 void tst_QQuickGridView::defaultValues()
1916 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
1917 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1919 QTRY_VERIFY(obj != 0);
1920 QTRY_VERIFY(obj->model() == QVariant());
1921 QTRY_VERIFY(obj->delegate() == 0);
1922 QTRY_COMPARE(obj->currentIndex(), -1);
1923 QTRY_VERIFY(obj->currentItem() == 0);
1924 QTRY_COMPARE(obj->count(), 0);
1925 QTRY_VERIFY(obj->highlight() == 0);
1926 QTRY_VERIFY(obj->highlightItem() == 0);
1927 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
1928 QTRY_VERIFY(obj->flow() == 0);
1929 QTRY_COMPARE(obj->isWrapEnabled(), false);
1930 QTRY_COMPARE(obj->cacheBuffer(), 0);
1931 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
1932 QTRY_COMPARE(obj->cellHeight(), qreal(100));
1936 void tst_QQuickGridView::properties()
1939 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
1940 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1942 QTRY_VERIFY(obj != 0);
1943 QTRY_VERIFY(obj->model() != QVariant());
1944 QTRY_VERIFY(obj->delegate() != 0);
1945 QTRY_COMPARE(obj->currentIndex(), 0);
1946 QTRY_VERIFY(obj->currentItem() != 0);
1947 QTRY_COMPARE(obj->count(), 4);
1948 QTRY_VERIFY(obj->highlight() != 0);
1949 QTRY_VERIFY(obj->highlightItem() != 0);
1950 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
1951 QTRY_VERIFY(obj->flow() == 0);
1952 QTRY_COMPARE(obj->isWrapEnabled(), true);
1953 QTRY_COMPARE(obj->cacheBuffer(), 200);
1954 QTRY_COMPARE(obj->cellWidth(), qreal(100));
1955 QTRY_COMPARE(obj->cellHeight(), qreal(100));
1959 void tst_QQuickGridView::propertyChanges()
1961 QQuickView *canvas = createView();
1962 QTRY_VERIFY(canvas);
1963 canvas->setSource(testFileUrl("propertychangestest.qml"));
1965 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
1966 QTRY_VERIFY(gridView);
1968 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
1969 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
1970 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
1971 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
1973 QTRY_COMPARE(gridView->isWrapEnabled(), true);
1974 QTRY_COMPARE(gridView->cacheBuffer(), 10);
1975 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
1977 gridView->setWrapEnabled(false);
1978 gridView->setCacheBuffer(3);
1979 gridView->setFlow(QQuickGridView::TopToBottom);
1981 QTRY_COMPARE(gridView->isWrapEnabled(), false);
1982 QTRY_COMPARE(gridView->cacheBuffer(), 3);
1983 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
1985 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1986 QTRY_COMPARE(cacheBufferSpy.count(),1);
1987 QTRY_COMPARE(flowSpy.count(),1);
1989 gridView->setWrapEnabled(false);
1990 gridView->setCacheBuffer(3);
1991 gridView->setFlow(QQuickGridView::TopToBottom);
1993 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1994 QTRY_COMPARE(cacheBufferSpy.count(),1);
1995 QTRY_COMPARE(flowSpy.count(),1);
1997 gridView->setFlow(QQuickGridView::LeftToRight);
1998 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
2000 gridView->setWrapEnabled(true);
2001 gridView->setCacheBuffer(5);
2002 gridView->setLayoutDirection(Qt::RightToLeft);
2004 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2005 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2006 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2008 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2009 QTRY_COMPARE(cacheBufferSpy.count(),2);
2010 QTRY_COMPARE(layoutSpy.count(),1);
2011 QTRY_COMPARE(flowSpy.count(),2);
2013 gridView->setWrapEnabled(true);
2014 gridView->setCacheBuffer(5);
2015 gridView->setLayoutDirection(Qt::RightToLeft);
2017 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2018 QTRY_COMPARE(cacheBufferSpy.count(),2);
2019 QTRY_COMPARE(layoutSpy.count(),1);
2020 QTRY_COMPARE(flowSpy.count(),2);
2022 gridView->setFlow(QQuickGridView::TopToBottom);
2023 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
2024 QTRY_COMPARE(flowSpy.count(),3);
2026 gridView->setFlow(QQuickGridView::TopToBottom);
2027 QTRY_COMPARE(flowSpy.count(),3);
2032 void tst_QQuickGridView::componentChanges()
2034 QQuickView *canvas = createView();
2035 QTRY_VERIFY(canvas);
2036 canvas->setSource(testFileUrl("propertychangestest.qml"));
2038 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2039 QTRY_VERIFY(gridView);
2041 QQmlComponent component(canvas->engine());
2042 component.setData("import QtQuick 1.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2044 QQmlComponent delegateComponent(canvas->engine());
2045 delegateComponent.setData("import QtQuick 1.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2047 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2048 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2049 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2050 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2052 gridView->setHighlight(&component);
2053 gridView->setDelegate(&delegateComponent);
2054 gridView->setHeader(&component);
2055 gridView->setFooter(&component);
2057 QTRY_COMPARE(gridView->highlight(), &component);
2058 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2059 QTRY_COMPARE(gridView->header(), &component);
2060 QTRY_COMPARE(gridView->footer(), &component);
2062 QTRY_COMPARE(highlightSpy.count(),1);
2063 QTRY_COMPARE(delegateSpy.count(),1);
2064 QTRY_COMPARE(headerSpy.count(),1);
2065 QTRY_COMPARE(footerSpy.count(),1);
2067 gridView->setHighlight(&component);
2068 gridView->setDelegate(&delegateComponent);
2069 gridView->setHeader(&component);
2070 gridView->setFooter(&component);
2072 QTRY_COMPARE(highlightSpy.count(),1);
2073 QTRY_COMPARE(delegateSpy.count(),1);
2074 QTRY_COMPARE(headerSpy.count(),1);
2075 QTRY_COMPARE(footerSpy.count(),1);
2080 void tst_QQuickGridView::modelChanges()
2082 QQuickView *canvas = createView();
2083 QTRY_VERIFY(canvas);
2084 canvas->setSource(testFileUrl("propertychangestest.qml"));
2086 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2087 QTRY_VERIFY(gridView);
2089 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2090 QTRY_VERIFY(alternateModel);
2091 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2092 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2094 gridView->setModel(modelVariant);
2095 QTRY_COMPARE(gridView->model(), modelVariant);
2096 QTRY_COMPARE(modelSpy.count(),1);
2098 gridView->setModel(modelVariant);
2099 QTRY_COMPARE(modelSpy.count(),1);
2101 gridView->setModel(QVariant());
2102 QTRY_COMPARE(modelSpy.count(),2);
2106 void tst_QQuickGridView::positionViewAtIndex()
2108 QQuickView *canvas = createView();
2111 for (int i = 0; i < 40; i++)
2112 model.addItem("Item" + QString::number(i), "");
2114 QQmlContext *ctxt = canvas->rootContext();
2115 ctxt->setContextProperty("testModel", &model);
2116 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2117 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2119 canvas->setSource(testFileUrl("gridview1.qml"));
2121 qApp->processEvents();
2123 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2124 QTRY_VERIFY(gridview != 0);
2125 QQuickItem *contentItem = gridview->contentItem();
2126 QTRY_VERIFY(contentItem != 0);
2127 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2129 // Confirm items positioned correctly
2130 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2131 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2132 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2133 if (!item) qWarning() << "Item" << i << "not found";
2135 QTRY_COMPARE(item->x(), (i%3)*80.);
2136 QTRY_COMPARE(item->y(), (i/3)*60.);
2139 // Position on a currently visible item
2140 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2141 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2142 QTRY_COMPARE(gridview->contentY(), 60.);
2144 // Confirm items positioned correctly
2145 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2146 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2147 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2148 if (!item) qWarning() << "Item" << i << "not found";
2150 QTRY_COMPARE(item->x(), (i%3)*80.);
2151 QTRY_COMPARE(item->y(), (i/3)*60.);
2154 // Position on an item beyond the visible items
2155 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2156 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2157 QTRY_COMPARE(gridview->contentY(), 420.);
2159 // Confirm items positioned correctly
2160 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2161 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2162 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2163 if (!item) qWarning() << "Item" << i << "not found";
2165 QTRY_COMPARE(item->x(), (i%3)*80.);
2166 QTRY_COMPARE(item->y(), (i/3)*60.);
2169 // Position on an item that would leave empty space if positioned at the top
2170 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2171 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2172 QTRY_COMPARE(gridview->contentY(), 520.);
2174 // Confirm items positioned correctly
2175 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2176 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2177 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2178 if (!item) qWarning() << "Item" << i << "not found";
2180 QTRY_COMPARE(item->x(), (i%3)*80.);
2181 QTRY_COMPARE(item->y(), (i/3)*60.);
2184 // Position at the beginning again
2185 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2186 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2187 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2188 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2189 QTRY_COMPARE(gridview->contentY(), 0.);
2191 // Confirm items positioned correctly
2192 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2193 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2194 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2195 if (!item) qWarning() << "Item" << i << "not found";
2197 QTRY_COMPARE(item->x(), (i%3)*80.);
2198 QTRY_COMPARE(item->y(), (i/3)*60.);
2202 gridview->positionViewAtIndex(30, QQuickGridView::End);
2203 QTRY_COMPARE(gridview->contentY(), 340.);
2205 // Position in Center
2206 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2207 QTRY_COMPARE(gridview->contentY(), 170.);
2209 // Ensure at least partially visible
2210 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2211 QTRY_COMPARE(gridview->contentY(), 170.);
2213 gridview->setContentY(302);
2214 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2215 QTRY_COMPARE(gridview->contentY(), 302.);
2217 gridview->setContentY(360);
2218 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2219 QTRY_COMPARE(gridview->contentY(), 300.);
2221 gridview->setContentY(60);
2222 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2223 QTRY_COMPARE(gridview->contentY(), 60.);
2225 gridview->setContentY(20);
2226 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2227 QTRY_COMPARE(gridview->contentY(), 100.);
2229 // Ensure completely visible
2230 gridview->setContentY(120);
2231 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2232 QTRY_COMPARE(gridview->contentY(), 120.);
2234 gridview->setContentY(302);
2235 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2236 QTRY_COMPARE(gridview->contentY(), 300.);
2238 gridview->setContentY(60);
2239 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2240 QTRY_COMPARE(gridview->contentY(), 100.);
2242 // Test for Top To Bottom layout
2243 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2245 // Confirm items positioned correctly
2246 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2247 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2248 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2249 if (!item) qWarning() << "Item" << i << "not found";
2251 QTRY_COMPARE(item->x(), (i/5)*80.);
2252 QTRY_COMPARE(item->y(), (i%5)*60.);
2256 gridview->positionViewAtIndex(30, QQuickGridView::End);
2257 QTRY_COMPARE(gridview->contentX(), 320.);
2258 QTRY_COMPARE(gridview->contentY(), 0.);
2260 // Position in Center
2261 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2262 QTRY_COMPARE(gridview->contentX(), 160.);
2264 // Ensure at least partially visible
2265 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2266 QTRY_COMPARE(gridview->contentX(), 160.);
2268 gridview->setContentX(170);
2269 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2270 QTRY_COMPARE(gridview->contentX(), 170.);
2272 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2273 QTRY_COMPARE(gridview->contentX(), 320.);
2275 gridview->setContentX(170);
2276 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2277 QTRY_COMPARE(gridview->contentX(), 240.);
2279 // positionViewAtBeginning
2280 gridview->positionViewAtBeginning();
2281 QTRY_COMPARE(gridview->contentX(), 0.);
2283 gridview->setContentX(80);
2284 canvas->rootObject()->setProperty("showHeader", true);
2285 gridview->positionViewAtBeginning();
2286 QTRY_COMPARE(gridview->contentX(), -30.);
2288 // positionViewAtEnd
2289 gridview->positionViewAtEnd();
2290 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2292 gridview->setContentX(80);
2293 canvas->rootObject()->setProperty("showFooter", true);
2294 gridview->positionViewAtEnd();
2295 QTRY_COMPARE(gridview->contentX(), 430.);
2297 // set current item to outside visible view, position at beginning
2298 // and ensure highlight moves to current item
2299 gridview->setCurrentIndex(6);
2300 gridview->positionViewAtBeginning();
2301 QTRY_COMPARE(gridview->contentX(), -30.);
2302 QVERIFY(gridview->highlightItem());
2303 QCOMPARE(gridview->highlightItem()->x(), 80.);
2308 void tst_QQuickGridView::snapping()
2310 QQuickView *canvas = createView();
2313 for (int i = 0; i < 40; i++)
2314 model.addItem("Item" + QString::number(i), "");
2316 QQmlContext *ctxt = canvas->rootContext();
2317 ctxt->setContextProperty("testModel", &model);
2318 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2319 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2321 canvas->setSource(testFileUrl("gridview1.qml"));
2322 qApp->processEvents();
2324 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2325 QTRY_VERIFY(gridview != 0);
2327 gridview->setHeight(220);
2328 QCOMPARE(gridview->height(), 220.);
2330 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2331 QCOMPARE(gridview->contentY(), 80.);
2333 gridview->setContentY(0);
2334 QCOMPARE(gridview->contentY(), 0.);
2336 gridview->setSnapMode(QQuickGridView::SnapToRow);
2337 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2339 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2340 QCOMPARE(gridview->contentY(), 60.);
2342 gridview->positionViewAtIndex(15, QQuickGridView::End);
2343 QCOMPARE(gridview->contentY(), 120.);
2349 void tst_QQuickGridView::mirroring()
2351 QQuickView *canvasA = createView();
2352 canvasA->setSource(testFileUrl("mirroring.qml"));
2353 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2354 QTRY_VERIFY(gridviewA != 0);
2356 QQuickView *canvasB = createView();
2357 canvasB->setSource(testFileUrl("mirroring.qml"));
2358 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2359 QTRY_VERIFY(gridviewA != 0);
2360 qApp->processEvents();
2362 QList<QString> objectNames;
2363 objectNames << "item1" << "item2"; // << "item3"
2365 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2366 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2367 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2370 foreach (const QString objectName, objectNames)
2371 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2373 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2374 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2377 foreach (const QString objectName, objectNames)
2378 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2380 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2381 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2382 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2384 // LTR != LTR+mirror
2385 foreach (const QString objectName, objectNames)
2386 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2388 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2390 // RTL == LTR+mirror
2391 foreach (const QString objectName, objectNames)
2392 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2394 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2396 // RTL != RTL+mirror
2397 foreach (const QString objectName, objectNames)
2398 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2400 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2402 // LTR == RTL+mirror
2403 foreach (const QString objectName, objectNames)
2404 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2410 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2412 QQuickView *canvas = createView();
2415 for (int i = 0; i < 40; i++)
2416 model.addItem("Item" + QString::number(i), "");
2418 QQmlContext *ctxt = canvas->rootContext();
2419 ctxt->setContextProperty("testModel", &model);
2420 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2421 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2423 canvas->setSource(testFileUrl("gridview1.qml"));
2424 qApp->processEvents();
2426 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2427 QTRY_VERIFY(gridview != 0);
2429 QQuickItem *contentItem = gridview->contentItem();
2430 QTRY_VERIFY(contentItem != 0);
2432 // Confirm items positioned correctly
2433 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2434 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2435 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2436 if (!item) qWarning() << "Item" << i << "not found";
2438 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2439 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2442 // Position on a currently visible item
2443 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2444 QTRY_COMPARE(gridview->contentX(), -320.);
2446 // Confirm items positioned correctly
2447 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2448 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2449 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2450 if (!item) qWarning() << "Item" << i << "not found";
2452 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2453 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2456 // Position on an item beyond the visible items
2457 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2458 QTRY_COMPARE(gridview->contentX(), -560.);
2460 // Confirm items positioned correctly
2461 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2462 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2463 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2464 if (!item) qWarning() << "Item" << i << "not found";
2466 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2467 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2470 // Position on an item that would leave empty space if positioned at the top
2471 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2472 QTRY_COMPARE(gridview->contentX(), -640.);
2474 // Confirm items positioned correctly
2475 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2476 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2477 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2478 if (!item) qWarning() << "Item" << i << "not found";
2480 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2481 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2484 // Position at the beginning again
2485 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2486 QTRY_COMPARE(gridview->contentX(), -240.);
2488 // Confirm items positioned correctly
2489 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2490 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2491 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2492 if (!item) qWarning() << "Item" << i << "not found";
2494 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2495 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2499 gridview->positionViewAtIndex(30, QQuickGridView::End);
2500 QTRY_COMPARE(gridview->contentX(), -560.);
2502 // Position in Center
2503 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2504 QTRY_COMPARE(gridview->contentX(), -400.);
2506 // Ensure at least partially visible
2507 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2508 QTRY_COMPARE(gridview->contentX(), -400.);
2510 gridview->setContentX(-555.);
2511 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2512 QTRY_COMPARE(gridview->contentX(), -555.);
2514 gridview->setContentX(-239);
2515 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2516 QTRY_COMPARE(gridview->contentX(), -320.);
2518 gridview->setContentX(-239);
2519 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2520 QTRY_COMPARE(gridview->contentX(), -400.);
2522 gridview->setContentX(-640);
2523 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2524 QTRY_COMPARE(gridview->contentX(), -560.);
2526 // Ensure completely visible
2527 gridview->setContentX(-400);
2528 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2529 QTRY_COMPARE(gridview->contentX(), -400.);
2531 gridview->setContentX(-315);
2532 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2533 QTRY_COMPARE(gridview->contentX(), -320.);
2535 gridview->setContentX(-640);
2536 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2537 QTRY_COMPARE(gridview->contentX(), -560.);
2542 void tst_QQuickGridView::resetModel()
2544 QQuickView *canvas = createView();
2546 QStringList strings;
2547 strings << "one" << "two" << "three";
2548 QStringListModel model(strings);
2550 QQmlContext *ctxt = canvas->rootContext();
2551 ctxt->setContextProperty("testModel", &model);
2553 canvas->setSource(testFileUrl("displaygrid.qml"));
2555 qApp->processEvents();
2557 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2558 QTRY_VERIFY(gridview != 0);
2559 QQuickItem *contentItem = gridview->contentItem();
2560 QTRY_VERIFY(contentItem != 0);
2561 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2563 QTRY_COMPARE(gridview->count(), model.rowCount());
2565 for (int i = 0; i < model.rowCount(); ++i) {
2566 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2567 QTRY_VERIFY(display != 0);
2568 QTRY_COMPARE(display->text(), strings.at(i));
2572 strings << "four" << "five" << "six" << "seven";
2573 model.setStringList(strings);
2575 QTRY_COMPARE(gridview->count(), model.rowCount());
2577 for (int i = 0; i < model.rowCount(); ++i) {
2578 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2579 QTRY_VERIFY(display != 0);
2580 QTRY_COMPARE(display->text(), strings.at(i));
2586 void tst_QQuickGridView::enforceRange()
2588 QQuickView *canvas = createView();
2591 for (int i = 0; i < 30; i++)
2592 model.addItem("Item" + QString::number(i), "");
2594 QQmlContext *ctxt = canvas->rootContext();
2595 ctxt->setContextProperty("testModel", &model);
2596 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2597 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2599 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2601 qApp->processEvents();
2602 QVERIFY(canvas->rootObject() != 0);
2604 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2605 QTRY_VERIFY(gridview != 0);
2607 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2608 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2609 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2610 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2612 QQuickItem *contentItem = gridview->contentItem();
2613 QTRY_VERIFY(contentItem != 0);
2615 // view should be positioned at the top of the range.
2616 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2618 QTRY_COMPARE(gridview->contentY(), -100.0);
2620 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2621 QTRY_VERIFY(name != 0);
2622 QTRY_COMPARE(name->text(), model.name(0));
2623 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2624 QTRY_VERIFY(number != 0);
2625 QTRY_COMPARE(number->text(), model.number(0));
2627 // Check currentIndex is updated when contentItem moves
2628 gridview->setContentY(0);
2629 QTRY_COMPARE(gridview->currentIndex(), 2);
2631 gridview->setCurrentIndex(5);
2632 QTRY_COMPARE(gridview->contentY(), 100.);
2635 for (int i = 0; i < 5; i++)
2636 model2.addItem("Item" + QString::number(i), "");
2638 ctxt->setContextProperty("testModel", &model2);
2639 QCOMPARE(gridview->count(), 5);
2644 void tst_QQuickGridView::enforceRange_rightToLeft()
2646 QQuickView *canvas = createView();
2649 for (int i = 0; i < 30; i++)
2650 model.addItem("Item" + QString::number(i), "");
2652 QQmlContext *ctxt = canvas->rootContext();
2653 ctxt->setContextProperty("testModel", &model);
2654 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2655 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2657 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2658 qApp->processEvents();
2659 QVERIFY(canvas->rootObject() != 0);
2661 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2662 QTRY_VERIFY(gridview != 0);
2664 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2665 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2666 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2668 QQuickItem *contentItem = gridview->contentItem();
2669 QVERIFY(contentItem != 0);
2671 // view should be positioned at the top of the range.
2672 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2674 QTRY_COMPARE(gridview->contentX(), -140.);
2675 QTRY_COMPARE(gridview->contentY(), 0.0);
2677 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2678 QTRY_VERIFY(name != 0);
2679 QTRY_COMPARE(name->text(), model.name(0));
2680 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2681 QTRY_VERIFY(number != 0);
2682 QTRY_COMPARE(number->text(), model.number(0));
2684 // Check currentIndex is updated when contentItem moves
2685 gridview->setContentX(-240);
2686 QTRY_COMPARE(gridview->currentIndex(), 3);
2688 gridview->setCurrentIndex(7);
2689 QTRY_COMPARE(gridview->contentX(), -340.);
2690 QTRY_COMPARE(gridview->contentY(), 0.0);
2693 for (int i = 0; i < 5; i++)
2694 model2.addItem("Item" + QString::number(i), "");
2696 ctxt->setContextProperty("testModel", &model2);
2697 QCOMPARE(gridview->count(), 5);
2702 void tst_QQuickGridView::QTBUG_8456()
2704 QQuickView *canvas = createView();
2706 canvas->setSource(testFileUrl("setindex.qml"));
2707 qApp->processEvents();
2709 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2710 QTRY_VERIFY(gridview != 0);
2712 QTRY_COMPARE(gridview->currentIndex(), 0);
2717 void tst_QQuickGridView::manualHighlight()
2719 QQuickView *canvas = createView();
2721 QString filename(testFile("manual-highlight.qml"));
2722 canvas->setSource(QUrl::fromLocalFile(filename));
2724 qApp->processEvents();
2726 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2727 QTRY_VERIFY(gridview != 0);
2729 QQuickItem *contentItem = gridview->contentItem();
2730 QTRY_VERIFY(contentItem != 0);
2732 QTRY_COMPARE(gridview->currentIndex(), 0);
2733 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2734 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2735 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2737 gridview->setCurrentIndex(2);
2739 QTRY_COMPARE(gridview->currentIndex(), 2);
2740 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2741 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2742 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2744 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
2746 QTRY_COMPARE(gridview->currentIndex(), 2);
2747 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2748 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2749 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2751 gridview->setFlow(QQuickGridView::TopToBottom);
2752 QTRY_COMPARE(gridview->flow(), QQuickGridView::TopToBottom);
2754 gridview->setCurrentIndex(0);
2755 QTRY_COMPARE(gridview->currentIndex(), 0);
2756 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2757 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2758 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2764 void tst_QQuickGridView::footer()
2766 QFETCH(QQuickGridView::Flow, flow);
2767 QFETCH(Qt::LayoutDirection, layoutDirection);
2768 QFETCH(QPointF, initialFooterPos);
2769 QFETCH(QPointF, changedFooterPos);
2770 QFETCH(QPointF, initialContentPos);
2771 QFETCH(QPointF, changedContentPos);
2772 QFETCH(QPointF, firstDelegatePos);
2773 QFETCH(QPointF, resizeContentPos);
2775 QQuickView *canvas = createView();
2779 for (int i = 0; i < 7; i++)
2780 model.addItem("Item" + QString::number(i), "");
2782 QQmlContext *ctxt = canvas->rootContext();
2783 ctxt->setContextProperty("testModel", &model);
2785 canvas->setSource(testFileUrl("footer.qml"));
2786 qApp->processEvents();
2788 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2789 QTRY_VERIFY(gridview != 0);
2790 gridview->setFlow(flow);
2791 gridview->setLayoutDirection(layoutDirection);
2793 QQuickItem *contentItem = gridview->contentItem();
2794 QTRY_VERIFY(contentItem != 0);
2796 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
2799 QVERIFY(footer == gridview->footerItem());
2801 QCOMPARE(footer->pos(), initialFooterPos);
2802 QCOMPARE(footer->width(), 100.);
2803 QCOMPARE(footer->height(), 30.);
2804 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2806 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2808 QCOMPARE(item->pos(), firstDelegatePos);
2810 if (flow == QQuickGridView::LeftToRight) {
2811 // shrink by one row
2812 model.removeItem(2);
2813 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
2815 // shrink by one column
2816 model.removeItem(2);
2817 model.removeItem(3);
2818 if (layoutDirection == Qt::LeftToRight)
2819 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
2821 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
2827 QPointF posWhenNoItems(0, 0);
2828 if (layoutDirection == Qt::RightToLeft)
2829 posWhenNoItems.setX(flow == QQuickGridView::LeftToRight ? gridview->width() - footer->width() : -footer->width());
2830 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2832 // if header is present, it's at a negative pos, so the footer should not move
2833 canvas->rootObject()->setProperty("showHeader", true);
2834 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
2835 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2836 canvas->rootObject()->setProperty("showHeader", false);
2839 for (int i = 0; i < 30; i++)
2840 model.addItem("Item" + QString::number(i), "");
2842 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
2843 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
2845 QCOMPARE(footerItemSpy.count(), 1);
2847 footer = findItem<QQuickText>(contentItem, "footer");
2849 footer = findItem<QQuickText>(contentItem, "footer2");
2852 QVERIFY(footer == gridview->footerItem());
2854 QCOMPARE(footer->pos(), changedFooterPos);
2855 QCOMPARE(footer->width(), 50.);
2856 QCOMPARE(footer->height(), 20.);
2857 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2859 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2861 QCOMPARE(item->pos(), firstDelegatePos);
2863 gridview->positionViewAtEnd();
2864 footer->setHeight(10);
2865 footer->setWidth(40);
2866 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
2871 void tst_QQuickGridView::footer_data()
2873 QTest::addColumn<QQuickGridView::Flow>("flow");
2874 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2875 QTest::addColumn<QPointF>("initialFooterPos");
2876 QTest::addColumn<QPointF>("changedFooterPos");
2877 QTest::addColumn<QPointF>("initialContentPos");
2878 QTest::addColumn<QPointF>("changedContentPos");
2879 QTest::addColumn<QPointF>("firstDelegatePos");
2880 QTest::addColumn<QPointF>("resizeContentPos");
2882 // footer1 = 100 x 30
2883 // footer2 = 50 x 20
2886 // view height = 320
2888 // footer below items, bottom left
2889 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
2890 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
2891 << QPointF(0, 10 * 60) // 30 items = 10 rows
2895 << QPointF(0, 10 * 60 - 320 + 10);
2897 // footer below items, bottom right
2898 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
2899 << QPointF(240 - 100, 3 * 60)
2900 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
2903 << QPointF(240 - 80, 0)
2904 << QPointF(0, 10 * 60 - 320 + 10);
2906 // footer to right of items
2907 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
2908 << QPointF(2 * 80, 0) // 2 columns, cell width 80
2909 << QPointF(6 * 80, 0) // 30 items = 6 columns
2913 << QPointF(6 * 80 - 240 + 40, 0);
2915 // footer to left of items
2916 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
2917 << QPointF(-(2 * 80) - 100, 0)
2918 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
2920 << QPointF(-240, 0) // unchanged, footer change doesn't change content pos
2922 << QPointF(-(6 * 80) - 40, 0);
2925 void tst_QQuickGridView::header()
2927 QFETCH(QQuickGridView::Flow, flow);
2928 QFETCH(Qt::LayoutDirection, layoutDirection);
2929 QFETCH(QPointF, initialHeaderPos);
2930 QFETCH(QPointF, changedHeaderPos);
2931 QFETCH(QPointF, initialContentPos);
2932 QFETCH(QPointF, changedContentPos);
2933 QFETCH(QPointF, firstDelegatePos);
2934 QFETCH(QPointF, resizeContentPos);
2937 for (int i = 0; i < 30; i++)
2938 model.addItem("Item" + QString::number(i), "");
2940 QQuickView *canvas = createView();
2941 canvas->rootContext()->setContextProperty("testModel", &model);
2942 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
2943 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
2944 canvas->setSource(testFileUrl("header.qml"));
2946 qApp->processEvents();
2948 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2949 QTRY_VERIFY(gridview != 0);
2950 gridview->setFlow(flow);
2951 gridview->setLayoutDirection(layoutDirection);
2952 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2954 QQuickItem *contentItem = gridview->contentItem();
2955 QTRY_VERIFY(contentItem != 0);
2957 QQuickText *header = findItem<QQuickText>(contentItem, "header");
2960 QVERIFY(header == gridview->headerItem());
2962 QCOMPARE(header->pos(), initialHeaderPos);
2963 QCOMPARE(header->width(), 100.);
2964 QCOMPARE(header->height(), 30.);
2965 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2967 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2969 QCOMPARE(item->pos(), firstDelegatePos);
2972 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2973 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
2975 for (int i = 0; i < 30; i++)
2976 model.addItem("Item" + QString::number(i), "");
2978 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
2979 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
2981 QCOMPARE(headerItemSpy.count(), 1);
2983 header = findItem<QQuickText>(contentItem, "header");
2985 header = findItem<QQuickText>(contentItem, "header2");
2988 QVERIFY(header == gridview->headerItem());
2990 QCOMPARE(header->pos(), changedHeaderPos);
2991 QCOMPARE(header->width(), 50.);
2992 QCOMPARE(header->height(), 20.);
2993 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2995 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2997 QCOMPARE(item->pos(), firstDelegatePos);
2999 header->setHeight(10);
3000 header->setWidth(40);
3001 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3006 // QTBUG-21207 header should become visible if view resizes from initial empty size
3008 canvas = createView();
3009 canvas->rootContext()->setContextProperty("testModel", &model);
3010 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3011 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3012 canvas->setSource(testFileUrl("header.qml"));
3014 qApp->processEvents();
3016 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3017 QTRY_VERIFY(gridview != 0);
3018 gridview->setFlow(flow);
3019 gridview->setLayoutDirection(layoutDirection);
3020 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3022 gridview->setWidth(240);
3023 gridview->setHeight(320);
3024 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3025 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3030 void tst_QQuickGridView::header_data()
3032 QTest::addColumn<QQuickGridView::Flow>("flow");
3033 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3034 QTest::addColumn<QPointF>("initialHeaderPos");
3035 QTest::addColumn<QPointF>("changedHeaderPos");
3036 QTest::addColumn<QPointF>("initialContentPos");
3037 QTest::addColumn<QPointF>("changedContentPos");
3038 QTest::addColumn<QPointF>("firstDelegatePos");
3039 QTest::addColumn<QPointF>("resizeContentPos");
3041 // header1 = 100 x 30
3042 // header2 = 50 x 20
3046 // header above items, top left
3047 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
3055 // header above items, top right
3056 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
3057 << QPointF(240 - 100, -30)
3058 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3064 // header to left of items
3065 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
3073 // header to right of items
3074 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
3077 << QPointF(-(240 - 100), 0)
3078 << QPointF(-(240 - 50), 0)
3080 << QPointF(-(240 - 40), 0);
3083 void tst_QQuickGridView::resizeViewAndRepaint()
3085 QQuickView *canvas = createView();
3089 for (int i = 0; i < 40; i++)
3090 model.addItem("Item" + QString::number(i), "");
3092 QQmlContext *ctxt = canvas->rootContext();
3093 ctxt->setContextProperty("testModel", &model);
3094 ctxt->setContextProperty("initialWidth", 240);
3095 ctxt->setContextProperty("initialHeight", 100);
3097 canvas->setSource(testFileUrl("resizeview.qml"));
3099 qApp->processEvents();
3101 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3102 QTRY_VERIFY(gridview != 0);
3103 QQuickItem *contentItem = gridview->contentItem();
3104 QTRY_VERIFY(contentItem != 0);
3105 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3107 // item at index 10 should not be currently visible
3108 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3110 gridview->setHeight(320);
3111 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3113 gridview->setHeight(100);
3114 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3116 // Ensure we handle -ve sizes
3117 gridview->setHeight(-100);
3118 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3120 gridview->setCacheBuffer(120);
3121 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3123 // ensure items in cache become visible
3124 gridview->setHeight(120);
3125 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3127 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3128 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3129 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3130 if (!item) qWarning() << "Item" << i << "not found";
3132 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3133 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3134 QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3137 // ensure items outside view become invisible
3138 gridview->setHeight(60);
3139 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3141 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3142 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3143 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3144 if (!item) qWarning() << "Item" << i << "not found";
3146 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3147 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3148 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3154 void tst_QQuickGridView::changeColumnCount()
3157 for (int i = 0; i < 40; i++)
3158 model.addItem("Item" + QString::number(i), "");
3160 QQuickView *canvas = createView();
3161 QQmlContext *ctxt = canvas->rootContext();
3162 ctxt->setContextProperty("testModel", &model);
3163 ctxt->setContextProperty("initialWidth", 100);
3164 ctxt->setContextProperty("initialHeight", 320);
3165 canvas->setSource(testFileUrl("resizeview.qml"));
3167 qApp->processEvents();
3169 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3170 QTRY_VERIFY(gridview != 0);
3171 QQuickItem *contentItem = gridview->contentItem();
3172 QTRY_VERIFY(contentItem != 0);
3173 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3175 // a single column of 6 items are visible
3176 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3177 QCOMPARE(itemCount, 6);
3178 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3179 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3180 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3181 QCOMPARE(item->x(), 0.0);
3182 QCOMPARE(item->y(), qreal(i*60));
3185 // now 6x3 grid is visible, plus 1 extra below for refill
3186 gridview->setWidth(240);
3187 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3188 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3189 QCOMPARE(itemCount, 6*3 + 1);
3190 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3191 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3192 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3193 QCOMPARE(item->x(), qreal((i%3)*80));
3194 QCOMPARE(item->y(), qreal((i/3)*60));
3197 // back to single column
3198 gridview->setWidth(100);
3199 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3200 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3201 QCOMPARE(itemCount, 6);
3202 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3203 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3204 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3205 QCOMPARE(item->x(), 0.0);
3206 QCOMPARE(item->y(), qreal(i*60));
3212 void tst_QQuickGridView::indexAt_itemAt_data()
3214 QTest::addColumn<qreal>("x");
3215 QTest::addColumn<qreal>("y");
3216 QTest::addColumn<int>("index");
3218 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3219 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3220 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3221 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3222 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3225 void tst_QQuickGridView::indexAt_itemAt()
3231 QQuickView *canvas = createView();
3234 model.addItem("Fred", "12345");
3235 model.addItem("John", "2345");
3236 model.addItem("Bob", "54321");
3237 model.addItem("Billy", "22345");
3238 model.addItem("Sam", "2945");
3239 model.addItem("Ben", "04321");
3240 model.addItem("Jim", "0780");
3242 QQmlContext *ctxt = canvas->rootContext();
3243 ctxt->setContextProperty("testModel", &model);
3244 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3245 ctxt->setContextProperty("testTopToBottom", QVariant(false));
3247 canvas->setSource(testFileUrl("gridview1.qml"));
3248 qApp->processEvents();
3250 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3251 QTRY_VERIFY(gridview != 0);
3253 QQuickItem *contentItem = gridview->contentItem();
3254 QTRY_VERIFY(contentItem != 0);
3256 QTRY_COMPARE(gridview->count(), model.count());
3258 QQuickItem *item = 0;
3260 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3263 QCOMPARE(gridview->indexAt(x, y), index);
3264 QVERIFY(gridview->itemAt(x, y) == item);
3269 void tst_QQuickGridView::onAdd()
3271 QFETCH(int, initialItemCount);
3272 QFETCH(int, itemsToAdd);
3274 const int delegateWidth = 50;
3275 const int delegateHeight = 100;
3277 QQuickView *canvas = createView();
3278 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3280 // these initial items should not trigger GridView.onAdd
3281 for (int i=0; i<initialItemCount; i++)
3282 model.addItem("dummy value", "dummy value");
3284 QQmlContext *ctxt = canvas->rootContext();
3285 ctxt->setContextProperty("testModel", &model);
3286 ctxt->setContextProperty("delegateWidth", delegateWidth);
3287 ctxt->setContextProperty("delegateHeight", delegateHeight);
3288 canvas->setSource(testFileUrl("attachedSignals.qml"));
3290 QObject *object = canvas->rootObject();
3291 object->setProperty("width", canvas->width());
3292 object->setProperty("height", canvas->height());
3293 qApp->processEvents();
3295 QList<QPair<QString, QString> > items;
3296 for (int i=0; i<itemsToAdd; i++)
3297 items << qMakePair(QString("value %1").arg(i), QString::number(i));
3298 model.addItems(items);
3300 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3301 qApp->processEvents();
3303 QVariantList result = object->property("addedDelegates").toList();
3304 QTRY_COMPARE(result.count(), items.count());
3305 for (int i=0; i<items.count(); i++)
3306 QCOMPARE(result[i].toString(), items[i].first);
3311 void tst_QQuickGridView::onAdd_data()
3313 QTest::addColumn<int>("initialItemCount");
3314 QTest::addColumn<int>("itemsToAdd");
3316 QTest::newRow("0, add 1") << 0 << 1;
3317 QTest::newRow("0, add 2") << 0 << 2;
3318 QTest::newRow("0, add 10") << 0 << 10;
3320 QTest::newRow("1, add 1") << 1 << 1;
3321 QTest::newRow("1, add 2") << 1 << 2;
3322 QTest::newRow("1, add 10") << 1 << 10;
3324 QTest::newRow("5, add 1") << 5 << 1;
3325 QTest::newRow("5, add 2") << 5 << 2;
3326 QTest::newRow("5, add 10") << 5 << 10;
3329 void tst_QQuickGridView::onRemove()
3331 QFETCH(int, initialItemCount);
3332 QFETCH(int, indexToRemove);
3333 QFETCH(int, removeCount);
3335 const int delegateWidth = 50;
3336 const int delegateHeight = 100;
3338 for (int i=0; i<initialItemCount; i++)
3339 model.addItem(QString("value %1").arg(i), "dummy value");
3341 QQuickView *canvas = createView();
3342 QQmlContext *ctxt = canvas->rootContext();
3343 ctxt->setContextProperty("testModel", &model);
3344 ctxt->setContextProperty("delegateWidth", delegateWidth);
3345 ctxt->setContextProperty("delegateHeight", delegateHeight);
3346 canvas->setSource(testFileUrl("attachedSignals.qml"));
3347 QObject *object = canvas->rootObject();
3349 model.removeItems(indexToRemove, removeCount);
3350 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3351 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
3356 void tst_QQuickGridView::onRemove_data()
3358 QTest::addColumn<int>("initialItemCount");
3359 QTest::addColumn<int>("indexToRemove");
3360 QTest::addColumn<int>("removeCount");
3362 QTest::newRow("remove first") << 1 << 0 << 1;
3363 QTest::newRow("two items, remove first") << 2 << 0 << 1;
3364 QTest::newRow("two items, remove last") << 2 << 1 << 1;
3365 QTest::newRow("two items, remove all") << 2 << 0 << 2;
3367 QTest::newRow("four items, remove first") << 4 << 0 << 1;
3368 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
3369 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
3370 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
3371 QTest::newRow("four items, remove last") << 4 << 3 << 1;
3372 QTest::newRow("four items, remove all") << 4 << 0 << 4;
3374 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
3375 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
3376 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
3379 void tst_QQuickGridView::columnCount()
3382 canvas.setSource(testFileUrl("gridview4.qml"));
3384 canvas.requestActivateWindow();
3385 QTest::qWaitForWindowShown(&canvas);
3387 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
3389 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
3390 QCOMPARE(view->cellHeight(), qreal(100));
3392 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
3393 QCOMPARE(items.size(), 18);
3394 QCOMPARE(items.at(8)->y(), qreal(0));
3395 QCOMPARE(items.at(9)->y(), qreal(100));
3398 void tst_QQuickGridView::margins()
3401 QQuickView *canvas = createView();
3404 for (int i = 0; i < 40; i++)
3405 model.addItem("Item" + QString::number(i), "");
3407 QQmlContext *ctxt = canvas->rootContext();
3408 ctxt->setContextProperty("testModel", &model);
3409 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3411 canvas->setSource(testFileUrl("margins.qml"));
3413 qApp->processEvents();
3415 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3416 QTRY_VERIFY(gridview != 0);
3417 QQuickItem *contentItem = gridview->contentItem();
3418 QTRY_VERIFY(contentItem != 0);
3419 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3421 QCOMPARE(gridview->contentX(), -30.);
3422 QCOMPARE(gridview->xOrigin(), 0.);
3425 gridview->positionViewAtEnd();
3426 qreal pos = gridview->contentX();
3427 gridview->setContentX(pos + 80);
3428 gridview->returnToBounds();
3429 QTRY_COMPARE(gridview->contentX(), pos + 50);
3431 // remove item before visible and check that left margin is maintained
3432 // and xOrigin is updated
3433 gridview->setContentX(200);
3434 model.removeItems(0, 4);
3436 gridview->setContentX(-50);
3437 gridview->returnToBounds();
3438 QCOMPARE(gridview->xOrigin(), 100.);
3439 QTRY_COMPARE(gridview->contentX(), 70.);
3441 // reduce left margin
3442 gridview->setLeftMargin(20);
3443 QCOMPARE(gridview->xOrigin(), 100.);
3444 QTRY_COMPARE(gridview->contentX(), 80.);
3447 gridview->positionViewAtEnd();
3448 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3449 pos = gridview->contentX();
3450 gridview->setContentX(pos + 80);
3451 gridview->returnToBounds();
3452 QTRY_COMPARE(gridview->contentX(), pos + 50);
3454 // reduce right margin
3455 pos = gridview->contentX();
3456 gridview->setRightMargin(40);
3457 QCOMPARE(gridview->xOrigin(), 0.);
3458 QTRY_COMPARE(gridview->contentX(), pos-10);
3464 QQuickView *canvas = createView();
3468 for (int i = 0; i < 40; i++)
3469 model.addItem("Item" + QString::number(i), "");
3471 QQmlContext *ctxt = canvas->rootContext();
3472 ctxt->setContextProperty("testModel", &model);
3473 ctxt->setContextProperty("testRightToLeft", QVariant(true));
3475 canvas->setSource(testFileUrl("margins.qml"));
3476 qApp->processEvents();
3478 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3479 QTRY_VERIFY(gridview != 0);
3481 QQuickItem *contentItem = gridview->contentItem();
3482 QTRY_VERIFY(contentItem != 0);
3484 QCOMPARE(gridview->contentX(), -240+30.);
3485 QCOMPARE(gridview->xOrigin(), 0.);
3488 gridview->positionViewAtEnd();
3489 qreal pos = gridview->contentX();
3490 gridview->setContentX(pos - 80);
3491 gridview->returnToBounds();
3492 QTRY_COMPARE(gridview->contentX(), pos - 50);
3494 // remove item before visible and check that left margin is maintained
3495 // and xOrigin is updated
3496 gridview->setContentX(-400);
3497 model.removeItems(0, 4);
3499 gridview->setContentX(-240+50);
3500 gridview->returnToBounds();
3501 QCOMPARE(gridview->xOrigin(), -100.);
3502 QTRY_COMPARE(gridview->contentX(), -240-70.);
3504 // reduce left margin (i.e. right side due to RTL)
3505 pos = gridview->contentX();
3506 gridview->setLeftMargin(20);
3507 QCOMPARE(gridview->xOrigin(), -100.);
3508 QTRY_COMPARE(gridview->contentX(), -240-80.);
3511 gridview->positionViewAtEnd();
3512 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3513 pos = gridview->contentX();
3514 gridview->setContentX(pos - 80);
3515 gridview->returnToBounds();
3516 QTRY_COMPARE(gridview->contentX(), pos - 50);
3518 // reduce right margin (i.e. left side due to RTL)
3519 pos = gridview->contentX();
3520 gridview->setRightMargin(40);
3521 QCOMPARE(gridview->xOrigin(), 0.);
3522 QTRY_COMPARE(gridview->contentX(), pos+10);
3528 void tst_QQuickGridView::creationContext()
3531 canvas.setGeometry(0,0,240,320);
3532 canvas.setSource(testFileUrl("creationContext.qml"));
3533 qApp->processEvents();
3535 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
3537 QVERIFY(rootItem->property("count").toInt() > 0);
3540 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
3541 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3542 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
3543 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3544 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
3545 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3548 void tst_QQuickGridView::snapToRow_data()
3550 QTest::addColumn<QQuickGridView::Flow>("flow");
3551 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3552 QTest::addColumn<int>("highlightRangeMode");
3553 QTest::addColumn<QPoint>("flickStart");
3554 QTest::addColumn<QPoint>("flickEnd");
3555 QTest::addColumn<qreal>("snapAlignment");
3556 QTest::addColumn<qreal>("endExtent");
3557 QTest::addColumn<qreal>("startExtent");
3559 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3560 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0;
3562 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3563 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0;
3565 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3566 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 << -240.0;
3568 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3569 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0;
3571 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3572 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0;
3574 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3575 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 - 140.0 << -220.0;
3578 void tst_QQuickGridView::snapToRow()
3580 QFETCH(QQuickGridView::Flow, flow);
3581 QFETCH(Qt::LayoutDirection, layoutDirection);
3582 QFETCH(int, highlightRangeMode);
3583 QFETCH(QPoint, flickStart);
3584 QFETCH(QPoint, flickEnd);
3585 QFETCH(qreal, snapAlignment);
3586 QFETCH(qreal, endExtent);
3587 QFETCH(qreal, startExtent);
3589 QQuickView *canvas = createView();
3591 canvas->setSource(testFileUrl("snapToRow.qml"));
3593 qApp->processEvents();
3595 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3596 QTRY_VERIFY(gridview != 0);
3598 gridview->setFlow(flow);
3599 gridview->setLayoutDirection(layoutDirection);
3600 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3601 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3603 QQuickItem *contentItem = gridview->contentItem();
3604 QTRY_VERIFY(contentItem != 0);
3606 // confirm that a flick hits an item boundary
3607 flick(canvas, flickStart, flickEnd, 180);
3608 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3609 if (flow == QQuickGridView::LeftToRight)
3610 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
3612 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
3616 flick(canvas, flickStart, flickEnd, 180);
3617 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3618 } while (flow == QQuickGridView::LeftToRight
3619 ? !gridview->isAtYEnd()
3620 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3622 if (flow == QQuickGridView::LeftToRight)
3623 QCOMPARE(gridview->contentY(), endExtent);
3625 QCOMPARE(gridview->contentX(), endExtent);
3629 flick(canvas, flickEnd, flickStart, 180);
3630 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3631 } while (flow == QQuickGridView::LeftToRight
3632 ? !gridview->isAtYBeginning()
3633 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3635 if (flow == QQuickGridView::LeftToRight)
3636 QCOMPARE(gridview->contentY(), startExtent);
3638 QCOMPARE(gridview->contentX(), startExtent);
3643 void tst_QQuickGridView::snapOneRow_data()
3645 QTest::addColumn<QQuickGridView::Flow>("flow");
3646 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3647 QTest::addColumn<int>("highlightRangeMode");
3648 QTest::addColumn<QPoint>("flickStart");
3649 QTest::addColumn<QPoint>("flickEnd");
3650 QTest::addColumn<qreal>("snapAlignment");
3651 QTest::addColumn<qreal>("endExtent");
3652 QTest::addColumn<qreal>("startExtent");
3654 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3655 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 360.0 << 0.0;
3657 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3658 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 360.0 << 0.0;
3660 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3661 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -360.0 - 240.0 << -240.0;
3663 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3664 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 460.0 << -20.0;
3666 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3667 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 460.0 << -20.0;
3669 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3670 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -360.0 - 240.0 - 100.0 << -220.0;
3673 void tst_QQuickGridView::snapOneRow()
3675 QFETCH(QQuickGridView::Flow, flow);
3676 QFETCH(Qt::LayoutDirection, layoutDirection);
3677 QFETCH(int, highlightRangeMode);
3678 QFETCH(QPoint, flickStart);
3679 QFETCH(QPoint, flickEnd);
3680 QFETCH(qreal, snapAlignment);
3681 QFETCH(qreal, endExtent);
3682 QFETCH(qreal, startExtent);
3684 QQuickView *canvas = createView();
3686 canvas->setSource(testFileUrl("snapOneRow.qml"));
3688 qApp->processEvents();
3690 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3691 QTRY_VERIFY(gridview != 0);
3693 gridview->setFlow(flow);
3694 gridview->setLayoutDirection(layoutDirection);
3695 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3696 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3698 QQuickItem *contentItem = gridview->contentItem();
3699 QTRY_VERIFY(contentItem != 0);
3701 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
3703 // confirm that a flick hits next row boundary
3704 flick(canvas, flickStart, flickEnd, 180);
3705 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3706 if (flow == QQuickGridView::LeftToRight)
3707 QCOMPARE(gridview->contentY(), snapAlignment);
3709 QCOMPARE(gridview->contentX(), snapAlignment);
3711 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3712 QCOMPARE(gridview->currentIndex(), 2);
3713 QCOMPARE(currentIndexSpy.count(), 1);
3718 flick(canvas, flickStart, flickEnd, 180);
3719 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3720 } while (flow == QQuickGridView::LeftToRight
3721 ? !gridview->isAtYEnd()
3722 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3724 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3725 QCOMPARE(gridview->currentIndex(), 8);
3726 QCOMPARE(currentIndexSpy.count(), 4);
3729 if (flow == QQuickGridView::LeftToRight)
3730 QCOMPARE(gridview->contentY(), endExtent);
3732 QCOMPARE(gridview->contentX(), endExtent);
3736 flick(canvas, flickEnd, flickStart, 180);
3737 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3738 } while (flow == QQuickGridView::LeftToRight
3739 ? !gridview->isAtYBeginning()
3740 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3742 if (flow == QQuickGridView::LeftToRight)
3743 QCOMPARE(gridview->contentY(), startExtent);
3745 QCOMPARE(gridview->contentX(), startExtent);
3747 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3748 QCOMPARE(gridview->currentIndex(), 0);
3749 QCOMPARE(currentIndexSpy.count(), 8);
3756 void tst_QQuickGridView::unaligned()
3758 QQuickView *canvas = createView();
3762 for (int i = 0; i < 10; i++)
3763 model.addItem("Item" + QString::number(i), "");
3765 QQmlContext *ctxt = canvas->rootContext();
3766 ctxt->setContextProperty("testModel", &model);
3768 canvas->setSource(testFileUrl("unaligned.qml"));
3769 qApp->processEvents();
3771 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3772 QVERIFY(gridview != 0);
3774 QQuickItem *contentItem = gridview->contentItem();
3775 QVERIFY(contentItem != 0);
3777 for (int i = 0; i < 10; ++i) {
3778 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3779 if (!item) qWarning() << "Item" << i << "not found";
3781 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3782 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3786 for (int i = 10; i < 18; ++i) {
3787 model.addItem("Item" + QString::number(i), "");
3788 QQuickItem *item = 0;
3789 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3790 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3791 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3795 for (int i = 0; i < 10; ++i) {
3796 model.insertItem(i, "Item" + QString::number(i), "");
3797 QQuickItem *item = 0;
3798 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3799 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3800 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3804 model.removeItems(7, 10);
3805 QTRY_COMPARE(model.count(), gridview->count());
3806 for (int i = 0; i < 18; ++i) {
3807 QQuickItem *item = 0;
3808 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3809 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
3810 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
3816 void tst_QQuickGridView::populateTransitions()
3818 QFETCH(bool, staticallyPopulate);
3819 QFETCH(bool, dynamicallyPopulate);
3820 QFETCH(bool, usePopulateTransition);
3822 QPointF transitionFrom(-50, -50);
3823 QPointF transitionVia(100, 100);
3824 QaimModel model_transitionFrom;
3825 QaimModel model_transitionVia;
3828 if (staticallyPopulate) {
3829 for (int i = 0; i < 30; i++)
3830 model.addItem("item" + QString::number(i), "");
3833 QQuickView *canvas = createView();
3834 canvas->rootContext()->setContextProperty("testModel", &model);
3835 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
3836 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
3837 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
3838 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
3839 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
3840 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
3841 canvas->setSource(testFileUrl("populateTransitions.qml"));
3844 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3846 QQuickItem *contentItem = gridview->contentItem();
3847 QVERIFY(contentItem);
3849 if (staticallyPopulate || dynamicallyPopulate) {
3850 // check the populate transition is run
3851 if (usePopulateTransition) {
3852 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 19);
3854 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3855 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
3857 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3859 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3862 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3863 if (usePopulateTransition)
3864 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3865 for (int i=0; i < model.count() && i < itemCount; ++i) {
3866 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3867 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3868 QCOMPARE(item->x(), (i%3)*80.0);
3869 QCOMPARE(item->y(), (i/3)*60.0);
3870 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3872 QTRY_COMPARE(name->text(), model.name(i));
3875 // add an item and check this is done with add transition, not populate
3876 model.insertItem(0, "another item", "");
3877 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
3878 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(),
3879 (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 19 : 0);
3882 canvas->rootContext()->setContextProperty("testModel", QVariant());
3883 QTRY_COMPARE(gridview->count(), 0);
3884 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
3885 gridview->setProperty("countPopulateTransitions", 0);
3886 gridview->setProperty("countAddTransitions", 0);
3888 // set to a valid model and check populate transition is run a second time
3890 for (int i = 0; i < 30; i++)
3891 model.addItem("item" + QString::number(i), "");
3892 canvas->rootContext()->setContextProperty("testModel", &model);
3893 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3895 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
3896 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3898 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3899 if (usePopulateTransition)
3900 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3901 for (int i=0; i < model.count() && i < itemCount; ++i) {
3902 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3903 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3904 QCOMPARE(item->x(), (i%3)*80.0);
3905 QCOMPARE(item->y(), (i/3)*60.0);
3906 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3908 QTRY_COMPARE(name->text(), model.name(i));
3911 // reset model and check populate transition is run again
3912 gridview->setProperty("countPopulateTransitions", 0);
3913 gridview->setProperty("countAddTransitions", 0);
3915 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
3916 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3918 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3919 if (usePopulateTransition)
3920 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3921 for (int i=0; i < model.count() && i < itemCount; ++i) {
3922 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3923 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3924 QCOMPARE(item->x(), (i%3)*80.0);
3925 QCOMPARE(item->y(), (i/3)*60.0);
3926 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3928 QTRY_COMPARE(name->text(), model.name(i));
3934 void tst_QQuickGridView::populateTransitions_data()
3936 QTest::addColumn<bool>("staticallyPopulate");
3937 QTest::addColumn<bool>("dynamicallyPopulate");
3938 QTest::addColumn<bool>("usePopulateTransition");
3940 QTest::newRow("static") << true << false << true;
3941 QTest::newRow("static, no populate") << true << false << false;
3943 QTest::newRow("dynamic") << false << true << true;
3944 QTest::newRow("dynamic, no populate") << false << true << false;
3946 QTest::newRow("empty to start with") << false << false << true;
3947 QTest::newRow("empty to start with, no populate") << false << false << false;
3950 void tst_QQuickGridView::addTransitions()
3952 QFETCH(int, initialItemCount);
3953 QFETCH(bool, shouldAnimateTargets);
3954 QFETCH(qreal, contentY);
3955 QFETCH(int, insertionIndex);
3956 QFETCH(int, insertionCount);
3957 QFETCH(ListRange, expectedDisplacedIndexes);
3959 // added items should start here
3960 QPointF targetItems_transitionFrom(-50, -50);
3962 // displaced items should pass through this point
3963 QPointF displacedItems_transitionVia(100, 100);
3966 for (int i = 0; i < initialItemCount; i++)
3967 model.addItem("Original item" + QString::number(i), "");
3968 QaimModel model_targetItems_transitionFrom;
3969 QaimModel model_displacedItems_transitionVia;
3971 QQuickView *canvas = createView();
3972 QQmlContext *ctxt = canvas->rootContext();
3973 ctxt->setContextProperty("testModel", &model);
3974 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
3975 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
3976 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
3977 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
3978 canvas->setSource(testFileUrl("addTransitions.qml"));
3981 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3982 QTRY_VERIFY(gridview != 0);
3983 QQuickItem *contentItem = gridview->contentItem();
3984 QVERIFY(contentItem != 0);
3985 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3987 if (contentY != 0) {
3988 gridview->setContentY(contentY);
3989 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3992 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
3994 // only target items that will become visible should be animated
3995 QList<QPair<QString, QString> > newData;
3996 QList<QPair<QString, QString> > expectedTargetData;
3997 QList<int> targetIndexes;
3998 if (shouldAnimateTargets) {
3999 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4000 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4002 // last visible item is the first item of the row beneath the view
4003 if (i >= (contentY / 60)*3 && i < qCeil((contentY + gridview->height()) / 60.0)*3) {
4004 expectedTargetData << newData.last();
4008 QVERIFY(expectedTargetData.count() > 0);
4012 if (!newData.isEmpty()) {
4013 model.insertItems(insertionIndex, newData);
4014 QTRY_COMPARE(model.count(), gridview->count());
4017 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4019 if (shouldAnimateTargets) {
4020 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4021 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4022 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4024 // check the target and displaced items were animated
4025 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4026 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4028 // check attached properties
4029 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4030 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4031 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4032 if (expectedDisplacedIndexes.isValid()) {
4033 // adjust expectedDisplacedIndexes to their final values after the move
4034 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4035 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4036 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4037 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4040 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4041 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4044 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4045 int firstVisibleIndex = -1;
4046 for (int i=0; i<items.count(); i++) {
4047 if (items[i]->y() >= contentY) {
4048 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4049 firstVisibleIndex = e.evaluate().toInt();
4053 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4055 // verify all items moved to the correct final positions
4056 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4057 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4058 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4059 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4060 QCOMPARE(item->x(), (i%3)*80.0);
4061 QCOMPARE(item->y(), (i/3)*60.0);
4062 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4064 QCOMPARE(name->text(), model.name(i));
4070 void tst_QQuickGridView::addTransitions_data()
4072 QTest::addColumn<int>("initialItemCount");
4073 QTest::addColumn<qreal>("contentY");
4074 QTest::addColumn<bool>("shouldAnimateTargets");
4075 QTest::addColumn<int>("insertionIndex");
4076 QTest::addColumn<int>("insertionCount");
4077 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4079 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4080 QTest::newRow("insert 1, just before start")
4081 << 30 << 20.0 << false
4082 << 0 << 1 << ListRange();
4083 QTest::newRow("insert 1, way before start")
4084 << 30 << 20.0 << false
4085 << 0 << 1 << ListRange();
4086 QTest::newRow("insert multiple, just before start")
4087 << 30 << 100.0 << false
4088 << 0 << 3 << ListRange();
4089 QTest::newRow("insert multiple (< 1 row), just before start")
4090 << 30 << 100.0 << false
4091 << 0 << 2 << ListRange();
4092 QTest::newRow("insert multiple, way before start")
4093 << 30 << 100.0 << false
4094 << 0 << 3 << ListRange();
4096 QTest::newRow("insert 1 at start")
4097 << 30 << 0.0 << true
4098 << 0 << 1 << ListRange(0, 17);
4099 QTest::newRow("insert multiple at start")
4100 << 30 << 0.0 << true
4101 << 0 << 3 << ListRange(0, 17);
4102 QTest::newRow("insert multiple (> 1 row) at start")
4103 << 30 << 0.0 << true
4104 << 0 << 5 << ListRange(0, 17);
4105 QTest::newRow("insert 1 at start, content y not 0")
4106 << 30 << 60.0 << true // first visible is index 3
4107 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4108 QTest::newRow("insert multiple at start, content y not 0")
4109 << 30 << 60.0 << true // first visible is index 3
4110 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4111 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4112 << 30 << 60.0 << true // first visible is index 3
4113 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4115 QTest::newRow("insert 1 at start, to empty grid")
4117 << 0 << 1 << ListRange();
4118 QTest::newRow("insert multiple at start, to empty grid")
4120 << 0 << 3 << ListRange();
4122 QTest::newRow("insert 1 at middle")
4123 << 30 << 0.0 << true
4124 << 7 << 1 << ListRange(7, 17);
4125 QTest::newRow("insert multiple at middle")
4126 << 30 << 0.0 << true
4127 << 7 << 3 << ListRange(7, 17);
4128 QTest::newRow("insert multiple (> 1 row) at middle")
4129 << 30 << 0.0 << true
4130 << 7 << 5 << ListRange(7, 17);
4132 QTest::newRow("insert 1 at bottom")
4133 << 30 << 0.0 << true
4134 << 17 << 1 << ListRange(17, 17);
4135 QTest::newRow("insert multiple at bottom")
4136 << 30 << 0.0 << true
4137 << 17 << 3 << ListRange(17, 17);
4138 QTest::newRow("insert 1 at bottom, content y not 0")
4139 << 30 << 20.0 * 3 << true
4140 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4141 QTest::newRow("insert multiple at bottom, content y not 0")
4142 << 30 << 20.0 * 3 << true
4143 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4146 // items added after the last visible will not be animated in, since they
4147 // do not appear in the final view
4148 QTest::newRow("insert 1 after end")
4149 << 30 << 0.0 << false
4150 << 18 << 1 << ListRange();
4151 QTest::newRow("insert multiple after end")
4152 << 30 << 0.0 << false
4153 << 18 << 3 << ListRange();
4156 void tst_QQuickGridView::moveTransitions()
4158 QFETCH(int, initialItemCount);
4159 QFETCH(qreal, contentY);
4160 QFETCH(qreal, itemsOffsetAfterMove);
4161 QFETCH(int, moveFrom);
4162 QFETCH(int, moveTo);
4163 QFETCH(int, moveCount);
4164 QFETCH(ListRange, expectedDisplacedIndexes);
4166 // target and displaced items should pass through these points
4167 QPointF targetItems_transitionVia(-50, 50);
4168 QPointF displacedItems_transitionVia(100, 100);
4171 for (int i = 0; i < initialItemCount; i++)
4172 model.addItem("Original item" + QString::number(i), "");
4173 QaimModel model_targetItems_transitionVia;
4174 QaimModel model_displacedItems_transitionVia;
4176 QQuickView *canvas = createView();
4177 QQmlContext *ctxt = canvas->rootContext();
4178 ctxt->setContextProperty("testModel", &model);
4179 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4180 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4181 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4182 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4183 canvas->setSource(testFileUrl("moveTransitions.qml"));
4186 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4187 QTRY_VERIFY(gridview != 0);
4188 QQuickItem *contentItem = gridview->contentItem();
4189 QVERIFY(contentItem != 0);
4192 if (contentY != 0) {
4193 gridview->setContentY(contentY);
4194 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4197 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4199 // Items moving to *or* from visible positions should be animated.
4200 // Otherwise, they should not be animated.
4201 QList<QPair<QString, QString> > expectedTargetData;
4202 QList<int> targetIndexes;
4203 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4204 int toIndex = moveTo + (i - moveFrom);
4205 int firstVisibleIndex = (contentY / 60) * 3;
4206 int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1;
4207 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4208 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4209 expectedTargetData << qMakePair(model.name(i), model.number(i));
4213 // ViewTransition.index provides the indices that items are moving to, not from
4214 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4217 model.moveItems(moveFrom, moveTo, moveCount);
4219 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4220 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4221 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4223 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4225 // check the target and displaced items were animated
4226 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4227 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4229 // check attached properties
4230 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4231 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4232 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4233 if (expectedDisplacedIndexes.isValid()) {
4234 // adjust expectedDisplacedIndexes to their final values after the move
4235 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4236 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4237 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4238 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4241 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4242 int firstVisibleIndex = -1;
4243 for (int i=0; i<items.count(); i++) {
4244 if (items[i]->y() >= contentY) {
4245 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4246 firstVisibleIndex = e.evaluate().toInt();
4250 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4252 // verify all items moved to the correct final positions
4253 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4254 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4255 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4256 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4257 QCOMPARE(item->x(), (i%3)*80.0);
4258 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
4259 name = findItem<QQuickText>(contentItem, "textName", i);
4261 QTRY_COMPARE(name->text(), model.name(i));
4267 void tst_QQuickGridView::moveTransitions_data()
4269 QTest::addColumn<int>("initialItemCount");
4270 QTest::addColumn<qreal>("contentY");
4271 QTest::addColumn<qreal>("itemsOffsetAfterMove");
4272 QTest::addColumn<int>("moveFrom");
4273 QTest::addColumn<int>("moveTo");
4274 QTest::addColumn<int>("moveCount");
4275 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4277 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 120.0 << 0.0
4278 << 1 << 10 << 1 << ListRange(6, 10);
4279 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 120.0 << 0.0
4280 << 0 << 10 << 1 << ListRange(6, 10);
4281 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 120.0 << 60.0
4282 << 1 << 10 << 3 << ListRange(13, 23);
4283 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 120.0 << 60.0
4284 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4285 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 120.0 << 120.0
4286 << 0 << 10 << 6 << ListRange(16, 23);
4288 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
4289 << 1 << 10 << 1 << ListRange(2, 10);
4290 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
4291 << 0 << 10 << 1 << ListRange(1, 10);
4292 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 120.0 << 0.0
4293 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
4294 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
4295 << 10 << 17 << 1 << ListRange(11, 17);
4296 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
4297 << 0 << 17 << 1 << ListRange(1, 17);
4299 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
4300 << 1 << 10 << 3 << ListRange(4, 12);
4301 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
4302 << 0 << 10 << 3 << ListRange(3, 12);
4303 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 60.0 << 0.0
4304 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
4305 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
4306 << 5 << 15 << 3 << ListRange(8, 17);
4307 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
4308 << 0 << 15 << 3 << ListRange(3, 17);
4310 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
4311 << 10 << 1 << 1 << ListRange(1, 9);
4312 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
4313 << 10 << 0 << 1 << ListRange(0, 9);
4314 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 120.0 << 0.0
4315 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
4316 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 80.0 << 0.0
4317 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
4318 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
4319 << 17 << 10 << 1 << ListRange(10, 16);
4320 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
4321 << 17 << 0 << 1 << ListRange(0, 16);
4323 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
4324 << 10 << 1 << 3 << ListRange(1, 9);
4325 QTest::newRow("move within view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0
4326 << 10 << 1 << 5 << ListRange(1, 9);
4327 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
4328 << 10 << 0 << 3 << ListRange(0, 9);
4329 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 60.0 << 0.0
4330 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
4331 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border") << 30 << 80.0 << 0.0
4332 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
4333 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
4334 << 15 << 5 << 3 << ListRange(5, 14);
4335 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
4336 << 15 << 0 << 3 << ListRange(0, 14);
4338 QTest::newRow("move from below view, move 1 up") << 30 << 0.0 << 0.0
4339 << 20 << 5 << 1 << ListRange(5, 17);
4340 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
4341 << 20 << 0 << 1 << ListRange(0, 17);
4342 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 60.0 << 0.0
4343 << 25 << 3 << 1 << ListRange(0+3, 17+3);
4344 QTest::newRow("move from below view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0
4345 << 20 << 5 << 5 << ListRange(5, 17);
4346 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
4347 << 20 << 0 << 3 << ListRange(0, 17);
4348 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 60.0 << 0.0
4349 << 25 << 3 << 3 << ListRange(0+3, 17+3);
4351 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
4352 << 20 << 17 << 1 << ListRange(17, 17);
4353 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0
4354 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
4355 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
4356 << 20 << 17 << 3 << ListRange(17, 17);
4357 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0
4358 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
4361 void tst_QQuickGridView::removeTransitions()
4363 QFETCH(int, initialItemCount);
4364 QFETCH(bool, shouldAnimateTargets);
4365 QFETCH(qreal, contentY);
4366 QFETCH(int, removalIndex);
4367 QFETCH(int, removalCount);
4368 QFETCH(ListRange, expectedDisplacedIndexes);
4370 // added items should end here
4371 QPointF targetItems_transitionTo(-50, -50);
4373 // displaced items should pass through this points
4374 QPointF displacedItems_transitionVia(100, 100);
4377 for (int i = 0; i < initialItemCount; i++)
4378 model.addItem("Original item" + QString::number(i), "");
4379 QaimModel model_targetItems_transitionTo;
4380 QaimModel model_displacedItems_transitionVia;
4382 QQuickView *canvas = createView();
4383 QQmlContext *ctxt = canvas->rootContext();
4384 ctxt->setContextProperty("testModel", &model);
4385 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
4386 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4387 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
4388 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4389 canvas->setSource(testFileUrl("removeTransitions.qml"));
4392 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4393 QTRY_VERIFY(gridview != 0);
4394 QQuickItem *contentItem = gridview->contentItem();
4395 QVERIFY(contentItem != 0);
4396 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4398 if (contentY != 0) {
4399 gridview->setContentY(contentY);
4400 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4403 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4405 // only target items that are visible should be animated
4406 QList<QPair<QString, QString> > expectedTargetData;
4407 QList<int> targetIndexes;
4408 if (shouldAnimateTargets) {
4409 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
4410 int firstVisibleIndex = (contentY / 60.0)*3;
4411 int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1;
4412 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
4413 expectedTargetData << qMakePair(model.name(i), model.number(i));
4417 QVERIFY(expectedTargetData.count() > 0);
4420 // calculate targetItems and expectedTargets before model changes
4421 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4422 QVariantMap expectedTargets;
4423 for (int i=0; i<targetIndexes.count(); i++)
4424 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
4427 model.removeItems(removalIndex, removalCount);
4428 QTRY_COMPARE(model.count(), gridview->count());
4430 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
4431 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4432 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4433 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4435 // check the target and displaced items were animated
4436 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
4437 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4439 // check attached properties
4440 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
4441 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4442 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4443 if (expectedDisplacedIndexes.isValid()) {
4444 // adjust expectedDisplacedIndexes to their final values after the move
4445 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
4446 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4447 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4448 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4451 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
4452 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4455 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4456 int itemCount = items.count();
4457 int firstVisibleIndex = -1;
4458 for (int i=0; i<items.count(); i++) {
4459 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4460 int index = e.evaluate().toInt();
4461 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
4462 firstVisibleIndex = index;
4464 itemCount--; // exclude deleted items
4466 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4468 // verify all items moved to the correct final positions
4469 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4470 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4471 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4472 QCOMPARE(item->x(), (i%3)*80.0);
4473 QCOMPARE(item->y(), contentY + ((i-firstVisibleIndex)/3) * 60.0);
4474 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4476 QTRY_COMPARE(name->text(), model.name(i));
4482 void tst_QQuickGridView::removeTransitions_data()
4484 QTest::addColumn<int>("initialItemCount");
4485 QTest::addColumn<qreal>("contentY");
4486 QTest::addColumn<bool>("shouldAnimateTargets");
4487 QTest::addColumn<int>("removalIndex");
4488 QTest::addColumn<int>("removalCount");
4489 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4491 // All items that are visible following the remove operation should be animated.
4492 // Remove targets that are outside of the view should not be animated.
4494 // For a GridView, removing any number of items other than a full row before the start
4495 // should displace all items in the view
4496 QTest::newRow("remove 1 before start")
4497 << 30 << 120.0 << false
4498 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
4499 QTest::newRow("remove 1 row, before start")
4500 << 30 << 120.0 << false
4501 << 3 << 3 << ListRange();
4502 QTest::newRow("remove between 1-2 rows, before start")
4503 << 30 << 120.0 << false
4504 << 0 << 5 << ListRange(6, 25);
4505 QTest::newRow("remove 2 rows, before start")
4506 << 30 << 120.0 << false
4507 << 0 << 6 << ListRange();
4508 QTest::newRow("remove mix of before and after start")
4509 << 30 << 60.0 << true
4510 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
4513 QTest::newRow("remove 1 from start")
4514 << 30 << 0.0 << true
4515 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
4516 QTest::newRow("remove multiple from start")
4517 << 30 << 0.0 << true
4518 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
4519 QTest::newRow("remove 1 from start, content y not 0")
4520 << 30 << 60.0 << true
4521 << 3 << 1 << ListRange(1 + 3, 18 + 3);
4522 QTest::newRow("remove multiple from start, content y not 0")
4523 << 30 << 60.0 << true
4524 << 3 << 3 << ListRange(3 + 3, 20 + 3);
4527 QTest::newRow("remove 1 from middle")
4528 << 30 << 0.0 << true
4529 << 5 << 1 << ListRange(6, 18);
4530 QTest::newRow("remove multiple from middle")
4531 << 30 << 0.0 << true
4532 << 5 << 3 << ListRange(8, 20);
4535 QTest::newRow("remove 1 from bottom")
4536 << 30 << 0.0 << true
4537 << 17 << 1 << ListRange(18, 18);
4538 QTest::newRow("remove multiple (1 row) from bottom")
4539 << 30 << 0.0 << true
4540 << 15 << 3 << ListRange(18, 20);
4541 QTest::newRow("remove multiple (> 1 row) from bottom")
4542 << 30 << 0.0 << true
4543 << 15 << 5 << ListRange(20, 22);
4544 QTest::newRow("remove 1 from bottom, content y not 0")
4545 << 30 << 60.0 << true
4546 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
4547 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
4548 << 30 << 60.0 << true
4549 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
4552 QTest::newRow("remove 1 after end")
4553 << 30 << 0.0 << false
4554 << 18 << 1 << ListRange();
4555 QTest::newRow("remove multiple after end")
4556 << 30 << 0.0 << false
4557 << 18 << 3 << ListRange();
4560 void tst_QQuickGridView::displacedTransitions()
4562 QFETCH(bool, useDisplaced);
4563 QFETCH(bool, displacedEnabled);
4564 QFETCH(bool, useAddDisplaced);
4565 QFETCH(bool, addDisplacedEnabled);
4566 QFETCH(bool, useMoveDisplaced);
4567 QFETCH(bool, moveDisplacedEnabled);
4568 QFETCH(bool, useRemoveDisplaced);
4569 QFETCH(bool, removeDisplacedEnabled);
4570 QFETCH(ListChange, change);
4571 QFETCH(ListRange, expectedDisplacedIndexes);
4574 for (int i = 0; i < 30; i++)
4575 model.addItem("Original item" + QString::number(i), "");
4576 QaimModel model_displaced_transitionVia;
4577 QaimModel model_addDisplaced_transitionVia;
4578 QaimModel model_moveDisplaced_transitionVia;
4579 QaimModel model_removeDisplaced_transitionVia;
4581 QPointF displaced_transitionVia(-50, -100);
4582 QPointF addDisplaced_transitionVia(-150, 100);
4583 QPointF moveDisplaced_transitionVia(50, -100);
4584 QPointF removeDisplaced_transitionVia(150, 100);
4586 QQuickView *canvas = createView();
4587 QQmlContext *ctxt = canvas->rootContext();
4588 ctxt->setContextProperty("testModel", &model);
4589 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
4590 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
4591 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
4592 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
4593 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
4594 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
4595 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
4596 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
4597 ctxt->setContextProperty("useDisplaced", useDisplaced);
4598 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
4599 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
4600 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
4601 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
4602 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
4603 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
4604 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
4605 canvas->setSource(testFileUrl("displacedTransitions.qml"));
4607 qApp->processEvents();
4609 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4610 QTRY_VERIFY(gridview != 0);
4611 QQuickItem *contentItem = gridview->contentItem();
4612 QVERIFY(contentItem != 0);
4613 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4615 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4616 gridview->setProperty("displaceTransitionsDone", false);
4618 switch (change.type) {
4619 case ListChange::Inserted:
4621 QList<QPair<QString, QString> > targetItemData;
4622 for (int i=change.index; i<change.index + change.count; ++i)
4623 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
4624 model.insertItems(change.index, targetItemData);
4625 QTRY_COMPARE(model.count(), gridview->count());
4628 case ListChange::Removed:
4629 model.removeItems(change.index, change.count);
4630 QTRY_COMPARE(model.count(), gridview->count());
4632 case ListChange::Moved:
4633 model.moveItems(change.index, change.to, change.count);
4634 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4636 case ListChange::SetCurrent:
4637 case ListChange::SetContentY:
4641 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
4642 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
4644 if ((useDisplaced && displacedEnabled)
4645 || (useAddDisplaced && addDisplacedEnabled)
4646 || (useMoveDisplaced && moveDisplacedEnabled)
4647 || (useRemoveDisplaced && removeDisplacedEnabled)) {
4648 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
4650 // check the correct number of target items and indexes were received
4651 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
4652 for (int i=0; i<resultTargetIndexes.count(); i++)
4653 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
4654 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
4655 for (int i=0; i<resultTargetItems.count(); i++)
4656 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
4658 QCOMPARE(resultTargetIndexes.count(), 0);
4659 QCOMPARE(resultTargetItems.count(), 0);
4662 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
4663 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
4665 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
4666 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
4667 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
4669 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
4670 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
4671 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
4673 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
4675 if (useDisplaced && displacedEnabled
4676 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
4677 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
4678 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
4679 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
4681 QCOMPARE(model_displaced_transitionVia.count(), 0);
4684 // verify all items moved to the correct final positions
4685 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4686 for (int i=0; i < model.count() && i < items.count(); ++i) {
4687 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4688 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4689 QCOMPARE(item->x(), (i%3)*80.0);
4690 QCOMPARE(item->y(), (i/3)*60.0);
4691 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4693 QTRY_COMPARE(name->text(), model.name(i));
4699 void tst_QQuickGridView::displacedTransitions_data()
4701 QTest::addColumn<bool>("useDisplaced");
4702 QTest::addColumn<bool>("displacedEnabled");
4703 QTest::addColumn<bool>("useAddDisplaced");
4704 QTest::addColumn<bool>("addDisplacedEnabled");
4705 QTest::addColumn<bool>("useMoveDisplaced");
4706 QTest::addColumn<bool>("moveDisplacedEnabled");
4707 QTest::addColumn<bool>("useRemoveDisplaced");
4708 QTest::addColumn<bool>("removeDisplacedEnabled");
4709 QTest::addColumn<ListChange>("change");
4710 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4712 QTest::newRow("no displaced transitions at all")
4717 << ListChange::insert(0, 1) << ListRange(0, 17);
4719 QTest::newRow("just displaced")
4724 << ListChange::insert(0, 1) << ListRange(0, 17);
4726 QTest::newRow("just displaced (not enabled)")
4731 << ListChange::insert(0, 1) << ListRange(0, 17);
4733 QTest::newRow("displaced + addDisplaced")
4738 << ListChange::insert(0, 1) << ListRange(0, 17);
4740 QTest::newRow("displaced + addDisplaced (not enabled)")
4745 << ListChange::insert(0, 1) << ListRange(0, 17);
4747 QTest::newRow("displaced + moveDisplaced")
4752 << ListChange::move(0, 10, 1) << ListRange(1, 10);
4754 QTest::newRow("displaced + moveDisplaced (not enabled)")
4759 << ListChange::move(0, 10, 1) << ListRange(1, 10);
4761 QTest::newRow("displaced + removeDisplaced")
4766 << ListChange::remove(0, 1) << ListRange(1, 18);
4768 QTest::newRow("displaced + removeDisplaced (not enabled)")
4773 << ListChange::remove(0, 1) << ListRange(1, 18);
4776 QTest::newRow("displaced + add, should use generic displaced for a remove")
4781 << ListChange::remove(0, 1) << ListRange(1, 18);
4784 void tst_QQuickGridView::multipleTransitions()
4786 // Tests that if you interrupt a transition in progress with another action that
4787 // cancels the previous transition, the resulting items are still placed correctly.
4789 QFETCH(int, initialCount);
4790 QFETCH(qreal, contentY);
4791 QFETCH(QList<ListChange>, changes);
4792 QFETCH(bool, rippleAddDisplaced);
4794 // add transitions on the left, moves on the right
4795 QPointF addTargets_transitionFrom(-50, -50);
4796 QPointF addDisplaced_transitionFrom(-50, 50);
4797 QPointF moveTargets_transitionFrom(50, -50);
4798 QPointF moveDisplaced_transitionFrom(50, 50);
4799 QPointF removeTargets_transitionTo(-100, 300);
4800 QPointF removeDisplaced_transitionFrom(100, 300);
4803 for (int i = 0; i < initialCount; i++)
4804 model.addItem("Original item" + QString::number(i), "");
4806 QQuickView *canvas = createView();
4807 QQmlContext *ctxt = canvas->rootContext();
4808 ctxt->setContextProperty("testModel", &model);
4809 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
4810 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
4811 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
4812 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
4813 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
4814 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
4815 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
4816 canvas->setSource(testFileUrl("multipleTransitions.qml"));
4818 QTest::qWaitForWindowShown(canvas);
4820 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4821 QTRY_VERIFY(gridview != 0);
4822 QQuickItem *contentItem = gridview->contentItem();
4823 QVERIFY(contentItem != 0);
4824 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4826 if (contentY != 0) {
4827 gridview->setContentY(contentY);
4828 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4831 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
4833 for (int i=0; i<changes.count(); i++) {
4834 switch (changes[i].type) {
4835 case ListChange::Inserted:
4837 QList<QPair<QString, QString> > targetItems;
4838 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
4839 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
4840 model.insertItems(changes[i].index, targetItems);
4841 QTRY_COMPARE(model.count(), gridview->count());
4842 if (i == changes.count() - 1) {
4843 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
4844 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
4846 QTest::qWait(timeBetweenActions);
4850 case ListChange::Removed:
4851 model.removeItems(changes[i].index, changes[i].count);
4852 QTRY_COMPARE(model.count(), gridview->count());
4853 if (i == changes.count() - 1) {
4854 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
4855 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
4857 QTest::qWait(timeBetweenActions);
4860 case ListChange::Moved:
4861 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
4862 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4863 if (i == changes.count() - 1) {
4864 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
4865 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
4867 QTest::qWait(timeBetweenActions);
4870 case ListChange::SetCurrent:
4871 gridview->setCurrentIndex(changes[i].index);
4872 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4874 case ListChange::SetContentY:
4875 gridview->setContentY(changes[i].pos);
4876 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4880 QCOMPARE(gridview->count(), model.count());
4882 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4883 int firstVisibleIndex = -1;
4884 for (int i=0; i<items.count(); i++) {
4885 if (items[i]->y() >= contentY) {
4886 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4887 firstVisibleIndex = e.evaluate().toInt();
4891 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4893 // verify all items moved to the correct final positions
4894 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4895 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4896 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4897 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4898 QCOMPARE(item->x(), (i%3)*80.0);
4899 QCOMPARE(item->y(), (i/3)*60.0);
4900 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4902 QTRY_COMPARE(name->text(), model.name(i));
4908 void tst_QQuickGridView::multipleTransitions_data()
4910 QTest::addColumn<int>("initialCount");
4911 QTest::addColumn<qreal>("contentY");
4912 QTest::addColumn<QList<ListChange> >("changes");
4913 QTest::addColumn<bool>("rippleAddDisplaced");
4915 // the added item and displaced items should move to final dest correctly
4916 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
4917 << ListChange::insert(0, 1)
4918 << ListChange::move(0, 3, 1)
4922 // items affected by the add should change from move to add transition
4923 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
4924 << ListChange::move(1, 10, 3)
4925 << ListChange::insert(0, 1)
4929 // items should be placed correctly if you trigger a transition then refill for that index
4930 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
4931 << ListChange::insert(0, 1)
4932 << ListChange::setContentY(160.0)
4933 << ListChange::setContentY(0.0)
4934 << ListChange::insert(0, 1)
4938 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
4939 << ListChange::insert(1, 1)
4940 << ListChange::remove(1, 1)
4945 void tst_QQuickGridView::cacheBuffer()
4947 QQuickView *canvas = createView();
4950 for (int i = 0; i < 90; i++)
4951 model.addItem("Item" + QString::number(i), "");
4953 QQmlContext *ctxt = canvas->rootContext();
4954 ctxt->setContextProperty("testModel", &model);
4955 ctxt->setContextProperty("testRightToLeft", QVariant(false));
4956 ctxt->setContextProperty("testTopToBottom", QVariant(false));
4958 canvas->setSource(testFileUrl("gridview1.qml"));
4960 qApp->processEvents();
4962 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4963 QVERIFY(gridview != 0);
4965 QQuickItem *contentItem = gridview->contentItem();
4966 QVERIFY(contentItem != 0);
4967 QVERIFY(gridview->delegate() != 0);
4968 QVERIFY(gridview->model() != 0);
4970 // Confirm items positioned correctly
4971 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
4972 for (int i = 0; i < model.count() && i < itemCount; ++i) {
4973 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4974 QTRY_COMPARE(item->x(), (i%3)*80.0);
4975 QTRY_COMPARE(item->y(), (i/3)*60.0);
4978 QQmlIncubationController controller;
4979 canvas->engine()->setIncubationController(&controller);
4981 canvas->rootObject()->setProperty("cacheBuffer", 200);
4982 QTRY_VERIFY(gridview->cacheBuffer() == 200);
4984 // items will be created one at a time
4985 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
4986 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
4987 QQuickItem *item = 0;
4990 controller.incubateWhile(&b);
4991 item = findItem<QQuickItem>(gridview, "wrapper", i);
4997 controller.incubateWhile(&b);
5000 int newItemCount = 0;
5001 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5003 // Confirm items positioned correctly
5004 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5005 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5007 QTRY_COMPARE(item->x(), (i%3)*80.0);
5008 QTRY_COMPARE(item->y(), (i/3)*60.0);
5011 // move view and confirm items in view are visible immediately and outside are created async
5012 gridview->setContentY(300);
5014 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5015 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5017 QTRY_COMPARE(item->x(), (i%3)*80.0);
5018 QTRY_COMPARE(item->y(), (i/3)*60.0);
5021 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5023 // ensure buffered items are created
5024 for (int i = 34; i < qMin(44,model.count()); ++i) {
5025 QQuickItem *item = 0;
5027 qGuiApp->processEvents(); // allow refill to happen
5029 controller.incubateWhile(&b);
5030 item = findItem<QQuickItem>(gridview, "wrapper", i);
5036 controller.incubateWhile(&b);
5042 void tst_QQuickGridView::asynchronous()
5044 QQuickView *canvas = createView();
5046 QQmlIncubationController controller;
5047 canvas->engine()->setIncubationController(&controller);
5049 canvas->setSource(testFile("asyncloader.qml"));
5051 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5052 QVERIFY(rootObject);
5054 QQuickGridView *gridview = 0;
5057 controller.incubateWhile(&b);
5058 gridview = rootObject->findChild<QQuickGridView*>("view");
5061 // items will be created one at a time
5062 for (int i = 0; i < 12; ++i) {
5063 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5064 QQuickItem *item = 0;
5067 controller.incubateWhile(&b);
5068 item = findItem<QQuickItem>(gridview, "wrapper", i);
5074 controller.incubateWhile(&b);
5077 // verify positioning
5078 QQuickItem *contentItem = gridview->contentItem();
5079 for (int i = 0; i < 12; ++i) {
5080 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5081 if (!item) qWarning() << "Item" << i << "not found";
5082 QVERIFY(item->x() == (i%3)*100);
5083 QVERIFY(item->y() == (i/3)*100);
5089 void tst_QQuickGridView::unrequestedVisibility()
5092 for (int i = 0; i < 30; i++)
5093 model.addItem("Item" + QString::number(i), QString::number(i));
5095 QQuickView *canvas = new QQuickView(0);
5096 canvas->setGeometry(0,0,240,320);
5098 QQmlContext *ctxt = canvas->rootContext();
5099 ctxt->setContextProperty("testModel", &model);
5100 ctxt->setContextProperty("testWrap", QVariant(false));
5102 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5106 qApp->processEvents();
5108 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5109 QTRY_VERIFY(leftview != 0);
5111 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5112 QTRY_VERIFY(rightview != 0);
5114 QQuickItem *leftContent = leftview->contentItem();
5115 QTRY_VERIFY(leftContent != 0);
5117 QQuickItem *rightContent = rightview->contentItem();
5118 QTRY_VERIFY(rightContent != 0);
5120 rightview->setCurrentIndex(12);
5122 QTRY_COMPARE(leftview->contentY(), 0.0);
5123 QTRY_COMPARE(rightview->contentY(), 240.0);
5127 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5128 QCOMPARE(item->isVisible(), true);
5129 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5130 QCOMPARE(item->isVisible(), false);
5132 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5133 QCOMPARE(item->isVisible(), false);
5134 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5135 QCOMPARE(item->isVisible(), true);
5137 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5138 QCOMPARE(item->isVisible(), true);
5139 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5140 QCOMPARE(item->isVisible(), false);
5141 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5142 QCOMPARE(item->isVisible(), false);
5143 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5144 QCOMPARE(item->isVisible(), true);
5146 rightview->setCurrentIndex(0);
5148 QTRY_COMPARE(leftview->contentY(), 0.0);
5149 QTRY_COMPARE(rightview->contentY(), 0.0);
5151 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5152 QCOMPARE(item->isVisible(), true);
5153 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5154 QTRY_COMPARE(item->isVisible(), true);
5156 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5157 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5159 leftview->setCurrentIndex(12);
5161 QTRY_COMPARE(leftview->contentY(), 240.0);
5162 QTRY_COMPARE(rightview->contentY(), 0.0);
5164 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5165 QTRY_COMPARE(item->isVisible(), false);
5166 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5167 QCOMPARE(item->isVisible(), true);
5169 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5170 QCOMPARE(item->isVisible(), true);
5171 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5172 QCOMPARE(item->isVisible(), false);
5174 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5175 QCOMPARE(item->isVisible(), false);
5176 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5177 QCOMPARE(item->isVisible(), true);
5178 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5179 QCOMPARE(item->isVisible(), true);
5180 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5181 QCOMPARE(item->isVisible(), false);
5183 // move a non-visible item into view
5184 model.moveItems(10, 9, 1);
5185 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5187 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5188 QCOMPARE(item->isVisible(), false);
5189 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5190 QCOMPARE(item->isVisible(), true);
5192 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5193 QCOMPARE(item->isVisible(), true);
5194 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5195 QCOMPARE(item->isVisible(), false);
5197 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5198 QCOMPARE(item->isVisible(), false);
5199 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5200 QCOMPARE(item->isVisible(), true);
5201 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5202 QCOMPARE(item->isVisible(), true);
5203 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5204 QCOMPARE(item->isVisible(), false);
5206 // move a visible item out of view
5207 model.moveItems(5, 3, 1);
5208 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5210 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5211 QCOMPARE(item->isVisible(), false);
5212 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5213 QCOMPARE(item->isVisible(), true);
5214 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5215 QCOMPARE(item->isVisible(), true);
5216 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5217 QCOMPARE(item->isVisible(), false);
5219 // move a non-visible item into view
5220 model.moveItems(3, 5, 1);
5221 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5223 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5224 QCOMPARE(item->isVisible(), false);
5225 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5226 QCOMPARE(item->isVisible(), true);
5227 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5228 QCOMPARE(item->isVisible(), true);
5229 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5230 QCOMPARE(item->isVisible(), false);
5232 // move a visible item out of view
5233 model.moveItems(9, 10, 1);
5234 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5236 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5237 QCOMPARE(item->isVisible(), false);
5238 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5239 QCOMPARE(item->isVisible(), true);
5240 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5241 QCOMPARE(item->isVisible(), true);
5242 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5243 QCOMPARE(item->isVisible(), false);
5245 // move a non-visible item into view
5246 model.moveItems(10, 9, 1);
5247 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5249 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5250 QCOMPARE(item->isVisible(), false);
5251 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5252 QCOMPARE(item->isVisible(), true);
5253 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5254 QCOMPARE(item->isVisible(), true);
5255 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5256 QCOMPARE(item->isVisible(), false);
5261 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
5265 for (int i=0; i<list.count(); i++) {
5266 ret << list[i].toInt(&ok);
5268 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
5274 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
5276 for (int i=0; i<indexLists.count(); i++) {
5277 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
5278 if (current != expectedIndexes.toSet())
5279 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
5280 QCOMPARE(current, expectedIndexes.toSet());
5284 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
5286 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
5287 QVERIFY(it.value().type() == QVariant::Int);
5288 QString name = it.key();
5289 int itemIndex = it.value().toInt();
5290 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
5291 if (model.name(itemIndex) != name)
5292 qDebug() << itemIndex;
5293 QCOMPARE(model.name(itemIndex), name);
5295 QCOMPARE(items.count(), expectedIndexes.count());
5298 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
5300 for (int i=0; i<itemLists.count(); i++) {
5301 QVariantList current = itemLists[i].toList();
5302 for (int j=0; j<current.count(); j++) {
5303 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
5304 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
5305 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
5307 QCOMPARE(current.count(), expectedItems.count());
5311 QTEST_MAIN(tst_QQuickGridView)
5313 #include "tst_qquickgridview.moc"