| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/password_manager/password_form_manager.h" | 10 #include "chrome/browser/password_manager/password_form_manager.h" |
| 12 #include "chrome/browser/password_manager/password_manager.h" | 11 #include "chrome/browser/password_manager/password_manager.h" |
| 13 #include "chrome/browser/password_manager/password_manager_delegate.h" | 12 #include "chrome/browser/password_manager/password_manager_delegate.h" |
| 14 #include "chrome/browser/password_manager/password_store.h" | 13 #include "chrome/browser/password_manager/password_store.h" |
| 15 #include "chrome/browser/password_manager/password_store_factory.h" | 14 #include "chrome/browser/password_manager/password_store_factory.h" |
| 16 #include "chrome/browser/password_manager/test_password_store.h" | 15 #include "chrome/browser/password_manager/test_password_store.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/public/common/password_form.h" | 18 #include "content/public/common/password_form.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 | 22 |
| 23 using content::PasswordForm; | 23 using content::PasswordForm; |
| 24 | 24 |
| 25 using ::testing::Eq; | 25 using ::testing::Eq; |
| 26 | 26 |
| 27 class TestPasswordManagerDelegate : public PasswordManagerDelegate { | 27 class TestPasswordManagerDelegate : public PasswordManagerDelegate { |
| 28 public: | 28 public: |
| 29 explicit TestPasswordManagerDelegate(Profile* profile) : profile_(profile) {} | 29 explicit TestPasswordManagerDelegate(Profile* profile) : profile_(profile) {} |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 size_t num_sent_messages() { | 67 size_t num_sent_messages() { |
| 68 return num_sent_messages_; | 68 return num_sent_messages_; |
| 69 } | 69 } |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 size_t num_sent_messages_; | 72 size_t num_sent_messages_; |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 class PasswordFormManagerTest : public testing::Test { | 75 class PasswordFormManagerTest : public testing::Test { |
| 76 public: | 76 public: |
| 77 PasswordFormManagerTest() { | |
| 78 } | |
| 79 virtual void SetUp() { | 77 virtual void SetUp() { |
| 80 observed_form_.origin = GURL("http://accounts.google.com/a/LoginAuth"); | 78 observed_form_.origin = GURL("http://accounts.google.com/a/LoginAuth"); |
| 81 observed_form_.action = GURL("http://accounts.google.com/a/Login"); | 79 observed_form_.action = GURL("http://accounts.google.com/a/Login"); |
| 82 observed_form_.username_element = ASCIIToUTF16("Email"); | 80 observed_form_.username_element = ASCIIToUTF16("Email"); |
| 83 observed_form_.password_element = ASCIIToUTF16("Passwd"); | 81 observed_form_.password_element = ASCIIToUTF16("Passwd"); |
| 84 observed_form_.submit_element = ASCIIToUTF16("signIn"); | 82 observed_form_.submit_element = ASCIIToUTF16("signIn"); |
| 85 observed_form_.signon_realm = "http://accounts.google.com"; | 83 observed_form_.signon_realm = "http://accounts.google.com"; |
| 86 | 84 |
| 87 saved_match_ = observed_form_; | 85 saved_match_ = observed_form_; |
| 88 saved_match_.origin = GURL("http://accounts.google.com/a/ServiceLoginAuth"); | 86 saved_match_.origin = GURL("http://accounts.google.com/a/ServiceLoginAuth"); |
| 89 saved_match_.action = GURL("http://accounts.google.com/a/ServiceLogin"); | 87 saved_match_.action = GURL("http://accounts.google.com/a/ServiceLogin"); |
| 90 saved_match_.preferred = true; | 88 saved_match_.preferred = true; |
| 91 saved_match_.username_value = ASCIIToUTF16("test@gmail.com"); | 89 saved_match_.username_value = ASCIIToUTF16("test@gmail.com"); |
| 92 saved_match_.password_value = ASCIIToUTF16("test1"); | 90 saved_match_.password_value = ASCIIToUTF16("test1"); |
| 93 saved_match_.other_possible_usernames.push_back( | 91 saved_match_.other_possible_usernames.push_back( |
| 94 ASCIIToUTF16("test2@gmail.com")); | 92 ASCIIToUTF16("test2@gmail.com")); |
| 95 profile_ = new TestingProfile(); | |
| 96 } | |
| 97 | |
| 98 virtual void TearDown() { | |
| 99 delete profile_; | |
| 100 } | 93 } |
| 101 | 94 |
| 102 PasswordForm* GetPendingCredentials(PasswordFormManager* p) { | 95 PasswordForm* GetPendingCredentials(PasswordFormManager* p) { |
| 103 return &p->pending_credentials_; | 96 return &p->pending_credentials_; |
| 104 } | 97 } |
| 105 | 98 |
| 106 void SimulateMatchingPhase(PasswordFormManager* p, bool find_match) { | 99 void SimulateMatchingPhase(PasswordFormManager* p, bool find_match) { |
| 107 // Roll up the state to mock out the matching phase. | 100 // Roll up the state to mock out the matching phase. |
| 108 p->state_ = PasswordFormManager::POST_MATCHING_PHASE; | 101 p->state_ = PasswordFormManager::POST_MATCHING_PHASE; |
| 109 if (!find_match) | 102 if (!find_match) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 130 } | 123 } |
| 131 | 124 |
| 132 void SanitizePossibleUsernames(PasswordFormManager* p, PasswordForm* form) { | 125 void SanitizePossibleUsernames(PasswordFormManager* p, PasswordForm* form) { |
| 133 p->SanitizePossibleUsernames(form); | 126 p->SanitizePossibleUsernames(form); |
| 134 } | 127 } |
| 135 | 128 |
| 136 bool IgnoredResult(PasswordFormManager* p, PasswordForm* form) { | 129 bool IgnoredResult(PasswordFormManager* p, PasswordForm* form) { |
| 137 return p->IgnoreResult(*form); | 130 return p->IgnoreResult(*form); |
| 138 } | 131 } |
| 139 | 132 |
| 140 Profile* profile() { return profile_; } | 133 Profile* profile() { return &profile_; } |
| 141 | 134 |
| 142 PasswordForm* observed_form() { return &observed_form_; } | 135 PasswordForm* observed_form() { return &observed_form_; } |
| 143 PasswordForm* saved_match() { return &saved_match_; } | 136 PasswordForm* saved_match() { return &saved_match_; } |
| 144 PasswordForm* CreateSavedMatch(bool blacklisted) { | 137 PasswordForm* CreateSavedMatch(bool blacklisted) { |
| 145 // Owned by the caller of this method. | 138 // Owned by the caller of this method. |
| 146 PasswordForm* match = new PasswordForm(saved_match_); | 139 PasswordForm* match = new PasswordForm(saved_match_); |
| 147 match->blacklisted_by_user = blacklisted; | 140 match->blacklisted_by_user = blacklisted; |
| 148 return match; | 141 return match; |
| 149 } | 142 } |
| 150 | 143 |
| 151 private: | 144 private: |
| 145 content::TestBrowserThreadBundle thread_bundle_; |
| 146 TestingProfile profile_; |
| 152 PasswordForm observed_form_; | 147 PasswordForm observed_form_; |
| 153 PasswordForm saved_match_; | 148 PasswordForm saved_match_; |
| 154 Profile* profile_; | |
| 155 }; | 149 }; |
| 156 | 150 |
| 157 TEST_F(PasswordFormManagerTest, TestNewLogin) { | 151 TEST_F(PasswordFormManagerTest, TestNewLogin) { |
| 158 PasswordFormManager* manager = new PasswordFormManager( | 152 PasswordFormManager* manager = new PasswordFormManager( |
| 159 profile(), NULL, NULL, *observed_form(), false); | 153 profile(), NULL, NULL, *observed_form(), false); |
| 160 SimulateMatchingPhase(manager, false); | 154 SimulateMatchingPhase(manager, false); |
| 161 // User submits credentials for the observed form. | 155 // User submits credentials for the observed form. |
| 162 PasswordForm credentials = *observed_form(); | 156 PasswordForm credentials = *observed_form(); |
| 163 credentials.username_value = saved_match()->username_value; | 157 credentials.username_value = saved_match()->username_value; |
| 164 credentials.password_value = saved_match()->password_value; | 158 credentials.password_value = saved_match()->password_value; |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 login, | 320 login, |
| 327 PasswordFormManager::IGNORE_OTHER_POSSIBLE_USERNAMES); | 321 PasswordFormManager::IGNORE_OTHER_POSSIBLE_USERNAMES); |
| 328 EXPECT_TRUE(manager->IsNewLogin()); | 322 EXPECT_TRUE(manager->IsNewLogin()); |
| 329 // Check that the provisionally saved action URL is the same as the submitted | 323 // Check that the provisionally saved action URL is the same as the submitted |
| 330 // action URL, not the one observed on page load. | 324 // action URL, not the one observed on page load. |
| 331 EXPECT_EQ(new_action, | 325 EXPECT_EQ(new_action, |
| 332 GetPendingCredentials(manager.get())->action); | 326 GetPendingCredentials(manager.get())->action); |
| 333 } | 327 } |
| 334 | 328 |
| 335 TEST_F(PasswordFormManagerTest, TestAlternateUsername) { | 329 TEST_F(PasswordFormManagerTest, TestAlternateUsername) { |
| 336 // Need a MessageLoop for callbacks. | |
| 337 base::MessageLoop message_loop; | |
| 338 PasswordStoreFactory::GetInstance()->SetTestingFactory( | 330 PasswordStoreFactory::GetInstance()->SetTestingFactory( |
| 339 profile(), &TestPasswordStore::Create); | 331 profile(), &TestPasswordStore::Create); |
| 340 scoped_refptr<TestPasswordStore> password_store = | 332 scoped_refptr<TestPasswordStore> password_store = |
| 341 static_cast<TestPasswordStore*>( | 333 static_cast<TestPasswordStore*>( |
| 342 PasswordStoreFactory::GetForProfile(profile(), | 334 PasswordStoreFactory::GetForProfile(profile(), |
| 343 Profile::IMPLICIT_ACCESS).get()); | 335 Profile::IMPLICIT_ACCESS).get()); |
| 344 TestPasswordManagerDelegate delegate(profile()); | 336 TestPasswordManagerDelegate delegate(profile()); |
| 345 TestPasswordManager password_manager(&delegate); | 337 TestPasswordManager password_manager(&delegate); |
| 346 scoped_ptr<TestPasswordFormManager> manager(new TestPasswordFormManager( | 338 scoped_ptr<TestPasswordFormManager> manager(new TestPasswordFormManager( |
| 347 profile(), &password_manager, *observed_form(), false)); | 339 profile(), &password_manager, *observed_form(), false)); |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 | 529 |
| 538 SanitizePossibleUsernames(manager.get(), &credentials); | 530 SanitizePossibleUsernames(manager.get(), &credentials); |
| 539 | 531 |
| 540 // SSN, duplicate in |other_possible_usernames| and duplicate of | 532 // SSN, duplicate in |other_possible_usernames| and duplicate of |
| 541 // |username_value| all removed. | 533 // |username_value| all removed. |
| 542 expected.clear(); | 534 expected.clear(); |
| 543 expected.push_back(ASCIIToUTF16("duplicate")); | 535 expected.push_back(ASCIIToUTF16("duplicate")); |
| 544 expected.push_back(ASCIIToUTF16("random")); | 536 expected.push_back(ASCIIToUTF16("random")); |
| 545 EXPECT_THAT(credentials.other_possible_usernames, Eq(expected)); | 537 EXPECT_THAT(credentials.other_possible_usernames, Eq(expected)); |
| 546 } | 538 } |
| OLD | NEW |