| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/password_manager/password_generation_manager.h" | 9 #include "chrome/browser/password_manager/password_generation_manager.h" |
| 9 #include "chrome/browser/password_manager/password_manager.h" | 10 #include "chrome/browser/password_manager/password_manager.h" |
| 10 #include "chrome/browser/password_manager/password_manager_delegate_impl.h" | 11 #include "chrome/browser/password_manager/password_manager_delegate_impl.h" |
| 11 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
| 12 #include "chrome/browser/sync/profile_sync_service_factory.h" | 13 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/autofill/content/browser/autocheckout_page_meta_data.h" |
| 18 #include "components/autofill/core/browser/autofill_field.h" |
| 19 #include "components/autofill/core/browser/autofill_metrics.h" |
| 20 #include "components/autofill/core/browser/form_structure.h" |
| 21 #include "components/autofill/core/common/form_data.h" |
| 22 #include "components/autofill/core/common/form_field_data.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "url/gurl.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, |
| 30 // which are handy for briefer test code. The AutofillMetrics class is |
| 31 // stateless, so this is safe. |
| 32 class TestAutofillMetrics : public autofill::AutofillMetrics { |
| 33 public: |
| 34 TestAutofillMetrics() {} |
| 35 virtual ~TestAutofillMetrics() {} |
| 36 }; |
| 37 |
| 38 } // anonymous namespace |
| 18 | 39 |
| 19 class TestPasswordGenerationManager : public PasswordGenerationManager { | 40 class TestPasswordGenerationManager : public PasswordGenerationManager { |
| 20 public: | 41 public: |
| 21 explicit TestPasswordGenerationManager(content::WebContents* contents) | 42 explicit TestPasswordGenerationManager(content::WebContents* contents) |
| 22 : PasswordGenerationManager(contents) {} | 43 : PasswordGenerationManager(contents) {} |
| 23 virtual ~TestPasswordGenerationManager() {} | 44 virtual ~TestPasswordGenerationManager() {} |
| 24 | 45 |
| 25 virtual void SendStateToRenderer(content::RenderViewHost* host, | 46 virtual void SendStateToRenderer(content::RenderViewHost* host, |
| 26 bool enabled) OVERRIDE { | 47 bool enabled) OVERRIDE { |
| 27 sent_states_.push_back(enabled); | 48 sent_states_.push_back(enabled); |
| 28 } | 49 } |
| 29 | 50 |
| 51 virtual void SendAccountCreationFormsToRenderer( |
| 52 content::RenderViewHost* host, |
| 53 const std::vector<autofill::FormData>& forms) OVERRIDE { |
| 54 sent_account_creation_forms_.insert( |
| 55 sent_account_creation_forms_.begin(), forms.begin(), forms.end()); |
| 56 } |
| 57 |
| 30 const std::vector<bool>& GetSentStates() { | 58 const std::vector<bool>& GetSentStates() { |
| 31 return sent_states_; | 59 return sent_states_; |
| 32 } | 60 } |
| 33 | 61 |
| 62 const std::vector<autofill::FormData>& GetSentAccountCreationForms() { |
| 63 return sent_account_creation_forms_; |
| 64 } |
| 65 |
| 34 void ClearSentStates() { | 66 void ClearSentStates() { |
| 35 sent_states_.clear(); | 67 sent_states_.clear(); |
| 36 } | 68 } |
| 37 | 69 |
| 70 void ClearSentAccountCreationForms() { |
| 71 sent_account_creation_forms_.clear(); |
| 72 } |
| 73 |
| 38 private: | 74 private: |
| 39 std::vector<bool> sent_states_; | 75 std::vector<bool> sent_states_; |
| 76 std::vector<autofill::FormData> sent_account_creation_forms_; |
| 40 | 77 |
| 41 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager); | 78 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager); |
| 42 }; | 79 }; |
| 43 | 80 |
| 44 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { | 81 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { |
| 45 protected: | 82 protected: |
| 46 virtual void SetUp() OVERRIDE { | 83 virtual void SetUp() OVERRIDE { |
| 47 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | 84 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 48 ChromeRenderViewHostTestHarness::SetUp(); | 85 ChromeRenderViewHostTestHarness::SetUp(); |
| 49 | 86 |
| 50 password_generation_manager_.reset( | 87 password_generation_manager_.reset( |
| 51 new TestPasswordGenerationManager(web_contents())); | 88 new TestPasswordGenerationManager(web_contents())); |
| 52 } | 89 } |
| 53 | 90 |
| 54 virtual void TearDown() OVERRIDE { | 91 virtual void TearDown() OVERRIDE { |
| 55 ChromeRenderViewHostTestHarness::TearDown(); | 92 ChromeRenderViewHostTestHarness::TearDown(); |
| 56 } | 93 } |
| 57 | 94 |
| 58 void UpdateState(bool new_renderer) { | 95 void UpdateState(bool new_renderer) { |
| 59 password_generation_manager_->UpdateState(NULL, new_renderer); | 96 password_generation_manager_->UpdateState(NULL, new_renderer); |
| 60 } | 97 } |
| 61 | 98 |
| 99 void DetectAccountCreationForms( |
| 100 const std::vector<autofill::FormStructure*>& forms) { |
| 101 password_generation_manager_->DetectAccountCreationForms(forms); |
| 102 } |
| 103 |
| 62 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_; | 104 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_; |
| 63 }; | 105 }; |
| 64 | 106 |
| 65 class IncognitoPasswordGenerationManagerTest : | 107 class IncognitoPasswordGenerationManagerTest : |
| 66 public PasswordGenerationManagerTest { | 108 public PasswordGenerationManagerTest { |
| 67 public: | 109 public: |
| 68 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { | 110 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { |
| 69 // Create an incognito profile. | 111 // Create an incognito profile. |
| 70 TestingProfile::Builder builder; | 112 TestingProfile::Builder builder; |
| 71 scoped_ptr<TestingProfile> profile = builder.Build(); | 113 scoped_ptr<TestingProfile> profile = builder.Build(); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 password_generation_manager_->ClearSentStates(); | 215 password_generation_manager_->ClearSentStates(); |
| 174 | 216 |
| 175 // When a new render_view is created, we send the state even if it's the | 217 // When a new render_view is created, we send the state even if it's the |
| 176 // same. | 218 // same. |
| 177 UpdateState(true); | 219 UpdateState(true); |
| 178 EXPECT_EQ(1u, password_generation_manager_->GetSentStates().size()); | 220 EXPECT_EQ(1u, password_generation_manager_->GetSentStates().size()); |
| 179 EXPECT_FALSE(password_generation_manager_->GetSentStates()[0]); | 221 EXPECT_FALSE(password_generation_manager_->GetSentStates()[0]); |
| 180 password_generation_manager_->ClearSentStates(); | 222 password_generation_manager_->ClearSentStates(); |
| 181 } | 223 } |
| 182 | 224 |
| 225 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) { |
| 226 autofill::FormData login_form; |
| 227 login_form.origin = GURL("http://www.yahoo.com/login/"); |
| 228 autofill::FormFieldData username; |
| 229 username.label = ASCIIToUTF16("username"); |
| 230 username.name = ASCIIToUTF16("login"); |
| 231 username.form_control_type = "text"; |
| 232 login_form.fields.push_back(username); |
| 233 autofill::FormFieldData password; |
| 234 password.label = ASCIIToUTF16("password"); |
| 235 password.name = ASCIIToUTF16("password"); |
| 236 password.form_control_type = "password"; |
| 237 login_form.fields.push_back(password); |
| 238 autofill::FormStructure form1(login_form, std::string()); |
| 239 std::vector<autofill::FormStructure*> forms; |
| 240 forms.push_back(&form1); |
| 241 autofill::FormData account_creation_form; |
| 242 account_creation_form.origin = GURL("http://accounts.yahoo.com/"); |
| 243 account_creation_form.fields.push_back(username); |
| 244 account_creation_form.fields.push_back(password); |
| 245 autofill::FormFieldData confirm_password; |
| 246 confirm_password.label = ASCIIToUTF16("confirm_password"); |
| 247 confirm_password.name = ASCIIToUTF16("password"); |
| 248 confirm_password.form_control_type = "password"; |
| 249 account_creation_form.fields.push_back(confirm_password); |
| 250 autofill::FormStructure form2(account_creation_form, std::string()); |
| 251 forms.push_back(&form2); |
| 252 |
| 253 // Simulate the server response to set the field types. |
| 254 autofill::AutocheckoutPageMetaData page_meta_data; |
| 255 const char* const kServerResponse = |
| 256 "<autofillqueryresponse>" |
| 257 "<field autofilltype=\"9\" />" |
| 258 "<field autofilltype=\"75\" />" |
| 259 "<field autofilltype=\"9\" />" |
| 260 "<field autofilltype=\"76\" />" |
| 261 "<field autofilltype=\"75\" />" |
| 262 "</autofillqueryresponse>"; |
| 263 autofill::FormStructure::ParseQueryResponse( |
| 264 kServerResponse, |
| 265 forms, |
| 266 &page_meta_data, |
| 267 TestAutofillMetrics()); |
| 268 |
| 269 DetectAccountCreationForms(forms); |
| 270 EXPECT_EQ(1u, |
| 271 password_generation_manager_->GetSentAccountCreationForms().size()); |
| 272 EXPECT_EQ( |
| 273 GURL("http://accounts.yahoo.com/"), |
| 274 password_generation_manager_->GetSentAccountCreationForms()[0].origin); |
| 275 } |
| 276 |
| 183 TEST_F(IncognitoPasswordGenerationManagerTest, | 277 TEST_F(IncognitoPasswordGenerationManagerTest, |
| 184 UpdatePasswordSyncStateIncognito) { | 278 UpdatePasswordSyncStateIncognito) { |
| 185 // Disable password manager by going incognito, and enable syncing. The | 279 // Disable password manager by going incognito, and enable syncing. The |
| 186 // feature should still be disabled, and nothing will be sent. | 280 // feature should still be disabled, and nothing will be sent. |
| 187 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); | 281 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); |
| 188 PasswordManager::CreateForWebContentsAndDelegate( | 282 PasswordManager::CreateForWebContentsAndDelegate( |
| 189 web_contents(), | 283 web_contents(), |
| 190 PasswordManagerDelegateImpl::FromWebContents(web_contents())); | 284 PasswordManagerDelegateImpl::FromWebContents(web_contents())); |
| 191 | 285 |
| 192 PrefService* prefs = profile()->GetPrefs(); | 286 PrefService* prefs = profile()->GetPrefs(); |
| 193 | 287 |
| 194 // Allow this test to control what should get synced. | 288 // Allow this test to control what should get synced. |
| 195 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); | 289 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); |
| 196 // Always set password generation enabled check box so we can test the | 290 // Always set password generation enabled check box so we can test the |
| 197 // behavior of password sync. | 291 // behavior of password sync. |
| 198 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); | 292 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); |
| 199 | 293 |
| 200 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); | 294 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); |
| 201 sync_prefs.SetSyncSetupCompleted(); | 295 sync_prefs.SetSyncSetupCompleted(); |
| 202 UpdateState(false); | 296 UpdateState(false); |
| 203 EXPECT_EQ(0u, password_generation_manager_->GetSentStates().size()); | 297 EXPECT_EQ(0u, password_generation_manager_->GetSentStates().size()); |
| 204 } | 298 } |
| OLD | NEW |