From ba804f5d04c61e7b39930a69adcbdd6201f20b23 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 10 Nov 2016 10:42:43 +0100 Subject: [PATCH] feat(Common/InvertedMouseArea): check if already exists a `MouseArea` at children root --- tests/ui/modules/Common/InvertedMouseArea.qml | 16 +++++++- tests/ui/modules/Linphone/Chat/Message.qml | 1 - tests/ui/scripts/Utils/utils.js | 39 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/tests/ui/modules/Common/InvertedMouseArea.qml b/tests/ui/modules/Common/InvertedMouseArea.qml index 60db8e6e6..5bf92098b 100644 --- a/tests/ui/modules/Common/InvertedMouseArea.qml +++ b/tests/ui/modules/Common/InvertedMouseArea.qml @@ -11,18 +11,30 @@ Item { id: item property bool _mouseAlwaysOutside - property var _mouseArea + property var _mouseArea: null // When emitted, returns a function to test if the click // is on a specific item. It takes only a item parameter. signal pressed (var pointIsInItem) function _createMouseArea () { + var parent = Utils.getTopParent(item, true) + var mouseArea = Utils.find(parent.children, function (element) { + return Utils.qmlTypeof(element, 'QQuickMouseArea') + }) + + Utils.assert( + _mouseArea === mouseArea, + 'It already exists a different `MouseArea` at window root. (' + + '`local mouse area`=' + _mouseArea + ', `root mouse area`=' + + mouseArea + ')' + ) + if (_mouseArea == null) { _mouseArea = builder.createObject() } - _mouseArea.parent = Utils.getTopParent(item, true) + _mouseArea.parent = parent _mouseAlwaysOutside = _mouseArea.parent !== Utils.getTopParent(item) } diff --git a/tests/ui/modules/Linphone/Chat/Message.qml b/tests/ui/modules/Linphone/Chat/Message.qml index 76f97e9e7..50e5868b7 100644 --- a/tests/ui/modules/Linphone/Chat/Message.qml +++ b/tests/ui/modules/Linphone/Chat/Message.qml @@ -22,7 +22,6 @@ Item { var children = root.children // Can be the `invertedMouseArea` of other message. - // Or another? It's a problem in this case... var mouseArea = children[children.length - 1] if (Utils.qmlTypeof(mouseArea, 'QQuickMouseArea')) { diff --git a/tests/ui/scripts/Utils/utils.js b/tests/ui/scripts/Utils/utils.js index 70928e3c6..59648354a 100644 --- a/tests/ui/scripts/Utils/utils.js +++ b/tests/ui/scripts/Utils/utils.js @@ -2,6 +2,8 @@ // Contains many common helpers. // =================================================================== +.pragma library + .import 'uri-tools.js' as UriTools // =================================================================== @@ -354,3 +356,40 @@ function includes (obj, value, startIndex) { return false } + +// ------------------------------------------------------------------- + +function _indexFinder (array, cb, context) { + var length = array.length + + for (var i = 0; i < length; i++) { + if (cb(array[index], index, array)) { + return i + } + } + + return -1 +} + +function _keyFinder (obj, cb, context) { + var keys = Object.keys(obj) + var length = keys.length + + for (var i = 0; i < length; i++) { + var key = keys[i] + if (cb(obj[key], key, obj)) { + return key + } + } +} + +// Get the first matching value in a array or object. +// The matching value is obtained if `cb` returns true. +function find (obj, cb, context) { + cb = _computeOptimizedCb(cb, context) + + var finder = isArray(obj) ? _indexFinder : _keyFinder + var key = finder(obj, cb, context) + + return key != null && key !== -1 ? obj[key] : null +}