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/chrome_password_manager_client.h"
10 #include "chrome/browser/password_manager/password_generation_manager.h"
11 #include "chrome/browser/password_manager/password_manager.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(
46 ChromePasswordManagerClient::FromWebContents(contents
)) {}
47 virtual ~TestPasswordGenerationManager() {}
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());
56 const std::vector
<autofill::FormData
>& GetSentAccountCreationForms() {
57 return sent_account_creation_forms_
;
60 void ClearSentAccountCreationForms() {
61 sent_account_creation_forms_
.clear();
65 std::vector
<autofill::FormData
> sent_account_creation_forms_
;
67 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager
);
70 class PasswordGenerationManagerTest
: public ChromeRenderViewHostTestHarness
{
72 virtual void SetUp() OVERRIDE
{
73 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD
);
74 ChromeRenderViewHostTestHarness::SetUp();
76 ChromePasswordManagerClient::CreateForWebContents(web_contents());
77 password_generation_manager_
.reset(
78 new TestPasswordGenerationManager(web_contents()));
81 virtual void TearDown() OVERRIDE
{
82 ChromeRenderViewHostTestHarness::TearDown();
85 bool IsGenerationEnabled() {
86 return password_generation_manager_
->IsGenerationEnabled();
89 void DetectAccountCreationForms(
90 const std::vector
<autofill::FormStructure
*>& forms
) {
91 password_generation_manager_
->DetectAccountCreationForms(forms
);
94 scoped_ptr
<TestPasswordGenerationManager
> password_generation_manager_
;
97 class IncognitoPasswordGenerationManagerTest
:
98 public PasswordGenerationManagerTest
{
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();
109 TEST_F(PasswordGenerationManagerTest
, IsGenerationEnabled
) {
110 PrefService
* prefs
= profile()->GetPrefs();
112 // Enable syncing. Generation should be enabled.
113 prefs
->SetBoolean(prefs::kSyncKeepEverythingSynced
, false);
114 ProfileSyncService
* sync_service
= ProfileSyncServiceFactory::GetForProfile(
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());
122 // Change syncing preferences to not include passwords. Generation should
124 preferred_set
.Put(syncer::EXTENSIONS
);
125 preferred_set
.Remove(syncer::PASSWORDS
);
126 sync_service
->ChangePreferredDataTypes(preferred_set
);
127 EXPECT_FALSE(IsGenerationEnabled());
129 // Disable syncing. Generation should also be disabled.
130 sync_service
->DisableForUser();
131 EXPECT_FALSE(IsGenerationEnabled());
134 TEST_F(PasswordGenerationManagerTest
, DetectAccountCreationForms
) {
135 // Setup so that IsGenerationEnabled() returns true.
136 ProfileSyncService
* sync_service
= ProfileSyncServiceFactory::GetForProfile(
138 sync_service
->SetSyncSetupCompleted();
140 autofill::FormData login_form
;
141 login_form
.origin
= GURL("http://www.yahoo.com/login/");
142 autofill::FormFieldData username
;
143 username
.label
= ASCIIToUTF16("username");
144 username
.name
= ASCIIToUTF16("login");
145 username
.form_control_type
= "text";
146 login_form
.fields
.push_back(username
);
147 autofill::FormFieldData password
;
148 password
.label
= ASCIIToUTF16("password");
149 password
.name
= ASCIIToUTF16("password");
150 password
.form_control_type
= "password";
151 login_form
.fields
.push_back(password
);
152 autofill::FormStructure
form1(login_form
);
153 std::vector
<autofill::FormStructure
*> forms
;
154 forms
.push_back(&form1
);
155 autofill::FormData account_creation_form
;
156 account_creation_form
.origin
= GURL("http://accounts.yahoo.com/");
157 account_creation_form
.fields
.push_back(username
);
158 account_creation_form
.fields
.push_back(password
);
159 autofill::FormFieldData confirm_password
;
160 confirm_password
.label
= ASCIIToUTF16("confirm_password");
161 confirm_password
.name
= ASCIIToUTF16("password");
162 confirm_password
.form_control_type
= "password";
163 account_creation_form
.fields
.push_back(confirm_password
);
164 autofill::FormStructure
form2(account_creation_form
);
165 forms
.push_back(&form2
);
167 // Simulate the server response to set the field types.
168 const char* const kServerResponse
=
169 "<autofillqueryresponse>"
170 "<field autofilltype=\"9\" />"
171 "<field autofilltype=\"75\" />"
172 "<field autofilltype=\"9\" />"
173 "<field autofilltype=\"76\" />"
174 "<field autofilltype=\"75\" />"
175 "</autofillqueryresponse>";
176 autofill::FormStructure::ParseQueryResponse(
179 TestAutofillMetrics());
181 DetectAccountCreationForms(forms
);
183 password_generation_manager_
->GetSentAccountCreationForms().size());
185 GURL("http://accounts.yahoo.com/"),
186 password_generation_manager_
->GetSentAccountCreationForms()[0].origin
);
189 TEST_F(IncognitoPasswordGenerationManagerTest
,
190 UpdatePasswordSyncStateIncognito
) {
191 // Disable password manager by going incognito. Even though syncing is
192 // enabled, generation should still be disabled.
193 PrefService
* prefs
= profile()->GetPrefs();
195 // Allow this test to control what should get synced.
196 prefs
->SetBoolean(prefs::kSyncKeepEverythingSynced
, false);
198 browser_sync::SyncPrefs
sync_prefs(profile()->GetPrefs());
199 sync_prefs
.SetSyncSetupCompleted();
200 EXPECT_FALSE(IsGenerationEnabled());