kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [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 | |
| 11 | #import <Foundation/Foundation.h> |
| 12 | #import <OCMock/OCMock.h> |
| 13 | |
| 14 | #include "webrtc/base/gunit.h" |
| 15 | |
| 16 | #include <Metal/RTCMTLNV12Renderer.h> |
| 17 | #include <WebRTC/RTCMTLVideoView.h> |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 18 | #include <WebRTC/RTCVideoFrameBuffer.h> |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 19 | |
| 20 | // Extension of RTCMTLVideoView for testing purposes. |
| 21 | @interface RTCMTLVideoView (Testing) |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 22 | |
| 23 | + (BOOL)isMetalAvailable; |
| 24 | + (UIView*)createMetalView:(CGRect)frame; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 25 | + (id<RTCMTLRenderer>)createNV12Renderer; |
| 26 | + (id<RTCMTLRenderer>)createI420Renderer; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 27 | - (void)drawInMTKView:(id)view; |
| 28 | @end |
| 29 | |
| 30 | @interface RTCMTLVideoViewTests : NSObject |
| 31 | @property(nonatomic, strong) id classMock; |
| 32 | @property(nonatomic, strong) id metalViewMock; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 33 | @property(nonatomic, strong) id rendererNV12Mock; |
| 34 | @property(nonatomic, strong) id rendererI420Mock; |
| 35 | @property(nonatomic, strong) id frameMock; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 36 | @end |
| 37 | |
| 38 | @implementation RTCMTLVideoViewTests |
| 39 | |
| 40 | @synthesize classMock = _classMock; |
| 41 | @synthesize metalViewMock = _metalViewMock; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 42 | @synthesize rendererNV12Mock = _rendererNV12Mock; |
| 43 | @synthesize rendererI420Mock = _rendererI420Mock; |
| 44 | @synthesize frameMock = _frameMock; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 45 | |
| 46 | - (void)setup { |
| 47 | self.classMock = OCMClassMock([RTCMTLVideoView class]); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | - (void)tearDown { |
| 51 | [self.classMock stopMocking]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 52 | [self.rendererI420Mock stopMocking]; |
| 53 | [self.rendererNV12Mock stopMocking]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 54 | [self.metalViewMock stopMocking]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 55 | [self.frameMock stopMocking]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 56 | self.classMock = nil; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 57 | self.rendererI420Mock = nil; |
| 58 | self.rendererNV12Mock = nil; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 59 | self.metalViewMock = nil; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 60 | self.frameMock = nil; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 63 | - (id)frameMockWithCVPixelBuffer:(BOOL)hasCVPixelBuffer { |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 64 | id frameMock = OCMClassMock([RTCVideoFrame class]); |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 65 | if (hasCVPixelBuffer) { |
| 66 | CVPixelBufferRef pixelBufferRef; |
| 67 | CVPixelBufferCreate(kCFAllocatorDefault, |
| 68 | 200, |
| 69 | 200, |
| 70 | kCVPixelFormatType_420YpCbCr8Planar, |
| 71 | nullptr, |
| 72 | &pixelBufferRef); |
| 73 | OCMStub([frameMock buffer]) |
| 74 | .andReturn([[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBufferRef]); |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 75 | } else { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 76 | OCMStub([frameMock buffer]).andReturn([[RTCI420Buffer alloc] initWithWidth:200 height:200]); |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 77 | } |
| 78 | return frameMock; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 79 | } |
| 80 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 81 | - (id)rendererMockWithSuccessfulSetup:(BOOL)sucess { |
| 82 | id rendererMock = OCMProtocolMock(@protocol(RTCMTLRenderer)); |
| 83 | OCMStub([rendererMock addRenderingDestination:[OCMArg any]]).andReturn(sucess); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 84 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 85 | return rendererMock; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 86 | } |
| 87 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 88 | #pragma mark - Test cases |
| 89 | - (void)testInitAssertsIfMetalUnavailabe { |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 90 | // given |
| 91 | OCMStub([self.classMock isMetalAvailable]).andReturn(NO); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 92 | |
| 93 | // when |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 94 | BOOL asserts = NO; |
| 95 | @try { |
| 96 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero]; |
| 97 | (void)realView; |
| 98 | } @catch (NSException *ex) { |
| 99 | asserts = YES; |
| 100 | } |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 101 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(asserts); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | - (void)testRTCVideoRenderNilFrameCallback { |
| 106 | // given |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 107 | OCMStub([self.classMock isMetalAvailable]).andReturn(YES); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 108 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] init]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 109 | self.frameMock = OCMClassMock([RTCVideoFrame class]); |
| 110 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 111 | [[self.frameMock reject] buffer]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 112 | [[self.classMock reject] createNV12Renderer]; |
| 113 | [[self.classMock reject] createI420Renderer]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 114 | |
| 115 | // when |
| 116 | [realView renderFrame:nil]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 117 | [realView drawInMTKView:self.metalViewMock]; |
| 118 | |
| 119 | // then |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 120 | [self.frameMock verify]; |
| 121 | [self.classMock verify]; |
| 122 | } |
| 123 | |
| 124 | - (void)testRTCVideoRenderFrameCallbackI420 { |
| 125 | // given |
| 126 | OCMStub([self.classMock isMetalAvailable]).andReturn(YES); |
| 127 | self.rendererI420Mock = [self rendererMockWithSuccessfulSetup:YES]; |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 128 | self.frameMock = [self frameMockWithCVPixelBuffer:NO]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 129 | |
| 130 | OCMExpect([self.rendererI420Mock drawFrame:self.frameMock]); |
| 131 | OCMExpect([self.classMock createI420Renderer]).andReturn(self.rendererI420Mock); |
| 132 | [[self.classMock reject] createNV12Renderer]; |
| 133 | |
| 134 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] init]; |
| 135 | |
| 136 | // when |
| 137 | [realView renderFrame:self.frameMock]; |
| 138 | [realView drawInMTKView:self.metalViewMock]; |
| 139 | |
| 140 | // then |
| 141 | [self.rendererI420Mock verify]; |
| 142 | [self.classMock verify]; |
| 143 | } |
| 144 | |
| 145 | - (void)testRTCVideoRenderFrameCallbackNV12 { |
| 146 | // given |
| 147 | OCMStub([self.classMock isMetalAvailable]).andReturn(YES); |
| 148 | self.rendererNV12Mock = [self rendererMockWithSuccessfulSetup:YES]; |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 149 | self.frameMock = [self frameMockWithCVPixelBuffer:YES]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 150 | |
| 151 | OCMExpect([self.rendererNV12Mock drawFrame:self.frameMock]); |
| 152 | OCMExpect([self.classMock createNV12Renderer]).andReturn(self.rendererNV12Mock); |
| 153 | [[self.classMock reject] createI420Renderer]; |
| 154 | |
| 155 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] init]; |
| 156 | |
| 157 | // when |
| 158 | [realView renderFrame:self.frameMock]; |
| 159 | [realView drawInMTKView:self.metalViewMock]; |
| 160 | |
| 161 | // then |
| 162 | [self.rendererNV12Mock verify]; |
| 163 | [self.classMock verify]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 164 | } |
| 165 | @end |
| 166 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 167 | TEST(RTCMTLVideoViewTests, InitAssertsIfMetalUnavailabe) { |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 168 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 169 | [test setup]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 170 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 171 | [test testInitAssertsIfMetalUnavailabe]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 172 | [test tearDown]; |
| 173 | } |
| 174 | |
| 175 | TEST(RTCMTLVideoViewTests, RTCVideoRenderNilFrameCallback) { |
| 176 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 177 | [test setup]; |
| 178 | [test testRTCVideoRenderNilFrameCallback]; |
| 179 | [test tearDown]; |
| 180 | } |
| 181 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 182 | TEST(RTCMTLVideoViewTests, RTCVideoRenderFrameCallbackI420) { |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 183 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 184 | [test setup]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 185 | |
| 186 | [test testRTCVideoRenderFrameCallbackI420]; |
| 187 | [test tearDown]; |
| 188 | } |
| 189 | |
magjed | 587ff11 | 2017-06-26 03:11:51 -0700 | [diff] [blame^] | 190 | TEST(RTCMTLVideoViewTests, RTCVideoRenderFrameCallbackNV12) { |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 191 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 192 | [test setup]; |
| 193 | |
| 194 | [test testRTCVideoRenderFrameCallbackNV12]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 195 | [test tearDown]; |
| 196 | } |