Skip to content

Commit

Permalink
Backwards compatibility and enabling unit tests for AudioRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann committed Aug 18, 2016
1 parent 916a072 commit 5b3a003
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
18 changes: 13 additions & 5 deletions app/script/audio/AudioRepository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,24 @@ class z.audio.AudioRepository
@return [Promise] Resolves with the HTMLAudioElement
###
_play: (audio_id, audio_element, play_in_loop = false) ->
return new Promise (resolve, reject) ->
if not audio_id or not audio_element
return Promise.reject new z.audio.AudioError 'Audio not found', z.audio.AudioError::TYPE.NOT_FOUND

return new Promise (resolve, reject) =>
if audio_element.paused
audio_element.loop = play_in_loop
audio_element.currentTime = 0 if audio_element.currentTime isnt 0
audio_element.play()
.then =>
play_promise = audio_element.play()

_play_success = =>
@currently_looping[audio_id] = audio_id if play_in_loop
resolve audio_element
.catch (error) ->
reject new z.audio.AudioError error.message, z.audio.AudioError::TYPE.FAILED_TO_PLAY

if play_promise
play_promise.then(_play_success).catch (error) ->
reject new z.audio.AudioError error.message, z.audio.AudioError::TYPE.FAILED_TO_PLAY
else
_play_success()
else
reject new z.audio.AudioError 'Sound is already playing', z.audio.AudioError::TYPE.ALREADY_PLAYING

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"grunt-todo": "0.5.0",
"jasmine-ajax": "3.2.0",
"jasmine-core": "2.4.1",
"karma": "1.2.0",
"karma": "1.1.2",
"karma-chrome-launcher": "1.0.1",
"karma-coverage": "1.1.1",
"karma-jasmine": "1.0.2",
Expand Down
22 changes: 19 additions & 3 deletions test/unit_tests/audio/AudioRepositorySpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ describe 'z.audio.AudioRepository', ->
expect(error.type).toBe z.audio.AudioError::TYPE.NOT_FOUND
done()

xdescribe '_play', ->
describe '_play', ->
beforeEach ->
audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL] = new Audio "/audio/#{z.audio.AudioType.OUTGOING_CALL}.mp3"

afterEach ->
audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL].stop()
audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL].pause()

it 'plays an available sound', (done) ->
audio_repository._play z.audio.AudioType.OUTGOING_CALL, audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL], false
Expand All @@ -87,10 +87,26 @@ describe 'z.audio.AudioRepository', ->
it 'does not play a sound twice concurrently', (done) ->
audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL].play()
.then ->
audio_repository._play z.audio.AudioType.OUTGOING_CALL
audio_repository._play z.audio.AudioType.OUTGOING_CALL, audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL]
.then done.fail
.catch (error) ->
expect(error).toEqual jasmine.any z.audio.AudioError
expect(error.type).toBe z.audio.AudioError::TYPE.ALREADY_PLAYING
done()
.catch done.fail

it 'handles a missing audio id sound', (done) ->
audio_repository._play undefined, audio_repository.audio_elements[z.audio.AudioType.OUTGOING_CALL]
.catch (error) ->
expect(error).toEqual jasmine.any z.audio.AudioError
expect(error.type).toBe z.audio.AudioError::TYPE.NOT_FOUND
done()
.catch done.fail

it 'handles a missing audio element', (done) ->
audio_repository._play z.audio.AudioType.OUTGOING_CALL, undefined
.catch (error) ->
expect(error).toEqual jasmine.any z.audio.AudioError
expect(error.type).toBe z.audio.AudioError::TYPE.NOT_FOUND
done()
.catch done.fail

0 comments on commit 5b3a003

Please sign in to comment.