Genymotion Java API enables direct manipulation of sensor values from Android tests. Android real devices cannot fake sensor values. Developers may need to test how applications behave when sensors return specific values. Genymotion Java API provides a way to do this without altering production source code. All sensors are mocked inside Genymotion. The Genymotion Java API allows you to directly manipulate sensor values from your Android tests. This section explains how to install and use Genymotion Java API.
Installing Genymotion Java API
To install Genymotion Java API, follow the steps corresponding to your build engine.
Maven
Add the Genymotion Java API repository to the pom.xml file:
<repository>
<id>genymotion</id>
<name>Genymotion repo</name>
<url>http://api.genymotion.com/repositories/releases/</url>
</repository>
Add the dependency:
<dependency>
<groupId>com.genymotion.api</groupId>
<artifactId>genymotion-api</artifactId>
<version>1.1.0</version>
</dependency>
Gradle
Add the Genymotion Java API repository to the build.gradle file:
repositories {
maven {
url "http://api.genymotion.com/repositories/releases/"
}
}
Add the dependency:
androidTestCompile 'com.genymotion.api:genymotion-api:1.1.0'
Other build systems
Add genymotion-api-1.1.0.jar file to the libs folder.
Using Genymotion Java API
To use the Genymotion Java API in your Android test project, follow the steps below.
1) Inside your Android test project, get a reference to the Genymotion object using:
GenymotionManager genymotion = GenymotionManager.getGenymotionManager(getInstrumentation().getContext())
2) Access sensors from the GenymotionManager. For example, to set the battery charge level, use the command below:
genymotion.getBattery().setLevel(10);
This call freezes the application for up to 10 seconds until the change is effective.
Tips
Most of the time, your application listens to sensor changes using a listener, then updates the application interface. Genymotion Java API freezes until sensor values are retrieved.
To ensure the interface has had enough time to perform the update before testing it, add:
getInstrumentation().waitForIdleSync();
To ensure your Android test runs only inside Genymotion Desktop and not on a real device, exit the test with:
if (!GenymotionManager.isGenymotionDevice()) {
return; // don't execute this test
}
Examples
We have developed an application called Binocle which showcases Genymotion Java API use. In this application, you can find activities for which the behavior depends on sensor values.
Below are some Android test examples built with Genymotion Java API to manipulate sensor values and check activity behaviors:
- Battery
- An application must display a warning message if the device is not plugged to a power source and has less than 10% of charge left.
- BatterySampleFragment.java
- TestBattery.java
- GPS
- An application must display a message if the device is localized near a specific place.
- GpsSampleFragment.java
- TestGps.java
- Radio
- An application must display a message if the device is a Nexus 4, as recognized by its IMEI number.
- RadioSampleFragment.java
- TestRadio.java
- ID
- An application must encrypt data using ANDROID_ID to avoid the backed up data to be moved to another Android device.
- IdSampleFragment.java
- TestId.java
- Phone
- An application must display a green check mark when the virtual device receives a text message containing the text "666".
- PhoneSampleFragment.java
- TestPhone.java
For more details, visit genymotion-binocle.
Back to top