blob: 5f038e616c9fc4d01d6780d775cb81ad4953288a [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "base/bind.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/renderer/autofill/fake_mojo_password_manager_driver.h"
#include "chrome/renderer/autofill/fake_password_generation_driver.h"
#include "chrome/renderer/autofill/password_generation_test_utils.h"
#include "chrome/test/base/chrome_render_view_test.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/content/renderer/form_tracker.h"
#include "components/autofill/content/renderer/password_generation_agent.h"
#include "components/autofill/content/renderer/test_password_autofill_agent.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom.h"
#include "components/autofill/core/common/password_form.h"
#include "components/autofill/core/common/password_form_field_prediction_map.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/safe_browsing/buildflags.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_view.h"
#include "content/public/test/browser_test_utils.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_widget.h"
#include "ui/events/keycodes/keyboard_codes.h"
#if defined(OS_WIN)
#include "third_party/blink/public/web/win/web_font_rendering.h"
#endif
using autofill::FormTracker;
using autofill::PasswordForm;
using autofill::mojom::FocusedFieldType;
using autofill::mojom::PasswordFormFieldPredictionType;
using autofill::mojom::SubmissionIndicatorEvent;
using base::ASCIIToUTF16;
using base::UTF16ToUTF8;
using blink::WebAutofillState;
using blink::WebDocument;
using blink::WebElement;
using blink::WebFormElement;
using blink::WebFrame;
using blink::WebInputElement;
using blink::WebString;
using testing::_;
namespace {
// The name of the username/password element in the form.
const char kUsernameName[] = "username";
const char kPasswordName[] = "password";
const char kDisplayName[] = "display-name";
const char kCreditCardOwnerName[] = "creditcardowner";
const char kCreditCardNumberName[] = "creditcardnumber";
const char kCreditCardVerificationName[] = "cvc";
const char kSearchField[] = "search";
const char kSocialMediaTextArea[] = "new_chirp";
const char kAliceUsername[] = "alice";
const char kAlicePassword[] = "password";
const char kBobUsername[] = "bob";
const char kBobPassword[] = "secret";
const char kCarolUsername[] = "Carol";
const char kCarolPassword[] = "test";
const char kCarolAlternateUsername[] = "RealCarolUsername";
const char kFormHTML[] =
"<FORM id='LoginTestForm' action='http://www.bidule.com'>"
" <INPUT type='text' id='random_field'/>"
" <INPUT type='text' id='username'/>"
" <INPUT type='password' id='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
const char kSocialNetworkPostFormHTML[] =
"<FORM id='SocialMediaPostForm' action='http://www.chirper.com'>"
" <TEXTAREA id='new_chirp'>"
" </TEXTAREA>"
" <INPUT type='submit' value='Chirp'/>"
"</FORM>";
const char kSearchFieldHTML[] =
"<FORM id='SearchFieldForm' action='http://www.gewgle.de'>"
" <INPUT type='search' id='search'/>"
" <INPUT type='submit' value='Chirp'/>"
"</FORM>";
const char kVisibleFormWithNoUsernameHTML[] =
"<head> <style> form {display: inline;} </style> </head>"
"<body>"
" <form name='LoginTestForm' action='http://www.bidule.com'>"
" <div>"
" <input type='password' id='password'/>"
" </div>"
" </form>"
"</body>";
const char kEmptyFormHTML[] =
"<head> <style> form {display: inline;} </style> </head>"
"<body> <form> </form> </body>";
const char kFormWithoutPasswordsHTML[] =
"<FORM>"
" <INPUT type='text' id='username'/>"
" <INPUT type='text' id='random_field'/>"
"</FORM>";
const char kNonVisibleFormHTML[] =
"<head> <style> form {visibility: hidden;} </style> </head>"
"<body>"
" <form>"
" <div>"
" <input type='password' id='password'/>"
" </div>"
" </form>"
"</body>";
const char kNonDisplayedFormHTML[] =
"<head> <style> form {display: none;} </style> </head>"
"<body>"
" <form>"
" <div>"
" <input type='password' id='password'/>"
" </div>"
" </form>"
"</body>";
const char kSignupFormHTML[] =
"<FORM id='LoginTestForm' name='LoginTestForm' "
" action='http://www.bidule.com'>"
" <INPUT type='text' id='random_info'/>"
" <INPUT type='password' id='new_password'/>"
" <INPUT type='password' id='confirm_password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
const char kEmptyWebpage[] =
"<html>"
" <head>"
" </head>"
" <body>"
" </body>"
"</html>";
const char kRedirectionWebpage[] =
"<html>"
" <head>"
" <meta http-equiv='Content-Type' content='text/html'>"
" <title>Redirection page</title>"
" <script></script>"
" </head>"
" <body>"
" <script type='text/javascript'>"
" function test(){}"
" </script>"
" </body>"
"</html>";
const char kSimpleWebpage[] =
"<html>"
" <head>"
" <meta charset='utf-8' />"
" <title>Title</title>"
" </head>"
" <body>"
" <form name='LoginTestForm'>"
" <input type='text' id='username'/>"
" <input type='password' id='password'/>"
" <input type='submit' value='Login'/>"
" </form>"
" </body>"
"</html>";
const char kWebpageWithDynamicContent[] =
"<html>"
" <head>"
" <meta charset='utf-8' />"
" <title>Title</title>"
" </head>"
" <body>"
" <script type='text/javascript'>"
" function addParagraph() {"
" var p = document.createElement('p');"
" document.body.appendChild(p);"
" }"
" window.onload = addParagraph;"
" </script>"
" </body>"
"</html>";
const char kJavaScriptClick[] =
"var event = new MouseEvent('click', {"
" 'view': window,"
" 'bubbles': true,"
" 'cancelable': true"
"});"
"var form = document.getElementById('myform1');"
"form.dispatchEvent(event);"
"console.log('clicked!');";
const char kFormHTMLWithTwoTextFields[] =
"<FORM name='LoginTestForm' id='LoginTestForm' "
"action='http://www.bidule.com'>"
" <INPUT type='text' id='display-name'/>"
" <INPUT type='text' id='username'/>"
" <INPUT type='password' id='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
const char kPasswordChangeFormHTML[] =
"<FORM name='ChangeWithUsernameForm' action='http://www.bidule.com'>"
" <INPUT type='text' id='username'/>"
" <INPUT type='password' id='password'/>"
" <INPUT type='password' id='newpassword'/>"
" <INPUT type='password' id='confirmpassword'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
const char kCreditCardFormHTML[] =
"<FORM name='ChangeWithUsernameForm' action='http://www.bidule.com'>"
" <INPUT type='text' id='creditcardowner'/>"
" <INPUT type='text' id='creditcardnumber'/>"
" <INPUT type='password' id='cvc'/>"
" <INPUT type='submit' value='Submit'/>"
"</FORM>";
const char kNoFormHTML[] =
" <INPUT type='text' id='username'/>"
" <INPUT type='password' id='password'/>";
const char kTwoNoUsernameFormsHTML[] =
"<FORM name='form1' action='http://www.bidule.com'>"
" <INPUT type='password' id='password1' name='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>"
"<FORM name='form2' action='http://www.bidule.com'>"
" <INPUT type='password' id='password2' name='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
const char kDivWrappedFormHTML[] =
"<DIV id='outer'>"
" <DIV id='inner'>"
" <FORM id='form' action='http://www.bidule.com'>"
" <INPUT type='text' id='username'/>"
" <INPUT type='password' id='password'/>"
" </FORM>"
" </DIV>"
"</DIV>";
// Sets the "readonly" attribute of |element| to the value given by |read_only|.
void SetElementReadOnly(WebInputElement& element, bool read_only) {
element.SetAttribute(WebString::FromUTF8("readonly"),
read_only ? WebString::FromUTF8("true") : WebString());
}
enum PasswordFormSourceType {
PasswordFormSubmitted,
PasswordFormSameDocumentNavigation,
};
enum class FieldChangeSource { USER, AUTOFILL, USER_AUTOFILL };
} // namespace
namespace autofill {
class PasswordAutofillAgentTest : public ChromeRenderViewTest {
public:
PasswordAutofillAgentTest() {
}
// Simulates the fill password form message being sent to the renderer.
// We use that so we don't have to make RenderView::OnFillPasswordForm()
// protected.
void SimulateOnFillPasswordForm(
const PasswordFormFillData& fill_data) {
password_autofill_agent_->FillPasswordForm(fill_data);
}
// Simulates the show initial password account suggestions message being sent
// to the renderer.
void SimulateOnShowInitialPasswordAccountSuggestions(
const PasswordFormFillData& fill_data) {
autofill_agent_->ShowInitialPasswordAccountSuggestions(fill_data);
}
void SendVisiblePasswordForms() {
static_cast<content::RenderFrameObserver*>(password_autofill_agent_)
->DidFinishLoad();
}
void SetUp() override {
ChromeRenderViewTest::SetUp();
#if defined(OS_WIN)
// Autofill uses the system font to render suggestion previews. On Windows
// an extra step is required to ensure that the system font is configured.
blink::WebFontRendering::SetMenuFontMetrics(
base::ASCIIToUTF16("Arial").c_str(), 12);
#endif
// TODO(crbug/862989): Remove workaround preventing non-test classes to bind
// fake_driver_ or fake_pw_client_.
password_autofill_agent_->GetPasswordManagerDriver();
password_generation_->RequestPasswordManagerClientForTesting();
base::RunLoop().RunUntilIdle(); // Executes binding the interfaces.
// Reject all requests to bind driver/client to anything but the test class:
view_->GetMainRenderFrame()
->GetRemoteAssociatedInterfaces()
->OverrideBinderForTesting(
mojom::PasswordGenerationDriver::Name_,
base::BindRepeating([](mojo::ScopedInterfaceEndpointHandle handle) {
handle.reset();
}));
view_->GetMainRenderFrame()
->GetRemoteAssociatedInterfaces()
->OverrideBinderForTesting(
mojom::PasswordManagerDriver::Name_,
base::BindRepeating([](mojo::ScopedInterfaceEndpointHandle handle) {
handle.reset();
}));
// Add a preferred login and an additional login to the FillData.
username1_ = ASCIIToUTF16(kAliceUsername);
password1_ = ASCIIToUTF16(kAlicePassword);
username2_ = ASCIIToUTF16(kBobUsername);
password2_ = ASCIIToUTF16(kBobPassword);
username3_ = ASCIIToUTF16(kCarolUsername);
password3_ = ASCIIToUTF16(kCarolPassword);
alternate_username3_ = ASCIIToUTF16(kCarolAlternateUsername);
FormFieldData username_field;
username_field.name = ASCIIToUTF16(kUsernameName);
username_field.value = username1_;
fill_data_.username_field = username_field;
FormFieldData password_field;
password_field.name = ASCIIToUTF16(kPasswordName);
password_field.value = password1_;
password_field.form_control_type = "password";
fill_data_.password_field = password_field;
PasswordAndRealm password2;
password2.password = password2_;
fill_data_.additional_logins[username2_] = password2;
PasswordAndRealm password3;
password3.password = password3_;
fill_data_.additional_logins[username3_] = password3;
// We need to set the origin so it matches the frame URL and the action so
// it matches the form action, otherwise we won't autocomplete.
UpdateOriginForHTML(kFormHTML);
fill_data_.action = GURL("http://www.bidule.com");
LoadHTML(kFormHTML);
// Necessary for SimulateElementClick() to work correctly.
GetWebWidget()->Resize(blink::WebSize(500, 500));
GetWebWidget()->SetFocus(true);
// Now retrieve the input elements so the test can access them.
UpdateUsernameAndPasswordElements();
}
void TearDown() override {
username_element_.Reset();
password_element_.Reset();
ChromeRenderViewTest::TearDown();
}
void RegisterMainFrameRemoteInterfaces() override {
// Because the test cases only involve the main frame in this test,
// the fake password client and the fake driver is only used on main frame.
blink::AssociatedInterfaceProvider* remote_associated_interfaces =
view_->GetMainRenderFrame()->GetRemoteAssociatedInterfaces();
remote_associated_interfaces->OverrideBinderForTesting(
mojom::PasswordGenerationDriver::Name_,
base::BindRepeating(
&PasswordAutofillAgentTest::BindPasswordManagerClient,
base::Unretained(this)));
remote_associated_interfaces->OverrideBinderForTesting(
mojom::PasswordManagerDriver::Name_,
base::BindRepeating(
&PasswordAutofillAgentTest::BindPasswordManagerDriver,
base::Unretained(this)));
}
void FocusElement(std::string element_id) {
std::string script =
"document.getElementById('" + element_id + "').focus()";
ExecuteJavaScriptForTests(script.c_str());
GetMainFrame()->AutofillClient()->DidCompleteFocusChangeInFrame();
}
void SetFillOnAccountSelect() {
scoped_feature_list_.InitAndEnableFeature(
password_manager::features::kFillOnAccountSelect);
}
void EnableShowAutofillSignatures() {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kShowAutofillSignatures);
}
void UpdateOriginForHTML(const std::string& html) {
std::string origin = "data:text/html;charset=utf-8," + html;
fill_data_.origin = GURL(origin);
}
void UpdateUsernameAndPasswordElements() {
WebDocument document = GetMainFrame()->GetDocument();
WebElement element =
document.GetElementById(WebString::FromUTF8(kUsernameName));
ASSERT_FALSE(element.IsNull());
username_element_ = element.To<WebInputElement>();
element = document.GetElementById(WebString::FromUTF8(kPasswordName));
ASSERT_FALSE(element.IsNull());
password_element_ = element.To<WebInputElement>();
}
// TODO(https://crbug.com/831123): Make the code of this function to be part
// of UpdateUsernameAndPasswordElements() when the old parsing is removed and
// filling by renderer ids is by default.
void UpdateRendererIDs() {
fill_data_.has_renderer_ids = true;
if (!username_element_.IsNull()) {
fill_data_.username_field.unique_renderer_id =
username_element_.UniqueRendererFormControlId();
}
ASSERT_FALSE(password_element_.IsNull());
fill_data_.password_field.unique_renderer_id =
password_element_.UniqueRendererFormControlId();
WebFormElement form = password_element_.Form();
fill_data_.form_renderer_id = form.IsNull()
? FormData::kNotSetFormRendererId
: form.UniqueRendererFormId();
}
void UpdateOnlyPasswordElement() {
WebDocument document = GetMainFrame()->GetDocument();
WebElement element =
document.GetElementById(WebString::FromUTF8(kPasswordName));
ASSERT_FALSE(element.IsNull());
password_element_ = element.To<WebInputElement>();
username_element_.Reset();
}
WebInputElement GetInputElementByID(const std::string& id) {
WebDocument document = GetMainFrame()->GetDocument();
WebElement element =
document.GetElementById(WebString::FromUTF8(id.c_str()));
return element.To<WebInputElement>();
}
void ClearUsernameAndPasswordFields() {
username_element_.SetValue(WebString());
username_element_.SetSuggestedValue(WebString());
username_element_.SetAutofillState(WebAutofillState::kNotFilled);
password_element_.SetValue(WebString());
password_element_.SetSuggestedValue(WebString());
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
}
void SimulateSuggestionChoice(WebInputElement& username_input) {
base::string16 username(ASCIIToUTF16(kAliceUsername));
base::string16 password(ASCIIToUTF16(kAlicePassword));
SimulateSuggestionChoiceOfUsernameAndPassword(username_input, username,
password);
}
void SimulateSuggestionChoiceOfUsernameAndPassword(
WebInputElement& input,
const base::string16& username,
const base::string16& password) {
// This call is necessary to setup the autofill agent appropriate for the
// user selection; simulates the menu actually popping up.
SimulatePointClick(gfx::Point(1, 1));
autofill_agent_->FormControlElementClicked(input, false);
autofill_agent_->FillPasswordSuggestion(username, password);
}
void SimulateUsernameTyping(const std::string& username) {
SimulatePointClick(gfx::Point(1, 1));
SimulateUserInputChangeForElement(&username_element_, username);
}
void SimulatePasswordTyping(const std::string& password) {
SimulateUserInputChangeForElement(&password_element_, password);
}
void SimulateUsernameFieldAutofill(const std::string& text) {
// Simulate set |username_element_| in focus.
static_cast<content::RenderFrameObserver*>(autofill_agent_)
->FocusedElementChanged(username_element_);
// Fill focused element (i.e. |username_element_|).
autofill_agent_->FillFieldWithValue(ASCIIToUTF16(text));
}
void SimulateUsernameFieldChange(FieldChangeSource change_source) {
switch (change_source) {
case FieldChangeSource::USER:
SimulateUsernameTyping("Alice");
break;
case FieldChangeSource::AUTOFILL:
SimulateUsernameFieldAutofill("Alice");
break;
case FieldChangeSource::USER_AUTOFILL:
SimulateUsernameTyping("A");
SimulateUsernameFieldAutofill("Alice");
break;
}
}
void CheckTextFieldsStateForElements(const WebInputElement& username_element,
const std::string& username,
bool username_autofilled,
const WebInputElement& password_element,
const std::string& password,
bool password_autofilled,
bool check_suggested_username,
bool check_suggested_password) {
EXPECT_EQ(username, check_suggested_username
? username_element.SuggestedValue().Utf8()
: username_element.Value().Utf8())
<< "check_suggested_username == " << check_suggested_username;
EXPECT_EQ(username_autofilled, username_element.IsAutofilled());
EXPECT_EQ(password, check_suggested_password
? password_element.SuggestedValue().Utf8()
: password_element.Value().Utf8())
<< "check_suggested_password == " << check_suggested_password;
EXPECT_EQ(password_autofilled, password_element.IsAutofilled());
}
// Checks the DOM-accessible value of the username element and the
// *suggested* value of the password element.
void CheckUsernameDOMStatePasswordSuggestedState(const std::string& username,
bool username_autofilled,
const std::string& password,
bool password_autofilled) {
CheckTextFieldsStateForElements(
username_element_, username, username_autofilled, password_element_,
password, password_autofilled, false /* check_suggested_username */,
true /* check_suggested_password */);
}
// Checks the DOM-accessible value of the username element and the
// DOM-accessible value of the password element.
void CheckTextFieldsDOMState(const std::string& username,
bool username_autofilled,
const std::string& password,
bool password_autofilled) {
CheckTextFieldsStateForElements(
username_element_, username, username_autofilled, password_element_,
password, password_autofilled, false /* check_suggested_username */,
false /* check_suggested_password */);
}
// Checks the suggested values of the |username| and |password| elements.
void CheckTextFieldsSuggestedState(const std::string& username,
bool username_autofilled,
const std::string& password,
bool password_autofilled) {
CheckTextFieldsStateForElements(
username_element_, username, username_autofilled, password_element_,
password, password_autofilled, true /* check_suggested_username */,
true /* check_suggested_password */);
}
void ResetFieldState(
WebInputElement* element,
const std::string& value = std::string(),
blink::WebAutofillState is_autofilled = WebAutofillState::kNotFilled) {
element->SetValue(WebString::FromUTF8(value));
element->SetSuggestedValue(WebString());
element->SetAutofillState(is_autofilled);
element->SetSelectionRange(value.size(), value.size());
}
void CheckUsernameSelection(int start, int end) {
EXPECT_EQ(start, username_element_.SelectionStart());
EXPECT_EQ(end, username_element_.SelectionEnd());
}
// Checks the message sent to PasswordAutofillManager to build the suggestion
// list. |username| is the expected username field value, and |show_all| is
// the expected flag for the PasswordAutofillManager, whether to show all
// suggestions, or only those starting with |username|.
void CheckSuggestions(const std::string& username, bool show_all) {
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(fake_driver_.called_show_pw_suggestions());
ASSERT_TRUE(static_cast<bool>(fake_driver_.show_pw_suggestions_username()));
EXPECT_EQ(ASCIIToUTF16(username),
*(fake_driver_.show_pw_suggestions_username()));
EXPECT_EQ(show_all,
static_cast<bool>(fake_driver_.show_pw_suggestions_options() &
autofill::SHOW_ALL));
fake_driver_.reset_show_pw_suggestions();
}
bool GetCalledShowPasswordSuggestions() {
base::RunLoop().RunUntilIdle();
return fake_driver_.called_show_pw_suggestions();
}
void ExpectFormSubmittedWithUsernameAndPasswords(
const std::string& username_value,
const std::string& password_value,
const std::string& new_password_value) {
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(fake_driver_.called_password_form_submitted());
ASSERT_TRUE(static_cast<bool>(fake_driver_.password_form_submitted()));
const autofill::PasswordForm& form =
*(fake_driver_.password_form_submitted());
EXPECT_EQ(ASCIIToUTF16(username_value), form.username_value);
EXPECT_EQ(ASCIIToUTF16(password_value), form.password_value);
EXPECT_EQ(ASCIIToUTF16(new_password_value), form.new_password_value);
EXPECT_EQ(SubmissionIndicatorEvent::HTML_FORM_SUBMISSION,
form.submission_event);
EXPECT_EQ(SubmissionIndicatorEvent::HTML_FORM_SUBMISSION,
form.form_data.submission_event);
}
void ExpectFieldPropertiesMasks(
PasswordFormSourceType expected_type,
const std::map<base::string16, FieldPropertiesMask>&
expected_properties_masks) {
base::RunLoop().RunUntilIdle();
autofill::PasswordForm form;
if (expected_type == PasswordFormSubmitted) {
ASSERT_TRUE(fake_driver_.called_password_form_submitted());
ASSERT_TRUE(static_cast<bool>(fake_driver_.password_form_submitted()));
form = *(fake_driver_.password_form_submitted());
} else {
ASSERT_EQ(PasswordFormSameDocumentNavigation, expected_type);
ASSERT_TRUE(fake_driver_.called_same_document_navigation());
ASSERT_TRUE(static_cast<bool>(
fake_driver_.password_form_same_document_navigation()));
form = *(fake_driver_.password_form_same_document_navigation());
}
size_t unchecked_masks = expected_properties_masks.size();
for (const FormFieldData& field : form.form_data.fields) {
const auto& it = expected_properties_masks.find(field.name);
if (it == expected_properties_masks.end())
continue;
EXPECT_EQ(field.properties_mask, it->second)
<< "Wrong mask for the field " << field.name;
unchecked_masks--;
}
EXPECT_TRUE(unchecked_masks == 0)
<< "Some expected masks are missed in FormData";
}
void ExpectSameDocumentNavigationWithUsernameAndPasswords(
const std::string& username_value,
const std::string& password_value,
const std::string& new_password_value,
SubmissionIndicatorEvent event) {
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(fake_driver_.called_same_document_navigation());
ASSERT_TRUE(static_cast<bool>(
fake_driver_.password_form_same_document_navigation()));
const autofill::PasswordForm& form =
*(fake_driver_.password_form_same_document_navigation());
EXPECT_EQ(ASCIIToUTF16(username_value), form.username_value);
EXPECT_EQ(ASCIIToUTF16(password_value), form.password_value);
EXPECT_EQ(ASCIIToUTF16(new_password_value), form.new_password_value);
EXPECT_EQ(event, form.submission_event);
EXPECT_EQ(event, form.form_data.submission_event);
}
void CheckIfEventsAreCalled(const std::vector<base::string16>& checkers,
bool expected) {
for (const base::string16& variable : checkers) {
int value;
EXPECT_TRUE(ExecuteJavaScriptAndReturnIntValue(variable, &value))
<< variable;
EXPECT_EQ(expected, value == 1) << variable;
}
}
void BindPasswordManagerDriver(mojo::ScopedInterfaceEndpointHandle handle) {
fake_driver_.BindRequest(
mojom::PasswordManagerDriverAssociatedRequest(std::move(handle)));
}
void BindPasswordManagerClient(mojo::ScopedInterfaceEndpointHandle handle) {
fake_pw_client_.BindRequest(
mojom::PasswordGenerationDriverAssociatedRequest(std::move(handle)));
}
void SaveAndSubmitForm() { SaveAndSubmitForm(username_element_.Form()); }
void SaveAndSubmitForm(const WebFormElement& form_element) {
FormTracker* tracker = autofill_agent_->form_tracker_for_testing();
static_cast<content::RenderFrameObserver*>(tracker)->WillSendSubmitEvent(
form_element);
static_cast<content::RenderFrameObserver*>(tracker)->WillSubmitForm(
form_element);
}
void CheckFirstFillingResult(FillingResult result) {
histogram_tester_.ExpectUniqueSample(
"PasswordManager.FirstRendererFillingResult", result, 1);
}
void SubmitForm() {
FormTracker* tracker = autofill_agent_->form_tracker_for_testing();
static_cast<content::RenderFrameObserver*>(tracker)->WillSubmitForm(
username_element_.Form());
}
void FireAjaxSucceeded() {
FormTracker* tracker = autofill_agent_->form_tracker_for_testing();
tracker->AjaxSucceeded();
}
void FireDidCommitProvisionalLoad() {
FormTracker* tracker = autofill_agent_->form_tracker_for_testing();
static_cast<content::RenderFrameObserver*>(tracker)
->DidCommitProvisionalLoad(true, ui::PAGE_TRANSITION_LINK);
}
void ClearField(FormFieldData* field) {
field->unique_renderer_id = std::numeric_limits<uint32_t>::max();
field->value.clear();
}
FakeMojoPasswordManagerDriver fake_driver_;
testing::NiceMock<FakePasswordGenerationDriver> fake_pw_client_;
base::string16 username1_;
base::string16 username2_;
base::string16 username3_;
base::string16 password1_;
base::string16 password2_;
base::string16 password3_;
base::string16 alternate_username3_;
PasswordFormFillData fill_data_;
WebInputElement username_element_;
WebInputElement password_element_;
base::test::ScopedFeatureList scoped_feature_list_;
protected:
base::HistogramTester histogram_tester_;
private:
DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgentTest);
};
// Tests that the password login is autocompleted as expected when the browser
// sends back the password info.
TEST_F(PasswordAutofillAgentTest, InitialAutocomplete) {
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// The username and password should have been autocompleted.
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
CheckFirstFillingResult(FillingResult::kSuccess);
}
// Tests that we correctly fill forms having an empty 'action' attribute.
TEST_F(PasswordAutofillAgentTest, InitialAutocompleteForEmptyAction) {
const char kEmptyActionFormHTML[] =
"<FORM name='LoginTestForm'>"
" <INPUT type='text' id='username'/>"
" <INPUT type='password' id='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
LoadHTML(kEmptyActionFormHTML);
// Retrieve the input elements so the test can access them.
UpdateUsernameAndPasswordElements();
// Set the expected form origin and action URLs.
UpdateOriginForHTML(kEmptyActionFormHTML);
fill_data_.action = GURL();
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// The username and password should have been autocompleted.
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
}
// Tests that if a password is marked as readonly, neither field is autofilled
// on page load.
TEST_F(PasswordAutofillAgentTest, NoInitialAutocompleteForReadOnlyPassword) {
SetElementReadOnly(password_element_, true);
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
CheckFirstFillingResult(FillingResult::kPasswordElementIsNotAutocompleteable);
}
// Can still fill a password field if the username is set to a value that
// matches.
TEST_F(PasswordAutofillAgentTest,
AutocompletePasswordForReadonlyUsernameMatched) {
username_element_.SetValue(WebString::FromUTF16(username3_));
SetElementReadOnly(username_element_, true);
// Filled even though username is not the preferred match.
SimulateOnFillPasswordForm(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(UTF16ToUTF8(username3_), false,
UTF16ToUTF8(password3_), true);
CheckFirstFillingResult(FillingResult::kSuccess);
}
// Fill username and password fields when username field contains a prefilled
// value that matches the list of known possible prefilled values usually used
// as placeholders.
TEST_F(PasswordAutofillAgentTest, AutocompleteForPrefilledUsernameValue) {
// Set the username element to a value from the prefilled values list.
// Comparison should be insensitive to leading and trailing whitespaces.
username_element_.SetValue(
WebString::FromUTF16(base::UTF8ToUTF16(" User Name ")));
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// The username and password should both have suggested values.
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
// Simulate a user click so that the password field's real value is filled.
SimulateElementClick(kUsernameName);
// The username and password should have been autocompleted.
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
histogram_tester_.ExpectUniqueSample(
"PasswordManager.PrefilledUsernameFillOutcome",
PrefilledUsernameFillOutcome::kPrefilledPlaceholderUsernameOverridden, 1);
CheckFirstFillingResult(FillingResult::kSuccess);
}
// Tests that if filling is invoked twice for the same autofill agent the
// prefilled username and first filling metrics are only logged once.
TEST_F(PasswordAutofillAgentTest, MetricsOnlyLoggedOnce) {
// Set the username element to a value from the prefilled values list.
// Comparison should be insensitive to leading and trailing whitespaces.
username_element_.SetValue(
WebString::FromUTF16(base::UTF8ToUTF16(" User Name ")));
// Simulate the browser sending back the login info multiple tims.
// This triggers the autocomplete.
SimulateOnFillPasswordForm(fill_data_);
SimulateOnFillPasswordForm(fill_data_);
histogram_tester_.ExpectUniqueSample(
"PasswordManager.PrefilledUsernameFillOutcome",
PrefilledUsernameFillOutcome::kPrefilledPlaceholderUsernameOverridden, 1);
CheckFirstFillingResult(FillingResult::kSuccess);
}
// Fill a password field if the stored username is a prefix of username in
// read-only field.
TEST_F(PasswordAutofillAgentTest,
AutocompletePasswordForReadonlyUsernamePrefixMatched) {
base::string16 username_at = username3_ + base::UTF8ToUTF16("@example.com");
username_element_.SetValue(WebString::FromUTF16(username_at));
SetElementReadOnly(username_element_, true);
// Filled even though the username in the form is only a proper prefix of the
// stored username.
SimulateOnFillPasswordForm(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(UTF16ToUTF8(username_at), false,
UTF16ToUTF8(password3_), true);
}
// Credentials are sent to the renderer even for sign-up forms as these may be
// eligible for filling via manual fall back. In this case, the username_field
// and password_field are not set. This test verifies that no failures are
// recorded in PasswordManager.FirstRendererFillingResult.
TEST_F(PasswordAutofillAgentTest, NoFillingOnSignupForm_NoMetrics) {
LoadHTML(kSignupFormHTML);
WebDocument document = GetMainFrame()->GetDocument();
WebElement element =
document.GetElementById(WebString::FromUTF8("random_info"));
ASSERT_FALSE(element.IsNull());
username_element_ = element.To<WebInputElement>();
fill_data_.has_renderer_ids = true;
fill_data_.username_field.unique_renderer_id =
FormFieldData::kNotSetFormControlRendererId;
fill_data_.password_field.unique_renderer_id =
FormFieldData::kNotSetFormControlRendererId;
WebFormElement form_element =
document.GetElementById("LoginTestForm").To<WebFormElement>();
fill_data_.form_renderer_id = form_element.UniqueRendererFormId();
SimulateOnFillPasswordForm(fill_data_);
histogram_tester_.ExpectTotalCount(
"PasswordManager.FirstRendererFillingResult", 0);
}
// Do not fill a password field if the stored username is a prefix without @
// of username in read-only field.
TEST_F(PasswordAutofillAgentTest,
DontAutocompletePasswordForReadonlyUsernamePrefixMatched) {
base::string16 prefilled_username =
username3_ + base::UTF8ToUTF16("example.com");
username_element_.SetValue(WebString::FromUTF16(prefilled_username));
SetElementReadOnly(username_element_, true);
// Filled even though the username in the form is only a proper prefix of the
// stored username.
SimulateOnFillPasswordForm(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(UTF16ToUTF8(prefilled_username),
false, std::string(), false);
CheckFirstFillingResult(
FillingResult::kUsernamePrefilledWithIncompatibleValue);
}
// Do not fill a password field if the field isn't readonly despite the stored
// username is a prefix without @ of username in read-only field.
TEST_F(
PasswordAutofillAgentTest,
DontAutocompletePasswordForNotReadonlyUsernameFieldEvenWhenPrefixMatched) {
base::string16 prefilled_username =
username3_ + base::UTF8ToUTF16("@example.com");
username_element_.SetValue(WebString::FromUTF16(prefilled_username));
// Filled even though the username in the form is only a proper prefix of the
// stored username.
SimulateOnFillPasswordForm(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(UTF16ToUTF8(prefilled_username),
false, std::string(), false);
}
// If a username field is empty and readonly, don't autofill.
TEST_F(PasswordAutofillAgentTest,
NoAutocompletePasswordForReadonlyUsernameUnmatched) {
username_element_.SetValue(WebString::FromUTF8(""));
SetElementReadOnly(username_element_, true);
SimulateOnFillPasswordForm(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(std::string(), false,
std::string(), false);
CheckFirstFillingResult(FillingResult::kFoundNoPasswordForUsername);
}
// Tests that having a non-matching username precludes the autocomplete.
TEST_F(PasswordAutofillAgentTest, NoAutocompleteForFilledFieldUnmatched) {
username_element_.SetValue(WebString::FromUTF8("bogus"));
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// Neither field should be autocompleted.
CheckUsernameDOMStatePasswordSuggestedState("bogus", false, std::string(),
false);
CheckFirstFillingResult(
FillingResult::kUsernamePrefilledWithIncompatibleValue);
}
// Don't try to complete a prefilled value that is a partial match
// to a username if the prefilled value isn't on the list of known values
// used as placeholders.
TEST_F(PasswordAutofillAgentTest, NoPartialMatchForPrefilledUsername) {
username_element_.SetValue(WebString::FromUTF8("ali"));
SimulateOnFillPasswordForm(fill_data_);
CheckTextFieldsSuggestedState("", false, std::string(), false);
CheckUsernameDOMStatePasswordSuggestedState("ali", false, std::string(),
false);
histogram_tester_.ExpectUniqueSample(
"PasswordManager.PrefilledUsernameFillOutcome",
autofill::PrefilledUsernameFillOutcome::kPrefilledUsernameNotOverridden,
1);
}
TEST_F(PasswordAutofillAgentTest, InputWithNoForms) {
const char kNoFormInputs[] =
"<input type='text' id='username'/>"
"<input type='password' id='password'/>";
LoadHTML(kNoFormInputs);
SimulateOnFillPasswordForm(fill_data_);
// Input elements that aren't in a <form> won't autofill.
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
}
TEST_F(PasswordAutofillAgentTest, NoAutocompleteForTextFieldPasswords) {
const char kTextFieldPasswordFormHTML[] =
"<FORM name='LoginTestForm' action='http://www.bidule.com'>"
" <INPUT type='text' id='username'/>"
" <INPUT type='text' id='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
LoadHTML(kTextFieldPasswordFormHTML);
// Retrieve the input elements so the test can access them.
UpdateUsernameAndPasswordElements();
// Set the expected form origin URL.
UpdateOriginForHTML(kTextFieldPasswordFormHTML);
SimulateOnFillPasswordForm(fill_data_);
// Fields should still be empty.
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
CheckFirstFillingResult(FillingResult::kNoFillableElementsFound);
}
TEST_F(PasswordAutofillAgentTest, NoAutocompleteForPasswordFieldUsernames) {
const char kPasswordFieldUsernameFormHTML[] =
"<FORM name='LoginTestForm' action='http://www.bidule.com'>"
" <INPUT type='password' id='username'/>"
" <INPUT type='password' id='password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
LoadHTML(kPasswordFieldUsernameFormHTML);
// Retrieve the input elements so the test can access them.
UpdateUsernameAndPasswordElements();
// Set the expected form origin URL.
UpdateOriginForHTML(kPasswordFieldUsernameFormHTML);
SimulateOnFillPasswordForm(fill_data_);
// Fields should still be empty.
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
CheckFirstFillingResult(FillingResult::kNoFillableElementsFound);
}
// Tests that having a matching username does not preclude the autocomplete.
TEST_F(PasswordAutofillAgentTest, InitialAutocompleteForMatchingFilledField) {
username_element_.SetValue(WebString::FromUTF8(kAliceUsername));
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// The username and password should have been autocompleted.
CheckUsernameDOMStatePasswordSuggestedState(kAliceUsername, true,
kAlicePassword, true);
CheckFirstFillingResult(FillingResult::kSuccess);
}
TEST_F(PasswordAutofillAgentTest, PasswordNotClearedOnEdit) {
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// Simulate the user changing the username to some unknown username.
SimulateUsernameTyping("alicia");
// The password should not have been cleared.
CheckTextFieldsDOMState("alicia", false, kAlicePassword, true);
}
// Tests that lost focus does not trigger filling when |wait_for_username| is
// true.
TEST_F(PasswordAutofillAgentTest, WaitUsername) {
// Simulate the browser sending back the login info.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
// No auto-fill should have taken place.
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
SimulateUsernameTyping(kAliceUsername);
// Change focus in between to make sure blur events don't trigger filling.
SetFocused(password_element_);
SetFocused(username_element_);
// No autocomplete should happen when text is entered in the username.
CheckUsernameDOMStatePasswordSuggestedState(kAliceUsername, false,
std::string(), false);
CheckFirstFillingResult(FillingResult::kWaitForUsername);
}
TEST_F(PasswordAutofillAgentTest, IsWebElementVisibleTest) {
blink::WebVector<WebFormElement> forms1, forms2, forms3;
blink::WebVector<blink::WebFormControlElement> web_control_elements;
blink::WebLocalFrame* frame;
LoadHTML(kVisibleFormWithNoUsernameHTML);
frame = GetMainFrame();
frame->GetDocument().Forms(forms1);
ASSERT_EQ(1u, forms1.size());
forms1[0].GetFormControlElements(web_control_elements);
ASSERT_EQ(1u, web_control_elements.size());
EXPECT_TRUE(form_util::IsWebElementVisible(web_control_elements[0]));
LoadHTML(kNonVisibleFormHTML);
frame = GetMainFrame();
frame->GetDocument().Forms(forms2);
ASSERT_EQ(1u, forms2.size());
forms2[0].GetFormControlElements(web_control_elements);
ASSERT_EQ(1u, web_control_elements.size());
EXPECT_FALSE(form_util::IsWebElementVisible(web_control_elements[0]));
LoadHTML(kNonDisplayedFormHTML);
frame = GetMainFrame();
frame->GetDocument().Forms(forms3);
ASSERT_EQ(1u, forms3.size());
forms3[0].GetFormControlElements(web_control_elements);
ASSERT_EQ(1u, web_control_elements.size());
EXPECT_FALSE(form_util::IsWebElementVisible(web_control_elements[0]));
}
TEST_F(PasswordAutofillAgentTest,
SendPasswordFormsTest_VisibleFormWithNoUsername) {
fake_driver_.reset_password_forms_calls();
LoadHTML(kVisibleFormWithNoUsernameHTML);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_FALSE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_EmptyForm) {
base::RunLoop().RunUntilIdle();
fake_driver_.reset_password_forms_calls();
LoadHTML(kEmptyFormHTML);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_password_forms_parsed());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_TRUE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_FormWithoutPasswords) {
base::RunLoop().RunUntilIdle();
fake_driver_.reset_password_forms_calls();
LoadHTML(kFormWithoutPasswordsHTML);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_password_forms_parsed());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_TRUE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest,
SendPasswordFormsTest_UndetectedPasswordField) {
base::RunLoop().RunUntilIdle();
fake_driver_.reset_password_forms_calls();
LoadHTML(kFormWithoutPasswordsHTML);
// Emulate that a password field appears but we don't detect that.
std::string script =
"document.getElementById('random_field').type = 'password';";
ExecuteJavaScriptForTests(script.c_str());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_password_forms_parsed());
// When the user clicks on the field, a request to the store will be sent.
EXPECT_TRUE(SimulateElementClick("random_field"));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_NonDisplayedForm) {
fake_driver_.reset_password_forms_calls();
LoadHTML(kNonDisplayedFormHTML);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_TRUE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_NonVisibleForm) {
fake_driver_.reset_password_forms_calls();
LoadHTML(kNonVisibleFormHTML);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_TRUE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_PasswordChangeForm) {
fake_driver_.reset_password_forms_calls();
LoadHTML(kPasswordChangeFormHTML);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_FALSE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest,
SendPasswordFormsTest_CannotCreatePasswordForm) {
// This test checks that a request to the store is sent even if it is a credit
// card form.
fake_driver_.reset_password_forms_calls();
LoadHTML(kCreditCardFormHTML);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
ASSERT_TRUE(fake_driver_.password_forms_rendered());
EXPECT_FALSE(fake_driver_.password_forms_rendered()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_ReloadTab) {
// PasswordAutofillAgent::sent_request_to_store_ disables duplicate requests
// to the store. This test checks that new request will be sent if the frame
// has been reloaded.
fake_driver_.reset_password_forms_calls();
LoadHTML(kNonVisibleFormHTML);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
fake_driver_.reset_password_forms_calls();
std::string url_string = "data:text/html;charset=utf-8,";
url_string.append(kNonVisibleFormHTML);
Reload(GURL(url_string));
EXPECT_TRUE(fake_driver_.called_password_forms_parsed());
ASSERT_TRUE(fake_driver_.password_forms_parsed());
EXPECT_FALSE(fake_driver_.password_forms_parsed()->empty());
}
TEST_F(PasswordAutofillAgentTest, SendPasswordFormsTest_Redirection) {
base::RunLoop().RunUntilIdle();
fake_driver_.reset_password_forms_calls();
LoadHTML(kEmptyWebpage);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_password_forms_rendered());
fake_driver_.reset_password_forms_calls();
LoadHTML(kRedirectionWebpage);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_password_forms_rendered());
fake_driver_.reset_password_forms_calls();
LoadHTML(kSimpleWebpage);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
fake_driver_.reset_password_forms_calls();
LoadHTML(kWebpageWithDynamicContent);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_password_forms_rendered());
}
// Tests that a password will only be filled as a suggested and will not be
// accessible by the DOM until a user gesture has occurred.
TEST_F(PasswordAutofillAgentTest, GestureRequiredTest) {
// Trigger the initial autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// The username and password should have been autocompleted.
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
// However, it should only have completed with the suggested value, as tested
// above, and it should not have completed into the DOM accessible value for
// the password field.
CheckTextFieldsDOMState(std::string(), true, std::string(), true);
// Simulate a user click so that the password field's real value is filled.
SimulateElementClick(kUsernameName);
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
}
// Verfies that a DOM-activated UI event will not cause an autofill.
TEST_F(PasswordAutofillAgentTest, NoDOMActivationTest) {
// Trigger the initial autocomplete.
SimulateOnFillPasswordForm(fill_data_);
ExecuteJavaScriptForTests(kJavaScriptClick);
CheckTextFieldsDOMState("", true, "", true);
}
// Verifies that password autofill triggers events in JavaScript for forms that
// are filled on page load.
TEST_F(PasswordAutofillAgentTest,
PasswordAutofillTriggersOnChangeEventsOnLoad) {
std::vector<base::string16> username_event_checkers;
std::vector<base::string16> password_event_checkers;
std::string events_registration_script =
CreateScriptToRegisterListeners(kUsernameName, &username_event_checkers) +
CreateScriptToRegisterListeners(kPasswordName, &password_event_checkers);
std::string html = std::string(kFormHTML) + events_registration_script;
LoadHTML(html.c_str());
UpdateOriginForHTML(html);
UpdateUsernameAndPasswordElements();
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
SimulateOnFillPasswordForm(fill_data_);
// The username and password should have been autocompleted...
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
// ... but since there hasn't been a user gesture yet, the autocompleted
// username and password should only be visible to the user.
CheckTextFieldsDOMState(std::string(), true, std::string(), true);
// JavaScript events shouldn't have been triggered for the username and the
// password yet.
CheckIfEventsAreCalled(username_event_checkers, false);
CheckIfEventsAreCalled(password_event_checkers, false);
// Simulate a user click so that the password field's real value is filled.
SimulateElementClick(kUsernameName);
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
// Now, JavaScript events should have been triggered.
CheckIfEventsAreCalled(username_event_checkers, true);
CheckIfEventsAreCalled(password_event_checkers, true);
}
// Verifies that password autofill triggers events in JavaScript for forms that
// are filled after page load.
TEST_F(PasswordAutofillAgentTest,
PasswordAutofillTriggersOnChangeEventsWaitForUsername) {
std::vector<base::string16> event_checkers;
std::string events_registration_script =
CreateScriptToRegisterListeners(kUsernameName, &event_checkers) +
CreateScriptToRegisterListeners(kPasswordName, &event_checkers);
std::string html = std::string(kFormHTML) + events_registration_script;
LoadHTML(html.c_str());
UpdateOriginForHTML(html);
UpdateUsernameAndPasswordElements();
// Simulate the browser sending back the login info, it triggers the
// autocomplete.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
// The username and password should not yet have been autocompleted.
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
// Simulate a click just to force a user gesture, since the username value is
// set directly.
SimulateElementClick(kUsernameName);
// Simulate the user entering the first letter of their username and selecting
// the matching autofill from the dropdown.
SimulateUsernameTyping("a");
// Since the username element has focus, blur event will be not triggered.
base::Erase(event_checkers, ASCIIToUTF16("username_blur_event"));
SimulateSuggestionChoice(username_element_);
// The username and password should now have been autocompleted.
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
// JavaScript events should have been triggered both for the username and for
// the password.
CheckIfEventsAreCalled(event_checkers, true);
}
// Tests that |FillSuggestion| properly fills the username and password.
TEST_F(PasswordAutofillAgentTest, FillSuggestion) {
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
for (const auto& selected_element : {username_element_, password_element_}) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// If the password field is not autocompletable, it should not be affected.
SetElementReadOnly(password_element_, true);
EXPECT_FALSE(password_autofill_agent_->FillSuggestion(
selected_element, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
SetElementReadOnly(password_element_, false);
// After filling with the suggestion, both fields should be autocompleted.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
selected_element, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
int username_length = strlen(kAliceUsername);
CheckUsernameSelection(username_length, username_length);
// Try Filling with a suggestion with password different from the one that
// was initially sent to the renderer.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
selected_element, ASCIIToUTF16(kBobUsername),
ASCIIToUTF16(kCarolPassword)));
CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true);
username_length = strlen(kBobUsername);
CheckUsernameSelection(username_length, username_length);
ClearUsernameAndPasswordFields();
}
}
// Tests that |FillSuggestion| properly fills the username and password when the
// username field is created dynamically in JavaScript.
TEST_F(PasswordAutofillAgentTest, FillSuggestionWithDynamicUsernameField) {
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
constexpr const char* kAddUsernameToFormScript =
"var new_input = document.createElement('input');"
"new_input.setAttribute('type', 'text');"
"new_input.setAttribute('id', 'username');"
"password_field = document.getElementById('password');"
"password_field.parentNode.insertBefore(new_input, password_field);";
ExecuteJavaScriptForTests(kAddUsernameToFormScript);
UpdateUsernameAndPasswordElements();
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// After filling with the suggestion, both fields should be autocompleted.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
password_element_, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
CheckFirstFillingResult(FillingResult::kWaitForUsername);
}
// Tests that |FillSuggestion| doesn't change non-empty non-autofilled username
// when interacting with the password field.
TEST_F(PasswordAutofillAgentTest,
FillSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
username_element_.SetValue(WebString::FromUTF8("user1"));
// Simulate the browser sending the login info, but set |wait_for_username| to
// prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
// Neither field should have been autocompleted.
CheckTextFieldsDOMState("user1", false, std::string(), false);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, fake_driver_.called_show_manual_fallback_for_saving_count());
// Only password field should be autocompleted.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
password_element_, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
CheckTextFieldsDOMState("user1", false, kAlicePassword, true);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, fake_driver_.called_show_manual_fallback_for_saving_count());
// Try Filling with a different password. Only password should be changed.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
password_element_, ASCIIToUTF16(kBobUsername),
ASCIIToUTF16(kCarolPassword)));
CheckTextFieldsDOMState("user1", false, kCarolPassword, true);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, fake_driver_.called_show_manual_fallback_for_saving_count());
}
// Tests that |FillSuggestion| properly fills the password if the username field
// is read-only.
TEST_F(PasswordAutofillAgentTest, FillSuggestionIfUsernameReadOnly) {
// Simulate the browser sending the login info.
SetElementReadOnly(username_element_, true);
SimulateOnFillPasswordForm(fill_data_);
for (const auto& selected_element : {username_element_, password_element_}) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// Username field is not autocompletable, it should not be affected.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
selected_element, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
CheckTextFieldsDOMState(std::string(), false, kAlicePassword, true);
// Try Filling with a suggestion with password different from the one that
// was initially sent to the renderer.
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
selected_element, ASCIIToUTF16(kBobUsername),
ASCIIToUTF16(kCarolPassword)));
CheckTextFieldsDOMState(std::string(), false, kCarolPassword, true);
ClearUsernameAndPasswordFields();
}
}
// Tests that |PreviewSuggestion| properly previews the username and password.
TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) {
// Simulate the browser sending the login info, but set |wait_for_username| to
// prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
for (const auto& selected_element : {username_element_, password_element_}) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// If the password field is not autocompletable, it should not be affected.
SetElementReadOnly(password_element_, true);
EXPECT_FALSE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
SetElementReadOnly(password_element_, false);
// After selecting the suggestion, both fields should be previewed with
// suggested values.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
// Since the suggestion is previewed as a placeholder, there should be no
// selected text.
CheckUsernameSelection(0, 0);
// Try previewing with a password different from the one that was initially
// sent to the renderer.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kBobUsername, kCarolPassword));
CheckTextFieldsSuggestedState(kBobUsername, true, kCarolPassword, true);
// Since the suggestion is previewed as a placeholder, there should be no
// selected text.
CheckUsernameSelection(0, 0);
ClearUsernameAndPasswordFields();
}
}
// Tests that |PreviewSuggestion| doesn't change non-empty non-autofilled
// username when previewing autofills on interacting with the password field.
TEST_F(PasswordAutofillAgentTest,
PreviewSuggestionFromPasswordFieldWithUsernameManuallyFilled) {
username_element_.SetValue(WebString::FromUTF8("user1"));
// Simulate the browser sending the login info, but set |wait_for_username| to
// prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
// Neither field should have been autocompleted.
CheckTextFieldsDOMState("user1", false, std::string(), false);
// Only password field should be autocompleted.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
password_element_, kAliceUsername, kAlicePassword));
CheckTextFieldsSuggestedState(std::string(), false, kAlicePassword, true);
CheckTextFieldsDOMState("user1", false, std::string(), true);
// Try previewing with a different password. Only password should be changed.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
password_element_, kBobUsername, kCarolPassword));
CheckTextFieldsSuggestedState(std::string(), false, kCarolPassword, true);
CheckTextFieldsDOMState("user1", false, std::string(), true);
}
// Tests that |PreviewSuggestion| properly previews the password if username is
// read-only.
TEST_F(PasswordAutofillAgentTest, PreviewSuggestionIfUsernameReadOnly) {
// Simulate the browser sending the login info.
SetElementReadOnly(username_element_, true);
SimulateOnFillPasswordForm(fill_data_);
for (const auto& selected_element : {username_element_, password_element_}) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// Username field is not autocompletable, it should not be affected.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
// Password field must be autofilled.
CheckTextFieldsSuggestedState(std::string(), false, kAlicePassword, true);
// Try previewing with a password different from the one that was initially
// sent to the renderer.
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kBobUsername, kCarolPassword));
CheckTextFieldsSuggestedState(std::string(), false, kCarolPassword, true);
ClearUsernameAndPasswordFields();
}
}
// Tests that |PreviewSuggestion| properly sets the username selection range.
TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) {
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
for (const auto& selected_element : {username_element_, password_element_}) {
ResetFieldState(&username_element_, "ali", WebAutofillState::kPreviewed);
ResetFieldState(&password_element_);
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
CheckTextFieldsSuggestedState(kAliceUsername, true, kAlicePassword, true);
// The selection should be set after the third character.
CheckUsernameSelection(3, 3);
}
}
// Tests that |ClearPreview| properly clears previewed username and password
// with password being previously autofilled.
TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) {
ResetFieldState(&password_element_, "sec", WebAutofillState::kPreviewed);
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
CheckTextFieldsDOMState(std::string(), false, "sec", true);
for (const auto& selected_element : {username_element_, password_element_}) {
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(selected_element));
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
CheckTextFieldsDOMState(std::string(), false, "sec", true);
CheckUsernameSelection(0, 0);
}
}
// Tests that |ClearPreview| properly clears previewed username and password
// with username being previously autofilled.
TEST_F(PasswordAutofillAgentTest, ClearPreviewWithUsernameAutofilled) {
ResetFieldState(&username_element_, "ali", WebAutofillState::kPreviewed);
username_element_.SetSelectionRange(3, 3);
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
CheckTextFieldsDOMState("ali", true, std::string(), false);
for (const auto& selected_element : {username_element_, password_element_}) {
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(selected_element));
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
CheckTextFieldsDOMState("ali", true, std::string(), false);
CheckUsernameSelection(3, 3);
}
}
// Tests that |ClearPreview| properly clears previewed username and password
// with username and password being previously autofilled.
TEST_F(PasswordAutofillAgentTest,
ClearPreviewWithAutofilledUsernameAndPassword) {
ResetFieldState(&username_element_, "ali", WebAutofillState::kPreviewed);
ResetFieldState(&password_element_, "sec", WebAutofillState::kPreviewed);
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
CheckTextFieldsDOMState("ali", true, "sec", true);
for (const auto& selected_element : {username_element_, password_element_}) {
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(selected_element));
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
CheckTextFieldsDOMState("ali", true, "sec", true);
CheckUsernameSelection(3, 3);
}
}
// Tests that TryToShowTouchToFill() works correctly for fillable and
// non-fillable fields.
TEST_F(PasswordAutofillAgentTest, TryToShowTouchToFill) {
// Initially no fill data is available.
WebInputElement random_element = GetInputElementByID("random_field");
EXPECT_FALSE(
password_autofill_agent_->TryToShowTouchToFill(username_element_));
EXPECT_FALSE(
password_autofill_agent_->TryToShowTouchToFill(password_element_));
EXPECT_FALSE(password_autofill_agent_->TryToShowTouchToFill(random_element));
// This changes once fill data is simulated. |random_element| continue to
// have no fill data, though.
SimulateOnFillPasswordForm(fill_data_);
EXPECT_CALL(fake_driver_, ShowTouchToFill);
EXPECT_TRUE(
password_autofill_agent_->TryToShowTouchToFill(username_element_));
base::RunLoop().RunUntilIdle();
EXPECT_CALL(fake_driver_, ShowTouchToFill);
EXPECT_TRUE(
password_autofill_agent_->TryToShowTouchToFill(password_element_));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(password_autofill_agent_->TryToShowTouchToFill(random_element));
}
// Tests that |FillIntoFocusedField| doesn't fill read-only text fields.
TEST_F(PasswordAutofillAgentTest, FillIntoFocusedReadonlyTextField) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// If the field is readonly, it should not be affected.
SetElementReadOnly(username_element_, true);
SimulateElementClick(kUsernameName);
password_autofill_agent_->FillIntoFocusedField(
/*is_password=*/false, ASCIIToUTF16(kAliceUsername));
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
}
// Tests that |FillIntoFocusedField| properly fills user-provided credentials.
TEST_F(PasswordAutofillAgentTest, FillIntoFocusedWritableTextField) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// The same field should be filled if it is writable.
FocusElement(kUsernameName);
SetElementReadOnly(username_element_, false);
password_autofill_agent_->FillIntoFocusedField(
/*is_password=*/false, ASCIIToUTF16(kAliceUsername));
CheckTextFieldsDOMState(kAliceUsername, true, std::string(), false);
CheckUsernameSelection(strlen(kAliceUsername), strlen(kAliceUsername));
}
// Tests that |FillIntoFocusedField| doesn't fill passwords in userfields.
TEST_F(PasswordAutofillAgentTest, FillIntoFocusedFieldOnlyIntoPasswordFields) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// Filling a password into a username field doesn't work.
SimulateElementClick(kUsernameName);
password_autofill_agent_->FillIntoFocusedField(
/*is_password=*/true, ASCIIToUTF16(kAlicePassword));
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// When a password field is focus, the filling works.
SimulateElementClick(kPasswordName);
password_autofill_agent_->FillIntoFocusedField(
/*is_password=*/true, ASCIIToUTF16(kAlicePassword));
CheckTextFieldsDOMState(std::string(), false, kAlicePassword, true);
}
// Tests that |FillIntoFocusedField| fills last focused, not last clicked field.
TEST_F(PasswordAutofillAgentTest, FillIntoFocusedFieldForNonClickFocus) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
// Click the username but shift the focus without click to the password.
SimulateElementClick(kUsernameName);
FocusElement(kPasswordName);
// The completion should now affect ONLY the password field. Don't fill a
// password so the error on failure shows where the filling happened.
// (see FillIntoFocusedFieldOnlyIntoPasswordFields).
password_autofill_agent_->FillIntoFocusedField(
/*is_password=*/false, ASCIIToUTF16("TextToFill"));
CheckTextFieldsDOMState(std::string(), false, "TextToFill", true);
}
// Tests that |ClearPreview| properly clears previewed username and password
// with neither username nor password being previously autofilled.
TEST_F(PasswordAutofillAgentTest,
ClearPreviewWithNotAutofilledUsernameAndPassword) {
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
for (const auto& selected_element : {username_element_, password_element_}) {
EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
selected_element, kAliceUsername, kAlicePassword));
EXPECT_TRUE(
password_autofill_agent_->DidClearAutofillSelection(selected_element));
EXPECT_TRUE(username_element_.SuggestedValue().IsEmpty());
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
CheckUsernameSelection(0, 0);
}
}
// Tests that logging is off by default.
TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) {
SendVisiblePasswordForms();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_record_save_progress());
}
// Test that logging can be turned on by a message.
TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_Activated) {
// Turn the logging on.
password_autofill_agent_->SetLoggingState(true);
SendVisiblePasswordForms();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(fake_driver_.called_record_save_progress());
}
// Test that logging can be turned off by a message.
TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_Deactivated) {
// Turn the logging on and then off.
password_autofill_agent_->SetLoggingState(true);
password_autofill_agent_->SetLoggingState(false);
SendVisiblePasswordForms();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(fake_driver_.called_record_save_progress());
}
// Tests that one user click on a username field is sufficient to bring up a
// credential suggestion popup, and the user can autocomplete the password by
// selecting the credential from the popup.
TEST_F(PasswordAutofillAgentTest, ClickAndSelect) {
// SimulateElementClick() is called so that a user gesture is actually made
// and the password can be filled. However, SimulateElementClick() does not
// actually lead to the AutofillAgent's InputElementClicked() method being
// called, so SimulateSuggestionChoice has to manually call
// InputElementClicked().
ClearUsernameAndPasswordFields();
SimulateOnFillPasswordForm(fill_data_);
SimulateElementClick(kUsernameName);
SimulateSuggestionChoice(username_element_);
CheckSuggestions(kAliceUsername, true);
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
}
// Tests the autosuggestions that are given when the element is clicked.
// Specifically, tests when the user clicks on the username element after page
// load and the element is autofilled, when the user clicks on an element that
// has a non-matching username, and when the user clicks on an element that's
// already been autofilled and they've already modified.
TEST_F(PasswordAutofillAgentTest, CredentialsOnClick) {
// Simulate the browser sending back the login info.
SimulateOnFillPasswordForm(fill_data_);
// Clear the text fields to start fresh.
ClearUsernameAndPasswordFields();
// Call SimulateElementClick() to produce a user gesture on the page so
// autofill will actually fill.
SimulateElementClick(kUsernameName);
// Simulate a user clicking on the username element. This should produce a
// message with all the usernames.
autofill_agent_->FormControlElementClicked(username_element_, false);
CheckSuggestions(std::string(), false);
// Now simulate a user typing in an unrecognized username and then
// clicking on the username element. This should also produce a message with
// all the usernames.
SimulateUsernameTyping("baz");
autofill_agent_->FormControlElementClicked(username_element_, true);
CheckSuggestions("baz", true);
ClearUsernameAndPasswordFields();
}
// Tests that there is an autosuggestion from the password manager when the
// user clicks on the password field when FillOnAccountSelect is enabled.
TEST_F(PasswordAutofillAgentTest,
FillOnAccountSelectOnlyNoCredentialsOnPasswordClick) {
SetFillOnAccountSelect();
// Simulate the browser sending back the login info.
SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
// Clear the text fields to start fresh.
ClearUsernameAndPasswordFields();
// Call SimulateElementClick() to produce a user gesture on the page so
// autofill will actually fill.
SimulateElementClick(kUsernameName);
// Simulate a user clicking on the password element. This should produce no
// message.
fake_driver_.reset_show_pw_suggestions();
autofill_agent_->FormControlElementClicked(password_element_, false);
EXPECT_TRUE(GetCalledShowPasswordSuggestions());
}
// Tests the autosuggestions that are given when a password element is clicked,
// the username element is not editable, and FillOnAccountSelect is enabled.
// Specifically, tests when the user clicks on the password element after page
// load, and the corresponding username element is readonly (and thus
// uneditable), that the credentials for the already-filled username are
// suggested.
TEST_F(PasswordAutofillAgentTest,
FillOnAccountSelectOnlyCredentialsOnPasswordClick) {
SetFillOnAccountSelect();
// Simulate the browser sending back the login info.
SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
// Clear the text fields to start fresh.
ClearUsernameAndPasswordFields();
// Simulate the page loading with a prefilled username element that is
// uneditable.
username_element_.SetValue("alicia");
SetElementReadOnly(username_element_, true);
// Call SimulateElementClick() to produce a user gesture on the page so
// autofill will actually fill.
SimulateElementClick(kUsernameName);
// Simulate a user clicking on the password element. This should produce a
// dropdown with suggestion of all available usernames and so username
// filter will be the empty string.
autofill_agent_->FormControlElementClicked(password_element_, false);
CheckSuggestions("", false);
}
// Tests that there is an autosuggestion from the password manager when the
// user clicks on the password field.
TEST_F(PasswordAutofillAgentTest, NoCredentialsOnPasswordClick) {
// Simulate the browser sending back the login info.
SimulateOnFillPasswordForm(fill_data_);
// Clear the text fields to start fresh.
ClearUsernameAndPasswordFields();
// Call SimulateElementClick() to produce a user gesture on the page so
// autofill will actually fill.
SimulateElementClick(kUsernameName);
// Simulate a user clicking on the password element. This should produce no
// message.
fake_driver_.reset_show_pw_suggestions();
autofill_agent_->FormControlElementClicked(password_element_, false);
EXPECT_TRUE(GetCalledShowPasswordSuggestions());
}
// The user types in a username and a password, but then just before sending
// the form off, a script clears them. This test checks that
// PasswordAutofillAgent can still remember the username and the password
// typed by the user.
TEST_F(PasswordAutofillAgentTest,
RememberLastNonEmptyUsernameAndPasswordOnSubmit_ScriptCleared) {
LoadHTML(kSignupFormHTML);
WebInputElement username_element = GetInputElementByID("random_info");
ASSERT_FALSE(username_element.IsNull());
SimulateUserInputChangeForElement(&username_element, "username");
WebInputElement new_password_element = GetInputElementByID("new_password");
ASSERT_FALSE(new_password_element.IsNull());
SimulateUserInputChangeForElement(&new_password_element, "random");
WebInputElement confirmation_password_element =
GetInputElementByID("confirm_password");
ASSERT_FALSE(confirmation_password_element.IsNull());
SimulateUserInputChangeForElement(&confirmation_password_element, "random");
// Simulate that the username and the password values were cleared by the
// site's JavaScript before submit.
username_element.SetValue(WebString());
new_password_element.SetValue(WebString());
confirmation_password_element.SetValue(WebString());
// Submit form.
FormTracker* tracker = autofill_agent_->form_tracker_for_testing();
static_cast<content::RenderFrameObserver*>(tracker)->WillSubmitForm(
username_element.Form());
// Observe that the PasswordAutofillAgent still remembered the last non-empty
// username and password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords("username", "", "random");
// Also check that |*_element| fields are correct.
const autofill::PasswordForm& form =
*(fake_driver_.password_form_submitted());
EXPECT_EQ(ASCIIToUTF16("random_info"), form.username_element);
EXPECT_TRUE(form.password_element.empty());
EXPECT_EQ(ASCIIToUTF16("new_password"), form.new_password_element);
EXPECT_EQ(ASCIIToUTF16("confirm_password"),
form.confirmation_password_element);
}
// Similar to RememberLastNonEmptyPasswordOnSubmit_ScriptCleared, but this time
// it's the user who clears the username and the password. This test checks
// that in that case, the last non-empty username and password are not
// remembered.
TEST_F(PasswordAutofillAgentTest,
RememberLastNonEmptyUsernameAndPasswordOnSubmit_UserCleared) {
SimulateUsernameTyping("temp");
SimulatePasswordTyping("random");
// Simulate that the user actually cleared the username and password again.
SimulateUsernameTyping("");
SimulatePasswordTyping("");
SubmitForm();
// Observe that the PasswordAutofillAgent respects the user having cleared the
// password.
ExpectFormSubmittedWithUsernameAndPasswords("", "", "");
}
// Similar to RememberLastNonEmptyPasswordOnSubmit_ScriptCleared, but uses the
// new password instead of the current password.
TEST_F(PasswordAutofillAgentTest,
RememberLastNonEmptyUsernameAndPasswordOnSubmit_New) {
const char kNewPasswordFormHTML[] =
"<FORM name='LoginTestForm' action='http://www.bidule.com'>"
" <INPUT type='text' id='username' autocomplete='username'/>"
" <INPUT type='password' id='password' autocomplete='new-password'/>"
" <INPUT type='submit' value='Login'/>"
"</FORM>";
LoadHTML(kNewPasswordFormHTML);
UpdateUsernameAndPasswordElements();
SimulateUsernameTyping("temp");
SimulatePasswordTyping("random");
// Simulate that the username and the password value was cleared by
// the site's JavaScript before submit.
username_element_.SetValue(WebString());
password_element_.SetValue(WebString());
SubmitForm();
// Observe that the PasswordAutofillAgent still remembered the last non-empty
// password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords("temp", "", "random");
}
// The user first accepts a suggestion, but then overwrites the password. This
// test checks that the overwritten password is not reverted back.
TEST_F(PasswordAutofillAgentTest,
NoopEditingDoesNotOverwriteManuallyEditedPassword) {
fill_data_.wait_for_username = true;
SimulateUsernameTyping(kAliceUsername);
SimulateOnFillPasswordForm(fill_data_);
SimulateSuggestionChoice(username_element_);
const std::string old_username(username_element_.Value().Utf8());
const std::string old_password(password_element_.Value().Utf8());
const std::string new_password(old_password + "modify");
// The user changes the password.
SimulatePasswordTyping(new_password);
// Change focus in between to make sure blur events don't trigger filling.
SetFocused(password_element_);
SetFocused(username_element_);
// The password should have stayed as the user changed it.
// The username should not be autofilled, because it was typed by the user.
CheckTextFieldsDOMState(old_username, false, new_password, false);
// The password should not have a suggested value.
CheckUsernameDOMStatePasswordSuggestedState(old_username, false,
std::string(), false);
}
// The user types the username, then accepts a suggestion. This test checks
// that autofilling does not rewrite the username, if the value is already
// there.
TEST_F(PasswordAutofillAgentTest, AcceptingSuggestionDoesntRewriteUsername) {
fill_data_.wait_for_username = true;
SimulateUsernameTyping(kAliceUsername);
SimulateOnFillPasswordForm(fill_data_);
SimulateSuggestionChoice(username_element_);
const std::string username(username_element_.Value().Utf8());
const std::string password(password_element_.Value().Utf8());
// The password was autofilled. The username was not.
CheckTextFieldsDOMState(username, false, password, true);
}
// The user types in a username and a password, but then just before sending
// the form off, a script changes them. This test checks that
// PasswordAutofillAgent can still remember the username and the password
// typed by the user.
TEST_F(PasswordAutofillAgentTest,
RememberLastTypedUsernameAndPasswordOnSubmit_ScriptChanged) {
SimulateUsernameTyping("temp");
SimulatePasswordTyping("random");
// Simulate that the username and the password value was changed by the
// site's JavaScript before submit.
username_element_.SetValue(WebString("new username"));
password_element_.SetValue(WebString("new password"));
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent still remembered the last typed
// username and password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", "");
}
TEST_F(PasswordAutofillAgentTest, RememberFieldPropertiesOnSubmit) {
SimulateUsernameTyping("temp");
SimulatePasswordTyping("random");
// Simulate that the username and the password value was changed by the
// site's JavaScript before submit.
username_element_.SetValue(WebString("new username"));
password_element_.SetValue(WebString("new password"));
SaveAndSubmitForm();
std::map<base::string16, FieldPropertiesMask> expected_properties_masks;
expected_properties_masks[ASCIIToUTF16("random_field")] =
FieldPropertiesFlags::HAD_FOCUS;
expected_properties_masks[ASCIIToUTF16("username")] =
FieldPropertiesFlags::USER_TYPED | FieldPropertiesFlags::HAD_FOCUS;
expected_properties_masks[ASCIIToUTF16("password")] =
FieldPropertiesFlags::USER_TYPED | FieldPropertiesFlags::HAD_FOCUS;
ExpectFieldPropertiesMasks(PasswordFormSubmitted, expected_properties_masks);
}
TEST_F(PasswordAutofillAgentTest,
RememberFieldPropertiesOnSameDocumentNavigation) {
LoadHTML(kNoFormHTML);
UpdateUsernameAndPasswordElements();
SimulateUsernameTyping("Bob");
SimulatePasswordTyping("mypassword");
std::string hide_elements =
"var password = document.getElementById('password');"
"password.style = 'display:none';"
"var username = document.getElementById('username');"
"username.style = 'display:none';";
ExecuteJavaScriptForTests(hide_elements.c_str());
FireAjaxSucceeded();
std::map<base::string16, FieldPropertiesMask> expected_properties_masks;
expected_properties_masks[ASCIIToUTF16("username")] =
FieldPropertiesFlags::USER_TYPED | FieldPropertiesFlags::HAD_FOCUS;
expected_properties_masks[ASCIIToUTF16("password")] =
FieldPropertiesFlags::USER_TYPED | FieldPropertiesFlags::HAD_FOCUS;
ExpectFieldPropertiesMasks(PasswordFormSameDocumentNavigation,
expected_properties_masks);
}
TEST_F(PasswordAutofillAgentTest,
RememberFieldPropertiesOnSameDocumentNavigation_2) {
LoadHTML(kNoFormHTML);
UpdateUsernameAndPasswordElements();
SimulateUsernameTyping("Bob");
SimulatePasswordTyping("mypassword");
FireAjaxSucceeded();
std::string hide_elements =
"var password = document.getElementById('password');"
"password.style = 'display:none';"
"var username = document.getElementById('username');"
"username.style = 'display:none';";
ExecuteJavaScriptForTests(hide_elements.c_str());
base::RunLoop().RunUntilIdle();
std::map<base::string16, FieldPropertiesMask> expected_properties_masks;
expected_properties_masks[ASCIIToUTF16("username")] =
FieldPropertiesFlags::USER_TYPED | FieldPropertiesFlags::HAD_FOCUS;
expected_properties_masks[ASCIIToUTF16("password")] =
FieldPropertiesFlags::USER_TYPED | FieldPropertiesFlags::HAD_FOCUS;
ExpectFieldPropertiesMasks(PasswordFormSameDocumentNavigation,
expected_properties_masks);
}
// The username/password is autofilled by password manager then just before
// sending the form off, a script changes them. This test checks that
// PasswordAutofillAgent can still get the username and the password autofilled.
TEST_F(PasswordAutofillAgentTest,
RememberLastAutofilledUsernameAndPasswordOnSubmit_ScriptChanged) {
SimulateOnFillPasswordForm(fill_data_);
// Simulate that the username and the password value was changed by the
// site's JavaScript before submit.
username_element_.SetValue(WebString("new username"));
password_element_.SetValue(WebString("new password"));
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent still remembered the autofilled
// username and password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords(kAliceUsername, kAlicePassword,
"");
}
// The username/password is autofilled by password manager then user types in a
// username and a password. Then just before sending the form off, a script
// changes them. This test checks that PasswordAutofillAgent can still remember
// the username and the password typed by the user.
TEST_F(
PasswordAutofillAgentTest,
RememberLastTypedAfterAutofilledUsernameAndPasswordOnSubmit_ScriptChanged) {
SimulateOnFillPasswordForm(fill_data_);
SimulateUsernameTyping("temp");
SimulatePasswordTyping("random");
// Simulate that the username and the password value was changed by the
// site's JavaScript before submit.
username_element_.SetValue(WebString("new username"));
password_element_.SetValue(WebString("new password"));
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent still remembered the last typed
// username and password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", "");
}
// The user starts typing username then it is autofilled.
// PasswordAutofillAgent should remember the username that was autofilled,
// not last typed.
TEST_F(PasswordAutofillAgentTest, RememberAutofilledUsername) {
SimulateUsernameTyping("Te");
// Simulate that the username was changed by autofilling.
username_element_.SetValue(WebString("temp"));
SimulatePasswordTyping("random");
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent still remembered the last typed
// username and password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", "");
}
// The user starts typing username then javascript suggests to select another
// username that was generated based on typed field value (e.g. surname field).
// PasswordAutofillAgent should remember the username that was selected,
// not last typed.
TEST_F(PasswordAutofillAgentTest,
RememberUsernameGeneratedBasingOnTypedFields) {
SimulateUsernameTyping("Temp");
SimulatePasswordTyping("random");
// Suppose that "random_field" contains surname.
WebInputElement surname_element = GetInputElementByID("random_field");
SimulateUserInputChangeForElement(&surname_element, "Smith");
// Simulate that the user selected username that was generated by script.
username_element_.SetValue(WebString("foo.smith"));
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent still remembered the last typed
// username and password and sent that to the browser.
ExpectFormSubmittedWithUsernameAndPasswords("foo.smith", "random", "");
}
TEST_F(PasswordAutofillAgentTest, FormFillDataMustHaveUsername) {
ClearUsernameAndPasswordFields();
PasswordFormFillData no_username_fill_data = fill_data_;
no_username_fill_data.username_field.name = base::string16();
SimulateOnFillPasswordForm(no_username_fill_data);
// The username and password should not have been autocompleted.
CheckTextFieldsSuggestedState("", false, "", false);
}
TEST_F(PasswordAutofillAgentTest, FillOnAccountSelectOnly) {
SetFillOnAccountSelect();
ClearUsernameAndPasswordFields();
// Simulate the browser sending back the login info for an initial page load.
SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
CheckTextFieldsSuggestedState(std::string(), false, std::string(), false);
CheckSuggestions(std::string(), true);
}
TEST_F(PasswordAutofillAgentTest, FillOnAccountSelectOnlyReadonlyUsername) {
SetFillOnAccountSelect();
ClearUsernameAndPasswordFields();
username_element_.SetValue("alice");
SetElementReadOnly(username_element_, true);
// Simulate the browser sending back the login info for an initial page load.
SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(std::string("alice"), false,
std::string(), false);
}
TEST_F(PasswordAutofillAgentTest,
FillOnAccountSelectOnlyReadonlyNotPreferredUsername) {
SetFillOnAccountSelect();
ClearUsernameAndPasswordFields();
username_element_.SetValue("Carol");
SetElementReadOnly(username_element_, true);
// Simulate the browser sending back the login info for an initial page load.
SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
CheckUsernameDOMStatePasswordSuggestedState(std::string("Carol"), false,
std::string(), false);
}
// If credentials contain username+password but the form contains only a
// password field, we don't autofill on page load.
TEST_F(PasswordAutofillAgentTest, DontFillFormWithNoUsername_WithRendererIds) {
// Load a form with no username and update test data.
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
UpdateRendererIDs();
SimulateOnFillPasswordForm(fill_data_);
// As the credential contains a username, but the form does not, the
// credential is not filled.
CheckFirstFillingResult(FillingResult::kFoundNoPasswordForUsername);
}
// If credentials contain username+password but the form contains only a
// password field, we don't autofill on page load.
TEST_F(PasswordAutofillAgentTest,
DontFillFormWithNoUsername_WithoutRendererIds) {
// Load a form with no username and update test data.
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
fill_data_.username_field.name.clear();
SimulateOnFillPasswordForm(fill_data_);
// As the credential contains a username, but the form does not, the
// credential is not filled.
CheckFirstFillingResult(FillingResult::kFoundNoPasswordForUsername);
}
TEST_F(PasswordAutofillAgentTest, FillOnAccountSelectOnlyNoUsername) {
SetFillOnAccountSelect();
// Load a form with no username and update test data.
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
fill_data_.username_field = FormFieldData();
UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
fill_data_.additional_logins.clear();
password_element_.SetValue("");
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
// Simulate the browser sending back the login info for an initial page load.
SimulateOnShowInitialPasswordAccountSuggestions(fill_data_);
EXPECT_TRUE(password_element_.SuggestedValue().IsEmpty());
EXPECT_FALSE(password_element_.IsAutofilled());
CheckSuggestions(std::string(), false);
}
TEST_F(PasswordAutofillAgentTest, ShowPopupOnEmptyPasswordField) {
// Load a form with no username and update test data.
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
fill_data_.username_field = FormFieldData();
UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
fill_data_.additional_logins.clear();
password_element_.SetValue("");
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
// Simulate the browser sending back the login info for an initial page load.
SimulateOnFillPasswordForm(fill_data_);
// Show popup suggesstion when the password field is empty.
password_element_.SetValue("");
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
SimulateSuggestionChoiceOfUsernameAndPassword(
password_element_, base::string16(), ASCIIToUTF16(kAlicePassword));
CheckSuggestions(std::string(), false);
EXPECT_EQ(ASCIIToUTF16(kAlicePassword), password_element_.Value().Utf16());
EXPECT_TRUE(password_element_.IsAutofilled());
}
TEST_F(PasswordAutofillAgentTest, ShowPopupOnAutofilledPasswordField) {
// Load a form with no username and update test data.
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
fill_data_.username_field = FormFieldData();
UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
fill_data_.additional_logins.clear();
password_element_.SetValue("");
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
// Simulate the browser sending back the login info for an initial page load.
SimulateOnFillPasswordForm(fill_data_);
// Show popup suggesstion when the password field is autofilled.
password_element_.SetValue("123");
password_element_.SetAutofillState(WebAutofillState::kAutofilled);
SimulateSuggestionChoiceOfUsernameAndPassword(
password_element_, base::string16(), ASCIIToUTF16(kAlicePassword));
CheckSuggestions(std::string(), false);
EXPECT_EQ(ASCIIToUTF16(kAlicePassword), password_element_.Value().Utf16());
EXPECT_TRUE(password_element_.IsAutofilled());
}
TEST_F(PasswordAutofillAgentTest, NotShowPopupPasswordField) {
// Load a form with no username and update test data.
LoadHTML(kVisibleFormWithNoUsernameHTML);
UpdateOnlyPasswordElement();
fill_data_.username_field = FormFieldData();
UpdateOriginForHTML(kVisibleFormWithNoUsernameHTML);
fill_data_.additional_logins.clear();
password_element_.SetValue("");
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
// Simulate the browser sending back the login info for an initial page load.
SimulateOnFillPasswordForm(fill_data_);
// Do not show popup suggesstion when the password field is not-empty and not
// autofilled.
password_element_.SetValue("123");
password_element_.SetAutofillState(WebAutofillState::kNotFilled);
SimulateSuggestionChoiceOfUsernameAndPassword(
password_element_, base::string16(), ASCIIToUTF16(kAlicePassword));
ASSERT_FALSE(GetCalledShowPasswordSuggestions());
}
// Tests with fill-on-account-select enabled that if the username element is
// read-only and filled with an unknown username, then the password field is not
// highlighted as autofillable (regression test for https://crbug.com/442564).
TEST_F(PasswordAutofillAgentTest,
FillOnAccountSelectOnlyReadonlyUnknownUsername) {
SetFillOnAccountSelect();
ClearUsernameAndPasswordFields();
username_element_.SetValue("foobar");
SetElementReadOnly(username_element_, true);
CheckUsernameDOMStatePasswordSuggestedState(std::string("foobar"), false,
std::string(), false);
}
// Test that the last plain text field before a password field is chosen as a
// username, in a form with 2 plain text fields without username predictions.
TEST_F(PasswordAutofillAgentTest, FindingUsernameWithoutAutofillPredictions) {
LoadHTML(kFormHTMLWithTwoTextFields);
UpdateUsernameAndPasswordElements();
WebInputElement display_name_element = GetInputElementByID(kDisplayName);
SimulateUsernameTyping("temp");
SimulateUserInputChangeForElement(&display_name_element, "User123");
SimulatePasswordTyping("random");
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent identifies the second field as
// username.
ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", "");
}
// Tests that field predictions are followed when identifying the username
// and password in a password form with two plain text fields.
TEST_F(PasswordAutofillAgentTest, FindingFieldsWithAutofillPredictions) {
LoadHTML(kFormHTMLWithTwoTextFields);
UpdateUsernameAndPasswordElements();
WebInputElement display_name_element = GetInputElementByID(kDisplayName);
SimulateUsernameTyping("temp");
SimulateUserInputChangeForElement(&display_name_element, "User123");
SimulatePasswordTyping("random");
// Find FormData for visible password form.
WebFormElement form_element = username_element_.Form();
FormData form_data;
ASSERT_TRUE(WebFormElementToFormData(
form_element, blink::WebFormControlElement(), nullptr,
form_util::EXTRACT_NONE, &form_data, nullptr));
// Simulate Autofill predictions: the first field is username, the third
// one is password.
autofill::FormsPredictionsMap predictions;
predictions[form_data][form_data.fields[0]] =
PasswordFormFieldPredictionType::kUsername;
predictions[form_data][form_data.fields[2]] =
PasswordFormFieldPredictionType::kNewPassword;
password_autofill_agent_->AutofillUsernameAndPasswordDataReceived(
predictions);
// The predictions should still match even if the form changes, as long
// as the particular elements don't change.
std::string add_field_to_form =
"var form = document.getElementById('LoginTestForm');"
"var new_input = document.createElement('input');"
"new_input.setAttribute('type', 'text');"
"new_input.setAttribute('id', 'other_field');"
"form.appendChild(new_input);";
ExecuteJavaScriptForTests(add_field_to_form.c_str());
SaveAndSubmitForm();
// Observe that the PasswordAutofillAgent identifies the first field as
// username.
// TODO(msramek): We should also test that adding another password field
// won't override the password field prediction either. However, the password
// field predictions are not taken into account yet.
ExpectFormSubmittedWithUsernameAndPasswords("User123", "random", "");
}
// The user types in a username and a password. Then JavaScript changes password
// field to readonly state before submit. PasswordAutofillAgent can correctly
// process readonly password field. This test models behaviour of gmail.com.
TEST_F(PasswordAutofillAgentTest, ReadonlyPasswordFieldOnSubmit) {
SimulateUsernameTyping("temp");
SimulatePasswordTyping("random");
// Simulate that JavaScript makes password field readonly.
SetElementReadOnly(password_element_, true);
SubmitForm();
// Observe that the PasswordAutofillAgent can correctly process submitted
// form.
ExpectFormSubmittedWithUsernameAndPasswords("temp", "random", "");
}
// Verify that typed passwords are saved correctly when autofill and generation
// both trigger. Regression test for https://crbug.com/493455
TEST_F(PasswordAutofillAgentTest, PasswordGenerationTriggered_TypedPassword) {
SimulateOnFillPasswordForm(fill_data_);
SetFoundFormEligibleForGeneration(
password_generation_, GetMainFrame()->GetDocument(),
"password" /* new_password_id */, nullptr /* confirm_password_id*/);
// Generation event is triggered due to focus events.
EXPECT_CALL(fake_pw_client_, GenerationElementLostFocus())
.Times(testing::AnyNumber());
SimulateUsernameTyping("NewGuy");
SimulatePasswordTyping("NewPassword");
SaveAndSubmitForm();
ExpectFormSubmittedWithUsernameAndPasswords("NewGuy", "NewPassword", "");
}
// Verify that generated passwords are saved correctly when autofill and
// generation both trigger. Regression test for https://crbug.com/493455.
TEST_F(PasswordAutofillAgentTest,
PasswordGenerationTriggered_GeneratedPassword) {
SimulateOnFillPasswordForm(fill_data_);
SetFoundFormEligibleForGeneration(
password_generation_, GetMainFrame()->GetDocument(),
"password" /* new_password_id */, nullptr /* confirm_password_id*/);
// Simulate the user clicks on a password field, that leads to showing
// generaiton pop-up. GeneratedPasswordAccepted can't be called without it.
SimulateElementClick(kPasswordName);
base::string16 password = ASCIIToUTF16("NewPass22");
EXPECT_CALL(fake_pw_client_,
PresaveGeneratedPassword(testing::Field(
&autofill::PasswordForm::password_value, password)));
password_generation_->GeneratedPasswordAccepted(password);
SaveAndSubmitForm();
ExpectFormSubmittedWithUsernameAndPasswords(kAliceUsername, "NewPass22", "");
}
TEST_F(PasswordAutofillAgentTest,
ResetPasswordGenerationWhenFieldIsAutofilled) {
// A user generates password.
SetFoundFormEligibleForGeneration(
password_generation_, GetMainFrame()->GetDocument(),
"password" /* new_password_id */, nullptr /* confirm_password_id*/);
// Simulate the user clicks on a password field, that leads to showing
// generaiton pop-up. GeneratedPasswordAccepted can't be called without it.
SimulateElementClick(kPasswordName);
base::string16 password = ASCIIToUTF16("NewPass22");
EXPECT_CALL(fake_pw_client_,
PresaveGeneratedPassword(testing::Field(
&autofill::PasswordForm::password_value, password)));
password_generation_->GeneratedPasswordAccepted(password);
// A user fills username and password, the generated value is gone.
EXPECT_CALL(fake_pw_client_, PasswordNoLongerGenerated(testing::_));
SimulateOnFillPasswordForm(fill_data_);
base::RunLoop().RunUntilIdle();
// The password field shoudln't reveal the value on focusing.
WebDocument document = GetMainFrame()->GetDocument();
WebElement element = document.GetElementById(WebString::FromUTF8("password"));
ASSERT_FALSE(element.IsNull());
WebInputElement password_element = element.To<WebInputElement>();
EXPECT_FALSE(password_element.ShouldRevealPassword());
EXPECT_TRUE(password_element.IsAutofilled());
SaveAndSubmitForm();
ExpectFormSubmittedWithUsernameAndPasswords(kAliceUsername, kAlicePassword,
"");
}
// If password generation is enabled for a field, password autofill should not
// show UI.
TEST_F(PasswordAutofillAgentTest, PasswordGenerationSupersedesAutofill) {
LoadHTML(kSignupFormHTML);
// Update password_element_;
WebDocument document = GetMainFrame()->GetDocument();
WebElement element =
document.GetElementById(WebString::FromUTF8("new_password"));
ASSERT_FALSE(element.IsNull());
password_element_ = element.To<WebInputElement>();
// Update fill_data_ for the new form and simulate filling. Pretend as if
// the password manager didn't detect a username field so it will try to
// show UI when the password field is focused.
fill_data_.wait_for_username = true;
fill_data_.username_field = FormFieldData();
fill_data_.password_field.name = ASCIIToUTF16("new_password");
UpdateOriginForHTML(kSignupFormHTML);
SimulateOnFillPasswordForm(fill_data_);
// Simulate generation triggering.
SetFoundFormEligibleForGeneration(
password_generation_, GetMainFrame()->GetDocument(),
"new_password" /* new_password_id */,
"confirm_password" /* confirm_password_id*/);
// Simulate the field being clicked to start typing. This should trigger
// generation but not password autofill.
SetFocused(password_element_);
EXPECT_CALL(fake_pw_client_, AutomaticGenerationAvailable(_));
SimulateElementClick("new_password");
base::RunLoop().RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&fake_pw_client_);
EXPECT_FALSE(GetCalledShowPasswordSuggestions());
// On destruction the state is updated.
EXPECT_CALL(fake_pw_client_, GenerationElementLostFocus())
.Times(testing::AnyNumber());
}
// Tests that a password change form is properly filled with the username and
// password.
TEST_F(PasswordAutofillAgentTest, FillSuggestionPasswordChangeForms) {
LoadHTML(kPasswordChangeFormHTML);
UpdateOriginForHTML(kPasswordChangeFormHTML);
UpdateUsernameAndPasswordElements();
// Simulate the browser sending the login info, but set |wait_for_username|
// to prevent the form from being immediately filled.
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
for (const auto& selected_element : {username_element_, password_element_}) {
// Neither field should be autocompleted.
CheckTextFieldsDOMState(std::string(), false, std::string(), false);
EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
selected_element, ASCIIToUTF16(kAliceUsername),
ASCIIToUTF16(kAlicePassword)));
CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
ClearUsernameAndPasswordFields();
}
}
// Tests that one user click on a username field is sufficient to bring up a
// credential suggestion popup on a change password form.
TEST_F(PasswordAutofillAgentTest,
SuggestionsOnUsernameFieldOfChangePasswordForm) {
LoadHTML(kPasswordChangeFormHTML);
UpdateOriginForHTML(kPasswordChangeFormHTML);
UpdateUsernameAndPasswordElements();
ClearUsernameAndPasswordFields();
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
// Simulate a user clicking on the username element. This should produce a
// message.
autofill_agent_->FormControlElementClicked(username_element_, true);
CheckSuggestions("", true);
}
// Tests that one user click on a password field is sufficient to bring up a
// credential suggestion popup on a change password form.
TEST_F(PasswordAutofillAgentTest,
SuggestionsOnPasswordFieldOfChangePasswordForm) {
LoadHTML(kPasswordChangeFormHTML);
UpdateOriginForHTML(kPasswordChangeFormHTML);
UpdateUsernameAndPasswordElements();
ClearUsernameAndPasswordFields();
fill_data_.wait_for_username = true;
SimulateOnFillPasswordForm(fill_data_);
// Simulate a user clicking on the password element. This should produce a
// message.
autofill_agent_->FormControlElementClicked(password_element_, true);
CheckSuggestions("", false);
}
// Tests that NOT_PASSWORD field predictions are followed so that no password
// form is submitted.
TEST_F(PasswordAutofillAgentTest, IgnoreNotPasswordFields) {
LoadHTML(kCreditCardFormHTML);
WebInputElement credit_card_owner_element =
GetInputElementByID(kCreditCardOwnerName);
WebInputElement credit_card_number_element =
GetInputElementByID(kCreditCardNumberName);
WebInputElement credit_card_verification_element =
GetInputElementByID(kCreditCardVerificationName);
SimulateUserInputChangeForElement(&credit_card_owner_element, "JohnSmith");
SimulateUserInputChangeForElement(&credit_card_number_element,
"1234123412341234");