TemplatedControlTests.cs 8.2 KB

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