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/>.
19 import Ubuntu.Components 0.1
20 import Unity.Test 0.1 as UT
25 // Fake implementation to be provided to items under test
26 property var fakeDateTime: new function() {
27 this.currentTimeMs = 0
28 this.getCurrentTimeMs = function() {return this.currentTimeMs}
31 // Flickable won't recognise a single mouse move as dragging the flickable.
32 // Use 5 steps because it's what
33 // Qt uses in QQuickViewTestUtil::flick
34 // speed is in pixels/second
35 function mouseFlick(item, x, y, toX, toY, pressMouse, releaseMouse,
37 pressMouse = ((pressMouse != null) ? pressMouse : true); // Default to true for pressMouse if not present
38 releaseMouse = ((releaseMouse != null) ? releaseMouse : true); // Default to true for releaseMouse if not present
40 // set a default speed if not specified
41 speed = (speed != null) ? speed : units.gu(10);
43 // set a default iterations if not specified
44 iterations = (iterations !== undefined) ? iterations : 5
46 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
47 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
49 var timeStep = totalTime / iterations
50 var diffX = (toX - x) / iterations
51 var diffY = (toY - y) / iterations
53 fakeDateTime.currentTimeMs += timeStep
54 mousePress(item, x, y)
56 for (var i = 0; i < iterations; ++i) {
57 fakeDateTime.currentTimeMs += timeStep
58 if (i === iterations - 1) {
59 // Avoid any rounding errors by making the last move be at precisely
60 // the point specified
61 mouseMove(item, toX, toY, iterations / speed)
63 mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed)
67 fakeDateTime.currentTimeMs += timeStep
68 mouseRelease(item, toX, toY)
73 // Find an object with the given name in the children tree of "obj"
74 function findChild(obj, objectName) {
75 return findChildIn(obj, "children", objectName);
78 // Find an object with the given name in the children tree of "obj"
79 // Including invisible children like animations, timers etc.
80 // Note: you should use findChild if you're not sure you need this
81 // as this tree is much bigger and might contain stuff that goes
83 function findInvisibleChild(obj, objectName) {
84 return findChildIn(obj, "data", objectName);
87 // Find a child in the named property
88 function findChildIn(obj, prop, objectName) {
89 var childs = new Array(0);
91 while (childs.length > 0) {
92 if (childs[0].objectName == objectName) {
95 for (var i in childs[0][prop]) {
96 childs.push(childs[0][prop][i])
103 // Type a full string instead of keyClick letter by letter
104 // TODO: this is not ugly, this is uber-ugly and does not support
105 // any special character. Remove the keyMap once keyClick(obj, char)
106 // has landed in upstream Qt.
107 function typeString(str) {
173 for (var i = 0; i < str.length; i++) {
174 keyClick(keyMap[str[i]])
178 // Keeps executing a given parameter-less function until it returns the given
179 // expected result or the timemout is reached (in which case a test failure
181 function tryCompareFunction(func, expectedResult, timeout) {
183 if (timeout === undefined)
187 while (timeSpent < timeout && !success) {
188 actualResult = func()
189 success = qtest_compareInternal(actualResult, expectedResult)
190 if (success === false) {
196 var act = qtest_results.stringify(actualResult)
197 var exp = qtest_results.stringify(expectedResult)
198 if (!qtest_results.compare(success,
199 "function returned unexpected result",
201 util.callerFile(), util.callerLine())) {
202 throw new Error("QtQuickTest::fail")
206 function touchEvent() {
207 return UT.Util.touchEvent()
210 // speed is in pixels/second
211 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
212 // Make sure the item is rendered
213 waitForRendering(item);
215 // Default to true for beginTouch if not present
216 beginTouch = (beginTouch !== undefined) ? beginTouch : true
218 // Default to true for endTouch if not present
219 endTouch = (endTouch !== undefined) ? endTouch : true
221 // Set a default speed if not specified
222 speed = (speed !== undefined) ? speed : units.gu(10)
224 // Set a default iterations if not specified
225 var iterations = (iterations !== undefined) ? iterations : 5
227 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
228 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
230 var timeStep = totalTime / iterations
231 var diffX = (toX - x) / iterations
232 var diffY = (toY - y) / iterations
234 fakeDateTime.currentTimeMs += timeStep
236 var event = touchEvent()
237 event.press(0 /* touchId */, x, y)
240 for (var i = 0; i < iterations; ++i) {
241 fakeDateTime.currentTimeMs += timeStep
242 if (i === iterations - 1) {
243 // Avoid any rounding errors by making the last move be at precisely
244 // the point specified
245 wait(iterations / speed)
246 var event = touchEvent()
247 event.move(0 /* touchId */, toX, toY)
250 wait(iterations / speed)
251 var event = touchEvent()
252 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
257 fakeDateTime.currentTimeMs += timeStep
258 var event = touchEvent()
259 event.release(0 /* touchId */, toX, toY)
264 function touchPinch(item, x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End) {
265 // Make sure the item is rendered
266 waitForRendering(item);
268 var event1 = touchEvent();
270 event1.press(0, x1Start, y1Start);
273 event1.stationary(0);
274 event1.press(1, x2Start, y2Start);
278 for (var i = 0.0; i < 1.0; i += 0.02) {
279 event1.move(0, x1Start + (x1End - x1Start) * i, y1Start + (y1End - y1Start) * i);
280 event1.move(1, x2Start + (x2End - x2Start) * i, y2Start + (y2End - y2Start) * i);
285 event1.release(0, x1End, y1End);
286 event1.release(1, x2End, y2End);
290 function fetchRootItem(item) {
292 return fetchRootItem(item.parent)
297 function touchPress(item, x, y) {
298 var root = fetchRootItem(item)
299 var rootPoint = item.mapToItem(root, x, y)
301 var event = touchEvent()
302 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
306 function touchRelease(item, x, y) {
307 var root = fetchRootItem(item)
308 var rootPoint = item.mapToItem(root, x, y)
310 var event = touchEvent()
311 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
315 function tap(item, x, y) {
316 var root = fetchRootItem(item)
317 var rootPoint = item.mapToItem(root, x, y)
319 var event = touchEvent()
320 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
324 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
328 Component.onCompleted: {
329 var rootItem = parent;
330 while (rootItem.parent != undefined) {
331 rootItem = rootItem.parent;
333 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
337 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
338 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
339 Thus to remove a variable that qmltests cannot really control, namely time, this
340 function removes all constraints from DirectionalDragAreas that are sensible to
343 This effectively makes DirectionalDragAreas easier to fool.
345 function removeTimeConstraintsFromDirectionalDragAreas(item) {
347 // use duck-typing to identify a DirectionalDragArea
348 if (item.minSpeed != undefined
349 && item.maxSilenceTime != undefined
350 && item.compositionTime != undefined) {
352 item.maxSilenceTime = 60 * 60 * 1000;
353 item.compositionTime = 0;
355 for (var i in item.children) {
356 removeTimeConstraintsFromDirectionalDragAreas(item.children[i]);