summaryrefslogtreecommitdiffstats
path: root/src/testlib
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2012-03-12 21:03:49 +0100
committerLars Knoll <lars.knoll@nokia.com>2012-03-12 21:03:49 +0100
commitd5098f2802255da10b749b93705084ad1fdfc6a5 (patch)
tree6462008a4ab7d13435d93490fed96c62c516cbdf /src/testlib
parentd5a85940f785459d7b982d5fdf59a9fd18825092 (diff)
parentb5b41c18345719612e5411cc482466d2dbafdaf7 (diff)
Merge remote-tracking branch 'origin/master' into api_changes
Conflicts: tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp Change-Id: I884afc3b6d65c6411733a897a1949e19393573a7
Diffstat (limited to 'src/testlib')
-rw-r--r--src/testlib/qtest.h8
-rw-r--r--src/testlib/qtest_gui.h30
-rw-r--r--src/testlib/qtestaccessible.h50
-rw-r--r--src/testlib/qtestcase.cpp49
-rw-r--r--src/testlib/qtestcase.h16
-rw-r--r--src/testlib/qtestresult.cpp38
-rw-r--r--src/testlib/qtestresult_p.h6
7 files changed, 116 insertions, 81 deletions
diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h
index 32b2f0ca9c..d167324aef 100644
--- a/src/testlib/qtest.h
+++ b/src/testlib/qtest.h
@@ -197,20 +197,20 @@ inline bool qCompare(QStringList const &t1, QStringList const &t2,
const int expectedSize = t2.count();
if (actualSize != expectedSize) {
qsnprintf(msg, sizeof(msg), "Compared QStringLists have different sizes.\n"
- " Actual (%s) size : '%d'\n"
+ " Actual (%s) size: '%d'\n"
" Expected (%s) size: '%d'", actual, actualSize, expected, expectedSize);
isOk = false;
}
for (int i = 0; isOk && i < actualSize; ++i) {
if (t1.at(i) != t2.at(i)) {
qsnprintf(msg, sizeof(msg), "Compared QStringLists differ at index %d.\n"
- " Actual (%s) : '%s'\n"
- " Expected (%s) : '%s'", i, actual, t1.at(i).toLatin1().constData(),
+ " Actual (%s): '%s'\n"
+ " Expected (%s): '%s'", i, actual, t1.at(i).toLatin1().constData(),
expected, t2.at(i).toLatin1().constData());
isOk = false;
}
}
- return compare_helper(isOk, msg, file, line);
+ return compare_helper(isOk, msg, 0, 0, actual, expected, file, line);
}
template <typename T>
diff --git a/src/testlib/qtest_gui.h b/src/testlib/qtest_gui.h
index d39e943926..f10ddd8473 100644
--- a/src/testlib/qtest_gui.h
+++ b/src/testlib/qtest_gui.h
@@ -100,31 +100,29 @@ inline bool qCompare(QImage const &t1, QImage const &t2,
const bool t2Null = t2.isNull();
if (t1Null != t2Null) {
qsnprintf(msg, 1024, "Compared QImages differ.\n"
- " Actual (%s).isNull() : %d\n"
+ " Actual (%s).isNull(): %d\n"
" Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
- return compare_helper(false, msg, file, line);
+ return compare_helper(false, msg, 0, 0, actual, expected, file, line);
}
if (t1Null && t2Null)
- return compare_helper(true, "COMPARE()", file, line);
+ return compare_helper(true, 0, 0, 0, actual, expected, file, line);
if (t1.width() != t2.width() || t2.height() != t2.height()) {
qsnprintf(msg, 1024, "Compared QImages differ in size.\n"
- " Actual (%s) : %dx%d\n"
+ " Actual (%s): %dx%d\n"
" Expected (%s): %dx%d",
actual, t1.width(), t1.height(),
expected, t2.width(), t2.height());
- return compare_helper(false, msg, file, line);
+ return compare_helper(false, msg, 0, 0, actual, expected, file, line);
}
if (t1.format() != t2.format()) {
qsnprintf(msg, 1024, "Compared QImages differ in format.\n"
- " Actual (%s) : %d\n"
+ " Actual (%s): %d\n"
" Expected (%s): %d",
actual, t1.format(), expected, t2.format());
- return compare_helper(false, msg, file, line);
+ return compare_helper(false, msg, 0, 0, actual, expected, file, line);
}
- return (t1 == t2)
- ? compare_helper(true, "COMPARE()", file, line)
- : compare_helper(false, "Compared values are not the same",
- toString(t1), toString(t2), actual, expected, file, line);
+ return compare_helper(t1 == t2, "Compared values are not the same",
+ toString(t1), toString(t2), actual, expected, file, line);
}
#ifndef QTEST_NO_SPECIALIZATIONS
@@ -139,19 +137,19 @@ inline bool qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, c
const bool t2Null = t2.isNull();
if (t1Null != t2Null) {
qsnprintf(msg, 1024, "Compared QPixmaps differ.\n"
- " Actual (%s).isNull() : %d\n"
+ " Actual (%s).isNull(): %d\n"
" Expected (%s).isNull(): %d", actual, t1Null, expected, t2Null);
- return compare_helper(false, msg, file, line);
+ return compare_helper(false, msg, 0, 0, actual, expected, file, line);
}
if (t1Null && t2Null)
- return compare_helper(true, "COMPARE()", file, line);
+ return compare_helper(true, 0, 0, 0, actual, expected, file, line);
if (t1.width() != t2.width() || t2.height() != t2.height()) {
qsnprintf(msg, 1024, "Compared QPixmaps differ in size.\n"
- " Actual (%s) : %dx%d\n"
+ " Actual (%s): %dx%d\n"
" Expected (%s): %dx%d",
actual, t1.width(), t1.height(),
expected, t2.width(), t2.height());
- return compare_helper(false, msg, file, line);
+ return compare_helper(false, msg, 0, 0, actual, expected, file, line);
}
return qCompare(t1.toImage(), t2.toImage(), actual, expected, file, line);
}
diff --git a/src/testlib/qtestaccessible.h b/src/testlib/qtestaccessible.h
index d402cc8cf8..f9b1b97ac6 100644
--- a/src/testlib/qtestaccessible.h
+++ b/src/testlib/qtestaccessible.h
@@ -49,8 +49,8 @@
#ifndef QT_NO_ACCESSIBILITY
-#define QVERIFY_EVENT(object, child, event) \
- QVERIFY(QTestAccessibility::verifyEvent(object, child, (int)event))
+#define QVERIFY_EVENT(event) \
+ QVERIFY(QTestAccessibility::verifyEvent(event))
#include <QtCore/qlist.h>
#include <QtGui/qaccessible.h>
@@ -63,22 +63,21 @@ QT_BEGIN_NAMESPACE
class QObject;
-struct QTestAccessibilityEvent
-{
- QTestAccessibilityEvent(QObject* o = 0, int c = 0, int e = 0)
- : object(o), child(c), event(e) {}
+typedef QList<QAccessibleEvent> EventList;
- bool operator==(const QTestAccessibilityEvent &o) const
- {
- return o.object == object && o.child == child && o.event == event;
+bool operator==(const QAccessibleEvent &l, const QAccessibleEvent &r)
+{
+ if (l.type() != r.type() ||
+ l.object() != r.object() ||
+ l.child() != r.child())
+ return false;
+
+ if (l.type() == QAccessible::StateChanged) {
+ return static_cast<const QAccessibleStateChangeEvent*>(&l)->changedStates()
+ == static_cast<const QAccessibleStateChangeEvent*>(&r)->changedStates();
}
-
- QObject* object;
- int child;
- int event;
-};
-
-typedef QList<QTestAccessibilityEvent> EventList;
+ return true;
+}
class QTestAccessibility
{
@@ -90,6 +89,7 @@ public:
qAddPostRoutine(cleanup);
}
}
+
static void cleanup()
{
delete instance();
@@ -97,18 +97,13 @@ public:
}
static void clearEvents() { eventList().clear(); }
static EventList events() { return eventList(); }
- static bool verifyEvent(const QTestAccessibilityEvent& ev)
+ static bool verifyEvent(const QAccessibleEvent& ev)
{
if (eventList().isEmpty())
return FALSE;
return eventList().takeFirst() == ev;
}
- static bool verifyEvent(QObject *o, int c, int e)
- {
- return verifyEvent(QTestAccessibilityEvent(o, c, e));
- }
-
private:
QTestAccessibility()
{
@@ -136,8 +131,15 @@ private:
static void updateHandler(const QAccessibleEvent &event)
{
- // qDebug("updateHandler called: %p %d %d", o, c, (int)e);
- eventList().append(QTestAccessibilityEvent(event.object(), event.child(), (int)event.type()));
+ eventList().append(copyEvent(event));
+ }
+
+ static QAccessibleEvent copyEvent(const QAccessibleEvent &event)
+ {
+ if (event.type() == QAccessible::StateChanged)
+ return QAccessibleStateChangeEvent(static_cast<const QAccessibleStateChangeEvent*>(&event)->changedStates(),
+ event.object(), event.child());
+ return QAccessibleEvent(event.type(), event.object(), event.child());
}
static EventList &eventList()
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index c1d19b8098..fb9a8e630c 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -410,6 +410,12 @@ QT_BEGIN_NAMESPACE
point to the working directory from which the compiler is invoked, or only
absolute paths to the source files are passed to the compiler. Otherwise, the
absolute path of the source directory cannot be determined.
+
+ \bold {Note:} For tests that use the \l QTEST_APPLESS_MAIN() macro to generate a
+ \c{main()} function, \c{QFINDTESTDATA} will not attempt to find test data
+ relative to QCoreApplication::applicationDirPath(). In practice, this means that
+ tests using \c{QTEST_APPLESS_MAIN()} will fail to find their test data
+ if run from a shadow build tree.
*/
/*! \macro QTEST_MAIN(TestClass)
@@ -2407,15 +2413,34 @@ QObject *QTest::testObject()
*/
bool QTest::compare_helper(bool success, const char *msg, const char *file, int line)
{
+ static bool warned = false;
+ if (!warned) {
+ warned = true;
+ QTest::qWarn("QTest::compare_helper(bool, const char *, const char *, int) is obsolete "
+ "and will soon be removed. Please update your code to use the other "
+ "version of this function.");
+ }
+
return QTestResult::compare(success, msg, file, line);
}
/*! \internal
+ This function is called by various specializations of QTest::qCompare
+ to decide whether to report a failure and to produce verbose test output.
+
+ The failureMsg parameter can be null, in which case a default message
+ will be output if the compare fails. If the compare succeeds, failureMsg
+ will not be output.
+
+ If the caller has already passed a failure message showing the compared
+ values, or if those values cannot be stringified, val1 and val2 can be null.
*/
-bool QTest::compare_helper(bool success, const char *msg, char *val1, char *val2,
- const char *actual, const char *expected, const char *file, int line)
+bool QTest::compare_helper(bool success, const char *failureMsg,
+ char *val1, char *val2,
+ const char *actual, const char *expected,
+ const char *file, int line)
{
- return QTestResult::compare(success, msg, val1, val2, actual, expected, file, line);
+ return QTestResult::compare(success, failureMsg, val1, val2, actual, expected, file, line);
}
/*! \fn bool QTest::qCompare<float>(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line)
@@ -2425,10 +2450,8 @@ template <>
Q_TESTLIB_EXPORT bool QTest::qCompare<float>(float const &t1, float const &t2, const char *actual, const char *expected,
const char *file, int line)
{
- return qFuzzyCompare(t1, t2)
- ? compare_helper(true, "COMPARE()", file, line)
- : compare_helper(false, "Compared floats are not the same (fuzzy compare)",
- toString(t1), toString(t2), actual, expected, file, line);
+ return compare_helper(qFuzzyCompare(t1, t2), "Compared floats are not the same (fuzzy compare)",
+ toString(t1), toString(t2), actual, expected, file, line);
}
/*! \fn bool QTest::qCompare<double>(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line)
@@ -2438,10 +2461,8 @@ template <>
Q_TESTLIB_EXPORT bool QTest::qCompare<double>(double const &t1, double const &t2, const char *actual, const char *expected,
const char *file, int line)
{
- return qFuzzyCompare(t1, t2)
- ? compare_helper(true, "COMPARE()", file, line)
- : compare_helper(false, "Compared doubles are not the same (fuzzy compare)",
- toString(t1), toString(t2), actual, expected, file, line);
+ return compare_helper(qFuzzyCompare(t1, t2), "Compared doubles are not the same (fuzzy compare)",
+ toString(t1), toString(t2), actual, expected, file, line);
}
#define TO_STRING_IMPL(TYPE, FORMAT) \
@@ -2494,10 +2515,8 @@ char *QTest::toString(const void *p)
bool QTest::compare_string_helper(const char *t1, const char *t2, const char *actual,
const char *expected, const char *file, int line)
{
- return (qstrcmp(t1, t2) == 0)
- ? compare_helper(true, "COMPARE()", file, line)
- : compare_helper(false, "Compared strings are not the same",
- toString(t1), toString(t2), actual, expected, file, line);
+ return compare_helper(qstrcmp(t1, t2) == 0, "Compared strings are not the same",
+ toString(t1), toString(t2), actual, expected, file, line);
}
/*! \fn bool QTest::compare_ptr_helper(const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line);
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index c64420b000..a344736f7b 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -200,7 +200,8 @@ namespace QTest
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, const char *file,
int line);
- Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, char *val1, char *val2,
+ Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
+ char *val1, char *val2,
const char *actual, const char *expected,
const char *file, int line);
Q_TESTLIB_EXPORT void qSleep(int ms);
@@ -217,13 +218,10 @@ namespace QTest
inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
const char *file, int line)
{
- return (t1 == t2)
- ? compare_helper(true, "COMPARE()", file, line)
- : compare_helper(false, "Compared values are not the same",
- toString<T>(t1), toString<T>(t2), actual, expected, file, line);
+ return compare_helper(t1 == t2, "Compared values are not the same",
+ toString<T>(t1), toString<T>(t2), actual, expected, file, line);
}
-
template <>
Q_TESTLIB_EXPORT bool qCompare<float>(float const &t1, float const &t2,
const char *actual, const char *expected, const char *file, int line);
@@ -235,10 +233,8 @@ namespace QTest
inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
const char *expected, const char *file, int line)
{
- return (t1 == t2)
- ? compare_helper(true, "COMPARE()", file, line)
- : compare_helper(false, "Compared pointers are not the same",
- toString(t1), toString(t2), actual, expected, file, line);
+ return compare_helper(t1 == t2, "Compared pointers are not the same",
+ toString(t1), toString(t2), actual, expected, file, line);
}
Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
diff --git a/src/testlib/qtestresult.cpp b/src/testlib/qtestresult.cpp
index 14ab29af2b..9d62a9eb57 100644
--- a/src/testlib/qtestresult.cpp
+++ b/src/testlib/qtestresult.cpp
@@ -230,6 +230,8 @@ static bool checkStatement(bool statement, const char *msg, const char *file, in
bool QTestResult::verify(bool statement, const char *statementStr,
const char *description, const char *file, int line)
{
+ QTEST_ASSERT(statementStr);
+
char msg[1024];
if (QTestLog::verboseLevel() >= 2) {
@@ -240,7 +242,7 @@ bool QTestResult::verify(bool statement, const char *statementStr,
const char * format = QTest::expectFailMode
? "'%s' returned TRUE unexpectedly. (%s)"
: "'%s' returned FALSE. (%s)";
- qsnprintf(msg, 1024, format, statementStr, description);
+ qsnprintf(msg, 1024, format, statementStr, description ? description : "");
return checkStatement(statement, msg, file, line);
}
@@ -254,22 +256,38 @@ bool QTestResult::compare(bool success, const char *msg, const char *file, int l
return checkStatement(success, msg, file, line);
}
-bool QTestResult::compare(bool success, const char *msg, char *val1, char *val2,
- const char *actual, const char *expected, const char *file, int line)
+bool QTestResult::compare(bool success, const char *failureMsg,
+ char *val1, char *val2,
+ const char *actual, const char *expected,
+ const char *file, int line)
{
QTEST_ASSERT(expected);
QTEST_ASSERT(actual);
- if (!val1 && !val2)
- return compare(success, msg, file, line);
+ char msg[1024];
+
+ if (QTestLog::verboseLevel() >= 2) {
+ qsnprintf(msg, 1024, "QCOMPARE(%s, %s)", actual, expected);
+ QTestLog::info(msg, file, line);
+ }
+
+ if (!failureMsg)
+ failureMsg = "Compared values are not the same";
+
+ if (success && QTest::expectFailMode) {
+ qsnprintf(msg, 1024, "QCOMPARE(%s, %s) returned TRUE unexpectedly.", actual, expected);
+ } else if (val1 || val2) {
+ qsnprintf(msg, 1024, "%s\n Actual (%s): %s\n Expected (%s): %s",
+ failureMsg,
+ actual, val1 ? val1 : "<null>",
+ expected, val2 ? val2 : "<null>");
+ } else
+ qsnprintf(msg, 1024, "%s", failureMsg);
- char buf[1024];
- qsnprintf(buf, 1024, "%s\n Actual (%s): %s\n Expected (%s): %s", msg,
- actual, val1 ? val1 : "<null>",
- expected, val2 ? val2 : "<null>");
delete [] val1;
delete [] val2;
- return compare(success, buf, file, line);
+
+ return checkStatement(success, msg, file, line);
}
void QTestResult::addFailure(const char *message, const char *file, int line)
diff --git a/src/testlib/qtestresult_p.h b/src/testlib/qtestresult_p.h
index b060926f2a..1bf070f6cf 100644
--- a/src/testlib/qtestresult_p.h
+++ b/src/testlib/qtestresult_p.h
@@ -77,8 +77,10 @@ public:
static void addFailure(const char *message, const char *file, int line);
static bool compare(bool success, const char *msg, const char *file, int line);
- static bool compare(bool success, const char *msg, char *val1, char *val2,
- const char *actual, const char *expected, const char *file, int line);
+ static bool compare(bool success, const char *failureMsg,
+ char *val1, char *val2,
+ const char *actual, const char *expected,
+ const char *file, int line);
static void setCurrentGlobalTestData(QTestData *data);
static void setCurrentTestData(QTestData *data);