Skip to content

[pigeon] More simple integration tests #2950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/pigeon/pigeons/core_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

import 'package:pigeon/pigeon.dart';

enum AnEnum {
one,
two,
three,
}

// A class containing all supported types.
class AllTypes {
bool? aBool;
Expand All @@ -21,6 +27,7 @@ class AllTypes {
List<List<bool?>?>? nestedList;
Map<String?, String?>? mapWithAnnotations;
Map<String?, Object?>? mapWithObject;
AnEnum? anEnum;
}

// A class for testing nested object handling.
Expand Down Expand Up @@ -64,10 +71,22 @@ abstract class HostIntegrationCoreApi {
@ObjCSelector('echoInt:')
int echoInt(int anInt);

/// Returns the passed in boolean asynchronously.
/// Returns passed in double.
@ObjCSelector('echoDouble:')
double echoDouble(double aDouble);

/// Returns the passed in boolean.
@ObjCSelector('echoBool:')
bool echoBool(bool aBool);

/// Returns the passed in string.
@ObjCSelector('echoString:')
String echoString(String aString);

/// Returns the passed in Uint8List.
@ObjCSelector('echoUint8List:')
Uint8List echoUint8List(Uint8List aUint8List);

// ========== Asyncronous method tests ==========

/// A no-op function taking no arguments and returning no value, to sanity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,26 @@ public Long echoInt(@NonNull Long anInt) {
return anInt;
}

@Override
public Double echoDouble(@NonNull Double aDouble) {
return aDouble;
}

@Override
public Boolean echoBool(@NonNull Boolean aBool) {
return aBool;
}

@Override
public String echoString(@NonNull String aString) {
return aString;
}

@Override
public byte[] echoUint8List(@NonNull byte[] aUint8List) {
return aUint8List;
}

@Override
public void noopAsync(Result<Void> result) {
result.success(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,25 @@ - (nullable NSNumber *)echoInt:(NSNumber *)anInt error:(FlutterError *_Nullable
return anInt;
}

- (nullable NSNumber *)echoDouble:(NSNumber *)aDouble
error:(FlutterError *_Nullable *_Nonnull)error {
return aDouble
}

- (nullable NSNumber *)echoBool:(NSNumber *)aBool error:(FlutterError *_Nullable *_Nonnull)error {
return aBool;
}

- (nullable NSString *)echoString:(NSString *)aString
error:(FlutterError *_Nullable *_Nonnull)error {
return aString;
}

- (nullable FlutterStandardTypedData *)echoUint8List:(FlutterStandardTypedData *)aUint8List
error:(FlutterError *_Nullable *_Nonnull)error {
return aUint8List;
}

- (void)noopAsyncWithCompletion:(void (^)(FlutterError *_Nullable))completion {
completion(nil);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;
import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;
import 'package:flutter/services.dart';

enum AnEnum {
one,
two,
three,
}

class AllTypes {
AllTypes({
this.aBool,
Expand All @@ -26,6 +32,7 @@ class AllTypes {
this.nestedList,
this.mapWithAnnotations,
this.mapWithObject,
this.anEnum,
});

bool? aBool;
Expand All @@ -41,6 +48,7 @@ class AllTypes {
List<List<bool?>?>? nestedList;
Map<String?, String?>? mapWithAnnotations;
Map<String?, Object?>? mapWithObject;
AnEnum? anEnum;

Object encode() {
final Map<Object?, Object?> pigeonMap = <Object?, Object?>{};
Expand All @@ -57,6 +65,7 @@ class AllTypes {
pigeonMap['nestedList'] = nestedList;
pigeonMap['mapWithAnnotations'] = mapWithAnnotations;
pigeonMap['mapWithObject'] = mapWithObject;
pigeonMap['anEnum'] = anEnum?.index;
return pigeonMap;
}

Expand All @@ -80,6 +89,9 @@ class AllTypes {
?.cast<String?, String?>(),
mapWithObject: (pigeonMap['mapWithObject'] as Map<Object?, Object?>?)
?.cast<String?, Object?>(),
anEnum: pigeonMap['anEnum'] != null
? AnEnum.values[pigeonMap['anEnum']! as int]
: null,
);
}
}
Expand Down Expand Up @@ -347,7 +359,37 @@ class HostIntegrationCoreApi {
}
}

/// Returns the passed in boolean asynchronously.
/// Returns passed in double.
Future<double> echoDouble(double arg_aDouble) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.HostIntegrationCoreApi.echoDouble', codec,
binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(<Object?>[arg_aDouble]) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error =
(replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else if (replyMap['result'] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (replyMap['result'] as double?)!;
}
}

/// Returns the passed in boolean.
Future<bool> echoBool(bool arg_aBool) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.HostIntegrationCoreApi.echoBool', codec,
Expand Down Expand Up @@ -377,6 +419,66 @@ class HostIntegrationCoreApi {
}
}

/// Returns the passed in string.
Future<String> echoString(String arg_aString) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.HostIntegrationCoreApi.echoString', codec,
binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(<Object?>[arg_aString]) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error =
(replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else if (replyMap['result'] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (replyMap['result'] as String?)!;
}
}

/// Returns the passed in Uint8List.
Future<Uint8List> echoUint8List(Uint8List arg_aUint8List) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.HostIntegrationCoreApi.echoUint8List', codec,
binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(<Object?>[arg_aUint8List]) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error =
(replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else if (replyMap['result'] == null) {
throw PlatformException(
code: 'null-error',
message: 'Host platform returned null value for non-null return value.',
);
} else {
return (replyMap['result'] as Uint8List?)!;
}
}

/// A no-op function taking no arguments and returning no value, to sanity
/// test basic asynchronous calling.
Future<void> noopAsync() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
<bool>[true, false],
<bool>[false, true]
],
anEnum: AnEnum.two,
);

final AllTypes echoObject = await api.echoAllTypes(sentObject);
Expand Down Expand Up @@ -90,6 +91,7 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
true);
expect(
mapEquals(echoObject.mapWithObject, sentObject.mapWithObject), true);
expect(echoObject.anEnum, sentObject.anEnum);
});

testWidgets('errors are returned correctly', (WidgetTester _) async {
Expand Down Expand Up @@ -143,9 +145,18 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
(WidgetTester _) async {
final HostIntegrationCoreApi api = HostIntegrationCoreApi();

const int inInt = -13;
final int anInt = await api.echoInt(inInt);
expect(anInt, inInt);
const int sentInt = -13;
final int receivedInt = await api.echoInt(sentInt);
expect(receivedInt, sentInt);
});

testWidgets('Doubles serialize and deserialize correctly',
(WidgetTester _) async {
final HostIntegrationCoreApi api = HostIntegrationCoreApi();

const double sentDouble = 2.0694;
final double receivedDouble = await api.echoDouble(sentDouble);
expect(receivedDouble, sentDouble);
});

testWidgets('booleans serialize and deserialize correctly',
Expand All @@ -157,6 +168,28 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
expect(receivedBool, sentBool);
}
});

testWidgets('strings serialize and deserialize correctly',
(WidgetTester _) async {
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
const String sentString = "I'm a computer";
final String receivedString = await api.echoString(sentString);
expect(receivedString, sentString);
});

// TODO(stuartmorgan): Enable these once they work for all generators;
// currently at least Swift is broken.
// TODO(tarrinneal): Finish tests for int lists once issue solved.
// See https://github.com/flutter/flutter/issues/115906
// testWidgets('Uint8List serialize and deserialize correctly',
// (WidgetTester _) async {
// final HostIntegrationCoreApi api = HostIntegrationCoreApi();
// final List<int> data = [102, 111, 114, 116, 121, 45, 116, 119, 111, 0];
// final Uint8List sentUint8List = Uint8List.fromList(data);
// final Uint8List receivedUint8List =
// await api.echoUint8List(sentUint8List);
// expect(receivedUint8List, sentUint8List);
// });
});

group('Host async API tests', () {
Expand Down
Loading