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 <QAbstractItemModelTester> 0022 #include <QModelIndex> 0023 #include <QModelIndexList> 0024 #include <QtCore/QString> 0025 #include <QtTest/QtTest> 0026 #include <memory> 0027 0028 #include "network/channel.h" 0029 #include "network/channelmodel.h" 0030 #include "network/serverconnection.h" 0031 #include "worker/networkhelper.h" 0032 0033 class TestChannelModel : public QObject 0034 { 0035 Q_OBJECT 0036 public: 0037 TestChannelModel(); 0038 0039 private slots: 0040 void init(); 0041 0042 void addTest(); 0043 void addTest_data(); 0044 0045 void removeTest(); 0046 void moveTest(); 0047 0048 void writeAndRead(); 0049 void writeAndRead_data(); 0050 0051 private: 0052 std::unique_ptr<ChannelModel> m_model; 0053 }; 0054 0055 TestChannelModel::TestChannelModel() {} 0056 0057 void TestChannelModel::init() 0058 { 0059 m_model.reset(new ChannelModel()); 0060 new QAbstractItemModelTester(m_model.get()); 0061 } 0062 0063 void TestChannelModel::addTest() 0064 { 0065 QFETCH(QStringList, channels); 0066 QFETCH(QByteArray, password); 0067 QFETCH(int, expected); 0068 0069 for(auto channel : channels) 0070 { 0071 m_model->addChannel(channel, password); 0072 } 0073 QCOMPARE(m_model->rowCount(QModelIndex()), expected); 0074 } 0075 0076 void TestChannelModel::addTest_data() 0077 { 0078 QTest::addColumn<QStringList>("channels"); 0079 QTest::addColumn<QByteArray>("password"); 0080 QTest::addColumn<int>("expected"); 0081 0082 QByteArray array("password"); 0083 QTest::addRow("list1") << QStringList() << array << 0; 0084 QTest::addRow("list2") << QStringList({"channel"}) << array << 1; 0085 QTest::addRow("list3") << QStringList({"channel1", "channel2"}) << array << 2; 0086 QTest::addRow("list3") << QStringList({"channel1", "channel2", "channel3"}) << array << 3; 0087 } 0088 0089 void TestChannelModel::removeTest() 0090 { 0091 m_model->addChannel("channel1", "password"); 0092 m_model->addChannel("channel2", "password"); 0093 QCOMPARE(m_model->rowCount(QModelIndex()), 2); 0094 0095 m_model->cleanUp(); 0096 QCOMPARE(m_model->rowCount(QModelIndex()), 0); 0097 0098 m_model->addChannel("channel1", "password"); 0099 auto id1= m_model->addChannel("channel2", "password"); 0100 QCOMPARE(m_model->rowCount(QModelIndex()), 2); 0101 0102 m_model->removeChild(id1); 0103 QCOMPARE(m_model->rowCount(QModelIndex()), 1); 0104 0105 QCOMPARE(m_model->getItemById(id1), nullptr); 0106 } 0107 0108 void TestChannelModel::moveTest() 0109 { 0110 auto idC= m_model->addChannel("channel1", {}); 0111 auto idC2= m_model->addChannel("channel2", {}); 0112 QCOMPARE(m_model->rowCount(QModelIndex()), 2); 0113 0114 ServerConnection client(nullptr, nullptr); 0115 client.setIsAdmin(true); 0116 auto id= client.uuid(); 0117 m_model->setLocalPlayerId(id); 0118 m_model->addConnectionToDefaultChannel(&client); 0119 auto channel= m_model->getItemById(idC); 0120 QCOMPARE(client.getParentChannel(), channel); 0121 0122 ClientMimeData data; 0123 auto parent= m_model->index(0, 0, QModelIndex()); 0124 auto clientIndex= m_model->index(0, 0, parent); 0125 0126 data.addClient(&client, clientIndex); 0127 auto b= m_model->dropMimeData(&data, Qt::MoveAction, 1, 1, m_model->index(1, 0, QModelIndex())); 0128 0129 QVERIFY(b); 0130 } 0131 0132 void TestChannelModel::writeAndRead() 0133 { 0134 QFETCH(QStringList, channels); 0135 QFETCH(QByteArray, password); 0136 QFETCH(int, expected); 0137 0138 for(const auto& channel : channels) 0139 { 0140 m_model->addChannel(channel, password); 0141 } 0142 QCOMPARE(m_model->rowCount(QModelIndex()), expected); 0143 0144 QJsonObject obj= helper::network::channelModelToJSonObject(m_model.get()); 0145 0146 ChannelModel model1; 0147 helper::network::fetchChannelModel(&model1, obj); 0148 0149 QCOMPARE(model1.rowCount(QModelIndex()), expected); 0150 } 0151 0152 void TestChannelModel::writeAndRead_data() 0153 { 0154 QTest::addColumn<QStringList>("channels"); 0155 QTest::addColumn<QByteArray>("password"); 0156 QTest::addColumn<int>("expected"); 0157 0158 QByteArray array("password"); 0159 QTest::addRow("list1") << QStringList() << array << 0; 0160 QTest::addRow("list2") << QStringList({"channel"}) << array << 1; 0161 QTest::addRow("list3") << QStringList({"channel1", "channel2"}) << array << 2; 0162 QTest::addRow("list3") << QStringList({"channel1", "channel2", "channel3"}) << array << 3; 0163 } 0164 QTEST_MAIN(TestChannelModel); 0165 0166 #include "tst_channelmodel.moc"