1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtCore/qstringlistmodel.h>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcomponent.h>
47 #include <QtQml/qqmlcontext.h>
48 #include <QtQml/qqmlexpression.h>
49 #include <QtQml/qqmlincubator.h>
50 #include <QtQml/qqmlcontext.h>
51 #include <QtQuick/private/qquickitem_p.h>
52 #include <QtQuick/private/qquickgridview_p.h>
53 #include <QtQuick/private/qquicktext_p.h>
54 #include <QtQuick/private/qquickvisualitemmodel_p.h>
55 #include <QtQml/private/qquicklistmodel_p.h>
56 #include <QtQml/private/qlistmodelinterface_p.h>
57 #include "../../shared/util.h"
58 #include "../shared/viewtestutil.h"
59 #include "../shared/visualtestutil.h"
60 #include <QtGui/qguiapplication.h>
62 Q_DECLARE_METATYPE(QQuickGridView::Flow)
63 Q_DECLARE_METATYPE(Qt::LayoutDirection)
64 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
65 Q_DECLARE_METATYPE(Qt::Key)
67 using namespace QQuickViewTestUtil;
68 using namespace QQuickVisualTestUtil;
72 class tst_QQuickGridView : public QQmlDataTest
82 void inserted_basic();
83 void inserted_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
84 void inserted_defaultLayout_data();
85 void insertBeforeVisible();
86 void insertBeforeVisible_data();
88 void removed_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
89 void removed_defaultLayout_data();
90 void addOrRemoveBeforeVisible();
91 void addOrRemoveBeforeVisible_data();
93 void moved_defaultLayout(QQuickGridView::Flow flow = QQuickGridView::FlowLeftToRight, Qt::LayoutDirection horizLayout = Qt::LeftToRight, QQuickItemView::VerticalLayoutDirection verticalLayout = QQuickItemView::TopToBottom);
94 void moved_defaultLayout_data();
95 void multipleChanges_condensed() { multipleChanges(true); }
96 void multipleChanges_condensed_data() { multipleChanges_data(); }
97 void multipleChanges_uncondensed() { multipleChanges(false); }
98 void multipleChanges_uncondensed_data() { multipleChanges_data(); }
99 void swapWithFirstItem();
102 void noCurrentIndex();
103 void keyNavigation();
104 void keyNavigation_data();
105 void defaultValues();
107 void propertyChanges();
108 void componentChanges();
110 void positionViewAtIndex();
111 void positionViewAtIndex_rightToLeft();
116 void enforceRange_rightToLeft();
118 void manualHighlight();
121 void initialZValues();
126 void resetModel_headerFooter();
127 void resizeViewAndRepaint();
129 void resizeGrid_data();
130 void changeColumnCount();
131 void indexAt_itemAt_data();
132 void indexAt_itemAt();
136 void onRemove_data();
139 void creationContext();
140 void snapToRow_data();
142 void snapOneRow_data();
147 void unrequestedVisibility();
149 void populateTransitions();
150 void populateTransitions_data();
151 void addTransitions();
152 void addTransitions_data();
153 void moveTransitions();
154 void moveTransitions_data();
155 void removeTransitions();
156 void removeTransitions_data();
157 void displacedTransitions();
158 void displacedTransitions_data();
159 void multipleTransitions();
160 void multipleTransitions_data();
161 void multipleDisplaced();
163 void inserted_leftToRight_RtL_TtB();
164 void inserted_leftToRight_RtL_TtB_data();
165 void inserted_leftToRight_LtR_BtT();
166 void inserted_leftToRight_LtR_BtT_data();
167 void inserted_leftToRight_RtL_BtT();
168 void inserted_leftToRight_RtL_BtT_data();
169 void inserted_topToBottom_LtR_TtB();
170 void inserted_topToBottom_LtR_TtB_data();
171 void inserted_topToBottom_RtL_TtB();
172 void inserted_topToBottom_RtL_TtB_data();
173 void inserted_topToBottom_LtR_BtT();
174 void inserted_topToBottom_LtR_BtT_data();
175 void inserted_topToBottom_RtL_BtT();
176 void inserted_topToBottom_RtL_BtT_data();
178 void removed_leftToRight_RtL_TtB();
179 void removed_leftToRight_RtL_TtB_data();
180 void removed_leftToRight_LtR_BtT();
181 void removed_leftToRight_LtR_BtT_data();
182 void removed_leftToRight_RtL_BtT();
183 void removed_leftToRight_RtL_BtT_data();
184 void removed_topToBottom_LtR_TtB();
185 void removed_topToBottom_LtR_TtB_data();
186 void removed_topToBottom_RtL_TtB();
187 void removed_topToBottom_RtL_TtB_data();
188 void removed_topToBottom_LtR_BtT();
189 void removed_topToBottom_LtR_BtT_data();
190 void removed_topToBottom_RtL_BtT();
191 void removed_topToBottom_RtL_BtT_data();
193 void moved_leftToRight_RtL_TtB();
194 void moved_leftToRight_RtL_TtB_data();
195 void moved_leftToRight_LtR_BtT();
196 void moved_leftToRight_LtR_BtT_data();
197 void moved_leftToRight_RtL_BtT();
198 void moved_leftToRight_RtL_BtT_data();
199 void moved_topToBottom_LtR_TtB();
200 void moved_topToBottom_LtR_TtB_data();
201 void moved_topToBottom_RtL_TtB();
202 void moved_topToBottom_RtL_TtB_data();
203 void moved_topToBottom_LtR_BtT();
204 void moved_topToBottom_LtR_BtT_data();
205 void moved_topToBottom_RtL_BtT();
206 void moved_topToBottom_RtL_BtT_data();
209 QList<int> toIntList(const QVariantList &list);
210 void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
211 void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
212 void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
214 void multipleChanges(bool condensed);
215 void multipleChanges_data();
217 QPointF expectedItemPos(QQuickGridView *grid, int index, qreal rowOffset = 0) {
220 if (grid->flow() == QQuickGridView::FlowLeftToRight) {
221 int columns = grid->width() / grid->cellWidth();
222 x = (index % columns) * grid->cellWidth();
223 y = (index / columns) * grid->cellHeight();
224 if (grid->effectiveLayoutDirection() == Qt::RightToLeft) {
225 int col = (index % columns) * grid->cellWidth();
226 x = grid->cellWidth() * (columns - 1) - col;
229 qreal offset = grid->cellHeight() * rowOffset;
230 if (grid->verticalLayoutDirection() == QQuickItemView::TopToBottom)
233 y = -grid->cellHeight() - y - offset;
235 int rows = grid->height() / grid->cellHeight();
236 x = (index / rows) * grid->cellWidth();
237 y = (index % rows) * grid->cellHeight();
238 if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
239 x = -x - grid->cellWidth();
241 qreal offset = grid->cellWidth() * rowOffset;
242 if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
246 if (grid->verticalLayoutDirection() == QQuickItemView::BottomToTop)
247 y = -grid->cellHeight() - y;
249 return QPointF(x, y);
252 // Sets contentY (or contentX in TopToBottom flow) according to given row offset
253 // (in LeftToRight flow) or col offset (in TopToBottom).
254 bool setContentPos(QQuickGridView *gridview, qreal rowOrColOffset) {
255 bool contentPosChanged = (rowOrColOffset != 0);
256 qreal contentOffset = gridview->flow() == QQuickGridView::FlowLeftToRight
257 ? rowOrColOffset * gridview->cellHeight()
258 : rowOrColOffset * gridview->cellWidth();
260 if (gridview->flow() == QQuickGridView::FlowLeftToRight) {
261 if (gridview->verticalLayoutDirection() == QQuickItemView::BottomToTop)
262 contentOffset = -gridview->height() - contentOffset;
264 if (gridview->effectiveLayoutDirection() == Qt::RightToLeft)
265 contentOffset = -gridview->width() - contentOffset;
267 if (gridview->flow() == QQuickGridView::FlowLeftToRight)
268 gridview->setContentY(contentOffset);
270 gridview->setContentX(contentOffset);
271 return contentPosChanged;
275 QQuickView *getView() {
277 if (QString(QTest::currentTestFunction()) != testForView) {
281 m_view->setSource(QUrl());
286 testForView = QTest::currentTestFunction();
287 m_view = createView();
290 void releaseView(QQuickView *view) {
291 Q_ASSERT(view == m_view);
292 m_view->setSource(QUrl());
295 QQuickView *getView() {
298 void releaseView(QQuickView *view) {
307 tst_QQuickGridView::tst_QQuickGridView() : m_view(0)
311 void tst_QQuickGridView::init()
314 if (m_view && QString(QTest::currentTestFunction()) != testForView) {
315 testForView = QString();
322 void tst_QQuickGridView::items()
324 QQuickView *canvas = createView();
327 model.addItem("Fred", "12345");
328 model.addItem("John", "2345");
329 model.addItem("Bob", "54321");
330 model.addItem("Billy", "22345");
331 model.addItem("Sam", "2945");
332 model.addItem("Ben", "04321");
333 model.addItem("Jim", "0780");
335 QQmlContext *ctxt = canvas->rootContext();
336 ctxt->setContextProperty("testModel", &model);
338 canvas->setSource(testFileUrl("gridview1.qml"));
339 qApp->processEvents();
341 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
342 QTRY_VERIFY(gridview != 0);
344 QQuickItem *contentItem = gridview->contentItem();
345 QTRY_VERIFY(contentItem != 0);
347 QTRY_COMPARE(gridview->count(), model.count());
348 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
349 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
351 for (int i = 0; i < model.count(); ++i) {
352 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
353 QTRY_VERIFY(name != 0);
354 QTRY_COMPARE(name->text(), model.name(i));
355 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
356 QTRY_VERIFY(number != 0);
357 QTRY_COMPARE(number->text(), model.number(i));
360 // set an empty model and confirm that items are destroyed
362 ctxt->setContextProperty("testModel", &model2);
364 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
365 QTRY_VERIFY(itemCount == 0);
370 void tst_QQuickGridView::changed()
372 QQuickView *canvas = createView();
375 model.addItem("Fred", "12345");
376 model.addItem("John", "2345");
377 model.addItem("Bob", "54321");
378 model.addItem("Billy", "22345");
379 model.addItem("Sam", "2945");
380 model.addItem("Ben", "04321");
381 model.addItem("Jim", "0780");
383 QQmlContext *ctxt = canvas->rootContext();
384 ctxt->setContextProperty("testModel", &model);
386 canvas->setSource(testFileUrl("gridview1.qml"));
387 qApp->processEvents();
389 QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
390 QTRY_VERIFY(gridview != 0);
392 QQuickItem *contentItem = gridview->contentItem();
393 QTRY_VERIFY(contentItem != 0);
395 model.modifyItem(1, "Will", "9876");
396 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
397 QTRY_VERIFY(name != 0);
398 QTRY_COMPARE(name->text(), model.name(1));
399 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
400 QTRY_VERIFY(number != 0);
401 QTRY_COMPARE(number->text(), model.number(1));
406 void tst_QQuickGridView::inserted_basic()
408 QQuickView *canvas = createView();
412 model.addItem("Fred", "12345");
413 model.addItem("John", "2345");
414 model.addItem("Bob", "54321");
416 canvas->rootContext()->setContextProperty("testModel", &model);
417 canvas->setSource(testFileUrl("gridview1.qml"));
418 qApp->processEvents();
420 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
421 QTRY_VERIFY(gridview != 0);
423 QQuickItem *contentItem = gridview->contentItem();
424 QTRY_VERIFY(contentItem != 0);
426 model.insertItem(1, "Will", "9876");
428 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
429 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
431 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
432 QTRY_VERIFY(name != 0);
433 QTRY_COMPARE(name->text(), model.name(1));
434 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
435 QTRY_VERIFY(number != 0);
436 QTRY_COMPARE(number->text(), model.number(1));
438 // Checks that onAdd is called
439 int added = canvas->rootObject()->property("added").toInt();
440 QTRY_COMPARE(added, 1);
442 // Confirm items positioned correctly
443 for (int i = 0; i < model.count(); ++i) {
444 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
445 QTRY_COMPARE(item->x(), (i%3)*80.0);
446 QTRY_COMPARE(item->y(), (i/3)*60.0);
449 model.insertItem(0, "Foo", "1111"); // zero index, and current item
451 QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
453 name = findItem<QQuickText>(contentItem, "textName", 0);
454 QTRY_VERIFY(name != 0);
455 QTRY_COMPARE(name->text(), model.name(0));
456 number = findItem<QQuickText>(contentItem, "textNumber", 0);
457 QTRY_VERIFY(number != 0);
458 QTRY_COMPARE(number->text(), model.number(0));
460 QTRY_COMPARE(gridview->currentIndex(), 1);
462 // Confirm items positioned correctly
463 for (int i = 0; i < model.count(); ++i) {
464 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
465 QTRY_VERIFY(item->x() == (i%3)*80);
466 QTRY_VERIFY(item->y() == (i/3)*60);
469 for (int i = model.count(); i < 30; ++i)
470 model.insertItem(i, "Hello", QString::number(i));
472 gridview->setContentY(120);
474 // Insert item outside visible area
475 model.insertItem(1, "Hello", "1324");
477 QTRY_VERIFY(gridview->contentY() == 120);
482 void tst_QQuickGridView::inserted_defaultLayout(QQuickGridView::Flow flow,
483 Qt::LayoutDirection horizLayout,
484 QQuickItemView::VerticalLayoutDirection verticalLayout)
486 QFETCH(qreal, contentYRowOffset);
487 QFETCH(int, insertIndex);
488 QFETCH(int, insertCount);
489 QFETCH(int, insertIndex_ttb);
490 QFETCH(int, insertCount_ttb);
491 QFETCH(qreal, rowOffsetAfterMove);
494 for (int i = 0; i < 30; i++)
495 model.addItem("Item" + QString::number(i), "");
497 QQuickView *canvas = getView();
498 QQmlContext *ctxt = canvas->rootContext();
499 ctxt->setContextProperty("testModel", &model);
500 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
501 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
502 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
503 canvas->setSource(testFileUrl("layouts.qml"));
505 qApp->processEvents();
507 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
508 QTRY_VERIFY(gridview != 0);
509 QQuickItem *contentItem = gridview->contentItem();
510 QTRY_VERIFY(contentItem != 0);
512 if (flow == QQuickGridView::FlowTopToBottom) {
513 insertIndex = insertIndex_ttb;
514 insertCount = insertCount_ttb;
516 if (setContentPos(gridview, contentYRowOffset))
517 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
519 QList<QPair<QString, QString> > newData;
520 for (int i=0; i<insertCount; i++)
521 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
522 model.insertItems(insertIndex, newData);
523 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
525 // check visibleItems.first() is in correct position
526 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
528 QPointF firstPos(0, 0);
529 if (horizLayout == Qt::RightToLeft)
530 firstPos.rx() = flow == QQuickGridView::FlowLeftToRight ? gridview->width() - gridview->cellWidth() : -gridview->cellWidth();
531 if (verticalLayout == QQuickItemView::BottomToTop)
532 firstPos.ry() -= gridview->cellHeight();
533 QCOMPARE(item0->pos(), firstPos);
535 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
536 int firstVisibleIndex = -1;
537 for (int i=0; i<items.count(); i++) {
538 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
539 if (item && delegateVisible(item)) {
540 firstVisibleIndex = i;
544 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
546 // Confirm items positioned correctly and indexes correct
547 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
548 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
549 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
550 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
551 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
553 QCOMPARE(name->text(), model.name(i));
559 void tst_QQuickGridView::inserted_defaultLayout_data()
561 QTest::addColumn<qreal>("contentYRowOffset");
562 QTest::addColumn<int>("insertIndex");
563 QTest::addColumn<int>("insertCount");
564 QTest::addColumn<int>("insertIndex_ttb");
565 QTest::addColumn<int>("insertCount_ttb");
566 QTest::addColumn<qreal>("rowOffsetAfterMove");
568 QTest::newRow("add 1, before visible items")
572 << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
573 // new 1st visible item is at 0
575 QTest::newRow("add 2, before visible items")
579 << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
581 QTest::newRow("add 3, before visible items")
585 << -1.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
587 QTest::newRow("add 5, before visible items")
591 << -1.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
592 // grid is rearranged and first visible moves forward within its row
594 QTest::newRow("add 6, before visible items")
598 << -1.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
602 QTest::newRow("add 1, at start of visible, content at start")
608 QTest::newRow("add multiple, at start of visible, content at start")
614 QTest::newRow("add 1, at start of visible, content not at start")
620 QTest::newRow("add multiple, at start of visible, content not at start")
627 QTest::newRow("add 1, at end of visible, content at start")
633 QTest::newRow("add row, at end of visible, content at start")
639 QTest::newRow("add 1, at end of visible, content not at start")
645 QTest::newRow("add multiple, at end of visible, content not at start")
652 QTest::newRow("add 1, after visible, content at start")
658 QTest::newRow("add row, after visible, content at start")
664 QTest::newRow("add 1, after visible, content not at start")
670 QTest::newRow("add multiple, after visible, content not at start")
677 void tst_QQuickGridView::insertBeforeVisible()
679 QFETCH(int, insertIndex);
680 QFETCH(int, insertCount);
681 QFETCH(int, cacheBuffer);
684 QQuickView *canvas = getView();
687 for (int i = 0; i < 30; i++)
688 model.addItem("Item" + QString::number(i), "");
690 QQmlContext *ctxt = canvas->rootContext();
691 ctxt->setContextProperty("testModel", &model);
692 canvas->setSource(testFileUrl("gridview1.qml"));
694 qApp->processEvents();
696 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
697 QTRY_VERIFY(gridview != 0);
698 QQuickItem *contentItem = gridview->contentItem();
699 QTRY_VERIFY(contentItem != 0);
701 gridview->setCacheBuffer(cacheBuffer);
702 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
704 // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
705 int firstVisibleIndex = 12; // move to an index where the top item is not visible
706 gridview->setContentY(firstVisibleIndex/3 * 60.0);
707 gridview->setCurrentIndex(firstVisibleIndex);
708 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
710 QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
711 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
713 QCOMPARE(item->y(), gridview->contentY());
715 QList<QPair<QString, QString> > newData;
716 for (int i=0; i<insertCount; i++)
717 newData << qMakePair(QString("value %1").arg(i), QString::number(i));
718 model.insertItems(insertIndex, newData);
719 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
721 // now, moving to the top of the view should position the inserted items correctly
722 int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
723 gridview->setCurrentIndex(0);
724 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
725 QTRY_COMPARE(gridview->currentIndex(), 0);
726 QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
728 // Confirm items positioned correctly and indexes correct
729 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
730 for (int i = 0; i < model.count() && i < itemCount; ++i) {
731 item = findItem<QQuickItem>(contentItem, "wrapper", i);
732 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
733 QCOMPARE(item->x(), (i%3)*80.0);
734 QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
735 name = findItem<QQuickText>(contentItem, "textName", i);
737 QTRY_COMPARE(name->text(), model.name(i));
743 void tst_QQuickGridView::insertBeforeVisible_data()
745 QTest::addColumn<int>("insertIndex");
746 QTest::addColumn<int>("insertCount");
747 QTest::addColumn<int>("cacheBuffer");
749 QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
750 QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
751 QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
753 QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
754 QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
755 QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
757 QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
758 QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
759 QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
761 QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
762 QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
763 QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
766 void tst_QQuickGridView::removed_basic()
768 QQuickView *canvas = createView();
772 for (int i = 0; i < 40; i++)
773 model.addItem("Item" + QString::number(i), "");
775 canvas->rootContext()->setContextProperty("testModel", &model);
776 canvas->setSource(testFileUrl("gridview1.qml"));
777 qApp->processEvents();
779 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
780 QTRY_VERIFY(gridview != 0);
781 QQuickItem *contentItem = gridview->contentItem();
782 QTRY_VERIFY(contentItem != 0);
783 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
786 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
788 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
789 QTRY_VERIFY(name != 0);
790 QTRY_COMPARE(name->text(), model.name(1));
791 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
792 QTRY_VERIFY(number != 0);
793 QTRY_COMPARE(number->text(), model.number(1));
796 // Checks that onRemove is called
797 QString removed = canvas->rootObject()->property("removed").toString();
798 QTRY_COMPARE(removed, QString("Item1"));
800 // Confirm items positioned correctly
801 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
802 for (int i = 0; i < model.count() && i < itemCount; ++i) {
803 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
804 if (!item) qWarning() << "Item" << i << "not found";
805 QTRY_VERIFY(item->x() == (i%3)*80);
806 QTRY_VERIFY(item->y() == (i/3)*60);
809 // Remove first item (which is the current item);
811 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
813 name = findItem<QQuickText>(contentItem, "textName", 0);
814 QTRY_VERIFY(name != 0);
815 QTRY_COMPARE(name->text(), model.name(0));
816 number = findItem<QQuickText>(contentItem, "textNumber", 0);
817 QTRY_VERIFY(number != 0);
818 QTRY_COMPARE(number->text(), model.number(0));
821 // Confirm items positioned correctly
822 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
823 for (int i = 0; i < model.count() && i < itemCount; ++i) {
824 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
825 if (!item) qWarning() << "Item" << i << "not found";
826 QTRY_VERIFY(item->x() == (i%3)*80);
827 QTRY_VERIFY(item->y() == (i/3)*60);
830 // Remove items not visible
831 model.removeItem(25);
832 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
834 // Confirm items positioned correctly
835 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
836 for (int i = 0; i < model.count() && i < itemCount; ++i) {
837 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
838 if (!item) qWarning() << "Item" << i << "not found";
839 QTRY_VERIFY(item->x() == (i%3)*80);
840 QTRY_VERIFY(item->y() == (i/3)*60);
843 // Remove items before visible
844 gridview->setContentY(120);
845 gridview->setCurrentIndex(10);
847 // Setting currentIndex above shouldn't cause view to scroll
848 QTRY_COMPARE(gridview->contentY(), 120.0);
851 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
853 // Confirm items positioned correctly
854 for (int i = 6; i < 18; ++i) {
855 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
856 if (!item) qWarning() << "Item" << i << "not found";
857 QTRY_VERIFY(item->x() == (i%3)*80);
858 QTRY_VERIFY(item->y() == (i/3)*60);
861 // Remove currentIndex
862 QQuickItem *oldCurrent = gridview->currentItem();
864 QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
866 QTRY_COMPARE(gridview->currentIndex(), 9);
867 QTRY_VERIFY(gridview->currentItem() != oldCurrent);
869 gridview->setContentY(0);
870 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
872 // Confirm items positioned correctly
873 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
874 for (int i = 0; i < model.count() && i < itemCount; ++i) {
875 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
876 QTRY_VERIFY(item->x() == (i%3)*80);
877 QTRY_VERIFY(item->y() == (i/3)*60);
880 // remove item outside current view.
881 gridview->setCurrentIndex(32);
882 gridview->setContentY(240);
884 model.removeItem(30);
885 QTRY_VERIFY(gridview->currentIndex() == 31);
887 // remove current item beyond visible items.
888 gridview->setCurrentIndex(20);
889 gridview->setContentY(0);
890 model.removeItem(20);
892 QTRY_COMPARE(gridview->currentIndex(), 20);
893 QTRY_VERIFY(gridview->currentItem() != 0);
895 // remove item before current, but visible
896 gridview->setCurrentIndex(8);
897 gridview->setContentY(240);
898 oldCurrent = gridview->currentItem();
901 QTRY_COMPARE(gridview->currentIndex(), 7);
902 QTRY_VERIFY(gridview->currentItem() == oldCurrent);
907 void tst_QQuickGridView::removed_defaultLayout(QQuickGridView::Flow flow,
908 Qt::LayoutDirection horizLayout,
909 QQuickItemView::VerticalLayoutDirection verticalLayout)
911 QFETCH(qreal, contentYRowOffset);
912 QFETCH(int, removeIndex);
913 QFETCH(int, removeCount);
914 QFETCH(int, removeIndex_ttb);
915 QFETCH(int, removeCount_ttb);
916 QFETCH(qreal, rowOffsetAfterMove);
917 QFETCH(QString, firstVisible);
918 QFETCH(QString, firstVisible_ttb);
920 QQuickView *canvas = getView();
923 for (int i = 0; i < 30; i++)
924 model.addItem("Item" + QString::number(i), "");
926 QQmlContext *ctxt = canvas->rootContext();
927 ctxt->setContextProperty("testModel", &model);
928 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
929 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
930 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
931 canvas->setSource(testFileUrl("layouts.qml"));
933 qApp->processEvents();
935 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
936 QTRY_VERIFY(gridview != 0);
937 QQuickItem *contentItem = gridview->contentItem();
938 QTRY_VERIFY(contentItem != 0);
940 if (flow == QQuickGridView::FlowTopToBottom) {
941 removeIndex = removeIndex_ttb;
942 removeCount = removeCount_ttb;
943 firstVisible = firstVisible_ttb;
945 if (setContentPos(gridview, contentYRowOffset))
946 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
948 model.removeItems(removeIndex, removeCount);
949 QTRY_COMPARE(gridview->property("count").toInt(), model.count());
952 int firstVisibleIndex = -1;
953 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
954 QRectF viewRect(gridview->contentX(), gridview->contentY(), gridview->width(), gridview->height());
955 for (int i=0; i<items.count(); i++) {
956 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
958 QRectF itemRect(item->x(), item->y(), item->width(), item->height());
959 if (delegateVisible(item) && viewRect.intersects(itemRect)) {
960 firstVisibleIndex = i;
961 QQmlExpression en(qmlContext(item), item, "name");
962 firstName = en.evaluate().toString();
967 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
968 QCOMPARE(firstName, firstVisible);
970 // Confirm items positioned correctly and indexes correct
971 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
972 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
973 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
974 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
975 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
977 QTRY_COMPARE(name->text(), model.name(i));
983 void tst_QQuickGridView::removed_defaultLayout_data()
985 QTest::addColumn<qreal>("contentYRowOffset");
986 QTest::addColumn<int>("removeIndex");
987 QTest::addColumn<int>("removeCount");
988 QTest::addColumn<int>("removeIndex_ttb");
989 QTest::addColumn<int>("removeCount_ttb");
990 QTest::addColumn<qreal>("rowOffsetAfterMove");
991 QTest::addColumn<QString>("firstVisible");
992 QTest::addColumn<QString>("firstVisible_ttb");
994 QTest::newRow("remove 1, before visible items")
998 << 0.0 << "Item7" << "Item11";
1000 QTest::newRow("remove 1, before visible position")
1004 << 0.0 << "Item7" << "Item11";
1006 QTest::newRow("remove multiple (1 row), all before visible items")
1010 << 1.0 << "Item6" << "Item10"; // removed top row, slide down by 1 row
1012 QTest::newRow("remove multiple, all before visible items, remove item 0")
1016 << 1.0 << "Item7" << "Item11"; // removed top row, slide down by 1 row
1018 QTest::newRow("remove multiple rows, all before visible items")
1019 << 4.0 // show 12-29
1022 << 2.0 << "Item13" << "Item17";
1024 QTest::newRow("remove one row before visible, content y not on item border")
1028 << 1.0 << "Item3" << "Item5"; // 1 row removed
1030 QTest::newRow("remove mix of visible/non-visible")
1034 << 1.0 << "Item6" << "Item8"; // 1 row removed
1037 // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
1038 // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
1039 QTest::newRow("remove multiple, mix of items from before and within visible items")
1043 << 1.0 << "Item8" << "Item12"; // adjust for the 1 row removed before the visible
1045 QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1049 << 1.0 * 2 << "Item8" << "Item12"; // adjust for the 2 rows removed before the visible
1052 QTest::newRow("remove 1, from start of visible, content at start")
1056 << 0.0 << "Item1" << "Item1";
1058 QTest::newRow("remove multiple, from start of visible, content at start")
1062 << 0.0 << "Item3" << "Item5";
1064 QTest::newRow("remove 1, from start of visible, content not at start")
1068 << 0.0 << "Item7" << "Item11";
1070 QTest::newRow("remove multiple, from start of visible, content not at start")
1074 << 0.0 << "Item9" << "Item15";
1077 QTest::newRow("remove 1, from middle of visible, content at start")
1081 << 0.0 << "Item0" << "Item0";
1083 QTest::newRow("remove multiple, from middle of visible, content at start")
1087 << 0.0 << "Item0" << "Item0";
1089 QTest::newRow("remove 1, from middle of visible, content not at start")
1093 << 0.0 << "Item6" << "Item10";
1095 QTest::newRow("remove multiple, from middle of visible, content not at start")
1099 << 0.0 << "Item6" << "Item10";
1102 QTest::newRow("remove 1, after visible, content at start")
1106 << 0.0 << "Item0" << "Item0";
1108 QTest::newRow("remove multiple, after visible, content at start")
1112 << 0.0 << "Item0" << "Item0";
1114 QTest::newRow("remove 1, after visible, content not at start")
1118 << 0.0 << "Item6" << "Item10";
1120 QTest::newRow("remove multiple, after visible, content not at start")
1124 << 0.0 << "Item6" << "Item10";
1126 QTest::newRow("remove multiple, mix of items from within and after visible items")
1130 << 0.0 << "Item6" << "Item10";
1133 void tst_QQuickGridView::addOrRemoveBeforeVisible()
1135 // QTBUG-21588: ensure re-layout is done on grid after adding or removing
1136 // items from before the visible area
1138 QFETCH(bool, doAdd);
1139 QFETCH(qreal, newTopContentY);
1141 QQuickView *canvas = getView();
1145 for (int i = 0; i < 30; i++)
1146 model.addItem("Item" + QString::number(i), "");
1148 canvas->rootContext()->setContextProperty("testModel", &model);
1149 canvas->setSource(testFileUrl("gridview1.qml"));
1151 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1152 QTRY_VERIFY(gridview != 0);
1153 QQuickItem *contentItem = gridview->contentItem();
1154 QTRY_VERIFY(contentItem != 0);
1156 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1157 QTRY_COMPARE(name->text(), QString("Item0"));
1159 gridview->setCurrentIndex(0);
1160 qApp->processEvents();
1161 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1163 // scroll down until item 0 is no longer drawn
1164 // (bug not triggered if we just move using content y, since that doesn't
1165 // refill and change the visible items)
1166 gridview->setCurrentIndex(24);
1167 qApp->processEvents();
1169 QTRY_COMPARE(gridview->currentIndex(), 24);
1170 QTRY_COMPARE(gridview->contentY(), 220.0);
1172 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1173 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0)); // 0 shouldn't be visible
1176 model.insertItem(0, "New Item", "New Item number");
1177 QTRY_COMPARE(gridview->count(), 31);
1179 model.removeItem(0);
1180 QTRY_COMPARE(gridview->count(), 29);
1183 // scroll back up and item 0 should be gone
1184 gridview->setCurrentIndex(0);
1185 qApp->processEvents();
1186 QTRY_COMPARE(gridview->currentIndex(), 0);
1187 QTRY_COMPARE(gridview->contentY(), newTopContentY);
1189 name = findItem<QQuickText>(contentItem, "textName", 0);
1191 QCOMPARE(name->text(), QString("New Item"));
1193 QCOMPARE(name->text(), QString("Item1"));
1195 // Confirm items positioned correctly
1196 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1197 for (int i = 0; i < model.count() && i < itemCount; ++i) {
1198 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
1199 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1200 QTRY_VERIFY(item->x() == (i%3)*80);
1201 QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1204 releaseView(canvas);
1207 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1209 QTest::addColumn<bool>("doAdd");
1210 QTest::addColumn<qreal>("newTopContentY");
1212 QTest::newRow("add") << true << -60.0;
1213 QTest::newRow("remove") << false << -60.0;
1216 void tst_QQuickGridView::clear()
1218 QQuickView *canvas = createView();
1221 for (int i = 0; i < 30; i++)
1222 model.addItem("Item" + QString::number(i), "");
1224 canvas->rootContext()->setContextProperty("testModel", &model);
1225 canvas->setSource(testFileUrl("gridview1.qml"));
1227 qApp->processEvents();
1229 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1230 QVERIFY(gridview != 0);
1231 QQuickItem *contentItem = gridview->contentItem();
1232 QVERIFY(contentItem != 0);
1233 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1237 QVERIFY(gridview->count() == 0);
1238 QVERIFY(gridview->currentItem() == 0);
1239 QVERIFY(gridview->contentY() == 0);
1240 QVERIFY(gridview->currentIndex() == -1);
1241 QCOMPARE(gridview->contentHeight(), 0.0);
1243 // confirm sanity when adding an item to cleared list
1244 model.addItem("New", "1");
1245 QTRY_COMPARE(gridview->count(), 1);
1246 QVERIFY(gridview->currentItem() != 0);
1247 QVERIFY(gridview->currentIndex() == 0);
1252 void tst_QQuickGridView::moved_defaultLayout(QQuickGridView::Flow flow,
1253 Qt::LayoutDirection horizLayout,
1254 QQuickItemView::VerticalLayoutDirection verticalLayout)
1256 QFETCH(qreal, contentYRowOffset);
1260 QFETCH(int, from_ttb);
1261 QFETCH(int, to_ttb);
1262 QFETCH(int, count_ttb);
1263 QFETCH(qreal, rowOffsetAfterMove);
1265 QQuickView *canvas = getView();
1268 for (int i = 0; i < 30; i++)
1269 model.addItem("Item" + QString::number(i), "");
1271 QQmlContext *ctxt = canvas->rootContext();
1272 ctxt->setContextProperty("testModel", &model);
1273 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
1274 ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
1275 ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
1276 canvas->setSource(testFileUrl("layouts.qml"));
1278 qApp->processEvents();
1280 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1281 QTRY_VERIFY(gridview != 0);
1282 QQuickItem *contentItem = gridview->contentItem();
1283 QTRY_VERIFY(contentItem != 0);
1284 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1286 QQuickItem *currentItem = gridview->currentItem();
1287 QTRY_VERIFY(currentItem != 0);
1289 if (flow == QQuickGridView::FlowTopToBottom) {
1294 if (setContentPos(gridview, contentYRowOffset))
1295 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1297 model.moveItems(from, to, count);
1298 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1300 // Confirm items positioned correctly and indexes correct
1301 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1302 int firstVisibleIndex = -1;
1303 for (int i=0; i<items.count(); i++) {
1304 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1305 if (item && delegateVisible(item)) {
1306 firstVisibleIndex = i;
1310 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1312 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1313 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1315 ( (flow == QQuickGridView::FlowLeftToRight && i >= firstVisibleIndex + (3*6))
1316 || flow == QQuickGridView::FlowTopToBottom && i >= firstVisibleIndex + (5*3) ) ) {
1317 continue; // index has moved out of view
1319 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1320 QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
1321 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1323 QTRY_COMPARE(name->text(), model.name(i));
1325 // current index should have been updated
1326 if (item == currentItem)
1327 QTRY_COMPARE(gridview->currentIndex(), i);
1330 releaseView(canvas);
1333 void tst_QQuickGridView::moved_defaultLayout_data()
1335 QTest::addColumn<qreal>("contentYRowOffset");
1336 QTest::addColumn<int>("from");
1337 QTest::addColumn<int>("to");
1338 QTest::addColumn<int>("count");
1339 QTest::addColumn<int>("from_ttb");
1340 QTest::addColumn<int>("to_ttb");
1341 QTest::addColumn<int>("count_ttb");
1342 QTest::addColumn<qreal>("rowOffsetAfterMove");
1344 // model starts with 30 items, each 80x60, in area 240x320
1345 // 18 items should be visible at a time
1347 // The first visible item should not move upwards and out of the view
1348 // if items are moved/removed before it.
1351 QTest::newRow("move 1 forwards, within visible items")
1357 QTest::newRow("move 1 forwards, from non-visible -> visible")
1363 QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1364 << 2.0 // // show 6-23
1369 QTest::newRow("move 1 forwards, from visible -> non-visible")
1375 QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1382 QTest::newRow("move 1 backwards, within visible items")
1388 QTest::newRow("move 1 backwards, within visible items (to first index)")
1394 QTest::newRow("move 1 backwards, from non-visible -> visible")
1400 QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1406 QTest::newRow("move 1 backwards, from visible -> non-visible")
1410 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1412 QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1416 << 0.0; // only 1 item moved back, so items shift accordingly and first row doesn't move
1419 QTest::newRow("move multiple forwards, within visible items")
1425 QTest::newRow("move multiple backwards, within visible items (move first item)")
1431 QTest::newRow("move multiple forwards, before visible items")
1433 << 3 << 4 << 3 // 3, 4, 5 move to after 6
1435 << 1.0; // row of 3,4,5 has moved down
1437 QTest::newRow("move multiple forwards, from non-visible -> visible")
1441 << 1.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1443 QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1447 << 1.0; // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1449 QTest::newRow("move multiple forwards, mix of non-visible/visible")
1453 << 1.0; // top two rows removed, third row is now the first visible
1455 QTest::newRow("move multiple forwards, to bottom of view")
1461 QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1467 QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1473 QTest::newRow("move multiple forwards, from visible -> non-visible")
1479 QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1486 QTest::newRow("move multiple backwards, within visible items")
1492 QTest::newRow("move multiple backwards, from non-visible -> visible")
1498 QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1504 QTest::newRow("move multiple backwards, from visible -> non-visible")
1508 << -1.0; // to minimize movement, items are added above visible area, all items move up by 1 row
1510 QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1514 << -1.0; // 16,17,18 move to above item 0, all items move up by 1 row
1517 void tst_QQuickGridView::multipleChanges(bool condensed)
1519 QFETCH(int, startCount);
1520 QFETCH(QList<ListChange>, changes);
1521 QFETCH(int, newCount);
1522 QFETCH(int, newCurrentIndex);
1524 QQuickView *canvas = getView();
1527 for (int i = 0; i < startCount; i++)
1528 model.addItem("Item" + QString::number(i), "");
1530 canvas->rootContext()->setContextProperty("testModel", &model);
1531 canvas->setSource(testFileUrl("gridview1.qml"));
1533 qApp->processEvents();
1535 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1536 QTRY_VERIFY(gridview != 0);
1537 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1539 for (int i=0; i<changes.count(); i++) {
1540 switch (changes[i].type) {
1541 case ListChange::Inserted:
1543 QList<QPair<QString, QString> > items;
1544 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1545 items << qMakePair(QString("new item " + j), QString::number(j));
1546 model.insertItems(changes[i].index, items);
1549 case ListChange::Removed:
1550 model.removeItems(changes[i].index, changes[i].count);
1552 case ListChange::Moved:
1553 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1555 case ListChange::SetCurrent:
1556 gridview->setCurrentIndex(changes[i].index);
1558 case ListChange::SetContentY:
1559 gridview->setContentY(changes[i].pos);
1563 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1566 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1568 QCOMPARE(gridview->count(), newCount);
1569 QCOMPARE(gridview->count(), model.count());
1570 QCOMPARE(gridview->currentIndex(), newCurrentIndex);
1574 QQuickItem *contentItem = gridview->contentItem();
1575 QTRY_VERIFY(contentItem != 0);
1576 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1577 for (int i=0; i < model.count() && i < itemCount; ++i) {
1578 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1579 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1580 name = findItem<QQuickText>(contentItem, "textName", i);
1582 QTRY_COMPARE(name->text(), model.name(i));
1583 number = findItem<QQuickText>(contentItem, "textNumber", i);
1584 QVERIFY(number != 0);
1585 QTRY_COMPARE(number->text(), model.number(i));
1588 releaseView(canvas);
1591 void tst_QQuickGridView::multipleChanges_data()
1593 QTest::addColumn<int>("startCount");
1594 QTest::addColumn<QList<ListChange> >("changes");
1595 QTest::addColumn<int>("newCount");
1596 QTest::addColumn<int>("newCurrentIndex");
1598 QList<ListChange> changes;
1600 for (int i=1; i<30; i++)
1601 changes << ListChange::remove(0);
1602 QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1604 changes << ListChange::remove(0);
1605 QTest::newRow("remove all") << 30 << changes << 0 << -1;
1608 changes << ListChange::setCurrent(29);
1609 for (int i=29; i>0; i--)
1610 changes << ListChange::remove(i);
1611 QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1613 QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1614 << ListChange::remove(0, 1)
1615 << ListChange::insert(0, 1)
1618 QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1619 << ListChange::setCurrent(2)
1620 << ListChange::remove(2, 1)
1621 << ListChange::insert(2, 1)
1624 QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1625 << ListChange::setCurrent(1)
1626 << ListChange::remove(1, 3)
1627 << ListChange::insert(2, 2)
1630 QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1631 << ListChange::setCurrent(2)
1632 << ListChange::remove(1, 3)
1633 << ListChange::move(1, 5, 1)
1636 QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1637 << ListChange::setCurrent(5)
1638 << ListChange::remove(4, 3)
1639 << ListChange::move(4, 1, 1)
1643 QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1644 << ListChange::insert(0, 2)
1645 << ListChange::insert(0, 4)
1646 << ListChange::insert(0, 6)
1649 QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1650 << ListChange::insert(0, 2)
1651 << ListChange::insert(0, 4)
1652 << ListChange::insert(0, 6)
1653 << ListChange::setCurrent(3)
1654 << ListChange::insert(3, 2)
1657 QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1658 << ListChange::insert(0, 30)
1659 << ListChange::remove(0, 30)
1662 QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1663 << ListChange::insert(1)
1664 << ListChange::setCurrent(1)
1665 << ListChange::remove(1)
1668 QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1669 << ListChange::insert(0, 10)
1670 << ListChange::remove(5, 10)
1673 QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1674 << ListChange::insert(0, 3)
1675 << ListChange::move(0, 10, 3)
1678 QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1679 << ListChange::insert(0, 3)
1680 << ListChange::move(0, 8, 5)
1683 QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1684 << ListChange::setCurrent(9)
1685 << ListChange::insert(10, 3)
1686 << ListChange::move(8, 0, 5)
1690 QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1691 << ListChange::setCurrent(1)
1692 << ListChange::move(1, 2, 2)
1693 << ListChange::move(2, 1, 2)
1696 QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1697 << ListChange::setCurrent(2)
1698 << ListChange::move(1, 2, 3)
1699 << ListChange::move(3, 0, 5)
1702 QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1703 << ListChange::setCurrent(5)
1704 << ListChange::move(5, 0, 1)
1705 << ListChange::remove(0)
1708 QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1709 << ListChange::setCurrent(5)
1710 << ListChange::move(5, 0, 1)
1711 << ListChange::insert(0)
1714 QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1715 << ListChange::setCurrent(1)
1716 << ListChange::move(5, 1, 3)
1717 << ListChange::remove(1, 3)
1720 QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1721 << ListChange::setCurrent(5)
1722 << ListChange::move(5, 1, 3)
1723 << ListChange::insert(1, 5)
1726 QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1727 << ListChange::setCurrent(3)
1728 << ListChange::move(0, 1, 2)
1729 << ListChange::insert(3, 5)
1733 QTest::newRow("clear current") << 0 << (QList<ListChange>()
1734 << ListChange::insert(0, 5)
1735 << ListChange::setCurrent(-1)
1736 << ListChange::remove(0, 5)
1737 << ListChange::insert(0, 5)
1740 QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1741 << ListChange::remove(20, 5)
1742 << ListChange::setContentY(20)
1745 QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1746 << ListChange::insert(9, 5)
1747 << ListChange::setContentY(20)
1750 QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1751 << ListChange::move(15, 8, 3)
1752 << ListChange::setContentY(0)
1755 QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1756 << ListChange::setContentY(20)
1757 << ListChange::remove(0, 30)
1758 << ListChange::insert(0, 2)
1759 << ListChange::setContentY(0)
1764 void tst_QQuickGridView::swapWithFirstItem()
1767 QQuickView *canvas = createView();
1770 for (int i = 0; i < 30; i++)
1771 model.addItem("Item" + QString::number(i), "");
1773 canvas->rootContext()->setContextProperty("testModel", &model);
1774 canvas->setSource(testFileUrl("gridview1.qml"));
1776 qApp->processEvents();
1778 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1779 QTRY_VERIFY(gridview != 0);
1781 // ensure content position is stable
1782 gridview->setContentY(0);
1783 model.moveItem(10, 0);
1784 QTRY_VERIFY(gridview->contentY() == 0);
1789 void tst_QQuickGridView::currentIndex()
1792 for (int i = 0; i < 60; i++)
1793 model.addItem("Item" + QString::number(i), QString::number(i));
1795 QQuickView *canvas = new QQuickView(0);
1796 canvas->setGeometry(0,0,240,320);
1799 QQmlContext *ctxt = canvas->rootContext();
1800 ctxt->setContextProperty("testModel", &model);
1802 QString filename(testFile("gridview-initCurrent.qml"));
1803 canvas->setSource(QUrl::fromLocalFile(filename));
1805 qApp->processEvents();
1807 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1808 QVERIFY(gridview != 0);
1809 QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1811 QQuickItem *contentItem = gridview->contentItem();
1812 QVERIFY(contentItem != 0);
1814 // current item should be third item
1815 QCOMPARE(gridview->currentIndex(), 35);
1816 QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1817 QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1818 QCOMPARE(gridview->contentY(), 400.0);
1820 gridview->setCurrentIndex(0);
1821 QCOMPARE(gridview->currentIndex(), 0);
1822 // confirm that the velocity is updated
1823 QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1825 // footer should become visible if it is out of view, and then current index moves to the first row
1826 canvas->rootObject()->setProperty("showFooter", true);
1827 QTRY_VERIFY(gridview->footerItem());
1828 gridview->setCurrentIndex(model.count()-3);
1829 QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1830 gridview->setCurrentIndex(model.count()-2);
1831 QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1832 canvas->rootObject()->setProperty("showFooter", false);
1834 // header should become visible if it is out of view, and then current index moves to the last row
1835 canvas->rootObject()->setProperty("showHeader", true);
1836 QTRY_VERIFY(gridview->headerItem());
1837 gridview->setCurrentIndex(3);
1838 QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1839 gridview->setCurrentIndex(1);
1840 QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1841 canvas->rootObject()->setProperty("showHeader", false);
1843 // turn off auto highlight
1844 gridview->setHighlightFollowsCurrentItem(false);
1845 QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1846 QVERIFY(gridview->highlightItem());
1847 qreal hlPosX = gridview->highlightItem()->x();
1848 qreal hlPosY = gridview->highlightItem()->y();
1850 gridview->setCurrentIndex(5);
1851 QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1852 QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1854 // insert item before currentIndex
1855 gridview->setCurrentIndex(28);
1856 model.insertItem(0, "Foo", "1111");
1857 QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1859 // check removing highlight by setting currentIndex to -1;
1860 gridview->setCurrentIndex(-1);
1862 QCOMPARE(gridview->currentIndex(), -1);
1863 QVERIFY(!gridview->highlightItem());
1864 QVERIFY(!gridview->currentItem());
1866 // moving currentItem out of view should make it invisible
1867 gridview->setCurrentIndex(0);
1868 QTRY_VERIFY(delegateVisible(gridview->currentItem()));
1869 gridview->setContentY(200);
1870 QTRY_VERIFY(!delegateVisible(gridview->currentItem()));
1875 void tst_QQuickGridView::noCurrentIndex()
1878 for (int i = 0; i < 60; i++)
1879 model.addItem("Item" + QString::number(i), QString::number(i));
1881 QQuickView *canvas = new QQuickView(0);
1882 canvas->setGeometry(0,0,240,320);
1884 QQmlContext *ctxt = canvas->rootContext();
1885 ctxt->setContextProperty("testModel", &model);
1887 QString filename(testFile("gridview-noCurrent.qml"));
1888 canvas->setSource(QUrl::fromLocalFile(filename));
1890 qApp->processEvents();
1892 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1893 QVERIFY(gridview != 0);
1894 QQuickItem *contentItem = gridview->contentItem();
1895 QVERIFY(contentItem != 0);
1896 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1898 // current index should be -1
1899 QCOMPARE(gridview->currentIndex(), -1);
1900 QVERIFY(!gridview->currentItem());
1901 QVERIFY(!gridview->highlightItem());
1902 QCOMPARE(gridview->contentY(), 0.0);
1904 gridview->setCurrentIndex(5);
1905 QCOMPARE(gridview->currentIndex(), 5);
1906 QVERIFY(gridview->currentItem());
1907 QVERIFY(gridview->highlightItem());
1912 void tst_QQuickGridView::keyNavigation()
1914 QFETCH(QQuickGridView::Flow, flow);
1915 QFETCH(Qt::LayoutDirection, layoutDirection);
1916 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
1917 QFETCH(Qt::Key, forwardsKey);
1918 QFETCH(Qt::Key, backwardsKey);
1919 QFETCH(QPointF, contentPosAtFirstItem);
1920 QFETCH(QPointF, contentPosAtLastItem);
1921 QFETCH(int, indexRightOf7);
1922 QFETCH(int, indexLeftOf7);
1923 QFETCH(int, indexUpFrom7);
1924 QFETCH(int, indexDownFrom7);
1927 for (int i = 0; i < 18; i++)
1928 model.addItem("Item" + QString::number(i), "");
1930 QQuickView *canvas = getView();
1931 canvas->rootContext()->setContextProperty("testModel", &model);
1932 canvas->setSource(testFileUrl("gridview1.qml"));
1934 qApp->processEvents();
1936 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1937 QTRY_VERIFY(gridview != 0);
1938 gridview->setFlow(flow);
1939 gridview->setLayoutDirection(layoutDirection);
1940 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
1941 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1943 canvas->requestActivateWindow();
1944 QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1945 QCOMPARE(gridview->currentIndex(), 0);
1947 QTest::keyClick(canvas, forwardsKey);
1948 QCOMPARE(gridview->currentIndex(), 1);
1950 QTest::keyClick(canvas, backwardsKey);
1951 QCOMPARE(gridview->currentIndex(), 0);
1953 gridview->setCurrentIndex(7);
1954 gridview->moveCurrentIndexRight();
1955 QCOMPARE(gridview->currentIndex(), indexRightOf7);
1956 gridview->moveCurrentIndexLeft();
1957 QCOMPARE(gridview->currentIndex(), 7);
1958 gridview->moveCurrentIndexLeft();
1959 QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1960 gridview->moveCurrentIndexRight();
1961 QCOMPARE(gridview->currentIndex(), 7);
1962 gridview->moveCurrentIndexUp();
1963 QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1964 gridview->moveCurrentIndexDown();
1965 QCOMPARE(gridview->currentIndex(), 7);
1966 gridview->moveCurrentIndexDown();
1967 QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1969 gridview->setCurrentIndex(7);
1970 QTest::keyClick(canvas, Qt::Key_Right);
1971 QCOMPARE(gridview->currentIndex(), indexRightOf7);
1972 QTest::keyClick(canvas, Qt::Key_Left);
1973 QCOMPARE(gridview->currentIndex(), 7);
1974 QTest::keyClick(canvas, Qt::Key_Left);
1975 QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1976 QTest::keyClick(canvas, Qt::Key_Right);
1977 QCOMPARE(gridview->currentIndex(), 7);
1978 QTest::keyClick(canvas, Qt::Key_Up);
1979 QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1980 QTest::keyClick(canvas, Qt::Key_Down);
1981 QCOMPARE(gridview->currentIndex(), 7);
1982 QTest::keyClick(canvas, Qt::Key_Down);
1983 QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1985 // hold down a key to go forwards
1986 gridview->setCurrentIndex(0);
1987 for (int i=0; i<model.count()-1; i++) {
1988 // QTest::qWait(500);
1989 QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
1990 QTRY_COMPARE(gridview->currentIndex(), i+1);
1992 QTest::keyRelease(canvas, forwardsKey);
1993 QTRY_COMPARE(gridview->currentIndex(), model.count()-1);
1994 QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
1995 QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
1997 // hold down a key to go backwards
1998 for (int i=model.count()-1; i > 0; i--) {
1999 QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
2000 QTRY_COMPARE(gridview->currentIndex(), i-1);
2002 QTest::keyRelease(canvas, backwardsKey);
2003 QTRY_COMPARE(gridview->currentIndex(), 0);
2004 QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2005 QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2008 QVERIFY(!gridview->isWrapEnabled());
2009 QTest::keyClick(canvas, forwardsKey);
2010 QCOMPARE(gridview->currentIndex(), 1);
2011 QTest::keyClick(canvas, backwardsKey);
2012 QCOMPARE(gridview->currentIndex(), 0);
2014 QTest::keyClick(canvas, backwardsKey);
2015 QCOMPARE(gridview->currentIndex(), 0);
2018 gridview->setWrapEnabled(true);
2019 QVERIFY(gridview->isWrapEnabled());
2021 QTest::keyClick(canvas, backwardsKey);
2022 QCOMPARE(gridview->currentIndex(), model.count()-1);
2023 QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
2024 QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
2026 QTest::keyClick(canvas, forwardsKey);
2027 QCOMPARE(gridview->currentIndex(), 0);
2028 QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2029 QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2031 // Test key press still accepted when position wraps to same index.
2032 canvas->rootObject()->setProperty("lastKey", 0);
2033 model.removeItems(1, model.count() - 1);
2035 QTest::keyClick(canvas, backwardsKey);
2036 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), 0);
2038 QTest::keyClick(canvas, forwardsKey);
2039 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), 0);
2041 // Test key press not accepted at limits when wrap is disabled.
2042 gridview->setWrapEnabled(false);
2044 QTest::keyClick(canvas, backwardsKey);
2045 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), int(backwardsKey));
2047 QTest::keyClick(canvas, forwardsKey);
2048 QCOMPARE(canvas->rootObject()->property("lastKey").toInt(), int(forwardsKey));
2050 releaseView(canvas);
2053 void tst_QQuickGridView::keyNavigation_data()
2055 QTest::addColumn<QQuickGridView::Flow>("flow");
2056 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2057 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2058 QTest::addColumn<Qt::Key>("forwardsKey");
2059 QTest::addColumn<Qt::Key>("backwardsKey");
2060 QTest::addColumn<QPointF>("contentPosAtFirstItem");
2061 QTest::addColumn<QPointF>("contentPosAtLastItem");
2062 QTest::addColumn<int>("indexRightOf7");
2063 QTest::addColumn<int>("indexLeftOf7");
2064 QTest::addColumn<int>("indexUpFrom7");
2065 QTest::addColumn<int>("indexDownFrom7");
2067 QTest::newRow("LeftToRight, LtR, TtB")
2068 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
2069 << Qt::Key_Right << Qt::Key_Left
2072 << 8 << 6 << 4 << 10;
2074 QTest::newRow("LeftToRight, RtL, TtB")
2075 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
2076 << Qt::Key_Left << Qt::Key_Right
2079 << 6 << 8 << 4 << 10;
2081 QTest::newRow("LeftToRight, LtR, BtT")
2082 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
2083 << Qt::Key_Right << Qt::Key_Left
2086 << 8 << 6 << 10 << 4;
2088 QTest::newRow("LeftToRight, RtL, BtT")
2089 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
2090 << Qt::Key_Left << Qt::Key_Right
2093 << 6 << 8 << 10 << 4;
2095 QTest::newRow("TopToBottom, LtR, TtB")
2096 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
2097 << Qt::Key_Down << Qt::Key_Up
2100 << 12 << 2 << 6 << 8;
2102 QTest::newRow("TopToBottom, RtL, TtB")
2103 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
2104 << Qt::Key_Down << Qt::Key_Up
2107 << 2 << 12 << 6 << 8;
2109 QTest::newRow("TopToBottom, LtR, BtT")
2110 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
2111 << Qt::Key_Up << Qt::Key_Down
2113 << QPointF(80, -320)
2114 << 12 << 2 << 8 << 6;
2116 QTest::newRow("TopToBottom, RtL, BtT")
2117 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
2118 << Qt::Key_Up << Qt::Key_Down
2119 << QPointF(-240, -320)
2120 << QPointF(-320, -320)
2121 << 2 << 12 << 8 << 6;
2124 void tst_QQuickGridView::changeFlow()
2126 QQuickView *canvas = createView();
2129 for (int i = 0; i < 30; i++)
2130 model.addItem("Item" + QString::number(i), QString::number(i));
2132 QQmlContext *ctxt = canvas->rootContext();
2133 ctxt->setContextProperty("testModel", &model);
2134 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2135 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2136 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2138 canvas->setSource(testFileUrl("layouts.qml"));
2139 qApp->processEvents();
2141 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2142 QTRY_VERIFY(gridview != 0);
2144 QQuickItem *contentItem = gridview->contentItem();
2145 QTRY_VERIFY(contentItem != 0);
2147 // Confirm items positioned correctly and indexes correct
2148 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2149 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2150 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2151 if (!item) qWarning() << "Item" << i << "not found";
2153 QTRY_COMPARE(item->x(), qreal((i%3)*80));
2154 QTRY_COMPARE(item->y(), qreal((i/3)*60));
2155 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2156 QTRY_VERIFY(name != 0);
2157 QTRY_COMPARE(name->text(), model.name(i));
2158 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2159 QTRY_VERIFY(number != 0);
2160 QTRY_COMPARE(number->text(), model.number(i));
2163 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2165 // Confirm items positioned correctly and indexes correct
2166 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2167 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2168 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2169 if (!item) qWarning() << "Item" << i << "not found";
2171 QTRY_COMPARE(item->x(), qreal((i/5)*80));
2172 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2173 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2174 QTRY_VERIFY(name != 0);
2175 QTRY_COMPARE(name->text(), model.name(i));
2176 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2177 QTRY_VERIFY(number != 0);
2178 QTRY_COMPARE(number->text(), model.number(i));
2181 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2183 // Confirm items positioned correctly and indexes correct
2184 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2185 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2186 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2187 if (!item) qWarning() << "Item" << i << "not found";
2189 QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
2190 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2191 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2192 QTRY_VERIFY(name != 0);
2193 QTRY_COMPARE(name->text(), model.name(i));
2194 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2195 QTRY_VERIFY(number != 0);
2196 QTRY_COMPARE(number->text(), model.number(i));
2198 gridview->setContentX(100);
2199 QTRY_COMPARE(gridview->contentX(), 100.);
2200 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2201 QTRY_COMPARE(gridview->contentX(), 0.);
2203 // Confirm items positioned correctly and indexes correct
2204 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2205 for (int i = 0; i < model.count() && i < itemCount; ++i) {
2206 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2207 if (!item) qWarning() << "Item" << i << "not found";
2209 QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
2210 QTRY_COMPARE(item->y(), qreal((i/3)*60));
2211 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2212 QTRY_VERIFY(name != 0);
2213 QTRY_COMPARE(name->text(), model.name(i));
2214 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2215 QTRY_VERIFY(number != 0);
2216 QTRY_COMPARE(number->text(), model.number(i));
2222 void tst_QQuickGridView::defaultValues()
2225 QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
2226 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2228 QTRY_VERIFY(obj != 0);
2229 QTRY_VERIFY(obj->model() == QVariant());
2230 QTRY_VERIFY(obj->delegate() == 0);
2231 QTRY_COMPARE(obj->currentIndex(), -1);
2232 QTRY_VERIFY(obj->currentItem() == 0);
2233 QTRY_COMPARE(obj->count(), 0);
2234 QTRY_VERIFY(obj->highlight() == 0);
2235 QTRY_VERIFY(obj->highlightItem() == 0);
2236 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
2237 QTRY_VERIFY(obj->flow() == 0);
2238 QTRY_COMPARE(obj->isWrapEnabled(), false);
2239 QTRY_COMPARE(obj->cacheBuffer(), 0);
2240 QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
2241 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2245 void tst_QQuickGridView::properties()
2248 QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
2249 QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2251 QTRY_VERIFY(obj != 0);
2252 QTRY_VERIFY(obj->model() != QVariant());
2253 QTRY_VERIFY(obj->delegate() != 0);
2254 QTRY_COMPARE(obj->currentIndex(), 0);
2255 QTRY_VERIFY(obj->currentItem() != 0);
2256 QTRY_COMPARE(obj->count(), 4);
2257 QTRY_VERIFY(obj->highlight() != 0);
2258 QTRY_VERIFY(obj->highlightItem() != 0);
2259 QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
2260 QTRY_VERIFY(obj->flow() == 0);
2261 QTRY_COMPARE(obj->isWrapEnabled(), true);
2262 QTRY_COMPARE(obj->cacheBuffer(), 200);
2263 QTRY_COMPARE(obj->cellWidth(), qreal(100));
2264 QTRY_COMPARE(obj->cellHeight(), qreal(100));
2268 void tst_QQuickGridView::propertyChanges()
2270 QQuickView *canvas = createView();
2271 QTRY_VERIFY(canvas);
2272 canvas->setSource(testFileUrl("propertychangestest.qml"));
2274 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2275 QTRY_VERIFY(gridView);
2277 QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
2278 QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
2279 QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
2280 QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
2282 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2283 QTRY_COMPARE(gridView->cacheBuffer(), 10);
2284 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2286 gridView->setWrapEnabled(false);
2287 gridView->setCacheBuffer(3);
2288 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2290 QTRY_COMPARE(gridView->isWrapEnabled(), false);
2291 QTRY_COMPARE(gridView->cacheBuffer(), 3);
2292 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2294 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2295 QTRY_COMPARE(cacheBufferSpy.count(),1);
2296 QTRY_COMPARE(flowSpy.count(),1);
2298 gridView->setWrapEnabled(false);
2299 gridView->setCacheBuffer(3);
2300 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2302 QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2303 QTRY_COMPARE(cacheBufferSpy.count(),1);
2304 QTRY_COMPARE(flowSpy.count(),1);
2306 gridView->setFlow(QQuickGridView::FlowLeftToRight);
2307 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2309 gridView->setWrapEnabled(true);
2310 gridView->setCacheBuffer(5);
2311 gridView->setLayoutDirection(Qt::RightToLeft);
2313 QTRY_COMPARE(gridView->isWrapEnabled(), true);
2314 QTRY_COMPARE(gridView->cacheBuffer(), 5);
2315 QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2317 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2318 QTRY_COMPARE(cacheBufferSpy.count(),2);
2319 QTRY_COMPARE(layoutSpy.count(),1);
2320 QTRY_COMPARE(flowSpy.count(),2);
2322 gridView->setWrapEnabled(true);
2323 gridView->setCacheBuffer(5);
2324 gridView->setLayoutDirection(Qt::RightToLeft);
2326 QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2327 QTRY_COMPARE(cacheBufferSpy.count(),2);
2328 QTRY_COMPARE(layoutSpy.count(),1);
2329 QTRY_COMPARE(flowSpy.count(),2);
2331 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2332 QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2333 QTRY_COMPARE(flowSpy.count(),3);
2335 gridView->setFlow(QQuickGridView::FlowTopToBottom);
2336 QTRY_COMPARE(flowSpy.count(),3);
2341 void tst_QQuickGridView::componentChanges()
2343 QQuickView *canvas = createView();
2344 QTRY_VERIFY(canvas);
2345 canvas->setSource(testFileUrl("propertychangestest.qml"));
2347 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2348 QTRY_VERIFY(gridView);
2350 QQmlComponent component(canvas->engine());
2351 component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2353 QQmlComponent delegateComponent(canvas->engine());
2354 delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2356 QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2357 QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2358 QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2359 QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2360 QSignalSpy headerItemSpy(gridView, SIGNAL(headerItemChanged()));
2361 QSignalSpy footerItemSpy(gridView, SIGNAL(footerItemChanged()));
2363 gridView->setHighlight(&component);
2364 gridView->setDelegate(&delegateComponent);
2365 gridView->setHeader(&component);
2366 gridView->setFooter(&component);
2368 QTRY_COMPARE(gridView->highlight(), &component);
2369 QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2370 QTRY_COMPARE(gridView->header(), &component);
2371 QTRY_COMPARE(gridView->footer(), &component);
2373 QVERIFY(gridView->headerItem());
2374 QVERIFY(gridView->footerItem());
2376 QTRY_COMPARE(highlightSpy.count(),1);
2377 QTRY_COMPARE(delegateSpy.count(),1);
2378 QTRY_COMPARE(headerSpy.count(),1);
2379 QTRY_COMPARE(footerSpy.count(),1);
2380 QTRY_COMPARE(headerItemSpy.count(),1);
2381 QTRY_COMPARE(footerItemSpy.count(),1);
2383 gridView->setHighlight(&component);
2384 gridView->setDelegate(&delegateComponent);
2385 gridView->setHeader(&component);
2386 gridView->setFooter(&component);
2388 QTRY_COMPARE(highlightSpy.count(),1);
2389 QTRY_COMPARE(delegateSpy.count(),1);
2390 QTRY_COMPARE(headerSpy.count(),1);
2391 QTRY_COMPARE(footerSpy.count(),1);
2392 QTRY_COMPARE(headerItemSpy.count(),1);
2393 QTRY_COMPARE(footerItemSpy.count(),1);
2398 void tst_QQuickGridView::modelChanges()
2400 QQuickView *canvas = createView();
2401 QTRY_VERIFY(canvas);
2402 canvas->setSource(testFileUrl("propertychangestest.qml"));
2404 QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2405 QTRY_VERIFY(gridView);
2407 QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2408 QTRY_VERIFY(alternateModel);
2409 QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2410 QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2412 gridView->setModel(modelVariant);
2413 QTRY_COMPARE(gridView->model(), modelVariant);
2414 QTRY_COMPARE(modelSpy.count(),1);
2416 gridView->setModel(modelVariant);
2417 QTRY_COMPARE(modelSpy.count(),1);
2419 gridView->setModel(QVariant());
2420 QTRY_COMPARE(modelSpy.count(),2);
2424 void tst_QQuickGridView::positionViewAtIndex()
2426 QQuickView *canvas = createView();
2429 for (int i = 0; i < 40; i++)
2430 model.addItem("Item" + QString::number(i), "");
2432 QQmlContext *ctxt = canvas->rootContext();
2433 ctxt->setContextProperty("testModel", &model);
2434 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2435 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2436 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2438 canvas->setSource(testFileUrl("layouts.qml"));
2440 qApp->processEvents();
2442 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2443 QTRY_VERIFY(gridview != 0);
2444 QQuickItem *contentItem = gridview->contentItem();
2445 QTRY_VERIFY(contentItem != 0);
2446 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2448 // Confirm items positioned correctly
2449 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2450 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2451 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2452 if (!item) qWarning() << "Item" << i << "not found";
2454 QTRY_COMPARE(item->x(), (i%3)*80.);
2455 QTRY_COMPARE(item->y(), (i/3)*60.);
2458 // Position on a currently visible item
2459 gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2460 QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2461 QTRY_COMPARE(gridview->contentY(), 60.);
2463 // Confirm items positioned correctly
2464 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2465 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2466 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2467 if (!item) qWarning() << "Item" << i << "not found";
2469 QTRY_COMPARE(item->x(), (i%3)*80.);
2470 QTRY_COMPARE(item->y(), (i/3)*60.);
2473 // Position on an item beyond the visible items
2474 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2475 QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2476 QTRY_COMPARE(gridview->contentY(), 420.);
2478 // Confirm items positioned correctly
2479 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2480 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2481 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2482 if (!item) qWarning() << "Item" << i << "not found";
2484 QTRY_COMPARE(item->x(), (i%3)*80.);
2485 QTRY_COMPARE(item->y(), (i/3)*60.);
2488 // Position on an item that would leave empty space if positioned at the top
2489 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2490 QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2491 QTRY_COMPARE(gridview->contentY(), 520.);
2493 // Confirm items positioned correctly
2494 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2495 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2496 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2497 if (!item) qWarning() << "Item" << i << "not found";
2499 QTRY_COMPARE(item->x(), (i%3)*80.);
2500 QTRY_COMPARE(item->y(), (i/3)*60.);
2503 // Position at the beginning again
2504 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2505 QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2506 QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2507 QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2508 QTRY_COMPARE(gridview->contentY(), 0.);
2510 // Confirm items positioned correctly
2511 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2512 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2513 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2514 if (!item) qWarning() << "Item" << i << "not found";
2516 QTRY_COMPARE(item->x(), (i%3)*80.);
2517 QTRY_COMPARE(item->y(), (i/3)*60.);
2521 gridview->positionViewAtIndex(30, QQuickGridView::End);
2522 QTRY_COMPARE(gridview->contentY(), 340.);
2524 // Position in Center
2525 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2526 QTRY_COMPARE(gridview->contentY(), 170.);
2528 // Ensure at least partially visible
2529 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2530 QTRY_COMPARE(gridview->contentY(), 170.);
2532 gridview->setContentY(302);
2533 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2534 QTRY_COMPARE(gridview->contentY(), 302.);
2536 gridview->setContentY(360);
2537 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2538 QTRY_COMPARE(gridview->contentY(), 300.);
2540 gridview->setContentY(60);
2541 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2542 QTRY_COMPARE(gridview->contentY(), 60.);
2544 gridview->setContentY(20);
2545 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2546 QTRY_COMPARE(gridview->contentY(), 100.);
2548 // Ensure completely visible
2549 gridview->setContentY(120);
2550 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2551 QTRY_COMPARE(gridview->contentY(), 120.);
2553 gridview->setContentY(302);
2554 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2555 QTRY_COMPARE(gridview->contentY(), 300.);
2557 gridview->setContentY(60);
2558 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2559 QTRY_COMPARE(gridview->contentY(), 100.);
2561 // Test for Top To Bottom layout
2562 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2564 // Confirm items positioned correctly
2565 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2566 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2567 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2568 if (!item) qWarning() << "Item" << i << "not found";
2570 QTRY_COMPARE(item->x(), (i/5)*80.);
2571 QTRY_COMPARE(item->y(), (i%5)*60.);
2575 gridview->positionViewAtIndex(30, QQuickGridView::End);
2576 QTRY_COMPARE(gridview->contentX(), 320.);
2577 QTRY_COMPARE(gridview->contentY(), 0.);
2579 // Position in Center
2580 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2581 QTRY_COMPARE(gridview->contentX(), 160.);
2583 // Ensure at least partially visible
2584 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2585 QTRY_COMPARE(gridview->contentX(), 160.);
2587 gridview->setContentX(170);
2588 gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2589 QTRY_COMPARE(gridview->contentX(), 170.);
2591 gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2592 QTRY_COMPARE(gridview->contentX(), 320.);
2594 gridview->setContentX(170);
2595 gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2596 QTRY_COMPARE(gridview->contentX(), 240.);
2598 // positionViewAtBeginning
2599 gridview->positionViewAtBeginning();
2600 QTRY_COMPARE(gridview->contentX(), 0.);
2602 gridview->setContentX(80);
2603 canvas->rootObject()->setProperty("showHeader", true);
2604 gridview->positionViewAtBeginning();
2605 QTRY_COMPARE(gridview->contentX(), -30.);
2607 // positionViewAtEnd
2608 gridview->positionViewAtEnd();
2609 QTRY_COMPARE(gridview->contentX(), 400.); // 8*80 - 240 (8 columns)
2611 gridview->setContentX(80);
2612 canvas->rootObject()->setProperty("showFooter", true);
2613 gridview->positionViewAtEnd();
2614 QTRY_COMPARE(gridview->contentX(), 430.);
2616 // set current item to outside visible view, position at beginning
2617 // and ensure highlight moves to current item
2618 gridview->setCurrentIndex(6);
2619 gridview->positionViewAtBeginning();
2620 QTRY_COMPARE(gridview->contentX(), -30.);
2621 QVERIFY(gridview->highlightItem());
2622 QCOMPARE(gridview->highlightItem()->x(), 80.);
2627 void tst_QQuickGridView::snapping()
2629 QQuickView *canvas = createView();
2632 for (int i = 0; i < 40; i++)
2633 model.addItem("Item" + QString::number(i), "");
2635 canvas->rootContext()->setContextProperty("testModel", &model);
2636 canvas->setSource(testFileUrl("gridview1.qml"));
2637 qApp->processEvents();
2639 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2640 QTRY_VERIFY(gridview != 0);
2642 gridview->setHeight(220);
2643 QCOMPARE(gridview->height(), 220.);
2645 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2646 QCOMPARE(gridview->contentY(), 80.);
2648 gridview->setContentY(0);
2649 QCOMPARE(gridview->contentY(), 0.);
2651 gridview->setSnapMode(QQuickGridView::SnapToRow);
2652 QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2654 gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2655 QCOMPARE(gridview->contentY(), 60.);
2657 gridview->positionViewAtIndex(15, QQuickGridView::End);
2658 QCOMPARE(gridview->contentY(), 120.);
2664 void tst_QQuickGridView::mirroring()
2666 QQuickView *canvasA = createView();
2667 canvasA->setSource(testFileUrl("mirroring.qml"));
2668 QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2669 QTRY_VERIFY(gridviewA != 0);
2671 QQuickView *canvasB = createView();
2672 canvasB->setSource(testFileUrl("mirroring.qml"));
2673 QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2674 QTRY_VERIFY(gridviewA != 0);
2675 qApp->processEvents();
2677 QList<QString> objectNames;
2678 objectNames << "item1" << "item2"; // << "item3"
2680 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2681 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2682 QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2685 foreach (const QString objectName, objectNames)
2686 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2688 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2689 gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2692 foreach (const QString objectName, objectNames)
2693 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2695 QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2696 QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2697 QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2699 // LTR != LTR+mirror
2700 foreach (const QString objectName, objectNames)
2701 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2703 gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2705 // RTL == LTR+mirror
2706 foreach (const QString objectName, objectNames)
2707 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2709 gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2711 // RTL != RTL+mirror
2712 foreach (const QString objectName, objectNames)
2713 QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2715 gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2717 // LTR == RTL+mirror
2718 foreach (const QString objectName, objectNames)
2719 QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2725 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2727 QQuickView *canvas = createView();
2730 for (int i = 0; i < 40; i++)
2731 model.addItem("Item" + QString::number(i), "");
2733 QQmlContext *ctxt = canvas->rootContext();
2734 ctxt->setContextProperty("testModel", &model);
2735 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2736 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2737 ctxt->setContextProperty("testBottomToTop", QVariant(false));
2739 canvas->setSource(testFileUrl("layouts.qml"));
2740 qApp->processEvents();
2742 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2743 QTRY_VERIFY(gridview != 0);
2745 QQuickItem *contentItem = gridview->contentItem();
2746 QTRY_VERIFY(contentItem != 0);
2748 // Confirm items positioned correctly
2749 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2750 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2751 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2752 if (!item) qWarning() << "Item" << i << "not found";
2754 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2755 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2758 // Position on a currently visible item
2759 gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2760 QTRY_COMPARE(gridview->contentX(), -320.);
2762 // Confirm items positioned correctly
2763 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2764 for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2765 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2766 if (!item) qWarning() << "Item" << i << "not found";
2768 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2769 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2772 // Position on an item beyond the visible items
2773 gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2774 QTRY_COMPARE(gridview->contentX(), -560.);
2776 // Confirm items positioned correctly
2777 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2778 for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2779 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2780 if (!item) qWarning() << "Item" << i << "not found";
2782 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2783 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2786 // Position on an item that would leave empty space if positioned at the top
2787 gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2788 QTRY_COMPARE(gridview->contentX(), -640.);
2790 // Confirm items positioned correctly
2791 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2792 for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2793 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2794 if (!item) qWarning() << "Item" << i << "not found";
2796 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2797 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2800 // Position at the beginning again
2801 gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2802 QTRY_COMPARE(gridview->contentX(), -240.);
2804 // Confirm items positioned correctly
2805 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2806 for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2807 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2808 if (!item) qWarning() << "Item" << i << "not found";
2810 QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2811 QTRY_COMPARE(item->y(), qreal((i%5)*60));
2815 gridview->positionViewAtIndex(30, QQuickGridView::End);
2816 QTRY_COMPARE(gridview->contentX(), -560.);
2818 // Position in Center
2819 gridview->positionViewAtIndex(15, QQuickGridView::Center);
2820 QTRY_COMPARE(gridview->contentX(), -400.);
2822 // Ensure at least partially visible
2823 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2824 QTRY_COMPARE(gridview->contentX(), -400.);
2826 gridview->setContentX(-555.);
2827 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2828 QTRY_COMPARE(gridview->contentX(), -555.);
2830 gridview->setContentX(-239);
2831 gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2832 QTRY_COMPARE(gridview->contentX(), -320.);
2834 gridview->setContentX(-239);
2835 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2836 QTRY_COMPARE(gridview->contentX(), -400.);
2838 gridview->setContentX(-640);
2839 gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2840 QTRY_COMPARE(gridview->contentX(), -560.);
2842 // Ensure completely visible
2843 gridview->setContentX(-400);
2844 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2845 QTRY_COMPARE(gridview->contentX(), -400.);
2847 gridview->setContentX(-315);
2848 gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2849 QTRY_COMPARE(gridview->contentX(), -320.);
2851 gridview->setContentX(-640);
2852 gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2853 QTRY_COMPARE(gridview->contentX(), -560.);
2858 void tst_QQuickGridView::resetModel()
2860 QQuickView *canvas = createView();
2862 QStringList strings;
2863 strings << "one" << "two" << "three";
2864 QStringListModel model(strings);
2866 QQmlContext *ctxt = canvas->rootContext();
2867 ctxt->setContextProperty("testModel", &model);
2869 canvas->setSource(testFileUrl("displaygrid.qml"));
2871 qApp->processEvents();
2873 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2874 QTRY_VERIFY(gridview != 0);
2875 QQuickItem *contentItem = gridview->contentItem();
2876 QTRY_VERIFY(contentItem != 0);
2877 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2879 QTRY_COMPARE(gridview->count(), model.rowCount());
2881 for (int i = 0; i < model.rowCount(); ++i) {
2882 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2883 QTRY_VERIFY(display != 0);
2884 QTRY_COMPARE(display->text(), strings.at(i));
2888 strings << "four" << "five" << "six" << "seven";
2889 model.setStringList(strings);
2891 QTRY_COMPARE(gridview->count(), model.rowCount());
2893 for (int i = 0; i < model.rowCount(); ++i) {
2894 QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2895 QTRY_VERIFY(display != 0);
2896 QTRY_COMPARE(display->text(), strings.at(i));
2902 void tst_QQuickGridView::enforceRange()
2904 QQuickView *canvas = createView();
2907 for (int i = 0; i < 30; i++)
2908 model.addItem("Item" + QString::number(i), "");
2910 QQmlContext *ctxt = canvas->rootContext();
2911 ctxt->setContextProperty("testModel", &model);
2912 ctxt->setContextProperty("testRightToLeft", QVariant(false));
2913 ctxt->setContextProperty("testTopToBottom", QVariant(false));
2915 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2917 qApp->processEvents();
2918 QVERIFY(canvas->rootObject() != 0);
2920 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2921 QTRY_VERIFY(gridview != 0);
2923 QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2924 QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2925 QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2926 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2928 QQuickItem *contentItem = gridview->contentItem();
2929 QTRY_VERIFY(contentItem != 0);
2931 // view should be positioned at the top of the range.
2932 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2934 QTRY_COMPARE(gridview->contentY(), -100.0);
2936 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2937 QTRY_VERIFY(name != 0);
2938 QTRY_COMPARE(name->text(), model.name(0));
2939 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2940 QTRY_VERIFY(number != 0);
2941 QTRY_COMPARE(number->text(), model.number(0));
2943 // Check currentIndex is updated when contentItem moves
2944 gridview->setContentY(0);
2945 QTRY_COMPARE(gridview->currentIndex(), 2);
2947 gridview->setCurrentIndex(5);
2948 QTRY_COMPARE(gridview->contentY(), 100.);
2951 for (int i = 0; i < 5; i++)
2952 model2.addItem("Item" + QString::number(i), "");
2954 ctxt->setContextProperty("testModel", &model2);
2955 QCOMPARE(gridview->count(), 5);
2960 void tst_QQuickGridView::enforceRange_rightToLeft()
2962 QQuickView *canvas = createView();
2965 for (int i = 0; i < 30; i++)
2966 model.addItem("Item" + QString::number(i), "");
2968 QQmlContext *ctxt = canvas->rootContext();
2969 ctxt->setContextProperty("testModel", &model);
2970 ctxt->setContextProperty("testRightToLeft", QVariant(true));
2971 ctxt->setContextProperty("testTopToBottom", QVariant(true));
2973 canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2974 qApp->processEvents();
2975 QVERIFY(canvas->rootObject() != 0);
2977 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2978 QTRY_VERIFY(gridview != 0);
2980 QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2981 QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2982 QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2984 QQuickItem *contentItem = gridview->contentItem();
2985 QVERIFY(contentItem != 0);
2987 // view should be positioned at the top of the range.
2988 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2990 QTRY_COMPARE(gridview->contentX(), -140.);
2991 QTRY_COMPARE(gridview->contentY(), 0.0);
2993 QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2994 QTRY_VERIFY(name != 0);
2995 QTRY_COMPARE(name->text(), model.name(0));
2996 QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2997 QTRY_VERIFY(number != 0);
2998 QTRY_COMPARE(number->text(), model.number(0));
3000 // Check currentIndex is updated when contentItem moves
3001 gridview->setContentX(-240);
3002 QTRY_COMPARE(gridview->currentIndex(), 3);
3004 gridview->setCurrentIndex(7);
3005 QTRY_COMPARE(gridview->contentX(), -340.);
3006 QTRY_COMPARE(gridview->contentY(), 0.0);
3009 for (int i = 0; i < 5; i++)
3010 model2.addItem("Item" + QString::number(i), "");
3012 ctxt->setContextProperty("testModel", &model2);
3013 QCOMPARE(gridview->count(), 5);
3018 void tst_QQuickGridView::QTBUG_8456()
3020 QQuickView *canvas = createView();
3022 canvas->setSource(testFileUrl("setindex.qml"));
3023 qApp->processEvents();
3025 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3026 QTRY_VERIFY(gridview != 0);
3028 QTRY_COMPARE(gridview->currentIndex(), 0);
3033 void tst_QQuickGridView::manualHighlight()
3035 QQuickView *canvas = createView();
3037 QString filename(testFile("manual-highlight.qml"));
3038 canvas->setSource(QUrl::fromLocalFile(filename));
3040 qApp->processEvents();
3042 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3043 QTRY_VERIFY(gridview != 0);
3045 QQuickItem *contentItem = gridview->contentItem();
3046 QTRY_VERIFY(contentItem != 0);
3048 QTRY_COMPARE(gridview->currentIndex(), 0);
3049 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3050 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3051 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3053 gridview->setCurrentIndex(2);
3055 QTRY_COMPARE(gridview->currentIndex(), 2);
3056 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3057 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3058 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3060 gridview->positionViewAtIndex(8, QQuickGridView::Contain);
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->setFlow(QQuickGridView::FlowTopToBottom);
3068 QTRY_COMPARE(gridview->flow(), QQuickGridView::FlowTopToBottom);
3070 gridview->setCurrentIndex(0);
3071 QTRY_COMPARE(gridview->currentIndex(), 0);
3072 QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3073 QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3074 QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3080 void tst_QQuickGridView::footer()
3082 QFETCH(QQuickGridView::Flow, flow);
3083 QFETCH(Qt::LayoutDirection, layoutDirection);
3084 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3085 QFETCH(QPointF, initialFooterPos);
3086 QFETCH(QPointF, changedFooterPos);
3087 QFETCH(QPointF, initialContentPos);
3088 QFETCH(QPointF, firstDelegatePos);
3089 QFETCH(QPointF, resizeContentPos);
3091 QQuickView *canvas = getView();
3095 for (int i = 0; i < 7; i++)
3096 model.addItem("Item" + QString::number(i), "");
3098 QQmlContext *ctxt = canvas->rootContext();
3099 ctxt->setContextProperty("testModel", &model);
3100 canvas->setSource(testFileUrl("footer.qml"));
3101 qApp->processEvents();
3103 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3104 QTRY_VERIFY(gridview != 0);
3105 gridview->setFlow(flow);
3106 gridview->setLayoutDirection(layoutDirection);
3107 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3108 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3110 QQuickItem *contentItem = gridview->contentItem();
3111 QTRY_VERIFY(contentItem != 0);
3113 QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3115 QVERIFY(footer == gridview->footerItem());
3117 QCOMPARE(footer->pos(), initialFooterPos);
3118 QCOMPARE(footer->width(), 100.);
3119 QCOMPARE(footer->height(), 30.);
3120 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3122 if (flow == QQuickGridView::FlowLeftToRight)
3123 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + footer->height());
3125 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + footer->width());
3127 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3129 QCOMPARE(item->pos(), firstDelegatePos);
3131 if (flow == QQuickGridView::FlowLeftToRight) {
3132 // shrink by one row
3133 model.removeItem(2);
3134 if (verticalLayoutDirection == QQuickItemView::TopToBottom)
3135 QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
3137 QTRY_COMPARE(footer->y(), initialFooterPos.y() + gridview->cellHeight());
3139 // shrink by one column
3140 model.removeItem(2);
3141 model.removeItem(3);
3142 if (layoutDirection == Qt::LeftToRight)
3143 QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
3145 QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
3150 if (flow == QQuickGridView::FlowLeftToRight)
3151 QTRY_COMPARE(gridview->contentHeight(), footer->height());
3153 QTRY_COMPARE(gridview->contentWidth(), footer->width());
3155 QPointF posWhenNoItems(0, 0);
3156 if (layoutDirection == Qt::RightToLeft)
3157 posWhenNoItems.setX(flow == QQuickGridView::FlowLeftToRight ? gridview->width() - footer->width() : -footer->width());
3158 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3159 posWhenNoItems.setY(-footer->height());
3160 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3162 // if header is toggled, it shouldn't affect the footer position
3163 canvas->rootObject()->setProperty("showHeader", true);
3164 QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
3165 QTRY_COMPARE(footer->pos(), posWhenNoItems);
3166 canvas->rootObject()->setProperty("showHeader", false);
3169 for (int i = 0; i < 30; i++)
3170 model.addItem("Item" + QString::number(i), "");
3172 QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
3173 QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3175 QCOMPARE(footerItemSpy.count(), 1);
3177 footer = findItem<QQuickText>(contentItem, "footer");
3179 footer = findItem<QQuickText>(contentItem, "footer2");
3181 QVERIFY(footer == gridview->footerItem());
3183 QCOMPARE(footer->pos(), changedFooterPos);
3184 QCOMPARE(footer->width(), 50.);
3185 QCOMPARE(footer->height(), 20.);
3187 // changing the footer shouldn't change the content pos
3188 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3190 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3192 QCOMPARE(item->pos(), firstDelegatePos);
3194 gridview->positionViewAtEnd();
3195 footer->setHeight(10);
3196 footer->setWidth(40);
3197 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3199 releaseView(canvas);
3202 void tst_QQuickGridView::footer_data()
3204 QTest::addColumn<QQuickGridView::Flow>("flow");
3205 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3206 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3207 QTest::addColumn<QPointF>("initialFooterPos");
3208 QTest::addColumn<QPointF>("changedFooterPos");
3209 QTest::addColumn<QPointF>("initialContentPos");
3210 QTest::addColumn<QPointF>("firstDelegatePos");
3211 QTest::addColumn<QPointF>("resizeContentPos");
3213 // footer1 = 100 x 30
3214 // footer2 = 50 x 20
3217 // view height = 320
3219 // footer below items, bottom left
3220 QTest::newRow("LeftToRight, LtR, TtB")
3221 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3222 << QPointF(0, 3 * 60) // 180 = height of 3 rows (cell height is 60)
3223 << QPointF(0, 10 * 60) // 30 items = 10 rows
3226 << QPointF(0, (10 * 60) - 320 + 10);
3228 // footer below items, bottom right
3229 QTest::newRow("LeftToRight, RtL, TtB")
3230 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3231 << QPointF(240 - 100, 3 * 60)
3232 << QPointF((240 - 100) + 50, 10 * 60) // 50 = width diff between old and new footers
3234 << QPointF(240 - 80, 0)
3235 << QPointF(0, (10 * 60) - 320 + 10);
3237 // footer above items, top left
3238 QTest::newRow("LeftToRight, LtR, BtT")
3239 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3240 << QPointF(0, -(3 * 60) - 30)
3241 << QPointF(0, -(10 * 60) - 20)
3244 << QPointF(0, -(10 * 60) - 10);
3246 // footer above items, top right
3247 QTest::newRow("LeftToRight, RtL, BtT")
3248 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3249 << QPointF(240 - 100, -(3 * 60) - 30)
3250 << QPointF((240 - 100) + 50, -(10 * 60) - 20)
3252 << QPointF(240 - 80, -60)
3253 << QPointF(0, -(10 * 60) - 10);
3256 // footer to right of items, bottom right
3257 QTest::newRow("TopToBottom, LtR, TtB")
3258 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3259 << QPointF(2 * 80, 0) // 2 columns, cell width 80
3260 << QPointF(6 * 80, 0) // 30 items = 6 columns
3263 << QPointF((6 * 80) - 240 + 40, 0);
3265 // footer to left of items, bottom right
3266 QTest::newRow("TopToBottom, RtL, TtB")
3267 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3268 << QPointF(-(2 * 80) - 100, 0)
3269 << QPointF(-(6 * 80) - 50, 0) // 50 = new footer width
3272 << QPointF(-(6 * 80) - 40, 0);
3274 // footer to right of items, top right
3275 QTest::newRow("TopToBottom, LtR, BtT")
3276 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3277 << QPointF(2 * 80, -30)
3278 << QPointF(6 * 80, -20)
3281 << QPointF((6 * 80) - 240 + 40, -320);
3283 // footer to left of items, top left
3284 QTest::newRow("TopToBottom, RtL, BtT")
3285 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3286 << QPointF(-(2 * 80) - 100, -30)
3287 << QPointF(-(6 * 80) - 50, -20)
3288 << QPointF(-240, -320)
3289 << QPointF(-80, -60)
3290 << QPointF(-(6 * 80) - 40, -320);
3293 void tst_QQuickGridView::initialZValues()
3295 QQuickView *canvas = createView();
3296 canvas->setSource(testFileUrl("initialZValues.qml"));
3297 qApp->processEvents();
3299 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3300 QTRY_VERIFY(gridview != 0);
3301 QQuickItem *contentItem = gridview->contentItem();
3302 QTRY_VERIFY(contentItem != 0);
3304 QVERIFY(gridview->headerItem());
3305 QTRY_COMPARE(gridview->headerItem()->z(), gridview->property("initialZ").toReal());
3307 QVERIFY(gridview->footerItem());
3308 QTRY_COMPARE(gridview->footerItem()->z(), gridview->property("initialZ").toReal());
3313 void tst_QQuickGridView::header()
3315 QFETCH(QQuickGridView::Flow, flow);
3316 QFETCH(Qt::LayoutDirection, layoutDirection);
3317 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3318 QFETCH(QPointF, initialHeaderPos);
3319 QFETCH(QPointF, changedHeaderPos);
3320 QFETCH(QPointF, initialContentPos);
3321 QFETCH(QPointF, changedContentPos);
3322 QFETCH(QPointF, firstDelegatePos);
3323 QFETCH(QPointF, resizeContentPos);
3326 for (int i = 0; i < 30; i++)
3327 model.addItem("Item" + QString::number(i), "");
3329 QQuickView *canvas = getView();
3330 canvas->rootContext()->setContextProperty("testModel", &model);
3331 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3332 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3333 canvas->setSource(testFileUrl("header.qml"));
3335 qApp->processEvents();
3337 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3338 QTRY_VERIFY(gridview != 0);
3339 gridview->setFlow(flow);
3340 gridview->setLayoutDirection(layoutDirection);
3341 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3342 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3344 QQuickItem *contentItem = gridview->contentItem();
3345 QTRY_VERIFY(contentItem != 0);
3347 QQuickText *header = findItem<QQuickText>(contentItem, "header");
3349 QVERIFY(header == gridview->headerItem());
3351 QCOMPARE(header->pos(), initialHeaderPos);
3352 QCOMPARE(header->width(), 100.);
3353 QCOMPARE(header->height(), 30.);
3354 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3356 if (flow == QQuickGridView::FlowLeftToRight)
3357 QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + header->height());
3359 QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + header->width());
3361 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3363 QCOMPARE(item->pos(), firstDelegatePos);
3366 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3367 QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3368 if (flow == QQuickGridView::FlowLeftToRight)
3369 QCOMPARE(gridview->contentHeight(), header->height());
3371 QCOMPARE(gridview->contentWidth(), header->width());
3373 for (int i = 0; i < 30; i++)
3374 model.addItem("Item" + QString::number(i), "");
3376 QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
3377 QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3379 QCOMPARE(headerItemSpy.count(), 1);
3381 header = findItem<QQuickText>(contentItem, "header");
3383 header = findItem<QQuickText>(contentItem, "header2");
3386 QVERIFY(header == gridview->headerItem());
3388 QCOMPARE(header->pos(), changedHeaderPos);
3389 QCOMPARE(header->width(), 50.);
3390 QCOMPARE(header->height(), 20.);
3391 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3393 item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3395 QCOMPARE(item->pos(), firstDelegatePos);
3397 header->setHeight(10);
3398 header->setWidth(40);
3399 QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3401 releaseView(canvas);
3404 // QTBUG-21207 header should become visible if view resizes from initial empty size
3407 canvas->rootContext()->setContextProperty("testModel", &model);
3408 canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3409 canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3410 canvas->setSource(testFileUrl("header.qml"));
3412 qApp->processEvents();
3414 gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3415 QTRY_VERIFY(gridview != 0);
3416 gridview->setFlow(flow);
3417 gridview->setLayoutDirection(layoutDirection);
3418 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3419 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3421 gridview->setWidth(240);
3422 gridview->setHeight(320);
3423 QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3424 QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3426 releaseView(canvas);
3429 void tst_QQuickGridView::header_data()
3431 QTest::addColumn<QQuickGridView::Flow>("flow");
3432 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3433 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3434 QTest::addColumn<QPointF>("initialHeaderPos");
3435 QTest::addColumn<QPointF>("changedHeaderPos");
3436 QTest::addColumn<QPointF>("initialContentPos");
3437 QTest::addColumn<QPointF>("changedContentPos");
3438 QTest::addColumn<QPointF>("firstDelegatePos");
3439 QTest::addColumn<QPointF>("resizeContentPos");
3441 // header1 = 100 x 30
3442 // header2 = 50 x 20
3446 // header above items, top left
3447 QTest::newRow("LeftToRight, LtR, TtB")
3448 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3456 // header above items, top right
3457 QTest::newRow("LeftToRight, RtL, TtB")
3458 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3459 << QPointF(240 - 100, -30)
3460 << QPointF((240 - 100) + 50, -20) // 50 = width diff between old and new headers
3466 // header below items, bottom left
3467 QTest::newRow("LeftToRight, LtR, BtT")
3468 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3471 << QPointF(0, -320 + 30)
3472 << QPointF(0, -320 + 20)
3474 << QPointF(0, -320 + 10);
3476 // header above items, top right
3477 QTest::newRow("LeftToRight, RtL, BtT")
3478 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3479 << QPointF(240 - 100, 0)
3480 << QPointF((240 - 100) + 50, 0)
3481 << QPointF(0, -320 + 30)
3482 << QPointF(0, -320 + 20)
3483 << QPointF(160, -60)
3484 << QPointF(0, -320 + 10);
3487 // header to left of items, bottom left
3488 QTest::newRow("TopToBottom, LtR, TtB")
3489 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3497 // header to right of items, bottom right
3498 QTest::newRow("TopToBottom, RtL, TtB")
3499 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3502 << QPointF(-(240 - 100), 0)
3503 << QPointF(-(240 - 50), 0)
3505 << QPointF(-(240 - 40), 0);
3507 // header to left of items, top left
3508 QTest::newRow("TopToBottom, LtR, BtT")
3509 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3510 << QPointF(-100, -30)
3511 << QPointF(-50, -20)
3512 << QPointF(-100, -320)
3513 << QPointF(-50, -320)
3515 << QPointF(-40, -320);
3517 // header to right of items, top right
3518 QTest::newRow("TopToBottom, RtL, BtT")
3519 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3522 << QPointF(-(240 - 100), -320)
3523 << QPointF(-(240 - 50), -320)
3524 << QPointF(-80, -60)
3525 << QPointF(-(240 - 40), -320);
3528 class GVAccessor : public QQuickGridView
3531 qreal minY() const { return minYExtent(); }
3532 qreal maxY() const { return maxYExtent(); }
3533 qreal minX() const { return minXExtent(); }
3534 qreal maxX() const { return maxXExtent(); }
3537 void tst_QQuickGridView::extents()
3539 QFETCH(QQuickGridView::Flow, flow);
3540 QFETCH(Qt::LayoutDirection, layoutDirection);
3541 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3542 QFETCH(QPointF, headerPos);
3543 QFETCH(QPointF, footerPos);
3544 QFETCH(QPointF, minPos);
3545 QFETCH(QPointF, maxPos);
3546 QFETCH(QPointF, origin_empty);
3547 QFETCH(QPointF, origin_nonEmpty);
3549 QQuickView *canvas = getView();
3552 QQmlContext *ctxt = canvas->rootContext();
3553 ctxt->setContextProperty("testModel", &model);
3554 canvas->setSource(testFileUrl("headerfooter.qml"));
3556 qApp->processEvents();
3558 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3559 QTRY_VERIFY(gridview != 0);
3560 gridview->setFlow(flow);
3561 gridview->setLayoutDirection(layoutDirection);
3562 gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3563 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3565 QQuickItem *contentItem = gridview->contentItem();
3566 QTRY_VERIFY(contentItem != 0);
3568 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3570 QCOMPARE(header->pos(), headerPos);
3572 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3574 QCOMPARE(footer->pos(), footerPos);
3576 QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), minPos.x());
3577 QCOMPARE(static_cast<GVAccessor*>(gridview)->minY(), minPos.y());
3578 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), maxPos.x());
3579 QCOMPARE(static_cast<GVAccessor*>(gridview)->maxY(), maxPos.y());
3581 QCOMPARE(gridview->xOrigin(), origin_empty.x());
3582 QCOMPARE(gridview->yOrigin(), origin_empty.y());
3583 for (int i=0; i<30; i++)
3584 model.addItem("Item" + QString::number(i), "");
3585 QTRY_COMPARE(gridview->count(), model.count());
3586 QCOMPARE(gridview->xOrigin(), origin_nonEmpty.x());
3587 QCOMPARE(gridview->yOrigin(), origin_nonEmpty.y());
3589 releaseView(canvas);
3592 void tst_QQuickGridView::extents_data()
3594 QTest::addColumn<QQuickGridView::Flow>("flow");
3595 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3596 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3597 QTest::addColumn<QPointF>("headerPos");
3598 QTest::addColumn<QPointF>("footerPos");
3599 QTest::addColumn<QPointF>("minPos");
3600 QTest::addColumn<QPointF>("maxPos");
3601 QTest::addColumn<QPointF>("origin_empty");
3602 QTest::addColumn<QPointF>("origin_nonEmpty");
3604 // header is 240x20 (or 20x320 in TopToBottom)
3605 // footer is 240x30 (or 30x320 in TopToBottom)
3606 // grid has 10 rows in LeftToRight mode and 6 columns in TopToBottom
3608 QTest::newRow("LeftToRight, LtR, TtB")
3609 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3610 << QPointF(0, -20) << QPointF(0, 0)
3611 << QPointF(0, 20) << QPointF(240, 20)
3612 << QPointF(0, -20) << QPointF(0, -20);
3614 QTest::newRow("LeftToRight, RtL, TtB")
3615 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3616 << QPointF(0, -20) << QPointF(0, 0)
3617 << QPointF(0, 20) << QPointF(240, 20)
3618 << QPointF(0, -20) << QPointF(0, -20);
3620 QTest::newRow("LeftToRight, LtR, BtT")
3621 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3622 << QPointF(0, 0) << QPointF(0, -30)
3623 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3624 << QPointF(0, -30) << QPointF(0, (-60.0 * 10) - 30);
3626 QTest::newRow("LeftToRight, RtL, BtT")
3627 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3628 << QPointF(0, 0) << QPointF(0, -30)
3629 << QPointF(0, 320 - 20) << QPointF(240, 320 - 20) // content flow is reversed
3630 << QPointF(0, -30) << QPointF(0, (-60.0 * 10) - 30);
3633 QTest::newRow("TopToBottom, LtR, TtB")
3634 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3635 << QPointF(-20, 0) << QPointF(0, 0)
3636 << QPointF(20, 0) << QPointF(20, 320)
3637 << QPointF(-20, 0) << QPointF(-20, 0);
3639 QTest::newRow("TopToBottom, RtL, TtB")
3640 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3641 << QPointF(0, 0) << QPointF(-30, 0)
3642 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3643 << QPointF(-30, 0) << QPointF((-80.0 * 6) - 30, 0);
3645 QTest::newRow("TopToBottom, LtR, BtT")
3646 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3647 << QPointF(-20, -320) << QPointF(0, -320)
3648 << QPointF(20, 0) << QPointF(20, 320)
3649 << QPointF(-20, 0) << QPointF(-20, 0);
3651 QTest::newRow("TopToBottom, RtL, BtT")
3652 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3653 << QPointF(0, -320) << QPointF(-30, -320)
3654 << QPointF(240 - 20, 0) << QPointF(240 - 20, 320) // content flow is reversed
3655 << QPointF(-30, 0) << QPointF((-80.0 * 6) - 30, 0);
3658 void tst_QQuickGridView::resetModel_headerFooter()
3660 // Resetting a model shouldn't crash in views with header/footer
3662 QQuickView *canvas = createView();
3665 for (int i = 0; i < 6; i++)
3666 model.addItem("Item" + QString::number(i), "");
3667 QQmlContext *ctxt = canvas->rootContext();
3668 ctxt->setContextProperty("testModel", &model);
3670 canvas->setSource(testFileUrl("headerfooter.qml"));
3671 qApp->processEvents();
3673 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3674 QTRY_VERIFY(gridview != 0);
3676 QQuickItem *contentItem = gridview->contentItem();
3677 QTRY_VERIFY(contentItem != 0);
3679 QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3681 QCOMPARE(header->y(), -header->height());
3683 QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3685 QCOMPARE(footer->y(), 60.*2);
3689 header = findItem<QQuickItem>(contentItem, "header");
3691 QCOMPARE(header->y(), -header->height());
3693 footer = findItem<QQuickItem>(contentItem, "footer");
3695 QCOMPARE(footer->y(), 60.*2);
3700 void tst_QQuickGridView::resizeViewAndRepaint()
3702 QQuickView *canvas = createView();
3706 for (int i = 0; i < 40; i++)
3707 model.addItem("Item" + QString::number(i), "");
3709 QQmlContext *ctxt = canvas->rootContext();
3710 ctxt->setContextProperty("testModel", &model);
3711 ctxt->setContextProperty("initialWidth", 240);
3712 ctxt->setContextProperty("initialHeight", 100);
3714 canvas->setSource(testFileUrl("resizeview.qml"));
3716 qApp->processEvents();
3718 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3719 QTRY_VERIFY(gridview != 0);
3720 QQuickItem *contentItem = gridview->contentItem();
3721 QTRY_VERIFY(contentItem != 0);
3722 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3724 // item at index 10 should not be currently visible
3725 QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3727 gridview->setHeight(320);
3728 QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3730 gridview->setHeight(100);
3731 QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3733 // Ensure we handle -ve sizes
3734 gridview->setHeight(-100);
3735 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3737 gridview->setCacheBuffer(120);
3738 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3740 // ensure items in cache become visible
3741 gridview->setHeight(120);
3742 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3744 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3745 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3746 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3747 if (!item) qWarning() << "Item" << i << "not found";
3749 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3750 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3751 QCOMPARE(delegateVisible(item), i < 9); // inside view visible, outside not visible
3754 // ensure items outside view become invisible
3755 gridview->setHeight(60);
3756 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3758 itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3759 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3760 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3761 if (!item) qWarning() << "Item" << i << "not found";
3763 QTRY_COMPARE(item->x(), qreal((i%3)*80));
3764 QTRY_COMPARE(item->y(), qreal((i/3)*60));
3765 QCOMPARE(delegateVisible(item), i < 6); // inside view visible, outside not visible
3771 void tst_QQuickGridView::resizeGrid()
3773 QFETCH(QQuickGridView::Flow, flow);
3774 QFETCH(Qt::LayoutDirection, layoutDirection);
3775 QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3776 QFETCH(QPointF, initialContentPos);
3777 QFETCH(QPointF, firstItemPos);
3780 for (int i = 0; i < 30; i++)
3781 model.addItem("Item" + QString::number(i), "");
3783 QQuickView *canvas = getView();
3784 QQmlContext *ctxt = canvas->rootContext();
3785 ctxt->setContextProperty("testModel", &model);
3786 ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
3787 ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
3788 ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickGridView::BottomToTop);
3789 canvas->setSource(testFileUrl("resizegrid.qml"));
3791 qApp->processEvents();
3793 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3794 QTRY_VERIFY(gridview != 0);
3795 QQuickItem *contentItem = gridview->contentItem();
3796 QTRY_VERIFY(contentItem != 0);
3798 // set the width to slightly larger than 3 items across, to test
3799 // items are aligned correctly in right-to-left
3800 canvas->rootObject()->setWidth(260);
3801 canvas->rootObject()->setHeight(320);
3802 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3804 QCOMPARE(gridview->contentX(), initialContentPos.x());
3805 QCOMPARE(gridview->contentY(), initialContentPos.y());
3807 QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
3809 QCOMPARE(item0->pos(), firstItemPos);
3811 // Confirm items positioned correctly and indexes correct
3812 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
3813 QVERIFY(items.count() >= 18 && items.count() <= 21);
3814 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3815 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3816 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3817 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3818 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3820 QCOMPARE(name->text(), model.name(i));
3823 // change from 3x5 grid to 4x7
3824 canvas->rootObject()->setWidth(canvas->rootObject()->width() + 80);
3825 canvas->rootObject()->setHeight(canvas->rootObject()->height() + 60*2);
3826 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3828 // other than in LeftToRight+RightToLeft layout, the first item should not move
3829 // if view is resized
3830 QCOMPARE(findItem<QQuickItem>(contentItem, "wrapper", 0), item0);
3831 if (flow == QQuickGridView::FlowLeftToRight && layoutDirection == Qt::RightToLeft)
3832 firstItemPos.rx() += 80;
3833 QCOMPARE(item0->pos(), firstItemPos);
3835 QPointF newContentPos = initialContentPos;
3836 if (flow == QQuickGridView::FlowTopToBottom && layoutDirection == Qt::RightToLeft)
3837 newContentPos.rx() -= 80.0;
3838 if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3839 newContentPos.ry() -= 60.0 * 2;
3840 QCOMPARE(gridview->contentX(), newContentPos.x());
3841 QCOMPARE(gridview->contentY(), newContentPos.y());
3843 // Confirm items positioned correctly and indexes correct
3844 items = findItems<QQuickItem>(contentItem, "wrapper");
3845 QVERIFY(items.count() >= 28);
3846 for (int i = 0; i < model.count() && i < items.count(); ++i) {
3847 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3848 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3849 QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3850 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3852 QCOMPARE(name->text(), model.name(i));
3855 releaseView(canvas);
3858 void tst_QQuickGridView::resizeGrid_data()
3860 QTest::addColumn<QQuickGridView::Flow>("flow");
3861 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3862 QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3863 QTest::addColumn<QPointF>("initialContentPos");
3864 QTest::addColumn<QPointF>("firstItemPos");
3866 // Initial view width is 260, so in LeftToRight + right-to-left mode the
3867 // content x should be -20
3869 QTest::newRow("LeftToRight, LtR, TtB")
3870 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3874 QTest::newRow("LeftToRight, RtL, TtB")
3875 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3876 << QPointF(-20.0, 0)
3877 << QPointF(80.0 * 2, 0);
3879 QTest::newRow("LeftToRight, LtR, BtT")
3880 << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3882 << QPointF(0, -60.0);
3884 QTest::newRow("LeftToRight, RtL, BtT")
3885 << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3886 << QPointF(-20.0, -320)
3887 << QPointF(80.0 * 2, -60.0);
3890 QTest::newRow("TopToBottom, LtR, TtB")
3891 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3895 QTest::newRow("TopToBottom, RtL, TtB")
3896 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3898 << QPointF(-80.0, 0);
3900 QTest::newRow("TopToBottom, LtR, BtT")
3901 << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3903 << QPointF(0, -60.0);
3905 QTest::newRow("TopToBottom, RtL, BtT")
3906 << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3907 << QPointF(-260, -320)
3908 << QPointF(-80.0, -60.0);
3912 void tst_QQuickGridView::changeColumnCount()
3915 for (int i = 0; i < 40; i++)
3916 model.addItem("Item" + QString::number(i), "");
3918 QQuickView *canvas = createView();
3919 QQmlContext *ctxt = canvas->rootContext();
3920 ctxt->setContextProperty("testModel", &model);
3921 ctxt->setContextProperty("initialWidth", 100);
3922 ctxt->setContextProperty("initialHeight", 320);
3923 canvas->setSource(testFileUrl("resizeview.qml"));
3925 qApp->processEvents();
3927 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3928 QTRY_VERIFY(gridview != 0);
3929 QQuickItem *contentItem = gridview->contentItem();
3930 QTRY_VERIFY(contentItem != 0);
3931 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3933 // a single column of 6 items are visible
3934 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3935 QCOMPARE(itemCount, 6);
3936 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3937 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3938 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3939 QCOMPARE(item->x(), 0.0);
3940 QCOMPARE(item->y(), qreal(i*60));
3943 // now 6x3 grid is visible, plus 1 extra below for refill
3944 gridview->setWidth(240);
3945 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3946 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3947 QCOMPARE(itemCount, 6*3 + 1);
3948 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3949 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3950 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3951 QCOMPARE(item->x(), qreal((i%3)*80));
3952 QCOMPARE(item->y(), qreal((i/3)*60));
3955 // back to single column
3956 gridview->setWidth(100);
3957 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3958 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3959 QCOMPARE(itemCount, 6);
3960 for (int i = 0; i < model.count() && i < itemCount; ++i) {
3961 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3962 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3963 QCOMPARE(item->x(), 0.0);
3964 QCOMPARE(item->y(), qreal(i*60));
3970 void tst_QQuickGridView::indexAt_itemAt_data()
3972 QTest::addColumn<qreal>("x");
3973 QTest::addColumn<qreal>("y");
3974 QTest::addColumn<int>("index");
3976 QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3977 QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3978 QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3979 QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3980 QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3983 void tst_QQuickGridView::indexAt_itemAt()
3989 QQuickView *canvas = getView();
3992 model.addItem("Fred", "12345");
3993 model.addItem("John", "2345");
3994 model.addItem("Bob", "54321");
3995 model.addItem("Billy", "22345");
3996 model.addItem("Sam", "2945");
3997 model.addItem("Ben", "04321");
3998 model.addItem("Jim", "0780");
4000 canvas->rootContext()->setContextProperty("testModel", &model);
4001 canvas->setSource(testFileUrl("gridview1.qml"));
4002 qApp->processEvents();
4004 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4005 QTRY_VERIFY(gridview != 0);
4007 QQuickItem *contentItem = gridview->contentItem();
4008 QTRY_VERIFY(contentItem != 0);
4010 QTRY_COMPARE(gridview->count(), model.count());
4012 QQuickItem *item = 0;
4014 item = findItem<QQuickItem>(contentItem, "wrapper", index);
4017 QCOMPARE(gridview->indexAt(x, y), index);
4018 QVERIFY(gridview->itemAt(x, y) == item);
4020 releaseView(canvas);
4023 void tst_QQuickGridView::onAdd()
4025 QFETCH(int, initialItemCount);
4026 QFETCH(int, itemsToAdd);
4028 const int delegateWidth = 50;
4029 const int delegateHeight = 100;
4031 QQuickView *canvas = getView();
4032 canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
4034 // these initial items should not trigger GridView.onAdd
4035 for (int i=0; i<initialItemCount; i++)
4036 model.addItem("dummy value", "dummy value");
4038 QQmlContext *ctxt = canvas->rootContext();
4039 ctxt->setContextProperty("testModel", &model);
4040 ctxt->setContextProperty("delegateWidth", delegateWidth);
4041 ctxt->setContextProperty("delegateHeight", delegateHeight);
4042 canvas->setSource(testFileUrl("attachedSignals.qml"));
4044 QObject *object = canvas->rootObject();
4045 object->setProperty("width", canvas->width());
4046 object->setProperty("height", canvas->height());
4047 qApp->processEvents();
4049 QList<QPair<QString, QString> > items;
4050 for (int i=0; i<itemsToAdd; i++)
4051 items << qMakePair(QString("value %1").arg(i), QString::number(i));
4052 model.addItems(items);
4054 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4055 qApp->processEvents();
4057 QVariantList result = object->property("addedDelegates").toList();
4058 QTRY_COMPARE(result.count(), items.count());
4059 for (int i=0; i<items.count(); i++)
4060 QCOMPARE(result[i].toString(), items[i].first);
4062 releaseView(canvas);
4065 void tst_QQuickGridView::onAdd_data()
4067 QTest::addColumn<int>("initialItemCount");
4068 QTest::addColumn<int>("itemsToAdd");
4070 QTest::newRow("0, add 1") << 0 << 1;
4071 QTest::newRow("0, add 2") << 0 << 2;
4072 QTest::newRow("0, add 10") << 0 << 10;
4074 QTest::newRow("1, add 1") << 1 << 1;
4075 QTest::newRow("1, add 2") << 1 << 2;
4076 QTest::newRow("1, add 10") << 1 << 10;
4078 QTest::newRow("5, add 1") << 5 << 1;
4079 QTest::newRow("5, add 2") << 5 << 2;
4080 QTest::newRow("5, add 10") << 5 << 10;
4083 void tst_QQuickGridView::onRemove()
4085 QFETCH(int, initialItemCount);
4086 QFETCH(int, indexToRemove);
4087 QFETCH(int, removeCount);
4089 const int delegateWidth = 50;
4090 const int delegateHeight = 100;
4092 for (int i=0; i<initialItemCount; i++)
4093 model.addItem(QString("value %1").arg(i), "dummy value");
4095 QQuickView *canvas = getView();
4096 QQmlContext *ctxt = canvas->rootContext();
4097 ctxt->setContextProperty("testModel", &model);
4098 ctxt->setContextProperty("delegateWidth", delegateWidth);
4099 ctxt->setContextProperty("delegateHeight", delegateHeight);
4100 canvas->setSource(testFileUrl("attachedSignals.qml"));
4101 QObject *object = canvas->rootObject();
4103 model.removeItems(indexToRemove, removeCount);
4104 QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4105 QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4107 releaseView(canvas);
4110 void tst_QQuickGridView::onRemove_data()
4112 QTest::addColumn<int>("initialItemCount");
4113 QTest::addColumn<int>("indexToRemove");
4114 QTest::addColumn<int>("removeCount");
4116 QTest::newRow("remove first") << 1 << 0 << 1;
4117 QTest::newRow("two items, remove first") << 2 << 0 << 1;
4118 QTest::newRow("two items, remove last") << 2 << 1 << 1;
4119 QTest::newRow("two items, remove all") << 2 << 0 << 2;
4121 QTest::newRow("four items, remove first") << 4 << 0 << 1;
4122 QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4123 QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4124 QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4125 QTest::newRow("four items, remove last") << 4 << 3 << 1;
4126 QTest::newRow("four items, remove all") << 4 << 0 << 4;
4128 QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4129 QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4130 QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4133 void tst_QQuickGridView::columnCount()
4136 canvas.setSource(testFileUrl("gridview4.qml"));
4138 canvas.requestActivateWindow();
4139 QTest::qWaitForWindowShown(&canvas);
4141 QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
4143 QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
4144 QCOMPARE(view->cellHeight(), qreal(100));
4146 QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
4147 QCOMPARE(items.size(), 18);
4148 QCOMPARE(items.at(8)->y(), qreal(0));
4149 QCOMPARE(items.at(9)->y(), qreal(100));
4152 void tst_QQuickGridView::margins()
4155 QQuickView *canvas = createView();
4158 for (int i = 0; i < 40; i++)
4159 model.addItem("Item" + QString::number(i), "");
4161 QQmlContext *ctxt = canvas->rootContext();
4162 ctxt->setContextProperty("testModel", &model);
4163 ctxt->setContextProperty("testRightToLeft", QVariant(false));
4165 canvas->setSource(testFileUrl("margins.qml"));
4167 qApp->processEvents();
4169 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4170 QTRY_VERIFY(gridview != 0);
4171 QQuickItem *contentItem = gridview->contentItem();
4172 QTRY_VERIFY(contentItem != 0);
4173 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4175 QCOMPARE(gridview->contentX(), -30.);
4176 QCOMPARE(gridview->xOrigin(), 0.);
4179 gridview->positionViewAtEnd();
4180 qreal pos = gridview->contentX();
4181 gridview->setContentX(pos + 80);
4182 gridview->returnToBounds();
4183 QTRY_COMPARE(gridview->contentX(), pos + 50);
4185 // remove item before visible and check that left margin is maintained
4186 // and xOrigin is updated
4187 gridview->setContentX(200);
4188 model.removeItems(0, 4);
4190 gridview->setContentX(-50);
4191 gridview->returnToBounds();
4192 QCOMPARE(gridview->xOrigin(), 100.);
4193 QTRY_COMPARE(gridview->contentX(), 70.);
4195 // reduce left margin
4196 gridview->setLeftMargin(20);
4197 QCOMPARE(gridview->xOrigin(), 100.);
4198 QTRY_COMPARE(gridview->contentX(), 80.);
4201 gridview->positionViewAtEnd();
4202 QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
4203 pos = gridview->contentX();
4204 gridview->setContentX(pos + 80);
4205 gridview->returnToBounds();
4206 QTRY_COMPARE(gridview->contentX(), pos + 50);
4208 // reduce right margin
4209 pos = gridview->contentX();
4210 gridview->setRightMargin(40);
4211 QCOMPARE(gridview->xOrigin(), 0.);
4212 QTRY_COMPARE(gridview->contentX(), pos-10);
4218 QQuickView *canvas = createView();
4222 for (int i = 0; i < 40; i++)
4223 model.addItem("Item" + QString::number(i), "");
4225 QQmlContext *ctxt = canvas->rootContext();
4226 ctxt->setContextProperty("testModel", &model);
4227 ctxt->setContextProperty("testRightToLeft", QVariant(true));
4229 canvas->setSource(testFileUrl("margins.qml"));
4230 qApp->processEvents();
4232 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4233 QTRY_VERIFY(gridview != 0);
4235 QQuickItem *contentItem = gridview->contentItem();
4236 QTRY_VERIFY(contentItem != 0);
4238 QTRY_COMPARE(gridview->contentX(), -240+50.);
4239 QTRY_COMPARE(gridview->xOrigin(), -100. * 10);
4242 gridview->positionViewAtEnd();
4243 qreal pos = gridview->contentX();
4244 gridview->setContentX(pos - 80);
4245 gridview->returnToBounds();
4246 QTRY_COMPARE(gridview->contentX(), pos - 30);
4248 // remove item before visible and check that left margin is maintained
4249 // and xOrigin is updated
4250 gridview->setContentX(-400);
4251 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4252 model.removeItems(0, 4);
4253 QTRY_COMPARE(model.count(), gridview->count());
4254 gridview->setContentX(-240+50);
4255 gridview->returnToBounds();
4256 QCOMPARE(gridview->xOrigin(), -1000.);
4257 QTRY_COMPARE(gridview->contentX(), -240-50.);
4259 // reduce right margin
4260 pos = gridview->contentX();
4261 gridview->setRightMargin(40);
4262 QCOMPARE(gridview->xOrigin(), -1000.);
4263 QTRY_COMPARE(gridview->contentX(), -240-100 + 40.);
4266 gridview->positionViewAtEnd();
4267 QCOMPARE(gridview->xOrigin(), -900.); // positionViewAtEnd() resets origin
4268 pos = gridview->contentX();
4269 gridview->setContentX(pos - 80);
4270 gridview->returnToBounds();
4271 QTRY_COMPARE(gridview->contentX(), pos - 30);
4273 // reduce left margin
4274 pos = gridview->contentX();
4275 gridview->setLeftMargin(20);
4276 QCOMPARE(gridview->xOrigin(), -900.);
4277 QTRY_COMPARE(gridview->contentX(), pos+10);
4283 void tst_QQuickGridView::creationContext()
4286 canvas.setGeometry(0,0,240,320);
4287 canvas.setSource(testFileUrl("creationContext.qml"));
4288 qApp->processEvents();
4290 QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4292 QVERIFY(rootItem->property("count").toInt() > 0);
4295 QVERIFY(item = findItem<QQuickItem>(rootItem, "listItem"));
4296 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4297 QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4298 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4299 QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4300 QCOMPARE(item->property("text").toString(), QString("Hello!"));
4303 void tst_QQuickGridView::snapToRow_data()
4305 QTest::addColumn<QQuickGridView::Flow>("flow");
4306 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4307 QTest::addColumn<int>("highlightRangeMode");
4308 QTest::addColumn<QPoint>("flickStart");
4309 QTest::addColumn<QPoint>("flickEnd");
4310 QTest::addColumn<qreal>("snapAlignment");
4311 QTest::addColumn<qreal>("endExtent");
4312 QTest::addColumn<qreal>("startExtent");
4314 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4315 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4317 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4318 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4320 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4321 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
4323 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4324 << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4326 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4327 << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4329 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4330 << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
4333 void tst_QQuickGridView::snapToRow()
4335 QFETCH(QQuickGridView::Flow, flow);
4336 QFETCH(Qt::LayoutDirection, layoutDirection);
4337 QFETCH(int, highlightRangeMode);
4338 QFETCH(QPoint, flickStart);
4339 QFETCH(QPoint, flickEnd);
4340 QFETCH(qreal, snapAlignment);
4341 QFETCH(qreal, endExtent);
4342 QFETCH(qreal, startExtent);
4344 QQuickView *canvas = getView();
4346 canvas->setSource(testFileUrl("snapToRow.qml"));
4348 qApp->processEvents();
4350 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4351 QTRY_VERIFY(gridview != 0);
4353 gridview->setFlow(flow);
4354 gridview->setLayoutDirection(layoutDirection);
4355 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4356 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4358 QQuickItem *contentItem = gridview->contentItem();
4359 QTRY_VERIFY(contentItem != 0);
4361 // confirm that a flick hits an item boundary
4362 flick(canvas, flickStart, flickEnd, 180);
4363 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4364 if (flow == QQuickGridView::FlowLeftToRight)
4365 QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
4367 QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
4371 flick(canvas, flickStart, flickEnd, 180);
4372 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4373 } while (flow == QQuickGridView::FlowLeftToRight
4374 ? !gridview->isAtYEnd()
4375 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4377 if (flow == QQuickGridView::FlowLeftToRight)
4378 QCOMPARE(gridview->contentY(), endExtent);
4380 QCOMPARE(gridview->contentX(), endExtent);
4384 flick(canvas, flickEnd, flickStart, 180);
4385 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4386 } while (flow == QQuickGridView::FlowLeftToRight
4387 ? !gridview->isAtYBeginning()
4388 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4390 if (flow == QQuickGridView::FlowLeftToRight)
4391 QCOMPARE(gridview->contentY(), startExtent);
4393 QCOMPARE(gridview->contentX(), startExtent);
4395 releaseView(canvas);
4398 void tst_QQuickGridView::snapOneRow_data()
4400 QTest::addColumn<QQuickGridView::Flow>("flow");
4401 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4402 QTest::addColumn<int>("highlightRangeMode");
4403 QTest::addColumn<QPoint>("flickStart");
4404 QTest::addColumn<QPoint>("flickEnd");
4405 QTest::addColumn<qreal>("snapAlignment");
4406 QTest::addColumn<qreal>("endExtent");
4407 QTest::addColumn<qreal>("startExtent");
4409 QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4410 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4412 QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4413 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4415 QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4416 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
4418 QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4419 << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4421 QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4422 << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4424 QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4425 << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
4428 void tst_QQuickGridView::snapOneRow()
4430 QFETCH(QQuickGridView::Flow, flow);
4431 QFETCH(Qt::LayoutDirection, layoutDirection);
4432 QFETCH(int, highlightRangeMode);
4433 QFETCH(QPoint, flickStart);
4434 QFETCH(QPoint, flickEnd);
4435 QFETCH(qreal, snapAlignment);
4436 QFETCH(qreal, endExtent);
4437 QFETCH(qreal, startExtent);
4439 QQuickView *canvas = getView();
4441 canvas->setSource(testFileUrl("snapOneRow.qml"));
4443 qApp->processEvents();
4445 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4446 QTRY_VERIFY(gridview != 0);
4448 gridview->setFlow(flow);
4449 gridview->setLayoutDirection(layoutDirection);
4450 gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4451 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4453 QQuickItem *contentItem = gridview->contentItem();
4454 QTRY_VERIFY(contentItem != 0);
4456 QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
4458 // confirm that a flick hits next row boundary
4459 flick(canvas, flickStart, flickEnd, 180);
4460 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4461 if (flow == QQuickGridView::FlowLeftToRight)
4462 QCOMPARE(gridview->contentY(), snapAlignment);
4464 QCOMPARE(gridview->contentX(), snapAlignment);
4466 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4467 QCOMPARE(gridview->currentIndex(), 2);
4468 QCOMPARE(currentIndexSpy.count(), 1);
4473 flick(canvas, flickStart, flickEnd, 180);
4474 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4475 } while (flow == QQuickGridView::FlowLeftToRight
4476 ? !gridview->isAtYEnd()
4477 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4479 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4480 QCOMPARE(gridview->currentIndex(), 6);
4481 QCOMPARE(currentIndexSpy.count(), 3);
4484 if (flow == QQuickGridView::FlowLeftToRight)
4485 QCOMPARE(gridview->contentY(), endExtent);
4487 QCOMPARE(gridview->contentX(), endExtent);
4491 flick(canvas, flickEnd, flickStart, 180);
4492 QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4493 } while (flow == QQuickGridView::FlowLeftToRight
4494 ? !gridview->isAtYBeginning()
4495 : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4497 if (flow == QQuickGridView::FlowLeftToRight)
4498 QCOMPARE(gridview->contentY(), startExtent);
4500 QCOMPARE(gridview->contentX(), startExtent);
4502 if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4503 QCOMPARE(gridview->currentIndex(), 0);
4504 QCOMPARE(currentIndexSpy.count(), 6);
4507 releaseView(canvas);
4511 void tst_QQuickGridView::unaligned()
4513 QQuickView *canvas = createView();
4517 for (int i = 0; i < 10; i++)
4518 model.addItem("Item" + QString::number(i), "");
4520 QQmlContext *ctxt = canvas->rootContext();
4521 ctxt->setContextProperty("testModel", &model);
4523 canvas->setSource(testFileUrl("unaligned.qml"));
4524 qApp->processEvents();
4526 QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
4527 QVERIFY(gridview != 0);
4529 QQuickItem *contentItem = gridview->contentItem();
4530 QVERIFY(contentItem != 0);
4532 for (int i = 0; i < 10; ++i) {
4533 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4534 if (!item) qWarning() << "Item" << i << "not found";
4536 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4537 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4541 for (int i = 10; i < 18; ++i) {
4542 model.addItem("Item" + QString::number(i), "");
4543 QQuickItem *item = 0;
4544 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4545 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4546 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4550 for (int i = 0; i < 10; ++i) {
4551 model.insertItem(i, "Item" + QString::number(i), "");
4552 QQuickItem *item = 0;
4553 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4554 QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4555 QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4559 model.removeItems(7, 10);
4560 QTRY_COMPARE(model.count(), gridview->count());
4561 for (int i = 0; i < 18; ++i) {
4562 QQuickItem *item = 0;
4563 QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4564 QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
4565 QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
4571 void tst_QQuickGridView::populateTransitions()
4573 QFETCH(bool, staticallyPopulate);
4574 QFETCH(bool, dynamicallyPopulate);
4575 QFETCH(bool, usePopulateTransition);
4577 QPointF transitionFrom(-50, -50);
4578 QPointF transitionVia(100, 100);
4579 QaimModel model_transitionFrom;
4580 QaimModel model_transitionVia;
4583 if (staticallyPopulate) {
4584 for (int i = 0; i < 30; i++)
4585 model.addItem("item" + QString::number(i), "");
4588 QQuickView *canvas = getView();
4589 canvas->rootContext()->setContextProperty("testModel", &model);
4590 canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4591 canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4592 canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4593 canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4594 canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4595 canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4596 canvas->setSource(testFileUrl("populateTransitions.qml"));
4599 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4601 QQuickItem *contentItem = gridview->contentItem();
4602 QVERIFY(contentItem);
4604 // check the populate transition is run
4605 if (staticallyPopulate && usePopulateTransition) {
4606 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 18);
4607 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4608 } else if (dynamicallyPopulate) {
4609 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4610 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 18);
4612 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4613 QCOMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4614 QCOMPARE(gridview->property("countAddTransitions").toInt(), 0);
4617 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4618 for (int i=0; i < model.count() && i < itemCount; ++i) {
4619 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4620 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4621 QTRY_COMPARE(item->x(), (i%3)*80.0);
4622 QTRY_COMPARE(item->y(), (i/3)*60.0);
4623 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4625 QTRY_COMPARE(name->text(), model.name(i));
4628 gridview->setProperty("countPopulateTransitions", 0);
4629 gridview->setProperty("countAddTransitions", 0);
4631 // add an item and check this is done with add transition, not populate
4632 model.insertItem(0, "another item", "");
4633 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
4634 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4637 canvas->rootContext()->setContextProperty("testModel", QVariant());
4638 QTRY_COMPARE(gridview->count(), 0);
4639 QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4640 gridview->setProperty("countPopulateTransitions", 0);
4641 gridview->setProperty("countAddTransitions", 0);
4643 // set to a valid model and check populate transition is run a second time
4645 for (int i = 0; i < 30; i++)
4646 model.addItem("item" + QString::number(i), "");
4647 canvas->rootContext()->setContextProperty("testModel", &model);
4648 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4650 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4651 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4653 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4654 for (int i=0; i < model.count() && i < itemCount; ++i) {
4655 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4656 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4657 QTRY_COMPARE(item->x(), (i%3)*80.0);
4658 QTRY_COMPARE(item->y(), (i/3)*60.0);
4659 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4661 QTRY_COMPARE(name->text(), model.name(i));
4664 // reset model and check populate transition is run again
4665 gridview->setProperty("countPopulateTransitions", 0);
4666 gridview->setProperty("countAddTransitions", 0);
4668 QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4669 QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4671 itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4672 for (int i=0; i < model.count() && i < itemCount; ++i) {
4673 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4674 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4675 QTRY_COMPARE(item->x(), (i%3)*80.0);
4676 QTRY_COMPARE(item->y(), (i/3)*60.0);
4677 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4679 QTRY_COMPARE(name->text(), model.name(i));
4682 releaseView(canvas);
4685 void tst_QQuickGridView::populateTransitions_data()
4687 QTest::addColumn<bool>("staticallyPopulate");
4688 QTest::addColumn<bool>("dynamicallyPopulate");
4689 QTest::addColumn<bool>("usePopulateTransition");
4691 QTest::newRow("static") << true << false << true;
4692 QTest::newRow("static, no populate") << true << false << false;
4694 QTest::newRow("dynamic") << false << true << true;
4695 QTest::newRow("dynamic, no populate") << false << true << false;
4697 QTest::newRow("empty to start with") << false << false << true;
4698 QTest::newRow("empty to start with, no populate") << false << false << false;
4701 void tst_QQuickGridView::addTransitions()
4703 QFETCH(int, initialItemCount);
4704 QFETCH(bool, shouldAnimateTargets);
4705 QFETCH(qreal, contentYRowOffset);
4706 QFETCH(int, insertionIndex);
4707 QFETCH(int, insertionCount);
4708 QFETCH(ListRange, expectedDisplacedIndexes);
4710 // added items should start here
4711 QPointF targetItems_transitionFrom(-50, -50);
4713 // displaced items should pass through this point
4714 QPointF displacedItems_transitionVia(100, 100);
4717 for (int i = 0; i < initialItemCount; i++)
4718 model.addItem("Original item" + QString::number(i), "");
4719 QaimModel model_targetItems_transitionFrom;
4720 QaimModel model_displacedItems_transitionVia;
4722 QQuickView *canvas = getView();
4723 QQmlContext *ctxt = canvas->rootContext();
4724 ctxt->setContextProperty("testModel", &model);
4725 ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4726 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4727 ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4728 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4729 canvas->setSource(testFileUrl("addTransitions.qml"));
4732 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4733 QTRY_VERIFY(gridview != 0);
4734 QQuickItem *contentItem = gridview->contentItem();
4735 QVERIFY(contentItem != 0);
4736 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4738 if (contentYRowOffset != 0) {
4739 gridview->setContentY(contentYRowOffset * 60.0);
4740 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4743 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4745 // only target items that will become visible should be animated
4746 QList<QPair<QString, QString> > newData;
4747 QList<QPair<QString, QString> > expectedTargetData;
4748 QList<int> targetIndexes;
4749 if (shouldAnimateTargets) {
4750 for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4751 newData << qMakePair(QString("New item %1").arg(i), QString(""));
4753 // last visible item is the first item of the row beneath the view
4754 if (i >= (gridview->contentY() / 60)*3 && i < qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) {
4755 expectedTargetData << newData.last();
4759 QVERIFY(expectedTargetData.count() > 0);
4763 if (!newData.isEmpty()) {
4764 model.insertItems(insertionIndex, newData);
4765 QTRY_COMPARE(model.count(), gridview->count());
4768 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4770 if (shouldAnimateTargets) {
4771 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4772 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4773 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4775 // check the target and displaced items were animated
4776 model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4777 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4779 // check attached properties
4780 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4781 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4782 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4783 if (expectedDisplacedIndexes.isValid()) {
4784 // adjust expectedDisplacedIndexes to their final values after the move
4785 QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4786 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4787 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4788 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4791 QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4792 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4795 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4796 int firstVisibleIndex = -1;
4797 for (int i=0; i<items.count(); i++) {
4798 if (items[i]->y() >= gridview->contentY()) {
4799 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4800 firstVisibleIndex = e.evaluate().toInt();
4804 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4806 // verify all items moved to the correct final positions
4807 for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4808 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4809 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4810 QCOMPARE(item->x(), (i%3)*80.0);
4811 QCOMPARE(item->y(), (i/3)*60.0);
4812 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4814 QCOMPARE(name->text(), model.name(i));
4817 releaseView(canvas);
4820 void tst_QQuickGridView::addTransitions_data()
4822 QTest::addColumn<int>("initialItemCount");
4823 QTest::addColumn<qreal>("contentYRowOffset");
4824 QTest::addColumn<bool>("shouldAnimateTargets");
4825 QTest::addColumn<int>("insertionIndex");
4826 QTest::addColumn<int>("insertionCount");
4827 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4829 // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4830 QTest::newRow("insert 1, just before start")
4831 << 30 << 1.0 << false
4832 << 0 << 1 << ListRange();
4833 QTest::newRow("insert 1, way before start")
4834 << 30 << 1.0 << false
4835 << 0 << 1 << ListRange();
4836 QTest::newRow("insert multiple, just before start")
4837 << 30 << 1.0 << false
4838 << 0 << 3 << ListRange();
4839 QTest::newRow("insert multiple (< 1 row), just before start")
4840 << 30 << 1.0 << false
4841 << 0 << 2 << ListRange();
4842 QTest::newRow("insert multiple, way before start")
4843 << 30 << 3.0 << false
4844 << 0 << 3 << ListRange();
4846 QTest::newRow("insert 1 at start")
4847 << 30 << 0.0 << true
4848 << 0 << 1 << ListRange(0, 17);
4849 QTest::newRow("insert multiple at start")
4850 << 30 << 0.0 << true
4851 << 0 << 3 << ListRange(0, 17);
4852 QTest::newRow("insert multiple (> 1 row) at start")
4853 << 30 << 0.0 << true
4854 << 0 << 5 << ListRange(0, 17);
4855 QTest::newRow("insert 1 at start, content y not 0")
4856 << 30 << 1.0 << true // first visible is index 3
4857 << 3 << 1 << ListRange(0 + 3, 17 + 3);
4858 QTest::newRow("insert multiple at start, content y not 0")
4859 << 30 << 1.0 << true // first visible is index 3
4860 << 3 << 3 << ListRange(0 + 3, 17 + 3);
4861 QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4862 << 30 << 1.0 << true // first visible is index 3
4863 << 3 << 5 << ListRange(0 + 3, 17 + 3);
4865 QTest::newRow("insert 1 at start, to empty grid")
4867 << 0 << 1 << ListRange();
4868 QTest::newRow("insert multiple at start, to empty grid")
4870 << 0 << 3 << ListRange();
4872 QTest::newRow("insert 1 at middle")
4873 << 30 << 0.0 << true
4874 << 7 << 1 << ListRange(7, 17);
4875 QTest::newRow("insert multiple at middle")
4876 << 30 << 0.0 << true
4877 << 7 << 3 << ListRange(7, 17);
4878 QTest::newRow("insert multiple (> 1 row) at middle")
4879 << 30 << 0.0 << true
4880 << 7 << 5 << ListRange(7, 17);
4882 QTest::newRow("insert 1 at bottom")
4883 << 30 << 0.0 << true
4884 << 17 << 1 << ListRange(17, 17);
4885 QTest::newRow("insert multiple at bottom")
4886 << 30 << 0.0 << true
4887 << 17 << 3 << ListRange(17, 17);
4888 QTest::newRow("insert 1 at bottom, content y not 0")
4889 << 30 << 1.0 << true
4890 << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4891 QTest::newRow("insert multiple at bottom, content y not 0")
4892 << 30 << 1.0 << true
4893 << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4896 // items added after the last visible will not be animated in, since they
4897 // do not appear in the final view
4898 QTest::newRow("insert 1 after end")
4899 << 30 << 0.0 << false
4900 << 18 << 1 << ListRange();
4901 QTest::newRow("insert multiple after end")
4902 << 30 << 0.0 << false
4903 << 18 << 3 << ListRange();
4906 void tst_QQuickGridView::moveTransitions()
4908 QFETCH(int, initialItemCount);
4909 QFETCH(qreal, contentYRowOffset);
4910 QFETCH(qreal, rowOffsetAfterMove);
4911 QFETCH(int, moveFrom);
4912 QFETCH(int, moveTo);
4913 QFETCH(int, moveCount);
4914 QFETCH(ListRange, expectedDisplacedIndexes);
4916 // target and displaced items should pass through these points
4917 QPointF targetItems_transitionVia(-50, 50);
4918 QPointF displacedItems_transitionVia(100, 100);
4921 for (int i = 0; i < initialItemCount; i++)
4922 model.addItem("Original item" + QString::number(i), "");
4923 QaimModel model_targetItems_transitionVia;
4924 QaimModel model_displacedItems_transitionVia;
4926 QQuickView *canvas = getView();
4927 QQmlContext *ctxt = canvas->rootContext();
4928 ctxt->setContextProperty("testModel", &model);
4929 ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4930 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4931 ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4932 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4933 canvas->setSource(testFileUrl("moveTransitions.qml"));
4936 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4937 QTRY_VERIFY(gridview != 0);
4938 QQuickItem *contentItem = gridview->contentItem();
4939 QVERIFY(contentItem != 0);
4942 if (contentYRowOffset != 0) {
4943 gridview->setContentY(contentYRowOffset * 60.0);
4944 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4947 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4949 // Items moving to *or* from visible positions should be animated.
4950 // Otherwise, they should not be animated.
4951 QList<QPair<QString, QString> > expectedTargetData;
4952 QList<int> targetIndexes;
4953 for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4954 int toIndex = moveTo + (i - moveFrom);
4955 int firstVisibleIndex = (gridview->contentY() / 60) * 3;
4956 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4957 if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4958 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4959 expectedTargetData << qMakePair(model.name(i), model.number(i));
4963 // ViewTransition.index provides the indices that items are moving to, not from
4964 targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4967 model.moveItems(moveFrom, moveTo, moveCount);
4969 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4970 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4971 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4973 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4975 // check the target and displaced items were animated
4976 model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4977 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4979 // check attached properties
4980 matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4981 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4982 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4983 if (expectedDisplacedIndexes.isValid()) {
4984 // adjust expectedDisplacedIndexes to their final values after the move
4985 QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4986 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4987 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4988 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4991 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4992 int firstVisibleIndex = -1;
4993 for (int i=0; i<items.count(); i++) {
4994 if (items[i]->y() >= gridview->contentY()) {
4995 QQmlExpression e(qmlContext(items[i]), items[i], "index");
4996 firstVisibleIndex = e.evaluate().toInt();
5000 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5002 // verify all items moved to the correct final positions
5003 qreal pixelOffset = 60 * rowOffsetAfterMove;
5004 for (int i=firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
5005 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5006 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5007 QCOMPARE(item->x(), (i%3)*80.0);
5008 QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
5009 name = findItem<QQuickText>(contentItem, "textName", i);
5011 QTRY_COMPARE(name->text(), model.name(i));
5014 releaseView(canvas);
5017 void tst_QQuickGridView::moveTransitions_data()
5019 QTest::addColumn<int>("initialItemCount");
5020 QTest::addColumn<qreal>("contentYRowOffset");
5021 QTest::addColumn<qreal>("rowOffsetAfterMove");
5022 QTest::addColumn<int>("moveFrom");
5023 QTest::addColumn<int>("moveTo");
5024 QTest::addColumn<int>("moveCount");
5025 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5027 QTest::newRow("move from above view, outside visible items, move 1")
5029 << 1 << 10 << 1 << ListRange(6, 10);
5030 QTest::newRow("move from above view, outside visible items, move 1 (first item)")
5032 << 0 << 10 << 1 << ListRange(6, 10);
5033 QTest::newRow("move from above view, outside visible items, move multiple")
5035 << 1 << 10 << 3 << ListRange(13, 23);
5036 QTest::newRow("move from above view, mix of visible/non-visible")
5038 << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
5039 QTest::newRow("move from above view, mix of visible/non-visible (move first)")
5041 << 0 << 10 << 6 << ListRange(16, 23);
5043 QTest::newRow("move within view, move 1 down")
5045 << 1 << 10 << 1 << ListRange(2, 10);
5046 QTest::newRow("move within view, move 1 down, move first item")
5048 << 0 << 10 << 1 << ListRange(1, 10);
5049 QTest::newRow("move within view, move 1 down, move first item, contentY not 0")
5051 << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
5052 QTest::newRow("move within view, move 1 down, to last item")
5054 << 10 << 17 << 1 << ListRange(11, 17);
5055 QTest::newRow("move within view, move first->last")
5057 << 0 << 17 << 1 << ListRange(1, 17);
5059 QTest::newRow("move within view, move multiple down")
5061 << 1 << 10 << 3 << ListRange(4, 12);
5062 QTest::newRow("move within view, move multiple down, move first item")
5064 << 0 << 10 << 3 << ListRange(3, 12);
5065 QTest::newRow("move within view, move multiple down, move first item, contentY not 0")
5067 << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
5068 QTest::newRow("move within view, move multiple down, displace last item")
5070 << 5 << 15 << 3 << ListRange(8, 17);
5071 QTest::newRow("move within view, move multiple down, move first->last")
5073 << 0 << 15 << 3 << ListRange(3, 17);
5075 QTest::newRow("move within view, move 1 up")
5077 << 10 << 1 << 1 << ListRange(1, 9);
5078 QTest::newRow("move within view, move 1 up, move to first index")
5080 << 10 << 0 << 1 << ListRange(0, 9);
5081 QTest::newRow("move within view, move 1 up, move to first index, contentY not 0")
5083 << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
5084 QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border")
5086 << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
5087 QTest::newRow("move within view, move 1 up, move last item")
5089 << 17 << 10 << 1 << ListRange(10, 16);
5090 QTest::newRow("move within view, move 1 up, move last->first")
5092 << 17 << 0 << 1 << ListRange(0, 16);
5094 QTest::newRow("move within view, move multiple up")
5096 << 10 << 1 << 3 << ListRange(1, 9);
5097 QTest::newRow("move within view, move multiple (> 1 row) up")
5099 << 10 << 1 << 5 << ListRange(1, 9);
5100 QTest::newRow("move within view, move multiple up, move to first index")
5102 << 10 << 0 << 3 << ListRange(0, 9);
5103 QTest::newRow("move within view, move multiple up, move to first index, contentY not 0")
5105 << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
5106 QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border")
5108 << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
5109 QTest::newRow("move within view, move multiple up, move last item")
5111 << 15 << 5 << 3 << ListRange(5, 14);
5112 QTest::newRow("move within view, move multiple up, move last->first")
5114 << 15 << 0 << 3 << ListRange(0, 14);
5116 QTest::newRow("move from below view, move 1 up")
5118 << 20 << 5 << 1 << ListRange(5, 17);
5119 QTest::newRow("move from below view, move 1 up, move to top")
5121 << 20 << 0 << 1 << ListRange(0, 17);
5122 QTest::newRow("move from below view, move 1 up, move to top, contentY not 0")
5124 << 25 << 3 << 1 << ListRange(0+3, 17+3);
5125 QTest::newRow("move from below view, move multiple (> 1 row) up")
5127 << 20 << 5 << 5 << ListRange(5, 17);
5128 QTest::newRow("move from below view, move multiple up, move to top")
5130 << 20 << 0 << 3 << ListRange(0, 17);
5131 QTest::newRow("move from below view, move multiple up, move to top, contentY not 0")
5133 << 25 << 3 << 3 << ListRange(0+3, 17+3);
5135 QTest::newRow("move from below view, move 1 up, move to bottom")
5137 << 20 << 17 << 1 << ListRange(17, 17);
5138 QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0")
5140 << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
5141 QTest::newRow("move from below view, move multiple up, move to to bottom")
5143 << 20 << 17 << 3 << ListRange(17, 17);
5144 QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0")
5146 << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
5149 void tst_QQuickGridView::removeTransitions()
5151 QFETCH(int, initialItemCount);
5152 QFETCH(bool, shouldAnimateTargets);
5153 QFETCH(qreal, contentYRowOffset);
5154 QFETCH(int, removalIndex);
5155 QFETCH(int, removalCount);
5156 QFETCH(ListRange, expectedDisplacedIndexes);
5158 // added items should end here
5159 QPointF targetItems_transitionTo(-50, -50);
5161 // displaced items should pass through this points
5162 QPointF displacedItems_transitionVia(100, 100);
5165 for (int i = 0; i < initialItemCount; i++)
5166 model.addItem("Original item" + QString::number(i), "");
5167 QaimModel model_targetItems_transitionTo;
5168 QaimModel model_displacedItems_transitionVia;
5170 QQuickView *canvas = getView();
5171 QQmlContext *ctxt = canvas->rootContext();
5172 ctxt->setContextProperty("testModel", &model);
5173 ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5174 ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5175 ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5176 ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5177 canvas->setSource(testFileUrl("removeTransitions.qml"));
5180 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5181 QTRY_VERIFY(gridview != 0);
5182 QQuickItem *contentItem = gridview->contentItem();
5183 QVERIFY(contentItem != 0);
5184 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5186 if (contentYRowOffset != 0) {
5187 gridview->setContentY(contentYRowOffset * 60.0);
5188 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5191 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5193 // only target items that are visible should be animated
5194 QList<QPair<QString, QString> > expectedTargetData;
5195 QList<int> targetIndexes;
5196 if (shouldAnimateTargets) {
5197 for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5198 int firstVisibleIndex = (gridview->contentY() / 60.0)*3;
5199 int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
5200 if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
5201 expectedTargetData << qMakePair(model.name(i), model.number(i));
5205 QVERIFY(expectedTargetData.count() > 0);
5208 // calculate targetItems and expectedTargets before model changes
5209 QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5210 QVariantMap expectedTargets;
5211 for (int i=0; i<targetIndexes.count(); i++)
5212 expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5215 model.removeItems(removalIndex, removalCount);
5216 QTRY_COMPARE(model.count(), gridview->count());
5218 if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
5219 QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5220 QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
5221 expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5223 // check the target and displaced items were animated
5224 model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5225 model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5227 // check attached properties
5228 QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
5229 matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5230 matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
5231 if (expectedDisplacedIndexes.isValid()) {
5232 // adjust expectedDisplacedIndexes to their final values after the move
5233 QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5234 matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5235 matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5236 matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
5239 QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5240 QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5243 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5244 int itemCount = items.count();
5245 int firstVisibleIndex = -1;
5246 for (int i=0; i<items.count(); i++) {
5247 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5248 int index = e.evaluate().toInt();
5249 if (firstVisibleIndex < 0 && items[i]->y() >= gridview->contentY())
5250 firstVisibleIndex = index;
5252 itemCount--; // exclude deleted items
5254 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5256 // verify all items moved to the correct final positions
5257 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5258 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5259 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5260 QCOMPARE(item->x(), (i%3)*80.0);
5261 QCOMPARE(item->y(), gridview->contentY() + ((i-firstVisibleIndex)/3) * 60.0);
5262 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5264 QTRY_COMPARE(name->text(), model.name(i));
5267 releaseView(canvas);
5270 void tst_QQuickGridView::removeTransitions_data()
5272 QTest::addColumn<int>("initialItemCount");
5273 QTest::addColumn<qreal>("contentYRowOffset");
5274 QTest::addColumn<bool>("shouldAnimateTargets");
5275 QTest::addColumn<int>("removalIndex");
5276 QTest::addColumn<int>("removalCount");
5277 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5279 // All items that are visible following the remove operation should be animated.
5280 // Remove targets that are outside of the view should not be animated.
5282 // For a GridView, removing any number of items other than a full row before the start
5283 // should displace all items in the view
5284 QTest::newRow("remove 1 before start")
5285 << 30 << 2.0 << false
5286 << 2 << 1 << ListRange(6, 24); // 6-24 are displaced
5287 QTest::newRow("remove 1 row, before start")
5288 << 30 << 2.0 << false
5289 << 3 << 3 << ListRange();
5290 QTest::newRow("remove between 1-2 rows, before start")
5291 << 30 << 2.0 << false
5292 << 0 << 5 << ListRange(6, 25);
5293 QTest::newRow("remove 2 rows, before start")
5294 << 30 << 2.0 << false
5295 << 0 << 6 << ListRange();
5296 QTest::newRow("remove mix of before and after start")
5297 << 30 << 1.0 << true
5298 << 2 << 3 << ListRange(5, 23); // 5-23 are displaced into view
5301 QTest::newRow("remove 1 from start")
5302 << 30 << 0.0 << true
5303 << 0 << 1 << ListRange(1, 18); // 1-18 are displaced into view
5304 QTest::newRow("remove multiple from start")
5305 << 30 << 0.0 << true
5306 << 0 << 3 << ListRange(3, 20); // 3-18 are displaced into view
5307 QTest::newRow("remove 1 from start, content y not 0")
5308 << 30 << 1.0 << true
5309 << 3 << 1 << ListRange(1 + 3, 18 + 3);
5310 QTest::newRow("remove multiple from start, content y not 0")
5311 << 30 << 1.0 << true
5312 << 3 << 3 << ListRange(3 + 3, 20 + 3);
5315 QTest::newRow("remove 1 from middle")
5316 << 30 << 0.0 << true
5317 << 5 << 1 << ListRange(6, 18);
5318 QTest::newRow("remove multiple from middle")
5319 << 30 << 0.0 << true
5320 << 5 << 3 << ListRange(8, 20);
5323 QTest::newRow("remove 1 from bottom")
5324 << 30 << 0.0 << true
5325 << 17 << 1 << ListRange(18, 18);
5326 QTest::newRow("remove multiple (1 row) from bottom")
5327 << 30 << 0.0 << true
5328 << 15 << 3 << ListRange(18, 20);
5329 QTest::newRow("remove multiple (> 1 row) from bottom")
5330 << 30 << 0.0 << true
5331 << 15 << 5 << ListRange(20, 22);
5332 QTest::newRow("remove 1 from bottom, content y not 0")
5333 << 30 << 1.0 << true
5334 << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
5335 QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
5336 << 30 << 1.0 << true
5337 << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
5340 QTest::newRow("remove 1 after end")
5341 << 30 << 0.0 << false
5342 << 18 << 1 << ListRange();
5343 QTest::newRow("remove multiple after end")
5344 << 30 << 0.0 << false
5345 << 18 << 3 << ListRange();
5348 void tst_QQuickGridView::displacedTransitions()
5350 QFETCH(bool, useDisplaced);
5351 QFETCH(bool, displacedEnabled);
5352 QFETCH(bool, useAddDisplaced);
5353 QFETCH(bool, addDisplacedEnabled);
5354 QFETCH(bool, useMoveDisplaced);
5355 QFETCH(bool, moveDisplacedEnabled);
5356 QFETCH(bool, useRemoveDisplaced);
5357 QFETCH(bool, removeDisplacedEnabled);
5358 QFETCH(ListChange, change);
5359 QFETCH(ListRange, expectedDisplacedIndexes);
5362 for (int i = 0; i < 30; i++)
5363 model.addItem("Original item" + QString::number(i), "");
5364 QaimModel model_displaced_transitionVia;
5365 QaimModel model_addDisplaced_transitionVia;
5366 QaimModel model_moveDisplaced_transitionVia;
5367 QaimModel model_removeDisplaced_transitionVia;
5369 QPointF displaced_transitionVia(-50, -100);
5370 QPointF addDisplaced_transitionVia(-150, 100);
5371 QPointF moveDisplaced_transitionVia(50, -100);
5372 QPointF removeDisplaced_transitionVia(150, 100);
5374 QQuickView *canvas = getView();
5375 QQmlContext *ctxt = canvas->rootContext();
5376 ctxt->setContextProperty("testModel", &model);
5377 ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
5378 ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
5379 ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
5380 ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
5381 ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
5382 ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
5383 ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
5384 ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
5385 ctxt->setContextProperty("useDisplaced", useDisplaced);
5386 ctxt->setContextProperty("displacedEnabled", displacedEnabled);
5387 ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
5388 ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
5389 ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
5390 ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
5391 ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
5392 ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
5393 canvas->setSource(testFileUrl("displacedTransitions.qml"));
5395 qApp->processEvents();
5397 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5398 QTRY_VERIFY(gridview != 0);
5399 QQuickItem *contentItem = gridview->contentItem();
5400 QVERIFY(contentItem != 0);
5401 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5403 QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5404 gridview->setProperty("displaceTransitionsDone", false);
5406 switch (change.type) {
5407 case ListChange::Inserted:
5409 QList<QPair<QString, QString> > targetItemData;
5410 for (int i=change.index; i<change.index + change.count; ++i)
5411 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
5412 model.insertItems(change.index, targetItemData);
5413 QTRY_COMPARE(model.count(), gridview->count());
5416 case ListChange::Removed:
5417 model.removeItems(change.index, change.count);
5418 QTRY_COMPARE(model.count(), gridview->count());
5420 case ListChange::Moved:
5421 model.moveItems(change.index, change.to, change.count);
5422 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5424 case ListChange::SetCurrent:
5425 case ListChange::SetContentY:
5429 QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
5430 QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
5432 if ((useDisplaced && displacedEnabled)
5433 || (useAddDisplaced && addDisplacedEnabled)
5434 || (useMoveDisplaced && moveDisplacedEnabled)
5435 || (useRemoveDisplaced && removeDisplacedEnabled)) {
5436 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5438 // check the correct number of target items and indexes were received
5439 QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
5440 for (int i=0; i<resultTargetIndexes.count(); i++)
5441 QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
5442 QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
5443 for (int i=0; i<resultTargetItems.count(); i++)
5444 QCOMPARE(resultTargetItems[i].toList().count(), change.count);
5446 QCOMPARE(resultTargetIndexes.count(), 0);
5447 QCOMPARE(resultTargetItems.count(), 0);
5450 if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
5451 model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
5453 QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
5454 if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
5455 model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
5457 QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
5458 if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
5459 model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
5461 QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
5463 if (useDisplaced && displacedEnabled
5464 && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
5465 || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
5466 || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
5467 model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
5469 QCOMPARE(model_displaced_transitionVia.count(), 0);
5472 // verify all items moved to the correct final positions
5473 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5474 for (int i=0; i < model.count() && i < items.count(); ++i) {
5475 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5476 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5477 QCOMPARE(item->x(), (i%3)*80.0);
5478 QCOMPARE(item->y(), (i/3)*60.0);
5479 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5481 QTRY_COMPARE(name->text(), model.name(i));
5484 releaseView(canvas);
5487 void tst_QQuickGridView::displacedTransitions_data()
5489 QTest::addColumn<bool>("useDisplaced");
5490 QTest::addColumn<bool>("displacedEnabled");
5491 QTest::addColumn<bool>("useAddDisplaced");
5492 QTest::addColumn<bool>("addDisplacedEnabled");
5493 QTest::addColumn<bool>("useMoveDisplaced");
5494 QTest::addColumn<bool>("moveDisplacedEnabled");
5495 QTest::addColumn<bool>("useRemoveDisplaced");
5496 QTest::addColumn<bool>("removeDisplacedEnabled");
5497 QTest::addColumn<ListChange>("change");
5498 QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5500 QTest::newRow("no displaced transitions at all")
5505 << ListChange::insert(0, 1) << ListRange(0, 17);
5507 QTest::newRow("just displaced")
5512 << ListChange::insert(0, 1) << ListRange(0, 17);
5514 QTest::newRow("just displaced (not enabled)")
5519 << ListChange::insert(0, 1) << ListRange(0, 17);
5521 QTest::newRow("displaced + addDisplaced")
5526 << ListChange::insert(0, 1) << ListRange(0, 17);
5528 QTest::newRow("displaced + addDisplaced (not enabled)")
5533 << ListChange::insert(0, 1) << ListRange(0, 17);
5535 QTest::newRow("displaced + moveDisplaced")
5540 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5542 QTest::newRow("displaced + moveDisplaced (not enabled)")
5547 << ListChange::move(0, 10, 1) << ListRange(1, 10);
5549 QTest::newRow("displaced + removeDisplaced")
5554 << ListChange::remove(0, 1) << ListRange(1, 18);
5556 QTest::newRow("displaced + removeDisplaced (not enabled)")
5561 << ListChange::remove(0, 1) << ListRange(1, 18);
5564 QTest::newRow("displaced + add, should use generic displaced for a remove")
5569 << ListChange::remove(0, 1) << ListRange(1, 18);
5572 void tst_QQuickGridView::multipleTransitions()
5574 // Tests that if you interrupt a transition in progress with another action that
5575 // cancels the previous transition, the resulting items are still placed correctly.
5577 QFETCH(int, initialCount);
5578 QFETCH(qreal, contentY);
5579 QFETCH(QList<ListChange>, changes);
5580 QFETCH(bool, enableAddTransitions);
5581 QFETCH(bool, enableMoveTransitions);
5582 QFETCH(bool, enableRemoveTransitions);
5583 QFETCH(bool, rippleAddDisplaced);
5585 // add transitions on the left, moves on the right
5586 QPointF addTargets_transitionFrom(-50, -50);
5587 QPointF addDisplaced_transitionFrom(-50, 50);
5588 QPointF moveTargets_transitionFrom(50, -50);
5589 QPointF moveDisplaced_transitionFrom(50, 50);
5590 QPointF removeTargets_transitionTo(-100, 300);
5591 QPointF removeDisplaced_transitionFrom(100, 300);
5594 for (int i = 0; i < initialCount; i++)
5595 model.addItem("Original item" + QString::number(i), "");
5597 QQuickView *canvas = getView();
5598 QQmlContext *ctxt = canvas->rootContext();
5599 ctxt->setContextProperty("testModel", &model);
5600 ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5601 ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5602 ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5603 ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5604 ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5605 ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5606 ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5607 ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5608 ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5609 ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5610 canvas->setSource(testFileUrl("multipleTransitions.qml"));
5612 QTest::qWaitForWindowShown(canvas);
5614 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5615 QTRY_VERIFY(gridview != 0);
5616 QQuickItem *contentItem = gridview->contentItem();
5617 QVERIFY(contentItem != 0);
5618 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5620 if (contentY != 0) {
5621 gridview->setContentY(contentY);
5622 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5625 int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5627 for (int i=0; i<changes.count(); i++) {
5628 switch (changes[i].type) {
5629 case ListChange::Inserted:
5631 QList<QPair<QString, QString> > targetItems;
5632 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5633 targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5634 model.insertItems(changes[i].index, targetItems);
5635 QTRY_COMPARE(model.count(), gridview->count());
5636 if (i == changes.count() - 1) {
5637 QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
5638 QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
5640 QTest::qWait(timeBetweenActions);
5644 case ListChange::Removed:
5645 model.removeItems(changes[i].index, changes[i].count);
5646 QTRY_COMPARE(model.count(), gridview->count());
5647 if (i == changes.count() - 1) {
5648 QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
5649 QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
5651 QTest::qWait(timeBetweenActions);
5654 case ListChange::Moved:
5655 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5656 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5657 if (i == changes.count() - 1) {
5658 QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
5659 QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
5661 QTest::qWait(timeBetweenActions);
5664 case ListChange::SetCurrent:
5665 gridview->setCurrentIndex(changes[i].index);
5666 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5668 case ListChange::SetContentY:
5669 gridview->setContentY(changes[i].pos);
5670 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5674 QCOMPARE(gridview->count(), model.count());
5676 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5677 int firstVisibleIndex = -1;
5678 for (int i=0; i<items.count(); i++) {
5679 if (items[i]->y() >= contentY) {
5680 QQmlExpression e(qmlContext(items[i]), items[i], "index");
5681 firstVisibleIndex = e.evaluate().toInt();
5685 QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5687 // verify all items moved to the correct final positions
5688 int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5689 for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5690 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5691 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5692 QTRY_COMPARE(item->x(), (i%3)*80.0);
5693 QTRY_COMPARE(item->y(), (i/3)*60.0);
5694 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5696 QTRY_COMPARE(name->text(), model.name(i));
5699 releaseView(canvas);
5702 void tst_QQuickGridView::multipleTransitions_data()
5704 QTest::addColumn<int>("initialCount");
5705 QTest::addColumn<qreal>("contentY");
5706 QTest::addColumn<QList<ListChange> >("changes");
5707 QTest::addColumn<bool>("enableAddTransitions");
5708 QTest::addColumn<bool>("enableMoveTransitions");
5709 QTest::addColumn<bool>("enableRemoveTransitions");
5710 QTest::addColumn<bool>("rippleAddDisplaced");
5712 // the added item and displaced items should move to final dest correctly
5713 QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5714 << ListChange::insert(0, 1)
5715 << ListChange::move(0, 3, 1)
5717 << true << true << true << false;
5719 // items affected by the add should change from move to add transition
5720 QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5721 << ListChange::move(1, 10, 3)
5722 << ListChange::insert(0, 1)
5724 << true << true << true << false;
5726 // items should be placed correctly if you trigger a transition then refill for that index
5727 QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5728 << ListChange::insert(0, 1)
5729 << ListChange::setContentY(160.0)
5730 << ListChange::setContentY(0.0)
5731 << ListChange::insert(0, 1)
5733 << true << true << true << false;
5735 QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5736 << ListChange::insert(1, 1)
5737 << ListChange::remove(1, 1)
5739 << true << true << true << true;
5741 // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5742 // even if a remove-displace transition is not present to re-animate them
5743 QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5744 << ListChange::insert(0, 1)
5745 << ListChange::remove(2, 1)
5747 << true << true << false << false;
5749 // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5750 // remove has changed the position of where it will move to
5751 QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5752 << ListChange::setContentY(-10.0)
5753 << ListChange::insert(0, 1)
5754 << ListChange::insert(0, 1)
5755 << ListChange::remove(2, 1)
5757 << true << true << false << false;
5760 void tst_QQuickGridView::multipleDisplaced()
5762 // multiple move() operations should only restart displace transitions for items that
5763 // moved from previously set positions, and not those that have moved from their current
5764 // item positions (which may e.g. still be changing from easing bounces in the last transition)
5767 for (int i = 0; i < 30; i++)
5768 model.addItem("Original item" + QString::number(i), "");
5770 QQuickView *canvas = createView();
5771 QQmlContext *ctxt = canvas->rootContext();
5772 ctxt->setContextProperty("testModel", &model);
5773 canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5775 QTest::qWaitForWindowShown(canvas);
5777 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5778 QTRY_VERIFY(gridview != 0);
5779 QQuickItem *contentItem = gridview->contentItem();
5780 QVERIFY(contentItem != 0);
5781 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5783 model.moveItems(12, 8, 1);
5784 QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5785 model.moveItems(8, 3, 1);
5786 QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5788 QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5789 foreach (const QString &name, transitionsStarted.keys()) {
5790 QVERIFY2(transitionsStarted[name] == 1,
5791 QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5794 // verify all items moved to the correct final positions
5795 QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5796 for (int i=0; i < model.count() && i < items.count(); ++i) {
5797 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5798 QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5799 QTRY_COMPARE(item->x(), (i%3)*80.0);
5800 QTRY_COMPARE(item->y(), (i/3)*60.0);
5801 QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5803 QTRY_COMPARE(name->text(), model.name(i));
5809 void tst_QQuickGridView::cacheBuffer()
5811 QQuickView *canvas = createView();
5814 for (int i = 0; i < 90; i++)
5815 model.addItem("Item" + QString::number(i), "");
5817 canvas->rootContext()->setContextProperty("testModel", &model);
5818 canvas->setSource(testFileUrl("gridview1.qml"));
5820 qApp->processEvents();
5822 QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5823 QVERIFY(gridview != 0);
5825 QQuickItem *contentItem = gridview->contentItem();
5826 QVERIFY(contentItem != 0);
5827 QVERIFY(gridview->delegate() != 0);
5828 QVERIFY(gridview->model() != 0);
5830 // Confirm items positioned correctly
5831 int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5832 for (int i = 0; i < model.count() && i < itemCount; ++i) {
5833 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5834 QTRY_COMPARE(item->x(), (i%3)*80.0);
5835 QTRY_COMPARE(item->y(), (i/3)*60.0);
5838 QQmlIncubationController controller;
5839 canvas->engine()->setIncubationController(&controller);
5841 canvas->rootObject()->setProperty("cacheBuffer", 200);
5842 QTRY_VERIFY(gridview->cacheBuffer() == 200);
5844 // items will be created one at a time
5845 for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5846 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5847 QQuickItem *item = 0;
5850 controller.incubateWhile(&b);
5851 item = findItem<QQuickItem>(gridview, "wrapper", i);
5857 controller.incubateWhile(&b);
5860 int newItemCount = 0;
5861 newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5863 // Confirm items positioned correctly
5864 for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5865 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5867 QTRY_COMPARE(item->x(), (i%3)*80.0);
5868 QTRY_COMPARE(item->y(), (i/3)*60.0);
5871 // move view and confirm items in view are visible immediately and outside are created async
5872 gridview->setContentY(300);
5874 for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
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 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5883 // ensure buffered items are created
5884 for (int i = 34; i < qMin(44,model.count()); ++i) {
5885 QQuickItem *item = 0;
5887 qGuiApp->processEvents(); // allow refill to happen
5889 controller.incubateWhile(&b);
5890 item = findItem<QQuickItem>(gridview, "wrapper", i);
5896 controller.incubateWhile(&b);
5902 void tst_QQuickGridView::asynchronous()
5904 QQuickView *canvas = createView();
5906 QQmlIncubationController controller;
5907 canvas->engine()->setIncubationController(&controller);
5909 canvas->setSource(testFileUrl("asyncloader.qml"));
5911 QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5912 QVERIFY(rootObject);
5914 QQuickGridView *gridview = 0;
5917 controller.incubateWhile(&b);
5918 gridview = rootObject->findChild<QQuickGridView*>("view");
5921 // items will be created one at a time
5922 for (int i = 0; i < 12; ++i) {
5923 QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5924 QQuickItem *item = 0;
5927 controller.incubateWhile(&b);
5928 item = findItem<QQuickItem>(gridview, "wrapper", i);
5934 controller.incubateWhile(&b);
5937 // verify positioning
5938 QQuickItem *contentItem = gridview->contentItem();
5939 for (int i = 0; i < 12; ++i) {
5940 QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5941 if (!item) qWarning() << "Item" << i << "not found";
5942 QVERIFY(item->x() == (i%3)*100);
5943 QVERIFY(item->y() == (i/3)*100);
5949 void tst_QQuickGridView::unrequestedVisibility()
5952 for (int i = 0; i < 30; i++)
5953 model.addItem("Item" + QString::number(i), QString::number(i));
5955 QQuickView *canvas = new QQuickView(0);
5956 canvas->setGeometry(0,0,240,320);
5958 QQmlContext *ctxt = canvas->rootContext();
5959 ctxt->setContextProperty("testModel", &model);
5960 ctxt->setContextProperty("testWrap", QVariant(false));
5962 canvas->setSource(testFileUrl("unrequestedItems.qml"));
5966 qApp->processEvents();
5968 QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5969 QTRY_VERIFY(leftview != 0);
5971 QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5972 QTRY_VERIFY(rightview != 0);
5974 QQuickItem *leftContent = leftview->contentItem();
5975 QTRY_VERIFY(leftContent != 0);
5977 QQuickItem *rightContent = rightview->contentItem();
5978 QTRY_VERIFY(rightContent != 0);
5980 rightview->setCurrentIndex(12);
5982 QTRY_COMPARE(leftview->contentY(), 0.0);
5983 QTRY_COMPARE(rightview->contentY(), 240.0);
5987 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5988 QCOMPARE(delegateVisible(item), true);
5989 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5990 QCOMPARE(delegateVisible(item), false);
5992 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5993 QCOMPARE(delegateVisible(item), false);
5994 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5995 QCOMPARE(delegateVisible(item), true);
5997 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5998 QCOMPARE(delegateVisible(item), true);
5999 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
6000 QCOMPARE(delegateVisible(item), false);
6001 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
6002 QCOMPARE(delegateVisible(item), false);
6003 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
6004 QCOMPARE(delegateVisible(item), true);
6006 rightview->setCurrentIndex(0);
6008 QTRY_COMPARE(leftview->contentY(), 0.0);
6009 QTRY_COMPARE(rightview->contentY(), 0.0);
6011 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
6012 QCOMPARE(delegateVisible(item), true);
6013 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6014 QTRY_COMPARE(delegateVisible(item), true);
6016 QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
6017 QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
6019 leftview->setCurrentIndex(12);
6021 QTRY_COMPARE(leftview->contentY(), 240.0);
6022 QTRY_COMPARE(rightview->contentY(), 0.0);
6024 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
6025 QTRY_COMPARE(delegateVisible(item), false);
6026 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6027 QCOMPARE(delegateVisible(item), true);
6029 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
6030 QCOMPARE(delegateVisible(item), true);
6031 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
6032 QCOMPARE(delegateVisible(item), false);
6034 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6035 QCOMPARE(delegateVisible(item), false);
6036 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6037 QCOMPARE(delegateVisible(item), true);
6038 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6039 QCOMPARE(delegateVisible(item), true);
6040 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6041 QCOMPARE(delegateVisible(item), false);
6043 // move a non-visible item into view
6044 model.moveItems(10, 9, 1);
6045 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6047 QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
6048 QCOMPARE(delegateVisible(item), false);
6049 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
6050 QCOMPARE(delegateVisible(item), true);
6052 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
6053 QCOMPARE(delegateVisible(item), true);
6054 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
6055 QCOMPARE(delegateVisible(item), false);
6057 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6058 QCOMPARE(delegateVisible(item), false);
6059 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6060 QCOMPARE(delegateVisible(item), true);
6061 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6062 QCOMPARE(delegateVisible(item), true);
6063 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6064 QCOMPARE(delegateVisible(item), false);
6066 // move a visible item out of view
6067 model.moveItems(5, 3, 1);
6068 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6070 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6071 QCOMPARE(delegateVisible(item), false);
6072 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6073 QCOMPARE(delegateVisible(item), true);
6074 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6075 QCOMPARE(delegateVisible(item), true);
6076 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6077 QCOMPARE(delegateVisible(item), false);
6079 // move a non-visible item into view
6080 model.moveItems(3, 5, 1);
6081 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6083 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6084 QCOMPARE(delegateVisible(item), false);
6085 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6086 QCOMPARE(delegateVisible(item), true);
6087 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6088 QCOMPARE(delegateVisible(item), true);
6089 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6090 QCOMPARE(delegateVisible(item), false);
6092 // move a visible item out of view
6093 model.moveItems(9, 10, 1);
6094 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6096 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6097 QCOMPARE(delegateVisible(item), false);
6098 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6099 QCOMPARE(delegateVisible(item), true);
6100 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6101 QCOMPARE(delegateVisible(item), true);
6102 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6103 QCOMPARE(delegateVisible(item), false);
6105 // move a non-visible item into view
6106 model.moveItems(10, 9, 1);
6107 QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6109 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6110 QCOMPARE(delegateVisible(item), false);
6111 QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6112 QCOMPARE(delegateVisible(item), true);
6113 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6114 QCOMPARE(delegateVisible(item), true);
6115 QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6116 QCOMPARE(delegateVisible(item), false);
6122 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB()
6124 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6127 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB_data()
6129 inserted_defaultLayout_data();
6132 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB()
6134 inserted_defaultLayout(QQuickGridView::FlowTopToBottom);
6137 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB_data()
6139 inserted_defaultLayout_data();
6142 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB()
6144 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6147 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB_data()
6149 inserted_defaultLayout_data();
6152 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT()
6154 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6157 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT_data()
6159 inserted_defaultLayout_data();
6162 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT()
6164 inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6167 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT_data()
6169 inserted_defaultLayout_data();
6172 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT()
6174 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6177 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT_data()
6179 inserted_defaultLayout_data();
6182 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT()
6184 inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6187 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT_data()
6189 inserted_defaultLayout_data();
6193 void tst_QQuickGridView::removed_leftToRight_RtL_TtB()
6195 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6198 void tst_QQuickGridView::removed_leftToRight_RtL_TtB_data()
6200 removed_defaultLayout_data();
6203 void tst_QQuickGridView::removed_topToBottom_LtR_TtB()
6205 removed_defaultLayout(QQuickGridView::FlowTopToBottom);
6208 void tst_QQuickGridView::removed_topToBottom_LtR_TtB_data()
6210 removed_defaultLayout_data();
6213 void tst_QQuickGridView::removed_topToBottom_RtL_TtB()
6215 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6218 void tst_QQuickGridView::removed_topToBottom_RtL_TtB_data()
6220 removed_defaultLayout_data();
6223 void tst_QQuickGridView::removed_leftToRight_LtR_BtT()
6225 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6228 void tst_QQuickGridView::removed_leftToRight_LtR_BtT_data()
6230 removed_defaultLayout_data();
6233 void tst_QQuickGridView::removed_leftToRight_RtL_BtT()
6235 removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6238 void tst_QQuickGridView::removed_leftToRight_RtL_BtT_data()
6240 removed_defaultLayout_data();
6243 void tst_QQuickGridView::removed_topToBottom_LtR_BtT()
6245 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6248 void tst_QQuickGridView::removed_topToBottom_LtR_BtT_data()
6250 removed_defaultLayout_data();
6253 void tst_QQuickGridView::removed_topToBottom_RtL_BtT()
6255 removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6258 void tst_QQuickGridView::removed_topToBottom_RtL_BtT_data()
6260 removed_defaultLayout_data();
6264 void tst_QQuickGridView::moved_leftToRight_RtL_TtB()
6266 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6269 void tst_QQuickGridView::moved_leftToRight_RtL_TtB_data()
6271 moved_defaultLayout_data();
6274 void tst_QQuickGridView::moved_topToBottom_LtR_TtB()
6276 moved_defaultLayout(QQuickGridView::FlowTopToBottom);
6279 void tst_QQuickGridView::moved_topToBottom_LtR_TtB_data()
6281 moved_defaultLayout_data();
6284 void tst_QQuickGridView::moved_topToBottom_RtL_TtB()
6286 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6289 void tst_QQuickGridView::moved_topToBottom_RtL_TtB_data()
6291 moved_defaultLayout_data();
6294 void tst_QQuickGridView::moved_leftToRight_LtR_BtT()
6296 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6299 void tst_QQuickGridView::moved_leftToRight_LtR_BtT_data()
6301 moved_defaultLayout_data();
6304 void tst_QQuickGridView::moved_leftToRight_RtL_BtT()
6306 moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6309 void tst_QQuickGridView::moved_leftToRight_RtL_BtT_data()
6311 moved_defaultLayout_data();
6314 void tst_QQuickGridView::moved_topToBottom_LtR_BtT()
6316 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6319 void tst_QQuickGridView::moved_topToBottom_LtR_BtT_data()
6321 moved_defaultLayout_data();
6324 void tst_QQuickGridView::moved_topToBottom_RtL_BtT()
6326 moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6329 void tst_QQuickGridView::moved_topToBottom_RtL_BtT_data()
6331 moved_defaultLayout_data();
6335 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
6339 for (int i=0; i<list.count(); i++) {
6340 ret << list[i].toInt(&ok);
6342 qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
6348 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6350 for (int i=0; i<indexLists.count(); i++) {
6351 QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6352 if (current != expectedIndexes.toSet())
6353 qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6354 QCOMPARE(current, expectedIndexes.toSet());
6358 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6360 for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6361 QVERIFY(it.value().type() == QVariant::Int);
6362 QString name = it.key();
6363 int itemIndex = it.value().toInt();
6364 QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6365 if (model.name(itemIndex) != name)
6366 qDebug() << itemIndex;
6367 QCOMPARE(model.name(itemIndex), name);
6369 QCOMPARE(items.count(), expectedIndexes.count());
6372 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6374 for (int i=0; i<itemLists.count(); i++) {
6375 QVariantList current = itemLists[i].toList();
6376 for (int j=0; j<current.count(); j++) {
6377 QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6378 QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6379 QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6381 QCOMPARE(current.count(), expectedItems.count());
6385 QTEST_MAIN(tst_QQuickGridView)
6387 #include "tst_qquickgridview.moc"