1
0

ContentPresenterTests_Standalone.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using Avalonia.Controls.Presenters;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Controls.Templates;
  4. using Avalonia.LogicalTree;
  5. using Avalonia.Styling;
  6. using Avalonia.UnitTests;
  7. using Avalonia.VisualTree;
  8. using Moq;
  9. using System;
  10. using System.Linq;
  11. using Xunit;
  12. using Avalonia.Rendering;
  13. using Avalonia.Media;
  14. using Avalonia.Data;
  15. namespace Avalonia.Controls.UnitTests.Presenters
  16. {
  17. /// <summary>
  18. /// Tests for ContentControls that aren't hosted in a control template.
  19. /// </summary>
  20. public class ContentPresenterTests_Standalone : ScopedTestBase
  21. {
  22. [Fact]
  23. public void Should_Set_Childs_Parent_To_Itself_Standalone()
  24. {
  25. var content = new Border();
  26. var target = new ContentPresenter { Content = content };
  27. target.UpdateChild();
  28. Assert.Same(target, content.Parent);
  29. }
  30. [Fact]
  31. public void Should_Add_Child_To_Own_LogicalChildren_Standalone()
  32. {
  33. var content = new Border();
  34. var target = new ContentPresenter { Content = content };
  35. target.UpdateChild();
  36. var logicalChildren = target.GetLogicalChildren();
  37. Assert.Single(logicalChildren);
  38. Assert.Equal(content, logicalChildren.First());
  39. }
  40. [Fact]
  41. public void Should_Raise_DetachedFromLogicalTree_On_Content_Changed_Standalone()
  42. {
  43. var target = new ContentPresenter
  44. {
  45. ContentTemplate =
  46. new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
  47. };
  48. var parentMock = new Mock<Control>();
  49. parentMock.As<IContentPresenterHost>();
  50. parentMock.As<IRenderRoot>();
  51. parentMock.As<ILogicalRoot>();
  52. (target as ISetLogicalParent).SetParent(parentMock.Object);
  53. target.Content = "foo";
  54. target.UpdateChild();
  55. var foo = target.Child as ContentControl;
  56. bool foodetached = false;
  57. Assert.NotNull(foo);
  58. Assert.Equal("foo", foo.Content);
  59. foo.DetachedFromLogicalTree += delegate { foodetached = true; };
  60. target.Content = "bar";
  61. target.UpdateChild();
  62. var bar = target.Child as ContentControl;
  63. Assert.NotNull(bar);
  64. Assert.True(bar != foo);
  65. Assert.False((foo as ILogical).IsAttachedToLogicalTree);
  66. Assert.True(foodetached);
  67. }
  68. [Fact]
  69. public void Should_Raise_DetachedFromLogicalTree_In_ContentControl_On_Content_Changed_Standalone()
  70. {
  71. var contentControl = new ContentControl
  72. {
  73. Template = new FuncControlTemplate<ContentControl>((c, scope) => new ContentPresenter()
  74. {
  75. Name = "PART_ContentPresenter",
  76. [~ContentPresenter.ContentProperty] = c[~ContentControl.ContentProperty],
  77. [~ContentPresenter.ContentTemplateProperty] = c[~ContentControl.ContentTemplateProperty]
  78. }.RegisterInNameScope(scope)),
  79. ContentTemplate =
  80. new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
  81. };
  82. var parentMock = new Mock<Control>();
  83. parentMock.As<IRenderRoot>();
  84. parentMock.As<ILogicalRoot>();
  85. parentMock.As<ILogical>().SetupGet(l => l.IsAttachedToLogicalTree).Returns(true);
  86. (contentControl as ISetLogicalParent).SetParent(parentMock.Object);
  87. contentControl.ApplyTemplate();
  88. var target = contentControl.Presenter as ContentPresenter;
  89. contentControl.Content = "foo";
  90. target.UpdateChild();
  91. var tbfoo = target.Child as ContentControl;
  92. bool foodetached = false;
  93. Assert.NotNull(tbfoo);
  94. Assert.Equal("foo", tbfoo.Content);
  95. tbfoo.DetachedFromLogicalTree += delegate { foodetached = true; };
  96. contentControl.Content = "bar";
  97. target.UpdateChild();
  98. var tbbar = target.Child as ContentControl;
  99. Assert.NotNull(tbbar);
  100. Assert.True(tbbar != tbfoo);
  101. Assert.False((tbfoo as ILogical).IsAttachedToLogicalTree);
  102. Assert.True(foodetached);
  103. }
  104. [Fact]
  105. public void Should_Raise_DetachedFromLogicalTree_On_Detached_Standalone()
  106. {
  107. var target = new ContentPresenter
  108. {
  109. ContentTemplate =
  110. new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
  111. };
  112. var parentMock = new Mock<Control>();
  113. parentMock.As<IContentPresenterHost>();
  114. parentMock.As<IRenderRoot>();
  115. parentMock.As<ILogicalRoot>();
  116. (target as ISetLogicalParent).SetParent(parentMock.Object);
  117. target.Content = "foo";
  118. target.UpdateChild();
  119. var foo = target.Child as ContentControl;
  120. bool foodetached = false;
  121. Assert.NotNull(foo);
  122. Assert.Equal("foo", foo.Content);
  123. foo.DetachedFromLogicalTree += delegate { foodetached = true; };
  124. (target as ISetLogicalParent).SetParent(null);
  125. Assert.False((foo as ILogical).IsAttachedToLogicalTree);
  126. Assert.True(foodetached);
  127. }
  128. [Fact]
  129. public void Should_Remove_Old_Child_From_LogicalChildren_On_ContentChanged_Standalone()
  130. {
  131. var target = new ContentPresenter
  132. {
  133. ContentTemplate =
  134. new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
  135. };
  136. target.Content = "foo";
  137. target.UpdateChild();
  138. var foo = target.Child as ContentControl;
  139. Assert.NotNull(foo);
  140. var logicalChildren = target.GetLogicalChildren();
  141. Assert.Single(logicalChildren);
  142. target.Content = "bar";
  143. target.UpdateChild();
  144. Assert.Null(foo.Parent);
  145. logicalChildren = target.GetLogicalChildren();
  146. Assert.Single(logicalChildren);
  147. Assert.NotEqual(foo, logicalChildren.First());
  148. }
  149. [Fact]
  150. public void Should_Not_Bind_Old_Child_To_New_DataContext()
  151. {
  152. // Test for issue #1099.
  153. var textBlock = new TextBlock
  154. {
  155. [!TextBlock.TextProperty] = new Binding(),
  156. };
  157. var target = new ContentPresenter()
  158. {
  159. DataTemplates =
  160. {
  161. new FuncDataTemplate<string>((x, _) => textBlock),
  162. new FuncDataTemplate<int>((x, _) => new Canvas()),
  163. },
  164. };
  165. var root = new TestRoot(target);
  166. target.Content = "foo";
  167. Assert.Same(textBlock, target.Child);
  168. textBlock.PropertyChanged += (s, e) =>
  169. {
  170. Assert.NotEqual(e.NewValue, "42");
  171. };
  172. target.Content = 42;
  173. }
  174. [Fact]
  175. public void Should_Reset_InheritanceParent_When_Child_Removed()
  176. {
  177. var logicalParent = new Canvas();
  178. var child = new TextBlock();
  179. var target = new ContentPresenter();
  180. var root = new TestRoot(target);
  181. ((ISetLogicalParent)child).SetParent(logicalParent);
  182. target.Content = child;
  183. target.Content = null;
  184. // InheritanceParent is exposed via StylingParent.
  185. Assert.Same(logicalParent, ((IStyleHost)child).StylingParent);
  186. }
  187. [Fact]
  188. public void Should_Create_Child_Even_With_Null_Content_When_ContentTemplate_Is_Set()
  189. {
  190. var target = new ContentPresenter
  191. {
  192. ContentTemplate = new FuncDataTemplate<object>(_ => true, (_, __) => new TextBlock
  193. {
  194. Text = "Hello World"
  195. }),
  196. Content = null
  197. };
  198. target.UpdateChild();
  199. var textBlock = Assert.IsType<TextBlock>(target.Child);
  200. Assert.Equal("Hello World", textBlock.Text);
  201. }
  202. [Fact]
  203. public void Should_Not_Create_Child_Even_With_Null_Content_And_DataTemplates_InsteadOf_ContentTemplate()
  204. {
  205. var target = new ContentPresenter
  206. {
  207. DataTemplates =
  208. {
  209. new FuncDataTemplate<object>(_ => true, (_, __) => new TextBlock
  210. {
  211. Text = "Hello World"
  212. })
  213. },
  214. Content = null
  215. };
  216. target.UpdateChild();
  217. Assert.Null(target.Child);
  218. }
  219. [Fact]
  220. public void Should_Not_Create_Child_When_Content_And_Template_Are_Null()
  221. {
  222. var target = new ContentPresenter
  223. {
  224. ContentTemplate = null,
  225. Content = null
  226. };
  227. target.UpdateChild();
  228. Assert.Null(target.Child);
  229. }
  230. [Fact]
  231. public void Should_Not_Create_When_Child_Content_Is_Null_But_Expected_ValueType_With_FuncDataTemplate()
  232. {
  233. var target = new ContentPresenter
  234. {
  235. ContentTemplate = new FuncDataTemplate<int>(_ => true, (_, __) => new TextBlock
  236. {
  237. Text = "Hello World"
  238. }),
  239. Content = null
  240. };
  241. target.UpdateChild();
  242. Assert.Null(target.Child);
  243. }
  244. [Fact]
  245. public void Should_Create_Child_When_Content_Is_Null_And_Expected_NullableValueType_With_FuncDataTemplate()
  246. {
  247. var target = new ContentPresenter
  248. {
  249. ContentTemplate = new FuncDataTemplate<int?>(_ => true, (_, __) => new TextBlock
  250. {
  251. Text = "Hello World"
  252. }),
  253. Content = null
  254. };
  255. target.UpdateChild();
  256. Assert.NotNull(target.Child);
  257. }
  258. }
  259. }