@@ -2,6 +2,7 @@ import 'dart:convert';
22import 'dart:async' ;
33import 'dart:io' ;
44import 'dart:ui' as ui;
5+
56import 'package:flutter/services.dart' ;
67
78import 'package:buzz/features/channels/native_photo_zoom_preview.dart' ;
@@ -483,118 +484,138 @@ void main() {
483484 debugDefaultTargetPlatformOverride = null ;
484485 }, skip: ! NativePhotoZoomPreview .enabled);
485486
486- for (final usePoster in [false , true ]) {
487- testWidgets (
488- 'video uses ${ usePoster ? ' poster' : 'snapshot' } and only loads the selected video' ,
489- ( tester) async {
490- debugDefaultTargetPlatformOverride = TargetPlatform .iOS;
491- final key = GlobalKey () ;
492- late BuildContext context ;
493- await tester. pumpWidget (
494- MaterialApp (
495- home : Builder (
496- builder : (value) {
497- context = value;
498- return Center (
499- child : RepaintBoundary (
500- key : key,
501- child : const SizedBox (
502- width : 100 ,
503- height : 100 ,
504- child : ColoredBox (color : Colors .black) ,
505- ),
487+ for (final posterMode in ['none' , 'cached' , 'pending' ]) {
488+ final usePoster = posterMode != 'none' ;
489+ testWidgets ( 'video uses $posterMode poster without delaying presentation' , (
490+ tester,
491+ ) async {
492+ debugDefaultTargetPlatformOverride = TargetPlatform .iOS ;
493+ final key = GlobalKey () ;
494+ late BuildContext context;
495+ await tester. pumpWidget (
496+ MaterialApp (
497+ home : Builder (
498+ builder : ( value) {
499+ context = value;
500+ return Center (
501+ child : RepaintBoundary (
502+ key : key,
503+ child : const SizedBox (
504+ width : 100 ,
505+ height : 100 ,
506+ child : ColoredBox (color : Colors .black ),
506507 ),
507- );
508- },
509- ) ,
508+ ),
509+ );
510+ } ,
510511 ),
512+ ),
513+ );
514+ final image = _PendingPreview ();
515+ final pendingPoster = _PendingPreview ();
516+ late Completer <void > posterUpdated;
517+ final videos = < int > [];
518+ const codec = StandardMethodCodec ();
519+ Future <Object ?> nativeCall (String method, int session, int index) async {
520+ final reply = Completer <Object ?>();
521+ await messenger.handlePlatformMessage (
522+ NativePhotoZoomPreview .channel.name,
523+ codec.encodeMethodCall (
524+ MethodCall (method, {'session' : session, 'index' : index}),
525+ ),
526+ (data) => reply.complete (codec.decodeEnvelope (data! )),
511527 );
512- final image = _PendingPreview ();
513- final videos = < int > [];
514- const codec = StandardMethodCodec ();
515- Future <Object ?> nativeCall (
516- String method,
517- int session,
518- int index,
519- ) async {
520- final reply = Completer <Object ?>();
521- await messenger.handlePlatformMessage (
522- NativePhotoZoomPreview .channel.name,
523- codec.encodeMethodCall (
524- MethodCall (method, {'session' : session, 'index' : index}),
525- ),
526- (data) => reply.complete (codec.decodeEnvelope (data! )),
527- );
528- return reply.future;
529- }
528+ return reply.future;
529+ }
530530
531- messenger.setMockMethodCallHandler (NativePhotoZoomPreview .channel, (
532- call,
533- ) async {
534- if (call.method == 'isSupported' ) return true ;
535- final args = call.arguments as Map ;
536- expect (args['videoIndices' ], [0 , 2 ]);
537- expect (args['bytes' ], isA <Uint8List >());
538- if (usePoster) {
539- final codec = await ui.instantiateImageCodec (
540- args['bytes' ] as Uint8List ,
541- );
542- final frame = await codec.getNextFrame ();
543- expect (
544- frame.image.width,
545- 2 ,
546- reason: 'Use the poster, not the 100-pixel source snapshot' ,
547- );
548- frame.image.dispose ();
549- codec.dispose ();
550- }
551- final session = args['session' ] as int ;
552- expect (await nativeCall ('image' , session, 0 ), isNull);
553- expect (await nativeCall ('video' , session, 1 ), isNull);
554- expect (await nativeCall ('video' , session, 2 ), isNull);
555- expect (await nativeCall ('video' , session - 1 , 0 ), isNull);
556- expect (await nativeCall ('video' , session, 0 ), '/temporary/clip.mp4' );
557- await nativeCall ('select' , session, 2 );
558- expect (await nativeCall ('video' , session, 0 ), isNull);
559- expect (await nativeCall ('video' , session, 2 ), '/temporary/clip.mp4' );
560- return 'closed' ;
561- });
562- await tester.runAsync (() async {
531+ messenger.setMockMethodCallHandler (NativePhotoZoomPreview .channel, (
532+ call,
533+ ) async {
534+ if (call.method == 'isSupported' ) return true ;
535+ if (call.method == 'imageFrame' ) {
536+ posterUpdated.complete ();
537+ return null ;
538+ }
539+ final args = call.arguments as Map ;
540+ if (posterMode == 'pending' ) {
563541 expect (
564- await NativePhotoZoomPreview .open (
565- context: context,
566- sourceKeys: [key, key, key],
567- providers: [image, provider, image],
568- previewProviders: usePoster
569- ? [provider, provider, provider]
570- : null ,
571- urls: [
572- 'https://example.org/clip.mp4' ,
573- 'https://example.org/photo.jpg' ,
574- 'https://example.org/second.mov' ,
575- ],
576- labels: ['Clip' , 'Photo' , 'Second' ],
577- initialIndex: 0 ,
578- videoIndices: {0 , 2 },
579- loadVideo: (index) async {
580- videos.add (index);
581- return '/temporary/clip.mp4' ;
582- },
583- onSelect: (_) async {},
584- ),
542+ pendingPoster.frame.isCompleted,
585543 isFalse,
544+ reason: 'Presentation must not wait for the remote poster' ,
586545 );
587- });
588- expect (videos, [0 , 2 ]);
546+ final decoder = await ui.instantiateImageCodec (provider.bytes);
547+ pendingPoster.frame.complete (
548+ ImageInfo (image: (await decoder.getNextFrame ()).image),
549+ );
550+ decoder.dispose ();
551+ await posterUpdated.future.timeout (const Duration (seconds: 5 ));
552+ }
553+ expect (args['videoIndices' ], [0 , 2 ]);
554+ expect (args['bytes' ], isA <Uint8List >());
555+ if (posterMode == 'cached' ) {
556+ final codec = await ui.instantiateImageCodec (
557+ args['bytes' ] as Uint8List ,
558+ );
559+ final frame = await codec.getNextFrame ();
560+ expect (
561+ frame.image.width,
562+ 2 ,
563+ reason: 'Use the poster, not the 100-pixel source snapshot' ,
564+ );
565+ frame.image.dispose ();
566+ codec.dispose ();
567+ }
568+ final session = args['session' ] as int ;
569+ expect (await nativeCall ('image' , session, 0 ), isNull);
570+ expect (await nativeCall ('video' , session, 1 ), isNull);
571+ expect (await nativeCall ('video' , session, 2 ), isNull);
572+ expect (await nativeCall ('video' , session - 1 , 0 ), isNull);
573+ expect (await nativeCall ('video' , session, 0 ), '/temporary/clip.mp4' );
574+ await nativeCall ('select' , session, 2 );
575+ expect (await nativeCall ('video' , session, 0 ), isNull);
576+ expect (await nativeCall ('video' , session, 2 ), '/temporary/clip.mp4' );
577+ return 'closed' ;
578+ });
579+ await tester.runAsync (() async {
580+ posterUpdated = Completer <void >();
581+ if (posterMode == 'cached' ) await precacheImage (provider, context);
589582 expect (
590- image.started,
583+ await NativePhotoZoomPreview .open (
584+ context: context,
585+ sourceKeys: [key, key, key],
586+ providers: [image, provider, image],
587+ previewProviders: usePoster
588+ ? [
589+ posterMode == 'pending' ? pendingPoster : provider,
590+ provider,
591+ provider,
592+ ]
593+ : null ,
594+ urls: [
595+ 'https://example.org/clip.mp4' ,
596+ 'https://example.org/photo.jpg' ,
597+ 'https://example.org/second.mov' ,
598+ ],
599+ labels: ['Clip' , 'Photo' , 'Second' ],
600+ initialIndex: 0 ,
601+ videoIndices: {0 , 2 },
602+ loadVideo: (index) async {
603+ videos.add (index);
604+ return '/temporary/clip.mp4' ;
605+ },
606+ onSelect: (_) async {},
607+ ),
591608 isFalse,
592- reason: 'A video URL must never enter the image decoder' ,
593609 );
594- debugDefaultTargetPlatformOverride = null ;
595- },
596- skip: ! NativePhotoZoomPreview .enabled,
597- );
610+ });
611+ expect (videos, [0 , 2 ]);
612+ expect (
613+ image.started,
614+ isFalse,
615+ reason: 'A video URL must never enter the image decoder' ,
616+ );
617+ debugDefaultTargetPlatformOverride = null ;
618+ }, skip: ! NativePhotoZoomPreview .enabled);
598619 }
599620
600621 testWidgets (
0 commit comments