File indexing completed on 2024-05-12 05:40:47

0001 /***************************************************************************
0002  *   Copyright (C) 2015 by Renaud Guezennec                                *
0003  *   http://renaudguezennec.homelinux.org/accueil,3.html                   *
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 #include <QtTest/QtTest>
0021 
0022 #include <QMetaMethod>
0023 #include <QMetaObject>
0024 #include <QMetaProperty>
0025 
0026 #include "data/character.h"
0027 #include "data/localpersonmodel.h"
0028 #include "data/person.h"
0029 #include "data/player.h"
0030 #include "model/playermodel.h"
0031 #include <helper.h>
0032 
0033 #include "controller/instantmessagingcontroller.h"
0034 #include "data/chatroom.h"
0035 #include "data/chatroomfactory.h"
0036 #include "instantmessaging/dicemessage.h"
0037 #include "instantmessaging/errormessage.h"
0038 #include "instantmessaging/messagefactory.h"
0039 #include "instantmessaging/messageinterface.h"
0040 #include "instantmessaging/textmessage.h"
0041 #include "instantmessaging/textwritercontroller.h"
0042 #include "model/chatroomsplittermodel.h"
0043 #include "model/filteredplayermodel.h"
0044 #include "model/filterinstantmessagingmodel.h"
0045 #include "model/instantmessagingmodel.h"
0046 #include "model/messagemodel.h"
0047 #include "qml_components/avatarprovider.h"
0048 #include "rwidgets/mediacontainers/instantmessagingview.h"
0049 #include <QAbstractItemModelTester>
0050 
0051 class Player;
0052 class NetworkLink;
0053 class NetworkMessage;
0054 
0055 class ChatWindowTest : public QObject
0056 {
0057     Q_OBJECT
0058 
0059 public:
0060     ChatWindowTest();
0061 
0062 private slots:
0063     void init();
0064     void cleanup();
0065 
0066     void propertiesSetGetTest();
0067     void propertiesSetGetTest_data();
0068 
0069     void chatRoomCountTest();
0070     void chatRoomCountTest_data();
0071 
0072     void textwriterControllerTest();
0073     void diceMessageTest();
0074 
0075     void errorMessageTest();
0076 
0077 private:
0078     std::unique_ptr<PlayerModel> m_playerModel;
0079     std::unique_ptr<InstantMessagingController> m_imCtrl;
0080 };
0081 ChatWindowTest::ChatWindowTest() {}
0082 
0083 void ChatWindowTest::init()
0084 {
0085     m_playerModel.reset(new PlayerModel);
0086     new QAbstractItemModelTester(m_playerModel.get());
0087     m_imCtrl.reset(new InstantMessagingController(nullptr, m_playerModel.get()));
0088 }
0089 void ChatWindowTest::errorMessageTest()
0090 {
0091     auto owner= Helper::randomString();
0092     auto writer= Helper::randomString();
0093     auto time= QDateTime::currentDateTime();
0094     InstantMessaging::ErrorMessage msg(owner, writer, time);
0095 
0096     QSignalSpy spy(&msg, &InstantMessaging::ErrorMessage::textChanged);
0097     auto text1= Helper::randomString();
0098     msg.setText(text1);
0099     msg.setText(text1);
0100 
0101     spy.wait(10);
0102     QCOMPARE(spy.count(), 1);
0103 
0104     auto text2= Helper::randomString();
0105     msg.setText(text2);
0106     spy.wait(10);
0107     QCOMPARE(spy.count(), 2);
0108 
0109     QCOMPARE(msg.text(), text2);
0110 
0111     QCOMPARE(msg.owner(), owner);
0112     QCOMPARE(msg.writer(), writer);
0113     QCOMPARE(msg.dateTime(), time);
0114 }
0115 void ChatWindowTest::propertiesSetGetTest()
0116 {
0117     QFETCH(QString, propertyName);
0118     QFETCH(QVariant, propertyVal);
0119 
0120     auto meta= m_imCtrl->metaObject();
0121 
0122     auto proper= meta->property(meta->indexOfProperty(propertyName.toUtf8()));
0123 
0124     auto signal= proper.notifySignal();
0125 
0126     QSignalSpy spy(m_imCtrl.get(), signal);
0127 
0128     auto b= m_imCtrl->setProperty(propertyName.toUtf8(), propertyVal);
0129 
0130     QVERIFY(b);
0131     QCOMPARE(spy.count(), 1);
0132 }
0133 
0134 void ChatWindowTest::propertiesSetGetTest_data()
0135 {
0136     QTest::addColumn<QString>("propertyName");
0137     QTest::addColumn<QVariant>("propertyVal");
0138 
0139     QTest::addRow("cmd 1") << "localId" << QVariant::fromValue(QString("tarniten"));
0140     QTest::addRow("cmd 2") << "nightMode" << QVariant::fromValue(true);
0141     QTest::addRow("cmd 3") << "visible" << QVariant::fromValue(true);
0142 }
0143 
0144 void ChatWindowTest::chatRoomCountTest()
0145 {
0146     QFETCH(QList<Player*>, playerList);
0147     QFETCH(int, expected);
0148 
0149     auto model= m_imCtrl->mainModel();
0150 
0151     auto chatrooms= model->data(model->index(0, 0), InstantMessaging::ChatroomSplitterModel::FilterModelRole)
0152                         .value<InstantMessaging::FilterInstantMessagingModel*>();
0153 
0154     for(auto player : playerList)
0155         m_playerModel->addPlayer(player);
0156 
0157     auto c= chatrooms->rowCount();
0158     QCOMPARE(c, expected);
0159 }
0160 
0161 void ChatWindowTest::chatRoomCountTest_data()
0162 {
0163     QTest::addColumn<QList<Player*>>("playerList");
0164     QTest::addColumn<int>("expected");
0165 
0166     QTest::addRow("cmd1") << QList<Player*>() << 1;
0167 
0168     QList<Player*> list;
0169     auto player= new Player();
0170     player->setUuid("1");
0171     list << player;
0172     QTest::addRow("cmd2") << list << 2;
0173 
0174     list.clear();
0175     player= new Player();
0176     player->setUuid("3");
0177     list << player;
0178     player= new Player();
0179     player->setUuid("2");
0180     list << player;
0181     QTest::addRow("cmd3") << list << 3;
0182 }
0183 
0184 void ChatWindowTest::textwriterControllerTest()
0185 {
0186     InstantMessaging::TextWriterController ctrl;
0187 
0188     QSignalSpy spy(&ctrl, &InstantMessaging::TextWriterController::urlChanged);
0189     QSignalSpy spy1(&ctrl, &InstantMessaging::TextWriterController::textComputed);
0190     QSignalSpy spy2(&ctrl, &InstantMessaging::TextWriterController::textChanged);
0191     QSignalSpy spy3(&ctrl, &InstantMessaging::TextWriterController::diceCommandChanged);
0192 
0193     auto str= Helper::randomString();
0194     ctrl.setText(str);
0195     ctrl.computeText();
0196 
0197     spy1.wait(100);
0198     QCOMPARE(spy1.count(), 1);
0199 
0200     spy1.clear();
0201     ctrl.setText(str);
0202 
0203     spy1.wait(10);
0204     QCOMPARE(spy1.count(), 0);
0205 
0206     auto url= Helper::randomUrl();
0207     ctrl.setText(url.toString());
0208     ctrl.computeText();
0209 
0210     spy.wait(10);
0211     spy1.wait(100);
0212 
0213     QCOMPARE(spy.count(), 2);
0214     QCOMPARE(ctrl.imageLink(), QString());
0215     ctrl.setText("www.perdu.com");
0216     ctrl.computeText();
0217 
0218     spy.wait(10);
0219     spy1.wait(100);
0220     QCOMPARE(spy.count(), 3);
0221 
0222     auto cmd= QString("!%1").arg(Helper::randomString());
0223     ctrl.setText(cmd);
0224 
0225     spy3.wait(10);
0226     QCOMPARE(spy3.count(), 1);
0227     QVERIFY(ctrl.diceCommand());
0228 
0229     ctrl.setText(Helper::randomString());
0230     cmd= QString("!%1").arg(Helper::randomString());
0231     ctrl.setText(cmd);
0232 
0233     spy3.wait(10);
0234     QCOMPARE(spy3.count(), 3);
0235 
0236     QCOMPARE(spy2.count(), 10);
0237 
0238     spy2.clear();
0239 
0240     static QStringList msgs{Helper::randomString(), Helper::randomString(), Helper::randomString(),
0241                             Helper::randomString()};
0242 
0243     ctrl.setText(msgs[0]);
0244     QCOMPARE(ctrl.text(), msgs[0]);
0245     ctrl.send();
0246 
0247     ctrl.setText(msgs[1]);
0248     QCOMPARE(ctrl.text(), msgs[1]);
0249     ctrl.send();
0250 
0251     ctrl.setText(msgs[2]);
0252     QCOMPARE(ctrl.text(), msgs[2]);
0253     ctrl.send();
0254 
0255     ctrl.setText(msgs[3]);
0256     QCOMPARE(ctrl.text(), msgs[3]);
0257     ctrl.send();
0258 
0259     ctrl.up();
0260     QCOMPARE(ctrl.text(), msgs[3]);
0261     ctrl.up();
0262     QCOMPARE(ctrl.text(), msgs[2]);
0263 
0264     ctrl.down();
0265     QCOMPARE(ctrl.text(), msgs[3]);
0266 
0267     QCOMPARE(spy2.count(), 11);
0268 
0269     for(int i= 0; i < 200; ++i)
0270     {
0271         ctrl.setText(Helper::randomString());
0272         ctrl.send();
0273     }
0274 
0275     for(int i= 0; i < 200; ++i)
0276         ctrl.up();
0277 
0278     for(int i= 0; i < 200; ++i)
0279         ctrl.down();
0280 }
0281 
0282 void ChatWindowTest::diceMessageTest()
0283 {
0284     auto owner= Helper::randomString();
0285     auto writer= Helper::randomString();
0286 
0287     InstantMessaging::DiceMessage msg(owner, writer, QDateTime::currentDateTime());
0288 
0289     msg.setText("{\n\"command\":\"8d10;\\\"Result: $1 [@1]\\\"\",\n   \"comment\":\"\",\n   \"error\":\"\",\n   "
0290                 "\"instructions\":[\n      {\n         \"diceval\":[\n            {\n               \"color\":\"\",\n  "
0291                 "             \"displayed\":false,\n               \"face\":10,\n               \"highlight\":true,\n  "
0292                 "             \"string\":\"<span style=\\\"color:red;font-weight:bold;\\\">9</span>\",\n               "
0293                 "\"uuid\":\"b8f031dc-04ec-41b6-9752-ae93f0b4d64f\",\n               \"value\":9\n            },\n      "
0294                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0295                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0296                 "style=\\\"color:red;font-weight:bold;\\\">2</span>\",\n               "
0297                 "\"uuid\":\"381cbf99-ba59-46d0-8350-596679315262\",\n               \"value\":2\n            },\n      "
0298                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0299                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0300                 "style=\\\"color:red;font-weight:bold;\\\">4</span>\",\n               "
0301                 "\"uuid\":\"c3632d2e-43af-41fb-97f3-d7e92d6d1f47\",\n               \"value\":4\n            },\n      "
0302                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0303                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0304                 "style=\\\"color:red;font-weight:bold;\\\">9</span>\",\n               "
0305                 "\"uuid\":\"f4962e7f-dd41-4fa9-afbe-f382a76274f0\",\n               \"value\":9\n            },\n      "
0306                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0307                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0308                 "style=\\\"color:red;font-weight:bold;\\\">10</span>\",\n               "
0309                 "\"uuid\":\"53679f15-6dde-418b-9743-a030df721c14\",\n               \"value\":10\n            },\n     "
0310                 "       {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0311                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0312                 "style=\\\"color:red;font-weight:bold;\\\">6</span>\",\n               "
0313                 "\"uuid\":\"20c8fe23-da7a-4319-9694-b93e6c4762d2\",\n               \"value\":6\n            },\n      "
0314                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0315                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0316                 "style=\\\"color:red;font-weight:bold;\\\">6</span>\",\n               "
0317                 "\"uuid\":\"28a8f62c-92b9-4784-bcb8-03d0880d6c14\",\n               \"value\":6\n            },\n      "
0318                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0319                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0320                 "style=\\\"color:red;font-weight:bold;\\\">7</span>\",\n               "
0321                 "\"uuid\":\"5aa9094b-e5f2-48b7-99bf-7d8a21ad7361\",\n               \"value\":7\n            }\n       "
0322                 "  ],\n         \"scalar\":53\n      },\n      {\n         \"string\":\"Result: $1 [@1]\"\n      }\n   "
0323                 "],\n   \"scalar\":\"53\",\n   \"string\":\"Result: 53 [<span "
0324                 "style=\\\"color:red;font-weight:bold;\\\">9</span>,<span "
0325                 "style=\\\"color:red;font-weight:bold;\\\">2</span>,<span "
0326                 "style=\\\"color:red;font-weight:bold;\\\">4</span>,<span "
0327                 "style=\\\"color:red;font-weight:bold;\\\">9</span>,<span "
0328                 "style=\\\"color:red;font-weight:bold;\\\">10</span>,<span "
0329                 "style=\\\"color:red;font-weight:bold;\\\">6</span>,<span "
0330                 "style=\\\"color:red;font-weight:bold;\\\">6</span>,<span "
0331                 "style=\\\"color:red;font-weight:bold;\\\">7</span>]\",\n   \"warning\":\"\"\n}");
0332 
0333     msg.setText("{\n\"command\":\"8d10;\\\"Result: $1 [@1]\\\"\",\n   \"comment\":\"\",\n   \"error\":\"\",\n   "
0334                 "\"instructions\":[\n      {\n         \"diceval\":[\n            {\n               \"color\":\"\",\n  "
0335                 "             \"displayed\":false,\n               \"face\":10,\n               \"highlight\":true,\n  "
0336                 "             \"string\":\"<span style=\\\"color:red;font-weight:bold;\\\">9</span>\",\n               "
0337                 "\"uuid\":\"b8f031dc-04ec-41b6-9752-ae93f0b4d64f\",\n               \"value\":9\n            },\n      "
0338                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0339                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0340                 "style=\\\"color:red;font-weight:bold;\\\">2</span>\",\n               "
0341                 "\"uuid\":\"381cbf99-ba59-46d0-8350-596679315262\",\n               \"value\":2\n            },\n      "
0342                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0343                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0344                 "style=\\\"color:red;font-weight:bold;\\\">4</span>\",\n               "
0345                 "\"uuid\":\"c3632d2e-43af-41fb-97f3-d7e92d6d1f47\",\n               \"value\":4\n            },\n      "
0346                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0347                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0348                 "style=\\\"color:red;font-weight:bold;\\\">9</span>\",\n               "
0349                 "\"uuid\":\"f4962e7f-dd41-4fa9-afbe-f382a76274f0\",\n               \"value\":9\n            },\n      "
0350                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0351                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0352                 "style=\\\"color:red;font-weight:bold;\\\">10</span>\",\n               "
0353                 "\"uuid\":\"53679f15-6dde-418b-9743-a030df721c14\",\n               \"value\":10\n            },\n     "
0354                 "       {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0355                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0356                 "style=\\\"color:red;font-weight:bold;\\\">6</span>\",\n               "
0357                 "\"uuid\":\"20c8fe23-da7a-4319-9694-b93e6c4762d2\",\n               \"value\":6\n            },\n      "
0358                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0359                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0360                 "style=\\\"color:red;font-weight:bold;\\\">6</span>\",\n               "
0361                 "\"uuid\":\"28a8f62c-92b9-4784-bcb8-03d0880d6c14\",\n               \"value\":6\n            },\n      "
0362                 "      {\n               \"color\":\"\",\n               \"displayed\":false,\n               "
0363                 "\"face\":10,\n               \"highlight\":true,\n               \"string\":\"<span "
0364                 "style=\\\"color:red;font-weight:bold;\\\">7</span>\",\n               "
0365                 "\"uuid\":\"5aa9094b-e5f2-48b7-99bf-7d8a21ad7361\",\n               \"value\":7\n            }\n       "
0366                 "  ],\n         \"scalar\":53\n      },\n      {\n         \"string\":\"Result: $1 [@1]\"\n      }\n   "
0367                 "],\n   \"scalar\":\"53\",\n   \"string\":\"Result: 53 [<span "
0368                 "style=\\\"color:red;font-weight:bold;\\\">9</span>,<span "
0369                 "style=\\\"color:red;font-weight:bold;\\\">2</span>,<span "
0370                 "style=\\\"color:red;font-weight:bold;\\\">4</span>,<span "
0371                 "style=\\\"color:red;font-weight:bold;\\\">9</span>,<span "
0372                 "style=\\\"color:red;font-weight:bold;\\\">10</span>,<span "
0373                 "style=\\\"color:red;font-weight:bold;\\\">6</span>,<span "
0374                 "style=\\\"color:red;font-weight:bold;\\\">6</span>,<span "
0375                 "style=\\\"color:red;font-weight:bold;\\\">7</span>]\",\n   \"warning\":\"\"\n}");
0376 
0377     msg.setText(
0378         "{\n    \"command\": \"2d6+2d8\",\n    \"comment\": \"\",\n    \"error\": \"\",\n    \"instructions\": [\n     "
0379         "   {\n            \"diceval\": [\n                {\n                    \"color\": \"\",\n                   "
0380         " \"displayed\": false,\n                    \"face\": 6,\n                    \"highlight\": true,\n          "
0381         "          \"string\": \"<span style=\\\"color:red;font-weight:bold;\\\">4</span>\",\n                    "
0382         "\"uuid\": \"f8077eac-d1d3-4711-bc1e-1f2ff6532d0c\",\n                    \"value\": 4\n                },\n   "
0383         "             {\n                    \"color\": \"\",\n                    \"displayed\": false,\n             "
0384         "       \"face\": 6,\n                    \"highlight\": true,\n                    \"string\": \"<span "
0385         "style=\\\"color:red;font-weight:bold;\\\">5</span>\",\n                    \"uuid\": "
0386         "\"763015b9-3764-43d9-8ef9-ae55cde23094\",\n                    \"value\": 5\n                },\n             "
0387         "   {\n                    \"color\": \"\",\n                    \"displayed\": false,\n                    "
0388         "\"face\": 8,\n                    \"highlight\": true,\n                    \"string\": \"<span "
0389         "style=\\\"color:red;font-weight:bold;\\\">3</span>\",\n                    \"uuid\": "
0390         "\"ecacc278-f5b8-41bd-a936-df29101c6d25\",\n                    \"value\": 3\n                },\n             "
0391         "   {\n                    \"color\": \"\",\n                    \"displayed\": false,\n                    "
0392         "\"face\": 8,\n                    \"highlight\": true,\n                    \"string\": \"<span "
0393         "style=\\\"color:red;font-weight:bold;\\\">4</span>\",\n                    \"uuid\": "
0394         "\"ae62c050-9155-4e50-8b01-ee9a8f3e0906\",\n                    \"value\": 4\n                }\n            "
0395         "],\n            \"scalar\": 16\n        }\n    ],\n    \"scalar\": \"16\",\n    \"string\": \"16\",\n    "
0396         "\"warning\": \"\"\n}\n");
0397 
0398     QCOMPARE(msg.command(), "2d6+2d8");
0399     QCOMPARE(msg.comment(), "");
0400     QVERIFY(!msg.result().isEmpty());
0401     QVERIFY(!msg.details().isEmpty());
0402     QVERIFY(!msg.text().isEmpty());
0403 }
0404 
0405 void ChatWindowTest::cleanup() {}
0406 
0407 QTEST_MAIN(ChatWindowTest);
0408 
0409 #include "tst_chat.moc"