From b96479032c3c0c02d9444efe90ccc5ea862d886c Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Fri, 2 Aug 2013 12:44:32 +0200 Subject: [PATCH] Allow passing a message to tryCompare Also adds a check to make sure timeout is a number, for people that are moving from compare() to tryCompare() and forget of the extra timeout parameter Change-Id: I54e56e2014c022e06622edebca1fd36172c75009 Reviewed-by: Yunqiao Yin Reviewed-by: Caroline Chao --- src/imports/testlib/TestCase.qml | 11 +++++++++-- src/imports/testlib/testcase.qdoc | 7 ++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index 0bec4cd..6e2c8e7 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -290,14 +290,21 @@ Item { return qtest_results.grabImage(item); } - function tryCompare(obj, prop, value, timeout) { + function tryCompare(obj, prop, value, timeout, msg) { if (arguments.length == 2) { qtest_results.fail("A value is required for tryCompare", util.callerFile(), util.callerLine()) throw new Error("QtQuickTest::fail") } + if (timeout !== undefined && typeof(timeout) != "number") { + qtest_results.fail("timeout should be a number", + util.callerFile(), util.callerLine()) + throw new Error("QtQuickTest::fail") + } if (!timeout) timeout = 5000 + if (msg === undefined) + msg = "property " + prop if (!qtest_compareInternal(obj[prop], value)) wait(0) var i = 0 @@ -309,7 +316,7 @@ Item { var act = qtest_results.stringify(actual) var exp = qtest_results.stringify(value) var success = qtest_compareInternal(actual, value) - if (!qtest_results.compare(success, "property " + prop, act, exp, util.callerFile(), util.callerLine())) + if (!qtest_results.compare(success, msg, act, exp, util.callerFile(), util.callerLine())) throw new Error("QtQuickTest::fail") } diff --git a/src/imports/testlib/testcase.qdoc b/src/imports/testlib/testcase.qdoc index 56fcb24..dd1d9e5 100644 --- a/src/imports/testlib/testcase.qdoc +++ b/src/imports/testlib/testcase.qdoc @@ -367,11 +367,12 @@ */ /*! - \qmlmethod TestCase::tryCompare(obj, property, expected, timeout = 5000) + \qmlmethod TestCase::tryCompare(obj, property, expected, timeout = 5000, message = "") Fails the current test case if the specified \a property on \a obj - is not the same as \a expected. The test will be retried multiple - times until the \a timeout (in milliseconds) is reached. + is not the same as \a expected, and displays the optional \a message. + The test will be retried multiple times until the + \a timeout (in milliseconds) is reached. This function is intended for testing applications where a property changes value based on asynchronous events. Use compare() for testing -- 2.7.4