5#include <fuchsia/inspect/cpp/fidl.h>
6#include <fuchsia/logger/cpp/fidl.h>
7#include <fuchsia/sysmem/cpp/fidl.h>
8#include <fuchsia/sysmem2/cpp/fidl.h>
9#include <fuchsia/tracing/provider/cpp/fidl.h>
10#include <fuchsia/ui/app/cpp/fidl.h>
11#include <fuchsia/ui/composition/cpp/fidl.h>
12#include <fuchsia/ui/display/singleton/cpp/fidl.h>
13#include <fuchsia/ui/observation/geometry/cpp/fidl.h>
14#include <fuchsia/ui/test/input/cpp/fidl.h>
15#include <fuchsia/ui/test/scene/cpp/fidl.h>
16#include <lib/async-loop/testing/cpp/real_loop.h>
17#include <lib/async/cpp/task.h>
18#include <lib/fidl/cpp/binding_set.h>
19#include <lib/sys/component/cpp/testing/realm_builder.h>
20#include <lib/sys/component/cpp/testing/realm_builder_types.h>
21#include <lib/zx/clock.h>
22#include <zircon/status.h>
23#include <zircon/time.h>
28#include "flutter/fml/logging.h"
29#include "gtest/gtest.h"
31#include "flutter/shell/platform/fuchsia/flutter/tests/integration/utils/check_view.h"
32#include "flutter/shell/platform/fuchsia/flutter/tests/integration/utils/screenshot.h"
38using component_testing::ChildOptions;
39using component_testing::ChildRef;
40using component_testing::DirectoryContents;
41using component_testing::ParentRef;
42using component_testing::Protocol;
43using component_testing::RealmRoot;
44using component_testing::Route;
45using component_testing::StartupMode;
51constexpr auto kVulkanLoaderServiceName =
"fuchsia.vulkan.loader.Loader";
53constexpr auto kFlutterJitRunnerUrl =
54 "fuchsia-pkg://fuchsia.com/oot_flutter_jit_runner#meta/"
55 "flutter_jit_runner.cm";
56constexpr auto kFlutterJitProductRunnerUrl =
57 "fuchsia-pkg://fuchsia.com/oot_flutter_jit_product_runner#meta/"
58 "flutter_jit_product_runner.cm";
59constexpr auto kFlutterAotRunnerUrl =
60 "fuchsia-pkg://fuchsia.com/oot_flutter_aot_runner#meta/"
61 "flutter_aot_runner.cm";
62constexpr auto kFlutterAotProductRunnerUrl =
63 "fuchsia-pkg://fuchsia.com/oot_flutter_aot_product_runner#meta/"
64 "flutter_aot_product_runner.cm";
65constexpr char kChildViewUrl[] =
66 "fuchsia-pkg://fuchsia.com/child-view#meta/child-view.cm";
67constexpr char kParentViewUrl[] =
68 "fuchsia-pkg://fuchsia.com/parent-view#meta/parent-view.cm";
69static constexpr auto kTestUiStackUrl =
70 "fuchsia-pkg://fuchsia.com/flatland-scene-manager-test-ui-stack#meta/"
73constexpr auto kFlutterRunnerEnvironment =
"flutter_runner_env";
74constexpr auto kFlutterJitRunner =
"flutter_jit_runner";
75constexpr auto kFlutterJitRunnerRef = ChildRef{kFlutterJitRunner};
76constexpr auto kFlutterJitProductRunner =
"flutter_jit_product_runner";
77constexpr auto kFlutterJitProductRunnerRef = ChildRef{kFlutterJitProductRunner};
78constexpr auto kFlutterAotRunner =
"flutter_aot_runner";
79constexpr auto kFlutterAotRunnerRef = ChildRef{kFlutterAotRunner};
80constexpr auto kFlutterAotProductRunner =
"flutter_aot_product_runner";
81constexpr auto kFlutterAotProductRunnerRef = ChildRef{kFlutterAotProductRunner};
82constexpr auto kChildView =
"child_view";
83constexpr auto kChildViewRef = ChildRef{kChildView};
84constexpr auto kParentView =
"parent_view";
85constexpr auto kParentViewRef = ChildRef{kParentView};
86constexpr auto kTestUiStack =
"ui";
87constexpr auto kTestUiStackRef = ChildRef{kTestUiStack};
103static uint32_t OverlayPixelCount(
104 std::map<fuchsia_test_utils::Pixel, uint32_t>& histogram) {
105 return histogram[kFlatlandOverlayColor];
116 public ::testing::Test {
119 : realm_builder_(component_testing::RealmBuilder::
Create()) {
120 FML_VLOG(1) <<
"Setting up base realm";
124 async::PostDelayedTask(
128 <<
"\n\n>> Test did not complete in time, terminating. <<\n\n";
134 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
136 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
138 zx_koid_t view_ref_koid);
141 const std::vector<std::string>& component_args = {});
147 fit::function<
void(std::map<fuchsia_test_utils::Pixel, uint32_t>)>
152 void SetUpRealmBase();
154 fuchsia::ui::test::scene::ControllerPtr scene_provider_;
155 fuchsia::ui::observation::geometry::ViewTreeWatcherPtr view_tree_watcher_;
156 fuchsia::ui::composition::ScreenshotPtr screenshot_;
159 component_testing::RealmBuilder realm_builder_;
160 std::unique_ptr<component_testing::RealmRoot> realm_;
162 uint64_t display_width_ = 0;
163 uint64_t display_height_ = 0;
166void FlutterEmbedderTest::SetUpRealmBase() {
167 FML_LOG(INFO) <<
"Setting up realm base.";
170 realm_builder_.AddChild(kFlutterJitRunner, kFlutterJitRunnerUrl);
171 realm_builder_.AddChild(kFlutterJitProductRunner,
172 kFlutterJitProductRunnerUrl);
173 realm_builder_.AddChild(kFlutterAotRunner, kFlutterAotRunnerUrl);
174 realm_builder_.AddChild(kFlutterAotProductRunner,
175 kFlutterAotProductRunnerUrl);
178 fuchsia::component::decl::Environment flutter_runner_environment;
179 flutter_runner_environment.set_name(kFlutterRunnerEnvironment);
180 flutter_runner_environment.set_extends(
181 fuchsia::component::decl::EnvironmentExtends::REALM);
182 flutter_runner_environment.set_runners({});
183 auto environment_runners = flutter_runner_environment.mutable_runners();
184 fuchsia::component::decl::RunnerRegistration flutter_jit_runner_reg;
185 flutter_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
186 fuchsia::component::decl::ChildRef{.name = kFlutterJitRunner}));
187 flutter_jit_runner_reg.set_source_name(kFlutterJitRunner);
188 flutter_jit_runner_reg.set_target_name(kFlutterJitRunner);
189 environment_runners->push_back(std::move(flutter_jit_runner_reg));
190 fuchsia::component::decl::RunnerRegistration flutter_jit_product_runner_reg;
191 flutter_jit_product_runner_reg.set_source(
192 fuchsia::component::decl::Ref::WithChild(
193 fuchsia::component::decl::ChildRef{.name =
194 kFlutterJitProductRunner}));
195 flutter_jit_product_runner_reg.set_source_name(kFlutterJitProductRunner);
196 flutter_jit_product_runner_reg.set_target_name(kFlutterJitProductRunner);
197 environment_runners->push_back(std::move(flutter_jit_product_runner_reg));
198 fuchsia::component::decl::RunnerRegistration flutter_aot_runner_reg;
199 flutter_aot_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
200 fuchsia::component::decl::ChildRef{.name = kFlutterAotRunner}));
201 flutter_aot_runner_reg.set_source_name(kFlutterAotRunner);
202 flutter_aot_runner_reg.set_target_name(kFlutterAotRunner);
203 environment_runners->push_back(std::move(flutter_aot_runner_reg));
204 fuchsia::component::decl::RunnerRegistration flutter_aot_product_runner_reg;
205 flutter_aot_product_runner_reg.set_source(
206 fuchsia::component::decl::Ref::WithChild(
207 fuchsia::component::decl::ChildRef{.name =
208 kFlutterAotProductRunner}));
209 flutter_aot_product_runner_reg.set_source_name(kFlutterAotProductRunner);
210 flutter_aot_product_runner_reg.set_target_name(kFlutterAotProductRunner);
211 environment_runners->push_back(std::move(flutter_aot_product_runner_reg));
212 auto realm_decl = realm_builder_.GetRealmDecl();
213 if (!realm_decl.has_environments()) {
214 realm_decl.set_environments({});
216 auto realm_environments = realm_decl.mutable_environments();
217 realm_environments->push_back(std::move(flutter_runner_environment));
218 realm_builder_.ReplaceRealmDecl(std::move(realm_decl));
221 realm_builder_.AddChild(kTestUiStack, kTestUiStackUrl);
224 realm_builder_.AddChild(kChildView, kChildViewUrl,
226 .environment = kFlutterRunnerEnvironment,
228 realm_builder_.AddChild(kParentView, kParentViewUrl,
230 .environment = kFlutterRunnerEnvironment,
234 realm_builder_.AddRoute(
235 Route{.capabilities =
237 Protocol{fuchsia::logger::LogSink::Name_},
238 Protocol{fuchsia::inspect::InspectSink::Name_},
239 Protocol{fuchsia::sysmem::Allocator::Name_},
244 Protocol{
"fuchsia.sysmem2.Allocator"},
245 Protocol{fuchsia::tracing::provider::Registry::Name_},
246 Protocol{kVulkanLoaderServiceName},
248 .source = ParentRef{},
249 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
250 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
253 realm_builder_.AddRoute(Route{
254 .capabilities = {Protocol{fuchsia::logger::LogSink::Name_},
255 Protocol{fuchsia::inspect::InspectSink::Name_},
256 Protocol{fuchsia::sysmem::Allocator::Name_},
261 Protocol{
"fuchsia.sysmem2.Allocator"},
262 Protocol{fuchsia::tracing::provider::Registry::Name_},
263 Protocol{kVulkanLoaderServiceName}},
264 .source = ParentRef{},
265 .targets = {kTestUiStackRef}});
268 realm_builder_.AddRoute(Route{
269 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
270 Protocol{fuchsia::ui::composition::Flatland::Name_}},
271 .source = kTestUiStackRef,
272 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
273 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
276 realm_builder_.AddRoute(Route{
277 .capabilities = {Protocol{fuchsia::ui::composition::Screenshot::Name_},
278 Protocol{fuchsia::ui::test::input::Registry::Name_},
279 Protocol{fuchsia::ui::test::scene::Controller::Name_},
280 Protocol{fuchsia::ui::display::singleton::Info::Name_}},
281 .source = kTestUiStackRef,
282 .targets = {ParentRef()}});
285 realm_builder_.AddRoute(
286 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
287 .source = kParentViewRef,
288 .targets = {ParentRef()}});
289 realm_builder_.AddRoute(
290 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
291 .source = kChildViewRef,
292 .targets = {kParentViewRef}});
299 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
301 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
303 zx_koid_t view_ref_koid) {
304 std::optional<fuchsia::ui::observation::geometry::WatchResponse> watch_result;
305 view_tree_watcher->Watch(
306 [&watch_result](
auto response) { watch_result = std::move(response); });
307 FML_LOG(INFO) <<
"Waiting for view tree watch result";
308 RunLoopUntil([&watch_result] {
return watch_result.has_value(); });
309 FML_LOG(INFO) <<
"Received for view tree watch result";
311 watch_response = std::move(watch_result);
313 return watch_response.has_value();
317 const std::vector<std::string>& component_args) {
318 FML_LOG(INFO) <<
"Launching parent-view";
320 if (!component_args.empty()) {
324 for (
const auto& arg : component_args) {
330 auto config_directory_contents = DirectoryContents();
331 config_directory_contents.AddFile(
"args.csv", csv);
332 realm_builder_.RouteReadOnlyDirectory(
"config-data", {kParentViewRef},
333 std::move(config_directory_contents));
335 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
338 std::optional<bool> display_metrics_obtained;
339 fuchsia::ui::display::singleton::InfoPtr display_info =
340 realm_->component().Connect<fuchsia::ui::display::singleton::Info>();
341 display_info->GetMetrics([
this, &display_metrics_obtained](
auto info) {
342 display_width_ =
info.extent_in_px().width;
343 display_height_ =
info.extent_in_px().height;
344 display_metrics_obtained =
true;
346 RunLoopUntil([&display_metrics_obtained] {
347 return display_metrics_obtained.has_value();
349 FML_LOG(INFO) <<
"Got display_width " << display_width_ <<
" display_height "
353 std::optional<zx_koid_t> view_ref_koid;
355 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
356 scene_provider_.set_error_handler(
357 [](
auto) {
FML_LOG(
ERROR) <<
"Error from test scene provider"; });
358 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
359 request.set_view_provider(
360 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
361 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
363 scene_provider_->AttachClientView(
364 std::move(request), [&view_ref_koid](
auto client_view_ref_koid) {
365 view_ref_koid = client_view_ref_koid;
368 FML_LOG(INFO) <<
"Waiting for client view ref koid";
369 RunLoopUntil([&view_ref_koid] {
return view_ref_koid.has_value(); });
372 std::optional<fuchsia::ui::observation::geometry::WatchResponse>
374 FML_LOG(INFO) <<
"Waiting for client view to render; koid is "
375 << (view_ref_koid.has_value() ? view_ref_koid.value() : 0);
376 RunLoopUntil([
this, &watch_response, &view_ref_koid] {
377 return HasViewConnected(view_tree_watcher_, watch_response, *view_ref_koid);
379 FML_LOG(INFO) <<
"Client view has rendered";
382 realm_->component().Connect<fuchsia::ui::composition::Screenshot>();
383 FML_LOG(INFO) <<
"Launched parent-view";
387 FML_LOG(INFO) <<
"Taking screenshot... ";
389 fuchsia::ui::composition::ScreenshotTakeRequest request;
390 request.set_format(fuchsia::ui::composition::ScreenshotFormat::BGRA_RAW);
392 std::optional<fuchsia::ui::composition::ScreenshotTakeResponse> response;
393 screenshot_->Take(std::move(request), [
this, &response](
auto screenshot) {
394 response = std::move(screenshot);
398 EXPECT_FALSE(RunLoopWithTimeout(kScreenshotTimeout))
399 <<
"Timed out waiting for screenshot.";
400 FML_LOG(INFO) <<
"Screenshot captured.";
403 response->vmo(), display_width_, display_height_, 0);
410 return RunLoopWithTimeoutOrUntil(
413 auto histogram = screenshot.Histogram();
415 bool color_found = histogram[
color] > 0;
416 if (color_found &&
callback !=
nullptr) {
425 LaunchParentViewInRealm();
428 ASSERT_TRUE(TakeScreenshotUntil(
429 kChildBackgroundColor,
430 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
433 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
434 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
435 EXPECT_GT(histogram[kParentBackgroundColor],
436 histogram[kChildBackgroundColor]);
441 LaunchParentViewInRealm({
"--showOverlay"});
444 ASSERT_TRUE(TakeScreenshotUntil(
445 kChildBackgroundColor,
446 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
449 const uint32_t overlay_pixel_count = OverlayPixelCount(histogram);
450 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
451 EXPECT_GT(overlay_pixel_count, 0u);
452 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
453 EXPECT_GT(histogram[kParentBackgroundColor],
454 histogram[kChildBackgroundColor]);
455 EXPECT_GT(overlay_pixel_count, histogram[kChildBackgroundColor]);
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
static sk_sp< Effect > Create()
fuchsia_test_utils::Screenshot TakeScreenshot()
bool HasViewConnected(const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr &view_tree_watcher, std::optional< fuchsia::ui::observation::geometry::WatchResponse > &watch_response, zx_koid_t view_ref_koid)
bool TakeScreenshotUntil(fuchsia_test_utils::Pixel color, fit::function< void(std::map< fuchsia_test_utils::Pixel, uint32_t >)> callback=nullptr, zx::duration timeout=kTestTimeout)
void LaunchParentViewInRealm(const std::vector< std::string > &component_args={})
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_VLOG(verbose_level)
#define FML_LOG(severity)
Dart_NativeFunction function
static float min(float r, float g, float b)
TEST_F(FlutterEmbedderTest, Embedding)
bool CheckViewExistsInUpdates(const std::vector< fuchsia::ui::observation::geometry::ViewTreeSnapshot > &updates, zx_koid_t view_ref_koid)
def timeout(deadline, cmd)