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