on ed260eb help sign in
unity8/tests/utils/modules/Unity/Test/UnityTestCase.qml
334 lines 11 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 var childs = new Array(0);
76 childs.push(obj)
77 while (childs.length > 0) {
78 if (childs[0].objectName == objectName) {
79 return childs[0]
80 }
81 for (var i in childs[0].children) {
82 childs.push(childs[0].children[i])
83 }
84 childs.splice(0, 1);
85 }
86 return undefined;
87 }
89 // Find an object with the given name in the children tree of "obj"
90 // Including invisible children like animations, timers etc.
91 // Note: you should use findChild if you're not sure you need this
92 // as this tree is much bigger and might contain stuff that goes
93 // away randomly.
94 function findInvisibleChild(obj,objectName) {
95 var childs = new Array(0);
96 childs.push(obj)
97 while (childs.length > 0) {
98 if (childs[0].objectName == objectName) {
99 return childs[0]
100 }
101 for (var i in childs[0].data) {
102 childs.push(childs[0].data[i])
103 }
104 childs.splice(0, 1);
105 }
106 return undefined;
107 }
109 // Type a full string instead of keyClick letter by letter
110 // TODO: this is not ugly, this is uber-ugly and does not support
111 // any special character. Remove the keyMap once keyClick(obj, char)
112 // has landed in upstream Qt.
113 function typeString(str) {
114 var keyMap = {
115 "a": Qt.Key_A,
116 "b": Qt.Key_B,
117 "c": Qt.Key_C,
118 "d": Qt.Key_D,
119 "e": Qt.Key_E,
120 "f": Qt.Key_F,
121 "g": Qt.Key_G,
122 "h": Qt.Key_H,
123 "i": Qt.Key_I,
124 "j": Qt.Key_J,
125 "k": Qt.Key_K,
126 "l": Qt.Key_L,
127 "m": Qt.Key_M,
128 "n": Qt.Key_N,
129 "o": Qt.Key_O,
130 "p": Qt.Key_P,
131 "q": Qt.Key_Q,
132 "r": Qt.Key_R,
133 "s": Qt.Key_S,
134 "t": Qt.Key_T,
135 "u": Qt.Key_U,
136 "v": Qt.Key_V,
137 "w": Qt.Key_W,
138 "x": Qt.Key_X,
139 "y": Qt.Key_Y,
140 "z": Qt.Key_Z,
141 "A": Qt.Key_A,
142 "B": Qt.Key_B,
143 "C": Qt.Key_C,
144 "D": Qt.Key_D,
145 "E": Qt.Key_E,
146 "F": Qt.Key_F,
147 "G": Qt.Key_G,
148 "H": Qt.Key_H,
149 "I": Qt.Key_I,
150 "J": Qt.Key_J,
151 "K": Qt.Key_K,
152 "L": Qt.Key_L,
153 "M": Qt.Key_M,
154 "N": Qt.Key_N,
155 "O": Qt.Key_O,
156 "P": Qt.Key_P,
157 "Q": Qt.Key_Q,
158 "R": Qt.Key_R,
159 "S": Qt.Key_S,
160 "T": Qt.Key_T,
161 "U": Qt.Key_U,
162 "V": Qt.Key_V,
163 "W": Qt.Key_W,
164 "X": Qt.Key_X,
165 "Y": Qt.Key_Y,
166 "Z": Qt.Key_Z,
167 "0": Qt.Key_0,
168 "1": Qt.Key_1,
169 "2": Qt.Key_2,
170 "3": Qt.Key_3,
171 "4": Qt.Key_4,
172 "5": Qt.Key_5,
173 "6": Qt.Key_6,
174 "7": Qt.Key_7,
175 "8": Qt.Key_8,
176 "9": Qt.Key_9,
177 " ": Qt.Key_Space,
178 }
179 for (var i = 0; i < str.length; i++) {
180 keyClick(keyMap[str[i]])
181 }
182 }
184 // Keeps executing a given parameter-less function until it returns the given
185 // expected result or the timemout is reached (in which case a test failure
186 // is generated)
187 function tryCompareFunction(func, expectedResult) {
188 var timeSpent = 0
189 var timeout = 5000
190 var success = false
191 var actualResult
192 while (timeSpent < timeout && !success) {
193 actualResult = func()
194 success = qtest_compareInternal(actualResult, expectedResult)
195 if (success === false) {
196 wait(50)
197 timeSpent += 50
198 }
199 }
201 var act = qtest_results.stringify(actualResult)
202 var exp = qtest_results.stringify(expectedResult)
203 if (!qtest_results.compare(success,
204 "function returned unexpected result",
205 act, exp,
206 util.callerFile(), util.callerLine())) {
207 throw new Error("QtQuickTest::fail")
208 }
209 }
211 function touchEvent() {
212 return UT.Util.touchEvent()
213 }
215 // speed is in pixels/second
216 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
218 // Default to true for beginTouch if not present
219 beginTouch = (beginTouch !== undefined) ? beginTouch : true
221 // Default to true for endTouch if not present
222 endTouch = (endTouch !== undefined) ? endTouch : true
224 // Set a default speed if not specified
225 speed = (speed !== undefined) ? speed : units.gu(10)
227 // Set a default iterations if not specified
228 var iterations = (iterations !== undefined) ? iterations : 5
230 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
231 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
233 var timeStep = totalTime / iterations
234 var diffX = (toX - x) / iterations
235 var diffY = (toY - y) / iterations
236 if (beginTouch) {
237 fakeDateTime.currentTimeMs += timeStep
239 var event = touchEvent()
240 event.press(0 /* touchId */, x, y)
241 event.commit()
242 }
243 for (var i = 0; i < iterations; ++i) {
244 fakeDateTime.currentTimeMs += timeStep
245 if (i === iterations - 1) {
246 // Avoid any rounding errors by making the last move be at precisely
247 // the point specified
248 wait(iterations / speed)
249 var event = touchEvent()
250 event.move(0 /* touchId */, toX, toY)
251 event.commit()
252 } else {
253 wait(iterations / speed)
254 var event = touchEvent()
255 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
256 event.commit()
257 }
258 }
259 if (endTouch) {
260 fakeDateTime.currentTimeMs += timeStep
261 var event = touchEvent()
262 event.release(0 /* touchId */, toX, toY)
263 event.commit()
264 }
265 }
267 function fetchRootItem(item) {
268 if (item.parent)
269 return fetchRootItem(item.parent)
270 else
271 return item
272 }
274 function touchPress(item, x, y) {
275 var root = fetchRootItem(item)
276 var rootPoint = item.mapToItem(root, x, y)
278 var event = touchEvent()
279 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
280 event.commit()
281 }
283 function touchRelease(item, x, y) {
284 var root = fetchRootItem(item)
285 var rootPoint = item.mapToItem(root, x, y)
287 var event = touchEvent()
288 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
289 event.commit()
290 }
292 function tap(item, x, y) {
293 var event = touchEvent()
294 event.press(0 /* touchId */, x, y)
295 event.commit()
297 event = touchEvent()
298 event.release(0 /* touchId */, x, y)
299 event.commit()
300 }
302 Component.onCompleted: {
303 var rootItem = parent;
304 while (rootItem.parent != undefined) {
305 rootItem = rootItem.parent;
306 }
307 removeTimeConstraintsFromDirectionalDragAreas(rootItem);
308 }
310 /*
311 In qmltests, sequences of touch events are sent all at once, unlike in "real life".
312 Also qmltests might run really slowly, e.g. when run from inside virtual machines.
313 Thus to remove a variable that qmltests cannot really control, namely time, this
314 function removes all constraints from DirectionalDragAreas that are sensible to
315 elapsed time.
317 This effectively makes DirectionalDragAreas easier to fool.
318 */
319 function removeTimeConstraintsFromDirectionalDragAreas(item) {
321 // use duck-typing to identify a DirectionalDragArea
322 if (item.minSpeed != undefined
323 && item.maxSilenceTime != undefined
324 && item.compositionTime != undefined) {
325 item.minSpeed = 0;
326 item.maxSilenceTime = 60 * 60 * 1000;
327 item.compositionTime = 0;
328 } else {
329 for (var i in item.children) {
330 removeTimeConstraintsFromDirectionalDragAreas(item.children[i]);
331 }
332 }
333 }