DecoratorTests.cs 3.6 KB

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