DeviceAdminActivity extends FragmentActivity { protected void requestDeviceAdminPermissions() { ComponentName deviceAdmin = new ComponentName(this, DeviceAdmin.class); Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Process will remove device data in case of robbery."); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin); startActivityForResult(intent, 1); }  @Override void onActivityResult(int requestCode, int resultCode, Intent data);  }
@Test public void shouldPromptUserForDeviceAdminPermissionsIfAdminDeviceIsDisabled(){ when(devicePolicyManager.isAdminActive((ComponentName) anyObject())).thenReturn(false); activityController.create(); verify(devicePolicyManager).isAdminActive((ComponentName) anyObject()); verify(deviceAdminActivity).requestDeviceAdminPermissions(); }
@Test public void shouldStartDeviceAdminWhenRequested(){ when(devicePolicyManager.isAdminActive((ComponentName) anyObject())).thenReturn(false); ShadowActivity shadowActivity = shadowOf_(deviceAdminActivity); deviceAdminActivity.requestDeviceAdminPermissions(); ShadowIntent shadowIntent = shadowOf_(shadowActivity.getNextStartedActivity()); verify(deviceAdminActivity).startActivityForResult(Matchers.any(Intent.class), Matchers.anyInt()); assertThat(shadowIntent.getAction(), equalTo(ACTION_ADD_DEVICE_ADMIN)); ComponentName componentName = (ComponentName)shadowIntent.getExtras().get(DevicePolicyManager.EXTRA_DEVICE_ADMIN); assertThat(componentName.getClass().toString(), equalTo(ComponentName.class.toString())); assertThat(shadowIntent.getExtras().get(DevicePolicyManager.EXTRA_ADD_EXPLANATION).toString(), equalTo("Process will remove device data in case of robbery.")); }