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) {
218 // Default to true for beginTouch if not present
219 beginTouch = (beginTouch !== undefined) ? beginTouch : true
221 // Default to true for endTouch if not present
222 endTouch = (endTouch !== undefined) ? endTouch : true
224 // Set a default speed if not specified
225 speed = (speed !== undefined) ? speed : units.gu(10)
227 // Set a default iterations if not specified
228 var iterations = (iterations !== undefined) ? iterations : 5
230 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
231 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
233 var timeStep = totalTime / iterations
234 var diffX = (toX - x) / iterations
235 var diffY = (toY - y) / iterations
237 fakeDateTime.currentTimeMs += timeStep
239 var event = touchEvent()
240 event.press(0 /* touchId */, x, y)
243 for (var i = 0; i < iterations; ++i) {
244 fakeDateTime.currentTimeMs += timeStep
245 if (i === iterations - 1) {
246 // Avoid any rounding errors by making the last move be at precisely
247 // the point specified
248 wait(iterations / speed)
249 var event = touchEvent()
250 event.move(0 /* touchId */, toX, toY)
253 wait(iterations / speed)
254 var event = touchEvent()
255 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
260 fakeDateTime.currentTimeMs += timeStep
261 var event = touchEvent()
262 event.release(0 /* touchId */, toX, toY)
267 function fetchRootItem(item) {
269 return fetchRootItem(item.parent)
274 function touchPress(item, x, y) {
275 var root = fetchRootItem(item)
276 var rootPoint = item.mapToItem(root, x, y)
278 var event = touchEvent()
279 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
283 function touchRelease(item, x, y) {
284 var root = fetchRootItem(item)
285 var rootPoint = item.mapToItem(root, x, y)
287 var event = touchEvent()
288 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
292 function tap(item, x, y) {
293 var event = touchEvent()
294 event.press(0 /* touchId */, x, y)
298 event.release(0 /* touchId */, x, y)
302 Component.onCompleted: {
303 var rootItem = parent;
304 while (rootItem.parent != undefined) {
305 rootItem = rootItem.parent;
307 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
311 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
312 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
313 Thus to remove a variable that qmltests cannot really control, namely time, this
314 function removes all constraints from DirectionalDragAreas that are sensible to
317 This effectively makes DirectionalDragAreas easier to fool.
319 function removeTimeConstraintsFromDirectionalDragAreas(item) {
321 // use duck-typing to identify a DirectionalDragArea
322 if (item.minSpeed != undefined
323 && item.maxSilenceTime != undefined
324 && item.compositionTime != undefined) {
326 item.maxSilenceTime = 60 * 60 * 1000;
327 item.compositionTime = 0;
329 for (var i in item.children) {
330 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