Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up logic parsing video details, added keywords property, and improved completion handler logic #505

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
92 changes: 55 additions & 37 deletions XCDYouTubeKit Tests/XCDYouTubeClientTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <XCDYouTubeKit/XCDYouTubeClient.h>
#import <XCDYouTubeKit/XCDYouTubeVideoOperation.h>

NSTimeInterval kDefaultNetworkTimeout = 5;

@interface XCDYouTubeClientTestCase : XCDYouTubeKitTestCase
@end

Expand All @@ -23,7 +25,7 @@ - (void) testThatVideoIsAvailalbeOnDetailPageEventLabel
XCTAssertNotNil(video);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testThatVideoHasMetadata
Expand All @@ -42,7 +44,7 @@ - (void) testThatVideoHasMetadata
XCTAssertTrue(video.duration > 0);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}
#if TARGET_OS_OSX
- (void) testThatThumbnailsAreOrderedCorrectly
Expand Down Expand Up @@ -70,7 +72,7 @@ - (void) testThatThumbnailsAreOrderedCorrectly

[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}
#endif
- (void) testThatVideoHasOtherStreams
Expand Down Expand Up @@ -105,7 +107,7 @@ - (void) testThatVideoHasOtherStreams

[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testVideoThatHasCaptions
Expand All @@ -120,7 +122,7 @@ - (void) testVideoThatHasCaptions
XCTAssertNotEqual(video.autoGeneratedCaptionURLs[@"en"], video.captionURLs[@"en"]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];

}

Expand All @@ -133,7 +135,7 @@ - (void)testVideoWithDashManifest
XCTAssertNotNil(video.streamURLs[@299], @"Could not find Dash video 299 in `streamURLs`"); //itag=299: {'ext': 'mp4', 'height': 1080, 'format_note': 'DASH video', 'vcodec': 'h264', 'fps': 60}
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

//See -[XCDYouTubeVideoOperation handleConnectionError:requestType]
Expand All @@ -147,7 +149,7 @@ - (void)testConnectionErrorWithDashManifest

[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testVideoWithUndeterminedCaptionLanguageCode
Expand All @@ -161,7 +163,7 @@ - (void) testVideoWithUndeterminedCaptionLanguageCode
XCTAssertNotNil(video.captionURLs);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];

}

Expand All @@ -182,7 +184,7 @@ - (void) testMobileRestrictedVideo
}];
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testLiveVideo
Expand All @@ -197,7 +199,7 @@ - (void) testLiveVideo
XCTAssertNotNil(video.streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

// Test for https://github.com/0xced/XCDYouTubeKit/issues/420
Expand All @@ -218,7 +220,7 @@ - (void) testVideo1
}];
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testVideo2
Expand Down Expand Up @@ -264,7 +266,7 @@ - (void) testVideo1IsPlayable
}];
[dataTask resume];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testVideo1ReturnsSomePlayableStreams
Expand Down Expand Up @@ -303,7 +305,7 @@ - (void) testVideo1ReturnsSomePlayableStreams
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

// Disable internet connection before running to allow some queries to fail
Expand Down Expand Up @@ -339,7 +341,7 @@ - (void) testVideo1ReturnsSomePlayableStreamsEvenIfSomeFailDueToConnectionError_
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

// Disable internet connection before running to allow all queries to fail
Expand Down Expand Up @@ -369,7 +371,7 @@ - (void) testVideo1ReturnsNoPlayableStreamsBecauseConnectionError_offline
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testVideo2ReturnsAllPlayableStreams
Expand Down Expand Up @@ -398,7 +400,7 @@ - (void) testVideo2ReturnsAllPlayableStreams
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testVideo3ReturnsSomePlayableStreams
Expand Down Expand Up @@ -439,7 +441,7 @@ - (void) testVideo3ReturnsSomePlayableStreams
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testThatQueryingLiveVideoReturnsPlayableStreams
Expand Down Expand Up @@ -471,7 +473,7 @@ - (void) testThatQueryingLiveVideoReturnsPlayableStreams
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testQueryingWithSpecifiedStreamURLs
Expand Down Expand Up @@ -515,7 +517,7 @@ - (void) testQueryingWithSpecifiedStreamURLs
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testQueryingWithSpecifiedStreamURLsSomeNotBeingInVideoObject
Expand Down Expand Up @@ -564,7 +566,7 @@ - (void) testQueryingWithSpecifiedStreamURLsSomeNotBeingInVideoObject
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testQueryingWhenNoSpecifiedURLsAreInVideoObject
Expand Down Expand Up @@ -600,7 +602,7 @@ - (void) testQueryingWhenNoSpecifiedURLsAreInVideoObject
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

-(void) testQueryingWhenSpecifiedURLsAreEmpty
Expand Down Expand Up @@ -629,7 +631,7 @@ -(void) testQueryingWhenSpecifiedURLsAreEmpty
}];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testExpiredLiveVideo
Expand All @@ -642,7 +644,7 @@ - (void) testExpiredLiveVideo
XCTAssertEqualObjects(error.localizedDescription, @"This live stream recording is not available.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testRestrictedVideo
Expand All @@ -658,7 +660,7 @@ - (void) testRestrictedVideo
XCTAssertEqualObjects(error.localizedDescription, @"This video is no longer available because the YouTube account associated with this video has been terminated.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];

if (logLevel)
setenv("XCDYouTubeKitLogLevel", logLevel, 1);
Expand All @@ -680,7 +682,7 @@ - (void) testTooManyRequestsError
XCTAssertEqualObjects(error.localizedDescription, @"The operation couldn’t be completed because too many requests were sent.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testRemovedVideo
Expand All @@ -694,7 +696,7 @@ - (void) testRemovedVideo
XCTAssertEqualObjects(error.localizedDescription, @"This video is no longer available due to a copyright claim by Digital Rights Group Ltd.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testGeoblockedVideo
Expand All @@ -708,7 +710,7 @@ - (void) testGeoblockedVideo
XCTAssertEqualObjects(error.localizedDescription, @"The uploader has not made this video available in your country.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testInvalidVideoIdentifier
Expand All @@ -722,7 +724,7 @@ - (void) testInvalidVideoIdentifier
XCTAssertEqualObjects(error.localizedDescription, @"Invalid parameters.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testEmptyResponse_offline
Expand All @@ -735,21 +737,24 @@ - (void) testEmptyResponse_offline
XCTAssertEqual(error.code, XCDYouTubeErrorEmptyResponse);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testNonExistentVideoIdentifier
{
__weak XCTestExpectation *expectation = [self expectationWithDescription:@""];
NSDate* date = [NSDate date];
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"xxxxxxxxxxx" completionHandler:^(XCDYouTubeVideo *video, NSError *error)
{
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
NSLog(@"time interval %@", @(timeInterval));
XCTAssertNil(video);
XCTAssertEqualObjects(error.domain, XCDYouTubeVideoErrorDomain);
XCTAssertEqual(error.code, XCDYouTubeErrorNoStreamAvailable);
XCTAssertEqualObjects(error.localizedDescription, @"Video unavailable");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:10 handler:nil];
}

- (void) testFrenchClient
Expand All @@ -763,7 +768,7 @@ - (void) testFrenchClient
XCTAssertEqualObjects(error.localizedDescription, @"Vidéo non disponible");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testNilVideoIdentifier
Expand All @@ -777,7 +782,7 @@ - (void) testNilVideoIdentifier
XCTAssertEqualObjects(error.localizedDescription, @"Invalid parameters.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testSpaceVideoIdentifier
Expand All @@ -791,7 +796,7 @@ - (void) testSpaceVideoIdentifier
XCTAssertEqualObjects(error.localizedDescription, @"Invalid parameters.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

// Disable internet connection before running
Expand All @@ -809,12 +814,12 @@ - (void) testConnectionError_offline
XCTAssertEqual(underlyingError.code, NSURLErrorNotConnectedToInternet);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testUsingClientOnNonMainThread
{
__weak XCTestExpectation *expectation = [self expectationWithDescription:@""];
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"It should call back on main thread"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
XCTAssertFalse([NSThread isMainThread]);
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"EdeVaT-zZt4" completionHandler:^(XCDYouTubeVideo *video, NSError *error)
Expand All @@ -823,7 +828,20 @@ - (void) testUsingClientOnNonMainThread
[expectation fulfill];
}];
});
[self waitForExpectationsWithTimeout:5 handler:nil];

__weak XCTestExpectation *expectation2 = [self expectationWithDescription:@"It should call back on calling operation queue"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
XCTAssertFalse([NSThread isMainThread]);
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"EdeVaT-zZt4" completionHandler:^(XCDYouTubeVideo *video, NSError *error)
{
XCTAssertFalse([NSThread isMainThread]);
XCTAssertEqual(queue, [NSOperationQueue currentQueue]);
[expectation2 fulfill];
}];
}];

[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testCancelingOperation
Expand Down Expand Up @@ -855,7 +873,7 @@ - (void) testCancelingOperationQueryOperation
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:5 handler:nil];
[self waitForExpectationsWithTimeout:kDefaultNetworkTimeout handler:nil];
}

- (void) testNilCompletionHandler
Expand Down
2 changes: 2 additions & 0 deletions XCDYouTubeKit Tests/XCDYouTubeKitTestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#import "VCR.h"
#import "VCRCassetteManager.h"

extern NSTimeInterval kDefaultNetworkTimeout;

@interface XCDYouTubeKitTestCase : XCTestCase
- (void) setUpTestWithSelector:(SEL)selector;
@property NSArray<NSHTTPCookie *>*cookies;
Expand Down
Loading