feat(Utils): better style

This commit is contained in:
Ronan Abhamon 2016-11-10 13:55:09 +01:00
parent d9b4dff22d
commit 64e5a70b36
2 changed files with 78 additions and 72 deletions

View file

@ -153,23 +153,6 @@ function openWindow (window, parent, options) {
// -------------------------------------------------------------------
// A copy of `Window.setTimeout` from js.
// delay is in milliseconds.
function setTimeout (parent, delay, cb) {
var timer = new (function (parent) {
return Qt.createQmlObject('import QtQuick 2.7; Timer { }', parent)
})(parent)
timer.interval = delay
timer.repeat = false
timer.triggered.connect(cb)
timer.start()
return timer
}
// -------------------------------------------------------------------
// Test if a point is in a item.
//
// `source` is the item that generated the point.
@ -203,6 +186,23 @@ function qmlTypeof (object, className) {
)
}
// -------------------------------------------------------------------
// A copy of `Window.setTimeout` from js.
// delay is in milliseconds.
function setTimeout (parent, delay, cb) {
var timer = new (function (parent) {
return Qt.createQmlObject('import QtQuick 2.7; Timer { }', parent)
})(parent)
timer.interval = delay
timer.repeat = false
timer.triggered.connect(cb)
timer.start()
return timer
}
// ===================================================================
// GENERIC.
// ===================================================================

View file

@ -10,6 +10,37 @@ import './utils.js' as Utils
TestCase {
id: testCase
// =================================================================
// QML helpers.
// =================================================================
function test_clearTimeout_data () {
return [
{ time: 0 },
{ time: 100 }
]
}
function test_clearTimeout (data) {
var failed = false
var timeout = Utils.setTimeout(testCase, data.time, function () {
failed = true
})
if (failed) {
fail('`setTimeout` callback was called before `wait`.')
}
Utils.clearTimeout(timeout)
wait(100)
if (failed) {
fail('`setTimeout` callback was called after `wait`.')
}
}
// -----------------------------------------------------------------
// Test only if a confirm dialog can be opened.
// The other tests are launched by `ConfirmDialog.spec.qml`.
function test_openConfirmDialog () {
@ -33,6 +64,33 @@ TestCase {
// -----------------------------------------------------------------
function test_qmlTypeof_data () {
return [
{
component: 'import QtQuick 2.7; ListModel {}',
result: true,
type: 'QQmlListModel'
}, {
component: 'import QtQuick 2.7; ListView {}',
result: true,
type: 'QQuickListView'
}, {
component: 'import QtQuick 2.7; MouseArea {}',
result: true,
type: 'QQuickMouseArea'
}
]
}
function test_qmlTypeof (data) {
var object = Qt.createQmlObject(data.component, testCase)
verify(object)
compare(Utils.qmlTypeof(object, data.type), data.result)
}
// -----------------------------------------------------------------
function test_setTimeoutWithoutParent () {
try {
Utils.setTimeout(null, 0, function () {
@ -67,61 +125,9 @@ TestCase {
}
}
// -----------------------------------------------------------------
function test_clearTimeout_data () {
return [
{ time: 0 },
{ time: 100 }
]
}
function test_clearTimeout (data) {
var failed = false
var timeout = Utils.setTimeout(testCase, data.time, function () {
failed = true
})
if (failed) {
fail('`setTimeout` callback was called before `wait`.')
}
Utils.clearTimeout(timeout)
wait(100)
if (failed) {
fail('`setTimeout` callback was called after `wait`.')
}
}
// -----------------------------------------------------------------
function test_qmlTypeof_data () {
return [
{
component: 'import QtQuick 2.7; ListModel {}',
result: true,
type: 'QQmlListModel'
}, {
component: 'import QtQuick 2.7; ListView {}',
result: true,
type: 'QQuickListView'
}, {
component: 'import QtQuick 2.7; MouseArea {}',
result: true,
type: 'QQuickMouseArea'
}
]
}
function test_qmlTypeof (data) {
var object = Qt.createQmlObject(data.component, testCase)
verify(object)
compare(Utils.qmlTypeof(object, data.type), data.result)
}
// -----------------------------------------------------------------
// =================================================================
// GENERIC.
// =================================================================
function test_genRandomNumber_data () {
return [