EnableFeatureAction extends Action { @NonNull @Override public ActionResult perform(@NonNull ActionArguments arguments) { String feature = arguments.getValue().getString(); Checks.checkNotNull(feature, "Missing feature."); AirshipLocationClient locationClient = UAirship.shared().getLocationClient(); switch (feature) { case FEATURE_BACKGROUND_LOCATION: if (locationClient == null) { return ActionResult.newEmptyResult(); } if (requestLocationPermissions()) { locationClient.setLocationUpdatesEnabled(true); locationClient.setBackgroundLocationAllowed(true); return ActionResult.newResult(ActionValue.wrap(true)); } return ActionResult.newResult(ActionValue.wrap(false)); case FEATURE_LOCATION: if (locationClient == null) { return ActionResult.newEmptyResult(); } if (requestLocationPermissions()) { locationClient.setLocationUpdatesEnabled(true); return ActionResult.newResult(ActionValue.wrap(true)); } return ActionResult.newResult(ActionValue.wrap(false)); case FEATURE_USER_NOTIFICATIONS: UAirship.shared().getPushManager().setUserNotificationsEnabled(true); if (!NotificationManagerCompat.from(UAirship.getApplicationContext()).areNotificationsEnabled()) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { navigateToNotificationSettings(); } }); } return ActionResult.newResult(ActionValue.wrap(true)); } return ActionResult.newResult(ActionValue.wrap(false)); } EnableFeatureAction(@NonNull PermissionsRequester permissionsRequester);  EnableFeatureAction(); @Override boolean acceptsArguments(@NonNull ActionArguments arguments); @NonNull @Override ActionResult perform(@NonNull ActionArguments arguments); @NonNull
    static final String DEFAULT_REGISTRY_NAME; @NonNull
    static final String DEFAULT_REGISTRY_SHORT_NAME; @NonNull
    static final String FEATURE_USER_NOTIFICATIONS; @NonNull
    static final String FEATURE_LOCATION; @NonNull
    static final String FEATURE_BACKGROUND_LOCATION; }
@Test public void testEnableUserNotifications() { ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, EnableFeatureAction.FEATURE_USER_NOTIFICATIONS); ActionResult result = action.perform(args); verify(pushManager).setUserNotificationsEnabled(true); assertTrue(result.getValue().getBoolean(false)); }
@Test public void testEnableLocation() { when(permissionsRequester.requestPermissions(any(Context.class), eq(Arrays.asList(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)))) .thenReturn(new int[] { PERMISSION_GRANTED, PERMISSION_GRANTED }); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, EnableFeatureAction.FEATURE_LOCATION); ActionResult result = action.perform(args); verify(locationClient).setLocationUpdatesEnabled(true); assertTrue(result.getValue().getBoolean(false)); }
@Test public void testEnableLocationPermissionDenied() { when(permissionsRequester.requestPermissions(any(Context.class), eq(Arrays.asList(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)))) .thenReturn(new int[] { PERMISSION_DENIED, PERMISSION_DENIED }); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, EnableFeatureAction.FEATURE_LOCATION); ActionResult result = action.perform(args); verifyZeroInteractions(locationClient); assertFalse(result.getValue().getBoolean(true)); }
@Test public void testEnableBackgroundLocation() { when(permissionsRequester.requestPermissions(any(Context.class), eq(Arrays.asList(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)))) .thenReturn(new int[] { PERMISSION_GRANTED, PERMISSION_GRANTED }); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, EnableFeatureAction.FEATURE_BACKGROUND_LOCATION); ActionResult result = action.perform(args); verify(locationClient).setLocationUpdatesEnabled(true); verify(locationClient).setBackgroundLocationAllowed(true); assertTrue(result.getValue().getBoolean(false)); }
@Test public void testEnableBackgroundLocationPermissionDenied() { when(permissionsRequester.requestPermissions(any(Context.class), eq(Arrays.asList(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)))) .thenReturn(new int[] { PERMISSION_DENIED, PERMISSION_DENIED }); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, EnableFeatureAction.FEATURE_BACKGROUND_LOCATION); ActionResult result = action.perform(args); verifyZeroInteractions(locationClient); assertFalse(result.getValue().getBoolean(true)); }