test_ruishou.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. """
  2. @Author : liujunshen
  3. @File : test_ruishou.py
  4. @Time : 2023/04/04 13:57:03
  5. """
  6. from collections import namedtuple
  7. import time
  8. import pytest
  9. from core.peripheral.hand.ruishou import Constants
  10. from core.peripheral.hand.ruishou import Protocol
  11. from core.peripheral.hand.ruishou import RuishouClient
  12. from core.peripheral.hand.ruishou import RuishouConnector
  13. buffer_time = 0.3
  14. ParamStruct = namedtuple(
  15. "ParamStruct",
  16. "hand_select thumb index_finger middle_finger ring_finger little_finger duration"
  17. )
  18. # ============= 测试解析模块 ================
  19. def test_protocol_get_pack_success():
  20. protocol = Protocol()
  21. ret = protocol.get_pck("finish_action")
  22. assert isinstance(ret, bytearray)
  23. def test_protocol_get_pack_with_fail_cmd_return_none():
  24. protocol = Protocol()
  25. ret = protocol.get_pck("error_cmd")
  26. assert ret is None
  27. def test_protocol_get_motion_control_pack_success():
  28. params = {
  29. Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
  30. Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
  31. Constants.SendPckLocation.MOTION_CONTROL_INDEX_FINGER_BENDING: 10,
  32. Constants.SendPckLocation.MOTION_CONTROL_MIDDLE_FINGER_BENDING: 10,
  33. Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
  34. Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
  35. Constants.SendPckLocation.MOTION_CONTROL_DURATION: 10
  36. }
  37. protocol = Protocol()
  38. ret = protocol.get_pck("motion_control", params)
  39. assert isinstance(ret, bytearray)
  40. def test_protocol_get_motion_control_pack_with_lack_update_dict_return_none():
  41. params = {
  42. Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
  43. Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
  44. Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
  45. Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
  46. Constants.SendPckLocation.MOTION_CONTROL_DURATION: 10,
  47. }
  48. protocol = Protocol()
  49. ret = protocol.get_pck("motion_control", params)
  50. assert ret is None
  51. def test_protocol_get_motion_control_pack_with_error_update_dict_return_none():
  52. params = {
  53. 11: buffer_time,
  54. Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
  55. Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
  56. Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
  57. Constants.SendPckLocation.MOTION_CONTROL_DURATION: 10,
  58. }
  59. protocol = Protocol()
  60. ret = protocol.get_pck("motion_control", params)
  61. assert ret is None
  62. def test_protocol_unpack_one_cmd_bytes_success():
  63. protocol = Protocol()
  64. b = b"\xae\xaf\x05\x01\x00\x00\xff\xff\x01\xff"
  65. ret = protocol.unpack_bytes(b)
  66. assert isinstance(ret, list)
  67. assert len(ret) == 1
  68. def test_protocol_unpack_two_cmd_bytes_success():
  69. protocol = Protocol()
  70. b = b"\xae\xaf\x05\x01\x00\x00\xff\xff\x01\xff\xae\xaf\x05\x02\xff\xff\xff\xff\x03\xfe"
  71. ret = protocol.unpack_bytes(b)
  72. assert isinstance(ret, list)
  73. assert len(ret) == 2
  74. def test_protocol_unpack_bytes_with_error_bytes_return_empty_list():
  75. protocol = Protocol()
  76. b = b"\x05\x01\x00\x00\xff\xff\x01\xff"
  77. ret = protocol.unpack_bytes(b)
  78. assert isinstance(ret, list)
  79. assert len(ret) == 0
  80. def test_protocol_parse_list_success():
  81. protocol = Protocol()
  82. b = b"\xae\xaf\x05\x01\x00\x00\xff\xff"
  83. unpack_data = protocol.unpack_bytes(b)
  84. parsed_data = protocol.parse_bytes(unpack_data[0])
  85. assert isinstance(parsed_data, dict)
  86. # =======以下需要启动设备或模拟测试软件=============
  87. @pytest.mark.ruishou
  88. def test_connector_connect_and_close_success():
  89. connector = RuishouConnector()
  90. connector.start_client()
  91. time.sleep(buffer_time)
  92. connector.close_client()
  93. time.sleep(buffer_time)
  94. @pytest.mark.ruishou
  95. def test_connector_sync_send_control_motion_data_success():
  96. connector = RuishouConnector()
  97. connector.start_client()
  98. params = {
  99. Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
  100. Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
  101. Constants.SendPckLocation.MOTION_CONTROL_INDEX_FINGER_BENDING: 10,
  102. Constants.SendPckLocation.MOTION_CONTROL_MIDDLE_FINGER_BENDING: 10,
  103. Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
  104. Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
  105. Constants.SendPckLocation.MOTION_CONTROL_DURATION: 5
  106. }
  107. res = connector.sync_send_data("motion_control", params)
  108. assert isinstance(res, dict)
  109. time.sleep(5)
  110. connector.close_client()
  111. time.sleep(buffer_time)
  112. @pytest.mark.ruishou
  113. def test_connector_sync_send_error_data_return_none():
  114. connector = RuishouConnector()
  115. connector.start_client()
  116. params = {}
  117. res = connector.sync_send_data("error_data", params)
  118. assert res is None
  119. time.sleep(buffer_time)
  120. connector.close_client()
  121. time.sleep(buffer_time)
  122. @pytest.mark.ruishou
  123. def test_connector_stop_operate_success():
  124. connector = RuishouConnector()
  125. connector.start_client()
  126. params = {
  127. Constants.SendPckLocation.MOTION_CONTROL_HAND: 1,
  128. Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
  129. Constants.SendPckLocation.MOTION_CONTROL_INDEX_FINGER_BENDING: 10,
  130. Constants.SendPckLocation.MOTION_CONTROL_MIDDLE_FINGER_BENDING: 10,
  131. Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
  132. Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
  133. Constants.SendPckLocation.MOTION_CONTROL_DURATION: 5
  134. }
  135. connector.sync_send_data("motion_control", params)
  136. time.sleep(3)
  137. connector.sync_send_data("finish_action")
  138. connector.close_client()
  139. time.sleep(buffer_time)
  140. @pytest.mark.ruishou
  141. def test_connector_sync_send_control_motion_error_params_fail():
  142. connector = RuishouConnector()
  143. connector.start_client()
  144. params = {
  145. Constants.SendPckLocation.MOTION_CONTROL_HAND: 2,
  146. Constants.SendPckLocation.MOTION_CONTROL_THUMB_BENDING: 15,
  147. Constants.SendPckLocation.MOTION_CONTROL_RING_FINGER_BENDING: 10,
  148. Constants.SendPckLocation.MOTION_CONTROL_LITTLE_FINGER_BENDING: 10,
  149. Constants.SendPckLocation.MOTION_CONTROL_DURATION: 5
  150. }
  151. res = connector.sync_send_data("motion_control", params)
  152. assert res is None
  153. time.sleep(buffer_time)
  154. connector.close_client()
  155. time.sleep(buffer_time)
  156. # ========== 测试睿手客户端(对业务) ============
  157. @pytest.mark.ruishou
  158. def test_client_init_success():
  159. client = RuishouClient()
  160. ret = client.init()
  161. assert ret["is_connected"]
  162. time.sleep(buffer_time)
  163. client.close()
  164. time.sleep(buffer_time)
  165. @pytest.mark.ruishou
  166. def test_client_get_status_success():
  167. client = RuishouClient()
  168. client.init()
  169. status = client.status()
  170. assert status["is_connected"]
  171. time.sleep(buffer_time)
  172. client.close()
  173. time.sleep(buffer_time)
  174. @pytest.mark.ruishou
  175. def test_client_get_status_with_not_init_return_not_connected():
  176. client = RuishouClient()
  177. status = client.status()
  178. assert not status["is_connected"]
  179. @pytest.mark.ruishou
  180. def test_client_get_status_with_close_client_return_not_connected():
  181. client = RuishouClient()
  182. client.init()
  183. time.sleep(buffer_time)
  184. client.close()
  185. status = client.status()
  186. assert not status["is_connected"]
  187. time.sleep(buffer_time)
  188. @pytest.mark.ruishou
  189. def test_client_start_operate_success():
  190. client = RuishouClient()
  191. client.init()
  192. params = ParamStruct("左手", 10, 10, 10, 10, 10, 6)
  193. res = client._control_motion(params)
  194. assert isinstance(res, dict)
  195. time.sleep(5)
  196. client.close()
  197. time.sleep(buffer_time)
  198. @pytest.mark.ruishou
  199. def test_client_reconnect_start_operate_success():
  200. client = RuishouClient()
  201. client.init()
  202. params = ParamStruct("左手", 10, 10, 10, 10, 10, 6)
  203. time.sleep(buffer_time)
  204. client.close()
  205. res = client._control_motion(params)
  206. assert isinstance(res, dict)
  207. time.sleep(5)
  208. client.close()
  209. time.sleep(buffer_time)
  210. @pytest.mark.ruishou
  211. def test_client_close_success():
  212. client = RuishouClient()
  213. client.init()
  214. time.sleep(buffer_time)
  215. client.close()
  216. assert not client.connector.is_connected
  217. time.sleep(buffer_time)
  218. @pytest.mark.ruishou
  219. def test_client_close_with_not_init_success():
  220. client = RuishouClient()
  221. client.close()
  222. assert not client.connector.is_connected