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.
5 #include "base/command_line.h"
6 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/common/autofill_switches.h"
15 #include "components/password_manager/core/browser/password_generation_manager.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
25 class TestPopupObserver
: public autofill::PasswordGenerationPopupObserver
{
28 : popup_showing_(false),
29 password_visible_(false) {}
30 virtual ~TestPopupObserver() {}
32 void OnPopupShown(bool password_visible
) override
{
33 popup_showing_
= true;
34 password_visible_
= password_visible
;
37 void OnPopupHidden() override
{ popup_showing_
= false; }
39 bool popup_showing() { return popup_showing_
; }
40 bool password_visible() { return password_visible_
; }
44 bool password_visible_
;
49 class PasswordGenerationInteractiveTest
: public InProcessBrowserTest
{
51 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
52 // Make sure the feature is enabled.
53 command_line
->AppendSwitch(autofill::switches::kEnablePasswordGeneration
);
55 // Don't require ping from autofill or blacklist checking.
56 command_line
->AppendSwitch(
57 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration
);
60 void SetUpOnMainThread() override
{
61 // Disable Autofill requesting access to AddressBook data. This will cause
62 // the tests to hang on Mac.
63 autofill::test::DisableSystemServices(browser()->profile()->GetPrefs());
65 // Set observer for popup.
66 ChromePasswordManagerClient
* client
=
67 ChromePasswordManagerClient::FromWebContents(GetWebContents());
68 client
->SetTestObserver(&observer_
);
70 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
71 GURL url
= embedded_test_server()->GetURL("/password/signup_form.html");
72 ui_test_utils::NavigateToURL(browser(), url
);
75 void TearDownOnMainThread() override
{
77 ChromePasswordManagerClient
* client
=
78 ChromePasswordManagerClient::FromWebContents(GetWebContents());
79 client
->HidePasswordGenerationPopup();
82 content::WebContents
* GetWebContents() {
83 return browser()->tab_strip_model()->GetActiveWebContents();
86 content::RenderViewHost
* GetRenderViewHost() {
87 return GetWebContents()->GetRenderViewHost();
90 std::string
GetFieldValue(const std::string
& field_id
) {
92 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
94 "window.domAutomationController.send("
95 " document.getElementById('" + field_id
+ "').value);",
100 std::string
GetFocusedElement() {
101 std::string focused_element
;
102 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
104 "window.domAutomationController.send("
105 " document.activeElement.id)",
107 return focused_element
;
110 void FocusPasswordField() {
111 ASSERT_TRUE(content::ExecuteScript(
113 "document.getElementById('password_field').focus()"));
116 void SendKeyToPopup(ui::KeyboardCode key
) {
117 content::NativeWebKeyboardEvent event
;
118 event
.windowsKeyCode
= key
;
119 event
.type
= blink::WebKeyboardEvent::RawKeyDown
;
120 GetRenderViewHost()->ForwardKeyboardEvent(event
);
123 bool GenerationPopupShowing() {
124 return observer_
.popup_showing() && observer_
.password_visible();
127 bool EditingPopupShowing() {
128 return observer_
.popup_showing() && !observer_
.password_visible();
132 TestPopupObserver observer_
;
135 #if defined(USE_AURA)
136 // Enabled on these platforms.
137 // Disabled due to flakiness, see http://crbug.com/407998
138 #define MAYBE_PopupShownAndPasswordSelected \
139 DISABLED_PopupShownAndPasswordSelected
140 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed
141 #define MAYBE_PopupShownAndDismissedByScrolling \
142 DISABLED_PopupShownAndDismissedByScrolling
144 // Popup not enabled for these platforms yet.
145 #define MAYBE_PopupShownAndPasswordSelected \
146 DISABLED_PopupShownAndPasswordSelected
147 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed
148 #define MAYBE_PopupShownAndDismissedByScrolling \
149 DISABLED_PopupShownAndDismissedByScrolling
152 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest
,
153 MAYBE_PopupShownAndPasswordSelected
) {
154 FocusPasswordField();
155 EXPECT_TRUE(GenerationPopupShowing());
156 SendKeyToPopup(ui::VKEY_DOWN
);
157 SendKeyToPopup(ui::VKEY_RETURN
);
159 // Selecting the password should fill the field and move focus to the
161 EXPECT_FALSE(GetFieldValue("password_field").empty());
162 EXPECT_FALSE(GenerationPopupShowing());
163 EXPECT_FALSE(EditingPopupShowing());
164 EXPECT_EQ("input_submit_button", GetFocusedElement());
166 // Re-focusing the password field should show the editing popup.
167 FocusPasswordField();
168 EXPECT_TRUE(EditingPopupShowing());
171 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest
,
172 MAYBE_PopupShownAndDismissed
) {
173 FocusPasswordField();
174 EXPECT_TRUE(GenerationPopupShowing());
176 SendKeyToPopup(ui::VKEY_ESCAPE
);
178 // Popup is dismissed.
179 EXPECT_FALSE(GenerationPopupShowing());
182 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest
,
183 MAYBE_PopupShownAndDismissedByScrolling
) {
184 FocusPasswordField();
185 EXPECT_TRUE(GenerationPopupShowing());
187 ASSERT_TRUE(content::ExecuteScript(GetRenderViewHost(),
188 "window.scrollTo(100, 0);"));
190 EXPECT_FALSE(GenerationPopupShowing());