A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

138 lines
4.6 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.Media;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using JetBrains.Annotations;
using Xunit;
namespace Avalonia.Markup.Xaml.UnitTests
{
public class XamlIlTests
{
[Fact]
public void Binding_Button_IsPressed_ShouldWork()
{
var parsed = (Button)AvaloniaXamlLoader.Parse(@"
<Button xmlns='https://github.com/avaloniaui' IsPressed='{Binding IsPressed, Mode=TwoWay}' />");
var ctx = new XamlIlBugTestsDataContext();
parsed.DataContext = ctx;
parsed.SetValue(Button.IsPressedProperty, true);
Assert.True(ctx.IsPressed);
}
[Fact]
public void Transitions_Should_Be_Properly_Parsed()
{
var parsed = (Grid)AvaloniaXamlLoader.Parse(@"
<Grid xmlns='https://github.com/avaloniaui' >
<Grid.Transitions>
<DoubleTransition Property='Opacity'
Easing='CircularEaseIn'
Duration='0:0:0.5' />
</Grid.Transitions>
</Grid>");
Assert.Equal(1, parsed.Transitions.Count);
Assert.Equal(Visual.OpacityProperty, parsed.Transitions[0].Property);
}
[Fact]
public void Parser_Should_Override_Precompiled_Xaml()
{
var precompiled = new XamlIlClassWithPrecompiledXaml();
Assert.Equal(Brushes.Red, precompiled.Background);
Assert.Equal(1, precompiled.Opacity);
var loaded = (XamlIlClassWithPrecompiledXaml)AvaloniaXamlLoader.Parse(@"
<UserControl xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class='Avalonia.Markup.Xaml.UnitTests.XamlIlClassWithPrecompiledXaml'
Opacity='0'>
</UserControl>");
Assert.Equal(loaded.Opacity, 0);
Assert.Null(loaded.Background);
}
[Fact]
public void RelativeSource_TemplatedParent_Works()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var parsed = (Window)AvaloniaXamlLoader.Parse(@"
<Window
xmlns='https://github.com/avaloniaui'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests;assembly=Avalonia.Markup.Xaml.UnitTests'
>
<Window.Styles>
<Style Selector='Button'>
<Setter Property='Template'>
<ControlTemplate>
<Grid><Grid><Grid>
<Canvas>
<Canvas.Background>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding>
<MultiBinding.Converter>
<local:XamlIlBugTestsBrushToColorConverter/>
</MultiBinding.Converter>
<Binding Path='Background' RelativeSource='{RelativeSource TemplatedParent}'/>
<Binding Path='Background' RelativeSource='{RelativeSource TemplatedParent}'/>
<Binding Path='Background' RelativeSource='{RelativeSource TemplatedParent}'/>
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Canvas.Background>
</Canvas>
</Grid></Grid></Grid>
</ControlTemplate>
</Setter>
</Style>
</Window.Styles>
<Button Background='Red' />
</Window>
");
var btn = ((Button)parsed.Content);
btn.ApplyTemplate();
var canvas = (Canvas)btn.GetVisualChildren().First()
.VisualChildren.First()
.VisualChildren.First()
.VisualChildren.First();
Assert.Equal(Brushes.Red, canvas.Background);
}
}
}
public class XamlIlBugTestsAsIsConverter : IMultiValueConverter
{
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)
{
return ((ISolidColorBrush)values[0]).Color;
}
}
public class XamlIlBugTestsDataContext : INotifyPropertyChanged
{
public bool IsPressed { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class XamlIlClassWithPrecompiledXaml : UserControl
{
}
}