OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "chrome/browser/password_manager/chrome_password_manager_client.h" | 6 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
7 #include "chrome/browser/password_manager/password_generation_manager.h" | 7 #include "chrome/browser/password_manager/password_generation_manager.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h" | 9 #include "chrome/browser/ui/autofill/password_generation_popup_observer.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
13 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
14 #include "components/autofill/core/browser/autofill_test_utils.h" | 14 #include "components/autofill/core/browser/autofill_test_utils.h" |
15 #include "components/autofill/core/common/autofill_switches.h" | 15 #include "components/autofill/core/common/autofill_switches.h" |
16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
19 #include "net/test/embedded_test_server/embedded_test_server.h" | 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/events/keycodes/keyboard_codes.h" | 21 #include "ui/events/keycodes/keyboard_codes.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 class TestPopupObserver : public autofill::PasswordGenerationPopupObserver { | 25 class TestPopupObserver : public autofill::PasswordGenerationPopupObserver { |
26 public: | 26 public: |
27 TestPopupObserver() | 27 TestPopupObserver() |
28 : popup_showing_(false) {} | 28 : popup_showing_(false), |
| 29 password_visible_(false) {} |
29 virtual ~TestPopupObserver() {} | 30 virtual ~TestPopupObserver() {} |
30 | 31 |
31 virtual void OnPopupShown() OVERRIDE { | 32 virtual void OnPopupShown(bool password_visible) OVERRIDE { |
32 popup_showing_ = true; | 33 popup_showing_ = true; |
| 34 password_visible_ = password_visible; |
33 } | 35 } |
34 | 36 |
35 virtual void OnPopupHidden() OVERRIDE { | 37 virtual void OnPopupHidden() OVERRIDE { |
36 popup_showing_ = false; | 38 popup_showing_ = false; |
37 } | 39 } |
38 | 40 |
39 bool popup_showing() { return popup_showing_; } | 41 bool popup_showing() { return popup_showing_; } |
| 42 bool password_visible() { return password_visible_; } |
40 | 43 |
41 private: | 44 private: |
42 bool popup_showing_; | 45 bool popup_showing_; |
| 46 bool password_visible_; |
43 }; | 47 }; |
44 | 48 |
45 } // namespace | 49 } // namespace |
46 | 50 |
47 class PasswordGenerationInteractiveTest : public InProcessBrowserTest { | 51 class PasswordGenerationInteractiveTest : public InProcessBrowserTest { |
48 public: | 52 public: |
49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 53 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
50 // Make sure the feature is enabled. | 54 // Make sure the feature is enabled. |
51 command_line->AppendSwitch(autofill::switches::kEnablePasswordGeneration); | 55 command_line->AppendSwitch(autofill::switches::kEnablePasswordGeneration); |
52 | 56 |
53 // Don't require ping from autofill or blacklist checking. | 57 // Don't require ping from autofill or blacklist checking. |
54 command_line->AppendSwitch( | 58 command_line->AppendSwitch( |
55 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration); | 59 autofill::switches::kLocalHeuristicsOnlyForPasswordGeneration); |
56 } | 60 } |
57 | 61 |
58 virtual void SetUpOnMainThread() OVERRIDE { | 62 virtual void SetUpOnMainThread() OVERRIDE { |
59 // Disable Autofill requesting access to AddressBook data. This will cause | 63 // Disable Autofill requesting access to AddressBook data. This will cause |
60 // test tests to hang on Mac. | 64 // the tests to hang on Mac. |
61 autofill::test::DisableSystemServices(browser()->profile()); | 65 autofill::test::DisableSystemServices(browser()->profile()); |
62 | 66 |
63 // Set observer for popup. | 67 // Set observer for popup. |
64 PasswordGenerationManager* generation_manager = | 68 PasswordGenerationManager* generation_manager = |
65 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( | 69 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( |
66 GetWebContents()); | 70 GetWebContents()); |
67 generation_manager->SetTestObserver(&observer_); | 71 generation_manager->SetTestObserver(&observer_); |
68 | 72 |
69 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 73 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
70 GURL url = embedded_test_server()->GetURL("/password/signup_form.html"); | 74 GURL url = embedded_test_server()->GetURL("/password/signup_form.html"); |
71 ui_test_utils::NavigateToURL(browser(), url); | 75 ui_test_utils::NavigateToURL(browser(), url); |
72 } | 76 } |
73 | 77 |
74 virtual void CleanUpOnMainThread() OVERRIDE { | 78 virtual void CleanUpOnMainThread() OVERRIDE { |
75 // Cleanup UI. | 79 // Clean up UI. |
76 PasswordGenerationManager* generation_manager = | 80 PasswordGenerationManager* generation_manager = |
77 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( | 81 ChromePasswordManagerClient::GetGenerationManagerFromWebContents( |
78 GetWebContents()); | 82 GetWebContents()); |
79 generation_manager->HidePopup(); | 83 generation_manager->HidePopup(); |
80 } | 84 } |
81 | 85 |
82 content::WebContents* GetWebContents() { | 86 content::WebContents* GetWebContents() { |
83 return browser()->tab_strip_model()->GetActiveWebContents(); | 87 return browser()->tab_strip_model()->GetActiveWebContents(); |
84 } | 88 } |
85 | 89 |
86 content::RenderViewHost* GetRenderViewHost() { | 90 content::RenderViewHost* GetRenderViewHost() { |
87 return GetWebContents()->GetRenderViewHost(); | 91 return GetWebContents()->GetRenderViewHost(); |
88 } | 92 } |
89 | 93 |
90 std::string GetFieldValue(const std::string& field_id) { | 94 std::string GetFieldValue(const std::string& field_id) { |
91 std::string value; | 95 std::string value; |
92 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 96 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
93 GetRenderViewHost(), | 97 GetRenderViewHost(), |
94 "window.domAutomationController.send(" | 98 "window.domAutomationController.send(" |
95 " document.getElementById('" + field_id + "').value);", | 99 " document.getElementById('" + field_id + "').value);", |
96 &value)); | 100 &value)); |
97 return value; | 101 return value; |
98 } | 102 } |
99 | 103 |
| 104 std::string GetFocusedElement() { |
| 105 std::string focused_element; |
| 106 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 107 GetRenderViewHost(), |
| 108 "window.domAutomationController.send(" |
| 109 " document.activeElement.id)", |
| 110 &focused_element)); |
| 111 return focused_element; |
| 112 } |
| 113 |
100 void FocusPasswordField() { | 114 void FocusPasswordField() { |
101 ASSERT_TRUE(content::ExecuteScript( | 115 ASSERT_TRUE(content::ExecuteScript( |
102 GetRenderViewHost(), | 116 GetRenderViewHost(), |
103 "document.getElementById('password_field').focus()")); | 117 "document.getElementById('password_field').focus()")); |
104 } | 118 } |
105 | 119 |
106 void SendKeyToPopup(ui::KeyboardCode key) { | 120 void SendKeyToPopup(ui::KeyboardCode key) { |
107 content::NativeWebKeyboardEvent event; | 121 content::NativeWebKeyboardEvent event; |
108 event.windowsKeyCode = key; | 122 event.windowsKeyCode = key; |
109 event.type = blink::WebKeyboardEvent::RawKeyDown; | 123 event.type = blink::WebKeyboardEvent::RawKeyDown; |
110 GetRenderViewHost()->ForwardKeyboardEvent(event); | 124 GetRenderViewHost()->ForwardKeyboardEvent(event); |
111 } | 125 } |
112 | 126 |
113 bool popup_showing() { | 127 bool GenerationPopupShowing() { |
114 return observer_.popup_showing(); | 128 return observer_.popup_showing() && observer_.password_visible(); |
| 129 } |
| 130 |
| 131 bool EditingPopupShowing() { |
| 132 return observer_.popup_showing() && !observer_.password_visible(); |
115 } | 133 } |
116 | 134 |
117 private: | 135 private: |
118 TestPopupObserver observer_; | 136 TestPopupObserver observer_; |
119 }; | 137 }; |
120 | 138 |
121 #if defined(USE_AURA) | 139 #if defined(USE_AURA) |
122 // Enabled on these platforms | 140 // Enabled on these platforms. |
123 #define MAYBE_PopupShownAndPasswordSelected PopupShownAndPasswordSelected | 141 #define MAYBE_PopupShownAndPasswordSelected PopupShownAndPasswordSelected |
124 #define MAYBE_PopupShownAndDismissed PopupShownAndDismissed | 142 #define MAYBE_PopupShownAndDismissed PopupShownAndDismissed |
125 #else | 143 #else |
126 // Popup not enabled for these platforms yet. | 144 // Popup not enabled for these platforms yet. |
127 #define MAYBE_PopupShownAndPasswordSelected DISABLED_PopupShownAndPasswordSelect
ed | 145 #define MAYBE_PopupShownAndPasswordSelected DISABLED_PopupShownAndPasswordSelect
ed |
128 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed | 146 #define MAYBE_PopupShownAndDismissed DISABLED_PopupShownAndDismissed |
129 #endif | 147 #endif |
130 | 148 |
131 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, | 149 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, |
132 MAYBE_PopupShownAndPasswordSelected) { | 150 MAYBE_PopupShownAndPasswordSelected) { |
133 FocusPasswordField(); | 151 FocusPasswordField(); |
134 EXPECT_TRUE(popup_showing()); | 152 EXPECT_TRUE(GenerationPopupShowing()); |
135 SendKeyToPopup(ui::VKEY_DOWN); | 153 SendKeyToPopup(ui::VKEY_DOWN); |
136 SendKeyToPopup(ui::VKEY_RETURN); | 154 SendKeyToPopup(ui::VKEY_RETURN); |
137 | 155 |
| 156 // Selecting the password should fill the field and move focus to the |
| 157 // submit button. |
138 EXPECT_FALSE(GetFieldValue("password_field").empty()); | 158 EXPECT_FALSE(GetFieldValue("password_field").empty()); |
| 159 EXPECT_FALSE(GenerationPopupShowing()); |
| 160 EXPECT_FALSE(EditingPopupShowing()); |
| 161 EXPECT_EQ("input_submit_button", GetFocusedElement()); |
| 162 |
| 163 // Re-focusing the password field should show the editing popup. |
| 164 FocusPasswordField(); |
| 165 EXPECT_TRUE(EditingPopupShowing()); |
139 } | 166 } |
140 | 167 |
141 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, | 168 IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest, |
142 MAYBE_PopupShownAndDismissed) { | 169 MAYBE_PopupShownAndDismissed) { |
143 FocusPasswordField(); | 170 FocusPasswordField(); |
144 EXPECT_TRUE(popup_showing()); | 171 EXPECT_TRUE(GenerationPopupShowing()); |
145 | 172 |
146 SendKeyToPopup(ui::VKEY_ESCAPE); | 173 SendKeyToPopup(ui::VKEY_ESCAPE); |
147 | 174 |
148 // Popup is dismissed. | 175 // Popup is dismissed. |
149 EXPECT_FALSE(popup_showing()); | 176 EXPECT_FALSE(GenerationPopupShowing()); |
150 } | 177 } |
OLD | NEW |