1631.7.1
by jonas-drange
ease creation of qml tests by adopting the same setup as ubuntu-settings-components |
1 |
/*
|
1631.7.2
by jonas-drange
years and formatting |
2 |
* Copyright 2016 Canonical Ltd.
|
1631.7.1
by jonas-drange
ease creation of qml tests by adopting the same setup as ubuntu-settings-components |
3 |
*
|
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.
|
|
7 |
*
|
|
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.
|
|
12 |
*
|
|
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/>.
|
|
15 |
*/
|
|
16 |
||
17 |
import QtQuick 2.4 |
|
18 |
import QtTest 1.0 |
|
19 |
import Ubuntu.Components 1.3 |
|
20 |
import Ubuntu.Test 0.1 as UT |
|
21 |
||
22 |
TestCase { |
|
23 |
id: testCase
|
|
24 |
TestUtil {id:util} |
|
25 |
||
26 |
ActivityIndicator { |
|
27 |
visible: testCase.running |
|
28 |
anchors.centerIn: parent |
|
29 |
Component.onCompleted: parent = testCase.parent |
|
30 |
z: 100 |
|
31 |
running: visible |
|
32 |
}
|
|
33 |
||
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} |
|
38 |
}
|
|
39 |
||
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, |
|
45 |
speed, iterations) { |
|
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 |
|
48 |
||
49 |
// set a default speed if not specified
|
|
50 |
speed = (speed != null) ? speed : units.gu(10); |
|
51 |
||
52 |
// set a default iterations if not specified
|
|
53 |
iterations = (iterations !== undefined) ? iterations : 5 |
|
54 |
||
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 */ |
|
57 |
||
58 |
var timeStep = totalTime / iterations |
|
59 |
var diffX = (toX - x) / iterations |
|
60 |
var diffY = (toY - y) / iterations |
|
61 |
if (pressMouse) { |
|
62 |
fakeDateTime.currentTimeMs += timeStep |
|
63 |
mousePress(item, x, y) |
|
64 |
}
|
|
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) |
|
71 |
} else { |
|
72 |
mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed) |
|
73 |
}
|
|
74 |
}
|
|
75 |
if (releaseMouse) { |
|
76 |
fakeDateTime.currentTimeMs += timeStep |
|
77 |
mouseRelease(item, toX, toY) |
|
78 |
}
|
|
79 |
}
|
|
80 |
||
81 |
||
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); |
|
85 |
childs.push(obj) |
|
86 |
while (childs.length > 0) { |
|
87 |
if (childs[0].objectName == objectName) { |
|
88 |
return childs[0] |
|
89 |
}
|
|
90 |
for (var i in childs[0].children) { |
|
91 |
childs.push(childs[0].children[i]) |
|
92 |
}
|
|
93 |
childs.splice(0, 1); |
|
94 |
}
|
|
95 |
return undefined; |
|
96 |
}
|
|
97 |
||
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
|
|
102 |
// away randomly.
|
|
103 |
function findInvisibleChild(obj,objectName) { |
|
104 |
var childs = new Array(0); |
|
105 |
childs.push(obj) |
|
106 |
while (childs.length > 0) { |
|
107 |
if (childs[0].objectName == objectName) { |
|
108 |
return childs[0] |
|
109 |
}
|
|
110 |
for (var i in childs[0].data) { |
|
111 |
childs.push(childs[0].data[i]) |
|
112 |
}
|
|
113 |
childs.splice(0, 1); |
|
114 |
}
|
|
115 |
return undefined; |
|
116 |
}
|
|
117 |
||
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) { |
|
123 |
var keyMap = { |
|
124 |
"a": Qt.Key_A, |
|
125 |
"b": Qt.Key_B, |
|
126 |
"c": Qt.Key_C, |
|
127 |
"d": Qt.Key_D, |
|
128 |
"e": Qt.Key_E, |
|
129 |
"f": Qt.Key_F, |
|
130 |
"g": Qt.Key_G, |
|
131 |
"h": Qt.Key_H, |
|
132 |
"i": Qt.Key_I, |
|
133 |
"j": Qt.Key_J, |
|
134 |
"k": Qt.Key_K, |
|
135 |
"l": Qt.Key_L, |
|
136 |
"m": Qt.Key_M, |
|
137 |
"n": Qt.Key_N, |
|
138 |
"o": Qt.Key_O, |
|
139 |
"p": Qt.Key_P, |
|
140 |
"q": Qt.Key_Q, |
|
141 |
"r": Qt.Key_R, |
|
142 |
"s": Qt.Key_S, |
|
143 |
"t": Qt.Key_T, |
|
144 |
"u": Qt.Key_U, |
|
145 |
"v": Qt.Key_V, |
|
146 |
"w": Qt.Key_W, |
|
147 |
"x": Qt.Key_X, |
|
148 |
"y": Qt.Key_Y, |
|
149 |
"z": Qt.Key_Z, |
|
150 |
"A": Qt.Key_A, |
|
151 |
"B": Qt.Key_B, |
|
152 |
"C": Qt.Key_C, |
|
153 |
"D": Qt.Key_D, |
|
154 |
"E": Qt.Key_E, |
|
155 |
"F": Qt.Key_F, |
|
156 |
"G": Qt.Key_G, |
|
157 |
"H": Qt.Key_H, |
|
158 |
"I": Qt.Key_I, |
|
159 |
"J": Qt.Key_J, |
|
160 |
"K": Qt.Key_K, |
|
161 |
"L": Qt.Key_L, |
|
162 |
"M": Qt.Key_M, |
|
163 |
"N": Qt.Key_N, |
|
164 |
"O": Qt.Key_O, |
|
165 |
"P": Qt.Key_P, |
|
166 |
"Q": Qt.Key_Q, |
|
167 |
"R": Qt.Key_R, |
|
168 |
"S": Qt.Key_S, |
|
169 |
"T": Qt.Key_T, |
|
170 |
"U": Qt.Key_U, |
|
171 |
"V": Qt.Key_V, |
|
172 |
"W": Qt.Key_W, |
|
173 |
"X": Qt.Key_X, |
|
174 |
"Y": Qt.Key_Y, |
|
175 |
"Z": Qt.Key_Z, |
|
176 |
"0": Qt.Key_0, |
|
177 |
"1": Qt.Key_1, |
|
178 |
"2": Qt.Key_2, |
|
179 |
"3": Qt.Key_3, |
|
180 |
"4": Qt.Key_4, |
|
181 |
"5": Qt.Key_5, |
|
182 |
"6": Qt.Key_6, |
|
183 |
"7": Qt.Key_7, |
|
184 |
"8": Qt.Key_8, |
|
185 |
"9": Qt.Key_9, |
|
186 |
" ": Qt.Key_Space, |
|
187 |
}
|
|
188 |
for (var i = 0; i < str.length; i++) { |
|
189 |
keyClick(keyMap[str[i]]) |
|
190 |
}
|
|
191 |
}
|
|
192 |
||
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
|
|
195 |
// is generated)
|
|
196 |
function tryCompareFunction(func, expectedResult) { |
|
197 |
var timeSpent = 0 |
|
198 |
var timeout = 5000 |
|
199 |
var success = false |
|
200 |
var actualResult |
|
201 |
while (timeSpent < timeout && !success) { |
|
202 |
actualResult = func() |
|
203 |
success = qtest_compareInternal(actualResult, expectedResult) |
|
204 |
if (success === false) { |
|
205 |
wait(50) |
|
206 |
timeSpent += 50 |
|
207 |
}
|
|
208 |
}
|
|
209 |
||
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", |
|
214 |
act, exp, |
|
215 |
util.callerFile(), util.callerLine())) { |
|
216 |
throw new Error("QtQuickTest::fail") |
|
217 |
}
|
|
218 |
}
|
|
219 |
||
220 |
function touchEvent() { |
|
221 |
return UT.Util.touchEvent() |
|
222 |
}
|
|
223 |
||
224 |
// speed is in pixels/second
|
|
225 |
function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) { |
|
226 |
||
227 |
// Default to true for beginTouch if not present
|
|
228 |
beginTouch = (beginTouch !== undefined) ? beginTouch : true |
|
229 |
||
230 |
// Default to true for endTouch if not present
|
|
231 |
endTouch = (endTouch !== undefined) ? endTouch : true |
|
232 |
||
233 |
// Set a default speed if not specified
|
|
234 |
speed = (speed !== undefined) ? speed : units.gu(10) |
|
235 |
||
236 |
// Set a default iterations if not specified
|
|
237 |
var iterations = (iterations !== undefined) ? iterations : 5 |
|
238 |
||
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 */ |
|
241 |
||
242 |
var timeStep = totalTime / iterations |
|
243 |
var diffX = (toX - x) / iterations |
|
244 |
var diffY = (toY - y) / iterations |
|
245 |
if (beginTouch) { |
|
246 |
fakeDateTime.currentTimeMs += timeStep |
|
247 |
||
248 |
var event = touchEvent() |
|
249 |
event.press(0 /* touchId */, x, y) |
|
250 |
event.commit() |
|
251 |
}
|
|
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) |
|
260 |
event.commit() |
|
261 |
} else { |
|
262 |
wait(iterations / speed) |
|
263 |
var event = touchEvent() |
|
264 |
event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY) |
|
265 |
event.commit() |
|
266 |
}
|
|
267 |
}
|
|
268 |
if (endTouch) { |
|
269 |
fakeDateTime.currentTimeMs += timeStep |
|
270 |
var event = touchEvent() |
|
271 |
event.release(0 /* touchId */, toX, toY) |
|
272 |
event.commit() |
|
273 |
}
|
|
274 |
}
|
|
275 |
||
276 |
function fetchRootItem(item) { |
|
277 |
if (item.parent) |
|
278 |
return fetchRootItem(item.parent) |
|
279 |
else
|
|
280 |
return item |
|
281 |
}
|
|
282 |
||
283 |
function touchPress(item, x, y) { |
|
284 |
var root = fetchRootItem(item) |
|
285 |
var rootPoint = item.mapToItem(root, x, y) |
|
286 |
||
287 |
var event = touchEvent() |
|
288 |
event.press(0 /* touchId */, rootPoint.x, rootPoint.y) |
|
289 |
event.commit() |
|
290 |
}
|
|
291 |
||
292 |
function touchRelease(item, x, y) { |
|
293 |
var root = fetchRootItem(item) |
|
294 |
var rootPoint = item.mapToItem(root, x, y) |
|
295 |
||
296 |
var event = touchEvent() |
|
297 |
event.release(0 /* touchId */, rootPoint.x, rootPoint.y) |
|
298 |
event.commit() |
|
299 |
}
|
|
300 |
||
301 |
function tap(item, x, y) { |
|
302 |
var event = touchEvent() |
|
303 |
event.press(0 /* touchId */, x, y) |
|
304 |
event.commit() |
|
305 |
||
306 |
event = touchEvent() |
|
307 |
event.release(0 /* touchId */, x, y) |
|
308 |
event.commit() |
|
309 |
}
|
|
310 |
||
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) |
|
314 |
timeout = 5000; |
|
315 |
if (!item) |
|
316 |
qtest_fail("No item given to waitForRendering", 1); |
|
317 |
return qtest_results.waitForRendering(item, timeout); |
|
318 |
}
|
|
319 |
}
|