File indexing completed on 2024-12-01 04:35:26

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "rocketchataccountmodeltest.h"
0008 #include "model/rocketchataccountmodel.h"
0009 #include "rocketchataccount.h"
0010 #include "test_model_helpers.h"
0011 #include <QSignalSpy>
0012 #include <QStandardPaths>
0013 #include <QTest>
0014 QTEST_GUILESS_MAIN(RocketChatAccountModelTest)
0015 
0016 RocketChatAccountModelTest::RocketChatAccountModelTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019     QStandardPaths::setTestModeEnabled(true);
0020 }
0021 
0022 void RocketChatAccountModelTest::shouldHaveDefaultValue()
0023 {
0024     RocketChatAccountModel w;
0025     // TODO
0026 }
0027 
0028 void RocketChatAccountModelTest::shouldAddAccountValue()
0029 {
0030     RocketChatAccountModel w;
0031     QVector<RocketChatAccount *> accounts;
0032     accounts.reserve(10);
0033     for (int i = 0; i < 10; ++i) {
0034         auto f = new RocketChatAccount();
0035         accounts.append(f);
0036     }
0037     QSignalSpy rowInsertedSpy(&w, &RocketChatAccountModel::rowsInserted);
0038     QSignalSpy rowABTInserted(&w, &RocketChatAccountModel::rowsAboutToBeInserted);
0039     QSignalSpy rowRemovedSpy(&w, &RocketChatAccountModel::rowsRemoved);
0040     QSignalSpy rowABTRemoved(&w, &RocketChatAccountModel::rowsAboutToBeRemoved);
0041     QSignalSpy modelResetSpy(&w, &RocketChatAccountModel::modelReset);
0042     QSignalSpy modelABTReset(&w, &RocketChatAccountModel::modelAboutToBeReset);
0043 
0044     w.setAccounts(accounts);
0045     QCOMPARE(w.rowCount(), 10);
0046     QCOMPARE(rowInsertedSpy.count(), 0);
0047     QCOMPARE(rowABTInserted.count(), 0);
0048     QCOMPARE(rowRemovedSpy.count(), 0);
0049     QCOMPARE(rowABTRemoved.count(), 0);
0050     QCOMPARE(modelResetSpy.count(), 1);
0051     QCOMPARE(modelABTReset.count(), 1);
0052 
0053     rowInsertedSpy.clear();
0054     rowABTInserted.clear();
0055     rowRemovedSpy.clear();
0056     rowABTRemoved.clear();
0057     modelResetSpy.clear();
0058     modelABTReset.clear();
0059 
0060     accounts.clear();
0061     accounts.reserve(3);
0062     for (int i = 0; i < 3; ++i) {
0063         auto f = new RocketChatAccount();
0064         accounts.append(f);
0065     }
0066 
0067     w.setAccounts(accounts);
0068 
0069     QCOMPARE(w.rowCount(), 3);
0070 
0071     QCOMPARE(rowInsertedSpy.count(), 0);
0072     QCOMPARE(rowABTInserted.count(), 0);
0073     QCOMPARE(rowRemovedSpy.count(), 0);
0074     QCOMPARE(rowABTRemoved.count(), 0);
0075     QCOMPARE(modelResetSpy.count(), 1);
0076     QCOMPARE(modelABTReset.count(), 1);
0077 }
0078 
0079 void RocketChatAccountModelTest::shouldClearAccounts()
0080 {
0081     RocketChatAccountModel w;
0082     QVector<RocketChatAccount *> accounts;
0083     accounts.reserve(10);
0084     for (int i = 0; i < 10; ++i) {
0085         auto f = new RocketChatAccount();
0086         accounts.append(f);
0087     }
0088     QSignalSpy rowInsertedSpy(&w, &RocketChatAccountModel::rowsInserted);
0089     QSignalSpy rowABTInserted(&w, &RocketChatAccountModel::rowsAboutToBeInserted);
0090     QSignalSpy rowRemovedSpy(&w, &RocketChatAccountModel::rowsRemoved);
0091     QSignalSpy rowABTRemoved(&w, &RocketChatAccountModel::rowsAboutToBeRemoved);
0092     QSignalSpy modelResetSpy(&w, &RocketChatAccountModel::modelReset);
0093     QSignalSpy modelABTReset(&w, &RocketChatAccountModel::modelAboutToBeReset);
0094 
0095     w.setAccounts(accounts);
0096     rowInsertedSpy.clear();
0097     rowABTInserted.clear();
0098     rowRemovedSpy.clear();
0099     rowABTRemoved.clear();
0100     modelResetSpy.clear();
0101     modelABTReset.clear();
0102 
0103     w.clear();
0104     QCOMPARE(w.rowCount(), 0);
0105     QCOMPARE(rowInsertedSpy.count(), 0);
0106     QCOMPARE(rowABTInserted.count(), 0);
0107     QCOMPARE(rowRemovedSpy.count(), 0);
0108     QCOMPARE(rowABTRemoved.count(), 0);
0109     QCOMPARE(modelResetSpy.count(), 1);
0110     QCOMPARE(modelABTReset.count(), 1);
0111 }
0112 
0113 void RocketChatAccountModelTest::shouldRemoveAccount()
0114 {
0115     RocketChatAccountModel w;
0116     QVector<RocketChatAccount *> accounts;
0117     for (int i = 0; i < 10; ++i) {
0118         auto f = new RocketChatAccount();
0119         f->setAccountName(QStringLiteral("foo%1").arg(i));
0120         accounts.append(f);
0121     }
0122     QSignalSpy rowInsertedSpy(&w, &RocketChatAccountModel::rowsInserted);
0123     QSignalSpy rowABTInserted(&w, &RocketChatAccountModel::rowsAboutToBeInserted);
0124     QSignalSpy rowRemovedSpy(&w, &RocketChatAccountModel::rowsRemoved);
0125     QSignalSpy rowABTRemoved(&w, &RocketChatAccountModel::rowsAboutToBeRemoved);
0126     QSignalSpy modelResetSpy(&w, &RocketChatAccountModel::modelReset);
0127     QSignalSpy modelABTReset(&w, &RocketChatAccountModel::modelAboutToBeReset);
0128 
0129     w.setAccounts(accounts);
0130     QCOMPARE(w.rowCount(), 10);
0131     QCOMPARE(rowInsertedSpy.count(), 0);
0132     QCOMPARE(rowABTInserted.count(), 0);
0133     QCOMPARE(rowRemovedSpy.count(), 0);
0134     QCOMPARE(rowABTRemoved.count(), 0);
0135     QCOMPARE(modelResetSpy.count(), 1);
0136     QCOMPARE(modelABTReset.count(), 1);
0137 
0138     rowInsertedSpy.clear();
0139     rowABTInserted.clear();
0140     rowRemovedSpy.clear();
0141     rowABTRemoved.clear();
0142     modelResetSpy.clear();
0143     modelABTReset.clear();
0144 
0145     w.removeAccount(QStringLiteral("foo2"));
0146 
0147     QCOMPARE(rowInsertedSpy.count(), 0);
0148     QCOMPARE(rowABTInserted.count(), 0);
0149     QCOMPARE(rowRemovedSpy.count(), 1);
0150     QCOMPARE(rowABTRemoved.count(), 1);
0151     QCOMPARE(TestModelHelpers::rowSpyToText(rowRemovedSpy), QStringLiteral("2,2"));
0152     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTRemoved), QStringLiteral("2,2"));
0153     QCOMPARE(modelResetSpy.count(), 0);
0154     QCOMPARE(modelABTReset.count(), 0);
0155 
0156     rowInsertedSpy.clear();
0157     rowABTInserted.clear();
0158     rowRemovedSpy.clear();
0159     rowABTRemoved.clear();
0160     modelResetSpy.clear();
0161     modelABTReset.clear();
0162 
0163     w.removeAccount(QStringLiteral("notexist"));
0164 
0165     QCOMPARE(rowInsertedSpy.count(), 0);
0166     QCOMPARE(rowABTInserted.count(), 0);
0167     QCOMPARE(rowRemovedSpy.count(), 0);
0168     QCOMPARE(rowABTRemoved.count(), 0);
0169     QCOMPARE(modelResetSpy.count(), 0);
0170     QCOMPARE(modelABTReset.count(), 0);
0171 }
0172 
0173 #include "moc_rocketchataccountmodeltest.cpp"