My app writes user preferences to a local file when the app is moved to the background (AppLifecycleState.paused) and I'd like to write a test for this behavior.
Is there a way to mimic this in a unit test? Or is this something that would need to be done as an integration test?
You can call binding.handleAppLifecycleStateChanged to fake the app going in and out of the foreground in a unit test.
tester.binding.handleAppLifecycleStateChanged is definitly the correct way to test it.
ex: Statefull widget on the home page that listen to app lifecycle
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
pushLoginPage(context);
}
}
If you want to test this, you have to paused first, and then resumed to ensure Flutter navigation.