|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Objective C support is only available on mac. |
| 6 | +@TestOn('mac-os') |
| 7 | + |
| 8 | +import 'dart:async'; |
| 9 | +import 'dart:ffi'; |
| 10 | +import 'dart:io'; |
| 11 | + |
| 12 | +import 'package:objective_c/objective_c.dart'; |
| 13 | +import 'package:test/test.dart'; |
| 14 | + |
| 15 | +import '../test_utils.dart'; |
| 16 | +import 'protocol_bindings.dart'; |
| 17 | +import 'util.dart'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + group('protocol', () { |
| 21 | + setUpAll(() { |
| 22 | + logWarnings(); |
| 23 | + // TODO(https://github.com/dart-lang/native/issues/1068): Remove this. |
| 24 | + DynamicLibrary.open('../objective_c/test/objective_c.dylib'); |
| 25 | + final dylib = File('test/native_objc_test/protocol_test.dylib'); |
| 26 | + verifySetupFile(dylib); |
| 27 | + DynamicLibrary.open(dylib.absolute.path); |
| 28 | + generateBindingsForCoverage('protocol'); |
| 29 | + }); |
| 30 | + |
| 31 | + group('ObjC implementation', () { |
| 32 | + test('Method implementation', () { |
| 33 | + final protoImpl = ObjCProtocolImpl.new1(); |
| 34 | + final consumer = ProtocolConsumer.new1(); |
| 35 | + |
| 36 | + // Required instance method. |
| 37 | + final result = consumer.callInstanceMethod_(protoImpl); |
| 38 | + expect(result.toString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14'); |
| 39 | + |
| 40 | + // Optional instance method. |
| 41 | + final intResult = consumer.callOptionalMethod_(protoImpl); |
| 42 | + expect(intResult, 579); |
| 43 | + |
| 44 | + // Required instance method from secondary protocol. |
| 45 | + final otherIntResult = consumer.callOtherMethod_(protoImpl); |
| 46 | + expect(otherIntResult, 10); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Unimplemented method', () { |
| 50 | + final protoImpl = ObjCProtocolImplMissingMethod.new1(); |
| 51 | + final consumer = ProtocolConsumer.new1(); |
| 52 | + |
| 53 | + // Optional instance method, not implemented. |
| 54 | + final intResult = consumer.callOptionalMethod_(protoImpl); |
| 55 | + expect(intResult, -999); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + group('Manual DartProxy implementation', () { |
| 60 | + test('Method implementation', () { |
| 61 | + final proxyBuilder = DartProxyBuilder.new1(); |
| 62 | + final consumer = ProtocolConsumer.new1(); |
| 63 | + final proto = getProtocol('MyProtocol'); |
| 64 | + final secondProto = getProtocol('SecondaryProtocol'); |
| 65 | + |
| 66 | + final sel = registerName('instanceMethod:withDouble:'); |
| 67 | + final signature = getProtocolMethodSignature(proto, sel, |
| 68 | + isRequired: true, isInstance: true); |
| 69 | + final block = DartInstanceMethodBlock.fromFunction( |
| 70 | + (Pointer<Void> p, NSString s, double x) { |
| 71 | + return 'DartProxy: $s: $x'.toNSString(); |
| 72 | + }); |
| 73 | + proxyBuilder.implementMethod_withSignature_andBlock_( |
| 74 | + sel, signature!, block.pointer.cast()); |
| 75 | + |
| 76 | + final optSel = registerName('optionalMethod:'); |
| 77 | + final optSignature = getProtocolMethodSignature(proto, optSel, |
| 78 | + isRequired: false, isInstance: true); |
| 79 | + final optBlock = |
| 80 | + DartOptMethodBlock.fromFunction((Pointer<Void> p, SomeStruct s) { |
| 81 | + return s.y - s.x; |
| 82 | + }); |
| 83 | + proxyBuilder.implementMethod_withSignature_andBlock_( |
| 84 | + optSel, optSignature!, optBlock.pointer.cast()); |
| 85 | + |
| 86 | + final otherSel = registerName('otherMethod:b:c:d:'); |
| 87 | + final otherSignature = getProtocolMethodSignature(secondProto, otherSel, |
| 88 | + isRequired: true, isInstance: true); |
| 89 | + final otherBlock = DartOtherMethodBlock.fromFunction( |
| 90 | + (Pointer<Void> p, int a, int b, int c, int d) { |
| 91 | + return a * b * c * d; |
| 92 | + }); |
| 93 | + proxyBuilder.implementMethod_withSignature_andBlock_( |
| 94 | + otherSel, otherSignature!, otherBlock.pointer.cast()); |
| 95 | + |
| 96 | + final proxy = DartProxy.newFromBuilder_(proxyBuilder); |
| 97 | + |
| 98 | + // Required instance method. |
| 99 | + final result = consumer.callInstanceMethod_(proxy); |
| 100 | + expect(result.toString(), "DartProxy: Hello from ObjC: 3.14"); |
| 101 | + |
| 102 | + // Optional instance method. |
| 103 | + final intResult = consumer.callOptionalMethod_(proxy); |
| 104 | + expect(intResult, 333); |
| 105 | + |
| 106 | + // Required instance method from secondary protocol. |
| 107 | + final otherIntResult = consumer.callOtherMethod_(proxy); |
| 108 | + expect(otherIntResult, 24); |
| 109 | + }); |
| 110 | + |
| 111 | + test('Unimplemented method', () { |
| 112 | + final proxyBuilder = DartProxyBuilder.new1(); |
| 113 | + final consumer = ProtocolConsumer.new1(); |
| 114 | + final proxy = DartProxy.newFromBuilder_(proxyBuilder); |
| 115 | + |
| 116 | + // Optional instance method, not implemented. |
| 117 | + final intResult = consumer.callOptionalMethod_(proxy); |
| 118 | + expect(intResult, -999); |
| 119 | + }); |
| 120 | + |
| 121 | + test('Threading stress test', () async { |
| 122 | + final proxyBuilder = DartProxyBuilder.new1(); |
| 123 | + final consumer = ProtocolConsumer.new1(); |
| 124 | + final proto = getProtocol('MyProtocol'); |
| 125 | + final completer = Completer<void>(); |
| 126 | + int count = 0; |
| 127 | + |
| 128 | + final sel = registerName('voidMethod:'); |
| 129 | + final signature = getProtocolMethodSignature(proto, sel, |
| 130 | + isRequired: false, isInstance: true); |
| 131 | + final block = DartVoidMethodBlock.listener((Pointer<Void> p, int x) { |
| 132 | + expect(x, 123); |
| 133 | + ++count; |
| 134 | + if (count == 1000) completer.complete(); |
| 135 | + }); |
| 136 | + proxyBuilder.implementMethod_withSignature_andBlock_( |
| 137 | + sel, signature!, block.pointer.cast()); |
| 138 | + |
| 139 | + final proxy = DartProxy.newFromBuilder_(proxyBuilder); |
| 140 | + |
| 141 | + for (int i = 0; i < 1000; ++i) { |
| 142 | + consumer.callMethodOnRandomThread_(proxy); |
| 143 | + } |
| 144 | + await completer.future; |
| 145 | + expect(count, 1000); |
| 146 | + }); |
| 147 | + |
| 148 | + (DartProxy, Pointer<ObjCBlock>) blockRefCountTestInner() { |
| 149 | + final proxyBuilder = DartProxyBuilder.new1(); |
| 150 | + final proto = getProtocol('MyProtocol'); |
| 151 | + |
| 152 | + final sel = registerName('instanceMethod:withDouble:'); |
| 153 | + final signature = getProtocolMethodSignature(proto, sel, |
| 154 | + isRequired: true, isInstance: true); |
| 155 | + final block = DartInstanceMethodBlock.fromFunction( |
| 156 | + (Pointer<Void> p, NSString s, double x) => 'Hello'.toNSString()); |
| 157 | + proxyBuilder.implementMethod_withSignature_andBlock_( |
| 158 | + sel, signature!, block.pointer.cast()); |
| 159 | + |
| 160 | + final proxy = DartProxy.newFromBuilder_(proxyBuilder); |
| 161 | + |
| 162 | + final proxyPtr = proxy.pointer; |
| 163 | + final blockPtr = block.pointer; |
| 164 | + |
| 165 | + // There are 2 references to the block. One owned by the Dart wrapper |
| 166 | + // object, and the other owned by the proxy. The method signature is |
| 167 | + // also an ObjC object, so the same is true for it. |
| 168 | + doGC(); |
| 169 | + expect(objectRetainCount(proxyPtr), 1); |
| 170 | + expect(blockRetainCount(blockPtr), 2); |
| 171 | + |
| 172 | + return (proxy, blockPtr); |
| 173 | + } |
| 174 | + |
| 175 | + (Pointer<ObjCObject>, Pointer<ObjCBlock>) blockRefCountTest() { |
| 176 | + final (proxy, blockPtr) = blockRefCountTestInner(); |
| 177 | + final proxyPtr = proxy.pointer; |
| 178 | + |
| 179 | + // The Dart side block pointer has gone out of scope, but the proxy |
| 180 | + // still owns a reference to it. Same for the signature. |
| 181 | + doGC(); |
| 182 | + expect(objectRetainCount(proxyPtr), 1); |
| 183 | + expect(blockRetainCount(blockPtr), 1); |
| 184 | + |
| 185 | + return (proxyPtr, blockPtr); |
| 186 | + } |
| 187 | + |
| 188 | + test('Block ref counting', () { |
| 189 | + final (proxyPtr, blockPtr) = blockRefCountTest(); |
| 190 | + |
| 191 | + // The proxy object has gone out of scope, so it should be cleaned up. |
| 192 | + // So should the block and the signature. |
| 193 | + doGC(); |
| 194 | + expect(objectRetainCount(proxyPtr), 0); |
| 195 | + expect(blockRetainCount(blockPtr), 0); |
| 196 | + }); |
| 197 | + }); |
| 198 | + }); |
| 199 | +} |
0 commit comments