TestLib: Add more mouseMove in mouseDrag implementation
authorCaroline Chao <caroline.chao@digia.com>
Tue, 2 Apr 2013 16:24:46 +0000 (18:24 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Sat, 6 Apr 2013 08:10:17 +0000 (10:10 +0200)
So the cursor has intermediate states while dragging instead of
jumping to the final position.

This is for example useful for testing the behavior of a control
during the drag.

Add autotest.

Change-Id: I061dd18ef7ac389aa4da4a5b60f9e128ee8c08d0
Reviewed-by: Alan Alpert <aalpert@blackberry.com>

src/imports/testlib/TestCase.qml
tests/auto/qmltest/events/tst_drag.qml

index f9a1a49..fbfad28 100644 (file)
@@ -441,9 +441,23 @@ Item {
         if (delay == undefined)
             delay = -1
 
+        // Divide dx and dy to have intermediate mouseMove while dragging
+        // Fractions of dx/dy need be superior to the dragThreshold
+        // to make the drag works though
+        var ddx = Math.round(dx/3)
+        if (ddx < (util.dragThreshold + 1))
+            ddx = 0
+        var ddy = Math.round(dy/3)
+        if (ddy < (util.dragThreshold + 1))
+            ddy = 0
+
         mousePress(item, x, y, button, modifiers, delay)
         //trigger dragging
         mouseMove(item, x + util.dragThreshold + 1, y + util.dragThreshold + 1, delay, button)
+        if (ddx > 0 || ddy > 0) {
+            mouseMove(item, x + ddx, y + ddy, delay, button)
+            mouseMove(item, x + 2*ddx, y + 2*ddy, delay, button)
+        }
         mouseMove(item, x + dx, y + dy, delay, button)
         mouseRelease(item, x + dx, y + dy, button, modifiers, delay)
     }
index 70e0ea2..7db1e1e 100644 (file)
@@ -51,6 +51,17 @@ Rectangle{
         id: util
     }
 
+    SignalSpy {
+        id: spyX
+        target: container2
+        signalName: "posXChanged"
+    }
+    SignalSpy {
+        id: spyY
+        target: container2
+        signalName: "posYChanged"
+    }
+
     Rectangle {
         id:container
         width:20
@@ -66,6 +77,55 @@ Rectangle{
         }
     }
 
+    Rectangle {
+        id: container2
+        x: 25
+        y: 25
+        width:100
+        height:100
+        color: "red"
+        property bool updatePositionWhileDragging: false
+        property var posX: 0
+        property var posY: 0
+
+        function reset() {
+            fakeHandle.x = 0
+            fakeHandle.y = 0
+            spyX.clear()
+            spyY.clear()
+        }
+
+        Binding {
+            when: container2.updatePositionWhileDragging
+            target: container2
+            property: "posX"
+            value: fakeHandle.x
+        }
+
+        Binding {
+            when: container2.updatePositionWhileDragging
+            target: container2
+            property: "posY"
+            value: fakeHandle.y
+        }
+
+        Item { id: fakeHandle }
+
+        MouseArea {
+            anchors.fill : container2
+            drag.maximumX: 180
+            drag.maximumY: 180
+            drag.minimumX: 0
+            drag.minimumY: 0
+            drag.target: fakeHandle
+
+            onReleased: if (!container2.updatePositionWhileDragging) {
+                            container2.posX = mouse.x;
+                            container2.posY = mouse.y
+                        }
+        }
+    }
+
     TestCase {
         name:"mouserelease"
         when:windowShown
@@ -74,5 +134,47 @@ Rectangle{
             compare(container.x, 20 - util.dragThreshold - 1);
             compare(container.y, 30 - util.dragThreshold - 1);
         }
+
+        function test_doSomethingWhileDragging() {
+            container2.updatePositionWhileDragging = false
+            // dx and dy are superior to 3 times util.dragThreshold.
+            // but here the dragging does not update posX and posY
+            // posX and posY are only updated on mouseRelease
+            container2.reset()
+            mouseDrag(container2, container2.x + 10, container2.y + 10, 10*util.dragThreshold, 10*util.dragThreshold);
+            compare(spyX.count, 1)
+            compare(spyY.count, 1)
+
+            container2.updatePositionWhileDragging = true
+            // dx and dy are superior to 3 times util.dragThreshold.
+            // 3 intermediate mouseMove when dragging
+            container2.reset()
+            mouseDrag(container2, container2.x + 10, container2.y + 10, 10*util.dragThreshold, 10*util.dragThreshold);
+            compare(spyX.count, 3)
+            compare(spyY.count, 3)
+
+            // dx and dy are inferior to 3 times util.dragThreshold.
+            // No intermediate mouseMove when dragging, only one mouseMove
+            container2.reset()
+            mouseDrag(container2, container2.x + 10, container2.y + 10, 2*util.dragThreshold, 2*util.dragThreshold);
+            compare(spyX.count, 1)
+            compare(spyY.count, 1)
+
+            // dx is superior to 3 times util.dragThreshold.
+            // 3 intermediate mouseMove when dragging on x axis
+            // no move on the y axis
+            container2.reset()
+            mouseDrag(container2, container2.x + 10, container2.y + 10, 10*util.dragThreshold, 0);
+            compare(spyX.count, 3)
+            compare(spyY.count, 0)
+
+            // dy is inferior to 3 times util.dragThreshold.
+            // No intermediate mouseMove when dragging, only one mouseMove on y axis
+            // no move on the x axis
+            container2.reset()
+            mouseDrag(container2, container2.x + 10, container2.y + 10, 0, 2*util.dragThreshold);
+            compare(spyX.count, 0)
+            compare(spyY.count, 1)
+        }
     }
 }