BasicTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Markup.Xaml.Data;
  7. using Avalonia.Markup.Xaml.Styling;
  8. using Avalonia.Markup.Xaml.Templates;
  9. using Avalonia.Media;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using Xunit;
  13. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  14. {
  15. public class BasicTests
  16. {
  17. [Fact]
  18. public void Simple_Property_Is_Set()
  19. {
  20. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
  21. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  22. Assert.NotNull(target);
  23. Assert.Equal("Foo", target.Content);
  24. }
  25. [Fact]
  26. public void Default_Content_Property_Is_Set()
  27. {
  28. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui'>Foo</ContentControl>";
  29. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  30. Assert.NotNull(target);
  31. Assert.Equal("Foo", target.Content);
  32. }
  33. [Fact]
  34. public void ContentControl_ContentTemplate_Is_Functional()
  35. {
  36. var xaml =
  37. @"<ContentControl xmlns='https://github.com/avaloniaui'>
  38. <ContentControl.ContentTemplate>
  39. <DataTemplate>
  40. <TextBlock Text='Foo' />
  41. </DataTemplate>
  42. </ContentControl.ContentTemplate>
  43. </ContentControl>";
  44. var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  45. var target = contentControl.ContentTemplate;
  46. Assert.NotNull(target);
  47. var txt = (TextBlock)target.Build(null);
  48. Assert.Equal("Foo", txt.Text);
  49. }
  50. [Fact]
  51. public void Named_Control_Is_Added_To_NameScope_Simple()
  52. {
  53. var xaml = @"
  54. <UserControl xmlns='https://github.com/avaloniaui'>
  55. <Button Name='button'>Foo</Button>
  56. </UserControl>";
  57. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  58. var button = control.FindControl<Button>("button");
  59. Assert.Equal("Foo", button.Content);
  60. }
  61. [Fact]
  62. public void Named_x_Control_Is_Added_To_NameScope_Simple()
  63. {
  64. var xaml = @"
  65. <UserControl xmlns='https://github.com/avaloniaui'
  66. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  67. <Button x:Name='button'>Foo</Button>
  68. </UserControl>";
  69. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  70. var button = control.FindControl<Button>("button");
  71. Assert.Equal("Foo", button.Content);
  72. }
  73. [Fact]
  74. public void Standart_TypeConverter_Is_Used()
  75. {
  76. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Width='200.5' />";
  77. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  78. Assert.Equal(200.5, control.Width);
  79. }
  80. [Fact]
  81. public void Avalonia_TypeConverter_Is_Used()
  82. {
  83. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Background='White' />";
  84. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  85. var bk = control.Background;
  86. Assert.IsType<SolidColorBrush>(bk);
  87. Assert.Equal(Colors.White, (bk as SolidColorBrush).Color);
  88. }
  89. [Fact]
  90. public void Simple_Style_Is_Parsed()
  91. {
  92. var xaml = @"
  93. <Styles xmlns='https://github.com/avaloniaui'
  94. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  95. <Style Selector='TextBlock'>
  96. <Setter Property='Background' Value='White'/>
  97. <Setter Property='Width' Value='100'/>
  98. </Style>
  99. </Styles>";
  100. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  101. Assert.Equal(1, styles.Count);
  102. var style = (Style)styles[0];
  103. var setters = style.Setters.Cast<Setter>().ToArray();
  104. Assert.Equal(2, setters.Length);
  105. Assert.Equal(TextBlock.BackgroundProperty, setters[0].Property);
  106. Assert.Equal(Brushes.White, setters[0].Value);
  107. Assert.Equal(TextBlock.WidthProperty, setters[1].Property);
  108. Assert.Equal(100.0, setters[1].Value);
  109. }
  110. [Fact]
  111. public void Style_Resources_Are_Build()
  112. {
  113. var xaml = @"
  114. <Style xmlns='https://github.com/avaloniaui'
  115. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  116. xmlns:sys='clr-namespace:System;assembly=mscorlib'>
  117. <Style.Resources>
  118. <SolidColorBrush x:Key='Brush'>White</SolidColorBrush>
  119. <sys:Double x:Key='Double'>10</sys:Double>
  120. </Style.Resources>
  121. </Style>";
  122. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  123. Assert.True(style.Resources.Count > 0);
  124. var brush = style.FindResource("Brush") as SolidColorBrush;
  125. Assert.NotNull(brush);
  126. Assert.Equal(Colors.White, brush.Color);
  127. var d = (double)style.FindResource("Double");
  128. Assert.Equal(10.0, d);
  129. }
  130. [Fact]
  131. public void StyleInclude_Is_Build()
  132. {
  133. using (UnitTestApplication.Start(TestServices.StyledWindow))
  134. {
  135. var xaml = @"
  136. <Styles xmlns='https://github.com/avaloniaui'
  137. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  138. <StyleInclude Source='resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default'/>
  139. </Styles>";
  140. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  141. Assert.True(styles.Count == 1);
  142. var styleInclude = styles.First() as StyleInclude;
  143. Assert.NotNull(styleInclude);
  144. var style = styleInclude.Loaded;
  145. Assert.NotNull(style);
  146. }
  147. }
  148. [Fact]
  149. public void Simple_Xaml_Binding_Is_Operational()
  150. {
  151. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper
  152. .With(windowingPlatform: new MockWindowingPlatform())))
  153. {
  154. var xaml =
  155. @"<Window xmlns='https://github.com/avaloniaui' Content='{Binding}'/>";
  156. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  157. Assert.Null(target.Content);
  158. target.DataContext = "Foo";
  159. Assert.Equal("Foo", target.Content);
  160. }
  161. }
  162. [Fact]
  163. public void Control_Template_Is_Operational()
  164. {
  165. var xaml = @"
  166. <ContentControl xmlns='https://github.com/avaloniaui'
  167. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  168. <ContentControl.Template>
  169. <ControlTemplate>
  170. <ContentPresenter Name='PART_ContentPresenter'
  171. Content='{TemplateBinding Content}'/>
  172. </ControlTemplate>
  173. </ContentControl.Template>
  174. </ContentControl>";
  175. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  176. Assert.NotNull(target.Template);
  177. Assert.Null(target.Presenter);
  178. target.ApplyTemplate();
  179. Assert.NotNull(target.Presenter);
  180. target.Content = "Foo";
  181. Assert.Equal("Foo", target.Presenter.Content);
  182. }
  183. [Fact]
  184. public void Style_ControlTemplate_Is_Build()
  185. {
  186. var xaml = @"
  187. <Style xmlns='https://github.com/avaloniaui' Selector='ContentControl'>
  188. <Setter Property='Template'>
  189. <ControlTemplate>
  190. <ContentPresenter Name='PART_ContentPresenter'
  191. Background='{TemplateBinding Background}'
  192. BorderBrush='{TemplateBinding BorderBrush}'
  193. BorderThickness='{TemplateBinding BorderThickness}'
  194. Content='{TemplateBinding Content}'
  195. ContentTemplate='{TemplateBinding ContentTemplate}'
  196. Padding='{TemplateBinding Padding}' />
  197. </ControlTemplate>
  198. </Setter>
  199. </Style> ";
  200. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  201. Assert.Equal(1, style.Setters.Count());
  202. var setter = (Setter)style.Setters.First();
  203. Assert.Equal(ContentControl.TemplateProperty, setter.Property);
  204. Assert.IsType<ControlTemplate>(setter.Value);
  205. var template = (ControlTemplate)setter.Value;
  206. var control = new ContentControl();
  207. var result = (ContentPresenter)template.Build(control);
  208. Assert.NotNull(result);
  209. }
  210. [Fact]
  211. public void Named_Control_Is_Added_To_NameScope()
  212. {
  213. using (UnitTestApplication.Start(TestServices.StyledWindow))
  214. {
  215. var xaml = @"
  216. <Window xmlns='https://github.com/avaloniaui'
  217. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  218. <Button Name='button'>Foo</Button>
  219. </Window>";
  220. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  221. var button = window.FindControl<Button>("button");
  222. Assert.Equal("Foo", button.Content);
  223. }
  224. }
  225. [Fact]
  226. public void Control_Is_Added_To_Parent_Before_Properties_Are_Set()
  227. {
  228. using (UnitTestApplication.Start(TestServices.StyledWindow))
  229. {
  230. var xaml = @"
  231. <Window xmlns='https://github.com/avaloniaui'
  232. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  233. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  234. <local:InitializationOrderTracker Width='100'/>
  235. </Window>";
  236. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  237. var tracker = (InitializationOrderTracker)window.Content;
  238. var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
  239. var widthChanged = tracker.Order.IndexOf("Property Width Changed");
  240. Assert.NotEqual(-1, attached);
  241. Assert.NotEqual(-1, widthChanged);
  242. Assert.True(attached < widthChanged);
  243. }
  244. }
  245. }
  246. }