1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QtTest> |
30 | #include <QtGlobal> |
31 | #include <QtAlgorithms> |
32 | #include <QtGui/QPageSize> |
33 | |
34 | #ifdef Q_OS_WIN |
35 | #include <QtCore/qt_windows.h> |
36 | #endif // Q_OS_WIN |
37 | |
38 | class tst_QPageSize : public QObject |
39 | { |
40 | Q_OBJECT |
41 | |
42 | private slots: |
43 | void basics(); |
44 | void fuzzy(); |
45 | void custom(); |
46 | void statics(); |
47 | }; |
48 | |
49 | void tst_QPageSize::basics() |
50 | { |
51 | // Invalid |
52 | QPageSize invalid = QPageSize(QPageSize::Custom); |
53 | QCOMPARE(invalid.isValid(), false); |
54 | invalid = QPageSize(QSize()); |
55 | QCOMPARE(invalid.isValid(), false); |
56 | invalid = QPageSize(QSizeF(), QPageSize::Millimeter); |
57 | QCOMPARE(invalid.isValid(), false); |
58 | |
59 | // Simple QPageSize::PaperSizeId |
60 | QPageSize a4 = QPageSize(QPageSize::A4); |
61 | QCOMPARE(a4.isValid(), true); |
62 | QCOMPARE(a4.key(), QString("A4" )); |
63 | QCOMPARE(a4.name(), QString("A4" )); |
64 | QCOMPARE(a4.id(), QPageSize::A4); |
65 | #ifdef Q_OS_WIN |
66 | QCOMPARE(a4.windowsId(), DMPAPER_A4); |
67 | #else |
68 | QCOMPARE(a4.windowsId(), 9); // DMPAPER_A4 |
69 | #endif |
70 | QCOMPARE(a4.definitionSize(), QSizeF(210, 297)); |
71 | QCOMPARE(a4.definitionUnits(), QPageSize::Millimeter); |
72 | QCOMPARE(a4.size(QPageSize::Millimeter), QSizeF(210, 297)); |
73 | QCOMPARE(a4.size(QPageSize::Inch), QSizeF(8.27, 11.69)); |
74 | QCOMPARE(a4.size(QPageSize::Pica), QSizeF(49.58, 70.17)); |
75 | QCOMPARE(a4.sizePoints(), QSize(595, 842)); |
76 | QCOMPARE(a4.sizePixels(72), QSize(595, 842)); |
77 | QCOMPARE(a4.rect(QPageSize::Millimeter), QRectF(0, 0, 210, 297)); |
78 | QCOMPARE(a4.rect(QPageSize::Inch), QRectF(0, 0, 8.27, 11.69)); |
79 | QCOMPARE(a4.rect(QPageSize::Pica), QRectF(0, 0, 49.58, 70.17)); |
80 | QCOMPARE(a4.rectPoints(), QRect(0, 0, 595, 842)); |
81 | QCOMPARE(a4.rectPixels(72), QRect(0, 0, 595, 842)); |
82 | |
83 | // Simple QPageSize::PaperSizeId later in list |
84 | QPageSize folio = QPageSize(QPageSize::Folio); |
85 | QCOMPARE(folio.isValid(), true); |
86 | QCOMPARE(folio.key(), QString("Folio" )); |
87 | QCOMPARE(folio.name(), QString("Folio (8.27 x 13 in)" )); |
88 | QCOMPARE(folio.id(), QPageSize::Folio); |
89 | QCOMPARE(folio.definitionSize(), QSizeF(210, 330)); |
90 | QCOMPARE(folio.definitionUnits(), QPageSize::Millimeter); |
91 | QCOMPARE(folio.size(QPageSize::Millimeter), QSizeF(210, 330)); |
92 | QCOMPARE(folio.sizePoints(), QSize(595, 935)); |
93 | QCOMPARE(folio.sizePixels(72), QSize(595, 935)); |
94 | QCOMPARE(folio.size(QPageSize::Inch), QSizeF(8.27, 13)); |
95 | |
96 | // Simple QPageSize::PaperSizeId last in list |
97 | QPageSize you4 = QPageSize(QPageSize::EnvelopeYou4); |
98 | QCOMPARE(you4.isValid(), true); |
99 | QCOMPARE(you4.key(), QString("EnvYou4" )); |
100 | QCOMPARE(you4.name(), QString("Envelope You 4" )); |
101 | QCOMPARE(you4.id(), QPageSize::EnvelopeYou4); |
102 | #ifdef Q_OS_WIN |
103 | QCOMPARE(you4.windowsId(), DMPAPER_JENV_YOU4); |
104 | #else |
105 | QCOMPARE(you4.windowsId(), 91); |
106 | #endif |
107 | QCOMPARE(you4.size(QPageSize::Millimeter), QSizeF(105, 235)); |
108 | QCOMPARE(you4.size(QPageSize::Inch), QSizeF(4.13, 9.25)); |
109 | QCOMPARE(you4.sizePoints(), QSize(298, 666)); |
110 | QCOMPARE(you4.sizePixels(72), QSize(298, 666)); |
111 | |
112 | // Simple QSize in Points |
113 | QPageSize a4b = QPageSize(QSize(595, 842)); |
114 | QCOMPARE(a4b.isValid(), true); |
115 | QCOMPARE(a4b.id(), QPageSize::A4); |
116 | QCOMPARE(a4b.sizePoints(), QSize(595, 842)); |
117 | |
118 | // Simple QSize in Points later in list, custom name |
119 | QPageSize folio2 = QPageSize(QSize(595, 935), QStringLiteral("Folio2" )); |
120 | QCOMPARE(folio2.isValid(), true); |
121 | QCOMPARE(folio2.name(), QString("Folio2" )); |
122 | QCOMPARE(folio2.id(), QPageSize::Folio); |
123 | QCOMPARE(folio2.sizePoints(), QSize(595, 935)); |
124 | |
125 | // Comparisons |
126 | QCOMPARE((a4 == folio), false); |
127 | QCOMPARE((a4 != folio), true); |
128 | QCOMPARE((a4.isEquivalentTo(folio)), false); |
129 | QCOMPARE((a4 == a4b), true); |
130 | QCOMPARE((a4 != a4b), false); |
131 | QCOMPARE((a4.isEquivalentTo(a4b)), true); |
132 | QCOMPARE((folio == folio2), false); // Name different |
133 | QCOMPARE((folio != folio2), true); // Name different |
134 | QCOMPARE((folio.isEquivalentTo(folio2)), true); |
135 | |
136 | // Simple QSize in Millimeters |
137 | QPageSize folio3 = QPageSize(QSizeF(210, 330), QPageSize::Millimeter); |
138 | QCOMPARE(folio3.isValid(), true); |
139 | QCOMPARE(folio3.id(), QPageSize::Folio); |
140 | QCOMPARE(folio3.sizePoints(), QSize(595, 935)); |
141 | } |
142 | |
143 | void tst_QPageSize::fuzzy() |
144 | { |
145 | // Using FuzzyMatch by default |
146 | |
147 | // Simple QSize within 3 Points |
148 | QPageSize a4a = QPageSize(QSize(592, 845)); |
149 | QCOMPARE(a4a.isValid(), true); |
150 | QCOMPARE(a4a.id(), QPageSize::A4); |
151 | QCOMPARE(a4a.sizePoints(), QSize(595, 842)); |
152 | |
153 | // Simple QSizeF within 1mm |
154 | QPageSize a4b = QPageSize(QSizeF(211, 298), QPageSize::Millimeter); |
155 | QCOMPARE(a4b.isValid(), true); |
156 | QCOMPARE(a4b.id(), QPageSize::A4); |
157 | QCOMPARE(a4b.size(QPageSize::Millimeter), QSizeF(210, 297)); |
158 | QCOMPARE(a4b.sizePoints(), QSize(595, 842)); |
159 | |
160 | // Using FuzzyOrientationMatch |
161 | |
162 | // Exact A4 in landscape mode |
163 | QPageSize a4l = QPageSize(QSize(842, 595)); |
164 | QCOMPARE(a4l.isValid(), true); |
165 | QCOMPARE(a4l.id(), QPageSize::Custom); |
166 | QCOMPARE(a4l.sizePoints(), QSize(842, 595)); |
167 | |
168 | a4l = QPageSize(QSize(842, 595), QString(), QPageSize::FuzzyOrientationMatch); |
169 | QCOMPARE(a4l.isValid(), true); |
170 | QCOMPARE(a4l.id(), QPageSize::A4); |
171 | QCOMPARE(a4l.sizePoints(), QSize(595, 842)); |
172 | |
173 | // Using ExactMatch |
174 | |
175 | // Simple QSize within 3 Points |
176 | QPageSize a4d = QPageSize(QSize(592, 845), QString(), QPageSize::ExactMatch); |
177 | QCOMPARE(a4d.isValid(), true); |
178 | QCOMPARE(a4d.id(), QPageSize::Custom); |
179 | QCOMPARE(a4d.sizePoints(), QSize(592, 845)); |
180 | |
181 | // Simple QSizeF within 1mm |
182 | QPageSize a4e = QPageSize(QSizeF(211, 298), QPageSize::Millimeter, QString(), QPageSize::ExactMatch); |
183 | QCOMPARE(a4e.isValid(), true); |
184 | QCOMPARE(a4e.id(), QPageSize::Custom); |
185 | QCOMPARE(a4e.size(QPageSize::Millimeter), QSizeF(211, 298)); |
186 | QCOMPARE(a4e.sizePoints(), QSize(598, 845)); |
187 | } |
188 | |
189 | void tst_QPageSize::custom() |
190 | { |
191 | // Simple non-standard Points QSize |
192 | QPageSize custom1 = QPageSize(QSize(500, 600)); |
193 | QCOMPARE(custom1.isValid(), true); |
194 | QCOMPARE(custom1.key(), QString("Custom.500x600" )); |
195 | QCOMPARE(custom1.name(), QString("Custom (500pt x 600pt)" )); |
196 | QCOMPARE(custom1.id(), QPageSize::Custom); |
197 | QCOMPARE(custom1.definitionSize(), QSizeF(500, 600)); |
198 | QCOMPARE(custom1.definitionUnits(), QPageSize::Point); |
199 | QCOMPARE(custom1.size(QPageSize::Millimeter), QSizeF(176.39, 211.67)); |
200 | QCOMPARE(custom1.size(QPageSize::Pica), QSizeF(41.67, 50)); |
201 | QCOMPARE(custom1.sizePoints(), QSize(500, 600)); |
202 | QCOMPARE(custom1.sizePixels(72), QSize(500, 600)); |
203 | |
204 | // Simple non-standard MM QSizeF |
205 | QPageSize custom2 = QPageSize(QSizeF(500.3, 600.57), QPageSize::Millimeter); |
206 | QCOMPARE(custom2.isValid(), true); |
207 | QCOMPARE(custom2.key(), QString("Custom.500.3x600.57mm" )); |
208 | QCOMPARE(custom2.name(), QString("Custom (500.3mm x 600.57mm)" )); |
209 | QCOMPARE(custom2.id(), QPageSize::Custom); |
210 | QCOMPARE(custom2.definitionSize(), QSizeF(500.3, 600.57)); |
211 | QCOMPARE(custom2.definitionUnits(), QPageSize::Millimeter); |
212 | QCOMPARE(custom2.size(QPageSize::Millimeter), QSizeF(500.3, 600.57)); |
213 | QCOMPARE(custom2.size(QPageSize::Pica), QSizeF(118.18, 141.87)); |
214 | QCOMPARE(custom2.sizePoints(), QSize(1418, 1702)); |
215 | QCOMPARE(custom2.sizePixels(72), QSize(1418, 1702)); |
216 | } |
217 | |
218 | void tst_QPageSize::statics() |
219 | { |
220 | QCOMPARE(QPageSize::key(QPageSize::EnvelopeYou4), QString("EnvYou4" )); |
221 | QCOMPARE(QPageSize::name(QPageSize::EnvelopeYou4), QString("Envelope You 4" )); |
222 | |
223 | #ifdef Q_OS_WIN |
224 | QCOMPARE(QPageSize::windowsId(QPageSize::EnvelopeYou4), DMPAPER_JENV_YOU4); |
225 | QCOMPARE(QPageSize::id(DMPAPER_JENV_YOU4), QPageSize::EnvelopeYou4); |
226 | QCOMPARE(QPageSize::id(DMPAPER_A4_ROTATED), QPageSize::A4); |
227 | #else |
228 | QCOMPARE(QPageSize::windowsId(QPageSize::EnvelopeYou4), 91); |
229 | QCOMPARE(QPageSize::id(91), QPageSize::EnvelopeYou4); |
230 | QCOMPARE(QPageSize::id(77), QPageSize::A4); |
231 | #endif |
232 | |
233 | QCOMPARE(QPageSize::id(QSize(298, 666)), QPageSize::EnvelopeYou4); |
234 | QCOMPARE(QPageSize::id(QSizeF(105, 235), QPageSize::Millimeter), QPageSize::EnvelopeYou4); |
235 | |
236 | QCOMPARE(QPageSize::definitionSize(QPageSize::Letter), QSizeF(8.5, 11)); |
237 | QCOMPARE(QPageSize::definitionUnits(QPageSize::Letter), QPageSize::Inch); |
238 | QCOMPARE(QPageSize::definitionSize(QPageSize::EnvelopeYou4), QSizeF(105, 235)); |
239 | QCOMPARE(QPageSize::definitionUnits(QPageSize::EnvelopeYou4), QPageSize::Millimeter); |
240 | |
241 | QCOMPARE(QPageSize::size(QPageSize::EnvelopeYou4, QPageSize::Millimeter), QSizeF(105, 235)); |
242 | QCOMPARE(QPageSize::sizePoints(QPageSize::EnvelopeYou4), QSize(298, 666)); |
243 | QCOMPARE(QPageSize::sizePixels(QPageSize::EnvelopeYou4, 72), QSize(298, 666)); |
244 | } |
245 | |
246 | QTEST_APPLESS_MAIN(tst_QPageSize) |
247 | |
248 | #include "tst_qpagesize.moc" |
249 | |