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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include "qbrush.h"
#include <QPainter>
#include <QBitmap>
#include <qdebug.h>
//TESTED_CLASS=
//TESTED_FILES=
class tst_QBrush : public QObject
{
Q_OBJECT
public:
tst_QBrush();
private slots:
void operator_eq_eq();
void operator_eq_eq_data();
void stream();
void stream_data();
void badStyles();
void testQLinearGradientSetters();
void testQRadialGradientSetters();
void testQConicalGradientSetters();
void testQGradientCopyConstructor();
void gradientStops();
void textures();
void nullBrush();
void isOpaque();
void debug();
};
Q_DECLARE_METATYPE(QBrush)
tst_QBrush::tst_QBrush()
{
}
void tst_QBrush::operator_eq_eq_data()
{
QTest::addColumn<QBrush>("brush1");
QTest::addColumn<QBrush>("brush2");
QTest::addColumn<bool>("isEqual");
QLinearGradient lg(10, 10, 100, 100);
lg.setColorAt(0, Qt::red);
lg.setColorAt(0.5, Qt::blue);
lg.setColorAt(1, Qt::green);
QTest::newRow("black vs black") << QBrush(Qt::black) << QBrush(Qt::black) << true;
QTest::newRow("black vs blue") << QBrush(Qt::black) << QBrush(Qt::blue) << false;
QTest::newRow("red vs no") << QBrush(Qt::red) << QBrush(Qt::NoBrush) << false;
QTest::newRow("no vs no") << QBrush(Qt::NoBrush) << QBrush(Qt::NoBrush) << true;
QTest::newRow("lg vs same lg") << QBrush(lg) << QBrush(lg) << true;
QTest::newRow("lg vs diff lg") << QBrush(lg) << QBrush(QLinearGradient(QPoint(0, 0), QPoint(1, 1)))
<< false;
QTest::newRow("rad vs con") << QBrush(QRadialGradient(0, 0, 0, 0, 0)) << QBrush(QConicalGradient(0, 0, 0)) << false;
QBrush b1(lg);
QBrush b2(lg);
b1.setTransform(QTransform().scale(2, 2));
QTest::newRow("lg with transform vs same lg") << b1 << b2 << false;
b2.setTransform(QTransform().scale(2, 2));
QTest::newRow("lg w/transform vs same lg w/same transform") << b1 << b2 << true;
}
void tst_QBrush::operator_eq_eq()
{
QFETCH(QBrush, brush1);
QFETCH(QBrush, brush2);
QFETCH(bool, isEqual);
QCOMPARE(brush1 == brush2, isEqual);
}
void tst_QBrush::stream_data()
{
QTest::addColumn<QBrush>("brush");
QLinearGradient lg(10, 10, 100, 100);
lg.setColorAt(0, Qt::red);
lg.setColorAt(0.5, Qt::blue);
lg.setColorAt(1, Qt::green);
QTest::newRow("black") << QBrush(Qt::black);
QTest::newRow("red") << QBrush(Qt::red);
QTest::newRow("no") << QBrush(Qt::NoBrush);
QTest::newRow("lg") << QBrush(lg);
QTest::newRow("rad") << QBrush(QRadialGradient(0, 0, 0, 0, 0));
QTest::newRow("con") << QBrush(QConicalGradient(0, 0, 0));
}
void tst_QBrush::stream()
{
QFETCH(QBrush, brush);
QByteArray data;
{
QDataStream stream(&data, QIODevice::WriteOnly);
stream << brush;
}
QBrush cmp;
{
QDataStream stream(&data, QIODevice::ReadOnly);
stream >> cmp;
}
QCOMPARE(brush.style(), cmp.style());
QCOMPARE(brush.color(), cmp.color());
QCOMPARE(brush, cmp);
}
void tst_QBrush::testQLinearGradientSetters()
{
QLinearGradient lg;
QCOMPARE(lg.start(), QPointF(0, 0));
QCOMPARE(lg.finalStop(), QPointF(1, 1));
lg.setStart(101, 102);
QCOMPARE(lg.start(), QPointF(101, 102));
lg.setStart(QPointF(201, 202));
QCOMPARE(lg.start(), QPointF(201, 202));
lg.setFinalStop(103, 104);
QCOMPARE(lg.finalStop(), QPointF(103, 104));
lg.setFinalStop(QPointF(203, 204));
QCOMPARE(lg.finalStop(), QPointF(203, 204));
}
void tst_QBrush::testQRadialGradientSetters()
{
QRadialGradient rg;
QCOMPARE(rg.radius(), qreal(1.0));
QCOMPARE(rg.center(), QPointF(0, 0));
QCOMPARE(rg.focalPoint(), QPointF(0, 0));
rg.setRadius(100);
QCOMPARE(rg.radius(), qreal(100.0));
rg.setCenter(101, 102);
QCOMPARE(rg.center(), QPointF(101, 102));
rg.setCenter(QPointF(201, 202));
QCOMPARE(rg.center(), QPointF(201, 202));
rg.setFocalPoint(103, 104);
QCOMPARE(rg.focalPoint(), QPointF(103, 104));
rg.setFocalPoint(QPointF(203, 204));
QCOMPARE(rg.focalPoint(), QPointF(203, 204));
}
void tst_QBrush::testQConicalGradientSetters()
{
QConicalGradient cg;
QCOMPARE(cg.angle(), qreal(0.0));
QCOMPARE(cg.center(), QPointF(0, 0));
cg.setAngle(100);
QCOMPARE(cg.angle(), qreal(100.0));
cg.setCenter(102, 103);
QCOMPARE(cg.center(), QPointF(102, 103));
cg.setCenter(QPointF(202, 203));
QCOMPARE(cg.center(), QPointF(202, 203));
}
void tst_QBrush::testQGradientCopyConstructor()
{
{
QLinearGradient lg1(101, 102, 103, 104);
QLinearGradient lg2 = lg1;
QCOMPARE(lg1.start(), lg2.start());
QCOMPARE(lg1.finalStop(), lg2.finalStop());
QGradient g = lg1;
QCOMPARE(((QLinearGradient *) &g)->start(), lg1.start());
QCOMPARE(((QLinearGradient *) &g)->finalStop(), lg1.finalStop());
}
{
QRadialGradient rg1(101, 102, 103, 104, 105);
QRadialGradient rg2 = rg1;
QCOMPARE(rg1.center(), rg2.center());
QCOMPARE(rg1.focalPoint(), rg2.focalPoint());
QCOMPARE(rg1.radius(), rg2.radius());
QGradient g = rg1;
QCOMPARE(((QRadialGradient *) &g)->center(), rg1.center());
QCOMPARE(((QRadialGradient *) &g)->focalPoint(), rg1.focalPoint());
QCOMPARE(((QRadialGradient *) &g)->radius(), rg1.radius());
}
{
QConicalGradient cg1(101, 102, 103);
QConicalGradient cg2 = cg1;
QCOMPARE(cg1.center(), cg2.center());
QCOMPARE(cg1.angle(), cg2.angle());
QGradient g = cg1;
QCOMPARE(((QConicalGradient *) &g)->center(), cg1.center());
QCOMPARE(((QConicalGradient *) &g)->angle(), cg1.angle());
}
}
void tst_QBrush::badStyles()
{
// QBrush(Qt::BrushStyle) constructor
QCOMPARE(QBrush(Qt::LinearGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(Qt::RadialGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(Qt::ConicalGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(Qt::TexturePattern).style(), Qt::NoBrush);
// QBrush(QColor, Qt::BrushStyle) constructor
QCOMPARE(QBrush(QColor(0, 0, 0), Qt::LinearGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(QColor(0, 0, 0), Qt::RadialGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(QColor(0, 0, 0), Qt::ConicalGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(QColor(0, 0, 0), Qt::TexturePattern).style(), Qt::NoBrush);
// QBrush(Qt::GlobalColor, Qt::BrushStyle) constructor
QCOMPARE(QBrush(Qt::black, Qt::LinearGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(Qt::black, Qt::RadialGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(Qt::black, Qt::ConicalGradientPattern).style(), Qt::NoBrush);
QCOMPARE(QBrush(Qt::black, Qt::TexturePattern).style(), Qt::NoBrush);
// Set style...
QBrush brush(Qt::red);
brush.setStyle(Qt::LinearGradientPattern);
QCOMPARE(brush.style(), Qt::SolidPattern);
brush.setStyle(Qt::RadialGradientPattern);
QCOMPARE(brush.style(), Qt::SolidPattern);
brush.setStyle(Qt::ConicalGradientPattern);
QCOMPARE(brush.style(), Qt::SolidPattern);
brush.setStyle(Qt::TexturePattern);
QCOMPARE(brush.style(), Qt::SolidPattern);
}
void tst_QBrush::gradientStops()
{
QLinearGradient gradient;
gradient.setColorAt(0, Qt::red);
gradient.setColorAt(1, Qt::blue);
QCOMPARE(gradient.stops().size(), 2);
QCOMPARE(gradient.stops().at(0), QGradientStop(0, QColor(Qt::red)));
QCOMPARE(gradient.stops().at(1), QGradientStop(1, QColor(Qt::blue)));
gradient.setColorAt(0, Qt::blue);
gradient.setColorAt(1, Qt::red);
QCOMPARE(gradient.stops().size(), 2);
QCOMPARE(gradient.stops().at(0), QGradientStop(0, QColor(Qt::blue)));
QCOMPARE(gradient.stops().at(1), QGradientStop(1, QColor(Qt::red)));
gradient.setColorAt(0.5, Qt::green);
QCOMPARE(gradient.stops().size(), 3);
QCOMPARE(gradient.stops().at(1), QGradientStop(0.5, QColor(Qt::green)));
// A hack in parseStopNode() in qsvghandler.cpp depends on inserting stops at NaN.
gradient.setStops(QGradientStops() << QGradientStop(qQNaN(), QColor()));
QCOMPARE(gradient.stops().size(), 1);
QVERIFY(qIsNaN(gradient.stops().at(0).first));
QCOMPARE(gradient.stops().at(0).second, QColor());
}
void fill(QPaintDevice *pd) {
QPainter p(pd);
int w = pd->width();
int h = pd->height();
p.fillRect(0, 0, w, h, Qt::white);
p.fillRect(0, 0, w/3, h/3, Qt::black);
}
void tst_QBrush::textures()
{
QPixmap pixmap_source(10, 10);
QImage image_source(10, 10, QImage::Format_RGB32);
fill(&pixmap_source);
fill(&image_source);
// Create a pixmap brush and compare its texture and textureImage
// to the expected image
QBrush pixmap_brush;
pixmap_brush.setTexture(pixmap_source);
QImage image = pixmap_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
QCOMPARE(image, image_source);
image = pixmap_brush.textureImage().convertToFormat(QImage::Format_RGB32);
QCOMPARE(image, image_source);
pixmap_brush = QBrush(pixmap_source);
image = pixmap_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
QCOMPARE(image, image_source);
image = pixmap_brush.textureImage().convertToFormat(QImage::Format_RGB32);
QCOMPARE(image, image_source);
// Create a image brush and compare its texture and textureImage
// to the expected image
QBrush image_brush;
image_brush.setTextureImage(image_source);
image = image_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
QCOMPARE(image, image_source);
QCOMPARE(image_brush.textureImage(), image_source);
image_brush = QBrush(image_source);
image = image_brush.texture().toImage().convertToFormat(QImage::Format_RGB32);
QCOMPARE(image, image_source);
QCOMPARE(image_brush.textureImage(), image_source);
}
void tst_QBrush::nullBrush()
{
QBrush brush(QColor(100,0,0), Qt::NoBrush);
QCOMPARE(brush.color(), QColor(100,0,0));
}
void tst_QBrush::isOpaque()
{
QBitmap bm(8, 8);
bm.fill(Qt::black);
QBrush brush(bm);
QVERIFY(!brush.isOpaque());
}
void tst_QBrush::debug()
{
QPixmap pixmap_source(10, 10);
fill(&pixmap_source);
QBrush pixmap_brush;
pixmap_brush.setTexture(pixmap_source);
QCOMPARE(pixmap_brush.style(), Qt::TexturePattern);
qDebug() << pixmap_brush; // don't crash
}
QTEST_MAIN(tst_QBrush)
#include "tst_qbrush.moc"
|