1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTEST_GUI_H |
5 | #define QTEST_GUI_H |
6 | |
7 | // enable GUI features |
8 | #ifndef QT_GUI_LIB |
9 | #define QT_GUI_LIB |
10 | #endif |
11 | #if 0 |
12 | #pragma qt_class(QtTestGui) |
13 | #endif |
14 | |
15 | #include <QtTest/qtestassert.h> |
16 | #include <QtTest/qtest.h> |
17 | #include <QtTest/qtestevent.h> |
18 | #include <QtTest/qtestmouse.h> |
19 | #include <QtTest/qtesttouch.h> |
20 | #include <QtTest/qtestkeyboard.h> |
21 | |
22 | #include <QtGui/qcolor.h> |
23 | #include <QtGui/qpixmap.h> |
24 | #include <QtGui/qimage.h> |
25 | #if QT_CONFIG(shortcut) |
26 | #include <QtGui/qkeysequence.h> |
27 | #endif |
28 | #include <QtGui/qregion.h> |
29 | #include <QtGui/qvector2d.h> |
30 | #include <QtGui/qvector3d.h> |
31 | #include <QtGui/qvector4d.h> |
32 | #include <QtGui/qicon.h> |
33 | |
34 | #if 0 |
35 | // inform syncqt |
36 | #pragma qt_no_master_include |
37 | #endif |
38 | |
39 | QT_BEGIN_NAMESPACE |
40 | |
41 | |
42 | namespace QTest |
43 | { |
44 | |
45 | template<> inline char *toString(const QColor &color) |
46 | { |
47 | return qstrdup(color.name(format: QColor::HexArgb).toLocal8Bit().constData()); |
48 | } |
49 | |
50 | template<> inline char *toString(const QRegion ®ion) |
51 | { |
52 | QByteArray result = "QRegion(" ; |
53 | if (region.isNull()) { |
54 | result += "null" ; |
55 | } else if (region.isEmpty()) { |
56 | result += "empty" ; |
57 | } else { |
58 | const auto rects = region.begin(); |
59 | const int rectCount = region.rectCount(); |
60 | if (rectCount > 1) { |
61 | result += QByteArray::number(rectCount); |
62 | result += " rectangles, " ; |
63 | } |
64 | for (int i = 0; i < rectCount; ++i) { |
65 | if (i) |
66 | result += ", " ; |
67 | const QRect &r = rects[i]; |
68 | result += QByteArray::number(r.width()); |
69 | result += 'x'; |
70 | result += QByteArray::number(r.height()); |
71 | if (r.x() >= 0) |
72 | result += '+'; |
73 | result += QByteArray::number(r.x()); |
74 | if (r.y() >= 0) |
75 | result += '+'; |
76 | result += QByteArray::number(r.y()); |
77 | } |
78 | } |
79 | result += ')'; |
80 | return qstrdup(result.constData()); |
81 | } |
82 | |
83 | #if !defined(QT_NO_VECTOR2D) || defined(Q_QDOC) |
84 | template<> inline char *toString(const QVector2D &v) |
85 | { |
86 | QByteArray result = "QVector2D(" + QByteArray::number(double(v.x())) + ", " |
87 | + QByteArray::number(double(v.y())) + ')'; |
88 | return qstrdup(result.constData()); |
89 | } |
90 | #endif // !QT_NO_VECTOR2D |
91 | #if !defined(QT_NO_VECTOR3D) || defined(Q_QDOC) |
92 | template<> inline char *toString(const QVector3D &v) |
93 | { |
94 | QByteArray result = "QVector3D(" + QByteArray::number(double(v.x())) + ", " |
95 | + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z())) + ')'; |
96 | return qstrdup(result.constData()); |
97 | } |
98 | #endif // !QT_NO_VECTOR3D |
99 | #if !defined(QT_NO_VECTOR4D) || defined(Q_QDOC) |
100 | template<> inline char *toString(const QVector4D &v) |
101 | { |
102 | QByteArray result = "QVector4D(" + QByteArray::number(double(v.x())) + ", " |
103 | + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z())) |
104 | + ", " + QByteArray::number(double(v.w())) + ')'; |
105 | return qstrdup(result.constData()); |
106 | } |
107 | #endif // !QT_NO_VECTOR4D |
108 | |
109 | #if QT_CONFIG(shortcut) |
110 | template<> inline char *toString(const QKeySequence &keySequence) |
111 | { |
112 | return toString(str: keySequence.toString()); |
113 | } |
114 | #endif |
115 | |
116 | inline bool qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected, |
117 | const char *file, int line) |
118 | { |
119 | QTEST_ASSERT(sizeof(QIcon) == sizeof(void *)); |
120 | return qCompare(t1: *reinterpret_cast<void * const *>(&t1), |
121 | t2: *reinterpret_cast<void * const *>(&t2), actual, expected, file, line); |
122 | } |
123 | |
124 | inline bool qCompare(QImage const &t1, QImage const &t2, |
125 | const char *actual, const char *expected, const char *file, int line) |
126 | { |
127 | char msg[1024]; |
128 | msg[0] = '\0'; |
129 | const bool t1Null = t1.isNull(); |
130 | const bool t2Null = t2.isNull(); |
131 | if (t1Null != t2Null) { |
132 | qsnprintf(str: msg, n: 1024, fmt: "Compared QImages differ.\n" |
133 | " Actual (%s).isNull(): %d\n" |
134 | " Expected (%s).isNull(): %d" , actual, t1Null, expected, t2Null); |
135 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
136 | } |
137 | if (t1Null && t2Null) |
138 | return compare_helper(success: true, failureMsg: nullptr, actual, expected, file, line); |
139 | if (!qFuzzyCompare(p1: t1.devicePixelRatio(), p2: t2.devicePixelRatio())) { |
140 | qsnprintf(str: msg, n: 1024, fmt: "Compared QImages differ in device pixel ratio.\n" |
141 | " Actual (%s): %g\n" |
142 | " Expected (%s): %g" , |
143 | actual, t1.devicePixelRatio(), |
144 | expected, t2.devicePixelRatio()); |
145 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
146 | } |
147 | if (t1.width() != t2.width() || t1.height() != t2.height()) { |
148 | qsnprintf(str: msg, n: 1024, fmt: "Compared QImages differ in size.\n" |
149 | " Actual (%s): %dx%d\n" |
150 | " Expected (%s): %dx%d" , |
151 | actual, t1.width(), t1.height(), |
152 | expected, t2.width(), t2.height()); |
153 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
154 | } |
155 | if (t1.format() != t2.format()) { |
156 | qsnprintf(str: msg, n: 1024, fmt: "Compared QImages differ in format.\n" |
157 | " Actual (%s): %d\n" |
158 | " Expected (%s): %d" , |
159 | actual, t1.format(), expected, t2.format()); |
160 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
161 | } |
162 | return compare_helper(success: t1 == t2, failureMsg: "Compared values are not the same" , |
163 | actual, expected, file, line); |
164 | } |
165 | |
166 | inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected, |
167 | const char *file, int line) |
168 | { |
169 | char msg[1024]; |
170 | msg[0] = '\0'; |
171 | const bool t1Null = t1.isNull(); |
172 | const bool t2Null = t2.isNull(); |
173 | if (t1Null != t2Null) { |
174 | qsnprintf(str: msg, n: 1024, fmt: "Compared QPixmaps differ.\n" |
175 | " Actual (%s).isNull(): %d\n" |
176 | " Expected (%s).isNull(): %d" , actual, t1Null, expected, t2Null); |
177 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
178 | } |
179 | if (t1Null && t2Null) |
180 | return compare_helper(success: true, failureMsg: nullptr, actual, expected, file, line); |
181 | if (!qFuzzyCompare(p1: t1.devicePixelRatio(), p2: t2.devicePixelRatio())) { |
182 | qsnprintf(str: msg, n: 1024, fmt: "Compared QPixmaps differ in device pixel ratio.\n" |
183 | " Actual (%s): %g\n" |
184 | " Expected (%s): %g" , |
185 | actual, t1.devicePixelRatio(), |
186 | expected, t2.devicePixelRatio()); |
187 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
188 | } |
189 | if (t1.width() != t2.width() || t1.height() != t2.height()) { |
190 | qsnprintf(str: msg, n: 1024, fmt: "Compared QPixmaps differ in size.\n" |
191 | " Actual (%s): %dx%d\n" |
192 | " Expected (%s): %dx%d" , |
193 | actual, t1.width(), t1.height(), |
194 | expected, t2.width(), t2.height()); |
195 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
196 | } |
197 | return qCompare(t1: t1.toImage(), t2: t2.toImage(), actual, expected, file, line); |
198 | } |
199 | |
200 | } |
201 | |
202 | QT_END_NAMESPACE |
203 | |
204 | #endif |
205 | |