Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Nits and stub change
  • Loading branch information
tarrinneal committed May 11, 2023
commit 46f3c7dbe5b09fd6a305898fa0b85227e18e0999
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,32 @@ @interface VideoPlayerTests : XCTestCase
@end

@interface StubAVPlayer : AVPlayer
@property(readonly) NSMutableArray *beforeTolerances;
@property(readonly) NSMutableArray *afterTolerances;
@property(readonly, nonatomic) NSNumber *beforeTolerance;
@property(readonly, nonatomic) NSNumber *afterTolerance;
@end

@implementation StubAVPlayer

- (instancetype)init {
self = [super init];
if (self) {
_beforeTolerances = [[NSMutableArray alloc] init];
_afterTolerances = [[NSMutableArray alloc] init];
}
return self;
}

- (void)seekToTime:(CMTime)time
toleranceBefore:(CMTime)toleranceBefore
toleranceAfter:(CMTime)toleranceAfter
completionHandler:(void (^)(BOOL finished))completionHandler {
[_beforeTolerances addObject:[NSNumber numberWithLong:toleranceBefore.value]];
[_afterTolerances addObject:[NSNumber numberWithLong:toleranceAfter.value]];
_beforeTolerance = [NSNumber numberWithLong:toleranceBefore.value];
_afterTolerance = [NSNumber numberWithLong:toleranceAfter.value];
completionHandler(YES);
}

@end

@interface StubFVPAVPlayerFactory : NSObject <FVPAVPlayerFactoryProtocol>
@interface StubFVPPlayerFactory : NSObject <FVPPlayerFactory>

@property(copy) StubAVPlayer *stubAVPlayer;
@property(nonatomic, strong) StubAVPlayer *stubAVPlayer;

- (instancetype)initWithPlayer:(StubAVPlayer *)stubAVPlayer;

@end

@implementation StubFVPAVPlayerFactory
@implementation StubFVPPlayerFactory

- (instancetype)initWithPlayer:(StubAVPlayer *)stubAVPlayer {
self = [super init];
Expand Down Expand Up @@ -276,17 +267,16 @@ - (void)testTransformFix {
[self validateTransformFixForOrientation:UIImageOrientationRightMirrored];
}

- (void)testSeekToleranceWhenSeekingToEndFix {
- (void)testSeekToleranceWhenNotSeekingToEnd {
NSObject<FlutterPluginRegistry> *registry =
(NSObject<FlutterPluginRegistry> *)[[UIApplication sharedApplication] delegate];
NSObject<FlutterPluginRegistrar> *registrar = [registry registrarForPlugin:@"TestSeekTolerance"];

StubAVPlayer *stubAVPlayer = [[StubAVPlayer alloc] init];
StubFVPAVPlayerFactory *stubFVPAVPlayerFactory =
[[StubFVPAVPlayerFactory alloc] initWithPlayer:stubAVPlayer];
StubFVPPlayerFactory *stubFVPPlayerFactory =
[[StubFVPPlayerFactory alloc] initWithPlayer:stubAVPlayer];
FLTVideoPlayerPlugin *pluginWithMockAVPlayer =
[[FLTVideoPlayerPlugin alloc] initWithFVPAVPlayerFactory:stubFVPAVPlayerFactory
registrar:registrar];
[[FLTVideoPlayerPlugin alloc] initWithPlayerFactory:stubFVPPlayerFactory registrar:registrar];

FlutterError *error;
[pluginWithMockAVPlayer initialize:&error];
Expand All @@ -309,19 +299,46 @@ - (void)testSeekToleranceWhenSeekingToEndFix {
[initializedExpectation fulfill];
}];

XCTestExpectation *initializedExpectationEnd =
[self waitForExpectationsWithTimeout:30.0 handler:nil];
XCTAssertEqual([stubAVPlayer.beforeTolerance intValue], 0);
XCTAssertEqual([stubAVPlayer.afterTolerance intValue], 0);
}

- (void)testSeekToleranceWhenSeekingToEnd {
NSObject<FlutterPluginRegistry> *registry =
(NSObject<FlutterPluginRegistry> *)[[UIApplication sharedApplication] delegate];
NSObject<FlutterPluginRegistrar> *registrar = [registry registrarForPlugin:@"TestSeekTolerance"];

StubAVPlayer *stubAVPlayer = [[StubAVPlayer alloc] init];
StubFVPPlayerFactory *stubFVPPlayerFactory =
[[StubFVPPlayerFactory alloc] initWithPlayer:stubAVPlayer];
FLTVideoPlayerPlugin *pluginWithMockAVPlayer =
[[FLTVideoPlayerPlugin alloc] initWithPlayerFactory:stubFVPPlayerFactory registrar:registrar];

FlutterError *error;
[pluginWithMockAVPlayer initialize:&error];
XCTAssertNil(error);

FLTCreateMessage *create = [FLTCreateMessage
makeWithAsset:nil
uri:@"https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4"
packageName:nil
formatHint:nil
httpHeaders:@{}];
FLTTextureMessage *textureMessage = [pluginWithMockAVPlayer create:create error:&error];
NSNumber *textureId = textureMessage.textureId;

XCTestExpectation *initializedExpectation =
[self expectationWithDescription:@"seekTo has non-zero tolerance when seeking to end"];
// The duration of this video is "0" due to the non standard initiliatazion process.
FLTPositionMessage *messageEnd = [FLTPositionMessage makeWithTextureId:textureId position:@0];
[pluginWithMockAVPlayer seekTo:messageEnd
FLTPositionMessage *message = [FLTPositionMessage makeWithTextureId:textureId position:@0];
[pluginWithMockAVPlayer seekTo:message
completion:^(FlutterError *_Nullable error) {
[initializedExpectationEnd fulfill];
[initializedExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:30.0 handler:nil];
XCTAssertEqual([[stubAVPlayer.beforeTolerances objectAtIndex:0] intValue], 0);
XCTAssertEqual([[stubAVPlayer.afterTolerances objectAtIndex:0] intValue], 0);
XCTAssertGreaterThan([[stubAVPlayer.beforeTolerances objectAtIndex:1] intValue], 0);
XCTAssertGreaterThan([[stubAVPlayer.afterTolerances objectAtIndex:1] intValue], 0);
XCTAssertGreaterThan([stubAVPlayer.beforeTolerance intValue], 0);
XCTAssertGreaterThan([stubAVPlayer.afterTolerance intValue], 0);
}

- (NSDictionary<NSString *, id> *)testPlugin:(FLTVideoPlayerPlugin *)videoPlayerPlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ - (void)onDisplayLink:(CADisplayLink *)link {
}
@end

@interface FVPDefaultAVPlayerFactory : NSObject <FVPAVPlayerFactoryProtocol>
@interface FVPDefaultPlayerFactory : NSObject <FVPPlayerFactory>
@end

@implementation FVPDefaultAVPlayerFactory
@implementation FVPDefaultPlayerFactory
- (AVPlayer *)playerWithPlayerItem:(AVPlayerItem *)playerItem {
return [AVPlayer playerWithPlayerItem:playerItem];
}
Expand All @@ -64,7 +64,7 @@ @interface FLTVideoPlayer : NSObject <FlutterTexture, FlutterStreamHandler>
- (instancetype)initWithURL:(NSURL *)url
frameUpdater:(FLTFrameUpdater *)frameUpdater
httpHeaders:(nonnull NSDictionary<NSString *, NSString *> *)headers
playerFactory:(id<FVPAVPlayerFactoryProtocol>)playerFactory;
playerFactory:(id<FVPPlayerFactory>)playerFactory;
@end

static void *timeRangeContext = &timeRangeContext;
Expand All @@ -79,7 +79,7 @@ - (instancetype)initWithURL:(NSURL *)url
@implementation FLTVideoPlayer
- (instancetype)initWithAsset:(NSString *)asset
frameUpdater:(FLTFrameUpdater *)frameUpdater
playerFactory:(id<FVPAVPlayerFactoryProtocol>)playerFactory {
playerFactory:(id<FVPPlayerFactory>)playerFactory {
NSString *path = [[NSBundle mainBundle] pathForResource:asset ofType:nil];
return [self initWithURL:[NSURL fileURLWithPath:path]
frameUpdater:frameUpdater
Expand Down Expand Up @@ -221,7 +221,7 @@ - (void)createVideoOutputAndDisplayLink:(FLTFrameUpdater *)frameUpdater {
- (instancetype)initWithURL:(NSURL *)url
frameUpdater:(FLTFrameUpdater *)frameUpdater
httpHeaders:(nonnull NSDictionary<NSString *, NSString *> *)headers
playerFactory:(id<FVPAVPlayerFactoryProtocol>)playerFactory {
playerFactory:(id<FVPPlayerFactory>)playerFactory {
NSDictionary<NSString *, id> *options = nil;
if ([headers count] != 0) {
options = @{@"AVURLAssetHTTPHeaderFieldsKey" : headers};
Expand All @@ -233,7 +233,7 @@ - (instancetype)initWithURL:(NSURL *)url

- (instancetype)initWithPlayerItem:(AVPlayerItem *)item
frameUpdater:(FLTFrameUpdater *)frameUpdater
playerFactory:(id<FVPAVPlayerFactoryProtocol>)playerFactory {
playerFactory:(id<FVPPlayerFactory>)playerFactory {
self = [super init];
NSAssert(self, @"super init cannot be nil");

Expand Down Expand Up @@ -548,7 +548,7 @@ @interface FLTVideoPlayerPlugin () <FLTAVFoundationVideoPlayerApi>
@property(readonly, strong, nonatomic)
NSMutableDictionary<NSNumber *, FLTVideoPlayer *> *playersByTextureId;
@property(readonly, strong, nonatomic) NSObject<FlutterPluginRegistrar> *registrar;
@property(nonatomic, strong) id<FVPAVPlayerFactoryProtocol> playerFactory;
@property(nonatomic, strong) id<FVPPlayerFactory> playerFactory;
@end

@implementation FLTVideoPlayerPlugin
Expand All @@ -559,12 +559,11 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
}

- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
return [self initWithFVPAVPlayerFactory:[[FVPDefaultAVPlayerFactory alloc] init]
registrar:registrar];
return [self initWithPlayerFactory:[[FVPDefaultPlayerFactory alloc] init] registrar:registrar];
}

- (instancetype)initWithFVPAVPlayerFactory:(id<FVPAVPlayerFactoryProtocol>)playerFactory
registrar:(NSObject<FlutterPluginRegistrar> *)registrar {
- (instancetype)initWithPlayerFactory:(id<FVPPlayerFactory>)playerFactory
registrar:(NSObject<FlutterPluginRegistrar> *)registrar {
self = [super init];
NSAssert(self, @"super init cannot be nil");
_registry = [registrar textures];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#import <AVFoundation/AVFoundation.h>

// Protocol for an AVPlayer instance factory. Used for injecting players in tests.
@protocol FVPAVPlayerFactoryProtocol
@protocol FVPPlayerFactory
- (AVPlayer *)playerWithPlayerItem:(AVPlayerItem *)playerItem;
@end

@interface FLTVideoPlayerPlugin ()

- (instancetype)initWithFVPAVPlayerFactory:(id<FVPAVPlayerFactoryProtocol>)playerFactory
registrar:(NSObject<FlutterPluginRegistrar> *)registrar;
- (instancetype)initWithPlayerFactory:(id<FVPPlayerFactory>)playerFactory
registrar:(NSObject<FlutterPluginRegistrar> *)registrar;
@end