DynamicResourceExtensionTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 System;
  4. using System.Linq;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Markup.Xaml.Data;
  9. using Avalonia.Media;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  13. using Xunit;
  14. namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
  15. {
  16. public class DynamicResourceExtensionTests
  17. {
  18. [Fact]
  19. public void DynamicResource_Can_Be_Assigned_To_Property()
  20. {
  21. var xaml = @"
  22. <UserControl xmlns='https://github.com/avaloniaui'
  23. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  24. <UserControl.Resources>
  25. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  26. </UserControl.Resources>
  27. <Border Name='border' Background='{DynamicResource brush}'/>
  28. </UserControl>";
  29. var loader = new AvaloniaXamlLoader();
  30. var userControl = (UserControl)loader.Load(xaml);
  31. var border = userControl.FindControl<Border>("border");
  32. DelayedBinding.ApplyBindings(border);
  33. var brush = (SolidColorBrush)border.Background;
  34. Assert.Equal(0xff506070, brush.Color.ToUint32());
  35. }
  36. [Fact]
  37. public void DynamicResource_Can_Be_Assigned_To_Attached_Property()
  38. {
  39. var xaml = @"
  40. <UserControl xmlns='https://github.com/avaloniaui'
  41. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  42. <UserControl.Resources>
  43. <x:Int32 x:Key='col'>5</x:Int32>
  44. </UserControl.Resources>
  45. <Border Name='border' Grid.Column='{DynamicResource col}'/>
  46. </UserControl>";
  47. var loader = new AvaloniaXamlLoader();
  48. var userControl = (UserControl)loader.Load(xaml);
  49. var border = userControl.FindControl<Border>("border");
  50. DelayedBinding.ApplyBindings(border);
  51. Assert.Equal(5, Grid.GetColumn(border));
  52. }
  53. [Fact]
  54. public void DynamicResource_From_Style_Can_Be_Assigned_To_Property()
  55. {
  56. var xaml = @"
  57. <UserControl xmlns='https://github.com/avaloniaui'
  58. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  59. <UserControl.Styles>
  60. <Style>
  61. <Style.Resources>
  62. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  63. </Style.Resources>
  64. </Style>
  65. </UserControl.Styles>
  66. <Border Name='border' Background='{DynamicResource brush}'/>
  67. </UserControl>";
  68. var loader = new AvaloniaXamlLoader();
  69. var userControl = (UserControl)loader.Load(xaml);
  70. var border = userControl.FindControl<Border>("border");
  71. DelayedBinding.ApplyBindings(border);
  72. var brush = (SolidColorBrush)border.Background;
  73. Assert.Equal(0xff506070, brush.Color.ToUint32());
  74. }
  75. [Fact]
  76. public void DynamicResource_From_MergedDictionary_Can_Be_Assigned_To_Property()
  77. {
  78. var xaml = @"
  79. <UserControl xmlns='https://github.com/avaloniaui'
  80. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  81. <UserControl.Resources>
  82. <ResourceDictionary>
  83. <ResourceDictionary.MergedDictionaries>
  84. <ResourceDictionary>
  85. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  86. </ResourceDictionary>
  87. </ResourceDictionary.MergedDictionaries>
  88. </ResourceDictionary>
  89. </UserControl.Resources>
  90. <Border Name='border' Background='{DynamicResource brush}'/>
  91. </UserControl>";
  92. var loader = new AvaloniaXamlLoader();
  93. var userControl = (UserControl)loader.Load(xaml);
  94. var border = userControl.FindControl<Border>("border");
  95. DelayedBinding.ApplyBindings(border);
  96. var brush = (SolidColorBrush)border.Background;
  97. Assert.Equal(0xff506070, brush.Color.ToUint32());
  98. }
  99. [Fact]
  100. public void DynamicResource_From_MergedDictionary_In_Style_Can_Be_Assigned_To_Property()
  101. {
  102. var xaml = @"
  103. <UserControl xmlns='https://github.com/avaloniaui'
  104. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  105. <UserControl.Styles>
  106. <Style>
  107. <Style.Resources>
  108. <ResourceDictionary>
  109. <ResourceDictionary.MergedDictionaries>
  110. <ResourceDictionary>
  111. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  112. </ResourceDictionary>
  113. </ResourceDictionary.MergedDictionaries>
  114. </ResourceDictionary>
  115. </Style.Resources>
  116. </Style>
  117. </UserControl.Styles>
  118. <Border Name='border' Background='{DynamicResource brush}'/>
  119. </UserControl>";
  120. var loader = new AvaloniaXamlLoader();
  121. var userControl = (UserControl)loader.Load(xaml);
  122. var border = userControl.FindControl<Border>("border");
  123. DelayedBinding.ApplyBindings(border);
  124. var brush = (SolidColorBrush)border.Background;
  125. Assert.Equal(0xff506070, brush.Color.ToUint32());
  126. }
  127. [Fact]
  128. public void DynamicResource_From_Application_Can_Be_Assigned_To_Property_In_Window()
  129. {
  130. using (StyledWindow())
  131. {
  132. Application.Current.Resources.Add("brush", new SolidColorBrush(0xff506070));
  133. var xaml = @"
  134. <Window xmlns='https://github.com/avaloniaui'
  135. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  136. <Border Name='border' Background='{DynamicResource brush}'/>
  137. </Window>";
  138. var loader = new AvaloniaXamlLoader();
  139. var window = (Window)loader.Load(xaml);
  140. var border = window.FindControl<Border>("border");
  141. var brush = (SolidColorBrush)border.Background;
  142. Assert.Equal(0xff506070, brush.Color.ToUint32());
  143. }
  144. }
  145. [Fact]
  146. public void DynamicResource_From_Application_Can_Be_Assigned_To_Property_In_UserControl()
  147. {
  148. using (UnitTestApplication.Start(TestServices.StyledWindow))
  149. {
  150. Application.Current.Resources.Add("brush", new SolidColorBrush(0xff506070));
  151. var xaml = @"
  152. <UserControl xmlns='https://github.com/avaloniaui'
  153. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  154. <Border Name='border' Background='{DynamicResource brush}'/>
  155. </UserControl>";
  156. var loader = new AvaloniaXamlLoader();
  157. var userControl = (UserControl)loader.Load(xaml);
  158. var border = userControl.FindControl<Border>("border");
  159. // We don't actually know where the global styles are until we attach the control
  160. // to a window, as Window has StylingParent set to Application.
  161. var window = new Window { Content = userControl };
  162. window.Show();
  163. var brush = (SolidColorBrush)border.Background;
  164. Assert.Equal(0xff506070, brush.Color.ToUint32());
  165. }
  166. }
  167. [Fact]
  168. public void DynamicResource_Can_Be_Assigned_To_Setter()
  169. {
  170. using (StyledWindow())
  171. {
  172. var xaml = @"
  173. <Window xmlns='https://github.com/avaloniaui'
  174. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  175. <Window.Resources>
  176. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  177. </Window.Resources>
  178. <Window.Styles>
  179. <Style Selector='Button'>
  180. <Setter Property='Background' Value='{DynamicResource brush}'/>
  181. </Style>
  182. </Window.Styles>
  183. <Button Name='button'/>
  184. </Window>";
  185. var loader = new AvaloniaXamlLoader();
  186. var window = (Window)loader.Load(xaml);
  187. var button = window.FindControl<Button>("button");
  188. var brush = (SolidColorBrush)button.Background;
  189. Assert.Equal(0xff506070, brush.Color.ToUint32());
  190. }
  191. }
  192. [Fact]
  193. public void DynamicResource_From_Style_Can_Be_Assigned_To_Setter()
  194. {
  195. using (StyledWindow())
  196. {
  197. var xaml = @"
  198. <Window xmlns='https://github.com/avaloniaui'
  199. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  200. <Window.Styles>
  201. <Style>
  202. <Style.Resources>
  203. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  204. </Style.Resources>
  205. </Style>
  206. <Style Selector='Button'>
  207. <Setter Property='Background' Value='{DynamicResource brush}'/>
  208. </Style>
  209. </Window.Styles>
  210. <Button Name='button'/>
  211. </Window>";
  212. var loader = new AvaloniaXamlLoader();
  213. var window = (Window)loader.Load(xaml);
  214. var button = window.FindControl<Button>("button");
  215. var brush = (SolidColorBrush)button.Background;
  216. Assert.Equal(0xff506070, brush.Color.ToUint32());
  217. }
  218. }
  219. [Fact]
  220. public void DynamicResource_Can_Be_Assigned_To_Setter_In_Styles_File()
  221. {
  222. var styleXaml = @"
  223. <Styles xmlns='https://github.com/avaloniaui'
  224. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  225. <Styles.Resources>
  226. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  227. </Styles.Resources>
  228. <Style Selector='Border'>
  229. <Setter Property='Background' Value='{DynamicResource brush}'/>
  230. </Style>
  231. </Styles>";
  232. using (StyledWindow(assets: ("test:style.xaml", styleXaml)))
  233. {
  234. var xaml = @"
  235. <Window xmlns='https://github.com/avaloniaui'
  236. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  237. <Window.Styles>
  238. <StyleInclude Source='test:style.xaml'/>
  239. </Window.Styles>
  240. <Border Name='border'/>
  241. </Window>";
  242. var loader = new AvaloniaXamlLoader();
  243. var window = (Window)loader.Load(xaml);
  244. var border = window.FindControl<Border>("border");
  245. var brush = (SolidColorBrush)border.Background;
  246. Assert.Equal(0xff506070, brush.Color.ToUint32());
  247. }
  248. }
  249. [Fact]
  250. public void DynamicResource_Can_Be_Assigned_To_Property_In_ControlTemplate_In_Styles_File()
  251. {
  252. var styleXaml = @"
  253. <Styles xmlns='https://github.com/avaloniaui'
  254. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  255. <Styles.Resources>
  256. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  257. </Styles.Resources>
  258. <Style Selector='Button'>
  259. <Setter Property='Template'>
  260. <ControlTemplate>
  261. <Border Name='border' Background='{DynamicResource brush}'/>
  262. </ControlTemplate>
  263. </Setter>
  264. </Style>
  265. </Styles>";
  266. using (StyledWindow(assets: ("test:style.xaml", styleXaml)))
  267. {
  268. var xaml = @"
  269. <Window xmlns='https://github.com/avaloniaui'
  270. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  271. <Window.Styles>
  272. <StyleInclude Source='test:style.xaml'/>
  273. </Window.Styles>
  274. <Button Name='button'/>
  275. </Window>";
  276. var loader = new AvaloniaXamlLoader();
  277. var window = (Window)loader.Load(xaml);
  278. var button = window.FindControl<Button>("button");
  279. window.Show();
  280. var border = (Border)button.GetVisualChildren().Single();
  281. var brush = (SolidColorBrush)border.Background;
  282. Assert.Equal(0xff506070, brush.Color.ToUint32());
  283. }
  284. }
  285. [Fact]
  286. public void DynamicResource_Can_Be_Assigned_To_Resource_Property()
  287. {
  288. var xaml = @"
  289. <UserControl xmlns='https://github.com/avaloniaui'
  290. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  291. <UserControl.Resources>
  292. <Color x:Key='color'>#ff506070</Color>
  293. <SolidColorBrush x:Key='brush' Color='{DynamicResource color}'/>
  294. </UserControl.Resources>
  295. <Border Name='border' Background='{DynamicResource brush}'/>
  296. </UserControl>";
  297. var loader = new AvaloniaXamlLoader();
  298. var userControl = (UserControl)loader.Load(xaml);
  299. var border = userControl.FindControl<Border>("border");
  300. DelayedBinding.ApplyBindings(border);
  301. var brush = (SolidColorBrush)border.Background;
  302. Assert.Equal(0xff506070, brush.Color.ToUint32());
  303. }
  304. [Fact]
  305. public void DynamicResource_Can_Be_Assigned_To_ItemTemplate_Property()
  306. {
  307. var xaml = @"
  308. <UserControl xmlns='https://github.com/avaloniaui'
  309. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  310. <UserControl.Resources>
  311. <DataTemplate x:Key='PurpleData'>
  312. <TextBlock Text='{Binding Name}' Background='Purple'/>
  313. </DataTemplate>
  314. </UserControl.Resources>
  315. <ListBox Name='listBox' ItemTemplate='{DynamicResource PurpleData}'/>
  316. </UserControl>";
  317. var loader = new AvaloniaXamlLoader();
  318. var userControl = (UserControl)loader.Load(xaml);
  319. var listBox = userControl.FindControl<ListBox>("listBox");
  320. DelayedBinding.ApplyBindings(listBox);
  321. Assert.NotNull(listBox.ItemTemplate);
  322. }
  323. [Fact]
  324. public void DynamicResource_Tracks_Added_Resource()
  325. {
  326. var xaml = @"
  327. <UserControl xmlns='https://github.com/avaloniaui'
  328. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  329. <Border Name='border' Background='{DynamicResource brush}'/>
  330. </UserControl>";
  331. var loader = new AvaloniaXamlLoader();
  332. var userControl = (UserControl)loader.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 loader = new AvaloniaXamlLoader();
  350. var userControl = (UserControl)loader.Load(xaml);
  351. var border = userControl.FindControl<Border>("border");
  352. DelayedBinding.ApplyBindings(border);
  353. Assert.Null(border.Background);
  354. userControl.Styles.Resources.Add("brush", new SolidColorBrush(0xff506070));
  355. var brush = (SolidColorBrush)border.Background;
  356. Assert.NotNull(brush);
  357. Assert.Equal(0xff506070, brush.Color.ToUint32());
  358. }
  359. [Fact]
  360. public void DynamicResource_Tracks_Added_Nested_Style_Resource()
  361. {
  362. var xaml = @"
  363. <UserControl xmlns='https://github.com/avaloniaui'
  364. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  365. <UserControl.Styles>
  366. <Style>
  367. </Style>
  368. </UserControl.Styles>
  369. <Border Name='border' Background='{DynamicResource brush}'/>
  370. </UserControl>";
  371. var loader = new AvaloniaXamlLoader();
  372. var userControl = (UserControl)loader.Load(xaml);
  373. var border = userControl.FindControl<Border>("border");
  374. DelayedBinding.ApplyBindings(border);
  375. Assert.Null(border.Background);
  376. ((Style)userControl.Styles[0]).Resources.Add("brush", new SolidColorBrush(0xff506070));
  377. var brush = (SolidColorBrush)border.Background;
  378. Assert.NotNull(brush);
  379. Assert.Equal(0xff506070, brush.Color.ToUint32());
  380. }
  381. [Fact]
  382. public void DynamicResource_Tracks_Added_MergedResource()
  383. {
  384. var xaml = @"
  385. <UserControl xmlns='https://github.com/avaloniaui'
  386. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  387. <UserControl.Resources>
  388. <ResourceDictionary>
  389. <ResourceDictionary.MergedDictionaries>
  390. <ResourceDictionary/>
  391. </ResourceDictionary.MergedDictionaries>
  392. </ResourceDictionary>
  393. </UserControl.Resources>
  394. <Border Name='border' Background='{DynamicResource brush}'/>
  395. </UserControl>";
  396. var loader = new AvaloniaXamlLoader();
  397. var userControl = (UserControl)loader.Load(xaml);
  398. var border = userControl.FindControl<Border>("border");
  399. DelayedBinding.ApplyBindings(border);
  400. Assert.Null(border.Background);
  401. ((IResourceDictionary)userControl.Resources.MergedDictionaries[0]).Add("brush", new SolidColorBrush(0xff506070));
  402. var brush = (SolidColorBrush)border.Background;
  403. Assert.NotNull(brush);
  404. Assert.Equal(0xff506070, brush.Color.ToUint32());
  405. }
  406. [Fact]
  407. public void DynamicResource_Tracks_Added_MergedResource_Dictionary()
  408. {
  409. var xaml = @"
  410. <UserControl xmlns='https://github.com/avaloniaui'
  411. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  412. <Border Name='border' Background='{DynamicResource brush}'/>
  413. </UserControl>";
  414. var loader = new AvaloniaXamlLoader();
  415. var userControl = (UserControl)loader.Load(xaml);
  416. var border = userControl.FindControl<Border>("border");
  417. DelayedBinding.ApplyBindings(border);
  418. Assert.Null(border.Background);
  419. var dictionary = new ResourceDictionary
  420. {
  421. { "brush", new SolidColorBrush(0xff506070) },
  422. };
  423. userControl.Resources.MergedDictionaries.Add(dictionary);
  424. var brush = (SolidColorBrush)border.Background;
  425. Assert.NotNull(brush);
  426. Assert.Equal(0xff506070, brush.Color.ToUint32());
  427. }
  428. [Fact]
  429. public void DynamicResource_Tracks_Added_Style_MergedResource_Dictionary()
  430. {
  431. var xaml = @"
  432. <UserControl xmlns='https://github.com/avaloniaui'
  433. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  434. <UserControl.Styles>
  435. <Style>
  436. </Style>
  437. </UserControl.Styles>
  438. <Border Name='border' Background='{DynamicResource brush}'/>
  439. </UserControl>";
  440. var loader = new AvaloniaXamlLoader();
  441. var userControl = (UserControl)loader.Load(xaml);
  442. var border = userControl.FindControl<Border>("border");
  443. DelayedBinding.ApplyBindings(border);
  444. Assert.Null(border.Background);
  445. var dictionary = new ResourceDictionary
  446. {
  447. { "brush", new SolidColorBrush(0xff506070) },
  448. };
  449. ((Style)userControl.Styles[0]).Resources.MergedDictionaries.Add(dictionary);
  450. var brush = (SolidColorBrush)border.Background;
  451. Assert.NotNull(brush);
  452. Assert.Equal(0xff506070, brush.Color.ToUint32());
  453. }
  454. [Fact]
  455. public void DynamicResource_Can_Be_Found_Across_Xaml_Style_Files()
  456. {
  457. var style1Xaml = @"
  458. <Style xmlns='https://github.com/avaloniaui'
  459. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  460. <Style.Resources>
  461. <Color x:Key='Red'>Red</Color>
  462. </Style.Resources>
  463. </Style>";
  464. var style2Xaml = @"
  465. <Style xmlns='https://github.com/avaloniaui'
  466. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  467. <Style.Resources>
  468. <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/>
  469. </Style.Resources>
  470. </Style>";
  471. using (StyledWindow(
  472. ("test:style1.xaml", style1Xaml),
  473. ("test:style2.xaml", style2Xaml)))
  474. {
  475. var xaml = @"
  476. <Window xmlns='https://github.com/avaloniaui'
  477. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  478. <Window.Styles>
  479. <StyleInclude Source='test:style1.xaml'/>
  480. <StyleInclude Source='test:style2.xaml'/>
  481. </Window.Styles>
  482. <Border Name='border' Background='{DynamicResource RedBrush}'/>
  483. </Window>";
  484. var loader = new AvaloniaXamlLoader();
  485. var window = (Window)loader.Load(xaml);
  486. var border = window.FindControl<Border>("border");
  487. var borderBrush = (ISolidColorBrush)border.Background;
  488. Assert.NotNull(borderBrush);
  489. Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());
  490. }
  491. }
  492. [Fact]
  493. public void Control_Property_Is_Updated_When_Parent_Is_Changed()
  494. {
  495. var xaml = @"
  496. <UserControl xmlns='https://github.com/avaloniaui'
  497. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  498. <UserControl.Resources>
  499. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  500. </UserControl.Resources>
  501. <Border Name='border' Background='{DynamicResource brush}'/>
  502. </UserControl>";
  503. var loader = new AvaloniaXamlLoader();
  504. var userControl = (UserControl)loader.Load(xaml);
  505. var border = userControl.FindControl<Border>("border");
  506. DelayedBinding.ApplyBindings(border);
  507. var brush = (SolidColorBrush)border.Background;
  508. Assert.Equal(0xff506070, brush.Color.ToUint32());
  509. userControl.Content = null;
  510. Assert.Null(border.Background);
  511. userControl.Content = border;
  512. brush = (SolidColorBrush)border.Background;
  513. Assert.Equal(0xff506070, brush.Color.ToUint32());
  514. }
  515. private IDisposable StyledWindow(params (string, string)[] assets)
  516. {
  517. var services = TestServices.StyledWindow.With(
  518. assetLoader: new MockAssetLoader(assets),
  519. theme: () => new Styles
  520. {
  521. WindowStyle(),
  522. });
  523. return UnitTestApplication.Start(services);
  524. }
  525. private Style WindowStyle()
  526. {
  527. return new Style(x => x.OfType<Window>())
  528. {
  529. Setters =
  530. {
  531. new Setter(
  532. Window.TemplateProperty,
  533. new FuncControlTemplate<Window>(x =>
  534. new ContentPresenter
  535. {
  536. Name = "PART_ContentPresenter",
  537. [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
  538. }))
  539. }
  540. };
  541. }
  542. }
  543. }