1 /* This file is part of the KDE project
2 * Copyright (C) 2012 Dag Andersen <danders@get2net.dk>
3 * Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
4 *
5 * QFUZZYCOMPARE() from marble/tests/TestUtils.h:
6 * Copyright (C) 2013 Dennis Nienhüser <earthwings@gentoo.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "FormatTest.h"
23 #include "KReportTestUtils.h"
24 #include "KReportPreRenderer.h"
25 #include "KReportDesigner.h"
26 #include "KReportLabelElement.h"
27 #include "KReportDocument.h"
28 #include "KReportDesignerSectionDetail.h"
29 #include "KReportSection.h"
30 #include "KReportDesignerItemLine.h"
31 #include "KReportItemLine.h"
32 #include "KReportDesignerItemRectBase.h"
33 #include "KReportUnit.h"
34 #include <KReportDesign>
35
36 #include <QPointF>
37 #include <QSizeF>
38 #include <QDomDocument>
39 #include <QScreen>
40 #include <QDir>
41 #include <QTest>
42 #include <QPageLayout>
43
QTEST_MAIN(FormatTest)44 QTEST_MAIN(FormatTest)
45
46 static bool openDesignFile(KReportDesign *design, QString *errorMessage)
47 {
48 const QString dir(QFile::decodeName(FILES_DATA_DIR));
49 const QString fname(QLatin1String(QTest::currentTestFunction()) + QLatin1String(".kreport"));
50 QFile file(dir + QDir::separator() + fname);
51 bool ok = file.open(QFile::ReadOnly | QFile::Text);
52 if (!ok) {
53 *errorMessage = QString::fromLatin1("Could not open file %1: ").arg(file.fileName()) + file.errorString();
54 return false;
55 }
56
57 QString content = file.readAll();
58 if (file.error() != QFileDevice::NoError) {
59 *errorMessage = QString::fromLatin1("Error reading file %1: ").arg(file.fileName()) + file.errorString();
60 return false;
61 }
62
63 KReportDesignReadingStatus status;
64 if (!design->setContent(content, &status)) {
65 QString message;
66 QDebug(&message) << status;
67 *errorMessage = QLatin1String("Failed to load content. ") + message;
68 return false;
69 }
70 errorMessage->clear();
71 return true;
72 }
73
testPageLayout()74 void FormatTest::testPageLayout()
75 {
76 KReportDesign design;
77 QString errorMessage;
78 if (!openDesignFile(&design, &errorMessage)) {
79 QFAIL(qPrintable(errorMessage));
80 }
81
82 const QPageLayout pageLayout = design.pageLayout();
83 QVERIFY(pageLayout.isValid());
84 QCOMPARE(pageLayout.pageSize().id(), QPageSize::A5);
85 QCOMPARE(pageLayout.pageSize().sizePoints(), QPageSize(QPageSize::A5).sizePoints());
86 QCOMPARE(pageLayout.orientation(), QPageLayout::Portrait);
87 QCOMPARE(pageLayout.margins(QPageLayout::Millimeter), QMarginsF(30.0, 20.0, 40.0, 15.0));
88
89 //! @todo move this renderer test to a separate place
90 #if 0
91 KReportDesigner designer;
92 QCOMPARE(designer.propertySet()->property("page-size").value().toString(), QLatin1String("A5"));
93 QCOMPARE(designer.propertySet()->property("margin-bottom").value().toDouble(), KReportUnit::parseValue("1.5cm"));
94 QCOMPARE(designer.propertySet()->property("margin-top").value().toDouble(), KReportUnit::parseValue("2.0cm"));
95 QCOMPARE(designer.propertySet()->property("margin-left").value().toDouble(), KReportUnit::parseValue("3.0cm"));
96 QCOMPARE(designer.propertySet()->property("margin-right").value().toDouble(), KReportUnit::parseValue("4.0cm"));
97
98 KReportPreRenderer renderer( designer.document() );
99 renderer.generate();
100 ReportPageOptions opt = renderer.reportData()->pageOptions();
101
102 QCOMPARE(opt.getPageSize(), QString("A5"));
103 QScreen *srn = QApplication::screens().at(0);
104 const qreal dpiY = srn->logicalDotsPerInchY();
105 qDebug() << opt.getMarginBottom() << INCH_TO_POINT(opt.getMarginBottom()) << KReportPrivate::dpiY() << dpiY << KReportUnit::parseValue("1.5cm");
106 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginBottom()) / KReportPrivate::dpiY(), KReportUnit::parseValue("1.5cm"), 0.2);
107 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginTop()) / KReportPrivate::dpiY(), KReportUnit::parseValue("2.0cm"), 0.2);
108 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginLeft()) / KReportPrivate::dpiX(), KReportUnit::parseValue("3.0cm"), 0.2);
109 QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginRight()) / KReportPrivate::dpiX(), KReportUnit::parseValue("4.0cm"), 0.3);
110 #endif
111 }
112
testLineElement()113 void FormatTest::testLineElement()
114 {
115 #if 0 // todo
116 KReportDesign design;
117 QString errorMessage;
118 if (!openDesignFile(&design, &errorMessage)) {
119 QFAIL(qPrintable(errorMessage));
120 }
121 #endif
122 //! @todo move this renderer test to a separate place
123 #if 0
124 KReportDesigner designer;
125 ReportSectionDetail *ds = designer.detailSection();
126 ReportSection *sec = ds->detailSection();
127 KReportItemLine *l = dynamic_cast<KReportItemLine*>(sec->items().first());
128
129 QVERIFY(l != 0);
130 QCOMPARE(l->Z, 1.5);
131 KReportPosition start = l->startPosition();
132 KReportPosition end = l->endPosition();
133 QCOMPARE(start.toPoint(), QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")));
134 QCOMPARE(end.toPoint(), QPointF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("2.5cm")));
135
136 KReportPreRenderer renderer( designer.document() );
137 renderer.generate();
138 l = dynamic_cast<KReportItemLine*>(renderer.reportData()->object("line1"));
139
140 QVERIFY(l != 0);
141 QCOMPARE(l->Z, 1.5);
142 start = l->startPosition();
143 end = l->endPosition();
144 QCOMPARE(start.toPoint(), QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")));
145 QCOMPARE(end.toPoint(), QPointF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("2.5cm")));
146 #endif
147 }
148
testLabelElement()149 void FormatTest::testLabelElement()
150 {
151 KReportDesign design;
152 QString errorMessage;
153 if (!openDesignFile(&design, &errorMessage)) {
154 QFAIL(qPrintable(errorMessage));
155 }
156
157 QCOMPARE(design.title(), QLatin1String("Label Element Test Report"));
158
159 QVERIFY(design.hasSection(KReportSection::Type::Detail));
160 KReportSection detailSection = design.section(KReportSection::Type::Detail);
161 QCOMPARE(detailSection.type(), KReportSection::Type::Detail);
162 QCOMPARE(detailSection.height(), CM_TO_POINT(5.0));
163 QCOMPARE(detailSection.backgroundColor(), QColor("#eeeeee"));
164
165 QList<KReportElement> elements = detailSection.elements();
166 QCOMPARE(elements.count(), 1);
167 KReportElement element = elements.first();
168 QCOMPARE(element.name(), QLatin1String("label1"));
169 KReportLabelElement label1(elements.first());
170 QCOMPARE(label1.text(), "Label");
171 QCOMPARE(label1.z(), 2.5);
172 const QRectF rect(CM_TO_POINT(1.5), CM_TO_POINT(0.5), CM_TO_POINT(4.5), CM_TO_POINT(0.75));
173 QCOMPARE(label1.rect(), rect);
174 QCOMPARE(label1.backgroundColor(), QColor("#dddddd"));
175 QCOMPARE(label1.foregroundColor(), QColor("#101010"));
176 QCOMPARE(label1.backgroundOpacity(), 0.9);
177 QCOMPARE(label1.alignment(), Qt::AlignRight | Qt::AlignBottom);
178 QCOMPARE(label1.font().capitalization(), QFont::AllLowercase);
179 QCOMPARE(label1.font().bold(), true);
180 QCOMPARE(label1.font().weight(), 99);
181 QCOMPARE(label1.font().italic(), true);
182 QCOMPARE(label1.font().fixedPitch(), true);
183 QCOMPARE(label1.font().family(), "Ubuntu");
184 QCOMPARE(label1.font().kerning(), true);
185 QCOMPARE(label1.font().underline(), true);
186 QCOMPARE(label1.font().strikeOut(), true);
187 QCOMPARE(label1.font().pointSizeF(), 9.0);
188 QCOMPARE(label1.font().letterSpacing(), 110.0);
189 QCOMPARE(label1.font().letterSpacingType(), QFont::PercentageSpacing);
190 QCOMPARE(label1.borderStyle().weight(), 1.0);
191 QCOMPARE(label1.borderStyle().color(), QColor("#400000"));
192 QCOMPARE(label1.borderStyle().penStyle(), Qt::DashLine);
193
194 //! @todo move this renderer test to a separate place
195 #if 0
196 KReportDesigner designer;//, doc.documentElement());
197 ReportSectionDetail *ds = designer.detailSection();
198 ReportSection *sec = ds->detailSection();
199 QVERIFY(sec->items().count() == 1);
200 KReportDesignerItemRectBase *rect = dynamic_cast<KReportDesignerItemRectBase*>(sec->items().first());
201
202 QVERIFY(rect != 0);
203 QRectF expected( QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm")));
204 QCOMPARE(rect->pointRect(), expected);
205
206 KReportPreRenderer renderer( designer.document() );
207 renderer.generate();
208 KReportItemBase *item = dynamic_cast<KReportItemBase*>(renderer.reportData()->object("label1"));
209
210 QVERIFY(item != 0);
211 KReportPosition pos = item->position();
212 KReportSize size = item->size();
213 QCOMPARE(pos.toPoint().x(), KReportUnit::parseValue("1.5cm"));
214 QCOMPARE(pos.toPoint().y(), KReportUnit::parseValue("0.5cm"));
215 QCOMPARE(size.toPoint(), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm")));
216 QCOMPARE(size.toPoint(), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm")));
217 #endif
218 }
219