Commit b3bd8685 authored by Vaclav Brozek's avatar Vaclav Brozek Committed by Commit Bot

Convert ReauthenticationManager.OverrideState to @IntDef

The OverrideState enum is converted in this CL to plain integers and
@IntDef for efficiency. More context on the associated bug.

Bug: 806315
Change-Id: Ie7b5d777394ca77c9f9447d8db33754bcf9f7017
Reviewed-on: https://chromium-review.googlesource.com/889738Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Commit-Queue: Vaclav Brozek <vabr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532810}
parent 5e4bfc34
......@@ -26,7 +26,12 @@ import java.lang.annotation.RetentionPolicy;
*/
public final class ReauthenticationManager {
// Used for various ways to override checks provided by this class.
public enum OverrideState { NOT_OVERRIDDEN, AVAILABLE, UNAVAILABLE }
@Retention(RetentionPolicy.SOURCE)
@IntDef({OVERRIDE_STATE_NOT_OVERRIDDEN, OVERRIDE_STATE_AVAILABLE, OVERRIDE_STATE_UNAVAILABLE})
public @interface OverrideState {}
public static final int OVERRIDE_STATE_NOT_OVERRIDDEN = 0;
public static final int OVERRIDE_STATE_AVAILABLE = 1;
public static final int OVERRIDE_STATE_UNAVAILABLE = 2;
// Used to specify the scope of the reauthentication -- either to grant bulk access like, e.g.,
// exporting passwords, or just one-at-a-time, like, e.g., viewing a single password.
......@@ -54,11 +59,13 @@ public final class ReauthenticationManager {
// Used in tests to override the result of checking for screen lock set-up. This allows the
// tests to be independent of a particular device configuration.
private static OverrideState sScreenLockSetUpOverride = OverrideState.NOT_OVERRIDDEN;
@OverrideState
private static int sScreenLockSetUpOverride = OVERRIDE_STATE_NOT_OVERRIDDEN;
// Used in tests to override the result of checking for availability of the screen-locking API.
// This allows the tests to be independent of a particular device configuration.
private static OverrideState sApiOverride = OverrideState.NOT_OVERRIDDEN;
@OverrideState
private static int sApiOverride = OVERRIDE_STATE_NOT_OVERRIDDEN;
// Used in tests to avoid displaying the OS reauth dialog.
private static boolean sSkipSystemReauth = false;
......@@ -83,14 +90,14 @@ public final class ReauthenticationManager {
}
@VisibleForTesting
public static void setScreenLockSetUpOverride(OverrideState screenLockSetUpOverride) {
public static void setScreenLockSetUpOverride(@OverrideState int screenLockSetUpOverride) {
sScreenLockSetUpOverride = screenLockSetUpOverride;
}
@VisibleForTesting
public static void setApiOverride(OverrideState apiOverride) {
public static void setApiOverride(@OverrideState int apiOverride) {
// Ensure that tests don't accidentally try to launch the OS-provided lock screen.
if (apiOverride == OverrideState.AVAILABLE) {
if (apiOverride == OVERRIDE_STATE_AVAILABLE) {
PasswordReauthenticationFragment.preventLockingForTesting();
}
......@@ -108,11 +115,11 @@ public final class ReauthenticationManager {
*/
public static boolean isReauthenticationApiAvailable() {
switch (sApiOverride) {
case NOT_OVERRIDDEN:
case OVERRIDE_STATE_NOT_OVERRIDDEN:
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
case AVAILABLE:
case OVERRIDE_STATE_AVAILABLE:
return true;
case UNAVAILABLE:
case OVERRIDE_STATE_UNAVAILABLE:
return false;
}
// This branch is not reachable.
......@@ -169,12 +176,12 @@ public final class ReauthenticationManager {
*/
public static boolean isScreenLockSetUp(Context context) {
switch (sScreenLockSetUpOverride) {
case NOT_OVERRIDDEN:
case OVERRIDE_STATE_NOT_OVERRIDDEN:
return ((KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE))
.isKeyguardSecure();
case AVAILABLE:
case OVERRIDE_STATE_AVAILABLE:
return true;
case UNAVAILABLE:
case OVERRIDE_STATE_UNAVAILABLE:
return false;
}
// This branch is not reachable.
......
......@@ -384,7 +384,7 @@ public class SavePasswordsPreferencesTest {
// Ensure there are no saved passwords reported to settings.
setPasswordSource(null);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -409,7 +409,7 @@ public class SavePasswordsPreferencesTest {
public void testExportMenuEnabled() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -434,7 +434,7 @@ public class SavePasswordsPreferencesTest {
@SmallTest
@Feature({"Preferences"})
public void testExportMenuMissing() throws Exception {
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -459,9 +459,9 @@ public class SavePasswordsPreferencesTest {
public void testExportTriggersSerialization() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -490,9 +490,9 @@ public class SavePasswordsPreferencesTest {
public void testExportMenuItem() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -521,9 +521,9 @@ public class SavePasswordsPreferencesTest {
public void testExportMenuItemNoLock() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.UNAVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_UNAVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -550,9 +550,9 @@ public class SavePasswordsPreferencesTest {
public void testExportMenuItemReenabledNoLock() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.UNAVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_UNAVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -582,7 +582,7 @@ public class SavePasswordsPreferencesTest {
public void testExportMenuItemReenabledReauthFailure() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setSkipSystemReauth(true);
final Preferences preferences =
......@@ -621,9 +621,9 @@ public class SavePasswordsPreferencesTest {
public void testExportIntent() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -665,9 +665,9 @@ public class SavePasswordsPreferencesTest {
public void testViewPasswordNoLock() throws Exception {
setPasswordSource(new SavedPasswordEntry("https://example.com", "test user", "password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.UNAVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_UNAVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -692,9 +692,9 @@ public class SavePasswordsPreferencesTest {
setPasswordSource(
new SavedPasswordEntry("https://example.com", "test user", "test password"));
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
......@@ -973,9 +973,9 @@ public class SavePasswordsPreferencesTest {
public void testSearchResultsPersistAfterEntryInspection() throws Exception {
setPasswordSourceWithMultipleEntries(GREEK_GODS);
setPasswordExceptions(new String[] {"http://exclu.de", "http://not-inclu.de"});
ReauthenticationManager.setApiOverride(ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.setApiOverride(ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
ReauthenticationManager.setScreenLockSetUpOverride(
ReauthenticationManager.OverrideState.AVAILABLE);
ReauthenticationManager.OVERRIDE_STATE_AVAILABLE);
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
SavePasswordsPreferences.class.getName());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment