@@ -98,6 +98,19 @@ class _FakeClosedCaptionFile extends ClosedCaptionFile {
9898}
9999
100100void main () {
101+ void _verifyPlayStateRespondsToLifecycle (
102+ VideoPlayerController controller, {
103+ required bool shouldPlayInBackground,
104+ }) {
105+ expect (controller.value.isPlaying, true );
106+ _ambiguate (WidgetsBinding .instance)!
107+ .handleAppLifecycleStateChanged (AppLifecycleState .paused);
108+ expect (controller.value.isPlaying, shouldPlayInBackground);
109+ _ambiguate (WidgetsBinding .instance)!
110+ .handleAppLifecycleStateChanged (AppLifecycleState .resumed);
111+ expect (controller.value.isPlaying, true );
112+ }
113+
101114 testWidgets ('update texture' , (WidgetTester tester) async {
102115 final FakeController controller = FakeController ();
103116 await tester.pumpWidget (VideoPlayer (controller));
@@ -194,6 +207,16 @@ void main() {
194207 });
195208
196209 group ('initialize' , () {
210+ test ('started app lifecycle observing' , () async {
211+ final VideoPlayerController controller = VideoPlayerController .network (
212+ 'https://127.0.0.1' ,
213+ );
214+ await controller.initialize ();
215+ await controller.play ();
216+ _verifyPlayStateRespondsToLifecycle (controller,
217+ shouldPlayInBackground: false );
218+ });
219+
197220 test ('asset' , () async {
198221 final VideoPlayerController controller = VideoPlayerController .asset (
199222 'a.avi' ,
@@ -900,6 +923,46 @@ void main() {
900923 });
901924 });
902925
926+ group ('VideoPlayerOptions' , () {
927+ test ('setMixWithOthers' , () async {
928+ final VideoPlayerController controller = VideoPlayerController .file (
929+ File ('' ),
930+ videoPlayerOptions: VideoPlayerOptions (mixWithOthers: true ));
931+ await controller.initialize ();
932+ expect (controller.videoPlayerOptions! .mixWithOthers, true );
933+ });
934+
935+ test ('true allowBackgroundPlayback continues playback' , () async {
936+ final VideoPlayerController controller = VideoPlayerController .file (
937+ File ('' ),
938+ videoPlayerOptions: VideoPlayerOptions (
939+ allowBackgroundPlayback: true ,
940+ ),
941+ );
942+ await controller.initialize ();
943+ await controller.play ();
944+ _verifyPlayStateRespondsToLifecycle (
945+ controller,
946+ shouldPlayInBackground: true ,
947+ );
948+ });
949+
950+ test ('false allowBackgroundPlayback pauses playback' , () async {
951+ final VideoPlayerController controller = VideoPlayerController .file (
952+ File ('' ),
953+ videoPlayerOptions: VideoPlayerOptions (
954+ allowBackgroundPlayback: false ,
955+ ),
956+ );
957+ await controller.initialize ();
958+ await controller.play ();
959+ _verifyPlayStateRespondsToLifecycle (
960+ controller,
961+ shouldPlayInBackground: false ,
962+ );
963+ });
964+ });
965+
903966 test ('VideoProgressColors' , () {
904967 const Color playedColor = Color .fromRGBO (0 , 0 , 255 , 0.75 );
905968 const Color bufferedColor = Color .fromRGBO (0 , 255 , 0 , 0.5 );
@@ -914,14 +977,6 @@ void main() {
914977 expect (colors.bufferedColor, bufferedColor);
915978 expect (colors.backgroundColor, backgroundColor);
916979 });
917-
918- test ('setMixWithOthers' , () async {
919- final VideoPlayerController controller = VideoPlayerController .file (
920- File ('' ),
921- videoPlayerOptions: VideoPlayerOptions (mixWithOthers: true ));
922- await controller.initialize ();
923- expect (controller.videoPlayerOptions! .mixWithOthers, true );
924- });
925980}
926981
927982class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
@@ -1010,3 +1065,9 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
10101065 calls.add ('setMixWithOthers' );
10111066 }
10121067}
1068+
1069+ /// This allows a value of type T or T? to be treated as a value of type T?.
1070+ ///
1071+ /// We use this so that APIs that have become non-nullable can still be used
1072+ /// with `!` and `?` on the stable branch.
1073+ T ? _ambiguate <T >(T ? value) => value;
0 commit comments