Test location provider in an Android device testing project

I have an application that uses LocationManager. Therefore, I am now writing a control test. I already found a similar answer, but this will not work for me.

public class LocationSensorTest extends AndroidTestCase {
  /*package*/ LocationManager lm;
  private LocationSensor sensor;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    sensor = new LocationSensor(getContext());
    lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
    lm.addTestProvider("test", false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
    lm.setTestProviderEnabled("test", true);
  }

  public void testHasAnyActiveLocationProvider() {
    assertTrue(sensor.hasAnyActiveLocationProvider());
  }
}

The test does not work during "addTestProvider" with a SecurityException exception that "android.permission.ACCESS_MOCK_LOCATION" is missing. The fact is that in the hardware test AndroidManifest.xml this is permission, but the application is not tested.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.app.android.tests" android:versionCode="1" android:versionName="1.0">
  <application>
    <uses-library android:name="android.test.runner" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
  </application>
  <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="de.app.android" android:label="Requester App Tests" />
  <uses-sdk android:minSdkVersion="4" />
</manifest> 

Does anyone know how to solve this?

+3
source share
1 answer
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

should be out <application></application>

+1
source

Source: https://habr.com/ru/post/1774969/


All Articles