| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- using System;
- using System.Linq;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Data;
- using Avalonia.Diagnostics;
- using Avalonia.Markup.Xaml.Templates;
- using Avalonia.Media;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Xunit;
- namespace Avalonia.Markup.Xaml.UnitTests.Xaml
- {
- public class ControlTemplateTests : XamlTestBase
- {
- [Fact]
- public void StyledProperties_Should_Be_Set_In_The_ControlTemplate()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- xmlns:controls=""using:Avalonia.Markup.Xaml.UnitTests.Xaml"">
- <Button>
- <Button.Template>
- <ControlTemplate>
- <controls:ListBoxHierachyLine>
- <controls:ListBoxHierachyLine.LineDashStyle>
- <DashStyle Dashes=""2,2"" Offset=""1"" />
- </controls:ListBoxHierachyLine.LineDashStyle>
- </controls:ListBoxHierachyLine>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var listBoxHierarchyLine = button.GetVisualChildren().ElementAt(0) as ListBoxHierarchyLine;
- Assert.Equal(1, listBoxHierarchyLine.LineDashStyle.Offset);
- Assert.Equal(2, listBoxHierarchyLine.LineDashStyle.Dashes.Count);
- Assert.Equal(2, listBoxHierarchyLine.LineDashStyle.Dashes[0]);
- Assert.Equal(2, listBoxHierarchyLine.LineDashStyle.Dashes[1]);
- }
-
- }
- [Fact]
- public void Inline_ControlTemplate_Styled_Values_Are_Set_With_Style_Priority()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Button>
- <Button.Template>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- Background='Red'/>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var presenter = (ContentPresenter)button.Presenter;
- Assert.Equal(Brushes.Red, presenter.Background);
- var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
- Assert.Equal(BindingPriority.Template, diagnostic.Priority);
- }
- }
- [Fact]
- public void Style_ControlTemplate_Styled_Values_Are_Set_With_Style_Priority()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Styles>
- <Style Selector='Button'>
- <Setter Property='Template'>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- Background='Red'/>
- </ControlTemplate>
- </Setter>
- </Style>
- </Window.Styles>
- <Button/>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var presenter = (ContentPresenter)button.Presenter;
- Assert.Equal(Brushes.Red, presenter.Background);
- var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
- Assert.Equal(BindingPriority.Template, diagnostic.Priority);
- }
- }
- [Fact]
- public void ControlTemplate_Attached_Values_Are_Set_With_Style_Priority()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Button>
- <Button.Template>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- DockPanel.Dock='Top'/>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var presenter = (ContentPresenter)button.Presenter;
- Assert.Equal(Dock.Top, DockPanel.GetDock(presenter));
- var diagnostic = presenter.GetDiagnostic(DockPanel.DockProperty);
- Assert.Equal(BindingPriority.Template, diagnostic.Priority);
- }
- }
- [Fact]
- public void ControlTemplate_StaticResources_Are_Set_With_Style_Priority()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <SolidColorBrush x:Key='red'>Red</SolidColorBrush>
- </Window.Resources>
- <Button Content='Foo'>
- <Button.Template>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- Background='{StaticResource red}'/>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var presenter = (ContentPresenter)button.Presenter;
- Assert.Equal(Brushes.Red, presenter.Background);
- var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
- Assert.Equal(BindingPriority.Template, diagnostic.Priority);
- }
- }
- [Fact]
- public void ControlTemplate_DynamicResources_Are_Set_With_Style_Priority()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <SolidColorBrush x:Key='red'>Red</SolidColorBrush>
- </Window.Resources>
- <Button Content='Foo'>
- <Button.Template>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- Background='{DynamicResource red}'/>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var presenter = (ContentPresenter)button.Presenter;
- Assert.Equal(Brushes.Red, presenter.Background);
- var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
- Assert.Equal(BindingPriority.Template, diagnostic.Priority);
- }
- }
- [Fact]
- public void ControlTemplate_TemplateBindings_Are_Set_With_TemplatedParent_Priority()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Button Content='Foo'>
- <Button.Template>
- <ControlTemplate>
- <ContentPresenter Name='PART_ContentPresenter'
- Content='{TemplateBinding Content}'/>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = (Button)window.Content;
- window.ApplyTemplate();
- button.ApplyTemplate();
- var presenter = (ContentPresenter)button.Presenter;
- Assert.Equal("Foo", presenter.Content);
- var diagnostic = presenter.GetDiagnostic(ContentPresenter.ContentProperty);
- Assert.Equal(BindingPriority.Template, diagnostic.Priority);
- }
- }
- [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 = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
- var parent = (ContentControl)template.Build(new ContentControl()).Control;
- Assert.Equal("parent", parent.Name);
- var child = parent.Content as ContentControl;
- Assert.NotNull(child);
- Assert.Equal("child", child.Name);
- }
- [Fact]
- public void ControlTemplate_With_TargetType_Is_Operational()
- {
- var xaml = @"
- <ControlTemplate xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- TargetType='{x:Type ContentControl}'>
- <ContentPresenter Content='{TemplateBinding Content}' />
- </ControlTemplate>
- ";
- var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
- Assert.Equal(typeof(ContentControl), template.TargetType);
- Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Control);
- }
- [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 = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
- var panel = (Panel)template.Build(new ContentControl()).Control;
- 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);
- }
- }
- public class ListBoxHierarchyLine : Panel
- {
- public static readonly StyledProperty<DashStyle> LineDashStyleProperty =
- AvaloniaProperty.Register<ListBoxHierarchyLine, DashStyle>(nameof(LineDashStyle));
- public DashStyle LineDashStyle
- {
- get => GetValue(LineDashStyleProperty);
- set => SetValue(LineDashStyleProperty, value);
- }
- }
- }
|