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

Unified Diff: webrtc/sdk/objc/Framework/UnitTests/avformatmappertests.mm

Issue 2526813002: Add unit tests for avfoundation format mapper functions and fix wrong implementation. (Closed)
Patch Set: Remove static methods exposure Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/sdk/objc/Framework/UnitTests/avformatmappertests.mm
diff --git a/webrtc/sdk/objc/Framework/UnitTests/avformatmappertests.mm b/webrtc/sdk/objc/Framework/UnitTests/avformatmappertests.mm
new file mode 100644
index 0000000000000000000000000000000000000000..13fd9e74631a8aa71e1151004869760033a6f2bc
--- /dev/null
+++ b/webrtc/sdk/objc/Framework/UnitTests/avformatmappertests.mm
@@ -0,0 +1,285 @@
+/*
+ * Copyright 2016 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#import <Foundation/Foundation.h>
+#import <OCMock/OCMock.h>
+#include "avfoundationformatmapper.h"
+#include "webrtc/base/gunit.h"
+
+// Width and height don't play any role so lets use predefined values throughout
+// the tests.
+static const int kFormatWidth = 789;
+static const int kFormatHeight = 987;
+
+// Hardcoded framrate to be used throughout the tests.
+static const int kFramerate = 30;
+
+// Same width and height is used so it's ok to expect same cricket::VideoFormat
+static cricket::VideoFormat SupportedCricketVideoFormat() {
magjed_webrtc 2016/11/29 13:34:01 It should be possible to just have a constant for
daniela-webrtc 2016/11/29 16:13:57 Done.
+ return cricket::VideoFormat(kFormatWidth, kFormatHeight,
+ cricket::VideoFormat::FpsToInterval(kFramerate),
+ cricket::FOURCC_NV12);
+}
+
+// Mock class for AVCaptureDeviceFormat.
+// Custom implementation needed because OCMock cannot handle the
+// CMVideoDescriptionRef mocking.
+@interface AVCaptureDeviceFormatMock : NSObject {
+ CMVideoFormatDescriptionRef _format;
+ OCMockObject *_rangeMock;
+}
+
+- (instancetype)initWithMediaSubtype:(FourCharCode)subtype
+ minFps:(float)minFps
+ maxFps:(float)maxFps;
++ (instancetype)validFormat;
++ (instancetype)invalidFpsFormat;
++ (instancetype)invalidMediaSubtypeFormat;
+
+@end
+
+@implementation AVCaptureDeviceFormatMock
+
+- (instancetype)initWithMediaSubtype:(FourCharCode)subtype
+ minFps:(float)minFps
+ maxFps:(float)maxFps {
+ if (self = [super init]) {
+ CMVideoFormatDescriptionCreate(nil, subtype, kFormatWidth, kFormatHeight,
+ nil, &_format);
+ // We can use OCMock for the range.
+ _rangeMock = [OCMockObject mockForClass:[AVFrameRateRange class]];
+ [[[_rangeMock stub] andReturnValue:@(minFps)] minFrameRate];
+ [[[_rangeMock stub] andReturnValue:@(maxFps)] maxFrameRate];
+ }
+
+ return self;
+}
+
++ (instancetype)validFormat {
+ AVCaptureDeviceFormatMock *instance = [[AVCaptureDeviceFormatMock alloc]
+ initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
+ minFps:0.0
+ maxFps:30.0];
+ return instance;
+}
+
++ (instancetype)invalidFpsFormat {
+ AVCaptureDeviceFormatMock *instance = [[AVCaptureDeviceFormatMock alloc]
+ initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
+ minFps:0.0
+ maxFps:22.0];
+ return instance;
+}
+
++ (instancetype)invalidMediaSubtypeFormat {
+ AVCaptureDeviceFormatMock *instance = [[AVCaptureDeviceFormatMock alloc]
+ initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8Planar
+ minFps:0.0
+ maxFps:60.0];
+ return instance;
+}
+
+- (void)dealloc {
+ if (_rangeMock != nil) {
+ [_rangeMock dealloc];
+ _rangeMock = nil;
+ }
+ if (_format != nil) {
+ CFRelease(_format);
+ _format = nil;
+ }
+ [super dealloc];
+}
+
+// Redefinition of AVCaptureDevice methods we want to mock.
+- (CMVideoFormatDescriptionRef)formatDescription {
+ return _format;
+}
+
+- (NSArray *)videoSupportedFrameRateRanges {
+ return @[ _rangeMock ];
+}
+
+@end
+
+class AVFormatMapperTest : public ::testing::Test {
+ protected:
+ void testSuportedCricketFormatsWithInvalidFramerateFormats() {
+ // given
+ id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]];
+
+ // Valid media subtype, invalid framerate
+ AVCaptureDeviceFormatMock* mockOne =
+ [AVCaptureDeviceFormatMock invalidFpsFormat];
+ // Valid media subtype, valid framerate
+ AVCaptureDeviceFormatMock* mockTwo =
+ [AVCaptureDeviceFormatMock validFormat];
+
+ [[[mockDevice stub] andReturn:@[ mockOne, mockTwo ]] formats];
+
+ cricket::VideoFormat expected = SupportedCricketVideoFormat();
+
+ // when
+ std::set<cricket::VideoFormat> result =
+ webrtc::GetSupportedVideoFormatsForDevice(mockDevice);
+
+ // then
+ EXPECT_EQ(1, result.size());
+ EXPECT_EQ(1u, result.count(expected));
+ }
+
+ void testSuportedCricketFormatsWithInvalidMediaSubtypeFormats() {
+ // given
+ id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]];
+
+ // Invalid media subtype, valid framerate
+ AVCaptureDeviceFormatMock* mockOne =
+ [AVCaptureDeviceFormatMock invalidMediaSubtypeFormat];
+ // Valid media subtype, valid framerate
+ AVCaptureDeviceFormatMock* mockTwo =
+ [AVCaptureDeviceFormatMock validFormat];
+
+ [[[mockDevice stub] andReturn:@[ mockOne, mockTwo ]] formats];
+
+ cricket::VideoFormat expected = SupportedCricketVideoFormat();
+
+ // when
+ std::set<cricket::VideoFormat> result =
+ webrtc::GetSupportedVideoFormatsForDevice(mockDevice);
+
+ // then
+ EXPECT_EQ(1, result.size());
+ EXPECT_EQ(1u, result.count(expected));
+ }
+
+ void testSuportedCricketFormats() {
+ // given
+ id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]];
+
+ // valid media subtype, valid framerate
+ AVCaptureDeviceFormatMock* mockOne =
+ [AVCaptureDeviceFormatMock validFormat];
+ [[[mockDevice stub] andReturn:@[ mockOne ]] formats];
+
+ cricket::VideoFormat expectedFormat = SupportedCricketVideoFormat();
+
+ // when
+ std::set<cricket::VideoFormat> result =
+ webrtc::GetSupportedVideoFormatsForDevice(mockDevice);
+
+ // then
+ EXPECT_EQ(1, result.size());
+
+ // make sure the set has the expected format
+ EXPECT_EQ(1u, result.count(expectedFormat));
+ }
+
+ void testAVFormatForCricketFormat() {
magjed_webrtc 2016/11/29 13:34:01 nit: I would like to rename this test to something
daniela-webrtc 2016/11/29 16:13:57 Done.
+ // given
+ id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]];
+
+ // valid media subtype, valid framerate
+ AVCaptureDeviceFormatMock* mockOne = [[AVCaptureDeviceFormatMock alloc]
+ initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
+ minFps:0.0
+ maxFps:30.0];
+
+ // valid media subtype, valid framerate.
+ // This media subtype should be the preffered one.
+ AVCaptureDeviceFormatMock* mockTwo = [[AVCaptureDeviceFormatMock alloc]
+ initWithMediaSubtype:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
+ minFps:0.0
+ maxFps:30.0];
+
+ [[[mockDevice stub] andReturnValue:@(YES)]
+ lockForConfiguration:[OCMArg setTo:nil]];
+ [[mockDevice stub] unlockForConfiguration];
+
+ [[[mockDevice stub] andReturn:@[ mockOne, mockTwo ]] formats];
+
+ cricket::VideoFormat inputFormat = SupportedCricketVideoFormat();
+
+ // to verify
+ [[mockDevice expect] setActiveFormat:(AVCaptureDeviceFormat*)mockTwo];
+ [[mockDevice expect]
+ setActiveVideoMinFrameDuration:CMTimeMake(1, kFramerate)];
+
+ // when
+ bool resultFormat =
+ webrtc::SetFormatForCaptureDevice(mockDevice, nil, inputFormat);
+
+ // then
+ EXPECT_TRUE(resultFormat);
+ [mockDevice verify];
+ }
+
+ void testSetFormatWhenDeviceCannotLock() {
+ // given
+ id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]];
+ [[[mockDevice stub] andReturnValue:@(NO)]
+ lockForConfiguration:[OCMArg setTo:nil]];
+
+ [[[mockDevice stub] andReturn:@[]] formats];
+
+ // when
+ bool resultFormat = webrtc::SetFormatForCaptureDevice(
+ mockDevice, nil, cricket::VideoFormat());
+
+ // then
+ EXPECT_FALSE(resultFormat);
+ }
+
+ void testSetFormatWhenFormatIsIncompatible() {
+ // given
+ id mockDevice = [OCMockObject mockForClass:[AVCaptureDevice class]];
+ [[[mockDevice stub] andReturn:@[]] formats];
+ [[[mockDevice stub] andReturnValue:@(YES)]
+ lockForConfiguration:[OCMArg setTo:nil]];
+
+ NSException* exception =
+ [NSException exceptionWithName:@"Test exception"
+ reason:@"Raised from unit tests"
+ userInfo:nil];
+ [[[mockDevice stub] andThrow:exception] setActiveFormat:[OCMArg any]];
+ [[mockDevice expect] unlockForConfiguration];
+
+ // when
+ bool resultFormat = webrtc::SetFormatForCaptureDevice(
+ mockDevice, nil, cricket::VideoFormat());
+
+ // then
+ EXPECT_FALSE(resultFormat);
+ [mockDevice verify];
+ }
+};
+
+TEST_F(AVFormatMapperTest, SuportedCricketFormatsWithInvalidFramerateFormats) {
+ testSuportedCricketFormatsWithInvalidFramerateFormats();
+}
+
+TEST_F(AVFormatMapperTest, SuportedCricketFormatsWithInvalidFormats) {
+ testSuportedCricketFormatsWithInvalidMediaSubtypeFormats();
+}
+
+TEST_F(AVFormatMapperTest, SuportedCricketFormats) {
+ testSuportedCricketFormats();
+}
+
+TEST_F(AVFormatMapperTest, AVFormatForCricketFormat) {
+ testAVFormatForCricketFormat();
+}
+
+TEST_F(AVFormatMapperTest, SetFormatWhenDeviceCannotLock) {
+ testSetFormatWhenDeviceCannotLock();
+}
+
+TEST_F(AVFormatMapperTest, SetFormatWhenFormatIsIncompatible) {
+ testSetFormatWhenFormatIsIncompatible();
+}
« no previous file with comments | « webrtc/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698