AndroidLocationClient extends BaseLocationClient implements android.location.LocationListener { @Override @SuppressLint("MissingPermission") public Location getLastLocation() { String provider = getProvider(); if (provider != null) { return getLocationManager().getLastKnownLocation(provider); } return null; } AndroidLocationClient(@NonNull Context context);   AndroidLocationClient(@NonNull LocationManager locationManager); @Override void start(); @Override void stop(); @Override @SuppressLint("MissingPermission") // Permission checks for location services handled in widgets void requestLocationUpdates(@NonNull LocationListener locationListener); @Override void stopLocationUpdates(); @Override void setListener(@Nullable LocationClientListener locationClientListener); @Override @SuppressLint("MissingPermission") // Permission checks for location services handled in widgets Location getLastLocation(); @Override boolean isMonitoringLocation(); @Override boolean canSetUpdateIntervals(); @Override void setUpdateIntervals(long updateInterval, long fastestUpdateInterval); @Override void resetUpdateIntervals(); @Override void onLocationChanged(Location location); @Override void onStatusChanged(String provider, int status, Bundle extras); @Override void onProviderEnabled(String provider); @Override void onProviderDisabled(String provider);  }
@Test public void getLastLocationShouldReturnCorrectFromCorrectProviderForPriority() { Location gpsLocation = newMockLocation(); when(locationManager.getLastKnownLocation(GPS_PROVIDER)) .thenReturn(gpsLocation); Location networkLocation = newMockLocation(); when(locationManager.getLastKnownLocation(NETWORK_PROVIDER)) .thenReturn(networkLocation); Location passiveLocation = newMockLocation(); when(locationManager.getLastKnownLocation(PASSIVE_PROVIDER)) .thenReturn(passiveLocation); androidLocationClient.setPriority(PRIORITY_HIGH_ACCURACY); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(GPS_PROVIDER, NETWORK_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), gpsLocation); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(NETWORK_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), networkLocation); androidLocationClient.setPriority(PRIORITY_BALANCED_POWER_ACCURACY); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(GPS_PROVIDER, NETWORK_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), networkLocation); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(GPS_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), gpsLocation); androidLocationClient.setPriority(PRIORITY_LOW_POWER); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(NETWORK_PROVIDER, PASSIVE_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), networkLocation); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(PASSIVE_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), passiveLocation); androidLocationClient.setPriority(PRIORITY_NO_POWER); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of(PASSIVE_PROVIDER)); assertSame(androidLocationClient.getLastLocation(), passiveLocation); when(locationManager.getProviders(true)) .thenReturn(ImmutableList.of()); assertNull(androidLocationClient.getLastLocation()); }