Skip to content

Commit afbd6a4

Browse files
Added test for getting buffer from AudioBufferSourceNode and fixed bug with nullability of said method.
1 parent 53f4109 commit afbd6a4

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ protected AudioBufferSourceNode(IJSRuntime jSRuntime, IJSObjectReference jSRefer
5252
public async Task<AudioBuffer?> GetBufferAsync()
5353
{
5454
IJSObjectReference helper = await webAudioHelperTask.Value;
55-
IJSObjectReference? jSInstance = await helper.InvokeAsync<IJSObjectReference?>("getAttribute", JSReference, "buffer");
55+
await using ValueReference bufferAttribute = new(JSRuntime, JSReference, "buffer");
56+
57+
if (await bufferAttribute.GetTypeNameAsync() is "null")
58+
{
59+
return null;
60+
}
61+
62+
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("getAttribute", JSReference, "buffer");
5663
return jSInstance is null ? null : await AudioBuffer.CreateAsync(JSRuntime, jSInstance, new() { DisposesJSReference = true });
5764
}
5865

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
1-
namespace IntegrationTests.AudioNodeTests;
1+
using FluentAssertions;
2+
3+
namespace IntegrationTests.AudioNodeTests;
24

35
public class AudioBufferSourceNodeTest : AudioNodeTest<AudioBufferSourceNode>
46
{
57
public override async Task<AudioBufferSourceNode> GetDefaultInstanceAsync()
68
{
79
return await AudioBufferSourceNode.CreateAsync(JSRuntime, await GetAudioContextAsync());
810
}
11+
12+
[Test]
13+
public async Task GetBufferAsync_ShouldReturnNull_WhenItHasNoBuffer()
14+
{
15+
// Arrange
16+
await using AudioContext context = await AudioContext.CreateAsync(JSRuntime);
17+
18+
await using AudioBufferSourceNode node = await AudioBufferSourceNode.CreateAsync(JSRuntime, context);
19+
20+
// Act
21+
AudioBuffer? buffer = await node.GetBufferAsync();
22+
23+
// Assert
24+
_ = buffer.Should().BeNull();
25+
}
26+
27+
[Test]
28+
public async Task GetBufferAsync_ShouldReturnBuffer_WhenItHasBuffer()
29+
{
30+
// Arrange
31+
await using AudioContext context = await AudioContext.CreateAsync(JSRuntime);
32+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions() { Length = 1, SampleRate = 8000 });
33+
34+
await using AudioBufferSourceNode node = await AudioBufferSourceNode.CreateAsync(JSRuntime, context, new()
35+
{
36+
Buffer = buffer
37+
});
38+
39+
// Act
40+
AudioBuffer? readBuffer = await node.GetBufferAsync();
41+
42+
// Assert
43+
_ = readBuffer.Should().NotBeNull();
44+
}
945
}

0 commit comments

Comments
 (0)