2 * Copyright 2013 Canonical Ltd.
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.
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.
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/>.
18import Ubuntu.Components 0.1
19import Unity.Test 0.1 as UT
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}
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,
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
39 // set a default speed if not specified
40 speed = (speed != null) ? speed : units.gu(10);
42 // set a default iterations if not specified
43 iterations = (iterations !== undefined) ? iterations : 5
45 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
46 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
48 var timeStep = totalTime / iterations
49 var diffX = (toX - x) / iterations
50 var diffY = (toY - y) / iterations
52 fakeDateTime.currentTimeMs += timeStep
53 mousePress(item, x, y)
55 for (var i = 0; i < iterations; ++i) {
56 fakeDateTime.currentTimeMs += timeStep
57 if (i === iterations - 1) {
58 // Avoid any rounding errors by making the last move be at precisely
59 // the point specified
60 mouseMove(item, toX, toY, iterations / speed)
62 mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed)
66 fakeDateTime.currentTimeMs += timeStep
67 mouseRelease(item, toX, toY)
72 // Find an object with the given name in the children tree of "obj"
73 function findChild(obj,objectName) {
74 var childs = new Array(0);
76 while (childs.length > 0) {
77 if (childs[0].objectName == objectName) {
80 for (var i in childs[0].children) {
81 childs.push(childs[0].children[i])
88 // Find an object with the given name in the children tree of "obj"
89 // Including invisible children like animations, timers etc.
90 // Note: you should use findChild if you're not sure you need this
91 // as this tree is much bigger and might contain stuff that goes
93 function findInvisibleChild(obj,objectName) {
94 var childs = new Array(0);
96 while (childs.length > 0) {
97 if (childs[0].objectName == objectName) {
100 for (var i in childs[0].data) {
101 childs.push(childs[0].data[i])
108 // Type a full string instead of keyClick letter by letter
109 // TODO: this is not ugly, this is uber-ugly and does not support
110 // any special character. Remove the keyMap once keyClick(obj, char)
111 // has landed in upstream Qt.
112 function typeString(str) {
178 for (var i = 0; i < str.length; i++) {
179 keyClick(keyMap[str[i]])
183 // Keeps executing a given parameter-less function until it returns the given
184 // expected result or the timemout is reached (in which case a test failure
186 function tryCompareFunction(func, expectedResult) {
191 while (timeSpent < timeout && !success) {
192 actualResult = func()
193 success = qtest_compareInternal(actualResult, expectedResult)
194 if (success === false) {
200 var act = qtest_results.stringify(actualResult)
201 var exp = qtest_results.stringify(expectedResult)
202 if (!qtest_results.compare(success,
203 "function returned unexpected result",
205 util.callerFile(), util.callerLine())) {
206 throw new Error("QtQuickTest::fail")
210 function touchEvent() {
211 return UT.Util.touchEvent()
214 // speed is in pixels/second
215 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
217 // Default to true for beginTouch if not present
218 beginTouch = (beginTouch !== undefined) ? beginTouch : true
220 // Default to true for endTouch if not present
221 endTouch = (endTouch !== undefined) ? endTouch : true
223 // Set a default speed if not specified
224 speed = (speed !== undefined) ? speed : units.gu(10)
226 // Set a default iterations if not specified
227 var iterations = (iterations !== undefined) ? iterations : 5
229 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
230 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
232 var timeStep = totalTime / iterations
233 var diffX = (toX - x) / iterations
234 var diffY = (toY - y) / iterations
236 fakeDateTime.currentTimeMs += timeStep
238 var event = touchEvent()
239 event.press(0 /* touchId */, x, y)
242 for (var i = 0; i < iterations; ++i) {
243 fakeDateTime.currentTimeMs += timeStep
244 if (i === iterations - 1) {
245 // Avoid any rounding errors by making the last move be at precisely
246 // the point specified
247 wait(iterations / speed)
248 var event = touchEvent()
249 event.move(0 /* touchId */, toX, toY)
252 wait(iterations / speed)
253 var event = touchEvent()
254 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
259 fakeDateTime.currentTimeMs += timeStep
260 var event = touchEvent()
261 event.release(0 /* touchId */, toX, toY)
266 function fetchRootItem(item) {
268 return fetchRootItem(item.parent)
273 function touchPress(item, x, y) {
274 var root = fetchRootItem(item)
275 var rootPoint = item.mapToItem(root, x, y)
277 var event = touchEvent()
278 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
282 function touchRelease(item, x, y) {
283 var root = fetchRootItem(item)
284 var rootPoint = item.mapToItem(root, x, y)
286 var event = touchEvent()
287 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
291 function tap(item, x, y) {
292 var event = touchEvent()
293 event.press(0 /* touchId */, x, y)
297 event.release(0 /* touchId */, x, y)
302 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
303 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
304 Thus to remove a variable that qmltests cannot really control, namely time, this
305 function removes all constraints from DirectionalDragAreas that are sensible to
308 This effectively makes DirectionalDragAreas easier to fool.
310 function removeTimeConstraintsFromDirectionalDragAreas(item) {
312 // use duck-typing to identify a DirectionalDragArea
313 if (item.minSpeed != undefined
314 && item.maxSilenceTime != undefined
315 && item.compositionTime != undefined) {
317 item.maxSilenceTime = 60 * 60 * 1000;
318 item.compositionTime = 0;
320 for (var i in item.children) {
321 removeTimeConstraintsFromDirectionalDragAreas(item.children[i]);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list