ContentPresenterTests_InTemplate.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Data;
  7. using Avalonia.LogicalTree;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. using Moq;
  11. using Xunit;
  12. namespace Avalonia.Controls.UnitTests.Presenters
  13. {
  14. /// <summary>
  15. /// Tests for ContentControls that are hosted in a control template.
  16. /// </summary>
  17. public class ContentPresenterTests_InTemplate
  18. {
  19. [Fact]
  20. public void Should_Register_With_Host_When_TemplatedParent_Set()
  21. {
  22. var host = new Mock<IContentPresenterHost>();
  23. var target = new ContentPresenter();
  24. target.SetValue(Control.TemplatedParentProperty, host.Object);
  25. host.Verify(x => x.RegisterContentPresenter(target));
  26. }
  27. [Fact]
  28. public void Setting_Content_To_Control_Should_Set_Child()
  29. {
  30. var (target, _) = CreateTarget();
  31. var child = new Border();
  32. target.Content = child;
  33. Assert.Equal(child, target.Child);
  34. }
  35. [Fact]
  36. public void Setting_Content_To_Control_Should_Update_Logical_Tree()
  37. {
  38. var (target, parent) = CreateTarget();
  39. var child = new Border();
  40. target.Content = child;
  41. Assert.Equal(parent, child.GetLogicalParent());
  42. Assert.Equal(new[] { child }, parent.GetLogicalChildren());
  43. }
  44. [Fact]
  45. public void Setting_Content_To_Control_Should_Update_Visual_Tree()
  46. {
  47. var (target, _) = CreateTarget();
  48. var child = new Border();
  49. target.Content = child;
  50. Assert.Equal(target, child.GetVisualParent());
  51. Assert.Equal(new[] { child }, target.GetVisualChildren());
  52. }
  53. [Fact]
  54. public void Setting_Content_To_String_Should_Create_TextBlock()
  55. {
  56. var (target, _) = CreateTarget();
  57. target.Content = "Foo";
  58. Assert.IsType<TextBlock>(target.Child);
  59. Assert.Equal("Foo", ((TextBlock)target.Child).Text);
  60. }
  61. [Fact]
  62. public void Setting_Content_To_String_Should_Update_Logical_Tree()
  63. {
  64. var (target, parent) = CreateTarget();
  65. target.Content = "Foo";
  66. var child = target.Child;
  67. Assert.Equal(parent, child.GetLogicalParent());
  68. Assert.Equal(new[] { child }, parent.GetLogicalChildren());
  69. }
  70. [Fact]
  71. public void Setting_Content_To_String_Should_Update_Visual_Tree()
  72. {
  73. var (target, _) = CreateTarget();
  74. target.Content = "Foo";
  75. var child = target.Child;
  76. Assert.Equal(target, child.GetVisualParent());
  77. Assert.Equal(new[] { child }, target.GetVisualChildren());
  78. }
  79. [Fact]
  80. public void Clearing_Control_Content_Should_Update_Logical_Tree()
  81. {
  82. var (target, _) = CreateTarget();
  83. var child = new Border();
  84. target.Content = child;
  85. target.Content = null;
  86. Assert.Null(child.GetLogicalParent());
  87. Assert.Empty(target.GetLogicalChildren());
  88. }
  89. [Fact]
  90. public void Clearing_Control_Content_Should_Update_Visual_Tree()
  91. {
  92. var (target, _) = CreateTarget();
  93. var child = new Border();
  94. target.Content = child;
  95. target.Content = null;
  96. Assert.Null(child.GetVisualParent());
  97. Assert.Empty(target.GetVisualChildren());
  98. }
  99. [Fact]
  100. public void Control_Content_Should_Not_Be_NameScope()
  101. {
  102. var (target, _) = CreateTarget();
  103. target.Content = new TextBlock();
  104. Assert.IsType<TextBlock>(target.Child);
  105. Assert.Null(NameScope.GetNameScope((Control)target.Child));
  106. }
  107. [Fact]
  108. public void Assigning_Control_To_Content_Should_Not_Set_DataContext()
  109. {
  110. var (target, _) = CreateTarget();
  111. target.Content = new Border();
  112. Assert.False(target.IsSet(Control.DataContextProperty));
  113. }
  114. [Fact]
  115. public void Assigning_NonControl_To_Content_Should_Set_DataContext_On_UpdateChild()
  116. {
  117. var (target, _) = CreateTarget();
  118. target.Content = "foo";
  119. Assert.Equal("foo", target.DataContext);
  120. }
  121. [Fact]
  122. public void Should_Use_ContentTemplate_If_Specified()
  123. {
  124. var (target, _) = CreateTarget();
  125. target.ContentTemplate = new FuncDataTemplate<string>((_, __) => new Canvas());
  126. target.Content = "Foo";
  127. Assert.IsType<Canvas>(target.Child);
  128. }
  129. [Fact]
  130. public void Should_Update_If_ContentTemplate_Changed()
  131. {
  132. var (target, _) = CreateTarget();
  133. target.Content = "Foo";
  134. Assert.IsType<TextBlock>(target.Child);
  135. target.ContentTemplate = new FuncDataTemplate<string>((_, __) => new Canvas());
  136. Assert.IsType<Canvas>(target.Child);
  137. target.ContentTemplate = null;
  138. Assert.IsType<TextBlock>(target.Child);
  139. }
  140. [Fact]
  141. public void Assigning_Control_To_Content_After_NonControl_Should_Clear_DataContext()
  142. {
  143. var (target, _) = CreateTarget();
  144. target.Content = "foo";
  145. Assert.True(target.IsSet(Control.DataContextProperty));
  146. target.Content = new Border();
  147. Assert.False(target.IsSet(Control.DataContextProperty));
  148. }
  149. [Fact]
  150. public void Recycles_DataTemplate()
  151. {
  152. var (target, _) = CreateTarget();
  153. target.DataTemplates.Add(new FuncDataTemplate<string>((_, __) => new Border(), true));
  154. target.Content = "foo";
  155. var control = target.Child;
  156. Assert.IsType<Border>(control);
  157. target.Content = "bar";
  158. Assert.Same(control, target.Child);
  159. }
  160. [Fact]
  161. public void Detects_DataTemplate_Doesnt_Match_And_Doesnt_Recycle()
  162. {
  163. var (target, _) = CreateTarget();
  164. target.DataTemplates.Add(new FuncDataTemplate<string>(x => x == "foo", _ => new Border(), true));
  165. target.Content = "foo";
  166. var control = target.Child;
  167. Assert.IsType<Border>(control);
  168. target.Content = "bar";
  169. Assert.IsType<TextBlock>(target.Child);
  170. }
  171. [Fact]
  172. public void Detects_DataTemplate_Doesnt_Support_Recycling()
  173. {
  174. var (target, _) = CreateTarget();
  175. target.DataTemplates.Add(new FuncDataTemplate<string>((_, __) => new Border(), false));
  176. target.Content = "foo";
  177. var control = target.Child;
  178. Assert.IsType<Border>(control);
  179. target.Content = "bar";
  180. Assert.NotSame(control, target.Child);
  181. }
  182. [Fact]
  183. public void Reevaluates_DataTemplates_When_Recycling()
  184. {
  185. var (target, _) = CreateTarget();
  186. target.DataTemplates.Add(new FuncDataTemplate<string>(x => x == "bar", _ => new Canvas(), true));
  187. target.DataTemplates.Add(new FuncDataTemplate<string>((_, __) => new Border(), true));
  188. target.Content = "foo";
  189. var control = target.Child;
  190. Assert.IsType<Border>(control);
  191. target.Content = "bar";
  192. Assert.IsType<Canvas>(target.Child);
  193. }
  194. [Fact]
  195. public void Should_Not_Bind_Old_Child_To_New_DataContext()
  196. {
  197. // Test for issue #1099.
  198. var textBlock = new TextBlock
  199. {
  200. [!TextBlock.TextProperty] = new Binding(),
  201. };
  202. var (target, host) = CreateTarget();
  203. host.DataTemplates.Add(new FuncDataTemplate<string>((_, __) => textBlock));
  204. host.DataTemplates.Add(new FuncDataTemplate<int>((_, __) => new Canvas()));
  205. target.Content = "foo";
  206. Assert.Same(textBlock, target.Child);
  207. textBlock.PropertyChanged += (s, e) =>
  208. {
  209. Assert.NotEqual(e.NewValue, "42");
  210. };
  211. target.Content = 42;
  212. }
  213. [Fact]
  214. public void Should_Set_InheritanceParent_Even_When_LogicalParent_Is_Already_Set()
  215. {
  216. var logicalParent = new Canvas();
  217. var child = new TextBlock();
  218. var (target, host) = CreateTarget();
  219. ((ISetLogicalParent)child).SetParent(logicalParent);
  220. target.Content = child;
  221. Assert.Same(logicalParent, child.Parent);
  222. // InheritanceParent is exposed via StylingParent.
  223. Assert.Same(target, ((IStyledElement)child).StylingParent);
  224. }
  225. [Fact]
  226. public void Should_Reset_InheritanceParent_When_Child_Removed()
  227. {
  228. var logicalParent = new Canvas();
  229. var child = new TextBlock();
  230. var (target, _) = CreateTarget();
  231. ((ISetLogicalParent)child).SetParent(logicalParent);
  232. target.Content = child;
  233. target.Content = null;
  234. // InheritanceParent is exposed via StylingParent.
  235. Assert.Same(logicalParent, ((IStyledElement)child).StylingParent);
  236. }
  237. (ContentPresenter presenter, ContentControl templatedParent) CreateTarget()
  238. {
  239. var templatedParent = new ContentControl
  240. {
  241. Template = new FuncControlTemplate<ContentControl>((_, s) =>
  242. new ContentPresenter
  243. {
  244. Name = "PART_ContentPresenter",
  245. }.RegisterInNameScope(s)),
  246. };
  247. var root = new TestRoot { Child = templatedParent };
  248. templatedParent.ApplyTemplate();
  249. return ((ContentPresenter)templatedParent.Presenter, templatedParent);
  250. }
  251. private class TestContentControl : ContentControl
  252. {
  253. public IControl Child { get; set; }
  254. }
  255. }
  256. }