File indexing completed on 2024-05-12 05:40:50
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 <QtCore/QString> 0022 #include <QtTest/QtTest> 0023 0024 #include <data/character.h> 0025 #include <data/characterstate.h> 0026 #include <charactersheet/charactersheet.h> 0027 0028 #include "worker/iohelper.h" 0029 #include <helper.h> 0030 0031 #define COUNT_TURN 2000 0032 0033 CharacterAction* buildAction() 0034 { 0035 auto res = new CharacterAction; 0036 res->setCommand(Helper::randomString()); 0037 res->setName(Helper::randomString()); 0038 return res; 0039 } 0040 CharacterProperty* buildProperty() 0041 { 0042 auto res = new CharacterProperty; 0043 res->setValue(Helper::randomString()); 0044 res->setName(Helper::randomString()); 0045 return res; 0046 } 0047 CharacterShape* buildShape() 0048 { 0049 auto res = new CharacterShape; 0050 res->setSize(Helper::generate<int>(1, 12)); 0051 res->setImage(QImage(Helper::imagePath())); 0052 res->setUri(Helper::imagePath()); 0053 res->setName(Helper::randomString()); 0054 return res; 0055 } 0056 0057 class DataCharacterTest : public QObject 0058 { 0059 Q_OBJECT 0060 0061 public: 0062 DataCharacterTest(); 0063 0064 private Q_SLOTS: 0065 void testSetAndGet(); 0066 void testProperty(); 0067 void initTestCase(); 0068 void cleanupTestCase(); 0069 void testCharacterAction(); 0070 void testCharacterShape(); 0071 void testCharacterProperty(); 0072 0073 void propertiesTest(); 0074 0075 private: 0076 std::unique_ptr<Character> m_character; 0077 }; 0078 0079 DataCharacterTest::DataCharacterTest() {} 0080 void DataCharacterTest::initTestCase() 0081 { 0082 m_character.reset(new Character()); 0083 } 0084 0085 void DataCharacterTest::cleanupTestCase() 0086 { 0087 } 0088 void DataCharacterTest::testSetAndGet() 0089 { 0090 m_character->setName("Name"); 0091 QVERIFY2(m_character->name() == "Name", "names are different"); 0092 0093 m_character->setAvatar(IOHelper::imageToData(QImage(":/img/girafe.jpg"))); 0094 QVERIFY2(m_character->hasAvatar(), "has no avatar"); 0095 0096 m_character->setColor(Qt::red); 0097 QVERIFY2(m_character->getColor() == Qt::red, "not the right color"); 0098 0099 m_character->setNpc(true); 0100 QVERIFY(m_character->isNpc()); 0101 0102 m_character->setNpc(false); 0103 QVERIFY(!m_character->isNpc()); 0104 0105 m_character->setNumber(10); 0106 QVERIFY(m_character->number() == 10); 0107 0108 0109 Character cart2(Helper::randomString(),Helper::randomString(),Helper::randomColor(), false, 0); 0110 0111 QList<CharacterAction*> actions; 0112 for(int i = Helper::generate<int>(1,10); i >= 0; --i) 0113 { 0114 actions << buildAction(); 0115 } 0116 m_character->defineActionList(actions); 0117 0118 QList<CharacterProperty*> properties; 0119 for(int i = Helper::generate<int>(1,10); i >= 0; --i) 0120 { 0121 properties << buildProperty(); 0122 } 0123 m_character->definePropertiesList(properties); 0124 0125 0126 QList<CharacterShape*> shapes; 0127 for(int i = Helper::generate<int>(1,10); i >= 0; --i) 0128 { 0129 shapes << buildShape(); 0130 } 0131 m_character->defineShapeList(shapes); 0132 0133 m_character->setImageProvider(nullptr); 0134 QCOMPARE(m_character->getImageProvider(), nullptr); 0135 0136 0137 auto p = reinterpret_cast<RolisteamImageProvider*>(Helper::generate<int>(1,10)); 0138 m_character->setImageProvider(p); 0139 QCOMPARE(m_character->getImageProvider(), p); 0140 0141 m_character->setDistancePerTurn(1.0); 0142 m_character->setDistancePerTurn(1.0); 0143 0144 QCOMPARE(m_character->getParentId(), QString()); 0145 0146 0147 auto vs = m_character->getVariableDictionnary(); 0148 QCOMPARE(vs.size(), properties.size()); 0149 0150 auto sheet= m_character->getSheet(); 0151 QVERIFY(sheet == nullptr); 0152 0153 m_character->setSheet(new CharacterSheet()); 0154 auto vs2 = m_character->getVariableDictionnary(); 0155 QCOMPARE(vs2.size(), 0); 0156 0157 m_character->setSheet(sheet); 0158 0159 m_character->addShape(Helper::randomString(), Helper::imagePath()); 0160 m_character->addShape(Helper::randomString(), Helper::randomUrl().toString()); 0161 QVERIFY(m_character->currentStateLabel().isEmpty()); 0162 QVERIFY(m_character->currentStateImage().isNull()); 0163 0164 m_character->setCurrentShape(0); 0165 m_character->setCurrentShape(800); 0166 m_character->setCurrentShape(2); 0167 m_character->setCurrentShape(2); 0168 m_character->setCurrentShape(nullptr); 0169 m_character->setCurrentShape(nullptr); 0170 QCOMPARE(m_character->currentShape(), nullptr); 0171 auto s = buildShape(); 0172 0173 m_character->addShape(s); 0174 m_character->setCurrentShape(s); 0175 QCOMPARE(m_character->currentShape(), s); 0176 0177 m_character->addShape(nullptr); 0178 0179 m_character->addProperty(buildProperty()); 0180 m_character->addProperty(nullptr); 0181 } 0182 void DataCharacterTest::testProperty() 0183 { 0184 QSignalSpy spy(m_character.get(), &Character::currentHealthPointsChanged); 0185 m_character->setHealthPointsCurrent(10); 0186 QVERIFY(spy.count() == 1); 0187 0188 QSignalSpy spyMaxHP(m_character.get(), &Character::maxHPChanged); 0189 m_character->setHealthPointsMax(200); 0190 QVERIFY(spyMaxHP.count() == 1); 0191 0192 QSignalSpy spyminHP(m_character.get(), &Character::minHPChanged); 0193 m_character->setHealthPointsMin(50); 0194 QVERIFY(spyminHP.count() == 1); 0195 0196 QSignalSpy spydistancePerTurn(m_character.get(), &Character::distancePerTurnChanged); 0197 m_character->setDistancePerTurn(25.9); 0198 QVERIFY(spydistancePerTurn.count() == 1); 0199 0200 QSignalSpy spyInit(m_character.get(), &Character::initiativeChanged); 0201 m_character->setInitiativeScore(24); 0202 QVERIFY(spyInit.count() == 1); 0203 0204 //auto state2= new CharacterState(); 0205 0206 0207 /*m_character->setState(state2); 0208 QSignalSpy spyState(m_character, &Character::stateChanged); 0209 auto state= new CharacterState(); 0210 state->setLabel("test"); 0211 state->setColor(Qt::red); 0212 state->setIsLocal(true); 0213 m_character->setState(state); 0214 QVERIFY(spyState.count() == 1); 0215 QVERIFY(m_character->getState() == state); 0216 m_character->setState(state); 0217 QVERIFY(spyState.count() == 1); 0218 0219 m_character->setAvatarPath("/home/rolisteam"); 0220 QSignalSpy spyAvatar(m_character, &Character::avatarPathChanged); 0221 m_character->setAvatarPath("/dev/null"); 0222 QVERIFY(spyAvatar.count() == 1); 0223 m_character->setAvatarPath("/dev/null"); 0224 QVERIFY(spyAvatar.count() == 1); 0225 0226 m_character->setNpc(false); 0227 QSignalSpy spyIsNpc(m_character, &Character::npcChanged); 0228 m_character->setNpc(true); 0229 QVERIFY(spyIsNpc.count() == 1); 0230 m_character->setNpc(true); 0231 QVERIFY(spyIsNpc.count() == 1); 0232 0233 m_character->setLifeColor(Qt::green); 0234 QSignalSpy spyColor(m_character, &Character::lifeColorChanged); 0235 m_character->setLifeColor(Qt::red); 0236 QVERIFY(spyColor.count() == 1); 0237 m_character->setLifeColor(Qt::red); 0238 QVERIFY(spyColor.count() == 1); 0239 0240 m_character->setInitCommand(""); 0241 QSignalSpy spyInitCommand(m_character, &Character::initCommandChanged); 0242 m_character->setInitCommand("8G4"); 0243 QVERIFY(spyColor.count() == 1); 0244 m_character->setInitCommand("8G4"); 0245 QVERIFY(spyColor.count() == 1);*/ 0246 } 0247 0248 void DataCharacterTest::testCharacterAction() 0249 { 0250 CharacterAction action; 0251 0252 QString command("8G4"); 0253 QString name("name"); 0254 QString name2("name2"); 0255 QString command2("9G4"); 0256 0257 action.setCommand(command); 0258 QVERIFY(action.command() == command); 0259 0260 action.setName(name); 0261 QVERIFY(action.name() == name); 0262 0263 QVERIFY(action.getType() == CharacterField::Action); 0264 0265 QVERIFY(action.getData(0, Qt::DisplayRole) == name); 0266 QVERIFY(action.getData(1, Qt::DisplayRole) == command); 0267 QVERIFY(!action.getData(1, Qt::DecorationRole).isValid()); 0268 0269 action.setData(0, name2, Qt::DisplayRole); 0270 action.setData(1, command2, Qt::DisplayRole); 0271 0272 QVERIFY(action.name() == name2); 0273 QVERIFY(action.command() == command2); 0274 } 0275 void DataCharacterTest::testCharacterShape() 0276 { 0277 CharacterShape shape; 0278 0279 QString uri(":/assets/img/girafe.jpg"); 0280 QString name("name"); 0281 QString name2("name2"); 0282 QString uri2(":/assets/img/lion.jpg"); 0283 QImage img(":/assets/img/girafe.jpg"); 0284 QImage img2(":/assets/img/lion.jpg"); 0285 0286 shape.setUri(uri); 0287 QVERIFY(shape.uri() == uri); 0288 0289 shape.setName(name); 0290 QVERIFY(shape.name() == name); 0291 0292 shape.setImage(img); 0293 QVERIFY(shape.image() == img); 0294 0295 QVERIFY(shape.getType() == CharacterField::Shape); 0296 0297 QVERIFY(shape.getData(0, Qt::DisplayRole) == name); 0298 QVERIFY(shape.getData(1, Qt::DisplayRole) == uri); 0299 0300 // QVERIFY(shape.getData(0,Qt::DecorationRole) == QPixmap::fromImage(img).scaled(64,64,Qt::KeepAspectRatio)); 0301 0302 shape.setData(0, name2, Qt::DisplayRole); 0303 shape.setData(1, uri2, Qt::DisplayRole); 0304 0305 QVERIFY(shape.name() == name2); 0306 QVERIFY(shape.uri() == uri2); 0307 } 0308 void DataCharacterTest::testCharacterProperty() 0309 { 0310 CharacterProperty property; 0311 0312 QString value("4"); 0313 QString name("name"); 0314 QString name2("name2"); 0315 QString value2("5"); 0316 0317 property.setValue(value); 0318 QVERIFY(property.value() == value); 0319 0320 property.setName(name); 0321 QVERIFY(property.name() == name); 0322 0323 QVERIFY(property.getType() == CharacterField::Property); 0324 0325 QVERIFY(property.getData(0, Qt::DisplayRole) == name); 0326 QVERIFY(property.getData(1, Qt::DisplayRole) == value); 0327 QVERIFY(!property.getData(1, Qt::DecorationRole).isValid()); 0328 0329 property.setData(0, name2, Qt::DisplayRole); 0330 property.setData(1, value2, Qt::DisplayRole); 0331 0332 QVERIFY(property.name() == name2); 0333 QVERIFY(property.value() == value2); 0334 } 0335 0336 void DataCharacterTest::propertiesTest() 0337 { 0338 { 0339 CharacterShape shape; 0340 auto movie = Helper::randomMovie(); 0341 shape.setMovie(movie); 0342 0343 QCOMPARE(shape.movie().fileName(), movie.fileName()); 0344 0345 auto uri = Helper::randomString(); 0346 shape.setUri(uri); 0347 shape.setUri(uri); 0348 0349 CharacterShape shape2; 0350 shape2.setUri(Helper::imagePath()); 0351 QVERIFY(!shape2.getData(0, Qt::DecorationRole).value<QImage>().isNull()); 0352 0353 QVERIFY(shape2.getData(0, Qt::BackgroundRole).isNull()); 0354 0355 shape2.setSize(10); 0356 shape2.setSize(10); 0357 shape2.setSize(12); 0358 } 0359 } 0360 0361 QTEST_MAIN(DataCharacterTest); 0362 0363 #include "tst_datacharactertest.moc"