Add wipe on login failure to Privacy Settings page

This CL adds information to the Enterprise Privacy Setting page that
tells the user how many times the password can be mistyped before
the device (or the work profile) is forcefully wiped.

Test: make RunSettingsRoboTests
Bug: 32692748

Change-Id: I4ae316802dbf5853ab4eacb0787647372d5e26c2
This commit is contained in:
Bartosz Fabianowski
2017-02-14 11:45:20 +01:00
parent c1a7723c17
commit 8903f66662
20 changed files with 542 additions and 21 deletions

View File

@@ -8153,6 +8153,16 @@
<string name="enterprise_privacy_lock_device">Admin can lock device and reset password</string> <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] --> <!-- 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> <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] --> <!-- 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> <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] --> <!-- Message indicating that the device is enterprise-managed by a Device Owner [CHAR LIMIT=NONE] -->

View File

@@ -106,5 +106,13 @@
android:title="@string/enterprise_privacy_wipe_device" android:title="@string/enterprise_privacy_wipe_device"
settings:allowDividerBelow="true" settings:allowDividerBelow="true"
settings:multiLine="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> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -26,12 +26,10 @@ import java.util.Date;
public abstract class AdminActionPreferenceControllerBase extends PreferenceController { public abstract class AdminActionPreferenceControllerBase extends PreferenceController {
private final Context mContext;
protected final EnterprisePrivacyFeatureProvider mFeatureProvider; protected final EnterprisePrivacyFeatureProvider mFeatureProvider;
public AdminActionPreferenceControllerBase(Context context) { public AdminActionPreferenceControllerBase(Context context) {
super(context); super(context);
mContext = context;
mFeatureProvider = FeatureFactory.getFactory(context) mFeatureProvider = FeatureFactory.getFactory(context)
.getEnterprisePrivacyFeatureProvider(context); .getEnterprisePrivacyFeatureProvider(context);
} }

View File

@@ -26,6 +26,13 @@ import android.support.annotation.Nullable;
* newer than the API version supported by Robolectric. * newer than the API version supported by Robolectric.
*/ */
public interface DevicePolicyManagerWrapper { public interface DevicePolicyManagerWrapper {
/**
* Calls {@code DevicePolicyManager.getMaximumFailedPasswordsForWipe()}.
*
* @see android.app.admin.DevicePolicyManager#getMaximumFailedPasswordsForWipe
*/
int getMaximumFailedPasswordsForWipe(@Nullable ComponentName admin, int userHandle);
/** /**
* Calls {@code DevicePolicyManager.getDeviceOwnerComponentOnAnyUser()}. * Calls {@code DevicePolicyManager.getDeviceOwnerComponentOnAnyUser()}.
* *
@@ -33,12 +40,26 @@ public interface DevicePolicyManagerWrapper {
*/ */
ComponentName getDeviceOwnerComponentOnAnyUser(); 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()}. * Calls {@code DevicePolicyManager.getDeviceOwnerNameOnAnyUser()}.
* *
* @see android.app.admin.DevicePolicyManager#getDeviceOwnerNameOnAnyUser * @see android.app.admin.DevicePolicyManager#getDeviceOwnerNameOnAnyUser
*/ */
public CharSequence getDeviceOwnerOrganizationName(); CharSequence getDeviceOwnerOrganizationName();
/** /**
* Calls {@code DevicePolicyManager.getPermissionGrantState()}. * Calls {@code DevicePolicyManager.getPermissionGrantState()}.
@@ -53,19 +74,19 @@ public interface DevicePolicyManagerWrapper {
* *
* @see android.app.admin.DevicePolicyManager#getLastSecurityLogRetrievalTime * @see android.app.admin.DevicePolicyManager#getLastSecurityLogRetrievalTime
*/ */
public long getLastSecurityLogRetrievalTime(); long getLastSecurityLogRetrievalTime();
/** /**
* Calls {@code DevicePolicyManager.getLastBugReportRequestTime()}. * Calls {@code DevicePolicyManager.getLastBugReportRequestTime()}.
* *
* @see android.app.admin.DevicePolicyManager#getLastBugReportRequestTime * @see android.app.admin.DevicePolicyManager#getLastBugReportRequestTime
*/ */
public long getLastBugReportRequestTime(); long getLastBugReportRequestTime();
/** /**
* Calls {@code DevicePolicyManager.getLastNetworkLogRetrievalTime()}. * Calls {@code DevicePolicyManager.getLastNetworkLogRetrievalTime()}.
* *
* @see android.app.admin.DevicePolicyManager#getLastNetworkLogRetrievalTime * @see android.app.admin.DevicePolicyManager#getLastNetworkLogRetrievalTime
*/ */
public long getLastNetworkLogRetrievalTime(); long getLastNetworkLogRetrievalTime();
} }

View File

@@ -27,11 +27,26 @@ public class DevicePolicyManagerWrapperImpl implements DevicePolicyManagerWrappe
mDpm = dpm; mDpm = dpm;
} }
@Override
public int getMaximumFailedPasswordsForWipe(@Nullable ComponentName admin, int userHandle) {
return mDpm.getMaximumFailedPasswordsForWipe(admin, userHandle);
}
@Override @Override
public ComponentName getDeviceOwnerComponentOnAnyUser() { public ComponentName getDeviceOwnerComponentOnAnyUser() {
return mDpm.getDeviceOwnerComponentOnAnyUser(); return mDpm.getDeviceOwnerComponentOnAnyUser();
} }
@Override
public int getDeviceOwnerUserId() {
return mDpm.getDeviceOwnerUserId();
}
@Override
public @Nullable ComponentName getProfileOwnerAsUser(final int userId) {
return mDpm.getProfileOwnerAsUser(userId);
}
@Override @Override
public CharSequence getDeviceOwnerOrganizationName() { public CharSequence getDeviceOwnerOrganizationName() {
return mDpm.getDeviceOwnerOrganizationName(); return mDpm.getDeviceOwnerOrganizationName();

View File

@@ -74,4 +74,16 @@ public interface EnterprisePrivacyFeatureProvider {
* Returns whether the Device Owner set a recommended global HTTP proxy. * Returns whether the Device Owner set a recommended global HTTP proxy.
*/ */
boolean isGlobalHttpProxySet(); 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();
} }

View File

@@ -16,6 +16,7 @@
package com.android.settings.enterprise; package com.android.settings.enterprise;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@@ -70,12 +71,12 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe
return userInfo.id; return userInfo.id;
} }
} }
return -1; return UserHandle.USER_NULL;
} }
@Override @Override
public boolean isInCompMode() { public boolean isInCompMode() {
return hasDeviceOwner() && getManagedProfileUserId() != -1; return hasDeviceOwner() && getManagedProfileUserId() != UserHandle.USER_NULL;
} }
@Override @Override
@@ -124,7 +125,7 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe
@Override @Override
public boolean isAlwaysOnVpnSetInManagedProfile() { public boolean isAlwaysOnVpnSetInManagedProfile() {
final int managedProfileUserId = getManagedProfileUserId(); final int managedProfileUserId = getManagedProfileUserId();
return managedProfileUserId != -1 && return managedProfileUserId != UserHandle.USER_NULL &&
VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId); VpnUtils.isAlwaysOnVpnSet(mCm, managedProfileUserId);
} }
@@ -133,6 +134,28 @@ public class EnterprisePrivacyFeatureProviderImpl implements EnterprisePrivacyFe
return mCm.getGlobalProxy() != null; 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 { protected static class EnterprisePrivacySpan extends ClickableSpan {
private final Context mContext; private final Context mContext;

View File

@@ -63,6 +63,8 @@ public class EnterprisePrivacySettings extends DashboardFragment {
controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context)); controllers.add(new AlwaysOnVpnPrimaryUserPreferenceController(context));
controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context)); controllers.add(new AlwaysOnVpnManagedProfilePreferenceController(context));
controllers.add(new GlobalHttpProxyPreferenceController(context)); controllers.add(new GlobalHttpProxyPreferenceController(context));
controllers.add(new FailedPasswordWipePrimaryUserPreferenceController(context));
controllers.add(new FailedPasswordWipeManagedProfilePreferenceController(context));
return controllers; return controllers;
} }

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -16,6 +16,8 @@
package com.android.settings.enterprise; package com.android.settings.enterprise;
import android.content.Context;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -53,7 +55,7 @@ public final class AdminActionPreferenceControllerBaseTest extends
private class AdminActionPreferenceControllerBaseTestable extends private class AdminActionPreferenceControllerBaseTestable extends
AdminActionPreferenceControllerBase { AdminActionPreferenceControllerBase {
AdminActionPreferenceControllerBaseTestable() { AdminActionPreferenceControllerBaseTestable() {
super(mContext); super(AdminActionPreferenceControllerBaseTest.this.mContext);
} }
@Override @Override

View File

@@ -54,7 +54,7 @@ public abstract class AdminActionPreferenceControllerTestBase {
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
} }
protected abstract void setDate(Date date); public abstract void setDate(Date date);
@Test @Test
public void testUpdateState() { public void testUpdateState() {

View File

@@ -80,7 +80,7 @@ public abstract class AdminGrantedPermissionsPreferenceControllerTestBase {
@Test @Test
public void testUpdateState() { public void testUpdateState() {
final Preference preference = new Preference(mContext, null, 0, 0); final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true); preference.setVisible(false);
setNumberOfPackagesWithAdminGrantedPermissions(20); setNumberOfPackagesWithAdminGrantedPermissions(20);
when(mContext.getResources().getQuantityString(mStringResourceId, 20, 20)) when(mContext.getResources().getQuantityString(mStringResourceId, 20, 20))

View File

@@ -55,8 +55,8 @@ import static org.mockito.Mockito.when;
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class EnterprisePrivacyFeatureProviderImplTest { public final class EnterprisePrivacyFeatureProviderImplTest {
private final ComponentName DEVICE_OWNER = new ComponentName("dummy", "component"); private final ComponentName OWNER = new ComponentName("dummy", "component");
private final String DEVICE_OWNER_ORGANIZATION = new String("ACME"); private final String OWNER_ORGANIZATION = new String("ACME");
private final Date TIMESTAMP = new Date(2011, 11, 11); private final Date TIMESTAMP = new Date(2011, 11, 11);
private final int MY_USER_ID = UserHandle.myUserId(); private final int MY_USER_ID = UserHandle.myUserId();
private final int MANAGED_PROFILE_USER_ID = MY_USER_ID + 1; private final int MANAGED_PROFILE_USER_ID = MY_USER_ID + 1;
@@ -91,13 +91,13 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null);
assertThat(mProvider.hasDeviceOwner()).isFalse(); assertThat(mProvider.hasDeviceOwner()).isFalse();
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER); when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
assertThat(mProvider.hasDeviceOwner()).isTrue(); assertThat(mProvider.hasDeviceOwner()).isTrue();
} }
@Test @Test
public void testIsInCompMode() { public void testIsInCompMode() {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER); when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
assertThat(mProvider.isInCompMode()).isFalse(); assertThat(mProvider.isInCompMode()).isFalse();
mProfiles.add(new UserInfo(MANAGED_PROFILE_USER_ID, "", "", UserInfo.FLAG_MANAGED_PROFILE)); 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_separator));
disclosure.append(mResources.getString(R.string.do_disclosure_learn_more), disclosure.append(mResources.getString(R.string.do_disclosure_learn_more),
new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(context), 0); new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(context), 0);
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(DEVICE_OWNER); when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(OWNER);
when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
assertThat(mProvider.getDeviceOwnerDisclosure(context)).isEqualTo(disclosure); assertThat(mProvider.getDeviceOwnerDisclosure(context)).isEqualTo(disclosure);
disclosure = new SpannableStringBuilder(); disclosure = new SpannableStringBuilder();
disclosure.append(mResources.getString(R.string.do_disclosure_with_name, 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_separator));
disclosure.append(mResources.getString(R.string.do_disclosure_learn_more), disclosure.append(mResources.getString(R.string.do_disclosure_learn_more),
new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(context), 0); new EnterprisePrivacyFeatureProviderImpl.EnterprisePrivacySpan(context), 0);
when(mDevicePolicyManager.getDeviceOwnerOrganizationName()) when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(OWNER_ORGANIZATION);
.thenReturn(DEVICE_OWNER_ORGANIZATION);
assertThat(mProvider.getDeviceOwnerDisclosure(context)).isEqualTo(disclosure); assertThat(mProvider.getDeviceOwnerDisclosure(context)).isEqualTo(disclosure);
} }
@@ -193,4 +192,29 @@ public final class EnterprisePrivacyFeatureProviderImplTest {
ProxyInfo.buildDirectProxy("localhost", 123)); ProxyInfo.buildDirectProxy("localhost", 123));
assertThat(mProvider.isGlobalHttpProxySet()).isTrue(); 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);
}
} }

View File

@@ -73,7 +73,7 @@ public final class EnterprisePrivacySettingsTest {
final List<PreferenceController> controllers = mSettings.getPreferenceControllers( final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
ShadowApplication.getInstance().getApplicationContext()); ShadowApplication.getInstance().getApplicationContext());
assertThat(controllers).isNotNull(); assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(12); assertThat(controllers.size()).isEqualTo(14);
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class); assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class); assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class); assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
@@ -93,5 +93,7 @@ public final class EnterprisePrivacySettingsTest {
assertThat(controllers.get(10)).isInstanceOf( assertThat(controllers.get(10)).isInstanceOf(
AlwaysOnVpnManagedProfilePreferenceController.class); AlwaysOnVpnManagedProfilePreferenceController.class);
assertThat(controllers.get(11)).isInstanceOf(GlobalHttpProxyPreferenceController.class); assertThat(controllers.get(11)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
assertThat(controllers.get(12)).isInstanceOf(FailedPasswordWipePrimaryUserPreferenceController.class);
assertThat(controllers.get(13)).isInstanceOf(FailedPasswordWipeManagedProfilePreferenceController.class);
} }
} }

View File

@@ -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);
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}