Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: chrome/browser/password_manager/password_generation_manager_unittest.cc

Issue 156173004: Abstract IPC send out of PasswordGenerationManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adjust PasswordGenerationManager unittests Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 {
Garrett Casto 2014/02/18 21:19:29 At some point we might want to make one Test/Mock
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 Profile* GetProfile() OVERRIDE { return profile_; }
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 }; 143 };
96 144
97 class IncognitoPasswordGenerationManagerTest : 145 class IncognitoPasswordGenerationManagerTest :
Garrett Casto 2014/02/18 21:19:29 Looks like having a separate class for this is no
blundell 2014/02/19 13:28:11 Done.
98 public PasswordGenerationManagerTest { 146 public PasswordGenerationManagerTest {
99 public: 147 public:
100 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { 148 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE {
101 // Create an incognito profile. 149 // Create an incognito profile.
102 TestingProfile::Builder builder; 150 TestingProfile::Builder builder;
103 builder.SetIncognito(); 151 builder.SetIncognito();
104 scoped_ptr<TestingProfile> profile = builder.Build(); 152 scoped_ptr<TestingProfile> profile = builder.Build();
105 return profile.release(); 153 return profile.release();
106 } 154 }
107 }; 155 };
108 156
109 TEST_F(PasswordGenerationManagerTest, IsGenerationEnabled) { 157 TEST_F(PasswordGenerationManagerTest, IsGenerationEnabled) {
110 PrefService* prefs = profile()->GetPrefs(); 158 // Enabling the PasswordManager and password sync should cause generation to
111 159 // be enabled.
112 // Enable syncing. Generation should be enabled. 160 PrefService* prefs = client_->GetPrefs();
113 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); 161 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true);
114 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile( 162 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()); 163 EXPECT_TRUE(IsGenerationEnabled());
121 164
122 // Change syncing preferences to not include passwords. Generation should 165 // Disabling password syncing should cause generation to be disabled.
123 // be disabled. 166 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()); 167 EXPECT_FALSE(IsGenerationEnabled());
128 168
129 // Disable syncing. Generation should also be disabled. 169 // Disabling the PasswordManager should cause generation to be disabled even
130 sync_service->DisableForUser(); 170 // if syncing is enabled.
171 prefs->SetBoolean(prefs::kPasswordManagerEnabled, false);
172 client_->set_is_password_sync_enabled(true);
131 EXPECT_FALSE(IsGenerationEnabled()); 173 EXPECT_FALSE(IsGenerationEnabled());
132 } 174 }
133 175
134 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) { 176 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) {
135 // Setup so that IsGenerationEnabled() returns true. 177 // Setup so that IsGenerationEnabled() returns true.
136 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile( 178 PrefService* prefs = client_->GetPrefs();
137 profile()); 179 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true);
138 sync_service->SetSyncSetupCompleted(); 180 client_->set_is_password_sync_enabled(true);
139 181
140 autofill::FormData login_form; 182 autofill::FormData login_form;
141 login_form.origin = GURL("http://www.yahoo.com/login/"); 183 login_form.origin = GURL("http://www.yahoo.com/login/");
142 autofill::FormFieldData username; 184 autofill::FormFieldData username;
143 username.label = ASCIIToUTF16("username"); 185 username.label = ASCIIToUTF16("username");
144 username.name = ASCIIToUTF16("login"); 186 username.name = ASCIIToUTF16("login");
145 username.form_control_type = "text"; 187 username.form_control_type = "text";
146 login_form.fields.push_back(username); 188 login_form.fields.push_back(username);
147 autofill::FormFieldData password; 189 autofill::FormFieldData password;
148 password.label = ASCIIToUTF16("password"); 190 password.label = ASCIIToUTF16("password");
(...skipping 23 matching lines...) Expand all
172 "<field autofilltype=\"9\" />" 214 "<field autofilltype=\"9\" />"
173 "<field autofilltype=\"76\" />" 215 "<field autofilltype=\"76\" />"
174 "<field autofilltype=\"75\" />" 216 "<field autofilltype=\"75\" />"
175 "</autofillqueryresponse>"; 217 "</autofillqueryresponse>";
176 autofill::FormStructure::ParseQueryResponse( 218 autofill::FormStructure::ParseQueryResponse(
177 kServerResponse, 219 kServerResponse,
178 forms, 220 forms,
179 TestAutofillMetrics()); 221 TestAutofillMetrics());
180 222
181 DetectAccountCreationForms(forms); 223 DetectAccountCreationForms(forms);
182 EXPECT_EQ(1u, 224 EXPECT_EQ(1u, GetTestDriver()->GetFoundAccountCreationForms().size());
183 password_generation_manager_->GetSentAccountCreationForms().size()); 225 EXPECT_EQ(GURL("http://accounts.yahoo.com/"),
184 EXPECT_EQ( 226 GetTestDriver()->GetFoundAccountCreationForms()[0].origin);
185 GURL("http://accounts.yahoo.com/"),
186 password_generation_manager_->GetSentAccountCreationForms()[0].origin);
187 } 227 }
188 228
189 TEST_F(IncognitoPasswordGenerationManagerTest, 229 TEST_F(IncognitoPasswordGenerationManagerTest,
190 UpdatePasswordSyncStateIncognito) { 230 UpdatePasswordSyncStateIncognito) {
191 // Disable password manager by going incognito. Even though syncing is 231 // Disable password manager by going incognito. Even though password
192 // enabled, generation should still be disabled. 232 // syncing is enabled, generation should still
193 PrefService* prefs = profile()->GetPrefs(); 233 // be disabled.
234 GetTestDriver()->set_is_off_the_record(true);
235 PrefService* prefs = client_->GetPrefs();
236 prefs->SetBoolean(prefs::kPasswordManagerEnabled, true);
237 client_->set_is_password_sync_enabled(true);
194 238
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()); 239 EXPECT_FALSE(IsGenerationEnabled());
201 } 240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698