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 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
43 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
46 var timeStep = totalTime / nIterations
47 var diffX = (toX - x) / nIterations
48 var diffY = (toY - y) / nIterations
50 fakeDateTime.currentTimeMs += timeStep
51 mousePress(item, x, y)
53 for (var i = 0; i < nIterations; ++i) {
54 fakeDateTime.currentTimeMs += timeStep
55 if (i === nIterations - 1) {
56 // Avoid any rounding errors by making the last move be at precisely
57 // the point specified
58 mouseMove(item, toX, toY, nIterations / speed)
60 mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, nIterations / speed)
64 fakeDateTime.currentTimeMs += timeStep
65 mouseRelease(item, toX, toY)
70 // Find an object with the given name recursively, starting
71 // at the given object "obj"
72 function findChild(obj,objectName) {
73 for (var i in obj.children) {
74 var child = obj.children[i];
75 if (child.objectName === objectName) return child;
76 var subChild = findChild(child,objectName);
77 if (subChild !== undefined) return subChild;
82 // Type a full string instead of keyClick letter by letter
83 // TODO: this is not ugly, this is uber-ugly and does not support
84 // any special character. Remove the keyMap once keyClick(obj, char)
85 // has landed in upstream Qt.
86 function typeString(str) {
152 for (var i = 0; i < str.length; i++) {
153 keyClick(keyMap[str[i]])
157 // Keeps executing a given parameter-less function until it returns the given
158 // expected result or the timemout is reached (in which case a test failure
160 function tryCompareFunction(func, expectedResult) {
165 while (timeSpent < timeout && !success) {
166 actualResult = func()
167 success = qtest_compareInternal(actualResult, expectedResult)
168 if (success === false) {
174 var act = qtest_results.stringify(actualResult)
175 var exp = qtest_results.stringify(expectedResult)
176 if (!qtest_results.compare(success,
177 "function returned unexpected result",
179 util.callerFile(), util.callerLine())) {
180 throw new Error("QtQuickTest::fail")
184 function touchEvent() {
185 return UT.Util.touchEvent()
188 // speed is in pixels/second
189 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed) {
191 // Default to true for beginTouch if not present
192 beginTouch = (beginTouch !== undefined) ? beginTouch : true
194 // Default to true for endTouch if not present
195 endTouch = (endTouch !== undefined) ? endTouch : true
197 // Set a default speed if not specified
198 speed = (speed !== undefined) ? speed : units.gu(10)
200 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
201 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
204 var timeStep = totalTime / nIterations
205 var diffX = (toX - x) / nIterations
206 var diffY = (toY - y) / nIterations
208 fakeDateTime.currentTimeMs += timeStep
210 var event = touchEvent()
211 event.press(0 /* touchId */, x, y)
214 for (var i = 0; i < nIterations; ++i) {
215 fakeDateTime.currentTimeMs += timeStep
216 if (i === nIterations - 1) {
217 // Avoid any rounding errors by making the last move be at precisely
218 // the point specified
219 wait(nIterations / speed)
220 var event = touchEvent()
221 event.move(0 /* touchId */, toX, toY)
224 wait(nIterations / speed)
225 var event = touchEvent()
226 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
231 fakeDateTime.currentTimeMs += timeStep
232 var event = touchEvent()
233 event.release(0 /* touchId */, toX, toY)
238 function fetchRootItem(item) {
240 return fetchRootItem(item.parent)
245 function touchPress(item, x, y) {
246 var root = fetchRootItem(item)
247 var rootPoint = item.mapToItem(root, x, y)
249 var event = touchEvent()
250 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
254 function touchRelease(item, x, y) {
255 var root = fetchRootItem(item)
256 var rootPoint = item.mapToItem(root, x, y)
258 var event = touchEvent()
259 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
263 function tap(item, x, y) {
264 var event = touchEvent()
265 event.press(0 /* touchId */, x, y)
269 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