diff --git a/packages/selectors/src/internal/match.test.ts b/packages/selectors/src/internal/match.test.ts index c9cf5ef8c1..7841ae604b 100644 --- a/packages/selectors/src/internal/match.test.ts +++ b/packages/selectors/src/internal/match.test.ts @@ -215,6 +215,25 @@ test('focused selector matches snapshot focus state', () => { assert.equal(resolved.node.ref, 'tv2'); }); +test('editable selector follows a published editable fact and infers from type without one', () => { + const nodes: SnapshotState['nodes'] = [ + { ref: 'e1', index: 0, type: 'com.example.RichEditor', label: 'Custom', editable: true }, + { ref: 'e2', index: 1, type: 'android.widget.EditText', label: 'ReadOnly', editable: false }, + { ref: 'e3', index: 2, type: 'android.widget.EditText', label: 'Disabled', enabled: false }, + { ref: 'e4', index: 3, type: 'android.widget.EditText', label: 'Inferred' }, + { ref: 'e5', index: 4, type: 'android.widget.TextView', label: 'Plain' }, + ]; + const labelsMatching = (selector: string) => + nodes + .filter((node) => + resolveSelectorChain([node], parseSelectorChain(selector), { platform: 'android' }), + ) + .map((node) => node.label); + + assert.deepEqual(labelsMatching('editable=true'), ['Custom', 'Inferred']); + assert.deepEqual(labelsMatching('editable=false'), ['ReadOnly', 'Disabled', 'Plain']); +}); + // ── appName / windowTitle selectors ────────────────────────────────────── test('appName selector matches nodes with appName field', () => { diff --git a/packages/selectors/src/internal/node.test.ts b/packages/selectors/src/internal/node.test.ts index a2b7510140..4a680abb9b 100644 --- a/packages/selectors/src/internal/node.test.ts +++ b/packages/selectors/src/internal/node.test.ts @@ -9,7 +9,7 @@ import assert from 'node:assert/strict'; import { test } from 'vitest'; import { attachRefs, formatRole, type SnapshotNode } from '@agent-device/kernel/snapshot'; import { normalizeType } from '@agent-device/contracts/snapshot'; -import { roleSpellingsOfNode } from './node.ts'; +import { isNodeEditable, roleSpellingsOfNode } from './node.ts'; function bareNode(type: string): SnapshotNode { return { index: 0, ref: 'e1', type, kind: formatRole(type) }; @@ -153,3 +153,25 @@ test('the window IS normalizeType: drift in the owner moves the window and the r } } }); + +test('isNodeEditable lets a published editable fact decide over the type', () => { + const customView: SnapshotNode = { ...bareNode('com.example.RichEditor'), editable: true }; + const readOnlyField: SnapshotNode = { ...bareNode('android.widget.EditText'), editable: false }; + assert.equal(isNodeEditable(customView, 'android'), true); + assert.equal(isNodeEditable(readOnlyField, 'android'), false); +}); + +test('isNodeEditable keeps the disabled refusal whatever the fact says', () => { + const disabledField: SnapshotNode = { + ...bareNode('android.widget.EditText'), + editable: true, + enabled: false, + }; + assert.equal(isNodeEditable(disabledField, 'android'), false); + assert.equal(isNodeEditable({ ...disabledField, editable: undefined }, 'android'), false); +}); + +test('isNodeEditable infers from the type when the fact is absent', () => { + assert.equal(isNodeEditable(bareNode('android.widget.EditText'), 'android'), true); + assert.equal(isNodeEditable(bareNode('com.example.RichEditor'), 'android'), false); +}); diff --git a/packages/selectors/src/internal/node.ts b/packages/selectors/src/internal/node.ts index 54b8569a04..83b68ae6c3 100644 --- a/packages/selectors/src/internal/node.ts +++ b/packages/selectors/src/internal/node.ts @@ -8,8 +8,10 @@ export function isNodeVisible(node: SnapshotNode): boolean { return node.rect.width > 0 && node.rect.height > 0; } +/** A published `editable` fact decides; the type inference applies only when the fact is absent. */ export function isNodeEditable(node: SnapshotNode, platform: Platform | PublicPlatform): boolean { - return isFillableType(node.type ?? '', platform) && node.enabled !== false; + if (node.enabled === false) return false; + return node.editable ?? isFillableType(node.type ?? '', platform); } /** diff --git a/packages/selectors/src/internal/predicates.test.ts b/packages/selectors/src/internal/predicates.test.ts index 91ef59e9d9..8c674f90fb 100644 --- a/packages/selectors/src/internal/predicates.test.ts +++ b/packages/selectors/src/internal/predicates.test.ts @@ -241,6 +241,27 @@ test('visible predicate does not use non-hittable Android layout ancestors for r assert.equal(result.pass, false); }); +test('editable predicate follows a published editable fact and infers from type without one', () => { + const editableAt = (node: Partial) => { + const full: SnapshotNode = { index: 0, ref: 'e1', ...node }; + return evaluateIsPredicate({ + predicate: 'editable', + node: full, + visibility: createSnapshotVisibility([full]), + platform: 'android', + }).pass; + }; + + assert.equal(editableAt({ type: 'com.example.RichEditor', editable: true }), true); + assert.equal(editableAt({ type: 'android.widget.EditText', editable: false }), false); + assert.equal( + editableAt({ type: 'android.widget.EditText', editable: true, enabled: false }), + false, + ); + assert.equal(editableAt({ type: 'android.widget.EditText' }), true); + assert.equal(editableAt({ type: 'com.example.RichEditor' }), false); +}); + /** One capture whose two rows share a label: one on screen, one below the fold. */ const SHARED_CAPTURE: SnapshotNode[] = [ { diff --git a/website/docs/docs/selectors.md b/website/docs/docs/selectors.md index 9b484b7147..b7feafadd0 100644 --- a/website/docs/docs/selectors.md +++ b/website/docs/docs/selectors.md @@ -91,6 +91,10 @@ Text values match the whole field, ignoring case and extra whitespace: `label="s Boolean keys: `visible`, `hidden`, `editable`, `selected`, `focused`, `enabled`, `hittable`. Write `visible=true` or `visible=false`; a bare `visible` means `visible=true`. +`editable` (and `is editable`) uses the node's native `editable` fact when the snapshot carries one, +so a custom text view can match and a read-only field cannot. Without the fact it is inferred from +the element type. A disabled element is never editable. + Quoting and alternatives: - Quote values that contain spaces, with double or single quotes: `label="Sign in"`. Escape a quote