7#include <fuchsia/inspect/cpp/fidl.h>
8#include <fuchsia/logger/cpp/fidl.h>
9#include <fuchsia/sysmem/cpp/fidl.h>
10#include <fuchsia/sysmem2/cpp/fidl.h>
11#include <fuchsia/tracing/provider/cpp/fidl.h>
12#include <fuchsia/ui/app/cpp/fidl.h>
13#include <lib/async/cpp/task.h>
14#include <lib/sys/component/cpp/testing/realm_builder.h>
15#include <lib/sys/component/cpp/testing/realm_builder_types.h>
24using component_testing::ChildOptions;
25using component_testing::ChildRef;
26using component_testing::ParentRef;
27using component_testing::Protocol;
28using component_testing::RealmRoot;
29using component_testing::Route;
30using component_testing::Storage;
46 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
49void PortableUITest::SetUpRealmBase() {
50 FML_LOG(INFO) <<
"Setting up realm base";
56 fuchsia::component::decl::Environment flutter_runner_environment;
58 flutter_runner_environment.set_extends(
59 fuchsia::component::decl::EnvironmentExtends::REALM);
60 flutter_runner_environment.set_runners({});
61 auto environment_runners = flutter_runner_environment.mutable_runners();
64 fuchsia::component::decl::RunnerRegistration flutter_jit_runner_reg;
65 flutter_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
69 environment_runners->push_back(std::move(flutter_jit_runner_reg));
70 auto realm_decl = realm_builder_.GetRealmDecl();
71 if (!realm_decl.has_environments()) {
72 realm_decl.set_environments({});
74 auto realm_environments = realm_decl.mutable_environments();
75 realm_environments->push_back(std::move(flutter_runner_environment));
76 realm_builder_.ReplaceRealmDecl(std::move(realm_decl));
79 realm_builder_.AddChild(
kTestUIStack, GetTestUIStackUrl());
82 realm_builder_.AddRoute(Route{
83 .capabilities = {Protocol{fuchsia::logger::LogSink::Name_},
84 Protocol{fuchsia::inspect::InspectSink::Name_},
85 Protocol{fuchsia::sysmem::Allocator::Name_},
86 Protocol{fuchsia::sysmem2::Allocator::Name_},
87 Protocol{fuchsia::tracing::provider::Registry::Name_},
88 Protocol{fuchsia::ui::input::ImeService::Name_},
91 component_testing::Directory{
"config-data"}},
92 .source = ParentRef(),
96 realm_builder_.AddRoute(Route{
97 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
98 Protocol{fuchsia::ui::composition::Flatland::Name_},
99 Protocol{fuchsia::ui::test::input::Registry::Name_},
100 Protocol{fuchsia::ui::test::scene::Controller::Name_},
101 Protocol{fuchsia::ui::display::singleton::Info::Name_},
107 realm_builder_.AddRoute(Route{.capabilities = {Storage{
"tmp"}},
108 .source = ParentRef(),
112void PortableUITest::ProcessViewGeometryResponse(
113 fuchsia::ui::observation::geometry::WatchResponse response) {
115 if (!response.has_error()) {
116 std::vector<fuchsia::ui::observation::geometry::ViewTreeSnapshot>* updates =
117 response.mutable_updates();
118 if (updates && !updates->empty()) {
119 last_view_tree_snapshot_ = std::move(updates->back());
123 const auto&
error = response.error();
124 if (
error | fuchsia::ui::observation::geometry::Error::CHANNEL_OVERFLOW) {
125 FML_LOG(INFO) <<
"View Tree watcher channel overflowed";
127 fuchsia::ui::observation::geometry::Error::BUFFER_OVERFLOW) {
128 FML_LOG(INFO) <<
"View Tree watcher buffer overflowed";
130 fuchsia::ui::observation::geometry::Error::VIEWS_OVERFLOW) {
134 <<
"View Tree watcher attempted to report too many views";
139void PortableUITest::WatchViewGeometry() {
141 <<
"View Tree watcher must be registered before calling Watch()";
143 view_tree_watcher_->Watch([
this](
auto response) {
144 ProcessViewGeometryResponse(std::move(response));
150 return last_view_tree_snapshot_.has_value() &&
156 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
157 scene_provider_.set_error_handler([](
auto) {
158 FML_LOG(ERROR) <<
"Error from test scene provider: "
159 << &zx_status_get_string;
162 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
163 request.set_view_provider(
164 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
165 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
167 scene_provider_->AttachClientView(
168 std::move(request), [
this](
auto client_view_ref_koid) {
169 client_root_view_ref_koid_ = client_view_ref_koid;
172 FML_LOG(INFO) <<
"Waiting for client view ref koid";
173 RunLoopUntil([
this] {
return client_root_view_ref_koid_.has_value(); });
177 FML_LOG(INFO) <<
"Waiting for client view to connect";
181 FML_LOG(INFO) <<
"Client view has rendered";
188 RunLoopUntil([
this] {
189 if (!last_view_tree_snapshot_.has_value() ||
190 !last_view_tree_snapshot_->has_views()) {
194 if (!client_root_view_ref_koid_.has_value()) {
198 for (
const auto&
view : last_view_tree_snapshot_->views()) {
199 if (!
view.has_view_ref_koid() ||
200 view.view_ref_koid() != *client_root_view_ref_koid_) {
204 if (
view.children().empty()) {
211 return std::count_if(
212 last_view_tree_snapshot_->views().begin(),
213 last_view_tree_snapshot_->views().end(),
215 view.children().back()](
const auto& view_to_check) {
216 return view_to_check.has_view_ref_koid() &&
217 view_to_check.view_ref_koid() == view_to_find;
224 FML_LOG(INFO) <<
"Embedded view has rendered";
228 FML_LOG(INFO) <<
"Registering fake touch screen";
230 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
231 input_registry_.set_error_handler([](
auto) {
232 FML_LOG(ERROR) <<
"Error from input helper: " << &zx_status_get_string;
235 bool touchscreen_registered =
false;
236 fuchsia::ui::test::input::RegistryRegisterTouchScreenRequest request;
237 request.set_device(fake_touchscreen_.NewRequest());
238 input_registry_->RegisterTouchScreen(
240 [&touchscreen_registered]() { touchscreen_registered =
true; });
242 RunLoopUntil([&touchscreen_registered] {
return touchscreen_registered; });
243 FML_LOG(INFO) <<
"Touchscreen registered";
247 FML_LOG(INFO) <<
"Registering fake mouse";
249 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
250 input_registry_.set_error_handler([](
auto) {
251 FML_LOG(ERROR) <<
"Error from input helper: " << &zx_status_get_string;
254 bool mouse_registered =
false;
255 fuchsia::ui::test::input::RegistryRegisterMouseRequest request;
256 request.set_device(fake_mouse_.NewRequest());
257 input_registry_->RegisterMouse(
258 std::move(request), [&mouse_registered]() { mouse_registered =
true; });
260 RunLoopUntil([&mouse_registered] {
return mouse_registered; });
261 FML_LOG(INFO) <<
"Mouse registered";
265 FML_LOG(INFO) <<
"Registering fake keyboard";
267 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
268 input_registry_.set_error_handler([](
auto) {
269 FML_LOG(ERROR) <<
"Error from input helper: " << &zx_status_get_string;
272 bool keyboard_registered =
false;
273 fuchsia::ui::test::input::RegistryRegisterKeyboardRequest request;
274 request.set_device(fake_keyboard_.NewRequest());
275 input_registry_->RegisterKeyboard(
277 [&keyboard_registered]() { keyboard_registered =
true; });
279 RunLoopUntil([&keyboard_registered] {
return keyboard_registered; });
280 FML_LOG(INFO) <<
"Keyboard registered";
284 fuchsia::ui::test::input::TouchScreenSimulateTapRequest tap_request;
285 tap_request.mutable_tap_location()->x =
x;
286 tap_request.mutable_tap_location()->y =
y;
288 FML_LOG(INFO) <<
"Injecting tap at (" << tap_request.tap_location().x <<
", "
289 << tap_request.tap_location().y <<
")";
290 fake_touchscreen_->SimulateTap(std::move(tap_request), [
this]() {
291 ++touch_injection_request_count_;
292 FML_LOG(INFO) <<
"*** Tap injected, count: "
293 << touch_injection_request_count_;
298 std::vector<fuchsia::ui::test::input::MouseButton> pressed_buttons,
301 fuchsia::ui::test::input::MouseSimulateMouseEventRequest request;
302 request.set_pressed_buttons(std::move(pressed_buttons));
303 request.set_movement_x(movement_x);
304 request.set_movement_y(movement_y);
306 FML_LOG(INFO) <<
"Injecting mouse input";
308 fake_mouse_->SimulateMouseEvent(
309 std::move(request), [] {
FML_LOG(INFO) <<
"Mouse event injected"; });
313 std::vector<fuchsia::ui::test::input::MouseButton> pressed_buttons,
316 bool use_physical_units) {
317 FML_LOG(INFO) <<
"Requesting mouse scroll";
318 fuchsia::ui::test::input::MouseSimulateMouseEventRequest request;
319 request.set_pressed_buttons(std::move(pressed_buttons));
320 if (use_physical_units) {
321 request.set_scroll_h_physical_pixel(scroll_x);
322 request.set_scroll_v_physical_pixel(scroll_y);
324 request.set_scroll_h_detent(scroll_x);
325 request.set_scroll_v_detent(scroll_y);
328 fake_mouse_->SimulateMouseEvent(std::move(request), [] {
329 FML_LOG(INFO) <<
"Mouse scroll event injected";
334 FML_LOG(INFO) <<
"Sending text request";
337 fuchsia::ui::test::input::KeyboardSimulateUsAsciiTextEntryRequest request;
338 request.set_text(
text);
339 fake_keyboard_->SimulateUsAsciiTextEntry(std::move(request),
340 [&done]() { done =
true; });
342 RunLoopUntil([&] {
return done; });
343 FML_LOG(INFO) <<
"Text request sent";
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)
void LaunchClientWithEmbeddedView()
bool HasViewConnected(zx_koid_t view_ref_koid)
static constexpr auto kTestUIStackRef
void SimulateTextEntry(std::string text)
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)