using System.Xml;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Xunit;
namespace Avalonia.Markup.Xaml.UnitTests.Xaml
{
public class ControlThemeTests : XamlTestBase
{
[Fact]
public void ControlTheme_Can_Be_StaticResource()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = $@"
{ControlThemeXaml}
";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var button = Assert.IsType(window.Content);
window.Show();
Assert.NotNull(button.Template);
var child = Assert.Single(button.GetVisualChildren());
var border = Assert.IsType(child);
Assert.Equal(Brushes.Red, border.Background);
}
}
[Fact]
public void ControlTheme_Can_Be_DynamicResource()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = $@"
{ControlThemeXaml}
";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var button = Assert.IsType(window.Content);
window.Show();
Assert.NotNull(button.Template);
var child = Assert.Single(button.GetVisualChildren());
var border = Assert.IsType(child);
Assert.Equal(Brushes.Red, border.Background);
}
}
[Fact]
public void ControlTheme_Can_Be_Set_In_Style()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = $@"
{ControlThemeXaml}
";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var button = Assert.IsType(window.Content);
window.Show();
Assert.NotNull(button.Template);
var child = Assert.Single(button.GetVisualChildren());
var border = Assert.IsType(child);
Assert.Equal(Brushes.Red, border.Background);
}
}
[Fact]
public void Correctly_Resolve_TemplateBinding_In_Nested_Style()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = $@"
";
var theme = (ControlTheme)AvaloniaRuntimeXamlLoader.Load(xaml);
var style = Assert.IsType
""";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var control = Assert.IsType(window.Content);
Assert.Same(TextDecorations.Strikethrough,control.GetValue(TextBlock.TextDecorationsProperty));
}
}
[Fact]
public void Can_Binding_Classes_In_Setter()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = $$$"""
""";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
window.ApplyTemplate();
var vm = window.DataContext as TestViewModel;
Assert.NotNull(vm);
var control = Assert.IsType(window.Content);
Assert.Null(control.GetValue(TextBlock.TextDecorationsProperty));
vm.Boolean = true;
Assert.Same(TextDecorations.Strikethrough, control.GetValue(TextBlock.TextDecorationsProperty));
}
}
private const string ControlThemeXaml = @"
";
}
}