Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion phone_agent/adb/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import subprocess
from typing import Optional

ADB_KEYBOARD_PACKAGE = "com.android.adbkeyboard"


def type_text(text: str, device_id: str | None = None) -> None:
"""
Expand Down Expand Up @@ -31,6 +33,8 @@ def type_text(text: str, device_id: str | None = None) -> None:
"--es",
"msg",
encoded_text,
"-p",
ADB_KEYBOARD_PACKAGE,
],
capture_output=True,
text=True,
Expand All @@ -47,7 +51,16 @@ def clear_text(device_id: str | None = None) -> None:
adb_prefix = _get_adb_prefix(device_id)

subprocess.run(
adb_prefix + ["shell", "am", "broadcast", "-a", "ADB_CLEAR_TEXT"],
adb_prefix
+ [
"shell",
"am",
"broadcast",
"-a",
"ADB_CLEAR_TEXT",
"-p",
ADB_KEYBOARD_PACKAGE,
],
capture_output=True,
text=True,
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_adb_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import subprocess

from phone_agent.adb import input as adb_input


def test_type_text_targets_adb_keyboard_package(monkeypatch) -> None:
commands: list[list[str]] = []

def fake_run(command, **kwargs):
commands.append(command)
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")

monkeypatch.setattr(adb_input.subprocess, "run", fake_run)

adb_input.type_text("你好", device_id="device-1")

command = commands[0]
assert command[:3] == ["adb", "-s", "device-1"]
assert command[-2:] == ["-p", "com.android.adbkeyboard"]


def test_clear_text_targets_adb_keyboard_package(monkeypatch) -> None:
commands: list[list[str]] = []

def fake_run(command, **kwargs):
commands.append(command)
return subprocess.CompletedProcess(command, 0, stdout="", stderr="")

monkeypatch.setattr(adb_input.subprocess, "run", fake_run)

adb_input.clear_text(device_id="device-1")

command = commands[0]
assert command[:3] == ["adb", "-s", "device-1"]
assert command[-2:] == ["-p", "com.android.adbkeyboard"]