OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
4 | |
5 #include "base/strings/stringprintf.h" | |
6 #include "chrome/browser/autofill/autofill_uitest_util.h" | |
7 #include "chrome/browser/chrome_notification_types.h" | |
8 #include "chrome/browser/ui/browser_window.h" | |
9 #include "chrome/browser/ui/chrome_pages.h" | |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "chrome/test/base/in_process_browser_test.h" | |
13 #include "chrome/test/base/interactive_test_utils.h" | |
14 #include "content/public/test/browser_test_utils.h" | |
15 | |
16 namespace { | |
17 | |
18 // This class tests the Autofill options settings. | |
19 // This test is part of the interactive_ui_tests instead of browser_tests | |
20 // because it is necessary to emulate pushing the tab key. | |
21 class AutofillOptionsWebUITest : public InProcessBrowserTest { | |
22 public: | |
23 AutofillOptionsWebUITest() {} | |
24 | |
25 // Navigate to the autofillEditAddress page. | |
26 void SetUpOnMainThread() override { | |
27 const GURL url = chrome::GetSettingsUrl("autofillEditAddress"); | |
28 ui_test_utils::NavigateToURL(browser(), url); | |
29 } | |
30 | |
31 protected: | |
32 const std::string kEditAddressOverlaySelector = | |
33 "#autofill-edit-address-overlay"; | |
34 | |
35 content::RenderFrameHost* GetActiveFrame() { | |
36 return GetActiveWebContents()->GetFocusedFrame(); | |
37 } | |
38 | |
39 content::RenderViewHost* GetRenderViewHost() { | |
40 return GetActiveWebContents()->GetRenderViewHost(); | |
41 } | |
42 | |
43 content::WebContents* GetActiveWebContents() { | |
44 return browser()->tab_strip_model()->GetActiveWebContents(); | |
45 } | |
46 | |
47 // Returns true if element contains document.activeElement. | |
48 bool ContainsActiveElement(const std::string& element_selector) { | |
49 const std::string script = base::StringPrintf( | |
50 "domAutomationController.send(" | |
51 "document.querySelector('%s').contains(document.activeElement));", | |
52 element_selector.c_str()); | |
53 bool result; | |
54 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
55 GetActiveFrame(), | |
56 script, | |
57 &result)); | |
58 return result; | |
59 } | |
60 | |
61 // Returns the number of items in the list. | |
62 int GetListSize(const std::string& list_selector) { | |
63 const std::string script = base::StringPrintf( | |
64 "domAutomationController.send(" | |
65 "document.querySelector('%s').items.length);", | |
66 list_selector.c_str()); | |
67 int length = -1; | |
68 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( | |
69 GetActiveFrame(), | |
70 script, | |
71 &length)); | |
72 return length; | |
73 } | |
74 | |
75 // Focus the first input field of the first list item. | |
76 void FocusFirstListItemInput(const std::string& list_selector) { | |
77 const std::string script = base::StringPrintf( | |
78 "document.querySelector('%s input').focus();", | |
79 list_selector.c_str()); | |
80 EXPECT_TRUE(content::ExecuteScript(GetActiveFrame(), script)); | |
81 } | |
82 | |
83 // Returns the text of the first item in the list. | |
84 std::string GetFirstListItemText(const std::string& list_selector) { | |
85 // EXPECT_TRUE will fail if there is no first item or first item does not | |
86 // have 'input'. | |
87 const std::string script = base::StringPrintf( | |
88 "domAutomationController.send(" | |
89 "document.querySelector('%s input').value);", | |
90 list_selector.c_str()); | |
91 std::string result; | |
92 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
93 GetActiveFrame(), | |
94 script, | |
95 &result)); | |
96 return result; | |
97 } | |
98 | |
99 // Returns true if the first item in the list has 'selected' attribute. | |
100 bool GetFirstListItemSelected(const std::string& list_selector) { | |
101 // EXPECT_TRUE will fail if there is no first item. | |
102 const std::string script = base::StringPrintf( | |
103 "domAutomationController.send(" | |
104 "document.querySelector('%s').items[0].hasAttribute('selected'));", | |
105 list_selector.c_str()); | |
106 bool result = false; | |
107 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
108 GetActiveFrame(), | |
109 script, | |
110 &result)); | |
111 return result; | |
112 } | |
113 | |
114 // Returns true if a row delete button ('X' button) is focused. | |
115 bool GetDeleteButtonFocused() { | |
116 const std::string script = | |
117 "domAutomationController.send(" | |
118 "document.activeElement.classList.contains('row-delete-button'));"; | |
119 bool result = false; | |
120 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
121 GetActiveFrame(), | |
122 script, | |
123 &result)); | |
124 return result; | |
125 } | |
126 | |
127 // Insert text into currently focused element. | |
128 void InsertText(const std::string& text) { | |
129 ASSERT_EQ(std::string::npos, text.find("'")); | |
130 const std::string script = base::StringPrintf( | |
131 "document.execCommand('insertText', false, '%s');", | |
132 text.c_str()); | |
133 EXPECT_TRUE(content::ExecuteScript(GetActiveFrame(), script)); | |
134 } | |
135 | |
136 // Press and release tab key in the browser. This will wait for the element on | |
137 // the page to change. | |
138 bool PressTab(bool shift) { | |
139 return ui_test_utils::SendKeyPressAndWait( | |
140 browser(), | |
141 ui::VKEY_TAB, | |
142 false, | |
143 shift, | |
144 false, | |
145 false, | |
146 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, | |
147 content::Source<content::RenderViewHost>(GetRenderViewHost())); | |
148 } | |
149 | |
150 void InitializeDomMessageQueue() { | |
151 dom_message_queue_.reset(new content::DOMMessageQueue); | |
152 } | |
153 | |
154 // Wait for a message from the DOM automation controller. | |
155 void WaitForDomMessage(const std::string& message) { | |
156 const std::string expected = "\"" + message + "\""; | |
157 std::string received; | |
158 do { | |
159 ASSERT_TRUE(dom_message_queue_->WaitForMessage(&received)); | |
160 } while (received != expected); | |
161 } | |
162 | |
163 // Add an event listener to send a DOM automation controller message from | |
164 // JavaScript each time validation completes for the list. | |
165 void ListenForDoneValidating(const std::string& list_selector) { | |
166 // doneValidating will execute the 'then' function immediately if no | |
167 // validations are pending, so wait for 'commitedit' event before calling | |
168 // doneValidating. | |
169 const std::string script = base::StringPrintf( | |
170 "document.querySelector('%s').addEventListener('commitedit'," | |
171 "function() {" | |
172 "document.querySelector('%s').doneValidating().then(function() {" | |
173 "domAutomationController.setAutomationId(0);" | |
174 "domAutomationController.send('done validating');" | |
175 "});" | |
176 "});", | |
177 list_selector.c_str(), | |
178 list_selector.c_str()); | |
179 | |
180 EXPECT_TRUE(content::ExecuteScript( | |
181 GetActiveFrame(), | |
182 script)); | |
183 } | |
184 | |
185 // Verifies that everything is the way it should be after list item is | |
186 // added or edited. | |
187 void VerifyEditAddressListPostConditions(const std::string& list_selector, | |
188 const std::string& input_text, | |
189 bool list_requires_validation) { | |
190 // Verify that neither the list nor any of its children still have focus. | |
191 EXPECT_FALSE(ContainsActiveElement(list_selector)); | |
192 | |
193 // Verify that focus moved to a different element of the overlay. | |
194 EXPECT_TRUE(ContainsActiveElement(kEditAddressOverlaySelector)); | |
195 | |
196 // Verify that list has exactly two items. They will be the item that was | |
197 // just added/modified + the placeholder. | |
198 EXPECT_EQ(2, GetListSize(list_selector)); | |
199 | |
200 // Verify that the first list item has the string that was inserted. | |
201 EXPECT_EQ(input_text, GetFirstListItemText(list_selector)); | |
202 | |
203 // TODO(bondd): phone list doesn't select first item after validation. | |
204 // It becomes selected later when the list is given focus. | |
bondd
2015/01/29 19:01:02
I'm leaving this as a TODO because IMO it's well p
| |
205 if (!list_requires_validation) { | |
206 // Verify that the first list item is the selected item in the list. | |
207 EXPECT_TRUE(GetFirstListItemSelected(list_selector)); | |
208 } | |
209 } | |
210 | |
211 // Make sure that when text is entered in the placeholder of an empty list and | |
212 // the tab key is pressed: | |
213 // + Focus leaves the list and goes to a different element on the page. | |
214 // + The text stays added and a new placeholder is created. | |
215 // + The list item with the newly added text is the selected list item (not | |
216 // the placeholder). | |
217 // | |
218 // Added to prevent http://crbug.com/440760 from regressing again. | |
219 void TestEditAddressListTabKeyAddItem(const std::string& list_selector, | |
220 const std::string& input_text, | |
221 bool list_requires_validation) { | |
222 // Focus the input field and insert test string. | |
223 FocusFirstListItemInput(list_selector); | |
224 InsertText(input_text); | |
225 | |
226 // Press tab key to move to next element after the list. | |
227 PressTab(false); | |
228 | |
229 if (list_requires_validation) | |
230 WaitForDomMessage("done validating"); | |
231 | |
232 // Make sure everything ended up the way it should be. | |
233 VerifyEditAddressListPostConditions(list_selector, input_text, | |
234 list_requires_validation); | |
235 } | |
236 | |
237 // Depends on state set up by TestEditAddressListTabKeyAddItem. Should be | |
238 // called immediately after that method. | |
239 // | |
240 // Make sure that when a list item's text is edited and the tab key is | |
241 // pressed twice: | |
242 // + After the first tab press the item's delete button is focused. | |
243 // + After the second tab press focus leaves the list and goes to a | |
244 // different element on the page. | |
245 // + The edited text persists. | |
246 // + The edited list item is the selected list item. | |
247 // | |
248 // Added to prevent http://crbug.com/443491 from regressing again. | |
249 void TestEditAddressListTabKeyEditItem(const std::string& list_selector, | |
250 const std::string& input_text, | |
251 bool list_requires_validation) { | |
252 // Press shift+tab to move back to the first item in the list. | |
253 PressTab(true); | |
254 // Verify that the first item in the list is focused. | |
255 EXPECT_TRUE(ContainsActiveElement(list_selector + " input")); | |
256 | |
257 // Insert modified text in the first list item. | |
258 std::string second_input = "second" + input_text; | |
259 InsertText(second_input); | |
260 | |
261 // Press tab key to focus the list item's delete button. | |
262 PressTab(false); | |
263 EXPECT_TRUE(GetDeleteButtonFocused()); | |
264 | |
265 // Press tab key again to move to next element after the list. | |
266 PressTab(false); | |
267 | |
268 if (list_requires_validation) | |
269 WaitForDomMessage("done validating"); | |
270 | |
271 // Make sure everything ended up the way it should be. | |
272 VerifyEditAddressListPostConditions(list_selector, second_input, | |
273 list_requires_validation); | |
274 } | |
275 | |
276 void TestEditAddressListTabKey(const std::string& field_name, | |
277 const std::string& input_text, | |
278 bool list_requires_validation) { | |
279 const std::string list_selector = kEditAddressOverlaySelector + " [field=" + | |
280 field_name + "]"; | |
281 | |
282 if (list_requires_validation) { | |
283 InitializeDomMessageQueue(); | |
284 ListenForDoneValidating(list_selector); | |
285 } | |
286 TestEditAddressListTabKeyAddItem(list_selector, input_text, | |
287 list_requires_validation); | |
288 TestEditAddressListTabKeyEditItem(list_selector, input_text, | |
289 list_requires_validation); | |
290 } | |
291 | |
292 private: | |
293 scoped_ptr<content::DOMMessageQueue> dom_message_queue_; | |
294 | |
295 DISALLOW_COPY_AND_ASSIGN(AutofillOptionsWebUITest); | |
296 }; | |
297 | |
298 } // namespace | |
299 | |
300 // Test the 'fullName' InlineEditableItemList in autofillEditAddress overlay. | |
301 IN_PROC_BROWSER_TEST_F(AutofillOptionsWebUITest, | |
302 TestEditAddressNameListTabKey) { | |
303 TestEditAddressListTabKey("fullName", "Test Name", false); | |
304 } | |
305 | |
306 // Test the 'phone' InlineEditableItemList in autofillEditAddress overlay. | |
307 IN_PROC_BROWSER_TEST_F(AutofillOptionsWebUITest, | |
308 TestEditAddressPhoneListTabKey) { | |
309 autofill::CreateTestProfile(browser()); | |
310 TestEditAddressListTabKey("phone", "123-456-7890", true); | |
311 } | |
312 | |
313 // Test the 'email' InlineEditableItemList in autofillEditAddress overlay. | |
314 IN_PROC_BROWSER_TEST_F(AutofillOptionsWebUITest, | |
315 TestEditAddressEmailListTabKey) { | |
316 TestEditAddressListTabKey("email", "test@example.com", false); | |
317 } | |
OLD | NEW |