Files
orca/src/main/emulator/android/android-capability-operations.test.ts
T
Kelvin Amoabaands546126 5ea047e34e fix(emulator): detect Android devices when only adb is installed (#37128)
* fix(emulator): detect Android devices with only adb installed

SDK discovery required both adb and the emulator binary under an SDK
root, so hosts with platform-tools only, or a standalone adb such as a
distro android-tools package, saw no Android devices at all. Discovery
now needs only adb, falls back to adb on PATH, and AVD listing and boot
are skipped when the emulator package is missing.

Fixes #32126

Co-authored-by: s546126 <268420947+s546126@users.noreply.github.com>

* test(emulator): drop type assertion in adb-only inventory test

---------

Co-authored-by: s546126 <268420947+s546126@users.noreply.github.com>
2026-11-01 20:10:20 +00:00

91 lines
3.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { AndroidCommandResult, AndroidCommandRunner } from './android-command-runner'
import type { AndroidSdkPaths } from './android-sdk-discovery'
import {
dumpAndroidAccessibilityTree,
installAndroidApk,
launchAndroidApp
} from './android-capability-operations'
const SDK: AndroidSdkPaths = {
sdkRoot: '/sdk',
adb: '/sdk/adb',
emulator: '/sdk/emulator'
}
const ok = (stdout: string): AndroidCommandResult => ({ stdout, stderr: '', code: 0 })
function runnerReturning(map: Record<string, AndroidCommandResult>): AndroidCommandRunner {
return (async (_binary: string, args: readonly string[]) =>
map[args.join(' ')] ?? ok('')) as unknown as AndroidCommandRunner
}
describe('installAndroidApk', () => {
it('installs and throws on a Failure stdout even with exit 0', async () => {
const good = vi.fn(runnerReturning({ '-s s install /a.apk': ok('Success') }))
await installAndroidApk(good as unknown as AndroidCommandRunner, SDK, 's', '/a.apk')
expect(good).toHaveBeenCalledWith(SDK.adb, ['-s', 's', 'install', '/a.apk'])
const bad = runnerReturning({ '-s s install /a.apk': ok('Failure [INSTALL_FAILED]') })
await expect(installAndroidApk(bad, SDK, 's', '/a.apk')).rejects.toMatchObject({
code: 'emulator_error'
})
})
})
describe('dumpAndroidAccessibilityTree', () => {
it.each([
{ stdout: '', stderr: 'ERROR: could not get idle state.' },
{ stdout: 'ERROR: could not get idle state.', stderr: 'WARNING: diagnostic output' }
])('rejects a zero-exit dump failure before reading stale XML: %j', async (diagnostic) => {
const runner = vi.fn<AndroidCommandRunner>().mockResolvedValueOnce({ ...diagnostic, code: 0 })
runner.mockResolvedValue(ok('<hierarchy><node text="Old screen"/></hierarchy>'))
await expect(dumpAndroidAccessibilityTree(runner, SDK, 's')).rejects.toMatchObject({
code: 'emulator_error',
message: expect.stringContaining('ERROR:')
})
expect(runner).toHaveBeenCalledTimes(1)
})
it('reads a successful dump despite unrelated stderr warnings', async () => {
const runner = vi.fn<AndroidCommandRunner>()
runner.mockResolvedValueOnce({
code: 0,
stdout: 'UI hierchary dumped to: /sdcard/window_dump.xml',
stderr: 'WARNING: diagnostic output'
})
runner.mockResolvedValueOnce(ok('<hierarchy><node text="Current screen"/></hierarchy>'))
const tree = await dumpAndroidAccessibilityTree(runner, SDK, 's')
expect(tree.children[0]).toMatchObject({ text: 'Current screen' })
expect(runner).toHaveBeenCalledTimes(2)
})
})
describe('launchAndroidApp', () => {
it.each([
{ stdout: '', stderr: 'Error type 3\nError: Activity class {com.x/.Missing} does not exist.' },
{ stdout: 'Error: Activity class {com.x/.Missing} does not exist.', stderr: '' }
])('rejects a zero-exit activity launch failure: %j', async (diagnostic) => {
const runner = vi.fn<AndroidCommandRunner>().mockResolvedValue({ ...diagnostic, code: 0 })
await expect(launchAndroidApp(runner, SDK, 's', 'com.x', '.Missing')).rejects.toMatchObject({
code: 'emulator_error',
message: expect.stringContaining('Error:')
})
})
it('accepts a successful launch with a brought-to-front warning', async () => {
const runner = vi.fn<AndroidCommandRunner>().mockResolvedValue({
code: 0,
stdout: 'Starting: Intent { cmp=com.Error/.ErrorActivity }',
stderr: 'Warning: Activity not started, its current task has been brought to the front'
})
await expect(
launchAndroidApp(runner, SDK, 's', 'com.Error', '.ErrorActivity')
).resolves.toBeUndefined()
})
})