import 'dart:async'; import 'package:cutdex_native/cutdex_native.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); const manager = MethodChannel('cutdex_native/file_drop'); const destination = MethodChannel('cutdex_native/file_drop/test'); final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; final calls = []; setUp(() { calls.clear(); messenger.setMockMethodCallHandler(manager, (_) async => null); messenger.setMockMethodCallHandler(destination, (call) async { calls.add(call); return null; }); }); tearDown(() { messenger.setMockMethodCallHandler(manager, null); messenger.setMockMethodCallHandler(destination, null); }); Future event( String phase, int session, { String channelName = 'cutdex_native/file_drop/test', }) async { await messenger.handlePlatformMessage( channelName, const StandardMethodCodec().encodeMethodCall( MethodCall('event', { 'phase': phase, 'session': session, 'x': 12.0, 'y': 8.0, 'files': [ {'path': '/example/input.wav'}, ], }), ), (_) {}, ); } testWidgets( 'inactive routes reject late drag events and release their grants', (tester) async { var entered = 0; var dropped = 0; await tester.pumpWidget( MaterialApp( home: TickerMode( enabled: false, child: NativeFileDropRegion( regionId: 'test', enabled: true, onEnter: (_) => entered++, onExit: () {}, onDrop: (_) async => dropped++, child: const SizedBox.expand(), ), ), ), ); await tester.pumpAndSettle(); await event('enter', 7); await event('drop', 7); await tester.pump(); expect(entered, 0); expect(dropped, 0); expect(calls.where((c) => c.method == 'release'), hasLength(1)); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }, ); testWidgets( 'equivalent policies at fixed bounds do not rewrite native state', (tester) async { Widget target() => MaterialApp( home: NativeFileDropRegion( regionId: 'test', enabled: true, configuration: NativeDropConfiguration( allowedExtensions: ['wav'], maximumFileCount: 1, ), onEnter: (_) {}, onExit: () {}, onDrop: (_) async {}, child: const SizedBox.expand(), ), ); await tester.pumpWidget(target()); await tester.pumpAndSettle(); final reports = calls.where((c) => c.method == 'setRegion').length; await tester.pumpWidget(target()); await tester.pumpAndSettle(); expect(calls.where((c) => c.method == 'setRegion'), hasLength(reports)); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }, ); testWidgets('a completed drop cannot erase the next drag session', ( tester, ) async { final pending = Completer(); var dropped = 0; await tester.pumpWidget( MaterialApp( home: NativeFileDropRegion( regionId: 'test', enabled: true, onEnter: (_) {}, onExit: () {}, onDrop: (_) async { if (++dropped == 1) await pending.future; }, child: const SizedBox.expand(), ), ), ); await tester.pumpAndSettle(); await event('enter', 7); unawaited(event('drop', 7)); await tester.pump(); await event('enter', 8); pending.complete(); await tester.pump(); await event('drop', 8); await tester.pump(); expect(dropped, 2); expect( calls .where((c) => c.method == 'release') .map((c) => (c.arguments as Map)['session']), [7, 8], ); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }); testWidgets('a disposed connection cannot steal a replacement binding', ( tester, ) async { final first = NativeFileDropConnection( regionId: 'test', onEvent: (_) async {}, ); final events = []; final second = NativeFileDropConnection( regionId: 'test', onEvent: (e) async => events.add(e), ); first.bind(); first.dispose(); await tester.pump(); second.bind(); expect(first.bind, throwsStateError); await event('move', 8); expect(events, hasLength(1)); second.dispose(); await tester.pump(); }); testWidgets('a stale connection cannot change native destination options', ( tester, ) async { final registrations = []; messenger.setMockMethodCallHandler(manager, (call) async { registrations.add(call); return null; }); final first = NativeFileDropConnection( regionId: 'test', hidesSourceImage: true, onEvent: (_) async {}, ); final second = NativeFileDropConnection( regionId: 'test', onEvent: (_) async {}, ); first.bind(); second.bind(); await second.setRegion( const Rect.fromLTWH(0, 0, 200, 100), NativeDropConfiguration.unrestricted, ); await first.setRegion( const Rect.fromLTWH(0, 0, 100, 100), NativeDropConfiguration.unrestricted, ); first.dispose(); expect(registrations, hasLength(1)); expect((registrations.single.arguments as Map)['hidesSourceImage'], false); second.dispose(); await tester.pump(); }); testWidgets( 'security grant survives disposal until the drop callback finishes', (tester) async { final pending = Completer(); var dropped = 0; await tester.pumpWidget( MaterialApp( home: NativeFileDropRegion( regionId: 'test', enabled: true, onEnter: (_) {}, onExit: () {}, onDrop: (paths) async { expect(paths, ['/example/input.wav']); dropped++; await pending.future; }, child: const SizedBox.expand(), ), ), ); await tester.pumpAndSettle(); await event('enter', 7); unawaited(event('drop', 7)); await tester.pump(); expect(dropped, 1); expect(calls.where((call) => call.method == 'release'), isEmpty); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); expect(calls.where((call) => call.method == 'release'), isEmpty); pending.complete(); await tester.pump(); expect(calls.where((call) => call.method == 'release'), hasLength(1)); expect((calls.last.arguments as Map)['session'], 7); }, ); testWidgets('late registration cannot clear a replacement destination', ( tester, ) async { final registrations = >[]; messenger.setMockMethodCallHandler(manager, (call) async { expect((call.arguments as Map)['id'], 'test'); final pending = Completer(); registrations.add(pending); await pending.future; return null; }); final first = NativeFileDropConnection( regionId: 'test', onEvent: (_) async {}, ); final events = []; final second = NativeFileDropConnection( regionId: 'test', onEvent: (event) async => events.add(event), ); first.bind(); final oldReport = first.setRegion( const Rect.fromLTWH(0, 0, 100, 100), NativeDropConfiguration.unrestricted, ); await tester.pump(); second.bind(); first.dispose(); final newReport = second.setRegion( const Rect.fromLTWH(0, 0, 200, 100), NativeDropConfiguration.unrestricted, ); await tester.pump(); for (final pending in registrations) { pending.complete(); } await tester.pump(); await Future.wait([oldReport, newReport]); expect(calls.where((call) => call.method == 'setRegion'), hasLength(1)); expect((calls.single.arguments as Map)['width'], 200.0); await event('move', 8); expect(events.single.position, const Offset(12, 8)); expect(events.single.phase, NativeFileDropPhase.move); messenger.setMockMethodCallHandler(manager, (_) async => null); second.dispose(); await tester.pump(); }); testWidgets( 'changing destinations preserves a pending drop on its original channel', (tester) async { const other = MethodChannel('cutdex_native/file_drop/other'); final otherCalls = []; messenger.setMockMethodCallHandler(other, (call) async { otherCalls.add(call); return null; }); addTearDown(() => messenger.setMockMethodCallHandler(other, null)); final pending = Completer(); Widget target(String id) => MaterialApp( home: NativeFileDropRegion( regionId: id, enabled: true, onEnter: (_) {}, onExit: () {}, onDrop: (_) async { if (id == 'test') await pending.future; }, child: const SizedBox.expand(), ), ); await tester.pumpWidget(target('test')); await tester.pumpAndSettle(); await event('enter', 7); unawaited(event('drop', 7)); await tester.pump(); await tester.pumpWidget(target('other')); await tester.pumpAndSettle(); expect(calls.where((call) => call.method == 'release'), isEmpty); await event('enter', 8, channelName: other.name); await event('drop', 8, channelName: other.name); await tester.pump(); expect( otherCalls.where((call) => call.method == 'release'), hasLength(1), ); pending.complete(); await tester.pump(); expect((calls.last.arguments as Map)['session'], 7); expect( otherCalls.where((call) => call.method == 'release'), hasLength(1), ); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }, ); }