36using component_testing::ChildOptions;
37using component_testing::ChildRef;
38using component_testing::DirectoryContents;
39using component_testing::ParentRef;
40using component_testing::Protocol;
41using component_testing::RealmRoot;
42using component_testing::Route;
43using component_testing::StartupMode;
49constexpr auto kVulkanLoaderServiceName =
"fuchsia.vulkan.loader.Loader";
51constexpr auto kFlutterJitRunnerUrl =
52 "fuchsia-pkg://fuchsia.com/oot_flutter_jit_runner#meta/"
53 "flutter_jit_runner.cm";
54constexpr auto kFlutterJitProductRunnerUrl =
55 "fuchsia-pkg://fuchsia.com/oot_flutter_jit_product_runner#meta/"
56 "flutter_jit_product_runner.cm";
57constexpr auto kFlutterAotRunnerUrl =
58 "fuchsia-pkg://fuchsia.com/oot_flutter_aot_runner#meta/"
59 "flutter_aot_runner.cm";
60constexpr auto kFlutterAotProductRunnerUrl =
61 "fuchsia-pkg://fuchsia.com/oot_flutter_aot_product_runner#meta/"
62 "flutter_aot_product_runner.cm";
63constexpr char kChildViewUrl[] =
64 "fuchsia-pkg://fuchsia.com/child-view#meta/child-view.cm";
65constexpr char kParentViewUrl[] =
66 "fuchsia-pkg://fuchsia.com/parent-view#meta/parent-view.cm";
67static constexpr auto kTestUiStackUrl =
68 "fuchsia-pkg://fuchsia.com/flatland-scene-manager-test-ui-stack#meta/"
71constexpr auto kFlutterRunnerEnvironment =
"flutter_runner_env";
72constexpr auto kFlutterJitRunner =
"flutter_jit_runner";
73constexpr auto kFlutterJitRunnerRef = ChildRef{kFlutterJitRunner};
74constexpr auto kFlutterJitProductRunner =
"flutter_jit_product_runner";
75constexpr auto kFlutterJitProductRunnerRef = ChildRef{kFlutterJitProductRunner};
76constexpr auto kFlutterAotRunner =
"flutter_aot_runner";
77constexpr auto kFlutterAotRunnerRef = ChildRef{kFlutterAotRunner};
78constexpr auto kFlutterAotProductRunner =
"flutter_aot_product_runner";
79constexpr auto kFlutterAotProductRunnerRef = ChildRef{kFlutterAotProductRunner};
80constexpr auto kChildView =
"child_view";
81constexpr auto kChildViewRef = ChildRef{kChildView};
82constexpr auto kParentView =
"parent_view";
83constexpr auto kParentViewRef = ChildRef{kParentView};
84constexpr auto kTestUiStack =
"ui";
85constexpr auto kTestUiStackRef = ChildRef{kTestUiStack};
101static uint32_t OverlayPixelCount(
102 std::map<fuchsia_test_utils::Pixel, uint32_t>& histogram) {
103 return histogram[kFlatlandOverlayColor];
107constexpr zx::duration kScreenshotTimeout = zx::sec(10);
109constexpr zx::duration kTestTimeout = zx::min(1);
114 public ::testing::Test {
117 : realm_builder_(component_testing::RealmBuilder::
Create()) {
118 FML_VLOG(1) <<
"Setting up base realm";
122 async::PostDelayedTask(
126 <<
"\n\n>> Test did not complete in time, terminating. <<\n\n";
132 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
134 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
136 zx_koid_t view_ref_koid);
139 const std::vector<std::string>& component_args = {});
145 fit::function<
void(std::map<fuchsia_test_utils::Pixel, uint32_t>)>
147 zx::duration timeout = kTestTimeout);
150 void SetUpRealmBase();
152 fuchsia::ui::test::scene::ControllerPtr scene_provider_;
153 fuchsia::ui::observation::geometry::ViewTreeWatcherPtr view_tree_watcher_;
154 fuchsia::ui::composition::ScreenshotPtr screenshot_;
157 component_testing::RealmBuilder realm_builder_;
158 std::unique_ptr<component_testing::RealmRoot> realm_;
160 uint64_t display_width_ = 0;
161 uint64_t display_height_ = 0;
164void FlutterEmbedderTest::SetUpRealmBase() {
165 FML_LOG(INFO) <<
"Setting up realm base.";
168 realm_builder_.AddChild(kFlutterJitRunner, kFlutterJitRunnerUrl);
169 realm_builder_.AddChild(kFlutterJitProductRunner,
170 kFlutterJitProductRunnerUrl);
171 realm_builder_.AddChild(kFlutterAotRunner, kFlutterAotRunnerUrl);
172 realm_builder_.AddChild(kFlutterAotProductRunner,
173 kFlutterAotProductRunnerUrl);
176 fuchsia::component::decl::Environment flutter_runner_environment;
177 flutter_runner_environment.set_name(kFlutterRunnerEnvironment);
178 flutter_runner_environment.set_extends(
179 fuchsia::component::decl::EnvironmentExtends::REALM);
180 flutter_runner_environment.set_runners({});
181 auto environment_runners = flutter_runner_environment.mutable_runners();
182 fuchsia::component::decl::RunnerRegistration flutter_jit_runner_reg;
183 flutter_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
184 fuchsia::component::decl::ChildRef{.name = kFlutterJitRunner}));
185 flutter_jit_runner_reg.set_source_name(kFlutterJitRunner);
186 flutter_jit_runner_reg.set_target_name(kFlutterJitRunner);
187 environment_runners->push_back(std::move(flutter_jit_runner_reg));
188 fuchsia::component::decl::RunnerRegistration flutter_jit_product_runner_reg;
189 flutter_jit_product_runner_reg.set_source(
190 fuchsia::component::decl::Ref::WithChild(
191 fuchsia::component::decl::ChildRef{.name =
192 kFlutterJitProductRunner}));
193 flutter_jit_product_runner_reg.set_source_name(kFlutterJitProductRunner);
194 flutter_jit_product_runner_reg.set_target_name(kFlutterJitProductRunner);
195 environment_runners->push_back(std::move(flutter_jit_product_runner_reg));
196 fuchsia::component::decl::RunnerRegistration flutter_aot_runner_reg;
197 flutter_aot_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
198 fuchsia::component::decl::ChildRef{.name = kFlutterAotRunner}));
199 flutter_aot_runner_reg.set_source_name(kFlutterAotRunner);
200 flutter_aot_runner_reg.set_target_name(kFlutterAotRunner);
201 environment_runners->push_back(std::move(flutter_aot_runner_reg));
202 fuchsia::component::decl::RunnerRegistration flutter_aot_product_runner_reg;
203 flutter_aot_product_runner_reg.set_source(
204 fuchsia::component::decl::Ref::WithChild(
205 fuchsia::component::decl::ChildRef{.name =
206 kFlutterAotProductRunner}));
207 flutter_aot_product_runner_reg.set_source_name(kFlutterAotProductRunner);
208 flutter_aot_product_runner_reg.set_target_name(kFlutterAotProductRunner);
209 environment_runners->push_back(std::move(flutter_aot_product_runner_reg));
210 auto realm_decl = realm_builder_.GetRealmDecl();
211 if (!realm_decl.has_environments()) {
212 realm_decl.set_environments({});
214 auto realm_environments = realm_decl.mutable_environments();
215 realm_environments->push_back(std::move(flutter_runner_environment));
216 realm_builder_.ReplaceRealmDecl(std::move(realm_decl));
219 realm_builder_.AddChild(kTestUiStack, kTestUiStackUrl);
222 realm_builder_.AddChild(kChildView, kChildViewUrl,
224 .environment = kFlutterRunnerEnvironment,
226 realm_builder_.AddChild(kParentView, kParentViewUrl,
228 .environment = kFlutterRunnerEnvironment,
232 realm_builder_.AddRoute(
233 Route{.capabilities =
235 Protocol{fuchsia::logger::LogSink::Name_},
236 Protocol{fuchsia::inspect::InspectSink::Name_},
237 Protocol{fuchsia::sysmem::Allocator::Name_},
238 Protocol{fuchsia::tracing::provider::Registry::Name_},
239 Protocol{kVulkanLoaderServiceName},
241 .source = ParentRef{},
242 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
243 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
246 realm_builder_.AddRoute(Route{
247 .capabilities = {Protocol{fuchsia::logger::LogSink::Name_},
248 Protocol{fuchsia::inspect::InspectSink::Name_},
249 Protocol{fuchsia::sysmem::Allocator::Name_},
250 Protocol{fuchsia::tracing::provider::Registry::Name_},
251 Protocol{kVulkanLoaderServiceName}},
252 .source = ParentRef{},
253 .targets = {kTestUiStackRef}});
256 realm_builder_.AddRoute(Route{
257 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
258 Protocol{fuchsia::ui::composition::Flatland::Name_}},
259 .source = kTestUiStackRef,
260 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
261 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
264 realm_builder_.AddRoute(Route{
265 .capabilities = {Protocol{fuchsia::ui::composition::Screenshot::Name_},
266 Protocol{fuchsia::ui::test::input::Registry::Name_},
267 Protocol{fuchsia::ui::test::scene::Controller::Name_},
268 Protocol{fuchsia::ui::display::singleton::Info::Name_}},
269 .source = kTestUiStackRef,
270 .targets = {ParentRef()}});
273 realm_builder_.AddRoute(
274 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
275 .source = kParentViewRef,
276 .targets = {ParentRef()}});
277 realm_builder_.AddRoute(
278 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
279 .source = kChildViewRef,
280 .targets = {kParentViewRef}});
287 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
289 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
291 zx_koid_t view_ref_koid) {
292 std::optional<fuchsia::ui::observation::geometry::WatchResponse> watch_result;
293 view_tree_watcher->Watch(
294 [&watch_result](
auto response) { watch_result = std::move(response); });
295 FML_LOG(INFO) <<
"Waiting for view tree watch result";
296 RunLoopUntil([&watch_result] {
return watch_result.has_value(); });
297 FML_LOG(INFO) <<
"Received for view tree watch result";
298 if (CheckViewExistsInUpdates(watch_result->updates(), view_ref_koid)) {
299 watch_response = std::move(watch_result);
301 return watch_response.has_value();
305 const std::vector<std::string>& component_args) {
306 FML_LOG(INFO) <<
"Launching parent-view";
308 if (!component_args.empty()) {
312 for (
const auto& arg : component_args) {
318 auto config_directory_contents = DirectoryContents();
319 config_directory_contents.AddFile(
"args.csv", csv);
320 realm_builder_.RouteReadOnlyDirectory(
"config-data", {kParentViewRef},
321 std::move(config_directory_contents));
323 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
326 std::optional<bool> display_metrics_obtained;
327 fuchsia::ui::display::singleton::InfoPtr display_info =
328 realm_->component().Connect<fuchsia::ui::display::singleton::Info>();
329 display_info->GetMetrics([
this, &display_metrics_obtained](
auto info) {
330 display_width_ =
info.extent_in_px().width;
331 display_height_ =
info.extent_in_px().height;
332 display_metrics_obtained =
true;
334 RunLoopUntil([&display_metrics_obtained] {
335 return display_metrics_obtained.has_value();
337 FML_LOG(INFO) <<
"Got display_width " << display_width_ <<
" display_height "
341 std::optional<zx_koid_t> view_ref_koid;
343 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
344 scene_provider_.set_error_handler(
345 [](
auto) {
FML_LOG(
ERROR) <<
"Error from test scene provider"; });
346 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
347 request.set_view_provider(
348 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
349 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
351 scene_provider_->AttachClientView(
352 std::move(request), [&view_ref_koid](
auto client_view_ref_koid) {
353 view_ref_koid = client_view_ref_koid;
356 FML_LOG(INFO) <<
"Waiting for client view ref koid";
357 RunLoopUntil([&view_ref_koid] {
return view_ref_koid.has_value(); });
360 std::optional<fuchsia::ui::observation::geometry::WatchResponse>
362 FML_LOG(INFO) <<
"Waiting for client view to render; koid is "
363 << (view_ref_koid.has_value() ? view_ref_koid.value() : 0);
364 RunLoopUntil([
this, &watch_response, &view_ref_koid] {
365 return HasViewConnected(view_tree_watcher_, watch_response, *view_ref_koid);
367 FML_LOG(INFO) <<
"Client view has rendered";
370 realm_->component().Connect<fuchsia::ui::composition::Screenshot>();
371 FML_LOG(INFO) <<
"Launched parent-view";
375 FML_LOG(INFO) <<
"Taking screenshot... ";
377 fuchsia::ui::composition::ScreenshotTakeRequest request;
378 request.set_format(fuchsia::ui::composition::ScreenshotFormat::BGRA_RAW);
380 std::optional<fuchsia::ui::composition::ScreenshotTakeResponse> response;
381 screenshot_->Take(std::move(request), [
this, &response](
auto screenshot) {
382 response = std::move(screenshot);
386 EXPECT_FALSE(RunLoopWithTimeout(kScreenshotTimeout))
387 <<
"Timed out waiting for screenshot.";
388 FML_LOG(INFO) <<
"Screenshot captured.";
391 response->vmo(), display_width_, display_height_, 0);
396 fit::function<
void(std::map<fuchsia_test_utils::Pixel, uint32_t>)>
callback,
397 zx::duration timeout) {
398 return RunLoopWithTimeoutOrUntil(
401 auto histogram = screenshot.Histogram();
403 bool color_found = histogram[
color] > 0;
404 if (color_found &&
callback !=
nullptr) {
413 LaunchParentViewInRealm();
416 ASSERT_TRUE(TakeScreenshotUntil(
417 kChildBackgroundColor,
418 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
421 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
422 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
423 EXPECT_GT(histogram[kParentBackgroundColor],
424 histogram[kChildBackgroundColor]);
429 LaunchParentViewInRealm({
"--showOverlay"});
432 ASSERT_TRUE(TakeScreenshotUntil(
433 kChildBackgroundColor,
434 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
437 const uint32_t overlay_pixel_count = OverlayPixelCount(histogram);
438 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
439 EXPECT_GT(overlay_pixel_count, 0u);
440 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
441 EXPECT_GT(histogram[kParentBackgroundColor],
442 histogram[kChildBackgroundColor]);
443 EXPECT_GT(overlay_pixel_count, histogram[kChildBackgroundColor]);