ContentControlTests.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.Collections.Specialized;
  4. using System.Linq;
  5. using Moq;
  6. using Perspex.Controls.Presenters;
  7. using Perspex.Controls.Templates;
  8. using Perspex.LogicalTree;
  9. using Perspex.Styling;
  10. using Perspex.UnitTests;
  11. using Perspex.VisualTree;
  12. using Xunit;
  13. namespace Perspex.Controls.UnitTests
  14. {
  15. public class ContentControlTests
  16. {
  17. [Fact]
  18. public void Template_Should_Be_Instantiated()
  19. {
  20. var target = new ContentControl();
  21. target.Content = "Foo";
  22. target.Template = GetTemplate();
  23. target.ApplyTemplate();
  24. ((ContentPresenter)target.Presenter).UpdateChild();
  25. var child = ((IVisual)target).VisualChildren.Single();
  26. Assert.IsType<Border>(child);
  27. child = child.VisualChildren.Single();
  28. Assert.IsType<ContentPresenter>(child);
  29. child = child.VisualChildren.Single();
  30. Assert.IsType<TextBlock>(child);
  31. }
  32. [Fact]
  33. public void Templated_Children_Should_Be_Styled()
  34. {
  35. var root = new TestRoot();
  36. var target = new ContentControl();
  37. var styler = new Mock<IStyler>();
  38. PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  39. target.Content = "Foo";
  40. target.Template = GetTemplate();
  41. root.Child = target;
  42. target.ApplyTemplate();
  43. styler.Verify(x => x.ApplyStyles(It.IsAny<ContentControl>()), Times.Once());
  44. styler.Verify(x => x.ApplyStyles(It.IsAny<Border>()), Times.Once());
  45. styler.Verify(x => x.ApplyStyles(It.IsAny<ContentPresenter>()), Times.Once());
  46. styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once());
  47. }
  48. [Fact]
  49. public void ContentPresenter_Should_Have_TemplatedParent_Set()
  50. {
  51. var target = new ContentControl();
  52. var child = new Border();
  53. target.Template = GetTemplate();
  54. target.Content = child;
  55. target.ApplyTemplate();
  56. ((ContentPresenter)target.Presenter).UpdateChild();
  57. var contentPresenter = child.GetVisualParent<ContentPresenter>();
  58. Assert.Equal(target, contentPresenter.TemplatedParent);
  59. }
  60. [Fact]
  61. public void Content_Should_Have_TemplatedParent_Set_To_Null()
  62. {
  63. var target = new ContentControl();
  64. var child = new Border();
  65. target.Template = GetTemplate();
  66. target.Content = child;
  67. target.ApplyTemplate();
  68. ((ContentPresenter)target.Presenter).UpdateChild();
  69. Assert.Null(child.TemplatedParent);
  70. }
  71. [Fact]
  72. public void Control_Content_Should_Be_Logical_Child_Before_ApplyTemplate()
  73. {
  74. var target = new ContentControl
  75. {
  76. Template = GetTemplate(),
  77. };
  78. var child = new Control();
  79. target.Content = child;
  80. Assert.Equal(child.Parent, target);
  81. Assert.Equal(child.GetLogicalParent(), target);
  82. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  83. }
  84. [Fact]
  85. public void DataTemplate_Created_Control_Should_Be_Logical_Child_After_ApplyTemplate()
  86. {
  87. var target = new ContentControl
  88. {
  89. Template = GetTemplate(),
  90. };
  91. target.Content = "Foo";
  92. target.ApplyTemplate();
  93. ((ContentPresenter)target.Presenter).UpdateChild();
  94. var child = target.Presenter.Child;
  95. Assert.NotNull(child);
  96. Assert.Equal(target, child.Parent);
  97. Assert.Equal(target, child.GetLogicalParent());
  98. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  99. }
  100. [Fact]
  101. public void Clearing_Content_Should_Clear_Logical_Child()
  102. {
  103. var target = new ContentControl();
  104. var child = new Control();
  105. target.Content = child;
  106. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  107. target.Content = null;
  108. Assert.Null(child.Parent);
  109. Assert.Null(child.GetLogicalParent());
  110. Assert.Empty(target.GetLogicalChildren());
  111. }
  112. [Fact]
  113. public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged()
  114. {
  115. var target = new ContentControl();
  116. var child = new Control();
  117. var called = false;
  118. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  119. called = e.Action == NotifyCollectionChangedAction.Add;
  120. target.Template = GetTemplate();
  121. target.Content = child;
  122. target.ApplyTemplate();
  123. ((ContentPresenter)target.Presenter).UpdateChild();
  124. Assert.True(called);
  125. }
  126. [Fact]
  127. public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  128. {
  129. var target = new ContentControl();
  130. var child = new Control();
  131. var called = false;
  132. target.Template = GetTemplate();
  133. target.Content = child;
  134. target.ApplyTemplate();
  135. ((ContentPresenter)target.Presenter).UpdateChild();
  136. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  137. target.Content = null;
  138. ((ContentPresenter)target.Presenter).UpdateChild();
  139. Assert.True(called);
  140. }
  141. [Fact]
  142. public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  143. {
  144. var target = new ContentControl();
  145. var child1 = new Control();
  146. var child2 = new Control();
  147. var called = false;
  148. target.Template = GetTemplate();
  149. target.Content = child1;
  150. target.ApplyTemplate();
  151. ((ContentPresenter)target.Presenter).UpdateChild();
  152. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  153. target.Content = child2;
  154. target.Presenter.ApplyTemplate();
  155. Assert.True(called);
  156. }
  157. [Fact]
  158. public void Changing_Content_Should_Update_Presenter()
  159. {
  160. var target = new ContentControl();
  161. target.Template = GetTemplate();
  162. target.ApplyTemplate();
  163. ((ContentPresenter)target.Presenter).UpdateChild();
  164. target.Content = "Foo";
  165. ((ContentPresenter)target.Presenter).UpdateChild();
  166. Assert.Equal("Foo", ((TextBlock)target.Presenter.Child).Text);
  167. target.Content = "Bar";
  168. ((ContentPresenter)target.Presenter).UpdateChild();
  169. Assert.Equal("Bar", ((TextBlock)target.Presenter.Child).Text);
  170. }
  171. [Fact]
  172. public void DataContext_Should_Be_Set_For_DataTemplate_Created_Content()
  173. {
  174. var target = new ContentControl();
  175. target.Template = GetTemplate();
  176. target.Content = "Foo";
  177. target.ApplyTemplate();
  178. ((ContentPresenter)target.Presenter).UpdateChild();
  179. Assert.Equal("Foo", target.Presenter.Child.DataContext);
  180. }
  181. [Fact]
  182. public void DataContext_Should_Not_Be_Set_For_Control_Content()
  183. {
  184. var target = new ContentControl();
  185. target.Template = GetTemplate();
  186. target.Content = new TextBlock();
  187. target.ApplyTemplate();
  188. ((ContentPresenter)target.Presenter).UpdateChild();
  189. Assert.Null(target.Presenter.Child.DataContext);
  190. }
  191. private FuncControlTemplate GetTemplate()
  192. {
  193. return new FuncControlTemplate<ContentControl>(parent =>
  194. {
  195. return new Border
  196. {
  197. Background = new Media.SolidColorBrush(0xffffffff),
  198. Child = new ContentPresenter
  199. {
  200. Name = "PART_ContentPresenter",
  201. [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
  202. }
  203. };
  204. });
  205. }
  206. }
  207. }