AndroidGpsLocationClient extends BaseLocationClient { @SuppressLint("MissingPermission") @Nullable @Override public Location getLastLocation() { Location lastLocationFromProvider = locationManager.getLastKnownLocation(getProvider()); return lastLocationFromProvider != null && (lastLocation == null || lastLocationFromProvider.getTime() > lastLocation.getTime()) ? lastLocationFromProvider : lastLocation; } AndroidGpsLocationClient(@NonNull Context context); @Override void stopLocationUpdates(); @SuppressLint("MissingPermission") @Nullable @Override Location getLastLocation(); @Override void requestLocationUpdates(@NonNull LocationListener locationListener); @Override void setUpdateIntervals(long updateInterval, long fastestUpdateInterval); @NonNull @Override String getProvider(); @Override void close();  }
@Test public void androidGpsLocationClientOnLocationChangedShouldCallRegisteredLocationListenerAndUpdateLastLocation() { androidGpsLocationClient = new AndroidGpsLocationClient(RuntimeEnvironment.application); LocationListener locationListener = Mockito.mock(LocationListener.class); LocationListener locationListener2 = Mockito.mock(LocationListener.class); KujakuLibrary kujakuLibrary = Mockito.mock(KujakuLibrary.class); ReflectionHelpers.setStaticField(KujakuLibrary.class, "library", kujakuLibrary); Mockito.doNothing().when(kujakuLibrary).showToast(Mockito.anyString()); Location location = new Location(LocationManager.GPS_PROVIDER); location.setAccuracy(4f); location.setAltitude(23f); location.setLatitude(3d); location.setLongitude(1d); androidGpsLocationClient.addLocationListener(locationListener); androidGpsLocationClient.setLocationListener(locationListener2); AndroidGpsLocationClient.AndroidGpsLocationListener androidGpsLocationListener = ReflectionHelpers.getField(androidGpsLocationClient, "androidGpsLocationListener"); androidGpsLocationListener.onLocationChanged(location); Mockito.verify(locationListener).onLocationChanged(location); Mockito.verify(locationListener2).onLocationChanged(location); Assert.assertEquals(location, androidGpsLocationClient.getLastLocation()); }
@Test public void getLastLocationShouldRequestLastLocationFromLocationManager() { LocationManager locationManager = Mockito.spy((LocationManager) ReflectionHelpers.getField(androidGpsLocationClient, "locationManager")); ReflectionHelpers.setField(androidGpsLocationClient, "locationManager", locationManager); Location location = new Location(LocationManager.GPS_PROVIDER); location.setAccuracy(4f); location.setAltitude(23f); location.setLatitude(3d); location.setLongitude(1d); Mockito.doReturn(location).when(locationManager).getLastKnownLocation(LocationManager.GPS_PROVIDER); Location lastLocation = androidGpsLocationClient.getLastLocation(); Assert.assertEquals(location, lastLocation); Mockito.verify(locationManager).getLastKnownLocation(LocationManager.GPS_PROVIDER); }
@Test public void getLastLocationShouldReturnLatestLocationWhenCachedLocationAndLocatioManagerProvideLocations() { LocationManager locationManager = Mockito.spy((LocationManager) ReflectionHelpers.getField(androidGpsLocationClient, "locationManager")); ReflectionHelpers.setField(androidGpsLocationClient, "locationManager", locationManager); Location location = new Location(LocationManager.GPS_PROVIDER); location.setAccuracy(4f); location.setAltitude(23f); location.setLatitude(3d); location.setLongitude(1d); location.setTime(900); Location locationFromLocationManager = new Location(LocationManager.GPS_PROVIDER); locationFromLocationManager.setAccuracy(4f); locationFromLocationManager.setAltitude(23f); locationFromLocationManager.setLatitude(3d); locationFromLocationManager.setLongitude(1d); locationFromLocationManager.setTime(1000); Mockito.doReturn(locationFromLocationManager).when(locationManager).getLastKnownLocation(LocationManager.GPS_PROVIDER); ReflectionHelpers.setField(androidGpsLocationClient, "lastLocation", location); Location lastLocation = androidGpsLocationClient.getLastLocation(); Assert.assertEquals(locationFromLocationManager, lastLocation); Mockito.verify(locationManager).getLastKnownLocation(LocationManager.GPS_PROVIDER); }