7#include <fuchsia/inspect/cpp/fidl.h>
8#include <fuchsia/logger/cpp/fidl.h>
9#include <fuchsia/tracing/provider/cpp/fidl.h>
10#include <fuchsia/ui/app/cpp/fidl.h>
11#include <lib/async/cpp/task.h>
12#include <lib/sys/component/cpp/testing/realm_builder.h>
13#include <lib/sys/component/cpp/testing/realm_builder_types.h>
16#include "flutter/fml/logging.h"
22using component_testing::ChildOptions;
23using component_testing::ChildRef;
24using component_testing::ParentRef;
25using component_testing::Protocol;
26using component_testing::RealmRoot;
27using component_testing::Route;
43 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
46void PortableUITest::SetUpRealmBase() {
47 FML_LOG(INFO) <<
"Setting up realm base";
53 fuchsia::component::decl::Environment flutter_runner_environment;
55 flutter_runner_environment.set_extends(
56 fuchsia::component::decl::EnvironmentExtends::REALM);
57 flutter_runner_environment.set_runners({});
58 auto environment_runners = flutter_runner_environment.mutable_runners();
61 fuchsia::component::decl::RunnerRegistration flutter_jit_runner_reg;
62 flutter_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
66 environment_runners->push_back(std::move(flutter_jit_runner_reg));
67 auto realm_decl = realm_builder_.GetRealmDecl();
68 if (!realm_decl.has_environments()) {
69 realm_decl.set_environments({});
71 auto realm_environments = realm_decl.mutable_environments();
72 realm_environments->push_back(std::move(flutter_runner_environment));
73 realm_builder_.ReplaceRealmDecl(std::move(realm_decl));
79 realm_builder_.AddRoute(Route{
80 .capabilities = {Protocol{fuchsia::logger::LogSink::Name_},
81 Protocol{fuchsia::inspect::InspectSink::Name_},
82 Protocol{fuchsia::sysmem::Allocator::Name_},
83 Protocol{fuchsia::tracing::provider::Registry::Name_},
84 Protocol{fuchsia::ui::input::ImeService::Name_},
87 component_testing::Directory{
"config-data"}},
88 .source = ParentRef(),
92 realm_builder_.AddRoute(Route{
93 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
94 Protocol{fuchsia::ui::composition::Flatland::Name_},
95 Protocol{fuchsia::ui::test::input::Registry::Name_},
96 Protocol{fuchsia::ui::test::scene::Controller::Name_},
97 Protocol{fuchsia::ui::display::singleton::Info::Name_},
103void PortableUITest::ProcessViewGeometryResponse(
104 fuchsia::ui::observation::geometry::WatchResponse response) {
106 if (!response.has_error()) {
107 std::vector<fuchsia::ui::observation::geometry::ViewTreeSnapshot>* updates =
108 response.mutable_updates();
109 if (updates && !updates->empty()) {
110 last_view_tree_snapshot_ = std::move(updates->back());
114 const auto&
error = response.error();
115 if (
error | fuchsia::ui::observation::geometry::Error::CHANNEL_OVERFLOW) {
116 FML_LOG(INFO) <<
"View Tree watcher channel overflowed";
118 fuchsia::ui::observation::geometry::Error::BUFFER_OVERFLOW) {
119 FML_LOG(INFO) <<
"View Tree watcher buffer overflowed";
121 fuchsia::ui::observation::geometry::Error::VIEWS_OVERFLOW) {
125 <<
"View Tree watcher attempted to report too many views";
130void PortableUITest::WatchViewGeometry() {
132 <<
"View Tree watcher must be registered before calling Watch()";
134 view_tree_watcher_->Watch([
this](
auto response) {
135 ProcessViewGeometryResponse(std::move(response));
141 return last_view_tree_snapshot_.has_value() &&
147 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
148 scene_provider_.set_error_handler([](
auto) {
150 << &zx_status_get_string;
153 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
154 request.set_view_provider(
155 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
156 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
158 scene_provider_->AttachClientView(
159 std::move(request), [
this](
auto client_view_ref_koid) {
160 client_root_view_ref_koid_ = client_view_ref_koid;
163 FML_LOG(INFO) <<
"Waiting for client view ref koid";
164 RunLoopUntil([
this] {
return client_root_view_ref_koid_.has_value(); });
168 FML_LOG(INFO) <<
"Waiting for client view to connect";
172 FML_LOG(INFO) <<
"Client view has rendered";
179 RunLoopUntil([
this] {
180 if (!last_view_tree_snapshot_.has_value() ||
181 !last_view_tree_snapshot_->has_views()) {
185 if (!client_root_view_ref_koid_.has_value()) {
189 for (
const auto& view : last_view_tree_snapshot_->views()) {
190 if (!view.has_view_ref_koid() ||
191 view.view_ref_koid() != *client_root_view_ref_koid_) {
195 if (view.children().empty()) {
202 return std::count_if(
203 last_view_tree_snapshot_->views().begin(),
204 last_view_tree_snapshot_->views().end(),
206 view.children().back()](
const auto& view_to_check) {
207 return view_to_check.has_view_ref_koid() &&
208 view_to_check.view_ref_koid() == view_to_find;
215 FML_LOG(INFO) <<
"Embedded view has rendered";
219 FML_LOG(INFO) <<
"Registering fake touch screen";
221 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
222 input_registry_.set_error_handler([](
auto) {
223 FML_LOG(
ERROR) <<
"Error from input helper: " << &zx_status_get_string;
226 bool touchscreen_registered =
false;
227 fuchsia::ui::test::input::RegistryRegisterTouchScreenRequest request;
228 request.set_device(fake_touchscreen_.NewRequest());
229 input_registry_->RegisterTouchScreen(
231 [&touchscreen_registered]() { touchscreen_registered =
true; });
233 RunLoopUntil([&touchscreen_registered] {
return touchscreen_registered; });
234 FML_LOG(INFO) <<
"Touchscreen registered";
238 FML_LOG(INFO) <<
"Registering fake mouse";
240 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
241 input_registry_.set_error_handler([](
auto) {
242 FML_LOG(
ERROR) <<
"Error from input helper: " << &zx_status_get_string;
245 bool mouse_registered =
false;
246 fuchsia::ui::test::input::RegistryRegisterMouseRequest request;
247 request.set_device(fake_mouse_.NewRequest());
248 input_registry_->RegisterMouse(
249 std::move(request), [&mouse_registered]() { mouse_registered =
true; });
251 RunLoopUntil([&mouse_registered] {
return mouse_registered; });
252 FML_LOG(INFO) <<
"Mouse registered";
256 FML_LOG(INFO) <<
"Registering fake keyboard";
258 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
259 input_registry_.set_error_handler([](
auto) {
260 FML_LOG(
ERROR) <<
"Error from input helper: " << &zx_status_get_string;
263 bool keyboard_registered =
false;
264 fuchsia::ui::test::input::RegistryRegisterKeyboardRequest request;
265 request.set_device(fake_keyboard_.NewRequest());
266 input_registry_->RegisterKeyboard(
268 [&keyboard_registered]() { keyboard_registered =
true; });
270 RunLoopUntil([&keyboard_registered] {
return keyboard_registered; });
271 FML_LOG(INFO) <<
"Keyboard registered";
275 fuchsia::ui::test::input::TouchScreenSimulateTapRequest tap_request;
276 tap_request.mutable_tap_location()->x =
x;
277 tap_request.mutable_tap_location()->y =
y;
279 FML_LOG(INFO) <<
"Injecting tap at (" << tap_request.tap_location().x <<
", "
280 << tap_request.tap_location().y <<
")";
281 fake_touchscreen_->SimulateTap(std::move(tap_request), [
this]() {
282 ++touch_injection_request_count_;
283 FML_LOG(INFO) <<
"*** Tap injected, count: "
284 << touch_injection_request_count_;
289 std::vector<fuchsia::ui::test::input::MouseButton> pressed_buttons,
292 fuchsia::ui::test::input::MouseSimulateMouseEventRequest request;
293 request.set_pressed_buttons(std::move(pressed_buttons));
294 request.set_movement_x(movement_x);
295 request.set_movement_y(movement_y);
297 FML_LOG(INFO) <<
"Injecting mouse input";
299 fake_mouse_->SimulateMouseEvent(
300 std::move(request), [] {
FML_LOG(INFO) <<
"Mouse event injected"; });
304 std::vector<fuchsia::ui::test::input::MouseButton> pressed_buttons,
307 bool use_physical_units) {
308 FML_LOG(INFO) <<
"Requesting mouse scroll";
309 fuchsia::ui::test::input::MouseSimulateMouseEventRequest request;
310 request.set_pressed_buttons(std::move(pressed_buttons));
311 if (use_physical_units) {
312 request.set_scroll_h_physical_pixel(scroll_x);
313 request.set_scroll_v_physical_pixel(scroll_y);
315 request.set_scroll_h_detent(scroll_x);
316 request.set_scroll_v_detent(scroll_y);
319 fake_mouse_->SimulateMouseEvent(std::move(request), [] {
320 FML_LOG(INFO) <<
"Mouse scroll event injected";
325 FML_LOG(INFO) <<
"Sending text request";
328 fuchsia::ui::test::input::KeyboardSimulateUsAsciiTextEntryRequest request;
329 request.set_text(
text);
330 fake_keyboard_->SimulateUsAsciiTextEntry(std::move(request),
333 RunLoopUntil([&] {
return done; });
334 FML_LOG(INFO) <<
"Text request sent";
static void done(const char *config, const char *src, const char *srcOptions, const char *name)
static constexpr auto kTestUIStack
void RegisterTouchScreen()
static constexpr auto kFlutterRunnerEnvironment
void SimulateMouseEvent(std::vector< fuchsia::ui::test::input::MouseButton > pressed_buttons, int movement_x, int movement_y)
void SimulateMouseScroll(std::vector< fuchsia::ui::test::input::MouseButton > pressed_buttons, int scroll_x, int scroll_y, bool use_physical_units=false)
static constexpr auto kFlutterJitRunnerRef
static constexpr auto kPointerInjectorRegistryName
void InjectTap(int32_t x, int32_t y)
virtual std::string GetTestUIStackUrl()=0
void LaunchClientWithEmbeddedView()
bool HasViewConnected(zx_koid_t view_ref_koid)
static constexpr auto kTestUIStackRef
void SimulateTextEntry(std::string text)
virtual void ExtendRealm()=0
static constexpr auto kFlutterJitRunner
static constexpr auto kFlutterJitRunnerUrl
static constexpr auto kVulkanLoaderServiceName
static constexpr auto kPosixSocketProviderName
void SetUp(bool build_realm=true)
const uint8_t uint32_t uint32_t GError ** error
#define FML_LOG(severity)
#define FML_CHECK(condition)
bool CheckViewExistsInSnapshot(const fuchsia::ui::observation::geometry::ViewTreeSnapshot &snapshot, zx_koid_t view_ref_koid)