Chromium Code Reviews| 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 int 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 int number_of_entries, | |
|
vabr (Chromium)
2016/03/08 13:09:28
int -> size_t
kolos1
2016/03/08 15:14:24
Done.
| |
| 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 (int i = 0; i < number_of_entries; i++) { | |
|
vabr (Chromium)
2016/03/08 13:09:28
int -> size_t
kolos1
2016/03/08 15:14:24
Done.
| |
| 135 const SortEntry entry = test_entries[i]; | |
|
vabr (Chromium)
2016/03/08 13:09:28
Make this a reference to avoid copying.
Also on l
kolos1
2016/03/08 15:14:24
Done.
| |
| 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 autofill::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 (int i = 0; i < number_of_entries; i++) { | |
| 159 const SortEntry entry = test_entries[i]; | |
| 160 if (entry.expected_position >= 0) { | |
| 161 EXPECT_EQ(GURL(base::ASCIIToUTF16(entry.origin)), | |
| 162 list[entry.expected_position]->origin) | |
| 163 << "position in sorted list: " << entry.expected_position; | |
|
vabr (Chromium)
2016/03/08 13:09:28
To avoid code duplication, use
SCOPED_TRACE(testin
kolos1
2016/03/08 15:14:24
Done.
| |
| 164 if (username_and_password_in_key) { | |
| 165 EXPECT_EQ(base::ASCIIToUTF16(entry.username), | |
| 166 list[entry.expected_position]->username_value) | |
| 167 << "position in sorted list: " << entry.expected_position; | |
| 168 EXPECT_EQ(base::ASCIIToUTF16(entry.password), | |
| 169 list[entry.expected_position]->password_value) | |
| 170 << "position in sorted list: " << entry.expected_position; | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 } | |
| 175 | |
| 113 namespace { | 176 namespace { |
| 114 | 177 |
| 115 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { | 178 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { |
| 116 EXPECT_CALL( | 179 EXPECT_CALL( |
| 117 *GetUIController(), | 180 *GetUIController(), |
| 118 SetPasswordList( | 181 SetPasswordList( |
| 119 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, | 182 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, |
| 120 Eq(0u)), | 183 Eq(0u)), |
| 121 testing::_)); | 184 testing::_)); |
| 122 EXPECT_CALL( | 185 EXPECT_CALL( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, | 221 Property(&std::vector<scoped_ptr<autofill::PasswordForm>>::size, |
| 159 Eq(2u)), | 222 Eq(2u)), |
| 160 testing::_)); | 223 testing::_)); |
| 161 EXPECT_CALL( | 224 EXPECT_CALL( |
| 162 *GetUIController(), | 225 *GetUIController(), |
| 163 SetPasswordExceptionList(Property( | 226 SetPasswordExceptionList(Property( |
| 164 &std::vector<scoped_ptr<autofill::PasswordForm>>::size, Eq(1u)))); | 227 &std::vector<scoped_ptr<autofill::PasswordForm>>::size, Eq(1u)))); |
| 165 UpdateLists(); | 228 UpdateLists(); |
| 166 } | 229 } |
| 167 | 230 |
| 231 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentOrigins) { | |
| 232 const SortEntry test_cases[] = { | |
| 233 {"http://example-b.com", "user_a", "pwd", nullptr, 2}, | |
| 234 {"http://example-a.com", "user_a1", "pwd", nullptr, 0}, | |
| 235 {"http://example-a.com", "user_a2", "pwd", nullptr, 1}, | |
| 236 {"http://example-c.com", "user_a", "pwd", nullptr, 3}}; | |
| 237 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | |
| 238 } | |
| 239 | |
| 240 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentUsernames) { | |
| 241 const SortEntry test_cases[] = { | |
| 242 {"http://example.com", "user_a", "pwd", nullptr, 0}, | |
| 243 {"http://example.com", "user_c", "pwd", nullptr, 2}, | |
| 244 {"http://example.com", "user_b", "pwd", nullptr, 1}}; | |
| 245 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | |
| 246 } | |
| 247 | |
| 248 TEST_F(PasswordManagerPresenterTest, Sorting_DifferentPasswords) { | |
| 249 const SortEntry test_cases[] = { | |
| 250 {"http://example.com", "user_a", "1", nullptr, 0}, | |
| 251 {"http://example.com", "user_a", "2", nullptr, 2}, | |
| 252 {"http://example.com", "user_a", "3", nullptr, 1}}; | |
| 253 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | |
| 254 } | |
| 255 | |
| 256 TEST_F(PasswordManagerPresenterTest, Sorting_HideDuplicates) { | |
| 257 const SortEntry test_cases[] = { | |
| 258 {"http://example.com", "user_a", "pwd", nullptr, 0}, | |
| 259 // Different username. | |
| 260 {"http://example.com", "user_b", "pwd", nullptr, 2}, | |
| 261 // Different password. | |
| 262 {"http://example.com", "user_a", "secret", nullptr, 1}, | |
| 263 // Different origin. | |
| 264 {"http://sub1.example.com", "user_a", "pwd", nullptr, 3}, | |
| 265 {"http://example.com", "user_a", "pwd", nullptr, -1} // Hide it. | |
| 266 }; | |
| 267 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | |
| 268 } | |
| 269 | |
| 270 TEST_F(PasswordManagerPresenterTest, Sorting_DontUseUsernameAndPasswordInKey) { | |
| 271 const SortEntry test_cases[] = { | |
| 272 {"http://example-b.com", nullptr, nullptr, nullptr, 1}, | |
| 273 {"http://example-a.com", nullptr, nullptr, nullptr, 0}, | |
| 274 {"http://example-a.com", nullptr, nullptr, nullptr, -1}, // Hide it. | |
| 275 {"http://example-c.com", nullptr, nullptr, nullptr, 2}}; | |
| 276 SortAndCheckPositions(test_cases, arraysize(test_cases), false); | |
| 277 } | |
| 278 | |
| 279 TEST_F(PasswordManagerPresenterTest, Sorting_AndroidCredentials) { | |
| 280 const SortEntry test_cases[] = { | |
| 281 {"https://alpha.com", "user", "secret", nullptr, 0}, | |
| 282 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", 1}, | |
| 283 {"android://hash@com.alpha", "user", "secret", "https://alpha.com", -1}, | |
| 284 {"android://hash@com.alpha", "user", "secret", nullptr, 2}, | |
| 285 | |
| 286 {"android://hash@com.betta.android", "user", "secret", | |
| 287 "https://betta.com", 3}, | |
| 288 {"android://hash@com.betta.android", "user", "secret", nullptr, 4}}; | |
| 289 SortAndCheckPositions(test_cases, arraysize(test_cases), true); | |
| 290 } | |
| 291 | |
| 168 } // namespace | 292 } // namespace |
| OLD | NEW |