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();
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::extents()
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);
3497 QFETCH(QPointF, origin_empty);
3498 QFETCH(QPointF, origin_nonEmpty);
3500 QQuickView *canvas = getView();
3503 QQmlContext *ctxt = canvas->rootContext();
3504 ctxt->setContextProperty("testModel", &model);
3505 canvas->setSource(testFileUrl("headerfooter.qml"));
3507 qApp->processEvents();
3509 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3510 QTRY_VERIFY(gridview != 0);
3511 gridview->setFlow(flow);
3512 gridview->setLayoutDirection(layoutDirection);
3513 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3514 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3516 QQuickItem *contentItem = gridview->contentItem();
3517 QTRY_VERIFY(contentItem != 0);
3519 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3521 QCOMPARE(header->pos(), headerPos);
3523 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3525 QCOMPARE(footer->pos(), footerPos);
3527 QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), minPos.x());
3528 QCOMPARE(static_cast<GVAccessor*>(gridview)->minY(), minPos.y());
3529 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), maxPos.x());
3530 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxY(), maxPos.y());
3532 QCOMPARE(gridview->xOrigin(), origin_empty.x());
3533 QCOMPARE(gridview->yOrigin(), origin_empty.y());
3534 for (int i=0; i<30; i++)
3535 model.addItem("Item" + QString::number(i), "");
3536 QTRY_COMPARE(gridview->count(), model.count());
3537 QCOMPARE(gridview->xOrigin(), origin_nonEmpty.x());
3538 QCOMPARE(gridview->yOrigin(), origin_nonEmpty.y());
3540 releaseView(canvas);
3543 void tst_QQuickGridView::extents_data()
3545 QTest::addColumn<QQuickGridView::Flow>("flow");
3546 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3547 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3548 QTest::addColumn<QPointF>("headerPos");
3549 QTest::addColumn<QPointF>("footerPos");
3550 QTest::addColumn<QPointF>("minPos");
3551 QTest::addColumn<QPointF>("maxPos");
3552 QTest::addColumn<QPointF>("origin_empty");
3553 QTest::addColumn<QPointF>("origin_nonEmpty");
3555 // header is 240x20 (or 20x320 in TopToBottom)
3556 // footer is 240x30 (or 30x320 in TopToBottom)
3557 // grid has 10 rows in LeftToRight mode and 6 columns in TopToBottom
3559 QTest::newRow("LeftToRight, LtR, TtB")
3560 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3561 << QPointF(0, -20) << QPointF(0, 0)
3562 << QPointF(0, 20) << QPointF(240, 20)
3563 << QPointF(0, -20) << QPointF(0, -20);
3565 QTest::newRow("LeftToRight, RtL, TtB")
3566 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3567 << QPointF(0, -20) << QPointF(0, 0)
3568 << QPointF(0, 20) << QPointF(240, 20)
3569 << QPointF(0, -20) << QPointF(0, -20);
3571 QTest::newRow("LeftToRight, LtR, BtT")
3572 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3573 << QPointF(0, 0) << QPointF(0, -30)
3574 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3575 << QPointF(0, -30) << QPointF(0, (-60.0 * 10) - 30);
3577 QTest::newRow("LeftToRight, RtL, BtT")
3578 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3579 << QPointF(0, 0) << QPointF(0, -30)
3580 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3581 << QPointF(0, -30) << QPointF(0, (-60.0 * 10) - 30);
3584 QTest::newRow("TopToBottom, LtR, TtB")
3585 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3586 << QPointF(-20, 0) << QPointF(0, 0)
3587 << QPointF(20, 0) << QPointF(20, 320)
3588 << QPointF(-20, 0) << QPointF(-20, 0);
3590 QTest::newRow("TopToBottom, RtL, TtB")
3591 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3592 << QPointF(0, 0) << QPointF(-30, 0)
3593 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3594 << QPointF(-30, 0) << QPointF((-80.0 * 6) - 30, 0);
3596 QTest::newRow("TopToBottom, LtR, BtT")
3597 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3598 << QPointF(-20, -320) << QPointF(0, -320)
3599 << QPointF(20, 0) << QPointF(20, 320)
3600 << QPointF(-20, 0) << QPointF(-20, 0);
3602 QTest::newRow("TopToBottom, RtL, BtT")
3603 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3604 << QPointF(0, -320) << QPointF(-30, -320)
3605 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3606 << QPointF(-30, 0) << QPointF((-80.0 * 6) - 30, 0);
3609 void tst_QQuickGridView::resetModel_headerFooter()
3611 // Resetting a model shouldn't crash in views with header/footer
3613 QQuickView *canvas = createView();
3616 for (int i = 0; i < 6; i++)
3617 model.addItem("Item" + QString::number(i), "");
3618 QQmlContext *ctxt = canvas->rootContext();
3619 ctxt->setContextProperty("testModel", &model);
3621 canvas->setSource(testFileUrl("headerfooter.qml"));
3622 qApp->processEvents();
3624 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3625 QTRY_VERIFY(gridview != 0);
3627 QQuickItem *contentItem = gridview->contentItem();
3628 QTRY_VERIFY(contentItem != 0);
3630 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3632 QCOMPARE(header->y(), -header->height());
3634 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3636 QCOMPARE(footer->y(), 60.*2);
3640 header = findItem<QQuickItem>(contentItem, "header");
3642 QCOMPARE(header->y(), -header->height());
3644 footer = findItem<QQuickItem>(contentItem, "footer");
3646 QCOMPARE(footer->y(), 60.*2);
3651 void tst_QQuickGridView::resizeViewAndRepaint()
3653 QQuickView *canvas = createView();
3657 for (int i = 0; i < 40; i++)
3658 model.addItem("Item" + QString::number(i), "");
3660 QQmlContext *ctxt = canvas->rootContext();
3661 ctxt->setContextProperty("testModel", &model);
3662 ctxt->setContextProperty("initialWidth", 240);
3663 ctxt->setContextProperty("initialHeight", 100);
3665 canvas->setSource(testFileUrl("resizeview.qml"));
3667 qApp->processEvents();
3669 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3670 QTRY_VERIFY(gridview != 0);
3671 QQuickItem *contentItem = gridview->contentItem();
3672 QTRY_VERIFY(contentItem != 0);
3673 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3675 // item at index 10 should not be currently visible
3676 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3678 gridview->setHeight(320);
3679 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3681 gridview->setHeight(100);
3682 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3684 // Ensure we handle -ve sizes
3685 gridview->setHeight(-100);
3686 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3688 gridview->setCacheBuffer(120);
3689 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3691 // ensure items in cache become visible
3692 gridview->setHeight(120);
3693 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3695 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3696 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3697 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3698 if (!item) qWarning() << "Item" << i << "not found";
3700 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3701 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3702 QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3705 // ensure items outside view become invisible
3706 gridview->setHeight(60);
3707 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3709 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3710 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3711 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3712 if (!item) qWarning() << "Item" << i << "not found";
3714 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3715 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3716 QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3722 void tst_QQuickGridView::resizeGrid()
3724 QFETCH(QQuickGridView::Flow, flow);
3725 QFETCH(Qt::LayoutDirection, layoutDirection);
3726 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3727 QFETCH(QPointF, initialContentPos);
3728 QFETCH(QPointF, firstItemPos);
3731 for (int i = 0; i < 30; i++)
3732 model.addItem("Item" + QString::number(i), "");
3734 QQuickView *canvas = getView();
3735 QQmlContext *ctxt = canvas->rootContext();
3736 ctxt->setContextProperty("testModel", &model);
3737 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
3738 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
3739 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickGridView::BottomToTop);
3740 canvas->setSource(testFileUrl("resizegrid.qml"));
3742 qApp->processEvents();
3744 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3745 QTRY_VERIFY(gridview != 0);
3746 QQuickItem *contentItem = gridview->contentItem();
3747 QTRY_VERIFY(contentItem != 0);
3749 // set the width to slightly larger than 3 items across, to test
3750 // items are aligned correctly in right-to-left
3751 canvas->rootObject()->setWidth(260);
3752 canvas->rootObject()->setHeight(320);
3753 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3755 QCOMPARE(gridview->contentX(), initialContentPos.x());
3756 QCOMPARE(gridview->contentY(), initialContentPos.y());
3758 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
3760 QCOMPARE(item0->pos(), firstItemPos);
3762 // Confirm items positioned correctly and indexes correct
3763 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
3764 QVERIFY(items.count() >= 18 && items.count() <= 21);
3765 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3766 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3767 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3768 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3769 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3771 QCOMPARE(name->text(), model.name(i));
3774 // change from 3x5 grid to 4x7
3775 canvas->rootObject()->setWidth(canvas->rootObject()->width() + 80);
3776 canvas->rootObject()->setHeight(canvas->rootObject()->height() + 60*2);
3777 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3779 // other than in LeftToRight+RightToLeft layout, the first item should not move
3780 // if view is resized
3781 QCOMPARE(findItem<QQuickItem>(contentItem, "wrapper", 0), item0);
3782 if (flow == QQuickGridView::FlowLeftToRight && layoutDirection == Qt::RightToLeft)
3783 firstItemPos.rx() += 80;
3784 QCOMPARE(item0->pos(), firstItemPos);
3786 QPointF newContentPos = initialContentPos;
3787 if (flow == QQuickGridView::FlowTopToBottom && layoutDirection == Qt::RightToLeft)
3788 newContentPos.rx() -= 80.0;
3789 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3790 newContentPos.ry() -= 60.0 * 2;
3791 QCOMPARE(gridview->contentX(), newContentPos.x());
3792 QCOMPARE(gridview->contentY(), newContentPos.y());
3794 // Confirm items positioned correctly and indexes correct
3795 items = findItems<QQuickItem>(contentItem, "wrapper");
3796 QVERIFY(items.count() >= 28);
3797 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3798 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3799 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3800 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3801 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3803 QCOMPARE(name->text(), model.name(i));
3806 releaseView(canvas);
3809 void tst_QQuickGridView::resizeGrid_data()
3811 QTest::addColumn<QQuickGridView::Flow>("flow");
3812 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3813 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3814 QTest::addColumn<QPointF>("initialContentPos");
3815 QTest::addColumn<QPointF>("firstItemPos");
3817 // Initial view width is 260, so in LeftToRight + right-to-left mode the
3818 // content x should be -20
3820 QTest::newRow("LeftToRight, LtR, TtB")
3821 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3825 QTest::newRow("LeftToRight, RtL, TtB")
3826 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3827 << QPointF(-20.0, 0)
3828 << QPointF(80.0 * 2, 0);
3830 QTest::newRow("LeftToRight, LtR, BtT")
3831 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3833 << QPointF(0, -60.0);
3835 QTest::newRow("LeftToRight, RtL, BtT")
3836 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3837 << QPointF(-20.0, -320)
3838 << QPointF(80.0 * 2, -60.0);
3841 QTest::newRow("TopToBottom, LtR, TtB")
3842 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3846 QTest::newRow("TopToBottom, RtL, TtB")
3847 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3849 << QPointF(-80.0, 0);
3851 QTest::newRow("TopToBottom, LtR, BtT")
3852 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3854 << QPointF(0, -60.0);
3856 QTest::newRow("TopToBottom, RtL, BtT")
3857 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3858 << QPointF(-260, -320)
3859 << QPointF(-80.0, -60.0);
3863 void tst_QQuickGridView::changeColumnCount()
3866 for (int i = 0; i < 40; i++)
3867 model.addItem("Item" + QString::number(i), "");
3869 QQuickView *canvas = createView();
3870 QQmlContext *ctxt = canvas->rootContext();
3871 ctxt->setContextProperty("testModel", &model);
3872 ctxt->setContextProperty("initialWidth", 100);
3873 ctxt->setContextProperty("initialHeight", 320);
3874 canvas->setSource(testFileUrl("resizeview.qml"));
3876 qApp->processEvents();
3878 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3879 QTRY_VERIFY(gridview != 0);
3880 QQuickItem *contentItem = gridview->contentItem();
3881 QTRY_VERIFY(contentItem != 0);
3882 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3884 // a single column of 6 items are visible
3885 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3886 QCOMPARE(itemCount, 6);
3887 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3888 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3889 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3890 QCOMPARE(item->x(), 0.0);
3891 QCOMPARE(item->y(), qreal(i*60));
3894 // now 6x3 grid is visible, plus 1 extra below for refill
3895 gridview->setWidth(240);
3896 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3897 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3898 QCOMPARE(itemCount, 6*3 + 1);
3899 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3900 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3901 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3902 QCOMPARE(item->x(), qreal((i%3)*80));
3903 QCOMPARE(item->y(), qreal((i/3)*60));
3906 // back to single column
3907 gridview->setWidth(100);
3908 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3909 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3910 QCOMPARE(itemCount, 6);
3911 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3912 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3913 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3914 QCOMPARE(item->x(), 0.0);
3915 QCOMPARE(item->y(), qreal(i*60));
3921 void tst_QQuickGridView::indexAt_itemAt_data()
3923 QTest::addColumn<qreal>("x");
3924 QTest::addColumn<qreal>("y");
3925 QTest::addColumn<int>("index");
3927 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3928 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3929 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3930 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3931 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3934 void tst_QQuickGridView::indexAt_itemAt()
3940 QQuickView *canvas = getView();
3943 model.addItem("Fred", "12345");
3944 model.addItem("John", "2345");
3945 model.addItem("Bob", "54321");
3946 model.addItem("Billy", "22345");
3947 model.addItem("Sam", "2945");
3948 model.addItem("Ben", "04321");
3949 model.addItem("Jim", "0780");
3951 canvas->rootContext()->setContextProperty("testModel", &model);
3952 canvas->setSource(testFileUrl("gridview1.qml"));
3953 qApp->processEvents();
3955 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3956 QTRY_VERIFY(gridview != 0);
3958 QQuickItem *contentItem = gridview->contentItem();
3959 QTRY_VERIFY(contentItem != 0);
3961 QTRY_COMPARE(gridview->count(), model.count());
3963 QQuickItem *item = 0;
3965 item = findItem<QQuickItem>(contentItem, "wrapper", index);
3968 QCOMPARE(gridview->indexAt(x, y), index);
3969 QVERIFY(gridview->itemAt(x, y) == item);
3971 releaseView(canvas);
3974 void tst_QQuickGridView::onAdd()
3976 QFETCH(int, initialItemCount);
3977 QFETCH(int, itemsToAdd);
3979 const int delegateWidth = 50;
3980 const int delegateHeight = 100;
3982 QQuickView *canvas = getView();
3983 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3985 // these initial items should not trigger GridView.onAdd
3986 for (int i=0; i<initialItemCount; i++)
3987 model.addItem("dummy value", "dummy value");
3989 QQmlContext *ctxt = canvas->rootContext();
3990 ctxt->setContextProperty("testModel", &model);
3991 ctxt->setContextProperty("delegateWidth", delegateWidth);
3992 ctxt->setContextProperty("delegateHeight", delegateHeight);
3993 canvas->setSource(testFileUrl("attachedSignals.qml"));
3995 QObject *object = canvas->rootObject();
3996 object->setProperty("width", canvas->width());
3997 object->setProperty("height", canvas->height());
3998 qApp->processEvents();
4000 QList<QPair<QString, QString> > items;
4001 for (int i=0; i<itemsToAdd; i++)
4002 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4003 model.addItems(items);
4005 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4006 qApp->processEvents();
4008 QVariantList result = object->property("addedDelegates").toList();
4009 QTRY_COMPARE(result.count(), items.count());
4010 for (int i=0; i<items.count(); i++)
4011 QCOMPARE(result[i].toString(), items[i].first);
4013 releaseView(canvas);
4016 void tst_QQuickGridView::onAdd_data()
4018 QTest::addColumn<int>("initialItemCount");
4019 QTest::addColumn<int>("itemsToAdd");
4021 QTest::newRow("0, add 1") << 0 << 1;
4022 QTest::newRow("0, add 2") << 0 << 2;
4023 QTest::newRow("0, add 10") << 0 << 10;
4025 QTest::newRow("1, add 1") << 1 << 1;
4026 QTest::newRow("1, add 2") << 1 << 2;
4027 QTest::newRow("1, add 10") << 1 << 10;
4029 QTest::newRow("5, add 1") << 5 << 1;
4030 QTest::newRow("5, add 2") << 5 << 2;
4031 QTest::newRow("5, add 10") << 5 << 10;
4034 void tst_QQuickGridView::onRemove()
4036 QFETCH(int, initialItemCount);
4037 QFETCH(int, indexToRemove);
4038 QFETCH(int, removeCount);
4040 const int delegateWidth = 50;
4041 const int delegateHeight = 100;
4043 for (int i=0; i<initialItemCount; i++)
4044 model.addItem(QString("value %1").arg(i), "dummy value");
4046 QQuickView *canvas = getView();
4047 QQmlContext *ctxt = canvas->rootContext();
4048 ctxt->setContextProperty("testModel", &model);
4049 ctxt->setContextProperty("delegateWidth", delegateWidth);
4050 ctxt->setContextProperty("delegateHeight", delegateHeight);
4051 canvas->setSource(testFileUrl("attachedSignals.qml"));
4052 QObject *object = canvas->rootObject();
4054 model.removeItems(indexToRemove, removeCount);
4055 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4056 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4058 releaseView(canvas);
4061 void tst_QQuickGridView::onRemove_data()
4063 QTest::addColumn<int>("initialItemCount");
4064 QTest::addColumn<int>("indexToRemove");
4065 QTest::addColumn<int>("removeCount");
4067 QTest::newRow("remove first") << 1 << 0 << 1;
4068 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4069 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4070 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4072 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4073 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4074 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4075 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4076 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4077 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4079 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4080 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4081 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4084 void tst_QQuickGridView::columnCount()
4087 canvas.setSource(testFileUrl("gridview4.qml"));
4089 canvas.requestActivateWindow();
4090 QTest::qWaitForWindowShown(&canvas);
4092 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
4094 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
4095 QCOMPARE(view->cellHeight(), qreal(100));
4097 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
4098 QCOMPARE(items.size(), 18);
4099 QCOMPARE(items.at(8)->y(), qreal(0));
4100 QCOMPARE(items.at(9)->y(), qreal(100));
4103 void tst_QQuickGridView::margins()
4106 QQuickView *canvas = createView();
4109 for (int i = 0; i < 40; i++)
4110 model.addItem("Item" + QString::number(i), "");
4112 QQmlContext *ctxt = canvas->rootContext();
4113 ctxt->setContextProperty("testModel", &model);
4114 ctxt->setContextProperty("testRightToLeft", QVariant(false));
4116 canvas->setSource(testFileUrl("margins.qml"));
4118 qApp->processEvents();
4120 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4121 QTRY_VERIFY(gridview != 0);
4122 QQuickItem *contentItem = gridview->contentItem();
4123 QTRY_VERIFY(contentItem != 0);
4124 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4126 QCOMPARE(gridview->contentX(), -30.);
4127 QCOMPARE(gridview->xOrigin(), 0.);
4130 gridview->positionViewAtEnd();
4131 qreal pos = gridview->contentX();
4132 gridview->setContentX(pos + 80);
4133 gridview->returnToBounds();
4134 QTRY_COMPARE(gridview->contentX(), pos + 50);
4136 // remove item before visible and check that left margin is maintained
4137 // and xOrigin is updated
4138 gridview->setContentX(200);
4139 model.removeItems(0, 4);
4141 gridview->setContentX(-50);
4142 gridview->returnToBounds();
4143 QCOMPARE(gridview->xOrigin(), 100.);
4144 QTRY_COMPARE(gridview->contentX(), 70.);
4146 // reduce left margin
4147 gridview->setLeftMargin(20);
4148 QCOMPARE(gridview->xOrigin(), 100.);
4149 QTRY_COMPARE(gridview->contentX(), 80.);
4152 gridview->positionViewAtEnd();
4153 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
4154 pos = gridview->contentX();
4155 gridview->setContentX(pos + 80);
4156 gridview->returnToBounds();
4157 QTRY_COMPARE(gridview->contentX(), pos + 50);
4159 // reduce right margin
4160 pos = gridview->contentX();
4161 gridview->setRightMargin(40);
4162 QCOMPARE(gridview->xOrigin(), 0.);
4163 QTRY_COMPARE(gridview->contentX(), pos-10);
4169 QQuickView *canvas = createView();
4173 for (int i = 0; i < 40; i++)
4174 model.addItem("Item" + QString::number(i), "");
4176 QQmlContext *ctxt = canvas->rootContext();
4177 ctxt->setContextProperty("testModel", &model);
4178 ctxt->setContextProperty("testRightToLeft", QVariant(true));
4180 canvas->setSource(testFileUrl("margins.qml"));
4181 qApp->processEvents();
4183 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4184 QTRY_VERIFY(gridview != 0);
4186 QQuickItem *contentItem = gridview->contentItem();
4187 QTRY_VERIFY(contentItem != 0);
4189 QTRY_COMPARE(gridview->contentX(), -240+50.);
4190 QTRY_COMPARE(gridview->xOrigin(), -100. * 10);
4193 gridview->positionViewAtEnd();
4194 qreal pos = gridview->contentX();
4195 gridview->setContentX(pos - 80);
4196 gridview->returnToBounds();
4197 QTRY_COMPARE(gridview->contentX(), pos - 30);
4199 // remove item before visible and check that left margin is maintained
4200 // and xOrigin is updated
4201 gridview->setContentX(-400);
4202 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4203 model.removeItems(0, 4);
4204 QTRY_COMPARE(model.count(), gridview->count());
4205 gridview->setContentX(-240+50);
4206 gridview->returnToBounds();
4207 QCOMPARE(gridview->xOrigin(), -1000.);
4208 QTRY_COMPARE(gridview->contentX(), -240-50.);
4210 // reduce right margin
4211 pos = gridview->contentX();
4212 gridview->setRightMargin(40);
4213 QCOMPARE(gridview->xOrigin(), -1000.);
4214 QTRY_COMPARE(gridview->contentX(), -240-100 + 40.);
4217 gridview->positionViewAtEnd();
4218 QCOMPARE(gridview->xOrigin(), -900.); // positionViewAtEnd() resets origin
4219 pos = gridview->contentX();
4220 gridview->setContentX(pos - 80);
4221 gridview->returnToBounds();
4222 QTRY_COMPARE(gridview->contentX(), pos - 30);
4224 // reduce left margin
4225 pos = gridview->contentX();
4226 gridview->setLeftMargin(20);
4227 QCOMPARE(gridview->xOrigin(), -900.);
4228 QTRY_COMPARE(gridview->contentX(), pos+10);
4234 void tst_QQuickGridView::creationContext()
4237 canvas.setGeometry(0,0,240,320);
4238 canvas.setSource(testFileUrl("creationContext.qml"));
4239 qApp->processEvents();
4241 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4243 QVERIFY(rootItem->property("count").toInt() > 0);
4246 QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
4247 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4248 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4249 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4250 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4251 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4254 void tst_QQuickGridView::snapToRow_data()
4256 QTest::addColumn<QQuickGridView::Flow>("flow");
4257 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4258 QTest::addColumn<int>("highlightRangeMode");
4259 QTest::addColumn<QPoint>("flickStart");
4260 QTest::addColumn<QPoint>("flickEnd");
4261 QTest::addColumn<qreal>("snapAlignment");
4262 QTest::addColumn<qreal>("endExtent");
4263 QTest::addColumn<qreal>("startExtent");
4265 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4266 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4268 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4269 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4271 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4272 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
4274 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4275 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4277 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4278 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4280 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4281 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
4284 void tst_QQuickGridView::snapToRow()
4286 QFETCH(QQuickGridView::Flow, flow);
4287 QFETCH(Qt::LayoutDirection, layoutDirection);
4288 QFETCH(int, highlightRangeMode);
4289 QFETCH(QPoint, flickStart);
4290 QFETCH(QPoint, flickEnd);
4291 QFETCH(qreal, snapAlignment);
4292 QFETCH(qreal, endExtent);
4293 QFETCH(qreal, startExtent);
4295 QQuickView *canvas = getView();
4297 canvas->setSource(testFileUrl("snapToRow.qml"));
4299 qApp->processEvents();
4301 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4302 QTRY_VERIFY(gridview != 0);
4304 gridview->setFlow(flow);
4305 gridview->setLayoutDirection(layoutDirection);
4306 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4307 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4309 QQuickItem *contentItem = gridview->contentItem();
4310 QTRY_VERIFY(contentItem != 0);
4312 // confirm that a flick hits an item boundary
4313 flick(canvas, flickStart, flickEnd, 180);
4314 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4315 if (flow == QQuickGridView::FlowLeftToRight)
4316 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
4318 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
4322 flick(canvas, flickStart, flickEnd, 180);
4323 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4324 } while (flow == QQuickGridView::FlowLeftToRight
4325 ? !gridview->isAtYEnd()
4326 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4328 if (flow == QQuickGridView::FlowLeftToRight)
4329 QCOMPARE(gridview->contentY(), endExtent);
4331 QCOMPARE(gridview->contentX(), endExtent);
4335 flick(canvas, flickEnd, flickStart, 180);
4336 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4337 } while (flow == QQuickGridView::FlowLeftToRight
4338 ? !gridview->isAtYBeginning()
4339 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4341 if (flow == QQuickGridView::FlowLeftToRight)
4342 QCOMPARE(gridview->contentY(), startExtent);
4344 QCOMPARE(gridview->contentX(), startExtent);
4346 releaseView(canvas);
4349 void tst_QQuickGridView::snapOneRow_data()
4351 QTest::addColumn<QQuickGridView::Flow>("flow");
4352 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4353 QTest::addColumn<int>("highlightRangeMode");
4354 QTest::addColumn<QPoint>("flickStart");
4355 QTest::addColumn<QPoint>("flickEnd");
4356 QTest::addColumn<qreal>("snapAlignment");
4357 QTest::addColumn<qreal>("endExtent");
4358 QTest::addColumn<qreal>("startExtent");
4360 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4361 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4363 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4364 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4366 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4367 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
4369 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4370 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4372 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4373 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4375 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4376 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
4379 void tst_QQuickGridView::snapOneRow()
4381 QFETCH(QQuickGridView::Flow, flow);
4382 QFETCH(Qt::LayoutDirection, layoutDirection);
4383 QFETCH(int, highlightRangeMode);
4384 QFETCH(QPoint, flickStart);
4385 QFETCH(QPoint, flickEnd);
4386 QFETCH(qreal, snapAlignment);
4387 QFETCH(qreal, endExtent);
4388 QFETCH(qreal, startExtent);
4390 QQuickView *canvas = getView();
4392 canvas->setSource(testFileUrl("snapOneRow.qml"));
4394 qApp->processEvents();
4396 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4397 QTRY_VERIFY(gridview != 0);
4399 gridview->setFlow(flow);
4400 gridview->setLayoutDirection(layoutDirection);
4401 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4402 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4404 QQuickItem *contentItem = gridview->contentItem();
4405 QTRY_VERIFY(contentItem != 0);
4407 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
4409 // confirm that a flick hits next row boundary
4410 flick(canvas, flickStart, flickEnd, 180);
4411 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4412 if (flow == QQuickGridView::FlowLeftToRight)
4413 QCOMPARE(gridview->contentY(), snapAlignment);
4415 QCOMPARE(gridview->contentX(), snapAlignment);
4417 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4418 QCOMPARE(gridview->currentIndex(), 2);
4419 QCOMPARE(currentIndexSpy.count(), 1);
4424 flick(canvas, flickStart, flickEnd, 180);
4425 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4426 } while (flow == QQuickGridView::FlowLeftToRight
4427 ? !gridview->isAtYEnd()
4428 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4430 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4431 QCOMPARE(gridview->currentIndex(), 6);
4432 QCOMPARE(currentIndexSpy.count(), 3);
4435 if (flow == QQuickGridView::FlowLeftToRight)
4436 QCOMPARE(gridview->contentY(), endExtent);
4438 QCOMPARE(gridview->contentX(), endExtent);
4442 flick(canvas, flickEnd, flickStart, 180);
4443 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4444 } while (flow == QQuickGridView::FlowLeftToRight
4445 ? !gridview->isAtYBeginning()
4446 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4448 if (flow == QQuickGridView::FlowLeftToRight)
4449 QCOMPARE(gridview->contentY(), startExtent);
4451 QCOMPARE(gridview->contentX(), startExtent);
4453 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4454 QCOMPARE(gridview->currentIndex(), 0);
4455 QCOMPARE(currentIndexSpy.count(), 6);
4458 releaseView(canvas);
4462 void tst_QQuickGridView::unaligned()
4464 QQuickView *canvas = createView();
4468 for (int i = 0; i < 10; i++)
4469 model.addItem("Item" + QString::number(i), "");
4471 QQmlContext *ctxt = canvas->rootContext();
4472 ctxt->setContextProperty("testModel", &model);
4474 canvas->setSource(testFileUrl("unaligned.qml"));
4475 qApp->processEvents();
4477 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
4478 QVERIFY(gridview != 0);
4480 QQuickItem *contentItem = gridview->contentItem();
4481 QVERIFY(contentItem != 0);
4483 for (int i = 0; i < 10; ++i) {
4484 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4485 if (!item) qWarning() << "Item" << i << "not found";
4487 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4488 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4492 for (int i = 10; i < 18; ++i) {
4493 model.addItem("Item" + QString::number(i), "");
4494 QQuickItem *item = 0;
4495 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4496 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4497 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4501 for (int i = 0; i < 10; ++i) {
4502 model.insertItem(i, "Item" + QString::number(i), "");
4503 QQuickItem *item = 0;
4504 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4505 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4506 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4510 model.removeItems(7, 10);
4511 QTRY_COMPARE(model.count(), gridview->count());
4512 for (int i = 0; i < 18; ++i) {
4513 QQuickItem *item = 0;
4514 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4515 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
4516 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
4522 void tst_QQuickGridView::populateTransitions()
4524 QFETCH(bool, staticallyPopulate);
4525 QFETCH(bool, dynamicallyPopulate);
4526 QFETCH(bool, usePopulateTransition);
4528 QPointF transitionFrom(-50, -50);
4529 QPointF transitionVia(100, 100);
4530 QaimModel model_transitionFrom;
4531 QaimModel model_transitionVia;
4534 if (staticallyPopulate) {
4535 for (int i = 0; i < 30; i++)
4536 model.addItem("item" + QString::number(i), "");
4539 QQuickView *canvas = getView();
4540 canvas->rootContext()->setContextProperty("testModel", &model);
4541 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4542 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4543 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4544 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4545 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4546 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4547 canvas->setSource(testFileUrl("populateTransitions.qml"));
4550 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4552 QQuickItem *contentItem = gridview->contentItem();
4553 QVERIFY(contentItem);
4555 // check the populate transition is run
4556 if (staticallyPopulate && usePopulateTransition) {
4557 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 18);
4558 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4559 } else if (dynamicallyPopulate) {
4560 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4561 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 18);
4563 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4564 QCOMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4565 QCOMPARE(gridview->property("countAddTransitions").toInt(), 0);
4568 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4569 for (int i=0; i < model.count() && i < itemCount; ++i) {
4570 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4571 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4572 QTRY_COMPARE(item->x(), (i%3)*80.0);
4573 QTRY_COMPARE(item->y(), (i/3)*60.0);
4574 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4576 QTRY_COMPARE(name->text(), model.name(i));
4579 gridview->setProperty("countPopulateTransitions", 0);
4580 gridview->setProperty("countAddTransitions", 0);
4582 // add an item and check this is done with add transition, not populate
4583 model.insertItem(0, "another item", "");
4584 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
4585 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4588 canvas->rootContext()->setContextProperty("testModel", QVariant());
4589 QTRY_COMPARE(gridview->count(), 0);
4590 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4591 gridview->setProperty("countPopulateTransitions", 0);
4592 gridview->setProperty("countAddTransitions", 0);
4594 // set to a valid model and check populate transition is run a second time
4596 for (int i = 0; i < 30; i++)
4597 model.addItem("item" + QString::number(i), "");
4598 canvas->rootContext()->setContextProperty("testModel", &model);
4599 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4601 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4602 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4604 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4605 for (int i=0; i < model.count() && i < itemCount; ++i) {
4606 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4607 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4608 QTRY_COMPARE(item->x(), (i%3)*80.0);
4609 QTRY_COMPARE(item->y(), (i/3)*60.0);
4610 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4612 QTRY_COMPARE(name->text(), model.name(i));
4615 // reset model and check populate transition is run again
4616 gridview->setProperty("countPopulateTransitions", 0);
4617 gridview->setProperty("countAddTransitions", 0);
4619 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4620 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4622 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4623 for (int i=0; i < model.count() && i < itemCount; ++i) {
4624 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4625 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4626 QTRY_COMPARE(item->x(), (i%3)*80.0);
4627 QTRY_COMPARE(item->y(), (i/3)*60.0);
4628 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4630 QTRY_COMPARE(name->text(), model.name(i));
4633 releaseView(canvas);
4636 void tst_QQuickGridView::populateTransitions_data()
4638 QTest::addColumn<bool>("staticallyPopulate");
4639 QTest::addColumn<bool>("dynamicallyPopulate");
4640 QTest::addColumn<bool>("usePopulateTransition");
4642 QTest::newRow("static") << true << false << true;
4643 QTest::newRow("static, no populate") << true << false << false;
4645 QTest::newRow("dynamic") << false << true << true;
4646 QTest::newRow("dynamic, no populate") << false << true << false;
4648 QTest::newRow("empty to start with") << false << false << true;
4649 QTest::newRow("empty to start with, no populate") << false << false << false;
4652 void tst_QQuickGridView::addTransitions()
4654 QFETCH(int, initialItemCount);
4655 QFETCH(bool, shouldAnimateTargets);
4656 QFETCH(qreal, contentYRowOffset);
4657 QFETCH(int, insertionIndex);
4658 QFETCH(int, insertionCount);
4659 QFETCH(ListRange, expectedDisplacedIndexes);
4661 // added items should start here
4662 QPointF targetItems_transitionFrom(-50, -50);
4664 // displaced items should pass through this point
4665 QPointF displacedItems_transitionVia(100, 100);
4668 for (int i = 0; i < initialItemCount; i++)
4669 model.addItem("Original item" + QString::number(i), "");
4670 QaimModel model_targetItems_transitionFrom;
4671 QaimModel model_displacedItems_transitionVia;
4673 QQuickView *canvas = getView();
4674 QQmlContext *ctxt = canvas->rootContext();
4675 ctxt->setContextProperty("testModel", &model);
4676 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4677 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4678 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4679 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4680 canvas->setSource(testFileUrl("addTransitions.qml"));
4683 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4684 QTRY_VERIFY(gridview != 0);
4685 QQuickItem *contentItem = gridview->contentItem();
4686 QVERIFY(contentItem != 0);
4687 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4689 if (contentYRowOffset != 0) {
4690 gridview->setContentY(contentYRowOffset * 60.0);
4691 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4694 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4696 // only target items that will become visible should be animated
4697 QList<QPair<QString, QString> > newData;
4698 QList<QPair<QString, QString> > expectedTargetData;
4699 QList<int> targetIndexes;
4700 if (shouldAnimateTargets) {
4701 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4702 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4704 // last visible item is the first item of the row beneath the view
4705 if (i >= (gridview->contentY() / 60)*3 && i < qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) {
4706 expectedTargetData << newData.last();
4710 QVERIFY(expectedTargetData.count() > 0);
4714 if (!newData.isEmpty()) {
4715 model.insertItems(insertionIndex, newData);
4716 QTRY_COMPARE(model.count(), gridview->count());
4719 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4721 if (shouldAnimateTargets) {
4722 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4723 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4724 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4726 // check the target and displaced items were animated
4727 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4728 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4730 // check attached properties
4731 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4732 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4733 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4734 if (expectedDisplacedIndexes.isValid()) {
4735 // adjust expectedDisplacedIndexes to their final values after the move
4736 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4737 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4738 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4739 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4742 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4743 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4746 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4747 int firstVisibleIndex = -1;
4748 for (int i=0; i<items.count(); i++) {
4749 if (items[i]->y() >= gridview->contentY()) {
4750 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4751 firstVisibleIndex = e.evaluate().toInt();
4755 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4757 // verify all items moved to the correct final positions
4758 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4759 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4760 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4761 QCOMPARE(item->x(), (i%3)*80.0);
4762 QCOMPARE(item->y(), (i/3)*60.0);
4763 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4765 QCOMPARE(name->text(), model.name(i));
4768 releaseView(canvas);
4771 void tst_QQuickGridView::addTransitions_data()
4773 QTest::addColumn<int>("initialItemCount");
4774 QTest::addColumn<qreal>("contentYRowOffset");
4775 QTest::addColumn<bool>("shouldAnimateTargets");
4776 QTest::addColumn<int>("insertionIndex");
4777 QTest::addColumn<int>("insertionCount");
4778 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4780 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4781 QTest::newRow("insert 1, just before start")
4782 << 30 << 1.0 << false
4783 << 0 << 1 << ListRange();
4784 QTest::newRow("insert 1, way before start")
4785 << 30 << 1.0 << false
4786 << 0 << 1 << ListRange();
4787 QTest::newRow("insert multiple, just before start")
4788 << 30 << 1.0 << false
4789 << 0 << 3 << ListRange();
4790 QTest::newRow("insert multiple (< 1 row), just before start")
4791 << 30 << 1.0 << false
4792 << 0 << 2 << ListRange();
4793 QTest::newRow("insert multiple, way before start")
4794 << 30 << 3.0 << false
4795 << 0 << 3 << ListRange();
4797 QTest::newRow("insert 1 at start")
4798 << 30 << 0.0 << true
4799 << 0 << 1 << ListRange(0, 17);
4800 QTest::newRow("insert multiple at start")
4801 << 30 << 0.0 << true
4802 << 0 << 3 << ListRange(0, 17);
4803 QTest::newRow("insert multiple (> 1 row) at start")
4804 << 30 << 0.0 << true
4805 << 0 << 5 << ListRange(0, 17);
4806 QTest::newRow("insert 1 at start, content y not 0")
4807 << 30 << 1.0 << true // first visible is index 3
4808 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4809 QTest::newRow("insert multiple at start, content y not 0")
4810 << 30 << 1.0 << true // first visible is index 3
4811 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4812 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4813 << 30 << 1.0 << true // first visible is index 3
4814 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4816 QTest::newRow("insert 1 at start, to empty grid")
4818 << 0 << 1 << ListRange();
4819 QTest::newRow("insert multiple at start, to empty grid")
4821 << 0 << 3 << ListRange();
4823 QTest::newRow("insert 1 at middle")
4824 << 30 << 0.0 << true
4825 << 7 << 1 << ListRange(7, 17);
4826 QTest::newRow("insert multiple at middle")
4827 << 30 << 0.0 << true
4828 << 7 << 3 << ListRange(7, 17);
4829 QTest::newRow("insert multiple (> 1 row) at middle")
4830 << 30 << 0.0 << true
4831 << 7 << 5 << ListRange(7, 17);
4833 QTest::newRow("insert 1 at bottom")
4834 << 30 << 0.0 << true
4835 << 17 << 1 << ListRange(17, 17);
4836 QTest::newRow("insert multiple at bottom")
4837 << 30 << 0.0 << true
4838 << 17 << 3 << ListRange(17, 17);
4839 QTest::newRow("insert 1 at bottom, content y not 0")
4840 << 30 << 1.0 << true
4841 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4842 QTest::newRow("insert multiple at bottom, content y not 0")
4843 << 30 << 1.0 << true
4844 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4847 // items added after the last visible will not be animated in, since they
4848 // do not appear in the final view
4849 QTest::newRow("insert 1 after end")
4850 << 30 << 0.0 << false
4851 << 18 << 1 << ListRange();
4852 QTest::newRow("insert multiple after end")
4853 << 30 << 0.0 << false
4854 << 18 << 3 << ListRange();
4857 void tst_QQuickGridView::moveTransitions()
4859 QFETCH(int, initialItemCount);
4860 QFETCH(qreal, contentYRowOffset);
4861 QFETCH(qreal, rowOffsetAfterMove);
4862 QFETCH(int, moveFrom);
4863 QFETCH(int, moveTo);
4864 QFETCH(int, moveCount);
4865 QFETCH(ListRange, expectedDisplacedIndexes);
4867 // target and displaced items should pass through these points
4868 QPointF targetItems_transitionVia(-50, 50);
4869 QPointF displacedItems_transitionVia(100, 100);
4872 for (int i = 0; i < initialItemCount; i++)
4873 model.addItem("Original item" + QString::number(i), "");
4874 QaimModel model_targetItems_transitionVia;
4875 QaimModel model_displacedItems_transitionVia;
4877 QQuickView *canvas = getView();
4878 QQmlContext *ctxt = canvas->rootContext();
4879 ctxt->setContextProperty("testModel", &model);
4880 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4881 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4882 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4883 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4884 canvas->setSource(testFileUrl("moveTransitions.qml"));
4887 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4888 QTRY_VERIFY(gridview != 0);
4889 QQuickItem *contentItem = gridview->contentItem();
4890 QVERIFY(contentItem != 0);
4893 if (contentYRowOffset != 0) {
4894 gridview->setContentY(contentYRowOffset * 60.0);
4895 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4898 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4900 // Items moving to *or* from visible positions should be animated.
4901 // Otherwise, they should not be animated.
4902 QList<QPair<QString, QString> > expectedTargetData;
4903 QList<int> targetIndexes;
4904 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4905 int toIndex = moveTo + (i - moveFrom);
4906 int firstVisibleIndex = (gridview->contentY() / 60) * 3;
4907 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4908 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4909 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4910 expectedTargetData << qMakePair(model.name(i), model.number(i));
4914 // ViewTransition.index provides the indices that items are moving to, not from
4915 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4918 model.moveItems(moveFrom, moveTo, moveCount);
4920 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4921 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4922 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4924 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4926 // check the target and displaced items were animated
4927 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4928 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4930 // check attached properties
4931 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4932 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4933 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4934 if (expectedDisplacedIndexes.isValid()) {
4935 // adjust expectedDisplacedIndexes to their final values after the move
4936 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4937 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4938 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4939 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4942 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4943 int firstVisibleIndex = -1;
4944 for (int i=0; i<items.count(); i++) {
4945 if (items[i]->y() >= gridview->contentY()) {
4946 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4947 firstVisibleIndex = e.evaluate().toInt();
4951 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4953 // verify all items moved to the correct final positions
4954 qreal pixelOffset = 60 * rowOffsetAfterMove;
4955 for (int i=firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4956 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4957 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4958 QCOMPARE(item->x(), (i%3)*80.0);
4959 QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
4960 name = findItem<QQuickText>(contentItem, "textName", i);
4962 QTRY_COMPARE(name->text(), model.name(i));
4965 releaseView(canvas);
4968 void tst_QQuickGridView::moveTransitions_data()
4970 QTest::addColumn<int>("initialItemCount");
4971 QTest::addColumn<qreal>("contentYRowOffset");
4972 QTest::addColumn<qreal>("rowOffsetAfterMove");
4973 QTest::addColumn<int>("moveFrom");
4974 QTest::addColumn<int>("moveTo");
4975 QTest::addColumn<int>("moveCount");
4976 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4978 QTest::newRow("move from above view, outside visible items, move 1")
4980 << 1 << 10 << 1 << ListRange(6, 10);
4981 QTest::newRow("move from above view, outside visible items, move 1 (first item)")
4983 << 0 << 10 << 1 << ListRange(6, 10);
4984 QTest::newRow("move from above view, outside visible items, move multiple")
4986 << 1 << 10 << 3 << ListRange(13, 23);
4987 QTest::newRow("move from above view, mix of visible/non-visible")
4989 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4990 QTest::newRow("move from above view, mix of visible/non-visible (move first)")
4992 << 0 << 10 << 6 << ListRange(16, 23);
4994 QTest::newRow("move within view, move 1 down")
4996 << 1 << 10 << 1 << ListRange(2, 10);
4997 QTest::newRow("move within view, move 1 down, move first item")
4999 << 0 << 10 << 1 << ListRange(1, 10);
5000 QTest::newRow("move within view, move 1 down, move first item, contentY not 0")
5002 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
5003 QTest::newRow("move within view, move 1 down, to last item")
5005 << 10 << 17 << 1 << ListRange(11, 17);
5006 QTest::newRow("move within view, move first->last")
5008 << 0 << 17 << 1 << ListRange(1, 17);
5010 QTest::newRow("move within view, move multiple down")
5012 << 1 << 10 << 3 << ListRange(4, 12);
5013 QTest::newRow("move within view, move multiple down, move first item")
5015 << 0 << 10 << 3 << ListRange(3, 12);
5016 QTest::newRow("move within view, move multiple down, move first item, contentY not 0")
5018 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
5019 QTest::newRow("move within view, move multiple down, displace last item")
5021 << 5 << 15 << 3 << ListRange(8, 17);
5022 QTest::newRow("move within view, move multiple down, move first->last")
5024 << 0 << 15 << 3 << ListRange(3, 17);
5026 QTest::newRow("move within view, move 1 up")
5028 << 10 << 1 << 1 << ListRange(1, 9);
5029 QTest::newRow("move within view, move 1 up, move to first index")
5031 << 10 << 0 << 1 << ListRange(0, 9);
5032 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0")
5034 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
5035 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border")
5037 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
5038 QTest::newRow("move within view, move 1 up, move last item")
5040 << 17 << 10 << 1 << ListRange(10, 16);
5041 QTest::newRow("move within view, move 1 up, move last->first")
5043 << 17 << 0 << 1 << ListRange(0, 16);
5045 QTest::newRow("move within view, move multiple up")
5047 << 10 << 1 << 3 << ListRange(1, 9);
5048 QTest::newRow("move within view, move multiple (> 1 row) up")
5050 << 10 << 1 << 5 << ListRange(1, 9);
5051 QTest::newRow("move within view, move multiple up, move to first index")
5053 << 10 << 0 << 3 << ListRange(0, 9);
5054 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0")
5056 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
5057 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border")
5059 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
5060 QTest::newRow("move within view, move multiple up, move last item")
5062 << 15 << 5 << 3 << ListRange(5, 14);
5063 QTest::newRow("move within view, move multiple up, move last->first")
5065 << 15 << 0 << 3 << ListRange(0, 14);
5067 QTest::newRow("move from below view, move 1 up")
5069 << 20 << 5 << 1 << ListRange(5, 17);
5070 QTest::newRow("move from below view, move 1 up, move to top")
5072 << 20 << 0 << 1 << ListRange(0, 17);
5073 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0")
5075 << 25 << 3 << 1 << ListRange(0+3, 17+3);
5076 QTest::newRow("move from below view, move multiple (> 1 row) up")
5078 << 20 << 5 << 5 << ListRange(5, 17);
5079 QTest::newRow("move from below view, move multiple up, move to top")
5081 << 20 << 0 << 3 << ListRange(0, 17);
5082 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0")
5084 << 25 << 3 << 3 << ListRange(0+3, 17+3);
5086 QTest::newRow("move from below view, move 1 up, move to bottom")
5088 << 20 << 17 << 1 << ListRange(17, 17);
5089 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0")
5091 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
5092 QTest::newRow("move from below view, move multiple up, move to to bottom")
5094 << 20 << 17 << 3 << ListRange(17, 17);
5095 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0")
5097 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
5100 void tst_QQuickGridView::removeTransitions()
5102 QFETCH(int, initialItemCount);
5103 QFETCH(bool, shouldAnimateTargets);
5104 QFETCH(qreal, contentYRowOffset);
5105 QFETCH(int, removalIndex);
5106 QFETCH(int, removalCount);
5107 QFETCH(ListRange, expectedDisplacedIndexes);
5109 // added items should end here
5110 QPointF targetItems_transitionTo(-50, -50);
5112 // displaced items should pass through this points
5113 QPointF displacedItems_transitionVia(100, 100);
5116 for (int i = 0; i < initialItemCount; i++)
5117 model.addItem("Original item" + QString::number(i), "");
5118 QaimModel model_targetItems_transitionTo;
5119 QaimModel model_displacedItems_transitionVia;
5121 QQuickView *canvas = getView();
5122 QQmlContext *ctxt = canvas->rootContext();
5123 ctxt->setContextProperty("testModel", &model);
5124 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5125 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5126 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5127 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5128 canvas->setSource(testFileUrl("removeTransitions.qml"));
5131 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5132 QTRY_VERIFY(gridview != 0);
5133 QQuickItem *contentItem = gridview->contentItem();
5134 QVERIFY(contentItem != 0);
5135 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5137 if (contentYRowOffset != 0) {
5138 gridview->setContentY(contentYRowOffset * 60.0);
5139 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5142 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5144 // only target items that are visible should be animated
5145 QList<QPair<QString, QString> > expectedTargetData;
5146 QList<int> targetIndexes;
5147 if (shouldAnimateTargets) {
5148 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5149 int firstVisibleIndex = (gridview->contentY() / 60.0)*3;
5150 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
5151 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
5152 expectedTargetData << qMakePair(model.name(i), model.number(i));
5156 QVERIFY(expectedTargetData.count() > 0);
5159 // calculate targetItems and expectedTargets before model changes
5160 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5161 QVariantMap expectedTargets;
5162 for (int i=0; i<targetIndexes.count(); i++)
5163 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5166 model.removeItems(removalIndex, removalCount);
5167 QTRY_COMPARE(model.count(), gridview->count());
5169 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
5170 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5171 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
5172 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5174 // check the target and displaced items were animated
5175 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5176 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5178 // check attached properties
5179 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
5180 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5181 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
5182 if (expectedDisplacedIndexes.isValid()) {
5183 // adjust expectedDisplacedIndexes to their final values after the move
5184 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5185 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5186 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5187 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
5190 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5191 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5194 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5195 int itemCount = items.count();
5196 int firstVisibleIndex = -1;
5197 for (int i=0; i<items.count(); i++) {
5198 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5199 int index = e.evaluate().toInt();
5200 if (firstVisibleIndex < 0 && items[i]->y() >= gridview->contentY())
5201 firstVisibleIndex = index;
5203 itemCount--; // exclude deleted items
5205 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5207 // verify all items moved to the correct final positions
5208 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5209 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5210 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5211 QCOMPARE(item->x(), (i%3)*80.0);
5212 QCOMPARE(item->y(), gridview->contentY() + ((i-firstVisibleIndex)/3) * 60.0);
5213 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5215 QTRY_COMPARE(name->text(), model.name(i));
5218 releaseView(canvas);
5221 void tst_QQuickGridView::removeTransitions_data()
5223 QTest::addColumn<int>("initialItemCount");
5224 QTest::addColumn<qreal>("contentYRowOffset");
5225 QTest::addColumn<bool>("shouldAnimateTargets");
5226 QTest::addColumn<int>("removalIndex");
5227 QTest::addColumn<int>("removalCount");
5228 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5230 // All items that are visible following the remove operation should be animated.
5231 // Remove targets that are outside of the view should not be animated.
5233 // For a GridView, removing any number of items other than a full row before the start
5234 // should displace all items in the view
5235 QTest::newRow("remove 1 before start")
5236 << 30 << 2.0 << false
5237 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
5238 QTest::newRow("remove 1 row, before start")
5239 << 30 << 2.0 << false
5240 << 3 << 3 << ListRange();
5241 QTest::newRow("remove between 1-2 rows, before start")
5242 << 30 << 2.0 << false
5243 << 0 << 5 << ListRange(6, 25);
5244 QTest::newRow("remove 2 rows, before start")
5245 << 30 << 2.0 << false
5246 << 0 << 6 << ListRange();
5247 QTest::newRow("remove mix of before and after start")
5248 << 30 << 1.0 << true
5249 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
5252 QTest::newRow("remove 1 from start")
5253 << 30 << 0.0 << true
5254 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
5255 QTest::newRow("remove multiple from start")
5256 << 30 << 0.0 << true
5257 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
5258 QTest::newRow("remove 1 from start, content y not 0")
5259 << 30 << 1.0 << true
5260 << 3 << 1 << ListRange(1 + 3, 18 + 3);
5261 QTest::newRow("remove multiple from start, content y not 0")
5262 << 30 << 1.0 << true
5263 << 3 << 3 << ListRange(3 + 3, 20 + 3);
5266 QTest::newRow("remove 1 from middle")
5267 << 30 << 0.0 << true
5268 << 5 << 1 << ListRange(6, 18);
5269 QTest::newRow("remove multiple from middle")
5270 << 30 << 0.0 << true
5271 << 5 << 3 << ListRange(8, 20);
5274 QTest::newRow("remove 1 from bottom")
5275 << 30 << 0.0 << true
5276 << 17 << 1 << ListRange(18, 18);
5277 QTest::newRow("remove multiple (1 row) from bottom")
5278 << 30 << 0.0 << true
5279 << 15 << 3 << ListRange(18, 20);
5280 QTest::newRow("remove multiple (> 1 row) from bottom")
5281 << 30 << 0.0 << true
5282 << 15 << 5 << ListRange(20, 22);
5283 QTest::newRow("remove 1 from bottom, content y not 0")
5284 << 30 << 1.0 << true
5285 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
5286 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
5287 << 30 << 1.0 << true
5288 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
5291 QTest::newRow("remove 1 after end")
5292 << 30 << 0.0 << false
5293 << 18 << 1 << ListRange();
5294 QTest::newRow("remove multiple after end")
5295 << 30 << 0.0 << false
5296 << 18 << 3 << ListRange();
5299 void tst_QQuickGridView::displacedTransitions()
5301 QFETCH(bool, useDisplaced);
5302 QFETCH(bool, displacedEnabled);
5303 QFETCH(bool, useAddDisplaced);
5304 QFETCH(bool, addDisplacedEnabled);
5305 QFETCH(bool, useMoveDisplaced);
5306 QFETCH(bool, moveDisplacedEnabled);
5307 QFETCH(bool, useRemoveDisplaced);
5308 QFETCH(bool, removeDisplacedEnabled);
5309 QFETCH(ListChange, change);
5310 QFETCH(ListRange, expectedDisplacedIndexes);
5313 for (int i = 0; i < 30; i++)
5314 model.addItem("Original item" + QString::number(i), "");
5315 QaimModel model_displaced_transitionVia;
5316 QaimModel model_addDisplaced_transitionVia;
5317 QaimModel model_moveDisplaced_transitionVia;
5318 QaimModel model_removeDisplaced_transitionVia;
5320 QPointF displaced_transitionVia(-50, -100);
5321 QPointF addDisplaced_transitionVia(-150, 100);
5322 QPointF moveDisplaced_transitionVia(50, -100);
5323 QPointF removeDisplaced_transitionVia(150, 100);
5325 QQuickView *canvas = getView();
5326 QQmlContext *ctxt = canvas->rootContext();
5327 ctxt->setContextProperty("testModel", &model);
5328 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
5329 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
5330 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
5331 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
5332 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
5333 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
5334 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
5335 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
5336 ctxt->setContextProperty("useDisplaced", useDisplaced);
5337 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
5338 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
5339 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
5340 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
5341 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
5342 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
5343 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
5344 canvas->setSource(testFileUrl("displacedTransitions.qml"));
5346 qApp->processEvents();
5348 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5349 QTRY_VERIFY(gridview != 0);
5350 QQuickItem *contentItem = gridview->contentItem();
5351 QVERIFY(contentItem != 0);
5352 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5354 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5355 gridview->setProperty("displaceTransitionsDone", false);
5357 switch (change.type) {
5358 case ListChange::Inserted:
5360 QList<QPair<QString, QString> > targetItemData;
5361 for (int i=change.index; i<change.index + change.count; ++i)
5362 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
5363 model.insertItems(change.index, targetItemData);
5364 QTRY_COMPARE(model.count(), gridview->count());
5367 case ListChange::Removed:
5368 model.removeItems(change.index, change.count);
5369 QTRY_COMPARE(model.count(), gridview->count());
5371 case ListChange::Moved:
5372 model.moveItems(change.index, change.to, change.count);
5373 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5375 case ListChange::SetCurrent:
5376 case ListChange::SetContentY:
5380 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
5381 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
5383 if ((useDisplaced && displacedEnabled)
5384 || (useAddDisplaced && addDisplacedEnabled)
5385 || (useMoveDisplaced && moveDisplacedEnabled)
5386 || (useRemoveDisplaced && removeDisplacedEnabled)) {
5387 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5389 // check the correct number of target items and indexes were received
5390 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
5391 for (int i=0; i<resultTargetIndexes.count(); i++)
5392 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
5393 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
5394 for (int i=0; i<resultTargetItems.count(); i++)
5395 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
5397 QCOMPARE(resultTargetIndexes.count(), 0);
5398 QCOMPARE(resultTargetItems.count(), 0);
5401 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
5402 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
5404 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
5405 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
5406 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
5408 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
5409 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
5410 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
5412 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
5414 if (useDisplaced && displacedEnabled
5415 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
5416 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
5417 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
5418 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
5420 QCOMPARE(model_displaced_transitionVia.count(), 0);
5423 // verify all items moved to the correct final positions
5424 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5425 for (int i=0; i < model.count() && i < items.count(); ++i) {
5426 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5427 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5428 QCOMPARE(item->x(), (i%3)*80.0);
5429 QCOMPARE(item->y(), (i/3)*60.0);
5430 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5432 QTRY_COMPARE(name->text(), model.name(i));
5435 releaseView(canvas);
5438 void tst_QQuickGridView::displacedTransitions_data()
5440 QTest::addColumn<bool>("useDisplaced");
5441 QTest::addColumn<bool>("displacedEnabled");
5442 QTest::addColumn<bool>("useAddDisplaced");
5443 QTest::addColumn<bool>("addDisplacedEnabled");
5444 QTest::addColumn<bool>("useMoveDisplaced");
5445 QTest::addColumn<bool>("moveDisplacedEnabled");
5446 QTest::addColumn<bool>("useRemoveDisplaced");
5447 QTest::addColumn<bool>("removeDisplacedEnabled");
5448 QTest::addColumn<ListChange>("change");
5449 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5451 QTest::newRow("no displaced transitions at all")
5456 << ListChange::insert(0, 1) << ListRange(0, 17);
5458 QTest::newRow("just displaced")
5463 << ListChange::insert(0, 1) << ListRange(0, 17);
5465 QTest::newRow("just displaced (not enabled)")
5470 << ListChange::insert(0, 1) << ListRange(0, 17);
5472 QTest::newRow("displaced + addDisplaced")
5477 << ListChange::insert(0, 1) << ListRange(0, 17);
5479 QTest::newRow("displaced + addDisplaced (not enabled)")
5484 << ListChange::insert(0, 1) << ListRange(0, 17);
5486 QTest::newRow("displaced + moveDisplaced")
5491 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5493 QTest::newRow("displaced + moveDisplaced (not enabled)")
5498 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5500 QTest::newRow("displaced + removeDisplaced")
5505 << ListChange::remove(0, 1) << ListRange(1, 18);
5507 QTest::newRow("displaced + removeDisplaced (not enabled)")
5512 << ListChange::remove(0, 1) << ListRange(1, 18);
5515 QTest::newRow("displaced + add, should use generic displaced for a remove")
5520 << ListChange::remove(0, 1) << ListRange(1, 18);
5523 void tst_QQuickGridView::multipleTransitions()
5525 // Tests that if you interrupt a transition in progress with another action that
5526 // cancels the previous transition, the resulting items are still placed correctly.
5528 QFETCH(int, initialCount);
5529 QFETCH(qreal, contentY);
5530 QFETCH(QList<ListChange>, changes);
5531 QFETCH(bool, enableAddTransitions);
5532 QFETCH(bool, enableMoveTransitions);
5533 QFETCH(bool, enableRemoveTransitions);
5534 QFETCH(bool, rippleAddDisplaced);
5536 // add transitions on the left, moves on the right
5537 QPointF addTargets_transitionFrom(-50, -50);
5538 QPointF addDisplaced_transitionFrom(-50, 50);
5539 QPointF moveTargets_transitionFrom(50, -50);
5540 QPointF moveDisplaced_transitionFrom(50, 50);
5541 QPointF removeTargets_transitionTo(-100, 300);
5542 QPointF removeDisplaced_transitionFrom(100, 300);
5545 for (int i = 0; i < initialCount; i++)
5546 model.addItem("Original item" + QString::number(i), "");
5548 QQuickView *canvas = getView();
5549 QQmlContext *ctxt = canvas->rootContext();
5550 ctxt->setContextProperty("testModel", &model);
5551 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5552 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5553 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5554 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5555 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5556 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5557 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5558 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5559 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5560 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5561 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5563 QTest::qWaitForWindowShown(canvas);
5565 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5566 QTRY_VERIFY(gridview != 0);
5567 QQuickItem *contentItem = gridview->contentItem();
5568 QVERIFY(contentItem != 0);
5569 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5571 if (contentY != 0) {
5572 gridview->setContentY(contentY);
5573 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5576 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5578 for (int i=0; i<changes.count(); i++) {
5579 switch (changes[i].type) {
5580 case ListChange::Inserted:
5582 QList<QPair<QString, QString> > targetItems;
5583 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5584 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5585 model.insertItems(changes[i].index, targetItems);
5586 QTRY_COMPARE(model.count(), gridview->count());
5587 if (i == changes.count() - 1) {
5588 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
5589 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
5591 QTest::qWait(timeBetweenActions);
5595 case ListChange::Removed:
5596 model.removeItems(changes[i].index, changes[i].count);
5597 QTRY_COMPARE(model.count(), gridview->count());
5598 if (i == changes.count() - 1) {
5599 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
5600 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
5602 QTest::qWait(timeBetweenActions);
5605 case ListChange::Moved:
5606 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5607 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5608 if (i == changes.count() - 1) {
5609 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
5610 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
5612 QTest::qWait(timeBetweenActions);
5615 case ListChange::SetCurrent:
5616 gridview->setCurrentIndex(changes[i].index);
5617 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5619 case ListChange::SetContentY:
5620 gridview->setContentY(changes[i].pos);
5621 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5625 QCOMPARE(gridview->count(), model.count());
5627 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5628 int firstVisibleIndex = -1;
5629 for (int i=0; i<items.count(); i++) {
5630 if (items[i]->y() >= contentY) {
5631 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5632 firstVisibleIndex = e.evaluate().toInt();
5636 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5638 // verify all items moved to the correct final positions
5639 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5640 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5641 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5642 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5643 QTRY_COMPARE(item->x(), (i%3)*80.0);
5644 QTRY_COMPARE(item->y(), (i/3)*60.0);
5645 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5647 QTRY_COMPARE(name->text(), model.name(i));
5650 releaseView(canvas);
5653 void tst_QQuickGridView::multipleTransitions_data()
5655 QTest::addColumn<int>("initialCount");
5656 QTest::addColumn<qreal>("contentY");
5657 QTest::addColumn<QList<ListChange> >("changes");
5658 QTest::addColumn<bool>("enableAddTransitions");
5659 QTest::addColumn<bool>("enableMoveTransitions");
5660 QTest::addColumn<bool>("enableRemoveTransitions");
5661 QTest::addColumn<bool>("rippleAddDisplaced");
5663 // the added item and displaced items should move to final dest correctly
5664 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5665 << ListChange::insert(0, 1)
5666 << ListChange::move(0, 3, 1)
5668 << true << true << true << false;
5670 // items affected by the add should change from move to add transition
5671 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5672 << ListChange::move(1, 10, 3)
5673 << ListChange::insert(0, 1)
5675 << true << true << true << false;
5677 // items should be placed correctly if you trigger a transition then refill for that index
5678 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5679 << ListChange::insert(0, 1)
5680 << ListChange::setContentY(160.0)
5681 << ListChange::setContentY(0.0)
5682 << ListChange::insert(0, 1)
5684 << true << true << true << false;
5686 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5687 << ListChange::insert(1, 1)
5688 << ListChange::remove(1, 1)
5690 << true << true << true << true;
5692 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5693 // even if a remove-displace transition is not present to re-animate them
5694 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5695 << ListChange::insert(0, 1)
5696 << ListChange::remove(2, 1)
5698 << true << true << false << false;
5700 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5701 // remove has changed the position of where it will move to
5702 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5703 << ListChange::setContentY(-10.0)
5704 << ListChange::insert(0, 1)
5705 << ListChange::insert(0, 1)
5706 << ListChange::remove(2, 1)
5708 << true << true << false << false;
5711 void tst_QQuickGridView::multipleDisplaced()
5713 // multiple move() operations should only restart displace transitions for items that
5714 // moved from previously set positions, and not those that have moved from their current
5715 // item positions (which may e.g. still be changing from easing bounces in the last transition)
5718 for (int i = 0; i < 30; i++)
5719 model.addItem("Original item" + QString::number(i), "");
5721 QQuickView *canvas = createView();
5722 QQmlContext *ctxt = canvas->rootContext();
5723 ctxt->setContextProperty("testModel", &model);
5724 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5726 QTest::qWaitForWindowShown(canvas);
5728 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5729 QTRY_VERIFY(gridview != 0);
5730 QQuickItem *contentItem = gridview->contentItem();
5731 QVERIFY(contentItem != 0);
5732 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5734 model.moveItems(12, 8, 1);
5735 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5736 model.moveItems(8, 3, 1);
5737 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5739 QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5740 foreach (const QString &name, transitionsStarted.keys()) {
5741 QVERIFY2(transitionsStarted[name] == 1,
5742 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5745 // verify all items moved to the correct final positions
5746 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5747 for (int i=0; i < model.count() && i < items.count(); ++i) {
5748 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5749 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5750 QTRY_COMPARE(item->x(), (i%3)*80.0);
5751 QTRY_COMPARE(item->y(), (i/3)*60.0);
5752 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5754 QTRY_COMPARE(name->text(), model.name(i));
5760 void tst_QQuickGridView::cacheBuffer()
5762 QQuickView *canvas = createView();
5765 for (int i = 0; i < 90; i++)
5766 model.addItem("Item" + QString::number(i), "");
5768 canvas->rootContext()->setContextProperty("testModel", &model);
5769 canvas->setSource(testFileUrl("gridview1.qml"));
5771 qApp->processEvents();
5773 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5774 QVERIFY(gridview != 0);
5776 QQuickItem *contentItem = gridview->contentItem();
5777 QVERIFY(contentItem != 0);
5778 QVERIFY(gridview->delegate() != 0);
5779 QVERIFY(gridview->model() != 0);
5781 // Confirm items positioned correctly
5782 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5783 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5784 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5785 QTRY_COMPARE(item->x(), (i%3)*80.0);
5786 QTRY_COMPARE(item->y(), (i/3)*60.0);
5789 QQmlIncubationController controller;
5790 canvas->engine()->setIncubationController(&controller);
5792 canvas->rootObject()->setProperty("cacheBuffer", 200);
5793 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5795 // items will be created one at a time
5796 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5797 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5798 QQuickItem *item = 0;
5801 controller.incubateWhile(&b);
5802 item = findItem<QQuickItem>(gridview, "wrapper", i);
5808 controller.incubateWhile(&b);
5811 int newItemCount = 0;
5812 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5814 // Confirm items positioned correctly
5815 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5816 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5818 QTRY_COMPARE(item->x(), (i%3)*80.0);
5819 QTRY_COMPARE(item->y(), (i/3)*60.0);
5822 // move view and confirm items in view are visible immediately and outside are created async
5823 gridview->setContentY(300);
5825 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5826 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5828 QTRY_COMPARE(item->x(), (i%3)*80.0);
5829 QTRY_COMPARE(item->y(), (i/3)*60.0);
5832 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5834 // ensure buffered items are created
5835 for (int i = 34; i < qMin(44,model.count()); ++i) {
5836 QQuickItem *item = 0;
5838 qGuiApp->processEvents(); // allow refill to happen
5840 controller.incubateWhile(&b);
5841 item = findItem<QQuickItem>(gridview, "wrapper", i);
5847 controller.incubateWhile(&b);
5853 void tst_QQuickGridView::asynchronous()
5855 QQuickView *canvas = createView();
5857 QQmlIncubationController controller;
5858 canvas->engine()->setIncubationController(&controller);
5860 canvas->setSource(testFileUrl("asyncloader.qml"));
5862 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5863 QVERIFY(rootObject);
5865 QQuickGridView *gridview = 0;
5868 controller.incubateWhile(&b);
5869 gridview = rootObject->findChild<QQuickGridView*>("view");
5872 // items will be created one at a time
5873 for (int i = 0; i < 12; ++i) {
5874 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5875 QQuickItem *item = 0;
5878 controller.incubateWhile(&b);
5879 item = findItem<QQuickItem>(gridview, "wrapper", i);
5885 controller.incubateWhile(&b);
5888 // verify positioning
5889 QQuickItem *contentItem = gridview->contentItem();
5890 for (int i = 0; i < 12; ++i) {
5891 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5892 if (!item) qWarning() << "Item" << i << "not found";
5893 QVERIFY(item->x() == (i%3)*100);
5894 QVERIFY(item->y() == (i/3)*100);
5900 void tst_QQuickGridView::unrequestedVisibility()
5903 for (int i = 0; i < 30; i++)
5904 model.addItem("Item" + QString::number(i), QString::number(i));
5906 QQuickView *canvas = new QQuickView(0);
5907 canvas->setGeometry(0,0,240,320);
5909 QQmlContext *ctxt = canvas->rootContext();
5910 ctxt->setContextProperty("testModel", &model);
5911 ctxt->setContextProperty("testWrap", QVariant(false));
5913 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5917 qApp->processEvents();
5919 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5920 QTRY_VERIFY(leftview != 0);
5922 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5923 QTRY_VERIFY(rightview != 0);
5925 QQuickItem *leftContent = leftview->contentItem();
5926 QTRY_VERIFY(leftContent != 0);
5928 QQuickItem *rightContent = rightview->contentItem();
5929 QTRY_VERIFY(rightContent != 0);
5931 rightview->setCurrentIndex(12);
5933 QTRY_COMPARE(leftview->contentY(), 0.0);
5934 QTRY_COMPARE(rightview->contentY(), 240.0);
5938 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5939 QCOMPARE(item->isVisible(), true);
5940 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5941 QCOMPARE(item->isVisible(), false);
5943 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5944 QCOMPARE(item->isVisible(), false);
5945 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5946 QCOMPARE(item->isVisible(), true);
5948 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5949 QCOMPARE(item->isVisible(), true);
5950 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5951 QCOMPARE(item->isVisible(), false);
5952 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5953 QCOMPARE(item->isVisible(), false);
5954 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5955 QCOMPARE(item->isVisible(), true);
5957 rightview->setCurrentIndex(0);
5959 QTRY_COMPARE(leftview->contentY(), 0.0);
5960 QTRY_COMPARE(rightview->contentY(), 0.0);
5962 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5963 QCOMPARE(item->isVisible(), true);
5964 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5965 QTRY_COMPARE(item->isVisible(), true);
5967 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5968 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5970 leftview->setCurrentIndex(12);
5972 QTRY_COMPARE(leftview->contentY(), 240.0);
5973 QTRY_COMPARE(rightview->contentY(), 0.0);
5975 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5976 QTRY_COMPARE(item->isVisible(), false);
5977 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5978 QCOMPARE(item->isVisible(), true);
5980 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5981 QCOMPARE(item->isVisible(), true);
5982 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5983 QCOMPARE(item->isVisible(), false);
5985 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5986 QCOMPARE(item->isVisible(), false);
5987 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5988 QCOMPARE(item->isVisible(), true);
5989 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5990 QCOMPARE(item->isVisible(), true);
5991 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5992 QCOMPARE(item->isVisible(), false);
5994 // move a non-visible item into view
5995 model.moveItems(10, 9, 1);
5996 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5998 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5999 QCOMPARE(item->isVisible(), false);
6000 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6001 QCOMPARE(item->isVisible(), true);
6003 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
6004 QCOMPARE(item->isVisible(), true);
6005 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
6006 QCOMPARE(item->isVisible(), false);
6008 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6009 QCOMPARE(item->isVisible(), false);
6010 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6011 QCOMPARE(item->isVisible(), true);
6012 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6013 QCOMPARE(item->isVisible(), true);
6014 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6015 QCOMPARE(item->isVisible(), false);
6017 // move a visible item out of view
6018 model.moveItems(5, 3, 1);
6019 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6021 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6022 QCOMPARE(item->isVisible(), false);
6023 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6024 QCOMPARE(item->isVisible(), true);
6025 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6026 QCOMPARE(item->isVisible(), true);
6027 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6028 QCOMPARE(item->isVisible(), false);
6030 // move a non-visible item into view
6031 model.moveItems(3, 5, 1);
6032 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6034 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6035 QCOMPARE(item->isVisible(), false);
6036 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6037 QCOMPARE(item->isVisible(), true);
6038 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6039 QCOMPARE(item->isVisible(), true);
6040 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6041 QCOMPARE(item->isVisible(), false);
6043 // move a visible item out of view
6044 model.moveItems(9, 10, 1);
6045 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6047 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6048 QCOMPARE(item->isVisible(), false);
6049 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6050 QCOMPARE(item->isVisible(), true);
6051 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6052 QCOMPARE(item->isVisible(), true);
6053 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6054 QCOMPARE(item->isVisible(), false);
6056 // move a non-visible item into view
6057 model.moveItems(10, 9, 1);
6058 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6060 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6061 QCOMPARE(item->isVisible(), false);
6062 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6063 QCOMPARE(item->isVisible(), true);
6064 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6065 QCOMPARE(item->isVisible(), true);
6066 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6067 QCOMPARE(item->isVisible(), false);
6073 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB()
6075 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6078 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB_data()
6080 inserted_defaultLayout_data();
6083 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB()
6085 inserted_defaultLayout(QQuickGridView::FlowTopToBottom);
6088 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB_data()
6090 inserted_defaultLayout_data();
6093 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB()
6095 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6098 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB_data()
6100 inserted_defaultLayout_data();
6103 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT()
6105 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6108 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT_data()
6110 inserted_defaultLayout_data();
6113 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT()
6115 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6118 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT_data()
6120 inserted_defaultLayout_data();
6123 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT()
6125 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6128 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT_data()
6130 inserted_defaultLayout_data();
6133 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT()
6135 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6138 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT_data()
6140 inserted_defaultLayout_data();
6144 void tst_QQuickGridView::removed_leftToRight_RtL_TtB()
6146 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6149 void tst_QQuickGridView::removed_leftToRight_RtL_TtB_data()
6151 removed_defaultLayout_data();
6154 void tst_QQuickGridView::removed_topToBottom_LtR_TtB()
6156 removed_defaultLayout(QQuickGridView::FlowTopToBottom);
6159 void tst_QQuickGridView::removed_topToBottom_LtR_TtB_data()
6161 removed_defaultLayout_data();
6164 void tst_QQuickGridView::removed_topToBottom_RtL_TtB()
6166 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6169 void tst_QQuickGridView::removed_topToBottom_RtL_TtB_data()
6171 removed_defaultLayout_data();
6174 void tst_QQuickGridView::removed_leftToRight_LtR_BtT()
6176 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6179 void tst_QQuickGridView::removed_leftToRight_LtR_BtT_data()
6181 removed_defaultLayout_data();
6184 void tst_QQuickGridView::removed_leftToRight_RtL_BtT()
6186 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6189 void tst_QQuickGridView::removed_leftToRight_RtL_BtT_data()
6191 removed_defaultLayout_data();
6194 void tst_QQuickGridView::removed_topToBottom_LtR_BtT()
6196 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6199 void tst_QQuickGridView::removed_topToBottom_LtR_BtT_data()
6201 removed_defaultLayout_data();
6204 void tst_QQuickGridView::removed_topToBottom_RtL_BtT()
6206 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6209 void tst_QQuickGridView::removed_topToBottom_RtL_BtT_data()
6211 removed_defaultLayout_data();
6215 void tst_QQuickGridView::moved_leftToRight_RtL_TtB()
6217 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6220 void tst_QQuickGridView::moved_leftToRight_RtL_TtB_data()
6222 moved_defaultLayout_data();
6225 void tst_QQuickGridView::moved_topToBottom_LtR_TtB()
6227 moved_defaultLayout(QQuickGridView::FlowTopToBottom);
6230 void tst_QQuickGridView::moved_topToBottom_LtR_TtB_data()
6232 moved_defaultLayout_data();
6235 void tst_QQuickGridView::moved_topToBottom_RtL_TtB()
6237 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6240 void tst_QQuickGridView::moved_topToBottom_RtL_TtB_data()
6242 moved_defaultLayout_data();
6245 void tst_QQuickGridView::moved_leftToRight_LtR_BtT()
6247 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6250 void tst_QQuickGridView::moved_leftToRight_LtR_BtT_data()
6252 moved_defaultLayout_data();
6255 void tst_QQuickGridView::moved_leftToRight_RtL_BtT()
6257 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6260 void tst_QQuickGridView::moved_leftToRight_RtL_BtT_data()
6262 moved_defaultLayout_data();
6265 void tst_QQuickGridView::moved_topToBottom_LtR_BtT()
6267 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6270 void tst_QQuickGridView::moved_topToBottom_LtR_BtT_data()
6272 moved_defaultLayout_data();
6275 void tst_QQuickGridView::moved_topToBottom_RtL_BtT()
6277 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6280 void tst_QQuickGridView::moved_topToBottom_RtL_BtT_data()
6282 moved_defaultLayout_data();
6286 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
6290 for (int i=0; i<list.count(); i++) {
6291 ret << list[i].toInt(&ok);
6293 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
6299 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6301 for (int i=0; i<indexLists.count(); i++) {
6302 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6303 if (current != expectedIndexes.toSet())
6304 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6305 QCOMPARE(current, expectedIndexes.toSet());
6309 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6311 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6312 QVERIFY(it.value().type() == QVariant::Int);
6313 QString name = it.key();
6314 int itemIndex = it.value().toInt();
6315 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6316 if (model.name(itemIndex) != name)
6317 qDebug() << itemIndex;
6318 QCOMPARE(model.name(itemIndex), name);
6320 QCOMPARE(items.count(), expectedIndexes.count());
6323 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6325 for (int i=0; i<itemLists.count(); i++) {
6326 QVariantList current = itemLists[i].toList();
6327 for (int j=0; j<current.count(); j++) {
6328 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6329 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6330 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6332 QCOMPARE(current.count(), expectedItems.count());
6336 QTEST_MAIN(tst_QQuickGridView)
6338 #include "tst_qquickgridview.moc"