Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 11 | #import "components/capturer/RTCFileVideoCapturer.h" |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 12 | |
| 13 | #import <XCTest/XCTest.h> |
| 14 | |
| 15 | #include "rtc_base/gunit.h" |
| 16 | |
| 17 | NSString *const kTestFileName = @"foreman.mp4"; |
| 18 | static const int kTestTimeoutMs = 5 * 1000; // 5secs. |
| 19 | |
| 20 | @interface MockCapturerDelegate : NSObject <RTCVideoCapturerDelegate> |
| 21 | |
| 22 | @property(nonatomic, assign) NSInteger capturedFramesCount; |
| 23 | |
| 24 | @end |
| 25 | |
| 26 | @implementation MockCapturerDelegate |
| 27 | @synthesize capturedFramesCount = _capturedFramesCount; |
| 28 | |
| 29 | - (void)capturer:(RTCVideoCapturer *)capturer didCaptureVideoFrame:(RTCVideoFrame *)frame { |
| 30 | self.capturedFramesCount++; |
| 31 | } |
| 32 | |
| 33 | @end |
| 34 | |
| 35 | NS_CLASS_AVAILABLE_IOS(10) |
| 36 | @interface RTCFileVideoCapturerTests : XCTestCase |
| 37 | |
| 38 | @property(nonatomic, strong) RTCFileVideoCapturer *capturer; |
| 39 | @property(nonatomic, strong) MockCapturerDelegate *mockDelegate; |
| 40 | |
| 41 | @end |
| 42 | |
| 43 | @implementation RTCFileVideoCapturerTests |
| 44 | @synthesize capturer = _capturer; |
| 45 | @synthesize mockDelegate = _mockDelegate; |
| 46 | |
| 47 | - (void)setUp { |
| 48 | self.mockDelegate = [[MockCapturerDelegate alloc] init]; |
| 49 | self.capturer = [[RTCFileVideoCapturer alloc] initWithDelegate:self.mockDelegate]; |
| 50 | } |
| 51 | |
| 52 | - (void)tearDown { |
| 53 | self.capturer = nil; |
| 54 | self.mockDelegate = nil; |
| 55 | } |
| 56 | |
| 57 | - (void)testCaptureWhenFileNotInBundle { |
| 58 | __block BOOL errorOccured = NO; |
| 59 | |
| 60 | RTCFileVideoCapturerErrorBlock errorBlock = ^void(NSError *error) { |
| 61 | errorOccured = YES; |
| 62 | }; |
| 63 | |
| 64 | [self.capturer startCapturingFromFileNamed:@"not_in_bundle.mov" onError:errorBlock]; |
| 65 | ASSERT_TRUE_WAIT(errorOccured, kTestTimeoutMs); |
| 66 | } |
| 67 | |
| 68 | - (void)testSecondStartCaptureCallFails { |
| 69 | __block BOOL secondError = NO; |
| 70 | |
| 71 | RTCFileVideoCapturerErrorBlock firstErrorBlock = ^void(NSError *error) { |
| 72 | // This block should never be called. |
| 73 | NSLog(@"Error: %@", [error userInfo]); |
| 74 | ASSERT_TRUE(false); |
| 75 | }; |
| 76 | |
| 77 | RTCFileVideoCapturerErrorBlock secondErrorBlock = ^void(NSError *error) { |
| 78 | secondError = YES; |
| 79 | }; |
| 80 | |
| 81 | [self.capturer startCapturingFromFileNamed:kTestFileName onError:firstErrorBlock]; |
| 82 | [self.capturer startCapturingFromFileNamed:kTestFileName onError:secondErrorBlock]; |
| 83 | |
| 84 | ASSERT_TRUE_WAIT(secondError, kTestTimeoutMs); |
| 85 | } |
| 86 | |
| 87 | - (void)testStartStopCapturer { |
| 88 | #if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0) |
| 89 | if (@available(iOS 10, *)) { |
| 90 | [self.capturer startCapturingFromFileNamed:kTestFileName onError:nil]; |
| 91 | |
| 92 | __block BOOL done = NO; |
| 93 | __block NSInteger capturedFrames = -1; |
| 94 | NSInteger capturedFramesAfterStop = -1; |
| 95 | |
| 96 | // We're dispatching the `stopCapture` with delay to ensure the capturer has |
| 97 | // had the chance to capture several frames. |
| 98 | dispatch_time_t captureDelay = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC); // 2secs. |
| 99 | dispatch_after(captureDelay, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 100 | capturedFrames = self.mockDelegate.capturedFramesCount; |
| 101 | [self.capturer stopCapture]; |
| 102 | done = YES; |
| 103 | }); |
| 104 | WAIT(done, kTestTimeoutMs); |
| 105 | |
| 106 | capturedFramesAfterStop = self.mockDelegate.capturedFramesCount; |
| 107 | ASSERT_TRUE(capturedFrames != -1); |
| 108 | ASSERT_EQ(capturedFrames, capturedFramesAfterStop); |
| 109 | } |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | @end |