ContentPresenterTests_Standalone.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.LogicalTree;
  7. using Avalonia.Styling;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. using Moq;
  11. using System;
  12. using System.Linq;
  13. using Xunit;
  14. namespace Avalonia.Controls.UnitTests.Presenters
  15. {
  16. /// <summary>
  17. /// Tests for ContentControls that aren't hosted in a control template.
  18. /// </summary>
  19. public class ContentPresenterTests_Standalone
  20. {
  21. [Fact]
  22. public void Should_Set_Childs_Parent_To_Itself_Standalone()
  23. {
  24. var content = new Border();
  25. var target = new ContentPresenter { Content = content };
  26. target.UpdateChild();
  27. Assert.Same(target, content.Parent);
  28. }
  29. [Fact]
  30. public void Should_Add_Child_To_Own_LogicalChildren_Standalone()
  31. {
  32. var content = new Border();
  33. var target = new ContentPresenter { Content = content };
  34. target.UpdateChild();
  35. var logicalChildren = target.GetLogicalChildren();
  36. Assert.Equal(1, logicalChildren.Count());
  37. Assert.Equal(content, logicalChildren.First());
  38. }
  39. [Fact]
  40. public void Should_Raise_DetachedFromLogicalTree_On_Content_Changed_Standalone()
  41. {
  42. var target = new ContentPresenter
  43. {
  44. ContentTemplate =
  45. new FuncDataTemplate<string>(t => new ContentControl() { Content = t }, false)
  46. };
  47. var parentMock = new Mock<Control>();
  48. parentMock.As<IContentPresenterHost>();
  49. parentMock.As<IStyleRoot>();
  50. (target as ISetLogicalParent).SetParent(parentMock.Object);
  51. target.Content = "foo";
  52. target.UpdateChild();
  53. var foo = target.Child as ContentControl;
  54. bool foodetached = false;
  55. Assert.NotNull(foo);
  56. Assert.Equal("foo", foo.Content);
  57. foo.DetachedFromLogicalTree += delegate { foodetached = true; };
  58. target.Content = "bar";
  59. target.UpdateChild();
  60. var bar = target.Child as ContentControl;
  61. Assert.NotNull(bar);
  62. Assert.True(bar != foo);
  63. Assert.False((foo as IControl).IsAttachedToLogicalTree);
  64. Assert.True(foodetached);
  65. }
  66. [Fact]
  67. public void Should_Raise_DetachedFromLogicalTree_In_ContentControl_On_Content_Changed_Standalone()
  68. {
  69. var contentControl = new ContentControl
  70. {
  71. Template = new FuncControlTemplate<ContentControl>(c => new ContentPresenter()
  72. {
  73. Name = "PART_ContentPresenter",
  74. [~ContentPresenter.ContentProperty] = c[~ContentControl.ContentProperty],
  75. [~ContentPresenter.ContentTemplateProperty] = c[~ContentControl.ContentTemplateProperty]
  76. }),
  77. ContentTemplate =
  78. new FuncDataTemplate<string>(t => new ContentControl() { Content = t }, false)
  79. };
  80. var parentMock = new Mock<Control>();
  81. parentMock.As<IStyleRoot>();
  82. parentMock.As<ILogical>().SetupGet(l => l.IsAttachedToLogicalTree).Returns(true);
  83. (contentControl as ISetLogicalParent).SetParent(parentMock.Object);
  84. contentControl.ApplyTemplate();
  85. var target = contentControl.Presenter as ContentPresenter;
  86. contentControl.Content = "foo";
  87. target.UpdateChild();
  88. var tbfoo = target.Child as ContentControl;
  89. bool foodetached = false;
  90. Assert.NotNull(tbfoo);
  91. Assert.Equal("foo", tbfoo.Content);
  92. tbfoo.DetachedFromLogicalTree += delegate { foodetached = true; };
  93. contentControl.Content = "bar";
  94. target.UpdateChild();
  95. var tbbar = target.Child as ContentControl;
  96. Assert.NotNull(tbbar);
  97. Assert.True(tbbar != tbfoo);
  98. Assert.False((tbfoo as IControl).IsAttachedToLogicalTree);
  99. Assert.True(foodetached);
  100. }
  101. [Fact]
  102. public void Should_Raise_DetachedFromLogicalTree_On_Detached_Standalone()
  103. {
  104. var target = new ContentPresenter
  105. {
  106. ContentTemplate =
  107. new FuncDataTemplate<string>(t => new ContentControl() { Content = t }, false)
  108. };
  109. var parentMock = new Mock<Control>();
  110. parentMock.As<IContentPresenterHost>();
  111. parentMock.As<IStyleRoot>();
  112. (target as ISetLogicalParent).SetParent(parentMock.Object);
  113. target.Content = "foo";
  114. target.UpdateChild();
  115. var foo = target.Child as ContentControl;
  116. bool foodetached = false;
  117. Assert.NotNull(foo);
  118. Assert.Equal("foo", foo.Content);
  119. foo.DetachedFromLogicalTree += delegate { foodetached = true; };
  120. (target as ISetLogicalParent).SetParent(null);
  121. Assert.False((foo as IControl).IsAttachedToLogicalTree);
  122. Assert.True(foodetached);
  123. }
  124. [Fact]
  125. public void Should_Remove_Old_Child_From_LogicalChildren_On_ContentChanged_Standalone()
  126. {
  127. var target = new ContentPresenter
  128. {
  129. ContentTemplate =
  130. new FuncDataTemplate<string>(t => new ContentControl() { Content = t }, false)
  131. };
  132. target.Content = "foo";
  133. target.UpdateChild();
  134. var foo = target.Child as ContentControl;
  135. Assert.NotNull(foo);
  136. var logicalChildren = target.GetLogicalChildren();
  137. Assert.Equal(1, logicalChildren.Count());
  138. target.Content = "bar";
  139. target.UpdateChild();
  140. Assert.Equal(null, foo.Parent);
  141. logicalChildren = target.GetLogicalChildren();
  142. Assert.Equal(1, logicalChildren.Count());
  143. Assert.NotEqual(foo, logicalChildren.First());
  144. }
  145. }
  146. }