Commit 24721f06 authored by Friedrich Horschig's avatar Friedrich Horschig Committed by Commit Bot

[PwdCheckAndroid] Trigger updating the password from edit fragment

Triggers the saving for non-empty passwords and closes the editing
activity. This completes the editing flow (see screenshot in the
linked bug).

Bug: 1114720,1092444
Change-Id: Iee86d7d598aef0f4049925328398b643ca093c3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2355945Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarNatalie Chouinard <chouinard@chromium.org>
Commit-Queue: Friedrich [CET] <fhorschig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799046}
parent 117f7f77
......@@ -30,6 +30,8 @@ import org.chromium.chrome.browser.ChromeBaseAppCompatActivity;
import org.chromium.chrome.browser.help.HelpAndFeedback;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.password_check.PasswordCheckComponentUiFactory;
import org.chromium.chrome.browser.password_check.PasswordCheckEditFragmentView;
import org.chromium.chrome.browser.password_check.PasswordCheckFactory;
import org.chromium.chrome.browser.password_check.PasswordCheckFragmentView;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.profiles.ProfileManagerUtils;
......@@ -284,6 +286,9 @@ public class SettingsActivity extends ChromeBaseAppCompatActivity
new SafetyCheckUpdatesDelegateImpl(this), new SettingsLauncherImpl());
} else if (fragment instanceof PasswordCheckFragmentView) {
PasswordCheckComponentUiFactory.create((PasswordCheckFragmentView) fragment);
} else if (fragment instanceof PasswordCheckEditFragmentView) {
PasswordCheckEditFragmentView editFragment = (PasswordCheckEditFragmentView) fragment;
editFragment.setCheckProvider(PasswordCheckFactory::getOrCreate);
}
}
......
......@@ -11,6 +11,7 @@ import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
......@@ -20,6 +21,8 @@ import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceFragmentCompat;
import org.chromium.base.supplier.Supplier;
/**
* This class is responsible for rendering the edit fragment where users can provide a new password
* for compromised credentials.
......@@ -30,11 +33,21 @@ public class PasswordCheckEditFragmentView extends PreferenceFragmentCompat {
@VisibleForTesting
static final String EXTRA_NEW_PASSWORD = "extra_new_password";
private Supplier<PasswordCheck> mPasswordCheckFactory;
private String mNewPassword;
private CompromisedCredential mCredential;
private EditText mPasswordText;
/**
* Initializes the password check factory that allows to retrieve a {@link PasswordCheck}
* implementation used for saving the changed credential.
* @param passwordCheckFactory A {@link Supplier<PasswordCheck>}.
*/
public void setCheckProvider(Supplier<PasswordCheck> passwordCheckFactory) {
mPasswordCheckFactory = passwordCheckFactory;
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {}
......@@ -91,6 +104,22 @@ public class PasswordCheckEditFragmentView extends PreferenceFragmentCompat {
outState.putString(EXTRA_NEW_PASSWORD, mNewPassword);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_save_edited_password) {
if (!TextUtils.isEmpty(mNewPassword)) saveChanges();
return true;
}
return super.onOptionsItemSelected(item);
}
private void saveChanges() {
assert !TextUtils.isEmpty(mNewPassword);
mPasswordCheckFactory.get().updateCredential(mCredential, mNewPassword);
getActivity().finish();
}
private CompromisedCredential getCredentialFromInstanceStateOrLaunchBundle(
Bundle savedInstanceState) {
if (savedInstanceState != null
......
......@@ -4,11 +4,17 @@
package org.chromium.chrome.browser.password_check;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.chromium.chrome.browser.password_check.PasswordCheckEditFragmentView.EXTRA_COMPROMISED_CREDENTIAL;
import static org.chromium.chrome.browser.password_check.PasswordCheckEditFragmentView.EXTRA_NEW_PASSWORD;
......@@ -25,12 +31,15 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.settings.SettingsActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.url.GURL;
/**
......@@ -38,6 +47,7 @@ import org.chromium.url.GURL;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@EnableFeatures({ChromeFeatureList.PASSWORD_CHECK})
public class PasswordCheckEditViewTest {
private static final CompromisedCredential ANA =
new CompromisedCredential("https://some-url.com/signin",
......@@ -50,17 +60,22 @@ public class PasswordCheckEditViewTest {
public SettingsActivityTestRule<PasswordCheckEditFragmentView> mTestRule =
new SettingsActivityTestRule<>(PasswordCheckEditFragmentView.class);
@Mock
private PasswordCheck mPasswordCheck;
@Before
public void setUp() throws InterruptedException {
MockitoAnnotations.initMocks(this);
PasswordCheckFactory.setPasswordCheckForTesting(mPasswordCheck);
setUpUiLaunchedFromSettings();
pollUiThread(() -> mPasswordCheckEditView != null);
mPasswordCheckEditView.setCheckProvider(PasswordCheckFactory::getOrCreate);
}
@Test
@MediumTest
public void testLoadsCredential() {
pollUiThread(() -> mPasswordCheckEditView != null);
EditText origin = mPasswordCheckEditView.getView().findViewById(R.id.site_edit);
assertNotNull(origin);
assertNotNull(origin.getText());
......@@ -84,8 +99,6 @@ public class PasswordCheckEditViewTest {
@Test
@MediumTest
public void testSavesCredentialAndChangedPasswordInBundle() {
pollUiThread(() -> mPasswordCheckEditView != null);
// Change the password.
final String newPassword = "NewPassword";
EditText password = mPasswordCheckEditView.getView().findViewById(R.id.password_edit);
......@@ -103,6 +116,20 @@ public class PasswordCheckEditViewTest {
assertThat(bundle.getString(EXTRA_NEW_PASSWORD), equalTo(newPassword));
}
@Test
@MediumTest
public void testTriggersSendingNewPasswordForCredential() {
// Change the password.
final String newPassword = "NewPassword";
EditText password = mPasswordCheckEditView.getView().findViewById(R.id.password_edit);
assertNotNull(password);
runOnUiThreadBlocking(() -> password.setText(newPassword));
onView(withId(R.id.action_save_edited_password)).perform(click());
verify(mPasswordCheck).updateCredential(eq(ANA), eq(newPassword));
}
private void setUpUiLaunchedFromSettings() {
Bundle fragmentArgs = new Bundle();
fragmentArgs.putParcelable(EXTRA_COMPROMISED_CREDENTIAL, ANA);
......
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