| 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 |
|
| 30 |
#include <QtTest/QtTest> |
| 31 |
|
| 32 |
#include "qpen.h" |
| 33 |
#include "qbrush.h" |
| 34 |
|
| 35 |
#include <qdebug.h> |
| 36 |
|
| 37 |
class tst_QPen : public QObject |
| 38 |
{ |
| 39 |
Q_OBJECT |
| 40 |
|
| 41 |
public: |
| 42 |
tst_QPen(); |
| 43 |
|
| 44 |
private slots: |
| 45 |
void getSetCheck(); |
| 46 |
void swap(); |
| 47 |
void move(); |
| 48 |
void move_assign(); |
| 49 |
void operator_eq_eq(); |
| 50 |
void operator_eq_eq_data(); |
| 51 |
|
| 52 |
void stream(); |
| 53 |
void stream_data(); |
| 54 |
|
| 55 |
void constructor(); |
| 56 |
void constructor_data(); |
| 57 |
}; |
| 58 |
|
| 59 |
// Testing get/set functions |
| 60 |
void tst_QPen::getSetCheck() |
| 61 |
{ |
| 62 |
QPen obj1; |
| 63 |
// qreal QPen::miterLimit() |
| 64 |
// void QPen::setMiterLimit(qreal) |
| 65 |
obj1.setMiterLimit(0.0); |
| 66 |
QCOMPARE(0.0, obj1.miterLimit()); |
| 67 |
obj1.setMiterLimit(qreal(1.1)); |
| 68 |
QCOMPARE(qreal(1.1), obj1.miterLimit()); |
| 69 |
|
| 70 |
// qreal QPen::widthF() |
| 71 |
// void QPen::setWidthF(qreal) |
| 72 |
obj1.setWidthF(0.0); |
| 73 |
QCOMPARE(0.0, obj1.widthF()); |
| 74 |
obj1.setWidthF(qreal(1.1)); |
| 75 |
QCOMPARE(qreal(1.1), obj1.widthF()); |
| 76 |
|
| 77 |
// int QPen::width() |
| 78 |
// void QPen::setWidth(int) |
| 79 |
for (int i = 0; i < 100; ++i) { |
| 80 |
obj1.setWidth(i); |
| 81 |
QCOMPARE(i, obj1.width()); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void tst_QPen::swap() |
| 86 |
{ |
| 87 |
QPen p1(Qt::black), p2(Qt::white); |
| 88 |
p1.swap(p2); |
| 89 |
QCOMPARE(p1.color(), QColor(Qt::white)); |
| 90 |
QCOMPARE(p2.color(), QColor(Qt::black)); |
| 91 |
} |
| 92 |
|
| 93 |
void tst_QPen::move() |
| 94 |
{ |
| 95 |
QPen p1(Qt::black); |
| 96 |
|
| 97 |
// check that moving does the right thing: |
| 98 |
QPen p2 = std::move(p1); // could be move or copy construction, so don't check p1's state |
| 99 |
QCOMPARE(p2.color(), QColor(Qt::black)); |
| 100 |
|
| 101 |
// this, executed ehre, would crash: |
| 102 |
// QVERIFY(p1.style() != Qt::NoPen); |
| 103 |
|
| 104 |
// check that moved-from QPen p1 can still be safely copied: |
| 105 |
const QPen p3 = p1; |
| 106 |
|
| 107 |
// check that moved-from QPen p1 can still be safely assigned to: |
| 108 |
const QPen p4(Qt::yellow); |
| 109 |
p1 = p4; |
| 110 |
QCOMPARE(p1.color(), QColor(Qt::yellow)); |
| 111 |
|
| 112 |
// check that moved-from QPens p2, p3 can still be safely destroyed: |
| 113 |
QPen p5 = std::move(p2); |
| 114 |
|
| 115 |
// intentionally no more statements beyond this point |
| 116 |
} |
| 117 |
|
| 118 |
void tst_QPen::move_assign() |
| 119 |
{ |
| 120 |
QPen p1(Qt::black), p2(Qt::white); |
| 121 |
|
| 122 |
// check that moving does the right thing: |
| 123 |
p2 = std::move(p1); // could be move or copy assignment, so don't check p1's state |
| 124 |
QCOMPARE(p2.color(), QColor(Qt::black)); |
| 125 |
|
| 126 |
// check that move-assigned-from QPen p1 can still be used, albeit |
| 127 |
// with undocumented state (it's p2's original state): |
| 128 |
QVERIFY(p1.style() != Qt::NoPen); |
| 129 |
|
| 130 |
// check that moved-from QPen p1 can still be safely copied: |
| 131 |
const QPen p3 = p1; |
| 132 |
|
| 133 |
// check that moved-from QPen p1 can still be safely assigned to: |
| 134 |
const QPen p4(Qt::yellow); |
| 135 |
p1 = p4; |
| 136 |
QCOMPARE(p1.color(), QColor(Qt::yellow)); |
| 137 |
|
| 138 |
// check that moved-from QPens p2, p3 can still be safely destroyed: |
| 139 |
QPen p5; |
| 140 |
p5 = std::move(p2); |
| 141 |
|
| 142 |
// intentionally no more statements beyond this point |
| 143 |
} |
| 144 |
|
| 145 |
tst_QPen::tst_QPen() |
| 146 |
|
| 147 |
{ |
| 148 |
} |
| 149 |
|
| 150 |
void tst_QPen::operator_eq_eq_data() |
| 151 |
{ |
| 152 |
QTest::addColumn<QPen>("pen1"); |
| 153 |
QTest::addColumn<QPen>("pen2"); |
| 154 |
QTest::addColumn<bool>("isEqual"); |
| 155 |
|
| 156 |
QTest::newRow("differentColor") << QPen(Qt::red) |
| 157 |
<< QPen(Qt::blue) |
| 158 |
<< false; |
| 159 |
QTest::newRow("differentWidth") << QPen(Qt::red, 2) |
| 160 |
<< QPen(Qt::red, 3) |
| 161 |
<< false; |
| 162 |
QTest::newRow("differentPenStyle") << QPen(Qt::red, 2, Qt::DashLine) |
| 163 |
<< QPen(Qt::red, 2, Qt::DotLine) |
| 164 |
<< false; |
| 165 |
QTest::newRow("differentCapStyle") << QPen(Qt::red, 2, Qt::DashLine, Qt::RoundCap, Qt::BevelJoin) |
| 166 |
<< QPen(Qt::red, 2, Qt::DashLine, Qt::SquareCap, Qt::BevelJoin) |
| 167 |
<< false; |
| 168 |
QTest::newRow("differentJoinStyle") << QPen(Qt::red, 2, Qt::DashLine, Qt::RoundCap, Qt::BevelJoin) |
| 169 |
<< QPen(Qt::red, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin) |
| 170 |
<< false; |
| 171 |
QTest::newRow("same") << QPen(Qt::red, 2, Qt::DashLine, Qt::RoundCap, Qt::BevelJoin) |
| 172 |
<< QPen(Qt::red, 2, Qt::DashLine, Qt::RoundCap, Qt::BevelJoin) |
| 173 |
<< true; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
void tst_QPen::operator_eq_eq() |
| 178 |
{ |
| 179 |
QFETCH(QPen, pen1); |
| 180 |
QFETCH(QPen, pen2); |
| 181 |
QFETCH(bool, isEqual); |
| 182 |
QCOMPARE(pen1 == pen2, isEqual); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
void tst_QPen::constructor_data() |
| 187 |
{ |
| 188 |
QTest::addColumn<QPen>("pen"); |
| 189 |
QTest::addColumn<QBrush>("brush"); |
| 190 |
QTest::addColumn<double>("width"); |
| 191 |
QTest::addColumn<int>("style"); |
| 192 |
QTest::addColumn<int>("capStyle"); |
| 193 |
QTest::addColumn<int>("joinStyle"); |
| 194 |
|
| 195 |
QTest::newRow("solid_black") << QPen() << QBrush(Qt::black) << 1. << (int)Qt::SolidLine |
| 196 |
<< (int) Qt::SquareCap << (int)Qt::BevelJoin; |
| 197 |
QTest::newRow("solid_red") << QPen(Qt::red) << QBrush(Qt::red) << 1. << (int)Qt::SolidLine |
| 198 |
<< (int)Qt::SquareCap << (int)Qt::BevelJoin; |
| 199 |
QTest::newRow("full") << QPen(QBrush(QLinearGradient(0, 0, 100, 100)), 10, |
| 200 |
Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin) |
| 201 |
<< QBrush(QLinearGradient(0, 0, 100, 100)) << 10. << (int)Qt::SolidLine |
| 202 |
<< (int)Qt::RoundCap << (int)Qt::MiterJoin; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
void tst_QPen::constructor() |
| 208 |
{ |
| 209 |
QFETCH(QPen, pen); |
| 210 |
QFETCH(QBrush, brush); |
| 211 |
QFETCH(double, width); |
| 212 |
QFETCH(int, style); |
| 213 |
QFETCH(int, capStyle); |
| 214 |
QFETCH(int, joinStyle); |
| 215 |
|
| 216 |
QCOMPARE(pen.style(), Qt::PenStyle(style)); |
| 217 |
QCOMPARE(pen.capStyle(), Qt::PenCapStyle(capStyle)); |
| 218 |
QCOMPARE(pen.joinStyle(), Qt::PenJoinStyle(joinStyle)); |
| 219 |
QCOMPARE(pen.widthF(), width); |
| 220 |
QCOMPARE(pen.brush(), brush); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
void tst_QPen::stream_data() |
| 225 |
{ |
| 226 |
QTest::addColumn<QPen>("pen"); |
| 227 |
|
| 228 |
QTest::newRow("solid_black") << QPen(); |
| 229 |
QTest::newRow("solid_red") << QPen(Qt::red); |
| 230 |
QTest::newRow("full") << QPen(QBrush(QLinearGradient(0, 0, 100, 100)), 10, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
void tst_QPen::stream() |
| 235 |
{ |
| 236 |
QFETCH(QPen, pen); |
| 237 |
|
| 238 |
QByteArray bytes; |
| 239 |
|
| 240 |
{ |
| 241 |
QDataStream stream(&bytes, QIODevice::WriteOnly); |
| 242 |
stream << pen; |
| 243 |
} |
| 244 |
|
| 245 |
QPen cmp; |
| 246 |
{ |
| 247 |
QDataStream stream(&bytes, QIODevice::ReadOnly); |
| 248 |
stream >> cmp; |
| 249 |
} |
| 250 |
|
| 251 |
QCOMPARE(pen.widthF(), cmp.widthF()); |
| 252 |
QCOMPARE(pen.style(), cmp.style()); |
| 253 |
QCOMPARE(pen.capStyle(), cmp.capStyle()); |
| 254 |
QCOMPARE(pen.joinStyle(), cmp.joinStyle()); |
| 255 |
QCOMPARE(pen.brush(), cmp.brush()); |
| 256 |
|
| 257 |
QCOMPARE(pen, cmp); |
| 258 |
} |
| 259 |
|
| 260 |
QTEST_APPLESS_MAIN(tst_QPen) |
| 261 |
#include "tst_qpen.moc" |