StyleTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Xml;
  6. using Avalonia.Controls;
  7. using Avalonia.Markup.Xaml.Styling;
  8. using Avalonia.Markup.Xaml.Templates;
  9. using Avalonia.Media;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using Xunit;
  13. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  14. {
  15. public class StyleTests : XamlTestBase
  16. {
  17. static StyleTests()
  18. {
  19. GC.KeepAlive(typeof(ItemsRepeater));
  20. }
  21. [Fact]
  22. public void Color_Can_Be_Added_To_Style_Resources()
  23. {
  24. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  25. {
  26. var xaml = @"
  27. <UserControl xmlns='https://github.com/avaloniaui'
  28. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  29. <UserControl.Styles>
  30. <Style>
  31. <Style.Resources>
  32. <Color x:Key='color'>#ff506070</Color>
  33. </Style.Resources>
  34. </Style>
  35. </UserControl.Styles>
  36. </UserControl>";
  37. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  38. var color = (Color)((Style)userControl.Styles[0]).Resources["color"];
  39. Assert.Equal(0xff506070, color.ToUint32());
  40. }
  41. }
  42. [Fact]
  43. public void DataTemplate_Can_Be_Added_To_Style_Resources()
  44. {
  45. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  46. {
  47. var xaml = @"
  48. <UserControl xmlns='https://github.com/avaloniaui'
  49. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  50. <UserControl.Styles>
  51. <Style>
  52. <Style.Resources>
  53. <DataTemplate x:Key='dataTemplate'><TextBlock/></DataTemplate>
  54. </Style.Resources>
  55. </Style>
  56. </UserControl.Styles>
  57. </UserControl>";
  58. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  59. var dataTemplate = (DataTemplate)((Style)userControl.Styles[0]).Resources["dataTemplate"];
  60. Assert.NotNull(dataTemplate);
  61. }
  62. }
  63. [Fact]
  64. public void ControlTemplate_Can_Be_Added_To_Style_Resources()
  65. {
  66. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  67. {
  68. var xaml = @"
  69. <UserControl xmlns='https://github.com/avaloniaui'
  70. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  71. <UserControl.Styles>
  72. <Style>
  73. <Style.Resources>
  74. <ControlTemplate x:Key='controlTemplate' TargetType='{x:Type Button}'>
  75. <ContentPresenter Content='{TemplateBinding Content}'/>
  76. </ControlTemplate>
  77. </Style.Resources>
  78. </Style>
  79. </UserControl.Styles>
  80. </UserControl>";
  81. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  82. var controlTemplate = (ControlTemplate)((Style)userControl.Styles[0]).Resources["controlTemplate"];
  83. Assert.NotNull(controlTemplate);
  84. Assert.Equal(typeof(Button), controlTemplate.TargetType);
  85. }
  86. }
  87. [Fact]
  88. public void SolidColorBrush_Can_Be_Added_To_Style_Resources()
  89. {
  90. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  91. {
  92. var xaml = @"
  93. <UserControl xmlns='https://github.com/avaloniaui'
  94. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  95. <UserControl.Styles>
  96. <Style>
  97. <Style.Resources>
  98. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  99. </Style.Resources>
  100. </Style>
  101. </UserControl.Styles>
  102. </UserControl>";
  103. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  104. var brush = (ISolidColorBrush)((Style)userControl.Styles[0]).Resources["brush"];
  105. Assert.Equal(0xff506070, brush.Color.ToUint32());
  106. }
  107. }
  108. [Fact]
  109. public void Setter_Can_Contain_Template()
  110. {
  111. using (UnitTestApplication.Start(TestServices.StyledWindow))
  112. {
  113. var xaml = @"
  114. <Window xmlns='https://github.com/avaloniaui'
  115. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  116. <Window.Styles>
  117. <Style Selector='ContentControl'>
  118. <Setter Property='Content'>
  119. <Template>
  120. <TextBlock>Hello World!</TextBlock>
  121. </Template>
  122. </Setter>
  123. </Style>
  124. </Window.Styles>
  125. <ContentControl Name='target'/>
  126. </Window>";
  127. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  128. var target = window.Find<ContentControl>("target");
  129. Assert.IsType<TextBlock>(target.Content);
  130. Assert.Equal("Hello World!", ((TextBlock)target.Content).Text);
  131. }
  132. }
  133. [Fact]
  134. public void Setter_Value_Is_Bound_Directly_If_The_Target_Type_Derives_From_ITemplate()
  135. {
  136. using (UnitTestApplication.Start(TestServices.StyledWindow))
  137. {
  138. var xaml = @"
  139. <Window xmlns='https://github.com/avaloniaui'
  140. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  141. <Window.Styles>
  142. <Style Selector=':is(Control)'>
  143. <Setter Property='FocusAdorner'>
  144. <FocusAdornerTemplate>
  145. <Rectangle Stroke='Black'
  146. StrokeThickness='1'
  147. StrokeDashArray='1,2'/>
  148. </FocusAdornerTemplate>
  149. </Setter>
  150. </Style>
  151. </Window.Styles>
  152. <TextBlock Name='target'/>
  153. </Window>";
  154. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  155. var target = window.Find<TextBlock>("target");
  156. Assert.NotNull(target.FocusAdorner);
  157. }
  158. }
  159. [Fact]
  160. public void Setter_Can_Set_Attached_Property()
  161. {
  162. using (UnitTestApplication.Start(TestServices.StyledWindow))
  163. {
  164. var xaml = @"
  165. <Window xmlns='https://github.com/avaloniaui'
  166. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  167. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  168. <Window.Styles>
  169. <Style Selector='TextBlock'>
  170. <Setter Property='DockPanel.Dock' Value='Right'/>
  171. </Style>
  172. </Window.Styles>
  173. <TextBlock/>
  174. </Window>";
  175. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  176. var textBlock = (TextBlock)window.Content;
  177. window.ApplyTemplate();
  178. Assert.Equal(Dock.Right, DockPanel.GetDock(textBlock));
  179. }
  180. }
  181. [Fact(Skip = "The animation system currently needs to be able to set any property on any object")]
  182. public void Disallows_Setting_Non_Registered_Property()
  183. {
  184. using (UnitTestApplication.Start(TestServices.StyledWindow))
  185. {
  186. var xaml = @"
  187. <Window xmlns='https://github.com/avaloniaui'
  188. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  189. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
  190. <Window.Styles>
  191. <Style Selector='TextBlock'>
  192. <Setter Property='Button.IsDefault' Value='True'/>
  193. </Style>
  194. </Window.Styles>
  195. <TextBlock/>
  196. </Window>";
  197. var ex = Assert.Throws<XmlException>(() => AvaloniaRuntimeXamlLoader.Load(xaml));
  198. Assert.Equal(
  199. "Property 'Button.IsDefault' is not registered on 'Avalonia.Controls.TextBlock'.",
  200. ex.InnerException.Message);
  201. }
  202. }
  203. [Fact]
  204. public void Style_Can_Use_Not_Selector()
  205. {
  206. using (UnitTestApplication.Start(TestServices.StyledWindow))
  207. {
  208. var xaml = @"
  209. <Window xmlns='https://github.com/avaloniaui'
  210. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  211. <Window.Styles>
  212. <Style Selector='Border:not(.foo)'>
  213. <Setter Property='Background' Value='Red'/>
  214. </Style>
  215. </Window.Styles>
  216. <StackPanel>
  217. <Border Name='foo' Classes='foo bar'/>
  218. <Border Name='notFoo' Classes='bar'/>
  219. </StackPanel>
  220. </Window>";
  221. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  222. var foo = window.FindControl<Border>("foo");
  223. var notFoo = window.FindControl<Border>("notFoo");
  224. Assert.Null(foo.Background);
  225. Assert.Equal(Colors.Red, ((ISolidColorBrush)notFoo.Background).Color);
  226. }
  227. }
  228. [Fact]
  229. public void Style_Can_Use_NthChild_Selector()
  230. {
  231. using (UnitTestApplication.Start(TestServices.StyledWindow))
  232. {
  233. var xaml = @"
  234. <Window xmlns='https://github.com/avaloniaui'
  235. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  236. <Window.Styles>
  237. <Style Selector='Border.foo:nth-child(2n+1)'>
  238. <Setter Property='Background' Value='Red'/>
  239. </Style>
  240. </Window.Styles>
  241. <StackPanel>
  242. <Border x:Name='b1' Classes='foo'/>
  243. <Border x:Name='b2' />
  244. </StackPanel>
  245. </Window>";
  246. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  247. var b1 = window.FindControl<Border>("b1");
  248. var b2 = window.FindControl<Border>("b2");
  249. Assert.Equal(Brushes.Red, b1.Background);
  250. Assert.Null(b2.Background);
  251. }
  252. }
  253. [Fact]
  254. public void Style_Can_Use_NthChild_Selector_After_Reorder()
  255. {
  256. using (UnitTestApplication.Start(TestServices.StyledWindow))
  257. {
  258. var xaml = @"
  259. <Window xmlns='https://github.com/avaloniaui'
  260. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  261. <Window.Styles>
  262. <Style Selector='Border:nth-child(2n)'>
  263. <Setter Property='Background' Value='Red'/>
  264. </Style>
  265. </Window.Styles>
  266. <StackPanel x:Name='parent'>
  267. <Border x:Name='b1' />
  268. <Border x:Name='b2' />
  269. </StackPanel>
  270. </Window>";
  271. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  272. var parent = window.FindControl<StackPanel>("parent");
  273. var b1 = window.FindControl<Border>("b1");
  274. var b2 = window.FindControl<Border>("b2");
  275. Assert.Null(b1.Background);
  276. Assert.Equal(Brushes.Red, b2.Background);
  277. parent.Children.Remove(b1);
  278. Assert.Null(b1.Background);
  279. Assert.Null(b2.Background);
  280. parent.Children.Add(b1);
  281. Assert.Equal(Brushes.Red, b1.Background);
  282. Assert.Null(b2.Background);
  283. }
  284. }
  285. [Fact]
  286. public void Style_Can_Use_NthLastChild_Selector_After_Reorder()
  287. {
  288. using (UnitTestApplication.Start(TestServices.StyledWindow))
  289. {
  290. var xaml = @"
  291. <Window xmlns='https://github.com/avaloniaui'
  292. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  293. <Window.Styles>
  294. <Style Selector='Border:nth-last-child(2n)'>
  295. <Setter Property='Background' Value='Red'/>
  296. </Style>
  297. </Window.Styles>
  298. <StackPanel x:Name='parent'>
  299. <Border x:Name='b1' />
  300. <Border x:Name='b2' />
  301. </StackPanel>
  302. </Window>";
  303. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  304. var parent = window.FindControl<StackPanel>("parent");
  305. var b1 = window.FindControl<Border>("b1");
  306. var b2 = window.FindControl<Border>("b2");
  307. Assert.Equal(Brushes.Red, b1.Background);
  308. Assert.Null(b2.Background);
  309. parent.Children.Remove(b1);
  310. Assert.Null(b1.Background);
  311. Assert.Null(b2.Background);
  312. parent.Children.Add(b1);
  313. Assert.Null(b1.Background);
  314. Assert.Equal(Brushes.Red, b2.Background);
  315. }
  316. }
  317. [Fact]
  318. public void Style_Can_Use_NthChild_Selector_With_ListBox()
  319. {
  320. using (UnitTestApplication.Start(TestServices.StyledWindow))
  321. {
  322. var xaml = @"
  323. <Window xmlns='https://github.com/avaloniaui'
  324. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  325. <Window.Styles>
  326. <Style Selector='ListBoxItem:nth-child(2n)'>
  327. <Setter Property='Background' Value='{Binding}'/>
  328. </Style>
  329. </Window.Styles>
  330. <ListBox x:Name='list' />
  331. </Window>";
  332. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  333. var collection = new ObservableCollection<IBrush>()
  334. {
  335. Brushes.Red, Brushes.Green, Brushes.Blue
  336. };
  337. var list = window.FindControl<ListBox>("list");
  338. list.ItemsSource = collection;
  339. window.Show();
  340. IEnumerable<IBrush> GetColors() => list.GetRealizedContainers().Cast<ListBoxItem>().Select(t => t.Background);
  341. Assert.Equal(new[] { Brushes.Transparent, Brushes.Green, Brushes.Transparent }, GetColors());
  342. collection.Remove(Brushes.Green);
  343. window.LayoutManager.ExecuteLayoutPass();
  344. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue }, GetColors());
  345. collection.Add(Brushes.Violet);
  346. collection.Add(Brushes.Black);
  347. window.LayoutManager.ExecuteLayoutPass();
  348. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue, Brushes.Transparent, Brushes.Black }, GetColors());
  349. }
  350. }
  351. [Fact]
  352. public void Style_Can_Use_NthChild_Selector_With_ItemsRepeater()
  353. {
  354. using (UnitTestApplication.Start(TestServices.StyledWindow))
  355. {
  356. var xaml = @"
  357. <Window xmlns='https://github.com/avaloniaui'
  358. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  359. <Window.Styles>
  360. <Style Selector='TextBlock'>
  361. <Setter Property='Foreground' Value='Transparent'/>
  362. </Style>
  363. <Style Selector='TextBlock:nth-child(2n)'>
  364. <Setter Property='Foreground' Value='{Binding}'/>
  365. </Style>
  366. </Window.Styles>
  367. <ItemsRepeater x:Name='list' />
  368. </Window>";
  369. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  370. var collection = new ObservableCollection<IBrush>()
  371. {
  372. Brushes.Red, Brushes.Green, Brushes.Blue
  373. };
  374. var list = window.FindControl<ItemsRepeater>("list");
  375. list.Items = collection;
  376. window.Show();
  377. IEnumerable<IBrush> GetColors() => Enumerable.Range(0, list.ItemsSourceView.Count)
  378. .Select(t => (list.GetOrCreateElement(t) as TextBlock)!.Foreground);
  379. Assert.Equal(new[] { Brushes.Transparent, Brushes.Green, Brushes.Transparent }, GetColors());
  380. collection.Remove(Brushes.Green);
  381. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue }, GetColors().ToList());
  382. collection.Add(Brushes.Violet);
  383. collection.Add(Brushes.Black);
  384. Assert.Equal(new[] { Brushes.Transparent, Brushes.Blue, Brushes.Transparent, Brushes.Black }, GetColors());
  385. }
  386. }
  387. [Fact]
  388. public void Style_Can_Use_Or_Selector_1()
  389. {
  390. using (UnitTestApplication.Start(TestServices.StyledWindow))
  391. {
  392. var xaml = @"
  393. <Window xmlns='https://github.com/avaloniaui'
  394. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  395. <Window.Styles>
  396. <Style Selector='Border.foo, Border.bar'>
  397. <Setter Property='Background' Value='Red'/>
  398. </Style>
  399. </Window.Styles>
  400. <StackPanel>
  401. <Border Name='foo' Classes='foo'/>
  402. <Border Name='bar' Classes='bar'/>
  403. <Border Name='baz' Classes='baz'/>
  404. </StackPanel>
  405. </Window>";
  406. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  407. var foo = window.FindControl<Border>("foo");
  408. var bar = window.FindControl<Border>("bar");
  409. var baz = window.FindControl<Border>("baz");
  410. Assert.Equal(Brushes.Red, foo.Background);
  411. Assert.Equal(Brushes.Red, bar.Background);
  412. Assert.Null(baz.Background);
  413. }
  414. }
  415. [Fact]
  416. public void Style_Can_Use_Or_Selector_2()
  417. {
  418. using (UnitTestApplication.Start(TestServices.StyledWindow))
  419. {
  420. var xaml = @"
  421. <Window xmlns='https://github.com/avaloniaui'
  422. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  423. <Window.Styles>
  424. <Style Selector='Button,Carousel,ListBox'>
  425. <Setter Property='Background' Value='Red'/>
  426. </Style>
  427. </Window.Styles>
  428. <StackPanel>
  429. <Button Name='button'/>
  430. <Carousel Name='carousel'/>
  431. <ListBox Name='listBox'/>
  432. </StackPanel>
  433. </Window>";
  434. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  435. var button = window.FindControl<Button>("button");
  436. var carousel = window.FindControl<Carousel>("carousel");
  437. var listBox = window.FindControl<ListBox>("listBox");
  438. Assert.Equal(Brushes.Red, button.Background);
  439. Assert.Equal(Brushes.Red, carousel.Background);
  440. Assert.Equal(Brushes.Red, listBox.Background);
  441. }
  442. }
  443. [Fact]
  444. public void Transitions_Can_Be_Styled()
  445. {
  446. using (UnitTestApplication.Start(TestServices.StyledWindow))
  447. {
  448. var xaml = @"
  449. <Window xmlns='https://github.com/avaloniaui'
  450. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  451. <Window.Styles>
  452. <Style Selector='Border'>
  453. <Setter Property='Transitions'>
  454. <Transitions>
  455. <DoubleTransition Property='Width' Duration='0:0:1'/>
  456. </Transitions>
  457. </Setter>
  458. </Style>
  459. <Style Selector='Border.foo'>
  460. <Setter Property='Transitions'>
  461. <Transitions>
  462. <DoubleTransition Property='Height' Duration='0:0:1'/>
  463. </Transitions>
  464. </Setter>
  465. </Style>
  466. </Window.Styles>
  467. <Border/>
  468. </Window>";
  469. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  470. var border = (Border)window.Content;
  471. Assert.Equal(1, border.Transitions.Count);
  472. Assert.Equal(Border.WidthProperty, border.Transitions[0].Property);
  473. border.Classes.Add("foo");
  474. Assert.Equal(1, border.Transitions.Count);
  475. Assert.Equal(Border.HeightProperty, border.Transitions[0].Property);
  476. border.Classes.Remove("foo");
  477. Assert.Equal(1, border.Transitions.Count);
  478. Assert.Equal(Border.WidthProperty, border.Transitions[0].Property);
  479. }
  480. }
  481. [Fact]
  482. public void Style_Can_Use_Class_Selector_With_Dash()
  483. {
  484. using (UnitTestApplication.Start(TestServices.StyledWindow))
  485. {
  486. var xaml = @"
  487. <Window xmlns='https://github.com/avaloniaui'
  488. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  489. <Window.Styles>
  490. <Style Selector='Border.foo-bar'>
  491. <Setter Property='Background' Value='Red'/>
  492. </Style>
  493. </Window.Styles>
  494. <StackPanel>
  495. <Border Name='foo' Classes='foo-bar'/>
  496. </StackPanel>
  497. </Window>";
  498. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  499. var foo = window.FindControl<Border>("foo");
  500. Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color);
  501. }
  502. }
  503. [Fact]
  504. public void Style_Can_Use_Pseudolass_Selector_With_Dash()
  505. {
  506. using (UnitTestApplication.Start(TestServices.StyledWindow))
  507. {
  508. var xaml = @"
  509. <Window xmlns='https://github.com/avaloniaui'
  510. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  511. <Window.Styles>
  512. <Style Selector='Border:foo-bar'>
  513. <Setter Property='Background' Value='Red'/>
  514. </Style>
  515. </Window.Styles>
  516. <StackPanel>
  517. <Border Name='foo'/>
  518. </StackPanel>
  519. </Window>";
  520. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  521. var foo = window.FindControl<Border>("foo");
  522. Assert.Null(foo.Background);
  523. ((IPseudoClasses)foo.Classes).Add(":foo-bar");
  524. Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color);
  525. }
  526. }
  527. [Fact]
  528. public void Can_Use_Nested_Styles()
  529. {
  530. using (UnitTestApplication.Start(TestServices.StyledWindow))
  531. {
  532. var xaml = @"
  533. <Window xmlns='https://github.com/avaloniaui'
  534. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  535. <Window.Styles>
  536. <Style Selector='Border'>
  537. <Style Selector='^.foo'>
  538. <Setter Property='Background' Value='Red'/>
  539. </Style>
  540. </Style>
  541. </Window.Styles>
  542. <StackPanel>
  543. <Border Name='foo'/>
  544. </StackPanel>
  545. </Window>";
  546. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  547. var foo = window.FindControl<Border>("foo");
  548. Assert.Null(foo.Background);
  549. foo.Classes.Add("foo");
  550. Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color);
  551. }
  552. }
  553. }
  554. }