GoogleLocationAdapter implements CTLocationAdapter { @WorkerThread @Override public void requestLocationUpdates() { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "requestLocationUpdates() called"); applySettings(context); if (!backgroundLocationUpdatesEnabled) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "not requesting location updates since background location updates is not enabled"); if (CTGeofenceAPI.getInstance(context).getCleverTapApi() != null) { CTGeofenceAPI.getInstance(context) .getCleverTapApi() .pushGeoFenceError(CTGeofenceConstants.ERROR_CODE, "not requesting location updates since background location updates is not enabled"); } return; } if (locationFetchMode == CTGeofenceSettings.FETCH_CURRENT_LOCATION_PERIODIC) { PendingIntent pendingIntent = PendingIntentFactory.getPendingIntent(context, PendingIntentFactory.PENDING_INTENT_LOCATION, FLAG_UPDATE_CURRENT); clearLocationWorkRequest(); CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "requesting current location periodically.."); try { @SuppressLint("MissingPermission") Task<Void> requestLocationUpdatesTask = fusedProviderClient .requestLocationUpdates(getLocationRequest(), pendingIntent); Tasks.await(requestLocationUpdatesTask); CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Finished requesting current location periodically.."); } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Failed to request location updates"); e.printStackTrace(); } } else { PendingIntent pendingIntent = PendingIntentFactory.getPendingIntent(context, PendingIntentFactory.PENDING_INTENT_LOCATION, FLAG_NO_CREATE); clearLocationUpdates(pendingIntent); scheduleManualLocationUpdates(); } } GoogleLocationAdapter(@NonNull Context context); @WorkerThread @Override void getLastLocation(@NonNull final CTLocationCallback callback); @WorkerThread @Override void removeLocationUpdates(@Nullable PendingIntent pendingIntent); @WorkerThread @Override void requestLocationUpdates();  }
@Test public void testRequestLocationUpdatesTC1() throws Exception { when(LocationServices.getFusedLocationProviderClient(application)) .thenReturn(providerClient); GoogleLocationAdapter locationAdapter = new GoogleLocationAdapter(application); CTGeofenceSettings ctGeofenceSettings = new CTGeofenceSettings.Builder() .setLocationFetchMode(CTGeofenceSettings.FETCH_CURRENT_LOCATION_PERIODIC) .enableBackgroundLocationUpdates(false).build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(ctGeofenceSettings); locationAdapter.requestLocationUpdates(); verify(cleverTapAPI).pushGeoFenceError(anyInt(), anyString()); verify(providerClient, never()).requestLocationUpdates( any(LocationRequest.class), any(PendingIntent.class)); }
@Test public void testRequestLocationUpdatesTC2() throws Exception { when(LocationServices.getFusedLocationProviderClient(application)) .thenReturn(providerClient); when(Utils.isConcurrentFuturesDependencyAvailable()) .thenReturn(true); GoogleLocationAdapter locationAdapter = new GoogleLocationAdapter(application); CTGeofenceSettings ctGeofenceSettings = new CTGeofenceSettings.Builder() .setLocationFetchMode(CTGeofenceSettings.FETCH_CURRENT_LOCATION_PERIODIC) .enableBackgroundLocationUpdates(true).build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(ctGeofenceSettings); WorkManager workManager = WorkManager.getInstance(application); PeriodicWorkRequest locationRequest = new PeriodicWorkRequest.Builder(BackgroundLocationWork.class, 1800000, TimeUnit.MILLISECONDS, 600000, TimeUnit.MILLISECONDS) .build(); workManager.enqueueUniquePeriodicWork(TAG_WORK_LOCATION_UPDATES, ExistingPeriodicWorkPolicy.KEEP, locationRequest).getResult().get(); locationAdapter.requestLocationUpdates(); WorkInfo workInfo = workManager.getWorkInfoById(locationRequest.getId()).get(); assertThat(workInfo.getState(), is(WorkInfo.State.CANCELLED)); verify(providerClient).requestLocationUpdates( any(LocationRequest.class), any(PendingIntent.class)); verifyStatic(Tasks.class); Tasks.await(null); }