diff options
author | Joel Fischer <joeljfischer@gmail.com> | 2019-05-30 13:58:31 -0400 |
---|---|---|
committer | Joel Fischer <joeljfischer@gmail.com> | 2019-05-30 13:58:31 -0400 |
commit | 09dc70795aede5ab840b27735beb9f2e346a58ae (patch) | |
tree | ad543664ae8a687ec72a6bd628b53867613a8dce | |
parent | 0bc09b69b1e6ff98aed152b460f83e5a54c3ad15 (diff) | |
download | sdl_ios-09dc70795aede5ab840b27735beb9f2e346a58ae.tar.gz |
AudioStreamManager tests
4 files changed, 108 insertions, 37 deletions
diff --git a/SmartDeviceLink/SDLAudioStreamManager.m b/SmartDeviceLink/SDLAudioStreamManager.m index 1ea9ac3ee..ff16f2b16 100755 --- a/SmartDeviceLink/SDLAudioStreamManager.m +++ b/SmartDeviceLink/SDLAudioStreamManager.m @@ -46,6 +46,13 @@ NSString *const SDLErrorDomainAudioStreamManager = @"com.sdl.extension.pcmAudioS return self; } +- (void)stop { + dispatch_async(_audioQueue, ^{ + self.shouldPlayWhenReady = NO; + [self.mutableQueue removeAllObjects]; + }); +} + #pragma mark - Getters - (NSArray<SDLFile *> *)queue { @@ -140,37 +147,30 @@ NSString *const SDLErrorDomainAudioStreamManager = @"com.sdl.extension.pcmAudioS float audioLengthSecs = (float)audioData.length / (float)32000.0; __weak typeof(self) weakself = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(audioLengthSecs * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - weakself.playing = NO; + __strong typeof(weakself) strongSelf = weakself; + + strongSelf.playing = NO; NSError *error = nil; - if (weakself.delegate != nil) { + if (strongSelf.delegate != nil) { if (file.inputFileURL != nil) { - [weakself.delegate audioStreamManager:weakself fileDidFinishPlaying:file.inputFileURL successfully:success]; - } else if ([weakself.delegate respondsToSelector:@selector(audioStreamManager:dataBufferDidFinishPlayingSuccessfully:)]) { - [weakself.delegate audioStreamManager:weakself dataBufferDidFinishPlayingSuccessfully:success]; + [strongSelf.delegate audioStreamManager:strongSelf fileDidFinishPlaying:file.inputFileURL successfully:success]; + } else if ([strongSelf.delegate respondsToSelector:@selector(audioStreamManager:dataBufferDidFinishPlayingSuccessfully:)]) { + [strongSelf.delegate audioStreamManager:strongSelf dataBufferDidFinishPlayingSuccessfully:success]; } } SDLLogD(@"Ending Audio file: %@", file); [[NSFileManager defaultManager] removeItemAtURL:file.outputFileURL error:&error]; - if (weakself.delegate != nil && error != nil) { + if (strongSelf.delegate != nil && error != nil) { if (file.inputFileURL != nil) { - [weakself.delegate audioStreamManager:weakself errorDidOccurForFile:file.inputFileURL error:error]; - } else if ([weakself.delegate respondsToSelector:@selector(audioStreamManager:errorDidOccurForDataBuffer:)]) { - [weakself.delegate audioStreamManager:weakself errorDidOccurForDataBuffer:error]; + [strongSelf.delegate audioStreamManager:strongSelf errorDidOccurForFile:file.inputFileURL error:error]; + } else if ([strongSelf.delegate respondsToSelector:@selector(audioStreamManager:errorDidOccurForDataBuffer:)]) { + [strongSelf.delegate audioStreamManager:strongSelf errorDidOccurForDataBuffer:error]; } } }); } -#pragma mark - Stopping Playback - -- (void)stop { - dispatch_async(_audioQueue, ^{ - self.shouldPlayWhenReady = NO; - [self.mutableQueue removeAllObjects]; - }); -} - @end NS_ASSUME_NONNULL_END diff --git a/SmartDeviceLinkTests/DevAPISpecs/SDLAudioStreamManagerSpec.m b/SmartDeviceLinkTests/DevAPISpecs/SDLAudioStreamManagerSpec.m index a087ecb5a..320282465 100644 --- a/SmartDeviceLinkTests/DevAPISpecs/SDLAudioStreamManagerSpec.m +++ b/SmartDeviceLinkTests/DevAPISpecs/SDLAudioStreamManagerSpec.m @@ -10,6 +10,7 @@ describe(@"the audio stream manager", ^{ __block SDLAudioStreamManager *testManager = nil; __block SDLStreamingAudioManagerMock *mockAudioManager = nil; __block NSURL *testAudioFileURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"testAudio" withExtension:@"mp3"]; + __block NSData *testAudioFileData = [NSData dataWithContentsOfURL:testAudioFileURL options:0 error:nil]; beforeEach(^{ mockAudioManager = [[SDLStreamingAudioManagerMock alloc] init]; @@ -27,30 +28,91 @@ describe(@"the audio stream manager", ^{ }); describe(@"when audio streaming is not connected", ^{ + context(@"with a file URL", ^{ + beforeEach(^{ + mockAudioManager.audioConnected = NO; + [testManager pushWithFileURL:testAudioFileURL]; + + [NSThread sleepForTimeInterval:0.5]; + }); + + describe(@"after attempting to play the file", ^{ + beforeEach(^{ + [mockAudioManager clearData]; + [testManager playNextWhenReady]; + }); + + it(@"should fail to send data", ^{ + expect(mockAudioManager.dataSinceClear.length).to(equal(0)); + expect(mockAudioManager.error.code).toEventually(equal(SDLAudioStreamManagerErrorNotConnected)); + }); + }); + }); + + context(@"with a data buffer", ^{ + beforeEach(^{ + mockAudioManager.audioConnected = NO; + [testManager pushWithData:testAudioFileData]; + + [NSThread sleepForTimeInterval:0.5]; + }); + + describe(@"after attempting to play the file", ^{ + beforeEach(^{ + [mockAudioManager clearData]; + [testManager playNextWhenReady]; + }); + + it(@"should fail to send data", ^{ + expect(mockAudioManager.dataSinceClear.length).to(equal(0)); + expect(mockAudioManager.error.code).toEventually(equal(SDLAudioStreamManagerErrorNotConnected)); + }); + }); + }); + }); + + describe(@"after adding an audio file to the queue", ^{ beforeEach(^{ - mockAudioManager.audioConnected = NO; + mockAudioManager.audioConnected = YES; [testManager pushWithFileURL:testAudioFileURL]; [NSThread sleepForTimeInterval:0.5]; }); + it(@"should have a file in the queue", ^{ + expect(testManager.queue).toNot(beEmpty()); + }); + describe(@"after attempting to play the file", ^{ beforeEach(^{ [mockAudioManager clearData]; [testManager playNextWhenReady]; }); - it(@"should fail to send data", ^{ - expect(mockAudioManager.dataSinceClear.length).to(equal(0)); - expect(mockAudioManager.fileError.code).to(equal(SDLAudioStreamManagerErrorNotConnected)); + fit(@"should be sending data", ^{ + expect(testManager.isPlaying).toEventually(beTrue()); + expect(mockAudioManager.dataSinceClear.length).toEventually(equal(34380)); + + // Fails when it shouldn't, `weakself` goes to nil in `sdl_playNextWhenReady` + expect(mockAudioManager.finishedPlaying).toEventually(beTrue()); + }); + }); + + describe(@"after stopping the manager", ^{ + beforeEach(^{ + [testManager stop]; + }); + + it(@"should have an empty queue", ^{ + expect(testManager.queue).toEventually(beEmpty()); }); }); }); - describe(@"after adding an audio file to the queue", ^{ + describe(@"after adding an audio buffer to the queue", ^{ beforeEach(^{ mockAudioManager.audioConnected = YES; - [testManager pushWithFileURL:testAudioFileURL]; + [testManager pushWithData:testAudioFileData]; [NSThread sleepForTimeInterval:0.5]; }); @@ -59,7 +121,7 @@ describe(@"the audio stream manager", ^{ expect(testManager.queue).toNot(beEmpty()); }); - describe(@"after attempting to play the file", ^{ + describe(@"after attempting to play the audio buffer", ^{ beforeEach(^{ [mockAudioManager clearData]; [testManager playNextWhenReady]; @@ -67,10 +129,10 @@ describe(@"the audio stream manager", ^{ it(@"should be sending data", ^{ expect(testManager.isPlaying).toEventually(beTrue()); - expect(mockAudioManager.dataSinceClear.length).toEventually(equal(34380)); + expect(mockAudioManager.dataSinceClear.length).toEventually(equal(14838)); // Fails when it shouldn't, `weakself` goes to nil in `sdl_playNextWhenReady` -// expect(mockAudioManager.fileFinishedPlaying).toEventually(beTrue()); + expect(mockAudioManager.finishedPlaying).toEventually(beTrue()); }); }); @@ -80,7 +142,7 @@ describe(@"the audio stream manager", ^{ }); it(@"should have an empty queue", ^{ - expect(testManager.queue).to(beEmpty()); + expect(testManager.queue).toEventually(beEmpty()); }); }); }); diff --git a/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.h b/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.h index 2717798a5..0b92d26d5 100644 --- a/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.h +++ b/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.h @@ -21,13 +21,14 @@ #pragma mark SDLStreamingAudioManagerType @property (assign, nonatomic, readonly, getter=isAudioConnected) BOOL audioConnected; - (BOOL)sendAudioData:(NSData *)audioData; - - (void)setAudioConnected:(BOOL)audioConnected; #pragma mark SDLAudioStreamManagerDelegate - (void)audioStreamManager:(SDLAudioStreamManager *)audioManager fileDidFinishPlaying:(SDLAudioFile *)file successfully:(BOOL)successfully; +- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager dataBufferDidFinishPlayingSuccessfully:(BOOL)successfully; - (void)audioStreamManager:(SDLAudioStreamManager *)audioManager errorDidOccurForFile:(SDLAudioFile *)file error:(NSError *)error; -@property (assign, nonatomic, readonly) BOOL fileFinishedPlaying; -@property (strong, nonatomic, readonly) NSError *fileError; +- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager errorDidOccurForDataBuffer:(NSError *)error; +@property (assign, nonatomic, readonly) BOOL finishedPlaying; +@property (strong, nonatomic, readonly) NSError *error; @end diff --git a/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.m b/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.m index 21d1d8732..6d03a7264 100644 --- a/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.m +++ b/SmartDeviceLinkTests/SDLStreamingAudioManagerMock.m @@ -14,8 +14,8 @@ @property (strong, nonatomic) NSMutableData *mutableDataSinceClear; -@property (assign, nonatomic, readwrite) BOOL fileFinishedPlaying; -@property (strong, nonatomic, readwrite) NSError *fileError; +@property (assign, nonatomic, readwrite) BOOL finishedPlaying; +@property (strong, nonatomic, readwrite) NSError *error; @end @@ -35,8 +35,8 @@ _lastSentData = nil; _mutableDataSinceClear = nil; - _fileFinishedPlaying = NO; - _fileError = nil; + _finishedPlaying = NO; + _error = nil; } #pragma mark SDLStreamingAudioManagerType @@ -62,11 +62,19 @@ #pragma mark SDLAudioStreamManagerDelegate - (void)audioStreamManager:(SDLAudioStreamManager *)audioManager fileDidFinishPlaying:(SDLAudioFile *)file successfully:(BOOL)successfully { - _fileFinishedPlaying = successfully; + _finishedPlaying = successfully; +} + +- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager dataBufferDidFinishPlayingSuccessfully:(BOOL)successfully { + _finishedPlaying = successfully; } - (void)audioStreamManager:(SDLAudioStreamManager *)audioManager errorDidOccurForFile:(SDLAudioFile *)file error:(NSError *)error { - _fileError = error; + _error = error; +} + +- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager errorDidOccurForDataBuffer:(NSError *)error { + _error = error; } @end |