Skip to content

Commit 4ed6109

Browse files
Restructure tests to be much simpler.
1 parent 4f32ee0 commit 4ed6109

15 files changed

+92
-147
lines changed
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11

22
namespace BlazorServer;
33

4-
public class EvaluationContext : IEvaluationContext<EvaluationContext>
4+
public class EvaluationContext
55
{
6-
public object? Result { get; set; }
7-
8-
public Exception? Exception { get; set; }
9-
10-
public Func<Task<object?>>? AfterRenderAsync { get; set; }
11-
12-
public static EvaluationContext Create(IServiceProvider provider)
13-
{
14-
return new EvaluationContext();
15-
}
166
}

tests/BlazorServer/IEvaluationContext.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/BlazorServer/Pages/Index.razor

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,9 @@
1717
[Inject]
1818
public required EvaluationContext EvaluationContext { get; set; }
1919

20-
protected override async Task OnAfterRenderAsync(bool firstRender)
20+
protected override void OnAfterRender(bool firstRender)
2121
{
2222
if (!firstRender) return;
23-
try
24-
{
25-
if (EvaluationContext.AfterRenderAsync is not null)
26-
{
27-
EvaluationContext.Result = await EvaluationContext.AfterRenderAsync.Invoke();
28-
}
29-
}
30-
catch (Exception e)
31-
{
32-
EvaluationContext.Exception = e;
33-
}
3423
result = "done";
3524
StateHasChanged();
3625
}
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
using Microsoft.JSInterop;
1+
using FluentAssertions;
2+
using KristofferStrube.Blazor.WebIDL;
3+
using Microsoft.JSInterop;
24

35
namespace IntegrationTests.AudioNodeTests;
46

57
public class AnalyserNodeTest : AudioNodeWithAudioNodeOptions<AnalyserNode, AnalyserOptions>
68
{
79
public override Task<AnalyserNode> CreateAsync(IJSRuntime jSRuntime, AudioContext context, AnalyserOptions? options)
810
=> AnalyserNode.CreateAsync(jSRuntime, context, options);
11+
12+
[Test]
13+
public async Task GetFloatFrequencyDataAsync_ShouldPopulateBuffer()
14+
{
15+
// Arrange
16+
await using AudioContext context = await GetAudioContextAsync();
17+
18+
await using OscillatorNode oscillator = await OscillatorNode.CreateAsync(EvaluationContext.JSRuntime, context);
19+
await using AnalyserNode node = await AnalyserNode.CreateAsync(EvaluationContext.JSRuntime, context);
20+
await oscillator.ConnectAsync(node);
21+
await oscillator.StartAsync();
22+
23+
int bufferLength = (int)await node.GetFrequencyBinCountAsync();
24+
await using Float32Array array = await Float32Array.CreateAsync(EvaluationContext.JSRuntime, bufferLength);
25+
26+
// Act
27+
await node.GetFloatFrequencyDataAsync(array);
28+
29+
// Assert
30+
float lastElement = await array.AtAsync(bufferLength - 1);
31+
_ = lastElement.Should().NotBe(0);
32+
}
933
}

tests/IntegrationTests/AudioNodeTests/AudioBufferSourceNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class AudioBufferSourceNodeTest : AudioNodeTest<AudioBufferSourceNode>
44
{
55
public override async Task<AudioBufferSourceNode> GetDefaultInstanceAsync()
66
{
7-
return await AudioBufferSourceNode.CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext());
7+
return await AudioBufferSourceNode.CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync());
88
}
99
}

tests/IntegrationTests/AudioNodeTests/AudioDestinationNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class AudioDestinationNodeTest : AudioNodeTest<AudioDestinationNode>
44
{
55
public override async Task<AudioDestinationNode> GetDefaultInstanceAsync()
66
{
7-
AudioContext context = await EvaluationContext.GetAudioContext();
7+
AudioContext context = await GetAudioContextAsync();
88
return await context.GetDestinationAsync();
99
}
1010
}

tests/IntegrationTests/AudioNodeTests/AudioNodeTest.cs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using FluentAssertions;
2-
using IntegrationTests.Infrastructure;
2+
using KristofferStrube.Blazor.WebIDL.Exceptions;
33

44
namespace IntegrationTests.AudioNodeTests;
55

6-
public abstract class AudioNodeTest<TAudioNode> : AudioContextBlazorTest where TAudioNode : AudioNode
6+
public abstract class AudioNodeTest<TAudioNode> : BlazorTest where TAudioNode : AudioNode
77
{
88
public abstract Task<TAudioNode> GetDefaultInstanceAsync();
99

@@ -14,18 +14,11 @@ public abstract class AudioNodeTest<TAudioNode> : AudioContextBlazorTest where T
1414
[Test]
1515
public async Task CreateAsync_WithNoOptions_Succeeds()
1616
{
17-
// Arrange
18-
AfterRenderAsync = async () =>
19-
{
20-
return await GetDefaultInstanceAsync();
21-
};
22-
2317
// Act
24-
await OnAfterRerenderAsync();
18+
await using TAudioNode node = await GetDefaultInstanceAsync();
2519

2620
// Assert
27-
_ = EvaluationContext.Exception.Should().BeNull();
28-
_ = EvaluationContext.Result.Should().BeOfType<TAudioNode>();
21+
_ = node.Should().BeOfType<TAudioNode>();
2922
}
3023

3124
[TestCase(ChannelCountMode.Max)]
@@ -34,29 +27,23 @@ public async Task CreateAsync_WithNoOptions_Succeeds()
3427
[Test]
3528
public async Task SettingChannelCountMode_SetsChannelCountMode_ExceptForUnsupportedValues(ChannelCountMode mode)
3629
{
37-
// Arrange
38-
AfterRenderAsync = async () =>
30+
// Act
31+
Func<Task<ChannelCountMode>> action = async () =>
3932
{
4033
await using TAudioNode node = await GetDefaultInstanceAsync();
41-
4234
await node.SetChannelCountModeAsync(mode);
43-
4435
return await node.GetChannelCountModeAsync();
4536
};
4637

47-
// Act
48-
await OnAfterRerenderAsync();
49-
5038
// Assert
5139
if (UnsupportedChannelCountModes.TryGetValue(mode, out Type? exceptionType))
5240
{
53-
_ = EvaluationContext.Result.Should().Be(null);
54-
_ = EvaluationContext.Exception.Should().BeOfType(exceptionType);
41+
_ = (await action.Should().ThrowAsync<WebIDLException>()).And.Should().BeOfType(exceptionType);
5542
}
5643
else
5744
{
58-
_ = EvaluationContext.Exception.Should().BeNull();
59-
_ = EvaluationContext.Result.Should().Be(mode);
45+
ChannelCountMode result = await action();
46+
_ = result.Should().Be(mode);
6047
}
6148
}
6249

@@ -65,29 +52,23 @@ public async Task SettingChannelCountMode_SetsChannelCountMode_ExceptForUnsuppor
6552
[Test]
6653
public async Task SettingChannelInterpretation_SetsInterpretation_ExceptForUnsupportedValues(ChannelInterpretation interpretation)
6754
{
68-
// Arrange
69-
AfterRenderAsync = async () =>
55+
// Act
56+
Func<Task<ChannelInterpretation>> action = async () =>
7057
{
7158
await using TAudioNode node = await GetDefaultInstanceAsync();
72-
7359
await node.SetChannelInterpretationAsync(interpretation);
74-
7560
return await node.GetChannelInterpretationAsync();
7661
};
7762

78-
// Act
79-
await OnAfterRerenderAsync();
80-
8163
// Assert
8264
if (UnsupportedChannelInterpretations.TryGetValue(interpretation, out Type? exceptionType))
8365
{
84-
_ = EvaluationContext.Result.Should().Be(null);
85-
_ = EvaluationContext.Exception.Should().BeOfType(exceptionType);
66+
_ = (await action.Should().ThrowAsync<WebIDLException>()).And.Should().BeOfType(exceptionType);
8667
}
8768
else
8869
{
89-
_ = EvaluationContext.Exception.Should().BeNull();
90-
_ = EvaluationContext.Result.Should().Be(interpretation);
70+
ChannelInterpretation result = await action();
71+
_ = result.Should().Be(interpretation);
9172
}
9273
}
9374

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using FluentAssertions;
2+
using FluentAssertions.Specialized;
3+
using KristofferStrube.Blazor.WebIDL.Exceptions;
24
using Microsoft.JSInterop;
35

46
namespace IntegrationTests.AudioNodeTests;
@@ -9,71 +11,46 @@ namespace IntegrationTests.AudioNodeTests;
911

1012
public override async Task<TAudioNode> GetDefaultInstanceAsync()
1113
{
12-
return await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), null);
14+
return await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), null);
1315
}
1416

1517
[Test]
1618
public async Task CreateAsync_WithEmptyOptions_Succeeds()
1719
{
18-
// Arrange
19-
AfterRenderAsync = async () =>
20-
{
21-
return await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), new TAudioNodeOptions());
22-
};
23-
2420
// Act
25-
await OnAfterRerenderAsync();
21+
await using TAudioNode node = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), new TAudioNodeOptions());
2622

2723
// Assert
28-
_ = EvaluationContext.Exception.Should().BeNull();
29-
_ = EvaluationContext.Result.Should().BeOfType<TAudioNode>();
24+
_ = node.Should().BeOfType<TAudioNode>();
3025
}
3126

3227
[Test]
3328
public async Task CreateAsync_WithEmptyOptions_HasSameChannelCountModeAsWhenNoOptionsAreUsed()
3429
{
3530
// Arrange
36-
AfterRenderAsync = async () =>
37-
{
38-
await using TAudioNode emptyOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), new TAudioNodeOptions());
39-
await using TAudioNode noOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), null);
40-
41-
ChannelCountMode emptyOptionsCountMode = await emptyOptionsNode.GetChannelCountModeAsync();
42-
ChannelCountMode noOptionsCountMode = await noOptionsNode.GetChannelCountModeAsync();
43-
44-
return (emptyOptionsCountMode, noOptionsCountMode);
45-
};
31+
await using TAudioNode emptyOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), new TAudioNodeOptions());
32+
await using TAudioNode noOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), null);
4633

4734
// Act
48-
await OnAfterRerenderAsync();
35+
ChannelCountMode emptyOptionsCountMode = await emptyOptionsNode.GetChannelCountModeAsync();
36+
ChannelCountMode noOptionsCountMode = await noOptionsNode.GetChannelCountModeAsync();
4937

5038
// Assert
51-
_ = EvaluationContext.Exception.Should().BeNull();
52-
(ChannelCountMode emptyOptionsCountMode, ChannelCountMode noOptionsCountMode) = EvaluationContext.Result.Should().BeOfType<(ChannelCountMode, ChannelCountMode)>().Subject;
5339
_ = emptyOptionsCountMode.Should().Be(noOptionsCountMode);
5440
}
5541

5642
[Test]
5743
public async Task CreateAsync_WithEmptyOptions_HasSameChannelInterpretationAsWhenNoOptionsAreUsed()
5844
{
5945
// Arrange
60-
AfterRenderAsync = async () =>
61-
{
62-
await using TAudioNode emptyOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), new TAudioNodeOptions());
63-
await using TAudioNode noOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), null);
64-
65-
ChannelInterpretation emptyOptionsChannelInterpretation = await emptyOptionsNode.GetChannelInterpretationAsync();
66-
ChannelInterpretation noOptionsChannelInterpretation = await noOptionsNode.GetChannelInterpretationAsync();
67-
68-
return (emptyOptionsChannelInterpretation, noOptionsChannelInterpretation);
69-
};
46+
await using TAudioNode emptyOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), new TAudioNodeOptions());
47+
await using TAudioNode noOptionsNode = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), null);
7048

7149
// Act
72-
await OnAfterRerenderAsync();
50+
ChannelInterpretation emptyOptionsChannelInterpretation = await emptyOptionsNode.GetChannelInterpretationAsync();
51+
ChannelInterpretation noOptionsChannelInterpretation = await noOptionsNode.GetChannelInterpretationAsync();
7352

7453
// Assert
75-
_ = EvaluationContext.Exception.Should().BeNull();
76-
(ChannelInterpretation emptyOptionsChannelInterpretation, ChannelInterpretation noOptionsChannelInterpretation) = EvaluationContext.Result.Should().BeOfType<(ChannelInterpretation, ChannelInterpretation)>().Subject;
7754
_ = emptyOptionsChannelInterpretation.Should().Be(noOptionsChannelInterpretation);
7855
}
7956

@@ -84,29 +61,25 @@ public async Task CreateAsync_WithEmptyOptions_HasSameChannelInterpretationAsWhe
8461
public async Task CreateAsync_WithDifferentChannelCountModes_SetsChannelCountMode_ExceptForUnsupportedValues(ChannelCountMode mode)
8562
{
8663
// Arrange
87-
AfterRenderAsync = async () =>
88-
{
89-
TAudioNodeOptions options = new();
90-
options.ChannelCountMode = mode;
91-
92-
await using TAudioNode node = await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), options);
64+
TAudioNodeOptions options = new();
65+
options.ChannelCountMode = mode;
9366

67+
// Act
68+
Func<Task<ChannelCountMode>> action = async () =>
69+
{
70+
await using TAudioNode node = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), options);
9471
return await node.GetChannelCountModeAsync();
9572
};
9673

97-
// Act
98-
await OnAfterRerenderAsync();
99-
10074
// Assert
10175
if (UnsupportedChannelCountModes.TryGetValue(mode, out Type? exceptionType))
10276
{
103-
_ = EvaluationContext.Result.Should().Be(null);
104-
_ = EvaluationContext.Exception.Should().BeOfType(exceptionType);
77+
_ = (await action.Should().ThrowAsync<WebIDLException>()).And.Should().BeOfType(exceptionType);
10578
}
10679
else
10780
{
108-
_ = EvaluationContext.Exception.Should().BeNull();
109-
_ = EvaluationContext.Result.Should().Be(mode);
81+
ChannelCountMode result = await action();
82+
_ = result.Should().Be(mode);
11083
}
11184
}
11285

@@ -116,29 +89,26 @@ public async Task CreateAsync_WithDifferentChannelCountModes_SetsChannelCountMod
11689
public async Task CreateAsync_WithDifferentChannelInterpretations_SetsChannelInterpretation_ExceptForUnsupportedValues(ChannelInterpretation interpretation)
11790
{
11891
// Arrange
119-
AfterRenderAsync = async () =>
120-
{
121-
TAudioNodeOptions options = new();
122-
options.ChannelInterpretation = interpretation;
123-
124-
await using TAudioNode node = await CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), options);
92+
TAudioNodeOptions options = new();
93+
options.ChannelInterpretation = interpretation;
12594

95+
// Act
96+
Func<Task<ChannelInterpretation>> action = async () =>
97+
{
98+
await using TAudioNode node = await CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), options);
12699
return await node.GetChannelInterpretationAsync();
127100
};
128101

129-
// Act
130-
await OnAfterRerenderAsync();
131-
132102
// Assert
133103
if (UnsupportedChannelInterpretations.TryGetValue(interpretation, out Type? exceptionType))
134104
{
135-
_ = EvaluationContext.Result.Should().Be(null);
136-
_ = EvaluationContext.Exception.Should().BeOfType(exceptionType);
105+
_ = (await action.Should().ThrowAsync<WebIDLException>()).And.Should().BeOfType(exceptionType);
106+
137107
}
138108
else
139109
{
140-
_ = EvaluationContext.Exception.Should().BeNull();
141-
_ = EvaluationContext.Result.Should().Be(interpretation);
110+
ChannelInterpretation result = await action();
111+
_ = result.Should().Be(interpretation);
142112
}
143113
}
144114
}

tests/IntegrationTests/AudioNodeTests/ConstantSourceNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class ConstantSourceNodeTest : AudioNodeTest<ConstantSourceNode>
44
{
55
public override async Task<ConstantSourceNode> GetDefaultInstanceAsync()
66
{
7-
return await ConstantSourceNode.CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext());
7+
return await ConstantSourceNode.CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync());
88
}
99
}

tests/IntegrationTests/AudioNodeTests/IIRFilterNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class IIRFilterNodeTest : AudioNodeTest<IIRFilterNode>
44
{
55
public override async Task<IIRFilterNode> GetDefaultInstanceAsync()
66
{
7-
return await IIRFilterNode.CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), new IIRFilterOptions()
7+
return await IIRFilterNode.CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), new IIRFilterOptions()
88
{
99
Feedforward = [1],
1010
Feedback = [1],

tests/IntegrationTests/AudioNodeTests/MediaElementAudioSourceNodeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class MediaElementAudioSourceNodeTest : AudioNodeTest<MediaElementAudioSo
88
public override async Task<MediaElementAudioSourceNode> GetDefaultInstanceAsync()
99
{
1010
IJSObjectReference element = await EvaluationContext.GetAudioElementAyns();
11-
return await MediaElementAudioSourceNode.CreateAsync(EvaluationContext.JSRuntime, await EvaluationContext.GetAudioContext(), new MediaElementAudioSourceOptions()
11+
return await MediaElementAudioSourceNode.CreateAsync(EvaluationContext.JSRuntime, await GetAudioContextAsync(), new MediaElementAudioSourceOptions()
1212
{
1313
MediaElement = element
1414
});

0 commit comments

Comments
 (0)