diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2016-06-28 10:13:17 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2016-07-20 07:55:27 +0000 |
commit | aa5f09a0d22e9fa6e125afc798f6b3289088b791 (patch) | |
tree | 3d9c8844e0b22f7fac7b157fcee6f3a540414681 /src/imports/testlib | |
parent | 877ce4d446b34375096f5e7fae30101405d7ea95 (diff) |
Add tryVerify()
This is useful for conditions which can't be tested using tryCompare.
One such situation is that of the currentItem property of ListView.
In Qt Quick Controls 2, the currentItem property of the ListView that
is internally a child of Tumbler can be null for a certain period of
time, so using tryCompare() would result in errors due to trying to
access a property of a null object:
tryCompare(tumblerView.currentItem, "text", "2")
The current workaround is to use wait(50) in a for loop, which is ugly
and could lead to flaky tests:
for (var delay = 1000; delay >= 0; delay -= 50) {
if (tumblerView.currentItem)
break;
wait(50);
}
verify(tumblerView.currentItem);
compare(tumblerView.currentItem.text, data.currentIndex.toString());
Using tryVerify(), we can first ensure that currentItem isn't null, and
then use a regular synchronous compare afterwards:
tryVerify(function(){ return tumblerView.currentItem; });
compare(tumbler.currentItem.text, data.currentIndex.toString());
[ChangeLog][QtTest][TestCase] Added tryVerify() function to allow
verification of asynchronous conditions that can't be tested using
tryCompare().
Change-Id: Ie93052b650f7fe0bf26853054a8f0f35a483e387
Task-number: QTBUG-19708
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src/imports/testlib')
-rw-r--r-- | src/imports/testlib/TestCase.qml | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index d5e256a123..bc6786f9f8 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -394,6 +394,73 @@ Item { throw new Error("QtQuickTest::fail") } + /*! + \since 5.8 + \qmlmethod TestCase::tryVerify(function, timeout = 5000, message = "") + + Fails the current test case if \a function does not evaluate to + \c true before the specified \a timeout (in milliseconds) has elapsed. + The function is evaluated multiple times until the timeout is + reached. An optional \a message is displayed upon failure. + + This function is intended for testing applications where a condition + changes based on asynchronous events. Use verify() for testing + synchronous condition changes, and tryCompare() for testing + asynchronous property changes. + + For example, in the code below, it's not possible to use tryCompare(), + because the \c currentItem property might be \c null for a short period + of time: + + \code + tryCompare(listView.currentItem, "text", "Hello"); + \endcode + + Instead, we can use tryVerify() to first check that \c currentItem + isn't \c null, and then use a regular compare afterwards: + + \code + tryVerify(function(){ return listView.currentItem }) + compare(listView.currentItem.text, "Hello") + \endcode + + \sa verify(), compare(), tryCompare(), SignalSpy::wait() + */ + function tryVerify(expressionFunction, timeout, msg) { + if (!expressionFunction || !(expressionFunction instanceof Function)) { + qtest_results.fail("First argument must be a function", util.callerFile(), util.callerLine()) + throw new Error("QtQuickTest::fail") + } + + if (timeout && typeof(timeout) !== "number") { + qtest_results.fail("timeout argument must be a number", util.callerFile(), util.callerLine()) + throw new Error("QtQuickTest::fail") + } + + if (msg && typeof(msg) !== "string") { + qtest_results.fail("message argument must be a string", util.callerFile(), util.callerLine()) + throw new Error("QtQuickTest::fail") + } + + if (!timeout) + timeout = 5000 + + if (msg === undefined) + msg = "function returned false" + + if (!expressionFunction()) + wait(0) + + var i = 0 + while (i < timeout && !expressionFunction()) { + wait(50) + i += 50 + } + + if (!qtest_results.verify(expressionFunction(), msg, util.callerFile(), util.callerLine())) + throw new Error("QtQuickTest::fail") + } + /*! \internal */ // Determine what is o. // Discussions and reference: http://philrathe.com/articles/equiv |