StyleTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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.Collections.Generic;
  5. using Avalonia.Controls;
  6. using Avalonia.Data;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. namespace Avalonia.Styling.UnitTests
  10. {
  11. public class StyleTests
  12. {
  13. [Fact]
  14. public void Style_With_Only_Type_Selector_Should_Update_Value()
  15. {
  16. Style style = new Style(x => x.OfType<Class1>())
  17. {
  18. Setters =
  19. {
  20. new Setter(Class1.FooProperty, "Foo"),
  21. },
  22. };
  23. var target = new Class1();
  24. style.TryAttach(target, null);
  25. Assert.Equal("Foo", target.Foo);
  26. }
  27. [Fact]
  28. public void Style_With_Class_Selector_Should_Update_And_Restore_Value()
  29. {
  30. Style style = new Style(x => x.OfType<Class1>().Class("foo"))
  31. {
  32. Setters =
  33. {
  34. new Setter(Class1.FooProperty, "Foo"),
  35. },
  36. };
  37. var target = new Class1();
  38. style.TryAttach(target, null);
  39. Assert.Equal("foodefault", target.Foo);
  40. target.Classes.Add("foo");
  41. Assert.Equal("Foo", target.Foo);
  42. target.Classes.Remove("foo");
  43. Assert.Equal("foodefault", target.Foo);
  44. }
  45. [Fact]
  46. public void Style_With_No_Selector_Should_Apply_To_Containing_Control()
  47. {
  48. Style style = new Style
  49. {
  50. Setters =
  51. {
  52. new Setter(Class1.FooProperty, "Foo"),
  53. },
  54. };
  55. var target = new Class1();
  56. style.TryAttach(target, target);
  57. Assert.Equal("Foo", target.Foo);
  58. }
  59. [Fact]
  60. public void Style_With_No_Selector_Should_Not_Apply_To_Other_Control()
  61. {
  62. Style style = new Style
  63. {
  64. Setters =
  65. {
  66. new Setter(Class1.FooProperty, "Foo"),
  67. },
  68. };
  69. var target = new Class1();
  70. var other = new Class1();
  71. style.TryAttach(target, other);
  72. Assert.Equal("foodefault", target.Foo);
  73. }
  74. [Fact]
  75. public void LocalValue_Should_Override_Style()
  76. {
  77. Style style = new Style(x => x.OfType<Class1>())
  78. {
  79. Setters =
  80. {
  81. new Setter(Class1.FooProperty, "Foo"),
  82. },
  83. };
  84. var target = new Class1
  85. {
  86. Foo = "Original",
  87. };
  88. style.TryAttach(target, null);
  89. Assert.Equal("Original", target.Foo);
  90. }
  91. [Fact]
  92. public void Later_Styles_Should_Override_Earlier()
  93. {
  94. Styles styles = new Styles
  95. {
  96. new Style(x => x.OfType<Class1>().Class("foo"))
  97. {
  98. Setters =
  99. {
  100. new Setter(Class1.FooProperty, "Foo"),
  101. },
  102. },
  103. new Style(x => x.OfType<Class1>().Class("foo"))
  104. {
  105. Setters =
  106. {
  107. new Setter(Class1.FooProperty, "Bar"),
  108. },
  109. }
  110. };
  111. var target = new Class1();
  112. List<string> values = new List<string>();
  113. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  114. styles.TryAttach(target, null);
  115. target.Classes.Add("foo");
  116. target.Classes.Remove("foo");
  117. Assert.Equal(new[] { "foodefault", "Foo", "Bar", "foodefault" }, values);
  118. }
  119. [Fact]
  120. public void Later_Styles_Should_Override_Earlier_2()
  121. {
  122. Styles styles = new Styles
  123. {
  124. new Style(x => x.OfType<Class1>().Class("foo"))
  125. {
  126. Setters =
  127. {
  128. new Setter(Class1.FooProperty, "Foo"),
  129. },
  130. },
  131. new Style(x => x.OfType<Class1>().Class("bar"))
  132. {
  133. Setters =
  134. {
  135. new Setter(Class1.FooProperty, "Bar"),
  136. },
  137. }
  138. };
  139. var target = new Class1();
  140. List<string> values = new List<string>();
  141. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  142. styles.TryAttach(target, null);
  143. target.Classes.Add("bar");
  144. target.Classes.Add("foo");
  145. target.Classes.Remove("foo");
  146. Assert.Equal(new[] { "foodefault", "Bar" }, values);
  147. }
  148. [Fact]
  149. public void Later_Styles_Should_Override_Earlier_3()
  150. {
  151. Styles styles = new Styles
  152. {
  153. new Style(x => x.OfType<Class1>().Class("foo"))
  154. {
  155. Setters =
  156. {
  157. new Setter(Class1.FooProperty, new Binding("Foo")),
  158. },
  159. },
  160. new Style(x => x.OfType<Class1>().Class("bar"))
  161. {
  162. Setters =
  163. {
  164. new Setter(Class1.FooProperty, new Binding("Bar")),
  165. },
  166. }
  167. };
  168. var target = new Class1
  169. {
  170. DataContext = new
  171. {
  172. Foo = "Foo",
  173. Bar = "Bar",
  174. }
  175. };
  176. List<string> values = new List<string>();
  177. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  178. styles.TryAttach(target, null);
  179. target.Classes.Add("bar");
  180. target.Classes.Add("foo");
  181. target.Classes.Remove("foo");
  182. Assert.Equal(new[] { "foodefault", "Bar" }, values);
  183. }
  184. [Fact]
  185. public void Style_Should_Detach_When_Control_Removed_From_Logical_Tree()
  186. {
  187. Border border;
  188. var style = new Style(x => x.OfType<Border>())
  189. {
  190. Setters =
  191. {
  192. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  193. }
  194. };
  195. var root = new TestRoot
  196. {
  197. Child = border = new Border(),
  198. };
  199. style.TryAttach(border, null);
  200. Assert.Equal(new Thickness(4), border.BorderThickness);
  201. root.Child = null;
  202. Assert.Equal(new Thickness(0), border.BorderThickness);
  203. }
  204. [Fact]
  205. public void Removing_Style_Should_Detach_From_Control()
  206. {
  207. using (UnitTestApplication.Start(TestServices.RealStyler))
  208. {
  209. var border = new Border();
  210. var root = new TestRoot
  211. {
  212. Styles =
  213. {
  214. new Style(x => x.OfType<Border>())
  215. {
  216. Setters =
  217. {
  218. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  219. }
  220. }
  221. },
  222. Child = border,
  223. };
  224. root.Measure(Size.Infinity);
  225. Assert.Equal(new Thickness(4), border.BorderThickness);
  226. root.Styles.RemoveAt(0);
  227. Assert.Equal(new Thickness(0), border.BorderThickness);
  228. }
  229. }
  230. [Fact]
  231. public void Adding_Style_Should_Attach_To_Control()
  232. {
  233. using (UnitTestApplication.Start(TestServices.RealStyler))
  234. {
  235. var border = new Border();
  236. var root = new TestRoot
  237. {
  238. Styles =
  239. {
  240. new Style(x => x.OfType<Border>())
  241. {
  242. Setters =
  243. {
  244. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  245. }
  246. }
  247. },
  248. Child = border,
  249. };
  250. root.Measure(Size.Infinity);
  251. Assert.Equal(new Thickness(4), border.BorderThickness);
  252. root.Styles.Add(new Style(x => x.OfType<Border>())
  253. {
  254. Setters =
  255. {
  256. new Setter(Border.BorderThicknessProperty, new Thickness(6)),
  257. }
  258. });
  259. root.Measure(Size.Infinity);
  260. Assert.Equal(new Thickness(6), border.BorderThickness);
  261. }
  262. }
  263. [Fact]
  264. public void Removing_Style_With_Nested_Style_Should_Detach_From_Control()
  265. {
  266. using (UnitTestApplication.Start(TestServices.RealStyler))
  267. {
  268. var border = new Border();
  269. var root = new TestRoot
  270. {
  271. Styles =
  272. {
  273. new Styles
  274. {
  275. new Style(x => x.OfType<Border>())
  276. {
  277. Setters =
  278. {
  279. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  280. }
  281. }
  282. }
  283. },
  284. Child = border,
  285. };
  286. root.Measure(Size.Infinity);
  287. Assert.Equal(new Thickness(4), border.BorderThickness);
  288. root.Styles.RemoveAt(0);
  289. Assert.Equal(new Thickness(0), border.BorderThickness);
  290. }
  291. }
  292. [Fact]
  293. public void Adding_Nested_Style_Should_Attach_To_Control()
  294. {
  295. using (UnitTestApplication.Start(TestServices.RealStyler))
  296. {
  297. var border = new Border();
  298. var root = new TestRoot
  299. {
  300. Styles =
  301. {
  302. new Styles
  303. {
  304. new Style(x => x.OfType<Border>())
  305. {
  306. Setters =
  307. {
  308. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  309. }
  310. }
  311. }
  312. },
  313. Child = border,
  314. };
  315. root.Measure(Size.Infinity);
  316. Assert.Equal(new Thickness(4), border.BorderThickness);
  317. ((Styles)root.Styles[0]).Add(new Style(x => x.OfType<Border>())
  318. {
  319. Setters =
  320. {
  321. new Setter(Border.BorderThicknessProperty, new Thickness(6)),
  322. }
  323. });
  324. root.Measure(Size.Infinity);
  325. Assert.Equal(new Thickness(6), border.BorderThickness);
  326. }
  327. }
  328. [Fact]
  329. public void Removing_Nested_Style_Should_Detach_From_Control()
  330. {
  331. using (UnitTestApplication.Start(TestServices.RealStyler))
  332. {
  333. var border = new Border();
  334. var root = new TestRoot
  335. {
  336. Styles =
  337. {
  338. new Styles
  339. {
  340. new Style(x => x.OfType<Border>())
  341. {
  342. Setters =
  343. {
  344. new Setter(Border.BorderThicknessProperty, new Thickness(4)),
  345. }
  346. },
  347. new Style(x => x.OfType<Border>())
  348. {
  349. Setters =
  350. {
  351. new Setter(Border.BorderThicknessProperty, new Thickness(6)),
  352. }
  353. },
  354. }
  355. },
  356. Child = border,
  357. };
  358. root.Measure(Size.Infinity);
  359. Assert.Equal(new Thickness(6), border.BorderThickness);
  360. ((Styles)root.Styles[0]).RemoveAt(1);
  361. root.Measure(Size.Infinity);
  362. Assert.Equal(new Thickness(4), border.BorderThickness);
  363. }
  364. }
  365. private class Class1 : Control
  366. {
  367. public static readonly StyledProperty<string> FooProperty =
  368. AvaloniaProperty.Register<Class1, string>(nameof(Foo), "foodefault");
  369. public string Foo
  370. {
  371. get { return GetValue(FooProperty); }
  372. set { SetValue(FooProperty, value); }
  373. }
  374. protected override Size MeasureOverride(Size availableSize)
  375. {
  376. throw new NotImplementedException();
  377. }
  378. }
  379. }
  380. }