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>
61 #include "qplatformdefs.h"
63 Q_DECLARE_METATYPE(QQuickGridView::Flow)
64 Q_DECLARE_METATYPE(Qt::LayoutDirection)
65 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
66 Q_DECLARE_METATYPE(Qt::Key)
68 using namespace QQuickViewTestUtil;
69 using namespace QQuickVisualTestUtil;
73 class tst_QQuickGridView : public QQmlDataTest
83 void inserted_basic();
84 void inserted_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
85 void inserted_defaultLayout_data();
86 void insertBeforeVisible();
87 void insertBeforeVisible_data();
89 void removed_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
90 void removed_defaultLayout_data();
91 void addOrRemoveBeforeVisible();
92 void addOrRemoveBeforeVisible_data();
94 void moved_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
95 void moved_defaultLayout_data();
96 void multipleChanges_condensed() { multipleChanges(true); }
97 void multipleChanges_condensed_data() { multipleChanges_data(); }
98 void multipleChanges_uncondensed() { multipleChanges(false); }
99 void multipleChanges_uncondensed_data() { multipleChanges_data(); }
100 void swapWithFirstItem();
103 void noCurrentIndex();
104 void keyNavigation();
105 void keyNavigation_data();
106 void defaultValues();
108 void propertyChanges();
109 void componentChanges();
111 void positionViewAtIndex();
112 void positionViewAtIndex_rightToLeft();
117 void enforceRange_rightToLeft();
119 void manualHighlight();
122 void initialZValues();
127 void resetModel_headerFooter();
128 void resizeViewAndRepaint();
130 void resizeGrid_data();
131 void changeColumnCount();
132 void indexAt_itemAt_data();
133 void indexAt_itemAt();
137 void onRemove_data();
140 void creationContext();
141 void snapToRow_data();
143 void snapOneRow_data();
148 void unrequestedVisibility();
150 void populateTransitions();
151 void populateTransitions_data();
152 void addTransitions();
153 void addTransitions_data();
154 void moveTransitions();
155 void moveTransitions_data();
156 void removeTransitions();
157 void removeTransitions_data();
158 void displacedTransitions();
159 void displacedTransitions_data();
160 void multipleTransitions();
161 void multipleTransitions_data();
162 void multipleDisplaced();
164 void inserted_leftToRight_RtL_TtB();
165 void inserted_leftToRight_RtL_TtB_data();
166 void inserted_leftToRight_LtR_BtT();
167 void inserted_leftToRight_LtR_BtT_data();
168 void inserted_leftToRight_RtL_BtT();
169 void inserted_leftToRight_RtL_BtT_data();
170 void inserted_topToBottom_LtR_TtB();
171 void inserted_topToBottom_LtR_TtB_data();
172 void inserted_topToBottom_RtL_TtB();
173 void inserted_topToBottom_RtL_TtB_data();
174 void inserted_topToBottom_LtR_BtT();
175 void inserted_topToBottom_LtR_BtT_data();
176 void inserted_topToBottom_RtL_BtT();
177 void inserted_topToBottom_RtL_BtT_data();
179 void removed_leftToRight_RtL_TtB();
180 void removed_leftToRight_RtL_TtB_data();
181 void removed_leftToRight_LtR_BtT();
182 void removed_leftToRight_LtR_BtT_data();
183 void removed_leftToRight_RtL_BtT();
184 void removed_leftToRight_RtL_BtT_data();
185 void removed_topToBottom_LtR_TtB();
186 void removed_topToBottom_LtR_TtB_data();
187 void removed_topToBottom_RtL_TtB();
188 void removed_topToBottom_RtL_TtB_data();
189 void removed_topToBottom_LtR_BtT();
190 void removed_topToBottom_LtR_BtT_data();
191 void removed_topToBottom_RtL_BtT();
192 void removed_topToBottom_RtL_BtT_data();
194 void moved_leftToRight_RtL_TtB();
195 void moved_leftToRight_RtL_TtB_data();
196 void moved_leftToRight_LtR_BtT();
197 void moved_leftToRight_LtR_BtT_data();
198 void moved_leftToRight_RtL_BtT();
199 void moved_leftToRight_RtL_BtT_data();
200 void moved_topToBottom_LtR_TtB();
201 void moved_topToBottom_LtR_TtB_data();
202 void moved_topToBottom_RtL_TtB();
203 void moved_topToBottom_RtL_TtB_data();
204 void moved_topToBottom_LtR_BtT();
205 void moved_topToBottom_LtR_BtT_data();
206 void moved_topToBottom_RtL_BtT();
207 void moved_topToBottom_RtL_BtT_data();
210 QList<int> toIntList(const QVariantList &list);
211 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
212 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
213 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
215 void multipleChanges(bool condensed);
216 void multipleChanges_data();
218 QPointF expectedItemPos(QQuickGridView *grid, int index, qreal rowOffset = 0) {
221 if (grid->flow() == QQuickGridView::FlowLeftToRight) {
222 int columns = grid->width() / grid->cellWidth();
223 x = (index % columns) * grid->cellWidth();
224 y = (index / columns) * grid->cellHeight();
225 if (grid->effectiveLayoutDirection() == Qt::RightToLeft) {
226 int col = (index % columns) * grid->cellWidth();
227 x = grid->cellWidth() * (columns - 1) - col;
230 qreal offset = grid->cellHeight() * rowOffset;
231 if (grid->verticalLayoutDirection() == QQuickItemView::TopToBottom)
234 y = -grid->cellHeight() - y - offset;
236 int rows = grid->height() / grid->cellHeight();
237 x = (index / rows) * grid->cellWidth();
238 y = (index % rows) * grid->cellHeight();
239 if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
240 x = -x - grid->cellWidth();
242 qreal offset = grid->cellWidth() * rowOffset;
243 if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
247 if (grid->verticalLayoutDirection() == QQuickItemView::BottomToTop)
248 y = -grid->cellHeight() - y;
250 return QPointF(x, y);
253 // Sets contentY (or contentX in TopToBottom flow) according to given row offset
254 // (in LeftToRight flow) or col offset (in TopToBottom).
255 bool setContentPos(QQuickGridView *gridview, qreal rowOrColOffset) {
256 bool contentPosChanged = (rowOrColOffset != 0);
257 qreal contentOffset = gridview->flow() == QQuickGridView::FlowLeftToRight
258 ? rowOrColOffset * gridview->cellHeight()
259 : rowOrColOffset * gridview->cellWidth();
261 if (gridview->flow() == QQuickGridView::FlowLeftToRight) {
262 if (gridview->verticalLayoutDirection() == QQuickItemView::BottomToTop)
263 contentOffset = -gridview->height() - contentOffset;
265 if (gridview->effectiveLayoutDirection() == Qt::RightToLeft)
266 contentOffset = -gridview->width() - contentOffset;
268 if (gridview->flow() == QQuickGridView::FlowLeftToRight)
269 gridview->setContentY(contentOffset);
271 gridview->setContentX(contentOffset);
272 return contentPosChanged;
276 QQuickView *getView() {
278 if (QString(QTest::currentTestFunction()) != testForView) {
282 m_view->setSource(QUrl());
287 testForView = QTest::currentTestFunction();
288 m_view = createView();
291 void releaseView(QQuickView *view) {
292 Q_ASSERT(view == m_view);
293 m_view->setSource(QUrl());
296 QQuickView *getView() {
299 void releaseView(QQuickView *view) {
308 tst_QQuickGridView::tst_QQuickGridView() : m_view(0)
312 void tst_QQuickGridView::init()
315 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
316 testForView = QString();
323 void tst_QQuickGridView::items()
325 QQuickView *canvas = createView();
328 model.addItem("Fred", "12345");
329 model.addItem("John", "2345");
330 model.addItem("Bob", "54321");
331 model.addItem("Billy", "22345");
332 model.addItem("Sam", "2945");
333 model.addItem("Ben", "04321");
334 model.addItem("Jim", "0780");
336 QQmlContext *ctxt = canvas->rootContext();
337 ctxt->setContextProperty("testModel", &model);
339 canvas->setSource(testFileUrl("gridview1.qml"));
340 qApp->processEvents();
342 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
343 QTRY_VERIFY(gridview != 0);
345 QQuickItem *contentItem = gridview->contentItem();
346 QTRY_VERIFY(contentItem != 0);
348 QTRY_COMPARE(gridview->count(), model.count());
349 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
350 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
352 for (int i = 0; i < model.count(); ++i) {
353 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
354 QTRY_VERIFY(name != 0);
355 QTRY_COMPARE(name->text(), model.name(i));
356 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
357 QTRY_VERIFY(number != 0);
358 QTRY_COMPARE(number->text(), model.number(i));
361 // set an empty model and confirm that items are destroyed
363 ctxt->setContextProperty("testModel", &model2);
365 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
366 QTRY_VERIFY(itemCount == 0);
371 void tst_QQuickGridView::changed()
373 QQuickView *canvas = createView();
376 model.addItem("Fred", "12345");
377 model.addItem("John", "2345");
378 model.addItem("Bob", "54321");
379 model.addItem("Billy", "22345");
380 model.addItem("Sam", "2945");
381 model.addItem("Ben", "04321");
382 model.addItem("Jim", "0780");
384 QQmlContext *ctxt = canvas->rootContext();
385 ctxt->setContextProperty("testModel", &model);
387 canvas->setSource(testFileUrl("gridview1.qml"));
388 qApp->processEvents();
390 QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
391 QTRY_VERIFY(gridview != 0);
393 QQuickItem *contentItem = gridview->contentItem();
394 QTRY_VERIFY(contentItem != 0);
396 model.modifyItem(1, "Will", "9876");
397 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
398 QTRY_VERIFY(name != 0);
399 QTRY_COMPARE(name->text(), model.name(1));
400 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
401 QTRY_VERIFY(number != 0);
402 QTRY_COMPARE(number->text(), model.number(1));
407 void tst_QQuickGridView::inserted_basic()
409 QQuickView *canvas = createView();
413 model.addItem("Fred", "12345");
414 model.addItem("John", "2345");
415 model.addItem("Bob", "54321");
417 canvas->rootContext()->setContextProperty("testModel", &model);
418 canvas->setSource(testFileUrl("gridview1.qml"));
419 qApp->processEvents();
421 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
422 QTRY_VERIFY(gridview != 0);
424 QQuickItem *contentItem = gridview->contentItem();
425 QTRY_VERIFY(contentItem != 0);
427 model.insertItem(1, "Will", "9876");
429 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
430 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
432 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
433 QTRY_VERIFY(name != 0);
434 QTRY_COMPARE(name->text(), model.name(1));
435 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
436 QTRY_VERIFY(number != 0);
437 QTRY_COMPARE(number->text(), model.number(1));
439 // Checks that onAdd is called
440 int added = canvas->rootObject()->property("added").toInt();
441 QTRY_COMPARE(added, 1);
443 // Confirm items positioned correctly
444 for (int i = 0; i < model.count(); ++i) {
445 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
446 QTRY_COMPARE(item->x(), (i%3)*80.0);
447 QTRY_COMPARE(item->y(), (i/3)*60.0);
450 model.insertItem(0, "Foo", "1111"); // zero index, and current item
452 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
454 name = findItem<QQuickText>(contentItem, "textName", 0);
455 QTRY_VERIFY(name != 0);
456 QTRY_COMPARE(name->text(), model.name(0));
457 number = findItem<QQuickText>(contentItem, "textNumber", 0);
458 QTRY_VERIFY(number != 0);
459 QTRY_COMPARE(number->text(), model.number(0));
461 QTRY_COMPARE(gridview->currentIndex(), 1);
463 // Confirm items positioned correctly
464 for (int i = 0; i < model.count(); ++i) {
465 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
466 QTRY_VERIFY(item->x() == (i%3)*80);
467 QTRY_VERIFY(item->y() == (i/3)*60);
470 for (int i = model.count(); i < 30; ++i)
471 model.insertItem(i, "Hello", QString::number(i));
473 gridview->setContentY(120);
475 // Insert item outside visible area
476 model.insertItem(1, "Hello", "1324");
478 QTRY_VERIFY(gridview->contentY() == 120);
483 void tst_QQuickGridView::inserted_defaultLayout(QQuickGridView::Flow flow,
484 Qt::LayoutDirection horizLayout,
485 QQuickItemView::VerticalLayoutDirection verticalLayout)
487 QFETCH(qreal, contentYRowOffset);
488 QFETCH(int, insertIndex);
489 QFETCH(int, insertCount);
490 QFETCH(int, insertIndex_ttb);
491 QFETCH(int, insertCount_ttb);
492 QFETCH(qreal, rowOffsetAfterMove);
495 for (int i = 0; i < 30; i++)
496 model.addItem("Item" + QString::number(i), "");
498 QQuickView *canvas = getView();
499 QQmlContext *ctxt = canvas->rootContext();
500 ctxt->setContextProperty("testModel", &model);
501 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
502 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
503 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
504 canvas->setSource(testFileUrl("layouts.qml"));
506 qApp->processEvents();
508 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
509 QTRY_VERIFY(gridview != 0);
510 QQuickItem *contentItem = gridview->contentItem();
511 QTRY_VERIFY(contentItem != 0);
513 if (flow == QQuickGridView::FlowTopToBottom) {
514 insertIndex = insertIndex_ttb;
515 insertCount = insertCount_ttb;
517 if (setContentPos(gridview, contentYRowOffset))
518 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
520 QList<QPair<QString, QString> > newData;
521 for (int i=0; i<insertCount; i++)
522 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
523 model.insertItems(insertIndex, newData);
524 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
526 // check visibleItems.first() is in correct position
527 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
529 QPointF firstPos(0, 0);
530 if (horizLayout == Qt::RightToLeft)
531 firstPos.rx() = flow == QQuickGridView::FlowLeftToRight ? gridview->width() - gridview->cellWidth() : -gridview->cellWidth();
532 if (verticalLayout == QQuickItemView::BottomToTop)
533 firstPos.ry() -= gridview->cellHeight();
534 QCOMPARE(item0->pos(), firstPos);
536 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
537 int firstVisibleIndex = -1;
538 for (int i=0; i<items.count(); i++) {
539 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
540 if (item && delegateVisible(item)) {
541 firstVisibleIndex = i;
545 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
547 // Confirm items positioned correctly and indexes correct
548 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
549 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
550 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
551 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
552 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
554 QCOMPARE(name->text(), model.name(i));
560 void tst_QQuickGridView::inserted_defaultLayout_data()
562 QTest::addColumn<qreal>("contentYRowOffset");
563 QTest::addColumn<int>("insertIndex");
564 QTest::addColumn<int>("insertCount");
565 QTest::addColumn<int>("insertIndex_ttb");
566 QTest::addColumn<int>("insertCount_ttb");
567 QTest::addColumn<qreal>("rowOffsetAfterMove");
569 QTest::newRow("add 1, before visible items")
573 << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
574 // new 1st visible item is at 0
576 QTest::newRow("add 2, before visible items")
580 << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
582 QTest::newRow("add 3, before visible items")
586 << -1.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
588 QTest::newRow("add 5, before visible items")
592 << -1.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
593 // grid is rearranged and first visible moves forward within its row
595 QTest::newRow("add 6, before visible items")
599 << -1.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
603 QTest::newRow("add 1, at start of visible, content at start")
609 QTest::newRow("add multiple, at start of visible, content at start")
615 QTest::newRow("add 1, at start of visible, content not at start")
621 QTest::newRow("add multiple, at start of visible, content not at start")
628 QTest::newRow("add 1, at end of visible, content at start")
634 QTest::newRow("add row, at end of visible, content at start")
640 QTest::newRow("add 1, at end of visible, content not at start")
646 QTest::newRow("add multiple, at end of visible, content not at start")
653 QTest::newRow("add 1, after visible, content at start")
659 QTest::newRow("add row, after visible, content at start")
665 QTest::newRow("add 1, after visible, content not at start")
671 QTest::newRow("add multiple, after visible, content not at start")
678 void tst_QQuickGridView::insertBeforeVisible()
680 QFETCH(int, insertIndex);
681 QFETCH(int, insertCount);
682 QFETCH(int, cacheBuffer);
685 QQuickView *canvas = getView();
688 for (int i = 0; i < 30; i++)
689 model.addItem("Item" + QString::number(i), "");
691 QQmlContext *ctxt = canvas->rootContext();
692 ctxt->setContextProperty("testModel", &model);
693 canvas->setSource(testFileUrl("gridview1.qml"));
695 qApp->processEvents();
697 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
698 QTRY_VERIFY(gridview != 0);
699 QQuickItem *contentItem = gridview->contentItem();
700 QTRY_VERIFY(contentItem != 0);
702 gridview->setCacheBuffer(cacheBuffer);
703 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
705 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
706 int firstVisibleIndex = 12; // move to an index where the top item is not visible
707 gridview->setContentY(firstVisibleIndex/3 * 60.0);
708 gridview->setCurrentIndex(firstVisibleIndex);
709 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
711 QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
712 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
714 QCOMPARE(item->y(), gridview->contentY());
716 QList<QPair<QString, QString> > newData;
717 for (int i=0; i<insertCount; i++)
718 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
719 model.insertItems(insertIndex, newData);
720 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
722 // now, moving to the top of the view should position the inserted items correctly
723 int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
724 gridview->setCurrentIndex(0);
725 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
726 QTRY_COMPARE(gridview->currentIndex(), 0);
727 QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
729 // Confirm items positioned correctly and indexes correct
730 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
731 for (int i = 0; i < model.count() && i < itemCount; ++i) {
732 item = findItem<QQuickItem>(contentItem, "wrapper", i);
733 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
734 QCOMPARE(item->x(), (i%3)*80.0);
735 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
736 name = findItem<QQuickText>(contentItem, "textName", i);
738 QTRY_COMPARE(name->text(), model.name(i));
744 void tst_QQuickGridView::insertBeforeVisible_data()
746 QTest::addColumn<int>("insertIndex");
747 QTest::addColumn<int>("insertCount");
748 QTest::addColumn<int>("cacheBuffer");
750 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
751 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
752 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
754 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
755 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
756 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
758 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
759 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
760 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
762 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
763 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
764 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
767 void tst_QQuickGridView::removed_basic()
769 QQuickView *canvas = createView();
773 for (int i = 0; i < 40; i++)
774 model.addItem("Item" + QString::number(i), "");
776 canvas->rootContext()->setContextProperty("testModel", &model);
777 canvas->setSource(testFileUrl("gridview1.qml"));
778 qApp->processEvents();
780 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
781 QTRY_VERIFY(gridview != 0);
782 QQuickItem *contentItem = gridview->contentItem();
783 QTRY_VERIFY(contentItem != 0);
784 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
787 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
789 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
790 QTRY_VERIFY(name != 0);
791 QTRY_COMPARE(name->text(), model.name(1));
792 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
793 QTRY_VERIFY(number != 0);
794 QTRY_COMPARE(number->text(), model.number(1));
797 // Checks that onRemove is called
798 QString removed = canvas->rootObject()->property("removed").toString();
799 QTRY_COMPARE(removed, QString("Item1"));
801 // Confirm items positioned correctly
802 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
803 for (int i = 0; i < model.count() && i < itemCount; ++i) {
804 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
805 if (!item) qWarning() << "Item" << i << "not found";
806 QTRY_VERIFY(item->x() == (i%3)*80);
807 QTRY_VERIFY(item->y() == (i/3)*60);
810 // Remove first item (which is the current item);
812 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
814 name = findItem<QQuickText>(contentItem, "textName", 0);
815 QTRY_VERIFY(name != 0);
816 QTRY_COMPARE(name->text(), model.name(0));
817 number = findItem<QQuickText>(contentItem, "textNumber", 0);
818 QTRY_VERIFY(number != 0);
819 QTRY_COMPARE(number->text(), model.number(0));
822 // Confirm items positioned correctly
823 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
824 for (int i = 0; i < model.count() && i < itemCount; ++i) {
825 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
826 if (!item) qWarning() << "Item" << i << "not found";
827 QTRY_VERIFY(item->x() == (i%3)*80);
828 QTRY_VERIFY(item->y() == (i/3)*60);
831 // Remove items not visible
832 model.removeItem(25);
833 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
835 // Confirm items positioned correctly
836 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
837 for (int i = 0; i < model.count() && i < itemCount; ++i) {
838 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
839 if (!item) qWarning() << "Item" << i << "not found";
840 QTRY_VERIFY(item->x() == (i%3)*80);
841 QTRY_VERIFY(item->y() == (i/3)*60);
844 // Remove items before visible
845 gridview->setContentY(120);
846 gridview->setCurrentIndex(10);
848 // Setting currentIndex above shouldn't cause view to scroll
849 QTRY_COMPARE(gridview->contentY(), 120.0);
852 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
854 // Confirm items positioned correctly
855 for (int i = 6; i < 18; ++i) {
856 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
857 if (!item) qWarning() << "Item" << i << "not found";
858 QTRY_VERIFY(item->x() == (i%3)*80);
859 QTRY_VERIFY(item->y() == (i/3)*60);
862 // Remove currentIndex
863 QQuickItem *oldCurrent = gridview->currentItem();
865 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
867 QTRY_COMPARE(gridview->currentIndex(), 9);
868 QTRY_VERIFY(gridview->currentItem() != oldCurrent);
870 gridview->setContentY(0);
871 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
873 // Confirm items positioned correctly
874 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
875 for (int i = 0; i < model.count() && i < itemCount; ++i) {
876 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
877 QTRY_VERIFY(item->x() == (i%3)*80);
878 QTRY_VERIFY(item->y() == (i/3)*60);
881 // remove item outside current view.
882 gridview->setCurrentIndex(32);
883 gridview->setContentY(240);
885 model.removeItem(30);
886 QTRY_VERIFY(gridview->currentIndex() == 31);
888 // remove current item beyond visible items.
889 gridview->setCurrentIndex(20);
890 gridview->setContentY(0);
891 model.removeItem(20);
893 QTRY_COMPARE(gridview->currentIndex(), 20);
894 QTRY_VERIFY(gridview->currentItem() != 0);
896 // remove item before current, but visible
897 gridview->setCurrentIndex(8);
898 gridview->setContentY(240);
899 oldCurrent = gridview->currentItem();
902 QTRY_COMPARE(gridview->currentIndex(), 7);
903 QTRY_VERIFY(gridview->currentItem() == oldCurrent);
908 void tst_QQuickGridView::removed_defaultLayout(QQuickGridView::Flow flow,
909 Qt::LayoutDirection horizLayout,
910 QQuickItemView::VerticalLayoutDirection verticalLayout)
912 QFETCH(qreal, contentYRowOffset);
913 QFETCH(int, removeIndex);
914 QFETCH(int, removeCount);
915 QFETCH(int, removeIndex_ttb);
916 QFETCH(int, removeCount_ttb);
917 QFETCH(qreal, rowOffsetAfterMove);
918 QFETCH(QString, firstVisible);
919 QFETCH(QString, firstVisible_ttb);
921 QQuickView *canvas = getView();
924 for (int i = 0; i < 30; i++)
925 model.addItem("Item" + QString::number(i), "");
927 QQmlContext *ctxt = canvas->rootContext();
928 ctxt->setContextProperty("testModel", &model);
929 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
930 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
931 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
932 canvas->setSource(testFileUrl("layouts.qml"));
934 qApp->processEvents();
936 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
937 QTRY_VERIFY(gridview != 0);
938 QQuickItem *contentItem = gridview->contentItem();
939 QTRY_VERIFY(contentItem != 0);
941 if (flow == QQuickGridView::FlowTopToBottom) {
942 removeIndex = removeIndex_ttb;
943 removeCount = removeCount_ttb;
944 firstVisible = firstVisible_ttb;
946 if (setContentPos(gridview, contentYRowOffset))
947 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
949 model.removeItems(removeIndex, removeCount);
950 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
953 int firstVisibleIndex = -1;
954 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
955 QRectF viewRect(gridview->contentX(), gridview->contentY(), gridview->width(), gridview->height());
956 for (int i=0; i<items.count(); i++) {
957 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
959 QRectF itemRect(item->x(), item->y(), item->width(), item->height());
960 if (delegateVisible(item) && viewRect.intersects(itemRect)) {
961 firstVisibleIndex = i;
962 QQmlExpression en(qmlContext(item), item, "name");
963 firstName = en.evaluate().toString();
968 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
969 QCOMPARE(firstName, firstVisible);
971 // Confirm items positioned correctly and indexes correct
972 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
973 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
974 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
975 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
976 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
978 QTRY_COMPARE(name->text(), model.name(i));
984 void tst_QQuickGridView::removed_defaultLayout_data()
986 QTest::addColumn<qreal>("contentYRowOffset");
987 QTest::addColumn<int>("removeIndex");
988 QTest::addColumn<int>("removeCount");
989 QTest::addColumn<int>("removeIndex_ttb");
990 QTest::addColumn<int>("removeCount_ttb");
991 QTest::addColumn<qreal>("rowOffsetAfterMove");
992 QTest::addColumn<QString>("firstVisible");
993 QTest::addColumn<QString>("firstVisible_ttb");
995 QTest::newRow("remove 1, before visible items")
999 << 0.0 << "Item7" << "Item11";
1001 QTest::newRow("remove 1, before visible position")
1005 << 0.0 << "Item7" << "Item11";
1007 QTest::newRow("remove multiple (1 row), all before visible items")
1011 << 1.0 << "Item6" << "Item10"; // removed top row, slide down by 1 row
1013 QTest::newRow("remove multiple, all before visible items, remove item 0")
1017 << 1.0 << "Item7" << "Item11"; // removed top row, slide down by 1 row
1019 QTest::newRow("remove multiple rows, all before visible items")
1020 << 4.0 // show 12-29
1023 << 2.0 << "Item13" << "Item17";
1025 QTest::newRow("remove one row before visible, content y not on item border")
1029 << 1.0 << "Item3" << "Item5"; // 1 row removed
1031 QTest::newRow("remove mix of visible/non-visible")
1035 << 1.0 << "Item6" << "Item8"; // 1 row removed
1038 // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
1039 // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
1040 QTest::newRow("remove multiple, mix of items from before and within visible items")
1044 << 1.0 << "Item8" << "Item12"; // adjust for the 1 row removed before the visible
1046 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1050 << 1.0 * 2 << "Item8" << "Item12"; // adjust for the 2 rows removed before the visible
1053 QTest::newRow("remove 1, from start of visible, content at start")
1057 << 0.0 << "Item1" << "Item1";
1059 QTest::newRow("remove multiple, from start of visible, content at start")
1063 << 0.0 << "Item3" << "Item5";
1065 QTest::newRow("remove 1, from start of visible, content not at start")
1069 << 0.0 << "Item7" << "Item11";
1071 QTest::newRow("remove multiple, from start of visible, content not at start")
1075 << 0.0 << "Item9" << "Item15";
1078 QTest::newRow("remove 1, from middle of visible, content at start")
1082 << 0.0 << "Item0" << "Item0";
1084 QTest::newRow("remove multiple, from middle of visible, content at start")
1088 << 0.0 << "Item0" << "Item0";
1090 QTest::newRow("remove 1, from middle of visible, content not at start")
1094 << 0.0 << "Item6" << "Item10";
1096 QTest::newRow("remove multiple, from middle of visible, content not at start")
1100 << 0.0 << "Item6" << "Item10";
1103 QTest::newRow("remove 1, after visible, content at start")
1107 << 0.0 << "Item0" << "Item0";
1109 QTest::newRow("remove multiple, after visible, content at start")
1113 << 0.0 << "Item0" << "Item0";
1115 QTest::newRow("remove 1, after visible, content not at start")
1119 << 0.0 << "Item6" << "Item10";
1121 QTest::newRow("remove multiple, after visible, content not at start")
1125 << 0.0 << "Item6" << "Item10";
1127 QTest::newRow("remove multiple, mix of items from within and after visible items")
1131 << 0.0 << "Item6" << "Item10";
1134 void tst_QQuickGridView::addOrRemoveBeforeVisible()
1136 // QTBUG-21588: ensure re-layout is done on grid after adding or removing
1137 // items from before the visible area
1139 QFETCH(bool, doAdd);
1140 QFETCH(qreal, newTopContentY);
1142 QQuickView *canvas = getView();
1146 for (int i = 0; i < 30; i++)
1147 model.addItem("Item" + QString::number(i), "");
1149 canvas->rootContext()->setContextProperty("testModel", &model);
1150 canvas->setSource(testFileUrl("gridview1.qml"));
1152 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1153 QTRY_VERIFY(gridview != 0);
1154 QQuickItem *contentItem = gridview->contentItem();
1155 QTRY_VERIFY(contentItem != 0);
1157 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1158 QTRY_COMPARE(name->text(), QString("Item0"));
1160 gridview->setCurrentIndex(0);
1161 qApp->processEvents();
1162 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1164 // scroll down until item 0 is no longer drawn
1165 // (bug not triggered if we just move using content y, since that doesn't
1166 // refill and change the visible items)
1167 gridview->setCurrentIndex(24);
1168 qApp->processEvents();
1170 QTRY_COMPARE(gridview->currentIndex(), 24);
1171 QTRY_COMPARE(gridview->contentY(), 220.0);
1173 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1174 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible
1177 model.insertItem(0, "New Item", "New Item number");
1178 QTRY_COMPARE(gridview->count(), 31);
1180 model.removeItem(0);
1181 QTRY_COMPARE(gridview->count(), 29);
1184 // scroll back up and item 0 should be gone
1185 gridview->setCurrentIndex(0);
1186 qApp->processEvents();
1187 QTRY_COMPARE(gridview->currentIndex(), 0);
1188 QTRY_COMPARE(gridview->contentY(), newTopContentY);
1190 name = findItem<QQuickText>(contentItem, "textName", 0);
1192 QCOMPARE(name->text(), QString("New Item"));
1194 QCOMPARE(name->text(), QString("Item1"));
1196 // Confirm items positioned correctly
1197 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1198 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1199 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
1200 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1201 QTRY_VERIFY(item->x() == (i%3)*80);
1202 QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1205 releaseView(canvas);
1208 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1210 QTest::addColumn<bool>("doAdd");
1211 QTest::addColumn<qreal>("newTopContentY");
1213 QTest::newRow("add") << true << -60.0;
1214 QTest::newRow("remove") << false << -60.0;
1217 void tst_QQuickGridView::clear()
1219 QQuickView *canvas = createView();
1222 for (int i = 0; i < 30; i++)
1223 model.addItem("Item" + QString::number(i), "");
1225 canvas->rootContext()->setContextProperty("testModel", &model);
1226 canvas->setSource(testFileUrl("gridview1.qml"));
1228 qApp->processEvents();
1230 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1231 QVERIFY(gridview != 0);
1232 QQuickItem *contentItem = gridview->contentItem();
1233 QVERIFY(contentItem != 0);
1234 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1238 QVERIFY(gridview->count() == 0);
1239 QVERIFY(gridview->currentItem() == 0);
1240 QVERIFY(gridview->contentY() == 0);
1241 QVERIFY(gridview->currentIndex() == -1);
1242 QCOMPARE(gridview->contentHeight(), 0.0);
1244 // confirm sanity when adding an item to cleared list
1245 model.addItem("New", "1");
1246 QTRY_COMPARE(gridview->count(), 1);
1247 QVERIFY(gridview->currentItem() != 0);
1248 QVERIFY(gridview->currentIndex() == 0);
1253 void tst_QQuickGridView::moved_defaultLayout(QQuickGridView::Flow flow,
1254 Qt::LayoutDirection horizLayout,
1255 QQuickItemView::VerticalLayoutDirection verticalLayout)
1257 QFETCH(qreal, contentYRowOffset);
1261 QFETCH(int, from_ttb);
1262 QFETCH(int, to_ttb);
1263 QFETCH(int, count_ttb);
1264 QFETCH(qreal, rowOffsetAfterMove);
1266 QQuickView *canvas = getView();
1269 for (int i = 0; i < 30; i++)
1270 model.addItem("Item" + QString::number(i), "");
1272 QQmlContext *ctxt = canvas->rootContext();
1273 ctxt->setContextProperty("testModel", &model);
1274 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
1275 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
1276 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
1277 canvas->setSource(testFileUrl("layouts.qml"));
1279 qApp->processEvents();
1281 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1282 QTRY_VERIFY(gridview != 0);
1283 QQuickItem *contentItem = gridview->contentItem();
1284 QTRY_VERIFY(contentItem != 0);
1285 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1287 QQuickItem *currentItem = gridview->currentItem();
1288 QTRY_VERIFY(currentItem != 0);
1290 if (flow == QQuickGridView::FlowTopToBottom) {
1295 if (setContentPos(gridview, contentYRowOffset))
1296 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1298 model.moveItems(from, to, count);
1299 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1301 // Confirm items positioned correctly and indexes correct
1302 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1303 int firstVisibleIndex = -1;
1304 for (int i=0; i<items.count(); i++) {
1305 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1306 if (item && delegateVisible(item)) {
1307 firstVisibleIndex = i;
1311 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1313 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1314 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1316 ( (flow == QQuickGridView::FlowLeftToRight && i >= firstVisibleIndex + (3*6))
1317 || (flow == QQuickGridView::FlowTopToBottom && i >= firstVisibleIndex + (5*3)) ) ) {
1318 continue; // index has moved out of view
1320 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1321 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
1322 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1324 QTRY_COMPARE(name->text(), model.name(i));
1326 // current index should have been updated
1327 if (item == currentItem)
1328 QTRY_COMPARE(gridview->currentIndex(), i);
1331 releaseView(canvas);
1334 void tst_QQuickGridView::moved_defaultLayout_data()
1336 QTest::addColumn<qreal>("contentYRowOffset");
1337 QTest::addColumn<int>("from");
1338 QTest::addColumn<int>("to");
1339 QTest::addColumn<int>("count");
1340 QTest::addColumn<int>("from_ttb");
1341 QTest::addColumn<int>("to_ttb");
1342 QTest::addColumn<int>("count_ttb");
1343 QTest::addColumn<qreal>("rowOffsetAfterMove");
1345 // model starts with 30 items, each 80x60, in area 240x320
1346 // 18 items should be visible at a time
1348 // The first visible item should not move upwards and out of the view
1349 // if items are moved/removed before it.
1352 QTest::newRow("move 1 forwards, within visible items")
1358 QTest::newRow("move 1 forwards, from non-visible -> visible")
1364 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1365 << 2.0 // // show 6-23
1370 QTest::newRow("move 1 forwards, from visible -> non-visible")
1376 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1383 QTest::newRow("move 1 backwards, within visible items")
1389 QTest::newRow("move 1 backwards, within visible items (to first index)")
1395 QTest::newRow("move 1 backwards, from non-visible -> visible")
1401 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1407 QTest::newRow("move 1 backwards, from visible -> non-visible")
1411 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1413 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1417 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1420 QTest::newRow("move multiple forwards, within visible items")
1426 QTest::newRow("move multiple backwards, within visible items (move first item)")
1432 QTest::newRow("move multiple forwards, before visible items")
1434 << 3 << 4 << 3 // 3, 4, 5 move to after 6
1436 << 1.0; // row of 3,4,5 has moved down
1438 QTest::newRow("move multiple forwards, from non-visible -> visible")
1442 << 1.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1444 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1448 << 1.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1450 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1454 << 1.0; // top two rows removed, third row is now the first visible
1456 QTest::newRow("move multiple forwards, to bottom of view")
1462 QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1468 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1474 QTest::newRow("move multiple forwards, from visible -> non-visible")
1480 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1487 QTest::newRow("move multiple backwards, within visible items")
1493 QTest::newRow("move multiple backwards, from non-visible -> visible")
1499 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1505 QTest::newRow("move multiple backwards, from visible -> non-visible")
1509 << -1.0; // to minimize movement, items are added above visible area, all items move up by 1 row
1511 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1515 << -1.0; // 16,17,18 move to above item 0, all items move up by 1 row
1518 void tst_QQuickGridView::multipleChanges(bool condensed)
1520 QFETCH(int, startCount);
1521 QFETCH(QList<ListChange>, changes);
1522 QFETCH(int, newCount);
1523 QFETCH(int, newCurrentIndex);
1525 QQuickView *canvas = getView();
1528 for (int i = 0; i < startCount; i++)
1529 model.addItem("Item" + QString::number(i), "");
1531 canvas->rootContext()->setContextProperty("testModel", &model);
1532 canvas->setSource(testFileUrl("gridview1.qml"));
1534 qApp->processEvents();
1536 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1537 QTRY_VERIFY(gridview != 0);
1538 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1540 for (int i=0; i<changes.count(); i++) {
1541 switch (changes[i].type) {
1542 case ListChange::Inserted:
1544 QList<QPair<QString, QString> > items;
1545 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1546 items << qMakePair(QString("new item " + j), QString::number(j));
1547 model.insertItems(changes[i].index, items);
1550 case ListChange::Removed:
1551 model.removeItems(changes[i].index, changes[i].count);
1553 case ListChange::Moved:
1554 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1556 case ListChange::SetCurrent:
1557 gridview->setCurrentIndex(changes[i].index);
1559 case ListChange::SetContentY:
1560 gridview->setContentY(changes[i].pos);
1562 case ListChange::Polish:
1566 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1569 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1571 QCOMPARE(gridview->count(), newCount);
1572 QCOMPARE(gridview->count(), model.count());
1573 QCOMPARE(gridview->currentIndex(), newCurrentIndex);
1577 QQuickItem *contentItem = gridview->contentItem();
1578 QTRY_VERIFY(contentItem != 0);
1579 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1580 for (int i=0; i < model.count() && i < itemCount; ++i) {
1581 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1582 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1583 name = findItem<QQuickText>(contentItem, "textName", i);
1585 QTRY_COMPARE(name->text(), model.name(i));
1586 number = findItem<QQuickText>(contentItem, "textNumber", i);
1587 QVERIFY(number != 0);
1588 QTRY_COMPARE(number->text(), model.number(i));
1591 releaseView(canvas);
1594 void tst_QQuickGridView::multipleChanges_data()
1596 QTest::addColumn<int>("startCount");
1597 QTest::addColumn<QList<ListChange> >("changes");
1598 QTest::addColumn<int>("newCount");
1599 QTest::addColumn<int>("newCurrentIndex");
1601 QList<ListChange> changes;
1603 for (int i=1; i<30; i++)
1604 changes << ListChange::remove(0);
1605 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1607 changes << ListChange::remove(0);
1608 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1611 changes << ListChange::setCurrent(29);
1612 for (int i=29; i>0; i--)
1613 changes << ListChange::remove(i);
1614 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1616 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1617 << ListChange::remove(0, 1)
1618 << ListChange::insert(0, 1)
1621 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1622 << ListChange::setCurrent(2)
1623 << ListChange::remove(2, 1)
1624 << ListChange::insert(2, 1)
1627 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1628 << ListChange::setCurrent(1)
1629 << ListChange::remove(1, 3)
1630 << ListChange::insert(2, 2)
1633 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1634 << ListChange::setCurrent(2)
1635 << ListChange::remove(1, 3)
1636 << ListChange::move(1, 5, 1)
1639 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1640 << ListChange::setCurrent(5)
1641 << ListChange::remove(4, 3)
1642 << ListChange::move(4, 1, 1)
1646 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1647 << ListChange::insert(0, 2)
1648 << ListChange::insert(0, 4)
1649 << ListChange::insert(0, 6)
1652 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1653 << ListChange::insert(0, 2)
1654 << ListChange::insert(0, 4)
1655 << ListChange::insert(0, 6)
1656 << ListChange::setCurrent(3)
1657 << ListChange::insert(3, 2)
1660 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1661 << ListChange::insert(0, 30)
1662 << ListChange::remove(0, 30)
1665 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1666 << ListChange::insert(1)
1667 << ListChange::setCurrent(1)
1668 << ListChange::remove(1)
1671 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1672 << ListChange::insert(0, 10)
1673 << ListChange::remove(5, 10)
1676 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1677 << ListChange::insert(0, 3)
1678 << ListChange::move(0, 10, 3)
1681 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1682 << ListChange::insert(0, 3)
1683 << ListChange::move(0, 8, 5)
1686 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1687 << ListChange::setCurrent(9)
1688 << ListChange::insert(10, 3)
1689 << ListChange::move(8, 0, 5)
1693 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1694 << ListChange::setCurrent(1)
1695 << ListChange::move(1, 2, 2)
1696 << ListChange::move(2, 1, 2)
1699 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1700 << ListChange::setCurrent(2)
1701 << ListChange::move(1, 2, 3)
1702 << ListChange::move(3, 0, 5)
1705 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1706 << ListChange::setCurrent(5)
1707 << ListChange::move(5, 0, 1)
1708 << ListChange::remove(0)
1711 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1712 << ListChange::setCurrent(5)
1713 << ListChange::move(5, 0, 1)
1714 << ListChange::insert(0)
1717 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1718 << ListChange::setCurrent(1)
1719 << ListChange::move(5, 1, 3)
1720 << ListChange::remove(1, 3)
1723 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1724 << ListChange::setCurrent(5)
1725 << ListChange::move(5, 1, 3)
1726 << ListChange::insert(1, 5)
1729 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1730 << ListChange::setCurrent(3)
1731 << ListChange::move(0, 1, 2)
1732 << ListChange::insert(3, 5)
1736 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1737 << ListChange::insert(0, 5)
1738 << ListChange::setCurrent(-1)
1739 << ListChange::remove(0, 5)
1740 << ListChange::insert(0, 5)
1743 QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1744 << ListChange::remove(20, 5)
1745 << ListChange::setContentY(20)
1748 QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1749 << ListChange::insert(9, 5)
1750 << ListChange::setContentY(20)
1753 QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1754 << ListChange::move(15, 8, 3)
1755 << ListChange::setContentY(0)
1758 QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1759 << ListChange::setContentY(20)
1760 << ListChange::remove(0, 30)
1761 << ListChange::insert(0, 2)
1762 << ListChange::setContentY(0)
1767 void tst_QQuickGridView::swapWithFirstItem()
1770 QQuickView *canvas = createView();
1773 for (int i = 0; i < 30; i++)
1774 model.addItem("Item" + QString::number(i), "");
1776 canvas->rootContext()->setContextProperty("testModel", &model);
1777 canvas->setSource(testFileUrl("gridview1.qml"));
1779 qApp->processEvents();
1781 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1782 QTRY_VERIFY(gridview != 0);
1784 // ensure content position is stable
1785 gridview->setContentY(0);
1786 model.moveItem(10, 0);
1787 QTRY_VERIFY(gridview->contentY() == 0);
1792 void tst_QQuickGridView::currentIndex()
1795 for (int i = 0; i < 60; i++)
1796 model.addItem("Item" + QString::number(i), QString::number(i));
1798 QQuickView *canvas = new QQuickView(0);
1799 canvas->setGeometry(0,0,240,320);
1802 QQmlContext *ctxt = canvas->rootContext();
1803 ctxt->setContextProperty("testModel", &model);
1805 QString filename(testFile("gridview-initCurrent.qml"));
1806 canvas->setSource(QUrl::fromLocalFile(filename));
1808 qApp->processEvents();
1810 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1811 QVERIFY(gridview != 0);
1812 QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1814 QQuickItem *contentItem = gridview->contentItem();
1815 QVERIFY(contentItem != 0);
1817 // current item should be third item
1818 QCOMPARE(gridview->currentIndex(), 35);
1819 QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1820 QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1821 QCOMPARE(gridview->contentY(), 400.0);
1823 gridview->setCurrentIndex(0);
1824 QCOMPARE(gridview->currentIndex(), 0);
1825 // confirm that the velocity is updated
1826 QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1828 // footer should become visible if it is out of view, and then current index moves to the first row
1829 canvas->rootObject()->setProperty("showFooter", true);
1830 QTRY_VERIFY(gridview->footerItem());
1831 gridview->setCurrentIndex(model.count()-3);
1832 QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1833 gridview->setCurrentIndex(model.count()-2);
1834 QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1835 canvas->rootObject()->setProperty("showFooter", false);
1837 // header should become visible if it is out of view, and then current index moves to the last row
1838 canvas->rootObject()->setProperty("showHeader", true);
1839 QTRY_VERIFY(gridview->headerItem());
1840 gridview->setCurrentIndex(3);
1841 QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1842 gridview->setCurrentIndex(1);
1843 QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1844 canvas->rootObject()->setProperty("showHeader", false);
1846 // turn off auto highlight
1847 gridview->setHighlightFollowsCurrentItem(false);
1848 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1849 QVERIFY(gridview->highlightItem());
1850 qreal hlPosX = gridview->highlightItem()->x();
1851 qreal hlPosY = gridview->highlightItem()->y();
1853 gridview->setCurrentIndex(5);
1854 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1855 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1857 // insert item before currentIndex
1858 gridview->setCurrentIndex(28);
1859 model.insertItem(0, "Foo", "1111");
1860 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1862 // check removing highlight by setting currentIndex to -1;
1863 gridview->setCurrentIndex(-1);
1865 QCOMPARE(gridview->currentIndex(), -1);
1866 QVERIFY(!gridview->highlightItem());
1867 QVERIFY(!gridview->currentItem());
1869 // moving currentItem out of view should make it invisible
1870 gridview->setCurrentIndex(0);
1871 QTRY_VERIFY(delegateVisible(gridview->currentItem()));
1872 gridview->setContentY(200);
1873 QTRY_VERIFY(!delegateVisible(gridview->currentItem()));
1878 void tst_QQuickGridView::noCurrentIndex()
1881 for (int i = 0; i < 60; i++)
1882 model.addItem("Item" + QString::number(i), QString::number(i));
1884 QQuickView *canvas = new QQuickView(0);
1885 canvas->setGeometry(0,0,240,320);
1887 QQmlContext *ctxt = canvas->rootContext();
1888 ctxt->setContextProperty("testModel", &model);
1890 QString filename(testFile("gridview-noCurrent.qml"));
1891 canvas->setSource(QUrl::fromLocalFile(filename));
1893 qApp->processEvents();
1895 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1896 QVERIFY(gridview != 0);
1897 QQuickItem *contentItem = gridview->contentItem();
1898 QVERIFY(contentItem != 0);
1899 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1901 // current index should be -1
1902 QCOMPARE(gridview->currentIndex(), -1);
1903 QVERIFY(!gridview->currentItem());
1904 QVERIFY(!gridview->highlightItem());
1905 QCOMPARE(gridview->contentY(), 0.0);
1907 gridview->setCurrentIndex(5);
1908 QCOMPARE(gridview->currentIndex(), 5);
1909 QVERIFY(gridview->currentItem());
1910 QVERIFY(gridview->highlightItem());
1915 void tst_QQuickGridView::keyNavigation()
1917 QFETCH(QQuickGridView::Flow, flow);
1918 QFETCH(Qt::LayoutDirection, layoutDirection);
1919 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
1920 QFETCH(Qt::Key, forwardsKey);
1921 QFETCH(Qt::Key, backwardsKey);
1922 QFETCH(QPointF, contentPosAtFirstItem);
1923 QFETCH(QPointF, contentPosAtLastItem);
1924 QFETCH(int, indexRightOf7);
1925 QFETCH(int, indexLeftOf7);
1926 QFETCH(int, indexUpFrom7);
1927 QFETCH(int, indexDownFrom7);
1930 for (int i = 0; i < 18; i++)
1931 model.addItem("Item" + QString::number(i), "");
1933 QQuickView *canvas = getView();
1934 canvas->rootContext()->setContextProperty("testModel", &model);
1935 canvas->setSource(testFileUrl("gridview1.qml"));
1937 qApp->processEvents();
1939 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1940 QTRY_VERIFY(gridview != 0);
1941 gridview->setFlow(flow);
1942 gridview->setLayoutDirection(layoutDirection);
1943 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
1944 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1946 canvas->requestActivateWindow();
1947 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1948 QCOMPARE(gridview->currentIndex(), 0);
1950 QTest::keyClick(canvas, forwardsKey);
1951 QCOMPARE(gridview->currentIndex(), 1);
1953 QTest::keyClick(canvas, backwardsKey);
1954 QCOMPARE(gridview->currentIndex(), 0);
1956 gridview->setCurrentIndex(7);
1957 gridview->moveCurrentIndexRight();
1958 QCOMPARE(gridview->currentIndex(), indexRightOf7);
1959 gridview->moveCurrentIndexLeft();
1960 QCOMPARE(gridview->currentIndex(), 7);
1961 gridview->moveCurrentIndexLeft();
1962 QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1963 gridview->moveCurrentIndexRight();
1964 QCOMPARE(gridview->currentIndex(), 7);
1965 gridview->moveCurrentIndexUp();
1966 QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1967 gridview->moveCurrentIndexDown();
1968 QCOMPARE(gridview->currentIndex(), 7);
1969 gridview->moveCurrentIndexDown();
1970 QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1972 gridview->setCurrentIndex(7);
1973 QTest::keyClick(canvas, Qt::Key_Right);
1974 QCOMPARE(gridview->currentIndex(), indexRightOf7);
1975 QTest::keyClick(canvas, Qt::Key_Left);
1976 QCOMPARE(gridview->currentIndex(), 7);
1977 QTest::keyClick(canvas, Qt::Key_Left);
1978 QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1979 QTest::keyClick(canvas, Qt::Key_Right);
1980 QCOMPARE(gridview->currentIndex(), 7);
1981 QTest::keyClick(canvas, Qt::Key_Up);
1982 QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1983 QTest::keyClick(canvas, Qt::Key_Down);
1984 QCOMPARE(gridview->currentIndex(), 7);
1985 QTest::keyClick(canvas, Qt::Key_Down);
1986 QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1988 // hold down a key to go forwards
1989 gridview->setCurrentIndex(0);
1990 for (int i=0; i<model.count()-1; i++) {
1991 // QTest::qWait(500);
1992 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
1993 QTRY_COMPARE(gridview->currentIndex(), i+1);
1995 QTest::keyRelease(canvas, forwardsKey);
1996 QTRY_COMPARE(gridview->currentIndex(), model.count()-1);
1997 QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
1998 QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
2000 // hold down a key to go backwards
2001 for (int i=model.count()-1; i > 0; i--) {
2002 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2003 QTRY_COMPARE(gridview->currentIndex(), i-1);
2005 QTest::keyRelease(canvas, backwardsKey);
2006 QTRY_COMPARE(gridview->currentIndex(), 0);
2007 QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2008 QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2011 QVERIFY(!gridview->isWrapEnabled());
2012 QTest::keyClick(canvas, forwardsKey);
2013 QCOMPARE(gridview->currentIndex(), 1);
2014 QTest::keyClick(canvas, backwardsKey);
2015 QCOMPARE(gridview->currentIndex(), 0);
2017 QTest::keyClick(canvas, backwardsKey);
2018 QCOMPARE(gridview->currentIndex(), 0);
2021 gridview->setWrapEnabled(true);
2022 QVERIFY(gridview->isWrapEnabled());
2024 QTest::keyClick(canvas, backwardsKey);
2025 QCOMPARE(gridview->currentIndex(), model.count()-1);
2026 QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
2027 QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
2029 QTest::keyClick(canvas, forwardsKey);
2030 QCOMPARE(gridview->currentIndex(), 0);
2031 QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2032 QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2034 // Test key press still accepted when position wraps to same index.
2035 canvas->rootObject()->setProperty("lastKey", 0);
2036 model.removeItems(1, model.count() - 1);
2038 QTest::keyClick(canvas, backwardsKey);
2039 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), 0);
2041 QTest::keyClick(canvas, forwardsKey);
2042 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), 0);
2044 // Test key press not accepted at limits when wrap is disabled.
2045 gridview->setWrapEnabled(false);
2047 QTest::keyClick(canvas, backwardsKey);
2048 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), int(backwardsKey));
2050 QTest::keyClick(canvas, forwardsKey);
2051 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), int(forwardsKey));
2053 releaseView(canvas);
2056 void tst_QQuickGridView::keyNavigation_data()
2058 QTest::addColumn<QQuickGridView::Flow>("flow");
2059 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2060 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2061 QTest::addColumn<Qt::Key>("forwardsKey");
2062 QTest::addColumn<Qt::Key>("backwardsKey");
2063 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2064 QTest::addColumn<QPointF>("contentPosAtLastItem");
2065 QTest::addColumn<int>("indexRightOf7");
2066 QTest::addColumn<int>("indexLeftOf7");
2067 QTest::addColumn<int>("indexUpFrom7");
2068 QTest::addColumn<int>("indexDownFrom7");
2070 QTest::newRow("LeftToRight, LtR, TtB")
2071 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
2072 << Qt::Key_Right << Qt::Key_Left
2075 << 8 << 6 << 4 << 10;
2077 QTest::newRow("LeftToRight, RtL, TtB")
2078 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
2079 << Qt::Key_Left << Qt::Key_Right
2082 << 6 << 8 << 4 << 10;
2084 QTest::newRow("LeftToRight, LtR, BtT")
2085 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
2086 << Qt::Key_Right << Qt::Key_Left
2089 << 8 << 6 << 10 << 4;
2091 QTest::newRow("LeftToRight, RtL, BtT")
2092 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
2093 << Qt::Key_Left << Qt::Key_Right
2096 << 6 << 8 << 10 << 4;
2098 QTest::newRow("TopToBottom, LtR, TtB")
2099 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
2100 << Qt::Key_Down << Qt::Key_Up
2103 << 12 << 2 << 6 << 8;
2105 QTest::newRow("TopToBottom, RtL, TtB")
2106 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
2107 << Qt::Key_Down << Qt::Key_Up
2110 << 2 << 12 << 6 << 8;
2112 QTest::newRow("TopToBottom, LtR, BtT")
2113 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
2114 << Qt::Key_Up << Qt::Key_Down
2116 << QPointF(80, -320)
2117 << 12 << 2 << 8 << 6;
2119 QTest::newRow("TopToBottom, RtL, BtT")
2120 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
2121 << Qt::Key_Up << Qt::Key_Down
2122 << QPointF(-240, -320)
2123 << QPointF(-320, -320)
2124 << 2 << 12 << 8 << 6;
2127 void tst_QQuickGridView::changeFlow()
2129 QQuickView *canvas = createView();
2132 for (int i = 0; i < 30; i++)
2133 model.addItem("Item" + QString::number(i), QString::number(i));
2135 QQmlContext *ctxt = canvas->rootContext();
2136 ctxt->setContextProperty("testModel", &model);
2137 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2138 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2139 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2141 canvas->setSource(testFileUrl("layouts.qml"));
2142 qApp->processEvents();
2144 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2145 QTRY_VERIFY(gridview != 0);
2147 QQuickItem *contentItem = gridview->contentItem();
2148 QTRY_VERIFY(contentItem != 0);
2150 // Confirm items positioned correctly and indexes correct
2151 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2152 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2153 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2154 if (!item) qWarning() << "Item" << i << "not found";
2156 QTRY_COMPARE(item->x(), qreal((i%3)*80));
2157 QTRY_COMPARE(item->y(), qreal((i/3)*60));
2158 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2159 QTRY_VERIFY(name != 0);
2160 QTRY_COMPARE(name->text(), model.name(i));
2161 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2162 QTRY_VERIFY(number != 0);
2163 QTRY_COMPARE(number->text(), model.number(i));
2166 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2168 // Confirm items positioned correctly and indexes correct
2169 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2170 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2171 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2172 if (!item) qWarning() << "Item" << i << "not found";
2174 QTRY_COMPARE(item->x(), qreal((i/5)*80));
2175 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2176 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2177 QTRY_VERIFY(name != 0);
2178 QTRY_COMPARE(name->text(), model.name(i));
2179 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2180 QTRY_VERIFY(number != 0);
2181 QTRY_COMPARE(number->text(), model.number(i));
2184 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2186 // Confirm items positioned correctly and indexes correct
2187 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2188 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2189 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2190 if (!item) qWarning() << "Item" << i << "not found";
2192 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
2193 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2194 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2195 QTRY_VERIFY(name != 0);
2196 QTRY_COMPARE(name->text(), model.name(i));
2197 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2198 QTRY_VERIFY(number != 0);
2199 QTRY_COMPARE(number->text(), model.number(i));
2201 gridview->setContentX(100);
2202 QTRY_COMPARE(gridview->contentX(), 100.);
2203 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2204 QTRY_COMPARE(gridview->contentX(), 0.);
2206 // Confirm items positioned correctly and indexes correct
2207 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2208 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2209 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2210 if (!item) qWarning() << "Item" << i << "not found";
2212 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
2213 QTRY_COMPARE(item->y(), qreal((i/3)*60));
2214 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2215 QTRY_VERIFY(name != 0);
2216 QTRY_COMPARE(name->text(), model.name(i));
2217 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2218 QTRY_VERIFY(number != 0);
2219 QTRY_COMPARE(number->text(), model.number(i));
2225 void tst_QQuickGridView::defaultValues()
2228 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
2229 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2231 QTRY_VERIFY(obj != 0);
2232 QTRY_VERIFY(obj->model() == QVariant());
2233 QTRY_VERIFY(obj->delegate() == 0);
2234 QTRY_COMPARE(obj->currentIndex(), -1);
2235 QTRY_VERIFY(obj->currentItem() == 0);
2236 QTRY_COMPARE(obj->count(), 0);
2237 QTRY_VERIFY(obj->highlight() == 0);
2238 QTRY_VERIFY(obj->highlightItem() == 0);
2239 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
2240 QTRY_VERIFY(obj->flow() == 0);
2241 QTRY_COMPARE(obj->isWrapEnabled(), false);
2242 #ifdef QML_VIEW_DEFAULTCACHEBUFFER
2243 QTRY_COMPARE(obj->cacheBuffer(), QML_VIEW_DEFAULTCACHEBUFFER);
2245 QTRY_COMPARE(obj->cacheBuffer(), 320);
2247 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
2248 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2252 void tst_QQuickGridView::properties()
2255 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
2256 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2258 QTRY_VERIFY(obj != 0);
2259 QTRY_VERIFY(obj->model() != QVariant());
2260 QTRY_VERIFY(obj->delegate() != 0);
2261 QTRY_COMPARE(obj->currentIndex(), 0);
2262 QTRY_VERIFY(obj->currentItem() != 0);
2263 QTRY_COMPARE(obj->count(), 4);
2264 QTRY_VERIFY(obj->highlight() != 0);
2265 QTRY_VERIFY(obj->highlightItem() != 0);
2266 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
2267 QTRY_VERIFY(obj->flow() == 0);
2268 QTRY_COMPARE(obj->isWrapEnabled(), true);
2269 QTRY_COMPARE(obj->cacheBuffer(), 200);
2270 QTRY_COMPARE(obj->cellWidth(), qreal(100));
2271 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2275 void tst_QQuickGridView::propertyChanges()
2277 QQuickView *canvas = createView();
2278 QTRY_VERIFY(canvas);
2279 canvas->setSource(testFileUrl("propertychangestest.qml"));
2281 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2282 QTRY_VERIFY(gridView);
2284 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
2285 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
2286 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
2287 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
2289 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2290 QTRY_COMPARE(gridView->cacheBuffer(), 10);
2291 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2293 gridView->setWrapEnabled(false);
2294 gridView->setCacheBuffer(3);
2295 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2297 QTRY_COMPARE(gridView->isWrapEnabled(), false);
2298 QTRY_COMPARE(gridView->cacheBuffer(), 3);
2299 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2301 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2302 QTRY_COMPARE(cacheBufferSpy.count(),1);
2303 QTRY_COMPARE(flowSpy.count(),1);
2305 gridView->setWrapEnabled(false);
2306 gridView->setCacheBuffer(3);
2307 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2309 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2310 QTRY_COMPARE(cacheBufferSpy.count(),1);
2311 QTRY_COMPARE(flowSpy.count(),1);
2313 gridView->setFlow(QQuickGridView::FlowLeftToRight);
2314 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2316 gridView->setWrapEnabled(true);
2317 gridView->setCacheBuffer(5);
2318 gridView->setLayoutDirection(Qt::RightToLeft);
2320 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2321 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2322 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2324 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2325 QTRY_COMPARE(cacheBufferSpy.count(),2);
2326 QTRY_COMPARE(layoutSpy.count(),1);
2327 QTRY_COMPARE(flowSpy.count(),2);
2329 gridView->setWrapEnabled(true);
2330 gridView->setCacheBuffer(5);
2331 gridView->setLayoutDirection(Qt::RightToLeft);
2333 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2334 QTRY_COMPARE(cacheBufferSpy.count(),2);
2335 QTRY_COMPARE(layoutSpy.count(),1);
2336 QTRY_COMPARE(flowSpy.count(),2);
2338 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2339 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2340 QTRY_COMPARE(flowSpy.count(),3);
2342 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2343 QTRY_COMPARE(flowSpy.count(),3);
2348 void tst_QQuickGridView::componentChanges()
2350 QQuickView *canvas = createView();
2351 QTRY_VERIFY(canvas);
2352 canvas->setSource(testFileUrl("propertychangestest.qml"));
2354 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2355 QTRY_VERIFY(gridView);
2357 QQmlComponent component(canvas->engine());
2358 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2360 QQmlComponent delegateComponent(canvas->engine());
2361 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2363 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2364 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2365 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2366 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2367 QSignalSpy headerItemSpy(gridView, SIGNAL(headerItemChanged()));
2368 QSignalSpy footerItemSpy(gridView, SIGNAL(footerItemChanged()));
2370 gridView->setHighlight(&component);
2371 gridView->setDelegate(&delegateComponent);
2372 gridView->setHeader(&component);
2373 gridView->setFooter(&component);
2375 QTRY_COMPARE(gridView->highlight(), &component);
2376 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2377 QTRY_COMPARE(gridView->header(), &component);
2378 QTRY_COMPARE(gridView->footer(), &component);
2380 QVERIFY(gridView->headerItem());
2381 QVERIFY(gridView->footerItem());
2383 QTRY_COMPARE(highlightSpy.count(),1);
2384 QTRY_COMPARE(delegateSpy.count(),1);
2385 QTRY_COMPARE(headerSpy.count(),1);
2386 QTRY_COMPARE(footerSpy.count(),1);
2387 QTRY_COMPARE(headerItemSpy.count(),1);
2388 QTRY_COMPARE(footerItemSpy.count(),1);
2390 gridView->setHighlight(&component);
2391 gridView->setDelegate(&delegateComponent);
2392 gridView->setHeader(&component);
2393 gridView->setFooter(&component);
2395 QTRY_COMPARE(highlightSpy.count(),1);
2396 QTRY_COMPARE(delegateSpy.count(),1);
2397 QTRY_COMPARE(headerSpy.count(),1);
2398 QTRY_COMPARE(footerSpy.count(),1);
2399 QTRY_COMPARE(headerItemSpy.count(),1);
2400 QTRY_COMPARE(footerItemSpy.count(),1);
2405 void tst_QQuickGridView::modelChanges()
2407 QQuickView *canvas = createView();
2408 QTRY_VERIFY(canvas);
2409 canvas->setSource(testFileUrl("propertychangestest.qml"));
2411 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2412 QTRY_VERIFY(gridView);
2414 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2415 QTRY_VERIFY(alternateModel);
2416 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2417 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2419 gridView->setModel(modelVariant);
2420 QTRY_COMPARE(gridView->model(), modelVariant);
2421 QTRY_COMPARE(modelSpy.count(),1);
2423 gridView->setModel(modelVariant);
2424 QTRY_COMPARE(modelSpy.count(),1);
2426 gridView->setModel(QVariant());
2427 QTRY_COMPARE(modelSpy.count(),2);
2431 void tst_QQuickGridView::positionViewAtIndex()
2433 QQuickView *canvas = createView();
2436 for (int i = 0; i < 40; i++)
2437 model.addItem("Item" + QString::number(i), "");
2439 QQmlContext *ctxt = canvas->rootContext();
2440 ctxt->setContextProperty("testModel", &model);
2441 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2442 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2443 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2445 canvas->setSource(testFileUrl("layouts.qml"));
2447 qApp->processEvents();
2449 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2450 QTRY_VERIFY(gridview != 0);
2451 QQuickItem *contentItem = gridview->contentItem();
2452 QTRY_VERIFY(contentItem != 0);
2453 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2455 // Confirm items positioned correctly
2456 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2457 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2458 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2459 if (!item) qWarning() << "Item" << i << "not found";
2461 QTRY_COMPARE(item->x(), (i%3)*80.);
2462 QTRY_COMPARE(item->y(), (i/3)*60.);
2465 // Position on a currently visible item
2466 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2467 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2468 QTRY_COMPARE(gridview->contentY(), 60.);
2470 // Confirm items positioned correctly
2471 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2472 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2473 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2474 if (!item) qWarning() << "Item" << i << "not found";
2476 QTRY_COMPARE(item->x(), (i%3)*80.);
2477 QTRY_COMPARE(item->y(), (i/3)*60.);
2480 // Position on an item beyond the visible items
2481 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2482 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2483 QTRY_COMPARE(gridview->contentY(), 420.);
2485 // Confirm items positioned correctly
2486 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2487 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2488 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2489 if (!item) qWarning() << "Item" << i << "not found";
2491 QTRY_COMPARE(item->x(), (i%3)*80.);
2492 QTRY_COMPARE(item->y(), (i/3)*60.);
2495 // Position on an item that would leave empty space if positioned at the top
2496 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2497 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2498 QTRY_COMPARE(gridview->contentY(), 520.);
2500 // Confirm items positioned correctly
2501 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2502 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2503 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2504 if (!item) qWarning() << "Item" << i << "not found";
2506 QTRY_COMPARE(item->x(), (i%3)*80.);
2507 QTRY_COMPARE(item->y(), (i/3)*60.);
2510 // Position at the beginning again
2511 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2512 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2513 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2514 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2515 QTRY_COMPARE(gridview->contentY(), 0.);
2517 // Confirm items positioned correctly
2518 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2519 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2520 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2521 if (!item) qWarning() << "Item" << i << "not found";
2523 QTRY_COMPARE(item->x(), (i%3)*80.);
2524 QTRY_COMPARE(item->y(), (i/3)*60.);
2528 gridview->positionViewAtIndex(30, QQuickGridView::End);
2529 QTRY_COMPARE(gridview->contentY(), 340.);
2531 // Position in Center
2532 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2533 QTRY_COMPARE(gridview->contentY(), 170.);
2535 // Ensure at least partially visible
2536 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2537 QTRY_COMPARE(gridview->contentY(), 170.);
2539 gridview->setContentY(302);
2540 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2541 QTRY_COMPARE(gridview->contentY(), 302.);
2543 gridview->setContentY(360);
2544 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2545 QTRY_COMPARE(gridview->contentY(), 300.);
2547 gridview->setContentY(60);
2548 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2549 QTRY_COMPARE(gridview->contentY(), 60.);
2551 gridview->setContentY(20);
2552 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2553 QTRY_COMPARE(gridview->contentY(), 100.);
2555 // Ensure completely visible
2556 gridview->setContentY(120);
2557 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2558 QTRY_COMPARE(gridview->contentY(), 120.);
2560 gridview->setContentY(302);
2561 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2562 QTRY_COMPARE(gridview->contentY(), 300.);
2564 gridview->setContentY(60);
2565 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2566 QTRY_COMPARE(gridview->contentY(), 100.);
2568 // Test for Top To Bottom layout
2569 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2571 // Confirm items positioned correctly
2572 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2573 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2574 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2575 if (!item) qWarning() << "Item" << i << "not found";
2577 QTRY_COMPARE(item->x(), (i/5)*80.);
2578 QTRY_COMPARE(item->y(), (i%5)*60.);
2582 gridview->positionViewAtIndex(30, QQuickGridView::End);
2583 QTRY_COMPARE(gridview->contentX(), 320.);
2584 QTRY_COMPARE(gridview->contentY(), 0.);
2586 // Position in Center
2587 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2588 QTRY_COMPARE(gridview->contentX(), 160.);
2590 // Ensure at least partially visible
2591 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2592 QTRY_COMPARE(gridview->contentX(), 160.);
2594 gridview->setContentX(170);
2595 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2596 QTRY_COMPARE(gridview->contentX(), 170.);
2598 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2599 QTRY_COMPARE(gridview->contentX(), 320.);
2601 gridview->setContentX(170);
2602 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2603 QTRY_COMPARE(gridview->contentX(), 240.);
2605 // positionViewAtBeginning
2606 gridview->positionViewAtBeginning();
2607 QTRY_COMPARE(gridview->contentX(), 0.);
2609 gridview->setContentX(80);
2610 canvas->rootObject()->setProperty("showHeader", true);
2611 gridview->positionViewAtBeginning();
2612 QTRY_COMPARE(gridview->contentX(), -30.);
2614 // positionViewAtEnd
2615 gridview->positionViewAtEnd();
2616 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2618 gridview->setContentX(80);
2619 canvas->rootObject()->setProperty("showFooter", true);
2620 gridview->positionViewAtEnd();
2621 QTRY_COMPARE(gridview->contentX(), 430.);
2623 // set current item to outside visible view, position at beginning
2624 // and ensure highlight moves to current item
2625 gridview->setCurrentIndex(6);
2626 gridview->positionViewAtBeginning();
2627 QTRY_COMPARE(gridview->contentX(), -30.);
2628 QVERIFY(gridview->highlightItem());
2629 QCOMPARE(gridview->highlightItem()->x(), 80.);
2634 void tst_QQuickGridView::snapping()
2636 QQuickView *canvas = createView();
2639 for (int i = 0; i < 40; i++)
2640 model.addItem("Item" + QString::number(i), "");
2642 canvas->rootContext()->setContextProperty("testModel", &model);
2643 canvas->setSource(testFileUrl("gridview1.qml"));
2644 qApp->processEvents();
2646 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2647 QTRY_VERIFY(gridview != 0);
2649 gridview->setHeight(220);
2650 QCOMPARE(gridview->height(), 220.);
2652 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2653 QCOMPARE(gridview->contentY(), 80.);
2655 gridview->setContentY(0);
2656 QCOMPARE(gridview->contentY(), 0.);
2658 gridview->setSnapMode(QQuickGridView::SnapToRow);
2659 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2661 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2662 QCOMPARE(gridview->contentY(), 60.);
2664 gridview->positionViewAtIndex(15, QQuickGridView::End);
2665 QCOMPARE(gridview->contentY(), 120.);
2671 void tst_QQuickGridView::mirroring()
2673 QQuickView *canvasA = createView();
2674 canvasA->setSource(testFileUrl("mirroring.qml"));
2675 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2676 QTRY_VERIFY(gridviewA != 0);
2678 QQuickView *canvasB = createView();
2679 canvasB->setSource(testFileUrl("mirroring.qml"));
2680 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2681 QTRY_VERIFY(gridviewA != 0);
2682 qApp->processEvents();
2684 QList<QString> objectNames;
2685 objectNames << "item1" << "item2"; // << "item3"
2687 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2688 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2689 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2692 foreach (const QString objectName, objectNames)
2693 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2695 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2696 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2699 foreach (const QString objectName, objectNames)
2700 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2702 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2703 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2704 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2706 // LTR != LTR+mirror
2707 foreach (const QString objectName, objectNames)
2708 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2710 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2712 // RTL == LTR+mirror
2713 foreach (const QString objectName, objectNames)
2714 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2716 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2718 // RTL != RTL+mirror
2719 foreach (const QString objectName, objectNames)
2720 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2722 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2724 // LTR == RTL+mirror
2725 foreach (const QString objectName, objectNames)
2726 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2732 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2734 QQuickView *canvas = createView();
2737 for (int i = 0; i < 40; i++)
2738 model.addItem("Item" + QString::number(i), "");
2740 QQmlContext *ctxt = canvas->rootContext();
2741 ctxt->setContextProperty("testModel", &model);
2742 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2743 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2744 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2746 canvas->setSource(testFileUrl("layouts.qml"));
2747 qApp->processEvents();
2749 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2750 QTRY_VERIFY(gridview != 0);
2752 QQuickItem *contentItem = gridview->contentItem();
2753 QTRY_VERIFY(contentItem != 0);
2755 // Confirm items positioned correctly
2756 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2757 for (int i = 0; i < model.count() && i < itemCount-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 a currently visible item
2766 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2767 QTRY_COMPARE(gridview->contentX(), -320.);
2769 // Confirm items positioned correctly
2770 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2771 for (int i = 3; i < model.count() && i < itemCount-3-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 on an item beyond the visible items
2780 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2781 QTRY_COMPARE(gridview->contentX(), -560.);
2783 // Confirm items positioned correctly
2784 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2785 for (int i = 22; i < model.count() && i < itemCount-22-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));
2793 // Position on an item that would leave empty space if positioned at the top
2794 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2795 QTRY_COMPARE(gridview->contentX(), -640.);
2797 // Confirm items positioned correctly
2798 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2799 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2800 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2801 if (!item) qWarning() << "Item" << i << "not found";
2803 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2804 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2807 // Position at the beginning again
2808 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2809 QTRY_COMPARE(gridview->contentX(), -240.);
2811 // Confirm items positioned correctly
2812 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2813 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2814 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2815 if (!item) qWarning() << "Item" << i << "not found";
2817 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2818 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2822 gridview->positionViewAtIndex(30, QQuickGridView::End);
2823 QTRY_COMPARE(gridview->contentX(), -560.);
2825 // Position in Center
2826 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2827 QTRY_COMPARE(gridview->contentX(), -400.);
2829 // Ensure at least partially visible
2830 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2831 QTRY_COMPARE(gridview->contentX(), -400.);
2833 gridview->setContentX(-555.);
2834 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2835 QTRY_COMPARE(gridview->contentX(), -555.);
2837 gridview->setContentX(-239);
2838 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2839 QTRY_COMPARE(gridview->contentX(), -320.);
2841 gridview->setContentX(-239);
2842 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2843 QTRY_COMPARE(gridview->contentX(), -400.);
2845 gridview->setContentX(-640);
2846 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2847 QTRY_COMPARE(gridview->contentX(), -560.);
2849 // Ensure completely visible
2850 gridview->setContentX(-400);
2851 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2852 QTRY_COMPARE(gridview->contentX(), -400.);
2854 gridview->setContentX(-315);
2855 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2856 QTRY_COMPARE(gridview->contentX(), -320.);
2858 gridview->setContentX(-640);
2859 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2860 QTRY_COMPARE(gridview->contentX(), -560.);
2865 void tst_QQuickGridView::resetModel()
2867 QQuickView *canvas = createView();
2869 QStringList strings;
2870 strings << "one" << "two" << "three";
2871 QStringListModel model(strings);
2873 QQmlContext *ctxt = canvas->rootContext();
2874 ctxt->setContextProperty("testModel", &model);
2876 canvas->setSource(testFileUrl("displaygrid.qml"));
2878 qApp->processEvents();
2880 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2881 QTRY_VERIFY(gridview != 0);
2882 QQuickItem *contentItem = gridview->contentItem();
2883 QTRY_VERIFY(contentItem != 0);
2884 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2886 QTRY_COMPARE(gridview->count(), model.rowCount());
2888 for (int i = 0; i < model.rowCount(); ++i) {
2889 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2890 QTRY_VERIFY(display != 0);
2891 QTRY_COMPARE(display->text(), strings.at(i));
2895 strings << "four" << "five" << "six" << "seven";
2896 model.setStringList(strings);
2898 QTRY_COMPARE(gridview->count(), model.rowCount());
2900 for (int i = 0; i < model.rowCount(); ++i) {
2901 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2902 QTRY_VERIFY(display != 0);
2903 QTRY_COMPARE(display->text(), strings.at(i));
2909 void tst_QQuickGridView::enforceRange()
2911 QQuickView *canvas = createView();
2914 for (int i = 0; i < 30; i++)
2915 model.addItem("Item" + QString::number(i), "");
2917 QQmlContext *ctxt = canvas->rootContext();
2918 ctxt->setContextProperty("testModel", &model);
2919 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2920 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2922 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2924 qApp->processEvents();
2925 QVERIFY(canvas->rootObject() != 0);
2927 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2928 QTRY_VERIFY(gridview != 0);
2930 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2931 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2932 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2933 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2935 QQuickItem *contentItem = gridview->contentItem();
2936 QTRY_VERIFY(contentItem != 0);
2938 // view should be positioned at the top of the range.
2939 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2941 QTRY_COMPARE(gridview->contentY(), -100.0);
2943 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2944 QTRY_VERIFY(name != 0);
2945 QTRY_COMPARE(name->text(), model.name(0));
2946 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2947 QTRY_VERIFY(number != 0);
2948 QTRY_COMPARE(number->text(), model.number(0));
2950 // Check currentIndex is updated when contentItem moves
2951 gridview->setContentY(0);
2952 QTRY_COMPARE(gridview->currentIndex(), 2);
2954 gridview->setCurrentIndex(5);
2955 QTRY_COMPARE(gridview->contentY(), 100.);
2958 for (int i = 0; i < 5; i++)
2959 model2.addItem("Item" + QString::number(i), "");
2961 ctxt->setContextProperty("testModel", &model2);
2962 QCOMPARE(gridview->count(), 5);
2967 void tst_QQuickGridView::enforceRange_rightToLeft()
2969 QQuickView *canvas = createView();
2972 for (int i = 0; i < 30; i++)
2973 model.addItem("Item" + QString::number(i), "");
2975 QQmlContext *ctxt = canvas->rootContext();
2976 ctxt->setContextProperty("testModel", &model);
2977 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2978 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2980 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2981 qApp->processEvents();
2982 QVERIFY(canvas->rootObject() != 0);
2984 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2985 QTRY_VERIFY(gridview != 0);
2987 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2988 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2989 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2991 QQuickItem *contentItem = gridview->contentItem();
2992 QVERIFY(contentItem != 0);
2994 // view should be positioned at the top of the range.
2995 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2997 QTRY_COMPARE(gridview->contentX(), -140.);
2998 QTRY_COMPARE(gridview->contentY(), 0.0);
3000 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
3001 QTRY_VERIFY(name != 0);
3002 QTRY_COMPARE(name->text(), model.name(0));
3003 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
3004 QTRY_VERIFY(number != 0);
3005 QTRY_COMPARE(number->text(), model.number(0));
3007 // Check currentIndex is updated when contentItem moves
3008 gridview->setContentX(-240);
3009 QTRY_COMPARE(gridview->currentIndex(), 3);
3011 gridview->setCurrentIndex(7);
3012 QTRY_COMPARE(gridview->contentX(), -340.);
3013 QTRY_COMPARE(gridview->contentY(), 0.0);
3016 for (int i = 0; i < 5; i++)
3017 model2.addItem("Item" + QString::number(i), "");
3019 ctxt->setContextProperty("testModel", &model2);
3020 QCOMPARE(gridview->count(), 5);
3025 void tst_QQuickGridView::QTBUG_8456()
3027 QQuickView *canvas = createView();
3029 canvas->setSource(testFileUrl("setindex.qml"));
3030 qApp->processEvents();
3032 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3033 QTRY_VERIFY(gridview != 0);
3035 QTRY_COMPARE(gridview->currentIndex(), 0);
3040 void tst_QQuickGridView::manualHighlight()
3042 QQuickView *canvas = createView();
3044 QString filename(testFile("manual-highlight.qml"));
3045 canvas->setSource(QUrl::fromLocalFile(filename));
3047 qApp->processEvents();
3049 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3050 QTRY_VERIFY(gridview != 0);
3052 QQuickItem *contentItem = gridview->contentItem();
3053 QTRY_VERIFY(contentItem != 0);
3055 QTRY_COMPARE(gridview->currentIndex(), 0);
3056 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3057 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3058 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3060 gridview->setCurrentIndex(2);
3062 QTRY_COMPARE(gridview->currentIndex(), 2);
3063 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3064 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3065 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3067 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
3069 QTRY_COMPARE(gridview->currentIndex(), 2);
3070 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3071 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3072 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3074 gridview->setFlow(QQuickGridView::FlowTopToBottom);
3075 QTRY_COMPARE(gridview->flow(), QQuickGridView::FlowTopToBottom);
3077 gridview->setCurrentIndex(0);
3078 QTRY_COMPARE(gridview->currentIndex(), 0);
3079 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3080 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3081 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3087 void tst_QQuickGridView::footer()
3089 QFETCH(QQuickGridView::Flow, flow);
3090 QFETCH(Qt::LayoutDirection, layoutDirection);
3091 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3092 QFETCH(QPointF, initialFooterPos);
3093 QFETCH(QPointF, changedFooterPos);
3094 QFETCH(QPointF, initialContentPos);
3095 QFETCH(QPointF, firstDelegatePos);
3096 QFETCH(QPointF, resizeContentPos);
3098 QQuickView *canvas = getView();
3102 for (int i = 0; i < 7; i++)
3103 model.addItem("Item" + QString::number(i), "");
3105 QQmlContext *ctxt = canvas->rootContext();
3106 ctxt->setContextProperty("testModel", &model);
3107 canvas->setSource(testFileUrl("footer.qml"));
3108 qApp->processEvents();
3110 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3111 QTRY_VERIFY(gridview != 0);
3112 gridview->setFlow(flow);
3113 gridview->setLayoutDirection(layoutDirection);
3114 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3115 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3117 QQuickItem *contentItem = gridview->contentItem();
3118 QTRY_VERIFY(contentItem != 0);
3120 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3122 QVERIFY(footer == gridview->footerItem());
3124 QCOMPARE(footer->pos(), initialFooterPos);
3125 QCOMPARE(footer->width(), 100.);
3126 QCOMPARE(footer->height(), 30.);
3127 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3129 if (flow == QQuickGridView::FlowLeftToRight)
3130 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + footer->height());
3132 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + footer->width());
3134 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3136 QCOMPARE(item->pos(), firstDelegatePos);
3138 if (flow == QQuickGridView::FlowLeftToRight) {
3139 // shrink by one row
3140 model.removeItem(2);
3141 if (verticalLayoutDirection == QQuickItemView::TopToBottom)
3142 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
3144 QTRY_COMPARE(footer->y(), initialFooterPos.y() + gridview->cellHeight());
3146 // shrink by one column
3147 model.removeItem(2);
3148 model.removeItem(3);
3149 if (layoutDirection == Qt::LeftToRight)
3150 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
3152 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
3157 if (flow == QQuickGridView::FlowLeftToRight)
3158 QTRY_COMPARE(gridview->contentHeight(), footer->height());
3160 QTRY_COMPARE(gridview->contentWidth(), footer->width());
3162 QPointF posWhenNoItems(0, 0);
3163 if (layoutDirection == Qt::RightToLeft)
3164 posWhenNoItems.setX(flow == QQuickGridView::FlowLeftToRight ? gridview->width() - footer->width() : -footer->width());
3165 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3166 posWhenNoItems.setY(-footer->height());
3167 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3169 // if header is toggled, it shouldn't affect the footer position
3170 canvas->rootObject()->setProperty("showHeader", true);
3171 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
3172 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3173 canvas->rootObject()->setProperty("showHeader", false);
3176 for (int i = 0; i < 30; i++)
3177 model.addItem("Item" + QString::number(i), "");
3179 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
3180 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3182 QCOMPARE(footerItemSpy.count(), 1);
3184 footer = findItem<QQuickText>(contentItem, "footer");
3186 footer = findItem<QQuickText>(contentItem, "footer2");
3188 QVERIFY(footer == gridview->footerItem());
3190 QCOMPARE(footer->pos(), changedFooterPos);
3191 QCOMPARE(footer->width(), 50.);
3192 QCOMPARE(footer->height(), 20.);
3194 // changing the footer shouldn't change the content pos
3195 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3197 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3199 QCOMPARE(item->pos(), firstDelegatePos);
3201 gridview->positionViewAtEnd();
3202 footer->setHeight(10);
3203 footer->setWidth(40);
3204 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3206 releaseView(canvas);
3209 void tst_QQuickGridView::footer_data()
3211 QTest::addColumn<QQuickGridView::Flow>("flow");
3212 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3213 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3214 QTest::addColumn<QPointF>("initialFooterPos");
3215 QTest::addColumn<QPointF>("changedFooterPos");
3216 QTest::addColumn<QPointF>("initialContentPos");
3217 QTest::addColumn<QPointF>("firstDelegatePos");
3218 QTest::addColumn<QPointF>("resizeContentPos");
3220 // footer1 = 100 x 30
3221 // footer2 = 50 x 20
3224 // view height = 320
3226 // footer below items, bottom left
3227 QTest::newRow("LeftToRight, LtR, TtB")
3228 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3229 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
3230 << QPointF(0, 10 * 60) // 30 items = 10 rows
3233 << QPointF(0, (10 * 60) - 320 + 10);
3235 // footer below items, bottom right
3236 QTest::newRow("LeftToRight, RtL, TtB")
3237 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3238 << QPointF(240 - 100, 3 * 60)
3239 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
3241 << QPointF(240 - 80, 0)
3242 << QPointF(0, (10 * 60) - 320 + 10);
3244 // footer above items, top left
3245 QTest::newRow("LeftToRight, LtR, BtT")
3246 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3247 << QPointF(0, -(3 * 60) - 30)
3248 << QPointF(0, -(10 * 60) - 20)
3251 << QPointF(0, -(10 * 60) - 10);
3253 // footer above items, top right
3254 QTest::newRow("LeftToRight, RtL, BtT")
3255 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3256 << QPointF(240 - 100, -(3 * 60) - 30)
3257 << QPointF((240 - 100) + 50, -(10 * 60) - 20)
3259 << QPointF(240 - 80, -60)
3260 << QPointF(0, -(10 * 60) - 10);
3263 // footer to right of items, bottom right
3264 QTest::newRow("TopToBottom, LtR, TtB")
3265 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3266 << QPointF(2 * 80, 0) // 2 columns, cell width 80
3267 << QPointF(6 * 80, 0) // 30 items = 6 columns
3270 << QPointF((6 * 80) - 240 + 40, 0);
3272 // footer to left of items, bottom right
3273 QTest::newRow("TopToBottom, RtL, TtB")
3274 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3275 << QPointF(-(2 * 80) - 100, 0)
3276 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
3279 << QPointF(-(6 * 80) - 40, 0);
3281 // footer to right of items, top right
3282 QTest::newRow("TopToBottom, LtR, BtT")
3283 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3284 << QPointF(2 * 80, -30)
3285 << QPointF(6 * 80, -20)
3288 << QPointF((6 * 80) - 240 + 40, -320);
3290 // footer to left of items, top left
3291 QTest::newRow("TopToBottom, RtL, BtT")
3292 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3293 << QPointF(-(2 * 80) - 100, -30)
3294 << QPointF(-(6 * 80) - 50, -20)
3295 << QPointF(-240, -320)
3296 << QPointF(-80, -60)
3297 << QPointF(-(6 * 80) - 40, -320);
3300 void tst_QQuickGridView::initialZValues()
3302 QQuickView *canvas = createView();
3303 canvas->setSource(testFileUrl("initialZValues.qml"));
3304 qApp->processEvents();
3306 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3307 QTRY_VERIFY(gridview != 0);
3308 QQuickItem *contentItem = gridview->contentItem();
3309 QTRY_VERIFY(contentItem != 0);
3311 QVERIFY(gridview->headerItem());
3312 QTRY_COMPARE(gridview->headerItem()->z(), gridview->property("initialZ").toReal());
3314 QVERIFY(gridview->footerItem());
3315 QTRY_COMPARE(gridview->footerItem()->z(), gridview->property("initialZ").toReal());
3320 void tst_QQuickGridView::header()
3322 QFETCH(QQuickGridView::Flow, flow);
3323 QFETCH(Qt::LayoutDirection, layoutDirection);
3324 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3325 QFETCH(QPointF, initialHeaderPos);
3326 QFETCH(QPointF, changedHeaderPos);
3327 QFETCH(QPointF, initialContentPos);
3328 QFETCH(QPointF, changedContentPos);
3329 QFETCH(QPointF, firstDelegatePos);
3330 QFETCH(QPointF, resizeContentPos);
3333 for (int i = 0; i < 30; i++)
3334 model.addItem("Item" + QString::number(i), "");
3336 QQuickView *canvas = getView();
3337 canvas->rootContext()->setContextProperty("testModel", &model);
3338 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3339 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3340 canvas->setSource(testFileUrl("header.qml"));
3342 qApp->processEvents();
3344 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3345 QTRY_VERIFY(gridview != 0);
3346 gridview->setFlow(flow);
3347 gridview->setLayoutDirection(layoutDirection);
3348 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3349 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3351 QQuickItem *contentItem = gridview->contentItem();
3352 QTRY_VERIFY(contentItem != 0);
3354 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3356 QVERIFY(header == gridview->headerItem());
3358 QCOMPARE(header->pos(), initialHeaderPos);
3359 QCOMPARE(header->width(), 100.);
3360 QCOMPARE(header->height(), 30.);
3361 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3363 if (flow == QQuickGridView::FlowLeftToRight)
3364 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + header->height());
3366 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + header->width());
3368 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3370 QCOMPARE(item->pos(), firstDelegatePos);
3373 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3374 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3375 if (flow == QQuickGridView::FlowLeftToRight)
3376 QCOMPARE(gridview->contentHeight(), header->height());
3378 QCOMPARE(gridview->contentWidth(), header->width());
3380 for (int i = 0; i < 30; i++)
3381 model.addItem("Item" + QString::number(i), "");
3383 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
3384 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3386 QCOMPARE(headerItemSpy.count(), 1);
3388 header = findItem<QQuickText>(contentItem, "header");
3390 header = findItem<QQuickText>(contentItem, "header2");
3393 QVERIFY(header == gridview->headerItem());
3395 QCOMPARE(header->pos(), changedHeaderPos);
3396 QCOMPARE(header->width(), 50.);
3397 QCOMPARE(header->height(), 20.);
3398 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3400 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3402 QCOMPARE(item->pos(), firstDelegatePos);
3404 header->setHeight(10);
3405 header->setWidth(40);
3406 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3408 releaseView(canvas);
3411 // QTBUG-21207 header should become visible if view resizes from initial empty size
3414 canvas->rootContext()->setContextProperty("testModel", &model);
3415 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3416 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3417 canvas->setSource(testFileUrl("header.qml"));
3419 qApp->processEvents();
3421 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3422 QTRY_VERIFY(gridview != 0);
3423 gridview->setFlow(flow);
3424 gridview->setLayoutDirection(layoutDirection);
3425 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3426 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3428 gridview->setWidth(240);
3429 gridview->setHeight(320);
3430 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3431 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3433 releaseView(canvas);
3436 void tst_QQuickGridView::header_data()
3438 QTest::addColumn<QQuickGridView::Flow>("flow");
3439 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3440 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3441 QTest::addColumn<QPointF>("initialHeaderPos");
3442 QTest::addColumn<QPointF>("changedHeaderPos");
3443 QTest::addColumn<QPointF>("initialContentPos");
3444 QTest::addColumn<QPointF>("changedContentPos");
3445 QTest::addColumn<QPointF>("firstDelegatePos");
3446 QTest::addColumn<QPointF>("resizeContentPos");
3448 // header1 = 100 x 30
3449 // header2 = 50 x 20
3453 // header above items, top left
3454 QTest::newRow("LeftToRight, LtR, TtB")
3455 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3463 // header above items, top right
3464 QTest::newRow("LeftToRight, RtL, TtB")
3465 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3466 << QPointF(240 - 100, -30)
3467 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3473 // header below items, bottom left
3474 QTest::newRow("LeftToRight, LtR, BtT")
3475 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3478 << QPointF(0, -320 + 30)
3479 << QPointF(0, -320 + 20)
3481 << QPointF(0, -320 + 10);
3483 // header above items, top right
3484 QTest::newRow("LeftToRight, RtL, BtT")
3485 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3486 << QPointF(240 - 100, 0)
3487 << QPointF((240 - 100) + 50, 0)
3488 << QPointF(0, -320 + 30)
3489 << QPointF(0, -320 + 20)
3490 << QPointF(160, -60)
3491 << QPointF(0, -320 + 10);
3494 // header to left of items, bottom left
3495 QTest::newRow("TopToBottom, LtR, TtB")
3496 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3504 // header to right of items, bottom right
3505 QTest::newRow("TopToBottom, RtL, TtB")
3506 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3509 << QPointF(-(240 - 100), 0)
3510 << QPointF(-(240 - 50), 0)
3512 << QPointF(-(240 - 40), 0);
3514 // header to left of items, top left
3515 QTest::newRow("TopToBottom, LtR, BtT")
3516 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3517 << QPointF(-100, -30)
3518 << QPointF(-50, -20)
3519 << QPointF(-100, -320)
3520 << QPointF(-50, -320)
3522 << QPointF(-40, -320);
3524 // header to right of items, top right
3525 QTest::newRow("TopToBottom, RtL, BtT")
3526 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3529 << QPointF(-(240 - 100), -320)
3530 << QPointF(-(240 - 50), -320)
3531 << QPointF(-80, -60)
3532 << QPointF(-(240 - 40), -320);
3535 class GVAccessor : public QQuickGridView
3538 qreal minY() const { return minYExtent(); }
3539 qreal maxY() const { return maxYExtent(); }
3540 qreal minX() const { return minXExtent(); }
3541 qreal maxX() const { return maxXExtent(); }
3544 void tst_QQuickGridView::extents()
3546 QFETCH(QQuickGridView::Flow, flow);
3547 QFETCH(Qt::LayoutDirection, layoutDirection);
3548 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3549 QFETCH(QPointF, headerPos);
3550 QFETCH(QPointF, footerPos);
3551 QFETCH(QPointF, minPos);
3552 QFETCH(QPointF, maxPos);
3553 QFETCH(QPointF, origin_empty);
3554 QFETCH(QPointF, origin_nonEmpty);
3556 QQuickView *canvas = getView();
3559 QQmlContext *ctxt = canvas->rootContext();
3560 ctxt->setContextProperty("testModel", &model);
3561 canvas->setSource(testFileUrl("headerfooter.qml"));
3563 qApp->processEvents();
3565 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3566 QTRY_VERIFY(gridview != 0);
3567 gridview->setFlow(flow);
3568 gridview->setLayoutDirection(layoutDirection);
3569 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3570 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3572 QQuickItem *contentItem = gridview->contentItem();
3573 QTRY_VERIFY(contentItem != 0);
3575 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3577 QCOMPARE(header->pos(), headerPos);
3579 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3581 QCOMPARE(footer->pos(), footerPos);
3583 QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), minPos.x());
3584 QCOMPARE(static_cast<GVAccessor*>(gridview)->minY(), minPos.y());
3585 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), maxPos.x());
3586 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxY(), maxPos.y());
3588 QCOMPARE(gridview->originX(), origin_empty.x());
3589 QCOMPARE(gridview->originY(), origin_empty.y());
3590 for (int i=0; i<30; i++)
3591 model.addItem("Item" + QString::number(i), "");
3592 QTRY_COMPARE(gridview->count(), model.count());
3593 QCOMPARE(gridview->originX(), origin_nonEmpty.x());
3594 QCOMPARE(gridview->originY(), origin_nonEmpty.y());
3596 releaseView(canvas);
3599 void tst_QQuickGridView::extents_data()
3601 QTest::addColumn<QQuickGridView::Flow>("flow");
3602 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3603 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3604 QTest::addColumn<QPointF>("headerPos");
3605 QTest::addColumn<QPointF>("footerPos");
3606 QTest::addColumn<QPointF>("minPos");
3607 QTest::addColumn<QPointF>("maxPos");
3608 QTest::addColumn<QPointF>("origin_empty");
3609 QTest::addColumn<QPointF>("origin_nonEmpty");
3611 // header is 240x20 (or 20x320 in TopToBottom)
3612 // footer is 240x30 (or 30x320 in TopToBottom)
3613 // grid has 10 rows in LeftToRight mode and 6 columns in TopToBottom
3615 QTest::newRow("LeftToRight, LtR, TtB")
3616 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3617 << QPointF(0, -20) << QPointF(0, 0)
3618 << QPointF(0, 20) << QPointF(240, 20)
3619 << QPointF(0, -20) << QPointF(0, -20);
3621 QTest::newRow("LeftToRight, RtL, TtB")
3622 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3623 << QPointF(0, -20) << QPointF(0, 0)
3624 << QPointF(0, 20) << QPointF(240, 20)
3625 << QPointF(0, -20) << QPointF(0, -20);
3627 QTest::newRow("LeftToRight, LtR, BtT")
3628 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3629 << QPointF(0, 0) << QPointF(0, -30)
3630 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3631 << QPointF(0, -30) << QPointF(0, (-60.0 * 10) - 30);
3633 QTest::newRow("LeftToRight, RtL, BtT")
3634 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3635 << QPointF(0, 0) << QPointF(0, -30)
3636 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3637 << QPointF(0, -30) << QPointF(0, (-60.0 * 10) - 30);
3640 QTest::newRow("TopToBottom, LtR, TtB")
3641 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3642 << QPointF(-20, 0) << QPointF(0, 0)
3643 << QPointF(20, 0) << QPointF(20, 320)
3644 << QPointF(-20, 0) << QPointF(-20, 0);
3646 QTest::newRow("TopToBottom, RtL, TtB")
3647 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3648 << QPointF(0, 0) << QPointF(-30, 0)
3649 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3650 << QPointF(-30, 0) << QPointF((-80.0 * 6) - 30, 0);
3652 QTest::newRow("TopToBottom, LtR, BtT")
3653 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3654 << QPointF(-20, -320) << QPointF(0, -320)
3655 << QPointF(20, 0) << QPointF(20, 320)
3656 << QPointF(-20, 0) << QPointF(-20, 0);
3658 QTest::newRow("TopToBottom, RtL, BtT")
3659 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3660 << QPointF(0, -320) << QPointF(-30, -320)
3661 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3662 << QPointF(-30, 0) << QPointF((-80.0 * 6) - 30, 0);
3665 void tst_QQuickGridView::resetModel_headerFooter()
3667 // Resetting a model shouldn't crash in views with header/footer
3669 QQuickView *canvas = createView();
3672 for (int i = 0; i < 6; i++)
3673 model.addItem("Item" + QString::number(i), "");
3674 QQmlContext *ctxt = canvas->rootContext();
3675 ctxt->setContextProperty("testModel", &model);
3677 canvas->setSource(testFileUrl("headerfooter.qml"));
3678 qApp->processEvents();
3680 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3681 QTRY_VERIFY(gridview != 0);
3683 QQuickItem *contentItem = gridview->contentItem();
3684 QTRY_VERIFY(contentItem != 0);
3686 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3688 QCOMPARE(header->y(), -header->height());
3690 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3692 QCOMPARE(footer->y(), 60.*2);
3696 header = findItem<QQuickItem>(contentItem, "header");
3698 QCOMPARE(header->y(), -header->height());
3700 footer = findItem<QQuickItem>(contentItem, "footer");
3702 QCOMPARE(footer->y(), 60.*2);
3707 void tst_QQuickGridView::resizeViewAndRepaint()
3709 QQuickView *canvas = createView();
3713 for (int i = 0; i < 40; i++)
3714 model.addItem("Item" + QString::number(i), "");
3716 QQmlContext *ctxt = canvas->rootContext();
3717 ctxt->setContextProperty("testModel", &model);
3718 ctxt->setContextProperty("initialWidth", 240);
3719 ctxt->setContextProperty("initialHeight", 100);
3721 canvas->setSource(testFileUrl("resizeview.qml"));
3723 qApp->processEvents();
3725 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3726 QTRY_VERIFY(gridview != 0);
3727 QQuickItem *contentItem = gridview->contentItem();
3728 QTRY_VERIFY(contentItem != 0);
3729 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3731 // item at index 10 should not be currently visible
3732 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3734 gridview->setHeight(320);
3735 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3737 gridview->setHeight(100);
3738 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3740 // Ensure we handle -ve sizes
3741 gridview->setHeight(-100);
3742 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3744 gridview->setCacheBuffer(120);
3745 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3747 // ensure items in cache become visible
3748 gridview->setHeight(120);
3749 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3751 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3752 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3753 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3754 if (!item) qWarning() << "Item" << i << "not found";
3756 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3757 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3758 QCOMPARE(delegateVisible(item), i < 9); // inside view visible, outside not visible
3761 // ensure items outside view become invisible
3762 gridview->setHeight(60);
3763 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3765 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3766 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3767 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3768 if (!item) qWarning() << "Item" << i << "not found";
3770 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3771 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3772 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
3778 void tst_QQuickGridView::resizeGrid()
3780 QFETCH(QQuickGridView::Flow, flow);
3781 QFETCH(Qt::LayoutDirection, layoutDirection);
3782 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3783 QFETCH(QPointF, initialContentPos);
3784 QFETCH(QPointF, firstItemPos);
3787 for (int i = 0; i < 30; i++)
3788 model.addItem("Item" + QString::number(i), "");
3790 QQuickView *canvas = getView();
3791 QQmlContext *ctxt = canvas->rootContext();
3792 ctxt->setContextProperty("testModel", &model);
3793 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
3794 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
3795 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickGridView::BottomToTop);
3796 canvas->setSource(testFileUrl("resizegrid.qml"));
3798 qApp->processEvents();
3800 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3801 QTRY_VERIFY(gridview != 0);
3802 QQuickItem *contentItem = gridview->contentItem();
3803 QTRY_VERIFY(contentItem != 0);
3805 // set the width to slightly larger than 3 items across, to test
3806 // items are aligned correctly in right-to-left
3807 canvas->rootObject()->setWidth(260);
3808 canvas->rootObject()->setHeight(320);
3809 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3811 QCOMPARE(gridview->contentX(), initialContentPos.x());
3812 QCOMPARE(gridview->contentY(), initialContentPos.y());
3814 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
3816 QCOMPARE(item0->pos(), firstItemPos);
3818 // Confirm items positioned correctly and indexes correct
3819 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
3820 QVERIFY(items.count() >= 18 && items.count() <= 21);
3821 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3822 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3823 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3824 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3825 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3827 QCOMPARE(name->text(), model.name(i));
3830 // change from 3x5 grid to 4x7
3831 canvas->rootObject()->setWidth(canvas->rootObject()->width() + 80);
3832 canvas->rootObject()->setHeight(canvas->rootObject()->height() + 60*2);
3833 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3835 // other than in LeftToRight+RightToLeft layout, the first item should not move
3836 // if view is resized
3837 QCOMPARE(findItem<QQuickItem>(contentItem, "wrapper", 0), item0);
3838 if (flow == QQuickGridView::FlowLeftToRight && layoutDirection == Qt::RightToLeft)
3839 firstItemPos.rx() += 80;
3840 QCOMPARE(item0->pos(), firstItemPos);
3842 QPointF newContentPos = initialContentPos;
3843 if (flow == QQuickGridView::FlowTopToBottom && layoutDirection == Qt::RightToLeft)
3844 newContentPos.rx() -= 80.0;
3845 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3846 newContentPos.ry() -= 60.0 * 2;
3847 QCOMPARE(gridview->contentX(), newContentPos.x());
3848 QCOMPARE(gridview->contentY(), newContentPos.y());
3850 // Confirm items positioned correctly and indexes correct
3851 items = findItems<QQuickItem>(contentItem, "wrapper");
3852 QVERIFY(items.count() >= 28);
3853 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3854 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3855 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3856 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3857 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3859 QCOMPARE(name->text(), model.name(i));
3862 releaseView(canvas);
3865 void tst_QQuickGridView::resizeGrid_data()
3867 QTest::addColumn<QQuickGridView::Flow>("flow");
3868 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3869 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3870 QTest::addColumn<QPointF>("initialContentPos");
3871 QTest::addColumn<QPointF>("firstItemPos");
3873 // Initial view width is 260, so in LeftToRight + right-to-left mode the
3874 // content x should be -20
3876 QTest::newRow("LeftToRight, LtR, TtB")
3877 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3881 QTest::newRow("LeftToRight, RtL, TtB")
3882 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3883 << QPointF(-20.0, 0)
3884 << QPointF(80.0 * 2, 0);
3886 QTest::newRow("LeftToRight, LtR, BtT")
3887 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3889 << QPointF(0, -60.0);
3891 QTest::newRow("LeftToRight, RtL, BtT")
3892 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3893 << QPointF(-20.0, -320)
3894 << QPointF(80.0 * 2, -60.0);
3897 QTest::newRow("TopToBottom, LtR, TtB")
3898 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3902 QTest::newRow("TopToBottom, RtL, TtB")
3903 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3905 << QPointF(-80.0, 0);
3907 QTest::newRow("TopToBottom, LtR, BtT")
3908 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3910 << QPointF(0, -60.0);
3912 QTest::newRow("TopToBottom, RtL, BtT")
3913 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3914 << QPointF(-260, -320)
3915 << QPointF(-80.0, -60.0);
3919 void tst_QQuickGridView::changeColumnCount()
3922 for (int i = 0; i < 40; i++)
3923 model.addItem("Item" + QString::number(i), "");
3925 QQuickView *canvas = createView();
3926 QQmlContext *ctxt = canvas->rootContext();
3927 ctxt->setContextProperty("testModel", &model);
3928 ctxt->setContextProperty("initialWidth", 100);
3929 ctxt->setContextProperty("initialHeight", 320);
3930 canvas->setSource(testFileUrl("resizeview.qml"));
3932 qApp->processEvents();
3934 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3935 QTRY_VERIFY(gridview != 0);
3936 QQuickItem *contentItem = gridview->contentItem();
3937 QTRY_VERIFY(contentItem != 0);
3938 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3940 // a single column of 6 items are visible
3941 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3942 QCOMPARE(itemCount, 6);
3943 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3944 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3945 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3946 QCOMPARE(item->x(), 0.0);
3947 QCOMPARE(item->y(), qreal(i*60));
3950 // now 6x3 grid is visible, plus 1 extra below for refill
3951 gridview->setWidth(240);
3952 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3953 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3954 QCOMPARE(itemCount, 6*3 + 1);
3955 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3956 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3957 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3958 QCOMPARE(item->x(), qreal((i%3)*80));
3959 QCOMPARE(item->y(), qreal((i/3)*60));
3962 // back to single column
3963 gridview->setWidth(100);
3964 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3965 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3966 QCOMPARE(itemCount, 6);
3967 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3968 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3969 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3970 QCOMPARE(item->x(), 0.0);
3971 QCOMPARE(item->y(), qreal(i*60));
3977 void tst_QQuickGridView::indexAt_itemAt_data()
3979 QTest::addColumn<qreal>("x");
3980 QTest::addColumn<qreal>("y");
3981 QTest::addColumn<int>("index");
3983 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3984 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3985 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3986 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3987 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3990 void tst_QQuickGridView::indexAt_itemAt()
3996 QQuickView *canvas = getView();
3999 model.addItem("Fred", "12345");
4000 model.addItem("John", "2345");
4001 model.addItem("Bob", "54321");
4002 model.addItem("Billy", "22345");
4003 model.addItem("Sam", "2945");
4004 model.addItem("Ben", "04321");
4005 model.addItem("Jim", "0780");
4007 canvas->rootContext()->setContextProperty("testModel", &model);
4008 canvas->setSource(testFileUrl("gridview1.qml"));
4009 qApp->processEvents();
4011 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4012 QTRY_VERIFY(gridview != 0);
4014 QQuickItem *contentItem = gridview->contentItem();
4015 QTRY_VERIFY(contentItem != 0);
4017 QTRY_COMPARE(gridview->count(), model.count());
4019 QQuickItem *item = 0;
4021 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4024 QCOMPARE(gridview->indexAt(x, y), index);
4025 QVERIFY(gridview->itemAt(x, y) == item);
4027 releaseView(canvas);
4030 void tst_QQuickGridView::onAdd()
4032 QFETCH(int, initialItemCount);
4033 QFETCH(int, itemsToAdd);
4035 const int delegateWidth = 50;
4036 const int delegateHeight = 100;
4038 QQuickView *canvas = getView();
4039 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
4041 // these initial items should not trigger GridView.onAdd
4042 for (int i=0; i<initialItemCount; i++)
4043 model.addItem("dummy value", "dummy value");
4045 QQmlContext *ctxt = canvas->rootContext();
4046 ctxt->setContextProperty("testModel", &model);
4047 ctxt->setContextProperty("delegateWidth", delegateWidth);
4048 ctxt->setContextProperty("delegateHeight", delegateHeight);
4049 canvas->setSource(testFileUrl("attachedSignals.qml"));
4051 QObject *object = canvas->rootObject();
4052 object->setProperty("width", canvas->width());
4053 object->setProperty("height", canvas->height());
4054 qApp->processEvents();
4056 QList<QPair<QString, QString> > items;
4057 for (int i=0; i<itemsToAdd; i++)
4058 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4059 model.addItems(items);
4061 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4062 qApp->processEvents();
4064 QVariantList result = object->property("addedDelegates").toList();
4065 QTRY_COMPARE(result.count(), items.count());
4066 for (int i=0; i<items.count(); i++)
4067 QCOMPARE(result[i].toString(), items[i].first);
4069 releaseView(canvas);
4072 void tst_QQuickGridView::onAdd_data()
4074 QTest::addColumn<int>("initialItemCount");
4075 QTest::addColumn<int>("itemsToAdd");
4077 QTest::newRow("0, add 1") << 0 << 1;
4078 QTest::newRow("0, add 2") << 0 << 2;
4079 QTest::newRow("0, add 10") << 0 << 10;
4081 QTest::newRow("1, add 1") << 1 << 1;
4082 QTest::newRow("1, add 2") << 1 << 2;
4083 QTest::newRow("1, add 10") << 1 << 10;
4085 QTest::newRow("5, add 1") << 5 << 1;
4086 QTest::newRow("5, add 2") << 5 << 2;
4087 QTest::newRow("5, add 10") << 5 << 10;
4090 void tst_QQuickGridView::onRemove()
4092 QFETCH(int, initialItemCount);
4093 QFETCH(int, indexToRemove);
4094 QFETCH(int, removeCount);
4096 const int delegateWidth = 50;
4097 const int delegateHeight = 100;
4099 for (int i=0; i<initialItemCount; i++)
4100 model.addItem(QString("value %1").arg(i), "dummy value");
4102 QQuickView *canvas = getView();
4103 QQmlContext *ctxt = canvas->rootContext();
4104 ctxt->setContextProperty("testModel", &model);
4105 ctxt->setContextProperty("delegateWidth", delegateWidth);
4106 ctxt->setContextProperty("delegateHeight", delegateHeight);
4107 canvas->setSource(testFileUrl("attachedSignals.qml"));
4108 QObject *object = canvas->rootObject();
4110 model.removeItems(indexToRemove, removeCount);
4111 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4112 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4114 releaseView(canvas);
4117 void tst_QQuickGridView::onRemove_data()
4119 QTest::addColumn<int>("initialItemCount");
4120 QTest::addColumn<int>("indexToRemove");
4121 QTest::addColumn<int>("removeCount");
4123 QTest::newRow("remove first") << 1 << 0 << 1;
4124 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4125 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4126 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4128 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4129 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4130 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4131 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4132 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4133 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4135 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4136 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4137 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4140 void tst_QQuickGridView::columnCount()
4143 canvas.setSource(testFileUrl("gridview4.qml"));
4145 canvas.requestActivateWindow();
4146 QTest::qWaitForWindowShown(&canvas);
4148 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
4150 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
4151 QCOMPARE(view->cellHeight(), qreal(100));
4153 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
4154 QCOMPARE(items.size(), 18);
4155 QCOMPARE(items.at(8)->y(), qreal(0));
4156 QCOMPARE(items.at(9)->y(), qreal(100));
4159 void tst_QQuickGridView::margins()
4162 QQuickView *canvas = createView();
4165 for (int i = 0; i < 40; i++)
4166 model.addItem("Item" + QString::number(i), "");
4168 QQmlContext *ctxt = canvas->rootContext();
4169 ctxt->setContextProperty("testModel", &model);
4170 ctxt->setContextProperty("testRightToLeft", QVariant(false));
4172 canvas->setSource(testFileUrl("margins.qml"));
4174 qApp->processEvents();
4176 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4177 QTRY_VERIFY(gridview != 0);
4178 QQuickItem *contentItem = gridview->contentItem();
4179 QTRY_VERIFY(contentItem != 0);
4180 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4182 QCOMPARE(gridview->contentX(), -30.);
4183 QCOMPARE(gridview->originX(), 0.);
4186 gridview->positionViewAtEnd();
4187 qreal pos = gridview->contentX();
4188 gridview->setContentX(pos + 80);
4189 gridview->returnToBounds();
4190 QTRY_COMPARE(gridview->contentX(), pos + 50);
4192 // remove item before visible and check that left margin is maintained
4193 // and originX is updated
4194 gridview->setContentX(200);
4195 model.removeItems(0, 4);
4197 gridview->setContentX(-50);
4198 gridview->returnToBounds();
4199 QCOMPARE(gridview->originX(), 100.);
4200 QTRY_COMPARE(gridview->contentX(), 70.);
4202 // reduce left margin
4203 gridview->setLeftMargin(20);
4204 QCOMPARE(gridview->originX(), 100.);
4205 QTRY_COMPARE(gridview->contentX(), 80.);
4208 gridview->positionViewAtEnd();
4209 QCOMPARE(gridview->originX(), 0.); // positionViewAtEnd() resets origin
4210 pos = gridview->contentX();
4211 gridview->setContentX(pos + 80);
4212 gridview->returnToBounds();
4213 QTRY_COMPARE(gridview->contentX(), pos + 50);
4215 // reduce right margin
4216 pos = gridview->contentX();
4217 gridview->setRightMargin(40);
4218 QCOMPARE(gridview->originX(), 0.);
4219 QTRY_COMPARE(gridview->contentX(), pos-10);
4225 QQuickView *canvas = createView();
4229 for (int i = 0; i < 40; i++)
4230 model.addItem("Item" + QString::number(i), "");
4232 QQmlContext *ctxt = canvas->rootContext();
4233 ctxt->setContextProperty("testModel", &model);
4234 ctxt->setContextProperty("testRightToLeft", QVariant(true));
4236 canvas->setSource(testFileUrl("margins.qml"));
4237 qApp->processEvents();
4239 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4240 QTRY_VERIFY(gridview != 0);
4242 QQuickItem *contentItem = gridview->contentItem();
4243 QTRY_VERIFY(contentItem != 0);
4245 QTRY_COMPARE(gridview->contentX(), -240+50.);
4246 QTRY_COMPARE(gridview->originX(), -100. * 10);
4249 gridview->positionViewAtEnd();
4250 qreal pos = gridview->contentX();
4251 gridview->setContentX(pos - 80);
4252 gridview->returnToBounds();
4253 QTRY_COMPARE(gridview->contentX(), pos - 30);
4255 // remove item before visible and check that left margin is maintained
4256 // and originX is updated
4257 gridview->setContentX(-400);
4258 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4259 model.removeItems(0, 4);
4260 QTRY_COMPARE(model.count(), gridview->count());
4261 gridview->setContentX(-240+50);
4262 gridview->returnToBounds();
4263 QCOMPARE(gridview->originX(), -1000.);
4264 QTRY_COMPARE(gridview->contentX(), -240-50.);
4266 // reduce right margin
4267 pos = gridview->contentX();
4268 gridview->setRightMargin(40);
4269 QCOMPARE(gridview->originX(), -1000.);
4270 QTRY_COMPARE(gridview->contentX(), -240-100 + 40.);
4273 gridview->positionViewAtEnd();
4274 QCOMPARE(gridview->originX(), -900.); // positionViewAtEnd() resets origin
4275 pos = gridview->contentX();
4276 gridview->setContentX(pos - 80);
4277 gridview->returnToBounds();
4278 QTRY_COMPARE(gridview->contentX(), pos - 30);
4280 // reduce left margin
4281 pos = gridview->contentX();
4282 gridview->setLeftMargin(20);
4283 QCOMPARE(gridview->originX(), -900.);
4284 QTRY_COMPARE(gridview->contentX(), pos+10);
4290 void tst_QQuickGridView::creationContext()
4293 canvas.setGeometry(0,0,240,320);
4294 canvas.setSource(testFileUrl("creationContext.qml"));
4295 qApp->processEvents();
4297 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4299 QVERIFY(rootItem->property("count").toInt() > 0);
4302 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
4303 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4304 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4305 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4306 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4307 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4310 void tst_QQuickGridView::snapToRow_data()
4312 QTest::addColumn<QQuickGridView::Flow>("flow");
4313 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4314 QTest::addColumn<int>("highlightRangeMode");
4315 QTest::addColumn<QPoint>("flickStart");
4316 QTest::addColumn<QPoint>("flickEnd");
4317 QTest::addColumn<qreal>("snapAlignment");
4318 QTest::addColumn<qreal>("endExtent");
4319 QTest::addColumn<qreal>("startExtent");
4321 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4322 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4324 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4325 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4327 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4328 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
4330 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4331 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4333 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4334 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4336 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4337 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
4340 void tst_QQuickGridView::snapToRow()
4342 QFETCH(QQuickGridView::Flow, flow);
4343 QFETCH(Qt::LayoutDirection, layoutDirection);
4344 QFETCH(int, highlightRangeMode);
4345 QFETCH(QPoint, flickStart);
4346 QFETCH(QPoint, flickEnd);
4347 QFETCH(qreal, snapAlignment);
4348 QFETCH(qreal, endExtent);
4349 QFETCH(qreal, startExtent);
4351 QQuickView *canvas = getView();
4353 canvas->setSource(testFileUrl("snapToRow.qml"));
4355 qApp->processEvents();
4357 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4358 QTRY_VERIFY(gridview != 0);
4360 gridview->setFlow(flow);
4361 gridview->setLayoutDirection(layoutDirection);
4362 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4363 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4365 QQuickItem *contentItem = gridview->contentItem();
4366 QTRY_VERIFY(contentItem != 0);
4368 // confirm that a flick hits an item boundary
4369 flick(canvas, flickStart, flickEnd, 180);
4370 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4371 if (flow == QQuickGridView::FlowLeftToRight)
4372 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
4374 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
4378 flick(canvas, flickStart, flickEnd, 180);
4379 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4380 } while (flow == QQuickGridView::FlowLeftToRight
4381 ? !gridview->isAtYEnd()
4382 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4384 if (flow == QQuickGridView::FlowLeftToRight)
4385 QCOMPARE(gridview->contentY(), endExtent);
4387 QCOMPARE(gridview->contentX(), endExtent);
4391 flick(canvas, flickEnd, flickStart, 180);
4392 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4393 } while (flow == QQuickGridView::FlowLeftToRight
4394 ? !gridview->isAtYBeginning()
4395 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4397 if (flow == QQuickGridView::FlowLeftToRight)
4398 QCOMPARE(gridview->contentY(), startExtent);
4400 QCOMPARE(gridview->contentX(), startExtent);
4402 releaseView(canvas);
4405 void tst_QQuickGridView::snapOneRow_data()
4407 QTest::addColumn<QQuickGridView::Flow>("flow");
4408 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4409 QTest::addColumn<int>("highlightRangeMode");
4410 QTest::addColumn<QPoint>("flickStart");
4411 QTest::addColumn<QPoint>("flickEnd");
4412 QTest::addColumn<qreal>("snapAlignment");
4413 QTest::addColumn<qreal>("endExtent");
4414 QTest::addColumn<qreal>("startExtent");
4416 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4417 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4419 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4420 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4422 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4423 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
4425 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4426 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4428 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4429 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4431 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4432 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
4435 void tst_QQuickGridView::snapOneRow()
4437 QFETCH(QQuickGridView::Flow, flow);
4438 QFETCH(Qt::LayoutDirection, layoutDirection);
4439 QFETCH(int, highlightRangeMode);
4440 QFETCH(QPoint, flickStart);
4441 QFETCH(QPoint, flickEnd);
4442 QFETCH(qreal, snapAlignment);
4443 QFETCH(qreal, endExtent);
4444 QFETCH(qreal, startExtent);
4446 QQuickView *canvas = getView();
4448 canvas->setSource(testFileUrl("snapOneRow.qml"));
4450 qApp->processEvents();
4452 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4453 QTRY_VERIFY(gridview != 0);
4455 gridview->setFlow(flow);
4456 gridview->setLayoutDirection(layoutDirection);
4457 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4458 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4460 QQuickItem *contentItem = gridview->contentItem();
4461 QTRY_VERIFY(contentItem != 0);
4463 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
4465 // confirm that a flick hits next row boundary
4466 flick(canvas, flickStart, flickEnd, 180);
4467 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4468 if (flow == QQuickGridView::FlowLeftToRight)
4469 QCOMPARE(gridview->contentY(), snapAlignment);
4471 QCOMPARE(gridview->contentX(), snapAlignment);
4473 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4474 QCOMPARE(gridview->currentIndex(), 2);
4475 QCOMPARE(currentIndexSpy.count(), 1);
4480 flick(canvas, flickStart, flickEnd, 180);
4481 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4482 } while (flow == QQuickGridView::FlowLeftToRight
4483 ? !gridview->isAtYEnd()
4484 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4486 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4487 QCOMPARE(gridview->currentIndex(), 6);
4488 QCOMPARE(currentIndexSpy.count(), 3);
4491 if (flow == QQuickGridView::FlowLeftToRight)
4492 QCOMPARE(gridview->contentY(), endExtent);
4494 QCOMPARE(gridview->contentX(), endExtent);
4498 flick(canvas, flickEnd, flickStart, 180);
4499 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4500 } while (flow == QQuickGridView::FlowLeftToRight
4501 ? !gridview->isAtYBeginning()
4502 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4504 if (flow == QQuickGridView::FlowLeftToRight)
4505 QCOMPARE(gridview->contentY(), startExtent);
4507 QCOMPARE(gridview->contentX(), startExtent);
4509 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4510 QCOMPARE(gridview->currentIndex(), 0);
4511 QCOMPARE(currentIndexSpy.count(), 6);
4514 releaseView(canvas);
4518 void tst_QQuickGridView::unaligned()
4520 QQuickView *canvas = createView();
4524 for (int i = 0; i < 10; i++)
4525 model.addItem("Item" + QString::number(i), "");
4527 QQmlContext *ctxt = canvas->rootContext();
4528 ctxt->setContextProperty("testModel", &model);
4530 canvas->setSource(testFileUrl("unaligned.qml"));
4531 qApp->processEvents();
4533 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
4534 QVERIFY(gridview != 0);
4536 QQuickItem *contentItem = gridview->contentItem();
4537 QVERIFY(contentItem != 0);
4539 for (int i = 0; i < 10; ++i) {
4540 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4541 if (!item) qWarning() << "Item" << i << "not found";
4543 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4544 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4548 for (int i = 10; i < 18; ++i) {
4549 model.addItem("Item" + QString::number(i), "");
4550 QQuickItem *item = 0;
4551 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4552 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4553 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4557 for (int i = 0; i < 10; ++i) {
4558 model.insertItem(i, "Item" + QString::number(i), "");
4559 QQuickItem *item = 0;
4560 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4561 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4562 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4566 model.removeItems(7, 10);
4567 QTRY_COMPARE(model.count(), gridview->count());
4568 for (int i = 0; i < 18; ++i) {
4569 QQuickItem *item = 0;
4570 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4571 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
4572 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
4578 void tst_QQuickGridView::populateTransitions()
4580 QFETCH(bool, staticallyPopulate);
4581 QFETCH(bool, dynamicallyPopulate);
4582 QFETCH(bool, usePopulateTransition);
4584 QPointF transitionFrom(-50, -50);
4585 QPointF transitionVia(100, 100);
4586 QaimModel model_transitionFrom;
4587 QaimModel model_transitionVia;
4590 if (staticallyPopulate) {
4591 for (int i = 0; i < 30; i++)
4592 model.addItem("item" + QString::number(i), "");
4595 QQuickView *canvas = getView();
4596 canvas->rootContext()->setContextProperty("testModel", &model);
4597 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4598 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4599 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4600 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4601 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4602 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4603 canvas->setSource(testFileUrl("populateTransitions.qml"));
4606 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4608 QQuickItem *contentItem = gridview->contentItem();
4609 QVERIFY(contentItem);
4611 // check the populate transition is run
4612 if (staticallyPopulate && usePopulateTransition) {
4613 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 18);
4614 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4615 } else if (dynamicallyPopulate) {
4616 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4617 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 18);
4619 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4620 QCOMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4621 QCOMPARE(gridview->property("countAddTransitions").toInt(), 0);
4624 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4625 for (int i=0; i < model.count() && i < itemCount; ++i) {
4626 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4627 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4628 QTRY_COMPARE(item->x(), (i%3)*80.0);
4629 QTRY_COMPARE(item->y(), (i/3)*60.0);
4630 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4632 QTRY_COMPARE(name->text(), model.name(i));
4635 gridview->setProperty("countPopulateTransitions", 0);
4636 gridview->setProperty("countAddTransitions", 0);
4638 // add an item and check this is done with add transition, not populate
4639 model.insertItem(0, "another item", "");
4640 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
4641 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4644 canvas->rootContext()->setContextProperty("testModel", QVariant());
4645 QTRY_COMPARE(gridview->count(), 0);
4646 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4647 gridview->setProperty("countPopulateTransitions", 0);
4648 gridview->setProperty("countAddTransitions", 0);
4650 // set to a valid model and check populate transition is run a second time
4652 for (int i = 0; i < 30; i++)
4653 model.addItem("item" + QString::number(i), "");
4654 canvas->rootContext()->setContextProperty("testModel", &model);
4655 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4657 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4658 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4660 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4661 for (int i=0; i < model.count() && i < itemCount; ++i) {
4662 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4663 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4664 QTRY_COMPARE(item->x(), (i%3)*80.0);
4665 QTRY_COMPARE(item->y(), (i/3)*60.0);
4666 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4668 QTRY_COMPARE(name->text(), model.name(i));
4671 // reset model and check populate transition is run again
4672 gridview->setProperty("countPopulateTransitions", 0);
4673 gridview->setProperty("countAddTransitions", 0);
4675 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4676 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4678 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4679 for (int i=0; i < model.count() && i < itemCount; ++i) {
4680 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4681 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4682 QTRY_COMPARE(item->x(), (i%3)*80.0);
4683 QTRY_COMPARE(item->y(), (i/3)*60.0);
4684 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4686 QTRY_COMPARE(name->text(), model.name(i));
4689 releaseView(canvas);
4692 void tst_QQuickGridView::populateTransitions_data()
4694 QTest::addColumn<bool>("staticallyPopulate");
4695 QTest::addColumn<bool>("dynamicallyPopulate");
4696 QTest::addColumn<bool>("usePopulateTransition");
4698 QTest::newRow("static") << true << false << true;
4699 QTest::newRow("static, no populate") << true << false << false;
4701 QTest::newRow("dynamic") << false << true << true;
4702 QTest::newRow("dynamic, no populate") << false << true << false;
4704 QTest::newRow("empty to start with") << false << false << true;
4705 QTest::newRow("empty to start with, no populate") << false << false << false;
4708 void tst_QQuickGridView::addTransitions()
4710 QFETCH(int, initialItemCount);
4711 QFETCH(bool, shouldAnimateTargets);
4712 QFETCH(qreal, contentYRowOffset);
4713 QFETCH(int, insertionIndex);
4714 QFETCH(int, insertionCount);
4715 QFETCH(ListRange, expectedDisplacedIndexes);
4717 // added items should start here
4718 QPointF targetItems_transitionFrom(-50, -50);
4720 // displaced items should pass through this point
4721 QPointF displacedItems_transitionVia(100, 100);
4724 for (int i = 0; i < initialItemCount; i++)
4725 model.addItem("Original item" + QString::number(i), "");
4726 QaimModel model_targetItems_transitionFrom;
4727 QaimModel model_displacedItems_transitionVia;
4729 QQuickView *canvas = getView();
4730 QQmlContext *ctxt = canvas->rootContext();
4731 ctxt->setContextProperty("testModel", &model);
4732 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4733 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4734 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4735 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4736 canvas->setSource(testFileUrl("addTransitions.qml"));
4739 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4740 QTRY_VERIFY(gridview != 0);
4741 QQuickItem *contentItem = gridview->contentItem();
4742 QVERIFY(contentItem != 0);
4743 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4745 if (contentYRowOffset != 0) {
4746 gridview->setContentY(contentYRowOffset * 60.0);
4747 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4750 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4752 // only target items that will become visible should be animated
4753 QList<QPair<QString, QString> > newData;
4754 QList<QPair<QString, QString> > expectedTargetData;
4755 QList<int> targetIndexes;
4756 if (shouldAnimateTargets) {
4757 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4758 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4760 // last visible item is the first item of the row beneath the view
4761 if (i >= (gridview->contentY() / 60)*3 && i < qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) {
4762 expectedTargetData << newData.last();
4766 QVERIFY(expectedTargetData.count() > 0);
4770 if (!newData.isEmpty()) {
4771 model.insertItems(insertionIndex, newData);
4772 QTRY_COMPARE(model.count(), gridview->count());
4775 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4777 if (shouldAnimateTargets) {
4778 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4779 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4780 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4782 // check the target and displaced items were animated
4783 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4784 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4786 // check attached properties
4787 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4788 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4789 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4790 if (expectedDisplacedIndexes.isValid()) {
4791 // adjust expectedDisplacedIndexes to their final values after the move
4792 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4793 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4794 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4795 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4798 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4799 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4802 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4803 int firstVisibleIndex = -1;
4804 for (int i=0; i<items.count(); i++) {
4805 if (items[i]->y() >= gridview->contentY()) {
4806 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4807 firstVisibleIndex = e.evaluate().toInt();
4811 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4813 // verify all items moved to the correct final positions
4814 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4815 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4816 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4817 QCOMPARE(item->x(), (i%3)*80.0);
4818 QCOMPARE(item->y(), (i/3)*60.0);
4819 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4821 QCOMPARE(name->text(), model.name(i));
4824 releaseView(canvas);
4827 void tst_QQuickGridView::addTransitions_data()
4829 QTest::addColumn<int>("initialItemCount");
4830 QTest::addColumn<qreal>("contentYRowOffset");
4831 QTest::addColumn<bool>("shouldAnimateTargets");
4832 QTest::addColumn<int>("insertionIndex");
4833 QTest::addColumn<int>("insertionCount");
4834 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4836 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4837 QTest::newRow("insert 1, just before start")
4838 << 30 << 1.0 << false
4839 << 0 << 1 << ListRange();
4840 QTest::newRow("insert 1, way before start")
4841 << 30 << 1.0 << false
4842 << 0 << 1 << ListRange();
4843 QTest::newRow("insert multiple, just before start")
4844 << 30 << 1.0 << false
4845 << 0 << 3 << ListRange();
4846 QTest::newRow("insert multiple (< 1 row), just before start")
4847 << 30 << 1.0 << false
4848 << 0 << 2 << ListRange();
4849 QTest::newRow("insert multiple, way before start")
4850 << 30 << 3.0 << false
4851 << 0 << 3 << ListRange();
4853 QTest::newRow("insert 1 at start")
4854 << 30 << 0.0 << true
4855 << 0 << 1 << ListRange(0, 17);
4856 QTest::newRow("insert multiple at start")
4857 << 30 << 0.0 << true
4858 << 0 << 3 << ListRange(0, 17);
4859 QTest::newRow("insert multiple (> 1 row) at start")
4860 << 30 << 0.0 << true
4861 << 0 << 5 << ListRange(0, 17);
4862 QTest::newRow("insert 1 at start, content y not 0")
4863 << 30 << 1.0 << true // first visible is index 3
4864 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4865 QTest::newRow("insert multiple at start, content y not 0")
4866 << 30 << 1.0 << true // first visible is index 3
4867 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4868 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4869 << 30 << 1.0 << true // first visible is index 3
4870 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4872 QTest::newRow("insert 1 at start, to empty grid")
4874 << 0 << 1 << ListRange();
4875 QTest::newRow("insert multiple at start, to empty grid")
4877 << 0 << 3 << ListRange();
4879 QTest::newRow("insert 1 at middle")
4880 << 30 << 0.0 << true
4881 << 7 << 1 << ListRange(7, 17);
4882 QTest::newRow("insert multiple at middle")
4883 << 30 << 0.0 << true
4884 << 7 << 3 << ListRange(7, 17);
4885 QTest::newRow("insert multiple (> 1 row) at middle")
4886 << 30 << 0.0 << true
4887 << 7 << 5 << ListRange(7, 17);
4889 QTest::newRow("insert 1 at bottom")
4890 << 30 << 0.0 << true
4891 << 17 << 1 << ListRange(17, 17);
4892 QTest::newRow("insert multiple at bottom")
4893 << 30 << 0.0 << true
4894 << 17 << 3 << ListRange(17, 17);
4895 QTest::newRow("insert 1 at bottom, content y not 0")
4896 << 30 << 1.0 << true
4897 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4898 QTest::newRow("insert multiple at bottom, content y not 0")
4899 << 30 << 1.0 << true
4900 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4903 // items added after the last visible will not be animated in, since they
4904 // do not appear in the final view
4905 QTest::newRow("insert 1 after end")
4906 << 30 << 0.0 << false
4907 << 18 << 1 << ListRange();
4908 QTest::newRow("insert multiple after end")
4909 << 30 << 0.0 << false
4910 << 18 << 3 << ListRange();
4913 void tst_QQuickGridView::moveTransitions()
4915 QFETCH(int, initialItemCount);
4916 QFETCH(qreal, contentYRowOffset);
4917 QFETCH(qreal, rowOffsetAfterMove);
4918 QFETCH(int, moveFrom);
4919 QFETCH(int, moveTo);
4920 QFETCH(int, moveCount);
4921 QFETCH(ListRange, expectedDisplacedIndexes);
4923 // target and displaced items should pass through these points
4924 QPointF targetItems_transitionVia(-50, 50);
4925 QPointF displacedItems_transitionVia(100, 100);
4928 for (int i = 0; i < initialItemCount; i++)
4929 model.addItem("Original item" + QString::number(i), "");
4930 QaimModel model_targetItems_transitionVia;
4931 QaimModel model_displacedItems_transitionVia;
4933 QQuickView *canvas = getView();
4934 QQmlContext *ctxt = canvas->rootContext();
4935 ctxt->setContextProperty("testModel", &model);
4936 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4937 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4938 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4939 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4940 canvas->setSource(testFileUrl("moveTransitions.qml"));
4943 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4944 QTRY_VERIFY(gridview != 0);
4945 QQuickItem *contentItem = gridview->contentItem();
4946 QVERIFY(contentItem != 0);
4949 if (contentYRowOffset != 0) {
4950 gridview->setContentY(contentYRowOffset * 60.0);
4951 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4954 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4956 // Items moving to *or* from visible positions should be animated.
4957 // Otherwise, they should not be animated.
4958 QList<QPair<QString, QString> > expectedTargetData;
4959 QList<int> targetIndexes;
4960 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4961 int toIndex = moveTo + (i - moveFrom);
4962 int firstVisibleIndex = (gridview->contentY() / 60) * 3;
4963 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4964 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4965 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4966 expectedTargetData << qMakePair(model.name(i), model.number(i));
4970 // ViewTransition.index provides the indices that items are moving to, not from
4971 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4974 model.moveItems(moveFrom, moveTo, moveCount);
4976 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4977 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4978 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4980 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4982 // check the target and displaced items were animated
4983 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4984 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4986 // check attached properties
4987 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4988 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4989 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4990 if (expectedDisplacedIndexes.isValid()) {
4991 // adjust expectedDisplacedIndexes to their final values after the move
4992 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4993 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4994 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4995 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4998 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4999 int firstVisibleIndex = -1;
5000 for (int i=0; i<items.count(); i++) {
5001 if (items[i]->y() >= gridview->contentY()) {
5002 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5003 firstVisibleIndex = e.evaluate().toInt();
5007 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5009 // verify all items moved to the correct final positions
5010 qreal pixelOffset = 60 * rowOffsetAfterMove;
5011 for (int i=firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
5012 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5013 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5014 QCOMPARE(item->x(), (i%3)*80.0);
5015 QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
5016 name = findItem<QQuickText>(contentItem, "textName", i);
5018 QTRY_COMPARE(name->text(), model.name(i));
5021 releaseView(canvas);
5024 void tst_QQuickGridView::moveTransitions_data()
5026 QTest::addColumn<int>("initialItemCount");
5027 QTest::addColumn<qreal>("contentYRowOffset");
5028 QTest::addColumn<qreal>("rowOffsetAfterMove");
5029 QTest::addColumn<int>("moveFrom");
5030 QTest::addColumn<int>("moveTo");
5031 QTest::addColumn<int>("moveCount");
5032 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5034 QTest::newRow("move from above view, outside visible items, move 1")
5036 << 1 << 10 << 1 << ListRange(6, 10);
5037 QTest::newRow("move from above view, outside visible items, move 1 (first item)")
5039 << 0 << 10 << 1 << ListRange(6, 10);
5040 QTest::newRow("move from above view, outside visible items, move multiple")
5042 << 1 << 10 << 3 << ListRange(13, 23);
5043 QTest::newRow("move from above view, mix of visible/non-visible")
5045 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
5046 QTest::newRow("move from above view, mix of visible/non-visible (move first)")
5048 << 0 << 10 << 6 << ListRange(16, 23);
5050 QTest::newRow("move within view, move 1 down")
5052 << 1 << 10 << 1 << ListRange(2, 10);
5053 QTest::newRow("move within view, move 1 down, move first item")
5055 << 0 << 10 << 1 << ListRange(1, 10);
5056 QTest::newRow("move within view, move 1 down, move first item, contentY not 0")
5058 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
5059 QTest::newRow("move within view, move 1 down, to last item")
5061 << 10 << 17 << 1 << ListRange(11, 17);
5062 QTest::newRow("move within view, move first->last")
5064 << 0 << 17 << 1 << ListRange(1, 17);
5066 QTest::newRow("move within view, move multiple down")
5068 << 1 << 10 << 3 << ListRange(4, 12);
5069 QTest::newRow("move within view, move multiple down, move first item")
5071 << 0 << 10 << 3 << ListRange(3, 12);
5072 QTest::newRow("move within view, move multiple down, move first item, contentY not 0")
5074 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
5075 QTest::newRow("move within view, move multiple down, displace last item")
5077 << 5 << 15 << 3 << ListRange(8, 17);
5078 QTest::newRow("move within view, move multiple down, move first->last")
5080 << 0 << 15 << 3 << ListRange(3, 17);
5082 QTest::newRow("move within view, move 1 up")
5084 << 10 << 1 << 1 << ListRange(1, 9);
5085 QTest::newRow("move within view, move 1 up, move to first index")
5087 << 10 << 0 << 1 << ListRange(0, 9);
5088 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0")
5090 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
5091 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border")
5093 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
5094 QTest::newRow("move within view, move 1 up, move last item")
5096 << 17 << 10 << 1 << ListRange(10, 16);
5097 QTest::newRow("move within view, move 1 up, move last->first")
5099 << 17 << 0 << 1 << ListRange(0, 16);
5101 QTest::newRow("move within view, move multiple up")
5103 << 10 << 1 << 3 << ListRange(1, 9);
5104 QTest::newRow("move within view, move multiple (> 1 row) up")
5106 << 10 << 1 << 5 << ListRange(1, 9);
5107 QTest::newRow("move within view, move multiple up, move to first index")
5109 << 10 << 0 << 3 << ListRange(0, 9);
5110 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0")
5112 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
5113 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border")
5115 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
5116 QTest::newRow("move within view, move multiple up, move last item")
5118 << 15 << 5 << 3 << ListRange(5, 14);
5119 QTest::newRow("move within view, move multiple up, move last->first")
5121 << 15 << 0 << 3 << ListRange(0, 14);
5123 QTest::newRow("move from below view, move 1 up")
5125 << 20 << 5 << 1 << ListRange(5, 17);
5126 QTest::newRow("move from below view, move 1 up, move to top")
5128 << 20 << 0 << 1 << ListRange(0, 17);
5129 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0")
5131 << 25 << 3 << 1 << ListRange(0+3, 17+3);
5132 QTest::newRow("move from below view, move multiple (> 1 row) up")
5134 << 20 << 5 << 5 << ListRange(5, 17);
5135 QTest::newRow("move from below view, move multiple up, move to top")
5137 << 20 << 0 << 3 << ListRange(0, 17);
5138 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0")
5140 << 25 << 3 << 3 << ListRange(0+3, 17+3);
5142 QTest::newRow("move from below view, move 1 up, move to bottom")
5144 << 20 << 17 << 1 << ListRange(17, 17);
5145 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0")
5147 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
5148 QTest::newRow("move from below view, move multiple up, move to to bottom")
5150 << 20 << 17 << 3 << ListRange(17, 17);
5151 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0")
5153 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
5156 void tst_QQuickGridView::removeTransitions()
5158 QFETCH(int, initialItemCount);
5159 QFETCH(bool, shouldAnimateTargets);
5160 QFETCH(qreal, contentYRowOffset);
5161 QFETCH(int, removalIndex);
5162 QFETCH(int, removalCount);
5163 QFETCH(ListRange, expectedDisplacedIndexes);
5165 // added items should end here
5166 QPointF targetItems_transitionTo(-50, -50);
5168 // displaced items should pass through this points
5169 QPointF displacedItems_transitionVia(100, 100);
5172 for (int i = 0; i < initialItemCount; i++)
5173 model.addItem("Original item" + QString::number(i), "");
5174 QaimModel model_targetItems_transitionTo;
5175 QaimModel model_displacedItems_transitionVia;
5177 QQuickView *canvas = getView();
5178 QQmlContext *ctxt = canvas->rootContext();
5179 ctxt->setContextProperty("testModel", &model);
5180 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5181 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5182 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5183 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5184 canvas->setSource(testFileUrl("removeTransitions.qml"));
5187 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5188 QTRY_VERIFY(gridview != 0);
5189 QQuickItem *contentItem = gridview->contentItem();
5190 QVERIFY(contentItem != 0);
5191 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5193 if (contentYRowOffset != 0) {
5194 gridview->setContentY(contentYRowOffset * 60.0);
5195 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5198 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5200 // only target items that are visible should be animated
5201 QList<QPair<QString, QString> > expectedTargetData;
5202 QList<int> targetIndexes;
5203 if (shouldAnimateTargets) {
5204 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5205 int firstVisibleIndex = (gridview->contentY() / 60.0)*3;
5206 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
5207 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
5208 expectedTargetData << qMakePair(model.name(i), model.number(i));
5212 QVERIFY(expectedTargetData.count() > 0);
5215 // calculate targetItems and expectedTargets before model changes
5216 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5217 QVariantMap expectedTargets;
5218 for (int i=0; i<targetIndexes.count(); i++)
5219 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5222 model.removeItems(removalIndex, removalCount);
5223 QTRY_COMPARE(model.count(), gridview->count());
5225 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
5226 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5227 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
5228 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5230 // check the target and displaced items were animated
5231 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5232 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5234 // check attached properties
5235 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
5236 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5237 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
5238 if (expectedDisplacedIndexes.isValid()) {
5239 // adjust expectedDisplacedIndexes to their final values after the move
5240 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5241 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5242 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5243 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
5246 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5247 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5250 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5251 int itemCount = items.count();
5252 int firstVisibleIndex = -1;
5253 for (int i=0; i<items.count(); i++) {
5254 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5255 int index = e.evaluate().toInt();
5256 if (firstVisibleIndex < 0 && items[i]->y() >= gridview->contentY())
5257 firstVisibleIndex = index;
5259 itemCount--; // exclude deleted items
5261 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5263 // verify all items moved to the correct final positions
5264 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5265 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5266 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5267 QCOMPARE(item->x(), (i%3)*80.0);
5268 QCOMPARE(item->y(), gridview->contentY() + ((i-firstVisibleIndex)/3) * 60.0);
5269 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5271 QTRY_COMPARE(name->text(), model.name(i));
5274 releaseView(canvas);
5277 void tst_QQuickGridView::removeTransitions_data()
5279 QTest::addColumn<int>("initialItemCount");
5280 QTest::addColumn<qreal>("contentYRowOffset");
5281 QTest::addColumn<bool>("shouldAnimateTargets");
5282 QTest::addColumn<int>("removalIndex");
5283 QTest::addColumn<int>("removalCount");
5284 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5286 // All items that are visible following the remove operation should be animated.
5287 // Remove targets that are outside of the view should not be animated.
5289 // For a GridView, removing any number of items other than a full row before the start
5290 // should displace all items in the view
5291 QTest::newRow("remove 1 before start")
5292 << 30 << 2.0 << false
5293 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
5294 QTest::newRow("remove 1 row, before start")
5295 << 30 << 2.0 << false
5296 << 3 << 3 << ListRange();
5297 QTest::newRow("remove between 1-2 rows, before start")
5298 << 30 << 2.0 << false
5299 << 0 << 5 << ListRange(6, 25);
5300 QTest::newRow("remove 2 rows, before start")
5301 << 30 << 2.0 << false
5302 << 0 << 6 << ListRange();
5303 QTest::newRow("remove mix of before and after start")
5304 << 30 << 1.0 << true
5305 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
5308 QTest::newRow("remove 1 from start")
5309 << 30 << 0.0 << true
5310 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
5311 QTest::newRow("remove multiple from start")
5312 << 30 << 0.0 << true
5313 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
5314 QTest::newRow("remove 1 from start, content y not 0")
5315 << 30 << 1.0 << true
5316 << 3 << 1 << ListRange(1 + 3, 18 + 3);
5317 QTest::newRow("remove multiple from start, content y not 0")
5318 << 30 << 1.0 << true
5319 << 3 << 3 << ListRange(3 + 3, 20 + 3);
5322 QTest::newRow("remove 1 from middle")
5323 << 30 << 0.0 << true
5324 << 5 << 1 << ListRange(6, 18);
5325 QTest::newRow("remove multiple from middle")
5326 << 30 << 0.0 << true
5327 << 5 << 3 << ListRange(8, 20);
5330 QTest::newRow("remove 1 from bottom")
5331 << 30 << 0.0 << true
5332 << 17 << 1 << ListRange(18, 18);
5333 QTest::newRow("remove multiple (1 row) from bottom")
5334 << 30 << 0.0 << true
5335 << 15 << 3 << ListRange(18, 20);
5336 QTest::newRow("remove multiple (> 1 row) from bottom")
5337 << 30 << 0.0 << true
5338 << 15 << 5 << ListRange(20, 22);
5339 QTest::newRow("remove 1 from bottom, content y not 0")
5340 << 30 << 1.0 << true
5341 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
5342 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
5343 << 30 << 1.0 << true
5344 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
5347 QTest::newRow("remove 1 after end")
5348 << 30 << 0.0 << false
5349 << 18 << 1 << ListRange();
5350 QTest::newRow("remove multiple after end")
5351 << 30 << 0.0 << false
5352 << 18 << 3 << ListRange();
5355 void tst_QQuickGridView::displacedTransitions()
5357 QFETCH(bool, useDisplaced);
5358 QFETCH(bool, displacedEnabled);
5359 QFETCH(bool, useAddDisplaced);
5360 QFETCH(bool, addDisplacedEnabled);
5361 QFETCH(bool, useMoveDisplaced);
5362 QFETCH(bool, moveDisplacedEnabled);
5363 QFETCH(bool, useRemoveDisplaced);
5364 QFETCH(bool, removeDisplacedEnabled);
5365 QFETCH(ListChange, change);
5366 QFETCH(ListRange, expectedDisplacedIndexes);
5369 for (int i = 0; i < 30; i++)
5370 model.addItem("Original item" + QString::number(i), "");
5371 QaimModel model_displaced_transitionVia;
5372 QaimModel model_addDisplaced_transitionVia;
5373 QaimModel model_moveDisplaced_transitionVia;
5374 QaimModel model_removeDisplaced_transitionVia;
5376 QPointF displaced_transitionVia(-50, -100);
5377 QPointF addDisplaced_transitionVia(-150, 100);
5378 QPointF moveDisplaced_transitionVia(50, -100);
5379 QPointF removeDisplaced_transitionVia(150, 100);
5381 QQuickView *canvas = getView();
5382 QQmlContext *ctxt = canvas->rootContext();
5383 ctxt->setContextProperty("testModel", &model);
5384 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
5385 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
5386 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
5387 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
5388 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
5389 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
5390 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
5391 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
5392 ctxt->setContextProperty("useDisplaced", useDisplaced);
5393 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
5394 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
5395 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
5396 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
5397 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
5398 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
5399 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
5400 canvas->setSource(testFileUrl("displacedTransitions.qml"));
5402 qApp->processEvents();
5404 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5405 QTRY_VERIFY(gridview != 0);
5406 QQuickItem *contentItem = gridview->contentItem();
5407 QVERIFY(contentItem != 0);
5408 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5410 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5411 gridview->setProperty("displaceTransitionsDone", false);
5413 switch (change.type) {
5414 case ListChange::Inserted:
5416 QList<QPair<QString, QString> > targetItemData;
5417 for (int i=change.index; i<change.index + change.count; ++i)
5418 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
5419 model.insertItems(change.index, targetItemData);
5420 QTRY_COMPARE(model.count(), gridview->count());
5423 case ListChange::Removed:
5424 model.removeItems(change.index, change.count);
5425 QTRY_COMPARE(model.count(), gridview->count());
5427 case ListChange::Moved:
5428 model.moveItems(change.index, change.to, change.count);
5429 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5431 case ListChange::SetCurrent:
5432 case ListChange::SetContentY:
5433 case ListChange::Polish:
5437 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
5438 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
5440 if ((useDisplaced && displacedEnabled)
5441 || (useAddDisplaced && addDisplacedEnabled)
5442 || (useMoveDisplaced && moveDisplacedEnabled)
5443 || (useRemoveDisplaced && removeDisplacedEnabled)) {
5444 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5446 // check the correct number of target items and indexes were received
5447 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
5448 for (int i=0; i<resultTargetIndexes.count(); i++)
5449 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
5450 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
5451 for (int i=0; i<resultTargetItems.count(); i++)
5452 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
5454 QCOMPARE(resultTargetIndexes.count(), 0);
5455 QCOMPARE(resultTargetItems.count(), 0);
5458 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
5459 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
5461 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
5462 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
5463 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
5465 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
5466 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
5467 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
5469 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
5471 if (useDisplaced && displacedEnabled
5472 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
5473 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
5474 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
5475 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
5477 QCOMPARE(model_displaced_transitionVia.count(), 0);
5480 // verify all items moved to the correct final positions
5481 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5482 for (int i=0; i < model.count() && i < items.count(); ++i) {
5483 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5484 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5485 QCOMPARE(item->x(), (i%3)*80.0);
5486 QCOMPARE(item->y(), (i/3)*60.0);
5487 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5489 QTRY_COMPARE(name->text(), model.name(i));
5492 releaseView(canvas);
5495 void tst_QQuickGridView::displacedTransitions_data()
5497 QTest::addColumn<bool>("useDisplaced");
5498 QTest::addColumn<bool>("displacedEnabled");
5499 QTest::addColumn<bool>("useAddDisplaced");
5500 QTest::addColumn<bool>("addDisplacedEnabled");
5501 QTest::addColumn<bool>("useMoveDisplaced");
5502 QTest::addColumn<bool>("moveDisplacedEnabled");
5503 QTest::addColumn<bool>("useRemoveDisplaced");
5504 QTest::addColumn<bool>("removeDisplacedEnabled");
5505 QTest::addColumn<ListChange>("change");
5506 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5508 QTest::newRow("no displaced transitions at all")
5513 << ListChange::insert(0, 1) << ListRange(0, 17);
5515 QTest::newRow("just displaced")
5520 << ListChange::insert(0, 1) << ListRange(0, 17);
5522 QTest::newRow("just displaced (not enabled)")
5527 << ListChange::insert(0, 1) << ListRange(0, 17);
5529 QTest::newRow("displaced + addDisplaced")
5534 << ListChange::insert(0, 1) << ListRange(0, 17);
5536 QTest::newRow("displaced + addDisplaced (not enabled)")
5541 << ListChange::insert(0, 1) << ListRange(0, 17);
5543 QTest::newRow("displaced + moveDisplaced")
5548 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5550 QTest::newRow("displaced + moveDisplaced (not enabled)")
5555 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5557 QTest::newRow("displaced + removeDisplaced")
5562 << ListChange::remove(0, 1) << ListRange(1, 18);
5564 QTest::newRow("displaced + removeDisplaced (not enabled)")
5569 << ListChange::remove(0, 1) << ListRange(1, 18);
5572 QTest::newRow("displaced + add, should use generic displaced for a remove")
5577 << ListChange::remove(0, 1) << ListRange(1, 18);
5580 void tst_QQuickGridView::multipleTransitions()
5582 // Tests that if you interrupt a transition in progress with another action that
5583 // cancels the previous transition, the resulting items are still placed correctly.
5585 QFETCH(int, initialCount);
5586 QFETCH(qreal, contentY);
5587 QFETCH(QList<ListChange>, changes);
5588 QFETCH(bool, enableAddTransitions);
5589 QFETCH(bool, enableMoveTransitions);
5590 QFETCH(bool, enableRemoveTransitions);
5591 QFETCH(bool, rippleAddDisplaced);
5593 // add transitions on the left, moves on the right
5594 QPointF addTargets_transitionFrom(-50, -50);
5595 QPointF addDisplaced_transitionFrom(-50, 50);
5596 QPointF moveTargets_transitionFrom(50, -50);
5597 QPointF moveDisplaced_transitionFrom(50, 50);
5598 QPointF removeTargets_transitionTo(-100, 300);
5599 QPointF removeDisplaced_transitionFrom(100, 300);
5602 for (int i = 0; i < initialCount; i++)
5603 model.addItem("Original item" + QString::number(i), "");
5605 QQuickView *canvas = getView();
5606 QQmlContext *ctxt = canvas->rootContext();
5607 ctxt->setContextProperty("testModel", &model);
5608 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5609 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5610 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5611 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5612 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5613 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5614 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5615 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5616 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5617 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5618 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5620 QTest::qWaitForWindowShown(canvas);
5622 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5623 QTRY_VERIFY(gridview != 0);
5624 QQuickItem *contentItem = gridview->contentItem();
5625 QVERIFY(contentItem != 0);
5626 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5628 if (contentY != 0) {
5629 gridview->setContentY(contentY);
5630 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5633 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5635 for (int i=0; i<changes.count(); i++) {
5636 switch (changes[i].type) {
5637 case ListChange::Inserted:
5639 QList<QPair<QString, QString> > targetItems;
5640 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5641 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5642 model.insertItems(changes[i].index, targetItems);
5643 QTRY_COMPARE(model.count(), gridview->count());
5644 if (i == changes.count() - 1) {
5645 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
5646 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
5648 QTest::qWait(timeBetweenActions);
5652 case ListChange::Removed:
5653 model.removeItems(changes[i].index, changes[i].count);
5654 QTRY_COMPARE(model.count(), gridview->count());
5655 if (i == changes.count() - 1) {
5656 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
5657 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
5659 QTest::qWait(timeBetweenActions);
5662 case ListChange::Moved:
5663 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5664 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5665 if (i == changes.count() - 1) {
5666 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
5667 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
5669 QTest::qWait(timeBetweenActions);
5672 case ListChange::SetCurrent:
5673 gridview->setCurrentIndex(changes[i].index);
5674 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5676 case ListChange::SetContentY:
5677 gridview->setContentY(changes[i].pos);
5678 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5680 case ListChange::Polish:
5684 QCOMPARE(gridview->count(), model.count());
5686 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5687 int firstVisibleIndex = -1;
5688 for (int i=0; i<items.count(); i++) {
5689 if (items[i]->y() >= contentY) {
5690 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5691 firstVisibleIndex = e.evaluate().toInt();
5695 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5697 // verify all items moved to the correct final positions
5698 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5699 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5700 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5701 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5702 QTRY_COMPARE(item->x(), (i%3)*80.0);
5703 QTRY_COMPARE(item->y(), (i/3)*60.0);
5704 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5706 QTRY_COMPARE(name->text(), model.name(i));
5709 releaseView(canvas);
5712 void tst_QQuickGridView::multipleTransitions_data()
5714 QTest::addColumn<int>("initialCount");
5715 QTest::addColumn<qreal>("contentY");
5716 QTest::addColumn<QList<ListChange> >("changes");
5717 QTest::addColumn<bool>("enableAddTransitions");
5718 QTest::addColumn<bool>("enableMoveTransitions");
5719 QTest::addColumn<bool>("enableRemoveTransitions");
5720 QTest::addColumn<bool>("rippleAddDisplaced");
5722 // the added item and displaced items should move to final dest correctly
5723 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5724 << ListChange::insert(0, 1)
5725 << ListChange::move(0, 3, 1)
5727 << true << true << true << false;
5729 // items affected by the add should change from move to add transition
5730 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5731 << ListChange::move(1, 10, 3)
5732 << ListChange::insert(0, 1)
5734 << true << true << true << false;
5736 // items should be placed correctly if you trigger a transition then refill for that index
5737 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5738 << ListChange::insert(0, 1)
5739 << ListChange::setContentY(160.0)
5740 << ListChange::setContentY(0.0)
5741 << ListChange::insert(0, 1)
5743 << true << true << true << false;
5745 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5746 << ListChange::insert(1, 1)
5747 << ListChange::remove(1, 1)
5749 << true << true << true << true;
5751 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5752 // even if a remove-displace transition is not present to re-animate them
5753 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5754 << ListChange::insert(0, 1)
5755 << ListChange::remove(2, 1)
5757 << true << true << false << false;
5759 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5760 // remove has changed the position of where it will move to
5761 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5762 << ListChange::setContentY(-10.0)
5763 << ListChange::insert(0, 1)
5764 << ListChange::insert(0, 1)
5765 << ListChange::remove(2, 1)
5767 << true << true << false << false;
5770 void tst_QQuickGridView::multipleDisplaced()
5772 // multiple move() operations should only restart displace transitions for items that
5773 // moved from previously set positions, and not those that have moved from their current
5774 // item positions (which may e.g. still be changing from easing bounces in the last transition)
5777 for (int i = 0; i < 30; i++)
5778 model.addItem("Original item" + QString::number(i), "");
5780 QQuickView *canvas = createView();
5781 QQmlContext *ctxt = canvas->rootContext();
5782 ctxt->setContextProperty("testModel", &model);
5783 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5785 QTest::qWaitForWindowShown(canvas);
5787 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5788 QTRY_VERIFY(gridview != 0);
5789 QQuickItem *contentItem = gridview->contentItem();
5790 QVERIFY(contentItem != 0);
5791 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5793 model.moveItems(12, 8, 1);
5794 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5795 model.moveItems(8, 3, 1);
5796 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5798 QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5799 foreach (const QString &name, transitionsStarted.keys()) {
5800 QVERIFY2(transitionsStarted[name] == 1,
5801 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5804 // verify all items moved to the correct final positions
5805 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5806 for (int i=0; i < model.count() && i < items.count(); ++i) {
5807 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5808 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5809 QTRY_COMPARE(item->x(), (i%3)*80.0);
5810 QTRY_COMPARE(item->y(), (i/3)*60.0);
5811 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5813 QTRY_COMPARE(name->text(), model.name(i));
5819 void tst_QQuickGridView::cacheBuffer()
5821 QQuickView *canvas = createView();
5824 for (int i = 0; i < 90; i++)
5825 model.addItem("Item" + QString::number(i), "");
5827 canvas->rootContext()->setContextProperty("testModel", &model);
5828 canvas->setSource(testFileUrl("gridview1.qml"));
5830 qApp->processEvents();
5832 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5833 QVERIFY(gridview != 0);
5835 QQuickItem *contentItem = gridview->contentItem();
5836 QVERIFY(contentItem != 0);
5837 QVERIFY(gridview->delegate() != 0);
5838 QVERIFY(gridview->model() != 0);
5840 // Confirm items positioned correctly
5841 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5842 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5843 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5844 QTRY_COMPARE(item->x(), (i%3)*80.0);
5845 QTRY_COMPARE(item->y(), (i/3)*60.0);
5848 QQmlIncubationController controller;
5849 canvas->engine()->setIncubationController(&controller);
5851 canvas->rootObject()->setProperty("cacheBuffer", 200);
5852 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5854 // items will be created one at a time
5855 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5856 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5857 QQuickItem *item = 0;
5860 controller.incubateWhile(&b);
5861 item = findItem<QQuickItem>(gridview, "wrapper", i);
5867 controller.incubateWhile(&b);
5870 int newItemCount = 0;
5871 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5873 // Confirm items positioned correctly
5874 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5875 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5877 QTRY_COMPARE(item->x(), (i%3)*80.0);
5878 QTRY_COMPARE(item->y(), (i/3)*60.0);
5881 // move view and confirm items in view are visible immediately and outside are created async
5882 gridview->setContentY(300);
5884 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5885 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5887 QTRY_COMPARE(item->x(), (i%3)*80.0);
5888 QTRY_COMPARE(item->y(), (i/3)*60.0);
5891 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5893 // ensure buffered items are created
5894 for (int i = 34; i < qMin(44,model.count()); ++i) {
5895 QQuickItem *item = 0;
5897 qGuiApp->processEvents(); // allow refill to happen
5899 controller.incubateWhile(&b);
5900 item = findItem<QQuickItem>(gridview, "wrapper", i);
5906 controller.incubateWhile(&b);
5912 void tst_QQuickGridView::asynchronous()
5914 QQuickView *canvas = createView();
5916 QQmlIncubationController controller;
5917 canvas->engine()->setIncubationController(&controller);
5919 canvas->setSource(testFileUrl("asyncloader.qml"));
5921 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5922 QVERIFY(rootObject);
5924 QQuickGridView *gridview = 0;
5927 controller.incubateWhile(&b);
5928 gridview = rootObject->findChild<QQuickGridView*>("view");
5931 // items will be created one at a time
5932 for (int i = 0; i < 12; ++i) {
5933 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5934 QQuickItem *item = 0;
5937 controller.incubateWhile(&b);
5938 item = findItem<QQuickItem>(gridview, "wrapper", i);
5944 controller.incubateWhile(&b);
5947 // verify positioning
5948 QQuickItem *contentItem = gridview->contentItem();
5949 for (int i = 0; i < 12; ++i) {
5950 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5951 if (!item) qWarning() << "Item" << i << "not found";
5952 QVERIFY(item->x() == (i%3)*100);
5953 QVERIFY(item->y() == (i/3)*100);
5959 void tst_QQuickGridView::unrequestedVisibility()
5962 for (int i = 0; i < 30; i++)
5963 model.addItem("Item" + QString::number(i), QString::number(i));
5965 QQuickView *canvas = new QQuickView(0);
5966 canvas->setGeometry(0,0,240,320);
5968 QQmlContext *ctxt = canvas->rootContext();
5969 ctxt->setContextProperty("testModel", &model);
5970 ctxt->setContextProperty("testWrap", QVariant(false));
5972 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5976 qApp->processEvents();
5978 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5979 QTRY_VERIFY(leftview != 0);
5981 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5982 QTRY_VERIFY(rightview != 0);
5984 QQuickItem *leftContent = leftview->contentItem();
5985 QTRY_VERIFY(leftContent != 0);
5987 QQuickItem *rightContent = rightview->contentItem();
5988 QTRY_VERIFY(rightContent != 0);
5990 rightview->setCurrentIndex(12);
5992 QTRY_COMPARE(leftview->contentY(), 0.0);
5993 QTRY_COMPARE(rightview->contentY(), 240.0);
5997 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5998 QCOMPARE(delegateVisible(item), true);
5999 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6000 QCOMPARE(delegateVisible(item), false);
6002 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
6003 QCOMPARE(delegateVisible(item), false);
6004 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
6005 QCOMPARE(delegateVisible(item), true);
6007 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
6008 QCOMPARE(delegateVisible(item), true);
6009 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
6010 QCOMPARE(delegateVisible(item), false);
6011 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
6012 QCOMPARE(delegateVisible(item), false);
6013 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
6014 QCOMPARE(delegateVisible(item), true);
6016 rightview->setCurrentIndex(0);
6018 QTRY_COMPARE(leftview->contentY(), 0.0);
6019 QTRY_COMPARE(rightview->contentY(), 0.0);
6021 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
6022 QCOMPARE(delegateVisible(item), true);
6023 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6024 QTRY_COMPARE(delegateVisible(item), true);
6026 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
6027 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
6029 leftview->setCurrentIndex(12);
6031 QTRY_COMPARE(leftview->contentY(), 240.0);
6032 QTRY_COMPARE(rightview->contentY(), 0.0);
6034 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
6035 QTRY_COMPARE(delegateVisible(item), false);
6036 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6037 QCOMPARE(delegateVisible(item), true);
6039 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
6040 QCOMPARE(delegateVisible(item), true);
6041 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
6042 QCOMPARE(delegateVisible(item), false);
6044 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6045 QCOMPARE(delegateVisible(item), false);
6046 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6047 QCOMPARE(delegateVisible(item), true);
6048 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6049 QCOMPARE(delegateVisible(item), true);
6050 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6051 QCOMPARE(delegateVisible(item), false);
6053 // move a non-visible item into view
6054 model.moveItems(10, 9, 1);
6055 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6057 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
6058 QCOMPARE(delegateVisible(item), false);
6059 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6060 QCOMPARE(delegateVisible(item), true);
6062 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
6063 QCOMPARE(delegateVisible(item), true);
6064 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
6065 QCOMPARE(delegateVisible(item), false);
6067 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6068 QCOMPARE(delegateVisible(item), false);
6069 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6070 QCOMPARE(delegateVisible(item), true);
6071 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6072 QCOMPARE(delegateVisible(item), true);
6073 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6074 QCOMPARE(delegateVisible(item), false);
6076 // move a visible item out of view
6077 model.moveItems(5, 3, 1);
6078 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6080 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6081 QCOMPARE(delegateVisible(item), false);
6082 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6083 QCOMPARE(delegateVisible(item), true);
6084 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6085 QCOMPARE(delegateVisible(item), true);
6086 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6087 QCOMPARE(delegateVisible(item), false);
6089 // move a non-visible item into view
6090 model.moveItems(3, 5, 1);
6091 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6093 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6094 QCOMPARE(delegateVisible(item), false);
6095 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6096 QCOMPARE(delegateVisible(item), true);
6097 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6098 QCOMPARE(delegateVisible(item), true);
6099 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6100 QCOMPARE(delegateVisible(item), false);
6102 // move a visible item out of view
6103 model.moveItems(9, 10, 1);
6104 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6106 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6107 QCOMPARE(delegateVisible(item), false);
6108 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6109 QCOMPARE(delegateVisible(item), true);
6110 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6111 QCOMPARE(delegateVisible(item), true);
6112 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6113 QCOMPARE(delegateVisible(item), false);
6115 // move a non-visible item into view
6116 model.moveItems(10, 9, 1);
6117 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6119 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6120 QCOMPARE(delegateVisible(item), false);
6121 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6122 QCOMPARE(delegateVisible(item), true);
6123 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6124 QCOMPARE(delegateVisible(item), true);
6125 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6126 QCOMPARE(delegateVisible(item), false);
6132 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB()
6134 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6137 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB_data()
6139 inserted_defaultLayout_data();
6142 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB()
6144 inserted_defaultLayout(QQuickGridView::FlowTopToBottom);
6147 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB_data()
6149 inserted_defaultLayout_data();
6152 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB()
6154 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6157 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB_data()
6159 inserted_defaultLayout_data();
6162 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT()
6164 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6167 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT_data()
6169 inserted_defaultLayout_data();
6172 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT()
6174 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6177 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT_data()
6179 inserted_defaultLayout_data();
6182 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT()
6184 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6187 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT_data()
6189 inserted_defaultLayout_data();
6192 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT()
6194 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6197 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT_data()
6199 inserted_defaultLayout_data();
6203 void tst_QQuickGridView::removed_leftToRight_RtL_TtB()
6205 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6208 void tst_QQuickGridView::removed_leftToRight_RtL_TtB_data()
6210 removed_defaultLayout_data();
6213 void tst_QQuickGridView::removed_topToBottom_LtR_TtB()
6215 removed_defaultLayout(QQuickGridView::FlowTopToBottom);
6218 void tst_QQuickGridView::removed_topToBottom_LtR_TtB_data()
6220 removed_defaultLayout_data();
6223 void tst_QQuickGridView::removed_topToBottom_RtL_TtB()
6225 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6228 void tst_QQuickGridView::removed_topToBottom_RtL_TtB_data()
6230 removed_defaultLayout_data();
6233 void tst_QQuickGridView::removed_leftToRight_LtR_BtT()
6235 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6238 void tst_QQuickGridView::removed_leftToRight_LtR_BtT_data()
6240 removed_defaultLayout_data();
6243 void tst_QQuickGridView::removed_leftToRight_RtL_BtT()
6245 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6248 void tst_QQuickGridView::removed_leftToRight_RtL_BtT_data()
6250 removed_defaultLayout_data();
6253 void tst_QQuickGridView::removed_topToBottom_LtR_BtT()
6255 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6258 void tst_QQuickGridView::removed_topToBottom_LtR_BtT_data()
6260 removed_defaultLayout_data();
6263 void tst_QQuickGridView::removed_topToBottom_RtL_BtT()
6265 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6268 void tst_QQuickGridView::removed_topToBottom_RtL_BtT_data()
6270 removed_defaultLayout_data();
6274 void tst_QQuickGridView::moved_leftToRight_RtL_TtB()
6276 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6279 void tst_QQuickGridView::moved_leftToRight_RtL_TtB_data()
6281 moved_defaultLayout_data();
6284 void tst_QQuickGridView::moved_topToBottom_LtR_TtB()
6286 moved_defaultLayout(QQuickGridView::FlowTopToBottom);
6289 void tst_QQuickGridView::moved_topToBottom_LtR_TtB_data()
6291 moved_defaultLayout_data();
6294 void tst_QQuickGridView::moved_topToBottom_RtL_TtB()
6296 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6299 void tst_QQuickGridView::moved_topToBottom_RtL_TtB_data()
6301 moved_defaultLayout_data();
6304 void tst_QQuickGridView::moved_leftToRight_LtR_BtT()
6306 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6309 void tst_QQuickGridView::moved_leftToRight_LtR_BtT_data()
6311 moved_defaultLayout_data();
6314 void tst_QQuickGridView::moved_leftToRight_RtL_BtT()
6316 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6319 void tst_QQuickGridView::moved_leftToRight_RtL_BtT_data()
6321 moved_defaultLayout_data();
6324 void tst_QQuickGridView::moved_topToBottom_LtR_BtT()
6326 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6329 void tst_QQuickGridView::moved_topToBottom_LtR_BtT_data()
6331 moved_defaultLayout_data();
6334 void tst_QQuickGridView::moved_topToBottom_RtL_BtT()
6336 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6339 void tst_QQuickGridView::moved_topToBottom_RtL_BtT_data()
6341 moved_defaultLayout_data();
6345 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
6349 for (int i=0; i<list.count(); i++) {
6350 ret << list[i].toInt(&ok);
6352 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
6358 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6360 for (int i=0; i<indexLists.count(); i++) {
6361 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6362 if (current != expectedIndexes.toSet())
6363 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6364 QCOMPARE(current, expectedIndexes.toSet());
6368 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6370 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6371 QVERIFY(it.value().type() == QVariant::Int);
6372 QString name = it.key();
6373 int itemIndex = it.value().toInt();
6374 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6375 if (model.name(itemIndex) != name)
6376 qDebug() << itemIndex;
6377 QCOMPARE(model.name(itemIndex), name);
6379 QCOMPARE(items.count(), expectedIndexes.count());
6382 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6384 for (int i=0; i<itemLists.count(); i++) {
6385 QVariantList current = itemLists[i].toList();
6386 for (int j=0; j<current.count(); j++) {
6387 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6388 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6389 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6391 QCOMPARE(current.count(), expectedItems.count());
6395 QTEST_MAIN(tst_QQuickGridView)
6397 #include "tst_qquickgridview.moc"