StyleTests.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 Avalonia.Controls;
  4. using Avalonia.Markup.Xaml.Data;
  5. using Avalonia.Media;
  6. using Avalonia.Styling;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  10. {
  11. public class StyleTests
  12. {
  13. [Fact]
  14. public void Color_Can_Be_Added_To_Style_Resources()
  15. {
  16. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  17. {
  18. var xaml = @"
  19. <UserControl xmlns='https://github.com/avaloniaui'
  20. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  21. <UserControl.Styles>
  22. <Style>
  23. <Style.Resources>
  24. <Color x:Key='color'>#ff506070</Color>
  25. </Style.Resources>
  26. </Style>
  27. </UserControl.Styles>
  28. </UserControl>";
  29. var loader = new AvaloniaXamlLoader();
  30. var userControl = (UserControl)loader.Load(xaml);
  31. var color = (Color)((Style)userControl.Styles[0]).Resources["color"];
  32. Assert.Equal(0xff506070, color.ToUint32());
  33. }
  34. }
  35. [Fact]
  36. public void SolidColorBrush_Can_Be_Added_To_Style_Resources()
  37. {
  38. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  39. {
  40. var xaml = @"
  41. <UserControl xmlns='https://github.com/avaloniaui'
  42. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  43. <UserControl.Styles>
  44. <Style>
  45. <Style.Resources>
  46. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  47. </Style.Resources>
  48. </Style>
  49. </UserControl.Styles>
  50. </UserControl>";
  51. var loader = new AvaloniaXamlLoader();
  52. var userControl = (UserControl)loader.Load(xaml);
  53. var brush = (SolidColorBrush)((Style)userControl.Styles[0]).Resources["brush"];
  54. Assert.Equal(0xff506070, brush.Color.ToUint32());
  55. }
  56. }
  57. [Fact]
  58. public void StyleResource_Can_Be_Assigned_To_Property()
  59. {
  60. var xaml = @"
  61. <UserControl xmlns='https://github.com/avaloniaui'
  62. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  63. <UserControl.Styles>
  64. <Style>
  65. <Style.Resources>
  66. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  67. </Style.Resources>
  68. </Style>
  69. </UserControl.Styles>
  70. <Border Name='border' Background='{StyleResource brush}'/>
  71. </UserControl>";
  72. var loader = new AvaloniaXamlLoader();
  73. var userControl = (UserControl)loader.Load(xaml);
  74. var border = userControl.FindControl<Border>("border");
  75. DelayedBinding.ApplyBindings(border);
  76. var brush = (SolidColorBrush)border.Background;
  77. Assert.Equal(0xff506070, brush.Color.ToUint32());
  78. }
  79. [Fact]
  80. public void StyleResource_Can_Be_Assigned_To_Setter()
  81. {
  82. using (UnitTestApplication.Start(TestServices.StyledWindow))
  83. {
  84. var xaml = @"
  85. <Window xmlns='https://github.com/avaloniaui'
  86. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  87. <Window.Styles>
  88. <Style>
  89. <Style.Resources>
  90. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  91. </Style.Resources>
  92. </Style>
  93. <Style Selector='Button'>
  94. <Setter Property='Background' Value='{StyleResource brush}'/>
  95. </Style>
  96. </Window.Styles>
  97. <Button Name='button'/>
  98. </Window>";
  99. var loader = new AvaloniaXamlLoader();
  100. var window = (Window)loader.Load(xaml);
  101. var button = window.FindControl<Button>("button");
  102. var brush = (SolidColorBrush)button.Background;
  103. Assert.Equal(0xff506070, brush.Color.ToUint32());
  104. }
  105. }
  106. [Fact]
  107. public void StyleResource_Can_Be_Assigned_To_StyleResource_Property()
  108. {
  109. using (UnitTestApplication.Start(TestServices.StyledWindow))
  110. {
  111. var xaml = @"
  112. <Window xmlns='https://github.com/avaloniaui'
  113. xmlns:mut='https://github.com/avaloniaui/mutable'
  114. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  115. <Window.Styles>
  116. <Style>
  117. <Style.Resources>
  118. <Color x:Key='color'>#ff506070</Color>
  119. <mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
  120. </Style.Resources>
  121. </Style>
  122. </Window.Styles>
  123. <Button Name='button' Background='{StyleResource brush}'/>
  124. </Window>";
  125. var loader = new AvaloniaXamlLoader();
  126. var window = (Window)loader.Load(xaml);
  127. var brush = (Avalonia.Media.Mutable.SolidColorBrush)window.FindStyleResource("brush");
  128. var button = window.FindControl<Button>("button");
  129. DelayedBinding.ApplyBindings(button);
  130. var buttonBrush = (Avalonia.Media.Mutable.SolidColorBrush)button.Background;
  131. Assert.Equal(0xff506070, brush.Color.ToUint32());
  132. Assert.Equal(0xff506070, buttonBrush.Color.ToUint32());
  133. }
  134. }
  135. [Fact]
  136. public void StyleResource_Can_Be_Found_In_TopLevel_Styles()
  137. {
  138. var xaml = @"
  139. <Styles xmlns='https://github.com/avaloniaui'
  140. xmlns:mut='https://github.com/avaloniaui/mutable'
  141. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  142. <Style>
  143. <Style.Resources>
  144. <Color x:Key='color'>#ff506070</Color>
  145. <mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
  146. </Style.Resources>
  147. </Style>
  148. </Styles>";
  149. var loader = new AvaloniaXamlLoader();
  150. var styles = (Styles)loader.Load(xaml);
  151. var brush = (Avalonia.Media.Mutable.SolidColorBrush)styles.FindResource("brush");
  152. Assert.Equal(0xff506070, brush.Color.ToUint32());
  153. }
  154. [Fact]
  155. public void StyleResource_Can_Be_Found_In_Sibling_Styles()
  156. {
  157. var xaml = @"
  158. <Styles xmlns='https://github.com/avaloniaui'
  159. xmlns:mut='https://github.com/avaloniaui/mutable'
  160. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  161. <Style>
  162. <Style.Resources>
  163. <Color x:Key='color'>#ff506070</Color>
  164. </Style.Resources>
  165. </Style>
  166. <Style>
  167. <Style.Resources>
  168. <mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
  169. </Style.Resources>
  170. </Style>
  171. </Styles>";
  172. var loader = new AvaloniaXamlLoader();
  173. var styles = (Styles)loader.Load(xaml);
  174. var brush = (Avalonia.Media.Mutable.SolidColorBrush)styles.FindResource("brush");
  175. Assert.Equal(0xff506070, brush.Color.ToUint32());
  176. }
  177. [Fact(Skip = "TODO: Issue #492")]
  178. public void StyleResource_Can_Be_Found_Across_Xaml_Files()
  179. {
  180. using (UnitTestApplication.Start(TestServices.StyledWindow))
  181. {
  182. var xaml = @"
  183. <Window xmlns='https://github.com/avaloniaui'
  184. xmlns:mut='https://github.com/avaloniaui/mutable'
  185. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  186. <Window.Styles>
  187. <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
  188. <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style2.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
  189. </Window.Styles>
  190. <Border Name='border' Background='{StyleResource RedBrush}'/>
  191. </Window>";
  192. var loader = new AvaloniaXamlLoader();
  193. var window = (Window)loader.Load(xaml);
  194. var border = window.FindControl<Border>("border");
  195. var borderBrush = (Avalonia.Media.Mutable.SolidColorBrush)border.Background;
  196. Assert.NotNull(borderBrush);
  197. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  198. }
  199. }
  200. [Fact]
  201. public void Setter_Can_Contain_Template()
  202. {
  203. using (UnitTestApplication.Start(TestServices.StyledWindow))
  204. {
  205. var xaml = @"
  206. <Window xmlns='https://github.com/avaloniaui'
  207. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  208. <Window.Styles>
  209. <Style Selector='ContentControl'>
  210. <Setter Property='Content'>
  211. <Template>
  212. <TextBlock>Hello World!</TextBlock>
  213. </Template>
  214. </Setter>
  215. </Style>
  216. </Window.Styles>
  217. <ContentControl Name='target'/>
  218. </Window>";
  219. var loader = new AvaloniaXamlLoader();
  220. var window = (Window)loader.Load(xaml);
  221. var target = window.Find<ContentControl>("target");
  222. Assert.IsType<TextBlock>(target.Content);
  223. Assert.Equal("Hello World!", ((TextBlock)target.Content).Text);
  224. }
  225. }
  226. [Fact]
  227. public void Setter_Value_Is_Bound_Directly_If_The_Target_Type_Derives_From_ITemplate()
  228. {
  229. using (UnitTestApplication.Start(TestServices.StyledWindow))
  230. {
  231. var xaml = @"
  232. <Window xmlns='https://github.com/avaloniaui'
  233. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  234. <Window.Styles>
  235. <Style Selector=':is(Control)'>
  236. <Setter Property='FocusAdorner'>
  237. <FocusAdornerTemplate>
  238. <Rectangle Stroke='Black'
  239. StrokeThickness='1'
  240. StrokeDashArray='1,2'/>
  241. </FocusAdornerTemplate>
  242. </Setter>
  243. </Style>
  244. </Window.Styles>
  245. <TextBlock Name='target'/>
  246. </Window>";
  247. var loader = new AvaloniaXamlLoader();
  248. var window = (Window)loader.Load(xaml);
  249. var target = window.Find<TextBlock>("target");
  250. Assert.NotNull(target.FocusAdorner);
  251. }
  252. }
  253. }
  254. }