StyleTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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:mut='https://github.com/avaloniaui/mutable'
  117. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  118. <Window.Styles>
  119. <Style>
  120. <Style.Resources>
  121. <Color x:Key='color'>#ff506070</Color>
  122. <mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
  123. </Style.Resources>
  124. </Style>
  125. </Window.Styles>
  126. <Button Name='button' Background='{StyleResource brush}'/>
  127. </Window>";
  128. var loader = new AvaloniaXamlLoader();
  129. var window = (Window)loader.Load(xaml);
  130. var brush = (Avalonia.Media.Mutable.SolidColorBrush)window.FindStyleResource("brush");
  131. var button = window.FindControl<Button>("button");
  132. DelayedBinding.ApplyBindings(button);
  133. var buttonBrush = (Avalonia.Media.Mutable.SolidColorBrush)button.Background;
  134. Assert.Equal(0xff506070, brush.Color.ToUint32());
  135. Assert.Equal(0xff506070, buttonBrush.Color.ToUint32());
  136. }
  137. }
  138. [Fact]
  139. public void StyleResource_Can_Be_Found_In_TopLevel_Styles()
  140. {
  141. var xaml = @"
  142. <Styles xmlns='https://github.com/avaloniaui'
  143. xmlns:mut='https://github.com/avaloniaui/mutable'
  144. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  145. <Style>
  146. <Style.Resources>
  147. <Color x:Key='color'>#ff506070</Color>
  148. <mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
  149. </Style.Resources>
  150. </Style>
  151. </Styles>";
  152. var loader = new AvaloniaXamlLoader();
  153. var styles = (Styles)loader.Load(xaml);
  154. var brush = (Avalonia.Media.Mutable.SolidColorBrush)styles.FindResource("brush");
  155. Assert.Equal(0xff506070, brush.Color.ToUint32());
  156. }
  157. [Fact]
  158. public void StyleResource_Can_Be_Found_In_Sibling_Styles()
  159. {
  160. var xaml = @"
  161. <Styles xmlns='https://github.com/avaloniaui'
  162. xmlns:mut='https://github.com/avaloniaui/mutable'
  163. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  164. <Style>
  165. <Style.Resources>
  166. <Color x:Key='color'>#ff506070</Color>
  167. </Style.Resources>
  168. </Style>
  169. <Style>
  170. <Style.Resources>
  171. <mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
  172. </Style.Resources>
  173. </Style>
  174. </Styles>";
  175. var loader = new AvaloniaXamlLoader();
  176. var styles = (Styles)loader.Load(xaml);
  177. var brush = (Avalonia.Media.Mutable.SolidColorBrush)styles.FindResource("brush");
  178. Assert.Equal(0xff506070, brush.Color.ToUint32());
  179. }
  180. [Fact(Skip = "TODO: Issue #492")]
  181. public void StyleResource_Can_Be_Found_Across_Xaml_Files()
  182. {
  183. using (UnitTestApplication.Start(TestServices.StyledWindow))
  184. {
  185. var xaml = @"
  186. <Window xmlns='https://github.com/avaloniaui'
  187. xmlns:mut='https://github.com/avaloniaui/mutable'
  188. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  189. <Window.Styles>
  190. <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
  191. <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style2.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
  192. </Window.Styles>
  193. <Border Name='border' Background='{StyleResource RedBrush}'/>
  194. </Window>";
  195. var loader = new AvaloniaXamlLoader();
  196. var window = (Window)loader.Load(xaml);
  197. var border = window.FindControl<Border>("border");
  198. var borderBrush = (Avalonia.Media.Mutable.SolidColorBrush)border.Background;
  199. Assert.NotNull(borderBrush);
  200. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  201. }
  202. }
  203. [Fact]
  204. public void StyleInclude_Is_Built()
  205. {
  206. using (UnitTestApplication.Start(TestServices.StyledWindow
  207. .With(theme: () => new Styles())))
  208. {
  209. var xaml = @"
  210. <ContentControl xmlns='https://github.com/avaloniaui'>
  211. <ContentControl.Styles>
  212. <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
  213. </ContentControl.Styles>
  214. </ContentControl>";
  215. var window = AvaloniaXamlLoader.Parse<ContentControl>(xaml);
  216. Assert.Equal(1, window.Styles.Count);
  217. var styleInclude = window.Styles[0] as StyleInclude;
  218. Assert.NotNull(styleInclude);
  219. Assert.NotNull(styleInclude.Source);
  220. Assert.NotNull(styleInclude.Loaded);
  221. }
  222. }
  223. [Fact]
  224. public void Setter_Can_Contain_Template()
  225. {
  226. using (UnitTestApplication.Start(TestServices.StyledWindow))
  227. {
  228. var xaml = @"
  229. <Window xmlns='https://github.com/avaloniaui'
  230. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  231. <Window.Styles>
  232. <Style Selector='ContentControl'>
  233. <Setter Property='Content'>
  234. <Template>
  235. <TextBlock>Hello World!</TextBlock>
  236. </Template>
  237. </Setter>
  238. </Style>
  239. </Window.Styles>
  240. <ContentControl Name='target'/>
  241. </Window>";
  242. var loader = new AvaloniaXamlLoader();
  243. var window = (Window)loader.Load(xaml);
  244. var target = window.Find<ContentControl>("target");
  245. Assert.IsType<TextBlock>(target.Content);
  246. Assert.Equal("Hello World!", ((TextBlock)target.Content).Text);
  247. }
  248. }
  249. [Fact]
  250. public void Setter_Value_Is_Bound_Directly_If_The_Target_Type_Derives_From_ITemplate()
  251. {
  252. using (UnitTestApplication.Start(TestServices.StyledWindow))
  253. {
  254. var xaml = @"
  255. <Window xmlns='https://github.com/avaloniaui'
  256. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  257. <Window.Styles>
  258. <Style Selector=':is(Control)'>
  259. <Setter Property='FocusAdorner'>
  260. <FocusAdornerTemplate>
  261. <Rectangle Stroke='Black'
  262. StrokeThickness='1'
  263. StrokeDashArray='1,2'/>
  264. </FocusAdornerTemplate>
  265. </Setter>
  266. </Style>
  267. </Window.Styles>
  268. <TextBlock Name='target'/>
  269. </Window>";
  270. var loader = new AvaloniaXamlLoader();
  271. var window = (Window)loader.Load(xaml);
  272. var target = window.Find<TextBlock>("target");
  273. Assert.NotNull(target.FocusAdorner);
  274. }
  275. }
  276. }
  277. }