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