LocationUpdateTask implements CTGeofenceTask { @WorkerThread @Override public void execute() { if (ctLocationAdapter == null) { return; } if (ctGeofenceSettings == null) { ctGeofenceSettings = CTGeofenceAPI.getInstance(context).initDefaultConfig(); } CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Executing LocationUpdateTask..."); PendingIntent locationPendingIntent = PendingIntentFactory.getPendingIntent(context, PendingIntentFactory.PENDING_INTENT_LOCATION, FLAG_NO_CREATE); if (!this.ctGeofenceSettings.isBackgroundLocationUpdatesEnabled() && locationPendingIntent != null) { ctLocationAdapter.removeLocationUpdates(locationPendingIntent); } else if (isRequestLocation(locationPendingIntent)) { ctLocationAdapter.requestLocationUpdates(); } else { CTGeofenceAPI.getLogger().verbose(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Dropping duplicate location update request"); } Utils.writeSettingsToFile(context, ctGeofenceSettings); if (onCompleteListener != null) { onCompleteListener.onComplete(); } CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Finished executing LocationUpdateTask"); } LocationUpdateTask(Context context); @WorkerThread @Override void execute(); @Override void setOnCompleteListener(@NonNull OnCompleteListener onCompleteListener);  }
@Test public void testExecuteTC2() { CTGeofenceSettings ctGeofenceSettings = new CTGeofenceSettings.Builder() .enableBackgroundLocationUpdates(false).build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(ctGeofenceSettings); LocationUpdateTask task = new LocationUpdateTask(application); task.execute(); verify(ctLocationAdapter, never()).requestLocationUpdates(); verify(ctLocationAdapter, never()).removeLocationUpdates(any(PendingIntent.class)); verifyStatic(Utils.class); Utils.writeSettingsToFile(any(Context.class), any(CTGeofenceSettings.class)); }
@Test public void testExecuteTC3() { CTGeofenceSettings ctGeofenceSettings = new CTGeofenceSettings.Builder() .enableBackgroundLocationUpdates(false).build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(ctGeofenceSettings); PendingIntent pendingIntent = PendingIntentFactory.getPendingIntent(application, PendingIntentFactory.PENDING_INTENT_LOCATION, PendingIntent.FLAG_UPDATE_CURRENT); LocationUpdateTask task = new LocationUpdateTask(application); task.execute(); verify(ctLocationAdapter).removeLocationUpdates(any(PendingIntent.class)); verify(ctLocationAdapter, never()).requestLocationUpdates(); verifyStatic(Utils.class); Utils.writeSettingsToFile(any(Context.class), any(CTGeofenceSettings.class)); }
@Test public void testExecuteWhenLocationAdapterIsNull() { WhiteboxImpl.setInternalState(ctGeofenceAPI, "ctLocationAdapter", (Object[]) null); LocationUpdateTask task = new LocationUpdateTask(application); task.execute(); verifyStatic(Utils.class, never()); Utils.writeSettingsToFile(any(Context.class), any(CTGeofenceSettings.class)); }