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