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

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 "usersforroommodeltest.h"
0008 #include "model/usersforroommodel.h"
0009 #include "test_model_helpers.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QSignalSpy>
0013 #include <QTest>
0014 QTEST_GUILESS_MAIN(UsersForRoomModelTest)
0015 
0016 UsersForRoomModelTest::UsersForRoomModelTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 void UsersForRoomModelTest::shouldHaveDefaultValue()
0022 {
0023     UsersForRoomModel w;
0024     QSignalSpy rowInsertedSpy(&w, &UsersForRoomModel::rowsInserted);
0025     QSignalSpy rowABTInserted(&w, &UsersForRoomModel::rowsAboutToBeInserted);
0026     QCOMPARE(w.rowCount(), 0);
0027     QCOMPARE(rowInsertedSpy.count(), 0);
0028     QCOMPARE(rowABTInserted.count(), 0);
0029 }
0030 
0031 void UsersForRoomModelTest::shouldAddValues()
0032 {
0033     UsersForRoomModel w;
0034     QVector<User> users;
0035     QSignalSpy rowInsertedSpy(&w, &UsersForRoomModel::rowsInserted);
0036     QSignalSpy rowABTInserted(&w, &UsersForRoomModel::rowsAboutToBeInserted);
0037     for (int i = 0; i < 10; i++) {
0038         User user;
0039         user.setName(QStringLiteral("name%1").arg(i));
0040         user.setStatus(User::PresenceStatus::PresenceAway);
0041         user.setUserId(QStringLiteral("userId%1").arg(i));
0042         user.setUserName(QStringLiteral("username%1").arg(i));
0043         users.append(std::move(user));
0044     }
0045     w.setUsers(users);
0046     QCOMPARE(w.rowCount(), 10);
0047     QCOMPARE(rowInsertedSpy.count(), 1);
0048     QCOMPARE(rowABTInserted.count(), 1);
0049     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,9"));
0050     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,9"));
0051     rowABTInserted.clear();
0052     rowInsertedSpy.clear();
0053 
0054     users.clear();
0055 
0056     for (int i = 0; i < 3; ++i) {
0057         User user;
0058         user.setName(QStringLiteral("name%1").arg(i));
0059         user.setStatus(User::PresenceStatus::PresenceOffline);
0060         user.setUserId(QStringLiteral("userId%1").arg(i));
0061         user.setUserName(QStringLiteral("username%1").arg(i));
0062         users.append(std::move(user));
0063     }
0064     w.clear();
0065     w.setUsers(users);
0066     QCOMPARE(w.rowCount(), 3);
0067     QCOMPARE(rowInsertedSpy.count(), 1);
0068     QCOMPARE(rowABTInserted.count(), 1);
0069     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,2"));
0070     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,2"));
0071 
0072     rowABTInserted.clear();
0073     rowInsertedSpy.clear();
0074 
0075     users.clear();
0076     w.clear();
0077     w.setUsers(users);
0078     QCOMPARE(w.rowCount(), 0);
0079     QCOMPARE(rowInsertedSpy.count(), 0);
0080     QCOMPARE(rowABTInserted.count(), 0);
0081     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QString());
0082     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QString());
0083 }
0084 
0085 void UsersForRoomModelTest::shouldVerifyData()
0086 {
0087     UsersForRoomModel w;
0088     QVector<User> users;
0089     for (int i = 0; i < 10; i++) {
0090         User user;
0091         user.setName(QStringLiteral("name%1").arg(i));
0092         user.setStatus(User::PresenceStatus::PresenceOnline);
0093         user.setUserId(QStringLiteral("userId%1").arg(i));
0094         user.setUserName(QStringLiteral("username%1").arg(i));
0095         users.append(std::move(user));
0096     }
0097     w.setUsers(users);
0098     QCOMPARE(w.rowCount(), 10);
0099 
0100     for (int i = 0; i < 10; ++i) {
0101         QCOMPARE(w.data(w.index(i), UsersForRoomModel::Name).toString(), QStringLiteral("name%1").arg(i));
0102         QCOMPARE(w.data(w.index(i), UsersForRoomModel::IconStatus).toString(), QStringLiteral("user-online"));
0103         QCOMPARE(w.data(w.index(i), UsersForRoomModel::UserId).toString(), QStringLiteral("userId%1").arg(i));
0104         QCOMPARE(w.data(w.index(i), UsersForRoomModel::UserName).toString(), QStringLiteral("username%1").arg(i));
0105     }
0106 }
0107 
0108 void UsersForRoomModelTest::shouldParseUsers_data()
0109 {
0110     QTest::addColumn<QString>("filename");
0111     QTest::addColumn<int>("numberOfUsers");
0112     QTest::addColumn<int>("numberOfSignal");
0113     QTest::addColumn<bool>("restApi");
0114     QTest::newRow("oneuser") << QStringLiteral("oneuser.json") << 1 << 1 << false;
0115     QTest::newRow("severalusers") << QStringLiteral("severalusers.json") << 14 << 1 << false;
0116     QTest::newRow("severalusers1-restapi") << QStringLiteral("severalusers1-restapi.json") << 2 << 1 << true;
0117 }
0118 
0119 QJsonObject loadFile(const QString &file)
0120 {
0121     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/usersforroom/") + file;
0122     QFile f(originalJsonFile);
0123     if (!f.open(QIODevice::ReadOnly)) {
0124         qWarning() << " Unable to load file " << file;
0125         return {};
0126     }
0127     const QByteArray content = f.readAll();
0128     f.close();
0129     const QJsonDocument doc = QJsonDocument::fromJson(content);
0130     const QJsonObject root = doc.object();
0131     return root;
0132 }
0133 
0134 void UsersForRoomModelTest::shouldParseUsers()
0135 {
0136     QFETCH(QString, filename);
0137     QFETCH(int, numberOfUsers);
0138     QFETCH(int, numberOfSignal);
0139     QFETCH(bool, restApi);
0140     UsersForRoomModel w;
0141     QSignalSpy rowInsertedSpy(&w, &UsersForRoomModel::rowsInserted);
0142     QSignalSpy rowABTInserted(&w, &UsersForRoomModel::rowsAboutToBeInserted);
0143     w.parseUsersForRooms(loadFile(filename), nullptr, restApi); // We don't have userstatus model here
0144     QCOMPARE(rowInsertedSpy.count(), numberOfSignal);
0145     QCOMPARE(rowABTInserted.count(), numberOfSignal);
0146     QCOMPARE(w.rowCount(), numberOfUsers);
0147 }
0148 
0149 #include "moc_usersforroommodeltest.cpp"