"""Arm selection integration tests without robot connections or user settings.""" import os from pathlib import Path import tempfile import unittest from unittest.mock import Mock, patch os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") from PyQt5 import QtCore, QtWidgets from ui.app import MainWindow from ui.robot_models import ROBOT_MODELS, RobotDescription, load_robot_description class FakeArmWorker(QtCore.QObject): updated = QtCore.pyqtSignal(dict) log = QtCore.pyqtSignal(str) def __init__(self): super().__init__() self.start = Mock() self.stop = Mock() self.wait = Mock(return_value=True) self.isRunning = Mock(return_value=True) self.deleteLater = Mock() class ArmSelectionTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) def setUp(self): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) settings_class = QtCore.QSettings settings_path = str(Path(directory.name) / "UI.ini") settings_patch = patch( "ui.app.QtCore.QSettings", side_effect=lambda *args, **kwargs: settings_class(settings_path, settings_class.IniFormat), ) settings_patch.start() self.addCleanup(settings_patch.stop) self.windows = [] def tearDown(self): for window in self.windows: window.close() window.deleteLater() self.app.sendPostedEvents(None, QtCore.QEvent.DeferredDelete) self.app.processEvents() def window(self): window = MainWindow() self.windows.append(window) return window def select(self, combo, value): index = combo.findData(value) self.assertGreaterEqual(index, 0) combo.setCurrentIndex(index) def device_ids(self, window): return [window.device_combo.itemData(index) for index in range(window.device_combo.count())] def test_options_match_model_and_keep_selected_side_across_models(self): window = self.window() self.assertFalse(window.device_combo.isEditable()) self.assertEqual(self.device_ids(window), ["right_arm", "left_arm"]) self.assertEqual(window._selected_device_id(), "right_arm") self.assertEqual(window._selected_arm().side, "right") self.assertEqual(window._selected_arm().device_id, "right_arm") self.assertEqual(window.ee_link_edit.currentText(), "R_FINGER_TIP") self.select(window.device_combo, "left_arm") self.assertEqual(window._selected_arm().side, "left") self.assertEqual(window.base_link_edit.currentText(), "PELVIS_S") self.assertEqual(window.ee_link_edit.currentText(), "L_WRIST_R_S") self.assertEqual(len(window._selected_arm().joint_names), 7) self.select(window.robot_model_combo, "xiaozhao") self.assertEqual(self.device_ids(window), ["xiaozhao_right_arm", "xiaozhao_left_arm", "xiaozhao_head"]) self.assertEqual(window._selected_device_id(), "xiaozhao_left_arm") self.assertEqual(window._selected_arm().side, "left") self.assertEqual(window._selected_arm().ee_link, "tcp-left") self.assertEqual(window.base_link_edit.currentText(), "base_link") self.assertEqual(window.ee_link_edit.currentText(), "tcp-left") self.assertEqual(window.table.rowCount(), 17) self.assertEqual(window.movej_table.rowCount(), 17) self.assertEqual(window.speedj_table.rowCount(), 17) self.select(window.robot_model_combo, "xiaoyan") self.assertEqual(window._selected_device_id(), "left_arm") self.assertEqual(window.ee_link_edit.currentText(), "L_WRIST_R_S") def test_all_command_actions_send_selected_device_id(self): window = self.window() self.select(window.robot_model_combo, "xiaozhao") self.select(window.device_combo, "xiaozhao_left_arm") window.movej_table.item(0, 0).setCheckState(QtCore.Qt.Checked) window.speedj_table.item(0, 0).setCheckState(QtCore.Qt.Checked) window.calibrate_joint_edit.setText("joint1-left") actions = { "movej": window._on_movej, "movel": window._on_movel, "speedj": window._on_speedj, "speedl": window._on_speedl, "torque_on": window._on_torque_on, "torque_off": window._on_torque_off, "stop_motion": window._on_stop_motion, "calibrate_zero_q": window._on_calibrate_zero, } with patch.object(window, "_start_process") as start_process: for action, callback in actions.items(): with self.subTest(action=action): start_process.reset_mock() callback() start_process.assert_called_once() args = start_process.call_args.args[0] self.assertIn(action, args) self.assertEqual(args[args.index("--device-id") + 1], "xiaozhao_left_arm") def test_pollers_use_selected_arm_and_stop_on_arm_change(self): window = self.window() self.select(window.robot_model_combo, "xiaozhao") self.select(window.device_combo, "xiaozhao_left_arm") joint_worker, pose_worker = FakeArmWorker(), FakeArmWorker() with patch("ui.app.JointStateWorker", return_value=joint_worker) as joint_factory, patch( "ui.app.PoseWorker", return_value=pose_worker ) as pose_factory: window.jointstate_toggle.setChecked(True) window.getpose_toggle.setChecked(True) self.assertEqual(joint_factory.call_args.kwargs["device_id"], "xiaozhao_left_arm") self.assertEqual(pose_factory.call_args.kwargs["device_id"], "xiaozhao_left_arm") self.assertEqual(pose_factory.call_args.kwargs["base_link"], "base_link") self.assertEqual(pose_factory.call_args.kwargs["ee_link"], "tcp-left") window.table.selectRow(0) joint_worker.updated.emit({"joint1-left": (0.3, 0.2)}) pose_worker.updated.emit({"x": 0.4}) window.movej_table.item(0, 0).setCheckState(QtCore.Qt.Checked) window.movej_table.item(0, 3).setText("0.5") window.speedj_table.item(0, 0).setCheckState(QtCore.Qt.Checked) window.speedj_table.item(0, 2).setText("0.6") window.movel_x_edit.setValue(0.7) window.speedl_wz_edit.setValue(0.8) window.calibrate_joint_edit.setText("joint1-left") self.select(window.device_combo, "xiaozhao_right_arm") for worker in (joint_worker, pose_worker): worker.stop.assert_called_once() worker.wait.assert_called_once() self.assertFalse(window.jointstate_toggle.isChecked()) self.assertFalse(window.getpose_toggle.isChecked()) self.assertIsNone(window._joint_worker) self.assertIsNone(window._pose_worker) self.assertEqual(window.ee_link_edit.currentText(), "tcp-right") self.assertEqual(window._get_selected_movej_joints(), []) self.assertEqual(window._get_selected_speedj_joints(), []) self.assertEqual(float(window.movej_table.item(0, 3).text()), 0.0) self.assertEqual(float(window.speedj_table.item(0, 2).text()), 0.0) self.assertEqual(window.movel_x_edit.value(), 0.0) self.assertEqual(window.speedl_wz_edit.value(), 0.0) self.assertEqual(window.calibrate_joint_edit.text(), "") self.assertFalse(window._plot_selected) for series in window.plot_page.pos_hist.values(): self.assertEqual(len(series.sample(10)[0]), 0) joint_worker.updated.emit({"joint1-left": (9.0, 9.0)}) pose_worker.updated.emit({"x": 9.0}) self.assertEqual(window._last_data, {}) self.assertEqual(window._last_pose, {}) def test_failed_model_load_preserves_arm_options_and_selection(self): window = self.window() self.select(window.device_combo, "left_arm") with patch("ui.app.load_robot_description", side_effect=ValueError("test failure")): self.select(window.robot_model_combo, "xiaozhao") self.assertEqual(window.robot_model_id, "xiaoyan") self.assertEqual(self.device_ids(window), ["right_arm", "left_arm"]) self.assertEqual(window._selected_device_id(), "left_arm") self.assertEqual(window.ee_link_edit.currentText(), "L_WRIST_R_S") def test_arm_options_require_corresponding_urdf_joints(self): window = self.window() self.select(window.device_combo, "left_arm") model = load_robot_description(ROBOT_MODELS["xiaozhao"].urdf_path) right_only = RobotDescription( joint_names=[name for name in model.joint_names if name.endswith("-right")], link_names=model.link_names, ) with patch("ui.app.load_robot_description", return_value=right_only): self.select(window.robot_model_combo, "xiaozhao") self.assertEqual(self.device_ids(window), ["xiaozhao_right_arm"]) self.assertEqual(window._selected_device_id(), "xiaozhao_right_arm") self.assertEqual(window.ee_link_edit.currentText(), "tcp-right") def test_arm_options_require_corresponding_urdf_ee_link(self): window = self.window() model = load_robot_description(ROBOT_MODELS["xiaozhao"].urdf_path) without_right_ee = RobotDescription( joint_names=model.joint_names, link_names=[name for name in model.link_names if name != "tcp-right"], ) with patch("ui.app.load_robot_description", return_value=without_right_ee): self.select(window.robot_model_combo, "xiaozhao") self.assertEqual(self.device_ids(window), ["xiaozhao_left_arm", "xiaozhao_head"]) self.assertEqual(window._selected_device_id(), "xiaozhao_left_arm") self.assertEqual(window.ee_link_edit.currentText(), "tcp-left") def test_head_commands_send_neck_joints_in_urdf_order(self): window = self.window() self.select(window.robot_model_combo, "xiaozhao") self.select(window.device_combo, "xiaozhao_head") neck_names = ["neck-joint1", "neck-joint2", "neck-joint3"] self.assertEqual(list(window._selected_arm().joint_names), neck_names) # Selection order must not reorder the three joint values sent to the service. for index in reversed(range(3)): row = window.joint_names.index(neck_names[index]) window.movej_table.item(row, 0).setCheckState(QtCore.Qt.Checked) window.movej_table.item(row, 3).setText(str((index + 1) / 10.0)) window.speedj_table.item(row, 0).setCheckState(QtCore.Qt.Checked) window.speedj_table.item(row, 2).setText(str(-(index + 1) / 10.0)) window.calibrate_joint_edit.setText("neck-joint1") actions = { "movej": window._on_movej, "movel": window._on_movel, "speedj": window._on_speedj, "speedl": window._on_speedl, "torque_on": window._on_torque_on, "torque_off": window._on_torque_off, "stop_motion": window._on_stop_motion, "calibrate_zero_q": window._on_calibrate_zero, } with patch.object(window, "_start_process") as start_process: for action, callback in actions.items(): with self.subTest(action=action): start_process.reset_mock() callback() start_process.assert_called_once() args = start_process.call_args.args[0] self.assertEqual(args[args.index("--device-id") + 1], "xiaozhao_head") self.assertIn(action, args) if action in ("movej", "speedj"): pairs = [part.split(":") for part in args[args.index("--joint-list") + 1].split(";")] self.assertEqual([name for name, _ in pairs], neck_names) sign = 1 if action == "movej" else -1 self.assertEqual([float(value) for _, value in pairs], [sign * value for value in (0.1, 0.2, 0.3)]) def test_head_pollers_pose_defaults_and_fallback_when_model_has_no_head(self): window = self.window() self.select(window.robot_model_combo, "xiaozhao") self.select(window.device_combo, "xiaozhao_head") self.assertEqual(window._selected_arm().side, "head") self.assertEqual(window.base_link_edit.currentText(), "base_link") self.assertEqual(window.ee_link_edit.currentText(), "neck_Link3") self.assertEqual(window.movel_ee_link_combo.currentText(), "neck_Link3") for table in (window.table, window.movej_table, window.speedj_table): self.assertEqual(table.rowCount(), 17) joint_worker, pose_worker = FakeArmWorker(), FakeArmWorker() with patch("ui.app.JointStateWorker", return_value=joint_worker) as joint_factory, patch( "ui.app.PoseWorker", return_value=pose_worker ) as pose_factory: window.jointstate_toggle.setChecked(True) window.getpose_toggle.setChecked(True) self.assertEqual(joint_factory.call_args.kwargs["device_id"], "xiaozhao_head") self.assertEqual(pose_factory.call_args.kwargs["device_id"], "xiaozhao_head") self.assertEqual(pose_factory.call_args.kwargs["base_link"], "base_link") self.assertEqual(pose_factory.call_args.kwargs["ee_link"], "neck_Link3") row = window.joint_names.index("neck-joint1") window.table.selectRow(row) joint_worker.updated.emit({"neck-joint1": (0.3, 0.2)}) pose_worker.updated.emit({"x": 0.4}) window.movej_table.item(row, 0).setCheckState(QtCore.Qt.Checked) window.movej_table.item(row, 3).setText("0.5") window.speedj_table.item(row, 0).setCheckState(QtCore.Qt.Checked) window.speedj_table.item(row, 2).setText("0.6") window.movel_x_edit.setValue(0.7) self.select(window.robot_model_combo, "xiaoyan") self.assertEqual(window._selected_device_id(), "right_arm") self.assertEqual(window.ee_link_edit.currentText(), "R_FINGER_TIP") self.assertEqual(window.movel_ee_link_combo.currentText(), "R_FINGER_TIP") self.assertEqual(window.table.rowCount(), 14) for worker in (joint_worker, pose_worker): worker.stop.assert_called_once() worker.wait.assert_called_once() self.assertFalse(window.jointstate_toggle.isChecked()) self.assertFalse(window.getpose_toggle.isChecked()) self.assertEqual(window._last_data, {}) self.assertEqual(window._last_pose, {}) self.assertFalse(window._plot_selected) self.assertEqual(window._get_selected_movej_joints(), []) self.assertEqual(window._get_selected_speedj_joints(), []) self.assertEqual(window.movel_x_edit.value(), 0.0) for series in window.plot_page.pos_hist.values(): self.assertEqual(len(series.sample(10)[0]), 0) def test_head_is_omitted_when_neck_joint_or_end_link_is_missing(self): window = self.window() model = load_robot_description(ROBOT_MODELS["xiaozhao"].urdf_path) incomplete_models = ( RobotDescription([name for name in model.joint_names if name != "neck-joint2"], model.link_names), RobotDescription(model.joint_names, [name for name in model.link_names if name != "neck_Link3"]), ) for description in incomplete_models: with self.subTest(description=description): with patch("ui.app.load_robot_description", return_value=description): self.select(window.robot_model_combo, "xiaozhao") self.assertEqual(self.device_ids(window), ["xiaozhao_right_arm", "xiaozhao_left_arm"]) self.assertEqual(window._selected_device_id(), "xiaozhao_right_arm") self.select(window.robot_model_combo, "xiaoyan") if __name__ == "__main__": unittest.main()