File: FormatTest.cpp

package info (click to toggle)
kreport 3.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,032 kB
  • sloc: cpp: 23,245; python: 779; xml: 197; sh: 108; makefile: 9
file content (218 lines) | stat: -rw-r--r-- 9,362 bytes parent folder | download | duplicates (4)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* This file is part of the KDE project
 * Copyright (C) 2012 Dag Andersen <danders@get2net.dk>
 * Copyright (C) 2015 Jarosław Staniek <staniek@kde.org>
 *
 * QFUZZYCOMPARE() from marble/tests/TestUtils.h:
 * Copyright (C) 2013 Dennis Nienhüser <earthwings@gentoo.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "FormatTest.h"
#include "KReportTestUtils.h"
#include "KReportPreRenderer.h"
#include "KReportDesigner.h"
#include "KReportLabelElement.h"
#include "KReportDocument.h"
#include "KReportDesignerSectionDetail.h"
#include "KReportSection.h"
#include "KReportDesignerItemLine.h"
#include "KReportItemLine.h"
#include "KReportDesignerItemRectBase.h"
#include "KReportUnit.h"
#include <KReportDesign>

#include <QPointF>
#include <QSizeF>
#include <QDomDocument>
#include <QScreen>
#include <QDir>
#include <QTest>
#include <QPageLayout>

QTEST_MAIN(FormatTest)

static bool openDesignFile(KReportDesign *design, QString *errorMessage)
{
    const QString dir(QFile::decodeName(FILES_DATA_DIR));
    const QString fname(QLatin1String(QTest::currentTestFunction()) + QLatin1String(".kreport"));
    QFile file(dir + QDir::separator() + fname);
    bool ok = file.open(QFile::ReadOnly | QFile::Text);
    if (!ok) {
        *errorMessage = QString::fromLatin1("Could not open file %1: ").arg(file.fileName()) + file.errorString();
        return false;
    }

    QString content = file.readAll();
    if (file.error() != QFileDevice::NoError) {
        *errorMessage = QString::fromLatin1("Error reading file %1: ").arg(file.fileName()) + file.errorString();
        return false;
    }

    KReportDesignReadingStatus status;
    if (!design->setContent(content, &status)) {
        QString message;
        QDebug(&message) << status;
        *errorMessage = QLatin1String("Failed to load content. ") + message;
        return false;
    }
    errorMessage->clear();
    return true;
}

void FormatTest::testPageLayout()
{
    KReportDesign design;
    QString errorMessage;
    if (!openDesignFile(&design, &errorMessage)) {
        QFAIL(qPrintable(errorMessage));
    }

    const QPageLayout pageLayout = design.pageLayout();
    QVERIFY(pageLayout.isValid());
    QCOMPARE(pageLayout.pageSize().id(), QPageSize::A5);
    QCOMPARE(pageLayout.pageSize().sizePoints(), QPageSize(QPageSize::A5).sizePoints());
    QCOMPARE(pageLayout.orientation(), QPageLayout::Portrait);
    QCOMPARE(pageLayout.margins(QPageLayout::Millimeter), QMarginsF(30.0, 20.0, 40.0, 15.0));

    //! @todo move this renderer test to a separate place
#if 0
    KReportDesigner designer;
    QCOMPARE(designer.propertySet()->property("page-size").value().toString(), QLatin1String("A5"));
    QCOMPARE(designer.propertySet()->property("margin-bottom").value().toDouble(), KReportUnit::parseValue("1.5cm"));
    QCOMPARE(designer.propertySet()->property("margin-top").value().toDouble(), KReportUnit::parseValue("2.0cm"));
    QCOMPARE(designer.propertySet()->property("margin-left").value().toDouble(), KReportUnit::parseValue("3.0cm"));
    QCOMPARE(designer.propertySet()->property("margin-right").value().toDouble(), KReportUnit::parseValue("4.0cm"));

    KReportPreRenderer renderer( designer.document() );
    renderer.generate();
    ReportPageOptions opt = renderer.reportData()->pageOptions();

    QCOMPARE(opt.getPageSize(), QString("A5"));
    QScreen *srn = QApplication::screens().at(0);
    const qreal dpiY = srn->logicalDotsPerInchY();
    qDebug() << opt.getMarginBottom() << INCH_TO_POINT(opt.getMarginBottom()) << KReportPrivate::dpiY() << dpiY << KReportUnit::parseValue("1.5cm");
    QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginBottom()) / KReportPrivate::dpiY(), KReportUnit::parseValue("1.5cm"), 0.2);
    QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginTop()) / KReportPrivate::dpiY(), KReportUnit::parseValue("2.0cm"), 0.2);
    QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginLeft()) / KReportPrivate::dpiX(), KReportUnit::parseValue("3.0cm"), 0.2);
    QFUZZYCOMPARE(INCH_TO_POINT(opt.getMarginRight()) / KReportPrivate::dpiX(), KReportUnit::parseValue("4.0cm"), 0.3);
#endif
}

void FormatTest::testLineElement()
{
#if 0 // todo
    KReportDesign design;
    QString errorMessage;
    if (!openDesignFile(&design, &errorMessage)) {
        QFAIL(qPrintable(errorMessage));
    }
#endif
    //! @todo move this renderer test to a separate place
#if 0
    KReportDesigner designer;
    ReportSectionDetail *ds = designer.detailSection();
    ReportSection *sec = ds->detailSection();
    KReportItemLine *l = dynamic_cast<KReportItemLine*>(sec->items().first());

    QVERIFY(l != 0);
    QCOMPARE(l->Z, 1.5);
    KReportPosition start = l->startPosition();
    KReportPosition end = l->endPosition();
    QCOMPARE(start.toPoint(), QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")));
    QCOMPARE(end.toPoint(), QPointF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("2.5cm")));

    KReportPreRenderer renderer( designer.document() );
    renderer.generate();
    l = dynamic_cast<KReportItemLine*>(renderer.reportData()->object("line1"));

    QVERIFY(l != 0);
    QCOMPARE(l->Z, 1.5);
    start = l->startPosition();
    end = l->endPosition();
    QCOMPARE(start.toPoint(), QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")));
    QCOMPARE(end.toPoint(), QPointF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("2.5cm")));
#endif
}

void FormatTest::testLabelElement()
{
    KReportDesign design;
    QString errorMessage;
    if (!openDesignFile(&design, &errorMessage)) {
        QFAIL(qPrintable(errorMessage));
    }

    QCOMPARE(design.title(), QLatin1String("Label Element Test Report"));

    QVERIFY(design.hasSection(KReportSection::Type::Detail));
    KReportSection detailSection = design.section(KReportSection::Type::Detail);
    QCOMPARE(detailSection.type(), KReportSection::Type::Detail);
    QCOMPARE(detailSection.height(), CM_TO_POINT(5.0));
    QCOMPARE(detailSection.backgroundColor(), QColor("#eeeeee"));

    QList<KReportElement> elements = detailSection.elements();
    QCOMPARE(elements.count(), 1);
    KReportElement element = elements.first();
    QCOMPARE(element.name(), QLatin1String("label1"));
    KReportLabelElement label1(elements.first());
    QCOMPARE(label1.text(), "Label");
    QCOMPARE(label1.z(), 2.5);
    const QRectF rect(CM_TO_POINT(1.5), CM_TO_POINT(0.5), CM_TO_POINT(4.5), CM_TO_POINT(0.75));
    QCOMPARE(label1.rect(), rect);
    QCOMPARE(label1.backgroundColor(), QColor("#dddddd"));
    QCOMPARE(label1.foregroundColor(), QColor("#101010"));
    QCOMPARE(label1.backgroundOpacity(), 0.9);
    QCOMPARE(label1.alignment(), Qt::AlignRight | Qt::AlignBottom);
    QCOMPARE(label1.font().capitalization(), QFont::AllLowercase);
    QCOMPARE(label1.font().bold(), true);
    QCOMPARE(label1.font().weight(), 99);
    QCOMPARE(label1.font().italic(), true);
    QCOMPARE(label1.font().fixedPitch(), true);
    QCOMPARE(label1.font().family(), "Ubuntu");
    QCOMPARE(label1.font().kerning(), true);
    QCOMPARE(label1.font().underline(), true);
    QCOMPARE(label1.font().strikeOut(), true);
    QCOMPARE(label1.font().pointSizeF(), 9.0);
    QCOMPARE(label1.font().letterSpacing(), 110.0);
    QCOMPARE(label1.font().letterSpacingType(), QFont::PercentageSpacing);
    QCOMPARE(label1.borderStyle().weight(), 1.0);
    QCOMPARE(label1.borderStyle().color(), QColor("#400000"));
    QCOMPARE(label1.borderStyle().penStyle(), Qt::DashLine);

    //! @todo move this renderer test to a separate place
#if 0
    KReportDesigner designer;//, doc.documentElement());
    ReportSectionDetail *ds = designer.detailSection();
    ReportSection *sec = ds->detailSection();
    QVERIFY(sec->items().count() == 1);
    KReportDesignerItemRectBase *rect = dynamic_cast<KReportDesignerItemRectBase*>(sec->items().first());

    QVERIFY(rect != 0);
    QRectF expected( QPointF(KReportUnit::parseValue("1.5cm"), KReportUnit::parseValue("0.5cm")), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm")));
    QCOMPARE(rect->pointRect(), expected);

    KReportPreRenderer renderer( designer.document() );
    renderer.generate();
    KReportItemBase *item = dynamic_cast<KReportItemBase*>(renderer.reportData()->object("label1"));

    QVERIFY(item != 0);
    KReportPosition pos = item->position();
    KReportSize size = item->size();
    QCOMPARE(pos.toPoint().x(), KReportUnit::parseValue("1.5cm"));
    QCOMPARE(pos.toPoint().y(), KReportUnit::parseValue("0.5cm"));
    QCOMPARE(size.toPoint(), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm")));
    QCOMPARE(size.toPoint(), QSizeF(KReportUnit::parseValue("4.5cm"), KReportUnit::parseValue("0.75cm")));
#endif
}