From 0e869730a389d2266f6ed2e30809adffcb9c1d28 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 20 Jul 2024 23:15:49 +0200 Subject: [PATCH] Fix context menu keyboard selection. (#16354) When opening a context menu and pressing the "arrow down" key, the first menu item was not selected. This issue was introduced by #11287. The fix is to make the `ContextMenu` focusable. Added an integration test to try to prevent regressions. Co-authored-by: Max Katz --- samples/IntegrationTestApp/MainWindow.axaml | 13 +++++++ .../Controls/ContextMenu.xaml | 1 + .../Controls/ContextMenu.xaml | 1 + .../ContextMenuTests.cs | 36 +++++++++++++++++++ .../ElementExtensions.cs | 3 +- 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/Avalonia.IntegrationTests.Appium/ContextMenuTests.cs diff --git a/samples/IntegrationTestApp/MainWindow.axaml b/samples/IntegrationTestApp/MainWindow.axaml index ce41a8c121..ec49e4bb10 100644 --- a/samples/IntegrationTestApp/MainWindow.axaml +++ b/samples/IntegrationTestApp/MainWindow.axaml @@ -93,6 +93,19 @@ + + + + + + Tray Icon Clicked diff --git a/src/Avalonia.Themes.Fluent/Controls/ContextMenu.xaml b/src/Avalonia.Themes.Fluent/Controls/ContextMenu.xaml index c4a93ccb19..50f4844271 100644 --- a/src/Avalonia.Themes.Fluent/Controls/ContextMenu.xaml +++ b/src/Avalonia.Themes.Fluent/Controls/ContextMenu.xaml @@ -38,6 +38,7 @@ + diff --git a/src/Avalonia.Themes.Simple/Controls/ContextMenu.xaml b/src/Avalonia.Themes.Simple/Controls/ContextMenu.xaml index 215f4eec5a..5c8f799f94 100644 --- a/src/Avalonia.Themes.Simple/Controls/ContextMenu.xaml +++ b/src/Avalonia.Themes.Simple/Controls/ContextMenu.xaml @@ -6,6 +6,7 @@ + diff --git a/tests/Avalonia.IntegrationTests.Appium/ContextMenuTests.cs b/tests/Avalonia.IntegrationTests.Appium/ContextMenuTests.cs new file mode 100644 index 0000000000..416c480a42 --- /dev/null +++ b/tests/Avalonia.IntegrationTests.Appium/ContextMenuTests.cs @@ -0,0 +1,36 @@ +using System.Threading; +using OpenQA.Selenium; +using OpenQA.Selenium.Interactions; +using Xunit; + +namespace Avalonia.IntegrationTests.Appium +{ + [Collection("Default")] + public class ContextMenuTests + { + private readonly AppiumDriver _session; + + public ContextMenuTests(DefaultAppFixture fixture) + { + _session = fixture.Session; + + var tabs = _session.FindElementByAccessibilityId("MainTabs"); + var tab = tabs.FindElementByName("ContextMenu"); + tab.Click(); + } + + [PlatformFact(TestPlatforms.Windows)] + public void Select_First_Item_With_Down_Arrow_Key() + { + var control = _session.FindElementByAccessibilityId("ShowContextMenu"); + + new Actions(_session) + .ContextClick(control) + .SendKeys(Keys.ArrowDown) + .Perform(); + + var clickedMenuItem = _session.FindElementByAccessibilityId("ContextMenuItem1"); + Assert.True(clickedMenuItem.GetIsFocused()); + } + } +} diff --git a/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs b/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs index 217d3cc4fa..d9317e8e6a 100644 --- a/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs +++ b/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs @@ -91,8 +91,7 @@ namespace Avalonia.IntegrationTests.Appium { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - var active = element.WrappedDriver.SwitchTo().ActiveElement() as AppiumWebElement; - return element.Id == active?.Id; + return element.GetAttribute("HasKeyboardFocus") == "True"; } else {