Skip to content

Commit

Permalink
Add new E2E tests to verify that the "Dispose" method works correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamoto committed Apr 13, 2024
1 parent b0d75be commit f580055
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
51 changes: 51 additions & 0 deletions HotKeys2.E2ETest/HotKeysOnBrowserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,55 @@ public async Task Remove_Test(HostingModel hostingModel)
await Task.Delay(500);
await page.WaitForAsync(async _ => (await h1.TextContentAsync()) == "Hi, there!");
}

[TestCaseSource(typeof(HotKeysOnBrowserTest), nameof(AllHostingModels))]
public async Task Dispose_Test(HostingModel hostingModel)
{
var context = TestContext.Instance;
var host = await context.StartHostAsync(hostingModel);

// Navigate to the "Home" page,
var page = await context.GetPageAsync();
await page.GotoAndWaitForReadyAsync(host.GetUrl("/"));

var h1 = page.Locator("h1");
var h1TextBefore = await h1.TextContentAsync();
h1TextBefore.Is("Hello, world!");

// Verify the hot key "G" works correctly at this time. (Emulate to press the "G" key.)
await page.Keyboard.DownAsync("g");
await page.Keyboard.UpAsync("g");
await page.WaitForAsync(async _ => (await h1.TextContentAsync()) == "Hi, there!");

// But after the hot key was removed...
await page.ClickAsync("text=Dispose the hot key context");
await Task.Delay(200);

// The hot key "G" is no longer working.
await page.Keyboard.DownAsync("g");
await page.Keyboard.UpAsync("g");
await Task.Delay(500);
await page.WaitForAsync(async _ => (await h1.TextContentAsync()) == "Hi, there!");
}

[TestCaseSource(typeof(HotKeysOnBrowserTest), nameof(AllHostingModels))]
public async Task DisposeAfterCreateContextImmediately_Test(HostingModel hostingModel)
{
var context = TestContext.Instance;
var host = await context.StartHostAsync(hostingModel);

// Navigate to the "Home" page,
var page = await context.GetPageAsync();
await page.GotoAndWaitForReadyAsync(host.GetUrl("/"));

var h1 = page.Locator("h1");
var h1TextBefore = await h1.TextContentAsync();
h1TextBefore.Is("Hello, world!");

// The hot key "u" should not available at this time.
await page.Keyboard.DownAsync("u");
await page.Keyboard.UpAsync("u");
await Task.Delay(500);
await page.WaitForAsync(async _ => (await h1.TextContentAsync()) == "Hello, world!");
}
}
15 changes: 15 additions & 0 deletions SampleSites/Components/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Welcome to your new app.
<button class="btn btn-secondary" @onclick="OnClickRemoveHotKey">
Remove the hot key "G"
</button>

<button class="btn btn-secondary" @onclick="OnClickDisposeHotKeyContext">
Dispose the hot key context
</button>
</p>

@code {
Expand All @@ -28,6 +32,11 @@ Welcome to your new app.
this.HotKeysContext = this.HotKeys.CreateContext()
.Add(Code.G, OnHotKey)
.Add(Key.At, OnHotKey);

// Check the issue #20.
var temporaryContext = this.HotKeys.CreateContext()
.Add(Code.U, OnHotKey);
temporaryContext.Dispose();
}

private void OnHotKey()
Expand All @@ -43,6 +52,12 @@ Welcome to your new app.
this.HotKeysContext?.Remove(Code.G);
}

private void OnClickDisposeHotKeyContext()
{
this.HotKeysContext?.Dispose();
this.HotKeysContext = null;
}

public void Dispose()
{
this.HotKeysContext?.Dispose();
Expand Down

0 comments on commit f580055

Please sign in to comment.