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