ContentPresenterTests_Unrooted.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Templates;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Controls.UnitTests.Presenters
  8. {
  9. /// <summary>
  10. /// Tests for ContentControls that are not attached to a logical tree.
  11. /// </summary>
  12. public class ContentPresenterTests_Unrooted
  13. {
  14. [Fact]
  15. public void Setting_Content_To_Control_Should_Not_Set_Child_Unless_UpdateChild_Called()
  16. {
  17. var target = new ContentPresenter();
  18. var child = new Border();
  19. target.Content = child;
  20. Assert.Null(target.Child);
  21. target.ApplyTemplate();
  22. Assert.Null(target.Child);
  23. target.UpdateChild();
  24. Assert.Equal(child, target.Child);
  25. }
  26. [Fact]
  27. public void Setting_Content_To_String_Should_Not_Create_TextBlock_Unless_UpdateChild_Called()
  28. {
  29. var target = new ContentPresenter();
  30. target.Content = "Foo";
  31. Assert.Null(target.Child);
  32. target.ApplyTemplate();
  33. Assert.Null(target.Child);
  34. target.UpdateChild();
  35. Assert.IsType<TextBlock>(target.Child);
  36. Assert.Equal("Foo", ((TextBlock)target.Child).Text);
  37. }
  38. [Fact]
  39. public void Clearing_Control_Content_Should_Remove_Child_Immediately()
  40. {
  41. var target = new ContentPresenter();
  42. var child = new Border();
  43. target.Content = child;
  44. target.UpdateChild();
  45. Assert.Equal(child, target.Child);
  46. target.Content = null;
  47. Assert.Null(target.Child);
  48. }
  49. [Fact]
  50. public void Clearing_String_Content_Should_Remove_Child_Immediately()
  51. {
  52. var target = new ContentPresenter();
  53. target.Content = "Foo";
  54. target.UpdateChild();
  55. Assert.IsType<TextBlock>(target.Child);
  56. target.Content = null;
  57. Assert.Null(target.Child);
  58. }
  59. [Fact]
  60. public void Adding_To_Logical_Tree_Should_Reevaluate_DataTemplates()
  61. {
  62. var root = new TestRoot();
  63. var target = new ContentPresenter();
  64. target.Content = "Foo";
  65. Assert.Null(target.Child);
  66. root.Child = target;
  67. target.ApplyTemplate();
  68. Assert.IsType<TextBlock>(target.Child);
  69. root.Child = null;
  70. root = new TestRoot
  71. {
  72. DataTemplates = new DataTemplates
  73. {
  74. new FuncDataTemplate<string>(x => new Decorator()),
  75. },
  76. };
  77. root.Child = target;
  78. target.ApplyTemplate();
  79. Assert.IsType<Decorator>(target.Child);
  80. }
  81. }
  82. }