From f47c2d0c45bcac4bac39995be17ad20101fdb5f6 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 3 Aug 2023 15:47:06 -0400 Subject: [PATCH 1/5] Copy the test, and make its import work correctly --- packages/pigeon/lib/dart_generator.dart | 3 +- packages/pigeon/lib/pigeon_lib.dart | 5 + .../test/genered_dart_test_code_test.dart | 100 ++++++++++ .../test/test_message.gen.dart | 176 ++++++++++++++++++ packages/pigeon/test/dart_generator_test.dart | 13 +- 5 files changed, 293 insertions(+), 4 deletions(-) create mode 100644 packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart create mode 100644 packages/pigeon/platform_tests/shared_test_plugin_code/test/test_message.gen.dart diff --git a/packages/pigeon/lib/dart_generator.dart b/packages/pigeon/lib/dart_generator.dart index 2c40a8ddece..d712ee54683 100644 --- a/packages/pigeon/lib/dart_generator.dart +++ b/packages/pigeon/lib/dart_generator.dart @@ -599,6 +599,7 @@ if (replyList == null) { Root root, StringSink sink, { required String dartPackageName, + required String dartOutputPackageName, }) { final Indent indent = Indent(sink); final String sourceOutPath = generatorOptions.sourceOutPath ?? ''; @@ -620,7 +621,7 @@ if (replyList == null) { } else { final String path = relativeDartPath.replaceFirst(RegExp(r'^.*/lib/'), ''); - indent.writeln("import 'package:$dartPackageName/$path';"); + indent.writeln("import 'package:$dartOutputPackageName/$path';"); } for (final Api api in root.apis) { if (api.location == ApiLocation.host && api.dartHostTestHandler != null) { diff --git a/packages/pigeon/lib/pigeon_lib.dart b/packages/pigeon/lib/pigeon_lib.dart index 2fca07a05bc..ae5d7ad346d 100644 --- a/packages/pigeon/lib/pigeon_lib.dart +++ b/packages/pigeon/lib/pigeon_lib.dart @@ -510,11 +510,16 @@ class DartTestGeneratorAdapter implements GeneratorAdapter { basePath: options.basePath ?? '', ); const DartGenerator testGenerator = DartGenerator(); + // The test code needs the actual package name of the Dart output, even if + // the package name has been overridden for other uses. + final String outputPackageName = + deducePackageName(options.dartOut ?? '') ?? options.getPackageName(); testGenerator.generateTest( dartOptionsWithHeader, root, sink, dartPackageName: options.getPackageName(), + dartOutputPackageName: outputPackageName, ); } diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart new file mode 100644 index 00000000000..805e956539c --- /dev/null +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart @@ -0,0 +1,100 @@ +// Copyright 2013 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. + +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_test_plugin_code/src/generated/message.gen.dart'; + +import 'test_message.gen.dart'; + +class Mock implements TestHostApi { + List log = []; + + @override + void initialize() { + log.add('initialize'); + } + + @override + MessageSearchReply search(MessageSearchRequest arg) { + log.add('search'); + return MessageSearchReply()..result = arg.query; + } +} + +class MockNested implements TestNestedApi { + bool didCall = false; + @override + MessageSearchReply search(MessageNested arg) { + didCall = true; + if (arg.request == null) { + return MessageSearchReply(); + } else { + return MessageSearchReply()..result = arg.request?.query; + } + } +} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + test('simple', () async { + final MessageNestedApi api = MessageNestedApi(); + final MockNested mock = MockNested(); + TestNestedApi.setup(mock); + final MessageSearchReply reply = + await api.search(MessageNested()..request = null); + expect(mock.didCall, true); + expect(reply.result, null); + }); + + test('nested', () async { + final MessageApi api = MessageApi(); + final Mock mock = Mock(); + TestHostApi.setup(mock); + final MessageSearchReply reply = + await api.search(MessageSearchRequest()..query = 'foo'); + expect(mock.log, ['search']); + expect(reply.result, 'foo'); + }); + + test('no-arg calls', () async { + final MessageApi api = MessageApi(); + final Mock mock = Mock(); + TestHostApi.setup(mock); + await api.initialize(); + expect(mock.log, ['initialize']); + }); + + test( + 'calling methods with null', + () async { + final Mock mock = Mock(); + TestHostApi.setup(mock); + expect( + await const BasicMessageChannel( + 'dev.flutter.pigeon.mock_handler_tester.MessageApi.initialize', + StandardMessageCodec(), + ).send([null]), + isEmpty, + ); + try { + await const BasicMessageChannel( + 'dev.flutter.pigeon.mock_handler_tester.MessageApi.search', + StandardMessageCodec(), + ).send([null]) as List?; + expect(true, isFalse); // should not reach here + } catch (error) { + expect(error, isAssertionError); + expect( + error.toString(), + contains( + 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageApi.search was null, expected non-null MessageSearchRequest.', + ), + ); + } + expect(mock.log, ['initialize']); + }, + ); +} diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/test/test_message.gen.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/test/test_message.gen.dart new file mode 100644 index 00000000000..ff3a3d9c89a --- /dev/null +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/test/test_message.gen.dart @@ -0,0 +1,176 @@ +// Copyright 2013 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. +// +// Autogenerated from Pigeon, do not edit directly. +// See also: https://pub.dev/packages/pigeon +// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import +// ignore_for_file: avoid_relative_lib_imports +import 'dart:async'; +import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List; +import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'package:shared_test_plugin_code/src/generated/message.gen.dart'; + +class _TestHostApiCodec extends StandardMessageCodec { + const _TestHostApiCodec(); + @override + void writeValue(WriteBuffer buffer, Object? value) { + if (value is MessageSearchReply) { + buffer.putUint8(128); + writeValue(buffer, value.encode()); + } else if (value is MessageSearchRequest) { + buffer.putUint8(129); + writeValue(buffer, value.encode()); + } else { + super.writeValue(buffer, value); + } + } + + @override + Object? readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + case 128: + return MessageSearchReply.decode(readValue(buffer)!); + case 129: + return MessageSearchRequest.decode(readValue(buffer)!); + default: + return super.readValueOfType(type, buffer); + } + } +} + +/// This comment is to test api documentation comments. +/// +/// This comment also tests multiple line comments. +abstract class TestHostApi { + static TestDefaultBinaryMessengerBinding? get _testBinaryMessengerBinding => + TestDefaultBinaryMessengerBinding.instance; + static const MessageCodec codec = _TestHostApiCodec(); + + /// This comment is to test documentation comments. + /// + /// This comment also tests multiple line comments. + void initialize(); + + /// This comment is to test method documentation comments. + MessageSearchReply search(MessageSearchRequest request); + + static void setup(TestHostApi? api, {BinaryMessenger? binaryMessenger}) { + { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.pigeon_integration_tests.MessageApi.initialize', + codec, + binaryMessenger: binaryMessenger); + if (api == null) { + _testBinaryMessengerBinding!.defaultBinaryMessenger + .setMockDecodedMessageHandler(channel, null); + } else { + _testBinaryMessengerBinding!.defaultBinaryMessenger + .setMockDecodedMessageHandler(channel, + (Object? message) async { + // ignore message + api.initialize(); + return []; + }); + } + } + { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.pigeon_integration_tests.MessageApi.search', + codec, + binaryMessenger: binaryMessenger); + if (api == null) { + _testBinaryMessengerBinding!.defaultBinaryMessenger + .setMockDecodedMessageHandler(channel, null); + } else { + _testBinaryMessengerBinding!.defaultBinaryMessenger + .setMockDecodedMessageHandler(channel, + (Object? message) async { + assert(message != null, + 'Argument for dev.flutter.pigeon.pigeon_integration_tests.MessageApi.search was null.'); + final List args = (message as List?)!; + final MessageSearchRequest? arg_request = + (args[0] as MessageSearchRequest?); + assert(arg_request != null, + 'Argument for dev.flutter.pigeon.pigeon_integration_tests.MessageApi.search was null, expected non-null MessageSearchRequest.'); + final MessageSearchReply output = api.search(arg_request!); + return [output]; + }); + } + } + } +} + +class _TestNestedApiCodec extends StandardMessageCodec { + const _TestNestedApiCodec(); + @override + void writeValue(WriteBuffer buffer, Object? value) { + if (value is MessageNested) { + buffer.putUint8(128); + writeValue(buffer, value.encode()); + } else if (value is MessageSearchReply) { + buffer.putUint8(129); + writeValue(buffer, value.encode()); + } else if (value is MessageSearchRequest) { + buffer.putUint8(130); + writeValue(buffer, value.encode()); + } else { + super.writeValue(buffer, value); + } + } + + @override + Object? readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + case 128: + return MessageNested.decode(readValue(buffer)!); + case 129: + return MessageSearchReply.decode(readValue(buffer)!); + case 130: + return MessageSearchRequest.decode(readValue(buffer)!); + default: + return super.readValueOfType(type, buffer); + } + } +} + +/// This comment is to test api documentation comments. +abstract class TestNestedApi { + static TestDefaultBinaryMessengerBinding? get _testBinaryMessengerBinding => + TestDefaultBinaryMessengerBinding.instance; + static const MessageCodec codec = _TestNestedApiCodec(); + + /// This comment is to test method documentation comments. + /// + /// This comment also tests multiple line comments. + MessageSearchReply search(MessageNested nested); + + static void setup(TestNestedApi? api, {BinaryMessenger? binaryMessenger}) { + { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.pigeon_integration_tests.MessageNestedApi.search', + codec, + binaryMessenger: binaryMessenger); + if (api == null) { + _testBinaryMessengerBinding!.defaultBinaryMessenger + .setMockDecodedMessageHandler(channel, null); + } else { + _testBinaryMessengerBinding!.defaultBinaryMessenger + .setMockDecodedMessageHandler(channel, + (Object? message) async { + assert(message != null, + 'Argument for dev.flutter.pigeon.pigeon_integration_tests.MessageNestedApi.search was null.'); + final List args = (message as List?)!; + final MessageNested? arg_nested = (args[0] as MessageNested?); + assert(arg_nested != null, + 'Argument for dev.flutter.pigeon.pigeon_integration_tests.MessageNestedApi.search was null, expected non-null MessageNested.'); + final MessageSearchReply output = api.search(arg_nested!); + return [output]; + }); + } + } + } +} diff --git a/packages/pigeon/test/dart_generator_test.dart b/packages/pigeon/test/dart_generator_test.dart index 9fbd5b53ff3..be31794a744 100644 --- a/packages/pigeon/test/dart_generator_test.dart +++ b/packages/pigeon/test/dart_generator_test.dart @@ -686,6 +686,7 @@ void main() { root, testCodeSink, dartPackageName: DEFAULT_PACKAGE_NAME, + dartOutputPackageName: DEFAULT_PACKAGE_NAME, ); final String testCode = testCodeSink.toString(); expect(testCode, contains(r"import 'fo\'o.dart';")); @@ -1351,7 +1352,10 @@ void main() { expect(code, contains('void doit(int? foo);')); }); - test('uses defined package name', () { + test('uses output package name for imports', () { + const String overriddenPackageName = 'custom_name'; + const String outputPackageName = 'some_output_package'; + assert(outputPackageName != DEFAULT_PACKAGE_NAME); final Directory tempDir = Directory.systemTemp.createTempSync('pigeon'); try { final Directory foo = Directory(path.join(tempDir.path, 'lib', 'foo')); @@ -1371,10 +1375,12 @@ name: foobar ), root, sink, - dartPackageName: DEFAULT_PACKAGE_NAME, + dartPackageName: overriddenPackageName, + dartOutputPackageName: outputPackageName, ); final String code = sink.toString(); - expect(code, contains("import 'package:test_package/foo/bar.dart';")); + expect( + code, contains("import 'package:$outputPackageName/foo/bar.dart';")); } finally { tempDir.deleteSync(recursive: true); } @@ -1598,6 +1604,7 @@ name: foobar root, sink, dartPackageName: DEFAULT_PACKAGE_NAME, + dartOutputPackageName: DEFAULT_PACKAGE_NAME, ); final String testCode = sink.toString(); From f85d481442f94a0ce42074c9a57db1e9a28d7331 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 3 Aug 2023 15:56:38 -0400 Subject: [PATCH 2/5] Fix channel names --- .../test/genered_dart_test_code_test.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart b/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart index 805e956539c..a0632cf3486 100644 --- a/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart +++ b/packages/pigeon/platform_tests/shared_test_plugin_code/test/genered_dart_test_code_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// This file specifically tests the test code generated by dartTestOut. + import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_test_plugin_code/src/generated/message.gen.dart'; @@ -74,14 +76,14 @@ void main() { TestHostApi.setup(mock); expect( await const BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.initialize', + 'dev.flutter.pigeon.pigeon_integration_tests.MessageApi.initialize', StandardMessageCodec(), ).send([null]), isEmpty, ); try { await const BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.search', + 'dev.flutter.pigeon.pigeon_integration_tests.MessageApi.search', StandardMessageCodec(), ).send([null]) as List?; expect(true, isFalse); // should not reach here @@ -90,7 +92,7 @@ void main() { expect( error.toString(), contains( - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageApi.search was null, expected non-null MessageSearchRequest.', + 'Argument for dev.flutter.pigeon.pigeon_integration_tests.MessageApi.search was null, expected non-null MessageSearchRequest.', ), ); } From 7333b0cc1ea626894168aab034014a0c86760b3c Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 3 Aug 2023 15:57:17 -0400 Subject: [PATCH 3/5] Delete mock_handler_tester --- .../pigeon/mock_handler_tester/.gitignore | 44 --- packages/pigeon/mock_handler_tester/.metadata | 10 - packages/pigeon/mock_handler_tester/README.md | 3 - .../pigeon/mock_handler_tester/lib/main.dart | 9 - .../pigeon/mock_handler_tester/pubspec.yaml | 20 - .../mock_handler_tester/test/message.dart | 357 ------------------ .../pigeon/mock_handler_tester/test/test.dart | 174 --------- .../mock_handler_tester/test/widget_test.dart | 100 ----- 8 files changed, 717 deletions(-) delete mode 100644 packages/pigeon/mock_handler_tester/.gitignore delete mode 100644 packages/pigeon/mock_handler_tester/.metadata delete mode 100644 packages/pigeon/mock_handler_tester/README.md delete mode 100644 packages/pigeon/mock_handler_tester/lib/main.dart delete mode 100644 packages/pigeon/mock_handler_tester/pubspec.yaml delete mode 100644 packages/pigeon/mock_handler_tester/test/message.dart delete mode 100644 packages/pigeon/mock_handler_tester/test/test.dart delete mode 100644 packages/pigeon/mock_handler_tester/test/widget_test.dart diff --git a/packages/pigeon/mock_handler_tester/.gitignore b/packages/pigeon/mock_handler_tester/.gitignore deleted file mode 100644 index f3c205341e7..00000000000 --- a/packages/pigeon/mock_handler_tester/.gitignore +++ /dev/null @@ -1,44 +0,0 @@ -# Miscellaneous -*.class -*.log -*.pyc -*.swp -.DS_Store -.atom/ -.buildlog/ -.history -.svn/ - -# IntelliJ related -*.iml -*.ipr -*.iws -.idea/ - -# The .vscode folder contains launch configuration and tasks you configure in -# VS Code which you may wish to be included in version control, so this line -# is commented out by default. -#.vscode/ - -# Flutter/Dart/Pub related -**/doc/api/ -**/ios/Flutter/.last_build_id -.dart_tool/ -.flutter-plugins -.flutter-plugins-dependencies -.packages -.pub-cache/ -.pub/ -/build/ - -# Web related -lib/generated_plugin_registrant.dart - -# Symbolication related -app.*.symbols - -# Obfuscation related -app.*.map.json - -# Exceptions to above rules. -!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/packages/pigeon/mock_handler_tester/.metadata b/packages/pigeon/mock_handler_tester/.metadata deleted file mode 100644 index bb4f09ae751..00000000000 --- a/packages/pigeon/mock_handler_tester/.metadata +++ /dev/null @@ -1,10 +0,0 @@ -# This file tracks properties of this Flutter project. -# Used by Flutter tool to assess capabilities and perform upgrades etc. -# -# This file should be version controlled and should not be manually edited. - -version: - revision: e6b697a9df5e9ce933024be3334e86b599c60e71 - channel: unknown - -project_type: app diff --git a/packages/pigeon/mock_handler_tester/README.md b/packages/pigeon/mock_handler_tester/README.md deleted file mode 100644 index 698b5065117..00000000000 --- a/packages/pigeon/mock_handler_tester/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# mock_handler_tester - -A test bed for testing the code generated by `dartHostTestHandler`. diff --git a/packages/pigeon/mock_handler_tester/lib/main.dart b/packages/pigeon/mock_handler_tester/lib/main.dart deleted file mode 100644 index b8ddfeba469..00000000000 --- a/packages/pigeon/mock_handler_tester/lib/main.dart +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2013 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. - -import 'package:flutter/material.dart'; - -void main() { - runApp(Container()); -} diff --git a/packages/pigeon/mock_handler_tester/pubspec.yaml b/packages/pigeon/mock_handler_tester/pubspec.yaml deleted file mode 100644 index 69eba8468ab..00000000000 --- a/packages/pigeon/mock_handler_tester/pubspec.yaml +++ /dev/null @@ -1,20 +0,0 @@ -name: mock_handler_tester -description: A testbed for testing dartHostTestHandler. -publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 1.0.0+1 - -environment: - sdk: ">=2.18.0 <4.0.0" - flutter: ">=3.3.0" - -dependencies: - cupertino_icons: ^1.0.2 - flutter: - sdk: flutter - -dev_dependencies: - flutter_test: - sdk: flutter - -flutter: - uses-material-design: true diff --git a/packages/pigeon/mock_handler_tester/test/message.dart b/packages/pigeon/mock_handler_tester/test/message.dart deleted file mode 100644 index 3a330815b39..00000000000 --- a/packages/pigeon/mock_handler_tester/test/message.dart +++ /dev/null @@ -1,357 +0,0 @@ -// Copyright 2013 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. -// -// Autogenerated from Pigeon (v10.1.4), do not edit directly. -// See also: https://pub.dev/packages/pigeon -// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import - -import 'dart:async'; -import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List; - -import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; -import 'package:flutter/services.dart'; - -/// This comment is to test enum documentation comments. -/// -/// This comment also tests multiple line comments. -/// -/// //////////////////////// -/// This comment also tests comments that start with '/' -/// //////////////////////// -enum MessageRequestState { - pending, - success, - failure, -} - -/// This comment is to test class documentation comments. -/// -/// This comment also tests multiple line comments. -class MessageSearchRequest { - MessageSearchRequest({ - this.query, - this.anInt, - this.aBool, - }); - - /// This comment is to test field documentation comments. - String? query; - - /// This comment is to test field documentation comments. - int? anInt; - - /// This comment is to test field documentation comments. - bool? aBool; - - Object encode() { - return [ - query, - anInt, - aBool, - ]; - } - - static MessageSearchRequest decode(Object result) { - result as List; - return MessageSearchRequest( - query: result[0] as String?, - anInt: result[1] as int?, - aBool: result[2] as bool?, - ); - } -} - -/// This comment is to test class documentation comments. -class MessageSearchReply { - MessageSearchReply({ - this.result, - this.error, - this.state, - }); - - /// This comment is to test field documentation comments. - /// - /// This comment also tests multiple line comments. - String? result; - - /// This comment is to test field documentation comments. - String? error; - - /// This comment is to test field documentation comments. - MessageRequestState? state; - - Object encode() { - return [ - result, - error, - state?.index, - ]; - } - - static MessageSearchReply decode(Object result) { - result as List; - return MessageSearchReply( - result: result[0] as String?, - error: result[1] as String?, - state: result[2] != null - ? MessageRequestState.values[result[2]! as int] - : null, - ); - } -} - -/// This comment is to test class documentation comments. -class MessageNested { - MessageNested({ - this.request, - }); - - /// This comment is to test field documentation comments. - MessageSearchRequest? request; - - Object encode() { - return [ - request?.encode(), - ]; - } - - static MessageNested decode(Object result) { - result as List; - return MessageNested( - request: result[0] != null - ? MessageSearchRequest.decode(result[0]! as List) - : null, - ); - } -} - -class _MessageApiCodec extends StandardMessageCodec { - const _MessageApiCodec(); - @override - void writeValue(WriteBuffer buffer, Object? value) { - if (value is MessageSearchReply) { - buffer.putUint8(128); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchRequest) { - buffer.putUint8(129); - writeValue(buffer, value.encode()); - } else { - super.writeValue(buffer, value); - } - } - - @override - Object? readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case 128: - return MessageSearchReply.decode(readValue(buffer)!); - case 129: - return MessageSearchRequest.decode(readValue(buffer)!); - default: - return super.readValueOfType(type, buffer); - } - } -} - -/// This comment is to test api documentation comments. -/// -/// This comment also tests multiple line comments. -class MessageApi { - /// Constructor for [MessageApi]. The [binaryMessenger] named argument is - /// available for dependency injection. If it is left null, the default - /// BinaryMessenger will be used which routes to the host platform. - MessageApi({BinaryMessenger? binaryMessenger}) - : _binaryMessenger = binaryMessenger; - final BinaryMessenger? _binaryMessenger; - - static const MessageCodec codec = _MessageApiCodec(); - - /// This comment is to test documentation comments. - /// - /// This comment also tests multiple line comments. - Future initialize() async { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.initialize', codec, - binaryMessenger: _binaryMessenger); - final List? replyList = await channel.send(null) as List?; - if (replyList == null) { - throw PlatformException( - code: 'channel-error', - message: 'Unable to establish connection on channel.', - ); - } else if (replyList.length > 1) { - throw PlatformException( - code: replyList[0]! as String, - message: replyList[1] as String?, - details: replyList[2], - ); - } else { - return; - } - } - - /// This comment is to test method documentation comments. - Future search(MessageSearchRequest arg_request) async { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.search', codec, - binaryMessenger: _binaryMessenger); - final List? replyList = - await channel.send([arg_request]) as List?; - if (replyList == null) { - throw PlatformException( - code: 'channel-error', - message: 'Unable to establish connection on channel.', - ); - } else if (replyList.length > 1) { - throw PlatformException( - code: replyList[0]! as String, - message: replyList[1] as String?, - details: replyList[2], - ); - } else if (replyList[0] == null) { - throw PlatformException( - code: 'null-error', - message: 'Host platform returned null value for non-null return value.', - ); - } else { - return (replyList[0] as MessageSearchReply?)!; - } - } -} - -class _MessageNestedApiCodec extends StandardMessageCodec { - const _MessageNestedApiCodec(); - @override - void writeValue(WriteBuffer buffer, Object? value) { - if (value is MessageNested) { - buffer.putUint8(128); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchReply) { - buffer.putUint8(129); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchRequest) { - buffer.putUint8(130); - writeValue(buffer, value.encode()); - } else { - super.writeValue(buffer, value); - } - } - - @override - Object? readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case 128: - return MessageNested.decode(readValue(buffer)!); - case 129: - return MessageSearchReply.decode(readValue(buffer)!); - case 130: - return MessageSearchRequest.decode(readValue(buffer)!); - default: - return super.readValueOfType(type, buffer); - } - } -} - -/// This comment is to test api documentation comments. -class MessageNestedApi { - /// Constructor for [MessageNestedApi]. The [binaryMessenger] named argument is - /// available for dependency injection. If it is left null, the default - /// BinaryMessenger will be used which routes to the host platform. - MessageNestedApi({BinaryMessenger? binaryMessenger}) - : _binaryMessenger = binaryMessenger; - final BinaryMessenger? _binaryMessenger; - - static const MessageCodec codec = _MessageNestedApiCodec(); - - /// This comment is to test method documentation comments. - /// - /// This comment also tests multiple line comments. - Future search(MessageNested arg_nested) async { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageNestedApi.search', codec, - binaryMessenger: _binaryMessenger); - final List? replyList = - await channel.send([arg_nested]) as List?; - if (replyList == null) { - throw PlatformException( - code: 'channel-error', - message: 'Unable to establish connection on channel.', - ); - } else if (replyList.length > 1) { - throw PlatformException( - code: replyList[0]! as String, - message: replyList[1] as String?, - details: replyList[2], - ); - } else if (replyList[0] == null) { - throw PlatformException( - code: 'null-error', - message: 'Host platform returned null value for non-null return value.', - ); - } else { - return (replyList[0] as MessageSearchReply?)!; - } - } -} - -class _MessageFlutterSearchApiCodec extends StandardMessageCodec { - const _MessageFlutterSearchApiCodec(); - @override - void writeValue(WriteBuffer buffer, Object? value) { - if (value is MessageSearchReply) { - buffer.putUint8(128); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchRequest) { - buffer.putUint8(129); - writeValue(buffer, value.encode()); - } else { - super.writeValue(buffer, value); - } - } - - @override - Object? readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case 128: - return MessageSearchReply.decode(readValue(buffer)!); - case 129: - return MessageSearchRequest.decode(readValue(buffer)!); - default: - return super.readValueOfType(type, buffer); - } - } -} - -/// This comment is to test api documentation comments. -abstract class MessageFlutterSearchApi { - static const MessageCodec codec = _MessageFlutterSearchApiCodec(); - - /// This comment is to test method documentation comments. - MessageSearchReply search(MessageSearchRequest request); - - static void setup(MessageFlutterSearchApi? api, - {BinaryMessenger? binaryMessenger}) { - { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageFlutterSearchApi.search', - codec, - binaryMessenger: binaryMessenger); - if (api == null) { - channel.setMessageHandler(null); - } else { - channel.setMessageHandler((Object? message) async { - assert(message != null, - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageFlutterSearchApi.search was null.'); - final List args = (message as List?)!; - final MessageSearchRequest? arg_request = - (args[0] as MessageSearchRequest?); - assert(arg_request != null, - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageFlutterSearchApi.search was null, expected non-null MessageSearchRequest.'); - final MessageSearchReply output = api.search(arg_request!); - return output; - }); - } - } - } -} diff --git a/packages/pigeon/mock_handler_tester/test/test.dart b/packages/pigeon/mock_handler_tester/test/test.dart deleted file mode 100644 index a6cf4682bd4..00000000000 --- a/packages/pigeon/mock_handler_tester/test/test.dart +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright 2013 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. -// -// Autogenerated from Pigeon (v10.1.4), do not edit directly. -// See also: https://pub.dev/packages/pigeon -// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import -// ignore_for_file: avoid_relative_lib_imports -import 'dart:async'; -import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List; -import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; -import 'package:flutter/services.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'message.dart'; - -class _TestHostApiCodec extends StandardMessageCodec { - const _TestHostApiCodec(); - @override - void writeValue(WriteBuffer buffer, Object? value) { - if (value is MessageSearchReply) { - buffer.putUint8(128); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchRequest) { - buffer.putUint8(129); - writeValue(buffer, value.encode()); - } else { - super.writeValue(buffer, value); - } - } - - @override - Object? readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case 128: - return MessageSearchReply.decode(readValue(buffer)!); - case 129: - return MessageSearchRequest.decode(readValue(buffer)!); - default: - return super.readValueOfType(type, buffer); - } - } -} - -/// This comment is to test api documentation comments. -/// -/// This comment also tests multiple line comments. -abstract class TestHostApi { - static TestDefaultBinaryMessengerBinding? get _testBinaryMessengerBinding => - TestDefaultBinaryMessengerBinding.instance; - static const MessageCodec codec = _TestHostApiCodec(); - - /// This comment is to test documentation comments. - /// - /// This comment also tests multiple line comments. - void initialize(); - - /// This comment is to test method documentation comments. - MessageSearchReply search(MessageSearchRequest request); - - static void setup(TestHostApi? api, {BinaryMessenger? binaryMessenger}) { - { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.initialize', codec, - binaryMessenger: binaryMessenger); - if (api == null) { - _testBinaryMessengerBinding!.defaultBinaryMessenger - .setMockDecodedMessageHandler(channel, null); - } else { - _testBinaryMessengerBinding!.defaultBinaryMessenger - .setMockDecodedMessageHandler(channel, - (Object? message) async { - // ignore message - api.initialize(); - return []; - }); - } - } - { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.search', codec, - binaryMessenger: binaryMessenger); - if (api == null) { - _testBinaryMessengerBinding!.defaultBinaryMessenger - .setMockDecodedMessageHandler(channel, null); - } else { - _testBinaryMessengerBinding!.defaultBinaryMessenger - .setMockDecodedMessageHandler(channel, - (Object? message) async { - assert(message != null, - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageApi.search was null.'); - final List args = (message as List?)!; - final MessageSearchRequest? arg_request = - (args[0] as MessageSearchRequest?); - assert(arg_request != null, - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageApi.search was null, expected non-null MessageSearchRequest.'); - final MessageSearchReply output = api.search(arg_request!); - return [output]; - }); - } - } - } -} - -class _TestNestedApiCodec extends StandardMessageCodec { - const _TestNestedApiCodec(); - @override - void writeValue(WriteBuffer buffer, Object? value) { - if (value is MessageNested) { - buffer.putUint8(128); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchReply) { - buffer.putUint8(129); - writeValue(buffer, value.encode()); - } else if (value is MessageSearchRequest) { - buffer.putUint8(130); - writeValue(buffer, value.encode()); - } else { - super.writeValue(buffer, value); - } - } - - @override - Object? readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case 128: - return MessageNested.decode(readValue(buffer)!); - case 129: - return MessageSearchReply.decode(readValue(buffer)!); - case 130: - return MessageSearchRequest.decode(readValue(buffer)!); - default: - return super.readValueOfType(type, buffer); - } - } -} - -/// This comment is to test api documentation comments. -abstract class TestNestedApi { - static TestDefaultBinaryMessengerBinding? get _testBinaryMessengerBinding => - TestDefaultBinaryMessengerBinding.instance; - static const MessageCodec codec = _TestNestedApiCodec(); - - /// This comment is to test method documentation comments. - /// - /// This comment also tests multiple line comments. - MessageSearchReply search(MessageNested nested); - - static void setup(TestNestedApi? api, {BinaryMessenger? binaryMessenger}) { - { - final BasicMessageChannel channel = BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageNestedApi.search', - codec, - binaryMessenger: binaryMessenger); - if (api == null) { - _testBinaryMessengerBinding!.defaultBinaryMessenger - .setMockDecodedMessageHandler(channel, null); - } else { - _testBinaryMessengerBinding!.defaultBinaryMessenger - .setMockDecodedMessageHandler(channel, - (Object? message) async { - assert(message != null, - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageNestedApi.search was null.'); - final List args = (message as List?)!; - final MessageNested? arg_nested = (args[0] as MessageNested?); - assert(arg_nested != null, - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageNestedApi.search was null, expected non-null MessageNested.'); - final MessageSearchReply output = api.search(arg_nested!); - return [output]; - }); - } - } - } -} diff --git a/packages/pigeon/mock_handler_tester/test/widget_test.dart b/packages/pigeon/mock_handler_tester/test/widget_test.dart deleted file mode 100644 index bb0af448434..00000000000 --- a/packages/pigeon/mock_handler_tester/test/widget_test.dart +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2013 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. - -import 'package:flutter/services.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'message.dart'; -import 'test.dart'; - -class Mock implements TestHostApi { - List log = []; - - @override - void initialize() { - log.add('initialize'); - } - - @override - MessageSearchReply search(MessageSearchRequest arg) { - log.add('search'); - return MessageSearchReply()..result = arg.query; - } -} - -class MockNested implements TestNestedApi { - bool didCall = false; - @override - MessageSearchReply search(MessageNested arg) { - didCall = true; - if (arg.request == null) { - return MessageSearchReply(); - } else { - return MessageSearchReply()..result = arg.request?.query; - } - } -} - -void main() { - TestWidgetsFlutterBinding.ensureInitialized(); - - test('simple', () async { - final MessageNestedApi api = MessageNestedApi(); - final MockNested mock = MockNested(); - TestNestedApi.setup(mock); - final MessageSearchReply reply = - await api.search(MessageNested()..request = null); - expect(mock.didCall, true); - expect(reply.result, null); - }); - - test('nested', () async { - final MessageApi api = MessageApi(); - final Mock mock = Mock(); - TestHostApi.setup(mock); - final MessageSearchReply reply = - await api.search(MessageSearchRequest()..query = 'foo'); - expect(mock.log, ['search']); - expect(reply.result, 'foo'); - }); - - test('no-arg calls', () async { - final MessageApi api = MessageApi(); - final Mock mock = Mock(); - TestHostApi.setup(mock); - await api.initialize(); - expect(mock.log, ['initialize']); - }); - - test( - 'calling methods with null', - () async { - final Mock mock = Mock(); - TestHostApi.setup(mock); - expect( - await const BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.initialize', - StandardMessageCodec(), - ).send([null]), - isEmpty, - ); - try { - await const BasicMessageChannel( - 'dev.flutter.pigeon.mock_handler_tester.MessageApi.search', - StandardMessageCodec(), - ).send([null]) as List?; - expect(true, isFalse); // should not reach here - } catch (error) { - expect(error, isAssertionError); - expect( - error.toString(), - contains( - 'Argument for dev.flutter.pigeon.mock_handler_tester.MessageApi.search was null, expected non-null MessageSearchRequest.', - ), - ); - } - expect(mock.log, ['initialize']); - }, - ); -} From 4894094d7e1a722e710dd0c1319c58d7a62c3858 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 3 Aug 2023 15:58:42 -0400 Subject: [PATCH 4/5] Delete the test suite --- packages/pigeon/tool/run_tests.dart | 1 - packages/pigeon/tool/shared/test_suites.dart | 22 -------------------- packages/pigeon/tool/test.dart | 1 - 3 files changed, 24 deletions(-) diff --git a/packages/pigeon/tool/run_tests.dart b/packages/pigeon/tool/run_tests.dart index 9b0ce7aecb9..0d7523bae97 100644 --- a/packages/pigeon/tool/run_tests.dart +++ b/packages/pigeon/tool/run_tests.dart @@ -160,7 +160,6 @@ Future> _modifiedFiles( Future main(List args) async { // Run most tests on Linux, since Linux tends to be the easiest and cheapest. const List linuxHostTests = [ - mockHandlerTests, commandLineTests, androidJavaUnitTests, androidJavaLint, diff --git a/packages/pigeon/tool/shared/test_suites.dart b/packages/pigeon/tool/shared/test_suites.dart index 658c4c083fa..5e45c31ff5c 100644 --- a/packages/pigeon/tool/shared/test_suites.dart +++ b/packages/pigeon/tool/shared/test_suites.dart @@ -50,7 +50,6 @@ const String windowsUnitTests = 'windows_unittests'; const String windowsIntegrationTests = 'windows_integration_tests'; const String dartUnitTests = 'dart_unittests'; const String flutterUnitTests = 'flutter_unittests'; -const String mockHandlerTests = 'mock_handler_tests'; const String commandLineTests = 'command_line_tests'; const Map testSuites = { @@ -101,9 +100,6 @@ const Map testSuites = { macOSSwiftIntegrationTests: TestInfo( function: _runMacOSSwiftIntegrationTests, description: 'Integration tests on generated Swift code on macOS.'), - mockHandlerTests: TestInfo( - function: _runMockHandlerTests, - description: 'Unit tests on generated Dart mock handler code.'), commandLineTests: TestInfo( function: _runCommandLineTests, description: 'Tests running pigeon with various command-line options.'), @@ -310,24 +306,6 @@ Future _runIOSSwiftIntegrationTests() async { return _runMobileIntegrationTests('iOS', _testPluginRelativePath); } -Future _runMockHandlerTests() async { - const String unitTestsPath = './mock_handler_tester'; - final int generateCode = await runPigeon( - input: './pigeons/message.dart', - dartOut: './mock_handler_tester/test/message.dart', - dartTestOut: './mock_handler_tester/test/test.dart', - ); - if (generateCode != 0) { - return generateCode; - } - - final int testCode = await runFlutterCommand(unitTestsPath, 'test'); - if (testCode != 0) { - return testCode; - } - return 0; -} - Future _runWindowsUnitTests() async { const String examplePath = './$_testPluginRelativePath/example'; final int compileCode = await runFlutterBuild(examplePath, 'windows'); diff --git a/packages/pigeon/tool/test.dart b/packages/pigeon/tool/test.dart index 9e31363a979..3667f44e78e 100644 --- a/packages/pigeon/tool/test.dart +++ b/packages/pigeon/tool/test.dart @@ -57,7 +57,6 @@ ${parser.usage}'''); const List dartTests = [ dartUnitTests, flutterUnitTests, - mockHandlerTests, commandLineTests, ]; const List androidTests = [ From d97a3d633e2219a22110fe5f2b89073b186d4e0f Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 3 Aug 2023 16:06:05 -0400 Subject: [PATCH 5/5] Bump version --- packages/pigeon/CHANGELOG.md | 4 ++++ packages/pigeon/lib/generator_tools.dart | 2 +- packages/pigeon/pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index cdf4c488f2d..1cd08060900 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,7 @@ +## 10.1.5 + +* Fixes import in generated Dart test output when overriding package name. + ## 10.1.4 * Adds package name to method channel strings to avoid potential collisions between plugins. diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart index b7b9107b805..62113aeef65 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -13,7 +13,7 @@ import 'ast.dart'; /// The current version of pigeon. /// /// This must match the version in pubspec.yaml. -const String pigeonVersion = '10.1.4'; +const String pigeonVersion = '10.1.5'; /// Read all the content from [stdin] to a String. String readStdin() { diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index a59786e19a7..90c81d7287e 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -2,7 +2,7 @@ name: pigeon description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. repository: https://github.com/flutter/packages/tree/main/packages/pigeon issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon -version: 10.1.4 # This must match the version in lib/generator_tools.dart +version: 10.1.5 # This must match the version in lib/generator_tools.dart environment: sdk: ">=2.19.0 <4.0.0"