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

0001 /***************************************************************************
0002  *   Copyright (C) 2022 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 <QSignalSpy>
0023 #include <QUrl>
0024 #include <QClipboard>
0025 
0026 #include "controller/view_controller/pdfcontroller.h"
0027 #include "test_root_path.h"
0028 #include "utils/iohelper.h"
0029 #include <memory>
0030 
0031 class PdfControllorTest : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     PdfControllorTest();
0037 
0038 private slots:
0039     void init();
0040     void cleanupTestCase();
0041 
0042     void create();
0043     void create_data();
0044 
0045     void zoomTest();
0046     void zoomTest_data();
0047 
0048     void shareAsPdf();
0049     void copyImage();
0050 
0051 private:
0052     std::unique_ptr<PdfController> m_ctrl;
0053 };
0054 
0055 PdfControllorTest::PdfControllorTest() {}
0056 
0057 void PdfControllorTest::init()
0058 {
0059     m_ctrl.reset(new PdfController());
0060 }
0061 
0062 void PdfControllorTest::cleanupTestCase() {}
0063 
0064 void PdfControllorTest::create()
0065 {
0066     QFETCH(QString, path);
0067     QFETCH(QByteArray, data);
0068     QFETCH(QByteArray, expected);
0069 
0070     QString id("idtest");
0071 
0072     m_ctrl.reset(new PdfController(id, QUrl::fromUserInput(path), data));
0073 
0074     QCOMPARE(m_ctrl->data(), expected);
0075 
0076     m_ctrl->setData(m_ctrl->data());
0077     m_ctrl->buffer();
0078 }
0079 
0080 void PdfControllorTest::create_data()
0081 {
0082     QTest::addColumn<QString>("path");
0083     QTest::addColumn<QByteArray>("data");
0084     QTest::addColumn<QByteArray>("expected");
0085 
0086     QString path("%1/resources/menu_restaurant.pdf");
0087     path = path.arg(tests::root_path);
0088 
0089     auto data = utils::IOHelper::loadFile(path);
0090 
0091     QTest::addRow("with path") << path << QByteArray() << data;
0092     QTest::addRow("with data") << QString() << data << data;
0093     QTest::addRow("with both") << path << data << data;
0094 }
0095 void PdfControllorTest::zoomTest()
0096 {
0097     QFETCH(QStringList,actions);
0098     QFETCH(qreal, expected);
0099     QFETCH(int, expectedCount);
0100 
0101     QSignalSpy spy(m_ctrl.get(), &PdfController::zoomFactorChanged);
0102 
0103     for(const auto &action : actions)
0104     {
0105         if(action == "+")
0106         {
0107             m_ctrl->zoomIn();
0108         }
0109         else if(action == "-")
0110         {
0111             m_ctrl->zoomOut();
0112         }
0113         else {
0114             m_ctrl->setZoomFactor(action.toDouble());
0115         }
0116     }
0117 
0118     spy.wait(20);
0119     QCOMPARE(spy.count(), expectedCount);
0120     QCOMPARE(m_ctrl->zoomFactor(), expected);
0121 }
0122 void PdfControllorTest::zoomTest_data()
0123 {
0124     QTest::addColumn<QStringList>("actions");
0125     QTest::addColumn<qreal>("expected");
0126     QTest::addColumn<int>("expectedCount");
0127 
0128     QTest::addRow("zoomin") << QStringList{"+","+","+","+","+"} << (1.1) << 5;
0129     QTest::addRow("zoomout") << QStringList{"-","-","-","-","-"} << (0.9) << 5;
0130     QTest::addRow("zommInAndOut") << QStringList{"-","-","+","+","1.5","1.5"} << (1.5) << 5;
0131 }
0132 
0133 void PdfControllorTest::shareAsPdf()
0134 {
0135     QSignalSpy spy(m_ctrl.get(), &PdfController::sharePdf);
0136 
0137     m_ctrl->shareAsPdf();
0138 
0139     QCOMPARE(spy.count(),0);
0140 
0141     QString path("%1/resources/menu_restaurant.pdf");
0142     path = path.arg(tests::root_path);
0143     m_ctrl->setData(utils::IOHelper::loadFile(path));
0144 
0145     m_ctrl->shareAsPdf();
0146 
0147     QCOMPARE(spy.count(),1);
0148 
0149 }
0150 void PdfControllorTest::copyImage()
0151 {
0152     m_ctrl->copyImage(QString("%1/resources/img/predateur.jpg").arg(tests::root_path));
0153 
0154     auto clip= QApplication::clipboard();
0155     QVERIFY(!clip->pixmap().isNull());
0156 }
0157 
0158 QTEST_MAIN(PdfControllorTest);
0159 
0160 #include "tst_pdfcontrollertest.moc"