ContentPresenterTests_Unrooted.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Avalonia.Controls.Presenters;
  2. using Avalonia.Controls.Templates;
  3. using Avalonia.Styling;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests.Presenters
  7. {
  8. /// <summary>
  9. /// Tests for ContentControls that are not attached to a logical tree.
  10. /// </summary>
  11. public class ContentPresenterTests_Unrooted : ScopedTestBase
  12. {
  13. [Fact]
  14. public void Setting_Content_To_Control_Should_Not_Set_Child_Unless_UpdateChild_Called()
  15. {
  16. var target = new ContentPresenter();
  17. var child = new Border();
  18. target.Content = child;
  19. Assert.Null(target.Child);
  20. target.ApplyTemplate();
  21. Assert.Null(target.Child);
  22. target.UpdateChild();
  23. Assert.Equal(child, target.Child);
  24. }
  25. [Fact]
  26. public void Setting_Content_To_String_Should_Not_Create_TextBlock_Unless_UpdateChild_Called()
  27. {
  28. var target = new ContentPresenter();
  29. target.Content = "Foo";
  30. Assert.Null(target.Child);
  31. target.ApplyTemplate();
  32. Assert.Null(target.Child);
  33. target.UpdateChild();
  34. Assert.IsType<TextBlock>(target.Child);
  35. Assert.Equal("Foo", ((TextBlock)target.Child).Text);
  36. }
  37. [Fact]
  38. public void Clearing_Control_Content_Should_Remove_Child_Immediately()
  39. {
  40. var target = new ContentPresenter();
  41. var child = new Border();
  42. target.Content = child;
  43. target.UpdateChild();
  44. Assert.Equal(child, target.Child);
  45. target.Content = null;
  46. Assert.Null(target.Child);
  47. }
  48. [Fact]
  49. public void Clearing_String_Content_Should_Remove_Child_Immediately()
  50. {
  51. var target = new ContentPresenter();
  52. target.Content = "Foo";
  53. target.UpdateChild();
  54. Assert.IsType<TextBlock>(target.Child);
  55. target.Content = null;
  56. Assert.Null(target.Child);
  57. }
  58. [Fact]
  59. public void Adding_To_Logical_Tree_Should_Reevaluate_DataTemplates()
  60. {
  61. var root = new TestRoot();
  62. var target = new ContentPresenter();
  63. target.Content = "Foo";
  64. Assert.Null(target.Child);
  65. root.Child = target;
  66. target.ApplyTemplate();
  67. Assert.IsType<TextBlock>(target.Child);
  68. root.Child = null;
  69. root = new TestRoot
  70. {
  71. DataTemplates =
  72. {
  73. new FuncDataTemplate<string>((x, _) => new Decorator()),
  74. },
  75. };
  76. root.Child = target;
  77. target.ApplyTemplate();
  78. Assert.IsType<Decorator>(target.Child);
  79. }
  80. [Fact]
  81. public void Should_Reset_InheritanceParent_When_Child_Removed()
  82. {
  83. var logicalParent = new Canvas();
  84. var child = new TextBlock();
  85. var target = new ContentPresenter();
  86. ((ISetLogicalParent)child).SetParent(logicalParent);
  87. target.Content = child;
  88. target.UpdateChild();
  89. target.Content = null;
  90. target.UpdateChild();
  91. // InheritanceParent is exposed via StylingParent.
  92. Assert.Same(logicalParent, ((IStyleHost)child).StylingParent);
  93. }
  94. }
  95. }