-1

I have a AndroidTestCase , I run my test in real device with Android 4.1.2 OS.

public class MyLocationTest extends AndroidTestCase{
  private String TEST_PROVIDER = LocationManager.NETWORK_PROVIDER;

  @Override
  public void setUp() throws Exception{
     super.setUp();

     mLocationManager = (LocationManager) getContext()
                                 .getSystemService(Context.LOCATION_SERVICE);

     //I check what are the providers available, 
     //the log shows me: network, gps, passive
     List<String> providers = mLocationManager.getAllProviders();
     for(int i=0; i<providers.size(); i++){
        Log.i("DEBUG", "Provider: "+providers.get(i));
     }

    //The following code raise Exception: java.lang.IllegalArgumentException: Provider "network" unknown
    mLocationManager.setTestProviderEnabled(TEST_PROVIDER, true);
    mLocationManager.setTestProviderStatus(TEST_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
  }

}

In the setUp() phase of my above test case, I checked the available providers, and got "network" in log. Then, I invoke setTestProviderEnabled(TEST_PROVIDER, true) , but I got Exception: java.lang.IllegalArgumentException: Provider "network" unknown

I don't understand why?? I have "network" provider but I got this exception...

By the way, I have enabled "Allow mocked location" on device, also have wifi network connected.

Mellon
  • 37,586
  • 78
  • 186
  • 264

1 Answers1

2

You need to create a mock location provider and add it to the active providers.

mLocationManager.addTestProvider(TEST_PROVIDER, false, 
false, false, false, false, true, true, 0, 5);
// your code
mLocationManager.setTestProviderEnabled(TEST_PROVIDER, true);

Hope this helps.

sam
  • 2,780
  • 1
  • 17
  • 30