doc: fix some typos in .qml files
[profile/ivi/qtdeclarative.git] / src / imports / testlib / TestCase.qml
index 02fa095..9d6bdba 100644 (file)
@@ -66,19 +66,16 @@ Item {
     // other test failed which this one depends on).
     property bool optional: false
 
-    // Property that is set to true when the main window is shown.
-    // We need to set the property value in an odd way to handle
-    // both qmlviewer and the QtQuickTest module test wrapper.
-    property bool windowShown: util.wrapper ? qtest.windowShown : false
+    property bool windowShown: qtest.windowShown
 
     // Internal private state.  Identifiers prefixed with qtest are reserved.
     property bool qtest_prevWhen: true
     property int qtest_testId: -1
     property bool qtest_componentCompleted : false
-    property variant qtest_testCaseResult
-    property variant qtest_results: qtest_results_normal
+    property var qtest_testCaseResult
+    property var qtest_results: qtest_results_normal
     TestResult { id: qtest_results_normal }
-    property variant qtest_events: qtest_events_normal
+    property var qtest_events: qtest_events_normal
     TestEvent { id: qtest_events_normal }
 
     function fail(msg) {
@@ -256,24 +253,9 @@ Item {
         return true
     }
 
-    function qtest_formatValue(value) {
-        if (qtest_typeof(value) == "object") {
-            if ("x" in value && "y" in value && "z" in value) {
-                return "Qt.vector3d(" + value.x + ", " +
-                       value.y + ", " + value.z + ")"
-            }
-            try {
-                return JSON.stringify(value)
-            } catch (ex) {
-                // stringify might fail (e.g. due to circular references)
-            }
-        }
-        return value
-    }
-
     function compare(actual, expected, msg) {
-        var act = testCase.qtest_formatValue(actual)
-        var exp = testCase.qtest_formatValue(expected)
+        var act = qtest_results.stringify(actual)
+        var exp = qtest_results.stringify(expected)
 
         var success = qtest_compareInternal(actual, expected)
         if (msg === undefined) {
@@ -287,6 +269,27 @@ Item {
         }
     }
 
+    function fuzzyCompare(actual, expected, delta, msg) {
+        if (delta === undefined)
+            qtest_fail("A delta value is required for fuzzyCompare", 2)
+
+        var success = qtest_results.fuzzyCompare(actual, expected, delta)
+        if (msg === undefined) {
+            if (success)
+                msg = "FUZZYCOMPARE()"
+            else
+                msg = "Compared values are not the same with delta(" + delta + ")"
+        }
+
+        if (!qtest_results.compare(success, msg, actual, expected, util.callerFile(), util.callerLine())) {
+            throw new Error("QtQuickTest::fail")
+        }
+    }
+
+    function grabImage(item) {
+        return qtest_results.grabImage(item);
+    }
+
     function tryCompare(obj, prop, value, timeout) {
         if (!timeout)
             timeout = 5000
@@ -298,8 +301,8 @@ Item {
             i += 50
         }
         var actual = obj[prop]
-        var act = testCase.qtest_formatValue(actual)
-        var exp = testCase.qtest_formatValue(value)
+        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()))
             throw new Error("QtQuickTest::fail")
@@ -312,12 +315,6 @@ Item {
         throw new Error("QtQuickTest::skip")
     }
 
-    function skipAll(msg) {
-        msg = "The skipAll function is no longer available. Please update this test by changing skipAll to skip."
-        qtest_results.fail(msg, util.callerFile(), util.callerLine())
-        throw new Error("QtQuickTest::skip")
-    }
-
     function expectFail(tag, msg) {
         if (tag === undefined) {
             warn("tag argument missing from expectFail()")
@@ -360,6 +357,12 @@ Item {
         qtest_results.wait(ms)
     }
 
+    function waitForRendering(item, timeout) {
+        if (timeout === undefined)
+            timeout = 5000
+        return qtest_results.waitForRendering(item, timeout)
+    }
+
     function sleep(ms) {
         qtest_results.sleep(ms)
     }
@@ -413,6 +416,22 @@ Item {
             qtest_fail("window not shown", 2)
     }
 
+    function mouseDrag(item, x, y, dx, dy, button, modifiers, delay) {
+        if (item.x === undefined || item.y === undefined)
+            return
+        if (button === undefined)
+            button = Qt.LeftButton
+        if (modifiers === undefined)
+            modifiers = Qt.NoModifier
+        if (delay == undefined)
+            delay = -1
+
+        mousePress(item, x, y, button, modifiers, delay)
+        //trigger dragging
+        mouseMove(item, x + util.dragThreshold + 1, y + util.dragThreshold + 1, delay, button)
+        mouseMove(item, x + dx, y + dy, delay, button)
+    }
+
     function mouseClick(item, x, y, button, modifiers, delay) {
         if (button === undefined)
             button = Qt.LeftButton
@@ -444,18 +463,18 @@ Item {
             qtest_fail("window not shown", 2)
     }
 
-    function mouseWheel(item, x, y, delta, buttons, modifiers, delay, orientation) {
+    function mouseWheel(item, x, y, xDelta, yDelta, buttons, modifiers, delay) {
         if (delay == undefined)
             delay = -1
         if (buttons == undefined)
             buttons = Qt.NoButton
         if (modifiers === undefined)
             modifiers = Qt.NoModifier
-        if (delta == undefined)
-            delta = 0
-        if (orientation == undefined)
-            orientation = Qt.Vertical
-        if (!qtest_events.mouseWheel(item, x, y, buttons, modifiers, delta, delay, orientation))
+        if (xDelta == undefined)
+            xDelta = 0
+        if (yDelta == undefined)
+            yDelta = 0
+        if (!qtest_events.mouseWheel(item, x, y, buttons, modifiers, xDelta, yDelta, delay))
             qtest_fail("window not shown", 2)
    }
 
@@ -477,7 +496,7 @@ Item {
                              e.fileName, e.lineNumber)
             }
         }
-        return !qtest_results.dataFailed
+        return !qtest_results.failed
     }
 
     function qtest_runFunction(prop, arg) {
@@ -486,6 +505,7 @@ Item {
             qtest_runInternal(prop, arg)
             qtest_results.finishTestData()
             qtest_runInternal("cleanup")
+            qtest_results.finishTestDataCleanup()
         }
     }
 
@@ -515,6 +535,7 @@ Item {
 
                 // Run the cleanup function.
                 qtest_runInternal("cleanup")
+                qtest_results.finishTestDataCleanup()
             } while (!qtest_results.measurementAccepted())
             qtest_results.endDataRun()
         } while (qtest_results.needsMoreMeasurements())
@@ -566,6 +587,8 @@ Item {
         var runTests = true
         if (!qtest_runInternal("initTestCase"))
             runTests = false
+        qtest_results.finishTestData()
+        qtest_results.finishTestDataCleanup()
         qtest_results.finishTestFunction()
 
         // Run the test methods.
@@ -593,6 +616,10 @@ Item {
                 functionsToRun.splice(index, 1)
             }
             qtest_results.functionName = prop
+
+            if (!(datafunc in testCase))
+                datafunc = "init_data";
+
             if (datafunc in testCase) {
                 if (qtest_runInternal(datafunc)) {
                     var table = qtest_testCaseResult
@@ -610,9 +637,13 @@ Item {
                             qtest_runFunction(prop, row)
                         qtest_results.dataTag = ""
                     }
-                    if (!haveData)
-                        qtest_results.warn("no data supplied for " + prop + "() by " + datafunc + "()"
-                                           , util.callerFile(), util.callerLine());
+                    if (!haveData) {
+                        if (datafunc === "init_data")
+                           qtest_runFunction(prop, null, isBenchmark)
+                        else
+                           qtest_results.warn("no data supplied for " + prop + "() by " + datafunc + "()"
+                                            , util.callerFile(), util.callerLine());
+                    }
                     qtest_results.clearTestTable()
                 }
             } else if (isBenchmark) {
@@ -636,6 +667,8 @@ Item {
         // Clean up and exit.
         running = false
         completed = true
+        qtest_results.finishTestData()
+        qtest_results.finishTestDataCleanup()
         qtest_results.finishTestFunction()
         qtest_results.functionName = ""
 
@@ -664,15 +697,6 @@ Item {
         }
     }
 
-    // The test framework will set util.windowShown when the
-    // window is actually shown.  If we are running with qmlviewer,
-    // then this won't happen.  So we use a timer instead.
-    Timer {
-        id: qtest_windowShowTimer
-        interval: 100
-        repeat: false
-        onTriggered: { windowShown = true }
-    }
 
     Component.onCompleted: {
         qtest.hasTestCase = true;
@@ -700,9 +724,6 @@ Item {
         if (optional)
             TestLogger.log_optional_test(qtest_testId)
         qtest_prevWhen = when
-        var isQmlViewer = util.wrapper ? false : true
-        if (isQmlViewer)
-            qtest_windowShowTimer.running = true
         if (when && !completed && !running)
             qtest_run()
     }