TemplatedControlTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.Generic;
  5. using System.Linq;
  6. using Moq;
  7. using Perspex.Controls.Presenters;
  8. using Perspex.Controls.Primitives;
  9. using Perspex.Controls.Templates;
  10. using Perspex.LogicalTree;
  11. using Perspex.Styling;
  12. using Perspex.VisualTree;
  13. using Xunit;
  14. namespace Perspex.Controls.UnitTests.Primitives
  15. {
  16. public class TemplatedControlTests
  17. {
  18. [Fact]
  19. public void Template_Doesnt_Get_Executed_On_Set()
  20. {
  21. bool executed = false;
  22. var template = new FuncControlTemplate(_ =>
  23. {
  24. executed = true;
  25. return new Control();
  26. });
  27. var target = new TemplatedControl
  28. {
  29. Template = template,
  30. };
  31. Assert.False(executed);
  32. }
  33. [Fact]
  34. public void Template_Gets_Executed_On_Measure()
  35. {
  36. bool executed = false;
  37. var template = new FuncControlTemplate(_ =>
  38. {
  39. executed = true;
  40. return new Control();
  41. });
  42. var target = new TemplatedControl
  43. {
  44. Template = template,
  45. };
  46. target.Measure(new Size(100, 100));
  47. Assert.True(executed);
  48. }
  49. [Fact]
  50. public void ApplyTemplate_Should_Create_Visual_Children()
  51. {
  52. var target = new TemplatedControl
  53. {
  54. Template = new FuncControlTemplate(_ => new Decorator
  55. {
  56. Child = new Panel
  57. {
  58. Children = new Controls
  59. {
  60. new TextBlock(),
  61. new Border(),
  62. }
  63. }
  64. }),
  65. };
  66. target.ApplyTemplate();
  67. var types = target.GetVisualDescendents().Select(x => x.GetType()).ToList();
  68. Assert.Equal(
  69. new[]
  70. {
  71. typeof(Decorator),
  72. typeof(Panel),
  73. typeof(TextBlock),
  74. typeof(Border)
  75. },
  76. types);
  77. Assert.Empty(target.GetLogicalChildren());
  78. }
  79. [Fact]
  80. public void Templated_Child_Should_Be_NameScope()
  81. {
  82. var target = new TemplatedControl
  83. {
  84. Template = new FuncControlTemplate(_ => new Decorator
  85. {
  86. Child = new Panel
  87. {
  88. Children = new Controls
  89. {
  90. new TextBlock(),
  91. new Border(),
  92. }
  93. }
  94. }),
  95. };
  96. target.ApplyTemplate();
  97. Assert.NotNull(NameScope.GetNameScope((Control)target.GetVisualChildren().Single()));
  98. }
  99. [Fact]
  100. public void Templated_Children_Should_Have_TemplatedParent_Set()
  101. {
  102. var target = new TemplatedControl
  103. {
  104. Template = new FuncControlTemplate(_ => new Decorator
  105. {
  106. Child = new Panel
  107. {
  108. Children = new Controls
  109. {
  110. new TextBlock(),
  111. new Border(),
  112. }
  113. }
  114. }),
  115. };
  116. target.ApplyTemplate();
  117. var templatedParents = target.GetVisualDescendents()
  118. .OfType<IControl>()
  119. .Select(x => x.TemplatedParent)
  120. .ToList();
  121. Assert.Equal(4, templatedParents.Count);
  122. Assert.True(templatedParents.All(x => x == target));
  123. }
  124. [Fact]
  125. public void Templated_Child_Should_Have_Parent_Set()
  126. {
  127. var target = new TemplatedControl
  128. {
  129. Template = new FuncControlTemplate(_ => new Decorator())
  130. };
  131. target.ApplyTemplate();
  132. var child = (Decorator)target.GetVisualChildren().Single();
  133. Assert.Equal(target, child.Parent);
  134. Assert.Equal(target, child.GetLogicalParent());
  135. }
  136. [Fact]
  137. public void Nested_Templated_Controls_Have_Correct_TemplatedParent()
  138. {
  139. var target = new TestTemplatedControl
  140. {
  141. Template = new FuncControlTemplate(_ =>
  142. {
  143. return new ContentControl
  144. {
  145. Template = new FuncControlTemplate(parent =>
  146. {
  147. return new Border
  148. {
  149. Child = new ContentPresenter
  150. {
  151. [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty),
  152. }
  153. };
  154. }),
  155. Content = new TextBlock
  156. {
  157. }
  158. };
  159. }),
  160. };
  161. target.ApplyTemplate();
  162. var contentControl = target.GetTemplateChildren().OfType<ContentControl>().Single();
  163. var border = contentControl.GetTemplateChildren().OfType<Border>().Single();
  164. var presenter = contentControl.GetTemplateChildren().OfType<ContentPresenter>().Single();
  165. var textBlock = (TextBlock)presenter.Content;
  166. Assert.Equal(target, contentControl.TemplatedParent);
  167. Assert.Equal(contentControl, border.TemplatedParent);
  168. Assert.Equal(contentControl, presenter.TemplatedParent);
  169. Assert.Equal(target, textBlock.TemplatedParent);
  170. }
  171. [Fact]
  172. public void Templated_Children_Should_Be_Styled()
  173. {
  174. using (PerspexLocator.EnterScope())
  175. {
  176. var styler = new Mock<IStyler>();
  177. PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
  178. TestTemplatedControl target;
  179. var root = new TestRoot
  180. {
  181. Child = target = new TestTemplatedControl
  182. {
  183. Template = new FuncControlTemplate(_ =>
  184. {
  185. return new StackPanel
  186. {
  187. Children = new Controls
  188. {
  189. new TextBlock
  190. {
  191. }
  192. }
  193. };
  194. }),
  195. }
  196. };
  197. target.ApplyTemplate();
  198. styler.Verify(x => x.ApplyStyles(It.IsAny<TestTemplatedControl>()), Times.Once());
  199. styler.Verify(x => x.ApplyStyles(It.IsAny<StackPanel>()), Times.Once());
  200. styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once());
  201. }
  202. }
  203. [Fact]
  204. public void Nested_TemplatedControls_Should_Register_With_Correct_NameScope()
  205. {
  206. var target = new ContentControl
  207. {
  208. Template = new FuncControlTemplate<ContentControl>(ScrollingContentControlTemplate),
  209. Content = "foo"
  210. };
  211. target.ApplyTemplate();
  212. var border = target.GetVisualChildren().FirstOrDefault();
  213. Assert.IsType<Border>(border);
  214. var scrollViewer = border.GetVisualChildren().FirstOrDefault();
  215. Assert.IsType<ScrollViewer>(scrollViewer);
  216. var scrollContentPresenter = scrollViewer.GetVisualChildren().FirstOrDefault();
  217. Assert.IsType<ScrollContentPresenter>(scrollContentPresenter);
  218. var contentPresenter = scrollContentPresenter.GetVisualChildren().FirstOrDefault();
  219. Assert.IsType<ContentPresenter>(contentPresenter);
  220. var borderNs = NameScope.GetNameScope((Control)border);
  221. var scrollContentPresenterNs = NameScope.GetNameScope((Control)scrollContentPresenter);
  222. Assert.NotNull(borderNs);
  223. Assert.Same(scrollViewer, borderNs.Find("ScrollViewer"));
  224. Assert.Same(contentPresenter, borderNs.Find("PART_ContentPresenter"));
  225. Assert.Same(scrollContentPresenter, scrollContentPresenterNs.Find("PART_ContentPresenter"));
  226. }
  227. [Fact]
  228. public void ApplyTemplate_Should_Raise_TemplateApplied()
  229. {
  230. var target = new TestTemplatedControl
  231. {
  232. Template = new FuncControlTemplate(_ => new Decorator())
  233. };
  234. var raised = false;
  235. target.TemplateApplied += (s, e) =>
  236. {
  237. Assert.Equal(TemplatedControl.TemplateAppliedEvent, e.RoutedEvent);
  238. Assert.Same(target, e.Source);
  239. Assert.NotNull(e.NameScope);
  240. raised = true;
  241. };
  242. target.ApplyTemplate();
  243. Assert.True(raised);
  244. }
  245. private static IControl ScrollingContentControlTemplate(ContentControl control)
  246. {
  247. return new Border
  248. {
  249. Child = new ScrollViewer
  250. {
  251. Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTemplate),
  252. Name = "ScrollViewer",
  253. Content = new ContentPresenter
  254. {
  255. Name = "PART_ContentPresenter",
  256. [!ContentPresenter.ContentProperty] = control[!ContentControl.ContentProperty],
  257. }
  258. }
  259. };
  260. }
  261. private static IControl ItemsControlTemplate(ItemsControl control)
  262. {
  263. return new Border
  264. {
  265. Child = new ScrollViewer
  266. {
  267. Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTemplate),
  268. Content = new ItemsPresenter
  269. {
  270. Name = "PART_ItemsPresenter",
  271. [!ItemsPresenter.ItemsProperty] = control[!ItemsControl.ItemsProperty],
  272. [!ItemsPresenter.ItemsPanelProperty] = control[!ItemsControl.ItemsPanelProperty],
  273. }
  274. }
  275. };
  276. }
  277. private static Control ScrollViewerTemplate(ScrollViewer control)
  278. {
  279. var result = new ScrollContentPresenter
  280. {
  281. Name = "PART_ContentPresenter",
  282. [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
  283. };
  284. return result;
  285. }
  286. private class ApplyTemplateTracker : Control
  287. {
  288. public List<Tuple<IVisual, ILogical>> Invocations { get; } = new List<Tuple<IVisual, ILogical>>();
  289. public override void ApplyTemplate()
  290. {
  291. base.ApplyTemplate();
  292. Invocations.Add(Tuple.Create(this.GetVisualParent(), this.GetLogicalParent()));
  293. }
  294. }
  295. }
  296. }