@Test public void requestLocationUpdatesShouldCallLocationManagerRequestLocationUpdates() { LocationListener locationListener = Mockito.mock(LocationListener.class); LocationManager locationManager = Mockito.spy((LocationManager) ReflectionHelpers.getField(androidGpsLocationClient, "locationManager")); ReflectionHelpers.setField(androidGpsLocationClient, "locationManager", locationManager); 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); Mockito.doReturn(true).when(locationManager).registerGnssStatusCallback(Mockito.any(GnssStatus.Callback.class)); Mockito.doReturn(location).when(locationManager).getLastKnownLocation(Mockito.eq(LocationManager.GPS_PROVIDER)); Mockito.doReturn(true).when(locationManager).isProviderEnabled(Mockito.eq(LocationManager.GPS_PROVIDER)); Mockito.doNothing().when(locationManager).requestLocationUpdates(Mockito.anyString(), Mockito.anyLong(), Mockito.anyFloat(), Mockito.any(LocationListener.class)); androidGpsLocationClient.requestLocationUpdates(locationListener); Mockito.verify(locationManager, Mockito.times(1)) .requestLocationUpdates(Mockito.eq(LocationManager.GPS_PROVIDER), Mockito.eq(1000L), Mockito.eq(0f), Mockito.any(LocationListener.class)); }
@Override public void requestLocationUpdates(@NonNull LocationListener locationListener) { addLocationListener(locationListener); if (isProviderEnabled()) { try { Location location = locationManager.getLastKnownLocation(getProvider()); if (location != null && (lastLocation == null || (location != null && isBetterLocation(location, lastLocation)))) { lastLocation = location; locationListener.onLocationChanged(lastLocation); } else { Toast.makeText(context, R.string.retrieving_gps_location_couple_of_minutes, Toast.LENGTH_SHORT).show(); } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER , fastestUpdateInterval , 0f , androidGpsLocationListener); registerForGpsStoppedEvent(); } catch (SecurityException e) { Timber.e(e); Toast.makeText(context, R.string.location_disabled_location_permissions_not_granted, Toast.LENGTH_LONG) .show(); } } else { removeLocationListener(locationListener); Timber.e(new Exception(), "The provider (" + getProvider() + ") is not enabled"); } }
AndroidGpsLocationClient extends BaseLocationClient { @Override public void requestLocationUpdates(@NonNull LocationListener locationListener) { addLocationListener(locationListener); if (isProviderEnabled()) { try { Location location = locationManager.getLastKnownLocation(getProvider()); if (location != null && (lastLocation == null || (location != null && isBetterLocation(location, lastLocation)))) { lastLocation = location; locationListener.onLocationChanged(lastLocation); } else { Toast.makeText(context, R.string.retrieving_gps_location_couple_of_minutes, Toast.LENGTH_SHORT).show(); } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER , fastestUpdateInterval , 0f , androidGpsLocationListener); registerForGpsStoppedEvent(); } catch (SecurityException e) { Timber.e(e); Toast.makeText(context, R.string.location_disabled_location_permissions_not_granted, Toast.LENGTH_LONG) .show(); } } else { removeLocationListener(locationListener); Timber.e(new Exception(), "The provider (" + getProvider() + ") is not enabled"); } } }
AndroidGpsLocationClient extends BaseLocationClient { @Override public void requestLocationUpdates(@NonNull LocationListener locationListener) { addLocationListener(locationListener); if (isProviderEnabled()) { try { Location location = locationManager.getLastKnownLocation(getProvider()); if (location != null && (lastLocation == null || (location != null && isBetterLocation(location, lastLocation)))) { lastLocation = location; locationListener.onLocationChanged(lastLocation); } else { Toast.makeText(context, R.string.retrieving_gps_location_couple_of_minutes, Toast.LENGTH_SHORT).show(); } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER , fastestUpdateInterval , 0f , androidGpsLocationListener); registerForGpsStoppedEvent(); } catch (SecurityException e) { Timber.e(e); Toast.makeText(context, R.string.location_disabled_location_permissions_not_granted, Toast.LENGTH_LONG) .show(); } } else { removeLocationListener(locationListener); Timber.e(new Exception(), "The provider (" + getProvider() + ") is not enabled"); } } AndroidGpsLocationClient(@NonNull Context context); }
AndroidGpsLocationClient extends BaseLocationClient { @Override public void requestLocationUpdates(@NonNull LocationListener locationListener) { addLocationListener(locationListener); if (isProviderEnabled()) { try { Location location = locationManager.getLastKnownLocation(getProvider()); if (location != null && (lastLocation == null || (location != null && isBetterLocation(location, lastLocation)))) { lastLocation = location; locationListener.onLocationChanged(lastLocation); } else { Toast.makeText(context, R.string.retrieving_gps_location_couple_of_minutes, Toast.LENGTH_SHORT).show(); } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER , fastestUpdateInterval , 0f , androidGpsLocationListener); registerForGpsStoppedEvent(); } catch (SecurityException e) { Timber.e(e); Toast.makeText(context, R.string.location_disabled_location_permissions_not_granted, Toast.LENGTH_LONG) .show(); } } else { removeLocationListener(locationListener); Timber.e(new Exception(), "The provider (" + getProvider() + ") is not enabled"); } } 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(); }
AndroidGpsLocationClient extends BaseLocationClient { @Override public void requestLocationUpdates(@NonNull LocationListener locationListener) { addLocationListener(locationListener); if (isProviderEnabled()) { try { Location location = locationManager.getLastKnownLocation(getProvider()); if (location != null && (lastLocation == null || (location != null && isBetterLocation(location, lastLocation)))) { lastLocation = location; locationListener.onLocationChanged(lastLocation); } else { Toast.makeText(context, R.string.retrieving_gps_location_couple_of_minutes, Toast.LENGTH_SHORT).show(); } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER , fastestUpdateInterval , 0f , androidGpsLocationListener); registerForGpsStoppedEvent(); } catch (SecurityException e) { Timber.e(e); Toast.makeText(context, R.string.location_disabled_location_permissions_not_granted, Toast.LENGTH_LONG) .show(); } } else { removeLocationListener(locationListener); Timber.e(new Exception(), "The provider (" + getProvider() + ") is not enabled"); } } 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();  }