Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting Mock Locations not Working
    primarykey
    data
    text
    <pre><code>I have written a simple Gps Service. Now I am writing a testcase for testing it. I am trying to send a mock location but the onLocation changed is not getting called in my GpsService location listener. Here is my GpsService /** * returns the binder object that client of this can bind to */ @Override public IBinder onBind(Intent arg0) { return mBinder; } @Override public void onCreate() { super.onCreate(); Log.e("GpsService","StartService"); } private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { Log.e("GpsService","Location in GpsService"+loc.toString()); if(loc.hasAccuracy() &amp;&amp; loc.getAccuracy() &lt;= minAccuracyMeters) { if(targetLocation != null) { float distance = loc.distanceTo(targetLocation); Log.e("GpsService ","Location latitude +"+loc.getLatitude()+"longitude "+loc.getLongitude()); if(distance &lt; 5.0) Toast.makeText(getBaseContext(), "You have reached the target location", Toast.LENGTH_LONG).show(); } } } public void setTargetLocation (Location _location){ targetLocation = _location; } /** * Class for clients to access. Because we know this service always runs in * the same process as its clients, we don't need to deal with IPC. */ public class LocalBinder extends Binder { GpsService getService() { return GpsService.this; } } private final IBinder mBinder = new LocalBinder(); @Override public void onDestroy() { if(lm != null) lm.removeUpdates(locationListener); super.onDestroy(); } //Setting up for test case public void setUpProvider(String provider){ lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new MyLocationListener(); lm.requestLocationUpdates(provider, minTimeMillis, minDistanceMeters, locationListener); } Now here is my TestCase </code></pre> <p>public class MockLocationTest extends ServiceTestCase {</p> <pre><code>private LocationManager locationManager = null; private static final String MOCK_GPS_PROVIDER = "MOCK_GPS_PROVIDER"; private List&lt;String&gt; data = new ArrayList&lt;String&gt;(); public MockLocationTest(Class&lt;GpsService&gt; serviceClass) { super(serviceClass); } public MockLocationTest() { super(GpsService.class); } @Override protected void setupService() { super.setupService(); System.out.println("Service Set Up"); Intent intent = new Intent(getContext(),GpsService.class); startService(intent); assertNotNull("The Service should not be null",getService()); System.out.println("Service Started"); } @Override protected void setUp() throws Exception { super.setUp(); } @MediumTest public void testBindable() { Intent startIntent = new Intent(); startIntent.setClass(getContext(), GpsService.class); IBinder service = bindService(startIntent); assertNotNull("Bound to service ",service); getService().setUpProvider(MOCK_GPS_PROVIDER); Location demo = new Location(MOCK_GPS_PROVIDER); demo.setLatitude(22.579937); demo.setLongitude(88.486805); getService().setTargetLocation(demo); System.out.println("Test Bindable"); } @SmallTest public void testMockData(){ locationManager = (LocationManager) getContext().getSystemService((Context.LOCATION_SERVICE)); locationManager.setTestProviderEnabled(MOCK_GPS_PROVIDER, true); Location location = new Location(MOCK_GPS_PROVIDER); location.setLatitude(22.579937); location.setLongitude(88.486805); location.setTime(System.currentTimeMillis()); // show debug message in log // provide the new location locationManager.setTestProviderLocation(MOCK_GPS_PROVIDER, location);//send mock data } @Override protected void tearDown() throws Exception { super.tearDown(); } </code></pre> <p>}</p> <p>Unfortunately I cant see any log in the onLocationChange callback in the service. The testcase runs successfully. I have added the following in both my service and testservice's manifest</p> <pre><code> &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/&gt; Can anybody help me here </code></pre> <p>I also tried putting this in one function </p> <p>@LargeTest public void testBindable() {</p> <pre><code> Intent startIntent = new Intent(); startIntent.setClass(getContext(), GpsService.class); IBinder service = bindService(startIntent); assertNotNull("Bound to service ",service); LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); locationManager.addTestProvider(MOCK_GPS_PROVIDER, false, false, false, false, false, false, false, 0, 5); locationManager.setTestProviderEnabled(MOCK_GPS_PROVIDER, true); getService().setUpProvider(MOCK_GPS_PROVIDER,locationManager); Location demo = new Location(MOCK_GPS_PROVIDER); demo.setLatitude(22.579937); demo.setLongitude(88.486805); demo.setTime(System.currentTimeMillis()); getService().setTargetLocation(demo); Location narkeldanga = new Location(MOCK_GPS_PROVIDER); narkeldanga.setLatitude(22.578986); narkeldanga.setLongitude(88.470154); narkeldanga.setTime(System.currentTimeMillis()); locationManager.setTestProviderLocation(MOCK_GPS_PROVIDER, narkeldanga); try { Thread.sleep(10000); // gracefully handle Thread interruption (important!) if(Thread.currentThread().isInterrupted()) throw new InterruptedException(""); } catch (InterruptedException e) { } locationManager.setTestProviderLocation(MOCK_GPS_PROVIDER, demo); try { Thread.sleep(10000); // gracefully handle Thread interruption (important!) if(Thread.currentThread().isInterrupted()) throw new InterruptedException(""); } catch (InterruptedException e) { } System.out.println("Test Bindable"); } </code></pre> <p>but to no avail.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload