Flutter Engine
 
Loading...
Searching...
No Matches
flutter-embedder-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 <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>
24
25#include <optional>
26#include <vector>
27
28#include "flutter/fml/logging.h"
29#include "gtest/gtest.h"
30
33
35namespace {
36
37// Types imported for the realm_builder library.
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;
46
48
49// The FIDL bindings for this service are not exposed in the Fuchsia SDK, so we
50// must encode the name manually here.
51constexpr auto kVulkanLoaderServiceName = "fuchsia.vulkan.loader.Loader";
52
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/"
71 "test-ui-stack.cm";
72
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};
88
89// Background and foreground color values.
90const fuchsia_test_utils::Pixel kParentBackgroundColor(0xFF,
91 0x00,
92 0x00,
93 0xFF); // Blue
94const fuchsia_test_utils::Pixel kChildBackgroundColor(0xFF,
95 0x00,
96 0xFF,
97 0xFF); // Pink
98const fuchsia_test_utils::Pixel kFlatlandOverlayColor(0x00,
99 0xFF,
100 0x00,
101 0xFF); // Green
102
103static uint32_t OverlayPixelCount(
104 std::map<fuchsia_test_utils::Pixel, uint32_t>& histogram) {
105 return histogram[kFlatlandOverlayColor];
106}
107
108// Timeout for |TakeScreenshot| FIDL call.
109constexpr zx::duration kScreenshotTimeout = zx::sec(10);
110// Timeout to fail the test if it goes beyond this duration.
111constexpr zx::duration kTestTimeout = zx::min(1);
112
113} // namespace
114
115class FlutterEmbedderTest : public ::loop_fixture::RealLoop,
116 public ::testing::Test {
117 public:
119 : realm_builder_(component_testing::RealmBuilder::Create()) {
120 FML_VLOG(1) << "Setting up base realm";
121 SetUpRealmBase();
122
123 // Post a "just in case" quit task, if the test hangs.
124 async::PostDelayedTask(
125 dispatcher(),
126 [] {
127 FML_LOG(FATAL)
128 << "\n\n>> Test did not complete in time, terminating. <<\n\n";
129 },
130 kTestTimeout);
131 }
132
133 bool HasViewConnected(
134 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
135 view_tree_watcher,
136 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
137 watch_response,
138 zx_koid_t view_ref_koid);
139
141 const std::vector<std::string>& component_args = {});
142
144
147 fit::function<void(std::map<fuchsia_test_utils::Pixel, uint32_t>)>
148 callback = nullptr,
149 zx::duration timeout = kTestTimeout);
150
151 private:
152 void SetUpRealmBase();
153
154 fuchsia::ui::test::scene::ControllerPtr scene_provider_;
155 fuchsia::ui::observation::geometry::ViewTreeWatcherPtr view_tree_watcher_;
156 fuchsia::ui::composition::ScreenshotPtr screenshot_;
157
158 // Wrapped in optional since the view is not created until the middle of SetUp
159 component_testing::RealmBuilder realm_builder_;
160 std::unique_ptr<component_testing::RealmRoot> realm_;
161
162 uint64_t display_width_ = 0;
163 uint64_t display_height_ = 0;
164};
165
166void FlutterEmbedderTest::SetUpRealmBase() {
167 FML_LOG(INFO) << "Setting up realm base.";
168
169 // First, add the flutter runner(s) as children.
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);
176
177 // Then, add an environment providing them.
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({});
215 }
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));
219
220 // Add test UI stack component.
221 realm_builder_.AddChild(kTestUiStack, kTestUiStackUrl);
222
223 // Add embedded parent and child components.
224 realm_builder_.AddChild(kChildView, kChildViewUrl,
225 ChildOptions{
226 .environment = kFlutterRunnerEnvironment,
227 });
228 realm_builder_.AddChild(kParentView, kParentViewUrl,
229 ChildOptions{
230 .environment = kFlutterRunnerEnvironment,
231 });
232
233 // Route base system services to flutter runners.
234 realm_builder_.AddRoute(
235 Route{.capabilities =
236 {
237 Protocol{fuchsia::logger::LogSink::Name_},
238 Protocol{fuchsia::inspect::InspectSink::Name_},
239 Protocol{fuchsia::sysmem::Allocator::Name_},
240 Protocol{fuchsia::sysmem2::Allocator::Name_},
241 Protocol{fuchsia::tracing::provider::Registry::Name_},
242 Protocol{kVulkanLoaderServiceName},
243 },
244 .source = ParentRef{},
245 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
246 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
247
248 // Route base system services to the test UI stack.
249 realm_builder_.AddRoute(Route{
250 .capabilities = {Protocol{fuchsia::logger::LogSink::Name_},
251 Protocol{fuchsia::inspect::InspectSink::Name_},
252 Protocol{fuchsia::sysmem::Allocator::Name_},
253 Protocol{fuchsia::sysmem2::Allocator::Name_},
254 Protocol{fuchsia::tracing::provider::Registry::Name_},
255 Protocol{kVulkanLoaderServiceName}},
256 .source = ParentRef{},
257 .targets = {kTestUiStackRef}});
258
259 // Route UI capabilities from test UI stack to flutter runners.
260 realm_builder_.AddRoute(Route{
261 .capabilities = {Protocol{fuchsia::ui::composition::Allocator::Name_},
262 Protocol{fuchsia::ui::composition::Flatland::Name_}},
263 .source = kTestUiStackRef,
264 .targets = {kFlutterJitRunnerRef, kFlutterJitProductRunnerRef,
265 kFlutterAotRunnerRef, kFlutterAotProductRunnerRef}});
266
267 // Route test capabilities from test UI stack to test driver.
268 realm_builder_.AddRoute(Route{
269 .capabilities = {Protocol{fuchsia::ui::composition::Screenshot::Name_},
270 Protocol{fuchsia::ui::test::input::Registry::Name_},
271 Protocol{fuchsia::ui::test::scene::Controller::Name_},
272 Protocol{fuchsia::ui::display::singleton::Info::Name_}},
273 .source = kTestUiStackRef,
274 .targets = {ParentRef()}});
275
276 // Route ViewProvider from child to parent, and parent to test.
277 realm_builder_.AddRoute(
278 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
279 .source = kParentViewRef,
280 .targets = {ParentRef()}});
281 realm_builder_.AddRoute(
282 Route{.capabilities = {Protocol{fuchsia::ui::app::ViewProvider::Name_}},
283 .source = kChildViewRef,
284 .targets = {kParentViewRef}});
285}
286
287// Checks whether the view with |view_ref_koid| has connected to the view tree.
288// The response of a f.u.o.g.Provider.Watch call is stored in |watch_response|
289// if it contains |view_ref_koid|.
291 const fuchsia::ui::observation::geometry::ViewTreeWatcherPtr&
292 view_tree_watcher,
293 std::optional<fuchsia::ui::observation::geometry::WatchResponse>&
294 watch_response,
295 zx_koid_t view_ref_koid) {
296 std::optional<fuchsia::ui::observation::geometry::WatchResponse> watch_result;
297 view_tree_watcher->Watch(
298 [&watch_result](auto response) { watch_result = std::move(response); });
299 FML_LOG(INFO) << "Waiting for view tree watch result";
300 RunLoopUntil([&watch_result] { return watch_result.has_value(); });
301 FML_LOG(INFO) << "Received for view tree watch result";
302 if (CheckViewExistsInUpdates(watch_result->updates(), view_ref_koid)) {
303 watch_response = std::move(watch_result);
304 };
305 return watch_response.has_value();
306}
307
309 const std::vector<std::string>& component_args) {
310 FML_LOG(INFO) << "Launching parent-view";
311
312 if (!component_args.empty()) {
313 // Construct a args.csv file containing the specified comma-separated
314 // component args.
315 std::string csv;
316 for (const auto& arg : component_args) {
317 csv += arg + ',';
318 }
319 // Remove last comma.
320 csv.pop_back();
321
322 auto config_directory_contents = DirectoryContents();
323 config_directory_contents.AddFile("args.csv", csv);
324 realm_builder_.RouteReadOnlyDirectory("config-data", {kParentViewRef},
325 std::move(config_directory_contents));
326 }
327 realm_ = std::make_unique<RealmRoot>(realm_builder_.Build());
328
329 // Get the display information using the |fuchsia.ui.display.singleton.Info|.
330 std::optional<bool> display_metrics_obtained;
331 fuchsia::ui::display::singleton::InfoPtr display_info =
332 realm_->component().Connect<fuchsia::ui::display::singleton::Info>();
333 display_info->GetMetrics([this, &display_metrics_obtained](auto info) {
334 display_width_ = info.extent_in_px().width;
335 display_height_ = info.extent_in_px().height;
336 display_metrics_obtained = true;
337 });
338 RunLoopUntil([&display_metrics_obtained] {
339 return display_metrics_obtained.has_value();
340 });
341 FML_LOG(INFO) << "Got display_width " << display_width_ << " display_height "
342 << display_height_;
343
344 // Instruct Test UI Stack to present parent-view's View.
345 std::optional<zx_koid_t> view_ref_koid;
346 scene_provider_ =
347 realm_->component().Connect<fuchsia::ui::test::scene::Controller>();
348 scene_provider_.set_error_handler(
349 [](auto) { FML_LOG(ERROR) << "Error from test scene provider"; });
350 fuchsia::ui::test::scene::ControllerAttachClientViewRequest request;
351 request.set_view_provider(
352 realm_->component().Connect<fuchsia::ui::app::ViewProvider>());
353 scene_provider_->RegisterViewTreeWatcher(view_tree_watcher_.NewRequest(),
354 []() {});
355 scene_provider_->AttachClientView(
356 std::move(request), [&view_ref_koid](auto client_view_ref_koid) {
357 view_ref_koid = client_view_ref_koid;
358 });
359
360 FML_LOG(INFO) << "Waiting for client view ref koid";
361 RunLoopUntil([&view_ref_koid] { return view_ref_koid.has_value(); });
362
363 // Wait for the client view to get attached to the view tree.
364 std::optional<fuchsia::ui::observation::geometry::WatchResponse>
365 watch_response;
366 FML_LOG(INFO) << "Waiting for client view to render; koid is "
367 << (view_ref_koid.has_value() ? view_ref_koid.value() : 0);
368 RunLoopUntil([this, &watch_response, &view_ref_koid] {
369 return HasViewConnected(view_tree_watcher_, watch_response, *view_ref_koid);
370 });
371 FML_LOG(INFO) << "Client view has rendered";
372
373 screenshot_ =
374 realm_->component().Connect<fuchsia::ui::composition::Screenshot>();
375 FML_LOG(INFO) << "Launched parent-view";
376}
377
379 FML_LOG(INFO) << "Taking screenshot... ";
380
381 fuchsia::ui::composition::ScreenshotTakeRequest request;
382 request.set_format(fuchsia::ui::composition::ScreenshotFormat::BGRA_RAW);
383
384 std::optional<fuchsia::ui::composition::ScreenshotTakeResponse> response;
385 screenshot_->Take(std::move(request), [this, &response](auto screenshot) {
386 response = std::move(screenshot);
387 QuitLoop();
388 });
389
390 EXPECT_FALSE(RunLoopWithTimeout(kScreenshotTimeout))
391 << "Timed out waiting for screenshot.";
392 FML_LOG(INFO) << "Screenshot captured.";
393
395 response->vmo(), display_width_, display_height_, /*display_rotation*/ 0);
396}
397
400 fit::function<void(std::map<fuchsia_test_utils::Pixel, uint32_t>)> callback,
401 zx::duration timeout) {
402 return RunLoopWithTimeoutOrUntil(
403 [this, &callback, &color] {
404 auto screenshot = TakeScreenshot();
405 auto histogram = screenshot.Histogram();
406
407 bool color_found = histogram[color] > 0;
408 if (color_found && callback != nullptr) {
409 callback(std::move(histogram));
410 }
411 return color_found;
412 },
413 timeout);
414}
415
417 LaunchParentViewInRealm();
418
419 // Take screenshot until we see the child-view's embedded color.
420 ASSERT_TRUE(TakeScreenshotUntil(
421 kChildBackgroundColor,
422 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
423 // Expect parent and child background colors, with parent color > child
424 // color.
425 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
426 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
427 EXPECT_GT(histogram[kParentBackgroundColor],
428 histogram[kChildBackgroundColor]);
429 }));
430}
431
432TEST_F(FlutterEmbedderTest, EmbeddingWithOverlay) {
433 LaunchParentViewInRealm({"--showOverlay"});
434
435 // Take screenshot until we see the child-view's embedded color.
436 ASSERT_TRUE(TakeScreenshotUntil(
437 kChildBackgroundColor,
438 [](std::map<fuchsia_test_utils::Pixel, uint32_t> histogram) {
439 // Expect parent, overlay and child background colors.
440 // With parent color > child color and overlay color > child color.
441 const uint32_t overlay_pixel_count = OverlayPixelCount(histogram);
442 EXPECT_GT(histogram[kParentBackgroundColor], 0u);
443 EXPECT_GT(overlay_pixel_count, 0u);
444 EXPECT_GT(histogram[kChildBackgroundColor], 0u);
445 EXPECT_GT(histogram[kParentBackgroundColor],
446 histogram[kChildBackgroundColor]);
447 EXPECT_GT(overlay_pixel_count, histogram[kChildBackgroundColor]);
448 }));
449}
450
451} // namespace flutter_embedder_test
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={})
FlutterDesktopBinaryReply callback
#define FML_VLOG(verbose_level)
Definition logging.h:117
#define FML_LOG(severity)
Definition logging.h:101
TEST_F(FlutterEmbedderTest, Embedding)
bool CheckViewExistsInUpdates(const std::vector< fuchsia::ui::observation::geometry::ViewTreeSnapshot > &updates, zx_koid_t view_ref_koid)
Definition check_view.cc:27