StyleTests.cs 11 KB

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