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/>.
18import Ubuntu.Components 0.1
19import Unity.Test 0.1 as UT
24 // Fake implementation to be provided to items under test
25 property var fakeDateTime: new function() {
26 this.currentTimeMs = 0
27 this.getCurrentTimeMs = function() {return this.currentTimeMs}
30 // Flickable won't recognise a single mouse move as dragging the flickable.
31 // Use 5 steps because it's what
32 // Qt uses in QQuickViewTestUtil::flick
33 // speed is in pixels/second
34 function mouseFlick(item, x, y, toX, toY, pressMouse, releaseMouse,
36 pressMouse = ((pressMouse != null) ? pressMouse : true); // Default to true for pressMouse if not present
37 releaseMouse = ((releaseMouse != null) ? releaseMouse : true); // Default to true for releaseMouse if not present
39 // set a default speed if not specified
40 speed = (speed != null) ? speed : units.gu(10);
42 // set a default iterations if not specified
43 iterations = (iterations !== undefined) ? iterations : 5
45 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
46 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
48 var timeStep = totalTime / iterations
49 var diffX = (toX - x) / iterations
50 var diffY = (toY - y) / iterations
52 fakeDateTime.currentTimeMs += timeStep
53 mousePress(item, x, y)
55 for (var i = 0; i < iterations; ++i) {
56 fakeDateTime.currentTimeMs += timeStep
57 if (i === iterations - 1) {
58 // Avoid any rounding errors by making the last move be at precisely
59 // the point specified
60 mouseMove(item, toX, toY, iterations / speed)
62 mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed)
66 fakeDateTime.currentTimeMs += timeStep
67 mouseRelease(item, toX, toY)
72 // Find an object with the given name recursively, starting
73 // at the given object "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].data) {
82 childs.push(childs[0].data[i])
89 // Type a full string instead of keyClick letter by letter
90 // TODO: this is not ugly, this is uber-ugly and does not support
91 // any special character. Remove the keyMap once keyClick(obj, char)
92 // has landed in upstream Qt.
93 function typeString(str) {
159 for (var i = 0; i < str.length; i++) {
160 keyClick(keyMap[str[i]])
164 // Keeps executing a given parameter-less function until it returns the given
165 // expected result or the timemout is reached (in which case a test failure
167 function tryCompareFunction(func, expectedResult) {
172 while (timeSpent < timeout && !success) {
173 actualResult = func()
174 success = qtest_compareInternal(actualResult, expectedResult)
175 if (success === false) {
181 var act = qtest_results.stringify(actualResult)
182 var exp = qtest_results.stringify(expectedResult)
183 if (!qtest_results.compare(success,
184 "function returned unexpected result",
186 util.callerFile(), util.callerLine())) {
187 throw new Error("QtQuickTest::fail")
191 function touchEvent() {
192 return UT.Util.touchEvent()
195 // speed is in pixels/second
196 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
198 // Default to true for beginTouch if not present
199 beginTouch = (beginTouch !== undefined) ? beginTouch : true
201 // Default to true for endTouch if not present
202 endTouch = (endTouch !== undefined) ? endTouch : true
204 // Set a default speed if not specified
205 speed = (speed !== undefined) ? speed : units.gu(10)
207 // Set a default iterations if not specified
208 var iterations = (iterations !== undefined) ? iterations : 5
210 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
211 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
213 var timeStep = totalTime / iterations
214 var diffX = (toX - x) / iterations
215 var diffY = (toY - y) / iterations
217 fakeDateTime.currentTimeMs += timeStep
219 var event = touchEvent()
220 event.press(0 /* touchId */, x, y)
223 for (var i = 0; i < iterations; ++i) {
224 fakeDateTime.currentTimeMs += timeStep
225 if (i === iterations - 1) {
226 // Avoid any rounding errors by making the last move be at precisely
227 // the point specified
228 wait(iterations / speed)
229 var event = touchEvent()
230 event.move(0 /* touchId */, toX, toY)
233 wait(iterations / speed)
234 var event = touchEvent()
235 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
240 fakeDateTime.currentTimeMs += timeStep
241 var event = touchEvent()
242 event.release(0 /* touchId */, toX, toY)
247 function fetchRootItem(item) {
249 return fetchRootItem(item.parent)
254 function touchPress(item, x, y) {
255 var root = fetchRootItem(item)
256 var rootPoint = item.mapToItem(root, x, y)
258 var event = touchEvent()
259 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
263 function touchRelease(item, x, y) {
264 var root = fetchRootItem(item)
265 var rootPoint = item.mapToItem(root, x, y)
267 var event = touchEvent()
268 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
272 function tap(item, x, y) {
273 var event = touchEvent()
274 event.press(0 /* touchId */, x, y)
278 event.release(0 /* touchId */, x, y)
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