forked from violetljj/blind-assist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_device_regression.ps1
More file actions
414 lines (360 loc) · 15.9 KB
/
Copy pathrun_device_regression.ps1
File metadata and controls
414 lines (360 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
param(
[string]$ApkPath = "app\build\outputs\apk\debug\app-debug.apk",
[string]$PackageName = "com.linnan.blindassist",
[string]$MainActivity = ".MainActivity",
[int]$SampleSeconds = 90,
[switch]$RunConnectedAndroidTest,
[string]$AdbPath
)
$ErrorActionPreference = "Stop"
function Resolve-RepoPath([string]$Path) {
if ([System.IO.Path]::IsPathRooted($Path)) {
return $Path
}
return (Join-Path $PSScriptRoot "..\$Path")
}
function Resolve-Adb([string]$RequestedPath) {
if ($RequestedPath) {
if (-not (Test-Path -LiteralPath $RequestedPath)) {
throw "ADB not found at $RequestedPath"
}
return (Resolve-Path -LiteralPath $RequestedPath).Path
}
$localAdb = Join-Path $PSScriptRoot "..\.android-sdk\platform-tools\adb.exe"
if (Test-Path -LiteralPath $localAdb) {
return (Resolve-Path -LiteralPath $localAdb).Path
}
$command = Get-Command adb -ErrorAction SilentlyContinue
if ($command) {
return $command.Source
}
throw "ADB not found. Pass -AdbPath or install platform-tools."
}
function Invoke-NativeAdb {
param(
[string]$Adb,
[string[]]$Arguments
)
$previousPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try {
$text = & $Adb @Arguments 2>&1
$code = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previousPreference
}
return [ordered]@{
Text = $text
Code = $code
}
}
function Invoke-Adb {
param(
[string]$Adb,
[string[]]$Arguments,
[string]$OutFile
)
$result = Invoke-NativeAdb $Adb $Arguments
$text = $result.Text
$code = $result.Code
if ($OutFile) {
$text | Out-File -FilePath $OutFile -Encoding utf8
} else {
$text
}
if ($code -ne 0) {
throw "adb $($Arguments -join ' ') failed with exit code $code"
}
return $text
}
function Get-SingleDevice([string]$Adb) {
$result = Invoke-NativeAdb $Adb @("devices")
$lines = $result.Text
if ($result.Code -ne 0) {
throw "adb devices failed: $($lines -join ' ')"
}
$devices = @(
$lines |
Where-Object { $_ -match "^\S+\s+device$" } |
ForEach-Object { ($_ -split "\s+")[0] }
)
if ($devices.Count -ne 1) {
throw "Expected exactly one online device, found $($devices.Count). Raw adb devices output: $($lines -join ' | ')"
}
return $devices[0]
}
function Get-UiHierarchy([string]$Adb, [string]$Device) {
$dump = Invoke-NativeAdb $Adb @("-s", $Device, "shell", "uiautomator", "dump", "/sdcard/blindassist-regression-ui.xml")
if ($dump.Code -ne 0) {
throw "uiautomator dump failed: $($dump.Text -join ' ')"
}
$content = Invoke-NativeAdb $Adb @("-s", $Device, "shell", "cat", "/sdcard/blindassist-regression-ui.xml")
if ($content.Code -ne 0) {
throw "reading uiautomator hierarchy failed: $($content.Text -join ' ')"
}
return [xml](($content.Text) -join "")
}
function Find-UiNodeByText([xml]$Hierarchy, [string[]]$Texts) {
foreach ($text in $Texts) {
$node = $Hierarchy.SelectNodes("//node") |
Where-Object {
$_.GetAttribute("text") -eq $text -or
$_.GetAttribute("content-desc") -eq $text
} |
Select-Object -First 1
if ($node) {
return $node
}
}
return $null
}
function Invoke-UiTapByText {
param(
[string]$Adb,
[string]$Device,
[string[]]$Texts,
[switch]$Optional
)
$hierarchy = Get-UiHierarchy $Adb $Device
$node = Find-UiNodeByText $hierarchy $Texts
if (-not $node) {
if ($Optional) {
return $false
}
throw "None of the expected UI texts were found: $($Texts -join ' | ')"
}
while ($node -and $node.GetAttribute("clickable") -ne "true") {
$node = $node.ParentNode
}
if (-not $node) {
throw "Expected UI text was found, but no clickable ancestor exists: $($Texts -join ' | ')"
}
$bounds = $node.GetAttribute("bounds")
if ($bounds -notmatch '^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$') {
throw "Unable to parse clickable UI bounds: $bounds"
}
$left = [int]$Matches[1]
$top = [int]$Matches[2]
$right = [int]$Matches[3]
$bottom = [int]$Matches[4]
$centerX = [int](($left + $right) / 2)
$centerY = [int](($top + $bottom) / 2)
Invoke-Adb $Adb @("-s", $Device, "shell", "input", "tap", "$centerX", "$centerY") $null | Out-Null
return $true
}
function Wait-ForUiText {
param(
[string]$Adb,
[string]$Device,
[string[]]$Texts,
[int]$TimeoutSeconds = 15
)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
$hierarchy = Get-UiHierarchy $Adb $Device
if (Find-UiNodeByText $hierarchy $Texts) {
return $hierarchy
}
Start-Sleep -Seconds 1
} while ((Get-Date) -lt $deadline)
throw "Timed out waiting for UI text: $($Texts -join ' | ')"
}
function Test-UiTextPresent {
param(
[xml]$Hierarchy,
[string[]]$Texts
)
return $null -ne (Find-UiNodeByText $Hierarchy $Texts)
}
function Test-CameraReadySemantics {
param([xml]$Hierarchy)
$nodes = @($Hierarchy.SelectNodes("//node"))
$texts = ($nodes | ForEach-Object {
"$($_.GetAttribute('text')) $($_.GetAttribute('content-desc'))"
}) -join "`n"
if ($texts -match "模型不可用|Model unavailable") {
throw "Camera UI reported that the model is unavailable"
}
$hasBackNavigation = Test-UiTextPresent $Hierarchy @("返回功能页", "Back to features")
$hasDetectionEnabled = $texts -match "检测.*(已开启|开启)|Detection.*(on|enabled)"
$hasStableGuidance = $texts -match (
"持续检测中|检测已开启|保持观察|前方.*(风险|近处|迫近)|请留意|Monitoring|Detection on|Keep observing|Risk ahead|Slow down"
)
return $hasBackNavigation -and $hasDetectionEnabled -and $hasStableGuidance
}
function Resolve-LaunchPrompts {
param(
[string]$Adb,
[string]$Device
)
if (Invoke-UiTapByText -Adb $Adb -Device $Device -Texts @("不再显示", "Don't show again", "Do not show again") -Optional) {
Start-Sleep -Milliseconds 500
return $true
}
if (Invoke-UiTapByText -Adb $Adb -Device $Device -Texts @("确定", "OK") -Optional) {
Start-Sleep -Milliseconds 500
return $true
}
if (Invoke-UiTapByText -Adb $Adb -Device $Device -Texts @("跳过引导", "Skip guide", "Skip onboarding") -Optional) {
Start-Sleep -Milliseconds 500
return $true
}
return $false
}
function Wait-ForStableCameraState {
param(
[string]$Adb,
[string]$Device,
[int]$TimeoutSeconds = 25
)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
Resolve-LaunchPrompts -Adb $Adb -Device $Device | Out-Null
$hierarchy = Get-UiHierarchy $Adb $Device
if (Test-CameraReadySemantics $hierarchy) {
return $hierarchy
}
Start-Sleep -Seconds 1
} while ((Get-Date) -lt $deadline)
throw "Timed out waiting for stable camera semantics (camera page, detection enabled, and live guidance)"
}
function Assert-ModelFramesAdvance {
param(
[string]$Adb,
[string]$Device,
[int]$TimeoutSeconds = 12
)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
$first = Invoke-NativeAdb $Adb @("-s", $Device, "logcat", "-d", "-s", "BlindAssistPerf")
$firstText = $first.Text -join "`n"
$firstReady = $firstText -match "status=(模型已加载|model loaded)"
$firstCount = @($first.Text | Where-Object { $_ -match "BlindAssistPerf:" }).Count
if ($firstReady) {
Start-Sleep -Seconds 2
$second = Invoke-NativeAdb $Adb @("-s", $Device, "logcat", "-d", "-s", "BlindAssistPerf")
$secondCount = @($second.Text | Where-Object { $_ -match "BlindAssistPerf:" }).Count
if ($secondCount -gt $firstCount) {
return @($first, $second)
}
}
Start-Sleep -Seconds 1
} while ((Get-Date) -lt $deadline)
throw "No continuously advancing model-loaded BlindAssistPerf frames were observed after camera preparation"
}
$repoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "..")).Path
$resolvedApk = Resolve-RepoPath $ApkPath
if (-not (Test-Path -LiteralPath $resolvedApk)) {
throw "APK not found: $resolvedApk"
}
$resolvedApk = (Resolve-Path -LiteralPath $resolvedApk).Path
$adb = Resolve-Adb $AdbPath
$device = Get-SingleDevice $adb
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$artifactRoot = Join-Path (Join-Path $repoRoot "test-artifacts.local\device-regression") $timestamp
New-Item -ItemType Directory -Force -Path $artifactRoot | Out-Null
$summary = [ordered]@{
timestamp = $timestamp
device = $device
apk = $resolvedApk
packageName = $PackageName
mainActivity = $MainActivity
sampleSeconds = $SampleSeconds
runConnectedAndroidTest = [bool]$RunConnectedAndroidTest
artifactRoot = $artifactRoot
}
try {
Invoke-Adb $adb @("devices", "-l") (Join-Path $artifactRoot "adb-devices.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "shell", "getprop") (Join-Path $artifactRoot "device-getprop.txt") | Out-Null
if ($RunConnectedAndroidTest) {
$gradleLauncher = Join-Path $PSScriptRoot "run_android_gradle.ps1"
$launcherParameters = @{
AndroidSerial = $device
GradleArguments = @(":app:connectedDebugAndroidTest")
}
& $gradleLauncher @launcherParameters 2>&1 |
Tee-Object -FilePath (
Join-Path $artifactRoot "connectedDebugAndroidTest.txt"
)
if ($LASTEXITCODE -ne 0) {
throw ":app:connectedDebugAndroidTest failed with exit code $LASTEXITCODE"
}
}
Invoke-Adb $adb @("-s", $device, "install", "-r", "-t", $resolvedApk) (Join-Path $artifactRoot "install.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "shell", "pm", "clear", $PackageName) (Join-Path $artifactRoot "pm-clear.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "shell", "pm", "grant", $PackageName, "android.permission.CAMERA") (Join-Path $artifactRoot "pm-grant-camera.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "logcat", "-c") $null | Out-Null
$component = if ($MainActivity.StartsWith(".")) { "$PackageName/$MainActivity" } else { "$PackageName/$MainActivity" }
Invoke-Adb $adb @("-s", $device, "shell", "am", "start", "-W", "-n", $component) (Join-Path $artifactRoot "cold-start.txt") | Out-Null
Start-Sleep -Seconds 2
while (Resolve-LaunchPrompts -Adb $adb -Device $device) { }
Invoke-UiTapByText -Adb $adb -Device $device -Texts @("使用手机摄像头", "Use phone camera") | Out-Null
$cameraHierarchy = Wait-ForStableCameraState -Adb $adb -Device $device
$cameraHierarchy.OuterXml | Out-File -FilePath (Join-Path $artifactRoot "camera-ready-ui.xml") -Encoding utf8
$modelFrameSamples = Assert-ModelFramesAdvance -Adb $adb -Device $device
$modelFrameSamples[0].Text | Out-File -FilePath (Join-Path $artifactRoot "BlindAssistPerf-ready-first.log") -Encoding utf8
$modelFrameSamples[1].Text | Out-File -FilePath (Join-Path $artifactRoot "BlindAssistPerf-ready-second.log") -Encoding utf8
$foreground = Invoke-NativeAdb $adb @("-s", $device, "shell", "dumpsys", "activity", "activities")
$foreground.Text | Out-File -FilePath (Join-Path $artifactRoot "foreground-activity.txt") -Encoding utf8
if (($foreground.Text -join "`n") -notmatch "(topResumedActivity|ResumedActivity|mResumedActivity).*$([regex]::Escape($PackageName))") {
throw "BlindAssist is not the resumed foreground activity after camera preparation"
}
$initialPerf = Invoke-NativeAdb $adb @("-s", $device, "logcat", "-d", "-s", "BlindAssistPerf")
$initialPerf.Text | Out-File -FilePath (Join-Path $artifactRoot "BlindAssistPerf-initial.log") -Encoding utf8
$initialPerfText = $initialPerf.Text -join "`n"
$initialPerfLineCount = @($initialPerf.Text | Where-Object { $_ -match "BlindAssistPerf:" }).Count
if ($initialPerfText -notmatch "status=(模型已加载|model loaded)") {
throw "No model-ready BlindAssistPerf frame was observed after entering the camera"
}
Invoke-Adb $adb @("-s", $device, "shell", "dumpsys", "package", $PackageName) (Join-Path $artifactRoot "dumpsys-package.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "shell", "cmd", "package", "dump", $PackageName) (Join-Path $artifactRoot "cmd-package-dump.txt") | Out-Null
$stopAt = (Get-Date).AddSeconds($SampleSeconds)
$iteration = 0
while ((Get-Date) -lt $stopAt) {
$iteration++
$suffix = "{0:D3}" -f $iteration
Invoke-Adb $adb @("-s", $device, "logcat", "-d", "-s", "BlindAssistPerf") (Join-Path $artifactRoot "BlindAssistPerf-$suffix.log") | Out-Null
Invoke-Adb $adb @("-s", $device, "shell", "dumpsys", "gfxinfo", $PackageName) (Join-Path $artifactRoot "gfxinfo-$suffix.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "shell", "dumpsys", "meminfo", $PackageName) (Join-Path $artifactRoot "meminfo-$suffix.txt") | Out-Null
Invoke-Adb $adb @("-s", $device, "exec-out", "uiautomator", "dump", "/dev/tty") (Join-Path $artifactRoot "ui-dump-$suffix.xml") | Out-Null
& $adb -s $device exec-out screencap -p > (Join-Path $artifactRoot "screenshot-$suffix.png")
if ($LASTEXITCODE -ne 0) {
throw "screenshot capture failed with exit code $LASTEXITCODE"
}
Start-Sleep -Seconds 15
}
$finalHierarchy = Wait-ForStableCameraState -Adb $adb -Device $device -TimeoutSeconds 8
$finalHierarchy.OuterXml | Out-File -FilePath (Join-Path $artifactRoot "camera-final-ui.xml") -Encoding utf8
$finalForeground = Invoke-NativeAdb $adb @("-s", $device, "shell", "dumpsys", "activity", "activities")
$finalForeground.Text | Out-File -FilePath (Join-Path $artifactRoot "foreground-activity-final.txt") -Encoding utf8
if (($finalForeground.Text -join "`n") -notmatch "(topResumedActivity|ResumedActivity|mResumedActivity).*$([regex]::Escape($PackageName))") {
throw "BlindAssist is not the resumed foreground activity after the sampling interval"
}
$finalPerf = Invoke-NativeAdb $adb @("-s", $device, "logcat", "-d", "-s", "BlindAssistPerf")
$finalPerf.Text | Out-File -FilePath (Join-Path $artifactRoot "BlindAssistPerf-final.log") -Encoding utf8
$finalPerfLineCount = @($finalPerf.Text | Where-Object { $_ -match "BlindAssistPerf:" }).Count
if ($finalPerfLineCount -le $initialPerfLineCount) {
throw "No new BlindAssistPerf frames were observed during the sampling interval"
}
$finalLogcat = Invoke-NativeAdb $adb @("-s", $device, "logcat", "-d", "-v", "time")
$finalLogcat.Text | Out-File -FilePath (Join-Path $artifactRoot "logcat-final.txt") -Encoding utf8
$finalLogText = $finalLogcat.Text -join "`n"
$escapedPackage = [regex]::Escape($PackageName)
$targetCrash = $finalLogText -match "(?s)FATAL EXCEPTION.{0,1200}Process:\s*$escapedPackage" -or
$finalLogText -match "ANR in $escapedPackage" -or
$finalLogText -match "am_crash.*$escapedPackage" -or
$finalLogText -match "Process $escapedPackage .* has died" -or
$finalLogText -match "(?s)Fatal signal.{0,1200}>>>\s*$escapedPackage\s*<<<"
if ($targetCrash) {
throw "Crash or ANR evidence was found in the final device log"
}
$summary.status = "passed"
} catch {
$summary.status = "failed"
$summary.error = $_.Exception.Message
throw
} finally {
$summary | ConvertTo-Json -Depth 5 | Out-File -FilePath (Join-Path $artifactRoot "summary.json") -Encoding utf8
"Device regression artifacts: $artifactRoot" | Out-File -FilePath (Join-Path $artifactRoot "README.txt") -Encoding utf8
Write-Host "Device regression artifacts: $artifactRoot"
}