| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- //go:build !service
- // phone_service_test.go — 「手机」工具纯函数单元测试。
- package main
- import (
- "encoding/base64"
- "os"
- "os/exec"
- "path/filepath"
- "reflect"
- "strings"
- "testing"
- )
- // TestParseADBDevices 验证 adb devices 输出的解析:
- // 跳过 daemon 启动行、表头与空行,正确提取序列号与状态。
- func TestParseADBDevices(t *testing.T) {
- out := "* daemon not running; starting now at tcp:5037\n" +
- "* daemon started successfully\n" +
- "List of devices attached\n" +
- "0123456789ABCDEF\tdevice\n" +
- "emulator-5554\tdevice\n" +
- "ABCDEF\tunauthorized\n"
- got := parseADBDevices(out)
- want := []PhoneDevice{
- {Serial: "0123456789ABCDEF", State: "device"},
- {Serial: "emulator-5554", State: "device"},
- {Serial: "ABCDEF", State: "unauthorized"},
- }
- if !reflect.DeepEqual(got, want) {
- t.Fatalf("parseADBDevices() = %+v, want %+v", got, want)
- }
- }
- // TestParseADBDevicesEmpty 验证空输出/无设备时返回空列表。
- func TestParseADBDevicesEmpty(t *testing.T) {
- for _, out := range []string{
- "",
- "List of devices attached\n",
- "\n\n",
- "* daemon started successfully\n",
- } {
- if got := parseADBDevices(out); len(got) != 0 {
- t.Fatalf("parseADBDevices(%q) = %+v, want empty", out, got)
- }
- }
- }
- // TestParseADBVersion 验证从 adb version 输出提取版本行。
- func TestParseADBVersion(t *testing.T) {
- out := "Android Debug Bridge version 1.0.41\nVersion 35.0.2-12742879\nInstalled as C:\\adb\\adb.exe\n"
- got := parseADBVersion(out)
- want := "Android Debug Bridge version 1.0.41"
- if got != want {
- t.Fatalf("parseADBVersion() = %q, want %q", got, want)
- }
- }
- // TestParseADBVersionFallback 验证无版本行时返回整体 trim 后输出。
- func TestParseADBVersionFallback(t *testing.T) {
- out := " some output \n"
- if got := parseADBVersion(out); got != "some output" {
- t.Fatalf("parseADBVersion() = %q, want %q", got, "some output")
- }
- }
- // TestScreenshotDataURI 验证 PNG 字节编码为 base64 data URI。
- func TestScreenshotDataURI(t *testing.T) {
- png := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A}
- want := "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
- if got := screenshotDataURI(png); got != want {
- t.Fatalf("screenshotDataURI() = %q, want %q", got, want)
- }
- }
- // TestDecodeScreenshotPNG 验证截图 data URI 解析:合法输入还原字节,
- // 非法前缀/非法 base64/空内容均返回错误。
- func TestDecodeScreenshotPNG(t *testing.T) {
- png := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A}
- uri := screenshotDataURI(png)
- got, err := decodeScreenshotPNG(uri)
- if err != nil {
- t.Fatalf("decodeScreenshotPNG(%q) 错误: %v", uri, err)
- }
- if !reflect.DeepEqual(got, png) {
- t.Fatalf("decodeScreenshotPNG() = %v, want %v", got, png)
- }
- // 非法前缀
- if _, err := decodeScreenshotPNG("data:image/jpeg;base64,AAAA"); err == nil {
- t.Fatal("非 PNG data URI 应返回错误")
- }
- // 非法 base64
- if _, err := decodeScreenshotPNG("data:image/png;base64,@@@"); err == nil {
- t.Fatal("非法 base64 应返回错误")
- }
- // 空内容
- if _, err := decodeScreenshotPNG("data:image/png;base64,"); err == nil {
- t.Fatal("空内容应返回错误")
- }
- }
- // TestParseClipboardServiceOutput 验证 `service call clipboard 2` 输出解析:
- // 拼接单引号内可打印 ASCII 片段(忽略不可打印字节/引号外文本)。
- func TestParseClipboardServiceOutput(t *testing.T) {
- out := "Result: Parcel(00000000 00000000 00000000 00000000 ' h e l l o' 00000000)\n" +
- "00000012 00000000 ' w o r l d '"
- got := parseClipboardServiceOutput(out)
- // 片段内所有可打印字符被拼接(含空格),非字母数字也保留
- if got != " h e l l o w o r l d " {
- t.Fatalf("parseClipboardServiceOutput() = %q, want %q", got, " h e l l o w o r l d ")
- }
- // 无单引号(空剪贴板)返回空串
- if v := parseClipboardServiceOutput("Result: Parcel(00000000)"); v != "" {
- t.Fatalf("空输出应返回空串,got %q", v)
- }
- }
- // TestClipboardServiceString 验证剪贴板写入字符串编码:转义内部单引号/反斜杠/双引号,
- // 并截断超长文本(binder 事务安全上限 1000 字符)。
- func TestClipboardServiceString(t *testing.T) {
- s := clipboardServiceString("it's a \"test\" \\ ok")
- want := `'it\'s a \"test\" \\ ok'`
- if s != want {
- t.Fatalf("clipboardServiceString() = %q, want %q", s, want)
- }
- // 超长截断
- long := strings.Repeat("a", 1500)
- if got := clipboardServiceString(long); len(got) != 1002 { // 1000 + 2 个引号
- t.Fatalf("超长文本未截断到 1000 字符,长度 = %d", len(got))
- }
- }
- // TestFingerprintDevices 验证设备集合指纹:空集为空串,非空集稳定且区分状态。
- func TestFingerprintDevices(t *testing.T) {
- if got := fingerprintDevices(nil); got != "" {
- t.Fatalf("fingerprintDevices(nil) = %q, want empty", got)
- }
- devs := []PhoneDevice{{Serial: "A", State: "device"}, {Serial: "B", State: "unauthorized"}}
- fp := fingerprintDevices(devs)
- if fp != "A=device;B=unauthorized;" {
- t.Fatalf("fingerprintDevices() = %q, want %q", fp, "A=device;B=unauthorized;")
- }
- // 状态变化应产生不同指纹(去抖依赖此差异)
- changed := []PhoneDevice{{Serial: "A", State: "device"}, {Serial: "B", State: "device"}}
- if fingerprintDevices(changed) == fp {
- t.Fatalf("fingerprintDevices() 应区分不同状态")
- }
- }
- // TestProbeInstalledADBSubdir 模拟 zip 解压出 platform-tools/ 子目录(含 adb.exe),
- // 验证 probeInstalled 能识别 adb 特判(候选名与顶层 .exe 扫描都无法命中该子目录)。
- func TestProbeInstalledADBSubdir(t *testing.T) {
- dir := t.TempDir()
- sub := filepath.Join(dir, "platform-tools")
- if err := os.MkdirAll(sub, 0o755); err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(filepath.Join(sub, "adb.exe"), []byte("MZ"), 0o755); err != nil {
- t.Fatal(err)
- }
- // 构造 adb 资源条目(与 defaultResources 中一致)
- var item ResourceItem
- for _, it := range defaultResources() {
- if it.Name == "adb" {
- item = it
- break
- }
- }
- if item.Name != "adb" {
- t.Fatal("defaultResources 中未找到 adb 条目")
- }
- if got := resourceSubdir(&item); got != "platform-tools" {
- t.Fatalf("resourceSubdir(adb) = %q, want platform-tools", got)
- }
- installed, ver, path := probeInstalled(item, dir)
- if !installed {
- t.Fatal("probeInstalled 未识别 platform-tools 子目录为已安装")
- }
- if ver != item.Version {
- t.Fatalf("probeInstalled version = %q, want %q", ver, item.Version)
- }
- if path != sub {
- t.Fatalf("probeInstalled path = %q, want %q", path, sub)
- }
- }
- // TestFindADBPathFallback 验证方案2:进程 PATH 不含 adb(模拟旧进程快照)时,
- // findADBPath 兜底从注册表用户级+系统级 PATH 命中 adb,Available() 因此可用。
- // 这是「PowerShell 能跑 adb 但程序搜不到」的根因回归测试。
- func TestFindADBPathFallback(t *testing.T) {
- orig := os.Getenv("Path")
- defer os.Setenv("Path", orig)
- // 模拟进程启动早于 adb 加入 PATH:进程 PATH 无任何 adb 目录
- os.Setenv("Path", `C:\Windows\System32;C:\Windows`)
- // 注册表 PATH 也无 adb 时跳过(无 adb 的机器上不误报)
- if _, err := exec.LookPath("adb"); err == nil {
- t.Fatal("干净 PATH 下 LookPath 不应命中(测试前提失效)")
- }
- path := findADBPath()
- if path == "" {
- t.Skip("注册表用户/系统 PATH 无 adb,跳过兜底断言")
- }
- t.Logf("兜底命中:%s", path)
- info := NewPhoneService().Available()
- if !info.Available {
- t.Fatalf("进程 PATH 无 adb 时 Available() 应兜底可用,得到 %+v", info)
- }
- if info.Path != path {
- t.Fatalf("Available() 路径 = %q, want %q", info.Path, path)
- }
- t.Logf("Available() 兜底成功:path=%s version=%q", info.Path, info.Version)
- }
|