on acff48e help sign in
unity8/tests/utils/modules/Unity/Test/UnityTestCase.qml
281 lines 9.4 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 QtTest 1.0
18import Ubuntu.Components 0.1
19import Unity.Test 0.1 as UT
21TestCase {
22 TestUtil {id:util}
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}
28 }
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,
35 speed, iterations) {
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 // set a default iterations if not specified
43 iterations = (iterations !== undefined) ? iterations : 5
45 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
46 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
48 var timeStep = totalTime / iterations
49 var diffX = (toX - x) / iterations
50 var diffY = (toY - y) / iterations
51 if (pressMouse) {
52 fakeDateTime.currentTimeMs += timeStep
53 mousePress(item, x, y)
54 }
55 for (var i = 0; i < iterations; ++i) {
56 fakeDateTime.currentTimeMs += timeStep
57 if (i === iterations - 1) {
58 // Avoid any rounding errors by making the last move be at precisely
59 // the point specified
60 mouseMove(item, toX, toY, iterations / speed)
61 } else {
62 mouseMove(item, x + (i + 1) * diffX, y + (i + 1) * diffY, iterations / speed)
63 }
64 }
65 if (releaseMouse) {
66 fakeDateTime.currentTimeMs += timeStep
67 mouseRelease(item, toX, toY)
68 }
69 }
72 // Find an object with the given name recursively, starting
73 // at the given object "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].data) {
82 childs.push(childs[0].data[i])
83 }
84 childs.splice(0, 1);
85 }
86 return undefined;
87 }
89 // Type a full string instead of keyClick letter by letter
90 // TODO: this is not ugly, this is uber-ugly and does not support
91 // any special character. Remove the keyMap once keyClick(obj, char)
92 // has landed in upstream Qt.
93 function typeString(str) {
94 var keyMap = {
95 "a": Qt.Key_A,
96 "b": Qt.Key_B,
97 "c": Qt.Key_C,
98 "d": Qt.Key_D,
99 "e": Qt.Key_E,
100 "f": Qt.Key_F,
101 "g": Qt.Key_G,
102 "h": Qt.Key_H,
103 "i": Qt.Key_I,
104 "j": Qt.Key_J,
105 "k": Qt.Key_K,
106 "l": Qt.Key_L,
107 "m": Qt.Key_M,
108 "n": Qt.Key_N,
109 "o": Qt.Key_O,
110 "p": Qt.Key_P,
111 "q": Qt.Key_Q,
112 "r": Qt.Key_R,
113 "s": Qt.Key_S,
114 "t": Qt.Key_T,
115 "u": Qt.Key_U,
116 "v": Qt.Key_V,
117 "w": Qt.Key_W,
118 "x": Qt.Key_X,
119 "y": Qt.Key_Y,
120 "z": Qt.Key_Z,
121 "A": Qt.Key_A,
122 "B": Qt.Key_B,
123 "C": Qt.Key_C,
124 "D": Qt.Key_D,
125 "E": Qt.Key_E,
126 "F": Qt.Key_F,
127 "G": Qt.Key_G,
128 "H": Qt.Key_H,
129 "I": Qt.Key_I,
130 "J": Qt.Key_J,
131 "K": Qt.Key_K,
132 "L": Qt.Key_L,
133 "M": Qt.Key_M,
134 "N": Qt.Key_N,
135 "O": Qt.Key_O,
136 "P": Qt.Key_P,
137 "Q": Qt.Key_Q,
138 "R": Qt.Key_R,
139 "S": Qt.Key_S,
140 "T": Qt.Key_T,
141 "U": Qt.Key_U,
142 "V": Qt.Key_V,
143 "W": Qt.Key_W,
144 "X": Qt.Key_X,
145 "Y": Qt.Key_Y,
146 "Z": Qt.Key_Z,
147 "0": Qt.Key_0,
148 "1": Qt.Key_1,
149 "2": Qt.Key_2,
150 "3": Qt.Key_3,
151 "4": Qt.Key_4,
152 "5": Qt.Key_5,
153 "6": Qt.Key_6,
154 "7": Qt.Key_7,
155 "8": Qt.Key_8,
156 "9": Qt.Key_9,
157 " ": Qt.Key_Space,
158 }
159 for (var i = 0; i < str.length; i++) {
160 keyClick(keyMap[str[i]])
161 }
162 }
164 // Keeps executing a given parameter-less function until it returns the given
165 // expected result or the timemout is reached (in which case a test failure
166 // is generated)
167 function tryCompareFunction(func, expectedResult) {
168 var timeSpent = 0
169 var timeout = 5000
170 var success = false
171 var actualResult
172 while (timeSpent < timeout && !success) {
173 actualResult = func()
174 success = qtest_compareInternal(actualResult, expectedResult)
175 if (success === false) {
176 wait(50)
177 timeSpent += 50
178 }
179 }
181 var act = qtest_results.stringify(actualResult)
182 var exp = qtest_results.stringify(expectedResult)
183 if (!qtest_results.compare(success,
184 "function returned unexpected result",
185 act, exp,
186 util.callerFile(), util.callerLine())) {
187 throw new Error("QtQuickTest::fail")
188 }
189 }
191 function touchEvent() {
192 return UT.Util.touchEvent()
193 }
195 // speed is in pixels/second
196 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
198 // Default to true for beginTouch if not present
199 beginTouch = (beginTouch !== undefined) ? beginTouch : true
201 // Default to true for endTouch if not present
202 endTouch = (endTouch !== undefined) ? endTouch : true
204 // Set a default speed if not specified
205 speed = (speed !== undefined) ? speed : units.gu(10)
207 // Set a default iterations if not specified
208 var iterations = (iterations !== undefined) ? iterations : 5
210 var distance = Math.sqrt(Math.pow(toX - x, 2) + Math.pow(toY - y, 2))
211 var totalTime = (distance / speed) * 1000 /* converting speed to pixels/ms */
213 var timeStep = totalTime / iterations
214 var diffX = (toX - x) / iterations
215 var diffY = (toY - y) / iterations
216 if (beginTouch) {
217 fakeDateTime.currentTimeMs += timeStep
219 var event = touchEvent()
220 event.press(0 /* touchId */, x, y)
221 event.commit()
222 }
223 for (var i = 0; i < iterations; ++i) {
224 fakeDateTime.currentTimeMs += timeStep
225 if (i === iterations - 1) {
226 // Avoid any rounding errors by making the last move be at precisely
227 // the point specified
228 wait(iterations / speed)
229 var event = touchEvent()
230 event.move(0 /* touchId */, toX, toY)
231 event.commit()
232 } else {
233 wait(iterations / speed)
234 var event = touchEvent()
235 event.move(0 /* touchId */, x + (i + 1) * diffX, y + (i + 1) * diffY)
236 event.commit()
237 }
238 }
239 if (endTouch) {
240 fakeDateTime.currentTimeMs += timeStep
241 var event = touchEvent()
242 event.release(0 /* touchId */, toX, toY)
243 event.commit()
244 }
245 }
247 function fetchRootItem(item) {
248 if (item.parent)
249 return fetchRootItem(item.parent)
250 else
251 return item
252 }
254 function touchPress(item, x, y) {
255 var root = fetchRootItem(item)
256 var rootPoint = item.mapToItem(root, x, y)
258 var event = touchEvent()
259 event.press(0 /* touchId */, rootPoint.x, rootPoint.y)
260 event.commit()
261 }
263 function touchRelease(item, x, y) {
264 var root = fetchRootItem(item)
265 var rootPoint = item.mapToItem(root, x, y)
267 var event = touchEvent()
268 event.release(0 /* touchId */, rootPoint.x, rootPoint.y)
269 event.commit()
270 }
272 function tap(item, x, y) {
273 var event = touchEvent()
274 event.press(0 /* touchId */, x, y)
275 event.commit()
277 event = touchEvent()
278 event.release(0 /* touchId */, x, y)
279 event.commit()
280 }