StyledElementTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 System.Reactive.Linq;
  6. using Moq;
  7. using Avalonia.Styling;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. using Avalonia.LogicalTree;
  11. using Avalonia.Controls;
  12. namespace Avalonia.Styling.UnitTests
  13. {
  14. public class StyledElementTests
  15. {
  16. [Fact]
  17. public void Classes_Should_Initially_Be_Empty()
  18. {
  19. var target = new StyledElement();
  20. Assert.Empty(target.Classes);
  21. }
  22. [Fact]
  23. public void Setting_Parent_Should_Also_Set_InheritanceParent()
  24. {
  25. var parent = new Decorator();
  26. var target = new TestControl();
  27. parent.Child = target;
  28. Assert.Equal(parent, target.Parent);
  29. Assert.Equal(parent, target.InheritanceParent);
  30. }
  31. [Fact]
  32. public void Setting_Parent_Should_Not_Set_InheritanceParent_If_Already_Set()
  33. {
  34. var parent = new Decorator();
  35. var inheritanceParent = new Decorator();
  36. var target = new TestControl();
  37. ((ISetInheritanceParent)target).SetParent(inheritanceParent);
  38. parent.Child = target;
  39. Assert.Equal(parent, target.Parent);
  40. Assert.Equal(inheritanceParent, target.InheritanceParent);
  41. }
  42. [Fact]
  43. public void InheritanceParent_Should_Be_Cleared_When_Removed_From_Parent()
  44. {
  45. var parent = new Decorator();
  46. var target = new TestControl();
  47. parent.Child = target;
  48. parent.Child = null;
  49. Assert.Null(target.InheritanceParent);
  50. }
  51. [Fact]
  52. public void InheritanceParent_Should_Be_Cleared_When_Removed_From_Parent_When_Has_Different_InheritanceParent()
  53. {
  54. var parent = new Decorator();
  55. var inheritanceParent = new Decorator();
  56. var target = new TestControl();
  57. ((ISetInheritanceParent)target).SetParent(inheritanceParent);
  58. parent.Child = target;
  59. parent.Child = null;
  60. Assert.Null(target.InheritanceParent);
  61. }
  62. [Fact]
  63. public void AttachedToLogicalParent_Should_Be_Called_When_Added_To_Tree()
  64. {
  65. var root = new TestRoot();
  66. var parent = new Border();
  67. var child = new Border();
  68. var grandchild = new Border();
  69. var parentRaised = false;
  70. var childRaised = false;
  71. var grandchildRaised = false;
  72. parent.AttachedToLogicalTree += (s, e) => parentRaised = true;
  73. child.AttachedToLogicalTree += (s, e) => childRaised = true;
  74. grandchild.AttachedToLogicalTree += (s, e) => grandchildRaised = true;
  75. parent.Child = child;
  76. child.Child = grandchild;
  77. Assert.False(parentRaised);
  78. Assert.False(childRaised);
  79. Assert.False(grandchildRaised);
  80. root.Child = parent;
  81. Assert.True(parentRaised);
  82. Assert.True(childRaised);
  83. Assert.True(grandchildRaised);
  84. }
  85. [Fact]
  86. public void AttachedToLogicalParent_Should_Be_Called_Before_Parent_Change_Signalled()
  87. {
  88. var root = new TestRoot();
  89. var child = new Border();
  90. var raised = new List<string>();
  91. child.AttachedToLogicalTree += (s, e) =>
  92. {
  93. Assert.Equal(root, child.Parent);
  94. raised.Add("attached");
  95. };
  96. child.GetObservable(StyledElement.ParentProperty).Skip(1).Subscribe(_ => raised.Add("parent"));
  97. root.Child = child;
  98. Assert.Equal(new[] { "attached", "parent" }, raised);
  99. }
  100. [Fact]
  101. public void AttachedToLogicalParent_Should_Not_Be_Called_With_GlobalStyles_As_Root()
  102. {
  103. var globalStyles = Mock.Of<IGlobalStyles>();
  104. var root = new TestRoot { StylingParent = globalStyles };
  105. var child = new Border();
  106. var raised = false;
  107. child.AttachedToLogicalTree += (s, e) =>
  108. {
  109. Assert.Equal(root, e.Root);
  110. raised = true;
  111. };
  112. root.Child = child;
  113. Assert.True(raised);
  114. }
  115. [Fact]
  116. public void DetachedFromLogicalParent_Should_Be_Called_When_Removed_From_Tree()
  117. {
  118. var root = new TestRoot();
  119. var parent = new Border();
  120. var child = new Border();
  121. var grandchild = new Border();
  122. var parentRaised = false;
  123. var childRaised = false;
  124. var grandchildRaised = false;
  125. parent.Child = child;
  126. child.Child = grandchild;
  127. root.Child = parent;
  128. parent.DetachedFromLogicalTree += (s, e) => parentRaised = true;
  129. child.DetachedFromLogicalTree += (s, e) => childRaised = true;
  130. grandchild.DetachedFromLogicalTree += (s, e) => grandchildRaised = true;
  131. root.Child = null;
  132. Assert.True(parentRaised);
  133. Assert.True(childRaised);
  134. Assert.True(grandchildRaised);
  135. }
  136. [Fact]
  137. public void DetachedFromLogicalParent_Should_Not_Be_Called_With_GlobalStyles_As_Root()
  138. {
  139. var globalStyles = Mock.Of<IGlobalStyles>();
  140. var root = new TestRoot { StylingParent = globalStyles };
  141. var child = new Border();
  142. var raised = false;
  143. child.DetachedFromLogicalTree += (s, e) =>
  144. {
  145. Assert.Equal(root, e.Root);
  146. raised = true;
  147. };
  148. root.Child = child;
  149. root.Child = null;
  150. Assert.True(raised);
  151. }
  152. [Fact]
  153. public void Adding_Tree_To_IStyleRoot_Should_Style_Controls()
  154. {
  155. using (AvaloniaLocator.EnterScope())
  156. {
  157. var root = new TestRoot();
  158. var parent = new Border();
  159. var child = new Border();
  160. var grandchild = new Control();
  161. var styler = new Mock<IStyler>();
  162. AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  163. parent.Child = child;
  164. child.Child = grandchild;
  165. styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
  166. root.Child = parent;
  167. styler.Verify(x => x.ApplyStyles(parent), Times.Once());
  168. styler.Verify(x => x.ApplyStyles(child), Times.Once());
  169. styler.Verify(x => x.ApplyStyles(grandchild), Times.Once());
  170. }
  171. }
  172. [Fact]
  173. public void Styles_Not_Applied_Until_Initialization_Finished()
  174. {
  175. using (AvaloniaLocator.EnterScope())
  176. {
  177. var root = new TestRoot();
  178. var child = new Border();
  179. var styler = new Mock<IStyler>();
  180. AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  181. ((ISupportInitialize)child).BeginInit();
  182. root.Child = child;
  183. styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
  184. ((ISupportInitialize)child).EndInit();
  185. styler.Verify(x => x.ApplyStyles(child), Times.Once());
  186. }
  187. }
  188. [Fact]
  189. public void Adding_To_Logical_Tree_Should_Register_With_NameScope()
  190. {
  191. using (AvaloniaLocator.EnterScope())
  192. {
  193. var root = new TestRoot();
  194. var child = new Border();
  195. child.Name = "foo";
  196. root.Child = child;
  197. Assert.Same(root.FindControl<Border>("foo"), child);
  198. }
  199. }
  200. [Fact]
  201. public void Name_Cannot_Be_Set_After_Added_To_Logical_Tree()
  202. {
  203. using (AvaloniaLocator.EnterScope())
  204. {
  205. var root = new TestRoot();
  206. var child = new Border();
  207. root.Child = child;
  208. Assert.Throws<InvalidOperationException>(() => child.Name = "foo");
  209. }
  210. }
  211. [Fact]
  212. public void Name_Can_Be_Set_While_Initializing()
  213. {
  214. using (AvaloniaLocator.EnterScope())
  215. {
  216. var root = new TestRoot();
  217. var child = new Border();
  218. ((ISupportInitialize)child).BeginInit();
  219. root.Child = child;
  220. child.Name = "foo";
  221. Assert.Null(root.FindControl<Border>("foo"));
  222. ((ISupportInitialize)child).EndInit();
  223. Assert.Same(root.FindControl<Border>("foo"), child);
  224. }
  225. }
  226. [Fact]
  227. public void StyleDetach_Is_Triggered_When_Control_Removed_From_Logical_Tree()
  228. {
  229. using (AvaloniaLocator.EnterScope())
  230. {
  231. var root = new TestRoot();
  232. var child = new Border();
  233. root.Child = child;
  234. bool styleDetachTriggered = false;
  235. ((IStyleable)child).StyleDetach.Subscribe(_ => styleDetachTriggered = true);
  236. root.Child = null;
  237. Assert.True(styleDetachTriggered);
  238. }
  239. }
  240. [Fact]
  241. public void EndInit_Should_Raise_Initialized()
  242. {
  243. var root = new TestRoot();
  244. var target = new Border();
  245. var called = false;
  246. target.Initialized += (s, e) => called = true;
  247. ((ISupportInitialize)target).BeginInit();
  248. root.Child = target;
  249. ((ISupportInitialize)target).EndInit();
  250. Assert.True(called);
  251. Assert.True(target.IsInitialized);
  252. }
  253. [Fact]
  254. public void Attaching_To_Visual_Tree_Should_Raise_Initialized()
  255. {
  256. var root = new TestRoot();
  257. var target = new Border();
  258. var called = false;
  259. target.Initialized += (s, e) => called = true;
  260. root.Child = target;
  261. Assert.True(called);
  262. Assert.True(target.IsInitialized);
  263. }
  264. [Fact]
  265. public void DataContextChanged_Should_Be_Called()
  266. {
  267. var root = new TestStackPanel
  268. {
  269. Name = "root",
  270. Children =
  271. {
  272. new TestControl
  273. {
  274. Name = "a1",
  275. Child = new TestControl
  276. {
  277. Name = "b1",
  278. }
  279. },
  280. new TestControl
  281. {
  282. Name = "a2",
  283. DataContext = "foo",
  284. },
  285. }
  286. };
  287. var called = new List<string>();
  288. void Record(object sender, EventArgs e) => called.Add(((StyledElement)sender).Name);
  289. root.DataContextChanged += Record;
  290. foreach (TestControl c in root.GetLogicalDescendants())
  291. {
  292. c.DataContextChanged += Record;
  293. }
  294. root.DataContext = "foo";
  295. Assert.Equal(new[] { "root", "a1", "b1", }, called);
  296. }
  297. [Fact]
  298. public void DataContext_Notifications_Should_Be_Called_In_Correct_Order()
  299. {
  300. var root = new TestStackPanel
  301. {
  302. Name = "root",
  303. Children =
  304. {
  305. new TestControl
  306. {
  307. Name = "a1",
  308. Child = new TestControl
  309. {
  310. Name = "b1",
  311. }
  312. },
  313. new TestControl
  314. {
  315. Name = "a2",
  316. DataContext = "foo",
  317. },
  318. }
  319. };
  320. var called = new List<string>();
  321. foreach (IDataContextEvents c in root.GetSelfAndLogicalDescendants())
  322. {
  323. c.DataContextBeginUpdate += (s, e) => called.Add("begin " + ((StyledElement)s).Name);
  324. c.DataContextChanged += (s, e) => called.Add("changed " + ((StyledElement)s).Name);
  325. c.DataContextEndUpdate += (s, e) => called.Add("end " + ((StyledElement)s).Name);
  326. }
  327. root.DataContext = "foo";
  328. Assert.Equal(
  329. new[]
  330. {
  331. "begin root",
  332. "begin a1",
  333. "begin b1",
  334. "changed root",
  335. "changed a1",
  336. "changed b1",
  337. "end b1",
  338. "end a1",
  339. "end root",
  340. },
  341. called);
  342. }
  343. private interface IDataContextEvents
  344. {
  345. event EventHandler DataContextBeginUpdate;
  346. event EventHandler DataContextChanged;
  347. event EventHandler DataContextEndUpdate;
  348. }
  349. private class TestControl : Decorator, IDataContextEvents
  350. {
  351. public event EventHandler DataContextBeginUpdate;
  352. public event EventHandler DataContextEndUpdate;
  353. public new IAvaloniaObject InheritanceParent => base.InheritanceParent;
  354. protected override void OnDataContextBeginUpdate()
  355. {
  356. DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
  357. base.OnDataContextBeginUpdate();
  358. }
  359. protected override void OnDataContextEndUpdate()
  360. {
  361. DataContextEndUpdate?.Invoke(this, EventArgs.Empty);
  362. base.OnDataContextEndUpdate();
  363. }
  364. }
  365. private class TestStackPanel : StackPanel, IDataContextEvents
  366. {
  367. public event EventHandler DataContextBeginUpdate;
  368. public event EventHandler DataContextEndUpdate;
  369. protected override void OnDataContextBeginUpdate()
  370. {
  371. DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
  372. base.OnDataContextBeginUpdate();
  373. }
  374. protected override void OnDataContextEndUpdate()
  375. {
  376. DataContextEndUpdate?.Invoke(this, EventArgs.Empty);
  377. base.OnDataContextEndUpdate();
  378. }
  379. }
  380. }
  381. }