Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(814)

Side by Side Diff: webrtc/sdk/objc/Framework/UnitTests/RTCCameraVideoCapturerTests.mm

Issue 2815823002: Add unit test class for RTCCameraVideoCapturer. (Closed)
Patch Set: Add tests that test only the public interface Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 createMockSampleBufferRef() {
magjed_webrtc 2017/04/26 14:12:09 This is nice! We will probably need something simi
daniela-webrtc 2017/04/27 09:40:20 Done.
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,
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,
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] ]);
magjed_webrtc 2017/04/26 14:12:08 Remove duplicate line
daniela-webrtc 2017/04/27 09:40:20 This line is not duplicate. The first one mocks AV
magjed_webrtc 2017/04/27 14:48:12 Acknowledged.
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
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)testDelegateCallbackWithValideBufferAndOrientationUpdate {
magjed_webrtc 2017/04/26 14:12:08 spelling nit: Valide
daniela-webrtc 2017/04/27 09:40:20 Done.
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 = createMockSampleBufferRef();
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);
magjed_webrtc 2017/04/26 14:12:09 I think it's preferable in this case to expose '(v
daniela-webrtc 2017/04/27 09:40:20 I'd prefer to keep this test as is because it test
magjed_webrtc 2017/04/27 14:48:12 Ok, it's the WAIT in particular I don't like. Is i
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, DelegateCallbackWithValideBufferAndOrientation Update) {
239 RTCCameraVideoCapturerTests *test = [[RTCCameraVideoCapturerTests alloc] init] ;
240 [test setup];
241 [test testDelegateCallbackWithValideBufferAndOrientationUpdate];
242 [test tearDown];
243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698