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());
1776 // moving currentItem out of view should make it invisible
1777 gridview->setCurrentIndex(0);
1778 QTRY_VERIFY(gridview->currentItem()->isVisible());
1779 gridview->setContentY(200);
1780 QTRY_VERIFY(!gridview->currentItem()->isVisible());
1785 void tst_QQuickGridView::noCurrentIndex()
1788 for (int i = 0; i < 60; i++)
1789 model.addItem("Item" + QString::number(i), QString::number(i));
1791 QQuickView *canvas = new QQuickView(0);
1792 canvas->setGeometry(0,0,240,320);
1794 QQmlContext *ctxt = canvas->rootContext();
1795 ctxt->setContextProperty("testModel", &model);
1797 QString filename(testFile("gridview-noCurrent.qml"));
1798 canvas->setSource(QUrl::fromLocalFile(filename));
1800 qApp->processEvents();
1802 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1803 QVERIFY(gridview != 0);
1804 QQuickItem *contentItem = gridview->contentItem();
1805 QVERIFY(contentItem != 0);
1806 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1808 // current index should be -1
1809 QCOMPARE(gridview->currentIndex(), -1);
1810 QVERIFY(!gridview->currentItem());
1811 QVERIFY(!gridview->highlightItem());
1812 QCOMPARE(gridview->contentY(), 0.0);
1814 gridview->setCurrentIndex(5);
1815 QCOMPARE(gridview->currentIndex(), 5);
1816 QVERIFY(gridview->currentItem());
1817 QVERIFY(gridview->highlightItem());
1822 void tst_QQuickGridView::changeFlow()
1824 QQuickView *canvas = createView();
1827 for (int i = 0; i < 30; i++)
1828 model.addItem("Item" + QString::number(i), QString::number(i));
1830 QQmlContext *ctxt = canvas->rootContext();
1831 ctxt->setContextProperty("testModel", &model);
1832 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1833 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1835 canvas->setSource(testFileUrl("gridview1.qml"));
1836 qApp->processEvents();
1838 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1839 QTRY_VERIFY(gridview != 0);
1841 QQuickItem *contentItem = gridview->contentItem();
1842 QTRY_VERIFY(contentItem != 0);
1844 // Confirm items positioned correctly and indexes correct
1845 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1846 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1847 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1848 if (!item) qWarning() << "Item" << i << "not found";
1850 QTRY_COMPARE(item->x(), qreal((i%3)*80));
1851 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1852 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1853 QTRY_VERIFY(name != 0);
1854 QTRY_COMPARE(name->text(), model.name(i));
1855 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1856 QTRY_VERIFY(number != 0);
1857 QTRY_COMPARE(number->text(), model.number(i));
1860 ctxt->setContextProperty("testTopToBottom", QVariant(true));
1862 // Confirm items positioned correctly and indexes correct
1863 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1864 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1865 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1866 if (!item) qWarning() << "Item" << i << "not found";
1868 QTRY_COMPARE(item->x(), qreal((i/5)*80));
1869 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1870 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1871 QTRY_VERIFY(name != 0);
1872 QTRY_COMPARE(name->text(), model.name(i));
1873 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1874 QTRY_VERIFY(number != 0);
1875 QTRY_COMPARE(number->text(), model.number(i));
1878 ctxt->setContextProperty("testRightToLeft", QVariant(true));
1880 // Confirm items positioned correctly and indexes correct
1881 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1882 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1883 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1884 if (!item) qWarning() << "Item" << i << "not found";
1886 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
1887 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1888 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1889 QTRY_VERIFY(name != 0);
1890 QTRY_COMPARE(name->text(), model.name(i));
1891 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1892 QTRY_VERIFY(number != 0);
1893 QTRY_COMPARE(number->text(), model.number(i));
1895 gridview->setContentX(100);
1896 QTRY_COMPARE(gridview->contentX(), 100.);
1897 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1898 QTRY_COMPARE(gridview->contentX(), 0.);
1900 // Confirm items positioned correctly and indexes correct
1901 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1902 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1903 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1904 if (!item) qWarning() << "Item" << i << "not found";
1906 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
1907 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1908 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1909 QTRY_VERIFY(name != 0);
1910 QTRY_COMPARE(name->text(), model.name(i));
1911 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1912 QTRY_VERIFY(number != 0);
1913 QTRY_COMPARE(number->text(), model.number(i));
1919 void tst_QQuickGridView::defaultValues()
1922 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
1923 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1925 QTRY_VERIFY(obj != 0);
1926 QTRY_VERIFY(obj->model() == QVariant());
1927 QTRY_VERIFY(obj->delegate() == 0);
1928 QTRY_COMPARE(obj->currentIndex(), -1);
1929 QTRY_VERIFY(obj->currentItem() == 0);
1930 QTRY_COMPARE(obj->count(), 0);
1931 QTRY_VERIFY(obj->highlight() == 0);
1932 QTRY_VERIFY(obj->highlightItem() == 0);
1933 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
1934 QTRY_VERIFY(obj->flow() == 0);
1935 QTRY_COMPARE(obj->isWrapEnabled(), false);
1936 QTRY_COMPARE(obj->cacheBuffer(), 0);
1937 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
1938 QTRY_COMPARE(obj->cellHeight(), qreal(100));
1942 void tst_QQuickGridView::properties()
1945 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
1946 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1948 QTRY_VERIFY(obj != 0);
1949 QTRY_VERIFY(obj->model() != QVariant());
1950 QTRY_VERIFY(obj->delegate() != 0);
1951 QTRY_COMPARE(obj->currentIndex(), 0);
1952 QTRY_VERIFY(obj->currentItem() != 0);
1953 QTRY_COMPARE(obj->count(), 4);
1954 QTRY_VERIFY(obj->highlight() != 0);
1955 QTRY_VERIFY(obj->highlightItem() != 0);
1956 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
1957 QTRY_VERIFY(obj->flow() == 0);
1958 QTRY_COMPARE(obj->isWrapEnabled(), true);
1959 QTRY_COMPARE(obj->cacheBuffer(), 200);
1960 QTRY_COMPARE(obj->cellWidth(), qreal(100));
1961 QTRY_COMPARE(obj->cellHeight(), qreal(100));
1965 void tst_QQuickGridView::propertyChanges()
1967 QQuickView *canvas = createView();
1968 QTRY_VERIFY(canvas);
1969 canvas->setSource(testFileUrl("propertychangestest.qml"));
1971 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
1972 QTRY_VERIFY(gridView);
1974 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
1975 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
1976 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
1977 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
1979 QTRY_COMPARE(gridView->isWrapEnabled(), true);
1980 QTRY_COMPARE(gridView->cacheBuffer(), 10);
1981 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
1983 gridView->setWrapEnabled(false);
1984 gridView->setCacheBuffer(3);
1985 gridView->setFlow(QQuickGridView::TopToBottom);
1987 QTRY_COMPARE(gridView->isWrapEnabled(), false);
1988 QTRY_COMPARE(gridView->cacheBuffer(), 3);
1989 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
1991 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1992 QTRY_COMPARE(cacheBufferSpy.count(),1);
1993 QTRY_COMPARE(flowSpy.count(),1);
1995 gridView->setWrapEnabled(false);
1996 gridView->setCacheBuffer(3);
1997 gridView->setFlow(QQuickGridView::TopToBottom);
1999 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2000 QTRY_COMPARE(cacheBufferSpy.count(),1);
2001 QTRY_COMPARE(flowSpy.count(),1);
2003 gridView->setFlow(QQuickGridView::LeftToRight);
2004 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
2006 gridView->setWrapEnabled(true);
2007 gridView->setCacheBuffer(5);
2008 gridView->setLayoutDirection(Qt::RightToLeft);
2010 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2011 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2012 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2014 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2015 QTRY_COMPARE(cacheBufferSpy.count(),2);
2016 QTRY_COMPARE(layoutSpy.count(),1);
2017 QTRY_COMPARE(flowSpy.count(),2);
2019 gridView->setWrapEnabled(true);
2020 gridView->setCacheBuffer(5);
2021 gridView->setLayoutDirection(Qt::RightToLeft);
2023 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2024 QTRY_COMPARE(cacheBufferSpy.count(),2);
2025 QTRY_COMPARE(layoutSpy.count(),1);
2026 QTRY_COMPARE(flowSpy.count(),2);
2028 gridView->setFlow(QQuickGridView::TopToBottom);
2029 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
2030 QTRY_COMPARE(flowSpy.count(),3);
2032 gridView->setFlow(QQuickGridView::TopToBottom);
2033 QTRY_COMPARE(flowSpy.count(),3);
2038 void tst_QQuickGridView::componentChanges()
2040 QQuickView *canvas = createView();
2041 QTRY_VERIFY(canvas);
2042 canvas->setSource(testFileUrl("propertychangestest.qml"));
2044 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2045 QTRY_VERIFY(gridView);
2047 QQmlComponent component(canvas->engine());
2048 component.setData("import QtQuick 1.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2050 QQmlComponent delegateComponent(canvas->engine());
2051 delegateComponent.setData("import QtQuick 1.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2053 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2054 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2055 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2056 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2058 gridView->setHighlight(&component);
2059 gridView->setDelegate(&delegateComponent);
2060 gridView->setHeader(&component);
2061 gridView->setFooter(&component);
2063 QTRY_COMPARE(gridView->highlight(), &component);
2064 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2065 QTRY_COMPARE(gridView->header(), &component);
2066 QTRY_COMPARE(gridView->footer(), &component);
2068 QTRY_COMPARE(highlightSpy.count(),1);
2069 QTRY_COMPARE(delegateSpy.count(),1);
2070 QTRY_COMPARE(headerSpy.count(),1);
2071 QTRY_COMPARE(footerSpy.count(),1);
2073 gridView->setHighlight(&component);
2074 gridView->setDelegate(&delegateComponent);
2075 gridView->setHeader(&component);
2076 gridView->setFooter(&component);
2078 QTRY_COMPARE(highlightSpy.count(),1);
2079 QTRY_COMPARE(delegateSpy.count(),1);
2080 QTRY_COMPARE(headerSpy.count(),1);
2081 QTRY_COMPARE(footerSpy.count(),1);
2086 void tst_QQuickGridView::modelChanges()
2088 QQuickView *canvas = createView();
2089 QTRY_VERIFY(canvas);
2090 canvas->setSource(testFileUrl("propertychangestest.qml"));
2092 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2093 QTRY_VERIFY(gridView);
2095 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2096 QTRY_VERIFY(alternateModel);
2097 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2098 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2100 gridView->setModel(modelVariant);
2101 QTRY_COMPARE(gridView->model(), modelVariant);
2102 QTRY_COMPARE(modelSpy.count(),1);
2104 gridView->setModel(modelVariant);
2105 QTRY_COMPARE(modelSpy.count(),1);
2107 gridView->setModel(QVariant());
2108 QTRY_COMPARE(modelSpy.count(),2);
2112 void tst_QQuickGridView::positionViewAtIndex()
2114 QQuickView *canvas = createView();
2117 for (int i = 0; i < 40; i++)
2118 model.addItem("Item" + QString::number(i), "");
2120 QQmlContext *ctxt = canvas->rootContext();
2121 ctxt->setContextProperty("testModel", &model);
2122 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2123 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2125 canvas->setSource(testFileUrl("gridview1.qml"));
2127 qApp->processEvents();
2129 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2130 QTRY_VERIFY(gridview != 0);
2131 QQuickItem *contentItem = gridview->contentItem();
2132 QTRY_VERIFY(contentItem != 0);
2133 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2135 // Confirm items positioned correctly
2136 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2137 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2138 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2139 if (!item) qWarning() << "Item" << i << "not found";
2141 QTRY_COMPARE(item->x(), (i%3)*80.);
2142 QTRY_COMPARE(item->y(), (i/3)*60.);
2145 // Position on a currently visible item
2146 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2147 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2148 QTRY_COMPARE(gridview->contentY(), 60.);
2150 // Confirm items positioned correctly
2151 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2152 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2153 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2154 if (!item) qWarning() << "Item" << i << "not found";
2156 QTRY_COMPARE(item->x(), (i%3)*80.);
2157 QTRY_COMPARE(item->y(), (i/3)*60.);
2160 // Position on an item beyond the visible items
2161 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2162 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2163 QTRY_COMPARE(gridview->contentY(), 420.);
2165 // Confirm items positioned correctly
2166 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2167 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2168 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2169 if (!item) qWarning() << "Item" << i << "not found";
2171 QTRY_COMPARE(item->x(), (i%3)*80.);
2172 QTRY_COMPARE(item->y(), (i/3)*60.);
2175 // Position on an item that would leave empty space if positioned at the top
2176 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2177 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2178 QTRY_COMPARE(gridview->contentY(), 520.);
2180 // Confirm items positioned correctly
2181 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2182 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2183 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2184 if (!item) qWarning() << "Item" << i << "not found";
2186 QTRY_COMPARE(item->x(), (i%3)*80.);
2187 QTRY_COMPARE(item->y(), (i/3)*60.);
2190 // Position at the beginning again
2191 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2192 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2193 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2194 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2195 QTRY_COMPARE(gridview->contentY(), 0.);
2197 // Confirm items positioned correctly
2198 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2199 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2200 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2201 if (!item) qWarning() << "Item" << i << "not found";
2203 QTRY_COMPARE(item->x(), (i%3)*80.);
2204 QTRY_COMPARE(item->y(), (i/3)*60.);
2208 gridview->positionViewAtIndex(30, QQuickGridView::End);
2209 QTRY_COMPARE(gridview->contentY(), 340.);
2211 // Position in Center
2212 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2213 QTRY_COMPARE(gridview->contentY(), 170.);
2215 // Ensure at least partially visible
2216 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2217 QTRY_COMPARE(gridview->contentY(), 170.);
2219 gridview->setContentY(302);
2220 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2221 QTRY_COMPARE(gridview->contentY(), 302.);
2223 gridview->setContentY(360);
2224 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2225 QTRY_COMPARE(gridview->contentY(), 300.);
2227 gridview->setContentY(60);
2228 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2229 QTRY_COMPARE(gridview->contentY(), 60.);
2231 gridview->setContentY(20);
2232 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2233 QTRY_COMPARE(gridview->contentY(), 100.);
2235 // Ensure completely visible
2236 gridview->setContentY(120);
2237 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2238 QTRY_COMPARE(gridview->contentY(), 120.);
2240 gridview->setContentY(302);
2241 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2242 QTRY_COMPARE(gridview->contentY(), 300.);
2244 gridview->setContentY(60);
2245 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2246 QTRY_COMPARE(gridview->contentY(), 100.);
2248 // Test for Top To Bottom layout
2249 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2251 // Confirm items positioned correctly
2252 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2253 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2254 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2255 if (!item) qWarning() << "Item" << i << "not found";
2257 QTRY_COMPARE(item->x(), (i/5)*80.);
2258 QTRY_COMPARE(item->y(), (i%5)*60.);
2262 gridview->positionViewAtIndex(30, QQuickGridView::End);
2263 QTRY_COMPARE(gridview->contentX(), 320.);
2264 QTRY_COMPARE(gridview->contentY(), 0.);
2266 // Position in Center
2267 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2268 QTRY_COMPARE(gridview->contentX(), 160.);
2270 // Ensure at least partially visible
2271 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2272 QTRY_COMPARE(gridview->contentX(), 160.);
2274 gridview->setContentX(170);
2275 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2276 QTRY_COMPARE(gridview->contentX(), 170.);
2278 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2279 QTRY_COMPARE(gridview->contentX(), 320.);
2281 gridview->setContentX(170);
2282 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2283 QTRY_COMPARE(gridview->contentX(), 240.);
2285 // positionViewAtBeginning
2286 gridview->positionViewAtBeginning();
2287 QTRY_COMPARE(gridview->contentX(), 0.);
2289 gridview->setContentX(80);
2290 canvas->rootObject()->setProperty("showHeader", true);
2291 gridview->positionViewAtBeginning();
2292 QTRY_COMPARE(gridview->contentX(), -30.);
2294 // positionViewAtEnd
2295 gridview->positionViewAtEnd();
2296 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2298 gridview->setContentX(80);
2299 canvas->rootObject()->setProperty("showFooter", true);
2300 gridview->positionViewAtEnd();
2301 QTRY_COMPARE(gridview->contentX(), 430.);
2303 // set current item to outside visible view, position at beginning
2304 // and ensure highlight moves to current item
2305 gridview->setCurrentIndex(6);
2306 gridview->positionViewAtBeginning();
2307 QTRY_COMPARE(gridview->contentX(), -30.);
2308 QVERIFY(gridview->highlightItem());
2309 QCOMPARE(gridview->highlightItem()->x(), 80.);
2314 void tst_QQuickGridView::snapping()
2316 QQuickView *canvas = createView();
2319 for (int i = 0; i < 40; i++)
2320 model.addItem("Item" + QString::number(i), "");
2322 QQmlContext *ctxt = canvas->rootContext();
2323 ctxt->setContextProperty("testModel", &model);
2324 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2325 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2327 canvas->setSource(testFileUrl("gridview1.qml"));
2328 qApp->processEvents();
2330 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2331 QTRY_VERIFY(gridview != 0);
2333 gridview->setHeight(220);
2334 QCOMPARE(gridview->height(), 220.);
2336 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2337 QCOMPARE(gridview->contentY(), 80.);
2339 gridview->setContentY(0);
2340 QCOMPARE(gridview->contentY(), 0.);
2342 gridview->setSnapMode(QQuickGridView::SnapToRow);
2343 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2345 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2346 QCOMPARE(gridview->contentY(), 60.);
2348 gridview->positionViewAtIndex(15, QQuickGridView::End);
2349 QCOMPARE(gridview->contentY(), 120.);
2355 void tst_QQuickGridView::mirroring()
2357 QQuickView *canvasA = createView();
2358 canvasA->setSource(testFileUrl("mirroring.qml"));
2359 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2360 QTRY_VERIFY(gridviewA != 0);
2362 QQuickView *canvasB = createView();
2363 canvasB->setSource(testFileUrl("mirroring.qml"));
2364 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2365 QTRY_VERIFY(gridviewA != 0);
2366 qApp->processEvents();
2368 QList<QString> objectNames;
2369 objectNames << "item1" << "item2"; // << "item3"
2371 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2372 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2373 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2376 foreach (const QString objectName, objectNames)
2377 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2379 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2380 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2383 foreach (const QString objectName, objectNames)
2384 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2386 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2387 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2388 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2390 // LTR != LTR+mirror
2391 foreach (const QString objectName, objectNames)
2392 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2394 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2396 // RTL == LTR+mirror
2397 foreach (const QString objectName, objectNames)
2398 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2400 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2402 // RTL != RTL+mirror
2403 foreach (const QString objectName, objectNames)
2404 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2406 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2408 // LTR == RTL+mirror
2409 foreach (const QString objectName, objectNames)
2410 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2416 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2418 QQuickView *canvas = createView();
2421 for (int i = 0; i < 40; i++)
2422 model.addItem("Item" + QString::number(i), "");
2424 QQmlContext *ctxt = canvas->rootContext();
2425 ctxt->setContextProperty("testModel", &model);
2426 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2427 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2429 canvas->setSource(testFileUrl("gridview1.qml"));
2430 qApp->processEvents();
2432 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2433 QTRY_VERIFY(gridview != 0);
2435 QQuickItem *contentItem = gridview->contentItem();
2436 QTRY_VERIFY(contentItem != 0);
2438 // Confirm items positioned correctly
2439 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2440 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2441 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2442 if (!item) qWarning() << "Item" << i << "not found";
2444 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2445 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2448 // Position on a currently visible item
2449 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2450 QTRY_COMPARE(gridview->contentX(), -320.);
2452 // Confirm items positioned correctly
2453 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2454 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2455 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2456 if (!item) qWarning() << "Item" << i << "not found";
2458 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2459 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2462 // Position on an item beyond the visible items
2463 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2464 QTRY_COMPARE(gridview->contentX(), -560.);
2466 // Confirm items positioned correctly
2467 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2468 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2469 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2470 if (!item) qWarning() << "Item" << i << "not found";
2472 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2473 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2476 // Position on an item that would leave empty space if positioned at the top
2477 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2478 QTRY_COMPARE(gridview->contentX(), -640.);
2480 // Confirm items positioned correctly
2481 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2482 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2483 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2484 if (!item) qWarning() << "Item" << i << "not found";
2486 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2487 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2490 // Position at the beginning again
2491 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2492 QTRY_COMPARE(gridview->contentX(), -240.);
2494 // Confirm items positioned correctly
2495 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2496 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2497 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2498 if (!item) qWarning() << "Item" << i << "not found";
2500 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2501 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2505 gridview->positionViewAtIndex(30, QQuickGridView::End);
2506 QTRY_COMPARE(gridview->contentX(), -560.);
2508 // Position in Center
2509 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2510 QTRY_COMPARE(gridview->contentX(), -400.);
2512 // Ensure at least partially visible
2513 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2514 QTRY_COMPARE(gridview->contentX(), -400.);
2516 gridview->setContentX(-555.);
2517 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2518 QTRY_COMPARE(gridview->contentX(), -555.);
2520 gridview->setContentX(-239);
2521 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2522 QTRY_COMPARE(gridview->contentX(), -320.);
2524 gridview->setContentX(-239);
2525 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2526 QTRY_COMPARE(gridview->contentX(), -400.);
2528 gridview->setContentX(-640);
2529 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2530 QTRY_COMPARE(gridview->contentX(), -560.);
2532 // Ensure completely visible
2533 gridview->setContentX(-400);
2534 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2535 QTRY_COMPARE(gridview->contentX(), -400.);
2537 gridview->setContentX(-315);
2538 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2539 QTRY_COMPARE(gridview->contentX(), -320.);
2541 gridview->setContentX(-640);
2542 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2543 QTRY_COMPARE(gridview->contentX(), -560.);
2548 void tst_QQuickGridView::resetModel()
2550 QQuickView *canvas = createView();
2552 QStringList strings;
2553 strings << "one" << "two" << "three";
2554 QStringListModel model(strings);
2556 QQmlContext *ctxt = canvas->rootContext();
2557 ctxt->setContextProperty("testModel", &model);
2559 canvas->setSource(testFileUrl("displaygrid.qml"));
2561 qApp->processEvents();
2563 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2564 QTRY_VERIFY(gridview != 0);
2565 QQuickItem *contentItem = gridview->contentItem();
2566 QTRY_VERIFY(contentItem != 0);
2567 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2569 QTRY_COMPARE(gridview->count(), model.rowCount());
2571 for (int i = 0; i < model.rowCount(); ++i) {
2572 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2573 QTRY_VERIFY(display != 0);
2574 QTRY_COMPARE(display->text(), strings.at(i));
2578 strings << "four" << "five" << "six" << "seven";
2579 model.setStringList(strings);
2581 QTRY_COMPARE(gridview->count(), model.rowCount());
2583 for (int i = 0; i < model.rowCount(); ++i) {
2584 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2585 QTRY_VERIFY(display != 0);
2586 QTRY_COMPARE(display->text(), strings.at(i));
2592 void tst_QQuickGridView::enforceRange()
2594 QQuickView *canvas = createView();
2597 for (int i = 0; i < 30; i++)
2598 model.addItem("Item" + QString::number(i), "");
2600 QQmlContext *ctxt = canvas->rootContext();
2601 ctxt->setContextProperty("testModel", &model);
2602 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2603 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2605 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2607 qApp->processEvents();
2608 QVERIFY(canvas->rootObject() != 0);
2610 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2611 QTRY_VERIFY(gridview != 0);
2613 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2614 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2615 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2616 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2618 QQuickItem *contentItem = gridview->contentItem();
2619 QTRY_VERIFY(contentItem != 0);
2621 // view should be positioned at the top of the range.
2622 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2624 QTRY_COMPARE(gridview->contentY(), -100.0);
2626 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2627 QTRY_VERIFY(name != 0);
2628 QTRY_COMPARE(name->text(), model.name(0));
2629 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2630 QTRY_VERIFY(number != 0);
2631 QTRY_COMPARE(number->text(), model.number(0));
2633 // Check currentIndex is updated when contentItem moves
2634 gridview->setContentY(0);
2635 QTRY_COMPARE(gridview->currentIndex(), 2);
2637 gridview->setCurrentIndex(5);
2638 QTRY_COMPARE(gridview->contentY(), 100.);
2641 for (int i = 0; i < 5; i++)
2642 model2.addItem("Item" + QString::number(i), "");
2644 ctxt->setContextProperty("testModel", &model2);
2645 QCOMPARE(gridview->count(), 5);
2650 void tst_QQuickGridView::enforceRange_rightToLeft()
2652 QQuickView *canvas = createView();
2655 for (int i = 0; i < 30; i++)
2656 model.addItem("Item" + QString::number(i), "");
2658 QQmlContext *ctxt = canvas->rootContext();
2659 ctxt->setContextProperty("testModel", &model);
2660 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2661 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2663 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2664 qApp->processEvents();
2665 QVERIFY(canvas->rootObject() != 0);
2667 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2668 QTRY_VERIFY(gridview != 0);
2670 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2671 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2672 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2674 QQuickItem *contentItem = gridview->contentItem();
2675 QVERIFY(contentItem != 0);
2677 // view should be positioned at the top of the range.
2678 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2680 QTRY_COMPARE(gridview->contentX(), -140.);
2681 QTRY_COMPARE(gridview->contentY(), 0.0);
2683 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2684 QTRY_VERIFY(name != 0);
2685 QTRY_COMPARE(name->text(), model.name(0));
2686 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2687 QTRY_VERIFY(number != 0);
2688 QTRY_COMPARE(number->text(), model.number(0));
2690 // Check currentIndex is updated when contentItem moves
2691 gridview->setContentX(-240);
2692 QTRY_COMPARE(gridview->currentIndex(), 3);
2694 gridview->setCurrentIndex(7);
2695 QTRY_COMPARE(gridview->contentX(), -340.);
2696 QTRY_COMPARE(gridview->contentY(), 0.0);
2699 for (int i = 0; i < 5; i++)
2700 model2.addItem("Item" + QString::number(i), "");
2702 ctxt->setContextProperty("testModel", &model2);
2703 QCOMPARE(gridview->count(), 5);
2708 void tst_QQuickGridView::QTBUG_8456()
2710 QQuickView *canvas = createView();
2712 canvas->setSource(testFileUrl("setindex.qml"));
2713 qApp->processEvents();
2715 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2716 QTRY_VERIFY(gridview != 0);
2718 QTRY_COMPARE(gridview->currentIndex(), 0);
2723 void tst_QQuickGridView::manualHighlight()
2725 QQuickView *canvas = createView();
2727 QString filename(testFile("manual-highlight.qml"));
2728 canvas->setSource(QUrl::fromLocalFile(filename));
2730 qApp->processEvents();
2732 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2733 QTRY_VERIFY(gridview != 0);
2735 QQuickItem *contentItem = gridview->contentItem();
2736 QTRY_VERIFY(contentItem != 0);
2738 QTRY_COMPARE(gridview->currentIndex(), 0);
2739 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2740 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2741 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2743 gridview->setCurrentIndex(2);
2745 QTRY_COMPARE(gridview->currentIndex(), 2);
2746 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2747 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2748 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2750 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
2752 QTRY_COMPARE(gridview->currentIndex(), 2);
2753 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2754 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2755 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2757 gridview->setFlow(QQuickGridView::TopToBottom);
2758 QTRY_COMPARE(gridview->flow(), QQuickGridView::TopToBottom);
2760 gridview->setCurrentIndex(0);
2761 QTRY_COMPARE(gridview->currentIndex(), 0);
2762 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2763 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2764 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2770 void tst_QQuickGridView::footer()
2772 QFETCH(QQuickGridView::Flow, flow);
2773 QFETCH(Qt::LayoutDirection, layoutDirection);
2774 QFETCH(QPointF, initialFooterPos);
2775 QFETCH(QPointF, changedFooterPos);
2776 QFETCH(QPointF, initialContentPos);
2777 QFETCH(QPointF, changedContentPos);
2778 QFETCH(QPointF, firstDelegatePos);
2779 QFETCH(QPointF, resizeContentPos);
2781 QQuickView *canvas = createView();
2785 for (int i = 0; i < 7; i++)
2786 model.addItem("Item" + QString::number(i), "");
2788 QQmlContext *ctxt = canvas->rootContext();
2789 ctxt->setContextProperty("testModel", &model);
2791 canvas->setSource(testFileUrl("footer.qml"));
2792 qApp->processEvents();
2794 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2795 QTRY_VERIFY(gridview != 0);
2796 gridview->setFlow(flow);
2797 gridview->setLayoutDirection(layoutDirection);
2799 QQuickItem *contentItem = gridview->contentItem();
2800 QTRY_VERIFY(contentItem != 0);
2802 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
2805 QVERIFY(footer == gridview->footerItem());
2807 QCOMPARE(footer->pos(), initialFooterPos);
2808 QCOMPARE(footer->width(), 100.);
2809 QCOMPARE(footer->height(), 30.);
2810 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2812 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2814 QCOMPARE(item->pos(), firstDelegatePos);
2816 if (flow == QQuickGridView::LeftToRight) {
2817 // shrink by one row
2818 model.removeItem(2);
2819 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
2821 // shrink by one column
2822 model.removeItem(2);
2823 model.removeItem(3);
2824 if (layoutDirection == Qt::LeftToRight)
2825 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
2827 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
2833 QPointF posWhenNoItems(0, 0);
2834 if (layoutDirection == Qt::RightToLeft)
2835 posWhenNoItems.setX(flow == QQuickGridView::LeftToRight ? gridview->width() - footer->width() : -footer->width());
2836 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2838 // if header is present, it's at a negative pos, so the footer should not move
2839 canvas->rootObject()->setProperty("showHeader", true);
2840 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
2841 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2842 canvas->rootObject()->setProperty("showHeader", false);
2845 for (int i = 0; i < 30; i++)
2846 model.addItem("Item" + QString::number(i), "");
2848 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
2849 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
2851 QCOMPARE(footerItemSpy.count(), 1);
2853 footer = findItem<QQuickText>(contentItem, "footer");
2855 footer = findItem<QQuickText>(contentItem, "footer2");
2858 QVERIFY(footer == gridview->footerItem());
2860 QCOMPARE(footer->pos(), changedFooterPos);
2861 QCOMPARE(footer->width(), 50.);
2862 QCOMPARE(footer->height(), 20.);
2863 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2865 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2867 QCOMPARE(item->pos(), firstDelegatePos);
2869 gridview->positionViewAtEnd();
2870 footer->setHeight(10);
2871 footer->setWidth(40);
2872 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
2877 void tst_QQuickGridView::footer_data()
2879 QTest::addColumn<QQuickGridView::Flow>("flow");
2880 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2881 QTest::addColumn<QPointF>("initialFooterPos");
2882 QTest::addColumn<QPointF>("changedFooterPos");
2883 QTest::addColumn<QPointF>("initialContentPos");
2884 QTest::addColumn<QPointF>("changedContentPos");
2885 QTest::addColumn<QPointF>("firstDelegatePos");
2886 QTest::addColumn<QPointF>("resizeContentPos");
2888 // footer1 = 100 x 30
2889 // footer2 = 50 x 20
2892 // view height = 320
2894 // footer below items, bottom left
2895 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
2896 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
2897 << QPointF(0, 10 * 60) // 30 items = 10 rows
2901 << QPointF(0, 10 * 60 - 320 + 10);
2903 // footer below items, bottom right
2904 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
2905 << QPointF(240 - 100, 3 * 60)
2906 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
2909 << QPointF(240 - 80, 0)
2910 << QPointF(0, 10 * 60 - 320 + 10);
2912 // footer to right of items
2913 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
2914 << QPointF(2 * 80, 0) // 2 columns, cell width 80
2915 << QPointF(6 * 80, 0) // 30 items = 6 columns
2919 << QPointF(6 * 80 - 240 + 40, 0);
2921 // footer to left of items
2922 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
2923 << QPointF(-(2 * 80) - 100, 0)
2924 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
2926 << QPointF(-240, 0) // unchanged, footer change doesn't change content pos
2928 << QPointF(-(6 * 80) - 40, 0);
2931 void tst_QQuickGridView::header()
2933 QFETCH(QQuickGridView::Flow, flow);
2934 QFETCH(Qt::LayoutDirection, layoutDirection);
2935 QFETCH(QPointF, initialHeaderPos);
2936 QFETCH(QPointF, changedHeaderPos);
2937 QFETCH(QPointF, initialContentPos);
2938 QFETCH(QPointF, changedContentPos);
2939 QFETCH(QPointF, firstDelegatePos);
2940 QFETCH(QPointF, resizeContentPos);
2943 for (int i = 0; i < 30; i++)
2944 model.addItem("Item" + QString::number(i), "");
2946 QQuickView *canvas = createView();
2947 canvas->rootContext()->setContextProperty("testModel", &model);
2948 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
2949 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
2950 canvas->setSource(testFileUrl("header.qml"));
2952 qApp->processEvents();
2954 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2955 QTRY_VERIFY(gridview != 0);
2956 gridview->setFlow(flow);
2957 gridview->setLayoutDirection(layoutDirection);
2958 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2960 QQuickItem *contentItem = gridview->contentItem();
2961 QTRY_VERIFY(contentItem != 0);
2963 QQuickText *header = findItem<QQuickText>(contentItem, "header");
2966 QVERIFY(header == gridview->headerItem());
2968 QCOMPARE(header->pos(), initialHeaderPos);
2969 QCOMPARE(header->width(), 100.);
2970 QCOMPARE(header->height(), 30.);
2971 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2973 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2975 QCOMPARE(item->pos(), firstDelegatePos);
2978 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2979 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
2981 for (int i = 0; i < 30; i++)
2982 model.addItem("Item" + QString::number(i), "");
2984 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
2985 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
2987 QCOMPARE(headerItemSpy.count(), 1);
2989 header = findItem<QQuickText>(contentItem, "header");
2991 header = findItem<QQuickText>(contentItem, "header2");
2994 QVERIFY(header == gridview->headerItem());
2996 QCOMPARE(header->pos(), changedHeaderPos);
2997 QCOMPARE(header->width(), 50.);
2998 QCOMPARE(header->height(), 20.);
2999 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3001 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3003 QCOMPARE(item->pos(), firstDelegatePos);
3005 header->setHeight(10);
3006 header->setWidth(40);
3007 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3012 // QTBUG-21207 header should become visible if view resizes from initial empty size
3014 canvas = createView();
3015 canvas->rootContext()->setContextProperty("testModel", &model);
3016 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3017 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3018 canvas->setSource(testFileUrl("header.qml"));
3020 qApp->processEvents();
3022 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3023 QTRY_VERIFY(gridview != 0);
3024 gridview->setFlow(flow);
3025 gridview->setLayoutDirection(layoutDirection);
3026 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3028 gridview->setWidth(240);
3029 gridview->setHeight(320);
3030 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3031 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3036 void tst_QQuickGridView::header_data()
3038 QTest::addColumn<QQuickGridView::Flow>("flow");
3039 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3040 QTest::addColumn<QPointF>("initialHeaderPos");
3041 QTest::addColumn<QPointF>("changedHeaderPos");
3042 QTest::addColumn<QPointF>("initialContentPos");
3043 QTest::addColumn<QPointF>("changedContentPos");
3044 QTest::addColumn<QPointF>("firstDelegatePos");
3045 QTest::addColumn<QPointF>("resizeContentPos");
3047 // header1 = 100 x 30
3048 // header2 = 50 x 20
3052 // header above items, top left
3053 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
3061 // header above items, top right
3062 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
3063 << QPointF(240 - 100, -30)
3064 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3070 // header to left of items
3071 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
3079 // header to right of items
3080 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
3083 << QPointF(-(240 - 100), 0)
3084 << QPointF(-(240 - 50), 0)
3086 << QPointF(-(240 - 40), 0);
3089 void tst_QQuickGridView::resizeViewAndRepaint()
3091 QQuickView *canvas = createView();
3095 for (int i = 0; i < 40; i++)
3096 model.addItem("Item" + QString::number(i), "");
3098 QQmlContext *ctxt = canvas->rootContext();
3099 ctxt->setContextProperty("testModel", &model);
3100 ctxt->setContextProperty("initialWidth", 240);
3101 ctxt->setContextProperty("initialHeight", 100);
3103 canvas->setSource(testFileUrl("resizeview.qml"));
3105 qApp->processEvents();
3107 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3108 QTRY_VERIFY(gridview != 0);
3109 QQuickItem *contentItem = gridview->contentItem();
3110 QTRY_VERIFY(contentItem != 0);
3111 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3113 // item at index 10 should not be currently visible
3114 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3116 gridview->setHeight(320);
3117 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3119 gridview->setHeight(100);
3120 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3122 // Ensure we handle -ve sizes
3123 gridview->setHeight(-100);
3124 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3126 gridview->setCacheBuffer(120);
3127 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3129 // ensure items in cache become visible
3130 gridview->setHeight(120);
3131 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3133 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3134 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3135 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3136 if (!item) qWarning() << "Item" << i << "not found";
3138 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3139 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3140 QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3143 // ensure items outside view become invisible
3144 gridview->setHeight(60);
3145 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3147 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3148 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3149 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3150 if (!item) qWarning() << "Item" << i << "not found";
3152 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3153 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3154 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3160 void tst_QQuickGridView::changeColumnCount()
3163 for (int i = 0; i < 40; i++)
3164 model.addItem("Item" + QString::number(i), "");
3166 QQuickView *canvas = createView();
3167 QQmlContext *ctxt = canvas->rootContext();
3168 ctxt->setContextProperty("testModel", &model);
3169 ctxt->setContextProperty("initialWidth", 100);
3170 ctxt->setContextProperty("initialHeight", 320);
3171 canvas->setSource(testFileUrl("resizeview.qml"));
3173 qApp->processEvents();
3175 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3176 QTRY_VERIFY(gridview != 0);
3177 QQuickItem *contentItem = gridview->contentItem();
3178 QTRY_VERIFY(contentItem != 0);
3179 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3181 // a single column of 6 items are visible
3182 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3183 QCOMPARE(itemCount, 6);
3184 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3185 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3186 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3187 QCOMPARE(item->x(), 0.0);
3188 QCOMPARE(item->y(), qreal(i*60));
3191 // now 6x3 grid is visible, plus 1 extra below for refill
3192 gridview->setWidth(240);
3193 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3194 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3195 QCOMPARE(itemCount, 6*3 + 1);
3196 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3197 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3198 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3199 QCOMPARE(item->x(), qreal((i%3)*80));
3200 QCOMPARE(item->y(), qreal((i/3)*60));
3203 // back to single column
3204 gridview->setWidth(100);
3205 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3206 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3207 QCOMPARE(itemCount, 6);
3208 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3209 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3210 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3211 QCOMPARE(item->x(), 0.0);
3212 QCOMPARE(item->y(), qreal(i*60));
3218 void tst_QQuickGridView::indexAt_itemAt_data()
3220 QTest::addColumn<qreal>("x");
3221 QTest::addColumn<qreal>("y");
3222 QTest::addColumn<int>("index");
3224 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3225 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3226 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3227 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3228 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3231 void tst_QQuickGridView::indexAt_itemAt()
3237 QQuickView *canvas = createView();
3240 model.addItem("Fred", "12345");
3241 model.addItem("John", "2345");
3242 model.addItem("Bob", "54321");
3243 model.addItem("Billy", "22345");
3244 model.addItem("Sam", "2945");
3245 model.addItem("Ben", "04321");
3246 model.addItem("Jim", "0780");
3248 QQmlContext *ctxt = canvas->rootContext();
3249 ctxt->setContextProperty("testModel", &model);
3250 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3251 ctxt->setContextProperty("testTopToBottom", QVariant(false));
3253 canvas->setSource(testFileUrl("gridview1.qml"));
3254 qApp->processEvents();
3256 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3257 QTRY_VERIFY(gridview != 0);
3259 QQuickItem *contentItem = gridview->contentItem();
3260 QTRY_VERIFY(contentItem != 0);
3262 QTRY_COMPARE(gridview->count(), model.count());
3264 QQuickItem *item = 0;
3266 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3269 QCOMPARE(gridview->indexAt(x, y), index);
3270 QVERIFY(gridview->itemAt(x, y) == item);
3275 void tst_QQuickGridView::onAdd()
3277 QFETCH(int, initialItemCount);
3278 QFETCH(int, itemsToAdd);
3280 const int delegateWidth = 50;
3281 const int delegateHeight = 100;
3283 QQuickView *canvas = createView();
3284 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3286 // these initial items should not trigger GridView.onAdd
3287 for (int i=0; i<initialItemCount; i++)
3288 model.addItem("dummy value", "dummy value");
3290 QQmlContext *ctxt = canvas->rootContext();
3291 ctxt->setContextProperty("testModel", &model);
3292 ctxt->setContextProperty("delegateWidth", delegateWidth);
3293 ctxt->setContextProperty("delegateHeight", delegateHeight);
3294 canvas->setSource(testFileUrl("attachedSignals.qml"));
3296 QObject *object = canvas->rootObject();
3297 object->setProperty("width", canvas->width());
3298 object->setProperty("height", canvas->height());
3299 qApp->processEvents();
3301 QList<QPair<QString, QString> > items;
3302 for (int i=0; i<itemsToAdd; i++)
3303 items << qMakePair(QString("value %1").arg(i), QString::number(i));
3304 model.addItems(items);
3306 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3307 qApp->processEvents();
3309 QVariantList result = object->property("addedDelegates").toList();
3310 QTRY_COMPARE(result.count(), items.count());
3311 for (int i=0; i<items.count(); i++)
3312 QCOMPARE(result[i].toString(), items[i].first);
3317 void tst_QQuickGridView::onAdd_data()
3319 QTest::addColumn<int>("initialItemCount");
3320 QTest::addColumn<int>("itemsToAdd");
3322 QTest::newRow("0, add 1") << 0 << 1;
3323 QTest::newRow("0, add 2") << 0 << 2;
3324 QTest::newRow("0, add 10") << 0 << 10;
3326 QTest::newRow("1, add 1") << 1 << 1;
3327 QTest::newRow("1, add 2") << 1 << 2;
3328 QTest::newRow("1, add 10") << 1 << 10;
3330 QTest::newRow("5, add 1") << 5 << 1;
3331 QTest::newRow("5, add 2") << 5 << 2;
3332 QTest::newRow("5, add 10") << 5 << 10;
3335 void tst_QQuickGridView::onRemove()
3337 QFETCH(int, initialItemCount);
3338 QFETCH(int, indexToRemove);
3339 QFETCH(int, removeCount);
3341 const int delegateWidth = 50;
3342 const int delegateHeight = 100;
3344 for (int i=0; i<initialItemCount; i++)
3345 model.addItem(QString("value %1").arg(i), "dummy value");
3347 QQuickView *canvas = createView();
3348 QQmlContext *ctxt = canvas->rootContext();
3349 ctxt->setContextProperty("testModel", &model);
3350 ctxt->setContextProperty("delegateWidth", delegateWidth);
3351 ctxt->setContextProperty("delegateHeight", delegateHeight);
3352 canvas->setSource(testFileUrl("attachedSignals.qml"));
3353 QObject *object = canvas->rootObject();
3355 model.removeItems(indexToRemove, removeCount);
3356 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3357 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
3362 void tst_QQuickGridView::onRemove_data()
3364 QTest::addColumn<int>("initialItemCount");
3365 QTest::addColumn<int>("indexToRemove");
3366 QTest::addColumn<int>("removeCount");
3368 QTest::newRow("remove first") << 1 << 0 << 1;
3369 QTest::newRow("two items, remove first") << 2 << 0 << 1;
3370 QTest::newRow("two items, remove last") << 2 << 1 << 1;
3371 QTest::newRow("two items, remove all") << 2 << 0 << 2;
3373 QTest::newRow("four items, remove first") << 4 << 0 << 1;
3374 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
3375 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
3376 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
3377 QTest::newRow("four items, remove last") << 4 << 3 << 1;
3378 QTest::newRow("four items, remove all") << 4 << 0 << 4;
3380 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
3381 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
3382 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
3385 void tst_QQuickGridView::columnCount()
3388 canvas.setSource(testFileUrl("gridview4.qml"));
3390 canvas.requestActivateWindow();
3391 QTest::qWaitForWindowShown(&canvas);
3393 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
3395 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
3396 QCOMPARE(view->cellHeight(), qreal(100));
3398 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
3399 QCOMPARE(items.size(), 18);
3400 QCOMPARE(items.at(8)->y(), qreal(0));
3401 QCOMPARE(items.at(9)->y(), qreal(100));
3404 void tst_QQuickGridView::margins()
3407 QQuickView *canvas = createView();
3410 for (int i = 0; i < 40; i++)
3411 model.addItem("Item" + QString::number(i), "");
3413 QQmlContext *ctxt = canvas->rootContext();
3414 ctxt->setContextProperty("testModel", &model);
3415 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3417 canvas->setSource(testFileUrl("margins.qml"));
3419 qApp->processEvents();
3421 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3422 QTRY_VERIFY(gridview != 0);
3423 QQuickItem *contentItem = gridview->contentItem();
3424 QTRY_VERIFY(contentItem != 0);
3425 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3427 QCOMPARE(gridview->contentX(), -30.);
3428 QCOMPARE(gridview->xOrigin(), 0.);
3431 gridview->positionViewAtEnd();
3432 qreal pos = gridview->contentX();
3433 gridview->setContentX(pos + 80);
3434 gridview->returnToBounds();
3435 QTRY_COMPARE(gridview->contentX(), pos + 50);
3437 // remove item before visible and check that left margin is maintained
3438 // and xOrigin is updated
3439 gridview->setContentX(200);
3440 model.removeItems(0, 4);
3442 gridview->setContentX(-50);
3443 gridview->returnToBounds();
3444 QCOMPARE(gridview->xOrigin(), 100.);
3445 QTRY_COMPARE(gridview->contentX(), 70.);
3447 // reduce left margin
3448 gridview->setLeftMargin(20);
3449 QCOMPARE(gridview->xOrigin(), 100.);
3450 QTRY_COMPARE(gridview->contentX(), 80.);
3453 gridview->positionViewAtEnd();
3454 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3455 pos = gridview->contentX();
3456 gridview->setContentX(pos + 80);
3457 gridview->returnToBounds();
3458 QTRY_COMPARE(gridview->contentX(), pos + 50);
3460 // reduce right margin
3461 pos = gridview->contentX();
3462 gridview->setRightMargin(40);
3463 QCOMPARE(gridview->xOrigin(), 0.);
3464 QTRY_COMPARE(gridview->contentX(), pos-10);
3470 QQuickView *canvas = createView();
3474 for (int i = 0; i < 40; i++)
3475 model.addItem("Item" + QString::number(i), "");
3477 QQmlContext *ctxt = canvas->rootContext();
3478 ctxt->setContextProperty("testModel", &model);
3479 ctxt->setContextProperty("testRightToLeft", QVariant(true));
3481 canvas->setSource(testFileUrl("margins.qml"));
3482 qApp->processEvents();
3484 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3485 QTRY_VERIFY(gridview != 0);
3487 QQuickItem *contentItem = gridview->contentItem();
3488 QTRY_VERIFY(contentItem != 0);
3490 QCOMPARE(gridview->contentX(), -240+30.);
3491 QCOMPARE(gridview->xOrigin(), 0.);
3494 gridview->positionViewAtEnd();
3495 qreal pos = gridview->contentX();
3496 gridview->setContentX(pos - 80);
3497 gridview->returnToBounds();
3498 QTRY_COMPARE(gridview->contentX(), pos - 50);
3500 // remove item before visible and check that left margin is maintained
3501 // and xOrigin is updated
3502 gridview->setContentX(-400);
3503 model.removeItems(0, 4);
3505 gridview->setContentX(-240+50);
3506 gridview->returnToBounds();
3507 QCOMPARE(gridview->xOrigin(), -100.);
3508 QTRY_COMPARE(gridview->contentX(), -240-70.);
3510 // reduce left margin (i.e. right side due to RTL)
3511 pos = gridview->contentX();
3512 gridview->setLeftMargin(20);
3513 QCOMPARE(gridview->xOrigin(), -100.);
3514 QTRY_COMPARE(gridview->contentX(), -240-80.);
3517 gridview->positionViewAtEnd();
3518 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3519 pos = gridview->contentX();
3520 gridview->setContentX(pos - 80);
3521 gridview->returnToBounds();
3522 QTRY_COMPARE(gridview->contentX(), pos - 50);
3524 // reduce right margin (i.e. left side due to RTL)
3525 pos = gridview->contentX();
3526 gridview->setRightMargin(40);
3527 QCOMPARE(gridview->xOrigin(), 0.);
3528 QTRY_COMPARE(gridview->contentX(), pos+10);
3534 void tst_QQuickGridView::creationContext()
3537 canvas.setGeometry(0,0,240,320);
3538 canvas.setSource(testFileUrl("creationContext.qml"));
3539 qApp->processEvents();
3541 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
3543 QVERIFY(rootItem->property("count").toInt() > 0);
3546 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
3547 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3548 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
3549 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3550 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
3551 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3554 void tst_QQuickGridView::snapToRow_data()
3556 QTest::addColumn<QQuickGridView::Flow>("flow");
3557 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3558 QTest::addColumn<int>("highlightRangeMode");
3559 QTest::addColumn<QPoint>("flickStart");
3560 QTest::addColumn<QPoint>("flickEnd");
3561 QTest::addColumn<qreal>("snapAlignment");
3562 QTest::addColumn<qreal>("endExtent");
3563 QTest::addColumn<qreal>("startExtent");
3565 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3566 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0;
3568 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3569 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1200.0 << 0.0;
3571 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3572 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 << -240.0;
3574 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3575 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0;
3577 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3578 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 1340.0 << -20.0;
3580 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3581 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -1200.0 - 240.0 - 140.0 << -220.0;
3584 void tst_QQuickGridView::snapToRow()
3586 QFETCH(QQuickGridView::Flow, flow);
3587 QFETCH(Qt::LayoutDirection, layoutDirection);
3588 QFETCH(int, highlightRangeMode);
3589 QFETCH(QPoint, flickStart);
3590 QFETCH(QPoint, flickEnd);
3591 QFETCH(qreal, snapAlignment);
3592 QFETCH(qreal, endExtent);
3593 QFETCH(qreal, startExtent);
3595 QQuickView *canvas = createView();
3597 canvas->setSource(testFileUrl("snapToRow.qml"));
3599 qApp->processEvents();
3601 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3602 QTRY_VERIFY(gridview != 0);
3604 gridview->setFlow(flow);
3605 gridview->setLayoutDirection(layoutDirection);
3606 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3607 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3609 QQuickItem *contentItem = gridview->contentItem();
3610 QTRY_VERIFY(contentItem != 0);
3612 // confirm that a flick hits an item boundary
3613 flick(canvas, flickStart, flickEnd, 180);
3614 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3615 if (flow == QQuickGridView::LeftToRight)
3616 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
3618 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
3622 flick(canvas, flickStart, flickEnd, 180);
3623 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3624 } while (flow == QQuickGridView::LeftToRight
3625 ? !gridview->isAtYEnd()
3626 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3628 if (flow == QQuickGridView::LeftToRight)
3629 QCOMPARE(gridview->contentY(), endExtent);
3631 QCOMPARE(gridview->contentX(), endExtent);
3635 flick(canvas, flickEnd, flickStart, 180);
3636 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3637 } while (flow == QQuickGridView::LeftToRight
3638 ? !gridview->isAtYBeginning()
3639 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3641 if (flow == QQuickGridView::LeftToRight)
3642 QCOMPARE(gridview->contentY(), startExtent);
3644 QCOMPARE(gridview->contentX(), startExtent);
3649 void tst_QQuickGridView::snapOneRow_data()
3651 QTest::addColumn<QQuickGridView::Flow>("flow");
3652 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3653 QTest::addColumn<int>("highlightRangeMode");
3654 QTest::addColumn<QPoint>("flickStart");
3655 QTest::addColumn<QPoint>("flickEnd");
3656 QTest::addColumn<qreal>("snapAlignment");
3657 QTest::addColumn<qreal>("endExtent");
3658 QTest::addColumn<qreal>("startExtent");
3660 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3661 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 360.0 << 0.0;
3663 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3664 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 360.0 << 0.0;
3666 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3667 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -360.0 - 240.0 << -240.0;
3669 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3670 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 460.0 << -20.0;
3672 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3673 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 460.0 << -20.0;
3675 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3676 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -360.0 - 240.0 - 100.0 << -220.0;
3679 void tst_QQuickGridView::snapOneRow()
3681 QFETCH(QQuickGridView::Flow, flow);
3682 QFETCH(Qt::LayoutDirection, layoutDirection);
3683 QFETCH(int, highlightRangeMode);
3684 QFETCH(QPoint, flickStart);
3685 QFETCH(QPoint, flickEnd);
3686 QFETCH(qreal, snapAlignment);
3687 QFETCH(qreal, endExtent);
3688 QFETCH(qreal, startExtent);
3690 QQuickView *canvas = createView();
3692 canvas->setSource(testFileUrl("snapOneRow.qml"));
3694 qApp->processEvents();
3696 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3697 QTRY_VERIFY(gridview != 0);
3699 gridview->setFlow(flow);
3700 gridview->setLayoutDirection(layoutDirection);
3701 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3702 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3704 QQuickItem *contentItem = gridview->contentItem();
3705 QTRY_VERIFY(contentItem != 0);
3707 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
3709 // confirm that a flick hits next row boundary
3710 flick(canvas, flickStart, flickEnd, 180);
3711 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3712 if (flow == QQuickGridView::LeftToRight)
3713 QCOMPARE(gridview->contentY(), snapAlignment);
3715 QCOMPARE(gridview->contentX(), snapAlignment);
3717 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3718 QCOMPARE(gridview->currentIndex(), 2);
3719 QCOMPARE(currentIndexSpy.count(), 1);
3724 flick(canvas, flickStart, flickEnd, 180);
3725 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3726 } while (flow == QQuickGridView::LeftToRight
3727 ? !gridview->isAtYEnd()
3728 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3730 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3731 QCOMPARE(gridview->currentIndex(), 8);
3732 QCOMPARE(currentIndexSpy.count(), 4);
3735 if (flow == QQuickGridView::LeftToRight)
3736 QCOMPARE(gridview->contentY(), endExtent);
3738 QCOMPARE(gridview->contentX(), endExtent);
3742 flick(canvas, flickEnd, flickStart, 180);
3743 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3744 } while (flow == QQuickGridView::LeftToRight
3745 ? !gridview->isAtYBeginning()
3746 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3748 if (flow == QQuickGridView::LeftToRight)
3749 QCOMPARE(gridview->contentY(), startExtent);
3751 QCOMPARE(gridview->contentX(), startExtent);
3753 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3754 QCOMPARE(gridview->currentIndex(), 0);
3755 QCOMPARE(currentIndexSpy.count(), 8);
3762 void tst_QQuickGridView::unaligned()
3764 QQuickView *canvas = createView();
3768 for (int i = 0; i < 10; i++)
3769 model.addItem("Item" + QString::number(i), "");
3771 QQmlContext *ctxt = canvas->rootContext();
3772 ctxt->setContextProperty("testModel", &model);
3774 canvas->setSource(testFileUrl("unaligned.qml"));
3775 qApp->processEvents();
3777 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3778 QVERIFY(gridview != 0);
3780 QQuickItem *contentItem = gridview->contentItem();
3781 QVERIFY(contentItem != 0);
3783 for (int i = 0; i < 10; ++i) {
3784 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3785 if (!item) qWarning() << "Item" << i << "not found";
3787 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3788 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3792 for (int i = 10; i < 18; ++i) {
3793 model.addItem("Item" + QString::number(i), "");
3794 QQuickItem *item = 0;
3795 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3796 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3797 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3801 for (int i = 0; i < 10; ++i) {
3802 model.insertItem(i, "Item" + QString::number(i), "");
3803 QQuickItem *item = 0;
3804 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3805 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3806 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3810 model.removeItems(7, 10);
3811 QTRY_COMPARE(model.count(), gridview->count());
3812 for (int i = 0; i < 18; ++i) {
3813 QQuickItem *item = 0;
3814 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3815 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
3816 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
3822 void tst_QQuickGridView::populateTransitions()
3824 QFETCH(bool, staticallyPopulate);
3825 QFETCH(bool, dynamicallyPopulate);
3826 QFETCH(bool, usePopulateTransition);
3828 QPointF transitionFrom(-50, -50);
3829 QPointF transitionVia(100, 100);
3830 QaimModel model_transitionFrom;
3831 QaimModel model_transitionVia;
3834 if (staticallyPopulate) {
3835 for (int i = 0; i < 30; i++)
3836 model.addItem("item" + QString::number(i), "");
3839 QQuickView *canvas = createView();
3840 canvas->rootContext()->setContextProperty("testModel", &model);
3841 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
3842 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
3843 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
3844 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
3845 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
3846 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
3847 canvas->setSource(testFileUrl("populateTransitions.qml"));
3850 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3852 QQuickItem *contentItem = gridview->contentItem();
3853 QVERIFY(contentItem);
3855 if (staticallyPopulate || dynamicallyPopulate) {
3856 // check the populate transition is run
3857 if (usePopulateTransition) {
3858 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 19);
3860 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3861 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
3863 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3865 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3868 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3869 if (usePopulateTransition)
3870 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3871 for (int i=0; i < model.count() && i < itemCount; ++i) {
3872 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3873 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3874 QCOMPARE(item->x(), (i%3)*80.0);
3875 QCOMPARE(item->y(), (i/3)*60.0);
3876 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3878 QTRY_COMPARE(name->text(), model.name(i));
3881 // add an item and check this is done with add transition, not populate
3882 model.insertItem(0, "another item", "");
3883 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
3884 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(),
3885 (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 19 : 0);
3888 canvas->rootContext()->setContextProperty("testModel", QVariant());
3889 QTRY_COMPARE(gridview->count(), 0);
3890 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
3891 gridview->setProperty("countPopulateTransitions", 0);
3892 gridview->setProperty("countAddTransitions", 0);
3894 // set to a valid model and check populate transition is run a second time
3896 for (int i = 0; i < 30; i++)
3897 model.addItem("item" + QString::number(i), "");
3898 canvas->rootContext()->setContextProperty("testModel", &model);
3899 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3901 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
3902 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3904 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3905 if (usePopulateTransition)
3906 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3907 for (int i=0; i < model.count() && i < itemCount; ++i) {
3908 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3909 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3910 QCOMPARE(item->x(), (i%3)*80.0);
3911 QCOMPARE(item->y(), (i/3)*60.0);
3912 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3914 QTRY_COMPARE(name->text(), model.name(i));
3917 // reset model and check populate transition is run again
3918 gridview->setProperty("countPopulateTransitions", 0);
3919 gridview->setProperty("countAddTransitions", 0);
3921 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
3922 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3924 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3925 if (usePopulateTransition)
3926 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3927 for (int i=0; i < model.count() && i < itemCount; ++i) {
3928 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3929 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3930 QCOMPARE(item->x(), (i%3)*80.0);
3931 QCOMPARE(item->y(), (i/3)*60.0);
3932 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3934 QTRY_COMPARE(name->text(), model.name(i));
3940 void tst_QQuickGridView::populateTransitions_data()
3942 QTest::addColumn<bool>("staticallyPopulate");
3943 QTest::addColumn<bool>("dynamicallyPopulate");
3944 QTest::addColumn<bool>("usePopulateTransition");
3946 QTest::newRow("static") << true << false << true;
3947 QTest::newRow("static, no populate") << true << false << false;
3949 QTest::newRow("dynamic") << false << true << true;
3950 QTest::newRow("dynamic, no populate") << false << true << false;
3952 QTest::newRow("empty to start with") << false << false << true;
3953 QTest::newRow("empty to start with, no populate") << false << false << false;
3956 void tst_QQuickGridView::addTransitions()
3958 QFETCH(int, initialItemCount);
3959 QFETCH(bool, shouldAnimateTargets);
3960 QFETCH(qreal, contentY);
3961 QFETCH(int, insertionIndex);
3962 QFETCH(int, insertionCount);
3963 QFETCH(ListRange, expectedDisplacedIndexes);
3965 // added items should start here
3966 QPointF targetItems_transitionFrom(-50, -50);
3968 // displaced items should pass through this point
3969 QPointF displacedItems_transitionVia(100, 100);
3972 for (int i = 0; i < initialItemCount; i++)
3973 model.addItem("Original item" + QString::number(i), "");
3974 QaimModel model_targetItems_transitionFrom;
3975 QaimModel model_displacedItems_transitionVia;
3977 QQuickView *canvas = createView();
3978 QQmlContext *ctxt = canvas->rootContext();
3979 ctxt->setContextProperty("testModel", &model);
3980 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
3981 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
3982 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
3983 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
3984 canvas->setSource(testFileUrl("addTransitions.qml"));
3987 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3988 QTRY_VERIFY(gridview != 0);
3989 QQuickItem *contentItem = gridview->contentItem();
3990 QVERIFY(contentItem != 0);
3991 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3993 if (contentY != 0) {
3994 gridview->setContentY(contentY);
3995 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3998 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4000 // only target items that will become visible should be animated
4001 QList<QPair<QString, QString> > newData;
4002 QList<QPair<QString, QString> > expectedTargetData;
4003 QList<int> targetIndexes;
4004 if (shouldAnimateTargets) {
4005 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4006 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4008 // last visible item is the first item of the row beneath the view
4009 if (i >= (contentY / 60)*3 && i < qCeil((contentY + gridview->height()) / 60.0)*3) {
4010 expectedTargetData << newData.last();
4014 QVERIFY(expectedTargetData.count() > 0);
4018 if (!newData.isEmpty()) {
4019 model.insertItems(insertionIndex, newData);
4020 QTRY_COMPARE(model.count(), gridview->count());
4023 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4025 if (shouldAnimateTargets) {
4026 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4027 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4028 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4030 // check the target and displaced items were animated
4031 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4032 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4034 // check attached properties
4035 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4036 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4037 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4038 if (expectedDisplacedIndexes.isValid()) {
4039 // adjust expectedDisplacedIndexes to their final values after the move
4040 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4041 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4042 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4043 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4046 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4047 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4050 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4051 int firstVisibleIndex = -1;
4052 for (int i=0; i<items.count(); i++) {
4053 if (items[i]->y() >= contentY) {
4054 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4055 firstVisibleIndex = e.evaluate().toInt();
4059 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4061 // verify all items moved to the correct final positions
4062 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4063 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4064 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4065 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4066 QCOMPARE(item->x(), (i%3)*80.0);
4067 QCOMPARE(item->y(), (i/3)*60.0);
4068 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4070 QCOMPARE(name->text(), model.name(i));
4076 void tst_QQuickGridView::addTransitions_data()
4078 QTest::addColumn<int>("initialItemCount");
4079 QTest::addColumn<qreal>("contentY");
4080 QTest::addColumn<bool>("shouldAnimateTargets");
4081 QTest::addColumn<int>("insertionIndex");
4082 QTest::addColumn<int>("insertionCount");
4083 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4085 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4086 QTest::newRow("insert 1, just before start")
4087 << 30 << 20.0 << false
4088 << 0 << 1 << ListRange();
4089 QTest::newRow("insert 1, way before start")
4090 << 30 << 20.0 << false
4091 << 0 << 1 << ListRange();
4092 QTest::newRow("insert multiple, just before start")
4093 << 30 << 100.0 << false
4094 << 0 << 3 << ListRange();
4095 QTest::newRow("insert multiple (< 1 row), just before start")
4096 << 30 << 100.0 << false
4097 << 0 << 2 << ListRange();
4098 QTest::newRow("insert multiple, way before start")
4099 << 30 << 100.0 << false
4100 << 0 << 3 << ListRange();
4102 QTest::newRow("insert 1 at start")
4103 << 30 << 0.0 << true
4104 << 0 << 1 << ListRange(0, 17);
4105 QTest::newRow("insert multiple at start")
4106 << 30 << 0.0 << true
4107 << 0 << 3 << ListRange(0, 17);
4108 QTest::newRow("insert multiple (> 1 row) at start")
4109 << 30 << 0.0 << true
4110 << 0 << 5 << ListRange(0, 17);
4111 QTest::newRow("insert 1 at start, content y not 0")
4112 << 30 << 60.0 << true // first visible is index 3
4113 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4114 QTest::newRow("insert multiple at start, content y not 0")
4115 << 30 << 60.0 << true // first visible is index 3
4116 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4117 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4118 << 30 << 60.0 << true // first visible is index 3
4119 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4121 QTest::newRow("insert 1 at start, to empty grid")
4123 << 0 << 1 << ListRange();
4124 QTest::newRow("insert multiple at start, to empty grid")
4126 << 0 << 3 << ListRange();
4128 QTest::newRow("insert 1 at middle")
4129 << 30 << 0.0 << true
4130 << 7 << 1 << ListRange(7, 17);
4131 QTest::newRow("insert multiple at middle")
4132 << 30 << 0.0 << true
4133 << 7 << 3 << ListRange(7, 17);
4134 QTest::newRow("insert multiple (> 1 row) at middle")
4135 << 30 << 0.0 << true
4136 << 7 << 5 << ListRange(7, 17);
4138 QTest::newRow("insert 1 at bottom")
4139 << 30 << 0.0 << true
4140 << 17 << 1 << ListRange(17, 17);
4141 QTest::newRow("insert multiple at bottom")
4142 << 30 << 0.0 << true
4143 << 17 << 3 << ListRange(17, 17);
4144 QTest::newRow("insert 1 at bottom, content y not 0")
4145 << 30 << 20.0 * 3 << true
4146 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4147 QTest::newRow("insert multiple at bottom, content y not 0")
4148 << 30 << 20.0 * 3 << true
4149 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4152 // items added after the last visible will not be animated in, since they
4153 // do not appear in the final view
4154 QTest::newRow("insert 1 after end")
4155 << 30 << 0.0 << false
4156 << 18 << 1 << ListRange();
4157 QTest::newRow("insert multiple after end")
4158 << 30 << 0.0 << false
4159 << 18 << 3 << ListRange();
4162 void tst_QQuickGridView::moveTransitions()
4164 QFETCH(int, initialItemCount);
4165 QFETCH(qreal, contentY);
4166 QFETCH(qreal, itemsOffsetAfterMove);
4167 QFETCH(int, moveFrom);
4168 QFETCH(int, moveTo);
4169 QFETCH(int, moveCount);
4170 QFETCH(ListRange, expectedDisplacedIndexes);
4172 // target and displaced items should pass through these points
4173 QPointF targetItems_transitionVia(-50, 50);
4174 QPointF displacedItems_transitionVia(100, 100);
4177 for (int i = 0; i < initialItemCount; i++)
4178 model.addItem("Original item" + QString::number(i), "");
4179 QaimModel model_targetItems_transitionVia;
4180 QaimModel model_displacedItems_transitionVia;
4182 QQuickView *canvas = createView();
4183 QQmlContext *ctxt = canvas->rootContext();
4184 ctxt->setContextProperty("testModel", &model);
4185 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4186 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4187 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4188 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4189 canvas->setSource(testFileUrl("moveTransitions.qml"));
4192 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4193 QTRY_VERIFY(gridview != 0);
4194 QQuickItem *contentItem = gridview->contentItem();
4195 QVERIFY(contentItem != 0);
4198 if (contentY != 0) {
4199 gridview->setContentY(contentY);
4200 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4203 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4205 // Items moving to *or* from visible positions should be animated.
4206 // Otherwise, they should not be animated.
4207 QList<QPair<QString, QString> > expectedTargetData;
4208 QList<int> targetIndexes;
4209 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4210 int toIndex = moveTo + (i - moveFrom);
4211 int firstVisibleIndex = (contentY / 60) * 3;
4212 int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1;
4213 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4214 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4215 expectedTargetData << qMakePair(model.name(i), model.number(i));
4219 // ViewTransition.index provides the indices that items are moving to, not from
4220 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4223 model.moveItems(moveFrom, moveTo, moveCount);
4225 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4226 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4227 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4229 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4231 // check the target and displaced items were animated
4232 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4233 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4235 // check attached properties
4236 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4237 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4238 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4239 if (expectedDisplacedIndexes.isValid()) {
4240 // adjust expectedDisplacedIndexes to their final values after the move
4241 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4242 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4243 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4244 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4247 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4248 int firstVisibleIndex = -1;
4249 for (int i=0; i<items.count(); i++) {
4250 if (items[i]->y() >= contentY) {
4251 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4252 firstVisibleIndex = e.evaluate().toInt();
4256 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4258 // verify all items moved to the correct final positions
4259 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4260 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4261 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4262 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4263 QCOMPARE(item->x(), (i%3)*80.0);
4264 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
4265 name = findItem<QQuickText>(contentItem, "textName", i);
4267 QTRY_COMPARE(name->text(), model.name(i));
4273 void tst_QQuickGridView::moveTransitions_data()
4275 QTest::addColumn<int>("initialItemCount");
4276 QTest::addColumn<qreal>("contentY");
4277 QTest::addColumn<qreal>("itemsOffsetAfterMove");
4278 QTest::addColumn<int>("moveFrom");
4279 QTest::addColumn<int>("moveTo");
4280 QTest::addColumn<int>("moveCount");
4281 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4283 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 120.0 << 0.0
4284 << 1 << 10 << 1 << ListRange(6, 10);
4285 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 120.0 << 0.0
4286 << 0 << 10 << 1 << ListRange(6, 10);
4287 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 120.0 << 60.0
4288 << 1 << 10 << 3 << ListRange(13, 23);
4289 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 120.0 << 60.0
4290 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4291 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 120.0 << 120.0
4292 << 0 << 10 << 6 << ListRange(16, 23);
4294 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
4295 << 1 << 10 << 1 << ListRange(2, 10);
4296 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
4297 << 0 << 10 << 1 << ListRange(1, 10);
4298 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 120.0 << 0.0
4299 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
4300 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
4301 << 10 << 17 << 1 << ListRange(11, 17);
4302 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
4303 << 0 << 17 << 1 << ListRange(1, 17);
4305 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
4306 << 1 << 10 << 3 << ListRange(4, 12);
4307 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
4308 << 0 << 10 << 3 << ListRange(3, 12);
4309 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 60.0 << 0.0
4310 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
4311 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
4312 << 5 << 15 << 3 << ListRange(8, 17);
4313 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
4314 << 0 << 15 << 3 << ListRange(3, 17);
4316 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
4317 << 10 << 1 << 1 << ListRange(1, 9);
4318 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
4319 << 10 << 0 << 1 << ListRange(0, 9);
4320 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 120.0 << 0.0
4321 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
4322 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 80.0 << 0.0
4323 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
4324 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
4325 << 17 << 10 << 1 << ListRange(10, 16);
4326 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
4327 << 17 << 0 << 1 << ListRange(0, 16);
4329 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
4330 << 10 << 1 << 3 << ListRange(1, 9);
4331 QTest::newRow("move within view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0
4332 << 10 << 1 << 5 << ListRange(1, 9);
4333 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
4334 << 10 << 0 << 3 << ListRange(0, 9);
4335 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 60.0 << 0.0
4336 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
4337 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border") << 30 << 80.0 << 0.0
4338 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
4339 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
4340 << 15 << 5 << 3 << ListRange(5, 14);
4341 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
4342 << 15 << 0 << 3 << ListRange(0, 14);
4344 QTest::newRow("move from below view, move 1 up") << 30 << 0.0 << 0.0
4345 << 20 << 5 << 1 << ListRange(5, 17);
4346 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
4347 << 20 << 0 << 1 << ListRange(0, 17);
4348 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 60.0 << 0.0
4349 << 25 << 3 << 1 << ListRange(0+3, 17+3);
4350 QTest::newRow("move from below view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0
4351 << 20 << 5 << 5 << ListRange(5, 17);
4352 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
4353 << 20 << 0 << 3 << ListRange(0, 17);
4354 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 60.0 << 0.0
4355 << 25 << 3 << 3 << ListRange(0+3, 17+3);
4357 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
4358 << 20 << 17 << 1 << ListRange(17, 17);
4359 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0
4360 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
4361 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
4362 << 20 << 17 << 3 << ListRange(17, 17);
4363 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0
4364 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
4367 void tst_QQuickGridView::removeTransitions()
4369 QFETCH(int, initialItemCount);
4370 QFETCH(bool, shouldAnimateTargets);
4371 QFETCH(qreal, contentY);
4372 QFETCH(int, removalIndex);
4373 QFETCH(int, removalCount);
4374 QFETCH(ListRange, expectedDisplacedIndexes);
4376 // added items should end here
4377 QPointF targetItems_transitionTo(-50, -50);
4379 // displaced items should pass through this points
4380 QPointF displacedItems_transitionVia(100, 100);
4383 for (int i = 0; i < initialItemCount; i++)
4384 model.addItem("Original item" + QString::number(i), "");
4385 QaimModel model_targetItems_transitionTo;
4386 QaimModel model_displacedItems_transitionVia;
4388 QQuickView *canvas = createView();
4389 QQmlContext *ctxt = canvas->rootContext();
4390 ctxt->setContextProperty("testModel", &model);
4391 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
4392 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4393 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
4394 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4395 canvas->setSource(testFileUrl("removeTransitions.qml"));
4398 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4399 QTRY_VERIFY(gridview != 0);
4400 QQuickItem *contentItem = gridview->contentItem();
4401 QVERIFY(contentItem != 0);
4402 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4404 if (contentY != 0) {
4405 gridview->setContentY(contentY);
4406 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4409 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4411 // only target items that are visible should be animated
4412 QList<QPair<QString, QString> > expectedTargetData;
4413 QList<int> targetIndexes;
4414 if (shouldAnimateTargets) {
4415 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
4416 int firstVisibleIndex = (contentY / 60.0)*3;
4417 int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1;
4418 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
4419 expectedTargetData << qMakePair(model.name(i), model.number(i));
4423 QVERIFY(expectedTargetData.count() > 0);
4426 // calculate targetItems and expectedTargets before model changes
4427 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4428 QVariantMap expectedTargets;
4429 for (int i=0; i<targetIndexes.count(); i++)
4430 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
4433 model.removeItems(removalIndex, removalCount);
4434 QTRY_COMPARE(model.count(), gridview->count());
4436 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
4437 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4438 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4439 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4441 // check the target and displaced items were animated
4442 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
4443 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4445 // check attached properties
4446 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
4447 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4448 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4449 if (expectedDisplacedIndexes.isValid()) {
4450 // adjust expectedDisplacedIndexes to their final values after the move
4451 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
4452 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4453 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4454 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4457 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
4458 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4461 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4462 int itemCount = items.count();
4463 int firstVisibleIndex = -1;
4464 for (int i=0; i<items.count(); i++) {
4465 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4466 int index = e.evaluate().toInt();
4467 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
4468 firstVisibleIndex = index;
4470 itemCount--; // exclude deleted items
4472 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4474 // verify all items moved to the correct final positions
4475 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4476 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4477 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4478 QCOMPARE(item->x(), (i%3)*80.0);
4479 QCOMPARE(item->y(), contentY + ((i-firstVisibleIndex)/3) * 60.0);
4480 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4482 QTRY_COMPARE(name->text(), model.name(i));
4488 void tst_QQuickGridView::removeTransitions_data()
4490 QTest::addColumn<int>("initialItemCount");
4491 QTest::addColumn<qreal>("contentY");
4492 QTest::addColumn<bool>("shouldAnimateTargets");
4493 QTest::addColumn<int>("removalIndex");
4494 QTest::addColumn<int>("removalCount");
4495 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4497 // All items that are visible following the remove operation should be animated.
4498 // Remove targets that are outside of the view should not be animated.
4500 // For a GridView, removing any number of items other than a full row before the start
4501 // should displace all items in the view
4502 QTest::newRow("remove 1 before start")
4503 << 30 << 120.0 << false
4504 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
4505 QTest::newRow("remove 1 row, before start")
4506 << 30 << 120.0 << false
4507 << 3 << 3 << ListRange();
4508 QTest::newRow("remove between 1-2 rows, before start")
4509 << 30 << 120.0 << false
4510 << 0 << 5 << ListRange(6, 25);
4511 QTest::newRow("remove 2 rows, before start")
4512 << 30 << 120.0 << false
4513 << 0 << 6 << ListRange();
4514 QTest::newRow("remove mix of before and after start")
4515 << 30 << 60.0 << true
4516 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
4519 QTest::newRow("remove 1 from start")
4520 << 30 << 0.0 << true
4521 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
4522 QTest::newRow("remove multiple from start")
4523 << 30 << 0.0 << true
4524 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
4525 QTest::newRow("remove 1 from start, content y not 0")
4526 << 30 << 60.0 << true
4527 << 3 << 1 << ListRange(1 + 3, 18 + 3);
4528 QTest::newRow("remove multiple from start, content y not 0")
4529 << 30 << 60.0 << true
4530 << 3 << 3 << ListRange(3 + 3, 20 + 3);
4533 QTest::newRow("remove 1 from middle")
4534 << 30 << 0.0 << true
4535 << 5 << 1 << ListRange(6, 18);
4536 QTest::newRow("remove multiple from middle")
4537 << 30 << 0.0 << true
4538 << 5 << 3 << ListRange(8, 20);
4541 QTest::newRow("remove 1 from bottom")
4542 << 30 << 0.0 << true
4543 << 17 << 1 << ListRange(18, 18);
4544 QTest::newRow("remove multiple (1 row) from bottom")
4545 << 30 << 0.0 << true
4546 << 15 << 3 << ListRange(18, 20);
4547 QTest::newRow("remove multiple (> 1 row) from bottom")
4548 << 30 << 0.0 << true
4549 << 15 << 5 << ListRange(20, 22);
4550 QTest::newRow("remove 1 from bottom, content y not 0")
4551 << 30 << 60.0 << true
4552 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
4553 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
4554 << 30 << 60.0 << true
4555 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
4558 QTest::newRow("remove 1 after end")
4559 << 30 << 0.0 << false
4560 << 18 << 1 << ListRange();
4561 QTest::newRow("remove multiple after end")
4562 << 30 << 0.0 << false
4563 << 18 << 3 << ListRange();
4566 void tst_QQuickGridView::displacedTransitions()
4568 QFETCH(bool, useDisplaced);
4569 QFETCH(bool, displacedEnabled);
4570 QFETCH(bool, useAddDisplaced);
4571 QFETCH(bool, addDisplacedEnabled);
4572 QFETCH(bool, useMoveDisplaced);
4573 QFETCH(bool, moveDisplacedEnabled);
4574 QFETCH(bool, useRemoveDisplaced);
4575 QFETCH(bool, removeDisplacedEnabled);
4576 QFETCH(ListChange, change);
4577 QFETCH(ListRange, expectedDisplacedIndexes);
4580 for (int i = 0; i < 30; i++)
4581 model.addItem("Original item" + QString::number(i), "");
4582 QaimModel model_displaced_transitionVia;
4583 QaimModel model_addDisplaced_transitionVia;
4584 QaimModel model_moveDisplaced_transitionVia;
4585 QaimModel model_removeDisplaced_transitionVia;
4587 QPointF displaced_transitionVia(-50, -100);
4588 QPointF addDisplaced_transitionVia(-150, 100);
4589 QPointF moveDisplaced_transitionVia(50, -100);
4590 QPointF removeDisplaced_transitionVia(150, 100);
4592 QQuickView *canvas = createView();
4593 QQmlContext *ctxt = canvas->rootContext();
4594 ctxt->setContextProperty("testModel", &model);
4595 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
4596 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
4597 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
4598 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
4599 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
4600 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
4601 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
4602 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
4603 ctxt->setContextProperty("useDisplaced", useDisplaced);
4604 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
4605 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
4606 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
4607 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
4608 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
4609 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
4610 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
4611 canvas->setSource(testFileUrl("displacedTransitions.qml"));
4613 qApp->processEvents();
4615 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4616 QTRY_VERIFY(gridview != 0);
4617 QQuickItem *contentItem = gridview->contentItem();
4618 QVERIFY(contentItem != 0);
4619 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4621 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4622 gridview->setProperty("displaceTransitionsDone", false);
4624 switch (change.type) {
4625 case ListChange::Inserted:
4627 QList<QPair<QString, QString> > targetItemData;
4628 for (int i=change.index; i<change.index + change.count; ++i)
4629 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
4630 model.insertItems(change.index, targetItemData);
4631 QTRY_COMPARE(model.count(), gridview->count());
4634 case ListChange::Removed:
4635 model.removeItems(change.index, change.count);
4636 QTRY_COMPARE(model.count(), gridview->count());
4638 case ListChange::Moved:
4639 model.moveItems(change.index, change.to, change.count);
4640 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4642 case ListChange::SetCurrent:
4643 case ListChange::SetContentY:
4647 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
4648 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
4650 if ((useDisplaced && displacedEnabled)
4651 || (useAddDisplaced && addDisplacedEnabled)
4652 || (useMoveDisplaced && moveDisplacedEnabled)
4653 || (useRemoveDisplaced && removeDisplacedEnabled)) {
4654 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
4656 // check the correct number of target items and indexes were received
4657 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
4658 for (int i=0; i<resultTargetIndexes.count(); i++)
4659 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
4660 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
4661 for (int i=0; i<resultTargetItems.count(); i++)
4662 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
4664 QCOMPARE(resultTargetIndexes.count(), 0);
4665 QCOMPARE(resultTargetItems.count(), 0);
4668 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
4669 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
4671 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
4672 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
4673 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
4675 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
4676 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
4677 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
4679 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
4681 if (useDisplaced && displacedEnabled
4682 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
4683 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
4684 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
4685 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
4687 QCOMPARE(model_displaced_transitionVia.count(), 0);
4690 // verify all items moved to the correct final positions
4691 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4692 for (int i=0; i < model.count() && i < items.count(); ++i) {
4693 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4694 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4695 QCOMPARE(item->x(), (i%3)*80.0);
4696 QCOMPARE(item->y(), (i/3)*60.0);
4697 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4699 QTRY_COMPARE(name->text(), model.name(i));
4705 void tst_QQuickGridView::displacedTransitions_data()
4707 QTest::addColumn<bool>("useDisplaced");
4708 QTest::addColumn<bool>("displacedEnabled");
4709 QTest::addColumn<bool>("useAddDisplaced");
4710 QTest::addColumn<bool>("addDisplacedEnabled");
4711 QTest::addColumn<bool>("useMoveDisplaced");
4712 QTest::addColumn<bool>("moveDisplacedEnabled");
4713 QTest::addColumn<bool>("useRemoveDisplaced");
4714 QTest::addColumn<bool>("removeDisplacedEnabled");
4715 QTest::addColumn<ListChange>("change");
4716 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4718 QTest::newRow("no displaced transitions at all")
4723 << ListChange::insert(0, 1) << ListRange(0, 17);
4725 QTest::newRow("just displaced")
4730 << ListChange::insert(0, 1) << ListRange(0, 17);
4732 QTest::newRow("just displaced (not enabled)")
4737 << ListChange::insert(0, 1) << ListRange(0, 17);
4739 QTest::newRow("displaced + addDisplaced")
4744 << ListChange::insert(0, 1) << ListRange(0, 17);
4746 QTest::newRow("displaced + addDisplaced (not enabled)")
4751 << ListChange::insert(0, 1) << ListRange(0, 17);
4753 QTest::newRow("displaced + moveDisplaced")
4758 << ListChange::move(0, 10, 1) << ListRange(1, 10);
4760 QTest::newRow("displaced + moveDisplaced (not enabled)")
4765 << ListChange::move(0, 10, 1) << ListRange(1, 10);
4767 QTest::newRow("displaced + removeDisplaced")
4772 << ListChange::remove(0, 1) << ListRange(1, 18);
4774 QTest::newRow("displaced + removeDisplaced (not enabled)")
4779 << ListChange::remove(0, 1) << ListRange(1, 18);
4782 QTest::newRow("displaced + add, should use generic displaced for a remove")
4787 << ListChange::remove(0, 1) << ListRange(1, 18);
4790 void tst_QQuickGridView::multipleTransitions()
4792 // Tests that if you interrupt a transition in progress with another action that
4793 // cancels the previous transition, the resulting items are still placed correctly.
4795 QFETCH(int, initialCount);
4796 QFETCH(qreal, contentY);
4797 QFETCH(QList<ListChange>, changes);
4798 QFETCH(bool, enableAddTransitions);
4799 QFETCH(bool, enableMoveTransitions);
4800 QFETCH(bool, enableRemoveTransitions);
4801 QFETCH(bool, rippleAddDisplaced);
4803 // add transitions on the left, moves on the right
4804 QPointF addTargets_transitionFrom(-50, -50);
4805 QPointF addDisplaced_transitionFrom(-50, 50);
4806 QPointF moveTargets_transitionFrom(50, -50);
4807 QPointF moveDisplaced_transitionFrom(50, 50);
4808 QPointF removeTargets_transitionTo(-100, 300);
4809 QPointF removeDisplaced_transitionFrom(100, 300);
4812 for (int i = 0; i < initialCount; i++)
4813 model.addItem("Original item" + QString::number(i), "");
4815 QQuickView *canvas = createView();
4816 QQmlContext *ctxt = canvas->rootContext();
4817 ctxt->setContextProperty("testModel", &model);
4818 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
4819 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
4820 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
4821 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
4822 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
4823 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
4824 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
4825 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
4826 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
4827 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
4828 canvas->setSource(testFileUrl("multipleTransitions.qml"));
4830 QTest::qWaitForWindowShown(canvas);
4832 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4833 QTRY_VERIFY(gridview != 0);
4834 QQuickItem *contentItem = gridview->contentItem();
4835 QVERIFY(contentItem != 0);
4836 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4838 if (contentY != 0) {
4839 gridview->setContentY(contentY);
4840 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4843 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
4845 for (int i=0; i<changes.count(); i++) {
4846 switch (changes[i].type) {
4847 case ListChange::Inserted:
4849 QList<QPair<QString, QString> > targetItems;
4850 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
4851 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
4852 model.insertItems(changes[i].index, targetItems);
4853 QTRY_COMPARE(model.count(), gridview->count());
4854 if (i == changes.count() - 1) {
4855 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
4856 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
4858 QTest::qWait(timeBetweenActions);
4862 case ListChange::Removed:
4863 model.removeItems(changes[i].index, changes[i].count);
4864 QTRY_COMPARE(model.count(), gridview->count());
4865 if (i == changes.count() - 1) {
4866 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
4867 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
4869 QTest::qWait(timeBetweenActions);
4872 case ListChange::Moved:
4873 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
4874 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4875 if (i == changes.count() - 1) {
4876 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
4877 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
4879 QTest::qWait(timeBetweenActions);
4882 case ListChange::SetCurrent:
4883 gridview->setCurrentIndex(changes[i].index);
4884 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4886 case ListChange::SetContentY:
4887 gridview->setContentY(changes[i].pos);
4888 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4892 QCOMPARE(gridview->count(), model.count());
4894 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4895 int firstVisibleIndex = -1;
4896 for (int i=0; i<items.count(); i++) {
4897 if (items[i]->y() >= contentY) {
4898 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4899 firstVisibleIndex = e.evaluate().toInt();
4903 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4905 // verify all items moved to the correct final positions
4906 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4907 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4908 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4909 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4910 QTRY_COMPARE(item->x(), (i%3)*80.0);
4911 QTRY_COMPARE(item->y(), (i/3)*60.0);
4912 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4914 QTRY_COMPARE(name->text(), model.name(i));
4920 void tst_QQuickGridView::multipleTransitions_data()
4922 QTest::addColumn<int>("initialCount");
4923 QTest::addColumn<qreal>("contentY");
4924 QTest::addColumn<QList<ListChange> >("changes");
4925 QTest::addColumn<bool>("enableAddTransitions");
4926 QTest::addColumn<bool>("enableMoveTransitions");
4927 QTest::addColumn<bool>("enableRemoveTransitions");
4928 QTest::addColumn<bool>("rippleAddDisplaced");
4930 // the added item and displaced items should move to final dest correctly
4931 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
4932 << ListChange::insert(0, 1)
4933 << ListChange::move(0, 3, 1)
4935 << true << true << true << false;
4937 // items affected by the add should change from move to add transition
4938 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
4939 << ListChange::move(1, 10, 3)
4940 << ListChange::insert(0, 1)
4942 << true << true << true << false;
4944 // items should be placed correctly if you trigger a transition then refill for that index
4945 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
4946 << ListChange::insert(0, 1)
4947 << ListChange::setContentY(160.0)
4948 << ListChange::setContentY(0.0)
4949 << ListChange::insert(0, 1)
4951 << true << true << true << false;
4953 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
4954 << ListChange::insert(1, 1)
4955 << ListChange::remove(1, 1)
4957 << true << true << true << true;
4959 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
4960 // even if a remove-displace transition is not present to re-animate them
4961 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
4962 << ListChange::insert(0, 1)
4963 << ListChange::remove(2, 1)
4965 << true << true << false << false;
4967 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
4968 // remove has changed the position of where it will move to
4969 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
4970 << ListChange::setContentY(-10.0)
4971 << ListChange::insert(0, 1)
4972 << ListChange::insert(0, 1)
4973 << ListChange::remove(2, 1)
4975 << true << true << false << false;
4978 void tst_QQuickGridView::cacheBuffer()
4980 QQuickView *canvas = createView();
4983 for (int i = 0; i < 90; i++)
4984 model.addItem("Item" + QString::number(i), "");
4986 QQmlContext *ctxt = canvas->rootContext();
4987 ctxt->setContextProperty("testModel", &model);
4988 ctxt->setContextProperty("testRightToLeft", QVariant(false));
4989 ctxt->setContextProperty("testTopToBottom", QVariant(false));
4991 canvas->setSource(testFileUrl("gridview1.qml"));
4993 qApp->processEvents();
4995 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4996 QVERIFY(gridview != 0);
4998 QQuickItem *contentItem = gridview->contentItem();
4999 QVERIFY(contentItem != 0);
5000 QVERIFY(gridview->delegate() != 0);
5001 QVERIFY(gridview->model() != 0);
5003 // Confirm items positioned correctly
5004 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5005 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5006 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 QQmlIncubationController controller;
5012 canvas->engine()->setIncubationController(&controller);
5014 canvas->rootObject()->setProperty("cacheBuffer", 200);
5015 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5017 // items will be created one at a time
5018 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5019 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5020 QQuickItem *item = 0;
5023 controller.incubateWhile(&b);
5024 item = findItem<QQuickItem>(gridview, "wrapper", i);
5030 controller.incubateWhile(&b);
5033 int newItemCount = 0;
5034 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5036 // Confirm items positioned correctly
5037 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5038 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5040 QTRY_COMPARE(item->x(), (i%3)*80.0);
5041 QTRY_COMPARE(item->y(), (i/3)*60.0);
5044 // move view and confirm items in view are visible immediately and outside are created async
5045 gridview->setContentY(300);
5047 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5048 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5050 QTRY_COMPARE(item->x(), (i%3)*80.0);
5051 QTRY_COMPARE(item->y(), (i/3)*60.0);
5054 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5056 // ensure buffered items are created
5057 for (int i = 34; i < qMin(44,model.count()); ++i) {
5058 QQuickItem *item = 0;
5060 qGuiApp->processEvents(); // allow refill to happen
5062 controller.incubateWhile(&b);
5063 item = findItem<QQuickItem>(gridview, "wrapper", i);
5069 controller.incubateWhile(&b);
5075 void tst_QQuickGridView::asynchronous()
5077 QQuickView *canvas = createView();
5079 QQmlIncubationController controller;
5080 canvas->engine()->setIncubationController(&controller);
5082 canvas->setSource(testFile("asyncloader.qml"));
5084 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5085 QVERIFY(rootObject);
5087 QQuickGridView *gridview = 0;
5090 controller.incubateWhile(&b);
5091 gridview = rootObject->findChild<QQuickGridView*>("view");
5094 // items will be created one at a time
5095 for (int i = 0; i < 12; ++i) {
5096 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5097 QQuickItem *item = 0;
5100 controller.incubateWhile(&b);
5101 item = findItem<QQuickItem>(gridview, "wrapper", i);
5107 controller.incubateWhile(&b);
5110 // verify positioning
5111 QQuickItem *contentItem = gridview->contentItem();
5112 for (int i = 0; i < 12; ++i) {
5113 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5114 if (!item) qWarning() << "Item" << i << "not found";
5115 QVERIFY(item->x() == (i%3)*100);
5116 QVERIFY(item->y() == (i/3)*100);
5122 void tst_QQuickGridView::unrequestedVisibility()
5125 for (int i = 0; i < 30; i++)
5126 model.addItem("Item" + QString::number(i), QString::number(i));
5128 QQuickView *canvas = new QQuickView(0);
5129 canvas->setGeometry(0,0,240,320);
5131 QQmlContext *ctxt = canvas->rootContext();
5132 ctxt->setContextProperty("testModel", &model);
5133 ctxt->setContextProperty("testWrap", QVariant(false));
5135 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5139 qApp->processEvents();
5141 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5142 QTRY_VERIFY(leftview != 0);
5144 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5145 QTRY_VERIFY(rightview != 0);
5147 QQuickItem *leftContent = leftview->contentItem();
5148 QTRY_VERIFY(leftContent != 0);
5150 QQuickItem *rightContent = rightview->contentItem();
5151 QTRY_VERIFY(rightContent != 0);
5153 rightview->setCurrentIndex(12);
5155 QTRY_COMPARE(leftview->contentY(), 0.0);
5156 QTRY_COMPARE(rightview->contentY(), 240.0);
5160 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5161 QCOMPARE(item->isVisible(), true);
5162 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5163 QCOMPARE(item->isVisible(), false);
5165 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5166 QCOMPARE(item->isVisible(), false);
5167 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5168 QCOMPARE(item->isVisible(), true);
5170 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5171 QCOMPARE(item->isVisible(), true);
5172 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5173 QCOMPARE(item->isVisible(), false);
5174 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5175 QCOMPARE(item->isVisible(), false);
5176 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5177 QCOMPARE(item->isVisible(), true);
5179 rightview->setCurrentIndex(0);
5181 QTRY_COMPARE(leftview->contentY(), 0.0);
5182 QTRY_COMPARE(rightview->contentY(), 0.0);
5184 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5185 QCOMPARE(item->isVisible(), true);
5186 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5187 QTRY_COMPARE(item->isVisible(), true);
5189 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5190 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5192 leftview->setCurrentIndex(12);
5194 QTRY_COMPARE(leftview->contentY(), 240.0);
5195 QTRY_COMPARE(rightview->contentY(), 0.0);
5197 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5198 QTRY_COMPARE(item->isVisible(), false);
5199 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5200 QCOMPARE(item->isVisible(), true);
5202 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5203 QCOMPARE(item->isVisible(), true);
5204 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5205 QCOMPARE(item->isVisible(), false);
5207 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5208 QCOMPARE(item->isVisible(), false);
5209 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5210 QCOMPARE(item->isVisible(), true);
5211 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5212 QCOMPARE(item->isVisible(), true);
5213 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5214 QCOMPARE(item->isVisible(), false);
5216 // move a non-visible item into view
5217 model.moveItems(10, 9, 1);
5218 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5220 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5221 QCOMPARE(item->isVisible(), false);
5222 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5223 QCOMPARE(item->isVisible(), true);
5225 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5226 QCOMPARE(item->isVisible(), true);
5227 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5228 QCOMPARE(item->isVisible(), false);
5230 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5231 QCOMPARE(item->isVisible(), false);
5232 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5233 QCOMPARE(item->isVisible(), true);
5234 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5235 QCOMPARE(item->isVisible(), true);
5236 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5237 QCOMPARE(item->isVisible(), false);
5239 // move a visible item out of view
5240 model.moveItems(5, 3, 1);
5241 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5243 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5244 QCOMPARE(item->isVisible(), false);
5245 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5246 QCOMPARE(item->isVisible(), true);
5247 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5248 QCOMPARE(item->isVisible(), true);
5249 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5250 QCOMPARE(item->isVisible(), false);
5252 // move a non-visible item into view
5253 model.moveItems(3, 5, 1);
5254 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5256 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5257 QCOMPARE(item->isVisible(), false);
5258 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5259 QCOMPARE(item->isVisible(), true);
5260 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5261 QCOMPARE(item->isVisible(), true);
5262 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5263 QCOMPARE(item->isVisible(), false);
5265 // move a visible item out of view
5266 model.moveItems(9, 10, 1);
5267 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5269 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5270 QCOMPARE(item->isVisible(), false);
5271 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5272 QCOMPARE(item->isVisible(), true);
5273 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5274 QCOMPARE(item->isVisible(), true);
5275 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5276 QCOMPARE(item->isVisible(), false);
5278 // move a non-visible item into view
5279 model.moveItems(10, 9, 1);
5280 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5282 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5283 QCOMPARE(item->isVisible(), false);
5284 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5285 QCOMPARE(item->isVisible(), true);
5286 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5287 QCOMPARE(item->isVisible(), true);
5288 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5289 QCOMPARE(item->isVisible(), false);
5294 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
5298 for (int i=0; i<list.count(); i++) {
5299 ret << list[i].toInt(&ok);
5301 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
5307 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
5309 for (int i=0; i<indexLists.count(); i++) {
5310 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
5311 if (current != expectedIndexes.toSet())
5312 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
5313 QCOMPARE(current, expectedIndexes.toSet());
5317 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
5319 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
5320 QVERIFY(it.value().type() == QVariant::Int);
5321 QString name = it.key();
5322 int itemIndex = it.value().toInt();
5323 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
5324 if (model.name(itemIndex) != name)
5325 qDebug() << itemIndex;
5326 QCOMPARE(model.name(itemIndex), name);
5328 QCOMPARE(items.count(), expectedIndexes.count());
5331 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
5333 for (int i=0; i<itemLists.count(); i++) {
5334 QVariantList current = itemLists[i].toList();
5335 for (int j=0; j<current.count(); j++) {
5336 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
5337 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
5338 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
5340 QCOMPARE(current.count(), expectedItems.count());
5344 QTEST_MAIN(tst_QQuickGridView)
5346 #include "tst_qquickgridview.moc"