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

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: Correct comment 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
« no previous file with comments | « no previous file | chrome/test/data/password/form_and_link.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
177 private: 182 private:
178 DISALLOW_COPY_AND_ASSIGN(PasswordManagerBrowserTest); 183 DISALLOW_COPY_AND_ASSIGN(PasswordManagerBrowserTest);
179 }; 184 };
180 185
186 void PasswordManagerBrowserTest::CheckScriptReturnValue(
187 std::string& script,
188 bool expected_return_value) {
189 const std::string wrapped_script =
190 std::string("window.domAutomationController.send(") + script + ");";
191 bool return_value = !expected_return_value;
192 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
193 RenderViewHost(), wrapped_script, &return_value));
194 EXPECT_EQ(expected_return_value, return_value) << "script = " << script;
195 }
196
181 // Actual tests --------------------------------------------------------------- 197 // Actual tests ---------------------------------------------------------------
182 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, 198 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
183 PromptForNormalSubmit) { 199 PromptForNormalSubmit) {
184 NavigateToFile("/password/password_form.html"); 200 NavigateToFile("/password/password_form.html");
185 201
186 // Fill a form and submit through a <input type="submit"> button. Nothing 202 // Fill a form and submit through a <input type="submit"> button. Nothing
187 // special. 203 // special.
188 NavigationObserver observer(WebContents()); 204 NavigationObserver observer(WebContents());
189 std::string fill_and_submit = 205 std::string fill_and_submit =
190 "document.getElementById('username_field').value = 'temp';" 206 "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)); 454 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
439 455
440 first_observer.Wait(); 456 first_observer.Wait();
441 ASSERT_TRUE(first_observer.infobar_shown()); 457 ASSERT_TRUE(first_observer.infobar_shown());
442 458
443 // Now navigate to a login form that has similar HTML markup. 459 // Now navigate to a login form that has similar HTML markup.
444 NavigateToFile("/password/password_form.html"); 460 NavigateToFile("/password/password_form.html");
445 461
446 // Simulate a user click to force an autofill of the form's DOM value, not 462 // Simulate a user click to force an autofill of the form's DOM value, not
447 // just the suggested value. 463 // just the suggested value.
448 std::string click = "document.getElementById('testform_no_name').click()"; 464 content::SimulateMouseClick(
449 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), click)); 465 WebContents(), 0, blink::WebMouseEvent::ButtonLeft);
450 466
451 // The form should be filled with the previously submitted username. 467 // The form should be filled with the previously submitted username.
452 std::string get_username = 468 std::string get_username =
453 "window.domAutomationController.send(" 469 "window.domAutomationController.send("
454 "document.getElementById('username_field').value);"; 470 "document.getElementById('username_field').value);";
455 std::string actual_username; 471 std::string actual_username;
456 ASSERT_TRUE(content::ExecuteScriptAndExtractString(RenderViewHost(), 472 ASSERT_TRUE(content::ExecuteScriptAndExtractString(RenderViewHost(),
457 get_username, 473 get_username,
458 &actual_username)); 474 &actual_username));
459 ASSERT_EQ("my_username", actual_username); 475 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';" 584 "document.getElementById('username_field').value = 'temp';"
569 "document.getElementById('password_field').value = 'random';" 585 "document.getElementById('password_field').value = 'random';"
570 "document.getElementById('input_submit_button').click();" 586 "document.getElementById('input_submit_button').click();"
571 "window.location.href = 'done.html';"; 587 "window.location.href = 'done.html';";
572 588
573 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove)); 589 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), save_and_remove));
574 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame)); 590 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), navigate_frame));
575 observer.Wait(); 591 observer.Wait();
576 // The only thing we check here is that there is no use-after-free reported. 592 // The only thing we check here is that there is no use-after-free reported.
577 } 593 }
594
595 IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PasswordValueAccessible) {
596 NavigateToFile("/password/form_and_link.html");
597
598 // Click on a link to open a new tab, then switch back to the first one.
599 EXPECT_EQ(1, browser()->tab_strip_model()->count());
600 std::string click =
601 "document.getElementById('testlink').click();";
602 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), click));
603 EXPECT_EQ(2, browser()->tab_strip_model()->count());
604 browser()->tab_strip_model()->ActivateTabAt(0, false);
605
606 // Fill in the credentials, and make sure they are saved.
607 NavigationObserver form_submit_observer(WebContents());
608 std::string fill_and_submit =
609 "document.getElementById('username_field').value = 'temp';"
610 "document.getElementById('password_field').value = 'random';"
611 "document.getElementById('input_submit_button').click();";
612 ASSERT_TRUE(content::ExecuteScript(RenderViewHost(), fill_and_submit));
613 form_submit_observer.Wait();
614 EXPECT_TRUE(form_submit_observer.infobar_shown());
615
616 // Reload the original page to have the saved credentials autofilled.
617 NavigationObserver reload_observer(WebContents());
618 NavigateToFile("/password/form_and_link.html");
619 reload_observer.Wait();
620
621 // Check that while the username is immediately available, the password value
622 // needs a user interaction to show up.
623 std::string check_username =
624 "document.getElementById('username_field').value == 'temp'";
625 std::string check_password =
626 "document.getElementById('password_field').value == 'random'";
627 CheckScriptReturnValue(check_username, true);
628 CheckScriptReturnValue(check_password, false);
629 content::SimulateMouseClick(
630 WebContents(), 0, blink::WebMouseEvent::ButtonLeft);
631 CheckScriptReturnValue(check_username, true);
632 CheckScriptReturnValue(check_password, true);
633 }
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/password/form_and_link.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698