StyledElementTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reactive.Linq;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Data;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Styling;
  10. using Avalonia.UnitTests;
  11. using Moq;
  12. using Xunit;
  13. namespace Avalonia.Base.UnitTests.Styling
  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 Adding_Element_With_Null_Parent_To_Logical_Tree_Should_Throw()
  65. {
  66. var target = new Border();
  67. var visualParent = new Panel();
  68. var logicalParent = new Panel();
  69. var root = new TestRoot();
  70. // Set the logical parent...
  71. ((ISetLogicalParent)target).SetParent(logicalParent);
  72. // ...so that when it's added to `visualParent`, the parent won't be set again.
  73. visualParent.Children.Add(target);
  74. // Clear the logical parent. It's now a logical child of `visualParent` but doesn't have
  75. // a logical parent itself.
  76. ((ISetLogicalParent)target).SetParent(null);
  77. // In this case, attaching the control to a logical tree should throw.
  78. logicalParent.Children.Add(visualParent);
  79. Assert.Throws<InvalidOperationException>(() => root.Child = logicalParent);
  80. }
  81. [Fact]
  82. public void AttachedToLogicalTree_Should_Be_Called_When_Added_To_Tree()
  83. {
  84. var root = new TestRoot();
  85. var parent = new Border();
  86. var child = new Border();
  87. var grandchild = new Border();
  88. var parentRaised = false;
  89. var childRaised = false;
  90. var grandchildRaised = false;
  91. parent.AttachedToLogicalTree += (s, e) => parentRaised = true;
  92. child.AttachedToLogicalTree += (s, e) => childRaised = true;
  93. grandchild.AttachedToLogicalTree += (s, e) => grandchildRaised = true;
  94. parent.Child = child;
  95. child.Child = grandchild;
  96. Assert.False(parentRaised);
  97. Assert.False(childRaised);
  98. Assert.False(grandchildRaised);
  99. root.Child = parent;
  100. Assert.True(parentRaised);
  101. Assert.True(childRaised);
  102. Assert.True(grandchildRaised);
  103. }
  104. [Fact]
  105. public void AttachedToLogicalTree_Should_Be_Called_Before_Parent_Change_Signalled()
  106. {
  107. var root = new TestRoot();
  108. var child = new Border();
  109. var raised = new List<string>();
  110. child.AttachedToLogicalTree += (s, e) =>
  111. {
  112. Assert.Equal(root, child.Parent);
  113. raised.Add("attached");
  114. };
  115. child.GetObservable(StyledElement.ParentProperty).Skip(1).Subscribe(_ => raised.Add("parent"));
  116. root.Child = child;
  117. Assert.Equal(new[] { "attached", "parent" }, raised);
  118. }
  119. [Fact]
  120. public void AttachedToLogicalTree_Should_Not_Be_Called_With_GlobalStyles_As_Root()
  121. {
  122. var globalStyles = Mock.Of<IGlobalStyles>();
  123. var root = new TestRoot { StylingParent = globalStyles };
  124. var child = new Border();
  125. var raised = false;
  126. child.AttachedToLogicalTree += (s, e) =>
  127. {
  128. Assert.Equal(root, e.Root);
  129. raised = true;
  130. };
  131. root.Child = child;
  132. Assert.True(raised);
  133. }
  134. [Fact]
  135. public void AttachedToLogicalTree_Should_Have_Source_Set()
  136. {
  137. var root = new TestRoot();
  138. var canvas = new Canvas();
  139. var border = new Border { Child = canvas };
  140. var raised = 0;
  141. void Attached(object sender, LogicalTreeAttachmentEventArgs e)
  142. {
  143. Assert.Same(border, e.Source);
  144. ++raised;
  145. }
  146. border.AttachedToLogicalTree += Attached;
  147. canvas.AttachedToLogicalTree += Attached;
  148. root.Child = border;
  149. Assert.Equal(2, raised);
  150. }
  151. [Fact]
  152. public void AttachedToLogicalTree_Should_Have_Parent_Set()
  153. {
  154. var root = new TestRoot();
  155. var canvas = new Canvas();
  156. var border = new Border { Child = canvas };
  157. var raised = 0;
  158. void Attached(object sender, LogicalTreeAttachmentEventArgs e)
  159. {
  160. Assert.Same(root, e.Parent);
  161. ++raised;
  162. }
  163. border.AttachedToLogicalTree += Attached;
  164. canvas.AttachedToLogicalTree += Attached;
  165. root.Child = border;
  166. Assert.Equal(2, raised);
  167. }
  168. [Fact]
  169. public void DetachedFromLogicalTree_Should_Be_Called_When_Removed_From_Tree()
  170. {
  171. var root = new TestRoot();
  172. var parent = new Border();
  173. var child = new Border();
  174. var grandchild = new Border();
  175. var parentRaised = false;
  176. var childRaised = false;
  177. var grandchildRaised = false;
  178. parent.Child = child;
  179. child.Child = grandchild;
  180. root.Child = parent;
  181. parent.DetachedFromLogicalTree += (s, e) => parentRaised = true;
  182. child.DetachedFromLogicalTree += (s, e) => childRaised = true;
  183. grandchild.DetachedFromLogicalTree += (s, e) => grandchildRaised = true;
  184. root.Child = null;
  185. Assert.True(parentRaised);
  186. Assert.True(childRaised);
  187. Assert.True(grandchildRaised);
  188. }
  189. [Fact]
  190. public void DetachedFromLogicalTree_Should_Not_Be_Called_With_GlobalStyles_As_Root()
  191. {
  192. var globalStyles = Mock.Of<IGlobalStyles>();
  193. var root = new TestRoot { StylingParent = globalStyles };
  194. var child = new Border();
  195. var raised = false;
  196. child.DetachedFromLogicalTree += (s, e) =>
  197. {
  198. Assert.Equal(root, e.Root);
  199. raised = true;
  200. };
  201. root.Child = child;
  202. root.Child = null;
  203. Assert.True(raised);
  204. }
  205. [Fact]
  206. public void Parent_Should_Be_Null_When_DetachedFromLogicalTree_Called()
  207. {
  208. var target = new TestControl();
  209. var root = new TestRoot(target);
  210. var called = 0;
  211. target.DetachedFromLogicalTree += (s, e) =>
  212. {
  213. Assert.Null(target.Parent);
  214. Assert.Null(target.InheritanceParent);
  215. ++called;
  216. };
  217. root.Child = null;
  218. Assert.Equal(1, called);
  219. }
  220. [Fact]
  221. public void Adding_Tree_To_IStyleRoot_Should_Style_Controls()
  222. {
  223. using (AvaloniaLocator.EnterScope())
  224. {
  225. var root = new TestRoot();
  226. var parent = new Border();
  227. var child = new Border();
  228. var grandchild = new Control();
  229. var styler = new Mock<IStyler>();
  230. AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  231. parent.Child = child;
  232. child.Child = grandchild;
  233. styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
  234. root.Child = parent;
  235. styler.Verify(x => x.ApplyStyles(parent), Times.Once());
  236. styler.Verify(x => x.ApplyStyles(child), Times.Once());
  237. styler.Verify(x => x.ApplyStyles(grandchild), Times.Once());
  238. }
  239. }
  240. [Fact]
  241. public void Styles_Not_Applied_Until_Initialization_Finished()
  242. {
  243. using (AvaloniaLocator.EnterScope())
  244. {
  245. var root = new TestRoot();
  246. var child = new Border();
  247. var styler = new Mock<IStyler>();
  248. AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  249. ((ISupportInitialize)child).BeginInit();
  250. root.Child = child;
  251. styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
  252. ((ISupportInitialize)child).EndInit();
  253. styler.Verify(x => x.ApplyStyles(child), Times.Once());
  254. }
  255. }
  256. [Fact]
  257. public void Name_Cannot_Be_Set_After_Added_To_Logical_Tree()
  258. {
  259. using (AvaloniaLocator.EnterScope())
  260. {
  261. var root = new TestRoot();
  262. var child = new Border();
  263. root.Child = child;
  264. Assert.Throws<InvalidOperationException>(() => child.Name = "foo");
  265. }
  266. }
  267. [Fact]
  268. public void Name_Can_Be_Set_While_Initializing()
  269. {
  270. using (AvaloniaLocator.EnterScope())
  271. {
  272. var root = new TestRoot();
  273. var child = new Border();
  274. child.BeginInit();
  275. root.Child = child;
  276. child.Name = "foo";
  277. child.EndInit();
  278. }
  279. }
  280. [Fact]
  281. public void StyleInstance_Is_Disposed_When_Control_Removed_From_Logical_Tree()
  282. {
  283. using (AvaloniaLocator.EnterScope())
  284. {
  285. var root = new TestRoot();
  286. var child = new Border();
  287. root.Child = child;
  288. var styleInstance = new Mock<IStyleInstance>();
  289. ((IStyleable)child).StyleApplied(styleInstance.Object);
  290. root.Child = null;
  291. styleInstance.Verify(x => x.Dispose(), Times.Once);
  292. }
  293. }
  294. [Fact]
  295. public void EndInit_Should_Raise_Initialized()
  296. {
  297. var root = new TestRoot();
  298. var target = new Border();
  299. var called = false;
  300. target.Initialized += (s, e) => called = true;
  301. ((ISupportInitialize)target).BeginInit();
  302. root.Child = target;
  303. ((ISupportInitialize)target).EndInit();
  304. Assert.True(called);
  305. Assert.True(target.IsInitialized);
  306. }
  307. [Fact]
  308. public void Attaching_To_Visual_Tree_Should_Raise_Initialized()
  309. {
  310. var root = new TestRoot();
  311. var target = new Border();
  312. var called = false;
  313. target.Initialized += (s, e) => called = true;
  314. root.Child = target;
  315. Assert.True(called);
  316. Assert.True(target.IsInitialized);
  317. }
  318. [Fact]
  319. public void DataContextChanged_Should_Be_Called()
  320. {
  321. var root = new TestStackPanel
  322. {
  323. Name = "root",
  324. Children =
  325. {
  326. new TestControl
  327. {
  328. Name = "a1",
  329. Child = new TestControl
  330. {
  331. Name = "b1",
  332. }
  333. },
  334. new TestControl
  335. {
  336. Name = "a2",
  337. DataContext = "foo",
  338. },
  339. }
  340. };
  341. var called = new List<string>();
  342. void Record(object sender, EventArgs e) => called.Add(((StyledElement)sender).Name);
  343. root.DataContextChanged += Record;
  344. foreach (TestControl c in root.GetLogicalDescendants())
  345. {
  346. c.DataContextChanged += Record;
  347. }
  348. root.DataContext = "foo";
  349. Assert.Equal(new[] { "root", "a1", "b1", }, called);
  350. }
  351. [Fact]
  352. public void DataContext_Notifications_Should_Be_Called_In_Correct_Order()
  353. {
  354. var root = new TestStackPanel
  355. {
  356. Name = "root",
  357. Children =
  358. {
  359. new TestControl
  360. {
  361. Name = "a1",
  362. Child = new TestControl
  363. {
  364. Name = "b1",
  365. }
  366. },
  367. new TestControl
  368. {
  369. Name = "a2",
  370. DataContext = "foo",
  371. },
  372. }
  373. };
  374. var called = new List<string>();
  375. foreach (IDataContextEvents c in root.GetSelfAndLogicalDescendants())
  376. {
  377. c.DataContextBeginUpdate += (s, e) => called.Add("begin " + ((StyledElement)s).Name);
  378. c.DataContextChanged += (s, e) => called.Add("changed " + ((StyledElement)s).Name);
  379. c.DataContextEndUpdate += (s, e) => called.Add("end " + ((StyledElement)s).Name);
  380. }
  381. root.DataContext = "foo";
  382. Assert.Equal(
  383. new[]
  384. {
  385. "begin root",
  386. "begin a1",
  387. "begin b1",
  388. "changed root",
  389. "changed a1",
  390. "changed b1",
  391. "end b1",
  392. "end a1",
  393. "end root",
  394. },
  395. called);
  396. }
  397. [Fact]
  398. public void Resources_Owner_Is_Set()
  399. {
  400. var target = new TestControl();
  401. Assert.Same(target, ((ResourceDictionary)target.Resources).Owner);
  402. }
  403. [Fact]
  404. public void Assigned_Resources_Parent_Is_Set()
  405. {
  406. var resources = new Mock<IResourceDictionary>();
  407. var target = new TestControl { Resources = resources.Object };
  408. resources.Verify(x => x.AddOwner(target));
  409. }
  410. [Fact]
  411. public void Assigning_Resources_Raises_ResourcesChanged()
  412. {
  413. var resources = new ResourceDictionary { { "foo", "bar" } };
  414. var target = new TestControl();
  415. var raised = 0;
  416. target.ResourcesChanged += (s, e) => ++raised;
  417. target.Resources = resources;
  418. Assert.Equal(1, raised);
  419. }
  420. [Fact]
  421. public void Styles_Owner_Is_Set()
  422. {
  423. var target = new TestControl();
  424. Assert.Same(target, target.Styles.Owner);
  425. }
  426. [Fact]
  427. public void Adding_To_Logical_Tree_Raises_ResourcesChanged()
  428. {
  429. var target = new TestRoot();
  430. var parent = new Decorator { Resources = { { "foo", "bar" } } };
  431. var raised = 0;
  432. target.ResourcesChanged += (s, e) => ++raised;
  433. parent.Child = target;
  434. Assert.Equal(1, raised);
  435. }
  436. [Fact]
  437. public void SetParent_Does_Not_Crash_Due_To_Reentrancy()
  438. {
  439. // Issue #3708
  440. var app = UnitTestApplication.Start(TestServices.StyledWindow);
  441. ContentControl target;
  442. var root = new TestRoot
  443. {
  444. DataContext = false,
  445. Child = target = new ContentControl
  446. {
  447. Styles =
  448. {
  449. new Style(x => x.OfType<ContentControl>())
  450. {
  451. Setters =
  452. {
  453. new Setter(
  454. ContentControl.ContentProperty,
  455. new FuncTemplate<IControl>(() => new TextBlock { Text = "Enabled" })),
  456. },
  457. },
  458. new Style(x => x.OfType<ContentControl>().Class(":disabled"))
  459. {
  460. Setters =
  461. {
  462. new Setter(
  463. ContentControl.ContentProperty,
  464. new FuncTemplate<IControl>(() => new TextBlock { Text = "Disabled" })),
  465. },
  466. },
  467. },
  468. [!ContentControl.IsEnabledProperty] = new Binding(),
  469. }
  470. };
  471. root.Measure(Size.Infinity);
  472. root.Arrange(new Rect(0, 0, 100, 100));
  473. var textBlock = Assert.IsType<TextBlock>(target.Content);
  474. Assert.Equal("Disabled", textBlock.Text);
  475. // #3708 was crashing here with AvaloniaInternalException.
  476. root.Child = null;
  477. }
  478. private interface IDataContextEvents
  479. {
  480. event EventHandler DataContextBeginUpdate;
  481. event EventHandler DataContextChanged;
  482. event EventHandler DataContextEndUpdate;
  483. }
  484. private class TestControl : Decorator, IDataContextEvents
  485. {
  486. public event EventHandler DataContextBeginUpdate;
  487. public event EventHandler DataContextEndUpdate;
  488. public new IAvaloniaObject InheritanceParent => base.InheritanceParent;
  489. protected override void OnDataContextBeginUpdate()
  490. {
  491. DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
  492. base.OnDataContextBeginUpdate();
  493. }
  494. protected override void OnDataContextEndUpdate()
  495. {
  496. DataContextEndUpdate?.Invoke(this, EventArgs.Empty);
  497. base.OnDataContextEndUpdate();
  498. }
  499. }
  500. private class TestStackPanel : StackPanel, IDataContextEvents
  501. {
  502. public event EventHandler DataContextBeginUpdate;
  503. public event EventHandler DataContextEndUpdate;
  504. protected override void OnDataContextBeginUpdate()
  505. {
  506. DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
  507. base.OnDataContextBeginUpdate();
  508. }
  509. protected override void OnDataContextEndUpdate()
  510. {
  511. DataContextEndUpdate?.Invoke(this, EventArgs.Empty);
  512. base.OnDataContextEndUpdate();
  513. }
  514. }
  515. }
  516. }