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 <QtQml/qqmlcontext.h>
51 #include <QtQuick/private/qquickitem_p.h>
52 #include <QtQuick/private/qquickgridview_p.h>
53 #include <QtQuick/private/qquicktext_p.h>
54 #include <QtQuick/private/qquickvisualitemmodel_p.h>
55 #include <QtQml/private/qquicklistmodel_p.h>
56 #include <QtQml/private/qlistmodelinterface_p.h>
57 #include "../../shared/util.h"
58 #include "../shared/viewtestutil.h"
59 #include "../shared/visualtestutil.h"
60 #include <QtGui/qguiapplication.h>
62 Q_DECLARE_METATYPE(QQuickGridView::Flow)
63 Q_DECLARE_METATYPE(Qt::LayoutDirection)
64 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
65 Q_DECLARE_METATYPE(Qt::Key)
67 using namespace QQuickViewTestUtil;
68 using namespace QQuickVisualTestUtil;
72 class tst_QQuickGridView : public QQmlDataTest
82 void inserted_basic();
83 void inserted_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
84 void inserted_defaultLayout_data();
85 void insertBeforeVisible();
86 void insertBeforeVisible_data();
88 void removed_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
89 void removed_defaultLayout_data();
90 void addOrRemoveBeforeVisible();
91 void addOrRemoveBeforeVisible_data();
93 void moved_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
94 void moved_defaultLayout_data();
95 void multipleChanges_condensed() { multipleChanges(true); }
96 void multipleChanges_condensed_data() { multipleChanges_data(); }
97 void multipleChanges_uncondensed() { multipleChanges(false); }
98 void multipleChanges_uncondensed_data() { multipleChanges_data(); }
99 void swapWithFirstItem();
102 void noCurrentIndex();
103 void keyNavigation();
104 void keyNavigation_data();
105 void defaultValues();
107 void propertyChanges();
108 void componentChanges();
110 void positionViewAtIndex();
111 void positionViewAtIndex_rightToLeft();
116 void enforceRange_rightToLeft();
118 void manualHighlight();
124 void headerFooter_data();
125 void resetModel_headerFooter();
126 void resizeViewAndRepaint();
128 void resizeGrid_data();
129 void changeColumnCount();
130 void indexAt_itemAt_data();
131 void indexAt_itemAt();
135 void onRemove_data();
138 void creationContext();
139 void snapToRow_data();
141 void snapOneRow_data();
146 void unrequestedVisibility();
148 void populateTransitions();
149 void populateTransitions_data();
150 void addTransitions();
151 void addTransitions_data();
152 void moveTransitions();
153 void moveTransitions_data();
154 void removeTransitions();
155 void removeTransitions_data();
156 void displacedTransitions();
157 void displacedTransitions_data();
158 void multipleTransitions();
159 void multipleTransitions_data();
160 void multipleDisplaced();
162 void inserted_leftToRight_RtL_TtB();
163 void inserted_leftToRight_RtL_TtB_data();
164 void inserted_leftToRight_LtR_BtT();
165 void inserted_leftToRight_LtR_BtT_data();
166 void inserted_leftToRight_RtL_BtT();
167 void inserted_leftToRight_RtL_BtT_data();
168 void inserted_topToBottom_LtR_TtB();
169 void inserted_topToBottom_LtR_TtB_data();
170 void inserted_topToBottom_RtL_TtB();
171 void inserted_topToBottom_RtL_TtB_data();
172 void inserted_topToBottom_LtR_BtT();
173 void inserted_topToBottom_LtR_BtT_data();
174 void inserted_topToBottom_RtL_BtT();
175 void inserted_topToBottom_RtL_BtT_data();
177 void removed_leftToRight_RtL_TtB();
178 void removed_leftToRight_RtL_TtB_data();
179 void removed_leftToRight_LtR_BtT();
180 void removed_leftToRight_LtR_BtT_data();
181 void removed_leftToRight_RtL_BtT();
182 void removed_leftToRight_RtL_BtT_data();
183 void removed_topToBottom_LtR_TtB();
184 void removed_topToBottom_LtR_TtB_data();
185 void removed_topToBottom_RtL_TtB();
186 void removed_topToBottom_RtL_TtB_data();
187 void removed_topToBottom_LtR_BtT();
188 void removed_topToBottom_LtR_BtT_data();
189 void removed_topToBottom_RtL_BtT();
190 void removed_topToBottom_RtL_BtT_data();
192 void moved_leftToRight_RtL_TtB();
193 void moved_leftToRight_RtL_TtB_data();
194 void moved_leftToRight_LtR_BtT();
195 void moved_leftToRight_LtR_BtT_data();
196 void moved_leftToRight_RtL_BtT();
197 void moved_leftToRight_RtL_BtT_data();
198 void moved_topToBottom_LtR_TtB();
199 void moved_topToBottom_LtR_TtB_data();
200 void moved_topToBottom_RtL_TtB();
201 void moved_topToBottom_RtL_TtB_data();
202 void moved_topToBottom_LtR_BtT();
203 void moved_topToBottom_LtR_BtT_data();
204 void moved_topToBottom_RtL_BtT();
205 void moved_topToBottom_RtL_BtT_data();
208 QList<int> toIntList(const QVariantList &list);
209 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
210 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
211 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
213 void multipleChanges(bool condensed);
214 void multipleChanges_data();
216 QPointF expectedItemPos(QQuickGridView *grid, int index, qreal rowOffset = 0) {
219 if (grid->flow() == QQuickGridView::FlowLeftToRight) {
220 int columns = grid->width() / grid->cellWidth();
221 x = (index % columns) * grid->cellWidth();
222 y = (index / columns) * grid->cellHeight();
223 if (grid->effectiveLayoutDirection() == Qt::RightToLeft) {
224 int col = (index % columns) * grid->cellWidth();
225 x = grid->cellWidth() * (columns - 1) - col;
228 qreal offset = grid->cellHeight() * rowOffset;
229 if (grid->verticalLayoutDirection() == QQuickItemView::TopToBottom)
232 y = -grid->cellHeight() - y - offset;
234 int rows = grid->height() / grid->cellHeight();
235 x = (index / rows) * grid->cellWidth();
236 y = (index % rows) * grid->cellHeight();
237 if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
238 x = -x - grid->cellWidth();
240 qreal offset = grid->cellWidth() * rowOffset;
241 if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
245 if (grid->verticalLayoutDirection() == QQuickItemView::BottomToTop)
246 y = -grid->cellHeight() - y;
248 return QPointF(x, y);
251 // Sets contentY (or contentX in TopToBottom flow) according to given row offset
252 // (in LeftToRight flow) or col offset (in TopToBottom).
253 bool setContentPos(QQuickGridView *gridview, qreal rowOrColOffset) {
254 bool contentPosChanged = (rowOrColOffset != 0);
255 qreal contentOffset = gridview->flow() == QQuickGridView::FlowLeftToRight
256 ? rowOrColOffset * gridview->cellHeight()
257 : rowOrColOffset * gridview->cellWidth();
259 if (gridview->flow() == QQuickGridView::FlowLeftToRight) {
260 if (gridview->verticalLayoutDirection() == QQuickItemView::BottomToTop)
261 contentOffset = -gridview->height() - contentOffset;
263 if (gridview->effectiveLayoutDirection() == Qt::RightToLeft)
264 contentOffset = -gridview->width() - contentOffset;
266 if (gridview->flow() == QQuickGridView::FlowLeftToRight)
267 gridview->setContentY(contentOffset);
269 gridview->setContentX(contentOffset);
270 return contentPosChanged;
274 QQuickView *getView() {
276 if (QString(QTest::currentTestFunction()) != testForView) {
280 m_view->setSource(QUrl());
285 testForView = QTest::currentTestFunction();
286 m_view = createView();
289 void releaseView(QQuickView *view) {
290 Q_ASSERT(view == m_view);
291 m_view->setSource(QUrl());
294 QQuickView *getView() {
297 void releaseView(QQuickView *view) {
306 tst_QQuickGridView::tst_QQuickGridView() : m_view(0)
310 void tst_QQuickGridView::init()
313 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
314 testForView = QString();
321 void tst_QQuickGridView::items()
323 QQuickView *canvas = createView();
326 model.addItem("Fred", "12345");
327 model.addItem("John", "2345");
328 model.addItem("Bob", "54321");
329 model.addItem("Billy", "22345");
330 model.addItem("Sam", "2945");
331 model.addItem("Ben", "04321");
332 model.addItem("Jim", "0780");
334 QQmlContext *ctxt = canvas->rootContext();
335 ctxt->setContextProperty("testModel", &model);
337 canvas->setSource(testFileUrl("gridview1.qml"));
338 qApp->processEvents();
340 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
341 QTRY_VERIFY(gridview != 0);
343 QQuickItem *contentItem = gridview->contentItem();
344 QTRY_VERIFY(contentItem != 0);
346 QTRY_COMPARE(gridview->count(), model.count());
347 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
348 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
350 for (int i = 0; i < model.count(); ++i) {
351 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
352 QTRY_VERIFY(name != 0);
353 QTRY_COMPARE(name->text(), model.name(i));
354 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
355 QTRY_VERIFY(number != 0);
356 QTRY_COMPARE(number->text(), model.number(i));
359 // set an empty model and confirm that items are destroyed
361 ctxt->setContextProperty("testModel", &model2);
363 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
364 QTRY_VERIFY(itemCount == 0);
369 void tst_QQuickGridView::changed()
371 QQuickView *canvas = createView();
374 model.addItem("Fred", "12345");
375 model.addItem("John", "2345");
376 model.addItem("Bob", "54321");
377 model.addItem("Billy", "22345");
378 model.addItem("Sam", "2945");
379 model.addItem("Ben", "04321");
380 model.addItem("Jim", "0780");
382 QQmlContext *ctxt = canvas->rootContext();
383 ctxt->setContextProperty("testModel", &model);
385 canvas->setSource(testFileUrl("gridview1.qml"));
386 qApp->processEvents();
388 QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
389 QTRY_VERIFY(gridview != 0);
391 QQuickItem *contentItem = gridview->contentItem();
392 QTRY_VERIFY(contentItem != 0);
394 model.modifyItem(1, "Will", "9876");
395 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
396 QTRY_VERIFY(name != 0);
397 QTRY_COMPARE(name->text(), model.name(1));
398 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
399 QTRY_VERIFY(number != 0);
400 QTRY_COMPARE(number->text(), model.number(1));
405 void tst_QQuickGridView::inserted_basic()
407 QQuickView *canvas = createView();
411 model.addItem("Fred", "12345");
412 model.addItem("John", "2345");
413 model.addItem("Bob", "54321");
415 canvas->rootContext()->setContextProperty("testModel", &model);
416 canvas->setSource(testFileUrl("gridview1.qml"));
417 qApp->processEvents();
419 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
420 QTRY_VERIFY(gridview != 0);
422 QQuickItem *contentItem = gridview->contentItem();
423 QTRY_VERIFY(contentItem != 0);
425 model.insertItem(1, "Will", "9876");
427 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
428 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
430 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
431 QTRY_VERIFY(name != 0);
432 QTRY_COMPARE(name->text(), model.name(1));
433 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
434 QTRY_VERIFY(number != 0);
435 QTRY_COMPARE(number->text(), model.number(1));
437 // Checks that onAdd is called
438 int added = canvas->rootObject()->property("added").toInt();
439 QTRY_COMPARE(added, 1);
441 // Confirm items positioned correctly
442 for (int i = 0; i < model.count(); ++i) {
443 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
444 QTRY_COMPARE(item->x(), (i%3)*80.0);
445 QTRY_COMPARE(item->y(), (i/3)*60.0);
448 model.insertItem(0, "Foo", "1111"); // zero index, and current item
450 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
452 name = findItem<QQuickText>(contentItem, "textName", 0);
453 QTRY_VERIFY(name != 0);
454 QTRY_COMPARE(name->text(), model.name(0));
455 number = findItem<QQuickText>(contentItem, "textNumber", 0);
456 QTRY_VERIFY(number != 0);
457 QTRY_COMPARE(number->text(), model.number(0));
459 QTRY_COMPARE(gridview->currentIndex(), 1);
461 // Confirm items positioned correctly
462 for (int i = 0; i < model.count(); ++i) {
463 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
464 QTRY_VERIFY(item->x() == (i%3)*80);
465 QTRY_VERIFY(item->y() == (i/3)*60);
468 for (int i = model.count(); i < 30; ++i)
469 model.insertItem(i, "Hello", QString::number(i));
471 gridview->setContentY(120);
473 // Insert item outside visible area
474 model.insertItem(1, "Hello", "1324");
476 QTRY_VERIFY(gridview->contentY() == 120);
481 void tst_QQuickGridView::inserted_defaultLayout(QQuickGridView::Flow flow,
482 Qt::LayoutDirection horizLayout,
483 QQuickItemView::VerticalLayoutDirection verticalLayout)
485 QFETCH(qreal, contentYRowOffset);
486 QFETCH(int, insertIndex);
487 QFETCH(int, insertCount);
488 QFETCH(int, insertIndex_ttb);
489 QFETCH(int, insertCount_ttb);
490 QFETCH(qreal, rowOffsetAfterMove);
493 for (int i = 0; i < 30; i++)
494 model.addItem("Item" + QString::number(i), "");
496 QQuickView *canvas = getView();
497 QQmlContext *ctxt = canvas->rootContext();
498 ctxt->setContextProperty("testModel", &model);
499 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
500 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
501 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
502 canvas->setSource(testFileUrl("layouts.qml"));
504 qApp->processEvents();
506 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
507 QTRY_VERIFY(gridview != 0);
508 QQuickItem *contentItem = gridview->contentItem();
509 QTRY_VERIFY(contentItem != 0);
511 if (flow == QQuickGridView::FlowTopToBottom) {
512 insertIndex = insertIndex_ttb;
513 insertCount = insertCount_ttb;
515 if (setContentPos(gridview, contentYRowOffset))
516 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
518 QList<QPair<QString, QString> > newData;
519 for (int i=0; i<insertCount; i++)
520 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
521 model.insertItems(insertIndex, newData);
522 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
524 // check visibleItems.first() is in correct position
525 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
527 QPointF firstPos(0, 0);
528 if (horizLayout == Qt::RightToLeft)
529 firstPos.rx() = flow == QQuickGridView::FlowLeftToRight ? gridview->width() - gridview->cellWidth() : -gridview->cellWidth();
530 if (verticalLayout == QQuickItemView::BottomToTop)
531 firstPos.ry() -= gridview->cellHeight();
532 QCOMPARE(item0->pos(), firstPos);
534 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
535 int firstVisibleIndex = -1;
536 for (int i=0; i<items.count(); i++) {
537 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
538 if (item && item->isVisible()) {
539 firstVisibleIndex = i;
543 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
545 // Confirm items positioned correctly and indexes correct
546 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
547 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
548 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
549 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
550 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
552 QCOMPARE(name->text(), model.name(i));
558 void tst_QQuickGridView::inserted_defaultLayout_data()
560 QTest::addColumn<qreal>("contentYRowOffset");
561 QTest::addColumn<int>("insertIndex");
562 QTest::addColumn<int>("insertCount");
563 QTest::addColumn<int>("insertIndex_ttb");
564 QTest::addColumn<int>("insertCount_ttb");
565 QTest::addColumn<qreal>("rowOffsetAfterMove");
567 QTest::newRow("add 1, before visible items")
571 << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
572 // new 1st visible item is at 0
574 QTest::newRow("add 2, before visible items")
578 << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
580 QTest::newRow("add 3, before visible items")
584 << -1.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
586 QTest::newRow("add 5, before visible items")
590 << -1.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
591 // grid is rearranged and first visible moves forward within its row
593 QTest::newRow("add 6, before visible items")
597 << -1.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
601 QTest::newRow("add 1, at start of visible, content at start")
607 QTest::newRow("add multiple, at start of visible, content at start")
613 QTest::newRow("add 1, at start of visible, content not at start")
619 QTest::newRow("add multiple, at start of visible, content not at start")
626 QTest::newRow("add 1, at end of visible, content at start")
632 QTest::newRow("add row, at end of visible, content at start")
638 QTest::newRow("add 1, at end of visible, content not at start")
644 QTest::newRow("add multiple, at end of visible, content not at start")
651 QTest::newRow("add 1, after visible, content at start")
657 QTest::newRow("add row, after visible, content at start")
663 QTest::newRow("add 1, after visible, content not at start")
669 QTest::newRow("add multiple, after visible, content not at start")
676 void tst_QQuickGridView::insertBeforeVisible()
678 QFETCH(int, insertIndex);
679 QFETCH(int, insertCount);
680 QFETCH(int, cacheBuffer);
683 QQuickView *canvas = getView();
686 for (int i = 0; i < 30; i++)
687 model.addItem("Item" + QString::number(i), "");
689 QQmlContext *ctxt = canvas->rootContext();
690 ctxt->setContextProperty("testModel", &model);
691 canvas->setSource(testFileUrl("gridview1.qml"));
693 qApp->processEvents();
695 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
696 QTRY_VERIFY(gridview != 0);
697 QQuickItem *contentItem = gridview->contentItem();
698 QTRY_VERIFY(contentItem != 0);
700 gridview->setCacheBuffer(cacheBuffer);
701 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
703 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
704 int firstVisibleIndex = 12; // move to an index where the top item is not visible
705 gridview->setContentY(firstVisibleIndex/3 * 60.0);
706 gridview->setCurrentIndex(firstVisibleIndex);
707 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
709 QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
710 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
712 QCOMPARE(item->y(), gridview->contentY());
714 QList<QPair<QString, QString> > newData;
715 for (int i=0; i<insertCount; i++)
716 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
717 model.insertItems(insertIndex, newData);
718 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
720 // now, moving to the top of the view should position the inserted items correctly
721 int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
722 gridview->setCurrentIndex(0);
723 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
724 QTRY_COMPARE(gridview->currentIndex(), 0);
725 QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
727 // Confirm items positioned correctly and indexes correct
728 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
729 for (int i = 0; i < model.count() && i < itemCount; ++i) {
730 item = findItem<QQuickItem>(contentItem, "wrapper", i);
731 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
732 QCOMPARE(item->x(), (i%3)*80.0);
733 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
734 name = findItem<QQuickText>(contentItem, "textName", i);
736 QTRY_COMPARE(name->text(), model.name(i));
742 void tst_QQuickGridView::insertBeforeVisible_data()
744 QTest::addColumn<int>("insertIndex");
745 QTest::addColumn<int>("insertCount");
746 QTest::addColumn<int>("cacheBuffer");
748 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
749 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
750 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
752 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
753 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
754 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
756 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
757 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
758 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
760 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
761 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
762 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
765 void tst_QQuickGridView::removed_basic()
767 QQuickView *canvas = createView();
771 for (int i = 0; i < 40; i++)
772 model.addItem("Item" + QString::number(i), "");
774 canvas->rootContext()->setContextProperty("testModel", &model);
775 canvas->setSource(testFileUrl("gridview1.qml"));
776 qApp->processEvents();
778 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
779 QTRY_VERIFY(gridview != 0);
780 QQuickItem *contentItem = gridview->contentItem();
781 QTRY_VERIFY(contentItem != 0);
782 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
785 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
787 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
788 QTRY_VERIFY(name != 0);
789 QTRY_COMPARE(name->text(), model.name(1));
790 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
791 QTRY_VERIFY(number != 0);
792 QTRY_COMPARE(number->text(), model.number(1));
795 // Checks that onRemove is called
796 QString removed = canvas->rootObject()->property("removed").toString();
797 QTRY_COMPARE(removed, QString("Item1"));
799 // Confirm items positioned correctly
800 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
801 for (int i = 0; i < model.count() && i < itemCount; ++i) {
802 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
803 if (!item) qWarning() << "Item" << i << "not found";
804 QTRY_VERIFY(item->x() == (i%3)*80);
805 QTRY_VERIFY(item->y() == (i/3)*60);
808 // Remove first item (which is the current item);
810 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
812 name = findItem<QQuickText>(contentItem, "textName", 0);
813 QTRY_VERIFY(name != 0);
814 QTRY_COMPARE(name->text(), model.name(0));
815 number = findItem<QQuickText>(contentItem, "textNumber", 0);
816 QTRY_VERIFY(number != 0);
817 QTRY_COMPARE(number->text(), model.number(0));
820 // Confirm items positioned correctly
821 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
822 for (int i = 0; i < model.count() && i < itemCount; ++i) {
823 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
824 if (!item) qWarning() << "Item" << i << "not found";
825 QTRY_VERIFY(item->x() == (i%3)*80);
826 QTRY_VERIFY(item->y() == (i/3)*60);
829 // Remove items not visible
830 model.removeItem(25);
831 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
833 // Confirm items positioned correctly
834 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
835 for (int i = 0; i < model.count() && i < itemCount; ++i) {
836 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
837 if (!item) qWarning() << "Item" << i << "not found";
838 QTRY_VERIFY(item->x() == (i%3)*80);
839 QTRY_VERIFY(item->y() == (i/3)*60);
842 // Remove items before visible
843 gridview->setContentY(120);
844 gridview->setCurrentIndex(10);
846 // Setting currentIndex above shouldn't cause view to scroll
847 QTRY_COMPARE(gridview->contentY(), 120.0);
850 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
852 // Confirm items positioned correctly
853 for (int i = 6; i < 18; ++i) {
854 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
855 if (!item) qWarning() << "Item" << i << "not found";
856 QTRY_VERIFY(item->x() == (i%3)*80);
857 QTRY_VERIFY(item->y() == (i/3)*60);
860 // Remove currentIndex
861 QQuickItem *oldCurrent = gridview->currentItem();
863 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
865 QTRY_COMPARE(gridview->currentIndex(), 9);
866 QTRY_VERIFY(gridview->currentItem() != oldCurrent);
868 gridview->setContentY(0);
869 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
871 // Confirm items positioned correctly
872 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
873 for (int i = 0; i < model.count() && i < itemCount; ++i) {
874 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
875 QTRY_VERIFY(item->x() == (i%3)*80);
876 QTRY_VERIFY(item->y() == (i/3)*60);
879 // remove item outside current view.
880 gridview->setCurrentIndex(32);
881 gridview->setContentY(240);
883 model.removeItem(30);
884 QTRY_VERIFY(gridview->currentIndex() == 31);
886 // remove current item beyond visible items.
887 gridview->setCurrentIndex(20);
888 gridview->setContentY(0);
889 model.removeItem(20);
891 QTRY_COMPARE(gridview->currentIndex(), 20);
892 QTRY_VERIFY(gridview->currentItem() != 0);
894 // remove item before current, but visible
895 gridview->setCurrentIndex(8);
896 gridview->setContentY(240);
897 oldCurrent = gridview->currentItem();
900 QTRY_COMPARE(gridview->currentIndex(), 7);
901 QTRY_VERIFY(gridview->currentItem() == oldCurrent);
906 void tst_QQuickGridView::removed_defaultLayout(QQuickGridView::Flow flow,
907 Qt::LayoutDirection horizLayout,
908 QQuickItemView::VerticalLayoutDirection verticalLayout)
910 QFETCH(qreal, contentYRowOffset);
911 QFETCH(int, removeIndex);
912 QFETCH(int, removeCount);
913 QFETCH(int, removeIndex_ttb);
914 QFETCH(int, removeCount_ttb);
915 QFETCH(qreal, rowOffsetAfterMove);
916 QFETCH(QString, firstVisible);
917 QFETCH(QString, firstVisible_ttb);
919 QQuickView *canvas = getView();
922 for (int i = 0; i < 30; i++)
923 model.addItem("Item" + QString::number(i), "");
925 QQmlContext *ctxt = canvas->rootContext();
926 ctxt->setContextProperty("testModel", &model);
927 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
928 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
929 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
930 canvas->setSource(testFileUrl("layouts.qml"));
932 qApp->processEvents();
934 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
935 QTRY_VERIFY(gridview != 0);
936 QQuickItem *contentItem = gridview->contentItem();
937 QTRY_VERIFY(contentItem != 0);
939 if (flow == QQuickGridView::FlowTopToBottom) {
940 removeIndex = removeIndex_ttb;
941 removeCount = removeCount_ttb;
942 firstVisible = firstVisible_ttb;
944 if (setContentPos(gridview, contentYRowOffset))
945 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
947 model.removeItems(removeIndex, removeCount);
948 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
951 int firstVisibleIndex = -1;
952 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
953 QRectF viewRect(gridview->contentX(), gridview->contentY(), gridview->width(), gridview->height());
954 for (int i=0; i<items.count(); i++) {
955 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
957 QRectF itemRect(item->x(), item->y(), item->width(), item->height());
958 if (item->isVisible() && viewRect.intersects(itemRect)) {
959 firstVisibleIndex = i;
960 QQmlExpression en(qmlContext(item), item, "name");
961 firstName = en.evaluate().toString();
966 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
967 QCOMPARE(firstName, firstVisible);
969 // Confirm items positioned correctly and indexes correct
970 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
971 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
972 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
973 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
974 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
976 QTRY_COMPARE(name->text(), model.name(i));
982 void tst_QQuickGridView::removed_defaultLayout_data()
984 QTest::addColumn<qreal>("contentYRowOffset");
985 QTest::addColumn<int>("removeIndex");
986 QTest::addColumn<int>("removeCount");
987 QTest::addColumn<int>("removeIndex_ttb");
988 QTest::addColumn<int>("removeCount_ttb");
989 QTest::addColumn<qreal>("rowOffsetAfterMove");
990 QTest::addColumn<QString>("firstVisible");
991 QTest::addColumn<QString>("firstVisible_ttb");
993 QTest::newRow("remove 1, before visible items")
997 << 0.0 << "Item7" << "Item11";
999 QTest::newRow("remove 1, before visible position")
1003 << 0.0 << "Item7" << "Item11";
1005 QTest::newRow("remove multiple (1 row), all before visible items")
1009 << 1.0 << "Item6" << "Item10"; // removed top row, slide down by 1 row
1011 QTest::newRow("remove multiple, all before visible items, remove item 0")
1015 << 1.0 << "Item7" << "Item11"; // removed top row, slide down by 1 row
1017 QTest::newRow("remove multiple rows, all before visible items")
1018 << 4.0 // show 12-29
1021 << 2.0 << "Item13" << "Item17";
1023 QTest::newRow("remove one row before visible, content y not on item border")
1027 << 1.0 << "Item3" << "Item5"; // 1 row removed
1029 QTest::newRow("remove mix of visible/non-visible")
1033 << 1.0 << "Item6" << "Item8"; // 1 row removed
1036 // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
1037 // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
1038 QTest::newRow("remove multiple, mix of items from before and within visible items")
1042 << 1.0 << "Item8" << "Item12"; // adjust for the 1 row removed before the visible
1044 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1048 << 1.0 * 2 << "Item8" << "Item12"; // adjust for the 2 rows removed before the visible
1051 QTest::newRow("remove 1, from start of visible, content at start")
1055 << 0.0 << "Item1" << "Item1";
1057 QTest::newRow("remove multiple, from start of visible, content at start")
1061 << 0.0 << "Item3" << "Item5";
1063 QTest::newRow("remove 1, from start of visible, content not at start")
1067 << 0.0 << "Item7" << "Item11";
1069 QTest::newRow("remove multiple, from start of visible, content not at start")
1073 << 0.0 << "Item9" << "Item15";
1076 QTest::newRow("remove 1, from middle of visible, content at start")
1080 << 0.0 << "Item0" << "Item0";
1082 QTest::newRow("remove multiple, from middle of visible, content at start")
1086 << 0.0 << "Item0" << "Item0";
1088 QTest::newRow("remove 1, from middle of visible, content not at start")
1092 << 0.0 << "Item6" << "Item10";
1094 QTest::newRow("remove multiple, from middle of visible, content not at start")
1098 << 0.0 << "Item6" << "Item10";
1101 QTest::newRow("remove 1, after visible, content at start")
1105 << 0.0 << "Item0" << "Item0";
1107 QTest::newRow("remove multiple, after visible, content at start")
1111 << 0.0 << "Item0" << "Item0";
1113 QTest::newRow("remove 1, after visible, content not at start")
1117 << 0.0 << "Item6" << "Item10";
1119 QTest::newRow("remove multiple, after visible, content not at start")
1123 << 0.0 << "Item6" << "Item10";
1125 QTest::newRow("remove multiple, mix of items from within and after visible items")
1129 << 0.0 << "Item6" << "Item10";
1132 void tst_QQuickGridView::addOrRemoveBeforeVisible()
1134 // QTBUG-21588: ensure re-layout is done on grid after adding or removing
1135 // items from before the visible area
1137 QFETCH(bool, doAdd);
1138 QFETCH(qreal, newTopContentY);
1140 QQuickView *canvas = getView();
1144 for (int i = 0; i < 30; i++)
1145 model.addItem("Item" + QString::number(i), "");
1147 canvas->rootContext()->setContextProperty("testModel", &model);
1148 canvas->setSource(testFileUrl("gridview1.qml"));
1150 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1151 QTRY_VERIFY(gridview != 0);
1152 QQuickItem *contentItem = gridview->contentItem();
1153 QTRY_VERIFY(contentItem != 0);
1155 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1156 QTRY_COMPARE(name->text(), QString("Item0"));
1158 gridview->setCurrentIndex(0);
1159 qApp->processEvents();
1160 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1162 // scroll down until item 0 is no longer drawn
1163 // (bug not triggered if we just move using content y, since that doesn't
1164 // refill and change the visible items)
1165 gridview->setCurrentIndex(24);
1166 qApp->processEvents();
1168 QTRY_COMPARE(gridview->currentIndex(), 24);
1169 QTRY_COMPARE(gridview->contentY(), 220.0);
1171 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1172 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible
1175 model.insertItem(0, "New Item", "New Item number");
1176 QTRY_COMPARE(gridview->count(), 31);
1178 model.removeItem(0);
1179 QTRY_COMPARE(gridview->count(), 29);
1182 // scroll back up and item 0 should be gone
1183 gridview->setCurrentIndex(0);
1184 qApp->processEvents();
1185 QTRY_COMPARE(gridview->currentIndex(), 0);
1186 QTRY_COMPARE(gridview->contentY(), newTopContentY);
1188 name = findItem<QQuickText>(contentItem, "textName", 0);
1190 QCOMPARE(name->text(), QString("New Item"));
1192 QCOMPARE(name->text(), QString("Item1"));
1194 // Confirm items positioned correctly
1195 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1196 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1197 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
1198 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1199 QTRY_VERIFY(item->x() == (i%3)*80);
1200 QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1203 releaseView(canvas);
1206 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1208 QTest::addColumn<bool>("doAdd");
1209 QTest::addColumn<qreal>("newTopContentY");
1211 QTest::newRow("add") << true << -60.0;
1212 QTest::newRow("remove") << false << -60.0;
1215 void tst_QQuickGridView::clear()
1217 QQuickView *canvas = createView();
1220 for (int i = 0; i < 30; i++)
1221 model.addItem("Item" + QString::number(i), "");
1223 canvas->rootContext()->setContextProperty("testModel", &model);
1224 canvas->setSource(testFileUrl("gridview1.qml"));
1226 qApp->processEvents();
1228 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1229 QVERIFY(gridview != 0);
1230 QQuickItem *contentItem = gridview->contentItem();
1231 QVERIFY(contentItem != 0);
1232 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1236 QVERIFY(gridview->count() == 0);
1237 QVERIFY(gridview->currentItem() == 0);
1238 QVERIFY(gridview->contentY() == 0);
1239 QVERIFY(gridview->currentIndex() == -1);
1241 // confirm sanity when adding an item to cleared list
1242 model.addItem("New", "1");
1243 QTRY_COMPARE(gridview->count(), 1);
1244 QVERIFY(gridview->currentItem() != 0);
1245 QVERIFY(gridview->currentIndex() == 0);
1250 void tst_QQuickGridView::moved_defaultLayout(QQuickGridView::Flow flow,
1251 Qt::LayoutDirection horizLayout,
1252 QQuickItemView::VerticalLayoutDirection verticalLayout)
1254 QFETCH(qreal, contentYRowOffset);
1258 QFETCH(int, from_ttb);
1259 QFETCH(int, to_ttb);
1260 QFETCH(int, count_ttb);
1261 QFETCH(qreal, rowOffsetAfterMove);
1263 QQuickView *canvas = getView();
1266 for (int i = 0; i < 30; i++)
1267 model.addItem("Item" + QString::number(i), "");
1269 QQmlContext *ctxt = canvas->rootContext();
1270 ctxt->setContextProperty("testModel", &model);
1271 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
1272 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
1273 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
1274 canvas->setSource(testFileUrl("layouts.qml"));
1276 qApp->processEvents();
1278 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1279 QTRY_VERIFY(gridview != 0);
1280 QQuickItem *contentItem = gridview->contentItem();
1281 QTRY_VERIFY(contentItem != 0);
1282 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1284 QQuickItem *currentItem = gridview->currentItem();
1285 QTRY_VERIFY(currentItem != 0);
1287 if (flow == QQuickGridView::FlowTopToBottom) {
1292 if (setContentPos(gridview, contentYRowOffset))
1293 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1295 model.moveItems(from, to, count);
1296 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1298 // Confirm items positioned correctly and indexes correct
1299 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1300 int firstVisibleIndex = -1;
1301 for (int i=0; i<items.count(); i++) {
1302 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1303 if (item && item->isVisible()) {
1304 firstVisibleIndex = i;
1308 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1310 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1311 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1313 ( (flow == QQuickGridView::FlowLeftToRight && i >= firstVisibleIndex + (3*6))
1314 || flow == QQuickGridView::FlowTopToBottom && i >= firstVisibleIndex + (5*3) ) ) {
1315 continue; // index has moved out of view
1317 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1318 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
1319 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1321 QTRY_COMPARE(name->text(), model.name(i));
1323 // current index should have been updated
1324 if (item == currentItem)
1325 QTRY_COMPARE(gridview->currentIndex(), i);
1328 releaseView(canvas);
1331 void tst_QQuickGridView::moved_defaultLayout_data()
1333 QTest::addColumn<qreal>("contentYRowOffset");
1334 QTest::addColumn<int>("from");
1335 QTest::addColumn<int>("to");
1336 QTest::addColumn<int>("count");
1337 QTest::addColumn<int>("from_ttb");
1338 QTest::addColumn<int>("to_ttb");
1339 QTest::addColumn<int>("count_ttb");
1340 QTest::addColumn<qreal>("rowOffsetAfterMove");
1342 // model starts with 30 items, each 80x60, in area 240x320
1343 // 18 items should be visible at a time
1345 // The first visible item should not move upwards and out of the view
1346 // if items are moved/removed before it.
1349 QTest::newRow("move 1 forwards, within visible items")
1355 QTest::newRow("move 1 forwards, from non-visible -> visible")
1361 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1362 << 2.0 // // show 6-23
1367 QTest::newRow("move 1 forwards, from visible -> non-visible")
1373 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1380 QTest::newRow("move 1 backwards, within visible items")
1386 QTest::newRow("move 1 backwards, within visible items (to first index)")
1392 QTest::newRow("move 1 backwards, from non-visible -> visible")
1398 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1404 QTest::newRow("move 1 backwards, from visible -> non-visible")
1408 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1410 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1414 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1417 QTest::newRow("move multiple forwards, within visible items")
1423 QTest::newRow("move multiple backwards, within visible items (move first item)")
1429 QTest::newRow("move multiple forwards, before visible items")
1431 << 3 << 4 << 3 // 3, 4, 5 move to after 6
1433 << 1.0; // row of 3,4,5 has moved down
1435 QTest::newRow("move multiple forwards, from non-visible -> visible")
1439 << 1.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1441 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1445 << 1.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1447 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1451 << 1.0; // top two rows removed, third row is now the first visible
1453 QTest::newRow("move multiple forwards, to bottom of view")
1459 QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1465 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1471 QTest::newRow("move multiple forwards, from visible -> non-visible")
1477 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1484 QTest::newRow("move multiple backwards, within visible items")
1490 QTest::newRow("move multiple backwards, from non-visible -> visible")
1496 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1502 QTest::newRow("move multiple backwards, from visible -> non-visible")
1506 << -1.0; // to minimize movement, items are added above visible area, all items move up by 1 row
1508 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1512 << -1.0; // 16,17,18 move to above item 0, all items move up by 1 row
1515 void tst_QQuickGridView::multipleChanges(bool condensed)
1517 QFETCH(int, startCount);
1518 QFETCH(QList<ListChange>, changes);
1519 QFETCH(int, newCount);
1520 QFETCH(int, newCurrentIndex);
1522 QQuickView *canvas = getView();
1525 for (int i = 0; i < startCount; i++)
1526 model.addItem("Item" + QString::number(i), "");
1528 canvas->rootContext()->setContextProperty("testModel", &model);
1529 canvas->setSource(testFileUrl("gridview1.qml"));
1531 qApp->processEvents();
1533 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1534 QTRY_VERIFY(gridview != 0);
1535 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1537 for (int i=0; i<changes.count(); i++) {
1538 switch (changes[i].type) {
1539 case ListChange::Inserted:
1541 QList<QPair<QString, QString> > items;
1542 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1543 items << qMakePair(QString("new item " + j), QString::number(j));
1544 model.insertItems(changes[i].index, items);
1547 case ListChange::Removed:
1548 model.removeItems(changes[i].index, changes[i].count);
1550 case ListChange::Moved:
1551 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1553 case ListChange::SetCurrent:
1554 gridview->setCurrentIndex(changes[i].index);
1556 case ListChange::SetContentY:
1557 gridview->setContentY(changes[i].pos);
1561 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1564 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1566 QCOMPARE(gridview->count(), newCount);
1567 QCOMPARE(gridview->count(), model.count());
1568 QCOMPARE(gridview->currentIndex(), newCurrentIndex);
1572 QQuickItem *contentItem = gridview->contentItem();
1573 QTRY_VERIFY(contentItem != 0);
1574 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1575 for (int i=0; i < model.count() && i < itemCount; ++i) {
1576 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1577 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1578 name = findItem<QQuickText>(contentItem, "textName", i);
1580 QTRY_COMPARE(name->text(), model.name(i));
1581 number = findItem<QQuickText>(contentItem, "textNumber", i);
1582 QVERIFY(number != 0);
1583 QTRY_COMPARE(number->text(), model.number(i));
1586 releaseView(canvas);
1589 void tst_QQuickGridView::multipleChanges_data()
1591 QTest::addColumn<int>("startCount");
1592 QTest::addColumn<QList<ListChange> >("changes");
1593 QTest::addColumn<int>("newCount");
1594 QTest::addColumn<int>("newCurrentIndex");
1596 QList<ListChange> changes;
1598 for (int i=1; i<30; i++)
1599 changes << ListChange::remove(0);
1600 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1602 changes << ListChange::remove(0);
1603 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1606 changes << ListChange::setCurrent(29);
1607 for (int i=29; i>0; i--)
1608 changes << ListChange::remove(i);
1609 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1611 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1612 << ListChange::remove(0, 1)
1613 << ListChange::insert(0, 1)
1616 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1617 << ListChange::setCurrent(2)
1618 << ListChange::remove(2, 1)
1619 << ListChange::insert(2, 1)
1622 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1623 << ListChange::setCurrent(1)
1624 << ListChange::remove(1, 3)
1625 << ListChange::insert(2, 2)
1628 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1629 << ListChange::setCurrent(2)
1630 << ListChange::remove(1, 3)
1631 << ListChange::move(1, 5, 1)
1634 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1635 << ListChange::setCurrent(5)
1636 << ListChange::remove(4, 3)
1637 << ListChange::move(4, 1, 1)
1641 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1642 << ListChange::insert(0, 2)
1643 << ListChange::insert(0, 4)
1644 << ListChange::insert(0, 6)
1647 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1648 << ListChange::insert(0, 2)
1649 << ListChange::insert(0, 4)
1650 << ListChange::insert(0, 6)
1651 << ListChange::setCurrent(3)
1652 << ListChange::insert(3, 2)
1655 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1656 << ListChange::insert(0, 30)
1657 << ListChange::remove(0, 30)
1660 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1661 << ListChange::insert(1)
1662 << ListChange::setCurrent(1)
1663 << ListChange::remove(1)
1666 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1667 << ListChange::insert(0, 10)
1668 << ListChange::remove(5, 10)
1671 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1672 << ListChange::insert(0, 3)
1673 << ListChange::move(0, 10, 3)
1676 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1677 << ListChange::insert(0, 3)
1678 << ListChange::move(0, 8, 5)
1681 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1682 << ListChange::setCurrent(9)
1683 << ListChange::insert(10, 3)
1684 << ListChange::move(8, 0, 5)
1688 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1689 << ListChange::setCurrent(1)
1690 << ListChange::move(1, 2, 2)
1691 << ListChange::move(2, 1, 2)
1694 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1695 << ListChange::setCurrent(2)
1696 << ListChange::move(1, 2, 3)
1697 << ListChange::move(3, 0, 5)
1700 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1701 << ListChange::setCurrent(5)
1702 << ListChange::move(5, 0, 1)
1703 << ListChange::remove(0)
1706 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1707 << ListChange::setCurrent(5)
1708 << ListChange::move(5, 0, 1)
1709 << ListChange::insert(0)
1712 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1713 << ListChange::setCurrent(1)
1714 << ListChange::move(5, 1, 3)
1715 << ListChange::remove(1, 3)
1718 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1719 << ListChange::setCurrent(5)
1720 << ListChange::move(5, 1, 3)
1721 << ListChange::insert(1, 5)
1724 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1725 << ListChange::setCurrent(3)
1726 << ListChange::move(0, 1, 2)
1727 << ListChange::insert(3, 5)
1731 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1732 << ListChange::insert(0, 5)
1733 << ListChange::setCurrent(-1)
1734 << ListChange::remove(0, 5)
1735 << ListChange::insert(0, 5)
1738 QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1739 << ListChange::remove(20, 5)
1740 << ListChange::setContentY(20)
1743 QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1744 << ListChange::insert(9, 5)
1745 << ListChange::setContentY(20)
1748 QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1749 << ListChange::move(15, 8, 3)
1750 << ListChange::setContentY(0)
1753 QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1754 << ListChange::setContentY(20)
1755 << ListChange::remove(0, 30)
1756 << ListChange::insert(0, 2)
1757 << ListChange::setContentY(0)
1762 void tst_QQuickGridView::swapWithFirstItem()
1765 QQuickView *canvas = createView();
1768 for (int i = 0; i < 30; i++)
1769 model.addItem("Item" + QString::number(i), "");
1771 canvas->rootContext()->setContextProperty("testModel", &model);
1772 canvas->setSource(testFileUrl("gridview1.qml"));
1774 qApp->processEvents();
1776 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1777 QTRY_VERIFY(gridview != 0);
1779 // ensure content position is stable
1780 gridview->setContentY(0);
1781 model.moveItem(10, 0);
1782 QTRY_VERIFY(gridview->contentY() == 0);
1787 void tst_QQuickGridView::currentIndex()
1790 for (int i = 0; i < 60; i++)
1791 model.addItem("Item" + QString::number(i), QString::number(i));
1793 QQuickView *canvas = new QQuickView(0);
1794 canvas->setGeometry(0,0,240,320);
1797 QQmlContext *ctxt = canvas->rootContext();
1798 ctxt->setContextProperty("testModel", &model);
1800 QString filename(testFile("gridview-initCurrent.qml"));
1801 canvas->setSource(QUrl::fromLocalFile(filename));
1803 qApp->processEvents();
1805 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1806 QVERIFY(gridview != 0);
1807 QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1809 QQuickItem *contentItem = gridview->contentItem();
1810 QVERIFY(contentItem != 0);
1812 // current item should be third item
1813 QCOMPARE(gridview->currentIndex(), 35);
1814 QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1815 QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1816 QCOMPARE(gridview->contentY(), 400.0);
1818 gridview->setCurrentIndex(0);
1819 QCOMPARE(gridview->currentIndex(), 0);
1820 // confirm that the velocity is updated
1821 QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1823 // footer should become visible if it is out of view, and then current index moves to the first row
1824 canvas->rootObject()->setProperty("showFooter", true);
1825 QTRY_VERIFY(gridview->footerItem());
1826 gridview->setCurrentIndex(model.count()-3);
1827 QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1828 gridview->setCurrentIndex(model.count()-2);
1829 QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1830 canvas->rootObject()->setProperty("showFooter", false);
1832 // header should become visible if it is out of view, and then current index moves to the last row
1833 canvas->rootObject()->setProperty("showHeader", true);
1834 QTRY_VERIFY(gridview->headerItem());
1835 gridview->setCurrentIndex(3);
1836 QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1837 gridview->setCurrentIndex(1);
1838 QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1839 canvas->rootObject()->setProperty("showHeader", false);
1841 // turn off auto highlight
1842 gridview->setHighlightFollowsCurrentItem(false);
1843 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1844 QVERIFY(gridview->highlightItem());
1845 qreal hlPosX = gridview->highlightItem()->x();
1846 qreal hlPosY = gridview->highlightItem()->y();
1848 gridview->setCurrentIndex(5);
1849 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1850 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1852 // insert item before currentIndex
1853 gridview->setCurrentIndex(28);
1854 model.insertItem(0, "Foo", "1111");
1855 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1857 // check removing highlight by setting currentIndex to -1;
1858 gridview->setCurrentIndex(-1);
1860 QCOMPARE(gridview->currentIndex(), -1);
1861 QVERIFY(!gridview->highlightItem());
1862 QVERIFY(!gridview->currentItem());
1864 // moving currentItem out of view should make it invisible
1865 gridview->setCurrentIndex(0);
1866 QTRY_VERIFY(gridview->currentItem()->isVisible());
1867 gridview->setContentY(200);
1868 QTRY_VERIFY(!gridview->currentItem()->isVisible());
1873 void tst_QQuickGridView::noCurrentIndex()
1876 for (int i = 0; i < 60; i++)
1877 model.addItem("Item" + QString::number(i), QString::number(i));
1879 QQuickView *canvas = new QQuickView(0);
1880 canvas->setGeometry(0,0,240,320);
1882 QQmlContext *ctxt = canvas->rootContext();
1883 ctxt->setContextProperty("testModel", &model);
1885 QString filename(testFile("gridview-noCurrent.qml"));
1886 canvas->setSource(QUrl::fromLocalFile(filename));
1888 qApp->processEvents();
1890 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1891 QVERIFY(gridview != 0);
1892 QQuickItem *contentItem = gridview->contentItem();
1893 QVERIFY(contentItem != 0);
1894 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1896 // current index should be -1
1897 QCOMPARE(gridview->currentIndex(), -1);
1898 QVERIFY(!gridview->currentItem());
1899 QVERIFY(!gridview->highlightItem());
1900 QCOMPARE(gridview->contentY(), 0.0);
1902 gridview->setCurrentIndex(5);
1903 QCOMPARE(gridview->currentIndex(), 5);
1904 QVERIFY(gridview->currentItem());
1905 QVERIFY(gridview->highlightItem());
1910 void tst_QQuickGridView::keyNavigation()
1912 QFETCH(QQuickGridView::Flow, flow);
1913 QFETCH(Qt::LayoutDirection, layoutDirection);
1914 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
1915 QFETCH(Qt::Key, forwardsKey);
1916 QFETCH(Qt::Key, backwardsKey);
1917 QFETCH(QPointF, contentPosAtFirstItem);
1918 QFETCH(QPointF, contentPosAtLastItem);
1919 QFETCH(int, indexRightOf7);
1920 QFETCH(int, indexLeftOf7);
1921 QFETCH(int, indexUpFrom7);
1922 QFETCH(int, indexDownFrom7);
1925 for (int i = 0; i < 18; i++)
1926 model.addItem("Item" + QString::number(i), "");
1928 QQuickView *canvas = getView();
1929 canvas->rootContext()->setContextProperty("testModel", &model);
1930 canvas->setSource(testFileUrl("gridview1.qml"));
1932 qApp->processEvents();
1934 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1935 QTRY_VERIFY(gridview != 0);
1936 gridview->setFlow(flow);
1937 gridview->setLayoutDirection(layoutDirection);
1938 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
1939 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1941 canvas->requestActivateWindow();
1942 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1943 QCOMPARE(gridview->currentIndex(), 0);
1945 QTest::keyClick(canvas, forwardsKey);
1946 QCOMPARE(gridview->currentIndex(), 1);
1948 QTest::keyClick(canvas, backwardsKey);
1949 QCOMPARE(gridview->currentIndex(), 0);
1951 gridview->setCurrentIndex(7);
1952 gridview->moveCurrentIndexRight();
1953 QCOMPARE(gridview->currentIndex(), indexRightOf7);
1954 gridview->moveCurrentIndexLeft();
1955 QCOMPARE(gridview->currentIndex(), 7);
1956 gridview->moveCurrentIndexLeft();
1957 QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1958 gridview->moveCurrentIndexRight();
1959 QCOMPARE(gridview->currentIndex(), 7);
1960 gridview->moveCurrentIndexUp();
1961 QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1962 gridview->moveCurrentIndexDown();
1963 QCOMPARE(gridview->currentIndex(), 7);
1964 gridview->moveCurrentIndexDown();
1965 QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1967 gridview->setCurrentIndex(7);
1968 QTest::keyClick(canvas, Qt::Key_Right);
1969 QCOMPARE(gridview->currentIndex(), indexRightOf7);
1970 QTest::keyClick(canvas, Qt::Key_Left);
1971 QCOMPARE(gridview->currentIndex(), 7);
1972 QTest::keyClick(canvas, Qt::Key_Left);
1973 QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1974 QTest::keyClick(canvas, Qt::Key_Right);
1975 QCOMPARE(gridview->currentIndex(), 7);
1976 QTest::keyClick(canvas, Qt::Key_Up);
1977 QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1978 QTest::keyClick(canvas, Qt::Key_Down);
1979 QCOMPARE(gridview->currentIndex(), 7);
1980 QTest::keyClick(canvas, Qt::Key_Down);
1981 QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1983 // hold down a key to go forwards
1984 gridview->setCurrentIndex(0);
1985 for (int i=0; i<model.count()-1; i++) {
1986 // QTest::qWait(500);
1987 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
1988 QTRY_COMPARE(gridview->currentIndex(), i+1);
1990 QTest::keyRelease(canvas, forwardsKey);
1991 QTRY_COMPARE(gridview->currentIndex(), model.count()-1);
1992 QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
1993 QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
1995 // hold down a key to go backwards
1996 for (int i=model.count()-1; i > 0; i--) {
1997 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
1998 QTRY_COMPARE(gridview->currentIndex(), i-1);
2000 QTest::keyRelease(canvas, backwardsKey);
2001 QTRY_COMPARE(gridview->currentIndex(), 0);
2002 QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2003 QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2006 QVERIFY(!gridview->isWrapEnabled());
2007 QTest::keyClick(canvas, forwardsKey);
2008 QCOMPARE(gridview->currentIndex(), 1);
2009 QTest::keyClick(canvas, backwardsKey);
2010 QCOMPARE(gridview->currentIndex(), 0);
2012 QTest::keyClick(canvas, backwardsKey);
2013 QCOMPARE(gridview->currentIndex(), 0);
2016 gridview->setWrapEnabled(true);
2017 QVERIFY(gridview->isWrapEnabled());
2019 QTest::keyClick(canvas, backwardsKey);
2020 QCOMPARE(gridview->currentIndex(), model.count()-1);
2021 QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
2022 QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
2024 QTest::keyClick(canvas, forwardsKey);
2025 QCOMPARE(gridview->currentIndex(), 0);
2026 QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2027 QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2029 releaseView(canvas);
2032 void tst_QQuickGridView::keyNavigation_data()
2034 QTest::addColumn<QQuickGridView::Flow>("flow");
2035 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2036 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2037 QTest::addColumn<Qt::Key>("forwardsKey");
2038 QTest::addColumn<Qt::Key>("backwardsKey");
2039 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2040 QTest::addColumn<QPointF>("contentPosAtLastItem");
2041 QTest::addColumn<int>("indexRightOf7");
2042 QTest::addColumn<int>("indexLeftOf7");
2043 QTest::addColumn<int>("indexUpFrom7");
2044 QTest::addColumn<int>("indexDownFrom7");
2046 QTest::newRow("LeftToRight, LtR, TtB")
2047 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
2048 << Qt::Key_Right << Qt::Key_Left
2051 << 8 << 6 << 4 << 10;
2053 QTest::newRow("LeftToRight, RtL, TtB")
2054 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
2055 << Qt::Key_Left << Qt::Key_Right
2058 << 6 << 8 << 4 << 10;
2060 QTest::newRow("LeftToRight, LtR, BtT")
2061 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
2062 << Qt::Key_Right << Qt::Key_Left
2065 << 8 << 6 << 10 << 4;
2067 QTest::newRow("LeftToRight, RtL, BtT")
2068 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
2069 << Qt::Key_Left << Qt::Key_Right
2072 << 6 << 8 << 10 << 4;
2074 QTest::newRow("TopToBottom, LtR, TtB")
2075 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
2076 << Qt::Key_Down << Qt::Key_Up
2079 << 12 << 2 << 6 << 8;
2081 QTest::newRow("TopToBottom, RtL, TtB")
2082 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
2083 << Qt::Key_Down << Qt::Key_Up
2086 << 2 << 12 << 6 << 8;
2088 QTest::newRow("TopToBottom, LtR, BtT")
2089 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
2090 << Qt::Key_Up << Qt::Key_Down
2092 << QPointF(80, -320)
2093 << 12 << 2 << 8 << 6;
2095 QTest::newRow("TopToBottom, RtL, BtT")
2096 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
2097 << Qt::Key_Up << Qt::Key_Down
2098 << QPointF(-240, -320)
2099 << QPointF(-320, -320)
2100 << 2 << 12 << 8 << 6;
2103 void tst_QQuickGridView::changeFlow()
2105 QQuickView *canvas = createView();
2108 for (int i = 0; i < 30; i++)
2109 model.addItem("Item" + QString::number(i), QString::number(i));
2111 QQmlContext *ctxt = canvas->rootContext();
2112 ctxt->setContextProperty("testModel", &model);
2113 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2114 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2115 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2117 canvas->setSource(testFileUrl("layouts.qml"));
2118 qApp->processEvents();
2120 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2121 QTRY_VERIFY(gridview != 0);
2123 QQuickItem *contentItem = gridview->contentItem();
2124 QTRY_VERIFY(contentItem != 0);
2126 // Confirm items positioned correctly and indexes correct
2127 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2128 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2129 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2130 if (!item) qWarning() << "Item" << i << "not found";
2132 QTRY_COMPARE(item->x(), qreal((i%3)*80));
2133 QTRY_COMPARE(item->y(), qreal((i/3)*60));
2134 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2135 QTRY_VERIFY(name != 0);
2136 QTRY_COMPARE(name->text(), model.name(i));
2137 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2138 QTRY_VERIFY(number != 0);
2139 QTRY_COMPARE(number->text(), model.number(i));
2142 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2144 // Confirm items positioned correctly and indexes correct
2145 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2146 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2147 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2148 if (!item) qWarning() << "Item" << i << "not found";
2150 QTRY_COMPARE(item->x(), qreal((i/5)*80));
2151 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2152 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2153 QTRY_VERIFY(name != 0);
2154 QTRY_COMPARE(name->text(), model.name(i));
2155 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2156 QTRY_VERIFY(number != 0);
2157 QTRY_COMPARE(number->text(), model.number(i));
2160 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2162 // Confirm items positioned correctly and indexes correct
2163 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2164 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2165 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2166 if (!item) qWarning() << "Item" << i << "not found";
2168 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
2169 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2170 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2171 QTRY_VERIFY(name != 0);
2172 QTRY_COMPARE(name->text(), model.name(i));
2173 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2174 QTRY_VERIFY(number != 0);
2175 QTRY_COMPARE(number->text(), model.number(i));
2177 gridview->setContentX(100);
2178 QTRY_COMPARE(gridview->contentX(), 100.);
2179 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2180 QTRY_COMPARE(gridview->contentX(), 0.);
2182 // Confirm items positioned correctly and indexes correct
2183 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2184 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2185 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2186 if (!item) qWarning() << "Item" << i << "not found";
2188 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
2189 QTRY_COMPARE(item->y(), qreal((i/3)*60));
2190 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2191 QTRY_VERIFY(name != 0);
2192 QTRY_COMPARE(name->text(), model.name(i));
2193 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2194 QTRY_VERIFY(number != 0);
2195 QTRY_COMPARE(number->text(), model.number(i));
2201 void tst_QQuickGridView::defaultValues()
2204 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
2205 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2207 QTRY_VERIFY(obj != 0);
2208 QTRY_VERIFY(obj->model() == QVariant());
2209 QTRY_VERIFY(obj->delegate() == 0);
2210 QTRY_COMPARE(obj->currentIndex(), -1);
2211 QTRY_VERIFY(obj->currentItem() == 0);
2212 QTRY_COMPARE(obj->count(), 0);
2213 QTRY_VERIFY(obj->highlight() == 0);
2214 QTRY_VERIFY(obj->highlightItem() == 0);
2215 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
2216 QTRY_VERIFY(obj->flow() == 0);
2217 QTRY_COMPARE(obj->isWrapEnabled(), false);
2218 QTRY_COMPARE(obj->cacheBuffer(), 0);
2219 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
2220 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2224 void tst_QQuickGridView::properties()
2227 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
2228 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2230 QTRY_VERIFY(obj != 0);
2231 QTRY_VERIFY(obj->model() != QVariant());
2232 QTRY_VERIFY(obj->delegate() != 0);
2233 QTRY_COMPARE(obj->currentIndex(), 0);
2234 QTRY_VERIFY(obj->currentItem() != 0);
2235 QTRY_COMPARE(obj->count(), 4);
2236 QTRY_VERIFY(obj->highlight() != 0);
2237 QTRY_VERIFY(obj->highlightItem() != 0);
2238 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
2239 QTRY_VERIFY(obj->flow() == 0);
2240 QTRY_COMPARE(obj->isWrapEnabled(), true);
2241 QTRY_COMPARE(obj->cacheBuffer(), 200);
2242 QTRY_COMPARE(obj->cellWidth(), qreal(100));
2243 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2247 void tst_QQuickGridView::propertyChanges()
2249 QQuickView *canvas = createView();
2250 QTRY_VERIFY(canvas);
2251 canvas->setSource(testFileUrl("propertychangestest.qml"));
2253 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2254 QTRY_VERIFY(gridView);
2256 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
2257 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
2258 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
2259 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
2261 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2262 QTRY_COMPARE(gridView->cacheBuffer(), 10);
2263 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2265 gridView->setWrapEnabled(false);
2266 gridView->setCacheBuffer(3);
2267 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2269 QTRY_COMPARE(gridView->isWrapEnabled(), false);
2270 QTRY_COMPARE(gridView->cacheBuffer(), 3);
2271 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2273 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2274 QTRY_COMPARE(cacheBufferSpy.count(),1);
2275 QTRY_COMPARE(flowSpy.count(),1);
2277 gridView->setWrapEnabled(false);
2278 gridView->setCacheBuffer(3);
2279 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2281 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2282 QTRY_COMPARE(cacheBufferSpy.count(),1);
2283 QTRY_COMPARE(flowSpy.count(),1);
2285 gridView->setFlow(QQuickGridView::FlowLeftToRight);
2286 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2288 gridView->setWrapEnabled(true);
2289 gridView->setCacheBuffer(5);
2290 gridView->setLayoutDirection(Qt::RightToLeft);
2292 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2293 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2294 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2296 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2297 QTRY_COMPARE(cacheBufferSpy.count(),2);
2298 QTRY_COMPARE(layoutSpy.count(),1);
2299 QTRY_COMPARE(flowSpy.count(),2);
2301 gridView->setWrapEnabled(true);
2302 gridView->setCacheBuffer(5);
2303 gridView->setLayoutDirection(Qt::RightToLeft);
2305 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2306 QTRY_COMPARE(cacheBufferSpy.count(),2);
2307 QTRY_COMPARE(layoutSpy.count(),1);
2308 QTRY_COMPARE(flowSpy.count(),2);
2310 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2311 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2312 QTRY_COMPARE(flowSpy.count(),3);
2314 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2315 QTRY_COMPARE(flowSpy.count(),3);
2320 void tst_QQuickGridView::componentChanges()
2322 QQuickView *canvas = createView();
2323 QTRY_VERIFY(canvas);
2324 canvas->setSource(testFileUrl("propertychangestest.qml"));
2326 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2327 QTRY_VERIFY(gridView);
2329 QQmlComponent component(canvas->engine());
2330 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2332 QQmlComponent delegateComponent(canvas->engine());
2333 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2335 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2336 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2337 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2338 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2339 QSignalSpy headerItemSpy(gridView, SIGNAL(headerItemChanged()));
2340 QSignalSpy footerItemSpy(gridView, SIGNAL(footerItemChanged()));
2342 gridView->setHighlight(&component);
2343 gridView->setDelegate(&delegateComponent);
2344 gridView->setHeader(&component);
2345 gridView->setFooter(&component);
2347 QTRY_COMPARE(gridView->highlight(), &component);
2348 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2349 QTRY_COMPARE(gridView->header(), &component);
2350 QTRY_COMPARE(gridView->footer(), &component);
2352 QVERIFY(gridView->headerItem());
2353 QVERIFY(gridView->footerItem());
2355 QTRY_COMPARE(highlightSpy.count(),1);
2356 QTRY_COMPARE(delegateSpy.count(),1);
2357 QTRY_COMPARE(headerSpy.count(),1);
2358 QTRY_COMPARE(footerSpy.count(),1);
2359 QTRY_COMPARE(headerItemSpy.count(),1);
2360 QTRY_COMPARE(footerItemSpy.count(),1);
2362 gridView->setHighlight(&component);
2363 gridView->setDelegate(&delegateComponent);
2364 gridView->setHeader(&component);
2365 gridView->setFooter(&component);
2367 QTRY_COMPARE(highlightSpy.count(),1);
2368 QTRY_COMPARE(delegateSpy.count(),1);
2369 QTRY_COMPARE(headerSpy.count(),1);
2370 QTRY_COMPARE(footerSpy.count(),1);
2371 QTRY_COMPARE(headerItemSpy.count(),1);
2372 QTRY_COMPARE(footerItemSpy.count(),1);
2377 void tst_QQuickGridView::modelChanges()
2379 QQuickView *canvas = createView();
2380 QTRY_VERIFY(canvas);
2381 canvas->setSource(testFileUrl("propertychangestest.qml"));
2383 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2384 QTRY_VERIFY(gridView);
2386 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2387 QTRY_VERIFY(alternateModel);
2388 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2389 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2391 gridView->setModel(modelVariant);
2392 QTRY_COMPARE(gridView->model(), modelVariant);
2393 QTRY_COMPARE(modelSpy.count(),1);
2395 gridView->setModel(modelVariant);
2396 QTRY_COMPARE(modelSpy.count(),1);
2398 gridView->setModel(QVariant());
2399 QTRY_COMPARE(modelSpy.count(),2);
2403 void tst_QQuickGridView::positionViewAtIndex()
2405 QQuickView *canvas = createView();
2408 for (int i = 0; i < 40; i++)
2409 model.addItem("Item" + QString::number(i), "");
2411 QQmlContext *ctxt = canvas->rootContext();
2412 ctxt->setContextProperty("testModel", &model);
2413 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2414 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2415 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2417 canvas->setSource(testFileUrl("layouts.qml"));
2419 qApp->processEvents();
2421 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2422 QTRY_VERIFY(gridview != 0);
2423 QQuickItem *contentItem = gridview->contentItem();
2424 QTRY_VERIFY(contentItem != 0);
2425 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2427 // Confirm items positioned correctly
2428 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2429 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2430 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2431 if (!item) qWarning() << "Item" << i << "not found";
2433 QTRY_COMPARE(item->x(), (i%3)*80.);
2434 QTRY_COMPARE(item->y(), (i/3)*60.);
2437 // Position on a currently visible item
2438 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2439 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2440 QTRY_COMPARE(gridview->contentY(), 60.);
2442 // Confirm items positioned correctly
2443 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2444 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2445 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2446 if (!item) qWarning() << "Item" << i << "not found";
2448 QTRY_COMPARE(item->x(), (i%3)*80.);
2449 QTRY_COMPARE(item->y(), (i/3)*60.);
2452 // Position on an item beyond the visible items
2453 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2454 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2455 QTRY_COMPARE(gridview->contentY(), 420.);
2457 // Confirm items positioned correctly
2458 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2459 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2460 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2461 if (!item) qWarning() << "Item" << i << "not found";
2463 QTRY_COMPARE(item->x(), (i%3)*80.);
2464 QTRY_COMPARE(item->y(), (i/3)*60.);
2467 // Position on an item that would leave empty space if positioned at the top
2468 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2469 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2470 QTRY_COMPARE(gridview->contentY(), 520.);
2472 // Confirm items positioned correctly
2473 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2474 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2475 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2476 if (!item) qWarning() << "Item" << i << "not found";
2478 QTRY_COMPARE(item->x(), (i%3)*80.);
2479 QTRY_COMPARE(item->y(), (i/3)*60.);
2482 // Position at the beginning again
2483 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2484 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2485 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2486 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2487 QTRY_COMPARE(gridview->contentY(), 0.);
2489 // Confirm items positioned correctly
2490 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2491 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2492 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2493 if (!item) qWarning() << "Item" << i << "not found";
2495 QTRY_COMPARE(item->x(), (i%3)*80.);
2496 QTRY_COMPARE(item->y(), (i/3)*60.);
2500 gridview->positionViewAtIndex(30, QQuickGridView::End);
2501 QTRY_COMPARE(gridview->contentY(), 340.);
2503 // Position in Center
2504 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2505 QTRY_COMPARE(gridview->contentY(), 170.);
2507 // Ensure at least partially visible
2508 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2509 QTRY_COMPARE(gridview->contentY(), 170.);
2511 gridview->setContentY(302);
2512 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2513 QTRY_COMPARE(gridview->contentY(), 302.);
2515 gridview->setContentY(360);
2516 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2517 QTRY_COMPARE(gridview->contentY(), 300.);
2519 gridview->setContentY(60);
2520 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2521 QTRY_COMPARE(gridview->contentY(), 60.);
2523 gridview->setContentY(20);
2524 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2525 QTRY_COMPARE(gridview->contentY(), 100.);
2527 // Ensure completely visible
2528 gridview->setContentY(120);
2529 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2530 QTRY_COMPARE(gridview->contentY(), 120.);
2532 gridview->setContentY(302);
2533 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2534 QTRY_COMPARE(gridview->contentY(), 300.);
2536 gridview->setContentY(60);
2537 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2538 QTRY_COMPARE(gridview->contentY(), 100.);
2540 // Test for Top To Bottom layout
2541 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2543 // Confirm items positioned correctly
2544 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2545 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2546 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2547 if (!item) qWarning() << "Item" << i << "not found";
2549 QTRY_COMPARE(item->x(), (i/5)*80.);
2550 QTRY_COMPARE(item->y(), (i%5)*60.);
2554 gridview->positionViewAtIndex(30, QQuickGridView::End);
2555 QTRY_COMPARE(gridview->contentX(), 320.);
2556 QTRY_COMPARE(gridview->contentY(), 0.);
2558 // Position in Center
2559 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2560 QTRY_COMPARE(gridview->contentX(), 160.);
2562 // Ensure at least partially visible
2563 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2564 QTRY_COMPARE(gridview->contentX(), 160.);
2566 gridview->setContentX(170);
2567 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2568 QTRY_COMPARE(gridview->contentX(), 170.);
2570 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2571 QTRY_COMPARE(gridview->contentX(), 320.);
2573 gridview->setContentX(170);
2574 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2575 QTRY_COMPARE(gridview->contentX(), 240.);
2577 // positionViewAtBeginning
2578 gridview->positionViewAtBeginning();
2579 QTRY_COMPARE(gridview->contentX(), 0.);
2581 gridview->setContentX(80);
2582 canvas->rootObject()->setProperty("showHeader", true);
2583 gridview->positionViewAtBeginning();
2584 QTRY_COMPARE(gridview->contentX(), -30.);
2586 // positionViewAtEnd
2587 gridview->positionViewAtEnd();
2588 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2590 gridview->setContentX(80);
2591 canvas->rootObject()->setProperty("showFooter", true);
2592 gridview->positionViewAtEnd();
2593 QTRY_COMPARE(gridview->contentX(), 430.);
2595 // set current item to outside visible view, position at beginning
2596 // and ensure highlight moves to current item
2597 gridview->setCurrentIndex(6);
2598 gridview->positionViewAtBeginning();
2599 QTRY_COMPARE(gridview->contentX(), -30.);
2600 QVERIFY(gridview->highlightItem());
2601 QCOMPARE(gridview->highlightItem()->x(), 80.);
2606 void tst_QQuickGridView::snapping()
2608 QQuickView *canvas = createView();
2611 for (int i = 0; i < 40; i++)
2612 model.addItem("Item" + QString::number(i), "");
2614 canvas->rootContext()->setContextProperty("testModel", &model);
2615 canvas->setSource(testFileUrl("gridview1.qml"));
2616 qApp->processEvents();
2618 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2619 QTRY_VERIFY(gridview != 0);
2621 gridview->setHeight(220);
2622 QCOMPARE(gridview->height(), 220.);
2624 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2625 QCOMPARE(gridview->contentY(), 80.);
2627 gridview->setContentY(0);
2628 QCOMPARE(gridview->contentY(), 0.);
2630 gridview->setSnapMode(QQuickGridView::SnapToRow);
2631 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2633 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2634 QCOMPARE(gridview->contentY(), 60.);
2636 gridview->positionViewAtIndex(15, QQuickGridView::End);
2637 QCOMPARE(gridview->contentY(), 120.);
2643 void tst_QQuickGridView::mirroring()
2645 QQuickView *canvasA = createView();
2646 canvasA->setSource(testFileUrl("mirroring.qml"));
2647 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2648 QTRY_VERIFY(gridviewA != 0);
2650 QQuickView *canvasB = createView();
2651 canvasB->setSource(testFileUrl("mirroring.qml"));
2652 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2653 QTRY_VERIFY(gridviewA != 0);
2654 qApp->processEvents();
2656 QList<QString> objectNames;
2657 objectNames << "item1" << "item2"; // << "item3"
2659 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2660 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2661 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2664 foreach (const QString objectName, objectNames)
2665 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2667 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2668 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2671 foreach (const QString objectName, objectNames)
2672 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2674 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2675 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2676 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2678 // LTR != LTR+mirror
2679 foreach (const QString objectName, objectNames)
2680 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2682 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2684 // RTL == LTR+mirror
2685 foreach (const QString objectName, objectNames)
2686 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2688 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2690 // RTL != RTL+mirror
2691 foreach (const QString objectName, objectNames)
2692 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2694 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2696 // LTR == RTL+mirror
2697 foreach (const QString objectName, objectNames)
2698 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2704 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2706 QQuickView *canvas = createView();
2709 for (int i = 0; i < 40; i++)
2710 model.addItem("Item" + QString::number(i), "");
2712 QQmlContext *ctxt = canvas->rootContext();
2713 ctxt->setContextProperty("testModel", &model);
2714 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2715 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2716 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2718 canvas->setSource(testFileUrl("layouts.qml"));
2719 qApp->processEvents();
2721 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2722 QTRY_VERIFY(gridview != 0);
2724 QQuickItem *contentItem = gridview->contentItem();
2725 QTRY_VERIFY(contentItem != 0);
2727 // Confirm items positioned correctly
2728 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2729 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2730 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2731 if (!item) qWarning() << "Item" << i << "not found";
2733 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2734 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2737 // Position on a currently visible item
2738 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2739 QTRY_COMPARE(gridview->contentX(), -320.);
2741 // Confirm items positioned correctly
2742 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2743 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2744 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2745 if (!item) qWarning() << "Item" << i << "not found";
2747 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2748 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2751 // Position on an item beyond the visible items
2752 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2753 QTRY_COMPARE(gridview->contentX(), -560.);
2755 // Confirm items positioned correctly
2756 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2757 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2758 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2759 if (!item) qWarning() << "Item" << i << "not found";
2761 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2762 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2765 // Position on an item that would leave empty space if positioned at the top
2766 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2767 QTRY_COMPARE(gridview->contentX(), -640.);
2769 // Confirm items positioned correctly
2770 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2771 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2772 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2773 if (!item) qWarning() << "Item" << i << "not found";
2775 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2776 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2779 // Position at the beginning again
2780 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2781 QTRY_COMPARE(gridview->contentX(), -240.);
2783 // Confirm items positioned correctly
2784 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2785 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2786 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2787 if (!item) qWarning() << "Item" << i << "not found";
2789 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2790 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2794 gridview->positionViewAtIndex(30, QQuickGridView::End);
2795 QTRY_COMPARE(gridview->contentX(), -560.);
2797 // Position in Center
2798 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2799 QTRY_COMPARE(gridview->contentX(), -400.);
2801 // Ensure at least partially visible
2802 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2803 QTRY_COMPARE(gridview->contentX(), -400.);
2805 gridview->setContentX(-555.);
2806 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2807 QTRY_COMPARE(gridview->contentX(), -555.);
2809 gridview->setContentX(-239);
2810 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2811 QTRY_COMPARE(gridview->contentX(), -320.);
2813 gridview->setContentX(-239);
2814 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2815 QTRY_COMPARE(gridview->contentX(), -400.);
2817 gridview->setContentX(-640);
2818 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2819 QTRY_COMPARE(gridview->contentX(), -560.);
2821 // Ensure completely visible
2822 gridview->setContentX(-400);
2823 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2824 QTRY_COMPARE(gridview->contentX(), -400.);
2826 gridview->setContentX(-315);
2827 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2828 QTRY_COMPARE(gridview->contentX(), -320.);
2830 gridview->setContentX(-640);
2831 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2832 QTRY_COMPARE(gridview->contentX(), -560.);
2837 void tst_QQuickGridView::resetModel()
2839 QQuickView *canvas = createView();
2841 QStringList strings;
2842 strings << "one" << "two" << "three";
2843 QStringListModel model(strings);
2845 QQmlContext *ctxt = canvas->rootContext();
2846 ctxt->setContextProperty("testModel", &model);
2848 canvas->setSource(testFileUrl("displaygrid.qml"));
2850 qApp->processEvents();
2852 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2853 QTRY_VERIFY(gridview != 0);
2854 QQuickItem *contentItem = gridview->contentItem();
2855 QTRY_VERIFY(contentItem != 0);
2856 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2858 QTRY_COMPARE(gridview->count(), model.rowCount());
2860 for (int i = 0; i < model.rowCount(); ++i) {
2861 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2862 QTRY_VERIFY(display != 0);
2863 QTRY_COMPARE(display->text(), strings.at(i));
2867 strings << "four" << "five" << "six" << "seven";
2868 model.setStringList(strings);
2870 QTRY_COMPARE(gridview->count(), model.rowCount());
2872 for (int i = 0; i < model.rowCount(); ++i) {
2873 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2874 QTRY_VERIFY(display != 0);
2875 QTRY_COMPARE(display->text(), strings.at(i));
2881 void tst_QQuickGridView::enforceRange()
2883 QQuickView *canvas = createView();
2886 for (int i = 0; i < 30; i++)
2887 model.addItem("Item" + QString::number(i), "");
2889 QQmlContext *ctxt = canvas->rootContext();
2890 ctxt->setContextProperty("testModel", &model);
2891 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2892 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2894 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2896 qApp->processEvents();
2897 QVERIFY(canvas->rootObject() != 0);
2899 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2900 QTRY_VERIFY(gridview != 0);
2902 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2903 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2904 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2905 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2907 QQuickItem *contentItem = gridview->contentItem();
2908 QTRY_VERIFY(contentItem != 0);
2910 // view should be positioned at the top of the range.
2911 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2913 QTRY_COMPARE(gridview->contentY(), -100.0);
2915 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2916 QTRY_VERIFY(name != 0);
2917 QTRY_COMPARE(name->text(), model.name(0));
2918 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2919 QTRY_VERIFY(number != 0);
2920 QTRY_COMPARE(number->text(), model.number(0));
2922 // Check currentIndex is updated when contentItem moves
2923 gridview->setContentY(0);
2924 QTRY_COMPARE(gridview->currentIndex(), 2);
2926 gridview->setCurrentIndex(5);
2927 QTRY_COMPARE(gridview->contentY(), 100.);
2930 for (int i = 0; i < 5; i++)
2931 model2.addItem("Item" + QString::number(i), "");
2933 ctxt->setContextProperty("testModel", &model2);
2934 QCOMPARE(gridview->count(), 5);
2939 void tst_QQuickGridView::enforceRange_rightToLeft()
2941 QQuickView *canvas = createView();
2944 for (int i = 0; i < 30; i++)
2945 model.addItem("Item" + QString::number(i), "");
2947 QQmlContext *ctxt = canvas->rootContext();
2948 ctxt->setContextProperty("testModel", &model);
2949 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2950 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2952 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2953 qApp->processEvents();
2954 QVERIFY(canvas->rootObject() != 0);
2956 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2957 QTRY_VERIFY(gridview != 0);
2959 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2960 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2961 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2963 QQuickItem *contentItem = gridview->contentItem();
2964 QVERIFY(contentItem != 0);
2966 // view should be positioned at the top of the range.
2967 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2969 QTRY_COMPARE(gridview->contentX(), -140.);
2970 QTRY_COMPARE(gridview->contentY(), 0.0);
2972 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2973 QTRY_VERIFY(name != 0);
2974 QTRY_COMPARE(name->text(), model.name(0));
2975 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2976 QTRY_VERIFY(number != 0);
2977 QTRY_COMPARE(number->text(), model.number(0));
2979 // Check currentIndex is updated when contentItem moves
2980 gridview->setContentX(-240);
2981 QTRY_COMPARE(gridview->currentIndex(), 3);
2983 gridview->setCurrentIndex(7);
2984 QTRY_COMPARE(gridview->contentX(), -340.);
2985 QTRY_COMPARE(gridview->contentY(), 0.0);
2988 for (int i = 0; i < 5; i++)
2989 model2.addItem("Item" + QString::number(i), "");
2991 ctxt->setContextProperty("testModel", &model2);
2992 QCOMPARE(gridview->count(), 5);
2997 void tst_QQuickGridView::QTBUG_8456()
2999 QQuickView *canvas = createView();
3001 canvas->setSource(testFileUrl("setindex.qml"));
3002 qApp->processEvents();
3004 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3005 QTRY_VERIFY(gridview != 0);
3007 QTRY_COMPARE(gridview->currentIndex(), 0);
3012 void tst_QQuickGridView::manualHighlight()
3014 QQuickView *canvas = createView();
3016 QString filename(testFile("manual-highlight.qml"));
3017 canvas->setSource(QUrl::fromLocalFile(filename));
3019 qApp->processEvents();
3021 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3022 QTRY_VERIFY(gridview != 0);
3024 QQuickItem *contentItem = gridview->contentItem();
3025 QTRY_VERIFY(contentItem != 0);
3027 QTRY_COMPARE(gridview->currentIndex(), 0);
3028 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3029 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3030 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3032 gridview->setCurrentIndex(2);
3034 QTRY_COMPARE(gridview->currentIndex(), 2);
3035 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3036 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3037 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3039 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
3041 QTRY_COMPARE(gridview->currentIndex(), 2);
3042 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3043 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3044 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3046 gridview->setFlow(QQuickGridView::FlowTopToBottom);
3047 QTRY_COMPARE(gridview->flow(), QQuickGridView::FlowTopToBottom);
3049 gridview->setCurrentIndex(0);
3050 QTRY_COMPARE(gridview->currentIndex(), 0);
3051 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3052 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3053 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3059 void tst_QQuickGridView::footer()
3061 QFETCH(QQuickGridView::Flow, flow);
3062 QFETCH(Qt::LayoutDirection, layoutDirection);
3063 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3064 QFETCH(QPointF, initialFooterPos);
3065 QFETCH(QPointF, changedFooterPos);
3066 QFETCH(QPointF, initialContentPos);
3067 QFETCH(QPointF, firstDelegatePos);
3068 QFETCH(QPointF, resizeContentPos);
3070 QQuickView *canvas = getView();
3074 for (int i = 0; i < 7; i++)
3075 model.addItem("Item" + QString::number(i), "");
3077 QQmlContext *ctxt = canvas->rootContext();
3078 ctxt->setContextProperty("testModel", &model);
3079 canvas->setSource(testFileUrl("footer.qml"));
3080 qApp->processEvents();
3082 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3083 QTRY_VERIFY(gridview != 0);
3084 gridview->setFlow(flow);
3085 gridview->setLayoutDirection(layoutDirection);
3086 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3087 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3089 QQuickItem *contentItem = gridview->contentItem();
3090 QTRY_VERIFY(contentItem != 0);
3092 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3094 QVERIFY(footer == gridview->footerItem());
3096 QCOMPARE(footer->pos(), initialFooterPos);
3097 QCOMPARE(footer->width(), 100.);
3098 QCOMPARE(footer->height(), 30.);
3099 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3101 if (flow == QQuickGridView::FlowLeftToRight)
3102 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + footer->height());
3104 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + footer->width());
3106 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3108 QCOMPARE(item->pos(), firstDelegatePos);
3110 if (flow == QQuickGridView::FlowLeftToRight) {
3111 // shrink by one row
3112 model.removeItem(2);
3113 if (verticalLayoutDirection == QQuickItemView::TopToBottom)
3114 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
3116 QTRY_COMPARE(footer->y(), initialFooterPos.y() + gridview->cellHeight());
3118 // shrink by one column
3119 model.removeItem(2);
3120 model.removeItem(3);
3121 if (layoutDirection == Qt::LeftToRight)
3122 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
3124 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
3130 QPointF posWhenNoItems(0, 0);
3131 if (layoutDirection == Qt::RightToLeft)
3132 posWhenNoItems.setX(flow == QQuickGridView::FlowLeftToRight ? gridview->width() - footer->width() : -footer->width());
3133 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3134 posWhenNoItems.setY(-footer->height());
3135 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3137 // if header is toggled, it shouldn't affect the footer position
3138 canvas->rootObject()->setProperty("showHeader", true);
3139 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
3140 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3141 canvas->rootObject()->setProperty("showHeader", false);
3144 for (int i = 0; i < 30; i++)
3145 model.addItem("Item" + QString::number(i), "");
3147 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
3148 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3150 QCOMPARE(footerItemSpy.count(), 1);
3152 footer = findItem<QQuickText>(contentItem, "footer");
3154 footer = findItem<QQuickText>(contentItem, "footer2");
3156 QVERIFY(footer == gridview->footerItem());
3158 QCOMPARE(footer->pos(), changedFooterPos);
3159 QCOMPARE(footer->width(), 50.);
3160 QCOMPARE(footer->height(), 20.);
3162 // changing the footer shouldn't change the content pos
3163 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3165 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3167 QCOMPARE(item->pos(), firstDelegatePos);
3169 gridview->positionViewAtEnd();
3170 footer->setHeight(10);
3171 footer->setWidth(40);
3172 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3174 releaseView(canvas);
3177 void tst_QQuickGridView::footer_data()
3179 QTest::addColumn<QQuickGridView::Flow>("flow");
3180 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3181 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3182 QTest::addColumn<QPointF>("initialFooterPos");
3183 QTest::addColumn<QPointF>("changedFooterPos");
3184 QTest::addColumn<QPointF>("initialContentPos");
3185 QTest::addColumn<QPointF>("firstDelegatePos");
3186 QTest::addColumn<QPointF>("resizeContentPos");
3188 // footer1 = 100 x 30
3189 // footer2 = 50 x 20
3192 // view height = 320
3194 // footer below items, bottom left
3195 QTest::newRow("LeftToRight, LtR, TtB")
3196 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3197 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
3198 << QPointF(0, 10 * 60) // 30 items = 10 rows
3201 << QPointF(0, (10 * 60) - 320 + 10);
3203 // footer below items, bottom right
3204 QTest::newRow("LeftToRight, RtL, TtB")
3205 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3206 << QPointF(240 - 100, 3 * 60)
3207 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
3209 << QPointF(240 - 80, 0)
3210 << QPointF(0, (10 * 60) - 320 + 10);
3212 // footer above items, top left
3213 QTest::newRow("LeftToRight, LtR, BtT")
3214 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3215 << QPointF(0, -(3 * 60) - 30)
3216 << QPointF(0, -(10 * 60) - 20)
3219 << QPointF(0, -(10 * 60) - 10);
3221 // footer above items, top right
3222 QTest::newRow("LeftToRight, RtL, BtT")
3223 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3224 << QPointF(240 - 100, -(3 * 60) - 30)
3225 << QPointF((240 - 100) + 50, -(10 * 60) - 20)
3227 << QPointF(240 - 80, -60)
3228 << QPointF(0, -(10 * 60) - 10);
3231 // footer to right of items, bottom right
3232 QTest::newRow("TopToBottom, LtR, TtB")
3233 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3234 << QPointF(2 * 80, 0) // 2 columns, cell width 80
3235 << QPointF(6 * 80, 0) // 30 items = 6 columns
3238 << QPointF((6 * 80) - 240 + 40, 0);
3240 // footer to left of items, bottom right
3241 QTest::newRow("TopToBottom, RtL, TtB")
3242 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3243 << QPointF(-(2 * 80) - 100, 0)
3244 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
3247 << QPointF(-(6 * 80) - 40, 0);
3249 // footer to right of items, top right
3250 QTest::newRow("TopToBottom, LtR, BtT")
3251 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3252 << QPointF(2 * 80, -30)
3253 << QPointF(6 * 80, -20)
3256 << QPointF((6 * 80) - 240 + 40, -320);
3258 // footer to left of items, top left
3259 QTest::newRow("TopToBottom, RtL, BtT")
3260 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3261 << QPointF(-(2 * 80) - 100, -30)
3262 << QPointF(-(6 * 80) - 50, -20)
3263 << QPointF(-240, -320)
3264 << QPointF(-80, -60)
3265 << QPointF(-(6 * 80) - 40, -320);
3268 void tst_QQuickGridView::header()
3270 QFETCH(QQuickGridView::Flow, flow);
3271 QFETCH(Qt::LayoutDirection, layoutDirection);
3272 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3273 QFETCH(QPointF, initialHeaderPos);
3274 QFETCH(QPointF, changedHeaderPos);
3275 QFETCH(QPointF, initialContentPos);
3276 QFETCH(QPointF, changedContentPos);
3277 QFETCH(QPointF, firstDelegatePos);
3278 QFETCH(QPointF, resizeContentPos);
3281 for (int i = 0; i < 30; i++)
3282 model.addItem("Item" + QString::number(i), "");
3284 QQuickView *canvas = getView();
3285 canvas->rootContext()->setContextProperty("testModel", &model);
3286 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3287 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3288 canvas->setSource(testFileUrl("header.qml"));
3290 qApp->processEvents();
3292 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3293 QTRY_VERIFY(gridview != 0);
3294 gridview->setFlow(flow);
3295 gridview->setLayoutDirection(layoutDirection);
3296 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3297 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3299 QQuickItem *contentItem = gridview->contentItem();
3300 QTRY_VERIFY(contentItem != 0);
3302 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3304 QVERIFY(header == gridview->headerItem());
3306 QCOMPARE(header->pos(), initialHeaderPos);
3307 QCOMPARE(header->width(), 100.);
3308 QCOMPARE(header->height(), 30.);
3309 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3311 if (flow == QQuickGridView::FlowLeftToRight)
3312 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + header->height());
3314 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + header->width());
3316 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3318 QCOMPARE(item->pos(), firstDelegatePos);
3321 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3322 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3324 for (int i = 0; i < 30; i++)
3325 model.addItem("Item" + QString::number(i), "");
3327 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
3328 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3330 QCOMPARE(headerItemSpy.count(), 1);
3332 header = findItem<QQuickText>(contentItem, "header");
3334 header = findItem<QQuickText>(contentItem, "header2");
3337 QVERIFY(header == gridview->headerItem());
3339 QCOMPARE(header->pos(), changedHeaderPos);
3340 QCOMPARE(header->width(), 50.);
3341 QCOMPARE(header->height(), 20.);
3342 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3344 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3346 QCOMPARE(item->pos(), firstDelegatePos);
3348 header->setHeight(10);
3349 header->setWidth(40);
3350 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3352 releaseView(canvas);
3355 // QTBUG-21207 header should become visible if view resizes from initial empty size
3358 canvas->rootContext()->setContextProperty("testModel", &model);
3359 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3360 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3361 canvas->setSource(testFileUrl("header.qml"));
3363 qApp->processEvents();
3365 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3366 QTRY_VERIFY(gridview != 0);
3367 gridview->setFlow(flow);
3368 gridview->setLayoutDirection(layoutDirection);
3369 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3370 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3372 gridview->setWidth(240);
3373 gridview->setHeight(320);
3374 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3375 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3377 releaseView(canvas);
3380 void tst_QQuickGridView::header_data()
3382 QTest::addColumn<QQuickGridView::Flow>("flow");
3383 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3384 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3385 QTest::addColumn<QPointF>("initialHeaderPos");
3386 QTest::addColumn<QPointF>("changedHeaderPos");
3387 QTest::addColumn<QPointF>("initialContentPos");
3388 QTest::addColumn<QPointF>("changedContentPos");
3389 QTest::addColumn<QPointF>("firstDelegatePos");
3390 QTest::addColumn<QPointF>("resizeContentPos");
3392 // header1 = 100 x 30
3393 // header2 = 50 x 20
3397 // header above items, top left
3398 QTest::newRow("LeftToRight, LtR, TtB")
3399 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3407 // header above items, top right
3408 QTest::newRow("LeftToRight, RtL, TtB")
3409 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3410 << QPointF(240 - 100, -30)
3411 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3417 // header below items, bottom left
3418 QTest::newRow("LeftToRight, LtR, BtT")
3419 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3422 << QPointF(0, -320 + 30)
3423 << QPointF(0, -320 + 20)
3425 << QPointF(0, -320 + 10);
3427 // header above items, top right
3428 QTest::newRow("LeftToRight, RtL, BtT")
3429 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3430 << QPointF(240 - 100, 0)
3431 << QPointF((240 - 100) + 50, 0)
3432 << QPointF(0, -320 + 30)
3433 << QPointF(0, -320 + 20)
3434 << QPointF(160, -60)
3435 << QPointF(0, -320 + 10);
3438 // header to left of items, bottom left
3439 QTest::newRow("TopToBottom, LtR, TtB")
3440 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3448 // header to right of items, bottom right
3449 QTest::newRow("TopToBottom, RtL, TtB")
3450 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3453 << QPointF(-(240 - 100), 0)
3454 << QPointF(-(240 - 50), 0)
3456 << QPointF(-(240 - 40), 0);
3458 // header to left of items, top left
3459 QTest::newRow("TopToBottom, LtR, BtT")
3460 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3461 << QPointF(-100, -30)
3462 << QPointF(-50, -20)
3463 << QPointF(-100, -320)
3464 << QPointF(-50, -320)
3466 << QPointF(-40, -320);
3468 // header to right of items, top right
3469 QTest::newRow("TopToBottom, RtL, BtT")
3470 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3473 << QPointF(-(240 - 100), -320)
3474 << QPointF(-(240 - 50), -320)
3475 << QPointF(-80, -60)
3476 << QPointF(-(240 - 40), -320);
3479 class GVAccessor : public QQuickGridView
3482 qreal minY() const { return minYExtent(); }
3483 qreal maxY() const { return maxYExtent(); }
3484 qreal minX() const { return minXExtent(); }
3485 qreal maxX() const { return maxXExtent(); }
3488 void tst_QQuickGridView::headerFooter()
3490 QFETCH(QQuickGridView::Flow, flow);
3491 QFETCH(Qt::LayoutDirection, layoutDirection);
3492 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3493 QFETCH(QPointF, headerPos);
3494 QFETCH(QPointF, footerPos);
3495 QFETCH(QPointF, minPos);
3496 QFETCH(QPointF, maxPos);
3498 QQuickView *canvas = getView();
3501 QQmlContext *ctxt = canvas->rootContext();
3502 ctxt->setContextProperty("testModel", &model);
3503 canvas->setSource(testFileUrl("headerfooter.qml"));
3505 qApp->processEvents();
3507 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3508 QTRY_VERIFY(gridview != 0);
3509 gridview->setFlow(flow);
3510 gridview->setLayoutDirection(layoutDirection);
3511 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3512 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3514 QQuickItem *contentItem = gridview->contentItem();
3515 QTRY_VERIFY(contentItem != 0);
3517 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3519 QCOMPARE(header->pos(), headerPos);
3521 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3523 QCOMPARE(footer->pos(), footerPos);
3525 QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), minPos.x());
3526 QCOMPARE(static_cast<GVAccessor*>(gridview)->minY(), minPos.y());
3527 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), maxPos.x());
3528 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxY(), maxPos.y());
3530 releaseView(canvas);
3533 void tst_QQuickGridView::headerFooter_data()
3535 QTest::addColumn<QQuickGridView::Flow>("flow");
3536 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3537 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3538 QTest::addColumn<QPointF>("headerPos");
3539 QTest::addColumn<QPointF>("footerPos");
3540 QTest::addColumn<QPointF>("minPos");
3541 QTest::addColumn<QPointF>("maxPos");
3543 // header is 240x20 (or 20x320 in TopToBottom)
3544 // footer is 240x30 (or 30x320 in TopToBottom)
3546 QTest::newRow("LeftToRight, LtR, TtB")
3547 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3548 << QPointF(0, -20) << QPointF(0, 0)
3549 << QPointF(0, 20) << QPointF(240, 20);
3551 QTest::newRow("LeftToRight, RtL, TtB")
3552 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3553 << QPointF(0, -20) << QPointF(0, 0)
3554 << QPointF(0, 20) << QPointF(240, 20);
3556 QTest::newRow("LeftToRight, LtR, BtT")
3557 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3558 << QPointF(0, 0) << QPointF(0, -30)
3559 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20); // content flow is reversed
3561 QTest::newRow("LeftToRight, RtL, BtT")
3562 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3563 << QPointF(0, 0) << QPointF(0, -30)
3564 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20); // content flow is reversed
3567 QTest::newRow("TopToBottom, LtR, TtB")
3568 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3569 << QPointF(-20, 0) << QPointF(0, 0)
3570 << QPointF(20, 0) << QPointF(20, 320);
3572 QTest::newRow("TopToBottom, RtL, TtB")
3573 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3574 << QPointF(0, 0) << QPointF(-30, 0)
3575 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320); // content flow is reversed
3577 QTest::newRow("TopToBottom, LtR, BtT")
3578 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3579 << QPointF(-20, -320) << QPointF(0, -320)
3580 << QPointF(20, 0) << QPointF(20, 320);
3582 QTest::newRow("TopToBottom, RtL, BtT")
3583 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3584 << QPointF(0, -320) << QPointF(-30, -320)
3585 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320); // content flow is reversed
3588 void tst_QQuickGridView::resetModel_headerFooter()
3590 // Resetting a model shouldn't crash in views with header/footer
3592 QQuickView *canvas = createView();
3595 for (int i = 0; i < 6; i++)
3596 model.addItem("Item" + QString::number(i), "");
3597 QQmlContext *ctxt = canvas->rootContext();
3598 ctxt->setContextProperty("testModel", &model);
3600 canvas->setSource(testFileUrl("headerfooter.qml"));
3601 qApp->processEvents();
3603 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3604 QTRY_VERIFY(gridview != 0);
3606 QQuickItem *contentItem = gridview->contentItem();
3607 QTRY_VERIFY(contentItem != 0);
3609 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3611 QCOMPARE(header->y(), -header->height());
3613 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3615 QCOMPARE(footer->y(), 80.*2);
3619 header = findItem<QQuickItem>(contentItem, "header");
3621 QCOMPARE(header->y(), -header->height());
3623 footer = findItem<QQuickItem>(contentItem, "footer");
3625 QCOMPARE(footer->y(), 80.*2);
3630 void tst_QQuickGridView::resizeViewAndRepaint()
3632 QQuickView *canvas = createView();
3636 for (int i = 0; i < 40; i++)
3637 model.addItem("Item" + QString::number(i), "");
3639 QQmlContext *ctxt = canvas->rootContext();
3640 ctxt->setContextProperty("testModel", &model);
3641 ctxt->setContextProperty("initialWidth", 240);
3642 ctxt->setContextProperty("initialHeight", 100);
3644 canvas->setSource(testFileUrl("resizeview.qml"));
3646 qApp->processEvents();
3648 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3649 QTRY_VERIFY(gridview != 0);
3650 QQuickItem *contentItem = gridview->contentItem();
3651 QTRY_VERIFY(contentItem != 0);
3652 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3654 // item at index 10 should not be currently visible
3655 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3657 gridview->setHeight(320);
3658 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3660 gridview->setHeight(100);
3661 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3663 // Ensure we handle -ve sizes
3664 gridview->setHeight(-100);
3665 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3667 gridview->setCacheBuffer(120);
3668 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3670 // ensure items in cache become visible
3671 gridview->setHeight(120);
3672 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3674 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3675 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3676 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3677 if (!item) qWarning() << "Item" << i << "not found";
3679 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3680 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3681 QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3684 // ensure items outside view become invisible
3685 gridview->setHeight(60);
3686 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3688 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3689 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3690 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3691 if (!item) qWarning() << "Item" << i << "not found";
3693 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3694 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3695 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3701 void tst_QQuickGridView::resizeGrid()
3703 QFETCH(QQuickGridView::Flow, flow);
3704 QFETCH(Qt::LayoutDirection, layoutDirection);
3705 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3706 QFETCH(QPointF, initialContentPos);
3707 QFETCH(QPointF, firstItemPos);
3710 for (int i = 0; i < 30; i++)
3711 model.addItem("Item" + QString::number(i), "");
3713 QQuickView *canvas = getView();
3714 QQmlContext *ctxt = canvas->rootContext();
3715 ctxt->setContextProperty("testModel", &model);
3716 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
3717 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
3718 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickGridView::BottomToTop);
3719 canvas->setSource(testFileUrl("resizegrid.qml"));
3721 qApp->processEvents();
3723 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3724 QTRY_VERIFY(gridview != 0);
3725 QQuickItem *contentItem = gridview->contentItem();
3726 QTRY_VERIFY(contentItem != 0);
3728 // set the width to slightly larger than 3 items across, to test
3729 // items are aligned correctly in right-to-left
3730 canvas->rootObject()->setWidth(260);
3731 canvas->rootObject()->setHeight(320);
3732 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3734 QCOMPARE(gridview->contentX(), initialContentPos.x());
3735 QCOMPARE(gridview->contentY(), initialContentPos.y());
3737 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
3739 QCOMPARE(item0->pos(), firstItemPos);
3741 // Confirm items positioned correctly and indexes correct
3742 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
3743 QVERIFY(items.count() >= 18 && items.count() <= 21);
3744 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3745 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3746 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3747 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3748 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3750 QCOMPARE(name->text(), model.name(i));
3753 // change from 3x5 grid to 4x7
3754 canvas->rootObject()->setWidth(canvas->rootObject()->width() + 80);
3755 canvas->rootObject()->setHeight(canvas->rootObject()->height() + 60*2);
3756 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3758 // other than in LeftToRight+RightToLeft layout, the first item should not move
3759 // if view is resized
3760 QCOMPARE(findItem<QQuickItem>(contentItem, "wrapper", 0), item0);
3761 if (flow == QQuickGridView::FlowLeftToRight && layoutDirection == Qt::RightToLeft)
3762 firstItemPos.rx() += 80;
3763 QCOMPARE(item0->pos(), firstItemPos);
3765 QPointF newContentPos = initialContentPos;
3766 if (flow == QQuickGridView::FlowTopToBottom && layoutDirection == Qt::RightToLeft)
3767 newContentPos.rx() -= 80.0;
3768 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3769 newContentPos.ry() -= 60.0 * 2;
3770 QCOMPARE(gridview->contentX(), newContentPos.x());
3771 QCOMPARE(gridview->contentY(), newContentPos.y());
3773 // Confirm items positioned correctly and indexes correct
3774 items = findItems<QQuickItem>(contentItem, "wrapper");
3775 QVERIFY(items.count() >= 28);
3776 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3777 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3778 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3779 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3780 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3782 QCOMPARE(name->text(), model.name(i));
3785 releaseView(canvas);
3788 void tst_QQuickGridView::resizeGrid_data()
3790 QTest::addColumn<QQuickGridView::Flow>("flow");
3791 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3792 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3793 QTest::addColumn<QPointF>("initialContentPos");
3794 QTest::addColumn<QPointF>("firstItemPos");
3796 // Initial view width is 260, so in LeftToRight + right-to-left mode the
3797 // content x should be -20
3799 QTest::newRow("LeftToRight, LtR, TtB")
3800 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3804 QTest::newRow("LeftToRight, RtL, TtB")
3805 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3806 << QPointF(-20.0, 0)
3807 << QPointF(80.0 * 2, 0);
3809 QTest::newRow("LeftToRight, LtR, BtT")
3810 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3812 << QPointF(0, -60.0);
3814 QTest::newRow("LeftToRight, RtL, BtT")
3815 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3816 << QPointF(-20.0, -320)
3817 << QPointF(80.0 * 2, -60.0);
3820 QTest::newRow("TopToBottom, LtR, TtB")
3821 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3825 QTest::newRow("TopToBottom, RtL, TtB")
3826 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3828 << QPointF(-80.0, 0);
3830 QTest::newRow("TopToBottom, LtR, BtT")
3831 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3833 << QPointF(0, -60.0);
3835 QTest::newRow("TopToBottom, RtL, BtT")
3836 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3837 << QPointF(-260, -320)
3838 << QPointF(-80.0, -60.0);
3842 void tst_QQuickGridView::changeColumnCount()
3845 for (int i = 0; i < 40; i++)
3846 model.addItem("Item" + QString::number(i), "");
3848 QQuickView *canvas = createView();
3849 QQmlContext *ctxt = canvas->rootContext();
3850 ctxt->setContextProperty("testModel", &model);
3851 ctxt->setContextProperty("initialWidth", 100);
3852 ctxt->setContextProperty("initialHeight", 320);
3853 canvas->setSource(testFileUrl("resizeview.qml"));
3855 qApp->processEvents();
3857 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3858 QTRY_VERIFY(gridview != 0);
3859 QQuickItem *contentItem = gridview->contentItem();
3860 QTRY_VERIFY(contentItem != 0);
3861 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3863 // a single column of 6 items are visible
3864 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3865 QCOMPARE(itemCount, 6);
3866 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3867 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3868 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3869 QCOMPARE(item->x(), 0.0);
3870 QCOMPARE(item->y(), qreal(i*60));
3873 // now 6x3 grid is visible, plus 1 extra below for refill
3874 gridview->setWidth(240);
3875 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3876 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3877 QCOMPARE(itemCount, 6*3 + 1);
3878 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3879 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3880 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3881 QCOMPARE(item->x(), qreal((i%3)*80));
3882 QCOMPARE(item->y(), qreal((i/3)*60));
3885 // back to single column
3886 gridview->setWidth(100);
3887 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3888 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3889 QCOMPARE(itemCount, 6);
3890 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3891 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3892 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3893 QCOMPARE(item->x(), 0.0);
3894 QCOMPARE(item->y(), qreal(i*60));
3900 void tst_QQuickGridView::indexAt_itemAt_data()
3902 QTest::addColumn<qreal>("x");
3903 QTest::addColumn<qreal>("y");
3904 QTest::addColumn<int>("index");
3906 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3907 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3908 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3909 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3910 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3913 void tst_QQuickGridView::indexAt_itemAt()
3919 QQuickView *canvas = getView();
3922 model.addItem("Fred", "12345");
3923 model.addItem("John", "2345");
3924 model.addItem("Bob", "54321");
3925 model.addItem("Billy", "22345");
3926 model.addItem("Sam", "2945");
3927 model.addItem("Ben", "04321");
3928 model.addItem("Jim", "0780");
3930 canvas->rootContext()->setContextProperty("testModel", &model);
3931 canvas->setSource(testFileUrl("gridview1.qml"));
3932 qApp->processEvents();
3934 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3935 QTRY_VERIFY(gridview != 0);
3937 QQuickItem *contentItem = gridview->contentItem();
3938 QTRY_VERIFY(contentItem != 0);
3940 QTRY_COMPARE(gridview->count(), model.count());
3942 QQuickItem *item = 0;
3944 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3947 QCOMPARE(gridview->indexAt(x, y), index);
3948 QVERIFY(gridview->itemAt(x, y) == item);
3950 releaseView(canvas);
3953 void tst_QQuickGridView::onAdd()
3955 QFETCH(int, initialItemCount);
3956 QFETCH(int, itemsToAdd);
3958 const int delegateWidth = 50;
3959 const int delegateHeight = 100;
3961 QQuickView *canvas = getView();
3962 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3964 // these initial items should not trigger GridView.onAdd
3965 for (int i=0; i<initialItemCount; i++)
3966 model.addItem("dummy value", "dummy value");
3968 QQmlContext *ctxt = canvas->rootContext();
3969 ctxt->setContextProperty("testModel", &model);
3970 ctxt->setContextProperty("delegateWidth", delegateWidth);
3971 ctxt->setContextProperty("delegateHeight", delegateHeight);
3972 canvas->setSource(testFileUrl("attachedSignals.qml"));
3974 QObject *object = canvas->rootObject();
3975 object->setProperty("width", canvas->width());
3976 object->setProperty("height", canvas->height());
3977 qApp->processEvents();
3979 QList<QPair<QString, QString> > items;
3980 for (int i=0; i<itemsToAdd; i++)
3981 items << qMakePair(QString("value %1").arg(i), QString::number(i));
3982 model.addItems(items);
3984 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3985 qApp->processEvents();
3987 QVariantList result = object->property("addedDelegates").toList();
3988 QTRY_COMPARE(result.count(), items.count());
3989 for (int i=0; i<items.count(); i++)
3990 QCOMPARE(result[i].toString(), items[i].first);
3992 releaseView(canvas);
3995 void tst_QQuickGridView::onAdd_data()
3997 QTest::addColumn<int>("initialItemCount");
3998 QTest::addColumn<int>("itemsToAdd");
4000 QTest::newRow("0, add 1") << 0 << 1;
4001 QTest::newRow("0, add 2") << 0 << 2;
4002 QTest::newRow("0, add 10") << 0 << 10;
4004 QTest::newRow("1, add 1") << 1 << 1;
4005 QTest::newRow("1, add 2") << 1 << 2;
4006 QTest::newRow("1, add 10") << 1 << 10;
4008 QTest::newRow("5, add 1") << 5 << 1;
4009 QTest::newRow("5, add 2") << 5 << 2;
4010 QTest::newRow("5, add 10") << 5 << 10;
4013 void tst_QQuickGridView::onRemove()
4015 QFETCH(int, initialItemCount);
4016 QFETCH(int, indexToRemove);
4017 QFETCH(int, removeCount);
4019 const int delegateWidth = 50;
4020 const int delegateHeight = 100;
4022 for (int i=0; i<initialItemCount; i++)
4023 model.addItem(QString("value %1").arg(i), "dummy value");
4025 QQuickView *canvas = getView();
4026 QQmlContext *ctxt = canvas->rootContext();
4027 ctxt->setContextProperty("testModel", &model);
4028 ctxt->setContextProperty("delegateWidth", delegateWidth);
4029 ctxt->setContextProperty("delegateHeight", delegateHeight);
4030 canvas->setSource(testFileUrl("attachedSignals.qml"));
4031 QObject *object = canvas->rootObject();
4033 model.removeItems(indexToRemove, removeCount);
4034 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4035 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4037 releaseView(canvas);
4040 void tst_QQuickGridView::onRemove_data()
4042 QTest::addColumn<int>("initialItemCount");
4043 QTest::addColumn<int>("indexToRemove");
4044 QTest::addColumn<int>("removeCount");
4046 QTest::newRow("remove first") << 1 << 0 << 1;
4047 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4048 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4049 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4051 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4052 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4053 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4054 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4055 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4056 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4058 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4059 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4060 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4063 void tst_QQuickGridView::columnCount()
4066 canvas.setSource(testFileUrl("gridview4.qml"));
4068 canvas.requestActivateWindow();
4069 QTest::qWaitForWindowShown(&canvas);
4071 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
4073 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
4074 QCOMPARE(view->cellHeight(), qreal(100));
4076 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
4077 QCOMPARE(items.size(), 18);
4078 QCOMPARE(items.at(8)->y(), qreal(0));
4079 QCOMPARE(items.at(9)->y(), qreal(100));
4082 void tst_QQuickGridView::margins()
4085 QQuickView *canvas = createView();
4088 for (int i = 0; i < 40; i++)
4089 model.addItem("Item" + QString::number(i), "");
4091 QQmlContext *ctxt = canvas->rootContext();
4092 ctxt->setContextProperty("testModel", &model);
4093 ctxt->setContextProperty("testRightToLeft", QVariant(false));
4095 canvas->setSource(testFileUrl("margins.qml"));
4097 qApp->processEvents();
4099 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4100 QTRY_VERIFY(gridview != 0);
4101 QQuickItem *contentItem = gridview->contentItem();
4102 QTRY_VERIFY(contentItem != 0);
4103 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4105 QCOMPARE(gridview->contentX(), -30.);
4106 QCOMPARE(gridview->xOrigin(), 0.);
4109 gridview->positionViewAtEnd();
4110 qreal pos = gridview->contentX();
4111 gridview->setContentX(pos + 80);
4112 gridview->returnToBounds();
4113 QTRY_COMPARE(gridview->contentX(), pos + 50);
4115 // remove item before visible and check that left margin is maintained
4116 // and xOrigin is updated
4117 gridview->setContentX(200);
4118 model.removeItems(0, 4);
4120 gridview->setContentX(-50);
4121 gridview->returnToBounds();
4122 QCOMPARE(gridview->xOrigin(), 100.);
4123 QTRY_COMPARE(gridview->contentX(), 70.);
4125 // reduce left margin
4126 gridview->setLeftMargin(20);
4127 QCOMPARE(gridview->xOrigin(), 100.);
4128 QTRY_COMPARE(gridview->contentX(), 80.);
4131 gridview->positionViewAtEnd();
4132 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
4133 pos = gridview->contentX();
4134 gridview->setContentX(pos + 80);
4135 gridview->returnToBounds();
4136 QTRY_COMPARE(gridview->contentX(), pos + 50);
4138 // reduce right margin
4139 pos = gridview->contentX();
4140 gridview->setRightMargin(40);
4141 QCOMPARE(gridview->xOrigin(), 0.);
4142 QTRY_COMPARE(gridview->contentX(), pos-10);
4148 QQuickView *canvas = createView();
4152 for (int i = 0; i < 40; i++)
4153 model.addItem("Item" + QString::number(i), "");
4155 QQmlContext *ctxt = canvas->rootContext();
4156 ctxt->setContextProperty("testModel", &model);
4157 ctxt->setContextProperty("testRightToLeft", QVariant(true));
4159 canvas->setSource(testFileUrl("margins.qml"));
4160 qApp->processEvents();
4162 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4163 QTRY_VERIFY(gridview != 0);
4165 QQuickItem *contentItem = gridview->contentItem();
4166 QTRY_VERIFY(contentItem != 0);
4168 QTRY_COMPARE(gridview->contentX(), -240+50.);
4169 QTRY_COMPARE(gridview->xOrigin(), 0.);
4172 gridview->positionViewAtEnd();
4173 qreal pos = gridview->contentX();
4174 gridview->setContentX(pos - 80);
4175 gridview->returnToBounds();
4176 QTRY_COMPARE(gridview->contentX(), pos - 30);
4178 // remove item before visible and check that left margin is maintained
4179 // and xOrigin is updated
4180 gridview->setContentX(-400);
4181 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4182 model.removeItems(0, 4);
4183 QTRY_COMPARE(model.count(), gridview->count());
4184 gridview->setContentX(-240+50);
4185 gridview->returnToBounds();
4186 QCOMPARE(gridview->xOrigin(), -100.);
4187 QTRY_COMPARE(gridview->contentX(), -240-50.);
4189 // reduce right margin
4190 pos = gridview->contentX();
4191 gridview->setRightMargin(40);
4192 QCOMPARE(gridview->xOrigin(), -100.);
4193 QTRY_COMPARE(gridview->contentX(), -240-100 + 40.);
4196 gridview->positionViewAtEnd();
4197 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
4198 pos = gridview->contentX();
4199 gridview->setContentX(pos - 80);
4200 gridview->returnToBounds();
4201 QTRY_COMPARE(gridview->contentX(), pos - 30);
4203 // reduce left margin
4204 pos = gridview->contentX();
4205 gridview->setLeftMargin(20);
4206 QCOMPARE(gridview->xOrigin(), 0.);
4207 QTRY_COMPARE(gridview->contentX(), pos+10);
4213 void tst_QQuickGridView::creationContext()
4216 canvas.setGeometry(0,0,240,320);
4217 canvas.setSource(testFileUrl("creationContext.qml"));
4218 qApp->processEvents();
4220 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4222 QVERIFY(rootItem->property("count").toInt() > 0);
4225 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
4226 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4227 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4228 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4229 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4230 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4233 void tst_QQuickGridView::snapToRow_data()
4235 QTest::addColumn<QQuickGridView::Flow>("flow");
4236 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4237 QTest::addColumn<int>("highlightRangeMode");
4238 QTest::addColumn<QPoint>("flickStart");
4239 QTest::addColumn<QPoint>("flickEnd");
4240 QTest::addColumn<qreal>("snapAlignment");
4241 QTest::addColumn<qreal>("endExtent");
4242 QTest::addColumn<qreal>("startExtent");
4244 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4245 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4247 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4248 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4250 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4251 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
4253 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4254 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4256 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4257 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4259 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4260 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
4263 void tst_QQuickGridView::snapToRow()
4265 QFETCH(QQuickGridView::Flow, flow);
4266 QFETCH(Qt::LayoutDirection, layoutDirection);
4267 QFETCH(int, highlightRangeMode);
4268 QFETCH(QPoint, flickStart);
4269 QFETCH(QPoint, flickEnd);
4270 QFETCH(qreal, snapAlignment);
4271 QFETCH(qreal, endExtent);
4272 QFETCH(qreal, startExtent);
4274 QQuickView *canvas = getView();
4276 canvas->setSource(testFileUrl("snapToRow.qml"));
4278 qApp->processEvents();
4280 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4281 QTRY_VERIFY(gridview != 0);
4283 gridview->setFlow(flow);
4284 gridview->setLayoutDirection(layoutDirection);
4285 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4286 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4288 QQuickItem *contentItem = gridview->contentItem();
4289 QTRY_VERIFY(contentItem != 0);
4291 // confirm that a flick hits an item boundary
4292 flick(canvas, flickStart, flickEnd, 180);
4293 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4294 if (flow == QQuickGridView::FlowLeftToRight)
4295 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
4297 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
4301 flick(canvas, flickStart, flickEnd, 180);
4302 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4303 } while (flow == QQuickGridView::FlowLeftToRight
4304 ? !gridview->isAtYEnd()
4305 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4307 if (flow == QQuickGridView::FlowLeftToRight)
4308 QCOMPARE(gridview->contentY(), endExtent);
4310 QCOMPARE(gridview->contentX(), endExtent);
4314 flick(canvas, flickEnd, flickStart, 180);
4315 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4316 } while (flow == QQuickGridView::FlowLeftToRight
4317 ? !gridview->isAtYBeginning()
4318 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4320 if (flow == QQuickGridView::FlowLeftToRight)
4321 QCOMPARE(gridview->contentY(), startExtent);
4323 QCOMPARE(gridview->contentX(), startExtent);
4325 releaseView(canvas);
4328 void tst_QQuickGridView::snapOneRow_data()
4330 QTest::addColumn<QQuickGridView::Flow>("flow");
4331 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4332 QTest::addColumn<int>("highlightRangeMode");
4333 QTest::addColumn<QPoint>("flickStart");
4334 QTest::addColumn<QPoint>("flickEnd");
4335 QTest::addColumn<qreal>("snapAlignment");
4336 QTest::addColumn<qreal>("endExtent");
4337 QTest::addColumn<qreal>("startExtent");
4339 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4340 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4342 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4343 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4345 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4346 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
4348 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4349 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4351 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4352 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4354 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4355 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
4358 void tst_QQuickGridView::snapOneRow()
4360 QFETCH(QQuickGridView::Flow, flow);
4361 QFETCH(Qt::LayoutDirection, layoutDirection);
4362 QFETCH(int, highlightRangeMode);
4363 QFETCH(QPoint, flickStart);
4364 QFETCH(QPoint, flickEnd);
4365 QFETCH(qreal, snapAlignment);
4366 QFETCH(qreal, endExtent);
4367 QFETCH(qreal, startExtent);
4369 QQuickView *canvas = getView();
4371 canvas->setSource(testFileUrl("snapOneRow.qml"));
4373 qApp->processEvents();
4375 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4376 QTRY_VERIFY(gridview != 0);
4378 gridview->setFlow(flow);
4379 gridview->setLayoutDirection(layoutDirection);
4380 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4381 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4383 QQuickItem *contentItem = gridview->contentItem();
4384 QTRY_VERIFY(contentItem != 0);
4386 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
4388 // confirm that a flick hits next row boundary
4389 flick(canvas, flickStart, flickEnd, 180);
4390 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4391 if (flow == QQuickGridView::FlowLeftToRight)
4392 QCOMPARE(gridview->contentY(), snapAlignment);
4394 QCOMPARE(gridview->contentX(), snapAlignment);
4396 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4397 QCOMPARE(gridview->currentIndex(), 2);
4398 QCOMPARE(currentIndexSpy.count(), 1);
4403 flick(canvas, flickStart, flickEnd, 180);
4404 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4405 } while (flow == QQuickGridView::FlowLeftToRight
4406 ? !gridview->isAtYEnd()
4407 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4409 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4410 QCOMPARE(gridview->currentIndex(), 6);
4411 QCOMPARE(currentIndexSpy.count(), 3);
4414 if (flow == QQuickGridView::FlowLeftToRight)
4415 QCOMPARE(gridview->contentY(), endExtent);
4417 QCOMPARE(gridview->contentX(), endExtent);
4421 flick(canvas, flickEnd, flickStart, 180);
4422 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4423 } while (flow == QQuickGridView::FlowLeftToRight
4424 ? !gridview->isAtYBeginning()
4425 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4427 if (flow == QQuickGridView::FlowLeftToRight)
4428 QCOMPARE(gridview->contentY(), startExtent);
4430 QCOMPARE(gridview->contentX(), startExtent);
4432 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4433 QCOMPARE(gridview->currentIndex(), 0);
4434 QCOMPARE(currentIndexSpy.count(), 6);
4437 releaseView(canvas);
4441 void tst_QQuickGridView::unaligned()
4443 QQuickView *canvas = createView();
4447 for (int i = 0; i < 10; i++)
4448 model.addItem("Item" + QString::number(i), "");
4450 QQmlContext *ctxt = canvas->rootContext();
4451 ctxt->setContextProperty("testModel", &model);
4453 canvas->setSource(testFileUrl("unaligned.qml"));
4454 qApp->processEvents();
4456 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
4457 QVERIFY(gridview != 0);
4459 QQuickItem *contentItem = gridview->contentItem();
4460 QVERIFY(contentItem != 0);
4462 for (int i = 0; i < 10; ++i) {
4463 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4464 if (!item) qWarning() << "Item" << i << "not found";
4466 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4467 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4471 for (int i = 10; i < 18; ++i) {
4472 model.addItem("Item" + QString::number(i), "");
4473 QQuickItem *item = 0;
4474 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4475 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4476 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4480 for (int i = 0; i < 10; ++i) {
4481 model.insertItem(i, "Item" + QString::number(i), "");
4482 QQuickItem *item = 0;
4483 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4484 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4485 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4489 model.removeItems(7, 10);
4490 QTRY_COMPARE(model.count(), gridview->count());
4491 for (int i = 0; i < 18; ++i) {
4492 QQuickItem *item = 0;
4493 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4494 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
4495 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
4501 void tst_QQuickGridView::populateTransitions()
4503 QFETCH(bool, staticallyPopulate);
4504 QFETCH(bool, dynamicallyPopulate);
4505 QFETCH(bool, usePopulateTransition);
4507 QPointF transitionFrom(-50, -50);
4508 QPointF transitionVia(100, 100);
4509 QaimModel model_transitionFrom;
4510 QaimModel model_transitionVia;
4513 if (staticallyPopulate) {
4514 for (int i = 0; i < 30; i++)
4515 model.addItem("item" + QString::number(i), "");
4518 QQuickView *canvas = getView();
4519 canvas->rootContext()->setContextProperty("testModel", &model);
4520 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4521 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4522 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4523 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4524 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4525 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4526 canvas->setSource(testFileUrl("populateTransitions.qml"));
4529 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4531 QQuickItem *contentItem = gridview->contentItem();
4532 QVERIFY(contentItem);
4534 // check the populate transition is run
4535 if (staticallyPopulate && usePopulateTransition) {
4536 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 18);
4537 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4538 } else if (dynamicallyPopulate) {
4539 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4540 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 18);
4542 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4543 QCOMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4544 QCOMPARE(gridview->property("countAddTransitions").toInt(), 0);
4547 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4548 for (int i=0; i < model.count() && i < itemCount; ++i) {
4549 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4550 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4551 QTRY_COMPARE(item->x(), (i%3)*80.0);
4552 QTRY_COMPARE(item->y(), (i/3)*60.0);
4553 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4555 QTRY_COMPARE(name->text(), model.name(i));
4558 gridview->setProperty("countPopulateTransitions", 0);
4559 gridview->setProperty("countAddTransitions", 0);
4561 // add an item and check this is done with add transition, not populate
4562 model.insertItem(0, "another item", "");
4563 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
4564 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4567 canvas->rootContext()->setContextProperty("testModel", QVariant());
4568 QTRY_COMPARE(gridview->count(), 0);
4569 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4570 gridview->setProperty("countPopulateTransitions", 0);
4571 gridview->setProperty("countAddTransitions", 0);
4573 // set to a valid model and check populate transition is run a second time
4575 for (int i = 0; i < 30; i++)
4576 model.addItem("item" + QString::number(i), "");
4577 canvas->rootContext()->setContextProperty("testModel", &model);
4578 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4580 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4581 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4583 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4584 for (int i=0; i < model.count() && i < itemCount; ++i) {
4585 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4586 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4587 QTRY_COMPARE(item->x(), (i%3)*80.0);
4588 QTRY_COMPARE(item->y(), (i/3)*60.0);
4589 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4591 QTRY_COMPARE(name->text(), model.name(i));
4594 // reset model and check populate transition is run again
4595 gridview->setProperty("countPopulateTransitions", 0);
4596 gridview->setProperty("countAddTransitions", 0);
4598 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4599 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4601 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4602 for (int i=0; i < model.count() && i < itemCount; ++i) {
4603 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4604 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4605 QTRY_COMPARE(item->x(), (i%3)*80.0);
4606 QTRY_COMPARE(item->y(), (i/3)*60.0);
4607 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4609 QTRY_COMPARE(name->text(), model.name(i));
4612 releaseView(canvas);
4615 void tst_QQuickGridView::populateTransitions_data()
4617 QTest::addColumn<bool>("staticallyPopulate");
4618 QTest::addColumn<bool>("dynamicallyPopulate");
4619 QTest::addColumn<bool>("usePopulateTransition");
4621 QTest::newRow("static") << true << false << true;
4622 QTest::newRow("static, no populate") << true << false << false;
4624 QTest::newRow("dynamic") << false << true << true;
4625 QTest::newRow("dynamic, no populate") << false << true << false;
4627 QTest::newRow("empty to start with") << false << false << true;
4628 QTest::newRow("empty to start with, no populate") << false << false << false;
4631 void tst_QQuickGridView::addTransitions()
4633 QFETCH(int, initialItemCount);
4634 QFETCH(bool, shouldAnimateTargets);
4635 QFETCH(qreal, contentYRowOffset);
4636 QFETCH(int, insertionIndex);
4637 QFETCH(int, insertionCount);
4638 QFETCH(ListRange, expectedDisplacedIndexes);
4640 // added items should start here
4641 QPointF targetItems_transitionFrom(-50, -50);
4643 // displaced items should pass through this point
4644 QPointF displacedItems_transitionVia(100, 100);
4647 for (int i = 0; i < initialItemCount; i++)
4648 model.addItem("Original item" + QString::number(i), "");
4649 QaimModel model_targetItems_transitionFrom;
4650 QaimModel model_displacedItems_transitionVia;
4652 QQuickView *canvas = getView();
4653 QQmlContext *ctxt = canvas->rootContext();
4654 ctxt->setContextProperty("testModel", &model);
4655 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4656 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4657 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4658 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4659 canvas->setSource(testFileUrl("addTransitions.qml"));
4662 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4663 QTRY_VERIFY(gridview != 0);
4664 QQuickItem *contentItem = gridview->contentItem();
4665 QVERIFY(contentItem != 0);
4666 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4668 if (contentYRowOffset != 0) {
4669 gridview->setContentY(contentYRowOffset * 60.0);
4670 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4673 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4675 // only target items that will become visible should be animated
4676 QList<QPair<QString, QString> > newData;
4677 QList<QPair<QString, QString> > expectedTargetData;
4678 QList<int> targetIndexes;
4679 if (shouldAnimateTargets) {
4680 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4681 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4683 // last visible item is the first item of the row beneath the view
4684 if (i >= (gridview->contentY() / 60)*3 && i < qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) {
4685 expectedTargetData << newData.last();
4689 QVERIFY(expectedTargetData.count() > 0);
4693 if (!newData.isEmpty()) {
4694 model.insertItems(insertionIndex, newData);
4695 QTRY_COMPARE(model.count(), gridview->count());
4698 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4700 if (shouldAnimateTargets) {
4701 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4702 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4703 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4705 // check the target and displaced items were animated
4706 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4707 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4709 // check attached properties
4710 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4711 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4712 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4713 if (expectedDisplacedIndexes.isValid()) {
4714 // adjust expectedDisplacedIndexes to their final values after the move
4715 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4716 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4717 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4718 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4721 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4722 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4725 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4726 int firstVisibleIndex = -1;
4727 for (int i=0; i<items.count(); i++) {
4728 if (items[i]->y() >= gridview->contentY()) {
4729 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4730 firstVisibleIndex = e.evaluate().toInt();
4734 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4736 // verify all items moved to the correct final positions
4737 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4738 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4739 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4740 QCOMPARE(item->x(), (i%3)*80.0);
4741 QCOMPARE(item->y(), (i/3)*60.0);
4742 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4744 QCOMPARE(name->text(), model.name(i));
4747 releaseView(canvas);
4750 void tst_QQuickGridView::addTransitions_data()
4752 QTest::addColumn<int>("initialItemCount");
4753 QTest::addColumn<qreal>("contentYRowOffset");
4754 QTest::addColumn<bool>("shouldAnimateTargets");
4755 QTest::addColumn<int>("insertionIndex");
4756 QTest::addColumn<int>("insertionCount");
4757 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4759 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4760 QTest::newRow("insert 1, just before start")
4761 << 30 << 1.0 << false
4762 << 0 << 1 << ListRange();
4763 QTest::newRow("insert 1, way before start")
4764 << 30 << 1.0 << false
4765 << 0 << 1 << ListRange();
4766 QTest::newRow("insert multiple, just before start")
4767 << 30 << 1.0 << false
4768 << 0 << 3 << ListRange();
4769 QTest::newRow("insert multiple (< 1 row), just before start")
4770 << 30 << 1.0 << false
4771 << 0 << 2 << ListRange();
4772 QTest::newRow("insert multiple, way before start")
4773 << 30 << 3.0 << false
4774 << 0 << 3 << ListRange();
4776 QTest::newRow("insert 1 at start")
4777 << 30 << 0.0 << true
4778 << 0 << 1 << ListRange(0, 17);
4779 QTest::newRow("insert multiple at start")
4780 << 30 << 0.0 << true
4781 << 0 << 3 << ListRange(0, 17);
4782 QTest::newRow("insert multiple (> 1 row) at start")
4783 << 30 << 0.0 << true
4784 << 0 << 5 << ListRange(0, 17);
4785 QTest::newRow("insert 1 at start, content y not 0")
4786 << 30 << 1.0 << true // first visible is index 3
4787 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4788 QTest::newRow("insert multiple at start, content y not 0")
4789 << 30 << 1.0 << true // first visible is index 3
4790 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4791 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4792 << 30 << 1.0 << true // first visible is index 3
4793 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4795 QTest::newRow("insert 1 at start, to empty grid")
4797 << 0 << 1 << ListRange();
4798 QTest::newRow("insert multiple at start, to empty grid")
4800 << 0 << 3 << ListRange();
4802 QTest::newRow("insert 1 at middle")
4803 << 30 << 0.0 << true
4804 << 7 << 1 << ListRange(7, 17);
4805 QTest::newRow("insert multiple at middle")
4806 << 30 << 0.0 << true
4807 << 7 << 3 << ListRange(7, 17);
4808 QTest::newRow("insert multiple (> 1 row) at middle")
4809 << 30 << 0.0 << true
4810 << 7 << 5 << ListRange(7, 17);
4812 QTest::newRow("insert 1 at bottom")
4813 << 30 << 0.0 << true
4814 << 17 << 1 << ListRange(17, 17);
4815 QTest::newRow("insert multiple at bottom")
4816 << 30 << 0.0 << true
4817 << 17 << 3 << ListRange(17, 17);
4818 QTest::newRow("insert 1 at bottom, content y not 0")
4819 << 30 << 1.0 << true
4820 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4821 QTest::newRow("insert multiple at bottom, content y not 0")
4822 << 30 << 1.0 << true
4823 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4826 // items added after the last visible will not be animated in, since they
4827 // do not appear in the final view
4828 QTest::newRow("insert 1 after end")
4829 << 30 << 0.0 << false
4830 << 18 << 1 << ListRange();
4831 QTest::newRow("insert multiple after end")
4832 << 30 << 0.0 << false
4833 << 18 << 3 << ListRange();
4836 void tst_QQuickGridView::moveTransitions()
4838 QFETCH(int, initialItemCount);
4839 QFETCH(qreal, contentYRowOffset);
4840 QFETCH(qreal, rowOffsetAfterMove);
4841 QFETCH(int, moveFrom);
4842 QFETCH(int, moveTo);
4843 QFETCH(int, moveCount);
4844 QFETCH(ListRange, expectedDisplacedIndexes);
4846 // target and displaced items should pass through these points
4847 QPointF targetItems_transitionVia(-50, 50);
4848 QPointF displacedItems_transitionVia(100, 100);
4851 for (int i = 0; i < initialItemCount; i++)
4852 model.addItem("Original item" + QString::number(i), "");
4853 QaimModel model_targetItems_transitionVia;
4854 QaimModel model_displacedItems_transitionVia;
4856 QQuickView *canvas = getView();
4857 QQmlContext *ctxt = canvas->rootContext();
4858 ctxt->setContextProperty("testModel", &model);
4859 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4860 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4861 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4862 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4863 canvas->setSource(testFileUrl("moveTransitions.qml"));
4866 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4867 QTRY_VERIFY(gridview != 0);
4868 QQuickItem *contentItem = gridview->contentItem();
4869 QVERIFY(contentItem != 0);
4872 if (contentYRowOffset != 0) {
4873 gridview->setContentY(contentYRowOffset * 60.0);
4874 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4877 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4879 // Items moving to *or* from visible positions should be animated.
4880 // Otherwise, they should not be animated.
4881 QList<QPair<QString, QString> > expectedTargetData;
4882 QList<int> targetIndexes;
4883 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4884 int toIndex = moveTo + (i - moveFrom);
4885 int firstVisibleIndex = (gridview->contentY() / 60) * 3;
4886 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4887 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4888 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4889 expectedTargetData << qMakePair(model.name(i), model.number(i));
4893 // ViewTransition.index provides the indices that items are moving to, not from
4894 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4897 model.moveItems(moveFrom, moveTo, moveCount);
4899 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4900 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4901 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4903 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4905 // check the target and displaced items were animated
4906 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4907 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4909 // check attached properties
4910 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4911 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4912 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4913 if (expectedDisplacedIndexes.isValid()) {
4914 // adjust expectedDisplacedIndexes to their final values after the move
4915 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4916 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4917 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4918 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4921 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4922 int firstVisibleIndex = -1;
4923 for (int i=0; i<items.count(); i++) {
4924 if (items[i]->y() >= gridview->contentY()) {
4925 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4926 firstVisibleIndex = e.evaluate().toInt();
4930 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4932 // verify all items moved to the correct final positions
4933 qreal pixelOffset = 60 * rowOffsetAfterMove;
4934 for (int i=firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4935 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4936 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4937 QCOMPARE(item->x(), (i%3)*80.0);
4938 QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
4939 name = findItem<QQuickText>(contentItem, "textName", i);
4941 QTRY_COMPARE(name->text(), model.name(i));
4944 releaseView(canvas);
4947 void tst_QQuickGridView::moveTransitions_data()
4949 QTest::addColumn<int>("initialItemCount");
4950 QTest::addColumn<qreal>("contentYRowOffset");
4951 QTest::addColumn<qreal>("rowOffsetAfterMove");
4952 QTest::addColumn<int>("moveFrom");
4953 QTest::addColumn<int>("moveTo");
4954 QTest::addColumn<int>("moveCount");
4955 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4957 QTest::newRow("move from above view, outside visible items, move 1")
4959 << 1 << 10 << 1 << ListRange(6, 10);
4960 QTest::newRow("move from above view, outside visible items, move 1 (first item)")
4962 << 0 << 10 << 1 << ListRange(6, 10);
4963 QTest::newRow("move from above view, outside visible items, move multiple")
4965 << 1 << 10 << 3 << ListRange(13, 23);
4966 QTest::newRow("move from above view, mix of visible/non-visible")
4968 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4969 QTest::newRow("move from above view, mix of visible/non-visible (move first)")
4971 << 0 << 10 << 6 << ListRange(16, 23);
4973 QTest::newRow("move within view, move 1 down")
4975 << 1 << 10 << 1 << ListRange(2, 10);
4976 QTest::newRow("move within view, move 1 down, move first item")
4978 << 0 << 10 << 1 << ListRange(1, 10);
4979 QTest::newRow("move within view, move 1 down, move first item, contentY not 0")
4981 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
4982 QTest::newRow("move within view, move 1 down, to last item")
4984 << 10 << 17 << 1 << ListRange(11, 17);
4985 QTest::newRow("move within view, move first->last")
4987 << 0 << 17 << 1 << ListRange(1, 17);
4989 QTest::newRow("move within view, move multiple down")
4991 << 1 << 10 << 3 << ListRange(4, 12);
4992 QTest::newRow("move within view, move multiple down, move first item")
4994 << 0 << 10 << 3 << ListRange(3, 12);
4995 QTest::newRow("move within view, move multiple down, move first item, contentY not 0")
4997 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
4998 QTest::newRow("move within view, move multiple down, displace last item")
5000 << 5 << 15 << 3 << ListRange(8, 17);
5001 QTest::newRow("move within view, move multiple down, move first->last")
5003 << 0 << 15 << 3 << ListRange(3, 17);
5005 QTest::newRow("move within view, move 1 up")
5007 << 10 << 1 << 1 << ListRange(1, 9);
5008 QTest::newRow("move within view, move 1 up, move to first index")
5010 << 10 << 0 << 1 << ListRange(0, 9);
5011 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0")
5013 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
5014 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border")
5016 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
5017 QTest::newRow("move within view, move 1 up, move last item")
5019 << 17 << 10 << 1 << ListRange(10, 16);
5020 QTest::newRow("move within view, move 1 up, move last->first")
5022 << 17 << 0 << 1 << ListRange(0, 16);
5024 QTest::newRow("move within view, move multiple up")
5026 << 10 << 1 << 3 << ListRange(1, 9);
5027 QTest::newRow("move within view, move multiple (> 1 row) up")
5029 << 10 << 1 << 5 << ListRange(1, 9);
5030 QTest::newRow("move within view, move multiple up, move to first index")
5032 << 10 << 0 << 3 << ListRange(0, 9);
5033 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0")
5035 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
5036 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border")
5038 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
5039 QTest::newRow("move within view, move multiple up, move last item")
5041 << 15 << 5 << 3 << ListRange(5, 14);
5042 QTest::newRow("move within view, move multiple up, move last->first")
5044 << 15 << 0 << 3 << ListRange(0, 14);
5046 QTest::newRow("move from below view, move 1 up")
5048 << 20 << 5 << 1 << ListRange(5, 17);
5049 QTest::newRow("move from below view, move 1 up, move to top")
5051 << 20 << 0 << 1 << ListRange(0, 17);
5052 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0")
5054 << 25 << 3 << 1 << ListRange(0+3, 17+3);
5055 QTest::newRow("move from below view, move multiple (> 1 row) up")
5057 << 20 << 5 << 5 << ListRange(5, 17);
5058 QTest::newRow("move from below view, move multiple up, move to top")
5060 << 20 << 0 << 3 << ListRange(0, 17);
5061 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0")
5063 << 25 << 3 << 3 << ListRange(0+3, 17+3);
5065 QTest::newRow("move from below view, move 1 up, move to bottom")
5067 << 20 << 17 << 1 << ListRange(17, 17);
5068 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0")
5070 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
5071 QTest::newRow("move from below view, move multiple up, move to to bottom")
5073 << 20 << 17 << 3 << ListRange(17, 17);
5074 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0")
5076 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
5079 void tst_QQuickGridView::removeTransitions()
5081 QFETCH(int, initialItemCount);
5082 QFETCH(bool, shouldAnimateTargets);
5083 QFETCH(qreal, contentYRowOffset);
5084 QFETCH(int, removalIndex);
5085 QFETCH(int, removalCount);
5086 QFETCH(ListRange, expectedDisplacedIndexes);
5088 // added items should end here
5089 QPointF targetItems_transitionTo(-50, -50);
5091 // displaced items should pass through this points
5092 QPointF displacedItems_transitionVia(100, 100);
5095 for (int i = 0; i < initialItemCount; i++)
5096 model.addItem("Original item" + QString::number(i), "");
5097 QaimModel model_targetItems_transitionTo;
5098 QaimModel model_displacedItems_transitionVia;
5100 QQuickView *canvas = getView();
5101 QQmlContext *ctxt = canvas->rootContext();
5102 ctxt->setContextProperty("testModel", &model);
5103 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5104 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5105 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5106 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5107 canvas->setSource(testFileUrl("removeTransitions.qml"));
5110 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5111 QTRY_VERIFY(gridview != 0);
5112 QQuickItem *contentItem = gridview->contentItem();
5113 QVERIFY(contentItem != 0);
5114 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5116 if (contentYRowOffset != 0) {
5117 gridview->setContentY(contentYRowOffset * 60.0);
5118 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5121 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5123 // only target items that are visible should be animated
5124 QList<QPair<QString, QString> > expectedTargetData;
5125 QList<int> targetIndexes;
5126 if (shouldAnimateTargets) {
5127 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5128 int firstVisibleIndex = (gridview->contentY() / 60.0)*3;
5129 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
5130 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
5131 expectedTargetData << qMakePair(model.name(i), model.number(i));
5135 QVERIFY(expectedTargetData.count() > 0);
5138 // calculate targetItems and expectedTargets before model changes
5139 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5140 QVariantMap expectedTargets;
5141 for (int i=0; i<targetIndexes.count(); i++)
5142 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5145 model.removeItems(removalIndex, removalCount);
5146 QTRY_COMPARE(model.count(), gridview->count());
5148 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
5149 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5150 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
5151 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5153 // check the target and displaced items were animated
5154 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5155 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5157 // check attached properties
5158 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
5159 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5160 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
5161 if (expectedDisplacedIndexes.isValid()) {
5162 // adjust expectedDisplacedIndexes to their final values after the move
5163 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5164 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5165 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5166 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
5169 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5170 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5173 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5174 int itemCount = items.count();
5175 int firstVisibleIndex = -1;
5176 for (int i=0; i<items.count(); i++) {
5177 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5178 int index = e.evaluate().toInt();
5179 if (firstVisibleIndex < 0 && items[i]->y() >= gridview->contentY())
5180 firstVisibleIndex = index;
5182 itemCount--; // exclude deleted items
5184 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5186 // verify all items moved to the correct final positions
5187 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5188 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5189 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5190 QCOMPARE(item->x(), (i%3)*80.0);
5191 QCOMPARE(item->y(), gridview->contentY() + ((i-firstVisibleIndex)/3) * 60.0);
5192 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5194 QTRY_COMPARE(name->text(), model.name(i));
5197 releaseView(canvas);
5200 void tst_QQuickGridView::removeTransitions_data()
5202 QTest::addColumn<int>("initialItemCount");
5203 QTest::addColumn<qreal>("contentYRowOffset");
5204 QTest::addColumn<bool>("shouldAnimateTargets");
5205 QTest::addColumn<int>("removalIndex");
5206 QTest::addColumn<int>("removalCount");
5207 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5209 // All items that are visible following the remove operation should be animated.
5210 // Remove targets that are outside of the view should not be animated.
5212 // For a GridView, removing any number of items other than a full row before the start
5213 // should displace all items in the view
5214 QTest::newRow("remove 1 before start")
5215 << 30 << 2.0 << false
5216 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
5217 QTest::newRow("remove 1 row, before start")
5218 << 30 << 2.0 << false
5219 << 3 << 3 << ListRange();
5220 QTest::newRow("remove between 1-2 rows, before start")
5221 << 30 << 2.0 << false
5222 << 0 << 5 << ListRange(6, 25);
5223 QTest::newRow("remove 2 rows, before start")
5224 << 30 << 2.0 << false
5225 << 0 << 6 << ListRange();
5226 QTest::newRow("remove mix of before and after start")
5227 << 30 << 1.0 << true
5228 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
5231 QTest::newRow("remove 1 from start")
5232 << 30 << 0.0 << true
5233 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
5234 QTest::newRow("remove multiple from start")
5235 << 30 << 0.0 << true
5236 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
5237 QTest::newRow("remove 1 from start, content y not 0")
5238 << 30 << 1.0 << true
5239 << 3 << 1 << ListRange(1 + 3, 18 + 3);
5240 QTest::newRow("remove multiple from start, content y not 0")
5241 << 30 << 1.0 << true
5242 << 3 << 3 << ListRange(3 + 3, 20 + 3);
5245 QTest::newRow("remove 1 from middle")
5246 << 30 << 0.0 << true
5247 << 5 << 1 << ListRange(6, 18);
5248 QTest::newRow("remove multiple from middle")
5249 << 30 << 0.0 << true
5250 << 5 << 3 << ListRange(8, 20);
5253 QTest::newRow("remove 1 from bottom")
5254 << 30 << 0.0 << true
5255 << 17 << 1 << ListRange(18, 18);
5256 QTest::newRow("remove multiple (1 row) from bottom")
5257 << 30 << 0.0 << true
5258 << 15 << 3 << ListRange(18, 20);
5259 QTest::newRow("remove multiple (> 1 row) from bottom")
5260 << 30 << 0.0 << true
5261 << 15 << 5 << ListRange(20, 22);
5262 QTest::newRow("remove 1 from bottom, content y not 0")
5263 << 30 << 1.0 << true
5264 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
5265 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
5266 << 30 << 1.0 << true
5267 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
5270 QTest::newRow("remove 1 after end")
5271 << 30 << 0.0 << false
5272 << 18 << 1 << ListRange();
5273 QTest::newRow("remove multiple after end")
5274 << 30 << 0.0 << false
5275 << 18 << 3 << ListRange();
5278 void tst_QQuickGridView::displacedTransitions()
5280 QFETCH(bool, useDisplaced);
5281 QFETCH(bool, displacedEnabled);
5282 QFETCH(bool, useAddDisplaced);
5283 QFETCH(bool, addDisplacedEnabled);
5284 QFETCH(bool, useMoveDisplaced);
5285 QFETCH(bool, moveDisplacedEnabled);
5286 QFETCH(bool, useRemoveDisplaced);
5287 QFETCH(bool, removeDisplacedEnabled);
5288 QFETCH(ListChange, change);
5289 QFETCH(ListRange, expectedDisplacedIndexes);
5292 for (int i = 0; i < 30; i++)
5293 model.addItem("Original item" + QString::number(i), "");
5294 QaimModel model_displaced_transitionVia;
5295 QaimModel model_addDisplaced_transitionVia;
5296 QaimModel model_moveDisplaced_transitionVia;
5297 QaimModel model_removeDisplaced_transitionVia;
5299 QPointF displaced_transitionVia(-50, -100);
5300 QPointF addDisplaced_transitionVia(-150, 100);
5301 QPointF moveDisplaced_transitionVia(50, -100);
5302 QPointF removeDisplaced_transitionVia(150, 100);
5304 QQuickView *canvas = getView();
5305 QQmlContext *ctxt = canvas->rootContext();
5306 ctxt->setContextProperty("testModel", &model);
5307 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
5308 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
5309 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
5310 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
5311 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
5312 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
5313 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
5314 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
5315 ctxt->setContextProperty("useDisplaced", useDisplaced);
5316 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
5317 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
5318 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
5319 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
5320 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
5321 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
5322 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
5323 canvas->setSource(testFileUrl("displacedTransitions.qml"));
5325 qApp->processEvents();
5327 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5328 QTRY_VERIFY(gridview != 0);
5329 QQuickItem *contentItem = gridview->contentItem();
5330 QVERIFY(contentItem != 0);
5331 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5333 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5334 gridview->setProperty("displaceTransitionsDone", false);
5336 switch (change.type) {
5337 case ListChange::Inserted:
5339 QList<QPair<QString, QString> > targetItemData;
5340 for (int i=change.index; i<change.index + change.count; ++i)
5341 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
5342 model.insertItems(change.index, targetItemData);
5343 QTRY_COMPARE(model.count(), gridview->count());
5346 case ListChange::Removed:
5347 model.removeItems(change.index, change.count);
5348 QTRY_COMPARE(model.count(), gridview->count());
5350 case ListChange::Moved:
5351 model.moveItems(change.index, change.to, change.count);
5352 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5354 case ListChange::SetCurrent:
5355 case ListChange::SetContentY:
5359 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
5360 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
5362 if ((useDisplaced && displacedEnabled)
5363 || (useAddDisplaced && addDisplacedEnabled)
5364 || (useMoveDisplaced && moveDisplacedEnabled)
5365 || (useRemoveDisplaced && removeDisplacedEnabled)) {
5366 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5368 // check the correct number of target items and indexes were received
5369 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
5370 for (int i=0; i<resultTargetIndexes.count(); i++)
5371 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
5372 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
5373 for (int i=0; i<resultTargetItems.count(); i++)
5374 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
5376 QCOMPARE(resultTargetIndexes.count(), 0);
5377 QCOMPARE(resultTargetItems.count(), 0);
5380 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
5381 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
5383 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
5384 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
5385 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
5387 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
5388 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
5389 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
5391 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
5393 if (useDisplaced && displacedEnabled
5394 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
5395 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
5396 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
5397 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
5399 QCOMPARE(model_displaced_transitionVia.count(), 0);
5402 // verify all items moved to the correct final positions
5403 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5404 for (int i=0; i < model.count() && i < items.count(); ++i) {
5405 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5406 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5407 QCOMPARE(item->x(), (i%3)*80.0);
5408 QCOMPARE(item->y(), (i/3)*60.0);
5409 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5411 QTRY_COMPARE(name->text(), model.name(i));
5414 releaseView(canvas);
5417 void tst_QQuickGridView::displacedTransitions_data()
5419 QTest::addColumn<bool>("useDisplaced");
5420 QTest::addColumn<bool>("displacedEnabled");
5421 QTest::addColumn<bool>("useAddDisplaced");
5422 QTest::addColumn<bool>("addDisplacedEnabled");
5423 QTest::addColumn<bool>("useMoveDisplaced");
5424 QTest::addColumn<bool>("moveDisplacedEnabled");
5425 QTest::addColumn<bool>("useRemoveDisplaced");
5426 QTest::addColumn<bool>("removeDisplacedEnabled");
5427 QTest::addColumn<ListChange>("change");
5428 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5430 QTest::newRow("no displaced transitions at all")
5435 << ListChange::insert(0, 1) << ListRange(0, 17);
5437 QTest::newRow("just displaced")
5442 << ListChange::insert(0, 1) << ListRange(0, 17);
5444 QTest::newRow("just displaced (not enabled)")
5449 << ListChange::insert(0, 1) << ListRange(0, 17);
5451 QTest::newRow("displaced + addDisplaced")
5456 << ListChange::insert(0, 1) << ListRange(0, 17);
5458 QTest::newRow("displaced + addDisplaced (not enabled)")
5463 << ListChange::insert(0, 1) << ListRange(0, 17);
5465 QTest::newRow("displaced + moveDisplaced")
5470 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5472 QTest::newRow("displaced + moveDisplaced (not enabled)")
5477 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5479 QTest::newRow("displaced + removeDisplaced")
5484 << ListChange::remove(0, 1) << ListRange(1, 18);
5486 QTest::newRow("displaced + removeDisplaced (not enabled)")
5491 << ListChange::remove(0, 1) << ListRange(1, 18);
5494 QTest::newRow("displaced + add, should use generic displaced for a remove")
5499 << ListChange::remove(0, 1) << ListRange(1, 18);
5502 void tst_QQuickGridView::multipleTransitions()
5504 // Tests that if you interrupt a transition in progress with another action that
5505 // cancels the previous transition, the resulting items are still placed correctly.
5507 QFETCH(int, initialCount);
5508 QFETCH(qreal, contentY);
5509 QFETCH(QList<ListChange>, changes);
5510 QFETCH(bool, enableAddTransitions);
5511 QFETCH(bool, enableMoveTransitions);
5512 QFETCH(bool, enableRemoveTransitions);
5513 QFETCH(bool, rippleAddDisplaced);
5515 // add transitions on the left, moves on the right
5516 QPointF addTargets_transitionFrom(-50, -50);
5517 QPointF addDisplaced_transitionFrom(-50, 50);
5518 QPointF moveTargets_transitionFrom(50, -50);
5519 QPointF moveDisplaced_transitionFrom(50, 50);
5520 QPointF removeTargets_transitionTo(-100, 300);
5521 QPointF removeDisplaced_transitionFrom(100, 300);
5524 for (int i = 0; i < initialCount; i++)
5525 model.addItem("Original item" + QString::number(i), "");
5527 QQuickView *canvas = getView();
5528 QQmlContext *ctxt = canvas->rootContext();
5529 ctxt->setContextProperty("testModel", &model);
5530 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5531 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5532 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5533 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5534 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5535 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5536 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5537 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5538 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5539 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5540 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5542 QTest::qWaitForWindowShown(canvas);
5544 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5545 QTRY_VERIFY(gridview != 0);
5546 QQuickItem *contentItem = gridview->contentItem();
5547 QVERIFY(contentItem != 0);
5548 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5550 if (contentY != 0) {
5551 gridview->setContentY(contentY);
5552 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5555 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5557 for (int i=0; i<changes.count(); i++) {
5558 switch (changes[i].type) {
5559 case ListChange::Inserted:
5561 QList<QPair<QString, QString> > targetItems;
5562 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5563 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5564 model.insertItems(changes[i].index, targetItems);
5565 QTRY_COMPARE(model.count(), gridview->count());
5566 if (i == changes.count() - 1) {
5567 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
5568 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
5570 QTest::qWait(timeBetweenActions);
5574 case ListChange::Removed:
5575 model.removeItems(changes[i].index, changes[i].count);
5576 QTRY_COMPARE(model.count(), gridview->count());
5577 if (i == changes.count() - 1) {
5578 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
5579 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
5581 QTest::qWait(timeBetweenActions);
5584 case ListChange::Moved:
5585 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5586 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5587 if (i == changes.count() - 1) {
5588 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
5589 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
5591 QTest::qWait(timeBetweenActions);
5594 case ListChange::SetCurrent:
5595 gridview->setCurrentIndex(changes[i].index);
5596 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5598 case ListChange::SetContentY:
5599 gridview->setContentY(changes[i].pos);
5600 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5604 QCOMPARE(gridview->count(), model.count());
5606 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5607 int firstVisibleIndex = -1;
5608 for (int i=0; i<items.count(); i++) {
5609 if (items[i]->y() >= contentY) {
5610 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5611 firstVisibleIndex = e.evaluate().toInt();
5615 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5617 // verify all items moved to the correct final positions
5618 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5619 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5620 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5621 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5622 QTRY_COMPARE(item->x(), (i%3)*80.0);
5623 QTRY_COMPARE(item->y(), (i/3)*60.0);
5624 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5626 QTRY_COMPARE(name->text(), model.name(i));
5629 releaseView(canvas);
5632 void tst_QQuickGridView::multipleTransitions_data()
5634 QTest::addColumn<int>("initialCount");
5635 QTest::addColumn<qreal>("contentY");
5636 QTest::addColumn<QList<ListChange> >("changes");
5637 QTest::addColumn<bool>("enableAddTransitions");
5638 QTest::addColumn<bool>("enableMoveTransitions");
5639 QTest::addColumn<bool>("enableRemoveTransitions");
5640 QTest::addColumn<bool>("rippleAddDisplaced");
5642 // the added item and displaced items should move to final dest correctly
5643 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5644 << ListChange::insert(0, 1)
5645 << ListChange::move(0, 3, 1)
5647 << true << true << true << false;
5649 // items affected by the add should change from move to add transition
5650 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5651 << ListChange::move(1, 10, 3)
5652 << ListChange::insert(0, 1)
5654 << true << true << true << false;
5656 // items should be placed correctly if you trigger a transition then refill for that index
5657 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5658 << ListChange::insert(0, 1)
5659 << ListChange::setContentY(160.0)
5660 << ListChange::setContentY(0.0)
5661 << ListChange::insert(0, 1)
5663 << true << true << true << false;
5665 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5666 << ListChange::insert(1, 1)
5667 << ListChange::remove(1, 1)
5669 << true << true << true << true;
5671 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5672 // even if a remove-displace transition is not present to re-animate them
5673 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5674 << ListChange::insert(0, 1)
5675 << ListChange::remove(2, 1)
5677 << true << true << false << false;
5679 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5680 // remove has changed the position of where it will move to
5681 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5682 << ListChange::setContentY(-10.0)
5683 << ListChange::insert(0, 1)
5684 << ListChange::insert(0, 1)
5685 << ListChange::remove(2, 1)
5687 << true << true << false << false;
5690 void tst_QQuickGridView::multipleDisplaced()
5692 // multiple move() operations should only restart displace transitions for items that
5693 // moved from previously set positions, and not those that have moved from their current
5694 // item positions (which may e.g. still be changing from easing bounces in the last transition)
5697 for (int i = 0; i < 30; i++)
5698 model.addItem("Original item" + QString::number(i), "");
5700 QQuickView *canvas = createView();
5701 QQmlContext *ctxt = canvas->rootContext();
5702 ctxt->setContextProperty("testModel", &model);
5703 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5705 QTest::qWaitForWindowShown(canvas);
5707 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5708 QTRY_VERIFY(gridview != 0);
5709 QQuickItem *contentItem = gridview->contentItem();
5710 QVERIFY(contentItem != 0);
5711 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5713 model.moveItems(12, 8, 1);
5714 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5715 model.moveItems(8, 3, 1);
5716 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5718 QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5719 foreach (const QString &name, transitionsStarted.keys()) {
5720 QVERIFY2(transitionsStarted[name] == 1,
5721 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5724 // verify all items moved to the correct final positions
5725 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5726 for (int i=0; i < model.count() && i < items.count(); ++i) {
5727 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5728 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5729 QTRY_COMPARE(item->x(), (i%3)*80.0);
5730 QTRY_COMPARE(item->y(), (i/3)*60.0);
5731 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5733 QTRY_COMPARE(name->text(), model.name(i));
5739 void tst_QQuickGridView::cacheBuffer()
5741 QQuickView *canvas = createView();
5744 for (int i = 0; i < 90; i++)
5745 model.addItem("Item" + QString::number(i), "");
5747 canvas->rootContext()->setContextProperty("testModel", &model);
5748 canvas->setSource(testFileUrl("gridview1.qml"));
5750 qApp->processEvents();
5752 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5753 QVERIFY(gridview != 0);
5755 QQuickItem *contentItem = gridview->contentItem();
5756 QVERIFY(contentItem != 0);
5757 QVERIFY(gridview->delegate() != 0);
5758 QVERIFY(gridview->model() != 0);
5760 // Confirm items positioned correctly
5761 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5762 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5763 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5764 QTRY_COMPARE(item->x(), (i%3)*80.0);
5765 QTRY_COMPARE(item->y(), (i/3)*60.0);
5768 QQmlIncubationController controller;
5769 canvas->engine()->setIncubationController(&controller);
5771 canvas->rootObject()->setProperty("cacheBuffer", 200);
5772 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5774 // items will be created one at a time
5775 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5776 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5777 QQuickItem *item = 0;
5780 controller.incubateWhile(&b);
5781 item = findItem<QQuickItem>(gridview, "wrapper", i);
5787 controller.incubateWhile(&b);
5790 int newItemCount = 0;
5791 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5793 // Confirm items positioned correctly
5794 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5795 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5797 QTRY_COMPARE(item->x(), (i%3)*80.0);
5798 QTRY_COMPARE(item->y(), (i/3)*60.0);
5801 // move view and confirm items in view are visible immediately and outside are created async
5802 gridview->setContentY(300);
5804 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5805 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5807 QTRY_COMPARE(item->x(), (i%3)*80.0);
5808 QTRY_COMPARE(item->y(), (i/3)*60.0);
5811 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5813 // ensure buffered items are created
5814 for (int i = 34; i < qMin(44,model.count()); ++i) {
5815 QQuickItem *item = 0;
5817 qGuiApp->processEvents(); // allow refill to happen
5819 controller.incubateWhile(&b);
5820 item = findItem<QQuickItem>(gridview, "wrapper", i);
5826 controller.incubateWhile(&b);
5832 void tst_QQuickGridView::asynchronous()
5834 QQuickView *canvas = createView();
5836 QQmlIncubationController controller;
5837 canvas->engine()->setIncubationController(&controller);
5839 canvas->setSource(testFileUrl("asyncloader.qml"));
5841 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5842 QVERIFY(rootObject);
5844 QQuickGridView *gridview = 0;
5847 controller.incubateWhile(&b);
5848 gridview = rootObject->findChild<QQuickGridView*>("view");
5851 // items will be created one at a time
5852 for (int i = 0; i < 12; ++i) {
5853 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5854 QQuickItem *item = 0;
5857 controller.incubateWhile(&b);
5858 item = findItem<QQuickItem>(gridview, "wrapper", i);
5864 controller.incubateWhile(&b);
5867 // verify positioning
5868 QQuickItem *contentItem = gridview->contentItem();
5869 for (int i = 0; i < 12; ++i) {
5870 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5871 if (!item) qWarning() << "Item" << i << "not found";
5872 QVERIFY(item->x() == (i%3)*100);
5873 QVERIFY(item->y() == (i/3)*100);
5879 void tst_QQuickGridView::unrequestedVisibility()
5882 for (int i = 0; i < 30; i++)
5883 model.addItem("Item" + QString::number(i), QString::number(i));
5885 QQuickView *canvas = new QQuickView(0);
5886 canvas->setGeometry(0,0,240,320);
5888 QQmlContext *ctxt = canvas->rootContext();
5889 ctxt->setContextProperty("testModel", &model);
5890 ctxt->setContextProperty("testWrap", QVariant(false));
5892 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5896 qApp->processEvents();
5898 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5899 QTRY_VERIFY(leftview != 0);
5901 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5902 QTRY_VERIFY(rightview != 0);
5904 QQuickItem *leftContent = leftview->contentItem();
5905 QTRY_VERIFY(leftContent != 0);
5907 QQuickItem *rightContent = rightview->contentItem();
5908 QTRY_VERIFY(rightContent != 0);
5910 rightview->setCurrentIndex(12);
5912 QTRY_COMPARE(leftview->contentY(), 0.0);
5913 QTRY_COMPARE(rightview->contentY(), 240.0);
5917 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5918 QCOMPARE(item->isVisible(), true);
5919 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5920 QCOMPARE(item->isVisible(), false);
5922 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5923 QCOMPARE(item->isVisible(), false);
5924 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5925 QCOMPARE(item->isVisible(), true);
5927 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5928 QCOMPARE(item->isVisible(), true);
5929 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5930 QCOMPARE(item->isVisible(), false);
5931 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5932 QCOMPARE(item->isVisible(), false);
5933 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5934 QCOMPARE(item->isVisible(), true);
5936 rightview->setCurrentIndex(0);
5938 QTRY_COMPARE(leftview->contentY(), 0.0);
5939 QTRY_COMPARE(rightview->contentY(), 0.0);
5941 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5942 QCOMPARE(item->isVisible(), true);
5943 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5944 QTRY_COMPARE(item->isVisible(), true);
5946 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5947 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5949 leftview->setCurrentIndex(12);
5951 QTRY_COMPARE(leftview->contentY(), 240.0);
5952 QTRY_COMPARE(rightview->contentY(), 0.0);
5954 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5955 QTRY_COMPARE(item->isVisible(), false);
5956 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5957 QCOMPARE(item->isVisible(), true);
5959 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5960 QCOMPARE(item->isVisible(), true);
5961 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5962 QCOMPARE(item->isVisible(), false);
5964 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5965 QCOMPARE(item->isVisible(), false);
5966 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5967 QCOMPARE(item->isVisible(), true);
5968 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5969 QCOMPARE(item->isVisible(), true);
5970 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5971 QCOMPARE(item->isVisible(), false);
5973 // move a non-visible item into view
5974 model.moveItems(10, 9, 1);
5975 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5977 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5978 QCOMPARE(item->isVisible(), false);
5979 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5980 QCOMPARE(item->isVisible(), true);
5982 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5983 QCOMPARE(item->isVisible(), true);
5984 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5985 QCOMPARE(item->isVisible(), false);
5987 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5988 QCOMPARE(item->isVisible(), false);
5989 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5990 QCOMPARE(item->isVisible(), true);
5991 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5992 QCOMPARE(item->isVisible(), true);
5993 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5994 QCOMPARE(item->isVisible(), false);
5996 // move a visible item out of view
5997 model.moveItems(5, 3, 1);
5998 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6000 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6001 QCOMPARE(item->isVisible(), false);
6002 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6003 QCOMPARE(item->isVisible(), true);
6004 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6005 QCOMPARE(item->isVisible(), true);
6006 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6007 QCOMPARE(item->isVisible(), false);
6009 // move a non-visible item into view
6010 model.moveItems(3, 5, 1);
6011 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6013 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6014 QCOMPARE(item->isVisible(), false);
6015 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6016 QCOMPARE(item->isVisible(), true);
6017 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6018 QCOMPARE(item->isVisible(), true);
6019 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6020 QCOMPARE(item->isVisible(), false);
6022 // move a visible item out of view
6023 model.moveItems(9, 10, 1);
6024 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6026 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6027 QCOMPARE(item->isVisible(), false);
6028 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6029 QCOMPARE(item->isVisible(), true);
6030 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6031 QCOMPARE(item->isVisible(), true);
6032 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6033 QCOMPARE(item->isVisible(), false);
6035 // move a non-visible item into view
6036 model.moveItems(10, 9, 1);
6037 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6039 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6040 QCOMPARE(item->isVisible(), false);
6041 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6042 QCOMPARE(item->isVisible(), true);
6043 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6044 QCOMPARE(item->isVisible(), true);
6045 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6046 QCOMPARE(item->isVisible(), false);
6052 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB()
6054 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6057 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB_data()
6059 inserted_defaultLayout_data();
6062 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB()
6064 inserted_defaultLayout(QQuickGridView::FlowTopToBottom);
6067 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB_data()
6069 inserted_defaultLayout_data();
6072 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB()
6074 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6077 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB_data()
6079 inserted_defaultLayout_data();
6082 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT()
6084 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6087 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT_data()
6089 inserted_defaultLayout_data();
6092 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT()
6094 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6097 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT_data()
6099 inserted_defaultLayout_data();
6102 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT()
6104 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6107 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT_data()
6109 inserted_defaultLayout_data();
6112 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT()
6114 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6117 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT_data()
6119 inserted_defaultLayout_data();
6123 void tst_QQuickGridView::removed_leftToRight_RtL_TtB()
6125 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6128 void tst_QQuickGridView::removed_leftToRight_RtL_TtB_data()
6130 removed_defaultLayout_data();
6133 void tst_QQuickGridView::removed_topToBottom_LtR_TtB()
6135 removed_defaultLayout(QQuickGridView::FlowTopToBottom);
6138 void tst_QQuickGridView::removed_topToBottom_LtR_TtB_data()
6140 removed_defaultLayout_data();
6143 void tst_QQuickGridView::removed_topToBottom_RtL_TtB()
6145 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6148 void tst_QQuickGridView::removed_topToBottom_RtL_TtB_data()
6150 removed_defaultLayout_data();
6153 void tst_QQuickGridView::removed_leftToRight_LtR_BtT()
6155 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6158 void tst_QQuickGridView::removed_leftToRight_LtR_BtT_data()
6160 removed_defaultLayout_data();
6163 void tst_QQuickGridView::removed_leftToRight_RtL_BtT()
6165 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6168 void tst_QQuickGridView::removed_leftToRight_RtL_BtT_data()
6170 removed_defaultLayout_data();
6173 void tst_QQuickGridView::removed_topToBottom_LtR_BtT()
6175 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6178 void tst_QQuickGridView::removed_topToBottom_LtR_BtT_data()
6180 removed_defaultLayout_data();
6183 void tst_QQuickGridView::removed_topToBottom_RtL_BtT()
6185 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6188 void tst_QQuickGridView::removed_topToBottom_RtL_BtT_data()
6190 removed_defaultLayout_data();
6194 void tst_QQuickGridView::moved_leftToRight_RtL_TtB()
6196 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6199 void tst_QQuickGridView::moved_leftToRight_RtL_TtB_data()
6201 moved_defaultLayout_data();
6204 void tst_QQuickGridView::moved_topToBottom_LtR_TtB()
6206 moved_defaultLayout(QQuickGridView::FlowTopToBottom);
6209 void tst_QQuickGridView::moved_topToBottom_LtR_TtB_data()
6211 moved_defaultLayout_data();
6214 void tst_QQuickGridView::moved_topToBottom_RtL_TtB()
6216 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6219 void tst_QQuickGridView::moved_topToBottom_RtL_TtB_data()
6221 moved_defaultLayout_data();
6224 void tst_QQuickGridView::moved_leftToRight_LtR_BtT()
6226 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6229 void tst_QQuickGridView::moved_leftToRight_LtR_BtT_data()
6231 moved_defaultLayout_data();
6234 void tst_QQuickGridView::moved_leftToRight_RtL_BtT()
6236 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6239 void tst_QQuickGridView::moved_leftToRight_RtL_BtT_data()
6241 moved_defaultLayout_data();
6244 void tst_QQuickGridView::moved_topToBottom_LtR_BtT()
6246 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6249 void tst_QQuickGridView::moved_topToBottom_LtR_BtT_data()
6251 moved_defaultLayout_data();
6254 void tst_QQuickGridView::moved_topToBottom_RtL_BtT()
6256 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6259 void tst_QQuickGridView::moved_topToBottom_RtL_BtT_data()
6261 moved_defaultLayout_data();
6265 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
6269 for (int i=0; i<list.count(); i++) {
6270 ret << list[i].toInt(&ok);
6272 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
6278 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6280 for (int i=0; i<indexLists.count(); i++) {
6281 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6282 if (current != expectedIndexes.toSet())
6283 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6284 QCOMPARE(current, expectedIndexes.toSet());
6288 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6290 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6291 QVERIFY(it.value().type() == QVariant::Int);
6292 QString name = it.key();
6293 int itemIndex = it.value().toInt();
6294 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6295 if (model.name(itemIndex) != name)
6296 qDebug() << itemIndex;
6297 QCOMPARE(model.name(itemIndex), name);
6299 QCOMPARE(items.count(), expectedIndexes.count());
6302 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6304 for (int i=0; i<itemLists.count(); i++) {
6305 QVariantList current = itemLists[i].toList();
6306 for (int j=0; j<current.count(); j++) {
6307 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6308 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6309 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6311 QCOMPARE(current.count(), expectedItems.count());
6315 QTEST_MAIN(tst_QQuickGridView)
6317 #include "tst_qquickgridview.moc"