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

Commit 3dfee51

Browse files
committed
test migration
1 parent 9467dfc commit 3dfee51

8 files changed

Lines changed: 68 additions & 99 deletions

File tree

testing/dart/channel_buffers_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ void main() {
3131
called = true;
3232
}
3333
buffers.push(channel, data, callback);
34+
// Ignoring the deprecated member use because we're specifically testing
35+
// deprecated API.
36+
// ignore: deprecated_member_use
3437
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
3538
expect(drainedData, equals(data));
3639
assert(!called);
@@ -52,6 +55,9 @@ void main() {
5255

5356
// Ignoring the returned future because the completion of the drain is
5457
// communicated using the `completer`.
58+
// Ignoring the deprecated member use because we're specifically testing
59+
// deprecated API.
60+
// ignore: deprecated_member_use
5561
buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
5662
log.add('callback');
5763
completer.complete();
@@ -77,6 +83,9 @@ void main() {
7783
_resize(buffers, channel, 0);
7884
buffers.push(channel, data, callback);
7985
bool didCall = false;
86+
// Ignoring the deprecated member use because we're specifically testing
87+
// deprecated API.
88+
// ignore: deprecated_member_use
8089
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
8190
didCall = true;
8291
});
@@ -87,6 +96,9 @@ void main() {
8796
const String channel = 'foo';
8897
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
8998
bool didCall = false;
99+
// Ignoring the deprecated member use because we're specifically testing
100+
// deprecated API.
101+
// ignore: deprecated_member_use
90102
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
91103
didCall = true;
92104
});
@@ -107,6 +119,9 @@ void main() {
107119
buffers.push(channel, three, callback);
108120
buffers.push(channel, four, callback);
109121
int counter = 0;
122+
// Ignoring the deprecated member use because we're specifically testing
123+
// deprecated API.
124+
// ignore: deprecated_member_use
110125
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
111126
switch (counter) {
112127
case 0:
@@ -132,6 +147,9 @@ void main() {
132147
buffers.push(channel, two, callback);
133148
_resize(buffers, channel, 1);
134149
int counter = 0;
150+
// Ignoring the deprecated member use because we're specifically testing
151+
// deprecated API.
152+
// ignore: deprecated_member_use
135153
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
136154
switch (counter) {
137155
case 0:

testing/dart/observatory/vmservice_methods_test.dart

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ void main() {
7474
fail('This test must not be run with --disable-vm-service.');
7575
}
7676

77-
final Completer<PlatformResponse> completer = Completer<PlatformResponse>();
78-
ui.PlatformDispatcher.instance.onPlatformMessage = (String name, ByteData? data, ui.PlatformMessageResponseCallback? callback) {
79-
final ByteBuffer buffer = data!.buffer;
80-
final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
81-
completer.complete(PlatformResponse(name: name, contents: utf8.decode(list)));
82-
};
77+
final Completer<String> completer = Completer<String>();
78+
ui.channelBuffers.setListener(
79+
'flutter/system',
80+
(ByteData? data, ui.PlatformMessageResponseCallback callback) {
81+
final ByteBuffer buffer = data!.buffer;
82+
final Uint8List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
83+
completer.complete(utf8.decode(list));
84+
},
85+
);
8386

8487
vmService = await vmServiceConnectUri(
8588
'ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws',
@@ -94,13 +97,11 @@ void main() {
9497
expect(fontChangeResponse.type, 'Success');
9598
expect(
9699
await completer.future,
97-
const PlatformResponse(
98-
name: 'flutter/system',
99-
contents: '{"type":"fontsChange"}',
100-
),
100+
'{"type":"fontsChange"}',
101101
);
102102
} finally {
103103
await vmService?.dispose();
104+
ui.channelBuffers.clearListener('flutter/system');
104105
}
105106
});
106107
}
@@ -121,22 +122,3 @@ Future<String?> getIsolateId(vms.VmService vmService) async {
121122
}
122123
return null;
123124
}
124-
125-
class PlatformResponse {
126-
const PlatformResponse({
127-
required this.name,
128-
required this.contents,
129-
});
130-
131-
final String name;
132-
final String contents;
133-
134-
@override
135-
bool operator ==(Object other) =>
136-
other is PlatformResponse &&
137-
other.name == name &&
138-
other.contents == contents;
139-
140-
@override
141-
int get hashCode => Object.hash(name, contents);
142-
}

testing/dart/text_test.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,19 @@ void testTextRange() {
195195

196196
void testLoadFontFromList() {
197197
test('loadFontFromList will send platform message after font is loaded', () async {
198-
final PlatformMessageCallback? oldHandler = PlatformDispatcher.instance.onPlatformMessage;
199-
late String actualName;
200198
late String message;
201-
PlatformDispatcher.instance.onPlatformMessage = (String name, ByteData? data, PlatformMessageResponseCallback? callback) {
202-
assert(data != null);
203-
actualName = name;
204-
final Uint8List list = data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
205-
message = utf8.decode(list);
206-
};
199+
channelBuffers.setListener(
200+
'flutter/system',
201+
(ByteData? data, PlatformMessageResponseCallback? callback) {
202+
assert(data != null);
203+
final Uint8List list = data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
204+
message = utf8.decode(list);
205+
},
206+
);
207207
final Uint8List fontData = Uint8List(0);
208208
await loadFontFromList(fontData, fontFamily: 'fake');
209-
PlatformDispatcher.instance.onPlatformMessage = oldHandler;
210-
expect(actualName, 'flutter/system');
211209
expect(message, '{"type":"fontsChange"}');
210+
channelBuffers.clearListener('flutter/system');
212211
});
213212
}
214213

testing/scenario_app/lib/main.dart

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ void main() {
1717
// FlutterView to the _view property.
1818
assert(PlatformDispatcher.instance.implicitView != null);
1919
PlatformDispatcher.instance
20-
..onPlatformMessage = _handlePlatformMessage
2120
..onBeginFrame = _onBeginFrame
2221
..onDrawFrame = _onDrawFrame
2322
..onMetricsChanged = _onMetricsChanged
2423
..onPointerDataPacket = _onPointerDataPacket
2524
..scheduleFrame();
25+
channelBuffers.setListener('drive', _handleDriverMessage);
26+
channelBuffers.setListener('write_timeline', _handleWriteTimelineMessage);
2627

2728
final FlutterView view = PlatformDispatcher.instance.implicitView!;
2829
// Asserting that this is greater than zero since this app runs on different
@@ -41,7 +42,8 @@ void main() {
4142
/// The FlutterView into which the [Scenario]s will be rendered.
4243
FlutterView get _view => PlatformDispatcher.instance.implicitView!;
4344

44-
void _handleDriverMessage(Map<String, dynamic> call) {
45+
void _handleDriverMessage(ByteData? data, PlatformMessageResponseCallback? callback) {
46+
final Map<String, dynamic> call = json.decode(utf8.decode(data!.buffer.asUint8List())) as Map<String, dynamic>;
4547
final String? methodName = call['method'] as String?;
4648
switch (methodName) {
4749
case 'set_scenario':
@@ -52,23 +54,9 @@ void _handleDriverMessage(Map<String, dynamic> call) {
5254
}
5355
}
5456

55-
Future<void> _handlePlatformMessage(
56-
String name, ByteData? data, PlatformMessageResponseCallback? callback) async {
57-
if (data != null) {
58-
print('$name = ${utf8.decode(data.buffer.asUint8List())}');
59-
} else {
60-
print(name);
61-
}
62-
63-
switch (name) {
64-
case 'driver':
65-
_handleDriverMessage(json.decode(utf8.decode(data!.buffer.asUint8List())) as Map<String, dynamic>);
66-
case 'write_timeline':
67-
final String timelineData = await _getTimelineData();
68-
callback!(Uint8List.fromList(utf8.encode(timelineData)).buffer.asByteData());
69-
default:
70-
currentScenario?.onPlatformMessage(name, data, callback);
71-
}
57+
Future<void> _handleWriteTimelineMessage(ByteData? data, PlatformMessageResponseCallback? callback) async {
58+
final String timelineData = await _getTimelineData();
59+
callback!(Uint8List.fromList(utf8.encode(timelineData)).buffer.asByteData());
7260
}
7361

7462
Future<String> _getTimelineData() async {

testing/scenario_app/lib/src/platform_echo_mixin.dart

Lines changed: 0 additions & 21 deletions
This file was deleted.

testing/scenario_app/lib/src/platform_view.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario
422422
required this.secondId,
423423
}) {
424424
_nextFrame = _firstFrame;
425+
channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage);
425426
}
426427

427428
/// The platform view identifier to use for the first platform view.
@@ -504,15 +505,10 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario
504505

505506
String _lastLifecycleState = '';
506507

507-
@override
508-
void onPlatformMessage(
509-
String name,
508+
void _onPlatformMessage(
510509
ByteData? data,
511510
PlatformMessageResponseCallback? callback,
512511
) {
513-
if (name != 'flutter/lifecycle') {
514-
return;
515-
}
516512
final String message = utf8.decode(data!.buffer.asUint8List());
517513
if (_lastLifecycleState == 'AppLifecycleState.inactive' &&
518514
message == 'AppLifecycleState.resumed') {
@@ -522,6 +518,12 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario
522518

523519
_lastLifecycleState = message;
524520
}
521+
522+
@override
523+
void unmount() {
524+
channelBuffers.clearListener('flutter/lifecycle');
525+
super.unmount();
526+
}
525527
}
526528

527529
/// Platform view with clip rect.

testing/scenario_app/lib/src/poppable_screen.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:typed_data';
56
import 'dart:ui';
67

78
import 'channel_util.dart';
89

9-
import 'platform_echo_mixin.dart';
1010
import 'scenario.dart';
1111

1212
/// A blank page with a button that pops the page when tapped.
13-
class PoppableScreenScenario extends Scenario with PlatformEchoMixin {
13+
class PoppableScreenScenario extends Scenario {
1414
/// Creates the PoppableScreenScenario.
15-
PoppableScreenScenario(super.view);
15+
PoppableScreenScenario(super.view) {
16+
channelBuffers.setListener('flutter/platform', _onHandlePlatformMessage);
17+
}
1618

1719
// Rect for the pop button. Only defined once onMetricsChanged is called.
1820
Rect? _buttonRect;
@@ -79,4 +81,14 @@ class PoppableScreenScenario extends Scenario with PlatformEchoMixin {
7981
// will fail.
8082
);
8183
}
84+
85+
void _onHandlePlatformMessage(ByteData? data, PlatformMessageResponseCallback callback) {
86+
view.platformDispatcher.sendPlatformMessage('flutter/platform', data, null);
87+
}
88+
89+
@override
90+
void unmount() {
91+
channelBuffers.clearListener('flutter/platform');
92+
super.unmount();
93+
}
8294
}

testing/scenario_app/lib/src/scenario.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:typed_data';
65
import 'dart:ui';
76

87
/// A scenario to run for testing.
@@ -51,14 +50,4 @@ abstract class Scenario {
5150
///
5251
/// See [PlatformDispatcher.onPointerDataPacket].
5352
void onPointerDataPacket(PointerDataPacket packet) {}
54-
55-
/// Called by the program when an engine side platform channel message is
56-
/// received.
57-
///
58-
/// See [PlatformDispatcher.onPlatformMessage].
59-
void onPlatformMessage(
60-
String name,
61-
ByteData? data,
62-
PlatformMessageResponseCallback? callback,
63-
) {}
6453
}

0 commit comments

Comments
 (0)