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

0001 /***************************************************************************
0002  *   Copyright (C) 2011 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 <QColor>
0022 #include <QtCore/QCoreApplication>
0023 #include <QtCore/QString>
0024 #include <QtTest/QtTest>
0025 #include <data/character.h>
0026 #include <data/player.h>
0027 #include <helper.h>
0028 
0029 #define COUNT_TURN 2000
0030 
0031 class DataplayerTest : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     DataplayerTest();
0037 
0038 private Q_SLOTS:
0039     void initTestCase();
0040     void cleanupTestCase();
0041     void testGetSet();
0042     void testChildrenAddAndRemove();
0043 
0044 private:
0045     Player* m_player;
0046 };
0047 
0048 DataplayerTest::DataplayerTest() {}
0049 
0050 void DataplayerTest::initTestCase()
0051 {
0052     m_player= new Player();
0053 }
0054 
0055 void DataplayerTest::cleanupTestCase()
0056 {
0057     delete m_player;
0058 }
0059 
0060 void DataplayerTest::testGetSet()
0061 {
0062     QColor tmpcolor(Qt::green);
0063     m_player->setColor(tmpcolor);
0064     QVERIFY2(m_player->getColor() == tmpcolor, "Colors are not the same! Failure");
0065 
0066     QString tmpname("nametest");
0067     m_player->setName(tmpname);
0068     QVERIFY2(m_player->name() == tmpname, "Names are different! Failure");
0069 
0070     QVERIFY(m_player->isFullyDefined());
0071 
0072     auto v= Helper::randomString();
0073     m_player->setUserVersion(v);
0074     QCOMPARE(m_player->userVersion(), v);
0075 
0076     Player p2;
0077     p2.copyPlayer(m_player);
0078 
0079     m_player->getVariableDictionnary();
0080     m_player->removeChild(nullptr);
0081     m_player->addCharacter(nullptr);
0082     auto c= new Character(Helper::randomString(), Helper::randomColor(), true);
0083     m_player->addCharacter(c);
0084     m_player->addCharacter(c);
0085     QCOMPARE(c->getParentId(), m_player->uuid());
0086     QCOMPARE(c->getParentPlayer(), m_player);
0087     QCOMPARE(m_player->indexOf(c), 0);
0088     QCOMPARE(m_player->characterById(c->uuid()), c);
0089     QCOMPARE(m_player->getCharacterByIndex(0), c);
0090     QCOMPARE(m_player->getCharacterByIndex(80), nullptr);
0091     auto const& children= m_player->children();
0092     QCOMPARE(children.size(), 1);
0093     // QCOMPARE(children[0], c);
0094 
0095     m_player->removeChild(c);
0096 
0097     Player p3(Helper::randomString(), Helper::randomString(), Helper::randomColor(), false);
0098 
0099     auto uuid= Helper::randomString();
0100     auto nameC= Helper::randomString();
0101     auto colorC= Helper::randomColor();
0102     auto dataC= Helper::imageData();
0103 
0104     m_player->addCharacter(uuid, nameC, colorC, dataC, {}, true);
0105     m_player->addCharacter(uuid, nameC, colorC, dataC, {}, true);
0106 }
0107 void DataplayerTest::testChildrenAddAndRemove()
0108 {
0109     QVERIFY2(m_player->isLeaf() == false, "It has children! Failure");
0110     QList<Character*> list;
0111     m_player->clearCharacterList();
0112     for(int i= 0; i < COUNT_TURN; i++)
0113     {
0114         Character* tmp= new Character("Unknown", Qt::blue, "/fake/path/");
0115         list.append(tmp);
0116         m_player->addCharacter(tmp);
0117         QVERIFY2(m_player->characterCount() == list.size(), "The children count is not as expected! Failure");
0118     }
0119 
0120     m_player->removeChild(list.at(0));
0121     QVERIFY2(m_player->characterCount() == (list.size() - 1), "The children count is not as expected! Failure");
0122     m_player->clearCharacterList();
0123     QVERIFY2(m_player->characterCount() == 0, "The children count is not as expected! 0 Failure");
0124 }
0125 
0126 QTEST_MAIN(DataplayerTest);
0127 
0128 #include "tst_dataplayertest.moc"