| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2022 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import pytest |
| 26 |
|
| 27 |
from pymeasure.display.Qt import QtCore |
| 28 |
from pymeasure.experiment import Procedure, BooleanParameter, Parameter, FloatParameter, \ |
| 29 |
IntegerParameter, ListParameter |
| 30 |
from pymeasure.display.widgets import InputsWidget |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize( |
| 34 |
"hide_groups,exp_visible,exp_enabled", [ |
| 35 |
(True, False, True), |
| 36 |
(False, True, False), |
| 37 |
] |
| 38 |
) |
| 39 |
def test_input_toggling(qtbot, hide_groups, exp_visible, exp_enabled): |
| 40 |
"""Test whether the basic toggling works. |
| 41 |
|
| 42 |
This test is run for both hiding (hide_groups=True) and disabling (hide_groups=False) the |
| 43 |
inputs. |
| 44 |
""" |
| 45 |
|
| 46 |
class TestProcedure(Procedure): |
| 47 |
toggle_par = BooleanParameter('toggle', default=True) |
| 48 |
x = Parameter('X', default='value', group_by='toggle_par') |
| 49 |
|
| 50 |
wdg = InputsWidget(TestProcedure, inputs=('toggle_par', 'x'), hide_groups=hide_groups) |
| 51 |
qtbot.addWidget(wdg) |
| 52 |
|
| 53 |
assert wdg.toggle_par.isChecked() is True |
| 54 |
assert wdg.x.isVisibleTo(wdg) is True |
| 55 |
assert wdg.x.isEnabled() is True |
| 56 |
|
| •57 |
qtbot.mouseClick(wdg.toggle_par, QtCore.Qt.MouseButton.LeftButton, |
| 58 |
pos=QtCore.QPoint(2, int(wdg.toggle_par.height()/2))) |
| 59 |
|
| 60 |
assert wdg.toggle_par.isChecked() is False |
| 61 |
assert wdg.x.isVisibleTo(wdg) is exp_visible |
| 62 |
assert wdg.x.isEnabled() is exp_enabled |
| 63 |
|
| •64 |
qtbot.mouseClick(wdg.toggle_par, QtCore.Qt.MouseButton.LeftButton, |
| 65 |
pos=QtCore.QPoint(2, int(wdg.toggle_par.height()/2))) |
| 66 |
|
| 67 |
assert wdg.toggle_par.isChecked() is True |
| 68 |
assert wdg.x.isVisibleTo(wdg) is True |
| 69 |
assert wdg.x.isEnabled() is True |
| 70 |
|
| 71 |
|
| 72 |
def test_input_toggling_start_hidden(qtbot): |
| 73 |
"""Test whether the basic toggling works. |
| 74 |
|
| 75 |
This test is run for both hiding (hide_groups=True) and disabling (hide_groups=False) the |
| 76 |
inputs. |
| 77 |
""" |
| 78 |
|
| 79 |
class TestProcedure(Procedure): |
| 80 |
toggle_par = BooleanParameter('toggle', default=False) |
| 81 |
x = Parameter('X', default='value', group_by='toggle_par') |
| 82 |
|
| 83 |
wdg = InputsWidget(TestProcedure, inputs=('toggle_par', 'x')) |
| 84 |
qtbot.addWidget(wdg) |
| 85 |
|
| 86 |
assert wdg.toggle_par.isChecked() is False |
| 87 |
assert wdg.x.isVisibleTo(wdg) is False |
| 88 |
|
| •89 |
qtbot.mouseClick(wdg.toggle_par, QtCore.Qt.MouseButton.LeftButton, |
| 90 |
pos=QtCore.QPoint(2, int(wdg.toggle_par.height()/2))) |
| 91 |
|
| 92 |
assert wdg.toggle_par.isChecked() is True |
| 93 |
assert wdg.x.isVisibleTo(wdg) is True |
| 94 |
|
| 95 |
|
| 96 |
def test_input_toggling_multiple_conditions(qtbot): |
| 97 |
"""Test if multiple conditions are handled correctly together.""" |
| 98 |
|
| 99 |
class TestProcedure(Procedure): |
| 100 |
toggle_par1 = BooleanParameter('toggle', default=True) |
| 101 |
toggle_par2 = BooleanParameter('toggle', default=False) |
| 102 |
x = Parameter('X', default='value', group_by=['toggle_par1', 'toggle_par2'], |
| 103 |
group_condition=[True, False]) |
| 104 |
|
| 105 |
wdg = InputsWidget(TestProcedure, inputs=('toggle_par1', 'toggle_par2', 'x')) |
| 106 |
qtbot.addWidget(wdg) |
| 107 |
|
| 108 |
assert wdg.toggle_par1.isChecked() is True |
| 109 |
assert wdg.toggle_par2.isChecked() is False |
| 110 |
assert wdg.x.isVisibleTo(wdg) is True |
| 111 |
|
| •112 |
qtbot.mouseClick(wdg.toggle_par1, QtCore.Qt.MouseButton.LeftButton, |
| 113 |
pos=QtCore.QPoint(2, int(wdg.toggle_par1.height()/2))) |
| 114 |
|
| 115 |
assert wdg.toggle_par1.isChecked() is False |
| 116 |
assert wdg.toggle_par2.isChecked() is False |
| 117 |
assert wdg.x.isVisibleTo(wdg) is False |
| 118 |
|
| •119 |
qtbot.mouseClick(wdg.toggle_par2, QtCore.Qt.MouseButton.LeftButton, |
| 120 |
pos=QtCore.QPoint(2, int(wdg.toggle_par2.height()/2))) |
| 121 |
|
| 122 |
assert wdg.toggle_par1.isChecked() is False |
| 123 |
assert wdg.toggle_par2.isChecked() is True |
| 124 |
assert wdg.x.isVisibleTo(wdg) is False |
| 125 |
|
| •126 |
qtbot.mouseClick(wdg.toggle_par1, QtCore.Qt.MouseButton.LeftButton, |
| 127 |
pos=QtCore.QPoint(2, int(wdg.toggle_par1.height()/2))) |
| 128 |
|
| 129 |
assert wdg.toggle_par1.isChecked() is True |
| 130 |
assert wdg.toggle_par2.isChecked() is True |
| 131 |
assert wdg.x.isVisibleTo(wdg) is False |
| 132 |
|
| •133 |
qtbot.mouseClick(wdg.toggle_par2, QtCore.Qt.MouseButton.LeftButton, |
| 134 |
pos=QtCore.QPoint(2, int(wdg.toggle_par2.height()/2))) |
| 135 |
|
| 136 |
assert wdg.toggle_par1.isChecked() is True |
| 137 |
assert wdg.toggle_par2.isChecked() is False |
| 138 |
assert wdg.x.isVisibleTo(wdg) is True |
| 139 |
|
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
"condition", [ |
| 143 |
True, |
| 144 |
False, |
| 145 |
] |
| 146 |
) |
| 147 |
def test_input_toggling_boolean(qtbot, condition): |
| 148 |
"""Test if a boolean conditions are handled correctly.""" |
| 149 |
|
| 150 |
class TestProcedure(Procedure): |
| 151 |
toggle_par = BooleanParameter('toggle', default=True) |
| 152 |
x = Parameter('X', default='value', group_by='toggle_par', group_condition=condition) |
| 153 |
|
| 154 |
wdg = InputsWidget(TestProcedure, inputs=('toggle_par', 'x')) |
| 155 |
qtbot.addWidget(wdg) |
| 156 |
|
| 157 |
assert wdg.toggle_par.isChecked() is True |
| 158 |
assert wdg.x.isVisibleTo(wdg) is condition |
| 159 |
|
| •160 |
qtbot.mouseClick(wdg.toggle_par, QtCore.Qt.MouseButton.LeftButton, |
| 161 |
pos=QtCore.QPoint(2, int(wdg.toggle_par.height()/2))) |
| 162 |
|
| 163 |
assert wdg.toggle_par.isChecked() is False |
| 164 |
assert wdg.x.isVisibleTo(wdg) is not condition |
| 165 |
|
| •166 |
qtbot.mouseClick(wdg.toggle_par, QtCore.Qt.MouseButton.LeftButton, |
| 167 |
pos=QtCore.QPoint(2, int(wdg.toggle_par.height()/2))) |
| 168 |
|
| 169 |
assert wdg.toggle_par.isChecked() is True |
| 170 |
assert wdg.x.isVisibleTo(wdg) is condition |
| 171 |
|
| 172 |
|
| 173 |
@pytest.mark.parametrize( |
| 174 |
"partype,default,condition,kwargs", [ |
| 175 |
(Parameter, "default_value", "condition_value", {}), |
| 176 |
(FloatParameter, 0.7, 5.89, {}), |
| 177 |
(IntegerParameter, 3, 5, {}), |
| 178 |
(BooleanParameter, False, True, {}), |
| 179 |
(ListParameter, "default", "condition", {"choices": ["default", "condition"]}), |
| 180 |
] |
| 181 |
) |
| 182 |
def test_input_toggling_various_inputs(qtbot, partype, default, condition, kwargs): |
| 183 |
"""Test if various input-types are handled correctly for toggling.""" |
| 184 |
|
| 185 |
class TestProcedure(Procedure): |
| 186 |
toggle_par = partype('toggle', default=default, **kwargs) |
| 187 |
x = Parameter('X', default='value', group_by='toggle_par', group_condition=condition) |
| 188 |
|
| 189 |
wdg = InputsWidget(TestProcedure, inputs=('toggle_par', 'x')) |
| 190 |
qtbot.addWidget(wdg) |
| 191 |
|
| 192 |
assert wdg.x.isVisibleTo(wdg) is False |
| 193 |
|
| 194 |
wdg.toggle_par.setValue(condition) |
| 195 |
|
| 196 |
assert wdg.x.isVisibleTo(wdg) is True |
| 197 |
|
| 198 |
wdg.toggle_par.setValue(default) |
| 199 |
|
| 200 |
assert wdg.x.isVisibleTo(wdg) is False |
| 201 |
|
| 202 |
|
| 203 |
def test_input_toggling_lambda_condition(qtbot): |
| 204 |
"""Test if a lambda-function is handled for toggling.""" |
| 205 |
|
| 206 |
class TestProcedure(Procedure): |
| 207 |
toggle_par = IntegerParameter('toggle', default=100) |
| 208 |
x = Parameter('X', default='value', group_by='toggle_par', |
| 209 |
group_condition=lambda v: 50 < v < 90) |
| 210 |
|
| 211 |
wdg = InputsWidget(TestProcedure, inputs=('toggle_par', 'x')) |
| 212 |
qtbot.addWidget(wdg) |
| 213 |
|
| 214 |
assert wdg.x.isVisibleTo(wdg) is False |
| 215 |
|
| 216 |
wdg.toggle_par.setValue(80) |
| 217 |
|
| 218 |
assert wdg.x.isVisibleTo(wdg) is True |
| 219 |
|
| 220 |
wdg.toggle_par.setValue(40) |
| 221 |
|
| 222 |
assert wdg.x.isVisibleTo(wdg) is False |