flutter_flutter/packages/flutter/test/services/message_codecs_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

281 lines
9.3 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This files contains message codec tests that are supported both on the Web
// and in the VM. For VM-only tests see message_codecs_vm_test.dart.
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'message_codecs_testing.dart';
void main() {
group('Binary codec', () {
const MessageCodec<ByteData?> binary = BinaryCodec();
test('should encode and decode simple messages', () {
checkEncodeDecode<ByteData?>(binary, null);
checkEncodeDecode<ByteData?>(binary, ByteData(0));
checkEncodeDecode<ByteData?>(binary, ByteData(4)..setInt32(0, -7));
});
});
group('String codec', () {
const MessageCodec<String?> string = StringCodec();
test('should encode and decode simple messages', () {
checkEncodeDecode<String?>(string, null);
checkEncodeDecode<String?>(string, '');
checkEncodeDecode<String?>(string, 'hello');
checkEncodeDecode<String?>(string, 'special chars >\u263A\u{1F602}<');
});
test('ByteData with offset', () {
const MessageCodec<String?> string = StringCodec();
final ByteData helloWorldByteData = string.encodeMessage('hello world')!;
final ByteData helloByteData = string.encodeMessage('hello')!;
final offsetByteData = ByteData.view(
helloWorldByteData.buffer,
helloByteData.lengthInBytes,
helloWorldByteData.lengthInBytes - helloByteData.lengthInBytes,
);
expect(string.decodeMessage(offsetByteData), ' world');
});
});
group('Standard method codec', () {
const MethodCodec method = StandardMethodCodec();
const messageCodec = StandardMessageCodec();
test('Should encode and decode objects produced from codec', () {
final ByteData? data = messageCodec.encodeMessage(<Object, Object>{'foo': true, 3: 'fizz'});
expect(messageCodec.decodeMessage(data), <Object?, Object?>{'foo': true, 3: 'fizz'});
});
test('should decode error envelope without native stacktrace', () {
final ByteData errorData = method.encodeErrorEnvelope(
code: 'errorCode',
message: 'errorMessage',
details: 'errorDetails',
);
expect(
() => method.decodeEnvelope(errorData),
throwsA(
predicate(
(PlatformException e) =>
e.code == 'errorCode' && e.message == 'errorMessage' && e.details == 'errorDetails',
),
),
);
});
test('should decode error envelope with native stacktrace.', () {
final buffer = WriteBuffer();
buffer.putUint8(1);
messageCodec.writeValue(buffer, 'errorCode');
messageCodec.writeValue(buffer, 'errorMessage');
messageCodec.writeValue(buffer, 'errorDetails');
messageCodec.writeValue(buffer, 'errorStacktrace');
final ByteData errorData = buffer.done();
expect(
() => method.decodeEnvelope(errorData),
throwsA(predicate((PlatformException e) => e.stacktrace == 'errorStacktrace')),
);
});
test('should allow null error message,', () {
final ByteData errorData = method.encodeErrorEnvelope(
code: 'errorCode',
details: 'errorDetails',
);
expect(
() => method.decodeEnvelope(errorData),
throwsA(
predicate((PlatformException e) {
return e.code == 'errorCode' && e.message == null && e.details == 'errorDetails';
}),
),
);
});
});
group('Json method codec', () {
const json = JsonCodec();
const stringCodec = StringCodec();
const jsonMethodCodec = JSONMethodCodec();
test('should decode error envelope without native stacktrace', () {
final ByteData errorData = jsonMethodCodec.encodeErrorEnvelope(
code: 'errorCode',
message: 'errorMessage',
details: 'errorDetails',
);
expect(
() => jsonMethodCodec.decodeEnvelope(errorData),
throwsA(
predicate(
(PlatformException e) =>
e.code == 'errorCode' && e.message == 'errorMessage' && e.details == 'errorDetails',
),
),
);
});
test('should decode error envelope with native stacktrace.', () {
final ByteData? errorData = stringCodec.encodeMessage(
json.encode(<dynamic>['errorCode', 'errorMessage', 'errorDetails', 'errorStacktrace']),
);
expect(
() => jsonMethodCodec.decodeEnvelope(errorData!),
throwsA(predicate((PlatformException e) => e.stacktrace == 'errorStacktrace')),
);
});
});
group('JSON message codec', () {
const MessageCodec<dynamic> json = JSONMessageCodec();
test('should encode and decode simple messages', () {
checkEncodeDecode<dynamic>(json, null);
checkEncodeDecode<dynamic>(json, true);
checkEncodeDecode<dynamic>(json, false);
checkEncodeDecode<dynamic>(json, 7);
checkEncodeDecode<dynamic>(json, -7);
checkEncodeDecode<dynamic>(json, 98742923489);
checkEncodeDecode<dynamic>(json, -98742923489);
checkEncodeDecode<dynamic>(json, 3.14);
checkEncodeDecode<dynamic>(json, '');
checkEncodeDecode<dynamic>(json, 'hello');
checkEncodeDecode<dynamic>(json, 'special chars >\u263A\u{1F602}<');
});
test('should encode and decode composite message', () {
final message = <dynamic>[
null,
true,
false,
-707,
-7000000007,
-3.14,
'',
'hello',
<dynamic>['nested', <dynamic>[]],
<dynamic, dynamic>{'a': 'nested', 'b': <dynamic, dynamic>{}},
'world',
];
checkEncodeDecode<dynamic>(json, message);
});
});
group('Standard message codec', () {
const MessageCodec<dynamic> standard = StandardMessageCodec();
test('should encode sizes correctly at boundary cases', () {
checkEncoding<dynamic>(standard, Uint8List(253), <int>[8, 253, ...List<int>.filled(253, 0)]);
checkEncoding<dynamic>(standard, Uint8List(254), <int>[
8,
254,
254,
0,
...List<int>.filled(254, 0),
]);
checkEncoding<dynamic>(standard, Uint8List(0xffff), <int>[
8,
254,
0xff,
0xff,
...List<int>.filled(0xffff, 0),
]);
checkEncoding<dynamic>(standard, Uint8List(0xffff + 1), <int>[
8,
255,
0,
0,
1,
0,
...List<int>.filled(0xffff + 1, 0),
]);
});
test('should encode and decode simple messages', () {
checkEncodeDecode<dynamic>(standard, null);
checkEncodeDecode<dynamic>(standard, true);
checkEncodeDecode<dynamic>(standard, false);
checkEncodeDecode<dynamic>(standard, 7);
checkEncodeDecode<dynamic>(standard, -7);
checkEncodeDecode<dynamic>(standard, 98742923489);
checkEncodeDecode<dynamic>(standard, -98742923489);
checkEncodeDecode<dynamic>(standard, 3.14);
checkEncodeDecode<dynamic>(standard, double.infinity);
checkEncodeDecode<dynamic>(standard, double.nan);
checkEncodeDecode<dynamic>(standard, '');
checkEncodeDecode<dynamic>(standard, 'hello');
checkEncodeDecode<dynamic>(standard, 'special chars >\u263A\u{1F602}<');
});
test('should encode and decode composite message', () {
final message = <dynamic>[
null,
true,
false,
-707,
-7000000007,
-3.14,
'',
'hello',
Uint8List.fromList(<int>[0xBA, 0x5E, 0xBA, 0x11]),
Int32List.fromList(<int>[-0x7fffffff - 1, 0, 0x7fffffff]),
null, // ensures the offset of the following list is unaligned.
null, // ensures the offset of the following list is unaligned.
Float64List.fromList(<double>[
double.negativeInfinity,
-double.maxFinite,
-double.minPositive,
-0.0,
0.0,
double.minPositive,
double.maxFinite,
double.infinity,
double.nan,
]),
Float32List.fromList(<double>[
double.negativeInfinity,
-double.maxFinite,
-double.minPositive,
-0.0,
0.0,
double.minPositive,
double.maxFinite,
double.infinity,
double.nan,
]),
<dynamic>['nested', <dynamic>[]],
<dynamic, dynamic>{'a': 'nested', null: <dynamic, dynamic>{}},
'world',
];
checkEncodeDecode<dynamic>(standard, message);
});
test('should align doubles to 8 bytes', () {
checkEncoding<dynamic>(standard, 1.0, <int>[
6,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0xf0,
0x3f,
]);
});
});
test('toString works as intended', () async {
const methodCall = MethodCall('sample method');
final platformException = PlatformException(code: '100');
final missingPluginException = MissingPluginException();
expect(methodCall.toString(), 'MethodCall(sample method, null)');
expect(platformException.toString(), 'PlatformException(100, null, null, null)');
expect(missingPluginException.toString(), 'MissingPluginException(null)');
});
}