From 6705553a40aea013dac604831e9aaf4005f70d0e Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Fri, 21 Oct 2016 10:33:12 +0200 Subject: [PATCH] feat(Form/ExclusiveButtons): add spec file to test emitted signals --- .../Common/Dialog/ConfirmDialog.spec.qml | 23 +++--- .../modules/Common/Form/ExclusiveButtons.qml | 8 +- .../Common/Form/ExclusiveButtons.spec.qml | 80 +++++++++++++++++++ tests/ui/modules/Common/InvertedMouseArea.qml | 2 +- .../ui/modules/Common/Popup/DropDownMenu.qml | 2 +- .../Common/View/ScrollableListView.qml | 2 +- tests/ui/views/MainWindow/Contact.qml | 2 +- 7 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 tests/ui/modules/Common/Form/ExclusiveButtons.spec.qml diff --git a/tests/ui/modules/Common/Dialog/ConfirmDialog.spec.qml b/tests/ui/modules/Common/Dialog/ConfirmDialog.spec.qml index 368bf346c..702d17fe4 100644 --- a/tests/ui/modules/Common/Dialog/ConfirmDialog.spec.qml +++ b/tests/ui/modules/Common/Dialog/ConfirmDialog.spec.qml @@ -8,20 +8,15 @@ TestCase { name: 'ConfirmDialogTests' - function createDialog () { - var component = Qt.createComponent( - './ConfirmDialog.qml' - ) + Component { + id: builder - if (component.status !== Component.Ready) { - if(component.status === Component.Error) { - fail('Error:' + component.errorString()) - } else { - fail('Dialog not ready.') - } - } + ConfirmDialog {} + } - var dialog = component.createObject(testCase) + function buildConfirmDialog () { + var dialog = builder.createObject(testCase) + verify(dialog) dialog.closing.connect(dialog.destroy.bind(dialog)) return dialog } @@ -34,7 +29,7 @@ TestCase { } function test_exitStatusViaButtons (data) { - var dialog = createDialog() + var dialog = buildConfirmDialog() dialog.exitStatus.connect(function (status) { compare(status, data.expectedStatus) @@ -44,7 +39,7 @@ TestCase { } function test_exitStatusViaClose () { - var dialog = createDialog() + var dialog = buildConfirmDialog() dialog.exitStatus.connect(function (status) { compare(status, 0) diff --git a/tests/ui/modules/Common/Form/ExclusiveButtons.qml b/tests/ui/modules/Common/Form/ExclusiveButtons.qml index 9780a1849..0115a6d65 100644 --- a/tests/ui/modules/Common/Form/ExclusiveButtons.qml +++ b/tests/ui/modules/Common/Form/ExclusiveButtons.qml @@ -9,7 +9,7 @@ Row { property var texts - property int _selectedButton: 0 + property int selectedButton: 0 signal clicked (int button) @@ -20,7 +20,7 @@ Row { SmallButton { anchors.verticalCenter: parent.verticalCenter - backgroundColor: _selectedButton === index + backgroundColor: selectedButton === index ? ExclusiveButtonsStyle.button.color.selected : (down ? ExclusiveButtonsStyle.button.color.pressed @@ -32,8 +32,8 @@ Row { text: modelData onClicked: { - if (_selectedButton !== index) { - _selectedButton = index + if (selectedButton !== index) { + selectedButton = index item.clicked(index) } } diff --git a/tests/ui/modules/Common/Form/ExclusiveButtons.spec.qml b/tests/ui/modules/Common/Form/ExclusiveButtons.spec.qml new file mode 100644 index 000000000..6ab9676f1 --- /dev/null +++ b/tests/ui/modules/Common/Form/ExclusiveButtons.spec.qml @@ -0,0 +1,80 @@ +import QtQuick 2.7 +import QtTest 1.1 + +// =================================================================== + +Item { + id: root + + Component { + id: builder + + Item { + ExclusiveButtons { + id: exclusiveButtons + + texts: [ + qsTr('A'), + qsTr('B'), + qsTr('C'), + qsTr('D'), + qsTr('E') + ] + } + + SignalSpy { + id: spy + + signalName: 'clicked' + target: exclusiveButtons + } + } + } + + function buildExclusiveButtons (defaultSelectedButton) { + var container = builder.createObject(root) + testCase.verify(container) + container.data[0].selectedButton = defaultSelectedButton + return container + } + + TestCase { + id: testCase + + name: 'ExclusiveButtonsTests' + when: windowShown + + function test_signals_data () { + return [ + { defaultSelectedButton: 0, buttonToClick: 2 }, + { defaultSelectedButton: 1, buttonToClick: 4 }, + { defaultSelectedButton: 3, buttonToClick: 1 }, + { defaultSelectedButton: 4, buttonToClick: 0 } + ] + } + + function test_signals (data) { + var container = buildExclusiveButtons(data.defaultSelectedButton) + var spy = container.data[1] + var exclusiveButtons = container.data[0] + + var buttonToClick = data.buttonToClick + + // Test default selected button. + compare(exclusiveButtons.selectedButton, data.defaultSelectedButton) + + // Test a click to change the selected button. + mouseClick(exclusiveButtons.data[buttonToClick]) + spy.wait(100) + compare(spy.signalArguments[0][0], buttonToClick) + compare(exclusiveButtons.selectedButton, buttonToClick) + + // No signal must be emitted. + mouseClick(exclusiveButtons.data[buttonToClick]) + wait(100) + compare(spy.count, 1) + + container.destroy() + } + } +} diff --git a/tests/ui/modules/Common/InvertedMouseArea.qml b/tests/ui/modules/Common/InvertedMouseArea.qml index 93fa60426..c1e2f5d1d 100644 --- a/tests/ui/modules/Common/InvertedMouseArea.qml +++ b/tests/ui/modules/Common/InvertedMouseArea.qml @@ -16,7 +16,7 @@ Item { function _createMouseArea () { if (_mouseArea == null) { - _mouseArea = builder.createObject(this) + _mouseArea = builder.createObject() } _mouseArea.parent = (function () { diff --git a/tests/ui/modules/Common/Popup/DropDownMenu.qml b/tests/ui/modules/Common/Popup/DropDownMenu.qml index 44b1262a7..e5a416b8d 100644 --- a/tests/ui/modules/Common/Popup/DropDownMenu.qml +++ b/tests/ui/modules/Common/Popup/DropDownMenu.qml @@ -38,7 +38,7 @@ Rectangle { layer { enabled: true - effect: PopupShadow { } + effect: PopupShadow {} } } } diff --git a/tests/ui/modules/Common/View/ScrollableListView.qml b/tests/ui/modules/Common/View/ScrollableListView.qml index 9c1157200..d400b71fe 100644 --- a/tests/ui/modules/Common/View/ScrollableListView.qml +++ b/tests/ui/modules/Common/View/ScrollableListView.qml @@ -6,7 +6,7 @@ import Common 1.0 // =================================================================== ListView { - ScrollBar.vertical: ForceScrollBar { } + ScrollBar.vertical: ForceScrollBar {} boundsBehavior: Flickable.StopAtBounds clip: true spacing: 0 diff --git a/tests/ui/views/MainWindow/Contact.qml b/tests/ui/views/MainWindow/Contact.qml index d8a600c93..fc82f0e91 100644 --- a/tests/ui/views/MainWindow/Contact.qml +++ b/tests/ui/views/MainWindow/Contact.qml @@ -68,7 +68,7 @@ ColumnLayout { Flickable { Layout.fillHeight: true Layout.fillWidth: true - ScrollBar.vertical: ForceScrollBar { } + ScrollBar.vertical: ForceScrollBar {} boundsBehavior: Flickable.StopAtBounds clip: true contentHeight: content.height