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 for (var i in obj.children) {
76 var child = obj.children[i];
77 if (child.objectName === objectName) return child;
78 var subChild = findChild(child,objectName);
79 if (subChild !== undefined) return subChild;
84 // Type a full string instead of keyClick letter by letter
85 // TODO: this is not ugly, this is uber-ugly and does not support
86 // any special character. Remove the keyMap once keyClick(obj, char)
87 // has landed in upstream Qt.
88 function typeString(str) {
154 for (var i = 0; i < str.length; i++) {
155 keyClick(keyMap[str[i]])
159 // Keeps executing a given parameter-less function until it returns the given
160 // expected result or the timemout is reached (in which case a test failure
162 function tryCompareFunction(func, expectedResult) {
167 while (timeSpent < timeout && !success) {
168 actualResult = func()
169 success = qtest_compareInternal(actualResult, expectedResult)
170 if (success === false) {
176 var act = qtest_results.stringify(actualResult)
177 var exp = qtest_results.stringify(expectedResult)
178 if (!qtest_results.compare(success,
179 "function returned unexpected result",
181 util.callerFile(), util.callerLine())) {
182 throw new Error("QtQuickTest::fail")
186 function touchEvent() {
187 return UT.Util.touchEvent()
190 // speed is in pixels/second
191 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
193 // Default to true for beginTouch if not present
194 beginTouch = (beginTouch !== undefined) ? beginTouch : true
196 // Default to true for endTouch if not present
197 endTouch = (endTouch !== undefined) ? endTouch : true
199 // Set a default speed if not specified
200 speed = (speed !== undefined) ? speed : units.gu(10)
202 // Set a default iterations if not specified
203 var iterations = (iterations !== undefined) ? iterations : 5
205 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
206 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
208 var timeStep = totalTime / iterations
209 var diffX = (toX - x) / iterations
210 var diffY = (toY - y) / iterations
212 fakeDateTime.currentTimeMs += timeStep
214 var event = touchEvent()
215 event.press(0 /* touchId */, x, y)
218 for (var i = 0; i < iterations; ++i) {
219 fakeDateTime.currentTimeMs += timeStep
220 if (i === iterations - 1) {
221 // Avoid any rounding errors by making the last move be at precisely
222 // the point specified
223 wait(iterations / speed)
224 var event = touchEvent()
225 event.move(0 /* touchId */, toX, toY)
228 wait(iterations / speed)
229 var event = touchEvent()
230 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
235 fakeDateTime.currentTimeMs += timeStep
236 var event = touchEvent()
237 event.release(0 /* touchId */, toX, toY)
242 function fetchRootItem(item) {
244 return fetchRootItem(item.parent)
249 function touchPress(item, x, y) {
250 var root = fetchRootItem(item)
251 var rootPoint = item.mapToItem(root, x, y)
253 var event = touchEvent()
254 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
258 function touchRelease(item, x, y) {
259 var root = fetchRootItem(item)
260 var rootPoint = item.mapToItem(root, x, y)
262 var event = touchEvent()
263 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
267 function tap(item, x, y) {
268 var event = touchEvent()
269 event.press(0 /* touchId */, x, y)
273 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