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 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 fetchRootItem(item) {
265 return fetchRootItem(item.parent)
270 function touchPress(item, x, y) {
271 var root = fetchRootItem(item)
272 var rootPoint = item.mapToItem(root, x, y)
274 var event = touchEvent()
275 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
279 function touchRelease(item, x, y) {
280 var root = fetchRootItem(item)
281 var rootPoint = item.mapToItem(root, x, y)
283 var event = touchEvent()
284 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
288 function tap(item, x, y) {
289 var event = touchEvent()
290 event.press(0 /* touchId */, x, y)
294 event.release(0 /* touchId */, x, y)
298 Component.onCompleted: {
299 var rootItem = parent;
300 while (rootItem.parent != undefined) {
301 rootItem = rootItem.parent;
303 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
307 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
308 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
309 Thus to remove a variable that qmltests cannot really control, namely time, this
310 function removes all constraints from DirectionalDragAreas that are sensible to
313 This effectively makes DirectionalDragAreas easier to fool.
315 function removeTimeConstraintsFromDirectionalDragAreas(item) {
317 // use duck-typing to identify a DirectionalDragArea
318 if (item.minSpeed != undefined
319 && item.maxSilenceTime != undefined
320 && item.compositionTime != undefined) {
322 item.maxSilenceTime = 60 * 60 * 1000;
323 item.compositionTime = 0;
325 for (var i in item.children) {
326 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