DynamicResourceExtensionTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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_ItemTemplate_Property()
  293. {
  294. var xaml = @"
  295. <UserControl xmlns='https://github.com/avaloniaui'
  296. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  297. <UserControl.Resources>
  298. <DataTemplate x:Key='PurpleData'>
  299. <TextBlock Text='{Binding Name}' Background='Purple'/>
  300. </DataTemplate>
  301. </UserControl.Resources>
  302. <ListBox Name='listBox' ItemTemplate='{DynamicResource PurpleData}'/>
  303. </UserControl>";
  304. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  305. var listBox = userControl.FindControl<ListBox>("listBox");
  306. DelayedBinding.ApplyBindings(listBox);
  307. Assert.NotNull(listBox.ItemTemplate);
  308. }
  309. [Fact]
  310. public void DynamicResource_Tracks_Added_Resource()
  311. {
  312. var xaml = @"
  313. <UserControl xmlns='https://github.com/avaloniaui'
  314. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  315. <Border Name='border' Background='{DynamicResource brush}'/>
  316. </UserControl>";
  317. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  318. var border = userControl.FindControl<Border>("border");
  319. DelayedBinding.ApplyBindings(border);
  320. Assert.Null(border.Background);
  321. userControl.Resources.Add("brush", new SolidColorBrush(0xff506070));
  322. var brush = (SolidColorBrush)border.Background;
  323. Assert.NotNull(brush);
  324. Assert.Equal(0xff506070, brush.Color.ToUint32());
  325. }
  326. [Fact]
  327. public void DynamicResource_Tracks_Added_Style_Resource()
  328. {
  329. var xaml = @"
  330. <UserControl xmlns='https://github.com/avaloniaui'
  331. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  332. <Border Name='border' Background='{DynamicResource brush}'/>
  333. </UserControl>";
  334. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  335. var border = userControl.FindControl<Border>("border");
  336. DelayedBinding.ApplyBindings(border);
  337. Assert.Null(border.Background);
  338. userControl.Styles.Resources.Add("brush", new SolidColorBrush(0xff506070));
  339. var brush = (SolidColorBrush)border.Background;
  340. Assert.NotNull(brush);
  341. Assert.Equal(0xff506070, brush.Color.ToUint32());
  342. }
  343. [Fact]
  344. public void DynamicResource_Tracks_Added_Nested_Style_Resource()
  345. {
  346. var xaml = @"
  347. <UserControl xmlns='https://github.com/avaloniaui'
  348. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  349. <UserControl.Styles>
  350. <Style>
  351. </Style>
  352. </UserControl.Styles>
  353. <Border Name='border' Background='{DynamicResource brush}'/>
  354. </UserControl>";
  355. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  356. var border = userControl.FindControl<Border>("border");
  357. DelayedBinding.ApplyBindings(border);
  358. Assert.Null(border.Background);
  359. ((Style)userControl.Styles[0]).Resources.Add("brush", new SolidColorBrush(0xff506070));
  360. var brush = (SolidColorBrush)border.Background;
  361. Assert.NotNull(brush);
  362. Assert.Equal(0xff506070, brush.Color.ToUint32());
  363. }
  364. [Fact]
  365. public void DynamicResource_Tracks_Added_MergedResource()
  366. {
  367. var xaml = @"
  368. <UserControl xmlns='https://github.com/avaloniaui'
  369. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  370. <UserControl.Resources>
  371. <ResourceDictionary>
  372. <ResourceDictionary.MergedDictionaries>
  373. <ResourceDictionary/>
  374. </ResourceDictionary.MergedDictionaries>
  375. </ResourceDictionary>
  376. </UserControl.Resources>
  377. <Border Name='border' Background='{DynamicResource brush}'/>
  378. </UserControl>";
  379. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  380. var border = userControl.FindControl<Border>("border");
  381. DelayedBinding.ApplyBindings(border);
  382. Assert.Null(border.Background);
  383. ((IResourceDictionary)userControl.Resources.MergedDictionaries[0]).Add("brush", new SolidColorBrush(0xff506070));
  384. var brush = (SolidColorBrush)border.Background;
  385. Assert.NotNull(brush);
  386. Assert.Equal(0xff506070, brush.Color.ToUint32());
  387. }
  388. [Fact]
  389. public void DynamicResource_Tracks_Added_MergedResource_Dictionary()
  390. {
  391. var xaml = @"
  392. <UserControl xmlns='https://github.com/avaloniaui'
  393. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  394. <Border Name='border' Background='{DynamicResource brush}'/>
  395. </UserControl>";
  396. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  397. var border = userControl.FindControl<Border>("border");
  398. DelayedBinding.ApplyBindings(border);
  399. Assert.Null(border.Background);
  400. var dictionary = new ResourceDictionary
  401. {
  402. { "brush", new SolidColorBrush(0xff506070) },
  403. };
  404. userControl.Resources.MergedDictionaries.Add(dictionary);
  405. var brush = (SolidColorBrush)border.Background;
  406. Assert.NotNull(brush);
  407. Assert.Equal(0xff506070, brush.Color.ToUint32());
  408. }
  409. [Fact]
  410. public void DynamicResource_Tracks_Added_Style_MergedResource_Dictionary()
  411. {
  412. var xaml = @"
  413. <UserControl xmlns='https://github.com/avaloniaui'
  414. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  415. <UserControl.Styles>
  416. <Style>
  417. </Style>
  418. </UserControl.Styles>
  419. <Border Name='border' Background='{DynamicResource brush}'/>
  420. </UserControl>";
  421. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  422. var border = userControl.FindControl<Border>("border");
  423. DelayedBinding.ApplyBindings(border);
  424. Assert.Null(border.Background);
  425. var dictionary = new ResourceDictionary
  426. {
  427. { "brush", new SolidColorBrush(0xff506070) },
  428. };
  429. ((Style)userControl.Styles[0]).Resources.MergedDictionaries.Add(dictionary);
  430. var brush = (SolidColorBrush)border.Background;
  431. Assert.NotNull(brush);
  432. Assert.Equal(0xff506070, brush.Color.ToUint32());
  433. }
  434. [Fact]
  435. public void DynamicResource_Can_Be_Found_Across_Xaml_Style_Files()
  436. {
  437. var style1Xaml = @"
  438. <Style xmlns='https://github.com/avaloniaui'
  439. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  440. <Style.Resources>
  441. <Color x:Key='Red'>Red</Color>
  442. </Style.Resources>
  443. </Style>";
  444. var style2Xaml = @"
  445. <Style xmlns='https://github.com/avaloniaui'
  446. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  447. <Style.Resources>
  448. <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/>
  449. </Style.Resources>
  450. </Style>";
  451. using (StyledWindow(
  452. ("test:style1.xaml", style1Xaml),
  453. ("test:style2.xaml", style2Xaml)))
  454. {
  455. var xaml = @"
  456. <Window xmlns='https://github.com/avaloniaui'
  457. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  458. <Window.Styles>
  459. <StyleInclude Source='test:style1.xaml'/>
  460. <StyleInclude Source='test:style2.xaml'/>
  461. </Window.Styles>
  462. <Border Name='border' Background='{DynamicResource RedBrush}'/>
  463. </Window>";
  464. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  465. var border = window.FindControl<Border>("border");
  466. var borderBrush = (ISolidColorBrush)border.Background;
  467. Assert.NotNull(borderBrush);
  468. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  469. }
  470. }
  471. [Fact]
  472. public void DynamicResource_Can_Be_Found_In_Nested_Style_File()
  473. {
  474. var style1Xaml = @"
  475. <Styles xmlns='https://github.com/avaloniaui'
  476. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  477. <StyleInclude Source='test:style2.xaml'/>
  478. </Styles>";
  479. var style2Xaml = @"
  480. <Style xmlns='https://github.com/avaloniaui'
  481. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  482. <Style.Resources>
  483. <Color x:Key='Red'>Red</Color>
  484. <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/>
  485. </Style.Resources>
  486. </Style>";
  487. using (StyledWindow(
  488. ("test:style1.xaml", style1Xaml),
  489. ("test:style2.xaml", style2Xaml)))
  490. {
  491. var xaml = @"
  492. <Window xmlns='https://github.com/avaloniaui'
  493. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  494. <Window.Styles>
  495. <StyleInclude Source='test:style1.xaml'/>
  496. </Window.Styles>
  497. <Border Name='border' Background='{DynamicResource RedBrush}'/>
  498. </Window>";
  499. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  500. var border = window.FindControl<Border>("border");
  501. var borderBrush = (ISolidColorBrush)border.Background;
  502. Assert.NotNull(borderBrush);
  503. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  504. }
  505. }
  506. [Fact]
  507. public void Control_Property_Is_Updated_When_Parent_Is_Changed()
  508. {
  509. using (StyledWindow())
  510. {
  511. var xaml = @"
  512. <Window xmlns='https://github.com/avaloniaui'
  513. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  514. <Window.Resources>
  515. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  516. </Window.Resources>
  517. <Border Name='border' Background='{DynamicResource brush}'/>
  518. </Window>";
  519. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  520. var border = window.FindControl<Border>("border");
  521. DelayedBinding.ApplyBindings(border);
  522. var brush = (SolidColorBrush)border.Background;
  523. Assert.Equal(0xff506070, brush.Color.ToUint32());
  524. window.Content = null;
  525. Assert.Null(border.Background);
  526. window.Content = border;
  527. brush = (SolidColorBrush)border.Background;
  528. Assert.Equal(0xff506070, brush.Color.ToUint32());
  529. }
  530. }
  531. [Fact]
  532. public void Resource_With_DynamicResource_Is_Updated_When_Added_To_Parent()
  533. {
  534. var xaml = @"
  535. <UserControl xmlns='https://github.com/avaloniaui'
  536. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  537. <UserControl.Resources>
  538. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  539. </UserControl.Resources>
  540. <Border Name='border' Background='{DynamicResource brush}'/>
  541. </UserControl>";
  542. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  543. var border = userControl.FindControl<Border>("border");
  544. DelayedBinding.ApplyBindings(border);
  545. var brush = (SolidColorBrush)border.Background;
  546. Assert.Equal(0u, brush.Color.ToUint32());
  547. brush.GetObservable(SolidColorBrush.ColorProperty).Subscribe(_ => { });
  548. using (UnitTestApplication.Start(TestServices.StyledWindow))
  549. {
  550. var window = new Window
  551. {
  552. Resources =
  553. {
  554. { "color", Colors.Red }
  555. },
  556. Content = userControl,
  557. };
  558. window.Show();
  559. Assert.Equal(Colors.Red, brush.Color);
  560. }
  561. }
  562. [Fact]
  563. public void MergedDictionary_Resource_With_DynamicResource_Is_Updated_When_Added_To_Parent()
  564. {
  565. var xaml = @"
  566. <UserControl xmlns='https://github.com/avaloniaui'
  567. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  568. <UserControl.Resources>
  569. <ResourceDictionary>
  570. <ResourceDictionary.MergedDictionaries>
  571. <ResourceDictionary>
  572. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  573. </ResourceDictionary>
  574. </ResourceDictionary.MergedDictionaries>
  575. </ResourceDictionary>
  576. </UserControl.Resources>
  577. <Border Name='border' Background='{DynamicResource brush}'/>
  578. </UserControl>";
  579. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  580. var border = userControl.FindControl<Border>("border");
  581. DelayedBinding.ApplyBindings(border);
  582. var brush = (SolidColorBrush)border.Background;
  583. Assert.Equal(0u, brush.Color.ToUint32());
  584. brush.GetObservable(SolidColorBrush.ColorProperty).Subscribe(_ => { });
  585. using (UnitTestApplication.Start(TestServices.StyledWindow))
  586. {
  587. var window = new Window
  588. {
  589. Resources =
  590. {
  591. { "color", Colors.Red }
  592. },
  593. Content = userControl,
  594. };
  595. window.Show();
  596. Assert.Equal(Colors.Red, brush.Color);
  597. }
  598. }
  599. [Fact]
  600. public void Style_Resource_With_DynamicResource_Is_Updated_When_Added_To_Parent()
  601. {
  602. var xaml = @"
  603. <UserControl xmlns='https://github.com/avaloniaui'
  604. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  605. <UserControl.Styles>
  606. <Style>
  607. <Style.Resources>
  608. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  609. </Style.Resources>
  610. </Style>
  611. </UserControl.Styles>
  612. <Border Name='border' Background='{DynamicResource brush}'/>
  613. </UserControl>";
  614. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  615. var border = userControl.FindControl<Border>("border");
  616. DelayedBinding.ApplyBindings(border);
  617. var brush = (SolidColorBrush)border.Background;
  618. Assert.Equal(0u, brush.Color.ToUint32());
  619. using (UnitTestApplication.Start(TestServices.StyledWindow))
  620. {
  621. var window = new Window
  622. {
  623. Resources =
  624. {
  625. { "color", Colors.Red }
  626. },
  627. Content = userControl,
  628. };
  629. window.Show();
  630. Assert.Equal(Colors.Red, brush.Color);
  631. }
  632. }
  633. [Fact]
  634. public void Automatically_Converts_Color_To_SolidColorBrush()
  635. {
  636. var xaml = @"
  637. <UserControl xmlns='https://github.com/avaloniaui'
  638. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  639. <UserControl.Resources>
  640. <Color x:Key='color'>#ff506070</Color>
  641. </UserControl.Resources>
  642. <Border Name='border' Background='{DynamicResource color}'/>
  643. </UserControl>";
  644. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  645. var border = userControl.FindControl<Border>("border");
  646. var brush = (ISolidColorBrush)border.Background;
  647. Assert.Equal(0xff506070, brush.Color.ToUint32());
  648. }
  649. [Fact]
  650. public void Resource_In_Non_Matching_Style_Is_Not_Resolved()
  651. {
  652. using var app = StyledWindow();
  653. var xaml = @"
  654. <Window xmlns='https://github.com/avaloniaui'
  655. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  656. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions'>
  657. <Window.Resources>
  658. <ResourceDictionary>
  659. <ResourceDictionary.MergedDictionaries>
  660. <local:TrackingResourceProvider/>
  661. </ResourceDictionary.MergedDictionaries>
  662. </ResourceDictionary>
  663. </Window.Resources>
  664. <Window.Styles>
  665. <Style Selector='Border.nomatch'>
  666. <Setter Property='Tag' Value='{DynamicResource foo}'/>
  667. </Style>
  668. <Style Selector='Border'>
  669. <Setter Property='Tag' Value='{DynamicResource bar}'/>
  670. </Style>
  671. </Window.Styles>
  672. <Border Name='border'/>
  673. </Window>";
  674. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  675. var border = window.FindControl<Border>("border");
  676. Assert.Equal("bar", border.Tag);
  677. var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0];
  678. Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources);
  679. }
  680. [Fact]
  681. public void Resource_In_Non_Active_Style_Is_Not_Resolved()
  682. {
  683. using var app = StyledWindow();
  684. var xaml = @"
  685. <Window xmlns='https://github.com/avaloniaui'
  686. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  687. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions'>
  688. <Window.Resources>
  689. <ResourceDictionary>
  690. <ResourceDictionary.MergedDictionaries>
  691. <local:TrackingResourceProvider/>
  692. </ResourceDictionary.MergedDictionaries>
  693. </ResourceDictionary>
  694. </Window.Resources>
  695. <Window.Styles>
  696. <Style Selector='Border'>
  697. <Setter Property='Tag' Value='{DynamicResource foo}'/>
  698. </Style>
  699. <Style Selector='Border'>
  700. <Setter Property='Tag' Value='{DynamicResource bar}'/>
  701. </Style>
  702. </Window.Styles>
  703. <Border Name='border'/>
  704. </Window>";
  705. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  706. var border = window.FindControl<Border>("border");
  707. Assert.Equal("bar", border.Tag);
  708. var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0];
  709. Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources);
  710. }
  711. private IDisposable StyledWindow(params (string, string)[] assets)
  712. {
  713. var services = TestServices.StyledWindow.With(
  714. assetLoader: new MockAssetLoader(assets),
  715. theme: () => new Styles
  716. {
  717. WindowStyle(),
  718. });
  719. return UnitTestApplication.Start(services);
  720. }
  721. private Style WindowStyle()
  722. {
  723. return new Style(x => x.OfType<Window>())
  724. {
  725. Setters =
  726. {
  727. new Setter(
  728. Window.TemplateProperty,
  729. new FuncControlTemplate<Window>((x, scope) =>
  730. new ContentPresenter
  731. {
  732. Name = "PART_ContentPresenter",
  733. [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
  734. }.RegisterInNameScope(scope)))
  735. }
  736. };
  737. }
  738. }
  739. public class TrackingResourceProvider : IResourceProvider
  740. {
  741. public IResourceHost Owner { get; private set; }
  742. public bool HasResources => true;
  743. public List<object> RequestedResources { get; } = new List<object>();
  744. public event EventHandler OwnerChanged;
  745. public void AddOwner(IResourceHost owner) => Owner = owner;
  746. public void RemoveOwner(IResourceHost owner) => Owner = null;
  747. public bool TryGetResource(object key, out object value)
  748. {
  749. RequestedResources.Add(key);
  750. value = key;
  751. return true;
  752. }
  753. }
  754. }