1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtCore/qstringlistmodel.h>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcomponent.h>
47 #include <QtQml/qqmlcontext.h>
48 #include <QtQml/qqmlexpression.h>
49 #include <QtQml/qqmlincubator.h>
50 #include <QtQuick/private/qquickitem_p.h>
51 #include <QtQuick/private/qquickgridview_p.h>
52 #include <QtQuick/private/qquicktext_p.h>
53 #include <QtQuick/private/qquickvisualitemmodel_p.h>
54 #include <QtQml/private/qquicklistmodel_p.h>
55 #include <QtQml/private/qlistmodelinterface_p.h>
56 #include "../../shared/util.h"
57 #include "../shared/viewtestutil.h"
58 #include "../shared/visualtestutil.h"
59 #include <QtGui/qguiapplication.h>
61 Q_DECLARE_METATYPE(Qt::LayoutDirection)
62 Q_DECLARE_METATYPE(QQuickGridView::Flow)
64 using namespace QQuickViewTestUtil;
65 using namespace QQuickVisualTestUtil;
69 class tst_QQuickGridView : public QQmlDataTest
81 void inserted_more_data();
82 void insertBeforeVisible();
83 void insertBeforeVisible_data();
86 void removed_more_data();
87 void addOrRemoveBeforeVisible();
88 void addOrRemoveBeforeVisible_data();
92 void multipleChanges();
93 void multipleChanges_data();
94 void swapWithFirstItem();
97 void noCurrentIndex();
100 void propertyChanges();
101 void componentChanges();
103 void positionViewAtIndex();
104 void positionViewAtIndex_rightToLeft();
109 void enforceRange_rightToLeft();
111 void manualHighlight();
116 void resizeViewAndRepaint();
117 void changeColumnCount();
118 void indexAt_itemAt_data();
119 void indexAt_itemAt();
123 void onRemove_data();
126 void creationContext();
127 void snapToRow_data();
129 void snapOneRow_data();
134 void unrequestedVisibility();
136 void populateTransitions();
137 void populateTransitions_data();
138 void addTransitions();
139 void addTransitions_data();
140 void moveTransitions();
141 void moveTransitions_data();
142 void removeTransitions();
143 void removeTransitions_data();
144 void displacedTransitions();
145 void displacedTransitions_data();
146 void multipleTransitions();
147 void multipleTransitions_data();
148 void multipleDisplaced();
151 QList<int> toIntList(const QVariantList &list);
152 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
153 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
154 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
157 QQuickView *getView() {
159 if (QString(QTest::currentTestFunction()) != testForView) {
163 m_view->setSource(QUrl());
168 testForView = QTest::currentTestFunction();
169 m_view = createView();
172 void releaseView(QQuickView *view) {
173 Q_ASSERT(view == m_view);
174 m_view->setSource(QUrl());
177 QQuickView *getView() {
180 void releaseView(QQuickView *view) {
189 tst_QQuickGridView::tst_QQuickGridView() : m_view(0)
193 void tst_QQuickGridView::init()
196 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
197 testForView = QString();
204 void tst_QQuickGridView::items()
206 QQuickView *canvas = createView();
209 model.addItem("Fred", "12345");
210 model.addItem("John", "2345");
211 model.addItem("Bob", "54321");
212 model.addItem("Billy", "22345");
213 model.addItem("Sam", "2945");
214 model.addItem("Ben", "04321");
215 model.addItem("Jim", "0780");
217 QQmlContext *ctxt = canvas->rootContext();
218 ctxt->setContextProperty("testModel", &model);
219 ctxt->setContextProperty("testRightToLeft", QVariant(false));
220 ctxt->setContextProperty("testTopToBottom", QVariant(false));
222 canvas->setSource(testFileUrl("gridview1.qml"));
223 qApp->processEvents();
225 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
226 QTRY_VERIFY(gridview != 0);
228 QQuickItem *contentItem = gridview->contentItem();
229 QTRY_VERIFY(contentItem != 0);
231 QTRY_COMPARE(gridview->count(), model.count());
232 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
233 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
235 for (int i = 0; i < model.count(); ++i) {
236 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
237 QTRY_VERIFY(name != 0);
238 QTRY_COMPARE(name->text(), model.name(i));
239 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
240 QTRY_VERIFY(number != 0);
241 QTRY_COMPARE(number->text(), model.number(i));
244 // set an empty model and confirm that items are destroyed
246 ctxt->setContextProperty("testModel", &model2);
248 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
249 QTRY_VERIFY(itemCount == 0);
254 void tst_QQuickGridView::changed()
256 QQuickView *canvas = createView();
259 model.addItem("Fred", "12345");
260 model.addItem("John", "2345");
261 model.addItem("Bob", "54321");
262 model.addItem("Billy", "22345");
263 model.addItem("Sam", "2945");
264 model.addItem("Ben", "04321");
265 model.addItem("Jim", "0780");
267 QQmlContext *ctxt = canvas->rootContext();
268 ctxt->setContextProperty("testModel", &model);
269 ctxt->setContextProperty("testRightToLeft", QVariant(false));
270 ctxt->setContextProperty("testTopToBottom", QVariant(false));
272 canvas->setSource(testFileUrl("gridview1.qml"));
273 qApp->processEvents();
275 QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
276 QTRY_VERIFY(gridview != 0);
278 QQuickItem *contentItem = gridview->contentItem();
279 QTRY_VERIFY(contentItem != 0);
281 model.modifyItem(1, "Will", "9876");
282 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
283 QTRY_VERIFY(name != 0);
284 QTRY_COMPARE(name->text(), model.name(1));
285 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
286 QTRY_VERIFY(number != 0);
287 QTRY_COMPARE(number->text(), model.number(1));
292 void tst_QQuickGridView::inserted()
294 QQuickView *canvas = createView();
298 model.addItem("Fred", "12345");
299 model.addItem("John", "2345");
300 model.addItem("Bob", "54321");
302 QQmlContext *ctxt = canvas->rootContext();
303 ctxt->setContextProperty("testModel", &model);
304 ctxt->setContextProperty("testRightToLeft", QVariant(false));
305 ctxt->setContextProperty("testTopToBottom", QVariant(false));
307 canvas->setSource(testFileUrl("gridview1.qml"));
308 qApp->processEvents();
310 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
311 QTRY_VERIFY(gridview != 0);
313 QQuickItem *contentItem = gridview->contentItem();
314 QTRY_VERIFY(contentItem != 0);
316 model.insertItem(1, "Will", "9876");
318 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
319 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
321 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
322 QTRY_VERIFY(name != 0);
323 QTRY_COMPARE(name->text(), model.name(1));
324 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
325 QTRY_VERIFY(number != 0);
326 QTRY_COMPARE(number->text(), model.number(1));
328 // Checks that onAdd is called
329 int added = canvas->rootObject()->property("added").toInt();
330 QTRY_COMPARE(added, 1);
332 // Confirm items positioned correctly
333 for (int i = 0; i < model.count(); ++i) {
334 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
335 QTRY_COMPARE(item->x(), (i%3)*80.0);
336 QTRY_COMPARE(item->y(), (i/3)*60.0);
339 model.insertItem(0, "Foo", "1111"); // zero index, and current item
341 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
343 name = findItem<QQuickText>(contentItem, "textName", 0);
344 QTRY_VERIFY(name != 0);
345 QTRY_COMPARE(name->text(), model.name(0));
346 number = findItem<QQuickText>(contentItem, "textNumber", 0);
347 QTRY_VERIFY(number != 0);
348 QTRY_COMPARE(number->text(), model.number(0));
350 QTRY_COMPARE(gridview->currentIndex(), 1);
352 // Confirm items positioned correctly
353 for (int i = 0; i < model.count(); ++i) {
354 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
355 QTRY_VERIFY(item->x() == (i%3)*80);
356 QTRY_VERIFY(item->y() == (i/3)*60);
359 for (int i = model.count(); i < 30; ++i)
360 model.insertItem(i, "Hello", QString::number(i));
362 gridview->setContentY(120);
364 // Insert item outside visible area
365 model.insertItem(1, "Hello", "1324");
367 QTRY_VERIFY(gridview->contentY() == 120);
372 void tst_QQuickGridView::inserted_more()
374 QFETCH(qreal, contentY);
375 QFETCH(int, insertIndex);
376 QFETCH(int, insertCount);
377 QFETCH(qreal, itemsOffsetAfterMove);
380 for (int i = 0; i < 30; i++)
381 model.addItem("Item" + QString::number(i), "");
383 QQuickView *canvas = getView();
384 QQmlContext *ctxt = canvas->rootContext();
385 ctxt->setContextProperty("testModel", &model);
386 ctxt->setContextProperty("testRightToLeft", QVariant(false));
387 ctxt->setContextProperty("testTopToBottom", QVariant(false));
389 canvas->setSource(testFileUrl("gridview1.qml"));
391 qApp->processEvents();
393 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
394 QTRY_VERIFY(gridview != 0);
395 QQuickItem *contentItem = gridview->contentItem();
396 QTRY_VERIFY(contentItem != 0);
398 gridview->setContentY(contentY);
399 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
401 QList<QPair<QString, QString> > newData;
402 for (int i=0; i<insertCount; i++)
403 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
404 model.insertItems(insertIndex, newData);
405 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
407 // check visibleItems.first() is in correct position
408 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
410 QCOMPARE(item0->y(), 0.0);
412 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
413 int firstVisibleIndex = -1;
414 for (int i=0; i<items.count(); i++) {
415 if (items[i]->y() >= contentY) {
416 QQmlExpression e(qmlContext(items[i]), items[i], "index");
417 firstVisibleIndex = e.evaluate().toInt();
421 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
423 // Confirm items positioned correctly and indexes correct
424 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
427 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
428 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
429 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
431 QCOMPARE(item->x(), (i%3)*80.0);
432 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
434 name = findItem<QQuickText>(contentItem, "textName", i);
436 QCOMPARE(name->text(), model.name(i));
437 number = findItem<QQuickText>(contentItem, "textNumber", i);
438 QVERIFY(number != 0);
439 QCOMPARE(number->text(), model.number(i));
445 void tst_QQuickGridView::inserted_more_data()
447 QTest::addColumn<qreal>("contentY");
448 QTest::addColumn<int>("insertIndex");
449 QTest::addColumn<int>("insertCount");
450 QTest::addColumn<qreal>("itemsOffsetAfterMove");
452 QTest::newRow("add 1, before visible items")
453 << 120.0 // show 6-23
455 << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
456 // new 1st visible item is at 0
458 QTest::newRow("add 2, before visible items")
459 << 120.0 // show 6-23
461 << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
463 QTest::newRow("add 3, before visible items")
464 << 120.0 // show 6-23
466 << -60.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
468 QTest::newRow("add 5, before visible items")
469 << 120.0 // show 6-23
471 << -60.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
472 // grid is rearranged and first visible moves forward within its row
474 QTest::newRow("add 6, before visible items")
475 << 120.0 // show 6-23
477 << -60.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
481 QTest::newRow("add 1, at start of visible, content at start")
486 QTest::newRow("add multiple, at start of visible, content at start")
491 QTest::newRow("add 1, at start of visible, content not at start")
492 << 120.0 // show 6-23
496 QTest::newRow("add multiple, at start of visible, content not at start")
497 << 120.0 // show 6-23
502 QTest::newRow("add 1, at end of visible, content at start")
507 QTest::newRow("add 1, at end of visible, content at start")
512 QTest::newRow("add 1, at end of visible, content not at start")
513 << 120.0 // show 6-23
517 QTest::newRow("add multiple, at end of visible, content not at start")
518 << 120.0 // show 6-23
523 QTest::newRow("add 1, after visible, content at start")
528 QTest::newRow("add 1, after visible, content at start")
533 QTest::newRow("add 1, after visible, content not at start")
534 << 120.0 // show 6-23
538 QTest::newRow("add multiple, after visible, content not at start")
539 << 120.0 // show 6-23
544 void tst_QQuickGridView::insertBeforeVisible()
546 QFETCH(int, insertIndex);
547 QFETCH(int, insertCount);
548 QFETCH(int, cacheBuffer);
551 QQuickView *canvas = getView();
554 for (int i = 0; i < 30; i++)
555 model.addItem("Item" + QString::number(i), "");
557 QQmlContext *ctxt = canvas->rootContext();
558 ctxt->setContextProperty("testModel", &model);
559 ctxt->setContextProperty("testRightToLeft", QVariant(false));
560 ctxt->setContextProperty("testTopToBottom", QVariant(false));
561 canvas->setSource(testFileUrl("gridview1.qml"));
563 qApp->processEvents();
565 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
566 QTRY_VERIFY(gridview != 0);
567 QQuickItem *contentItem = gridview->contentItem();
568 QTRY_VERIFY(contentItem != 0);
570 gridview->setCacheBuffer(cacheBuffer);
571 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
573 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
574 int firstVisibleIndex = 12; // move to an index where the top item is not visible
575 gridview->setContentY(firstVisibleIndex/3 * 60.0);
576 gridview->setCurrentIndex(firstVisibleIndex);
577 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
579 QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
580 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
582 QCOMPARE(item->y(), gridview->contentY());
584 QList<QPair<QString, QString> > newData;
585 for (int i=0; i<insertCount; i++)
586 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
587 model.insertItems(insertIndex, newData);
588 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
590 // now, moving to the top of the view should position the inserted items correctly
591 int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
592 gridview->setCurrentIndex(0);
593 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
594 QTRY_COMPARE(gridview->currentIndex(), 0);
595 QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
597 // Confirm items positioned correctly and indexes correct
598 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
599 for (int i = 0; i < model.count() && i < itemCount; ++i) {
600 item = findItem<QQuickItem>(contentItem, "wrapper", i);
601 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
602 QCOMPARE(item->x(), (i%3)*80.0);
603 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
604 name = findItem<QQuickText>(contentItem, "textName", i);
606 QTRY_COMPARE(name->text(), model.name(i));
612 void tst_QQuickGridView::insertBeforeVisible_data()
614 QTest::addColumn<int>("insertIndex");
615 QTest::addColumn<int>("insertCount");
616 QTest::addColumn<int>("cacheBuffer");
618 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
619 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
620 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
622 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
623 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
624 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
626 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
627 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
628 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
630 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
631 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
632 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
635 void tst_QQuickGridView::removed()
637 QQuickView *canvas = createView();
641 for (int i = 0; i < 40; i++)
642 model.addItem("Item" + QString::number(i), "");
644 QQmlContext *ctxt = canvas->rootContext();
645 ctxt->setContextProperty("testModel", &model);
646 ctxt->setContextProperty("testRightToLeft", QVariant(false));
647 ctxt->setContextProperty("testTopToBottom", QVariant(false));
649 canvas->setSource(testFileUrl("gridview1.qml"));
650 qApp->processEvents();
652 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
653 QTRY_VERIFY(gridview != 0);
654 QQuickItem *contentItem = gridview->contentItem();
655 QTRY_VERIFY(contentItem != 0);
656 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
659 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
661 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
662 QTRY_VERIFY(name != 0);
663 QTRY_COMPARE(name->text(), model.name(1));
664 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
665 QTRY_VERIFY(number != 0);
666 QTRY_COMPARE(number->text(), model.number(1));
669 // Checks that onRemove is called
670 QString removed = canvas->rootObject()->property("removed").toString();
671 QTRY_COMPARE(removed, QString("Item1"));
673 // Confirm items positioned correctly
674 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
675 for (int i = 0; i < model.count() && i < itemCount; ++i) {
676 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
677 if (!item) qWarning() << "Item" << i << "not found";
678 QTRY_VERIFY(item->x() == (i%3)*80);
679 QTRY_VERIFY(item->y() == (i/3)*60);
682 // Remove first item (which is the current item);
684 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
686 name = findItem<QQuickText>(contentItem, "textName", 0);
687 QTRY_VERIFY(name != 0);
688 QTRY_COMPARE(name->text(), model.name(0));
689 number = findItem<QQuickText>(contentItem, "textNumber", 0);
690 QTRY_VERIFY(number != 0);
691 QTRY_COMPARE(number->text(), model.number(0));
694 // Confirm items positioned correctly
695 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
696 for (int i = 0; i < model.count() && i < itemCount; ++i) {
697 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
698 if (!item) qWarning() << "Item" << i << "not found";
699 QTRY_VERIFY(item->x() == (i%3)*80);
700 QTRY_VERIFY(item->y() == (i/3)*60);
703 // Remove items not visible
704 model.removeItem(25);
705 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
707 // Confirm items positioned correctly
708 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
709 for (int i = 0; i < model.count() && i < itemCount; ++i) {
710 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
711 if (!item) qWarning() << "Item" << i << "not found";
712 QTRY_VERIFY(item->x() == (i%3)*80);
713 QTRY_VERIFY(item->y() == (i/3)*60);
716 // Remove items before visible
717 gridview->setContentY(120);
718 gridview->setCurrentIndex(10);
720 // Setting currentIndex above shouldn't cause view to scroll
721 QTRY_COMPARE(gridview->contentY(), 120.0);
724 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
726 // Confirm items positioned correctly
727 for (int i = 6; i < 18; ++i) {
728 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
729 if (!item) qWarning() << "Item" << i << "not found";
730 QTRY_VERIFY(item->x() == (i%3)*80);
731 QTRY_VERIFY(item->y() == (i/3)*60);
734 // Remove currentIndex
735 QQuickItem *oldCurrent = gridview->currentItem();
737 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
739 QTRY_COMPARE(gridview->currentIndex(), 9);
740 QTRY_VERIFY(gridview->currentItem() != oldCurrent);
742 gridview->setContentY(0);
743 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
745 // Confirm items positioned correctly
746 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
747 for (int i = 0; i < model.count() && i < itemCount; ++i) {
748 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
749 QTRY_VERIFY(item->x() == (i%3)*80);
750 QTRY_VERIFY(item->y() == (i/3)*60);
753 // remove item outside current view.
754 gridview->setCurrentIndex(32);
755 gridview->setContentY(240);
757 model.removeItem(30);
758 QTRY_VERIFY(gridview->currentIndex() == 31);
760 // remove current item beyond visible items.
761 gridview->setCurrentIndex(20);
762 gridview->setContentY(0);
763 model.removeItem(20);
765 QTRY_COMPARE(gridview->currentIndex(), 20);
766 QTRY_VERIFY(gridview->currentItem() != 0);
768 // remove item before current, but visible
769 gridview->setCurrentIndex(8);
770 gridview->setContentY(240);
771 oldCurrent = gridview->currentItem();
774 QTRY_COMPARE(gridview->currentIndex(), 7);
775 QTRY_VERIFY(gridview->currentItem() == oldCurrent);
780 void tst_QQuickGridView::removed_more()
782 QFETCH(qreal, contentY);
783 QFETCH(int, removeIndex);
784 QFETCH(int, removeCount);
785 QFETCH(qreal, itemsOffsetAfterMove);
786 QFETCH(QString, firstVisible);
790 QQuickView *canvas = getView();
793 for (int i = 0; i < 30; i++)
794 model.addItem("Item" + QString::number(i), "");
796 QQmlContext *ctxt = canvas->rootContext();
797 ctxt->setContextProperty("testModel", &model);
798 ctxt->setContextProperty("testRightToLeft", QVariant(false));
799 ctxt->setContextProperty("testTopToBottom", QVariant(false));
800 canvas->setSource(testFileUrl("gridview1.qml"));
802 qApp->processEvents();
804 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
805 QTRY_VERIFY(gridview != 0);
806 QQuickItem *contentItem = gridview->contentItem();
807 QTRY_VERIFY(contentItem != 0);
809 gridview->setContentY(contentY);
810 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
812 model.removeItems(removeIndex, removeCount);
813 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
816 int firstVisibleIndex = -1;
817 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
818 for (int i=0; i<items.count(); i++) {
819 if (items[i]->y() >= contentY) {
820 QQmlExpression e(qmlContext(items[i]), items[i], "index");
821 firstVisibleIndex = e.evaluate().toInt();
822 QQmlExpression en(qmlContext(items[i]), items[i], "name");
823 firstName = en.evaluate().toString();
827 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
828 QCOMPARE(firstName, firstVisible);
830 // Confirm items positioned correctly and indexes correct
831 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
832 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
833 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
834 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
836 QTRY_COMPARE(item->x(), (i%3)*80.0);
837 QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
839 name = findItem<QQuickText>(contentItem, "textName", i);
841 QTRY_COMPARE(name->text(), model.name(i));
842 number = findItem<QQuickText>(contentItem, "textNumber", i);
843 QVERIFY(number != 0);
844 QTRY_COMPARE(number->text(), model.number(i));
850 void tst_QQuickGridView::removed_more_data()
852 QTest::addColumn<qreal>("contentY");
853 QTest::addColumn<int>("removeIndex");
854 QTest::addColumn<int>("removeCount");
855 QTest::addColumn<qreal>("itemsOffsetAfterMove");
856 QTest::addColumn<QString>("firstVisible");
858 QTest::newRow("remove 1, before visible items")
859 << 120.0 // show 6-23
863 QTest::newRow("remove 1, before visible position")
864 << 120.0 // show 6-23
868 QTest::newRow("remove multiple, all before visible items")
871 << 60.0 << "Item6"; // removed top row, slide down by 1 row
873 QTest::newRow("remove multiple, all before visible items, remove item 0")
876 << 60.0 << "Item7"; // removed top row, slide down by 1 row
878 QTest::newRow("remove multiple rows, all before visible items")
879 << 240.0 // show 12-29
881 << 120.0 << "Item13";
883 QTest::newRow("remove one row before visible, content y not on item border")
886 << 60.0 << "Item6"; // 1 row removed
888 QTest::newRow("remove mix of visible/non-visible")
889 << 120.0 // show 6-23
891 << 60.0 << "Item6"; // 1 row removed
894 // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
895 // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
896 QTest::newRow("remove multiple, mix of items from before and within visible items")
899 << 60.0 << "Item8"; // adjust for the 1 row removed before the visible
901 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
904 << 60.0 * 2 << "Item8"; // adjust for the 2 rows removed before the visible
907 QTest::newRow("remove 1, from start of visible, content at start")
912 QTest::newRow("remove multiple, from start of visible, content at start")
917 QTest::newRow("remove 1, from start of visible, content not at start")
918 << 120.0 // show 6-23
922 QTest::newRow("remove multiple, from start of visible, content not at start")
923 << 120.0 // show 6-23
928 QTest::newRow("remove 1, from middle of visible, content at start")
933 QTest::newRow("remove multiple, from middle of visible, content at start")
938 QTest::newRow("remove 1, from middle of visible, content not at start")
939 << 120.0 // show 6-23
943 QTest::newRow("remove multiple, from middle of visible, content not at start")
944 << 120.0 // show 6-23
949 QTest::newRow("remove 1, after visible, content at start")
954 QTest::newRow("remove multiple, after visible, content at start")
959 QTest::newRow("remove 1, after visible, content not at start")
960 << 120.0 // show 6-23
964 QTest::newRow("remove multiple, after visible, content not at start")
965 << 120.0 // show 6-23
969 QTest::newRow("remove multiple, mix of items from within and after visible items")
970 << 120.0 // show 6-23
975 void tst_QQuickGridView::addOrRemoveBeforeVisible()
977 // QTBUG-21588: ensure re-layout is done on grid after adding or removing
978 // items from before the visible area
981 QFETCH(qreal, newTopContentY);
983 QQuickView *canvas = getView();
987 for (int i = 0; i < 30; i++)
988 model.addItem("Item" + QString::number(i), "");
990 QQmlContext *ctxt = canvas->rootContext();
991 ctxt->setContextProperty("testModel", &model);
992 ctxt->setContextProperty("testRightToLeft", QVariant(false));
993 ctxt->setContextProperty("testTopToBottom", QVariant(false));
994 canvas->setSource(testFileUrl("gridview1.qml"));
996 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
997 QTRY_VERIFY(gridview != 0);
998 QQuickItem *contentItem = gridview->contentItem();
999 QTRY_VERIFY(contentItem != 0);
1001 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1002 QTRY_COMPARE(name->text(), QString("Item0"));
1004 gridview->setCurrentIndex(0);
1005 qApp->processEvents();
1006 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1008 // scroll down until item 0 is no longer drawn
1009 // (bug not triggered if we just move using content y, since that doesn't
1010 // refill and change the visible items)
1011 gridview->setCurrentIndex(24);
1012 qApp->processEvents();
1014 QTRY_COMPARE(gridview->currentIndex(), 24);
1015 QTRY_COMPARE(gridview->contentY(), 220.0);
1017 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1018 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible
1021 model.insertItem(0, "New Item", "New Item number");
1022 QTRY_COMPARE(gridview->count(), 31);
1024 model.removeItem(0);
1025 QTRY_COMPARE(gridview->count(), 29);
1028 // scroll back up and item 0 should be gone
1029 gridview->setCurrentIndex(0);
1030 qApp->processEvents();
1031 QTRY_COMPARE(gridview->currentIndex(), 0);
1032 QTRY_COMPARE(gridview->contentY(), newTopContentY);
1034 name = findItem<QQuickText>(contentItem, "textName", 0);
1036 QCOMPARE(name->text(), QString("New Item"));
1038 QCOMPARE(name->text(), QString("Item1"));
1040 // Confirm items positioned correctly
1041 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1042 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1043 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
1044 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1045 QTRY_VERIFY(item->x() == (i%3)*80);
1046 QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1049 releaseView(canvas);
1052 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1054 QTest::addColumn<bool>("doAdd");
1055 QTest::addColumn<qreal>("newTopContentY");
1057 QTest::newRow("add") << true << -60.0;
1058 QTest::newRow("remove") << false << -60.0;
1061 void tst_QQuickGridView::clear()
1063 QQuickView *canvas = createView();
1066 for (int i = 0; i < 30; i++)
1067 model.addItem("Item" + QString::number(i), "");
1069 QQmlContext *ctxt = canvas->rootContext();
1070 ctxt->setContextProperty("testModel", &model);
1071 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1072 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1074 canvas->setSource(testFileUrl("gridview1.qml"));
1076 qApp->processEvents();
1078 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1079 QVERIFY(gridview != 0);
1080 QQuickItem *contentItem = gridview->contentItem();
1081 QVERIFY(contentItem != 0);
1082 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1086 QVERIFY(gridview->count() == 0);
1087 QVERIFY(gridview->currentItem() == 0);
1088 QVERIFY(gridview->contentY() == 0);
1089 QVERIFY(gridview->currentIndex() == -1);
1091 // confirm sanity when adding an item to cleared list
1092 model.addItem("New", "1");
1093 QTRY_COMPARE(gridview->count(), 1);
1094 QVERIFY(gridview->currentItem() != 0);
1095 QVERIFY(gridview->currentIndex() == 0);
1100 void tst_QQuickGridView::moved()
1102 QFETCH(qreal, contentY);
1106 QFETCH(qreal, itemsOffsetAfterMove);
1110 QQuickView *canvas = getView();
1113 for (int i = 0; i < 30; i++)
1114 model.addItem("Item" + QString::number(i), "");
1116 QQmlContext *ctxt = canvas->rootContext();
1117 ctxt->setContextProperty("testModel", &model);
1118 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1119 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1121 canvas->setSource(testFileUrl("gridview1.qml"));
1123 qApp->processEvents();
1125 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1126 QTRY_VERIFY(gridview != 0);
1127 QQuickItem *contentItem = gridview->contentItem();
1128 QTRY_VERIFY(contentItem != 0);
1129 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1131 QQuickItem *currentItem = gridview->currentItem();
1132 QTRY_VERIFY(currentItem != 0);
1134 if (contentY != 0) {
1135 gridview->setContentY(contentY);
1136 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1139 model.moveItems(from, to, count);
1140 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1142 // Confirm items positioned correctly and indexes correct
1143 int firstVisibleIndex = qCeil(contentY / 60.0) * 3;
1144 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1145 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
1146 if (i >= firstVisibleIndex + 18) // index has moved out of view
1148 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1149 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1151 QTRY_COMPARE(item->x(), (i%3)*80.0);
1152 QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
1154 name = findItem<QQuickText>(contentItem, "textName", i);
1156 QTRY_COMPARE(name->text(), model.name(i));
1157 number = findItem<QQuickText>(contentItem, "textNumber", i);
1158 QVERIFY(number != 0);
1159 QTRY_COMPARE(number->text(), model.number(i));
1161 // current index should have been updated
1162 if (item == currentItem)
1163 QTRY_COMPARE(gridview->currentIndex(), i);
1166 releaseView(canvas);
1169 void tst_QQuickGridView::moved_data()
1171 QTest::addColumn<qreal>("contentY");
1172 QTest::addColumn<int>("from");
1173 QTest::addColumn<int>("to");
1174 QTest::addColumn<int>("count");
1175 QTest::addColumn<qreal>("itemsOffsetAfterMove");
1177 // model starts with 30 items, each 80x60, in area 240x320
1178 // 18 items should be visible at a time
1180 // The first visible item should not move upwards and out of the view
1181 // if items are moved/removed before it.
1184 QTest::newRow("move 1 forwards, within visible items")
1189 QTest::newRow("move 1 forwards, from non-visible -> visible")
1190 << 120.0 // show 6-23
1194 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1195 << 120.0 // // show 6-23
1199 QTest::newRow("move 1 forwards, from visible -> non-visible")
1204 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1210 QTest::newRow("move 1 backwards, within visible items")
1215 QTest::newRow("move 1 backwards, within visible items (to first index)")
1220 QTest::newRow("move 1 backwards, from non-visible -> visible")
1225 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1230 QTest::newRow("move 1 backwards, from visible -> non-visible")
1231 << 120.0 // show 6-23
1233 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1235 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1236 << 120.0 // show 6-23
1238 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1241 QTest::newRow("move multiple forwards, within visible items")
1246 QTest::newRow("move multiple backwards, within visible items (move first item)")
1251 QTest::newRow("move multiple forwards, before visible items")
1252 << 120.0 // show 6-23
1253 << 3 << 4 << 3 // 3, 4, 5 move to after 6
1254 << 60.0; // row of 3,4,5 has moved down
1256 QTest::newRow("move multiple forwards, from non-visible -> visible")
1257 << 120.0 // show 6-23
1259 << 60.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1261 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1262 << 120.0 // show 6-23
1264 << 60.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1266 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1269 << 60.0; // top two rows removed, third row is now the first visible
1271 QTest::newRow("move multiple forwards, to bottom of view")
1276 QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1281 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1286 QTest::newRow("move multiple forwards, from visible -> non-visible")
1291 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1297 QTest::newRow("move multiple backwards, within visible items")
1302 QTest::newRow("move multiple backwards, from non-visible -> visible")
1307 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1312 QTest::newRow("move multiple backwards, from visible -> non-visible")
1313 << 120.0 // show 6-23
1315 << -60.0; // to minimize movement, items are added above visible area, all items move up by 1 row
1317 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1318 << 120.0 // show 6-23
1320 << -60.0; // 16,17,18 move to above item 0, all items move up by 1 row
1323 void tst_QQuickGridView::multipleChanges()
1325 QFETCH(int, startCount);
1326 QFETCH(QList<ListChange>, changes);
1327 QFETCH(int, newCount);
1328 QFETCH(int, newCurrentIndex);
1330 QQuickView *canvas = getView();
1333 for (int i = 0; i < startCount; i++)
1334 model.addItem("Item" + QString::number(i), "");
1336 QQmlContext *ctxt = canvas->rootContext();
1337 ctxt->setContextProperty("testModel", &model);
1338 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1339 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1341 canvas->setSource(testFileUrl("gridview1.qml"));
1343 qApp->processEvents();
1345 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1346 QTRY_VERIFY(gridview != 0);
1347 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1349 for (int i=0; i<changes.count(); i++) {
1350 switch (changes[i].type) {
1351 case ListChange::Inserted:
1353 QList<QPair<QString, QString> > items;
1354 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1355 items << qMakePair(QString("new item " + j), QString::number(j));
1356 model.insertItems(changes[i].index, items);
1359 case ListChange::Removed:
1360 model.removeItems(changes[i].index, changes[i].count);
1361 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1363 case ListChange::Moved:
1364 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1365 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1367 case ListChange::SetCurrent:
1368 gridview->setCurrentIndex(changes[i].index);
1369 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1371 case ListChange::SetContentY:
1372 gridview->setContentY(changes[i].pos);
1373 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1378 QTRY_COMPARE(gridview->count(), newCount);
1379 QCOMPARE(gridview->count(), model.count());
1380 QTRY_COMPARE(gridview->currentIndex(), newCurrentIndex);
1384 QQuickItem *contentItem = gridview->contentItem();
1385 QTRY_VERIFY(contentItem != 0);
1386 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1387 for (int i=0; i < model.count() && i < itemCount; ++i) {
1388 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1389 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1390 name = findItem<QQuickText>(contentItem, "textName", i);
1392 QTRY_COMPARE(name->text(), model.name(i));
1393 number = findItem<QQuickText>(contentItem, "textNumber", i);
1394 QVERIFY(number != 0);
1395 QTRY_COMPARE(number->text(), model.number(i));
1398 releaseView(canvas);
1401 void tst_QQuickGridView::multipleChanges_data()
1403 QTest::addColumn<int>("startCount");
1404 QTest::addColumn<QList<ListChange> >("changes");
1405 QTest::addColumn<int>("newCount");
1406 QTest::addColumn<int>("newCurrentIndex");
1408 QList<ListChange> changes;
1410 for (int i=1; i<30; i++)
1411 changes << ListChange::remove(0);
1412 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1414 changes << ListChange::remove(0);
1415 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1418 changes << ListChange::setCurrent(29);
1419 for (int i=29; i>0; i--)
1420 changes << ListChange::remove(i);
1421 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1423 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1424 << ListChange::remove(0, 1)
1425 << ListChange::insert(0, 1)
1428 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1429 << ListChange::setCurrent(2)
1430 << ListChange::remove(2, 1)
1431 << ListChange::insert(2, 1)
1434 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1435 << ListChange::setCurrent(1)
1436 << ListChange::remove(1, 3)
1437 << ListChange::insert(2, 2)
1440 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1441 << ListChange::setCurrent(2)
1442 << ListChange::remove(1, 3)
1443 << ListChange::move(1, 5, 1)
1446 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1447 << ListChange::setCurrent(5)
1448 << ListChange::remove(4, 3)
1449 << ListChange::move(4, 1, 1)
1453 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1454 << ListChange::insert(0, 2)
1455 << ListChange::insert(0, 4)
1456 << ListChange::insert(0, 6)
1459 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1460 << ListChange::insert(0, 2)
1461 << ListChange::insert(0, 4)
1462 << ListChange::insert(0, 6)
1463 << ListChange::setCurrent(3)
1464 << ListChange::insert(3, 2)
1467 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1468 << ListChange::insert(0, 30)
1469 << ListChange::remove(0, 30)
1472 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1473 << ListChange::insert(1)
1474 << ListChange::setCurrent(1)
1475 << ListChange::remove(1)
1478 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1479 << ListChange::insert(0, 10)
1480 << ListChange::remove(5, 10)
1483 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1484 << ListChange::insert(0, 3)
1485 << ListChange::move(0, 10, 3)
1488 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1489 << ListChange::insert(0, 3)
1490 << ListChange::move(0, 8, 5)
1493 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1494 << ListChange::setCurrent(9)
1495 << ListChange::insert(10, 3)
1496 << ListChange::move(8, 0, 5)
1500 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1501 << ListChange::setCurrent(1)
1502 << ListChange::move(1, 2, 2)
1503 << ListChange::move(2, 1, 2)
1506 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1507 << ListChange::setCurrent(2)
1508 << ListChange::move(1, 2, 3)
1509 << ListChange::move(3, 0, 5)
1512 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1513 << ListChange::setCurrent(5)
1514 << ListChange::move(5, 0, 1)
1515 << ListChange::remove(0)
1518 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1519 << ListChange::setCurrent(5)
1520 << ListChange::move(5, 0, 1)
1521 << ListChange::insert(0)
1524 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1525 << ListChange::setCurrent(1)
1526 << ListChange::move(5, 1, 3)
1527 << ListChange::remove(1, 3)
1530 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1531 << ListChange::setCurrent(5)
1532 << ListChange::move(5, 1, 3)
1533 << ListChange::insert(1, 5)
1536 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1537 << ListChange::setCurrent(3)
1538 << ListChange::move(0, 1, 2)
1539 << ListChange::insert(3, 5)
1543 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1544 << ListChange::insert(0, 5)
1545 << ListChange::setCurrent(-1)
1546 << ListChange::remove(0, 5)
1547 << ListChange::insert(0, 5)
1552 void tst_QQuickGridView::swapWithFirstItem()
1555 QQuickView *canvas = createView();
1558 for (int i = 0; i < 30; i++)
1559 model.addItem("Item" + QString::number(i), "");
1561 QQmlContext *ctxt = canvas->rootContext();
1562 ctxt->setContextProperty("testModel", &model);
1563 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1564 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1566 canvas->setSource(testFileUrl("gridview1.qml"));
1568 qApp->processEvents();
1570 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1571 QTRY_VERIFY(gridview != 0);
1573 // ensure content position is stable
1574 gridview->setContentY(0);
1575 model.moveItem(10, 0);
1576 QTRY_VERIFY(gridview->contentY() == 0);
1581 void tst_QQuickGridView::currentIndex()
1584 for (int i = 0; i < 60; i++)
1585 model.addItem("Item" + QString::number(i), QString::number(i));
1587 QQuickView *canvas = new QQuickView(0);
1588 canvas->setGeometry(0,0,240,320);
1591 QQmlContext *ctxt = canvas->rootContext();
1592 ctxt->setContextProperty("testModel", &model);
1594 QString filename(testFile("gridview-initCurrent.qml"));
1595 canvas->setSource(QUrl::fromLocalFile(filename));
1597 qApp->processEvents();
1599 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1600 QVERIFY(gridview != 0);
1601 QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1603 QQuickItem *contentItem = gridview->contentItem();
1604 QVERIFY(contentItem != 0);
1606 // current item should be third item
1607 QCOMPARE(gridview->currentIndex(), 35);
1608 QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1609 QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1610 QCOMPARE(gridview->contentY(), 400.0);
1612 gridview->moveCurrentIndexRight();
1613 QCOMPARE(gridview->currentIndex(), 36);
1614 gridview->moveCurrentIndexDown();
1615 QCOMPARE(gridview->currentIndex(), 39);
1616 gridview->moveCurrentIndexUp();
1617 QCOMPARE(gridview->currentIndex(), 36);
1618 gridview->moveCurrentIndexLeft();
1619 QCOMPARE(gridview->currentIndex(), 35);
1621 // wait until motion stops
1622 QTRY_VERIFY(gridview->verticalVelocity() == 0.0);
1625 gridview->setCurrentIndex(0);
1626 QCOMPARE(gridview->currentIndex(), 0);
1627 // confirm that the velocity is updated
1628 QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1630 gridview->moveCurrentIndexUp();
1631 QCOMPARE(gridview->currentIndex(), 0);
1633 gridview->moveCurrentIndexLeft();
1634 QCOMPARE(gridview->currentIndex(), 0);
1636 gridview->setCurrentIndex(model.count()-1);
1637 QCOMPARE(gridview->currentIndex(), model.count()-1);
1639 gridview->moveCurrentIndexRight();
1640 QCOMPARE(gridview->currentIndex(), model.count()-1);
1642 gridview->moveCurrentIndexDown();
1643 QCOMPARE(gridview->currentIndex(), model.count()-1);
1646 gridview->setWrapEnabled(true);
1648 gridview->setCurrentIndex(0);
1649 QCOMPARE(gridview->currentIndex(), 0);
1651 gridview->moveCurrentIndexLeft();
1652 QCOMPARE(gridview->currentIndex(), model.count()-1);
1654 qApp->processEvents();
1655 QTRY_COMPARE(gridview->contentY(), 880.0);
1657 gridview->moveCurrentIndexRight();
1658 QCOMPARE(gridview->currentIndex(), 0);
1660 QTRY_COMPARE(gridview->contentY(), 0.0);
1663 // footer should become visible if it is out of view, and then current index moves to the first row
1664 canvas->rootObject()->setProperty("showFooter", true);
1665 QTRY_VERIFY(gridview->footerItem());
1666 gridview->setCurrentIndex(model.count()-3);
1667 QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1668 gridview->setCurrentIndex(model.count()-2);
1669 QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1670 canvas->rootObject()->setProperty("showFooter", false);
1672 // header should become visible if it is out of view, and then current index moves to the last row
1673 canvas->rootObject()->setProperty("showHeader", true);
1674 QTRY_VERIFY(gridview->headerItem());
1675 gridview->setCurrentIndex(3);
1676 QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1677 gridview->setCurrentIndex(1);
1678 QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1679 canvas->rootObject()->setProperty("showHeader", false);
1683 canvas->requestActivateWindow();
1684 QTest::qWaitForWindowShown(canvas);
1685 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1687 gridview->setCurrentIndex(0);
1689 QTest::keyClick(canvas, Qt::Key_Down);
1690 QCOMPARE(gridview->currentIndex(), 3);
1692 QTest::keyClick(canvas, Qt::Key_Up);
1693 QCOMPARE(gridview->currentIndex(), 0);
1695 // hold down Key_Down
1696 for (int i=0; i<(model.count() / 3) - 1; i++) {
1697 QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
1698 QTRY_COMPARE(gridview->currentIndex(), i*3 + 3);
1700 QTest::keyRelease(canvas, Qt::Key_Down);
1701 QTRY_COMPARE(gridview->currentIndex(), 57);
1702 QTRY_COMPARE(gridview->contentY(), 880.0);
1705 for (int i=(model.count() / 3) - 1; i > 0; i--) {
1706 QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
1707 QTRY_COMPARE(gridview->currentIndex(), i*3 - 3);
1709 QTest::keyRelease(canvas, Qt::Key_Up);
1710 QTRY_COMPARE(gridview->currentIndex(), 0);
1711 QTRY_COMPARE(gridview->contentY(), 0.0);
1714 gridview->setFlow(QQuickGridView::TopToBottom);
1716 canvas->requestActivateWindow();
1717 QTest::qWaitForWindowShown(canvas);
1718 QVERIFY(qGuiApp->focusWindow() == canvas);
1719 qApp->processEvents();
1721 QTest::keyClick(canvas, Qt::Key_Right);
1722 QCOMPARE(gridview->currentIndex(), 5);
1724 QTest::keyClick(canvas, Qt::Key_Left);
1725 QCOMPARE(gridview->currentIndex(), 0);
1727 QTest::keyClick(canvas, Qt::Key_Down);
1728 QCOMPARE(gridview->currentIndex(), 1);
1730 QTest::keyClick(canvas, Qt::Key_Up);
1731 QCOMPARE(gridview->currentIndex(), 0);
1733 // hold down Key_Right
1734 for (int i=0; i<(model.count() / 5) - 1; i++) {
1735 QTest::simulateEvent(canvas, true, Qt::Key_Right, Qt::NoModifier, "", true);
1736 QTRY_COMPARE(gridview->currentIndex(), i*5 + 5);
1739 QTest::keyRelease(canvas, Qt::Key_Right);
1740 QTRY_COMPARE(gridview->currentIndex(), 55);
1741 QTRY_COMPARE(gridview->contentX(), 720.0);
1743 // hold down Key_Left
1744 for (int i=(model.count() / 5) - 1; i > 0; i--) {
1745 QTest::simulateEvent(canvas, true, Qt::Key_Left, Qt::NoModifier, "", true);
1746 QTRY_COMPARE(gridview->currentIndex(), i*5 - 5);
1748 QTest::keyRelease(canvas, Qt::Key_Left);
1749 QTRY_COMPARE(gridview->currentIndex(), 0);
1750 QTRY_COMPARE(gridview->contentX(), 0.0);
1753 // turn off auto highlight
1754 gridview->setHighlightFollowsCurrentItem(false);
1755 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1756 QVERIFY(gridview->highlightItem());
1757 qreal hlPosX = gridview->highlightItem()->x();
1758 qreal 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 gridview->setHighlightFollowsCurrentItem(true);
1778 gridview->setFlow(QQuickGridView::LeftToRight);
1779 gridview->setLayoutDirection(Qt::RightToLeft);
1781 canvas->requestActivateWindow();
1782 QTest::qWaitForWindowShown(canvas);
1783 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1784 qApp->processEvents();
1786 gridview->setCurrentIndex(35);
1788 QTest::keyClick(canvas, Qt::Key_Right);
1789 QCOMPARE(gridview->currentIndex(), 34);
1791 QTest::keyClick(canvas, Qt::Key_Down);
1792 QCOMPARE(gridview->currentIndex(), 37);
1794 QTest::keyClick(canvas, Qt::Key_Up);
1795 QCOMPARE(gridview->currentIndex(), 34);
1797 QTest::keyClick(canvas, Qt::Key_Left);
1798 QCOMPARE(gridview->currentIndex(), 35);
1801 // turn off auto highlight
1802 gridview->setHighlightFollowsCurrentItem(false);
1803 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1804 QVERIFY(gridview->highlightItem());
1805 hlPosX = gridview->highlightItem()->x();
1806 hlPosY = gridview->highlightItem()->y();
1808 gridview->setCurrentIndex(5);
1809 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1810 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1812 // insert item before currentIndex
1813 gridview->setCurrentIndex(28);
1814 model.insertItem(0, "Foo", "1111");
1815 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1817 // check removing highlight by setting currentIndex to -1;
1818 gridview->setCurrentIndex(-1);
1820 QCOMPARE(gridview->currentIndex(), -1);
1821 QVERIFY(!gridview->highlightItem());
1822 QVERIFY(!gridview->currentItem());
1824 // moving currentItem out of view should make it invisible
1825 gridview->setCurrentIndex(0);
1826 QTRY_VERIFY(gridview->currentItem()->isVisible());
1827 gridview->setContentY(200);
1828 QTRY_VERIFY(!gridview->currentItem()->isVisible());
1833 void tst_QQuickGridView::noCurrentIndex()
1836 for (int i = 0; i < 60; i++)
1837 model.addItem("Item" + QString::number(i), QString::number(i));
1839 QQuickView *canvas = new QQuickView(0);
1840 canvas->setGeometry(0,0,240,320);
1842 QQmlContext *ctxt = canvas->rootContext();
1843 ctxt->setContextProperty("testModel", &model);
1845 QString filename(testFile("gridview-noCurrent.qml"));
1846 canvas->setSource(QUrl::fromLocalFile(filename));
1848 qApp->processEvents();
1850 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1851 QVERIFY(gridview != 0);
1852 QQuickItem *contentItem = gridview->contentItem();
1853 QVERIFY(contentItem != 0);
1854 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1856 // current index should be -1
1857 QCOMPARE(gridview->currentIndex(), -1);
1858 QVERIFY(!gridview->currentItem());
1859 QVERIFY(!gridview->highlightItem());
1860 QCOMPARE(gridview->contentY(), 0.0);
1862 gridview->setCurrentIndex(5);
1863 QCOMPARE(gridview->currentIndex(), 5);
1864 QVERIFY(gridview->currentItem());
1865 QVERIFY(gridview->highlightItem());
1870 void tst_QQuickGridView::changeFlow()
1872 QQuickView *canvas = createView();
1875 for (int i = 0; i < 30; i++)
1876 model.addItem("Item" + QString::number(i), QString::number(i));
1878 QQmlContext *ctxt = canvas->rootContext();
1879 ctxt->setContextProperty("testModel", &model);
1880 ctxt->setContextProperty("testRightToLeft", QVariant(false));
1881 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1883 canvas->setSource(testFileUrl("gridview1.qml"));
1884 qApp->processEvents();
1886 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1887 QTRY_VERIFY(gridview != 0);
1889 QQuickItem *contentItem = gridview->contentItem();
1890 QTRY_VERIFY(contentItem != 0);
1892 // Confirm items positioned correctly and indexes correct
1893 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1894 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1895 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1896 if (!item) qWarning() << "Item" << i << "not found";
1898 QTRY_COMPARE(item->x(), qreal((i%3)*80));
1899 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1900 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1901 QTRY_VERIFY(name != 0);
1902 QTRY_COMPARE(name->text(), model.name(i));
1903 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1904 QTRY_VERIFY(number != 0);
1905 QTRY_COMPARE(number->text(), model.number(i));
1908 ctxt->setContextProperty("testTopToBottom", QVariant(true));
1910 // Confirm items positioned correctly and indexes correct
1911 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1912 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1913 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1914 if (!item) qWarning() << "Item" << i << "not found";
1916 QTRY_COMPARE(item->x(), qreal((i/5)*80));
1917 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1918 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1919 QTRY_VERIFY(name != 0);
1920 QTRY_COMPARE(name->text(), model.name(i));
1921 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1922 QTRY_VERIFY(number != 0);
1923 QTRY_COMPARE(number->text(), model.number(i));
1926 ctxt->setContextProperty("testRightToLeft", QVariant(true));
1928 // Confirm items positioned correctly and indexes correct
1929 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1930 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1931 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1932 if (!item) qWarning() << "Item" << i << "not found";
1934 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
1935 QTRY_COMPARE(item->y(), qreal((i%5)*60));
1936 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1937 QTRY_VERIFY(name != 0);
1938 QTRY_COMPARE(name->text(), model.name(i));
1939 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1940 QTRY_VERIFY(number != 0);
1941 QTRY_COMPARE(number->text(), model.number(i));
1943 gridview->setContentX(100);
1944 QTRY_COMPARE(gridview->contentX(), 100.);
1945 ctxt->setContextProperty("testTopToBottom", QVariant(false));
1946 QTRY_COMPARE(gridview->contentX(), 0.);
1948 // Confirm items positioned correctly and indexes correct
1949 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1950 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1951 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1952 if (!item) qWarning() << "Item" << i << "not found";
1954 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
1955 QTRY_COMPARE(item->y(), qreal((i/3)*60));
1956 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1957 QTRY_VERIFY(name != 0);
1958 QTRY_COMPARE(name->text(), model.name(i));
1959 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
1960 QTRY_VERIFY(number != 0);
1961 QTRY_COMPARE(number->text(), model.number(i));
1967 void tst_QQuickGridView::defaultValues()
1970 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
1971 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1973 QTRY_VERIFY(obj != 0);
1974 QTRY_VERIFY(obj->model() == QVariant());
1975 QTRY_VERIFY(obj->delegate() == 0);
1976 QTRY_COMPARE(obj->currentIndex(), -1);
1977 QTRY_VERIFY(obj->currentItem() == 0);
1978 QTRY_COMPARE(obj->count(), 0);
1979 QTRY_VERIFY(obj->highlight() == 0);
1980 QTRY_VERIFY(obj->highlightItem() == 0);
1981 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
1982 QTRY_VERIFY(obj->flow() == 0);
1983 QTRY_COMPARE(obj->isWrapEnabled(), false);
1984 QTRY_COMPARE(obj->cacheBuffer(), 0);
1985 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
1986 QTRY_COMPARE(obj->cellHeight(), qreal(100));
1990 void tst_QQuickGridView::properties()
1993 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
1994 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
1996 QTRY_VERIFY(obj != 0);
1997 QTRY_VERIFY(obj->model() != QVariant());
1998 QTRY_VERIFY(obj->delegate() != 0);
1999 QTRY_COMPARE(obj->currentIndex(), 0);
2000 QTRY_VERIFY(obj->currentItem() != 0);
2001 QTRY_COMPARE(obj->count(), 4);
2002 QTRY_VERIFY(obj->highlight() != 0);
2003 QTRY_VERIFY(obj->highlightItem() != 0);
2004 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
2005 QTRY_VERIFY(obj->flow() == 0);
2006 QTRY_COMPARE(obj->isWrapEnabled(), true);
2007 QTRY_COMPARE(obj->cacheBuffer(), 200);
2008 QTRY_COMPARE(obj->cellWidth(), qreal(100));
2009 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2013 void tst_QQuickGridView::propertyChanges()
2015 QQuickView *canvas = createView();
2016 QTRY_VERIFY(canvas);
2017 canvas->setSource(testFileUrl("propertychangestest.qml"));
2019 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2020 QTRY_VERIFY(gridView);
2022 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
2023 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
2024 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
2025 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
2027 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2028 QTRY_COMPARE(gridView->cacheBuffer(), 10);
2029 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
2031 gridView->setWrapEnabled(false);
2032 gridView->setCacheBuffer(3);
2033 gridView->setFlow(QQuickGridView::TopToBottom);
2035 QTRY_COMPARE(gridView->isWrapEnabled(), false);
2036 QTRY_COMPARE(gridView->cacheBuffer(), 3);
2037 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
2039 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2040 QTRY_COMPARE(cacheBufferSpy.count(),1);
2041 QTRY_COMPARE(flowSpy.count(),1);
2043 gridView->setWrapEnabled(false);
2044 gridView->setCacheBuffer(3);
2045 gridView->setFlow(QQuickGridView::TopToBottom);
2047 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2048 QTRY_COMPARE(cacheBufferSpy.count(),1);
2049 QTRY_COMPARE(flowSpy.count(),1);
2051 gridView->setFlow(QQuickGridView::LeftToRight);
2052 QTRY_COMPARE(gridView->flow(), QQuickGridView::LeftToRight);
2054 gridView->setWrapEnabled(true);
2055 gridView->setCacheBuffer(5);
2056 gridView->setLayoutDirection(Qt::RightToLeft);
2058 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2059 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2060 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2062 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2063 QTRY_COMPARE(cacheBufferSpy.count(),2);
2064 QTRY_COMPARE(layoutSpy.count(),1);
2065 QTRY_COMPARE(flowSpy.count(),2);
2067 gridView->setWrapEnabled(true);
2068 gridView->setCacheBuffer(5);
2069 gridView->setLayoutDirection(Qt::RightToLeft);
2071 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2072 QTRY_COMPARE(cacheBufferSpy.count(),2);
2073 QTRY_COMPARE(layoutSpy.count(),1);
2074 QTRY_COMPARE(flowSpy.count(),2);
2076 gridView->setFlow(QQuickGridView::TopToBottom);
2077 QTRY_COMPARE(gridView->flow(), QQuickGridView::TopToBottom);
2078 QTRY_COMPARE(flowSpy.count(),3);
2080 gridView->setFlow(QQuickGridView::TopToBottom);
2081 QTRY_COMPARE(flowSpy.count(),3);
2086 void tst_QQuickGridView::componentChanges()
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 QQmlComponent component(canvas->engine());
2096 component.setData("import QtQuick 1.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2098 QQmlComponent delegateComponent(canvas->engine());
2099 delegateComponent.setData("import QtQuick 1.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2101 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2102 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2103 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2104 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2106 gridView->setHighlight(&component);
2107 gridView->setDelegate(&delegateComponent);
2108 gridView->setHeader(&component);
2109 gridView->setFooter(&component);
2111 QTRY_COMPARE(gridView->highlight(), &component);
2112 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2113 QTRY_COMPARE(gridView->header(), &component);
2114 QTRY_COMPARE(gridView->footer(), &component);
2116 QTRY_COMPARE(highlightSpy.count(),1);
2117 QTRY_COMPARE(delegateSpy.count(),1);
2118 QTRY_COMPARE(headerSpy.count(),1);
2119 QTRY_COMPARE(footerSpy.count(),1);
2121 gridView->setHighlight(&component);
2122 gridView->setDelegate(&delegateComponent);
2123 gridView->setHeader(&component);
2124 gridView->setFooter(&component);
2126 QTRY_COMPARE(highlightSpy.count(),1);
2127 QTRY_COMPARE(delegateSpy.count(),1);
2128 QTRY_COMPARE(headerSpy.count(),1);
2129 QTRY_COMPARE(footerSpy.count(),1);
2134 void tst_QQuickGridView::modelChanges()
2136 QQuickView *canvas = createView();
2137 QTRY_VERIFY(canvas);
2138 canvas->setSource(testFileUrl("propertychangestest.qml"));
2140 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2141 QTRY_VERIFY(gridView);
2143 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2144 QTRY_VERIFY(alternateModel);
2145 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2146 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2148 gridView->setModel(modelVariant);
2149 QTRY_COMPARE(gridView->model(), modelVariant);
2150 QTRY_COMPARE(modelSpy.count(),1);
2152 gridView->setModel(modelVariant);
2153 QTRY_COMPARE(modelSpy.count(),1);
2155 gridView->setModel(QVariant());
2156 QTRY_COMPARE(modelSpy.count(),2);
2160 void tst_QQuickGridView::positionViewAtIndex()
2162 QQuickView *canvas = createView();
2165 for (int i = 0; i < 40; i++)
2166 model.addItem("Item" + QString::number(i), "");
2168 QQmlContext *ctxt = canvas->rootContext();
2169 ctxt->setContextProperty("testModel", &model);
2170 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2171 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2173 canvas->setSource(testFileUrl("gridview1.qml"));
2175 qApp->processEvents();
2177 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2178 QTRY_VERIFY(gridview != 0);
2179 QQuickItem *contentItem = gridview->contentItem();
2180 QTRY_VERIFY(contentItem != 0);
2181 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2183 // Confirm items positioned correctly
2184 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2185 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2186 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2187 if (!item) qWarning() << "Item" << i << "not found";
2189 QTRY_COMPARE(item->x(), (i%3)*80.);
2190 QTRY_COMPARE(item->y(), (i/3)*60.);
2193 // Position on a currently visible item
2194 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2195 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2196 QTRY_COMPARE(gridview->contentY(), 60.);
2198 // Confirm items positioned correctly
2199 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2200 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2201 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2202 if (!item) qWarning() << "Item" << i << "not found";
2204 QTRY_COMPARE(item->x(), (i%3)*80.);
2205 QTRY_COMPARE(item->y(), (i/3)*60.);
2208 // Position on an item beyond the visible items
2209 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2210 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2211 QTRY_COMPARE(gridview->contentY(), 420.);
2213 // Confirm items positioned correctly
2214 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2215 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2216 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2217 if (!item) qWarning() << "Item" << i << "not found";
2219 QTRY_COMPARE(item->x(), (i%3)*80.);
2220 QTRY_COMPARE(item->y(), (i/3)*60.);
2223 // Position on an item that would leave empty space if positioned at the top
2224 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2225 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2226 QTRY_COMPARE(gridview->contentY(), 520.);
2228 // Confirm items positioned correctly
2229 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2230 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2231 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2232 if (!item) qWarning() << "Item" << i << "not found";
2234 QTRY_COMPARE(item->x(), (i%3)*80.);
2235 QTRY_COMPARE(item->y(), (i/3)*60.);
2238 // Position at the beginning again
2239 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2240 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2241 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2242 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2243 QTRY_COMPARE(gridview->contentY(), 0.);
2245 // Confirm items positioned correctly
2246 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2247 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2248 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2249 if (!item) qWarning() << "Item" << i << "not found";
2251 QTRY_COMPARE(item->x(), (i%3)*80.);
2252 QTRY_COMPARE(item->y(), (i/3)*60.);
2256 gridview->positionViewAtIndex(30, QQuickGridView::End);
2257 QTRY_COMPARE(gridview->contentY(), 340.);
2259 // Position in Center
2260 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2261 QTRY_COMPARE(gridview->contentY(), 170.);
2263 // Ensure at least partially visible
2264 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2265 QTRY_COMPARE(gridview->contentY(), 170.);
2267 gridview->setContentY(302);
2268 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2269 QTRY_COMPARE(gridview->contentY(), 302.);
2271 gridview->setContentY(360);
2272 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2273 QTRY_COMPARE(gridview->contentY(), 300.);
2275 gridview->setContentY(60);
2276 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2277 QTRY_COMPARE(gridview->contentY(), 60.);
2279 gridview->setContentY(20);
2280 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2281 QTRY_COMPARE(gridview->contentY(), 100.);
2283 // Ensure completely visible
2284 gridview->setContentY(120);
2285 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2286 QTRY_COMPARE(gridview->contentY(), 120.);
2288 gridview->setContentY(302);
2289 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2290 QTRY_COMPARE(gridview->contentY(), 300.);
2292 gridview->setContentY(60);
2293 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2294 QTRY_COMPARE(gridview->contentY(), 100.);
2296 // Test for Top To Bottom layout
2297 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2299 // Confirm items positioned correctly
2300 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2301 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2302 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2303 if (!item) qWarning() << "Item" << i << "not found";
2305 QTRY_COMPARE(item->x(), (i/5)*80.);
2306 QTRY_COMPARE(item->y(), (i%5)*60.);
2310 gridview->positionViewAtIndex(30, QQuickGridView::End);
2311 QTRY_COMPARE(gridview->contentX(), 320.);
2312 QTRY_COMPARE(gridview->contentY(), 0.);
2314 // Position in Center
2315 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2316 QTRY_COMPARE(gridview->contentX(), 160.);
2318 // Ensure at least partially visible
2319 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2320 QTRY_COMPARE(gridview->contentX(), 160.);
2322 gridview->setContentX(170);
2323 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2324 QTRY_COMPARE(gridview->contentX(), 170.);
2326 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2327 QTRY_COMPARE(gridview->contentX(), 320.);
2329 gridview->setContentX(170);
2330 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2331 QTRY_COMPARE(gridview->contentX(), 240.);
2333 // positionViewAtBeginning
2334 gridview->positionViewAtBeginning();
2335 QTRY_COMPARE(gridview->contentX(), 0.);
2337 gridview->setContentX(80);
2338 canvas->rootObject()->setProperty("showHeader", true);
2339 gridview->positionViewAtBeginning();
2340 QTRY_COMPARE(gridview->contentX(), -30.);
2342 // positionViewAtEnd
2343 gridview->positionViewAtEnd();
2344 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2346 gridview->setContentX(80);
2347 canvas->rootObject()->setProperty("showFooter", true);
2348 gridview->positionViewAtEnd();
2349 QTRY_COMPARE(gridview->contentX(), 430.);
2351 // set current item to outside visible view, position at beginning
2352 // and ensure highlight moves to current item
2353 gridview->setCurrentIndex(6);
2354 gridview->positionViewAtBeginning();
2355 QTRY_COMPARE(gridview->contentX(), -30.);
2356 QVERIFY(gridview->highlightItem());
2357 QCOMPARE(gridview->highlightItem()->x(), 80.);
2362 void tst_QQuickGridView::snapping()
2364 QQuickView *canvas = createView();
2367 for (int i = 0; i < 40; i++)
2368 model.addItem("Item" + QString::number(i), "");
2370 QQmlContext *ctxt = canvas->rootContext();
2371 ctxt->setContextProperty("testModel", &model);
2372 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2373 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2375 canvas->setSource(testFileUrl("gridview1.qml"));
2376 qApp->processEvents();
2378 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2379 QTRY_VERIFY(gridview != 0);
2381 gridview->setHeight(220);
2382 QCOMPARE(gridview->height(), 220.);
2384 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2385 QCOMPARE(gridview->contentY(), 80.);
2387 gridview->setContentY(0);
2388 QCOMPARE(gridview->contentY(), 0.);
2390 gridview->setSnapMode(QQuickGridView::SnapToRow);
2391 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2393 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2394 QCOMPARE(gridview->contentY(), 60.);
2396 gridview->positionViewAtIndex(15, QQuickGridView::End);
2397 QCOMPARE(gridview->contentY(), 120.);
2403 void tst_QQuickGridView::mirroring()
2405 QQuickView *canvasA = createView();
2406 canvasA->setSource(testFileUrl("mirroring.qml"));
2407 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2408 QTRY_VERIFY(gridviewA != 0);
2410 QQuickView *canvasB = createView();
2411 canvasB->setSource(testFileUrl("mirroring.qml"));
2412 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2413 QTRY_VERIFY(gridviewA != 0);
2414 qApp->processEvents();
2416 QList<QString> objectNames;
2417 objectNames << "item1" << "item2"; // << "item3"
2419 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2420 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2421 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2424 foreach (const QString objectName, objectNames)
2425 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2427 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2428 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2431 foreach (const QString objectName, objectNames)
2432 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2434 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2435 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2436 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2438 // LTR != LTR+mirror
2439 foreach (const QString objectName, objectNames)
2440 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2442 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2444 // RTL == LTR+mirror
2445 foreach (const QString objectName, objectNames)
2446 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2448 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2450 // RTL != RTL+mirror
2451 foreach (const QString objectName, objectNames)
2452 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2454 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2456 // LTR == RTL+mirror
2457 foreach (const QString objectName, objectNames)
2458 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2464 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2466 QQuickView *canvas = createView();
2469 for (int i = 0; i < 40; i++)
2470 model.addItem("Item" + QString::number(i), "");
2472 QQmlContext *ctxt = canvas->rootContext();
2473 ctxt->setContextProperty("testModel", &model);
2474 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2475 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2477 canvas->setSource(testFileUrl("gridview1.qml"));
2478 qApp->processEvents();
2480 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2481 QTRY_VERIFY(gridview != 0);
2483 QQuickItem *contentItem = gridview->contentItem();
2484 QTRY_VERIFY(contentItem != 0);
2486 // Confirm items positioned correctly
2487 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2488 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2489 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2490 if (!item) qWarning() << "Item" << i << "not found";
2492 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2493 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2496 // Position on a currently visible item
2497 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2498 QTRY_COMPARE(gridview->contentX(), -320.);
2500 // Confirm items positioned correctly
2501 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2502 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2503 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2504 if (!item) qWarning() << "Item" << i << "not found";
2506 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2507 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2510 // Position on an item beyond the visible items
2511 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2512 QTRY_COMPARE(gridview->contentX(), -560.);
2514 // Confirm items positioned correctly
2515 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2516 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2517 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2518 if (!item) qWarning() << "Item" << i << "not found";
2520 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2521 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2524 // Position on an item that would leave empty space if positioned at the top
2525 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2526 QTRY_COMPARE(gridview->contentX(), -640.);
2528 // Confirm items positioned correctly
2529 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2530 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2531 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2532 if (!item) qWarning() << "Item" << i << "not found";
2534 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2535 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2538 // Position at the beginning again
2539 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2540 QTRY_COMPARE(gridview->contentX(), -240.);
2542 // Confirm items positioned correctly
2543 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2544 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2545 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2546 if (!item) qWarning() << "Item" << i << "not found";
2548 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2549 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2553 gridview->positionViewAtIndex(30, QQuickGridView::End);
2554 QTRY_COMPARE(gridview->contentX(), -560.);
2556 // Position in Center
2557 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2558 QTRY_COMPARE(gridview->contentX(), -400.);
2560 // Ensure at least partially visible
2561 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2562 QTRY_COMPARE(gridview->contentX(), -400.);
2564 gridview->setContentX(-555.);
2565 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2566 QTRY_COMPARE(gridview->contentX(), -555.);
2568 gridview->setContentX(-239);
2569 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2570 QTRY_COMPARE(gridview->contentX(), -320.);
2572 gridview->setContentX(-239);
2573 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2574 QTRY_COMPARE(gridview->contentX(), -400.);
2576 gridview->setContentX(-640);
2577 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2578 QTRY_COMPARE(gridview->contentX(), -560.);
2580 // Ensure completely visible
2581 gridview->setContentX(-400);
2582 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2583 QTRY_COMPARE(gridview->contentX(), -400.);
2585 gridview->setContentX(-315);
2586 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2587 QTRY_COMPARE(gridview->contentX(), -320.);
2589 gridview->setContentX(-640);
2590 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2591 QTRY_COMPARE(gridview->contentX(), -560.);
2596 void tst_QQuickGridView::resetModel()
2598 QQuickView *canvas = createView();
2600 QStringList strings;
2601 strings << "one" << "two" << "three";
2602 QStringListModel model(strings);
2604 QQmlContext *ctxt = canvas->rootContext();
2605 ctxt->setContextProperty("testModel", &model);
2607 canvas->setSource(testFileUrl("displaygrid.qml"));
2609 qApp->processEvents();
2611 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2612 QTRY_VERIFY(gridview != 0);
2613 QQuickItem *contentItem = gridview->contentItem();
2614 QTRY_VERIFY(contentItem != 0);
2615 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2617 QTRY_COMPARE(gridview->count(), model.rowCount());
2619 for (int i = 0; i < model.rowCount(); ++i) {
2620 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2621 QTRY_VERIFY(display != 0);
2622 QTRY_COMPARE(display->text(), strings.at(i));
2626 strings << "four" << "five" << "six" << "seven";
2627 model.setStringList(strings);
2629 QTRY_COMPARE(gridview->count(), model.rowCount());
2631 for (int i = 0; i < model.rowCount(); ++i) {
2632 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2633 QTRY_VERIFY(display != 0);
2634 QTRY_COMPARE(display->text(), strings.at(i));
2640 void tst_QQuickGridView::enforceRange()
2642 QQuickView *canvas = createView();
2645 for (int i = 0; i < 30; i++)
2646 model.addItem("Item" + QString::number(i), "");
2648 QQmlContext *ctxt = canvas->rootContext();
2649 ctxt->setContextProperty("testModel", &model);
2650 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2651 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2653 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2655 qApp->processEvents();
2656 QVERIFY(canvas->rootObject() != 0);
2658 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2659 QTRY_VERIFY(gridview != 0);
2661 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2662 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2663 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2664 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2666 QQuickItem *contentItem = gridview->contentItem();
2667 QTRY_VERIFY(contentItem != 0);
2669 // view should be positioned at the top of the range.
2670 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2672 QTRY_COMPARE(gridview->contentY(), -100.0);
2674 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2675 QTRY_VERIFY(name != 0);
2676 QTRY_COMPARE(name->text(), model.name(0));
2677 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2678 QTRY_VERIFY(number != 0);
2679 QTRY_COMPARE(number->text(), model.number(0));
2681 // Check currentIndex is updated when contentItem moves
2682 gridview->setContentY(0);
2683 QTRY_COMPARE(gridview->currentIndex(), 2);
2685 gridview->setCurrentIndex(5);
2686 QTRY_COMPARE(gridview->contentY(), 100.);
2689 for (int i = 0; i < 5; i++)
2690 model2.addItem("Item" + QString::number(i), "");
2692 ctxt->setContextProperty("testModel", &model2);
2693 QCOMPARE(gridview->count(), 5);
2698 void tst_QQuickGridView::enforceRange_rightToLeft()
2700 QQuickView *canvas = createView();
2703 for (int i = 0; i < 30; i++)
2704 model.addItem("Item" + QString::number(i), "");
2706 QQmlContext *ctxt = canvas->rootContext();
2707 ctxt->setContextProperty("testModel", &model);
2708 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2709 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2711 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2712 qApp->processEvents();
2713 QVERIFY(canvas->rootObject() != 0);
2715 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2716 QTRY_VERIFY(gridview != 0);
2718 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2719 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2720 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2722 QQuickItem *contentItem = gridview->contentItem();
2723 QVERIFY(contentItem != 0);
2725 // view should be positioned at the top of the range.
2726 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2728 QTRY_COMPARE(gridview->contentX(), -140.);
2729 QTRY_COMPARE(gridview->contentY(), 0.0);
2731 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2732 QTRY_VERIFY(name != 0);
2733 QTRY_COMPARE(name->text(), model.name(0));
2734 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2735 QTRY_VERIFY(number != 0);
2736 QTRY_COMPARE(number->text(), model.number(0));
2738 // Check currentIndex is updated when contentItem moves
2739 gridview->setContentX(-240);
2740 QTRY_COMPARE(gridview->currentIndex(), 3);
2742 gridview->setCurrentIndex(7);
2743 QTRY_COMPARE(gridview->contentX(), -340.);
2744 QTRY_COMPARE(gridview->contentY(), 0.0);
2747 for (int i = 0; i < 5; i++)
2748 model2.addItem("Item" + QString::number(i), "");
2750 ctxt->setContextProperty("testModel", &model2);
2751 QCOMPARE(gridview->count(), 5);
2756 void tst_QQuickGridView::QTBUG_8456()
2758 QQuickView *canvas = createView();
2760 canvas->setSource(testFileUrl("setindex.qml"));
2761 qApp->processEvents();
2763 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2764 QTRY_VERIFY(gridview != 0);
2766 QTRY_COMPARE(gridview->currentIndex(), 0);
2771 void tst_QQuickGridView::manualHighlight()
2773 QQuickView *canvas = createView();
2775 QString filename(testFile("manual-highlight.qml"));
2776 canvas->setSource(QUrl::fromLocalFile(filename));
2778 qApp->processEvents();
2780 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2781 QTRY_VERIFY(gridview != 0);
2783 QQuickItem *contentItem = gridview->contentItem();
2784 QTRY_VERIFY(contentItem != 0);
2786 QTRY_COMPARE(gridview->currentIndex(), 0);
2787 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2788 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2789 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2791 gridview->setCurrentIndex(2);
2793 QTRY_COMPARE(gridview->currentIndex(), 2);
2794 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2795 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2796 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2798 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
2800 QTRY_COMPARE(gridview->currentIndex(), 2);
2801 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
2802 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2803 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2805 gridview->setFlow(QQuickGridView::TopToBottom);
2806 QTRY_COMPARE(gridview->flow(), QQuickGridView::TopToBottom);
2808 gridview->setCurrentIndex(0);
2809 QTRY_COMPARE(gridview->currentIndex(), 0);
2810 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
2811 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2812 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2818 void tst_QQuickGridView::footer()
2820 QFETCH(QQuickGridView::Flow, flow);
2821 QFETCH(Qt::LayoutDirection, layoutDirection);
2822 QFETCH(QPointF, initialFooterPos);
2823 QFETCH(QPointF, changedFooterPos);
2824 QFETCH(QPointF, initialContentPos);
2825 QFETCH(QPointF, changedContentPos);
2826 QFETCH(QPointF, firstDelegatePos);
2827 QFETCH(QPointF, resizeContentPos);
2829 QQuickView *canvas = getView();
2833 for (int i = 0; i < 7; i++)
2834 model.addItem("Item" + QString::number(i), "");
2836 QQmlContext *ctxt = canvas->rootContext();
2837 ctxt->setContextProperty("testModel", &model);
2839 canvas->setSource(testFileUrl("footer.qml"));
2840 qApp->processEvents();
2842 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2843 QTRY_VERIFY(gridview != 0);
2844 gridview->setFlow(flow);
2845 gridview->setLayoutDirection(layoutDirection);
2847 QQuickItem *contentItem = gridview->contentItem();
2848 QTRY_VERIFY(contentItem != 0);
2850 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
2853 QVERIFY(footer == gridview->footerItem());
2855 QCOMPARE(footer->pos(), initialFooterPos);
2856 QCOMPARE(footer->width(), 100.);
2857 QCOMPARE(footer->height(), 30.);
2858 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2860 if (flow == QQuickGridView::LeftToRight)
2861 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + footer->height());
2863 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + footer->width());
2865 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2867 QCOMPARE(item->pos(), firstDelegatePos);
2869 if (flow == QQuickGridView::LeftToRight) {
2870 // shrink by one row
2871 model.removeItem(2);
2872 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
2874 // shrink by one column
2875 model.removeItem(2);
2876 model.removeItem(3);
2877 if (layoutDirection == Qt::LeftToRight)
2878 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
2880 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
2886 QPointF posWhenNoItems(0, 0);
2887 if (layoutDirection == Qt::RightToLeft)
2888 posWhenNoItems.setX(flow == QQuickGridView::LeftToRight ? gridview->width() - footer->width() : -footer->width());
2889 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2891 // if header is present, it's at a negative pos, so the footer should not move
2892 canvas->rootObject()->setProperty("showHeader", true);
2893 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
2894 QTRY_COMPARE(footer->pos(), posWhenNoItems);
2895 canvas->rootObject()->setProperty("showHeader", false);
2898 for (int i = 0; i < 30; i++)
2899 model.addItem("Item" + QString::number(i), "");
2901 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
2902 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
2904 QCOMPARE(footerItemSpy.count(), 1);
2906 footer = findItem<QQuickText>(contentItem, "footer");
2908 footer = findItem<QQuickText>(contentItem, "footer2");
2911 QVERIFY(footer == gridview->footerItem());
2913 QCOMPARE(footer->pos(), changedFooterPos);
2914 QCOMPARE(footer->width(), 50.);
2915 QCOMPARE(footer->height(), 20.);
2916 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2918 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2920 QCOMPARE(item->pos(), firstDelegatePos);
2922 gridview->positionViewAtEnd();
2923 footer->setHeight(10);
2924 footer->setWidth(40);
2925 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
2927 releaseView(canvas);
2930 void tst_QQuickGridView::footer_data()
2932 QTest::addColumn<QQuickGridView::Flow>("flow");
2933 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2934 QTest::addColumn<QPointF>("initialFooterPos");
2935 QTest::addColumn<QPointF>("changedFooterPos");
2936 QTest::addColumn<QPointF>("initialContentPos");
2937 QTest::addColumn<QPointF>("changedContentPos");
2938 QTest::addColumn<QPointF>("firstDelegatePos");
2939 QTest::addColumn<QPointF>("resizeContentPos");
2941 // footer1 = 100 x 30
2942 // footer2 = 50 x 20
2945 // view height = 320
2947 // footer below items, bottom left
2948 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
2949 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
2950 << QPointF(0, 10 * 60) // 30 items = 10 rows
2954 << QPointF(0, 10 * 60 - 320 + 10);
2956 // footer below items, bottom right
2957 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
2958 << QPointF(240 - 100, 3 * 60)
2959 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
2962 << QPointF(240 - 80, 0)
2963 << QPointF(0, 10 * 60 - 320 + 10);
2965 // footer to right of items
2966 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
2967 << QPointF(2 * 80, 0) // 2 columns, cell width 80
2968 << QPointF(6 * 80, 0) // 30 items = 6 columns
2972 << QPointF(6 * 80 - 240 + 40, 0);
2974 // footer to left of items
2975 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
2976 << QPointF(-(2 * 80) - 100, 0)
2977 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
2979 << QPointF(-240, 0) // unchanged, footer change doesn't change content pos
2981 << QPointF(-(6 * 80) - 40, 0);
2984 void tst_QQuickGridView::header()
2986 QFETCH(QQuickGridView::Flow, flow);
2987 QFETCH(Qt::LayoutDirection, layoutDirection);
2988 QFETCH(QPointF, initialHeaderPos);
2989 QFETCH(QPointF, changedHeaderPos);
2990 QFETCH(QPointF, initialContentPos);
2991 QFETCH(QPointF, changedContentPos);
2992 QFETCH(QPointF, firstDelegatePos);
2993 QFETCH(QPointF, resizeContentPos);
2996 for (int i = 0; i < 30; i++)
2997 model.addItem("Item" + QString::number(i), "");
2999 QQuickView *canvas = getView();
3000 canvas->rootContext()->setContextProperty("testModel", &model);
3001 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3002 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3003 canvas->setSource(testFileUrl("header.qml"));
3005 qApp->processEvents();
3007 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3008 QTRY_VERIFY(gridview != 0);
3009 gridview->setFlow(flow);
3010 gridview->setLayoutDirection(layoutDirection);
3011 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3013 QQuickItem *contentItem = gridview->contentItem();
3014 QTRY_VERIFY(contentItem != 0);
3016 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3019 QVERIFY(header == gridview->headerItem());
3021 QCOMPARE(header->pos(), initialHeaderPos);
3022 QCOMPARE(header->width(), 100.);
3023 QCOMPARE(header->height(), 30.);
3024 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3026 if (flow == QQuickGridView::LeftToRight)
3027 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + header->height());
3029 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + header->width());
3031 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3033 QCOMPARE(item->pos(), firstDelegatePos);
3036 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3037 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3039 for (int i = 0; i < 30; i++)
3040 model.addItem("Item" + QString::number(i), "");
3042 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
3043 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3045 QCOMPARE(headerItemSpy.count(), 1);
3047 header = findItem<QQuickText>(contentItem, "header");
3049 header = findItem<QQuickText>(contentItem, "header2");
3052 QVERIFY(header == gridview->headerItem());
3054 QCOMPARE(header->pos(), changedHeaderPos);
3055 QCOMPARE(header->width(), 50.);
3056 QCOMPARE(header->height(), 20.);
3057 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3059 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3061 QCOMPARE(item->pos(), firstDelegatePos);
3063 header->setHeight(10);
3064 header->setWidth(40);
3065 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3067 releaseView(canvas);
3070 // QTBUG-21207 header should become visible if view resizes from initial empty size
3073 canvas->rootContext()->setContextProperty("testModel", &model);
3074 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3075 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3076 canvas->setSource(testFileUrl("header.qml"));
3078 qApp->processEvents();
3080 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3081 QTRY_VERIFY(gridview != 0);
3082 gridview->setFlow(flow);
3083 gridview->setLayoutDirection(layoutDirection);
3084 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3086 gridview->setWidth(240);
3087 gridview->setHeight(320);
3088 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3089 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3091 releaseView(canvas);
3094 void tst_QQuickGridView::header_data()
3096 QTest::addColumn<QQuickGridView::Flow>("flow");
3097 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3098 QTest::addColumn<QPointF>("initialHeaderPos");
3099 QTest::addColumn<QPointF>("changedHeaderPos");
3100 QTest::addColumn<QPointF>("initialContentPos");
3101 QTest::addColumn<QPointF>("changedContentPos");
3102 QTest::addColumn<QPointF>("firstDelegatePos");
3103 QTest::addColumn<QPointF>("resizeContentPos");
3105 // header1 = 100 x 30
3106 // header2 = 50 x 20
3110 // header above items, top left
3111 QTest::newRow("flow left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight
3119 // header above items, top right
3120 QTest::newRow("flow left to right, layout right to left") << QQuickGridView::LeftToRight << Qt::RightToLeft
3121 << QPointF(240 - 100, -30)
3122 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3128 // header to left of items
3129 QTest::newRow("flow top to bottom, layout left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight
3137 // header to right of items
3138 QTest::newRow("flow top to bottom, layout right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft
3141 << QPointF(-(240 - 100), 0)
3142 << QPointF(-(240 - 50), 0)
3144 << QPointF(-(240 - 40), 0);
3147 void tst_QQuickGridView::resizeViewAndRepaint()
3149 QQuickView *canvas = createView();
3153 for (int i = 0; i < 40; i++)
3154 model.addItem("Item" + QString::number(i), "");
3156 QQmlContext *ctxt = canvas->rootContext();
3157 ctxt->setContextProperty("testModel", &model);
3158 ctxt->setContextProperty("initialWidth", 240);
3159 ctxt->setContextProperty("initialHeight", 100);
3161 canvas->setSource(testFileUrl("resizeview.qml"));
3163 qApp->processEvents();
3165 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3166 QTRY_VERIFY(gridview != 0);
3167 QQuickItem *contentItem = gridview->contentItem();
3168 QTRY_VERIFY(contentItem != 0);
3169 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3171 // item at index 10 should not be currently visible
3172 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3174 gridview->setHeight(320);
3175 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3177 gridview->setHeight(100);
3178 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3180 // Ensure we handle -ve sizes
3181 gridview->setHeight(-100);
3182 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3184 gridview->setCacheBuffer(120);
3185 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3187 // ensure items in cache become visible
3188 gridview->setHeight(120);
3189 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3191 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3192 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3193 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3194 if (!item) qWarning() << "Item" << i << "not found";
3196 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3197 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3198 QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3201 // ensure items outside view become invisible
3202 gridview->setHeight(60);
3203 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3205 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3206 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3207 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3208 if (!item) qWarning() << "Item" << i << "not found";
3210 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3211 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3212 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3218 void tst_QQuickGridView::changeColumnCount()
3221 for (int i = 0; i < 40; i++)
3222 model.addItem("Item" + QString::number(i), "");
3224 QQuickView *canvas = createView();
3225 QQmlContext *ctxt = canvas->rootContext();
3226 ctxt->setContextProperty("testModel", &model);
3227 ctxt->setContextProperty("initialWidth", 100);
3228 ctxt->setContextProperty("initialHeight", 320);
3229 canvas->setSource(testFileUrl("resizeview.qml"));
3231 qApp->processEvents();
3233 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3234 QTRY_VERIFY(gridview != 0);
3235 QQuickItem *contentItem = gridview->contentItem();
3236 QTRY_VERIFY(contentItem != 0);
3237 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3239 // a single column of 6 items are visible
3240 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3241 QCOMPARE(itemCount, 6);
3242 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3243 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3244 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3245 QCOMPARE(item->x(), 0.0);
3246 QCOMPARE(item->y(), qreal(i*60));
3249 // now 6x3 grid is visible, plus 1 extra below for refill
3250 gridview->setWidth(240);
3251 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3252 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3253 QCOMPARE(itemCount, 6*3 + 1);
3254 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3255 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3256 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3257 QCOMPARE(item->x(), qreal((i%3)*80));
3258 QCOMPARE(item->y(), qreal((i/3)*60));
3261 // back to single column
3262 gridview->setWidth(100);
3263 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3264 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3265 QCOMPARE(itemCount, 6);
3266 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3267 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3268 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3269 QCOMPARE(item->x(), 0.0);
3270 QCOMPARE(item->y(), qreal(i*60));
3276 void tst_QQuickGridView::indexAt_itemAt_data()
3278 QTest::addColumn<qreal>("x");
3279 QTest::addColumn<qreal>("y");
3280 QTest::addColumn<int>("index");
3282 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3283 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3284 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3285 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3286 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3289 void tst_QQuickGridView::indexAt_itemAt()
3295 QQuickView *canvas = getView();
3298 model.addItem("Fred", "12345");
3299 model.addItem("John", "2345");
3300 model.addItem("Bob", "54321");
3301 model.addItem("Billy", "22345");
3302 model.addItem("Sam", "2945");
3303 model.addItem("Ben", "04321");
3304 model.addItem("Jim", "0780");
3306 QQmlContext *ctxt = canvas->rootContext();
3307 ctxt->setContextProperty("testModel", &model);
3308 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3309 ctxt->setContextProperty("testTopToBottom", QVariant(false));
3311 canvas->setSource(testFileUrl("gridview1.qml"));
3312 qApp->processEvents();
3314 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3315 QTRY_VERIFY(gridview != 0);
3317 QQuickItem *contentItem = gridview->contentItem();
3318 QTRY_VERIFY(contentItem != 0);
3320 QTRY_COMPARE(gridview->count(), model.count());
3322 QQuickItem *item = 0;
3324 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3327 QCOMPARE(gridview->indexAt(x, y), index);
3328 QVERIFY(gridview->itemAt(x, y) == item);
3330 releaseView(canvas);
3333 void tst_QQuickGridView::onAdd()
3335 QFETCH(int, initialItemCount);
3336 QFETCH(int, itemsToAdd);
3338 const int delegateWidth = 50;
3339 const int delegateHeight = 100;
3341 QQuickView *canvas = getView();
3342 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3344 // these initial items should not trigger GridView.onAdd
3345 for (int i=0; i<initialItemCount; i++)
3346 model.addItem("dummy value", "dummy value");
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"));
3354 QObject *object = canvas->rootObject();
3355 object->setProperty("width", canvas->width());
3356 object->setProperty("height", canvas->height());
3357 qApp->processEvents();
3359 QList<QPair<QString, QString> > items;
3360 for (int i=0; i<itemsToAdd; i++)
3361 items << qMakePair(QString("value %1").arg(i), QString::number(i));
3362 model.addItems(items);
3364 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3365 qApp->processEvents();
3367 QVariantList result = object->property("addedDelegates").toList();
3368 QTRY_COMPARE(result.count(), items.count());
3369 for (int i=0; i<items.count(); i++)
3370 QCOMPARE(result[i].toString(), items[i].first);
3372 releaseView(canvas);
3375 void tst_QQuickGridView::onAdd_data()
3377 QTest::addColumn<int>("initialItemCount");
3378 QTest::addColumn<int>("itemsToAdd");
3380 QTest::newRow("0, add 1") << 0 << 1;
3381 QTest::newRow("0, add 2") << 0 << 2;
3382 QTest::newRow("0, add 10") << 0 << 10;
3384 QTest::newRow("1, add 1") << 1 << 1;
3385 QTest::newRow("1, add 2") << 1 << 2;
3386 QTest::newRow("1, add 10") << 1 << 10;
3388 QTest::newRow("5, add 1") << 5 << 1;
3389 QTest::newRow("5, add 2") << 5 << 2;
3390 QTest::newRow("5, add 10") << 5 << 10;
3393 void tst_QQuickGridView::onRemove()
3395 QFETCH(int, initialItemCount);
3396 QFETCH(int, indexToRemove);
3397 QFETCH(int, removeCount);
3399 const int delegateWidth = 50;
3400 const int delegateHeight = 100;
3402 for (int i=0; i<initialItemCount; i++)
3403 model.addItem(QString("value %1").arg(i), "dummy value");
3405 QQuickView *canvas = getView();
3406 QQmlContext *ctxt = canvas->rootContext();
3407 ctxt->setContextProperty("testModel", &model);
3408 ctxt->setContextProperty("delegateWidth", delegateWidth);
3409 ctxt->setContextProperty("delegateHeight", delegateHeight);
3410 canvas->setSource(testFileUrl("attachedSignals.qml"));
3411 QObject *object = canvas->rootObject();
3413 model.removeItems(indexToRemove, removeCount);
3414 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3415 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
3417 releaseView(canvas);
3420 void tst_QQuickGridView::onRemove_data()
3422 QTest::addColumn<int>("initialItemCount");
3423 QTest::addColumn<int>("indexToRemove");
3424 QTest::addColumn<int>("removeCount");
3426 QTest::newRow("remove first") << 1 << 0 << 1;
3427 QTest::newRow("two items, remove first") << 2 << 0 << 1;
3428 QTest::newRow("two items, remove last") << 2 << 1 << 1;
3429 QTest::newRow("two items, remove all") << 2 << 0 << 2;
3431 QTest::newRow("four items, remove first") << 4 << 0 << 1;
3432 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
3433 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
3434 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
3435 QTest::newRow("four items, remove last") << 4 << 3 << 1;
3436 QTest::newRow("four items, remove all") << 4 << 0 << 4;
3438 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
3439 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
3440 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
3443 void tst_QQuickGridView::columnCount()
3446 canvas.setSource(testFileUrl("gridview4.qml"));
3448 canvas.requestActivateWindow();
3449 QTest::qWaitForWindowShown(&canvas);
3451 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
3453 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
3454 QCOMPARE(view->cellHeight(), qreal(100));
3456 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
3457 QCOMPARE(items.size(), 18);
3458 QCOMPARE(items.at(8)->y(), qreal(0));
3459 QCOMPARE(items.at(9)->y(), qreal(100));
3462 void tst_QQuickGridView::margins()
3465 QQuickView *canvas = createView();
3468 for (int i = 0; i < 40; i++)
3469 model.addItem("Item" + QString::number(i), "");
3471 QQmlContext *ctxt = canvas->rootContext();
3472 ctxt->setContextProperty("testModel", &model);
3473 ctxt->setContextProperty("testRightToLeft", QVariant(false));
3475 canvas->setSource(testFileUrl("margins.qml"));
3477 qApp->processEvents();
3479 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3480 QTRY_VERIFY(gridview != 0);
3481 QQuickItem *contentItem = gridview->contentItem();
3482 QTRY_VERIFY(contentItem != 0);
3483 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3485 QCOMPARE(gridview->contentX(), -30.);
3486 QCOMPARE(gridview->xOrigin(), 0.);
3489 gridview->positionViewAtEnd();
3490 qreal pos = gridview->contentX();
3491 gridview->setContentX(pos + 80);
3492 gridview->returnToBounds();
3493 QTRY_COMPARE(gridview->contentX(), pos + 50);
3495 // remove item before visible and check that left margin is maintained
3496 // and xOrigin is updated
3497 gridview->setContentX(200);
3498 model.removeItems(0, 4);
3500 gridview->setContentX(-50);
3501 gridview->returnToBounds();
3502 QCOMPARE(gridview->xOrigin(), 100.);
3503 QTRY_COMPARE(gridview->contentX(), 70.);
3505 // reduce left margin
3506 gridview->setLeftMargin(20);
3507 QCOMPARE(gridview->xOrigin(), 100.);
3508 QTRY_COMPARE(gridview->contentX(), 80.);
3511 gridview->positionViewAtEnd();
3512 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3513 pos = gridview->contentX();
3514 gridview->setContentX(pos + 80);
3515 gridview->returnToBounds();
3516 QTRY_COMPARE(gridview->contentX(), pos + 50);
3518 // reduce right margin
3519 pos = gridview->contentX();
3520 gridview->setRightMargin(40);
3521 QCOMPARE(gridview->xOrigin(), 0.);
3522 QTRY_COMPARE(gridview->contentX(), pos-10);
3528 QQuickView *canvas = createView();
3532 for (int i = 0; i < 40; i++)
3533 model.addItem("Item" + QString::number(i), "");
3535 QQmlContext *ctxt = canvas->rootContext();
3536 ctxt->setContextProperty("testModel", &model);
3537 ctxt->setContextProperty("testRightToLeft", QVariant(true));
3539 canvas->setSource(testFileUrl("margins.qml"));
3540 qApp->processEvents();
3542 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3543 QTRY_VERIFY(gridview != 0);
3545 QQuickItem *contentItem = gridview->contentItem();
3546 QTRY_VERIFY(contentItem != 0);
3548 QCOMPARE(gridview->contentX(), -240+30.);
3549 QCOMPARE(gridview->xOrigin(), 0.);
3552 gridview->positionViewAtEnd();
3553 qreal pos = gridview->contentX();
3554 gridview->setContentX(pos - 80);
3555 gridview->returnToBounds();
3556 QTRY_COMPARE(gridview->contentX(), pos - 50);
3558 // remove item before visible and check that left margin is maintained
3559 // and xOrigin is updated
3560 gridview->setContentX(-400);
3561 model.removeItems(0, 4);
3563 gridview->setContentX(-240+50);
3564 gridview->returnToBounds();
3565 QCOMPARE(gridview->xOrigin(), -100.);
3566 QTRY_COMPARE(gridview->contentX(), -240-70.);
3568 // reduce left margin (i.e. right side due to RTL)
3569 pos = gridview->contentX();
3570 gridview->setLeftMargin(20);
3571 QCOMPARE(gridview->xOrigin(), -100.);
3572 QTRY_COMPARE(gridview->contentX(), -240-80.);
3575 gridview->positionViewAtEnd();
3576 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
3577 pos = gridview->contentX();
3578 gridview->setContentX(pos - 80);
3579 gridview->returnToBounds();
3580 QTRY_COMPARE(gridview->contentX(), pos - 50);
3582 // reduce right margin (i.e. left side due to RTL)
3583 pos = gridview->contentX();
3584 gridview->setRightMargin(40);
3585 QCOMPARE(gridview->xOrigin(), 0.);
3586 QTRY_COMPARE(gridview->contentX(), pos+10);
3592 void tst_QQuickGridView::creationContext()
3595 canvas.setGeometry(0,0,240,320);
3596 canvas.setSource(testFileUrl("creationContext.qml"));
3597 qApp->processEvents();
3599 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
3601 QVERIFY(rootItem->property("count").toInt() > 0);
3604 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
3605 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3606 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
3607 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3608 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
3609 QCOMPARE(item->property("text").toString(), QString("Hello!"));
3612 void tst_QQuickGridView::snapToRow_data()
3614 QTest::addColumn<QQuickGridView::Flow>("flow");
3615 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3616 QTest::addColumn<int>("highlightRangeMode");
3617 QTest::addColumn<QPoint>("flickStart");
3618 QTest::addColumn<QPoint>("flickEnd");
3619 QTest::addColumn<qreal>("snapAlignment");
3620 QTest::addColumn<qreal>("endExtent");
3621 QTest::addColumn<qreal>("startExtent");
3623 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3624 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
3626 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3627 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
3629 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3630 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
3632 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3633 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
3635 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3636 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
3638 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3639 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
3642 void tst_QQuickGridView::snapToRow()
3644 QFETCH(QQuickGridView::Flow, flow);
3645 QFETCH(Qt::LayoutDirection, layoutDirection);
3646 QFETCH(int, highlightRangeMode);
3647 QFETCH(QPoint, flickStart);
3648 QFETCH(QPoint, flickEnd);
3649 QFETCH(qreal, snapAlignment);
3650 QFETCH(qreal, endExtent);
3651 QFETCH(qreal, startExtent);
3653 QQuickView *canvas = getView();
3655 canvas->setSource(testFileUrl("snapToRow.qml"));
3657 qApp->processEvents();
3659 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3660 QTRY_VERIFY(gridview != 0);
3662 gridview->setFlow(flow);
3663 gridview->setLayoutDirection(layoutDirection);
3664 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3665 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3667 QQuickItem *contentItem = gridview->contentItem();
3668 QTRY_VERIFY(contentItem != 0);
3670 // confirm that a flick hits an item boundary
3671 flick(canvas, flickStart, flickEnd, 180);
3672 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3673 if (flow == QQuickGridView::LeftToRight)
3674 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
3676 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
3680 flick(canvas, flickStart, flickEnd, 180);
3681 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3682 } while (flow == QQuickGridView::LeftToRight
3683 ? !gridview->isAtYEnd()
3684 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3686 if (flow == QQuickGridView::LeftToRight)
3687 QCOMPARE(gridview->contentY(), endExtent);
3689 QCOMPARE(gridview->contentX(), endExtent);
3693 flick(canvas, flickEnd, flickStart, 180);
3694 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3695 } while (flow == QQuickGridView::LeftToRight
3696 ? !gridview->isAtYBeginning()
3697 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3699 if (flow == QQuickGridView::LeftToRight)
3700 QCOMPARE(gridview->contentY(), startExtent);
3702 QCOMPARE(gridview->contentX(), startExtent);
3704 releaseView(canvas);
3707 void tst_QQuickGridView::snapOneRow_data()
3709 QTest::addColumn<QQuickGridView::Flow>("flow");
3710 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3711 QTest::addColumn<int>("highlightRangeMode");
3712 QTest::addColumn<QPoint>("flickStart");
3713 QTest::addColumn<QPoint>("flickEnd");
3714 QTest::addColumn<qreal>("snapAlignment");
3715 QTest::addColumn<qreal>("endExtent");
3716 QTest::addColumn<qreal>("startExtent");
3718 QTest::newRow("vertical, left to right") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3719 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
3721 QTest::newRow("horizontal, left to right") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
3722 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
3724 QTest::newRow("horizontal, right to left") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
3725 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
3727 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::LeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3728 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
3730 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::TopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
3731 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
3733 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::TopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
3734 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
3737 void tst_QQuickGridView::snapOneRow()
3739 QFETCH(QQuickGridView::Flow, flow);
3740 QFETCH(Qt::LayoutDirection, layoutDirection);
3741 QFETCH(int, highlightRangeMode);
3742 QFETCH(QPoint, flickStart);
3743 QFETCH(QPoint, flickEnd);
3744 QFETCH(qreal, snapAlignment);
3745 QFETCH(qreal, endExtent);
3746 QFETCH(qreal, startExtent);
3748 QQuickView *canvas = getView();
3750 canvas->setSource(testFileUrl("snapOneRow.qml"));
3752 qApp->processEvents();
3754 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3755 QTRY_VERIFY(gridview != 0);
3757 gridview->setFlow(flow);
3758 gridview->setLayoutDirection(layoutDirection);
3759 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
3760 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3762 QQuickItem *contentItem = gridview->contentItem();
3763 QTRY_VERIFY(contentItem != 0);
3765 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
3767 // confirm that a flick hits next row boundary
3768 flick(canvas, flickStart, flickEnd, 180);
3769 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3770 if (flow == QQuickGridView::LeftToRight)
3771 QCOMPARE(gridview->contentY(), snapAlignment);
3773 QCOMPARE(gridview->contentX(), snapAlignment);
3775 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3776 QCOMPARE(gridview->currentIndex(), 2);
3777 QCOMPARE(currentIndexSpy.count(), 1);
3782 flick(canvas, flickStart, flickEnd, 180);
3783 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3784 } while (flow == QQuickGridView::LeftToRight
3785 ? !gridview->isAtYEnd()
3786 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
3788 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3789 QCOMPARE(gridview->currentIndex(), 6);
3790 QCOMPARE(currentIndexSpy.count(), 3);
3793 if (flow == QQuickGridView::LeftToRight)
3794 QCOMPARE(gridview->contentY(), endExtent);
3796 QCOMPARE(gridview->contentX(), endExtent);
3800 flick(canvas, flickEnd, flickStart, 180);
3801 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
3802 } while (flow == QQuickGridView::LeftToRight
3803 ? !gridview->isAtYBeginning()
3804 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
3806 if (flow == QQuickGridView::LeftToRight)
3807 QCOMPARE(gridview->contentY(), startExtent);
3809 QCOMPARE(gridview->contentX(), startExtent);
3811 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
3812 QCOMPARE(gridview->currentIndex(), 0);
3813 QCOMPARE(currentIndexSpy.count(), 6);
3816 releaseView(canvas);
3820 void tst_QQuickGridView::unaligned()
3822 QQuickView *canvas = createView();
3826 for (int i = 0; i < 10; i++)
3827 model.addItem("Item" + QString::number(i), "");
3829 QQmlContext *ctxt = canvas->rootContext();
3830 ctxt->setContextProperty("testModel", &model);
3832 canvas->setSource(testFileUrl("unaligned.qml"));
3833 qApp->processEvents();
3835 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3836 QVERIFY(gridview != 0);
3838 QQuickItem *contentItem = gridview->contentItem();
3839 QVERIFY(contentItem != 0);
3841 for (int i = 0; i < 10; ++i) {
3842 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3843 if (!item) qWarning() << "Item" << i << "not found";
3845 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3846 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3850 for (int i = 10; i < 18; ++i) {
3851 model.addItem("Item" + QString::number(i), "");
3852 QQuickItem *item = 0;
3853 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3854 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3855 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3859 for (int i = 0; i < 10; ++i) {
3860 model.insertItem(i, "Item" + QString::number(i), "");
3861 QQuickItem *item = 0;
3862 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3863 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
3864 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
3868 model.removeItems(7, 10);
3869 QTRY_COMPARE(model.count(), gridview->count());
3870 for (int i = 0; i < 18; ++i) {
3871 QQuickItem *item = 0;
3872 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
3873 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
3874 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
3880 void tst_QQuickGridView::populateTransitions()
3882 QFETCH(bool, staticallyPopulate);
3883 QFETCH(bool, dynamicallyPopulate);
3884 QFETCH(bool, usePopulateTransition);
3886 QPointF transitionFrom(-50, -50);
3887 QPointF transitionVia(100, 100);
3888 QaimModel model_transitionFrom;
3889 QaimModel model_transitionVia;
3892 if (staticallyPopulate) {
3893 for (int i = 0; i < 30; i++)
3894 model.addItem("item" + QString::number(i), "");
3897 QQuickView *canvas = getView();
3898 canvas->rootContext()->setContextProperty("testModel", &model);
3899 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
3900 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
3901 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
3902 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
3903 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
3904 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
3905 canvas->setSource(testFileUrl("populateTransitions.qml"));
3908 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3910 QQuickItem *contentItem = gridview->contentItem();
3911 QVERIFY(contentItem);
3913 if (staticallyPopulate || dynamicallyPopulate) {
3914 // check the populate transition is run
3915 if (usePopulateTransition) {
3916 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 19);
3918 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3919 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
3921 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3923 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3926 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3927 if (usePopulateTransition)
3928 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3929 for (int i=0; i < model.count() && i < itemCount; ++i) {
3930 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3931 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3932 QCOMPARE(item->x(), (i%3)*80.0);
3933 QCOMPARE(item->y(), (i/3)*60.0);
3934 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3936 QTRY_COMPARE(name->text(), model.name(i));
3939 // add an item and check this is done with add transition, not populate
3940 model.insertItem(0, "another item", "");
3941 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
3942 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(),
3943 (usePopulateTransition && (staticallyPopulate || dynamicallyPopulate)) ? 19 : 0);
3946 canvas->rootContext()->setContextProperty("testModel", QVariant());
3947 QTRY_COMPARE(gridview->count(), 0);
3948 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
3949 gridview->setProperty("countPopulateTransitions", 0);
3950 gridview->setProperty("countAddTransitions", 0);
3952 // set to a valid model and check populate transition is run a second time
3954 for (int i = 0; i < 30; i++)
3955 model.addItem("item" + QString::number(i), "");
3956 canvas->rootContext()->setContextProperty("testModel", &model);
3957 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3959 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
3960 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3962 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3963 if (usePopulateTransition)
3964 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3965 for (int i=0; i < model.count() && i < itemCount; ++i) {
3966 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3967 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3968 QCOMPARE(item->x(), (i%3)*80.0);
3969 QCOMPARE(item->y(), (i/3)*60.0);
3970 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3972 QTRY_COMPARE(name->text(), model.name(i));
3975 // reset model and check populate transition is run again
3976 gridview->setProperty("countPopulateTransitions", 0);
3977 gridview->setProperty("countAddTransitions", 0);
3979 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 19 : 0);
3980 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
3982 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3983 if (usePopulateTransition)
3984 QCOMPARE(itemCount, gridview->property("countPopulateTransitions").toInt());
3985 for (int i=0; i < model.count() && i < itemCount; ++i) {
3986 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3987 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3988 QCOMPARE(item->x(), (i%3)*80.0);
3989 QCOMPARE(item->y(), (i/3)*60.0);
3990 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3992 QTRY_COMPARE(name->text(), model.name(i));
3995 releaseView(canvas);
3998 void tst_QQuickGridView::populateTransitions_data()
4000 QTest::addColumn<bool>("staticallyPopulate");
4001 QTest::addColumn<bool>("dynamicallyPopulate");
4002 QTest::addColumn<bool>("usePopulateTransition");
4004 QTest::newRow("static") << true << false << true;
4005 QTest::newRow("static, no populate") << true << false << false;
4007 QTest::newRow("dynamic") << false << true << true;
4008 QTest::newRow("dynamic, no populate") << false << true << false;
4010 QTest::newRow("empty to start with") << false << false << true;
4011 QTest::newRow("empty to start with, no populate") << false << false << false;
4014 void tst_QQuickGridView::addTransitions()
4016 QFETCH(int, initialItemCount);
4017 QFETCH(bool, shouldAnimateTargets);
4018 QFETCH(qreal, contentY);
4019 QFETCH(int, insertionIndex);
4020 QFETCH(int, insertionCount);
4021 QFETCH(ListRange, expectedDisplacedIndexes);
4023 // added items should start here
4024 QPointF targetItems_transitionFrom(-50, -50);
4026 // displaced items should pass through this point
4027 QPointF displacedItems_transitionVia(100, 100);
4030 for (int i = 0; i < initialItemCount; i++)
4031 model.addItem("Original item" + QString::number(i), "");
4032 QaimModel model_targetItems_transitionFrom;
4033 QaimModel model_displacedItems_transitionVia;
4035 QQuickView *canvas = getView();
4036 QQmlContext *ctxt = canvas->rootContext();
4037 ctxt->setContextProperty("testModel", &model);
4038 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4039 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4040 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4041 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4042 canvas->setSource(testFileUrl("addTransitions.qml"));
4045 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4046 QTRY_VERIFY(gridview != 0);
4047 QQuickItem *contentItem = gridview->contentItem();
4048 QVERIFY(contentItem != 0);
4049 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4051 if (contentY != 0) {
4052 gridview->setContentY(contentY);
4053 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4056 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4058 // only target items that will become visible should be animated
4059 QList<QPair<QString, QString> > newData;
4060 QList<QPair<QString, QString> > expectedTargetData;
4061 QList<int> targetIndexes;
4062 if (shouldAnimateTargets) {
4063 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4064 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4066 // last visible item is the first item of the row beneath the view
4067 if (i >= (contentY / 60)*3 && i < qCeil((contentY + gridview->height()) / 60.0)*3) {
4068 expectedTargetData << newData.last();
4072 QVERIFY(expectedTargetData.count() > 0);
4076 if (!newData.isEmpty()) {
4077 model.insertItems(insertionIndex, newData);
4078 QTRY_COMPARE(model.count(), gridview->count());
4081 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4083 if (shouldAnimateTargets) {
4084 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4085 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4086 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4088 // check the target and displaced items were animated
4089 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4090 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4092 // check attached properties
4093 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4094 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4095 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4096 if (expectedDisplacedIndexes.isValid()) {
4097 // adjust expectedDisplacedIndexes to their final values after the move
4098 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4099 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4100 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4101 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4104 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4105 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4108 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4109 int firstVisibleIndex = -1;
4110 for (int i=0; i<items.count(); i++) {
4111 if (items[i]->y() >= contentY) {
4112 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4113 firstVisibleIndex = e.evaluate().toInt();
4117 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4119 // verify all items moved to the correct final positions
4120 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4121 for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4122 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4123 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4124 QCOMPARE(item->x(), (i%3)*80.0);
4125 QCOMPARE(item->y(), (i/3)*60.0);
4126 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4128 QCOMPARE(name->text(), model.name(i));
4131 releaseView(canvas);
4134 void tst_QQuickGridView::addTransitions_data()
4136 QTest::addColumn<int>("initialItemCount");
4137 QTest::addColumn<qreal>("contentY");
4138 QTest::addColumn<bool>("shouldAnimateTargets");
4139 QTest::addColumn<int>("insertionIndex");
4140 QTest::addColumn<int>("insertionCount");
4141 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4143 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4144 QTest::newRow("insert 1, just before start")
4145 << 30 << 20.0 << false
4146 << 0 << 1 << ListRange();
4147 QTest::newRow("insert 1, way before start")
4148 << 30 << 20.0 << false
4149 << 0 << 1 << ListRange();
4150 QTest::newRow("insert multiple, just before start")
4151 << 30 << 100.0 << false
4152 << 0 << 3 << ListRange();
4153 QTest::newRow("insert multiple (< 1 row), just before start")
4154 << 30 << 100.0 << false
4155 << 0 << 2 << ListRange();
4156 QTest::newRow("insert multiple, way before start")
4157 << 30 << 100.0 << false
4158 << 0 << 3 << ListRange();
4160 QTest::newRow("insert 1 at start")
4161 << 30 << 0.0 << true
4162 << 0 << 1 << ListRange(0, 17);
4163 QTest::newRow("insert multiple at start")
4164 << 30 << 0.0 << true
4165 << 0 << 3 << ListRange(0, 17);
4166 QTest::newRow("insert multiple (> 1 row) at start")
4167 << 30 << 0.0 << true
4168 << 0 << 5 << ListRange(0, 17);
4169 QTest::newRow("insert 1 at start, content y not 0")
4170 << 30 << 60.0 << true // first visible is index 3
4171 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4172 QTest::newRow("insert multiple at start, content y not 0")
4173 << 30 << 60.0 << true // first visible is index 3
4174 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4175 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4176 << 30 << 60.0 << true // first visible is index 3
4177 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4179 QTest::newRow("insert 1 at start, to empty grid")
4181 << 0 << 1 << ListRange();
4182 QTest::newRow("insert multiple at start, to empty grid")
4184 << 0 << 3 << ListRange();
4186 QTest::newRow("insert 1 at middle")
4187 << 30 << 0.0 << true
4188 << 7 << 1 << ListRange(7, 17);
4189 QTest::newRow("insert multiple at middle")
4190 << 30 << 0.0 << true
4191 << 7 << 3 << ListRange(7, 17);
4192 QTest::newRow("insert multiple (> 1 row) at middle")
4193 << 30 << 0.0 << true
4194 << 7 << 5 << ListRange(7, 17);
4196 QTest::newRow("insert 1 at bottom")
4197 << 30 << 0.0 << true
4198 << 17 << 1 << ListRange(17, 17);
4199 QTest::newRow("insert multiple at bottom")
4200 << 30 << 0.0 << true
4201 << 17 << 3 << ListRange(17, 17);
4202 QTest::newRow("insert 1 at bottom, content y not 0")
4203 << 30 << 20.0 * 3 << true
4204 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4205 QTest::newRow("insert multiple at bottom, content y not 0")
4206 << 30 << 20.0 * 3 << true
4207 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4210 // items added after the last visible will not be animated in, since they
4211 // do not appear in the final view
4212 QTest::newRow("insert 1 after end")
4213 << 30 << 0.0 << false
4214 << 18 << 1 << ListRange();
4215 QTest::newRow("insert multiple after end")
4216 << 30 << 0.0 << false
4217 << 18 << 3 << ListRange();
4220 void tst_QQuickGridView::moveTransitions()
4222 QFETCH(int, initialItemCount);
4223 QFETCH(qreal, contentY);
4224 QFETCH(qreal, itemsOffsetAfterMove);
4225 QFETCH(int, moveFrom);
4226 QFETCH(int, moveTo);
4227 QFETCH(int, moveCount);
4228 QFETCH(ListRange, expectedDisplacedIndexes);
4230 // target and displaced items should pass through these points
4231 QPointF targetItems_transitionVia(-50, 50);
4232 QPointF displacedItems_transitionVia(100, 100);
4235 for (int i = 0; i < initialItemCount; i++)
4236 model.addItem("Original item" + QString::number(i), "");
4237 QaimModel model_targetItems_transitionVia;
4238 QaimModel model_displacedItems_transitionVia;
4240 QQuickView *canvas = getView();
4241 QQmlContext *ctxt = canvas->rootContext();
4242 ctxt->setContextProperty("testModel", &model);
4243 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4244 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4245 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4246 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4247 canvas->setSource(testFileUrl("moveTransitions.qml"));
4250 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4251 QTRY_VERIFY(gridview != 0);
4252 QQuickItem *contentItem = gridview->contentItem();
4253 QVERIFY(contentItem != 0);
4256 if (contentY != 0) {
4257 gridview->setContentY(contentY);
4258 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4261 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4263 // Items moving to *or* from visible positions should be animated.
4264 // Otherwise, they should not be animated.
4265 QList<QPair<QString, QString> > expectedTargetData;
4266 QList<int> targetIndexes;
4267 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4268 int toIndex = moveTo + (i - moveFrom);
4269 int firstVisibleIndex = (contentY / 60) * 3;
4270 int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1;
4271 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4272 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4273 expectedTargetData << qMakePair(model.name(i), model.number(i));
4277 // ViewTransition.index provides the indices that items are moving to, not from
4278 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4281 model.moveItems(moveFrom, moveTo, moveCount);
4283 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4284 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4285 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4287 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4289 // check the target and displaced items were animated
4290 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4291 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4293 // check attached properties
4294 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4295 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4296 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4297 if (expectedDisplacedIndexes.isValid()) {
4298 // adjust expectedDisplacedIndexes to their final values after the move
4299 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4300 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4301 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4302 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4305 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4306 int firstVisibleIndex = -1;
4307 for (int i=0; i<items.count(); i++) {
4308 if (items[i]->y() >= contentY) {
4309 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4310 firstVisibleIndex = e.evaluate().toInt();
4314 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4316 // verify all items moved to the correct final positions
4317 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4318 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4319 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4320 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4321 QCOMPARE(item->x(), (i%3)*80.0);
4322 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
4323 name = findItem<QQuickText>(contentItem, "textName", i);
4325 QTRY_COMPARE(name->text(), model.name(i));
4328 releaseView(canvas);
4331 void tst_QQuickGridView::moveTransitions_data()
4333 QTest::addColumn<int>("initialItemCount");
4334 QTest::addColumn<qreal>("contentY");
4335 QTest::addColumn<qreal>("itemsOffsetAfterMove");
4336 QTest::addColumn<int>("moveFrom");
4337 QTest::addColumn<int>("moveTo");
4338 QTest::addColumn<int>("moveCount");
4339 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4341 QTest::newRow("move from above view, outside visible items, move 1") << 30 << 120.0 << 0.0
4342 << 1 << 10 << 1 << ListRange(6, 10);
4343 QTest::newRow("move from above view, outside visible items, move 1 (first item)") << 30 << 120.0 << 0.0
4344 << 0 << 10 << 1 << ListRange(6, 10);
4345 QTest::newRow("move from above view, outside visible items, move multiple") << 30 << 120.0 << 60.0
4346 << 1 << 10 << 3 << ListRange(13, 23);
4347 QTest::newRow("move from above view, mix of visible/non-visible") << 30 << 120.0 << 60.0
4348 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4349 QTest::newRow("move from above view, mix of visible/non-visible (move first)") << 30 << 120.0 << 120.0
4350 << 0 << 10 << 6 << ListRange(16, 23);
4352 QTest::newRow("move within view, move 1 down") << 30 << 0.0 << 0.0
4353 << 1 << 10 << 1 << ListRange(2, 10);
4354 QTest::newRow("move within view, move 1 down, move first item") << 30 << 0.0 << 0.0
4355 << 0 << 10 << 1 << ListRange(1, 10);
4356 QTest::newRow("move within view, move 1 down, move first item, contentY not 0") << 30 << 120.0 << 0.0
4357 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
4358 QTest::newRow("move within view, move 1 down, to last item") << 30 << 0.0 << 0.0
4359 << 10 << 17 << 1 << ListRange(11, 17);
4360 QTest::newRow("move within view, move first->last") << 30 << 0.0 << 0.0
4361 << 0 << 17 << 1 << ListRange(1, 17);
4363 QTest::newRow("move within view, move multiple down") << 30 << 0.0 << 0.0
4364 << 1 << 10 << 3 << ListRange(4, 12);
4365 QTest::newRow("move within view, move multiple down, move first item") << 30 << 0.0 << 0.0
4366 << 0 << 10 << 3 << ListRange(3, 12);
4367 QTest::newRow("move within view, move multiple down, move first item, contentY not 0") << 30 << 60.0 << 0.0
4368 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
4369 QTest::newRow("move within view, move multiple down, displace last item") << 30 << 0.0 << 0.0
4370 << 5 << 15 << 3 << ListRange(8, 17);
4371 QTest::newRow("move within view, move multiple down, move first->last") << 30 << 0.0 << 0.0
4372 << 0 << 15 << 3 << ListRange(3, 17);
4374 QTest::newRow("move within view, move 1 up") << 30 << 0.0 << 0.0
4375 << 10 << 1 << 1 << ListRange(1, 9);
4376 QTest::newRow("move within view, move 1 up, move to first index") << 30 << 0.0 << 0.0
4377 << 10 << 0 << 1 << ListRange(0, 9);
4378 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0") << 30 << 120.0 << 0.0
4379 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
4380 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border") << 30 << 80.0 << 0.0
4381 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
4382 QTest::newRow("move within view, move 1 up, move last item") << 30 << 0.0 << 0.0
4383 << 17 << 10 << 1 << ListRange(10, 16);
4384 QTest::newRow("move within view, move 1 up, move last->first") << 30 << 0.0 << 0.0
4385 << 17 << 0 << 1 << ListRange(0, 16);
4387 QTest::newRow("move within view, move multiple up") << 30 << 0.0 << 0.0
4388 << 10 << 1 << 3 << ListRange(1, 9);
4389 QTest::newRow("move within view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0
4390 << 10 << 1 << 5 << ListRange(1, 9);
4391 QTest::newRow("move within view, move multiple up, move to first index") << 30 << 0.0 << 0.0
4392 << 10 << 0 << 3 << ListRange(0, 9);
4393 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0") << 30 << 60.0 << 0.0
4394 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
4395 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border") << 30 << 80.0 << 0.0
4396 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
4397 QTest::newRow("move within view, move multiple up, move last item") << 30 << 0.0 << 0.0
4398 << 15 << 5 << 3 << ListRange(5, 14);
4399 QTest::newRow("move within view, move multiple up, move last->first") << 30 << 0.0 << 0.0
4400 << 15 << 0 << 3 << ListRange(0, 14);
4402 QTest::newRow("move from below view, move 1 up") << 30 << 0.0 << 0.0
4403 << 20 << 5 << 1 << ListRange(5, 17);
4404 QTest::newRow("move from below view, move 1 up, move to top") << 30 << 0.0 << 0.0
4405 << 20 << 0 << 1 << ListRange(0, 17);
4406 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0") << 30 << 60.0 << 0.0
4407 << 25 << 3 << 1 << ListRange(0+3, 17+3);
4408 QTest::newRow("move from below view, move multiple (> 1 row) up") << 30 << 0.0 << 0.0
4409 << 20 << 5 << 5 << ListRange(5, 17);
4410 QTest::newRow("move from below view, move multiple up, move to top") << 30 << 0.0 << 0.0
4411 << 20 << 0 << 3 << ListRange(0, 17);
4412 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0") << 30 << 60.0 << 0.0
4413 << 25 << 3 << 3 << ListRange(0+3, 17+3);
4415 QTest::newRow("move from below view, move 1 up, move to bottom") << 30 << 0.0 << 0.0
4416 << 20 << 17 << 1 << ListRange(17, 17);
4417 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0
4418 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
4419 QTest::newRow("move from below view, move multiple up, move to to bottom") << 30 << 0.0 << 0.0
4420 << 20 << 17 << 3 << ListRange(17, 17);
4421 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0") << 30 << 60.0 << 0.0
4422 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
4425 void tst_QQuickGridView::removeTransitions()
4427 QFETCH(int, initialItemCount);
4428 QFETCH(bool, shouldAnimateTargets);
4429 QFETCH(qreal, contentY);
4430 QFETCH(int, removalIndex);
4431 QFETCH(int, removalCount);
4432 QFETCH(ListRange, expectedDisplacedIndexes);
4434 // added items should end here
4435 QPointF targetItems_transitionTo(-50, -50);
4437 // displaced items should pass through this points
4438 QPointF displacedItems_transitionVia(100, 100);
4441 for (int i = 0; i < initialItemCount; i++)
4442 model.addItem("Original item" + QString::number(i), "");
4443 QaimModel model_targetItems_transitionTo;
4444 QaimModel model_displacedItems_transitionVia;
4446 QQuickView *canvas = getView();
4447 QQmlContext *ctxt = canvas->rootContext();
4448 ctxt->setContextProperty("testModel", &model);
4449 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
4450 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4451 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
4452 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4453 canvas->setSource(testFileUrl("removeTransitions.qml"));
4456 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4457 QTRY_VERIFY(gridview != 0);
4458 QQuickItem *contentItem = gridview->contentItem();
4459 QVERIFY(contentItem != 0);
4460 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4462 if (contentY != 0) {
4463 gridview->setContentY(contentY);
4464 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4467 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4469 // only target items that are visible should be animated
4470 QList<QPair<QString, QString> > expectedTargetData;
4471 QList<int> targetIndexes;
4472 if (shouldAnimateTargets) {
4473 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
4474 int firstVisibleIndex = (contentY / 60.0)*3;
4475 int lastVisibleIndex = (qCeil((contentY + gridview->height()) / 60.0)*3) - 1;
4476 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
4477 expectedTargetData << qMakePair(model.name(i), model.number(i));
4481 QVERIFY(expectedTargetData.count() > 0);
4484 // calculate targetItems and expectedTargets before model changes
4485 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4486 QVariantMap expectedTargets;
4487 for (int i=0; i<targetIndexes.count(); i++)
4488 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
4491 model.removeItems(removalIndex, removalCount);
4492 QTRY_COMPARE(model.count(), gridview->count());
4494 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
4495 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4496 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4497 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4499 // check the target and displaced items were animated
4500 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
4501 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4503 // check attached properties
4504 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
4505 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4506 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4507 if (expectedDisplacedIndexes.isValid()) {
4508 // adjust expectedDisplacedIndexes to their final values after the move
4509 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
4510 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4511 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4512 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4515 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
4516 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4519 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4520 int itemCount = items.count();
4521 int firstVisibleIndex = -1;
4522 for (int i=0; i<items.count(); i++) {
4523 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4524 int index = e.evaluate().toInt();
4525 if (firstVisibleIndex < 0 && items[i]->y() >= contentY)
4526 firstVisibleIndex = index;
4528 itemCount--; // exclude deleted items
4530 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4532 // verify all items moved to the correct final positions
4533 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4534 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4535 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4536 QCOMPARE(item->x(), (i%3)*80.0);
4537 QCOMPARE(item->y(), contentY + ((i-firstVisibleIndex)/3) * 60.0);
4538 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4540 QTRY_COMPARE(name->text(), model.name(i));
4543 releaseView(canvas);
4546 void tst_QQuickGridView::removeTransitions_data()
4548 QTest::addColumn<int>("initialItemCount");
4549 QTest::addColumn<qreal>("contentY");
4550 QTest::addColumn<bool>("shouldAnimateTargets");
4551 QTest::addColumn<int>("removalIndex");
4552 QTest::addColumn<int>("removalCount");
4553 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4555 // All items that are visible following the remove operation should be animated.
4556 // Remove targets that are outside of the view should not be animated.
4558 // For a GridView, removing any number of items other than a full row before the start
4559 // should displace all items in the view
4560 QTest::newRow("remove 1 before start")
4561 << 30 << 120.0 << false
4562 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
4563 QTest::newRow("remove 1 row, before start")
4564 << 30 << 120.0 << false
4565 << 3 << 3 << ListRange();
4566 QTest::newRow("remove between 1-2 rows, before start")
4567 << 30 << 120.0 << false
4568 << 0 << 5 << ListRange(6, 25);
4569 QTest::newRow("remove 2 rows, before start")
4570 << 30 << 120.0 << false
4571 << 0 << 6 << ListRange();
4572 QTest::newRow("remove mix of before and after start")
4573 << 30 << 60.0 << true
4574 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
4577 QTest::newRow("remove 1 from start")
4578 << 30 << 0.0 << true
4579 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
4580 QTest::newRow("remove multiple from start")
4581 << 30 << 0.0 << true
4582 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
4583 QTest::newRow("remove 1 from start, content y not 0")
4584 << 30 << 60.0 << true
4585 << 3 << 1 << ListRange(1 + 3, 18 + 3);
4586 QTest::newRow("remove multiple from start, content y not 0")
4587 << 30 << 60.0 << true
4588 << 3 << 3 << ListRange(3 + 3, 20 + 3);
4591 QTest::newRow("remove 1 from middle")
4592 << 30 << 0.0 << true
4593 << 5 << 1 << ListRange(6, 18);
4594 QTest::newRow("remove multiple from middle")
4595 << 30 << 0.0 << true
4596 << 5 << 3 << ListRange(8, 20);
4599 QTest::newRow("remove 1 from bottom")
4600 << 30 << 0.0 << true
4601 << 17 << 1 << ListRange(18, 18);
4602 QTest::newRow("remove multiple (1 row) from bottom")
4603 << 30 << 0.0 << true
4604 << 15 << 3 << ListRange(18, 20);
4605 QTest::newRow("remove multiple (> 1 row) from bottom")
4606 << 30 << 0.0 << true
4607 << 15 << 5 << ListRange(20, 22);
4608 QTest::newRow("remove 1 from bottom, content y not 0")
4609 << 30 << 60.0 << true
4610 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
4611 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
4612 << 30 << 60.0 << true
4613 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
4616 QTest::newRow("remove 1 after end")
4617 << 30 << 0.0 << false
4618 << 18 << 1 << ListRange();
4619 QTest::newRow("remove multiple after end")
4620 << 30 << 0.0 << false
4621 << 18 << 3 << ListRange();
4624 void tst_QQuickGridView::displacedTransitions()
4626 QFETCH(bool, useDisplaced);
4627 QFETCH(bool, displacedEnabled);
4628 QFETCH(bool, useAddDisplaced);
4629 QFETCH(bool, addDisplacedEnabled);
4630 QFETCH(bool, useMoveDisplaced);
4631 QFETCH(bool, moveDisplacedEnabled);
4632 QFETCH(bool, useRemoveDisplaced);
4633 QFETCH(bool, removeDisplacedEnabled);
4634 QFETCH(ListChange, change);
4635 QFETCH(ListRange, expectedDisplacedIndexes);
4638 for (int i = 0; i < 30; i++)
4639 model.addItem("Original item" + QString::number(i), "");
4640 QaimModel model_displaced_transitionVia;
4641 QaimModel model_addDisplaced_transitionVia;
4642 QaimModel model_moveDisplaced_transitionVia;
4643 QaimModel model_removeDisplaced_transitionVia;
4645 QPointF displaced_transitionVia(-50, -100);
4646 QPointF addDisplaced_transitionVia(-150, 100);
4647 QPointF moveDisplaced_transitionVia(50, -100);
4648 QPointF removeDisplaced_transitionVia(150, 100);
4650 QQuickView *canvas = getView();
4651 QQmlContext *ctxt = canvas->rootContext();
4652 ctxt->setContextProperty("testModel", &model);
4653 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
4654 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
4655 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
4656 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
4657 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
4658 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
4659 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
4660 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
4661 ctxt->setContextProperty("useDisplaced", useDisplaced);
4662 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
4663 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
4664 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
4665 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
4666 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
4667 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
4668 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
4669 canvas->setSource(testFileUrl("displacedTransitions.qml"));
4671 qApp->processEvents();
4673 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4674 QTRY_VERIFY(gridview != 0);
4675 QQuickItem *contentItem = gridview->contentItem();
4676 QVERIFY(contentItem != 0);
4677 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4679 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4680 gridview->setProperty("displaceTransitionsDone", false);
4682 switch (change.type) {
4683 case ListChange::Inserted:
4685 QList<QPair<QString, QString> > targetItemData;
4686 for (int i=change.index; i<change.index + change.count; ++i)
4687 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
4688 model.insertItems(change.index, targetItemData);
4689 QTRY_COMPARE(model.count(), gridview->count());
4692 case ListChange::Removed:
4693 model.removeItems(change.index, change.count);
4694 QTRY_COMPARE(model.count(), gridview->count());
4696 case ListChange::Moved:
4697 model.moveItems(change.index, change.to, change.count);
4698 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4700 case ListChange::SetCurrent:
4701 case ListChange::SetContentY:
4705 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
4706 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
4708 if ((useDisplaced && displacedEnabled)
4709 || (useAddDisplaced && addDisplacedEnabled)
4710 || (useMoveDisplaced && moveDisplacedEnabled)
4711 || (useRemoveDisplaced && removeDisplacedEnabled)) {
4712 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
4714 // check the correct number of target items and indexes were received
4715 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
4716 for (int i=0; i<resultTargetIndexes.count(); i++)
4717 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
4718 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
4719 for (int i=0; i<resultTargetItems.count(); i++)
4720 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
4722 QCOMPARE(resultTargetIndexes.count(), 0);
4723 QCOMPARE(resultTargetItems.count(), 0);
4726 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
4727 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
4729 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
4730 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
4731 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
4733 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
4734 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
4735 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
4737 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
4739 if (useDisplaced && displacedEnabled
4740 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
4741 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
4742 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
4743 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
4745 QCOMPARE(model_displaced_transitionVia.count(), 0);
4748 // verify all items moved to the correct final positions
4749 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4750 for (int i=0; i < model.count() && i < items.count(); ++i) {
4751 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4752 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4753 QCOMPARE(item->x(), (i%3)*80.0);
4754 QCOMPARE(item->y(), (i/3)*60.0);
4755 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4757 QTRY_COMPARE(name->text(), model.name(i));
4760 releaseView(canvas);
4763 void tst_QQuickGridView::displacedTransitions_data()
4765 QTest::addColumn<bool>("useDisplaced");
4766 QTest::addColumn<bool>("displacedEnabled");
4767 QTest::addColumn<bool>("useAddDisplaced");
4768 QTest::addColumn<bool>("addDisplacedEnabled");
4769 QTest::addColumn<bool>("useMoveDisplaced");
4770 QTest::addColumn<bool>("moveDisplacedEnabled");
4771 QTest::addColumn<bool>("useRemoveDisplaced");
4772 QTest::addColumn<bool>("removeDisplacedEnabled");
4773 QTest::addColumn<ListChange>("change");
4774 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4776 QTest::newRow("no displaced transitions at all")
4781 << ListChange::insert(0, 1) << ListRange(0, 17);
4783 QTest::newRow("just displaced")
4788 << ListChange::insert(0, 1) << ListRange(0, 17);
4790 QTest::newRow("just displaced (not enabled)")
4795 << ListChange::insert(0, 1) << ListRange(0, 17);
4797 QTest::newRow("displaced + addDisplaced")
4802 << ListChange::insert(0, 1) << ListRange(0, 17);
4804 QTest::newRow("displaced + addDisplaced (not enabled)")
4809 << ListChange::insert(0, 1) << ListRange(0, 17);
4811 QTest::newRow("displaced + moveDisplaced")
4816 << ListChange::move(0, 10, 1) << ListRange(1, 10);
4818 QTest::newRow("displaced + moveDisplaced (not enabled)")
4823 << ListChange::move(0, 10, 1) << ListRange(1, 10);
4825 QTest::newRow("displaced + removeDisplaced")
4830 << ListChange::remove(0, 1) << ListRange(1, 18);
4832 QTest::newRow("displaced + removeDisplaced (not enabled)")
4837 << ListChange::remove(0, 1) << ListRange(1, 18);
4840 QTest::newRow("displaced + add, should use generic displaced for a remove")
4845 << ListChange::remove(0, 1) << ListRange(1, 18);
4848 void tst_QQuickGridView::multipleTransitions()
4850 // Tests that if you interrupt a transition in progress with another action that
4851 // cancels the previous transition, the resulting items are still placed correctly.
4853 QFETCH(int, initialCount);
4854 QFETCH(qreal, contentY);
4855 QFETCH(QList<ListChange>, changes);
4856 QFETCH(bool, enableAddTransitions);
4857 QFETCH(bool, enableMoveTransitions);
4858 QFETCH(bool, enableRemoveTransitions);
4859 QFETCH(bool, rippleAddDisplaced);
4861 // add transitions on the left, moves on the right
4862 QPointF addTargets_transitionFrom(-50, -50);
4863 QPointF addDisplaced_transitionFrom(-50, 50);
4864 QPointF moveTargets_transitionFrom(50, -50);
4865 QPointF moveDisplaced_transitionFrom(50, 50);
4866 QPointF removeTargets_transitionTo(-100, 300);
4867 QPointF removeDisplaced_transitionFrom(100, 300);
4870 for (int i = 0; i < initialCount; i++)
4871 model.addItem("Original item" + QString::number(i), "");
4873 QQuickView *canvas = getView();
4874 QQmlContext *ctxt = canvas->rootContext();
4875 ctxt->setContextProperty("testModel", &model);
4876 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
4877 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
4878 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
4879 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
4880 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
4881 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
4882 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
4883 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
4884 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
4885 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
4886 canvas->setSource(testFileUrl("multipleTransitions.qml"));
4888 QTest::qWaitForWindowShown(canvas);
4890 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4891 QTRY_VERIFY(gridview != 0);
4892 QQuickItem *contentItem = gridview->contentItem();
4893 QVERIFY(contentItem != 0);
4894 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4896 if (contentY != 0) {
4897 gridview->setContentY(contentY);
4898 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4901 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
4903 for (int i=0; i<changes.count(); i++) {
4904 switch (changes[i].type) {
4905 case ListChange::Inserted:
4907 QList<QPair<QString, QString> > targetItems;
4908 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
4909 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
4910 model.insertItems(changes[i].index, targetItems);
4911 QTRY_COMPARE(model.count(), gridview->count());
4912 if (i == changes.count() - 1) {
4913 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
4914 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
4916 QTest::qWait(timeBetweenActions);
4920 case ListChange::Removed:
4921 model.removeItems(changes[i].index, changes[i].count);
4922 QTRY_COMPARE(model.count(), gridview->count());
4923 if (i == changes.count() - 1) {
4924 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
4925 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
4927 QTest::qWait(timeBetweenActions);
4930 case ListChange::Moved:
4931 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
4932 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4933 if (i == changes.count() - 1) {
4934 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
4935 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
4937 QTest::qWait(timeBetweenActions);
4940 case ListChange::SetCurrent:
4941 gridview->setCurrentIndex(changes[i].index);
4942 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4944 case ListChange::SetContentY:
4945 gridview->setContentY(changes[i].pos);
4946 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4950 QCOMPARE(gridview->count(), model.count());
4952 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4953 int firstVisibleIndex = -1;
4954 for (int i=0; i<items.count(); i++) {
4955 if (items[i]->y() >= contentY) {
4956 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4957 firstVisibleIndex = e.evaluate().toInt();
4961 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4963 // verify all items moved to the correct final positions
4964 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4965 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
4966 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4967 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4968 QTRY_COMPARE(item->x(), (i%3)*80.0);
4969 QTRY_COMPARE(item->y(), (i/3)*60.0);
4970 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4972 QTRY_COMPARE(name->text(), model.name(i));
4975 releaseView(canvas);
4978 void tst_QQuickGridView::multipleTransitions_data()
4980 QTest::addColumn<int>("initialCount");
4981 QTest::addColumn<qreal>("contentY");
4982 QTest::addColumn<QList<ListChange> >("changes");
4983 QTest::addColumn<bool>("enableAddTransitions");
4984 QTest::addColumn<bool>("enableMoveTransitions");
4985 QTest::addColumn<bool>("enableRemoveTransitions");
4986 QTest::addColumn<bool>("rippleAddDisplaced");
4988 // the added item and displaced items should move to final dest correctly
4989 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
4990 << ListChange::insert(0, 1)
4991 << ListChange::move(0, 3, 1)
4993 << true << true << true << false;
4995 // items affected by the add should change from move to add transition
4996 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
4997 << ListChange::move(1, 10, 3)
4998 << ListChange::insert(0, 1)
5000 << true << true << true << false;
5002 // items should be placed correctly if you trigger a transition then refill for that index
5003 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5004 << ListChange::insert(0, 1)
5005 << ListChange::setContentY(160.0)
5006 << ListChange::setContentY(0.0)
5007 << ListChange::insert(0, 1)
5009 << true << true << true << false;
5011 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5012 << ListChange::insert(1, 1)
5013 << ListChange::remove(1, 1)
5015 << true << true << true << true;
5017 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5018 // even if a remove-displace transition is not present to re-animate them
5019 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5020 << ListChange::insert(0, 1)
5021 << ListChange::remove(2, 1)
5023 << true << true << false << false;
5025 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5026 // remove has changed the position of where it will move to
5027 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5028 << ListChange::setContentY(-10.0)
5029 << ListChange::insert(0, 1)
5030 << ListChange::insert(0, 1)
5031 << ListChange::remove(2, 1)
5033 << true << true << false << false;
5036 void tst_QQuickGridView::multipleDisplaced()
5038 // multiple move() operations should only restart displace transitions for items that
5039 // moved from previously set positions, and not those that have moved from their current
5040 // item positions (which may e.g. still be changing from easing bounces in the last transition)
5043 for (int i = 0; i < 30; i++)
5044 model.addItem("Original item" + QString::number(i), "");
5046 QQuickView *canvas = createView();
5047 QQmlContext *ctxt = canvas->rootContext();
5048 ctxt->setContextProperty("testModel", &model);
5049 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5051 QTest::qWaitForWindowShown(canvas);
5053 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5054 QTRY_VERIFY(gridview != 0);
5055 QQuickItem *contentItem = gridview->contentItem();
5056 QVERIFY(contentItem != 0);
5057 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5059 model.moveItems(12, 8, 1);
5060 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5061 model.moveItems(8, 3, 1);
5062 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5064 QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5065 foreach (const QString &name, transitionsStarted.keys()) {
5066 QVERIFY2(transitionsStarted[name] == 1,
5067 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5070 // verify all items moved to the correct final positions
5071 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5072 for (int i=0; i < model.count() && i < items.count(); ++i) {
5073 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5074 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5075 QTRY_COMPARE(item->x(), (i%3)*80.0);
5076 QTRY_COMPARE(item->y(), (i/3)*60.0);
5077 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5079 QTRY_COMPARE(name->text(), model.name(i));
5085 void tst_QQuickGridView::cacheBuffer()
5087 QQuickView *canvas = createView();
5090 for (int i = 0; i < 90; i++)
5091 model.addItem("Item" + QString::number(i), "");
5093 QQmlContext *ctxt = canvas->rootContext();
5094 ctxt->setContextProperty("testModel", &model);
5095 ctxt->setContextProperty("testRightToLeft", QVariant(false));
5096 ctxt->setContextProperty("testTopToBottom", QVariant(false));
5098 canvas->setSource(testFileUrl("gridview1.qml"));
5100 qApp->processEvents();
5102 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5103 QVERIFY(gridview != 0);
5105 QQuickItem *contentItem = gridview->contentItem();
5106 QVERIFY(contentItem != 0);
5107 QVERIFY(gridview->delegate() != 0);
5108 QVERIFY(gridview->model() != 0);
5110 // Confirm items positioned correctly
5111 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5112 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5113 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5114 QTRY_COMPARE(item->x(), (i%3)*80.0);
5115 QTRY_COMPARE(item->y(), (i/3)*60.0);
5118 QQmlIncubationController controller;
5119 canvas->engine()->setIncubationController(&controller);
5121 canvas->rootObject()->setProperty("cacheBuffer", 200);
5122 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5124 // items will be created one at a time
5125 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5126 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5127 QQuickItem *item = 0;
5130 controller.incubateWhile(&b);
5131 item = findItem<QQuickItem>(gridview, "wrapper", i);
5137 controller.incubateWhile(&b);
5140 int newItemCount = 0;
5141 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5143 // Confirm items positioned correctly
5144 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5145 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5147 QTRY_COMPARE(item->x(), (i%3)*80.0);
5148 QTRY_COMPARE(item->y(), (i/3)*60.0);
5151 // move view and confirm items in view are visible immediately and outside are created async
5152 gridview->setContentY(300);
5154 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5155 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5157 QTRY_COMPARE(item->x(), (i%3)*80.0);
5158 QTRY_COMPARE(item->y(), (i/3)*60.0);
5161 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5163 // ensure buffered items are created
5164 for (int i = 34; i < qMin(44,model.count()); ++i) {
5165 QQuickItem *item = 0;
5167 qGuiApp->processEvents(); // allow refill to happen
5169 controller.incubateWhile(&b);
5170 item = findItem<QQuickItem>(gridview, "wrapper", i);
5176 controller.incubateWhile(&b);
5182 void tst_QQuickGridView::asynchronous()
5184 QQuickView *canvas = createView();
5186 QQmlIncubationController controller;
5187 canvas->engine()->setIncubationController(&controller);
5189 canvas->setSource(testFileUrl("asyncloader.qml"));
5191 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5192 QVERIFY(rootObject);
5194 QQuickGridView *gridview = 0;
5197 controller.incubateWhile(&b);
5198 gridview = rootObject->findChild<QQuickGridView*>("view");
5201 // items will be created one at a time
5202 for (int i = 0; i < 12; ++i) {
5203 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5204 QQuickItem *item = 0;
5207 controller.incubateWhile(&b);
5208 item = findItem<QQuickItem>(gridview, "wrapper", i);
5214 controller.incubateWhile(&b);
5217 // verify positioning
5218 QQuickItem *contentItem = gridview->contentItem();
5219 for (int i = 0; i < 12; ++i) {
5220 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5221 if (!item) qWarning() << "Item" << i << "not found";
5222 QVERIFY(item->x() == (i%3)*100);
5223 QVERIFY(item->y() == (i/3)*100);
5229 void tst_QQuickGridView::unrequestedVisibility()
5232 for (int i = 0; i < 30; i++)
5233 model.addItem("Item" + QString::number(i), QString::number(i));
5235 QQuickView *canvas = new QQuickView(0);
5236 canvas->setGeometry(0,0,240,320);
5238 QQmlContext *ctxt = canvas->rootContext();
5239 ctxt->setContextProperty("testModel", &model);
5240 ctxt->setContextProperty("testWrap", QVariant(false));
5242 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5246 qApp->processEvents();
5248 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5249 QTRY_VERIFY(leftview != 0);
5251 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5252 QTRY_VERIFY(rightview != 0);
5254 QQuickItem *leftContent = leftview->contentItem();
5255 QTRY_VERIFY(leftContent != 0);
5257 QQuickItem *rightContent = rightview->contentItem();
5258 QTRY_VERIFY(rightContent != 0);
5260 rightview->setCurrentIndex(12);
5262 QTRY_COMPARE(leftview->contentY(), 0.0);
5263 QTRY_COMPARE(rightview->contentY(), 240.0);
5267 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5268 QCOMPARE(item->isVisible(), true);
5269 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5270 QCOMPARE(item->isVisible(), false);
5272 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5273 QCOMPARE(item->isVisible(), false);
5274 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5275 QCOMPARE(item->isVisible(), true);
5277 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5278 QCOMPARE(item->isVisible(), true);
5279 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5280 QCOMPARE(item->isVisible(), false);
5281 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5282 QCOMPARE(item->isVisible(), false);
5283 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5284 QCOMPARE(item->isVisible(), true);
5286 rightview->setCurrentIndex(0);
5288 QTRY_COMPARE(leftview->contentY(), 0.0);
5289 QTRY_COMPARE(rightview->contentY(), 0.0);
5291 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5292 QCOMPARE(item->isVisible(), true);
5293 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5294 QTRY_COMPARE(item->isVisible(), true);
5296 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5297 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5299 leftview->setCurrentIndex(12);
5301 QTRY_COMPARE(leftview->contentY(), 240.0);
5302 QTRY_COMPARE(rightview->contentY(), 0.0);
5304 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5305 QTRY_COMPARE(item->isVisible(), false);
5306 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5307 QCOMPARE(item->isVisible(), true);
5309 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5310 QCOMPARE(item->isVisible(), true);
5311 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5312 QCOMPARE(item->isVisible(), false);
5314 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5315 QCOMPARE(item->isVisible(), false);
5316 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5317 QCOMPARE(item->isVisible(), true);
5318 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5319 QCOMPARE(item->isVisible(), true);
5320 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5321 QCOMPARE(item->isVisible(), false);
5323 // move a non-visible item into view
5324 model.moveItems(10, 9, 1);
5325 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5327 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5328 QCOMPARE(item->isVisible(), false);
5329 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5330 QCOMPARE(item->isVisible(), true);
5332 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5333 QCOMPARE(item->isVisible(), true);
5334 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5335 QCOMPARE(item->isVisible(), false);
5337 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5338 QCOMPARE(item->isVisible(), false);
5339 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5340 QCOMPARE(item->isVisible(), true);
5341 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5342 QCOMPARE(item->isVisible(), true);
5343 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5344 QCOMPARE(item->isVisible(), false);
5346 // move a visible item out of view
5347 model.moveItems(5, 3, 1);
5348 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5350 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5351 QCOMPARE(item->isVisible(), false);
5352 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5353 QCOMPARE(item->isVisible(), true);
5354 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5355 QCOMPARE(item->isVisible(), true);
5356 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5357 QCOMPARE(item->isVisible(), false);
5359 // move a non-visible item into view
5360 model.moveItems(3, 5, 1);
5361 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5363 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5364 QCOMPARE(item->isVisible(), false);
5365 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5366 QCOMPARE(item->isVisible(), true);
5367 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5368 QCOMPARE(item->isVisible(), true);
5369 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5370 QCOMPARE(item->isVisible(), false);
5372 // move a visible item out of view
5373 model.moveItems(9, 10, 1);
5374 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5376 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5377 QCOMPARE(item->isVisible(), false);
5378 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5379 QCOMPARE(item->isVisible(), true);
5380 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5381 QCOMPARE(item->isVisible(), true);
5382 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5383 QCOMPARE(item->isVisible(), false);
5385 // move a non-visible item into view
5386 model.moveItems(10, 9, 1);
5387 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5389 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5390 QCOMPARE(item->isVisible(), false);
5391 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5392 QCOMPARE(item->isVisible(), true);
5393 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5394 QCOMPARE(item->isVisible(), true);
5395 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5396 QCOMPARE(item->isVisible(), false);
5401 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
5405 for (int i=0; i<list.count(); i++) {
5406 ret << list[i].toInt(&ok);
5408 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
5414 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
5416 for (int i=0; i<indexLists.count(); i++) {
5417 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
5418 if (current != expectedIndexes.toSet())
5419 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
5420 QCOMPARE(current, expectedIndexes.toSet());
5424 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
5426 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
5427 QVERIFY(it.value().type() == QVariant::Int);
5428 QString name = it.key();
5429 int itemIndex = it.value().toInt();
5430 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
5431 if (model.name(itemIndex) != name)
5432 qDebug() << itemIndex;
5433 QCOMPARE(model.name(itemIndex), name);
5435 QCOMPARE(items.count(), expectedIndexes.count());
5438 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
5440 for (int i=0; i<itemLists.count(); i++) {
5441 QVariantList current = itemLists[i].toList();
5442 for (int j=0; j<current.count(); j++) {
5443 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
5444 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
5445 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
5447 QCOMPARE(current.count(), expectedItems.count());
5451 QTEST_MAIN(tst_QQuickGridView)
5453 #include "tst_qquickgridview.moc"