Convert trust agent pref list logic to PreferenceController
Bug: 32953042 Test: robotests Change-Id: I1420e90d4910a11fc5837ef2e150c822635b74a5
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.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.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.widget.LockPatternUtils;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.security.SecuritySettingsV2;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class TrustAgentListPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private TrustAgentManager mTrustAgentManager;
|
||||
@Mock
|
||||
private LockPatternUtils mLockPatternUtils;
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private PreferenceCategory mCategory;
|
||||
@Mock
|
||||
private SecuritySettingsV2 mFragment;
|
||||
|
||||
private Lifecycle mLifecycle;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private Activity mActivity;
|
||||
|
||||
private TrustAgentListPreferenceController mController;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mActivity = Robolectric.buildActivity(Activity.class).get();
|
||||
mLifecycle = new Lifecycle(() -> mLifecycle);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(any(Context.class)))
|
||||
.thenReturn(mLockPatternUtils);
|
||||
when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager())
|
||||
.thenReturn(mTrustAgentManager);
|
||||
when(mCategory.getKey()).thenReturn(PREF_KEY_SECURITY_CATEGORY);
|
||||
when(mCategory.getContext()).thenReturn(mActivity);
|
||||
when(mScreen.findPreference(PREF_KEY_SECURITY_CATEGORY))
|
||||
.thenReturn(mCategory);
|
||||
mController = new TrustAgentListPreferenceController(mActivity, mFragment, mLifecycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstants() {
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
assertThat(mController.getPreferenceKey()).isEqualTo(PREF_KEY_TRUST_AGENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_shouldClearOldAgents() {
|
||||
final Preference oldAgent = new Preference(mActivity);
|
||||
oldAgent.setKey(PREF_KEY_TRUST_AGENT);
|
||||
when(mCategory.findPreference(PREF_KEY_TRUST_AGENT))
|
||||
.thenReturn(oldAgent)
|
||||
.thenReturn(null);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onResume();
|
||||
|
||||
verify(mCategory).removePreference(oldAgent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_shouldAddNewAgents() {
|
||||
final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
|
||||
final TrustAgentManager.TrustAgentComponentInfo agent = 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);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onResume();
|
||||
|
||||
verify(mCategory).addPreference(any(Preference.class));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user