ValueStoreTests_Inheritance.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Xunit;
  2. #nullable enable
  3. namespace Avalonia.Base.UnitTests.PropertyStore
  4. {
  5. public class ValueStoreTests_Inheritance
  6. {
  7. [Fact]
  8. public void InheritanceAncestor_Is_Initially_Null()
  9. {
  10. var parent = new Class1();
  11. var child = new Class1 { Parent = parent };
  12. var grandchild = new Class1 { Parent = child };
  13. Assert.Null(parent.GetValueStore().InheritanceAncestor);
  14. Assert.Null(child.GetValueStore().InheritanceAncestor);
  15. Assert.Null(grandchild.GetValueStore().InheritanceAncestor);
  16. }
  17. [Fact]
  18. public void Setting_Value_In_Parent_Updates_InheritanceAncestor()
  19. {
  20. var parent = new Class1();
  21. var child = new Class1 { Parent = parent };
  22. var grandchild = new Class1 { Parent = child };
  23. parent.Foo = "changed";
  24. var parentStore = parent.GetValueStore();
  25. Assert.Null(parentStore.InheritanceAncestor);
  26. Assert.Same(parentStore, child.GetValueStore().InheritanceAncestor);
  27. Assert.Same(parentStore, grandchild.GetValueStore().InheritanceAncestor);
  28. }
  29. [Fact]
  30. public void Setting_Value_In_Parent_Doesnt_Update_Grandchild_InheritanceAncestor_If_Child_Has_Value_Set()
  31. {
  32. var parent = new Class1();
  33. var child = new Class1 { Parent = parent };
  34. var grandchild = new Class1 { Parent = child };
  35. child.Foo = "foochanged";
  36. parent.Foo = "changed";
  37. var parentStore = parent.GetValueStore();
  38. Assert.Null(parentStore.InheritanceAncestor);
  39. Assert.Same(parentStore, child.GetValueStore().InheritanceAncestor);
  40. Assert.Same(child.GetValueStore(), grandchild.GetValueStore().InheritanceAncestor);
  41. }
  42. [Fact]
  43. public void Clearing_Value_In_Parent_Updates_InheritanceAncestor()
  44. {
  45. var parent = new Class1();
  46. var child = new Class1 { Parent = parent };
  47. var grandchild = new Class1 { Parent = child };
  48. parent.Foo = "changed";
  49. parent.ClearValue(Class1.FooProperty);
  50. Assert.Null(parent.GetValueStore().InheritanceAncestor);
  51. Assert.Null(child.GetValueStore().InheritanceAncestor);
  52. Assert.Null(grandchild.GetValueStore().InheritanceAncestor);
  53. }
  54. [Fact]
  55. public void Clear_Value_In_Parent_Doesnt_Update_Grandchild_InheritanceAncestor_If_Child_Has_Value_Set()
  56. {
  57. var parent = new Class1();
  58. var child = new Class1 { Parent = parent };
  59. var grandchild = new Class1 { Parent = child };
  60. child.Foo = "foochanged";
  61. parent.Foo = "changed";
  62. parent.ClearValue(Class1.FooProperty);
  63. Assert.Null(parent.GetValueStore().InheritanceAncestor);
  64. Assert.Null(child.GetValueStore().InheritanceAncestor);
  65. Assert.Same(child.GetValueStore(), grandchild.GetValueStore().InheritanceAncestor);
  66. }
  67. [Fact]
  68. public void Clearing_Value_In_Child_Updates_InheritanceAncestor()
  69. {
  70. var parent = new Class1();
  71. var child = new Class1 { Parent = parent };
  72. var grandchild = new Class1 { Parent = child };
  73. parent.Foo = "changed";
  74. child.Foo = "foochanged";
  75. child.ClearValue(Class1.FooProperty);
  76. var parentStore = parent.GetValueStore();
  77. Assert.Null(parentStore.InheritanceAncestor);
  78. Assert.Same(parentStore, child.GetValueStore().InheritanceAncestor);
  79. Assert.Same(parentStore, grandchild.GetValueStore().InheritanceAncestor);
  80. }
  81. [Fact]
  82. public void Child_Notifies_About_Setting_Back_To_Default_Value()
  83. {
  84. var parent = new Class1();
  85. var child = new Class1();
  86. parent.Foo = "changed";
  87. child.Parent = parent;
  88. bool raised = false;
  89. child.PropertyChanged += (_, args) =>
  90. {
  91. raised = args.Property == Class1.FooProperty && args.GetNewValue<string>() == "foodefault";
  92. };
  93. Assert.Equal("changed", child.Foo); // inherited from parent.
  94. child.Foo = "foodefault"; // reset back to default.
  95. Assert.True(raised); // expect event to be raised, as actual value was changed.
  96. }
  97. [Fact]
  98. public void Adding_Child_Sets_InheritanceAncestor()
  99. {
  100. var parent = new Class1();
  101. var child = new Class1();
  102. var grandchild = new Class1 { Parent = child };
  103. parent.Foo = "changed";
  104. child.Parent = parent;
  105. var parentStore = parent.GetValueStore();
  106. Assert.Null(parentStore.InheritanceAncestor);
  107. Assert.Same(parentStore, child.GetValueStore().InheritanceAncestor);
  108. Assert.Same(parentStore, grandchild.GetValueStore().InheritanceAncestor);
  109. }
  110. private class Class1 : AvaloniaObject
  111. {
  112. public static readonly StyledProperty<string> FooProperty =
  113. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault", inherits: true);
  114. public string Foo
  115. {
  116. get => GetValue(FooProperty);
  117. set => SetValue(FooProperty, value);
  118. }
  119. public Class1? Parent
  120. {
  121. get { return (Class1?)InheritanceParent; }
  122. set { InheritanceParent = value; }
  123. }
  124. }
  125. }
  126. }