I'm trying to get mock updates from FusedLocationProviderApi
, but I can't seem to make it work. This my set up method in android instrumentation test:
locationProvider = new LocationProvider(InstrumentationRegistry.getTargetContext().getApplicationContext(), settings);
// Connect first, so that we don't receive 'true' location
locationProvider.googleApiClient.blockingConnect();
// Set mock mode, to receive only mock locations
LocationServices.FusedLocationApi.setMockMode(locationProvider.googleApiClient, true);
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
// Start receiving locations; this call will connect api client, and if it's already connected (or after it connects) it will register for location updates
locationProvider.start();
}
});
// wait until we can request location updates
while (!locationProvider.isReceivingLocationUpdates) {
Thread.sleep(10);
}
After this point I would expect any calls to LocationServices.fusedLocationApi.setMockLocation(apiClient, location)
to set mock location that my listener would receive. Unfortunately this is not the case, and the listener stays silent.
My foolproof (or so I thought) method to set mock location looks like this:
private void setMockLocation(final Location location) throws Exception {
assertTrue(locationProvider.googleApiClient.isConnected());
assertTrue(locationProvider.isReceivingLocationUpdates);
final CountDownLatch countDownLatch = new CountDownLatch(1);
LocationServices.FusedLocationApi.setMockMode(locationProvider.googleApiClient, true)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
assertTrue(status.isSuccess());
LocationServices.FusedLocationApi.setMockLocation(locationProvider.googleApiClient, location)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
assertTrue(status.isSuccess());
countDownLatch.countDown();
}
});
}
});
assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
}
Method returns successfully, but no location is received by the listener. I'm really at a loss here. The worst part is sometimes the test would pass, but extremely randomly (to a point when the same code executed several times would pass and fail in subsequent calls). My debug manifest, for completeness, has following permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
I have allowed mock locations in settings.
Aucun commentaire:
Enregistrer un commentaire