Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtest_gui.h
Go to the documentation of this file.
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/qtestwheel.h>
21#include <QtTest/qtestkeyboard.h>
22
23#include <QtGui/qcolor.h>
24#include <QtGui/qpixmap.h>
25#include <QtGui/qimage.h>
26#if QT_CONFIG(shortcut)
27#include <QtGui/qkeysequence.h>
28#endif
29#include <QtGui/qregion.h>
30#include <QtGui/qvector2d.h>
31#include <QtGui/qvector3d.h>
32#include <QtGui/qvector4d.h>
33#include <QtGui/qicon.h>
34
35#if 0
36// inform syncqt
37#pragma qt_no_master_include
38#endif
39
41
42
43namespace QTest
44{
45
46template<> inline char *toString(const QColor &color)
47{
48 return qstrdup(color.name(QColor::HexArgb).toLocal8Bit().constData());
49}
50
51template<> inline char *toString(const QRegion &region)
52{
53 QByteArray result = "QRegion(";
54 if (region.isNull()) {
55 result += "null";
56 } else if (region.isEmpty()) {
57 result += "empty";
58 } else {
59 const auto rects = region.begin();
60 const int rectCount = region.rectCount();
61 if (rectCount > 1) {
62 result += QByteArray::number(rectCount);
63 result += " rectangles, ";
64 }
65 for (int i = 0; i < rectCount; ++i) {
66 if (i)
67 result += ", ";
68 const QRect &r = rects[i];
69 result += QByteArray::number(r.width());
70 result += 'x';
71 result += QByteArray::number(r.height());
72 if (r.x() >= 0)
73 result += '+';
75 if (r.y() >= 0)
76 result += '+';
78 }
79 }
80 result += ')';
81 return qstrdup(result.constData());
82}
83
84#if !defined(QT_NO_VECTOR2D) || defined(Q_QDOC)
85template<> inline char *toString(const QVector2D &v)
86{
87 QByteArray result = "QVector2D(" + QByteArray::number(double(v.x())) + ", "
88 + QByteArray::number(double(v.y())) + ')';
89 return qstrdup(result.constData());
90}
91#endif // !QT_NO_VECTOR2D
92#if !defined(QT_NO_VECTOR3D) || defined(Q_QDOC)
93template<> inline char *toString(const QVector3D &v)
94{
95 QByteArray result = "QVector3D(" + QByteArray::number(double(v.x())) + ", "
96 + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z())) + ')';
97 return qstrdup(result.constData());
98}
99#endif // !QT_NO_VECTOR3D
100#if !defined(QT_NO_VECTOR4D) || defined(Q_QDOC)
101template<> inline char *toString(const QVector4D &v)
102{
103 QByteArray result = "QVector4D(" + QByteArray::number(double(v.x())) + ", "
104 + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z()))
105 + ", " + QByteArray::number(double(v.w())) + ')';
106 return qstrdup(result.constData());
107}
108#endif // !QT_NO_VECTOR4D
109
110#if QT_CONFIG(shortcut)
111template<> inline char *toString(const QKeySequence &keySequence)
112{
113 return toString(keySequence.toString());
114}
115#endif
116
117inline bool qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected,
118 const char *file, int line)
119{
120 QTEST_ASSERT(sizeof(QIcon) == sizeof(void *));
121 return qCompare(*reinterpret_cast<void * const *>(&t1),
122 *reinterpret_cast<void * const *>(&t2), actual, expected, file, line);
123}
124
125inline bool qCompare(QImage const &t1, QImage const &t2,
126 const char *actual, const char *expected, const char *file, int line)
127{
128 char msg[1024];
129 msg[0] = '\0';
130 const bool t1Null = t1.isNull();
131 const bool t2Null = t2.isNull();
132 if (t1Null != t2Null) {
133 qsnprintf(msg, 1024, "Compared QImages differ.\n"
134 " Actual (%s).isNull(): %d\n"
135 " Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
136 return compare_helper(false, msg, actual, expected, file, line);
137 }
138 if (t1Null && t2Null)
139 return compare_helper(true, nullptr, actual, expected, file, line);
140 if (!qFuzzyCompare(t1.devicePixelRatio(), t2.devicePixelRatio())) {
141 qsnprintf(msg, 1024, "Compared QImages differ in device pixel ratio.\n"
142 " Actual (%s): %g\n"
143 " Expected (%s): %g",
144 actual, t1.devicePixelRatio(),
145 expected, t2.devicePixelRatio());
146 return compare_helper(false, msg, actual, expected, file, line);
147 }
148 if (t1.width() != t2.width() || t1.height() != t2.height()) {
149 qsnprintf(msg, 1024, "Compared QImages differ in size.\n"
150 " Actual (%s): %dx%d\n"
151 " Expected (%s): %dx%d",
152 actual, t1.width(), t1.height(),
153 expected, t2.width(), t2.height());
154 return compare_helper(false, msg, actual, expected, file, line);
155 }
156 if (t1.format() != t2.format()) {
157 qsnprintf(msg, 1024, "Compared QImages differ in format.\n"
158 " Actual (%s): %d\n"
159 " Expected (%s): %d",
160 actual, t1.format(), expected, t2.format());
161 return compare_helper(false, msg, actual, expected, file, line);
162 }
163 return compare_helper(t1 == t2, "Compared values are not the same",
164 actual, expected, file, line);
165}
166
167inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected,
168 const char *file, int line)
169{
170 char msg[1024];
171 msg[0] = '\0';
172 const bool t1Null = t1.isNull();
173 const bool t2Null = t2.isNull();
174 if (t1Null != t2Null) {
175 qsnprintf(msg, 1024, "Compared QPixmaps differ.\n"
176 " Actual (%s).isNull(): %d\n"
177 " Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
178 return compare_helper(false, msg, actual, expected, file, line);
179 }
180 if (t1Null && t2Null)
181 return compare_helper(true, nullptr, actual, expected, file, line);
182 if (!qFuzzyCompare(t1.devicePixelRatio(), t2.devicePixelRatio())) {
183 qsnprintf(msg, 1024, "Compared QPixmaps differ in device pixel ratio.\n"
184 " Actual (%s): %g\n"
185 " Expected (%s): %g",
186 actual, t1.devicePixelRatio(),
187 expected, t2.devicePixelRatio());
188 return compare_helper(false, msg, actual, expected, file, line);
189 }
190 if (t1.width() != t2.width() || t1.height() != t2.height()) {
191 qsnprintf(msg, 1024, "Compared QPixmaps differ in size.\n"
192 " Actual (%s): %dx%d\n"
193 " Expected (%s): %dx%d",
194 actual, t1.width(), t1.height(),
195 expected, t2.width(), t2.height());
196 return compare_helper(false, msg, actual, expected, file, line);
197 }
198 return qCompare(t1.toImage(), t2.toImage(), actual, expected, file, line);
199}
200
201}
202
204
205#endif
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
@ HexArgb
Definition qcolor.h:36
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
\inmodule QtGui
Definition qimage.h:37
The QKeySequence class encapsulates a key sequence as used by shortcuts.
QString toString(SequenceFormat format=PortableText) const
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore\reentrant
Definition qrect.h:30
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
int rectCount() const noexcept
bool isNull() const
bool isEmpty() const
Returns true if the region is empty; otherwise returns false.
const_iterator begin() const noexcept
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
The QVector4D class represents a vector or vertex in 4D space.
Definition qvectornd.h:330
QJSValue expected
Definition qjsengine.cpp:12
Combined button and popup list for selecting options.
char * toString(const MyPoint &point)
bool qCompare(QString const &t1, QLatin1StringView const &t2, const char *actual, const char *expected, const char *file, int line)
Definition qtest.h:31
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg, const void *actualPtr, const void *expectedPtr, const char *(*actualFormatter)(const void *), const char *(*expectedFormatter)(const void *), const char *actual, const char *expected, const char *file, int line)
Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt,...)
Q_CORE_EXPORT char * qstrdup(const char *)
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
GLsizei const GLfloat * v
[13]
GLboolean r
[2]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLuint color
[2]
GLuint64EXT * result
[6]
#define t2
#define QTEST_ASSERT(cond)
Definition qtestassert.h:11
QFile file
[0]