Enabling mock locations for LocationManager and FusedLocationProvider before running any Android instrumentation tests.
It providers a Junit-rule to grant needed permissions by executing adb commands and setting mock mode enabled for different location providers.
dependencies {
androidTestImplementation 'io.saeid.automator:mock-location-automator:1.0.0'
}
@RunWith(AndroidJUnit4::class)
@LargeTest
class YourScenarioTest {
@get:Rule
val mockLocationRule = MockLocationRule()
@Test
fun test_scenario_x() {
// ...
mockLocation(latitude, longitude)
// ...
}
}
In order to use this library, you must add MockLocationRule
into your test class.
This rule do the following steps:
- Add test apk to
Settings > Developer Options > Select Mock location app
, So all mock locations can be applied from test classes. - Grant
android.permission.ACCESS_FINE_LOCATION
andandroid.permission.ACCESS_COARSE_LOCATION
to avoid any permission errors.
You can mock a location by calling mockLocation
method. To avoid jumping back to the real position, It will preserve it periodically.
If you don't want this behaviour, you can simply disable preserving mode:
mockLocation(latitude = 12.45, longitude = 56.78, preserve = false)
You can also mock multiple locations by passing a list of DelayedLocation
. It will mock each location after a deplay which specified in the DelayedLocation
.
Here is an example:
mockLocations(
listOf(
DelayedLocation(location1, 1000),
DelayedLocation(location2, 2000),
DelayedLocation(location3, 3000)
)
)
So after the execution of this method, location1
will be applied after 1 second, then 3 seconds (1+2) later locations2
will be applied and so on.