OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 <Foundation/Foundation.h> |
| 12 #import <OCMock/OCMock.h> |
| 13 |
| 14 #include "webrtc/base/gunit.h" |
| 15 |
| 16 #include "avfoundationformatmapper.h" |
| 17 |
| 18 |
| 19 // Width and height don't play any role so lets use predefined values throughout |
| 20 // the tests. |
| 21 static const int kFormatWidth = 789; |
| 22 static const int kFormatHeight = 987; |
| 23 |
| 24 // Hardcoded framrate to be used throughout the tests. |
| 25 static const int kFramerate = 30; |
| 26 |
| 27 // Same width and height is used so it's ok to expect same cricket::VideoFormat |
| 28 static cricket::VideoFormat expectedFormat = |
| 29 cricket::VideoFormat(kFormatWidth, |
| 30 kFormatHeight, |
| 31 cricket::VideoFormat::FpsToInterval(kFramerate), |
| 32 cricket::FOURCC_NV12); |
| 33 |
| 34 // Mock class for AVCaptureDeviceFormat. |
| 35 // Custom implementation needed because OCMock cannot handle the |
| 36 // CMVideoDescriptionRef mocking. |
| 37 @interface AVCaptureDeviceFormatMock : NSObject |
| 38 |
| 39 @property (nonatomic, assign) CMVideoFormatDescriptionRef format; |
| 40 @property (nonatomic, strong) OCMockObject *rangeMock; |
| 41 |
| 42 - (instancetype)initWithMediaSubtype:(FourCharCode)subtype |
| 43 minFps:(float)minFps |
| 44 maxFps:(float)maxFps; |
| 45 + (instancetype)validFormat; |
| 46 + (instancetype)invalidFpsFormat; |
| 47 + (instancetype)invalidMediaSubtypeFormat; |
| 48 |
| 49 @end |
| 50 |
| 51 @implementation AVCaptureDeviceFormatMock |
| 52 |
| 53 @synthesize format = _format; |
| 54 @synthesize rangeMock = _rangeMock; |
| 55 |
| 56 - (instancetype)initWithMediaSubtype:(FourCharCode)subtype |
| 57 minFps:(float)minFps |
| 58 maxFps:(float)maxFps { |
| 59 if (self = [super init]) { |
| 60 CMVideoFormatDescriptionCreate(nil, subtype, kFormatWidth, kFormatHeight, |
| 61 nil, &_format); |
| 62 // We can use OCMock for the range. |
| 63 _rangeMock = [OCMockObject mockForClass:[AVFrameRateRange class]]; |
| 64 [[[_rangeMock stub] andReturnValue:@(minFps)] minFrameRate]; |
| 65 [[[_rangeMock stub] andReturnValue:@(maxFps)] maxFrameRate]; |
| 66 } |
| 67 |
| 68 return self; |
| 69 } |
| 70 |
| 71 + (instancetype)validFormat { |
| 72 AVCaptureDeviceFormatMock *instance = [[AVCaptureDeviceFormatMock alloc] |
| 73 initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange |
| 74 minFps:0.0 |
| 75 maxFps:30.0]; |
| 76 return instance; |
| 77 } |
| 78 |
| 79 + (instancetype)invalidFpsFormat { |
| 80 AVCaptureDeviceFormatMock *instance = [[AVCaptureDeviceFormatMock alloc] |
| 81 initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange |
| 82 minFps:0.0 |
| 83 maxFps:22.0]; |
| 84 return instance; |
| 85 } |
| 86 |
| 87 + (instancetype)invalidMediaSubtypeFormat { |
| 88 AVCaptureDeviceFormatMock *instance = [[AVCaptureDeviceFormatMock alloc] |
| 89 initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8Planar |
| 90 minFps:0.0 |
| 91 maxFps:60.0]; |
| 92 return instance; |
| 93 } |
| 94 |
| 95 - (void)dealloc { |
| 96 if (_format != nil) { |
| 97 CFRelease(_format); |
| 98 _format = nil; |
| 99 } |
| 100 [super dealloc]; |
| 101 } |
| 102 |
| 103 // Redefinition of AVCaptureDevice methods we want to mock. |
| 104 - (CMVideoFormatDescriptionRef)formatDescription { |
| 105 return self.format; |
| 106 } |
| 107 |
| 108 - (NSArray *)videoSupportedFrameRateRanges { |
| 109 return @[ self.rangeMock ]; |
| 110 } |
| 111 |
| 112 @end |
| 113 |
| 114 TEST(AVFormatMapperTest, SuportedCricketFormatsWithInvalidFramerateFormats) { |
| 115 // given |
| 116 id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]]; |
| 117 |
| 118 // Valid media subtype, invalid framerate |
| 119 AVCaptureDeviceFormatMock* mock = |
| 120 [AVCaptureDeviceFormatMock invalidFpsFormat]; |
| 121 |
| 122 [[[mockDevice stub] andReturn:@[ mock ]] formats]; |
| 123 |
| 124 // when |
| 125 std::set<cricket::VideoFormat> result = |
| 126 webrtc::GetSupportedVideoFormatsForDevice(mockDevice); |
| 127 |
| 128 // then |
| 129 EXPECT_TRUE(result.empty()); |
| 130 } |
| 131 |
| 132 TEST(AVFormatMapperTest, SuportedCricketFormatsWithInvalidFormats) { |
| 133 // given |
| 134 id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]]; |
| 135 |
| 136 // Invalid media subtype, valid framerate |
| 137 AVCaptureDeviceFormatMock* mock = |
| 138 [AVCaptureDeviceFormatMock invalidMediaSubtypeFormat]; |
| 139 |
| 140 [[[mockDevice stub] andReturn:@[ mock ]] formats]; |
| 141 |
| 142 // when |
| 143 std::set<cricket::VideoFormat> result = |
| 144 webrtc::GetSupportedVideoFormatsForDevice(mockDevice); |
| 145 |
| 146 // then |
| 147 EXPECT_TRUE(result.empty()); |
| 148 } |
| 149 |
| 150 TEST(AVFormatMapperTest, SuportedCricketFormats) { |
| 151 // given |
| 152 id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]]; |
| 153 |
| 154 // valid media subtype, valid framerate |
| 155 AVCaptureDeviceFormatMock* mock = [AVCaptureDeviceFormatMock validFormat]; |
| 156 [[[mockDevice stub] andReturn:@[ mock ]] formats]; |
| 157 |
| 158 // when |
| 159 std::set<cricket::VideoFormat> result = |
| 160 webrtc::GetSupportedVideoFormatsForDevice(mockDevice); |
| 161 |
| 162 // then |
| 163 EXPECT_EQ(1, result.size()); |
| 164 |
| 165 // make sure the set has the expected format |
| 166 EXPECT_EQ(expectedFormat, *result.begin()); |
| 167 } |
| 168 |
| 169 TEST(AVFormatMapperTest, MediaSubtypePreference) { |
| 170 // given |
| 171 id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]]; |
| 172 |
| 173 // valid media subtype, valid framerate |
| 174 AVCaptureDeviceFormatMock* mockOne = [[AVCaptureDeviceFormatMock alloc] |
| 175 initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange |
| 176 minFps:0.0 |
| 177 maxFps:30.0]; |
| 178 |
| 179 // valid media subtype, valid framerate. |
| 180 // This media subtype should be the preffered one. |
| 181 AVCaptureDeviceFormatMock* mockTwo = [[AVCaptureDeviceFormatMock alloc] |
| 182 initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange |
| 183 minFps:0.0 |
| 184 maxFps:30.0]; |
| 185 |
| 186 [[[mockDevice stub] andReturnValue:@(YES)] |
| 187 lockForConfiguration:[OCMArg setTo:nil]]; |
| 188 [[mockDevice stub] unlockForConfiguration]; |
| 189 |
| 190 [[[mockDevice stub] andReturn:@[ mockOne, mockTwo ]] formats]; |
| 191 |
| 192 // to verify |
| 193 [[mockDevice expect] setActiveFormat:(AVCaptureDeviceFormat*)mockTwo]; |
| 194 [[mockDevice expect] |
| 195 setActiveVideoMinFrameDuration:CMTimeMake(1, kFramerate)]; |
| 196 |
| 197 // when |
| 198 bool resultFormat = |
| 199 webrtc::SetFormatForCaptureDevice(mockDevice, nil, expectedFormat); |
| 200 |
| 201 // then |
| 202 EXPECT_TRUE(resultFormat); |
| 203 [mockDevice verify]; |
| 204 } |
| 205 |
| 206 TEST(AVFormatMapperTest, SetFormatWhenDeviceCannotLock) { |
| 207 // given |
| 208 id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]]; |
| 209 [[[mockDevice stub] andReturnValue:@(NO)] |
| 210 lockForConfiguration:[OCMArg setTo:nil]]; |
| 211 |
| 212 [[[mockDevice stub] andReturn:@[]] formats]; |
| 213 |
| 214 // when |
| 215 bool resultFormat = webrtc::SetFormatForCaptureDevice(mockDevice, nil, |
| 216 cricket::VideoFormat()); |
| 217 |
| 218 // then |
| 219 EXPECT_FALSE(resultFormat); |
| 220 } |
| 221 |
| 222 TEST(AVFormatMapperTest, SetFormatWhenFormatIsIncompatible) { |
| 223 // given |
| 224 id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]]; |
| 225 [[[mockDevice stub] andReturn:@[]] formats]; |
| 226 [[[mockDevice stub] andReturnValue:@(YES)] |
| 227 lockForConfiguration:[OCMArg setTo:nil]]; |
| 228 |
| 229 NSException* exception = |
| 230 [NSException exceptionWithName:@"Test exception" |
| 231 reason:@"Raised from unit tests" |
| 232 userInfo:nil]; |
| 233 [[[mockDevice stub] andThrow:exception] setActiveFormat:[OCMArg any]]; |
| 234 [[mockDevice expect] unlockForConfiguration]; |
| 235 |
| 236 // when |
| 237 bool resultFormat = webrtc::SetFormatForCaptureDevice(mockDevice, nil, |
| 238 cricket::VideoFormat()); |
| 239 |
| 240 // then |
| 241 EXPECT_FALSE(resultFormat); |
| 242 [mockDevice verify]; |
| 243 } |
OLD | NEW |