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/>.
19import Ubuntu.Components 0.1
20import 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 var childs = new Array(0);
77 while (childs.length > 0) {
78 if (childs[0].objectName == objectName) {
81 for (var i in childs[0].children) {
82 childs.push(childs[0].children[i])
89 // Find an object with the given name in the children tree of "obj"
90 // Including invisible children like animations, timers etc.
91 // Note: you should use findChild if you're not sure you need this
92 // as this tree is much bigger and might contain stuff that goes
94 function findInvisibleChild(obj,objectName) {
95 var childs = new Array(0);
97 while (childs.length > 0) {
98 if (childs[0].objectName == objectName) {
101 for (var i in childs[0].data) {
102 childs.push(childs[0].data[i])
109 // Type a full string instead of keyClick letter by letter
110 // TODO: this is not ugly, this is uber-ugly and does not support
111 // any special character. Remove the keyMap once keyClick(obj, char)
112 // has landed in upstream Qt.
113 function typeString(str) {
179 for (var i = 0; i < str.length; i++) {
180 keyClick(keyMap[str[i]])
184 // Keeps executing a given parameter-less function until it returns the given
185 // expected result or the timemout is reached (in which case a test failure
187 function tryCompareFunction(func, expectedResult) {
192 while (timeSpent < timeout && !success) {
193 actualResult = func()
194 success = qtest_compareInternal(actualResult, expectedResult)
195 if (success === false) {
201 var act = qtest_results.stringify(actualResult)
202 var exp = qtest_results.stringify(expectedResult)
203 if (!qtest_results.compare(success,
204 "function returned unexpected result",
206 util.callerFile(), util.callerLine())) {
207 throw new Error("QtQuickTest::fail")
211 function touchEvent() {
212 return UT.Util.touchEvent()
215 // speed is in pixels/second
216 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
217 // Make sure the item is rendered
218 waitForRendering(item);
220 // Default to true for beginTouch if not present
221 beginTouch = (beginTouch !== undefined) ? beginTouch : true
223 // Default to true for endTouch if not present
224 endTouch = (endTouch !== undefined) ? endTouch : true
226 // Set a default speed if not specified
227 speed = (speed !== undefined) ? speed : units.gu(10)
229 // Set a default iterations if not specified
230 var iterations = (iterations !== undefined) ? iterations : 5
232 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
233 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
235 var timeStep = totalTime / iterations
236 var diffX = (toX - x) / iterations
237 var diffY = (toY - y) / iterations
239 fakeDateTime.currentTimeMs += timeStep
241 var event = touchEvent()
242 event.press(0 /* touchId */, x, y)
245 for (var i = 0; i < iterations; ++i) {
246 fakeDateTime.currentTimeMs += timeStep
247 if (i === iterations - 1) {
248 // Avoid any rounding errors by making the last move be at precisely
249 // the point specified
250 wait(iterations / speed)
251 var event = touchEvent()
252 event.move(0 /* touchId */, toX, toY)
255 wait(iterations / speed)
256 var event = touchEvent()
257 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
262 fakeDateTime.currentTimeMs += timeStep
263 var event = touchEvent()
264 event.release(0 /* touchId */, toX, toY)
269 function fetchRootItem(item) {
271 return fetchRootItem(item.parent)
276 function touchPress(item, x, y) {
277 var root = fetchRootItem(item)
278 var rootPoint = item.mapToItem(root, x, y)
280 var event = touchEvent()
281 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
285 function touchRelease(item, x, y) {
286 var root = fetchRootItem(item)
287 var rootPoint = item.mapToItem(root, x, y)
289 var event = touchEvent()
290 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
294 function tap(item, x, y) {
295 var event = touchEvent()
296 event.press(0 /* touchId */, x, y)
300 event.release(0 /* touchId */, x, y)
304 Component.onCompleted: {
305 var rootItem = parent;
306 while (rootItem.parent != undefined) {
307 rootItem = rootItem.parent;
309 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
313 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
314 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
315 Thus to remove a variable that qmltests cannot really control, namely time, this
316 function removes all constraints from DirectionalDragAreas that are sensible to
319 This effectively makes DirectionalDragAreas easier to fool.
321 function removeTimeConstraintsFromDirectionalDragAreas(item) {
323 // use duck-typing to identify a DirectionalDragArea
324 if (item.minSpeed != undefined
325 && item.maxSilenceTime != undefined
326 && item.compositionTime != undefined) {
328 item.maxSilenceTime = 60 * 60 * 1000;
329 item.compositionTime = 0;
331 for (var i in item.children) {
332 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