diff options
Diffstat (limited to 'src/imports')
-rw-r--r-- | src/imports/imports.pro | 1 | ||||
-rw-r--r-- | src/imports/testlib/SignalSpy.qml | 109 | ||||
-rw-r--r-- | src/imports/testlib/TestCase.qml | 692 | ||||
-rw-r--r-- | src/imports/testlib/main.cpp | 133 | ||||
-rw-r--r-- | src/imports/testlib/qmldir | 3 | ||||
-rw-r--r-- | src/imports/testlib/signalspy.h | 83 | ||||
-rw-r--r-- | src/imports/testlib/signalspy.qdoc | 146 | ||||
-rw-r--r-- | src/imports/testlib/testcase.h | 94 | ||||
-rw-r--r-- | src/imports/testlib/testcase.qdoc | 597 | ||||
-rw-r--r-- | src/imports/testlib/testlib.pro | 29 | ||||
-rw-r--r-- | src/imports/testlib/testlogger.js | 98 |
11 files changed, 1985 insertions, 0 deletions
diff --git a/src/imports/imports.pro b/src/imports/imports.pro index 6704b3ae3c..e81c4bf5cf 100644 --- a/src/imports/imports.pro +++ b/src/imports/imports.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += folderlistmodel particles gestures inputcontext etcprovider +contains(QT_CONFIG, qmltest): SUBDIRS += testlib diff --git a/src/imports/testlib/SignalSpy.qml b/src/imports/testlib/SignalSpy.qml new file mode 100644 index 0000000000..676412998b --- /dev/null +++ b/src/imports/testlib/SignalSpy.qml @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Item { + id: spy + visible: false + + // Public API. + + property variant target: null + property string signalName: "" + property int count: 0 + + function clear() { + count = 0 + qtest_expectedCount = 0 + } + + function wait(timeout) { + if (timeout === undefined) + timeout = 5000 + var expected = ++qtest_expectedCount + var i = 0 + while (i < timeout && count < expected) { + qtest_results.wait(50) + i += 50 + } + var success = (count >= expected) + if (!qtest_results.verify(success, "wait for signal " + signalName, Qt.qtest_caller_file(), Qt.qtest_caller_line())) + throw new Error("QtQuickTest::fail") + } + + // Internal implementation detail follows. + + TestResult { id: qtest_results } + + onTargetChanged: { + qtest_update() + } + onSignalNameChanged: { + qtest_update() + } + + property variant qtest_prevTarget: null + property string qtest_prevSignalName: "" + property int qtest_expectedCount: 0 + + function qtest_update() { + if (qtest_prevTarget != null) { + qtest_prevTarget[qtest_prevSignalName].disconnect(spy, "qtest_activated") + qtest_prevTarget = null + qtest_prevSignalName = "" + } + if (target != null && signalName != "") { + var func = target[signalName] + if (func === undefined) { + console.log("Signal '" + signalName + "' not found") + } else { + qtest_prevTarget = target + qtest_prevSignalName = signalName + func.connect(spy.qtest_activated) + } + } + } + + function qtest_activated() { + ++count + } +} diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml new file mode 100644 index 0000000000..3659c6029d --- /dev/null +++ b/src/imports/testlib/TestCase.qml @@ -0,0 +1,692 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtTest 1.0 +import "testlogger.js" as TestLogger + +Item { + id: testCase + visible: false + + // Name of the test case to prefix the function name in messages. + property string name + + // Set to true to start the test running. + property bool when: true + + // Set to true once the test has completed. + property bool completed: false + + // Set to true when the test is running but not yet complete. + property bool running: false + + // Set to true if the test doesn't have to run (because some + // other test failed which this one depends on). + property bool optional: false + + // Property that is set to true when the main window is shown. + // We need to set the property value in an odd way to handle + // both qmlviewer and the QtQuickTest module test wrapper. + property bool windowShown: Qt.qtest_wrapper ? qtest.windowShown : false + + // Internal private state. Identifiers prefixed with qtest are reserved. + property bool qtest_prevWhen: true + property int qtest_testId: -1 + property variant qtest_testCaseResult + property variant qtest_results: qtest_results_normal + TestResult { id: qtest_results_normal } + property variant qtest_events: qtest_events_normal + TestEvent { id: qtest_events_normal } + + function fail(msg) { + if (msg === undefined) + msg = ""; + qtest_results.fail(msg, Qt.qtest_caller_file(), Qt.qtest_caller_line()) + throw new Error("QtQuickTest::fail") + } + + function qtest_fail(msg, frame) { + if (msg === undefined) + msg = ""; + qtest_results.fail(msg, Qt.qtest_caller_file(frame), Qt.qtest_caller_line(frame)) + throw new Error("QtQuickTest::fail") + } + + function verify(cond, msg) { + if (msg === undefined) + msg = ""; + if (!qtest_results.verify(cond, msg, Qt.qtest_caller_file(), Qt.qtest_caller_line())) + throw new Error("QtQuickTest::fail") + } + + // Determine what is o. + // Discussions and reference: http://philrathe.com/articles/equiv + // Test suites: http://philrathe.com/tests/equiv + // Author: Philippe Rathé <prathe@gmail.com> + function qtest_typeof(o) { + if (typeof o === "undefined") { + return "undefined"; + + // consider: typeof null === object + } else if (o === null) { + return "null"; + + } else if (o.constructor === String) { + return "string"; + + } else if (o.constructor === Boolean) { + return "boolean"; + + } else if (o.constructor === Number) { + + if (isNaN(o)) { + return "nan"; + } else { + return "number"; + } + // consider: typeof [] === object + } else if (o instanceof Array) { + return "array"; + + // consider: typeof new Date() === object + } else if (o instanceof Date) { + return "date"; + + // consider: /./ instanceof Object; + // /./ instanceof RegExp; + // typeof /./ === "function"; // => false in IE and Opera, + // true in FF and Safari + } else if (o instanceof RegExp) { + return "regexp"; + + } else if (typeof o === "object") { + if ("mapFromItem" in o && "mapToItem" in o) { + return "declarativeitem"; // @todo improve detection of declarative items + } else if ("x" in o && "y" in o && "z" in o) { + return "vector3d"; // Qt3D vector + } + return "object"; + } else if (o instanceof Function) { + return "function"; + } else { + return undefined; + } + } + + // Test for equality + // Large parts contain sources from QUnit or http://philrathe.com + // Discussions and reference: http://philrathe.com/articles/equiv + // Test suites: http://philrathe.com/tests/equiv + // Author: Philippe Rathé <prathe@gmail.com> + function qtest_compareInternal(act, exp) { + var success = false; + + if (act === exp) { + success = true; // catch the most you can + } else if (act === null || exp === null || typeof act === "undefined" || typeof exp === "undefined") { + success = false; // don't lose time with error prone cases + } else { + var typeExp = qtest_typeof(exp), typeAct = qtest_typeof(act) + + if (typeExp !== typeAct) { + // allow object vs string comparison (e.g. for colors) + // else break on different types + if ((typeExp === "string" && typeAct === "object") || (typeExp === "object" && typeAct === "string")) { + success = (act == exp) + } + } else if (typeExp === "string" || typeExp === "boolean" || typeExp === "number" || + typeExp === "null" || typeExp === "undefined") { + if (exp instanceof act.constructor || act instanceof exp.constructor) { + // to catch short annotaion VS 'new' annotation of act declaration + // e.g. var i = 1; + // var j = new Number(1); + success = (act == exp) + } else { + success = (act === exp) + } + } else if (typeExp === "nan") { + success = isNaN(act); + } else if (typeExp == "number") { + // Use act fuzzy compare if the two values are floats + if (Math.abs(act - exp) <= 0.00001) { + success = true + } + } else if (typeExp === "array") { + success = qtest_compareInternalArrays(act, exp) + } else if (typeExp === "object") { + success = qtest_compareInternalObjects(act, exp) + } else if (typeExp === "declarativeitem") { + success = qtest_compareInternalObjects(act, exp) // @todo improve comparison of declarative items + } else if (typeExp === "vector3d") { + success = (Math.abs(act.x - exp.x) <= 0.00001 && + Math.abs(act.y - exp.y) <= 0.00001 && + Math.abs(act.z - exp.z) <= 0.00001) + } else if (typeExp === "date") { + success = (act.valueOf() === exp.valueOf()) + } else if (typeExp === "regexp") { + success = (act.source === exp.source && // the regex itself + act.global === exp.global && // and its modifers (gmi) ... + act.ignoreCase === exp.ignoreCase && + act.multiline === exp.multiline) + } + } + return success + } + + function qtest_compareInternalObjects(act, exp) { + var i; + var eq = true; // unless we can proove it + var aProperties = [], bProperties = []; // collection of strings + + // comparing constructors is more strict than using instanceof + if (act.constructor !== exp.constructor) { + return false; + } + + for (i in act) { // be strict: don't ensures hasOwnProperty and go deep + aProperties.push(i); // collect act's properties + + if (!qtest_compareInternal(act[i], exp[i])) { + eq = false; + break; + } + } + + for (i in exp) { + bProperties.push(i); // collect exp's properties + } + + // Ensures identical properties name + return eq && qtest_compareInternal(aProperties.sort(), bProperties.sort()); + + } + + function qtest_compareInternalArrays(actual, expected) { + if (actual.length != expected.length) { + return false + } + + for (var i = 0, len = actual.length; i < len; i++) { + if (!qtest_compareInternal(actual[i], expected[i])) { + return false + } + } + + return true + } + + function qtest_formatValue(value) { + if (typeof value == "object") { + if ("x" in value && "y" in value && "z" in value) { + return "Qt.vector3d(" + value.x + ", " + + value.y + ", " + value.z + ")" + } + try { + return JSON.stringify(value) + } catch (ex) { + // stringify might fail (e.g. due to circular references) + } + } + return value + } + + function compare(actual, expected, msg) { + var act = qtest_formatValue(actual) + var exp = qtest_formatValue(expected) + var success = qtest_compareInternal(actual, expected) + if (msg === undefined) { + if (success) + msg = "COMPARE()" + else + msg = "Compared values are not the same" + } + if (!qtest_results.compare(success, msg, act, exp, Qt.qtest_caller_file(), Qt.qtest_caller_line())) + throw new Error("QtQuickTest::fail") + } + + function tryCompare(obj, prop, value, timeout) { + if (!timeout) + timeout = 5000 + if (!qtest_compareInternal(obj[prop], value)) + wait(0) + var i = 0 + while (i < timeout && !qtest_compareInternal(obj[prop], value)) { + wait(50) + i += 50 + } + var actual = obj[prop] + var act = qtest_formatValue(actual) + var exp = qtest_formatValue(value) + var success = qtest_compareInternal(actual, value) + if (!qtest_results.compare(success, "property " + prop, act, exp, Qt.qtest_caller_file(), Qt.qtest_caller_line())) + throw new Error("QtQuickTest::fail") + } + + function skip(msg) { + if (msg === undefined) + msg = "" + qtest_results.skipSingle(msg, Qt.qtest_caller_file(), Qt.qtest_caller_line()) + throw new Error("QtQuickTest::skip") + } + + function skipAll(msg) { + if (msg === undefined) + msg = "" + qtest_results.skipAll(msg, Qt.qtest_caller_file(), Qt.qtest_caller_line()) + throw new Error("QtQuickTest::skip") + } + + function expectFail(tag, msg) { + if (tag === undefined) { + warn("tag argument missing from expectFail()") + tag = "" + } + if (msg === undefined) { + warn("message argument missing from expectFail()") + msg = "" + } + if (!qtest_results.expectFail(tag, msg, Qt.qtest_caller_file(), Qt.qtest_caller_line())) + throw new Error("QtQuickTest::expectFail") + } + + function expectFailContinue(tag, msg) { + if (tag === undefined) { + warn("tag argument missing from expectFailContinue()") + tag = "" + } + if (msg === undefined) { + warn("message argument missing from expectFailContinue()") + msg = "" + } + if (!qtest_results.expectFailContinue(tag, msg, Qt.qtest_caller_file(), Qt.qtest_caller_line())) + throw new Error("QtQuickTest::expectFail") + } + + function warn(msg) { + if (msg === undefined) + msg = "" + qtest_results.warn(msg); + } + + function ignoreWarning(msg) { + if (msg === undefined) + msg = "" + qtest_results.ignoreWarning(msg) + } + + function wait(ms) { + qtest_results.wait(ms) + } + + function sleep(ms) { + qtest_results.sleep(ms) + } + + function keyPress(key, modifiers, delay) { + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.keyPress(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function keyRelease(key, modifiers, delay) { + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.keyRelease(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function keyClick(key, modifiers, delay) { + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.keyClick(key, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function mousePress(item, x, y, button, modifiers, delay) { + if (button === undefined) + button = Qt.LeftButton + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.mousePress(item, x, y, button, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function mouseRelease(item, x, y, button, modifiers, delay) { + if (button === undefined) + button = Qt.LeftButton + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.mouseRelease(item, x, y, button, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function mouseClick(item, x, y, button, modifiers, delay) { + if (button === undefined) + button = Qt.LeftButton + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.mouseClick(item, x, y, button, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function mouseDoubleClick(item, x, y, button, modifiers, delay) { + if (button === undefined) + button = Qt.LeftButton + if (modifiers === undefined) + modifiers = Qt.NoModifier + if (delay == undefined) + delay = -1 + if (!qtest_events.mouseDoubleClick(item, x, y, button, modifiers, delay)) + qtest_fail("window not shown", 2) + } + + function mouseMove(item, x, y, delay) { + if (delay == undefined) + delay = -1 + if (!qtest_events.mouseMove(item, x, y, delay)) + qtest_fail("window not shown", 2) + } + + // Functions that can be overridden in subclasses for init/cleanup duties. + function initTestCase() {} + function cleanupTestCase() {} + function init() {} + function cleanup() {} + + function qtest_runInternal(prop, arg) { + try { + qtest_testCaseResult = testCase[prop](arg) + } catch (e) { + qtest_testCaseResult = [] + if (e.message.indexOf("QtQuickTest::") != 0) { + // Test threw an unrecognized exception - fail. + qtest_results.fail("Uncaught exception: " + e.message, + e.fileName, e.lineNumber) + } + } + return !qtest_results.dataFailed + } + + function qtest_runFunction(prop, arg) { + qtest_results.functionType = TestResult.InitFunc + qtest_runInternal("init") + if (!qtest_results.skipped) { + qtest_results.functionType = TestResult.Func + qtest_runInternal(prop, arg) + qtest_results.functionType = TestResult.CleanupFunc + qtest_runInternal("cleanup") + } + qtest_results.functionType = TestResult.NoWhere + } + + function qtest_runBenchmarkFunction(prop, arg) { + qtest_results.startMeasurement() + do { + qtest_results.beginDataRun() + do { + // Run the initialization function. + qtest_results.functionType = TestResult.InitFunc + qtest_runInternal("init") + if (qtest_results.skipped) + break + + // Execute the benchmark function. + qtest_results.functionType = TestResult.Func + if (prop.indexOf("benchmark_once_") != 0) + qtest_results.startBenchmark(TestResult.RepeatUntilValidMeasurement, qtest_results.dataTag) + else + qtest_results.startBenchmark(TestResult.RunOnce, qtest_results.dataTag) + while (!qtest_results.isBenchmarkDone()) { + if (!qtest_runInternal(prop, arg)) + break + qtest_results.nextBenchmark() + } + qtest_results.stopBenchmark() + + // Run the cleanup function. + qtest_results.functionType = TestResult.CleanupFunc + qtest_runInternal("cleanup") + qtest_results.functionType = TestResult.NoWhere + } while (!qtest_results.measurementAccepted()) + qtest_results.endDataRun() + } while (qtest_results.needsMoreMeasurements()) + } + + function qtest_run() { + if (Qt.qtest_printAvailableFunctions) { + completed = true + return + } + + if (TestLogger.log_start_test()) { + qtest_results.reset() + qtest_results.testCaseName = name + qtest_results.startLogging() + } else { + qtest_results.testCaseName = name + } + running = true + + // Check the run list to see if this class is mentioned. + var functionsToRun = qtest_results.functionsToRun + if (functionsToRun.length > 0) { + var found = false + var list = [] + if (name.length > 0) { + var prefix = name + "::" + for (var index in functionsToRun) { + if (functionsToRun[index].indexOf(prefix) == 0) { + list.push(functionsToRun[index]) + found = true + } + } + } + if (!found) { + completed = true + if (!TestLogger.log_complete_test(qtest_testId)) { + qtest_results.stopLogging() + Qt.quit() + } + qtest_results.testCaseName = "" + return + } + functionsToRun = list + } + + // Run the initTestCase function. + qtest_results.functionName = "initTestCase" + qtest_results.functionType = TestResult.InitFunc + var runTests = true + if (!qtest_runInternal("initTestCase")) + runTests = false + qtest_results.finishTestFunction() + + // Run the test methods. + var testList = [] + if (runTests) { + for (var prop in testCase) { + if (prop.indexOf("test_") != 0 && prop.indexOf("benchmark_") != 0) + continue + var tail = prop.lastIndexOf("_data"); + if (tail != -1 && tail == (prop.length - 5)) + continue + testList.push(prop) + } + testList.sort() + } + var checkNames = (functionsToRun.length > 0) + for (var index in testList) { + var prop = testList[index] + var datafunc = prop + "_data" + var isBenchmark = (prop.indexOf("benchmark_") == 0) + if (checkNames) { + var index = functionsToRun.indexOf(name + "::" + prop) + if (index < 0) + continue + functionsToRun.splice(index, 1) + } + qtest_results.functionName = prop + if (datafunc in testCase) { + qtest_results.functionType = TestResult.DataFunc + if (qtest_runInternal(datafunc)) { + var table = qtest_testCaseResult + var haveData = false + qtest_results.initTestTable() + for (var index in table) { + haveData = true + var row = table[index] + if (!row.tag) + row.tag = "row " + index // Must have something + qtest_results.dataTag = row.tag + if (isBenchmark) + qtest_runBenchmarkFunction(prop, row) + else + qtest_runFunction(prop, row) + qtest_results.dataTag = "" + } + if (!haveData) + qtest_results.warn("no data supplied for " + prop + "() by " + datafunc + "()") + qtest_results.clearTestTable() + } + } else if (isBenchmark) { + qtest_runBenchmarkFunction(prop, null, isBenchmark) + } else { + qtest_runFunction(prop, null, isBenchmark) + } + qtest_results.finishTestFunction() + qtest_results.skipped = false + } + + // Run the cleanupTestCase function. + qtest_results.skipped = false + qtest_results.functionName = "cleanupTestCase" + qtest_results.functionType = TestResult.CleanupFunc + qtest_runInternal("cleanupTestCase") + + // Complain about missing functions that we were supposed to run. + if (functionsToRun.length > 0) + qtest_results.fail("Could not find functions: " + functionsToRun, "", 0) + + // Clean up and exit. + running = false + completed = true + qtest_results.finishTestFunction() + qtest_results.functionName = "" + + // Stop if there are no more tests to be run. + if (!TestLogger.log_complete_test(qtest_testId)) { + qtest_results.stopLogging() + Qt.quit() + } + qtest_results.testCaseName = "" + } + + onWhenChanged: { + if (when != qtest_prevWhen) { + qtest_prevWhen = when + if (when && !completed && !running) + qtest_run() + } + } + + onOptionalChanged: { + if (!completed) { + if (optional) + TestLogger.log_optional_test(qtest_testId) + else + TestLogger.log_mandatory_test(qtest_testId) + } + } + + // The test framework will set qtest.windowShown when the + // window is actually shown. If we are running with qmlviewer, + // then this won't happen. So we use a timer instead. + Timer { + id: qtest_windowShowTimer + interval: 100 + repeat: false + onTriggered: { windowShown = true } + } + + Component.onCompleted: { + if (Qt.qtest_printAvailableFunctions) { + var testList = [] + for (var prop in testCase) { + if (prop.indexOf("test_") != 0 && prop.indexOf("benchmark_") != 0) + continue + var tail = prop.lastIndexOf("_data"); + if (tail != -1 && tail == (prop.length - 5)) + continue + // Note: cannot run functions in TestCase elements + // that lack a name. + if (name.length > 0) + testList.push(name + "::" + prop + "()") + } + testList.sort() + for (var index in testList) + console.log(testList[index]) + return + } + qtest_testId = TestLogger.log_register_test(name) + if (optional) + TestLogger.log_optional_test(qtest_testId) + qtest_prevWhen = when + var isQmlViewer = Qt.qtest_wrapper ? false : true + if (isQmlViewer) + qtest_windowShowTimer.running = true + if (when && !completed && !running) + qtest_run() + } +} diff --git a/src/imports/testlib/main.cpp b/src/imports/testlib/main.cpp new file mode 100644 index 0000000000..5ef07b9d55 --- /dev/null +++ b/src/imports/testlib/main.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtDeclarative/qdeclarativeextensionplugin.h> +#include <QtDeclarative/qdeclarative.h> +#include <QtScript/qscriptvalue.h> +#include <QtScript/qscriptcontext.h> +#include <QtScript/qscriptcontextinfo.h> +#include <QtScript/qscriptengine.h> +#include "QtQuickTest/private/quicktestresult_p.h" +#include "QtQuickTest/private/quicktestevent_p.h" +QT_BEGIN_NAMESPACE + +QML_DECLARE_TYPE(QuickTestResult) +QML_DECLARE_TYPE(QuickTestEvent) + +// Copied from qdeclarativedebughelper_p.h in Qt, to avoid a dependency +// on a private header from Qt. +class Q_DECLARATIVE_EXPORT QDeclarativeDebugHelper +{ +public: + static QScriptEngine *getScriptEngine(QDeclarativeEngine *engine); + static void setAnimationSlowDownFactor(qreal factor); + static void enableDebugging(); +}; + +static QScriptContext *qtest_find_frame(QScriptContext *ctx) +{ + qint32 frame = 1; + if (ctx->argumentCount() > 0) + frame = ctx->argument(0).toInt32(); + ++frame; // Exclude the native function; start at its caller. + while (ctx) { + if (frame-- <= 0) + break; + ctx = ctx->parentContext(); + } + return ctx; +} + +static QScriptValue qtest_caller_file + (QScriptContext *ctx, QScriptEngine *engine) +{ + ctx = qtest_find_frame(ctx); + if (ctx) { + QScriptContextInfo info(ctx); + return engine->newVariant(info.fileName()); + } + return engine->newVariant(QLatin1String("")); +} + +static QScriptValue qtest_caller_line + (QScriptContext *ctx, QScriptEngine *engine) +{ + ctx = qtest_find_frame(ctx); + if (ctx) { + QScriptContextInfo info(ctx); + return engine->newVariant(info.lineNumber()); + } + return engine->newVariant(qint32(0)); +} + +class QTestQmlModule : public QDeclarativeExtensionPlugin +{ + Q_OBJECT +public: + virtual void registerTypes(const char *uri) + { + Q_ASSERT(QLatin1String(uri) == QLatin1String("QtTest")); + qmlRegisterType<QuickTestResult>(uri,1,0,"TestResult"); + qmlRegisterType<QuickTestEvent>(uri,1,0,"TestEvent"); + } + void initializeEngine(QDeclarativeEngine *engine, const char *) + { + // Install some helper functions in the global "Qt" object + // for walking the stack and finding a caller's location. + // Normally we would use an exception's backtrace, but JSC + // only provides the top-most frame in the backtrace. + QScriptEngine *eng = QDeclarativeDebugHelper::getScriptEngine(engine); + QScriptValue qtObject + = eng->globalObject().property(QLatin1String("Qt")); + qtObject.setProperty + (QLatin1String("qtest_caller_file"), + eng->newFunction(qtest_caller_file)); + qtObject.setProperty + (QLatin1String("qtest_caller_line"), + eng->newFunction(qtest_caller_line)); + } +}; + +QT_END_NAMESPACE + +#include "main.moc" + +Q_EXPORT_PLUGIN2(qmltestplugin, QT_PREPEND_NAMESPACE(QTestQmlModule)); diff --git a/src/imports/testlib/qmldir b/src/imports/testlib/qmldir new file mode 100644 index 0000000000..9e872f9683 --- /dev/null +++ b/src/imports/testlib/qmldir @@ -0,0 +1,3 @@ +plugin qmltestplugin +TestCase 1.0 TestCase.qml +SignalSpy 1.0 SignalSpy.qml diff --git a/src/imports/testlib/signalspy.h b/src/imports/testlib/signalspy.h new file mode 100644 index 0000000000..4a97959339 --- /dev/null +++ b/src/imports/testlib/signalspy.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SIGNALSPY_H +#define SIGNALSPY_H + +// This is a dummy header for defining the interface of "SignalSpy.qml" to qdoc. + +#include <QtDeclarative/qdeclarativeitem.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +class SignalSpy : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QObject *target READ target WRITE setTarget NOTIFY targetChanged) + Q_PROPERTY(QString signalName READ signalName WRITE signalName NOTIFY signalNameChanged) + Q_PROPERTY(int count READ count countChanged) +public: + SignalSpy(QDeclarativeItem *parent) : QDeclarativeItem(parent) {} + ~SignalSpy() + + QObject *target() const; + void setTarget(QObject *target); + + QString signalName() const; + void setSignalName(const QString &signalName); + + int count() const; + +Q_SIGNALS: + void targetChanged(); + void signalNameChanged(); + void countChanged(); +}; + +QML_DECLARE_TYPE(SignalSpy) + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/imports/testlib/signalspy.qdoc b/src/imports/testlib/signalspy.qdoc new file mode 100644 index 0000000000..3ed39c4ad2 --- /dev/null +++ b/src/imports/testlib/signalspy.qdoc @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \qmlclass SignalSpy SignalSpy + \brief The SignalSpy item enables introspection of signal emission. + \since 4.8 + \ingroup qtest::qml + + In the following example, a SignalSpy is installed to watch the + "clicked" signal on a user-defined Button element. When the signal + is emitted, the \l count property on the spy will be increased. + + \code + Button { + id: button + SignalSpy { + id: spy + target: button + signalName: "clicked" + } + TestCase { + name: "ButtonClick" + function test_click() { + compare(spy.count, 0) + button.clicked(); + compare(spy.count, 1) + } + } + } + \endcode + + The above style of test is suitable for signals that are emitted + synchronously. For asynchronous signals, the wait() method can be + used to block the test until the signal occurs (or a timeout expires). + + \sa TestCase +*/ + +/*! + \qmlproperty object SignalSpy::target + + This property defines the target object that will be used to + listen for emissions of the \l signalName signal. + + \sa signalName, count +*/ + +/*! + \qmlproperty string SignalSpy::signalName + + This property defines the name of the signal on \l target to + listen for. + + \sa target, count +*/ + +/*! + \qmlproperty int SignalSpy::count + + This property defines the number of times that \l signalName has + been emitted from \l target since the last call to clear(). + + \sa target, signalName, clear() +*/ + +/*! + \qmlmethod SignalSpy::clear() + + Clears \l count to 0. + + \sa count, wait() +*/ + +/*! + \qmlmethod SignalSpy::wait(timeout = 5000) + + Waits for the signal \l signalName on \l target to be emitted, + for up to \a timeout milliseconds. The test case will fail if + the signal is not emitted. + + \code + SignalSpy { + id: spy + target: button + signalName: "clicked" + } + + function test_async_click() { + ... + // do something that will cause clicked() to be emitted + ... + spy.wait() + compare(spy.count, 1) + } + \endcode + + There are two possible scenarios: the signal has already been + emitted when wait() is called, or the signal has not yet been + emitted. The wait() function handles the first scenario by immediately + returning if the signal has already occurred. + + The clear() method can be used to discard information about signals + that have already occurred to synchronize wait() with future signal + emissions. + + \sa clear(), TestCase::tryCompare() +*/ diff --git a/src/imports/testlib/testcase.h b/src/imports/testlib/testcase.h new file mode 100644 index 0000000000..f7a1992290 --- /dev/null +++ b/src/imports/testlib/testcase.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TESTCASE_H +#define TESTCASE_H + +// This is a dummy header for defining the interface of "TestCase.qml" to qdoc. + +#include <QtDeclarative/qdeclarativeitem.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +class TestCase : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(bool when READ when WRITE setWhen NOTIFY whenChanged) + Q_PROPERTY(bool optional READ optional WRITE setOptional NOTIFY optionalChanged) + Q_PROPERTY(bool completed READ completed NOTIFY completedChanged) + Q_PROPERTY(bool running READ running NOTIFY runningChanged) + Q_PROPERTY(bool windowShown READ windowShown NOTIFY windowShownChanged) +public: + TestCase(QDeclarativeItem *parent) : QDeclarativeItem(parent) {} + ~TestCase() + + QString name() const; + void setName(const QString &name); + + bool when() const; + void setWhen(bool when); + + bool optional() const; + void setOptional(bool optional); + + bool completed() const; + bool running() const; + bool windowShown() const; + +Q_SIGNALS: + void nameChanged(); + void whenChanged(); + void optionalChanged(); + void completedChanged(); + void runningChanged(); + void windowShownChanged(); +}; + +QML_DECLARE_TYPE(TestCase) + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif diff --git a/src/imports/testlib/testcase.qdoc b/src/imports/testlib/testcase.qdoc new file mode 100644 index 0000000000..bb7044f907 --- /dev/null +++ b/src/imports/testlib/testcase.qdoc @@ -0,0 +1,597 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \qmlclass TestCase TestCase + \brief The TestCase item represents a unit test case. + \since 4.8 + \ingroup qtest::qml + + \section1 Introduction to QML test cases + + Test cases are written as JavaScript functions within a TestCase + element: + + \code + import QtQuick 1.0 + import QtQuickTest 1.0 + + TestCase { + name: "MathTests" + + function test_math() { + compare(2 + 2, 4, "2 + 2 = 4") + } + + function test_fail() { + compare(2 + 2, 5, "2 + 2 = 5") + } + } + \endcode + + Functions whose names start with "test_" are treated as test cases + to be executed. The \l name property is used to prefix the functions + in the output: + + \code + ********* Start testing of MathTests ********* + Config: Using QTest library 4.7.2, Qt 4.7.2 + PASS : MathTests::initTestCase() + FAIL! : MathTests::test_fail() 2 + 2 = 5 + Actual (): 4 + Expected (): 5 + Loc: [/home/.../tst_math.qml(12)] + PASS : MathTests::test_math() + PASS : MathTests::cleanupTestCase() + Totals: 3 passed, 1 failed, 0 skipped + ********* Finished testing of MathTests ********* + \endcode + + Because of the way JavaScript properties work, the order in which the + test functions are found is unpredictable. To assist with predictability, + the test framework will sort the functions on ascending order of name. + This can help when there are two tests that must be run in order. + + Multiple TestCase elements can be supplied. The test program will exit + once they have all completed. If a test case doesn't need to run + (because a precondition has failed), then \l optional can be set to true. + + \section1 Data-driven tests + + Table data can be provided to a test using a function name that ends + with "_data": + + \code + import QtQuick 1.0 + import QtQuickTest 1.0 + + TestCase { + name: "DataTests" + + function test_table_data() { + return [ + {tag: "2 + 2 = 4", a: 2, b: 2, answer: 4 }, + {tag: "2 + 6 = 8", a: 2, b: 6, answer: 8 }, + ] + } + + function test_table(data) { + compare(data.a + data.b, data.answer) + } + } + \endcode + + The test framework will iterate over all of the rows in the table + and pass each row to the test function. As shown, the columns can be + extracted for use in the test. The \c tag column is special - it is + printed by the test framework when a row fails, to help the reader + identify which case failed amongst a set of otherwise passing tests. + + \section1 Benchmarks + + Functions whose names start with "benchmark_" will be run multiple + times with the Qt benchmark framework, with an average timing value + reported for the runs. This is equivalent to using the \c{QBENCHMARK} + macro in the C++ version of QTestLib. + + \code + TestCase { + id: top + name: "CreateBenchmark" + + function benchmark_create_component() { + var component = Qt.createComponent("item.qml") + var obj = component.createObject(top) + obj.destroy() + component.destroy() + } + } + + RESULT : CreateBenchmark::benchmark_create_component: + 0.23 msecs per iteration (total: 60, iterations: 256) + PASS : CreateBenchmark::benchmark_create_component() + \endcode + + To get the effect of the \c{QBENCHMARK_ONCE} macro, prefix the test + function name with "benchmark_once_". + + \section1 Simulating keyboard and mouse events + + The keyPress(), keyRelease(), and keyClick() methods can be used + to simulate keyboard events within unit tests. The events are + delivered to the currently focused QML item. + + \code + Rectangle { + width: 50; height: 50 + focus: true + + TestCase { + name: "KeyClick" + when: windowShown + + function test_key_click() { + keyClick(Qt.Key_Left) + ... + } + } + } + \endcode + + The mousePress(), mouseRelease(), mouseClick(), mouseDoubleClick(), + and mouseMove() methods can be used to simulate mouse events in a + similar fashion. + + \bold{Note:} keyboard and mouse events can only be delivered once the + main window has been shown. Attempts to deliver events before then + will fail. Use the \l when and windowShown properties to track + when the main window has been shown. + + \sa SignalSpy +*/ + +/*! + \qmlproperty string TestCase::name + + This property defines the name of the test case for result reporting. + The default is the empty string. + + \code + TestCase { + name: "ButtonTests" + ... + } + \endcode +*/ + +/*! + \qmlproperty bool TestCase::when + + This property should be set to true when the application wants + the test cases to run. The default value is true. In the following + example, a test is run when the user presses the mouse button: + + \code + Rectangle { + id: foo + width: 640; height: 480 + color: "cyan" + + MouseArea { + id: area + anchors.fill: parent + } + + property bool bar: true + + TestCase { + name: "ItemTests" + when: area.pressed + id: test1 + + function test_bar() { + verify(bar) + } + } + } + \endcode + + The test application will exit once all \l TestCase elements + have been triggered and have run. The \l optional property can + be used to exclude a \l TestCase element. + + \sa optional, completed +*/ + +/*! + \qmlproperty bool TestCase::optional + + Multiple \l TestCase elements can be supplied in a test application. + The application will exit once they have all completed. If a test case + does not need to run (because a precondition has failed), then this + property can be set to true. The default value is false. + + \code + TestCase { + when: false + optional: true + function test_not_run() { + verify(false) + } + } + \endcode + + \sa when, completed +*/ + +/*! + \qmlproperty bool TestCase::completed + + This property will be set to true once the test case has completed + execution. Test cases are only executed once. The initial value + is false. + + \sa running, when +*/ + +/*! + \qmlproperty bool TestCase::running + + This property will be set to true while the test case is running. + The initial value is false, and the value will become false again + once the test case completes. + + \sa completed, when +*/ + +/*! + \qmlproperty bool TestCase::windowShown + + This property will be set to true after the QML viewing window has + been displayed. Normally test cases run as soon as the test application + is loaded and before a window is displayed. If the test case involves + visual elements and behaviors, then it may need to be delayed until + after the window is shown. + + \code + Button { + id: button + onClicked: text = "Clicked" + TestCase { + name: "ClickTest" + when: windowShown + function test_click() { + button.clicked(); + compare(button.text, "Clicked"); + } + } + } + \endcode +*/ + +/*! + \qmlmethod TestCase::fail(message = "") + + Fails the current test case, with the optional \a message. + Similar to \c{QFAIL(message)} in C++. +*/ + +/*! + \qmlmethod TestCase::verify(condition, message = "") + + Fails the current test case if \a condition is false, and + displays the optional \a message. Similar to \c{QVERIFY(condition)} + or \c{QVERIFY2(condition, message)} in C++. +*/ + +/*! + \qmlmethod TestCase::compare(actual, expected, message = "") + + Fails the current test case if \a actual is not the same as + \a expected, and displays the optional \a message. Similar + to \c{QCOMPARE(actual, expected)} in C++. + + \sa tryCompare() +*/ + +/*! + \qmlmethod TestCase::tryCompare(obj, property, expected, timeout = 5000) + + Fails the current test case if the specified \a property on \a obj + is not the same as \a expected. The test will be retried multiple + times until the \a timeout (in milliseconds) is reached. + + This function is intended for testing applications where a property + changes value based on asynchronous events. Use compare() for testing + synchronous property changes. + + \code + tryCompare(img, "status", BorderImage.Ready) + compare(img.width, 120) + compare(img.height, 120) + compare(img.horizontalTileMode, BorderImage.Stretch) + compare(img.verticalTileMode, BorderImage.Stretch) + \endcode + + SignalSpy::wait() provides an alternative method to wait for a + signal to be emitted. + + \sa compare(), SignalSpy::wait() +*/ + +/*! + \qmlmethod TestCase::skip(message = "") + + Skips the current test case and prints the optional \a message. + If this is a data-driven test, then only the current row is skipped. + Similar to \c{QSKIP(message, SkipSingle)} in C++. + + \sa skipAll() +*/ + +/*! + \qmlmethod TestCase::skipAll(message = "") + + Skips the current test case and prints the optional \a message. + If this is a data-driven test, then all remaining rows are skipped. + Similar to \c{QSKIP(message, SkipAll)} in C++. + + \sa skip() +*/ + +/*! + \qmlmethod TestCase::expectFail(tag, message) + + In a data-driven test, marks the row associated with \a tag as + expected to fail. When the fail occurs, display the \a message, + abort the test, and mark the test as passing. Similar to + \c{QEXPECT_FAIL(tag, message, Abort)} in C++. + + If the test is not data-driven, then \a tag must be set to + the empty string. + + \sa expectFailContinue() +*/ + +/*! + \qmlmethod TestCase::expectFailContinue(tag, message) + + In a data-driven test, marks the row associated with \a tag as + expected to fail. When the fail occurs, display the \a message, + and then continue the test. Similar to + \c{QEXPECT_FAIL(tag, message, Continue)} in C++. + + If the test is not data-driven, then \a tag must be set to + the empty string. + + \sa expectFail() +*/ + +/*! + \qmlmethod TestCase::warn(message) + + Prints \a message as a warning message. Similar to + \c{QWARN(message)} in C++. + + \sa ignoreWarning() +*/ + +/*! + \qmlmethod TestCase::ignoreWarning(message) + + Marks \a message as an ignored warning message. When it occurs, + the warning will not be printed and the test passes. If the message + does not occur, then the test will fail. Similar to + \c{QTest::ignoreMessage(QtWarningMsg, message)} in C++. + + \sa warn() +*/ + +/*! + \qmlmethod TestCase::wait(ms) + + Waits for \a ms milliseconds while processing Qt events. + + \sa sleep() +*/ + +/*! + \qmlmethod TestCase::sleep(ms) + + Sleeps for \a ms milliseconds without processing Qt events. + + \sa wait() +*/ + +/*! + \qmlmethod TestCase::keyClick(key, modifiers = Qt.NoModifier, delay = -1) + + Simulates clicking of \a key with an optional \a modifier on the currently + focused item. If \a delay is larger than 0, the test will wait for + \a delay milliseconds. + + \sa keyPress(), keyRelease() +*/ + +/*! + \qmlmethod TestCase::keyPress(key, modifiers = Qt.NoModifier, delay = -1) + + Simulates pressing a \a key with an optional \a modifier on the currently + focused item. If \a delay is larger than 0, the test will wait for + \a delay milliseconds. + + \bold{Note:} At some point you should release the key using keyRelease(). + + \sa keyRelease(), keyClick() +*/ + +/*! + \qmlmethod TestCase::keyRelease(key, modifiers = Qt.NoModifier, delay = -1) + + Simulates releasing a \a key with an optional \a modifier on the currently + focused item. If \a delay is larger than 0, the test will wait for + \a delay milliseconds. + + \sa keyPress(), keyClick() +*/ + +/*! + \qmlmethod TestCase::mousePress(item, x, y, button = Qt.LeftButton, modifiers = Qt.NoModifier, delay = -1) + + Simulates pressing a mouse \a button with an optional \a modifier + on an \a item. The position is defined by \a x and \a y. If \a delay is + specified, the test will wait for the specified amount of milliseconds + before the press. + + The position given by \a x and \a y is transformed from the co-ordinate + system of \a item into window co-ordinates and then delivered. + If \a item is obscured by another item, or a child of \a item occupies + that position, then the event will be delivered to the other item instead. + + \sa mouseRelease(), mouseClick(), mouseDoubleClick(), mouseMove() +*/ + +/*! + \qmlmethod TestCase::mouseRelease(item, x, y, button = Qt.LeftButton, modifiers = Qt.NoModifier, delay = -1) + + Simulates releasing a mouse \a button with an optional \a modifier + on an \a item. The position of the release is defined by \a x and \a y. + If \a delay is specified, the test will wait for the specified amount of + milliseconds before releasing the button. + + The position given by \a x and \a y is transformed from the co-ordinate + system of \a item into window co-ordinates and then delivered. + If \a item is obscured by another item, or a child of \a item occupies + that position, then the event will be delivered to the other item instead. + + \sa mousePress(), mouseClick(), mouseDoubleClick(), mouseMove() +*/ + +/*! + \qmlmethod TestCase::mouseClick(item, x, y, button = Qt.LeftButton, modifiers = Qt.NoModifier, delay = -1) + + Simulates clicking a mouse \a button with an optional \a modifier + on an \a item. The position of the click is defined by \a x and \a y. + If \a delay is specified, the test will wait for the specified amount of + milliseconds before pressing and before releasing the button. + + The position given by \a x and \a y is transformed from the co-ordinate + system of \a item into window co-ordinates and then delivered. + If \a item is obscured by another item, or a child of \a item occupies + that position, then the event will be delivered to the other item instead. + + \sa mousePress(), mouseRelease(), mouseDoubleClick(), mouseMove() +*/ + +/*! + \qmlmethod TestCase::mouseDoubleClick(item, x, y, button = Qt.LeftButton, modifiers = Qt.NoModifier, delay = -1) + + Simulates double-clicking a mouse \a button with an optional \a modifier + on an \a item. The position of the click is defined by \a x and \a y. + If \a delay is specified, the test will wait for the specified amount of + milliseconds before pressing and before releasing the button. + + The position given by \a x and \a y is transformed from the co-ordinate + system of \a item into window co-ordinates and then delivered. + If \a item is obscured by another item, or a child of \a item occupies + that position, then the event will be delivered to the other item instead. + + \sa mousePress(), mouseRelease(), mouseClick(), mouseMove() +*/ + +/*! + \qmlmethod TestCase::mouseMove(item, x, y, delay = -1) + + Moves the mouse pointer to the position given by \a x and \a y within + \a item. If a \a delay (in milliseconds) is given, the test will wait + before moving the mouse pointer. + + The position given by \a x and \a y is transformed from the co-ordinate + system of \a item into window co-ordinates and then delivered. + If \a item is obscured by another item, or a child of \a item occupies + that position, then the event will be delivered to the other item instead. + + \sa mousePress(), mouseRelease(), mouseClick(), mouseDoubleClick() +*/ + +/*! + \qmlmethod TestCase::initTestCase() + + This function is called before any other test functions in the + \l TestCase element. The default implementation does nothing. + The application can provide its own implementation to perform + test case initialization. + + \sa cleanupTestCase(), init() +*/ + +/*! + \qmlmethod TestCase::cleanupTestCase() + + This function is called after all other test functions in the + \l TestCase element have completed. The default implementation + does nothing. The application can provide its own implementation + to perform test case cleanup. + + \sa initTestCase(), cleanup() +*/ + +/*! + \qmlmethod TestCase::init() + + This function is called before each test function that is + executed in the \l TestCase element. The default implementation + does nothing. The application can provide its own implementation + to perform initialization before each test function. + + \sa cleanup(), initTestCase() +*/ + +/*! + \qmlmethod TestCase::cleanup() + + This function is called after each test function that is + executed in the \l TestCase element. The default implementation + does nothing. The application can provide its own implementation + to perform cleanup after each test function. + + \sa init(), cleanupTestCase() +*/ diff --git a/src/imports/testlib/testlib.pro b/src/imports/testlib/testlib.pro new file mode 100644 index 0000000000..cc29d53f51 --- /dev/null +++ b/src/imports/testlib/testlib.pro @@ -0,0 +1,29 @@ +TARGET = qmltestplugin +TARGETPATH = QtTest +include(../qimportbase.pri) + +CONFIG += qt plugin + +symbian { + CONFIG += epocallowdlldata + contains(QT_EDITION, OpenSource) { + TARGET.CAPABILITY = LocalServices NetworkServices ReadUserData UserEnvironment WriteUserData + } else { + TARGET.CAPABILITY = All -Tcb + } +} + +QT += declarative script qmltest qmltest-private + +SOURCES += main.cpp +HEADERS += + +qdeclarativesources.files += \ + qmldir \ + TestCase.qml \ + SignalSpy.qml \ + testlogger.js + +qdeclarativesources.path += $$[QT_INSTALL_IMPORTS]/QtTest +target.path += $$[QT_INSTALL_IMPORTS]/QtTest +INSTALLS += qdeclarativesources target diff --git a/src/imports/testlib/testlogger.js b/src/imports/testlib/testlogger.js new file mode 100644 index 0000000000..7fd33ca6f4 --- /dev/null +++ b/src/imports/testlib/testlogger.js @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +.pragma library + +// We need a global place to store the results that can be +// shared between multiple TestCase instances. Because QML +// creates a separate scope for every inclusion of this file, +// we hijack the global "Qt" object to store our data. +function log_init_results() +{ + if (!Qt.testResults) { + Qt.testResults = { + reportedStart: false, + nextId: 0, + testCases: [] + } + } +} + +function log_register_test(name) +{ + log_init_results() + var testId = Qt.testResults.nextId++ + Qt.testResults.testCases.push(testId) + return testId +} + +function log_optional_test(testId) +{ + log_init_results() + var index = Qt.testResults.testCases.indexOf(testId) + if (index >= 0) + Qt.testResults.testCases.splice(index, 1) +} + +function log_mandatory_test(testId) +{ + log_init_results() + var index = Qt.testResults.testCases.indexOf(testId) + if (index == -1) + Qt.testResults.testCases.push(testId) +} + +function log_start_test() +{ + log_init_results() + if (Qt.testResults.reportedStart) + return false + Qt.testResults.reportedStart = true + return true +} + +function log_complete_test(testId) +{ + var index = Qt.testResults.testCases.indexOf(testId) + if (index >= 0) + Qt.testResults.testCases.splice(index, 1) + return Qt.testResults.testCases.length > 0 +} |