23 from mock
import ANY, MagicMock, create_autospec
24 from prbt_hardware_support.msg
import OperationModes
25 from PyQt5.QtGui
import QPixmap
26 from python_qt_binding.QtWidgets
import QApplication, QMainWindow
29 GREEN, RED, PilzStatusIndicatorWidget)
34 Needed to skip TestStatusIndicatorWidget.__init__, especially the super 35 call within. Was not able to mock. Better solution welcome. 42 class TestStatusIndicatorWidget(unittest.TestCase):
44 These tests ensure that status changes for the widget make the correct 45 calls internally to display these information correctly. 49 """initializing a widget to be tested.""" 53 """Testing whether the LED for the ROS status is turning correctly 56 self.psi.labelROS = MagicMock()
57 self.psi.labelROS.setStyleSheet = MagicMock()
60 self.psi.set_ROS_status(
True)
61 self.psi.labelROS.setStyleSheet.assert_called_with(
62 "QLabel { background-color: %s }" % GREEN)
65 self.psi.set_ROS_status(
False)
66 self.psi.labelROS.setStyleSheet.assert_called_with(
67 "QLabel { background-color: %s }" % RED)
70 """Testing whether the LED for the HW status is turning correctly 73 self.psi.labelHW = MagicMock()
74 self.psi.labelHW.setStyleSheet = MagicMock()
77 self.psi.set_HW_status(
True)
78 self.psi.labelHW.setStyleSheet.assert_called_with(
79 "QLabel { background-color: %s }" % GREEN)
82 self.psi.set_HW_status(
False)
83 self.psi.labelHW.setStyleSheet.assert_called_with(
84 "QLabel { background-color: %s }" % RED)
87 """Testing whether setting the operation mode loads the correct icon 88 and displays the right text.""" 90 Mock_qpixmap = create_autospec(QPixmap)
91 self.psi.labelOM = MagicMock()
92 self.psi.labelOM_text = MagicMock()
93 self.psi.labelOM.setPixmap = MagicMock()
94 self.psi.labelOM_text.setText = MagicMock()
98 icon_path_auto = os.path.join(rospkg.RosPack().get_path(
99 'pilz_status_indicator_rqt'),
'resource',
'auto.png')
100 self.psi.set_operation_mode(OperationModes.AUTO, Mock_qpixmap)
101 Mock_qpixmap.assert_called_with(icon_path_auto)
102 self.psi.labelOM.setPixmap.assert_called_with(ANY)
103 self.psi.labelOM_text.setText.assert_called_with(
"Auto")
107 icon_path_t1 = os.path.join(rospkg.RosPack().get_path(
108 'pilz_status_indicator_rqt'),
'resource',
't1.png')
109 self.psi.set_operation_mode(OperationModes.T1, Mock_qpixmap)
110 Mock_qpixmap.assert_called_with(icon_path_t1)
111 self.psi.labelOM.setPixmap.assert_called_with(ANY)
112 self.psi.labelOM_text.setText.assert_called_with(
"T1")
116 icon_path_t2 = os.path.join(rospkg.RosPack().get_path(
117 'pilz_status_indicator_rqt'),
'resource',
't2.png')
118 self.psi.set_operation_mode(OperationModes.T2, Mock_qpixmap)
119 Mock_qpixmap.assert_called_with(icon_path_t2)
120 self.psi.labelOM.setPixmap.assert_called_with(ANY)
121 self.psi.labelOM_text.setText.assert_called_with(
"T2")
125 icon_path_unknown = os.path.join(rospkg.RosPack().get_path(
126 'pilz_status_indicator_rqt'),
'resource',
'unknown.png')
127 self.psi.set_operation_mode(OperationModes.UNKNOWN, Mock_qpixmap)
128 Mock_qpixmap.assert_called_with(icon_path_unknown)
129 self.psi.labelOM.setPixmap.assert_called_with(ANY)
130 self.psi.labelOM_text.setText.assert_called_with(
"Unknown")
133 """Testing whether the speed override values are interpreted in the 134 widget correctly, both for the conversion to percentage and the 135 capping of input values between 0 and 1.""" 137 self.psi.barSpeed = MagicMock()
138 self.psi.barSpeed.setValue = MagicMock()
141 self.psi.set_speed(0)
142 self.psi.barSpeed.setValue.assert_called_with(0)
145 self.psi.set_speed(.5)
146 self.psi.barSpeed.setValue.assert_called_with(50)
149 self.psi.set_speed(1)
150 self.psi.barSpeed.setValue.assert_called_with(100)
153 self.psi.set_speed(-1)
154 self.psi.barSpeed.setValue.assert_called_with(0)
157 self.psi.set_speed(2)
158 self.psi.barSpeed.setValue.assert_called_with(100)
161 if __name__ ==
'__main__':
163 rosunit.unitrun(
'pilz_status_indicator_rqt',
164 'test_status_indicator_widget', TestStatusIndicatorWidget)