OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import <Foundation/Foundation.h> | 11 #import <Foundation/Foundation.h> |
12 | 12 |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" | 15 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" |
| 16 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession+Private.h" |
| 17 |
| 18 @interface RTCAudioSessionTestDelegate : NSObject <RTCAudioSessionDelegate> |
| 19 @end |
| 20 |
| 21 @implementation RTCAudioSessionTestDelegate |
| 22 |
| 23 - (void)audioSessionDidBeginInterruption:(RTCAudioSession *)session { |
| 24 } |
| 25 |
| 26 - (void)audioSessionDidEndInterruption:(RTCAudioSession *)session |
| 27 shouldResumeSession:(BOOL)shouldResumeSession { |
| 28 } |
| 29 |
| 30 - (void)audioSessionDidChangeRoute:(RTCAudioSession *)session |
| 31 reason:(AVAudioSessionRouteChangeReason)reason |
| 32 previousRoute:(AVAudioSessionRouteDescription *)previousRoute { |
| 33 } |
| 34 |
| 35 - (void)audioSessionMediaServicesWereLost:(RTCAudioSession *)session { |
| 36 } |
| 37 |
| 38 - (void)audioSessionMediaServicesWereReset:(RTCAudioSession *)session { |
| 39 } |
| 40 |
| 41 - (void)audioSessionShouldConfigure:(RTCAudioSession *)session { |
| 42 } |
| 43 |
| 44 - (void)audioSessionShouldUnconfigure:(RTCAudioSession *)session { |
| 45 } |
| 46 |
| 47 @end |
| 48 |
16 | 49 |
17 @interface RTCAudioSessionTest : NSObject | 50 @interface RTCAudioSessionTest : NSObject |
18 | 51 |
19 - (void)testLockForConfiguration; | 52 - (void)testLockForConfiguration; |
20 | 53 |
21 @end | 54 @end |
22 | 55 |
23 @implementation RTCAudioSessionTest | 56 @implementation RTCAudioSessionTest |
24 | 57 |
25 - (void)testLockForConfiguration { | 58 - (void)testLockForConfiguration { |
26 RTCAudioSession *session = [RTCAudioSession sharedInstance]; | 59 RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
27 | 60 |
28 for (size_t i = 0; i < 2; i++) { | 61 for (size_t i = 0; i < 2; i++) { |
29 [session lockForConfiguration]; | 62 [session lockForConfiguration]; |
30 EXPECT_TRUE(session.isLocked); | 63 EXPECT_TRUE(session.isLocked); |
31 } | 64 } |
32 for (size_t i = 0; i < 2; i++) { | 65 for (size_t i = 0; i < 2; i++) { |
33 EXPECT_TRUE(session.isLocked); | 66 EXPECT_TRUE(session.isLocked); |
34 [session unlockForConfiguration]; | 67 [session unlockForConfiguration]; |
35 } | 68 } |
36 EXPECT_FALSE(session.isLocked); | 69 EXPECT_FALSE(session.isLocked); |
37 } | 70 } |
38 | 71 |
| 72 - (void)testAddAndRemoveDelegates { |
| 73 RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 74 NSMutableArray *delegates = [NSMutableArray array]; |
| 75 const size_t count = 5; |
| 76 for (size_t i = 0; i < count; ++i) { |
| 77 RTCAudioSessionTestDelegate *delegate = |
| 78 [[RTCAudioSessionTestDelegate alloc] init]; |
| 79 [session addDelegate:delegate]; |
| 80 [delegates addObject:delegate]; |
| 81 EXPECT_EQ(i + 1, session.delegates.size()); |
| 82 } |
| 83 [delegates enumerateObjectsUsingBlock:^(RTCAudioSessionTestDelegate *obj, |
| 84 NSUInteger idx, |
| 85 BOOL *stop) { |
| 86 [session removeDelegate:obj]; |
| 87 }]; |
| 88 EXPECT_EQ(0u, session.delegates.size()); |
| 89 } |
| 90 |
| 91 - (void)testPushDelegate { |
| 92 RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 93 NSMutableArray *delegates = [NSMutableArray array]; |
| 94 const size_t count = 2; |
| 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 } |
| 101 // Test that it gets added to the front of the list. |
| 102 RTCAudioSessionTestDelegate *pushedDelegate = |
| 103 [[RTCAudioSessionTestDelegate alloc] init]; |
| 104 [session pushDelegate:pushedDelegate]; |
| 105 EXPECT_TRUE(pushedDelegate == session.delegates[0]); |
| 106 |
| 107 // Test that it stays at the front of the list. |
| 108 for (size_t i = 0; i < count; ++i) { |
| 109 RTCAudioSessionTestDelegate *delegate = |
| 110 [[RTCAudioSessionTestDelegate alloc] init]; |
| 111 [session addDelegate:delegate]; |
| 112 [delegates addObject:delegate]; |
| 113 } |
| 114 EXPECT_TRUE(pushedDelegate == session.delegates[0]); |
| 115 |
| 116 // Test that the next one goes to the front too. |
| 117 pushedDelegate = [[RTCAudioSessionTestDelegate alloc] init]; |
| 118 [session pushDelegate:pushedDelegate]; |
| 119 EXPECT_TRUE(pushedDelegate == session.delegates[0]); |
| 120 } |
| 121 |
| 122 // Tests that delegates added to the audio session properly zero out. This is |
| 123 // checking an implementation detail (that vectors of __weak work as expected). |
| 124 - (void)testZeroingWeakDelegate { |
| 125 RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 126 @autoreleasepool { |
| 127 // Add a delegate to the session. There should be one delegate at this |
| 128 // point. |
| 129 RTCAudioSessionTestDelegate *delegate = |
| 130 [[RTCAudioSessionTestDelegate alloc] init]; |
| 131 [session addDelegate:delegate]; |
| 132 EXPECT_EQ(1u, session.delegates.size()); |
| 133 EXPECT_TRUE(session.delegates[0]); |
| 134 } |
| 135 // The previously created delegate should've de-alloced, leaving a nil ptr. |
| 136 EXPECT_FALSE(session.delegates[0]); |
| 137 RTCAudioSessionTestDelegate *delegate = |
| 138 [[RTCAudioSessionTestDelegate alloc] init]; |
| 139 [session addDelegate:delegate]; |
| 140 // On adding a new delegate, nil ptrs should've been cleared. |
| 141 EXPECT_EQ(1u, session.delegates.size()); |
| 142 EXPECT_TRUE(session.delegates[0]); |
| 143 } |
| 144 |
39 @end | 145 @end |
40 | 146 |
41 TEST(RTCAudioSessionTest, LockForConfiguration) { | 147 namespace webrtc { |
| 148 |
| 149 class AudioSessionTest : public ::testing::Test { |
| 150 protected: |
| 151 void TearDown() { |
| 152 RTCAudioSession *session = [RTCAudioSession sharedInstance]; |
| 153 for (id<RTCAudioSessionDelegate> delegate : session.delegates) { |
| 154 [session removeDelegate:delegate]; |
| 155 } |
| 156 } |
| 157 }; |
| 158 |
| 159 TEST_F(AudioSessionTest, LockForConfiguration) { |
42 RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init]; | 160 RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init]; |
43 [test testLockForConfiguration]; | 161 [test testLockForConfiguration]; |
44 } | 162 } |
| 163 |
| 164 TEST_F(AudioSessionTest, AddAndRemoveDelegates) { |
| 165 RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init]; |
| 166 [test testAddAndRemoveDelegates]; |
| 167 } |
| 168 |
| 169 TEST_F(AudioSessionTest, PushDelegate) { |
| 170 RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init]; |
| 171 [test testPushDelegate]; |
| 172 } |
| 173 |
| 174 TEST_F(AudioSessionTest, ZeroingWeakDelegate) { |
| 175 RTCAudioSessionTest *test = [[RTCAudioSessionTest alloc] init]; |
| 176 [test testZeroingWeakDelegate]; |
| 177 } |
| 178 |
| 179 } // namespace webrtc |
OLD | NEW |