| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/strings/string_number_conversions.h" | 6 #include "base/strings/string_number_conversions.h" |
| 7 #include "content/browser/accessibility/accessibility_test_utils.h" |
| 7 #include "content/browser/accessibility/browser_accessibility.h" | 8 #include "content/browser/accessibility/browser_accessibility.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/public/test/browser_test_utils.h" | 10 #include "content/public/test/browser_test_utils.h" |
| 10 #include "content/public/test/content_browser_test.h" | 11 #include "content/public/test/content_browser_test.h" |
| 11 #include "content/public/test/content_browser_test_utils.h" | 12 #include "content/public/test/content_browser_test_utils.h" |
| 12 #include "content/shell/browser/shell.h" | 13 #include "content/shell/browser/shell.h" |
| 13 #include "content/test/accessibility_browser_test_utils.h" | 14 #include "content/test/accessibility_browser_test_utils.h" |
| 15 #include "net/dns/mock_host_resolver.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 14 #include "ui/accessibility/ax_node_data.h" | 17 #include "ui/accessibility/ax_node_data.h" |
| 15 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
| 16 #include "ui/aura/window_tree_host.h" | 19 #include "ui/aura/window_tree_host.h" |
| 17 #include "ui/events/event.h" | 20 #include "ui/events/event.h" |
| 18 #include "ui/events/event_processor.h" | 21 #include "ui/events/event_processor.h" |
| 19 #include "ui/events/event_utils.h" | 22 #include "ui/events/event_utils.h" |
| 20 | 23 |
| 21 namespace content { | 24 namespace content { |
| 22 | 25 |
| 23 class TouchAccessibilityBrowserTest : public ContentBrowserTest { | 26 class TouchAccessibilityBrowserTest : public ContentBrowserTest { |
| 24 public: | 27 public: |
| 25 TouchAccessibilityBrowserTest() {} | 28 TouchAccessibilityBrowserTest() {} |
| 26 | 29 |
| 30 protected: |
| 31 void SetUpOnMainThread() override { |
| 32 host_resolver()->AddRule("*", "127.0.0.1"); |
| 33 ASSERT_TRUE(embedded_test_server()->Start()); |
| 34 SetupCrossSiteRedirector(embedded_test_server()); |
| 35 } |
| 36 |
| 37 void NavigateToUrlAndWaitForAccessibilityTree(const GURL& url) { |
| 38 AccessibilityNotificationWaiter waiter( |
| 39 shell(), AccessibilityModeComplete, ui::AX_EVENT_LOAD_COMPLETE); |
| 40 NavigateToURL(shell(), url); |
| 41 waiter.WaitForNotification(); |
| 42 } |
| 43 |
| 44 void SendTouchExplorationEvent(int x, int y) { |
| 45 aura::Window* window = shell()->web_contents()->GetContentNativeView(); |
| 46 ui::EventProcessor* dispatcher = window->GetHost()->event_processor(); |
| 47 gfx::Rect bounds = window->GetBoundsInRootWindow(); |
| 48 gfx::Point location(bounds.x() + x, bounds.y() + y); |
| 49 int flags = ui::EF_TOUCH_ACCESSIBILITY; |
| 50 std::unique_ptr<ui::Event> mouse_move_event( |
| 51 new ui::MouseEvent(ui::ET_MOUSE_MOVED, location, location, |
| 52 ui::EventTimeForNow(), flags, 0)); |
| 53 ignore_result(dispatcher->OnEventFromSource(mouse_move_event.get())); |
| 54 } |
| 55 |
| 27 DISALLOW_COPY_AND_ASSIGN(TouchAccessibilityBrowserTest); | 56 DISALLOW_COPY_AND_ASSIGN(TouchAccessibilityBrowserTest); |
| 28 }; | 57 }; |
| 29 | 58 |
| 30 IN_PROC_BROWSER_TEST_F(TouchAccessibilityBrowserTest, | 59 IN_PROC_BROWSER_TEST_F(TouchAccessibilityBrowserTest, |
| 31 TouchExplorationSendsHoverEvents) { | 60 TouchExplorationSendsHoverEvents) { |
| 32 // Create HTML with a 7 x 5 table, each exactly 50 x 50 pixels. | 61 // Create HTML with a 7 x 5 table, each exactly 50 x 50 pixels. |
| 33 std::string html_url = | 62 std::string html_url = |
| 34 "data:text/html," | 63 "data:text/html," |
| 35 "<style>" | 64 "<style>" |
| 36 " body { margin: 0; }" | 65 " body { margin: 0; }" |
| 37 " table { border-spacing: 0; border-collapse: collapse; }" | 66 " table { border-spacing: 0; border-collapse: collapse; }" |
| 38 " td { width: 50px; height: 50px; padding: 0; }" | 67 " td { width: 50px; height: 50px; padding: 0; }" |
| 39 "</style>" | 68 "</style>" |
| 40 "<body>" | 69 "<body>" |
| 41 "<table>"; | 70 "<table>"; |
| 42 int cell = 0; | 71 int cell = 0; |
| 43 for (int row = 0; row < 5; ++row) { | 72 for (int row = 0; row < 5; ++row) { |
| 44 html_url += "<tr>"; | 73 html_url += "<tr>"; |
| 45 for (int col = 0; col < 7; ++col) { | 74 for (int col = 0; col < 7; ++col) { |
| 46 html_url += "<td>" + base::IntToString(cell) + "</td>"; | 75 html_url += "<td>" + base::IntToString(cell) + "</td>"; |
| 47 ++cell; | 76 ++cell; |
| 48 } | 77 } |
| 49 html_url += "</tr>"; | 78 html_url += "</tr>"; |
| 50 } | 79 } |
| 51 html_url += "</table></body>"; | 80 html_url += "</table></body>"; |
| 52 | 81 |
| 53 // Navigate to the url and load the accessibility tree. | 82 NavigateToUrlAndWaitForAccessibilityTree(GURL(html_url)); |
| 54 AccessibilityNotificationWaiter waiter( | |
| 55 shell(), AccessibilityModeComplete, ui::AX_EVENT_LOAD_COMPLETE); | |
| 56 GURL url(html_url); | |
| 57 NavigateToURL(shell(), url); | |
| 58 waiter.WaitForNotification(); | |
| 59 | 83 |
| 60 // Get the BrowserAccessibilityManager. | 84 // Get the BrowserAccessibilityManager. |
| 61 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( | 85 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>( |
| 62 shell()->web_contents()); | 86 shell()->web_contents()); |
| 63 BrowserAccessibilityManager* manager = | 87 BrowserAccessibilityManager* manager = |
| 64 web_contents->GetRootBrowserAccessibilityManager(); | 88 web_contents->GetRootBrowserAccessibilityManager(); |
| 65 ASSERT_NE(nullptr, manager); | 89 ASSERT_NE(nullptr, manager); |
| 66 | 90 |
| 67 // Loop over all of the cells in the table. For each one, send a simulated | 91 // Loop over all of the cells in the table. For each one, send a simulated |
| 68 // touch exploration event in the center of that cell, and assert that we | 92 // touch exploration event in the center of that cell, and assert that we |
| 69 // get an accessibility hover event fired in the correct cell. | 93 // get an accessibility hover event fired in the correct cell. |
| 70 AccessibilityNotificationWaiter waiter2( | 94 AccessibilityNotificationWaiter waiter( |
| 71 shell(), AccessibilityModeComplete, ui::AX_EVENT_HOVER); | 95 shell(), AccessibilityModeComplete, ui::AX_EVENT_HOVER); |
| 72 aura::Window* window = web_contents->GetContentNativeView(); | |
| 73 ui::EventProcessor* dispatcher = window->GetHost()->event_processor(); | |
| 74 for (int row = 0; row < 5; ++row) { | 96 for (int row = 0; row < 5; ++row) { |
| 75 for (int col = 0; col < 7; ++col) { | 97 for (int col = 0; col < 7; ++col) { |
| 76 std::string expected_cell_text = base::IntToString(row * 7 + col); | 98 std::string expected_cell_text = base::IntToString(row * 7 + col); |
| 77 VLOG(1) << "Sending event in row " << row << " col " << col | 99 VLOG(1) << "Sending event in row " << row << " col " << col |
| 78 << " with text " << expected_cell_text; | 100 << " with text " << expected_cell_text; |
| 79 | 101 SendTouchExplorationEvent(50 * col + 25, 50 * row + 25); |
| 80 // Send a touch exploration event to the center of the particular grid | |
| 81 // cell. A touch exploration event is just a mouse move event with | |
| 82 // the ui::EF_TOUCH_ACCESSIBILITY flag set. | |
| 83 gfx::Rect bounds = window->GetBoundsInRootWindow(); | |
| 84 gfx::Point location(bounds.x() + 50 * col + 25, | |
| 85 bounds.y() + 50 * row + 25); | |
| 86 int flags = ui::EF_TOUCH_ACCESSIBILITY; | |
| 87 std::unique_ptr<ui::Event> mouse_move_event( | |
| 88 new ui::MouseEvent(ui::ET_MOUSE_MOVED, location, location, | |
| 89 ui::EventTimeForNow(), flags, 0)); | |
| 90 ignore_result(dispatcher->OnEventFromSource(mouse_move_event.get())); | |
| 91 | 102 |
| 92 // Wait until we get a hover event in the expected grid cell. | 103 // Wait until we get a hover event in the expected grid cell. |
| 93 // Tolerate additional events, keep looping until we get the expected one. | 104 // Tolerate additional events, keep looping until we get the expected one. |
| 94 std::string cell_text; | 105 std::string cell_text; |
| 95 do { | 106 do { |
| 96 waiter2.WaitForNotification(); | 107 waiter.WaitForNotification(); |
| 97 int target_id = waiter2.event_target_id(); | 108 int target_id = waiter.event_target_id(); |
| 98 BrowserAccessibility* hit = manager->GetFromID(target_id); | 109 BrowserAccessibility* hit = manager->GetFromID(target_id); |
| 99 BrowserAccessibility* child = hit->PlatformGetChild(0); | 110 BrowserAccessibility* child = hit->PlatformGetChild(0); |
| 100 ASSERT_NE(nullptr, child); | 111 ASSERT_NE(nullptr, child); |
| 101 cell_text = child->GetData().GetStringAttribute(ui::AX_ATTR_NAME); | 112 cell_text = child->GetData().GetStringAttribute(ui::AX_ATTR_NAME); |
| 102 VLOG(1) << "Got hover event in cell with text: " << cell_text; | 113 VLOG(1) << "Got hover event in cell with text: " << cell_text; |
| 103 } while (cell_text != expected_cell_text); | 114 } while (cell_text != expected_cell_text); |
| 104 } | 115 } |
| 105 } | 116 } |
| 106 } | 117 } |
| 107 | 118 |
| 119 IN_PROC_BROWSER_TEST_F(TouchAccessibilityBrowserTest, |
| 120 TouchExplorationInIframe) { |
| 121 NavigateToUrlAndWaitForAccessibilityTree( |
| 122 embedded_test_server()->GetURL( |
| 123 "/accessibility/html/iframe-coordinates.html")); |
| 124 |
| 125 WaitForAccessibilityTreeToContainNodeWithName( |
| 126 shell()->web_contents(), "Ordinary Button"); |
| 127 |
| 128 // Get the BrowserAccessibilityManager for the first child frame. |
| 129 RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>( |
| 130 shell()->web_contents()->GetMainFrame()); |
| 131 RenderFrameHostImpl* child_frame = |
| 132 main_frame->frame_tree_node()->child_at(0)->current_frame_host(); |
| 133 BrowserAccessibilityManager* child_manager = |
| 134 child_frame->GetOrCreateBrowserAccessibilityManager(); |
| 135 ASSERT_NE(nullptr, child_manager); |
| 136 |
| 137 // Send a touch exploration event to the button in the first iframe. |
| 138 // A touch exploration event is just a mouse move event with |
| 139 // the ui::EF_TOUCH_ACCESSIBILITY flag set. |
| 140 AccessibilityNotificationWaiter waiter( |
| 141 child_frame, ui::AX_EVENT_HOVER); |
| 142 SendTouchExplorationEvent(50, 350); |
| 143 waiter.WaitForNotification(); |
| 144 int target_id = waiter.event_target_id(); |
| 145 BrowserAccessibility* hit = child_manager->GetFromID(target_id); |
| 146 EXPECT_EQ(ui::AX_ROLE_BUTTON, hit->GetData().role); |
| 147 std::string text = hit->GetData().GetStringAttribute(ui::AX_ATTR_NAME); |
| 148 EXPECT_EQ("Ordinary Button", text); |
| 149 } |
| 150 |
| 151 IN_PROC_BROWSER_TEST_F(TouchAccessibilityBrowserTest, |
| 152 DISABLED_TouchExplorationInCrossSiteIframe) { |
| 153 NavigateToUrlAndWaitForAccessibilityTree( |
| 154 embedded_test_server()->GetURL( |
| 155 "/accessibility/html/iframe-coordinates-cross-process.html")); |
| 156 |
| 157 WaitForAccessibilityTreeToContainNodeWithName( |
| 158 shell()->web_contents(), "Ordinary Button"); |
| 159 |
| 160 // Get the BrowserAccessibilityManager for the first child frame. |
| 161 RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>( |
| 162 shell()->web_contents()->GetMainFrame()); |
| 163 RenderFrameHostImpl* child_frame = |
| 164 main_frame->frame_tree_node()->child_at(0)->current_frame_host(); |
| 165 BrowserAccessibilityManager* child_manager = |
| 166 child_frame->GetOrCreateBrowserAccessibilityManager(); |
| 167 ASSERT_NE(nullptr, child_manager); |
| 168 |
| 169 // Send a touch exploration event to the button in the first iframe. |
| 170 // A touch exploration event is just a mouse move event with |
| 171 // the ui::EF_TOUCH_ACCESSIBILITY flag set. |
| 172 AccessibilityNotificationWaiter waiter( |
| 173 child_frame, ui::AX_EVENT_HOVER); |
| 174 SendTouchExplorationEvent(50, 350); |
| 175 waiter.WaitForNotification(); |
| 176 int target_id = waiter.event_target_id(); |
| 177 BrowserAccessibility* hit = child_manager->GetFromID(target_id); |
| 178 EXPECT_EQ(ui::AX_ROLE_BUTTON, hit->GetData().role); |
| 179 std::string text = hit->GetData().GetStringAttribute(ui::AX_ATTR_NAME); |
| 180 EXPECT_EQ("Ordinary Button", text); |
| 181 } |
| 182 |
| 108 } // namespace content | 183 } // namespace content |
| OLD | NEW |