TemplatedControlTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Moq;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Styling;
  10. using Avalonia.UnitTests;
  11. using Avalonia.VisualTree;
  12. using Xunit;
  13. using Avalonia.Media;
  14. namespace Avalonia.Controls.UnitTests.Primitives
  15. {
  16. public class TemplatedControlTests : ScopedTestBase
  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 Avalonia.Controls.Primitives.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 Avalonia.Controls.Primitives.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 Avalonia.Controls.Primitives.TemplatedControl
  53. {
  54. Template = new FuncControlTemplate((_, __) => new Decorator
  55. {
  56. Child = new Panel
  57. {
  58. Children =
  59. {
  60. new TextBlock(),
  61. new Border(),
  62. }
  63. }
  64. }),
  65. };
  66. target.ApplyTemplate();
  67. var types = target.GetVisualDescendants().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_Children_Should_Have_TemplatedParent_Set()
  81. {
  82. var target = new Avalonia.Controls.Primitives.TemplatedControl
  83. {
  84. Template = new FuncControlTemplate((_, __) => new Decorator
  85. {
  86. Child = new Panel
  87. {
  88. Children =
  89. {
  90. new TextBlock(),
  91. new Border(),
  92. }
  93. }
  94. }),
  95. };
  96. target.ApplyTemplate();
  97. var templatedParents = target.GetVisualDescendants()
  98. .OfType<Control>()
  99. .Select(x => x.TemplatedParent)
  100. .ToList();
  101. Assert.Equal(4, templatedParents.Count);
  102. Assert.True(templatedParents.All(x => x == target));
  103. }
  104. [Fact]
  105. public void Templated_Child_Should_Have_Parent_Set()
  106. {
  107. var target = new Avalonia.Controls.Primitives.TemplatedControl
  108. {
  109. Template = new FuncControlTemplate((_, __) => new Decorator())
  110. };
  111. target.ApplyTemplate();
  112. var child = (Decorator)target.GetVisualChildren().Single();
  113. Assert.Equal(target, child.Parent);
  114. Assert.Equal(target, child.GetLogicalParent());
  115. }
  116. [Fact]
  117. public void Changing_Template_Should_Clear_Old_Templated_Childs_Parent()
  118. {
  119. var target = new Avalonia.Controls.Primitives.TemplatedControl
  120. {
  121. Template = new FuncControlTemplate((_, __) => new Decorator())
  122. };
  123. target.ApplyTemplate();
  124. var child = (Decorator)target.GetVisualChildren().Single();
  125. target.Template = new FuncControlTemplate((_, __) => new Canvas());
  126. target.ApplyTemplate();
  127. Assert.Null(child.Parent);
  128. }
  129. [Fact]
  130. public void Nested_Templated_Control_Should_Not_Have_Template_Applied()
  131. {
  132. var target = new Avalonia.Controls.Primitives.TemplatedControl
  133. {
  134. Template = new FuncControlTemplate((_, __) => new ScrollViewer())
  135. };
  136. target.ApplyTemplate();
  137. var child = (ScrollViewer)target.GetVisualChildren().Single();
  138. Assert.Empty(child.GetVisualChildren());
  139. }
  140. [Fact]
  141. public void Templated_Children_Should_Be_Styled()
  142. {
  143. TestTemplatedControl target;
  144. var root = new TestRoot
  145. {
  146. Styles =
  147. {
  148. new Style(x => x.Is<Control>())
  149. {
  150. Setters =
  151. {
  152. new Setter(Control.TagProperty, "foo")
  153. }
  154. }
  155. },
  156. Child = target = new TestTemplatedControl
  157. {
  158. Template = new FuncControlTemplate((_, __) =>
  159. {
  160. return new StackPanel
  161. {
  162. Children =
  163. {
  164. new TextBlock
  165. {
  166. }
  167. }
  168. };
  169. }),
  170. }
  171. };
  172. target.ApplyTemplate();
  173. foreach (Control child in target.GetTemplateChildren())
  174. Assert.Equal("foo", child.Tag);
  175. }
  176. [Fact]
  177. public void Nested_Templated_Controls_Have_Correct_TemplatedParent()
  178. {
  179. var target = new TestTemplatedControl
  180. {
  181. Template = new FuncControlTemplate((_, __) =>
  182. {
  183. return new ContentControl
  184. {
  185. Template = new FuncControlTemplate((parent, ___) =>
  186. {
  187. return new Border
  188. {
  189. Child = new ContentPresenter
  190. {
  191. [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty).ToBinding(),
  192. }
  193. };
  194. }),
  195. Content = new Decorator
  196. {
  197. Child = new TextBlock()
  198. }
  199. };
  200. }),
  201. };
  202. target.ApplyTemplate();
  203. var contentControl = target.GetTemplateChildren().OfType<ContentControl>().Single();
  204. contentControl.ApplyTemplate();
  205. var border = contentControl.GetTemplateChildren().OfType<Border>().Single();
  206. var presenter = contentControl.GetTemplateChildren().OfType<ContentPresenter>().Single();
  207. var decorator = (Decorator)presenter.Content;
  208. var textBlock = (TextBlock)decorator.Child;
  209. Assert.Equal(target, contentControl.TemplatedParent);
  210. Assert.Equal(contentControl, border.TemplatedParent);
  211. Assert.Equal(contentControl, presenter.TemplatedParent);
  212. Assert.Equal(target, decorator.TemplatedParent);
  213. Assert.Equal(target, textBlock.TemplatedParent);
  214. }
  215. [Fact]
  216. public void ApplyTemplate_Should_Raise_TemplateApplied()
  217. {
  218. var target = new TestTemplatedControl
  219. {
  220. Template = new FuncControlTemplate((_, __) => new Decorator())
  221. };
  222. var raised = false;
  223. target.TemplateApplied += (s, e) =>
  224. {
  225. Assert.Equal(Avalonia.Controls.Primitives.TemplatedControl.TemplateAppliedEvent, e.RoutedEvent);
  226. Assert.Same(target, e.Source);
  227. Assert.NotNull(e.NameScope);
  228. raised = true;
  229. };
  230. target.ApplyTemplate();
  231. Assert.True(raised);
  232. }
  233. [Fact]
  234. public void Applying_New_Template_Clears_TemplatedParent_Of_Old_Template_Children()
  235. {
  236. var target = new TestTemplatedControl
  237. {
  238. Template = new FuncControlTemplate((_, __) => new Decorator
  239. {
  240. Child = new Border(),
  241. })
  242. };
  243. target.ApplyTemplate();
  244. var decorator = (Decorator)target.GetVisualChildren().Single();
  245. var border = (Border)decorator.Child;
  246. Assert.Equal(target, decorator.TemplatedParent);
  247. Assert.Equal(target, border.TemplatedParent);
  248. target.Template = new FuncControlTemplate((_, __) => new Canvas());
  249. // Templated children should not be removed here: the control may be re-added
  250. // somewhere with the same template, so they could still be of use.
  251. Assert.Same(decorator, target.GetVisualChildren().Single());
  252. Assert.Equal(target, decorator.TemplatedParent);
  253. Assert.Equal(target, border.TemplatedParent);
  254. target.ApplyTemplate();
  255. Assert.Null(decorator.TemplatedParent);
  256. Assert.Null(border.TemplatedParent);
  257. }
  258. [Fact]
  259. public void TemplateChild_AttachedToLogicalTree_Should_Be_Raised()
  260. {
  261. Border templateChild = new Border();
  262. var root = new TestRoot
  263. {
  264. Child = new TestTemplatedControl
  265. {
  266. Template = new FuncControlTemplate((_, __) => new Decorator
  267. {
  268. Child = templateChild,
  269. })
  270. }
  271. };
  272. var raised = false;
  273. templateChild.AttachedToLogicalTree += (s, e) => raised = true;
  274. root.Child.ApplyTemplate();
  275. Assert.True(raised);
  276. }
  277. [Fact]
  278. public void TemplateChild_DetachedFromLogicalTree_Should_Be_Raised()
  279. {
  280. Border templateChild = new Border();
  281. var root = new TestRoot
  282. {
  283. Child = new TestTemplatedControl
  284. {
  285. Template = new FuncControlTemplate((_, __) => new Decorator
  286. {
  287. Child = templateChild,
  288. })
  289. }
  290. };
  291. root.Child.ApplyTemplate();
  292. var raised = false;
  293. templateChild.DetachedFromLogicalTree += (s, e) => raised = true;
  294. root.Child = null;
  295. Assert.True(raised);
  296. }
  297. [Fact]
  298. public void Removing_From_LogicalTree_Should_Not_Remove_Child()
  299. {
  300. Border templateChild = new Border();
  301. TestTemplatedControl target;
  302. var root = new TestRoot
  303. {
  304. Styles =
  305. {
  306. new Style(x => x.OfType<TestTemplatedControl>())
  307. {
  308. Setters =
  309. {
  310. new Setter(
  311. TemplatedControl.TemplateProperty,
  312. new FuncControlTemplate((_, __) => new Decorator
  313. {
  314. Child = new Border(),
  315. }))
  316. }
  317. }
  318. },
  319. Child = target = new TestTemplatedControl()
  320. };
  321. Assert.NotNull(target.Template);
  322. target.ApplyTemplate();
  323. root.Child = null;
  324. Assert.Null(target.Template);
  325. Assert.IsType<Decorator>(target.GetVisualChildren().Single());
  326. }
  327. [Fact]
  328. public void Re_adding_To_Same_LogicalTree_Should_Not_Recreate_Template()
  329. {
  330. TestTemplatedControl target;
  331. var root = new TestRoot
  332. {
  333. Styles =
  334. {
  335. new Style(x => x.OfType<TestTemplatedControl>())
  336. {
  337. Setters =
  338. {
  339. new Setter(
  340. TemplatedControl.TemplateProperty,
  341. new FuncControlTemplate((_, __) => new Decorator
  342. {
  343. Child = new Border(),
  344. }))
  345. }
  346. }
  347. },
  348. Child = target = new TestTemplatedControl()
  349. };
  350. Assert.NotNull(target.Template);
  351. target.ApplyTemplate();
  352. var expected = (Decorator)target.GetVisualChildren().Single();
  353. root.Child = null;
  354. root.Child = target;
  355. target.ApplyTemplate();
  356. Assert.Same(expected, target.GetVisualChildren().Single());
  357. }
  358. [Fact]
  359. public void Re_adding_To_Different_LogicalTree_Should_Recreate_Template()
  360. {
  361. TestTemplatedControl target;
  362. var root = new TestRoot
  363. {
  364. Styles =
  365. {
  366. new Style(x => x.OfType<TestTemplatedControl>())
  367. {
  368. Setters =
  369. {
  370. new Setter(
  371. TemplatedControl.TemplateProperty,
  372. new FuncControlTemplate((_, __) => new Decorator
  373. {
  374. Child = new Border(),
  375. }))
  376. }
  377. }
  378. },
  379. Child = target = new TestTemplatedControl()
  380. };
  381. var root2 = new TestRoot
  382. {
  383. Styles =
  384. {
  385. new Style(x => x.OfType<TestTemplatedControl>())
  386. {
  387. Setters =
  388. {
  389. new Setter(
  390. TemplatedControl.TemplateProperty,
  391. new FuncControlTemplate((_, __) => new Decorator
  392. {
  393. Child = new Border(),
  394. }))
  395. }
  396. }
  397. },
  398. };
  399. Assert.NotNull(target.Template);
  400. target.ApplyTemplate();
  401. var expected = (Decorator)target.GetVisualChildren().Single();
  402. root.Child = null;
  403. root2.Child = target;
  404. target.ApplyTemplate();
  405. var child = target.GetVisualChildren().Single();
  406. Assert.NotNull(target.Template);
  407. Assert.NotNull(child);
  408. Assert.NotSame(expected, child);
  409. }
  410. [Fact]
  411. public void Moving_To_New_LogicalTree_Should_Detach_Attach_Template_Child()
  412. {
  413. TestTemplatedControl target;
  414. var root = new TestRoot
  415. {
  416. Child = target = new TestTemplatedControl
  417. {
  418. Template = new FuncControlTemplate((_, __) => new Decorator()),
  419. }
  420. };
  421. Assert.NotNull(target.Template);
  422. target.ApplyTemplate();
  423. var templateChild = (ILogical)target.GetVisualChildren().Single();
  424. Assert.True(templateChild.IsAttachedToLogicalTree);
  425. root.Child = null;
  426. Assert.False(templateChild.IsAttachedToLogicalTree);
  427. var newRoot = new TestRoot { Child = target };
  428. Assert.True(templateChild.IsAttachedToLogicalTree);
  429. }
  430. [Fact]
  431. public void Templated_Child_Should_Find_Resource_In_TemplatedParent()
  432. {
  433. var target = new ContentControl
  434. {
  435. Resources =
  436. {
  437. { "red", Brushes.Red },
  438. },
  439. Template = new FuncControlTemplate<ContentControl>((x, scope) =>
  440. {
  441. var result = new ContentPresenter
  442. {
  443. Name = "PART_ContentPresenter",
  444. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  445. }.RegisterInNameScope(scope);
  446. result.Bind(ContentPresenter.BackgroundProperty, result.GetResourceObservable("red"));
  447. return result;
  448. }),
  449. };
  450. var root = new TestRoot(target);
  451. target.ApplyTemplate();
  452. var contentPresenter = Assert.IsType<ContentPresenter>(target.GetVisualChildren().Single());
  453. Assert.Same(Brushes.Red, contentPresenter.Background);
  454. }
  455. [Fact]
  456. public void Changing_Resource_In_Templated_Parent_Should_Affect_Templated_Child()
  457. {
  458. var target = new ContentControl
  459. {
  460. Resources =
  461. {
  462. { "red", Brushes.Red },
  463. },
  464. Template = new FuncControlTemplate<ContentControl>((x, scope) =>
  465. {
  466. var result = new ContentPresenter
  467. {
  468. Name = "PART_ContentPresenter",
  469. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  470. }.RegisterInNameScope(scope);
  471. result.Bind(ContentPresenter.BackgroundProperty, result.GetResourceObservable("red"));
  472. return result;
  473. }),
  474. };
  475. var root = new TestRoot(target);
  476. target.ApplyTemplate();
  477. var contentPresenter = Assert.IsType<ContentPresenter>(target.GetVisualChildren().Single());
  478. Assert.Same(Brushes.Red, contentPresenter.Background);
  479. target.Resources["red"] = Brushes.Green;
  480. Assert.Same(Brushes.Green, contentPresenter.Background);
  481. }
  482. private static Control ScrollingContentControlTemplate(ContentControl control, INameScope scope)
  483. {
  484. return new Border
  485. {
  486. Child = new ScrollViewer
  487. {
  488. Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTemplate),
  489. Name = "ScrollViewer",
  490. Content = new ContentPresenter
  491. {
  492. Name = "PART_ContentPresenter",
  493. [!ContentPresenter.ContentProperty] = control[!ContentControl.ContentProperty],
  494. }.RegisterInNameScope(scope)
  495. }.RegisterInNameScope(scope)
  496. };
  497. }
  498. private static Control ScrollViewerTemplate(ScrollViewer control, INameScope scope)
  499. {
  500. var result = new ScrollContentPresenter
  501. {
  502. Name = "PART_ContentPresenter",
  503. [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
  504. }.RegisterInNameScope(scope);
  505. return result;
  506. }
  507. }
  508. }