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

Fix invalid cast in SavePasswordsPreferences#resetList

The resetList method tries to call PreferenceGroup#removeAll on
TextMessagePreference (which is not a PreferenceGroup) if an update of
the settings happens while the last state had no saved passwords to
display.

This is a rare situation because people who have no saved passwords
are not very often inspecting their passwords settings, but can happen
in reality as well (e.g., when somebody inspects the page during early
moments of syncing and Chrome sync populates the page as the user
views it).

This CL introduces a special method to do the job of resetList in the
case there are no lists, and adds a test which executed the invalid
cast before the fix.

Bug: 788701
Change-Id: I64ada510deb78cc7b8f22f085ef2f401e9bb76ba
Reviewed-on: https://chromium-review.googlesource.com/806165
Commit-Queue: Vaclav Brozek <vabr@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521667}
parent e46da065
......@@ -52,10 +52,10 @@ public class SavePasswordsPreferences extends PreferenceFragment
public static final String PREF_SAVE_PASSWORDS_SWITCH = "save_passwords_switch";
public static final String PREF_AUTOSIGNIN_SWITCH = "autosignin_switch";
private static final String PREF_CATEGORY_SAVED_PASSWORDS = "saved_passwords";
private static final String PREF_CATEGORY_EXCEPTIONS = "exceptions";
private static final String PREF_MANAGE_ACCOUNT_LINK = "manage_account_link";
private static final String PREF_CATEGORY_SAVED_PASSWORDS_NO_TEXT = "saved_passwords_no_text";
private static final String PREF_KEY_CATEGORY_SAVED_PASSWORDS = "saved_passwords";
private static final String PREF_KEY_CATEGORY_EXCEPTIONS = "exceptions";
private static final String PREF_KEY_MANAGE_ACCOUNT_LINK = "manage_account_link";
private static final String PREF_KEY_SAVED_PASSWORDS_NO_TEXT = "saved_passwords_no_text";
// Name of the feature controlling the password export functionality.
private static final String EXPORT_PASSWORDS = "password-export";
......@@ -108,7 +108,7 @@ public class SavePasswordsPreferences extends PreferenceFragment
private void displayEmptyScreenMessage() {
mEmptyView = new TextMessagePreference(getActivity(), null);
mEmptyView.setSummary(R.string.saved_passwords_none_text);
mEmptyView.setKey(PREF_CATEGORY_SAVED_PASSWORDS_NO_TEXT);
mEmptyView.setKey(PREF_KEY_SAVED_PASSWORDS_NO_TEXT);
mEmptyView.setOrder(ORDER_SAVED_PASSWORDS_NO_TEXT);
getPreferenceScreen().addPreference(mEmptyView);
}
......@@ -134,6 +134,10 @@ public class SavePasswordsPreferences extends PreferenceFragment
mPasswordManagerHandler.updatePasswordLists();
}
/**
* Removes the UI displaying the list of saved passwords or exceptions.
* @param preferenceCategoryKey The key string identifying the PreferenceCategory to be removed.
*/
private void resetList(String preferenceCategoryKey) {
PreferenceCategory profileCategory =
(PreferenceCategory) getPreferenceScreen().findPreference(preferenceCategoryKey);
......@@ -143,10 +147,20 @@ public class SavePasswordsPreferences extends PreferenceFragment
}
}
/**
* Removes the message informing the user that there are no saved entries to display.
*/
private void resetNoEntriesTextMessage() {
Preference message = getPreferenceScreen().findPreference(PREF_KEY_SAVED_PASSWORDS_NO_TEXT);
if (message != null) {
getPreferenceScreen().removePreference(message);
}
}
@Override
public void passwordListAvailable(int count) {
resetList(PREF_CATEGORY_SAVED_PASSWORDS);
resetList(PREF_CATEGORY_SAVED_PASSWORDS_NO_TEXT);
resetList(PREF_KEY_CATEGORY_SAVED_PASSWORDS);
resetNoEntriesTextMessage();
mNoPasswords = count == 0;
if (mNoPasswords) {
......@@ -157,7 +171,7 @@ public class SavePasswordsPreferences extends PreferenceFragment
displayManageAccountLink();
PreferenceCategory profileCategory = new PreferenceCategory(getActivity());
profileCategory.setKey(PREF_CATEGORY_SAVED_PASSWORDS);
profileCategory.setKey(PREF_KEY_CATEGORY_SAVED_PASSWORDS);
profileCategory.setTitle(R.string.section_saved_passwords);
profileCategory.setOrder(ORDER_SAVED_PASSWORDS);
getPreferenceScreen().addPreference(profileCategory);
......@@ -182,8 +196,8 @@ public class SavePasswordsPreferences extends PreferenceFragment
@Override
public void passwordExceptionListAvailable(int count) {
resetList(PREF_CATEGORY_EXCEPTIONS);
resetList(PREF_CATEGORY_SAVED_PASSWORDS_NO_TEXT);
resetList(PREF_KEY_CATEGORY_EXCEPTIONS);
resetNoEntriesTextMessage();
mNoPasswordExceptions = count == 0;
if (mNoPasswordExceptions) {
......@@ -194,7 +208,7 @@ public class SavePasswordsPreferences extends PreferenceFragment
displayManageAccountLink();
PreferenceCategory profileCategory = new PreferenceCategory(getActivity());
profileCategory.setKey(PREF_CATEGORY_EXCEPTIONS);
profileCategory.setKey(PREF_KEY_CATEGORY_EXCEPTIONS);
profileCategory.setTitle(R.string.section_saved_passwords_exceptions);
profileCategory.setOrder(ORDER_EXCEPTIONS);
getPreferenceScreen().addPreference(profileCategory);
......@@ -300,7 +314,7 @@ public class SavePasswordsPreferences extends PreferenceFragment
}
private void displayManageAccountLink() {
if (getPreferenceScreen().findPreference(PREF_MANAGE_ACCOUNT_LINK) == null) {
if (getPreferenceScreen().findPreference(PREF_KEY_MANAGE_ACCOUNT_LINK) == null) {
if (mLinkPref == null) {
ForegroundColorSpan colorSpan = new ForegroundColorSpan(
ApiCompatibilityUtils.getColor(getResources(), R.color.google_blue_700));
......@@ -308,7 +322,7 @@ public class SavePasswordsPreferences extends PreferenceFragment
getString(R.string.manage_passwords_text),
new SpanApplier.SpanInfo("<link>", "</link>", colorSpan));
mLinkPref = new ChromeBasePreference(getActivity());
mLinkPref.setKey(PREF_MANAGE_ACCOUNT_LINK);
mLinkPref.setKey(PREF_KEY_MANAGE_ACCOUNT_LINK);
mLinkPref.setTitle(title);
mLinkPref.setOnPreferenceClickListener(this);
mLinkPref.setOrder(ORDER_MANAGE_ACCOUNT_LINK);
......
......@@ -40,6 +40,29 @@ public class SavePasswordsPreferencesTest {
@Rule
public TestRule mProcessor = new Features.InstrumentationProcessor();
/**
* Ensure that resetting of empty passwords list works.
*/
@Test
@SmallTest
@Feature({"Preferences"})
public void testResetListEmpty() throws Exception {
// Load the preferences, they should show the empty list.
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
SavePasswordsPreferences.class.getName());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
SavePasswordsPreferences savePasswordPreferences =
(SavePasswordsPreferences) preferences.getFragmentForTest();
// Emulate an update from PasswordStore. This should not crash.
savePasswordPreferences.passwordListAvailable(0);
}
});
}
/**
* Ensure that the on/off switch in "Save Passwords" settings actually enables and disables
* password saving.
......
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