123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- """
- @Author : liujunshen
- @File : test_ruishou.py
- @Time : 2023/04/04 13:57:03
- """
- from collections import namedtuple
- import time
- import pytest
- from core.peripheral.hand.ruishou import Constants
- from core.peripheral.hand.ruishou import Protocol
- from core.peripheral.hand.ruishou import RuishouClient
- from core.peripheral.hand.ruishou import RuishouConnector
- buffer_time = 0.3
- ParamStruct = namedtuple(
- "ParamStruct",
- "hand_select thumb index_finger middle_finger ring_finger little_finger duration"
- )
- # ============= 测试解析模块 ================
- def test_protocol_get_pack_success():
- protocol = Protocol()
- ret = protocol.get_pck("finish_action")
- assert isinstance(ret, bytearray)
- def test_protocol_get_pack_with_fail_cmd_return_none():
- protocol = Protocol()
- ret = protocol.get_pck("error_cmd")
- assert ret is None
- def test_protocol_get_motion_control_pack_success():
- params = {
- Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
- Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
- Constants.SendPckLocation.MOTION_CONTROL_INDEX_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_MIDDLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_DURATION: 10
- }
- protocol = Protocol()
- ret = protocol.get_pck("motion_control", params)
- assert isinstance(ret, bytearray)
- def test_protocol_get_motion_control_pack_with_lack_update_dict_return_none():
- params = {
- Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
- Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
- Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_DURATION: 10,
- }
- protocol = Protocol()
- ret = protocol.get_pck("motion_control", params)
- assert ret is None
- def test_protocol_get_motion_control_pack_with_error_update_dict_return_none():
- params = {
- 11: buffer_time,
- Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
- Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_DURATION: 10,
- }
- protocol = Protocol()
- ret = protocol.get_pck("motion_control", params)
- assert ret is None
- def test_protocol_unpack_one_cmd_bytes_success():
- protocol = Protocol()
- b = b"\xae\xaf\x05\x01\x00\x00\xff\xff\x01\xff"
- ret = protocol.unpack_bytes(b)
- assert isinstance(ret, list)
- assert len(ret) == 1
- def test_protocol_unpack_two_cmd_bytes_success():
- protocol = Protocol()
- b = b"\xae\xaf\x05\x01\x00\x00\xff\xff\x01\xff\xae\xaf\x05\x02\xff\xff\xff\xff\x03\xfe"
- ret = protocol.unpack_bytes(b)
- assert isinstance(ret, list)
- assert len(ret) == 2
- def test_protocol_unpack_bytes_with_error_bytes_return_empty_list():
- protocol = Protocol()
- b = b"\x05\x01\x00\x00\xff\xff\x01\xff"
- ret = protocol.unpack_bytes(b)
- assert isinstance(ret, list)
- assert len(ret) == 0
- def test_protocol_parse_list_success():
- protocol = Protocol()
- b = b"\xae\xaf\x05\x01\x00\x00\xff\xff"
- unpack_data = protocol.unpack_bytes(b)
- parsed_data = protocol.parse_bytes(unpack_data[0])
- assert isinstance(parsed_data, dict)
- # =======以下需要启动设备或模拟测试软件=============
- @pytest.mark.ruishou
- def test_connector_connect_and_close_success():
- connector = RuishouConnector()
- connector.start_client()
- time.sleep(buffer_time)
- connector.close_client()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_connector_sync_send_control_motion_data_success():
- connector = RuishouConnector()
- connector.start_client()
- params = {
- Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
- Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
- Constants.SendPckLocation.MOTION_CONTROL_INDEX_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_MIDDLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_DURATION: 5
- }
- res = connector.sync_send_data("motion_control", params)
- assert isinstance(res, dict)
- time.sleep(5)
- connector.close_client()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_connector_sync_send_error_data_return_none():
- connector = RuishouConnector()
- connector.start_client()
- params = {}
- res = connector.sync_send_data("error_data", params)
- assert res is None
- time.sleep(buffer_time)
- connector.close_client()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_connector_stop_operate_success():
- connector = RuishouConnector()
- connector.start_client()
- params = {
- Constants.SendPckLocation.MOTION_CONTROL_HAND: 1,
- Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
- Constants.SendPckLocation.MOTION_CONTROL_INDEX_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_MIDDLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_DURATION: 5
- }
- connector.sync_send_data("motion_control", params)
- time.sleep(3)
- connector.sync_send_data("finish_action")
- connector.close_client()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_connector_sync_send_control_motion_error_params_fail():
- connector = RuishouConnector()
- connector.start_client()
- params = {
- Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
- Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
- Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
- Constants.SendPckLocation.MOTION_CONTROL_DURATION: 5
- }
- res = connector.sync_send_data("motion_control", params)
- assert res is None
- time.sleep(buffer_time)
- connector.close_client()
- time.sleep(buffer_time)
- # ========== 测试睿手客户端(对业务) ============
- @pytest.mark.ruishou
- def test_client_init_success():
- client = RuishouClient()
- ret = client.init()
- assert ret["is_connected"]
- time.sleep(buffer_time)
- client.close()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_client_get_status_success():
- client = RuishouClient()
- client.init()
- status = client.status()
- assert status["is_connected"]
- time.sleep(buffer_time)
- client.close()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_client_get_status_with_not_init_return_not_connected():
- client = RuishouClient()
- status = client.status()
- assert not status["is_connected"]
- @pytest.mark.ruishou
- def test_client_get_status_with_close_client_return_not_connected():
- client = RuishouClient()
- client.init()
- time.sleep(buffer_time)
- client.close()
- status = client.status()
- assert not status["is_connected"]
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_client_start_operate_success():
- client = RuishouClient()
- client.init()
- params = ParamStruct("左手", 10, 10, 10, 10, 10, 6)
- res = client._control_motion(params)
- assert isinstance(res, dict)
- time.sleep(5)
- client.close()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_client_reconnect_start_operate_success():
- client = RuishouClient()
- client.init()
- params = ParamStruct("左手", 10, 10, 10, 10, 10, 6)
- time.sleep(buffer_time)
- client.close()
- res = client._control_motion(params)
- assert isinstance(res, dict)
- time.sleep(5)
- client.close()
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_client_close_success():
- client = RuishouClient()
- client.init()
- time.sleep(buffer_time)
- client.close()
- assert not client.connector.is_connected
- time.sleep(buffer_time)
- @pytest.mark.ruishou
- def test_client_close_with_not_init_success():
- client = RuishouClient()
- client.close()
- assert not client.connector.is_connected
|