summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2010-12-07 11:12:43 +1000
committerRhys Weatherley <rhys.weatherley@nokia.com>2010-12-07 11:20:29 +1000
commit27dfe1fb63977807c9c0a430e778a06290d4f4a4 (patch)
tree6feaddd7cdb73536cf16fafd99447ba18f1f9964
parentea4adaa0f859929d9fc1a5e6a169c7f493f64dee (diff)
Add tryCompare() function for asynchronous testing
-rw-r--r--doc/testcases.txt16
-rw-r--r--src/imports/testlib/TestCase.qml39
-rw-r--r--tests/qdeclarativeborderimage/data/tst_borderimage.qml93
3 files changed, 136 insertions, 12 deletions
diff --git a/doc/testcases.txt b/doc/testcases.txt
index 4a5e53f..8b52ae1 100644
--- a/doc/testcases.txt
+++ b/doc/testcases.txt
@@ -80,6 +80,13 @@ A number of helper functions are available to assist with writing tests:
- Compare "actual" with "expected", fail with "msg" if not.
- Similar to QCOMPARE in C++
+ tryCompare(obj, prop, value[, timeout])
+ - Tries comparing the property called "prop" on "obj" with "value".
+ If the compare fails, wait for up to "timeout" milliseconds,
+ processing Qt events, until the property becomes "value".
+ - The default timeout is 5000 milliseconds.
+ - e.g. tryCompare(img, "status", BorderImage.Ready)
+
skip(msg) ["Single" mode]
skipAll(msg)
- Skip the current test and show "msg".
@@ -152,6 +159,15 @@ case failed amongst a set of otherwise passing tests.
Asynchronous testing
====================
+The easiest method for asynchronous testing is to use tryCompare() to
+wait for a property to transition to an expected value. For example:
+
+ tryCompare(img, "status", BorderImage.Ready)
+ compare(img.width, 120)
+ compare(img.height, 120)
+ compare(img.horizontalTileMode, BorderImage.Stretch)
+ compare(img.verticalTileMode, BorderImage.Stretch)
+
The "when" property can be used to cause a "TestCase" element to trigger
only when a certain condition is true. For example, the following example
runs a test when the user presses the mouse button:
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml
index deb7605..5adee9c 100644
--- a/src/imports/testlib/TestCase.qml
+++ b/src/imports/testlib/TestCase.qml
@@ -84,9 +84,7 @@ Item {
throw new Error("QtTest::fail")
}
- function compare(actual, expected, msg) {
- var act = actual
- var exp = expected
+ function compareInternal(actual, expected) {
var success = false
if (typeof actual == "number" && typeof expected == "number") {
// Use a fuzzy compare if the two values are floats
@@ -99,11 +97,6 @@ Item {
Math.abs(actual.y - expected.y) <= 0.00001 &&
Math.abs(actual.z - expected.z) <= 0.00001) {
success = true
- } else {
- act = "Qt.vector3d(" + actual.x + ", " +
- actual.y + ", " + actual.z + ")"
- exp = "Qt.vector3d(" + expected.x + ", " +
- expected.y + ", " + expected.z + ")"
}
} else if (actual == expected) {
success = true
@@ -111,12 +104,42 @@ Item {
} else if (actual == expected) {
success = true
}
+ return success
+ }
+
+ function formatValue(value) {
+ if (typeof value == "object") {
+ if ("x" in value && "y" in value && "z" in value) {
+ return "Qt.vector3d(" + value.x + ", " +
+ value.y + ", " + value.z + ")"
+ }
+ }
+ return value
+ }
+
+ function compare(actual, expected, msg) {
+ var act = formatValue(actual)
+ var exp = formatValue(expected)
+ var success = compareInternal(actual, expected)
if (!msg)
msg = ""
if (!results.compare(success, msg, act, exp))
throw new Error("QtTest::fail")
}
+ function tryCompare(obj, prop, value, timeout) {
+ if (!timeout)
+ timeout = 5000
+ if (!compareInternal(obj[prop], value))
+ wait(0)
+ var i = 0
+ while (i < timeout && !compareInternal(obj[prop], value)) {
+ wait(50)
+ i += 50
+ }
+ compare(obj[prop], value, "property " + prop)
+ }
+
function skip(msg) {
if (!msg)
msg = ""
diff --git a/tests/qdeclarativeborderimage/data/tst_borderimage.qml b/tests/qdeclarativeborderimage/data/tst_borderimage.qml
index 5a803e8..1048c05 100644
--- a/tests/qdeclarativeborderimage/data/tst_borderimage.qml
+++ b/tests/qdeclarativeborderimage/data/tst_borderimage.qml
@@ -101,7 +101,48 @@ Item {
compare(noSource.verticalTileMode, BorderImage.Stretch)
}
- // TODO: imageSource()
+ function test_imageSource_data() {
+ return [
+ {
+ tag: "local",
+ source: "colors.png",
+ remote: false,
+ error: ""
+ },
+ {
+ tag: "local not found",
+ source: "no-such-file.png",
+ remote: false,
+ error: "qrc:data/inline:1:21: QML BorderImage: Cannot open: qrc:data/no-such-file.png"
+ }
+ // TODO: remote tests that need to use http
+ ]
+ }
+
+ function test_imageSource(row) {
+ var expectError = (row.error.length != 0)
+ if (expectError)
+ ignoreWarning(row.error)
+
+ var img = Qt.createQmlObject
+ ('import QtQuick 1.0; BorderImage { source: "' +
+ row.source + '" }', top)
+
+ if (row.remote)
+ tryCompare(img, "status", BorderImage.Loading)
+
+ if (!expectError) {
+ tryCompare(img, "status", BorderImage.Ready)
+ compare(img.width, 120)
+ compare(img.height, 120)
+ compare(img.horizontalTileMode, BorderImage.Stretch)
+ compare(img.verticalTileMode, BorderImage.Stretch)
+ } else {
+ tryCompare(img, "status", BorderImage.Error)
+ }
+
+ img.destroy()
+ }
function test_clearSource() {
compare(clearSource.source, "qrc:data/colors.png")
@@ -141,7 +182,53 @@ Item {
compare(tileModes2.verticalTileMode, BorderImage.Round)
}
- // TODO: sciSource()
+ function test_sciSource_data() {
+ return [
+ {
+ tag: "local",
+ source: "colors-round.sci",
+ sourceUrl: "qrc:data/colors-round.sci",
+ remote: false,
+ valid: true
+ },
+ {
+ tag: "local not found",
+ source: "no-such-file.sci",
+ sourceUrl: "qrc:data/no-such-file.sci",
+ remote: false,
+ valid: false
+ }
+ // TODO: remote tests that need to use http
+ ]
+ }
+
+ function test_sciSource(row) {
+ var img = Qt.createQmlObject
+ ('import QtQuick 1.0; BorderImage { source: "' +
+ row.source + '"; width: 300; height: 300 }', top)
+
+ if (row.remote)
+ tryCompare(img, "status", BorderImage.Loading)
+
+ compare(img.source, row.sourceUrl)
+ compare(img.width, 300)
+ compare(img.height, 300)
+
+ if (row.valid) {
+ tryCompare(img, "status", BorderImage.Ready)
+ compare(img.border.left, 10)
+ compare(img.border.top, 20)
+ compare(img.border.right, 30)
+ compare(img.border.bottom, 40)
+ compare(img.horizontalTileMode, BorderImage.Round)
+ compare(img.verticalTileMode, BorderImage.Repeat)
+ } else {
+ tryCompare(img, "status", BorderImage.Error)
+ }
+
+ img.destroy()
+ }
+
function test_invalidSciFile() {
ignoreWarning("QDeclarativeGridScaledImage: Invalid tile rule specified. Using Stretch.") // for "Roun"
@@ -156,7 +243,5 @@ Item {
compare(invalidSciFile.horizontalTileMode, BorderImage.Stretch)
compare(invalidSciFile.verticalTileMode, BorderImage.Stretch)
}
-
- // TODO: pendingRemoteRequest()
}
}