BasicTests.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.Markup.Xaml.Styling;
  6. using Avalonia.Media;
  7. using Avalonia.Styling;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  11. {
  12. public class BasicTests
  13. {
  14. [Fact]
  15. public void Simple_Property_Is_Set()
  16. {
  17. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui' Content='Foo'/>";
  18. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  19. Assert.NotNull(target);
  20. Assert.Equal("Foo", target.Content);
  21. }
  22. [Fact]
  23. public void Default_Content_Property_Is_Set()
  24. {
  25. var xaml = @"<ContentControl xmlns='https://github.com/avaloniaui'>Foo</ContentControl>";
  26. var target = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  27. Assert.NotNull(target);
  28. Assert.Equal("Foo", target.Content);
  29. }
  30. [Fact]
  31. public void ContentControl_ContentTemplate_Is_Functional()
  32. {
  33. var xaml =
  34. @"<ContentControl xmlns='https://github.com/avaloniaui'>
  35. <ContentControl.ContentTemplate>
  36. <DataTemplate>
  37. <TextBlock Text='Foo' />
  38. </DataTemplate>
  39. </ContentControl.ContentTemplate>
  40. </ContentControl>";
  41. var contentControl = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  42. var target = contentControl.ContentTemplate;
  43. Assert.NotNull(target);
  44. var txt = (TextBlock)target.Build(null);
  45. Assert.Equal("Foo", txt.Text);
  46. }
  47. [Fact]
  48. public void Named_Control_Is_Added_To_NameScope_Simple()
  49. {
  50. var xaml = @"
  51. <UserControl xmlns='https://github.com/avaloniaui'>
  52. <Button Name='button'>Foo</Button>
  53. </UserControl>";
  54. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  55. var button = control.FindControl<Button>("button");
  56. Assert.Equal("Foo", button.Content);
  57. }
  58. [Fact]
  59. public void Named_x_Control_Is_Added_To_NameScope_Simple()
  60. {
  61. var xaml = @"
  62. <UserControl xmlns='https://github.com/avaloniaui'
  63. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  64. <Button x:Name='button'>Foo</Button>
  65. </UserControl>";
  66. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  67. var button = control.FindControl<Button>("button");
  68. Assert.Equal("Foo", button.Content);
  69. }
  70. [Fact]
  71. public void Standart_TypeConverter_Is_Used()
  72. {
  73. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Width='200.5' />";
  74. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  75. Assert.Equal(200.5, control.Width);
  76. }
  77. [Fact]
  78. public void Avalonia_TypeConverter_Is_Used()
  79. {
  80. var xaml = @"<UserControl xmlns='https://github.com/avaloniaui' Background='White' />";
  81. var control = AvaloniaXamlLoader.Parse<UserControl>(xaml);
  82. var bk = control.Background;
  83. Assert.IsType<SolidColorBrush>(bk);
  84. Assert.Equal(Colors.White, (bk as SolidColorBrush).Color);
  85. }
  86. [Fact]
  87. public void Simple_Style_Is_Parsed()
  88. {
  89. var xaml = @"
  90. <Styles xmlns='https://github.com/avaloniaui'
  91. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  92. <Style Selector='TextBlock'>
  93. <Setter Property='Background' Value='White'/>
  94. <Setter Property='Width' Value='100'/>
  95. </Style>
  96. </Styles>";
  97. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  98. Assert.Equal(1, styles.Count);
  99. var style = (Style)styles[0];
  100. var setters = style.Setters.Cast<Setter>().ToArray();
  101. Assert.Equal(2, setters.Length);
  102. Assert.Equal(TextBlock.BackgroundProperty, setters[0].Property);
  103. Assert.Equal(Brushes.White, setters[0].Value);
  104. Assert.Equal(TextBlock.WidthProperty, setters[1].Property);
  105. Assert.Equal(100.0, setters[1].Value);
  106. }
  107. [Fact]
  108. public void Style_Resources_Are_Build()
  109. {
  110. var xaml = @"
  111. <Style xmlns='https://github.com/avaloniaui'
  112. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  113. xmlns:sys='clr-namespace:System;assembly=mscorlib'>
  114. <Style.Resources>
  115. <SolidColorBrush x:Key='Brush'>White</SolidColorBrush>
  116. <sys:Double x:Key='Double'>10</sys:Double>
  117. </Style.Resources>
  118. </Style>";
  119. var style = AvaloniaXamlLoader.Parse<Style>(xaml);
  120. Assert.True(style.Resources.Count > 0);
  121. var brush = style.FindResource("Brush") as SolidColorBrush;
  122. Assert.NotNull(brush);
  123. Assert.Equal(Colors.White, brush.Color);
  124. var d = (double)style.FindResource("Double");
  125. Assert.Equal(10.0, d);
  126. }
  127. [Fact]
  128. public void StyleInclude_Is_Build()
  129. {
  130. using (UnitTestApplication.Start(TestServices.StyledWindow))
  131. {
  132. var xaml = @"
  133. <Styles xmlns='https://github.com/avaloniaui'
  134. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  135. <StyleInclude Source='resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default'/>
  136. </Styles>";
  137. var styles = AvaloniaXamlLoader.Parse<Styles>(xaml);
  138. Assert.True(styles.Count == 1);
  139. var styleInclude = styles.First() as StyleInclude;
  140. Assert.NotNull(styleInclude);
  141. var style = styleInclude.Loaded;
  142. Assert.NotNull(style);
  143. }
  144. }
  145. [Fact]
  146. public void Named_Control_Is_Added_To_NameScope()
  147. {
  148. using (UnitTestApplication.Start(TestServices.StyledWindow))
  149. {
  150. var xaml = @"
  151. <Window xmlns='https://github.com/avaloniaui'
  152. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  153. <Button Name='button'>Foo</Button>
  154. </Window>";
  155. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  156. var button = window.FindControl<Button>("button");
  157. Assert.Equal("Foo", button.Content);
  158. }
  159. }
  160. [Fact]
  161. public void Control_Is_Added_To_Parent_Before_Properties_Are_Set()
  162. {
  163. using (UnitTestApplication.Start(TestServices.StyledWindow))
  164. {
  165. var xaml = @"
  166. <Window xmlns='https://github.com/avaloniaui'
  167. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  168. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  169. <local:InitializationOrderTracker Width='100'/>
  170. </Window>";
  171. var window = AvaloniaXamlLoader.Parse<Window>(xaml);
  172. var tracker = (InitializationOrderTracker)window.Content;
  173. var attached = tracker.Order.IndexOf("AttachedToLogicalTree");
  174. var widthChanged = tracker.Order.IndexOf("Property Width Changed");
  175. Assert.NotEqual(-1, attached);
  176. Assert.NotEqual(-1, widthChanged);
  177. Assert.True(attached < widthChanged);
  178. }
  179. }
  180. }
  181. }