File indexing completed on 2024-05-12 05:40:47
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 0021 #include <QJsonObject> 0022 #include <QModelIndex> 0023 #include <QModelIndexList> 0024 #include <QTemporaryFile> 0025 #include <QtCore/QString> 0026 #include <QtTest/QtTest> 0027 #include <memory> 0028 0029 #include "data/character.h" 0030 #include "model/characterstatemodel.h" 0031 #include "network/networkmessagereader.h" 0032 #include "network/networkmessagewriter.h" 0033 #include "worker/fileserializer.h" 0034 #include "worker/iohelper.h" 0035 #include "worker/messagehelper.h" 0036 #include "worker/modelhelper.h" 0037 #include <QAbstractItemModelTester> 0038 0039 class TestCharacterStateModel : public QObject 0040 { 0041 Q_OBJECT 0042 public: 0043 enum Action 0044 { 0045 Top, 0046 Bottom, 0047 Up, 0048 Down 0049 }; 0050 TestCharacterStateModel(); 0051 0052 void addDefaultState(); 0053 private slots: 0054 void init(); 0055 0056 void addTest(); 0057 void addTest_data(); 0058 0059 void moveTest(); 0060 void moveTest_data(); 0061 void removeTest(); 0062 0063 void saveModelTest(); 0064 0065 void networkTest(); 0066 0067 private: 0068 std::unique_ptr<CharacterStateModel> m_model; 0069 }; 0070 0071 TestCharacterStateModel::TestCharacterStateModel() {} 0072 0073 void TestCharacterStateModel::init() 0074 { 0075 m_model.reset(new CharacterStateModel()); 0076 new QAbstractItemModelTester(m_model.get()); 0077 } 0078 0079 void TestCharacterStateModel::addDefaultState() 0080 { 0081 CharacterState state; 0082 state.setColor(Qt::black); 0083 state.setLabel(tr("Healthy")); 0084 0085 CharacterState state2; 0086 state2.setColor(QColor(255, 100, 100)); 0087 state2.setLabel(tr("Lightly Wounded")); 0088 0089 CharacterState state3; 0090 state3.setColor(QColor(255, 0, 0)); 0091 state3.setLabel(tr("Seriously injured")); 0092 0093 CharacterState state4; 0094 state4.setColor(Qt::gray); 0095 state4.setLabel(tr("Dead")); 0096 0097 CharacterState state5; 0098 state5.setColor(QColor(80, 80, 255)); 0099 state5.setLabel(tr("Sleeping")); 0100 0101 CharacterState state6; 0102 state6.setColor(QColor(0, 200, 0)); 0103 state6.setLabel(tr("Bewitched")); 0104 0105 m_model->appendState(std::move(state)); 0106 m_model->appendState(std::move(state2)); 0107 m_model->appendState(std::move(state3)); 0108 m_model->appendState(std::move(state4)); 0109 m_model->appendState(std::move(state5)); 0110 m_model->appendState(std::move(state6)); 0111 } 0112 0113 void TestCharacterStateModel::addTest() 0114 { 0115 QFETCH(QString, label); 0116 QFETCH(QColor, color); 0117 QFETCH(QString, imgPath); 0118 0119 QVERIFY(m_model->rowCount() == 0); 0120 0121 CharacterState state; 0122 state.setColor(color); 0123 state.setImagePath(imgPath); 0124 state.setLabel(label); 0125 m_model->appendState(std::move(state)); 0126 0127 QCOMPARE(m_model->rowCount(), 1); 0128 } 0129 0130 void TestCharacterStateModel::addTest_data() 0131 { 0132 QTest::addColumn<QString>("label"); 0133 QTest::addColumn<QColor>("color"); 0134 QTest::addColumn<QString>("imgPath"); 0135 0136 QTest::addRow("state1") << "" << QColor() << ""; 0137 QTest::addRow("state2") << "girafe" << QColor(Qt::red) << ":/assets/img/girafe.jpg"; 0138 QTest::addRow("state3") << "lion" << QColor(Qt::yellow) << ":/assets/img/lion.jpg"; 0139 QTest::addRow("state4") << "blue" << QColor(Qt::blue) << ""; 0140 } 0141 0142 void TestCharacterStateModel::removeTest() 0143 { 0144 addDefaultState(); 0145 QCOMPARE(m_model->rowCount(), 6); 0146 0147 m_model->clear(); 0148 QCOMPARE(m_model->rowCount(), 0); 0149 0150 addDefaultState(); 0151 QCOMPARE(m_model->rowCount(), 6); 0152 0153 QModelIndexList indexList; 0154 auto index= m_model->index(0, 0); 0155 indexList << index; 0156 m_model->deleteState(index); 0157 QCOMPARE(m_model->rowCount(), 5); 0158 0159 m_model->clear(); 0160 QCOMPARE(m_model->rowCount(), 0); 0161 0162 addDefaultState(); 0163 QCOMPARE(m_model->rowCount(), 6); 0164 0165 index= m_model->index(0, 0); 0166 auto index1= m_model->index(1, 0); 0167 m_model->deleteState(index1); 0168 m_model->deleteState(index); 0169 QCOMPARE(m_model->rowCount(), 4); 0170 } 0171 0172 void TestCharacterStateModel::moveTest() 0173 { 0174 QFETCH(int, source); 0175 QFETCH(int, action); 0176 QFETCH(int, destination); 0177 QFETCH(QString, text); 0178 0179 addDefaultState(); 0180 QCOMPARE(m_model->rowCount(), 6); 0181 0182 auto index= m_model->index(source, 0); 0183 switch(static_cast<Action>(action)) 0184 { 0185 case Up: 0186 m_model->upState(index); 0187 break; 0188 case Down: 0189 m_model->downState(index); 0190 break; 0191 case Top: 0192 m_model->topState(index); 0193 break; 0194 case Bottom: 0195 m_model->bottomState(index); 0196 break; 0197 } 0198 auto const& listState= m_model->statesList(); 0199 QCOMPARE(listState.at(destination)->label(), text); 0200 } 0201 0202 void TestCharacterStateModel::moveTest_data() 0203 { 0204 QTest::addColumn<int>("source"); 0205 QTest::addColumn<int>("action"); 0206 QTest::addColumn<int>("destination"); 0207 QTest::addColumn<QString>("text"); 0208 0209 QTest::newRow("test1") << 1 << static_cast<int>(Up) << 0 << QString("Lightly Wounded"); 0210 QTest::newRow("test2") << 2 << static_cast<int>(Down) << 3 << QString("Seriously injured"); 0211 QTest::newRow("test3") << 5 << static_cast<int>(Top) << 0 << QString("Bewitched"); 0212 QTest::newRow("test4") << 0 << static_cast<int>(Bottom) << 5 << QString("Healthy"); 0213 } 0214 0215 void TestCharacterStateModel::saveModelTest() 0216 { 0217 addDefaultState(); 0218 QCOMPARE(m_model->rowCount(), 6); 0219 0220 QTemporaryFile file; 0221 QVERIFY(file.open()); 0222 0223 auto array= campaign::FileSerializer::statesToArray(m_model->statesList(), file.fileName()); 0224 0225 // campaign::FileSerializer::writeStatesIntoCampaign(file.fileName(), ); 0226 0227 CharacterStateModel model; 0228 ModelHelper::fetchCharacterStateModel(array, &model); 0229 0230 QCOMPARE(model.rowCount(), 6); 0231 auto const& list= model.statesList(); 0232 QCOMPARE(list.at(0)->label(), "Healthy"); 0233 QCOMPARE(list.at(5)->label(), "Bewitched"); 0234 } 0235 0236 void TestCharacterStateModel::networkTest() 0237 { 0238 addDefaultState(); 0239 QCOMPARE(m_model->rowCount(), 6); 0240 0241 NetworkMessageWriter msg(NetMsg::CampaignCategory, NetMsg::DiceAliasModel); 0242 CharacterState state; 0243 state.setLabel("TestState"); 0244 state.setColor(Qt::red); 0245 msg.uint32(1); 0246 0247 msg.uint64(0); 0248 msg.string32(state.label()); 0249 msg.rgb(state.color().rgb()); 0250 msg.pixmap(state.pixmap()); 0251 auto data= msg.data(); 0252 NetworkMessageReader msgReader; 0253 msgReader.setData(data); 0254 0255 MessageHelper::fetchCharacterStatesFromNetwork(&msgReader, m_model.get()); 0256 0257 // m_model->processAddState(&msgReader); 0258 QCOMPARE(m_model->rowCount(), 1); 0259 0260 // m_model->setGM(false); 0261 auto list= Character::getCharacterStateList(); 0262 if(list == nullptr) 0263 return; 0264 QCOMPARE(list->size(), 1); 0265 } 0266 0267 QTEST_MAIN(TestCharacterStateModel); 0268 0269 #include "tst_characterstatemodel.moc"