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