c478f0dab30ff2aad2322d2799fdc0c769a3d5cb
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qsggridview / tst_qsggridview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QtGui/qstringlistmodel.h>
44 #include <QtDeclarative/qsgview.h>
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativecomponent.h>
47 #include <QtDeclarative/qdeclarativecontext.h>
48 #include <QtDeclarative/qdeclarativeexpression.h>
49 #include <QtDeclarative/private/qsgitem_p.h>
50 #include <QtDeclarative/private/qlistmodelinterface_p.h>
51 #include <QtDeclarative/private/qsggridview_p.h>
52 #include <QtDeclarative/private/qsgtext_p.h>
53 #include <QtDeclarative/private/qdeclarativelistmodel_p.h>
54 #include "../../../shared/util.h"
55 #include <QtOpenGL/QGLShaderProgram>
56
57 #ifdef Q_OS_SYMBIAN
58 // In Symbian OS test data is located in applications private dir
59 #define SRCDIR "."
60 #endif
61
62 Q_DECLARE_METATYPE(Qt::LayoutDirection)
63 Q_DECLARE_METATYPE(QSGGridView::Flow)
64
65 class tst_QSGGridView : public QObject
66 {
67     Q_OBJECT
68 public:
69     tst_QSGGridView();
70
71 private slots:
72     void initTestCase();
73     void cleanupTestCase();
74     void items();
75     void changed();
76     void inserted();
77     void removed();
78     void clear();
79     void moved();
80     void moved_data();
81     void swapWithFirstItem();
82     void changeFlow();
83     void currentIndex();
84     void noCurrentIndex();
85     void defaultValues();
86     void properties();
87     void propertyChanges();
88     void componentChanges();
89     void modelChanges();
90     void positionViewAtIndex();
91     void positionViewAtIndex_rightToLeft();
92     void mirroring();
93     void snapping();
94     void resetModel();
95     void enforceRange();
96     void enforceRange_rightToLeft();
97     void QTBUG_8456();
98     void manualHighlight();
99     void footer();
100     void footer_data();
101     void header();
102     void header_data();
103     void indexAt();
104     void onAdd();
105     void onAdd_data();
106     void onRemove();
107     void onRemove_data();
108     void testQtQuick11Attributes();
109     void testQtQuick11Attributes_data();
110
111 private:
112     QSGView *createView();
113     template<typename T>
114     T *findItem(QSGItem *parent, const QString &id, int index=-1);
115     template<typename T>
116     QList<T*> findItems(QSGItem *parent, const QString &objectName);
117     void dumpTree(QSGItem *parent, int depth = 0);
118 };
119
120 template<typename T>
121 void tst_qsggridview_move(int from, int to, int n, T *items)
122 {
123     if (n == 1) {
124         items->move(from, to);
125     } else {
126         T replaced;
127         int i=0;
128         typename T::ConstIterator it=items->begin(); it += from+n;
129         for (; i<to-from; ++i,++it)
130             replaced.append(*it);
131         i=0;
132         it=items->begin(); it += from;
133         for (; i<n; ++i,++it)
134             replaced.append(*it);
135         typename T::ConstIterator f=replaced.begin();
136         typename T::Iterator t=items->begin(); t += from;
137         for (; f != replaced.end(); ++f, ++t)
138             *t = *f;
139     }
140 }
141
142 void tst_QSGGridView::initTestCase()
143 {
144     QSGView canvas;
145     if (!QGLShaderProgram::hasOpenGLShaderPrograms(canvas.context()))
146         QSKIP("QSGGridView needs OpenGL 2.0", SkipAll);
147 }
148
149 void tst_QSGGridView::cleanupTestCase()
150 {
151
152 }
153 class TestModel : public QAbstractListModel
154 {
155 public:
156     enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 };
157
158     TestModel(QObject *parent=0) : QAbstractListModel(parent) {
159         QHash<int, QByteArray> roles;
160         roles[Name] = "name";
161         roles[Number] = "number";
162         setRoleNames(roles);
163     }
164
165     int rowCount(const QModelIndex &parent=QModelIndex()) const { Q_UNUSED(parent); return list.count(); }
166     QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
167         QVariant rv;
168         if (role == Name)
169             rv = list.at(index.row()).first;
170         else if (role == Number)
171             rv = list.at(index.row()).second;
172
173         return rv;
174     }
175
176     int count() const { return rowCount(); }
177     QString name(int index) const { return list.at(index).first; }
178     QString number(int index) const { return list.at(index).second; }
179
180     void addItem(const QString &name, const QString &number) {
181         emit beginInsertRows(QModelIndex(), list.count(), list.count());
182         list.append(QPair<QString,QString>(name, number));
183         emit endInsertRows();
184     }
185
186     void addItems(const QList<QPair<QString, QString> > &items) {
187         emit beginInsertRows(QModelIndex(), list.count(), list.count()+items.count()-1);
188         for (int i=0; i<items.count(); i++)
189             list.append(QPair<QString,QString>(items[i].first, items[i].second));
190         emit endInsertRows();
191     }
192
193     void insertItem(int index, const QString &name, const QString &number) {
194         emit beginInsertRows(QModelIndex(), index, index);
195         list.insert(index, QPair<QString,QString>(name, number));
196         emit endInsertRows();
197     }
198
199     void removeItem(int index) {
200         emit beginRemoveRows(QModelIndex(), index, index);
201         list.removeAt(index);
202         emit endRemoveRows();
203     }
204
205     void removeItems(int index, int count) {
206         emit beginRemoveRows(QModelIndex(), index, index+count-1);
207         while (count--)
208             list.removeAt(index);
209         emit endRemoveRows();
210     }
211
212     void moveItem(int from, int to) {
213         emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
214         list.move(from, to);
215         emit endMoveRows();
216     }
217
218     void moveItems(int from, int to, int count) {
219         emit beginMoveRows(QModelIndex(), from, from+count-1, QModelIndex(), to > from ? to+count : to);
220         tst_qsggridview_move(from, to, count, &list);
221         emit endMoveRows();
222     }
223
224     void modifyItem(int idx, const QString &name, const QString &number) {
225         list[idx] = QPair<QString,QString>(name, number);
226         emit dataChanged(index(idx,0), index(idx,0));
227     }
228
229     void clear() {
230         int count = list.count();
231         emit beginRemoveRows(QModelIndex(), 0, count-1);
232         list.clear();
233         emit endRemoveRows();
234     }
235
236
237 private:
238     QList<QPair<QString,QString> > list;
239 };
240
241 tst_QSGGridView::tst_QSGGridView()
242 {
243 }
244
245 void tst_QSGGridView::items()
246 {
247     QSGView *canvas = createView();
248
249     TestModel model;
250     model.addItem("Fred", "12345");
251     model.addItem("John", "2345");
252     model.addItem("Bob", "54321");
253     model.addItem("Billy", "22345");
254     model.addItem("Sam", "2945");
255     model.addItem("Ben", "04321");
256     model.addItem("Jim", "0780");
257
258     QDeclarativeContext *ctxt = canvas->rootContext();
259     ctxt->setContextProperty("testModel", &model);
260     ctxt->setContextProperty("testRightToLeft", QVariant(false));
261     ctxt->setContextProperty("testTopToBottom", QVariant(false));
262
263     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
264     qApp->processEvents();
265
266     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
267     QTRY_VERIFY(gridview != 0);
268
269     QSGItem *contentItem = gridview->contentItem();
270     QTRY_VERIFY(contentItem != 0);
271
272     QTRY_COMPARE(gridview->count(), model.count());
273     QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count());
274     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
275
276     for (int i = 0; i < model.count(); ++i) {
277         QSGText *name = findItem<QSGText>(contentItem, "textName", i);
278         QTRY_VERIFY(name != 0);
279         QTRY_COMPARE(name->text(), model.name(i));
280         QSGText *number = findItem<QSGText>(contentItem, "textNumber", i);
281         QTRY_VERIFY(number != 0);
282         QTRY_COMPARE(number->text(), model.number(i));
283     }
284
285     // set an empty model and confirm that items are destroyed
286     TestModel model2;
287     ctxt->setContextProperty("testModel", &model2);
288
289     int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
290     QTRY_VERIFY(itemCount == 0);
291
292     delete canvas;
293 }
294
295 void tst_QSGGridView::changed()
296 {
297     QSGView *canvas = createView();
298
299     TestModel model;
300     model.addItem("Fred", "12345");
301     model.addItem("John", "2345");
302     model.addItem("Bob", "54321");
303     model.addItem("Billy", "22345");
304     model.addItem("Sam", "2945");
305     model.addItem("Ben", "04321");
306     model.addItem("Jim", "0780");
307
308     QDeclarativeContext *ctxt = canvas->rootContext();
309     ctxt->setContextProperty("testModel", &model);
310     ctxt->setContextProperty("testRightToLeft", QVariant(false));
311     ctxt->setContextProperty("testTopToBottom", QVariant(false));
312
313     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
314     qApp->processEvents();
315
316     QSGFlickable *gridview = findItem<QSGFlickable>(canvas->rootObject(), "grid");
317     QTRY_VERIFY(gridview != 0);
318
319     QSGItem *contentItem = gridview->contentItem();
320     QTRY_VERIFY(contentItem != 0);
321
322     model.modifyItem(1, "Will", "9876");
323     QSGText *name = findItem<QSGText>(contentItem, "textName", 1);
324     QTRY_VERIFY(name != 0);
325     QTRY_COMPARE(name->text(), model.name(1));
326     QSGText *number = findItem<QSGText>(contentItem, "textNumber", 1);
327     QTRY_VERIFY(number != 0);
328     QTRY_COMPARE(number->text(), model.number(1));
329
330     delete canvas;
331 }
332
333 void tst_QSGGridView::inserted()
334 {
335     QSGView *canvas = createView();
336     canvas->show();
337
338     TestModel model;
339     model.addItem("Fred", "12345");
340     model.addItem("John", "2345");
341     model.addItem("Bob", "54321");
342
343     QDeclarativeContext *ctxt = canvas->rootContext();
344     ctxt->setContextProperty("testModel", &model);
345     ctxt->setContextProperty("testRightToLeft", QVariant(false));
346     ctxt->setContextProperty("testTopToBottom", QVariant(false));
347
348     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
349     qApp->processEvents();
350
351     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
352     QTRY_VERIFY(gridview != 0);
353
354     QSGItem *contentItem = gridview->contentItem();
355     QTRY_VERIFY(contentItem != 0);
356
357     model.insertItem(1, "Will", "9876");
358     QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count());
359
360     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
361
362     QSGText *name = findItem<QSGText>(contentItem, "textName", 1);
363     QTRY_VERIFY(name != 0);
364     QTRY_COMPARE(name->text(), model.name(1));
365     QSGText *number = findItem<QSGText>(contentItem, "textNumber", 1);
366     QTRY_VERIFY(number != 0);
367     QTRY_COMPARE(number->text(), model.number(1));
368
369     // Checks that onAdd is called
370     int added = canvas->rootObject()->property("added").toInt();
371     QTRY_COMPARE(added, 1);
372
373     // Confirm items positioned correctly
374     for (int i = 0; i < model.count(); ++i) {
375         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
376         QTRY_COMPARE(item->x(), (i%3)*80.0);
377         QTRY_COMPARE(item->y(), (i/3)*60.0);
378     }
379
380     model.insertItem(0, "Foo", "1111"); // zero index, and current item
381
382     QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item
383
384     name = findItem<QSGText>(contentItem, "textName", 0);
385     QTRY_VERIFY(name != 0);
386     QTRY_COMPARE(name->text(), model.name(0));
387     number = findItem<QSGText>(contentItem, "textNumber", 0);
388     QTRY_VERIFY(number != 0);
389     QTRY_COMPARE(number->text(), model.number(0));
390
391     QTRY_COMPARE(gridview->currentIndex(), 1);
392
393     // Confirm items positioned correctly
394     for (int i = 0; i < model.count(); ++i) {
395         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
396         QTRY_VERIFY(item->x() == (i%3)*80);
397         QTRY_VERIFY(item->y() == (i/3)*60);
398     }
399
400     for (int i = model.count(); i < 30; ++i)
401         model.insertItem(i, "Hello", QString::number(i));
402
403     gridview->setContentY(120);
404
405     // Insert item outside visible area
406     model.insertItem(1, "Hello", "1324");
407
408     QTRY_VERIFY(gridview->contentY() == 120);
409
410     delete canvas;
411 }
412
413 void tst_QSGGridView::removed()
414 {
415     QSGView *canvas = createView();
416     canvas->show();
417
418     TestModel model;
419     for (int i = 0; i < 40; i++)
420         model.addItem("Item" + QString::number(i), "");
421
422     QDeclarativeContext *ctxt = canvas->rootContext();
423     ctxt->setContextProperty("testModel", &model);
424     ctxt->setContextProperty("testRightToLeft", QVariant(false));
425     ctxt->setContextProperty("testTopToBottom", QVariant(false));
426
427     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
428     qApp->processEvents();
429
430     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
431     QTRY_VERIFY(gridview != 0);
432
433     QSGItem *contentItem = gridview->contentItem();
434     QTRY_VERIFY(contentItem != 0);
435
436     model.removeItem(1);
437     QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count());
438
439     QSGText *name = findItem<QSGText>(contentItem, "textName", 1);
440     QTRY_VERIFY(name != 0);
441     QTRY_COMPARE(name->text(), model.name(1));
442     QSGText *number = findItem<QSGText>(contentItem, "textNumber", 1);
443     QTRY_VERIFY(number != 0);
444     QTRY_COMPARE(number->text(), model.number(1));
445
446     // Checks that onRemove is called
447     QString removed = canvas->rootObject()->property("removed").toString();
448     QTRY_COMPARE(removed, QString("Item1"));
449
450     // Confirm items positioned correctly
451     int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
452     for (int i = 0; i < model.count() && i < itemCount; ++i) {
453         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
454         if (!item) qWarning() << "Item" << i << "not found";
455         QTRY_VERIFY(item);
456         QTRY_VERIFY(item->x() == (i%3)*80);
457         QTRY_VERIFY(item->y() == (i/3)*60);
458     }
459
460     // Remove first item (which is the current item);
461     model.removeItem(0);
462
463     name = findItem<QSGText>(contentItem, "textName", 0);
464     QTRY_VERIFY(name != 0);
465     QTRY_COMPARE(name->text(), model.name(0));
466     number = findItem<QSGText>(contentItem, "textNumber", 0);
467     QTRY_VERIFY(number != 0);
468     QTRY_COMPARE(number->text(), model.number(0));
469
470     // Confirm items positioned correctly
471     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
472     for (int i = 0; i < model.count() && i < itemCount; ++i) {
473         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
474         if (!item) qWarning() << "Item" << i << "not found";
475         QTRY_VERIFY(item);
476         QTRY_VERIFY(item->x() == (i%3)*80);
477         QTRY_VERIFY(item->y() == (i/3)*60);
478     }
479
480     // Remove items not visible
481     model.removeItem(25);
482
483     // Confirm items positioned correctly
484     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
485     for (int i = 0; i < model.count() && i < itemCount; ++i) {
486         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
487         if (!item) qWarning() << "Item" << i << "not found";
488         QTRY_VERIFY(item);
489         QTRY_VERIFY(item->x() == (i%3)*80);
490         QTRY_VERIFY(item->y() == (i/3)*60);
491     }
492
493     // Remove items before visible
494     gridview->setContentY(120);
495     gridview->setCurrentIndex(10);
496
497     // Setting currentIndex above shouldn't cause view to scroll
498     QTRY_COMPARE(gridview->contentY(), 120.0);
499
500     model.removeItem(1);
501
502     // Confirm items positioned correctly
503     for (int i = 6; i < 18; ++i) {
504         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
505         if (!item) qWarning() << "Item" << i << "not found";
506         QTRY_VERIFY(item);
507         QTRY_VERIFY(item->x() == (i%3)*80);
508         QTRY_VERIFY(item->y() == (i/3)*60);
509     }
510
511     // Remove currentIndex
512     QSGItem *oldCurrent = gridview->currentItem();
513     model.removeItem(9);
514
515     QTRY_COMPARE(gridview->currentIndex(), 9);
516     QTRY_VERIFY(gridview->currentItem() != oldCurrent);
517
518     gridview->setContentY(0);
519     // let transitions settle.
520     QTest::qWait(100);
521
522     // Confirm items positioned correctly
523     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
524     for (int i = 0; i < model.count() && i < itemCount; ++i) {
525         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
526         if (!item) qWarning() << "Item" << i << "not found";
527         QTRY_VERIFY(item);
528         QTRY_VERIFY(item->x() == (i%3)*80);
529         QTRY_VERIFY(item->y() == (i/3)*60);
530     }
531
532     // remove item outside current view.
533     gridview->setCurrentIndex(32);
534     gridview->setContentY(240);
535
536     model.removeItem(30);
537     QTRY_VERIFY(gridview->currentIndex() == 31);
538
539     // remove current item beyond visible items.
540     gridview->setCurrentIndex(20);
541     gridview->setContentY(0);
542     model.removeItem(20);
543
544     QTRY_COMPARE(gridview->currentIndex(), 20);
545     QTRY_VERIFY(gridview->currentItem() != 0);
546
547     // remove item before current, but visible
548     gridview->setCurrentIndex(8);
549     gridview->setContentY(240);
550     oldCurrent = gridview->currentItem();
551     model.removeItem(6);
552
553     QTRY_COMPARE(gridview->currentIndex(), 7);
554     QTRY_VERIFY(gridview->currentItem() == oldCurrent);
555
556     delete canvas;
557 }
558
559 void tst_QSGGridView::clear()
560 {
561     QSGView *canvas = createView();
562
563     TestModel model;
564     for (int i = 0; i < 30; i++)
565         model.addItem("Item" + QString::number(i), "");
566
567     QDeclarativeContext *ctxt = canvas->rootContext();
568     ctxt->setContextProperty("testModel", &model);
569     ctxt->setContextProperty("testRightToLeft", QVariant(false));
570     ctxt->setContextProperty("testTopToBottom", QVariant(false));
571
572     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
573     qApp->processEvents();
574
575     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
576     QVERIFY(gridview != 0);
577
578     QSGItem *contentItem = gridview->contentItem();
579     QVERIFY(contentItem != 0);
580
581     model.clear();
582
583     QVERIFY(gridview->count() == 0);
584     QVERIFY(gridview->currentItem() == 0);
585     QVERIFY(gridview->contentY() == 0);
586     QVERIFY(gridview->currentIndex() == -1);
587
588     // confirm sanity when adding an item to cleared list
589     model.addItem("New", "1");
590     QVERIFY(gridview->count() == 1);
591     QVERIFY(gridview->currentItem() != 0);
592     QVERIFY(gridview->currentIndex() == 0);
593
594     delete canvas;
595 }
596
597 void tst_QSGGridView::moved()
598 {
599     QFETCH(qreal, contentY);
600     QFETCH(int, from);
601     QFETCH(int, to);
602     QFETCH(int, count);
603     QFETCH(qreal, itemsOffsetAfterMove);
604
605     QSGText *name;
606     QSGText *number;
607     QSGView *canvas = createView();
608     canvas->show();
609
610     TestModel model;
611     for (int i = 0; i < 30; i++)
612         model.addItem("Item" + QString::number(i), "");
613
614     QDeclarativeContext *ctxt = canvas->rootContext();
615     ctxt->setContextProperty("testModel", &model);
616     ctxt->setContextProperty("testRightToLeft", QVariant(false));
617     ctxt->setContextProperty("testTopToBottom", QVariant(false));
618
619     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
620     qApp->processEvents();
621
622     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
623     QTRY_VERIFY(gridview != 0);
624
625     QSGItem *contentItem = gridview->contentItem();
626     QTRY_VERIFY(contentItem != 0);
627
628     QSGItem *currentItem = gridview->currentItem();
629     QTRY_VERIFY(currentItem != 0);
630
631     gridview->setContentY(contentY);
632     model.moveItems(from, to, count);
633
634     // Confirm items positioned correctly and indexes correct
635     int firstVisibleIndex = qCeil(contentY / 60.0) * 3;
636     int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
637
638     for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
639         if (i >= firstVisibleIndex + 18)    // index has moved out of view
640             continue;
641         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
642         QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
643
644         QTRY_COMPARE(item->x(), (i%3)*80.0);
645         QTRY_COMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
646
647         name = findItem<QSGText>(contentItem, "textName", i);
648         QVERIFY(name != 0);
649         QTRY_COMPARE(name->text(), model.name(i));
650         number = findItem<QSGText>(contentItem, "textNumber", i);
651         QVERIFY(number != 0);
652         QTRY_COMPARE(number->text(), model.number(i));
653
654         // current index should have been updated
655         if (item == currentItem)
656             QTRY_COMPARE(gridview->currentIndex(), i);
657     }
658
659     delete canvas;
660 }
661
662 void tst_QSGGridView::moved_data()
663 {
664     QTest::addColumn<qreal>("contentY");
665     QTest::addColumn<int>("from");
666     QTest::addColumn<int>("to");
667     QTest::addColumn<int>("count");
668     QTest::addColumn<qreal>("itemsOffsetAfterMove");
669
670     // model starts with 30 items, each 80x60, in area 240x320
671     // 18 items should be visible at a time
672
673     QTest::newRow("move 1 forwards, within visible items")
674             << 0.0
675             << 1 << 8 << 1
676             << 0.0;
677
678     QTest::newRow("move 1 forwards, from non-visible -> visible")
679             << 120.0     // show 6-23
680             << 1 << 23 << 1
681             << 0.0;
682
683     QTest::newRow("move 1 forwards, from non-visible -> visible (move first item)")
684             << 120.0     // // show 6-23
685             << 0 << 6 << 1
686             << 0.0;
687
688     QTest::newRow("move 1 forwards, from visible -> non-visible")
689             << 0.0
690             << 1 << 20 << 1
691             << 0.0;
692
693     QTest::newRow("move 1 forwards, from visible -> non-visible (move first item)")
694             << 0.0
695             << 0 << 20 << 1
696             << 0.0;
697
698
699     QTest::newRow("move 1 backwards, within visible items")
700             << 0.0
701             << 10 << 5 << 1
702             << 0.0;
703
704     QTest::newRow("move 1 backwards, from non-visible -> visible")
705             << 0.0
706             << 28 << 8 << 1
707             << 0.0;
708
709     QTest::newRow("move 1 backwards, from non-visible -> visible (move last item)")
710             << 0.0
711             << 29 << 14 << 1
712             << 0.0;
713
714     QTest::newRow("move 1 backwards, from visible -> non-visible")
715             << 120.0     // show 6-23
716             << 7 << 1 << 1
717             << 60.0 * 2;   // this results in a forward movement that removes 6 items (2 rows)
718
719     QTest::newRow("move 1 backwards, from visible -> non-visible (move first item)")
720             << 120.0     // show 6-23
721             << 7 << 0 << 1
722             << 60.0 * 2;     // first visible item moved, so all items shift 2 rows down with it
723
724
725     QTest::newRow("move multiple forwards, within visible items")
726             << 0.0
727             << 0 << 5 << 3
728             << 60.0;    // moved 3 items (i.e. 1 row) down
729
730     QTest::newRow("move multiple forwards, from non-visible -> visible")
731             << 120.0     // show 6-23
732             << 1 << 6 << 3
733             << 60.0;    // top row moved, all items should shift down by 1 row
734
735     QTest::newRow("move multiple forwards, from non-visible -> visible (move first item)")
736             << 120.0     // show 6-23
737             << 0 << 6 << 3
738             << 60.0;    // top row moved, all items should shift down by 1 row
739
740     QTest::newRow("move multiple forwards, from visible -> non-visible")
741             << 0.0
742             << 1 << 16 << 3
743             << 0.0;
744
745     QTest::newRow("move multiple forwards, from visible -> non-visible (move first item)")
746             << 0.0
747             << 0 << 16 << 3
748             << 60.0;
749
750
751     QTest::newRow("move multiple backwards, within visible items")
752             << 0.0
753             << 4 << 1 << 3
754             << 0.0;
755
756     QTest::newRow("move multiple backwards, from non-visible -> visible")
757             << 0.0
758             << 20 << 4 << 3
759             << 0.0;
760
761     QTest::newRow("move multiple backwards, from non-visible -> visible (move last item)")
762             << 0.0
763             << 27 << 10 << 3
764             << 0.0;
765
766     QTest::newRow("move multiple backwards, from visible -> non-visible")
767             << 120.0     // show 6-23
768             << 16 << 1 << 3
769             << 60.0 * 5;   // this results in a forward movement that removes 15 items (5 rows)
770
771     QTest::newRow("move multiple backwards, from visible -> non-visible (move first item)")
772             << 120.0     // show 6-23
773             << 16 << 0 << 3
774             << 60.0 * 5;    // this results in a forward movement that removes 16 items (5 rows)
775 }
776
777 void tst_QSGGridView::swapWithFirstItem()
778 {
779     // QTBUG_9697
780     QSGView *canvas = createView();
781     canvas->show();
782
783     TestModel model;
784     for (int i = 0; i < 30; i++)
785         model.addItem("Item" + QString::number(i), "");
786
787     QDeclarativeContext *ctxt = canvas->rootContext();
788     ctxt->setContextProperty("testModel", &model);
789     ctxt->setContextProperty("testRightToLeft", QVariant(false));
790     ctxt->setContextProperty("testTopToBottom", QVariant(false));
791
792     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
793     qApp->processEvents();
794
795     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
796     QTRY_VERIFY(gridview != 0);
797
798     // ensure content position is stable
799     gridview->setContentY(0);
800     model.moveItem(10, 0);
801     QTRY_VERIFY(gridview->contentY() == 0);
802
803     delete canvas;
804 }
805
806 void tst_QSGGridView::currentIndex()
807 {
808     TestModel model;
809     for (int i = 0; i < 60; i++)
810         model.addItem("Item" + QString::number(i), QString::number(i));
811
812     QSGView *canvas = new QSGView(0);
813     canvas->setFixedSize(240,320);
814     canvas->show();
815
816     QDeclarativeContext *ctxt = canvas->rootContext();
817     ctxt->setContextProperty("testModel", &model);
818
819     QString filename(SRCDIR "/data/gridview-initCurrent.qml");
820     canvas->setSource(QUrl::fromLocalFile(filename));
821
822     qApp->processEvents();
823
824     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
825     QVERIFY(gridview != 0);
826
827     QSGItem *contentItem = gridview->contentItem();
828     QVERIFY(contentItem != 0);
829
830     // current item should be third item
831     QCOMPARE(gridview->currentIndex(), 35);
832     QCOMPARE(gridview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 35));
833     QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
834     QCOMPARE(gridview->contentY(), 400.0);
835
836     gridview->moveCurrentIndexRight();
837     QCOMPARE(gridview->currentIndex(), 36);
838     gridview->moveCurrentIndexDown();
839     QCOMPARE(gridview->currentIndex(), 39);
840     gridview->moveCurrentIndexUp();
841     QCOMPARE(gridview->currentIndex(), 36);
842     gridview->moveCurrentIndexLeft();
843     QCOMPARE(gridview->currentIndex(), 35);
844
845     // no wrap
846     gridview->setCurrentIndex(0);
847     QCOMPARE(gridview->currentIndex(), 0);
848     // confirm that the velocity is updated
849     QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
850
851     gridview->moveCurrentIndexUp();
852     QCOMPARE(gridview->currentIndex(), 0);
853
854     gridview->moveCurrentIndexLeft();
855     QCOMPARE(gridview->currentIndex(), 0);
856
857     gridview->setCurrentIndex(model.count()-1);
858     QCOMPARE(gridview->currentIndex(), model.count()-1);
859
860     gridview->moveCurrentIndexRight();
861     QCOMPARE(gridview->currentIndex(), model.count()-1);
862
863     gridview->moveCurrentIndexDown();
864     QCOMPARE(gridview->currentIndex(), model.count()-1);
865
866     // with wrap
867     gridview->setWrapEnabled(true);
868
869     gridview->setCurrentIndex(0);
870     QCOMPARE(gridview->currentIndex(), 0);
871
872     gridview->moveCurrentIndexLeft();
873     QCOMPARE(gridview->currentIndex(), model.count()-1);
874
875     qApp->processEvents();
876     QTRY_COMPARE(gridview->contentY(), 880.0);
877
878     gridview->moveCurrentIndexRight();
879     QCOMPARE(gridview->currentIndex(), 0);
880
881     QTRY_COMPARE(gridview->contentY(), 0.0);
882
883     // Test keys
884     qApp->setActiveWindow(canvas);
885 #ifdef Q_WS_X11
886     // to be safe and avoid failing setFocus with window managers
887     qt_x11_wait_for_window_manager(canvas);
888 #endif
889     QTRY_VERIFY(canvas->hasFocus());
890     qApp->processEvents();
891
892     QTest::keyClick(canvas, Qt::Key_Down);
893     QCOMPARE(gridview->currentIndex(), 3);
894
895     QTest::keyClick(canvas, Qt::Key_Up);
896     QCOMPARE(gridview->currentIndex(), 0);
897
898     // hold down Key_Down
899     for (int i=0; i<(model.count() / 3) - 1; i++) {
900         QTest::simulateEvent(canvas, true, Qt::Key_Down, Qt::NoModifier, "", true);
901         QTRY_COMPARE(gridview->currentIndex(), i*3 + 3);
902     }
903     QTest::keyRelease(canvas, Qt::Key_Down);
904     QTRY_COMPARE(gridview->currentIndex(), 57);
905     QTRY_COMPARE(gridview->contentY(), 880.0);
906
907     // hold down Key_Up
908     for (int i=(model.count() / 3) - 1; i > 0; i--) {
909         QTest::simulateEvent(canvas, true, Qt::Key_Up, Qt::NoModifier, "", true);
910         QTRY_COMPARE(gridview->currentIndex(), i*3 - 3);
911     }
912     QTest::keyRelease(canvas, Qt::Key_Up);
913     QTRY_COMPARE(gridview->currentIndex(), 0);
914     QTRY_COMPARE(gridview->contentY(), 0.0);
915
916
917     gridview->setFlow(QSGGridView::TopToBottom);
918
919     qApp->setActiveWindow(canvas);
920 #ifdef Q_WS_X11
921     // to be safe and avoid failing setFocus with window managers
922     qt_x11_wait_for_window_manager(canvas);
923 #endif
924     QTRY_VERIFY(canvas->hasFocus());
925     qApp->processEvents();
926
927     QTest::keyClick(canvas, Qt::Key_Right);
928     QCOMPARE(gridview->currentIndex(), 5);
929
930     QTest::keyClick(canvas, Qt::Key_Left);
931     QCOMPARE(gridview->currentIndex(), 0);
932
933     QTest::keyClick(canvas, Qt::Key_Down);
934     QCOMPARE(gridview->currentIndex(), 1);
935
936     QTest::keyClick(canvas, Qt::Key_Up);
937     QCOMPARE(gridview->currentIndex(), 0);
938
939     // hold down Key_Right
940     for (int i=0; i<(model.count() / 5) - 1; i++) {
941         QTest::simulateEvent(canvas, true, Qt::Key_Right, Qt::NoModifier, "", true);
942         QTRY_COMPARE(gridview->currentIndex(), i*5 + 5);
943     }
944     QTest::keyRelease(canvas, Qt::Key_Right);
945     QTRY_COMPARE(gridview->currentIndex(), 55);
946     QTRY_COMPARE(gridview->contentX(), 720.0);
947
948     // hold down Key_Left
949     for (int i=(model.count() / 5) - 1; i > 0; i--) {
950         QTest::simulateEvent(canvas, true, Qt::Key_Left, Qt::NoModifier, "", true);
951         QTRY_COMPARE(gridview->currentIndex(), i*5 - 5);
952     }
953     QTest::keyRelease(canvas, Qt::Key_Left);
954     QTRY_COMPARE(gridview->currentIndex(), 0);
955     QTRY_COMPARE(gridview->contentX(), 0.0);
956
957
958     // turn off auto highlight
959     gridview->setHighlightFollowsCurrentItem(false);
960     QVERIFY(gridview->highlightFollowsCurrentItem() == false);
961     QVERIFY(gridview->highlightItem());
962     qreal hlPosX = gridview->highlightItem()->x();
963     qreal hlPosY = gridview->highlightItem()->y();
964
965     gridview->setCurrentIndex(5);
966     QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
967     QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
968
969     // insert item before currentIndex
970     gridview->setCurrentIndex(28);
971     model.insertItem(0, "Foo", "1111");
972     QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
973
974     // check removing highlight by setting currentIndex to -1;
975     gridview->setCurrentIndex(-1);
976
977     QCOMPARE(gridview->currentIndex(), -1);
978     QVERIFY(!gridview->highlightItem());
979     QVERIFY(!gridview->currentItem());
980
981     gridview->setHighlightFollowsCurrentItem(true);
982
983     gridview->setFlow(QSGGridView::LeftToRight);
984     gridview->setLayoutDirection(Qt::RightToLeft);
985
986     qApp->setActiveWindow(canvas);
987 #ifdef Q_WS_X11
988     // to be safe and avoid failing setFocus with window managers
989     qt_x11_wait_for_window_manager(canvas);
990 #endif
991     QTRY_VERIFY(canvas->hasFocus());
992     qApp->processEvents();
993
994     gridview->setCurrentIndex(35);
995
996     QTest::keyClick(canvas, Qt::Key_Right);
997     QCOMPARE(gridview->currentIndex(), 34);
998
999     QTest::keyClick(canvas, Qt::Key_Down);
1000     QCOMPARE(gridview->currentIndex(), 37);
1001
1002     QTest::keyClick(canvas, Qt::Key_Up);
1003     QCOMPARE(gridview->currentIndex(), 34);
1004
1005     QTest::keyClick(canvas, Qt::Key_Left);
1006     QCOMPARE(gridview->currentIndex(), 35);
1007
1008
1009     // turn off auto highlight
1010     gridview->setHighlightFollowsCurrentItem(false);
1011     QVERIFY(gridview->highlightFollowsCurrentItem() == false);
1012     QVERIFY(gridview->highlightItem());
1013     hlPosX = gridview->highlightItem()->x();
1014     hlPosY = gridview->highlightItem()->y();
1015
1016     gridview->setCurrentIndex(5);
1017     QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX);
1018     QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY);
1019
1020     // insert item before currentIndex
1021     gridview->setCurrentIndex(28);
1022     model.insertItem(0, "Foo", "1111");
1023     QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29);
1024
1025     // check removing highlight by setting currentIndex to -1;
1026     gridview->setCurrentIndex(-1);
1027
1028     QCOMPARE(gridview->currentIndex(), -1);
1029     QVERIFY(!gridview->highlightItem());
1030     QVERIFY(!gridview->currentItem());
1031
1032     delete canvas;
1033 }
1034
1035 void tst_QSGGridView::noCurrentIndex()
1036 {
1037     TestModel model;
1038     for (int i = 0; i < 60; i++)
1039         model.addItem("Item" + QString::number(i), QString::number(i));
1040
1041     QSGView *canvas = new QSGView(0);
1042     canvas->setFixedSize(240,320);
1043
1044     QDeclarativeContext *ctxt = canvas->rootContext();
1045     ctxt->setContextProperty("testModel", &model);
1046
1047     QString filename(SRCDIR "/data/gridview-noCurrent.qml");
1048     canvas->setSource(QUrl::fromLocalFile(filename));
1049
1050     qApp->processEvents();
1051
1052     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1053     QVERIFY(gridview != 0);
1054
1055     QSGItem *contentItem = gridview->contentItem();
1056     QVERIFY(contentItem != 0);
1057
1058     // current index should be -1
1059     QCOMPARE(gridview->currentIndex(), -1);
1060     QVERIFY(!gridview->currentItem());
1061     QVERIFY(!gridview->highlightItem());
1062     QCOMPARE(gridview->contentY(), 0.0);
1063
1064     gridview->setCurrentIndex(5);
1065     QCOMPARE(gridview->currentIndex(), 5);
1066     QVERIFY(gridview->currentItem());
1067     QVERIFY(gridview->highlightItem());
1068
1069     delete canvas;
1070 }
1071
1072 void tst_QSGGridView::changeFlow()
1073 {
1074     QSGView *canvas = createView();
1075
1076     TestModel model;
1077     for (int i = 0; i < 30; i++)
1078         model.addItem("Item" + QString::number(i), QString::number(i));
1079
1080     QDeclarativeContext *ctxt = canvas->rootContext();
1081     ctxt->setContextProperty("testModel", &model);
1082     ctxt->setContextProperty("testRightToLeft", QVariant(false));
1083     ctxt->setContextProperty("testTopToBottom", QVariant(false));
1084
1085     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
1086     qApp->processEvents();
1087
1088     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1089     QTRY_VERIFY(gridview != 0);
1090
1091     QSGItem *contentItem = gridview->contentItem();
1092     QTRY_VERIFY(contentItem != 0);
1093
1094     // Confirm items positioned correctly and indexes correct
1095     int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1096     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1097         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1098         if (!item) qWarning() << "Item" << i << "not found";
1099         QTRY_VERIFY(item);
1100         QTRY_COMPARE(item->x(), qreal((i%3)*80));
1101         QTRY_COMPARE(item->y(), qreal((i/3)*60));
1102         QSGText *name = findItem<QSGText>(contentItem, "textName", i);
1103         QTRY_VERIFY(name != 0);
1104         QTRY_COMPARE(name->text(), model.name(i));
1105         QSGText *number = findItem<QSGText>(contentItem, "textNumber", i);
1106         QTRY_VERIFY(number != 0);
1107         QTRY_COMPARE(number->text(), model.number(i));
1108     }
1109
1110     ctxt->setContextProperty("testTopToBottom", QVariant(true));
1111
1112     // Confirm items positioned correctly and indexes correct
1113     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1114     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1115         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1116         if (!item) qWarning() << "Item" << i << "not found";
1117         QTRY_VERIFY(item);
1118         QTRY_COMPARE(item->x(), qreal((i/5)*80));
1119         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1120         QSGText *name = findItem<QSGText>(contentItem, "textName", i);
1121         QTRY_VERIFY(name != 0);
1122         QTRY_COMPARE(name->text(), model.name(i));
1123         QSGText *number = findItem<QSGText>(contentItem, "textNumber", i);
1124         QTRY_VERIFY(number != 0);
1125         QTRY_COMPARE(number->text(), model.number(i));
1126     }
1127
1128     ctxt->setContextProperty("testRightToLeft", QVariant(true));
1129
1130     // Confirm items positioned correctly and indexes correct
1131     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1132     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1133         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1134         if (!item) qWarning() << "Item" << i << "not found";
1135         QTRY_VERIFY(item);
1136         QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width()));
1137         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1138         QSGText *name = findItem<QSGText>(contentItem, "textName", i);
1139         QTRY_VERIFY(name != 0);
1140         QTRY_COMPARE(name->text(), model.name(i));
1141         QSGText *number = findItem<QSGText>(contentItem, "textNumber", i);
1142         QTRY_VERIFY(number != 0);
1143         QTRY_COMPARE(number->text(), model.number(i));
1144     }
1145     gridview->setContentX(100);
1146     QTRY_COMPARE(gridview->contentX(), 100.);
1147     ctxt->setContextProperty("testTopToBottom", QVariant(false));
1148     QTRY_COMPARE(gridview->contentX(), 0.);
1149
1150     // Confirm items positioned correctly and indexes correct
1151     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1152     for (int i = 0; i < model.count() && i < itemCount; ++i) {
1153         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1154         if (!item) qWarning() << "Item" << i << "not found";
1155         QTRY_VERIFY(item);
1156         QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80));
1157         QTRY_COMPARE(item->y(), qreal((i/3)*60));
1158         QSGText *name = findItem<QSGText>(contentItem, "textName", i);
1159         QTRY_VERIFY(name != 0);
1160         QTRY_COMPARE(name->text(), model.name(i));
1161         QSGText *number = findItem<QSGText>(contentItem, "textNumber", i);
1162         QTRY_VERIFY(number != 0);
1163         QTRY_COMPARE(number->text(), model.number(i));
1164     }
1165
1166     delete canvas;
1167 }
1168
1169 void tst_QSGGridView::defaultValues()
1170 {
1171     QDeclarativeEngine engine;
1172     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/gridview3.qml"));
1173     QSGGridView *obj = qobject_cast<QSGGridView*>(c.create());
1174
1175     QTRY_VERIFY(obj != 0);
1176     QTRY_VERIFY(obj->model() == QVariant());
1177     QTRY_VERIFY(obj->delegate() == 0);
1178     QTRY_COMPARE(obj->currentIndex(), -1);
1179     QTRY_VERIFY(obj->currentItem() == 0);
1180     QTRY_COMPARE(obj->count(), 0);
1181     QTRY_VERIFY(obj->highlight() == 0);
1182     QTRY_VERIFY(obj->highlightItem() == 0);
1183     QTRY_COMPARE(obj->highlightFollowsCurrentItem(), true);
1184     QTRY_VERIFY(obj->flow() == 0);
1185     QTRY_COMPARE(obj->isWrapEnabled(), false);
1186     QTRY_COMPARE(obj->cacheBuffer(), 0);
1187     QTRY_COMPARE(obj->cellWidth(), 100); //### Should 100 be the default?
1188     QTRY_COMPARE(obj->cellHeight(), 100);
1189     delete obj;
1190 }
1191
1192 void tst_QSGGridView::properties()
1193 {
1194     QDeclarativeEngine engine;
1195     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/gridview2.qml"));
1196     QSGGridView *obj = qobject_cast<QSGGridView*>(c.create());
1197
1198     QTRY_VERIFY(obj != 0);
1199     QTRY_VERIFY(obj->model() != QVariant());
1200     QTRY_VERIFY(obj->delegate() != 0);
1201     QTRY_COMPARE(obj->currentIndex(), 0);
1202     QTRY_VERIFY(obj->currentItem() != 0);
1203     QTRY_COMPARE(obj->count(), 4);
1204     QTRY_VERIFY(obj->highlight() != 0);
1205     QTRY_VERIFY(obj->highlightItem() != 0);
1206     QTRY_COMPARE(obj->highlightFollowsCurrentItem(), false);
1207     QTRY_VERIFY(obj->flow() == 0);
1208     QTRY_COMPARE(obj->isWrapEnabled(), true);
1209     QTRY_COMPARE(obj->cacheBuffer(), 200);
1210     QTRY_COMPARE(obj->cellWidth(), 100);
1211     QTRY_COMPARE(obj->cellHeight(), 100);
1212     delete obj;
1213 }
1214
1215 void tst_QSGGridView::propertyChanges()
1216 {
1217     QSGView *canvas = createView();
1218     QTRY_VERIFY(canvas);
1219     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/propertychangestest.qml"));
1220
1221     QSGGridView *gridView = canvas->rootObject()->findChild<QSGGridView*>("gridView");
1222     QTRY_VERIFY(gridView);
1223
1224     QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged()));
1225     QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged()));
1226     QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged()));
1227     QSignalSpy flowSpy(gridView, SIGNAL(flowChanged()));
1228
1229     QTRY_COMPARE(gridView->isWrapEnabled(), true);
1230     QTRY_COMPARE(gridView->cacheBuffer(), 10);
1231     QTRY_COMPARE(gridView->flow(), QSGGridView::LeftToRight);
1232
1233     gridView->setWrapEnabled(false);
1234     gridView->setCacheBuffer(3);
1235     gridView->setFlow(QSGGridView::TopToBottom);
1236
1237     QTRY_COMPARE(gridView->isWrapEnabled(), false);
1238     QTRY_COMPARE(gridView->cacheBuffer(), 3);
1239     QTRY_COMPARE(gridView->flow(), QSGGridView::TopToBottom);
1240
1241     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1242     QTRY_COMPARE(cacheBufferSpy.count(),1);
1243     QTRY_COMPARE(flowSpy.count(),1);
1244
1245     gridView->setWrapEnabled(false);
1246     gridView->setCacheBuffer(3);
1247     gridView->setFlow(QSGGridView::TopToBottom);
1248
1249     QTRY_COMPARE(keyNavigationWrapsSpy.count(),1);
1250     QTRY_COMPARE(cacheBufferSpy.count(),1);
1251     QTRY_COMPARE(flowSpy.count(),1);
1252
1253     gridView->setFlow(QSGGridView::LeftToRight);
1254     QTRY_COMPARE(gridView->flow(), QSGGridView::LeftToRight);
1255
1256     gridView->setWrapEnabled(true);
1257     gridView->setCacheBuffer(5);
1258     gridView->setLayoutDirection(Qt::RightToLeft);
1259
1260     QTRY_COMPARE(gridView->isWrapEnabled(), true);
1261     QTRY_COMPARE(gridView->cacheBuffer(), 5);
1262     QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft);
1263
1264     QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
1265     QTRY_COMPARE(cacheBufferSpy.count(),2);
1266     QTRY_COMPARE(layoutSpy.count(),1);
1267     QTRY_COMPARE(flowSpy.count(),2);
1268
1269     gridView->setWrapEnabled(true);
1270     gridView->setCacheBuffer(5);
1271     gridView->setLayoutDirection(Qt::RightToLeft);
1272
1273     QTRY_COMPARE(keyNavigationWrapsSpy.count(),2);
1274     QTRY_COMPARE(cacheBufferSpy.count(),2);
1275     QTRY_COMPARE(layoutSpy.count(),1);
1276     QTRY_COMPARE(flowSpy.count(),2);
1277
1278     gridView->setFlow(QSGGridView::TopToBottom);
1279     QTRY_COMPARE(gridView->flow(), QSGGridView::TopToBottom);
1280     QTRY_COMPARE(flowSpy.count(),3);
1281
1282     gridView->setFlow(QSGGridView::TopToBottom);
1283     QTRY_COMPARE(flowSpy.count(),3);
1284
1285     delete canvas;
1286 }
1287
1288 void tst_QSGGridView::componentChanges()
1289 {
1290     QSGView *canvas = createView();
1291     QTRY_VERIFY(canvas);
1292     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/propertychangestest.qml"));
1293
1294     QSGGridView *gridView = canvas->rootObject()->findChild<QSGGridView*>("gridView");
1295     QTRY_VERIFY(gridView);
1296
1297     QDeclarativeComponent component(canvas->engine());
1298     component.setData("import QtQuick 1.0; Rectangle { color: \"blue\"; }", QUrl::fromLocalFile(""));
1299
1300     QDeclarativeComponent delegateComponent(canvas->engine());
1301     delegateComponent.setData("import QtQuick 1.0; Text { text: '<b>Name:</b> ' + name }", QUrl::fromLocalFile(""));
1302
1303     QSignalSpy highlightSpy(gridView, SIGNAL(highlightChanged()));
1304     QSignalSpy delegateSpy(gridView, SIGNAL(delegateChanged()));
1305     QSignalSpy headerSpy(gridView, SIGNAL(headerChanged()));
1306     QSignalSpy footerSpy(gridView, SIGNAL(footerChanged()));
1307
1308     gridView->setHighlight(&component);
1309     gridView->setDelegate(&delegateComponent);
1310     gridView->setHeader(&component);
1311     gridView->setFooter(&component);
1312
1313     QTRY_COMPARE(gridView->highlight(), &component);
1314     QTRY_COMPARE(gridView->delegate(), &delegateComponent);
1315     QTRY_COMPARE(gridView->header(), &component);
1316     QTRY_COMPARE(gridView->footer(), &component);
1317
1318     QTRY_COMPARE(highlightSpy.count(),1);
1319     QTRY_COMPARE(delegateSpy.count(),1);
1320     QTRY_COMPARE(headerSpy.count(),1);
1321     QTRY_COMPARE(footerSpy.count(),1);
1322
1323     gridView->setHighlight(&component);
1324     gridView->setDelegate(&delegateComponent);
1325     gridView->setHeader(&component);
1326     gridView->setFooter(&component);
1327
1328     QTRY_COMPARE(highlightSpy.count(),1);
1329     QTRY_COMPARE(delegateSpy.count(),1);
1330     QTRY_COMPARE(headerSpy.count(),1);
1331     QTRY_COMPARE(footerSpy.count(),1);
1332
1333     delete canvas;
1334 }
1335
1336 void tst_QSGGridView::modelChanges()
1337 {
1338     QSGView *canvas = createView();
1339     QTRY_VERIFY(canvas);
1340     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/propertychangestest.qml"));
1341
1342     QSGGridView *gridView = canvas->rootObject()->findChild<QSGGridView*>("gridView");
1343     QTRY_VERIFY(gridView);
1344
1345     QDeclarativeListModel *alternateModel = canvas->rootObject()->findChild<QDeclarativeListModel*>("alternateModel");
1346     QTRY_VERIFY(alternateModel);
1347     QVariant modelVariant = QVariant::fromValue<QObject *>(alternateModel);
1348     QSignalSpy modelSpy(gridView, SIGNAL(modelChanged()));
1349
1350     gridView->setModel(modelVariant);
1351     QTRY_COMPARE(gridView->model(), modelVariant);
1352     QTRY_COMPARE(modelSpy.count(),1);
1353
1354     gridView->setModel(modelVariant);
1355     QTRY_COMPARE(modelSpy.count(),1);
1356
1357     gridView->setModel(QVariant());
1358     QTRY_COMPARE(modelSpy.count(),2);
1359     delete canvas;
1360 }
1361
1362 void tst_QSGGridView::positionViewAtIndex()
1363 {
1364     QSGView *canvas = createView();
1365
1366     TestModel model;
1367     for (int i = 0; i < 40; i++)
1368         model.addItem("Item" + QString::number(i), "");
1369
1370     QDeclarativeContext *ctxt = canvas->rootContext();
1371     ctxt->setContextProperty("testModel", &model);
1372     ctxt->setContextProperty("testRightToLeft", QVariant(false));
1373     ctxt->setContextProperty("testTopToBottom", QVariant(false));
1374
1375     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
1376     qApp->processEvents();
1377
1378     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1379     QTRY_VERIFY(gridview != 0);
1380
1381     QSGItem *contentItem = gridview->contentItem();
1382     QTRY_VERIFY(contentItem != 0);
1383
1384     // Confirm items positioned correctly
1385     int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1386     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
1387         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1388         if (!item) qWarning() << "Item" << i << "not found";
1389         QTRY_VERIFY(item);
1390         QTRY_COMPARE(item->x(), (i%3)*80.);
1391         QTRY_COMPARE(item->y(), (i/3)*60.);
1392     }
1393
1394     // Position on a currently visible item
1395     gridview->positionViewAtIndex(4, QSGGridView::Beginning);
1396     QTRY_COMPARE(gridview->indexAt(120, 90), 4);
1397     QTRY_COMPARE(gridview->contentY(), 60.);
1398
1399     // Confirm items positioned correctly
1400     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1401     for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
1402         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1403         if (!item) qWarning() << "Item" << i << "not found";
1404         QTRY_VERIFY(item);
1405         QTRY_COMPARE(item->x(), (i%3)*80.);
1406         QTRY_COMPARE(item->y(), (i/3)*60.);
1407     }
1408
1409     // Position on an item beyond the visible items
1410     gridview->positionViewAtIndex(21, QSGGridView::Beginning);
1411     QTRY_COMPARE(gridview->indexAt(40, 450), 21);
1412     QTRY_COMPARE(gridview->contentY(), 420.);
1413
1414     // Confirm items positioned correctly
1415     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1416     for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
1417         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1418         if (!item) qWarning() << "Item" << i << "not found";
1419         QTRY_VERIFY(item);
1420         QTRY_COMPARE(item->x(), (i%3)*80.);
1421         QTRY_COMPARE(item->y(), (i/3)*60.);
1422     }
1423
1424     // Position on an item that would leave empty space if positioned at the top
1425     gridview->positionViewAtIndex(31, QSGGridView::Beginning);
1426     QTRY_COMPARE(gridview->indexAt(120, 630), 31);
1427     QTRY_COMPARE(gridview->contentY(), 520.);
1428
1429     // Confirm items positioned correctly
1430     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1431     for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
1432         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1433         if (!item) qWarning() << "Item" << i << "not found";
1434         QTRY_VERIFY(item);
1435         QTRY_COMPARE(item->x(), (i%3)*80.);
1436         QTRY_COMPARE(item->y(), (i/3)*60.);
1437     }
1438
1439     // Position at the beginning again
1440     gridview->positionViewAtIndex(0, QSGGridView::Beginning);
1441     QTRY_COMPARE(gridview->indexAt(0, 0), 0);
1442     QTRY_COMPARE(gridview->indexAt(40, 30), 0);
1443     QTRY_COMPARE(gridview->indexAt(80, 60), 4);
1444     QTRY_COMPARE(gridview->contentY(), 0.);
1445
1446     // Confirm items positioned correctly
1447     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1448     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
1449         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1450         if (!item) qWarning() << "Item" << i << "not found";
1451         QTRY_VERIFY(item);
1452         QTRY_COMPARE(item->x(), (i%3)*80.);
1453         QTRY_COMPARE(item->y(), (i/3)*60.);
1454     }
1455
1456     // Position at End
1457     gridview->positionViewAtIndex(30, QSGGridView::End);
1458     QTRY_COMPARE(gridview->contentY(), 340.);
1459
1460     // Position in Center
1461     gridview->positionViewAtIndex(15, QSGGridView::Center);
1462     QTRY_COMPARE(gridview->contentY(), 170.);
1463
1464     // Ensure at least partially visible
1465     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1466     QTRY_COMPARE(gridview->contentY(), 170.);
1467
1468     gridview->setContentY(302);
1469     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1470     QTRY_COMPARE(gridview->contentY(), 302.);
1471
1472     gridview->setContentY(360);
1473     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1474     QTRY_COMPARE(gridview->contentY(), 300.);
1475
1476     gridview->setContentY(60);
1477     gridview->positionViewAtIndex(20, QSGGridView::Visible);
1478     QTRY_COMPARE(gridview->contentY(), 60.);
1479
1480     gridview->setContentY(20);
1481     gridview->positionViewAtIndex(20, QSGGridView::Visible);
1482     QTRY_COMPARE(gridview->contentY(), 100.);
1483
1484     // Ensure completely visible
1485     gridview->setContentY(120);
1486     gridview->positionViewAtIndex(20, QSGGridView::Contain);
1487     QTRY_COMPARE(gridview->contentY(), 120.);
1488
1489     gridview->setContentY(302);
1490     gridview->positionViewAtIndex(15, QSGGridView::Contain);
1491     QTRY_COMPARE(gridview->contentY(), 300.);
1492
1493     gridview->setContentY(60);
1494     gridview->positionViewAtIndex(20, QSGGridView::Contain);
1495     QTRY_COMPARE(gridview->contentY(), 100.);
1496
1497     // Test for Top To Bottom layout
1498     ctxt->setContextProperty("testTopToBottom", QVariant(true));
1499
1500     // Confirm items positioned correctly
1501     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1502     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
1503         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1504         if (!item) qWarning() << "Item" << i << "not found";
1505         QTRY_VERIFY(item);
1506         QTRY_COMPARE(item->x(), (i/5)*80.);
1507         QTRY_COMPARE(item->y(), (i%5)*60.);
1508     }
1509
1510     // Position at End
1511     gridview->positionViewAtIndex(30, QSGGridView::End);
1512     QTRY_COMPARE(gridview->contentX(), 320.);
1513     QTRY_COMPARE(gridview->contentY(), 0.);
1514
1515     // Position in Center
1516     gridview->positionViewAtIndex(15, QSGGridView::Center);
1517     QTRY_COMPARE(gridview->contentX(), 160.);
1518
1519     // Ensure at least partially visible
1520     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1521     QTRY_COMPARE(gridview->contentX(), 160.);
1522
1523     gridview->setContentX(170);
1524     gridview->positionViewAtIndex(25, QSGGridView::Visible);
1525     QTRY_COMPARE(gridview->contentX(), 170.);
1526
1527     gridview->positionViewAtIndex(30, QSGGridView::Visible);
1528     QTRY_COMPARE(gridview->contentX(), 320.);
1529
1530     gridview->setContentX(170);
1531     gridview->positionViewAtIndex(25, QSGGridView::Contain);
1532     QTRY_COMPARE(gridview->contentX(), 240.);
1533
1534     // positionViewAtBeginning
1535     gridview->positionViewAtBeginning();
1536     QTRY_COMPARE(gridview->contentX(), 0.);
1537
1538     gridview->setContentX(80);
1539     canvas->rootObject()->setProperty("showHeader", true);
1540     gridview->positionViewAtBeginning();
1541     QTRY_COMPARE(gridview->contentX(), -30.);
1542
1543     // positionViewAtEnd
1544     gridview->positionViewAtEnd();
1545     QTRY_COMPARE(gridview->contentX(), 400.);   // 8*80 - 240   (8 columns)
1546
1547     gridview->setContentX(80);
1548     canvas->rootObject()->setProperty("showFooter", true);
1549     gridview->positionViewAtEnd();
1550     QTRY_COMPARE(gridview->contentX(), 430.);
1551
1552     // set current item to outside visible view, position at beginning
1553     // and ensure highlight moves to current item
1554     gridview->setCurrentIndex(6);
1555     gridview->positionViewAtBeginning();
1556     QTRY_COMPARE(gridview->contentX(), -30.);
1557     QVERIFY(gridview->highlightItem());
1558     QCOMPARE(gridview->highlightItem()->x(), 80.);
1559
1560     delete canvas;
1561 }
1562
1563 void tst_QSGGridView::snapping()
1564 {
1565     QSGView *canvas = createView();
1566
1567     TestModel model;
1568     for (int i = 0; i < 40; i++)
1569         model.addItem("Item" + QString::number(i), "");
1570
1571     QDeclarativeContext *ctxt = canvas->rootContext();
1572     ctxt->setContextProperty("testModel", &model);
1573     ctxt->setContextProperty("testTopToBottom", QVariant(false));
1574     ctxt->setContextProperty("testRightToLeft", QVariant(false));
1575
1576     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
1577     qApp->processEvents();
1578
1579     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1580     QTRY_VERIFY(gridview != 0);
1581
1582     gridview->setHeight(220);
1583     QCOMPARE(gridview->height(), 220.);
1584
1585     gridview->positionViewAtIndex(12, QSGGridView::Visible);
1586     QCOMPARE(gridview->contentY(), 80.);
1587
1588     gridview->setContentY(0);
1589     QCOMPARE(gridview->contentY(), 0.);
1590
1591     gridview->setSnapMode(QSGGridView::SnapToRow);
1592     QCOMPARE(gridview->snapMode(), QSGGridView::SnapToRow);
1593
1594     gridview->positionViewAtIndex(12, QSGGridView::Visible);
1595     QCOMPARE(gridview->contentY(), 60.);
1596
1597     gridview->positionViewAtIndex(15, QSGGridView::End);
1598     QCOMPARE(gridview->contentY(), 120.);
1599
1600     delete canvas;
1601
1602 }
1603
1604 void tst_QSGGridView::mirroring()
1605 {
1606     QSGView *canvasA = createView();
1607     canvasA->setSource(QUrl::fromLocalFile(SRCDIR "/data/mirroring.qml"));
1608     QSGGridView *gridviewA = findItem<QSGGridView>(canvasA->rootObject(), "view");
1609     QTRY_VERIFY(gridviewA != 0);
1610
1611     QSGView *canvasB = createView();
1612     canvasB->setSource(QUrl::fromLocalFile(SRCDIR "/data/mirroring.qml"));
1613     QSGGridView *gridviewB = findItem<QSGGridView>(canvasB->rootObject(), "view");
1614     QTRY_VERIFY(gridviewA != 0);
1615     qApp->processEvents();
1616
1617     QList<QString> objectNames;
1618     objectNames << "item1" << "item2"; // << "item3"
1619
1620     gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
1621     gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
1622     QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection());
1623
1624     // LTR != RTL
1625     foreach(const QString objectName, objectNames)
1626         QVERIFY(findItem<QSGItem>(gridviewA, objectName)->x() != findItem<QSGItem>(gridviewB, objectName)->x());
1627
1628     gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
1629     gridviewB->setProperty("layoutDirection", Qt::LeftToRight);
1630
1631     // LTR == LTR
1632     foreach(const QString objectName, objectNames)
1633         QCOMPARE(findItem<QSGItem>(gridviewA, objectName)->x(), findItem<QSGItem>(gridviewB, objectName)->x());
1634
1635     QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection());
1636     QSGItemPrivate::get(gridviewB)->setLayoutMirror(true);
1637     QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection());
1638
1639     // LTR != LTR+mirror
1640     foreach(const QString objectName, objectNames)
1641         QVERIFY(findItem<QSGItem>(gridviewA, objectName)->x() != findItem<QSGItem>(gridviewB, objectName)->x());
1642
1643     gridviewA->setProperty("layoutDirection", Qt::RightToLeft);
1644
1645     // RTL == LTR+mirror
1646     foreach(const QString objectName, objectNames)
1647         QCOMPARE(findItem<QSGItem>(gridviewA, objectName)->x(), findItem<QSGItem>(gridviewB, objectName)->x());
1648
1649     gridviewB->setProperty("layoutDirection", Qt::RightToLeft);
1650
1651     // RTL != RTL+mirror
1652     foreach(const QString objectName, objectNames)
1653         QVERIFY(findItem<QSGItem>(gridviewA, objectName)->x() != findItem<QSGItem>(gridviewB, objectName)->x());
1654
1655     gridviewA->setProperty("layoutDirection", Qt::LeftToRight);
1656
1657     // LTR == RTL+mirror
1658     foreach(const QString objectName, objectNames)
1659         QCOMPARE(findItem<QSGItem>(gridviewA, objectName)->x(), findItem<QSGItem>(gridviewB, objectName)->x());
1660
1661     delete canvasA;
1662     delete canvasB;
1663 }
1664
1665 void tst_QSGGridView::positionViewAtIndex_rightToLeft()
1666 {
1667     QSGView *canvas = createView();
1668
1669     TestModel model;
1670     for (int i = 0; i < 40; i++)
1671         model.addItem("Item" + QString::number(i), "");
1672
1673     QDeclarativeContext *ctxt = canvas->rootContext();
1674     ctxt->setContextProperty("testModel", &model);
1675     ctxt->setContextProperty("testTopToBottom", QVariant(true));
1676     ctxt->setContextProperty("testRightToLeft", QVariant(true));
1677
1678     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
1679     qApp->processEvents();
1680
1681     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1682     QTRY_VERIFY(gridview != 0);
1683
1684     QSGItem *contentItem = gridview->contentItem();
1685     QTRY_VERIFY(contentItem != 0);
1686
1687     // Confirm items positioned correctly
1688     int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1689     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
1690         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1691         if (!item) qWarning() << "Item" << i << "not found";
1692         QTRY_VERIFY(item);
1693         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
1694         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1695     }
1696
1697     // Position on a currently visible item
1698     gridview->positionViewAtIndex(6, QSGGridView::Beginning);
1699     QTRY_COMPARE(gridview->contentX(), -320.);
1700
1701     // Confirm items positioned correctly
1702     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1703     for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
1704         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1705         if (!item) qWarning() << "Item" << i << "not found";
1706         QTRY_VERIFY(item);
1707         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
1708         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1709     }
1710
1711     // Position on an item beyond the visible items
1712     gridview->positionViewAtIndex(21, QSGGridView::Beginning);
1713     QTRY_COMPARE(gridview->contentX(), -560.);
1714
1715     // Confirm items positioned correctly
1716     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1717     for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
1718         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1719         if (!item) qWarning() << "Item" << i << "not found";
1720         QTRY_VERIFY(item);
1721         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
1722         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1723     }
1724
1725     // Position on an item that would leave empty space if positioned at the top
1726     gridview->positionViewAtIndex(31, QSGGridView::Beginning);
1727     QTRY_COMPARE(gridview->contentX(), -640.);
1728
1729     // Confirm items positioned correctly
1730     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1731     for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
1732         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1733         if (!item) qWarning() << "Item" << i << "not found";
1734         QTRY_VERIFY(item);
1735         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
1736         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1737     }
1738
1739     // Position at the beginning again
1740     gridview->positionViewAtIndex(0, QSGGridView::Beginning);
1741     QTRY_COMPARE(gridview->contentX(), -240.);
1742
1743     // Confirm items positioned correctly
1744     itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
1745     for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
1746         QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
1747         if (!item) qWarning() << "Item" << i << "not found";
1748         QTRY_VERIFY(item);
1749         QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width()));
1750         QTRY_COMPARE(item->y(), qreal((i%5)*60));
1751     }
1752
1753     // Position at End
1754     gridview->positionViewAtIndex(30, QSGGridView::End);
1755     QTRY_COMPARE(gridview->contentX(), -560.);
1756
1757     // Position in Center
1758     gridview->positionViewAtIndex(15, QSGGridView::Center);
1759     QTRY_COMPARE(gridview->contentX(), -400.);
1760
1761     // Ensure at least partially visible
1762     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1763     QTRY_COMPARE(gridview->contentX(), -400.);
1764
1765     gridview->setContentX(-555.);
1766     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1767     QTRY_COMPARE(gridview->contentX(), -555.);
1768
1769     gridview->setContentX(-239);
1770     gridview->positionViewAtIndex(15, QSGGridView::Visible);
1771     QTRY_COMPARE(gridview->contentX(), -320.);
1772
1773     gridview->setContentX(-239);
1774     gridview->positionViewAtIndex(20, QSGGridView::Visible);
1775     QTRY_COMPARE(gridview->contentX(), -400.);
1776
1777     gridview->setContentX(-640);
1778     gridview->positionViewAtIndex(20, QSGGridView::Visible);
1779     QTRY_COMPARE(gridview->contentX(), -560.);
1780
1781     // Ensure completely visible
1782     gridview->setContentX(-400);
1783     gridview->positionViewAtIndex(20, QSGGridView::Contain);
1784     QTRY_COMPARE(gridview->contentX(), -400.);
1785
1786     gridview->setContentX(-315);
1787     gridview->positionViewAtIndex(15, QSGGridView::Contain);
1788     QTRY_COMPARE(gridview->contentX(), -320.);
1789
1790     gridview->setContentX(-640);
1791     gridview->positionViewAtIndex(20, QSGGridView::Contain);
1792     QTRY_COMPARE(gridview->contentX(), -560.);
1793
1794     delete canvas;
1795 }
1796
1797 void tst_QSGGridView::resetModel()
1798 {
1799     QSGView *canvas = createView();
1800
1801     QStringList strings;
1802     strings << "one" << "two" << "three";
1803     QStringListModel model(strings);
1804
1805     QDeclarativeContext *ctxt = canvas->rootContext();
1806     ctxt->setContextProperty("testModel", &model);
1807
1808     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/displaygrid.qml"));
1809     qApp->processEvents();
1810
1811     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1812     QTRY_VERIFY(gridview != 0);
1813
1814     QSGItem *contentItem = gridview->contentItem();
1815     QTRY_VERIFY(contentItem != 0);
1816
1817     QTRY_COMPARE(gridview->count(), model.rowCount());
1818
1819     for (int i = 0; i < model.rowCount(); ++i) {
1820         QSGText *display = findItem<QSGText>(contentItem, "displayText", i);
1821         QTRY_VERIFY(display != 0);
1822         QTRY_COMPARE(display->text(), strings.at(i));
1823     }
1824
1825     strings.clear();
1826     strings << "four" << "five" << "six" << "seven";
1827     model.setStringList(strings);
1828
1829     QTRY_COMPARE(gridview->count(), model.rowCount());
1830
1831     for (int i = 0; i < model.rowCount(); ++i) {
1832         QSGText *display = findItem<QSGText>(contentItem, "displayText", i);
1833         QTRY_VERIFY(display != 0);
1834         QTRY_COMPARE(display->text(), strings.at(i));
1835     }
1836
1837     delete canvas;
1838 }
1839
1840 void tst_QSGGridView::enforceRange()
1841 {
1842     QSGView *canvas = createView();
1843
1844     TestModel model;
1845     for (int i = 0; i < 30; i++)
1846         model.addItem("Item" + QString::number(i), "");
1847
1848     QDeclarativeContext *ctxt = canvas->rootContext();
1849     ctxt->setContextProperty("testModel", &model);
1850     ctxt->setContextProperty("testRightToLeft", QVariant(false));
1851     ctxt->setContextProperty("testTopToBottom", QVariant(false));
1852
1853     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview-enforcerange.qml"));
1854     qApp->processEvents();
1855     QVERIFY(canvas->rootObject() != 0);
1856
1857     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1858     QTRY_VERIFY(gridview != 0);
1859
1860     QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
1861     QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
1862     QTRY_COMPARE(gridview->highlightRangeMode(), QSGGridView::StrictlyEnforceRange);
1863
1864     QSGItem *contentItem = gridview->contentItem();
1865     QTRY_VERIFY(contentItem != 0);
1866
1867     // view should be positioned at the top of the range.
1868     QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
1869     QTRY_VERIFY(item);
1870     QTRY_COMPARE(gridview->contentY(), -100.0);
1871
1872     QSGText *name = findItem<QSGText>(contentItem, "textName", 0);
1873     QTRY_VERIFY(name != 0);
1874     QTRY_COMPARE(name->text(), model.name(0));
1875     QSGText *number = findItem<QSGText>(contentItem, "textNumber", 0);
1876     QTRY_VERIFY(number != 0);
1877     QTRY_COMPARE(number->text(), model.number(0));
1878
1879     // Check currentIndex is updated when contentItem moves
1880     gridview->setContentY(0);
1881     QTRY_COMPARE(gridview->currentIndex(), 2);
1882
1883     gridview->setCurrentIndex(5);
1884     QTRY_COMPARE(gridview->contentY(), 100.);
1885
1886     TestModel model2;
1887     for (int i = 0; i < 5; i++)
1888         model2.addItem("Item" + QString::number(i), "");
1889
1890     ctxt->setContextProperty("testModel", &model2);
1891     QCOMPARE(gridview->count(), 5);
1892
1893     delete canvas;
1894 }
1895
1896 void tst_QSGGridView::enforceRange_rightToLeft()
1897 {
1898     QSGView *canvas = createView();
1899
1900     TestModel model;
1901     for (int i = 0; i < 30; i++)
1902         model.addItem("Item" + QString::number(i), "");
1903
1904     QDeclarativeContext *ctxt = canvas->rootContext();
1905     ctxt->setContextProperty("testModel", &model);
1906     ctxt->setContextProperty("testRightToLeft", QVariant(true));
1907     ctxt->setContextProperty("testTopToBottom", QVariant(true));
1908
1909     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview-enforcerange.qml"));
1910     qApp->processEvents();
1911     QVERIFY(canvas->rootObject() != 0);
1912
1913     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1914     QTRY_VERIFY(gridview != 0);
1915
1916     QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0);
1917     QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0);
1918     QTRY_COMPARE(gridview->highlightRangeMode(), QSGGridView::StrictlyEnforceRange);
1919
1920     QSGItem *contentItem = gridview->contentItem();
1921     QTRY_VERIFY(contentItem != 0);
1922
1923     // view should be positioned at the top of the range.
1924     QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
1925     QTRY_VERIFY(item);
1926     QTRY_COMPARE(gridview->contentX(), -100.);
1927     QTRY_COMPARE(gridview->contentY(), 0.0);
1928
1929     QSGText *name = findItem<QSGText>(contentItem, "textName", 0);
1930     QTRY_VERIFY(name != 0);
1931     QTRY_COMPARE(name->text(), model.name(0));
1932     QSGText *number = findItem<QSGText>(contentItem, "textNumber", 0);
1933     QTRY_VERIFY(number != 0);
1934     QTRY_COMPARE(number->text(), model.number(0));
1935
1936     // Check currentIndex is updated when contentItem moves
1937     gridview->setContentX(-200);
1938     QTRY_COMPARE(gridview->currentIndex(), 3);
1939
1940     gridview->setCurrentIndex(7);
1941     QTRY_COMPARE(gridview->contentX(), -300.);
1942     QTRY_COMPARE(gridview->contentY(), 0.0);
1943
1944     TestModel model2;
1945     for (int i = 0; i < 5; i++)
1946         model2.addItem("Item" + QString::number(i), "");
1947
1948     ctxt->setContextProperty("testModel", &model2);
1949     QCOMPARE(gridview->count(), 5);
1950
1951     delete canvas;
1952 }
1953
1954 void tst_QSGGridView::QTBUG_8456()
1955 {
1956     QSGView *canvas = createView();
1957
1958     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/setindex.qml"));
1959     qApp->processEvents();
1960
1961     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1962     QTRY_VERIFY(gridview != 0);
1963
1964     QTRY_COMPARE(gridview->currentIndex(), 0);
1965
1966     delete canvas;
1967 }
1968
1969 void tst_QSGGridView::manualHighlight()
1970 {
1971     QSGView *canvas = createView();
1972
1973     QString filename(SRCDIR "/data/manual-highlight.qml");
1974     canvas->setSource(QUrl::fromLocalFile(filename));
1975
1976     qApp->processEvents();
1977
1978     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
1979     QTRY_VERIFY(gridview != 0);
1980
1981     QSGItem *contentItem = gridview->contentItem();
1982     QTRY_VERIFY(contentItem != 0);
1983
1984     QTRY_COMPARE(gridview->currentIndex(), 0);
1985     QTRY_COMPARE(gridview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 0));
1986     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
1987     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
1988
1989     gridview->setCurrentIndex(2);
1990
1991     QTRY_COMPARE(gridview->currentIndex(), 2);
1992     QTRY_COMPARE(gridview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 2));
1993     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
1994     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
1995
1996     gridview->positionViewAtIndex(8, QSGGridView::Contain);
1997
1998     QTRY_COMPARE(gridview->currentIndex(), 2);
1999     QTRY_COMPARE(gridview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 2));
2000     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2001     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2002
2003     gridview->setFlow(QSGGridView::TopToBottom);
2004     QTRY_COMPARE(gridview->flow(), QSGGridView::TopToBottom);
2005
2006     gridview->setCurrentIndex(0);
2007     QTRY_COMPARE(gridview->currentIndex(), 0);
2008     QTRY_COMPARE(gridview->currentItem(), findItem<QSGItem>(contentItem, "wrapper", 0));
2009     QTRY_COMPARE(gridview->highlightItem()->y() - 5, gridview->currentItem()->y());
2010     QTRY_COMPARE(gridview->highlightItem()->x() - 5, gridview->currentItem()->x());
2011
2012     delete canvas;
2013 }
2014
2015
2016 void tst_QSGGridView::footer()
2017 {
2018     QFETCH(QSGGridView::Flow, flow);
2019     QFETCH(Qt::LayoutDirection, layoutDirection);
2020     QFETCH(QPointF, initialFooterPos);
2021     QFETCH(QPointF, changedFooterPos);
2022     QFETCH(QPointF, initialContentPos);
2023     QFETCH(QPointF, changedContentPos);
2024     QFETCH(QPointF, firstDelegatePos);
2025
2026     QSGView *canvas = createView();
2027     canvas->show();
2028
2029     TestModel model;
2030     for (int i = 0; i < 7; i++)
2031         model.addItem("Item" + QString::number(i), "");
2032
2033     QDeclarativeContext *ctxt = canvas->rootContext();
2034     ctxt->setContextProperty("testModel", &model);
2035
2036     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/footer.qml"));
2037     qApp->processEvents();
2038
2039     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
2040     QTRY_VERIFY(gridview != 0);
2041     gridview->setFlow(flow);
2042     gridview->setLayoutDirection(layoutDirection);
2043
2044     QSGItem *contentItem = gridview->contentItem();
2045     QTRY_VERIFY(contentItem != 0);
2046
2047     QSGText *footer = findItem<QSGText>(contentItem, "footer");
2048     QVERIFY(footer);
2049
2050     QVERIFY(footer == gridview->footerItem());
2051
2052     QCOMPARE(footer->pos(), initialFooterPos);
2053     QCOMPARE(footer->width(), 100.);
2054     QCOMPARE(footer->height(), 30.);
2055     QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2056
2057     QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
2058     QVERIFY(item);
2059     QCOMPARE(item->pos(), firstDelegatePos);
2060
2061     if (flow == QSGGridView::LeftToRight) {
2062         // shrink by one row
2063         model.removeItem(2);
2064         QTRY_COMPARE(footer->y(), initialFooterPos.y() - gridview->cellHeight());
2065     } else {
2066         // shrink by one column
2067         model.removeItem(2);
2068         model.removeItem(3);
2069         if (layoutDirection == Qt::LeftToRight)
2070             QTRY_COMPARE(footer->x(), initialFooterPos.x() - gridview->cellWidth());
2071         else
2072             QTRY_COMPARE(footer->x(), initialFooterPos.x() + gridview->cellWidth());
2073     }
2074
2075     // remove all items
2076     model.clear();
2077
2078     QPointF posWhenNoItems(0, 0);
2079     if (layoutDirection == Qt::RightToLeft)
2080         posWhenNoItems.setX(flow == QSGGridView::LeftToRight ? gridview->width() - footer->width() : -footer->width());
2081     QTRY_COMPARE(footer->pos(), posWhenNoItems);
2082
2083     // if header is present, it's at a negative pos, so the footer should not move
2084     canvas->rootObject()->setProperty("showHeader", true);
2085     QVERIFY(findItem<QSGItem>(contentItem, "header") != 0);
2086     QTRY_COMPARE(footer->pos(), posWhenNoItems);
2087     canvas->rootObject()->setProperty("showHeader", false);
2088
2089     // add 30 items
2090     for (int i = 0; i < 30; i++)
2091         model.addItem("Item" + QString::number(i), "");
2092
2093     QSignalSpy footerItemSpy(gridview, SIGNAL(footerItemChanged()));
2094     QMetaObject::invokeMethod(canvas->rootObject(), "changeFooter");
2095
2096     QCOMPARE(footerItemSpy.count(), 1);
2097
2098     footer = findItem<QSGText>(contentItem, "footer");
2099     QVERIFY(!footer);
2100     footer = findItem<QSGText>(contentItem, "footer2");
2101     QVERIFY(footer);
2102
2103     QVERIFY(footer == gridview->footerItem());
2104
2105     QCOMPARE(footer->pos(), changedFooterPos);
2106     QCOMPARE(footer->width(), 50.);
2107     QCOMPARE(footer->height(), 20.);
2108     QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2109
2110     item = findItem<QSGItem>(contentItem, "wrapper", 0);
2111     QVERIFY(item);
2112     QCOMPARE(item->pos(), firstDelegatePos);
2113
2114     delete canvas;
2115 }
2116
2117 void tst_QSGGridView::footer_data()
2118 {
2119     QTest::addColumn<QSGGridView::Flow>("flow");
2120     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2121     QTest::addColumn<QPointF>("initialFooterPos");
2122     QTest::addColumn<QPointF>("changedFooterPos");
2123     QTest::addColumn<QPointF>("initialContentPos");
2124     QTest::addColumn<QPointF>("changedContentPos");
2125     QTest::addColumn<QPointF>("firstDelegatePos");
2126
2127     // footer1 = 100 x 30
2128     // footer2 = 100 x 20
2129     // cells = 80 * 60
2130     // view width = 240
2131
2132     // footer below items, bottom left
2133     QTest::newRow("flow left to right") << QSGGridView::LeftToRight << Qt::LeftToRight
2134         << QPointF(0, 3 * 60)  // 180 = height of 3 rows (cell height is 60)
2135         << QPointF(0, 10 * 60)  // 30 items = 10 rows
2136         << QPointF(0, 0)
2137         << QPointF(0, 0)
2138         << QPointF(0, 0);
2139
2140     // footer below items, bottom right
2141     QTest::newRow("flow left to right, layout right to left") << QSGGridView::LeftToRight << Qt::RightToLeft
2142         << QPointF(240 - 100, 3 * 60)
2143         << QPointF((240 - 100) + 50, 10 * 60)     // 50 = width diff between old and new footers
2144         << QPointF(0, 0)
2145         << QPointF(0, 0)
2146         << QPointF(240 - 80, 0);
2147
2148     // footer to right of items
2149     QTest::newRow("flow top to bottom, layout left to right") << QSGGridView::TopToBottom << Qt::LeftToRight
2150         << QPointF(2 * 80, 0)      // 2 columns, cell width 80
2151         << QPointF(6 * 80, 0)      // 30 items = 6 columns
2152         << QPointF(0, 0)
2153         << QPointF(0, 0)
2154         << QPointF(0, 0);
2155
2156     // footer to left of items
2157     QTest::newRow("flow top to bottom, layout right to left") << QSGGridView::TopToBottom << Qt::RightToLeft
2158         << QPointF(-(2 * 80) - 100, 0)
2159         << QPointF(-(6 * 80) - 50, 0)     // 50 = new footer width
2160         << QPointF(-240, 0)
2161         << QPointF(-240, 0)    // unchanged, footer change doesn't change content pos
2162         << QPointF(-80, 0);
2163 }
2164
2165 void tst_QSGGridView::header()
2166 {
2167     QFETCH(QSGGridView::Flow, flow);
2168     QFETCH(Qt::LayoutDirection, layoutDirection);
2169     QFETCH(QPointF, initialHeaderPos);
2170     QFETCH(QPointF, changedHeaderPos);
2171     QFETCH(QPointF, initialContentPos);
2172     QFETCH(QPointF, changedContentPos);
2173     QFETCH(QPointF, firstDelegatePos);
2174
2175     QSGView *canvas = createView();
2176
2177     TestModel model;
2178     for (int i = 0; i < 30; i++)
2179         model.addItem("Item" + QString::number(i), "");
2180
2181     QDeclarativeContext *ctxt = canvas->rootContext();
2182     ctxt->setContextProperty("testModel", &model);
2183
2184     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/header.qml"));
2185     qApp->processEvents();
2186
2187     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
2188     QTRY_VERIFY(gridview != 0);
2189     gridview->setFlow(flow);
2190     gridview->setLayoutDirection(layoutDirection);
2191
2192     QSGItem *contentItem = gridview->contentItem();
2193     QTRY_VERIFY(contentItem != 0);
2194
2195     QSGText *header = findItem<QSGText>(contentItem, "header");
2196     QVERIFY(header);
2197
2198     QVERIFY(header == gridview->headerItem());
2199
2200     QCOMPARE(header->pos(), initialHeaderPos);
2201     QCOMPARE(header->width(), 100.);
2202     QCOMPARE(header->height(), 30.);
2203     QCOMPARE(QPointF(gridview->contentX(), gridview->contentY()), initialContentPos);
2204
2205     QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", 0);
2206     QVERIFY(item);
2207     QCOMPARE(item->pos(), firstDelegatePos);
2208
2209     model.clear();
2210     QCOMPARE(header->pos(), initialHeaderPos); // header should stay where it is
2211
2212     for (int i = 0; i < 30; i++)
2213         model.addItem("Item" + QString::number(i), "");
2214
2215     QSignalSpy headerItemSpy(gridview, SIGNAL(headerItemChanged()));
2216     QMetaObject::invokeMethod(canvas->rootObject(), "changeHeader");
2217
2218     QCOMPARE(headerItemSpy.count(), 1);
2219
2220     header = findItem<QSGText>(contentItem, "header");
2221     QVERIFY(!header);
2222     header = findItem<QSGText>(contentItem, "header2");
2223     QVERIFY(header);
2224
2225     QVERIFY(header == gridview->headerItem());
2226
2227     QCOMPARE(header->pos(), changedHeaderPos);
2228     QCOMPARE(header->width(), 50.);
2229     QCOMPARE(header->height(), 20.);
2230     QTRY_COMPARE(QPointF(gridview->contentX(), gridview->contentY()), changedContentPos);
2231
2232     item = findItem<QSGItem>(contentItem, "wrapper", 0);
2233     QVERIFY(item);
2234     QCOMPARE(item->pos(), firstDelegatePos);
2235
2236     delete canvas;
2237 }
2238
2239 void tst_QSGGridView::header_data()
2240 {
2241     QTest::addColumn<QSGGridView::Flow>("flow");
2242     QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
2243     QTest::addColumn<QPointF>("initialHeaderPos");
2244     QTest::addColumn<QPointF>("changedHeaderPos");
2245     QTest::addColumn<QPointF>("initialContentPos");
2246     QTest::addColumn<QPointF>("changedContentPos");
2247     QTest::addColumn<QPointF>("firstDelegatePos");
2248
2249     // header1 = 100 x 30
2250     // header2 = 100 x 20
2251     // cells = 80 x 60
2252     // view width = 240
2253
2254     // header above items, top left
2255     QTest::newRow("flow left to right") << QSGGridView::LeftToRight << Qt::LeftToRight
2256         << QPointF(0, -30)
2257         << QPointF(0, -20)
2258         << QPointF(0, -30)
2259         << QPointF(0, -20)
2260         << QPointF(0, 0);
2261
2262     // header above items, top right
2263     QTest::newRow("flow left to right, layout right to left") << QSGGridView::LeftToRight << Qt::RightToLeft
2264         << QPointF(240 - 100, -30)
2265         << QPointF((240 - 100) + 50, -20)     // 50 = width diff between old and new headers
2266         << QPointF(0, -30)
2267         << QPointF(0, -20)
2268         << QPointF(160, 0);
2269
2270     // header to left of items
2271     QTest::newRow("flow top to bottom, layout left to right") << QSGGridView::TopToBottom << Qt::LeftToRight
2272         << QPointF(-100, 0)
2273         << QPointF(-50, 0)
2274         << QPointF(-100, 0)
2275         << QPointF(-50, 0)
2276         << QPointF(0, 0);
2277
2278     // header to right of items
2279     QTest::newRow("flow top to bottom, layout right to left") << QSGGridView::TopToBottom << Qt::RightToLeft
2280         << QPointF(0, 0)
2281         << QPointF(0, 0)
2282         << QPointF(-(240 - 100), 0)
2283         << QPointF(-(240 - 50), 0)
2284         << QPointF(-80, 0);
2285 }
2286
2287 void tst_QSGGridView::indexAt()
2288 {
2289     QSGView *canvas = createView();
2290
2291     TestModel model;
2292     model.addItem("Fred", "12345");
2293     model.addItem("John", "2345");
2294     model.addItem("Bob", "54321");
2295     model.addItem("Billy", "22345");
2296     model.addItem("Sam", "2945");
2297     model.addItem("Ben", "04321");
2298     model.addItem("Jim", "0780");
2299
2300     QDeclarativeContext *ctxt = canvas->rootContext();
2301     ctxt->setContextProperty("testModel", &model);
2302     ctxt->setContextProperty("testRightToLeft", QVariant(false));
2303     ctxt->setContextProperty("testTopToBottom", QVariant(false));
2304
2305     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml"));
2306     qApp->processEvents();
2307
2308     QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
2309     QTRY_VERIFY(gridview != 0);
2310
2311     QSGItem *contentItem = gridview->contentItem();
2312     QTRY_VERIFY(contentItem != 0);
2313
2314     QTRY_COMPARE(gridview->count(), model.count());
2315
2316     QCOMPARE(gridview->indexAt(0, 0), 0);
2317     QCOMPARE(gridview->indexAt(79, 59), 0);
2318     QCOMPARE(gridview->indexAt(80, 0), 1);
2319     QCOMPARE(gridview->indexAt(0, 60), 3);
2320     QCOMPARE(gridview->indexAt(240, 0), -1);
2321
2322     delete canvas;
2323 }
2324
2325 void tst_QSGGridView::onAdd()
2326 {
2327     QFETCH(int, initialItemCount);
2328     QFETCH(int, itemsToAdd);
2329
2330     const int delegateWidth = 50;
2331     const int delegateHeight = 100;
2332     TestModel model;
2333     QSGView *canvas = createView();
2334     canvas->setFixedSize(5 * delegateWidth, 5 * delegateHeight); // just ensure all items fit
2335
2336     // these initial items should not trigger GridView.onAdd
2337     for (int i=0; i<initialItemCount; i++)
2338         model.addItem("dummy value", "dummy value");
2339
2340     QDeclarativeContext *ctxt = canvas->rootContext();
2341     ctxt->setContextProperty("testModel", &model);
2342     ctxt->setContextProperty("delegateWidth", delegateWidth);
2343     ctxt->setContextProperty("delegateHeight", delegateHeight);
2344     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/attachedSignals.qml"));
2345
2346     QObject *object = canvas->rootObject();
2347     object->setProperty("width", canvas->width());
2348     object->setProperty("height", canvas->height());
2349     qApp->processEvents();
2350
2351     QList<QPair<QString, QString> > items;
2352     for (int i=0; i<itemsToAdd; i++)
2353         items << qMakePair(QString("value %1").arg(i), QString::number(i));
2354     model.addItems(items);
2355
2356     qApp->processEvents();
2357
2358     QVariantList result = object->property("addedDelegates").toList();
2359     QCOMPARE(result.count(), items.count());
2360     for (int i=0; i<items.count(); i++)
2361         QCOMPARE(result[i].toString(), items[i].first);
2362
2363     delete canvas;
2364 }
2365
2366 void tst_QSGGridView::onAdd_data()
2367 {
2368     QTest::addColumn<int>("initialItemCount");
2369     QTest::addColumn<int>("itemsToAdd");
2370
2371     QTest::newRow("0, add 1") << 0 << 1;
2372     QTest::newRow("0, add 2") << 0 << 2;
2373     QTest::newRow("0, add 10") << 0 << 10;
2374
2375     QTest::newRow("1, add 1") << 1 << 1;
2376     QTest::newRow("1, add 2") << 1 << 2;
2377     QTest::newRow("1, add 10") << 1 << 10;
2378
2379     QTest::newRow("5, add 1") << 5 << 1;
2380     QTest::newRow("5, add 2") << 5 << 2;
2381     QTest::newRow("5, add 10") << 5 << 10;
2382 }
2383
2384 void tst_QSGGridView::onRemove()
2385 {
2386     QFETCH(int, initialItemCount);
2387     QFETCH(int, indexToRemove);
2388     QFETCH(int, removeCount);
2389
2390     const int delegateWidth = 50;
2391     const int delegateHeight = 100;
2392     TestModel model;
2393     for (int i=0; i<initialItemCount; i++)
2394         model.addItem(QString("value %1").arg(i), "dummy value");
2395
2396     QSGView *canvas = createView();
2397     QDeclarativeContext *ctxt = canvas->rootContext();
2398     ctxt->setContextProperty("testModel", &model);
2399     ctxt->setContextProperty("delegateWidth", delegateWidth);
2400     ctxt->setContextProperty("delegateHeight", delegateHeight);
2401     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/attachedSignals.qml"));
2402     QObject *object = canvas->rootObject();
2403
2404     qApp->processEvents();
2405
2406     model.removeItems(indexToRemove, removeCount);
2407     qApp->processEvents();
2408     QCOMPARE(object->property("removedDelegateCount"), QVariant(removeCount));
2409
2410     delete canvas;
2411 }
2412
2413 void tst_QSGGridView::onRemove_data()
2414 {
2415     QTest::addColumn<int>("initialItemCount");
2416     QTest::addColumn<int>("indexToRemove");
2417     QTest::addColumn<int>("removeCount");
2418
2419     QTest::newRow("remove first") << 1 << 0 << 1;
2420     QTest::newRow("two items, remove first") << 2 << 0 << 1;
2421     QTest::newRow("two items, remove last") << 2 << 1 << 1;
2422     QTest::newRow("two items, remove all") << 2 << 0 << 2;
2423
2424     QTest::newRow("four items, remove first") << 4 << 0 << 1;
2425     QTest::newRow("four items, remove 0-2") << 4 << 0 << 2;
2426     QTest::newRow("four items, remove 1-3") << 4 << 1 << 2;
2427     QTest::newRow("four items, remove 2-4") << 4 << 2 << 2;
2428     QTest::newRow("four items, remove last") << 4 << 3 << 1;
2429     QTest::newRow("four items, remove all") << 4 << 0 << 4;
2430
2431     QTest::newRow("ten items, remove 1-8") << 10 << 0 << 8;
2432     QTest::newRow("ten items, remove 2-7") << 10 << 2 << 5;
2433     QTest::newRow("ten items, remove 4-10") << 10 << 4 << 6;
2434 }
2435
2436 void tst_QSGGridView::testQtQuick11Attributes()
2437 {
2438     QFETCH(QString, code);
2439     QFETCH(QString, warning);
2440     QFETCH(QString, error);
2441
2442     QDeclarativeEngine engine;
2443     QObject *obj;
2444
2445     QDeclarativeComponent valid(&engine);
2446     valid.setData("import QtQuick 1.1; GridView { " + code.toUtf8() + " }", QUrl(""));
2447     obj = valid.create();
2448     QVERIFY(obj);
2449     QVERIFY(valid.errorString().isEmpty());
2450     delete obj;
2451
2452     QDeclarativeComponent invalid(&engine);
2453     invalid.setData("import QtQuick 1.0; GridView { " + code.toUtf8() + " }", QUrl(""));
2454     QTest::ignoreMessage(QtWarningMsg, warning.toUtf8());
2455     obj = invalid.create();
2456     QCOMPARE(invalid.errorString(), error);
2457     delete obj;
2458 }
2459
2460 void tst_QSGGridView::testQtQuick11Attributes_data()
2461 {
2462     QTest::addColumn<QString>("code");
2463     QTest::addColumn<QString>("warning");
2464     QTest::addColumn<QString>("error");
2465
2466     QTest::newRow("positionViewAtBeginning") << "Component.onCompleted: positionViewAtBeginning()"
2467         << "<Unknown File>:1: ReferenceError: Can't find variable: positionViewAtBeginning"
2468         << "";
2469
2470     QTest::newRow("positionViewAtEnd") << "Component.onCompleted: positionViewAtEnd()"
2471         << "<Unknown File>:1: ReferenceError: Can't find variable: positionViewAtEnd"
2472         << "";
2473 }
2474
2475 QSGView *tst_QSGGridView::createView()
2476 {
2477     QSGView *canvas = new QSGView(0);
2478     canvas->setFixedSize(240,320);
2479
2480     return canvas;
2481 }
2482
2483 /*
2484    Find an item with the specified objectName.  If index is supplied then the
2485    item must also evaluate the {index} expression equal to index
2486 */
2487 template<typename T>
2488 T *tst_QSGGridView::findItem(QSGItem *parent, const QString &objectName, int index)
2489 {
2490     const QMetaObject &mo = T::staticMetaObject;
2491     //qDebug() << parent->childItems().count() << "children";
2492     for (int i = 0; i < parent->childItems().count(); ++i) {
2493         QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
2494         if(!item)
2495             continue;
2496         //qDebug() << "try" << item;
2497         if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
2498             if (index != -1) {
2499                 QDeclarativeContext *context = QDeclarativeEngine::contextForObject(item);
2500                 if (context) {
2501                     if (context->contextProperty("index").toInt() == index) {
2502                         return static_cast<T*>(item);
2503                     }
2504                 }
2505             } else {
2506                 return static_cast<T*>(item);
2507             }
2508         }
2509         item = findItem<T>(item, objectName, index);
2510         if (item)
2511             return static_cast<T*>(item);
2512     }
2513
2514     return 0;
2515 }
2516
2517 template<typename T>
2518 QList<T*> tst_QSGGridView::findItems(QSGItem *parent, const QString &objectName)
2519 {
2520     QList<T*> items;
2521     const QMetaObject &mo = T::staticMetaObject;
2522     //qDebug() << parent->childItems().count() << "children";
2523     for (int i = 0; i < parent->childItems().count(); ++i) {
2524         QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
2525         if(!item)
2526             continue;
2527         //qDebug() << "try" << item;
2528         if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
2529             items.append(static_cast<T*>(item));
2530             //qDebug() << " found:" << item;
2531         }
2532         items += findItems<T>(item, objectName);
2533     }
2534
2535     return items;
2536 }
2537
2538 void tst_QSGGridView::dumpTree(QSGItem *parent, int depth)
2539 {
2540     static QString padding("                       ");
2541     for (int i = 0; i < parent->childItems().count(); ++i) {
2542         QSGItem *item = qobject_cast<QSGItem*>(parent->childItems().at(i));
2543         if(!item)
2544             continue;
2545         QDeclarativeContext *context = QDeclarativeEngine::contextForObject(item);
2546         qDebug() << padding.left(depth*2) << item << (context ? context->contextProperty("index").toInt() : -1);
2547         dumpTree(item, depth+1);
2548     }
2549 }
2550
2551
2552 QTEST_MAIN(tst_QSGGridView)
2553
2554 #include "tst_qsggridview.moc"
2555