DynamicResourceExtensionTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Markup.Data;
  8. using Avalonia.Media;
  9. using Avalonia.Styling;
  10. using Avalonia.UnitTests;
  11. using Avalonia.VisualTree;
  12. using Xunit;
  13. namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
  14. {
  15. public class DynamicResourceExtensionTests : XamlTestBase
  16. {
  17. [Fact]
  18. public void DynamicResource_Can_Be_Assigned_To_Property()
  19. {
  20. var xaml = @"
  21. <UserControl xmlns='https://github.com/avaloniaui'
  22. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  23. <UserControl.Resources>
  24. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  25. </UserControl.Resources>
  26. <Border Name='border' Background='{DynamicResource brush}'/>
  27. </UserControl>";
  28. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  29. var border = userControl.FindControl<Border>("border");
  30. DelayedBinding.ApplyBindings(border);
  31. var brush = (SolidColorBrush)border.Background;
  32. Assert.Equal(0xff506070, brush.Color.ToUint32());
  33. }
  34. [Fact]
  35. public void DynamicResource_Can_Be_Assigned_To_Attached_Property()
  36. {
  37. var xaml = @"
  38. <UserControl xmlns='https://github.com/avaloniaui'
  39. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  40. <UserControl.Resources>
  41. <x:Int32 x:Key='col'>5</x:Int32>
  42. </UserControl.Resources>
  43. <Border Name='border' Grid.Column='{DynamicResource col}'/>
  44. </UserControl>";
  45. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  46. var border = userControl.FindControl<Border>("border");
  47. DelayedBinding.ApplyBindings(border);
  48. Assert.Equal(5, Grid.GetColumn(border));
  49. }
  50. [Fact]
  51. public void DynamicResource_From_Style_Can_Be_Assigned_To_Property()
  52. {
  53. var xaml = @"
  54. <UserControl xmlns='https://github.com/avaloniaui'
  55. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  56. <UserControl.Styles>
  57. <Style>
  58. <Style.Resources>
  59. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  60. </Style.Resources>
  61. </Style>
  62. </UserControl.Styles>
  63. <Border Name='border' Background='{DynamicResource brush}'/>
  64. </UserControl>";
  65. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  66. var border = userControl.FindControl<Border>("border");
  67. DelayedBinding.ApplyBindings(border);
  68. var brush = (SolidColorBrush)border.Background;
  69. Assert.Equal(0xff506070, brush.Color.ToUint32());
  70. }
  71. [Fact]
  72. public void DynamicResource_From_MergedDictionary_Can_Be_Assigned_To_Property()
  73. {
  74. var xaml = @"
  75. <UserControl xmlns='https://github.com/avaloniaui'
  76. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  77. <UserControl.Resources>
  78. <ResourceDictionary>
  79. <ResourceDictionary.MergedDictionaries>
  80. <ResourceDictionary>
  81. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  82. </ResourceDictionary>
  83. </ResourceDictionary.MergedDictionaries>
  84. </ResourceDictionary>
  85. </UserControl.Resources>
  86. <Border Name='border' Background='{DynamicResource brush}'/>
  87. </UserControl>";
  88. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  89. var border = userControl.FindControl<Border>("border");
  90. DelayedBinding.ApplyBindings(border);
  91. var brush = (SolidColorBrush)border.Background;
  92. Assert.Equal(0xff506070, brush.Color.ToUint32());
  93. }
  94. [Fact]
  95. public void DynamicResource_From_MergedDictionary_In_Style_Can_Be_Assigned_To_Property()
  96. {
  97. var xaml = @"
  98. <UserControl xmlns='https://github.com/avaloniaui'
  99. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  100. <UserControl.Styles>
  101. <Style>
  102. <Style.Resources>
  103. <ResourceDictionary>
  104. <ResourceDictionary.MergedDictionaries>
  105. <ResourceDictionary>
  106. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  107. </ResourceDictionary>
  108. </ResourceDictionary.MergedDictionaries>
  109. </ResourceDictionary>
  110. </Style.Resources>
  111. </Style>
  112. </UserControl.Styles>
  113. <Border Name='border' Background='{DynamicResource brush}'/>
  114. </UserControl>";
  115. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  116. var border = userControl.FindControl<Border>("border");
  117. DelayedBinding.ApplyBindings(border);
  118. var brush = (SolidColorBrush)border.Background;
  119. Assert.Equal(0xff506070, brush.Color.ToUint32());
  120. }
  121. [Fact]
  122. public void DynamicResource_From_Application_Can_Be_Assigned_To_Property_In_Window()
  123. {
  124. using (StyledWindow())
  125. {
  126. Application.Current.Resources.Add("brush", new SolidColorBrush(0xff506070));
  127. var xaml = @"
  128. <Window xmlns='https://github.com/avaloniaui'
  129. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  130. <Border Name='border' Background='{DynamicResource brush}'/>
  131. </Window>";
  132. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  133. var border = window.FindControl<Border>("border");
  134. var brush = (SolidColorBrush)border.Background;
  135. Assert.Equal(0xff506070, brush.Color.ToUint32());
  136. }
  137. }
  138. [Fact]
  139. public void DynamicResource_From_Application_Can_Be_Assigned_To_Property_In_UserControl()
  140. {
  141. using (UnitTestApplication.Start(TestServices.StyledWindow))
  142. {
  143. Application.Current.Resources.Add("brush", new SolidColorBrush(0xff506070));
  144. var xaml = @"
  145. <UserControl xmlns='https://github.com/avaloniaui'
  146. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  147. <Border Name='border' Background='{DynamicResource brush}'/>
  148. </UserControl>";
  149. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  150. var border = userControl.FindControl<Border>("border");
  151. // We don't actually know where the global styles are until we attach the control
  152. // to a window, as Window has StylingParent set to Application.
  153. var window = new Window { Content = userControl };
  154. window.Show();
  155. var brush = (SolidColorBrush)border.Background;
  156. Assert.Equal(0xff506070, brush.Color.ToUint32());
  157. }
  158. }
  159. [Fact]
  160. public void DynamicResource_Can_Be_Assigned_To_Setter()
  161. {
  162. using (StyledWindow())
  163. {
  164. var xaml = @"
  165. <Window xmlns='https://github.com/avaloniaui'
  166. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  167. <Window.Resources>
  168. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  169. </Window.Resources>
  170. <Window.Styles>
  171. <Style Selector='Button'>
  172. <Setter Property='Background' Value='{DynamicResource brush}'/>
  173. </Style>
  174. </Window.Styles>
  175. <Button Name='button'/>
  176. </Window>";
  177. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  178. var button = window.FindControl<Button>("button");
  179. var brush = (SolidColorBrush)button.Background;
  180. Assert.Equal(0xff506070, brush.Color.ToUint32());
  181. }
  182. }
  183. [Fact]
  184. public void DynamicResource_From_Style_Can_Be_Assigned_To_Setter()
  185. {
  186. using (StyledWindow())
  187. {
  188. var xaml = @"
  189. <Window xmlns='https://github.com/avaloniaui'
  190. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  191. <Window.Styles>
  192. <Style>
  193. <Style.Resources>
  194. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  195. </Style.Resources>
  196. </Style>
  197. <Style Selector='Button'>
  198. <Setter Property='Background' Value='{DynamicResource brush}'/>
  199. </Style>
  200. </Window.Styles>
  201. <Button Name='button'/>
  202. </Window>";
  203. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  204. var button = window.FindControl<Button>("button");
  205. var brush = (SolidColorBrush)button.Background;
  206. Assert.Equal(0xff506070, brush.Color.ToUint32());
  207. }
  208. }
  209. [Fact]
  210. public void DynamicResource_Can_Be_Assigned_To_Setter_In_Styles_File()
  211. {
  212. var styleXaml = @"
  213. <Styles xmlns='https://github.com/avaloniaui'
  214. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  215. <Styles.Resources>
  216. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  217. </Styles.Resources>
  218. <Style Selector='Border'>
  219. <Setter Property='Background' Value='{DynamicResource brush}'/>
  220. </Style>
  221. </Styles>";
  222. using (StyledWindow(assets: ("test:style.xaml", styleXaml)))
  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. <StyleInclude Source='test:style.xaml'/>
  229. </Window.Styles>
  230. <Border Name='border'/>
  231. </Window>";
  232. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  233. var border = window.FindControl<Border>("border");
  234. var brush = (SolidColorBrush)border.Background;
  235. Assert.Equal(0xff506070, brush.Color.ToUint32());
  236. }
  237. }
  238. [Fact]
  239. public void DynamicResource_Can_Be_Assigned_To_Property_In_ControlTemplate_In_Styles_File()
  240. {
  241. var styleXaml = @"
  242. <Styles xmlns='https://github.com/avaloniaui'
  243. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  244. <Styles.Resources>
  245. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  246. </Styles.Resources>
  247. <Style Selector='Button'>
  248. <Setter Property='Template'>
  249. <ControlTemplate>
  250. <Border Name='border' Background='{DynamicResource brush}'/>
  251. </ControlTemplate>
  252. </Setter>
  253. </Style>
  254. </Styles>";
  255. using (StyledWindow(assets: ("test:style.xaml", styleXaml)))
  256. {
  257. var xaml = @"
  258. <Window xmlns='https://github.com/avaloniaui'
  259. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  260. <Window.Styles>
  261. <StyleInclude Source='test:style.xaml'/>
  262. </Window.Styles>
  263. <Button Name='button'/>
  264. </Window>";
  265. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  266. var button = window.FindControl<Button>("button");
  267. window.Show();
  268. var border = (Border)button.GetVisualChildren().Single();
  269. var brush = (SolidColorBrush)border.Background;
  270. Assert.Equal(0xff506070, brush.Color.ToUint32());
  271. }
  272. }
  273. [Fact]
  274. public void DynamicResource_Can_Be_Assigned_To_Resource_Property()
  275. {
  276. var xaml = @"
  277. <UserControl xmlns='https://github.com/avaloniaui'
  278. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  279. <UserControl.Resources>
  280. <Color x:Key='color'>#ff506070</Color>
  281. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  282. </UserControl.Resources>
  283. <Border Name='border' Background='{DynamicResource brush}'/>
  284. </UserControl>";
  285. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  286. var border = userControl.FindControl<Border>("border");
  287. DelayedBinding.ApplyBindings(border);
  288. var brush = (SolidColorBrush)border.Background;
  289. Assert.Equal(0xff506070, brush.Color.ToUint32());
  290. }
  291. [Fact]
  292. public void DynamicResource_Can_Be_Assigned_To_Resource_Property_In_Application()
  293. {
  294. var xaml = @"
  295. <Application xmlns='https://github.com/avaloniaui'
  296. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  297. <Application.Resources>
  298. <Color x:Key='color'>#ff506070</Color>
  299. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  300. </Application.Resources>
  301. </Application>";
  302. var application = (Application)AvaloniaRuntimeXamlLoader.Load(xaml);
  303. var brush = (SolidColorBrush)application.Resources["brush"];
  304. Assert.Equal(0xff506070, brush.Color.ToUint32());
  305. }
  306. [Fact]
  307. public void DynamicResource_Can_Be_Assigned_To_ItemTemplate_Property()
  308. {
  309. var xaml = @"
  310. <UserControl xmlns='https://github.com/avaloniaui'
  311. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  312. <UserControl.Resources>
  313. <DataTemplate x:Key='PurpleData'>
  314. <TextBlock Text='{Binding Name}' Background='Purple'/>
  315. </DataTemplate>
  316. </UserControl.Resources>
  317. <ListBox Name='listBox' ItemTemplate='{DynamicResource PurpleData}'/>
  318. </UserControl>";
  319. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  320. var listBox = userControl.FindControl<ListBox>("listBox");
  321. DelayedBinding.ApplyBindings(listBox);
  322. Assert.NotNull(listBox.ItemTemplate);
  323. }
  324. [Fact]
  325. public void DynamicResource_Tracks_Added_Resource()
  326. {
  327. var xaml = @"
  328. <UserControl xmlns='https://github.com/avaloniaui'
  329. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  330. <Border Name='border' Background='{DynamicResource brush}'/>
  331. </UserControl>";
  332. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  333. var border = userControl.FindControl<Border>("border");
  334. DelayedBinding.ApplyBindings(border);
  335. Assert.Null(border.Background);
  336. userControl.Resources.Add("brush", new SolidColorBrush(0xff506070));
  337. var brush = (SolidColorBrush)border.Background;
  338. Assert.NotNull(brush);
  339. Assert.Equal(0xff506070, brush.Color.ToUint32());
  340. }
  341. [Fact]
  342. public void DynamicResource_Tracks_Added_Style_Resource()
  343. {
  344. var xaml = @"
  345. <UserControl xmlns='https://github.com/avaloniaui'
  346. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  347. <Border Name='border' Background='{DynamicResource brush}'/>
  348. </UserControl>";
  349. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  350. var border = userControl.FindControl<Border>("border");
  351. DelayedBinding.ApplyBindings(border);
  352. Assert.Null(border.Background);
  353. userControl.Styles.Resources.Add("brush", new SolidColorBrush(0xff506070));
  354. var brush = (SolidColorBrush)border.Background;
  355. Assert.NotNull(brush);
  356. Assert.Equal(0xff506070, brush.Color.ToUint32());
  357. }
  358. [Fact]
  359. public void DynamicResource_Tracks_Added_Nested_Style_Resource()
  360. {
  361. var xaml = @"
  362. <UserControl xmlns='https://github.com/avaloniaui'
  363. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  364. <UserControl.Styles>
  365. <Style>
  366. </Style>
  367. </UserControl.Styles>
  368. <Border Name='border' Background='{DynamicResource brush}'/>
  369. </UserControl>";
  370. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  371. var border = userControl.FindControl<Border>("border");
  372. DelayedBinding.ApplyBindings(border);
  373. Assert.Null(border.Background);
  374. ((Style)userControl.Styles[0]).Resources.Add("brush", new SolidColorBrush(0xff506070));
  375. var brush = (SolidColorBrush)border.Background;
  376. Assert.NotNull(brush);
  377. Assert.Equal(0xff506070, brush.Color.ToUint32());
  378. }
  379. [Fact]
  380. public void DynamicResource_Tracks_Added_MergedResource()
  381. {
  382. var xaml = @"
  383. <UserControl xmlns='https://github.com/avaloniaui'
  384. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  385. <UserControl.Resources>
  386. <ResourceDictionary>
  387. <ResourceDictionary.MergedDictionaries>
  388. <ResourceDictionary/>
  389. </ResourceDictionary.MergedDictionaries>
  390. </ResourceDictionary>
  391. </UserControl.Resources>
  392. <Border Name='border' Background='{DynamicResource brush}'/>
  393. </UserControl>";
  394. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  395. var border = userControl.FindControl<Border>("border");
  396. DelayedBinding.ApplyBindings(border);
  397. Assert.Null(border.Background);
  398. ((IResourceDictionary)userControl.Resources.MergedDictionaries[0]).Add("brush", new SolidColorBrush(0xff506070));
  399. var brush = (SolidColorBrush)border.Background;
  400. Assert.NotNull(brush);
  401. Assert.Equal(0xff506070, brush.Color.ToUint32());
  402. }
  403. [Fact]
  404. public void DynamicResource_Tracks_Added_MergedResource_Dictionary()
  405. {
  406. var xaml = @"
  407. <UserControl xmlns='https://github.com/avaloniaui'
  408. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  409. <Border Name='border' Background='{DynamicResource brush}'/>
  410. </UserControl>";
  411. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  412. var border = userControl.FindControl<Border>("border");
  413. DelayedBinding.ApplyBindings(border);
  414. Assert.Null(border.Background);
  415. var dictionary = new ResourceDictionary
  416. {
  417. { "brush", new SolidColorBrush(0xff506070) },
  418. };
  419. userControl.Resources.MergedDictionaries.Add(dictionary);
  420. var brush = (SolidColorBrush)border.Background;
  421. Assert.NotNull(brush);
  422. Assert.Equal(0xff506070, brush.Color.ToUint32());
  423. }
  424. [Fact]
  425. public void DynamicResource_Tracks_Added_Style_MergedResource_Dictionary()
  426. {
  427. var xaml = @"
  428. <UserControl xmlns='https://github.com/avaloniaui'
  429. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  430. <UserControl.Styles>
  431. <Style>
  432. </Style>
  433. </UserControl.Styles>
  434. <Border Name='border' Background='{DynamicResource brush}'/>
  435. </UserControl>";
  436. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  437. var border = userControl.FindControl<Border>("border");
  438. DelayedBinding.ApplyBindings(border);
  439. Assert.Null(border.Background);
  440. var dictionary = new ResourceDictionary
  441. {
  442. { "brush", new SolidColorBrush(0xff506070) },
  443. };
  444. ((Style)userControl.Styles[0]).Resources.MergedDictionaries.Add(dictionary);
  445. var brush = (SolidColorBrush)border.Background;
  446. Assert.NotNull(brush);
  447. Assert.Equal(0xff506070, brush.Color.ToUint32());
  448. }
  449. [Fact]
  450. public void DynamicResource_Can_Be_Found_Across_Xaml_Style_Files()
  451. {
  452. var style1Xaml = @"
  453. <Style xmlns='https://github.com/avaloniaui'
  454. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  455. <Style.Resources>
  456. <Color x:Key='Red'>Red</Color>
  457. </Style.Resources>
  458. </Style>";
  459. var style2Xaml = @"
  460. <Style xmlns='https://github.com/avaloniaui'
  461. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  462. <Style.Resources>
  463. <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/>
  464. </Style.Resources>
  465. </Style>";
  466. using (StyledWindow(
  467. ("test:style1.xaml", style1Xaml),
  468. ("test:style2.xaml", style2Xaml)))
  469. {
  470. var xaml = @"
  471. <Window xmlns='https://github.com/avaloniaui'
  472. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  473. <Window.Styles>
  474. <StyleInclude Source='test:style1.xaml'/>
  475. <StyleInclude Source='test:style2.xaml'/>
  476. </Window.Styles>
  477. <Border Name='border' Background='{DynamicResource RedBrush}'/>
  478. </Window>";
  479. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  480. var border = window.FindControl<Border>("border");
  481. var borderBrush = (ISolidColorBrush)border.Background;
  482. Assert.NotNull(borderBrush);
  483. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  484. }
  485. }
  486. [Fact]
  487. public void DynamicResource_Can_Be_Found_In_Nested_Style_File()
  488. {
  489. var style1Xaml = @"
  490. <Styles xmlns='https://github.com/avaloniaui'
  491. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  492. <StyleInclude Source='test:style2.xaml'/>
  493. </Styles>";
  494. var style2Xaml = @"
  495. <Style xmlns='https://github.com/avaloniaui'
  496. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  497. <Style.Resources>
  498. <Color x:Key='Red'>Red</Color>
  499. <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/>
  500. </Style.Resources>
  501. </Style>";
  502. using (StyledWindow(
  503. ("test:style1.xaml", style1Xaml),
  504. ("test:style2.xaml", style2Xaml)))
  505. {
  506. var xaml = @"
  507. <Window xmlns='https://github.com/avaloniaui'
  508. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  509. <Window.Styles>
  510. <StyleInclude Source='test:style1.xaml'/>
  511. </Window.Styles>
  512. <Border Name='border' Background='{DynamicResource RedBrush}'/>
  513. </Window>";
  514. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  515. var border = window.FindControl<Border>("border");
  516. var borderBrush = (ISolidColorBrush)border.Background;
  517. Assert.NotNull(borderBrush);
  518. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  519. }
  520. }
  521. [Fact]
  522. public void Control_Property_Is_Updated_When_Parent_Is_Changed()
  523. {
  524. using (StyledWindow())
  525. {
  526. var xaml = @"
  527. <Window xmlns='https://github.com/avaloniaui'
  528. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  529. <Window.Resources>
  530. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  531. </Window.Resources>
  532. <Border Name='border' Background='{DynamicResource brush}'/>
  533. </Window>";
  534. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  535. var border = window.FindControl<Border>("border");
  536. DelayedBinding.ApplyBindings(border);
  537. var brush = (SolidColorBrush)border.Background;
  538. Assert.Equal(0xff506070, brush.Color.ToUint32());
  539. window.Content = null;
  540. Assert.Null(border.Background);
  541. window.Content = border;
  542. brush = (SolidColorBrush)border.Background;
  543. Assert.Equal(0xff506070, brush.Color.ToUint32());
  544. }
  545. }
  546. [Fact]
  547. public void Resource_With_DynamicResource_Is_Updated_When_Added_To_Parent()
  548. {
  549. var xaml = @"
  550. <UserControl xmlns='https://github.com/avaloniaui'
  551. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  552. <UserControl.Resources>
  553. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  554. </UserControl.Resources>
  555. <Border Name='border' Background='{DynamicResource brush}'/>
  556. </UserControl>";
  557. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  558. var border = userControl.FindControl<Border>("border");
  559. DelayedBinding.ApplyBindings(border);
  560. var brush = (SolidColorBrush)border.Background;
  561. Assert.Equal(0u, brush.Color.ToUint32());
  562. brush.GetObservable(SolidColorBrush.ColorProperty).Subscribe(_ => { });
  563. using (UnitTestApplication.Start(TestServices.StyledWindow))
  564. {
  565. var window = new Window
  566. {
  567. Resources =
  568. {
  569. { "color", Colors.Red }
  570. },
  571. Content = userControl,
  572. };
  573. window.Show();
  574. Assert.Equal(Colors.Red, brush.Color);
  575. }
  576. }
  577. [Fact]
  578. public void MergedDictionary_Resource_With_DynamicResource_Is_Updated_When_Added_To_Parent()
  579. {
  580. var xaml = @"
  581. <UserControl xmlns='https://github.com/avaloniaui'
  582. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  583. <UserControl.Resources>
  584. <ResourceDictionary>
  585. <ResourceDictionary.MergedDictionaries>
  586. <ResourceDictionary>
  587. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  588. </ResourceDictionary>
  589. </ResourceDictionary.MergedDictionaries>
  590. </ResourceDictionary>
  591. </UserControl.Resources>
  592. <Border Name='border' Background='{DynamicResource brush}'/>
  593. </UserControl>";
  594. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  595. var border = userControl.FindControl<Border>("border");
  596. DelayedBinding.ApplyBindings(border);
  597. var brush = (SolidColorBrush)border.Background;
  598. Assert.Equal(0u, brush.Color.ToUint32());
  599. brush.GetObservable(SolidColorBrush.ColorProperty).Subscribe(_ => { });
  600. using (UnitTestApplication.Start(TestServices.StyledWindow))
  601. {
  602. var window = new Window
  603. {
  604. Resources =
  605. {
  606. { "color", Colors.Red }
  607. },
  608. Content = userControl,
  609. };
  610. window.Show();
  611. Assert.Equal(Colors.Red, brush.Color);
  612. }
  613. }
  614. [Fact]
  615. public void Style_Resource_With_DynamicResource_Is_Updated_When_Added_To_Parent()
  616. {
  617. var xaml = @"
  618. <UserControl xmlns='https://github.com/avaloniaui'
  619. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  620. <UserControl.Styles>
  621. <Style>
  622. <Style.Resources>
  623. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  624. </Style.Resources>
  625. </Style>
  626. </UserControl.Styles>
  627. <Border Name='border' Background='{DynamicResource brush}'/>
  628. </UserControl>";
  629. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  630. var border = userControl.FindControl<Border>("border");
  631. DelayedBinding.ApplyBindings(border);
  632. var brush = (SolidColorBrush)border.Background;
  633. Assert.Equal(0u, brush.Color.ToUint32());
  634. using (UnitTestApplication.Start(TestServices.StyledWindow))
  635. {
  636. var window = new Window
  637. {
  638. Resources =
  639. {
  640. { "color", Colors.Red }
  641. },
  642. Content = userControl,
  643. };
  644. window.Show();
  645. Assert.Equal(Colors.Red, brush.Color);
  646. }
  647. }
  648. [Fact]
  649. public void Automatically_Converts_Color_To_SolidColorBrush()
  650. {
  651. var xaml = @"
  652. <UserControl xmlns='https://github.com/avaloniaui'
  653. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  654. <UserControl.Resources>
  655. <Color x:Key='color'>#ff506070</Color>
  656. </UserControl.Resources>
  657. <Border Name='border' Background='{DynamicResource color}'/>
  658. </UserControl>";
  659. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  660. var border = userControl.FindControl<Border>("border");
  661. var brush = (ISolidColorBrush)border.Background;
  662. Assert.Equal(0xff506070, brush.Color.ToUint32());
  663. }
  664. [Fact]
  665. public void Resource_In_Non_Matching_Style_Is_Not_Resolved()
  666. {
  667. using var app = StyledWindow();
  668. var xaml = @"
  669. <Window xmlns='https://github.com/avaloniaui'
  670. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  671. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions'>
  672. <Window.Resources>
  673. <ResourceDictionary>
  674. <ResourceDictionary.MergedDictionaries>
  675. <local:TrackingResourceProvider/>
  676. </ResourceDictionary.MergedDictionaries>
  677. </ResourceDictionary>
  678. </Window.Resources>
  679. <Window.Styles>
  680. <Style Selector='Border.nomatch'>
  681. <Setter Property='Tag' Value='{DynamicResource foo}'/>
  682. </Style>
  683. <Style Selector='Border'>
  684. <Setter Property='Tag' Value='{DynamicResource bar}'/>
  685. </Style>
  686. </Window.Styles>
  687. <Border Name='border'/>
  688. </Window>";
  689. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  690. var border = window.FindControl<Border>("border");
  691. Assert.Equal("bar", border.Tag);
  692. var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0];
  693. Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources);
  694. }
  695. [Fact]
  696. public void Resource_In_Non_Active_Style_Is_Not_Resolved()
  697. {
  698. using var app = StyledWindow();
  699. var xaml = @"
  700. <Window xmlns='https://github.com/avaloniaui'
  701. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  702. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions'>
  703. <Window.Resources>
  704. <ResourceDictionary>
  705. <ResourceDictionary.MergedDictionaries>
  706. <local:TrackingResourceProvider/>
  707. </ResourceDictionary.MergedDictionaries>
  708. </ResourceDictionary>
  709. </Window.Resources>
  710. <Window.Styles>
  711. <Style Selector='Border'>
  712. <Setter Property='Tag' Value='{DynamicResource foo}'/>
  713. </Style>
  714. <Style Selector='Border'>
  715. <Setter Property='Tag' Value='{DynamicResource bar}'/>
  716. </Style>
  717. </Window.Styles>
  718. <Border Name='border'/>
  719. </Window>";
  720. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  721. var border = window.FindControl<Border>("border");
  722. Assert.Equal("bar", border.Tag);
  723. var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0];
  724. Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources);
  725. }
  726. private IDisposable StyledWindow(params (string, string)[] assets)
  727. {
  728. var services = TestServices.StyledWindow.With(
  729. assetLoader: new MockAssetLoader(assets),
  730. theme: () => new Styles
  731. {
  732. WindowStyle(),
  733. });
  734. return UnitTestApplication.Start(services);
  735. }
  736. private Style WindowStyle()
  737. {
  738. return new Style(x => x.OfType<Window>())
  739. {
  740. Setters =
  741. {
  742. new Setter(
  743. Window.TemplateProperty,
  744. new FuncControlTemplate<Window>((x, scope) =>
  745. new ContentPresenter
  746. {
  747. Name = "PART_ContentPresenter",
  748. [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
  749. }.RegisterInNameScope(scope)))
  750. }
  751. };
  752. }
  753. }
  754. public class TrackingResourceProvider : IResourceProvider
  755. {
  756. public IResourceHost Owner { get; private set; }
  757. public bool HasResources => true;
  758. public List<object> RequestedResources { get; } = new List<object>();
  759. public event EventHandler OwnerChanged;
  760. public void AddOwner(IResourceHost owner) => Owner = owner;
  761. public void RemoveOwner(IResourceHost owner) => Owner = null;
  762. public bool TryGetResource(object key, out object value)
  763. {
  764. RequestedResources.Add(key);
  765. value = key;
  766. return true;
  767. }
  768. }
  769. }