Add "Your work policy info" entry in Privacy settings

Bug: 132904820
Test: manual
Change-Id: Id706d450c3ad6a6a8c1e402d39d18e048cdb6519
This commit is contained in:
Ivan Podogov
2019-05-17 15:20:25 +01:00
parent 4af74dd86d
commit 75a10d1fd2
9 changed files with 392 additions and 15 deletions

View File

@@ -18,15 +18,24 @@ package com.android.settings.enterprise;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.net.ConnectivityManager;
@@ -38,9 +47,12 @@ import android.text.SpannableStringBuilder;
import com.android.settings.R;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@@ -346,6 +358,111 @@ public class EnterprisePrivacyFeatureProviderImplTest {
.isEqualTo(3);
}
@Test
public void workPolicyInfo_unmanagedDevice_shouldDoNothing() {
// Even if we have the intent resolved, don't show it if there's no DO or PO
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null);
addWorkPolicyInfoIntent(OWNER.getPackageName(), true, false);
assertThat(mProvider.hasWorkPolicyInfo()).isFalse();
assertThat(mProvider.showWorkPolicyInfo()).isFalse();
verify(mContext, never()).startActivity(any());
}
@Test
public void workPolicyInfo_deviceOwner_shouldResolveIntent() {
// If the intent is not resolved, then there's no info to show for DO
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
assertThat(mProvider.hasWorkPolicyInfo()).isFalse();
assertThat(mProvider.showWorkPolicyInfo()).isFalse();
// If the intent is resolved, then we can use it to launch the activity
Intent intent = addWorkPolicyInfoIntent(OWNER.getPackageName(), true, false);
assertThat(mProvider.hasWorkPolicyInfo()).isTrue();
assertThat(mProvider.showWorkPolicyInfo()).isTrue();
verify(mContext).startActivity(intentEquals(intent));
}
@Test
public void workPolicyInfo_profileOwner_shouldResolveIntent() {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null);
mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
when(mDevicePolicyManager.getProfileOwnerAsUser(MANAGED_PROFILE_USER_ID)).thenReturn(OWNER);
// If the intent is not resolved, then there's no info to show for PO
assertThat(mProvider.hasWorkPolicyInfo()).isFalse();
assertThat(mProvider.showWorkPolicyInfo()).isFalse();
// If the intent is resolved, then we can use it to launch the activity in managed profile
Intent intent = addWorkPolicyInfoIntent(OWNER.getPackageName(), false, true);
assertThat(mProvider.hasWorkPolicyInfo()).isTrue();
assertThat(mProvider.showWorkPolicyInfo()).isTrue();
verify(mContext)
.startActivityAsUser(
intentEquals(intent),
argThat(handle -> handle.getIdentifier() == MANAGED_PROFILE_USER_ID));
}
@Test
public void workPolicyInfo_comp_shouldUseDeviceOwnerIntent() {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE));
when(mDevicePolicyManager.getProfileOwnerAsUser(MY_USER_ID)).thenReturn(OWNER);
// If the intent is not resolved, then there's no info to show for COMP
assertThat(mProvider.hasWorkPolicyInfo()).isFalse();
assertThat(mProvider.showWorkPolicyInfo()).isFalse();
// If the intent is resolved, then we can use it to launch the activity for device owner
Intent intent = addWorkPolicyInfoIntent(OWNER.getPackageName(), true, true);
assertThat(mProvider.hasWorkPolicyInfo()).isTrue();
assertThat(mProvider.showWorkPolicyInfo()).isTrue();
verify(mContext).startActivity(intentEquals(intent));
}
private Intent addWorkPolicyInfoIntent(
String packageName, boolean deviceOwner, boolean profileOwner) {
Intent intent =
new Intent(mResources.getString(R.string.config_work_policy_info_intent_action));
intent.setPackage(packageName);
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.resolvePackageName = packageName;
resolveInfo.activityInfo = new ActivityInfo();
resolveInfo.activityInfo.name = "activityName";
resolveInfo.activityInfo.packageName = packageName;
List<ResolveInfo> activities = ImmutableList.of(resolveInfo);
if (deviceOwner) {
when(mPackageManager.queryIntentActivities(intentEquals(intent), anyInt()))
.thenReturn(activities);
}
if (profileOwner) {
when(mPackageManager.queryIntentActivitiesAsUser(
intentEquals(intent), anyInt(), eq(MANAGED_PROFILE_USER_ID)))
.thenReturn(activities);
}
return intent;
}
private static class IntentMatcher implements ArgumentMatcher<Intent> {
private final Intent mExpectedIntent;
public IntentMatcher(Intent expectedIntent) {
mExpectedIntent = expectedIntent;
}
@Override
public boolean matches(Intent actualIntent) {
// filterEquals() compares only the action, data, type, class, and categories.
return actualIntent != null && mExpectedIntent.filterEquals(actualIntent);
}
}
private static Intent intentEquals(Intent intent) {
return argThat(new IntentMatcher(intent));
}
private void resetAndInitializePackageManager() {
reset(mPackageManager);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN))