diff --git a/pkgs/ffigen/test/native_objc_test/event_order_config.yaml b/pkgs/ffigen/test/native_objc_test/event_order_config.yaml new file mode 100644 index 000000000..cfd4878d5 --- /dev/null +++ b/pkgs/ffigen/test/native_objc_test/event_order_config.yaml @@ -0,0 +1,18 @@ +name: EventOrderTestObjCLibrary +description: 'Tests the ordering of listener callback events' +language: objc +output: + bindings: 'event_order_bindings.dart' + objc-bindings: 'event_order_bindings.m' +exclude-all-by-default: true +objc-interfaces: + include: + - EventOrderTest +objc-protocols: + include: + - EventOrderProtocol +headers: + entry-points: + - 'event_order_test.h' +preamble: | + // ignore_for_file: camel_case_types, non_constant_identifier_names, unnecessary_non_null_assertion, unused_element, unused_field diff --git a/pkgs/ffigen/test/native_objc_test/event_order_test.dart b/pkgs/ffigen/test/native_objc_test/event_order_test.dart new file mode 100644 index 000000000..d5a9f68ff --- /dev/null +++ b/pkgs/ffigen/test/native_objc_test/event_order_test.dart @@ -0,0 +1,52 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Objective C support is only available on mac. +@TestOn('mac-os') + +import 'dart:ffi'; +import 'dart:io'; + +import 'package:ffi/ffi.dart'; +import 'package:test/test.dart'; +import '../test_utils.dart'; +import 'event_order_bindings.dart'; +import 'util.dart'; + +void main() { + group('event ordering', () { + setUpAll(() { + // TODO(https://github.com/dart-lang/native/issues/1068): Remove this. + DynamicLibrary.open('../objective_c/test/objective_c.dylib'); + final dylib = File('test/native_objc_test/objc_test.dylib'); + verifySetupFile(dylib); + DynamicLibrary.open(dylib.absolute.path); + generateBindingsForCoverage('event_order'); + }); + + test('Many listener calls with different signatures', () async { + final result = []; + + final protocol = EventOrderProtocol.implementAsListener( + method1_y_: (x, _) => result.add(x), + method2_y_: (x, _) => result.add(x), + method3_y_: (x, _) => result.add(x), + method4_y_: (x, _) => result.add(x), + method5_y_: (x, _) => result.add(x), + method6_y_: (x, _) => result.add(x), + method7_y_: (x, _) => result.add(x), + method8_y_: (x, _) => result.add(x), + method9_y_: (x, _) => result.add(x), + method10_y_: (x, _) => result.add(x), + ); + + EventOrderTest.countTo1000OnNewThread_(protocol); + + while (result.length < 1000) { + await Future.delayed(Duration(milliseconds: 10)); + } + expect(result, [ for (int i = 1; i <= 1000; ++i) i ]); + }); + }); +} diff --git a/pkgs/ffigen/test/native_objc_test/event_order_test.h b/pkgs/ffigen/test/native_objc_test/event_order_test.h new file mode 100644 index 000000000..91ab5b795 --- /dev/null +++ b/pkgs/ffigen/test/native_objc_test/event_order_test.h @@ -0,0 +1,26 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#import + +@protocol EventOrderProtocol +@required +- (void) method1:(int32_t)x y:(int8_t)y; +- (void) method2:(int32_t)x y:(int16_t)y; +- (void) method3:(int32_t)x y:(int32_t)y; +- (void) method4:(int32_t)x y:(int64_t)y; +- (void) method5:(int32_t)x y:(uint8_t)y; +- (void) method6:(int32_t)x y:(uint16_t)y; +- (void) method7:(int32_t)x y:(uint32_t)y; +- (void) method8:(int32_t)x y:(uint64_t)y; +- (void) method9:(int32_t)x y:(double)y; +- (void) method10:(int32_t)x y:(float)y; +@end + +@interface EventOrderTest { +} + ++(void)countTo1000OnNewThread: (id) protocol; + +@end diff --git a/pkgs/ffigen/test/native_objc_test/event_order_test.m b/pkgs/ffigen/test/native_objc_test/event_order_test.m new file mode 100644 index 000000000..c32ac6cf5 --- /dev/null +++ b/pkgs/ffigen/test/native_objc_test/event_order_test.m @@ -0,0 +1,26 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "event_order_test.h" + +@implementation EventOrderTest + ++(void)countTo1000OnNewThread: (id) protocol { + return [[[NSThread alloc] initWithBlock:^{ + for (int32_t i = 0; i < 1000; i += 10) { + [protocol method1: i + 1 y: 0]; + [protocol method2: i + 2 y: 0]; + [protocol method3: i + 3 y: 0]; + [protocol method4: i + 4 y: 0]; + [protocol method5: i + 5 y: 0]; + [protocol method6: i + 6 y: 0]; + [protocol method7: i + 7 y: 0]; + [protocol method8: i + 8 y: 0]; + [protocol method9: i + 9 y: 0]; + [protocol method10: i + 10 y: 0]; + } + }] start]; +} + +@end