File indexing completed on 2024-05-12 05:40:51
0001 /*************************************************************************** 0002 * Copyright (C) 2023 by Renaud Guezennec * 0003 * renaud@rolisteam.org * 0004 * * 0005 * Rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include <QDebug> 0022 #include <QObject> 0023 #include <QQmlEngine> 0024 #include <QQuickStyle> 0025 #include <QSignalSpy> 0026 #include <QTest> 0027 #include <memory> 0028 0029 #include "controller/instantmessagingcontroller.h" 0030 // #include "rwidgets/mediacontainers/instantmessagingview.h" 0031 #include "data/chatroom.h" 0032 #include "data/player.h" 0033 #include "diceparser_qobject/diceroller.h" 0034 #include "helper.h" 0035 #include "model/chatroomsplittermodel.h" 0036 #include "model/messagemodel.h" 0037 #include "model/playermodel.h" 0038 #include "test_root_path.h" 0039 #include <common_qml/theme.h> 0040 0041 void registerTypeTest() 0042 { 0043 static bool registered= false; 0044 0045 if(registered) 0046 return; 0047 0048 Q_INIT_RESOURCE(viewsqml); 0049 Q_INIT_RESOURCE(textedit); 0050 Q_INIT_RESOURCE(rolisteam); 0051 Q_INIT_RESOURCE(resources); 0052 0053 customization::Theme::setPath(QString("%1/resources/stylesheet/qml/theme.ini").arg(tests::root_path)); 0054 qRegisterMetaType<PlayerModel*>("PlayerModel*"); 0055 qRegisterMetaType<customization::Theme*>("customization::Theme*"); 0056 qRegisterMetaType<customization::StyleSheet*>("customization::StyleSheet*"); 0057 0058 qmlRegisterAnonymousType<PlayerModel>("PlayerModel", 1); 0059 0060 qmlRegisterSingletonType<customization::Theme>("Customization", 1, 0, "Theme", 0061 [](QQmlEngine* engine, QJSEngine*) -> QObject* 0062 { 0063 auto instead= customization::Theme::instance(); 0064 engine->setObjectOwnership(instead, QQmlEngine::CppOwnership); 0065 return instead; 0066 }); 0067 0068 QQuickStyle::setStyle("rolistyle"); 0069 QQuickStyle::setFallbackStyle("Fusion"); 0070 0071 registered= true; 0072 } 0073 0074 class InstantMessagingTest : public QObject 0075 { 0076 Q_OBJECT 0077 private slots: 0078 void initTestCase(); 0079 void init(); 0080 void cleanup(); 0081 0082 void sendMessage(); 0083 void sendMessage_data(); 0084 0085 void splitScreen(); 0086 void detachScreen(); 0087 0088 private: 0089 std::unique_ptr<PlayerModel> m_model; 0090 std::unique_ptr<InstantMessagingController> m_ctrl; 0091 std::unique_ptr<Player> m_player1; 0092 std::unique_ptr<Player> m_player2; 0093 QString m_name1{Helper::randomString()}; 0094 QString m_name2{Helper::randomString()}; 0095 }; 0096 void InstantMessagingTest::initTestCase() 0097 { 0098 registerTypeTest(); 0099 } 0100 void InstantMessagingTest::init() 0101 { 0102 m_player1= std::make_unique<Player>(m_name1, Qt::blue, true); 0103 m_player2= std::make_unique<Player>(m_name2, Qt::green, false); 0104 0105 m_model.reset(new PlayerModel()); 0106 m_ctrl.reset(new InstantMessagingController(new DiceRoller(this), m_model.get())); 0107 0108 // m_ctrl->setDiceParser(); 0109 0110 m_model->addPlayer(m_player1.get()); 0111 m_model->addPlayer(m_player2.get()); 0112 0113 auto model= m_ctrl->mainModel(); 0114 0115 QCOMPARE(model->rowCount(), 1); 0116 } 0117 0118 void InstantMessagingTest::cleanup() 0119 { 0120 m_ctrl.release(); 0121 m_model.release(); 0122 m_player1.release(); 0123 m_player2.release(); 0124 } 0125 0126 void InstantMessagingTest::sendMessage() 0127 { 0128 QFETCH(QString, text); 0129 QFETCH(QUrl, url); 0130 QFETCH(int, idx); 0131 QFETCH(QString, name); 0132 QFETCH(InstantMessaging::MessageInterface::MessageType, type); 0133 0134 QList<Player*> list({m_player1.get(), m_player2.get()}); 0135 auto personId= list[idx]->uuid(); 0136 0137 auto global= m_ctrl->globalChatroom(); 0138 auto model= global->messageModel(); 0139 0140 QSignalSpy spy(model, &InstantMessaging::MessageModel::messageAdded); 0141 0142 global->addMessage(text, url, personId, name); 0143 0144 spy.wait(2000); 0145 0146 QCOMPARE(spy.count(), 1); 0147 QCOMPARE(model->rowCount(), 1); 0148 0149 auto msgtype= model->data(model->index(0, 0), InstantMessaging::MessageModel::MessageTypeRole) 0150 .value<InstantMessaging::MessageInterface::MessageType>(); 0151 0152 QCOMPARE(msgtype, type); 0153 } 0154 void InstantMessagingTest::sendMessage_data() 0155 { 0156 QTest::addColumn<QString>("text"); 0157 QTest::addColumn<QUrl>("url"); 0158 QTest::addColumn<int>("idx"); 0159 QTest::addColumn<QString>("name"); 0160 QTest::addColumn<InstantMessaging::MessageInterface::MessageType>("type"); 0161 0162 QTest::addRow("text1") << "hello there" << QUrl{} << 0 << m_name1 << InstantMessaging::MessageInterface::Text; 0163 QTest::addRow("text2") << "General Kenobi" << QUrl{} << 1 << m_name2 << InstantMessaging::MessageInterface::Text; 0164 0165 QTest::addRow("dice1") << "!1d100" << QUrl{} << 0 << m_name1 << InstantMessaging::MessageInterface::Dice; 0166 QTest::addRow("dice2") << "!10d10" << QUrl{} << 1 << m_name2 << InstantMessaging::MessageInterface::Dice; 0167 0168 QTest::addRow("cmd1") << "/e draws his sword" << QUrl{} << 0 << m_name1 0169 << InstantMessaging::MessageInterface::Command; 0170 QTest::addRow("cmd2") << "/me pets the cat" << QUrl{} << 1 << m_name2 0171 << InstantMessaging::MessageInterface::Command; 0172 0173 QTest::addRow("error1") << "!4d10k8" << QUrl{} << 0 << m_name1 << InstantMessaging::MessageInterface::Error; 0174 QTest::addRow("error2") << "!4d10e[>0]" << QUrl{} << 1 << m_name2 << InstantMessaging::MessageInterface::Error; 0175 } 0176 0177 void InstantMessagingTest::splitScreen() {} 0178 void InstantMessagingTest::detachScreen() {} 0179 0180 QTEST_MAIN(InstantMessagingTest); 0181 0182 #include "tst_im.moc"