QAbstractItemView test: check that the selection model is in sync with the view

Change-Id: Ifca91154b47184a9d9a1979e1fba471517e16698
Reviewed-by: Stephen Kelly <ske@ableton.com>
This commit is contained in:
Giuseppe D'Angelo 2016-03-22 15:53:36 +01:00
parent 060e0f6628
commit 4ca7785161

View File

@ -253,6 +253,7 @@ private slots:
void shiftSelectionAfterChangingModelContents();
void QTBUG48968_reentrant_updateEditorGeometries();
void QTBUG50535_update_on_new_selection_model();
void testSelectionModelInSyncWithView();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -2082,5 +2083,59 @@ void tst_QAbstractItemView::QTBUG50535_update_on_new_selection_model()
QTRY_VERIFY(view.m_paintEventsCount > oldPaintEventsCount);
}
void tst_QAbstractItemView::testSelectionModelInSyncWithView()
{
QStandardItemModel model;
for (int i = 0; i < 10; ++i)
model.appendRow(new QStandardItem(QStringLiteral("%1").arg(i)));
class ListView : public QListView
{
public:
using QListView::selectedIndexes;
};
ListView view;
QVERIFY(!view.selectionModel());
view.setModel(&model);
QVERIFY(view.selectionModel());
QVERIFY(view.selectedIndexes().isEmpty());
QVERIFY(view.selectionModel()->selection().isEmpty());
view.setCurrentIndex(model.index(0, 0));
QCOMPARE(view.currentIndex(), model.index(0, 0));
QCOMPARE(view.selectionModel()->currentIndex(), model.index(0, 0));
view.selectionModel()->setCurrentIndex(model.index(1, 0), QItemSelectionModel::SelectCurrent);
QCOMPARE(view.currentIndex(), model.index(1, 0));
QCOMPARE(view.selectedIndexes(), QModelIndexList() << model.index(1, 0));
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0));
QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(1, 0));
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QItemSelectionModel selectionModel(&model);
selectionModel.setCurrentIndex(model.index(2, 0), QItemSelectionModel::Current);
view.setSelectionModel(&selectionModel);
QCOMPARE(view.currentIndex(), model.index(2, 0));
QCOMPARE(view.selectedIndexes(), QModelIndexList());
QCOMPARE(view.selectionModel()->currentIndex(), model.index(2, 0));
QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList());
QItemSelectionModel selectionModel2(&model);
selectionModel2.select(model.index(0, 0), QItemSelectionModel::ClearAndSelect);
selectionModel2.setCurrentIndex(model.index(1, 0), QItemSelectionModel::Current);
view.setSelectionModel(&selectionModel2);
QCOMPARE(view.currentIndex(), model.index(1, 0));
QCOMPARE(view.selectedIndexes(), QModelIndexList() << model.index(0, 0));
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0));
QCOMPARE(view.selectionModel()->selection().indexes(), QModelIndexList() << model.index(0, 0));
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"