aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2011-10-12 14:30:18 +1000
committerQt by Nokia <qt-info@nokia.com>2011-10-14 08:40:15 +0200
commita5e8e81c1d7f3fdf9421cb590de5e4613baa7fa1 (patch)
tree7337eb0cdabd92278369d277e03877795e8a5255 /tests
parenta490c7caf1b7fba4fcd16573faf89591d8fb902d (diff)
Fix insertions above the visible index
The first visible item was repositioned incorrectly after an insertion above the visible index since any insertions above the position + cache buffer were ignored and not considered for repositioning the first item. GridView insertion code has changed to be similar to the ListView implementation to fix various issues when inserting above the visible index and to remove code that crossed indexes from visibleItems with model indexes and visible indexes. Also adds extra insertion tests for ListView and GridView. Change-Id: I5e129c605fdad733b61bd29850465b3b752fb63f Reviewed-on: http://codereview.qt-project.org/6485 Reviewed-by: Bea Lam <bea.lam@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/declarative/qsggridview/tst_qsggridview.cpp174
-rw-r--r--tests/auto/declarative/qsglistview/tst_qsglistview.cpp179
2 files changed, 353 insertions, 0 deletions
diff --git a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp
index 42bd6193bd..03975e295d 100644
--- a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp
+++ b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp
@@ -70,6 +70,8 @@ private slots:
void items();
void changed();
void inserted();
+ void inserted_more();
+ void inserted_more_data();
void removed();
void clear();
void moved();
@@ -417,6 +419,178 @@ void tst_QSGGridView::inserted()
delete canvas;
}
+void tst_QSGGridView::inserted_more()
+{
+ QFETCH(qreal, contentY);
+ QFETCH(int, insertIndex);
+ QFETCH(int, insertCount);
+ QFETCH(qreal, itemsOffsetAfterMove);
+
+ QSGText *name;
+ QSGText *number;
+ QSGView *canvas = createView();
+ canvas->show();
+
+ TestModel model;
+ for (int i = 0; i < 30; i++)
+ model.addItem("Item" + QString::number(i), "");
+
+ QDeclarativeContext *ctxt = canvas->rootContext();
+ ctxt->setContextProperty("testModel", &model);
+ ctxt->setContextProperty("testRightToLeft", QVariant(false));
+ ctxt->setContextProperty("testTopToBottom", QVariant(false));
+
+ canvas->setSource(QUrl::fromLocalFile(TESTDATA("/data/gridview1.qml")));
+ qApp->processEvents();
+
+ QSGGridView *gridview = findItem<QSGGridView>(canvas->rootObject(), "grid");
+ QTRY_VERIFY(gridview != 0);
+ QSGItem *contentItem = gridview->contentItem();
+ QTRY_VERIFY(contentItem != 0);
+
+ gridview->setContentY(contentY);
+
+ QList<QPair<QString, QString> > newData;
+ for (int i=0; i<insertCount; i++)
+ newData << qMakePair(QString("value %1").arg(i), QString::number(i));
+ model.insertItems(insertIndex, newData);
+ QTRY_COMPARE(gridview->property("count").toInt(), model.count());
+
+ // check visibleItems.first() is in correct position
+ QSGItem *item0 = findItem<QSGItem>(contentItem, "wrapper", 0);
+ QVERIFY(item0);
+ QCOMPARE(item0->y(), itemsOffsetAfterMove);
+
+ QList<QSGItem*> items = findItems<QSGItem>(contentItem, "wrapper");
+ int firstVisibleIndex = -1;
+ for (int i=0; i<items.count(); i++) {
+ if (items[i]->y() >= contentY) {
+ QDeclarativeExpression e(qmlContext(items[i]), items[i], "index");
+ firstVisibleIndex = e.evaluate().toInt();
+ break;
+ }
+ }
+ QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
+
+ // Confirm items positioned correctly and indexes correct
+ int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
+ for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
+ QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
+ QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
+
+ QCOMPARE(item->x(), (i%3)*80.0);
+ QCOMPARE(item->y(), (i/3)*60.0 + itemsOffsetAfterMove);
+
+ name = findItem<QSGText>(contentItem, "textName", i);
+ QVERIFY(name != 0);
+ QCOMPARE(name->text(), model.name(i));
+ number = findItem<QSGText>(contentItem, "textNumber", i);
+ QVERIFY(number != 0);
+ QCOMPARE(number->text(), model.number(i));
+ }
+
+ delete canvas;
+}
+
+void tst_QSGGridView::inserted_more_data()
+{
+ QTest::addColumn<qreal>("contentY");
+ QTest::addColumn<int>("insertIndex");
+ QTest::addColumn<int>("insertCount");
+ QTest::addColumn<qreal>("itemsOffsetAfterMove");
+
+ QTest::newRow("add 1, before visible items")
+ << 120.0 // show 6-23
+ << 5 << 1
+ << 0.0; // insert 1 above first visible, grid is rearranged; first visible moves forward within its row
+ // new 1st visible item is at 0
+
+ QTest::newRow("add 2, before visible items")
+ << 120.0 // show 6-23
+ << 5 << 2
+ << 0.0; // insert 2 above first visible, grid is rearranged; first visible moves forward within its row
+
+ QTest::newRow("add 3, before visible items")
+ << 120.0 // show 6-23
+ << 5 << 3
+ << -60.0; // insert 3 (1 row) above first visible in negative pos, first visible does not move
+
+ QTest::newRow("add 5, before visible items")
+ << 120.0 // show 6-23
+ << 5 << 5
+ << -60.0; // insert 1 row + 2 items above first visible, 1 row added at negative pos,
+ // grid is rearranged and first visible moves forward within its row
+
+ QTest::newRow("add 6, before visible items")
+ << 120.0 // show 6-23
+ << 5 << 6
+ << -60.0 * 2; // insert 2 rows above first visible in negative pos, first visible does not move
+
+
+
+ QTest::newRow("add 1, at start of visible, content at start")
+ << 0.0
+ << 0 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, at start of visible, content at start")
+ << 0.0
+ << 0 << 3
+ << 0.0;
+
+ QTest::newRow("add 1, at start of visible, content not at start")
+ << 120.0 // show 6-23
+ << 6 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, at start of visible, content not at start")
+ << 120.0 // show 6-23
+ << 6 << 3
+ << 0.0;
+
+
+ QTest::newRow("add 1, at end of visible, content at start")
+ << 0.0
+ << 17 << 1
+ << 0.0;
+
+ QTest::newRow("add 1, at end of visible, content at start")
+ << 0.0
+ << 17 << 3
+ << 0.0;
+
+ QTest::newRow("add 1, at end of visible, content not at start")
+ << 120.0 // show 6-23
+ << 23 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, at end of visible, content not at start")
+ << 120.0 // show 6-23
+ << 23 << 3
+ << 0.0;
+
+
+ QTest::newRow("add 1, after visible, content at start")
+ << 0.0
+ << 20 << 1
+ << 0.0;
+
+ QTest::newRow("add 1, after visible, content at start")
+ << 0.0
+ << 20 << 3
+ << 0.0;
+
+ QTest::newRow("add 1, after visible, content not at start")
+ << 120.0 // show 6-23
+ << 24 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, after visible, content not at start")
+ << 120.0 // show 6-23
+ << 24 << 3
+ << 0.0;
+}
+
void tst_QSGGridView::removed()
{
QSGView *canvas = createView();
diff --git a/tests/auto/declarative/qsglistview/tst_qsglistview.cpp b/tests/auto/declarative/qsglistview/tst_qsglistview.cpp
index 2b6705ff10..386a03b81a 100644
--- a/tests/auto/declarative/qsglistview/tst_qsglistview.cpp
+++ b/tests/auto/declarative/qsglistview/tst_qsglistview.cpp
@@ -76,7 +76,11 @@ private slots:
void qAbstractItemModel_changed();
void qListModelInterface_inserted();
+ void qListModelInterface_inserted_more();
+ void qListModelInterface_inserted_more_data();
void qAbstractItemModel_inserted();
+ void qAbstractItemModel_inserted_more();
+ void qAbstractItemModel_inserted_more_data();
void qListModelInterface_removed();
void qAbstractItemModel_removed();
@@ -140,6 +144,7 @@ private:
template <class T> void items();
template <class T> void changed();
template <class T> void inserted();
+ template <class T> void inserted_more();
template <class T> void removed(bool animated);
template <class T> void moved();
template <class T> void clear();
@@ -151,6 +156,7 @@ private:
QList<T*> findItems(QSGItem *parent, const QString &objectName);
void dumpTree(QSGItem *parent, int depth = 0);
+ void inserted_more_data();
void moved_data();
};
@@ -654,6 +660,159 @@ void tst_QSGListView::inserted()
}
template <class T>
+void tst_QSGListView::inserted_more()
+{
+ QFETCH(qreal, contentY);
+ QFETCH(int, insertIndex);
+ QFETCH(int, insertCount);
+ QFETCH(qreal, itemsOffsetAfterMove);
+
+ QSGText *name;
+ QSGText *number;
+ QSGView *canvas = createView();
+ canvas->show();
+
+ T model;
+ for (int i = 0; i < 30; i++)
+ model.addItem("Item" + QString::number(i), "");
+
+ QDeclarativeContext *ctxt = canvas->rootContext();
+ ctxt->setContextProperty("testModel", &model);
+
+ TestObject *testObject = new TestObject;
+ ctxt->setContextProperty("testObject", testObject);
+
+ canvas->setSource(QUrl::fromLocalFile(TESTDATA("/data/listviewtest.qml")));
+ qApp->processEvents();
+
+ QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
+ QTRY_VERIFY(listview != 0);
+ QSGItem *contentItem = listview->contentItem();
+ QTRY_VERIFY(contentItem != 0);
+
+ listview->setContentY(contentY);
+
+ QList<QPair<QString, QString> > newData;
+ for (int i=0; i<insertCount; i++)
+ newData << qMakePair(QString("value %1").arg(i), QString::number(i));
+ model.insertItems(insertIndex, newData);
+ QTRY_COMPARE(listview->property("count").toInt(), model.count());
+
+ // check visibleItems.first() is in correct position
+ QSGItem *item0 = findItem<QSGItem>(contentItem, "wrapper", 0);
+ QVERIFY(item0);
+ QCOMPARE(item0->y(), itemsOffsetAfterMove);
+
+ QList<QSGItem*> items = findItems<QSGItem>(contentItem, "wrapper");
+ int firstVisibleIndex = -1;
+ for (int i=0; i<items.count(); i++) {
+ if (items[i]->y() >= contentY) {
+ QDeclarativeExpression e(qmlContext(items[i]), items[i], "index");
+ firstVisibleIndex = e.evaluate().toInt();
+ break;
+ }
+ }
+ QVERIFY2(firstVisibleIndex >= 0, QTest::toString(firstVisibleIndex));
+
+ // Confirm items positioned correctly and indexes correct
+ int itemCount = findItems<QSGItem>(contentItem, "wrapper").count();
+ for (int i = firstVisibleIndex; i < model.count() && i < itemCount; ++i) {
+ QSGItem *item = findItem<QSGItem>(contentItem, "wrapper", i);
+ QVERIFY2(item, QTest::toString(QString("Item %1 not found").arg(i)));
+ QTRY_COMPARE(item->y(), i*20.0 + itemsOffsetAfterMove);
+ name = findItem<QSGText>(contentItem, "textName", i);
+ QVERIFY(name != 0);
+ QTRY_COMPARE(name->text(), model.name(i));
+ number = findItem<QSGText>(contentItem, "textNumber", i);
+ QVERIFY(number != 0);
+ QTRY_COMPARE(number->text(), model.number(i));
+ }
+
+ delete canvas;
+ delete testObject;
+}
+
+void tst_QSGListView::inserted_more_data()
+{
+ QTest::addColumn<qreal>("contentY");
+ QTest::addColumn<int>("insertIndex");
+ QTest::addColumn<int>("insertCount");
+ QTest::addColumn<qreal>("itemsOffsetAfterMove");
+
+ QTest::newRow("add 1, before visible items")
+ << 80.0 // show 4-19
+ << 3 << 1
+ << -20.0; // insert above first visible i.e. 0 is at -20, first visible should not move
+
+ QTest::newRow("add multiple, before visible")
+ << 80.0 // show 4-19
+ << 3 << 3
+ << -20.0 * 3; // again first visible should not move
+
+ QTest::newRow("add 1, at start of visible, content at start")
+ << 0.0
+ << 0 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, start of visible, content at start")
+ << 0.0
+ << 0 << 3
+ << 0.0;
+
+ QTest::newRow("add 1, at start of visible, content not at start")
+ << 80.0 // show 4-19
+ << 4 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, at start of visible, content not at start")
+ << 80.0 // show 4-19
+ << 4 << 3
+ << 0.0;
+
+
+ QTest::newRow("add 1, at end of visible, content at start")
+ << 0.0
+ << 15 << 1
+ << 0.0;
+
+ QTest::newRow("add 1, at end of visible, content at start")
+ << 0.0
+ << 15 << 3
+ << 0.0;
+
+ QTest::newRow("add 1, at end of visible, content not at start")
+ << 80.0 // show 4-19
+ << 19 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, at end of visible, content not at start")
+ << 80.0 // show 4-19
+ << 19 << 3
+ << 0.0;
+
+
+ QTest::newRow("add 1, after visible, content at start")
+ << 0.0
+ << 16 << 1
+ << 0.0;
+
+ QTest::newRow("add 1, after visible, content at start")
+ << 0.0
+ << 16 << 3
+ << 0.0;
+
+ QTest::newRow("add 1, after visible, content not at start")
+ << 80.0 // show 4-19
+ << 20 << 1
+ << 0.0;
+
+ QTest::newRow("add multiple, after visible, content not at start")
+ << 80.0 // show 4-19
+ << 20 << 3
+ << 0.0;
+}
+
+template <class T>
void tst_QSGListView::removed(bool animated)
{
QSGView *canvas = createView();
@@ -3666,11 +3825,31 @@ void tst_QSGListView::qListModelInterface_inserted()
inserted<TestModel>();
}
+void tst_QSGListView::qListModelInterface_inserted_more()
+{
+ inserted_more<TestModel>();
+}
+
+void tst_QSGListView::qListModelInterface_inserted_more_data()
+{
+ inserted_more_data();
+}
+
void tst_QSGListView::qAbstractItemModel_inserted()
{
inserted<TestModel2>();
}
+void tst_QSGListView::qAbstractItemModel_inserted_more()
+{
+ inserted_more<TestModel2>();
+}
+
+void tst_QSGListView::qAbstractItemModel_inserted_more_data()
+{
+ inserted_more_data();
+}
+
void tst_QSGListView::qListModelInterface_removed()
{
removed<TestModel>(false);