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
3 // found in the LICENSE file.
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/password_manager/password_generation_manager.h"
10 #include "chrome/browser/password_manager/password_manager.h"
11 #include "chrome/browser/password_manager/password_manager_delegate_impl.h"
12 #include "chrome/browser/sync/profile_sync_service.h"
13 #include "chrome/browser/sync/profile_sync_service_factory.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/autofill/core/browser/autofill_field.h"
18 #include "components/autofill/core/browser/autofill_metrics.h"
19 #include "components/autofill/core/browser/form_structure.h"
20 #include "components/autofill/core/common/form_data.h"
21 #include "components/autofill/core/common/form_field_data.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 using base::ASCIIToUTF16
;
30 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
31 // which are handy for briefer test code. The AutofillMetrics class is
32 // stateless, so this is safe.
33 class TestAutofillMetrics
: public autofill::AutofillMetrics
{
35 TestAutofillMetrics() {}
36 virtual ~TestAutofillMetrics() {}
39 } // anonymous namespace
41 class TestPasswordGenerationManager
: public PasswordGenerationManager
{
43 explicit TestPasswordGenerationManager(content::WebContents
* contents
)
44 : PasswordGenerationManager(contents
) {}
45 virtual ~TestPasswordGenerationManager() {}
47 virtual void SendAccountCreationFormsToRenderer(
48 content::RenderViewHost
* host
,
49 const std::vector
<autofill::FormData
>& forms
) OVERRIDE
{
50 sent_account_creation_forms_
.insert(
51 sent_account_creation_forms_
.begin(), forms
.begin(), forms
.end());
54 const std::vector
<autofill::FormData
>& GetSentAccountCreationForms() {
55 return sent_account_creation_forms_
;
58 void ClearSentAccountCreationForms() {
59 sent_account_creation_forms_
.clear();
63 std::vector
<autofill::FormData
> sent_account_creation_forms_
;
65 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager
);
68 class PasswordGenerationManagerTest
: public ChromeRenderViewHostTestHarness
{
70 virtual void SetUp() OVERRIDE
{
71 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD
);
72 ChromeRenderViewHostTestHarness::SetUp();
74 password_generation_manager_
.reset(
75 new TestPasswordGenerationManager(web_contents()));
78 virtual void TearDown() OVERRIDE
{
79 ChromeRenderViewHostTestHarness::TearDown();
82 bool IsGenerationEnabled() {
83 return password_generation_manager_
->IsGenerationEnabled();
86 void DetectAccountCreationForms(
87 const std::vector
<autofill::FormStructure
*>& forms
) {
88 password_generation_manager_
->DetectAccountCreationForms(forms
);
91 scoped_ptr
<TestPasswordGenerationManager
> password_generation_manager_
;
94 class IncognitoPasswordGenerationManagerTest
:
95 public PasswordGenerationManagerTest
{
97 virtual content::BrowserContext
* CreateBrowserContext() OVERRIDE
{
98 // Create an incognito profile.
99 TestingProfile::Builder builder
;
100 builder
.SetIncognito();
101 scoped_ptr
<TestingProfile
> profile
= builder
.Build();
102 return profile
.release();
106 TEST_F(PasswordGenerationManagerTest
, IsGenerationEnabled
) {
107 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
108 PasswordManager::CreateForWebContentsAndDelegate(
110 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
112 PrefService
* prefs
= profile()->GetPrefs();
114 // Always set password sync enabled so we can test the behavior of password
116 prefs
->SetBoolean(prefs::kSyncKeepEverythingSynced
, false);
117 ProfileSyncService
* sync_service
= ProfileSyncServiceFactory::GetForProfile(
119 sync_service
->SetSyncSetupCompleted();
120 syncer::ModelTypeSet preferred_set
;
121 preferred_set
.Put(syncer::PASSWORDS
);
122 sync_service
->ChangePreferredDataTypes(preferred_set
);
124 // Pref is false, should not be enabled.
125 prefs
->SetBoolean(prefs::kPasswordGenerationEnabled
, false);
126 EXPECT_FALSE(IsGenerationEnabled());
128 // Pref is true, should be enabled.
129 prefs
->SetBoolean(prefs::kPasswordGenerationEnabled
, true);
130 EXPECT_TRUE(IsGenerationEnabled());
132 // Change syncing preferences to not include passwords. Generation should
134 preferred_set
.Put(syncer::EXTENSIONS
);
135 preferred_set
.Remove(syncer::PASSWORDS
);
136 sync_service
->ChangePreferredDataTypes(preferred_set
);
137 EXPECT_FALSE(IsGenerationEnabled());
139 // Disable syncing. Generation should also be disabled.
140 sync_service
->DisableForUser();
141 EXPECT_FALSE(IsGenerationEnabled());
144 TEST_F(PasswordGenerationManagerTest
, DetectAccountCreationForms
) {
145 // Setup so that IsGenerationEnabled() returns true.
146 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
147 PasswordManager::CreateForWebContentsAndDelegate(
149 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
151 ProfileSyncService
* sync_service
= ProfileSyncServiceFactory::GetForProfile(
153 sync_service
->SetSyncSetupCompleted();
155 profile()->GetPrefs()->SetBoolean(prefs::kPasswordGenerationEnabled
, true);
157 autofill::FormData login_form
;
158 login_form
.origin
= GURL("http://www.yahoo.com/login/");
159 autofill::FormFieldData username
;
160 username
.label
= ASCIIToUTF16("username");
161 username
.name
= ASCIIToUTF16("login");
162 username
.form_control_type
= "text";
163 login_form
.fields
.push_back(username
);
164 autofill::FormFieldData password
;
165 password
.label
= ASCIIToUTF16("password");
166 password
.name
= ASCIIToUTF16("password");
167 password
.form_control_type
= "password";
168 login_form
.fields
.push_back(password
);
169 autofill::FormStructure
form1(login_form
);
170 std::vector
<autofill::FormStructure
*> forms
;
171 forms
.push_back(&form1
);
172 autofill::FormData account_creation_form
;
173 account_creation_form
.origin
= GURL("http://accounts.yahoo.com/");
174 account_creation_form
.fields
.push_back(username
);
175 account_creation_form
.fields
.push_back(password
);
176 autofill::FormFieldData confirm_password
;
177 confirm_password
.label
= ASCIIToUTF16("confirm_password");
178 confirm_password
.name
= ASCIIToUTF16("password");
179 confirm_password
.form_control_type
= "password";
180 account_creation_form
.fields
.push_back(confirm_password
);
181 autofill::FormStructure
form2(account_creation_form
);
182 forms
.push_back(&form2
);
184 // Simulate the server response to set the field types.
185 const char* const kServerResponse
=
186 "<autofillqueryresponse>"
187 "<field autofilltype=\"9\" />"
188 "<field autofilltype=\"75\" />"
189 "<field autofilltype=\"9\" />"
190 "<field autofilltype=\"76\" />"
191 "<field autofilltype=\"75\" />"
192 "</autofillqueryresponse>";
193 autofill::FormStructure::ParseQueryResponse(
196 TestAutofillMetrics());
198 DetectAccountCreationForms(forms
);
200 password_generation_manager_
->GetSentAccountCreationForms().size());
202 GURL("http://accounts.yahoo.com/"),
203 password_generation_manager_
->GetSentAccountCreationForms()[0].origin
);
206 TEST_F(IncognitoPasswordGenerationManagerTest
,
207 UpdatePasswordSyncStateIncognito
) {
208 // Disable password manager by going incognito. Even though syncing is
209 // enabled, generation should still be disabled.
210 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
211 PasswordManager::CreateForWebContentsAndDelegate(
213 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
215 PrefService
* prefs
= profile()->GetPrefs();
217 // Allow this test to control what should get synced.
218 prefs
->SetBoolean(prefs::kSyncKeepEverythingSynced
, false);
219 // Always set password generation enabled check box so we can test the
220 // behavior of password sync.
221 prefs
->SetBoolean(prefs::kPasswordGenerationEnabled
, true);
223 browser_sync::SyncPrefs
sync_prefs(profile()->GetPrefs());
224 sync_prefs
.SetSyncSetupCompleted();
225 EXPECT_FALSE(IsGenerationEnabled());