| 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 "chrome/browser/ui/passwords/password_manager_presenter.h" | 5 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/password_manager/password_store_factory.h" | 13 #include "chrome/browser/password_manager/password_store_factory.h" |
| 14 #include "chrome/browser/ui/passwords/password_ui_view.h" | 14 #include "chrome/browser/ui/passwords/password_ui_view.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/password_manager/core/browser/mock_password_store.h" | 16 #include "components/password_manager/core/browser/mock_password_store.h" |
| 17 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 17 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 using base::ASCIIToUTF16; | 22 using base::ASCIIToUTF16; |
| 23 using testing::Eq; | 23 using testing::Eq; |
| 24 using testing::Property; | 24 using testing::Property; |
| 25 | 25 |
| 26 namespace { |
| 27 |
| 28 struct SortEntry { |
| 29 const char* const origin; |
| 30 const char* const username; |
| 31 const char* const password; |
| 32 const char* const affiliated_web_realm; |
| 33 const int expected_position; |
| 34 }; |
| 35 |
| 36 } // namespace |
| 37 |
| 26 class MockPasswordUIView : public PasswordUIView { | 38 class MockPasswordUIView : public PasswordUIView { |
| 27 public: | 39 public: |
| 28 explicit MockPasswordUIView(Profile* profile) | 40 explicit MockPasswordUIView(Profile* profile) |
| 29 : profile_(profile), password_manager_presenter_(this) { | 41 : profile_(profile), password_manager_presenter_(this) { |
| 30 password_manager_presenter_.Initialize(); | 42 password_manager_presenter_.Initialize(); |
| 31 } | 43 } |
| 32 ~MockPasswordUIView() override {} | 44 ~MockPasswordUIView() override {} |
| 33 Profile* GetProfile() override; | 45 Profile* GetProfile() override; |
| 34 #if !defined(OS_ANDROID) | 46 #if !defined(OS_ANDROID) |
| 35 gfx::NativeWindow GetNativeWindow() const override; | 47 gfx::NativeWindow GetNativeWindow() const override; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 password_manager::BuildPasswordStore< | 80 password_manager::BuildPasswordStore< |
| 69 content::BrowserContext, password_manager::MockPasswordStore>); | 81 content::BrowserContext, password_manager::MockPasswordStore>); |
| 70 mock_controller_.reset(new MockPasswordUIView(&profile_)); | 82 mock_controller_.reset(new MockPasswordUIView(&profile_)); |
| 71 } | 83 } |
| 72 void AddPasswordEntry(const GURL& origin, | 84 void AddPasswordEntry(const GURL& origin, |
| 73 const std::string& user_name, | 85 const std::string& user_name, |
| 74 const std::string& password); | 86 const std::string& password); |
| 75 void AddPasswordException(const GURL& origin); | 87 void AddPasswordException(const GURL& origin); |
| 76 void UpdateLists(); | 88 void UpdateLists(); |
| 77 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } | 89 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } |
| 90 void SortAndCheckPositions(const SortEntry test_entries[], |
| 91 size_t number_of_entries, |
| 92 bool username_and_password_in_key); |
| 78 | 93 |
| 79 private: | 94 private: |
| 80 content::TestBrowserThreadBundle thread_bundle_; | 95 content::TestBrowserThreadBundle thread_bundle_; |
| 81 TestingProfile profile_; | 96 TestingProfile profile_; |
| 82 scoped_ptr<MockPasswordUIView> mock_controller_; | 97 scoped_ptr<MockPasswordUIView> mock_controller_; |
| 83 | 98 |
| 84 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); | 99 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); |
| 85 }; | 100 }; |
| 86 | 101 |
| 87 void PasswordManagerPresenterTest::AddPasswordEntry( | 102 void PasswordManagerPresenterTest::AddPasswordEntry( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 103 form->origin = origin; | 118 form->origin = origin; |
| 104 mock_controller_->GetPasswordManagerPresenter() | 119 mock_controller_->GetPasswordManagerPresenter() |
| 105 ->password_exception_list_.push_back(std::move(form)); | 120 ->password_exception_list_.push_back(std::move(form)); |
| 106 } | 121 } |
| 107 | 122 |
| 108 void PasswordManagerPresenterTest::UpdateLists() { | 123 void PasswordManagerPresenterTest::UpdateLists() { |
| 109 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); | 124 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); |
| 110 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); | 125 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); |
| 111 } | 126 } |
| 112 | 127 |
| 128 void PasswordManagerPresenterTest::SortAndCheckPositions( |
| 129 const SortEntry test_entries[], |
| 130 size_t number_of_entries, |
| 131 bool username_and_password_in_key) { |
| 132 std::vector<scoped_ptr<autofill::PasswordForm>> list; |
| 133 size_t expected_number_of_unique_entries = 0; |
| 134 for (size_t i = 0; i < number_of_entries; i++) { |
| 135 const SortEntry& entry = test_entries[i]; |
| 136 scoped_ptr<autofill::PasswordForm> form(new autofill::PasswordForm()); |
| 137 form->signon_realm = entry.origin; |
| 138 form->origin = GURL(base::ASCIIToUTF16(entry.origin)); |
| 139 if (username_and_password_in_key) { |
| 140 form->username_value = base::ASCIIToUTF16(entry.username); |
| 141 form->password_value = base::ASCIIToUTF16(entry.password); |
| 142 } |
| 143 if (entry.affiliated_web_realm) |
| 144 form->affiliated_web_realm = entry.affiliated_web_realm; |
| 145 list.push_back(std::move(form)); |
| 146 if (entry.expected_position >= 0) |
| 147 expected_number_of_unique_entries++; |
| 148 } |
| 149 |
| 150 DuplicatesMap duplicates; |
| 151 std::string languages; |
| 152 mock_controller_->GetPasswordManagerPresenter()->SortEntriesAndHideDuplicates( |
| 153 languages, &list, &duplicates, username_and_password_in_key); |
| 154 |
| 155 ASSERT_EQ(expected_number_of_unique_entries, list.size()); |
| 156 ASSERT_EQ(number_of_entries - expected_number_of_unique_entries, |
| 157 duplicates.size()); |
| 158 for (size_t i = 0; i < number_of_entries; i++) { |
| 159 const SortEntry& entry = test_entries[i]; |
| 160 if (entry.expected_position >= 0) { |
| 161 SCOPED_TRACE(testing::Message("position in sorted list: ") |
| 162 << entry.expected_position); |
| 163 EXPECT_EQ(GURL(base::ASCIIToUTF16(entry.origin)), |
| 164 list[entry.expected_position]->origin); |
| 165 if (username_and_password_in_key) { |
| 166 EXPECT_EQ(base::ASCIIToUTF16(entry.username), |
| 167 list[entry.expected_position]->username_value); |
| 168 EXPECT_EQ(base::ASCIIToUTF16(entry.password), |
| 169 list[entry.expected_position]->password_value); |
| 170 } |
| 171 } |
| 172 } |
| 173 } |
| 174 |
| 113 namespace { | 175 namespace { |
| 114 | 176 |
| 115 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { | 177 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { |
| 116 EXPECT_CALL( | 178 EXPECT_CALL( |
| 117 *GetUIController(), | 179 *GetUIController(), |
| 118 SetPasswordList( | 180 SetPasswordList( |
| 119 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, | 181 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, |
| 120 Eq(0u)), | 182 Eq(0u)), |
| 121 testing::_)); | 183 testing::_)); |
| 122 EXPECT_CALL( | 184 EXPECT_CALL( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, | 220 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, |
| 159 Eq(2u)), | 221 Eq(2u)), |
| 160 testing::_)); | 222 testing::_)); |
| 161 EXPECT_CALL( | 223 EXPECT_CALL( |
| 162 *GetUIController(), | 224 *GetUIController(), |
| 163 SetPasswordExceptionList(Property( | 225 SetPasswordExceptionList(Property( |
| 164 &std::vector<scoped_ptr<autofill::PasswordForm>>::size, Eq(1u)))); | 226 &std::vector<scoped_ptr<autofill::PasswordForm>>::size, Eq(1u)))); |
| 165 UpdateLists(); | 227 UpdateLists(); |
| 166 } | 228 } |
| 167 | 229 |
| 230 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentOrigins) { |
| 231 const SortEntry test_cases[] = { |
| 232 {"http://example-b.com", "user_a", "pwd", nullptr, 2}, |
| 233 {"http://example-a.com", "user_a1", "pwd", nullptr, 0}, |
| 234 {"http://example-a.com", "user_a2", "pwd", nullptr, 1}, |
| 235 {"http://example-c.com", "user_a", "pwd", nullptr, 3}}; |
| 236 SortAndCheckPositions(test_cases, arraysize(test_cases), true); |
| 237 } |
| 238 |
| 239 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentUsernames) { |
| 240 const SortEntry test_cases[] = { |
| 241 {"http://example.com", "user_a", "pwd", nullptr, 0}, |
| 242 {"http://example.com", "user_c", "pwd", nullptr, 2}, |
| 243 {"http://example.com", "user_b", "pwd", nullptr, 1}}; |
| 244 SortAndCheckPositions(test_cases, arraysize(test_cases), true); |
| 245 } |
| 246 |
| 247 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentPasswords) { |
| 248 const SortEntry test_cases[] = { |
| 249 {"http://example.com", "user_a", "1", nullptr, 0}, |
| 250 {"http://example.com", "user_a", "2", nullptr, 1}, |
| 251 {"http://example.com", "user_a", "3", nullptr, 2}}; |
| 252 SortAndCheckPositions(test_cases, arraysize(test_cases), true); |
| 253 } |
| 254 |
| 255 TEST_F(PasswordManagerPresenterTest, Sorting_HideDuplicates) { |
| 256 const SortEntry test_cases[] = { |
| 257 {"http://example.com", "user_a", "pwd", nullptr, 0}, |
| 258 // Different username. |
| 259 {"http://example.com", "user_b", "pwd", nullptr, 2}, |
| 260 // Different password. |
| 261 {"http://example.com", "user_a", "secret", nullptr, 1}, |
| 262 // Different origin. |
| 263 {"http://sub1.example.com", "user_a", "pwd", nullptr, 3}, |
| 264 {"http://example.com", "user_a", "pwd", nullptr, -1} // Hide it. |
| 265 }; |
| 266 SortAndCheckPositions(test_cases, arraysize(test_cases), true); |
| 267 } |
| 268 |
| 269 TEST_F(PasswordManagerPresenterTest, Sorting_DontUseUsernameAndPasswordInKey) { |
| 270 const SortEntry test_cases[] = { |
| 271 {"http://example-b.com", nullptr, nullptr, nullptr, 1}, |
| 272 {"http://example-a.com", nullptr, nullptr, nullptr, 0}, |
| 273 {"http://example-a.com", nullptr, nullptr, nullptr, -1}, // Hide it. |
| 274 {"http://example-c.com", nullptr, nullptr, nullptr, 2}}; |
| 275 SortAndCheckPositions(test_cases, arraysize(test_cases), false); |
| 276 } |
| 277 |
| 278 TEST_F(PasswordManagerPresenterTest, Sorting_AndroidCredentials) { |
| 279 const SortEntry test_cases[] = { |
| 280 {"https://alpha.com", "user", "secret", nullptr, 0}, |
| 281 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", 1}, |
| 282 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", -1}, |
| 283 {"android://hash@com.alpha", "user", "secret", nullptr, 2}, |
| 284 |
| 285 {"android://hash@com.betta.android", "user", "secret", |
| 286 "https://betta.com", 3}, |
| 287 {"android://hash@com.betta.android", "user", "secret", nullptr, 4}}; |
| 288 SortAndCheckPositions(test_cases, arraysize(test_cases), true); |
| 289 } |
| 290 |
| 168 } // namespace | 291 } // namespace |
| OLD | NEW |