feat: 支持 USB 设备转 Wi-Fi ADB (#25)
This commit is contained in:
@@ -16,6 +16,25 @@ def completed(command, stdout="", stderr="", returncode=0):
|
||||
|
||||
|
||||
class AndroidDeviceServiceTest(unittest.TestCase):
|
||||
def test_parse_wifi_ipv4_from_route(self):
|
||||
output = (
|
||||
"10.20.0.0/16 dev rmnet_data0 scope link src 10.20.1.2\n"
|
||||
"default via 192.168.0.1 dev wlan0 proto dhcp\n"
|
||||
"192.168.0.0/24 dev wlan0 proto kernel scope link "
|
||||
"src 192.168.0.173\n"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
AndroidDeviceService.parse_wifi_ipv4(output),
|
||||
"192.168.0.173",
|
||||
)
|
||||
|
||||
def test_missing_wifi_ipv4_has_recovery_message(self):
|
||||
with self.assertRaisesRegex(AndroidDeviceSearchError, "连接.*Wi-Fi"):
|
||||
AndroidDeviceService.parse_wifi_ipv4(
|
||||
"default via 10.0.0.1 dev wlan0\n"
|
||||
)
|
||||
|
||||
def test_parse_usb_wifi_and_unavailable_devices(self):
|
||||
output = """List of devices attached
|
||||
USB-001 device product:p model:Pixel_8 device:husky transport_id:1
|
||||
@@ -104,6 +123,119 @@ USB-002 unauthorized usb:1-2 transport_id:3
|
||||
with self.assertRaisesRegex(AndroidDeviceSearchError, "didn't ACK"):
|
||||
AndroidDeviceService(runner).search()
|
||||
|
||||
def test_convert_usb_to_wifi_reconnects_after_usb_is_removed(self):
|
||||
commands = []
|
||||
device_list_count = 0
|
||||
|
||||
def runner(command, _timeout):
|
||||
nonlocal device_list_count
|
||||
command = list(command)
|
||||
commands.append(command)
|
||||
if command[-3:] == ["shell", "ip", "route"]:
|
||||
return completed(
|
||||
command,
|
||||
"192.168.0.0/24 dev wlan0 scope link "
|
||||
"src 192.168.0.173\n",
|
||||
)
|
||||
if command[:2] == ["adb", "connect"]:
|
||||
return completed(command, "connected to 192.168.0.173:5555\n")
|
||||
if command == ["adb", "devices", "-l"]:
|
||||
device_list_count += 1
|
||||
usb_line = (
|
||||
"USB-001 device model:Phone\n"
|
||||
if device_list_count == 1
|
||||
else ""
|
||||
)
|
||||
return completed(
|
||||
command,
|
||||
"List of devices attached\n"
|
||||
f"{usb_line}"
|
||||
"192.168.0.173:5555 device model:Phone\n",
|
||||
)
|
||||
if command[-1] == "ro.build.version.release":
|
||||
return completed(command, "14\n")
|
||||
return completed(command, "restarting in TCP mode port: 5555\n")
|
||||
|
||||
progress = []
|
||||
service = AndroidDeviceService(
|
||||
runner,
|
||||
usb_disconnect_timeout_seconds=0.03,
|
||||
usb_poll_interval_seconds=0.01,
|
||||
sleeper=lambda _seconds: None,
|
||||
)
|
||||
|
||||
result = service.convert_usb_to_wifi(
|
||||
"USB-001",
|
||||
on_progress=progress.append,
|
||||
)
|
||||
|
||||
self.assertEqual(result.wifi_serial, "192.168.0.173:5555")
|
||||
self.assertEqual(result.devices[0].connection_type, "Wi-Fi")
|
||||
self.assertIn(
|
||||
["adb", "-s", "USB-001", "tcpip", "5555"],
|
||||
commands,
|
||||
)
|
||||
self.assertEqual(
|
||||
commands.count(["adb", "connect", "192.168.0.173:5555"]),
|
||||
2,
|
||||
)
|
||||
self.assertTrue(any("拔掉 USB" in message for message in progress))
|
||||
|
||||
def test_convert_usb_to_wifi_times_out_without_unplug(self):
|
||||
def runner(command, _timeout):
|
||||
command = list(command)
|
||||
if command[-3:] == ["shell", "ip", "route"]:
|
||||
return completed(command, "local src 192.168.0.173\n")
|
||||
if command[:2] == ["adb", "connect"]:
|
||||
return completed(command, "connected to 192.168.0.173:5555\n")
|
||||
if command == ["adb", "devices", "-l"]:
|
||||
return completed(
|
||||
command,
|
||||
"List of devices attached\nUSB-001 device\n",
|
||||
)
|
||||
return completed(command, "restarting in TCP mode port: 5555\n")
|
||||
|
||||
service = AndroidDeviceService(
|
||||
runner,
|
||||
usb_disconnect_timeout_seconds=0.03,
|
||||
usb_poll_interval_seconds=0.01,
|
||||
sleeper=lambda _seconds: None,
|
||||
)
|
||||
|
||||
with self.assertRaisesRegex(AndroidDeviceSearchError, "拔出 USB 超时"):
|
||||
service.convert_usb_to_wifi("USB-001")
|
||||
|
||||
def test_convert_usb_to_wifi_rejects_wifi_serial(self):
|
||||
with self.assertRaisesRegex(AndroidDeviceSearchError, "已经是 Wi-Fi"):
|
||||
AndroidDeviceService().convert_usb_to_wifi(
|
||||
"192.168.0.173:5555"
|
||||
)
|
||||
|
||||
def test_convert_usb_to_wifi_requires_connected_wifi_status(self):
|
||||
def runner(command, _timeout):
|
||||
command = list(command)
|
||||
if command[-3:] == ["shell", "ip", "route"]:
|
||||
return completed(command, "local src 192.168.0.173\n")
|
||||
if command[:2] == ["adb", "connect"]:
|
||||
return completed(command, "connected to 192.168.0.173:5555\n")
|
||||
if command == ["adb", "devices", "-l"]:
|
||||
return completed(
|
||||
command,
|
||||
"List of devices attached\n"
|
||||
"192.168.0.173:5555 offline\n",
|
||||
)
|
||||
return completed(command, "restarting in TCP mode port: 5555\n")
|
||||
|
||||
service = AndroidDeviceService(
|
||||
runner,
|
||||
usb_disconnect_timeout_seconds=0.03,
|
||||
usb_poll_interval_seconds=0.01,
|
||||
sleeper=lambda _seconds: None,
|
||||
)
|
||||
|
||||
with self.assertRaisesRegex(AndroidDeviceSearchError, "状态为 offline"):
|
||||
service.convert_usb_to_wifi("USB-001")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user