Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/compiler_specific.h" | |
| 6 #include "base/message_loop/message_loop.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "components/autofill/core/browser/popup_item_ids.h" | |
| 9 #include "components/autofill/core/browser/test_autofill_driver.h" | |
| 10 #include "components/autofill/core/browser/test_autofill_manager_delegate.h" | |
| 11 #include "components/password_manager/core/browser/password_autofill_manager.h" | |
| 12 #include "components/password_manager/core/browser/password_manager_client.h" | |
| 13 #include "components/password_manager/core/browser/password_manager_driver.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "ui/gfx/geometry/rect_f.h" | |
| 17 | |
| 18 // The name of the username/password element in the form. | |
| 19 const char kUsernameName[] = "username"; | |
| 20 const char kInvalidUsername[] = "no-username"; | |
| 21 const char kPasswordName[] = "password"; | |
| 22 | |
| 23 const char kAliceUsername[] = "alice"; | |
| 24 const char kAlicePassword[] = "password"; | |
| 25 | |
| 26 using testing::_; | |
| 27 | |
| 28 namespace autofill { | |
| 29 class AutofillPopupDelegate; | |
| 30 } | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // TODO(dubroy): Implement a TestPasswordManagerDriver that can be shared by | |
|
Garrett Casto
2014/03/14 08:25:22
Want to file a bug for this? Having a canonical Te
Patrick Dubroy
2014/03/14 12:07:19
Done.
| |
| 35 // all the tests that need it. | |
| 36 class MockPasswordManagerDriver : public PasswordManagerDriver { | |
| 37 public: | |
| 38 MOCK_METHOD1(FillPasswordForm, void(const autofill::PasswordFormFillData&)); | |
| 39 MOCK_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); | |
| 40 MOCK_METHOD0(IsOffTheRecord, bool()); | |
| 41 MOCK_METHOD0(GetPasswordGenerationManager, PasswordGenerationManager*()); | |
| 42 MOCK_METHOD0(GetPasswordManager, PasswordManager*()); | |
| 43 MOCK_METHOD0(GetAutofillManager, autofill::AutofillManager*()); | |
| 44 MOCK_METHOD1(AllowPasswordGenerationForForm, void(autofill::PasswordForm*)); | |
| 45 MOCK_METHOD1(AccountCreationFormsFound, | |
| 46 void(const std::vector<autofill::FormData>&)); | |
| 47 MOCK_METHOD2(AcceptPasswordAutofillSuggestion, | |
| 48 void(const base::string16&, const base::string16&)); | |
| 49 MOCK_METHOD0(GetPasswordAutofillManager, PasswordAutofillManager*()); | |
| 50 }; | |
| 51 | |
| 52 class TestPasswordManagerClient : public PasswordManagerClient { | |
| 53 public: | |
| 54 virtual void PromptUserToSavePassword(PasswordFormManager* form_to_save) | |
| 55 OVERRIDE {} | |
| 56 virtual PasswordStore* GetPasswordStore() OVERRIDE { return NULL; } | |
| 57 virtual PrefService* GetPrefs() OVERRIDE { return NULL; } | |
| 58 virtual PasswordManagerDriver* GetDriver() OVERRIDE { return &driver_; } | |
| 59 virtual void AuthenticateAutofillAndFillForm( | |
| 60 scoped_ptr<autofill::PasswordFormFillData> fill_data) OVERRIDE {} | |
| 61 virtual bool IsPasswordSyncEnabled() OVERRIDE { return false; } | |
| 62 | |
| 63 MockPasswordManagerDriver* mock_driver() { return &driver_; } | |
| 64 | |
| 65 private: | |
| 66 MockPasswordManagerDriver driver_; | |
| 67 }; | |
| 68 | |
| 69 class MockAutofillManagerDelegate | |
| 70 : public autofill::TestAutofillManagerDelegate { | |
| 71 public: | |
| 72 MOCK_METHOD7(ShowAutofillPopup, | |
| 73 void(const gfx::RectF& element_bounds, | |
| 74 base::i18n::TextDirection text_direction, | |
| 75 const std::vector<base::string16>& values, | |
| 76 const std::vector<base::string16>& labels, | |
| 77 const std::vector<base::string16>& icons, | |
| 78 const std::vector<int>& identifiers, | |
| 79 base::WeakPtr<autofill::AutofillPopupDelegate> delegate)); | |
| 80 MOCK_METHOD0(HideAutofillPopup, void()); | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 class PasswordAutofillManagerTest : public testing::Test { | |
| 86 protected: | |
| 87 PasswordAutofillManagerTest() : | |
| 88 test_username_(base::ASCIIToUTF16(kAliceUsername)), | |
| 89 test_password_(base::ASCIIToUTF16(kAlicePassword)) | |
| 90 {} | |
| 91 | |
| 92 virtual void SetUp() OVERRIDE { | |
| 93 // Add a preferred login and an additional login to the FillData. | |
| 94 username_field_.name = base::ASCIIToUTF16(kUsernameName); | |
| 95 username_field_.value = test_username_; | |
| 96 fill_data_.basic_data.fields.push_back(username_field_); | |
| 97 | |
| 98 autofill::FormFieldData password_field; | |
| 99 password_field.name = base::ASCIIToUTF16(kPasswordName); | |
| 100 password_field.value = test_password_; | |
| 101 fill_data_.basic_data.fields.push_back(password_field); | |
| 102 } | |
| 103 | |
| 104 void InitializePasswordAutofillManager( | |
| 105 PasswordManagerClient* client, | |
| 106 autofill::AutofillManagerDelegate* autofill_manager_delegate) { | |
| 107 password_autofill_manager_.reset( | |
| 108 new PasswordAutofillManager(client, autofill_manager_delegate)); | |
| 109 password_autofill_manager_->AddPasswordFormMapping(username_field_, | |
| 110 fill_data_); | |
| 111 } | |
| 112 | |
| 113 protected: | |
| 114 scoped_ptr<PasswordAutofillManager> password_autofill_manager_; | |
| 115 | |
| 116 autofill::FormFieldData username_field_; | |
| 117 base::string16 test_username_; | |
| 118 base::string16 test_password_; | |
| 119 | |
| 120 private: | |
| 121 autofill::PasswordFormFillData fill_data_; | |
| 122 | |
| 123 // The TestAutofillDriver uses a SequencedWorkerPool which expects the | |
| 124 // existence of a MessageLoop. | |
| 125 base::MessageLoop message_loop_; | |
| 126 }; | |
| 127 | |
| 128 TEST_F(PasswordAutofillManagerTest, AcceptAutofillSuggestion) { | |
| 129 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); | |
| 130 InitializePasswordAutofillManager(client.get(), NULL); | |
| 131 | |
| 132 EXPECT_CALL(*client->mock_driver(), | |
| 133 AcceptPasswordAutofillSuggestion(test_username_, test_password_)); | |
| 134 EXPECT_TRUE(password_autofill_manager_->AcceptAutofillSuggestion( | |
| 135 username_field_, test_username_)); | |
| 136 testing::Mock::VerifyAndClearExpectations(client->mock_driver()); | |
| 137 | |
| 138 EXPECT_CALL(*client->mock_driver(), | |
| 139 AcceptPasswordAutofillSuggestion(_, _)).Times(0); | |
| 140 EXPECT_FALSE(password_autofill_manager_->AcceptAutofillSuggestion( | |
| 141 username_field_, base::ASCIIToUTF16(kInvalidUsername))); | |
| 142 | |
| 143 autofill::FormFieldData invalid_username_field; | |
| 144 invalid_username_field.name = base::ASCIIToUTF16(kInvalidUsername); | |
| 145 | |
| 146 EXPECT_FALSE(password_autofill_manager_->AcceptAutofillSuggestion( | |
| 147 invalid_username_field, test_username_)); | |
| 148 | |
| 149 password_autofill_manager_->Reset(); | |
| 150 EXPECT_FALSE(password_autofill_manager_->AcceptAutofillSuggestion( | |
| 151 username_field_, test_username_)); | |
| 152 } | |
| 153 | |
| 154 // Test that the popup is marked as visible after recieving password | |
| 155 // suggestions. | |
| 156 TEST_F(PasswordAutofillManagerTest, ExternalDelegatePasswordSuggestions) { | |
| 157 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); | |
| 158 scoped_ptr<MockAutofillManagerDelegate> delegate( | |
| 159 new MockAutofillManagerDelegate); | |
| 160 InitializePasswordAutofillManager(client.get(), delegate.get()); | |
| 161 | |
| 162 gfx::RectF element_bounds; | |
| 163 std::vector<base::string16> suggestions; | |
| 164 suggestions.push_back(test_username_); | |
| 165 std::vector<base::string16> realms; | |
| 166 realms.push_back(base::ASCIIToUTF16("http://foo.com/")); | |
| 167 | |
| 168 // The enums must be cast to ints to prevent compile errors on linux_rel. | |
| 169 EXPECT_CALL( | |
| 170 *delegate, | |
| 171 ShowAutofillPopup(_, _, _, _, _, | |
| 172 testing::ElementsAre( | |
| 173 autofill::POPUP_ITEM_ID_PASSWORD_ENTRY), | |
| 174 _)); | |
| 175 password_autofill_manager_->ShowPasswordSuggestions( | |
| 176 username_field_, element_bounds, suggestions, realms); | |
| 177 | |
| 178 // Accepting a suggestion should trigger a call to hide the popup. | |
| 179 EXPECT_CALL(*delegate, HideAutofillPopup()); | |
| 180 password_autofill_manager_->DidAcceptSuggestion( | |
| 181 suggestions[0], autofill::POPUP_ITEM_ID_PASSWORD_ENTRY); | |
| 182 } | |
| OLD | NEW |