ContentControlTests.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.Collections.Specialized;
  4. using System.Linq;
  5. using Moq;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Styling;
  10. using Avalonia.UnitTests;
  11. using Avalonia.VisualTree;
  12. using Xunit;
  13. namespace Avalonia.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. AvaloniaLocator.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 Control_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  86. {
  87. var target = new ContentControl
  88. {
  89. Template = GetTemplate(),
  90. };
  91. var child = new Control();
  92. target.Content = child;
  93. target.ApplyTemplate();
  94. ((ContentPresenter)target.Presenter).UpdateChild();
  95. Assert.Equal(child.Parent, target);
  96. Assert.Equal(child.GetLogicalParent(), target);
  97. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  98. }
  99. [Fact]
  100. public void Should_Use_ContentTemplate_To_Create_Control()
  101. {
  102. var target = new ContentControl
  103. {
  104. Template = GetTemplate(),
  105. ContentTemplate = new FuncDataTemplate<string>(_ => new Canvas()),
  106. };
  107. target.Content = "Foo";
  108. target.ApplyTemplate();
  109. ((ContentPresenter)target.Presenter).UpdateChild();
  110. var child = target.Presenter.Child;
  111. Assert.IsType<Canvas>(child);
  112. }
  113. [Fact]
  114. public void DataTemplate_Created_Control_Should_Be_Logical_Child_After_ApplyTemplate()
  115. {
  116. var target = new ContentControl
  117. {
  118. Template = GetTemplate(),
  119. };
  120. target.Content = "Foo";
  121. target.ApplyTemplate();
  122. ((ContentPresenter)target.Presenter).UpdateChild();
  123. var child = target.Presenter.Child;
  124. Assert.NotNull(child);
  125. Assert.Equal(target, child.Parent);
  126. Assert.Equal(target, child.GetLogicalParent());
  127. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  128. }
  129. [Fact]
  130. public void Clearing_Content_Should_Clear_Logical_Child()
  131. {
  132. var target = new ContentControl();
  133. var child = new Control();
  134. target.Content = child;
  135. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  136. target.Content = null;
  137. Assert.Null(child.Parent);
  138. Assert.Null(child.GetLogicalParent());
  139. Assert.Empty(target.GetLogicalChildren());
  140. }
  141. [Fact]
  142. public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged()
  143. {
  144. var target = new ContentControl();
  145. var child = new Control();
  146. var called = false;
  147. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  148. called = e.Action == NotifyCollectionChangedAction.Add;
  149. target.Template = GetTemplate();
  150. target.Content = child;
  151. target.ApplyTemplate();
  152. ((ContentPresenter)target.Presenter).UpdateChild();
  153. Assert.True(called);
  154. }
  155. [Fact]
  156. public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  157. {
  158. var target = new ContentControl();
  159. var child = new Control();
  160. var called = false;
  161. target.Template = GetTemplate();
  162. target.Content = child;
  163. target.ApplyTemplate();
  164. ((ContentPresenter)target.Presenter).UpdateChild();
  165. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  166. target.Content = null;
  167. ((ContentPresenter)target.Presenter).UpdateChild();
  168. Assert.True(called);
  169. }
  170. [Fact]
  171. public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  172. {
  173. var target = new ContentControl();
  174. var child1 = new Control();
  175. var child2 = new Control();
  176. var called = false;
  177. target.Template = GetTemplate();
  178. target.Content = child1;
  179. target.ApplyTemplate();
  180. ((ContentPresenter)target.Presenter).UpdateChild();
  181. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  182. target.Content = child2;
  183. target.Presenter.ApplyTemplate();
  184. Assert.True(called);
  185. }
  186. [Fact]
  187. public void Changing_Content_Should_Update_Presenter()
  188. {
  189. var target = new ContentControl();
  190. target.Template = GetTemplate();
  191. target.ApplyTemplate();
  192. ((ContentPresenter)target.Presenter).UpdateChild();
  193. target.Content = "Foo";
  194. ((ContentPresenter)target.Presenter).UpdateChild();
  195. Assert.Equal("Foo", ((TextBlock)target.Presenter.Child).Text);
  196. target.Content = "Bar";
  197. ((ContentPresenter)target.Presenter).UpdateChild();
  198. Assert.Equal("Bar", ((TextBlock)target.Presenter.Child).Text);
  199. }
  200. [Fact]
  201. public void DataContext_Should_Be_Set_For_DataTemplate_Created_Content()
  202. {
  203. var target = new ContentControl();
  204. target.Template = GetTemplate();
  205. target.Content = "Foo";
  206. target.ApplyTemplate();
  207. ((ContentPresenter)target.Presenter).UpdateChild();
  208. Assert.Equal("Foo", target.Presenter.Child.DataContext);
  209. }
  210. [Fact]
  211. public void DataContext_Should_Not_Be_Set_For_Control_Content()
  212. {
  213. var target = new ContentControl();
  214. target.Template = GetTemplate();
  215. target.Content = new TextBlock();
  216. target.ApplyTemplate();
  217. ((ContentPresenter)target.Presenter).UpdateChild();
  218. Assert.Null(target.Presenter.Child.DataContext);
  219. }
  220. private FuncControlTemplate GetTemplate()
  221. {
  222. return new FuncControlTemplate<ContentControl>(parent =>
  223. {
  224. return new Border
  225. {
  226. Background = new Media.SolidColorBrush(0xffffffff),
  227. Child = new ContentPresenter
  228. {
  229. Name = "PART_ContentPresenter",
  230. [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
  231. [~ContentPresenter.ContentTemplateProperty] = parent[~ContentControl.ContentTemplateProperty],
  232. }
  233. };
  234. });
  235. }
  236. }
  237. }