import {TestBed} from '@angular/core/testing'; import {SoundOxService} from './sound-ox.service'; import {PostMessageBridgeFactory} from 'ngx-post-message'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {PreloaderOxService, ResourceOx, ResourceType, ScreenTypeOx, SettingsService} from 'ox-core'; import {ScreenOxService} from './screen-ox.service'; import {of} from 'rxjs'; import {EventEmitter} from '@angular/core'; import {AudiOX} from '../models/audio-ox'; describe('SoundOxService', () => { const fakeData = function () { return { _muted: false, _loop: false, _paused: false, _playing: false, _stopped: false, play: function () { this._paused = false; this._playing = true; }, pause: function () { this._paused = true; this._playing = false; }, stop: function () { this._stopped = true; this._paused = false; this._playing = false; }, once: () => { }, mute: function (b) { this._muted = b; }, loop: function (b) { this._loop = b; }, }; }; const resourceMap = new Map(); const resource = new ResourceOx('testAudio1.mp3', ResourceType.Audio, [], true); resource.loaded = true; resource.data = fakeData(); const resource2 = new ResourceOx('testAudio1.mp3', ResourceType.Audio, [], true); resource2.loaded = true; resource2.data = fakeData(); resourceMap.set('testAudio1.mp3', resource); resourceMap.set('testAudio2.mp3', resource2); const preloaderSpy: PreloaderOxService = jasmine.createSpyObj('PrealoderSpy', ['addResourceToLoad', 'loadAll']); preloaderSpy.resources = resourceMap; preloaderSpy.screenReady = new EventEmitter(); preloaderSpy['loadAll'] = () => of(undefined); let screenOxService: ScreenOxService; let soundService: SoundOxService; beforeEach(() => { TestBed.configureTestingModule( { providers: [ {provide: PreloaderOxService, useValue: preloaderSpy}, SettingsService, ScreenOxService, PostMessageBridgeFactory ] , imports: [HttpClientTestingModule] }); soundService = TestBed.get(SoundOxService); soundService.setMusicOn(true); soundService.setSoundOn(true); screenOxService = TestBed.get(ScreenOxService); screenOxService.parentScreen = document.createElement('element'); screenOxService.showLoading(); preloaderSpy.resources.get('testAudio1.mp3').data = fakeData(); preloaderSpy.resources.get('testAudio2.mp3').data = fakeData(); }); it('should be created', () => { expect(soundService).toBeTruthy(); }); it('play howl works', () => { const audiox = new AudiOX(resourceMap.get('testAudio1.mp3').data, ScreenTypeOx.Game); spyOn(audiox.audio, 'play'); soundService['playHowl'](audiox); expect(audiox.audio.play).toHaveBeenCalledTimes(1); }); // Music test it('play music create a new audiox and play the music if the asking screen is the current screen, and set the music to loop', () => { soundService.playMusic('testAudio1.mp3', ScreenTypeOx.Loading); expect(soundService['musics'].length).toBe(1); const music = soundService['musics'][0]; expect(music.audio['_loop']).toBeTruthy(); isPlaying(music.audio); }); it('play music create a new audiox and add the music but not play it if the asking screen is not current screen', () => { askForNoCurrentScreenMusic(ScreenTypeOx.Game); }); it('if screen changes and it was a music for that screen (and music is on) music starts playing', () => { askForNoCurrentScreenMusic(ScreenTypeOx.Game); screenOxService['showGame'](); expect(soundService['musics'][0].audio['_playing']).toBeTruthy(); }); it('After setting music on checkMusicByScreen is called', () => { spyOn(soundService as any, 'checkMusicByScreen'); askForNoCurrentScreenMusic(ScreenTypeOx.Game); soundService.setMusicOn(true); expect(soundService.musicOn).toBeTruthy(); soundService.setMusicOn(false); expect(soundService['checkMusicByScreen']).toHaveBeenCalledTimes(2); }); it('checkMusicByScreen set musics pause if the music is off or if they are from another than the current screen', () => { soundService.playMusic('testAudio1.mp3', ScreenTypeOx.Game); soundService.playMusic('testAudio2.mp3', ScreenTypeOx.Loading); screenOxService['showGame'](); soundService.setMusicOn(true); const mustBePlaying = soundService['musics'].find(x => x.askedFor === ScreenTypeOx.Loading); const gameScreenMusic = soundService['musics'].find(x => x.askedFor === ScreenTypeOx.Game); isPlaying(gameScreenMusic.audio); expect(mustBePlaying.audio['_playing']).toBeFalsy(); soundService.setMusicOn(false); expect(gameScreenMusic.audio['_playing']).toBeFalsy(); expect(mustBePlaying.audio['_playing']).toBeFalsy(); screenOxService['showInGameMenu'](); soundService.setMusicOn(true); expect(gameScreenMusic.audio['_playing']).toBeFalsy(); expect(mustBePlaying.audio['_playing']).toBeFalsy(); }); it('addLast screen music add the music with the screen, and stop and remove previous musics for that screen', () => { const oldMusic = new AudiOX(resourceMap.get('testAudio1.mp3').data, ScreenTypeOx.Game); const newMusic = new AudiOX(resourceMap.get('testAudio2.mp3').data, ScreenTypeOx.Game); soundService['addAsLastScreenMusic'](oldMusic); soundService['addAsLastScreenMusic'](newMusic); expect(oldMusic.audio['_stopped']).toBeTruthy(); expect(newMusic.audio['_stopped']).toBeFalsy(); expect(soundService['musics'].length).toBe(1); }); it('pause music pauses music for asking screen', () => { soundService.playMusic('testAudio2.mp3', ScreenTypeOx.Loading); const audio = soundService['musics'][0].audio; isPlaying(audio); soundService.pauseMusic(ScreenTypeOx.Loading); expect(audio['_paused']).toBeTruthy(); }); it('playCurrentMusic plays music for ask screen if its the same as current screen', () => { soundService.playMusic('testAudio1.mp3', ScreenTypeOx.Game); soundService.playMusic('testAudio2.mp3', ScreenTypeOx.Loading); const audioGame = soundService['musics'][0].audio; const audioLoading = soundService['musics'][1].audio; isPlaying(audioLoading); soundService.pauseMusic(ScreenTypeOx.Loading); expect(audioLoading['_paused']).toBeTruthy(); soundService.playCurrentMusic(ScreenTypeOx.Game); expect(audioGame['_playing']).toBeFalsy(); }); // Sounds test it('play sound effect must add a sound effect to sounds and play it if its in current screen', () => { soundService.playSoundEffect('testAudio1.mp3', ScreenTypeOx.Loading); expect(soundService['audios'].length).toBe(1); const sound = soundService['audios'][0]; expect(sound.audio['_loop']).toBeFalsy(); isPlaying(sound.audio); }); it('play sound effect must add a sound effect to sounds and play it if its in current screen' + 'must play no matter if sound is on/off if playAnyway flag is true', () => { soundService.setSoundOn(false); soundService.playSoundEffect('testAudio1.mp3', ScreenTypeOx.Loading, false, true); expect(soundService['audios'].length).toBe(1); const sound = soundService['audios'][0]; expect(sound.audio['_loop']).toBeFalsy(); isPlaying(sound.audio); }); it('play sound effect must add one more sound even if is the same if duplicateIfIsTheSame', () => { soundService.playSoundEffect('testAudio1.mp3', ScreenTypeOx.Loading, true); soundService.playSoundEffect('testAudio1.mp3', ScreenTypeOx.Loading, true); expect(soundService['audios'].length).toBe(2); const sound = soundService['audios'][0]; const sound2 = soundService['audios'][1]; isPlaying(sound.audio); isPlaying(sound2.audio); }); it('play sound effect play the audio in mute in sound is off, and unmute the audio if the sound is setted on again', () => { soundService.setSoundOn(false); soundService.playSoundEffect('testAudio1.mp3', ScreenTypeOx.Loading); expect(soundService['audios'].length).toBe(1); const sound = soundService['audios'][0]; expect(sound.audio['_muted']).toBeTruthy(); expect(sound.audio['_playing']).toBeTruthy(); soundService.setSoundOn(true); isPlaying(sound.audio); }); it('play sound effect play the audio in mute in sound is from a diffent screen than current' + ', and unmute the audio if the sound screen is setted', () => { soundService.playSoundEffect('testAudio1.mp3', ScreenTypeOx.Game); expect(soundService['audios'].length).toBe(1); const sound = soundService['audios'][0]; expect(sound.audio['_muted']).toBeTruthy(); expect(sound.audio['_playing']).toBeTruthy(); screenOxService['showGame'](); isPlaying(sound.audio); }); function isPlaying(audio) { expect(audio['_playing']).toBe(true); expect(audio['_muted']).toBe(false); expect(audio['_stopped']).toBe(false); expect(audio['_paused']).toBe(false); } function isMuted(audio) { expect(audio['_playing']).toBe(true); expect(audio['_muted']).toBe(true); expect(audio['_stopped']).toBe(false); expect(audio['_paused']).toBe(false); } function askForNoCurrentScreenMusic(wrongScreen) { soundService.playMusic('testAudio1.mp3', wrongScreen); expect(soundService['musics'].length).toBe(1); const music = soundService['musics'][0]; expect(music.audio['_playing']).toBeFalsy(); } });