| 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/macros.h" | |
| 6 #include "base/strings/stringprintf.h" | |
| 7 #include "base/test/scoped_feature_list.h" | |
| 8 #include "build/build_config.h" | |
| 9 #include "chrome/browser/chrome_notification_types.h" | |
| 10 #include "chrome/browser/ui/browser_window.h" | |
| 11 #include "chrome/browser/ui/chrome_pages.h" | |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 13 #include "chrome/common/chrome_features.h" | |
| 14 #include "chrome/common/url_constants.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chrome/test/base/interactive_test_utils.h" | |
| 17 #include "content/public/test/browser_test_utils.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // This class tests the language dictionary 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 LanguageDictionaryWebUITest : public InProcessBrowserTest { | |
| 25 public: | |
| 26 LanguageDictionaryWebUITest() {} | |
| 27 | |
| 28 // Navigate to the editDictionary page. | |
| 29 void SetUpOnMainThread() override { | |
| 30 disable_md_settings_.InitAndDisableFeature( | |
| 31 features::kMaterialDesignSettings); | |
| 32 const GURL url = chrome::GetSettingsUrl("editDictionary"); | |
| 33 ui_test_utils::NavigateToURL(browser(), url); | |
| 34 } | |
| 35 | |
| 36 protected: | |
| 37 const std::string kDictionaryListSelector = | |
| 38 "#language-dictionary-overlay-word-list"; | |
| 39 | |
| 40 content::RenderFrameHost* GetActiveFrame() { | |
| 41 return GetActiveWebContents()->GetFocusedFrame(); | |
| 42 } | |
| 43 | |
| 44 content::RenderViewHost* GetRenderViewHost() { | |
| 45 return GetActiveWebContents()->GetRenderViewHost(); | |
| 46 } | |
| 47 | |
| 48 content::WebContents* GetActiveWebContents() { | |
| 49 return browser()->tab_strip_model()->GetActiveWebContents(); | |
| 50 } | |
| 51 | |
| 52 // Add a few test words to the dictionary. | |
| 53 void SetTestWords(const std::string& list_selector) { | |
| 54 const std::string script = base::StringPrintf( | |
| 55 "document.querySelector('%s').setWordList(['cat', 'dog', 'bird']);", | |
| 56 list_selector.c_str()); | |
| 57 EXPECT_TRUE(content::ExecuteScript(GetActiveFrame(), script)); | |
| 58 // Expected list size is 4: 3 word items + 1 placeholder. | |
| 59 EXPECT_EQ(4, GetListSize(list_selector)); | |
| 60 } | |
| 61 | |
| 62 // Returns the number of items in the list. | |
| 63 int GetListSize(const std::string& list_selector) { | |
| 64 const std::string script = base::StringPrintf( | |
| 65 "domAutomationController.send(" | |
| 66 "document.querySelector('%s').items.length);", | |
| 67 list_selector.c_str()); | |
| 68 int length = -1; | |
| 69 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( | |
| 70 GetActiveFrame(), | |
| 71 script, | |
| 72 &length)); | |
| 73 return length; | |
| 74 } | |
| 75 | |
| 76 // Returns true if element contains document.activeElement. | |
| 77 bool ContainsActiveElement(const std::string& element_selector) { | |
| 78 const std::string script = base::StringPrintf( | |
| 79 "domAutomationController.send(" | |
| 80 "document.querySelector('%s').contains(document.activeElement));", | |
| 81 element_selector.c_str()); | |
| 82 bool result; | |
| 83 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 84 GetActiveFrame(), | |
| 85 script, | |
| 86 &result)); | |
| 87 return result; | |
| 88 } | |
| 89 | |
| 90 // Returns true if list item[|index|] contains document.activeElement. | |
| 91 bool ListItemContainsActiveElement(const std::string& list_selector, | |
| 92 int index) { | |
| 93 EXPECT_GE(index, 0); | |
| 94 // EXPECT_TRUE will fail if index is out of bounds. | |
| 95 const std::string script = base::StringPrintf( | |
| 96 "domAutomationController.send(" | |
| 97 "document.querySelector('%s').items[%d].contains(" | |
| 98 "document.activeElement));", | |
| 99 list_selector.c_str(), | |
| 100 index); | |
| 101 bool result; | |
| 102 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 103 GetActiveFrame(), | |
| 104 script, | |
| 105 &result)); | |
| 106 return result; | |
| 107 } | |
| 108 | |
| 109 // Returns true if list item[|index|] has 'selected' attribute. | |
| 110 bool ListItemSelected(const std::string& list_selector, int index) { | |
| 111 EXPECT_GE(index, 0); | |
| 112 // EXPECT_TRUE will fail if index is out of bounds. | |
| 113 const std::string script = base::StringPrintf( | |
| 114 "domAutomationController.send(" | |
| 115 "document.querySelector('%s').items[%d].hasAttribute('selected'));", | |
| 116 list_selector.c_str(), | |
| 117 index); | |
| 118 bool result = false; | |
| 119 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 120 GetActiveFrame(), | |
| 121 script, | |
| 122 &result)); | |
| 123 return result; | |
| 124 } | |
| 125 | |
| 126 // Returns true if list item[|index|] has 'selected' attribute and contains | |
| 127 // document.activeElement. | |
| 128 bool ListItemSelectedAndFocused(const std::string& list_selector, | |
| 129 int index) { | |
| 130 EXPECT_GE(index, 0); | |
| 131 return ListItemSelected(list_selector, index) && | |
| 132 ListItemContainsActiveElement(list_selector, index); | |
| 133 } | |
| 134 | |
| 135 // Press and release a key in the browser. This will wait for the element on | |
| 136 // the page to change. | |
| 137 bool PressKey(ui::KeyboardCode key_code, bool shift) { | |
| 138 return ui_test_utils::SendKeyPressAndWait( | |
| 139 browser(), | |
| 140 key_code, | |
| 141 false, | |
| 142 shift, | |
| 143 false, | |
| 144 false, | |
| 145 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, | |
| 146 content::Source<content::RenderViewHost>(GetRenderViewHost())); | |
| 147 } | |
| 148 | |
| 149 void InitializeDomMessageQueue() { | |
| 150 dom_message_queue_.reset(new content::DOMMessageQueue); | |
| 151 } | |
| 152 | |
| 153 // Wait for a message from the DOM automation controller. | |
| 154 void WaitForDomMessage(const std::string& message) { | |
| 155 const std::string expected = "\"" + message + "\""; | |
| 156 std::string received; | |
| 157 do { | |
| 158 ASSERT_TRUE(dom_message_queue_->WaitForMessage(&received)); | |
| 159 } while (received != expected); | |
| 160 } | |
| 161 | |
| 162 // Add a JavaScript event listener to send a DOM automation controller message | |
| 163 // whenever the |selected| property of the list item changes. | |
| 164 void ListenForItemSelectedChange(const std::string& list_selector, | |
| 165 int index) { | |
| 166 EXPECT_GE(index, 0); | |
| 167 // EXPECT_TRUE will fail if index is out of bounds. | |
| 168 const std::string script = base::StringPrintf( | |
| 169 "document.querySelector('%s').items[%d].addEventListener(" | |
| 170 "'selectedChange', function() {" | |
| 171 "domAutomationController.setAutomationId(0);" | |
| 172 "domAutomationController.send('selected=' + this.selected);" | |
| 173 "});", | |
| 174 list_selector.c_str(), | |
| 175 index); | |
| 176 | |
| 177 EXPECT_TRUE(content::ExecuteScript( | |
| 178 GetActiveFrame(), | |
| 179 script)); | |
| 180 } | |
| 181 | |
| 182 private: | |
| 183 std::unique_ptr<content::DOMMessageQueue> dom_message_queue_; | |
| 184 base::test::ScopedFeatureList disable_md_settings_; | |
| 185 | |
| 186 DISALLOW_COPY_AND_ASSIGN(LanguageDictionaryWebUITest); | |
| 187 }; | |
| 188 | |
| 189 } // namespace | |
| 190 | |
| 191 // Test InlineEditableItemList keyboard focus behavior in editDictionary | |
| 192 // overlay. | |
| 193 // editDictionary overlay doesn't exist on OSX so disable it there. | |
| 194 #if !defined(OS_MACOSX) | |
| 195 | |
| 196 // Crashes on Win 7. http://crbug.com/500609 | |
| 197 #if defined(OS_WIN) | |
| 198 #define MAYBE_TestListKeyboardFocus DISABLED_TestListKeyboardFocus | |
| 199 #else | |
| 200 #define MAYBE_TestListKeyboardFocus TestListKeyboardFocus | |
| 201 #endif | |
| 202 | |
| 203 IN_PROC_BROWSER_TEST_F(LanguageDictionaryWebUITest, | |
| 204 MAYBE_TestListKeyboardFocus) { | |
| 205 const std::string list_selector = kDictionaryListSelector; | |
| 206 | |
| 207 // Populate the list with some test words. | |
| 208 SetTestWords(list_selector); | |
| 209 int placeholder_index = GetListSize(list_selector) - 1; | |
| 210 | |
| 211 // Listen for changes of the placeholder item's |selected| property so that | |
| 212 // test can wait until change has taken place after key press before | |
| 213 // continuing. | |
| 214 InitializeDomMessageQueue(); | |
| 215 ListenForItemSelectedChange(list_selector, placeholder_index); | |
| 216 | |
| 217 // Press tab to focus the placeholder. | |
| 218 PressKey(ui::VKEY_TAB, false); | |
| 219 | |
| 220 // Wait for placeholder item to become selected. | |
| 221 WaitForDomMessage("selected=true"); | |
| 222 | |
| 223 // Verify that the placeholder is selected and has focus. | |
| 224 EXPECT_TRUE(ListItemSelectedAndFocused(list_selector, placeholder_index)); | |
| 225 | |
| 226 // Press up arrow to select item above the placeholder. | |
| 227 PressKey(ui::VKEY_UP, false); | |
| 228 | |
| 229 // Wait for placeholder to become unselected. | |
| 230 WaitForDomMessage("selected=false"); | |
| 231 | |
| 232 // Verify that the placeholder is no longer selected. | |
| 233 EXPECT_FALSE(ListItemSelected(list_selector, placeholder_index)); | |
| 234 | |
| 235 // Verify that the item above the placeholder is selected and has focus. | |
| 236 EXPECT_TRUE(ListItemSelectedAndFocused(list_selector, | |
| 237 placeholder_index - 1)); | |
| 238 | |
| 239 // Press tab to leave the list. | |
| 240 PressKey(ui::VKEY_TAB, false); | |
| 241 | |
| 242 // Verify that focus has left the list. | |
| 243 EXPECT_FALSE(ContainsActiveElement(list_selector)); | |
| 244 | |
| 245 // Verify that the item above the placeholder is still selected. | |
| 246 EXPECT_TRUE(ListItemSelected(list_selector, placeholder_index - 1)); | |
| 247 | |
| 248 // Press shift+tab to go back to the list. | |
| 249 PressKey(ui::VKEY_TAB, true); | |
| 250 | |
| 251 // Verify that the item above the placeholder is selected and has focus. | |
| 252 EXPECT_TRUE(ListItemSelectedAndFocused(list_selector, | |
| 253 placeholder_index - 1)); | |
| 254 } | |
| 255 #endif // !defined(OS_MACOSX) | |
| OLD | NEW |