StyleIncludeTests.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using Avalonia.Styling;
  6. using Avalonia.Themes.Simple;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. namespace Avalonia.Markup.Xaml.UnitTests.Xaml;
  10. public class StyleIncludeTests
  11. {
  12. [Fact]
  13. public void StyleInclude_Is_Built()
  14. {
  15. using (UnitTestApplication.Start(TestServices.StyledWindow
  16. .With(theme: () => new Styles())))
  17. {
  18. var xaml = @"
  19. <ContentControl xmlns='https://github.com/avaloniaui'>
  20. <ContentControl.Styles>
  21. <StyleInclude Source='avares://Avalonia.Markup.Xaml.UnitTests/Xaml/Style1.xaml'/>
  22. </ContentControl.Styles>
  23. </ContentControl>";
  24. var window = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
  25. Assert.IsType<Style>(window.Styles[0]);
  26. }
  27. }
  28. [Fact]
  29. public void StyleInclude_Is_Built_Resources()
  30. {
  31. using (UnitTestApplication.Start(TestServices.StyledWindow
  32. .With(theme: () => new Styles())))
  33. {
  34. var xaml = @"
  35. <ContentControl xmlns='https://github.com/avaloniaui'
  36. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  37. <ContentControl.Resources>
  38. <StyleInclude x:Key='Include' Source='avares://Avalonia.Markup.Xaml.UnitTests/Xaml/Style1.xaml'/>
  39. </ContentControl.Resources>
  40. </ContentControl>";
  41. var contentControl = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
  42. Assert.IsType<Style>(contentControl.Resources["Include"]);
  43. }
  44. }
  45. [Fact]
  46. public void StyleInclude_Is_Resolved_With_Two_Files()
  47. {
  48. var documents = new[]
  49. {
  50. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Style.xaml"), @"
  51. <Style xmlns='https://github.com/avaloniaui'
  52. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  53. <Style.Resources>
  54. <Color x:Key='Red'>Red</Color>
  55. </Style.Resources>
  56. </Style>"),
  57. new RuntimeXamlLoaderDocument(@"
  58. <ContentControl xmlns='https://github.com/avaloniaui'
  59. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  60. <ContentControl.Resources>
  61. <StyleInclude x:Key='Include' Source='avares://Tests/Style.xaml'/>
  62. </ContentControl.Resources>
  63. </ContentControl>")
  64. };
  65. var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  66. var style = Assert.IsType<Style>(objects[0]);
  67. var contentControl = Assert.IsType<ContentControl>(objects[1]);
  68. Assert.IsType<Style>(contentControl.Resources["Include"]);
  69. }
  70. [Fact]
  71. public void Relative_Back_StyleInclude_Is_Resolved_With_Two_Files()
  72. {
  73. var documents = new[]
  74. {
  75. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Subfolder/Style.xaml"), @"
  76. <Style xmlns='https://github.com/avaloniaui'
  77. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  78. <Style.Resources>
  79. <Color x:Key='Red'>Red</Color>
  80. </Style.Resources>
  81. </Style>"),
  82. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Subfolder/Folder/Root.xaml"), @"
  83. <ContentControl xmlns='https://github.com/avaloniaui'
  84. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  85. <ContentControl.Resources>
  86. <StyleInclude x:Key='Include' Source='../Style.xaml'/>
  87. </ContentControl.Resources>
  88. </ContentControl>")
  89. };
  90. var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  91. var style = Assert.IsType<Style>(objects[0]);
  92. var contentControl = Assert.IsType<ContentControl>(objects[1]);
  93. Assert.IsType<Style>(contentControl.Resources["Include"]);
  94. }
  95. [Fact]
  96. public void Relative_Root_StyleInclude_Is_Resolved_With_Two_Files()
  97. {
  98. var documents = new[]
  99. {
  100. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Style.xaml"), @"
  101. <Style xmlns='https://github.com/avaloniaui'
  102. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  103. <Style.Resources>
  104. <Color x:Key='Red'>Red</Color>
  105. </Style.Resources>
  106. </Style>"),
  107. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @"
  108. <ContentControl xmlns='https://github.com/avaloniaui'
  109. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  110. <ContentControl.Resources>
  111. <StyleInclude x:Key='Include' Source='/Style.xaml'/>
  112. </ContentControl.Resources>
  113. </ContentControl>")
  114. };
  115. var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  116. var style = Assert.IsType<Style>(objects[0]);
  117. var contentControl = Assert.IsType<ContentControl>(objects[1]);
  118. Assert.IsType<Style>(contentControl.Resources["Include"]);
  119. }
  120. [Fact]
  121. public void Relative_StyleInclude_Is_Resolved_With_Two_Files()
  122. {
  123. var documents = new[]
  124. {
  125. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Style.xaml"), @"
  126. <Style xmlns='https://github.com/avaloniaui'
  127. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  128. <Style.Resources>
  129. <Color x:Key='Red'>Red</Color>
  130. </Style.Resources>
  131. </Style>"),
  132. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @"
  133. <ContentControl xmlns='https://github.com/avaloniaui'
  134. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  135. <ContentControl.Resources>
  136. <StyleInclude x:Key='Include' Source='Style.xaml'/>
  137. </ContentControl.Resources>
  138. </ContentControl>")
  139. };
  140. var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  141. var style = Assert.IsType<Style>(objects[0]);
  142. var contentControl = Assert.IsType<ContentControl>(objects[1]);
  143. Assert.IsType<Style>(contentControl.Resources["Include"]);
  144. }
  145. [Fact]
  146. public void Relative_Dot_Syntax__StyleInclude_Is_Resolved_With_Two_Files()
  147. {
  148. var documents = new[]
  149. {
  150. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Style.xaml"), @"
  151. <Style xmlns='https://github.com/avaloniaui'
  152. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  153. <Style.Resources>
  154. <Color x:Key='Red'>Red</Color>
  155. </Style.Resources>
  156. </Style>"),
  157. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @"
  158. <ContentControl xmlns='https://github.com/avaloniaui'
  159. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  160. <ContentControl.Resources>
  161. <StyleInclude x:Key='Include' Source='./Style.xaml'/>
  162. </ContentControl.Resources>
  163. </ContentControl>")
  164. };
  165. var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  166. var style = Assert.IsType<Style>(objects[0]);
  167. var contentControl = Assert.IsType<ContentControl>(objects[1]);
  168. Assert.IsType<Style>(contentControl.Resources["Include"]);
  169. }
  170. [Fact]
  171. public void NonLatin_StyleInclude_Is_Resolved_With_Two_Files()
  172. {
  173. var documents = new[]
  174. {
  175. new RuntimeXamlLoaderDocument(new Uri("avares://アセンブリ/スタイル.xaml"), @"
  176. <Style xmlns='https://github.com/avaloniaui'
  177. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  178. <Style.Resources>
  179. <Color x:Key='Red'>Red</Color>
  180. </Style.Resources>
  181. </Style>"),
  182. new RuntimeXamlLoaderDocument(@"
  183. <ContentControl xmlns='https://github.com/avaloniaui'
  184. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  185. <ContentControl.Resources>
  186. <StyleInclude x:Key='Include' Source='avares://アセンブリ/スタイル.xaml'/>
  187. </ContentControl.Resources>
  188. </ContentControl>")
  189. };
  190. var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  191. var style = Assert.IsType<Style>(objects[0]);
  192. var contentControl = Assert.IsType<ContentControl>(objects[1]);
  193. Assert.IsType<Style>(contentControl.Resources["Include"]);
  194. }
  195. [Fact]
  196. public void Missing_ResourceKey_In_StyleInclude_Does_Not_Cause_StackOverflow()
  197. {
  198. var documents = new[]
  199. {
  200. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Style.xaml"), @"
  201. <Style xmlns='https://github.com/avaloniaui'
  202. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  203. <Style.Resources>
  204. <StaticResource x:Key='brush' ResourceKey='missing' />
  205. </Style.Resources>
  206. </Style>"),
  207. new RuntimeXamlLoaderDocument(@"
  208. <ContentControl xmlns='https://github.com/avaloniaui'
  209. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  210. <ContentControl.Styles>
  211. <StyleInclude Source='avares://Tests/Style.xaml'/>
  212. </ContentControl.Styles>
  213. </ContentControl>")
  214. };
  215. try
  216. {
  217. _ = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  218. }
  219. catch (KeyNotFoundException)
  220. {
  221. }
  222. }
  223. [Fact]
  224. public void StyleInclude_Should_Be_Replaced_With_Direct_Call()
  225. {
  226. var control = (ContentControl)AvaloniaRuntimeXamlLoader.Load(@"
  227. <ContentControl xmlns='https://github.com/avaloniaui'
  228. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  229. xmlns:themes='clr-namespace:Avalonia.Themes.Simple;assembly=Avalonia.Themes.Simple'>
  230. <ContentControl.Styles>
  231. <themes:SimpleTheme />
  232. <StyleInclude Source='avares://Avalonia.Themes.Simple/SimpleTheme.xaml'/>
  233. </ContentControl.Styles>
  234. </ContentControl>");
  235. Assert.IsType<SimpleTheme>(control.Styles[0]);
  236. Assert.IsType<SimpleTheme>(control.Styles[1]);
  237. }
  238. }