| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/macros.h" | |
| 6 #include "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/password_manager/mock_password_store_service.h" | 6 #include "chrome/browser/password_manager/mock_password_store_service.h" |
| 8 #include "chrome/browser/password_manager/password_store_factory.h" | 7 #include "chrome/browser/password_manager/password_store_factory.h" |
| 9 #include "chrome/browser/ui/passwords/password_manager_presenter.h" | 8 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 10 #include "chrome/browser/ui/passwords/password_ui_view.h" | 9 #include "chrome/browser/ui/passwords/password_ui_view.h" |
| 11 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| 12 #include "components/password_manager/core/browser/mock_password_store.h" | 11 #include "components/password_manager/core/browser/mock_password_store.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 using base::ASCIIToUTF16; | 15 using base::ASCIIToUTF16; |
| 17 using testing::Eq; | 16 using testing::Eq; |
| 18 using testing::Field; | |
| 19 using testing::Property; | 17 using testing::Property; |
| 20 | 18 |
| 21 class MockPasswordUIView : public PasswordUIView { | 19 class MockPasswordUIView : public PasswordUIView { |
| 22 public: | 20 public: |
| 23 explicit MockPasswordUIView(Profile* profile) | 21 explicit MockPasswordUIView(Profile* profile) |
| 24 : profile_(profile), password_manager_presenter_(this) { | 22 : profile_(profile), password_manager_presenter_(this) { |
| 25 password_manager_presenter_.Initialize(); | 23 password_manager_presenter_.Initialize(); |
| 26 } | 24 } |
| 27 virtual ~MockPasswordUIView() {} | 25 virtual ~MockPasswordUIView() {} |
| 28 virtual Profile* GetProfile() OVERRIDE; | 26 virtual Profile* GetProfile() OVERRIDE; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 | 50 |
| 53 class PasswordManagerPresenterTest : public testing::Test { | 51 class PasswordManagerPresenterTest : public testing::Test { |
| 54 protected: | 52 protected: |
| 55 PasswordManagerPresenterTest() {} | 53 PasswordManagerPresenterTest() {} |
| 56 | 54 |
| 57 virtual ~PasswordManagerPresenterTest() {} | 55 virtual ~PasswordManagerPresenterTest() {} |
| 58 virtual void SetUp() OVERRIDE { | 56 virtual void SetUp() OVERRIDE { |
| 59 PasswordStoreFactory::GetInstance()->SetTestingFactory( | 57 PasswordStoreFactory::GetInstance()->SetTestingFactory( |
| 60 &profile_, MockPasswordStoreService::Build); | 58 &profile_, MockPasswordStoreService::Build); |
| 61 mock_controller_.reset(new MockPasswordUIView(&profile_)); | 59 mock_controller_.reset(new MockPasswordUIView(&profile_)); |
| 62 mock_store_ = static_cast<password_manager::MockPasswordStore*>( | |
| 63 PasswordStoreFactory::GetForProfile(&profile_, | |
| 64 Profile::EXPLICIT_ACCESS).get()); | |
| 65 } | 60 } |
| 66 void AddPasswordEntry(const GURL& origin, | 61 void AddPasswordEntry(const GURL& origin, |
| 67 const std::string& user_name, | 62 const std::string& user_name, |
| 68 const std::string& password); | 63 const std::string& password); |
| 69 void AddPasswordException(const GURL& origin); | 64 void AddPasswordException(const GURL& origin); |
| 70 void UpdateLists(); | 65 void UpdateLists(); |
| 71 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } | 66 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } |
| 72 PasswordManagerPresenter* GetPasswordManagerPresenter() { | |
| 73 return mock_controller_->GetPasswordManagerPresenter(); | |
| 74 } | |
| 75 password_manager::MockPasswordStore* GetPasswordStore() { | |
| 76 return mock_store_.get(); | |
| 77 } | |
| 78 | 67 |
| 79 private: | 68 private: |
| 80 TestingProfile profile_; | 69 TestingProfile profile_; |
| 81 scoped_ptr<MockPasswordUIView> mock_controller_; | 70 scoped_ptr<MockPasswordUIView> mock_controller_; |
| 82 scoped_refptr<password_manager::MockPasswordStore> mock_store_; | |
| 83 | 71 |
| 84 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); | 72 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); |
| 85 }; | 73 }; |
| 86 | 74 |
| 87 void PasswordManagerPresenterTest::AddPasswordEntry( | 75 void PasswordManagerPresenterTest::AddPasswordEntry( |
| 88 const GURL& origin, | 76 const GURL& origin, |
| 89 const std::string& user_name, | 77 const std::string& user_name, |
| 90 const std::string& password) { | 78 const std::string& password) { |
| 91 autofill::PasswordForm* form = new autofill::PasswordForm(); | 79 autofill::PasswordForm* form = new autofill::PasswordForm(); |
| 92 form->origin = origin; | 80 form->origin = origin; |
| 93 form->username_element = ASCIIToUTF16("Email"); | 81 form->username_element = base::ASCIIToUTF16("Email"); |
| 94 form->username_value = ASCIIToUTF16(user_name); | 82 form->username_value = base::ASCIIToUTF16(user_name); |
| 95 form->password_element = ASCIIToUTF16("Passwd"); | 83 form->password_element = base::ASCIIToUTF16("Passwd"); |
| 96 form->password_value = ASCIIToUTF16(password); | 84 form->password_value = base::ASCIIToUTF16(password); |
| 97 GetPasswordManagerPresenter()->password_list_.push_back(form); | 85 mock_controller_->GetPasswordManagerPresenter()->password_list_ |
| 86 .push_back(form); |
| 98 } | 87 } |
| 99 | 88 |
| 100 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) { | 89 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) { |
| 101 autofill::PasswordForm* form = new autofill::PasswordForm(); | 90 autofill::PasswordForm* form = new autofill::PasswordForm(); |
| 102 form->origin = origin; | 91 form->origin = origin; |
| 103 GetPasswordManagerPresenter()->password_exception_list_.push_back(form); | 92 mock_controller_->GetPasswordManagerPresenter()->password_exception_list_ |
| 93 .push_back(form); |
| 104 } | 94 } |
| 105 | 95 |
| 106 void PasswordManagerPresenterTest::UpdateLists() { | 96 void PasswordManagerPresenterTest::UpdateLists() { |
| 107 GetPasswordManagerPresenter()->SetPasswordList(); | 97 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); |
| 108 GetPasswordManagerPresenter()->SetPasswordExceptionList(); | 98 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); |
| 109 } | 99 } |
| 110 | 100 |
| 111 namespace { | 101 namespace { |
| 112 | 102 |
| 113 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { | 103 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { |
| 114 EXPECT_CALL( | 104 EXPECT_CALL( |
| 115 *GetUIController(), | 105 *GetUIController(), |
| 116 SetPasswordList( | 106 SetPasswordList( |
| 117 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)), | 107 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)), |
| 118 testing::_)); | 108 testing::_)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 148 *GetUIController(), | 138 *GetUIController(), |
| 149 SetPasswordList( | 139 SetPasswordList( |
| 150 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)), | 140 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)), |
| 151 testing::_)); | 141 testing::_)); |
| 152 EXPECT_CALL(*GetUIController(), | 142 EXPECT_CALL(*GetUIController(), |
| 153 SetPasswordExceptionList(Property( | 143 SetPasswordExceptionList(Property( |
| 154 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); | 144 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); |
| 155 UpdateLists(); | 145 UpdateLists(); |
| 156 } | 146 } |
| 157 | 147 |
| 158 // AddPassword and UpdatePassword are never called on Android. | |
| 159 #if !defined(OS_ANDROID) | |
| 160 TEST_F(PasswordManagerPresenterTest, CallAddPassword) { | |
| 161 GURL basic_origin("http://host.com"); | |
| 162 base::string16 username = ASCIIToUTF16("username"); | |
| 163 base::string16 password = ASCIIToUTF16("password"); | |
| 164 EXPECT_CALL( | |
| 165 *GetPasswordStore(), | |
| 166 AddLogin(testing::AllOf( | |
| 167 Field(&autofill::PasswordForm::signon_realm, Eq(basic_origin.spec())), | |
| 168 Field(&autofill::PasswordForm::origin, Eq(basic_origin)), | |
| 169 Field(&autofill::PasswordForm::username_value, Eq(username)), | |
| 170 Field(&autofill::PasswordForm::password_value, Eq(password)), | |
| 171 Field(&autofill::PasswordForm::ssl_valid, Eq(false))))); | |
| 172 GetPasswordManagerPresenter()->AddPassword(basic_origin, username, password); | |
| 173 | |
| 174 GURL complex_origin("https://foo:bar@host.com:1234/path?query=v#ref"); | |
| 175 EXPECT_CALL( | |
| 176 *GetPasswordStore(), | |
| 177 AddLogin(testing::AllOf( | |
| 178 Field(&autofill::PasswordForm::signon_realm, | |
| 179 Eq("https://host.com:1234/")), | |
| 180 Field(&autofill::PasswordForm::origin, | |
| 181 Eq(GURL("https://host.com:1234/path"))), | |
| 182 Field(&autofill::PasswordForm::username_value, Eq(username)), | |
| 183 Field(&autofill::PasswordForm::password_value, Eq(password)), | |
| 184 Field(&autofill::PasswordForm::ssl_valid, Eq(true))))); | |
| 185 GetPasswordManagerPresenter()->AddPassword(complex_origin, | |
| 186 username, | |
| 187 password); | |
| 188 } | |
| 189 | |
| 190 TEST_F(PasswordManagerPresenterTest, CallUpdatePassword) { | |
| 191 GURL origin1("http://host.com"); | |
| 192 static const char kUsername1[] = "username"; | |
| 193 AddPasswordEntry(origin1, kUsername1, "password"); | |
| 194 GURL origin2("https://example.com"); | |
| 195 static const char kUsername2[] = "testname"; | |
| 196 AddPasswordEntry(origin2, kUsername2, "abcd"); | |
| 197 | |
| 198 base::string16 new_password = ASCIIToUTF16("testpassword"); | |
| 199 EXPECT_CALL( | |
| 200 *GetPasswordStore(), | |
| 201 UpdateLogin(testing::AllOf( | |
| 202 Field(&autofill::PasswordForm::origin, Eq(origin1)), | |
| 203 Field(&autofill::PasswordForm::username_value, | |
| 204 Eq(ASCIIToUTF16(kUsername1))), | |
| 205 Field(&autofill::PasswordForm::password_value, | |
| 206 Eq(new_password))))); | |
| 207 GetPasswordManagerPresenter()->UpdatePassword(0, new_password); | |
| 208 | |
| 209 base::string16 new_password_again = ASCIIToUTF16("testpassword_again"); | |
| 210 EXPECT_CALL( | |
| 211 *GetPasswordStore(), | |
| 212 UpdateLogin(testing::AllOf( | |
| 213 Field(&autofill::PasswordForm::origin, Eq(origin1)), | |
| 214 Field(&autofill::PasswordForm::username_value, | |
| 215 Eq(ASCIIToUTF16(kUsername1))), | |
| 216 Field(&autofill::PasswordForm::password_value, | |
| 217 Eq(new_password_again))))); | |
| 218 GetPasswordManagerPresenter()->UpdatePassword(0, new_password_again); | |
| 219 | |
| 220 base::string16 another_password = ASCIIToUTF16("mypassword"); | |
| 221 EXPECT_CALL( | |
| 222 *GetPasswordStore(), | |
| 223 UpdateLogin(testing::AllOf( | |
| 224 Field(&autofill::PasswordForm::origin, Eq(origin2)), | |
| 225 Field(&autofill::PasswordForm::username_value, | |
| 226 Eq(ASCIIToUTF16(kUsername2))), | |
| 227 Field(&autofill::PasswordForm::password_value, | |
| 228 Eq(another_password))))); | |
| 229 GetPasswordManagerPresenter()->UpdatePassword(1, another_password); | |
| 230 } | |
| 231 #endif // !defined(OS_ANDROID) | |
| 232 | |
| 233 TEST(PasswordManagerPresenterTestSimple, CallCheckOriginValidityForAdding) { | |
| 234 static const char* const kValidOrigins[] = { | |
| 235 "http://host.com", | |
| 236 "http://host.com/path", | |
| 237 "https://host.com", | |
| 238 "https://foo:bar@host.com/path?query=v#ref", | |
| 239 "https://foo:bar@host.com:1234/path?query=v#ref" | |
| 240 }; | |
| 241 for (size_t i = 0; i < arraysize(kValidOrigins); ++i) { | |
| 242 SCOPED_TRACE(kValidOrigins[i]); | |
| 243 EXPECT_TRUE(PasswordManagerPresenter::CheckOriginValidityForAdding( | |
| 244 GURL(kValidOrigins[i]))); | |
| 245 } | |
| 246 | |
| 247 static const char* const kInvalidOrigins[] = { | |
| 248 "noscheme", | |
| 249 "invalidscheme:host.com", | |
| 250 "ftp://ftp.host.com", | |
| 251 "about:test" | |
| 252 }; | |
| 253 for (size_t i = 0; i < arraysize(kInvalidOrigins); ++i) { | |
| 254 SCOPED_TRACE(kInvalidOrigins[i]); | |
| 255 EXPECT_FALSE(PasswordManagerPresenter::CheckOriginValidityForAdding( | |
| 256 GURL(kInvalidOrigins[i]))); | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 } // namespace | 148 } // namespace |
| OLD | NEW |