diff --git a/res/values/strings.xml b/res/values/strings.xml index dfcbc96d693..38ba6c5780b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -8166,6 +8166,16 @@ Admin can lock device and reset password Admin can delete all device data + + + Admin set maximum password attempts to %d before deleting all device data + Admin set maximum password attempts to %d before deleting all device data + + + + Admin set maximum password attempts to %d before deleting work profile data + Admin set maximum password attempts to %d before deleting work profile data + This device is managed. diff --git a/res/xml/enterprise_privacy_settings.xml b/res/xml/enterprise_privacy_settings.xml index cc63d5fcdd2..bfb12478f7f 100644 --- a/res/xml/enterprise_privacy_settings.xml +++ b/res/xml/enterprise_privacy_settings.xml @@ -106,5 +106,13 @@ android:title="@string/enterprise_privacy_wipe_device" settings:allowDividerBelow="true" settings:multiLine="true"/> + + diff --git a/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java b/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java index ef03cfb3292..6b725737411 100644 --- a/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java +++ b/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java @@ -26,12 +26,10 @@ import java.util.Date; public abstract class AdminActionPreferenceControllerBase extends PreferenceController { - private final Context mContext; protected final EnterprisePrivacyFeatureProvider mFeatureProvider; public AdminActionPreferenceControllerBase(Context context) { super(context); - mContext = context; mFeatureProvider = FeatureFactory.getFactory(context) .getEnterprisePrivacyFeatureProvider(context); } diff --git a/src/com/android/settings/enterprise/DevicePolicyManagerWrapper.java b/src/com/android/settings/enterprise/DevicePolicyManagerWrapper.java index 5be78849280..014092f71a1 100644 --- a/src/com/android/settings/enterprise/DevicePolicyManagerWrapper.java +++ b/src/com/android/settings/enterprise/DevicePolicyManagerWrapper.java @@ -26,6 +26,13 @@ import android.support.annotation.Nullable; * newer than the API version supported by Robolectric. */ public interface DevicePolicyManagerWrapper { + /** + * Calls {@code DevicePolicyManager.getMaximumFailedPasswordsForWipe()}. + * + * @see android.app.admin.DevicePolicyManager#getMaximumFailedPasswordsForWipe + */ + int getMaximumFailedPasswordsForWipe(@Nullable ComponentName admin, int userHandle); + /** * Calls {@code DevicePolicyManager.getDeviceOwnerComponentOnAnyUser()}. * @@ -33,12 +40,26 @@ public interface DevicePolicyManagerWrapper { */ ComponentName getDeviceOwnerComponentOnAnyUser(); + /** + * Calls {@code DevicePolicyManager.getDeviceOwnerUserId()}. + * + * @see android.app.admin.DevicePolicyManager#getDeviceOwnerUserId + */ + int getDeviceOwnerUserId(); + + /** + * Calls {@code DevicePolicyManager.getProfileOwnerAsUser()}. + * + * @see android.app.admin.DevicePolicyManager#getProfileOwnerAsUser + */ + @Nullable ComponentName getProfileOwnerAsUser(final int userId); + /** * Calls {@code DevicePolicyManager.getDeviceOwnerNameOnAnyUser()}. * * @see android.app.admin.DevicePolicyManager#getDeviceOwnerNameOnAnyUser */ - public CharSequence getDeviceOwnerOrganizationName(); + CharSequence getDeviceOwnerOrganizationName(); /** * Calls {@code DevicePolicyManager.getPermissionGrantState()}. @@ -53,19 +74,19 @@ public interface DevicePolicyManagerWrapper { * * @see android.app.admin.DevicePolicyManager#getLastSecurityLogRetrievalTime */ - public long getLastSecurityLogRetrievalTime(); + long getLastSecurityLogRetrievalTime(); /** * Calls {@code DevicePolicyManager.getLastBugReportRequestTime()}. * * @see android.app.admin.DevicePolicyManager#getLastBugReportRequestTime */ - public long getLastBugReportRequestTime(); + long getLastBugReportRequestTime(); /** * Calls {@code DevicePolicyManager.getLastNetworkLogRetrievalTime()}. * * @see android.app.admin.DevicePolicyManager#getLastNetworkLogRetrievalTime */ - public long getLastNetworkLogRetrievalTime(); + long getLastNetworkLogRetrievalTime(); } diff --git a/src/com/android/settings/enterprise/DevicePolicyManagerWrapperImpl.java b/src/com/android/settings/enterprise/DevicePolicyManagerWrapperImpl.java index 6e162a8bf59..210faec5bd6 100644 --- a/src/com/android/settings/enterprise/DevicePolicyManagerWrapperImpl.java +++ b/src/com/android/settings/enterprise/DevicePolicyManagerWrapperImpl.java @@ -27,11 +27,26 @@ public class DevicePolicyManagerWrapperImpl implements DevicePolicyManagerWrappe mDpm = dpm; } + @Override + public int getMaximumFailedPasswordsForWipe(@Nullable ComponentName admin, int userHandle) { + return mDpm.getMaximumFailedPasswordsForWipe(admin, userHandle); + } + @Override public ComponentName getDeviceOwnerComponentOnAnyUser() { return mDpm.getDeviceOwnerComponentOnAnyUser(); } + @Override + public int getDeviceOwnerUserId() { + return mDpm.getDeviceOwnerUserId(); + } + + @Override + public @Nullable ComponentName getProfileOwnerAsUser(final int userId) { + return mDpm.getProfileOwnerAsUser(userId); + } + @Override public CharSequence getDeviceOwnerOrganizationName() { return mDpm.getDeviceOwnerOrganizationName(); diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java index 91ad119a7fc..79b12e633e1 100644 --- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java +++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java @@ -74,4 +74,16 @@ public interface EnterprisePrivacyFeatureProvider { * Returns whether the Device Owner set a recommended global HTTP proxy. */ boolean isGlobalHttpProxySet(); + + /** + * Returns the number of failed login attempts that the Device Owner allows before the entire + * device is wiped, or zero if no such limit is set. + */ + int getMaximumFailedPasswordsBeforeWipeInPrimaryUser(); + + /** + * Returns the number of failed login attempts that the Profile Owner allows before the current + * user's managed profile (if any) is wiped, or zero if no such limit is set. + */ + int getMaximumFailedPasswordsBeforeWipeInManagedProfile(); } diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java index bb5412b8cbc..645a1f50fed 100644 --- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java +++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java @@ -16,6 +16,7 @@ package com.android.settings.enterprise; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -70,12 +71,12 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe return userInfo.id; } } - return -1; + return UserHandle.USER_NULL; } @Override public boolean isInCompMode() { - return hasDeviceOwner() && getManagedProfileUserId() != -1; + return hasDeviceOwner() && getManagedProfileUserId() != UserHandle.USER_NULL; } @Override @@ -124,7 +125,7 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe @Override public boolean isAlwaysOnVpnSetInManagedProfile() { final int managedProfileUserId = getManagedProfileUserId(); - return managedProfileUserId != -1 && + return managedProfileUserId != UserHandle.USER_NULL && VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId); } @@ -133,6 +134,28 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe return mCm.getGlobalProxy() != null; } + @Override + public int getMaximumFailedPasswordsBeforeWipeInPrimaryUser() { + final ComponentName deviceOwner = mDpm.getDeviceOwnerComponentOnAnyUser(); + if (deviceOwner == null) { + return 0; + } + return mDpm.getMaximumFailedPasswordsForWipe(deviceOwner, mDpm.getDeviceOwnerUserId()); + } + + @Override + public int getMaximumFailedPasswordsBeforeWipeInManagedProfile() { + final int userId = getManagedProfileUserId(); + if (userId == UserHandle.USER_NULL) { + return 0; + } + final ComponentName profileOwner = mDpm.getProfileOwnerAsUser(userId); + if (profileOwner == null) { + return 0; + } + return mDpm.getMaximumFailedPasswordsForWipe(profileOwner, userId); + } + protected static class EnterprisePrivacySpan extends ClickableSpan { private final Context mContext; diff --git a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java index 75d3b1070ba..821b7ff9520 100644 --- a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java +++ b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java @@ -63,6 +63,8 @@ public class EnterprisePrivacySettings extends DashboardFragment { controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context)); controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context)); controllers.add(new GlobalHttpProxyPreferenceController(context)); + controllers.add(new FailedPasswordWipePrimaryUserPreferenceController(context)); + controllers.add(new FailedPasswordWipeManagedProfilePreferenceController(context)); return controllers; } diff --git a/src/com/android/settings/enterprise/FailedPasswordWipeManagedProfilePreferenceController.java b/src/com/android/settings/enterprise/FailedPasswordWipeManagedProfilePreferenceController.java new file mode 100644 index 00000000000..2eac0a947a3 --- /dev/null +++ b/src/com/android/settings/enterprise/FailedPasswordWipeManagedProfilePreferenceController.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; + +import com.android.settings.R; + +public class FailedPasswordWipeManagedProfilePreferenceController + extends FailedPasswordWipePreferenceControllerBase { + + private static final String KEY_FAILED_PASSWORD_WIPE_MANAGED_PROFILE + = "failed_password_wipe_managed_profile"; + + public FailedPasswordWipeManagedProfilePreferenceController(Context context) { + super(context, R.plurals.enterprise_privacy_failed_password_wipe_work); + } + + @Override + protected int getMaximumFailedPasswordsBeforeWipe() { + return mFeatureProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile(); + } + + @Override + public String getPreferenceKey() { + return KEY_FAILED_PASSWORD_WIPE_MANAGED_PROFILE; + } +} diff --git a/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerBase.java b/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerBase.java new file mode 100644 index 00000000000..7cdd8dafe9d --- /dev/null +++ b/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerBase.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; +import android.content.res.Resources; +import android.support.v7.preference.Preference; + +import com.android.settings.core.PreferenceController; +import com.android.settings.overlay.FeatureFactory; + +public abstract class FailedPasswordWipePreferenceControllerBase extends PreferenceController { + + private final int mStringResourceId; + protected final EnterprisePrivacyFeatureProvider mFeatureProvider; + + public FailedPasswordWipePreferenceControllerBase(Context context, int stringResourceId) { + super(context); + mStringResourceId = stringResourceId; + mFeatureProvider = FeatureFactory.getFactory(context) + .getEnterprisePrivacyFeatureProvider(context); + } + + protected abstract int getMaximumFailedPasswordsBeforeWipe(); + + @Override + public void updateState(Preference preference) { + final int failedPasswordsBeforeWipe = getMaximumFailedPasswordsBeforeWipe(); + if (failedPasswordsBeforeWipe == 0) { + preference.setVisible(false); + } else { + preference.setVisible(true); + preference.setTitle(mContext.getResources().getQuantityString( + mStringResourceId, failedPasswordsBeforeWipe, failedPasswordsBeforeWipe)); + } + } + + @Override + public boolean isAvailable() { + return true; + } +} diff --git a/src/com/android/settings/enterprise/FailedPasswordWipePrimaryUserPreferenceController.java b/src/com/android/settings/enterprise/FailedPasswordWipePrimaryUserPreferenceController.java new file mode 100644 index 00000000000..91bdf9bd9e5 --- /dev/null +++ b/src/com/android/settings/enterprise/FailedPasswordWipePrimaryUserPreferenceController.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; + +import com.android.settings.R; + +public class FailedPasswordWipePrimaryUserPreferenceController + extends FailedPasswordWipePreferenceControllerBase { + + private static final String KEY_FAILED_PASSWORD_WIPE_PRIMARY_USER + = "failed_password_wipe_primary_user"; + + public FailedPasswordWipePrimaryUserPreferenceController(Context context) { + super(context, R.plurals.enterprise_privacy_failed_password_wipe_device); + } + + @Override + protected int getMaximumFailedPasswordsBeforeWipe() { + return mFeatureProvider.getMaximumFailedPasswordsBeforeWipeInPrimaryUser(); + } + + @Override + public String getPreferenceKey() { + return KEY_FAILED_PASSWORD_WIPE_PRIMARY_USER; + } +} diff --git a/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerBaseTest.java b/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerBaseTest.java index 66233f52f15..64422426cf7 100644 --- a/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerBaseTest.java +++ b/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerBaseTest.java @@ -16,6 +16,8 @@ package com.android.settings.enterprise; +import android.content.Context; + import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -53,7 +55,7 @@ public final class AdminActionPreferenceControllerBaseTest extends private class AdminActionPreferenceControllerBaseTestable extends AdminActionPreferenceControllerBase { AdminActionPreferenceControllerBaseTestable() { - super(mContext); + super(AdminActionPreferenceControllerBaseTest.this.mContext); } @Override diff --git a/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerTestBase.java b/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerTestBase.java index c8cb69c05ce..59043eda6e5 100644 --- a/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerTestBase.java +++ b/tests/robotests/src/com/android/settings/enterprise/AdminActionPreferenceControllerTestBase.java @@ -54,7 +54,7 @@ public abstract class AdminActionPreferenceControllerTestBase { mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); } - protected abstract void setDate(Date date); + public abstract void setDate(Date date); @Test public void testUpdateState() { diff --git a/tests/robotests/src/com/android/settings/enterprise/AdminGrantedPermissionsPreferenceControllerTestBase.java b/tests/robotests/src/com/android/settings/enterprise/AdminGrantedPermissionsPreferenceControllerTestBase.java index 610692d12d0..68ded374a5c 100644 --- a/tests/robotests/src/com/android/settings/enterprise/AdminGrantedPermissionsPreferenceControllerTestBase.java +++ b/tests/robotests/src/com/android/settings/enterprise/AdminGrantedPermissionsPreferenceControllerTestBase.java @@ -80,7 +80,7 @@ public abstract class AdminGrantedPermissionsPreferenceControllerTestBase { @Test public void testUpdateState() { final Preference preference = new Preference(mContext, null, 0, 0); - preference.setVisible(true); + preference.setVisible(false); setNumberOfPackagesWithAdminGrantedPermissions(20); when(mContext.getResources().getQuantityString(mStringResourceId, 20, 20)) diff --git a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java index 7724db8f84f..9b955e4862f 100644 --- a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java +++ b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImplTest.java @@ -55,8 +55,8 @@ import static org.mockito.Mockito.when; @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public final class EnterprisePrivacyFeatureProviderImplTest { - private final ComponentName DEVICE_OWNER = new ComponentName("dummy", "component"); - private final String DEVICE_OWNER_ORGANIZATION = new String("ACME"); + private final ComponentName OWNER = new ComponentName("dummy", "component"); + private final String OWNER_ORGANIZATION = new String("ACME"); private final Date TIMESTAMP = new Date(2011, 11, 11); private final int MY_USER_ID = UserHandle.myUserId(); private final int MANAGED_PROFILE_USER_ID = MY_USER_ID + 1; @@ -91,13 +91,13 @@ public final class EnterprisePrivacyFeatureProviderImplTest { when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); assertThat(mProvider.hasDeviceOwner()).isFalse(); - when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER); + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER); assertThat(mProvider.hasDeviceOwner()).isTrue(); } @Test public void testIsInCompMode() { - when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER); + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER); assertThat(mProvider.isInCompMode()).isFalse(); mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE)); @@ -116,18 +116,17 @@ public final class EnterprisePrivacyFeatureProviderImplTest { disclosure.append(mResources.getString(R.string.do_disclosure_learn_more_separator)); disclosure.append(mResources.getString(R.string.do_disclosure_learn_more), new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(context), 0); - when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER); + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER); when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); assertThat(mProvider.getDeviceOwnerDisclosure(context)).isEqualTo(disclosure); disclosure = new SpannableStringBuilder(); disclosure.append(mResources.getString(R.string.do_disclosure_with_name, - DEVICE_OWNER_ORGANIZATION)); + OWNER_ORGANIZATION)); disclosure.append(mResources.getString(R.string.do_disclosure_learn_more_separator)); disclosure.append(mResources.getString(R.string.do_disclosure_learn_more), new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(context), 0); - when(mDevicePolicyManager.getDeviceOwnerOrganizationName()) - .thenReturn(DEVICE_OWNER_ORGANIZATION); + when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(OWNER_ORGANIZATION); assertThat(mProvider.getDeviceOwnerDisclosure(context)).isEqualTo(disclosure); } @@ -193,4 +192,29 @@ public final class EnterprisePrivacyFeatureProviderImplTest { ProxyInfo.buildDirectProxy("localhost", 123)); assertThat(mProvider.isGlobalHttpProxySet()).isTrue(); } + + @Test + public void testGetMaximumFailedPasswordsForWipeInPrimaryUser() { + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); + when(mDevicePolicyManager.getDeviceOwnerUserId()).thenReturn(UserHandle.USER_NULL); + assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInPrimaryUser()).isEqualTo(0); + + when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER); + when(mDevicePolicyManager.getDeviceOwnerUserId()).thenReturn(UserHandle.USER_SYSTEM); + when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(OWNER, UserHandle.USER_SYSTEM)) + .thenReturn(10); + assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInPrimaryUser()).isEqualTo(10); + } + + @Test + public void testGetMaximumFailedPasswordsForWipeInManagedProfile() { + when(mDevicePolicyManager.getProfileOwnerAsUser(MANAGED_PROFILE_USER_ID)).thenReturn(OWNER); + when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(OWNER, MANAGED_PROFILE_USER_ID)) + .thenReturn(10); + + assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(0); + + mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE)); + assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(10); + } } diff --git a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java index 255976933b4..6c062aeaecb 100644 --- a/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java +++ b/tests/robotests/src/com/android/settings/enterprise/EnterprisePrivacySettingsTest.java @@ -73,7 +73,7 @@ public final class EnterprisePrivacySettingsTest { final List controllers = mSettings.getPreferenceControllers( ShadowApplication.getInstance().getApplicationContext()); assertThat(controllers).isNotNull(); - assertThat(controllers.size()).isEqualTo(12); + assertThat(controllers.size()).isEqualTo(14); assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class); assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class); assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class); @@ -93,5 +93,7 @@ public final class EnterprisePrivacySettingsTest { assertThat(controllers.get(10)).isInstanceOf( AlwaysOnVpnManagedProfilePreferenceController.class); assertThat(controllers.get(11)).isInstanceOf(GlobalHttpProxyPreferenceController.class); + assertThat(controllers.get(12)).isInstanceOf(FailedPasswordWipePrimaryUserPreferenceController.class); + assertThat(controllers.get(13)).isInstanceOf(FailedPasswordWipeManagedProfilePreferenceController.class); } } diff --git a/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipeManagedProfilePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipeManagedProfilePreferenceControllerTest.java new file mode 100644 index 00000000000..29952a701a2 --- /dev/null +++ b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipeManagedProfilePreferenceControllerTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; + +import com.android.settings.R; +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; + +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import static org.mockito.Mockito.when; + +/** + * Tests for {@link FailedPasswordWipeManagedProfilePreferenceController}. + */ +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public final class FailedPasswordWipeManagedProfilePreferenceControllerTest extends + FailedPasswordWipePreferenceControllerTestBase { + + private int mMaximumFailedPasswordsBeforeWipe = 0; + + public FailedPasswordWipeManagedProfilePreferenceControllerTest() { + super("failed_password_wipe_managed_profile", + R.plurals.enterprise_privacy_failed_password_wipe_work); + } + + @Override + public void setUp() { + super.setUp(); + mController = new FailedPasswordWipeManagedProfilePreferenceController(mContext); + } + + @Override + public void setMaximumFailedPasswordsBeforeWipe(int maximum) { + when(mFeatureFactory.enterprisePrivacyFeatureProvider + .getMaximumFailedPasswordsBeforeWipeInManagedProfile()).thenReturn(maximum); + } +} diff --git a/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerBaseTest.java b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerBaseTest.java new file mode 100644 index 00000000000..97d0d6d843d --- /dev/null +++ b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerBaseTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; + +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +/** + * Tests for {@link FailedPasswordWipePreferenceControllerBase}. + */ +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public final class FailedPasswordWipePreferenceControllerBaseTest extends + FailedPasswordWipePreferenceControllerTestBase { + + private int mMaximumFailedPasswordsBeforeWipe = 0; + + public FailedPasswordWipePreferenceControllerBaseTest() { + super(null, 123 /* stringResourceId */); + } + + @Override + public void setUp() { + super.setUp(); + mController = new FailedPasswordWipePreferenceControllerBaseTestable(); + } + + @Override + public void setMaximumFailedPasswordsBeforeWipe(int maximum) { + mMaximumFailedPasswordsBeforeWipe = maximum; + } + + private class FailedPasswordWipePreferenceControllerBaseTestable extends + FailedPasswordWipePreferenceControllerBase { + FailedPasswordWipePreferenceControllerBaseTestable() { + super(FailedPasswordWipePreferenceControllerBaseTest.this.mContext, mStringResourceId); + } + + @Override + protected int getMaximumFailedPasswordsBeforeWipe() { + return mMaximumFailedPasswordsBeforeWipe; + } + + @Override + public String getPreferenceKey() { + return null; + } + } +} diff --git a/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerTestBase.java b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerTestBase.java new file mode 100644 index 00000000000..5a74fa58e44 --- /dev/null +++ b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePreferenceControllerTestBase.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; +import android.content.res.Resources; +import android.support.v7.preference.Preference; + +import com.android.settings.testutils.FakeFeatureFactory; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +/** + * Common base for testing subclasses of {@link FailedPasswordWipePreferenceControllerBase}. + */ +public abstract class FailedPasswordWipePreferenceControllerTestBase { + + protected final String mKey; + protected final int mStringResourceId; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + protected Context mContext; + protected FakeFeatureFactory mFeatureFactory; + + protected FailedPasswordWipePreferenceControllerBase mController; + + public FailedPasswordWipePreferenceControllerTestBase(String key, int stringResourceId) { + mKey = key; + mStringResourceId = stringResourceId; + } + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + FakeFeatureFactory.setupForTest(mContext); + mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); + } + + public abstract void setMaximumFailedPasswordsBeforeWipe(int maximum); + + @Test + public void testUpdateState() { + final Preference preference = new Preference(mContext, null, 0, 0); + preference.setVisible(false); + + setMaximumFailedPasswordsBeforeWipe(10); + when(mContext.getResources().getQuantityString(mStringResourceId, 10, 10)) + .thenReturn("10 attempts"); + mController.updateState(preference); + assertThat(preference.getTitle()).isEqualTo("10 attempts"); + assertThat(preference.isVisible()).isTrue(); + + setMaximumFailedPasswordsBeforeWipe(0); + mController.updateState(preference); + assertThat(preference.isVisible()).isFalse(); + } + + @Test + public void testIsAvailable() { + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void testHandlePreferenceTreeClick() { + assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) + .isFalse(); + } + + @Test + public void testGetPreferenceKey() { + assertThat(mController.getPreferenceKey()).isEqualTo(mKey); + } +} diff --git a/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePrimaryUserPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePrimaryUserPreferenceControllerTest.java new file mode 100644 index 00000000000..ea6d977a4af --- /dev/null +++ b/tests/robotests/src/com/android/settings/enterprise/FailedPasswordWipePrimaryUserPreferenceControllerTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2017 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.enterprise; + +import android.content.Context; + +import com.android.settings.R; +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; + +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import static org.mockito.Mockito.when; + +/** + * Tests for {@link FailedPasswordWipePrimaryUserPreferenceController}. + */ +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public final class FailedPasswordWipePrimaryUserPreferenceControllerTest extends + FailedPasswordWipePreferenceControllerTestBase { + + private int mMaximumFailedPasswordsBeforeWipe = 0; + + public FailedPasswordWipePrimaryUserPreferenceControllerTest() { + super("failed_password_wipe_primary_user", + R.plurals.enterprise_privacy_failed_password_wipe_device); + } + + @Override + public void setUp() { + super.setUp(); + mController = new FailedPasswordWipePrimaryUserPreferenceController(mContext); + } + + @Override + public void setMaximumFailedPasswordsBeforeWipe(int maximum) { + when(mFeatureFactory.enterprisePrivacyFeatureProvider + .getMaximumFailedPasswordsBeforeWipeInPrimaryUser()).thenReturn(maximum); + } +}