Flutter Engine
The Flutter Engine
dart-jit-runner-integration-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 <dart/test/cpp/fidl.h>
6#include <fuchsia/tracing/provider/cpp/fidl.h>
7#include <lib/async-loop/testing/cpp/real_loop.h>
8#include <lib/sys/component/cpp/testing/realm_builder.h>
9#include <lib/sys/component/cpp/testing/realm_builder_types.h>
10
11#include "flutter/fml/logging.h"
12#include "gtest/gtest.h"
13
15namespace {
16
17// Types imported for the realm_builder library
18using component_testing::ChildOptions;
19using component_testing::ChildRef;
20using component_testing::Directory;
21using component_testing::ParentRef;
22using component_testing::Protocol;
23using component_testing::RealmBuilder;
24using component_testing::RealmRoot;
25using component_testing::Route;
26
27constexpr auto kDartRunnerEnvironment = "dart_runner_env";
28
29constexpr auto kDartJitRunner = "dart_jit_runner";
30constexpr auto kDartJitRunnerRef = ChildRef{kDartJitRunner};
31constexpr auto kDartJitRunnerUrl =
32 "fuchsia-pkg://fuchsia.com/oot_dart_jit_runner#meta/"
33 "dart_jit_runner.cm";
34
35constexpr auto kDartJitEchoServer = "dart_jit_echo_server";
36constexpr auto kDartJitEchoServerRef = ChildRef{kDartJitEchoServer};
37constexpr auto kDartJitEchoServerUrl =
38 "fuchsia-pkg://fuchsia.com/dart_jit_echo_server#meta/"
39 "dart_jit_echo_server.cm";
40
41class RealmBuilderTest : public ::loop_fixture::RealLoop,
42 public ::testing::Test {
43 public:
44 RealmBuilderTest() = default;
45};
46
47TEST_F(RealmBuilderTest, DartRunnerStartsUp) {
48 auto realm_builder = RealmBuilder::Create();
49 // Add Dart JIT runner as a child of RealmBuilder
50 realm_builder.AddChild(kDartJitRunner, kDartJitRunnerUrl);
51
52 // Add environment providing the Dart JIT runner
53 fuchsia::component::decl::Environment dart_runner_environment;
54 dart_runner_environment.set_name(kDartRunnerEnvironment);
55 dart_runner_environment.set_extends(
56 fuchsia::component::decl::EnvironmentExtends::REALM);
57 dart_runner_environment.set_runners({});
58 auto environment_runners = dart_runner_environment.mutable_runners();
59
60 fuchsia::component::decl::RunnerRegistration dart_jit_runner_reg;
61 dart_jit_runner_reg.set_source(fuchsia::component::decl::Ref::WithChild(
62 fuchsia::component::decl::ChildRef{.name = kDartJitRunner}));
63 dart_jit_runner_reg.set_source_name(kDartJitRunner);
64 dart_jit_runner_reg.set_target_name(kDartJitRunner);
65 environment_runners->push_back(std::move(dart_jit_runner_reg));
66 auto realm_decl = realm_builder.GetRealmDecl();
67 if (!realm_decl.has_environments()) {
68 realm_decl.set_environments({});
69 }
70 auto realm_environments = realm_decl.mutable_environments();
71 realm_environments->push_back(std::move(dart_runner_environment));
72 realm_builder.ReplaceRealmDecl(std::move(realm_decl));
73
74 // Add Dart server component as a child of Realm Builder
75 realm_builder.AddChild(kDartJitEchoServer, kDartJitEchoServerUrl,
76 ChildOptions{.environment = kDartRunnerEnvironment});
77
78 // Route base capabilities to the Dart JIT runner
79 realm_builder.AddRoute(
80 Route{.capabilities = {Protocol{"fuchsia.logger.LogSink"},
81 Protocol{"fuchsia.tracing.provider.Registry"},
82 Protocol{"fuchsia.posix.socket.Provider"},
83 Protocol{"fuchsia.intl.PropertyProvider"},
84 Protocol{"fuchsia.inspect.InspectSink"},
85 Directory{"config-data"}},
86 .source = ParentRef(),
87 .targets = {kDartJitRunnerRef, kDartJitEchoServerRef}});
88
89 // Route the Echo FIDL protocol, this allows the Dart echo server to
90 // communicate with the Realm Builder
91 realm_builder.AddRoute(Route{.capabilities = {Protocol{"dart.test.Echo"}},
92 .source = kDartJitEchoServerRef,
93 .targets = {ParentRef()}});
94
95 // Build the Realm with the provided child and protocols
96 auto realm = realm_builder.Build(dispatcher());
97 FML_LOG(INFO) << "Realm built: " << realm.component().GetChildName();
98 // Connect to the Dart echo server
99 auto echo = realm.component().ConnectSync<dart::test::Echo>();
100 fidl::StringPtr response;
101 // Attempt to ping the Dart echo server for a response
102 echo->EchoString("hello", &response);
103 ASSERT_EQ(response, "hello");
104}
105
106} // namespace
107} // namespace dart_jit_runner_testing::testing
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
#define FML_LOG(severity)
Definition: logging.h:82
static void Echo(Thread *thread, JSONStream *js)
Definition: service.cc:1785
TEST_F(DisplayListTest, Defaults)