Skip to content

Commit 1963bb3

Browse files
Simplify tests
1 parent e9604dd commit 1963bb3

File tree

1 file changed

+50
-130
lines changed

1 file changed

+50
-130
lines changed

packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.m

Lines changed: 50 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ - (void)testSeekToWhilePausedStartsDisplayLinkTemporarily {
292292
StubFVPDisplayLinkFactory *stubDisplayLinkFactory =
293293
[[StubFVPDisplayLinkFactory alloc] initWithDisplayLink:mockDisplayLink];
294294
AVPlayerItemVideoOutput *mockVideoOutput = OCMPartialMock([[AVPlayerItemVideoOutput alloc] init]);
295+
// Display link and frame updater wire-up is currently done in FVPVideoPlayerPlugin, so create
296+
// the player via the plugin instead of directly to include that logic in the test.
295297
FVPVideoPlayerPlugin *videoPlayerPlugin = [[FVPVideoPlayerPlugin alloc]
296298
initWithAVFactory:[[StubFVPAVFactory alloc] initWithPlayer:nil output:mockVideoOutput]
297299
displayLinkFactory:stubDisplayLinkFactory
@@ -310,8 +312,6 @@ - (void)testSeekToWhilePausedStartsDisplayLinkTemporarily {
310312
viewType:FVPPlatformVideoViewTypeTextureView];
311313
FlutterError *createError;
312314
NSNumber *playerIdentifier = [videoPlayerPlugin createWithOptions:create error:&createError];
313-
314-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
315315
FVPTextureBasedVideoPlayer *player =
316316
(FVPTextureBasedVideoPlayer *)videoPlayerPlugin.playersByIdentifier[playerIdentifier];
317317

@@ -411,6 +411,8 @@ - (void)testSeekToWhilePlayingDoesNotStopDisplayLink {
411411
StubFVPDisplayLinkFactory *stubDisplayLinkFactory =
412412
[[StubFVPDisplayLinkFactory alloc] initWithDisplayLink:mockDisplayLink];
413413
AVPlayerItemVideoOutput *mockVideoOutput = OCMPartialMock([[AVPlayerItemVideoOutput alloc] init]);
414+
// Display link and frame updater wire-up is currently done in FVPVideoPlayerPlugin, so create
415+
// the player via the plugin instead of directly to include that logic in the test.
414416
FVPVideoPlayerPlugin *videoPlayerPlugin = [[FVPVideoPlayerPlugin alloc]
415417
initWithAVFactory:[[StubFVPAVFactory alloc] initWithPlayer:nil output:mockVideoOutput]
416418
displayLinkFactory:stubDisplayLinkFactory
@@ -429,8 +431,6 @@ - (void)testSeekToWhilePlayingDoesNotStopDisplayLink {
429431
viewType:FVPPlatformVideoViewTypeTextureView];
430432
FlutterError *createError;
431433
NSNumber *playerIdentifier = [videoPlayerPlugin createWithOptions:create error:&createError];
432-
433-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
434434
FVPTextureBasedVideoPlayer *player =
435435
(FVPTextureBasedVideoPlayer *)videoPlayerPlugin.playersByIdentifier[playerIdentifier];
436436

@@ -474,6 +474,8 @@ - (void)testPauseWhileWaitingForFrameDoesNotStopDisplayLink {
474474
StubFVPDisplayLinkFactory *stubDisplayLinkFactory =
475475
[[StubFVPDisplayLinkFactory alloc] initWithDisplayLink:mockDisplayLink];
476476
AVPlayerItemVideoOutput *mockVideoOutput = OCMPartialMock([[AVPlayerItemVideoOutput alloc] init]);
477+
// Display link and frame updater wire-up is currently done in FVPVideoPlayerPlugin, so create
478+
// the player via the plugin instead of directly to include that logic in the test.
477479
FVPVideoPlayerPlugin *videoPlayerPlugin = [[FVPVideoPlayerPlugin alloc]
478480
initWithAVFactory:[[StubFVPAVFactory alloc] initWithPlayer:nil output:mockVideoOutput]
479481
displayLinkFactory:stubDisplayLinkFactory
@@ -492,8 +494,6 @@ - (void)testPauseWhileWaitingForFrameDoesNotStopDisplayLink {
492494
viewType:FVPPlatformVideoViewTypeTextureView];
493495
FlutterError *createError;
494496
NSNumber *playerIdentifier = [videoPlayerPlugin createWithOptions:create error:&createError];
495-
496-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
497497
FVPTextureBasedVideoPlayer *player =
498498
(FVPTextureBasedVideoPlayer *)videoPlayerPlugin.playersByIdentifier[playerIdentifier];
499499

@@ -583,59 +583,36 @@ - (void)testBufferingStateFromPlayer {
583583
}
584584

585585
- (void)testVideoControls {
586-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
587-
588-
FVPVideoPlayerPlugin *videoPlayerPlugin =
589-
(FVPVideoPlayerPlugin *)[[FVPVideoPlayerPlugin alloc] initWithRegistrar:registrar];
590-
591586
NSDictionary<NSString *, id> *videoInitialization =
592-
[self testPlugin:videoPlayerPlugin
593-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"];
587+
[self sanityTestURI:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"];
594588
XCTAssertEqualObjects(videoInitialization[@"height"], @720);
595589
XCTAssertEqualObjects(videoInitialization[@"width"], @1280);
596590
XCTAssertEqualWithAccuracy([videoInitialization[@"duration"] intValue], 4000, 200);
597591
}
598592

599593
- (void)testAudioControls {
600-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
601-
602-
FVPVideoPlayerPlugin *videoPlayerPlugin =
603-
(FVPVideoPlayerPlugin *)[[FVPVideoPlayerPlugin alloc] initWithRegistrar:registrar];
604-
605-
NSDictionary<NSString *, id> *audioInitialization =
606-
[self testPlugin:videoPlayerPlugin
607-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/audio/rooster.mp3"];
594+
NSDictionary<NSString *, id> *audioInitialization = [self
595+
sanityTestURI:@"https://flutter.github.io/assets-for-api-docs/assets/audio/rooster.mp3"];
608596
XCTAssertEqualObjects(audioInitialization[@"height"], @0);
609597
XCTAssertEqualObjects(audioInitialization[@"width"], @0);
610598
// Perfect precision not guaranteed.
611599
XCTAssertEqualWithAccuracy([audioInitialization[@"duration"] intValue], 5400, 200);
612600
}
613601

614602
- (void)testHLSControls {
615-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
616-
617-
FVPVideoPlayerPlugin *videoPlayerPlugin =
618-
(FVPVideoPlayerPlugin *)[[FVPVideoPlayerPlugin alloc] initWithRegistrar:registrar];
619-
620-
NSDictionary<NSString *, id> *videoInitialization =
621-
[self testPlugin:videoPlayerPlugin
622-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/hls/bee.m3u8"];
603+
NSDictionary<NSString *, id> *videoInitialization = [self
604+
sanityTestURI:@"https://flutter.github.io/assets-for-api-docs/assets/videos/hls/bee.m3u8"];
623605
XCTAssertEqualObjects(videoInitialization[@"height"], @720);
624606
XCTAssertEqualObjects(videoInitialization[@"width"], @1280);
625607
XCTAssertEqualWithAccuracy([videoInitialization[@"duration"] intValue], 4000, 200);
626608
}
627609

628610
- (void)testAudioOnlyHLSControls {
629611
XCTSkip(@"Flaky; see https://github.com/flutter/flutter/issues/164381");
630-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
631-
632-
FVPVideoPlayerPlugin *videoPlayerPlugin =
633-
(FVPVideoPlayerPlugin *)[[FVPVideoPlayerPlugin alloc] initWithRegistrar:registrar];
634612

635613
NSDictionary<NSString *, id> *videoInitialization =
636-
[self testPlugin:videoPlayerPlugin
637-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/hls/"
638-
@"bee_audio_only.m3u8"];
614+
[self sanityTestURI:@"https://flutter.github.io/assets-for-api-docs/assets/videos/hls/"
615+
@"bee_audio_only.m3u8"];
639616
XCTAssertEqualObjects(videoInitialization[@"height"], @0);
640617
XCTAssertEqualObjects(videoInitialization[@"width"], @0);
641618
XCTAssertEqualWithAccuracy([videoInitialization[@"duration"] intValue], 4000, 200);
@@ -655,40 +632,22 @@ - (void)testTransformFix {
655632
#endif
656633

657634
- (void)testSeekToleranceWhenNotSeekingToEnd {
658-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
659-
660635
StubAVPlayer *stubAVPlayer = [[StubAVPlayer alloc] init];
661636
StubFVPAVFactory *stubAVFactory = [[StubFVPAVFactory alloc] initWithPlayer:stubAVPlayer
662637
output:nil];
663-
FVPVideoPlayerPlugin *pluginWithMockAVPlayer =
664-
[[FVPVideoPlayerPlugin alloc] initWithAVFactory:stubAVFactory
665-
displayLinkFactory:nil
666-
viewProvider:[[StubViewProvider alloc] initWithView:nil]
667-
registrar:registrar];
668-
669-
FlutterError *initializationError;
670-
[pluginWithMockAVPlayer initialize:&initializationError];
671-
XCTAssertNil(initializationError);
672-
673-
FVPCreationOptions *create = [FVPCreationOptions
674-
makeWithAsset:nil
675-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"
676-
packageName:nil
677-
formatHint:nil
678-
httpHeaders:@{}
679-
viewType:FVPPlatformVideoViewTypeTextureView];
680-
FlutterError *createError;
681-
NSNumber *playerIdentifier = [pluginWithMockAVPlayer createWithOptions:create error:&createError];
682-
683-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
684-
FVPTextureBasedVideoPlayer *player =
685-
(FVPTextureBasedVideoPlayer *)pluginWithMockAVPlayer.playersByIdentifier[playerIdentifier];
686-
687-
XCTestExpectation *initializedExpectation =
638+
FVPVideoPlayer *player = [[FVPVideoPlayer alloc]
639+
initWithURL:
640+
[NSURL
641+
URLWithString:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"]
642+
httpHeaders:@{}
643+
avFactory:stubAVFactory
644+
viewProvider:[[StubViewProvider alloc] initWithView:nil]];
645+
646+
XCTestExpectation *seekExpectation =
688647
[self expectationWithDescription:@"seekTo has zero tolerance when seeking not to end"];
689648
[player seekTo:1234
690649
completion:^(FlutterError *_Nullable error) {
691-
[initializedExpectation fulfill];
650+
[seekExpectation fulfill];
692651
}];
693652

694653
[self waitForExpectationsWithTimeout:30.0 handler:nil];
@@ -697,64 +656,37 @@ - (void)testSeekToleranceWhenNotSeekingToEnd {
697656
}
698657

699658
- (void)testSeekToleranceWhenSeekingToEnd {
700-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
701-
702659
StubAVPlayer *stubAVPlayer = [[StubAVPlayer alloc] init];
703660
StubFVPAVFactory *stubAVFactory = [[StubFVPAVFactory alloc] initWithPlayer:stubAVPlayer
704661
output:nil];
705-
FVPVideoPlayerPlugin *pluginWithMockAVPlayer =
706-
[[FVPVideoPlayerPlugin alloc] initWithAVFactory:stubAVFactory
707-
displayLinkFactory:nil
708-
viewProvider:[[StubViewProvider alloc] initWithView:nil]
709-
registrar:registrar];
710-
711-
FlutterError *initializationError;
712-
[pluginWithMockAVPlayer initialize:&initializationError];
713-
XCTAssertNil(initializationError);
714-
715-
FVPCreationOptions *create = [FVPCreationOptions
716-
makeWithAsset:nil
717-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"
718-
packageName:nil
719-
formatHint:nil
720-
httpHeaders:@{}
721-
viewType:FVPPlatformVideoViewTypeTextureView];
722-
FlutterError *createError;
723-
NSNumber *playerIdentifier = [pluginWithMockAVPlayer createWithOptions:create error:&createError];
724-
725-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
726-
FVPTextureBasedVideoPlayer *player =
727-
(FVPTextureBasedVideoPlayer *)pluginWithMockAVPlayer.playersByIdentifier[playerIdentifier];
728-
729-
XCTestExpectation *initializedExpectation =
662+
FVPVideoPlayer *player = [[FVPVideoPlayer alloc]
663+
initWithURL:
664+
[NSURL
665+
URLWithString:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"]
666+
httpHeaders:@{}
667+
avFactory:stubAVFactory
668+
viewProvider:[[StubViewProvider alloc] initWithView:nil]];
669+
670+
XCTestExpectation *seekExpectation =
730671
[self expectationWithDescription:@"seekTo has non-zero tolerance when seeking to end"];
731672
// The duration of this video is "0" due to the non standard initiliatazion process.
732673
[player seekTo:0
733674
completion:^(FlutterError *_Nullable error) {
734-
[initializedExpectation fulfill];
675+
[seekExpectation fulfill];
735676
}];
736677
[self waitForExpectationsWithTimeout:30.0 handler:nil];
737678
XCTAssertGreaterThan([stubAVPlayer.beforeTolerance intValue], 0);
738679
XCTAssertGreaterThan([stubAVPlayer.afterTolerance intValue], 0);
739680
}
740681

741-
- (NSDictionary<NSString *, id> *)testPlugin:(FVPVideoPlayerPlugin *)videoPlayerPlugin
742-
uri:(NSString *)uri {
743-
FlutterError *error;
744-
[videoPlayerPlugin initialize:&error];
745-
XCTAssertNil(error);
746-
747-
FVPCreationOptions *create =
748-
[FVPCreationOptions makeWithAsset:nil
749-
uri:uri
750-
packageName:nil
751-
formatHint:nil
752-
httpHeaders:@{}
753-
viewType:FVPPlatformVideoViewTypeTextureView];
754-
NSNumber *playerIdentifier = [videoPlayerPlugin createWithOptions:create error:&error];
755-
756-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
757-
FVPVideoPlayer *player = videoPlayerPlugin.playersByIdentifier[playerIdentifier];
682+
/// Sanity checks a video player playing the given URL with the actual AVPlayer. This is essentially
683+
/// a mini integration test of the player component.
684+
- (NSDictionary<NSString *, id> *)sanityTestURI:(NSString *)uri {
685+
FVPVideoPlayer *player =
686+
[[FVPVideoPlayer alloc] initWithURL:[NSURL URLWithString:uri]
687+
httpHeaders:@{}
688+
avFactory:[[FVPDefaultAVFactory alloc] init]
689+
viewProvider:[[StubViewProvider alloc] initWithView:nil]];
758690
XCTAssertNotNil(player);
759691

760692
XCTestExpectation *initializedExpectation = [self expectationWithDescription:@"initialized"];
@@ -776,6 +708,7 @@ - (void)testSeekToleranceWhenSeekingToEnd {
776708
XCTAssertEqual(avPlayer.timeControlStatus, AVPlayerTimeControlStatusPaused);
777709

778710
// Change playback speed.
711+
FlutterError *error;
779712
[player setPlaybackSpeed:2 error:&error];
780713
XCTAssertNil(error);
781714
[player playWithError:&error];
@@ -963,27 +896,13 @@ - (void)testFailedToLoadVideoEventShouldBeAlwaysSent {
963896
}
964897

965898
- (void)testUpdatePlayingStateShouldNotResetRate {
966-
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
967-
968-
FVPVideoPlayerPlugin *videoPlayerPlugin = [[FVPVideoPlayerPlugin alloc]
969-
initWithAVFactory:[[StubFVPAVFactory alloc] initWithPlayer:nil output:nil]
970-
displayLinkFactory:nil
971-
viewProvider:[[StubViewProvider alloc] initWithView:nil]
972-
registrar:registrar];
973-
974-
FlutterError *error;
975-
[videoPlayerPlugin initialize:&error];
976-
XCTAssertNil(error);
977-
FVPCreationOptions *create = [FVPCreationOptions
978-
makeWithAsset:nil
979-
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"
980-
packageName:nil
981-
formatHint:nil
982-
httpHeaders:@{}
983-
viewType:FVPPlatformVideoViewTypeTextureView];
984-
NSNumber *playerIdentifier = [videoPlayerPlugin createWithOptions:create error:&error];
985-
// TODO(stuartmorgan): Rework this test to only create the player, not the whole plugin.
986-
FVPVideoPlayer *player = videoPlayerPlugin.playersByIdentifier[playerIdentifier];
899+
FVPVideoPlayer *player = [[FVPVideoPlayer alloc]
900+
initWithURL:
901+
[NSURL
902+
URLWithString:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"]
903+
httpHeaders:@{}
904+
avFactory:[[StubFVPAVFactory alloc] initWithPlayer:nil output:nil]
905+
viewProvider:[[StubViewProvider alloc] initWithView:nil]];
987906

988907
XCTestExpectation *initializedExpectation = [self expectationWithDescription:@"initialized"];
989908
[player onListenWithArguments:nil
@@ -994,6 +913,7 @@ - (void)testUpdatePlayingStateShouldNotResetRate {
994913
}];
995914
[self waitForExpectationsWithTimeout:10 handler:nil];
996915

916+
FlutterError *error;
997917
[player setPlaybackSpeed:2 error:&error];
998918
[player playWithError:&error];
999919
XCTAssertEqual(player.player.rate, 2);

0 commit comments

Comments
 (0)