1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/**
* SPDX-FileCopyrightText: 2021 by Alexander Stippich <a.stippich@gmx.net>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include <memory>
#include <QPageSize>
#include <QSignalSpy>
#include <QTemporaryFile>
#include <QTest>
#include <QUrl>
#include "../src/DocumentSaver.h"
#include "../src/OCREngine.h"
#include "../src/SkanpageUtils.h"
class DocumentSaverTest : public QObject
{
Q_OBJECT
SkanpageUtils::DocumentPages testDocument;
SkanpageUtils::DocumentPages testDocumentOCRSerif;
SkanpageUtils::DocumentPages testDocumentOCRSans;
private Q_SLOTS:
void initTestCase();
void testPDFWriter();
void testPDFOCRWriter();
};
void DocumentSaverTest::initTestCase()
{
QImage image(QFINDTESTDATA("images/test150.jpg"));
QPageSize pageSize = QPageSize(QPageSize::A4);
int rotation = 0;
for (int i = 0; i < 5; i++) {
QTemporaryFile *tempImageFile = new QTemporaryFile();
tempImageFile->open();
image.save(tempImageFile, "PNG");
tempImageFile->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile), pageSize, 150, rotation});
rotation += 90;
if (rotation >= 360) {
rotation = 0;
}
}
QTemporaryFile *tempImageFile = new QTemporaryFile();
tempImageFile->open();
image.save(tempImageFile, "PNG");
tempImageFile->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile), pageSize, 150, 90});
QTemporaryFile *tempImageFile2 = new QTemporaryFile();
tempImageFile2->open();
image.save(tempImageFile2, "PNG");
tempImageFile2->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile2), pageSize, 150, 90});
QTemporaryFile *tempImageFile3 = new QTemporaryFile();
tempImageFile3->open();
image.save(tempImageFile3, "PNG");
tempImageFile3->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile3), pageSize, 150, 270});
QTemporaryFile *tempImageFile4 = new QTemporaryFile();
tempImageFile4->open();
image.save(tempImageFile4, "PNG");
tempImageFile4->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile4), pageSize, 150, 180});
QTemporaryFile *tempImageFile5 = new QTemporaryFile();
tempImageFile5->open();
image.save(tempImageFile5, "PNG");
tempImageFile5->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile5), pageSize, 150, 180});
QTemporaryFile *tempImageFile6 = new QTemporaryFile();
tempImageFile6->open();
image.save(tempImageFile6, "PNG");
tempImageFile6->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile6), pageSize, 150, 0});
QTemporaryFile *tempImageFile7 = new QTemporaryFile();
tempImageFile7->open();
image.save(tempImageFile7, "PNG");
tempImageFile7->close();
testDocument.append({std::shared_ptr<QTemporaryFile>(tempImageFile7), pageSize, 150, 0});
QImage imageOCRSerif(QFINDTESTDATA("images/testOCRSerif150.jpg"));
QImage imageOCRSerifFlipped(QFINDTESTDATA("images/testOCRSerif150Flipped.jpg"));
QImage imageOCRSerifRight(QFINDTESTDATA("images/testOCRSerif150Right.jpg"));
QImage imageOCRSerifLeft(QFINDTESTDATA("images/testOCRSerif150Left.jpg"));
rotation = 0;
for (int i = 0; i < 5; i++) {
QTemporaryFile *tempImageFile8 = new QTemporaryFile();
tempImageFile8->open();
imageOCRSerif.save(tempImageFile8, "PNG");
tempImageFile8->close();
testDocumentOCRSerif.append({std::shared_ptr<QTemporaryFile>(tempImageFile8), pageSize, 150, rotation});
rotation += 90;
if (rotation >= 360) {
rotation = 0;
}
}
for (int i = 0; i < 5; i++) {
QTemporaryFile *tempImageFile9 = new QTemporaryFile();
tempImageFile9->open();
imageOCRSerifFlipped.save(tempImageFile9, "PNG");
tempImageFile9->close();
testDocumentOCRSerif.append({std::shared_ptr<QTemporaryFile>(tempImageFile9), pageSize, 150, rotation});
rotation += 90;
if (rotation >= 360) {
rotation = 0;
}
}
QSizeF pageSizeMM = pageSize.size(QPageSize::Millimeter);
QPageSize pageSizeLandscape = QPageSize(QSizeF(pageSizeMM.height(), pageSizeMM.width()), QPageSize::Millimeter);
QTemporaryFile *tempImageFileRight = new QTemporaryFile();
tempImageFileRight->open();
imageOCRSerifRight.save(tempImageFileRight, "PNG");
tempImageFileRight->close();
testDocumentOCRSerif.append({std::shared_ptr<QTemporaryFile>(tempImageFileRight), pageSizeLandscape, 150, 90});
QTemporaryFile *tempImageFileLeft = new QTemporaryFile();
tempImageFileLeft->open();
imageOCRSerifLeft.save(tempImageFileLeft, "PNG");
tempImageFileLeft->close();
testDocumentOCRSerif.append({std::shared_ptr<QTemporaryFile>(tempImageFileLeft), pageSizeLandscape, 150, 270});
QImage imageOCRSans(QFINDTESTDATA("images/testOCRSans150.jpg"));
rotation = 0;
for (int i = 0; i < 5; i++) {
QTemporaryFile *tempImageFile10 = new QTemporaryFile();
tempImageFile10->open();
imageOCRSans.save(tempImageFile10, "PNG");
tempImageFile10->close();
testDocumentOCRSans.append({std::shared_ptr<QTemporaryFile>(tempImageFile10), pageSize, 150, rotation});
rotation += 90;
if (rotation >= 360) {
rotation = 0;
}
}
}
void DocumentSaverTest::testPDFWriter()
{
DocumentSaver tempSaver;
QSignalSpy spy(&tempSaver, &DocumentSaver::fileSaved);
tempSaver.saveDocument(QUrl::fromLocalFile(QStringLiteral("temp.pdf")), testDocument, SkanpageUtils::EntireDocument, QString());
spy.wait();
}
void DocumentSaverTest::testPDFOCRWriter()
{
DocumentSaver tempSaver;
OCREngine ocrEngine;
ocrEngine.setColor(Qt::red);
tempSaver.setOCREngine(&ocrEngine);
QSignalSpy spy(&tempSaver, &DocumentSaver::fileSaved);
tempSaver.saveDocument(QUrl::fromLocalFile(QStringLiteral("tempOCRSerif.pdf")), testDocumentOCRSerif, SkanpageUtils::OCRDocument, QString());
spy.wait();
tempSaver.saveDocument(QUrl::fromLocalFile(QStringLiteral("tempOCRSans.pdf")), testDocumentOCRSans, SkanpageUtils::OCRDocument, QString());
spy.wait();
}
QTEST_MAIN(DocumentSaverTest)
#include "documentSaverTest.moc"
|