Warning, file /plasma/discover/discover/autotests/PaginateModelTest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "../PaginateModel.h" 0008 #include <QAbstractItemModelTester> 0009 #include <QStringListModel> 0010 #include <QtTest> 0011 0012 void insertRow(QStringListModel *model, int row, const QString &appendString) 0013 { 0014 model->insertRow(row); 0015 model->setData(model->index(row, 0), appendString); 0016 } 0017 void appendRow(QStringListModel *model, const QString &appendString) 0018 { 0019 int count = model->rowCount(); 0020 insertRow(model, count, appendString); 0021 } 0022 0023 class PaginateModelTest : public QObject 0024 { 0025 Q_OBJECT 0026 public: 0027 PaginateModelTest() 0028 : m_testModel(new QStringListModel) 0029 { 0030 for (int i = 0; i < 13; ++i) { 0031 appendRow(m_testModel, QStringLiteral("figui%1").arg(i)); 0032 } 0033 } 0034 0035 private Q_SLOTS: 0036 void testPages() 0037 { 0038 PaginateModel pm; 0039 new QAbstractItemModelTester(&pm, &pm); 0040 pm.setSourceModel(m_testModel); 0041 pm.setPageSize(5); 0042 QCOMPARE(pm.pageCount(), 3); 0043 QCOMPARE(pm.rowCount(), 5); 0044 QCOMPARE(pm.firstItem(), 0); 0045 QCOMPARE(pm.currentPage(), 0); 0046 pm.nextPage(); 0047 QCOMPARE(pm.rowCount(), 5); 0048 QCOMPARE(pm.currentPage(), 1); 0049 pm.nextPage(); 0050 QCOMPARE(pm.rowCount(), 3); 0051 QCOMPARE(pm.currentPage(), 2); 0052 0053 pm.firstPage(); 0054 QCOMPARE(pm.firstItem(), 0); 0055 pm.setFirstItem(0); 0056 QCOMPARE(pm.firstItem(), 0); 0057 QCOMPARE(pm.currentPage(), 0); 0058 pm.lastPage(); 0059 QCOMPARE(pm.firstItem(), 10); 0060 QCOMPARE(pm.currentPage(), 2); 0061 } 0062 0063 void testPageSize() 0064 { 0065 PaginateModel pm; 0066 new QAbstractItemModelTester(&pm, &pm); 0067 pm.setSourceModel(m_testModel); 0068 pm.setPageSize(5); 0069 QCOMPARE(pm.pageCount(), 3); 0070 pm.setPageSize(10); 0071 QCOMPARE(pm.pageCount(), 2); 0072 pm.setPageSize(5); 0073 QCOMPARE(pm.pageCount(), 3); 0074 } 0075 0076 void testItemAdded() 0077 { 0078 PaginateModel pm; 0079 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0080 new QAbstractItemModelTester(&pm, &pm); 0081 #endif 0082 pm.setSourceModel(m_testModel); 0083 pm.setPageSize(5); 0084 QCOMPARE(pm.pageCount(), 3); 0085 QSignalSpy spy(&pm, &QAbstractItemModel::rowsAboutToBeInserted); 0086 insertRow(m_testModel, 3, QStringLiteral("mwahahaha")); 0087 insertRow(m_testModel, 3, QStringLiteral("mwahahaha")); 0088 QCOMPARE(spy.count(), 0); 0089 appendRow(m_testModel, QStringLiteral("mwahahaha")); 0090 0091 pm.lastPage(); 0092 for (int i = 0; i < 7; ++i) 0093 appendRow(m_testModel, QStringLiteral("mwahahaha%1").arg(i)); 0094 QCOMPARE(spy.count(), 4); 0095 pm.firstPage(); 0096 0097 for (int i = 0; i < 7; ++i) 0098 appendRow(m_testModel, QStringLiteral("faraway%1").arg(i)); 0099 QCOMPARE(spy.count(), 4); 0100 } 0101 0102 void testItemAddBeginning() 0103 { 0104 QStringListModel smallerModel; 0105 0106 PaginateModel pm; 0107 new QAbstractItemModelTester(&pm, &pm); 0108 pm.setSourceModel(&smallerModel); 0109 pm.setPageSize(5); 0110 QCOMPARE(pm.pageCount(), 1); 0111 QCOMPARE(pm.rowCount(), 0); 0112 insertRow(&smallerModel, 0, QStringLiteral("just one")); 0113 QCOMPARE(pm.pageCount(), 1); 0114 QCOMPARE(pm.rowCount(), 1); 0115 smallerModel.removeRow(0); 0116 QCOMPARE(pm.pageCount(), 1); 0117 QCOMPARE(pm.rowCount(), 0); 0118 } 0119 0120 void testItemRemoved() 0121 { 0122 PaginateModel pm; 0123 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0124 new QAbstractItemModelTester(&pm, &pm); 0125 #endif 0126 pm.setSourceModel(m_testModel); 0127 pm.setPageSize(5); 0128 QCOMPARE(pm.pageCount(), 5); 0129 QSignalSpy spy(&pm, &QAbstractItemModel::rowsAboutToBeRemoved); 0130 m_testModel->removeRow(3); 0131 QCOMPARE(spy.count(), 0); 0132 spy.clear(); 0133 0134 pm.lastPage(); 0135 m_testModel->removeRow(m_testModel->rowCount() - 1); 0136 QCOMPARE(spy.count(), 1); 0137 } 0138 0139 void testMove() 0140 { 0141 PaginateModel pm; 0142 new QAbstractItemModelTester(&pm, &pm); 0143 pm.setSourceModel(m_testModel); 0144 pm.setPageSize(5); 0145 m_testModel->moveRow({}, 0, {}, 3); 0146 } 0147 0148 private: 0149 QStringListModel *const m_testModel; 0150 }; 0151 0152 QTEST_GUILESS_MAIN(PaginateModelTest) 0153 0154 #include "PaginateModelTest.moc"