@@ -134,6 +134,21 @@ void main() {
134
134
});
135
135
});
136
136
137
+ test ('init errors' , () async {
138
+ final VideoPlayerController controller = VideoPlayerController .network (
139
+ 'http://testing.com/invalid_url' ,
140
+ );
141
+ try {
142
+ dynamic error;
143
+ fakeVideoPlayerPlatform.forceInitError = true ;
144
+ await controller.initialize ().catchError ((dynamic e) => error = e);
145
+ final PlatformException platformEx = error;
146
+ expect (platformEx.code, equals ('VideoError' ));
147
+ } finally {
148
+ fakeVideoPlayerPlatform.forceInitError = false ;
149
+ }
150
+ });
151
+
137
152
test ('file' , () async {
138
153
final VideoPlayerController controller =
139
154
VideoPlayerController .file (File ('a.avi' ));
@@ -437,6 +452,7 @@ class FakeVideoPlayerPlatform {
437
452
List <MethodCall > calls = < MethodCall > [];
438
453
List <Map <String , dynamic >> dataSourceDescriptions = < Map <String , dynamic >> [];
439
454
final Map <int , FakeVideoEventStream > streams = < int , FakeVideoEventStream > {};
455
+ bool forceInitError = false ;
440
456
int nextTextureId = 0 ;
441
457
final Map <int , Duration > _positions = < int , Duration > {};
442
458
@@ -447,8 +463,8 @@ class FakeVideoPlayerPlatform {
447
463
initialized.complete (true );
448
464
break ;
449
465
case 'create' :
450
- streams[nextTextureId] = FakeVideoEventStream (
451
- nextTextureId, 100 , 100 , const Duration (seconds: 1 ));
466
+ streams[nextTextureId] = FakeVideoEventStream (nextTextureId, 100 , 100 ,
467
+ const Duration (seconds: 1 ), forceInitError );
452
468
final Map <dynamic , dynamic > dataSource = call.arguments;
453
469
dataSourceDescriptions.add (dataSource.cast <String , dynamic >());
454
470
return Future <Map <String , int >>.sync (() {
@@ -481,7 +497,8 @@ class FakeVideoPlayerPlatform {
481
497
}
482
498
483
499
class FakeVideoEventStream {
484
- FakeVideoEventStream (this .textureId, this .width, this .height, this .duration) {
500
+ FakeVideoEventStream (this .textureId, this .width, this .height, this .duration,
501
+ this .initWithError) {
485
502
eventsChannel = FakeEventsChannel (
486
503
'flutter.io/videoPlayer/videoEvents$textureId ' , onListen);
487
504
}
@@ -490,16 +507,20 @@ class FakeVideoEventStream {
490
507
int width;
491
508
int height;
492
509
Duration duration;
510
+ bool initWithError;
493
511
FakeEventsChannel eventsChannel;
494
512
495
513
void onListen () {
496
- final Map <String , dynamic > initializedEvent = < String , dynamic > {
497
- 'event' : 'initialized' ,
498
- 'duration' : duration.inMilliseconds,
499
- 'width' : width,
500
- 'height' : height,
501
- };
502
- eventsChannel.sendEvent (initializedEvent);
514
+ if (! initWithError) {
515
+ eventsChannel.sendEvent (< String , dynamic > {
516
+ 'event' : 'initialized' ,
517
+ 'duration' : duration.inMilliseconds,
518
+ 'width' : width,
519
+ 'height' : height,
520
+ });
521
+ } else {
522
+ eventsChannel.sendError ('VideoError' , 'Video player had error XYZ' );
523
+ }
503
524
}
504
525
}
505
526
@@ -522,13 +543,23 @@ class FakeEventsChannel {
522
543
}
523
544
524
545
void sendEvent (dynamic event) {
546
+ _sendMessage (const StandardMethodCodec ().encodeSuccessEnvelope (event));
547
+ }
548
+
549
+ void sendError (String code, [String message, dynamic details]) {
550
+ _sendMessage (const StandardMethodCodec ().encodeErrorEnvelope (
551
+ code: code,
552
+ message: message,
553
+ details: details,
554
+ ));
555
+ }
556
+
557
+ void _sendMessage (ByteData data) {
525
558
// TODO(jackson): This has been deprecated and should be replaced
526
559
// with `ServicesBinding.instance.defaultBinaryMessenger` when it's
527
560
// available on all the versions of Flutter that we test.
528
561
// ignore: deprecated_member_use
529
562
defaultBinaryMessenger.handlePlatformMessage (
530
- eventsMethodChannel.name,
531
- const StandardMethodCodec ().encodeSuccessEnvelope (event),
532
- (ByteData data) {});
563
+ eventsMethodChannel.name, data, (ByteData data) {});
533
564
}
534
565
}
0 commit comments