unitTest.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import unittest
  2. from ControlPanelUI import ControlPanelUI
  3. import tkinter as tk
  4. class TestControlPanelUI(unittest.TestCase):
  5. def setUp(self):
  6. self.root = tk.Tk()
  7. self.ui = ControlPanelUI(self.root)
  8. def tearDown(self):
  9. self.root.destroy()
  10. def test_refresh_status(self):
  11. # 模拟更新电机状态
  12. self.ui.Core.motor_is_running = True
  13. self.ui.Core.motor_position = 100
  14. self.ui.Core.motor_angle = 45
  15. self.ui.Core.motor_velocity = 50
  16. self.ui.Core.motor_mode = "模式 1"
  17. self.ui.refresh_status()
  18. # 验证标签文本是否更新
  19. self.assertEqual(self.ui.status_label.cget("text"), "状态: 使能")
  20. self.assertEqual(self.ui.position_label.cget("text"), "位置: 100")
  21. self.assertEqual(self.ui.motor_angle_label.cget("text"), "水平: 45°")
  22. self.assertEqual(self.ui.velocity_label.cget("text"), "速度: 50 °/s")
  23. self.assertEqual(self.ui.mode_label.cget("text"), "模式: 模式 1")
  24. def test_update_ui_labels(self):
  25. # 模拟电机状态
  26. self.ui.Core.motor_is_running = False
  27. self.ui.Core.motor_position = 200
  28. self.ui.Core.motor_angle = 30
  29. self.ui.Core.motor_velocity = 30
  30. self.ui.Core.motor_mode = "模式 2"
  31. self.ui.update_ui_labels()
  32. # 验证标签文本是否更新
  33. self.assertEqual(self.ui.status_label.cget("text"), "状态: 失能")
  34. self.assertEqual(self.ui.position_label.cget("text"), "位置: 200")
  35. self.assertEqual(self.ui.motor_angle_label.cget("text"), "水平: 30°")
  36. self.assertEqual(self.ui.velocity_label.cget("text"), "速度: 30 °/s")
  37. self.assertEqual(self.ui.mode_label.cget("text"), "模式: 模式 2")
  38. def test_schedule_next_refresh(self):
  39. # 验证定时刷新线程是否启动
  40. self.ui.schedule_next_refresh()
  41. self.assertIsNotNone(self.ui.refresh_thread)
  42. def test_create_joystick(self):
  43. # 验证摇杆画布和相关元素是否创建
  44. self.assertIsNotNone(self.ui.joystick_canvas)
  45. self.assertIsNotNone(self.ui.joystick)
  46. self.assertIsNotNone(self.ui.angle_label)
  47. def test_update_joystick_position(self):
  48. # 模拟鼠标移动事件
  49. event = tk.Event()
  50. event.x = 150
  51. event.y = 150
  52. self.ui.update_joystick_position(event)
  53. # 验证摇杆位置是否更新
  54. joystick_coords = self.ui.joystick_canvas.coords(self.ui.joystick)
  55. expected_x = self.ui.center[0] + self.ui.circle_radius * math.cos(math.radians(45))
  56. expected_y = self.ui.center[1] - self.ui.circle_radius * math.sin(math.radians(45))
  57. self.assertAlmostEqual(joystick_coords[0], expected_x - self.ui.joystick_radius, places=2)
  58. self.assertAlmostEqual(joystick_coords[1], expected_y - self.ui.joystick_radius, places=2)
  59. self.assertAlmostEqual(joystick_coords[2], expected_x + self.ui.joystick_radius, places=2)
  60. self.assertAlmostEqual(joystick_coords[3], expected_y + self.ui.joystick_radius, places=2)
  61. def test_on_joystick_release(self):
  62. # 模拟摇杆释放事件
  63. event = tk.Event()
  64. event.x = 150
  65. event.y = 150
  66. self.ui.on_joystick_release(event)
  67. # 验证电机位置是否更新
  68. self.assertEqual(self.ui.Core.motor.target_position, 1) # 根据具体计算逻辑确定预期值
  69. def test_update_motor_position(self):
  70. # 模拟角度
  71. angle = 90
  72. self.ui.update_motor_position(angle)
  73. # 验证电机位置是否更新
  74. self.assertEqual(self.ui.Core.motor.target_position, 2) # 根据具体计算逻辑确定预期值
  75. def test_setup_running_ui(self):
  76. # 验证运行界面的控件是否创建
  77. self.assertIsNotNone(self.ui.motor_velocity_entry)
  78. self.assertIsNotNone(self.ui.diff_entry)
  79. self.assertIsNotNone(self.ui.increase_pitch_button)
  80. self.assertIsNotNone(self.ui.decrease_pitch_button)
  81. self.assertIsNotNone(self.ui.zero_pitch_button)
  82. self.assertIsNotNone(self.ui.start_button)
  83. self.assertIsNotNone(self.ui.stop_button)
  84. self.assertIsNotNone(self.ui.motor_zero_button)
  85. self.assertIsNotNone(self.ui.pitch_motor_zero_button)
  86. def test_on_tab_change(self):
  87. # 模拟标签页切换事件
  88. event = tk.Event()
  89. event.widget.tab('current')['text'] = '遥控'
  90. self.ui.on_tab_change(event)
  91. # 验证是否正确处理位置控制模式切换
  92. self.assertTrue(self.ui.joystick_active)
  93. def test_center_window(self):
  94. # 验证窗口是否居中
  95. width = 400
  96. height = 300
  97. self.ui.center_window(width, height)
  98. # 获取窗口实际位置和大小
  99. x, y, w, h = self.root.winfo_geometry().split('+')
  100. # 验证窗口是否居中
  101. self.assertEqual(int(x), (self.root.winfo_screenwidth() - width) // 2)
  102. self.assertEqual(int(y), (self.root.winfo_screenheight() - height) // 2)
  103. def test_setup_segment_ui(self):
  104. # 验证多段运行界面的控件是否创建
  105. self.assertIsNotNone(self.ui.step_entry)
  106. self.assertIsNotNone(self.ui.stop_delay_entry)
  107. self.assertIsNotNone(self.ui.speed_entry)
  108. self.assertIsNotNone(self.ui.acceleration_entry)
  109. self.assertIsNotNone(self.ui.continuous_check)
  110. self.assertIsNotNone(self.ui.download_button)
  111. self.assertIsNotNone(self.ui.start_button)
  112. self.assertIsNotNone(self.ui.stop_button)
  113. def test_download_setting(self):
  114. # 模拟设置参数
  115. self.ui.step_entry.insert(0, "60")
  116. self.ui.acceleration_entry.insert(0, "100")
  117. self.ui.stop_delay_entry.insert(0, "500")
  118. self.ui.download_setting()
  119. # 验证是否正确下载配置
  120. # 可以根据具体的下载逻辑进行进一步的验证
  121. def test_start_segment_button(self):
  122. # 模拟点击启动按钮
  123. self.ui.start_segment_button()
  124. # 验证是否启动多段运行线程
  125. # 可以根据具体的多段运行逻辑进行进一步的验证
  126. def test_stop_motor_button(self):
  127. # 模拟点击停止按钮
  128. self.ui.stop_motor_button()
  129. # 验证是否启动停止电机线程
  130. # 可以根据具体的停止电机逻辑进行进一步的验证
  131. def test_start_motor_button(self):
  132. # 模拟输入有效的速度值
  133. self.ui.motor_velocity_entry.insert(0, "120")
  134. self.ui.start_motor_button()
  135. # 验证是否启动电机线程
  136. # 可以根据具体的启动电机逻辑进行进一步的验证
  137. def test_motor_home_button(self):
  138. # 模拟点击转台回零按钮
  139. self.ui.motor_home_button()
  140. # 验证是否启动转台回零线程
  141. # 可以根据具体的转台回零逻辑进行进一步的验证
  142. def test_pitch_motor_home_button(self):
  143. # 模拟点击俯仰回零按钮
  144. self.ui.pitch_motor_home_button()
  145. # 验证是否启动俯仰回零线程
  146. # 可以根据具体的俯仰回零逻辑进行进一步的验证
  147. def test_increase_pitch_button(self):
  148. # 模拟点击增加俯仰角按钮
  149. self.ui.increase_pitch_button()
  150. # 验证俯仰角是否增加
  151. # 可以根据具体的增加俯仰角逻辑进行进一步的验证
  152. def test_decrease_pitch_button(self):
  153. # 模拟点击减少俯仰角按钮
  154. self.ui.decrease_pitch_button()
  155. # 验证俯仰角是否减少
  156. # 可以根据具体的减少俯仰角逻辑进行进一步的验证
  157. def test_zero_pitch_button(self):
  158. # 模拟点击回到零度按钮
  159. self.ui.zero_pitch_button()
  160. # 验证俯仰角是否回到零度
  161. # 可以根据具体的回到零度逻辑进行进一步的验证
  162. if __name__ == '__main__':
  163. unittest.main()