ControlTemplateTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Data;
  6. using Avalonia.Diagnostics;
  7. using Avalonia.Markup.Xaml.Templates;
  8. using Avalonia.Media;
  9. using Avalonia.UnitTests;
  10. using Avalonia.VisualTree;
  11. using Xunit;
  12. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  13. {
  14. public class ControlTemplateTests : XamlTestBase
  15. {
  16. [Fact]
  17. public void StyledProperties_Should_Be_Set_In_The_ControlTemplate()
  18. {
  19. using (UnitTestApplication.Start(TestServices.StyledWindow))
  20. {
  21. var xaml = @"
  22. <Window xmlns='https://github.com/avaloniaui'
  23. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  24. xmlns:controls=""using:Avalonia.Markup.Xaml.UnitTests.Xaml"">
  25. <Button>
  26. <Button.Template>
  27. <ControlTemplate>
  28. <controls:ListBoxHierachyLine>
  29. <controls:ListBoxHierachyLine.LineDashStyle>
  30. <DashStyle Dashes=""2,2"" Offset=""1"" />
  31. </controls:ListBoxHierachyLine.LineDashStyle>
  32. </controls:ListBoxHierachyLine>
  33. </ControlTemplate>
  34. </Button.Template>
  35. </Button>
  36. </Window>";
  37. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  38. var button = (Button)window.Content;
  39. window.ApplyTemplate();
  40. button.ApplyTemplate();
  41. var listBoxHierarchyLine = button.GetVisualChildren().ElementAt(0) as ListBoxHierarchyLine;
  42. Assert.Equal(1, listBoxHierarchyLine.LineDashStyle.Offset);
  43. Assert.Equal(2, listBoxHierarchyLine.LineDashStyle.Dashes.Count);
  44. Assert.Equal(2, listBoxHierarchyLine.LineDashStyle.Dashes[0]);
  45. Assert.Equal(2, listBoxHierarchyLine.LineDashStyle.Dashes[1]);
  46. }
  47. }
  48. [Fact]
  49. public void Inline_ControlTemplate_Styled_Values_Are_Set_With_Style_Priority()
  50. {
  51. using (UnitTestApplication.Start(TestServices.StyledWindow))
  52. {
  53. var xaml = @"
  54. <Window xmlns='https://github.com/avaloniaui'
  55. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  56. <Button>
  57. <Button.Template>
  58. <ControlTemplate>
  59. <ContentPresenter Name='PART_ContentPresenter'
  60. Background='Red'/>
  61. </ControlTemplate>
  62. </Button.Template>
  63. </Button>
  64. </Window>";
  65. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  66. var button = (Button)window.Content;
  67. window.ApplyTemplate();
  68. button.ApplyTemplate();
  69. var presenter = (ContentPresenter)button.Presenter;
  70. Assert.Equal(Brushes.Red, presenter.Background);
  71. var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
  72. Assert.Equal(BindingPriority.Template, diagnostic.Priority);
  73. }
  74. }
  75. [Fact]
  76. public void Style_ControlTemplate_Styled_Values_Are_Set_With_Style_Priority()
  77. {
  78. using (UnitTestApplication.Start(TestServices.StyledWindow))
  79. {
  80. var xaml = @"
  81. <Window xmlns='https://github.com/avaloniaui'
  82. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  83. <Window.Styles>
  84. <Style Selector='Button'>
  85. <Setter Property='Template'>
  86. <ControlTemplate>
  87. <ContentPresenter Name='PART_ContentPresenter'
  88. Background='Red'/>
  89. </ControlTemplate>
  90. </Setter>
  91. </Style>
  92. </Window.Styles>
  93. <Button/>
  94. </Window>";
  95. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  96. var button = (Button)window.Content;
  97. window.ApplyTemplate();
  98. button.ApplyTemplate();
  99. var presenter = (ContentPresenter)button.Presenter;
  100. Assert.Equal(Brushes.Red, presenter.Background);
  101. var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
  102. Assert.Equal(BindingPriority.Template, diagnostic.Priority);
  103. }
  104. }
  105. [Fact]
  106. public void ControlTemplate_Attached_Values_Are_Set_With_Style_Priority()
  107. {
  108. using (UnitTestApplication.Start(TestServices.StyledWindow))
  109. {
  110. var xaml = @"
  111. <Window xmlns='https://github.com/avaloniaui'
  112. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  113. <Button>
  114. <Button.Template>
  115. <ControlTemplate>
  116. <ContentPresenter Name='PART_ContentPresenter'
  117. DockPanel.Dock='Top'/>
  118. </ControlTemplate>
  119. </Button.Template>
  120. </Button>
  121. </Window>";
  122. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  123. var button = (Button)window.Content;
  124. window.ApplyTemplate();
  125. button.ApplyTemplate();
  126. var presenter = (ContentPresenter)button.Presenter;
  127. Assert.Equal(Dock.Top, DockPanel.GetDock(presenter));
  128. var diagnostic = presenter.GetDiagnostic(DockPanel.DockProperty);
  129. Assert.Equal(BindingPriority.Template, diagnostic.Priority);
  130. }
  131. }
  132. [Fact]
  133. public void ControlTemplate_StaticResources_Are_Set_With_Style_Priority()
  134. {
  135. using (UnitTestApplication.Start(TestServices.StyledWindow))
  136. {
  137. var xaml = @"
  138. <Window xmlns='https://github.com/avaloniaui'
  139. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  140. <Window.Resources>
  141. <SolidColorBrush x:Key='red'>Red</SolidColorBrush>
  142. </Window.Resources>
  143. <Button Content='Foo'>
  144. <Button.Template>
  145. <ControlTemplate>
  146. <ContentPresenter Name='PART_ContentPresenter'
  147. Background='{StaticResource red}'/>
  148. </ControlTemplate>
  149. </Button.Template>
  150. </Button>
  151. </Window>";
  152. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  153. var button = (Button)window.Content;
  154. window.ApplyTemplate();
  155. button.ApplyTemplate();
  156. var presenter = (ContentPresenter)button.Presenter;
  157. Assert.Equal(Brushes.Red, presenter.Background);
  158. var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
  159. Assert.Equal(BindingPriority.Template, diagnostic.Priority);
  160. }
  161. }
  162. [Fact]
  163. public void ControlTemplate_DynamicResources_Are_Set_With_Style_Priority()
  164. {
  165. using (UnitTestApplication.Start(TestServices.StyledWindow))
  166. {
  167. var xaml = @"
  168. <Window xmlns='https://github.com/avaloniaui'
  169. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  170. <Window.Resources>
  171. <SolidColorBrush x:Key='red'>Red</SolidColorBrush>
  172. </Window.Resources>
  173. <Button Content='Foo'>
  174. <Button.Template>
  175. <ControlTemplate>
  176. <ContentPresenter Name='PART_ContentPresenter'
  177. Background='{DynamicResource red}'/>
  178. </ControlTemplate>
  179. </Button.Template>
  180. </Button>
  181. </Window>";
  182. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  183. var button = (Button)window.Content;
  184. window.ApplyTemplate();
  185. button.ApplyTemplate();
  186. var presenter = (ContentPresenter)button.Presenter;
  187. Assert.Equal(Brushes.Red, presenter.Background);
  188. var diagnostic = presenter.GetDiagnostic(Button.BackgroundProperty);
  189. Assert.Equal(BindingPriority.Template, diagnostic.Priority);
  190. }
  191. }
  192. [Fact]
  193. public void ControlTemplate_TemplateBindings_Are_Set_With_TemplatedParent_Priority()
  194. {
  195. using (UnitTestApplication.Start(TestServices.StyledWindow))
  196. {
  197. var xaml = @"
  198. <Window xmlns='https://github.com/avaloniaui'
  199. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  200. <Button Content='Foo'>
  201. <Button.Template>
  202. <ControlTemplate>
  203. <ContentPresenter Name='PART_ContentPresenter'
  204. Content='{TemplateBinding Content}'/>
  205. </ControlTemplate>
  206. </Button.Template>
  207. </Button>
  208. </Window>";
  209. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  210. var button = (Button)window.Content;
  211. window.ApplyTemplate();
  212. button.ApplyTemplate();
  213. var presenter = (ContentPresenter)button.Presenter;
  214. Assert.Equal("Foo", presenter.Content);
  215. var diagnostic = presenter.GetDiagnostic(ContentPresenter.ContentProperty);
  216. Assert.Equal(BindingPriority.Template, diagnostic.Priority);
  217. }
  218. }
  219. [Fact]
  220. public void ControlTemplate_With_Nested_Child_Is_Operational()
  221. {
  222. var xaml = @"
  223. <ControlTemplate xmlns='https://github.com/avaloniaui'>
  224. <ContentControl Name='parent'>
  225. <ContentControl Name='child' />
  226. </ContentControl>
  227. </ControlTemplate>
  228. ";
  229. var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
  230. var parent = (ContentControl)template.Build(new ContentControl()).Control;
  231. Assert.Equal("parent", parent.Name);
  232. var child = parent.Content as ContentControl;
  233. Assert.NotNull(child);
  234. Assert.Equal("child", child.Name);
  235. }
  236. [Fact]
  237. public void ControlTemplate_With_TargetType_Is_Operational()
  238. {
  239. var xaml = @"
  240. <ControlTemplate xmlns='https://github.com/avaloniaui'
  241. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  242. TargetType='{x:Type ContentControl}'>
  243. <ContentPresenter Content='{TemplateBinding Content}' />
  244. </ControlTemplate>
  245. ";
  246. var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
  247. Assert.Equal(typeof(ContentControl), template.TargetType);
  248. Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Control);
  249. }
  250. [Fact]
  251. public void ControlTemplate_With_Panel_Children_Are_Added()
  252. {
  253. var xaml = @"
  254. <ControlTemplate xmlns='https://github.com/avaloniaui'>
  255. <Panel Name='panel'>
  256. <ContentControl Name='Foo' />
  257. <ContentControl Name='Bar' />
  258. </Panel>
  259. </ControlTemplate>
  260. ";
  261. var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
  262. var panel = (Panel)template.Build(new ContentControl()).Control;
  263. Assert.Equal(2, panel.Children.Count);
  264. var foo = panel.Children[0];
  265. var bar = panel.Children[1];
  266. Assert.Equal("Foo", foo.Name);
  267. Assert.Equal("Bar", bar.Name);
  268. }
  269. }
  270. public class ListBoxHierarchyLine : Panel
  271. {
  272. public static readonly StyledProperty<DashStyle> LineDashStyleProperty =
  273. AvaloniaProperty.Register<ListBoxHierarchyLine, DashStyle>(nameof(LineDashStyle));
  274. public DashStyle LineDashStyle
  275. {
  276. get => GetValue(LineDashStyleProperty);
  277. set => SetValue(LineDashStyleProperty, value);
  278. }
  279. }
  280. }