File indexing completed on 2024-12-22 04:46:03

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "roomwidgettest.h"
0008 #include "model/roommodel.h"
0009 #include "offlinewidget/offlinewidget.h"
0010 #include "otr/otrwidget.h"
0011 #include "rocketchataccount.h"
0012 #include "room/messagelinewidget.h"
0013 #include "room/messagelistview.h"
0014 #include "room/plugintextmessagewidget.h"
0015 #include "room/readonlylineeditwidget.h"
0016 #include "room/reconnectinfowidget.h"
0017 #include "room/roomcounterinfowidget.h"
0018 #include "room/roomheaderwidget.h"
0019 #include "room/roomquotemessagewidget.h"
0020 #include "room/roomreplythreadwidget.h"
0021 #include "room/roomwidget.h"
0022 #include "room/uploadfileprogressstatuslistwidget.h"
0023 #include "room/usersinroomflowwidget.h"
0024 
0025 #include "ruqola.h"
0026 
0027 #include <QStackedWidget>
0028 #include <QStandardPaths>
0029 #include <QTest>
0030 #include <QVBoxLayout>
0031 
0032 QTEST_MAIN(RoomWidgetTest)
0033 RoomWidgetTest::RoomWidgetTest(QObject *parent)
0034     : QObject(parent)
0035 {
0036     QStandardPaths::setTestModeEnabled(true);
0037 }
0038 
0039 void RoomWidgetTest::shouldHaveDefaultValues()
0040 {
0041     RoomWidget w;
0042     auto mainLayout = w.findChild<QVBoxLayout *>(QStringLiteral("mainLayout"));
0043     QVERIFY(mainLayout);
0044     QCOMPARE(mainLayout->contentsMargins(), QMargins{});
0045 
0046     auto mRoomHeaderWidget = w.findChild<RoomHeaderWidget *>(QStringLiteral("mRoomHeaderWidget"));
0047     QVERIFY(mRoomHeaderWidget);
0048 
0049     auto mUsersInRoomFlowWidget = w.findChild<UsersInRoomFlowWidget *>(QStringLiteral("mUsersInRoomFlowWidget"));
0050     QVERIFY(mUsersInRoomFlowWidget);
0051 
0052     auto mMessageListView = w.findChild<MessageListView *>(QStringLiteral("mMessageListView"));
0053     QVERIFY(mMessageListView);
0054 
0055     auto mStackedWidget = w.findChild<QStackedWidget *>(QStringLiteral("mStackedWidget"));
0056     QVERIFY(mStackedWidget);
0057     QCOMPARE(mStackedWidget->count(), 2);
0058 
0059     auto mMessageLineWidget = w.findChild<MessageLineWidget *>(QStringLiteral("mMessageLineWidget"));
0060     QVERIFY(mMessageLineWidget);
0061 
0062     auto mReadOnlyLineEditWidget = w.findChild<ReadOnlyLineEditWidget *>(QStringLiteral("mReadOnlyLineEditWidget"));
0063     QVERIFY(mReadOnlyLineEditWidget);
0064     QCOMPARE(mStackedWidget->currentWidget(), mMessageLineWidget);
0065 
0066     QVERIFY(w.roomId().isEmpty());
0067 
0068     auto mRoomCounterInfoWidget = w.findChild<RoomCounterInfoWidget *>(QStringLiteral("mRoomCounterInfoWidget"));
0069     QVERIFY(mRoomCounterInfoWidget);
0070 
0071 #if 0 // Load on demand
0072     auto mRoomReconnectInfoWidget = w.findChild<ReconnectInfoWidget *>(QStringLiteral("mRoomReconnectInfoWidget"));
0073     QVERIFY(mRoomReconnectInfoWidget);
0074 #endif
0075     auto mRoomReplyThreadWidget = w.findChild<RoomReplyThreadWidget *>(QStringLiteral("mRoomReplyThreadWidget"));
0076     QVERIFY(mRoomReplyThreadWidget);
0077     QVERIFY(!mRoomReplyThreadWidget->isVisible());
0078 
0079     auto mRoomQuoteMessageWidget = w.findChild<RoomQuoteMessageWidget *>(QStringLiteral("mRoomQuoteMessageWidget"));
0080     QVERIFY(mRoomQuoteMessageWidget);
0081     QVERIFY(!mRoomQuoteMessageWidget->isVisible());
0082 
0083     auto mUploadFileProgressStatusListWidget = w.findChild<UploadFileProgressStatusListWidget *>(QStringLiteral("mUploadFileProgressStatusListWidget"));
0084     QVERIFY(mUploadFileProgressStatusListWidget);
0085     QVERIFY(!mUploadFileProgressStatusListWidget->isVisible());
0086 
0087 #if 0 // Load on demand
0088     auto mOtrWidget = w.findChild<OtrWidget *>(QStringLiteral("mOtrWidget"));
0089     QVERIFY(mOtrWidget);
0090 
0091     auto mOffLineWidget = w.findChild<OffLineWidget *>(QStringLiteral("mOffLineWidget"));
0092     QVERIFY(mOffLineWidget);
0093 
0094     auto mPluginTextMessageWidget = w.findChild<PluginTextMessageWidget *>(QStringLiteral("mPluginTextMessageWidget"));
0095     QVERIFY(mPluginTextMessageWidget);
0096 #endif
0097 }
0098 
0099 static Room *createRoom(const QString &roomId, const QString &roomName)
0100 {
0101     RocketChatAccount *rcAccount = Ruqola::self()->rocketChatAccount();
0102     Room *r = new Room(rcAccount);
0103     r->setRoomId(roomId);
0104     r->setName(roomName);
0105     const Room::RoomType roomType = Room::RoomType::Channel; // should be an enum...
0106     r->setChannelType(roomType);
0107     r->setArchived(true); // workaround to skip RestApiConnection::membersInRoom
0108     return r;
0109 }
0110 
0111 void RoomWidgetTest::shouldStorePendingTextPerRoom()
0112 {
0113     // GIVEN two rooms
0114     RoomWidget w;
0115     RocketChatAccount *rcAccount = Ruqola::self()->rocketChatAccount();
0116     w.setCurrentRocketChatAccount(rcAccount);
0117     const QString roomId1 = QStringLiteral("roomId1");
0118     Room *room1 = createRoom(roomId1, QStringLiteral("roomName1"));
0119     QVERIFY(rcAccount->roomModel()->addRoom(room1));
0120     const QString roomId2 = QStringLiteral("roomId2");
0121     Room *room2 = createRoom(roomId2, QStringLiteral("roomName2"));
0122     QVERIFY(rcAccount->roomModel()->addRoom(room2));
0123 
0124     // Ensure switching between rooms works
0125     w.setChannelSelected(room1->roomId(), room1->channelType());
0126     QCOMPARE(w.roomId(), roomId1);
0127     w.setChannelSelected(room2->roomId(), room2->channelType());
0128     QCOMPARE(w.roomId(), roomId2);
0129 
0130     // WHEN typing text and switching rooms
0131     auto mMessageLineWidget = w.findChild<MessageLineWidget *>(QStringLiteral("mMessageLineWidget"));
0132     QVERIFY(mMessageLineWidget);
0133     mMessageLineWidget->setText(QStringLiteral("Text for room 2"));
0134 
0135     w.setChannelSelected(room1->roomId(), room1->channelType());
0136     // THEN the text should be empty
0137     QCOMPARE(mMessageLineWidget->text(), QString());
0138 
0139     // WHEN typing some text and switching back
0140     mMessageLineWidget->setText(QStringLiteral("Text for room 1"));
0141 
0142     // THEN the orig text should appear again
0143     w.setChannelSelected(room2->roomId(), room2->channelType());
0144     QCOMPARE(mMessageLineWidget->text(), QStringLiteral("Text for room 2"));
0145     mMessageLineWidget->setText(QString());
0146 
0147     // WHEN switching again
0148     mMessageLineWidget->setText(QStringLiteral("Text for room 1"));
0149 
0150     // THEN the other text should appear again
0151     w.setChannelSelected(room2->roomId(), room2->channelType());
0152     QCOMPARE(mMessageLineWidget->text(), QStringLiteral("Text for room 1"));
0153 }
0154 
0155 void RoomWidgetTest::shouldShowNoticeWhenReplyingToThread()
0156 {
0157     QEventLoop loop;
0158     RoomWidget w;
0159 
0160     auto mRoomReplyThreadWidget = w.findChild<RoomReplyThreadWidget *>(QStringLiteral("mRoomReplyThreadWidget"));
0161     QVERIFY(!mRoomReplyThreadWidget->isVisible());
0162 
0163     auto mMessageLineWidget = w.findChild<MessageLineWidget *>(QStringLiteral("mMessageLineWidget"));
0164     QVERIFY(mMessageLineWidget);
0165 
0166     mMessageLineWidget->setThreadMessageId(QStringLiteral("placeholder"));
0167     loop.processEvents();
0168     QVERIFY(!mRoomReplyThreadWidget->isHidden());
0169 
0170     mMessageLineWidget->setThreadMessageId({});
0171     loop.processEvents();
0172     QVERIFY(mRoomReplyThreadWidget->isHidden());
0173 }
0174 
0175 #include "moc_roomwidgettest.cpp"