|
| 1 | +// ignore_for_file: implementation_imports |
| 2 | + |
| 3 | +import 'dart:ffi'; |
1 | 4 | import 'dart:io'; |
2 | 5 | import 'dart:isolate'; |
3 | 6 | import 'dart:typed_data'; |
4 | 7 |
|
| 8 | +import 'package:ffi/ffi.dart'; |
5 | 9 | import 'package:flutter_test/flutter_test.dart'; |
6 | 10 | import 'package:opentype_shaper/opentype_shaper.dart'; |
| 11 | +import 'package:opentype_shaper/src/bindings.dart'; |
7 | 12 | import 'package:otzaria/printing/shaped_text/shaper_font_scope.dart'; |
8 | 13 |
|
9 | 14 | bool _isRegistered(int handle) { |
@@ -34,6 +39,16 @@ Future<int> _shapeAfterResume( |
34 | 39 | }, |
35 | 40 | ); |
36 | 41 |
|
| 42 | + |
| 43 | +int _drainUnexpectedNativeReferences(int handle) { |
| 44 | + for (var count = 0; count < 32; count++) { |
| 45 | + final status = ShaperBindings.instance.releaseFont(handle); |
| 46 | + if (status == ShaperStatus.unknownHandle) return count; |
| 47 | + expect(status, ShaperStatus.ok); |
| 48 | + } |
| 49 | + throw StateError('Native font reference count exceeded 32'); |
| 50 | +} |
| 51 | + |
37 | 52 | void main() { |
38 | 53 | late Uint8List bytes; |
39 | 54 |
|
@@ -159,4 +174,101 @@ void main() { |
159 | 174 | ready.close(); |
160 | 175 | } |
161 | 176 | }); |
| 177 | + |
| 178 | + test('native refcount returns to baseline after fallback registration fails', () async { |
| 179 | + final anchor = ShaperFont.register(bytes); |
| 180 | + final handle = anchor.handle; |
| 181 | + try { |
| 182 | + await expectLater( |
| 183 | + withShaperFontPair<void>( |
| 184 | + createPrimary: () => ShaperFont.register(bytes), |
| 185 | + createFallback: () => ShaperFont.register(Uint8List(0)), |
| 186 | + action: (_, _) => fail('must not construct a PDF'), |
| 187 | + ), |
| 188 | + throwsA(isA<ShaperException>()), |
| 189 | + ); |
| 190 | + expect(anchor.shape('בדיקה').glyphCount, greaterThan(0)); |
| 191 | + } finally { |
| 192 | + anchor.dispose(); |
| 193 | + } |
| 194 | + expect(_drainUnexpectedNativeReferences(handle), 0); |
| 195 | + }); |
| 196 | + |
| 197 | + test('fallback constructor failure after native registration releases its ref', () async { |
| 198 | + // Parseable SFNT header with missing tables: native register succeeds, |
| 199 | + // but ShaperFont.register throws while reading metrics in _attach. |
| 200 | + final malformed = (ByteData(12)..setUint32(0, 0x10000)) |
| 201 | + .buffer |
| 202 | + .asUint8List(); |
| 203 | + final malformedHandle = using((arena) { |
| 204 | + final data = arena<Uint8>(malformed.length); |
| 205 | + data.asTypedList(malformed.length).setAll(0, malformed); |
| 206 | + return ShaperBindings.instance.registerFont(data, malformed.length, 0); |
| 207 | + }); |
| 208 | + expect(malformedHandle, greaterThan(0)); |
| 209 | + |
| 210 | + final validAnchor = ShaperFont.register(bytes); |
| 211 | + final validHandle = validAnchor.handle; |
| 212 | + try { |
| 213 | + await expectLater( |
| 214 | + withShaperFontPair<void>( |
| 215 | + createPrimary: () => ShaperFont.register(bytes), |
| 216 | + createFallback: () => ShaperFont.register(malformed), |
| 217 | + action: (_, _) => fail('must not construct a PDF'), |
| 218 | + ), |
| 219 | + throwsA(isA<ShaperException>()), |
| 220 | + ); |
| 221 | + expect(validAnchor.shape('בדיקה').glyphCount, greaterThan(0)); |
| 222 | + // Exactly one native reference should remain: our raw registration. |
| 223 | + expect( |
| 224 | + ShaperBindings.instance.releaseFont(malformedHandle), |
| 225 | + ShaperStatus.ok, |
| 226 | + ); |
| 227 | + expect( |
| 228 | + ShaperBindings.instance.releaseFont(malformedHandle), |
| 229 | + ShaperStatus.unknownHandle, |
| 230 | + ); |
| 231 | + } finally { |
| 232 | + validAnchor.dispose(); |
| 233 | + while (ShaperBindings.instance.releaseFont(malformedHandle) == |
| 234 | + ShaperStatus.ok) {} |
| 235 | + } |
| 236 | + expect(_drainUnexpectedNativeReferences(validHandle), 0); |
| 237 | + }); |
| 238 | + |
| 239 | + test('native registry is freed after repeated save failures and double dispose', () async { |
| 240 | + for (var cycle = 0; cycle < 16; cycle++) { |
| 241 | + final anchor = ShaperFont.register(bytes); |
| 242 | + final handle = anchor.handle; |
| 243 | + final failure = StateError('pdf.save failed $cycle'); |
| 244 | + try { |
| 245 | + await expectLater( |
| 246 | + withShaperFontPair<void>( |
| 247 | + createPrimary: () => ShaperFont.register(bytes), |
| 248 | + createFallback: () => ShaperFont.register(bytes), |
| 249 | + action: (primary, fallback) async { |
| 250 | + expect(primary.handle, handle); |
| 251 | + expect(fallback.handle, handle); |
| 252 | + if (cycle.isEven) { |
| 253 | + // Both owners may already be disposed by a client. |
| 254 | + primary.dispose(); |
| 255 | + fallback.dispose(); |
| 256 | + } |
| 257 | + await Future<void>.delayed(Duration.zero); |
| 258 | + throw failure; |
| 259 | + }, |
| 260 | + ), |
| 261 | + throwsA(same(failure)), |
| 262 | + ); |
| 263 | + expect(anchor.shape('בדיקה').glyphCount, greaterThan(0)); |
| 264 | + } finally { |
| 265 | + anchor.dispose(); |
| 266 | + } |
| 267 | + expect( |
| 268 | + _drainUnexpectedNativeReferences(handle), |
| 269 | + 0, |
| 270 | + reason: 'native reference leak on failure cycle $cycle', |
| 271 | + ); |
| 272 | + } |
| 273 | + }); |
162 | 274 | } |
0 commit comments