Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 11 #import <OCMock/OCMock.h> | |
| 12 #import <UIKit/UIKit.h> | |
| 13 | |
| 14 #include "webrtc/base/gunit.h" | |
| 15 | |
| 16 #import <WebRTC/RTCCameraVideoCapturer.h> | |
| 17 #import <WebRTC/RTCDispatcher.h> | |
| 18 #import <WebRTC/RTCVideoFrame.h> | |
| 19 | |
| 20 // Helper method. | |
| 21 CMSampleBufferRef createTestSampleBufferRef() { | |
| 22 // This image is already in the testing bundle. | |
| 23 UIImage *image = [UIImage imageNamed:@"Default.png"]; | |
| 24 CGSize size = image.size; | |
| 25 CGImageRef imageRef = [image CGImage]; | |
| 26 | |
| 27 CVPixelBufferRef pixelBuffer = nullptr; | |
| 28 CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelForm atType_32ARGB, nil, | |
| 29 &pixelBuffer); | |
| 30 | |
| 31 CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); | |
| 32 CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, 8, 8 * size.width, | |
|
sakal
2017/04/28 11:20:32
nit: can you add comment on these magic numbers
daniela-webrtc
2017/05/02 08:06:11
Done.
| |
| 33 rgbColorSpace, kCGImageAlphaPremu ltipliedFirst); | |
| 34 | |
| 35 CGContextDrawImage( | |
| 36 context, CGRectMake(0, 0, CGImageGetWidth(imageRef), CGImageGetHeight(imag eRef)), imageRef); | |
| 37 | |
| 38 CGColorSpaceRelease(rgbColorSpace); | |
| 39 CGContextRelease(context); | |
| 40 | |
| 41 // We don't really care about the timing. | |
| 42 CMSampleTimingInfo timing = {kCMTimeInvalid, kCMTimeInvalid, kCMTimeInvalid}; | |
| 43 CMVideoFormatDescriptionRef description = nullptr; | |
| 44 CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &description); | |
| 45 | |
| 46 CMSampleBufferRef sampleBuffer = nullptr; | |
| 47 CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, YES, NULL , NULL, description, | |
| 48 &timing, &sampleBuffer); | |
| 49 CFRelease(pixelBuffer); | |
| 50 | |
| 51 return sampleBuffer; | |
| 52 } | |
| 53 | |
| 54 @interface RTCCameraVideoCapturer (Tests)<AVCaptureVideoDataOutputSampleBufferDe legate> | |
| 55 @end | |
| 56 | |
| 57 @interface RTCCameraVideoCapturerTests : NSObject | |
| 58 @property(nonatomic, strong) id delegateMock; | |
| 59 @property(nonatomic, strong) id deviceMock; | |
| 60 @property(nonatomic, strong) RTCCameraVideoCapturer *capturer; | |
| 61 @end | |
| 62 | |
| 63 @implementation RTCCameraVideoCapturerTests | |
| 64 @synthesize delegateMock = _delegateMock; | |
| 65 @synthesize capturer = _capturer; | |
| 66 @synthesize deviceMock = _deviceMock; | |
| 67 | |
| 68 - (void)setup { | |
| 69 self.delegateMock = OCMProtocolMock(@protocol(RTCVideoCapturerDelegate)); | |
| 70 self.capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:self.delegate Mock]; | |
| 71 self.deviceMock = [self createDeviceMock]; | |
| 72 } | |
| 73 | |
| 74 - (void)tearDown { | |
| 75 [self.delegateMock stopMocking]; | |
| 76 [self.deviceMock stopMocking]; | |
| 77 self.delegateMock = nil; | |
| 78 self.deviceMock = nil; | |
| 79 self.capturer = nil; | |
| 80 } | |
| 81 | |
| 82 #pragma mark - utils | |
| 83 | |
| 84 - (id)createDeviceMock { | |
| 85 return OCMClassMock([AVCaptureDevice class]); | |
| 86 } | |
| 87 | |
| 88 #pragma mark - test cases | |
| 89 | |
| 90 - (void)testSetupSession { | |
| 91 AVCaptureSession *session = self.capturer.captureSession; | |
| 92 EXPECT_TRUE(session != nil); | |
| 93 EXPECT_EQ(session.sessionPreset, AVCaptureSessionPresetInputPriority); | |
| 94 EXPECT_EQ(session.usesApplicationAudioSession, NO); | |
| 95 EXPECT_EQ(session.outputs.count, 1u); | |
| 96 } | |
| 97 | |
| 98 - (void)testSetupSessionOutput { | |
| 99 AVCaptureVideoDataOutput *videoOutput = self.capturer.captureSession.outputs[0 ]; | |
| 100 EXPECT_TRUE(videoOutput.videoSettings != nil); | |
| 101 EXPECT_EQ(videoOutput.alwaysDiscardsLateVideoFrames, NO); | |
| 102 EXPECT_EQ(videoOutput.sampleBufferDelegate, self.capturer); | |
| 103 } | |
| 104 | |
| 105 - (void)testSupportedFormatsForDevice { | |
| 106 // given | |
| 107 id validFormat1 = OCMClassMock([AVCaptureDeviceFormat class]); | |
| 108 CMVideoFormatDescriptionRef format; | |
| 109 CMVideoFormatDescriptionCreate(nil, kCVPixelFormatType_420YpCbCr8PlanarFullRan ge, 123, 456, nil, | |
|
sakal
2017/04/28 11:20:32
nit: can you add comment on these magic numbers +
daniela-webrtc
2017/05/02 08:06:11
Done.
| |
| 110 &format); | |
| 111 OCMStub([validFormat1 formatDescription]).andReturn(format); | |
| 112 | |
| 113 id validFormat2 = OCMClassMock([AVCaptureDeviceFormat class]); | |
| 114 CMVideoFormatDescriptionCreate(nil, kCVPixelFormatType_420YpCbCr8BiPlanarVideo Range, 123, 456, | |
| 115 nil, &format); | |
| 116 OCMStub([validFormat2 formatDescription]).andReturn(format); | |
| 117 | |
| 118 id invalidFormat = OCMClassMock([AVCaptureDeviceFormat class]); | |
| 119 CMVideoFormatDescriptionCreate(nil, kCVPixelFormatType_422YpCbCr8_yuvs, 123, 4 56, nil, &format); | |
| 120 OCMStub([invalidFormat formatDescription]).andReturn(format); | |
| 121 | |
| 122 NSArray *formats = @[ validFormat1, validFormat2, invalidFormat ]; | |
| 123 OCMStub([self.deviceMock formats]).andReturn(formats); | |
| 124 | |
| 125 // when | |
| 126 NSArray *supportedFormats = [RTCCameraVideoCapturer supportedFormatsForDevice: self.deviceMock]; | |
| 127 | |
| 128 // then | |
| 129 EXPECT_EQ(supportedFormats.count, 2u); | |
| 130 EXPECT_TRUE([supportedFormats containsObject:validFormat1]); | |
| 131 EXPECT_TRUE([supportedFormats containsObject:validFormat2]); | |
| 132 // cleanup | |
| 133 [validFormat1 stopMocking]; | |
| 134 [validFormat2 stopMocking]; | |
| 135 [invalidFormat stopMocking]; | |
| 136 validFormat1 = nil; | |
| 137 validFormat2 = nil; | |
| 138 invalidFormat = nil; | |
| 139 } | |
| 140 | |
| 141 - (void)testCaptureDevices { | |
| 142 OCMStub([self.deviceMock devicesWithMediaType:AVMediaTypeVideo]).andReturn(@[ [NSObject new] ]); | |
| 143 OCMStub([self.deviceMock devicesWithMediaType:AVMediaTypeAudio]).andReturn(@[ [NSObject new] ]); | |
| 144 | |
| 145 NSArray *captureDevices = [RTCCameraVideoCapturer captureDevices]; | |
| 146 | |
| 147 EXPECT_EQ(captureDevices.count, 1u); | |
| 148 } | |
| 149 | |
| 150 - (void)testDelegateCallbackNotCalledWhenInvalidBuffer { | |
| 151 // given | |
| 152 CMSampleBufferRef sampleBuffer = nullptr; | |
| 153 [[self.delegateMock reject] capturer:[OCMArg any] didCaptureVideoFrame:[OCMArg any]]; | |
| 154 | |
| 155 // when | |
| 156 | |
|
sakal
2017/04/28 11:20:32
nit: remove empty line
daniela-webrtc
2017/05/02 08:06:11
Done.
| |
| 157 [self.capturer captureOutput:self.capturer.captureSession.outputs[0] | |
| 158 didOutputSampleBuffer:sampleBuffer | |
| 159 fromConnection:nil]; | |
| 160 | |
| 161 // then | |
| 162 [self.delegateMock verify]; | |
| 163 } | |
| 164 | |
| 165 - (void)testDelegateCallbackWithValidBufferAndOrientationUpdate { | |
| 166 // given | |
| 167 UIDevice *currentDeviceMock = OCMClassMock([UIDevice class]); | |
| 168 // UpsideDown -> RTCVideoRotation_270. | |
| 169 OCMStub(currentDeviceMock.orientation).andReturn(UIDeviceOrientationPortraitUp sideDown); | |
| 170 id classMock = OCMClassMock([UIDevice class]); | |
| 171 OCMStub([classMock currentDevice]).andReturn(currentDeviceMock); | |
| 172 | |
| 173 CMSampleBufferRef sampleBuffer = createTestSampleBufferRef(); | |
| 174 | |
| 175 // then | |
| 176 [[self.delegateMock expect] capturer:self.capturer | |
| 177 didCaptureVideoFrame:[OCMArg checkWithBlock:^BOOL(RTCVideoFram e *expectedFrame) { | |
| 178 EXPECT_EQ(expectedFrame.rotation, RTCVideoRotation_270); | |
| 179 return YES; | |
| 180 }]]; | |
| 181 | |
| 182 // when | |
| 183 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
| 184 [center postNotificationName:UIDeviceOrientationDidChangeNotification object:n il]; | |
| 185 | |
| 186 // We need to wait for the dispatch to finish. | |
| 187 WAIT(0, 1000); | |
| 188 | |
| 189 [self.capturer captureOutput:self.capturer.captureSession.outputs[0] | |
| 190 didOutputSampleBuffer:sampleBuffer | |
| 191 fromConnection:nil]; | |
| 192 | |
| 193 [self.delegateMock verify]; | |
| 194 | |
| 195 [(id)currentDeviceMock stopMocking]; | |
| 196 currentDeviceMock = nil; | |
| 197 [classMock stopMocking]; | |
| 198 classMock = nil; | |
| 199 CFRelease(sampleBuffer); | |
| 200 } | |
| 201 @end | |
| 202 | |
| 203 TEST(RTCCameraVideoCapturerTests, SetupSession) { | |
| 204 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; | |
| 205 [test setup]; | |
| 206 [test testSetupSession]; | |
| 207 [test tearDown]; | |
| 208 } | |
| 209 | |
| 210 TEST(RTCCameraVideoCapturerTests, SetupSessionOutput) { | |
| 211 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; | |
| 212 [test setup]; | |
| 213 [test testSetupSessionOutput]; | |
| 214 [test tearDown]; | |
| 215 } | |
| 216 | |
| 217 TEST(RTCCameraVideoCapturerTests, SupportedFormatsForDevice) { | |
| 218 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; | |
| 219 [test setup]; | |
| 220 [test testSupportedFormatsForDevice]; | |
| 221 [test tearDown]; | |
| 222 } | |
| 223 | |
| 224 TEST(RTCCameraVideoCapturerTests, CaptureDevices) { | |
| 225 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; | |
| 226 [test setup]; | |
| 227 [test testCaptureDevices]; | |
| 228 [test tearDown]; | |
| 229 } | |
| 230 | |
| 231 TEST(RTCCameraVideoCapturerTests, DelegateCallbackNotCalledWhenInvalidBuffer) { | |
| 232 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; | |
| 233 [test setup]; | |
| 234 [test testDelegateCallbackNotCalledWhenInvalidBuffer]; | |
| 235 [test tearDown]; | |
| 236 } | |
| 237 | |
| 238 TEST(RTCCameraVideoCapturerTests, DelegateCallbackWithValidBufferAndOrientationU pdate) { | |
| 239 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ; | |
| 240 [test setup]; | |
| 241 [test testDelegateCallbackWithValidBufferAndOrientationUpdate]; | |
| 242 [test tearDown]; | |
| 243 } | |
| OLD | NEW |