ContentControlTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using Moq;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.LogicalTree;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  13. using Xunit;
  14. using Avalonia.Markup.Data;
  15. using Avalonia.Data;
  16. using System.Collections.Generic;
  17. namespace Avalonia.Controls.UnitTests
  18. {
  19. public class ContentControlTests
  20. {
  21. [Fact]
  22. public void Template_Should_Be_Instantiated()
  23. {
  24. var target = new ContentControl();
  25. target.Content = "Foo";
  26. target.Template = GetTemplate();
  27. target.ApplyTemplate();
  28. ((ContentPresenter)target.Presenter).UpdateChild();
  29. var child = ((IVisual)target).VisualChildren.Single();
  30. Assert.IsType<Border>(child);
  31. child = child.VisualChildren.Single();
  32. Assert.IsType<ContentPresenter>(child);
  33. child = child.VisualChildren.Single();
  34. Assert.IsType<TextBlock>(child);
  35. }
  36. [Fact]
  37. public void Templated_Children_Should_Be_Styled()
  38. {
  39. var root = new TestRoot();
  40. var target = new ContentControl();
  41. var styler = new Mock<IStyler>();
  42. AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  43. target.Content = "Foo";
  44. target.Template = GetTemplate();
  45. root.Child = target;
  46. target.ApplyTemplate();
  47. styler.Verify(x => x.ApplyStyles(It.IsAny<ContentControl>()), Times.Once());
  48. styler.Verify(x => x.ApplyStyles(It.IsAny<Border>()), Times.Once());
  49. styler.Verify(x => x.ApplyStyles(It.IsAny<ContentPresenter>()), Times.Once());
  50. styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once());
  51. }
  52. [Fact]
  53. public void ContentPresenter_Should_Have_TemplatedParent_Set()
  54. {
  55. var target = new ContentControl();
  56. var child = new Border();
  57. target.Template = GetTemplate();
  58. target.Content = child;
  59. target.ApplyTemplate();
  60. ((ContentPresenter)target.Presenter).UpdateChild();
  61. var contentPresenter = child.GetVisualParent<ContentPresenter>();
  62. Assert.Equal(target, contentPresenter.TemplatedParent);
  63. }
  64. [Fact]
  65. public void Content_Should_Have_TemplatedParent_Set_To_Null()
  66. {
  67. var target = new ContentControl();
  68. var child = new Border();
  69. target.Template = GetTemplate();
  70. target.Content = child;
  71. target.ApplyTemplate();
  72. ((ContentPresenter)target.Presenter).UpdateChild();
  73. Assert.Null(child.TemplatedParent);
  74. }
  75. [Fact]
  76. public void Control_Content_Should_Be_Logical_Child_Before_ApplyTemplate()
  77. {
  78. var target = new ContentControl
  79. {
  80. Template = GetTemplate(),
  81. };
  82. var child = new Control();
  83. target.Content = child;
  84. Assert.Equal(child.Parent, target);
  85. Assert.Equal(child.GetLogicalParent(), target);
  86. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  87. }
  88. [Fact]
  89. public void Control_Content_Should_Be_Logical_Child_After_ApplyTemplate()
  90. {
  91. var target = new ContentControl
  92. {
  93. Template = GetTemplate(),
  94. };
  95. var child = new Control();
  96. target.Content = child;
  97. target.ApplyTemplate();
  98. ((ContentPresenter)target.Presenter).UpdateChild();
  99. Assert.Equal(child.Parent, target);
  100. Assert.Equal(child.GetLogicalParent(), target);
  101. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  102. }
  103. [Fact]
  104. public void Should_Use_ContentTemplate_To_Create_Control()
  105. {
  106. var target = new ContentControl
  107. {
  108. Template = GetTemplate(),
  109. ContentTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  110. };
  111. target.Content = "Foo";
  112. target.ApplyTemplate();
  113. ((ContentPresenter)target.Presenter).UpdateChild();
  114. var child = target.Presenter.Child;
  115. Assert.IsType<Canvas>(child);
  116. }
  117. [Fact]
  118. public void DataTemplate_Created_Control_Should_Be_Logical_Child_After_ApplyTemplate()
  119. {
  120. var target = new ContentControl
  121. {
  122. Template = GetTemplate(),
  123. };
  124. target.Content = "Foo";
  125. target.ApplyTemplate();
  126. ((ContentPresenter)target.Presenter).UpdateChild();
  127. var child = target.Presenter.Child;
  128. Assert.NotNull(child);
  129. Assert.Equal(target, child.Parent);
  130. Assert.Equal(target, child.GetLogicalParent());
  131. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  132. }
  133. [Fact]
  134. public void Clearing_Content_Should_Clear_Logical_Child()
  135. {
  136. var target = new ContentControl();
  137. var child = new Control();
  138. target.Content = child;
  139. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  140. target.Content = null;
  141. Assert.Null(child.Parent);
  142. Assert.Null(child.GetLogicalParent());
  143. Assert.Empty(target.GetLogicalChildren());
  144. }
  145. [Fact]
  146. public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged()
  147. {
  148. var target = new ContentControl();
  149. var child = new Control();
  150. var called = false;
  151. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  152. called = e.Action == NotifyCollectionChangedAction.Add;
  153. target.Template = GetTemplate();
  154. target.Content = child;
  155. target.ApplyTemplate();
  156. ((ContentPresenter)target.Presenter).UpdateChild();
  157. Assert.True(called);
  158. }
  159. [Fact]
  160. public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  161. {
  162. var target = new ContentControl();
  163. var child = new Control();
  164. var called = false;
  165. target.Template = GetTemplate();
  166. target.Content = child;
  167. target.ApplyTemplate();
  168. ((ContentPresenter)target.Presenter).UpdateChild();
  169. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  170. target.Content = null;
  171. ((ContentPresenter)target.Presenter).UpdateChild();
  172. Assert.True(called);
  173. }
  174. [Fact]
  175. public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  176. {
  177. var target = new ContentControl();
  178. var child1 = new Control();
  179. var child2 = new Control();
  180. var called = false;
  181. target.Template = GetTemplate();
  182. target.Content = child1;
  183. target.ApplyTemplate();
  184. ((ContentPresenter)target.Presenter).UpdateChild();
  185. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  186. target.Content = child2;
  187. target.Presenter.ApplyTemplate();
  188. Assert.True(called);
  189. }
  190. [Fact]
  191. public void Changing_Content_Should_Update_Presenter()
  192. {
  193. var target = new ContentControl();
  194. target.Template = GetTemplate();
  195. target.ApplyTemplate();
  196. ((ContentPresenter)target.Presenter).UpdateChild();
  197. target.Content = "Foo";
  198. ((ContentPresenter)target.Presenter).UpdateChild();
  199. Assert.Equal("Foo", ((TextBlock)target.Presenter.Child).Text);
  200. target.Content = "Bar";
  201. ((ContentPresenter)target.Presenter).UpdateChild();
  202. Assert.Equal("Bar", ((TextBlock)target.Presenter.Child).Text);
  203. }
  204. [Fact]
  205. public void DataContext_Should_Be_Set_For_DataTemplate_Created_Content()
  206. {
  207. var target = new ContentControl();
  208. target.Template = GetTemplate();
  209. target.Content = "Foo";
  210. target.ApplyTemplate();
  211. ((ContentPresenter)target.Presenter).UpdateChild();
  212. Assert.Equal("Foo", target.Presenter.Child.DataContext);
  213. }
  214. [Fact]
  215. public void DataContext_Should_Not_Be_Set_For_Control_Content()
  216. {
  217. var target = new ContentControl();
  218. target.Template = GetTemplate();
  219. target.Content = new TextBlock();
  220. target.ApplyTemplate();
  221. ((ContentPresenter)target.Presenter).UpdateChild();
  222. Assert.Null(target.Presenter.Child.DataContext);
  223. }
  224. [Fact]
  225. public void Binding_ContentTemplate_After_Content_Does_Not_Leave_Orpaned_TextBlock()
  226. {
  227. // Test for #1271.
  228. var children = new List<IControl>();
  229. var presenter = new ContentPresenter();
  230. // The content and then the content template property need to be bound with delayed bindings
  231. // as they are in Avalonia.Markup.Xaml.
  232. DelayedBinding.Add(presenter, ContentPresenter.ContentProperty, new Binding("Content")
  233. {
  234. Priority = BindingPriority.TemplatedParent,
  235. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  236. });
  237. DelayedBinding.Add(presenter, ContentPresenter.ContentTemplateProperty, new Binding("ContentTemplate")
  238. {
  239. Priority = BindingPriority.TemplatedParent,
  240. RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
  241. });
  242. presenter.GetObservable(ContentPresenter.ChildProperty).Subscribe(children.Add);
  243. var target = new ContentControl
  244. {
  245. Template = new FuncControlTemplate<ContentControl>((_, __) => presenter),
  246. ContentTemplate = new FuncDataTemplate<string>((_, __) => new Canvas()),
  247. Content = "foo",
  248. };
  249. // The control must be rooted.
  250. var root = new TestRoot
  251. {
  252. Child = target,
  253. };
  254. target.ApplyTemplate();
  255. // When the template is applied, the Content property is bound before the ContentTemplate
  256. // property, causing a TextBlock to be created by the default template before ContentTemplate
  257. // is bound.
  258. Assert.Collection(
  259. children,
  260. x => Assert.Null(x),
  261. x => Assert.IsType<TextBlock>(x),
  262. x => Assert.IsType<Canvas>(x));
  263. var textBlock = (TextBlock)children[1];
  264. // The leak in #1271 was caused by the TextBlock's logical parent not being cleared when
  265. // it is replaced by the Canvas.
  266. Assert.Null(textBlock.GetLogicalParent());
  267. }
  268. [Fact]
  269. public void Should_Set_Child_LogicalParent_After_Removing_And_Adding_Back_To_Logical_Tree()
  270. {
  271. using (UnitTestApplication.Start(TestServices.RealStyler))
  272. {
  273. var target = new ContentControl();
  274. var root = new TestRoot
  275. {
  276. Styles =
  277. {
  278. new Style(x => x.OfType<ContentControl>())
  279. {
  280. Setters =
  281. {
  282. new Setter(ContentControl.TemplateProperty, GetTemplate()),
  283. }
  284. }
  285. },
  286. Child = target
  287. };
  288. target.Content = "Foo";
  289. target.ApplyTemplate();
  290. Assert.Equal(target, target.Presenter.Child.LogicalParent);
  291. root.Child = null;
  292. Assert.Null(target.Template);
  293. target.Content = null;
  294. root.Child = target;
  295. target.Content = "Bar";
  296. Assert.Equal(target, target.Presenter.Child.LogicalParent);
  297. }
  298. }
  299. private FuncControlTemplate GetTemplate()
  300. {
  301. return new FuncControlTemplate<ContentControl>((parent, scope) =>
  302. {
  303. return new Border
  304. {
  305. Background = new Media.SolidColorBrush(0xffffffff),
  306. Child = new ContentPresenter
  307. {
  308. Name = "PART_ContentPresenter",
  309. [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
  310. [~ContentPresenter.ContentTemplateProperty] = parent[~ContentControl.ContentTemplateProperty],
  311. }.RegisterInNameScope(scope)
  312. };
  313. });
  314. }
  315. }
  316. }