ReviewOS

also looking at this

craft-native/craft

fix(android): resolve share() with whether the person picked an app

#210
Merged glennmichael123 wants to merge fix/android-share-outcome into main
12 files +399 -56
scripts/mobile-e2e/android.tsmodified+30-1
Changes to scripts/mobile-e2e/android.ts
@@ -2,7 +2,7 @@ import type { LegOutcome, RunnerOptions } from './types'
22import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
33import { join } from 'node:path'
44import { init } from '../../packages/android/src/index'
5import { androidDeclines, evaluateRun, hasTerminated } from './protocol'
5import { androidDeclines, awaitedNeeds, DISMISS_SHARE_MENU, evaluateRun, hasTerminated, shareMenuInFront } from './protocol'
66import { command, driverPage, waitForFile } from './support'
77
88/**
@@ -149,6 +149,24 @@ async function runLeg(leg: Leg, options: RunnerOptions): Promise<LegOutcome> {
149149 await adb(['shell', 'am', 'force-stop', PACKAGE], { serial, allowFailure: true })
150150 await adb(['shell', 'am', 'start', '-n', `${PACKAGE}/.MainActivity`], { serial, logPath: join(evidence, 'adb.log') })
151151
152 // The share case needs its menu dismissed, and nothing on the device will
153 // do that: the harness plays the person. It acts only once the page has
154 // asked and the menu is really in front, and it keeps a screenshot of the
155 // menu, which is the evidence that there was one to dismiss.
156 let shareMenu: 'not asked' | 'asked' | 'dismissed' = 'not asked'
157
158 async function dismissShareMenuWhenAsked(log: string): Promise<void> {
159 if (shareMenu === 'dismissed' || !awaitedNeeds(log).includes(DISMISS_SHARE_MENU)) return
160 shareMenu = 'asked'
161
162 const windows = await command(['adb', '-s', serial, 'shell', 'dumpsys', 'window'], { allowFailure: true })
163 if (!shareMenuInFront(windows.stdout)) return
164
165 await command(['adb', '-s', serial, 'exec-out', 'screencap', '-p'], { allowFailure: true, outPath: join(evidence, 'share-menu.png') })
166 await adb(['shell', 'input', 'keyevent', 'KEYCODE_BACK'], { serial, logPath: join(evidence, 'adb.log') })
167 shareMenu = 'dismissed'
168 }
169
152170 // logcat -d dumps and exits, so poll it into the evidence file rather than
153171 // streaming: a stream would have to be killed at exactly the right moment,
154172 // and the dump is cheap.
@@ -156,6 +174,7 @@ async function runLeg(leg: Leg, options: RunnerOptions): Promise<LegOutcome> {
156174 const finished = await waitForFile(logPath, options.timeoutMs, hasTerminated, async () => {
157175 const dumped = await command(['adb', '-s', serial, 'logcat', '-d', ...LOGCAT_FILTER], { allowFailure: true })
158176 writeFileSync(logPath, dumped.stdout)
177 await dismissShareMenuWhenAsked(dumped.stdout)
159178 })
160179
161180 await command(['adb', '-s', serial, 'exec-out', 'screencap', '-p'], { allowFailure: true, outPath: join(evidence, 'screen.png') })
@@ -170,6 +189,16 @@ async function runLeg(leg: Leg, options: RunnerOptions): Promise<LegOutcome> {
170189 const verdict = evaluateRun('android', logText, nonce)
171190 failures.push(...verdict.failures)
172191
192 // The page's case only sees that share resolved false. That is also what a
193 // share that never opened a menu at all would resolve, so the harness says
194 // whether there was a menu to dismiss.
195 if (verdict.planned.includes('share.dismissed.resolvesFalse')) {
196 if (shareMenu === 'not asked')
197 failures.push('the page never asked for the share menu to be dismissed, so the dismissal case did not reach the menu')
198 else if (shareMenu === 'asked')
199 failures.push('the page asked for the share menu to be dismissed, and no share menu ever came to the front')
200 }
201
173202 // The page reported the SDK level the bridge told it; the host asked the
174203 // device directly. Agreement means the value crossed the bridge rather than
175204 // being invented in JavaScript - this leg's substitute for the pasteboard