ContentControlTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.Specialized;
  5. using System.Linq;
  6. using Moq;
  7. using Perspex.Controls.Presenters;
  8. using Perspex.Controls.Templates;
  9. using Perspex.LogicalTree;
  10. using Perspex.Platform;
  11. using Perspex.Styling;
  12. using Perspex.VisualTree;
  13. using Ploeh.AutoFixture;
  14. using Ploeh.AutoFixture.AutoMoq;
  15. using Xunit;
  16. namespace Perspex.Controls.UnitTests
  17. {
  18. public class ContentControlTests
  19. {
  20. [Fact]
  21. public void Template_Should_Be_Instantiated()
  22. {
  23. using (var ctx = RegisterServices())
  24. {
  25. var target = new ContentControl();
  26. target.Content = "Foo";
  27. target.Template = GetTemplate();
  28. target.Measure(new Size(100, 100));
  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. }
  37. [Fact]
  38. public void Templated_Children_Should_Be_Styled()
  39. {
  40. using (var ctx = RegisterServices())
  41. {
  42. var root = new TestRoot();
  43. var target = new ContentControl();
  44. var styler = new Mock<IStyler>();
  45. PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  46. target.Content = "Foo";
  47. target.Template = GetTemplate();
  48. root.Child = target;
  49. target.ApplyTemplate();
  50. styler.Verify(x => x.ApplyStyles(It.IsAny<ContentControl>()), Times.Once());
  51. styler.Verify(x => x.ApplyStyles(It.IsAny<Border>()), Times.Once());
  52. styler.Verify(x => x.ApplyStyles(It.IsAny<ContentPresenter>()), Times.Once());
  53. styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once());
  54. }
  55. }
  56. [Fact]
  57. public void ContentPresenter_Should_Have_TemplatedParent_Set()
  58. {
  59. var target = new ContentControl();
  60. var child = new Border();
  61. target.Template = GetTemplate();
  62. target.Content = child;
  63. target.ApplyTemplate();
  64. var contentPresenter = child.GetVisualParent<ContentPresenter>();
  65. Assert.Equal(target, contentPresenter.TemplatedParent);
  66. }
  67. [Fact]
  68. public void Content_Should_Have_TemplatedParent_Set_To_Null()
  69. {
  70. var target = new ContentControl();
  71. var child = new Border();
  72. target.Template = GetTemplate();
  73. target.Content = child;
  74. target.ApplyTemplate();
  75. Assert.Null(child.TemplatedParent);
  76. }
  77. [Fact]
  78. public void Setting_Content_To_Control_Should_Set_Child_Controls_Parent()
  79. {
  80. var target = new ContentControl
  81. {
  82. Template = GetTemplate(),
  83. };
  84. var child = new Control();
  85. target.Content = child;
  86. target.ApplyTemplate();
  87. Assert.Equal(child.Parent, target);
  88. Assert.Equal(((ILogical)child).LogicalParent, target);
  89. }
  90. [Fact]
  91. public void Setting_Content_To_String_Should_Set_Child_Controls_Parent()
  92. {
  93. var target = new ContentControl
  94. {
  95. Template = GetTemplate(),
  96. };
  97. target.Content = "Foo";
  98. target.ApplyTemplate();
  99. var child = target.Presenter.Child;
  100. Assert.Equal(child.Parent, target);
  101. Assert.Equal(((ILogical)child).LogicalParent, target);
  102. }
  103. [Fact]
  104. public void Clearing_Content_Should_Clear_Child_Controls_Parent()
  105. {
  106. var target = new ContentControl();
  107. var child = new Control();
  108. target.Content = child;
  109. target.Content = null;
  110. Assert.Null(child.Parent);
  111. Assert.Null(((ILogical)child).LogicalParent);
  112. }
  113. [Fact]
  114. public void Setting_Content_To_Control_Should_Make_Control_Appear_In_LogicalChildren()
  115. {
  116. var target = new ContentControl();
  117. var child = new Control();
  118. target.Template = GetTemplate();
  119. target.Content = child;
  120. target.ApplyTemplate();
  121. Assert.Equal(new[] { child }, ((ILogical)target).LogicalChildren.ToList());
  122. }
  123. [Fact]
  124. public void Setting_Content_To_String_Should_Make_TextBlock_Appear_In_LogicalChildren()
  125. {
  126. var target = new ContentControl();
  127. var child = new Control();
  128. target.Template = GetTemplate();
  129. target.Content = "Foo";
  130. target.ApplyTemplate();
  131. var logical = (ILogical)target;
  132. Assert.Equal(1, logical.LogicalChildren.Count);
  133. Assert.IsType<TextBlock>(logical.LogicalChildren[0]);
  134. }
  135. [Fact]
  136. public void Clearing_Content_Should_Remove_From_LogicalChildren()
  137. {
  138. var target = new ContentControl();
  139. var child = new Control();
  140. target.Template = GetTemplate();
  141. target.Content = child;
  142. target.ApplyTemplate();
  143. target.Content = null;
  144. // Need to call ApplyTemplate on presenter for LogicalChildren to be updated.
  145. target.Presenter.ApplyTemplate();
  146. Assert.Empty(target.GetLogicalChildren());
  147. }
  148. [Fact]
  149. public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged()
  150. {
  151. var target = new ContentControl();
  152. var child = new Control();
  153. var called = false;
  154. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  155. called = e.Action == NotifyCollectionChangedAction.Add;
  156. target.Template = GetTemplate();
  157. target.Content = child;
  158. target.ApplyTemplate();
  159. // Need to call ApplyTemplate on presenter for LogicalChildren to be updated.
  160. target.Presenter.ApplyTemplate();
  161. Assert.True(called);
  162. }
  163. [Fact]
  164. public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  165. {
  166. var target = new ContentControl();
  167. var child = new Control();
  168. var called = false;
  169. target.Template = GetTemplate();
  170. target.Content = child;
  171. target.ApplyTemplate();
  172. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  173. target.Content = null;
  174. // Need to call ApplyTemplate on presenter for CollectionChanged to be called.
  175. var presenter = target.GetTemplateChildren().Single(x => x.Name == "contentPresenter");
  176. presenter.ApplyTemplate();
  177. Assert.True(called);
  178. }
  179. [Fact]
  180. public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  181. {
  182. var contentControl = new ContentControl();
  183. var child1 = new Control();
  184. var child2 = new Control();
  185. var called = false;
  186. contentControl.Template = GetTemplate();
  187. contentControl.Content = child1;
  188. contentControl.ApplyTemplate();
  189. ((ILogical)contentControl).LogicalChildren.CollectionChanged += (s, e) => called = true;
  190. contentControl.Content = child2;
  191. // Need to call ApplyTemplate on presenter for CollectionChanged to be called.
  192. var presenter = contentControl.GetTemplateChildren().Single(x => x.Name == "contentPresenter");
  193. presenter.ApplyTemplate();
  194. Assert.True(called);
  195. }
  196. [Fact]
  197. public void Changing_Content_Should_Update_Presenter()
  198. {
  199. var target = new ContentControl();
  200. target.Template = GetTemplate();
  201. target.ApplyTemplate();
  202. target.Content = "Foo";
  203. target.Presenter.ApplyTemplate();
  204. Assert.Equal("Foo", ((TextBlock)target.Presenter.Child).Text);
  205. target.Content = "Bar";
  206. target.Presenter.ApplyTemplate();
  207. Assert.Equal("Bar", ((TextBlock)target.Presenter.Child).Text);
  208. }
  209. [Fact]
  210. public void DataContext_Should_Be_Set_For_Templated_Data()
  211. {
  212. var target = new ContentControl();
  213. target.Template = GetTemplate();
  214. target.Content = "Foo";
  215. target.ApplyTemplate();
  216. Assert.Equal("Foo", target.Presenter.Child.DataContext);
  217. }
  218. [Fact]
  219. public void DataContext_Should_Not_Be_Set_For_Control_Data()
  220. {
  221. var target = new ContentControl();
  222. target.Template = GetTemplate();
  223. target.Content = new TextBlock();
  224. target.ApplyTemplate();
  225. Assert.Null(target.Presenter.Child.DataContext);
  226. }
  227. private FuncControlTemplate GetTemplate()
  228. {
  229. return new FuncControlTemplate<ContentControl>(parent =>
  230. {
  231. return new Border
  232. {
  233. Background = new Media.SolidColorBrush(0xffffffff),
  234. Child = new ContentPresenter
  235. {
  236. Name = "contentPresenter",
  237. [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
  238. }
  239. };
  240. });
  241. }
  242. private IDisposable RegisterServices()
  243. {
  244. var result = PerspexLocator.EnterScope();
  245. var fixture = new Fixture().Customize(new AutoMoqCustomization());
  246. var renderInterface = fixture.Create<IPlatformRenderInterface>();
  247. PerspexLocator.CurrentMutable
  248. .Bind<IPlatformRenderInterface>().ToConstant(renderInterface);
  249. return result;
  250. }
  251. }
  252. }