ControlTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) The Perspex 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 Perspex.Styling;
  8. using Perspex.UnitTests;
  9. using Xunit;
  10. namespace Perspex.Controls.UnitTests
  11. {
  12. public class ControlTests
  13. {
  14. [Fact]
  15. public void Classes_Should_Initially_Be_Empty()
  16. {
  17. var target = new Control();
  18. Assert.Equal(0, target.Classes.Count);
  19. }
  20. [Fact]
  21. public void LogicalParent_Should_Be_Set_To_Parent()
  22. {
  23. var parent = new Decorator();
  24. var target = new TestControl();
  25. parent.Child = target;
  26. Assert.Equal(parent, target.InheritanceParent);
  27. }
  28. [Fact]
  29. public void LogicalParent_Should_Be_Cleared_When_Removed_From_Parent()
  30. {
  31. var parent = new Decorator();
  32. var target = new TestControl();
  33. parent.Child = target;
  34. parent.Child = null;
  35. Assert.Null(target.InheritanceParent);
  36. }
  37. [Fact]
  38. public void AttachedToLogicalParent_Should_Be_Called_When_Added_To_Tree()
  39. {
  40. var root = new TestRoot();
  41. var parent = new Border();
  42. var child = new Border();
  43. var grandchild = new Border();
  44. var parentRaised = false;
  45. var childRaised = false;
  46. var grandchildRaised = false;
  47. parent.AttachedToLogicalTree += (s, e) => parentRaised = true;
  48. child.AttachedToLogicalTree += (s, e) => childRaised = true;
  49. grandchild.AttachedToLogicalTree += (s, e) => grandchildRaised = true;
  50. parent.Child = child;
  51. child.Child = grandchild;
  52. Assert.False(parentRaised);
  53. Assert.False(childRaised);
  54. Assert.False(grandchildRaised);
  55. root.Child = parent;
  56. Assert.True(parentRaised);
  57. Assert.True(childRaised);
  58. Assert.True(grandchildRaised);
  59. }
  60. [Fact]
  61. public void AttachedToLogicalParent_Should_Be_Called_Before_Parent_Change_Signalled()
  62. {
  63. var root = new TestRoot();
  64. var child = new Border();
  65. var raised = new List<string>();
  66. child.AttachedToLogicalTree += (s, e) =>
  67. {
  68. Assert.Equal(root, child.Parent);
  69. raised.Add("attached");
  70. };
  71. child.GetObservable(Control.ParentProperty).Skip(1).Subscribe(_ => raised.Add("parent"));
  72. root.Child = child;
  73. Assert.Equal(new[] { "attached", "parent" }, raised);
  74. }
  75. [Fact]
  76. public void DetachedToLogicalParent_Should_Be_Called_When_Removed_From_Tree()
  77. {
  78. var root = new TestRoot();
  79. var parent = new Border();
  80. var child = new Border();
  81. var grandchild = new Border();
  82. var parentRaised = false;
  83. var childRaised = false;
  84. var grandchildRaised = false;
  85. parent.Child = child;
  86. child.Child = grandchild;
  87. root.Child = parent;
  88. parent.DetachedFromLogicalTree += (s, e) => parentRaised = true;
  89. child.DetachedFromLogicalTree += (s, e) => childRaised = true;
  90. grandchild.DetachedFromLogicalTree += (s, e) => grandchildRaised = true;
  91. root.Child = null;
  92. Assert.True(parentRaised);
  93. Assert.True(childRaised);
  94. Assert.True(grandchildRaised);
  95. }
  96. [Fact]
  97. public void Adding_Tree_To_IStyleRoot_Should_Style_Controls()
  98. {
  99. using (PerspexLocator.EnterScope())
  100. {
  101. var root = new TestRoot();
  102. var parent = new Border();
  103. var child = new Border();
  104. var grandchild = new Control();
  105. var styler = new Mock<IStyler>();
  106. PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  107. parent.Child = child;
  108. child.Child = grandchild;
  109. styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
  110. root.Child = parent;
  111. styler.Verify(x => x.ApplyStyles(parent), Times.Once());
  112. styler.Verify(x => x.ApplyStyles(child), Times.Once());
  113. styler.Verify(x => x.ApplyStyles(grandchild), Times.Once());
  114. }
  115. }
  116. private class TestControl : Control
  117. {
  118. public new PerspexObject InheritanceParent => base.InheritanceParent;
  119. }
  120. }
  121. }