From 1ce3a17ef51e10c03627842b9cdd0bb543ee8c83 Mon Sep 17 00:00:00 2001 From: Charles Yin Date: Thu, 7 Jun 2012 13:08:13 +1000 Subject: Add fuzzyCompare() to qmltest Change-Id: I4834f4af0839fb89424ed25f0addfb618e5374f8 Reviewed-by: Yunqiao Yin --- src/qmltest/qmltest.pro | 4 +- src/qmltest/quicktestresult.cpp | 94 +++++++++++++++++++++++++++++++++++++++-- src/qmltest/quicktestresult_p.h | 6 ++- 3 files changed, 98 insertions(+), 6 deletions(-) (limited to 'src/qmltest') 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 #include #include +#include +#include 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(); + + QQml_colorProvider()->colorFromString(expected.toString(), &ok); + if (!ok) + return false; + exp = var.value(); + + 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 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(); + 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 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 #include #include +#include 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); -- cgit v1.2.3