From 5fe9006ea262101ecc583edaff2f7ea794b2c03e Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 29 Apr 2015 14:21:54 +0200 Subject: Introducing ControlSpy Allows specifying expected sequences on signals for a control, and optionally test for property values when each signal is emitted. Rewrote Button auto-test suite using ControlSpy. Change-Id: Id27c1b9ca7ee070898bcd008379e06959341ee04 Reviewed-by: J-P Nurmi --- tests/auto/controls/controls.pro | 2 +- tests/auto/controls/data/ControlSpy.qml | 94 ++++++++++++++++++++++++ tests/auto/controls/data/tst_button.qml | 125 +++++++++----------------------- 3 files changed, 128 insertions(+), 93 deletions(-) create mode 100644 tests/auto/controls/data/ControlSpy.qml (limited to 'tests') diff --git a/tests/auto/controls/controls.pro b/tests/auto/controls/controls.pro index 81f18623..d1dd6814 100644 --- a/tests/auto/controls/controls.pro +++ b/tests/auto/controls/controls.pro @@ -9,4 +9,4 @@ OTHER_FILES += \ $$PWD/data/* TESTDATA += \ - $$PWD/data/* + $$PWD/data/tst_* diff --git a/tests/auto/controls/data/ControlSpy.qml b/tests/auto/controls/data/ControlSpy.qml new file mode 100644 index 00000000..eedcdf98 --- /dev/null +++ b/tests/auto/controls/data/ControlSpy.qml @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.5 + +QtObject { + property QtObject target: null + property var expectedSequence: [] + property int sequenceIndex: 0 + property bool sequenceFailure: false + readonly property bool success: sequenceIndex === expectedSequence.length + && !sequenceFailure + + function reset() { + sequenceIndex = 0 + sequenceFailure = false + } + + function checkSignal(signalName) { + if (sequenceFailure) + return; + + if (!expectedSequence.length || sequenceIndex >= expectedSequence.length) { + console.warn("ControlSpy: received unexpected signal '" + signalName + "'.") + sequenceFailure = true + return + } + + var expectedSignal = expectedSequence[sequenceIndex] + if (typeof expectedSignal === "string") { + if (expectedSignal === signalName) { + sequenceIndex++ + return + } + } else if (typeof expectedSignal === "object") { + expectedSignal = expectedSignal[0] + if (expectedSignal === signalName) { + var expectedValues = expectedSequence[sequenceIndex][1] + for (var p in expectedValues) + if (target[p] != expectedValues[p]) { + console.warn("ControlSpy: property value mismatch for '" + p + "'" + + " after '" + signalName + "' signal. " + + "Expected: " + expectedValues[p] + ", actual: " + control[p] +".") + sequenceFailure = true + return + } + sequenceIndex++ + return + } + } + console.warn("ControlSpy: signal names mismatch. " + + "Expected: " + expectedSignal + ", received: " + signalName +".") + sequenceFailure = true + } + + onExpectedSequenceChanged: reset() +} diff --git a/tests/auto/controls/data/tst_button.qml b/tests/auto/controls/data/tst_button.qml index f4ada30c..865b495b 100644 --- a/tests/auto/controls/data/tst_button.qml +++ b/tests/auto/controls/data/tst_button.qml @@ -50,47 +50,28 @@ TestCase { when: windowShown name: "Button" - SignalSpy { - id: pressedChangedSpy - signalName: "pressedChanged" - } - - SignalSpy { - id: releasedSpy - signalName: "released" - } - - SignalSpy { - id: canceledSpy - signalName: "canceled" - } - - SignalSpy { - id: clickedSpy - signalName: "clicked" - } - Component { id: button - Button { } + Button { + id: control + + property ControlSpy spy: ControlSpy { + id: spy + target: control + } + + onPressed: spy.checkSignal("pressed") + onReleased: spy.checkSignal("released") + onCanceled: spy.checkSignal("canceled") + onClicked: spy.checkSignal("clicked") + onPressedChanged: spy.checkSignal("pressedChanged") + } } function init() { - verify(!pressedChangedSpy.target) - verify(!releasedSpy.target) - verify(!clickedSpy.target) - compare(pressedChangedSpy.count, 0) - compare(releasedSpy.count, 0) - compare(clickedSpy.count, 0) } function cleanup() { - pressedChangedSpy.target = null - releasedSpy.target = null - clickedSpy.target = null - pressedChangedSpy.clear() - releasedSpy.clear() - clickedSpy.clear() } function test_defaults() { @@ -115,77 +96,41 @@ TestCase { function test_mouse() { var control = button.createObject(testCase) - pressedChangedSpy.target = control - releasedSpy.target = control - canceledSpy.target = control - clickedSpy.target = control - verify(pressedChangedSpy.valid) - verify(releasedSpy.valid) - verify(canceledSpy.valid) - verify(clickedSpy.valid) - - // check + // click + control.spy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"] mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton) - compare(pressedChangedSpy.count, 1) - compare(releasedSpy.count, 0) - compare(canceledSpy.count, 0) - compare(clickedSpy.count, 0) compare(control.pressed, true) + verify(control.spy.success) + control.spy.expectedSequence = [["pressedChanged", { "pressed": false }], "released", "clicked"] mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton) - compare(pressedChangedSpy.count, 2) - compare(releasedSpy.count, 1) - compare(canceledSpy.count, 0) - compare(clickedSpy.count, 1) - compare(control.pressed, false) - - // uncheck - mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton) - compare(pressedChangedSpy.count, 3) - compare(releasedSpy.count, 1) - compare(canceledSpy.count, 0) - compare(clickedSpy.count, 1) - compare(control.pressed, true) - - mouseRelease(control, control.width / 2, control.height / 2, Qt.LeftButton) - compare(pressedChangedSpy.count, 4) - compare(releasedSpy.count, 2) - compare(canceledSpy.count, 0) - compare(clickedSpy.count, 2) compare(control.pressed, false) + verify(control.spy.success) // release outside + control.spy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed"] mousePress(control, control.width / 2, control.height / 2, Qt.LeftButton) - compare(pressedChangedSpy.count, 5) - compare(releasedSpy.count, 2) - compare(canceledSpy.count, 0) - compare(clickedSpy.count, 2) compare(control.pressed, true) + verify(control.spy.success) + control.spy.expectedSequence = [["pressedChanged", { "pressed": false }]] mouseMove(control, control.width * 2, control.height * 2, 0, Qt.LeftButton) compare(control.pressed, false) + verify(control.spy.success) + control.spy.expectedSequence = [["canceled", { "pressed": false }]] mouseRelease(control, control.width * 2, control.height * 2, Qt.LeftButton) - compare(pressedChangedSpy.count, 6) - compare(releasedSpy.count, 2) - compare(canceledSpy.count, 1) - compare(clickedSpy.count, 2) compare(control.pressed, false) + verify(control.spy.success) // right button + control.spy.expectedSequence = [] mousePress(control, control.width / 2, control.height / 2, Qt.RightButton) - compare(pressedChangedSpy.count, 6) - compare(releasedSpy.count, 2) - compare(canceledSpy.count, 1) - compare(clickedSpy.count, 2) compare(control.pressed, false) mouseRelease(control, control.width / 2, control.height / 2, Qt.RightButton) - compare(pressedChangedSpy.count, 6) - compare(releasedSpy.count, 2) - compare(canceledSpy.count, 1) - compare(clickedSpy.count, 2) compare(control.pressed, false) + verify(control.spy.success) control.destroy() } @@ -193,25 +138,21 @@ TestCase { function test_keys() { var control = button.createObject(testCase) - clickedSpy.target = control - verify(clickedSpy.valid) - control.forceActiveFocus() verify(control.activeFocus) - // check - keyClick(Qt.Key_Space) - compare(clickedSpy.count, 1) - - // uncheck + // click + control.spy.expectedSequence = [["pressedChanged", { "pressed": true }], "pressed", + ["pressedChanged", { "pressed": false }], "released", "clicked"] keyClick(Qt.Key_Space) - compare(clickedSpy.count, 2) + verify(control.spy.success) // no change + control.spy.expectedSequence = [] var keys = [Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape, Qt.Key_Tab] for (var i = 0; i < keys.length; ++i) { keyClick(keys[i]) - compare(clickedSpy.count, 2) + verify(control.spy.success) } control.destroy() -- cgit v1.2.3