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

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

Issue 163843002: Fix check for user gesture on password autofill (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: New implementation using isUserGestureEventType 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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/metrics/histogram_samples.h" 8 #include "base/metrics/histogram_samples.h"
9 #include "base/metrics/statistics_recorder.h" 9 #include "base/metrics/statistics_recorder.h"
10 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
(...skipping 13 matching lines...) Expand all
24 #include "content/public/browser/notification_registrar.h" 24 #include "content/public/browser/notification_registrar.h"
25 #include "content/public/browser/notification_service.h" 25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_view_host.h" 26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_observer.h" 28 #include "content/public/browser/web_contents_observer.h"
29 #include "content/public/test/browser_test_utils.h" 29 #include "content/public/test/browser_test_utils.h"
30 #include "content/public/test/test_utils.h" 30 #include "content/public/test/test_utils.h"
31 #include "net/test/embedded_test_server/embedded_test_server.h" 31 #include "net/test/embedded_test_server/embedded_test_server.h"
32 #include "net/url_request/test_url_fetcher_factory.h" 32 #include "net/url_request/test_url_fetcher_factory.h"
33 #include "testing/gmock/include/gmock/gmock.h" 33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "third_party/WebKit/public/web/WebInputEvent.h"
34 #include "ui/events/keycodes/keyboard_codes.h" 35 #include "ui/events/keycodes/keyboard_codes.h"
35 36
36 37
37 // NavigationObserver --------------------------------------------------------- 38 // NavigationObserver ---------------------------------------------------------
38 39
39 namespace { 40 namespace {
40 41
41 // Observer that waits for navigation to complete and for the password infobar 42 // Observer that waits for navigation to complete and for the password infobar
42 // to be shown. 43 // to be shown.
43 class NavigationObserver : public content::NotificationObserver, 44 class NavigationObserver : public content::NotificationObserver,
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 void NavigateToFile(const std::string& path) { 168 void NavigateToFile(const std::string& path) {
168 if (!embedded_test_server()->Started()) 169 if (!embedded_test_server()->Started())
169 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 170 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
170 171
171 NavigationObserver observer(WebContents()); 172 NavigationObserver observer(WebContents());
172 GURL url = embedded_test_server()->GetURL(path); 173 GURL url = embedded_test_server()->GetURL(path);
173 ui_test_utils::NavigateToURL(browser(), url); 174 ui_test_utils::NavigateToURL(browser(), url);
174 observer.Wait(); 175 observer.Wait();
175 } 176 }
176 177
178 // Executes |script| and uses the EXPECT macros to check the return value
179 // against |expected_return_value|.
180 void CheckScriptReturnValue(std::string& script, bool expected_return_value);
181
182 // Simulate a user clicking somewhere in the page.
183 void SimulateClick();
184
177 private: 185 private:
178 DISALLOW_COPY_AND_ASSIGN(PasswordManagerBrowserTest); 186 DISALLOW_COPY_AND_ASSIGN(PasswordManagerBrowserTest);
179 }; 187 };
180 188
189 void PasswordManagerBrowserTest::CheckScriptReturnValue(
190 std::string& script,
191 bool expected_return_value) {
192 const std::string wrapped_script =
193 std::string("window.domAutomationController.send(") + script + ");";
194 bool return_value = !expected_return_value;
195 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
196 RenderViewHost(), wrapped_script, &return_value));
197 EXPECT_EQ(expected_return_value, return_value) << "script = " << script;
198 }
199
200 void PasswordManagerBrowserTest::SimulateClick() {
201 blink::WebMouseEvent mouse_event;
202 mouse_event.type = blink::WebInputEvent::MouseDown;
203 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
204 mouse_event.x = 1;
205 mouse_event.y = 1;
206 mouse_event.clickCount = 1;
207 WebContents()->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
208 }
209
181 // Actual tests --------------------------------------------------------------- 210 // Actual tests ---------------------------------------------------------------
182 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, 211 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
183 PromptForNormalSubmit) { 212 PromptForNormalSubmit) {
184 NavigateToFile("/password/password_form.html"); 213 NavigateToFile("/password/password_form.html");
185 214
186 // Fill a form and submit through a <input type="submit"> button. Nothing 215 // Fill a form and submit through a <input type="submit"> button. Nothing
187 // special. 216 // special.
188 NavigationObserver observer(WebContents()); 217 NavigationObserver observer(WebContents());
189 std::string fill_and_submit = 218 std::string fill_and_submit =
190 "document.getElementById('username_field').value = 'temp';" 219 "document.getElementById('username_field').value = 'temp';"
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit)); 467 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
439 468
440 first_observer.Wait(); 469 first_observer.Wait();
441 ASSERT_TRUE(first_observer.infobar_shown()); 470 ASSERT_TRUE(first_observer.infobar_shown());
442 471
443 // Now navigate to a login form that has similar HTML markup. 472 // Now navigate to a login form that has similar HTML markup.
444 NavigateToFile("/password/password_form.html"); 473 NavigateToFile("/password/password_form.html");
445 474
446 // Simulate a user click to force an autofill of the form's DOM value, not 475 // Simulate a user click to force an autofill of the form's DOM value, not
447 // just the suggested value. 476 // just the suggested value.
448 std::string click = "document.getElementById('testform_no_name').click()"; 477 SimulateClick();
449 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), click));
450 478
451 // The form should be filled with the previously submitted username. 479 // The form should be filled with the previously submitted username.
452 std::string get_username = 480 std::string get_username =
453 "window.domAutomationController.send(" 481 "window.domAutomationController.send("
454 "document.getElementById('username_field').value);"; 482 "document.getElementById('username_field').value);";
455 std::string actual_username; 483 std::string actual_username;
456 ASSERT_TRUE(content::ExecuteScriptAndExtractString(RenderViewHost(), 484 ASSERT_TRUE(content::ExecuteScriptAndExtractString(RenderViewHost(),
457 get_username, 485 get_username,
458 &actual_username)); 486 &actual_username));
459 ASSERT_EQ("my_username", actual_username); 487 ASSERT_EQ("my_username", actual_username);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 "document.getElementById('username_field').value = 'temp';" 596 "document.getElementById('username_field').value = 'temp';"
569 "document.getElementById('password_field').value = 'random';" 597 "document.getElementById('password_field').value = 'random';"
570 "document.getElementById('input_submit_button').click();" 598 "document.getElementById('input_submit_button').click();"
571 "window.location.href = 'done.html';"; 599 "window.location.href = 'done.html';";
572 600
573 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove)); 601 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove));
574 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame)); 602 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame));
575 observer.Wait(); 603 observer.Wait();
576 // The only thing we check here is that there is no use-after-free reported. 604 // The only thing we check here is that there is no use-after-free reported.
577 } 605 }
606
607 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PasswordValueAccessible) {
608 NavigateToFile("/password/form_and_link.html");
609
610 // Click on a link to open a new tab, then switch back to the first one.
611 EXPECT_EQ(1, browser()->tab_strip_model()->count());
612 std::string click =
613 "document.getElementById('testlink').click();";
614 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), click));
615 EXPECT_EQ(2, browser()->tab_strip_model()->count());
616 browser()->tab_strip_model()->ActivateTabAt(0, false);
617
618 // Fill in the credentials, and make sure they are saved.
619 NavigationObserver form_submit_observer(WebContents());
620 std::string fill_and_submit =
621 "document.getElementById('username_field').value = 'temp';"
622 "document.getElementById('password_field').value = 'random';"
623 "document.getElementById('input_submit_button').click();";
624 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
625 form_submit_observer.Wait();
626 EXPECT_TRUE(form_submit_observer.infobar_shown());
627
628 // Reload the original page to have the saved credentials autofilled.
629 NavigationObserver reload_observer(WebContents());
630 NavigateToFile("/password/form_and_link.html");
631 reload_observer.Wait();
632
633 // Check that while the username is immediately available, the password value
634 // needs a user interaction to show up.
635 std::string check_username =
636 "document.getElementById('username_field').value == 'temp'";
637 std::string check_password =
638 "document.getElementById('password_field').value == 'random'";
639 CheckScriptReturnValue(check_username, true);
640 CheckScriptReturnValue(check_password, false);
641 SimulateClick();
642 CheckScriptReturnValue(check_username, true);
643 CheckScriptReturnValue(check_password, true);
644 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698