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

Side by Side Diff: chrome/renderer/autofill/password_generation_agent_browsertest.cc

Issue 2352533003: [Autofill] Migrate ChromePasswordManagerClient<-->PasswordGenerationAgent IPCs to mojo. (Closed)
Patch Set: Rebase only Created 4 years, 2 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
« no previous file with comments | « chrome/renderer/autofill/password_autofill_agent_browsertest.cc ('k') | chrome/test/BUILD.gn » ('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.h> 5 #include <string.h>
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/histogram_tester.h" 13 #include "base/test/histogram_tester.h"
13 #include "chrome/renderer/autofill/fake_content_password_manager_driver.h" 14 #include "chrome/renderer/autofill/fake_content_password_manager_driver.h"
15 #include "chrome/renderer/autofill/fake_password_manager_client.h"
14 #include "chrome/renderer/autofill/password_generation_test_utils.h" 16 #include "chrome/renderer/autofill/password_generation_test_utils.h"
15 #include "chrome/test/base/chrome_render_view_test.h" 17 #include "chrome/test/base/chrome_render_view_test.h"
16 #include "components/autofill/content/common/autofill_messages.h"
17 #include "components/autofill/content/renderer/autofill_agent.h" 18 #include "components/autofill/content/renderer/autofill_agent.h"
18 #include "components/autofill/content/renderer/test_password_generation_agent.h" 19 #include "components/autofill/content/renderer/test_password_generation_agent.h"
19 #include "components/autofill/core/common/form_data.h" 20 #include "components/autofill/core/common/form_data.h"
20 #include "components/autofill/core/common/password_generation_util.h" 21 #include "components/autofill/core/common/password_generation_util.h"
22 #include "content/public/common/associated_interface_provider.h"
21 #include "content/public/renderer/render_frame.h" 23 #include "content/public/renderer/render_frame.h"
22 #include "content/public/renderer/render_view.h" 24 #include "content/public/renderer/render_view.h"
23 #include "services/shell/public/cpp/interface_provider.h" 25 #include "services/shell/public/cpp/interface_provider.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/WebKit/public/platform/WebString.h" 27 #include "third_party/WebKit/public/platform/WebString.h"
26 #include "third_party/WebKit/public/web/WebDocument.h" 28 #include "third_party/WebKit/public/web/WebDocument.h"
27 #include "third_party/WebKit/public/web/WebLocalFrame.h" 29 #include "third_party/WebKit/public/web/WebLocalFrame.h"
28 #include "third_party/WebKit/public/web/WebWidget.h" 30 #include "third_party/WebKit/public/web/WebWidget.h"
29 #include "ui/events/keycodes/keyboard_codes.h" 31 #include "ui/events/keycodes/keyboard_codes.h"
30 32
(...skipping 12 matching lines...) Expand all
43 void RegisterMainFrameRemoteInterfaces() override { 45 void RegisterMainFrameRemoteInterfaces() override {
44 // We only use the fake driver for main frame 46 // We only use the fake driver for main frame
45 // because our test cases only involve the main frame. 47 // because our test cases only involve the main frame.
46 shell::InterfaceProvider* remote_interfaces = 48 shell::InterfaceProvider* remote_interfaces =
47 view_->GetMainRenderFrame()->GetRemoteInterfaces(); 49 view_->GetMainRenderFrame()->GetRemoteInterfaces();
48 shell::InterfaceProvider::TestApi test_api(remote_interfaces); 50 shell::InterfaceProvider::TestApi test_api(remote_interfaces);
49 test_api.SetBinderForName( 51 test_api.SetBinderForName(
50 mojom::PasswordManagerDriver::Name_, 52 mojom::PasswordManagerDriver::Name_,
51 base::Bind(&PasswordGenerationAgentTest::BindPasswordManagerDriver, 53 base::Bind(&PasswordGenerationAgentTest::BindPasswordManagerDriver,
52 base::Unretained(this))); 54 base::Unretained(this)));
55
56 // Because the test cases only involve the main frame in this test,
57 // the fake password client is only used for the main frame.
58 content::AssociatedInterfaceProvider* remote_associated_interfaces =
59 view_->GetMainRenderFrame()->GetRemoteAssociatedInterfaces();
60 remote_associated_interfaces->OverrideBinderForTesting(
61 mojom::PasswordManagerClient::Name_,
62 base::Bind(&PasswordGenerationAgentTest::BindPasswordManagerClient,
63 base::Unretained(this)));
53 } 64 }
54 65
55 void TearDown() override { 66 void TearDown() override {
56 LoadHTML(""); 67 LoadHTML("");
57 ChromeRenderViewTest::TearDown(); 68 ChromeRenderViewTest::TearDown();
58 } 69 }
59 70
60 void LoadHTMLWithUserGesture(const char* html) { 71 void LoadHTMLWithUserGesture(const char* html) {
61 LoadHTML(html); 72 LoadHTML(html);
62 73
63 // Enable show-ime event when element is focused by indicating that a user 74 // Enable show-ime event when element is focused by indicating that a user
64 // gesture has been processed since load. 75 // gesture has been processed since load.
65 EXPECT_TRUE(SimulateElementClick("dummy")); 76 EXPECT_TRUE(SimulateElementClick("dummy"));
66 } 77 }
67 78
68 void FocusField(const char* element_id) { 79 void FocusField(const char* element_id) {
69 WebDocument document = GetMainFrame()->document(); 80 WebDocument document = GetMainFrame()->document();
70 blink::WebElement element = 81 blink::WebElement element =
71 document.getElementById(blink::WebString::fromUTF8(element_id)); 82 document.getElementById(blink::WebString::fromUTF8(element_id));
72 ASSERT_FALSE(element.isNull()); 83 ASSERT_FALSE(element.isNull());
73 ExecuteJavaScriptForTests( 84 ExecuteJavaScriptForTests(
74 base::StringPrintf("document.getElementById('%s').focus();", 85 base::StringPrintf("document.getElementById('%s').focus();",
75 element_id).c_str()); 86 element_id).c_str());
76 } 87 }
77 88
78 void ExpectGenerationAvailable(const char* element_id, 89 void ExpectGenerationAvailable(const char* element_id,
79 bool available) { 90 bool available) {
80 FocusField(element_id); 91 FocusField(element_id);
81 const IPC::Message* message = 92 base::RunLoop().RunUntilIdle();
82 render_thread_->sink().GetFirstMessageMatching( 93 fake_pw_client_.Flush();
83 AutofillHostMsg_ShowPasswordGenerationPopup::ID); 94 bool called = fake_pw_client_.called_show_pw_generation_popup();
84 if (available) 95 if (available)
85 ASSERT_TRUE(message); 96 ASSERT_TRUE(called);
86 else 97 else
87 ASSERT_FALSE(message); 98 ASSERT_FALSE(called);
88 99
89 render_thread_->sink().ClearMessages(); 100 fake_pw_client_.reset_called_show_pw_generation_popup();
90 } 101 }
91 102
92 void AllowToRunFormClassifier() { 103 void AllowToRunFormClassifier() {
93 password_generation_->AllowToRunFormClassifier(); 104 password_generation_->AllowToRunFormClassifier();
94 } 105 }
95 106
96 void ExpectFormClassifierVoteReceived( 107 void ExpectFormClassifierVoteReceived(
97 bool received, 108 bool received,
98 const base::string16& expected_generation_element) { 109 const base::string16& expected_generation_element) {
99 base::RunLoop().RunUntilIdle(); 110 base::RunLoop().RunUntilIdle();
100 if (received) { 111 if (received) {
101 ASSERT_TRUE(fake_driver_.called_save_generation_field()); 112 ASSERT_TRUE(fake_driver_.called_save_generation_field());
102 EXPECT_EQ(expected_generation_element, 113 EXPECT_EQ(expected_generation_element,
103 fake_driver_.save_generation_field()); 114 fake_driver_.save_generation_field());
104 } else { 115 } else {
105 ASSERT_FALSE(fake_driver_.called_save_generation_field()); 116 ASSERT_FALSE(fake_driver_.called_save_generation_field());
106 } 117 }
107 118
108 fake_driver_.reset_save_generation_field(); 119 fake_driver_.reset_save_generation_field();
109 } 120 }
110 121
122 bool GetCalledShowPasswordGenerationPopup() {
123 fake_pw_client_.Flush();
124 return fake_pw_client_.called_show_pw_generation_popup();
125 }
126
111 void ShowGenerationPopUpManually(const char* element_id) { 127 void ShowGenerationPopUpManually(const char* element_id) {
112 FocusField(element_id); 128 FocusField(element_id);
113 password_generation_->UserTriggeredGeneratePassword(); 129 password_generation_->UserTriggeredGeneratePassword();
114 } 130 }
115 131
116 void BindPasswordManagerDriver(mojo::ScopedMessagePipeHandle handle) { 132 void BindPasswordManagerDriver(mojo::ScopedMessagePipeHandle handle) {
117 fake_driver_.BindRequest( 133 fake_driver_.BindRequest(
118 mojo::MakeRequest<mojom::PasswordManagerDriver>(std::move(handle))); 134 mojo::MakeRequest<mojom::PasswordManagerDriver>(std::move(handle)));
119 } 135 }
120 136
137 void BindPasswordManagerClient(mojo::ScopedInterfaceEndpointHandle handle) {
138 fake_pw_client_.BindRequest(
139 mojo::MakeAssociatedRequest<mojom::PasswordManagerClient>(
140 std::move(handle)));
141 }
142
121 FakeContentPasswordManagerDriver fake_driver_; 143 FakeContentPasswordManagerDriver fake_driver_;
144 FakePasswordManagerClient fake_pw_client_;
122 145
123 private: 146 private:
124 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationAgentTest); 147 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationAgentTest);
125 }; 148 };
126 149
127 const char kSigninFormHTML[] = 150 const char kSigninFormHTML[] =
128 "<FORM name = 'blah' action = 'http://www.random.com/'> " 151 "<FORM name = 'blah' action = 'http://www.random.com/'> "
129 " <INPUT type = 'text' id = 'username'/> " 152 " <INPUT type = 'text' id = 'username'/> "
130 " <INPUT type = 'password' id = 'password'/> " 153 " <INPUT type = 'password' id = 'password'/> "
131 " <INPUT type = 'button' id = 'dummy'/> " 154 " <INPUT type = 'button' id = 'dummy'/> "
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 EXPECT_EQ(password, second_password_element.value()); 382 EXPECT_EQ(password, second_password_element.value());
360 383
361 // After editing the first field they are still the same. 384 // After editing the first field they are still the same.
362 std::string edited_password_ascii = "edited_password"; 385 std::string edited_password_ascii = "edited_password";
363 SimulateUserInputChangeForElement(&first_password_element, 386 SimulateUserInputChangeForElement(&first_password_element,
364 edited_password_ascii); 387 edited_password_ascii);
365 base::string16 edited_password = base::ASCIIToUTF16(edited_password_ascii); 388 base::string16 edited_password = base::ASCIIToUTF16(edited_password_ascii);
366 EXPECT_EQ(edited_password, first_password_element.value()); 389 EXPECT_EQ(edited_password, first_password_element.value());
367 EXPECT_EQ(edited_password, second_password_element.value()); 390 EXPECT_EQ(edited_password, second_password_element.value());
368 391
369 // Clear any uninteresting sent messages.
370 render_thread_->sink().ClearMessages();
371 fake_driver_.reset_called_password_no_longer_generated(); 392 fake_driver_.reset_called_password_no_longer_generated();
372 393
373 // Verify that password mirroring works correctly even when the password 394 // Verify that password mirroring works correctly even when the password
374 // is deleted. 395 // is deleted.
375 SimulateUserInputChangeForElement(&first_password_element, std::string()); 396 SimulateUserInputChangeForElement(&first_password_element, std::string());
376 EXPECT_EQ(base::string16(), first_password_element.value()); 397 EXPECT_EQ(base::string16(), first_password_element.value());
377 EXPECT_EQ(base::string16(), second_password_element.value()); 398 EXPECT_EQ(base::string16(), second_password_element.value());
378 399
379 // Should have notified the browser that the password is no longer generated 400 // Should have notified the browser that the password is no longer generated
380 // and trigger generation again. 401 // and trigger generation again.
381 base::RunLoop().RunUntilIdle(); 402 base::RunLoop().RunUntilIdle();
382 EXPECT_TRUE(fake_driver_.called_password_no_longer_generated()); 403 EXPECT_TRUE(fake_driver_.called_password_no_longer_generated());
383 EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching( 404 EXPECT_TRUE(GetCalledShowPasswordGenerationPopup());
384 AutofillHostMsg_ShowPasswordGenerationPopup::ID));
385 } 405 }
386 406
387 TEST_F(PasswordGenerationAgentTest, BlacklistedTest) { 407 TEST_F(PasswordGenerationAgentTest, BlacklistedTest) {
388 // Did not receive not blacklisted message. Don't show password generation 408 // Did not receive not blacklisted message. Don't show password generation
389 // icon. 409 // icon.
390 LoadHTMLWithUserGesture(kAccountCreationFormHTML); 410 LoadHTMLWithUserGesture(kAccountCreationFormHTML);
391 SetAccountCreationFormsDetectedMessage(password_generation_, 411 SetAccountCreationFormsDetectedMessage(password_generation_,
392 GetMainFrame()->document(), 0, 1); 412 GetMainFrame()->document(), 0, 1);
393 ExpectGenerationAvailable("first_password", false); 413 ExpectGenerationAvailable("first_password", false);
394 414
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 WebElement element = 467 WebElement element =
448 document.getElementById(WebString::fromUTF8("first_password")); 468 document.getElementById(WebString::fromUTF8("first_password"));
449 ASSERT_FALSE(element.isNull()); 469 ASSERT_FALSE(element.isNull());
450 WebInputElement first_password_element = element.to<WebInputElement>(); 470 WebInputElement first_password_element = element.to<WebInputElement>();
451 471
452 // Make a password just under maximum offer size. 472 // Make a password just under maximum offer size.
453 SimulateUserInputChangeForElement( 473 SimulateUserInputChangeForElement(
454 &first_password_element, 474 &first_password_element,
455 std::string(password_generation_->kMaximumOfferSize - 1, 'a')); 475 std::string(password_generation_->kMaximumOfferSize - 1, 'a'));
456 // There should now be a message to show the UI. 476 // There should now be a message to show the UI.
457 EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching( 477 EXPECT_TRUE(GetCalledShowPasswordGenerationPopup());
458 AutofillHostMsg_ShowPasswordGenerationPopup::ID)); 478 fake_pw_client_.reset_called_show_pw_generation_popup();
459 render_thread_->sink().ClearMessages();
460 479
480 fake_pw_client_.reset_called_hide_pw_generation_popup();
461 // Simulate a user typing a password just over maximum offer size. 481 // Simulate a user typing a password just over maximum offer size.
462 SimulateUserTypingASCIICharacter('a', false); 482 SimulateUserTypingASCIICharacter('a', false);
463 SimulateUserTypingASCIICharacter('a', true); 483 SimulateUserTypingASCIICharacter('a', true);
464 // There should now be a message to hide the UI. 484 // There should now be a message to hide the UI.
465 EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching( 485 fake_pw_client_.Flush();
466 AutofillHostMsg_HidePasswordGenerationPopup::ID)); 486 EXPECT_TRUE(fake_pw_client_.called_hide_pw_generation_popup());
467 render_thread_->sink().ClearMessages(); 487 fake_pw_client_.reset_called_show_pw_generation_popup();
468 488
469 // Simulate the user deleting characters. The generation popup should be shown 489 // Simulate the user deleting characters. The generation popup should be shown
470 // again. 490 // again.
471 SimulateUserTypingASCIICharacter(ui::VKEY_BACK, true); 491 SimulateUserTypingASCIICharacter(ui::VKEY_BACK, true);
472 // There should now be a message to show the UI. 492 // There should now be a message to show the UI.
473 EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching( 493 EXPECT_TRUE(GetCalledShowPasswordGenerationPopup());
474 AutofillHostMsg_ShowPasswordGenerationPopup::ID)); 494 fake_pw_client_.reset_called_show_pw_generation_popup();
475 render_thread_->sink().ClearMessages();
476 495
477 // Change focus. Bubble should be hidden, but that is handled by AutofilAgent, 496 // Change focus. Bubble should be hidden, but that is handled by AutofilAgent,
478 // so no messages are sent. 497 // so no messages are sent.
479 ExecuteJavaScriptForTests("document.getElementById('username').focus();"); 498 ExecuteJavaScriptForTests("document.getElementById('username').focus();");
480 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching( 499 EXPECT_FALSE(GetCalledShowPasswordGenerationPopup());
481 AutofillHostMsg_ShowPasswordGenerationPopup::ID)); 500 fake_pw_client_.reset_called_show_pw_generation_popup();
482 render_thread_->sink().ClearMessages();
483 501
484 // Focusing the password field will bring up the generation UI again. 502 // Focusing the password field will bring up the generation UI again.
485 ExecuteJavaScriptForTests( 503 ExecuteJavaScriptForTests(
486 "document.getElementById('first_password').focus();"); 504 "document.getElementById('first_password').focus();");
487 EXPECT_TRUE(render_thread_->sink().GetFirstMessageMatching( 505 EXPECT_TRUE(GetCalledShowPasswordGenerationPopup());
488 AutofillHostMsg_ShowPasswordGenerationPopup::ID)); 506 fake_pw_client_.reset_called_show_pw_generation_popup();
489 render_thread_->sink().ClearMessages();
490 507
491 // Loading a different page triggers UMA stat upload. Verify that only one 508 // Loading a different page triggers UMA stat upload. Verify that only one
492 // display event is sent even though 509 // display event is sent even though
493 LoadHTMLWithUserGesture(kSigninFormHTML); 510 LoadHTMLWithUserGesture(kSigninFormHTML);
494 511
495 histogram_tester.ExpectBucketCount( 512 histogram_tester.ExpectBucketCount(
496 "PasswordGeneration.Event", 513 "PasswordGeneration.Event",
497 autofill::password_generation::GENERATION_POPUP_SHOWN, 514 autofill::password_generation::GENERATION_POPUP_SHOWN,
498 1); 515 1);
499 } 516 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 // correctly (generation should still be available). 578 // correctly (generation should still be available).
562 SetNotBlacklistedMessage(password_generation_, kAccountCreationFormHTML); 579 SetNotBlacklistedMessage(password_generation_, kAccountCreationFormHTML);
563 580
564 // Need to focus another field first for verification to work. 581 // Need to focus another field first for verification to work.
565 ExpectGenerationAvailable("second_password", false); 582 ExpectGenerationAvailable("second_password", false);
566 ExpectGenerationAvailable("first_password", true); 583 ExpectGenerationAvailable("first_password", true);
567 } 584 }
568 585
569 // Losing focus should not trigger a password generation popup. 586 // Losing focus should not trigger a password generation popup.
570 TEST_F(PasswordGenerationAgentTest, BlurTest) { 587 TEST_F(PasswordGenerationAgentTest, BlurTest) {
571 render_thread_->sink().ClearMessages();
572 LoadHTMLWithUserGesture(kDisabledElementAccountCreationFormHTML); 588 LoadHTMLWithUserGesture(kDisabledElementAccountCreationFormHTML);
573 SetNotBlacklistedMessage(password_generation_, 589 SetNotBlacklistedMessage(password_generation_,
574 kDisabledElementAccountCreationFormHTML); 590 kDisabledElementAccountCreationFormHTML);
575 SetAccountCreationFormsDetectedMessage(password_generation_, 591 SetAccountCreationFormsDetectedMessage(password_generation_,
576 GetMainFrame()->document(), 0, 1); 592 GetMainFrame()->document(), 0, 1);
577 593
578 // Focus on the first password field: password generation popup should show 594 // Focus on the first password field: password generation popup should show
579 // up. 595 // up.
580 ExpectGenerationAvailable("first_password", true); 596 ExpectGenerationAvailable("first_password", true);
581 597
598 fake_pw_client_.reset_called_generation_available_for_form();
582 // Remove focus from everywhere by clicking an unfocusable element: password 599 // Remove focus from everywhere by clicking an unfocusable element: password
583 // generation popup should not show up. 600 // generation popup should not show up.
584 EXPECT_TRUE(SimulateElementClick("disabled")); 601 EXPECT_TRUE(SimulateElementClick("disabled"));
585 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching( 602 fake_pw_client_.Flush();
586 AutofillHostMsg_GenerationAvailableForForm::ID)); 603 EXPECT_FALSE(fake_pw_client_.called_generation_available_for_form());
587 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching( 604 EXPECT_FALSE(GetCalledShowPasswordGenerationPopup());
588 AutofillHostMsg_ShowPasswordGenerationPopup::ID));
589 } 605 }
590 606
591 TEST_F(PasswordGenerationAgentTest, AutocompleteAttributesTest) { 607 TEST_F(PasswordGenerationAgentTest, AutocompleteAttributesTest) {
592 // Verify that autocomplete attributes can override Autofill to enable 608 // Verify that autocomplete attributes can override Autofill to enable
593 // generation 609 // generation
594 LoadHTMLWithUserGesture(kBothAutocompleteAttributesFormHTML); 610 LoadHTMLWithUserGesture(kBothAutocompleteAttributesFormHTML);
595 SetNotBlacklistedMessage(password_generation_, 611 SetNotBlacklistedMessage(password_generation_,
596 kBothAutocompleteAttributesFormHTML); 612 kBothAutocompleteAttributesFormHTML);
597 613
598 ExpectGenerationAvailable("first_password", true); 614 ExpectGenerationAvailable("first_password", true);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 base::string16()); 717 base::string16());
702 } 718 }
703 719
704 TEST_F(PasswordGenerationAgentTest, FormClassifierDisabled) { 720 TEST_F(PasswordGenerationAgentTest, FormClassifierDisabled) {
705 LoadHTMLWithUserGesture(kSigninFormHTML); 721 LoadHTMLWithUserGesture(kSigninFormHTML);
706 ExpectFormClassifierVoteReceived(false /* vote is not expected */, 722 ExpectFormClassifierVoteReceived(false /* vote is not expected */,
707 base::string16()); 723 base::string16());
708 } 724 }
709 725
710 } // namespace autofill 726 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/renderer/autofill/password_autofill_agent_browsertest.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698