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 QtTest module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QTEST_GUI_H
41 #define QTEST_GUI_H
42 
43 // enable GUI features
44 #ifndef QT_GUI_LIB
45 #define QT_GUI_LIB
46 #endif
47 #if 0
48 #pragma qt_class(QtTestGui)
49 #endif
50 
51 #include <QtTest/qtestassert.h>
52 #include <QtTest/qtest.h>
53 #include <QtTest/qtestevent.h>
54 #include <QtTest/qtestmouse.h>
55 #include <QtTest/qtesttouch.h>
56 #include <QtTest/qtestkeyboard.h>
57 
58 #include <QtGui/qcolor.h>
59 #include <QtGui/qpixmap.h>
60 #include <QtGui/qimage.h>
61 #include <QtGui/qregion.h>
62 #include <QtGui/qvector2d.h>
63 #include <QtGui/qvector3d.h>
64 #include <QtGui/qvector4d.h>
65 #include <QtGui/qicon.h>
66 
67 #if 0
68 // inform syncqt
69 #pragma qt_no_master_include
70 #endif
71 
72 QT_BEGIN_NAMESPACE
73 
74 
75 namespace QTest
76 {
77 
toString(const QColor & color)78 template<> inline char *toString(const QColor &color)
79 {
80     return qstrdup(color.name(QColor::HexArgb).toLocal8Bit().constData());
81 }
82 
toString(const QRegion & region)83 template<> inline char *toString(const QRegion &region)
84 {
85     QByteArray result = "QRegion(";
86     if (region.isNull()) {
87         result += "null";
88     } else if (region.isEmpty()) {
89         result += "empty";
90     } else {
91         const auto rects = region.begin();
92         const int rectCount = region.rectCount();
93         if (rectCount > 1) {
94             result += QByteArray::number(rectCount);
95             result += " rectangles, ";
96         }
97         for (int i = 0; i < rectCount; ++i) {
98             if (i)
99                 result += ", ";
100             const QRect &r = rects[i];
101             result += QByteArray::number(r.width());
102             result += 'x';
103             result += QByteArray::number(r.height());
104             if (r.x() >= 0)
105                 result += '+';
106             result += QByteArray::number(r.x());
107             if (r.y() >= 0)
108                 result += '+';
109             result += QByteArray::number(r.y());
110         }
111     }
112     result += ')';
113     return qstrdup(result.constData());
114 }
115 
116 #if !defined(QT_NO_VECTOR2D) || defined(Q_CLANG_QDOC)
toString(const QVector2D & v)117 template<> inline char *toString(const QVector2D &v)
118 {
119     QByteArray result = "QVector2D(" + QByteArray::number(double(v.x())) + ", "
120         + QByteArray::number(double(v.y())) + ')';
121     return qstrdup(result.constData());
122 }
123 #endif // !QT_NO_VECTOR2D
124 #if !defined(QT_NO_VECTOR3D) || defined(Q_CLANG_QDOC)
toString(const QVector3D & v)125 template<> inline char *toString(const QVector3D &v)
126 {
127     QByteArray result = "QVector3D(" + QByteArray::number(double(v.x())) + ", "
128         + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z())) + ')';
129     return qstrdup(result.constData());
130 }
131 #endif // !QT_NO_VECTOR3D
132 #if !defined(QT_NO_VECTOR4D) || defined(Q_CLANG_QDOC)
toString(const QVector4D & v)133 template<> inline char *toString(const QVector4D &v)
134 {
135     QByteArray result = "QVector4D(" + QByteArray::number(double(v.x())) + ", "
136         + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z()))
137         + ", " + QByteArray::number(double(v.w())) + ')';
138     return qstrdup(result.constData());
139 }
140 #endif // !QT_NO_VECTOR4D
141 
qCompare(QIcon const & t1,QIcon const & t2,const char * actual,const char * expected,const char * file,int line)142 inline bool qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected,
143                     const char *file, int line)
144 {
145     QTEST_ASSERT(sizeof(QIcon) == sizeof(void *));
146     return qCompare(*reinterpret_cast<void * const *>(&t1),
147                    *reinterpret_cast<void * const *>(&t2), actual, expected, file, line);
148 }
149 
qCompare(QImage const & t1,QImage const & t2,const char * actual,const char * expected,const char * file,int line)150 inline bool qCompare(QImage const &t1, QImage const &t2,
151                      const char *actual, const char *expected, const char *file, int line)
152 {
153     char msg[1024];
154     msg[0] = '\0';
155     const bool t1Null = t1.isNull();
156     const bool t2Null = t2.isNull();
157     if (t1Null != t2Null) {
158         qsnprintf(msg, 1024, "Compared QImages differ.\n"
159                   "   Actual   (%s).isNull(): %d\n"
160                   "   Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
161         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
162     }
163     if (t1Null && t2Null)
164         return compare_helper(true, nullptr, nullptr, nullptr, actual, expected, file, line);
165     if (!qFuzzyCompare(t1.devicePixelRatioF(), t2.devicePixelRatioF())) {
166         qsnprintf(msg, 1024, "Compared QImages differ in device pixel ratio.\n"
167                   "   Actual   (%s): %g\n"
168                   "   Expected (%s): %g",
169                   actual, t1.devicePixelRatioF(),
170                   expected, t2.devicePixelRatioF());
171         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
172     }
173     if (t1.width() != t2.width() || t1.height() != t2.height()) {
174         qsnprintf(msg, 1024, "Compared QImages differ in size.\n"
175                   "   Actual   (%s): %dx%d\n"
176                   "   Expected (%s): %dx%d",
177                   actual, t1.width(), t1.height(),
178                   expected, t2.width(), t2.height());
179         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
180     }
181     if (t1.format() != t2.format()) {
182         qsnprintf(msg, 1024, "Compared QImages differ in format.\n"
183                   "   Actual   (%s): %d\n"
184                   "   Expected (%s): %d",
185                   actual, t1.format(), expected, t2.format());
186         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
187     }
188     return compare_helper(t1 == t2, "Compared values are not the same",
189                           toString(t1), toString(t2), actual, expected, file, line);
190 }
191 
qCompare(QPixmap const & t1,QPixmap const & t2,const char * actual,const char * expected,const char * file,int line)192 inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected,
193                     const char *file, int line)
194 {
195     char msg[1024];
196     msg[0] = '\0';
197     const bool t1Null = t1.isNull();
198     const bool t2Null = t2.isNull();
199     if (t1Null != t2Null) {
200         qsnprintf(msg, 1024, "Compared QPixmaps differ.\n"
201                   "   Actual   (%s).isNull(): %d\n"
202                   "   Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
203         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
204     }
205     if (t1Null && t2Null)
206         return compare_helper(true, nullptr, nullptr, nullptr, actual, expected, file, line);
207     if (!qFuzzyCompare(t1.devicePixelRatioF(), t2.devicePixelRatioF())) {
208         qsnprintf(msg, 1024, "Compared QPixmaps differ in device pixel ratio.\n"
209                   "   Actual   (%s): %g\n"
210                   "   Expected (%s): %g",
211                   actual, t1.devicePixelRatioF(),
212                   expected, t2.devicePixelRatioF());
213         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
214     }
215     if (t1.width() != t2.width() || t1.height() != t2.height()) {
216         qsnprintf(msg, 1024, "Compared QPixmaps differ in size.\n"
217                   "   Actual   (%s): %dx%d\n"
218                   "   Expected (%s): %dx%d",
219                   actual, t1.width(), t1.height(),
220                   expected, t2.width(), t2.height());
221         return compare_helper(false, msg, nullptr, nullptr, actual, expected, file, line);
222     }
223     return qCompare(t1.toImage(), t2.toImage(), actual, expected, file, line);
224 }
225 
226 }
227 
228 QT_END_NAMESPACE
229 
230 #endif
231