Skip to content
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

WIP: [ffigen] Event ordering test #1781

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 18 additions & 0 deletions pkgs/ffigen/test/native_objc_test/event_order_config.yaml
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions pkgs/ffigen/test/native_objc_test/event_order_test.dart
Original file line number Diff line number Diff line change
@@ -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 = <int>[];

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 ]);
});
});
}
26 changes: 26 additions & 0 deletions pkgs/ffigen/test/native_objc_test/event_order_test.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/NSThread.h>

@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<EventOrderProtocol>) protocol;

@end
26 changes: 26 additions & 0 deletions pkgs/ffigen/test/native_objc_test/event_order_test.m
Original file line number Diff line number Diff line change
@@ -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<EventOrderProtocol>) 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
Loading