Merge "Add wipe on login failure to Privacy Settings page"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index dfcbc96..38ba6c5 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8166,6 +8166,16 @@
     <string name="enterprise_privacy_lock_device">Admin can lock device and reset password</string>
     <!-- Label explaining that the admin can wipe the device remotely. [CHAR LIMIT=NONE] -->
     <string name="enterprise_privacy_wipe_device">Admin can delete all device data</string>
+    <!-- Label explaining that the admin configured the device to wipe itself when the password is mistyped this many times. [CHAR LIMIT=NONE] -->
+    <plurals name="enterprise_privacy_failed_password_wipe_device">
+        <item quantity="one">Admin set maximum password attempts to <xliff:g id="count">%d</xliff:g> before deleting all device data</item>
+        <item quantity="other">Admin set maximum password attempts to <xliff:g id="count">%d</xliff:g> before deleting all device data</item>
+    </plurals>
+    <!-- Label explaining that the admin configured the work profile to wipe itself when the password is mistyped this many times. [CHAR LIMIT=NONE] -->
+    <plurals name="enterprise_privacy_failed_password_wipe_work">
+        <item quantity="one">Admin set maximum password attempts to <xliff:g id="count">%d</xliff:g> before deleting work profile data</item>
+        <item quantity="other">Admin set maximum password attempts to <xliff:g id="count">%d</xliff:g> before deleting work profile data</item>
+    </plurals>
     <!-- Message indicating that the device is enterprise-managed by a Device Owner [CHAR LIMIT=NONE] -->
     <string name="do_disclosure_generic">This device is managed.</string>
     <!-- Message indicating that the device is enterprise-managed by a Device Owner [CHAR LIMIT=NONE] -->
diff --git a/res/xml/enterprise_privacy_settings.xml b/res/xml/enterprise_privacy_settings.xml
index cc63d5f..bfb1247 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"/>
+        <com.android.settings.DividerPreference
+                android:key="failed_password_wipe_primary_user"
+                settings:allowDividerBelow="true"
+                settings:multiLine="true"/>
+        <com.android.settings.DividerPreference
+                android:key="failed_password_wipe_managed_profile"
+                settings:allowDividerBelow="true"
+                settings:multiLine="true"/>
     </PreferenceCategory>
 </PreferenceScreen>
diff --git a/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java b/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java
index ef03cfb..6b72573 100644
--- a/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java
+++ b/src/com/android/settings/enterprise/AdminActionPreferenceControllerBase.java
@@ -26,12 +26,10 @@
 
 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 5be7884..014092f 100644
--- a/src/com/android/settings/enterprise/DevicePolicyManagerWrapper.java
+++ b/src/com/android/settings/enterprise/DevicePolicyManagerWrapper.java
@@ -27,6 +27,13 @@
  */
 public interface DevicePolicyManagerWrapper {
     /**
+     * Calls {@code DevicePolicyManager.getMaximumFailedPasswordsForWipe()}.
+     *
+     * @see android.app.admin.DevicePolicyManager#getMaximumFailedPasswordsForWipe
+     */
+    int getMaximumFailedPasswordsForWipe(@Nullable ComponentName admin, int userHandle);
+
+    /**
      * Calls {@code DevicePolicyManager.getDeviceOwnerComponentOnAnyUser()}.
      *
      * @see android.app.admin.DevicePolicyManager#getDeviceOwnerComponentOnAnyUser
@@ -34,11 +41,25 @@
     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 @@
      *
      * @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 6e162a8..210faec 100644
--- a/src/com/android/settings/enterprise/DevicePolicyManagerWrapperImpl.java
+++ b/src/com/android/settings/enterprise/DevicePolicyManagerWrapperImpl.java
@@ -28,11 +28,26 @@
     }
 
     @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 91ad119..79b12e6 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProvider.java
@@ -74,4 +74,16 @@
      * 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 bb5412b..645a1f5 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 @@
                 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 @@
     @Override
     public boolean isAlwaysOnVpnSetInManagedProfile() {
         final int managedProfileUserId = getManagedProfileUserId();
-        return managedProfileUserId != -1 &&
+        return managedProfileUserId != UserHandle.USER_NULL &&
                 VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId);
     }
 
@@ -133,6 +134,28 @@
         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 75d3b10..821b7ff 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacySettings.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacySettings.java
@@ -63,6 +63,8 @@
         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 0000000..2eac0a9
--- /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 0000000..7cdd8da
--- /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 0000000..91bdf9b
--- /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 66233f5..6442242 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 @@
     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 c8cb69c..59043ed 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 @@
         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 610692d..68ded37 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 @@
     @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 7724db8..9b955e4 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 @@
 @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 @@
         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 @@
         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 @@
                 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 2559769..6c062ae 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 @@
         final List<PreferenceController> 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 @@
         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 0000000..29952a7
--- /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 0000000..97d0d6d
--- /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 0000000..5a74fa5
--- /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 0000000..ea6d977
--- /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);
+    }
+}