aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmltest
diff options
context:
space:
mode:
authorCharles Yin <charles.yin@nokia.com>2012-06-07 13:08:13 +1000
committerQt by Nokia <qt-info@nokia.com>2012-07-18 05:19:44 +0200
commit1ce3a17ef51e10c03627842b9cdd0bb543ee8c83 (patch)
treee1450d6230744cd51421574899fbc00bda7e6ac2 /src/qmltest
parent1fb66c77d55cfdbe5df9919cbe2f42ddde34fb22 (diff)
Add fuzzyCompare() to qmltest
Change-Id: I4834f4af0839fb89424ed25f0addfb618e5374f8 Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
Diffstat (limited to 'src/qmltest')
-rw-r--r--src/qmltest/qmltest.pro4
-rw-r--r--src/qmltest/quicktestresult.cpp94
-rw-r--r--src/qmltest/quicktestresult_p.h6
3 files changed, 98 insertions, 6 deletions
diff --git a/src/qmltest/qmltest.pro b/src/qmltest/qmltest.pro
index f0b265e0f9..330d622f6a 100644
--- a/src/qmltest/qmltest.pro
+++ b/src/qmltest/qmltest.pro
@@ -2,9 +2,9 @@ load(qt_build_config)
TARGET = QtQuickTest
CONFIG += dll warn_on
-QT += qml testlib-private gui-private
DEFINES += QT_NO_URL_CAST_FROM_STRING
+QT += testlib testlib-private qml quick gui qml-private v8-private core-private
load(qt_module_config)
@@ -28,4 +28,4 @@ HEADERS += \
$$PWD/quicktestresult_p.h \
$$PWD/qtestoptions_p.h
-DEFINES += QT_QML_DEBUG_NO_WARNING
+DEFINES += QT_QML_DEBUG_NO_WARNING \ No newline at end of file
diff --git a/src/qmltest/quicktestresult.cpp b/src/qmltest/quicktestresult.cpp
index ff2cf05102..f6fd873011 100644
--- a/src/qmltest/quicktestresult.cpp
+++ b/src/qmltest/quicktestresult.cpp
@@ -56,6 +56,8 @@
#include <QtCore/QUrl>
#include <QtCore/QDir>
#include <QtQuick/qquickwindow.h>
+#include <QtGui/qvector3d.h>
+#include <QtQml/private/qqmlglobal_p.h>
QT_BEGIN_NAMESPACE
@@ -426,15 +428,101 @@ bool QuickTestResult::verify
}
}
+bool QuickTestResult::fuzzyCompare(const QVariant &actual, const QVariant &expected, qreal delta)
+{
+ if (actual.type() == QVariant::Color || expected.type() == QVariant::Color) {
+ if (!actual.canConvert(QVariant::Color) || !expected.canConvert(QVariant::Color))
+ return false;
+
+ //fuzzy color comparison
+ QColor act;
+ QColor exp;
+ bool ok(false);
+
+ QVariant var = QQml_colorProvider()->colorFromString(actual.toString(), &ok);
+ if (!ok)
+ return false;
+ act = var.value<QColor>();
+
+ QQml_colorProvider()->colorFromString(expected.toString(), &ok);
+ if (!ok)
+ return false;
+ exp = var.value<QColor>();
+
+ return ( qAbs(act.red() - exp.red()) <= delta
+ && qAbs(act.green() - exp.green()) <= delta
+ && qAbs(act.blue() - exp.blue()) <= delta
+ && qAbs(act.alpha() - exp.alpha()) <= delta);
+ } else {
+ //number comparison
+ bool ok = true;
+ qreal act = actual.toFloat(&ok);
+ if (!ok)
+ return false;
+
+ qreal exp = expected.toFloat(&ok);
+ if (!ok)
+ return false;
+
+ return (qAbs(act - exp) <= delta);
+ }
+
+ return false;
+}
+
+void QuickTestResult::stringify(QQmlV8Function *args)
+{
+ if (args->Length() < 1)
+ args->returnValue(v8::Null());
+
+ v8::Local<v8::Value> value = (*args)[0];
+
+ QString result;
+ QV8Engine *engine = args->engine();
+
+ //Check for Object Type
+ if (value->IsObject()
+ && !value->IsFunction()
+ && !value->IsArray()
+ && !value->IsDate()
+ && !value->IsRegExp()) {
+ QVariant v = engine->toVariant(value, QMetaType::UnknownType);
+ if (v.isValid()) {
+ switch (v.type()) {
+ case QVariant::Vector3D:
+ {
+ QVector3D v3d = v.value<QVector3D>();
+ result = QString::fromLatin1("Qt.vector3d(%1, %2, %3)").arg(v3d.x()).arg(v3d.y()).arg(v3d.z());
+ break;
+ }
+ default:
+ result = v.toString();
+ }
+
+ } else {
+ result = QLatin1String("Object");
+ }
+ } else {
+ v8::Local<v8::String> jsstr = value->ToString();
+ QString tmp = engine->toString(jsstr);
+ if (value->IsArray())
+ result.append(QString::fromLatin1("[%1]").arg(tmp));
+ else
+ result.append(tmp);
+ }
+
+ args->returnValue(args->engine()->toString(result));
+}
+
bool QuickTestResult::compare
(bool success, const QString &message,
- const QString &val1, const QString &val2,
+ const QVariant &val1, const QVariant &val2,
const QUrl &location, int line)
{
return QTestResult::compare
(success, message.toLocal8Bit().constData(),
- QTest::toString(val1.toLatin1().constData()),
- QTest::toString(val2.toLatin1().constData()),
+ QTest::toString(val1.toString().toLatin1().constData()),
+ QTest::toString(val2.toString().toLatin1().constData()),
"", "",
qtestFixUrl(location).toLatin1().constData(), line);
}
diff --git a/src/qmltest/quicktestresult_p.h b/src/qmltest/quicktestresult_p.h
index 76761b68ed..0ef41ade29 100644
--- a/src/qmltest/quicktestresult_p.h
+++ b/src/qmltest/quicktestresult_p.h
@@ -48,6 +48,7 @@
#include <QtCore/qstringlist.h>
#include <QtCore/qscopedpointer.h>
#include <QtQuick/qquickitem.h>
+#include <QtQml/private/qv8engine_p.h>
QT_BEGIN_NAMESPACE
@@ -111,12 +112,15 @@ public Q_SLOTS:
void finishTestDataCleanup();
void finishTestFunction();
+ void stringify(QQmlV8Function *args);
+
void fail(const QString &message, const QUrl &location, int line);
bool verify(bool success, const QString &message,
const QUrl &location, int line);
bool compare(bool success, const QString &message,
- const QString &val1, const QString &val2,
+ const QVariant &val1, const QVariant &val2,
const QUrl &location, int line);
+ bool fuzzyCompare(const QVariant &actual, const QVariant &expected, qreal delta);
void skip(const QString &message, const QUrl &location, int line);
bool expectFail(const QString &tag, const QString &comment,
const QUrl &location, int line);