status_indicator_widget_unit_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2020 Pilz GmbH & Co. KG
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 import os
18 import sys
19 import unittest
20 
21 import rospkg
22 import rospy
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
27 
29  GREEN, RED, PilzStatusIndicatorWidget)
30 
31 
32 class TestablePilzStatusIndicatorWidget(PilzStatusIndicatorWidget):
33  """
34  Needed to skip TestStatusIndicatorWidget.__init__, especially the super
35  call within. Was not able to mock. Better solution welcome.
36  """
37 
38  def __init__(self, serial_number):
39  pass
40 
41 
42 class TestStatusIndicatorWidget(unittest.TestCase):
43  """
44  These tests ensure that status changes for the widget make the correct
45  calls internally to display these information correctly.
46  """
47 
48  def setUp(self):
49  """initializing a widget to be tested."""
51 
53  """Testing whether the LED for the ROS status is turning correctly
54  red and green."""
55  # Preparing mocks within the widget
56  self.psi.labelROS = MagicMock()
57  self.psi.labelROS.setStyleSheet = MagicMock()
58 
59  # Testing if light turns green for status True
60  self.psi.set_ROS_status(True)
61  self.psi.labelROS.setStyleSheet.assert_called_with(
62  "QLabel { background-color: %s }" % GREEN)
63 
64  # Testing if light turns red for status False
65  self.psi.set_ROS_status(False)
66  self.psi.labelROS.setStyleSheet.assert_called_with(
67  "QLabel { background-color: %s }" % RED)
68 
69  def test_set_HW_status(self):
70  """Testing whether the LED for the HW status is turning correctly
71  red and green."""
72  # Preparing mocks within the widget
73  self.psi.labelHW = MagicMock()
74  self.psi.labelHW.setStyleSheet = MagicMock()
75 
76  # Testing if light turns green for status True
77  self.psi.set_HW_status(True)
78  self.psi.labelHW.setStyleSheet.assert_called_with(
79  "QLabel { background-color: %s }" % GREEN)
80 
81  # Testing if light turns red for status False
82  self.psi.set_HW_status(False)
83  self.psi.labelHW.setStyleSheet.assert_called_with(
84  "QLabel { background-color: %s }" % RED)
85 
87  """Testing whether setting the operation mode loads the correct icon
88  and displays the right text."""
89  # Preparing mocks within the widget
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()
95 
96  # Operation mode Auto should load the correct icon and display the
97  # correct text.
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")
104 
105  # Operation mode T1 should load the correct icon and display the
106  # correct text.
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")
113 
114  # Operation mode T2 should load the correct icon and display the
115  # correct text.
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")
122 
123  # Operation mode Unknown should load the correct icon and display the
124  # correct text.
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")
131 
132  def test_set_speed(self):
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."""
136  # Preparing mocks within the widget
137  self.psi.barSpeed = MagicMock()
138  self.psi.barSpeed.setValue = MagicMock()
139 
140  # 0 speed should be displayed as 0%
141  self.psi.set_speed(0)
142  self.psi.barSpeed.setValue.assert_called_with(0)
143 
144  # 0.5 speed should be displayed as 50%
145  self.psi.set_speed(.5)
146  self.psi.barSpeed.setValue.assert_called_with(50)
147 
148  # 1 speed should be displayed as 100%
149  self.psi.set_speed(1)
150  self.psi.barSpeed.setValue.assert_called_with(100)
151 
152  # speed values below 0 should be displayed as 0%
153  self.psi.set_speed(-1)
154  self.psi.barSpeed.setValue.assert_called_with(0)
155 
156  # speed values above 1 should be displayed as 100%
157  self.psi.set_speed(2)
158  self.psi.barSpeed.setValue.assert_called_with(100)
159 
160 
161 if __name__ == '__main__':
162  import rosunit
163  rosunit.unitrun('pilz_status_indicator_rqt',
164  'test_status_indicator_widget', TestStatusIndicatorWidget)


pilz_status_indicator_rqt
Author(s):
autogenerated on Tue Feb 2 2021 03:50:27