From 066e181e25f99362e10414dcc3c35382b82aef51 Mon Sep 17 00:00:00 2001 From: jsakamoto Date: Sun, 1 Oct 2023 20:52:22 +0900 Subject: [PATCH] Update E2E test for the "ExcludeSelector" option --- HotKeys2.E2ETest/HotKeysOnBrowserTest.cs | 39 ++++++++++++++++++++++ SampleSites/Components/Pages/Counter.razor | 10 +++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs b/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs index ab1abae..6960d46 100644 --- a/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs +++ b/HotKeys2.E2ETest/HotKeysOnBrowserTest.cs @@ -269,6 +269,45 @@ public async Task ExcludeContentEditable_Test(HostingModel hostingModel) await page.AssertUrlIsAsync(host.GetUrl("/fetchdata")); } + [Test] + [TestCaseSource(typeof(HotKeysOnBrowserTest), nameof(AllHostingModels))] + public async Task ExcludeSelector_Test(HostingModel hostingModel) + { + var context = TestContext.Instance; + var host = await context.StartHostAsync(hostingModel); + + // Navigate to the "Counter" page, + var page = await context.GetPageAsync(); + await page.GotoAndWaitForReadyAsync(host.GetUrl("/counter")); + + // Verify the counter is 0. + var counter = page.Locator("h1+p"); + await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 0"); + + // Set focus to the "Hotkeys are enabled in this field" input element, and type "U" key. + // Then the counter should be incremented. + var inputElement1 = page.GetByPlaceholder("Hotkeys are enabled in this field"); + await inputElement1.FocusAsync(); + await page.Keyboard.DownAsync("u"); + await page.Keyboard.UpAsync("u"); + await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 1"); + await page.Keyboard.DownAsync("u"); + await page.Keyboard.UpAsync("u"); + await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 2"); + + // Set focus to the "Hotkeys are disabled in this field" input element, and type "U" key. + // Then the counter should not be incremented. + var inputElement2 = page.GetByPlaceholder("Hotkeys are disabled in this field"); + await inputElement2.FocusAsync(); + await page.Keyboard.DownAsync("u"); + await page.Keyboard.UpAsync("u"); + await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 2"); + await page.Keyboard.DownAsync("u"); + await page.Keyboard.UpAsync("u"); + await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 2"); + await page.AssertEqualsAsync(_ => inputElement2.InputValueAsync(), "uu"); + } + [Test] [TestCaseSource(typeof(HotKeysOnBrowserTest), nameof(AllHostingModels))] public async Task ByNativeKey_Test(HostingModel hostingModel) diff --git a/SampleSites/Components/Pages/Counter.razor b/SampleSites/Components/Pages/Counter.razor index e5e4ad4..f49a7e3 100644 --- a/SampleSites/Components/Pages/Counter.razor +++ b/SampleSites/Components/Pages/Counter.razor @@ -17,6 +17,14 @@ } +
+ +
+ +
+ +
+ @code { private readonly bool IsWasm = RuntimeInformation.OSDescription == "web" || RuntimeInformation.OSDescription == "Browser"; @@ -27,7 +35,7 @@ protected override void OnInitialized() { this.HotKeysContext = this.HotKeys.CreateContext() - .Add(Code.U, this.IncrementCount); + .Add(Code.U, this.IncrementCount, new() { Exclude = Exclude.None, ExcludeSelector = ".disabled-hotkeys *" }); this.HotKeys.KeyDown += HotKeys_KeyDown; }