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