# Genymotion Java API

Sometimes, in your Android tests (formerly known as instrumented tests), you need to test what happens in your application when sensors return specific values. As Android real devices cannot fake sensor values, you need to modify your source code to mock sensors and create proxy objects. This adds unwanted noise into your project source code only useful for testing, making your code less readable and harder to maintain.

All sensors being already 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

  1. 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>
    
  2. Add the dependency

    <dependency>
      <groupId>com.genymotion.api</groupId>
      <artifactId>genymotion-api</artifactId>
      <version>1.1.0</version>
    </dependency>
    

# Gradle

  1. Add the Genymotion Java API repository to the build.gradle file:

    repositories {
      maven{
        url "http://api.genymotion.com/repositories/releases/"
      }
    }
    
  2. Add the dependency:

    androidTestCompile 'com.genymotion.api:genymotion-api:1.1.0\'
    

# Other build systems

Add genymotion-api-1.1.0.jar (opens new window) 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 (10 seconds maximum) until the change is really effective.

# Tips

Most of the time, your application listens to sensor changes using a listener, then updates the application interface. Genymotion Java API only freezes until sensor values are retrieved.

To make sure your interface has had enough time to perform the update before testing it, you can add the following code snippet:

getInstrumentation().waitForIdleSync();

To make sure your Android test is only run inside Genymotion Desktop and not on a real device, you can exit the test by running the following command:

if (!GenymotionManager.isGenymotionDevice()) {
  return; //don't execute this test
}

# Examples

An application called Binocle 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.

For more information about the Binocle application, you can refer to Binocle source code (opens new window).