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