DecoratorTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 System.Collections.Specialized;
  4. using System.Linq;
  5. using Avalonia.LogicalTree;
  6. using Xunit;
  7. namespace Avalonia.Controls.UnitTests
  8. {
  9. public class DecoratorTests
  10. {
  11. [Fact]
  12. public void Setting_Content_Should_Set_Child_Controls_Parent()
  13. {
  14. var decorator = new Decorator();
  15. var child = new Control();
  16. decorator.Child = child;
  17. Assert.Equal(child.Parent, decorator);
  18. Assert.Equal(((ILogical)child).LogicalParent, decorator);
  19. }
  20. [Fact]
  21. public void Clearing_Content_Should_Clear_Child_Controls_Parent()
  22. {
  23. var decorator = new Decorator();
  24. var child = new Control();
  25. decorator.Child = child;
  26. decorator.Child = null;
  27. Assert.Null(child.Parent);
  28. Assert.Null(((ILogical)child).LogicalParent);
  29. }
  30. [Fact]
  31. public void Content_Control_Should_Appear_In_LogicalChildren()
  32. {
  33. var decorator = new Decorator();
  34. var child = new Control();
  35. decorator.Child = child;
  36. Assert.Equal(new[] { child }, ((ILogical)decorator).LogicalChildren.ToList());
  37. }
  38. [Fact]
  39. public void Clearing_Content_Should_Remove_From_LogicalChildren()
  40. {
  41. var decorator = new Decorator();
  42. var child = new Control();
  43. decorator.Child = child;
  44. decorator.Child = null;
  45. Assert.Equal(new ILogical[0], ((ILogical)decorator).LogicalChildren.ToList());
  46. }
  47. [Fact]
  48. public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged()
  49. {
  50. var decorator = new Decorator();
  51. var child = new Control();
  52. var called = false;
  53. ((ILogical)decorator).LogicalChildren.CollectionChanged += (s, e) =>
  54. called = e.Action == NotifyCollectionChangedAction.Add;
  55. decorator.Child = child;
  56. Assert.True(called);
  57. }
  58. [Fact]
  59. public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  60. {
  61. var decorator = new Decorator();
  62. var child = new Control();
  63. var called = false;
  64. decorator.Child = child;
  65. ((ILogical)decorator).LogicalChildren.CollectionChanged += (s, e) =>
  66. called = e.Action == NotifyCollectionChangedAction.Remove;
  67. decorator.Child = null;
  68. Assert.True(called);
  69. }
  70. [Fact]
  71. public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  72. {
  73. var decorator = new Decorator();
  74. var child1 = new Control();
  75. var child2 = new Control();
  76. var called = false;
  77. decorator.Child = child1;
  78. ((ILogical)decorator).LogicalChildren.CollectionChanged += (s, e) => called = true;
  79. decorator.Child = child2;
  80. Assert.True(called);
  81. }
  82. [Fact]
  83. public void Measure_Should_Return_Padding_When_No_Child_Present()
  84. {
  85. var target = new Decorator
  86. {
  87. Padding = new Thickness(8),
  88. };
  89. target.Measure(new Size(100, 100));
  90. Assert.Equal(new Size(16, 16), target.DesiredSize);
  91. }
  92. }
  93. }