Adding a KVO context to avoid issues with future super/sub-classing.

Bug: webrtc:8342
Change-Id: I457858056ffc7f33bbfb261153301ea2ccd71a51
Reviewed-on: https://webrtc-review.googlesource.com/6440
Commit-Queue: Peter Hanspers <peterhanspers@webrtc.org>
Reviewed-by: Anders Carlsson <andersc@webrtc.org>
Reviewed-by: Daniela Jovanoska Petrenko <denicija@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20389}
This commit is contained in:
Peter Hanspers
2017-10-05 11:39:15 +02:00
committed by Commit Bot
parent bd92d8dd2a
commit 47217364f5
2 changed files with 37 additions and 16 deletions

View File

@ -56,8 +56,13 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
} }
- (instancetype)init { - (instancetype)init {
return [self initWithAudioSession:[AVAudioSession sharedInstance]];
}
/** This initializer provides a way for unit tests to inject a fake/mock audio session. */
- (instancetype)initWithAudioSession:(id)audioSession {
if (self = [super init]) { if (self = [super init]) {
_session = [AVAudioSession sharedInstance]; _session = audioSession;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self [center addObserver:self
@ -91,7 +96,7 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
[_session addObserver:self [_session addObserver:self
forKeyPath:kRTCAudioSessionOutputVolumeSelector forKeyPath:kRTCAudioSessionOutputVolumeSelector
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil]; context:(__bridge void*)RTCAudioSession.class];
RTCLog(@"RTCAudioSession (%p): init.", self); RTCLog(@"RTCAudioSession (%p): init.", self);
} }
@ -100,7 +105,9 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
[_session removeObserver:self forKeyPath:kRTCAudioSessionOutputVolumeSelector context:nil]; [_session removeObserver:self
forKeyPath:kRTCAudioSessionOutputVolumeSelector
context:(__bridge void*)RTCAudioSession.class];
RTCLog(@"RTCAudioSession (%p): dealloc.", self); RTCLog(@"RTCAudioSession (%p): dealloc.", self);
} }
@ -815,10 +822,12 @@ NSString * const kRTCAudioSessionOutputVolumeSelector = @"outputVolume";
ofObject:(id)object ofObject:(id)object
change:(NSDictionary *)change change:(NSDictionary *)change
context:(void *)context { context:(void *)context {
if (object == _session) { if (context == (__bridge void*)RTCAudioSession.class) {
NSNumber *newVolume = change[NSKeyValueChangeNewKey]; if (object == _session) {
RTCLog(@"OutputVolumeDidChange to %f", newVolume.floatValue); NSNumber *newVolume = change[NSKeyValueChangeNewKey];
[self notifyDidChangeOutputVolume:newVolume.floatValue]; RTCLog(@"OutputVolumeDidChange to %f", newVolume.floatValue);
[self notifyDidChangeOutputVolume:newVolume.floatValue];
}
} else { } else {
[super observeValueForKeyPath:keyPath [super observeValueForKeyPath:keyPath
ofObject:object ofObject:object

View File

@ -18,6 +18,22 @@
#import "WebRTC/RTCAudioSession.h" #import "WebRTC/RTCAudioSession.h"
#import "WebRTC/RTCAudioSessionConfiguration.h" #import "WebRTC/RTCAudioSessionConfiguration.h"
@interface RTCAudioSession (UnitTesting)
- (instancetype)initWithAudioSession:(id)audioSession;
@end
@interface MockAVAudioSession : NSObject
@property (nonatomic, readwrite, assign) float outputVolume;
@end
@implementation MockAVAudioSession
@synthesize outputVolume = _outputVolume;
@end
@interface RTCAudioSessionTestDelegate : NSObject <RTCAudioSessionDelegate> @interface RTCAudioSessionTestDelegate : NSObject <RTCAudioSessionDelegate>
@property (nonatomic, readonly) float outputVolume; @property (nonatomic, readonly) float outputVolume;
@ -265,20 +281,16 @@ OCMLocation *OCMMakeLocation(id testCase, const char *fileCString, int line){
} }
- (void)testAudioVolumeDidNotify { - (void)testAudioVolumeDidNotify {
RTCAudioSession *session = [RTCAudioSession sharedInstance]; MockAVAudioSession *mockAVAudioSession = [[MockAVAudioSession alloc] init];
RTCAudioSession *session = [[RTCAudioSession alloc] initWithAudioSession:mockAVAudioSession];
RTCAudioSessionTestDelegate *delegate = RTCAudioSessionTestDelegate *delegate =
[[RTCAudioSessionTestDelegate alloc] init]; [[RTCAudioSessionTestDelegate alloc] init];
[session addDelegate:delegate]; [session addDelegate:delegate];
[session observeValueForKeyPath:@"outputVolume" float expectedVolume = 0.75;
ofObject:[AVAudioSession sharedInstance] mockAVAudioSession.outputVolume = expectedVolume;
change:
@{NSKeyValueChangeNewKey :
@([AVAudioSession sharedInstance].outputVolume) }
context:nil];
EXPECT_NE(delegate.outputVolume, -1); EXPECT_EQ(expectedVolume, delegate.outputVolume);
EXPECT_EQ([AVAudioSession sharedInstance].outputVolume, delegate.outputVolume);
} }
@end @end