TemplatedControlTests.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Linq;
  4. using Moq;
  5. using Perspex.Controls;
  6. using Perspex.Controls.Presenters;
  7. using Perspex.Controls.Primitives;
  8. using Perspex.Controls.Templates;
  9. using Perspex.Styling;
  10. using Perspex.VisualTree;
  11. using Xunit;
  12. namespace Perspex.Controls.UnitTests
  13. {
  14. public class TemplatedControlTests
  15. {
  16. [Fact]
  17. public void Template_Doesnt_Get_Executed_On_Set()
  18. {
  19. bool executed = false;
  20. var template = new FuncControlTemplate(_ =>
  21. {
  22. executed = true;
  23. return new Control();
  24. });
  25. var target = new TemplatedControl
  26. {
  27. Template = template,
  28. };
  29. Assert.False(executed);
  30. }
  31. [Fact]
  32. public void Template_Gets_Executed_On_Measure()
  33. {
  34. bool executed = false;
  35. var template = new FuncControlTemplate(_ =>
  36. {
  37. executed = true;
  38. return new Control();
  39. });
  40. var target = new TemplatedControl
  41. {
  42. Template = template,
  43. };
  44. target.Measure(new Size(100, 100));
  45. Assert.True(executed);
  46. }
  47. [Fact]
  48. public void Template_Result_Becomes_Visual_Child()
  49. {
  50. Control templateResult = new Control();
  51. var template = new FuncControlTemplate(_ =>
  52. {
  53. return templateResult;
  54. });
  55. var target = new TemplatedControl
  56. {
  57. Template = template,
  58. };
  59. target.Measure(new Size(100, 100));
  60. var children = target.GetVisualChildren().ToList();
  61. Assert.Equal(new[] { templateResult }, children);
  62. }
  63. [Fact]
  64. public void TemplatedParent_Is_Set_On_Generated_Template()
  65. {
  66. Control templateResult = new Control();
  67. var template = new FuncControlTemplate(_ =>
  68. {
  69. return templateResult;
  70. });
  71. var target = new TemplatedControl
  72. {
  73. Template = template,
  74. };
  75. target.Measure(new Size(100, 100));
  76. Assert.Equal(target, templateResult.TemplatedParent);
  77. }
  78. [Fact]
  79. public void OnTemplateApplied_Is_Called()
  80. {
  81. var target = new TestTemplatedControl
  82. {
  83. Template = new FuncControlTemplate(_ =>
  84. {
  85. return new Control();
  86. })
  87. };
  88. target.Measure(new Size(100, 100));
  89. Assert.True(target.OnTemplateAppliedCalled);
  90. }
  91. [Fact]
  92. public void Template_Should_Be_Instantated()
  93. {
  94. var target = new TestTemplatedControl
  95. {
  96. Template = new FuncControlTemplate(_ =>
  97. {
  98. return new StackPanel
  99. {
  100. Children = new Controls
  101. {
  102. new TextBlock
  103. {
  104. }
  105. }
  106. };
  107. }),
  108. };
  109. target.ApplyTemplate();
  110. var child = target.GetVisualChildren().Single();
  111. Assert.IsType<StackPanel>(child);
  112. child = child.VisualChildren.Single();
  113. Assert.IsType<TextBlock>(child);
  114. }
  115. [Fact]
  116. public void Templated_Children_Should_Be_Styled()
  117. {
  118. using (PerspexLocator.EnterScope())
  119. {
  120. var styler = new Mock<IStyler>();
  121. PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  122. TestTemplatedControl target;
  123. var root = new TestRoot
  124. {
  125. Child = target = new TestTemplatedControl
  126. {
  127. Template = new FuncControlTemplate(_ =>
  128. {
  129. return new StackPanel
  130. {
  131. Children = new Controls
  132. {
  133. new TextBlock
  134. {
  135. }
  136. }
  137. };
  138. }),
  139. }
  140. };
  141. target.ApplyTemplate();
  142. styler.Verify(x => x.ApplyStyles(It.IsAny<TestTemplatedControl>()), Times.Once());
  143. styler.Verify(x => x.ApplyStyles(It.IsAny<StackPanel>()), Times.Once());
  144. styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once());
  145. }
  146. }
  147. [Fact]
  148. public void Templated_Children_Should_Have_TemplatedParent_Set()
  149. {
  150. var target = new TestTemplatedControl
  151. {
  152. Template = new FuncControlTemplate(_ =>
  153. {
  154. return new StackPanel
  155. {
  156. Children = new Controls
  157. {
  158. new TextBlock
  159. {
  160. }
  161. }
  162. };
  163. }),
  164. };
  165. target.ApplyTemplate();
  166. var panel = target.GetTemplateChildren().OfType<StackPanel>().Single();
  167. var textBlock = target.GetTemplateChildren().OfType<TextBlock>().Single();
  168. Assert.Equal(target, panel.TemplatedParent);
  169. Assert.Equal(target, textBlock.TemplatedParent);
  170. }
  171. [Fact]
  172. public void Presenter_Children_Should_Not_Have_TemplatedParent_Set()
  173. {
  174. var target = new TestTemplatedControl
  175. {
  176. Template = new FuncControlTemplate(_ =>
  177. {
  178. return new ContentPresenter
  179. {
  180. Content = new TextBlock
  181. {
  182. }
  183. };
  184. }),
  185. };
  186. target.ApplyTemplate();
  187. var presenter = target.GetTemplateChildren().OfType<ContentPresenter>().Single();
  188. var textBlock = (TextBlock)presenter.Child;
  189. Assert.Equal(target, presenter.TemplatedParent);
  190. Assert.Null(textBlock.TemplatedParent);
  191. }
  192. [Fact]
  193. public void Nested_Templated_Controls_Have_Correct_TemplatedParent()
  194. {
  195. var target = new TestTemplatedControl
  196. {
  197. Template = new FuncControlTemplate(_ =>
  198. {
  199. return new ContentControl
  200. {
  201. Template = new FuncControlTemplate(parent =>
  202. {
  203. return new Border
  204. {
  205. Child = new ContentPresenter
  206. {
  207. [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty),
  208. }
  209. };
  210. }),
  211. Content = new TextBlock
  212. {
  213. }
  214. };
  215. }),
  216. };
  217. target.ApplyTemplate();
  218. var contentControl = target.GetTemplateChildren().OfType<ContentControl>().Single();
  219. var border = contentControl.GetTemplateChildren().OfType<Border>().Single();
  220. var presenter = contentControl.GetTemplateChildren().OfType<ContentPresenter>().Single();
  221. var textBlock = (TextBlock)presenter.Content;
  222. Assert.Equal(target, contentControl.TemplatedParent);
  223. Assert.Equal(contentControl, border.TemplatedParent);
  224. Assert.Equal(contentControl, presenter.TemplatedParent);
  225. Assert.Equal(target, textBlock.TemplatedParent);
  226. }
  227. }
  228. }