on xenial help sign in
Better error message if the item does not exist

Instead of Cannot read property 'width' of null and a line pointing to UnityTestCase.qml, you get No item given and a line pointing to the offending code

commit4a93bc77c117c86df53a5b078a37113340ca0e13 authorAlbert Astals Cid <albert.astals@canonical.com> dateThu 31 Mar 2016, 17:48 (11 years ago) parentfbb130c
1 file changed+84−7
tests/utils/modules/Unity/Test/UnityTestCase.qml+84−7
tests/utils/modules/Unity/Test/UnityTestCase.qml+84−7
@@ -50,8 +50,11 @@ TestCase {
5050 this.getCurrentTimeMs = function() {return this.currentTimeMs}
5151 }
5252
53− // TODO This function can be removed altogether once we use Qt 5.5 which has the same feature
53+ // TODO This function can be removed altogether once we use Qt 5.7 which has the same feature
5454 function mouseClick(item, x, y, button, modifiers, delay) {
55+ if (!item)
56+ qtest_fail("no item given", 1);
57+
5558 if (button === undefined)
5659 button = Qt.LeftButton;
5760 if (modifiers === undefined)
@@ -66,8 +69,11 @@ TestCase {
6669 qtest_fail("window not shown", 2);
6770 }
6871
69− // TODO This function can be removed altogether once we use Qt 5.5 which has the same feature
72+ // TODO This function can be removed altogether once we use Qt 5.7 which has the same feature
7073 function mouseDoubleClick(item, x, y, button, modifiers, delay) {
74+ if (!item)
75+ qtest_fail("no item given", 1);
76+
7177 if (button === undefined)
7278 button = Qt.LeftButton;
7379 if (modifiers === undefined)
@@ -82,8 +88,11 @@ TestCase {
8288 qtest_fail("window not shown", 2)
8389 }
8490
85− // TODO This function can be removed altogether once we use Qt 5.5 which has the same feature
91+ // TODO This function can be removed altogether once we use Qt 5.7 which has the same feature
8692 function mousePress(item, x, y, button, modifiers, delay) {
93+ if (!item)
94+ qtest_fail("no item given", 1);
95+
8796 if (button === undefined)
8897 button = Qt.LeftButton;
8998 if (modifiers === undefined)
@@ -98,8 +107,11 @@ TestCase {
98107 qtest_fail("window not shown", 2)
99108 }
100109
101− // TODO This function can be removed altogether once we use Qt 5.5 which has the same feature
110+ // TODO This function can be removed altogether once we use Qt 5.7 which has the same feature
102111 function mouseRelease(item, x, y, button, modifiers, delay) {
112+ if (!item)
113+ qtest_fail("no item given", 1);
114+
103115 if (button === undefined)
104116 button = Qt.LeftButton;
105117 if (modifiers === undefined)
@@ -121,6 +133,9 @@ TestCase {
121133 // speed is in pixels/second
122134 function mouseFlick(item, x, y, toX, toY, pressMouse, releaseMouse,
123135 speed, iterations) {
136+ if (!item)
137+ qtest_fail("no item given", 1);
138+
124139 pressMouse = ((pressMouse != null) ? pressMouse : true); // Default to true for pressMouse if not present
125140 releaseMouse = ((releaseMouse != null) ? releaseMouse : true); // Default to true for releaseMouse if not present
126141
@@ -159,6 +174,9 @@ TestCase {
159174
160175 // Find an object with the given name in the children tree of "obj"
161176 function findChild(obj, objectName) {
177+ if (!obj)
178+ qtest_fail("no obj given", 1);
179+
162180 return findChildIn(obj, "children", objectName);
163181 }
164182
@@ -168,11 +186,17 @@ TestCase {
168186 // as this tree is much bigger and might contain stuff that goes
169187 // away randomly.
170188 function findInvisibleChild(obj, objectName) {
189+ if (!obj)
190+ qtest_fail("no obj given", 1);
191+
171192 return findChildIn(obj, "data", objectName);
172193 }
173194
174195 // Find a child in the named property
175196 function findChildIn(obj, prop, objectName) {
197+ if (!obj)
198+ qtest_fail("no obj given", 1);
199+
176200 var childs = new Array(0);
177201 childs.push(obj)
178202 while (childs.length > 0) {
@@ -188,6 +212,9 @@ TestCase {
188212 }
189213
190214 function findChildsByType(obj, typeName) {
215+ if (!obj)
216+ qtest_fail("no obj given", 1);
217+
191218 var res = new Array(0);
192219 for (var i in obj.children) {
193220 var c = obj.children[i];
@@ -235,6 +262,9 @@ TestCase {
235262 }
236263
237264 function flickToYEnd(item) {
265+ if (!item)
266+ qtest_fail("no item given", 1);
267+
238268 var i = 0;
239269 var x = item.width / 2;
240270 var y = item.height - units.gu(1);
@@ -254,6 +284,9 @@ TestCase {
254284
255285 // speed is in pixels/second
256286 function touchFlick(item, x, y, toX, toY, beginTouch, endTouch, speed, iterations) {
287+ if (!item)
288+ qtest_fail("no item given", 1);
289+
257290 // Make sure the item is rendered
258291 waitForRendering(item);
259292
@@ -313,10 +346,16 @@ TestCase {
313346 // perform a drag in the given direction until the given condition is true
314347 // The condition is a function to be evaluated after every step
315348 function touchDragUntil(item, startX, startY, stepX, stepY, condition) {
349+ if (!item)
350+ qtest_fail("no item given", 1);
351+
316352 multiTouchDragUntil([0], item, startX, startY, stepX, stepY, condition);
317353 }
318354
319355 function multiTouchDragUntil(touchIds, item, startX, startY, stepX, stepY, condition) {
356+ if (!item)
357+ qtest_fail("no item given", 1);
358+
320359 var root = fetchRootItem(item);
321360 var pos = item.mapToItem(root, startX, startY);
322361
@@ -361,9 +400,17 @@ TestCase {
361400 event.commit()
362401 }
363402
364− function touchMove(item, tox, toy) { multiTouchMove(0, item, tox, toy); }
403+ function touchMove(item, tox, toy) {
404+ if (!item)
405+ qtest_fail("no item given", 1);
406+
407+ multiTouchMove(0, item, tox, toy);
408+ }
365409
366410 function multiTouchMove(touchId, item, tox, toy) {
411+ if (!item)
412+ qtest_fail("no item given", 1);
413+
367414 if (typeof touchId !== "number") touchId = 0;
368415 var root = fetchRootItem(item)
369416 var rootPoint = item.mapToItem(root, tox, toy)
@@ -374,6 +421,9 @@ TestCase {
374421 }
375422
376423 function touchPinch(item, x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End) {
424+ if (!item)
425+ qtest_fail("no item given", 1);
426+
377427 // Make sure the item is rendered
378428 waitForRendering(item);
379429
@@ -400,13 +450,21 @@ TestCase {
400450 }
401451
402452 function fetchRootItem(item) {
453+ if (!item)
454+ qtest_fail("no item given", 1);
455+
403456 if (item.parent)
404457 return fetchRootItem(item.parent)
405458 else
406459 return item
407460 }
408461
409− function touchPress(item, x, y) { multiTouchPress(0, item, x, y, []); }
462+ function touchPress(item, x, y) {
463+ if (!item)
464+ qtest_fail("no item given", 1);
465+
466+ multiTouchPress(0, item, x, y, []);
467+ }
410468
411469 /*! \brief Release a touch point
412470
@@ -417,6 +475,9 @@ TestCase {
417475 \param stationaryPoints An array of touchIds which are "already touched"
418476 */
419477 function multiTouchPress(touchId, item, x, y, stationaryPoints) {
478+ if (!item)
479+ qtest_fail("no item given", 1);
480+
420481 if (typeof touchId !== "number") touchId = 0;
421482 if (typeof x !== "number") x = item.width / 2;
422483 if (typeof y !== "number") y = item.height / 2;
@@ -432,7 +493,12 @@ TestCase {
432493 event.commit()
433494 }
434495
435− function touchRelease(item, x, y) { multiTouchRelease(0, item, x, y, []); }
496+ function touchRelease(item, x, y) {
497+ if (!item)
498+ qtest_fail("no item given", 1);
499+
500+ multiTouchRelease(0, item, x, y, []);
501+ }
436502
437503 /*! \brief Release a touch point
438504
@@ -443,6 +509,9 @@ TestCase {
443509 \param stationaryPoints An array of touchIds which are "still touched"
444510 */
445511 function multiTouchRelease(touchId, item, x, y, stationaryPoints) {
512+ if (!item)
513+ qtest_fail("no item given", 1);
514+
446515 if (typeof touchId !== "number") touchId = 0;
447516 if (typeof x !== "number") x = item.width / 2;
448517 if (typeof y !== "number") y = item.height / 2;
@@ -465,10 +534,16 @@ TestCase {
465534 \param y The y coordinate of the tap, defaults to vertical center
466535 */
467536 function tap(item, x, y) {
537+ if (!item)
538+ qtest_fail("no item given", 1);
539+
468540 multiTouchTap([0], item, x, y);
469541 }
470542
471543 function multiTouchTap(touchIds, item, x, y) {
544+ if (!item)
545+ qtest_fail("no item given", 1);
546+
472547 if (typeof touchIds !== "object") touchIds = [0];
473548 if (typeof x !== "number") x = item.width / 2;
474549 if (typeof y !== "number") y = item.height / 2;
@@ -508,6 +583,8 @@ TestCase {
508583 This effectively makes DirectionalDragAreas easier to fool.
509584 */
510585 function removeTimeConstraintsFromDirectionalDragAreas(item) {
586+ if (!item)
587+ qtest_fail("no item given", 1);
511588
512589 // use duck-typing to identify a DirectionalDragArea
513590 if (item.removeTimeConstraints != undefined) {