Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 1 | // Copyright 2019 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 <fuchsia/accessibility/semantics/cpp/fidl.h> |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 6 | #include <lib/ui/scenic/cpp/view_ref_pair.h> |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 7 | #include <zircon/types.h> |
| 8 | |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 9 | #include "content/public/test/browser_test.h" |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 10 | #include "fuchsia/base/frame_test_util.h" |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 11 | #include "fuchsia/base/test_navigation_listener.h" |
| 12 | #include "fuchsia/engine/browser/accessibility_bridge.h" |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 13 | #include "fuchsia/engine/browser/fake_semantics_manager.h" |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 14 | #include "fuchsia/engine/browser/frame_impl.h" |
| 15 | #include "fuchsia/engine/test/test_data.h" |
| 16 | #include "fuchsia/engine/test/web_engine_browser_test.h" |
| 17 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 19 | #include "ui/gfx/switches.h" |
| 20 | #include "ui/ozone/public/ozone_switches.h" |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 21 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | const char kPage1Path[] = "/ax1.html"; |
| 25 | const char kPage2Path[] = "/batching.html"; |
| 26 | const char kPage1Title[] = "accessibility 1"; |
| 27 | const char kPage2Title[] = "lots of nodes!"; |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 28 | const char kButtonName1[] = "a button"; |
| 29 | const char kButtonName2[] = "another button"; |
| 30 | const char kButtonName3[] = "button 3"; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 31 | const char kNodeName[] = "last node"; |
| 32 | const char kParagraphName[] = "a third paragraph"; |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 33 | const char kOffscreenNodeName[] = "offscreen node"; |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 34 | const size_t kPage1NodeCount = 29; |
Sharon Yang | aa2c9bd | 2019-11-21 17:59:22 | [diff] [blame] | 35 | const size_t kPage2NodeCount = 190; |
Sharon Yang | 2caf82e | 2020-10-12 18:00:35 | [diff] [blame] | 36 | const size_t kInitialRangeValue = 51; |
| 37 | const size_t kStepSize = 3; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 38 | |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 39 | fuchsia::math::PointF GetCenterOfBox(fuchsia::ui::gfx::BoundingBox box) { |
| 40 | fuchsia::math::PointF center; |
| 41 | center.x = (box.min.x + box.max.x) / 2; |
| 42 | center.y = (box.min.y + box.max.y) / 2; |
| 43 | return center; |
| 44 | } |
| 45 | |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 46 | // Creates an AXEventNotificationDetails that contains an AxTreeUpdate that |
| 47 | // builds a tree from scratch of the form: (1 (2 ... (|tree_size|))). |
| 48 | content::AXEventNotificationDetails CreateTreeAccessibilityEvent( |
| 49 | size_t tree_size) { |
| 50 | content::AXEventNotificationDetails event; |
| 51 | event.ax_tree_id = ui::AXTreeID ::CreateNewAXTreeID(); |
| 52 | ui::AXTreeUpdate update; |
| 53 | update.root_id = 1; |
| 54 | update.nodes.resize(tree_size); |
| 55 | |
| 56 | for (int i = 1; i <= static_cast<int>(tree_size); ++i) { |
| 57 | auto& node = update.nodes[i - 1]; // vector 0-indexed, IDs 1-indexed. |
| 58 | node.id = i; |
| 59 | node.child_ids.push_back(i + 1); |
| 60 | } |
| 61 | |
| 62 | // The deepest node does not have any child. |
| 63 | update.nodes.back().child_ids.clear(); |
| 64 | event.updates.push_back(std::move(update)); |
| 65 | return event; |
| 66 | } |
| 67 | |
| 68 | // Creates an AXEventNotificationDetails that contains |update|. |
| 69 | content::AXEventNotificationDetails CreateAccessibilityEventWithUpdate( |
| 70 | ui::AXTreeUpdate update) { |
| 71 | content::AXEventNotificationDetails event; |
| 72 | event.updates.push_back(std::move(update)); |
| 73 | return event; |
| 74 | } |
| 75 | |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame^] | 76 | // Returns whether or not the given node supports the given action. |
| 77 | bool HasAction(const fuchsia::accessibility::semantics::Node& node, |
| 78 | fuchsia::accessibility::semantics::Action action) { |
| 79 | for (const auto& node_action : node.actions()) { |
| 80 | if (node_action == action) |
| 81 | return true; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 86 | } // namespace |
| 87 | |
| 88 | class AccessibilityBridgeTest : public cr_fuchsia::WebEngineBrowserTest { |
| 89 | public: |
Wez | 7a5f5ca | 2020-08-17 07:41:08 | [diff] [blame] | 90 | AccessibilityBridgeTest() { |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 91 | cr_fuchsia::WebEngineBrowserTest::set_test_server_root( |
| 92 | base::FilePath(cr_fuchsia::kTestServerRoot)); |
| 93 | } |
| 94 | |
| 95 | ~AccessibilityBridgeTest() override = default; |
| 96 | |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 97 | AccessibilityBridgeTest(const AccessibilityBridgeTest&) = delete; |
| 98 | AccessibilityBridgeTest& operator=(const AccessibilityBridgeTest&) = delete; |
| 99 | |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 100 | void SetUp() override { |
| 101 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 102 | command_line->AppendSwitchNative(switches::kOzonePlatform, |
| 103 | switches::kHeadless); |
| 104 | command_line->AppendSwitch(switches::kHeadless); |
| 105 | cr_fuchsia::WebEngineBrowserTest::SetUp(); |
| 106 | } |
| 107 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 108 | void SetUpOnMainThread() override { |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 109 | frame_ptr_ = |
| 110 | cr_fuchsia::WebEngineBrowserTest::CreateFrame(&navigation_listener_); |
| 111 | frame_impl_ = context_impl()->GetFrameImplForTest(&frame_ptr_); |
Wez | 7a5f5ca | 2020-08-17 07:41:08 | [diff] [blame] | 112 | frame_impl_->set_semantics_manager_for_test(&semantics_manager_); |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 113 | frame_ptr_->EnableHeadlessRendering(); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 114 | |
Wez | 19de8b52 | 2020-01-28 20:50:04 | [diff] [blame] | 115 | semantics_manager_.WaitUntilViewRegistered(); |
| 116 | ASSERT_TRUE(semantics_manager_.is_view_registered()); |
| 117 | ASSERT_TRUE(semantics_manager_.is_listener_valid()); |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 118 | |
| 119 | frame_ptr_->GetNavigationController(navigation_controller_.NewRequest()); |
| 120 | ASSERT_TRUE(embedded_test_server()->Start()); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 121 | |
| 122 | // Change the accessibility mode on the Fuchsia side and check that it is |
| 123 | // propagated correctly. |
| 124 | ASSERT_FALSE(frame_impl_->web_contents_for_test() |
| 125 | ->IsWebContentsOnlyAccessibilityModeForTesting()); |
| 126 | |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 127 | semantics_manager_.SetSemanticsModeEnabled(true); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 128 | base::RunLoop().RunUntilIdle(); |
| 129 | |
| 130 | ASSERT_TRUE(frame_impl_->web_contents_for_test() |
| 131 | ->IsWebContentsOnlyAccessibilityModeForTesting()); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 132 | } |
| 133 | |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 134 | void LoadPage(base::StringPiece url, base::StringPiece page_title) { |
| 135 | GURL page_url(embedded_test_server()->GetURL(std::string(url))); |
| 136 | ASSERT_TRUE(cr_fuchsia::LoadUrlAndExpectResponse( |
| 137 | navigation_controller_.get(), fuchsia::web::LoadUrlParams(), |
| 138 | page_url.spec())); |
| 139 | navigation_listener_.RunUntilUrlAndTitleEquals(page_url, page_title); |
| 140 | } |
| 141 | |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 142 | // Helper function that checks if |num_deletes|, |num_updates| and |
| 143 | // |num_commits| match the ones in the FakeSemanticTree. |
| 144 | void CheckCallsToFakeSemanticTree(size_t num_deletes, |
| 145 | size_t num_updates, |
| 146 | size_t num_commits) { |
| 147 | auto* tree = semantics_manager_.semantic_tree(); |
| 148 | EXPECT_EQ(tree->num_delete_calls(), num_deletes); |
| 149 | EXPECT_EQ(tree->num_update_calls(), num_updates); |
| 150 | EXPECT_EQ(tree->num_commit_calls(), num_commits); |
| 151 | } |
| 152 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 153 | protected: |
| 154 | fuchsia::web::FramePtr frame_ptr_; |
| 155 | FrameImpl* frame_impl_; |
| 156 | FakeSemanticsManager semantics_manager_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 157 | cr_fuchsia::TestNavigationListener navigation_listener_; |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 158 | fuchsia::web::NavigationControllerPtr navigation_controller_; |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 159 | }; |
| 160 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 161 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, CorrectDataSent) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 162 | LoadPage(kPage1Path, kPage1Title); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 163 | |
| 164 | // Check that the data values are correct in the FakeSemanticTree. |
| 165 | // TODO(fxb/18796): Test more fields once Chrome to Fuchsia conversions are |
| 166 | // available. |
| 167 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
| 168 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 169 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kPage1Title)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 170 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 171 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 172 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 173 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Batching is performed when the number of nodes to send or delete exceeds the |
| 177 | // maximum, as set on the Fuchsia side. Check that all nodes are received by the |
| 178 | // Semantic Tree when batching is performed. |
| 179 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, DataSentWithBatching) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 180 | LoadPage(kPage2Path, kPage2Title); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 181 | |
| 182 | // Run until we expect more than a batch's worth of nodes to be present. |
| 183 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage2NodeCount); |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 184 | EXPECT_TRUE(semantics_manager_.semantic_tree()->GetNodeFromLabel(kNodeName)); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 185 | EXPECT_EQ(semantics_manager_.semantic_tree()->num_delete_calls(), 0u); |
| 186 | |
| 187 | // Checks if the actual batching happened. |
| 188 | EXPECT_GE(semantics_manager_.semantic_tree()->num_update_calls(), 18u); |
| 189 | EXPECT_EQ(semantics_manager_.semantic_tree()->num_commit_calls(), 1u); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Check that semantics information is correctly sent when navigating from page |
| 193 | // to page. |
| 194 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, TestNavigation) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 195 | LoadPage(kPage1Path, kPage1Title); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 196 | |
| 197 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 198 | EXPECT_EQ(semantics_manager_.semantic_tree()->num_commit_calls(), 1u); |
| 199 | |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 200 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 201 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kPage1Title)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 202 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 203 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 204 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 205 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 206 | |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 207 | LoadPage(kPage2Path, kPage2Title); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 208 | |
| 209 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage2NodeCount); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 210 | EXPECT_EQ(semantics_manager_.semantic_tree()->num_commit_calls(), 2u); |
| 211 | |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 212 | EXPECT_TRUE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 213 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kPage2Title)); |
| 214 | EXPECT_TRUE(semantics_manager_.semantic_tree()->GetNodeFromLabel(kNodeName)); |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 215 | |
| 216 | // Check that data from the first page has been deleted successfully. |
| 217 | EXPECT_FALSE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 218 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1)); |
Sharon Yang | ca61642 | 2019-12-18 23:52:39 | [diff] [blame] | 219 | EXPECT_FALSE( |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 220 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName)); |
Sharon Yang | fbb9ba4a | 2019-11-18 23:59:56 | [diff] [blame] | 221 | } |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 222 | |
| 223 | // Checks that the correct node ID is returned when performing hit testing. |
Wez | 99fac70b | 2020-02-07 15:26:10 | [diff] [blame] | 224 | // TODO(https://crbug.com/1050049): Re-enable once flake is fixed. |
| 225 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, DISABLED_HitTest) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 226 | LoadPage(kPage1Path, kPage1Title); |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 227 | |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 228 | fuchsia::accessibility::semantics::Node* hit_test_node = |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 229 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName); |
Sharon Yang | fb25b4a | 2020-02-10 23:50:15 | [diff] [blame] | 230 | EXPECT_TRUE(hit_test_node); |
Sharon Yang | 8b548ab9 | 2019-12-20 01:52:54 | [diff] [blame] | 231 | |
| 232 | fuchsia::math::PointF target_point = |
| 233 | GetCenterOfBox(hit_test_node->location()); |
| 234 | |
| 235 | EXPECT_EQ(hit_test_node->node_id(), |
| 236 | semantics_manager_.HitTestAtPointSync(std::move(target_point))); |
| 237 | |
| 238 | // Expect hit testing to return the root when the point given is out of |
| 239 | // bounds or there is no semantic node at that position. |
| 240 | target_point.x = -1; |
| 241 | target_point.y = -1; |
| 242 | EXPECT_EQ(0u, semantics_manager_.HitTestAtPointSync(std::move(target_point))); |
| 243 | target_point.x = 1; |
| 244 | target_point.y = 1; |
| 245 | EXPECT_EQ(0u, semantics_manager_.HitTestAtPointSync(std::move(target_point))); |
| 246 | } |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 247 | |
| 248 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, PerformDefaultAction) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 249 | LoadPage(kPage1Path, kPage1Title); |
| 250 | |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 251 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
| 252 | |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 253 | fuchsia::accessibility::semantics::Node* button1 = |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 254 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1); |
| 255 | EXPECT_TRUE(button1); |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 256 | fuchsia::accessibility::semantics::Node* button2 = |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 257 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName2); |
| 258 | EXPECT_TRUE(button2); |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 259 | fuchsia::accessibility::semantics::Node* button3 = |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 260 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName3); |
| 261 | EXPECT_TRUE(button3); |
| 262 | |
Tess Eisenberger | 77314d5 | 2020-10-12 23:30:21 | [diff] [blame^] | 263 | EXPECT_TRUE( |
| 264 | HasAction(*button1, fuchsia::accessibility::semantics::Action::DEFAULT)); |
| 265 | EXPECT_TRUE( |
| 266 | HasAction(*button2, fuchsia::accessibility::semantics::Action::DEFAULT)); |
| 267 | EXPECT_TRUE( |
| 268 | HasAction(*button3, fuchsia::accessibility::semantics::Action::DEFAULT)); |
| 269 | |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 270 | // Perform the default action (click) on multiple buttons. |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 271 | semantics_manager_.RequestAccessibilityAction( |
| 272 | button1->node_id(), fuchsia::accessibility::semantics::Action::DEFAULT); |
| 273 | semantics_manager_.RequestAccessibilityAction( |
| 274 | button2->node_id(), fuchsia::accessibility::semantics::Action::DEFAULT); |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 275 | semantics_manager_.RunUntilNumActionsHandledEquals(2); |
Sharon Yang | 5fb4ce2 | 2020-02-08 00:45:21 | [diff] [blame] | 276 | } |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 277 | |
| 278 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, PerformUnsupportedAction) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 279 | LoadPage(kPage1Path, kPage1Title); |
| 280 | |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 281 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
| 282 | |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 283 | fuchsia::accessibility::semantics::Node* button1 = |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 284 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1); |
| 285 | EXPECT_TRUE(button1); |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 286 | fuchsia::accessibility::semantics::Node* button2 = |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 287 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName2); |
| 288 | EXPECT_TRUE(button2); |
| 289 | |
| 290 | // Perform one supported action (DEFAULT) and one non-supported action |
| 291 | // (SET_VALUE); |
Sharon Yang | d0d86f4 | 2020-07-11 15:38:53 | [diff] [blame] | 292 | semantics_manager_.RequestAccessibilityAction( |
| 293 | button1->node_id(), fuchsia::accessibility::semantics::Action::DEFAULT); |
| 294 | semantics_manager_.RequestAccessibilityAction( |
| 295 | button2->node_id(), fuchsia::accessibility::semantics::Action::SET_VALUE); |
Sharon Yang | 1dd8b6db3 | 2020-04-10 23:10:18 | [diff] [blame] | 296 | semantics_manager_.RunUntilNumActionsHandledEquals(2); |
| 297 | |
| 298 | EXPECT_EQ(1, semantics_manager_.num_actions_handled()); |
| 299 | EXPECT_EQ(1, semantics_manager_.num_actions_unhandled()); |
| 300 | } |
Sharon Yang | 7919fab | 2020-07-28 16:46:20 | [diff] [blame] | 301 | |
| 302 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, Disconnect) { |
| 303 | base::RunLoop run_loop; |
| 304 | frame_ptr_.set_error_handler([&run_loop](zx_status_t status) { |
| 305 | EXPECT_EQ(ZX_ERR_INTERNAL, status); |
| 306 | run_loop.Quit(); |
| 307 | }); |
| 308 | |
| 309 | semantics_manager_.semantic_tree()->Disconnect(); |
| 310 | run_loop.Run(); |
| 311 | } |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 312 | |
Adam Ettenberger | 9c15a3a | 2020-09-01 22:10:27 | [diff] [blame] | 313 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, PerformScrollToMakeVisible) { |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 314 | constexpr int kScreenWidth = 720; |
| 315 | constexpr int kScreenHeight = 640; |
| 316 | gfx::Rect screen_bounds(kScreenWidth, kScreenHeight); |
| 317 | |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 318 | LoadPage(kPage1Path, kPage1Title); |
| 319 | |
Sharon Yang | 3231ad9 | 2020-08-12 02:22:08 | [diff] [blame] | 320 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
| 321 | |
| 322 | auto* content_view = |
| 323 | frame_impl_->web_contents_for_test()->GetContentNativeView(); |
| 324 | content_view->SetBounds(screen_bounds); |
| 325 | |
| 326 | // Get a node that is off the screen. |
| 327 | fuchsia::accessibility::semantics::Node* node = |
| 328 | semantics_manager_.semantic_tree()->GetNodeFromLabel(kOffscreenNodeName); |
| 329 | ASSERT_TRUE(node); |
| 330 | AccessibilityBridge* bridge = frame_impl_->accessibility_bridge_for_test(); |
| 331 | ui::AXNode* ax_node = bridge->ax_tree_for_test()->GetFromId(node->node_id()); |
| 332 | ASSERT_TRUE(ax_node); |
| 333 | bool is_offscreen = false; |
| 334 | bridge->ax_tree_for_test()->GetTreeBounds(ax_node, &is_offscreen); |
| 335 | EXPECT_TRUE(is_offscreen); |
| 336 | |
| 337 | // Perform SHOW_ON_SCREEN on that node and check that it is on the screen. |
| 338 | base::RunLoop run_loop; |
| 339 | bridge->set_event_received_callback_for_test(run_loop.QuitClosure()); |
| 340 | semantics_manager_.RequestAccessibilityAction( |
| 341 | node->node_id(), |
| 342 | fuchsia::accessibility::semantics::Action::SHOW_ON_SCREEN); |
| 343 | semantics_manager_.RunUntilNumActionsHandledEquals(1); |
| 344 | run_loop.Run(); |
| 345 | |
| 346 | // Initialize |is_offscreen| to false before calling GetTreeBounds as |
| 347 | // specified by the API. |
| 348 | is_offscreen = false; |
| 349 | bridge->ax_tree_for_test()->GetTreeBounds(ax_node, &is_offscreen); |
| 350 | |
| 351 | EXPECT_FALSE(is_offscreen); |
| 352 | } |
Sharon Yang | 2caf82e | 2020-10-12 18:00:35 | [diff] [blame] | 353 | |
| 354 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, Slider) { |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 355 | LoadPage(kPage1Path, kPage1Title); |
| 356 | |
Sharon Yang | 2caf82e | 2020-10-12 18:00:35 | [diff] [blame] | 357 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
| 358 | |
| 359 | fuchsia::accessibility::semantics::Node* node = |
| 360 | semantics_manager_.semantic_tree()->GetNodeFromRole( |
| 361 | fuchsia::accessibility::semantics::Role::SLIDER); |
| 362 | EXPECT_TRUE(node); |
| 363 | EXPECT_TRUE(node->has_states() && node->states().has_range_value()); |
| 364 | EXPECT_EQ(node->states().range_value(), kInitialRangeValue); |
| 365 | |
| 366 | AccessibilityBridge* bridge = frame_impl_->accessibility_bridge_for_test(); |
| 367 | base::RunLoop run_loop; |
| 368 | bridge->set_event_received_callback_for_test(run_loop.QuitClosure()); |
| 369 | semantics_manager_.RequestAccessibilityAction( |
| 370 | node->node_id(), fuchsia::accessibility::semantics::Action::INCREMENT); |
| 371 | semantics_manager_.RunUntilNumActionsHandledEquals(1); |
| 372 | run_loop.Run(); |
| 373 | |
| 374 | // Wait for the slider node to be updated, then check the value. |
| 375 | base::RunLoop run_loop2; |
| 376 | semantics_manager_.semantic_tree()->SetNodeUpdatedCallback( |
| 377 | node->node_id(), run_loop2.QuitClosure()); |
| 378 | run_loop2.Run(); |
| 379 | |
| 380 | node = semantics_manager_.semantic_tree()->GetNodeWithId(node->node_id()); |
| 381 | EXPECT_TRUE(node->has_states() && node->states().has_range_value()); |
| 382 | EXPECT_EQ(node->states().range_value(), kInitialRangeValue + kStepSize); |
| 383 | } |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 384 | |
| 385 | // This test makes sure that when semantic updates toggle on / off / on, the |
| 386 | // full semantic tree is sent in the first update when back on. |
| 387 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, TogglesSemanticsUpdates) { |
| 388 | LoadPage(kPage1Path, kPage1Title); |
| 389 | |
| 390 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 391 | EXPECT_EQ(semantics_manager_.semantic_tree()->num_commit_calls(), 1u); |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 392 | |
| 393 | semantics_manager_.SetSemanticsModeEnabled(false); |
| 394 | base::RunLoop().RunUntilIdle(); |
| 395 | |
| 396 | ASSERT_FALSE(frame_impl_->web_contents_for_test() |
| 397 | ->IsWebContentsOnlyAccessibilityModeForTesting()); |
| 398 | |
| 399 | // The tree gets cleared when semantic updates are off. |
| 400 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), 0u); |
| 401 | semantics_manager_.SetSemanticsModeEnabled(true); |
| 402 | base::RunLoop().RunUntilIdle(); |
| 403 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount); |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 404 | EXPECT_EQ(semantics_manager_.semantic_tree()->num_commit_calls(), 2u); |
Lucas Radaelli | 8c50054 | 2020-10-12 21:31:02 | [diff] [blame] | 405 | |
| 406 | ASSERT_TRUE(frame_impl_->web_contents_for_test() |
| 407 | ->IsWebContentsOnlyAccessibilityModeForTesting()); |
| 408 | } |
Lucas Radaelli | 0b226d23 | 2020-10-12 22:00:32 | [diff] [blame] | 409 | |
| 410 | // This test performs several tree modifications (insertions, changes, removals |
| 411 | // and reparentings). All operations must leave the tree in a valid state and |
| 412 | // also forward the nodes in a way that leaves the tree in the Fuchsia side in a |
| 413 | // valid state. Note that every time that a new tree is sent to Fuchsia, the |
| 414 | // FakeSemantiTree checks if the tree is valid. |
| 415 | IN_PROC_BROWSER_TEST_F(AccessibilityBridgeTest, TreeModificationsAreForwarded) { |
| 416 | AccessibilityBridge* bridge = frame_impl_->accessibility_bridge_for_test(); |
| 417 | size_t tree_size = 5; |
| 418 | |
| 419 | // The tree has the following form: (1 (2 (3 (4 (5))))) |
| 420 | auto tree_accessibility_event = CreateTreeAccessibilityEvent(tree_size); |
| 421 | bridge->AccessibilityEventReceived(tree_accessibility_event); |
| 422 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(tree_size); |
| 423 | CheckCallsToFakeSemanticTree(/*num_deletes=*/0, /*num_updates=*/1, |
| 424 | /*num_commits=*/1); |
| 425 | |
| 426 | // Adds a new node with ID 6. |
| 427 | // (1 (2 (3 (4 (5 6))))) |
| 428 | { |
| 429 | ui::AXTreeUpdate update; |
| 430 | update.root_id = 1; |
| 431 | update.nodes.resize(2); |
| 432 | update.nodes[0].id = 4; |
| 433 | update.nodes[0].child_ids.push_back(5); |
| 434 | update.nodes[0].child_ids.push_back(6); |
| 435 | update.nodes[1].id = 6; |
| 436 | |
| 437 | bridge->AccessibilityEventReceived( |
| 438 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 439 | semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(tree_size + 1); |
| 440 | CheckCallsToFakeSemanticTree(/*num_deletes=*/0, /*num_updates=*/2, |
| 441 | /*num_commits=*/2); |
| 442 | } |
| 443 | |
| 444 | // Removes the added node 6. |
| 445 | // (1 (2 (3 (4 (5))))) |
| 446 | { |
| 447 | ui::AXTreeUpdate update; |
| 448 | update.root_id = 1; |
| 449 | update.node_id_to_clear = 4; |
| 450 | update.nodes.resize(2); |
| 451 | update.nodes[0].id = 4; |
| 452 | update.nodes[0].child_ids.push_back(5); |
| 453 | |
| 454 | update.nodes[1].id = 5; |
| 455 | |
| 456 | bridge->AccessibilityEventReceived( |
| 457 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 458 | |
| 459 | semantics_manager_.semantic_tree()->RunUntilCommitCountIs(3); |
| 460 | CheckCallsToFakeSemanticTree(/*num_deletes=*/1, /*num_updates=*/3, |
| 461 | /*num_commits=*/3); |
| 462 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), tree_size); |
| 463 | } |
| 464 | |
| 465 | // Reparents node 5 to be a child of node 3. |
| 466 | // (1 (2 (3 (4 5)))) |
| 467 | { |
| 468 | ui::AXTreeUpdate update; |
| 469 | update.root_id = 1; |
| 470 | update.node_id_to_clear = 3; |
| 471 | update.nodes.resize(3); |
| 472 | update.nodes[0].id = 3; |
| 473 | update.nodes[0].child_ids.push_back(4); |
| 474 | update.nodes[0].child_ids.push_back(5); |
| 475 | |
| 476 | update.nodes[1].id = 4; |
| 477 | update.nodes[2].id = 5; |
| 478 | |
| 479 | bridge->AccessibilityEventReceived( |
| 480 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 481 | |
| 482 | semantics_manager_.semantic_tree()->RunUntilCommitCountIs(4); |
| 483 | CheckCallsToFakeSemanticTree(/*num_deletes=*/1, /*num_updates=*/4, |
| 484 | /*num_commits=*/4); |
| 485 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), tree_size); |
| 486 | } |
| 487 | |
| 488 | // Reparents the subtree rooted at node 3 to be a child of node 1. |
| 489 | // (1 (2 3 (4 5))) |
| 490 | { |
| 491 | ui::AXTreeUpdate update; |
| 492 | update.root_id = 1; |
| 493 | update.node_id_to_clear = 2; |
| 494 | update.nodes.resize(5); |
| 495 | update.nodes[0].id = 1; |
| 496 | update.nodes[0].child_ids.push_back(2); |
| 497 | update.nodes[0].child_ids.push_back(3); |
| 498 | |
| 499 | update.nodes[1].id = 2; |
| 500 | update.nodes[2].id = 3; |
| 501 | update.nodes[2].child_ids.push_back(4); |
| 502 | update.nodes[2].child_ids.push_back(5); |
| 503 | |
| 504 | update.nodes[3].id = 4; |
| 505 | update.nodes[4].id = 5; |
| 506 | |
| 507 | bridge->AccessibilityEventReceived( |
| 508 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 509 | |
| 510 | semantics_manager_.semantic_tree()->RunUntilCommitCountIs(5); |
| 511 | CheckCallsToFakeSemanticTree(/*num_deletes=*/1, /*num_updates=*/5, |
| 512 | /*num_commits=*/5); |
| 513 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), tree_size); |
| 514 | } |
| 515 | |
| 516 | // Deletes the subtree rooted at node 3. |
| 517 | // (1 (2)) |
| 518 | { |
| 519 | ui::AXTreeUpdate update; |
| 520 | update.root_id = 1; |
| 521 | update.node_id_to_clear = 1; |
| 522 | update.nodes.resize(2); |
| 523 | update.nodes[0].id = 1; |
| 524 | update.nodes[0].child_ids.push_back(2); |
| 525 | |
| 526 | update.nodes[1].id = 2; |
| 527 | |
| 528 | bridge->AccessibilityEventReceived( |
| 529 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 530 | |
| 531 | semantics_manager_.semantic_tree()->RunUntilCommitCountIs(6); |
| 532 | CheckCallsToFakeSemanticTree(/*num_deletes=*/2, /*num_updates=*/6, |
| 533 | /*num_commits=*/6); |
| 534 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), 2u); |
| 535 | } |
| 536 | |
| 537 | // Give this tree a new root. |
| 538 | // (7 (2)) |
| 539 | { |
| 540 | ui::AXTreeUpdate update; |
| 541 | update.root_id = 7; |
| 542 | update.node_id_to_clear = 1; |
| 543 | update.nodes.resize(2); |
| 544 | update.nodes[0].id = 7; |
| 545 | update.nodes[0].child_ids.push_back(2); |
| 546 | |
| 547 | update.nodes[1].id = 2; |
| 548 | |
| 549 | bridge->AccessibilityEventReceived( |
| 550 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 551 | |
| 552 | semantics_manager_.semantic_tree()->RunUntilCommitCountIs(7); |
| 553 | CheckCallsToFakeSemanticTree(/*num_deletes=*/3, /*num_updates=*/7, |
| 554 | /*num_commits=*/7); |
| 555 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), 2u); |
| 556 | } |
| 557 | |
| 558 | // Delete child and change root ID. |
| 559 | // (1) |
| 560 | { |
| 561 | ui::AXTreeUpdate update; |
| 562 | update.root_id = 1; |
| 563 | update.node_id_to_clear = 7; |
| 564 | update.nodes.resize(1); |
| 565 | update.nodes[0].id = 1; |
| 566 | |
| 567 | bridge->AccessibilityEventReceived( |
| 568 | CreateAccessibilityEventWithUpdate(std::move(update))); |
| 569 | |
| 570 | semantics_manager_.semantic_tree()->RunUntilCommitCountIs(8); |
| 571 | CheckCallsToFakeSemanticTree(/*num_deletes=*/4, /*num_updates=*/8, |
| 572 | /*num_commits=*/8); |
| 573 | EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), 1u); |
| 574 | } |
| 575 | } |