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

0001 /*
0002    SPDX-FileCopyrightText: 2020 Olivier de Gaalon <olivier.jg@gmail.com>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "accountschannelsmodeltest.h"
0008 
0009 #include "accountmanager.h"
0010 #include "model/accountschannelsmodel.h"
0011 #include "rocketchataccount.h"
0012 #include "ruqola.h"
0013 
0014 #include <QAbstractItemModelTester>
0015 #include <QStandardPaths>
0016 #include <QTest>
0017 
0018 QTEST_GUILESS_MAIN(AccountsChannelsModelTest)
0019 
0020 AccountsChannelsModelTest::AccountsChannelsModelTest(QObject *parent)
0021     : QObject(parent)
0022 {
0023 }
0024 
0025 void AccountsChannelsModelTest::initTestCase()
0026 {
0027     QStandardPaths::setTestModeEnabled(true);
0028 }
0029 
0030 void AccountsChannelsModelTest::accountsAndChannels()
0031 {
0032     AccountsChannelsModel model;
0033     QAbstractItemModelTester tester(&model, QAbstractItemModelTester::FailureReportingMode::QtTest);
0034 
0035     QCOMPARE(model.rowCount(), 1); // Ruqola creates one account by default
0036     QCOMPARE(model.data(model.index(1, 0)).toString(), QString());
0037     QCOMPARE(model.rowCount(model.index(1, 0)), 0);
0038 
0039     const auto newAcctName = QStringLiteral("Test Account");
0040     const auto acct = new RocketChatAccount;
0041     Ruqola::self()->accountManager()->addAccount(acct);
0042     const auto newAcctIndex = model.index(1, 0);
0043     QCOMPARE(model.rowCount(), 2);
0044     QVERIFY(!model.hasChildren(newAcctIndex));
0045     QCOMPARE(model.data(newAcctIndex).toString(), QString());
0046     acct->setAccountName(newAcctName);
0047     QCOMPARE(model.data(newAcctIndex).toString(), newAcctName);
0048 
0049     Ruqola::self()->setCurrentAccount(newAcctName);
0050 
0051     const auto newRoomId = QStringLiteral("RoomId");
0052     const auto newRoomName = QStringLiteral("Room Name");
0053     acct->roomModel()->addRoom(newRoomId, newRoomName);
0054     QCOMPARE(model.rowCount(newAcctIndex), 1);
0055 
0056     const auto newRoomIndex = model.index(0, 0, newAcctIndex);
0057     QVERIFY(!model.hasChildren(newRoomIndex));
0058     QCOMPARE(model.data(newRoomIndex).toString(), newRoomName);
0059 
0060     // TODO: RoomsModel currently has no API for removing rooms
0061 
0062     Ruqola::self()->accountManager()->removeAccount(newAcctName);
0063     QCOMPARE(model.rowCount(), 1); // Only the default account remains
0064 }
0065 
0066 #include "moc_accountschannelsmodeltest.cpp"