2
* Copyright 2016 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/>.
19
import Ubuntu.Components 1.3
20
import Ubuntu.Test 0.1 as UT
27
visible: testCase.running
28
anchors.centerIn: parent
29
Component.onCompleted: parent = testCase.parent
34
// Fake implementation to be provided to items under test
35
property var fakeDateTime: new function() {
36
this.currentTimeMs = 0
37
this.getCurrentTimeMs = function() {return this.currentTimeMs}
40
// Flickable won't recognise a single mouse move as dragging the flickable.
41
// Use 5 steps because it's what
42
// Qt uses in QQuickViewTestUtil::flick
43
// speed is in pixels/second
44
function mouseFlick(item, x, y, toX, toY, pressMouse, releaseMouse,
46
pressMouse = ((pressMouse != null) ? pressMouse : true); // Default to true for pressMouse if not present
47
releaseMouse = ((releaseMouse != null) ? releaseMouse : true); // Default to true for releaseMouse if not present
49
// set a default speed if not specified
50
speed = (speed != null) ? speed : units.gu(10);
52
// set a default iterations if not specified
53
iterations = (iterations !== undefined) ? iterations : 5
55
var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
56
var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
58
var timeStep = totalTime / iterations
59
var diffX = (toX - x) / iterations
60
var diffY = (toY - y) / iterations
62
fakeDateTime.currentTimeMs += timeStep
63
mousePress(item, x, y)
65
for (var i = 0; i < iterations; ++i) {
66
fakeDateTime.currentTimeMs += timeStep
67
if (i === iterations - 1) {
68
// Avoid any rounding errors by making the last move be at precisely
69
// the point specified
70
mouseMove(item, toX, toY, iterations / speed)
72
mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed)
76
fakeDateTime.currentTimeMs += timeStep
77
mouseRelease(item, toX, toY)
82
// Find an object with the given name in the children tree of "obj"
83
function findChild(obj,objectName) {
84
var childs = new Array(0);
86
while (childs.length > 0) {
87
if (childs[0].objectName == objectName) {
90
for (var i in childs[0].children) {
91
childs.push(childs[0].children[i])
98
// Find an object with the given name in the children tree of "obj"
99
// Including invisible children like animations, timers etc.
100
// Note: you should use findChild if you're not sure you need this
101
// as this tree is much bigger and might contain stuff that goes
103
function findInvisibleChild(obj,objectName) {
104
var childs = new Array(0);
106
while (childs.length > 0) {
107
if (childs[0].objectName == objectName) {
110
for (var i in childs[0].data) {
111
childs.push(childs[0].data[i])
118
// Type a full string instead of keyClick letter by letter
119
// TODO: this is not ugly, this is uber-ugly and does not support
120
// any special character. Remove the keyMap once keyClick(obj, char)
121
// has landed in upstream Qt.
122
function typeString(str) {
188
for (var i = 0; i < str.length; i++) {
189
keyClick(keyMap[str[i]])
193
// Keeps executing a given parameter-less function until it returns the given
194
// expected result or the timemout is reached (in which case a test failure
196
function tryCompareFunction(func, expectedResult) {
201
while (timeSpent < timeout && !success) {
202
actualResult = func()
203
success = qtest_compareInternal(actualResult, expectedResult)
204
if (success === false) {
210
var act = qtest_results.stringify(actualResult)
211
var exp = qtest_results.stringify(expectedResult)
212
if (!qtest_results.compare(success,
213
"function returned unexpected result",
215
util.callerFile(), util.callerLine())) {
216
throw new Error("QtQuickTest::fail")
220
function touchEvent() {
221
return UT.Util.touchEvent()
224
// speed is in pixels/second
225
function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
227
// Default to true for beginTouch if not present
228
beginTouch = (beginTouch !== undefined) ? beginTouch : true
230
// Default to true for endTouch if not present
231
endTouch = (endTouch !== undefined) ? endTouch : true
233
// Set a default speed if not specified
234
speed = (speed !== undefined) ? speed : units.gu(10)
236
// Set a default iterations if not specified
237
var iterations = (iterations !== undefined) ? iterations : 5
239
var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
240
var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
242
var timeStep = totalTime / iterations
243
var diffX = (toX - x) / iterations
244
var diffY = (toY - y) / iterations
246
fakeDateTime.currentTimeMs += timeStep
248
var event = touchEvent()
249
event.press(0 /* touchId */, x, y)
252
for (var i = 0; i < iterations; ++i) {
253
fakeDateTime.currentTimeMs += timeStep
254
if (i === iterations - 1) {
255
// Avoid any rounding errors by making the last move be at precisely
256
// the point specified
257
wait(iterations / speed)
258
var event = touchEvent()
259
event.move(0 /* touchId */, toX, toY)
262
wait(iterations / speed)
263
var event = touchEvent()
264
event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
269
fakeDateTime.currentTimeMs += timeStep
270
var event = touchEvent()
271
event.release(0 /* touchId */, toX, toY)
276
function fetchRootItem(item) {
278
return fetchRootItem(item.parent)
283
function touchPress(item, x, y) {
284
var root = fetchRootItem(item)
285
var rootPoint = item.mapToItem(root, x, y)
287
var event = touchEvent()
288
event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
292
function touchRelease(item, x, y) {
293
var root = fetchRootItem(item)
294
var rootPoint = item.mapToItem(root, x, y)
296
var event = touchEvent()
297
event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
301
function tap(item, x, y) {
302
var event = touchEvent()
303
event.press(0 /* touchId */, x, y)
307
event.release(0 /* touchId */, x, y)
311
// TODO This function can be removed altogether once we use Qt 5.5 which has the same feature
312
function waitForRendering(item, timeout) {
313
if (timeout === undefined)
316
qtest_fail("No item given to waitForRendering", 1);
317
return qtest_results.waitForRendering(item, timeout);