| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2014 John Layt <jlayt@kde.org> | 
| 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 <QtGui/qpagelayout.h> | 
| 31 |  | 
| 32 | class tst_QPageLayout : public QObject | 
| 33 | { | 
| 34 |     Q_OBJECT | 
| 35 |  | 
| 36 | private slots: | 
| 37 |     void invalid(); | 
| 38 |     void basics(); | 
| 39 |     void setGetMargins(); | 
| 40 | }; | 
| 41 |  | 
| 42 | void tst_QPageLayout::invalid() | 
| 43 | { | 
| 44 |     // Invalid | 
| 45 |     QPageLayout invalid = QPageLayout(); | 
| 46 |     QCOMPARE(invalid.isValid(), false); | 
| 47 |     invalid = QPageLayout(QPageSize(), QPageLayout::Portrait, QMarginsF()); | 
| 48 |     QCOMPARE(invalid.isValid(), false); | 
| 49 | } | 
| 50 |  | 
| 51 | void tst_QPageLayout::basics() | 
| 52 | { | 
| 53 |     // Simple A4, no margins | 
| 54 |     QPageLayout simple = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF(0, 0, 0, 0)); | 
| 55 |     QCOMPARE(simple.isValid(), true); | 
| 56 |     QCOMPARE(simple.pageSize().id(), QPageSize::A4); | 
| 57 |     QCOMPARE(simple.orientation(), QPageLayout::Portrait); | 
| 58 |     QCOMPARE(simple.margins(), QMarginsF(0, 0, 0, 0)); | 
| 59 |     QCOMPARE(simple.margins(QPageLayout::Millimeter), QMarginsF(0, 0, 0, 0)); | 
| 60 |     QCOMPARE(simple.marginsPoints(), QMargins(0, 0, 0, 0)); | 
| 61 |     QCOMPARE(simple.marginsPixels(72), QMargins(0, 0, 0, 0)); | 
| 62 |     QCOMPARE(simple.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 63 |     QCOMPARE(simple.maximumMargins(), QMarginsF(595, 842, 595, 842)); | 
| 64 |     QCOMPARE(simple.fullRect(), QRectF(0, 0, 595, 842)); | 
| 65 |     QCOMPARE(simple.fullRect(QPageLayout::Millimeter), QRectF(0, 0, 210, 297)); | 
| 66 |     QCOMPARE(simple.fullRectPoints(), QRect(0, 0, 595, 842)); | 
| 67 |     QCOMPARE(simple.fullRectPixels(72), QRect(0, 0, 595, 842)); | 
| 68 |     QCOMPARE(simple.paintRect(), QRectF(0, 0, 595, 842)); | 
| 69 |     QCOMPARE(simple.paintRect(QPageLayout::Millimeter), QRectF(0, 0, 210, 297)); | 
| 70 |     QCOMPARE(simple.paintRectPoints(), QRect(0, 0, 595, 842)); | 
| 71 |     QCOMPARE(simple.paintRectPixels(72), QRect(0, 0, 595, 842)); | 
| 72 |  | 
| 73 |     const QPageLayout a4portrait = simple; | 
| 74 |     QCOMPARE(a4portrait, simple); | 
| 75 |  | 
| 76 |     // Change orientation | 
| 77 |     simple.setOrientation(QPageLayout::Landscape); | 
| 78 |     QVERIFY(simple != a4portrait); | 
| 79 |     QCOMPARE(simple.orientation(), QPageLayout::Landscape); | 
| 80 |     QCOMPARE(simple.margins(), QMarginsF(0, 0, 0, 0)); | 
| 81 |     QCOMPARE(simple.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 82 |     QCOMPARE(simple.maximumMargins(), QMarginsF(842, 595, 842, 595)); | 
| 83 |     QCOMPARE(simple.fullRect(), QRectF(0, 0, 842, 595)); | 
| 84 |     QCOMPARE(simple.fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 85 |     QCOMPARE(simple.fullRectPoints(), QRect(0, 0, 842, 595)); | 
| 86 |     QCOMPARE(simple.fullRectPixels(72), QRect(0, 0, 842, 595)); | 
| 87 |     QCOMPARE(simple.paintRect(), QRectF(0, 0, 842, 595)); | 
| 88 |     QCOMPARE(simple.paintRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 89 |     QCOMPARE(simple.paintRectPoints(), QRect(0, 0, 842, 595)); | 
| 90 |     QCOMPARE(simple.paintRectPixels(72), QRect(0, 0, 842, 595)); | 
| 91 |  | 
| 92 |     // Change mode | 
| 93 |     QCOMPARE(simple.mode(), QPageLayout::StandardMode); | 
| 94 |     simple.setMode(QPageLayout::FullPageMode); | 
| 95 |     QCOMPARE(simple.mode(), QPageLayout::FullPageMode); | 
| 96 |     QCOMPARE(simple.orientation(), QPageLayout::Landscape); | 
| 97 |     QCOMPARE(simple.margins(), QMarginsF(0, 0, 0, 0)); | 
| 98 |     QCOMPARE(simple.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 99 |     QCOMPARE(simple.maximumMargins(), QMarginsF(842, 595, 842, 595)); | 
| 100 |     QCOMPARE(simple.fullRect(), QRectF(0, 0, 842, 595)); | 
| 101 |     QCOMPARE(simple.fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 102 |     QCOMPARE(simple.fullRectPoints(), QRect(0, 0, 842, 595)); | 
| 103 |     QCOMPARE(simple.fullRectPixels(72), QRect(0, 0, 842, 595)); | 
| 104 |     QCOMPARE(simple.paintRect(), QRectF(0, 0, 842, 595)); | 
| 105 |     QCOMPARE(simple.paintRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 106 |     QCOMPARE(simple.paintRectPoints(), QRect(0, 0, 842, 595)); | 
| 107 |     QCOMPARE(simple.paintRectPixels(72), QRect(0, 0, 842, 595)); | 
| 108 |  | 
| 109 |     // A4, 10pt margins | 
| 110 |     QPageLayout tenpoint = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF(10, 10, 10, 10)); | 
| 111 |     QCOMPARE(tenpoint.isValid(), true); | 
| 112 |     QCOMPARE(tenpoint.margins(), QMarginsF(10, 10, 10, 10)); | 
| 113 |     QCOMPARE(tenpoint.margins(QPageLayout::Millimeter), QMarginsF(3.53, 3.53, 3.53, 3.53)); | 
| 114 |     QCOMPARE(tenpoint.marginsPoints(), QMargins(10, 10, 10, 10)); | 
| 115 |     QCOMPARE(tenpoint.marginsPixels(72), QMargins(10, 10, 10, 10)); | 
| 116 |     QCOMPARE(tenpoint.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 117 |     QCOMPARE(tenpoint.maximumMargins(), QMarginsF(595, 842, 595, 842)); | 
| 118 |     QCOMPARE(tenpoint.fullRect(), QRectF(0, 0, 595, 842)); | 
| 119 |     QCOMPARE(tenpoint.fullRect(QPageLayout::Millimeter), QRectF(0, 0, 210, 297)); | 
| 120 |     QCOMPARE(tenpoint.fullRectPoints(), QRect(0, 0, 595, 842)); | 
| 121 |     QCOMPARE(tenpoint.fullRectPixels(72), QRect(0, 0, 595, 842)); | 
| 122 |     QCOMPARE(tenpoint.paintRect(), QRectF(10, 10, 575, 822)); | 
| 123 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter), QRectF(3.53, 3.53, 202.94, 289.94)); | 
| 124 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).x(), 3.53); | 
| 125 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).y(), 3.53); | 
| 126 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).width(), 202.94); | 
| 127 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).height(), 289.94); | 
| 128 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).left(), 3.53); | 
| 129 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).right(), 206.47); | 
| 130 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).top(), 3.53); | 
| 131 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter).bottom(), 293.47); | 
| 132 |     QCOMPARE(tenpoint.paintRectPoints(), QRect(10, 10, 575, 822)); | 
| 133 |     QCOMPARE(tenpoint.paintRectPixels(72), QRect(10, 10, 575, 822)); | 
| 134 |  | 
| 135 |     // Change orientation | 
| 136 |     tenpoint.setOrientation(QPageLayout::Landscape); | 
| 137 |     QCOMPARE(tenpoint.orientation(), QPageLayout::Landscape); | 
| 138 |     QCOMPARE(tenpoint.margins(), QMarginsF(10, 10, 10, 10)); | 
| 139 |     QCOMPARE(tenpoint.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 140 |     QCOMPARE(tenpoint.maximumMargins(), QMarginsF(842, 595, 842, 595)); | 
| 141 |     QCOMPARE(tenpoint.fullRect(), QRectF(0, 0, 842, 595)); | 
| 142 |     QCOMPARE(tenpoint.fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 143 |     QCOMPARE(tenpoint.fullRectPoints(), QRect(0, 0, 842, 595)); | 
| 144 |     QCOMPARE(tenpoint.fullRectPixels(72), QRect(0, 0, 842, 595)); | 
| 145 |     QCOMPARE(tenpoint.paintRect(), QRectF(10, 10, 822, 575)); | 
| 146 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter), QRectF(3.53, 3.53, 289.94, 202.94)); | 
| 147 |     QCOMPARE(tenpoint.paintRectPoints(), QRect(10, 10, 822, 575)); | 
| 148 |     QCOMPARE(tenpoint.paintRectPixels(72), QRect(10, 10, 822, 575)); | 
| 149 |  | 
| 150 |     // Change mode | 
| 151 |     QCOMPARE(tenpoint.mode(), QPageLayout::StandardMode); | 
| 152 |     tenpoint.setMode(QPageLayout::FullPageMode); | 
| 153 |     QCOMPARE(tenpoint.mode(), QPageLayout::FullPageMode); | 
| 154 |     QCOMPARE(tenpoint.orientation(), QPageLayout::Landscape); | 
| 155 |     QCOMPARE(tenpoint.margins(), QMarginsF(10, 10, 10, 10)); | 
| 156 |     QCOMPARE(tenpoint.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 157 |     QCOMPARE(tenpoint.maximumMargins(), QMarginsF(842, 595, 842, 595)); | 
| 158 |     QCOMPARE(tenpoint.fullRect(), QRectF(0, 0, 842, 595)); | 
| 159 |     QCOMPARE(tenpoint.fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 160 |     QCOMPARE(tenpoint.fullRectPoints(), QRect(0, 0, 842, 595)); | 
| 161 |     QCOMPARE(tenpoint.fullRectPixels(72), QRect(0, 0, 842, 595)); | 
| 162 |     QCOMPARE(tenpoint.paintRect(), QRectF(0, 0, 842, 595)); | 
| 163 |     QCOMPARE(tenpoint.paintRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210)); | 
| 164 |     QCOMPARE(tenpoint.paintRectPoints(), QRect(0, 0, 842, 595)); | 
| 165 |     QCOMPARE(tenpoint.paintRectPixels(72), QRect(0, 0, 842, 595)); | 
| 166 | } | 
| 167 |  | 
| 168 | void tst_QPageLayout::setGetMargins() | 
| 169 | { | 
| 170 |     // A4, 20pt margins | 
| 171 |     QMarginsF margins = QMarginsF(10, 10, 10, 10); | 
| 172 |     QMarginsF min = QMarginsF(10, 10, 10, 10); | 
| 173 |     QMarginsF max = QMarginsF(585, 832, 585, 832); | 
| 174 |     QPageLayout change = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, margins, QPageLayout::Point, min); | 
| 175 |     QCOMPARE(change.isValid(), true); | 
| 176 |     QCOMPARE(change.margins(), margins); | 
| 177 |     QCOMPARE(change.margins(QPageLayout::Millimeter), QMarginsF(3.53, 3.53, 3.53, 3.53)); | 
| 178 |     QCOMPARE(change.marginsPoints(), QMargins(10, 10, 10, 10)); | 
| 179 |     QCOMPARE(change.marginsPixels(72), QMargins(10, 10, 10, 10)); | 
| 180 |     QCOMPARE(change.minimumMargins(), min); | 
| 181 |     QCOMPARE(change.maximumMargins(), max); | 
| 182 |  | 
| 183 |     // Set magins within min/max ok | 
| 184 |     margins = QMarginsF(20, 20, 20, 20); | 
| 185 |     change.setMargins(margins); | 
| 186 |     QCOMPARE(change.margins(QPageLayout::Millimeter), QMarginsF(7.06, 7.06, 7.06, 7.06)); | 
| 187 |     QCOMPARE(change.marginsPoints(), QMargins(20, 20, 20, 20)); | 
| 188 |     QCOMPARE(change.marginsPixels(72), QMargins(20, 20, 20, 20)); | 
| 189 |     QCOMPARE(change.margins(), margins); | 
| 190 |  | 
| 191 |     // Set margins all below min is rejected | 
| 192 |     change.setMargins(QMarginsF(0, 0, 0, 0)); | 
| 193 |     QCOMPARE(change.margins(), margins); | 
| 194 |  | 
| 195 |     // Set margins all above max is rejected | 
| 196 |     change.setMargins(QMarginsF(1000, 1000, 1000, 1000)); | 
| 197 |     QCOMPARE(change.margins(), margins); | 
| 198 |  | 
| 199 |     // Only 1 wrong, set still rejects | 
| 200 |     change.setMargins(QMarginsF(50, 50, 50, 0)); | 
| 201 |     QCOMPARE(change.margins(), margins); | 
| 202 |  | 
| 203 |     // Set page size resets min/max, clamps existing margins | 
| 204 |     change.setMargins(change.maximumMargins()); | 
| 205 |     change.setPageSize(pageSize: QPageSize(QPageSize::A5)); | 
| 206 |     QCOMPARE(change.margins(), QMarginsF(420, 595, 420, 595)); | 
| 207 |     QCOMPARE(change.minimumMargins(), QMarginsF(0, 0, 0, 0)); | 
| 208 |     QCOMPARE(change.maximumMargins(), QMarginsF(420, 595, 420, 595)); | 
| 209 |  | 
| 210 |     // Set page size, sets min/max, clamps existing margins | 
| 211 |     margins = QMarginsF(20, 500, 20, 500); | 
| 212 |     change.setMargins(margins); | 
| 213 |     QCOMPARE(change.margins(), margins); | 
| 214 |     min = QMarginsF(30, 30, 30, 30); | 
| 215 |     max = QMarginsF(267, 390, 267, 390); | 
| 216 |     change.setPageSize(pageSize: QPageSize(QPageSize::A6)); | 
| 217 |     change.setMinimumMargins(min); | 
| 218 |     QCOMPARE(change.margins(), QMarginsF(30, 390, 30, 390)); | 
| 219 |     QCOMPARE(change.minimumMargins(), min); | 
| 220 |     QCOMPARE(change.maximumMargins(), max); | 
| 221 |  | 
| 222 |     // A4, 20pt margins | 
| 223 |     margins = QMarginsF(20, 20, 20, 20); | 
| 224 |     min = QMarginsF(10, 10, 10, 10); | 
| 225 |     max = QMarginsF(585, 832, 585, 832); | 
| 226 |     QPageLayout fullPage = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, margins, QPageLayout::Point, min); | 
| 227 |     fullPage.setMode(QPageLayout::FullPageMode); | 
| 228 |     QCOMPARE(fullPage.isValid(), true); | 
| 229 |     QCOMPARE(fullPage.margins(), margins); | 
| 230 |     QCOMPARE(fullPage.minimumMargins(), min); | 
| 231 |     QCOMPARE(fullPage.maximumMargins(), max); | 
| 232 |  | 
| 233 |     // Set margins within min/max ok | 
| 234 |     margins = QMarginsF(50, 50, 50, 50); | 
| 235 |     fullPage.setMargins(margins); | 
| 236 |     QCOMPARE(fullPage.margins(), margins); | 
| 237 |  | 
| 238 |     // Set margins all below min is accepted | 
| 239 |     margins = QMarginsF(0, 0, 0, 0); | 
| 240 |     fullPage.setMargins(margins); | 
| 241 |     QCOMPARE(fullPage.margins(), margins); | 
| 242 |  | 
| 243 |     // Set margins all above max is rejected | 
| 244 |     margins = QMarginsF(1000, 1000, 1000, 1000); | 
| 245 |     fullPage.setMargins(margins); | 
| 246 |     QCOMPARE(fullPage.margins(), margins); | 
| 247 |  | 
| 248 |     // Only 1 wrong, set still accepts | 
| 249 |     margins = QMarginsF(50, 50, 50, 0); | 
| 250 |     fullPage.setMargins(margins); | 
| 251 |     QCOMPARE(fullPage.margins(), margins); | 
| 252 |  | 
| 253 |     // Set page size, sets min/max, clamps existing margins | 
| 254 |     margins = QMarginsF(20, 500, 20, 500); | 
| 255 |     fullPage.setMargins(margins); | 
| 256 |     QCOMPARE(fullPage.margins(), margins); | 
| 257 |     min = QMarginsF(30, 30, 30, 30); | 
| 258 |     max = QMarginsF(267, 390, 267, 390); | 
| 259 |     fullPage.setPageSize(pageSize: QPageSize(QPageSize::A6)); | 
| 260 |     fullPage.setMinimumMargins(min); | 
| 261 |     QCOMPARE(fullPage.margins(), margins); | 
| 262 |     QCOMPARE(fullPage.minimumMargins(), min); | 
| 263 |     QCOMPARE(fullPage.maximumMargins(), max); | 
| 264 | } | 
| 265 |  | 
| 266 | QTEST_APPLESS_MAIN(tst_QPageLayout) | 
| 267 |  | 
| 268 | #include "tst_qpagelayout.moc" | 
| 269 |  |