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 "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/password_manager/chrome_password_manager_client.h" | |
10 #include "chrome/browser/password_manager/password_generation_manager.h" | 9 #include "chrome/browser/password_manager/password_generation_manager.h" |
11 #include "chrome/browser/password_manager/password_manager.h" | 10 #include "chrome/browser/password_manager/password_manager.h" |
12 #include "chrome/browser/sync/profile_sync_service.h" | 11 #include "chrome/browser/password_manager/password_manager_client.h" |
13 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
14 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
16 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
17 #include "components/autofill/core/browser/autofill_field.h" | 15 #include "components/autofill/core/browser/autofill_field.h" |
18 #include "components/autofill/core/browser/autofill_metrics.h" | 16 #include "components/autofill/core/browser/autofill_metrics.h" |
19 #include "components/autofill/core/browser/form_structure.h" | 17 #include "components/autofill/core/browser/form_structure.h" |
20 #include "components/autofill/core/common/form_data.h" | 18 #include "components/autofill/core/common/form_data.h" |
21 #include "components/autofill/core/common/form_field_data.h" | 19 #include "components/autofill/core/common/form_field_data.h" |
22 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "url/gurl.h" | 22 #include "url/gurl.h" |
25 | 23 |
26 using base::ASCIIToUTF16; | 24 using base::ASCIIToUTF16; |
27 | 25 |
28 namespace { | 26 namespace { |
29 | 27 |
| 28 class TestPasswordManagerDriver : public PasswordManagerDriver { |
| 29 public: |
| 30 TestPasswordManagerDriver(content::WebContents* web_contents, |
| 31 PasswordManagerClient* client) |
| 32 : password_manager_(client), |
| 33 password_generation_manager_(web_contents, client), |
| 34 is_off_the_record_(false) {} |
| 35 virtual ~TestPasswordManagerDriver() {} |
| 36 |
| 37 // PasswordManagerDriver implementation. |
| 38 virtual void FillPasswordForm(const autofill::PasswordFormFillData& form_data) |
| 39 OVERRIDE {} |
| 40 virtual bool DidLastPageLoadEncounterSSLErrors() OVERRIDE { return false; } |
| 41 virtual bool IsOffTheRecord() OVERRIDE { return is_off_the_record_; } |
| 42 virtual PasswordGenerationManager* GetPasswordGenerationManager() OVERRIDE { |
| 43 return &password_generation_manager_; |
| 44 } |
| 45 virtual PasswordManager* GetPasswordManager() OVERRIDE { |
| 46 return &password_manager_; |
| 47 } |
| 48 virtual autofill::AutofillManager* GetAutofillManager() OVERRIDE { |
| 49 return NULL; |
| 50 } |
| 51 virtual void AllowPasswordGenerationForForm(autofill::PasswordForm* form) |
| 52 OVERRIDE {} |
| 53 virtual void AccountCreationFormsFound( |
| 54 const std::vector<autofill::FormData>& forms) OVERRIDE { |
| 55 found_account_creation_forms_.insert( |
| 56 found_account_creation_forms_.begin(), forms.begin(), forms.end()); |
| 57 } |
| 58 |
| 59 const std::vector<autofill::FormData>& GetFoundAccountCreationForms() { |
| 60 return found_account_creation_forms_; |
| 61 } |
| 62 void set_is_off_the_record(bool is_off_the_record) { |
| 63 is_off_the_record_ = is_off_the_record; |
| 64 } |
| 65 |
| 66 private: |
| 67 PasswordManager password_manager_; |
| 68 PasswordGenerationManager password_generation_manager_; |
| 69 std::vector<autofill::FormData> found_account_creation_forms_; |
| 70 bool is_off_the_record_; |
| 71 }; |
| 72 |
| 73 class TestPasswordManagerClient : public PasswordManagerClient { |
| 74 public: |
| 75 explicit TestPasswordManagerClient(content::WebContents* web_contents, |
| 76 Profile* profile) |
| 77 : profile_(profile), |
| 78 driver_(web_contents, this), |
| 79 is_sync_enabled_(false) {} |
| 80 |
| 81 virtual void PromptUserToSavePassword(PasswordFormManager* form_to_save) |
| 82 OVERRIDE {} |
| 83 virtual PasswordStore* GetPasswordStore() OVERRIDE { return NULL; } |
| 84 virtual PrefService* GetPrefs() OVERRIDE { return profile_->GetPrefs(); } |
| 85 virtual PasswordManagerDriver* GetDriver() OVERRIDE { return &driver_; } |
| 86 virtual void AuthenticateAutofillAndFillForm( |
| 87 scoped_ptr<autofill::PasswordFormFillData> fill_data) OVERRIDE {} |
| 88 virtual bool IsPasswordSyncEnabled() OVERRIDE { return is_sync_enabled_; } |
| 89 |
| 90 void set_is_password_sync_enabled(bool enabled) { |
| 91 is_sync_enabled_ = enabled; |
| 92 } |
| 93 |
| 94 private: |
| 95 Profile* profile_; |
| 96 TestPasswordManagerDriver driver_; |
| 97 bool is_sync_enabled_; |
| 98 }; |
| 99 |
30 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, | 100 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, |
31 // which are handy for briefer test code. The AutofillMetrics class is | 101 // which are handy for briefer test code. The AutofillMetrics class is |
32 // stateless, so this is safe. | 102 // stateless, so this is safe. |
33 class TestAutofillMetrics : public autofill::AutofillMetrics { | 103 class TestAutofillMetrics : public autofill::AutofillMetrics { |
34 public: | 104 public: |
35 TestAutofillMetrics() {} | 105 TestAutofillMetrics() {} |
36 virtual ~TestAutofillMetrics() {} | 106 virtual ~TestAutofillMetrics() {} |
37 }; | 107 }; |
38 | 108 |
39 } // anonymous namespace | 109 } // anonymous namespace |
40 | 110 |
41 class TestPasswordGenerationManager : public PasswordGenerationManager { | |
42 public: | |
43 explicit TestPasswordGenerationManager(content::WebContents* contents) | |
44 : PasswordGenerationManager( | |
45 contents, | |
46 ChromePasswordManagerClient::FromWebContents(contents)) {} | |
47 virtual ~TestPasswordGenerationManager() {} | |
48 | |
49 virtual void SendAccountCreationFormsToRenderer( | |
50 content::RenderViewHost* host, | |
51 const std::vector<autofill::FormData>& forms) OVERRIDE { | |
52 sent_account_creation_forms_.insert( | |
53 sent_account_creation_forms_.begin(), forms.begin(), forms.end()); | |
54 } | |
55 | |
56 const std::vector<autofill::FormData>& GetSentAccountCreationForms() { | |
57 return sent_account_creation_forms_; | |
58 } | |
59 | |
60 void ClearSentAccountCreationForms() { | |
61 sent_account_creation_forms_.clear(); | |
62 } | |
63 | |
64 private: | |
65 std::vector<autofill::FormData> sent_account_creation_forms_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager); | |
68 }; | |
69 | |
70 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { | 111 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { |
71 protected: | 112 protected: |
72 virtual void SetUp() OVERRIDE { | 113 virtual void SetUp() OVERRIDE { |
73 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | 114 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
74 ChromeRenderViewHostTestHarness::SetUp(); | 115 ChromeRenderViewHostTestHarness::SetUp(); |
75 | 116 |
76 ChromePasswordManagerClient::CreateForWebContents(web_contents()); | 117 client_.reset(new TestPasswordManagerClient(web_contents(), profile())); |
77 password_generation_manager_.reset( | |
78 new TestPasswordGenerationManager(web_contents())); | |
79 } | 118 } |
80 | 119 |
81 virtual void TearDown() OVERRIDE { | 120 virtual void TearDown() OVERRIDE { |
| 121 client_.reset(); |
82 ChromeRenderViewHostTestHarness::TearDown(); | 122 ChromeRenderViewHostTestHarness::TearDown(); |
83 } | 123 } |
84 | 124 |
| 125 PasswordGenerationManager* GetGenerationManager() { |
| 126 return client_->GetDriver()->GetPasswordGenerationManager(); |
| 127 } |
| 128 |
| 129 TestPasswordManagerDriver* GetTestDriver() { |
| 130 return static_cast<TestPasswordManagerDriver*>(client_->GetDriver()); |
| 131 } |
| 132 |
85 bool IsGenerationEnabled() { | 133 bool IsGenerationEnabled() { |
86 return password_generation_manager_->IsGenerationEnabled(); | 134 return GetGenerationManager()->IsGenerationEnabled(); |
87 } | 135 } |
88 | 136 |
89 void DetectAccountCreationForms( | 137 void DetectAccountCreationForms( |
90 const std::vector<autofill::FormStructure*>& forms) { | 138 const std::vector<autofill::FormStructure*>& forms) { |
91 password_generation_manager_->DetectAccountCreationForms(forms); | 139 GetGenerationManager()->DetectAccountCreationForms(forms); |
92 } | 140 } |
93 | 141 |
94 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_; | 142 scoped_ptr<TestPasswordManagerClient> client_; |
95 }; | |
96 | |
97 class IncognitoPasswordGenerationManagerTest : | |
98 public PasswordGenerationManagerTest { | |
99 public: | |
100 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { | |
101 // Create an incognito profile. | |
102 TestingProfile::Builder builder; | |
103 builder.SetIncognito(); | |
104 scoped_ptr<TestingProfile> profile = builder.Build(); | |
105 return profile.release(); | |
106 } | |
107 }; | 143 }; |
108 | 144 |
109 TEST_F(PasswordGenerationManagerTest, IsGenerationEnabled) { | 145 TEST_F(PasswordGenerationManagerTest, IsGenerationEnabled) { |
110 PrefService* prefs = profile()->GetPrefs(); | 146 // Enabling the PasswordManager and password sync should cause generation to |
111 | 147 // be enabled. |
112 // Enable syncing. Generation should be enabled. | 148 PrefService* prefs = client_->GetPrefs(); |
113 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); | 149 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true); |
114 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile( | 150 client_->set_is_password_sync_enabled(true); |
115 profile()); | |
116 sync_service->SetSyncSetupCompleted(); | |
117 syncer::ModelTypeSet preferred_set; | |
118 preferred_set.Put(syncer::PASSWORDS); | |
119 sync_service->ChangePreferredDataTypes(preferred_set); | |
120 EXPECT_TRUE(IsGenerationEnabled()); | 151 EXPECT_TRUE(IsGenerationEnabled()); |
121 | 152 |
122 // Change syncing preferences to not include passwords. Generation should | 153 // Disabling password syncing should cause generation to be disabled. |
123 // be disabled. | 154 client_->set_is_password_sync_enabled(false); |
124 preferred_set.Put(syncer::EXTENSIONS); | |
125 preferred_set.Remove(syncer::PASSWORDS); | |
126 sync_service->ChangePreferredDataTypes(preferred_set); | |
127 EXPECT_FALSE(IsGenerationEnabled()); | 155 EXPECT_FALSE(IsGenerationEnabled()); |
128 | 156 |
129 // Disable syncing. Generation should also be disabled. | 157 // Disabling the PasswordManager should cause generation to be disabled even |
130 sync_service->DisableForUser(); | 158 // if syncing is enabled. |
| 159 prefs->SetBoolean(prefs::kPasswordManagerEnabled, false); |
| 160 client_->set_is_password_sync_enabled(true); |
131 EXPECT_FALSE(IsGenerationEnabled()); | 161 EXPECT_FALSE(IsGenerationEnabled()); |
132 } | 162 } |
133 | 163 |
134 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) { | 164 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) { |
135 // Setup so that IsGenerationEnabled() returns true. | 165 // Setup so that IsGenerationEnabled() returns true. |
136 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile( | 166 PrefService* prefs = client_->GetPrefs(); |
137 profile()); | 167 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true); |
138 sync_service->SetSyncSetupCompleted(); | 168 client_->set_is_password_sync_enabled(true); |
139 | 169 |
140 autofill::FormData login_form; | 170 autofill::FormData login_form; |
141 login_form.origin = GURL("http://www.yahoo.com/login/"); | 171 login_form.origin = GURL("http://www.yahoo.com/login/"); |
142 autofill::FormFieldData username; | 172 autofill::FormFieldData username; |
143 username.label = ASCIIToUTF16("username"); | 173 username.label = ASCIIToUTF16("username"); |
144 username.name = ASCIIToUTF16("login"); | 174 username.name = ASCIIToUTF16("login"); |
145 username.form_control_type = "text"; | 175 username.form_control_type = "text"; |
146 login_form.fields.push_back(username); | 176 login_form.fields.push_back(username); |
147 autofill::FormFieldData password; | 177 autofill::FormFieldData password; |
148 password.label = ASCIIToUTF16("password"); | 178 password.label = ASCIIToUTF16("password"); |
(...skipping 23 matching lines...) Expand all Loading... |
172 "<field autofilltype=\"9\" />" | 202 "<field autofilltype=\"9\" />" |
173 "<field autofilltype=\"76\" />" | 203 "<field autofilltype=\"76\" />" |
174 "<field autofilltype=\"75\" />" | 204 "<field autofilltype=\"75\" />" |
175 "</autofillqueryresponse>"; | 205 "</autofillqueryresponse>"; |
176 autofill::FormStructure::ParseQueryResponse( | 206 autofill::FormStructure::ParseQueryResponse( |
177 kServerResponse, | 207 kServerResponse, |
178 forms, | 208 forms, |
179 TestAutofillMetrics()); | 209 TestAutofillMetrics()); |
180 | 210 |
181 DetectAccountCreationForms(forms); | 211 DetectAccountCreationForms(forms); |
182 EXPECT_EQ(1u, | 212 EXPECT_EQ(1u, GetTestDriver()->GetFoundAccountCreationForms().size()); |
183 password_generation_manager_->GetSentAccountCreationForms().size()); | 213 EXPECT_EQ(GURL("http://accounts.yahoo.com/"), |
184 EXPECT_EQ( | 214 GetTestDriver()->GetFoundAccountCreationForms()[0].origin); |
185 GURL("http://accounts.yahoo.com/"), | |
186 password_generation_manager_->GetSentAccountCreationForms()[0].origin); | |
187 } | 215 } |
188 | 216 |
189 TEST_F(IncognitoPasswordGenerationManagerTest, | 217 TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) { |
190 UpdatePasswordSyncStateIncognito) { | 218 // Disable password manager by going incognito. Even though password |
191 // Disable password manager by going incognito. Even though syncing is | 219 // syncing is enabled, generation should still |
192 // enabled, generation should still be disabled. | 220 // be disabled. |
193 PrefService* prefs = profile()->GetPrefs(); | 221 GetTestDriver()->set_is_off_the_record(true); |
| 222 PrefService* prefs = client_->GetPrefs(); |
| 223 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true); |
| 224 client_->set_is_password_sync_enabled(true); |
194 | 225 |
195 // Allow this test to control what should get synced. | |
196 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); | |
197 | |
198 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); | |
199 sync_prefs.SetSyncSetupCompleted(); | |
200 EXPECT_FALSE(IsGenerationEnabled()); | 226 EXPECT_FALSE(IsGenerationEnabled()); |
201 } | 227 } |
OLD | NEW |