**Overview — goal**Show you can reliably reproduce process death, configuration changes, low-memory and scene-restore across unit, integration and UI layers on Android and iOS so state-restoration logic is correct.**Unit / Component tests**- Android: test ViewModel + SavedStateHandle directly. Use SavedStateHandle/Bundle mocks to assert state saved/restored. Use Robolectric's ActivityController to simulate destroy/create cycles:kotlin
val controller = Robolectric.buildActivity(MyActivity::class.java).create().start().resume()
controller.pause().stop().destroy()
val recreated = Robolectric.buildActivity(MyActivity::class.java).create().start().resume()
- iOS: XCTest unit tests for state encode/decode (NSCoding/Codable) and for scene delegate restore methods by instantiating view controllers, encoding state into NSCoder mocks, then decoding.**Integration tests**- Android: FragmentScenario / ActivityScenario: simulate config change with recreate(), validate SavedStateRegistry. To emulate process death, background app then kill process with adb, then relaunch to ensure onCreate(Bundle) receives saved state.bash
adb shell am start -n com.example/.MainActivity
adb shell input keyevent KEYCODE_APP_SWITCH
adb shell am kill com.example.myapp # kills process
adb shell monkey -p com.example.myapp -c android.intent.category.LAUNCHER 1
- iOS: In XCTest, use XCUIApplication.terminate() to simulate kill, then launch() to test restoration. Use launchArguments/Environment to force cold start paths.**UI / End-to-end tests**- Android: Espresso + UIAutomator. Background with pressHome(), then adb kill or `adb shell am force-stop <pkg>` while in background to simulate OS killing due to low memory. Use emulator settings to reduce memory or use `adb shell am send-trim-memory <pkg> COMPLETE`.- iOS: XCUITest: XCUIDevice.shared.press(.home), then useswift
let app = XCUIApplication(); app.terminate(); app.launch()
To simulate memory warnings in Simulator: Debug -> Simulate Memory Warning or:bash
xcrun simctl spawn booted notify_post com.apple.simulator.memoryWarning
To terminate process directly:bash
xcrun simctl spawn booted killall YourAppExecutableName
**Configuration changes / rotation**- Android: ActivityScenario.recreate() or test with `adb shell am broadcast` to toggle orientation, or use `ActivityScenarioRule` and call activity.requestedOrientation = ...- iOS: Test size-class changes in simulator by setting different device types or by using XCUIDevice orientation APIs.**Tools & emulator settings**- Android: adb, emulator advanced settings (low RAM), Developer Options -> Background process limit (set to 0 apps), `adb shell am send-trim-memory`, Espresso + UIAutomator, Robolectric for JVM-fast cycles.- iOS: Xcode Simulator (Simulate Memory Warning, Device → Rotate), simctl (launch/terminate/notify_post), XCUITest, unit tests for state encoding.**Edge cases & best practices**- Always test both cold launch (no saved state) and warm restore (saved Bundle/scene). Verify both transient UI state and persistent model state.- Automate reproducible commands in CI (emulator images with limited RAM, scripted adb/simctl commands).- Consider flaky timing: wait for backgrounding to complete before killing; use polling/assertions rather than fixed sleeps.This demonstrates strategies, sample commands, and which frameworks to use at each test level to reliably reproduce process kills and validate restoration.