Skip to content

Commit 785f717

Browse files
Updated tests to simpler model.
1 parent 9027f4e commit 785f717

File tree

7 files changed

+154
-166
lines changed

7 files changed

+154
-166
lines changed
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
namespace BlazorServer;
1+

2+
namespace BlazorServer;
23

34
public class EvaluationContext
45
{
5-
public object? Result { get; set; }
6-
7-
public Exception? Exception { get; set; }
8-
9-
public Func<Task<object?>>? AfterRenderAsync { get; set; }
10-
}
6+
}

tests/BlazorServer/Pages/Index.razor

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,9 @@
88
[Inject]
99
public required EvaluationContext EvaluationContext { get; set; }
1010

11-
protected override async Task OnAfterRenderAsync(bool firstRender)
11+
protected override void OnAfterRender(bool firstRender)
1212
{
1313
if (!firstRender) return;
14-
try
15-
{
16-
if (EvaluationContext.AfterRenderAsync is not null)
17-
{
18-
EvaluationContext.Result = await EvaluationContext.AfterRenderAsync.Invoke();
19-
}
20-
}
21-
catch (Exception e)
22-
{
23-
EvaluationContext.Exception = e;
24-
}
2514
result = "done";
2615
StateHasChanged();
2716
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using BlazorServer;
2+
using Microsoft.AspNetCore.Hosting.Server;
3+
using Microsoft.AspNetCore.Hosting.Server.Features;
4+
using Microsoft.AspNetCore.Http.Features;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.JSInterop;
8+
using Microsoft.Playwright;
9+
10+
namespace KristofferStrube.Blazor.MediaCaptureStreams.IntegrationTests.Infrastructure;
11+
12+
[TestFixture("Chrome")]
13+
public class BlazorTest(string browserName)
14+
{
15+
private IHost? _host;
16+
17+
protected Uri RootUri;
18+
protected virtual string[] Args => ["--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream"];
19+
20+
protected MediaDevicesEvaluationContext EvaluationContext { get; set; } = default!;
21+
protected MediaDevicesEvaluationContext EvaluationContextCreator(IServiceProvider sp)
22+
{
23+
EvaluationContext = MediaDevicesEvaluationContext.Create(sp);
24+
return EvaluationContext;
25+
}
26+
27+
protected IJSRuntime JSRuntime => EvaluationContext.JSRuntime;
28+
protected IMediaDevicesService MediaDevicesService => EvaluationContext.MediaDevicesService;
29+
30+
protected IPlaywright PlaywrightInstance { get; set; }
31+
protected IBrowser Browser { get; set; }
32+
protected IBrowserContext Context { get; set; }
33+
protected IPage Page { get; set; }
34+
35+
[OneTimeSetUp]
36+
public async Task Setup()
37+
{
38+
PlaywrightInstance = await Playwright.CreateAsync();
39+
40+
IBrowserType browserType = browserName switch
41+
{
42+
"Firefox" => PlaywrightInstance.Firefox,
43+
_ => PlaywrightInstance.Chromium,
44+
};
45+
46+
Browser = await browserType.LaunchAsync(new()
47+
{
48+
Args = Args,
49+
});
50+
// Create a new incognito browser context
51+
Context = await Browser.NewContextAsync();
52+
// Create a new page inside context.
53+
Page = await Context.NewPageAsync();
54+
55+
_host = BlazorServer.Program.BuildWebHost([],
56+
serviceBuilder =>
57+
{
58+
_ = serviceBuilder
59+
.AddScoped(typeof(EvaluationContext), EvaluationContextCreator)
60+
.AddMediaDevicesService();
61+
}
62+
);
63+
64+
await _host.StartAsync();
65+
66+
RootUri = new(_host.Services.GetRequiredService<IServer>().Features
67+
.GetRequiredFeature<IServerAddressesFeature>()
68+
.Addresses.Single());
69+
}
70+
71+
[SetUp]
72+
public async Task SetupBeforeEachTestRun()
73+
{
74+
await OnAfterRerenderAsync();
75+
}
76+
77+
[OneTimeTearDown]
78+
public async Task TearDown()
79+
{
80+
if (Page is not null)
81+
{
82+
await Page.CloseAsync();
83+
}
84+
if (Context is not null)
85+
{
86+
await Context.CloseAsync();
87+
}
88+
if (Browser is not null)
89+
{
90+
await Browser.CloseAsync();
91+
}
92+
if (PlaywrightInstance is not null)
93+
{
94+
PlaywrightInstance.Dispose();
95+
}
96+
if (_host is not null)
97+
{
98+
await _host.StopAsync();
99+
_host.Dispose();
100+
}
101+
}
102+
103+
protected async Task OnAfterRerenderAsync()
104+
{
105+
_ = await Page.GotoAsync(RootUri.AbsoluteUri);
106+
await Assertions.Expect(Page.GetByTestId("result")).ToHaveTextAsync($"done");
107+
}
108+
}

tests/IntegrationTests/Infrastructure/MediaBlazorTest.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
using BlazorServer;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.JSInterop;
24

35
namespace KristofferStrube.Blazor.MediaCaptureStreams.IntegrationTests.Infrastructure;
46

5-
public class MediaDevicesEvaluationContext(IMediaDevicesService mediaDeviceService) : EvaluationContext
7+
public class MediaDevicesEvaluationContext(IJSRuntime jSRuntime, IMediaDevicesService mediaDeviceService) : EvaluationContext
68
{
9+
public IJSRuntime JSRuntime => jSRuntime;
10+
711
public IMediaDevicesService MediaDevicesService => mediaDeviceService;
12+
13+
public static MediaDevicesEvaluationContext Create(IServiceProvider provider)
14+
{
15+
IMediaDevicesService mediaDevicesService = provider.GetRequiredService<IMediaDevicesService>();
16+
IJSRuntime jSRuntime = provider.GetRequiredService<IJSRuntime>();
17+
18+
return new MediaDevicesEvaluationContext(jSRuntime, mediaDevicesService);
19+
}
820
}

tests/IntegrationTests/MediaDevicesNotSupportedTest.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22

33
namespace KristofferStrube.Blazor.MediaCaptureStreams.IntegrationTests;
44

5-
public class MediaDevicesNotSupportedTest : MediaBlazorTest
5+
public class MediaDevicesNotSupportedTest(string browserName) : BlazorTest(browserName)
66
{
77
protected override string[] Args => [];
88

99
[Test]
1010
public async Task GetUserMedia_Throws_NotSupportedErrorException_WhenNotSupported()
1111
{
1212
// Arrange
13-
AfterRenderAsync = async () =>
14-
{
15-
await using MediaDevices mediaDevices = await EvaluationContext.MediaDevicesService.GetMediaDevicesAsync();
16-
MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(new() { Audio = true });
17-
MediaStreamTrack[] videoTracks = await mediaStream.GetVideoTracksAsync();
18-
return videoTracks.Length;
19-
};
20-
21-
// Act
22-
await DoneLoadingPageAsync();
13+
await using MediaDevices mediaDevices = await MediaDevicesService.GetMediaDevicesAsync();
2314

2415
// Assert
25-
Assert.That(EvaluationContext.Exception, Is.InstanceOf<NotSupportedErrorException>().Or.InstanceOf<NotFoundErrorException>());
16+
Assert.ThrowsAsync<NotSupportedErrorException>(async () => await mediaDevices.GetUserMediaAsync(new() { Audio = true }));
2617
}
2718
}

0 commit comments

Comments
 (0)