CTGeofenceBootReceiver extends BroadcastReceiver { @MainThread @Override public void onReceive(final Context context, Intent intent) { final Context applicationContext = context.getApplicationContext(); CTGeofenceAPI.getLogger().debug(GEOFENCE_LOG_TAG, "onReceive called after " + "device reboot"); if (intent == null || intent.getAction() == null) { return; } if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { if (!Utils.hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "We don't have ACCESS_FINE_LOCATION permission! Not registering " + "geofences and location updates after device reboot"); return; } if (!Utils.hasBackgroundLocationPermission(context)) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "We don't have ACCESS_BACKGROUND_LOCATION permission! not registering " + "geofences and location updates after device reboot"); return; } final PendingResult result = goAsync(); Thread thread = new Thread() { public void run() { try { if (!Utils.initCTGeofenceApiIfRequired(applicationContext)) { finishPendingIntent(result); return; } CTGeofenceAPI.getLogger().info(GEOFENCE_LOG_TAG, "registering geofences after device reboot"); GeofenceUpdateTask geofenceUpdateTask = new GeofenceUpdateTask(applicationContext, null); Future<?> futureGeofence = CTGeofenceTaskManager.getInstance() .postAsyncSafely("ProcessGeofenceUpdatesOnBoot", geofenceUpdateTask); try { if (futureGeofence != null) { futureGeofence.get(BROADCAST_INTENT_TIME_MS, TimeUnit.MILLISECONDS); } } catch (TimeoutException e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Timeout geofence update task execution limit of 3 secs"); } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Exception while executing geofence update task"); e.printStackTrace(); } CTGeofenceAPI.getLogger().info(GEOFENCE_LOG_TAG, "registering location updates after device reboot"); LocationUpdateTask locationUpdateTask = new LocationUpdateTask(applicationContext); Future<?> futureLocation = CTGeofenceTaskManager.getInstance() .postAsyncSafely("IntitializeLocationUpdatesOnBoot", locationUpdateTask); try { if (futureLocation != null) { futureLocation.get(BROADCAST_INTENT_TIME_MS, TimeUnit.MILLISECONDS); } } catch (TimeoutException e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Timeout location update task execution limit of 3 secs"); } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Exception while executing location update task"); e.printStackTrace(); } finishPendingIntent(result); } catch (Exception e) { finishPendingIntent(result); CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Exception while processing Boot receiver intent"); e.printStackTrace(); } } }; thread.start(); } }  @MainThread @Override void onReceive(final Context context, Intent intent);  }
@Test public void testOnReceiveWhenIntentIstNull() { CTGeofenceBootReceiver receiver = new CTGeofenceBootReceiver(); CTGeofenceBootReceiver spy = Mockito.spy(receiver); spy.onReceive(application, null); verify(spy, never()).goAsync(); }
@Test public void testOnReceiveWhenIntentNotNullTC1() { CTGeofenceBootReceiver receiver = new CTGeofenceBootReceiver(); CTGeofenceBootReceiver spy = Mockito.spy(receiver); when(spy.goAsync()).thenReturn(pendingResult); final Boolean[] isFinished = {false}; doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { isFinished[0] = true; return null; } }).when(pendingResult).finish(); PowerMockito.when(Utils.hasPermission(application, Manifest.permission.ACCESS_FINE_LOCATION)) .thenReturn(true); PowerMockito.when(Utils.hasBackgroundLocationPermission(application)).thenReturn(true); PowerMockito.when(Utils.initCTGeofenceApiIfRequired(application)).thenReturn(true); Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); spy.onReceive(application, intent); await().until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return isFinished[0]; } }); verify(pendingResult).finish(); }
@Test public void testOnReceiveWhenIntentNotNullTC2() { CTGeofenceBootReceiver receiver = new CTGeofenceBootReceiver(); CTGeofenceBootReceiver spy = Mockito.spy(receiver); when(spy.goAsync()).thenReturn(pendingResult); final Boolean[] isFinished = {false}; doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { isFinished[0] = true; return null; } }).when(pendingResult).finish(); PowerMockito.when(Utils.hasPermission(application, Manifest.permission.ACCESS_FINE_LOCATION)) .thenReturn(true); PowerMockito.when(Utils.hasBackgroundLocationPermission(application)).thenReturn(true); PowerMockito.when(Utils.initCTGeofenceApiIfRequired(application)).thenReturn(false); Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); spy.onReceive(application, intent); await().until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return isFinished[0]; } }); verify(pendingResult).finish(); }
@Test public void testOnReceiveWhenIntentNotNullTC3() { CTGeofenceBootReceiver receiver = new CTGeofenceBootReceiver(); CTGeofenceBootReceiver spy = Mockito.spy(receiver); PowerMockito.when(Utils.hasPermission(application, Manifest.permission.ACCESS_FINE_LOCATION)) .thenReturn(false); Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); spy.onReceive(application, intent); verify(spy, never()).goAsync(); }
@Test public void testOnReceiveWhenIntentNotNullTC4() { CTGeofenceBootReceiver receiver = new CTGeofenceBootReceiver(); CTGeofenceBootReceiver spy = Mockito.spy(receiver); PowerMockito.when(Utils.hasPermission(application, Manifest.permission.ACCESS_FINE_LOCATION)) .thenReturn(true); PowerMockito.when(Utils.hasBackgroundLocationPermission(application)) .thenReturn(false); Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); spy.onReceive(application, intent); verify(spy, never()).goAsync(); }