| 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 | #include <cstdio> |
| 36 | |
| 37 | #if 0 |
| 38 | // inform syncqt |
| 39 | #pragma qt_no_master_include |
| 40 | #endif |
| 41 | |
| 42 | QT_BEGIN_NAMESPACE |
| 43 | |
| 44 | |
| 45 | namespace QTest |
| 46 | { |
| 47 | |
| 48 | template<> inline char *toString(const QColor &color) |
| 49 | { |
| 50 | return qstrdup(color.name(format: QColor::HexArgb).toLocal8Bit().constData()); |
| 51 | } |
| 52 | |
| 53 | template<> inline char *toString(const QRegion ®ion) |
| 54 | { |
| 55 | QByteArray result = "QRegion(" ; |
| 56 | if (region.isNull()) { |
| 57 | result += "null" ; |
| 58 | } else if (region.isEmpty()) { |
| 59 | result += "empty" ; |
| 60 | } else { |
| 61 | const auto rects = region.begin(); |
| 62 | const int rectCount = region.rectCount(); |
| 63 | if (rectCount > 1) { |
| 64 | result += QByteArray::number(rectCount); |
| 65 | result += " rectangles, " ; |
| 66 | } |
| 67 | for (int i = 0; i < rectCount; ++i) { |
| 68 | if (i) |
| 69 | result += ", " ; |
| 70 | const QRect &r = rects[i]; |
| 71 | result += QByteArray::number(r.width()); |
| 72 | result += 'x'; |
| 73 | result += QByteArray::number(r.height()); |
| 74 | if (r.x() >= 0) |
| 75 | result += '+'; |
| 76 | result += QByteArray::number(r.x()); |
| 77 | if (r.y() >= 0) |
| 78 | result += '+'; |
| 79 | result += QByteArray::number(r.y()); |
| 80 | } |
| 81 | } |
| 82 | result += ')'; |
| 83 | return qstrdup(result.constData()); |
| 84 | } |
| 85 | |
| 86 | #if !defined(QT_NO_VECTOR2D) || defined(Q_QDOC) |
| 87 | template<> inline char *toString(const QVector2D &v) |
| 88 | { |
| 89 | QByteArray result = "QVector2D(" + QByteArray::number(double(v.x())) + ", " |
| 90 | + QByteArray::number(double(v.y())) + ')'; |
| 91 | return qstrdup(result.constData()); |
| 92 | } |
| 93 | #endif // !QT_NO_VECTOR2D |
| 94 | #if !defined(QT_NO_VECTOR3D) || defined(Q_QDOC) |
| 95 | template<> inline char *toString(const QVector3D &v) |
| 96 | { |
| 97 | QByteArray result = "QVector3D(" + QByteArray::number(double(v.x())) + ", " |
| 98 | + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z())) + ')'; |
| 99 | return qstrdup(result.constData()); |
| 100 | } |
| 101 | #endif // !QT_NO_VECTOR3D |
| 102 | #if !defined(QT_NO_VECTOR4D) || defined(Q_QDOC) |
| 103 | template<> inline char *toString(const QVector4D &v) |
| 104 | { |
| 105 | QByteArray result = "QVector4D(" + QByteArray::number(double(v.x())) + ", " |
| 106 | + QByteArray::number(double(v.y())) + ", " + QByteArray::number(double(v.z())) |
| 107 | + ", " + QByteArray::number(double(v.w())) + ')'; |
| 108 | return qstrdup(result.constData()); |
| 109 | } |
| 110 | #endif // !QT_NO_VECTOR4D |
| 111 | |
| 112 | #if QT_CONFIG(shortcut) |
| 113 | template<> inline char *toString(const QKeySequence &keySequence) |
| 114 | { |
| 115 | return toString(str: keySequence.toString()); |
| 116 | } |
| 117 | #endif |
| 118 | |
| 119 | inline bool qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected, |
| 120 | const char *file, int line) |
| 121 | { |
| 122 | QTEST_ASSERT(sizeof(QIcon) == sizeof(void *)); |
| 123 | return qCompare(t1: *reinterpret_cast<void * const *>(&t1), |
| 124 | t2: *reinterpret_cast<void * const *>(&t2), actual, expected, file, line); |
| 125 | } |
| 126 | |
| 127 | inline bool qCompare(QImage const &t1, QImage const &t2, |
| 128 | const char *actual, const char *expected, const char *file, int line) |
| 129 | { |
| 130 | char msg[1024]; |
| 131 | msg[0] = '\0'; |
| 132 | const bool t1Null = t1.isNull(); |
| 133 | const bool t2Null = t2.isNull(); |
| 134 | if (t1Null != t2Null) { |
| 135 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QImages differ.\n" |
| 136 | " Actual (%s).isNull(): %d\n" |
| 137 | " Expected (%s).isNull(): %d" , actual, t1Null, expected, t2Null); |
| 138 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 139 | } |
| 140 | if (t1Null && t2Null) |
| 141 | return compare_helper(success: true, failureMsg: nullptr, actual, expected, file, line); |
| 142 | if (!qFuzzyCompare(p1: t1.devicePixelRatio(), p2: t2.devicePixelRatio())) { |
| 143 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QImages differ in device pixel ratio.\n" |
| 144 | " Actual (%s): %g\n" |
| 145 | " Expected (%s): %g" , |
| 146 | actual, t1.devicePixelRatio(), |
| 147 | expected, t2.devicePixelRatio()); |
| 148 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 149 | } |
| 150 | if (t1.width() != t2.width() || t1.height() != t2.height()) { |
| 151 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QImages differ in size.\n" |
| 152 | " Actual (%s): %dx%d\n" |
| 153 | " Expected (%s): %dx%d" , |
| 154 | actual, t1.width(), t1.height(), |
| 155 | expected, t2.width(), t2.height()); |
| 156 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 157 | } |
| 158 | if (t1.format() != t2.format()) { |
| 159 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QImages differ in format.\n" |
| 160 | " Actual (%s): %d\n" |
| 161 | " Expected (%s): %d" , |
| 162 | actual, t1.format(), expected, t2.format()); |
| 163 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 164 | } |
| 165 | return compare_helper(success: t1 == t2, failureMsg: "Compared values are not the same" , |
| 166 | actual, expected, file, line); |
| 167 | } |
| 168 | |
| 169 | inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected, |
| 170 | const char *file, int line) |
| 171 | { |
| 172 | char msg[1024]; |
| 173 | msg[0] = '\0'; |
| 174 | const bool t1Null = t1.isNull(); |
| 175 | const bool t2Null = t2.isNull(); |
| 176 | if (t1Null != t2Null) { |
| 177 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QPixmaps differ.\n" |
| 178 | " Actual (%s).isNull(): %d\n" |
| 179 | " Expected (%s).isNull(): %d" , actual, t1Null, expected, t2Null); |
| 180 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 181 | } |
| 182 | if (t1Null && t2Null) |
| 183 | return compare_helper(success: true, failureMsg: nullptr, actual, expected, file, line); |
| 184 | if (!qFuzzyCompare(p1: t1.devicePixelRatio(), p2: t2.devicePixelRatio())) { |
| 185 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QPixmaps differ in device pixel ratio.\n" |
| 186 | " Actual (%s): %g\n" |
| 187 | " Expected (%s): %g" , |
| 188 | actual, t1.devicePixelRatio(), |
| 189 | expected, t2.devicePixelRatio()); |
| 190 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 191 | } |
| 192 | if (t1.width() != t2.width() || t1.height() != t2.height()) { |
| 193 | std::snprintf(s: msg, maxlen: 1024, format: "Compared QPixmaps differ in size.\n" |
| 194 | " Actual (%s): %dx%d\n" |
| 195 | " Expected (%s): %dx%d" , |
| 196 | actual, t1.width(), t1.height(), |
| 197 | expected, t2.width(), t2.height()); |
| 198 | return compare_helper(success: false, failureMsg: msg, actual, expected, file, line); |
| 199 | } |
| 200 | return qCompare(t1: t1.toImage(), t2: t2.toImage(), actual, expected, file, line); |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | QT_END_NAMESPACE |
| 206 | |
| 207 | #endif |
| 208 | |