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