| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System.Linq;
- using Avalonia.Controls;
- using Avalonia.Markup.Xaml.Styling;
- using Avalonia.Media;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Markup.Xaml.UnitTests.Xaml
- {
- public class BasicTests
- {
- [Fact]
- public void Simple_Property_Is_Set()
- {
- var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
- var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target);
- Assert.Equal("Foo", target.Content);
- }
- [Fact]
- public void Default_Content_Property_Is_Set()
- {
- var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui'>Foo</ContentControl>";
- var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target);
- Assert.Equal("Foo", target.Content);
- }
- [Fact]
- public void ContentControl_ContentTemplate_Is_Functional()
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui'>
- <ContentControl.ContentTemplate>
- <DataTemplate>
- <TextBlock Text='Foo' />
- </DataTemplate>
- </ContentControl.ContentTemplate>
- </ContentControl>";
- var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- var target = contentControl.ContentTemplate;
- Assert.NotNull(target);
- var txt = (TextBlock)target.Build(null);
- Assert.Equal("Foo", txt.Text);
- }
- [Fact]
- public void Named_Control_Is_Added_To_NameScope_Simple()
- {
- var xaml = @"
- <UserControl xmlns='https://github.com/avaloniaui'>
- <Button Name='button'>Foo</Button>
- </UserControl>";
- var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
- var button = control.FindControl<Button>("button");
- Assert.Equal("Foo", button.Content);
- }
- [Fact]
- public void Named_x_Control_Is_Added_To_NameScope_Simple()
- {
- var xaml = @"
- <UserControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Button x:Name='button'>Foo</Button>
- </UserControl>";
- var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
- var button = control.FindControl<Button>("button");
- Assert.Equal("Foo", button.Content);
- }
- [Fact]
- public void Standart_TypeConverter_Is_Used()
- {
- var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Width='200.5' />";
- var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
- Assert.Equal(200.5, control.Width);
- }
- [Fact]
- public void Avalonia_TypeConverter_Is_Used()
- {
- var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Background='White' />";
- var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
- var bk = control.Background;
- Assert.IsType<SolidColorBrush>(bk);
- Assert.Equal(Colors.White, (bk as SolidColorBrush).Color);
- }
- [Fact]
- public void Simple_Style_Is_Parsed()
- {
- var xaml = @"
- <Styles xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style Selector='TextBlock'>
- <Setter Property='Background' Value='White'/>
- <Setter Property='Width' Value='100'/>
- </Style>
- </Styles>";
- var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
- Assert.Equal(1, styles.Count);
- var style = (Style)styles[0];
- var setters = style.Setters.Cast<Setter>().ToArray();
- Assert.Equal(2, setters.Length);
- Assert.Equal(TextBlock.BackgroundProperty, setters[0].Property);
- Assert.Equal(Brushes.White, setters[0].Value);
- Assert.Equal(TextBlock.WidthProperty, setters[1].Property);
- Assert.Equal(100.0, setters[1].Value);
- }
- [Fact]
- public void Style_Resources_Are_Build()
- {
- var xaml = @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- xmlns:sys='clr-namespace:System;assembly=mscorlib'>
- <Style.Resources>
- <SolidColorBrush x:Key='Brush'>White</SolidColorBrush>
- <sys:Double x:Key='Double'>10</sys:Double>
- </Style.Resources>
- </Style>";
- var style = AvaloniaXamlLoader.Parse<Style>(xaml);
- Assert.True(style.Resources.Count > 0);
- var brush = style.FindResource("Brush") as SolidColorBrush;
- Assert.NotNull(brush);
- Assert.Equal(Colors.White, brush.Color);
- var d = (double)style.FindResource("Double");
- Assert.Equal(10.0, d);
- }
- [Fact]
- public void StyleInclude_Is_Build()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Styles xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <StyleInclude Source='resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default'/>
- </Styles>";
- var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
- Assert.True(styles.Count == 1);
- var styleInclude = styles.First() as StyleInclude;
- Assert.NotNull(styleInclude);
- var style = styleInclude.Loaded;
- Assert.NotNull(style);
- }
- }
- [Fact]
- public void Named_Control_Is_Added_To_NameScope()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Button Name='button'>Foo</Button>
- </Window>";
- var window = AvaloniaXamlLoader.Parse<Window>(xaml);
- var button = window.FindControl<Button>("button");
- Assert.Equal("Foo", button.Content);
- }
- }
- [Fact]
- public void Control_Is_Added_To_Parent_Before_Properties_Are_Set()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
- <local:InitializationOrderTracker Width='100'/>
- </Window>";
- var window = AvaloniaXamlLoader.Parse<Window>(xaml);
- var tracker = (InitializationOrderTracker)window.Content;
- var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
- var widthChanged = tracker.Order.IndexOf("Property Width Changed");
- Assert.NotEqual(-1, attached);
- Assert.NotEqual(-1, widthChanged);
- Assert.True(attached < widthChanged);
- }
- }
- }
- }
|