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

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 #include <QtTest/QtTest>
0021 
0022 #include <QImage>
0023 #include <QPainter>
0024 #include <QRect>
0025 #include <QString>
0026 #include <memory>
0027 
0028 #include "utils/countdownobject.h"
0029 #include "utils/iohelper.h"
0030 #include "utils/mappinghelper.h"
0031 #include "worker/iohelper.h"
0032 #include "worker/utilshelper.h"
0033 #include "worker/convertionhelper.h"
0034 #include "mindmap/data/mindmaptypes.h"
0035 #include <helper.h>
0036 
0037 class WorkerTest : public QObject
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     enum class Type {
0043         Integer,
0044         PointF,
0045         Real,
0046         Bool,
0047         ScaleUnit,
0048         PermissionMode,
0049         GridPattern,
0050         Layer,
0051         VisibilityMode,
0052         Color,
0053         Image,
0054         RectF,
0055         uint16,
0056         ByteArray,
0057         Font,
0058         VecPoint,
0059         ShapeVision,
0060         Size,
0061         PermissionParticipiant,
0062         ArrowDirection
0063     };
0064     Q_ENUM(Type)
0065     WorkerTest();
0066 
0067 private slots:
0068     void init();
0069     void cleanupTestCase();
0070     void helperUtilsTest();
0071     void helperUtilsTest_data();
0072 
0073     void imageIsSquare();
0074     void imageIsSquare_data();
0075 
0076     void mappingHelperTest();
0077     void iohelperTest();
0078 
0079     void countDownTest();
0080 
0081     void convertionHelperTest();
0082     void convertionHelperTest_data();
0083 };
0084 
0085 WorkerTest::WorkerTest() {}
0086 
0087 void WorkerTest::init() {}
0088 
0089 void WorkerTest::cleanupTestCase() {}
0090 
0091 void WorkerTest::helperUtilsTest()
0092 {
0093     QFETCH(QRect, rect);
0094     QFETCH(qreal, ratio);
0095 
0096     auto newRect= helper::utils::computerBiggerRectInside(rect, ratio);
0097 
0098     QVERIFY(newRect.width() <= rect.width());
0099     QVERIFY(newRect.height() <= rect.height());
0100 
0101     QVERIFY(qFuzzyCompare(static_cast<qreal>(newRect.width()) / newRect.height(), ratio));
0102 }
0103 
0104 void WorkerTest::helperUtilsTest_data()
0105 {
0106     QTest::addColumn<QRect>("rect");
0107     QTest::addColumn<qreal>("ratio");
0108 
0109     QVector<qreal> ratios({1., 16. / 9., 4. / 3.});
0110     long long int idx= 0;
0111 
0112     constexpr int k_width= 100;  // 1920
0113     constexpr int k_height= 100; // 1080
0114 
0115     // QTest::addRow("%s", QString("cmd %1").arg(idx++).toStdString().c_str()) << QRect(0, 0, 16, 10) << ratios[2];
0116 
0117     for(int i= 10; i <= k_width; ++i)
0118     {
0119         for(int j= 10; j <= k_height; ++j)
0120         {
0121             for(auto r : qAsConst(ratios))
0122             {
0123                 QTest::addRow("%s", QString("cmd %1").arg(idx++).toStdString().c_str()) << QRect(0, 0, i, j) << r;
0124             }
0125         }
0126     }
0127 }
0128 
0129 void WorkerTest::imageIsSquare()
0130 {
0131     QFETCH(QByteArray, image);
0132     QFETCH(bool, expected);
0133     QCOMPARE(helper::utils::isSquareImage(image), expected);
0134 }
0135 void WorkerTest::imageIsSquare_data()
0136 {
0137     QTest::addColumn<QByteArray>("image");
0138     QTest::addColumn<bool>("expected");
0139 
0140     QTest::addRow("cmd1") << IOHelper::imageToData(IOHelper::readImageFromFile(":/img/arbre_500.jpg")) << false;
0141     QTest::addRow("cmd2") << IOHelper::imageToData(IOHelper::readImageFromFile(":/img/arbre_square_500.jpg")) << true;
0142 }
0143 
0144 void WorkerTest::iohelperTest()
0145 {
0146     QTemporaryDir dir;
0147 
0148     if(dir.isValid())
0149     {
0150         utils::IOHelper::makeDir(dir.path());
0151         utils::IOHelper::copyFile(":/img/arbre_500.jpg", dir.path());
0152         utils::IOHelper::removeFile(QString("%1/arbre_500.jpg").arg(dir.path()));
0153         auto src= utils::IOHelper::copyFile(":/img/arbre_500.jpg", dir.path());
0154         utils::IOHelper::readPixmapFromURL(QString("%1/arbre_500.jpg").arg(dir.path()));
0155         utils::IOHelper::shortNameFromPath(QString("%1/arbre_500.jpg").arg(dir.path()));
0156         auto data= utils::IOHelper::readTextFile(":/sharednotes/scenario.md");
0157         QVERIFY(!data.isEmpty());
0158         /*QVERIFY(!utils::IOHelper::moveFile(QStringLiteral("%1/%2").arg(QDir::rootPath(), Helper::randomString()),
0159                                            QStringLiteral("%1/%2").arg(QDir::rootPath(), Helper::randomString())));*/
0160 
0161         QVERIFY(utils::IOHelper::moveFile(QStringLiteral("%1/arbre_500.jpg").arg(dir.path()),
0162                                           QStringLiteral("%1/image_test.jpg").arg(dir.path())));
0163 
0164         QVERIFY(!utils::IOHelper::writeFile(QString("%1/%2").arg(QDir::rootPath(), Helper::randomString()),
0165                                             Helper::imageData()));
0166     }
0167 }
0168 
0169 void WorkerTest::mappingHelperTest()
0170 {
0171     utils::MappingHelper helper;
0172 
0173     auto res= helper.mapSizeTo(QSizeF(100., 100.), QSizeF(400., 200.));
0174     QCOMPARE(res, QSizeF(100., 50.));
0175 
0176     auto max= helper.maxRect(QRectF(0., 0., 500., 500.), QRectF(1000., 400., 10., 10.));
0177     QCOMPARE(max, QRectF(0, 0, 1010., 500.));
0178 
0179     helper.mapRectInto(QRectF(0., 0., 500., 500.), QRectF(0., 0., 500., 500.), QRectF(0., 0., 500., 500.));
0180 }
0181 
0182 void WorkerTest::countDownTest()
0183 {
0184     auto tryCount= Helper::generate<int>(1, 5);
0185     auto intervalCount= Helper::generate<int>(1, 10);
0186 
0187     CountDownObject obj(tryCount, intervalCount, 100);//100 ms
0188 
0189     QSignalSpy spy(&obj, &CountDownObject::triggered);
0190     QSignalSpy spy2(&obj, &CountDownObject::countDownChanged);
0191 
0192     QVERIFY(!obj.isRunning());
0193 
0194     obj.start();
0195     QVERIFY(obj.isRunning());
0196 
0197     auto c = tryCount * intervalCount;
0198 
0199     QTest::qWait((c+5)*100);
0200 
0201     QCOMPARE(spy2.count(), c);
0202     QCOMPARE(spy.count(), tryCount+1);
0203     QVERIFY(!obj.isRunning());
0204 }
0205 
0206 void WorkerTest::convertionHelperTest()
0207 {
0208     QFETCH(QVariant, data);
0209     QFETCH(ulong, size);
0210     QFETCH(Type, type);
0211 
0212     NetworkMessageWriter writer(NetMsg::InstantMessageCategory, NetMsg::AddMessage);
0213 
0214     QCOMPARE(writer.currentPos(), 9);
0215 
0216     auto realSize = 9+size;
0217 
0218     switch(type)
0219     {
0220 
0221     case Type::Integer:
0222         Helper::variantToType<int>(data.value<int>(), writer);
0223         break;
0224     case Type::PointF:
0225         Helper::variantToType<QPointF>(data.value<QPointF>(), writer);
0226         break;
0227     case Type::Real:
0228         Helper::variantToType<qreal>(data.value<qreal>(), writer);
0229         break;
0230     case Type::Bool:
0231         Helper::variantToType<bool>(data.value<bool>(), writer);
0232         break;
0233     case Type::ScaleUnit:
0234         Helper::variantToType<Core::ScaleUnit>(data.value<Core::ScaleUnit>(), writer);
0235         break;
0236     case Type::PermissionMode:
0237         Helper::variantToType<Core::PermissionMode>(data.value<Core::PermissionMode>(), writer);
0238         break;
0239     case Type::GridPattern:
0240         Helper::variantToType<Core::GridPattern>(data.value<Core::GridPattern>(), writer);
0241         break;
0242     case Type::Layer:
0243         Helper::variantToType<Core::Layer>(data.value<Core::Layer>(), writer);
0244         break;
0245     case Type::VisibilityMode:
0246         Helper::variantToType<Core::VisibilityMode>(data.value<Core::VisibilityMode>(), writer);
0247         break;
0248     case Type::Color:
0249         Helper::variantToType<QColor>(data.value<QColor>(), writer);
0250         break;
0251     case Type::Image:
0252         Helper::variantToType<QImage>(data.value<QImage>(), writer);
0253         break;
0254     case Type::RectF:
0255         Helper::variantToType<QRectF>(data.value<QRectF>(), writer);
0256         break;
0257     case Type::uint16:
0258         Helper::variantToType<quint16>(data.value<quint16>(), writer);
0259         break;
0260     case Type::ByteArray:
0261         Helper::variantToType<QByteArray>(data.value<QByteArray>(), writer);
0262         break;
0263     case Type::Font:
0264         Helper::variantToType<QFont>(data.value<QFont>(), writer);
0265         break;
0266     case Type::VecPoint:
0267         Helper::variantToType<std::vector<QPointF>>(data.value<std::vector<QPointF>>(), writer);
0268         break;
0269     case Type::ShapeVision:
0270         Helper::variantToType<CharacterVision::SHAPE>(data.value<CharacterVision::SHAPE>(), writer);
0271         break;
0272     case Type::Size:
0273         Helper::variantToType<QSize>(data.value<QSize>(), writer);
0274         break;
0275     case Type::PermissionParticipiant:
0276         Helper::variantToType<ParticipantModel::Permission>(data.value<ParticipantModel::Permission>(), writer);
0277         break;
0278     case Type::ArrowDirection:
0279         Helper::variantToType<mindmap::ArrowDirection>(data.value<mindmap::ArrowDirection>(), writer);
0280         break;
0281     }
0282     QCOMPARE(writer.currentPos(), realSize);
0283 }
0284 
0285 
0286 void WorkerTest::convertionHelperTest_data()
0287 {
0288     QTest::addColumn<QVariant>("data");
0289     QTest::addColumn<ulong>("size");
0290     QTest::addColumn<Type>("type");
0291 
0292 
0293 
0294     QTest::addRow("cmd1") << QVariant::fromValue(true) << sizeof(bool) << Type::Bool;
0295     QTest::addRow("cmd2") << QVariant::fromValue(static_cast<qint64>(23)) << sizeof(qint64) << Type::Integer;
0296     QTest::addRow("cmd3") << QVariant::fromValue(23.0) << sizeof(qreal)<< Type::Real;
0297     QTest::addRow("cmd4") << QVariant::fromValue(Core::ScaleUnit::FEET) << sizeof(quint8)<< Type::ScaleUnit;
0298     QTest::addRow("cmd5") << QVariant::fromValue(Core::PermissionMode::GM_ONLY) << sizeof(quint8)<< Type::PermissionMode;
0299     QTest::addRow("cmd6") << QVariant::fromValue(Core::GridPattern::HEXAGON) << sizeof(Core::GridPattern)<< Type::GridPattern;
0300     QTest::addRow("cmd7") << QVariant::fromValue(Core::Layer::GRIDLAYER) << sizeof(Core::Layer)<< Type::Layer;
0301     QTest::addRow("cmd8") << QVariant::fromValue(Core::VisibilityMode::FOGOFWAR) << sizeof(quint8)<< Type::VisibilityMode;
0302     QTest::addRow("cmd9") << QVariant::fromValue(QColor(Qt::blue)) << sizeof(unsigned int)<< Type::Color;
0303     QImage img = QImage::fromData(Helper::imageData());
0304 
0305     QByteArray data2;
0306     QDataStream in(&data2, QIODevice::WriteOnly);
0307     in.setVersion(QDataStream::Qt_5_7);
0308     in << img;
0309 
0310 
0311     QTest::addRow("cmd10") << QVariant::fromValue(img) << static_cast<ulong>(sizeof(quint32)+data2.size()) << Type::Image;
0312     QTest::addRow("cmd11") << QVariant::fromValue(QPointF{10.0,100.0}) << sizeof(qreal)+sizeof(qreal) << Type::PointF;
0313     QTest::addRow("cmd12") << QVariant::fromValue(QRectF{10.0,100.0,100.,100.}) << 4*sizeof(qreal)<< Type::RectF;
0314 
0315     QTest::addRow("cmd13") << QVariant::fromValue(static_cast<quint16>(250)) << sizeof(quint16)<< Type::uint16;
0316     auto data = Helper::randomData();
0317     QTest::addRow("cmd14") << QVariant::fromValue(data) << static_cast<ulong>(sizeof(quint32)+data.size())<< Type::ByteArray;
0318     QFont font;
0319     QTest::addRow("cmd15") << QVariant::fromValue(font) << static_cast<ulong>(sizeof(quint16)+static_cast<quint64>(font.toString().size()) * static_cast<quint64>(sizeof(QChar))) << Type::Font;
0320 
0321     std::vector<QPointF> vec{{100.0,100.0},{500.0, 500.0}};
0322     QTest::addRow("cmd16") << QVariant::fromValue(vec) << static_cast<ulong>(sizeof(quint64)+sizeof(qreal)*4)<< Type::VecPoint;
0323 
0324 
0325     QTest::addRow("cmd17") << QVariant::fromValue(CharacterVision::SHAPE::ANGLE) << sizeof(quint8)<< Type::ShapeVision;
0326     QTest::addRow("cmd18") << QVariant::fromValue(QSize(100,100)) << sizeof(QSize)<< Type::Size;
0327     QTest::addRow("cmd19") << QVariant::fromValue(ParticipantModel::Permission::hidden) << sizeof(quint8)<< Type::PermissionParticipiant;
0328     QTest::addRow("cmd20") << QVariant::fromValue(mindmap::ArrowDirection::EndToStart) << sizeof(mindmap::ArrowDirection)<< Type::ArrowDirection;
0329 
0330 
0331 
0332 }
0333 
0334 QTEST_MAIN(WorkerTest);
0335 
0336 #include "tst_worker.moc"