Merge "Add "Your work policy info" entry in Privacy settings" into qt-dev am: 9a1c75c57a am: 8573d27d31

am: aa813f5618

Change-Id: I2a1cb8143c105d4a03c4bffc34ba7bedfa9a83f4
This commit is contained in:
Ivan Podogov
2019-06-17 06:03:09 -07:00
committed by android-build-merger
9 changed files with 392 additions and 15 deletions

View File

@@ -24,4 +24,7 @@
<!-- Fake dimen value for restricted icon size - needed to get around Robolectric
issue loading framework hidden resources -->
<dimen name="restricted_icon_size">24dp</dimen>
</resources>
<!-- Fake string to avoid empty intent action -->
<string name="config_work_policy_info_intent_action">ACTION_SHOW_WORK_POLICY_INFO</string>
</resources>

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))

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privacy;
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import androidx.preference.Preference;
import com.android.settings.enterprise.EnterprisePrivacyFeatureProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class WorkPolicyInfoPreferenceControllerTest {
private Context mContext;
private FakeFeatureFactory mFakeFeatureFactory;
private EnterprisePrivacyFeatureProvider mEnterpriseProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mEnterpriseProvider = mFakeFeatureFactory.getEnterprisePrivacyFeatureProvider(mContext);
}
@Test
public void getAvailabilityStatus_noWorkPolicyInfo_shouldReturnUnsupported() {
when(mEnterpriseProvider.hasWorkPolicyInfo()).thenReturn(false);
WorkPolicyInfoPreferenceController controller =
new WorkPolicyInfoPreferenceController(mContext, "test_key");
assertThat(controller.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_haveWorkPolicyInfo_shouldReturnAvailableUnsearchable() {
when(mEnterpriseProvider.hasWorkPolicyInfo()).thenReturn(true);
WorkPolicyInfoPreferenceController controller =
new WorkPolicyInfoPreferenceController(mContext, "test_key");
assertThat(controller.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
}
@Test
public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() {
when(mEnterpriseProvider.hasWorkPolicyInfo()).thenReturn(true);
WorkPolicyInfoPreferenceController controller =
new WorkPolicyInfoPreferenceController(mContext, "test_key");
final Preference pref = new Preference(mContext);
assertThat(controller.handlePreferenceTreeClick(pref)).isFalse();
verify(mEnterpriseProvider, never()).showWorkPolicyInfo();
}
@Test
public void handlePreferenceTreeClick_matchingKey_shouldShowWorkPolicyInfo() {
when(mEnterpriseProvider.hasWorkPolicyInfo()).thenReturn(true);
WorkPolicyInfoPreferenceController controller =
new WorkPolicyInfoPreferenceController(mContext, "test_key");
final Preference pref = new Preference(mContext);
pref.setKey(controller.getPreferenceKey());
assertThat(controller.handlePreferenceTreeClick(pref)).isTrue();
verify(mEnterpriseProvider).showWorkPolicyInfo();
}
}