mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2025-03-21 04:10:36 -04:00
Add tests for play debounce
This commit is contained in:
parent
e2da92fae7
commit
aa77d8d376
2 changed files with 45 additions and 2 deletions
|
@ -200,6 +200,9 @@ class SoundPlayer extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
play () {
|
play () {
|
||||||
if (this.isStarting) {
|
if (this.isStarting) {
|
||||||
|
this.emit('stop');
|
||||||
|
this.emit('play');
|
||||||
|
this.isPlaying = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ tap.test('SoundPlayer', suite => {
|
||||||
audioContext = null;
|
audioContext = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
suite.plan(4);
|
suite.plan(5);
|
||||||
|
|
||||||
suite.test('play initializes and creates source node', t => {
|
suite.test('play initializes and creates source node', t => {
|
||||||
t.plan(3);
|
t.plan(3);
|
||||||
|
@ -109,6 +109,45 @@ tap.test('SoundPlayer', suite => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
suite.test('play while playing debounces', t => {
|
||||||
|
t.plan(7);
|
||||||
|
const log = [];
|
||||||
|
soundPlayer.connect(audioEngine);
|
||||||
|
soundPlayer.play();
|
||||||
|
t.equal(soundPlayer.isStarting, true, 'player.isStarting');
|
||||||
|
const originalNode = soundPlayer.outputNode;
|
||||||
|
// the second play should still "finish" this play
|
||||||
|
soundPlayer.finished().then(() => log.push('finished first'));
|
||||||
|
soundPlayer.play();
|
||||||
|
soundPlayer.finished().then(() => log.push('finished second'));
|
||||||
|
soundPlayer.play();
|
||||||
|
soundPlayer.finished().then(() => log.push('finished third'));
|
||||||
|
soundPlayer.play();
|
||||||
|
t.equal(originalNode, soundPlayer.outputNode, 'same output node');
|
||||||
|
t.equal(soundPlayer.outputNode.$state, 'PLAYING');
|
||||||
|
return Promise.resolve().then(() => {
|
||||||
|
t.deepEqual(log, ['finished first', 'finished second', 'finished third'], 'finished in order');
|
||||||
|
|
||||||
|
// fast forward to one ms before decay time
|
||||||
|
audioContext.$processTo(audioEngine.DECAY_TIME - 0.001);
|
||||||
|
soundPlayer.play();
|
||||||
|
|
||||||
|
t.equal(originalNode, soundPlayer.outputNode, 'same output node');
|
||||||
|
|
||||||
|
|
||||||
|
// now at DECAY_TIME, we should meet a new player as the old one is taken/stopped
|
||||||
|
audioContext.$processTo(audioEngine.DECAY_TIME);
|
||||||
|
|
||||||
|
t.equal(soundPlayer.isStarting, false, 'player.isStarting now false');
|
||||||
|
|
||||||
|
soundPlayer.play();
|
||||||
|
t.notEqual(originalNode, soundPlayer.outputNode, 'New output node');
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
suite.test('play while playing', t => {
|
suite.test('play while playing', t => {
|
||||||
t.plan(15);
|
t.plan(15);
|
||||||
const log = [];
|
const log = [];
|
||||||
|
@ -117,7 +156,8 @@ tap.test('SoundPlayer', suite => {
|
||||||
soundPlayer.connect(audioEngine);
|
soundPlayer.connect(audioEngine);
|
||||||
const firstPlayNode = soundPlayer.outputNode;
|
const firstPlayNode = soundPlayer.outputNode;
|
||||||
|
|
||||||
audioContext.$processTo(0.005);
|
// go past debounce time and play again
|
||||||
|
audioContext.$processTo(audioEngine.DECAY_TIME);
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|
Loading…
Reference in a new issue