feat: 内置并统一使用项目 ADB (#149)

This commit is contained in:
chengma
2026-08-11 12:01:07 +08:00
parent 56b1a46c5c
commit 8f2037db2f
21 changed files with 355 additions and 46 deletions
+23 -19
View File
@@ -3,12 +3,16 @@
import subprocess
import unittest
from src.adb_runtime import bundled_adb_path
from src.android_device_service import (
AndroidDeviceSearchError,
AndroidDeviceService,
)
ADB = str(bundled_adb_path())
def completed(command, stdout="", stderr="", returncode=0):
"""创建一条假的 ADB 命令结果。"""
@@ -56,7 +60,7 @@ USB-002 unauthorized usb:1-2 transport_id:3
def runner(command, _timeout):
commands.append(list(command))
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\n"
@@ -111,12 +115,12 @@ USB-002 unauthorized usb:1-2 transport_id:3
def runner(command, _timeout):
command = list(command)
commands.append(command)
if command[:2] == ["adb", "connect"]:
if command[:2] == [ADB, "connect"]:
return completed(
command,
"already connected to 192.168.0.173:5555\n",
)
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\n"
@@ -131,7 +135,7 @@ USB-002 unauthorized usb:1-2 transport_id:3
self.assertEqual(devices[0].serial, "192.168.0.173:5555")
self.assertEqual(
commands[0],
["adb", "connect", "192.168.0.173:5555"],
[ADB, "connect", "192.168.0.173:5555"],
)
def test_restore_saved_wifi_waits_for_authorization_handshake(self):
@@ -141,9 +145,9 @@ USB-002 unauthorized usb:1-2 transport_id:3
def runner(command, _timeout):
nonlocal device_list_count
command = list(command)
if command[:2] == ["adb", "connect"]:
if command[:2] == [ADB, "connect"]:
return completed(command, "connected to 192.168.0.173:5555\n")
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
device_list_count += 1
status = "unauthorized" if device_list_count == 1 else "device"
return completed(
@@ -167,7 +171,7 @@ USB-002 unauthorized usb:1-2 transport_id:3
def runner(command, _timeout):
command = list(command)
commands.append(command)
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\n"
@@ -181,12 +185,12 @@ USB-002 unauthorized usb:1-2 transport_id:3
self.assertEqual(devices[0].serial, "USB-001")
self.assertFalse(
any(command[:2] == ["adb", "connect"] for command in commands)
any(command[:2] == [ADB, "connect"] for command in commands)
)
def test_missing_model_is_read_from_device(self):
def runner(command, _timeout):
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\nUSB-001 device transport_id:1\n",
@@ -202,7 +206,7 @@ USB-002 unauthorized usb:1-2 transport_id:3
def test_property_failure_keeps_discovered_device(self):
def runner(command, _timeout):
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\nUSB-001 device model:Phone\n",
@@ -218,7 +222,7 @@ USB-002 unauthorized usb:1-2 transport_id:3
def runner(_command, _timeout):
raise FileNotFoundError("adb")
with self.assertRaisesRegex(AndroidDeviceSearchError, "未找到 adb"):
with self.assertRaisesRegex(AndroidDeviceSearchError, "内置 adb.exe"):
AndroidDeviceService(runner).search()
def test_timeout_has_recovery_message(self):
@@ -253,7 +257,7 @@ USB-002 unauthorized usb:1-2 transport_id:3
self.assertEqual(
commands,
[[
"adb",
ADB,
"-s",
"USB-001",
"shell",
@@ -296,9 +300,9 @@ USB-002 unauthorized usb:1-2 transport_id:3
"192.168.0.0/24 dev wlan0 scope link "
"src 192.168.0.173\n",
)
if command[:2] == ["adb", "connect"]:
if command[:2] == [ADB, "connect"]:
return completed(command, "connected to 192.168.0.173:5555\n")
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\n"
@@ -323,14 +327,14 @@ USB-002 unauthorized usb:1-2 transport_id:3
["USB-001", "192.168.0.173:5555"],
)
self.assertIn(
["adb", "-s", "USB-001", "tcpip", "5555"],
[ADB, "-s", "USB-001", "tcpip", "5555"],
commands,
)
self.assertEqual(
commands.count(["adb", "connect", "192.168.0.173:5555"]),
commands.count([ADB, "connect", "192.168.0.173:5555"]),
1,
)
self.assertEqual(commands.count(["adb", "devices", "-l"]), 3)
self.assertEqual(commands.count([ADB, "devices", "-l"]), 3)
self.assertTrue(
any("保存后拔掉 USB" in message for message in progress)
)
@@ -346,9 +350,9 @@ USB-002 unauthorized usb:1-2 transport_id:3
command = list(command)
if command[-3:] == ["shell", "ip", "route"]:
return completed(command, "local src 192.168.0.173\n")
if command[:2] == ["adb", "connect"]:
if command[:2] == [ADB, "connect"]:
return completed(command, "connected to 192.168.0.173:5555\n")
if command == ["adb", "devices", "-l"]:
if command == [ADB, "devices", "-l"]:
return completed(
command,
"List of devices attached\n"