diff --git a/res/xml/security_settings_v2.xml b/res/xml/security_settings_v2.xml index 334333ac570..155ee70d717 100644 --- a/res/xml/security_settings_v2.xml +++ b/res/xml/security_settings_v2.xml @@ -63,8 +63,7 @@ + android:title="@string/sim_lock_settings_category"> 0) { - manageAgents.setSummary(getActivity().getResources().getQuantityString( - R.plurals.manage_trust_agents_summary_on, - numberOfTrustAgent, numberOfTrustAgent)); - } else { - manageAgents.setSummary(R.string.manage_trust_agents_summary); - } - } - } - @VisibleForTesting void setLockscreenPreferencesSummary(PreferenceGroup group) { final Preference lockscreenPreferences = group.findPreference(KEY_LOCKSCREEN_PREFERENCES); @@ -411,8 +362,9 @@ public class SecuritySettingsV2 extends DashboardFragment * The preference must be a RestrictedPreference. */ private void disableIfPasswordQualityManaged(String preferenceKey, int userId) { - final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet( - getActivity(), userId); + final RestrictedLockUtils.EnforcedAdmin admin = + RestrictedLockUtils.checkIfPasswordQualityIsSet( + getActivity(), userId); if (admin != null && mDPM.getPasswordQuality(admin.component, userId) == DevicePolicyManager.PASSWORD_QUALITY_MANAGED) { final RestrictedPreference pref = @@ -433,8 +385,8 @@ public class SecuritySettingsV2 extends DashboardFragment // Return the number of trust agents being added private int addTrustAgentSettings(PreferenceGroup securityCategory) { final boolean hasSecurity = mLockPatternUtils.isSecure(MY_USER_ID); - final List agents = mTrustAgentManager.getActiveTrustAgents( - getActivity(), mLockPatternUtils); + final List agents = + mTrustAgentManager.getActiveTrustAgents(getActivity(), mLockPatternUtils); for (TrustAgentManager.TrustAgentComponentInfo agent : agents) { final RestrictedPreference trustAgentPreference = new RestrictedPreference(securityCategory.getContext()); @@ -458,43 +410,6 @@ public class SecuritySettingsV2 extends DashboardFragment return agents.size(); } - /* Return true if a there is a Slot that has Icc. - */ - private boolean isSimIccReady() { - TelephonyManager tm = TelephonyManager.getDefault(); - final List subInfoList = - mSubscriptionManager.getActiveSubscriptionInfoList(); - - if (subInfoList != null) { - for (SubscriptionInfo subInfo : subInfoList) { - if (tm.hasIccCard(subInfo.getSimSlotIndex())) { - return true; - } - } - } - - return false; - } - - /* Return true if a SIM is ready for locking. - * TODO: consider adding to TelephonyManager or SubscritpionManasger. - */ - private boolean isSimReady() { - final List subInfoList = - mSubscriptionManager.getActiveSubscriptionInfoList(); - if (subInfoList != null) { - for (SubscriptionInfo subInfo : subInfoList) { - final int simState = TelephonyManager.getDefault() - .getSimState(subInfo.getSimSlotIndex()); - if ((simState != TelephonyManager.SIM_STATE_ABSENT) && - (simState != TelephonyManager.SIM_STATE_UNKNOWN)) { - return true; - } - } - } - return false; - } - @Override public void onGearClick(GearPreference p) { if (KEY_UNLOCK_SET_OR_CHANGE.equals(p.getKey())) { @@ -524,12 +439,10 @@ public class SecuritySettingsV2 extends DashboardFragment } updateUnificationPreference(); - - if (mShowPassword != null) { - mShowPassword.setChecked(Settings.System.getInt(getContentResolver(), - Settings.System.TEXT_SHOW_PASSWORD, 1) != 0); - } - + final Preference showPasswordPref = getPreferenceScreen().findPreference( + mShowPasswordPreferenceController.getPreferenceKey()); + showPasswordPref.setOnPreferenceChangeListener(mShowPasswordPreferenceController); + mShowPasswordPreferenceController.updateState(showPasswordPref); mLocationController.updateSummary(); } @@ -581,7 +494,7 @@ public class SecuritySettingsV2 extends DashboardFragment mTrustAgentClickIntent = preference.getIntent(); boolean confirmationLaunched = helper.launchConfirmationActivity( CHANGE_TRUST_AGENT_SETTINGS, preference.getTitle()); - if (!confirmationLaunched&& mTrustAgentClickIntent != null) { + if (!confirmationLaunched && mTrustAgentClickIntent != null) { // If this returns false, it means no password confirmation is required. startActivity(mTrustAgentClickIntent); mTrustAgentClickIntent = null; @@ -715,15 +628,11 @@ public class SecuritySettingsV2 extends DashboardFragment R.string.unlock_set_unlock_launch_picker_title); final ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(getActivity(), this); - if(!helper.launchConfirmationActivity( + if (!helper.launchConfirmationActivity( UNUNIFY_LOCK_CONFIRM_DEVICE_REQUEST, title, true, MY_USER_ID)) { ununifyLocks(); } } - } else if (KEY_SHOW_PASSWORD.equals(key)) { - Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD, - ((Boolean) value) ? 1 : 0); - lockPatternUtils.setVisiblePasswordEnabled((Boolean) value, MY_USER_ID); } return result; } @@ -788,8 +697,9 @@ public class SecuritySettingsV2 extends DashboardFragment } private boolean isPasswordManaged(int userId, Context context, DevicePolicyManager dpm) { - final RestrictedLockUtils.EnforcedAdmin admin = RestrictedLockUtils.checkIfPasswordQualityIsSet( - context, userId); + final RestrictedLockUtils.EnforcedAdmin admin = + RestrictedLockUtils.checkIfPasswordQualityIsSet( + context, userId); return admin != null && dpm.getPasswordQuality(admin.component, userId) == DevicePolicyManager.PASSWORD_QUALITY_MANAGED; } @@ -847,20 +757,13 @@ public class SecuritySettingsV2 extends DashboardFragment @Override public List getNonIndexableKeys(Context context) { final List keys = super.getNonIndexableKeys(context); + final LockPatternUtils lockPatternUtils = new LockPatternUtils(context); - LockPatternUtils lockPatternUtils = new LockPatternUtils(context); - - // Do not display SIM lock for devices without an Icc card - final UserManager um = UserManager.get(context); - final TelephonyManager tm = TelephonyManager.from(context); - if (!um.isAdminUser() || !tm.hasIccCard()) { - keys.add(KEY_SIM_LOCK); - } + new SimLockPreferenceController(context).updateNonIndexableKeys(keys); // TrustAgent settings disappear when the user has no primary security. if (!lockPatternUtils.isSecure(MY_USER_ID)) { keys.add(KEY_TRUST_AGENT); - keys.add(KEY_MANAGE_TRUST_AGENTS); } if (!(new EnterprisePrivacyPreferenceController(context)) @@ -873,7 +776,6 @@ public class SecuritySettingsV2 extends DashboardFragment // Duplicates between parent-child keys.add(KEY_LOCATION); keys.add(KEY_ENCRYPTION_AND_CREDENTIALS); - keys.add(KEY_SCREEN_PINNING); keys.add(KEY_LOCATION_SCANNING); return keys; diff --git a/src/com/android/settings/security/ShowPasswordPreferenceController.java b/src/com/android/settings/security/ShowPasswordPreferenceController.java new file mode 100644 index 00000000000..9f9b52db71b --- /dev/null +++ b/src/com/android/settings/security/ShowPasswordPreferenceController.java @@ -0,0 +1,58 @@ +/* + * 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; + +import android.content.Context; +import android.os.UserHandle; +import android.provider.Settings; + +import com.android.internal.widget.LockPatternUtils; +import com.android.settings.core.TogglePreferenceController; +import com.android.settings.overlay.FeatureFactory; + +public class ShowPasswordPreferenceController extends TogglePreferenceController { + + private static final String KEY_SHOW_PASSWORD = "show_password"; + private static final int MY_USER_ID = UserHandle.myUserId(); + private final LockPatternUtils mLockPatternUtils; + + public ShowPasswordPreferenceController(Context context) { + super(context, KEY_SHOW_PASSWORD); + mLockPatternUtils = FeatureFactory.getFactory(context) + .getSecurityFeatureProvider() + .getLockPatternUtils(context); + } + + @Override + public boolean isChecked() { + return Settings.System.getInt(mContext.getContentResolver(), + Settings.System.TEXT_SHOW_PASSWORD, 1) != 0; + } + + @Override + public void setChecked(boolean isChecked) { + Settings.System.putInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD, + isChecked ? 1 : 0); + mLockPatternUtils.setVisiblePasswordEnabled(isChecked, MY_USER_ID); + } + + @Override + public int getAvailabilityStatus() { + return AVAILABLE; + } +} + diff --git a/src/com/android/settings/security/SimLockPreferenceController.java b/src/com/android/settings/security/SimLockPreferenceController.java new file mode 100644 index 00000000000..797f09840e6 --- /dev/null +++ b/src/com/android/settings/security/SimLockPreferenceController.java @@ -0,0 +1,108 @@ +/* + * 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; + +import android.content.Context; +import android.os.PersistableBundle; +import android.os.UserManager; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import com.android.settings.core.BasePreferenceController; + +import java.util.List; + +public class SimLockPreferenceController extends BasePreferenceController { + + private static final String KEY_SIM_LOCK = "sim_lock_settings"; + + private final CarrierConfigManager mCarrierConfigManager; + private final UserManager mUserManager; + private final SubscriptionManager mSubscriptionManager; + private final TelephonyManager mTelephonyManager; + + public SimLockPreferenceController(Context context) { + super(context, KEY_SIM_LOCK); + mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); + mCarrierConfigManager = (CarrierConfigManager) + mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); + mSubscriptionManager = (SubscriptionManager) context + .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); + mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + } + + @Override + public int getAvailabilityStatus() { + final PersistableBundle b = mCarrierConfigManager.getConfig(); + final boolean IsAdmin = mUserManager.isAdminUser(); + if (!IsAdmin || !isSimIccReady() || + b.getBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL)) { + return DISABLED_FOR_USER; + } + return AVAILABLE; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + final Preference preference = screen.findPreference(getPreferenceKey()); + if (preference == null) { + return; + } + // Disable SIM lock if there is no ready SIM card. + preference.setEnabled(isSimReady()); + } + + /* Return true if a SIM is ready for locking. + * TODO: consider adding to TelephonyManager or SubscritpionManasger. + */ + private boolean isSimReady() { + final List subInfoList = + mSubscriptionManager.getActiveSubscriptionInfoList(); + if (subInfoList != null) { + for (SubscriptionInfo subInfo : subInfoList) { + final int simState = mTelephonyManager.getSimState(subInfo.getSimSlotIndex()); + if ((simState != TelephonyManager.SIM_STATE_ABSENT) && + (simState != TelephonyManager.SIM_STATE_UNKNOWN)) { + return true; + } + } + } + return false; + } + + /** + * Return true if a there is a Slot that has Icc + */ + private boolean isSimIccReady() { + final List subInfoList = + mSubscriptionManager.getActiveSubscriptionInfoList(); + + if (subInfoList != null) { + for (SubscriptionInfo subInfo : subInfoList) { + if (mTelephonyManager.hasIccCard(subInfo.getSimSlotIndex())) { + return true; + } + } + } + return false; + } +} diff --git a/src/com/android/settings/security/trustagent/ManageTrustAgentsPreferenceController.java b/src/com/android/settings/security/trustagent/ManageTrustAgentsPreferenceController.java new file mode 100644 index 00000000000..c615effe35d --- /dev/null +++ b/src/com/android/settings/security/trustagent/ManageTrustAgentsPreferenceController.java @@ -0,0 +1,76 @@ +/* + * 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 android.content.Context; +import android.os.UserHandle; +import android.support.annotation.VisibleForTesting; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.internal.widget.LockPatternUtils; +import com.android.settings.R; +import com.android.settings.core.BasePreferenceController; +import com.android.settings.overlay.FeatureFactory; +import com.android.settings.security.SecurityFeatureProvider; + +public class ManageTrustAgentsPreferenceController extends BasePreferenceController { + + @VisibleForTesting + static final String KEY_MANAGE_TRUST_AGENTS = "manage_trust_agents"; + private static final int MY_USER_ID = UserHandle.myUserId(); + + private final LockPatternUtils mLockPatternUtils; + private TrustAgentManager mTrustAgentManager; + + public ManageTrustAgentsPreferenceController(Context context) { + super(context, KEY_MANAGE_TRUST_AGENTS); + final SecurityFeatureProvider securityFeatureProvider = FeatureFactory.getFactory(context) + .getSecurityFeatureProvider(); + mLockPatternUtils = securityFeatureProvider.getLockPatternUtils(context); + mTrustAgentManager = securityFeatureProvider.getTrustAgentManager(); + } + + @Override + public int getAvailabilityStatus() { + return AVAILABLE; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + final Preference preference = screen.findPreference(getPreferenceKey()); + if (preference == null) { + return; + } + final int numberOfTrustAgent = getTrustAgentCount(); + if (!mLockPatternUtils.isSecure(MY_USER_ID)) { + preference.setEnabled(false); + preference.setSummary(R.string.disabled_because_no_backup_security); + } else if (numberOfTrustAgent > 0) { + preference.setSummary(mContext.getResources().getQuantityString( + R.plurals.manage_trust_agents_summary_on, + numberOfTrustAgent, numberOfTrustAgent)); + } else { + preference.setSummary(R.string.manage_trust_agents_summary); + } + } + + private int getTrustAgentCount() { + return mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils).size(); + } +} diff --git a/tests/robotests/src/com/android/settings/security/ScreenPinningPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/security/ScreenPinningPreferenceControllerTest.java new file mode 100644 index 00000000000..797e7d0b5bb --- /dev/null +++ b/tests/robotests/src/com/android/settings/security/ScreenPinningPreferenceControllerTest.java @@ -0,0 +1,99 @@ +/* + * 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; + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.provider.Settings; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.R; +import com.android.settings.TestConfig; +import com.android.settings.testutils.FakeFeatureFactory; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class ScreenPinningPreferenceControllerTest { + + @Mock + private PreferenceScreen mScreen; + + private FakeFeatureFactory mFeatureFactory; + private Context mContext; + private ScreenPinningPreferenceController mController; + private Preference mPreference; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mFeatureFactory = FakeFeatureFactory.setupForTest(); + mController = new ScreenPinningPreferenceController(mContext); + mPreference = new Preference(mContext); + mPreference.setKey(mController.getPreferenceKey()); + when(mScreen.findPreference(mController.getPreferenceKey())) + .thenReturn(mPreference); + } + + @After + public void tearDown() { + Settings.System.putInt(mContext.getContentResolver(), + Settings.System.LOCK_TO_APP_ENABLED, 0); + } + + @Test + public void isAlwaysAvailable() { + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + @Test + public void displayPreference_isOff_shouldDisableOffSummary() { + Settings.System.putInt(mContext.getContentResolver(), + Settings.System.LOCK_TO_APP_ENABLED, 0); + + mController.displayPreference(mScreen); + + assertThat(mPreference.getSummary()) + .isEqualTo(mContext.getString(R.string.switch_off_text)); + } + + @Test + public void displayPreference_isOn_shouldDisableOnSummary() { + Settings.System.putInt(mContext.getContentResolver(), + Settings.System.LOCK_TO_APP_ENABLED, 1); + + mController.displayPreference(mScreen); + + assertThat(mPreference.getSummary()) + .isEqualTo(mContext.getString(R.string.switch_on_text)); + } + +} diff --git a/tests/robotests/src/com/android/settings/security/ShowPasswordPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/security/ShowPasswordPreferenceControllerTest.java new file mode 100644 index 00000000000..63cf00b9d43 --- /dev/null +++ b/tests/robotests/src/com/android/settings/security/ShowPasswordPreferenceControllerTest.java @@ -0,0 +1,107 @@ +/* + * 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; + + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.UserHandle; +import android.provider.Settings; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.internal.widget.LockPatternUtils; +import com.android.settings.TestConfig; +import com.android.settings.testutils.FakeFeatureFactory; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class ShowPasswordPreferenceControllerTest { + + @Mock + private LockPatternUtils mLockPatternUtils; + @Mock + private PreferenceScreen mScreen; + + private FakeFeatureFactory mFeatureFactory; + private Context mContext; + private ShowPasswordPreferenceController mController; + private Preference mPreference; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mFeatureFactory = FakeFeatureFactory.setupForTest(); + when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) + .thenReturn(mLockPatternUtils); + mController = new ShowPasswordPreferenceController(mContext); + mPreference = new Preference(mContext); + mPreference.setKey(mController.getPreferenceKey()); + when(mScreen.findPreference(mController.getPreferenceKey())) + .thenReturn(mPreference); + } + + @Test + public void isAlwaysAvailable() { + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + @Test + public void isChecked_settingIsOff_false() { + Settings.System.putInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD, + 0); + + assertThat(mController.isChecked()).isFalse(); + } + + @Test + public void isChecked_settingIsOn_true() { + Settings.System.putInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD, + 1); + assertThat(mController.isChecked()).isTrue(); + } + + @Test + public void changePref_turnOn_shouldChangeSettingTo1() { + mController.onPreferenceChange(mPreference, true); + + assertThat(mController.isChecked()).isTrue(); + verify(mLockPatternUtils).setVisiblePasswordEnabled(true, UserHandle.myUserId()); + } + + @Test + public void changePref_turnOff_shouldChangeSettingTo0() { + mController.onPreferenceChange(mPreference, false); + + assertThat(mController.isChecked()).isFalse(); + verify(mLockPatternUtils).setVisiblePasswordEnabled(false, UserHandle.myUserId()); + } +} diff --git a/tests/robotests/src/com/android/settings/security/SimLockPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/security/SimLockPreferenceControllerTest.java new file mode 100644 index 00000000000..a3a4fe36442 --- /dev/null +++ b/tests/robotests/src/com/android/settings/security/SimLockPreferenceControllerTest.java @@ -0,0 +1,163 @@ +/* + * 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; + +import static android.telephony.TelephonyManager.SIM_STATE_READY; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.PersistableBundle; +import android.os.UserManager; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import com.android.settings.TestConfig; +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowApplication; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class SimLockPreferenceControllerTest { + + @Mock + private SubscriptionManager mSubscriptionManager; + @Mock + private CarrierConfigManager mCarrierManager; + @Mock + private UserManager mUserManager; + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private SimLockPreferenceController mController; + private Preference mPreference; + + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + ShadowApplication shadowApplication = ShadowApplication.getInstance(); + shadowApplication.setSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, + mSubscriptionManager); + shadowApplication.setSystemService(Context.CARRIER_CONFIG_SERVICE, mCarrierManager); + shadowApplication.setSystemService(Context.USER_SERVICE, mUserManager); + shadowApplication.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager); + mController = new SimLockPreferenceController(mContext); + mPreference = new Preference(mContext); + mPreference.setKey(mController.getPreferenceKey()); + when(mScreen.findPreference(mController.getPreferenceKey())) + .thenReturn(mPreference); + } + + @Test + public void isAvailable_notAdmin_false() { + when(mUserManager.isAdminUser()).thenReturn(false); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); + } + + @Test + public void isAvailable_simIccNotReady_false() { + when(mUserManager.isAdminUser()).thenReturn(true); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); + } + + @Test + public void isAvailable_carrierConfigDisabled_false() { + when(mUserManager.isAdminUser()).thenReturn(true); + setupMockIcc(); + final PersistableBundle pb = new PersistableBundle(); + pb.putBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL, true); + when(mCarrierManager.getConfig()).thenReturn(pb); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); + } + + @Test + public void isAvailable_true() { + when(mUserManager.isAdminUser()).thenReturn(true); + setupMockIcc(); + final PersistableBundle pb = new PersistableBundle(); + when(mCarrierManager.getConfig()).thenReturn(pb); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void displayPreference_simReady_enablePreference() { + mController.displayPreference(mScreen); + + assertThat(mPreference.isEnabled()).isFalse(); + } + + @Test + public void displayPreference_simNotReady_disablePreference() { + setupMockSimReady(); + + mController.displayPreference(mScreen); + + assertThat(mPreference.isEnabled()).isTrue(); + } + + private void setupMockIcc() { + final List subscriptionInfoList = new ArrayList<>(); + SubscriptionInfo info = mock(SubscriptionInfo.class); + subscriptionInfoList.add(info); + when(mTelephonyManager.hasIccCard(anyInt())) + .thenReturn(true); + when(mSubscriptionManager.getActiveSubscriptionInfoList()) + .thenReturn(subscriptionInfoList); + } + + private void setupMockSimReady() { + final List subscriptionInfoList = new ArrayList<>(); + SubscriptionInfo info = mock(SubscriptionInfo.class); + subscriptionInfoList.add(info); + when(mTelephonyManager.getSimState(anyInt())) + .thenReturn(SIM_STATE_READY); + when(mSubscriptionManager.getActiveSubscriptionInfoList()) + .thenReturn(subscriptionInfoList); + } + +} diff --git a/tests/robotests/src/com/android/settings/security/trustagent/ManageTrustAgentsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/security/trustagent/ManageTrustAgentsPreferenceControllerTest.java new file mode 100644 index 00000000000..88186026b93 --- /dev/null +++ b/tests/robotests/src/com/android/settings/security/trustagent/ManageTrustAgentsPreferenceControllerTest.java @@ -0,0 +1,119 @@ +/* + * 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.core.BasePreferenceController.AVAILABLE; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.internal.widget.LockPatternUtils; +import com.android.settings.R; +import com.android.settings.TestConfig; +import com.android.settings.testutils.FakeFeatureFactory; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.ArrayList; +import java.util.Arrays; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class ManageTrustAgentsPreferenceControllerTest { + + @Mock + private TrustAgentManager mTrustAgentManager; + @Mock + private LockPatternUtils mLockPatternUtils; + @Mock + private PreferenceScreen mScreen; + + private FakeFeatureFactory mFeatureFactory; + private Context mContext; + private ManageTrustAgentsPreferenceController mController; + private Preference mPreference; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mFeatureFactory = FakeFeatureFactory.setupForTest(); + when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) + .thenReturn(mLockPatternUtils); + when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager()) + .thenReturn(mTrustAgentManager); + mController = new ManageTrustAgentsPreferenceController(mContext); + mPreference = new Preference(mContext); + mPreference.setKey(mController.getPreferenceKey()); + when(mScreen.findPreference(mController.getPreferenceKey())) + .thenReturn(mPreference); + } + + @Test + public void isAlwaysAvailable() { + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + @Test + public void displayPreference_isNotSecure_shouldDisablePreference() { + when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); + + mController.displayPreference(mScreen); + + assertThat(mPreference.isEnabled()).isFalse(); + assertThat(mPreference.getSummary()) + .isEqualTo(mContext.getString(R.string.disabled_because_no_backup_security)); + } + + @Test + public void displayPreference_isSecure_noTrustAgent_shouldShowGenericSummary() { + when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); + when(mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils)) + .thenReturn(new ArrayList<>()); + + mController.displayPreference(mScreen); + + assertThat(mPreference.isEnabled()).isTrue(); + assertThat(mPreference.getSummary()) + .isEqualTo(mContext.getString(R.string.manage_trust_agents_summary)); + } + + @Test + public void displayPreference_isSecure_hasTrustAgent_shouldShowDetailedSummary() { + when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); + when(mTrustAgentManager.getActiveTrustAgents(mContext, mLockPatternUtils)) + .thenReturn(Arrays.asList(new TrustAgentManager.TrustAgentComponentInfo())); + + mController.displayPreference(mScreen); + + assertThat(mPreference.isEnabled()).isTrue(); + assertThat(mPreference.getSummary()) + .isEqualTo(mContext.getResources().getQuantityString( + R.plurals.manage_trust_agents_summary_on, 1, 1)); + } +}