import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:todo/ui/attachment_picking.dart'; void main() { testWidgets('the button passes picked files on, ignores a cancel', ( tester, ) async { final got = >[]; var result = []; await tester.pumpWidget( MaterialApp( home: Scaffold( body: AttachButton(onFiles: got.add, picking: (_) async => result), ), ), ); await tester.tap(find.bySemanticsLabel('Attach')); await tester.pump(); expect(got, isEmpty); result = [XFile('/picked/a.pdf')]; await tester.tap(find.bySemanticsLabel('Attach')); await tester.pump(); expect(got.single.single.name, 'a.pdf'); }); Future?> openSheet( WidgetTester tester, List asked, ) async { List? result; await tester.pumpWidget( MaterialApp( home: Scaffold( body: Builder( builder: (context) => TextButton( onPressed: () async => result = await pickAttachments( context, fromPhone: (source) async { asked.add(source); return [XFile('/picked/${source.name}')]; }, ), child: const Text('pick'), ), ), ), ), ); await tester.tap(find.text('pick')); await tester.pumpAndSettle(); return result; } Future choose(WidgetTester tester, String entry) async { await tester.tap(find.text('pick')); await tester.pumpAndSettle(); await tester.tap(find.bySemanticsLabel(entry)); await tester.pumpAndSettle(); } testWidgets('the phone sheet offers Camera, Gallery and File', ( tester, ) async { final asked = []; await openSheet(tester, asked); expect(find.bySemanticsLabel('Camera'), findsOneWidget); expect(find.bySemanticsLabel('File'), findsOneWidget); await tester.tap(find.bySemanticsLabel('Gallery')); await tester.pumpAndSettle(); await choose(tester, 'Camera'); await choose(tester, 'File'); expect(asked, [ AttachmentPickSource.gallery, AttachmentPickSource.camera, AttachmentPickSource.file, ]); }); testWidgets('dismissing the sheet picks nothing', (tester) async { final asked = []; await openSheet(tester, asked); await tester.tapAt(const Offset(10, 10)); await tester.pumpAndSettle(); expect(asked, isEmpty); }); }