diff --git a/src/containers/sound-editor.jsx b/src/containers/sound-editor.jsx index b3edc48fba628e2c40981c5cb8207a1cbb484b69..28fe3efb5087731ce706108ca8737aed3fcc8898 100644 --- a/src/containers/sound-editor.jsx +++ b/src/containers/sound-editor.jsx @@ -190,8 +190,11 @@ SoundEditor.propTypes = { }; const mapStateToProps = (state, {soundIndex}) => { - const sound = state.vm.editingTarget.sprite.sounds[soundIndex]; - const audioBuffer = state.vm.getSoundBuffer(soundIndex); + const sprite = state.vm.editingTarget.sprite; + // Make sure the sound index doesn't go out of range. + const index = soundIndex < sprite.sounds.length ? soundIndex : sprite.sounds.length - 1; + const sound = state.vm.editingTarget.sprite.sounds[index]; + const audioBuffer = state.vm.getSoundBuffer(index); return { soundId: sound.soundId, sampleRate: audioBuffer.sampleRate, diff --git a/test/integration/test.js b/test/integration/test.js index d28814cb4597d23f8317c86d3d0d845d9a5efae9..7fa5cadb6e0161b16545f2c334f34d20a995322e 100644 --- a/test/integration/test.js +++ b/test/integration/test.js @@ -272,4 +272,28 @@ describe('costumes, sounds and variables', () => { const logs = await getLogs(errorWhitelist); await expect(logs).toEqual([]); }); + + // Regression test for gui issue #1320 + test('Switching sprites with different numbers of sounds', async () => { + await loadUri(uri); + await clickXpath('//button[@title="tryit"]'); + + // Add a sound so this sprite has 2 sounds. + await clickText('Sounds'); + await clickText('Add Sound'); + await clickText('A Bass'); // Closes the modal + + // Now add a sprite with only one sound. + await clickText('Add Sprite'); + await clickText('Abby'); // Doing this used to crash the editor. + + await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for error + + // Make sure the 'Oops' screen is not visible + const content = await driver.getPageSource(); + expect(content.indexOf('Oops')).toEqual(-1); + + const logs = await getLogs(errorWhitelist); + await expect(logs).toEqual([]); + }); });