~unity-team/+junk/dashboard-playground

« back to all changes in this revision

Viewing changes to tests/utils/modules/Unity/Test/UnityTestCase.qml

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
import QtTest 1.0
 
18
import Ubuntu.Components 0.1
 
19
import Unity.Test 0.1 as UT
 
20
 
 
21
TestCase {
 
22
    TestUtil {id:util}
 
23
 
 
24
    // Fake implementation to be provided to items under test
 
25
    property var fakeDateTime: new function() {
 
26
        this.currentTimeMs = 0
 
27
        this.getCurrentTimeMs = function() {return this.currentTimeMs}
 
28
    }
 
29
 
 
30
    // Flickable won't recognise a single mouse move as dragging the flickable.
 
31
    // Use 5 steps because it's what
 
32
    // Qt uses in QQuickViewTestUtil::flick
 
33
    // speed is in pixels/second
 
34
    function mouseFlick(item, x, y, toX, toY, pressMouse, releaseMouse,
 
35
                        speed) {
 
36
        pressMouse = ((pressMouse != null) ? pressMouse : true); // Default to true for pressMouse if not present
 
37
        releaseMouse = ((releaseMouse != null) ? releaseMouse : true); // Default to true for releaseMouse if not present
 
38
 
 
39
        // set a default speed if not specified
 
40
        speed = (speed != null) ? speed : units.gu(10);
 
41
 
 
42
        var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
 
43
        var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
 
44
 
 
45
        var nIterations = 5
 
46
        var timeStep = totalTime / nIterations
 
47
        var diffX = (toX - x) / nIterations
 
48
        var diffY = (toY - y) / nIterations
 
49
        if (pressMouse) {
 
50
            fakeDateTime.currentTimeMs += timeStep
 
51
            mousePress(item, x, y)
 
52
        }
 
53
        for (var i = 0; i < nIterations; ++i) {
 
54
            fakeDateTime.currentTimeMs += timeStep
 
55
            if (i === nIterations - 1) {
 
56
                // Avoid any rounding errors by making the last move be at precisely
 
57
                // the point specified
 
58
                mouseMove(item, toX, toY, nIterations / speed)
 
59
            } else {
 
60
                mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, nIterations / speed)
 
61
            }
 
62
        }
 
63
        if (releaseMouse) {
 
64
            fakeDateTime.currentTimeMs += timeStep
 
65
            mouseRelease(item, toX, toY)
 
66
        }
 
67
    }
 
68
 
 
69
 
 
70
    // Find an object with the given name recursively, starting
 
71
    // at the given object "obj"
 
72
    function findChild(obj,objectName) {
 
73
        for (var i in obj.children) {
 
74
            var child = obj.children[i];
 
75
            if (child.objectName === objectName) return child;
 
76
            var subChild = findChild(child,objectName);
 
77
            if (subChild !== undefined) return subChild;
 
78
        }
 
79
        return undefined;
 
80
    }
 
81
 
 
82
    // Type a full string instead of keyClick letter by letter
 
83
    // TODO: this is not ugly, this is uber-ugly and does not support
 
84
    // any special character. Remove the keyMap once keyClick(obj, char)
 
85
    // has landed in upstream Qt.
 
86
    function typeString(str) {
 
87
        var keyMap = {
 
88
            "a": Qt.Key_A,
 
89
            "b": Qt.Key_B,
 
90
            "c": Qt.Key_C,
 
91
            "d": Qt.Key_D,
 
92
            "e": Qt.Key_E,
 
93
            "f": Qt.Key_F,
 
94
            "g": Qt.Key_G,
 
95
            "h": Qt.Key_H,
 
96
            "i": Qt.Key_I,
 
97
            "j": Qt.Key_J,
 
98
            "k": Qt.Key_K,
 
99
            "l": Qt.Key_L,
 
100
            "m": Qt.Key_M,
 
101
            "n": Qt.Key_N,
 
102
            "o": Qt.Key_O,
 
103
            "p": Qt.Key_P,
 
104
            "q": Qt.Key_Q,
 
105
            "r": Qt.Key_R,
 
106
            "s": Qt.Key_S,
 
107
            "t": Qt.Key_T,
 
108
            "u": Qt.Key_U,
 
109
            "v": Qt.Key_V,
 
110
            "w": Qt.Key_W,
 
111
            "x": Qt.Key_X,
 
112
            "y": Qt.Key_Y,
 
113
            "z": Qt.Key_Z,
 
114
            "A": Qt.Key_A,
 
115
            "B": Qt.Key_B,
 
116
            "C": Qt.Key_C,
 
117
            "D": Qt.Key_D,
 
118
            "E": Qt.Key_E,
 
119
            "F": Qt.Key_F,
 
120
            "G": Qt.Key_G,
 
121
            "H": Qt.Key_H,
 
122
            "I": Qt.Key_I,
 
123
            "J": Qt.Key_J,
 
124
            "K": Qt.Key_K,
 
125
            "L": Qt.Key_L,
 
126
            "M": Qt.Key_M,
 
127
            "N": Qt.Key_N,
 
128
            "O": Qt.Key_O,
 
129
            "P": Qt.Key_P,
 
130
            "Q": Qt.Key_Q,
 
131
            "R": Qt.Key_R,
 
132
            "S": Qt.Key_S,
 
133
            "T": Qt.Key_T,
 
134
            "U": Qt.Key_U,
 
135
            "V": Qt.Key_V,
 
136
            "W": Qt.Key_W,
 
137
            "X": Qt.Key_X,
 
138
            "Y": Qt.Key_Y,
 
139
            "Z": Qt.Key_Z,
 
140
            "0": Qt.Key_0,
 
141
            "1": Qt.Key_1,
 
142
            "2": Qt.Key_2,
 
143
            "3": Qt.Key_3,
 
144
            "4": Qt.Key_4,
 
145
            "5": Qt.Key_5,
 
146
            "6": Qt.Key_6,
 
147
            "7": Qt.Key_7,
 
148
            "8": Qt.Key_8,
 
149
            "9": Qt.Key_9,
 
150
            " ": Qt.Key_Space,
 
151
        }
 
152
        for (var i = 0; i < str.length; i++) {
 
153
            keyClick(keyMap[str[i]])
 
154
        }
 
155
    }
 
156
 
 
157
    // Keeps executing a given parameter-less function until it returns the given
 
158
    // expected result or the timemout is reached (in which case a test failure
 
159
    // is generated)
 
160
    function tryCompareFunction(func, expectedResult) {
 
161
        var timeSpent = 0
 
162
        var timeout = 5000
 
163
        var success = false
 
164
        var actualResult
 
165
        while (timeSpent < timeout && !success) {
 
166
            actualResult = func()
 
167
            success = qtest_compareInternal(actualResult, expectedResult)
 
168
            if (success === false) {
 
169
                wait(50)
 
170
                timeSpent += 50
 
171
            }
 
172
        }
 
173
 
 
174
        var act = qtest_results.stringify(actualResult)
 
175
        var exp = qtest_results.stringify(expectedResult)
 
176
        if (!qtest_results.compare(success,
 
177
                                   "function returned unexpected result",
 
178
                                   act, exp,
 
179
                                   util.callerFile(), util.callerLine())) {
 
180
            throw new Error("QtQuickTest::fail")
 
181
        }
 
182
    }
 
183
 
 
184
    function touchEvent() {
 
185
        return UT.Util.touchEvent()
 
186
    }
 
187
 
 
188
    // speed is in pixels/second
 
189
    function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed) {
 
190
 
 
191
        // Default to true for beginTouch if not present
 
192
        beginTouch = (beginTouch !== undefined) ? beginTouch : true
 
193
 
 
194
        // Default to true for endTouch if not present
 
195
        endTouch = (endTouch !== undefined) ? endTouch : true
 
196
 
 
197
        // Set a default speed if not specified
 
198
        speed = (speed !== undefined) ? speed : units.gu(10)
 
199
 
 
200
        var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
 
201
        var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
 
202
 
 
203
        var nIterations = 5
 
204
        var timeStep = totalTime / nIterations
 
205
        var diffX = (toX - x) / nIterations
 
206
        var diffY = (toY - y) / nIterations
 
207
        if (beginTouch) {
 
208
            fakeDateTime.currentTimeMs += timeStep
 
209
 
 
210
            var event = touchEvent()
 
211
            event.press(0 /* touchId */, x, y)
 
212
            event.commit()
 
213
        }
 
214
        for (var i = 0; i < nIterations; ++i) {
 
215
            fakeDateTime.currentTimeMs += timeStep
 
216
            if (i === nIterations - 1) {
 
217
                // Avoid any rounding errors by making the last move be at precisely
 
218
                // the point specified
 
219
                wait(nIterations / speed)
 
220
                var event = touchEvent()
 
221
                event.move(0 /* touchId */, toX, toY)
 
222
                event.commit()
 
223
            } else {
 
224
                wait(nIterations / speed)
 
225
                var event = touchEvent()
 
226
                event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
 
227
                event.commit()
 
228
            }
 
229
        }
 
230
        if (endTouch) {
 
231
            fakeDateTime.currentTimeMs += timeStep
 
232
            var event = touchEvent()
 
233
            event.release(0 /* touchId */, toX, toY)
 
234
            event.commit()
 
235
        }
 
236
    }
 
237
 
 
238
    function fetchRootItem(item) {
 
239
        if (item.parent)
 
240
            return fetchRootItem(item.parent)
 
241
        else
 
242
            return item
 
243
    }
 
244
 
 
245
    function touchPress(item, x, y) {
 
246
        var root = fetchRootItem(item)
 
247
        var rootPoint = item.mapToItem(root, x, y)
 
248
 
 
249
        var event = touchEvent()
 
250
        event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
 
251
        event.commit()
 
252
    }
 
253
 
 
254
    function touchRelease(item, x, y) {
 
255
        var root = fetchRootItem(item)
 
256
        var rootPoint = item.mapToItem(root, x, y)
 
257
 
 
258
        var event = touchEvent()
 
259
        event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
 
260
        event.commit()
 
261
    }
 
262
 
 
263
    function tap(item, x, y) {
 
264
        var event = touchEvent()
 
265
        event.press(0 /* touchId */, x, y)
 
266
        event.commit()
 
267
 
 
268
        event = touchEvent()
 
269
        event.release(0 /* touchId */, x, y)
 
270
        event.commit()
 
271
    }
 
272
}