| 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" |
| 5 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 6 #include "chrome/browser/password_manager/mock_password_store_service.h" | 7 #include "chrome/browser/password_manager/mock_password_store_service.h" |
| 7 #include "chrome/browser/password_manager/password_store_factory.h" | 8 #include "chrome/browser/password_manager/password_store_factory.h" |
| 8 #include "chrome/browser/ui/passwords/password_manager_presenter.h" | 9 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 9 #include "chrome/browser/ui/passwords/password_ui_view.h" | 10 #include "chrome/browser/ui/passwords/password_ui_view.h" |
| 10 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/password_manager/core/browser/mock_password_store.h" | 12 #include "components/password_manager/core/browser/mock_password_store.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using base::ASCIIToUTF16; | 16 using base::ASCIIToUTF16; |
| 16 using testing::Eq; | 17 using testing::Eq; |
| 18 using testing::Field; |
| 17 using testing::Property; | 19 using testing::Property; |
| 18 | 20 |
| 19 class MockPasswordUIView : public PasswordUIView { | 21 class MockPasswordUIView : public PasswordUIView { |
| 20 public: | 22 public: |
| 21 explicit MockPasswordUIView(Profile* profile) | 23 explicit MockPasswordUIView(Profile* profile) |
| 22 : profile_(profile), password_manager_presenter_(this) { | 24 : profile_(profile), password_manager_presenter_(this) { |
| 23 password_manager_presenter_.Initialize(); | 25 password_manager_presenter_.Initialize(); |
| 24 } | 26 } |
| 25 virtual ~MockPasswordUIView() {} | 27 virtual ~MockPasswordUIView() {} |
| 26 virtual Profile* GetProfile() OVERRIDE; | 28 virtual Profile* GetProfile() OVERRIDE; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 50 | 52 |
| 51 class PasswordManagerPresenterTest : public testing::Test { | 53 class PasswordManagerPresenterTest : public testing::Test { |
| 52 protected: | 54 protected: |
| 53 PasswordManagerPresenterTest() {} | 55 PasswordManagerPresenterTest() {} |
| 54 | 56 |
| 55 virtual ~PasswordManagerPresenterTest() {} | 57 virtual ~PasswordManagerPresenterTest() {} |
| 56 virtual void SetUp() OVERRIDE { | 58 virtual void SetUp() OVERRIDE { |
| 57 PasswordStoreFactory::GetInstance()->SetTestingFactory( | 59 PasswordStoreFactory::GetInstance()->SetTestingFactory( |
| 58 &profile_, MockPasswordStoreService::Build); | 60 &profile_, MockPasswordStoreService::Build); |
| 59 mock_controller_.reset(new MockPasswordUIView(&profile_)); | 61 mock_controller_.reset(new MockPasswordUIView(&profile_)); |
| 62 mock_store_ = static_cast<password_manager::MockPasswordStore*>( |
| 63 PasswordStoreFactory::GetForProfile(&profile_, |
| 64 Profile::EXPLICIT_ACCESS).get()); |
| 60 } | 65 } |
| 61 void AddPasswordEntry(const GURL& origin, | 66 void AddPasswordEntry(const GURL& origin, |
| 62 const std::string& user_name, | 67 const std::string& user_name, |
| 63 const std::string& password); | 68 const std::string& password); |
| 64 void AddPasswordException(const GURL& origin); | 69 void AddPasswordException(const GURL& origin); |
| 65 void UpdateLists(); | 70 void UpdateLists(); |
| 66 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } | 71 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 } |
| 67 | 78 |
| 68 private: | 79 private: |
| 69 TestingProfile profile_; | 80 TestingProfile profile_; |
| 70 scoped_ptr<MockPasswordUIView> mock_controller_; | 81 scoped_ptr<MockPasswordUIView> mock_controller_; |
| 82 scoped_refptr<password_manager::MockPasswordStore> mock_store_; |
| 71 | 83 |
| 72 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); | 84 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); |
| 73 }; | 85 }; |
| 74 | 86 |
| 75 void PasswordManagerPresenterTest::AddPasswordEntry( | 87 void PasswordManagerPresenterTest::AddPasswordEntry( |
| 76 const GURL& origin, | 88 const GURL& origin, |
| 77 const std::string& user_name, | 89 const std::string& user_name, |
| 78 const std::string& password) { | 90 const std::string& password) { |
| 79 autofill::PasswordForm* form = new autofill::PasswordForm(); | 91 autofill::PasswordForm* form = new autofill::PasswordForm(); |
| 80 form->origin = origin; | 92 form->origin = origin; |
| 81 form->username_element = base::ASCIIToUTF16("Email"); | 93 form->username_element = ASCIIToUTF16("Email"); |
| 82 form->username_value = base::ASCIIToUTF16(user_name); | 94 form->username_value = ASCIIToUTF16(user_name); |
| 83 form->password_element = base::ASCIIToUTF16("Passwd"); | 95 form->password_element = ASCIIToUTF16("Passwd"); |
| 84 form->password_value = base::ASCIIToUTF16(password); | 96 form->password_value = ASCIIToUTF16(password); |
| 85 mock_controller_->GetPasswordManagerPresenter()->password_list_ | 97 GetPasswordManagerPresenter()->password_list_.push_back(form); |
| 86 .push_back(form); | |
| 87 } | 98 } |
| 88 | 99 |
| 89 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) { | 100 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) { |
| 90 autofill::PasswordForm* form = new autofill::PasswordForm(); | 101 autofill::PasswordForm* form = new autofill::PasswordForm(); |
| 91 form->origin = origin; | 102 form->origin = origin; |
| 92 mock_controller_->GetPasswordManagerPresenter()->password_exception_list_ | 103 GetPasswordManagerPresenter()->password_exception_list_.push_back(form); |
| 93 .push_back(form); | |
| 94 } | 104 } |
| 95 | 105 |
| 96 void PasswordManagerPresenterTest::UpdateLists() { | 106 void PasswordManagerPresenterTest::UpdateLists() { |
| 97 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); | 107 GetPasswordManagerPresenter()->SetPasswordList(); |
| 98 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); | 108 GetPasswordManagerPresenter()->SetPasswordExceptionList(); |
| 99 } | 109 } |
| 100 | 110 |
| 101 namespace { | 111 namespace { |
| 102 | 112 |
| 103 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { | 113 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { |
| 104 EXPECT_CALL( | 114 EXPECT_CALL( |
| 105 *GetUIController(), | 115 *GetUIController(), |
| 106 SetPasswordList( | 116 SetPasswordList( |
| 107 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)), | 117 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)), |
| 108 testing::_)); | 118 testing::_)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 138 *GetUIController(), | 148 *GetUIController(), |
| 139 SetPasswordList( | 149 SetPasswordList( |
| 140 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)), | 150 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)), |
| 141 testing::_)); | 151 testing::_)); |
| 142 EXPECT_CALL(*GetUIController(), | 152 EXPECT_CALL(*GetUIController(), |
| 143 SetPasswordExceptionList(Property( | 153 SetPasswordExceptionList(Property( |
| 144 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); | 154 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); |
| 145 UpdateLists(); | 155 UpdateLists(); |
| 146 } | 156 } |
| 147 | 157 |
| 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 |
| 148 } // namespace | 260 } // namespace |
| OLD | NEW |