I recorded an espresso test with Espresso Recorder. I want to test some location changes in my app.
Currently I'm mocking the location with this code:
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_FINE );
String mocLocationProvider = LocationManager.GPS_PROVIDER;//lm.getBestProvider( criteria, true );
lm.addTestProvider(mocLocationProvider, false, false,
false, false, true, true, true, 0, 5);
lm.setTestProviderEnabled(mocLocationProvider, true);
Location loc = new Location(mocLocationProvider);
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(-26.902038); // double
mockLocation.setLongitude(-48.671337);
mockLocation.setAltitude(loc.getAltitude());
mockLocation.setTime(System.currentTimeMillis());
mockLocation.setAccuracy(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
lm.setTestProviderLocation( mocLocationProvider, mockLocation);
I also added the permission to the debug manifest file:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
But unluckily I still get a Security Exception:
java.lang.SecurityException: mypackage.test from uid not allowed to perform MOCK_LOCATION
I want to run the recorded test case with the mocked location in Google Firebase Test Lab. How can I solve this problem?
kotlin import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation executeShellCommand("appops set " + InstrumentationRegistry.getInstrumentation().context.packageName + " android:mock_location allow")
– Colchester