diff --git a/packages/capture-kit/src/react-native-overlay.test.ts b/packages/capture-kit/src/react-native-overlay.test.ts index f2c54e4837..c82fad4fec 100644 --- a/packages/capture-kit/src/react-native-overlay.test.ts +++ b/packages/capture-kit/src/react-native-overlay.test.ts @@ -347,3 +347,647 @@ describe('React Native collapsed banner with the entry-count badge', () => { expect(analyzeReactNativeOverlay(nodes).detected).toBe(false); }); }); + +/** + * Shapes from an iOS 27 capture of React Native 0.88 with the app's own `[button] "Close"` and + * `[button] "Dismiss"` on screen, where the overlay controls are LogBox's. Tests marked synthetic + * build app layouts and Android or RedBox shapes from their layout code instead. + */ +describe('React Native overlay dismissal beside app Close and Dismiss buttons', () => { + const viewport = snapshotViewportSizeFrom({ x: 0, y: 0, width: 402, height: 874 }); + const appButtons = [ + node({ + index: 9, + ref: 'e10', + type: 'Button', + label: 'Close', + rect: { x: 18, y: 231, width: 73, height: 45 }, + hittable: true, + }), + node({ + index: 10, + ref: 'e11', + type: 'Button', + label: 'Dismiss', + rect: { x: 99, y: 231, width: 89, height: 45 }, + hittable: true, + }), + ]; + const collapsedBanner = node({ + index: 48, + ref: 'e49', + type: 'Other', + label: '!, agent-device overlay check', + rect: { x: 10, y: 788, width: 382, height: 48 }, + hittable: true, + }); + const componentStackText = node({ + index: 50, + ref: 'e51', + type: 'StaticText', + label: 'Component Stack', + rect: { x: 12, y: 659, width: 378, height: 20 }, + }); + const footerTile = (index: number, label: string, x: number, extra: Partial = {}) => + node({ + index, + ref: `e${index + 1}`, + type: 'Other', + label, + rect: { x, y: 792, width: 134, height: 48 }, + hittable: false, + ...extra, + }); + /** LogBox's inspector footer as the XCTest capture lists it: three `flex: 1` tiles. */ + const inspectorFooter = [ + footerTile(59, 'Dismiss', 0), + footerTile(60, 'Minimize', 134), + footerTile(61, 'Copy', 268), + ]; + + test('a collapsed banner is closed at its own affordance, not at an app Close or Dismiss', () => { + const state = analyzeReactNativeOverlay([...appButtons, collapsedBanner], viewport); + + expect(state.primaryAction).toMatchObject({ + action: 'close-collapsed-banner', + ref: 'e49', + point: { x: 370, y: 812 }, + }); + }); + + test.each(['Dismiss', '×', 'Close'])( + 'synthetic: an app %s under the floating banner is not the banner close', + (label) => { + // LogBox floats over app content, so an app control can sit inside the banner's frame. + const state = analyzeReactNativeOverlay( + [ + collapsedBanner, + node({ + index: 60, + ref: 'e60', + type: 'Button', + label, + parentIndex: 1, + rect: { x: 300, y: 796, width: 80, height: 32 }, + hittable: true, + }), + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }, + ); + + test('an app Dismiss beside inspector text alone is not a target', () => { + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + node({ + index: 84, + ref: 'e84', + type: 'StaticText', + label: 'Call Stack', + rect: { x: 12, y: 300, width: 90, height: 24 }, + }), + ], + viewport, + ); + + expect(state.detected).toBe(true); + expect(state.primaryAction).toBe(null); + }); + + test('the inspector footer Dismiss wins over app buttons whatever its role', () => { + const state = analyzeReactNativeOverlay( + [...appButtons, componentStackText, ...inspectorFooter, collapsedBanner], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e60', + point: { x: 67, y: 816 }, + }); + }); + + test('synthetic: a full-width app bar listed before the inspector footer loses to it', () => { + // The simulator accessibility capture lists the LogBox window after the app's. + const bar = (index: number, label: string, x: number) => + node({ + index, + ref: `e${index}`, + type: 'Button', + label, + rect: { x, y: 100, width: 201, height: 44 }, + }); + const state = analyzeReactNativeOverlay( + [bar(20, 'Dismiss', 0), bar(21, 'Copy', 201), componentStackText, ...inspectorFooter], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'dismiss', ref: 'e60' }); + }); + + test.each([ + { direction: 'left-to-right', minimizeX: 134, copyX: 268, tapX: 67 }, + { direction: 'right-to-left', minimizeX: 134, copyX: 0, tapX: 335 }, + ])( + 'a $direction footer folded into its Dismiss row is tapped in the Dismiss column', + ({ minimizeX, copyX, tapX }) => { + // The simulator accessibility capture labels the footer row with its first tile and nests + // Minimize and Copy inside it, so the row's center would land on Minimize. Left to right is + // from the device; right to left mirrors it. + const row = { y: 792, width: 134, height: 82 }; + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + componentStackText, + node({ + index: 64, + ref: 'e65', + type: 'Other', + label: 'Dismiss', + rect: { x: 0, y: 792, width: 402, height: 82 }, + hittable: true, + }), + node({ index: 65, ref: 'e66', label: 'Minimize', rect: { ...row, x: minimizeX } }), + node({ index: 66, ref: 'e67', label: 'Copy', rect: { ...row, x: copyX } }), + { ...collapsedBanner, rect: { x: 10, y: 786.67, width: 382, height: 67.33 } }, + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e65', + point: { x: tapX, y: 833 }, + }); + }, + ); + + test('synthetic: a right-to-left footer keeps its Dismiss across float32 tile edges', () => { + // A 430pt window splits into thirds that the tree reports as float32. + const tile = (index: number, label: string, x: number) => + node({ + index, + ref: `e${index}`, + label, + rect: { x, y: 850, width: 143.3333282470703, height: 48 }, + }); + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + componentStackText, + tile(59, 'Copy', 0), + tile(60, 'Minimize', 143.33334350585938), + tile(61, 'Dismiss', 286.6666564941406), + ], + snapshotViewportSizeFrom({ x: 0, y: 0, width: 430, height: 932 }), + ); + + expect(state.primaryAction).toMatchObject({ action: 'dismiss', ref: 'e61' }); + }); + + test('synthetic: an app row with its own Reload and Dismiss is not the inspector footer', () => { + const state = analyzeReactNativeOverlay( + [ + node({ + index: 30, + ref: 'e30', + type: 'Button', + label: 'Reload', + rect: { x: 18, y: 300, width: 80, height: 44 }, + hittable: true, + }), + node({ + index: 31, + ref: 'e31', + type: 'Button', + label: 'Dismiss', + rect: { x: 106, y: 300, width: 89, height: 44 }, + hittable: true, + }), + componentStackText, + ...inspectorFooter, + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'dismiss', ref: 'e60' }); + }); + + test.each([ + { shape: 'an inset segmented pair in portrait', width: 402, height: 874, x: 66 }, + { shape: 'an alert button pair in landscape', width: 874, height: 402, x: 302 }, + ])('synthetic: $shape of flush Reload and Dismiss is not a footer', ({ width, height, x }) => { + const button = (index: number, label: string, left: number) => + node({ + index, + ref: `e${index}`, + type: 'Button', + label, + rect: { x: left, y: 300, width: 135, height: 44 }, + hittable: true, + }); + const state = analyzeReactNativeOverlay( + [button(30, 'Reload', x), button(31, 'Dismiss', x + 135), collapsedBanner], + snapshotViewportSizeFrom({ x: 0, y: 0, width, height }), + ); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }); + + test('synthetic: an app Dismiss centered on screen with no footer partners is not a footer', () => { + const state = analyzeReactNativeOverlay( + [ + node({ + index: 30, + ref: 'e30', + type: 'Button', + label: 'Dismiss', + rect: { x: 151, y: 500, width: 100, height: 44 }, + hittable: true, + }), + collapsedBanner, + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }); + + test('synthetic: a footer row with a second Copy off its column is not read as a footer', () => { + const state = analyzeReactNativeOverlay( + [ + componentStackText, + node({ + index: 70, + ref: 'e70', + label: 'Copy', + rect: { x: 300, y: 792, width: 60, height: 48 }, + }), + ...inspectorFooter, + ], + viewport, + ); + + expect(state.detected).toBe(true); + expect(state.primaryAction).toBe(null); + }); + + test('without a surface, a three-column footer is still read', () => { + // macOS desktop captures publish no viewport. + const state = analyzeReactNativeOverlay([ + componentStackText, + ...inspectorFooter, + collapsedBanner, + ]); + + expect(state.primaryAction).toMatchObject({ action: 'dismiss', ref: 'e60', point: { x: 67 } }); + }); + + test("synthetic: without a surface, an app's flush two-button row is not a footer", () => { + const button = (index: number, label: string, x: number) => + node({ index, ref: `e${index}`, label, rect: { x, y: 300, width: 135, height: 44 } }); + const state = analyzeReactNativeOverlay([ + button(30, 'Reload', 66), + button(31, 'Dismiss', 201), + collapsedBanner, + ]); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }); + + test('synthetic: a malformed footer row does not fall back to the banner under it', () => { + const state = analyzeReactNativeOverlay( + [ + componentStackText, + inspectorFooter[0]!, + footerTile(60, 'Minimize', 113), + inspectorFooter[2]!, + collapsedBanner, + ], + viewport, + ); + + expect(state.detected).toBe(true); + expect(state.primaryAction).toBe(null); + }); + + test('synthetic: an app Close beside a media player Minimize leaves the banner close', () => { + const state = analyzeReactNativeOverlay( + [footerTile(30, 'Minimize', 134), footerTile(31, 'Close', 0), collapsedBanner], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }); + + test.each([ + { shape: 'stops short of the left edge', start: 20, width: 127.33 }, + { shape: 'stops short of the right edge', start: 0, width: 127.33 }, + { shape: 'runs past the left edge', start: -20, width: 140.67 }, + ])('synthetic: a portrait footer row that $shape is not read', ({ start, width }) => { + const tiles = ['Dismiss', 'Minimize', 'Copy'].map((label, column) => + node({ + index: 59 + column, + ref: `e${60 + column}`, + label, + rect: { x: start + column * width, y: 792, width, height: 48 }, + }), + ); + const state = analyzeReactNativeOverlay( + [componentStackText, ...tiles, collapsedBanner], + viewport, + ); + + expect(state.primaryAction).toBe(null); + }); + + test('synthetic: an app Dismiss and a media player Minimize on different rows leave the banner close', () => { + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + node({ + index: 30, + ref: 'e30', + type: 'Button', + label: 'Minimize', + rect: { x: 18, y: 400, width: 100, height: 44 }, + hittable: true, + }), + collapsedBanner, + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }); + + test('synthetic: a Close tile beside Minimize and Copy is not the footer Dismiss', () => { + const state = analyzeReactNativeOverlay( + [componentStackText, footerTile(59, 'Close', 0), ...inspectorFooter.slice(1)], + viewport, + ); + + expect(state.primaryAction).toBe(null); + }); + + test('synthetic: an app container titled with overlay words does not vouch for its Close', () => { + const state = analyzeReactNativeOverlay( + [ + node({ + index: 1, + ref: 'e1', + type: 'Other', + label: 'Runtime error', + rect: { x: 16, y: 300, width: 370, height: 200 }, + }), + node({ + index: 2, + ref: 'e2', + type: 'Button', + label: 'Close', + parentIndex: 1, + rect: { x: 334, y: 308, width: 44, height: 44 }, + hittable: true, + }), + node({ + index: 3, + ref: 'e3', + type: 'StaticText', + label: 'Call Stack', + parentIndex: 1, + rect: { x: 24, y: 360, width: 90, height: 24 }, + }), + ], + viewport, + ); + + expect(state.detected).toBe(true); + expect(state.primaryAction).toBe(null); + }); + + test('synthetic: an app "Dismiss (Esc)" button beside a banner is not RedBox', () => { + const state = analyzeReactNativeOverlay( + [ + node({ + index: 30, + ref: 'e30', + type: 'Button', + label: 'Dismiss (Esc)', + rect: { x: 18, y: 400, width: 120, height: 44 }, + hittable: true, + }), + collapsedBanner, + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ action: 'close-collapsed-banner', ref: 'e49' }); + }); + + test("synthetic: RedBox's footer Dismiss wins over an app Dismiss button", () => { + // RCTRedBoxController: four buttons in a fill-equally stack view across the window. + const redBoxButton = (index: number, label: string, x: number) => + node({ + index, + ref: `e${index}`, + type: 'Button', + label, + rect: { x, y: 792, width: 100.5, height: 48 }, + hittable: true, + }); + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + node({ index: 1, ref: 'e1', label: 'App.tsx:42:7' }), + redBoxButton(40, 'Dismiss\n(ESC)', 0), + redBoxButton(41, 'Reload\n(⌘R)', 100.5), + redBoxButton(42, 'Copy\n(⌥⌘C)', 201), + redBoxButton(43, 'Extra Info\n(⌘E)', 301.5), + ], + viewport, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e40', + point: { x: 50, y: 816 }, + }); + }); + + /** + * Landscape shapes from the same device. Each footer button sits in its own SafeAreaView, so the + * outer tiles lose the 62 pt side insets: the XCTest capture lists tiles at 62, 291.33 and 582.67, + * and the simulator accessibility capture folds them into a full-width row labelled Dismiss. + */ + describe('iPhone landscape inspector footer', () => { + const landscape = snapshotViewportSizeFrom({ x: 0, y: 0, width: 874, height: 402 }); + const tile = (index: number, label: string, x: number, width: number, height = 48) => + node({ index, ref: `e${index + 1}`, label, rect: { x, y: 334, width, height } }); + + test('the inset tiles are read as the footer', () => { + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + componentStackText, + tile(15, 'Dismiss', 62, 229.33), + tile(16, 'Minimize', 291.33, 291.33), + tile(17, 'Copy', 582.67, 229.33), + ], + landscape, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e16', + point: { x: 177, y: 358 }, + }); + }); + + test('a folded row is tapped in its Dismiss column', () => { + const state = analyzeReactNativeOverlay( + [ + ...appButtons, + componentStackText, + tile(41, 'Dismiss', 0, 874, 68), + tile(42, 'Minimize', 291.33, 291.33, 68), + tile(43, 'Copy', 582.67, 291.33, 68), + ], + landscape, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e42', + point: { x: 146, y: 368 }, + }); + }); + + test('synthetic: a row inset past the landscape side inset is not a footer', () => { + const state = analyzeReactNativeOverlay( + [ + componentStackText, + tile(15, 'Dismiss', 132, 180), + tile(16, 'Minimize', 312, 180), + tile(17, 'Copy', 492, 180), + ], + landscape, + ); + + expect(state.primaryAction).toBe(null); + }); + }); + + /** + * Android publishes physical pixels. The footer's Pressable carries its text as content + * description, but a capture may list only the centered TextViews, or both. + */ + describe('synthetic Android inspector footer', () => { + const surface = { width: 1080, height: 2400, rectUnitsPerDp: 2.625 }; + const text = (index: number, label: string, centerX: number, width: number) => + node({ + index, + ref: `e${index}`, + type: 'android.widget.TextView', + label, + rect: { x: centerX - width / 2, y: 2213, width, height: 53 }, + }); + const footerTexts = [ + // The Dismiss text sits a few pixels off its column's center, within 2 dp at this density. + text(70, 'Dismiss', 183, 160), + text(71, 'Minimize', 540, 182), + text(72, 'Copy', 900, 102), + ]; + const footerTiles = ['Dismiss', 'Minimize', 'Copy'].map((label, column) => + node({ + index: 80 + column, + ref: `e${80 + column}`, + type: 'android.view.ViewGroup', + label, + rect: { x: column * 360, y: 2176, width: 360, height: 126 }, + hittable: true, + }), + ); + const banner = node({ + index: 90, + ref: 'e90', + label: '!, agent-device overlay check', + rect: { x: 26, y: 2148, width: 1028, height: 126 }, + hittable: true, + }); + const appDismiss = node({ + index: 20, + ref: 'e20', + type: 'android.widget.Button', + label: 'Dismiss', + rect: { x: 260, y: 600, width: 230, height: 118 }, + hittable: true, + }); + + test('only the footer text is listed: the Dismiss text is tapped, not the banner below', () => { + const state = analyzeReactNativeOverlay( + [appDismiss, componentStackText, ...footerTexts, banner], + surface, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e70', + point: { x: 183, y: 2240 }, + }); + }); + + test('tiles and their text are both listed: Dismiss is still one column', () => { + const state = analyzeReactNativeOverlay( + [ + appDismiss, + componentStackText, + node({ index: 99, ref: 'e99', label: 'Dismiss' }), + ...footerTiles, + ...footerTexts, + node({ index: 98, ref: 'e98', label: 'Minimize' }), + banner, + ], + surface, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e70', + point: { x: 183 }, + }); + }); + + test('in landscape, a footer inset by the cutout and the navigation bar is still read', () => { + // LogBoxDialog margins the root by the cutout (110 px) and the side navigation bar (126 px). + const columnWidth = (2400 - 110 - 126) / 3; + const tile = (index: number, label: string, column: number) => + node({ + index, + ref: `e${index}`, + label, + rect: { x: 110 + column * columnWidth, y: 940, width: columnWidth, height: 126 }, + hittable: true, + }); + const state = analyzeReactNativeOverlay( + [ + appDismiss, + componentStackText, + tile(80, 'Dismiss', 0), + tile(81, 'Minimize', 1), + tile(82, 'Copy', 2), + ], + { width: 2400, height: 1080, rectUnitsPerDp: 2.625 }, + ); + + expect(state.primaryAction).toMatchObject({ + action: 'dismiss', + ref: 'e80', + point: { x: 471 }, + }); + }); + }); +}); diff --git a/packages/capture-kit/src/react-native-overlay.ts b/packages/capture-kit/src/react-native-overlay.ts index 664105e29f..cb31eef7f5 100644 --- a/packages/capture-kit/src/react-native-overlay.ts +++ b/packages/capture-kit/src/react-native-overlay.ts @@ -4,7 +4,6 @@ import { type Rect, type SnapshotNode, } from '@agent-device/kernel/snapshot'; -import { rectArea } from '@agent-device/kernel/rect'; import { isReactNativeCollapsedWarningBanner, isReactNativeCollapsedWarningCountBadgeBanner, @@ -27,7 +26,7 @@ export type ReactNativeOverlayState = { }; export type ReactNativeOverlayDismissTarget = { - action: 'close' | 'dismiss' | 'close-collapsed-banner'; + action: 'dismiss' | 'close-collapsed-banner'; point: Point; rect?: Rect; ref?: string; @@ -40,6 +39,9 @@ type ReactNativeOverlayFacts = { minimizeNodes: SnapshotNode[]; collapsedNodes: SnapshotNode[]; collapsedCountBadgeNodes: SnapshotNode[]; + /** Minimize, Reload, Copy and Extra Info: the controls that share a footer with Dismiss. */ + footerPartnerNodes: SnapshotNode[]; + surface: ReactNativeOverlaySurface | undefined; detected: boolean; }; @@ -50,6 +52,21 @@ const REACT_NATIVE_STACK_FRAME_PATTERNS = [ /\b[\w.$<>/-]+\.(?:tsx?|jsx?)\s+\(\d+:\d+\)/, ] as const; const CLOSE_ICON_LABELS = new Set(['x', '×', '✕', '✖', '⨯']); +const FOOTER_PARTNER_PATTERN = /^(?:minimi[sz]e|reload(?: js)?|copy|extra info)(?:\s*\([^)]*\))?$/; +/** Text centered in its column can sit a rounding step off the column's center. */ +const FOOTER_ALIGNMENT_TOLERANCE_DP = 2; +/** + * The widest side gap a footer keeps from the surface edge in landscape. iOS wraps each footer button + * in a SafeAreaView, and an iPhone's landscape side inset is at most 62 pt; Android's LogBoxDialog and + * RedBoxDialogSurfaceDelegate margin the root by the system-bar and cutout insets, 48 dp for a side + * navigation bar. An app's centered button pair sits hundreds of points in. + */ +const FOOTER_LANDSCAPE_SIDE_INSET_MAX_DP = 80; +/** + * Without a surface the side gaps cannot be measured, so only a footer of three or more columns is + * read: LogBox's has three and RedBox's four, while an app's two-button row would pass the spacing. + */ +const FOOTER_MIN_COLUMNS_WITHOUT_SURFACE = 3; export function formatReactNativeOverlayWarning( nodes: SnapshotNode[], @@ -103,6 +120,8 @@ function collectReactNativeOverlayFacts( minimizeNodes, collapsedNodes, collapsedCountBadgeNodes, + footerPartnerNodes: collectOverlayNodes(nodes, (label) => FOOTER_PARTNER_PATTERN.test(label)), + surface, detected: isReactNativeOverlayDetected({ text, hasReactNativeStackFrame, @@ -134,11 +153,25 @@ function isCollapsedWarningNode( ); } +/** + * Apps name their own buttons Dismiss and Close, so the only control taken is the footer Dismiss of + * LogBox's inspector or of RedBox. A collapsed banner is closed at its own affordance, except while + * the inspector's Dismiss and Minimize are on screen: the banner then lies under the inspector. + */ function resolveSafeDismissAction( facts: ReactNativeOverlayFacts, ): ReactNativeOverlayDismissTarget | null { - const dismiss = firstControlNodeWithRect(facts.dismissNodes); - if (dismiss) return targetFromNode(dismiss, actionFromDismissNode(dismiss)); + const footer = findOverlayFooterDismiss(facts); + if (footer) { + return { + action: 'dismiss', + point: centerOfRect(footer.column), + rect: footer.column, + ref: footer.node.ref, + label: readNodeLabel(footer.node), + }; + } + if (isInspectorFooterVocabularyOnScreen(facts)) return null; const collapsed = chooseCollapsedWarningNode( facts.collapsedNodes.filter((node) => isSafeCollapsedWarningCoordinateFallback(node, facts)), @@ -254,43 +287,126 @@ function collectOverlayNodes( return nodes.filter((node) => node.ref && nodeHasMatchingLabel(node, matches)); } -function firstControlNodeWithRect(nodes: SnapshotNode[]): SnapshotNode | null { - return selectHighestScoredNode(nodes, controlNodeScores); -} +type OverlayFooterDismiss = { node: SnapshotNode; column: Rect }; +type RectNode = SnapshotNode & { rect: Rect }; -function isSemanticControlNode(node: ReactNativeOverlayNode): boolean { - const roleText = [node.type, node.role, node.subrole].join(' ').toLowerCase(); - return /\b(button|menuitem|link)\b/.test(roleText); +/** + * LogBox's inspector footer (`flex: 1` buttons) and RedBox's footer (a fill-equally stack view on + * iOS, weighted buttons on Android) give each control an equal column across one row, so Dismiss and + * its partners sit centered at an even spacing. The row may stop short of the surface edges only by + * the side inset a landscape window keeps. A capture hands over the full tile, only its text, or a + * row it folded the Dismiss tile into, labelled Dismiss and holding the other tiles; the target is + * Dismiss's column in each case. + * + * Candidates are tried from the end of the capture. The simulator accessibility capture lists the + * LogBox and RedBox windows after the app's, so an app bar shaped like a footer loses to the real one + * there, and a row is listed before the tiles it holds, so a tile wins over the row folded around it. + */ +function findOverlayFooterDismiss(facts: ReactNativeOverlayFacts): OverlayFooterDismiss | null { + const tolerance = footerTolerance(facts.surface); + const dismissNodes = facts.dismissNodes.filter( + (node): node is RectNode => + node.rect !== undefined && nodeHasMatchingLabel(node, isReactNativeOverlayDismissLabel), + ); + for (const node of [...dismissNodes].reverse()) { + const column = + footerDismissColumn(node.rect, false, facts, tolerance) ?? + footerDismissColumn(node.rect, true, facts, tolerance); + if (column) return { node, column }; + } + return null; } -function controlNodeScores(node: SnapshotNode): number[] { - return [ - booleanScore(isSemanticControlNode(node)), - booleanScore(node.hittable), - -(node.rect ? rectArea(node.rect) : Number.POSITIVE_INFINITY), - ]; +function footerDismissColumn( + rect: Rect, + folded: boolean, + facts: ReactNativeOverlayFacts, + tolerance: number, +): Rect | undefined { + const partnerCenters = footerPartnerCenters(rect, facts.footerPartnerNodes, tolerance); + if (!partnerCenters || partnerCenters.length === 0) return undefined; + let dismissCenter = rect.x + rect.width / 2; + if (folded) { + // The Dismiss slot is the column before the partners, or after them right to left. + if (partnerCenters.length < 2) return undefined; + const spacing = partnerCenters[1]! - partnerCenters[0]!; + const slot = [partnerCenters[0]! - spacing, partnerCenters.at(-1)! + spacing].find( + (center) => center > rect.x && center < rect.x + rect.width, + ); + if (slot === undefined) return undefined; + dismissCenter = slot; + } + const centers = [...partnerCenters, dismissCenter].sort((left, right) => left - right); + const first = centers[0]!; + const spacing = (centers.at(-1)! - first) / (centers.length - 1); + if (centers.some((center, column) => Math.abs(center - (first + column * spacing)) > tolerance)) { + return undefined; + } + const rowStart = first - spacing / 2; + const rowEnd = centers.at(-1)! + spacing / 2; + if (!hasFooterSideGaps(rowStart, rowEnd, centers.length, facts.surface, tolerance)) { + return undefined; + } + return { x: dismissCenter - spacing / 2, y: rect.y, width: spacing, height: rect.height }; } -function targetFromNode( - node: SnapshotNode, - action: ReactNativeOverlayDismissTarget['action'], -): ReactNativeOverlayDismissTarget { - if (!node.rect) { - throw new Error('React Native overlay target node must have rect'); +/** + * One center per partner name in the row, sorted. A capture can list a control twice, as its tile + * and as its text, so a name counts once; a name at two positions is not a footer. + */ +function footerPartnerCenters( + rect: Rect, + partners: SnapshotNode[], + tolerance: number, +): number[] | undefined { + const centersByName = new Map(); + for (const partner of partners) { + if (!partner.rect || !isSameRow(partner.rect, rect, tolerance)) continue; + const name = readNodeLabel(partner)?.trim().toLowerCase().split(/[\s(]/)[0] ?? ''; + const center = partner.rect.x + partner.rect.width / 2; + const known = centersByName.get(name); + if (known !== undefined && Math.abs(known - center) > tolerance) return undefined; + centersByName.set(name, center); } - return { - action, - point: centerOfRect(node.rect), - rect: node.rect, - ref: node.ref, - label: readNodeLabel(node), - }; + return [...centersByName.values()].sort((left, right) => left - right); +} + +function hasFooterSideGaps( + rowStart: number, + rowEnd: number, + columns: number, + surface: ReactNativeOverlaySurface | undefined, + tolerance: number, +): boolean { + if (!surface) return columns >= FOOTER_MIN_COLUMNS_WITHOUT_SURFACE; + // Portrait has no side safe-area inset, so the row meets both edges. + const maxGap = + surface.width > surface.height + ? FOOTER_LANDSCAPE_SIDE_INSET_MAX_DP * (surface.rectUnitsPerDp ?? 1) + : tolerance; + return [rowStart, surface.width - rowEnd].every((gap) => gap >= -tolerance && gap <= maxGap); +} + +function footerTolerance(surface: ReactNativeOverlaySurface | undefined): number { + return FOOTER_ALIGNMENT_TOLERANCE_DP * (surface?.rectUnitsPerDp ?? 1); } -function actionFromDismissNode(node: SnapshotNode): ReactNativeOverlayDismissTarget['action'] { - const label = readNodeLabel(node)?.trim().toLowerCase(); - if (label && isReactNativeOverlayDismissLabel(label)) return 'dismiss'; - return 'close'; +function isSameRow(rect: Rect, other: Rect, tolerance: number): boolean { + return Math.abs(rect.y + rect.height / 2 - (other.y + other.height / 2)) <= tolerance; +} + +/** The inspector's Dismiss and Minimize share its footer row; the banner it opened from stays below. */ +function isInspectorFooterVocabularyOnScreen(facts: ReactNativeOverlayFacts): boolean { + const tolerance = footerTolerance(facts.surface); + return facts.dismissNodes.some( + (dismiss) => + dismiss.rect !== undefined && + nodeHasMatchingLabel(dismiss, isReactNativeOverlayDismissLabel) && + facts.minimizeNodes.some( + (minimize) => + minimize.rect !== undefined && isSameRow(minimize.rect, dismiss.rect!, tolerance), + ), + ); } function chooseCollapsedWarningNode(nodes: SnapshotNode[]): SnapshotNode | null { diff --git a/packages/daemon/src/execution/handlers/__tests__/react-native.test.ts b/packages/daemon/src/execution/handlers/__tests__/react-native.test.ts index 9c96389c2c..4b4ea2a10e 100644 --- a/packages/daemon/src/execution/handlers/__tests__/react-native.test.ts +++ b/packages/daemon/src/execution/handlers/__tests__/react-native.test.ts @@ -26,6 +26,18 @@ const mockCaptureSnapshot = vi.mocked(captureSnapshot); */ const mockDismissTap = mockTapPoint; +/** RCTRedBoxController's footer: one button per label in a fill-equally stack across the window. */ +function redBoxFooter(labels: string[], y: number, windowWidth: number) { + const width = windowWidth / labels.length; + return labels.map((label, column) => ({ + index: column + 1, + ref: `e${column + 2}`, + type: 'XCUIElementTypeButton', + label, + rect: { x: column * width, y, width, height: 44 }, + })); +} + beforeEach(() => { mockCaptureSnapshot.mockReset(); resetGetRuntimeFixture(); @@ -97,7 +109,7 @@ test('react-native dismiss-overlay taps collapsed warning close affordance inste }); }); -test('react-native dismiss-overlay prefers non-trailing collapsed warning close controls', async () => { +test('react-native dismiss-overlay closes a collapsed banner at its own affordance, not at a Close over it', async () => { const sessionName = 'rn-session'; const sessionStore = makeSessionStore(); sessionStore.publish(sessionName, makeSession(sessionName)); @@ -114,6 +126,7 @@ test('react-native dismiss-overlay prefers non-trailing collapsed warning close { index: 1, ref: 'e91', + type: 'Button', label: 'Close', rect: { x: 10, y: 803, width: 34, height: 34 }, hittable: true, @@ -141,13 +154,13 @@ test('react-native dismiss-overlay prefers non-trailing collapsed warning close expect(response?.ok).toBe(true); expect(mockDismissTap).toHaveBeenCalledWith( - expect.objectContaining({ point: { x: 27, y: 820 } }), + expect.objectContaining({ point: { x: 379, y: 820 } }), ); expect(response?.ok && response.data).toMatchObject({ action: 'dismiss-overlay', - overlayAction: 'close', - ref: 'e91', - x: 27, + overlayAction: 'close-collapsed-banner', + ref: 'e90', + x: 379, y: 820, }); }); @@ -332,7 +345,7 @@ test('react-native dismiss-overlay rejects unsafe collapsed warning coordinate f }); }); -test('react-native dismiss-overlay dismisses RedBox error overlays instead of minimizing them', async () => { +test('react-native dismiss-overlay dismisses the LogBox inspector instead of minimizing it', async () => { const sessionName = 'rn-redbox-session'; const sessionStore = makeSessionStore(); sessionStore.publish(sessionName, makeSession(sessionName)); @@ -350,15 +363,22 @@ test('react-native dismiss-overlay dismisses RedBox error overlays instead of mi index: 1, ref: 'e2', label: 'Dismiss', - rect: { x: 20, y: 730, width: 150, height: 44 }, + rect: { x: 0, y: 730, width: 130, height: 44 }, }, { index: 2, ref: 'e3', label: 'Minimize', - rect: { x: 190, y: 730, width: 150, height: 44 }, + rect: { x: 130, y: 730, width: 130, height: 44 }, + }, + { + index: 3, + ref: 'e4', + label: 'Copy', + rect: { x: 260, y: 730, width: 130, height: 44 }, }, ], + viewport: snapshotViewportSizeFrom({ x: 0, y: 0, width: 390, height: 844 }), createdAt: Date.now(), }, }) @@ -387,7 +407,7 @@ test('react-native dismiss-overlay dismisses RedBox error overlays instead of mi expect(response?.ok).toBe(true); expect(mockDismissTap).toHaveBeenCalledWith( - expect.objectContaining({ point: { x: 95, y: 752 } }), + expect.objectContaining({ point: { x: 65, y: 752 } }), ); expect(response?.ok && response.data).toMatchObject({ action: 'dismiss-overlay', @@ -397,13 +417,13 @@ test('react-native dismiss-overlay dismisses RedBox error overlays instead of mi verified: true, verificationRequired: false, message: 'React Native overlay dismiss action sent and verified gone', - x: 95, + x: 65, y: 752, }); expect(response?.ok && response.data?.minimized).toBeUndefined(); }); -test('react-native dismiss-overlay reports unverified dismiss when RedBox controls remain', async () => { +test('react-native dismiss-overlay reports unverified dismiss when the inspector controls remain', async () => { const sessionName = 'rn-redbox-still-full-session'; const sessionStore = makeSessionStore(); sessionStore.publish(sessionName, makeSession(sessionName)); @@ -420,15 +440,22 @@ test('react-native dismiss-overlay reports unverified dismiss when RedBox contro index: 1, ref: 'e2', label: 'Dismiss', - rect: { x: 20, y: 730, width: 150, height: 44 }, + rect: { x: 0, y: 730, width: 130, height: 44 }, }, { index: 2, ref: 'e3', label: 'Minimize', - rect: { x: 190, y: 730, width: 150, height: 44 }, + rect: { x: 130, y: 730, width: 130, height: 44 }, + }, + { + index: 3, + ref: 'e4', + label: 'Copy', + rect: { x: 260, y: 730, width: 130, height: 44 }, }, ], + viewport: snapshotViewportSizeFrom({ x: 0, y: 0, width: 390, height: 844 }), createdAt: Date.now(), }, }; @@ -478,13 +505,9 @@ test('react-native dismiss-overlay uses Dismiss when RedBox Minimize is absent', label: 'Runtime Error', rect: { x: 0, y: 0, width: 390, height: 100 }, }, - { - index: 1, - ref: 'e2', - label: 'Dismiss', - rect: { x: 20, y: 730, width: 150, height: 44 }, - }, + ...redBoxFooter(['Dismiss', 'Reload JS', 'Copy', 'Extra Info'], 730, 390), ], + viewport: snapshotViewportSizeFrom({ x: 0, y: 0, width: 390, height: 844 }), createdAt: Date.now(), }, }); @@ -507,7 +530,7 @@ test('react-native dismiss-overlay uses Dismiss when RedBox Minimize is absent', expect(response?.ok).toBe(true); expect(mockDismissTap).toHaveBeenCalledWith( - expect.objectContaining({ point: { x: 95, y: 752 } }), + expect.objectContaining({ point: { x: 49, y: 752 } }), ); expect(response?.ok && response.data).toMatchObject({ action: 'dismiss-overlay', @@ -530,13 +553,13 @@ test('react-native dismiss-overlay accepts RedBox control labels with keyboard s label: 'Runtime Error: NativeModule is null', rect: { x: 0, y: 0, width: 390, height: 620 }, }, - { - index: 1, - ref: 'e2', - label: 'Dismiss (ESC)', - rect: { x: 18, y: 700, width: 104, height: 44 }, - }, + ...redBoxFooter( + ['Dismiss (ESC)', 'Reload JS (⌘R)', 'Copy (⌥⌘C)', 'Extra Info (⌘E)'], + 700, + 390, + ), ], + viewport: snapshotViewportSizeFrom({ x: 0, y: 0, width: 390, height: 844 }), createdAt: Date.now(), }, }); @@ -559,7 +582,7 @@ test('react-native dismiss-overlay accepts RedBox control labels with keyboard s expect(response?.ok).toBe(true); expect(mockDismissTap).toHaveBeenCalledWith( - expect.objectContaining({ point: { x: 70, y: 722 } }), + expect.objectContaining({ point: { x: 49, y: 722 } }), ); expect(response?.ok && response.data).toMatchObject({ action: 'dismiss-overlay', @@ -594,9 +617,31 @@ test('react-native dismiss-overlay prefers concrete RedBox buttons over labeled ref: 'e43', type: 'XCUIElementTypeButton', label: 'Dismiss (ESC)', - rect: { x: 156, y: 805, width: 90, height: 44 }, + rect: { x: 0, y: 805, width: 100.5, height: 44 }, + }, + { + index: 3, + ref: 'e44', + type: 'XCUIElementTypeButton', + label: 'Reload JS (⌘R)', + rect: { x: 100.5, y: 805, width: 100.5, height: 44 }, + }, + { + index: 4, + ref: 'e45', + type: 'XCUIElementTypeButton', + label: 'Copy (⌥⌘C)', + rect: { x: 201, y: 805, width: 100.5, height: 44 }, + }, + { + index: 5, + ref: 'e46', + type: 'XCUIElementTypeButton', + label: 'Extra Info (⌘E)', + rect: { x: 301.5, y: 805, width: 100.5, height: 44 }, }, ], + viewport: snapshotViewportSizeFrom({ x: 0, y: 0, width: 402, height: 874 }), createdAt: Date.now(), }, }); @@ -622,7 +667,7 @@ test('react-native dismiss-overlay prefers concrete RedBox buttons over labeled action: 'dismiss-overlay', overlayAction: 'dismiss', ref: 'e43', - x: 201, + x: 50, y: 827, }); }); @@ -638,14 +683,8 @@ test('react-native dismiss-overlay reports verified success after a clean post-d { index: 0, ref: 'e1', - label: 'LogBox', - rect: { x: 0, y: 640, width: 390, height: 120 }, - }, - { - index: 1, - ref: 'e2', - label: 'Close', - rect: { x: 84, y: 692, width: 42, height: 44 }, + label: '!, Warning: Failed prop type', + rect: { x: 10, y: 640, width: 370, height: 48 }, }, ], createdAt: Date.now(), @@ -687,7 +726,7 @@ test('react-native dismiss-overlay reports verified success after a clean post-d expect(mockCaptureSnapshot).toHaveBeenCalledTimes(2); expect(response.data).toMatchObject({ action: 'dismiss-overlay', - overlayAction: 'close', + overlayAction: 'close-collapsed-banner', verified: true, verificationRequired: false, }); @@ -773,14 +812,8 @@ test('react-native dismiss-overlay reports unverified dismiss when post-dismiss { index: 0, ref: 'e1', - label: 'LogBox', - rect: { x: 0, y: 640, width: 390, height: 120 }, - }, - { - index: 1, - ref: 'e2', - label: 'Close', - rect: { x: 84, y: 692, width: 42, height: 44 }, + label: '!, Warning: Failed prop type', + rect: { x: 10, y: 640, width: 370, height: 48 }, }, ], createdAt: Date.now(), @@ -841,14 +874,8 @@ test('react-native dismiss-overlay reports still-visible overlays with recovery { index: 0, ref: 'e1', - label: 'LogBox', - rect: { x: 0, y: 640, width: 390, height: 120 }, - }, - { - index: 1, - ref: 'e2', - label: 'Close', - rect: { x: 84, y: 692, width: 42, height: 44 }, + label: '!, Warning: Failed prop type', + rect: { x: 10, y: 640, width: 370, height: 48 }, }, ], createdAt: Date.now(),