diff --git a/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs b/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs index 09fae7207f..7e1f7f1dc3 100644 --- a/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs +++ b/tests/Avalonia.Input.UnitTests/InputElement_Focus.cs @@ -121,5 +121,93 @@ namespace Avalonia.Input.UnitTests Assert.False(target2.Classes.Contains(":focus-visible")); } } + + [Fact] + public void Control_FocusWithin_PseudoClass_Should_Be_Applied() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target1 = new Decorator(); + var target2 = new Decorator(); + var root = new TestRoot + { + Child = new StackPanel + { + Children = + { + target1, + target2 + } + } + }; + + target1.ApplyTemplate(); + target2.ApplyTemplate(); + + FocusManager.Instance?.Focus(target1); + Assert.True(target1.IsFocused); + Assert.True(target1.Classes.Contains(":focus-within")); + Assert.True(target1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + } + } + + [Fact] + public void Control_FocusWithin_PseudoClass_Should_Be_Applied_and_Removed() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target1 = new Decorator(); + var target2 = new Decorator(); + var panel1 = new Panel { Children = { target1 } }; + var panel2 = new Panel { Children = { target2 } }; + var root = new TestRoot + { + Child = new StackPanel + { + Children = + { + panel1, + panel2 + } + } + }; + + target1.ApplyTemplate(); + target2.ApplyTemplate(); + + FocusManager.Instance?.Focus(target1); + Assert.True(target1.IsFocused); + Assert.True(target1.Classes.Contains(":focus-within")); + Assert.True(target1.IsKeyboardFocusWithin); + Assert.True(panel1.Classes.Contains(":focus-within")); + Assert.True(panel1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + + FocusManager.Instance?.Focus(target2); + + Assert.False(target1.IsFocused); + Assert.False(target1.Classes.Contains(":focus-within")); + Assert.False(target1.IsKeyboardFocusWithin); + Assert.False(panel1.Classes.Contains(":focus-within")); + Assert.False(panel1.IsKeyboardFocusWithin); + Assert.True(root.Child.Classes.Contains(":focus-within")); + Assert.True(root.Child.IsKeyboardFocusWithin); + Assert.True(root.Classes.Contains(":focus-within")); + Assert.True(root.IsKeyboardFocusWithin); + + Assert.True(target2.IsFocused); + Assert.True(target2.Classes.Contains(":focus-within")); + Assert.True(target2.IsKeyboardFocusWithin); + Assert.True(panel2.Classes.Contains(":focus-within")); + Assert.True(panel2.IsKeyboardFocusWithin); + } + } } }