Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / password_generation_manager_unittest.cc
1 // Copyright 2014 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.
4
5 #include <vector>
6
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/autofill_field.h"
12 #include "components/autofill/core/browser/autofill_metrics.h"
13 #include "components/autofill/core/browser/form_structure.h"
14 #include "components/autofill/core/common/form_data.h"
15 #include "components/autofill/core/common/form_field_data.h"
16 #include "components/password_manager/core/browser/password_autofill_manager.h"
17 #include "components/password_manager/core/browser/password_generation_manager.h"
18 #include "components/password_manager/core/browser/password_manager.h"
19 #include "components/password_manager/core/browser/stub_password_manager_client.h"
20 #include "components/password_manager/core/browser/stub_password_manager_driver.h"
21 #include "components/password_manager/core/browser/test_password_store.h"
22 #include "components/password_manager/core/common/password_manager_pref_names.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25
26 using base::ASCIIToUTF16;
27
28 namespace password_manager {
29
30 namespace {
31
32 class TestPasswordManagerDriver : public StubPasswordManagerDriver {
33  public:
34   TestPasswordManagerDriver(PasswordManagerClient* client)
35       : password_manager_(client),
36         password_generation_manager_(client),
37         password_autofill_manager_(client, NULL),
38         is_off_the_record_(false) {}
39   ~TestPasswordManagerDriver() override {}
40
41   // PasswordManagerDriver implementation.
42   bool IsOffTheRecord() override { return is_off_the_record_; }
43   PasswordGenerationManager* GetPasswordGenerationManager() override {
44     return &password_generation_manager_;
45   }
46   PasswordManager* GetPasswordManager() override { return &password_manager_; }
47   PasswordAutofillManager* GetPasswordAutofillManager() override {
48     return &password_autofill_manager_;
49   }
50   void AccountCreationFormsFound(
51       const std::vector<autofill::FormData>& forms) override {
52     found_account_creation_forms_.insert(
53         found_account_creation_forms_.begin(), forms.begin(), forms.end());
54   }
55
56   const std::vector<autofill::FormData>& GetFoundAccountCreationForms() {
57     return found_account_creation_forms_;
58   }
59   void set_is_off_the_record(bool is_off_the_record) {
60     is_off_the_record_ = is_off_the_record;
61   }
62
63  private:
64   PasswordManager password_manager_;
65   PasswordGenerationManager password_generation_manager_;
66   PasswordAutofillManager password_autofill_manager_;
67   std::vector<autofill::FormData> found_account_creation_forms_;
68   bool is_off_the_record_;
69 };
70
71 class TestPasswordManagerClient : public StubPasswordManagerClient {
72  public:
73   TestPasswordManagerClient(scoped_ptr<PrefService> prefs)
74       : prefs_(prefs.Pass()),
75         store_(new TestPasswordStore),
76         driver_(this),
77         is_sync_enabled_(false) {}
78
79   ~TestPasswordManagerClient() override {
80     store_->Shutdown();
81   }
82
83   PasswordStore* GetPasswordStore() override { return store_.get(); }
84   PrefService* GetPrefs() override { return prefs_.get(); }
85   PasswordManagerDriver* GetDriver() override { return &driver_; }
86   bool IsPasswordSyncEnabled(CustomPassphraseState state) override {
87     return is_sync_enabled_;
88   }
89
90   void set_is_password_sync_enabled(bool enabled) {
91     is_sync_enabled_ = enabled;
92   }
93
94  private:
95   scoped_ptr<PrefService> prefs_;
96   scoped_refptr<TestPasswordStore> store_;
97   TestPasswordManagerDriver driver_;
98   bool is_sync_enabled_;
99 };
100
101 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
102 // which are handy for briefer test code.  The AutofillMetrics class is
103 // stateless, so this is safe.
104 class TestAutofillMetrics : public autofill::AutofillMetrics {
105  public:
106   TestAutofillMetrics() {}
107   ~TestAutofillMetrics() override {}
108 };
109
110 }  // anonymous namespace
111
112 class PasswordGenerationManagerTest : public testing::Test {
113  protected:
114   void SetUp() override {
115     // Construct a PrefService and register all necessary prefs before handing
116     // it off to |client_|, as the initialization flow of |client_| will
117     // indirectly cause those prefs to be immediately accessed.
118     scoped_ptr<TestingPrefServiceSimple> prefs(new TestingPrefServiceSimple());
119     prefs->registry()->RegisterBooleanPref(prefs::kPasswordManagerSavingEnabled,
120                                            true);
121     client_.reset(new TestPasswordManagerClient(prefs.Pass()));
122   }
123
124   void TearDown() override { client_.reset(); }
125
126   PasswordGenerationManager* GetGenerationManager() {
127     return client_->GetDriver()->GetPasswordGenerationManager();
128   }
129
130   TestPasswordManagerDriver* GetTestDriver() {
131     return static_cast<TestPasswordManagerDriver*>(client_->GetDriver());
132   }
133
134   bool IsGenerationEnabled() {
135     return GetGenerationManager()->IsGenerationEnabled();
136   }
137
138   void DetectAccountCreationForms(
139       const std::vector<autofill::FormStructure*>& forms) {
140     GetGenerationManager()->DetectAccountCreationForms(forms);
141   }
142
143   scoped_ptr<TestPasswordManagerClient> client_;
144 };
145
146 TEST_F(PasswordGenerationManagerTest, IsGenerationEnabled) {
147   // Enabling the PasswordManager and password sync should cause generation to
148   // be enabled.
149   PrefService* prefs = client_->GetPrefs();
150   prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true);
151   client_->set_is_password_sync_enabled(true);
152   EXPECT_TRUE(IsGenerationEnabled());
153
154   // Disabling password syncing should cause generation to be disabled.
155   client_->set_is_password_sync_enabled(false);
156   EXPECT_FALSE(IsGenerationEnabled());
157
158   // Disabling the PasswordManager should cause generation to be disabled even
159   // if syncing is enabled.
160   prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, false);
161   client_->set_is_password_sync_enabled(true);
162   EXPECT_FALSE(IsGenerationEnabled());
163 }
164
165 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) {
166   // Setup so that IsGenerationEnabled() returns true.
167   PrefService* prefs = client_->GetPrefs();
168   prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true);
169   client_->set_is_password_sync_enabled(true);
170
171   autofill::FormData login_form;
172   login_form.origin = GURL("http://www.yahoo.com/login/");
173   autofill::FormFieldData username;
174   username.label = ASCIIToUTF16("username");
175   username.name = ASCIIToUTF16("login");
176   username.form_control_type = "text";
177   login_form.fields.push_back(username);
178   autofill::FormFieldData password;
179   password.label = ASCIIToUTF16("password");
180   password.name = ASCIIToUTF16("password");
181   password.form_control_type = "password";
182   login_form.fields.push_back(password);
183   autofill::FormStructure form1(login_form);
184   std::vector<autofill::FormStructure*> forms;
185   forms.push_back(&form1);
186   autofill::FormData account_creation_form;
187   account_creation_form.origin = GURL("http://accounts.yahoo.com/");
188   account_creation_form.fields.push_back(username);
189   account_creation_form.fields.push_back(password);
190   autofill::FormFieldData confirm_password;
191   confirm_password.label = ASCIIToUTF16("confirm_password");
192   confirm_password.name = ASCIIToUTF16("password");
193   confirm_password.form_control_type = "password";
194   account_creation_form.fields.push_back(confirm_password);
195   autofill::FormStructure form2(account_creation_form);
196   forms.push_back(&form2);
197
198   // Simulate the server response to set the field types.
199   const char* const kServerResponse =
200       "<autofillqueryresponse>"
201       "<field autofilltype=\"9\" />"
202       "<field autofilltype=\"75\" />"
203       "<field autofilltype=\"9\" />"
204       "<field autofilltype=\"76\" />"
205       "<field autofilltype=\"75\" />"
206       "</autofillqueryresponse>";
207   autofill::FormStructure::ParseQueryResponse(
208       kServerResponse, forms, TestAutofillMetrics());
209
210   DetectAccountCreationForms(forms);
211   EXPECT_EQ(1u, GetTestDriver()->GetFoundAccountCreationForms().size());
212   EXPECT_EQ(GURL("http://accounts.yahoo.com/"),
213             GetTestDriver()->GetFoundAccountCreationForms()[0].origin);
214 }
215
216 TEST_F(PasswordGenerationManagerTest, UpdatePasswordSyncStateIncognito) {
217   // Disable password manager by going incognito. Even though password
218   // syncing is enabled, generation should still
219   // be disabled.
220   GetTestDriver()->set_is_off_the_record(true);
221   PrefService* prefs = client_->GetPrefs();
222   prefs->SetBoolean(prefs::kPasswordManagerSavingEnabled, true);
223   client_->set_is_password_sync_enabled(true);
224
225   EXPECT_FALSE(IsGenerationEnabled());
226 }
227
228 }  // namespace password_manager