File indexing completed on 2024-05-12 16:25:31

0001 /*
0002    SPDX-FileCopyrightText: 2021 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "roomlistheadingsproxymodeltest.h"
0007 #include "model/roomfilterproxymodel.h"
0008 #include "model/roomlistheadingsproxymodel.h"
0009 
0010 #include "rocketchataccount.h"
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 QTEST_GUILESS_MAIN(RoomListHeadingsProxyModelTest)
0015 
0016 RoomListHeadingsProxyModelTest::RoomListHeadingsProxyModelTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019     QStandardPaths::setTestModeEnabled(true);
0020 }
0021 
0022 static QStandardItem *newItem(const char *text, RoomModel::Section section)
0023 {
0024     auto item = new QStandardItem(QString::fromUtf8(text));
0025     item->setData(QVariant::fromValue(section), RoomModel::RoomSection);
0026     return item;
0027 }
0028 
0029 void RoomListHeadingsProxyModelTest::initTestCase()
0030 {
0031     // 0 favorites, 2 teams, 1 discussion, 2 private messages
0032     int row = 0;
0033     mSourceModel.setItem(row++, 0, newItem("Team 1", RoomModel::Section::Teams));
0034     mSourceModel.setItem(row++, 0, newItem("Team 2", RoomModel::Section::Teams));
0035     mSourceModel.setItem(row++, 0, newItem("PM 1", RoomModel::Section::PrivateMessages));
0036     mSourceModel.setItem(row++, 0, newItem("Discuss 1", RoomModel::Section::Discussions));
0037     mSourceModel.setItem(row++, 0, newItem("Discuss 2", RoomModel::Section::Discussions));
0038     QCOMPARE(mSourceModel.rowCount(), row);
0039 }
0040 
0041 void RoomListHeadingsProxyModelTest::shouldBeEmptyByDefault()
0042 {
0043     // GIVEN
0044     RoomListHeadingsProxyModel proxy;
0045     QStandardItemModel sourceModel;
0046     // WHEN
0047     proxy.setSourceModel(&sourceModel);
0048     // THEN
0049     QCOMPARE(proxy.rowCount({}), proxy.sectionCount);
0050     for (uint section = 0; section < proxy.sectionCount; ++section) {
0051         QCOMPARE(proxy.rowCount(proxy.index(section, 0, {})), 0);
0052     }
0053     QCOMPARE(proxy.sourceModel(), &sourceModel);
0054 }
0055 
0056 void RoomListHeadingsProxyModelTest::shouldReturnRowCount()
0057 {
0058     // GIVEN
0059     RoomListHeadingsProxyModel proxy;
0060     // WHEN
0061     proxy.setSourceModel(&mSourceModel);
0062     // THEN
0063     const auto expectedCount = [](RoomModel::Section section) {
0064         switch (section) {
0065         case RoomModel::Section::Unread:
0066         case RoomModel::Section::Favorites:
0067         case RoomModel::Section::Rooms:
0068         case RoomModel::Section::Unknown:
0069             return 0;
0070         case RoomModel::Section::Teams:
0071         case RoomModel::Section::Discussions:
0072             return 2;
0073         case RoomModel::Section::PrivateMessages:
0074             return 1;
0075         case RoomModel::Section::NSections:
0076             Q_UNREACHABLE();
0077         }
0078         Q_UNREACHABLE();
0079     };
0080 
0081     for (uint section = 0; section < proxy.sectionCount; ++section) {
0082         QCOMPARE(proxy.rowCount(proxy.index(section, 0, {})), expectedCount(RoomModel::Section(section)));
0083     }
0084 }
0085 
0086 void RoomListHeadingsProxyModelTest::shouldMapProxyRows_data()
0087 {
0088     QTest::addColumn<RoomModel::Section>("section");
0089     QTest::addColumn<int>("proxyRow");
0090     QTest::addColumn<int>("expectedSourceRow");
0091 
0092     QTest::newRow("0") << RoomModel::Section::Teams << 0 << 0;
0093     QTest::newRow("1") << RoomModel::Section::Teams << 1 << 1;
0094     QTest::newRow("2") << RoomModel::Section::PrivateMessages << 0 << 2;
0095     QTest::newRow("3") << RoomModel::Section::Discussions << 0 << 3;
0096     QTest::newRow("4") << RoomModel::Section::Discussions << 1 << 4;
0097 }
0098 
0099 void RoomListHeadingsProxyModelTest::shouldMapProxyRows()
0100 {
0101     // GIVEN
0102     QFETCH(RoomModel::Section, section);
0103     QFETCH(int, proxyRow);
0104     QFETCH(int, expectedSourceRow);
0105     RoomListHeadingsProxyModel proxy;
0106     proxy.setSourceModel(&mSourceModel);
0107     // WHEN
0108     const QModelIndex proxyIndex = proxy.index(proxyRow, 0, proxy.index(int(section), 0, {}));
0109     // THEN
0110     QCOMPARE(proxy.mapToSource(proxyIndex).row(), expectedSourceRow);
0111 }
0112 
0113 void RoomListHeadingsProxyModelTest::shouldMapSourceRows_data()
0114 {
0115     QTest::addColumn<int>("sourceRow");
0116     QTest::addColumn<RoomModel::Section>("expectedSection");
0117     QTest::addColumn<int>("expectedProxyRow");
0118 
0119     QTest::newRow("0") << 0 << RoomModel::Section::Teams << 0;
0120     QTest::newRow("1") << 1 << RoomModel::Section::Teams << 1;
0121     QTest::newRow("2") << 2 << RoomModel::Section::PrivateMessages << 0;
0122     QTest::newRow("3") << 3 << RoomModel::Section::Discussions << 0;
0123     QTest::newRow("4") << 4 << RoomModel::Section::Discussions << 1;
0124 }
0125 
0126 void RoomListHeadingsProxyModelTest::shouldMapSourceRows()
0127 {
0128     // GIVEN
0129     QFETCH(int, sourceRow);
0130     QFETCH(RoomModel::Section, expectedSection);
0131     QFETCH(int, expectedProxyRow);
0132     RoomListHeadingsProxyModel proxy;
0133     proxy.setSourceModel(&mSourceModel);
0134     // WHEN
0135     const QModelIndex sourceIndex = mSourceModel.index(sourceRow, 0);
0136     // THEN
0137     const QModelIndex proxyIndex = proxy.mapFromSource(sourceIndex);
0138     QCOMPARE(proxyIndex.row(), expectedProxyRow);
0139     QCOMPARE(proxyIndex.parent().row(), int(expectedSection));
0140 }
0141 
0142 static QStringList initialExpectedList()
0143 {
0144     return QStringList{QStringLiteral("Teams"),
0145                        QStringLiteral("Team 1"),
0146                        QStringLiteral("Team 2"),
0147                        QStringLiteral("Private Messages"),
0148                        QStringLiteral("PM 1"),
0149                        QStringLiteral("Discussions"),
0150                        QStringLiteral("Discuss 1"),
0151                        QStringLiteral("Discuss 2")};
0152 }
0153 
0154 static QStringList extractTexts(QAbstractItemModel *model)
0155 {
0156     const int count = model->rowCount();
0157     QStringList texts;
0158     texts.reserve(count);
0159     for (int sectionId = 0; sectionId < count; ++sectionId) {
0160         const auto section = model->index(sectionId, 0, {});
0161         const auto sectionSize = model->rowCount(section);
0162 
0163         if (sectionSize == 0)
0164             continue;
0165 
0166         texts.append(section.data().toString());
0167 
0168         for (int row = 0; row < sectionSize; ++row) {
0169             texts.append(model->index(row, 0, section).data().toString());
0170         }
0171     }
0172     return texts;
0173 }
0174 
0175 static bool compareWithExpected(const QStringList &texts, const QStringList &expected)
0176 {
0177     if (expected.count() != texts.count()) {
0178         qWarning() << "FAIL: expected" << expected.count() << "got" << texts.count() << '\n' << texts;
0179         return false;
0180     }
0181     for (int row = 0; row < expected.count(); ++row) {
0182         if (texts[row] != expected[row]) {
0183             qWarning() << "FAIL:" << row << texts[row] << expected[row];
0184             return false;
0185         }
0186     }
0187     return true;
0188 }
0189 
0190 void RoomListHeadingsProxyModelTest::shouldReturnData()
0191 {
0192     // GIVEN
0193     RoomListHeadingsProxyModel proxy;
0194     proxy.setSourceModel(&mSourceModel);
0195     // WHEN/THEN
0196     QVERIFY(compareWithExpected(extractTexts(&proxy), initialExpectedList()));
0197 }
0198 
0199 void RoomListHeadingsProxyModelTest::shouldUpdateOnSectionUpdates()
0200 {
0201     // GIVEN
0202     RoomListHeadingsProxyModel proxy;
0203     proxy.setSourceModel(&mSourceModel);
0204 
0205     // WHEN
0206     mSourceModel.item(0)->setData(QVariant::fromValue(RoomModel::Section::Favorites), RoomModel::RoomSection);
0207 
0208     // THEN
0209     const QStringList newExpected{QStringLiteral("Favorites"),
0210                                   QStringLiteral("Team 1"),
0211                                   QStringLiteral("Teams"),
0212                                   QStringLiteral("Team 2"),
0213                                   QStringLiteral("Private Messages"),
0214                                   QStringLiteral("PM 1"),
0215                                   QStringLiteral("Discussions"),
0216                                   QStringLiteral("Discuss 1"),
0217                                   QStringLiteral("Discuss 2")};
0218     QVERIFY(compareWithExpected(extractTexts(&proxy), newExpected));
0219 }
0220 
0221 #include "moc_roomlistheadingsproxymodeltest.cpp"