|
| 1 | +describe("AudioSource", () => { |
| 2 | + const testSoundFilePath = path.join(WEBCLIENT_FIXTURES_DIR, "WebAudio", "dumbo.ogg"); |
| 3 | + const instance = new AudioSource(testSoundFilePath); |
| 4 | + it("should be exported into the global environment", () => { |
| 5 | + assertEquals(typeof AudioSource.constructor, "function"); |
| 6 | + }); |
| 7 | + |
| 8 | + const exportedApiSurface = [ |
| 9 | + "getPlaybackRate", |
| 10 | + "setPlaybackRate", |
| 11 | + "fadeIn", |
| 12 | + "fadeOut", |
| 13 | + "pause", |
| 14 | + "resume", |
| 15 | + "getVolume", |
| 16 | + "setVolume", |
| 17 | + "isPlaying", |
| 18 | + "startPlaying", |
| 19 | + "stopPlaying", |
| 20 | + "getFilePath", |
| 21 | + "destroy", |
| 22 | + "getUniqueID", |
| 23 | + ]; |
| 24 | + |
| 25 | + exportedApiSurface.forEach((namedExport) => { |
| 26 | + it("should export function " + namedExport, () => { |
| 27 | + assertEquals(typeof instance[namedExport], "function"); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("setPlaybackRate", () => { |
| 32 | + // Also covers getPlaybackRate, coincidentally |
| 33 | + it("should be able to adjust the playback rate", () => { |
| 34 | + instance.setPlaybackRate(0.15); |
| 35 | + const newPlaybackRate = instance.getPlaybackRate(); |
| 36 | + assertEquals(newPlaybackRate, 0.15); |
| 37 | + }); |
| 38 | + |
| 39 | + const rangeError = new RangeError(AudioSource.ERROR_INVALID_PLAYBACK_RATE); |
| 40 | + it("should throw a RangeError if the playback rate is negative", () => { |
| 41 | + assertThrows(() => instance.setPlaybackRate(-1), rangeError); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should throw a RangeError if the playback rate is zero", () => { |
| 45 | + assertThrows(() => instance.setPlaybackRate(0), rangeError); |
| 46 | + }); |
| 47 | + |
| 48 | + const typeError = new TypeError("Usage: AudioSource.setPlaybackRate(Number newPlaybackRate)"); |
| 49 | + it("should throw a TypeError if the playback rate is not a String", () => { |
| 50 | + assertThrows(() => instance.setPlaybackRate(null), typeError); |
| 51 | + assertThrows(() => instance.setPlaybackRate(undefined), typeError); |
| 52 | + assertThrows(() => instance.setPlaybackRate("Hi"), typeError); |
| 53 | + assertThrows(() => instance.setPlaybackRate([]), typeError); |
| 54 | + assertThrows(() => instance.setPlaybackRate({}), typeError); |
| 55 | + assertThrows(() => instance.setPlaybackRate(() => console.log("test")), typeError); |
| 56 | + assertThrows(() => instance.setPlaybackRate(NaN), typeError); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("getUniqueID", () => { |
| 61 | + it("should return a string identifier", () => { |
| 62 | + // We can't guarantee it will be unique, just that it will be extremely unlikely to not be unique... |
| 63 | + const guid = instance.getUniqueID(); |
| 64 | + assertEquals(typeof guid, "string"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should return the same identifier if called repeatedly", () => { |
| 68 | + const guid1 = instance.getUniqueID(); |
| 69 | + const guid2 = instance.getUniqueID(); |
| 70 | + const guid3 = instance.getUniqueID(); |
| 71 | + |
| 72 | + assertEquals(guid1, guid2); |
| 73 | + assertEquals(guid1, guid3); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("destroy", () => { |
| 78 | + it("should return true when successfully disposing of the audio buffer", () => { |
| 79 | + assertTrue(instance.destroy()); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should return false when the audio buffer has already been disposed of", () => { |
| 83 | + assertFalse(instance.destroy()); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments