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