123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- import unittest
- from ControlPanelUI import ControlPanelUI
- import tkinter as tk
- class TestControlPanelUI(unittest.TestCase):
- def setUp(self):
- self.root = tk.Tk()
- self.ui = ControlPanelUI(self.root)
- def tearDown(self):
- self.root.destroy()
- def test_refresh_status(self):
- # 模拟更新电机状态
- self.ui.Core.motor_is_running = True
- self.ui.Core.motor_position = 100
- self.ui.Core.motor_angle = 45
- self.ui.Core.motor_velocity = 50
- self.ui.Core.motor_mode = "模式 1"
- self.ui.refresh_status()
- # 验证标签文本是否更新
- self.assertEqual(self.ui.status_label.cget("text"), "状态: 使能")
- self.assertEqual(self.ui.position_label.cget("text"), "位置: 100")
- self.assertEqual(self.ui.motor_angle_label.cget("text"), "水平: 45°")
- self.assertEqual(self.ui.velocity_label.cget("text"), "速度: 50 °/s")
- self.assertEqual(self.ui.mode_label.cget("text"), "模式: 模式 1")
- def test_update_ui_labels(self):
- # 模拟电机状态
- self.ui.Core.motor_is_running = False
- self.ui.Core.motor_position = 200
- self.ui.Core.motor_angle = 30
- self.ui.Core.motor_velocity = 30
- self.ui.Core.motor_mode = "模式 2"
- self.ui.update_ui_labels()
- # 验证标签文本是否更新
- self.assertEqual(self.ui.status_label.cget("text"), "状态: 失能")
- self.assertEqual(self.ui.position_label.cget("text"), "位置: 200")
- self.assertEqual(self.ui.motor_angle_label.cget("text"), "水平: 30°")
- self.assertEqual(self.ui.velocity_label.cget("text"), "速度: 30 °/s")
- self.assertEqual(self.ui.mode_label.cget("text"), "模式: 模式 2")
- def test_schedule_next_refresh(self):
- # 验证定时刷新线程是否启动
- self.ui.schedule_next_refresh()
- self.assertIsNotNone(self.ui.refresh_thread)
- def test_create_joystick(self):
- # 验证摇杆画布和相关元素是否创建
- self.assertIsNotNone(self.ui.joystick_canvas)
- self.assertIsNotNone(self.ui.joystick)
- self.assertIsNotNone(self.ui.angle_label)
- def test_update_joystick_position(self):
- # 模拟鼠标移动事件
- event = tk.Event()
- event.x = 150
- event.y = 150
- self.ui.update_joystick_position(event)
- # 验证摇杆位置是否更新
- joystick_coords = self.ui.joystick_canvas.coords(self.ui.joystick)
- expected_x = self.ui.center[0] + self.ui.circle_radius * math.cos(math.radians(45))
- expected_y = self.ui.center[1] - self.ui.circle_radius * math.sin(math.radians(45))
- self.assertAlmostEqual(joystick_coords[0], expected_x - self.ui.joystick_radius, places=2)
- self.assertAlmostEqual(joystick_coords[1], expected_y - self.ui.joystick_radius, places=2)
- self.assertAlmostEqual(joystick_coords[2], expected_x + self.ui.joystick_radius, places=2)
- self.assertAlmostEqual(joystick_coords[3], expected_y + self.ui.joystick_radius, places=2)
- def test_on_joystick_release(self):
- # 模拟摇杆释放事件
- event = tk.Event()
- event.x = 150
- event.y = 150
- self.ui.on_joystick_release(event)
- # 验证电机位置是否更新
- self.assertEqual(self.ui.Core.motor.target_position, 1) # 根据具体计算逻辑确定预期值
- def test_update_motor_position(self):
- # 模拟角度
- angle = 90
- self.ui.update_motor_position(angle)
- # 验证电机位置是否更新
- self.assertEqual(self.ui.Core.motor.target_position, 2) # 根据具体计算逻辑确定预期值
- def test_setup_running_ui(self):
- # 验证运行界面的控件是否创建
- self.assertIsNotNone(self.ui.motor_velocity_entry)
- self.assertIsNotNone(self.ui.diff_entry)
- self.assertIsNotNone(self.ui.increase_pitch_button)
- self.assertIsNotNone(self.ui.decrease_pitch_button)
- self.assertIsNotNone(self.ui.zero_pitch_button)
- self.assertIsNotNone(self.ui.start_button)
- self.assertIsNotNone(self.ui.stop_button)
- self.assertIsNotNone(self.ui.motor_zero_button)
- self.assertIsNotNone(self.ui.pitch_motor_zero_button)
- def test_on_tab_change(self):
- # 模拟标签页切换事件
- event = tk.Event()
- event.widget.tab('current')['text'] = '遥控'
- self.ui.on_tab_change(event)
- # 验证是否正确处理位置控制模式切换
- self.assertTrue(self.ui.joystick_active)
- def test_center_window(self):
- # 验证窗口是否居中
- width = 400
- height = 300
- self.ui.center_window(width, height)
- # 获取窗口实际位置和大小
- x, y, w, h = self.root.winfo_geometry().split('+')
- # 验证窗口是否居中
- self.assertEqual(int(x), (self.root.winfo_screenwidth() - width) // 2)
- self.assertEqual(int(y), (self.root.winfo_screenheight() - height) // 2)
- def test_setup_segment_ui(self):
- # 验证多段运行界面的控件是否创建
- self.assertIsNotNone(self.ui.step_entry)
- self.assertIsNotNone(self.ui.stop_delay_entry)
- self.assertIsNotNone(self.ui.speed_entry)
- self.assertIsNotNone(self.ui.acceleration_entry)
- self.assertIsNotNone(self.ui.continuous_check)
- self.assertIsNotNone(self.ui.download_button)
- self.assertIsNotNone(self.ui.start_button)
- self.assertIsNotNone(self.ui.stop_button)
- def test_download_setting(self):
- # 模拟设置参数
- self.ui.step_entry.insert(0, "60")
- self.ui.acceleration_entry.insert(0, "100")
- self.ui.stop_delay_entry.insert(0, "500")
- self.ui.download_setting()
- # 验证是否正确下载配置
- # 可以根据具体的下载逻辑进行进一步的验证
- def test_start_segment_button(self):
- # 模拟点击启动按钮
- self.ui.start_segment_button()
- # 验证是否启动多段运行线程
- # 可以根据具体的多段运行逻辑进行进一步的验证
- def test_stop_motor_button(self):
- # 模拟点击停止按钮
- self.ui.stop_motor_button()
- # 验证是否启动停止电机线程
- # 可以根据具体的停止电机逻辑进行进一步的验证
- def test_start_motor_button(self):
- # 模拟输入有效的速度值
- self.ui.motor_velocity_entry.insert(0, "120")
- self.ui.start_motor_button()
- # 验证是否启动电机线程
- # 可以根据具体的启动电机逻辑进行进一步的验证
- def test_motor_home_button(self):
- # 模拟点击转台回零按钮
- self.ui.motor_home_button()
- # 验证是否启动转台回零线程
- # 可以根据具体的转台回零逻辑进行进一步的验证
- def test_pitch_motor_home_button(self):
- # 模拟点击俯仰回零按钮
- self.ui.pitch_motor_home_button()
- # 验证是否启动俯仰回零线程
- # 可以根据具体的俯仰回零逻辑进行进一步的验证
- def test_increase_pitch_button(self):
- # 模拟点击增加俯仰角按钮
- self.ui.increase_pitch_button()
- # 验证俯仰角是否增加
- # 可以根据具体的增加俯仰角逻辑进行进一步的验证
- def test_decrease_pitch_button(self):
- # 模拟点击减少俯仰角按钮
- self.ui.decrease_pitch_button()
- # 验证俯仰角是否减少
- # 可以根据具体的减少俯仰角逻辑进行进一步的验证
- def test_zero_pitch_button(self):
- # 模拟点击回到零度按钮
- self.ui.zero_pitch_button()
- # 验证俯仰角是否回到零度
- # 可以根据具体的回到零度逻辑进行进一步的验证
- if __name__ == '__main__':
- unittest.main()
|