From 7a203330e1d3e2faf5f1e92e774de6effe21bfb9 Mon Sep 17 00:00:00 2001 From: Darren Chan <74682667+chandarrengoog@users.noreply.github.com> Date: Thu, 24 Jun 2021 14:48:10 -0400 Subject: [PATCH] Emit viewRefFocused events in the flutter runner. (flutter/engine#26791) Fulfill HostView.getCurrentFocusState and HostView.getNextFocusState platform message requests to deliver focus-related events to dart code. Consolidate focus functionality (including requestFocus handling) to a new "FocusDelegate" class. See https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/fidl/fuchsia.ui.views/view_ref_focused.fidl for details about focus state transitions and their meanings. See https://fxbug.dev/77481. --- .../ci/licenses_golden/licenses_flutter | 3 + .../shell/platform/fuchsia/flutter/BUILD.gn | 4 + .../shell/platform/fuchsia/flutter/engine.cc | 6 +- .../fuchsia/flutter/focus_delegate.cc | 94 ++++++++ .../platform/fuchsia/flutter/focus_delegate.h | 66 ++++++ .../flutter/focus_delegate_unittests.cc | 176 ++++++++++++++ .../platform/fuchsia/flutter/platform_view.cc | 79 ++----- .../platform/fuchsia/flutter/platform_view.h | 4 +- .../fuchsia/flutter/platform_view_unittest.cc | 218 ++++++++++-------- .../fuchsia/flutter/tests/fakes/BUILD.gn | 22 ++ .../fuchsia/flutter/tests/fakes/focuser.h | 41 ++++ .../flutter/tests/fakes/platform_message.h | 63 +++++ .../flutter/tests/fakes/view_ref_focused.h | 36 +++ 13 files changed, 650 insertions(+), 162 deletions(-) create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.cc create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.h create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate_unittests.cc create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/BUILD.gn create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/focuser.h create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/platform_message.h create mode 100644 engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/view_ref_focused.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 745b3568fb..d1f2718bc0 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1349,6 +1349,9 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/engine.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/flutter_runner_fakes.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h +FILE: ../../../flutter/shell/platform/fuchsia/flutter/focus_delegate.cc +FILE: ../../../flutter/shell/platform/fuchsia/flutter/focus_delegate.h +FILE: ../../../flutter/shell/platform/fuchsia/flutter/focus_delegate_unittests.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/fuchsia_external_view_embedder.cc FILE: ../../../flutter/shell/platform/fuchsia/flutter/fuchsia_external_view_embedder.h FILE: ../../../flutter/shell/platform/fuchsia/flutter/fuchsia_intl.cc diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn index 893ef9dd05..41960072af 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn @@ -59,6 +59,8 @@ template("runner_sources") { "engine.h", "flutter_runner_product_configuration.cc", "flutter_runner_product_configuration.h", + "focus_delegate.cc", + "focus_delegate.h", "fuchsia_external_view_embedder.cc", "fuchsia_external_view_embedder.h", "fuchsia_intl.cc", @@ -447,6 +449,7 @@ executable("flutter_runner_unittests") { "accessibility_bridge_unittest.cc", "component_unittest.cc", "flutter_runner_fakes.h", + "focus_delegate_unittests.cc", "fuchsia_intl_unittest.cc", "keyboard_unittest.cc", "platform_view_unittest.cc", @@ -471,6 +474,7 @@ executable("flutter_runner_unittests") { ] deps = [ + "tests/fakes", ":flutter_runner_fixtures", ":flutter_runner_sources", "//build/fuchsia/pkg:sys_cpp_testing", diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc index 0e4da2afbc..f161423211 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc @@ -21,6 +21,7 @@ #include "third_party/skia/include/ports/SkFontMgr_fuchsia.h" #include "../runtime/dart/utils/files.h" +#include "focus_delegate.h" #include "fuchsia_intl.h" #include "platform_view.h" #include "surface.h" @@ -99,6 +100,8 @@ Engine::Engine(Delegate& delegate, endpoints.set_session_listener(session_listener.Bind()); fidl::InterfaceHandle focuser; endpoints.set_view_focuser(focuser.NewRequest()); + fidl::InterfaceHandle view_ref_focused; + endpoints.set_view_ref_focused(view_ref_focused.NewRequest()); scenic->CreateSessionT(std::move(endpoints), [] {}); // Make clones of the `ViewRef` before sending it down to Scenic, since the @@ -275,6 +278,7 @@ Engine::Engine(Delegate& delegate, std::move(parent_environment_service_provider), session_listener_request = std::move(session_listener_request), focuser = std::move(focuser), + view_ref_focused = std::move(view_ref_focused), on_session_listener_error_callback = std::move(on_session_listener_error_callback), on_enable_wireframe_callback = @@ -339,7 +343,7 @@ Engine::Engine(Delegate& delegate, std::move(runner_services), std::move(parent_environment_service_provider), // services std::move(session_listener_request), // session listener - std::move(focuser), + std::move(view_ref_focused), std::move(focuser), // Server-side part of the fuchsia.ui.input3.KeyboardListener // connection. std::move(keyboard_listener_request), diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.cc new file mode 100644 index 0000000000..2a4851fb30 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.cc @@ -0,0 +1,94 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +#include "focus_delegate.h" + +namespace flutter_runner { + +void FocusDelegate::WatchLoop(std::function callback) { + if (watch_loop_) { + FML_LOG(ERROR) << "FocusDelegate::WatchLoop() cannot be called twice."; + return; + } + + watch_loop_ = [this, callback = std::move(callback)](auto focus_state) { + callback(is_focused_ = focus_state.focused()); + if (next_focus_request_) { + CompleteCurrentFocusState(std::exchange(next_focus_request_, nullptr)); + } + view_ref_focused_->Watch(watch_loop_); + }; + view_ref_focused_->Watch(watch_loop_); +} + +void FocusDelegate::CompleteCurrentFocusState( + fml::RefPtr response) { + std::string result(is_focused_ ? "[true]" : "[false]"); + response->Complete(std::make_unique( + std::vector(result.begin(), result.end()))); +} + +void FocusDelegate::CompleteNextFocusState( + fml::RefPtr response) { + if (next_focus_request_) { + FML_LOG(ERROR) << "An outstanding PlatformMessageResponse already exists " + "for the next focus state!"; + response->CompleteEmpty(); + } else { + next_focus_request_ = std::move(response); + } +} + +void FocusDelegate::RequestFocus( + rapidjson::Value request, + fml::RefPtr response) { + auto args_it = request.FindMember("args"); + if (args_it == request.MemberEnd() || !args_it->value.IsObject()) { + FML_LOG(ERROR) << "No arguments found."; + return; + } + const auto& args = args_it->value; + + auto view_ref = args.FindMember("viewRef"); + if (!view_ref->value.IsUint64()) { + FML_LOG(ERROR) << "Argument 'viewRef' is not a int64"; + return; + } + + zx_handle_t handle = view_ref->value.GetUint64(); + zx_handle_t out_handle; + zx_status_t status = + zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &out_handle); + if (status != ZX_OK) { + FML_LOG(ERROR) << "Argument 'viewRef' is not valid"; + return; + } + auto ref = fuchsia::ui::views::ViewRef({ + .reference = zx::eventpair(out_handle), + }); + focuser_->RequestFocus( + std::move(ref), + [view_ref = view_ref->value.GetUint64(), response = std::move(response)]( + fuchsia::ui::views::Focuser_RequestFocus_Result result) { + if (!response.get()) { + return; + } + int result_code = + result.is_err() + ? static_cast< + std::underlying_type_t>( + result.err()) + : 0; + + std::ostringstream out; + out << "[" << result_code << "]"; + std::string output = out.str(); + response->Complete(std::make_unique( + std::vector(output.begin(), output.end()))); + }); +} + +} // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.h b/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.h new file mode 100644 index 0000000000..523ba871c6 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate.h @@ -0,0 +1,66 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FOCUS_DELEGATE_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FOCUS_DELEGATE_H_ + +#include +#include + +#include "flutter/fml/macros.h" +#include "flutter/lib/ui/window/platform_message.h" +#include "third_party/rapidjson/include/rapidjson/document.h" + +namespace flutter_runner { + +class FocusDelegate { + public: + FocusDelegate(fidl::InterfaceHandle + view_ref_focused, + fidl::InterfaceHandle focuser) + : view_ref_focused_(view_ref_focused.Bind()), focuser_(focuser.Bind()) {} + + virtual ~FocusDelegate() = default; + + /// Continuously watches the host viewRef for focus events, invoking a + /// callback each time. + /// + /// This can only be called once. + virtual void WatchLoop(std::function callback); + + /// Completes the platform message request with the FocusDelegate's most + /// recent focus state. + virtual void CompleteCurrentFocusState( + fml::RefPtr response); + + /// Completes the platform message request with the FocusDelegate's next focus + /// state. + /// + /// Only one outstanding request may exist at a time. Any others will be + /// completed empty. + virtual void CompleteNextFocusState( + fml::RefPtr response); + + /// Completes a platform message request by attempting to give focus for a + /// given viewRef. + virtual void RequestFocus( + rapidjson::Value request, + fml::RefPtr response); + + private: + fuchsia::ui::views::ViewRefFocusedPtr view_ref_focused_; + fuchsia::ui::views::FocuserPtr focuser_; + + std::function watch_loop_; + bool is_focused_; + fml::RefPtr next_focus_request_; + + void Complete(fml::RefPtr response, + bool value); + + FML_DISALLOW_COPY_AND_ASSIGN(FocusDelegate); +}; + +} // namespace flutter_runner +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FOCUS_DELEGATE_H_ diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate_unittests.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate_unittests.cc new file mode 100644 index 0000000000..e5f7a74878 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/focus_delegate_unittests.cc @@ -0,0 +1,176 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include +#include +#include +#include +#include +#include + +#include "focus_delegate.h" +#include "tests/fakes/focuser.h" +#include "tests/fakes/platform_message.h" +#include "tests/fakes/view_ref_focused.h" +#include "third_party/rapidjson/include/rapidjson/document.h" + +rapidjson::Value ParsePlatformMessage(std::string json) { + rapidjson::Document document; + document.Parse(json); + if (document.HasParseError() || !document.IsObject()) { + FML_LOG(ERROR) << "Could not parse document"; + return rapidjson::Value(); + } + return document.GetObject(); +} + +namespace flutter_runner::testing { + +class FocusDelegateTest : public ::testing::Test { + public: + std::unique_ptr vrf; + std::unique_ptr focuser; + std::unique_ptr focus_delegate; + + void RunLoopUntilIdle() { loop_.RunUntilIdle(); } + + protected: + FocusDelegateTest() : loop_(&kAsyncLoopConfigAttachToCurrentThread) {} + + void SetUp() override { + vrf = std::make_unique(); + focuser = std::make_unique(); + focus_delegate = std::make_unique( + vrf_bindings.AddBinding(vrf.get()), + focuser_bindings.AddBinding(focuser.get())); + } + + void TearDown() override { + vrf_bindings.CloseAll(); + focuser_bindings.CloseAll(); + loop_.Quit(); + loop_.ResetQuit(); + } + + private: + async::Loop loop_; + fidl::BindingSet vrf_bindings; + fidl::BindingSet focuser_bindings; + + FML_DISALLOW_COPY_AND_ASSIGN(FocusDelegateTest); +}; + +// Tests that WatchLoop() should callback and complete PlatformMessageResponses +// correctly, given a series of vrf invocations. +TEST_F(FocusDelegateTest, WatchCallbackSeries) { + std::vector vrf_states{false, true, true, false, + true, false, true, true}; + std::size_t vrf_index = 0; + std::size_t callback_index = 0; + focus_delegate->WatchLoop([&](bool focus_state) { + // Make sure the focus state that FocusDelegate gives us is consistent with + // what was fired from the vrf. + EXPECT_EQ(vrf_states[callback_index], focus_state); + + // CompleteCurrentFocusState should complete with the current (up to date) + // focus state. + auto response = FakePlatformMessageResponse::Create(); + focus_delegate->CompleteCurrentFocusState(response); + response->ExpectCompleted(focus_state ? "[true]" : "[false]"); + + // Ensure this callback always happens in lockstep with + // vrf->ScheduleCallback. + EXPECT_EQ(vrf_index, callback_index++); + }); + + // Subsequent WatchLoop calls should not be respected. + focus_delegate->WatchLoop([](bool _) { + ADD_FAILURE() << "Subsequent WatchLoops should not be respected!"; + }); + + do { + // Ensure the next focus state is handled correctly. + auto response1 = FakePlatformMessageResponse::Create(); + focus_delegate->CompleteNextFocusState(response1); + + // Since there's already an outstanding PlatformMessageResponse, this one + // should be completed empty. + auto response = FakePlatformMessageResponse::Create(); + focus_delegate->CompleteNextFocusState(response); + response->ExpectCompleted(""); + + // Post watch events and trigger the next vrf event. + RunLoopUntilIdle(); + vrf->ScheduleCallback(vrf_states[vrf_index]); + RunLoopUntilIdle(); + + // Next focus state should be completed by now. + response1->ExpectCompleted(vrf_states[vrf_index] ? "[true]" : "[false]"); + + // Check CompleteCurrentFocusState again, and increment vrf_index, since we + // move on to the next focus state. + response = FakePlatformMessageResponse::Create(); + focus_delegate->CompleteCurrentFocusState(response); + response->ExpectCompleted(vrf_states[vrf_index++] ? "[true]" : "[false]"); + + // vrf->times_watched should always be 1 more than the amount of vrf events + // emitted. + EXPECT_EQ(vrf_index + 1, vrf->times_watched); + } while (vrf_index < vrf_states.size()); +} + +// Tests that RequestFocus() completes the platform message's response with a +// non-error status code. +TEST_F(FocusDelegateTest, RequestFocusTest) { + // This "Mock" ViewRef serves as the target for the RequestFocus operation. + auto mock_view_ref_pair = scenic::ViewRefPair::New(); + // Create the platform message request. + std::ostringstream message; + message << "{" + << " \"method\":\"View.requestFocus\"," + << " \"args\": {" + << " \"viewRef\":" + << mock_view_ref_pair.view_ref.reference.get() << " }" + << "}"; + + // Dispatch the plaform message request with an expected completion response. + auto response = FakePlatformMessageResponse::Create(); + focus_delegate->RequestFocus(ParsePlatformMessage(message.str()), response); + RunLoopUntilIdle(); + + response->ExpectCompleted("[0]"); + EXPECT_TRUE(focuser->request_focus_called()); +} + +// Tests that RequestFocus() completes the platform message's response with a +// Error::DENIED status code. +TEST_F(FocusDelegateTest, RequestFocusFailTest) { + // This "Mock" ViewRef serves as the target for the RequestFocus operation. + auto mock_view_ref_pair = scenic::ViewRefPair::New(); + // We're testing the focus failure case. + focuser->fail_request_focus(); + // Create the platform message request. + std::ostringstream message; + message << "{" + << " \"method\":\"View.requestFocus\"," + << " \"args\": {" + << " \"viewRef\":" + << mock_view_ref_pair.view_ref.reference.get() << " }" + << "}"; + + // Dispatch the plaform message request with an expected completion response. + auto response = FakePlatformMessageResponse::Create(); + focus_delegate->RequestFocus(ParsePlatformMessage(message.str()), response); + RunLoopUntilIdle(); + + response->ExpectCompleted( + "[" + + std::to_string( + static_cast>( + fuchsia::ui::views::Error::DENIED)) + + "]"); + EXPECT_TRUE(focuser->request_focus_called()); +} + +} // namespace flutter_runner::testing diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc index 925e082554..ec0e12c0f5 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc @@ -64,6 +64,7 @@ PlatformView::PlatformView( parent_environment_service_provider_handle, fidl::InterfaceRequest session_listener_request, + fidl::InterfaceHandle vrf, fidl::InterfaceHandle focuser, fidl::InterfaceRequest keyboard_listener_request, @@ -83,7 +84,8 @@ PlatformView::PlatformView( : flutter::PlatformView(delegate, std::move(task_runners)), debug_label_(std::move(debug_label)), view_ref_(std::move(view_ref)), - focuser_(focuser.Bind()), + focus_delegate_( + std::make_shared(std::move(vrf), std::move(focuser))), session_listener_binding_(this, std::move(session_listener_request)), session_listener_error_callback_( std::move(session_listener_error_callback)), @@ -119,6 +121,16 @@ PlatformView::PlatformView( fuchsia::ui::input::ImeService::Name_, text_sync_service_.NewRequest().TakeChannel()); + focus_delegate_->WatchLoop([&](bool focused) { + // Ensure last_text_state_ is set to make sure Flutter actually wants + // an IME. + if (focused && last_text_state_ != nullptr) { + ActivateIme(); + } else if (!focused) { + DeactivateIme(); + } + }); + // Finally! Register the native platform message handlers. RegisterPlatformMessageHandlers(); } @@ -341,10 +353,8 @@ void PlatformView::OnScenicEvent( break; case fuchsia::ui::scenic::Event::Tag::kInput: switch (event.input().Which()) { - case fuchsia::ui::input::InputEvent::Tag::kFocus: { - OnHandleFocusEvent(event.input().focus()); + case fuchsia::ui::input::InputEvent::Tag::kFocus: break; - } case fuchsia::ui::input::InputEvent::Tag::kPointer: { OnHandlePointerEvent(event.input().pointer()); break; @@ -662,19 +672,6 @@ void PlatformView::OnKeyEvent( callback(fuchsia::ui::input3::KeyEventStatus::HANDLED); } -bool PlatformView::OnHandleFocusEvent( - const fuchsia::ui::input::FocusEvent& focus) { - // Ensure last_text_state_ is set to make sure Flutter actually wants an IME. - if (focus.focused && last_text_state_ != nullptr) { - ActivateIme(); - return true; - } else if (!focus.focused) { - DeactivateIme(); - return true; - } - return false; -} - void PlatformView::ActivateIme() { DEBUG_CHECK(last_text_state_ != nullptr, LOG_TAG, ""); @@ -1089,50 +1086,12 @@ void PlatformView::HandleFlutterPlatformViewsChannelPlatformMessage( }); }; on_destroy_view_callback_(view_id_raw, std::move(on_view_unbound)); + } else if (method->value == "HostView.getCurrentFocusState") { + focus_delegate_->CompleteCurrentFocusState(message->response()); + } else if (method->value == "HostView.getNextFocusState") { + focus_delegate_->CompleteNextFocusState(message->response()); } else if (method->value == "View.requestFocus") { - auto args_it = root.FindMember("args"); - if (args_it == root.MemberEnd() || !args_it->value.IsObject()) { - FML_LOG(ERROR) << "No arguments found."; - return; - } - const auto& args = args_it->value; - - auto view_ref = args.FindMember("viewRef"); - if (!view_ref->value.IsUint64()) { - FML_LOG(ERROR) << "Argument 'viewRef' is not a int64"; - return; - } - - zx_handle_t handle = view_ref->value.GetUint64(); - zx_handle_t out_handle; - zx_status_t status = - zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &out_handle); - if (status != ZX_OK) { - FML_LOG(ERROR) << "Argument 'viewRef' is not valid"; - return; - } - auto ref = fuchsia::ui::views::ViewRef({ - .reference = zx::eventpair(out_handle), - }); - focuser_->RequestFocus( - std::move(ref), - [view_ref = view_ref->value.GetUint64(), message = std::move(message)]( - fuchsia::ui::views::Focuser_RequestFocus_Result result) { - if (message->response().get()) { - int result_code = - result.is_err() - ? static_cast< - std::underlying_type_t>( - result.err()) - : 0; - - std::ostringstream out; - out << "[" << result_code << "]"; - message->response()->Complete( - std::make_unique( - (const uint8_t*)out.str().c_str(), out.str().length())); - } - }); + focus_delegate_->RequestFocus(root, message->response()); } else { FML_DLOG(ERROR) << "Unknown " << message->channel() << " method " << method->value.GetString(); diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h index 2f3787f806..2e98e0d828 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h @@ -26,6 +26,7 @@ #include "flutter/shell/platform/fuchsia/flutter/fuchsia_external_view_embedder.h" #include "flutter/shell/platform/fuchsia/flutter/keyboard.h" #include "flutter/shell/platform/fuchsia/flutter/vsync_waiter.h" +#include "focus_delegate.h" namespace flutter_runner { @@ -72,6 +73,7 @@ class PlatformView final : public flutter::PlatformView, parent_environment_service_provider, fidl::InterfaceRequest session_listener_request, + fidl::InterfaceHandle vrf, fidl::InterfaceHandle focuser, fidl::InterfaceRequest keyboard_listener, @@ -183,7 +185,7 @@ class PlatformView final : public flutter::PlatformView, // TODO(MI4-2490): remove once ViewRefControl is passed to Scenic and kept // alive there const fuchsia::ui::views::ViewRef view_ref_; - fuchsia::ui::views::FocuserPtr focuser_; + std::shared_ptr focus_delegate_; // Logical size and logical->physical ratio. These are optional to provide // an "unset" state during program startup, before Scenic has sent any diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc index 8f4088073d..e0e8c37ebd 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/fuchsia/flutter/platform_view.h" - #include #include #include @@ -22,11 +20,14 @@ #include "flutter/flow/embedded_views.h" #include "flutter/lib/ui/window/platform_message.h" #include "flutter/lib/ui/window/viewport_metrics.h" +#include "flutter/shell/platform/fuchsia/flutter/platform_view.h" #include "gmock/gmock.h" #include "gtest/gtest.h" - #include "surface.h" #include "task_runner_adapter.h" +#include "tests/fakes/focuser.h" +#include "tests/fakes/platform_message.h" +#include "tests/fakes/view_ref_focused.h" namespace flutter_runner::testing { namespace { @@ -150,30 +151,6 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { bool semantics_enabled_ = false; }; -class MockFocuser : public fuchsia::ui::views::Focuser { - public: - MockFocuser(bool fail_request_focus = false) - : fail_request_focus_(fail_request_focus) {} - - bool request_focus_called() const { return request_focus_called_; } - - private: - void RequestFocus(fuchsia::ui::views::ViewRef view_ref, - RequestFocusCallback callback) override { - request_focus_called_ = true; - auto result = - fail_request_focus_ - ? fuchsia::ui::views::Focuser_RequestFocus_Result::WithErr( - fuchsia::ui::views::Error::DENIED) - : fuchsia::ui::views::Focuser_RequestFocus_Result::WithResponse( - fuchsia::ui::views::Focuser_RequestFocus_Response()); - callback(std::move(result)); - } - - bool request_focus_called_ = false; - bool fail_request_focus_ = false; -}; - class MockResponse : public flutter::PlatformMessageResponse { public: MOCK_METHOD1(Complete, void(std::unique_ptr data)); @@ -204,6 +181,12 @@ class PlatformViewBuilder { return *this; } + PlatformViewBuilder& SetViewRefFocused( + fidl::InterfaceHandle vrf) { + vrf_ = std::move(vrf); + return *this; + } + PlatformViewBuilder& SetFocuser( fidl::InterfaceHandle focuser) { focuser_ = std::move(focuser); @@ -260,14 +243,13 @@ class PlatformViewBuilder { // Once Build is called, the instance is no longer usable. PlatformView Build() { - EXPECT_EQ(false, built_) + EXPECT_FALSE(std::exchange(built_, true)) << "Build() was already called, this buider is good for one use only."; - built_ = true; return PlatformView( delegate_, debug_label_, std::move(view_ref_), task_runners_, runner_services_, std::move(parent_environment_service_provider_), - std::move(session_listener_request_), std::move(focuser_), - std::move(keyboard_listener_), + std::move(session_listener_request_), std::move(vrf_), + std::move(focuser_), std::move(keyboard_listener_), std::move(on_session_listener_error_callback_), std::move(wireframe_enabled_callback_), std::move(on_create_view_callback_), @@ -297,6 +279,7 @@ class PlatformViewBuilder { // Optional elements. fidl::InterfaceRequest session_listener_request_{nullptr}; + fidl::InterfaceHandle vrf_{nullptr}; fidl::InterfaceHandle focuser_{nullptr}; fidl::InterfaceRequest keyboard_listener_{nullptr}; @@ -967,6 +950,74 @@ TEST_F(PlatformViewTests, ViewEventsTest) { ToString(view_state_changed_msg->data())); } +// This test makes sure that the PlatformView forwards messages on the +// "flutter/platform_views" channel for GetCurrentFocusState and +// GetNextFocusState. +TEST_F(PlatformViewTests, GetFocusStatesTest) { + sys::testing::ServiceDirectoryProvider services_provider(dispatcher()); + MockPlatformViewDelegate delegate; + flutter::TaskRunners task_runners = + flutter::TaskRunners("test_runners", nullptr, nullptr, nullptr, nullptr); + + FakeViewRefFocused vrf; + fidl::BindingSet vrf_bindings; + auto vrf_handle = vrf_bindings.AddBinding(&vrf); + + flutter_runner::PlatformView platform_view = + PlatformViewBuilder(delegate, std::move(task_runners), + services_provider.service_directory()) + .SetViewRefFocused(std::move(vrf_handle)) + .Build(); + + // Cast platform_view to its base view so we can have access to the public + // "HandlePlatformMessage" function. + auto base_view = static_cast(&platform_view); + EXPECT_TRUE(base_view); + + std::vector vrf_states{false, true, true, false, + true, false, true, true}; + + for (std::size_t i = 0; i < vrf_states.size(); ++i) { + // HostView.getNextFocusState should complete with the next focus state. + auto response1 = FakePlatformMessageResponse::Create(); + base_view->HandlePlatformMessage( + response1->WithMessage("flutter/platform_views", + "{\"method\":\"HostView.getNextFocusState\"}")); + // Duplicate HostView.getNextFocusState requests should complete empty. + auto response2 = FakePlatformMessageResponse::Create(); + base_view->HandlePlatformMessage( + response2->WithMessage("flutter/platform_views", + "{\"method\":\"HostView.getNextFocusState\"}")); + + // Post watch events and make sure the hanging get is invoked each time. + RunLoopUntilIdle(); + EXPECT_EQ(vrf.times_watched, i + 1); + + // Dispatch the next vrf event. + vrf.ScheduleCallback(vrf_states[i]); + RunLoopUntilIdle(); + + // Make sure HostView.getCurrentFocusState completes with the current focus + // state. + auto response3 = FakePlatformMessageResponse::Create(); + base_view->HandlePlatformMessage(response3->WithMessage( + "flutter/platform_views", + "{\"method\":\"HostView.getCurrentFocusState\"}")); + // Duplicate HostView.getCurrentFocusState are allowed. + auto response4 = FakePlatformMessageResponse::Create(); + base_view->HandlePlatformMessage(response4->WithMessage( + "flutter/platform_views", + "{\"method\":\"HostView.getCurrentFocusState\"}")); + + // Run event loop and check our results. + RunLoopUntilIdle(); + response1->ExpectCompleted(vrf_states[i] ? "[true]" : "[false]"); + response2->ExpectCompleted(""); + response3->ExpectCompleted(vrf_states[i] ? "[true]" : "[false]"); + response4->ExpectCompleted(vrf_states[i] ? "[true]" : "[false]"); + } +} + // This test makes sure that the PlatformView forwards messages on the // "flutter/platform_views" channel for RequestFocus. TEST_F(PlatformViewTests, RequestFocusTest) { @@ -975,9 +1026,9 @@ TEST_F(PlatformViewTests, RequestFocusTest) { flutter::TaskRunners task_runners = flutter::TaskRunners("test_runners", nullptr, nullptr, nullptr, nullptr); - MockFocuser mock_focuser; + FakeFocuser focuser; fidl::BindingSet focuser_bindings; - auto focuser_handle = focuser_bindings.AddBinding(&mock_focuser); + auto focuser_handle = focuser_bindings.AddBinding(&focuser); flutter_runner::PlatformView platform_view = PlatformViewBuilder(delegate, std::move(task_runners), @@ -994,39 +1045,22 @@ TEST_F(PlatformViewTests, RequestFocusTest) { auto mock_view_ref_pair = scenic::ViewRefPair::New(); // JSON for the message to be passed into the PlatformView. - char buff[254]; - snprintf(buff, sizeof(buff), - "{" - " \"method\":\"View.requestFocus\"," - " \"args\": {" - " \"viewRef\":%u" - " }" - "}", - mock_view_ref_pair.view_ref.reference.get()); - - // Define a custom gmock matcher to capture the response to platform message. - struct DataArg { - void Complete(std::unique_ptr data) { - this->data = std::move(data); - } - std::unique_ptr data; - }; - DataArg data_arg; - fml::RefPtr response = fml::MakeRefCounted(); - EXPECT_CALL(*response, Complete(::testing::_)) - .WillOnce(::testing::Invoke(&data_arg, &DataArg::Complete)); - - std::unique_ptr message = - std::make_unique( - "flutter/platform_views", - fml::MallocMapping::Copy(buff, sizeof(buff)), response); - base_view->HandlePlatformMessage(std::move(message)); + std::ostringstream message; + message << "{" + << " \"method\":\"View.requestFocus\"," + << " \"args\": {" + << " \"viewRef\":" + << mock_view_ref_pair.view_ref.reference.get() << " }" + << "}"; + // Dispatch the plaform message request. + auto response = FakePlatformMessageResponse::Create(); + base_view->HandlePlatformMessage( + response->WithMessage("flutter/platform_views", message.str())); RunLoopUntilIdle(); - EXPECT_TRUE(mock_focuser.request_focus_called()); - auto result = ToString(*data_arg.data); - EXPECT_EQ(std::string("[0]"), result); + response->ExpectCompleted("[0]"); + EXPECT_TRUE(focuser.request_focus_called()); } // This test makes sure that the PlatformView correctly replies with an error @@ -1037,9 +1071,10 @@ TEST_F(PlatformViewTests, RequestFocusFailTest) { flutter::TaskRunners task_runners = flutter::TaskRunners("test_runners", nullptr, nullptr, nullptr, nullptr); - MockFocuser mock_focuser(true /*fail_request_focus*/); + FakeFocuser focuser; + focuser.fail_request_focus(); fidl::BindingSet focuser_bindings; - auto focuser_handle = focuser_bindings.AddBinding(&mock_focuser); + auto focuser_handle = focuser_bindings.AddBinding(&focuser); flutter_runner::PlatformView platform_view = PlatformViewBuilder(delegate, std::move(task_runners), @@ -1056,44 +1091,27 @@ TEST_F(PlatformViewTests, RequestFocusFailTest) { auto mock_view_ref_pair = scenic::ViewRefPair::New(); // JSON for the message to be passed into the PlatformView. - char buff[254]; - snprintf(buff, sizeof(buff), - "{" - " \"method\":\"View.requestFocus\"," - " \"args\": {" - " \"viewRef\":%u" - " }" - "}", - mock_view_ref_pair.view_ref.reference.get()); - - // Define a custom gmock matcher to capture the response to platform message. - struct DataArg { - void Complete(std::unique_ptr data) { - this->data = std::move(data); - } - std::unique_ptr data; - }; - DataArg data_arg; - fml::RefPtr response = fml::MakeRefCounted(); - EXPECT_CALL(*response, Complete(::testing::_)) - .WillOnce(::testing::Invoke(&data_arg, &DataArg::Complete)); - - std::unique_ptr message = - std::make_unique( - "flutter/platform_views", - fml::MallocMapping::Copy(buff, sizeof(buff)), response); - base_view->HandlePlatformMessage(std::move(message)); + std::ostringstream message; + message << "{" + << " \"method\":\"View.requestFocus\"," + << " \"args\": {" + << " \"viewRef\":" + << mock_view_ref_pair.view_ref.reference.get() << " }" + << "}"; + // Dispatch the plaform message request. + auto response = FakePlatformMessageResponse::Create(); + base_view->HandlePlatformMessage( + response->WithMessage("flutter/platform_views", message.str())); RunLoopUntilIdle(); - EXPECT_TRUE(mock_focuser.request_focus_called()); - auto result = ToString(*data_arg.data); - std::ostringstream out; - out << "[" - << static_cast>( - fuchsia::ui::views::Error::DENIED) - << "]"; - EXPECT_EQ(out.str(), result); + response->ExpectCompleted( + "[" + + std::to_string( + static_cast>( + fuchsia::ui::views::Error::DENIED)) + + "]"); + EXPECT_TRUE(focuser.request_focus_called()); } struct EventFlow { diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/BUILD.gn new file mode 100644 index 0000000000..11c88e3004 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright 2013 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +assert(is_fuchsia) + +source_set("fakes") { + testonly = true + + sources = [ + "focuser.h", + "platform_message.h", + "view_ref_focused.h", + ] + + deps = [ + "//build/fuchsia/pkg:sys_cpp_testing", + "//flutter/lib/ui", + "//flutter/testing", + "//third_party/rapidjson", + ] +} diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/focuser.h b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/focuser.h new file mode 100644 index 0000000000..275af5671f --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/focuser.h @@ -0,0 +1,41 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_FOCUSER_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_FOCUSER_H_ + +#include + +using Focuser = fuchsia::ui::views::Focuser; + +namespace flutter_runner::testing { + +class FakeFocuser : public Focuser { + public: + bool request_focus_called() const { return request_focus_called_; } + + void fail_request_focus(bool fail_request = true) { + fail_request_focus_ = fail_request; + } + + private: + void RequestFocus(fuchsia::ui::views::ViewRef view_ref, + RequestFocusCallback callback) override { + request_focus_called_ = true; + auto result = + fail_request_focus_ + ? fuchsia::ui::views::Focuser_RequestFocus_Result::WithErr( + fuchsia::ui::views::Error::DENIED) + : fuchsia::ui::views::Focuser_RequestFocus_Result::WithResponse( + fuchsia::ui::views::Focuser_RequestFocus_Response()); + callback(std::move(result)); + } + + bool request_focus_called_ = false; + bool fail_request_focus_ = false; +}; + +} // namespace flutter_runner::testing + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_FOCUSER_H_ diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/platform_message.h b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/platform_message.h new file mode 100644 index 0000000000..ef1a095528 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/platform_message.h @@ -0,0 +1,63 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_PLATFORM_MESSAGE_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_PLATFORM_MESSAGE_H_ + +#include +#include + +#include "flutter/lib/ui/window/platform_message.h" +#include "third_party/rapidjson/include/rapidjson/document.h" + +using PlatformMessageResponse = flutter::PlatformMessageResponse; +using PlatformMessage = flutter::PlatformMessage; + +namespace flutter_runner::testing { + +class FakePlatformMessageResponse : public PlatformMessageResponse { + public: + static fml::RefPtr Create() { + return fml::AdoptRef(new FakePlatformMessageResponse()); + } + + void ExpectCompleted(std::string expected) { + EXPECT_TRUE(is_complete_); + if (is_complete_) { + EXPECT_EQ(expected, response_); + } + } + + std::unique_ptr WithMessage(std::string channel, + std::string message) { + return std::make_unique( + channel, + fml::MallocMapping::Copy(message.c_str(), + message.c_str() + message.size()), + fml::RefPtr(this)); + } + + void Complete(std::unique_ptr data) override { + response_ = + std::string(data->GetMapping(), data->GetMapping() + data->GetSize()); + FinalizeComplete(); + }; + + void CompleteEmpty() override { FinalizeComplete(); }; + + private: + // Private constructors. + FakePlatformMessageResponse() {} + + void FinalizeComplete() { + EXPECT_FALSE(std::exchange(is_complete_, true)) + << "Platform message responses can only be completed once!"; + } + + std::string response_; +}; + +} // namespace flutter_runner::testing + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_PLATFORM_MESSAGE_H_ diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/view_ref_focused.h b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/view_ref_focused.h new file mode 100644 index 0000000000..84223a44e7 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/fakes/view_ref_focused.h @@ -0,0 +1,36 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_VIEW_REF_FOCUSED_H_ +#define FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_VIEW_REF_FOCUSED_H_ + +#include + +using ViewRefFocused = fuchsia::ui::views::ViewRefFocused; + +namespace flutter_runner::testing { + +class FakeViewRefFocused : public ViewRefFocused { + public: + using WatchCallback = ViewRefFocused::WatchCallback; + std::size_t times_watched = 0; + + void Watch(WatchCallback callback) override { + callback_ = std::move(callback); + ++times_watched; + } + + void ScheduleCallback(bool focused) { + fuchsia::ui::views::FocusState focus_state; + focus_state.set_focused(focused); + callback_(std::move(focus_state)); + } + + private: + WatchCallback callback_; +}; + +} // namespace flutter_runner::testing + +#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_TESTS_FAKES_VIEW_REF_FOCUSED_H_