StyledElementTests.cs 14 KB

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