diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2016-11-17 15:18:17 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2016-12-14 07:20:30 +0000 |
commit | 7aaebe557476c088d339f1b4b114a671885c4ea8 (patch) | |
tree | 827ca5f0b3e94b705d9a1a62597181eb28ff1893 /tests | |
parent | 9fbc5aa8866bbc19cc0db9f5f247276f36f3e283 (diff) |
TestCase: add createTemporaryObject(), createTemporaryQmlObject()
A typical pattern with QML tests is to dynamically create
an item and then destroy it at the end of the test function:
TestCase {
id: testCase
name: "MyTest"
when: windowShown
function test_click() {
var item = Qt.createQmlObject("import QtQuick 2.0; Item {}", testCase);
verify(item);
// Test item...
item.destroy();
}
}
The problem with this pattern is that any failures in the test function
will cause the call to item.destroy() to be skipped, leaving the item
hanging around in the scene until the test case has finished. This can
result in interference with future tests; for example, by blocking input
events or producing unrelated debug output that makes it difficult to
follow the code's execution.
By calling e.g. createTemporaryQmlObject() instead, the object is
guaranteed to be destroyed at the end of the test function:
TestCase {
id: testCase
name: "MyTest"
when: windowShown
function test_click() {
var item = createTemporaryObject("import QtQuick 2.0; Item {}", testCase);
verify(item);
// Test item...
// Don't need to worry about destroying "item" here.
}
}
[ChangeLog][TestCase] Added createTemporaryObject() and
createTemporaryQmlObject() functions that can be used to ensure that
dynamically created objects are destroyed at the end of each test
function.
Change-Id: I483342052462aa86464c86bf2082892712dceb58
Task-number: QTBUG-56361
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Michael Brasser <michael.brasser@live.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qmltest/selftests/tst_createTemporaryObject.qml | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/tests/auto/qmltest/selftests/tst_createTemporaryObject.qml b/tests/auto/qmltest/selftests/tst_createTemporaryObject.qml new file mode 100644 index 0000000000..c4912c7388 --- /dev/null +++ b/tests/auto/qmltest/selftests/tst_createTemporaryObject.qml @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.8 +import QtQuick.Window 2.2 +import QtTest 1.2 + +TestCase { + id: testCase + name: "tst_createTemporaryObject" + width: 100 + height: 100 + when: windowShown + + property var createdObjectNames: [] + + function verifyNoChildren() { + for (var i = 0; i < createdObjectNames.length; ++i) { + verify(!findChild(testCase, createdObjectNames[i])); + } + } + + function init() { + // The items are destroyed after cleanup(), so we check here after every test, + // and once for the last test in cleanupTestCase(). + verifyNoChildren(); + } + + function cleanupTestCase() { + verifyNoChildren(); + } + + function test_fromQml_data() { + return [ + { tag: "QtObject", qml: "import QtQml 2.0; QtObject {}" }, + { tag: "Item", qml: "import QtQuick 2.0; Item {}" }, + ]; + } + + function test_fromQml(data) { + var object = createTemporaryQmlObject(data.qml, testCase); + verify(object); + + object.objectName = data.tag + "FromQml"; + compare(findChild(testCase, object.objectName), object); + + createdObjectNames.push(object.objectName); + } + + Component { + id: objectComponent + + QtObject {} + } + + Component { + id: itemComponent + + Item {} + } + + Component { + id: windowComponent + + Window {} + } + + function test_fromComponent_data() { + return [ + { tag: "QtObject", component: objectComponent }, + { tag: "Item", component: itemComponent }, + { tag: "Window", component: windowComponent }, + ]; + } + + function test_fromComponent(data) { + var object = createTemporaryObject(data.component, testCase); + verify(object); + + object.objectName = data.tag + "FromComponent"; + compare(findChild(testCase, object.objectName), object); + + if (object.hasOwnProperty("contentItem")) + object.contentItem.objectName = "WindowContentItemFromComponent"; + + createdObjectNames.push(object.objectName); + } +} |