StyleTests.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Data;
  6. using Avalonia.Styling;
  7. using Avalonia.UnitTests;
  8. using Moq;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.Styling
  11. {
  12. public class StyleTests
  13. {
  14. [Fact]
  15. public void Style_With_Only_Type_Selector_Should_Update_Value()
  16. {
  17. Style style = new Style(x => x.OfType<Class1>())
  18. {
  19. Setters =
  20. {
  21. new Setter(Class1.FooProperty, "Foo"),
  22. },
  23. };
  24. var target = new Class1();
  25. style.TryAttach(target, null);
  26. Assert.Equal("Foo", target.Foo);
  27. }
  28. [Fact]
  29. public void Style_With_Class_Selector_Should_Update_And_Restore_Value()
  30. {
  31. Style style = new Style(x => x.OfType<Class1>().Class("foo"))
  32. {
  33. Setters =
  34. {
  35. new Setter(Class1.FooProperty, "Foo"),
  36. },
  37. };
  38. var target = new Class1();
  39. style.TryAttach(target, null);
  40. Assert.Equal("foodefault", target.Foo);
  41. target.Classes.Add("foo");
  42. Assert.Equal("Foo", target.Foo);
  43. target.Classes.Remove("foo");
  44. Assert.Equal("foodefault", target.Foo);
  45. }
  46. [Fact]
  47. public void Style_With_No_Selector_Should_Apply_To_Containing_Control()
  48. {
  49. Style style = new Style
  50. {
  51. Setters =
  52. {
  53. new Setter(Class1.FooProperty, "Foo"),
  54. },
  55. };
  56. var target = new Class1();
  57. style.TryAttach(target, target);
  58. Assert.Equal("Foo", target.Foo);
  59. }
  60. [Fact]
  61. public void Style_With_No_Selector_Should_Not_Apply_To_Other_Control()
  62. {
  63. Style style = new Style
  64. {
  65. Setters =
  66. {
  67. new Setter(Class1.FooProperty, "Foo"),
  68. },
  69. };
  70. var target = new Class1();
  71. var other = new Class1();
  72. style.TryAttach(target, other);
  73. Assert.Equal("foodefault", target.Foo);
  74. }
  75. [Fact]
  76. public void LocalValue_Should_Override_Style()
  77. {
  78. Style style = new Style(x => x.OfType<Class1>())
  79. {
  80. Setters =
  81. {
  82. new Setter(Class1.FooProperty, "Foo"),
  83. },
  84. };
  85. var target = new Class1
  86. {
  87. Foo = "Original",
  88. };
  89. style.TryAttach(target, null);
  90. Assert.Equal("Original", target.Foo);
  91. }
  92. [Fact]
  93. public void Later_Styles_Should_Override_Earlier()
  94. {
  95. Styles styles = new Styles
  96. {
  97. new Style(x => x.OfType<Class1>().Class("foo"))
  98. {
  99. Setters =
  100. {
  101. new Setter(Class1.FooProperty, "Foo"),
  102. },
  103. },
  104. new Style(x => x.OfType<Class1>().Class("foo"))
  105. {
  106. Setters =
  107. {
  108. new Setter(Class1.FooProperty, "Bar"),
  109. },
  110. }
  111. };
  112. var target = new Class1();
  113. List<string> values = new List<string>();
  114. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  115. styles.TryAttach(target, null);
  116. target.Classes.Add("foo");
  117. target.Classes.Remove("foo");
  118. Assert.Equal(new[] { "foodefault", "Foo", "Bar", "foodefault" }, values);
  119. }
  120. [Fact]
  121. public void Later_Styles_Should_Override_Earlier_2()
  122. {
  123. Styles styles = new Styles
  124. {
  125. new Style(x => x.OfType<Class1>().Class("foo"))
  126. {
  127. Setters =
  128. {
  129. new Setter(Class1.FooProperty, "Foo"),
  130. },
  131. },
  132. new Style(x => x.OfType<Class1>().Class("bar"))
  133. {
  134. Setters =
  135. {
  136. new Setter(Class1.FooProperty, "Bar"),
  137. },
  138. }
  139. };
  140. var target = new Class1();
  141. List<string> values = new List<string>();
  142. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  143. styles.TryAttach(target, null);
  144. target.Classes.Add("bar");
  145. target.Classes.Add("foo");
  146. target.Classes.Remove("foo");
  147. Assert.Equal(new[] { "foodefault", "Bar" }, values);
  148. }
  149. [Fact]
  150. public void Later_Styles_Should_Override_Earlier_3()
  151. {
  152. Styles styles = new Styles
  153. {
  154. new Style(x => x.OfType<Class1>().Class("foo"))
  155. {
  156. Setters =
  157. {
  158. new Setter(Class1.FooProperty, new Binding("Foo")),
  159. },
  160. },
  161. new Style(x => x.OfType<Class1>().Class("bar"))
  162. {
  163. Setters =
  164. {
  165. new Setter(Class1.FooProperty, new Binding("Bar")),
  166. },
  167. }
  168. };
  169. var target = new Class1
  170. {
  171. DataContext = new
  172. {
  173. Foo = "Foo",
  174. Bar = "Bar",
  175. }
  176. };
  177. List<string> values = new List<string>();
  178. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  179. styles.TryAttach(target, null);
  180. target.Classes.Add("bar");
  181. target.Classes.Add("foo");
  182. target.Classes.Remove("foo");
  183. Assert.Equal(new[] { "foodefault", "Bar" }, values);
  184. }
  185. [Fact]
  186. public void Inactive_Values_Should_Not_Be_Made_Active_During_Style_Attach()
  187. {
  188. using var app = UnitTestApplication.Start(TestServices.RealStyler);
  189. var root = new TestRoot
  190. {
  191. Styles =
  192. {
  193. new Style(x => x.OfType<Class1>())
  194. {
  195. Setters =
  196. {
  197. new Setter(Class1.FooProperty, "Foo"),
  198. },
  199. },
  200. new Style(x => x.OfType<Class1>())
  201. {
  202. Setters =
  203. {
  204. new Setter(Class1.FooProperty, "Bar"),
  205. },
  206. }
  207. }
  208. };
  209. var values = new List<string>();
  210. var target = new Class1();
  211. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  212. root.Child = target;
  213. Assert.Equal(new[] { "foodefault", "Bar" }, values);
  214. }
  215. [Fact]
  216. public void Inactive_Bindings_Should_Not_Be_Made_Active_During_Style_Attach()
  217. {
  218. using var app = UnitTestApplication.Start(TestServices.RealStyler);
  219. var root = new TestRoot
  220. {
  221. Styles =
  222. {
  223. new Style(x => x.OfType<Class1>())
  224. {
  225. Setters =
  226. {
  227. new Setter(Class1.FooProperty, new Binding("Foo")),
  228. },
  229. },
  230. new Style(x => x.OfType<Class1>())
  231. {
  232. Setters =
  233. {
  234. new Setter(Class1.FooProperty, new Binding("Bar")),
  235. },
  236. }
  237. }
  238. };
  239. var values = new List<string>();
  240. var target = new Class1
  241. {
  242. DataContext = new
  243. {
  244. Foo = "Foo",
  245. Bar = "Bar",
  246. }
  247. };
  248. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  249. root.Child = target;
  250. Assert.Equal(new[] { "foodefault", "Bar" }, values);
  251. }
  252. [Fact]
  253. public void Inactive_Values_Should_Not_Be_Made_Active_During_Style_Detach()
  254. {
  255. using var app = UnitTestApplication.Start(TestServices.RealStyler);
  256. var root = new TestRoot
  257. {
  258. Styles =
  259. {
  260. new Style(x => x.OfType<Class1>())
  261. {
  262. Setters =
  263. {
  264. new Setter(Class1.FooProperty, "Foo"),
  265. },
  266. },
  267. new Style(x => x.OfType<Class1>())
  268. {
  269. Setters =
  270. {
  271. new Setter(Class1.FooProperty, "Bar"),
  272. },
  273. }
  274. }
  275. };
  276. var target = new Class1();
  277. root.Child = target;
  278. var values = new List<string>();
  279. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  280. root.Child = null;
  281. Assert.Equal(new[] { "Bar", "foodefault" }, values);
  282. }
  283. [Fact]
  284. public void Inactive_Values_Should_Not_Be_Made_Active_During_Style_Detach_2()
  285. {
  286. using var app = UnitTestApplication.Start(TestServices.RealStyler);
  287. var root = new TestRoot
  288. {
  289. Styles =
  290. {
  291. new Style(x => x.OfType<Class1>().Class("foo"))
  292. {
  293. Setters =
  294. {
  295. new Setter(Class1.FooProperty, "Foo"),
  296. },
  297. },
  298. new Style(x => x.OfType<Class1>())
  299. {
  300. Setters =
  301. {
  302. new Setter(Class1.FooProperty, "Bar"),
  303. },
  304. }
  305. }
  306. };
  307. var target = new Class1 { Classes = { "foo" } };
  308. root.Child = target;
  309. var values = new List<string>();
  310. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  311. root.Child = null;
  312. Assert.Equal(new[] { "Foo", "foodefault" }, values);
  313. }
  314. [Fact]
  315. public void Inactive_Bindings_Should_Not_Be_Made_Active_During_Style_Detach()
  316. {
  317. using var app = UnitTestApplication.Start(TestServices.RealStyler);
  318. var root = new TestRoot
  319. {
  320. Styles =
  321. {
  322. new Style(x => x.OfType<Class1>())
  323. {
  324. Setters =
  325. {
  326. new Setter(Class1.FooProperty, new Binding("Foo")),
  327. },
  328. },
  329. new Style(x => x.OfType<Class1>())
  330. {
  331. Setters =
  332. {
  333. new Setter(Class1.FooProperty, new Binding("Bar")),
  334. },
  335. }
  336. }
  337. };
  338. var target = new Class1
  339. {
  340. DataContext = new
  341. {
  342. Foo = "Foo",
  343. Bar = "Bar",
  344. }
  345. };
  346. root.Child = target;
  347. var values = new List<string>();
  348. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  349. root.Child = null;
  350. Assert.Equal(new[] { "Bar", "foodefault" }, values);
  351. }
  352. [Fact]
  353. public void Template_In_Non_Matching_Style_Is_Not_Built()
  354. {
  355. var instantiationCount = 0;
  356. var template = new FuncTemplate<Class1>(() =>
  357. {
  358. ++instantiationCount;
  359. return new Class1();
  360. });
  361. Styles styles = new Styles
  362. {
  363. new Style(x => x.OfType<Class1>().Class("foo"))
  364. {
  365. Setters =
  366. {
  367. new Setter(Class1.ChildProperty, template),
  368. },
  369. },
  370. new Style(x => x.OfType<Class1>())
  371. {
  372. Setters =
  373. {
  374. new Setter(Class1.ChildProperty, template),
  375. },
  376. }
  377. };
  378. var target = new Class1();
  379. styles.TryAttach(target, null);
  380. Assert.NotNull(target.Child);
  381. Assert.Equal(1, instantiationCount);
  382. }
  383. [Fact]
  384. public void Template_In_Inactive_Style_Is_Not_Built()
  385. {
  386. var instantiationCount = 0;
  387. var template = new FuncTemplate<Class1>(() =>
  388. {
  389. ++instantiationCount;
  390. return new Class1();
  391. });
  392. Styles styles = new Styles
  393. {
  394. new Style(x => x.OfType<Class1>())
  395. {
  396. Setters =
  397. {
  398. new Setter(Class1.ChildProperty, template),
  399. },
  400. },
  401. new Style(x => x.OfType<Class1>())
  402. {
  403. Setters =
  404. {
  405. new Setter(Class1.ChildProperty, template),
  406. },
  407. }
  408. };
  409. var target = new Class1();
  410. target.BeginBatchUpdate();
  411. styles.TryAttach(target, null);
  412. target.EndBatchUpdate();
  413. Assert.NotNull(target.Child);
  414. Assert.Equal(1, instantiationCount);
  415. }
  416. [Fact]
  417. public void Style_Should_Detach_When_Control_Removed_From_Logical_Tree()
  418. {
  419. Border border;
  420. var style = new Style(x => x.OfType<Border>())
  421. {
  422. Setters =
  423. {
  424. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  425. }
  426. };
  427. var root = new TestRoot
  428. {
  429. Child = border = new Border(),
  430. };
  431. style.TryAttach(border, null);
  432. Assert.Equal(new Thickness(4), border.BorderThickness);
  433. root.Child = null;
  434. Assert.Equal(new Thickness(0), border.BorderThickness);
  435. }
  436. [Fact]
  437. public void Removing_Style_Should_Detach_From_Control()
  438. {
  439. using (UnitTestApplication.Start(TestServices.RealStyler))
  440. {
  441. var border = new Border();
  442. var root = new TestRoot
  443. {
  444. Styles =
  445. {
  446. new Style(x => x.OfType<Border>())
  447. {
  448. Setters =
  449. {
  450. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  451. }
  452. }
  453. },
  454. Child = border,
  455. };
  456. root.Measure(Size.Infinity);
  457. Assert.Equal(new Thickness(4), border.BorderThickness);
  458. root.Styles.RemoveAt(0);
  459. Assert.Equal(new Thickness(0), border.BorderThickness);
  460. }
  461. }
  462. [Fact]
  463. public void Adding_Style_Should_Attach_To_Control()
  464. {
  465. using (UnitTestApplication.Start(TestServices.RealStyler))
  466. {
  467. var border = new Border();
  468. var root = new TestRoot
  469. {
  470. Styles =
  471. {
  472. new Style(x => x.OfType<Border>())
  473. {
  474. Setters =
  475. {
  476. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  477. }
  478. }
  479. },
  480. Child = border,
  481. };
  482. root.Measure(Size.Infinity);
  483. Assert.Equal(new Thickness(4), border.BorderThickness);
  484. root.Styles.Add(new Style(x => x.OfType<Border>())
  485. {
  486. Setters =
  487. {
  488. new Setter(Border.BorderThicknessProperty, new Thickness(6)),
  489. }
  490. });
  491. root.Measure(Size.Infinity);
  492. Assert.Equal(new Thickness(6), border.BorderThickness);
  493. }
  494. }
  495. [Fact]
  496. public void Removing_Style_With_Nested_Style_Should_Detach_From_Control()
  497. {
  498. using (UnitTestApplication.Start(TestServices.RealStyler))
  499. {
  500. var border = new Border();
  501. var root = new TestRoot
  502. {
  503. Styles =
  504. {
  505. new Styles
  506. {
  507. new Style(x => x.OfType<Border>())
  508. {
  509. Setters =
  510. {
  511. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  512. }
  513. }
  514. }
  515. },
  516. Child = border,
  517. };
  518. root.Measure(Size.Infinity);
  519. Assert.Equal(new Thickness(4), border.BorderThickness);
  520. root.Styles.RemoveAt(0);
  521. Assert.Equal(new Thickness(0), border.BorderThickness);
  522. }
  523. }
  524. [Fact]
  525. public void Adding_Nested_Style_Should_Attach_To_Control()
  526. {
  527. using (UnitTestApplication.Start(TestServices.RealStyler))
  528. {
  529. var border = new Border();
  530. var root = new TestRoot
  531. {
  532. Styles =
  533. {
  534. new Styles
  535. {
  536. new Style(x => x.OfType<Border>())
  537. {
  538. Setters =
  539. {
  540. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  541. }
  542. }
  543. }
  544. },
  545. Child = border,
  546. };
  547. root.Measure(Size.Infinity);
  548. Assert.Equal(new Thickness(4), border.BorderThickness);
  549. ((Styles)root.Styles[0]).Add(new Style(x => x.OfType<Border>())
  550. {
  551. Setters =
  552. {
  553. new Setter(Border.BorderThicknessProperty, new Thickness(6)),
  554. }
  555. });
  556. root.Measure(Size.Infinity);
  557. Assert.Equal(new Thickness(6), border.BorderThickness);
  558. }
  559. }
  560. [Fact]
  561. public void Removing_Nested_Style_Should_Detach_From_Control()
  562. {
  563. using (UnitTestApplication.Start(TestServices.RealStyler))
  564. {
  565. var border = new Border();
  566. var root = new TestRoot
  567. {
  568. Styles =
  569. {
  570. new Styles
  571. {
  572. new Style(x => x.OfType<Border>())
  573. {
  574. Setters =
  575. {
  576. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  577. }
  578. },
  579. new Style(x => x.OfType<Border>())
  580. {
  581. Setters =
  582. {
  583. new Setter(Border.BorderThicknessProperty, new Thickness(6)),
  584. }
  585. },
  586. }
  587. },
  588. Child = border,
  589. };
  590. root.Measure(Size.Infinity);
  591. Assert.Equal(new Thickness(6), border.BorderThickness);
  592. ((Styles)root.Styles[0]).RemoveAt(1);
  593. root.Measure(Size.Infinity);
  594. Assert.Equal(new Thickness(4), border.BorderThickness);
  595. }
  596. }
  597. [Fact]
  598. public void Should_Set_Owner_On_Assigned_Resources()
  599. {
  600. var host = new Mock<IResourceHost>();
  601. var target = new Style();
  602. ((IResourceProvider)target).AddOwner(host.Object);
  603. var resources = new Mock<IResourceDictionary>();
  604. target.Resources = resources.Object;
  605. resources.Verify(x => x.AddOwner(host.Object), Times.Once);
  606. }
  607. [Fact]
  608. public void Should_Set_Owner_On_Assigned_Resources_2()
  609. {
  610. var host = new Mock<IResourceHost>();
  611. var target = new Style();
  612. var resources = new Mock<IResourceDictionary>();
  613. target.Resources = resources.Object;
  614. host.Invocations.Clear();
  615. ((IResourceProvider)target).AddOwner(host.Object);
  616. resources.Verify(x => x.AddOwner(host.Object), Times.Once);
  617. }
  618. [Fact]
  619. public void Nested_Style_Can_Be_Added()
  620. {
  621. var parent = new Style(x => x.OfType<Class1>());
  622. var nested = new Style(x => x.Nesting().Class("foo"));
  623. parent.Children.Add(nested);
  624. Assert.Same(parent, nested.Parent);
  625. }
  626. [Fact]
  627. public void Nested_Or_Style_Can_Be_Added()
  628. {
  629. var parent = new Style(x => x.OfType<Class1>());
  630. var nested = new Style(x => Selectors.Or(
  631. x.Nesting().Class("foo"),
  632. x.Nesting().Class("bar")));
  633. parent.Children.Add(nested);
  634. Assert.Same(parent, nested.Parent);
  635. }
  636. [Fact]
  637. public void Nested_Style_Without_Selector_Throws()
  638. {
  639. var parent = new Style(x => x.OfType<Class1>());
  640. var nested = new Style();
  641. Assert.Throws<InvalidOperationException>(() => parent.Children.Add(nested));
  642. }
  643. [Fact(Skip = "TODO")]
  644. public void Nested_Style_Without_Nesting_Operator_Throws()
  645. {
  646. var parent = new Style(x => x.OfType<Class1>());
  647. var nested = new Style(x => x.Class("foo"));
  648. Assert.Throws<InvalidOperationException>(() => parent.Children.Add(nested));
  649. }
  650. private class Class1 : Control
  651. {
  652. public static readonly StyledProperty<string> FooProperty =
  653. AvaloniaProperty.Register<Class1, string>(nameof(Foo), "foodefault");
  654. public static readonly StyledProperty<Class1> ChildProperty =
  655. AvaloniaProperty.Register<Class1, Class1>(nameof(Child));
  656. public string Foo
  657. {
  658. get { return GetValue(FooProperty); }
  659. set { SetValue(FooProperty, value); }
  660. }
  661. public Class1 Child
  662. {
  663. get => GetValue(ChildProperty);
  664. set => SetValue(ChildProperty, value);
  665. }
  666. protected override Size MeasureOverride(Size availableSize)
  667. {
  668. throw new NotImplementedException();
  669. }
  670. }
  671. }
  672. }