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

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "discussionsmodeltest.h"
0008 #include "discussions/discussions.h"
0009 #include "model/discussionsmodel.h"
0010 #include "test_model_helpers.h"
0011 #include <QTest>
0012 QTEST_GUILESS_MAIN(DiscussionsModelTest)
0013 DiscussionsModelTest::DiscussionsModelTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 void DiscussionsModelTest::shouldHaveDefaultValues()
0019 {
0020     DiscussionsModel model;
0021     QVERIFY(!model.loadMoreDiscussionsInProgress());
0022 }
0023 
0024 void DiscussionsModelTest::shouldAssignValues()
0025 {
0026     DiscussionsModel w;
0027     QSignalSpy rowInsertedSpy(&w, &DiscussionsModel::rowsInserted);
0028     QSignalSpy rowABTInserted(&w, &DiscussionsModel::rowsAboutToBeInserted);
0029     QSignalSpy modelAboutToBeResetSpy(&w, &DiscussionsModel::modelAboutToBeReset);
0030 
0031     Discussions discussionList;
0032     for (int i = 0; i < 10; ++i) {
0033         Discussion c;
0034         c.setDescription(QStringLiteral("roomid%1").arg(i));
0035         c.setNumberMessages(i);
0036         c.setParentRoomId(QStringLiteral("online"));
0037         discussionList.append(std::move(c));
0038     }
0039     w.setDiscussions(discussionList);
0040     QCOMPARE(w.rowCount(), 10);
0041     QCOMPARE(rowInsertedSpy.count(), 1);
0042     QCOMPARE(rowABTInserted.count(), 1);
0043     QCOMPARE(modelAboutToBeResetSpy.count(), 0);
0044     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,9"));
0045     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,9"));
0046 
0047     // add Empty list
0048     discussionList.clear();
0049     rowInsertedSpy.clear();
0050     rowABTInserted.clear();
0051     modelAboutToBeResetSpy.clear();
0052 
0053     w.setDiscussions(discussionList);
0054 
0055     QCOMPARE(w.rowCount(), 0);
0056     QCOMPARE(rowInsertedSpy.count(), 0);
0057     QCOMPARE(rowABTInserted.count(), 0);
0058     QCOMPARE(modelAboutToBeResetSpy.count(), 1);
0059 
0060     // Add same element
0061     rowInsertedSpy.clear();
0062     rowABTInserted.clear();
0063     modelAboutToBeResetSpy.clear();
0064 
0065     w.setDiscussions(discussionList);
0066 
0067     QCOMPARE(w.rowCount(), 0);
0068     QCOMPARE(rowInsertedSpy.count(), 0);
0069     QCOMPARE(rowABTInserted.count(), 0);
0070     QCOMPARE(modelAboutToBeResetSpy.count(), 0);
0071     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QString());
0072     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QString());
0073 
0074     // Test add same number of element.
0075     discussionList.clear();
0076     rowInsertedSpy.clear();
0077     rowABTInserted.clear();
0078     modelAboutToBeResetSpy.clear();
0079 
0080     for (int i = 0; i < 5; ++i) {
0081         Discussion c;
0082         c.setDescription(QStringLiteral("roomid%1").arg(i));
0083         c.setNumberMessages(i);
0084         c.setParentRoomId(QStringLiteral("online"));
0085         discussionList.append(std::move(c));
0086     }
0087     w.setDiscussions(discussionList);
0088     QCOMPARE(w.rowCount(), 5);
0089     QCOMPARE(rowInsertedSpy.count(), 1);
0090     QCOMPARE(rowABTInserted.count(), 1);
0091     QCOMPARE(modelAboutToBeResetSpy.count(), 0);
0092     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,4"));
0093     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,4"));
0094 
0095     rowInsertedSpy.clear();
0096     rowABTInserted.clear();
0097     modelAboutToBeResetSpy.clear();
0098     w.setDiscussions(discussionList);
0099 
0100     QCOMPARE(rowInsertedSpy.count(), 1);
0101     QCOMPARE(rowABTInserted.count(), 1);
0102     QCOMPARE(modelAboutToBeResetSpy.count(), 1);
0103     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,4"));
0104     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,4"));
0105 }
0106 
0107 #include "moc_discussionsmodeltest.cpp"