Only run populate for items in model at creation
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquickgridview / tst_qquickgridview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QtCore/qstringlistmodel.h>
44 #include <QtQuick/qquickview.h>
45 #include <QtQml/qqmlengine.h>
46 #include <QtQml/qqmlcomponent.h>
47 #include <QtQml/qqmlcontext.h>
48 #include <QtQml/qqmlexpression.h>
49 #include <QtQml/qqmlincubator.h>
50 #include <QtQml/qqmlcontext.h>
51 #include <QtQuick/private/qquickitem_p.h>
52 #include <QtQuick/private/qquickgridview_p.h>
53 #include <QtQuick/private/qquicktext_p.h>
54 #include <QtQuick/private/qquickvisualitemmodel_p.h>
55 #include <QtQml/private/qquicklistmodel_p.h>
56 #include <QtQml/private/qlistmodelinterface_p.h>
57 #include "../../shared/util.h"
58 #include "../shared/viewtestutil.h"
59 #include "../shared/visualtestutil.h"
60 #include <QtGui/qguiapplication.h>
61
62 Q_DECLARE_METATYPE(QQuickGridView::Flow)
63 Q_DECLARE_METATYPE(Qt::LayoutDirection)
64 Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection)
65 Q_DECLARE_METATYPE(Qt::Key)
66
67 using namespace QQuickViewTestUtil;
68 using namespace QQuickVisualTestUtil;
69
70 #define SHARE_VIEWS
71
72 class tst_QQuickGridView : public QQmlDataTest
73 {
74     Q_OBJECT
75 public:
76     tst_QQuickGridView();
77
78 private slots:
79     void init();
80     void items();
81     void changed();
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();
87     void removed_basic();
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();
92     void clear();
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();
100     void changeFlow();
101     void currentIndex();
102     void noCurrentIndex();
103     void keyNavigation();
104     void keyNavigation_data();
105     void defaultValues();
106     void properties();
107     void propertyChanges();
108     void componentChanges();
109     void modelChanges();
110     void positionViewAtIndex();
111     void positionViewAtIndex_rightToLeft();
112     void mirroring();
113     void snapping();
114     void resetModel();
115     void enforceRange();
116     void enforceRange_rightToLeft();
117     void QTBUG_8456();
118     void manualHighlight();
119     void footer();
120     void footer_data();
121     void header();
122     void header_data();
123     void headerFooter();
124     void headerFooter_data();
125     void resetModel_headerFooter();
126     void resizeViewAndRepaint();
127     void resizeGrid();
128     void resizeGrid_data();
129     void changeColumnCount();
130     void indexAt_itemAt_data();
131     void indexAt_itemAt();
132     void onAdd();
133     void onAdd_data();
134     void onRemove();
135     void onRemove_data();
136     void columnCount();
137     void margins();
138     void creationContext();
139     void snapToRow_data();
140     void snapToRow();
141     void snapOneRow_data();
142     void snapOneRow();
143     void unaligned();
144     void cacheBuffer();
145     void asynchronous();
146     void unrequestedVisibility();
147
148     void populateTransitions();
149     void populateTransitions_data();
150     void addTransitions();
151     void addTransitions_data();
152     void moveTransitions();
153     void moveTransitions_data();
154     void removeTransitions();
155     void removeTransitions_data();
156     void displacedTransitions();
157     void displacedTransitions_data();
158     void multipleTransitions();
159     void multipleTransitions_data();
160     void multipleDisplaced();
161
162     void inserted_leftToRight_RtL_TtB();
163     void inserted_leftToRight_RtL_TtB_data();
164     void inserted_leftToRight_LtR_BtT();
165     void inserted_leftToRight_LtR_BtT_data();
166     void inserted_leftToRight_RtL_BtT();
167     void inserted_leftToRight_RtL_BtT_data();
168     void inserted_topToBottom_LtR_TtB();
169     void inserted_topToBottom_LtR_TtB_data();
170     void inserted_topToBottom_RtL_TtB();
171     void inserted_topToBottom_RtL_TtB_data();
172     void inserted_topToBottom_LtR_BtT();
173     void inserted_topToBottom_LtR_BtT_data();
174     void inserted_topToBottom_RtL_BtT();
175     void inserted_topToBottom_RtL_BtT_data();
176
177     void removed_leftToRight_RtL_TtB();
178     void removed_leftToRight_RtL_TtB_data();
179     void removed_leftToRight_LtR_BtT();
180     void removed_leftToRight_LtR_BtT_data();
181     void removed_leftToRight_RtL_BtT();
182     void removed_leftToRight_RtL_BtT_data();
183     void removed_topToBottom_LtR_TtB();
184     void removed_topToBottom_LtR_TtB_data();
185     void removed_topToBottom_RtL_TtB();
186     void removed_topToBottom_RtL_TtB_data();
187     void removed_topToBottom_LtR_BtT();
188     void removed_topToBottom_LtR_BtT_data();
189     void removed_topToBottom_RtL_BtT();
190     void removed_topToBottom_RtL_BtT_data();
191
192     void moved_leftToRight_RtL_TtB();
193     void moved_leftToRight_RtL_TtB_data();
194     void moved_leftToRight_LtR_BtT();
195     void moved_leftToRight_LtR_BtT_data();
196     void moved_leftToRight_RtL_BtT();
197     void moved_leftToRight_RtL_BtT_data();
198     void moved_topToBottom_LtR_TtB();
199     void moved_topToBottom_LtR_TtB_data();
200     void moved_topToBottom_RtL_TtB();
201     void moved_topToBottom_RtL_TtB_data();
202     void moved_topToBottom_LtR_BtT();
203     void moved_topToBottom_LtR_BtT_data();
204     void moved_topToBottom_RtL_BtT();
205     void moved_topToBottom_RtL_BtT_data();
206
207 private:
208     QList<int> toIntList(const QVariantList &list);
209     void matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes);
210     void matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes);
211     void matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems);
212
213     void multipleChanges(bool condensed);
214     void multipleChanges_data();
215
216     QPointF expectedItemPos(QQuickGridView *grid, int index, qreal rowOffset = 0) {
217         qreal x;
218         qreal y;
219         if (grid->flow() == QQuickGridView::FlowLeftToRight) {
220             int columns = grid->width() / grid->cellWidth();
221             x = (index % columns) * grid->cellWidth();
222             y = (index / columns) * grid->cellHeight();
223             if (grid->effectiveLayoutDirection() == Qt::RightToLeft) {
224                 int col = (index % columns) * grid->cellWidth();
225                 x = grid->cellWidth() * (columns - 1) - col;
226             }
227
228             qreal offset = grid->cellHeight() * rowOffset;
229             if (grid->verticalLayoutDirection() == QQuickItemView::TopToBottom)
230                 y += offset;
231             else
232                 y = -grid->cellHeight() - y - offset;
233         } else {
234             int rows = grid->height() / grid->cellHeight();
235             x = (index / rows) * grid->cellWidth();
236             y = (index % rows) * grid->cellHeight();
237             if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
238                 x = -x - grid->cellWidth();
239
240             qreal offset = grid->cellWidth() * rowOffset;
241             if (grid->effectiveLayoutDirection() == Qt::RightToLeft)
242                 x -= offset;
243             else
244                 x += offset;
245             if (grid->verticalLayoutDirection() == QQuickItemView::BottomToTop)
246                 y = -grid->cellHeight() - y;
247         }
248         return QPointF(x, y);
249     }
250
251     // Sets contentY (or contentX in TopToBottom flow) according to given row offset
252     // (in LeftToRight flow) or col offset (in TopToBottom).
253     bool setContentPos(QQuickGridView *gridview, qreal rowOrColOffset) {
254         bool contentPosChanged = (rowOrColOffset != 0);
255         qreal contentOffset = gridview->flow() == QQuickGridView::FlowLeftToRight
256                 ? rowOrColOffset * gridview->cellHeight()
257                 : rowOrColOffset * gridview->cellWidth();
258
259         if (gridview->flow() == QQuickGridView::FlowLeftToRight) {
260             if (gridview->verticalLayoutDirection() == QQuickItemView::BottomToTop)
261                 contentOffset = -gridview->height() - contentOffset;
262         } else {
263             if (gridview->effectiveLayoutDirection() == Qt::RightToLeft)
264                 contentOffset = -gridview->width() - contentOffset;
265         }
266         if (gridview->flow() == QQuickGridView::FlowLeftToRight)
267             gridview->setContentY(contentOffset);
268         else
269             gridview->setContentX(contentOffset);
270         return contentPosChanged;
271     }
272
273 #ifdef SHARE_VIEWS
274     QQuickView *getView() {
275         if (m_view) {
276             if (QString(QTest::currentTestFunction()) != testForView) {
277                 delete m_view;
278                 m_view = 0;
279             } else {
280                 m_view->setSource(QUrl());
281                 return m_view;
282             }
283         }
284
285         testForView = QTest::currentTestFunction();
286         m_view = createView();
287         return m_view;
288     }
289     void releaseView(QQuickView *view) {
290         Q_ASSERT(view == m_view);
291         m_view->setSource(QUrl());
292     }
293 #else
294     QQuickView *getView() {
295         return createView();
296     }
297     void releaseView(QQuickView *view) {
298         delete view;
299     }
300 #endif
301
302     QQuickView *m_view;
303     QString testForView;
304 };
305
306 tst_QQuickGridView::tst_QQuickGridView() : m_view(0)
307 {
308 }
309
310 void tst_QQuickGridView::init()
311 {
312 #ifdef SHARE_VIEWS
313     if (m_view && QString(QTest::currentTestFunction()) != testForView) {
314         testForView = QString();
315         delete m_view;
316         m_view = 0;
317     }
318 #endif
319 }
320
321 void tst_QQuickGridView::items()
322 {
323     QQuickView *canvas = createView();
324
325     QaimModel model;
326     model.addItem("Fred", "12345");
327     model.addItem("John", "2345");
328     model.addItem("Bob", "54321");
329     model.addItem("Billy", "22345");
330     model.addItem("Sam", "2945");
331     model.addItem("Ben", "04321");
332     model.addItem("Jim", "0780");
333
334     QQmlContext *ctxt = canvas->rootContext();
335     ctxt->setContextProperty("testModel", &model);
336
337     canvas->setSource(testFileUrl("gridview1.qml"));
338     qApp->processEvents();
339
340     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
341     QTRY_VERIFY(gridview != 0);
342
343     QQuickItem *contentItem = gridview->contentItem();
344     QTRY_VERIFY(contentItem != 0);
345
346     QTRY_COMPARE(gridview->count(), model.count());
347     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
348     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
349
350     for (int i = 0; i < model.count(); ++i) {
351         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
352         QTRY_VERIFY(name != 0);
353         QTRY_COMPARE(name->text(), model.name(i));
354         QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
355         QTRY_VERIFY(number != 0);
356         QTRY_COMPARE(number->text(), model.number(i));
357     }
358
359     // set an empty model and confirm that items are destroyed
360     QaimModel model2;
361     ctxt->setContextProperty("testModel", &model2);
362
363     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
364     QTRY_VERIFY(itemCount == 0);
365
366     delete canvas;
367 }
368
369 void tst_QQuickGridView::changed()
370 {
371     QQuickView *canvas = createView();
372
373     QaimModel model;
374     model.addItem("Fred", "12345");
375     model.addItem("John", "2345");
376     model.addItem("Bob", "54321");
377     model.addItem("Billy", "22345");
378     model.addItem("Sam", "2945");
379     model.addItem("Ben", "04321");
380     model.addItem("Jim", "0780");
381
382     QQmlContext *ctxt = canvas->rootContext();
383     ctxt->setContextProperty("testModel", &model);
384
385     canvas->setSource(testFileUrl("gridview1.qml"));
386     qApp->processEvents();
387
388     QQuickFlickable *gridview = findItem<QQuickFlickable>(canvas->rootObject(), "grid");
389     QTRY_VERIFY(gridview != 0);
390
391     QQuickItem *contentItem = gridview->contentItem();
392     QTRY_VERIFY(contentItem != 0);
393
394     model.modifyItem(1, "Will", "9876");
395     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
396     QTRY_VERIFY(name != 0);
397     QTRY_COMPARE(name->text(), model.name(1));
398     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
399     QTRY_VERIFY(number != 0);
400     QTRY_COMPARE(number->text(), model.number(1));
401
402     delete canvas;
403 }
404
405 void tst_QQuickGridView::inserted_basic()
406 {
407     QQuickView *canvas = createView();
408     canvas->show();
409
410     QaimModel model;
411     model.addItem("Fred", "12345");
412     model.addItem("John", "2345");
413     model.addItem("Bob", "54321");
414
415     canvas->rootContext()->setContextProperty("testModel", &model);
416     canvas->setSource(testFileUrl("gridview1.qml"));
417     qApp->processEvents();
418
419     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
420     QTRY_VERIFY(gridview != 0);
421
422     QQuickItem *contentItem = gridview->contentItem();
423     QTRY_VERIFY(contentItem != 0);
424
425     model.insertItem(1, "Will", "9876");
426
427     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
428     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
429
430     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
431     QTRY_VERIFY(name != 0);
432     QTRY_COMPARE(name->text(), model.name(1));
433     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
434     QTRY_VERIFY(number != 0);
435     QTRY_COMPARE(number->text(), model.number(1));
436
437     // Checks that onAdd is called
438     int added = canvas->rootObject()->property("added").toInt();
439     QTRY_COMPARE(added, 1);
440
441     // Confirm items positioned correctly
442     for (int i = 0; i < model.count(); ++i) {
443         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
444         QTRY_COMPARE(item->x(), (i%3)*80.0);
445         QTRY_COMPARE(item->y(), (i/3)*60.0);
446     }
447
448     model.insertItem(0, "Foo", "1111"); // zero index, and current item
449
450     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
451
452     name = findItem<QQuickText>(contentItem, "textName", 0);
453     QTRY_VERIFY(name != 0);
454     QTRY_COMPARE(name->text(), model.name(0));
455     number = findItem<QQuickText>(contentItem, "textNumber", 0);
456     QTRY_VERIFY(number != 0);
457     QTRY_COMPARE(number->text(), model.number(0));
458
459     QTRY_COMPARE(gridview->currentIndex(), 1);
460
461     // Confirm items positioned correctly
462     for (int i = 0; i < model.count(); ++i) {
463         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
464         QTRY_VERIFY(item->x() == (i%3)*80);
465         QTRY_VERIFY(item->y() == (i/3)*60);
466     }
467
468     for (int i = model.count(); i < 30; ++i)
469         model.insertItem(i, "Hello", QString::number(i));
470
471     gridview->setContentY(120);
472
473     // Insert item outside visible area
474     model.insertItem(1, "Hello", "1324");
475
476     QTRY_VERIFY(gridview->contentY() == 120);
477
478     delete canvas;
479 }
480
481 void tst_QQuickGridView::inserted_defaultLayout(QQuickGridView::Flow flow,
482                                        Qt::LayoutDirection horizLayout,
483                                        QQuickItemView::VerticalLayoutDirection verticalLayout)
484 {
485     QFETCH(qreal, contentYRowOffset);
486     QFETCH(int, insertIndex);
487     QFETCH(int, insertCount);
488     QFETCH(int, insertIndex_ttb);
489     QFETCH(int, insertCount_ttb);
490     QFETCH(qreal, rowOffsetAfterMove);
491
492     QaimModel model;
493     for (int i = 0; i < 30; i++)
494         model.addItem("Item" + QString::number(i), "");
495
496     QQuickView *canvas = getView();
497     QQmlContext *ctxt = canvas->rootContext();
498     ctxt->setContextProperty("testModel", &model);
499     ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
500     ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
501     ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
502     canvas->setSource(testFileUrl("layouts.qml"));
503     canvas->show();
504     qApp->processEvents();
505
506     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
507     QTRY_VERIFY(gridview != 0);
508     QQuickItem *contentItem = gridview->contentItem();
509     QTRY_VERIFY(contentItem != 0);
510
511     if (flow == QQuickGridView::FlowTopToBottom) {
512         insertIndex = insertIndex_ttb;
513         insertCount = insertCount_ttb;
514     }
515     if (setContentPos(gridview, contentYRowOffset))
516         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
517
518     QList<QPair<QString, QString> > newData;
519     for (int i=0; i<insertCount; i++)
520         newData << qMakePair(QString("value %1").arg(i), QString::number(i));
521     model.insertItems(insertIndex, newData);
522     QTRY_COMPARE(gridview->property("count").toInt(), model.count());
523
524     // check visibleItems.first() is in correct position
525     QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
526     QVERIFY(item0);
527     QPointF firstPos(0, 0);
528     if (horizLayout == Qt::RightToLeft)
529         firstPos.rx() = flow == QQuickGridView::FlowLeftToRight ? gridview->width() - gridview->cellWidth() : -gridview->cellWidth();
530     if (verticalLayout == QQuickItemView::BottomToTop)
531         firstPos.ry() -= gridview->cellHeight();
532     QCOMPARE(item0->pos(), firstPos);
533
534     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
535     int firstVisibleIndex = -1;
536     for (int i=0; i<items.count(); i++) {
537         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
538         if (item && item->isVisible()) {
539             firstVisibleIndex = i;
540             break;
541         }
542     }
543     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
544
545     // Confirm items positioned correctly and indexes correct
546     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
547         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
548         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
549         QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
550         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
551         QVERIFY(name != 0);
552         QCOMPARE(name->text(), model.name(i));
553     }
554
555     releaseView(canvas);
556 }
557
558 void tst_QQuickGridView::inserted_defaultLayout_data()
559 {
560     QTest::addColumn<qreal>("contentYRowOffset");
561     QTest::addColumn<int>("insertIndex");
562     QTest::addColumn<int>("insertCount");
563     QTest::addColumn<int>("insertIndex_ttb");
564     QTest::addColumn<int>("insertCount_ttb");
565     QTest::addColumn<qreal>("rowOffsetAfterMove");
566
567     QTest::newRow("add 1, before visible items")
568             << 2.0     // show 6-23
569             << 5 << 1
570                << 9 << 1
571             << 0.0;   // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
572                       // new 1st visible item is at 0
573
574     QTest::newRow("add 2, before visible items")
575             << 2.0     // show 6-23
576             << 5 << 2
577                << 9 << 2
578             << 0.0;   // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
579
580     QTest::newRow("add 3, before visible items")
581             << 2.0     // show 6-23
582             << 5 << 3
583                << 9 << 5
584             << -1.0;   // insert 3 (1 row) above first visible in negative pos, first visible does not move
585
586     QTest::newRow("add 5, before visible items")
587             << 2.0     // show 6-23
588             << 5 << 5
589                << 9 << 7
590             << -1.0;   // insert 1 row + 2 items above first visible, 1 row added at negative pos,
591                         // grid is rearranged and first visible moves forward within its row
592
593     QTest::newRow("add 6, before visible items")
594             << 2.0     // show 6-23
595             << 5 << 6
596                << 9 << 10
597             << -1.0 * 2;   // insert 2 rows above first visible in negative pos, first visible does not move
598
599
600
601    QTest::newRow("add 1, at start of visible, content at start")
602             << 0.0
603             << 0 << 1
604                << 0 << 1
605             << 0.0;
606
607     QTest::newRow("add multiple, at start of visible, content at start")
608             << 0.0
609             << 0 << 3
610                << 0 << 5
611             << 0.0;
612
613     QTest::newRow("add 1, at start of visible, content not at start")
614             << 2.0     // show 6-23
615             << 6 << 1
616                << 10 << 1
617             << 0.0;
618
619     QTest::newRow("add multiple, at start of visible, content not at start")
620             << 2.0     // show 6-23
621             << 6 << 3
622                << 10 << 5
623             << 0.0;
624
625
626     QTest::newRow("add 1, at end of visible, content at start")
627             << 0.0
628             << 17 << 1
629                << 14 << 1
630             << 0.0;
631
632     QTest::newRow("add row, at end of visible, content at start")
633             << 0.0
634             << 17 << 3
635                << 14 << 5
636             << 0.0;
637
638     QTest::newRow("add 1, at end of visible, content not at start")
639             << 2.0     // show 6-23
640             << 17+6 << 1
641                << 14+10 << 1
642             << 0.0;
643
644     QTest::newRow("add multiple, at end of visible, content not at start")
645             << 2.0     // show 6-23
646             << 17+6 << 3
647                << 14+10 << 5
648             << 0.0;
649
650
651     QTest::newRow("add 1, after visible, content at start")
652             << 0.0
653             << 20 << 1
654                << 18 << 1
655             << 0.0;
656
657     QTest::newRow("add row, after visible, content at start")
658             << 0.0
659             << 20 << 3
660                << 18 << 5
661             << 0.0;
662
663     QTest::newRow("add 1, after visible, content not at start")
664             << 2.0     // show 6-23
665             << 20+6 << 1
666                << 18+10 << 1
667             << 0.0;
668
669     QTest::newRow("add multiple, after visible, content not at start")
670             << 2.0     // show 6-23
671             << 20+6 << 3
672                << 18+10 << 3
673             << 0.0;
674 }
675
676 void tst_QQuickGridView::insertBeforeVisible()
677 {
678     QFETCH(int, insertIndex);
679     QFETCH(int, insertCount);
680     QFETCH(int, cacheBuffer);
681
682     QQuickText *name;
683     QQuickView *canvas = getView();
684
685     QaimModel model;
686     for (int i = 0; i < 30; i++)
687         model.addItem("Item" + QString::number(i), "");
688
689     QQmlContext *ctxt = canvas->rootContext();
690     ctxt->setContextProperty("testModel", &model);
691     canvas->setSource(testFileUrl("gridview1.qml"));
692     canvas->show();
693     qApp->processEvents();
694
695     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
696     QTRY_VERIFY(gridview != 0);
697     QQuickItem *contentItem = gridview->contentItem();
698     QTRY_VERIFY(contentItem != 0);
699
700     gridview->setCacheBuffer(cacheBuffer);
701     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
702
703     // trigger a refill (not just setting contentY) so that the visibleItems grid is updated
704     int firstVisibleIndex = 12;     // move to an index where the top item is not visible
705     gridview->setContentY(firstVisibleIndex/3 * 60.0);
706     gridview->setCurrentIndex(firstVisibleIndex);
707     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
708
709     QTRY_COMPARE(gridview->currentIndex(), firstVisibleIndex);
710     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", firstVisibleIndex);
711     QVERIFY(item);
712     QCOMPARE(item->y(), gridview->contentY());
713
714     QList<QPair<QString, QString> > newData;
715     for (int i=0; i<insertCount; i++)
716         newData << qMakePair(QString("value %1").arg(i), QString::number(i));
717     model.insertItems(insertIndex, newData);
718     QTRY_COMPARE(gridview->property("count").toInt(), model.count());
719
720     // now, moving to the top of the view should position the inserted items correctly
721     int itemsOffsetAfterMove = (insertCount / 3) * -60.0;
722     gridview->setCurrentIndex(0);
723     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
724     QTRY_COMPARE(gridview->currentIndex(), 0);
725     QTRY_COMPARE(gridview->contentY(), 0.0 + itemsOffsetAfterMove);
726
727     // Confirm items positioned correctly and indexes correct
728     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
729     for (int i = 0; i < model.count() && i < itemCount; ++i) {
730         item = findItem<QQuickItem>(contentItem, "wrapper", i);
731         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
732         QCOMPARE(item->x(), (i%3)*80.0);
733         QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
734         name = findItem<QQuickText>(contentItem, "textName", i);
735         QVERIFY(name != 0);
736         QTRY_COMPARE(name->text(), model.name(i));
737     }
738
739     releaseView(canvas);
740 }
741
742 void tst_QQuickGridView::insertBeforeVisible_data()
743 {
744     QTest::addColumn<int>("insertIndex");
745     QTest::addColumn<int>("insertCount");
746     QTest::addColumn<int>("cacheBuffer");
747
748     QTest::newRow("insert 1 at 0, 0 buffer") << 0 << 1 << 0;
749     QTest::newRow("insert 1 at 0, 100 buffer") << 0 << 1 << 100;
750     QTest::newRow("insert 1 at 0, 500 buffer") << 0 << 1 << 500;
751
752     QTest::newRow("insert 1 at 1, 0 buffer") << 1 << 1 << 0;
753     QTest::newRow("insert 1 at 1, 100 buffer") << 1 << 1 << 100;
754     QTest::newRow("insert 1 at 1, 500 buffer") << 1 << 1 << 500;
755
756     QTest::newRow("insert multiple at 0, 0 buffer") << 0 << 6 << 0;
757     QTest::newRow("insert multiple at 0, 100 buffer") << 0 << 6 << 100;
758     QTest::newRow("insert multiple at 0, 500 buffer") << 0 << 6 << 500;
759
760     QTest::newRow("insert multiple at 1, 0 buffer") << 1 << 6 << 0;
761     QTest::newRow("insert multiple at 1, 100 buffer") << 1 << 6 << 100;
762     QTest::newRow("insert multiple at 1, 500 buffer") << 1 << 6 << 500;
763 }
764
765 void tst_QQuickGridView::removed_basic()
766 {
767     QQuickView *canvas = createView();
768     canvas->show();
769
770     QaimModel model;
771     for (int i = 0; i < 40; i++)
772         model.addItem("Item" + QString::number(i), "");
773
774     canvas->rootContext()->setContextProperty("testModel", &model);
775     canvas->setSource(testFileUrl("gridview1.qml"));
776     qApp->processEvents();
777
778     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
779     QTRY_VERIFY(gridview != 0);
780     QQuickItem *contentItem = gridview->contentItem();
781     QTRY_VERIFY(contentItem != 0);
782     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
783
784     model.removeItem(1);
785     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
786
787     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 1);
788     QTRY_VERIFY(name != 0);
789     QTRY_COMPARE(name->text(), model.name(1));
790     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 1);
791     QTRY_VERIFY(number != 0);
792     QTRY_COMPARE(number->text(), model.number(1));
793
794
795     // Checks that onRemove is called
796     QString removed = canvas->rootObject()->property("removed").toString();
797     QTRY_COMPARE(removed, QString("Item1"));
798
799     // Confirm items positioned correctly
800     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
801     for (int i = 0; i < model.count() && i < itemCount; ++i) {
802         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
803         if (!item) qWarning() << "Item" << i << "not found";
804         QTRY_VERIFY(item->x() == (i%3)*80);
805         QTRY_VERIFY(item->y() == (i/3)*60);
806     }
807
808     // Remove first item (which is the current item);
809     model.removeItem(0);
810     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
811
812     name = findItem<QQuickText>(contentItem, "textName", 0);
813     QTRY_VERIFY(name != 0);
814     QTRY_COMPARE(name->text(), model.name(0));
815     number = findItem<QQuickText>(contentItem, "textNumber", 0);
816     QTRY_VERIFY(number != 0);
817     QTRY_COMPARE(number->text(), model.number(0));
818
819
820     // Confirm items positioned correctly
821     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
822     for (int i = 0; i < model.count() && i < itemCount; ++i) {
823         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
824         if (!item) qWarning() << "Item" << i << "not found";
825         QTRY_VERIFY(item->x() == (i%3)*80);
826         QTRY_VERIFY(item->y() == (i/3)*60);
827     }
828
829     // Remove items not visible
830     model.removeItem(25);
831     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
832
833     // Confirm items positioned correctly
834     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
835     for (int i = 0; i < model.count() && i < itemCount; ++i) {
836         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
837         if (!item) qWarning() << "Item" << i << "not found";
838         QTRY_VERIFY(item->x() == (i%3)*80);
839         QTRY_VERIFY(item->y() == (i/3)*60);
840     }
841
842     // Remove items before visible
843     gridview->setContentY(120);
844     gridview->setCurrentIndex(10);
845
846     // Setting currentIndex above shouldn't cause view to scroll
847     QTRY_COMPARE(gridview->contentY(), 120.0);
848
849     model.removeItem(1);
850     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
851
852     // Confirm items positioned correctly
853     for (int i = 6; i < 18; ++i) {
854         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
855         if (!item) qWarning() << "Item" << i << "not found";
856         QTRY_VERIFY(item->x() == (i%3)*80);
857         QTRY_VERIFY(item->y() == (i/3)*60);
858     }
859
860     // Remove currentIndex
861     QQuickItem *oldCurrent = gridview->currentItem();
862     model.removeItem(9);
863     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
864
865     QTRY_COMPARE(gridview->currentIndex(), 9);
866     QTRY_VERIFY(gridview->currentItem() != oldCurrent);
867
868     gridview->setContentY(0);
869     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
870
871     // Confirm items positioned correctly
872     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
873     for (int i = 0; i < model.count() && i < itemCount; ++i) {
874         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
875         QTRY_VERIFY(item->x() == (i%3)*80);
876         QTRY_VERIFY(item->y() == (i/3)*60);
877     }
878
879     // remove item outside current view.
880     gridview->setCurrentIndex(32);
881     gridview->setContentY(240);
882
883     model.removeItem(30);
884     QTRY_VERIFY(gridview->currentIndex() == 31);
885
886     // remove current item beyond visible items.
887     gridview->setCurrentIndex(20);
888     gridview->setContentY(0);
889     model.removeItem(20);
890
891     QTRY_COMPARE(gridview->currentIndex(), 20);
892     QTRY_VERIFY(gridview->currentItem() != 0);
893
894     // remove item before current, but visible
895     gridview->setCurrentIndex(8);
896     gridview->setContentY(240);
897     oldCurrent = gridview->currentItem();
898     model.removeItem(6);
899
900     QTRY_COMPARE(gridview->currentIndex(), 7);
901     QTRY_VERIFY(gridview->currentItem() == oldCurrent);
902
903     delete canvas;
904 }
905
906 void tst_QQuickGridView::removed_defaultLayout(QQuickGridView::Flow flow,
907                                                Qt::LayoutDirection horizLayout,
908                                                QQuickItemView::VerticalLayoutDirection verticalLayout)
909 {
910     QFETCH(qreal, contentYRowOffset);
911     QFETCH(int, removeIndex);
912     QFETCH(int, removeCount);
913     QFETCH(int, removeIndex_ttb);
914     QFETCH(int, removeCount_ttb);
915     QFETCH(qreal, rowOffsetAfterMove);
916     QFETCH(QString, firstVisible);
917     QFETCH(QString, firstVisible_ttb);
918
919     QQuickView *canvas = getView();
920
921     QaimModel model;
922     for (int i = 0; i < 30; i++)
923         model.addItem("Item" + QString::number(i), "");
924
925     QQmlContext *ctxt = canvas->rootContext();
926     ctxt->setContextProperty("testModel", &model);
927     ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
928     ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
929     ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
930     canvas->setSource(testFileUrl("layouts.qml"));
931     canvas->show();
932     qApp->processEvents();
933
934     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
935     QTRY_VERIFY(gridview != 0);
936     QQuickItem *contentItem = gridview->contentItem();
937     QTRY_VERIFY(contentItem != 0);
938
939     if (flow == QQuickGridView::FlowTopToBottom) {
940         removeIndex = removeIndex_ttb;
941         removeCount = removeCount_ttb;
942         firstVisible = firstVisible_ttb;
943     }
944     if (setContentPos(gridview, contentYRowOffset))
945         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
946
947     model.removeItems(removeIndex, removeCount);
948     QTRY_COMPARE(gridview->property("count").toInt(), model.count());
949
950     QString firstName;
951     int firstVisibleIndex = -1;
952     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
953     QRectF viewRect(gridview->contentX(), gridview->contentY(), gridview->width(), gridview->height());
954     for (int i=0; i<items.count(); i++) {
955         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
956         if (item) {
957             QRectF itemRect(item->x(), item->y(), item->width(), item->height());
958             if (item->isVisible() && viewRect.intersects(itemRect)) {
959                 firstVisibleIndex = i;
960                 QQmlExpression en(qmlContext(item), item, "name");
961                 firstName = en.evaluate().toString();
962                 break;
963             }
964         }
965     }
966     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
967     QCOMPARE(firstName, firstVisible);
968
969     // Confirm items positioned correctly and indexes correct
970     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
971         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
972         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
973         QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
974         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
975         QVERIFY(name != 0);
976         QTRY_COMPARE(name->text(), model.name(i));
977     }
978
979     releaseView(canvas);
980 }
981
982 void tst_QQuickGridView::removed_defaultLayout_data()
983 {
984     QTest::addColumn<qreal>("contentYRowOffset");
985     QTest::addColumn<int>("removeIndex");
986     QTest::addColumn<int>("removeCount");
987     QTest::addColumn<int>("removeIndex_ttb");
988     QTest::addColumn<int>("removeCount_ttb");
989     QTest::addColumn<qreal>("rowOffsetAfterMove");
990     QTest::addColumn<QString>("firstVisible");
991     QTest::addColumn<QString>("firstVisible_ttb");
992
993     QTest::newRow("remove 1, before visible items")
994             << 2.0     // show 6-23
995             << 2 << 1
996                << 4 << 1
997             << 0.0 << "Item7" << "Item11";
998
999     QTest::newRow("remove 1, before visible position")
1000             << 2.0     // show 6-23
1001             << 3 << 1
1002                << 5 << 1
1003             << 0.0 << "Item7" << "Item11";
1004
1005     QTest::newRow("remove multiple (1 row), all before visible items")
1006             << 2.0
1007             << 1 << 3
1008                << 1 << 5
1009             << 1.0 << "Item6" << "Item10";    // removed top row, slide down by 1 row
1010
1011     QTest::newRow("remove multiple, all before visible items, remove item 0")
1012             << 2.0
1013             << 0 << 4
1014                << 0 << 6
1015             << 1.0 << "Item7" << "Item11";    // removed top row, slide down by 1 row
1016
1017     QTest::newRow("remove multiple rows, all before visible items")
1018             << 4.0     // show 12-29
1019             << 1 << 7
1020                << 1 << 12
1021             << 2.0 << "Item13" << "Item17";
1022
1023     QTest::newRow("remove one row before visible, content y not on item border")
1024             << 1.5
1025             << 0 << 3
1026                << 0 << 5
1027             << 1.0 << "Item3" << "Item5"; // 1 row removed
1028
1029     QTest::newRow("remove mix of visible/non-visible")
1030             << 2.0     // show 6-23
1031             << 2 << 3
1032                << 4 << 3
1033             << 1.0 << "Item6" << "Item8"; // 1 row removed
1034
1035
1036     // remove 3,4,5 before the visible pos, first row moves down to just before the visible pos,
1037     // items 6,7 are removed from view, item 8 slides up to original pos of item 6 (120px)
1038     QTest::newRow("remove multiple, mix of items from before and within visible items")
1039             << 2.0
1040             << 3 << 5
1041                 << 5 << 7
1042             << 1.0 << "Item8" << "Item12";    // adjust for the 1 row removed before the visible
1043
1044     QTest::newRow("remove multiple, mix of items from before and within visible items, remove item 0")
1045             << 2.0
1046             << 0 << 8
1047                << 0 << 12
1048             << 1.0 * 2 << "Item8" << "Item12";    // adjust for the 2 rows removed before the visible
1049
1050
1051     QTest::newRow("remove 1, from start of visible, content at start")
1052             << 0.0
1053             << 0 << 1
1054                << 0 << 1
1055             << 0.0 << "Item1" << "Item1";
1056
1057     QTest::newRow("remove multiple, from start of visible, content at start")
1058             << 0.0
1059             << 0 << 3
1060                << 0 << 5
1061             << 0.0 << "Item3" << "Item5";
1062
1063     QTest::newRow("remove 1, from start of visible, content not at start")
1064             << 2.0     // show 6-23
1065             << 4 << 1
1066                << 7 << 1
1067             << 0.0 << "Item7" << "Item11";
1068
1069     QTest::newRow("remove multiple, from start of visible, content not at start")
1070             << 2.0     // show 6-23
1071             << 4 << 3
1072                << 7 << 5
1073             << 0.0 << "Item9" << "Item15";
1074
1075
1076     QTest::newRow("remove 1, from middle of visible, content at start")
1077             << 0.0
1078             << 10 << 1
1079                << 12 << 1
1080             << 0.0 << "Item0" << "Item0";
1081
1082     QTest::newRow("remove multiple, from middle of visible, content at start")
1083             << 0.0
1084             << 10 << 5
1085                << 12 << 5
1086             << 0.0 << "Item0" << "Item0";
1087
1088     QTest::newRow("remove 1, from middle of visible, content not at start")
1089             << 2.0     // show 6-23
1090             << 10 << 1
1091                << 12 << 1
1092             << 0.0 << "Item6" << "Item10";
1093
1094     QTest::newRow("remove multiple, from middle of visible, content not at start")
1095             << 2.0     // show 6-23
1096             << 10 << 5
1097                << 12 << 7
1098             << 0.0 << "Item6" << "Item10";
1099
1100
1101     QTest::newRow("remove 1, after visible, content at start")
1102             << 0.0
1103             << 16 << 1
1104                << 15 << 1
1105             << 0.0 << "Item0" << "Item0";
1106
1107     QTest::newRow("remove multiple, after visible, content at start")
1108             << 0.0
1109             << 16 << 5
1110                << 15 << 7
1111             << 0.0 << "Item0" << "Item0";
1112
1113     QTest::newRow("remove 1, after visible, content not at start")
1114             << 2.0     // show 6-23
1115             << 16+4 << 1
1116                << 15+10 << 1
1117             << 0.0 << "Item6" << "Item10";
1118
1119     QTest::newRow("remove multiple, after visible, content not at start")
1120             << 2.0     // show 6-23
1121             << 16+4 << 5
1122                << 15+10 << 7
1123             << 0.0 << "Item6" << "Item10";
1124
1125     QTest::newRow("remove multiple, mix of items from within and after visible items")
1126             << 2.0     // show 6-23
1127             << 20 << 5
1128                << 22 << 7
1129             << 0.0 << "Item6" << "Item10";
1130 }
1131
1132 void tst_QQuickGridView::addOrRemoveBeforeVisible()
1133 {
1134     // QTBUG-21588: ensure re-layout is done on grid after adding or removing
1135     // items from before the visible area
1136
1137     QFETCH(bool, doAdd);
1138     QFETCH(qreal, newTopContentY);
1139
1140     QQuickView *canvas = getView();
1141     canvas->show();
1142
1143     QaimModel model;
1144     for (int i = 0; i < 30; i++)
1145         model.addItem("Item" + QString::number(i), "");
1146
1147     canvas->rootContext()->setContextProperty("testModel", &model);
1148     canvas->setSource(testFileUrl("gridview1.qml"));
1149
1150     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1151     QTRY_VERIFY(gridview != 0);
1152     QQuickItem *contentItem = gridview->contentItem();
1153     QTRY_VERIFY(contentItem != 0);
1154
1155     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
1156     QTRY_COMPARE(name->text(), QString("Item0"));
1157
1158     gridview->setCurrentIndex(0);
1159     qApp->processEvents();
1160     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1161
1162     // scroll down until item 0 is no longer drawn
1163     // (bug not triggered if we just move using content y, since that doesn't
1164     // refill and change the visible items)
1165     gridview->setCurrentIndex(24);
1166     qApp->processEvents();
1167
1168     QTRY_COMPARE(gridview->currentIndex(), 24);
1169     QTRY_COMPARE(gridview->contentY(), 220.0);
1170
1171     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1172     QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 0));  // 0 shouldn't be visible
1173
1174     if (doAdd) {
1175         model.insertItem(0, "New Item", "New Item number");
1176         QTRY_COMPARE(gridview->count(), 31);
1177     } else {
1178         model.removeItem(0);
1179         QTRY_COMPARE(gridview->count(), 29);
1180     }
1181
1182     // scroll back up and item 0 should be gone
1183     gridview->setCurrentIndex(0);
1184     qApp->processEvents();
1185     QTRY_COMPARE(gridview->currentIndex(), 0);
1186     QTRY_COMPARE(gridview->contentY(), newTopContentY);
1187
1188     name = findItem<QQuickText>(contentItem, "textName", 0);
1189     if (doAdd)
1190         QCOMPARE(name->text(), QString("New Item"));
1191     else
1192         QCOMPARE(name->text(), QString("Item1"));
1193
1194     // Confirm items positioned correctly
1195     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1196     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1197         QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", i));
1198         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1199         QTRY_VERIFY(item->x() == (i%3)*80);
1200         QTRY_VERIFY(item->y() == (i/3)*60 + newTopContentY);
1201     }
1202
1203     releaseView(canvas);
1204 }
1205
1206 void tst_QQuickGridView::addOrRemoveBeforeVisible_data()
1207 {
1208     QTest::addColumn<bool>("doAdd");
1209     QTest::addColumn<qreal>("newTopContentY");
1210
1211     QTest::newRow("add") << true << -60.0;
1212     QTest::newRow("remove") << false << -60.0;
1213 }
1214
1215 void tst_QQuickGridView::clear()
1216 {
1217     QQuickView *canvas = createView();
1218
1219     QaimModel model;
1220     for (int i = 0; i < 30; i++)
1221         model.addItem("Item" + QString::number(i), "");
1222
1223     canvas->rootContext()->setContextProperty("testModel", &model);
1224     canvas->setSource(testFileUrl("gridview1.qml"));
1225     canvas->show();
1226     qApp->processEvents();
1227
1228     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1229     QVERIFY(gridview != 0);
1230     QQuickItem *contentItem = gridview->contentItem();
1231     QVERIFY(contentItem != 0);
1232     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1233
1234     model.clear();
1235
1236     QVERIFY(gridview->count() == 0);
1237     QVERIFY(gridview->currentItem() == 0);
1238     QVERIFY(gridview->contentY() == 0);
1239     QVERIFY(gridview->currentIndex() == -1);
1240
1241     // confirm sanity when adding an item to cleared list
1242     model.addItem("New", "1");
1243     QTRY_COMPARE(gridview->count(), 1);
1244     QVERIFY(gridview->currentItem() != 0);
1245     QVERIFY(gridview->currentIndex() == 0);
1246
1247     delete canvas;
1248 }
1249
1250 void tst_QQuickGridView::moved_defaultLayout(QQuickGridView::Flow flow,
1251                                              Qt::LayoutDirection horizLayout,
1252                                              QQuickItemView::VerticalLayoutDirection verticalLayout)
1253 {
1254     QFETCH(qreal, contentYRowOffset);
1255     QFETCH(int, from);
1256     QFETCH(int, to);
1257     QFETCH(int, count);
1258     QFETCH(int, from_ttb);
1259     QFETCH(int, to_ttb);
1260     QFETCH(int, count_ttb);
1261     QFETCH(qreal, rowOffsetAfterMove);
1262
1263     QQuickView *canvas = getView();
1264
1265     QaimModel model;
1266     for (int i = 0; i < 30; i++)
1267         model.addItem("Item" + QString::number(i), "");
1268
1269     QQmlContext *ctxt = canvas->rootContext();
1270     ctxt->setContextProperty("testModel", &model);
1271     ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
1272     ctxt->setContextProperty("testRightToLeft", horizLayout == Qt::RightToLeft);
1273     ctxt->setContextProperty("testBottomToTop", verticalLayout == QQuickGridView::BottomToTop);
1274     canvas->setSource(testFileUrl("layouts.qml"));
1275     canvas->show();
1276     qApp->processEvents();
1277
1278     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1279     QTRY_VERIFY(gridview != 0);
1280     QQuickItem *contentItem = gridview->contentItem();
1281     QTRY_VERIFY(contentItem != 0);
1282     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1283
1284     QQuickItem *currentItem = gridview->currentItem();
1285     QTRY_VERIFY(currentItem != 0);
1286
1287     if (flow == QQuickGridView::FlowTopToBottom) {
1288         from = from_ttb;
1289         to = to_ttb;
1290         count = count_ttb;
1291     }
1292     if (setContentPos(gridview, contentYRowOffset))
1293         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1294
1295     model.moveItems(from, to, count);
1296     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1297
1298     // Confirm items positioned correctly and indexes correct
1299     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
1300     int firstVisibleIndex = -1;
1301     for (int i=0; i<items.count(); i++) {
1302         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1303         if (item && item->isVisible()) {
1304             firstVisibleIndex = i;
1305             break;
1306         }
1307     }
1308     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
1309
1310     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
1311         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1312         if (!item &&
1313                 ( (flow == QQuickGridView::FlowLeftToRight && i >= firstVisibleIndex + (3*6))
1314                 || flow == QQuickGridView::FlowTopToBottom && i >= firstVisibleIndex + (5*3) ) ) {
1315             continue;   // index has moved out of view
1316         }
1317         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1318         QCOMPARE(item->pos(), expectedItemPos(gridview, i, rowOffsetAfterMove));
1319         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
1320         QVERIFY(name != 0);
1321         QTRY_COMPARE(name->text(), model.name(i));
1322
1323         // current index should have been updated
1324         if (item == currentItem)
1325             QTRY_COMPARE(gridview->currentIndex(), i);
1326     }
1327
1328     releaseView(canvas);
1329 }
1330
1331 void tst_QQuickGridView::moved_defaultLayout_data()
1332 {
1333     QTest::addColumn<qreal>("contentYRowOffset");
1334     QTest::addColumn<int>("from");
1335     QTest::addColumn<int>("to");
1336     QTest::addColumn<int>("count");
1337     QTest::addColumn<int>("from_ttb");
1338     QTest::addColumn<int>("to_ttb");
1339     QTest::addColumn<int>("count_ttb");
1340     QTest::addColumn<qreal>("rowOffsetAfterMove");
1341
1342     // model starts with 30 items, each 80x60, in area 240x320
1343     // 18 items should be visible at a time
1344
1345     // The first visible item should not move upwards and out of the view
1346     // if items are moved/removed before it.
1347
1348
1349     QTest::newRow("move 1 forwards, within visible items")
1350             << 0.0
1351             << 1 << 8 << 1
1352                << 2 << 12 << 1
1353             << 0.0;
1354
1355     QTest::newRow("move 1 forwards, from non-visible -> visible")
1356             << 2.0     // show 6-23
1357             << 1 << 8+6 << 1
1358                << 2 << 12+10 << 1
1359             << 0.0;
1360
1361     QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
1362             << 2.0     // // show 6-23
1363             << 0 << 6 << 1
1364                << 0 << 10 << 1
1365             << 0.0;
1366
1367     QTest::newRow("move 1 forwards, from visible -> non-visible")
1368             << 0.0
1369             << 1 << 20 << 1
1370                << 1 << 20 << 1
1371             << 0.0;
1372
1373     QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
1374             << 0.0
1375             << 0 << 20 << 1
1376                << 0 << 20 << 1
1377             << 0.0;
1378
1379
1380     QTest::newRow("move 1 backwards, within visible items")
1381             << 0.0
1382             << 10 << 5 << 1
1383                << 10 << 5 << 1
1384             << 0.0;
1385
1386     QTest::newRow("move 1 backwards, within visible items (to first index)")
1387             << 0.0
1388             << 10 << 0 << 1
1389                << 10 << 0 << 1
1390             << 0.0;
1391
1392     QTest::newRow("move 1 backwards, from non-visible -> visible")
1393             << 0.0
1394             << 28 << 8 << 1
1395                << 28 << 8 << 1
1396             << 0.0;
1397
1398     QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
1399             << 0.0
1400             << 29 << 14 << 1
1401                << 29 << 14 << 1
1402             << 0.0;
1403
1404     QTest::newRow("move 1 backwards, from visible -> non-visible")
1405             << 2.0     // show 6-23
1406             << 7 << 1 << 1
1407                << 10 << 1 << 1
1408             << 0.0;     // only 1 item moved back, so items shift accordingly and first row doesn't move
1409
1410     QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
1411             << 2.0     // show 6-23
1412             << 7 << 0 << 1
1413                << 10 << 0 << 1
1414             << 0.0;     // only 1 item moved back, so items shift accordingly and first row doesn't move
1415
1416
1417     QTest::newRow("move multiple forwards, within visible items")
1418             << 0.0
1419             << 0 << 5 << 3
1420                << 0 << 7 << 5
1421             << 0.0;
1422
1423     QTest::newRow("move multiple backwards, within visible items (move first item)")
1424             << 0.0
1425             << 10 << 0 << 3
1426                << 12 << 0 << 5
1427             << 0.0;
1428
1429     QTest::newRow("move multiple forwards, before visible items")
1430             << 2.0     // show 6-23
1431             << 3 << 4 << 3      // 3, 4, 5 move to after 6
1432                << 5 << 6 << 5
1433             << 1.0;      // row of 3,4,5 has moved down
1434
1435     QTest::newRow("move multiple forwards, from non-visible -> visible")
1436             << 2.0     // show 6-23
1437             << 1 << 6 << 3
1438                << 1 << 10 << 5
1439             << 1.0; // 1st row (it's above visible area) disappears, 0 drops down 1 row, first visible item (6) stays where it is
1440
1441     QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
1442             << 2.0     // show 6-23
1443             << 0 << 6 << 3
1444                << 0 << 10 << 5
1445             << 1.0;    // top row moved and shifted to below 3rd row, all items should shift down by 1 row
1446
1447     QTest::newRow("move multiple forwards, mix of non-visible/visible")
1448             << 2.0
1449             << 3 << 16 << 6
1450                << 5 << 18 << 10
1451             << 1.0;    // top two rows removed, third row is now the first visible
1452
1453     QTest::newRow("move multiple forwards, to bottom of view")
1454             << 0.0
1455             << 5 << 13 << 5
1456                << 1 << 8 << 6
1457             << 0.0;
1458
1459     QTest::newRow("move multiple forwards, to bottom of view, first row -> last")
1460             << 0.0
1461             << 0 << 15 << 3
1462                << 0 << 10 << 5
1463             << 0.0;
1464
1465     QTest::newRow("move multiple forwards, to bottom of view, content y not 0")
1466             << 2.0
1467             << 5+4 << 13+4 << 5
1468                << 11 << 19 << 6
1469             << 0.0;
1470
1471     QTest::newRow("move multiple forwards, from visible -> non-visible")
1472             << 0.0
1473             << 1 << 16 << 3
1474                << 1 << 18 << 5
1475             << 0.0;
1476
1477     QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
1478             << 0.0
1479             << 0 << 16 << 3
1480                << 1 << 18 << 5
1481             << 0.0;
1482
1483
1484     QTest::newRow("move multiple backwards, within visible items")
1485             << 0.0
1486             << 4 << 1 << 3
1487                << 7 << 1 << 5
1488             << 0.0;
1489
1490     QTest::newRow("move multiple backwards, from non-visible -> visible")
1491             << 0.0
1492             << 20 << 4 << 3
1493                << 20 << 4 << 5
1494             << 0.0;
1495
1496     QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
1497             << 0.0
1498             << 27 << 10 << 3
1499                << 25 << 8 << 5
1500             << 0.0;
1501
1502     QTest::newRow("move multiple backwards, from visible -> non-visible")
1503             << 2.0     // show 6-23
1504             << 16 << 1 << 3
1505                << 17 << 1 << 5
1506             << -1.0;   // to minimize movement, items are added above visible area, all items move up by 1 row
1507
1508     QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
1509             << 2.0     // show 6-23
1510             << 16 << 0 << 3
1511                << 17 << 0 << 5
1512             << -1.0;   // 16,17,18 move to above item 0, all items move up by 1 row
1513 }
1514
1515 void tst_QQuickGridView::multipleChanges(bool condensed)
1516 {
1517     QFETCH(int, startCount);
1518     QFETCH(QList<ListChange>, changes);
1519     QFETCH(int, newCount);
1520     QFETCH(int, newCurrentIndex);
1521
1522     QQuickView *canvas = getView();
1523
1524     QaimModel model;
1525     for (int i = 0; i < startCount; i++)
1526         model.addItem("Item" + QString::number(i), "");
1527
1528     canvas->rootContext()->setContextProperty("testModel", &model);
1529     canvas->setSource(testFileUrl("gridview1.qml"));
1530     canvas->show();
1531     qApp->processEvents();
1532
1533     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1534     QTRY_VERIFY(gridview != 0);
1535     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1536
1537     for (int i=0; i<changes.count(); i++) {
1538         switch (changes[i].type) {
1539             case ListChange::Inserted:
1540             {
1541                 QList<QPair<QString, QString> > items;
1542                 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
1543                     items << qMakePair(QString("new item " + j), QString::number(j));
1544                 model.insertItems(changes[i].index, items);
1545                 break;
1546             }
1547             case ListChange::Removed:
1548                 model.removeItems(changes[i].index, changes[i].count);
1549                 break;
1550             case ListChange::Moved:
1551                 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
1552                 break;
1553             case ListChange::SetCurrent:
1554                 gridview->setCurrentIndex(changes[i].index);
1555                 break;
1556             case ListChange::SetContentY:
1557                 gridview->setContentY(changes[i].pos);
1558                 break;
1559         }
1560         if (condensed) {
1561             QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1562         }
1563     }
1564     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1565
1566     QCOMPARE(gridview->count(), newCount);
1567     QCOMPARE(gridview->count(), model.count());
1568     QCOMPARE(gridview->currentIndex(), newCurrentIndex);
1569
1570     QQuickText *name;
1571     QQuickText *number;
1572     QQuickItem *contentItem = gridview->contentItem();
1573     QTRY_VERIFY(contentItem != 0);
1574     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
1575     for (int i=0; i < model.count() && i < itemCount; ++i) {
1576         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
1577         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
1578         name = findItem<QQuickText>(contentItem, "textName", i);
1579         QVERIFY(name != 0);
1580         QTRY_COMPARE(name->text(), model.name(i));
1581         number = findItem<QQuickText>(contentItem, "textNumber", i);
1582         QVERIFY(number != 0);
1583         QTRY_COMPARE(number->text(), model.number(i));
1584     }
1585
1586     releaseView(canvas);
1587 }
1588
1589 void tst_QQuickGridView::multipleChanges_data()
1590 {
1591     QTest::addColumn<int>("startCount");
1592     QTest::addColumn<QList<ListChange> >("changes");
1593     QTest::addColumn<int>("newCount");
1594     QTest::addColumn<int>("newCurrentIndex");
1595
1596     QList<ListChange> changes;
1597
1598     for (int i=1; i<30; i++)
1599         changes << ListChange::remove(0);
1600     QTest::newRow("remove all but 1, first->last") << 30 << changes << 1 << 0;
1601
1602     changes << ListChange::remove(0);
1603     QTest::newRow("remove all") << 30 << changes << 0 << -1;
1604
1605     changes.clear();
1606     changes << ListChange::setCurrent(29);
1607     for (int i=29; i>0; i--)
1608         changes << ListChange::remove(i);
1609     QTest::newRow("remove last (current) -> first") << 30 << changes << 1 << 0;
1610
1611     QTest::newRow("remove then insert at 0") << 10 << (QList<ListChange>()
1612             << ListChange::remove(0, 1)
1613             << ListChange::insert(0, 1)
1614             ) << 10 << 1;
1615
1616     QTest::newRow("remove then insert at non-zero index") << 10 << (QList<ListChange>()
1617             << ListChange::setCurrent(2)
1618             << ListChange::remove(2, 1)
1619             << ListChange::insert(2, 1)
1620             ) << 10 << 3;
1621
1622     QTest::newRow("remove current then insert below it") << 10 << (QList<ListChange>()
1623             << ListChange::setCurrent(1)
1624             << ListChange::remove(1, 3)
1625             << ListChange::insert(2, 2)
1626             ) << 9 << 1;
1627
1628     QTest::newRow("remove current index then move it down") << 10 << (QList<ListChange>()
1629             << ListChange::setCurrent(2)
1630             << ListChange::remove(1, 3)
1631             << ListChange::move(1, 5, 1)
1632             ) << 7 << 5;
1633
1634     QTest::newRow("remove current index then move it up") << 10 << (QList<ListChange>()
1635             << ListChange::setCurrent(5)
1636             << ListChange::remove(4, 3)
1637             << ListChange::move(4, 1, 1)
1638             ) << 7 << 1;
1639
1640
1641     QTest::newRow("insert multiple times") << 0 << (QList<ListChange>()
1642             << ListChange::insert(0, 2)
1643             << ListChange::insert(0, 4)
1644             << ListChange::insert(0, 6)
1645             ) << 12 << 10;
1646
1647     QTest::newRow("insert multiple times with current index changes") << 0 << (QList<ListChange>()
1648             << ListChange::insert(0, 2)
1649             << ListChange::insert(0, 4)
1650             << ListChange::insert(0, 6)
1651             << ListChange::setCurrent(3)
1652             << ListChange::insert(3, 2)
1653             ) << 14 << 5;
1654
1655     QTest::newRow("insert and remove all") << 0 << (QList<ListChange>()
1656             << ListChange::insert(0, 30)
1657             << ListChange::remove(0, 30)
1658             ) << 0 << -1;
1659
1660     QTest::newRow("insert and remove current") << 30 << (QList<ListChange>()
1661             << ListChange::insert(1)
1662             << ListChange::setCurrent(1)
1663             << ListChange::remove(1)
1664             ) << 30 << 1;
1665
1666     QTest::newRow("insert before 0, then remove cross section of new and old items") << 10 << (QList<ListChange>()
1667             << ListChange::insert(0, 10)
1668             << ListChange::remove(5, 10)
1669             ) << 10 << 5;
1670
1671     QTest::newRow("insert multiple, then move new items to end") << 10 << (QList<ListChange>()
1672             << ListChange::insert(0, 3)
1673             << ListChange::move(0, 10, 3)
1674             ) << 13 << 0;
1675
1676     QTest::newRow("insert multiple, then move new and some old items to end") << 10 << (QList<ListChange>()
1677             << ListChange::insert(0, 3)
1678             << ListChange::move(0, 8, 5)
1679             ) << 13 << 11;
1680
1681     QTest::newRow("insert multiple at end, then move new and some old items to start") << 10 << (QList<ListChange>()
1682             << ListChange::setCurrent(9)
1683             << ListChange::insert(10, 3)
1684             << ListChange::move(8, 0, 5)
1685             ) << 13 << 1;
1686
1687
1688     QTest::newRow("move back and forth to same index") << 10 << (QList<ListChange>()
1689             << ListChange::setCurrent(1)
1690             << ListChange::move(1, 2, 2)
1691             << ListChange::move(2, 1, 2)
1692             ) << 10 << 1;
1693
1694     QTest::newRow("move forwards then back") << 10 << (QList<ListChange>()
1695             << ListChange::setCurrent(2)
1696             << ListChange::move(1, 2, 3)
1697             << ListChange::move(3, 0, 5)
1698             ) << 10 << 0;
1699
1700     QTest::newRow("move current, then remove it") << 10 << (QList<ListChange>()
1701             << ListChange::setCurrent(5)
1702             << ListChange::move(5, 0, 1)
1703             << ListChange::remove(0)
1704             ) << 9 << 0;
1705
1706     QTest::newRow("move current, then insert before it") << 10 << (QList<ListChange>()
1707             << ListChange::setCurrent(5)
1708             << ListChange::move(5, 0, 1)
1709             << ListChange::insert(0)
1710             ) << 11 << 1;
1711
1712     QTest::newRow("move multiple, then remove them") << 10 << (QList<ListChange>()
1713             << ListChange::setCurrent(1)
1714             << ListChange::move(5, 1, 3)
1715             << ListChange::remove(1, 3)
1716             ) << 7 << 1;
1717
1718     QTest::newRow("move multiple, then insert before them") << 10 << (QList<ListChange>()
1719             << ListChange::setCurrent(5)
1720             << ListChange::move(5, 1, 3)
1721             << ListChange::insert(1, 5)
1722             ) << 15 << 6;
1723
1724     QTest::newRow("move multiple, then insert after them") << 10 << (QList<ListChange>()
1725             << ListChange::setCurrent(3)
1726             << ListChange::move(0, 1, 2)
1727             << ListChange::insert(3, 5)
1728             ) << 15 << 8;
1729
1730
1731     QTest::newRow("clear current") << 0 << (QList<ListChange>()
1732             << ListChange::insert(0, 5)
1733             << ListChange::setCurrent(-1)
1734             << ListChange::remove(0, 5)
1735             << ListChange::insert(0, 5)
1736             ) << 5 << -1;
1737
1738     QTest::newRow("remove, scroll") << 30 << (QList<ListChange>()
1739             << ListChange::remove(20, 5)
1740             << ListChange::setContentY(20)
1741             ) << 25 << 0;
1742
1743     QTest::newRow("insert, scroll") << 10 << (QList<ListChange>()
1744             << ListChange::insert(9, 5)
1745             << ListChange::setContentY(20)
1746             ) << 15 << 0;
1747
1748     QTest::newRow("move, scroll") << 20 << (QList<ListChange>()
1749             << ListChange::move(15, 8, 3)
1750             << ListChange::setContentY(0)
1751             ) << 20 << 0;
1752
1753     QTest::newRow("clear, insert, scroll") << 30 << (QList<ListChange>()
1754             << ListChange::setContentY(20)
1755             << ListChange::remove(0, 30)
1756             << ListChange::insert(0, 2)
1757             << ListChange::setContentY(0)
1758             ) << 2 << 0;
1759 }
1760
1761
1762 void tst_QQuickGridView::swapWithFirstItem()
1763 {
1764     // QTBUG_9697
1765     QQuickView *canvas = createView();
1766
1767     QaimModel model;
1768     for (int i = 0; i < 30; i++)
1769         model.addItem("Item" + QString::number(i), "");
1770
1771     canvas->rootContext()->setContextProperty("testModel", &model);
1772     canvas->setSource(testFileUrl("gridview1.qml"));
1773     canvas->show();
1774     qApp->processEvents();
1775
1776     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1777     QTRY_VERIFY(gridview != 0);
1778
1779     // ensure content position is stable
1780     gridview->setContentY(0);
1781     model.moveItem(10, 0);
1782     QTRY_VERIFY(gridview->contentY() == 0);
1783
1784     delete canvas;
1785 }
1786
1787 void tst_QQuickGridView::currentIndex()
1788 {
1789     QaimModel model;
1790     for (int i = 0; i < 60; i++)
1791         model.addItem("Item" + QString::number(i), QString::number(i));
1792
1793     QQuickView *canvas = new QQuickView(0);
1794     canvas->setGeometry(0,0,240,320);
1795     canvas->show();
1796
1797     QQmlContext *ctxt = canvas->rootContext();
1798     ctxt->setContextProperty("testModel", &model);
1799
1800     QString filename(testFile("gridview-initCurrent.qml"));
1801     canvas->setSource(QUrl::fromLocalFile(filename));
1802
1803     qApp->processEvents();
1804
1805     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1806     QVERIFY(gridview != 0);
1807     QTRY_VERIFY(!QQuickItemPrivate::get(gridview)->polishScheduled);
1808
1809     QQuickItem *contentItem = gridview->contentItem();
1810     QVERIFY(contentItem != 0);
1811
1812     // current item should be third item
1813     QCOMPARE(gridview->currentIndex(), 35);
1814     QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
1815     QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
1816     QCOMPARE(gridview->contentY(), 400.0);
1817
1818     gridview->setCurrentIndex(0);
1819     QCOMPARE(gridview->currentIndex(), 0);
1820     // confirm that the velocity is updated
1821     QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
1822
1823     // footer should become visible if it is out of view, and then current index moves to the first row
1824     canvas->rootObject()->setProperty("showFooter", true);
1825     QTRY_VERIFY(gridview->footerItem());
1826     gridview->setCurrentIndex(model.count()-3);
1827     QTRY_VERIFY(gridview->footerItem()->y() > gridview->contentY() + gridview->height());
1828     gridview->setCurrentIndex(model.count()-2);
1829     QTRY_COMPARE(gridview->contentY() + gridview->height(), (60.0 * model.count()/3) + gridview->footerItem()->height());
1830     canvas->rootObject()->setProperty("showFooter", false);
1831
1832     // header should become visible if it is out of view, and then current index moves to the last row
1833     canvas->rootObject()->setProperty("showHeader", true);
1834     QTRY_VERIFY(gridview->headerItem());
1835     gridview->setCurrentIndex(3);
1836     QTRY_VERIFY(gridview->headerItem()->y() + gridview->headerItem()->height() < gridview->contentY());
1837     gridview->setCurrentIndex(1);
1838     QTRY_COMPARE(gridview->contentY(), -gridview->headerItem()->height());
1839     canvas->rootObject()->setProperty("showHeader", false);
1840
1841     // turn off auto highlight
1842     gridview->setHighlightFollowsCurrentItem(false);
1843     QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1844     QVERIFY(gridview->highlightItem());
1845     qreal hlPosX = gridview->highlightItem()->x();
1846     qreal hlPosY = gridview->highlightItem()->y();
1847
1848     gridview->setCurrentIndex(5);
1849     QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1850     QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1851
1852     // insert item before currentIndex
1853     gridview->setCurrentIndex(28);
1854     model.insertItem(0, "Foo", "1111");
1855     QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1856
1857     // check removing highlight by setting currentIndex to -1;
1858     gridview->setCurrentIndex(-1);
1859
1860     QCOMPARE(gridview->currentIndex(), -1);
1861     QVERIFY(!gridview->highlightItem());
1862     QVERIFY(!gridview->currentItem());
1863
1864     // moving currentItem out of view should make it invisible
1865     gridview->setCurrentIndex(0);
1866     QTRY_VERIFY(gridview->currentItem()->isVisible());
1867     gridview->setContentY(200);
1868     QTRY_VERIFY(!gridview->currentItem()->isVisible());
1869
1870     delete canvas;
1871 }
1872
1873 void tst_QQuickGridView::noCurrentIndex()
1874 {
1875     QaimModel model;
1876     for (int i = 0; i < 60; i++)
1877         model.addItem("Item" + QString::number(i), QString::number(i));
1878
1879     QQuickView *canvas = new QQuickView(0);
1880     canvas->setGeometry(0,0,240,320);
1881
1882     QQmlContext *ctxt = canvas->rootContext();
1883     ctxt->setContextProperty("testModel", &model);
1884
1885     QString filename(testFile("gridview-noCurrent.qml"));
1886     canvas->setSource(QUrl::fromLocalFile(filename));
1887     canvas->show();
1888     qApp->processEvents();
1889
1890     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1891     QVERIFY(gridview != 0);
1892     QQuickItem *contentItem = gridview->contentItem();
1893     QVERIFY(contentItem != 0);
1894     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1895
1896     // current index should be -1
1897     QCOMPARE(gridview->currentIndex(), -1);
1898     QVERIFY(!gridview->currentItem());
1899     QVERIFY(!gridview->highlightItem());
1900     QCOMPARE(gridview->contentY(), 0.0);
1901
1902     gridview->setCurrentIndex(5);
1903     QCOMPARE(gridview->currentIndex(), 5);
1904     QVERIFY(gridview->currentItem());
1905     QVERIFY(gridview->highlightItem());
1906
1907     delete canvas;
1908 }
1909
1910 void tst_QQuickGridView::keyNavigation()
1911 {
1912     QFETCH(QQuickGridView::Flow, flow);
1913     QFETCH(Qt::LayoutDirection, layoutDirection);
1914     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
1915     QFETCH(Qt::Key, forwardsKey);
1916     QFETCH(Qt::Key, backwardsKey);
1917     QFETCH(QPointF, contentPosAtFirstItem);
1918     QFETCH(QPointF, contentPosAtLastItem);
1919     QFETCH(int, indexRightOf7);
1920     QFETCH(int, indexLeftOf7);
1921     QFETCH(int, indexUpFrom7);
1922     QFETCH(int, indexDownFrom7);
1923
1924     QmlListModel model;
1925     for (int i = 0; i < 18; i++)
1926         model.addItem("Item" + QString::number(i), "");
1927
1928     QQuickView *canvas = getView();
1929     canvas->rootContext()->setContextProperty("testModel", &model);
1930     canvas->setSource(testFileUrl("gridview1.qml"));
1931     canvas->show();
1932     qApp->processEvents();
1933
1934     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
1935     QTRY_VERIFY(gridview != 0);
1936     gridview->setFlow(flow);
1937     gridview->setLayoutDirection(layoutDirection);
1938     gridview->setVerticalLayoutDirection(verticalLayoutDirection);
1939     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
1940
1941     canvas->requestActivateWindow();
1942     QTRY_VERIFY(qGuiApp->focusWindow() == canvas);
1943     QCOMPARE(gridview->currentIndex(), 0);
1944
1945     QTest::keyClick(canvas, forwardsKey);
1946     QCOMPARE(gridview->currentIndex(), 1);
1947
1948     QTest::keyClick(canvas, backwardsKey);
1949     QCOMPARE(gridview->currentIndex(), 0);
1950
1951     gridview->setCurrentIndex(7);
1952     gridview->moveCurrentIndexRight();
1953     QCOMPARE(gridview->currentIndex(), indexRightOf7);
1954     gridview->moveCurrentIndexLeft();
1955     QCOMPARE(gridview->currentIndex(), 7);
1956     gridview->moveCurrentIndexLeft();
1957     QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1958     gridview->moveCurrentIndexRight();
1959     QCOMPARE(gridview->currentIndex(), 7);
1960     gridview->moveCurrentIndexUp();
1961     QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1962     gridview->moveCurrentIndexDown();
1963     QCOMPARE(gridview->currentIndex(), 7);
1964     gridview->moveCurrentIndexDown();
1965     QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1966
1967     gridview->setCurrentIndex(7);
1968     QTest::keyClick(canvas, Qt::Key_Right);
1969     QCOMPARE(gridview->currentIndex(), indexRightOf7);
1970     QTest::keyClick(canvas, Qt::Key_Left);
1971     QCOMPARE(gridview->currentIndex(), 7);
1972     QTest::keyClick(canvas, Qt::Key_Left);
1973     QCOMPARE(gridview->currentIndex(), indexLeftOf7);
1974     QTest::keyClick(canvas, Qt::Key_Right);
1975     QCOMPARE(gridview->currentIndex(), 7);
1976     QTest::keyClick(canvas, Qt::Key_Up);
1977     QCOMPARE(gridview->currentIndex(), indexUpFrom7);
1978     QTest::keyClick(canvas, Qt::Key_Down);
1979     QCOMPARE(gridview->currentIndex(), 7);
1980     QTest::keyClick(canvas, Qt::Key_Down);
1981     QCOMPARE(gridview->currentIndex(), indexDownFrom7);
1982
1983     // hold down a key to go forwards
1984     gridview->setCurrentIndex(0);
1985     for (int i=0; i<model.count()-1; i++) {
1986 //        QTest::qWait(500);
1987         QTest::simulateEvent(canvas, true, forwardsKey, Qt::NoModifier, "", true);
1988         QTRY_COMPARE(gridview->currentIndex(), i+1);
1989     }
1990     QTest::keyRelease(canvas, forwardsKey);
1991     QTRY_COMPARE(gridview->currentIndex(), model.count()-1);
1992     QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
1993     QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
1994
1995     // hold down a key to go backwards
1996     for (int i=model.count()-1; i > 0; i--) {
1997         QTest::simulateEvent(canvas, true, backwardsKey, Qt::NoModifier, "", true);
1998         QTRY_COMPARE(gridview->currentIndex(), i-1);
1999     }
2000     QTest::keyRelease(canvas, backwardsKey);
2001     QTRY_COMPARE(gridview->currentIndex(), 0);
2002     QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2003     QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2004
2005     // no wrap
2006     QVERIFY(!gridview->isWrapEnabled());
2007     QTest::keyClick(canvas, forwardsKey);
2008     QCOMPARE(gridview->currentIndex(), 1);
2009     QTest::keyClick(canvas, backwardsKey);
2010     QCOMPARE(gridview->currentIndex(), 0);
2011
2012     QTest::keyClick(canvas, backwardsKey);
2013     QCOMPARE(gridview->currentIndex(), 0);
2014
2015     // with wrap
2016     gridview->setWrapEnabled(true);
2017     QVERIFY(gridview->isWrapEnabled());
2018
2019     QTest::keyClick(canvas, backwardsKey);
2020     QCOMPARE(gridview->currentIndex(), model.count()-1);
2021     QTRY_COMPARE(gridview->contentX(), contentPosAtLastItem.x());
2022     QTRY_COMPARE(gridview->contentY(), contentPosAtLastItem.y());
2023
2024     QTest::keyClick(canvas, forwardsKey);
2025     QCOMPARE(gridview->currentIndex(), 0);
2026     QTRY_COMPARE(gridview->contentX(), contentPosAtFirstItem.x());
2027     QTRY_COMPARE(gridview->contentY(), contentPosAtFirstItem.y());
2028
2029     releaseView(canvas);
2030 }
2031
2032 void tst_QQuickGridView::keyNavigation_data()
2033 {
2034     QTest::addColumn<QQuickGridView::Flow>("flow");
2035     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2036     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
2037     QTest::addColumn<Qt::Key>("forwardsKey");
2038     QTest::addColumn<Qt::Key>("backwardsKey");
2039     QTest::addColumn<QPointF>("contentPosAtFirstItem");
2040     QTest::addColumn<QPointF>("contentPosAtLastItem");
2041     QTest::addColumn<int>("indexRightOf7");
2042     QTest::addColumn<int>("indexLeftOf7");
2043     QTest::addColumn<int>("indexUpFrom7");
2044     QTest::addColumn<int>("indexDownFrom7");
2045
2046     QTest::newRow("LeftToRight, LtR, TtB")
2047             << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
2048             << Qt::Key_Right << Qt::Key_Left
2049             << QPointF(0, 0)
2050             << QPointF(0, 40)
2051             << 8 << 6 << 4 << 10;
2052
2053     QTest::newRow("LeftToRight, RtL, TtB")
2054             << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
2055             << Qt::Key_Left << Qt::Key_Right
2056             << QPointF(0, 0)
2057             << QPointF(0, 40)
2058             << 6 << 8 << 4 << 10;
2059
2060     QTest::newRow("LeftToRight, LtR, BtT")
2061             << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
2062             << Qt::Key_Right << Qt::Key_Left
2063             << QPointF(0, -320)
2064             << QPointF(0, -360)
2065             << 8 << 6 << 10 << 4;
2066
2067     QTest::newRow("LeftToRight, RtL, BtT")
2068             << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
2069             << Qt::Key_Left << Qt::Key_Right
2070             << QPointF(0, -320)
2071             << QPointF(0, -360)
2072             << 6 << 8 << 10 << 4;
2073
2074     QTest::newRow("TopToBottom, LtR, TtB")
2075             << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
2076             << Qt::Key_Down << Qt::Key_Up
2077             << QPointF(0, 0)
2078             << QPointF(80, 0)
2079             << 12 << 2 << 6 << 8;
2080
2081     QTest::newRow("TopToBottom, RtL, TtB")
2082             << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
2083             << Qt::Key_Down << Qt::Key_Up
2084             << QPointF(-240, 0)
2085             << QPointF(-320, 0)
2086             << 2 << 12 << 6 << 8;
2087
2088     QTest::newRow("TopToBottom, LtR, BtT")
2089             << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
2090             << Qt::Key_Up << Qt::Key_Down
2091             << QPointF(0, -320)
2092             << QPointF(80, -320)
2093             << 12 << 2 << 8 << 6;
2094
2095     QTest::newRow("TopToBottom, RtL, BtT")
2096             << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
2097             << Qt::Key_Up << Qt::Key_Down
2098             << QPointF(-240, -320)
2099             << QPointF(-320, -320)
2100             << 2 << 12 << 8 << 6;
2101 }
2102
2103 void tst_QQuickGridView::changeFlow()
2104 {
2105     QQuickView *canvas = createView();
2106
2107     QaimModel model;
2108     for (int i = 0; i < 30; i++)
2109         model.addItem("Item" + QString::number(i), QString::number(i));
2110
2111     QQmlContext *ctxt = canvas->rootContext();
2112     ctxt->setContextProperty("testModel", &model);
2113     ctxt->setContextProperty("testRightToLeft", QVariant(false));
2114     ctxt->setContextProperty("testTopToBottom", QVariant(false));
2115     ctxt->setContextProperty("testBottomToTop", QVariant(false));
2116
2117     canvas->setSource(testFileUrl("layouts.qml"));
2118     qApp->processEvents();
2119
2120     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2121     QTRY_VERIFY(gridview != 0);
2122
2123     QQuickItem *contentItem = gridview->contentItem();
2124     QTRY_VERIFY(contentItem != 0);
2125
2126     // Confirm items positioned correctly and indexes correct
2127     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2128     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2129         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2130         if (!item) qWarning() << "Item" << i << "not found";
2131         QTRY_VERIFY(item);
2132         QTRY_COMPARE(item->x(), qreal((i%3)*80));
2133         QTRY_COMPARE(item->y(), qreal((i/3)*60));
2134         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2135         QTRY_VERIFY(name != 0);
2136         QTRY_COMPARE(name->text(), model.name(i));
2137         QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2138         QTRY_VERIFY(number != 0);
2139         QTRY_COMPARE(number->text(), model.number(i));
2140     }
2141
2142     ctxt->setContextProperty("testTopToBottom", QVariant(true));
2143
2144     // Confirm items positioned correctly and indexes correct
2145     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2146     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2147         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2148         if (!item) qWarning() << "Item" << i << "not found";
2149         QTRY_VERIFY(item);
2150         QTRY_COMPARE(item->x(), qreal((i/5)*80));
2151         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2152         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2153         QTRY_VERIFY(name != 0);
2154         QTRY_COMPARE(name->text(), model.name(i));
2155         QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2156         QTRY_VERIFY(number != 0);
2157         QTRY_COMPARE(number->text(), model.number(i));
2158     }
2159
2160     ctxt->setContextProperty("testRightToLeft", QVariant(true));
2161
2162     // Confirm items positioned correctly and indexes correct
2163     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2164     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2165         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2166         if (!item) qWarning() << "Item" << i << "not found";
2167         QTRY_VERIFY(item);
2168         QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
2169         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2170         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2171         QTRY_VERIFY(name != 0);
2172         QTRY_COMPARE(name->text(), model.name(i));
2173         QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2174         QTRY_VERIFY(number != 0);
2175         QTRY_COMPARE(number->text(), model.number(i));
2176     }
2177     gridview->setContentX(100);
2178     QTRY_COMPARE(gridview->contentX(), 100.);
2179     ctxt->setContextProperty("testTopToBottom", QVariant(false));
2180     QTRY_COMPARE(gridview->contentX(), 0.);
2181
2182     // Confirm items positioned correctly and indexes correct
2183     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2184     for (int i = 0; i < model.count() && i < itemCount; ++i) {
2185         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2186         if (!item) qWarning() << "Item" << i << "not found";
2187         QTRY_VERIFY(item);
2188         QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
2189         QTRY_COMPARE(item->y(), qreal((i/3)*60));
2190         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
2191         QTRY_VERIFY(name != 0);
2192         QTRY_COMPARE(name->text(), model.name(i));
2193         QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", i);
2194         QTRY_VERIFY(number != 0);
2195         QTRY_COMPARE(number->text(), model.number(i));
2196     }
2197
2198     delete canvas;
2199 }
2200
2201 void tst_QQuickGridView::defaultValues()
2202 {
2203     QQmlEngine engine;
2204     QQmlComponent c(&engine, testFileUrl("gridview3.qml"));
2205     QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2206
2207     QTRY_VERIFY(obj != 0);
2208     QTRY_VERIFY(obj->model() == QVariant());
2209     QTRY_VERIFY(obj->delegate() == 0);
2210     QTRY_COMPARE(obj->currentIndex(), -1);
2211     QTRY_VERIFY(obj->currentItem() == 0);
2212     QTRY_COMPARE(obj->count(), 0);
2213     QTRY_VERIFY(obj->highlight() == 0);
2214     QTRY_VERIFY(obj->highlightItem() == 0);
2215     QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
2216     QTRY_VERIFY(obj->flow() == 0);
2217     QTRY_COMPARE(obj->isWrapEnabled(), false);
2218     QTRY_COMPARE(obj->cacheBuffer(), 0);
2219     QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
2220     QTRY_COMPARE(obj->cellHeight(), qreal(100));
2221     delete obj;
2222 }
2223
2224 void tst_QQuickGridView::properties()
2225 {
2226     QQmlEngine engine;
2227     QQmlComponent c(&engine, testFileUrl("gridview2.qml"));
2228     QQuickGridView *obj = qobject_cast<QQuickGridView*>(c.create());
2229
2230     QTRY_VERIFY(obj != 0);
2231     QTRY_VERIFY(obj->model() != QVariant());
2232     QTRY_VERIFY(obj->delegate() != 0);
2233     QTRY_COMPARE(obj->currentIndex(), 0);
2234     QTRY_VERIFY(obj->currentItem() != 0);
2235     QTRY_COMPARE(obj->count(), 4);
2236     QTRY_VERIFY(obj->highlight() != 0);
2237     QTRY_VERIFY(obj->highlightItem() != 0);
2238     QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
2239     QTRY_VERIFY(obj->flow() == 0);
2240     QTRY_COMPARE(obj->isWrapEnabled(), true);
2241     QTRY_COMPARE(obj->cacheBuffer(), 200);
2242     QTRY_COMPARE(obj->cellWidth(), qreal(100));
2243     QTRY_COMPARE(obj->cellHeight(), qreal(100));
2244     delete obj;
2245 }
2246
2247 void tst_QQuickGridView::propertyChanges()
2248 {
2249     QQuickView *canvas = createView();
2250     QTRY_VERIFY(canvas);
2251     canvas->setSource(testFileUrl("propertychangestest.qml"));
2252
2253     QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2254     QTRY_VERIFY(gridView);
2255
2256     QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
2257     QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
2258     QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
2259     QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
2260
2261     QTRY_COMPARE(gridView->isWrapEnabled(), true);
2262     QTRY_COMPARE(gridView->cacheBuffer(), 10);
2263     QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2264
2265     gridView->setWrapEnabled(false);
2266     gridView->setCacheBuffer(3);
2267     gridView->setFlow(QQuickGridView::FlowTopToBottom);
2268
2269     QTRY_COMPARE(gridView->isWrapEnabled(), false);
2270     QTRY_COMPARE(gridView->cacheBuffer(), 3);
2271     QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2272
2273     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2274     QTRY_COMPARE(cacheBufferSpy.count(),1);
2275     QTRY_COMPARE(flowSpy.count(),1);
2276
2277     gridView->setWrapEnabled(false);
2278     gridView->setCacheBuffer(3);
2279     gridView->setFlow(QQuickGridView::FlowTopToBottom);
2280
2281     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
2282     QTRY_COMPARE(cacheBufferSpy.count(),1);
2283     QTRY_COMPARE(flowSpy.count(),1);
2284
2285     gridView->setFlow(QQuickGridView::FlowLeftToRight);
2286     QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowLeftToRight);
2287
2288     gridView->setWrapEnabled(true);
2289     gridView->setCacheBuffer(5);
2290     gridView->setLayoutDirection(Qt::RightToLeft);
2291
2292     QTRY_COMPARE(gridView->isWrapEnabled(), true);
2293     QTRY_COMPARE(gridView->cacheBuffer(), 5);
2294     QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
2295
2296     QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2297     QTRY_COMPARE(cacheBufferSpy.count(),2);
2298     QTRY_COMPARE(layoutSpy.count(),1);
2299     QTRY_COMPARE(flowSpy.count(),2);
2300
2301     gridView->setWrapEnabled(true);
2302     gridView->setCacheBuffer(5);
2303     gridView->setLayoutDirection(Qt::RightToLeft);
2304
2305     QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
2306     QTRY_COMPARE(cacheBufferSpy.count(),2);
2307     QTRY_COMPARE(layoutSpy.count(),1);
2308     QTRY_COMPARE(flowSpy.count(),2);
2309
2310     gridView->setFlow(QQuickGridView::FlowTopToBottom);
2311     QTRY_COMPARE(gridView->flow(), QQuickGridView::FlowTopToBottom);
2312     QTRY_COMPARE(flowSpy.count(),3);
2313
2314     gridView->setFlow(QQuickGridView::FlowTopToBottom);
2315     QTRY_COMPARE(flowSpy.count(),3);
2316
2317     delete canvas;
2318 }
2319
2320 void tst_QQuickGridView::componentChanges()
2321 {
2322     QQuickView *canvas = createView();
2323     QTRY_VERIFY(canvas);
2324     canvas->setSource(testFileUrl("propertychangestest.qml"));
2325
2326     QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2327     QTRY_VERIFY(gridView);
2328
2329     QQmlComponent component(canvas->engine());
2330     component.setData("import QtQuick 2.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
2331
2332     QQmlComponent delegateComponent(canvas->engine());
2333     delegateComponent.setData("import QtQuick 2.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
2334
2335     QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
2336     QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
2337     QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
2338     QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
2339     QSignalSpy headerItemSpy(gridView, SIGNAL(headerItemChanged()));
2340     QSignalSpy footerItemSpy(gridView, SIGNAL(footerItemChanged()));
2341
2342     gridView->setHighlight(&component);
2343     gridView->setDelegate(&delegateComponent);
2344     gridView->setHeader(&component);
2345     gridView->setFooter(&component);
2346
2347     QTRY_COMPARE(gridView->highlight(), &component);
2348     QTRY_COMPARE(gridView->delegate(), &delegateComponent);
2349     QTRY_COMPARE(gridView->header(), &component);
2350     QTRY_COMPARE(gridView->footer(), &component);
2351
2352     QVERIFY(gridView->headerItem());
2353     QVERIFY(gridView->footerItem());
2354
2355     QTRY_COMPARE(highlightSpy.count(),1);
2356     QTRY_COMPARE(delegateSpy.count(),1);
2357     QTRY_COMPARE(headerSpy.count(),1);
2358     QTRY_COMPARE(footerSpy.count(),1);
2359     QTRY_COMPARE(headerItemSpy.count(),1);
2360     QTRY_COMPARE(footerItemSpy.count(),1);
2361
2362     gridView->setHighlight(&component);
2363     gridView->setDelegate(&delegateComponent);
2364     gridView->setHeader(&component);
2365     gridView->setFooter(&component);
2366
2367     QTRY_COMPARE(highlightSpy.count(),1);
2368     QTRY_COMPARE(delegateSpy.count(),1);
2369     QTRY_COMPARE(headerSpy.count(),1);
2370     QTRY_COMPARE(footerSpy.count(),1);
2371     QTRY_COMPARE(headerItemSpy.count(),1);
2372     QTRY_COMPARE(footerItemSpy.count(),1);
2373
2374     delete canvas;
2375 }
2376
2377 void tst_QQuickGridView::modelChanges()
2378 {
2379     QQuickView *canvas = createView();
2380     QTRY_VERIFY(canvas);
2381     canvas->setSource(testFileUrl("propertychangestest.qml"));
2382
2383     QQuickGridView *gridView = canvas->rootObject()->findChild<QQuickGridView*>("gridView");
2384     QTRY_VERIFY(gridView);
2385
2386     QQuickListModel *alternateModel = canvas->rootObject()->findChild<QQuickListModel*>("alternateModel");
2387     QTRY_VERIFY(alternateModel);
2388     QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
2389     QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
2390
2391     gridView->setModel(modelVariant);
2392     QTRY_COMPARE(gridView->model(), modelVariant);
2393     QTRY_COMPARE(modelSpy.count(),1);
2394
2395     gridView->setModel(modelVariant);
2396     QTRY_COMPARE(modelSpy.count(),1);
2397
2398     gridView->setModel(QVariant());
2399     QTRY_COMPARE(modelSpy.count(),2);
2400     delete canvas;
2401 }
2402
2403 void tst_QQuickGridView::positionViewAtIndex()
2404 {
2405     QQuickView *canvas = createView();
2406
2407     QaimModel model;
2408     for (int i = 0; i < 40; i++)
2409         model.addItem("Item" + QString::number(i), "");
2410
2411     QQmlContext *ctxt = canvas->rootContext();
2412     ctxt->setContextProperty("testModel", &model);
2413     ctxt->setContextProperty("testRightToLeft", QVariant(false));
2414     ctxt->setContextProperty("testTopToBottom", QVariant(false));
2415     ctxt->setContextProperty("testBottomToTop", QVariant(false));
2416
2417     canvas->setSource(testFileUrl("layouts.qml"));
2418     canvas->show();
2419     qApp->processEvents();
2420
2421     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2422     QTRY_VERIFY(gridview != 0);
2423     QQuickItem *contentItem = gridview->contentItem();
2424     QTRY_VERIFY(contentItem != 0);
2425     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2426
2427     // Confirm items positioned correctly
2428     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2429     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2430         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2431         if (!item) qWarning() << "Item" << i << "not found";
2432         QTRY_VERIFY(item);
2433         QTRY_COMPARE(item->x(), (i%3)*80.);
2434         QTRY_COMPARE(item->y(), (i/3)*60.);
2435     }
2436
2437     // Position on a currently visible item
2438     gridview->positionViewAtIndex(4, QQuickGridView::Beginning);
2439     QTRY_COMPARE(gridview->indexAt(120, 90), 4);
2440     QTRY_COMPARE(gridview->contentY(), 60.);
2441
2442     // Confirm items positioned correctly
2443     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2444     for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2445         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2446         if (!item) qWarning() << "Item" << i << "not found";
2447         QTRY_VERIFY(item);
2448         QTRY_COMPARE(item->x(), (i%3)*80.);
2449         QTRY_COMPARE(item->y(), (i/3)*60.);
2450     }
2451
2452     // Position on an item beyond the visible items
2453     gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2454     QTRY_COMPARE(gridview->indexAt(40, 450), 21);
2455     QTRY_COMPARE(gridview->contentY(), 420.);
2456
2457     // Confirm items positioned correctly
2458     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2459     for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2460         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2461         if (!item) qWarning() << "Item" << i << "not found";
2462         QTRY_VERIFY(item);
2463         QTRY_COMPARE(item->x(), (i%3)*80.);
2464         QTRY_COMPARE(item->y(), (i/3)*60.);
2465     }
2466
2467     // Position on an item that would leave empty space if positioned at the top
2468     gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2469     QTRY_COMPARE(gridview->indexAt(120, 630), 31);
2470     QTRY_COMPARE(gridview->contentY(), 520.);
2471
2472     // Confirm items positioned correctly
2473     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2474     for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2475         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2476         if (!item) qWarning() << "Item" << i << "not found";
2477         QTRY_VERIFY(item);
2478         QTRY_COMPARE(item->x(), (i%3)*80.);
2479         QTRY_COMPARE(item->y(), (i/3)*60.);
2480     }
2481
2482     // Position at the beginning again
2483     gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2484     QTRY_COMPARE(gridview->indexAt(0, 0), 0);
2485     QTRY_COMPARE(gridview->indexAt(40, 30), 0);
2486     QTRY_COMPARE(gridview->indexAt(80, 60), 4);
2487     QTRY_COMPARE(gridview->contentY(), 0.);
2488
2489     // Confirm items positioned correctly
2490     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2491     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2492         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2493         if (!item) qWarning() << "Item" << i << "not found";
2494         QTRY_VERIFY(item);
2495         QTRY_COMPARE(item->x(), (i%3)*80.);
2496         QTRY_COMPARE(item->y(), (i/3)*60.);
2497     }
2498
2499     // Position at End
2500     gridview->positionViewAtIndex(30, QQuickGridView::End);
2501     QTRY_COMPARE(gridview->contentY(), 340.);
2502
2503     // Position in Center
2504     gridview->positionViewAtIndex(15, QQuickGridView::Center);
2505     QTRY_COMPARE(gridview->contentY(), 170.);
2506
2507     // Ensure at least partially visible
2508     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2509     QTRY_COMPARE(gridview->contentY(), 170.);
2510
2511     gridview->setContentY(302);
2512     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2513     QTRY_COMPARE(gridview->contentY(), 302.);
2514
2515     gridview->setContentY(360);
2516     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2517     QTRY_COMPARE(gridview->contentY(), 300.);
2518
2519     gridview->setContentY(60);
2520     gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2521     QTRY_COMPARE(gridview->contentY(), 60.);
2522
2523     gridview->setContentY(20);
2524     gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2525     QTRY_COMPARE(gridview->contentY(), 100.);
2526
2527     // Ensure completely visible
2528     gridview->setContentY(120);
2529     gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2530     QTRY_COMPARE(gridview->contentY(), 120.);
2531
2532     gridview->setContentY(302);
2533     gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2534     QTRY_COMPARE(gridview->contentY(), 300.);
2535
2536     gridview->setContentY(60);
2537     gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2538     QTRY_COMPARE(gridview->contentY(), 100.);
2539
2540     // Test for Top To Bottom layout
2541     ctxt->setContextProperty("testTopToBottom", QVariant(true));
2542
2543     // Confirm items positioned correctly
2544     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2545     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2546         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2547         if (!item) qWarning() << "Item" << i << "not found";
2548         QTRY_VERIFY(item);
2549         QTRY_COMPARE(item->x(), (i/5)*80.);
2550         QTRY_COMPARE(item->y(), (i%5)*60.);
2551     }
2552
2553     // Position at End
2554     gridview->positionViewAtIndex(30, QQuickGridView::End);
2555     QTRY_COMPARE(gridview->contentX(), 320.);
2556     QTRY_COMPARE(gridview->contentY(), 0.);
2557
2558     // Position in Center
2559     gridview->positionViewAtIndex(15, QQuickGridView::Center);
2560     QTRY_COMPARE(gridview->contentX(), 160.);
2561
2562     // Ensure at least partially visible
2563     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2564     QTRY_COMPARE(gridview->contentX(), 160.);
2565
2566     gridview->setContentX(170);
2567     gridview->positionViewAtIndex(25, QQuickGridView::Visible);
2568     QTRY_COMPARE(gridview->contentX(), 170.);
2569
2570     gridview->positionViewAtIndex(30, QQuickGridView::Visible);
2571     QTRY_COMPARE(gridview->contentX(), 320.);
2572
2573     gridview->setContentX(170);
2574     gridview->positionViewAtIndex(25, QQuickGridView::Contain);
2575     QTRY_COMPARE(gridview->contentX(), 240.);
2576
2577     // positionViewAtBeginning
2578     gridview->positionViewAtBeginning();
2579     QTRY_COMPARE(gridview->contentX(), 0.);
2580
2581     gridview->setContentX(80);
2582     canvas->rootObject()->setProperty("showHeader", true);
2583     gridview->positionViewAtBeginning();
2584     QTRY_COMPARE(gridview->contentX(), -30.);
2585
2586     // positionViewAtEnd
2587     gridview->positionViewAtEnd();
2588     QTRY_COMPARE(gridview->contentX(), 400.);   // 8*80 - 240   (8 columns)
2589
2590     gridview->setContentX(80);
2591     canvas->rootObject()->setProperty("showFooter", true);
2592     gridview->positionViewAtEnd();
2593     QTRY_COMPARE(gridview->contentX(), 430.);
2594
2595     // set current item to outside visible view, position at beginning
2596     // and ensure highlight moves to current item
2597     gridview->setCurrentIndex(6);
2598     gridview->positionViewAtBeginning();
2599     QTRY_COMPARE(gridview->contentX(), -30.);
2600     QVERIFY(gridview->highlightItem());
2601     QCOMPARE(gridview->highlightItem()->x(), 80.);
2602
2603     delete canvas;
2604 }
2605
2606 void tst_QQuickGridView::snapping()
2607 {
2608     QQuickView *canvas = createView();
2609
2610     QaimModel model;
2611     for (int i = 0; i < 40; i++)
2612         model.addItem("Item" + QString::number(i), "");
2613
2614     canvas->rootContext()->setContextProperty("testModel", &model);
2615     canvas->setSource(testFileUrl("gridview1.qml"));
2616     qApp->processEvents();
2617
2618     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2619     QTRY_VERIFY(gridview != 0);
2620
2621     gridview->setHeight(220);
2622     QCOMPARE(gridview->height(), 220.);
2623
2624     gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2625     QCOMPARE(gridview->contentY(), 80.);
2626
2627     gridview->setContentY(0);
2628     QCOMPARE(gridview->contentY(), 0.);
2629
2630     gridview->setSnapMode(QQuickGridView::SnapToRow);
2631     QCOMPARE(gridview->snapMode(), QQuickGridView::SnapToRow);
2632
2633     gridview->positionViewAtIndex(12, QQuickGridView::Visible);
2634     QCOMPARE(gridview->contentY(), 60.);
2635
2636     gridview->positionViewAtIndex(15, QQuickGridView::End);
2637     QCOMPARE(gridview->contentY(), 120.);
2638
2639     delete canvas;
2640
2641 }
2642
2643 void tst_QQuickGridView::mirroring()
2644 {
2645     QQuickView *canvasA = createView();
2646     canvasA->setSource(testFileUrl("mirroring.qml"));
2647     QQuickGridView *gridviewA = findItem<QQuickGridView>(canvasA->rootObject(), "view");
2648     QTRY_VERIFY(gridviewA != 0);
2649
2650     QQuickView *canvasB = createView();
2651     canvasB->setSource(testFileUrl("mirroring.qml"));
2652     QQuickGridView *gridviewB = findItem<QQuickGridView>(canvasB->rootObject(), "view");
2653     QTRY_VERIFY(gridviewA != 0);
2654     qApp->processEvents();
2655
2656     QList<QString> objectNames;
2657     objectNames << "item1" << "item2"; // << "item3"
2658
2659     gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2660     gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2661     QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
2662
2663     // LTR != RTL
2664     foreach (const QString objectName, objectNames)
2665         QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2666
2667     gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2668     gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
2669
2670     // LTR == LTR
2671     foreach (const QString objectName, objectNames)
2672         QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2673
2674     QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
2675     QQuickItemPrivate::get(gridviewB)->setLayoutMirror(true);
2676     QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
2677
2678     // LTR != LTR+mirror
2679     foreach (const QString objectName, objectNames)
2680         QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2681
2682     gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
2683
2684     // RTL == LTR+mirror
2685     foreach (const QString objectName, objectNames)
2686         QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2687
2688     gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
2689
2690     // RTL != RTL+mirror
2691     foreach (const QString objectName, objectNames)
2692         QVERIFY(findItem<QQuickItem>(gridviewA, objectName)->x() != findItem<QQuickItem>(gridviewB, objectName)->x());
2693
2694     gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
2695
2696     // LTR == RTL+mirror
2697     foreach (const QString objectName, objectNames)
2698         QCOMPARE(findItem<QQuickItem>(gridviewA, objectName)->x(), findItem<QQuickItem>(gridviewB, objectName)->x());
2699
2700     delete canvasA;
2701     delete canvasB;
2702 }
2703
2704 void tst_QQuickGridView::positionViewAtIndex_rightToLeft()
2705 {
2706     QQuickView *canvas = createView();
2707
2708     QaimModel model;
2709     for (int i = 0; i < 40; i++)
2710         model.addItem("Item" + QString::number(i), "");
2711
2712     QQmlContext *ctxt = canvas->rootContext();
2713     ctxt->setContextProperty("testModel", &model);
2714     ctxt->setContextProperty("testTopToBottom", QVariant(true));
2715     ctxt->setContextProperty("testRightToLeft", QVariant(true));
2716     ctxt->setContextProperty("testBottomToTop", QVariant(false));
2717
2718     canvas->setSource(testFileUrl("layouts.qml"));
2719     qApp->processEvents();
2720
2721     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2722     QTRY_VERIFY(gridview != 0);
2723
2724     QQuickItem *contentItem = gridview->contentItem();
2725     QTRY_VERIFY(contentItem != 0);
2726
2727     // Confirm items positioned correctly
2728     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2729     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2730         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2731         if (!item) qWarning() << "Item" << i << "not found";
2732         QTRY_VERIFY(item);
2733         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2734         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2735     }
2736
2737     // Position on a currently visible item
2738     gridview->positionViewAtIndex(6, QQuickGridView::Beginning);
2739     QTRY_COMPARE(gridview->contentX(), -320.);
2740
2741     // Confirm items positioned correctly
2742     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2743     for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
2744         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2745         if (!item) qWarning() << "Item" << i << "not found";
2746         QTRY_VERIFY(item);
2747         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2748         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2749     }
2750
2751     // Position on an item beyond the visible items
2752     gridview->positionViewAtIndex(21, QQuickGridView::Beginning);
2753     QTRY_COMPARE(gridview->contentX(), -560.);
2754
2755     // Confirm items positioned correctly
2756     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2757     for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
2758         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2759         if (!item) qWarning() << "Item" << i << "not found";
2760         QTRY_VERIFY(item);
2761         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2762         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2763     }
2764
2765     // Position on an item that would leave empty space if positioned at the top
2766     gridview->positionViewAtIndex(31, QQuickGridView::Beginning);
2767     QTRY_COMPARE(gridview->contentX(), -640.);
2768
2769     // Confirm items positioned correctly
2770     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2771     for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
2772         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2773         if (!item) qWarning() << "Item" << i << "not found";
2774         QTRY_VERIFY(item);
2775         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2776         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2777     }
2778
2779     // Position at the beginning again
2780     gridview->positionViewAtIndex(0, QQuickGridView::Beginning);
2781     QTRY_COMPARE(gridview->contentX(), -240.);
2782
2783     // Confirm items positioned correctly
2784     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
2785     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
2786         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
2787         if (!item) qWarning() << "Item" << i << "not found";
2788         QTRY_VERIFY(item);
2789         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
2790         QTRY_COMPARE(item->y(), qreal((i%5)*60));
2791     }
2792
2793     // Position at End
2794     gridview->positionViewAtIndex(30, QQuickGridView::End);
2795     QTRY_COMPARE(gridview->contentX(), -560.);
2796
2797     // Position in Center
2798     gridview->positionViewAtIndex(15, QQuickGridView::Center);
2799     QTRY_COMPARE(gridview->contentX(), -400.);
2800
2801     // Ensure at least partially visible
2802     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2803     QTRY_COMPARE(gridview->contentX(), -400.);
2804
2805     gridview->setContentX(-555.);
2806     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2807     QTRY_COMPARE(gridview->contentX(), -555.);
2808
2809     gridview->setContentX(-239);
2810     gridview->positionViewAtIndex(15, QQuickGridView::Visible);
2811     QTRY_COMPARE(gridview->contentX(), -320.);
2812
2813     gridview->setContentX(-239);
2814     gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2815     QTRY_COMPARE(gridview->contentX(), -400.);
2816
2817     gridview->setContentX(-640);
2818     gridview->positionViewAtIndex(20, QQuickGridView::Visible);
2819     QTRY_COMPARE(gridview->contentX(), -560.);
2820
2821     // Ensure completely visible
2822     gridview->setContentX(-400);
2823     gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2824     QTRY_COMPARE(gridview->contentX(), -400.);
2825
2826     gridview->setContentX(-315);
2827     gridview->positionViewAtIndex(15, QQuickGridView::Contain);
2828     QTRY_COMPARE(gridview->contentX(), -320.);
2829
2830     gridview->setContentX(-640);
2831     gridview->positionViewAtIndex(20, QQuickGridView::Contain);
2832     QTRY_COMPARE(gridview->contentX(), -560.);
2833
2834     delete canvas;
2835 }
2836
2837 void tst_QQuickGridView::resetModel()
2838 {
2839     QQuickView *canvas = createView();
2840
2841     QStringList strings;
2842     strings << "one" << "two" << "three";
2843     QStringListModel model(strings);
2844
2845     QQmlContext *ctxt = canvas->rootContext();
2846     ctxt->setContextProperty("testModel", &model);
2847
2848     canvas->setSource(testFileUrl("displaygrid.qml"));
2849     canvas->show();
2850     qApp->processEvents();
2851
2852     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2853     QTRY_VERIFY(gridview != 0);
2854     QQuickItem *contentItem = gridview->contentItem();
2855     QTRY_VERIFY(contentItem != 0);
2856     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2857
2858     QTRY_COMPARE(gridview->count(), model.rowCount());
2859
2860     for (int i = 0; i < model.rowCount(); ++i) {
2861         QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2862         QTRY_VERIFY(display != 0);
2863         QTRY_COMPARE(display->text(), strings.at(i));
2864     }
2865
2866     strings.clear();
2867     strings << "four" << "five" << "six" << "seven";
2868     model.setStringList(strings);
2869
2870     QTRY_COMPARE(gridview->count(), model.rowCount());
2871
2872     for (int i = 0; i < model.rowCount(); ++i) {
2873         QQuickText *display = findItem<QQuickText>(contentItem, "displayText", i);
2874         QTRY_VERIFY(display != 0);
2875         QTRY_COMPARE(display->text(), strings.at(i));
2876     }
2877
2878     delete canvas;
2879 }
2880
2881 void tst_QQuickGridView::enforceRange()
2882 {
2883     QQuickView *canvas = createView();
2884
2885     QaimModel model;
2886     for (int i = 0; i < 30; i++)
2887         model.addItem("Item" + QString::number(i), "");
2888
2889     QQmlContext *ctxt = canvas->rootContext();
2890     ctxt->setContextProperty("testModel", &model);
2891     ctxt->setContextProperty("testRightToLeft", QVariant(false));
2892     ctxt->setContextProperty("testTopToBottom", QVariant(false));
2893
2894     canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2895     canvas->show();
2896     qApp->processEvents();
2897     QVERIFY(canvas->rootObject() != 0);
2898
2899     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2900     QTRY_VERIFY(gridview != 0);
2901
2902     QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
2903     QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
2904     QTRY_COMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2905     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
2906
2907     QQuickItem *contentItem = gridview->contentItem();
2908     QTRY_VERIFY(contentItem != 0);
2909
2910     // view should be positioned at the top of the range.
2911     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2912     QTRY_VERIFY(item);
2913     QTRY_COMPARE(gridview->contentY(), -100.0);
2914
2915     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2916     QTRY_VERIFY(name != 0);
2917     QTRY_COMPARE(name->text(), model.name(0));
2918     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2919     QTRY_VERIFY(number != 0);
2920     QTRY_COMPARE(number->text(), model.number(0));
2921
2922     // Check currentIndex is updated when contentItem moves
2923     gridview->setContentY(0);
2924     QTRY_COMPARE(gridview->currentIndex(), 2);
2925
2926     gridview->setCurrentIndex(5);
2927     QTRY_COMPARE(gridview->contentY(), 100.);
2928
2929     QaimModel model2;
2930     for (int i = 0; i < 5; i++)
2931         model2.addItem("Item" + QString::number(i), "");
2932
2933     ctxt->setContextProperty("testModel", &model2);
2934     QCOMPARE(gridview->count(), 5);
2935
2936     delete canvas;
2937 }
2938
2939 void tst_QQuickGridView::enforceRange_rightToLeft()
2940 {
2941     QQuickView *canvas = createView();
2942
2943     QaimModel model;
2944     for (int i = 0; i < 30; i++)
2945         model.addItem("Item" + QString::number(i), "");
2946
2947     QQmlContext *ctxt = canvas->rootContext();
2948     ctxt->setContextProperty("testModel", &model);
2949     ctxt->setContextProperty("testRightToLeft", QVariant(true));
2950     ctxt->setContextProperty("testTopToBottom", QVariant(true));
2951
2952     canvas->setSource(testFileUrl("gridview-enforcerange.qml"));
2953     qApp->processEvents();
2954     QVERIFY(canvas->rootObject() != 0);
2955
2956     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
2957     QTRY_VERIFY(gridview != 0);
2958
2959     QCOMPARE(gridview->preferredHighlightBegin(), 100.0);
2960     QCOMPARE(gridview->preferredHighlightEnd(), 100.0);
2961     QCOMPARE(gridview->highlightRangeMode(), QQuickGridView::StrictlyEnforceRange);
2962
2963     QQuickItem *contentItem = gridview->contentItem();
2964     QVERIFY(contentItem != 0);
2965
2966     // view should be positioned at the top of the range.
2967     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
2968     QVERIFY(item);
2969     QTRY_COMPARE(gridview->contentX(), -140.);
2970     QTRY_COMPARE(gridview->contentY(), 0.0);
2971
2972     QQuickText *name = findItem<QQuickText>(contentItem, "textName", 0);
2973     QTRY_VERIFY(name != 0);
2974     QTRY_COMPARE(name->text(), model.name(0));
2975     QQuickText *number = findItem<QQuickText>(contentItem, "textNumber", 0);
2976     QTRY_VERIFY(number != 0);
2977     QTRY_COMPARE(number->text(), model.number(0));
2978
2979     // Check currentIndex is updated when contentItem moves
2980     gridview->setContentX(-240);
2981     QTRY_COMPARE(gridview->currentIndex(), 3);
2982
2983     gridview->setCurrentIndex(7);
2984     QTRY_COMPARE(gridview->contentX(), -340.);
2985     QTRY_COMPARE(gridview->contentY(), 0.0);
2986
2987     QaimModel model2;
2988     for (int i = 0; i < 5; i++)
2989         model2.addItem("Item" + QString::number(i), "");
2990
2991     ctxt->setContextProperty("testModel", &model2);
2992     QCOMPARE(gridview->count(), 5);
2993
2994     delete canvas;
2995 }
2996
2997 void tst_QQuickGridView::QTBUG_8456()
2998 {
2999     QQuickView *canvas = createView();
3000
3001     canvas->setSource(testFileUrl("setindex.qml"));
3002     qApp->processEvents();
3003
3004     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3005     QTRY_VERIFY(gridview != 0);
3006
3007     QTRY_COMPARE(gridview->currentIndex(), 0);
3008
3009     delete canvas;
3010 }
3011
3012 void tst_QQuickGridView::manualHighlight()
3013 {
3014     QQuickView *canvas = createView();
3015
3016     QString filename(testFile("manual-highlight.qml"));
3017     canvas->setSource(QUrl::fromLocalFile(filename));
3018
3019     qApp->processEvents();
3020
3021     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3022     QTRY_VERIFY(gridview != 0);
3023
3024     QQuickItem *contentItem = gridview->contentItem();
3025     QTRY_VERIFY(contentItem != 0);
3026
3027     QTRY_COMPARE(gridview->currentIndex(), 0);
3028     QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3029     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3030     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3031
3032     gridview->setCurrentIndex(2);
3033
3034     QTRY_COMPARE(gridview->currentIndex(), 2);
3035     QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3036     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3037     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3038
3039     gridview->positionViewAtIndex(8, QQuickGridView::Contain);
3040
3041     QTRY_COMPARE(gridview->currentIndex(), 2);
3042     QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 2));
3043     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3044     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3045
3046     gridview->setFlow(QQuickGridView::FlowTopToBottom);
3047     QTRY_COMPARE(gridview->flow(), QQuickGridView::FlowTopToBottom);
3048
3049     gridview->setCurrentIndex(0);
3050     QTRY_COMPARE(gridview->currentIndex(), 0);
3051     QTRY_COMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
3052     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
3053     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
3054
3055     delete canvas;
3056 }
3057
3058
3059 void tst_QQuickGridView::footer()
3060 {
3061     QFETCH(QQuickGridView::Flow, flow);
3062     QFETCH(Qt::LayoutDirection, layoutDirection);
3063     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3064     QFETCH(QPointF, initialFooterPos);
3065     QFETCH(QPointF, changedFooterPos);
3066     QFETCH(QPointF, initialContentPos);
3067     QFETCH(QPointF, firstDelegatePos);
3068     QFETCH(QPointF, resizeContentPos);
3069
3070     QQuickView *canvas = getView();
3071     canvas->show();
3072
3073     QaimModel model;
3074     for (int i = 0; i < 7; i++)
3075         model.addItem("Item" + QString::number(i), "");
3076
3077     QQmlContext *ctxt = canvas->rootContext();
3078     ctxt->setContextProperty("testModel", &model);
3079     canvas->setSource(testFileUrl("footer.qml"));
3080     qApp->processEvents();
3081
3082     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3083     QTRY_VERIFY(gridview != 0);
3084     gridview->setFlow(flow);
3085     gridview->setLayoutDirection(layoutDirection);
3086     gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3087     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3088
3089     QQuickItem *contentItem = gridview->contentItem();
3090     QTRY_VERIFY(contentItem != 0);
3091
3092     QQuickText *footer = findItem<QQuickText>(contentItem, "footer");
3093     QVERIFY(footer);
3094     QVERIFY(footer == gridview->footerItem());
3095
3096     QCOMPARE(footer->pos(), initialFooterPos);
3097     QCOMPARE(footer->width(), 100.);
3098     QCOMPARE(footer->height(), 30.);
3099     QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3100
3101     if (flow == QQuickGridView::FlowLeftToRight)
3102         QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + footer->height());
3103     else
3104         QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + footer->width());
3105
3106     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3107     QVERIFY(item);
3108     QCOMPARE(item->pos(), firstDelegatePos);
3109
3110     if (flow == QQuickGridView::FlowLeftToRight) {
3111         // shrink by one row
3112         model.removeItem(2);
3113         if (verticalLayoutDirection == QQuickItemView::TopToBottom)
3114             QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
3115         else
3116             QTRY_COMPARE(footer->y(), initialFooterPos.y() + gridview->cellHeight());
3117     } else {
3118         // shrink by one column
3119         model.removeItem(2);
3120         model.removeItem(3);
3121         if (layoutDirection == Qt::LeftToRight)
3122             QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
3123         else
3124             QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
3125     }
3126
3127     // remove all items
3128     model.clear();
3129
3130     QPointF posWhenNoItems(0, 0);
3131     if (layoutDirection == Qt::RightToLeft)
3132         posWhenNoItems.setX(flow == QQuickGridView::FlowLeftToRight ? gridview->width() - footer->width() : -footer->width());
3133     if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3134         posWhenNoItems.setY(-footer->height());
3135     QTRY_COMPARE(footer->pos(), posWhenNoItems);
3136
3137     // if header is toggled, it shouldn't affect the footer position
3138     canvas->rootObject()->setProperty("showHeader", true);
3139     QVERIFY(findItem<QQuickItem>(contentItem, "header") != 0);
3140     QTRY_COMPARE(footer->pos(), posWhenNoItems);
3141     canvas->rootObject()->setProperty("showHeader", false);
3142
3143     // add 30 items
3144     for (int i = 0; i < 30; i++)
3145         model.addItem("Item" + QString::number(i), "");
3146
3147     QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
3148     QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
3149
3150     QCOMPARE(footerItemSpy.count(), 1);
3151
3152     footer = findItem<QQuickText>(contentItem, "footer");
3153     QVERIFY(!footer);
3154     footer = findItem<QQuickText>(contentItem, "footer2");
3155     QVERIFY(footer);
3156     QVERIFY(footer == gridview->footerItem());
3157
3158     QCOMPARE(footer->pos(), changedFooterPos);
3159     QCOMPARE(footer->width(), 50.);
3160     QCOMPARE(footer->height(), 20.);
3161
3162     // changing the footer shouldn't change the content pos
3163     QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3164
3165     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3166     QVERIFY(item);
3167     QCOMPARE(item->pos(), firstDelegatePos);
3168
3169     gridview->positionViewAtEnd();
3170     footer->setHeight(10);
3171     footer->setWidth(40);
3172     QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3173
3174     releaseView(canvas);
3175 }
3176
3177 void tst_QQuickGridView::footer_data()
3178 {
3179     QTest::addColumn<QQuickGridView::Flow>("flow");
3180     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3181     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3182     QTest::addColumn<QPointF>("initialFooterPos");
3183     QTest::addColumn<QPointF>("changedFooterPos");
3184     QTest::addColumn<QPointF>("initialContentPos");
3185     QTest::addColumn<QPointF>("firstDelegatePos");
3186     QTest::addColumn<QPointF>("resizeContentPos");
3187
3188     // footer1 = 100 x 30
3189     // footer2 = 50 x 20
3190     // cells = 80 * 60
3191     // view width = 240
3192     // view height = 320
3193
3194     // footer below items, bottom left
3195     QTest::newRow("LeftToRight, LtR, TtB")
3196         << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3197         << QPointF(0, 3 * 60)  // 180 = height of 3 rows (cell height is 60)
3198         << QPointF(0, 10 * 60)  // 30 items = 10 rows
3199         << QPointF(0, 0)
3200         << QPointF(0, 0)
3201         << QPointF(0, (10 * 60) - 320 + 10);
3202
3203     // footer below items, bottom right
3204     QTest::newRow("LeftToRight, RtL, TtB")
3205         << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3206         << QPointF(240 - 100, 3 * 60)
3207         << QPointF((240 - 100) + 50, 10 * 60)     // 50 = width diff between old and new footers
3208         << QPointF(0, 0)
3209         << QPointF(240 - 80, 0)
3210         << QPointF(0, (10 * 60) - 320 + 10);
3211
3212     // footer above items, top left
3213     QTest::newRow("LeftToRight, LtR, BtT")
3214         << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3215         << QPointF(0, -(3 * 60) - 30)
3216         << QPointF(0, -(10 * 60) - 20)
3217         << QPointF(0, -320)
3218         << QPointF(0, -60)
3219         << QPointF(0, -(10 * 60) - 10);
3220
3221     // footer above items, top right
3222     QTest::newRow("LeftToRight, RtL, BtT")
3223         << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3224         << QPointF(240 - 100, -(3 * 60) - 30)
3225         << QPointF((240 - 100) + 50, -(10 * 60) - 20)
3226         << QPointF(0, -320)
3227         << QPointF(240 - 80, -60)
3228         << QPointF(0, -(10 * 60) - 10);
3229
3230
3231     // footer to right of items, bottom right
3232     QTest::newRow("TopToBottom, LtR, TtB")
3233         << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3234         << QPointF(2 * 80, 0)      // 2 columns, cell width 80
3235         << QPointF(6 * 80, 0)      // 30 items = 6 columns
3236         << QPointF(0, 0)
3237         << QPointF(0, 0)
3238         << QPointF((6 * 80) - 240 + 40, 0);
3239
3240     // footer to left of items, bottom right
3241     QTest::newRow("TopToBottom, RtL, TtB")
3242         << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3243         << QPointF(-(2 * 80) - 100, 0)
3244         << QPointF(-(6 * 80) - 50, 0)     // 50 = new footer width
3245         << QPointF(-240, 0)
3246         << QPointF(-80, 0)
3247         << QPointF(-(6 * 80) - 40, 0);
3248
3249     // footer to right of items, top right
3250     QTest::newRow("TopToBottom, LtR, BtT")
3251         << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3252         << QPointF(2 * 80, -30)
3253         << QPointF(6 * 80, -20)
3254         << QPointF(0, -320)
3255         << QPointF(0, -60)
3256         << QPointF((6 * 80) - 240 + 40, -320);
3257
3258     // footer to left of items, top left
3259     QTest::newRow("TopToBottom, RtL, BtT")
3260         << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3261         << QPointF(-(2 * 80) - 100, -30)
3262         << QPointF(-(6 * 80) - 50, -20)
3263         << QPointF(-240, -320)
3264         << QPointF(-80, -60)
3265         << QPointF(-(6 * 80) - 40, -320);
3266 }
3267
3268 void tst_QQuickGridView::header()
3269 {
3270     QFETCH(QQuickGridView::Flow, flow);
3271     QFETCH(Qt::LayoutDirection, layoutDirection);
3272     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3273     QFETCH(QPointF, initialHeaderPos);
3274     QFETCH(QPointF, changedHeaderPos);
3275     QFETCH(QPointF, initialContentPos);
3276     QFETCH(QPointF, changedContentPos);
3277     QFETCH(QPointF, firstDelegatePos);
3278     QFETCH(QPointF, resizeContentPos);
3279
3280     QaimModel model;
3281     for (int i = 0; i < 30; i++)
3282         model.addItem("Item" + QString::number(i), "");
3283
3284     QQuickView *canvas = getView();
3285     canvas->rootContext()->setContextProperty("testModel", &model);
3286     canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3287     canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3288     canvas->setSource(testFileUrl("header.qml"));
3289     canvas->show();
3290     qApp->processEvents();
3291
3292     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3293     QTRY_VERIFY(gridview != 0);
3294     gridview->setFlow(flow);
3295     gridview->setLayoutDirection(layoutDirection);
3296     gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3297     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3298
3299     QQuickItem *contentItem = gridview->contentItem();
3300     QTRY_VERIFY(contentItem != 0);
3301
3302     QQuickText *header = findItem<QQuickText>(contentItem, "header");
3303     QVERIFY(header);
3304     QVERIFY(header == gridview->headerItem());
3305
3306     QCOMPARE(header->pos(), initialHeaderPos);
3307     QCOMPARE(header->width(), 100.);
3308     QCOMPARE(header->height(), 30.);
3309     QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3310
3311     if (flow == QQuickGridView::FlowLeftToRight)
3312         QCOMPARE(gridview->contentHeight(), (model.count()+2) / 3 * 60. + header->height());
3313     else
3314         QCOMPARE(gridview->contentWidth(), (model.count()+3) / 5 * 80. + header->width());
3315
3316     QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3317     QVERIFY(item);
3318     QCOMPARE(item->pos(), firstDelegatePos);
3319
3320     model.clear();
3321     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3322     QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
3323
3324     for (int i = 0; i < 30; i++)
3325         model.addItem("Item" + QString::number(i), "");
3326
3327     QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
3328     QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
3329
3330     QCOMPARE(headerItemSpy.count(), 1);
3331
3332     header = findItem<QQuickText>(contentItem, "header");
3333     QVERIFY(!header);
3334     header = findItem<QQuickText>(contentItem, "header2");
3335     QVERIFY(header);
3336
3337     QVERIFY(header == gridview->headerItem());
3338
3339     QCOMPARE(header->pos(), changedHeaderPos);
3340     QCOMPARE(header->width(), 50.);
3341     QCOMPARE(header->height(), 20.);
3342     QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
3343
3344     item = findItem<QQuickItem>(contentItem, "wrapper", 0);
3345     QVERIFY(item);
3346     QCOMPARE(item->pos(), firstDelegatePos);
3347
3348     header->setHeight(10);
3349     header->setWidth(40);
3350     QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), resizeContentPos);
3351
3352     releaseView(canvas);
3353
3354
3355     // QTBUG-21207 header should become visible if view resizes from initial empty size
3356
3357     canvas = getView();
3358     canvas->rootContext()->setContextProperty("testModel", &model);
3359     canvas->rootContext()->setContextProperty("initialViewWidth", 240);
3360     canvas->rootContext()->setContextProperty("initialViewHeight", 320);
3361     canvas->setSource(testFileUrl("header.qml"));
3362     canvas->show();
3363     qApp->processEvents();
3364
3365     gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3366     QTRY_VERIFY(gridview != 0);
3367     gridview->setFlow(flow);
3368     gridview->setLayoutDirection(layoutDirection);
3369     gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3370     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3371
3372     gridview->setWidth(240);
3373     gridview->setHeight(320);
3374     QTRY_COMPARE(gridview->headerItem()->pos(), initialHeaderPos);
3375     QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
3376
3377     releaseView(canvas);
3378 }
3379
3380 void tst_QQuickGridView::header_data()
3381 {
3382     QTest::addColumn<QQuickGridView::Flow>("flow");
3383     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3384     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3385     QTest::addColumn<QPointF>("initialHeaderPos");
3386     QTest::addColumn<QPointF>("changedHeaderPos");
3387     QTest::addColumn<QPointF>("initialContentPos");
3388     QTest::addColumn<QPointF>("changedContentPos");
3389     QTest::addColumn<QPointF>("firstDelegatePos");
3390     QTest::addColumn<QPointF>("resizeContentPos");
3391
3392     // header1 = 100 x 30
3393     // header2 = 50 x 20
3394     // cells = 80 x 60
3395     // view width = 240
3396
3397     // header above items, top left
3398     QTest::newRow("LeftToRight, LtR, TtB")
3399         << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3400         << QPointF(0, -30)
3401         << QPointF(0, -20)
3402         << QPointF(0, -30)
3403         << QPointF(0, -20)
3404         << QPointF(0, 0)
3405         << QPointF(0, -10);
3406
3407     // header above items, top right
3408     QTest::newRow("LeftToRight, RtL, TtB")
3409         << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3410         << QPointF(240 - 100, -30)
3411         << QPointF((240 - 100) + 50, -20)     // 50 = width diff between old and new headers
3412         << QPointF(0, -30)
3413         << QPointF(0, -20)
3414         << QPointF(160, 0)
3415         << QPointF(0, -10);
3416
3417     // header below items, bottom left
3418     QTest::newRow("LeftToRight, LtR, BtT")
3419         << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3420         << QPointF(0, 0)
3421         << QPointF(0, 0)
3422         << QPointF(0, -320 + 30)
3423         << QPointF(0, -320 + 20)
3424         << QPointF(0, -60)
3425         << QPointF(0, -320 + 10);
3426
3427     // header above items, top right
3428     QTest::newRow("LeftToRight, RtL, BtT")
3429         << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3430         << QPointF(240 - 100, 0)
3431         << QPointF((240 - 100) + 50, 0)
3432         << QPointF(0, -320 + 30)
3433         << QPointF(0, -320 + 20)
3434         << QPointF(160, -60)
3435         << QPointF(0, -320 + 10);
3436
3437
3438     // header to left of items, bottom left
3439     QTest::newRow("TopToBottom, LtR, TtB")
3440         << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3441         << QPointF(-100, 0)
3442         << QPointF(-50, 0)
3443         << QPointF(-100, 0)
3444         << QPointF(-50, 0)
3445         << QPointF(0, 0)
3446         << QPointF(-40, 0);
3447
3448     // header to right of items, bottom right
3449     QTest::newRow("TopToBottom, RtL, TtB")
3450         << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3451         << QPointF(0, 0)
3452         << QPointF(0, 0)
3453         << QPointF(-(240 - 100), 0)
3454         << QPointF(-(240 - 50), 0)
3455         << QPointF(-80, 0)
3456         << QPointF(-(240 - 40), 0);
3457
3458     // header to left of items, top left
3459     QTest::newRow("TopToBottom, LtR, BtT")
3460         << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3461         << QPointF(-100, -30)
3462         << QPointF(-50, -20)
3463         << QPointF(-100, -320)
3464         << QPointF(-50, -320)
3465         << QPointF(0, -60)
3466         << QPointF(-40, -320);
3467
3468     // header to right of items, top right
3469     QTest::newRow("TopToBottom, RtL, BtT")
3470         << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3471         << QPointF(0, -30)
3472         << QPointF(0, -20)
3473         << QPointF(-(240 - 100), -320)
3474         << QPointF(-(240 - 50), -320)
3475         << QPointF(-80, -60)
3476         << QPointF(-(240 - 40), -320);
3477 }
3478
3479 class GVAccessor : public QQuickGridView
3480 {
3481 public:
3482     qreal minY() const { return minYExtent(); }
3483     qreal maxY() const { return maxYExtent(); }
3484     qreal minX() const { return minXExtent(); }
3485     qreal maxX() const { return maxXExtent(); }
3486 };
3487
3488 void tst_QQuickGridView::headerFooter()
3489 {
3490     QFETCH(QQuickGridView::Flow, flow);
3491     QFETCH(Qt::LayoutDirection, layoutDirection);
3492     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3493     QFETCH(QPointF, headerPos);
3494     QFETCH(QPointF, footerPos);
3495     QFETCH(QPointF, minPos);
3496     QFETCH(QPointF, maxPos);
3497
3498     QQuickView *canvas = getView();
3499
3500     QmlListModel model;
3501     QQmlContext *ctxt = canvas->rootContext();
3502     ctxt->setContextProperty("testModel", &model);
3503     canvas->setSource(testFileUrl("headerfooter.qml"));
3504     canvas->show();
3505     qApp->processEvents();
3506
3507     QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3508     QTRY_VERIFY(gridview != 0);
3509     gridview->setFlow(flow);
3510     gridview->setLayoutDirection(layoutDirection);
3511     gridview->setVerticalLayoutDirection(verticalLayoutDirection);
3512     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3513
3514     QQuickItem *contentItem = gridview->contentItem();
3515     QTRY_VERIFY(contentItem != 0);
3516
3517     QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3518     QVERIFY(header);
3519     QCOMPARE(header->pos(), headerPos);
3520
3521     QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3522     QVERIFY(footer);
3523     QCOMPARE(footer->pos(), footerPos);
3524
3525     QCOMPARE(static_cast<GVAccessor*>(gridview)->minX(), minPos.x());
3526     QCOMPARE(static_cast<GVAccessor*>(gridview)->minY(), minPos.y());
3527     QCOMPARE(static_cast<GVAccessor*>(gridview)->maxX(), maxPos.x());
3528     QCOMPARE(static_cast<GVAccessor*>(gridview)->maxY(), maxPos.y());
3529
3530     releaseView(canvas);
3531 }
3532
3533 void tst_QQuickGridView::headerFooter_data()
3534 {
3535     QTest::addColumn<QQuickGridView::Flow>("flow");
3536     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3537     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3538     QTest::addColumn<QPointF>("headerPos");
3539     QTest::addColumn<QPointF>("footerPos");
3540     QTest::addColumn<QPointF>("minPos");
3541     QTest::addColumn<QPointF>("maxPos");
3542
3543     // header is 240x20 (or 20x320 in TopToBottom)
3544     // footer is 240x30 (or 30x320 in TopToBottom)
3545
3546     QTest::newRow("LeftToRight, LtR, TtB")
3547             << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3548             << QPointF(0, -20) << QPointF(0, 0)
3549             << QPointF(0, 20) << QPointF(240, 20);
3550
3551     QTest::newRow("LeftToRight, RtL, TtB")
3552             << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3553             << QPointF(0, -20) << QPointF(0, 0)
3554             << QPointF(0, 20) << QPointF(240, 20);
3555
3556     QTest::newRow("LeftToRight, LtR, BtT")
3557             << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3558             << QPointF(0, 0) << QPointF(0, -30)
3559             << QPointF(0, 320 - 20) << QPointF(240, 320 - 20);  // content flow is reversed
3560
3561     QTest::newRow("LeftToRight, RtL, BtT")
3562             << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3563             << QPointF(0, 0) << QPointF(0, -30)
3564             << QPointF(0, 320 - 20) << QPointF(240, 320 - 20);  // content flow is reversed
3565
3566
3567     QTest::newRow("TopToBottom, LtR, TtB")
3568             << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3569             << QPointF(-20, 0) << QPointF(0, 0)
3570             << QPointF(20, 0) << QPointF(20, 320);
3571
3572     QTest::newRow("TopToBottom, RtL, TtB")
3573             << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3574             << QPointF(0, 0) << QPointF(-30, 0)
3575             << QPointF(240 - 20, 0) << QPointF(240 - 20, 320);  // content flow is reversed
3576
3577     QTest::newRow("TopToBottom, LtR, BtT")
3578             << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3579             << QPointF(-20, -320) << QPointF(0, -320)
3580             << QPointF(20, 0) << QPointF(20, 320);
3581
3582     QTest::newRow("TopToBottom, RtL, BtT")
3583             << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3584             << QPointF(0, -320) << QPointF(-30, -320)
3585             << QPointF(240 - 20, 0) << QPointF(240 - 20, 320);  // content flow is reversed
3586 }
3587
3588 void tst_QQuickGridView::resetModel_headerFooter()
3589 {
3590     // Resetting a model shouldn't crash in views with header/footer
3591
3592     QQuickView *canvas = createView();
3593
3594     QaimModel model;
3595     for (int i = 0; i < 6; i++)
3596         model.addItem("Item" + QString::number(i), "");
3597     QQmlContext *ctxt = canvas->rootContext();
3598     ctxt->setContextProperty("testModel", &model);
3599
3600     canvas->setSource(testFileUrl("headerfooter.qml"));
3601     qApp->processEvents();
3602
3603     QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
3604     QTRY_VERIFY(gridview != 0);
3605
3606     QQuickItem *contentItem = gridview->contentItem();
3607     QTRY_VERIFY(contentItem != 0);
3608
3609     QQuickItem *header = findItem<QQuickItem>(contentItem, "header");
3610     QVERIFY(header);
3611     QCOMPARE(header->y(), -header->height());
3612
3613     QQuickItem *footer = findItem<QQuickItem>(contentItem, "footer");
3614     QVERIFY(footer);
3615     QCOMPARE(footer->y(), 80.*2);
3616
3617     model.reset();
3618
3619     header = findItem<QQuickItem>(contentItem, "header");
3620     QVERIFY(header);
3621     QCOMPARE(header->y(), -header->height());
3622
3623     footer = findItem<QQuickItem>(contentItem, "footer");
3624     QVERIFY(footer);
3625     QCOMPARE(footer->y(), 80.*2);
3626
3627     delete canvas;
3628 }
3629
3630 void tst_QQuickGridView::resizeViewAndRepaint()
3631 {
3632     QQuickView *canvas = createView();
3633     canvas->show();
3634
3635     QaimModel model;
3636     for (int i = 0; i < 40; i++)
3637         model.addItem("Item" + QString::number(i), "");
3638
3639     QQmlContext *ctxt = canvas->rootContext();
3640     ctxt->setContextProperty("testModel", &model);
3641     ctxt->setContextProperty("initialWidth", 240);
3642     ctxt->setContextProperty("initialHeight", 100);
3643
3644     canvas->setSource(testFileUrl("resizeview.qml"));
3645     canvas->show();
3646     qApp->processEvents();
3647
3648     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3649     QTRY_VERIFY(gridview != 0);
3650     QQuickItem *contentItem = gridview->contentItem();
3651     QTRY_VERIFY(contentItem != 0);
3652     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3653
3654     // item at index 10 should not be currently visible
3655     QVERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3656
3657     gridview->setHeight(320);
3658     QTRY_VERIFY(findItem<QQuickItem>(contentItem, "wrapper", 10));
3659
3660     gridview->setHeight(100);
3661     QTRY_VERIFY(!findItem<QQuickItem>(contentItem, "wrapper", 10));
3662
3663     // Ensure we handle -ve sizes
3664     gridview->setHeight(-100);
3665     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 3);
3666
3667     gridview->setCacheBuffer(120);
3668     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 9);
3669
3670     // ensure items in cache become visible
3671     gridview->setHeight(120);
3672     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 15);
3673
3674     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3675     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3676         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3677         if (!item) qWarning() << "Item" << i << "not found";
3678         QTRY_VERIFY(item);
3679         QTRY_COMPARE(item->x(), qreal((i%3)*80));
3680         QTRY_COMPARE(item->y(), qreal((i/3)*60));
3681         QCOMPARE(item->isVisible(), i < 9); // inside view visible, outside not visible
3682     }
3683
3684     // ensure items outside view become invisible
3685     gridview->setHeight(60);
3686     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper", false).count(), 12);
3687
3688     itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
3689     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3690         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3691         if (!item) qWarning() << "Item" << i << "not found";
3692         QTRY_VERIFY(item);
3693         QTRY_COMPARE(item->x(), qreal((i%3)*80));
3694         QTRY_COMPARE(item->y(), qreal((i/3)*60));
3695         QCOMPARE(item->isVisible(), i < 6); // inside view visible, outside not visible
3696     }
3697
3698     delete canvas;
3699 }
3700
3701 void tst_QQuickGridView::resizeGrid()
3702 {
3703     QFETCH(QQuickGridView::Flow, flow);
3704     QFETCH(Qt::LayoutDirection, layoutDirection);
3705     QFETCH(QQuickItemView::VerticalLayoutDirection, verticalLayoutDirection);
3706     QFETCH(QPointF, initialContentPos);
3707     QFETCH(QPointF, firstItemPos);
3708
3709     QaimModel model;
3710     for (int i = 0; i < 30; i++)
3711         model.addItem("Item" + QString::number(i), "");
3712
3713     QQuickView *canvas = getView();
3714     QQmlContext *ctxt = canvas->rootContext();
3715     ctxt->setContextProperty("testModel", &model);
3716     ctxt->setContextProperty("testTopToBottom", flow == QQuickGridView::FlowTopToBottom);
3717     ctxt->setContextProperty("testRightToLeft", layoutDirection == Qt::RightToLeft);
3718     ctxt->setContextProperty("testBottomToTop", verticalLayoutDirection == QQuickGridView::BottomToTop);
3719     canvas->setSource(testFileUrl("resizegrid.qml"));
3720     canvas->show();
3721     qApp->processEvents();
3722
3723     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3724     QTRY_VERIFY(gridview != 0);
3725     QQuickItem *contentItem = gridview->contentItem();
3726     QTRY_VERIFY(contentItem != 0);
3727
3728     // set the width to slightly larger than 3 items across, to test
3729     // items are aligned correctly in right-to-left
3730     canvas->rootObject()->setWidth(260);
3731     canvas->rootObject()->setHeight(320);
3732     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3733
3734     QCOMPARE(gridview->contentX(), initialContentPos.x());
3735     QCOMPARE(gridview->contentY(), initialContentPos.y());
3736
3737     QQuickItem *item0 = findItem<QQuickItem>(contentItem, "wrapper", 0);
3738     QVERIFY(item0);
3739     QCOMPARE(item0->pos(), firstItemPos);
3740
3741     // Confirm items positioned correctly and indexes correct
3742     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
3743     QVERIFY(items.count() >= 18 && items.count() <= 21);
3744     for (int i = 0; i < model.count() && i < items.count(); ++i) {
3745         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3746         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3747         QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3748         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3749         QVERIFY(name != 0);
3750         QCOMPARE(name->text(), model.name(i));
3751     }
3752
3753     // change from 3x5 grid to 4x7
3754     canvas->rootObject()->setWidth(canvas->rootObject()->width() + 80);
3755     canvas->rootObject()->setHeight(canvas->rootObject()->height() + 60*2);
3756     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3757
3758     // other than in LeftToRight+RightToLeft layout, the first item should not move
3759     // if view is resized
3760     QCOMPARE(findItem<QQuickItem>(contentItem, "wrapper", 0), item0);
3761     if (flow == QQuickGridView::FlowLeftToRight && layoutDirection == Qt::RightToLeft)
3762         firstItemPos.rx() += 80;
3763     QCOMPARE(item0->pos(), firstItemPos);
3764
3765     QPointF newContentPos = initialContentPos;
3766     if (flow == QQuickGridView::FlowTopToBottom && layoutDirection == Qt::RightToLeft)
3767         newContentPos.rx() -= 80.0;
3768     if (verticalLayoutDirection == QQuickItemView::BottomToTop)
3769         newContentPos.ry() -= 60.0 * 2;
3770     QCOMPARE(gridview->contentX(), newContentPos.x());
3771     QCOMPARE(gridview->contentY(), newContentPos.y());
3772
3773     // Confirm items positioned correctly and indexes correct
3774     items = findItems<QQuickItem>(contentItem, "wrapper");
3775     QVERIFY(items.count() >= 28);
3776     for (int i = 0; i < model.count() && i < items.count(); ++i) {
3777         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3778         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3779         QCOMPARE(item->pos(), expectedItemPos(gridview, i, 0));
3780         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
3781         QVERIFY(name != 0);
3782         QCOMPARE(name->text(), model.name(i));
3783     }
3784
3785     releaseView(canvas);
3786 }
3787
3788 void tst_QQuickGridView::resizeGrid_data()
3789 {
3790     QTest::addColumn<QQuickGridView::Flow>("flow");
3791     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
3792     QTest::addColumn<QQuickItemView::VerticalLayoutDirection>("verticalLayoutDirection");
3793     QTest::addColumn<QPointF>("initialContentPos");
3794     QTest::addColumn<QPointF>("firstItemPos");
3795
3796     // Initial view width is 260, so in LeftToRight + right-to-left mode the
3797     // content x should be -20
3798
3799     QTest::newRow("LeftToRight, LtR, TtB")
3800             << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::TopToBottom
3801             << QPointF(0, 0)
3802             << QPointF(0, 0);
3803
3804     QTest::newRow("LeftToRight, RtL, TtB")
3805             << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::TopToBottom
3806             << QPointF(-20.0, 0)
3807             << QPointF(80.0 * 2, 0);
3808
3809     QTest::newRow("LeftToRight, LtR, BtT")
3810             << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << QQuickItemView::BottomToTop
3811             << QPointF(0, -320)
3812             << QPointF(0, -60.0);
3813
3814     QTest::newRow("LeftToRight, RtL, BtT")
3815             << QQuickGridView::FlowLeftToRight << Qt::RightToLeft << QQuickItemView::BottomToTop
3816             << QPointF(-20.0, -320)
3817             << QPointF(80.0 * 2, -60.0);
3818
3819
3820     QTest::newRow("TopToBottom, LtR, TtB")
3821             << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::TopToBottom
3822             << QPointF(0, 0)
3823             << QPointF(0, 0);
3824
3825     QTest::newRow("TopToBottom, RtL, TtB")
3826             << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::TopToBottom
3827             << QPointF(-260, 0)
3828             << QPointF(-80.0, 0);
3829
3830     QTest::newRow("TopToBottom, LtR, BtT")
3831             << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << QQuickItemView::BottomToTop
3832             << QPointF(0, -320)
3833             << QPointF(0, -60.0);
3834
3835     QTest::newRow("TopToBottom, RtL, BtT")
3836             << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << QQuickItemView::BottomToTop
3837             << QPointF(-260, -320)
3838             << QPointF(-80.0, -60.0);
3839 }
3840
3841
3842 void tst_QQuickGridView::changeColumnCount()
3843 {
3844     QmlListModel model;
3845     for (int i = 0; i < 40; i++)
3846         model.addItem("Item" + QString::number(i), "");
3847
3848     QQuickView *canvas = createView();
3849     QQmlContext *ctxt = canvas->rootContext();
3850     ctxt->setContextProperty("testModel", &model);
3851     ctxt->setContextProperty("initialWidth", 100);
3852     ctxt->setContextProperty("initialHeight", 320);
3853     canvas->setSource(testFileUrl("resizeview.qml"));
3854     canvas->show();
3855     qApp->processEvents();
3856
3857     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3858     QTRY_VERIFY(gridview != 0);
3859     QQuickItem *contentItem = gridview->contentItem();
3860     QTRY_VERIFY(contentItem != 0);
3861     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3862
3863     // a single column of 6 items are visible
3864     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3865     QCOMPARE(itemCount, 6);
3866     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3867         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3868         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3869         QCOMPARE(item->x(), 0.0);
3870         QCOMPARE(item->y(), qreal(i*60));
3871     }
3872
3873     // now 6x3 grid is visible, plus 1 extra below for refill
3874     gridview->setWidth(240);
3875     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3876     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3877     QCOMPARE(itemCount, 6*3 + 1);
3878     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3879         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3880         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3881         QCOMPARE(item->x(), qreal((i%3)*80));
3882         QCOMPARE(item->y(), qreal((i/3)*60));
3883     }
3884
3885     // back to single column
3886     gridview->setWidth(100);
3887     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
3888     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
3889     QCOMPARE(itemCount, 6);
3890     for (int i = 0; i < model.count() && i < itemCount; ++i) {
3891         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
3892         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
3893         QCOMPARE(item->x(), 0.0);
3894         QCOMPARE(item->y(), qreal(i*60));
3895     }
3896
3897     delete canvas;
3898 }
3899
3900 void tst_QQuickGridView::indexAt_itemAt_data()
3901 {
3902     QTest::addColumn<qreal>("x");
3903     QTest::addColumn<qreal>("y");
3904     QTest::addColumn<int>("index");
3905
3906     QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
3907     QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
3908     QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
3909     QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
3910     QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
3911 }
3912
3913 void tst_QQuickGridView::indexAt_itemAt()
3914 {
3915     QFETCH(qreal, x);
3916     QFETCH(qreal, y);
3917     QFETCH(int, index);
3918
3919     QQuickView *canvas = getView();
3920
3921     QaimModel model;
3922     model.addItem("Fred", "12345");
3923     model.addItem("John", "2345");
3924     model.addItem("Bob", "54321");
3925     model.addItem("Billy", "22345");
3926     model.addItem("Sam", "2945");
3927     model.addItem("Ben", "04321");
3928     model.addItem("Jim", "0780");
3929
3930     canvas->rootContext()->setContextProperty("testModel", &model);
3931     canvas->setSource(testFileUrl("gridview1.qml"));
3932     qApp->processEvents();
3933
3934     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
3935     QTRY_VERIFY(gridview != 0);
3936
3937     QQuickItem *contentItem = gridview->contentItem();
3938     QTRY_VERIFY(contentItem != 0);
3939
3940     QTRY_COMPARE(gridview->count(), model.count());
3941
3942     QQuickItem *item = 0;
3943     if (index >= 0) {
3944         item = findItem<QQuickItem>(contentItem, "wrapper", index);
3945         QVERIFY(item);
3946     }
3947     QCOMPARE(gridview->indexAt(x, y), index);
3948     QVERIFY(gridview->itemAt(x, y) == item);
3949
3950     releaseView(canvas);
3951 }
3952
3953 void tst_QQuickGridView::onAdd()
3954 {
3955     QFETCH(int, initialItemCount);
3956     QFETCH(int, itemsToAdd);
3957
3958     const int delegateWidth = 50;
3959     const int delegateHeight = 100;
3960     QaimModel model;
3961     QQuickView *canvas = getView();
3962     canvas->setGeometry(0,0,5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
3963
3964     // these initial items should not trigger GridView.onAdd
3965     for (int i=0; i<initialItemCount; i++)
3966         model.addItem("dummy value", "dummy value");
3967
3968     QQmlContext *ctxt = canvas->rootContext();
3969     ctxt->setContextProperty("testModel", &model);
3970     ctxt->setContextProperty("delegateWidth", delegateWidth);
3971     ctxt->setContextProperty("delegateHeight", delegateHeight);
3972     canvas->setSource(testFileUrl("attachedSignals.qml"));
3973
3974     QObject *object = canvas->rootObject();
3975     object->setProperty("width", canvas->width());
3976     object->setProperty("height", canvas->height());
3977     qApp->processEvents();
3978
3979     QList<QPair<QString, QString> > items;
3980     for (int i=0; i<itemsToAdd; i++)
3981         items << qMakePair(QString("value %1").arg(i), QString::number(i));
3982     model.addItems(items);
3983
3984     QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
3985     qApp->processEvents();
3986
3987     QVariantList result = object->property("addedDelegates").toList();
3988     QTRY_COMPARE(result.count(), items.count());
3989     for (int i=0; i<items.count(); i++)
3990         QCOMPARE(result[i].toString(), items[i].first);
3991
3992     releaseView(canvas);
3993 }
3994
3995 void tst_QQuickGridView::onAdd_data()
3996 {
3997     QTest::addColumn<int>("initialItemCount");
3998     QTest::addColumn<int>("itemsToAdd");
3999
4000     QTest::newRow("0, add 1") << 0 << 1;
4001     QTest::newRow("0, add 2") << 0 << 2;
4002     QTest::newRow("0, add 10") << 0 << 10;
4003
4004     QTest::newRow("1, add 1") << 1 << 1;
4005     QTest::newRow("1, add 2") << 1 << 2;
4006     QTest::newRow("1, add 10") << 1 << 10;
4007
4008     QTest::newRow("5, add 1") << 5 << 1;
4009     QTest::newRow("5, add 2") << 5 << 2;
4010     QTest::newRow("5, add 10") << 5 << 10;
4011 }
4012
4013 void tst_QQuickGridView::onRemove()
4014 {
4015     QFETCH(int, initialItemCount);
4016     QFETCH(int, indexToRemove);
4017     QFETCH(int, removeCount);
4018
4019     const int delegateWidth = 50;
4020     const int delegateHeight = 100;
4021     QaimModel model;
4022     for (int i=0; i<initialItemCount; i++)
4023         model.addItem(QString("value %1").arg(i), "dummy value");
4024
4025     QQuickView *canvas = getView();
4026     QQmlContext *ctxt = canvas->rootContext();
4027     ctxt->setContextProperty("testModel", &model);
4028     ctxt->setContextProperty("delegateWidth", delegateWidth);
4029     ctxt->setContextProperty("delegateHeight", delegateHeight);
4030     canvas->setSource(testFileUrl("attachedSignals.qml"));
4031     QObject *object = canvas->rootObject();
4032
4033     model.removeItems(indexToRemove, removeCount);
4034     QTRY_COMPARE(model.count(), qobject_cast<QQuickGridView*>(canvas->rootObject())->count());
4035     QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
4036
4037     releaseView(canvas);
4038 }
4039
4040 void tst_QQuickGridView::onRemove_data()
4041 {
4042     QTest::addColumn<int>("initialItemCount");
4043     QTest::addColumn<int>("indexToRemove");
4044     QTest::addColumn<int>("removeCount");
4045
4046     QTest::newRow("remove first") << 1 << 0 << 1;
4047     QTest::newRow("two items, remove first") << 2 << 0 << 1;
4048     QTest::newRow("two items, remove last") << 2 << 1 << 1;
4049     QTest::newRow("two items, remove all") << 2 << 0 << 2;
4050
4051     QTest::newRow("four items, remove first") << 4 << 0 << 1;
4052     QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
4053     QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
4054     QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
4055     QTest::newRow("four items, remove last") << 4 << 3 << 1;
4056     QTest::newRow("four items, remove all") << 4 << 0 << 4;
4057
4058     QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
4059     QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
4060     QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
4061 }
4062
4063 void tst_QQuickGridView::columnCount()
4064 {
4065     QQuickView canvas;
4066     canvas.setSource(testFileUrl("gridview4.qml"));
4067     canvas.show();
4068     canvas.requestActivateWindow();
4069     QTest::qWaitForWindowShown(&canvas);
4070
4071     QQuickGridView *view = qobject_cast<QQuickGridView*>(canvas.rootObject());
4072
4073     QCOMPARE(view->cellWidth(), qreal(405)/qreal(9));
4074     QCOMPARE(view->cellHeight(), qreal(100));
4075
4076     QList<QQuickItem*> items = findItems<QQuickItem>(view, "delegate");
4077     QCOMPARE(items.size(), 18);
4078     QCOMPARE(items.at(8)->y(), qreal(0));
4079     QCOMPARE(items.at(9)->y(), qreal(100));
4080 }
4081
4082 void tst_QQuickGridView::margins()
4083 {
4084     {
4085         QQuickView *canvas = createView();
4086
4087         QaimModel model;
4088         for (int i = 0; i < 40; i++)
4089             model.addItem("Item" + QString::number(i), "");
4090
4091         QQmlContext *ctxt = canvas->rootContext();
4092         ctxt->setContextProperty("testModel", &model);
4093         ctxt->setContextProperty("testRightToLeft", QVariant(false));
4094
4095         canvas->setSource(testFileUrl("margins.qml"));
4096         canvas->show();
4097         qApp->processEvents();
4098
4099         QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4100         QTRY_VERIFY(gridview != 0);
4101         QQuickItem *contentItem = gridview->contentItem();
4102         QTRY_VERIFY(contentItem != 0);
4103         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4104
4105         QCOMPARE(gridview->contentX(), -30.);
4106         QCOMPARE(gridview->xOrigin(), 0.);
4107
4108         // check end bound
4109         gridview->positionViewAtEnd();
4110         qreal pos = gridview->contentX();
4111         gridview->setContentX(pos + 80);
4112         gridview->returnToBounds();
4113         QTRY_COMPARE(gridview->contentX(), pos + 50);
4114
4115         // remove item before visible and check that left margin is maintained
4116         // and xOrigin is updated
4117         gridview->setContentX(200);
4118         model.removeItems(0, 4);
4119         QTest::qWait(100);
4120         gridview->setContentX(-50);
4121         gridview->returnToBounds();
4122         QCOMPARE(gridview->xOrigin(), 100.);
4123         QTRY_COMPARE(gridview->contentX(), 70.);
4124
4125         // reduce left margin
4126         gridview->setLeftMargin(20);
4127         QCOMPARE(gridview->xOrigin(), 100.);
4128         QTRY_COMPARE(gridview->contentX(), 80.);
4129
4130         // check end bound
4131         gridview->positionViewAtEnd();
4132         QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
4133         pos = gridview->contentX();
4134         gridview->setContentX(pos + 80);
4135         gridview->returnToBounds();
4136         QTRY_COMPARE(gridview->contentX(), pos + 50);
4137
4138         // reduce right margin
4139         pos = gridview->contentX();
4140         gridview->setRightMargin(40);
4141         QCOMPARE(gridview->xOrigin(), 0.);
4142         QTRY_COMPARE(gridview->contentX(), pos-10);
4143
4144         delete canvas;
4145     }
4146     {
4147         //RTL
4148         QQuickView *canvas = createView();
4149         canvas->show();
4150
4151         QaimModel model;
4152         for (int i = 0; i < 40; i++)
4153             model.addItem("Item" + QString::number(i), "");
4154
4155         QQmlContext *ctxt = canvas->rootContext();
4156         ctxt->setContextProperty("testModel", &model);
4157         ctxt->setContextProperty("testRightToLeft", QVariant(true));
4158
4159         canvas->setSource(testFileUrl("margins.qml"));
4160         qApp->processEvents();
4161
4162         QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4163         QTRY_VERIFY(gridview != 0);
4164
4165         QQuickItem *contentItem = gridview->contentItem();
4166         QTRY_VERIFY(contentItem != 0);
4167
4168         QTRY_COMPARE(gridview->contentX(), -240+50.);
4169         QTRY_COMPARE(gridview->xOrigin(), 0.);
4170
4171         // check end bound
4172         gridview->positionViewAtEnd();
4173         qreal pos = gridview->contentX();
4174         gridview->setContentX(pos - 80);
4175         gridview->returnToBounds();
4176         QTRY_COMPARE(gridview->contentX(), pos - 30);
4177
4178         // remove item before visible and check that left margin is maintained
4179         // and xOrigin is updated
4180         gridview->setContentX(-400);
4181         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4182         model.removeItems(0, 4);
4183         QTRY_COMPARE(model.count(), gridview->count());
4184         gridview->setContentX(-240+50);
4185         gridview->returnToBounds();
4186         QCOMPARE(gridview->xOrigin(), -100.);
4187         QTRY_COMPARE(gridview->contentX(), -240-50.);
4188
4189         // reduce right margin
4190         pos = gridview->contentX();
4191         gridview->setRightMargin(40);
4192         QCOMPARE(gridview->xOrigin(), -100.);
4193         QTRY_COMPARE(gridview->contentX(), -240-100 + 40.);
4194
4195         // check end bound
4196         gridview->positionViewAtEnd();
4197         QCOMPARE(gridview->xOrigin(), 0.); // positionViewAtEnd() resets origin
4198         pos = gridview->contentX();
4199         gridview->setContentX(pos - 80);
4200         gridview->returnToBounds();
4201         QTRY_COMPARE(gridview->contentX(), pos - 30);
4202
4203         // reduce left margin
4204         pos = gridview->contentX();
4205         gridview->setLeftMargin(20);
4206         QCOMPARE(gridview->xOrigin(), 0.);
4207         QTRY_COMPARE(gridview->contentX(), pos+10);
4208
4209         delete canvas;
4210     }
4211 }
4212
4213 void tst_QQuickGridView::creationContext()
4214 {
4215     QQuickView canvas;
4216     canvas.setGeometry(0,0,240,320);
4217     canvas.setSource(testFileUrl("creationContext.qml"));
4218     qApp->processEvents();
4219
4220     QQuickItem *rootItem = qobject_cast<QQuickItem *>(canvas.rootObject());
4221     QVERIFY(rootItem);
4222     QVERIFY(rootItem->property("count").toInt() > 0);
4223
4224     QQuickItem *item;
4225     QVERIFY(item = rootItem->findChild<QQuickItem *>("listItem"));
4226     QCOMPARE(item->property("text").toString(), QString("Hello!"));
4227     QVERIFY(item = rootItem->findChild<QQuickItem *>("header"));
4228     QCOMPARE(item->property("text").toString(), QString("Hello!"));
4229     QVERIFY(item = rootItem->findChild<QQuickItem *>("footer"));
4230     QCOMPARE(item->property("text").toString(), QString("Hello!"));
4231 }
4232
4233 void tst_QQuickGridView::snapToRow_data()
4234 {
4235     QTest::addColumn<QQuickGridView::Flow>("flow");
4236     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4237     QTest::addColumn<int>("highlightRangeMode");
4238     QTest::addColumn<QPoint>("flickStart");
4239     QTest::addColumn<QPoint>("flickEnd");
4240     QTest::addColumn<qreal>("snapAlignment");
4241     QTest::addColumn<qreal>("endExtent");
4242     QTest::addColumn<qreal>("startExtent");
4243
4244     QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4245         << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4246
4247     QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4248         << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 800.0 << 0.0;
4249
4250     QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4251         << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 << -240.0;
4252
4253     QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4254         << QPoint(20, 200) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4255
4256     QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4257         << QPoint(200, 20) << QPoint(20, 20) << 60.0 << 940.0 << -20.0;
4258
4259     QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4260         << QPoint(20, 20) << QPoint(200, 20) << -60.0 << -800.0 - 240.0 - 140.0 << -220.0;
4261 }
4262
4263 void tst_QQuickGridView::snapToRow()
4264 {
4265     QFETCH(QQuickGridView::Flow, flow);
4266     QFETCH(Qt::LayoutDirection, layoutDirection);
4267     QFETCH(int, highlightRangeMode);
4268     QFETCH(QPoint, flickStart);
4269     QFETCH(QPoint, flickEnd);
4270     QFETCH(qreal, snapAlignment);
4271     QFETCH(qreal, endExtent);
4272     QFETCH(qreal, startExtent);
4273
4274     QQuickView *canvas = getView();
4275
4276     canvas->setSource(testFileUrl("snapToRow.qml"));
4277     canvas->show();
4278     qApp->processEvents();
4279
4280     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4281     QTRY_VERIFY(gridview != 0);
4282
4283     gridview->setFlow(flow);
4284     gridview->setLayoutDirection(layoutDirection);
4285     gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4286     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4287
4288     QQuickItem *contentItem = gridview->contentItem();
4289     QTRY_VERIFY(contentItem != 0);
4290
4291     // confirm that a flick hits an item boundary
4292     flick(canvas, flickStart, flickEnd, 180);
4293     QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4294     if (flow == QQuickGridView::FlowLeftToRight)
4295         QCOMPARE(qreal(fmod(gridview->contentY(),80.0)), snapAlignment);
4296     else
4297         QCOMPARE(qreal(fmod(gridview->contentX(),80.0)), snapAlignment);
4298
4299     // flick to end
4300     do {
4301         flick(canvas, flickStart, flickEnd, 180);
4302         QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4303     } while (flow == QQuickGridView::FlowLeftToRight
4304            ? !gridview->isAtYEnd()
4305            : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4306
4307     if (flow == QQuickGridView::FlowLeftToRight)
4308         QCOMPARE(gridview->contentY(), endExtent);
4309     else
4310         QCOMPARE(gridview->contentX(), endExtent);
4311
4312     // flick to start
4313     do {
4314         flick(canvas, flickEnd, flickStart, 180);
4315         QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4316     } while (flow == QQuickGridView::FlowLeftToRight
4317            ? !gridview->isAtYBeginning()
4318            : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4319
4320     if (flow == QQuickGridView::FlowLeftToRight)
4321         QCOMPARE(gridview->contentY(), startExtent);
4322     else
4323         QCOMPARE(gridview->contentX(), startExtent);
4324
4325     releaseView(canvas);
4326 }
4327
4328 void tst_QQuickGridView::snapOneRow_data()
4329 {
4330     QTest::addColumn<QQuickGridView::Flow>("flow");
4331     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
4332     QTest::addColumn<int>("highlightRangeMode");
4333     QTest::addColumn<QPoint>("flickStart");
4334     QTest::addColumn<QPoint>("flickEnd");
4335     QTest::addColumn<qreal>("snapAlignment");
4336     QTest::addColumn<qreal>("endExtent");
4337     QTest::addColumn<qreal>("startExtent");
4338
4339     QTest::newRow("vertical, left to right") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4340         << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4341
4342     QTest::newRow("horizontal, left to right") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::NoHighlightRange)
4343         << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 240.0 << 0.0;
4344
4345     QTest::newRow("horizontal, right to left") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::NoHighlightRange)
4346         << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 << -240.0;
4347
4348     QTest::newRow("vertical, left to right, enforce range") << QQuickGridView::FlowLeftToRight << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4349         << QPoint(20, 200) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4350
4351     QTest::newRow("horizontal, left to right, enforce range") << QQuickGridView::FlowTopToBottom << Qt::LeftToRight << int(QQuickItemView::StrictlyEnforceRange)
4352         << QPoint(200, 20) << QPoint(20, 20) << 100.0 << 340.0 << -20.0;
4353
4354     QTest::newRow("horizontal, right to left, enforce range") << QQuickGridView::FlowTopToBottom << Qt::RightToLeft << int(QQuickItemView::StrictlyEnforceRange)
4355         << QPoint(20, 20) << QPoint(200, 20) << -340.0 << -240.0 - 240.0 - 100.0 << -220.0;
4356 }
4357
4358 void tst_QQuickGridView::snapOneRow()
4359 {
4360     QFETCH(QQuickGridView::Flow, flow);
4361     QFETCH(Qt::LayoutDirection, layoutDirection);
4362     QFETCH(int, highlightRangeMode);
4363     QFETCH(QPoint, flickStart);
4364     QFETCH(QPoint, flickEnd);
4365     QFETCH(qreal, snapAlignment);
4366     QFETCH(qreal, endExtent);
4367     QFETCH(qreal, startExtent);
4368
4369     QQuickView *canvas = getView();
4370
4371     canvas->setSource(testFileUrl("snapOneRow.qml"));
4372     canvas->show();
4373     qApp->processEvents();
4374
4375     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4376     QTRY_VERIFY(gridview != 0);
4377
4378     gridview->setFlow(flow);
4379     gridview->setLayoutDirection(layoutDirection);
4380     gridview->setHighlightRangeMode(QQuickItemView::HighlightRangeMode(highlightRangeMode));
4381     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4382
4383     QQuickItem *contentItem = gridview->contentItem();
4384     QTRY_VERIFY(contentItem != 0);
4385
4386     QSignalSpy currentIndexSpy(gridview, SIGNAL(currentIndexChanged()));
4387
4388     // confirm that a flick hits next row boundary
4389     flick(canvas, flickStart, flickEnd, 180);
4390     QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4391     if (flow == QQuickGridView::FlowLeftToRight)
4392         QCOMPARE(gridview->contentY(), snapAlignment);
4393     else
4394         QCOMPARE(gridview->contentX(), snapAlignment);
4395
4396     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4397         QCOMPARE(gridview->currentIndex(), 2);
4398         QCOMPARE(currentIndexSpy.count(), 1);
4399     }
4400
4401     // flick to end
4402     do {
4403         flick(canvas, flickStart, flickEnd, 180);
4404         QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4405     } while (flow == QQuickGridView::FlowLeftToRight
4406            ? !gridview->isAtYEnd()
4407            : layoutDirection == Qt::LeftToRight ? !gridview->isAtXEnd() : !gridview->isAtXBeginning());
4408
4409     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4410         QCOMPARE(gridview->currentIndex(), 6);
4411         QCOMPARE(currentIndexSpy.count(), 3);
4412     }
4413
4414     if (flow == QQuickGridView::FlowLeftToRight)
4415         QCOMPARE(gridview->contentY(), endExtent);
4416     else
4417         QCOMPARE(gridview->contentX(), endExtent);
4418
4419     // flick to start
4420     do {
4421         flick(canvas, flickEnd, flickStart, 180);
4422         QTRY_VERIFY(gridview->isMoving() == false); // wait until it stops
4423     } while (flow == QQuickGridView::FlowLeftToRight
4424            ? !gridview->isAtYBeginning()
4425            : layoutDirection == Qt::LeftToRight ? !gridview->isAtXBeginning() : !gridview->isAtXEnd());
4426
4427     if (flow == QQuickGridView::FlowLeftToRight)
4428         QCOMPARE(gridview->contentY(), startExtent);
4429     else
4430         QCOMPARE(gridview->contentX(), startExtent);
4431
4432     if (QQuickItemView::HighlightRangeMode(highlightRangeMode) == QQuickItemView::StrictlyEnforceRange) {
4433         QCOMPARE(gridview->currentIndex(), 0);
4434         QCOMPARE(currentIndexSpy.count(), 6);
4435     }
4436
4437     releaseView(canvas);
4438 }
4439
4440
4441 void tst_QQuickGridView::unaligned()
4442 {
4443     QQuickView *canvas = createView();
4444     canvas->show();
4445
4446     QaimModel model;
4447     for (int i = 0; i < 10; i++)
4448         model.addItem("Item" + QString::number(i), "");
4449
4450     QQmlContext *ctxt = canvas->rootContext();
4451     ctxt->setContextProperty("testModel", &model);
4452
4453     canvas->setSource(testFileUrl("unaligned.qml"));
4454     qApp->processEvents();
4455
4456     QQuickGridView *gridview = qobject_cast<QQuickGridView*>(canvas->rootObject());
4457     QVERIFY(gridview != 0);
4458
4459     QQuickItem *contentItem = gridview->contentItem();
4460     QVERIFY(contentItem != 0);
4461
4462     for (int i = 0; i < 10; ++i) {
4463         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4464         if (!item) qWarning() << "Item" << i << "not found";
4465         QVERIFY(item);
4466         QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4467         QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4468     }
4469
4470     // appending
4471     for (int i = 10; i < 18; ++i) {
4472         model.addItem("Item" + QString::number(i), "");
4473         QQuickItem *item = 0;
4474         QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4475         QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4476         QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4477     }
4478
4479     // inserting
4480     for (int i = 0; i < 10; ++i) {
4481         model.insertItem(i, "Item" + QString::number(i), "");
4482         QQuickItem *item = 0;
4483         QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4484         QCOMPARE(item->x(), qreal((i%9)*gridview->cellWidth()));
4485         QCOMPARE(item->y(), qreal((i/9)*gridview->cellHeight()));
4486     }
4487
4488     // removing
4489     model.removeItems(7, 10);
4490     QTRY_COMPARE(model.count(), gridview->count());
4491     for (int i = 0; i < 18; ++i) {
4492         QQuickItem *item = 0;
4493         QTRY_VERIFY(item = findItem<QQuickItem>(contentItem, "wrapper", i));
4494         QCOMPARE(item->x(), qreal(i%9)*gridview->cellWidth());
4495         QCOMPARE(item->y(), qreal(i/9)*gridview->cellHeight());
4496     }
4497
4498     delete canvas;
4499 }
4500
4501 void tst_QQuickGridView::populateTransitions()
4502 {
4503     QFETCH(bool, staticallyPopulate);
4504     QFETCH(bool, dynamicallyPopulate);
4505     QFETCH(bool, usePopulateTransition);
4506
4507     QPointF transitionFrom(-50, -50);
4508     QPointF transitionVia(100, 100);
4509     QaimModel model_transitionFrom;
4510     QaimModel model_transitionVia;
4511
4512     QaimModel model;
4513     if (staticallyPopulate) {
4514         for (int i = 0; i < 30; i++)
4515             model.addItem("item" + QString::number(i), "");
4516     }
4517
4518     QQuickView *canvas = getView();
4519     canvas->rootContext()->setContextProperty("testModel", &model);
4520     canvas->rootContext()->setContextProperty("usePopulateTransition", usePopulateTransition);
4521     canvas->rootContext()->setContextProperty("dynamicallyPopulate", dynamicallyPopulate);
4522     canvas->rootContext()->setContextProperty("transitionFrom", transitionFrom);
4523     canvas->rootContext()->setContextProperty("transitionVia", transitionVia);
4524     canvas->rootContext()->setContextProperty("model_transitionFrom", &model_transitionFrom);
4525     canvas->rootContext()->setContextProperty("model_transitionVia", &model_transitionVia);
4526     canvas->setSource(testFileUrl("populateTransitions.qml"));
4527     canvas->show();
4528
4529     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4530     QVERIFY(gridview);
4531     QQuickItem *contentItem = gridview->contentItem();
4532     QVERIFY(contentItem);
4533
4534     // check the populate transition is run
4535     if (staticallyPopulate && usePopulateTransition) {
4536         QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 18);
4537         QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4538     } else if (dynamicallyPopulate) {
4539         QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4540         QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 18);
4541     } else {
4542         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4543         QCOMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4544         QCOMPARE(gridview->property("countAddTransitions").toInt(), 0);
4545     }
4546
4547     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4548     for (int i=0; i < model.count() && i < itemCount; ++i) {
4549         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4550         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4551         QTRY_COMPARE(item->x(), (i%3)*80.0);
4552         QTRY_COMPARE(item->y(), (i/3)*60.0);
4553         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4554         QVERIFY(name != 0);
4555         QTRY_COMPARE(name->text(), model.name(i));
4556     }
4557
4558     gridview->setProperty("countPopulateTransitions", 0);
4559     gridview->setProperty("countAddTransitions", 0);
4560
4561     // add an item and check this is done with add transition, not populate
4562     model.insertItem(0, "another item", "");
4563     QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 1);
4564     QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), 0);
4565
4566     // clear the model
4567     canvas->rootContext()->setContextProperty("testModel", QVariant());
4568     QTRY_COMPARE(gridview->count(), 0);
4569     QTRY_COMPARE(findItems<QQuickItem>(contentItem, "wrapper").count(), 0);
4570     gridview->setProperty("countPopulateTransitions", 0);
4571     gridview->setProperty("countAddTransitions", 0);
4572
4573     // set to a valid model and check populate transition is run a second time
4574     model.clear();
4575     for (int i = 0; i < 30; i++)
4576         model.addItem("item" + QString::number(i), "");
4577     canvas->rootContext()->setContextProperty("testModel", &model);
4578     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4579
4580     QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4581     QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4582
4583     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4584     for (int i=0; i < model.count() && i < itemCount; ++i) {
4585         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4586         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4587         QTRY_COMPARE(item->x(), (i%3)*80.0);
4588         QTRY_COMPARE(item->y(), (i/3)*60.0);
4589         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4590         QVERIFY(name != 0);
4591         QTRY_COMPARE(name->text(), model.name(i));
4592     }
4593
4594     // reset model and check populate transition is run again
4595     gridview->setProperty("countPopulateTransitions", 0);
4596     gridview->setProperty("countAddTransitions", 0);
4597     model.reset();
4598     QTRY_COMPARE(gridview->property("countPopulateTransitions").toInt(), usePopulateTransition ? 18 : 0);
4599     QTRY_COMPARE(gridview->property("countAddTransitions").toInt(), 0);
4600
4601     itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
4602     for (int i=0; i < model.count() && i < itemCount; ++i) {
4603         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4604         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4605         QTRY_COMPARE(item->x(), (i%3)*80.0);
4606         QTRY_COMPARE(item->y(), (i/3)*60.0);
4607         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4608         QVERIFY(name != 0);
4609         QTRY_COMPARE(name->text(), model.name(i));
4610     }
4611
4612     releaseView(canvas);
4613 }
4614
4615 void tst_QQuickGridView::populateTransitions_data()
4616 {
4617     QTest::addColumn<bool>("staticallyPopulate");
4618     QTest::addColumn<bool>("dynamicallyPopulate");
4619     QTest::addColumn<bool>("usePopulateTransition");
4620
4621     QTest::newRow("static") << true << false << true;
4622     QTest::newRow("static, no populate") << true << false << false;
4623
4624     QTest::newRow("dynamic") << false << true << true;
4625     QTest::newRow("dynamic, no populate") << false << true << false;
4626
4627     QTest::newRow("empty to start with") << false << false << true;
4628     QTest::newRow("empty to start with, no populate") << false << false << false;
4629 }
4630
4631 void tst_QQuickGridView::addTransitions()
4632 {
4633     QFETCH(int, initialItemCount);
4634     QFETCH(bool, shouldAnimateTargets);
4635     QFETCH(qreal, contentYRowOffset);
4636     QFETCH(int, insertionIndex);
4637     QFETCH(int, insertionCount);
4638     QFETCH(ListRange, expectedDisplacedIndexes);
4639
4640     // added items should start here
4641     QPointF targetItems_transitionFrom(-50, -50);
4642
4643     // displaced items should pass through this point
4644     QPointF displacedItems_transitionVia(100, 100);
4645
4646     QaimModel model;
4647     for (int i = 0; i < initialItemCount; i++)
4648         model.addItem("Original item" + QString::number(i), "");
4649     QaimModel model_targetItems_transitionFrom;
4650     QaimModel model_displacedItems_transitionVia;
4651
4652     QQuickView *canvas = getView();
4653     QQmlContext *ctxt = canvas->rootContext();
4654     ctxt->setContextProperty("testModel", &model);
4655     ctxt->setContextProperty("model_targetItems_transitionFrom", &model_targetItems_transitionFrom);
4656     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4657     ctxt->setContextProperty("targetItems_transitionFrom", targetItems_transitionFrom);
4658     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4659     canvas->setSource(testFileUrl("addTransitions.qml"));
4660     canvas->show();
4661
4662     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4663     QTRY_VERIFY(gridview != 0);
4664     QQuickItem *contentItem = gridview->contentItem();
4665     QVERIFY(contentItem != 0);
4666     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4667
4668     if (contentYRowOffset != 0) {
4669         gridview->setContentY(contentYRowOffset * 60.0);
4670         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4671     }
4672
4673     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4674
4675     // only target items that will become visible should be animated
4676     QList<QPair<QString, QString> > newData;
4677     QList<QPair<QString, QString> > expectedTargetData;
4678     QList<int> targetIndexes;
4679     if (shouldAnimateTargets) {
4680         for (int i=insertionIndex; i<insertionIndex+insertionCount; i++) {
4681             newData << qMakePair(QString("New item %1").arg(i), QString(""));
4682
4683             // last visible item is the first item of the row beneath the view
4684             if (i >= (gridview->contentY() / 60)*3 && i < qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) {
4685                 expectedTargetData << newData.last();
4686                 targetIndexes << i;
4687             }
4688         }
4689         QVERIFY(expectedTargetData.count() > 0);
4690     }
4691
4692     // start animation
4693     if (!newData.isEmpty()) {
4694         model.insertItems(insertionIndex, newData);
4695         QTRY_COMPARE(model.count(), gridview->count());
4696     }
4697
4698     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4699
4700     if (shouldAnimateTargets) {
4701         QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4702         QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4703                      expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4704
4705         // check the target and displaced items were animated
4706         model_targetItems_transitionFrom.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4707         model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4708
4709         // check attached properties
4710         matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4711         matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4712         matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4713         if (expectedDisplacedIndexes.isValid()) {
4714             // adjust expectedDisplacedIndexes to their final values after the move
4715             QList<int> displacedIndexes = adjustIndexesForAddDisplaced(expectedDisplacedIndexes.indexes, insertionIndex, insertionCount);
4716             matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4717             matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4718             matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4719         }
4720     } else {
4721         QTRY_COMPARE(model_targetItems_transitionFrom.count(), 0);
4722         QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
4723     }
4724
4725     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4726     int firstVisibleIndex = -1;
4727     for (int i=0; i<items.count(); i++) {
4728         if (items[i]->y() >= gridview->contentY()) {
4729             QQmlExpression e(qmlContext(items[i]), items[i], "index");
4730             firstVisibleIndex = e.evaluate().toInt();
4731             break;
4732         }
4733     }
4734     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4735
4736     // verify all items moved to the correct final positions
4737     for (int i = firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4738         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4739         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4740         QCOMPARE(item->x(), (i%3)*80.0);
4741         QCOMPARE(item->y(), (i/3)*60.0);
4742         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
4743         QVERIFY(name != 0);
4744         QCOMPARE(name->text(), model.name(i));
4745     }
4746
4747     releaseView(canvas);
4748 }
4749
4750 void tst_QQuickGridView::addTransitions_data()
4751 {
4752     QTest::addColumn<int>("initialItemCount");
4753     QTest::addColumn<qreal>("contentYRowOffset");
4754     QTest::addColumn<bool>("shouldAnimateTargets");
4755     QTest::addColumn<int>("insertionIndex");
4756     QTest::addColumn<int>("insertionCount");
4757     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4758
4759     // if inserting a full row before visible index, items don't appear or animate in, even if there are > 1 new items
4760     QTest::newRow("insert 1, just before start")
4761             << 30 << 1.0 << false
4762             << 0 << 1 << ListRange();
4763     QTest::newRow("insert 1, way before start")
4764             << 30 << 1.0 << false
4765             << 0 << 1 << ListRange();
4766     QTest::newRow("insert multiple, just before start")
4767             << 30 << 1.0 << false
4768             << 0 << 3 << ListRange();
4769     QTest::newRow("insert multiple (< 1 row), just before start")
4770             << 30 << 1.0 << false
4771             << 0 << 2 << ListRange();
4772     QTest::newRow("insert multiple, way before start")
4773             << 30 << 3.0 << false
4774             << 0 << 3 << ListRange();
4775
4776     QTest::newRow("insert 1 at start")
4777             << 30 << 0.0 << true
4778             << 0 << 1 << ListRange(0, 17);
4779     QTest::newRow("insert multiple at start")
4780             << 30 << 0.0 << true
4781             << 0 << 3 << ListRange(0, 17);
4782     QTest::newRow("insert multiple (> 1 row) at start")
4783             << 30 << 0.0 << true
4784             << 0 << 5 << ListRange(0, 17);
4785     QTest::newRow("insert 1 at start, content y not 0")
4786             << 30 << 1.0 << true  // first visible is index 3
4787             << 3 << 1 << ListRange(0 + 3, 17 + 3);
4788     QTest::newRow("insert multiple at start, content y not 0")
4789             << 30 << 1.0 << true    // first visible is index 3
4790             << 3 << 3 << ListRange(0 + 3, 17 + 3);
4791     QTest::newRow("insert multiple (> 1 row) at start, content y not 0")
4792             << 30 << 1.0 << true    // first visible is index 3
4793             << 3 << 5 << ListRange(0 + 3, 17 + 3);
4794
4795     QTest::newRow("insert 1 at start, to empty grid")
4796             << 0 << 0.0 << true
4797             << 0 << 1 << ListRange();
4798     QTest::newRow("insert multiple at start, to empty grid")
4799             << 0 << 0.0 << true
4800             << 0 << 3 << ListRange();
4801
4802     QTest::newRow("insert 1 at middle")
4803             << 30 << 0.0 << true
4804             << 7 << 1 << ListRange(7, 17);
4805     QTest::newRow("insert multiple at middle")
4806             << 30 << 0.0 << true
4807             << 7 << 3 << ListRange(7, 17);
4808     QTest::newRow("insert multiple (> 1 row) at middle")
4809             << 30 << 0.0 << true
4810             << 7 << 5 << ListRange(7, 17);
4811
4812     QTest::newRow("insert 1 at bottom")
4813             << 30 << 0.0 << true
4814             << 17 << 1 << ListRange(17, 17);
4815     QTest::newRow("insert multiple at bottom")
4816             << 30 << 0.0 << true
4817             << 17 << 3 << ListRange(17, 17);
4818     QTest::newRow("insert 1 at bottom, content y not 0")
4819             << 30 << 1.0 << true
4820             << 17 + 3 << 1 << ListRange(17 + 3, 17 + 3);
4821     QTest::newRow("insert multiple at bottom, content y not 0")
4822             << 30 << 1.0 << true
4823             << 17 + 3 << 3 << ListRange(17 + 3, 17 + 3);
4824
4825
4826     // items added after the last visible will not be animated in, since they
4827     // do not appear in the final view
4828     QTest::newRow("insert 1 after end")
4829             << 30 << 0.0 << false
4830             << 18 << 1 << ListRange();
4831     QTest::newRow("insert multiple after end")
4832             << 30 << 0.0 << false
4833             << 18 << 3 << ListRange();
4834 }
4835
4836 void tst_QQuickGridView::moveTransitions()
4837 {
4838     QFETCH(int, initialItemCount);
4839     QFETCH(qreal, contentYRowOffset);
4840     QFETCH(qreal, rowOffsetAfterMove);
4841     QFETCH(int, moveFrom);
4842     QFETCH(int, moveTo);
4843     QFETCH(int, moveCount);
4844     QFETCH(ListRange, expectedDisplacedIndexes);
4845
4846     // target and displaced items should pass through these points
4847     QPointF targetItems_transitionVia(-50, 50);
4848     QPointF displacedItems_transitionVia(100, 100);
4849
4850     QaimModel model;
4851     for (int i = 0; i < initialItemCount; i++)
4852         model.addItem("Original item" + QString::number(i), "");
4853     QaimModel model_targetItems_transitionVia;
4854     QaimModel model_displacedItems_transitionVia;
4855
4856     QQuickView *canvas = getView();
4857     QQmlContext *ctxt = canvas->rootContext();
4858     ctxt->setContextProperty("testModel", &model);
4859     ctxt->setContextProperty("model_targetItems_transitionVia", &model_targetItems_transitionVia);
4860     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
4861     ctxt->setContextProperty("targetItems_transitionVia", targetItems_transitionVia);
4862     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
4863     canvas->setSource(testFileUrl("moveTransitions.qml"));
4864     canvas->show();
4865
4866     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
4867     QTRY_VERIFY(gridview != 0);
4868     QQuickItem *contentItem = gridview->contentItem();
4869     QVERIFY(contentItem != 0);
4870     QQuickText *name;
4871
4872     if (contentYRowOffset != 0) {
4873         gridview->setContentY(contentYRowOffset * 60.0);
4874         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
4875     }
4876
4877     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
4878
4879     // Items moving to *or* from visible positions should be animated.
4880     // Otherwise, they should not be animated.
4881     QList<QPair<QString, QString> > expectedTargetData;
4882     QList<int> targetIndexes;
4883     for (int i=moveFrom; i<moveFrom+moveCount; i++) {
4884         int toIndex = moveTo + (i - moveFrom);
4885         int firstVisibleIndex = (gridview->contentY() / 60) * 3;
4886         int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
4887         if ((i >= firstVisibleIndex && i <= lastVisibleIndex)
4888                 || (toIndex >= firstVisibleIndex && toIndex <= lastVisibleIndex)) {
4889             expectedTargetData << qMakePair(model.name(i), model.number(i));
4890             targetIndexes << i;
4891         }
4892     }
4893     // ViewTransition.index provides the indices that items are moving to, not from
4894     targetIndexes = adjustIndexesForMove(targetIndexes, moveFrom, moveTo, moveCount);
4895
4896     // start animation
4897     model.moveItems(moveFrom, moveTo, moveCount);
4898
4899     QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
4900     QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
4901                  expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
4902
4903     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
4904
4905     // check the target and displaced items were animated
4906     model_targetItems_transitionVia.matchAgainst(expectedTargetData, "wasn't animated from target 'from' pos", "shouldn't have been animated from target 'from' pos");
4907     model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
4908
4909     // check attached properties
4910     matchItemsAndIndexes(gridview->property("targetTrans_items").toMap(), model, targetIndexes);
4911     matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
4912     matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
4913     if (expectedDisplacedIndexes.isValid()) {
4914         // adjust expectedDisplacedIndexes to their final values after the move
4915         QList<int> displacedIndexes = adjustIndexesForMove(expectedDisplacedIndexes.indexes, moveFrom, moveTo, moveCount);
4916         matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
4917         matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
4918         matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
4919     }
4920
4921     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
4922     int firstVisibleIndex = -1;
4923     for (int i=0; i<items.count(); i++) {
4924         if (items[i]->y() >= gridview->contentY()) {
4925             QQmlExpression e(qmlContext(items[i]), items[i], "index");
4926             firstVisibleIndex = e.evaluate().toInt();
4927             break;
4928         }
4929     }
4930     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
4931
4932     // verify all items moved to the correct final positions
4933     qreal pixelOffset = 60 * rowOffsetAfterMove;
4934     for (int i=firstVisibleIndex; i < model.count() && i < items.count(); ++i) {
4935         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
4936         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
4937         QCOMPARE(item->x(), (i%3)*80.0);
4938         QCOMPARE(item->y(), (i/3)*60.0 + pixelOffset);
4939         name = findItem<QQuickText>(contentItem, "textName", i);
4940         QVERIFY(name != 0);
4941         QTRY_COMPARE(name->text(), model.name(i));
4942     }
4943
4944     releaseView(canvas);
4945 }
4946
4947 void tst_QQuickGridView::moveTransitions_data()
4948 {
4949     QTest::addColumn<int>("initialItemCount");
4950     QTest::addColumn<qreal>("contentYRowOffset");
4951     QTest::addColumn<qreal>("rowOffsetAfterMove");
4952     QTest::addColumn<int>("moveFrom");
4953     QTest::addColumn<int>("moveTo");
4954     QTest::addColumn<int>("moveCount");
4955     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
4956
4957     QTest::newRow("move from above view, outside visible items, move 1")
4958             << 30 << 2.0 << 0.0
4959             << 1 << 10 << 1 << ListRange(6, 10);
4960     QTest::newRow("move from above view, outside visible items, move 1 (first item)")
4961             << 30 << 2.0 << 0.0
4962             << 0 << 10 << 1 << ListRange(6, 10);
4963     QTest::newRow("move from above view, outside visible items, move multiple")
4964             << 30 << 2.0 << 1.0
4965             << 1 << 10 << 3 << ListRange(13, 23);
4966     QTest::newRow("move from above view, mix of visible/non-visible")
4967             << 30 << 2.0 << 1.0
4968             << 1 << 10 << 6 << (ListRange(7, 15) + ListRange(16, 23));
4969     QTest::newRow("move from above view, mix of visible/non-visible (move first)")
4970             << 30 << 2.0 << 2.0
4971             << 0 << 10 << 6 << ListRange(16, 23);
4972
4973     QTest::newRow("move within view, move 1 down")
4974             << 30 << 0.0 << 0.0
4975             << 1 << 10 << 1 << ListRange(2, 10);
4976     QTest::newRow("move within view, move 1 down, move first item")
4977             << 30 << 0.0 << 0.0
4978             << 0 << 10 << 1 << ListRange(1, 10);
4979     QTest::newRow("move within view, move 1 down, move first item, contentY not 0")
4980             << 30 << 2.0 << 0.0
4981             << 0+6 << 10+6 << 1 << ListRange(1+6, 10+6);
4982     QTest::newRow("move within view, move 1 down, to last item")
4983             << 30 << 0.0 << 0.0
4984             << 10 << 17 << 1 << ListRange(11, 17);
4985     QTest::newRow("move within view, move first->last")
4986             << 30 << 0.0 << 0.0
4987             << 0 << 17 << 1 << ListRange(1, 17);
4988
4989     QTest::newRow("move within view, move multiple down")
4990             << 30 << 0.0 << 0.0
4991             << 1 << 10 << 3 << ListRange(4, 12);
4992     QTest::newRow("move within view, move multiple down, move first item")
4993             << 30 << 0.0 << 0.0
4994             << 0 << 10 << 3 << ListRange(3, 12);
4995     QTest::newRow("move within view, move multiple down, move first item, contentY not 0")
4996             << 30 << 1.0 << 0.0
4997             << 0+3 << 10+3 << 3 << ListRange(3+3, 12+3);
4998     QTest::newRow("move within view, move multiple down, displace last item")
4999             << 30 << 0.0 << 0.0
5000             << 5 << 15 << 3 << ListRange(8, 17);
5001     QTest::newRow("move within view, move multiple down, move first->last")
5002             << 30 << 0.0 << 0.0
5003             << 0 << 15 << 3 << ListRange(3, 17);
5004
5005     QTest::newRow("move within view, move 1 up")
5006             << 30 << 0.0 << 0.0
5007             << 10 << 1 << 1 << ListRange(1, 9);
5008     QTest::newRow("move within view, move 1 up, move to first index")
5009             << 30 << 0.0 << 0.0
5010             << 10 << 0 << 1 << ListRange(0, 9);
5011     QTest::newRow("move within view, move 1 up, move to first index, contentY not 0")
5012             << 30 << 2.0 << 0.0
5013             << 10+6 << 0+6 << 1 << ListRange(0+6, 9+6);
5014     QTest::newRow("move within view, move 1 up, move to first index, contentY not on item border")
5015             << 30 << 1.5 << 0.0
5016             << 10+3 << 0+3 << 1 << ListRange(0+3, 9+3);
5017     QTest::newRow("move within view, move 1 up, move last item")
5018             << 30 << 0.0 << 0.0
5019             << 17 << 10 << 1 << ListRange(10, 16);
5020     QTest::newRow("move within view, move 1 up, move last->first")
5021             << 30 << 0.0 << 0.0
5022             << 17 << 0 << 1 << ListRange(0, 16);
5023
5024     QTest::newRow("move within view, move multiple up")
5025             << 30 << 0.0 << 0.0
5026             << 10 << 1 << 3 << ListRange(1, 9);
5027     QTest::newRow("move within view, move multiple (> 1 row) up")
5028             << 30 << 0.0 << 0.0
5029             << 10 << 1 << 5 << ListRange(1, 9);
5030     QTest::newRow("move within view, move multiple up, move to first index")
5031             << 30 << 0.0 << 0.0
5032             << 10 << 0 << 3 << ListRange(0, 9);
5033     QTest::newRow("move within view, move multiple up, move to first index, contentY not 0")
5034             << 30 << 1.0 << 0.0
5035             << 10+3 << 0+3 << 3 << ListRange(0+3, 9+3);
5036     QTest::newRow("move within view, move multiple up (> 1 row), move to first index, contentY not on border")
5037             << 30 << 1.5 << 0.0
5038             << 10+3 << 0+3 << 5 << ListRange(0+3, 9+3);
5039     QTest::newRow("move within view, move multiple up, move last item")
5040             << 30 << 0.0 << 0.0
5041             << 15 << 5 << 3 << ListRange(5, 14);
5042     QTest::newRow("move within view, move multiple up, move last->first")
5043             << 30 << 0.0 << 0.0
5044             << 15 << 0 << 3 << ListRange(0, 14);
5045
5046     QTest::newRow("move from below view, move 1 up")
5047             << 30 << 0.0 << 0.0
5048             << 20 << 5 << 1 << ListRange(5, 17);
5049     QTest::newRow("move from below view, move 1 up, move to top")
5050             << 30 << 0.0 << 0.0
5051             << 20 << 0 << 1 << ListRange(0, 17);
5052     QTest::newRow("move from below view, move 1 up, move to top, contentY not 0")
5053             << 30 << 1.0 << 0.0
5054             << 25 << 3 << 1 << ListRange(0+3, 17+3);
5055     QTest::newRow("move from below view, move multiple (> 1 row) up")
5056             << 30 << 0.0 << 0.0
5057             << 20 << 5 << 5 << ListRange(5, 17);
5058     QTest::newRow("move from below view, move multiple up, move to top")
5059             << 30 << 0.0 << 0.0
5060             << 20 << 0 << 3 << ListRange(0, 17);
5061     QTest::newRow("move from below view, move multiple up, move to top, contentY not 0")
5062             << 30 << 1.0 << 0.0
5063             << 25 << 3 << 3 << ListRange(0+3, 17+3);
5064
5065     QTest::newRow("move from below view, move 1 up, move to bottom")
5066             << 30 << 0.0 << 0.0
5067             << 20 << 17 << 1 << ListRange(17, 17);
5068     QTest::newRow("move from below view, move 1 up, move to bottom, contentY not 0")
5069             << 30 << 1.0 << 0.0
5070             << 25 << 17+3 << 1 << ListRange(17+3, 17+3);
5071     QTest::newRow("move from below view, move multiple up, move to to bottom")
5072             << 30 << 0.0 << 0.0
5073             << 20 << 17 << 3 << ListRange(17, 17);
5074     QTest::newRow("move from below view, move multiple up, move to bottom, contentY not 0")
5075             << 30 << 1.0 << 0.0
5076             << 25 << 17+3 << 3 << ListRange(17+3, 17+3);
5077 }
5078
5079 void tst_QQuickGridView::removeTransitions()
5080 {
5081     QFETCH(int, initialItemCount);
5082     QFETCH(bool, shouldAnimateTargets);
5083     QFETCH(qreal, contentYRowOffset);
5084     QFETCH(int, removalIndex);
5085     QFETCH(int, removalCount);
5086     QFETCH(ListRange, expectedDisplacedIndexes);
5087
5088     // added items should end here
5089     QPointF targetItems_transitionTo(-50, -50);
5090
5091     // displaced items should pass through this points
5092     QPointF displacedItems_transitionVia(100, 100);
5093
5094     QaimModel model;
5095     for (int i = 0; i < initialItemCount; i++)
5096         model.addItem("Original item" + QString::number(i), "");
5097     QaimModel model_targetItems_transitionTo;
5098     QaimModel model_displacedItems_transitionVia;
5099
5100     QQuickView *canvas = getView();
5101     QQmlContext *ctxt = canvas->rootContext();
5102     ctxt->setContextProperty("testModel", &model);
5103     ctxt->setContextProperty("model_targetItems_transitionTo", &model_targetItems_transitionTo);
5104     ctxt->setContextProperty("model_displacedItems_transitionVia", &model_displacedItems_transitionVia);
5105     ctxt->setContextProperty("targetItems_transitionTo", targetItems_transitionTo);
5106     ctxt->setContextProperty("displacedItems_transitionVia", displacedItems_transitionVia);
5107     canvas->setSource(testFileUrl("removeTransitions.qml"));
5108     canvas->show();
5109
5110     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5111     QTRY_VERIFY(gridview != 0);
5112     QQuickItem *contentItem = gridview->contentItem();
5113     QVERIFY(contentItem != 0);
5114     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5115
5116     if (contentYRowOffset != 0) {
5117         gridview->setContentY(contentYRowOffset * 60.0);
5118         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5119     }
5120
5121     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5122
5123     // only target items that are visible should be animated
5124     QList<QPair<QString, QString> > expectedTargetData;
5125     QList<int> targetIndexes;
5126     if (shouldAnimateTargets) {
5127         for (int i=removalIndex; i<removalIndex+removalCount; i++) {
5128             int firstVisibleIndex = (gridview->contentY() / 60.0)*3;
5129             int lastVisibleIndex = (qCeil((gridview->contentY() + gridview->height()) / 60.0)*3) - 1;
5130             if (i >= firstVisibleIndex && i <= lastVisibleIndex) {
5131                 expectedTargetData << qMakePair(model.name(i), model.number(i));
5132                 targetIndexes << i;
5133             }
5134         }
5135         QVERIFY(expectedTargetData.count() > 0);
5136     }
5137
5138     // calculate targetItems and expectedTargets before model changes
5139     QList<QQuickItem *> targetItems = findItems<QQuickItem>(contentItem, "wrapper", targetIndexes);
5140     QVariantMap expectedTargets;
5141     for (int i=0; i<targetIndexes.count(); i++)
5142         expectedTargets[model.name(targetIndexes[i])] = targetIndexes[i];
5143
5144     // start animation
5145     model.removeItems(removalIndex, removalCount);
5146     QTRY_COMPARE(model.count(), gridview->count());
5147
5148     if (shouldAnimateTargets || expectedDisplacedIndexes.isValid()) {
5149         QTRY_COMPARE(gridview->property("targetTransitionsDone").toInt(), expectedTargetData.count());
5150         QTRY_COMPARE(gridview->property("displaceTransitionsDone").toInt(),
5151                      expectedDisplacedIndexes.isValid() ? expectedDisplacedIndexes.count() : 0);
5152
5153         // check the target and displaced items were animated
5154         model_targetItems_transitionTo.matchAgainst(expectedTargetData, "wasn't animated to target 'to' pos", "shouldn't have been animated to target 'to' pos");
5155         model_displacedItems_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with displaced anim", "shouldn't have been animated with displaced anim");
5156
5157         // check attached properties
5158         QCOMPARE(gridview->property("targetTrans_items").toMap(), expectedTargets);
5159         matchIndexLists(gridview->property("targetTrans_targetIndexes").toList(), targetIndexes);
5160         matchItemLists(gridview->property("targetTrans_targetItems").toList(), targetItems);
5161         if (expectedDisplacedIndexes.isValid()) {
5162             // adjust expectedDisplacedIndexes to their final values after the move
5163             QList<int> displacedIndexes = adjustIndexesForRemoveDisplaced(expectedDisplacedIndexes.indexes, removalIndex, removalCount);
5164             matchItemsAndIndexes(gridview->property("displacedTrans_items").toMap(), model, displacedIndexes);
5165             matchIndexLists(gridview->property("displacedTrans_targetIndexes").toList(), targetIndexes);
5166             matchItemLists(gridview->property("displacedTrans_targetItems").toList(), targetItems);
5167         }
5168     } else {
5169         QTRY_COMPARE(model_targetItems_transitionTo.count(), 0);
5170         QTRY_COMPARE(model_displacedItems_transitionVia.count(), 0);
5171     }
5172
5173     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5174     int itemCount = items.count();
5175     int firstVisibleIndex = -1;
5176     for (int i=0; i<items.count(); i++) {
5177         QQmlExpression e(qmlContext(items[i]), items[i], "index");
5178         int index = e.evaluate().toInt();
5179         if (firstVisibleIndex < 0 && items[i]->y() >= gridview->contentY())
5180             firstVisibleIndex = index;
5181         else if (index < 0)
5182             itemCount--;    // exclude deleted items
5183     }
5184     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5185
5186     // verify all items moved to the correct final positions
5187     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5188         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5189         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5190         QCOMPARE(item->x(), (i%3)*80.0);
5191         QCOMPARE(item->y(), gridview->contentY() + ((i-firstVisibleIndex)/3) * 60.0);
5192         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5193         QVERIFY(name != 0);
5194         QTRY_COMPARE(name->text(), model.name(i));
5195     }
5196
5197     releaseView(canvas);
5198 }
5199
5200 void tst_QQuickGridView::removeTransitions_data()
5201 {
5202     QTest::addColumn<int>("initialItemCount");
5203     QTest::addColumn<qreal>("contentYRowOffset");
5204     QTest::addColumn<bool>("shouldAnimateTargets");
5205     QTest::addColumn<int>("removalIndex");
5206     QTest::addColumn<int>("removalCount");
5207     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5208
5209     // All items that are visible following the remove operation should be animated.
5210     // Remove targets that are outside of the view should not be animated.
5211
5212     // For a GridView, removing any number of items other than a full row before the start
5213     // should displace all items in the view
5214     QTest::newRow("remove 1 before start")
5215             << 30 << 2.0 << false
5216             << 2 << 1 << ListRange(6, 24);    // 6-24 are displaced
5217     QTest::newRow("remove 1 row, before start")
5218             << 30 << 2.0 << false
5219             << 3 << 3 << ListRange();
5220     QTest::newRow("remove between 1-2 rows, before start")
5221             << 30 << 2.0 << false
5222             << 0 << 5 << ListRange(6, 25);
5223     QTest::newRow("remove 2 rows, before start")
5224             << 30 << 2.0 << false
5225             << 0 << 6 << ListRange();
5226     QTest::newRow("remove mix of before and after start")
5227             << 30 << 1.0 << true
5228             << 2 << 3 << ListRange(5, 23);  // 5-23 are displaced into view
5229
5230
5231     QTest::newRow("remove 1 from start")
5232             << 30 << 0.0 << true
5233             << 0 << 1 << ListRange(1, 18);  // 1-18 are displaced into view
5234     QTest::newRow("remove multiple from start")
5235             << 30 << 0.0 << true
5236             << 0 << 3 << ListRange(3, 20);  // 3-18 are displaced into view
5237     QTest::newRow("remove 1 from start, content y not 0")
5238             << 30 << 1.0 << true
5239             << 3 << 1 << ListRange(1 + 3, 18 + 3);
5240     QTest::newRow("remove multiple from start, content y not 0")
5241             << 30 << 1.0 << true
5242             << 3 << 3 << ListRange(3 + 3, 20 + 3);
5243
5244
5245     QTest::newRow("remove 1 from middle")
5246             << 30 << 0.0 << true
5247             << 5 << 1 << ListRange(6, 18);
5248     QTest::newRow("remove multiple from middle")
5249             << 30 << 0.0 << true
5250             << 5 << 3 << ListRange(8, 20);
5251
5252
5253     QTest::newRow("remove 1 from bottom")
5254             << 30 << 0.0 << true
5255             << 17 << 1 << ListRange(18, 18);
5256     QTest::newRow("remove multiple (1 row) from bottom")
5257             << 30 << 0.0 << true
5258             << 15 << 3 << ListRange(18, 20);
5259     QTest::newRow("remove multiple (> 1 row) from bottom")
5260             << 30 << 0.0 << true
5261             << 15 << 5 << ListRange(20, 22);
5262     QTest::newRow("remove 1 from bottom, content y not 0")
5263             << 30 << 1.0 << true
5264             << 17 + 3 << 1 << ListRange(18 + 3, 18 + 3);
5265     QTest::newRow("remove multiple (1 row) from bottom, content y not 0")
5266             << 30 << 1.0 << true
5267             << 15 + 3 << 3 << ListRange(18 + 3, 20 + 3);
5268
5269
5270     QTest::newRow("remove 1 after end")
5271             << 30 << 0.0 << false
5272             << 18 << 1 << ListRange();
5273     QTest::newRow("remove multiple after end")
5274             << 30 << 0.0 << false
5275             << 18 << 3 << ListRange();
5276 }
5277
5278 void tst_QQuickGridView::displacedTransitions()
5279 {
5280     QFETCH(bool, useDisplaced);
5281     QFETCH(bool, displacedEnabled);
5282     QFETCH(bool, useAddDisplaced);
5283     QFETCH(bool, addDisplacedEnabled);
5284     QFETCH(bool, useMoveDisplaced);
5285     QFETCH(bool, moveDisplacedEnabled);
5286     QFETCH(bool, useRemoveDisplaced);
5287     QFETCH(bool, removeDisplacedEnabled);
5288     QFETCH(ListChange, change);
5289     QFETCH(ListRange, expectedDisplacedIndexes);
5290
5291     QaimModel model;
5292     for (int i = 0; i < 30; i++)
5293         model.addItem("Original item" + QString::number(i), "");
5294     QaimModel model_displaced_transitionVia;
5295     QaimModel model_addDisplaced_transitionVia;
5296     QaimModel model_moveDisplaced_transitionVia;
5297     QaimModel model_removeDisplaced_transitionVia;
5298
5299     QPointF displaced_transitionVia(-50, -100);
5300     QPointF addDisplaced_transitionVia(-150, 100);
5301     QPointF moveDisplaced_transitionVia(50, -100);
5302     QPointF removeDisplaced_transitionVia(150, 100);
5303
5304     QQuickView *canvas = getView();
5305     QQmlContext *ctxt = canvas->rootContext();
5306     ctxt->setContextProperty("testModel", &model);
5307     ctxt->setContextProperty("model_displaced_transitionVia", &model_displaced_transitionVia);
5308     ctxt->setContextProperty("model_addDisplaced_transitionVia", &model_addDisplaced_transitionVia);
5309     ctxt->setContextProperty("model_moveDisplaced_transitionVia", &model_moveDisplaced_transitionVia);
5310     ctxt->setContextProperty("model_removeDisplaced_transitionVia", &model_removeDisplaced_transitionVia);
5311     ctxt->setContextProperty("displaced_transitionVia", displaced_transitionVia);
5312     ctxt->setContextProperty("addDisplaced_transitionVia", addDisplaced_transitionVia);
5313     ctxt->setContextProperty("moveDisplaced_transitionVia", moveDisplaced_transitionVia);
5314     ctxt->setContextProperty("removeDisplaced_transitionVia", removeDisplaced_transitionVia);
5315     ctxt->setContextProperty("useDisplaced", useDisplaced);
5316     ctxt->setContextProperty("displacedEnabled", displacedEnabled);
5317     ctxt->setContextProperty("useAddDisplaced", useAddDisplaced);
5318     ctxt->setContextProperty("addDisplacedEnabled", addDisplacedEnabled);
5319     ctxt->setContextProperty("useMoveDisplaced", useMoveDisplaced);
5320     ctxt->setContextProperty("moveDisplacedEnabled", moveDisplacedEnabled);
5321     ctxt->setContextProperty("useRemoveDisplaced", useRemoveDisplaced);
5322     ctxt->setContextProperty("removeDisplacedEnabled", removeDisplacedEnabled);
5323     canvas->setSource(testFileUrl("displacedTransitions.qml"));
5324     canvas->show();
5325     qApp->processEvents();
5326
5327     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5328     QTRY_VERIFY(gridview != 0);
5329     QQuickItem *contentItem = gridview->contentItem();
5330     QVERIFY(contentItem != 0);
5331     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5332
5333     QList<QPair<QString,QString> > expectedDisplacedValues = expectedDisplacedIndexes.getModelDataValues(model);
5334     gridview->setProperty("displaceTransitionsDone", false);
5335
5336     switch (change.type) {
5337         case ListChange::Inserted:
5338         {
5339             QList<QPair<QString, QString> > targetItemData;
5340             for (int i=change.index; i<change.index + change.count; ++i)
5341                 targetItemData << qMakePair(QString("new item %1").arg(i), QString::number(i));
5342             model.insertItems(change.index, targetItemData);
5343             QTRY_COMPARE(model.count(), gridview->count());
5344             break;
5345         }
5346         case ListChange::Removed:
5347             model.removeItems(change.index, change.count);
5348             QTRY_COMPARE(model.count(), gridview->count());
5349             break;
5350         case ListChange::Moved:
5351             model.moveItems(change.index, change.to, change.count);
5352             QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5353             break;
5354         case ListChange::SetCurrent:
5355         case ListChange::SetContentY:
5356             break;
5357     }
5358
5359     QVariantList resultTargetIndexes = gridview->property("displacedTargetIndexes").toList();
5360     QVariantList resultTargetItems = gridview->property("displacedTargetItems").toList();
5361
5362     if ((useDisplaced && displacedEnabled)
5363             || (useAddDisplaced && addDisplacedEnabled)
5364             || (useMoveDisplaced && moveDisplacedEnabled)
5365             || (useRemoveDisplaced && removeDisplacedEnabled)) {
5366         QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5367
5368         // check the correct number of target items and indexes were received
5369         QCOMPARE(resultTargetIndexes.count(), expectedDisplacedIndexes.count());
5370         for (int i=0; i<resultTargetIndexes.count(); i++)
5371             QCOMPARE(resultTargetIndexes[i].value<QList<int> >().count(), change.count);
5372         QCOMPARE(resultTargetItems.count(), expectedDisplacedIndexes.count());
5373         for (int i=0; i<resultTargetItems.count(); i++)
5374             QCOMPARE(resultTargetItems[i].toList().count(), change.count);
5375     } else {
5376         QCOMPARE(resultTargetIndexes.count(), 0);
5377         QCOMPARE(resultTargetItems.count(), 0);
5378     }
5379
5380     if (change.type == ListChange::Inserted && useAddDisplaced && addDisplacedEnabled)
5381         model_addDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with add displaced", "shouldn't have been animated with add displaced");
5382     else
5383         QCOMPARE(model_addDisplaced_transitionVia.count(), 0);
5384     if (change.type == ListChange::Moved && useMoveDisplaced && moveDisplacedEnabled)
5385         model_moveDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with move displaced", "shouldn't have been animated with move displaced");
5386     else
5387         QCOMPARE(model_moveDisplaced_transitionVia.count(), 0);
5388     if (change.type == ListChange::Removed && useRemoveDisplaced && removeDisplacedEnabled)
5389         model_removeDisplaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with remove displaced", "shouldn't have been animated with remove displaced");
5390     else
5391         QCOMPARE(model_removeDisplaced_transitionVia.count(), 0);
5392
5393     if (useDisplaced && displacedEnabled
5394             && ( (change.type == ListChange::Inserted && (!useAddDisplaced || !addDisplacedEnabled))
5395                  || (change.type == ListChange::Moved && (!useMoveDisplaced || !moveDisplacedEnabled))
5396                  || (change.type == ListChange::Removed && (!useRemoveDisplaced || !removeDisplacedEnabled))) ) {
5397         model_displaced_transitionVia.matchAgainst(expectedDisplacedValues, "wasn't animated with generic displaced", "shouldn't have been animated with generic displaced");
5398     } else {
5399         QCOMPARE(model_displaced_transitionVia.count(), 0);
5400     }
5401
5402     // verify all items moved to the correct final positions
5403     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5404     for (int i=0; i < model.count() && i < items.count(); ++i) {
5405         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5406         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5407         QCOMPARE(item->x(), (i%3)*80.0);
5408         QCOMPARE(item->y(), (i/3)*60.0);
5409         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5410         QVERIFY(name != 0);
5411         QTRY_COMPARE(name->text(), model.name(i));
5412     }
5413
5414     releaseView(canvas);
5415 }
5416
5417 void tst_QQuickGridView::displacedTransitions_data()
5418 {
5419     QTest::addColumn<bool>("useDisplaced");
5420     QTest::addColumn<bool>("displacedEnabled");
5421     QTest::addColumn<bool>("useAddDisplaced");
5422     QTest::addColumn<bool>("addDisplacedEnabled");
5423     QTest::addColumn<bool>("useMoveDisplaced");
5424     QTest::addColumn<bool>("moveDisplacedEnabled");
5425     QTest::addColumn<bool>("useRemoveDisplaced");
5426     QTest::addColumn<bool>("removeDisplacedEnabled");
5427     QTest::addColumn<ListChange>("change");
5428     QTest::addColumn<ListRange>("expectedDisplacedIndexes");
5429
5430     QTest::newRow("no displaced transitions at all")
5431             << false << false
5432             << false << false
5433             << false << false
5434             << false << false
5435             << ListChange::insert(0, 1) << ListRange(0, 17);
5436
5437     QTest::newRow("just displaced")
5438             << true << true
5439             << false << false
5440             << false << false
5441             << false << false
5442             << ListChange::insert(0, 1) << ListRange(0, 17);
5443
5444     QTest::newRow("just displaced (not enabled)")
5445             << true << false
5446             << false << false
5447             << false << false
5448             << false << false
5449             << ListChange::insert(0, 1) << ListRange(0, 17);
5450
5451     QTest::newRow("displaced + addDisplaced")
5452             << true << true
5453             << true << true
5454             << false << false
5455             << false << false
5456             << ListChange::insert(0, 1) << ListRange(0, 17);
5457
5458     QTest::newRow("displaced + addDisplaced (not enabled)")
5459             << true << true
5460             << true << false
5461             << false << false
5462             << false << false
5463             << ListChange::insert(0, 1) << ListRange(0, 17);
5464
5465     QTest::newRow("displaced + moveDisplaced")
5466             << true << true
5467             << false << false
5468             << true << true
5469             << false << false
5470             << ListChange::move(0, 10, 1) << ListRange(1, 10);
5471
5472     QTest::newRow("displaced + moveDisplaced (not enabled)")
5473             << true << true
5474             << false << false
5475             << true << false
5476             << false << false
5477             << ListChange::move(0, 10, 1) << ListRange(1, 10);
5478
5479     QTest::newRow("displaced + removeDisplaced")
5480             << true << true
5481             << false << false
5482             << false << false
5483             << true << true
5484             << ListChange::remove(0, 1) << ListRange(1, 18);
5485
5486     QTest::newRow("displaced + removeDisplaced (not enabled)")
5487             << true << true
5488             << false << false
5489             << false << false
5490             << true << false
5491             << ListChange::remove(0, 1) << ListRange(1, 18);
5492
5493
5494     QTest::newRow("displaced + add, should use generic displaced for a remove")
5495             << true << true
5496             << true << true
5497             << false << false
5498             << true << false
5499             << ListChange::remove(0, 1) << ListRange(1, 18);
5500 }
5501
5502 void tst_QQuickGridView::multipleTransitions()
5503 {
5504     // Tests that if you interrupt a transition in progress with another action that
5505     // cancels the previous transition, the resulting items are still placed correctly.
5506
5507     QFETCH(int, initialCount);
5508     QFETCH(qreal, contentY);
5509     QFETCH(QList<ListChange>, changes);
5510     QFETCH(bool, enableAddTransitions);
5511     QFETCH(bool, enableMoveTransitions);
5512     QFETCH(bool, enableRemoveTransitions);
5513     QFETCH(bool, rippleAddDisplaced);
5514
5515     // add transitions on the left, moves on the right
5516     QPointF addTargets_transitionFrom(-50, -50);
5517     QPointF addDisplaced_transitionFrom(-50, 50);
5518     QPointF moveTargets_transitionFrom(50, -50);
5519     QPointF moveDisplaced_transitionFrom(50, 50);
5520     QPointF removeTargets_transitionTo(-100, 300);
5521     QPointF removeDisplaced_transitionFrom(100, 300);
5522
5523     QmlListModel model;
5524     for (int i = 0; i < initialCount; i++)
5525         model.addItem("Original item" + QString::number(i), "");
5526
5527     QQuickView *canvas = getView();
5528     QQmlContext *ctxt = canvas->rootContext();
5529     ctxt->setContextProperty("testModel", &model);
5530     ctxt->setContextProperty("addTargets_transitionFrom", addTargets_transitionFrom);
5531     ctxt->setContextProperty("addDisplaced_transitionFrom", addDisplaced_transitionFrom);
5532     ctxt->setContextProperty("moveTargets_transitionFrom", moveTargets_transitionFrom);
5533     ctxt->setContextProperty("moveDisplaced_transitionFrom", moveDisplaced_transitionFrom);
5534     ctxt->setContextProperty("removeTargets_transitionTo", removeTargets_transitionTo);
5535     ctxt->setContextProperty("removeDisplaced_transitionFrom", removeDisplaced_transitionFrom);
5536     ctxt->setContextProperty("enableAddTransitions", enableAddTransitions);
5537     ctxt->setContextProperty("enableMoveTransitions", enableMoveTransitions);
5538     ctxt->setContextProperty("enableRemoveTransitions", enableRemoveTransitions);
5539     ctxt->setContextProperty("rippleAddDisplaced", rippleAddDisplaced);
5540     canvas->setSource(testFileUrl("multipleTransitions.qml"));
5541     canvas->show();
5542     QTest::qWaitForWindowShown(canvas);
5543
5544     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5545     QTRY_VERIFY(gridview != 0);
5546     QQuickItem *contentItem = gridview->contentItem();
5547     QVERIFY(contentItem != 0);
5548     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5549
5550     if (contentY != 0) {
5551         gridview->setContentY(contentY);
5552         QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5553     }
5554
5555     int timeBetweenActions = canvas->rootObject()->property("timeBetweenActions").toInt();
5556
5557     for (int i=0; i<changes.count(); i++) {
5558         switch (changes[i].type) {
5559             case ListChange::Inserted:
5560             {
5561                 QList<QPair<QString, QString> > targetItems;
5562                 for (int j=changes[i].index; j<changes[i].index + changes[i].count; ++j)
5563                     targetItems << qMakePair(QString("new item %1").arg(j), QString::number(j));
5564                 model.insertItems(changes[i].index, targetItems);
5565                 QTRY_COMPARE(model.count(), gridview->count());
5566                 if (i == changes.count() - 1) {
5567                     QTRY_VERIFY(!gridview->property("runningAddTargets").toBool());
5568                     QTRY_VERIFY(!gridview->property("runningAddDisplaced").toBool());
5569                 } else {
5570                     QTest::qWait(timeBetweenActions);
5571                 }
5572                 break;
5573             }
5574             case ListChange::Removed:
5575                 model.removeItems(changes[i].index, changes[i].count);
5576                 QTRY_COMPARE(model.count(), gridview->count());
5577                 if (i == changes.count() - 1) {
5578                     QTRY_VERIFY(!gridview->property("runningRemoveTargets").toBool());
5579                     QTRY_VERIFY(!gridview->property("runningRemoveDisplaced").toBool());
5580                 } else {
5581                     QTest::qWait(timeBetweenActions);
5582                 }
5583                 break;
5584             case ListChange::Moved:
5585                 model.moveItems(changes[i].index, changes[i].to, changes[i].count);
5586                 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5587                 if (i == changes.count() - 1) {
5588                     QTRY_VERIFY(!gridview->property("runningMoveTargets").toBool());
5589                     QTRY_VERIFY(!gridview->property("runningMoveDisplaced").toBool());
5590                 } else {
5591                     QTest::qWait(timeBetweenActions);
5592                 }
5593                 break;
5594             case ListChange::SetCurrent:
5595                 gridview->setCurrentIndex(changes[i].index);
5596                 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5597                 break;
5598             case ListChange::SetContentY:
5599                 gridview->setContentY(changes[i].pos);
5600                 QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5601                 break;
5602         }
5603     }
5604     QCOMPARE(gridview->count(), model.count());
5605
5606     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5607     int firstVisibleIndex = -1;
5608     for (int i=0; i<items.count(); i++) {
5609         if (items[i]->y() >= contentY) {
5610             QQmlExpression e(qmlContext(items[i]), items[i], "index");
5611             firstVisibleIndex = e.evaluate().toInt();
5612             break;
5613         }
5614     }
5615     QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
5616
5617     // verify all items moved to the correct final positions
5618     int itemCount = findItems<QQuickItem>(contentItem, "wrapper").count();
5619     for (int i=firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
5620         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5621         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5622         QTRY_COMPARE(item->x(), (i%3)*80.0);
5623         QTRY_COMPARE(item->y(), (i/3)*60.0);
5624         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5625         QVERIFY(name != 0);
5626         QTRY_COMPARE(name->text(), model.name(i));
5627     }
5628
5629     releaseView(canvas);
5630 }
5631
5632 void tst_QQuickGridView::multipleTransitions_data()
5633 {
5634     QTest::addColumn<int>("initialCount");
5635     QTest::addColumn<qreal>("contentY");
5636     QTest::addColumn<QList<ListChange> >("changes");
5637     QTest::addColumn<bool>("enableAddTransitions");
5638     QTest::addColumn<bool>("enableMoveTransitions");
5639     QTest::addColumn<bool>("enableRemoveTransitions");
5640     QTest::addColumn<bool>("rippleAddDisplaced");
5641
5642     // the added item and displaced items should move to final dest correctly
5643     QTest::newRow("add item, then move it immediately") << 10 << 0.0 << (QList<ListChange>()
5644              << ListChange::insert(0, 1)
5645              << ListChange::move(0, 3, 1)
5646              )
5647              << true << true << true << false;
5648
5649     // items affected by the add should change from move to add transition
5650     QTest::newRow("move, then insert item before the moved item") << 20 << 0.0 << (QList<ListChange>()
5651             << ListChange::move(1, 10, 3)
5652             << ListChange::insert(0, 1)
5653             )
5654             << true << true << true << false;
5655
5656     // items should be placed correctly if you trigger a transition then refill for that index
5657     QTest::newRow("add at 0, flick down, flick back to top and add at 0 again") << 20 << 0.0 << (QList<ListChange>()
5658             << ListChange::insert(0, 1)
5659             << ListChange::setContentY(160.0)
5660             << ListChange::setContentY(0.0)
5661             << ListChange::insert(0, 1)
5662             )
5663             << true << true << true << false;
5664
5665     QTest::newRow("insert then remove same index, with ripple effect on add displaced") << 20 << 0.0 << (QList<ListChange>()
5666             << ListChange::insert(1, 1)
5667             << ListChange::remove(1, 1)
5668             )
5669             << true << true << true << true;
5670
5671     // if item is removed while undergoing a displaced transition, all other items should end up at their correct positions,
5672     // even if a remove-displace transition is not present to re-animate them
5673     QTest::newRow("insert then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5674             << ListChange::insert(0, 1)
5675             << ListChange::remove(2, 1)
5676             )
5677             << true << true << false << false;
5678
5679     // if last item is not flush with the edge of the view, it should still be refilled in correctly after a
5680     // remove has changed the position of where it will move to
5681     QTest::newRow("insert twice then remove, with remove disabled") << 20 << 0.0 << (QList<ListChange>()
5682             << ListChange::setContentY(-10.0)
5683             << ListChange::insert(0, 1)
5684             << ListChange::insert(0, 1)
5685             << ListChange::remove(2, 1)
5686             )
5687             << true << true << false << false;
5688 }
5689
5690 void tst_QQuickGridView::multipleDisplaced()
5691 {
5692     // multiple move() operations should only restart displace transitions for items that
5693     // moved from previously set positions, and not those that have moved from their current
5694     // item positions (which may e.g. still be changing from easing bounces in the last transition)
5695
5696     QmlListModel model;
5697     for (int i = 0; i < 30; i++)
5698         model.addItem("Original item" + QString::number(i), "");
5699
5700     QQuickView *canvas = createView();
5701     QQmlContext *ctxt = canvas->rootContext();
5702     ctxt->setContextProperty("testModel", &model);
5703     canvas->setSource(testFileUrl("multipleDisplaced.qml"));
5704     canvas->show();
5705     QTest::qWaitForWindowShown(canvas);
5706
5707     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5708     QTRY_VERIFY(gridview != 0);
5709     QQuickItem *contentItem = gridview->contentItem();
5710     QVERIFY(contentItem != 0);
5711     QTRY_COMPARE(QQuickItemPrivate::get(gridview)->polishScheduled, false);
5712
5713     model.moveItems(12, 8, 1);
5714     QTest::qWait(canvas->rootObject()->property("duration").toInt() / 2);
5715     model.moveItems(8, 3, 1);
5716     QTRY_VERIFY(gridview->property("displaceTransitionsDone").toBool());
5717
5718     QVariantMap transitionsStarted = gridview->property("displaceTransitionsStarted").toMap();
5719     foreach (const QString &name, transitionsStarted.keys()) {
5720         QVERIFY2(transitionsStarted[name] == 1,
5721                  QTest::toString(QString("%1 was displaced %2 times").arg(name).arg(transitionsStarted[name].toInt())));
5722     }
5723
5724     // verify all items moved to the correct final positions
5725     QList<QQuickItem*> items = findItems<QQuickItem>(contentItem, "wrapper");
5726     for (int i=0; i < model.count() && i < items.count(); ++i) {
5727         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5728         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
5729         QTRY_COMPARE(item->x(), (i%3)*80.0);
5730         QTRY_COMPARE(item->y(), (i/3)*60.0);
5731         QQuickText *name = findItem<QQuickText>(contentItem, "textName", i);
5732         QVERIFY(name != 0);
5733         QTRY_COMPARE(name->text(), model.name(i));
5734     }
5735
5736     delete canvas;
5737 }
5738
5739 void tst_QQuickGridView::cacheBuffer()
5740 {
5741     QQuickView *canvas = createView();
5742
5743     QaimModel model;
5744     for (int i = 0; i < 90; i++)
5745         model.addItem("Item" + QString::number(i), "");
5746
5747     canvas->rootContext()->setContextProperty("testModel", &model);
5748     canvas->setSource(testFileUrl("gridview1.qml"));
5749     canvas->show();
5750     qApp->processEvents();
5751
5752     QQuickGridView *gridview = findItem<QQuickGridView>(canvas->rootObject(), "grid");
5753     QVERIFY(gridview != 0);
5754
5755     QQuickItem *contentItem = gridview->contentItem();
5756     QVERIFY(contentItem != 0);
5757     QVERIFY(gridview->delegate() != 0);
5758     QVERIFY(gridview->model() != 0);
5759
5760     // Confirm items positioned correctly
5761     int itemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5762     for (int i = 0; i < model.count() && i < itemCount; ++i) {
5763         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5764         QTRY_COMPARE(item->x(), (i%3)*80.0);
5765         QTRY_COMPARE(item->y(), (i/3)*60.0);
5766     }
5767
5768     QQmlIncubationController controller;
5769     canvas->engine()->setIncubationController(&controller);
5770
5771     canvas->rootObject()->setProperty("cacheBuffer", 200);
5772     QTRY_VERIFY(gridview->cacheBuffer() == 200);
5773
5774     // items will be created one at a time
5775     for (int i = itemCount; i < qMin(itemCount+9,model.count()); ++i) {
5776         QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5777         QQuickItem *item = 0;
5778         while (!item) {
5779             bool b = false;
5780             controller.incubateWhile(&b);
5781             item = findItem<QQuickItem>(gridview, "wrapper", i);
5782         }
5783     }
5784
5785     {
5786         bool b = true;
5787         controller.incubateWhile(&b);
5788     }
5789
5790     int newItemCount = 0;
5791     newItemCount = findItems<QQuickItem>(contentItem, "wrapper", false).count();
5792
5793     // Confirm items positioned correctly
5794     for (int i = 0; i < model.count() && i < newItemCount; ++i) {
5795         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5796         QVERIFY(item);
5797         QTRY_COMPARE(item->x(), (i%3)*80.0);
5798         QTRY_COMPARE(item->y(), (i/3)*60.0);
5799     }
5800
5801     // move view and confirm items in view are visible immediately and outside are created async
5802     gridview->setContentY(300);
5803
5804     for (int i = 15; i < 34; ++i) { // 34 due to staggered item creation
5805         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5806         QVERIFY(item);
5807         QTRY_COMPARE(item->x(), (i%3)*80.0);
5808         QTRY_COMPARE(item->y(), (i/3)*60.0);
5809     }
5810
5811     QVERIFY(findItem<QQuickItem>(gridview, "wrapper", 34) == 0);
5812
5813     // ensure buffered items are created
5814     for (int i = 34; i < qMin(44,model.count()); ++i) {
5815         QQuickItem *item = 0;
5816         while (!item) {
5817             qGuiApp->processEvents(); // allow refill to happen
5818             bool b = false;
5819             controller.incubateWhile(&b);
5820             item = findItem<QQuickItem>(gridview, "wrapper", i);
5821         }
5822     }
5823
5824     {
5825         bool b = true;
5826         controller.incubateWhile(&b);
5827     }
5828
5829     delete canvas;
5830 }
5831
5832 void tst_QQuickGridView::asynchronous()
5833 {
5834     QQuickView *canvas = createView();
5835     canvas->show();
5836     QQmlIncubationController controller;
5837     canvas->engine()->setIncubationController(&controller);
5838
5839     canvas->setSource(testFileUrl("asyncloader.qml"));
5840
5841     QQuickItem *rootObject = qobject_cast<QQuickItem*>(canvas->rootObject());
5842     QVERIFY(rootObject);
5843
5844     QQuickGridView *gridview = 0;
5845     while (!gridview) {
5846         bool b = false;
5847         controller.incubateWhile(&b);
5848         gridview = rootObject->findChild<QQuickGridView*>("view");
5849     }
5850
5851     // items will be created one at a time
5852     for (int i = 0; i < 12; ++i) {
5853         QVERIFY(findItem<QQuickItem>(gridview, "wrapper", i) == 0);
5854         QQuickItem *item = 0;
5855         while (!item) {
5856             bool b = false;
5857             controller.incubateWhile(&b);
5858             item = findItem<QQuickItem>(gridview, "wrapper", i);
5859         }
5860     }
5861
5862     {
5863         bool b = true;
5864         controller.incubateWhile(&b);
5865     }
5866
5867     // verify positioning
5868     QQuickItem *contentItem = gridview->contentItem();
5869     for (int i = 0; i < 12; ++i) {
5870         QQuickItem *item = findItem<QQuickItem>(contentItem, "wrapper", i);
5871         if (!item) qWarning() << "Item" << i << "not found";
5872         QVERIFY(item->x() == (i%3)*100);
5873         QVERIFY(item->y() == (i/3)*100);
5874     }
5875
5876     delete canvas;
5877 }
5878
5879 void tst_QQuickGridView::unrequestedVisibility()
5880 {
5881     QaimModel model;
5882     for (int i = 0; i < 30; i++)
5883         model.addItem("Item" + QString::number(i), QString::number(i));
5884
5885     QQuickView *canvas = new QQuickView(0);
5886     canvas->setGeometry(0,0,240,320);
5887
5888     QQmlContext *ctxt = canvas->rootContext();
5889     ctxt->setContextProperty("testModel", &model);
5890     ctxt->setContextProperty("testWrap", QVariant(false));
5891
5892     canvas->setSource(testFileUrl("unrequestedItems.qml"));
5893
5894     canvas->show();
5895
5896     qApp->processEvents();
5897
5898     QQuickGridView *leftview = findItem<QQuickGridView>(canvas->rootObject(), "leftGrid");
5899     QTRY_VERIFY(leftview != 0);
5900
5901     QQuickGridView *rightview = findItem<QQuickGridView>(canvas->rootObject(), "rightGrid");
5902     QTRY_VERIFY(rightview != 0);
5903
5904     QQuickItem *leftContent = leftview->contentItem();
5905     QTRY_VERIFY(leftContent != 0);
5906
5907     QQuickItem *rightContent = rightview->contentItem();
5908     QTRY_VERIFY(rightContent != 0);
5909
5910     rightview->setCurrentIndex(12);
5911
5912     QTRY_COMPARE(leftview->contentY(), 0.0);
5913     QTRY_COMPARE(rightview->contentY(), 240.0);
5914
5915     QQuickItem *item;
5916
5917     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5918     QCOMPARE(item->isVisible(), true);
5919     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5920     QCOMPARE(item->isVisible(), false);
5921
5922     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5923     QCOMPARE(item->isVisible(), false);
5924     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5925     QCOMPARE(item->isVisible(), true);
5926
5927     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 9));
5928     QCOMPARE(item->isVisible(), true);
5929     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 10));
5930     QCOMPARE(item->isVisible(), false);
5931     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 3));
5932     QCOMPARE(item->isVisible(), false);
5933     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 4));
5934     QCOMPARE(item->isVisible(), true);
5935
5936     rightview->setCurrentIndex(0);
5937
5938     QTRY_COMPARE(leftview->contentY(), 0.0);
5939     QTRY_COMPARE(rightview->contentY(), 0.0);
5940
5941     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5942     QCOMPARE(item->isVisible(), true);
5943     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5944     QTRY_COMPARE(item->isVisible(), true);
5945
5946     QVERIFY(!findItem<QQuickItem>(leftContent, "wrapper", 11));
5947     QVERIFY(!findItem<QQuickItem>(rightContent, "wrapper", 11));
5948
5949     leftview->setCurrentIndex(12);
5950
5951     QTRY_COMPARE(leftview->contentY(), 240.0);
5952     QTRY_COMPARE(rightview->contentY(), 0.0);
5953
5954     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5955     QTRY_COMPARE(item->isVisible(), false);
5956     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5957     QCOMPARE(item->isVisible(), true);
5958
5959     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5960     QCOMPARE(item->isVisible(), true);
5961     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5962     QCOMPARE(item->isVisible(), false);
5963
5964     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5965     QCOMPARE(item->isVisible(), false);
5966     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5967     QCOMPARE(item->isVisible(), true);
5968     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5969     QCOMPARE(item->isVisible(), true);
5970     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5971     QCOMPARE(item->isVisible(), false);
5972
5973     // move a non-visible item into view
5974     model.moveItems(10, 9, 1);
5975     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5976
5977     QTRY_VERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 1));
5978     QCOMPARE(item->isVisible(), false);
5979     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 1));
5980     QCOMPARE(item->isVisible(), true);
5981
5982     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 11));
5983     QCOMPARE(item->isVisible(), true);
5984     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 11));
5985     QCOMPARE(item->isVisible(), false);
5986
5987     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
5988     QCOMPARE(item->isVisible(), false);
5989     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
5990     QCOMPARE(item->isVisible(), true);
5991     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
5992     QCOMPARE(item->isVisible(), true);
5993     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
5994     QCOMPARE(item->isVisible(), false);
5995
5996     // move a visible item out of view
5997     model.moveItems(5, 3, 1);
5998     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
5999
6000     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6001     QCOMPARE(item->isVisible(), false);
6002     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6003     QCOMPARE(item->isVisible(), true);
6004     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6005     QCOMPARE(item->isVisible(), true);
6006     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6007     QCOMPARE(item->isVisible(), false);
6008
6009     // move a non-visible item into view
6010     model.moveItems(3, 5, 1);
6011     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6012
6013     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6014     QCOMPARE(item->isVisible(), false);
6015     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6016     QCOMPARE(item->isVisible(), true);
6017     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6018     QCOMPARE(item->isVisible(), true);
6019     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6020     QCOMPARE(item->isVisible(), false);
6021
6022     // move a visible item out of view
6023     model.moveItems(9, 10, 1);
6024     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6025
6026     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6027     QCOMPARE(item->isVisible(), false);
6028     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6029     QCOMPARE(item->isVisible(), true);
6030     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6031     QCOMPARE(item->isVisible(), true);
6032     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6033     QCOMPARE(item->isVisible(), false);
6034
6035     // move a non-visible item into view
6036     model.moveItems(10, 9, 1);
6037     QTRY_COMPARE(QQuickItemPrivate::get(leftview)->polishScheduled, false);
6038
6039     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 3));
6040     QCOMPARE(item->isVisible(), false);
6041     QVERIFY(item = findItem<QQuickItem>(leftContent, "wrapper", 5));
6042     QCOMPARE(item->isVisible(), true);
6043     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 9));
6044     QCOMPARE(item->isVisible(), true);
6045     QVERIFY(item = findItem<QQuickItem>(rightContent, "wrapper", 10));
6046     QCOMPARE(item->isVisible(), false);
6047
6048     delete canvas;
6049 }
6050
6051
6052 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB()
6053 {
6054     inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6055 }
6056
6057 void tst_QQuickGridView::inserted_leftToRight_RtL_TtB_data()
6058 {
6059     inserted_defaultLayout_data();
6060 }
6061
6062 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB()
6063 {
6064     inserted_defaultLayout(QQuickGridView::FlowTopToBottom);
6065 }
6066
6067 void tst_QQuickGridView::inserted_topToBottom_LtR_TtB_data()
6068 {
6069     inserted_defaultLayout_data();
6070 }
6071
6072 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB()
6073 {
6074     inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6075 }
6076
6077 void tst_QQuickGridView::inserted_topToBottom_RtL_TtB_data()
6078 {
6079     inserted_defaultLayout_data();
6080 }
6081
6082 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT()
6083 {
6084     inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6085 }
6086
6087 void tst_QQuickGridView::inserted_leftToRight_LtR_BtT_data()
6088 {
6089     inserted_defaultLayout_data();
6090 }
6091
6092 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT()
6093 {
6094     inserted_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6095 }
6096
6097 void tst_QQuickGridView::inserted_leftToRight_RtL_BtT_data()
6098 {
6099     inserted_defaultLayout_data();
6100 }
6101
6102 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT()
6103 {
6104     inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6105 }
6106
6107 void tst_QQuickGridView::inserted_topToBottom_LtR_BtT_data()
6108 {
6109     inserted_defaultLayout_data();
6110 }
6111
6112 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT()
6113 {
6114     inserted_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6115 }
6116
6117 void tst_QQuickGridView::inserted_topToBottom_RtL_BtT_data()
6118 {
6119     inserted_defaultLayout_data();
6120 }
6121
6122
6123 void tst_QQuickGridView::removed_leftToRight_RtL_TtB()
6124 {
6125     removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6126 }
6127
6128 void tst_QQuickGridView::removed_leftToRight_RtL_TtB_data()
6129 {
6130     removed_defaultLayout_data();
6131 }
6132
6133 void tst_QQuickGridView::removed_topToBottom_LtR_TtB()
6134 {
6135     removed_defaultLayout(QQuickGridView::FlowTopToBottom);
6136 }
6137
6138 void tst_QQuickGridView::removed_topToBottom_LtR_TtB_data()
6139 {
6140     removed_defaultLayout_data();
6141 }
6142
6143 void tst_QQuickGridView::removed_topToBottom_RtL_TtB()
6144 {
6145     removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6146 }
6147
6148 void tst_QQuickGridView::removed_topToBottom_RtL_TtB_data()
6149 {
6150     removed_defaultLayout_data();
6151 }
6152
6153 void tst_QQuickGridView::removed_leftToRight_LtR_BtT()
6154 {
6155     removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6156 }
6157
6158 void tst_QQuickGridView::removed_leftToRight_LtR_BtT_data()
6159 {
6160     removed_defaultLayout_data();
6161 }
6162
6163 void tst_QQuickGridView::removed_leftToRight_RtL_BtT()
6164 {
6165     removed_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6166 }
6167
6168 void tst_QQuickGridView::removed_leftToRight_RtL_BtT_data()
6169 {
6170     removed_defaultLayout_data();
6171 }
6172
6173 void tst_QQuickGridView::removed_topToBottom_LtR_BtT()
6174 {
6175     removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6176 }
6177
6178 void tst_QQuickGridView::removed_topToBottom_LtR_BtT_data()
6179 {
6180     removed_defaultLayout_data();
6181 }
6182
6183 void tst_QQuickGridView::removed_topToBottom_RtL_BtT()
6184 {
6185     removed_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6186 }
6187
6188 void tst_QQuickGridView::removed_topToBottom_RtL_BtT_data()
6189 {
6190     removed_defaultLayout_data();
6191 }
6192
6193
6194 void tst_QQuickGridView::moved_leftToRight_RtL_TtB()
6195 {
6196     moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft);
6197 }
6198
6199 void tst_QQuickGridView::moved_leftToRight_RtL_TtB_data()
6200 {
6201     moved_defaultLayout_data();
6202 }
6203
6204 void tst_QQuickGridView::moved_topToBottom_LtR_TtB()
6205 {
6206     moved_defaultLayout(QQuickGridView::FlowTopToBottom);
6207 }
6208
6209 void tst_QQuickGridView::moved_topToBottom_LtR_TtB_data()
6210 {
6211     moved_defaultLayout_data();
6212 }
6213
6214 void tst_QQuickGridView::moved_topToBottom_RtL_TtB()
6215 {
6216     moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft);
6217 }
6218
6219 void tst_QQuickGridView::moved_topToBottom_RtL_TtB_data()
6220 {
6221     moved_defaultLayout_data();
6222 }
6223
6224 void tst_QQuickGridView::moved_leftToRight_LtR_BtT()
6225 {
6226     moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::LeftToRight, QQuickItemView::BottomToTop);
6227 }
6228
6229 void tst_QQuickGridView::moved_leftToRight_LtR_BtT_data()
6230 {
6231     moved_defaultLayout_data();
6232 }
6233
6234 void tst_QQuickGridView::moved_leftToRight_RtL_BtT()
6235 {
6236     moved_defaultLayout(QQuickGridView::FlowLeftToRight, Qt::RightToLeft, QQuickItemView::BottomToTop);
6237 }
6238
6239 void tst_QQuickGridView::moved_leftToRight_RtL_BtT_data()
6240 {
6241     moved_defaultLayout_data();
6242 }
6243
6244 void tst_QQuickGridView::moved_topToBottom_LtR_BtT()
6245 {
6246     moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::LeftToRight, QQuickItemView::BottomToTop);
6247 }
6248
6249 void tst_QQuickGridView::moved_topToBottom_LtR_BtT_data()
6250 {
6251     moved_defaultLayout_data();
6252 }
6253
6254 void tst_QQuickGridView::moved_topToBottom_RtL_BtT()
6255 {
6256     moved_defaultLayout(QQuickGridView::FlowTopToBottom, Qt::RightToLeft, QQuickItemView::BottomToTop);
6257 }
6258
6259 void tst_QQuickGridView::moved_topToBottom_RtL_BtT_data()
6260 {
6261     moved_defaultLayout_data();
6262 }
6263
6264
6265 QList<int> tst_QQuickGridView::toIntList(const QVariantList &list)
6266 {
6267     QList<int> ret;
6268     bool ok = true;
6269     for (int i=0; i<list.count(); i++) {
6270         ret << list[i].toInt(&ok);
6271         if (!ok)
6272             qWarning() << "tst_QQuickGridView::toIntList(): not a number:" << list[i];
6273     }
6274
6275     return ret;
6276 }
6277
6278 void tst_QQuickGridView::matchIndexLists(const QVariantList &indexLists, const QList<int> &expectedIndexes)
6279 {
6280     for (int i=0; i<indexLists.count(); i++) {
6281         QSet<int> current = indexLists[i].value<QList<int> >().toSet();
6282         if (current != expectedIndexes.toSet())
6283             qDebug() << "Cannot match actual targets" << current << "with expected" << expectedIndexes;
6284         QCOMPARE(current, expectedIndexes.toSet());
6285     }
6286 }
6287
6288 void tst_QQuickGridView::matchItemsAndIndexes(const QVariantMap &items, const QaimModel &model, const QList<int> &expectedIndexes)
6289 {
6290     for (QVariantMap::const_iterator it = items.begin(); it != items.end(); ++it) {
6291         QVERIFY(it.value().type() == QVariant::Int);
6292         QString name = it.key();
6293         int itemIndex = it.value().toInt();
6294         QVERIFY2(expectedIndexes.contains(itemIndex), QTest::toString(QString("Index %1 not found in expectedIndexes").arg(itemIndex)));
6295         if (model.name(itemIndex) != name)
6296             qDebug() << itemIndex;
6297         QCOMPARE(model.name(itemIndex), name);
6298     }
6299     QCOMPARE(items.count(), expectedIndexes.count());
6300 }
6301
6302 void tst_QQuickGridView::matchItemLists(const QVariantList &itemLists, const QList<QQuickItem *> &expectedItems)
6303 {
6304     for (int i=0; i<itemLists.count(); i++) {
6305         QVariantList current = itemLists[i].toList();
6306         for (int j=0; j<current.count(); j++) {
6307             QQuickItem *o = qobject_cast<QQuickItem*>(current[j].value<QObject*>());
6308             QVERIFY2(o, QTest::toString(QString("Invalid actual item at %1").arg(j)));
6309             QVERIFY2(expectedItems.contains(o), QTest::toString(QString("Cannot match item %1").arg(j)));
6310         }
6311         QCOMPARE(current.count(), expectedItems.count());
6312     }
6313 }
6314
6315 QTEST_MAIN(tst_QQuickGridView)
6316
6317 #include "tst_qquickgridview.moc"
6318