| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001 |
- // 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;
- 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 Portable.Xaml;
- using System.Collections;
- using System.ComponentModel;
- using System.Linq;
- using System.Xml;
- 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 AvaloniaProperty_Without_Getter_And_Setter_Is_Set()
- {
- var xaml =
- @"<local:NonControl xmlns='https://github.com/avaloniaui'
- xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
- Foo='55' />";
- var target = AvaloniaXamlLoader.Parse<NonControl>(xaml);
- Assert.Equal(55, target.GetValue(NonControl.FooProperty));
- }
- [Fact]
- public void AvaloniaProperty_With_Getter_And_No_Setter_Is_Set()
- {
- if(!AvaloniaXamlLoader.UseLegacyXamlLoader)
- return;
- var xaml =
- @"<local:NonControl xmlns='https://github.com/avaloniaui'
- xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'
- Bar='bar' />";
- var target = AvaloniaXamlLoader.Parse<NonControl>(xaml);
- Assert.Equal("bar", target.Bar);
- }
- [Fact]
- public void Attached_Property_Is_Set()
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.FontSize='21'/>";
- var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- Assert.NotNull(target);
- Assert.Equal(21.0, TextBlock.GetFontSize(target));
- }
- [Fact]
- public void Attached_Property_Is_Set_On_Control_Outside_Avalonia_Namspace()
- {
- // 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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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' TextBlock.FontSize='{Binding}'/>";
- var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- target.DataContext = 21.0;
- Assert.Equal(21.0, TextBlock.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 = AvaloniaXamlLoader.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(() => AvaloniaXamlLoader.Parse<ContentControl>(xaml));
- }
- [Fact]
- public void Non_Attached_Property_With_Attached_Property_Syntax_Throws()
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.Text='foo'/>";
- XamlTestHelpers.AssertThrowsXamlException(() => AvaloniaXamlLoader.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 = 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 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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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 ControlTemplate_With_Nested_Child_Is_Operational()
- {
- var xaml = @"
- <ControlTemplate xmlns='https://github.com/avaloniaui'>
- <ContentControl Name='parent'>
- <ContentControl Name='child' />
- </ContentControl>
- </ControlTemplate>
- ";
- var template = AvaloniaXamlLoader.Parse<ControlTemplate>(xaml);
- var parent = (ContentControl)template.Build(new ContentControl());
- Assert.Equal("parent", parent.Name);
- var child = parent.Content as ContentControl;
- Assert.NotNull(child);
- Assert.Equal("child", child.Name);
- }
- [Fact]
- public void ControlTemplate_With_Panel_Children_Are_Added()
- {
- var xaml = @"
- <ControlTemplate xmlns='https://github.com/avaloniaui'>
- <Panel Name='panel'>
- <ContentControl Name='Foo' />
- <ContentControl Name='Bar' />
- </Panel>
- </ControlTemplate>
- ";
- var template = AvaloniaXamlLoader.Parse<ControlTemplate>(xaml);
- var panel = (Panel)template.Build(new ContentControl());
- Assert.Equal(2, panel.Children.Count);
- var foo = panel.Children[0];
- var bar = panel.Children[1];
- Assert.Equal("Foo", foo.Name);
- Assert.Equal("Bar", bar.Name);
- }
- [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<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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.Parse<Style>(xaml);
- Assert.True(style.Resources.Count > 0);
- style.TryGetResource("Brush", out var brush);
- Assert.NotNull(brush);
- Assert.IsType<SolidColorBrush>(brush);
- Assert.Equal(Colors.White, ((ISolidColorBrush)brush).Color);
- style.TryGetResource("Double", out var d);
- Assert.Equal(10.0, d);
- }
- [Fact]
- public void StyleInclude_Is_Built()
- {
- 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.ContextMenu.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 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 = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- Assert.Null(target.Content);
- target.DataContext = "Foo";
- Assert.Equal("Foo", target.Content);
- }
- }
- [Fact]
- public void Xaml_Binding_Is_Delayed()
- {
- if (!AvaloniaXamlLoader.UseLegacyXamlLoader)
- return;
-
- using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
- {
- var xaml =
- @"<ContentControl xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
- var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
- Assert.Null(target.Content);
- target.DataContext = "Foo";
- Assert.Null(target.Content);
- DelayedBinding.ApplyBindings(target);
- 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 = AvaloniaXamlLoader.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' Items='{Binding}'>
- </ItemsControl>
- </Window>
- ";
- var target = AvaloniaXamlLoader.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.Items);
- }
- }
- [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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.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);
- 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 = AvaloniaXamlLoader.Parse<Window>(xaml);
- var button = window.FindControl<Button>("button");
- Assert.Equal("Foo", button.Content);
- }
- }
- [Fact(Skip =
- @"Doesn't work with Portable.xaml, it's working in different creation order -
- Handled in test 'Control_Is_Added_To_Parent_Before_Final_EndInit'
- do we need it?")]
- 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);
- }
- }
- [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 = AvaloniaXamlLoader.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 = AvaloniaXamlLoader.Parse<Window>(xaml);
- var tracker = (InitializationOrderTracker)window.Content;
- //ensure binding is set and operational first
- Assert.Equal(100.0, tracker.Tag);
- Assert.Equal("EndInit 0", tracker.Order.Last());
- }
- }
- [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 = AvaloniaXamlLoader.Parse<Window>(xaml);
- var tracker = (InitializationOrderTracker)window.Content;
- Assert.Equal(0, tracker.InitState);
- }
- }
- [Fact]
- public void DeferedXamlLoader_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 = AvaloniaXamlLoader.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 Items='{Binding Items}' SelectedItems='{Binding SelectedItems}'/>
- </Window>";
- var window = AvaloniaXamlLoader.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.Items);
- 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 = AvaloniaXamlLoader.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 loader = new AvaloniaXamlLoader {IsDesignMode = designMode};
- var obj = (Window)loader.Load(xaml);
- 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));
- }
- }
- }
- }
- 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 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);
- }
- }
|