1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.security.trustagent; 18 19 import static com.android.settings.security.trustagent.TrustAgentListPreferenceController.PREF_KEY_SECURITY_CATEGORY; 20 import static com.android.settings.security.trustagent.TrustAgentListPreferenceController.PREF_KEY_TRUST_AGENT; 21 import static com.google.common.truth.Truth.assertThat; 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.Mockito.atLeastOnce; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.app.Activity; 30 import android.arch.lifecycle.LifecycleOwner; 31 import android.content.ComponentName; 32 import android.content.Context; 33 import android.support.v7.preference.Preference; 34 import android.support.v7.preference.PreferenceCategory; 35 import android.support.v7.preference.PreferenceScreen; 36 37 import com.android.internal.widget.LockPatternUtils; 38 import com.android.settings.core.PreferenceControllerMixin; 39 import com.android.settings.security.SecuritySettings; 40 import com.android.settings.testutils.FakeFeatureFactory; 41 import com.android.settings.testutils.SettingsRobolectricTestRunner; 42 import com.android.settingslib.core.lifecycle.Lifecycle; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.Robolectric; 50 import org.robolectric.annotation.Config; 51 52 import java.util.ArrayList; 53 import java.util.List; 54 55 @RunWith(SettingsRobolectricTestRunner.class) 56 public class TrustAgentListPreferenceControllerTest { 57 58 @Mock 59 private TrustAgentManager mTrustAgentManager; 60 @Mock 61 private LockPatternUtils mLockPatternUtils; 62 @Mock 63 private PreferenceScreen mScreen; 64 @Mock 65 private PreferenceCategory mCategory; 66 @Mock 67 private SecuritySettings mFragment; 68 69 private Lifecycle mLifecycle; 70 private LifecycleOwner mLifecycleOwner; 71 private FakeFeatureFactory mFeatureFactory; 72 private Activity mActivity; 73 74 private TrustAgentListPreferenceController mController; 75 76 @Before 77 public void setUp() { 78 MockitoAnnotations.initMocks(this); 79 mActivity = Robolectric.buildActivity(Activity.class).get(); 80 mLifecycleOwner = () -> mLifecycle; 81 mLifecycle = new Lifecycle(mLifecycleOwner); 82 mFeatureFactory = FakeFeatureFactory.setupForTest(); 83 when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(any(Context.class))) 84 .thenReturn(mLockPatternUtils); 85 when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager()) 86 .thenReturn(mTrustAgentManager); 87 when(mCategory.getKey()).thenReturn(PREF_KEY_SECURITY_CATEGORY); 88 when(mCategory.getContext()).thenReturn(mActivity); 89 when(mScreen.findPreference(PREF_KEY_SECURITY_CATEGORY)).thenReturn(mCategory); 90 mController = new TrustAgentListPreferenceController(mActivity, mFragment, mLifecycle); 91 } 92 93 @Test 94 public void testConstants() { 95 assertThat(mController.isAvailable()).isTrue(); 96 assertThat(mController.getPreferenceKey()).isEqualTo(PREF_KEY_TRUST_AGENT); 97 assertThat(mController).isInstanceOf(PreferenceControllerMixin.class); 98 } 99 100 @Test 101 @Config(qualifiers = "mcc999") 102 public void isAvailable_whenNotVisible_isFalse() { 103 assertThat(mController.isAvailable()).isFalse(); 104 } 105 106 @Test 107 public void onResume_shouldClearOldAgents() { 108 final Preference oldAgent = new Preference(mActivity); 109 oldAgent.setKey(PREF_KEY_TRUST_AGENT); 110 when(mCategory.findPreference(PREF_KEY_TRUST_AGENT)) 111 .thenReturn(oldAgent) 112 .thenReturn(null); 113 114 mController.displayPreference(mScreen); 115 mController.onResume(); 116 117 verify(mCategory).removePreference(oldAgent); 118 } 119 120 @Test 121 public void onResume_shouldAddNewAgents() { 122 final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>(); 123 final TrustAgentManager.TrustAgentComponentInfo agent = 124 mock(TrustAgentManager.TrustAgentComponentInfo.class); 125 agent.title = "Test_title"; 126 agent.summary = "test summary"; 127 agent.componentName = new ComponentName("pkg", "agent"); 128 agent.admin = null; 129 agents.add(agent); 130 when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils)) 131 .thenReturn(agents); 132 133 mController.displayPreference(mScreen); 134 mController.onResume(); 135 136 verify(mCategory, atLeastOnce()).addPreference(any(Preference.class)); 137 } 138 139 @Test 140 @Config(qualifiers = "mcc999") 141 public void onResume_ifNotAvailable_shouldNotAddNewAgents() { 142 final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>(); 143 final TrustAgentManager.TrustAgentComponentInfo agent = 144 mock(TrustAgentManager.TrustAgentComponentInfo.class); 145 agent.title = "Test_title"; 146 agent.summary = "test summary"; 147 agent.componentName = new ComponentName("pkg", "agent"); 148 agent.admin = null; 149 agents.add(agent); 150 when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils)) 151 .thenReturn(agents); 152 153 mController.displayPreference(mScreen); 154 mController.onResume(); 155 156 verify(mCategory, never()).addPreference(any(Preference.class)); 157 } 158 } 159