Skip to content

Commit a704e46

Browse files
committed
Tests: Add more tests for the AudioSource primitive
1 parent 7735ec4 commit a704e46

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
});

Tests/Builtins/AudioSource.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

Tests/run-renderer-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const testSuites = {
33
Builtins: [
44
"Builtins/Assertions.js",
55
"Builtins/LocalCacheTests.js",
6-
"Builtins/AudioSource.js",
76
"Builtins/AudioTrack.js",
87
"Builtins/Decoder.js",
98
"Builtins/UniqueID.js",
@@ -15,6 +14,7 @@ const testSuites = {
1514
"API/C_Settings/validateUserSettings.js",
1615
],
1716
C_WebAudio: [
17+
"API/C_WebAudio/AudioSource.js",
1818
"API/C_WebAudio/BuiltinAudioDecoder.js",
1919
"API/C_WebAudio/createTrack.js",
2020
"API/C_WebAudio/getTrackInfo.js",

0 commit comments

Comments
 (0)