Automated tests with Doze mode on android - android

I am trying to write some automated tests for how my app functions when the device enters and exits Doze mode. I know that you can use dumpsys deviceidle force-idle to force the device into idle mode when doing manual testing.
Is there a way to run that command from within a test app, or (better yet) an Android API to request Doze mode? If there isn't, does anyone have a recommendation for documentation regarding a good way to run adb commands at fixed points in an integration test?

Related

How to keep Android tests running even if the cable is disconnected?

I want to run Android tests for my App even if the cable is disconnected from machine.
Scenario:
I start Android tests from Android studio.
The tests take long time to finish.
The cable sometimes get disconnected (for many reasons) then the tests break.
I have to reset test data
Repeat from the beginning
However, I want my tests to keep running until they finish all test cases regardless of being connected to Android Studio.
I'm assuming the question is more about running tests on physical device. If that's the case, you may want to consider adb over tcp: https://developer.android.com/studio/command-line/adb#wireless however you will end up with another problem: your device will need to be charged from time to time, which basically means plugging your device anyways. Thus I'd suggest using Firebase test lab if that's an option: https://firebase.google.com/docs/test-lab. Last time I was checking, there was free tier for that.

Should a device behave as if nothing happened during Battery tests?

I need to test whether an app behaves correctly after restoring from Doze / App standby. The thing is that when I use commands from the Android Developers site, nothing happens with either the app nor with the device itself. The command prompt seems to respond correctly though.
Commands for Doze:
adb shell dumpsys deviceidle unforce
adb shell dumpsys battery reset
Commands for App Standby:
adb shell dumpsys battery unplug
adb shell am set-inactive <packageName> true
Wake up from App Standby:
adb shell am set-inactive <packageName> false
adb shell am get-inactive <packageName>
https://developer.android.com/training/monitoring-device-state/doze-standby.html#testing_doze_and_app_standby
Do you think that if nothing bad happens to the app, it means that it is behaving correctly? Or have I made some mistakes during the tests? I'm asking because from what I've read on the internet, the Doze should trigger with at least locked screen, so it all looks suspicious to me. I don't have any other ideas how to confirm whether these commands actually work.
With the Doze test, make sure you force the system into idle mode by running the following command first:
adb shell dumpsys deviceidle force-idle
But otherwise the commands you are running look correct.
You primarily want to test that the app returns to a good state after being in Doze or App Standby mode, and that any restrictions in place during that mode are lifted. The restrictions Doze and App Standby may impose on apps include limited or no network access, suspended background tasks, suspended Notifications, ignored wake requests, and alarms.
If your app can send notifications or trigger alarms, you can test that those are blocked during this mode.
According to the Android docs:
Observe the behavior of your app when it is woken. Make sure it
recovers gracefully from standby mode. In particular, you should check
if your app's Notifications and background jobs continue to function
as expected.

Update user location to server when application is in background

Mobile Technology: React native
Live Tracking using: Socket.io
I have taxi services application, in which I'm updating driver's location continuously to server. It is working fine when application is foreground. Now I want to update location of driver to server using socket on any other way when the application is in background. I need solution that works for both ios/android.
Please share your suggestions
im not sure about using socket but you can try saving your data on an sqlite database then using react-native-background-task for when your app is in the background
https://github.com/jamesisaac/react-native-background-task
use it like so:
BackgroundTask.define(() => {
//API CALL
BackgroundTask.finish()
})
componentDidMount() {
BackgroundTask.schedule();
}
please note that it takes ATLEAST 15 minutes for it to be called on iOS
OK, here goes not such an answer more an explanation of the situation you are in.
I've been in the same situation before and there are some things you can do to test your React Native app in the same scenario as being in 'Doze Mode'. Android introduced this as a way to limit CPU and battery usage whilst the phone is in Doze Mode. Doze mode itself is reached if the phone is stationary and/or hasn't had any interaction with the user for a set amount of time.
Firstly, with your current solution - you want to test your app in Standby/Doze Mode. You can achieve this by doing the below:
Testing your app with Doze
You can test Doze mode by following these steps:
https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze
Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image.
Connect the device to your development machine and install your app.
Run your app and leave it active.
Force the system into idle mode by running the following command:
$ adb shell dumpsys deviceidle force-idle
When ready, exit idle mode by running the following command:
$ adb shell dumpsys deviceidle unforce
Reactivate the device by performing the following command:
$ adb shell dumpsys battery reset
Observe the behavior of your app after you reactivate the device. Make sure the app recovers gracefully when the device exits Doze.
This will give you your testing scenario in order to test on Android.
The problem:
Android gives the apps less and less control of phones connectivity whilst in the background. It will stop networking all together and only opens in a 'Maintenance Window'. Android state:
The Doze restriction on network access is also likely to affect your app, especially if the app relies on real-time messages such as tickles or notifications. If your app requires a persistent connection to the network to receive messages, you should use Firebase Cloud Messaging (FCM) if possible.
The issue:
The problem is not running your app in the background, your problem will be using network in the background. For example, headless JS will run - runnable tasks will run BUT it may not be able to make any requests for network.
Possible solutions:
Using Alarms
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock()continue to fire normally — the system exits Doze shortly before those alarms fire.
Putting your App into a whitelist (visit the link above for acceptable use cases).
iOS:
For React Native, I used this:
https://github.com/mauron85/react-native-background-geolocation
This also works for Android, however the Doze mode (in my case) wouldn't send off network requests.

All ADB devices offline when starting adb at boot

I'm testing an application which records user travels for later review. One part of testing involves testing application behavior when under user specified battery level.
It must stop recording to save battery. I have built a system with a raspberry pi that when you flip a switch, adb commands are sent to all connected devices to mock battery status.
My issue is when I tried to have adb as startup or as a service it, adb list all device offline thus the device doesn't work. My script will work as long you start manually the adb server.
More info:
It is the cold season here so we use a car to record travels. We can't have a screen on the device to comply with local regulations that is why, I require the adb to start at boot or during account automatic login.
Can anyone help me on this issue?

How long does an App need to be idle for App Standby to kick in (Android Marshmallow)

I've looked all over the internet to find specifics about App Standby mode in Android Marshmallow, but I've not found any specific data for when it happens, other than some listed conditions that might cause it to happen. I also see a nebulous "If the device is idle for long periods of time, the system allows idle apps network access around once a day" without any specifics about how long this window would be.
Doze mode seems pretty well researched (This gist was particularly helpful and consistent with my findings), but I haven't been able to see App Standby in action. I created a test app that sends data to a server every 5 minutes via an Alarm and ran it for a week, and it continued to hit the server every 5 minutes unless the phone went into Doze mode and only hit the server in maintenance periods, then go back to every 5 minutes when the phone was awoken, so it did not appear that Android ever put it in "App Standby" state even though I didn't directly interact with the app.
Is there any specific data on how App Standby works?
You can force the device to enter this mode using the code below:
adb shell dumpsys deviceidle enable
adb shell dumpsys battery unplug
adb shell dumpsys deviceidle step
adb shell dumpsys deviceidle force-idle

Categories

Resources