Flutter Engine
 
Loading...
Searching...
No Matches
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 void handle_unknown_method(uint64_t ordinal,
69 bool method_has_response) override;
70
71 private:
72 /// Helper for actually running the Dart main. Returns a promise.
73 fpromise::promise<> RunDartMain();
74
75 /// Creates and binds the namespace for this component. Returns true if
76 /// successful, false otherwise.
77 bool CreateAndBindNamespace();
78
79 bool SetUpFromKernel();
80 bool SetUpFromAppSnapshot();
81
82 bool CreateIsolate(const uint8_t* isolate_snapshot_data,
83 const uint8_t* isolate_snapshot_instructions);
84
85 // |ComponentController|
86 void Kill() override;
87 void Stop() override;
88
89 // Idle notification.
90 void MessageEpilogue(Dart_Handle result);
91 void OnIdleTimer(async_dispatcher_t* dispatcher,
92 async::WaitBase* wait,
93 zx_status_t status,
94 const zx_packet_signal* signal);
95
96 // |CaseIterator|
97 class CaseIterator final : public fuchsia::test::CaseIterator {
98 public:
99 CaseIterator(fidl::InterfaceRequest<fuchsia::test::CaseIterator> request,
100 async_dispatcher_t* dispatcher,
101 std::string test_component_name,
102 fit::function<void(CaseIterator*)> done_callback);
103
104 void GetNext(GetNextCallback callback) override;
105
106 private:
107 bool first_case_ = true;
108 fidl::Binding<fuchsia::test::CaseIterator> binding_;
109 std::string test_component_name_;
110 fit::function<void(CaseIterator*)> done_callback_;
111 };
112
113 std::unique_ptr<CaseIterator> RemoveCaseInterator(CaseIterator*);
114
115 // We only need one case_listener currently as dart tests are run as one
116 // large test file. In future iterations, case_listeners must be
117 // created per test case.
118 fidl::InterfacePtr<fuchsia::test::CaseListener> case_listener_;
119 std::map<CaseIterator*, std::unique_ptr<CaseIterator>> case_iterators_;
120
121 // |Suite|
122
123 /// Exposes suite protocol on behalf of test component.
124 std::string test_component_name_;
125 std::unique_ptr<sys::ComponentContext> suite_context_;
126 fidl::BindingSet<fuchsia::test::Suite> suite_bindings_;
127
128 // The loop must be the first declared member so that it gets destroyed after
129 // binding_ which expects the existence of a loop.
130 std::unique_ptr<async::Loop> loop_;
131 async::Executor executor_;
132
133 std::string label_;
134 std::string url_;
135 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
136 std::string data_path_;
137 std::unique_ptr<sys::ComponentContext> context_;
138
139 fuchsia::component::runner::ComponentStartInfo start_info_;
140 fidl::Binding<fuchsia::component::runner::ComponentController> binding_;
141 DoneCallback done_callback_;
142
143 zx::socket out_, err_, out_client_, err_client_;
144 fdio_ns_t* namespace_ = nullptr;
145 int stdout_fd_ = -1;
146 int stderr_fd_ = -1;
147
148 dart_utils::ElfSnapshot elf_snapshot_; // AOT snapshot
149 dart_utils::MappedResource isolate_snapshot_data_; // JIT snapshot
150 dart_utils::MappedResource isolate_snapshot_instructions_; // JIT snapshot
151 std::vector<dart_utils::MappedResource> kernel_peices_;
152
153 Dart_Isolate isolate_;
154 int32_t return_code_ = 0;
155
156 zx::time idle_start_{0};
157 zx::timer idle_timer_;
158 async::WaitMethod<DartTestComponentController,
159 &DartTestComponentController::OnIdleTimer>
160 idle_wait_{this};
161
162 // Disallow copy and assignment.
163 DartTestComponentController(const DartTestComponentController&) = delete;
164 DartTestComponentController& operator=(const DartTestComponentController&) =
165 delete;
166};
167
168} // namespace dart_runner
169
170#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_TEST_COMPONENT_CONTROLLER_H_
void Run(std::vector< fuchsia::test::Invocation > tests, fuchsia::test::RunOptions options, fidl::InterfaceHandle< fuchsia::test::RunListener > listener) override
|Suite| protocol implementation.
void handle_unknown_method(uint64_t ordinal, bool method_has_response) override
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)
FlutterDesktopBinaryReply callback
fidl::Binding< fuchsia::ui::composition::ChildViewWatcher > binding_