File indexing completed on 2024-05-12 05:28:48

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 <QSignalSpy>
0010 #include <QStringListModel>
0011 #include <QTest>
0012 
0013 void insertRow(QStringListModel *model, int row, const QString &appendString)
0014 {
0015     model->insertRow(row);
0016     model->setData(model->index(row, 0), appendString);
0017 }
0018 void appendRow(QStringListModel *model, const QString &appendString)
0019 {
0020     int count = model->rowCount();
0021     insertRow(model, count, appendString);
0022 }
0023 
0024 class PaginateModelTest : public QObject
0025 {
0026     Q_OBJECT
0027 public:
0028     PaginateModelTest()
0029         : m_testModel(new QStringListModel)
0030     {
0031         for (int i = 0; i < 13; ++i) {
0032             appendRow(m_testModel, QStringLiteral("figui%1").arg(i));
0033         }
0034     }
0035 
0036 private Q_SLOTS:
0037     void testPages()
0038     {
0039         PaginateModel pm;
0040         new QAbstractItemModelTester(&pm, &pm);
0041         pm.setSourceModel(m_testModel);
0042         pm.setPageSize(5);
0043         QCOMPARE(pm.pageCount(), 3);
0044         QCOMPARE(pm.rowCount(), 5);
0045         QCOMPARE(pm.firstItem(), 0);
0046         QCOMPARE(pm.currentPage(), 0);
0047         pm.nextPage();
0048         QCOMPARE(pm.rowCount(), 5);
0049         QCOMPARE(pm.currentPage(), 1);
0050         pm.nextPage();
0051         QCOMPARE(pm.rowCount(), 3);
0052         QCOMPARE(pm.currentPage(), 2);
0053 
0054         pm.firstPage();
0055         QCOMPARE(pm.firstItem(), 0);
0056         pm.setFirstItem(0);
0057         QCOMPARE(pm.firstItem(), 0);
0058         QCOMPARE(pm.currentPage(), 0);
0059         pm.lastPage();
0060         QCOMPARE(pm.firstItem(), 10);
0061         QCOMPARE(pm.currentPage(), 2);
0062     }
0063 
0064     void testPageSize()
0065     {
0066         PaginateModel pm;
0067         new QAbstractItemModelTester(&pm, &pm);
0068         pm.setSourceModel(m_testModel);
0069         pm.setPageSize(5);
0070         QCOMPARE(pm.pageCount(), 3);
0071         pm.setPageSize(10);
0072         QCOMPARE(pm.pageCount(), 2);
0073         pm.setPageSize(5);
0074         QCOMPARE(pm.pageCount(), 3);
0075     }
0076 
0077     void testItemAdded()
0078     {
0079         PaginateModel pm;
0080         pm.setSourceModel(m_testModel);
0081         pm.setPageSize(5);
0082         QCOMPARE(pm.pageCount(), 3);
0083         QSignalSpy spy(&pm, &QAbstractItemModel::rowsAboutToBeInserted);
0084         insertRow(m_testModel, 3, QStringLiteral("mwahahaha"));
0085         insertRow(m_testModel, 3, QStringLiteral("mwahahaha"));
0086         QCOMPARE(spy.count(), 0);
0087         appendRow(m_testModel, QStringLiteral("mwahahaha"));
0088 
0089         pm.lastPage();
0090         for (int i = 0; i < 7; ++i)
0091             appendRow(m_testModel, QStringLiteral("mwahahaha%1").arg(i));
0092         QCOMPARE(spy.count(), 4);
0093         pm.firstPage();
0094 
0095         for (int i = 0; i < 7; ++i)
0096             appendRow(m_testModel, QStringLiteral("faraway%1").arg(i));
0097         QCOMPARE(spy.count(), 4);
0098     }
0099 
0100     void testItemAddBeginning()
0101     {
0102         QStringListModel smallerModel;
0103 
0104         PaginateModel pm;
0105         new QAbstractItemModelTester(&pm, &pm);
0106         pm.setSourceModel(&smallerModel);
0107         pm.setPageSize(5);
0108         QCOMPARE(pm.pageCount(), 1);
0109         QCOMPARE(pm.rowCount(), 0);
0110         insertRow(&smallerModel, 0, QStringLiteral("just one"));
0111         QCOMPARE(pm.pageCount(), 1);
0112         QCOMPARE(pm.rowCount(), 1);
0113         smallerModel.removeRow(0);
0114         QCOMPARE(pm.pageCount(), 1);
0115         QCOMPARE(pm.rowCount(), 0);
0116     }
0117 
0118     void testItemRemoved()
0119     {
0120         PaginateModel pm;
0121         pm.setSourceModel(m_testModel);
0122         pm.setPageSize(5);
0123         QCOMPARE(pm.pageCount(), 5);
0124         QSignalSpy spy(&pm, &QAbstractItemModel::rowsAboutToBeRemoved);
0125         m_testModel->removeRow(3);
0126         QCOMPARE(spy.count(), 0);
0127         spy.clear();
0128 
0129         pm.lastPage();
0130         m_testModel->removeRow(m_testModel->rowCount() - 1);
0131         QCOMPARE(spy.count(), 1);
0132     }
0133 
0134     void testMove()
0135     {
0136         PaginateModel pm;
0137         new QAbstractItemModelTester(&pm, &pm);
0138         pm.setSourceModel(m_testModel);
0139         pm.setPageSize(5);
0140         m_testModel->moveRow({}, 0, {}, 3);
0141     }
0142 
0143 private:
0144     QStringListModel *const m_testModel;
0145 };
0146 
0147 QTEST_GUILESS_MAIN(PaginateModelTest)
0148 
0149 #include "PaginateModelTest.moc"