Skip to content

fix: adb empty typeText clears the field; verify dismissKeyboard - #60

Merged
cryptotavares merged 5 commits into
mainfrom
cryptotavares/mmcli-p1-49-adb-type-keyboard
Nov 18, 2026
Merged

cryptotavares merged 5 commits into
mainfrom
cryptotavares/mmcli-p1-49-adb-type-keyboard

Conversation

@cryptotavares

@cryptotavares cryptotavares commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #57. Fixes #58. Part of the mm CLI v2 effort (ticket P1-49). Bottom of stack #62 (#61 is on top). Ships in device-mcp 0.9.0.

Summary: On Android, typeText("") now clears the field, and a retype replaces the old text. dismissKeyboard now checks that the keyboard closed, and fails if it is still shown.

Problem

There are three bugs in the adb backend:

  1. typeText("") runs adb shell input text with no argument. The device throws Argument expected after "text", so mm type <field> "" fails with exit 4.
  2. The clear step selects nothing in a React Native field on API 36, so DEL removes one character. A retype appends.
  3. dismissKeyboard sends one KEYCODE_ESCAPE and returns. It reports success while the keyboard is still shown. The next tap can then hit a key.
# 0.8.0, account search field
type "zzq", then type "abc"   → field shows "zzabc"
type ""                       → MM_TYPE_FAILED … Argument expected after "text"
dismiss-keyboard              → {"dismissed":true}, but mInputShown=true

Solution

  1. Clear: after the old select step, send input keycombination KEYCODE_CTRL_LEFT KEYCODE_A, then DEL. keycombination exists only on newer Android, so the call is best effort.
  2. Empty text: return after the clear step.
  3. Verified dismiss: read only the mInputShown line of dumpsys input_method, not the full dump (about 1 MB).
flowchart TD
    A[dismissKeyboard] --> B{"mInputShown?"}
    B -->|false| R[Return. No key sent.]
    B -->|unknown| E0[Send ESCAPE. Return, as in 0.8.0.]
    B -->|true| E[Send ESCAPE]
    E --> W{"hidden within 2 s?"}
    W -->|yes| OK[Return]
    W -->|no| C{"read again: still shown?"}
    C -->|no| OK
    C -->|yes| K[Send BACK]
    K --> W2{"hidden within 2 s?"}
    W2 -->|yes| OK
    W2 -->|no| X[Throw. The caller gets a failure, not dismissed: true.]

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class B,W,C,K,W2 new
    class X stop
Loading

Yellow boxes are new. Red boxes stop the call.

What changes

Call 0.8.0 This PR
typeText("") Throws Argument expected after "text" Clears the field
typeText("abc") over zzq Field shows zzabc Field shows abc
dismissKeyboard(), keyboard shown, ESCAPE hides it Returns Returns after the keyboard hides
dismissKeyboard(), ESCAPE does not hide it Returns, keyboard still shown Sends BACK, returns after the keyboard hides
dismissKeyboard(), still shown after BACK Returns Throws Keyboard still shown after ESCAPE and BACK. …
dismissKeyboard(), no keyboard Sends ESCAPE Sends nothing
dismissKeyboard(), mInputShown missing Sends ESCAPE Same, no check

Core maps the new throw to MM_DEVICE_ACTION_FAILED.

Decisions for reviewers

Decision Reason
BACK after ESCAPE fails ESCAPE is not a documented "hide keyboard" key, and it sometimes does nothing. While the keyboard is shown, BACK only hides it.
Wait 2 s after ESCAPE, then read again right before BACK If the keyboard hides in the gap between the last read and BACK, BACK reaches the app and can navigate. The long wait and the fresh read make that gap one adb round trip. The remaining risk is in the doc comment. The cost is about 2 s when ESCAPE does nothing.
An unreadable state sends ESCAPE only, unverified A blind BACK could navigate. This keeps 0.8.0 behaviour on devices without mInputShown.
Ctrl+A is best effort (exec, result ignored) Older Android has no input keycombination. There the clear is the same as before.

Review guide

Read the files in this order:

# File What to check
1 src/backends/adb-backend.ts typeText: the Ctrl+A step and the empty-text return. dismissKeyboard, #isKeyboardShown, #waitForKeyboardHidden.
2 src/tools/type-text.ts, src/tools/dismiss-keyboard.ts Tool descriptions for the new Android behaviour
3 src/backends/adb-backend.test.ts Exact adb call sequences, the BACK path, the unreadable state, the throw
Testing
  • yarn build, yarn lint and yarn test pass (35 files, 830 tests, 6 skipped). Coverage thresholds are unchanged.
  • Live: Android emulator (API 36, Gboard), MetaMask Mobile dev build, mm from core 0.14.0, account search field. Before = npm 0.8.0, after = a yalc build of this branch.
Check 0.8.0 This branch
mm type <search field> "" after zzq exit 4 MM_TYPE_FAILED exit 0, textLength: 0, placeholder shown (6 of 6)
dismiss-keyboard, keyboard shown dismissed: true, keyboard still shown 10 of 10 hidden, then 5 of 5 after the BACK change
dismiss-keyboard time, keyboard shown 27–69 ms, unverified p50 316 ms when ESCAPE hides it, about 2.4 s when BACK does
dismiss-keyboard, no keyboard sends ESCAPE p50 24 ms, no key

In the 5 BACK-path tries, the account sheet stayed open, so BACK did not reach the app.

Known gap (follow-up)

A retype over existing text kept one stray character in 3 of 9 tries (abc → c). The cause is probably a race between the focusing tap and the clear. Clearing to empty passed 6 of 6, and every retype is better than 0.8.0. A follow-up can add a settle step or a read-back.

`typeText("")` cleared the field, then ran `adb shell input text` with no
argument, which throws `Argument expected after "text"` on the device.
Return after the clear step when the text is empty.
`dismissKeyboard` sent one KEYCODE_ESCAPE and returned, so it reported
success while the keyboard could stay up. Now it reads `mInputShown` from
`dumpsys input_method` first and sends no key when the keyboard is hidden.
Otherwise it sends ESCAPE and polls for up to 1 s, then sends BACK once
(while the keyboard is shown, BACK only hides it), and throws when the
keyboard is still shown.
On API 36 the Shift + MOVE_END selection selects nothing in a React
Native text field, so the clear deleted one character: typing "abc"
over "zzq" gave "zzabc", and an empty typeText left "zz". Also send
Ctrl+A (`input keycombination`) before DEL. `keycombination` only exists
on newer Android, so a failure there is ignored.
…oard

Read the keyboard state with 'dumpsys input_method | grep -m1 mInputShown' instead of the ~1 MB dump, poll up to 2 s after ESCAPE, and re-read the state right before BACK so BACK is sent only while the keyboard is still shown. Document the remaining race and the unreadable-state path (ESCAPE only, as in 0.8.0). Assert that Ctrl+A is sent before DEL.
@cryptotavares
cryptotavares added this pull request to stack #62 November 17, 2026 07:50
`expect(...).toBeDefined()` on the two call-order values only narrowed
types that are already `number`, and the existing helper-trust test in
this file asserts order the same way without them. The test still fails
if either call is missing, through `toBeLessThan`.
@cryptotavares
cryptotavares marked this pull request as ready for review November 17, 2026 14:31
@cryptotavares
cryptotavares requested a review from a team as a code owner November 17, 2026 14:31
@cryptotavares
cryptotavares merged commit f530e71 into main Nov 18, 2026
35 checks passed
@cryptotavares
cryptotavares deleted the cryptotavares/mmcli-p1-49-adb-type-keyboard branch November 18, 2026 15:53
cryptotavares added a commit that referenced this pull request Nov 18, 2026
Part of the mm CLI v2 effort (ticket `P1-50`). Stacked on #60. Ships in
device-mcp 0.9.0.

> **Summary:** An Android tap on a target that something else covers now
fails with `TARGET_OBSCURED`, and nothing is tapped. Before, the tap hit
the overlay or the keyboard and the call reported success.

## Problem

`AdbBackend.tapElement` checks only that the target is inside the
viewport, then taps its center. On the MetaMask Mobile dev build, the
Expo "Tools" button covers `navbar-hamburger-menu-button`. A click on
the hamburger opens the Expo dev menu and returns `clicked: true`. With
the keyboard up, a tap on a covered target can hit a key. iOS has had
this check since #51. Android has not.

```text
# 0.8.0
$ mm click --testid navbar-hamburger-menu-button
{"clicked":true,"target":"testId:navbar-hamburger-menu-button"}   # the Expo dev menu opens
```

## Solution

Before each tap and long press, `assertAndroidHittable` checks the tap
point against the snapshot the target came from. The snapshot helper
already records window type and layer, `drawing-order` and
`visible-to-user`, so no Java change is needed. Three checks run in
order:

1. **Higher window:** a drawn node of a window above the target's window
(the keyboard, a system window).
2. **Keyboard area:** while the snapshot has a keyboard window, one
`dumpsys window InputMethod` read gives the keyboard's live touchable
region.
3. **Same window:** a node drawn after the target (higher
`drawing-order` where the branches split) that draws at the point.
`hitsTarget`, the iOS rule, decides whether it is part of the target.

```mermaid
flowchart TD
    A[tapElement / longPress] --> B[viewport check]
    B --> C{"window data in the snapshot?"}
    C -->|no: stock uiautomator dump| T[tap, as in 0.8.0]
    C -->|yes| D{"drawn node of a higher window at the point?"}
    D -->|yes| X[TARGET_OBSCURED]
    D -->|no| E{"keyboard window above the target?"}
    E -->|yes| F["read the keyboard touchable region"]
    F --> G{"point inside it?"}
    G -->|yes| X
    G -->|no| H
    E -->|no| H{"later-drawn node in the same window draws at the point, and is not part of the target?"}
    H -->|yes| X
    H -->|no| T
    X --> N[No tap. Error names the cover. Keyboard sets keyboard: true.]

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class C,D,E,F,G,H new
    class X,N stop
```

Yellow boxes are new. Red boxes stop the tap.

## What changes

```text
TARGET_OBSCURED: android.view.ViewGroup id="navbar-hamburger-menu-button" is covered at (973, 137) by android.view.View around android.widget.ImageView label="Tools". Nothing was tapped. Close the overlay, or scroll the target into a free area.
```

| Condition | Result |
|---|---|
| Target is free | Tap, as before |
| Overlay drawn later in the app window (Expo Tools button) |
`TARGET_OBSCURED: … by <type> around <named node>. …` |
| A keyboard node at the point | `… by the keyboard: <class>. … Dismiss
the keyboard (device_dismiss_keyboard) and retry.` with `keyboard: true`
|
| Point in the keyboard's touchable region, key missing from the
snapshot | `… by the keyboard: touchable area [l,t][r,b]. …` with
`keyboard: true` |
| Snapshot without window data, or an element that is not from a parsed
snapshot | No check. Tap, as in 0.8.0 |

Core 0.14.0 already maps `TARGET_OBSCURED` to `MM_TARGET_OBSCURED` (exit
4). With `keyboard: true`, `click --dismiss-keyboard` dismisses the
keyboard and retries once.

## Decisions for reviewers

| Decision | Reason |
|---|---|
| Read the keyboard's live touchable region (one adb call, about 22 ms)
| Gboard's floating suggestion key is sometimes missing from the
snapshot. The keyboard window's bounds lag behind it. Window bounds, or
the drawn keys widened to full width, either miss that key or refuse
free targets on the upper screen. The read runs only when the snapshot
has a keyboard window. A failed read falls back to the snapshot rules. |
| A later-drawn node inside the target's frame is accepted | React
Native draws a control's own content (an input over its container, an
icon over its background) as later-drawn siblings. A small sibling
overlay inside the target is therefore a false accept. iOS behaviour is
unchanged. |
| A wrapper counts as drawn only when it is small or close around its
drawn nodes | Each side at most 20% of the window's shorter side, or its
area at most 2× the drawn area. This catches the Tools button's
background and lets `box-none` hosts and full-width rows through. |
| A large unlabelled, non-clickable leaf is transparent | At least 50%
of the window area, or full width. These are React Native absolute-fill
and `pointerEvents` hosts. |
| Fail open without window data | A stock `uiautomator dump` has no
`window-*` or `visible-to-user`. From android15-qpr2 it has
`drawing-order`, so it must not reach the same-window check. |
| `TargetObscuredError` gets an optional 4th argument | Android names
the cover differently and knows the keyboard from the window type. iOS
calls it with three arguments and is unchanged. |

## Review guide

Read the files in this order:

| # | File | What to check |
|---|---|---|
| 1 | `src/utils/android-hit-test.ts` | `assertAndroidHittable`: the
three checks. Then `drawsItself`, `drawsAt` and `drawnAfter`. |
| 2 | `src/backends/adb-backend.ts` | `#assertHittable` before
`tapCoordinates` in `tapElement` and `longPress`.
`parseHitTestAttributes` in `parseAndroidHierarchy`. |
| 3 | `src/utils/hit-test.ts` | The optional `options` argument of
`TargetObscuredError`. |
| 4 | `*.test.ts` and `__fixtures__/` | Recorded helper XML (texts
blanked) and `dumpsys window InputMethod` output, keyboard shown and
hidden. |

<details>
<summary>Testing</summary>

- `yarn build`, `yarn lint` and `yarn test` pass (36 files, 885 tests, 6
skipped). Coverage thresholds are unchanged. `hit-test.test.ts` and
`idb-backend.test.ts` are unchanged.
- Live: Android emulator (API 36, Gboard), MetaMask Mobile dev build,
`mm` from core 0.14.0. Before = npm 0.8.0, after = a yalc build of this
branch.

| Check | 0.8.0 | This branch |
|---|---|---|
| `click` on the hamburger under the Tools button | `clicked: true`,
Expo dev menu opens | exit 4 `MM_TARGET_OBSCURED`, still on wallet home
|
| Tap on a target under the keyboard, 10 tries | not sent (no check) |
10 of 10 refused, `keyboard: true` |
| Same, with the suggestion key removed from each snapshot | – | 10 of
10 refused by the touchable region |
| Free targets with the keyboard up | – | 4 of 4 tapped |
| Read-only clicks: wallet home, account list, Settings, tab bar | – |
14 of 14 tapped, no refusal |
| `click` p50 (wall / tool ms), 12 runs | 688 / 522 | 566 / 410 |

- An offline replay of 9 recorded screens refuses only targets that are
really covered: the hamburger, `account-list-manage-accounts-button`
under Tools, and token rows under the floating tab bar.

</details>

<details>
<summary>Known gaps</summary>

- A helper tree cut at `maxNodes` can miss the nodes drawn last. An
overlay there is not seen, so the tap goes ahead. This is a false
accept, never a false refusal.
- A copied or JSON-round-tripped `options.element` has no recorded node
data, so the check is skipped.

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants