diff --git a/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs b/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs index 6960d46..f7f3112 100644 --- a/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs +++ b/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs @@ -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!"); + } } diff --git a/SampleSites/Components/Pages/Index.razor b/SampleSites/Components/Pages/Index.razor index 935dc81..6fd6bfd 100644 --- a/SampleSites/Components/Pages/Index.razor +++ b/SampleSites/Components/Pages/Index.razor @@ -12,6 +12,10 @@ Welcome to your new app. + +

@code { @@ -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() @@ -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();