Skip to content

Commit 0e842d7

Browse files
Added tests for exception cases of start method.
1 parent 5858a64 commit 0e842d7

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/KristofferStrube.Blazor.WebAudio/AudioNodes/AudioBufferSourceNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ public async Task SetLoopEndAsync(double value)
188188
/// </summary>
189189
/// <remarks>
190190
/// It throws an <see cref="InvalidStateErrorException"/> if it has already been started.<br />
191-
/// It throws an <see cref="RangeErrorException"/> if <paramref name="offset"/> is negative.<br />
192-
/// It throws an <see cref="RangeErrorException"/> if <paramref name="duration"/> is negative.
191+
/// It throws a <see cref="RangeErrorException"/> if <paramref name="offset"/> is negative.<br />
192+
/// It throws a <see cref="RangeErrorException"/> if <paramref name="duration"/> is negative.
193193
/// </remarks>
194194
/// <param name="when">The when parameter describes at what time (in seconds) the sound should start playing. It is in the same time coordinate system as the AudioContext's currentTime attribute.</param>
195195
/// <param name="offset">The offset parameter supplies a playhead position where playback will begin. If <c>0</c> is passed in for this value, then playback will start from the beginning of the buffer.</param>

tests/IntegrationTests/AudioNodeTests/AudioBufferSourceNodeTest.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FluentAssertions;
22
using KristofferStrube.Blazor.DOM;
3+
using KristofferStrube.Blazor.WebIDL.Exceptions;
34

45
namespace IntegrationTests.AudioNodeTests;
56

@@ -341,4 +342,74 @@ public async Task StartAsync_WithOffsetParameter_StartsOffsetInBuffer(double off
341342
_ = eventListenerTriggered.Should().BeTrue();
342343
await node.RemoveOnEndedEventListenerAsync(onEndedListener);
343344
}
345+
346+
[Test]
347+
public async Task StartAsync_ThrowsInvalidStateErrorException_WhenItHasAlreadyBeenStarted()
348+
{
349+
// Arrange
350+
await using AudioContext context = await AudioContext.CreateAsync(JSRuntime);
351+
352+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions()
353+
{
354+
SampleRate = 8000,
355+
Length = 1
356+
});
357+
await using AudioBufferSourceNode node = await AudioBufferSourceNode.CreateAsync(JSRuntime, context, new()
358+
{
359+
Buffer = buffer
360+
});
361+
await node.StartAsync();
362+
363+
// Act
364+
Func<Task> action = async () => await node.StartAsync();
365+
366+
// Assert
367+
_ = await action.Should().ThrowAsync<InvalidStateErrorException>();
368+
}
369+
370+
[Test]
371+
public async Task StartAsync_WithNegativeOffset_ThrowsRangeErrorException()
372+
{
373+
// Arrange
374+
await using AudioContext context = await AudioContext.CreateAsync(JSRuntime);
375+
376+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions()
377+
{
378+
SampleRate = 8000,
379+
Length = 1
380+
});
381+
await using AudioBufferSourceNode node = await AudioBufferSourceNode.CreateAsync(JSRuntime, context, new()
382+
{
383+
Buffer = buffer
384+
});
385+
386+
// Act
387+
Func<Task> action = async () => await node.StartAsync(when: 0, offset: -1);
388+
389+
// Assert
390+
_ = await action.Should().ThrowAsync<RangeErrorException>();
391+
}
392+
393+
[Test]
394+
public async Task StartAsync_WithNegativeDuration_ThrowsRangeErrorException()
395+
{
396+
// Arrange
397+
await using AudioContext context = await AudioContext.CreateAsync(JSRuntime);
398+
399+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions()
400+
{
401+
SampleRate = 8000,
402+
Length = 1
403+
});
404+
await using AudioBufferSourceNode node = await AudioBufferSourceNode.CreateAsync(JSRuntime, context, new()
405+
{
406+
Buffer = buffer
407+
});
408+
409+
// Act
410+
Func<Task> action = async () => await node.StartAsync(when: 0, offset: 0, duration: -1);
411+
412+
// Assert
413+
_ = await action.Should().ThrowAsync<RangeErrorException>();
414+
}
344415
}

0 commit comments

Comments
 (0)