diff --git a/tests/ui/scripts/Utils/utils.js b/tests/ui/scripts/Utils/utils.js index 92714738c..d6e8c49d2 100644 --- a/tests/ui/scripts/Utils/utils.js +++ b/tests/ui/scripts/Utils/utils.js @@ -50,8 +50,6 @@ function openWindow (window, parent, options) { object.show() } -// ------------------------------------------------------------------- - // Display a simple ConfirmDialog component. // Wrap the openWindow function. function openConfirmDialog (parent, options) { @@ -71,6 +69,15 @@ function openConfirmDialog (parent, options) { // ------------------------------------------------------------------- +function _computeOptimizedCb (func, context) { + return (context != null) + ? (function () { + return func.apply(context, arguments) + }) : func +} + +// ------------------------------------------------------------------- + function snakeToCamel (s) { return s.replace(/(\_\w)/g, function (matches) { return matches[1].toUpperCase() @@ -97,7 +104,21 @@ function setTimeout (delay, cb) { } function clearTimeout (timer) { - timer.destroy() // Unnecessary call: `timer.stop()` + timer.stop() // NECESSARY. + timer.destroy() +} + +// ------------------------------------------------------------------- + +function times (n, cb, context) { + var arr = Array(Math.max(0, n)) + cb = _computeOptimizedCb(cb, context, 1) + + for (var i = 0; i < n; i++) { + arr[i] = cb(i) + } + + return arr } // ------------------------------------------------------------------- diff --git a/tests/ui/scripts/Utils/utils.spec.qml b/tests/ui/scripts/Utils/utils.spec.qml index 609540541..243f03ca8 100644 --- a/tests/ui/scripts/Utils/utils.spec.qml +++ b/tests/ui/scripts/Utils/utils.spec.qml @@ -25,6 +25,8 @@ TestCase { compare(Utils.snakeToCamel(data.input), data.output) } + // ----------------------------------------------------------------- + function test_setTimeoutWithoutParent () { try { Utils.setTimeout(0, function () { @@ -58,4 +60,54 @@ TestCase { fail('`setTimeout` failed because callback it was not called in due course') } } + + // ----------------------------------------------------------------- + + function test_clearTimeout_data () { + return [ + { time: 0 }, + { time: 100 } + ] + } + + function test_clearTimeout (data) { + var failed = false + var timeout = Utils.setTimeout.call(testCase, data.time, function () { + failed = true + }) + + // Simulate time + Utils.times(500000, function (i) { + // Nothing. + }) + + if (failed) { + fail('`setTimeout` callback was called') + } + + Utils.clearTimeout(timeout) + wait(100) + + if (failed) { + fail('`setTimeout` callback was called') + } + } + + // ----------------------------------------------------------------- + + function test_isString_data () { + return [ + { input: 'foo', output: true }, + { input: Object('bar'), output: true }, + { input: [ 0 ], output: false }, + { input: /baz/, output: false }, + { input: new Error, output: false }, + { input: true, output: false }, + { input: 42, output: false } + ] + } + + function test_isString (data) { + compare(Utils.isString(data.input), data.output) + } }