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 |
| 35 // all the tests that need it (crbug.com/352566). |
| 36 class MockPasswordManagerDriver |
| 37 : public password_manager::PasswordManagerDriver { |
| 38 public: |
| 39 MOCK_METHOD1(FillPasswordForm, void(const autofill::PasswordFormFillData&)); |
| 40 MOCK_METHOD0(DidLastPageLoadEncounterSSLErrors, bool()); |
| 41 MOCK_METHOD0(IsOffTheRecord, bool()); |
| 42 MOCK_METHOD0(GetPasswordGenerationManager, |
| 43 password_manager::PasswordGenerationManager*()); |
| 44 MOCK_METHOD0(GetPasswordManager, password_manager::PasswordManager*()); |
| 45 MOCK_METHOD0(GetAutofillManager, autofill::AutofillManager*()); |
| 46 MOCK_METHOD1(AllowPasswordGenerationForForm, void(autofill::PasswordForm*)); |
| 47 MOCK_METHOD1(AccountCreationFormsFound, |
| 48 void(const std::vector<autofill::FormData>&)); |
| 49 MOCK_METHOD2(AcceptPasswordAutofillSuggestion, |
| 50 void(const base::string16&, const base::string16&)); |
| 51 MOCK_METHOD0(GetPasswordAutofillManager, |
| 52 password_manager::PasswordAutofillManager*()); |
| 53 }; |
| 54 |
| 55 class TestPasswordManagerClient |
| 56 : public password_manager::PasswordManagerClient { |
| 57 public: |
| 58 virtual void PromptUserToSavePassword( |
| 59 password_manager::PasswordFormManager* form_to_save) OVERRIDE {} |
| 60 virtual password_manager::PasswordStore* GetPasswordStore() OVERRIDE { |
| 61 return NULL; |
| 62 } |
| 63 virtual PrefService* GetPrefs() OVERRIDE { return NULL; } |
| 64 virtual password_manager::PasswordManagerDriver* GetDriver() OVERRIDE { |
| 65 return &driver_; |
| 66 } |
| 67 virtual void AuthenticateAutofillAndFillForm( |
| 68 scoped_ptr<autofill::PasswordFormFillData> fill_data) OVERRIDE {} |
| 69 virtual bool IsPasswordSyncEnabled() OVERRIDE { return false; } |
| 70 |
| 71 MockPasswordManagerDriver* mock_driver() { return &driver_; } |
| 72 |
| 73 private: |
| 74 MockPasswordManagerDriver driver_; |
| 75 }; |
| 76 |
| 77 class MockAutofillManagerDelegate |
| 78 : public autofill::TestAutofillManagerDelegate { |
| 79 public: |
| 80 MOCK_METHOD7(ShowAutofillPopup, |
| 81 void(const gfx::RectF& element_bounds, |
| 82 base::i18n::TextDirection text_direction, |
| 83 const std::vector<base::string16>& values, |
| 84 const std::vector<base::string16>& labels, |
| 85 const std::vector<base::string16>& icons, |
| 86 const std::vector<int>& identifiers, |
| 87 base::WeakPtr<autofill::AutofillPopupDelegate> delegate)); |
| 88 MOCK_METHOD0(HideAutofillPopup, void()); |
| 89 }; |
| 90 |
| 91 } // namespace |
| 92 |
| 93 class PasswordAutofillManagerTest : public testing::Test { |
| 94 protected: |
| 95 PasswordAutofillManagerTest() |
| 96 : test_username_(base::ASCIIToUTF16(kAliceUsername)), |
| 97 test_password_(base::ASCIIToUTF16(kAlicePassword)) {} |
| 98 |
| 99 virtual void SetUp() OVERRIDE { |
| 100 // Add a preferred login and an additional login to the FillData. |
| 101 username_field_.name = base::ASCIIToUTF16(kUsernameName); |
| 102 username_field_.value = test_username_; |
| 103 fill_data_.basic_data.fields.push_back(username_field_); |
| 104 |
| 105 autofill::FormFieldData password_field; |
| 106 password_field.name = base::ASCIIToUTF16(kPasswordName); |
| 107 password_field.value = test_password_; |
| 108 fill_data_.basic_data.fields.push_back(password_field); |
| 109 } |
| 110 |
| 111 void InitializePasswordAutofillManager( |
| 112 password_manager::PasswordManagerClient* client, |
| 113 autofill::AutofillManagerDelegate* autofill_manager_delegate) { |
| 114 password_autofill_manager_.reset( |
| 115 new password_manager::PasswordAutofillManager( |
| 116 client, autofill_manager_delegate)); |
| 117 password_autofill_manager_->OnAddPasswordFormMapping(username_field_, |
| 118 fill_data_); |
| 119 } |
| 120 |
| 121 protected: |
| 122 scoped_ptr<password_manager::PasswordAutofillManager> |
| 123 password_autofill_manager_; |
| 124 |
| 125 autofill::FormFieldData username_field_; |
| 126 base::string16 test_username_; |
| 127 base::string16 test_password_; |
| 128 |
| 129 private: |
| 130 autofill::PasswordFormFillData fill_data_; |
| 131 |
| 132 // The TestAutofillDriver uses a SequencedWorkerPool which expects the |
| 133 // existence of a MessageLoop. |
| 134 base::MessageLoop message_loop_; |
| 135 }; |
| 136 |
| 137 TEST_F(PasswordAutofillManagerTest, AcceptSuggestion) { |
| 138 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); |
| 139 InitializePasswordAutofillManager(client.get(), NULL); |
| 140 |
| 141 EXPECT_CALL(*client->mock_driver(), |
| 142 AcceptPasswordAutofillSuggestion(test_username_, test_password_)); |
| 143 EXPECT_TRUE(password_autofill_manager_->AcceptSuggestionForTest( |
| 144 username_field_, test_username_)); |
| 145 testing::Mock::VerifyAndClearExpectations(client->mock_driver()); |
| 146 |
| 147 EXPECT_CALL(*client->mock_driver(), |
| 148 AcceptPasswordAutofillSuggestion(_, _)).Times(0); |
| 149 EXPECT_FALSE(password_autofill_manager_->AcceptSuggestionForTest( |
| 150 username_field_, base::ASCIIToUTF16(kInvalidUsername))); |
| 151 |
| 152 autofill::FormFieldData invalid_username_field; |
| 153 invalid_username_field.name = base::ASCIIToUTF16(kInvalidUsername); |
| 154 |
| 155 EXPECT_FALSE(password_autofill_manager_->AcceptSuggestionForTest( |
| 156 invalid_username_field, test_username_)); |
| 157 |
| 158 password_autofill_manager_->Reset(); |
| 159 EXPECT_FALSE(password_autofill_manager_->AcceptSuggestionForTest( |
| 160 username_field_, test_username_)); |
| 161 } |
| 162 |
| 163 // Test that the popup is marked as visible after recieving password |
| 164 // suggestions. |
| 165 TEST_F(PasswordAutofillManagerTest, ExternalDelegatePasswordSuggestions) { |
| 166 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); |
| 167 scoped_ptr<MockAutofillManagerDelegate> delegate( |
| 168 new MockAutofillManagerDelegate); |
| 169 InitializePasswordAutofillManager(client.get(), delegate.get()); |
| 170 |
| 171 gfx::RectF element_bounds; |
| 172 std::vector<base::string16> suggestions; |
| 173 suggestions.push_back(test_username_); |
| 174 std::vector<base::string16> realms; |
| 175 realms.push_back(base::ASCIIToUTF16("http://foo.com/")); |
| 176 |
| 177 // The enums must be cast to ints to prevent compile errors on linux_rel. |
| 178 EXPECT_CALL( |
| 179 *delegate, |
| 180 ShowAutofillPopup(_, _, _, _, _, |
| 181 testing::ElementsAre( |
| 182 autofill::POPUP_ITEM_ID_PASSWORD_ENTRY), |
| 183 _)); |
| 184 password_autofill_manager_->OnShowPasswordSuggestions( |
| 185 username_field_, element_bounds, suggestions, realms); |
| 186 |
| 187 // Accepting a suggestion should trigger a call to hide the popup. |
| 188 EXPECT_CALL(*delegate, HideAutofillPopup()); |
| 189 password_autofill_manager_->DidAcceptSuggestion( |
| 190 suggestions[0], autofill::POPUP_ITEM_ID_PASSWORD_ENTRY); |
| 191 } |
OLD | NEW |