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