ControlTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Layout;
  8. using Perspex.Platform;
  9. using Perspex.Rendering;
  10. using Perspex.Styling;
  11. using Xunit;
  12. namespace Perspex.Controls.UnitTests
  13. {
  14. public class ControlTests
  15. {
  16. [Fact]
  17. public void Classes_Should_Initially_Be_Empty()
  18. {
  19. var target = new Control();
  20. Assert.Equal(0, target.Classes.Count);
  21. }
  22. [Fact]
  23. public void LogicalParent_Should_Be_Set_To_Parent()
  24. {
  25. var parent = new Decorator();
  26. var target = new TestControl();
  27. parent.Child = target;
  28. Assert.Equal(parent, target.InheritanceParent);
  29. }
  30. [Fact]
  31. public void LogicalParent_Should_Be_Cleared_When_Removed_From_Parent()
  32. {
  33. var parent = new Decorator();
  34. var target = new TestControl();
  35. parent.Child = target;
  36. parent.Child = null;
  37. Assert.Null(target.InheritanceParent);
  38. }
  39. [Fact]
  40. public void AttachedToLogicalParent_Should_Be_Called_When_Added_To_Tree()
  41. {
  42. var root = new TestRoot();
  43. var parent = new Border();
  44. var child = new Border();
  45. var grandchild = new Border();
  46. var parentRaised = false;
  47. var childRaised = false;
  48. var grandchildRaised = false;
  49. parent.AttachedToLogicalTree += (s, e) => parentRaised = true;
  50. child.AttachedToLogicalTree += (s, e) => childRaised = true;
  51. grandchild.AttachedToLogicalTree += (s, e) => grandchildRaised = true;
  52. parent.Child = child;
  53. child.Child = grandchild;
  54. Assert.False(parentRaised);
  55. Assert.False(childRaised);
  56. Assert.False(grandchildRaised);
  57. root.Child = parent;
  58. Assert.True(parentRaised);
  59. Assert.True(childRaised);
  60. Assert.True(grandchildRaised);
  61. }
  62. [Fact]
  63. public void AttachedToLogicalParent_Should_Be_Called_Before_Parent_Change_Signalled()
  64. {
  65. var root = new TestRoot();
  66. var child = new Border();
  67. var raised = new List<string>();
  68. child.AttachedToLogicalTree += (s, e) =>
  69. {
  70. Assert.Equal(root, child.Parent);
  71. raised.Add("attached");
  72. };
  73. child.GetObservable(Control.ParentProperty).Skip(1).Subscribe(_ => raised.Add("parent"));
  74. root.Child = child;
  75. Assert.Equal(new[] { "attached", "parent" }, raised);
  76. }
  77. [Fact]
  78. public void DetachedToLogicalParent_Should_Be_Called_When_Removed_From_Tree()
  79. {
  80. var root = new TestRoot();
  81. var parent = new Border();
  82. var child = new Border();
  83. var grandchild = new Border();
  84. var parentRaised = false;
  85. var childRaised = false;
  86. var grandchildRaised = false;
  87. parent.Child = child;
  88. child.Child = grandchild;
  89. root.Child = parent;
  90. parent.DetachedFromLogicalTree += (s, e) => parentRaised = true;
  91. child.DetachedFromLogicalTree += (s, e) => childRaised = true;
  92. grandchild.DetachedFromLogicalTree += (s, e) => grandchildRaised = true;
  93. root.Child = null;
  94. Assert.True(parentRaised);
  95. Assert.True(childRaised);
  96. Assert.True(grandchildRaised);
  97. }
  98. [Fact]
  99. public void Adding_Tree_To_IStyleRoot_Should_Style_Controls()
  100. {
  101. using (PerspexLocator.EnterScope())
  102. {
  103. var root = new TestRoot();
  104. var parent = new Border();
  105. var child = new Border();
  106. var grandchild = new Control();
  107. var styler = new Mock<IStyler>();
  108. PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  109. parent.Child = child;
  110. child.Child = grandchild;
  111. styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
  112. root.Child = parent;
  113. styler.Verify(x => x.ApplyStyles(parent), Times.Once());
  114. styler.Verify(x => x.ApplyStyles(child), Times.Once());
  115. styler.Verify(x => x.ApplyStyles(grandchild), Times.Once());
  116. }
  117. }
  118. private class TestRoot : Decorator, ILayoutRoot, IRenderRoot, IStyleRoot
  119. {
  120. public Size ClientSize
  121. {
  122. get { throw new NotImplementedException(); }
  123. }
  124. public Size MaxClientSize
  125. {
  126. get { throw new NotImplementedException(); }
  127. }
  128. public ILayoutManager LayoutManager
  129. {
  130. get { throw new NotImplementedException(); }
  131. }
  132. public IRenderTarget RenderTarget
  133. {
  134. get { throw new NotImplementedException(); }
  135. }
  136. public IRenderQueueManager RenderQueueManager
  137. {
  138. get { throw new NotImplementedException(); }
  139. }
  140. public Point TranslatePointToScreen(Point p)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. }
  145. private class TestControl : Control
  146. {
  147. public new PerspexObject InheritanceParent => base.InheritanceParent;
  148. }
  149. }
  150. }