fix(mobile)!: use density-independent pixels on Android (ENG-1491)
What
This is a breaking change for Android tests. On Android, element boxes, device.viewport(), and the points passed to taps and swipes (screen.tapAt(), tap({ position }), screen.swipe(), and the agent's swipe tool) are now in density-independent pixels (dp), like iOS points, instead of physical pixels. It ships as a minor with **Breaking:** and no deprecation step, by maintainer decision.
const box = await screen.getByLabel('Tap pad').boundingBox();
if (box === null) throw new Error('pad has no box');
// about 240 by 100 dp at density 2.625 and 3.0, like iOS (630 by 263 px on main at 2.625)
await screen.tapAt({ x: box.x + 40, y: box.y + 30 }); // the app reads 40,30
What Android users have to change:
- Convert a hard-coded point to dp, or use a percentage in
screen.tapAt()andscreen.swipe(). - Run
--update-snapshotsfor storedtoHaveScreenshotbaselines, since the compared image is the dp viewport's size. - Re-record agent steps that tap or swipe at a point. Their recordings hold the old physical-pixel viewport, so they go stale (
viewport-changed).
device.unstable_raw stays in physical pixels.
Why
On Android, these values were the screen's physical pixels, while iOS used points. On main, the mobile reference listed this as a gap ("rect and every point are the screen's physical pixels; the display density it reports is not applied yet"), and boundingBox() said "physical pixels on Android". So a fixed point landed in a different place on screens of different density. agent-device reports the display density beside the Android rects (androidSnapshot.pixelDensity), so the engine converts with it.
How it works
- Each Android capture goes through
inDp(packages/mobile/src/surface.ts:1160,:1187). Node rects, the viewport, and the keyboard frame are divided by a per-axis factor. The viewport is rounded to whole dp (1080 px at density 2.625 is 411 dp), because the report schema and the replay cache store whole units, so each axis gets the factor that maps it back exactly. - A capture without screen geometry doesn't set a factor. A capture without a density (agent-device's older Android snapshot helper) keeps physical pixels. The learned viewport is dropped if the effective density changes, even to or from a capture without one (
surface.ts:1192). - Each point sent back is converted in
toDevicePoint(surface.ts:1216). Point actions are converted in the dispatch switch (packages/mobile/src/actions.ts:74,:86), and node swipes, the keyboard-dismiss drag, and the agent'sswipetool at their call sites. When agent-device refuses a swipe path as out of bounds, the compared screen size is converted to physical pixels (surface.ts:1849). readPixelScale(surface.ts:1229) captures the screen when no factor is known, fromperformAt(surface.ts:1840) and theswipetool (packages/mobile/src/tools.ts:156, with a 15 s budget attools.ts:27). A screen that can't be read yet is captured again until the budget runs out. Then the action fails withOPERATION_TIMEOUT(ACTION_FAILEDin a test) without sending input.- The factor is dropped before each test point action (
surface.ts:1839), so the next point converts accurately if the density changed. That costs one capture per test point action on Android.device.setOrientation()(packages/mobile/src/device.ts:337) anddevice.fold()(surface.ts:2066) drop it for the agent's point actions. - Secure-field masks scale each axis by its image-to-viewport ratio (
surface.ts:2237). App screenshots are masked with that capture's viewport (surface.ts:2147).ObservationPixels.scaleis the image's width plus height over the viewport's (surface.ts:2185,:2201), so the image divided by it rounds back to the viewport on both axes. That held for all 3,080,948 screen sizes and densities in a sweep, where the harmonic mean of the two axes missed 546,067. isSystemBarallows 0.5 units of slack at the bottom (packages/mobile/src/nodes.ts:564). After the dp conversion, a 48 dp bar on a 1080x2280 screen at density 2.75 ends at 828.9999999999999 of 829 dp, and the navigation bar's Back button showed up in the tree.- The mobile benchmark's "pointer verbs land at the point asked for" test now runs on Android. Its size checks are exact on iOS and within half a dp on Android (
apps/mobile-benchmark/tests/controls.e2e.ts:350). Swipe tolerance is 20 on Android and stays 15 on iOS (controls.e2e.ts:387), since Android flings can end at x=84 for a target of x=100.
locator.screenshot() in core still crops with one scale for both axes, so on Android its crop can be off by a few pixel rows near the bottom of a tall screen. Scaling each axis there conflicts with core's handling of a screenshot taller than the viewport, so it's left for a separate change.
Issue
Part of ENG-1434.
Verified
Ran it locally: yes
- Android: API 36
google_apisarm64 emulators, 1080x2400, at 420 dpi (density 2.625, CI'spixel_7) and at 480 dpi (hw.lcd.density=480, density 3.0). The benchmark's release APK was built the waymobile.ymlbuilds it. - iOS: an iPhone 18 Pro simulator on iOS 27.2. CI pins 26.5, which this Mac doesn't have.
The PR's pointer test on Android at 420 dpi, with main's mobile engine and with this PR's, e2e run tests/controls.e2e.ts --target android-emulator --grep 'pointer verbs':
| main's mobile engine | this PR's mobile engine |
|---|---|
failed, ASSERTION_FAILED: expected 630 to be close to 240 (0 digits) | passed |
main's engine: Tests 1 failed | 0 passed (1)
this PR's engine: Tests 1 passed (1)
Suites on this PR's code at aabd8a0c, before the later rebases onto main. Main's newer mobile engine commits add device.lock(), device.pressButton, node attributes for getAttribute, and argument checks for installApp and setLocation. None of them reads or sends screen coordinates, but the suites weren't rerun on them. The commits after that code commit (b3bf8ee9 after the rebase), up to the head 0a45d383, change only the changeset, docs, comments, an error message's wording, and test titles:
| Suite | Android 420 dpi | Android 480 dpi | iOS 27.2 |
|---|---|---|---|
tests/ | 64 passed, 1 skipped | 64 passed, 1 skipped | 59 passed, 5 failed, 1 skipped |
tests-agent/ with --strict-cache, no model key | 12 passed, 8 failed | not run | not run |
android 420 dpi, tests/: Tests 64 passed | 1 skipped (65)
android 480 dpi, tests/: Tests 64 passed | 1 skipped (65)
android 420 dpi, tests-agent/ --strict-cache: Tests 8 failed | 12 passed (20)
ios 27.2, tests/: Tests 5 failed | 59 passed | 1 skipped (65)
tests-agent/on Android had no model key:- The re-recorded Vision Only entry replays all three of its taps in dp and reaches "Sequence complete". The re-recorded Broken Accessibility entry replays its first tap, then hands its
typecall to the agent as before. No recording went stale from the change. - Seven tests failed at their first model call (
AI_GATEWAY_API_KEY is not set). - Permission Prompt failed with
target-not-found, because thetests/run before it had granted microphone access on that emulator, so the dialog didn't show. - An earlier run with a key, on
deb6a516, passed 19 of 20, and Permission Prompt passed alone afteradb shell pm revoke dev.e2e.benchmark android.permission.RECORD_AUDIO.
- The re-recorded Vision Only entry replays all three of its taps in dp and reaches "Sequence complete". The re-recorded Broken Accessibility entry replays its first tap, then hands its
- iOS: the five failures (four keyboard tests and the web view coupon form) also failed with main's mobile engine on the same iOS 27.2 simulator, in the run before these review fixes.
same 5 tests, main's mobile engine: Tests 5 failed | 0 passed (5)
same 5 tests, this PR's engine: Tests 5 failed | 0 passed (5)
pnpm bench:ab: not run. It runs a suite on one device through two builds, and the device runs above already compare main and this PR on the changed path. CI runners are too noisy for wall-time verdicts on mobile.- Video: not recorded. The runs were headless, and the pointer test asserts the exact points the app reports.
- Docs screenshots: not captured. The docs changes are wording in tables and paragraphs, with no layout change.
Checklist
- Changeset added (
pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. - Breaking change: title carries
!, the changeset body names what breaks and what replaces it. There's no deprecation window, by maintainer decision. - Docs updated in the same PR: the
docs/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.