on b2c7059 help sign in
unity8/tests/utils/modules/Unity/Test/UnityTestCase.qml
359 lines 12 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
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 */
17import QtQuick 2.0
18import QtTest 1.0
19import Ubuntu.Components 0.1
20import Unity.Test 0.1 as UT
22TestCase {
23 TestUtil {id:util}
25 // Fake implementation to be provided to items under test
26 property var fakeDateTime: new function() {
27 this.currentTimeMs = 0
28 this.getCurrentTimeMs = function() {return this.currentTimeMs}
29 }
31 // Flickable won't recognise a single mouse move as dragging the flickable.
32 // Use 5 steps because it's what
33 // Qt uses in QQuickViewTestUtil::flick
34 // speed is in pixels/second
35 function mouseFlick(item, x, y, toX, toY, pressMouse, releaseMouse,
36 speed, iterations) {
37 pressMouse = ((pressMouse != null) ? pressMouse : true); // Default to true for pressMouse if not present
38 releaseMouse = ((releaseMouse != null) ? releaseMouse : true); // Default to true for releaseMouse if not present
40 // set a default speed if not specified
41 speed = (speed != null) ? speed : units.gu(10);
43 // set a default iterations if not specified
44 iterations = (iterations !== undefined) ? iterations : 5
46 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
47 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
49 var timeStep = totalTime / iterations
50 var diffX = (toX - x) / iterations
51 var diffY = (toY - y) / iterations
52 if (pressMouse) {
53 fakeDateTime.currentTimeMs += timeStep
54 mousePress(item, x, y)
55 }
56 for (var i = 0; i < iterations; ++i) {
57 fakeDateTime.currentTimeMs += timeStep
58 if (i === iterations - 1) {
59 // Avoid any rounding errors by making the last move be at precisely
60 // the point specified
61 mouseMove(item, toX, toY, iterations / speed)
62 } else {
63 mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed)
64 }
65 }
66 if (releaseMouse) {
67 fakeDateTime.currentTimeMs += timeStep
68 mouseRelease(item, toX, toY)
69 }
70 }
73 // Find an object with the given name in the children tree of "obj"
74 function findChild(obj, objectName) {
75 return findChildIn(obj, "children", objectName);
76 }
78 // Find an object with the given name in the children tree of "obj"
79 // Including invisible children like animations, timers etc.
80 // Note: you should use findChild if you're not sure you need this
81 // as this tree is much bigger and might contain stuff that goes
82 // away randomly.
83 function findInvisibleChild(obj, objectName) {
84 return findChildIn(obj, "data", objectName);
85 }
87 // Find a child in the named property
88 function findChildIn(obj, prop, objectName) {
89 var childs = new Array(0);
90 childs.push(obj)
91 while (childs.length > 0) {
92 if (childs[0].objectName == objectName) {
93 return childs[0]
94 }
95 for (var i in childs[0][prop]) {
96 childs.push(childs[0][prop][i])
97 }
98 childs.splice(0, 1);
99 }
100 return null;
101 }
103 // Type a full string instead of keyClick letter by letter
104 // TODO: this is not ugly, this is uber-ugly and does not support
105 // any special character. Remove the keyMap once keyClick(obj, char)
106 // has landed in upstream Qt.
107 function typeString(str) {
108 var keyMap = {
109 "a": Qt.Key_A,
110 "b": Qt.Key_B,
111 "c": Qt.Key_C,
112 "d": Qt.Key_D,
113 "e": Qt.Key_E,
114 "f": Qt.Key_F,
115 "g": Qt.Key_G,
116 "h": Qt.Key_H,
117 "i": Qt.Key_I,
118 "j": Qt.Key_J,
119 "k": Qt.Key_K,
120 "l": Qt.Key_L,
121 "m": Qt.Key_M,
122 "n": Qt.Key_N,
123 "o": Qt.Key_O,
124 "p": Qt.Key_P,
125 "q": Qt.Key_Q,
126 "r": Qt.Key_R,
127 "s": Qt.Key_S,
128 "t": Qt.Key_T,
129 "u": Qt.Key_U,
130 "v": Qt.Key_V,
131 "w": Qt.Key_W,
132 "x": Qt.Key_X,
133 "y": Qt.Key_Y,
134 "z": Qt.Key_Z,
135 "A": Qt.Key_A,
136 "B": Qt.Key_B,
137 "C": Qt.Key_C,
138 "D": Qt.Key_D,
139 "E": Qt.Key_E,
140 "F": Qt.Key_F,
141 "G": Qt.Key_G,
142 "H": Qt.Key_H,
143 "I": Qt.Key_I,
144 "J": Qt.Key_J,
145 "K": Qt.Key_K,
146 "L": Qt.Key_L,
147 "M": Qt.Key_M,
148 "N": Qt.Key_N,
149 "O": Qt.Key_O,
150 "P": Qt.Key_P,
151 "Q": Qt.Key_Q,
152 "R": Qt.Key_R,
153 "S": Qt.Key_S,
154 "T": Qt.Key_T,
155 "U": Qt.Key_U,
156 "V": Qt.Key_V,
157 "W": Qt.Key_W,
158 "X": Qt.Key_X,
159 "Y": Qt.Key_Y,
160 "Z": Qt.Key_Z,
161 "0": Qt.Key_0,
162 "1": Qt.Key_1,
163 "2": Qt.Key_2,
164 "3": Qt.Key_3,
165 "4": Qt.Key_4,
166 "5": Qt.Key_5,
167 "6": Qt.Key_6,
168 "7": Qt.Key_7,
169 "8": Qt.Key_8,
170 "9": Qt.Key_9,
171 " ": Qt.Key_Space,
172 }
173 for (var i = 0; i < str.length; i++) {
174 keyClick(keyMap[str[i]])
175 }
176 }
178 // Keeps executing a given parameter-less function until it returns the given
179 // expected result or the timemout is reached (in which case a test failure
180 // is generated)
181 function tryCompareFunction(func, expectedResult) {
182 var timeSpent = 0
183 var timeout = 5000
184 var success = false
185 var actualResult
186 while (timeSpent < timeout && !success) {
187 actualResult = func()
188 success = qtest_compareInternal(actualResult, expectedResult)
189 if (success === false) {
190 wait(50)
191 timeSpent += 50
192 }
193 }
195 var act = qtest_results.stringify(actualResult)
196 var exp = qtest_results.stringify(expectedResult)
197 if (!qtest_results.compare(success,
198 "function returned unexpected result",
199 act, exp,
200 util.callerFile(), util.callerLine())) {
201 throw new Error("QtQuickTest::fail")
202 }
203 }
205 // Keeps executing a given parameter-less function until it returns the given
206 // expected result or the timemout is reached (in which case a test failure
207 // is generated)
208 function tryCompareFunction2(func, expectedResultFunc) {
209 var timeSpent = 0
210 var timeout = 5000
211 var success = false
212 var actualResult
213 var expectedResult
214 while (timeSpent < timeout && !success) {
215 actualResult = func()
216 expectedResult = expectedResultFunc()
217 success = qtest_compareInternal(actualResult, expectedResult)
218 if (success === false) {
219 wait(50)
220 timeSpent += 50
221 }
222 }
224 var act = qtest_results.stringify(actualResult)
225 var exp = qtest_results.stringify(expectedResult)
226 if (!qtest_results.compare(success,
227 "function returned unexpected result",
228 act, exp,
229 util.callerFile(), util.callerLine())) {
230 throw new Error("QtQuickTest::fail")
231 }
232 }
234 function touchEvent() {
235 return UT.Util.touchEvent()
236 }
238 // speed is in pixels/second
239 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
240 // Make sure the item is rendered
241 waitForRendering(item);
243 // Default to true for beginTouch if not present
244 beginTouch = (beginTouch !== undefined) ? beginTouch : true
246 // Default to true for endTouch if not present
247 endTouch = (endTouch !== undefined) ? endTouch : true
249 // Set a default speed if not specified
250 speed = (speed !== undefined) ? speed : units.gu(10)
252 // Set a default iterations if not specified
253 var iterations = (iterations !== undefined) ? iterations : 5
255 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
256 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
258 var timeStep = totalTime / iterations
259 var diffX = (toX - x) / iterations
260 var diffY = (toY - y) / iterations
261 if (beginTouch) {
262 fakeDateTime.currentTimeMs += timeStep
264 var event = touchEvent()
265 event.press(0 /* touchId */, x, y)
266 event.commit()
267 }
268 for (var i = 0; i < iterations; ++i) {
269 fakeDateTime.currentTimeMs += timeStep
270 if (i === iterations - 1) {
271 // Avoid any rounding errors by making the last move be at precisely
272 // the point specified
273 wait(iterations / speed)
274 var event = touchEvent()
275 event.move(0 /* touchId */, toX, toY)
276 event.commit()
277 } else {
278 wait(iterations / speed)
279 var event = touchEvent()
280 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
281 event.commit()
282 }
283 }
284 if (endTouch) {
285 fakeDateTime.currentTimeMs += timeStep
286 var event = touchEvent()
287 event.release(0 /* touchId */, toX, toY)
288 event.commit()
289 }
290 }
292 function fetchRootItem(item) {
293 if (item.parent)
294 return fetchRootItem(item.parent)
295 else
296 return item
297 }
299 function touchPress(item, x, y) {
300 var root = fetchRootItem(item)
301 var rootPoint = item.mapToItem(root, x, y)
303 var event = touchEvent()
304 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
305 event.commit()
306 }
308 function touchRelease(item, x, y) {
309 var root = fetchRootItem(item)
310 var rootPoint = item.mapToItem(root, x, y)
312 var event = touchEvent()
313 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
314 event.commit()
315 }
317 function tap(item, x, y) {
318 var event = touchEvent()
319 event.press(0 /* touchId */, x, y)
320 event.commit()
322 event = touchEvent()
323 event.release(0 /* touchId */, x, y)
324 event.commit()
325 }
327 Component.onCompleted: {
328 var rootItem = parent;
329 while (rootItem.parent != undefined) {
330 rootItem = rootItem.parent;
331 }
332 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
333 }
335 /*
336 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
337 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
338 Thus to remove a variable that qmltests cannot really control, namely time, this
339 function removes all constraints from DirectionalDragAreas that are sensible to
340 elapsed time.
342 This effectively makes DirectionalDragAreas easier to fool.
343 */
344 function removeTimeConstraintsFromDirectionalDragAreas(item) {
346 // use duck-typing to identify a DirectionalDragArea
347 if (item.minSpeed != undefined
348 && item.maxSilenceTime != undefined
349 && item.compositionTime != undefined) {
350 item.minSpeed = 0;
351 item.maxSilenceTime = 60 * 60 * 1000;
352 item.compositionTime = 0;
353 } else {
354 for (var i in item.children) {
355 removeTimeConstraintsFromDirectionalDragAreas(item.children[i]);
356 }
357 }
358 }