Skip to content

Instantly share code, notes, and snippets.

@Nek

Nek/playerSpec.js

Created Jan 31, 2012
Embed
What would you like to do?
PlayerSpec
describe( "Player library", function () {
describe("Player", function() {
it("exists in a library namespace", function() {
expect(mediaITPro.Player).toBeDefined();
});
it("has an attachPlayer method", function() {
expect(mediaITPro.Player.attachPlayer).toBeDefined();
});
});
describe("Mixer", function() {
it("exists in a library namespace", function() {
expect(mediaITPro.Mixer).toBeDefined();
});
it("can be instantiated", function() {
var mixer = new mediaITPro.Mixer();
expect(mixer).not.toBeNull();
});
describe("add sound to mixer", function() {
var mixer;
beforeEach(function () {
mixer = new mediaITPro.Mixer();
});
it("throws errors for wrong url", function() {
var testNoPath = function () {
mixer.addSound();
}
expect(testNoPath).toThrow(new Error("You shall provide a URL to audio file."));
});
it("returns accending integer sound id", function() {
var id = mixer.addSound("audio1.ogg");
expect(id).toEqual(0);
var id = mixer.addSound("audio2.ogg");
expect(id).toEqual(1);
});
it("creates sound model and stores it in sounds array", function() {
mixer.addSound("audio1.ogg");
expect(mixer.sounds.length).toEqual(1);
expect(mixer.sounds[0].url).toEqual("audio1.ogg");
expect(mixer.sounds[0].id).toEqual(0);
});
it("sets default sound gain to 1", function() {
mixer.addSound("audio1.ogg");
expect(mixer.sounds[0].gain).toEqual(1);
});
it("creates audio context", function() {
mixer.addSound("audio1.ogg");
expect(mixer.context.constructor).toEqual(webkitAudioContext);
});
it("fires added callback", function() {
sinon.spy(mixer, "added");
mixer.addSound("audio1.ogg");
expect(mixer.added).toHaveBeenCalledWith(0, "audio1.ogg");
});
it("fires progress callback", function() {
sinon.spy(mixer, "progress");
runs(function() {
mixer.addSound("audio1.ogg");
});
waits(500);
runs(function() {
expect(mixer.progress).toHaveBeenCalledWith(0);
});
});
it("fires loaded callback", function() {
var isLoaded = false;
runs(function() {
mixer.loaded = function() {
isLoaded = true;
}
sinon.spy(mixer, "loaded");
mixer.addSound("audio1.ogg");
});
waitsFor(function() {
return isLoaded;
}, "Audio file load never completed.", 10000);
runs(function() {
expect(mixer.loaded).toHaveBeenCalledWith(0);
expect(mixer.loaded).toHaveBeenCalledOnce();
});
});
it("decodes loaded audio into sound model buffer property of AudioBuffer type", function() {
var isLoaded = false;
runs(function() {
mixer.loaded = function() {
isLoaded = true;
}
mixer.addSound("audio1.ogg");
});
waitsFor(function() {
return isLoaded;
}, "Audio file load never completed.", 10000);
runs(function() {
expect(mixer.sounds[0].buffer).toBeDefined();
expect(mixer.sounds[0].buffer.constructor).toEqual(new webkitAudioContext().createBuffer(2, 512, 44100).constructor);
});
});
});
describe("set sound gain", function() {
var mixer;
beforeEach(function () {
mixer = new mediaITPro.Mixer();
});
it("throws error for undefined id", function() {
var testNoId = function () {
mixer.setSoundGain();
}
expect(testNoId).toThrow(new Error("You shall provide a sound id."));
})
it("throws error for id less than 0", function() {
var testWrongId = function () {
mixer.setSoundGain(-1);
}
expect(testWrongId).toThrow(new Error("You shall provide a correct sound id."));
});
it("throws error for not existing id", function() {
var testWrongId = function () {
mixer.setSoundGain(0);
}
expect(testWrongId).toThrow(new Error("You shall provide an existing sound id."));
});
it("throws error for undefined gain", function(){
var testNoGain = function () {
var id = mixer.addSound("audio1.ogg");
mixer.setSoundGain(id);
}
expect(testNoGain).toThrow(new Error("You shall provide a sound gain value."));
});
});
});
});
@Nek

This comment has been minimized.

Copy link
Owner Author

@Nek Nek commented Jan 31, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.