You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import <Foundation/Foundation.h>
  11. #import <OCMock/OCMock.h>
  12. #include "rtc_base/gunit.h"
  13. #import "components/audio/RTCAudioSession+Private.h"
  14. #import "components/audio/RTCAudioSession.h"
  15. #import "components/audio/RTCAudioSessionConfiguration.h"
  16. @interface RTCAudioSession (UnitTesting)
  17. - (instancetype)initWithAudioSession:(id)audioSession;
  18. @end
  19. @interface MockAVAudioSession : NSObject
  20. @property (nonatomic, readwrite, assign) float outputVolume;
  21. @end
  22. @implementation MockAVAudioSession
  23. @synthesize outputVolume = _outputVolume;
  24. @end
  25. @interface RTCAudioSessionTestDelegate : NSObject <RTCAudioSessionDelegate>
  26. @property (nonatomic, readonly) float outputVolume;
  27. @end
  28. @implementation RTCAudioSessionTestDelegate
  29. @synthesize outputVolume = _outputVolume;
  30. - (instancetype)init {
  31. if (self = [super init]) {
  32. _outputVolume = -1;
  33. }
  34. return self;
  35. }
  36. - (void)audioSessionDidBeginInterruption:(RTCAudioSession *)session {
  37. }
  38. - (void)audioSessionDidEndInterruption:(RTCAudioSession *)session
  39. shouldResumeSession:(BOOL)shouldResumeSession {
  40. }
  41. - (void)audioSessionDidChangeRoute:(RTCAudioSession *)session
  42. reason:(AVAudioSessionRouteChangeReason)reason
  43. previousRoute:(AVAudioSessionRouteDescription *)previousRoute {
  44. }
  45. - (void)audioSessionMediaServerTerminated:(RTCAudioSession *)session {
  46. }
  47. - (void)audioSessionMediaServerReset:(RTCAudioSession *)session {
  48. }
  49. - (void)audioSessionShouldConfigure:(RTCAudioSession *)session {
  50. }
  51. - (void)audioSessionShouldUnconfigure:(RTCAudioSession *)session {
  52. }
  53. - (void)audioSession:(RTCAudioSession *)audioSession
  54. didChangeOutputVolume:(float)outputVolume {
  55. _outputVolume = outputVolume;
  56. }
  57. @end
  58. // A delegate that adds itself to the audio session on init and removes itself
  59. // in its dealloc.
  60. @interface RTCTestRemoveOnDeallocDelegate : RTCAudioSessionTestDelegate
  61. @end
  62. @implementation RTCTestRemoveOnDeallocDelegate
  63. - (instancetype)init {
  64. if (self = [super init]) {
  65. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  66. [session addDelegate:self];
  67. }
  68. return self;
  69. }
  70. - (void)dealloc {
  71. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  72. [session removeDelegate:self];
  73. }
  74. @end
  75. @interface RTCAudioSessionTest : NSObject
  76. - (void)testLockForConfiguration;
  77. @end
  78. @implementation RTCAudioSessionTest
  79. - (void)testLockForConfiguration {
  80. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  81. for (size_t i = 0; i < 2; i++) {
  82. [session lockForConfiguration];
  83. EXPECT_TRUE(session.isLocked);
  84. }
  85. for (size_t i = 0; i < 2; i++) {
  86. EXPECT_TRUE(session.isLocked);
  87. [session unlockForConfiguration];
  88. }
  89. EXPECT_FALSE(session.isLocked);
  90. }
  91. - (void)testAddAndRemoveDelegates {
  92. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  93. NSMutableArray *delegates = [NSMutableArray array];
  94. const size_t count = 5;
  95. for (size_t i = 0; i < count; ++i) {
  96. RTCAudioSessionTestDelegate *delegate =
  97. [[RTCAudioSessionTestDelegate alloc] init];
  98. [session addDelegate:delegate];
  99. [delegates addObject:delegate];
  100. EXPECT_EQ(i + 1, session.delegates.size());
  101. }
  102. [delegates enumerateObjectsUsingBlock:^(RTCAudioSessionTestDelegate *obj,
  103. NSUInteger idx,
  104. BOOL *stop) {
  105. [session removeDelegate:obj];
  106. }];
  107. EXPECT_EQ(0u, session.delegates.size());
  108. }
  109. - (void)testPushDelegate {
  110. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  111. NSMutableArray *delegates = [NSMutableArray array];
  112. const size_t count = 2;
  113. for (size_t i = 0; i < count; ++i) {
  114. RTCAudioSessionTestDelegate *delegate =
  115. [[RTCAudioSessionTestDelegate alloc] init];
  116. [session addDelegate:delegate];
  117. [delegates addObject:delegate];
  118. }
  119. // Test that it gets added to the front of the list.
  120. RTCAudioSessionTestDelegate *pushedDelegate =
  121. [[RTCAudioSessionTestDelegate alloc] init];
  122. [session pushDelegate:pushedDelegate];
  123. EXPECT_TRUE(pushedDelegate == session.delegates[0]);
  124. // Test that it stays at the front of the list.
  125. for (size_t i = 0; i < count; ++i) {
  126. RTCAudioSessionTestDelegate *delegate =
  127. [[RTCAudioSessionTestDelegate alloc] init];
  128. [session addDelegate:delegate];
  129. [delegates addObject:delegate];
  130. }
  131. EXPECT_TRUE(pushedDelegate == session.delegates[0]);
  132. // Test that the next one goes to the front too.
  133. pushedDelegate = [[RTCAudioSessionTestDelegate alloc] init];
  134. [session pushDelegate:pushedDelegate];
  135. EXPECT_TRUE(pushedDelegate == session.delegates[0]);
  136. }
  137. // Tests that delegates added to the audio session properly zero out. This is
  138. // checking an implementation detail (that vectors of __weak work as expected).
  139. - (void)testZeroingWeakDelegate {
  140. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  141. @autoreleasepool {
  142. // Add a delegate to the session. There should be one delegate at this
  143. // point.
  144. RTCAudioSessionTestDelegate *delegate =
  145. [[RTCAudioSessionTestDelegate alloc] init];
  146. [session addDelegate:delegate];
  147. EXPECT_EQ(1u, session.delegates.size());
  148. EXPECT_TRUE(session.delegates[0]);
  149. }
  150. // The previously created delegate should've de-alloced, leaving a nil ptr.
  151. EXPECT_FALSE(session.delegates[0]);
  152. RTCAudioSessionTestDelegate *delegate =
  153. [[RTCAudioSessionTestDelegate alloc] init];
  154. [session addDelegate:delegate];
  155. // On adding a new delegate, nil ptrs should've been cleared.
  156. EXPECT_EQ(1u, session.delegates.size());
  157. EXPECT_TRUE(session.delegates[0]);
  158. }
  159. // Tests that we don't crash when removing delegates in dealloc.
  160. // Added as a regression test.
  161. - (void)testRemoveDelegateOnDealloc {
  162. @autoreleasepool {
  163. RTCTestRemoveOnDeallocDelegate *delegate =
  164. [[RTCTestRemoveOnDeallocDelegate alloc] init];
  165. EXPECT_TRUE(delegate);
  166. }
  167. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  168. EXPECT_EQ(0u, session.delegates.size());
  169. }
  170. - (void)testAudioSessionActivation {
  171. RTCAudioSession *audioSession = [RTCAudioSession sharedInstance];
  172. EXPECT_EQ(0, audioSession.activationCount);
  173. [audioSession audioSessionDidActivate:[AVAudioSession sharedInstance]];
  174. EXPECT_EQ(1, audioSession.activationCount);
  175. [audioSession audioSessionDidDeactivate:[AVAudioSession sharedInstance]];
  176. EXPECT_EQ(0, audioSession.activationCount);
  177. }
  178. // Hack - fixes OCMVerify link error
  179. // Link error is: Undefined symbols for architecture i386:
  180. // "OCMMakeLocation(objc_object*, char const*, int)", referenced from:
  181. // -[RTCAudioSessionTest testConfigureWebRTCSession] in RTCAudioSessionTest.o
  182. // ld: symbol(s) not found for architecture i386
  183. // REASON: https://github.com/erikdoe/ocmock/issues/238
  184. OCMLocation *OCMMakeLocation(id testCase, const char *fileCString, int line){
  185. return [OCMLocation locationWithTestCase:testCase
  186. file:[NSString stringWithUTF8String:fileCString]
  187. line:line];
  188. }
  189. - (void)testConfigureWebRTCSession {
  190. NSError *error = nil;
  191. void (^setActiveBlock)(NSInvocation *invocation) = ^(NSInvocation *invocation) {
  192. __autoreleasing NSError **retError;
  193. [invocation getArgument:&retError atIndex:4];
  194. *retError = [NSError errorWithDomain:@"AVAudioSession"
  195. code:AVAudioSessionErrorInsufficientPriority
  196. userInfo:nil];
  197. BOOL failure = NO;
  198. [invocation setReturnValue:&failure];
  199. };
  200. id mockAVAudioSession = OCMPartialMock([AVAudioSession sharedInstance]);
  201. OCMStub([[mockAVAudioSession ignoringNonObjectArgs]
  202. setActive:YES withOptions:0 error:((NSError __autoreleasing **)[OCMArg anyPointer])]).
  203. andDo(setActiveBlock);
  204. id mockAudioSession = OCMPartialMock([RTCAudioSession sharedInstance]);
  205. OCMStub([mockAudioSession session]).andReturn(mockAVAudioSession);
  206. RTCAudioSession *audioSession = mockAudioSession;
  207. EXPECT_EQ(0, audioSession.activationCount);
  208. [audioSession lockForConfiguration];
  209. EXPECT_TRUE([audioSession checkLock:nil]);
  210. // configureWebRTCSession is forced to fail in the above mock interface,
  211. // so activationCount should remain 0
  212. OCMExpect([[mockAVAudioSession ignoringNonObjectArgs]
  213. setActive:YES withOptions:0 error:((NSError __autoreleasing **)[OCMArg anyPointer])]).
  214. andDo(setActiveBlock);
  215. OCMExpect([mockAudioSession session]).andReturn(mockAVAudioSession);
  216. EXPECT_FALSE([audioSession configureWebRTCSession:&error]);
  217. EXPECT_EQ(0, audioSession.activationCount);
  218. id session = audioSession.session;
  219. EXPECT_EQ(session, mockAVAudioSession);
  220. EXPECT_EQ(NO, [mockAVAudioSession setActive:YES withOptions:0 error:&error]);
  221. [audioSession unlockForConfiguration];
  222. OCMVerify([mockAudioSession session]);
  223. OCMVerify([[mockAVAudioSession ignoringNonObjectArgs] setActive:YES withOptions:0 error:&error]);
  224. OCMVerify([[mockAVAudioSession ignoringNonObjectArgs] setActive:NO withOptions:0 error:&error]);
  225. [mockAVAudioSession stopMocking];
  226. [mockAudioSession stopMocking];
  227. }
  228. - (void)testAudioVolumeDidNotify {
  229. MockAVAudioSession *mockAVAudioSession = [[MockAVAudioSession alloc] init];
  230. RTCAudioSession *session = [[RTCAudioSession alloc] initWithAudioSession:mockAVAudioSession];
  231. RTCAudioSessionTestDelegate *delegate =
  232. [[RTCAudioSessionTestDelegate alloc] init];
  233. [session addDelegate:delegate];
  234. float expectedVolume = 0.75;
  235. mockAVAudioSession.outputVolume = expectedVolume;
  236. EXPECT_EQ(expectedVolume, delegate.outputVolume);
  237. }
  238. @end
  239. namespace webrtc {
  240. class AudioSessionTest : public ::testing::Test {
  241. protected:
  242. void TearDown() override {
  243. RTCAudioSession *session = [RTCAudioSession sharedInstance];
  244. for (id<RTCAudioSessionDelegate> delegate : session.delegates) {
  245. [session removeDelegate:delegate];
  246. }
  247. }
  248. };
  249. TEST_F(AudioSessionTest, LockForConfiguration) {
  250. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  251. [test testLockForConfiguration];
  252. }
  253. TEST_F(AudioSessionTest, AddAndRemoveDelegates) {
  254. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  255. [test testAddAndRemoveDelegates];
  256. }
  257. TEST_F(AudioSessionTest, PushDelegate) {
  258. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  259. [test testPushDelegate];
  260. }
  261. TEST_F(AudioSessionTest, ZeroingWeakDelegate) {
  262. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  263. [test testZeroingWeakDelegate];
  264. }
  265. TEST_F(AudioSessionTest, RemoveDelegateOnDealloc) {
  266. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  267. [test testRemoveDelegateOnDealloc];
  268. }
  269. TEST_F(AudioSessionTest, AudioSessionActivation) {
  270. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  271. [test testAudioSessionActivation];
  272. }
  273. TEST_F(AudioSessionTest, ConfigureWebRTCSession) {
  274. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  275. [test testConfigureWebRTCSession];
  276. }
  277. TEST_F(AudioSessionTest, AudioVolumeDidNotify) {
  278. RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init];
  279. [test testAudioVolumeDidNotify];
  280. }
  281. } // namespace webrtc