Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cccaae2

Browse files
authored
[fuchsia] Replace deprecated AddLocalChild (#38788)
`AddLocalChild(LocalComponent*)` is deprecated. It was replaced by: `AddLocalChild(LocalComponentFactory)`, which is a type alias for a lambda that returns a `std::unique_ptr<LocalComponentImpl>`. This change addresses problems that arised due to object lifetime management of the components, and allows RealmBuilder to model the component lifecycle of local components in a way that's more consistent with other components. The RealmBuilder-built `Realm` now owns the lifetime of the local components, instead of the client, and those objects are valid until the `Realm` is destroyed. Bug: fxbug.dev/109804
1 parent 87ead94 commit cccaae2

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

shell/platform/fuchsia/flutter/tests/integration/mouse-input/mouse-input-test.cc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ namespace {
4949
// Types imported for the realm_builder library.
5050
using component_testing::ChildRef;
5151
using component_testing::ConfigValue;
52-
using component_testing::LocalComponent;
53-
using component_testing::LocalComponentHandles;
52+
using component_testing::LocalComponentImpl;
5453
using component_testing::ParentRef;
5554
using component_testing::Protocol;
5655
using component_testing::Realm;
@@ -109,7 +108,7 @@ int ButtonsToInt(
109108
// us know what position and button press state the mouse cursor has.
110109
class MouseInputListenerServer
111110
: public fuchsia::ui::test::input::MouseInputListener,
112-
public LocalComponent {
111+
public LocalComponentImpl {
113112
public:
114113
explicit MouseInputListenerServer(async_dispatcher_t* dispatcher)
115114
: dispatcher_(dispatcher) {}
@@ -121,19 +120,18 @@ class MouseInputListenerServer
121120
events_.push(std::move(request));
122121
}
123122

124-
// |MockComponent::Start|
123+
// |MockComponent::OnStart|
125124
// When the component framework requests for this component to start, this
126125
// method will be invoked by the realm_builder library.
127-
void Start(std::unique_ptr<LocalComponentHandles> mock_handles) override {
126+
void OnStart() override {
128127
FML_LOG(INFO) << "Starting MouseInputServer";
129-
ASSERT_EQ(ZX_OK, mock_handles->outgoing()->AddPublicService(
128+
ASSERT_EQ(ZX_OK, outgoing()->AddPublicService(
130129
fidl::InterfaceRequestHandler<
131130
fuchsia::ui::test::input::MouseInputListener>(
132131
[this](auto request) {
133132
bindings_.AddBinding(this, std::move(request),
134133
dispatcher_);
135134
})));
136-
mock_handles_.emplace_back(std::move(mock_handles));
137135
}
138136

139137
size_t SizeOfEvents() const { return events_.size(); }
@@ -156,7 +154,6 @@ class MouseInputListenerServer
156154
// Not owned.
157155
async_dispatcher_t* dispatcher_ = nullptr;
158156
fidl::BindingSet<fuchsia::ui::test::input::MouseInputListener> bindings_;
159-
std::vector<std::unique_ptr<LocalComponentHandles>> mock_handles_;
160157
std::queue<
161158
fuchsia::ui::test::input::MouseInputListenerReportMouseInputRequest>
162159
events_;
@@ -191,7 +188,7 @@ class MouseInputTest : public PortableUITest,
191188
}
192189

193190
MouseInputListenerServer* mouse_input_listener() {
194-
return mouse_input_listener_.get();
191+
return mouse_input_listener_;
195192
}
196193

197194
// Helper method for checking the test.mouse.MouseInputListener response from
@@ -254,13 +251,17 @@ class MouseInputTest : public PortableUITest,
254251
private:
255252
void ExtendRealm() override {
256253
FML_LOG(INFO) << "Extending realm";
257-
mouse_input_listener_ =
258-
std::make_unique<MouseInputListenerServer>(dispatcher());
259254

260255
// Key part of service setup: have this test component vend the
261256
// |MouseInputListener| service in the constructed realm.
262-
realm_builder()->AddLocalChild(kMouseInputListener,
263-
mouse_input_listener_.get());
257+
auto mouse_input_listener =
258+
std::make_unique<MouseInputListenerServer>(dispatcher());
259+
mouse_input_listener_ = mouse_input_listener.get();
260+
realm_builder()->AddLocalChild(
261+
kMouseInputListener,
262+
[mouse_input_listener = std::move(mouse_input_listener)]() mutable {
263+
return std::move(mouse_input_listener);
264+
});
264265

265266
realm_builder()->AddChild(kMouseInputView, kMouseInputViewUrl,
266267
component_testing::ChildOptions{
@@ -281,7 +282,7 @@ class MouseInputTest : public PortableUITest,
281282

282283
ParamType GetTestUIStackUrl() override { return GetParam(); };
283284

284-
std::unique_ptr<MouseInputListenerServer> mouse_input_listener_;
285+
MouseInputListenerServer* mouse_input_listener_;
285286

286287
fuchsia::ui::scenic::ScenicPtr scenic_;
287288
uint32_t display_width_ = 0;

shell/platform/fuchsia/flutter/tests/integration/text-input/text-input-test.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ namespace {
4141

4242
// Types imported for the realm_builder library.
4343
using component_testing::ChildRef;
44-
using component_testing::LocalComponent;
45-
using component_testing::LocalComponentHandles;
44+
using component_testing::LocalComponentImpl;
4645
using component_testing::ParentRef;
4746
using component_testing::Protocol;
4847
using component_testing::RealmBuilder;
@@ -76,7 +75,7 @@ constexpr auto kTestUIStackUrl =
7675
/// additions of characters, not just the end result.
7776
class KeyboardInputListenerServer
7877
: public fuchsia::ui::test::input::KeyboardInputListener,
79-
public LocalComponent {
78+
public LocalComponentImpl {
8079
public:
8180
explicit KeyboardInputListenerServer(async_dispatcher_t* dispatcher)
8281
: dispatcher_(dispatcher) {}
@@ -90,10 +89,9 @@ class KeyboardInputListenerServer
9089
}
9190

9291
/// Starts this server.
93-
void Start(std::unique_ptr<LocalComponentHandles> local_handles) override {
92+
void OnStart() override {
9493
FML_LOG(INFO) << "Starting KeyboardInputListenerServer";
95-
local_handles_ = std::move(local_handles);
96-
ASSERT_EQ(ZX_OK, local_handles_->outgoing()->AddPublicService(
94+
ASSERT_EQ(ZX_OK, outgoing()->AddPublicService(
9795
bindings_.GetHandler(this, dispatcher_)));
9896
}
9997

@@ -124,7 +122,6 @@ class KeyboardInputListenerServer
124122

125123
private:
126124
async_dispatcher_t* dispatcher_ = nullptr;
127-
std::unique_ptr<LocalComponentHandles> local_handles_;
128125
fidl::BindingSet<fuchsia::ui::test::input::KeyboardInputListener> bindings_;
129126
std::vector<std::string> response_list_;
130127
bool ready_ = false;
@@ -169,18 +166,22 @@ class TextInputTest : public PortableUITest,
169166

170167
std::string GetTestUIStackUrl() override { return GetParam(); };
171168

172-
std::unique_ptr<KeyboardInputListenerServer> keyboard_input_listener_server_;
169+
KeyboardInputListenerServer* keyboard_input_listener_server_;
173170

174171
private:
175172
void ExtendRealm() override {
176173
FML_LOG(INFO) << "Extending realm";
177174
// Key part of service setup: have this test component vend the
178175
// |KeyboardInputListener| service in the constructed realm.
179-
keyboard_input_listener_server_ =
176+
auto keyboard_input_listener_server =
180177
std::make_unique<KeyboardInputListenerServer>(dispatcher());
181-
182-
realm_builder()->AddLocalChild(kKeyboardInputListener,
183-
keyboard_input_listener_server_.get());
178+
keyboard_input_listener_server_ = keyboard_input_listener_server.get();
179+
realm_builder()->AddLocalChild(
180+
kKeyboardInputListener,
181+
[keyboard_input_listener_server =
182+
std::move(keyboard_input_listener_server)]() mutable {
183+
return std::move(keyboard_input_listener_server);
184+
});
184185

185186
// Add text-input-view to the Realm
186187
realm_builder()->AddChild(kTextInputView, kTextInputViewUrl,

shell/platform/fuchsia/flutter/tests/integration/touch-input/touch-input-test.cc

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ namespace {
113113
using component_testing::ChildRef;
114114
using component_testing::ConfigValue;
115115
using component_testing::DirectoryContents;
116-
using component_testing::LocalComponent;
117-
using component_testing::LocalComponentHandles;
116+
using component_testing::LocalComponentImpl;
118117
using component_testing::ParentRef;
119118
using component_testing::Protocol;
120119
using component_testing::Realm;
@@ -151,17 +150,17 @@ bool CompareDouble(double f0, double f1, double epsilon) {
151150
}
152151

153152
// This component implements the TouchInput protocol
154-
// and the interface for a RealmBuilder LocalComponent. A LocalComponent
153+
// and the interface for a RealmBuilder LocalComponentImpl. A LocalComponentImpl
155154
// is a component that is implemented here in the test, as opposed to
156155
// elsewhere in the system. When it's inserted to the realm, it will act
157156
// like a proper component. This is accomplished, in part, because the
158157
// realm_builder library creates the necessary plumbing. It creates a manifest
159158
// for the component and routes all capabilities to and from it.
160-
// LocalComponent:
159+
// LocalComponentImpl:
161160
// https://fuchsia.dev/fuchsia-src/development/testing/components/realm_builder#mock-components
162161
class TouchInputListenerServer
163162
: public fuchsia::ui::test::input::TouchInputListener,
164-
public LocalComponent {
163+
public LocalComponentImpl {
165164
public:
166165
explicit TouchInputListenerServer(async_dispatcher_t* dispatcher)
167166
: dispatcher_(dispatcher) {}
@@ -174,21 +173,20 @@ class TouchInputListenerServer
174173
events_received_.push_back(std::move(request));
175174
}
176175

177-
// |LocalComponent::Start|
176+
// |LocalComponentImpl::OnStart|
178177
// When the component framework requests for this component to start, this
179178
// method will be invoked by the realm_builder library.
180-
void Start(std::unique_ptr<LocalComponentHandles> local_handles) override {
179+
void OnStart() override {
181180
FML_LOG(INFO) << "Starting TouchInputListenerServer";
182181
// When this component starts, add a binding to the
183182
// protocol to this component's outgoing directory.
184-
ASSERT_EQ(ZX_OK, local_handles->outgoing()->AddPublicService(
183+
ASSERT_EQ(ZX_OK, outgoing()->AddPublicService(
185184
fidl::InterfaceRequestHandler<
186185
fuchsia::ui::test::input::TouchInputListener>(
187186
[this](auto request) {
188187
bindings_.AddBinding(this, std::move(request),
189188
dispatcher_);
190189
})));
191-
local_handles_.emplace_back(std::move(local_handles));
192190
}
193191

194192
const std::vector<
@@ -199,7 +197,6 @@ class TouchInputListenerServer
199197

200198
private:
201199
async_dispatcher_t* dispatcher_ = nullptr;
202-
std::vector<std::unique_ptr<LocalComponentHandles>> local_handles_;
203200
fidl::BindingSet<fuchsia::ui::test::input::TouchInputListener> bindings_;
204201
std::vector<
205202
fuchsia::ui::test::input::TouchInputListenerReportTouchInputRequest>
@@ -290,7 +287,7 @@ class FlutterTapTestBase : public PortableUITest,
290287

291288
ParamType GetTestUIStackUrl() override { return GetParam(); };
292289

293-
std::unique_ptr<TouchInputListenerServer> touch_input_listener_server_;
290+
TouchInputListenerServer* touch_input_listener_server_;
294291
};
295292

296293
class FlutterTapTest : public FlutterTapTestBase {
@@ -299,10 +296,14 @@ class FlutterTapTest : public FlutterTapTestBase {
299296
FML_LOG(INFO) << "Extending realm";
300297
// Key part of service setup: have this test component vend the
301298
// |TouchInputListener| service in the constructed realm.
302-
touch_input_listener_server_ =
299+
auto touch_input_listener_server =
303300
std::make_unique<TouchInputListenerServer>(dispatcher());
304-
realm_builder()->AddLocalChild(kMockTouchInputListener,
305-
touch_input_listener_server_.get());
301+
touch_input_listener_server_ = touch_input_listener_server.get();
302+
realm_builder()->AddLocalChild(
303+
kMockTouchInputListener, [touch_input_listener_server = std::move(
304+
touch_input_listener_server)]() mutable {
305+
return std::move(touch_input_listener_server);
306+
});
306307

307308
// Add touch-input-view to the Realm
308309
realm_builder()->AddChild(kTouchInputView, kTouchInputViewUrl,
@@ -381,10 +382,14 @@ class FlutterEmbedTapTest : public FlutterTapTestBase {
381382
FML_LOG(INFO) << "Extending realm";
382383
// Key part of service setup: have this test component vend the
383384
// |TouchInputListener| service in the constructed realm.
384-
touch_input_listener_server_ =
385+
auto touch_input_listener_server =
385386
std::make_unique<TouchInputListenerServer>(dispatcher());
386-
realm_builder()->AddLocalChild(kMockTouchInputListener,
387-
touch_input_listener_server_.get());
387+
touch_input_listener_server_ = touch_input_listener_server.get();
388+
realm_builder()->AddLocalChild(
389+
kMockTouchInputListener, [touch_input_listener_server = std::move(
390+
touch_input_listener_server)]() mutable {
391+
return std::move(touch_input_listener_server);
392+
});
388393

389394
// Add touch-input-view to the Realm
390395
realm_builder()->AddChild(kTouchInputView, kTouchInputViewUrl,

0 commit comments

Comments
 (0)