OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/test/base/chrome_render_view_test.h" | 10 #include "chrome/test/base/chrome_render_view_test.h" |
11 #include "components/autofill/content/renderer/password_generation_manager.h" | 11 #include "components/autofill/content/renderer/password_generation_manager.h" |
12 #include "components/autofill/core/common/autofill_messages.h" | 12 #include "components/autofill/core/common/autofill_messages.h" |
| 13 #include "components/autofill/core/common/form_data.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/WebKit/public/platform/WebString.h" | 15 #include "third_party/WebKit/public/platform/WebString.h" |
15 #include "third_party/WebKit/public/web/WebDocument.h" | 16 #include "third_party/WebKit/public/web/WebDocument.h" |
16 #include "third_party/WebKit/public/web/WebWidget.h" | 17 #include "third_party/WebKit/public/web/WebWidget.h" |
17 | 18 |
18 using WebKit::WebDocument; | 19 using WebKit::WebDocument; |
19 using WebKit::WebElement; | 20 using WebKit::WebElement; |
20 using WebKit::WebInputElement; | 21 using WebKit::WebInputElement; |
21 using WebKit::WebNode; | 22 using WebKit::WebNode; |
22 using WebKit::WebString; | 23 using WebKit::WebString; |
23 | 24 |
24 namespace autofill { | 25 namespace autofill { |
25 | 26 |
26 class TestPasswordGenerationManager : public PasswordGenerationManager { | 27 class TestPasswordGenerationManager : public PasswordGenerationManager { |
27 public: | 28 public: |
28 explicit TestPasswordGenerationManager(content::RenderView* view) | 29 explicit TestPasswordGenerationManager(content::RenderView* view) |
29 : PasswordGenerationManager(view) {} | 30 : PasswordGenerationManager(view) {} |
30 virtual ~TestPasswordGenerationManager() {} | 31 virtual ~TestPasswordGenerationManager() {} |
31 | 32 |
32 // Make this public | 33 // Make this public |
33 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 34 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
34 return PasswordGenerationManager::OnMessageReceived(message); | 35 return PasswordGenerationManager::OnMessageReceived(message); |
35 } | 36 } |
36 | 37 |
37 const std::vector<IPC::Message*>& messages() { | 38 const std::vector<IPC::Message*>& messages() { |
38 return messages_.get(); | 39 return messages_.get(); |
39 } | 40 } |
40 | 41 |
| 42 void ClearMessages() { |
| 43 messages_.clear(); |
| 44 } |
| 45 |
41 protected: | 46 protected: |
42 virtual bool ShouldAnalyzeDocument(const WebKit::WebDocument& document) const | 47 virtual bool ShouldAnalyzeDocument(const WebKit::WebDocument& document) const |
43 OVERRIDE { | 48 OVERRIDE { |
44 return true; | 49 return true; |
45 } | 50 } |
46 | 51 |
47 virtual bool Send(IPC::Message* message) OVERRIDE { | 52 virtual bool Send(IPC::Message* message) OVERRIDE { |
48 messages_.push_back(message); | 53 messages_.push_back(message); |
49 return true; | 54 return true; |
50 } | 55 } |
(...skipping 16 matching lines...) Expand all Loading... |
67 generation_manager_.reset(new TestPasswordGenerationManager(view_)); | 72 generation_manager_.reset(new TestPasswordGenerationManager(view_)); |
68 } | 73 } |
69 | 74 |
70 virtual void TearDown() { | 75 virtual void TearDown() { |
71 LoadHTML(""); | 76 LoadHTML(""); |
72 generation_manager_.reset(); | 77 generation_manager_.reset(); |
73 ChromeRenderViewTest::TearDown(); | 78 ChromeRenderViewTest::TearDown(); |
74 } | 79 } |
75 | 80 |
76 void SimulateClickOnDecoration(WebKit::WebInputElement* input_element) { | 81 void SimulateClickOnDecoration(WebKit::WebInputElement* input_element) { |
| 82 generation_manager_->ClearMessages(); |
77 WebKit::WebElement decoration = | 83 WebKit::WebElement decoration = |
78 input_element->decorationElementFor(generation_manager_.get()); | 84 input_element->decorationElementFor(generation_manager_.get()); |
79 decoration.simulateClick(); | 85 decoration.simulateClick(); |
80 } | 86 } |
81 | 87 |
82 bool DecorationIsVisible(WebKit::WebInputElement* input_element) { | 88 bool DecorationIsVisible(WebKit::WebInputElement* input_element) { |
83 WebKit::WebElement decoration = | 89 WebKit::WebElement decoration = |
84 input_element->decorationElementFor(generation_manager_.get()); | 90 input_element->decorationElementFor(generation_manager_.get()); |
85 return decoration.hasNonEmptyBoundingBox(); | 91 return decoration.hasNonEmptyBoundingBox(); |
86 } | 92 } |
87 | 93 |
| 94 void SetPasswordGenerationEnabledMessage(void) { |
| 95 AutofillMsg_PasswordGenerationEnabled msg(0, true); |
| 96 generation_manager_->OnMessageReceived(msg); |
| 97 } |
| 98 |
88 void SetNotBlacklistedMessage(const char* form_str) { | 99 void SetNotBlacklistedMessage(const char* form_str) { |
89 content::PasswordForm form; | 100 content::PasswordForm form; |
90 form.origin = | 101 form.origin = |
91 GURL(base::StringPrintf("data:text/html;charset=utf-8,%s", form_str)); | 102 GURL(base::StringPrintf("data:text/html;charset=utf-8,%s", form_str)); |
92 AutofillMsg_FormNotBlacklisted msg(0, form); | 103 AutofillMsg_FormNotBlacklisted msg(0, form); |
93 generation_manager_->OnMessageReceived(msg); | 104 generation_manager_->OnMessageReceived(msg); |
94 } | 105 } |
95 | 106 |
| 107 void SetAccountCreationFormsDetectedMessage(const char* form_str) { |
| 108 autofill::FormData form; |
| 109 form.origin = |
| 110 GURL(base::StringPrintf("data:text/html;charset=utf-8,%s", form_str)); |
| 111 std::vector<autofill::FormData> forms; |
| 112 forms.push_back(form); |
| 113 AutofillMsg_AccountCreationFormsDetected msg(0, forms); |
| 114 generation_manager_->OnMessageReceived(msg); |
| 115 } |
| 116 |
| 117 void ExpectPasswordGenerationIconShown(const char* element_id, bool shown) { |
| 118 WebDocument document = GetMainFrame()->document(); |
| 119 WebElement element = |
| 120 document.getElementById(WebString::fromUTF8(element_id)); |
| 121 ASSERT_FALSE(element.isNull()); |
| 122 WebInputElement target_element = element.to<WebInputElement>(); |
| 123 if (shown) { |
| 124 EXPECT_TRUE(DecorationIsVisible(&target_element)); |
| 125 SimulateClickOnDecoration(&target_element); |
| 126 EXPECT_EQ(1u, generation_manager_->messages().size()); |
| 127 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, |
| 128 generation_manager_->messages()[0]->type()); |
| 129 } else { |
| 130 EXPECT_FALSE(DecorationIsVisible(&target_element)); |
| 131 } |
| 132 } |
| 133 |
96 protected: | 134 protected: |
97 scoped_ptr<TestPasswordGenerationManager> generation_manager_; | 135 scoped_ptr<TestPasswordGenerationManager> generation_manager_; |
98 | 136 |
99 private: | 137 private: |
100 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationManagerTest); | 138 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationManagerTest); |
101 }; | 139 }; |
102 | 140 |
103 const char kSigninFormHTML[] = | 141 const char kSigninFormHTML[] = |
104 "<FORM name = 'blah' action = 'http://www.random.com/'> " | 142 "<FORM name = 'blah' action = 'http://www.random.com/'> " |
105 " <INPUT type = 'text' id = 'username'/> " | 143 " <INPUT type = 'text' id = 'username'/> " |
(...skipping 21 matching lines...) Expand all Loading... |
127 | 165 |
128 const char kInvalidActionAccountCreationFormHTML[] = | 166 const char kInvalidActionAccountCreationFormHTML[] = |
129 "<FORM name = 'blah' action = 'invalid'> " | 167 "<FORM name = 'blah' action = 'invalid'> " |
130 " <INPUT type = 'text' id = 'username'/> " | 168 " <INPUT type = 'text' id = 'username'/> " |
131 " <INPUT type = 'password' id = 'first_password'/> " | 169 " <INPUT type = 'password' id = 'first_password'/> " |
132 " <INPUT type = 'password' id = 'second_password'/> " | 170 " <INPUT type = 'password' id = 'second_password'/> " |
133 " <INPUT type = 'submit' value = 'LOGIN' />" | 171 " <INPUT type = 'submit' value = 'LOGIN' />" |
134 "</FORM>"; | 172 "</FORM>"; |
135 | 173 |
136 TEST_F(PasswordGenerationManagerTest, DetectionTest) { | 174 TEST_F(PasswordGenerationManagerTest, DetectionTest) { |
| 175 // Don't shown the icon for non account creation forms. |
137 LoadHTML(kSigninFormHTML); | 176 LoadHTML(kSigninFormHTML); |
138 | 177 ExpectPasswordGenerationIconShown("password", false); |
139 WebDocument document = GetMainFrame()->document(); | |
140 WebElement element = | |
141 document.getElementById(WebString::fromUTF8("password")); | |
142 ASSERT_FALSE(element.isNull()); | |
143 WebInputElement password_element = element.to<WebInputElement>(); | |
144 EXPECT_FALSE(DecorationIsVisible(&password_element)); | |
145 | |
146 LoadHTML(kAccountCreationFormHTML); | |
147 | 178 |
148 // We don't show the decoration yet because the feature isn't enabled. | 179 // We don't show the decoration yet because the feature isn't enabled. |
149 document = GetMainFrame()->document(); | 180 LoadHTML(kAccountCreationFormHTML); |
150 element = document.getElementById(WebString::fromUTF8("first_password")); | 181 ExpectPasswordGenerationIconShown("first_password", false); |
151 ASSERT_FALSE(element.isNull()); | |
152 WebInputElement first_password_element = element.to<WebInputElement>(); | |
153 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); | |
154 | 182 |
155 // Pretend like password generation was enabled. | 183 // Pretend like password generation was enabled. |
156 AutofillMsg_PasswordGenerationEnabled msg(0, true); | 184 SetPasswordGenerationEnabledMessage(); |
157 generation_manager_->OnMessageReceived(msg); | |
158 | 185 |
| 186 // Pretend like We have received message indicating site is not blacklisted, |
| 187 // and we have received message indicating the form is classified as |
| 188 // ACCOUNT_CREATION_FORM form Autofill server. We should show the icon. |
159 LoadHTML(kAccountCreationFormHTML); | 189 LoadHTML(kAccountCreationFormHTML); |
160 | |
161 // Pretend like we have received message indicating site is not blacklisted. | |
162 SetNotBlacklistedMessage(kAccountCreationFormHTML); | 190 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
163 | 191 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
164 document = GetMainFrame()->document(); | 192 ExpectPasswordGenerationIconShown("first_password", true); |
165 element = document.getElementById(WebString::fromUTF8("first_password")); | |
166 ASSERT_FALSE(element.isNull()); | |
167 first_password_element = element.to<WebInputElement>(); | |
168 EXPECT_TRUE(DecorationIsVisible(&first_password_element)); | |
169 SimulateClickOnDecoration(&first_password_element); | |
170 EXPECT_EQ(1u, generation_manager_->messages().size()); | |
171 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, | |
172 generation_manager_->messages()[0]->type()); | |
173 | 193 |
174 // This doesn't trigger because hidden password fields are ignored. | 194 // This doesn't trigger because hidden password fields are ignored. |
175 LoadHTML(kHiddenPasswordAccountCreationFormHTML); | 195 LoadHTML(kHiddenPasswordAccountCreationFormHTML); |
176 SetNotBlacklistedMessage(kAccountCreationFormHTML); | 196 SetNotBlacklistedMessage(kHiddenPasswordAccountCreationFormHTML); |
177 document = GetMainFrame()->document(); | 197 SetAccountCreationFormsDetectedMessage( |
178 element = document.getElementById(WebString::fromUTF8("first_password")); | 198 kHiddenPasswordAccountCreationFormHTML); |
179 ASSERT_FALSE(element.isNull()); | 199 ExpectPasswordGenerationIconShown("first_password", false); |
180 first_password_element = element.to<WebInputElement>(); | |
181 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); | |
182 | 200 |
183 // This doesn't trigger because the form action is invalid. | 201 // This doesn't trigger because the form action is invalid. |
184 LoadHTML(kInvalidActionAccountCreationFormHTML); | 202 LoadHTML(kInvalidActionAccountCreationFormHTML); |
185 SetNotBlacklistedMessage(kAccountCreationFormHTML); | 203 SetNotBlacklistedMessage(kInvalidActionAccountCreationFormHTML); |
186 document = GetMainFrame()->document(); | 204 SetAccountCreationFormsDetectedMessage(kInvalidActionAccountCreationFormHTML); |
187 element = document.getElementById(WebString::fromUTF8("first_password")); | 205 ExpectPasswordGenerationIconShown("first_password", false); |
188 ASSERT_FALSE(element.isNull()); | |
189 first_password_element = element.to<WebInputElement>(); | |
190 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); | |
191 } | 206 } |
192 | 207 |
193 TEST_F(PasswordGenerationManagerTest, FillTest) { | 208 TEST_F(PasswordGenerationManagerTest, FillTest) { |
194 // Make sure that we are enabled before loading HTML. | 209 // Make sure that we are enabled before loading HTML. |
195 AutofillMsg_PasswordGenerationEnabled enabled_msg(0, true); | 210 SetPasswordGenerationEnabledMessage(); |
196 generation_manager_->OnMessageReceived(enabled_msg); | |
197 LoadHTML(kAccountCreationFormHTML); | 211 LoadHTML(kAccountCreationFormHTML); |
198 | 212 |
199 WebDocument document = GetMainFrame()->document(); | 213 WebDocument document = GetMainFrame()->document(); |
200 WebElement element = | 214 WebElement element = |
201 document.getElementById(WebString::fromUTF8("first_password")); | 215 document.getElementById(WebString::fromUTF8("first_password")); |
202 ASSERT_FALSE(element.isNull()); | 216 ASSERT_FALSE(element.isNull()); |
203 WebInputElement first_password_element = element.to<WebInputElement>(); | 217 WebInputElement first_password_element = element.to<WebInputElement>(); |
204 element = document.getElementById(WebString::fromUTF8("second_password")); | 218 element = document.getElementById(WebString::fromUTF8("second_password")); |
205 ASSERT_FALSE(element.isNull()); | 219 ASSERT_FALSE(element.isNull()); |
206 WebInputElement second_password_element = element.to<WebInputElement>(); | 220 WebInputElement second_password_element = element.to<WebInputElement>(); |
(...skipping 15 matching lines...) Expand all Loading... |
222 // Focus moved to the next input field. | 236 // Focus moved to the next input field. |
223 // TODO(zysxqn): Change this back to the address element once Bug 90224 | 237 // TODO(zysxqn): Change this back to the address element once Bug 90224 |
224 // https://bugs.webkit.org/show_bug.cgi?id=90224 has been fixed. | 238 // https://bugs.webkit.org/show_bug.cgi?id=90224 has been fixed. |
225 element = document.getElementById(WebString::fromUTF8("first_password")); | 239 element = document.getElementById(WebString::fromUTF8("first_password")); |
226 ASSERT_FALSE(element.isNull()); | 240 ASSERT_FALSE(element.isNull()); |
227 EXPECT_EQ(element, document.focusedNode()); | 241 EXPECT_EQ(element, document.focusedNode()); |
228 } | 242 } |
229 | 243 |
230 TEST_F(PasswordGenerationManagerTest, BlacklistedTest) { | 244 TEST_F(PasswordGenerationManagerTest, BlacklistedTest) { |
231 // Make sure password generation is enabled. | 245 // Make sure password generation is enabled. |
232 AutofillMsg_PasswordGenerationEnabled enabled_msg(0, true); | 246 SetPasswordGenerationEnabledMessage(); |
233 generation_manager_->OnMessageReceived(enabled_msg); | |
234 | 247 |
235 // Did not receive not blacklisted message. Don't show password generation | 248 // Did not receive not blacklisted message. Don't show password generation |
236 // icon. | 249 // icon. |
237 LoadHTML(kAccountCreationFormHTML); | 250 LoadHTML(kAccountCreationFormHTML); |
238 WebDocument document = GetMainFrame()->document(); | 251 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
239 WebElement element = | 252 ExpectPasswordGenerationIconShown("first_password", false); |
240 document.getElementById(WebString::fromUTF8("first_password")); | |
241 ASSERT_FALSE(element.isNull()); | |
242 WebInputElement first_password_element = element.to<WebInputElement>(); | |
243 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); | |
244 | 253 |
245 // Receive one not blacklisted message for non account creation form. Don't | 254 // Receive one not blacklisted message for non account creation form. Don't |
246 // show password generation icon. | 255 // show password generation icon. |
247 LoadHTML(kAccountCreationFormHTML); | 256 LoadHTML(kAccountCreationFormHTML); |
248 SetNotBlacklistedMessage(kSigninFormHTML); | 257 SetNotBlacklistedMessage(kSigninFormHTML); |
249 document = GetMainFrame()->document(); | 258 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
250 element = document.getElementById(WebString::fromUTF8("first_password")); | 259 ExpectPasswordGenerationIconShown("first_password", false); |
251 ASSERT_FALSE(element.isNull()); | |
252 first_password_element = element.to<WebInputElement>(); | |
253 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); | |
254 | 260 |
255 // Receive one not blackliste message for account creation form. Show password | 261 // Receive one not blackliste message for account creation form. Show password |
256 // generation icon. | 262 // generation icon. |
257 LoadHTML(kAccountCreationFormHTML); | 263 LoadHTML(kAccountCreationFormHTML); |
258 SetNotBlacklistedMessage(kAccountCreationFormHTML); | 264 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
259 document = GetMainFrame()->document(); | 265 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
260 element = document.getElementById(WebString::fromUTF8("first_password")); | 266 ExpectPasswordGenerationIconShown("first_password", true); |
261 ASSERT_FALSE(element.isNull()); | |
262 first_password_element = element.to<WebInputElement>(); | |
263 EXPECT_TRUE(DecorationIsVisible(&first_password_element)); | |
264 SimulateClickOnDecoration(&first_password_element); | |
265 EXPECT_EQ(1u, generation_manager_->messages().size()); | |
266 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, | |
267 generation_manager_->messages()[0]->type()); | |
268 | 267 |
269 // Receive two not blacklisted messages, one is for account creation form and | 268 // Receive two not blacklisted messages, one is for account creation form and |
270 // the other is not. Show password generation icon. | 269 // the other is not. Show password generation icon. |
271 LoadHTML(kAccountCreationFormHTML); | 270 LoadHTML(kAccountCreationFormHTML); |
272 SetNotBlacklistedMessage(kAccountCreationFormHTML); | 271 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
273 SetNotBlacklistedMessage(kSigninFormHTML); | 272 SetNotBlacklistedMessage(kSigninFormHTML); |
274 document = GetMainFrame()->document(); | 273 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
275 element = document.getElementById(WebString::fromUTF8("first_password")); | 274 ExpectPasswordGenerationIconShown("first_password", true); |
276 ASSERT_FALSE(element.isNull()); | 275 } |
277 first_password_element = element.to<WebInputElement>(); | 276 |
278 EXPECT_TRUE(DecorationIsVisible(&first_password_element)); | 277 TEST_F(PasswordGenerationManagerTest, AccountCreationFormsDetectedTest) { |
279 SimulateClickOnDecoration(&first_password_element); | 278 // Make sure password generation is enabled. |
280 EXPECT_EQ(2u, generation_manager_->messages().size()); | 279 SetPasswordGenerationEnabledMessage(); |
281 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, | 280 |
282 generation_manager_->messages()[1]->type()); | 281 // Did not receive account creation forms detected messege. Don't show |
| 282 // password generation icon. |
| 283 LoadHTML(kAccountCreationFormHTML); |
| 284 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
| 285 ExpectPasswordGenerationIconShown("first_password", false); |
| 286 |
| 287 // Receive the account creation forms detected message. Show password |
| 288 // generation icon. |
| 289 LoadHTML(kAccountCreationFormHTML); |
| 290 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
| 291 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
| 292 ExpectPasswordGenerationIconShown("first_password", true); |
283 } | 293 } |
284 | 294 |
285 } // namespace autofill | 295 } // namespace autofill |
OLD | NEW |