Consolidate SignalSpy item
authorCharles Yin <charles.yin@nokia.com>
Thu, 22 Mar 2012 02:27:30 +0000 (12:27 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 1 May 2012 04:06:55 +0000 (06:06 +0200)
1. Add signalArguments to track emitted signal parameters
2. Add valid property to track signal connection status
3. Make count, valid, signalArguments read only properties

Task-number: QTBUG-18531
Change-Id: I08e99c1086786a0e5095bf0d04750dec33153fde
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/imports/testlib/SignalSpy.qml
src/imports/testlib/signalspy.qdoc
tests/auto/qmltest/buttonclick/Button.qml
tests/auto/qmltest/buttonclick/tst_buttonclick.qml

index 539cb17..a3c5111 100644 (file)
@@ -49,16 +49,17 @@ Item {
     TestUtil {
         id: util
     }
-
     // Public API.
-
     property var target: null
     property string signalName: ""
-    property int count: 0
+    readonly property alias count: spy.qtest_count
+    readonly property alias valid:spy.qtest_valid
+    readonly property alias signalArguments:spy.qtest_signalArguments
 
     function clear() {
-        count = 0
+        qtest_count = 0
         qtest_expectedCount = 0
+        qtest_signalArguments = []
     }
 
     function wait(timeout) {
@@ -66,11 +67,11 @@ Item {
             timeout = 5000
         var expected = ++qtest_expectedCount
         var i = 0
-        while (i < timeout && count < expected) {
+        while (i < timeout && qtest_count < expected) {
             qtest_results.wait(50)
             i += 50
         }
-        var success = (count >= expected)
+        var success = (qtest_count >= expected)
         if (!qtest_results.verify(success, "wait for signal " + signalName, util.callerFile(), util.callerLine()))
             throw new Error("QtQuickTest::fail")
     }
@@ -89,6 +90,9 @@ Item {
     property var qtest_prevTarget: null
     property string qtest_prevSignalName: ""
     property int qtest_expectedCount: 0
+    property var qtest_signalArguments:[]
+    property int qtest_count: 0
+    property bool qtest_valid:false
 
     function qtest_update() {
         if (qtest_prevTarget != null) {
@@ -101,16 +105,22 @@ Item {
         if (target != null && signalName != "") {
             var func = target[signalName]
             if (func === undefined) {
+                spy.qtest_valid = false
                 console.log("Signal '" + signalName + "' not found")
             } else {
                 qtest_prevTarget = target
                 qtest_prevSignalName = signalName
                 func.connect(spy.qtest_activated)
+                spy.qtest_valid = true
+                spy.qtest_signalArguments = []
             }
+        } else {
+            spy.qtest_valid = false
         }
     }
 
     function qtest_activated() {
-        ++count
+        ++qtest_count
+        spy.qtest_signalArguments[spy.qtest_signalArguments.length] = arguments
     }
 }
index cdf6637..504ab8d 100644 (file)
 */
 
 /*!
+    \qmlproperty list SignalSpy::signalArguments
+
+    This property holds a list of emitted signal arguments. Each emission of the signal will append one item to the list, containing the arguments of the signal.
+    When connecting to a new \l target or new \l signalName or calling the \l clear() method, the \l signalArguments will be reset to empty.
+
+    \sa signalName, clear()
+    \readonly
+*/
+
+/*!
+    \qmlproperty bool SignalSpy::valid
+
+    This property defines the current signal connection status. It will be true when the \l signalName of the \l target is connected successfully, otherwise it will be false.
+
+    \sa count, target, signalName, clear()
+    \readonly
+*/
+
+/*!
     \qmlproperty int SignalSpy::count
 
     This property defines the number of times that \l signalName has
     been emitted from \l target since the last call to clear().
 
     \sa target, signalName, clear()
+    \readonly
 */
 
 /*!
     \qmlmethod SignalSpy::clear()
 
-    Clears \l count to 0.
+    Clears \l count to 0, resets \l valid to false and clears the \l signalArguments to empty.
 
     \sa count, wait()
 */
index e5adb9d..723f1c6 100644 (file)
@@ -46,7 +46,7 @@ Rectangle {
 
     property string text: "Button"
 
-    signal clicked
+    signal clicked(int x, int y)
 
     width: buttonLabel.width + 20; height: buttonLabel.height + 5
     border { width: 1; color: "black" }
@@ -62,7 +62,7 @@ Rectangle {
     MouseArea {
         id: mouseArea
         anchors.fill: parent
-        onClicked: container.clicked();
+        onClicked: container.clicked(mouse.x, mouse.y);
     }
 
     Text {
index 6ecbfea..840a7fd 100644 (file)
@@ -58,9 +58,19 @@ Button {
 
         function test_click() {
             compare(spy.count, 0)
-            button.clicked();
+            button.clicked(1, 2);
             compare(button.text, "Clicked");
             compare(spy.count, 1)
+            compare(spy.signalArguments.length, 1)
+            compare(spy.signalArguments[0][0], 1)
+            compare(spy.signalArguments[0][1], 2)
+            verify(spy.valid)
+            spy.clear()
+            compare(spy.count, 0)
+            verify(spy.valid)
+            compare(spy.signalArguments.length, 0)
+            spy.signalName = ""
+            verify(!spy.valid)
         }
     }
 }