| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982 |
- using System;
- using Avalonia.Collections;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Data;
- using Avalonia.Data.Converters;
- using Avalonia.Markup.Data;
- using Avalonia.Markup.Xaml.Styling;
- using Avalonia.Markup.Xaml.Templates;
- using Avalonia.Media;
- using Avalonia.Media.Immutable;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using System.Collections;
- using System.ComponentModel;
- using System.Linq;
- using System.Xml;
- using Xunit;
- using Avalonia.Controls.Documents;
- using Avalonia.Metadata;
- using Avalonia.Themes.Simple;
- namespace Avalonia.Markup.Xaml.UnitTests.Xaml
- {
- public class BasicTests : XamlTestBase
- {
- [Fact]
- public void Simple_Property_Is_Set()
- {
- var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
- var target = AvaloniaRuntimeXamlLoader.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 = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target);
- Assert.Equal("Foo", target.Content);
- }
- [Fact]
- public void Attached_Property_Is_Set()
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui' TextElement.FontSize='21'/>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target);
- Assert.Equal(21.0, TextElement.GetFontSize(target));
- }
- [Fact]
- public void Attached_Property_Is_Set_On_Control_Outside_Avalonia_Namespace()
- {
- // Test for issue #1548
- var xaml =
- @"<UserControl xmlns='https://github.com/avaloniaui'
- xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
- <local:TestControl Grid.Column='2' />
- </UserControl>";
- var target = AvaloniaRuntimeXamlLoader.Parse<UserControl>(xaml);
- Assert.Equal(2, Grid.GetColumn((TestControl)target.Content));
- }
- [Fact]
- public void Attached_Property_With_Namespace_Is_Set()
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:test='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
- test:BasicTestsAttachedPropertyHolder.Foo='Bar'/>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target);
- Assert.Equal("Bar", BasicTestsAttachedPropertyHolder.GetFoo(target));
- }
- [Fact]
- public void Attached_Property_Supports_Binding()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var xaml =
- @"<Window xmlns='https://github.com/avaloniaui' TextElement.FontSize='{Binding}'/>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- target.DataContext = 21.0;
- Assert.Equal(21.0, TextElement.GetFontSize(target));
- }
- }
- [Fact]
- public void Attached_Property_In_Panel_Is_Set()
- {
- var xaml = @"
- <Panel xmlns='https://github.com/avaloniaui'>
- <ToolTip.Tip>Foo</ToolTip.Tip>
- </Panel>";
- var target = AvaloniaRuntimeXamlLoader.Parse<Panel>(xaml);
- Assert.Empty(target.Children);
- Assert.Equal("Foo", ToolTip.GetTip(target));
- }
-
- [Fact]
- public void NonExistent_Property_Throws()
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui' DoesntExist='foo'/>";
- XamlTestHelpers.AssertThrowsXamlException(() => AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml));
- }
- [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 = AvaloniaRuntimeXamlLoader.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 = AvaloniaRuntimeXamlLoader.Parse<UserControl>(xaml);
- var button = control.FindControl<Button>("button");
- Assert.Equal("Foo", button.Content);
- }
- [Fact]
- public void Direct_Content_In_ItemsControl_Is_Operational()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'>
- <ItemsControl Name='items'>
- <ContentControl>Foo</ContentControl>
- <ContentControl>Bar</ContentControl>
- </ItemsControl>
- </Window>";
- var control = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var itemsControl = control.FindControl<ItemsControl>("items");
- Assert.NotNull(itemsControl);
- var items = itemsControl.Items.Cast<ContentControl>().ToArray();
- Assert.Equal("Foo", items[0].Content);
- Assert.Equal("Bar", items[1].Content);
- }
- }
- [Fact]
- public void Panel_Children_Are_Added()
- {
- var xaml = @"
- <UserControl xmlns='https://github.com/avaloniaui'>
- <Panel Name='panel'>
- <ContentControl Name='Foo' />
- <ContentControl Name='Bar' />
- </Panel>
- </UserControl>";
- var control = AvaloniaRuntimeXamlLoader.Parse<UserControl>(xaml);
- var panel = control.FindControl<Panel>("panel");
- Assert.Equal(2, panel.Children.Count);
- var foo = control.FindControl<ContentControl>("Foo");
- var bar = control.FindControl<ContentControl>("Bar");
- Assert.Contains(foo, panel.Children);
- Assert.Contains(bar, panel.Children);
- }
- [Fact]
- public void Grid_Row_Col_Definitions_Are_Built()
- {
- var xaml = @"
- <Grid xmlns='https://github.com/avaloniaui'>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width='100' />
- <ColumnDefinition Width='Auto' />
- <ColumnDefinition Width='*' />
- <ColumnDefinition Width='100*' />
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height='100' />
- <RowDefinition Height='Auto' />
- <RowDefinition Height='*' />
- <RowDefinition Height='100*' />
- </Grid.RowDefinitions>
- </Grid>";
- var grid = AvaloniaRuntimeXamlLoader.Parse<Grid>(xaml);
- Assert.Equal(4, grid.ColumnDefinitions.Count);
- Assert.Equal(4, grid.RowDefinitions.Count);
- var expected1 = new GridLength(100);
- var expected2 = GridLength.Auto;
- var expected3 = new GridLength(1, GridUnitType.Star);
- var expected4 = new GridLength(100, GridUnitType.Star);
- Assert.Equal(expected1, grid.ColumnDefinitions[0].Width);
- Assert.Equal(expected2, grid.ColumnDefinitions[1].Width);
- Assert.Equal(expected3, grid.ColumnDefinitions[2].Width);
- Assert.Equal(expected4, grid.ColumnDefinitions[3].Width);
- Assert.Equal(expected1, grid.RowDefinitions[0].Height);
- Assert.Equal(expected2, grid.RowDefinitions[1].Height);
- Assert.Equal(expected3, grid.RowDefinitions[2].Height);
- Assert.Equal(expected4, grid.RowDefinitions[3].Height);
- }
- [Fact]
- public void Grid_Row_Col_Definitions_Are_Parsed()
- {
- var xaml = @"
- <Grid xmlns='https://github.com/avaloniaui'
- ColumnDefinitions='100,Auto,*,100*'
- RowDefinitions='100,Auto,*,100*'>
- </Grid>";
- var grid = AvaloniaRuntimeXamlLoader.Parse<Grid>(xaml);
- Assert.Equal(4, grid.ColumnDefinitions.Count);
- Assert.Equal(4, grid.RowDefinitions.Count);
- var expected1 = new GridLength(100);
- var expected2 = GridLength.Auto;
- var expected3 = new GridLength(1, GridUnitType.Star);
- var expected4 = new GridLength(100, GridUnitType.Star);
- Assert.Equal(expected1, grid.ColumnDefinitions[0].Width);
- Assert.Equal(expected2, grid.ColumnDefinitions[1].Width);
- Assert.Equal(expected3, grid.ColumnDefinitions[2].Width);
- Assert.Equal(expected4, grid.ColumnDefinitions[3].Width);
- Assert.Equal(expected1, grid.RowDefinitions[0].Height);
- Assert.Equal(expected2, grid.RowDefinitions[1].Height);
- Assert.Equal(expected3, grid.RowDefinitions[2].Height);
- Assert.Equal(expected4, grid.RowDefinitions[3].Height);
- }
- [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 = AvaloniaRuntimeXamlLoader.Parse<UserControl>(xaml);
- var button = control.FindControl<Button>("button");
- Assert.Equal("Foo", button.Content);
- }
- [Fact]
- public void Standard_TypeConverter_Is_Used()
- {
- var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Width='200.5' />";
- var control = AvaloniaRuntimeXamlLoader.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 = AvaloniaRuntimeXamlLoader.Parse<UserControl>(xaml);
- var bk = control.Background;
- Assert.IsType<ImmutableSolidColorBrush>(bk);
- Assert.Equal(Colors.White, (bk as ISolidColorBrush).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 = AvaloniaRuntimeXamlLoader.Parse<Styles>(xaml);
- Assert.Single(styles);
- 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.Color, ((ISolidColorBrush)setters[0].Value).Color);
- Assert.Equal(TextBlock.WidthProperty, setters[1].Property);
- Assert.Equal(100.0, setters[1].Value);
- }
- [Fact]
- public void Style_Setter_With_AttachedProperty_Is_Parsed()
- {
- var xaml = @"
- <Styles xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style Selector='ContentControl'>
- <Setter Property='TextBlock.FontSize' Value='21'/>
- </Style>
- </Styles>";
- var styles = AvaloniaRuntimeXamlLoader.Parse<Styles>(xaml);
- Assert.Single(styles);
- var style = (Style)styles[0];
- var setters = style.Setters.Cast<Setter>().ToArray();
- Assert.Single(setters);
- Assert.Equal(TextBlock.FontSizeProperty, setters[0].Property);
- Assert.Equal(21.0, setters[0].Value);
- }
- [Fact]
- public void Complex_Style_Is_Parsed()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Styles xmlns='https://github.com/avaloniaui'>
- <Style Selector='CheckBox'>
- <Setter Property='BorderBrush' Value='{DynamicResource ThemeBorderMidBrush}'/>
- <Setter Property='BorderThickness' Value='{DynamicResource ThemeBorderThickness}'/>
- <Setter Property='Template'>
- <ControlTemplate>
- <Grid ColumnDefinitions='Auto,*'>
- <Border Name='border'
- BorderBrush='{TemplateBinding BorderBrush}'
- BorderThickness='{TemplateBinding BorderThickness}'
- Width='18'
- Height='18'
- VerticalAlignment='Center'>
- <Path Name='checkMark'
- Fill='{StaticResource HighlightBrush}'
- Width='11'
- Height='10'
- Stretch='Uniform'
- HorizontalAlignment='Center'
- VerticalAlignment='Center'
- Data='M 1145.607177734375,430 C1145.607177734375,430 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1138,434.5538330078125 1138,434.5538330078125 1138,434.5538330078125 1141.482177734375,438 1141.482177734375,438 1141.482177734375,438 1141.96875,437.9375 1141.96875,437.9375 1141.96875,437.9375 1147,431.34619140625 1147,431.34619140625 1147,431.34619140625 1145.607177734375,430 1145.607177734375,430 z'/>
- </Border>
- <ContentPresenter Name='PART_ContentPresenter'
- Content='{TemplateBinding Content}'
- ContentTemplate='{TemplateBinding ContentTemplate}'
- Margin='4,0,0,0'
- VerticalAlignment='Center'
- Grid.Column='1'/>
- </Grid>
- </ControlTemplate>
- </Setter>
- </Style>
- </Styles>
- ";
- var styles = AvaloniaRuntimeXamlLoader.Parse<Styles>(xaml);
- Assert.Single(styles);
- var style = (Style)styles[0];
- var setters = style.Setters.Cast<Setter>().ToArray();
- Assert.Equal(3, setters.Length);
- Assert.Equal(CheckBox.BorderBrushProperty, setters[0].Property);
- Assert.Equal(CheckBox.BorderThicknessProperty, setters[1].Property);
- Assert.Equal(CheckBox.TemplateProperty, setters[2].Property);
- Assert.IsType<ControlTemplate>(setters[2].Value);
- }
- }
- [Fact]
- public void Style_Resources_Are_Built()
- {
- var xaml = @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- xmlns:sys='clr-namespace:System;assembly=netstandard'>
- <Style.Resources>
- <SolidColorBrush x:Key='Brush'>White</SolidColorBrush>
- <sys:Double x:Key='Double'>10</sys:Double>
- </Style.Resources>
- </Style>";
- var style = AvaloniaRuntimeXamlLoader.Parse<Style>(xaml);
- Assert.True(style.Resources.Count > 0);
- style.TryGetResource("Brush", null, out var brush);
- Assert.NotNull(brush);
- Assert.IsAssignableFrom<ISolidColorBrush>(brush);
- Assert.Equal(Colors.White, ((ISolidColorBrush)brush).Color);
- style.TryGetResource("Double", null, out var d);
- Assert.Equal(10.0, d);
- }
- [Fact]
- public void Simple_Xaml_Binding_Is_Operational()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
- .With(windowingPlatform: new MockWindowingPlatform())))
- {
- var xaml =
- @"<Window xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.Null(target.Content);
- target.DataContext = "Foo";
- Assert.Equal("Foo", target.Content);
- }
- }
- [Fact]
- public void Double_Xaml_Binding_Is_Operational()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
- .With(windowingPlatform: new MockWindowingPlatform())))
- {
- var xaml =
- @"<Window xmlns='https://github.com/avaloniaui' Width='{Binding}'/>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.Null(target.Content);
- target.DataContext = 55.0;
- Assert.Equal(55.0, target.Width);
- }
- }
- [Fact]
- public void Collection_Xaml_Binding_Is_Operational()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
- .With(windowingPlatform: new MockWindowingPlatform())))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'>
- <ItemsControl Name='itemsControl' ItemsSource='{Binding}'>
- </ItemsControl>
- </Window>
- ";
- var target = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- Assert.NotNull(target.Content);
- var itemsControl = target.FindControl<ItemsControl>("itemsControl");
- var items = new string[] { "Foo", "Bar" };
- //DelayedBinding.ApplyBindings(itemsControl);
- target.DataContext = items;
- Assert.Equal(items, itemsControl.ItemsSource);
- }
- }
- [Fact]
- public void Multi_Xaml_Binding_Is_Parsed()
- {
- var xaml =
- @"<MultiBinding xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- Converter ='{x:Static BoolConverters.And}'>
- <Binding Path='Foo' />
- <Binding Path='Bar' />
- </MultiBinding>";
- var target = AvaloniaRuntimeXamlLoader.Parse<MultiBinding>(xaml);
- Assert.Equal(2, target.Bindings.Count);
- Assert.Equal(BoolConverters.And, target.Converter);
- var bindings = target.Bindings.Cast<Binding>().ToArray();
- Assert.Equal("Foo", bindings[0].Path);
- Assert.Equal("Bar", bindings[1].Path);
- }
- [Fact]
- public void Control_Template_Is_Operational()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
- .With(windowingPlatform: new MockWindowingPlatform())))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Template>
- <ControlTemplate TargetType='Window'>
- <ContentPresenter Name='PART_ContentPresenter'
- Content='{TemplateBinding Content}'/>
- </ControlTemplate>
- </Window.Template>
- </Window>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target.Template);
- Assert.Null(target.Presenter);
- target.ApplyTemplate();
- Assert.NotNull(target.Presenter);
- target.Content = "Foo";
- Assert.Equal("Foo", target.Presenter.Content);
- }
- }
- [Fact]
- public void Style_ControlTemplate_Is_Built()
- {
- var xaml = @"
- <Style xmlns='https://github.com/avaloniaui' Selector='ContentControl'>
- <Setter Property='Template'>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- Content='{TemplateBinding Content}'
- ContentTemplate='{TemplateBinding ContentTemplate}' />
- </ControlTemplate>
- </Setter>
- </Style> ";
- var style = AvaloniaRuntimeXamlLoader.Parse<Style>(xaml);
- Assert.Single(style.Setters);
- var setter = (Setter)style.Setters.First();
- Assert.Equal(ContentControl.TemplateProperty, setter.Property);
- Assert.IsType<ControlTemplate>(setter.Value);
- var template = (ControlTemplate)setter.Value;
- var control = new ContentControl();
- var result = (ContentPresenter)template.Build(control).Control;
- Assert.NotNull(result);
- }
- [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 = AvaloniaRuntimeXamlLoader.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 = AvaloniaRuntimeXamlLoader.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);
- }
- }
- [Fact]
- public void Control_Is_Added_To_Parent_Before_Final_EndInit()
- {
- 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 = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var tracker = (InitializationOrderTracker)window.Content;
- var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
- var endInit = tracker.Order.IndexOf("EndInit 0");
- Assert.NotEqual(-1, attached);
- Assert.NotEqual(-1, endInit);
- Assert.True(attached < endInit);
- }
- }
- [Fact]
- public void All_Properties_Are_Set_Before_Final_EndInit()
- {
- 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' Height='100'
- Tag='{Binding Height, RelativeSource={RelativeSource Self}}' />
- </Window>";
- var window = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var tracker = (InitializationOrderTracker)window.Content;
- //ensure binding is set and operational first
- Assert.Equal(100.0, tracker.Tag);
- // EndInit should be second-to-last operation, as last operation will be
- // caused by styling being applied on EndInit.
- Assert.Equal("EndInit 0", tracker.Order[tracker.Order.Count - 3]);
- // Caused by styling.
- Assert.Equal("Property FontFamily Changed", tracker.Order[tracker.Order.Count - 2]);
- Assert.Equal("Property Foreground Changed", tracker.Order[tracker.Order.Count - 1]);
- }
- }
- [Fact]
- public void BeginInit_Matches_EndInit()
- {
- 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 />
- </Window>";
- var window = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var tracker = (InitializationOrderTracker)window.Content;
- Assert.Equal(0, tracker.InitState);
- }
- }
- [Fact]
- public void DeferredXamlLoader_Should_Preserve_NamespacesContext()
- {
- var xaml =
- @"<ContentControl 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'>
- <ContentControl.ContentTemplate>
- <DataTemplate>
- <TextBlock Tag='{x:Static local:NonControl.StringProperty}'/>
- </DataTemplate>
- </ContentControl.ContentTemplate>
- </ContentControl>";
- var contentControl = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- var template = contentControl.ContentTemplate;
- Assert.NotNull(template);
- var txt = (TextBlock)template.Build(null);
- Assert.Equal((object)NonControl.StringProperty, txt.Tag);
- }
- [Fact]
- public void Binding_To_List_AvaloniaProperty_Is_Operational()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'>
- <ListBox ItemsSource='{Binding Items}' SelectedItems='{Binding SelectedItems}'/>
- </Window>";
- var window = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var listBox = (ListBox)window.Content;
- var vm = new SelectedItemsViewModel()
- {
- Items = new string[] { "foo", "bar", "baz" }
- };
- window.DataContext = vm;
- Assert.Equal(vm.Items, listBox.ItemsSource);
- Assert.Equal(vm.SelectedItems, listBox.SelectedItems);
- }
- }
- [Fact]
- public void Element_Whitespace_Should_Be_Trimmed()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'>
- <TextBlock>
- Hello World!
- </TextBlock>
- </Window>";
- var window = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var textBlock = (TextBlock)window.Content;
- Assert.Equal("Hello World!", textBlock.Text);
- }
- }
- [Fact]
- public void Design_Mode_Properties_Should_Be_Ignored_At_Runtime_And_Set_In_Design_Mode()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
- xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
- mc:Ignorable='d'
- d:DataContext='data-context'
- d:DesignWidth='123'
- d:DesignHeight='321'
- >
- </Window>";
- foreach (var designMode in new[] {true, false})
- {
- var obj = (Window)AvaloniaRuntimeXamlLoader.Load(xaml, designMode: designMode);
- var context = Design.GetDataContext(obj);
- var width = Design.GetWidth(obj);
- var height = Design.GetHeight(obj);
- if (designMode)
- {
- Assert.Equal("data-context", context);
- Assert.Equal(123, width);
- Assert.Equal(321, height);
- }
- else
- {
- Assert.False(obj.IsSet(Design.DataContextProperty));
- Assert.False(obj.IsSet(Design.WidthProperty));
- Assert.False(obj.IsSet(Design.HeightProperty));
- }
- }
- }
- }
- [Fact]
- public void Slider_Properties_Can_Be_Set_In_Any_Order()
- {
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'>
- <Slider Width='400' Value='500' Minimum='0' Maximum='1000'/>
- </Window>";
- var window = AvaloniaRuntimeXamlLoader.Parse<Window>(xaml);
- var slider = (Slider)window.Content;
- Assert.Equal(0, slider.Minimum);
- Assert.Equal(1000, slider.Maximum);
- Assert.Equal(500, slider.Value);
- }
- }
- [Fact]
- public void Should_Parse_Tip_With_Comment()
- {
- var xaml = @"
- <TextBlock xmlns='https://github.com/avaloniaui' Text='TextBlock with tooltip'>
- <ToolTip.Tip>
- <!--Comment-->
- <ToolTip>
- Foo
- </ToolTip>
- </ToolTip.Tip>
- </TextBlock>";
- var textBlock = AvaloniaRuntimeXamlLoader.Parse<TextBlock>(xaml);
- var toolTip = ToolTip.GetTip(textBlock) as ToolTip;
- Assert.NotNull(toolTip);
- Assert.Equal("Foo", toolTip.Content);
- }
- [Fact]
- public void AddChild_Child_Is_Set()
- {
- var xaml = @"<ObjectWithAddChild xmlns='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml'>Foo</ObjectWithAddChild>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ObjectWithAddChild>(xaml);
- Assert.NotNull(target);
- Assert.Equal("Foo", target.Child);
- }
- [Fact]
- public void AddChildOfT_Child_Is_Set()
- {
- var xaml = @"<ObjectWithAddChildOfT xmlns='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml'>Foo</ObjectWithAddChildOfT>";
- var target = AvaloniaRuntimeXamlLoader.Parse<ObjectWithAddChildOfT>(xaml);
- Assert.NotNull(target);
- Assert.Null(target.Child);
- Assert.Equal("Foo", target.Text);
- }
- [Fact]
- public void Should_Parse_And_Populate_Type_Without_Public_Ctor()
- {
- var xaml = @"<ObjectWithoutPublicCtor xmlns='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml' Test2='World' />";
- var target = (ObjectWithoutPublicCtor)AvaloniaRuntimeXamlLoader.Load(xaml, rootInstance: new ObjectWithoutPublicCtor("Hello"));
- Assert.NotNull(target);
- Assert.Equal("World", target.Test2);
- Assert.Equal("Hello", target.Test1);
- }
-
- private class SelectedItemsViewModel : INotifyPropertyChanged
- {
- public string[] Items { get; set; }
- public event PropertyChangedEventHandler PropertyChanged;
- private IList _selectedItems = new AvaloniaList<string>();
- public IList SelectedItems
- {
- get { return _selectedItems; }
- set
- {
- _selectedItems = value;
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItems)));
- }
- }
- }
- }
- public class ObjectWithAddChild : IAddChild
- {
- public object Child { get; set; }
- void IAddChild.AddChild(object child)
- {
- Child = child;
- }
- }
-
- public class ObjectWithoutPublicCtor
- {
- public ObjectWithoutPublicCtor(string param)
- {
- Test1 = param;
- }
-
- public string Test1 { get; set; }
-
- public string Test2 { get; set; }
- }
- public class ObjectWithAddChildOfT : IAddChild, IAddChild<string>
- {
- public string Text { get; set; }
- public object Child { get; set; }
- void IAddChild.AddChild(object child)
- {
- Child = child;
- }
- void IAddChild<string>.AddChild(string child)
- {
- Text = child;
- }
- }
- public class BasicTestsAttachedPropertyHolder
- {
- public static AvaloniaProperty<string> FooProperty =
- AvaloniaProperty.RegisterAttached<BasicTestsAttachedPropertyHolder, AvaloniaObject, string>("Foo");
- public static void SetFoo(AvaloniaObject target, string value) => target.SetValue(FooProperty, value);
- public static string GetFoo(AvaloniaObject target) => (string)target.GetValue(FooProperty);
- }
- }
|