| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtTest module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifndef QTEST_H | 
|---|
| 43 | #define QTEST_H | 
|---|
| 44 |  | 
|---|
| 45 | #include <QtTest/qtest_global.h> | 
|---|
| 46 | #include <QtTest/qtestcase.h> | 
|---|
| 47 | #include <QtTest/qtestdata.h> | 
|---|
| 48 | #include <QtTest/qtestdata.h> | 
|---|
| 49 | #include <QtTest/qbenchmark.h> | 
|---|
| 50 |  | 
|---|
| 51 | #include <QtCore/qbytearray.h> | 
|---|
| 52 | #include <QtCore/qstring.h> | 
|---|
| 53 | #include <QtCore/qstringlist.h> | 
|---|
| 54 | #include <QtCore/qdatetime.h> | 
|---|
| 55 | #include <QtCore/qobject.h> | 
|---|
| 56 | #include <QtCore/qurl.h> | 
|---|
| 57 |  | 
|---|
| 58 | #include <QtCore/qpoint.h> | 
|---|
| 59 | #include <QtCore/qsize.h> | 
|---|
| 60 | #include <QtCore/qrect.h> | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | QT_BEGIN_HEADER | 
|---|
| 64 |  | 
|---|
| 65 | QT_BEGIN_NAMESPACE | 
|---|
| 66 |  | 
|---|
| 67 | QT_MODULE(Test) | 
|---|
| 68 |  | 
|---|
| 69 | namespace QTest | 
|---|
| 70 | { | 
|---|
| 71 |  | 
|---|
| 72 | template<> inline char *toString(const QLatin1String &str) | 
|---|
| 73 | { | 
|---|
| 74 | return qstrdup(str.latin1()); | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | template<> inline char *toString(const QString &str) | 
|---|
| 78 | { | 
|---|
| 79 | return qstrdup(str.toLatin1().constData()); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | template<> inline char *toString(const QByteArray &ba) | 
|---|
| 83 | { | 
|---|
| 84 | return QTest::toHexRepresentation(ba.constData(), ba.length()); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | template<> inline char *toString(const QTime &time) | 
|---|
| 88 | { | 
|---|
| 89 | return time.isValid() | 
|---|
| 90 | ? qstrdup(time.toString(QLatin1String("hh:mm:ss.zzz")).toLatin1()) | 
|---|
| 91 | : qstrdup("Invalid QTime"); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | template<> inline char *toString(const QDate &date) | 
|---|
| 95 | { | 
|---|
| 96 | return date.isValid() | 
|---|
| 97 | ? qstrdup(date.toString(QLatin1String("yyyy/MM/dd")).toLatin1()) | 
|---|
| 98 | : qstrdup("Invalid QDate"); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | template<> inline char *toString(const QDateTime &dateTime) | 
|---|
| 102 | { | 
|---|
| 103 | return dateTime.isValid() | 
|---|
| 104 | ? qstrdup((dateTime.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz")) + | 
|---|
| 105 | (dateTime.timeSpec() == Qt::LocalTime ? QLatin1String("[local time]") : QLatin1String("[UTC]"))).toLatin1()) | 
|---|
| 106 | : qstrdup("Invalid QDateTime"); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | template<> inline char *toString(const QChar &c) | 
|---|
| 110 | { | 
|---|
| 111 | return qstrdup(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16)).toLatin1().constData()); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | template<> inline char *toString(const QPoint &p) | 
|---|
| 115 | { | 
|---|
| 116 | return qstrdup(QString::fromLatin1("QPoint(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData()); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | template<> inline char *toString(const QSize &s) | 
|---|
| 120 | { | 
|---|
| 121 | return qstrdup(QString::fromLatin1("QSize(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData()); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | template<> inline char *toString(const QRect &s) | 
|---|
| 125 | { | 
|---|
| 126 | return qstrdup(QString::fromLatin1("QRect(%1,%2 %5x%6) (bottomright %3,%4)").arg(s.left()).arg(s.top()).arg(s.right()).arg(s.bottom()).arg(s.width()).arg(s.height()).toLatin1().constData()); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | template<> inline char *toString(const QPointF &p) | 
|---|
| 130 | { | 
|---|
| 131 | return qstrdup(QString::fromLatin1("QPointF(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData()); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | template<> inline char *toString(const QSizeF &s) | 
|---|
| 135 | { | 
|---|
| 136 | return qstrdup(QString::fromLatin1("QSizeF(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData()); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | template<> inline char *toString(const QRectF &s) | 
|---|
| 140 | { | 
|---|
| 141 | return qstrdup(QString::fromLatin1("QRectF(%1,%2 %5x%6) (bottomright %3,%4)").arg(s.left()).arg(s.top()).arg(s.right()).arg(s.bottom()).arg(s.width()).arg(s.height()).toLatin1().constData()); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | template<> inline char *toString(const QUrl &uri) | 
|---|
| 145 | { | 
|---|
| 146 | return qstrdup(uri.toEncoded().constData()); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | #ifndef QTEST_NO_SPECIALIZATIONS | 
|---|
| 150 | template<> | 
|---|
| 151 | #endif | 
|---|
| 152 | inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual, | 
|---|
| 153 | const char *expected, const char *file, int line) | 
|---|
| 154 | { | 
|---|
| 155 | return qCompare<QString>(t1, QString(t2), actual, expected, file, line); | 
|---|
| 156 | } | 
|---|
| 157 | #ifndef QTEST_NO_SPECIALIZATIONS | 
|---|
| 158 | template<> | 
|---|
| 159 | #endif | 
|---|
| 160 | inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual, | 
|---|
| 161 | const char *expected, const char *file, int line) | 
|---|
| 162 | { | 
|---|
| 163 | return qCompare<QString>(QString(t1), t2, actual, expected, file, line); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | template<> | 
|---|
| 167 | inline bool qCompare(QStringList const &t1, QStringList const &t2, | 
|---|
| 168 | const char *actual, const char *expected, const char *file, int line) | 
|---|
| 169 | { | 
|---|
| 170 | char msg[1024]; | 
|---|
| 171 | msg[0] = '\0'; | 
|---|
| 172 | bool isOk = true; | 
|---|
| 173 | if (t1.count() != t2.count()) { | 
|---|
| 174 | qt_snprintf(msg, 1024, "Compared QStringLists have different sizes.\n" | 
|---|
| 175 | "   Actual (%s) size  : '%d'\n" | 
|---|
| 176 | "   Expected (%s) size: '%d'", actual, t1.count(), expected, t2.count()); | 
|---|
| 177 | isOk = false; | 
|---|
| 178 | } | 
|---|
| 179 | const int min = qMin(t1.count(), t2.count()); | 
|---|
| 180 | for (int i = 0; isOk && i < min; ++i) { | 
|---|
| 181 | if (t1.at(i) != t2.at(i)) { | 
|---|
| 182 | qt_snprintf(msg, 1024, "Compared QStringLists differ at index %d.\n" | 
|---|
| 183 | "   Actual (%s) : '%s'\n" | 
|---|
| 184 | "   Expected (%s) : '%s'", i, actual, t1.at(i).toLatin1().constData(), | 
|---|
| 185 | expected, t2.at(i).toLatin1().constData()); | 
|---|
| 186 | isOk = false; | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 | return compare_helper(isOk, msg, file, line); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | template <typename T> | 
|---|
| 193 | inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected, | 
|---|
| 194 | const char *file, int line) | 
|---|
| 195 | { | 
|---|
| 196 | return qCompare(int(t1), int(t2), actual, expected, file, line); | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | template <typename T> | 
|---|
| 200 | inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected, | 
|---|
| 201 | const char *file, int line) | 
|---|
| 202 | { | 
|---|
| 203 | return qCompare(int(t1), t2, actual, expected, file, line); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | } | 
|---|
| 207 | QT_END_NAMESPACE | 
|---|
| 208 |  | 
|---|
| 209 | #define QTEST_APPLESS_MAIN(TestObject) \ | 
|---|
| 210 | int main(int argc, char *argv[]) \ | 
|---|
| 211 | { \ | 
|---|
| 212 | TestObject tc; \ | 
|---|
| 213 | return QTest::qExec(&tc, argc, argv); \ | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | #define QTEST_NOOP_MAIN \ | 
|---|
| 217 | int main(int argc, char *argv[]) \ | 
|---|
| 218 | { \ | 
|---|
| 219 | QObject tc; \ | 
|---|
| 220 | return QTest::qExec(&tc, argc, argv); \ | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | #include <QtTest/qtestsystem.h> | 
|---|
| 224 |  | 
|---|
| 225 | #ifdef QT_GUI_LIB | 
|---|
| 226 |  | 
|---|
| 227 | #include <QtTest/qtest_gui.h> | 
|---|
| 228 |  | 
|---|
| 229 | #define QTEST_MAIN(TestObject) \ | 
|---|
| 230 | int main(int argc, char *argv[]) \ | 
|---|
| 231 | { \ | 
|---|
| 232 | QApplication app(argc, argv); \ | 
|---|
| 233 | TestObject tc; \ | 
|---|
| 234 | return QTest::qExec(&tc, argc, argv); \ | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | #else | 
|---|
| 238 |  | 
|---|
| 239 | #define QTEST_MAIN(TestObject) \ | 
|---|
| 240 | int main(int argc, char *argv[]) \ | 
|---|
| 241 | { \ | 
|---|
| 242 | QCoreApplication app(argc, argv); \ | 
|---|
| 243 | TestObject tc; \ | 
|---|
| 244 | return QTest::qExec(&tc, argc, argv); \ | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | #endif // QT_GUI_LIB | 
|---|
| 248 |  | 
|---|
| 249 | QT_END_HEADER | 
|---|
| 250 |  | 
|---|
| 251 | #endif | 
|---|