TrustAgentSettings use DashboardFragment

- Remove dummy preference from trust_agent_setting.xml.
- Build a controller to generate/manage a list of preferences.
- Move some logics to the controller and add tests.

Bug: 73899467
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.security
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.core
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.search
Test: atest SettingsGatewayTest UniquePreferenceTest
Change-Id: Ideae0c1e7311d7647cf522e01592822e565a0ff7
This commit is contained in:
Mill Chen
2018-04-13 03:59:19 +00:00
committed by Fan Zhang
parent 02aaca9529
commit fc64a6c8ad
9 changed files with 541 additions and 180 deletions

View File

@@ -57,7 +57,6 @@ com.android.settings.network.ApnSettings
com.android.settings.wifi.calling.WifiCallingSettingsForSub
com.android.settings.password.SetupChooseLockGeneric$SetupChooseLockGenericFragment
com.android.settings.SetupRedactionInterstitial$SetupRedactionInterstitialFragment
com.android.settings.security.trustagent.TrustAgentSettings
com.android.settings.password.ChooseLockGeneric$ChooseLockGenericFragment
com.android.settings.IccLockSettings
com.android.settings.TetherSettings

View File

@@ -16,8 +16,10 @@
package com.android.settings.security.trustagent;
import static com.android.settings.security.trustagent.TrustAgentListPreferenceController.PREF_KEY_SECURITY_CATEGORY;
import static com.android.settings.security.trustagent.TrustAgentListPreferenceController.PREF_KEY_TRUST_AGENT;
import static com.android.settings.security.trustagent.TrustAgentListPreferenceController
.PREF_KEY_SECURITY_CATEGORY;
import static com.android.settings.security.trustagent.TrustAgentListPreferenceController
.PREF_KEY_TRUST_AGENT;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
@@ -27,12 +29,8 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import androidx.lifecycle.LifecycleOwner;
import android.content.ComponentName;
import android.content.Context;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.PreferenceControllerMixin;
@@ -52,6 +50,11 @@ import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
public class TrustAgentListPreferenceControllerTest {
@@ -81,9 +84,9 @@ public class TrustAgentListPreferenceControllerTest {
mLifecycle = new Lifecycle(mLifecycleOwner);
mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(any(Context.class)))
.thenReturn(mLockPatternUtils);
.thenReturn(mLockPatternUtils);
when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager())
.thenReturn(mTrustAgentManager);
.thenReturn(mTrustAgentManager);
when(mCategory.getKey()).thenReturn(PREF_KEY_SECURITY_CATEGORY);
when(mCategory.getContext()).thenReturn(mActivity);
when(mScreen.findPreference(PREF_KEY_SECURITY_CATEGORY)).thenReturn(mCategory);
@@ -121,14 +124,14 @@ public class TrustAgentListPreferenceControllerTest {
public void onResume_shouldAddNewAgents() {
final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
final TrustAgentManager.TrustAgentComponentInfo agent =
mock(TrustAgentManager.TrustAgentComponentInfo.class);
mock(TrustAgentManager.TrustAgentComponentInfo.class);
agent.title = "Test_title";
agent.summary = "test summary";
agent.componentName = new ComponentName("pkg", "agent");
agent.admin = null;
agents.add(agent);
when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
.thenReturn(agents);
.thenReturn(agents);
mController.displayPreference(mScreen);
mController.onResume();
@@ -141,7 +144,7 @@ public class TrustAgentListPreferenceControllerTest {
public void onResume_ifNotAvailable_shouldNotAddNewAgents() {
final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
final TrustAgentManager.TrustAgentComponentInfo agent =
mock(TrustAgentManager.TrustAgentComponentInfo.class);
mock(TrustAgentManager.TrustAgentComponentInfo.class);
agent.title = "Test_title";
agent.summary = "test summary";
agent.componentName = new ComponentName("pkg", "agent");

View File

@@ -0,0 +1,231 @@
/*
* Copyright (C) 2018 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.security.trustagent;
import static com.google.common.truth.Truth.assertThat;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.service.trust.TrustAgentService;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowApplicationPackageManager;
import java.util.ArrayList;
import java.util.List;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {
ShadowLockPatternUtils.class,
ShadowRestrictedLockUtils.class,
ShadowDevicePolicyManager.class,
ShadowApplicationPackageManager.class,
TrustAgentsPreferenceControllerTest.ShadowTrustAgentManager.class
})
public class TrustAgentsPreferenceControllerTest {
private static final Intent TEST_INTENT =
new Intent(TrustAgentService.SERVICE_INTERFACE);
private Context mContext;
private ShadowApplicationPackageManager mPackageManager;
private TrustAgentsPreferenceController mController;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mPackageManager = (ShadowApplicationPackageManager) Shadows.shadowOf(
mContext.getPackageManager());
mController = new TrustAgentsPreferenceController(mContext, "pref_key");
mPreferenceManager = new PreferenceManager(mContext);
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
mPreferenceScreen.setKey("pref_key");
}
@After
public void tearDown() {
ShadowTrustAgentManager.clearPermissionGrantedList();
}
@Test
public void getAvailabilityStatus_byDefault_shouldBeShown() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void onStart_noTrustAgent_shouldNotAddPreference() {
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
mController.displayPreference(mPreferenceScreen);
mController.onStart();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
}
@Test
public void
onStart_hasAUninstalledTrustAgent_shouldRemoveOnePreferenceAndLeaveTwoPreferences() {
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
final ResolveInfo uninstalledTrustAgent = availableAgents.get(0);
for (ResolveInfo rInfo : availableAgents) {
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
}
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
mController.displayPreference(mPreferenceScreen);
mController.onStart();
availableAgents.remove(uninstalledTrustAgent);
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
mController.onStart();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(2);
}
@Test
public void onStart_hasANewTrustAgent_shouldAddOnePreferenceAndHaveFourPreferences() {
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
final ComponentName newComponentName = new ComponentName("test.data.packageD", "clzDDD");
final ResolveInfo newTrustAgent = createFakeResolveInfo(newComponentName);
for (ResolveInfo rInfo : availableAgents) {
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
}
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
mController.displayPreference(mPreferenceScreen);
mController.onStart();
availableAgents.add(newTrustAgent);
ShadowTrustAgentManager.grantPermissionToResolveInfo(newTrustAgent);
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
mController.onStart();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(4);
}
@Test
public void onStart_hasUnrestrictedTrustAgent_shouldAddThreeChangeablePreferences() {
ShadowRestrictedLockUtils.setKeyguardDisabledFeatures(0);
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
for (ResolveInfo rInfo : availableAgents) {
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
}
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
mController.displayPreference(mPreferenceScreen);
mController.onStart();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(3);
for (int i = 0; i < mPreferenceScreen.getPreferenceCount(); i++) {
RestrictedSwitchPreference preference =
(RestrictedSwitchPreference) mPreferenceScreen.getPreference(i);
assertThat(preference.isDisabledByAdmin()).isFalse();
}
}
@Test
public void onStart_hasRestrictedTructAgent_shouldAddThreeUnchangeablePreferences() {
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
for (ResolveInfo rInfo : availableAgents) {
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
}
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
ShadowRestrictedLockUtils.setKeyguardDisabledFeatures(
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS);
mController.displayPreference(mPreferenceScreen);
mController.onStart();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(3);
for (int i = 0; i < mPreferenceScreen.getPreferenceCount(); i++) {
RestrictedSwitchPreference preference =
(RestrictedSwitchPreference) mPreferenceScreen.getPreference(i);
assertThat(preference.isDisabledByAdmin()).isTrue();
}
}
private List<ResolveInfo> createFakeAvailableAgents() {
final List<ComponentName> componentNames = new ArrayList<>();
componentNames.add(new ComponentName("test.data.packageA", "clzAAA"));
componentNames.add(new ComponentName("test.data.packageB", "clzBBB"));
componentNames.add(new ComponentName("test.data.packageC", "clzCCC"));
final List<ResolveInfo> result = new ArrayList<>();
for (ComponentName cn : componentNames) {
final ResolveInfo ri = createFakeResolveInfo(cn);
result.add(ri);
}
return result;
}
private ResolveInfo createFakeResolveInfo(ComponentName cn) {
final ResolveInfo ri = new ResolveInfo();
ri.serviceInfo = new ServiceInfo();
ri.serviceInfo.packageName = cn.getPackageName();
ri.serviceInfo.name = cn.getClassName();
ri.serviceInfo.applicationInfo = new ApplicationInfo();
ri.serviceInfo.applicationInfo.packageName = cn.getPackageName();
ri.serviceInfo.applicationInfo.name = cn.getClassName();
return ri;
}
@Implements(TrustAgentManager.class)
public static class ShadowTrustAgentManager {
private final static List<ResolveInfo> sPermissionGrantedList = new ArrayList<>();
@Implementation
public boolean shouldProvideTrust(ResolveInfo resolveInfo, PackageManager pm) {
if (sPermissionGrantedList.contains(resolveInfo)) {
return true;
}
return false;
}
public static void grantPermissionToResolveInfo(ResolveInfo rInfo) {
sPermissionGrantedList.add(rInfo);
}
public static void clearPermissionGrantedList() {
sPermissionGrantedList.clear();
}
}
}

View File

@@ -5,6 +5,7 @@ import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.os.PersistableBundle;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
@@ -12,6 +13,7 @@ import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -75,4 +77,10 @@ public class ShadowDevicePolicyManager extends org.robolectric.shadows.ShadowDev
return (ShadowDevicePolicyManager) Shadow.extract(
RuntimeEnvironment.application.getSystemService(DevicePolicyManager.class));
}
public @Nullable
List<PersistableBundle> getTrustAgentConfiguration(
@Nullable ComponentName admin, @NonNull ComponentName agent) {
return null;
}
}

View File

@@ -17,12 +17,15 @@
package com.android.settings.testutils.shadow;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import com.android.internal.widget.LockPatternUtils;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.List;
@Implements(LockPatternUtils.class)
public class ShadowLockPatternUtils {
@@ -49,6 +52,11 @@ public class ShadowLockPatternUtils {
return sDeviceEncryptionEnabled;
}
@Implementation
public List<ComponentName> getEnabledTrustAgents(int userId) {
return null;
}
public static void setDeviceEncryptionEnabled(boolean deviceEncryptionEnabled) {
sDeviceEncryptionEnabled = deviceEncryptionEnabled;
}