From b4736ee71da11bbbef1e4cff60ed52b7ca7f6377 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 30 May 2022 15:12:04 +0200 Subject: [PATCH] Add various tests for attached property syntax. While investigating #7925, I created a bunch of tests that describe how attached property syntax should work, cross-referencing with WPF. Most tests are passing but one failure. --- .../Xaml/BasicTests.cs | 92 +++++++++++- .../Xaml/StyleTests.cs | 131 ++++++++++++++++++ 2 files changed, 222 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs index f12785a7ce..58ad93721a 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs @@ -115,7 +115,97 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml Assert.Equal("Foo", ToolTip.GetTip(target)); } - + + [Fact] + public void Can_Use_Attached_Property_Syntax_On_NonAttached_Property() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var border = (Border)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(Colors.Red, ((ISolidColorBrush)border.Background).Color); + } + } + + [Fact] + public void Can_Use_Attached_Property_Syntax_On_Attached_AddOwnered_Property() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = (TextBlock)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(48, textBlock.FontSize); + } + } + + [Fact] + public void Can_Use_Attached_Property_Syntax_On_Attached_AddOwnered_Property_2() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = (TextBlock)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(48, textBlock.FontSize); + } + } + + [Fact] + public void Cannot_Use_Attached_Property_Syntax_From_Another_Control_On_Non_Attached_Property() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + Assert.ThrowsAny(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + } + } + + [Fact] + public void Cannot_Use_Attached_Property_Syntax_From_Another_Control_On_Non_Attached_Property_With_Markup_Extension() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + Assert.ThrowsAny(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + } + } + [Fact] public void NonExistent_Property_Throws() { diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs index bdd5cbbe2b..996c85f8bc 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs @@ -1,8 +1,10 @@ +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Xml; using Avalonia.Controls; +using Avalonia.Controls.Presenters; using Avalonia.Markup.Xaml.Styling; using Avalonia.Markup.Xaml.Templates; using Avalonia.Media; @@ -218,6 +220,135 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml } } + [Fact] + public void Setter_Can_Use_Attached_Property_Syntax_On_NonAttached_Property() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var border = (Border)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(Colors.Red, ((ISolidColorBrush)border.Background).Color); + } + } + + [Fact] + public void Setter_Can_Use_Attached_Property_Syntax_On_Attached_AddOwnered_Property() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = (TextBlock)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(48, textBlock.FontSize); + } + } + + [Fact] + public void Setter_Can_Use_Attached_Property_Syntax_On_Attached_AddOwnered_Property_2() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = (TextBlock)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(48, textBlock.FontSize); + } + } + + [Fact] + public void Binding_Setter_Can_Use_NonAttached_Property_Syntax_On_Attached_AddOwnered_Property_3() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentPresenter = (ContentPresenter)window.Content; + window.DataContext = new { Brush = Brushes.Red }; + + window.ApplyTemplate(); + + Assert.Equal(Colors.Red, ((ISolidColorBrush)contentPresenter.Foreground).Color); + } + } + + [Fact] + public void Setter_Can_Use_Attached_Property_Syntax_From_Another_Control_On_Non_Attached_Property() + { + // This is a weird one, but it follows WPF. Even though + // is an error in WPF, you can set the same property in a style and it works (because in + // the end they're actually the same property). + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = (TextBlock)window.Content; + + window.ApplyTemplate(); + + Assert.Equal(48, textBlock.FontSize); + } + } + [Fact(Skip = "The animation system currently needs to be able to set any property on any object")] public void Disallows_Setting_Non_Registered_Property() {