fake_semantics_manager.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "fuchsia_web/webengine/browser/fake_semantics_manager.h"
  5. #include "base/check.h"
  6. #include "base/notreached.h"
  7. #include "base/run_loop.h"
  8. FakeSemanticsManager::FakeSemanticsManager() = default;
  9. FakeSemanticsManager::~FakeSemanticsManager() = default;
  10. void FakeSemanticsManager::SetSemanticsModeEnabled(bool is_enabled) {
  11. if (!is_enabled)
  12. semantic_tree_.Clear();
  13. listener_->OnSemanticsModeChanged(is_enabled, []() {});
  14. }
  15. void FakeSemanticsManager::WaitUntilViewRegistered() {
  16. base::RunLoop loop;
  17. on_view_registered_ = loop.QuitClosure();
  18. loop.Run();
  19. }
  20. uint32_t FakeSemanticsManager::HitTestAtPointSync(
  21. fuchsia::math::PointF target_point) {
  22. hit_test_result_.reset();
  23. base::RunLoop run_loop;
  24. listener_->HitTest(target_point,
  25. [quit = run_loop.QuitClosure(),
  26. this](fuchsia::accessibility::semantics::Hit hit) {
  27. if (hit.has_node_id()) {
  28. hit_test_result_ = hit.node_id();
  29. }
  30. quit.Run();
  31. });
  32. run_loop.Run();
  33. return hit_test_result_.value();
  34. }
  35. void FakeSemanticsManager::CheckNumActions() {
  36. if (num_actions_handled_ + num_actions_unhandled_ >= expected_num_actions_) {
  37. DCHECK(on_expected_num_actions_);
  38. on_expected_num_actions_.Run();
  39. }
  40. }
  41. bool FakeSemanticsManager::RequestAccessibilityActionSync(
  42. uint32_t node_id,
  43. fuchsia::accessibility::semantics::Action action) {
  44. base::RunLoop run_loop;
  45. bool action_handled = false;
  46. listener_->OnAccessibilityActionRequested(
  47. node_id, action, [&action_handled, &run_loop](bool handled) {
  48. action_handled = handled;
  49. run_loop.QuitClosure().Run();
  50. });
  51. run_loop.Run();
  52. return action_handled;
  53. }
  54. void FakeSemanticsManager::RequestAccessibilityAction(
  55. uint32_t node_id,
  56. fuchsia::accessibility::semantics::Action action) {
  57. listener_->OnAccessibilityActionRequested(node_id, action,
  58. [this](bool handled) {
  59. if (handled) {
  60. num_actions_handled_++;
  61. } else {
  62. num_actions_unhandled_++;
  63. }
  64. CheckNumActions();
  65. });
  66. }
  67. void FakeSemanticsManager::RunUntilNumActionsHandledEquals(
  68. int32_t num_actions) {
  69. DCHECK(!on_expected_num_actions_);
  70. if (num_actions_handled_ + num_actions_unhandled_ >= num_actions)
  71. return;
  72. expected_num_actions_ = num_actions;
  73. base::RunLoop run_loop;
  74. on_expected_num_actions_ = run_loop.QuitClosure();
  75. run_loop.Run();
  76. }
  77. void FakeSemanticsManager::RegisterViewForSemantics(
  78. fuchsia::ui::views::ViewRef view_ref,
  79. fidl::InterfaceHandle<fuchsia::accessibility::semantics::SemanticListener>
  80. listener,
  81. fidl::InterfaceRequest<fuchsia::accessibility::semantics::SemanticTree>
  82. semantic_tree_request) {
  83. view_ref_ = std::move(view_ref);
  84. listener_ = listener.Bind();
  85. semantic_tree_.Bind(std::move(semantic_tree_request));
  86. if (on_view_registered_) {
  87. std::move(on_view_registered_).Run();
  88. }
  89. }
  90. void FakeSemanticsManager::NotImplemented_(const std::string& name) {
  91. NOTIMPLEMENTED() << name;
  92. }