using System; using System.Diagnostics.CodeAnalysis; using System.Reactive.Disposables; using System.Runtime.InteropServices; using Avalonia.Controls; using Avalonia.Markup.Xaml.MarkupExtensions; using Avalonia.Media; using Avalonia.Metadata; using Xunit; namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions; public class OptionsMarkupExtensionTests : XamlTestBase { public static Func? RaisedOption; public static int? ObjectsCreated; [Fact] public void Resolve_Default_Value() { using var _ = SetupTestGlobals("default"); var xaml = @" "; var textBlock = (TextBlock)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal("Hello World", textBlock.Text); } [Fact] public void Resolve_Default_Value_From_Ctor() { using var _ = SetupTestGlobals("default"); var xaml = @" "; var textBlock = (TextBlock)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal("Hello World", textBlock.Text); } [Fact] public void Resolve_Implicit_Default_Value_Ref_Type() { using var _ = SetupTestGlobals("default"); var xaml = @" "; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(null, userControl.Tag); } [Fact] public void Resolve_Implicit_Default_Value_Val_Type() { using var _ = SetupTestGlobals("default"); var xaml = @" "; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(0d, userControl.Height); } [Fact] public void Resolve_Implicit_Default_Value_Avalonia_Val_Type() { using var _ = SetupTestGlobals("default"); var xaml = @" "; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(new Thickness(0), userControl.Margin); } [Theory] [InlineData("option 1", "Im Option 1")] [InlineData("option 2", "Im Option 2")] [InlineData("3", "Im Option 3")] [InlineData("unknown", "Default value")] public void Resolve_Expected_Value_Per_Option(object option, string expectedResult) { using var _ = SetupTestGlobals(option); var xaml = @" "; var textBlock = (TextBlock)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(expectedResult, textBlock.Text); } [Theory] [InlineData("option 1", "Im Option 1")] [InlineData("option 2", "Im Option 2")] [InlineData("3", "Im Option 3")] [InlineData("unknown", "Default value")] public void Resolve_Expected_Value_Per_Option_Create_Single_Object(object option, string expectedResult) { using var _ = SetupTestGlobals(option); var xaml = @" "; var contentControl = (ContentControl)AvaloniaRuntimeXamlLoader.Load(xaml); var obj = Assert.IsType(contentControl.Content); Assert.Equal(expectedResult, obj.Name); Assert.Equal(1, ObjectsCreated); } [Fact] public void Convert_Bcl_Type() { using var _ = SetupTestGlobals("option 1"); var xaml = @" "; var border = (Border)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(50.1, border.Height); } [Fact] public void Convert_Avalonia_Type() { using var _ = SetupTestGlobals("option 1"); var xaml = @" "; var border = (Border)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(new Thickness(10, 8, 10, 8), border.Padding); } [PlatformFact(TestPlatforms.Windows | TestPlatforms.Linux, "TypeArguments test is failing on macOS from SRE emit")] public void Respect_Custom_TypeArgument() { using var _ = SetupTestGlobals("option 1"); var xaml = @" "; var textBlock = (TextBlock)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(new Thickness(10, 10, 10, 10), textBlock.Tag); } [Fact] public void Allow_Nester_Markup_Extensions() { using var _ = SetupTestGlobals("option 1"); var xaml = @" #ff506070 "; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); var border = (Border)userControl.Content!; Assert.Equal(Color.Parse("#ff506070"), ((ISolidColorBrush)border.Background!).Color); } [Fact] public void Allow_Nester_On_Platform_Markup_Extensions() { using var _ = SetupTestGlobals("option 1"); var xaml = @" "; var border = (Border)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(new Thickness(10), border.Margin); } [Fact] public void Support_Xml_Syntax() { using var _ = SetupTestGlobals("option 1"); var xaml = @" "; var border = (Border)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(Color.Parse("#ff506070"), ((ISolidColorBrush)border.Background!).Color); } [PlatformFact(TestPlatforms.Windows | TestPlatforms.Linux, "TypeArguments test is failing on macOS from SRE emit")] public void Support_Xml_Syntax_With_Custom_TypeArguments() { using var _ = SetupTestGlobals("option 1"); var xaml = @" "; var border = (Border)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(new Thickness(10, 10, 10, 10), border.Tag); } [Theory] [InlineData("option 1", "#ff506070")] [InlineData("3", "#000")] public void Support_Special_On_Syntax(object option, string color) { using var _ = SetupTestGlobals(option); var xaml = @" "; var border = (Border)AvaloniaRuntimeXamlLoader.Load(xaml); Assert.Equal(Color.Parse(color), ((ISolidColorBrush)border.Background!).Color); } [Fact] public void Support_Control_Inside_Xml_Syntax() { using var _ = SetupTestGlobals("option 1"); var xaml = @"