Skip to content

android: Keep a test app's data when Android restores the development host - #4263

Open
madcodelife wants to merge 1 commit into
mainfrom
android-keep-test-data-on-restore
Open

madcodelife wants to merge 1 commit into
mainfrom
android-keep-test-data-on-restore

Conversation

@madcodelife

Copy link
Copy Markdown
Member

Problem

nsh test --target android gives each app a data directory of its own (dataDir) and the development host (DevActivity) empties it so the app starts with nothing stored. It emptied it on every onCreate, including when Android restores the activity after killing its process (savedInstanceState != null, e.g. the process was killed in the background and the user comes back from Recents). The app then lost its storage and caches on restore, unlike a packaged app (AppActivity), whose data survives process death. Process-death recovery (restoring a saved draft, reading persisted state) could not be checked on a device or emulator.

Minimal reproduction, with an app that saves Storage.setItem("draft", "hello") on a press of Save and shows Storage.getItem("draft") when it starts (draft: none when nothing is stored):

  1. nsh run <app> --target android serves the app and installs the development host as <id> (the app's id). Start the host as nsh test does, with a data directory: adb shell am start -S --activity-clear-task -n <id>/com.longbridge.nsh.DevActivity --es url ws://127.0.0.1:8790/ws --es dataDir app-0.
  2. Press Save: draft: hello (saved).
  3. adb shell input keyevent KEYCODE_HOME, then adb shell am kill <id>: the process is gone, the task stays in Recents.
  4. Bring the task back: adb shell am start -n <id>/com.longbridge.nsh.DevActivity (no -S / clear-task flags) brings the existing task to the front, as Recents does, and Android restores the activity with its saved state and its original extras.

Before: draft: none, the directory was deleted. After: draft: hello.

Change

  • The directory is emptied only on a launch (savedInstanceState == null); a restored activity keeps it. The CLI always launches the host with am start -S --activity-clear-task, so every test open is still a fresh launch with an empty directory.
  • The rule moves to a small Android-free helper (TestData.open) so it can be tested with the plain JDK, like FileRoots and ForeignIntents.
  • docs/dev/tooling.md and the module comment of test_android.rs say the directory is kept on restore.

No public API changes.

Tests

  • New JDK test TestDataTest (runs in the Android workflow's "Compile the Java runtime and run its JDK tests" step): a launch empties the app's directory and leaves other apps' directories; a restore keeps its storage and caches. It fails with the old always-empty behavior.
  • The whole Java runtime and its tests compile with javac --release 17 against android-35, and every JDK test passes.
  • Emulator (API 36), the steps above, with the host built from main and from this branch:
    • main: after the restore the app shows draft: none, and cache/nsh-test/<dataDir> no longer exists.
    • This branch: after the restore the app shows draft: hello, and the directory is the same one (same inode). A new am start -S --activity-clear-task launch afterwards shows draft: none again.
  • nsh test --target android crates/nsh-platform-android/tests/input-press.test.tsx on the emulator: 4 passed.

… host

The development host emptied the data directory of a test session's app (`dataDir`) on every `onCreate`, also when Android restored the activity after killing its process, so the app lost its storage on a restore that a packaged app survives. Empty it only on a launch (`savedInstanceState == null`), in an Android-free helper (`TestData`) covered by a JDK test.

@huacnlee huacnlee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ DevActivity now empties a test app's dataDir only on a fresh launch, so an activity that Android restores after killing its process keeps its storage, as a packaged app does.

Reviewed head: 8b587c0

This was a diff review. I did not build anything locally.

No issues found.

  • TestData.open(root, name, state != null) keeps the old delete on launch. nsh test always starts the host with am start -S --activity-clear-task, so every test open still begins with an empty directory. Only the restore path (savedInstanceState != null) changes.
  • The deletion is limited to <cacheDir>/nsh-test/<name>, and the JDK test checks that other apps' directories are left alone.
  • A restore after "Don't keep activities" or a configuration recreate now also keeps the data. That is the intended packaged-app behaviour, and previously a configuration recreate could silently wipe a test's storage in the middle of a session.

No public API change. One gap: the description does not say whether TestDataTest is wired into any CI script or is run by hand like the other plain-JDK tests.

CI: still running. check (windows-latest) passed, the macOS and Linux check jobs were pending, and changed jobs were skipped at the time of review.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee huacnlee added the Approved Review approved label Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved Review approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants