39using component_testing::ChildOptions;
40using component_testing::ChildRef;
41using component_testing::DirectoryContents;
42using component_testing::ParentRef;
43using component_testing::Protocol;
44using component_testing::RealmRoot;
45using component_testing::Route;
46using component_testing::StartupMode;
52constexpr auto kVulkanLoaderServiceName =
"fuchsia.vulkan.loader.Loader";
54constexpr auto kFlutterJitRunnerUrl =
55 "flutter_jit_runner#meta/flutter_jit_runner.cm";
56constexpr auto kFlutterJitProductRunnerUrl =
57 "flutter_jit_product_runner#meta/flutter_jit_product_runner.cm";
58constexpr auto kFlutterAotRunnerUrl =
59 "flutter_aot_runner#meta/flutter_aot_runner.cm";
60constexpr auto kFlutterAotProductRunnerUrl =
61 "flutter_aot_product_runner#meta/flutter_aot_product_runner.cm";
62constexpr char kChildViewUrl[] =
"child-view#meta/child-view.cm";
63constexpr char kParentViewUrl[] =
"parent-view#meta/parent-view.cm";
64static constexpr auto kTestUiStackUrl =
65 "fuchsia-pkg://fuchsia.com/flatland-scene-manager-test-ui-stack#meta/"
68constexpr auto kFlutterRunnerEnvironment =
"flutter_runner_env";
69constexpr auto kFlutterJitRunner =
"flutter_jit_runner";
70constexpr auto kFlutterJitRunnerRef = ChildRef{kFlutterJitRunner};
71constexpr auto kFlutterJitProductRunner =
"flutter_jit_product_runner";
72constexpr auto kFlutterJitProductRunnerRef = ChildRef{kFlutterJitProductRunner};
73constexpr auto kFlutterAotRunner =
"flutter_aot_runner";
74constexpr auto kFlutterAotRunnerRef = ChildRef{kFlutterAotRunner};
75constexpr auto kFlutterAotProductRunner =
"flutter_aot_product_runner";
76constexpr auto kFlutterAotProductRunnerRef = ChildRef{kFlutterAotProductRunner};
77constexpr auto kChildView =
"child_view";
78constexpr auto kChildViewRef = ChildRef{kChildView};
79constexpr auto kParentView =
"parent_view";
80constexpr auto kParentViewRef = ChildRef{kParentView};
81constexpr auto kTestUiStack =
"ui";
82constexpr auto kTestUiStackRef = ChildRef{kTestUiStack};
98static uint32_t OverlayPixelCount(
99 std::map<fuchsia_test_utils::Pixel, uint32_t>& histogram) {
100 return histogram[kFlatlandOverlayColor];
104constexpr zx::duration kScreenshotTimeout = zx::sec(10);
106constexpr zx::duration kTestTimeout = zx::min(1);
111 public ::testing::Test {
114 : realm_builder_(component_testing::RealmBuilder::Create()) {
115 FML_VLOG(1) <<
"Setting up base realm";
119 async::PostDelayedTask(
123 <<
"\n\n>> Test did not complete in time, terminating. <<\n\n";
129 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
131 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
133 zx_koid_t view_ref_koid);
136 const std::vector<std::string>& component_args = {});
142 fit::function<
void(std::map<fuchsia_test_utils::Pixel, uint32_t>)>
144 zx::duration timeout = kTestTimeout);
147 void SetUpRealmBase();
149 fuchsia::ui::test::scene::ControllerPtr scene_provider_;
150 fuchsia::ui::observation::geometry::ViewTreeWatcherPtr view_tree_watcher_;
151 fuchsia::ui::composition::ScreenshotPtr screenshot_;
154 component_testing::RealmBuilder realm_builder_;
155 std::unique_ptr<component_testing::RealmRoot> realm_;
157 uint64_t display_width_ = 0;
158 uint64_t display_height_ = 0;
161void FlutterEmbedderTest::SetUpRealmBase() {
162 FML_LOG(INFO) <<
"Setting up realm base.";
165 realm_builder_.AddChild(kFlutterJitRunner, kFlutterJitRunnerUrl);
166 realm_builder_.AddChild(kFlutterJitProductRunner,
167 kFlutterJitProductRunnerUrl);
168 realm_builder_.AddChild(kFlutterAotRunner, kFlutterAotRunnerUrl);
169 realm_builder_.AddChild(kFlutterAotProductRunner,
170 kFlutterAotProductRunnerUrl);
173 fuchsia::component::decl::Environment flutter_runner_environment;
174 flutter_runner_environment.set_name(kFlutterRunnerEnvironment);
175 flutter_runner_environment.set_extends(
176 fuchsia::component::decl::EnvironmentExtends::REALM);
177 flutter_runner_environment.set_runners({});
178 auto environment_runners = flutter_runner_environment.mutable_runners();
179 fuchsia::component::decl::RunnerRegistration flutter_jit_runner_reg;
180 flutter_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
181 fuchsia::component::decl::ChildRef{.name = kFlutterJitRunner}));
182 flutter_jit_runner_reg.set_source_name(kFlutterJitRunner);
183 flutter_jit_runner_reg.set_target_name(kFlutterJitRunner);
184 environment_runners->push_back(std::move(flutter_jit_runner_reg));
185 fuchsia::component::decl::RunnerRegistration flutter_jit_product_runner_reg;
186 flutter_jit_product_runner_reg.set_source(
187 fuchsia::component::decl::Ref::WithChild(
188 fuchsia::component::decl::ChildRef{.name =
189 kFlutterJitProductRunner}));
190 flutter_jit_product_runner_reg.set_source_name(kFlutterJitProductRunner);
191 flutter_jit_product_runner_reg.set_target_name(kFlutterJitProductRunner);
192 environment_runners->push_back(std::move(flutter_jit_product_runner_reg));
193 fuchsia::component::decl::RunnerRegistration flutter_aot_runner_reg;
194 flutter_aot_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
195 fuchsia::component::decl::ChildRef{.name = kFlutterAotRunner}));
196 flutter_aot_runner_reg.set_source_name(kFlutterAotRunner);
197 flutter_aot_runner_reg.set_target_name(kFlutterAotRunner);
198 environment_runners->push_back(std::move(flutter_aot_runner_reg));
199 fuchsia::component::decl::RunnerRegistration flutter_aot_product_runner_reg;
200 flutter_aot_product_runner_reg.set_source(
201 fuchsia::component::decl::Ref::WithChild(
202 fuchsia::component::decl::ChildRef{.name =
203 kFlutterAotProductRunner}));
204 flutter_aot_product_runner_reg.set_source_name(kFlutterAotProductRunner);
205 flutter_aot_product_runner_reg.set_target_name(kFlutterAotProductRunner);
206 environment_runners->push_back(std::move(flutter_aot_product_runner_reg));
207 auto realm_decl = realm_builder_.GetRealmDecl();
208 if (!realm_decl.has_environments()) {
209 realm_decl.set_environments({});
211 auto realm_environments = realm_decl.mutable_environments();
212 realm_environments->push_back(std::move(flutter_runner_environment));
213 realm_builder_.ReplaceRealmDecl(std::move(realm_decl));
216 realm_builder_.AddChild(kTestUiStack, kTestUiStackUrl);
219 realm_builder_.AddChild(kChildView, kChildViewUrl,
221 .environment = kFlutterRunnerEnvironment,
223 realm_builder_.AddChild(kParentView, kParentViewUrl,
225 .environment = kFlutterRunnerEnvironment,
229 realm_builder_.AddRoute(
230 Route{.capabilities =
232 Protocol{fuchsia::inspect::InspectSink::Name_},
233 Protocol{fuchsia::kernel::VmexResource::Name_},
234 Protocol{fuchsia::logger::LogSink::Name_},
235 Protocol{fuchsia::sysmem::Allocator::Name_},
236 Protocol{fuchsia::sysmem2::Allocator::Name_},
237 Protocol{fuchsia::tracing::provider::Registry::Name_},
238 Protocol{kVulkanLoaderServiceName},
240 .source = ParentRef{},
241 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
242 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
245 realm_builder_.AddRoute(Route{
246 .capabilities = {Protocol{fuchsia::inspect::InspectSink::Name_},
247 Protocol{fuchsia::kernel::VmexResource::Name_},
248 Protocol{fuchsia::logger::LogSink::Name_},
249 Protocol{fuchsia::sysmem::Allocator::Name_},
250 Protocol{fuchsia::sysmem2::Allocator::Name_},
251 Protocol{fuchsia::tracing::provider::Registry::Name_},
252 Protocol{kVulkanLoaderServiceName}},
253 .source = ParentRef{},
254 .targets = {kTestUiStackRef}});
257 realm_builder_.AddRoute(Route{
258 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
259 Protocol{fuchsia::ui::composition::Flatland::Name_}},
260 .source = kTestUiStackRef,
261 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
262 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
265 realm_builder_.AddRoute(Route{
266 .capabilities = {Protocol{fuchsia::ui::composition::Screenshot::Name_},
267 Protocol{fuchsia::ui::test::input::Registry::Name_},
268 Protocol{fuchsia::ui::test::scene::Controller::Name_},
269 Protocol{fuchsia::ui::display::singleton::Info::Name_}},
270 .source = kTestUiStackRef,
271 .targets = {ParentRef()}});
274 realm_builder_.AddRoute(
275 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
276 .source = kParentViewRef,
277 .targets = {ParentRef()}});
278 realm_builder_.AddRoute(
279 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
280 .source = kChildViewRef,
281 .targets = {kParentViewRef}});
288 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
290 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
292 zx_koid_t view_ref_koid) {
293 std::optional<fuchsia::ui::observation::geometry::WatchResponse> watch_result;
294 view_tree_watcher->Watch(
295 [&watch_result](
auto response) { watch_result = std::move(response); });
296 FML_LOG(INFO) <<
"Waiting for view tree watch result";
297 RunLoopUntil([&watch_result] {
return watch_result.has_value(); });
298 FML_LOG(INFO) <<
"Received for view tree watch result";
299 if (CheckViewExistsInUpdates(watch_result->updates(), view_ref_koid)) {
300 watch_response = std::move(watch_result);
302 return watch_response.has_value();
306 const std::vector<std::string>& component_args) {
307 FML_LOG(INFO) <<
"Launching parent-view";
309 if (!component_args.empty()) {
313 for (
const auto& arg : component_args) {
319 auto config_directory_contents = DirectoryContents();
320 config_directory_contents.AddFile(
"args.csv", csv);
321 realm_builder_.RouteReadOnlyDirectory(
"config-data", {kParentViewRef},
322 std::move(config_directory_contents));
324 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
327 std::optional<bool> display_metrics_obtained;
328 fuchsia::ui::display::singleton::InfoPtr display_info =
329 realm_->component().Connect<fuchsia::ui::display::singleton::Info>();
330 display_info->GetMetrics([
this, &display_metrics_obtained](
auto info) {
331 display_width_ = info.extent_in_px().width;
332 display_height_ = info.extent_in_px().height;
333 display_metrics_obtained =
true;
335 RunLoopUntil([&display_metrics_obtained] {
336 return display_metrics_obtained.has_value();
338 FML_LOG(INFO) <<
"Got display_width " << display_width_ <<
" display_height "
342 std::optional<zx_koid_t> view_ref_koid;
344 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
345 scene_provider_.set_error_handler(
346 [](
auto) {
FML_LOG(ERROR) <<
"Error from test scene provider"; });
347 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
348 request.set_view_provider(
349 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
350 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
352 scene_provider_->AttachClientView(
353 std::move(request), [&view_ref_koid](
auto client_view_ref_koid) {
354 view_ref_koid = client_view_ref_koid;
357 FML_LOG(INFO) <<
"Waiting for client view ref koid";
358 RunLoopUntil([&view_ref_koid] {
return view_ref_koid.has_value(); });
361 std::optional<fuchsia::ui::observation::geometry::WatchResponse>
363 FML_LOG(INFO) <<
"Waiting for client view to render; koid is "
364 << (view_ref_koid.has_value() ? view_ref_koid.value() : 0);
365 RunLoopUntil([
this, &watch_response, &view_ref_koid] {
366 return HasViewConnected(view_tree_watcher_, watch_response, *view_ref_koid);
368 FML_LOG(INFO) <<
"Client view has rendered";
371 realm_->component().Connect<fuchsia::ui::composition::Screenshot>();
372 FML_LOG(INFO) <<
"Launched parent-view";
376 FML_LOG(INFO) <<
"Taking screenshot... ";
378 fuchsia::ui::composition::ScreenshotTakeRequest request;
379 request.set_format(fuchsia::ui::composition::ScreenshotFormat::BGRA_RAW);
381 std::optional<fuchsia::ui::composition::ScreenshotTakeResponse> response;
382 screenshot_->Take(std::move(request), [
this, &response](
auto screenshot) {
383 response = std::move(screenshot);
387 EXPECT_FALSE(RunLoopWithTimeout(kScreenshotTimeout))
388 <<
"Timed out waiting for screenshot.";
389 FML_LOG(INFO) <<
"Screenshot captured.";
392 response->vmo(), display_width_, display_height_, 0);
397 fit::function<
void(std::map<fuchsia_test_utils::Pixel, uint32_t>)>
callback,
398 zx::duration timeout) {
399 return RunLoopWithTimeoutOrUntil(
402 auto histogram = screenshot.Histogram();
404 bool color_found = histogram[color] > 0;
405 if (color_found &&
callback !=
nullptr) {
414 LaunchParentViewInRealm();
417 ASSERT_TRUE(TakeScreenshotUntil(
418 kChildBackgroundColor,
419 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
422 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
423 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
424 EXPECT_GT(histogram[kParentBackgroundColor],
425 histogram[kChildBackgroundColor]);
430 LaunchParentViewInRealm({
"--showOverlay"});
433 ASSERT_TRUE(TakeScreenshotUntil(
434 kChildBackgroundColor,
435 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
438 const uint32_t overlay_pixel_count = OverlayPixelCount(histogram);
439 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
440 EXPECT_GT(overlay_pixel_count, 0u);
441 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
442 EXPECT_GT(histogram[kParentBackgroundColor],
443 histogram[kChildBackgroundColor]);
444 EXPECT_GT(overlay_pixel_count, histogram[kChildBackgroundColor]);