Flutter Engine
 
Loading...
Searching...
No Matches
portable_ui_test.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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
5#include "portable_ui_test.h"
6
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>
16
17#include "check_view.h"
18#include "flutter/fml/logging.h"
19
20namespace fuchsia_test_utils {
21namespace {
22
23// Types imported for the realm_builder library.
24using component_testing::ChildOptions;
25using component_testing::ChildRef;
26using component_testing::ParentRef;
27using component_testing::Protocol;
28using component_testing::RealmRoot;
29using component_testing::Route;
30
32
33} // namespace
34
35void PortableUITest::SetUp(bool build_realm) {
36 SetUpRealmBase();
37 ExtendRealm();
38
39 if (build_realm) {
40 BuildRealm();
41 }
42}
43
45 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
46}
47
48void PortableUITest::SetUpRealmBase() {
49 FML_LOG(INFO) << "Setting up realm base";
50
51 // Add Flutter JIT runner as a child of the RealmBuilder
52 realm_builder_.AddChild(kFlutterJitRunner, kFlutterJitRunnerUrl);
53
54 // Add environment providing the Flutter JIT runner
55 fuchsia::component::decl::Environment flutter_runner_environment;
56 flutter_runner_environment.set_name(kFlutterRunnerEnvironment);
57 flutter_runner_environment.set_extends(
58 fuchsia::component::decl::EnvironmentExtends::REALM);
59 flutter_runner_environment.set_runners({});
60 auto environment_runners = flutter_runner_environment.mutable_runners();
61
62 // Add Flutter JIT runner to the environment
63 fuchsia::component::decl::RunnerRegistration flutter_jit_runner_reg;
64 flutter_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
65 fuchsia::component::decl::ChildRef{.name = kFlutterJitRunner}));
66 flutter_jit_runner_reg.set_source_name(kFlutterJitRunner);
67 flutter_jit_runner_reg.set_target_name(kFlutterJitRunner);
68 environment_runners->push_back(std::move(flutter_jit_runner_reg));
69 auto realm_decl = realm_builder_.GetRealmDecl();
70 if (!realm_decl.has_environments()) {
71 realm_decl.set_environments({});
72 }
73 auto realm_environments = realm_decl.mutable_environments();
74 realm_environments->push_back(std::move(flutter_runner_environment));
75 realm_builder_.ReplaceRealmDecl(std::move(realm_decl));
76
77 // Add test UI stack component.
78 realm_builder_.AddChild(kTestUIStack, GetTestUIStackUrl());
79
80 // // Route base system services to flutter and the test UI stack.
81 realm_builder_.AddRoute(Route{
82 .capabilities = {Protocol{fuchsia::logger::LogSink::Name_},
83 Protocol{fuchsia::inspect::InspectSink::Name_},
84 Protocol{fuchsia::sysmem::Allocator::Name_},
85 Protocol{fuchsia::sysmem2::Allocator::Name_},
86 Protocol{fuchsia::tracing::provider::Registry::Name_},
87 Protocol{fuchsia::ui::input::ImeService::Name_},
90 component_testing::Directory{"config-data"}},
91 .source = ParentRef(),
93
94 // Route UI capabilities to test driver and Flutter runner
95 realm_builder_.AddRoute(Route{
96 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
97 Protocol{fuchsia::ui::composition::Flatland::Name_},
98 Protocol{fuchsia::ui::test::input::Registry::Name_},
99 Protocol{fuchsia::ui::test::scene::Controller::Name_},
100 Protocol{fuchsia::ui::display::singleton::Info::Name_},
102 .source = kTestUIStackRef,
103 .targets = {ParentRef(), kFlutterJitRunnerRef}});
104}
105
106void PortableUITest::ProcessViewGeometryResponse(
107 fuchsia::ui::observation::geometry::WatchResponse response) {
108 // Process update if no error
109 if (!response.has_error()) {
110 std::vector<fuchsia::ui::observation::geometry::ViewTreeSnapshot>* updates =
111 response.mutable_updates();
112 if (updates && !updates->empty()) {
113 last_view_tree_snapshot_ = std::move(updates->back());
114 }
115 } else {
116 // Otherwise process error
117 const auto& error = response.error();
118 if (error | fuchsia::ui::observation::geometry::Error::CHANNEL_OVERFLOW) {
119 FML_LOG(INFO) << "View Tree watcher channel overflowed";
120 } else if (error |
121 fuchsia::ui::observation::geometry::Error::BUFFER_OVERFLOW) {
122 FML_LOG(INFO) << "View Tree watcher buffer overflowed";
123 } else if (error |
124 fuchsia::ui::observation::geometry::Error::VIEWS_OVERFLOW) {
125 // This one indicates some possible data loss, so we log with a high
126 // severity
127 FML_LOG(WARNING)
128 << "View Tree watcher attempted to report too many views";
129 }
130 }
131}
132
133void PortableUITest::WatchViewGeometry() {
134 FML_CHECK(view_tree_watcher_)
135 << "View Tree watcher must be registered before calling Watch()";
136
137 view_tree_watcher_->Watch([this](auto response) {
138 ProcessViewGeometryResponse(std::move(response));
139 WatchViewGeometry();
140 });
141}
142
143bool PortableUITest::HasViewConnected(zx_koid_t view_ref_koid) {
144 return last_view_tree_snapshot_.has_value() &&
145 CheckViewExistsInSnapshot(*last_view_tree_snapshot_, view_ref_koid);
146}
147
149 scene_provider_ =
150 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
151 scene_provider_.set_error_handler([](auto) {
152 FML_LOG(ERROR) << "Error from test scene provider: "
153 << &zx_status_get_string;
154 });
155
156 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
157 request.set_view_provider(
158 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
159 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
160 []() {});
161 scene_provider_->AttachClientView(
162 std::move(request), [this](auto client_view_ref_koid) {
163 client_root_view_ref_koid_ = client_view_ref_koid;
164 });
165
166 FML_LOG(INFO) << "Waiting for client view ref koid";
167 RunLoopUntil([this] { return client_root_view_ref_koid_.has_value(); });
168
169 WatchViewGeometry();
170
171 FML_LOG(INFO) << "Waiting for client view to connect";
172 // Wait for the client view to get attached to the view tree.
173 RunLoopUntil(
174 [this] { return HasViewConnected(*client_root_view_ref_koid_); });
175 FML_LOG(INFO) << "Client view has rendered";
176}
177
179 LaunchClient();
180 // At this point, the parent view must have rendered, so we just need to wait
181 // for the embedded view.
182 RunLoopUntil([this] {
183 if (!last_view_tree_snapshot_.has_value() ||
184 !last_view_tree_snapshot_->has_views()) {
185 return false;
186 }
187
188 if (!client_root_view_ref_koid_.has_value()) {
189 return false;
190 }
191
192 for (const auto& view : last_view_tree_snapshot_->views()) {
193 if (!view.has_view_ref_koid() ||
194 view.view_ref_koid() != *client_root_view_ref_koid_) {
195 continue;
196 }
197
198 if (view.children().empty()) {
199 return false;
200 }
201
202 // NOTE: We can't rely on the presence of the child view in
203 // `view.children()` to guarantee that it has rendered. The child view
204 // also needs to be present in `last_view_tree_snapshot_->views`.
205 return std::count_if(
206 last_view_tree_snapshot_->views().begin(),
207 last_view_tree_snapshot_->views().end(),
208 [view_to_find =
209 view.children().back()](const auto& view_to_check) {
210 return view_to_check.has_view_ref_koid() &&
211 view_to_check.view_ref_koid() == view_to_find;
212 }) > 0;
213 }
214
215 return false;
216 });
217
218 FML_LOG(INFO) << "Embedded view has rendered";
219}
220
222 FML_LOG(INFO) << "Registering fake touch screen";
223 input_registry_ =
224 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
225 input_registry_.set_error_handler([](auto) {
226 FML_LOG(ERROR) << "Error from input helper: " << &zx_status_get_string;
227 });
228
229 bool touchscreen_registered = false;
230 fuchsia::ui::test::input::RegistryRegisterTouchScreenRequest request;
231 request.set_device(fake_touchscreen_.NewRequest());
232 input_registry_->RegisterTouchScreen(
233 std::move(request),
234 [&touchscreen_registered]() { touchscreen_registered = true; });
235
236 RunLoopUntil([&touchscreen_registered] { return touchscreen_registered; });
237 FML_LOG(INFO) << "Touchscreen registered";
238}
239
241 FML_LOG(INFO) << "Registering fake mouse";
242 input_registry_ =
243 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
244 input_registry_.set_error_handler([](auto) {
245 FML_LOG(ERROR) << "Error from input helper: " << &zx_status_get_string;
246 });
247
248 bool mouse_registered = false;
249 fuchsia::ui::test::input::RegistryRegisterMouseRequest request;
250 request.set_device(fake_mouse_.NewRequest());
251 input_registry_->RegisterMouse(
252 std::move(request), [&mouse_registered]() { mouse_registered = true; });
253
254 RunLoopUntil([&mouse_registered] { return mouse_registered; });
255 FML_LOG(INFO) << "Mouse registered";
256}
257
259 FML_LOG(INFO) << "Registering fake keyboard";
260 input_registry_ =
261 realm_->component().Connect<fuchsia::ui::test::input::Registry>();
262 input_registry_.set_error_handler([](auto) {
263 FML_LOG(ERROR) << "Error from input helper: " << &zx_status_get_string;
264 });
265
266 bool keyboard_registered = false;
267 fuchsia::ui::test::input::RegistryRegisterKeyboardRequest request;
268 request.set_device(fake_keyboard_.NewRequest());
269 input_registry_->RegisterKeyboard(
270 std::move(request),
271 [&keyboard_registered]() { keyboard_registered = true; });
272
273 RunLoopUntil([&keyboard_registered] { return keyboard_registered; });
274 FML_LOG(INFO) << "Keyboard registered";
275}
276
277void PortableUITest::InjectTap(int32_t x, int32_t y) {
278 fuchsia::ui::test::input::TouchScreenSimulateTapRequest tap_request;
279 tap_request.mutable_tap_location()->x = x;
280 tap_request.mutable_tap_location()->y = y;
281
282 FML_LOG(INFO) << "Injecting tap at (" << tap_request.tap_location().x << ", "
283 << tap_request.tap_location().y << ")";
284 fake_touchscreen_->SimulateTap(std::move(tap_request), [this]() {
285 ++touch_injection_request_count_;
286 FML_LOG(INFO) << "*** Tap injected, count: "
287 << touch_injection_request_count_;
288 });
289}
290
292 std::vector<fuchsia::ui::test::input::MouseButton> pressed_buttons,
293 int movement_x,
294 int movement_y) {
295 fuchsia::ui::test::input::MouseSimulateMouseEventRequest request;
296 request.set_pressed_buttons(std::move(pressed_buttons));
297 request.set_movement_x(movement_x);
298 request.set_movement_y(movement_y);
299
300 FML_LOG(INFO) << "Injecting mouse input";
301
302 fake_mouse_->SimulateMouseEvent(
303 std::move(request), [] { FML_LOG(INFO) << "Mouse event injected"; });
304}
305
307 std::vector<fuchsia::ui::test::input::MouseButton> pressed_buttons,
308 int scroll_x,
309 int scroll_y,
310 bool use_physical_units) {
311 FML_LOG(INFO) << "Requesting mouse scroll";
312 fuchsia::ui::test::input::MouseSimulateMouseEventRequest request;
313 request.set_pressed_buttons(std::move(pressed_buttons));
314 if (use_physical_units) {
315 request.set_scroll_h_physical_pixel(scroll_x);
316 request.set_scroll_v_physical_pixel(scroll_y);
317 } else {
318 request.set_scroll_h_detent(scroll_x);
319 request.set_scroll_v_detent(scroll_y);
320 }
321
322 fake_mouse_->SimulateMouseEvent(std::move(request), [] {
323 FML_LOG(INFO) << "Mouse scroll event injected";
324 });
325}
326
328 FML_LOG(INFO) << "Sending text request";
329 bool done = false;
330
331 fuchsia::ui::test::input::KeyboardSimulateUsAsciiTextEntryRequest request;
332 request.set_text(text);
333 fake_keyboard_->SimulateUsAsciiTextEntry(std::move(request),
334 [&done]() { done = true; });
335
336 RunLoopUntil([&] { return done; });
337 FML_LOG(INFO) << "Text request sent";
338}
339
340} // namespace fuchsia_test_utils
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)
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)
int32_t x
if(end==-1)
FlView * view
const uint8_t uint32_t uint32_t GError ** error
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
std::u16string text
double y
bool CheckViewExistsInSnapshot(const fuchsia::ui::observation::geometry::ViewTreeSnapshot &snapshot, zx_koid_t view_ref_koid)
Definition check_view.cc:11