diff options
Diffstat (limited to 'tests/auto/corelib/itemmodels/qitemselectionmodel')
-rw-r--r-- | tests/auto/corelib/itemmodels/qitemselectionmodel/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp | 249 |
2 files changed, 155 insertions, 103 deletions
diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/CMakeLists.txt b/tests/auto/corelib/itemmodels/qitemselectionmodel/CMakeLists.txt index 8eadf67b66..88ec493305 100644 --- a/tests/auto/corelib/itemmodels/qitemselectionmodel/CMakeLists.txt +++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/CMakeLists.txt @@ -1,9 +1,16 @@ -# Generated from qitemselectionmodel.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qitemselectionmodel Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qitemselectionmodel LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qitemselectionmodel SOURCES tst_qitemselectionmodel.cpp diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp index 7c4dbaedbf..08233a1f7b 100644 --- a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp +++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp @@ -1,32 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> +#include <QtTest/private/qcomparisontesthelper_p.h> #include <QtTest/private/qpropertytesthelper_p.h> #include <QSignalSpy> @@ -49,6 +25,7 @@ public slots: void cleanupTestCase(); void init(); private slots: + void compareCompiles(); void clear_data(); void clear(); void clearAndSelect(); @@ -80,8 +57,10 @@ private slots: void merge(); void isRowSelected(); void childrenDeselectionSignal(); +#if QT_CONFIG(proxymodel) void layoutChangedWithAllSelected1(); void layoutChangedWithAllSelected2(); +#endif void layoutChangedTreeSelection(); void deselectRemovedMiddleRange(); void setModel(); @@ -98,14 +77,20 @@ private slots: void QTBUG48402(); void QTBUG58851_data(); +#if QT_CONFIG(proxymodel) void QTBUG58851(); +#endif void QTBUG18001_data(); void QTBUG18001(); void QTBUG93305(); + void testSignalsDisconnection(); + void destroyModel(); + private: + static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); QAbstractItemModel *model; QItemSelectionModel *selection; }; @@ -166,8 +151,8 @@ QDataStream &operator>>(QDataStream &s, QModelIndex &output) QDataStream &operator<<(QDataStream &s, const QModelIndexList &input) { - s << input.count(); - for (int i=0; i<input.count(); ++i) + s << input.size(); + for (int i=0; i<input.size(); ++i) s << input.at(i); return s; } @@ -234,6 +219,11 @@ void tst_QItemSelectionModel::init() model->insertRow(0, QModelIndex()); } +void tst_QItemSelectionModel::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QItemSelectionRange>(); +} + void tst_QItemSelectionModel::clear_data() { QTest::addColumn<QModelIndexList>("indexList"); @@ -303,7 +293,7 @@ void tst_QItemSelectionModel::clear() QFETCH(IntList, commandList); // do selections - for (int i=0; i<indexList.count(); ++i) { + for (int i=0; i<indexList.size(); ++i) { selection->select(indexList.at(i), (QItemSelectionModel::SelectionFlags)commandList.at(i)); } // test that we have selected items @@ -317,7 +307,7 @@ void tst_QItemSelectionModel::clearAndSelect() { // populate selectionmodel selection->select(model->index(1, 1, QModelIndex()), QItemSelectionModel::Select); - QCOMPARE(selection->selectedIndexes().count(), 1); + QCOMPARE(selection->selectedIndexes().size(), 1); QVERIFY(selection->hasSelection()); // ClearAndSelect with empty selection @@ -335,26 +325,26 @@ void tst_QItemSelectionModel::toggleSelection() //and hasSelection returns the correct value selection->clearSelection(); - QCOMPARE(selection->selectedIndexes().count(), 0); + QCOMPARE(selection->selectedIndexes().size(), 0); QVERIFY(selection->hasSelection()==false); QModelIndex index=model->index(1, 1, QModelIndex()); // populate selectionmodel selection->select(index, QItemSelectionModel::Toggle); - QCOMPARE(selection->selectedIndexes().count(), 1); + QCOMPARE(selection->selectedIndexes().size(), 1); QVERIFY(selection->hasSelection()==true); selection->select(index, QItemSelectionModel::Toggle); - QCOMPARE(selection->selectedIndexes().count(), 0); + QCOMPARE(selection->selectedIndexes().size(), 0); QVERIFY(selection->hasSelection()==false); // populate selectionmodel with rows selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows); - QCOMPARE(selection->selectedIndexes().count(), model->columnCount()); + QCOMPARE(selection->selectedIndexes().size(), model->columnCount()); QVERIFY(selection->hasSelection()==true); selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows); - QCOMPARE(selection->selectedIndexes().count(), 0); + QCOMPARE(selection->selectedIndexes().size(), 0); QVERIFY(selection->hasSelection()==false); } @@ -1246,7 +1236,7 @@ void tst_QItemSelectionModel::select() int lastCommand = 0; // do selections - for (int i = 0; i<commandList.count(); ++i) { + for (int i = 0; i<commandList.size(); ++i) { if (useRanges) { selection->select(QItemSelection(indexList.at(2*i), indexList.at(2*i+1)), (QItemSelectionModel::SelectionFlags)commandList.at(i)); @@ -1263,13 +1253,13 @@ void tst_QItemSelectionModel::select() QVERIFY(selection->hasSelection()!=selectedList.isEmpty()); // test that the number of indices are as expected - QVERIFY2(selectedList.count() == expectedList.count(), + QVERIFY2(selectedList.size() == expectedList.size(), QString("expected indices: %1 actual indices: %2") - .arg(expectedList.count()) - .arg(selectedList.count()).toLatin1()); + .arg(expectedList.size()) + .arg(selectedList.size()).toLatin1()); // test existence of each index - for (int i=0; i<expectedList.count(); ++i) { + for (int i=0; i<expectedList.size(); ++i) { QVERIFY2(selectedList.contains(expectedList.at(i)), QString("expected index(%1, %2) not found in selectedIndexes()") .arg(expectedList.at(i).row()) @@ -1277,7 +1267,7 @@ void tst_QItemSelectionModel::select() } // test that isSelected agrees - for (int i=0; i<indexList.count(); ++i) { + for (int i=0; i<indexList.size(); ++i) { QModelIndex idx = indexList.at(i); QVERIFY2(selection->isSelected(idx) == selectedList.contains(idx), QString("isSelected(index: %1, %2) does not match selectedIndexes()") @@ -1288,7 +1278,7 @@ void tst_QItemSelectionModel::select() //for now we assume Rows/Columns flag is the same for all commands, therefore we just check lastCommand // test that isRowSelected agrees if (lastCommand & QItemSelectionModel::Rows) { - for (int i=0; i<selectedList.count(); ++i) + for (int i=0; i<selectedList.size(); ++i) QVERIFY2(selection->isRowSelected(selectedList.at(i).row(), model->parent(selectedList.at(i))), QString("isRowSelected(row: %1) does not match selectedIndexes()") @@ -1297,7 +1287,7 @@ void tst_QItemSelectionModel::select() // test that isColumnSelected agrees if (lastCommand & QItemSelectionModel::Columns) { - for (int i=0; i<selectedList.count(); ++i) + for (int i=0; i<selectedList.size(); ++i) QVERIFY2(selection->isColumnSelected(selectedList.at(i).column(), model->parent(selectedList.at(i))), QString("isColumnSelected(column: %1) does not match selectedIndexes()") @@ -1481,8 +1471,8 @@ void tst_QItemSelectionModel::persistentselections() QCOMPARE(model->columnCount(QModelIndex()), 5); // do selections - for (int i=0; i<commandList.count(); ++i) { - if (indexList.count() == commandList.count()) { + for (int i=0; i<commandList.size(); ++i) { + if (indexList.size() == commandList.size()) { QModelIndex index = model->index(indexList.at(i).first, indexList.at(i).second, QModelIndex()); @@ -1503,18 +1493,18 @@ void tst_QItemSelectionModel::persistentselections() QVERIFY(selection->hasSelection()); // insert/delete row and/or columns - if (insertRows.count() > 1) + if (insertRows.size() > 1) model->insertRows(insertRows.at(0), insertRows.at(1), QModelIndex()); - if (insertColumns.count() > 1) + if (insertColumns.size() > 1) model->insertColumns(insertColumns.at(0), insertColumns.at(1), QModelIndex()); - if (deleteRows.count() > 1) + if (deleteRows.size() > 1) model->removeRows(deleteRows.at(0), deleteRows.at(1), QModelIndex()); - if (deleteColumns.count() > 1) + if (deleteColumns.size() > 1) model->removeColumns(deleteColumns.at(0), deleteColumns.at(1), QModelIndex()); // check that the selected items are the correct number and indexes QModelIndexList selectedList = selection->selectedIndexes(); - QCOMPARE(selectedList.count(), expectedList.count()); + QCOMPARE(selectedList.size(), expectedList.size()); for (const auto &pair : expectedList) { QModelIndex index = model->index(pair.first, pair.second, QModelIndex()); QVERIFY(selectedList.contains(index)); @@ -1540,7 +1530,7 @@ void tst_QItemSelectionModel::resetModel() selectionModel->select(QItemSelection(model.index(0, 0), model.index(5, 5)), QItemSelectionModel::Select); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); model.reset(); @@ -1549,8 +1539,8 @@ void tst_QItemSelectionModel::resetModel() selectionModel->select(QItemSelection(model.index(0, 0), model.index(5, 5)), QItemSelectionModel::Select); - QCOMPARE(spy.count(), 2); - QCOMPARE(spy.at(1).count(), 2); + QCOMPARE(spy.size(), 2); + QCOMPARE(spy.at(1).size(), 2); // make sure we don't get an "old selection" QCOMPARE(spy.at(1).at(1).userType(), qMetaTypeId<QItemSelection>()); QVERIFY(qvariant_cast<QItemSelection>(spy.at(1).at(1)).isEmpty()); @@ -1605,14 +1595,14 @@ void tst_QItemSelectionModel::removeRows() QModelIndex br = model.index(selectBottom, selectRight); selections.select(QItemSelection(tl, br), QItemSelectionModel::ClearAndSelect); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QVERIFY(selections.isSelected(tl)); QVERIFY(selections.isSelected(br)); QVERIFY(selections.hasSelection()); model.removeRows(removeTop, removeBottom - removeTop + 1); - QCOMPARE(spy.count(), 2); + QCOMPARE(spy.size(), 2); tl = model.index(expectedTop, expectedLeft); br = model.index(expectedBottom, expectedRight); QVERIFY(selections.isSelected(tl)); @@ -1668,14 +1658,14 @@ void tst_QItemSelectionModel::removeColumns() QModelIndex br = model.index(selectBottom, selectRight); selections.select(QItemSelection(tl, br), QItemSelectionModel::ClearAndSelect); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QVERIFY(selections.isSelected(tl)); QVERIFY(selections.isSelected(br)); QVERIFY(selections.hasSelection()); model.removeColumns(removeLeft, removeRight - removeLeft + 1); - QCOMPARE(spy.count(), 2); + QCOMPARE(spy.size(), 2); tl = model.index(expectedTop, expectedLeft); br = model.index(expectedBottom, expectedRight); QVERIFY(selections.isSelected(tl)); @@ -1746,7 +1736,7 @@ void tst_QItemSelectionModel::modelLayoutChanged() QFETCH(int, sortColumn); QFETCH(IntPairPairList, expectedSelectedRanges); - MyStandardItemModel model(items.at(0).count(), items.count()); + MyStandardItemModel model(items.at(0).size(), items.size()); // initialize model data for (int i = 0; i < model.rowCount(); ++i) { for (int j = 0; j < model.columnCount(); ++j) { @@ -1771,10 +1761,10 @@ void tst_QItemSelectionModel::modelLayoutChanged() // verify that selection is as expected QItemSelection selection = selectionModel.selection(); - QCOMPARE(selection.count(), expectedSelectedRanges.count()); + QCOMPARE(selection.size(), expectedSelectedRanges.size()); QCOMPARE(selectionModel.hasSelection(), !expectedSelectedRanges.isEmpty()); - for (int i = 0; i < expectedSelectedRanges.count(); ++i) { + for (int i = 0; i < expectedSelectedRanges.size(); ++i) { IntPairPair expectedRange = expectedSelectedRanges.at(i); IntPair expectedTl = expectedRange.first; IntPair expectedBr = expectedRange.second; @@ -1828,21 +1818,21 @@ void tst_QItemSelectionModel::selectedRows() MyStandardItemModel model(rowCount, columnCount); QItemSelectionModel selectionModel(&model); - for (int i = 0; i < selectRows.count(); ++i) + for (int i = 0; i < selectRows.size(); ++i) selectionModel.select(model.index(selectRows.at(i), 0), QItemSelectionModel::Select |QItemSelectionModel::Rows); - for (int j = 0; j < selectRows.count(); ++j) + for (int j = 0; j < selectRows.size(); ++j) QVERIFY(selectionModel.isRowSelected(expectedRows.at(j), QModelIndex())); - for (int k = 0; k < selectRows.count(); ++k) + for (int k = 0; k < selectRows.size(); ++k) QVERIFY(!selectionModel.isRowSelected(unexpectedRows.at(k), QModelIndex())); QModelIndexList selectedRowIndexes = selectionModel.selectedRows(column); - QCOMPARE(selectedRowIndexes.count(), expectedRows.count()); + QCOMPARE(selectedRowIndexes.size(), expectedRows.size()); std::sort(selectedRowIndexes.begin(), selectedRowIndexes.end()); - for (int l = 0; l < selectedRowIndexes.count(); ++l) { + for (int l = 0; l < selectedRowIndexes.size(); ++l) { QCOMPARE(selectedRowIndexes.at(l).row(), expectedRows.at(l)); QCOMPARE(selectedRowIndexes.at(l).column(), column); } @@ -1888,21 +1878,21 @@ void tst_QItemSelectionModel::selectedColumns() MyStandardItemModel model(rowCount, columnCount); QItemSelectionModel selectionModel(&model); - for (int i = 0; i < selectColumns.count(); ++i) + for (int i = 0; i < selectColumns.size(); ++i) selectionModel.select(model.index(0, selectColumns.at(i)), QItemSelectionModel::Select |QItemSelectionModel::Columns); - for (int j = 0; j < selectColumns.count(); ++j) + for (int j = 0; j < selectColumns.size(); ++j) QVERIFY(selectionModel.isColumnSelected(expectedColumns.at(j), QModelIndex())); - for (int k = 0; k < selectColumns.count(); ++k) + for (int k = 0; k < selectColumns.size(); ++k) QVERIFY(!selectionModel.isColumnSelected(unexpectedColumns.at(k), QModelIndex())); QModelIndexList selectedColumnIndexes = selectionModel.selectedColumns(row); - QCOMPARE(selectedColumnIndexes.count(), expectedColumns.count()); + QCOMPARE(selectedColumnIndexes.size(), expectedColumns.size()); std::sort(selectedColumnIndexes.begin(), selectedColumnIndexes.end()); - for (int l = 0; l < selectedColumnIndexes.count(); ++l) { + for (int l = 0; l < selectedColumnIndexes.size(); ++l) { QCOMPARE(selectedColumnIndexes.at(l).column(), expectedColumns.at(l)); QCOMPARE(selectedColumnIndexes.at(l).row(), row); } @@ -1933,18 +1923,18 @@ void tst_QItemSelectionModel::setCurrentIndex() treemodel->index(0, 0, treemodel->index(1, 0)), QItemSelectionModel::SelectCurrent); - QCOMPARE(currentSpy.count(), 1); - QCOMPARE(rowSpy.count(), 1); - QCOMPARE(columnSpy.count(), 1); + QCOMPARE(currentSpy.size(), 1); + QCOMPARE(rowSpy.size(), 1); + QCOMPARE(columnSpy.size(), 1); // Select another row in the same parent selectionModel.setCurrentIndex( treemodel->index(1, 0, treemodel->index(1, 0)), QItemSelectionModel::SelectCurrent); - QCOMPARE(currentSpy.count(), 2); - QCOMPARE(rowSpy.count(), 2); - QCOMPARE(columnSpy.count(), 1); + QCOMPARE(currentSpy.size(), 2); + QCOMPARE(rowSpy.size(), 2); + QCOMPARE(columnSpy.size(), 1); } void tst_QItemSelectionModel::splitOnInsert() @@ -2068,14 +2058,14 @@ void tst_QItemSelectionModel::unselectable() } QItemSelectionModel selectionModel(&model); selectionModel.select(QItemSelection(model.index(0, 0), model.index(9, 0)), QItemSelectionModel::Select); - QCOMPARE(selectionModel.selectedIndexes().count(), 10); - QCOMPARE(selectionModel.selectedRows().count(), 10); + QCOMPARE(selectionModel.selectedIndexes().size(), 10); + QCOMPARE(selectionModel.selectedRows().size(), 10); QVERIFY(selectionModel.hasSelection()); for (int j = 0; j < 10; ++j) model.item(j)->setFlags({ }); QVERIFY(!selectionModel.hasSelection()); - QCOMPARE(selectionModel.selectedIndexes().count(), 0); - QCOMPARE(selectionModel.selectedRows().count(), 0); + QCOMPARE(selectionModel.selectedIndexes().size(), 0); + QCOMPARE(selectionModel.selectedRows().size(), 0); } void tst_QItemSelectionModel::selectedIndexes() @@ -2089,8 +2079,8 @@ void tst_QItemSelectionModel::selectedIndexes() //we select the 1st row selectionModel.select(selection, QItemSelectionModel::Rows | QItemSelectionModel::Select); - QCOMPARE(selectionModel.selectedRows().count(), 1); - QCOMPARE(selectionModel.selectedIndexes().count(), model.columnCount()); + QCOMPARE(selectionModel.selectedRows().size(), 1); + QCOMPARE(selectionModel.selectedIndexes().size(), model.columnCount()); } @@ -2128,14 +2118,14 @@ void tst_QItemSelectionModel::layoutChanged() QtTestTableModel model(1,1); QItemSelectionModel selectionModel(&model); selectionModel.select(model.index(0,0), QItemSelectionModel::Select); - QCOMPARE(selectionModel.selectedIndexes().count() , 1); + QCOMPARE(selectionModel.selectedIndexes().size() , 1); emit model.layoutAboutToBeChanged(); model.row_count = 5; emit model.layoutChanged(); //The selection should not change. - QCOMPARE(selectionModel.selectedIndexes().count() , 1); + QCOMPARE(selectionModel.selectedIndexes().size() , 1); QCOMPARE(selectionModel.selectedIndexes().first() , model.index(0,0)); } @@ -2215,7 +2205,7 @@ void tst_QItemSelectionModel::isRowSelected() model.setData(model.index(0,0), 0, Qt::UserRole - 1); QItemSelectionModel sel(&model); sel.select( QItemSelection(model.index(0,0), model.index(0, 1)), QItemSelectionModel::Select); - QCOMPARE(sel.selectedIndexes().count(), 1); + QCOMPARE(sel.selectedIndexes().size(), 1); QVERIFY(sel.isRowSelected(0, QModelIndex())); } @@ -2240,7 +2230,7 @@ void tst_QItemSelectionModel::childrenDeselectionSignal() QSignalSpy deselectSpy(&selectionModel, &QItemSelectionModel::selectionChanged); QVERIFY(deselectSpy.isValid()); model.removeRows(0, 1, root); - QCOMPARE(deselectSpy.count(), 1); + QCOMPARE(deselectSpy.size(), 1); // More testing stress for the patch. model.clear(); @@ -2269,11 +2259,12 @@ void tst_QItemSelectionModel::childrenDeselectionSignal() QVERIFY(selectionModel.selection().contains(sel2)); deselectSpy.clear(); model.removeRow(0, model.index(0, 0)); - QCOMPARE(deselectSpy.count(), 1); + QCOMPARE(deselectSpy.size(), 1); QVERIFY(!selectionModel.selection().contains(sel)); QVERIFY(selectionModel.selection().contains(sel2)); } +#if QT_CONFIG(proxymodel) void tst_QItemSelectionModel::layoutChangedWithAllSelected1() { QStringListModel model( QStringList() << "foo" << "bar" << "foo2"); @@ -2291,7 +2282,7 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected1() selection.select(QItemSelection(indexList.first(), indexList.last()), QItemSelectionModel::Select); //let's check the selection hasn't changed - QCOMPARE(selection.selectedIndexes().count(), indexList.count()); + QCOMPARE(selection.selectedIndexes().size(), indexList.size()); for (const auto &index : indexList) QVERIFY(selection.isSelected(index)); @@ -2299,7 +2290,7 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected1() QCOMPARE(proxy.rowCount(), 3); //let's check the selection hasn't changed - QCOMPARE(selection.selectedIndexes().count(), indexList.count()); + QCOMPARE(selection.selectedIndexes().size(), indexList.size()); for (const auto &index : indexList) QVERIFY(selection.isSelected(index)); } @@ -2348,10 +2339,11 @@ void tst_QItemSelectionModel::layoutChangedWithAllSelected2() QCOMPARE(proxy.rowCount(), int(cNumRows)); //let's check the selection hasn't changed - QCOMPARE(selection.selectedIndexes().count(), indexList.count()); + QCOMPARE(selection.selectedIndexes().size(), indexList.size()); for (const auto &index : indexList) QVERIFY(selection.isSelected(index)); } +#endif // This test is a regression test for QTBUG-2804. void tst_QItemSelectionModel::layoutChangedTreeSelection() @@ -2372,11 +2364,11 @@ void tst_QItemSelectionModel::layoutChangedTreeSelection() selModel.select(sub23.index(), QItemSelectionModel::Select); QModelIndexList list = selModel.selectedIndexes(); - QCOMPARE(list.count(), 4); + QCOMPARE(list.size(), 4); model.sort(0); //this will provoke a relayout - QCOMPARE(selModel.selectedIndexes().count(), 4); + QCOMPARE(selModel.selectedIndexes().size(), 4); } class RemovalObserver : public QObject @@ -2435,7 +2427,7 @@ void tst_QItemSelectionModel::setModel() QStringListModel model(QStringList() << "Blah" << "Blah" << "Blah"); sel.setModel(&model); QCOMPARE(sel.model(), &model); - QCOMPARE(modelChangedSpy.count(), 1); + QCOMPARE(modelChangedSpy.size(), 1); sel.select(model.index(0), QItemSelectionModel::Select); QVERIFY(!sel.selection().isEmpty()); sel.setModel(0); @@ -2735,6 +2727,9 @@ void tst_QItemSelectionModel::QTBUG48402() model.removeRows(removeTop, removeBottom - removeTop + 1); QCOMPARE(QItemSelectionRange(helper.tl, helper.br), QItemSelectionRange(dtl, dbr)); + QT_TEST_EQUALITY_OPS(QItemSelectionRange(helper.tl, helper.br), QItemSelectionRange(dtl, dbr), true); + QT_TEST_EQUALITY_OPS(QItemSelectionRange(), QItemSelectionRange(), true); + QT_TEST_EQUALITY_OPS(QItemSelectionRange(helper.tl, helper.br), QItemSelectionRange(), false); } void tst_QItemSelectionModel::QTBUG58851_data() @@ -2763,6 +2758,7 @@ void tst_QItemSelectionModel::QTBUG58851_data() << IntPair(2, 3)); } +#if QT_CONFIG(proxymodel) void tst_QItemSelectionModel::QTBUG58851() { using IntPair = std::pair<int, int>; @@ -2802,11 +2798,12 @@ void tst_QItemSelectionModel::QTBUG58851() QVERIFY(selections.isSelected(i)); } proxy.sort(1, Qt::DescendingOrder); - QCOMPARE(selections.selectedIndexes().count(), (int)expectedSelectedIndexes.size()); + QCOMPARE(selections.selectedIndexes().size(), (int)expectedSelectedIndexes.size()); for (const QPersistentModelIndex &i : expectedSelectedIndexes) { QVERIFY(selections.isSelected(i)); } } +#endif void tst_QItemSelectionModel::QTBUG18001_data() { @@ -2876,12 +2873,12 @@ void tst_QItemSelectionModel::QTBUG18001() QItemSelectionModel selectionModel(&model); - for (int i = 0; i < indexesToSelect.count(); ++i) { + for (int i = 0; i < indexesToSelect.size(); ++i) { QModelIndex idx = model.index( indexesToSelect.at(i).first, indexesToSelect.at(i).second ); selectionModel.select(idx, QItemSelectionModel::SelectionFlag(selectionCommands.at(i))); } - for (int i = 0; i < expectedSelectedRows.count(); ++i) { + for (int i = 0; i < expectedSelectedRows.size(); ++i) { const bool expected = expectedSelectedRows.at(i); const bool actual = selectionModel.isRowSelected(i, QModelIndex()); QByteArray description = QByteArray("Row ") + QByteArray::number(i) @@ -2890,7 +2887,7 @@ void tst_QItemSelectionModel::QTBUG18001() QVERIFY2(expected == actual, description.data()); } - for (int i = 0; i < expectedSelectedColums.count(); ++i) { + for (int i = 0; i < expectedSelectedColums.size(); ++i) { const bool expected = expectedSelectedColums.at(i); const bool actual = selectionModel.isColumnSelected(i, QModelIndex()); QByteArray description = QByteArray("Col ") + QByteArray::number(i) @@ -2913,7 +2910,7 @@ void tst_QItemSelectionModel::QTBUG93305() QModelIndex index = model->index(1, 0, QModelIndex()); selection->select(index, QItemSelectionModel::ClearAndSelect); QVERIFY(selection->hasSelection()); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); // removing row 0 does not change which cells are selected, but it // does change the row number of the selected cells. Thus it changes @@ -2924,7 +2921,7 @@ void tst_QItemSelectionModel::QTBUG93305() // delete row 0 model->removeRows(0, 1, QModelIndex()); QVERIFY(selection->hasSelection()); - QCOMPARE(spy.count(), 2); + QCOMPARE(spy.size(), 2); // inserting a row before the first row again does not change which cells // are selected, but does change the row number of the selected cells. @@ -2934,12 +2931,60 @@ void tst_QItemSelectionModel::QTBUG93305() // insert row 0 again model->insertRows(0, 1, QModelIndex()); QVERIFY(selection->hasSelection()); - QCOMPARE(spy.count(), 3); + QCOMPARE(spy.size(), 3); // test for inserting multiple (6) rows model->insertRows(0, 6, QModelIndex()); QVERIFY(selection->hasSelection()); - QCOMPARE(spy.count(), 4); + QCOMPARE(spy.size(), 4); +} + +static void (*oldMessageHandler)(QtMsgType, const QMessageLogContext&, const QString&); +static bool signalError = false; + +// detect disconnect warning: +// qt.core.qobject.connect: QObject::disconnect: No such signal +void tst_QItemSelectionModel::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + Q_ASSERT(oldMessageHandler); + + if (type == QtWarningMsg + && QString(context.category) == "qt.core.qobject.connect" + && msg.contains("No such")) { + signalError = true; + } + + return oldMessageHandler(type, context, msg); +} + +void tst_QItemSelectionModel::testSignalsDisconnection() +{ + oldMessageHandler = qInstallMessageHandler(messageHandler); + auto resetMessageHandler = qScopeGuard([] { qInstallMessageHandler(oldMessageHandler); }); + auto *newModel = new QStandardItemModel(model); + selection->setModel(newModel); + QSignalSpy spy(newModel, &QObject::destroyed); + delete newModel; + QTRY_COMPARE(spy.count(), 1); + qDebug() << spy; + selection->setModel(nullptr); + QVERIFY(!signalError); +} + +void tst_QItemSelectionModel::destroyModel() +{ + auto itemModel = std::make_unique<QStandardItemModel>(5, 5); + auto selectionModel = std::make_unique<QItemSelectionModel>(); + selectionModel->setModel(itemModel.get()); + selectionModel->select(itemModel->index(0, 0), QItemSelectionModel::Select); + QVERIFY(!selectionModel->selection().isEmpty()); + selectionModel->setCurrentIndex(itemModel->index(1, 0), QItemSelectionModel::Select); + QVERIFY(selectionModel->currentIndex().isValid()); + + QTest::failOnWarning(); + itemModel.reset(); + QVERIFY(!selectionModel->currentIndex().isValid()); + QVERIFY(selectionModel->selection().isEmpty()); } QTEST_MAIN(tst_QItemSelectionModel) |