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) {
186 while (timeSpent < timeout && !success) {
187 actualResult = func()
188 success = qtest_compareInternal(actualResult, expectedResult)
189 if (success === false) {
195 var act = qtest_results.stringify(actualResult)
196 var exp = qtest_results.stringify(expectedResult)
197 if (!qtest_results.compare(success,
198 "function returned unexpected result",
200 util.callerFile(), util.callerLine())) {
201 throw new Error("QtQuickTest::fail")
205 function touchEvent() {
206 return UT.Util.touchEvent()
209 // speed is in pixels/second
210 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
211 // Make sure the item is rendered
212 waitForRendering(item);
214 // Default to true for beginTouch if not present
215 beginTouch = (beginTouch !== undefined) ? beginTouch : true
217 // Default to true for endTouch if not present
218 endTouch = (endTouch !== undefined) ? endTouch : true
220 // Set a default speed if not specified
221 speed = (speed !== undefined) ? speed : units.gu(10)
223 // Set a default iterations if not specified
224 var iterations = (iterations !== undefined) ? iterations : 5
226 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
227 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
229 var timeStep = totalTime / iterations
230 var diffX = (toX - x) / iterations
231 var diffY = (toY - y) / iterations
233 fakeDateTime.currentTimeMs += timeStep
235 var event = touchEvent()
236 event.press(0 /* touchId */, x, y)
239 for (var i = 0; i < iterations; ++i) {
240 fakeDateTime.currentTimeMs += timeStep
241 if (i === iterations - 1) {
242 // Avoid any rounding errors by making the last move be at precisely
243 // the point specified
244 wait(iterations / speed)
245 var event = touchEvent()
246 event.move(0 /* touchId */, toX, toY)
249 wait(iterations / speed)
250 var event = touchEvent()
251 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
256 fakeDateTime.currentTimeMs += timeStep
257 var event = touchEvent()
258 event.release(0 /* touchId */, toX, toY)
263 function touchPinch(item, x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End) {
264 // Make sure the item is rendered
265 waitForRendering(item);
267 var event1 = touchEvent();
269 event1.press(0, x1Start, y1Start);
272 event1.stationary(0);
273 event1.press(1, x2Start, y2Start);
277 for (var i = 0.0; i < 1.0; i += 0.02) {
278 event1.move(0, x1Start + (x1End - x1Start) * i, y1Start + (y1End - y1Start) * i);
279 event1.move(1, x2Start + (x2End - x2Start) * i, y2Start + (y2End - y2Start) * i);
284 event1.release(0, x1End, y1End);
285 event1.release(1, x2End, y2End);
289 function fetchRootItem(item) {
291 return fetchRootItem(item.parent)
296 function touchPress(item, x, y) {
297 var root = fetchRootItem(item)
298 var rootPoint = item.mapToItem(root, x, y)
300 var event = touchEvent()
301 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
305 function touchRelease(item, x, y) {
306 var root = fetchRootItem(item)
307 var rootPoint = item.mapToItem(root, x, y)
309 var event = touchEvent()
310 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
314 function tap(item, x, y) {
315 var root = fetchRootItem(item)
316 var rootPoint = item.mapToItem(root, x, y)
318 var event = touchEvent()
319 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
323 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
327 Component.onCompleted: {
328 var rootItem = parent;
329 while (rootItem.parent != undefined) {
330 rootItem = rootItem.parent;
332 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
336 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
337 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
338 Thus to remove a variable that qmltests cannot really control, namely time, this
339 function removes all constraints from DirectionalDragAreas that are sensible to
342 This effectively makes DirectionalDragAreas easier to fool.
344 function removeTimeConstraintsFromDirectionalDragAreas(item) {
346 // use duck-typing to identify a DirectionalDragArea
347 if (item.minSpeed != undefined
348 && item.maxSilenceTime != undefined
349 && item.compositionTime != undefined) {
351 item.maxSilenceTime = 60 * 60 * 1000;
352 item.compositionTime = 0;
354 for (var i in item.children) {
355 removeTimeConstraintsFromDirectionalDragAreas(item.children[i]);