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 | 7583390 | 2017-06-21 10:53:19 +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 | 7583390 | 2017-06-21 10:53:19 +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 | 7583390 | 2017-06-21 10:53:19 +0200 | [diff] [blame^] | 65 | if (hasCVPixelBuffer) { |
| 66 | OCMStub([frameMock buffer]) |
| 67 | .andReturn( |
| 68 | [[RTCCVPixelBuffer alloc] initWithPixelBuffer:(CVPixelBufferRef)[OCMArg anyPointer]]); |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 69 | } else { |
Anders Carlsson | 7583390 | 2017-06-21 10:53:19 +0200 | [diff] [blame^] | 70 | OCMStub([frameMock buffer]).andReturn([[RTCI420Buffer alloc] initWithWidth:200 height:200]); |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 71 | } |
| 72 | return frameMock; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 73 | } |
| 74 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 75 | - (id)rendererMockWithSuccessfulSetup:(BOOL)sucess { |
| 76 | id rendererMock = OCMProtocolMock(@protocol(RTCMTLRenderer)); |
| 77 | OCMStub([rendererMock addRenderingDestination:[OCMArg any]]).andReturn(sucess); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 78 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 79 | return rendererMock; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 80 | } |
| 81 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 82 | #pragma mark - Test cases |
| 83 | - (void)testInitAssertsIfMetalUnavailabe { |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 84 | // given |
| 85 | OCMStub([self.classMock isMetalAvailable]).andReturn(NO); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 86 | |
| 87 | // when |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 88 | BOOL asserts = NO; |
| 89 | @try { |
| 90 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero]; |
| 91 | (void)realView; |
| 92 | } @catch (NSException *ex) { |
| 93 | asserts = YES; |
| 94 | } |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 95 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 96 | EXPECT_TRUE(asserts); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | - (void)testRTCVideoRenderNilFrameCallback { |
| 100 | // given |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 101 | OCMStub([self.classMock isMetalAvailable]).andReturn(YES); |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 102 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] init]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 103 | self.frameMock = OCMClassMock([RTCVideoFrame class]); |
| 104 | |
Anders Carlsson | 7583390 | 2017-06-21 10:53:19 +0200 | [diff] [blame^] | 105 | [[self.frameMock reject] buffer]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 106 | [[self.classMock reject] createNV12Renderer]; |
| 107 | [[self.classMock reject] createI420Renderer]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 108 | |
| 109 | // when |
| 110 | [realView renderFrame:nil]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 111 | [realView drawInMTKView:self.metalViewMock]; |
| 112 | |
| 113 | // then |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 114 | [self.frameMock verify]; |
| 115 | [self.classMock verify]; |
| 116 | } |
| 117 | |
| 118 | - (void)testRTCVideoRenderFrameCallbackI420 { |
| 119 | // given |
| 120 | OCMStub([self.classMock isMetalAvailable]).andReturn(YES); |
| 121 | self.rendererI420Mock = [self rendererMockWithSuccessfulSetup:YES]; |
Anders Carlsson | 7583390 | 2017-06-21 10:53:19 +0200 | [diff] [blame^] | 122 | self.frameMock = [self frameMockWithCVPixelBuffer:NO]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 123 | |
| 124 | OCMExpect([self.rendererI420Mock drawFrame:self.frameMock]); |
| 125 | OCMExpect([self.classMock createI420Renderer]).andReturn(self.rendererI420Mock); |
| 126 | [[self.classMock reject] createNV12Renderer]; |
| 127 | |
| 128 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] init]; |
| 129 | |
| 130 | // when |
| 131 | [realView renderFrame:self.frameMock]; |
| 132 | [realView drawInMTKView:self.metalViewMock]; |
| 133 | |
| 134 | // then |
| 135 | [self.rendererI420Mock verify]; |
| 136 | [self.classMock verify]; |
| 137 | } |
| 138 | |
| 139 | - (void)testRTCVideoRenderFrameCallbackNV12 { |
| 140 | // given |
| 141 | OCMStub([self.classMock isMetalAvailable]).andReturn(YES); |
| 142 | self.rendererNV12Mock = [self rendererMockWithSuccessfulSetup:YES]; |
Anders Carlsson | 7583390 | 2017-06-21 10:53:19 +0200 | [diff] [blame^] | 143 | self.frameMock = [self frameMockWithCVPixelBuffer:YES]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 144 | |
| 145 | OCMExpect([self.rendererNV12Mock drawFrame:self.frameMock]); |
| 146 | OCMExpect([self.classMock createNV12Renderer]).andReturn(self.rendererNV12Mock); |
| 147 | [[self.classMock reject] createI420Renderer]; |
| 148 | |
| 149 | RTCMTLVideoView *realView = [[RTCMTLVideoView alloc] init]; |
| 150 | |
| 151 | // when |
| 152 | [realView renderFrame:self.frameMock]; |
| 153 | [realView drawInMTKView:self.metalViewMock]; |
| 154 | |
| 155 | // then |
| 156 | [self.rendererNV12Mock verify]; |
| 157 | [self.classMock verify]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 158 | } |
| 159 | @end |
| 160 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 161 | TEST(RTCMTLVideoViewTests, InitAssertsIfMetalUnavailabe) { |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 162 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 163 | [test setup]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 164 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 165 | [test testInitAssertsIfMetalUnavailabe]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 166 | [test tearDown]; |
| 167 | } |
| 168 | |
| 169 | TEST(RTCMTLVideoViewTests, RTCVideoRenderNilFrameCallback) { |
| 170 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 171 | [test setup]; |
| 172 | [test testRTCVideoRenderNilFrameCallback]; |
| 173 | [test tearDown]; |
| 174 | } |
| 175 | |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 176 | TEST(RTCMTLVideoViewTests, RTCVideoRenderFrameCallbackI420) { |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 177 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 178 | [test setup]; |
denicija | d208815 | 2017-04-28 02:14:54 -0700 | [diff] [blame] | 179 | |
| 180 | [test testRTCVideoRenderFrameCallbackI420]; |
| 181 | [test tearDown]; |
| 182 | } |
| 183 | |
| 184 | TEST(RTCMTLVideoViewTests, RTCVideoRenderFrameCallbackNV12) { |
| 185 | RTCMTLVideoViewTests *test = [[RTCMTLVideoViewTests alloc] init]; |
| 186 | [test setup]; |
| 187 | |
| 188 | [test testRTCVideoRenderFrameCallbackNV12]; |
kthelgason | 954d9b9 | 2017-03-09 03:36:58 -0800 | [diff] [blame] | 189 | [test tearDown]; |
| 190 | } |