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

0001 /***************************************************************************
0002  *   Copyright (C) 2011 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 
0021 #include "controller/view_controller/mindmapcontrollerbase.h"
0022 #include "controller/view_controller/webpagecontroller.h"
0023 #include "data/player.h"
0024 #include "data/shortcutmodel.h"
0025 #include "model/filteredplayermodel.h"
0026 #include "model/historymodel.h"
0027 #include "model/participantsmodel.h"
0028 #include "model/playermodel.h"
0029 #include "model/playerproxymodel.h"
0030 #include "model/contentmodel.h"
0031 #include "model/singlecontenttypemodel.h"
0032 #include "model/languagemodel.h"
0033 #include "model/colormodel.h"
0034 #include "model/characterstatemodel.h"
0035 #include "model/nonplayablecharactermodel.h"
0036 #include "model/actiononlistmodel.h"
0037 #include "data/shortcutmodel.h"
0038 #include "rwidgets/customs/shortcutvisitor.h"
0039 #include <QAbstractItemModelTester>
0040 #include <QClipboard>
0041 #include <QGuiApplication>
0042 #include <QtTest/QtTest>
0043 #include <helper.h>
0044 #include <memory>
0045 
0046 class ModelTest : public QObject
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051     ModelTest();
0052 
0053 private slots:
0054     void init();
0055 
0056     void participantsModel();
0057     void playerProxyModel();
0058     void historyModel();
0059     void filteredPlayerModel();
0060 
0061     void shortcutModel();
0062     void shortcutVisitor();
0063 
0064     void singleContentTypeModel();
0065 
0066     void languageModel();
0067     void actionListModel();
0068     void colorModel();
0069 
0070 private:
0071 };
0072 
0073 ModelTest::ModelTest() {}
0074 
0075 void ModelTest::init() {}
0076 
0077 void ModelTest::participantsModel()
0078 {
0079     std::unique_ptr<PlayerModel> player(new PlayerModel());
0080     std::unique_ptr<ParticipantsModel> model(new ParticipantsModel());
0081 
0082     model->setSourceModel(player.get());
0083 
0084     Player* p1= new Player;
0085     p1->setGM(true);
0086     p1->setName("GM");
0087     p1->setUuid("a");
0088     player->addPlayer(p1);
0089 
0090     model->setOwnerId(p1->uuid());
0091     QCOMPARE(model->ownerId(), p1->uuid());
0092 
0093     new QAbstractItemModelTester(player.get());
0094     new QAbstractItemModelTester(model.get());
0095 
0096     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::Hidden);
0097 
0098     model->promotePlayerToRead(QModelIndex());
0099     model->promotePlayerToReadWrite(QModelIndex());
0100     model->demotePlayerToRead(QModelIndex());
0101     model->demotePlayerToHidden(QModelIndex());
0102     model->promotePlayer(QModelIndex());
0103     model->demotePlayer(QModelIndex());
0104 
0105     QJsonObject obj;
0106     model->saveModel(obj);
0107     QVERIFY(!obj.isEmpty());
0108     model->loadModel(obj);
0109 
0110     //    qDebug() << "Parent" << model->rowCount(QModelIndex());
0111     //    qDebug() << "Write" << model->rowCount(model->index(0, 0, QModelIndex()));
0112     //    qDebug() << "Read" << model->rowCount(model->index(1, 0, QModelIndex()));
0113     //    qDebug() << "Hidden" << model->rowCount(model->index(2, 0, QModelIndex()));
0114 
0115     model->promotePlayerToRead(model->index(0, 0, model->index(2, 0, QModelIndex())));
0116 
0117     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::ReadOnly);
0118 
0119     model->promotePlayerToReadWrite(model->index(0, 0, model->index(1, 0, QModelIndex())));
0120     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::ReadWrite);
0121 
0122     model->demotePlayerToRead(model->index(0, 0, model->index(0, 0, QModelIndex())));
0123     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::ReadOnly);
0124 
0125     model->demotePlayerToHidden(model->index(0, 0, model->index(1, 0, QModelIndex())));
0126     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::Hidden);
0127 
0128     model->promotePlayer(model->index(0, 0, model->index(2, 0, QModelIndex())));
0129     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::ReadOnly);
0130 
0131     model->demotePlayer(model->index(0, 0, model->index(1, 0, QModelIndex())));
0132     QCOMPARE(model->getPermissionFor(p1), ParticipantsModel::Hidden);
0133 
0134     Player* p2= new Player;
0135     p2->setGM(false);
0136     p2->setName("Player");
0137     p2->setUuid("b");
0138     player->addPlayer(p2);
0139     QCOMPARE(model->getPermissionFor(p2), ParticipantsModel::Hidden);
0140 }
0141 
0142 void ModelTest::playerProxyModel()
0143 {
0144     std::unique_ptr<PlayerModel> player(new PlayerModel());
0145     std::unique_ptr<PlayerProxyModel> model(new PlayerProxyModel());
0146 
0147     model->setSourceModel(player.get());
0148 
0149     new QAbstractItemModelTester(player.get());
0150     new QAbstractItemModelTester(model.get());
0151 
0152     Player* p1= new Player;
0153     p1->setGM(true);
0154     p1->setName("GM");
0155     p1->setUuid("a");
0156     player->addPlayer(p1);
0157 
0158     Player* p2= new Player;
0159     p2->setGM(false);
0160     p2->addCharacter(Helper::randomString(), Helper::randomString(), Helper::randomColor(), {}, {}, false);
0161     p2->setName("Player");
0162     p2->setUuid("b");
0163     player->addPlayer(p2);
0164 
0165     QCOMPARE(model->rowCount(), 2);
0166 
0167     QVERIFY(!model->hasChildren(model->index(0, 0, QModelIndex())));
0168     QCOMPARE(model->rowCount(model->index(1, 0, QModelIndex())), 0);
0169 }
0170 
0171 void ModelTest::historyModel()
0172 {
0173     history::HistoryModel model;
0174     new QAbstractItemModelTester(&model);
0175 
0176     auto max= Helper::generate<int>(1, 10);
0177     model.setMaxCapacity(max);
0178     QCOMPARE(model.maxCapacity(), max);
0179 
0180     model.setMaxCapacity(max);
0181 
0182     model.addLink(Helper::randomUrl(), Helper::randomString(),
0183                   Helper::randomFromList<Core::ContentType>(
0184                       {Core::ContentType::VECTORIALMAP, Core::ContentType::PICTURE, Core::ContentType::NOTES,
0185                        Core::ContentType::CHARACTERSHEET, Core::ContentType::SHAREDNOTE, Core::ContentType::PDF,
0186                        Core::ContentType::WEBVIEW, Core::ContentType::INSTANTMESSAGING, Core::ContentType::MINDMAP,
0187                        Core::ContentType::UNKNOWN}));
0188 
0189     QCOMPARE(model.rowCount(), 1);
0190     model.addLink(Helper::randomUrl(), Helper::randomString(),
0191                   Helper::randomFromList<Core::ContentType>(
0192                       {Core::ContentType::VECTORIALMAP, Core::ContentType::PICTURE, Core::ContentType::NOTES,
0193                        Core::ContentType::CHARACTERSHEET, Core::ContentType::SHAREDNOTE, Core::ContentType::PDF,
0194                        Core::ContentType::WEBVIEW, Core::ContentType::INSTANTMESSAGING, Core::ContentType::MINDMAP,
0195                        Core::ContentType::UNKNOWN}));
0196 
0197     QCOMPARE(model.rowCount(), 2);
0198 
0199     auto infos= model.data();
0200 
0201     for(const auto& info : infos)
0202     {
0203         QCOMPARE(model.idToPath(info.id).bookmarked, info.bookmarked);
0204         QCOMPARE(model.idToPath(info.id).displayName, info.displayName);
0205         QCOMPARE(model.idToPath(info.id).id, info.id);
0206         QCOMPARE(model.idToPath(info.id).lastAccess, info.lastAccess);
0207         QCOMPARE(model.idToPath(info.id).type, info.type);
0208         QCOMPARE(model.idToPath(info.id).url, info.url);
0209     }
0210 
0211     for(int i= 0; i < model.rowCount(); i++)
0212     {
0213         for(int j= 0; j < model.columnCount(); j++)
0214         {
0215             auto val1= model.data(model.index(i, j), Qt::DisplayRole);
0216             auto val2= model.data(model.index(i, j), Qt::UserRole + 1 + j);
0217             QCOMPARE(val2, val1);
0218         }
0219 
0220         for(int k= Qt::UserRole + 1; k < history::HistoryModel::BookmarkRole; ++k)
0221         {
0222             model.data(model.index(i, 0), k);
0223         }
0224     }
0225 
0226     model.clear();
0227     model.setLinks(infos);
0228 
0229     model.refreshAccess(infos[0].id);
0230     model.refreshAccess(infos[1].id);
0231 }
0232 
0233 void ModelTest::filteredPlayerModel()
0234 {
0235     std::unique_ptr<PlayerModel> player(new PlayerModel());
0236     std::unique_ptr<InstantMessaging::FilteredPlayerModel> model(
0237         new InstantMessaging::FilteredPlayerModel(QStringList{"b"}, nullptr));
0238 
0239     model->setSourceModel(player.get());
0240 
0241     new QAbstractItemModelTester(player.get());
0242     new QAbstractItemModelTester(model.get());
0243 
0244     Player* p1= new Player;
0245     p1->setGM(true);
0246     p1->setName("GM");
0247     p1->setUuid("a");
0248     player->addPlayer(p1);
0249 
0250     Player* p2= new Player;
0251     p2->setGM(false);
0252     p2->addCharacter(Helper::randomString(), Helper::randomString(), Helper::randomColor(), {}, {}, false);
0253     p2->setName("Player");
0254     p2->setUuid("b");
0255     player->addPlayer(p2);
0256 
0257     QCOMPARE(model->rowCount(), 1);
0258 
0259     QCOMPARE(model->recipiantIds(), QStringList{"b"});
0260     QVERIFY(model->hasRecipiant(QString("b")));
0261     QCOMPARE(model->recipiantName(QString("b")), QString("Player"));
0262     QVERIFY(!model->hasRecipiant(Helper::randomString()));
0263 }
0264 
0265 void ModelTest::shortcutModel()
0266 {
0267     ShortCutModel model;
0268 
0269     new QAbstractItemModelTester(&model);
0270 
0271     QCOMPARE(model.rowCount(), 0);
0272 
0273     auto cat1= Helper::randomString();
0274 
0275     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0276     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0277     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0278     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0279     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0280 
0281     QCOMPARE(model.rowCount(), 0);
0282 
0283     model.addCategory(cat1);
0284     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0285     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0286     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0287     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0288     model.insertShortCut(cat1, Helper::randomString(), Helper::randomString());
0289 
0290     QCOMPARE(model.rowCount(), 1);
0291     QCOMPARE(model.rowCount(model.index(0, 0, QModelIndex())), 5);
0292 
0293     auto cat2= Helper::randomString();
0294     model.addCategory(cat2);
0295 
0296     auto cat3= Helper::randomString();
0297     model.addCategory(cat3);
0298 
0299     model.insertShortCut(cat2, Helper::randomString(), Helper::randomString());
0300     model.insertShortCut(cat2, Helper::randomString(), Helper::randomString());
0301     QCOMPARE(model.rowCount(), 3);
0302     QCOMPARE(model.rowCount(model.index(1, 0, QModelIndex())), 2);
0303 
0304     model.insertShortCut(cat3, Helper::randomString(), Helper::randomString());
0305     model.insertShortCut(cat3, Helper::randomString(), Helper::randomString());
0306     model.insertShortCut(cat3, Helper::randomString(), Helper::randomString());
0307     model.insertShortCut(cat3, Helper::randomString(), Helper::randomString());
0308 
0309     QCOMPARE(model.rowCount(), 3);
0310     QCOMPARE(model.rowCount(model.index(2, 0, QModelIndex())), 4);
0311 
0312     model.removeCategory(cat2);
0313     model.removeCategory(Helper::randomString());
0314     QCOMPARE(model.rowCount(), 2);
0315     QCOMPARE(model.rowCount(model.index(0, 0, QModelIndex())), 5);
0316     QCOMPARE(model.rowCount(model.index(1, 0, QModelIndex())), 4);
0317 
0318     QVERIFY(!model.headerData(0, Qt::Vertical, Qt::EditRole).isValid());
0319     QVERIFY(!model.headerData(0, Qt::Vertical, Qt::DisplayRole).isValid());
0320 
0321     QCOMPARE(model.headerData(0, Qt::Horizontal, Qt::DisplayRole).toString(), "Action");
0322     QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), "Key");
0323 
0324     model.index(-1, -1, QModelIndex());
0325 
0326     auto name= Helper::randomString();
0327 
0328     Category cat(name);
0329 
0330     QCOMPARE(cat.name(), name);
0331     name= Helper::randomString();
0332     cat.setName(name);
0333     QCOMPARE(cat.name(), name);
0334 
0335     {
0336         auto seq= Helper::randomString();
0337         auto name= Helper::randomString();
0338         ShortCut cut(name, seq);
0339 
0340         QCOMPARE(cut.getName(), name);
0341         QCOMPARE(cut.getSequence(), QKeySequence(seq));
0342         cat.insertShortcut(name, seq);
0343         auto shortcut= cat.getShortCut(0);
0344 
0345         QVERIFY(cat.hasShortCut(shortcut));
0346         QCOMPARE(cat.indexOf(shortcut), 0);
0347 
0348         QVERIFY(!cat.hasShortCut(nullptr));
0349         QCOMPARE(cat.indexOf(nullptr), -1);
0350     }
0351 }
0352 
0353 void ModelTest::shortcutVisitor()
0354 {
0355     QWidget wid;
0356     wid.setObjectName(Helper::randomString());
0357 
0358     auto act= new QAction(Helper::randomString());
0359     act->setShortcut(QKeySequence::New);
0360     wid.addAction(act);
0361 
0362     act= new QAction(Helper::randomString());
0363     act->setShortcut(QKeySequence::Open);
0364     wid.addAction(act);
0365 
0366     act= new QAction(Helper::randomString());
0367     act->setShortcut(QKeySequence("Ctrl+E"));
0368     wid.addAction(act);
0369 
0370     ShortcutVisitor visitor;
0371     QWidget wid2;
0372 
0373     act= new QAction(Helper::randomString());
0374     act->setShortcut(Helper::randomString(1));
0375     wid2.addAction(act);
0376 
0377     QWidget wid22(&wid2);
0378 
0379     act= new QAction(Helper::randomString());
0380     act->setShortcut(Helper::randomString(1));
0381     wid22.addAction(act);
0382 
0383     QWidget wid4;
0384 
0385     {
0386         QWidget wid3;
0387 
0388         act= new QAction(Helper::randomString());
0389         act->setShortcut(Helper::randomString(1));
0390         wid3.addAction(act);
0391 
0392         act= new QAction(Helper::randomString());
0393         act->setShortcut(Helper::randomString(1));
0394         wid3.addAction(act);
0395 
0396         visitor.registerWidget(&wid, Helper::randomString(), true);
0397         visitor.registerWidget(&wid, Helper::randomString(), true);
0398         visitor.registerWidget(&wid2, Helper::randomString(), true);
0399         visitor.registerWidget(&wid3, Helper::randomString(), true);
0400         visitor.registerWidget(&wid4, Helper::randomString(), true);
0401 
0402         auto model= visitor.getModel();
0403 
0404         QCOMPARE(model->rowCount(), 3);
0405         QCOMPARE(model->rowCount(model->index(0, 0, QModelIndex())), 3);
0406         QCOMPARE(model->rowCount(model->index(1, 0, QModelIndex())), 1);
0407         QCOMPARE(model->rowCount(model->index(2, 0, QModelIndex())), 2);
0408     }
0409     auto model= visitor.getModel();
0410 
0411     QCOMPARE(model->rowCount(), 2);
0412     visitor.unregisterWidget(&wid2);
0413     visitor.unregisterWidget(&wid2);
0414     QCOMPARE(model->rowCount(), 1);
0415 }
0416 
0417 void ModelTest::singleContentTypeModel()
0418 {
0419     auto singleModel = std::make_unique<SingleContentTypeModel>(Core::ContentType::WEBVIEW);
0420     auto contentModel = std::make_unique<ContentModel>();
0421 
0422     singleModel->setSourceModel(contentModel.get());
0423 
0424     new QAbstractItemModelTester(singleModel.get());
0425     new QAbstractItemModelTester(contentModel.get());
0426 
0427     auto id = Helper::randomString();
0428     auto data = new WebpageController(id);
0429     contentModel->appendMedia(data);
0430     contentModel->appendMedia(new WebpageController());
0431     contentModel->appendMedia(new WebpageController());
0432     contentModel->appendMedia(new mindmap::MindMapControllerBase(true, Helper::randomString()));
0433 
0434     QCOMPARE(singleModel->rowCount(), 3);
0435     QVERIFY(singleModel->contains(id));
0436     QVERIFY(!singleModel->contains(Helper::randomString(12)));
0437     QCOMPARE(dynamic_cast<WebpageController*>(singleModel->controller(id)), data);
0438 
0439 
0440 }
0441 void ModelTest::languageModel()
0442 {
0443     Q_INIT_RESOURCE(translations);
0444 
0445     auto langModel = std::make_unique<LanguageModel>();
0446 
0447     new QAbstractItemModelTester(langModel.get());
0448     auto syst = QLocale::system();
0449 
0450     if(langModel->rowCount()==0) {
0451         qDebug() << "Language model is Empty!!";
0452         return;
0453     }
0454 
0455     auto idx = langModel->indexSystemLocale(QLocale::languageToCode(syst.language()));
0456     qDebug() << "id" << idx << QLocale::languageToCode(syst.language());
0457     QVERIFY(idx > -1);
0458     QVERIFY(langModel->indexSystemLocale(Helper::randomString()) == -1);
0459 
0460     langModel->pathFromIndex(langModel->index(idx,0));
0461 }
0462 
0463 void ModelTest::actionListModel()
0464 {
0465     auto actModel = std::make_unique<ActionOnListModel>(QStringList{},QList<ActionInfo>{},QString{});
0466     new QAbstractItemModelTester(actModel.get());
0467 }
0468 
0469 void ModelTest::colorModel()
0470 {
0471     auto model = std::make_unique<ColorModel>();
0472     new QAbstractItemModelTester(model.get());
0473     auto src = new campaign::NonPlayableCharacterModel(new CharacterStateModel());
0474     model->setSourceModel(src);
0475 }
0476 
0477 QTEST_MAIN(ModelTest);
0478 
0479 #include "tst_modelstest.moc"