File indexing completed on 2024-05-05 04:43:09
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2012 Dag Andersen <danders@get2net.dk> 0003 * Copyright (C) 2015 Jarosław Staniek <staniek@kde.org> 0004 * 0005 * QFUZZYCOMPARE() from marble/tests/TestUtils.h: 0006 * Copyright (C) 2013 Dennis Nienhüser <earthwings@gentoo.org> 0007 * 0008 * This library is free software; you can redistribute it and/or 0009 * modify it under the terms of the GNU Lesser General Public 0010 * License as published by the Free Software Foundation; either 0011 * version 2.1 of the License, or (at your option) any later version. 0012 * 0013 * This library is distributed in the hope that it will be useful, 0014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 * Lesser General Public License for more details. 0017 * 0018 * You should have received a copy of the GNU Lesser General Public 0019 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0020 */ 0021 0022 #include "FormatTest.h" 0023 #include "KReportTestUtils.h" 0024 #include "KReportPreRenderer.h" 0025 #include "KReportDesigner.h" 0026 #include "KReportLabelElement.h" 0027 #include "KReportDocument.h" 0028 #include "KReportDesignerSectionDetail.h" 0029 #include "KReportSection.h" 0030 #include "KReportDesignerItemLine.h" 0031 #include "KReportItemLine.h" 0032 #include "KReportDesignerItemRectBase.h" 0033 #include "KReportUnit.h" 0034 #include <KReportDesign> 0035 0036 #include <QPointF> 0037 #include <QSizeF> 0038 #include <QDomDocument> 0039 #include <QScreen> 0040 #include <QDir> 0041 #include <QTest> 0042 #include <QPageLayout> 0043 0044 QTEST_MAIN(FormatTest) 0045 0046 static bool openDesignFile(KReportDesign *design, QString *errorMessage) 0047 { 0048 const QString dir(QFile::decodeName(FILES_DATA_DIR)); 0049 const QString fname(QLatin1String(QTest::currentTestFunction()) + QLatin1String(".kreport")); 0050 QFile file(dir + QDir::separator() + fname); 0051 bool ok = file.open(QFile::ReadOnly | QFile::Text); 0052 if (!ok) { 0053 *errorMessage = QString::fromLatin1("Could not open file %1: ").arg(file.fileName()) + file.errorString(); 0054 return false; 0055 } 0056 0057 QString content = file.readAll(); 0058 if (file.error() != QFileDevice::NoError) { 0059 *errorMessage = QString::fromLatin1("Error reading file %1: ").arg(file.fileName()) + file.errorString(); 0060 return false; 0061 } 0062 0063 KReportDesignReadingStatus status; 0064 if (!design->setContent(content, &status)) { 0065 QString message; 0066 QDebug(&message) << status; 0067 *errorMessage = QLatin1String("Failed to load content. ") + message; 0068 return false; 0069 } 0070 errorMessage->clear(); 0071 return true; 0072 } 0073 0074 void FormatTest::testPageLayout() 0075 { 0076 KReportDesign design; 0077 QString errorMessage; 0078 if (!openDesignFile(&design, &errorMessage)) { 0079 QFAIL(qPrintable(errorMessage)); 0080 } 0081 0082 const QPageLayout pageLayout = design.pageLayout(); 0083 QVERIFY(pageLayout.isValid()); 0084 QCOMPARE(pageLayout.pageSize().id(), QPageSize::A5); 0085 QCOMPARE(pageLayout.pageSize().sizePoints(), QPageSize(QPageSize::A5).sizePoints()); 0086 QCOMPARE(pageLayout.orientation(), QPageLayout::Portrait); 0087 QCOMPARE(pageLayout.margins(QPageLayout::Millimeter), QMarginsF(30.0, 20.0, 40.0, 15.0)); 0088 0089 //! @todo move this renderer test to a separate place 0090 #if 0 0091 KReportDesigner designer; 0092 QCOMPARE(designer.propertySet()->property("page-size").value().toString(), QLatin1String("A5")); 0093 QCOMPARE(designer.propertySet()->property("margin-bottom").value().toDouble(), KReportUnit::parseValue("1.5cm")); 0094 QCOMPARE(designer.propertySet()->property("margin-top").value().toDouble(), KReportUnit::parseValue("2.0cm")); 0095 QCOMPARE(designer.propertySet()->property("margin-left").value().toDouble(), KReportUnit::parseValue("3.0cm")); 0096 QCOMPARE(designer.propertySet()->property("margin-right").value().toDouble(), KReportUnit::parseValue("4.0cm")); 0097 0098 KReportPreRenderer renderer( designer.document() ); 0099 renderer.generate(); 0100 ReportPageOptions opt = renderer.reportData()->pageOptions(); 0101 0102 QCOMPARE(opt.getPageSize(), QString("A5")); 0103 QScreen *srn = QApplication::screens().at(0); 0104 const qreal dpiY = srn->logicalDotsPerInchY(); 0105 qDebug() << opt.getMarginBottom() << INCH_TO_POINT(opt.getMarginBottom()) << KReportPrivate::dpiY() << dpiY << KReportUnit::parseValue("1.5cm"); 0106 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginBottom()) / KReportPrivate::dpiY(), KReportUnit::parseValue("1.5cm"), 0.2); 0107 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginTop()) / KReportPrivate::dpiY(), KReportUnit::parseValue("2.0cm"), 0.2); 0108 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginLeft()) / KReportPrivate::dpiX(), KReportUnit::parseValue("3.0cm"), 0.2); 0109 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginRight()) / KReportPrivate::dpiX(), KReportUnit::parseValue("4.0cm"), 0.3); 0110 #endif 0111 } 0112 0113 void FormatTest::testLineElement() 0114 { 0115 #if 0 // todo 0116 KReportDesign design; 0117 QString errorMessage; 0118 if (!openDesignFile(&design, &errorMessage)) { 0119 QFAIL(qPrintable(errorMessage)); 0120 } 0121 #endif 0122 //! @todo move this renderer test to a separate place 0123 #if 0 0124 KReportDesigner designer; 0125 ReportSectionDetail *ds = designer.detailSection(); 0126 ReportSection *sec = ds->detailSection(); 0127 KReportItemLine *l = dynamic_cast<KReportItemLine*>(sec->items().first()); 0128 0129 QVERIFY(l != 0); 0130 QCOMPARE(l->Z, 1.5); 0131 KReportPosition start = l->startPosition(); 0132 KReportPosition end = l->endPosition(); 0133 QCOMPARE(start.toPoint(), QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm"))); 0134 QCOMPARE(end.toPoint(), QPointF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("2.5cm"))); 0135 0136 KReportPreRenderer renderer( designer.document() ); 0137 renderer.generate(); 0138 l = dynamic_cast<KReportItemLine*>(renderer.reportData()->object("line1")); 0139 0140 QVERIFY(l != 0); 0141 QCOMPARE(l->Z, 1.5); 0142 start = l->startPosition(); 0143 end = l->endPosition(); 0144 QCOMPARE(start.toPoint(), QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm"))); 0145 QCOMPARE(end.toPoint(), QPointF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("2.5cm"))); 0146 #endif 0147 } 0148 0149 void FormatTest::testLabelElement() 0150 { 0151 KReportDesign design; 0152 QString errorMessage; 0153 if (!openDesignFile(&design, &errorMessage)) { 0154 QFAIL(qPrintable(errorMessage)); 0155 } 0156 0157 QCOMPARE(design.title(), QLatin1String("Label Element Test Report")); 0158 0159 QVERIFY(design.hasSection(KReportSection::Type::Detail)); 0160 KReportSection detailSection = design.section(KReportSection::Type::Detail); 0161 QCOMPARE(detailSection.type(), KReportSection::Type::Detail); 0162 QCOMPARE(detailSection.height(), CM_TO_POINT(5.0)); 0163 QCOMPARE(detailSection.backgroundColor(), QColor("#eeeeee")); 0164 0165 QList<KReportElement> elements = detailSection.elements(); 0166 QCOMPARE(elements.count(), 1); 0167 KReportElement element = elements.first(); 0168 QCOMPARE(element.name(), QLatin1String("label1")); 0169 KReportLabelElement label1(elements.first()); 0170 QCOMPARE(label1.text(), "Label"); 0171 QCOMPARE(label1.z(), 2.5); 0172 const QRectF rect(CM_TO_POINT(1.5), CM_TO_POINT(0.5), CM_TO_POINT(4.5), CM_TO_POINT(0.75)); 0173 QCOMPARE(label1.rect(), rect); 0174 QCOMPARE(label1.backgroundColor(), QColor("#dddddd")); 0175 QCOMPARE(label1.foregroundColor(), QColor("#101010")); 0176 QCOMPARE(label1.backgroundOpacity(), 0.9); 0177 QCOMPARE(label1.alignment(), Qt::AlignRight | Qt::AlignBottom); 0178 QCOMPARE(label1.font().capitalization(), QFont::AllLowercase); 0179 QCOMPARE(label1.font().bold(), true); 0180 QCOMPARE(label1.font().weight(), 99); 0181 QCOMPARE(label1.font().italic(), true); 0182 QCOMPARE(label1.font().fixedPitch(), true); 0183 QCOMPARE(label1.font().family(), "Ubuntu"); 0184 QCOMPARE(label1.font().kerning(), true); 0185 QCOMPARE(label1.font().underline(), true); 0186 QCOMPARE(label1.font().strikeOut(), true); 0187 QCOMPARE(label1.font().pointSizeF(), 9.0); 0188 QCOMPARE(label1.font().letterSpacing(), 110.0); 0189 QCOMPARE(label1.font().letterSpacingType(), QFont::PercentageSpacing); 0190 QCOMPARE(label1.borderStyle().weight(), 1.0); 0191 QCOMPARE(label1.borderStyle().color(), QColor("#400000")); 0192 QCOMPARE(label1.borderStyle().penStyle(), Qt::DashLine); 0193 0194 //! @todo move this renderer test to a separate place 0195 #if 0 0196 KReportDesigner designer;//, doc.documentElement()); 0197 ReportSectionDetail *ds = designer.detailSection(); 0198 ReportSection *sec = ds->detailSection(); 0199 QVERIFY(sec->items().count() == 1); 0200 KReportDesignerItemRectBase *rect = dynamic_cast<KReportDesignerItemRectBase*>(sec->items().first()); 0201 0202 QVERIFY(rect != 0); 0203 QRectF expected( QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm"))); 0204 QCOMPARE(rect->pointRect(), expected); 0205 0206 KReportPreRenderer renderer( designer.document() ); 0207 renderer.generate(); 0208 KReportItemBase *item = dynamic_cast<KReportItemBase*>(renderer.reportData()->object("label1")); 0209 0210 QVERIFY(item != 0); 0211 KReportPosition pos = item->position(); 0212 KReportSize size = item->size(); 0213 QCOMPARE(pos.toPoint().x(), KReportUnit::parseValue("1.5cm")); 0214 QCOMPARE(pos.toPoint().y(), KReportUnit::parseValue("0.5cm")); 0215 QCOMPARE(size.toPoint(), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm"))); 0216 QCOMPARE(size.toPoint(), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm"))); 0217 #endif 0218 }