Flutter Engine
The Flutter Engine
dart_test_component_controller.h
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#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_TEST_COMPONENT_CONTROLLER_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_TEST_COMPONENT_CONTROLLER_H_
7
8#include <memory>
9
10#include <fuchsia/component/runner/cpp/fidl.h>
11#include <fuchsia/test/cpp/fidl.h>
12#include <lib/async-loop/cpp/loop.h>
13#include <lib/async/cpp/executor.h>
14#include <lib/async/cpp/wait.h>
15#include <lib/fdio/namespace.h>
16#include <lib/sys/cpp/component_context.h>
17#include <lib/sys/cpp/service_directory.h>
18#include <lib/zx/timer.h>
19
20#include <lib/fidl/cpp/binding_set.h>
21#include "lib/fidl/cpp/binding.h"
23#include "third_party/dart/runtime/include/dart_api.h"
24
25namespace dart_runner {
26
27/// Starts a Dart test component written in CFv2. It's different from
28/// DartComponentController in that it must implement the
29/// |fuchsia.test.Suite| protocol. It was forked to avoid a naming clash
30/// between the two classes' methods as the Suite protocol requires a Run()
31/// method for the test_manager to call on. This way, we avoid an extra layer
32/// between the test_manager and actual test execution.
33/// TODO(fxb/98369): Look into combining the two component classes once dart
34/// testing is stable.
36 : public fuchsia::component::runner::ComponentController,
37 public fuchsia::test::Suite {
38 using DoneCallback = fit::function<void(DartTestComponentController*)>;
39
40 public:
42 fuchsia::component::runner::ComponentStartInfo start_info,
43 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
44 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
45 controller,
46 DoneCallback done_callback);
47
49
50 /// Sets up the controller.
51 ///
52 /// This should be called before |Run|.
53 void SetUp();
54
55 /// |Suite| protocol implementation.
56 void GetTests(
57 fidl::InterfaceRequest<fuchsia::test::CaseIterator> iterator) override;
58
59 /// |Suite| protocol implementation.
60 void Run(std::vector<fuchsia::test::Invocation> tests,
61 fuchsia::test::RunOptions options,
62 fidl::InterfaceHandle<fuchsia::test::RunListener> listener) override;
63
64 fidl::InterfaceRequestHandler<fuchsia::test::Suite> GetHandler() {
65 return suite_bindings_.GetHandler(this, loop_->dispatcher());
66 }
67
68 private:
69 /// Helper for actually running the Dart main. Returns a promise.
70 fpromise::promise<> RunDartMain();
71
72 /// Creates and binds the namespace for this component. Returns true if
73 /// successful, false otherwise.
74 bool CreateAndBindNamespace();
75
76 bool SetUpFromKernel();
77 bool SetUpFromAppSnapshot();
78
79 bool CreateIsolate(const uint8_t* isolate_snapshot_data,
80 const uint8_t* isolate_snapshot_instructions);
81
82 // |ComponentController|
83 void Kill() override;
84 void Stop() override;
85
86 // Idle notification.
87 void MessageEpilogue(Dart_Handle result);
88 void OnIdleTimer(async_dispatcher_t* dispatcher,
89 async::WaitBase* wait,
90 zx_status_t status,
91 const zx_packet_signal* signal);
92
93 // |CaseIterator|
94 class CaseIterator final : public fuchsia::test::CaseIterator {
95 public:
96 CaseIterator(fidl::InterfaceRequest<fuchsia::test::CaseIterator> request,
97 async_dispatcher_t* dispatcher,
98 std::string test_component_name,
99 fit::function<void(CaseIterator*)> done_callback);
100
101 void GetNext(GetNextCallback callback) override;
102
103 private:
104 bool first_case_ = true;
105 fidl::Binding<fuchsia::test::CaseIterator> binding_;
106 std::string test_component_name_;
107 fit::function<void(CaseIterator*)> done_callback_;
108 };
109
110 std::unique_ptr<CaseIterator> RemoveCaseInterator(CaseIterator*);
111
112 // We only need one case_listener currently as dart tests are run as one
113 // large test file. In future iterations, case_listeners must be
114 // created per test case.
115 fidl::InterfacePtr<fuchsia::test::CaseListener> case_listener_;
116 std::map<CaseIterator*, std::unique_ptr<CaseIterator>> case_iterators_;
117
118 // |Suite|
119
120 /// Exposes suite protocol on behalf of test component.
121 std::string test_component_name_;
122 std::unique_ptr<sys::ComponentContext> suite_context_;
123 fidl::BindingSet<fuchsia::test::Suite> suite_bindings_;
124
125 // The loop must be the first declared member so that it gets destroyed after
126 // binding_ which expects the existence of a loop.
127 std::unique_ptr<async::Loop> loop_;
128 async::Executor executor_;
129
130 std::string label_;
131 std::string url_;
132 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
133 std::string data_path_;
134 std::unique_ptr<sys::ComponentContext> context_;
135
136 fuchsia::component::runner::ComponentStartInfo start_info_;
137 fidl::Binding<fuchsia::component::runner::ComponentController> binding_;
138 DoneCallback done_callback_;
139
140 zx::socket out_, err_, out_client_, err_client_;
141 fdio_ns_t* namespace_ = nullptr;
142 int stdout_fd_ = -1;
143 int stderr_fd_ = -1;
144
145 dart_utils::ElfSnapshot elf_snapshot_; // AOT snapshot
146 dart_utils::MappedResource isolate_snapshot_data_; // JIT snapshot
147 dart_utils::MappedResource isolate_snapshot_instructions_; // JIT snapshot
148 std::vector<dart_utils::MappedResource> kernel_peices_;
149
150 Dart_Isolate isolate_;
151 int32_t return_code_ = 0;
152
153 zx::time idle_start_{0};
154 zx::timer idle_timer_;
155 async::WaitMethod<DartTestComponentController,
156 &DartTestComponentController::OnIdleTimer>
157 idle_wait_{this};
158
159 // Disallow copy and assignment.
162 delete;
163};
164
165} // namespace dart_runner
166
167#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_TEST_COMPONENT_CONTROLLER_H_
static BlurTest tests[]
Definition: BlurTest.cpp:84
const char * options
void Run(std::vector< fuchsia::test::Invocation > tests, fuchsia::test::RunOptions options, fidl::InterfaceHandle< fuchsia::test::RunListener > listener) override
|Suite| protocol implementation.
fidl::InterfaceRequestHandler< fuchsia::test::Suite > GetHandler()
void GetTests(fidl::InterfaceRequest< fuchsia::test::CaseIterator > iterator) override
|Suite| protocol implementation.
DartTestComponentController(fuchsia::component::runner::ComponentStartInfo start_info, std::shared_ptr< sys::ServiceDirectory > runner_incoming_services, fidl::InterfaceRequest< fuchsia::component::runner::ComponentController > controller, DoneCallback done_callback)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
Dart_NativeFunction function
Definition: fuchsia.cc:51
const uint8_t * isolate_snapshot_data
Definition: gen_snapshot.cc:69
const uint8_t * isolate_snapshot_instructions
Definition: gen_snapshot.cc:70
static double time(int loops, Benchmark *bench, Target *target)
Definition: nanobench.cpp:394
fidl::Binding< fuchsia::ui::composition::ChildViewWatcher > binding_