File indexing completed on 2024-05-19 05:41:34

0001 /***************************************************************************
0002  *   Copyright (C) 2018 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 "data/cleveruri.h"
0023 #include <QMouseEvent>
0024 #include <data/character.h>
0025 #include <helper.h>
0026 #include <session/sessionitemmodel.h>
0027 //#include <session/sessionmanager.h>
0028 
0029 class SessionTest : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     SessionTest();
0035 
0036 private slots:
0037     void init();
0038     void testModel();
0039     void testManager();
0040     void cleanupTestCase();
0041     void saveAndLoadTest();
0042     void saveAndLoadTest_data();
0043 
0044 private:
0045     // std::unique_ptr<SessionManager> m_sessionManager;
0046     session::SessionItemModel* m_model;
0047 };
0048 
0049 Q_DECLARE_METATYPE(std::vector<Core::ContentType>);
0050 
0051 SessionTest::SessionTest()
0052 {
0053     qRegisterMetaType<ResourcesNode*>("ResourcesNode*");
0054 }
0055 
0056 void SessionTest::init()
0057 {
0058     /*   m_sessionManager.reset(new SessionManager(nullptr));
0059        m_model= m_sessionManager->getModel();*/
0060 }
0061 
0062 void SessionTest::testModel()
0063 {
0064     /*    m_sessionManager->show();
0065         QApplication::processEvents();
0066 
0067         CleverURI* uri1= new CleverURI("uri1", "/path/uri1", Core::ContentType::CHARACTERSHEET);
0068         CleverURI* uri2= new CleverURI("uri2", "/path/uri2", Core::ContentType::VECTORIALMAP);
0069         Character* character= new Character("character1", Qt::red, true);
0070         Chapter* chapter= new Chapter();
0071         chapter->setName("chapter1");
0072 
0073         QModelIndex index;
0074         m_model->addResource(uri1, index);
0075         m_model->addResource(uri2, index);
0076         m_model->addResource(character, index);
0077         m_model->addResource(chapter, index);
0078 
0079         QVERIFY2(m_model->rowCount(index) == 4, "NOT the excepted number of data");
0080 
0081         QSignalSpy layout(m_model, &SessionItemModel::layoutChanged);
0082         QPoint local(10, 10);
0083         QTest::mouseClick(m_sessionManager.get(), Qt::LeftButton, Qt::KeyboardModifiers(), local);
0084 
0085         for(int i= 0; i < 55; ++i)
0086         {
0087             QPoint moveMouse(10, 10 + i);
0088             QTest::mouseMove(m_sessionManager.get(), moveMouse);
0089         }
0090 
0091         QTest::mouseMove(m_sessionManager.get(), QPoint(10, 65));
0092         QTest::mouseRelease(m_sessionManager.get(), Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(10, 65));
0093 
0094         qDebug() << m_model->rowCount(index) << chapter << character << uri2 << uri1;
0095 
0096         qDebug() << "rowCount:" << m_model->rowCount(index);
0097         QVERIFY2(m_model->rowCount(index) == 4, "NOT the excepted number of data");
0098 
0099         // QVERIFY2(layout.count() == 1, "Drag and drop not working");
0100 
0101         m_model->clearData();*/
0102 }
0103 
0104 void SessionTest::testManager()
0105 {
0106     /*    QSignalSpy spy(m_sessionManager.get(), &SessionManager::sessionChanged);
0107         CleverURI* resources= new CleverURI();
0108         m_sessionManager->addRessource(resources);
0109         QVERIFY2(spy.count() == 1, "no signal about change in session");
0110 
0111         QSignalSpy spyOpen(m_sessionManager.get(), &SessionManager::openResource);
0112         auto index= m_model->index(0, 0);
0113         m_sessionManager->openResources(index);
0114         QVERIFY2(spyOpen.count() == 1, "no signal about change in session");*/
0115 }
0116 
0117 void SessionTest::saveAndLoadTest()
0118 {
0119     /*   QFETCH(std::vector<Core::ContentType>, list);
0120 
0121        auto imageUri= new CleverURI("Girafe", ":/assets/img/girafe.jpg", Core::ContentType::PICTURE);
0122        auto characterSheet= new CleverURI("bitume", ":/assets/charactersheet/bitume_fixed.rcs",
0123        Core::ContentType::CHARACTERSHEET); auto vmap= new CleverURI("vmap", ":/assets/vmap/test.vmap",
0124        Core::ContentType::VECTORIALMAP); auto text= new CleverURI("text", ":/assets/notes/test.odt",
0125        Core::ContentType::NOTES); auto sharednote= new CleverURI("sharednote", ":/assets/sharednotes/test.rsn",
0126        Core::ContentType::SHAREDNOTE); auto playlist= new CleverURI("playlist", ":/assets/list/list.m3u",
0127        CleverURI::SONGLIST); auto webview= new CleverURI("webview", "http://www.rolisteam.org",
0128        Core::ContentType::WEBVIEW);
0129 
0130        std::vector<CleverURI*> data;
0131        for(auto id : list)
0132        {
0133            CleverURI* uri= nullptr;
0134            switch(id)
0135            {
0136            case Core::ContentType::PICTURE:
0137                uri= imageUri;
0138                break;
0139            case Core::ContentType::CHARACTERSHEET:
0140                uri= characterSheet;
0141                break;
0142            case Core::ContentType::NOTES:
0143                uri= text;
0144                break;
0145            case Core::ContentType::VECTORIALMAP:
0146                uri= vmap;
0147                break;
0148            case Core::ContentType::SHAREDNOTE:
0149                uri= sharednote;
0150                break;
0151            case CleverURI::SONGLIST:
0152                uri= playlist;
0153                break;
0154            case Core::ContentType::WEBVIEW:
0155                uri= webview;
0156                break;
0157            default:
0158                break;
0159            }
0160            data.push_back(uri);
0161        }
0162 
0163        QSignalSpy spy(m_sessionManager.get(), &SessionManager::sessionChanged);
0164        for(auto uri : data)
0165        {
0166            m_sessionManager->addRessource(uri);
0167        }
0168        QCOMPARE(spy.count(), list.size());
0169 
0170        QByteArray array;
0171        QDataStream in(&array, QIODevice::WriteOnly);
0172        in.setVersion(QDataStream::Qt_5_7);
0173 
0174        m_sessionManager->saveSession(in);
0175 
0176        std::unique_ptr<SessionManager> sessionManager(new SessionManager(nullptr));
0177        QDataStream out(&array, QIODevice::ReadOnly);
0178        out.setVersion(QDataStream::Qt_5_7);
0179        sessionManager->loadSession(out);
0180 
0181        QCOMPARE(sessionManager->getModel()->rowCount(QModelIndex()),
0182                 m_sessionManager->getModel()->rowCount(QModelIndex()));
0183        QByteArray array2;
0184        QDataStream in2(&array2, QIODevice::WriteOnly);
0185        in2.setVersion(QDataStream::Qt_5_7);
0186        sessionManager->saveSession(in2);
0187 
0188        QCOMPARE(array.size(), array2.size());*/
0189 }
0190 
0191 void SessionTest::saveAndLoadTest_data()
0192 {
0193     /*    QTest::addColumn<std::vector<Core::ContentType>>("list");
0194 
0195         std::vector<Core::ContentType> data({Core::ContentType::VECTORIALMAP, Core::ContentType::SHAREDNOTE,
0196        Core::ContentType::CHARACTERSHEET, Core::ContentType::NOTES, Core::ContentType::PICTURE, CleverURI::SONGLIST,
0197                                                   Core::ContentType::WEBVIEW}); //
0198 
0199         // auto list = new std::vector<CleverURI*>();
0200         std::vector<Core::ContentType> list;
0201 
0202         int index= 0;
0203         for(unsigned int i= 0; i < data.size(); ++i)
0204         {
0205             auto comb_size= i + 1;
0206             do
0207             {
0208                 list.clear();
0209                 for(auto it= data.begin(); it < data.begin() + comb_size; ++it)
0210                 {
0211                     list.push_back(*it);
0212                 }
0213                 QTest::addRow("save %d", index) << list;
0214             } while(Helper::next_combination(data.begin(), data.begin() + comb_size, data.end()));
0215         }*/
0216 }
0217 
0218 void SessionTest::cleanupTestCase() {}
0219 QTEST_MAIN(SessionTest);
0220 
0221 #include "tst_session.moc"