accessibility_browsertest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // Copyright 2022 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. #include <fuchsia/accessibility/semantics/cpp/fidl.h>
  5. #include <lib/ui/scenic/cpp/view_ref_pair.h>
  6. #include <zircon/types.h>
  7. #include "base/command_line.h"
  8. #include "base/fuchsia/mem_buffer_util.h"
  9. #include "base/fuchsia/scoped_service_binding.h"
  10. #include "base/fuchsia/test_component_context_for_process.h"
  11. #include "base/strings/stringprintf.h"
  12. #include "base/test/bind.h"
  13. #include "content/public/test/browser_test.h"
  14. #include "fuchsia_web/common/test/frame_test_util.h"
  15. #include "fuchsia_web/common/test/test_navigation_listener.h"
  16. #include "fuchsia_web/webengine/browser/accessibility_bridge.h"
  17. #include "fuchsia_web/webengine/browser/context_impl.h"
  18. #include "fuchsia_web/webengine/browser/fake_semantics_manager.h"
  19. #include "fuchsia_web/webengine/browser/frame_impl.h"
  20. #include "fuchsia_web/webengine/test/frame_for_test.h"
  21. #include "fuchsia_web/webengine/test/test_data.h"
  22. #include "fuchsia_web/webengine/test/web_engine_browser_test.h"
  23. #include "net/test/embedded_test_server/embedded_test_server.h"
  24. #include "testing/gtest/include/gtest/gtest.h"
  25. #include "ui/accessibility/ax_action_data.h"
  26. #include "ui/accessibility/ax_tree_observer.h"
  27. #include "ui/accessibility/platform/fuchsia/ax_platform_node_fuchsia.h"
  28. #include "ui/gfx/switches.h"
  29. #include "ui/ozone/public/ozone_switches.h"
  30. namespace {
  31. const char kPage1Path[] = "/ax1.html";
  32. const char kPage2Path[] = "/batching.html";
  33. const char kPageIframePath[] = "/iframe.html";
  34. const char kPage1Title[] = "accessibility 1";
  35. const char kPage2Title[] = "lots of nodes!";
  36. const char kPageIframeTitle[] = "iframe title";
  37. const char kButtonName1[] = "a button";
  38. const char kButtonName2[] = "another button";
  39. const char kButtonName3[] = "button 3";
  40. const char kNodeName[] = "last node";
  41. const char kParagraphName[] = "a third paragraph";
  42. const char kOffscreenNodeName[] = "offscreen node";
  43. const size_t kPage1NodeCount = 29;
  44. const size_t kPage2NodeCount = 190;
  45. const size_t kInitialRangeValue = 51;
  46. const size_t kStepSize = 3;
  47. // Simulated screen bounds to use.
  48. constexpr gfx::Size kTestWindowSize = {720, 640};
  49. fuchsia::math::PointF GetCenterOfBox(fuchsia::ui::gfx::BoundingBox box) {
  50. fuchsia::math::PointF center;
  51. center.x = (box.min.x + box.max.x) / 2;
  52. center.y = (box.min.y + box.max.y) / 2;
  53. return center;
  54. }
  55. // Returns whether or not the given node supports the given action.
  56. bool HasAction(const fuchsia::accessibility::semantics::Node& node,
  57. fuchsia::accessibility::semantics::Action action) {
  58. for (const auto& node_action : node.actions()) {
  59. if (node_action == action)
  60. return true;
  61. }
  62. return false;
  63. }
  64. } // namespace
  65. class FuchsiaFrameAccessibilityTest : public WebEngineBrowserTest {
  66. public:
  67. FuchsiaFrameAccessibilityTest() {
  68. WebEngineBrowserTest::set_test_server_root(base::FilePath(kTestServerRoot));
  69. }
  70. ~FuchsiaFrameAccessibilityTest() override = default;
  71. FuchsiaFrameAccessibilityTest(const FuchsiaFrameAccessibilityTest&) = delete;
  72. FuchsiaFrameAccessibilityTest& operator=(
  73. const FuchsiaFrameAccessibilityTest&) = delete;
  74. void SetUp() override {
  75. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  76. command_line->AppendSwitchNative(switches::kOzonePlatform,
  77. switches::kHeadless);
  78. command_line->AppendSwitch(switches::kHeadless);
  79. WebEngineBrowserTest::SetUp();
  80. }
  81. void SetUpOnMainThread() override {
  82. test_context_.emplace(
  83. base::TestComponentContextForProcess::InitialState::kCloneAll);
  84. WebEngineBrowserTest::SetUpOnMainThread();
  85. // Remove the injected a11y manager from /svc; otherwise, we won't be able
  86. // to replace it with the fake owned by the test fixture.
  87. test_context_->additional_services()
  88. ->RemovePublicService<
  89. fuchsia::accessibility::semantics::SemanticsManager>();
  90. semantics_manager_binding_.emplace(test_context_->additional_services(),
  91. &semantics_manager_);
  92. frame_ = FrameForTest::Create(context(), {});
  93. base::RunLoop().RunUntilIdle();
  94. frame_impl_ = context_impl()->GetFrameImplForTest(&frame_.ptr());
  95. frame_impl_->set_window_size_for_test(kTestWindowSize);
  96. frame_impl_->set_use_v2_accessibility_bridge(true);
  97. frame_->EnableHeadlessRendering();
  98. semantics_manager_.WaitUntilViewRegistered();
  99. ASSERT_TRUE(semantics_manager_.is_view_registered());
  100. ASSERT_TRUE(semantics_manager_.is_listener_valid());
  101. ASSERT_TRUE(embedded_test_server()->Start());
  102. // Change the accessibility mode on the Fuchsia side and check that it is
  103. // propagated correctly.
  104. ASSERT_FALSE(frame_impl_->web_contents_for_test()
  105. ->IsFullAccessibilityModeForTesting());
  106. semantics_manager_.SetSemanticsModeEnabled(true);
  107. base::RunLoop().RunUntilIdle();
  108. ASSERT_TRUE(frame_impl_->web_contents_for_test()
  109. ->IsFullAccessibilityModeForTesting());
  110. }
  111. void LoadPage(base::StringPiece url, base::StringPiece page_title) {
  112. GURL page_url(embedded_test_server()->GetURL(std::string(url)));
  113. ASSERT_TRUE(LoadUrlAndExpectResponse(frame_.GetNavigationController(),
  114. fuchsia::web::LoadUrlParams(),
  115. page_url.spec()));
  116. frame_.navigation_listener().RunUntilUrlAndTitleEquals(page_url,
  117. page_title);
  118. }
  119. protected:
  120. // TODO(crbug.com/1038786): Maybe move to WebEngineBrowserTest.
  121. absl::optional<base::TestComponentContextForProcess> test_context_;
  122. FrameForTest frame_;
  123. FrameImpl* frame_impl_;
  124. FakeSemanticsManager semantics_manager_;
  125. // Binding to the fake semantics manager.
  126. // Optional so that it can be instantiated outside the constructor.
  127. absl::optional<base::ScopedServiceBinding<
  128. fuchsia::accessibility::semantics::SemanticsManager>>
  129. semantics_manager_binding_;
  130. };
  131. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, CorrectDataSent) {
  132. LoadPage(kPage1Path, kPage1Title);
  133. // Check that the data values are correct in the FakeSemanticTree.
  134. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  135. EXPECT_TRUE(
  136. semantics_manager_.semantic_tree()->GetNodeFromLabel(kPage1Title));
  137. EXPECT_TRUE(
  138. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1));
  139. EXPECT_TRUE(
  140. semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName));
  141. }
  142. // Batching is performed when the number of nodes to send or delete exceeds the
  143. // maximum, as set on the Fuchsia side. Check that all nodes are received by the
  144. // Semantic Tree when batching is performed.
  145. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, DataSentWithBatching) {
  146. LoadPage(kPage2Path, kPage2Title);
  147. // Run until we expect more than a batch's worth of nodes to be present.
  148. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage2NodeCount);
  149. EXPECT_TRUE(semantics_manager_.semantic_tree()->GetNodeFromLabel(kNodeName));
  150. // Checks if the actual batching happened.
  151. EXPECT_GE(semantics_manager_.semantic_tree()->num_update_calls(), 18u);
  152. // Checks if one or more commit calls were made to send the data.
  153. EXPECT_GE(semantics_manager_.semantic_tree()->num_commit_calls(), 1u);
  154. }
  155. // Check that semantics information is correctly sent when navigating from page
  156. // to page.
  157. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, NavigateFromPageToPage) {
  158. LoadPage(kPage1Path, kPage1Title);
  159. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  160. EXPECT_TRUE(
  161. semantics_manager_.semantic_tree()->GetNodeFromLabel(kPage1Title));
  162. EXPECT_TRUE(
  163. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1));
  164. EXPECT_TRUE(
  165. semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName));
  166. LoadPage(kPage2Path, kPage2Title);
  167. semantics_manager_.semantic_tree()->RunUntilNodeWithLabelIsInTree(
  168. kPage2Title);
  169. EXPECT_TRUE(
  170. semantics_manager_.semantic_tree()->GetNodeFromLabel(kPage2Title));
  171. EXPECT_TRUE(semantics_manager_.semantic_tree()->GetNodeFromLabel(kNodeName));
  172. // Check that data from the first page has been deleted successfully.
  173. EXPECT_FALSE(
  174. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1));
  175. EXPECT_FALSE(
  176. semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName));
  177. }
  178. // Checks that the correct node ID is returned when performing hit testing.
  179. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, HitTest) {
  180. LoadPage(kPage1Path, kPage1Title);
  181. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  182. fuchsia::accessibility::semantics::Node* target_node =
  183. semantics_manager_.semantic_tree()->GetNodeFromLabel(kParagraphName);
  184. EXPECT_TRUE(target_node);
  185. fuchsia::math::PointF target_point = GetCenterOfBox(target_node->location());
  186. float scale_factor = 20.f;
  187. // Make the bridge use scaling in hit test calculations.
  188. frame_impl_->OnPixelScaleUpdate(scale_factor);
  189. // Downscale the target point, since the hit test calculation will scale it
  190. // back up.
  191. target_point.x /= scale_factor;
  192. target_point.y /= scale_factor;
  193. uint32_t hit_node_id =
  194. semantics_manager_.HitTestAtPointSync(std::move(target_point));
  195. fuchsia::accessibility::semantics::Node* hit_node =
  196. semantics_manager_.semantic_tree()->GetNodeWithId(hit_node_id);
  197. EXPECT_EQ(hit_node->attributes().label(), kParagraphName);
  198. // Expect hit testing to return the root when the point given is out of
  199. // bounds or there is no semantic node at that position.
  200. target_point.x = -1;
  201. target_point.y = -1;
  202. EXPECT_EQ(0u, semantics_manager_.HitTestAtPointSync(std::move(target_point)));
  203. target_point.x = 1. / scale_factor;
  204. target_point.y = 1. / scale_factor;
  205. EXPECT_EQ(0u, semantics_manager_.HitTestAtPointSync(std::move(target_point)));
  206. }
  207. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, PerformDefaultAction) {
  208. LoadPage(kPage1Path, kPage1Title);
  209. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  210. fuchsia::accessibility::semantics::Node* button1 =
  211. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1);
  212. EXPECT_TRUE(button1);
  213. fuchsia::accessibility::semantics::Node* button2 =
  214. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName2);
  215. EXPECT_TRUE(button2);
  216. fuchsia::accessibility::semantics::Node* button3 =
  217. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName3);
  218. EXPECT_TRUE(button3);
  219. EXPECT_TRUE(
  220. HasAction(*button1, fuchsia::accessibility::semantics::Action::DEFAULT));
  221. EXPECT_TRUE(semantics_manager_.RequestAccessibilityActionSync(
  222. button1->node_id(), fuchsia::accessibility::semantics::Action::DEFAULT));
  223. }
  224. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest,
  225. PerformUnsupportedAction) {
  226. LoadPage(kPage1Path, kPage1Title);
  227. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  228. fuchsia::accessibility::semantics::Node* button1 =
  229. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1);
  230. EXPECT_TRUE(button1);
  231. fuchsia::accessibility::semantics::Node* button2 =
  232. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName2);
  233. EXPECT_TRUE(button2);
  234. // Attempt to perform unsupported action.
  235. EXPECT_FALSE(semantics_manager_.RequestAccessibilityActionSync(
  236. button2->node_id(),
  237. fuchsia::accessibility::semantics::Action::SECONDARY));
  238. }
  239. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, Disconnect) {
  240. base::RunLoop run_loop;
  241. frame_.ptr().set_error_handler([&run_loop](zx_status_t status) {
  242. EXPECT_EQ(ZX_ERR_INTERNAL, status);
  243. run_loop.Quit();
  244. });
  245. semantics_manager_.semantic_tree()->Disconnect();
  246. run_loop.Run();
  247. }
  248. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest,
  249. PerformScrollToMakeVisible) {
  250. // Set the screen height to be small so that we can detect if we've
  251. // scrolled past our target, even if the max scroll is bounded.
  252. constexpr int kScreenWidth = 720;
  253. constexpr int kScreenHeight = 20;
  254. gfx::Rect screen_bounds(kScreenWidth, kScreenHeight);
  255. LoadPage(kPage1Path, kPage1Title);
  256. auto* semantic_tree = semantics_manager_.semantic_tree();
  257. ASSERT_TRUE(semantic_tree);
  258. semantic_tree->RunUntilNodeCountAtLeast(kPage1NodeCount);
  259. auto* content_view =
  260. frame_impl_->web_contents_for_test()->GetContentNativeView();
  261. content_view->SetBounds(screen_bounds);
  262. // Get a node that is off the screen, and verify that it is off the screen.
  263. fuchsia::accessibility::semantics::Node* fuchsia_node =
  264. semantic_tree->GetNodeFromLabel(kOffscreenNodeName);
  265. ASSERT_TRUE(fuchsia_node);
  266. // Get the corresponding AXPlatformNode.
  267. auto* fuchsia_platform_node = static_cast<ui::AXPlatformNodeFuchsia*>(
  268. ui::AXPlatformNodeBase::GetFromUniqueId(fuchsia_node->node_id()));
  269. ASSERT_TRUE(fuchsia_platform_node);
  270. auto* delegate = fuchsia_platform_node->GetDelegate();
  271. ui::AXOffscreenResult offscreen_result;
  272. delegate->GetClippedScreenBoundsRect(&offscreen_result);
  273. EXPECT_EQ(offscreen_result, ui::AXOffscreenResult::kOffscreen);
  274. // Perform SHOW_ON_SCREEN on that node.
  275. EXPECT_TRUE(semantics_manager_.RequestAccessibilityActionSync(
  276. fuchsia_node->node_id(),
  277. fuchsia::accessibility::semantics::Action::SHOW_ON_SCREEN));
  278. semantic_tree->RunUntilConditionIsTrue(
  279. base::BindLambdaForTesting([semantic_tree]() {
  280. auto* root = semantic_tree->GetNodeWithId(0u);
  281. if (!root)
  282. return false;
  283. // Once the scroll action has been handled, the root should have a
  284. // non-zero y-scroll offset.
  285. return root->has_states() && root->states().has_viewport_offset() &&
  286. root->states().viewport_offset().y > 0;
  287. }));
  288. // Verify that the AXNode we tried to make visible is now onscreen.
  289. delegate->GetClippedScreenBoundsRect(&offscreen_result);
  290. EXPECT_EQ(offscreen_result, ui::AXOffscreenResult::kOnscreen);
  291. }
  292. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, Slider) {
  293. LoadPage(kPage1Path, kPage1Title);
  294. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  295. fuchsia::accessibility::semantics::Node* node =
  296. semantics_manager_.semantic_tree()->GetNodeFromRole(
  297. fuchsia::accessibility::semantics::Role::SLIDER);
  298. EXPECT_TRUE(node);
  299. EXPECT_TRUE(node->has_states() && node->states().has_range_value());
  300. EXPECT_EQ(node->states().range_value(), kInitialRangeValue);
  301. base::RunLoop run_loop;
  302. semantics_manager_.semantic_tree()->SetNodeUpdatedCallback(
  303. node->node_id(), run_loop.QuitClosure());
  304. semantics_manager_.RequestAccessibilityActionSync(
  305. node->node_id(), fuchsia::accessibility::semantics::Action::INCREMENT);
  306. run_loop.Run();
  307. node = semantics_manager_.semantic_tree()->GetNodeWithId(node->node_id());
  308. EXPECT_TRUE(node->has_states() && node->states().has_range_value());
  309. EXPECT_EQ(node->states().range_value(), kInitialRangeValue + kStepSize);
  310. }
  311. // This test makes sure that when semantic updates toggle on / off / on, the
  312. // full semantic tree is sent in the first update when back on.
  313. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, TogglesSemanticsUpdates) {
  314. LoadPage(kPage1Path, kPage1Title);
  315. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  316. semantics_manager_.SetSemanticsModeEnabled(false);
  317. base::RunLoop().RunUntilIdle();
  318. EXPECT_FALSE(frame_impl_->web_contents_for_test()
  319. ->IsFullAccessibilityModeForTesting());
  320. // The tree gets cleared when semantic updates are off.
  321. EXPECT_EQ(semantics_manager_.semantic_tree()->tree_size(), 0u);
  322. semantics_manager_.SetSemanticsModeEnabled(true);
  323. base::RunLoop().RunUntilIdle();
  324. EXPECT_TRUE(frame_impl_->web_contents_for_test()
  325. ->IsFullAccessibilityModeForTesting());
  326. }
  327. // This test performs several tree modifications (insertions, changes, and
  328. // removals). All operations must leave the tree in a valid state and
  329. // also forward the nodes in a way that leaves the tree in the Fuchsia side in a
  330. // valid state. Note that every time that a new tree is sent to Fuchsia, the
  331. // FakeSemantiTree checks if the tree is valid.
  332. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest,
  333. TreeModificationsAreForwarded) {
  334. LoadPage(kPage1Path, kPage1Title);
  335. auto* semantic_tree = semantics_manager_.semantic_tree();
  336. semantics_manager_.semantic_tree()->RunUntilNodeCountAtLeast(kPage1NodeCount);
  337. // Create a new HTML element.
  338. {
  339. const auto script = base::StringPrintf(
  340. "var p = document.createElement(\"p\"); var text = "
  341. "document.createTextNode(\"new_label\"); p.appendChild(text); "
  342. "document.body.appendChild(p);");
  343. frame_->ExecuteJavaScript(
  344. {"*"}, base::MemBufferFromString(script, "add node"),
  345. [](fuchsia::web::Frame_ExecuteJavaScript_Result result) {
  346. EXPECT_TRUE(result.is_response());
  347. });
  348. semantic_tree->RunUntilNodeWithLabelIsInTree("new_label");
  349. }
  350. // Remove an HTML element.
  351. {
  352. // Verify that slider is present initially.
  353. EXPECT_TRUE(semantic_tree->GetNodeFromRole(
  354. fuchsia::accessibility::semantics::Role::SLIDER));
  355. const auto script = base::StringPrintf(
  356. "var slider = document.getElementById(\"myRange\"); slider.remove();");
  357. frame_->ExecuteJavaScript(
  358. {"*"}, base::MemBufferFromString(script, "reparent nodes"),
  359. [](fuchsia::web::Frame_ExecuteJavaScript_Result result) {
  360. EXPECT_TRUE(result.is_response());
  361. });
  362. semantic_tree->RunUntilConditionIsTrue(
  363. base::BindLambdaForTesting([semantic_tree]() {
  364. return !semantic_tree->GetNodeFromRole(
  365. fuchsia::accessibility::semantics::Role::SLIDER);
  366. }));
  367. }
  368. }
  369. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, OutOfProcessIframe) {
  370. constexpr int64_t kBindingsId = 1234;
  371. // Start a different embedded test server, and load a page on it. The URL for
  372. // this page will have a different port and be considered out of process when
  373. // used as the src for an iframe.
  374. net::EmbeddedTestServer second_test_server;
  375. second_test_server.ServeFilesFromSourceDirectory(
  376. base::FilePath(kTestServerRoot));
  377. ASSERT_TRUE(second_test_server.Start());
  378. GURL out_of_process_url = second_test_server.GetURL(kPage1Path);
  379. // Before loading a page on the default embedded test server, set the iframe
  380. // src to be |out_of_process_url|.
  381. frame_->AddBeforeLoadJavaScript(
  382. kBindingsId, {"*"},
  383. base::MemBufferFromString(
  384. base::StringPrintf("iframeSrc = '%s'",
  385. out_of_process_url.spec().c_str()),
  386. "test"),
  387. [](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
  388. EXPECT_TRUE(result.is_response());
  389. });
  390. LoadPage(kPageIframePath, "iframe loaded");
  391. // Run until the title of the iframe page is in the semantic tree. Because
  392. // the iframe's semantic tree is only sent when it is connected to the parent
  393. // tree, it is guaranteed that both trees will be present.
  394. semantics_manager_.semantic_tree()->RunUntilNodeWithLabelIsInTree(
  395. kPage1Title);
  396. // Two frames should be present.
  397. int num_frames = CollectAllRenderFrameHosts(
  398. frame_impl_->web_contents_for_test()->GetPrimaryPage())
  399. .size();
  400. EXPECT_EQ(num_frames, 2);
  401. // Check that the iframe node has been loaded.
  402. EXPECT_TRUE(
  403. semantics_manager_.semantic_tree()->GetNodeFromLabel(kPageIframeTitle));
  404. // Data that is part of the iframe should be in the semantic tree.
  405. EXPECT_TRUE(
  406. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1));
  407. // Makes the iframe navigate to a different page.
  408. GURL out_of_process_url_2 = second_test_server.GetURL(kPage2Path);
  409. const auto script =
  410. base::StringPrintf("document.getElementById(\"iframeId\").src = '%s'",
  411. out_of_process_url_2.spec().c_str());
  412. frame_->ExecuteJavaScript(
  413. {"*"}, base::MemBufferFromString(script, "test2"),
  414. [](fuchsia::web::Frame_ExecuteJavaScript_Result result) {
  415. EXPECT_TRUE(result.is_response());
  416. });
  417. semantics_manager_.semantic_tree()->RunUntilNodeWithLabelIsInTree(
  418. kPage2Title);
  419. // check that the iframe navigated to a different page.
  420. EXPECT_TRUE(semantics_manager_.semantic_tree()->GetNodeFromLabel(kNodeName));
  421. // Old iframe data should be gone.
  422. EXPECT_FALSE(
  423. semantics_manager_.semantic_tree()->GetNodeFromLabel(kButtonName1));
  424. // Makes the main page navigate to a different page, causing the iframe to go
  425. // away.
  426. LoadPage(kPage2Path, kPage2Title);
  427. // Wait for the root to be updated, which means that we navigated to a new
  428. // page.
  429. base::RunLoop run_loop;
  430. semantics_manager_.semantic_tree()->SetNodeUpdatedCallback(
  431. 0u, run_loop.QuitClosure());
  432. run_loop.Run();
  433. // We've navigated to a different page that has no iframes. Only one frame
  434. // should be present.
  435. num_frames = CollectAllRenderFrameHosts(
  436. frame_impl_->web_contents_for_test()->GetPrimaryPage())
  437. .size();
  438. EXPECT_EQ(num_frames, 1);
  439. }
  440. IN_PROC_BROWSER_TEST_F(FuchsiaFrameAccessibilityTest, UpdatesFocusInformation) {
  441. LoadPage(kPage1Path, kPage1Title);
  442. auto* semantic_tree = semantics_manager_.semantic_tree();
  443. semantic_tree->RunUntilNodeCountAtLeast(kPage1NodeCount);
  444. // Get a node that is off the screen, and verify that it is off the screen.
  445. fuchsia::accessibility::semantics::Node* fuchsia_node =
  446. semantic_tree->GetNodeFromLabel(kButtonName1);
  447. ASSERT_TRUE(fuchsia_node);
  448. EXPECT_FALSE(fuchsia_node->states().has_input_focus());
  449. // Get the corresponding AXPlatformNode.
  450. auto* fuchsia_platform_node = static_cast<ui::AXPlatformNodeFuchsia*>(
  451. ui::AXPlatformNodeBase::GetFromUniqueId(fuchsia_node->node_id()));
  452. ASSERT_TRUE(fuchsia_platform_node);
  453. // Focus the node.
  454. ui::AXActionData action_data;
  455. action_data.action = ax::mojom::Action::kFocus;
  456. fuchsia_platform_node->PerformAction(action_data);
  457. semantic_tree->RunUntilConditionIsTrue(
  458. base::BindLambdaForTesting([semantic_tree, fuchsia_node]() {
  459. auto* node = semantic_tree->GetNodeWithId(fuchsia_node->node_id());
  460. if (!node)
  461. return false;
  462. return node->has_states() && node->states().has_has_input_focus() &&
  463. node->states().has_input_focus();
  464. }));
  465. // Changes the focus to a different node and checks that the old value is
  466. // cleared.
  467. fuchsia::accessibility::semantics::Node* new_focus_node =
  468. semantic_tree->GetNodeFromLabel(kButtonName2);
  469. ASSERT_TRUE(new_focus_node);
  470. // Get the corresponding AXPlatformNode.
  471. auto* new_focus_platform_node = static_cast<ui::AXPlatformNodeFuchsia*>(
  472. ui::AXPlatformNodeBase::GetFromUniqueId(new_focus_node->node_id()));
  473. ASSERT_TRUE(new_focus_platform_node);
  474. // Focus the new node. We can reuse the original action data.
  475. new_focus_platform_node->PerformAction(action_data);
  476. semantic_tree->RunUntilConditionIsTrue(base::BindLambdaForTesting(
  477. [semantic_tree, new_focus_id = new_focus_node->node_id(),
  478. old_focus_id = fuchsia_node->node_id()]() {
  479. auto* old_focus = semantic_tree->GetNodeWithId(old_focus_id);
  480. auto* node = semantic_tree->GetNodeWithId(new_focus_id);
  481. if (!node || !old_focus)
  482. return false;
  483. // Node has the focus, root does not.
  484. return (node->has_states() && node->states().has_has_input_focus() &&
  485. node->states().has_input_focus()) &&
  486. (old_focus->has_states() &&
  487. old_focus->states().has_has_input_focus() &&
  488. !old_focus->states().has_input_focus());
  489. }));
  490. }