Fetch active trustagents from LockPatternUtils

LockPatternUtils is what drives the TrustAgentsPreferenceController, and
therefore should be used to determine how many active trust agents there
are. TrustAgentManager filters on whether preferences are displayable in
the advanced settings app, which does not represent if the agent is
actually active.

Test: make RunSettingsRoboTests
Bug: 217217034
Change-Id: I8cae74d322b8e0658aabfe7d4646e9a299af4934
This commit is contained in:
Derek Jedral
2022-01-31 11:11:32 -08:00
parent a2781721e2
commit c1c39b6717
2 changed files with 5 additions and 12 deletions

View File

@@ -32,14 +32,12 @@ public class ManageTrustAgentsPreferenceController extends BasePreferenceControl
private static final int MY_USER_ID = UserHandle.myUserId(); private static final int MY_USER_ID = UserHandle.myUserId();
private final LockPatternUtils mLockPatternUtils; private final LockPatternUtils mLockPatternUtils;
private TrustAgentManager mTrustAgentManager;
public ManageTrustAgentsPreferenceController(Context context, String key) { public ManageTrustAgentsPreferenceController(Context context, String key) {
super(context, key); super(context, key);
final SecurityFeatureProvider securityFeatureProvider = FeatureFactory.getFactory(context) final SecurityFeatureProvider securityFeatureProvider = FeatureFactory.getFactory(context)
.getSecurityFeatureProvider(); .getSecurityFeatureProvider();
mLockPatternUtils = securityFeatureProvider.getLockPatternUtils(context); mLockPatternUtils = securityFeatureProvider.getLockPatternUtils(context);
mTrustAgentManager = securityFeatureProvider.getTrustAgentManager();
} }
@Override @Override
@@ -66,6 +64,6 @@ public class ManageTrustAgentsPreferenceController extends BasePreferenceControl
} }
private int getTrustAgentCount() { private int getTrustAgentCount() {
return mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils).size(); return mLockPatternUtils.getEnabledTrustAgents(MY_USER_ID).size();
} }
} }

View File

@@ -21,13 +21,13 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import androidx.preference.Preference; import androidx.preference.Preference;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.security.trustagent.TrustAgentManager.TrustAgentComponentInfo;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before; import org.junit.Before;
@@ -45,8 +45,6 @@ import java.util.Collections;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ManageTrustAgentsPreferenceControllerTest { public class ManageTrustAgentsPreferenceControllerTest {
@Mock
private TrustAgentManager mTrustAgentManager;
@Mock @Mock
private LockPatternUtils mLockPatternUtils; private LockPatternUtils mLockPatternUtils;
@@ -62,8 +60,6 @@ public class ManageTrustAgentsPreferenceControllerTest {
mFeatureFactory = FakeFeatureFactory.setupForTest(); mFeatureFactory = FakeFeatureFactory.setupForTest();
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
.thenReturn(mLockPatternUtils); .thenReturn(mLockPatternUtils);
when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager())
.thenReturn(mTrustAgentManager);
mController = new ManageTrustAgentsPreferenceController(mContext, "key"); mController = new ManageTrustAgentsPreferenceController(mContext, "key");
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey()); mPreference.setKey(mController.getPreferenceKey());
@@ -94,8 +90,7 @@ public class ManageTrustAgentsPreferenceControllerTest {
@Test @Test
public void updateState_isSecure_noTrustAgent_shouldShowGenericSummary() { public void updateState_isSecure_noTrustAgent_shouldShowGenericSummary() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
when(mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils)) when(mLockPatternUtils.getEnabledTrustAgents(anyInt())).thenReturn(new ArrayList<>());
.thenReturn(new ArrayList<>());
mController.updateState(mPreference); mController.updateState(mPreference);
@@ -107,8 +102,8 @@ public class ManageTrustAgentsPreferenceControllerTest {
@Test @Test
public void updateState_isSecure_hasTrustAgent_shouldShowDetailedSummary() { public void updateState_isSecure_hasTrustAgent_shouldShowDetailedSummary() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
when(mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils)) when(mLockPatternUtils.getEnabledTrustAgents(anyInt())).thenReturn(
.thenReturn(Collections.singletonList(new TrustAgentComponentInfo())); Collections.singletonList(new ComponentName("packageName", "className")));
mController.updateState(mPreference); mController.updateState(mPreference);