AvaloniaObjectTests_Inheritance.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Generic;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests
  6. {
  7. public class AvaloniaObjectTests_Inheritance
  8. {
  9. [Fact]
  10. public void GetValue_Returns_Inherited_Value()
  11. {
  12. Class1 parent = new Class1();
  13. Class2 child = new Class2 { Parent = parent };
  14. parent.SetValue(Class1.BazProperty, "changed");
  15. Assert.Equal("changed", child.GetValue(Class1.BazProperty));
  16. }
  17. [Fact]
  18. public void Setting_InheritanceParent_Raises_PropertyChanged_When_Value_Changed_In_Parent()
  19. {
  20. bool raised = false;
  21. Class1 parent = new Class1();
  22. parent.SetValue(Class1.BazProperty, "changed");
  23. Class2 child = new Class2();
  24. child.PropertyChanged += (s, e) =>
  25. raised = s == child &&
  26. e.Property == Class1.BazProperty &&
  27. (string)e.OldValue == "bazdefault" &&
  28. (string)e.NewValue == "changed";
  29. child.Parent = parent;
  30. Assert.True(raised);
  31. }
  32. [Fact]
  33. public void Setting_InheritanceParent_Raises_PropertyChanged_For_Attached_Property_When_Value_Changed_In_Parent()
  34. {
  35. bool raised = false;
  36. Class1 parent = new Class1();
  37. parent.SetValue(AttachedOwner.AttachedProperty, "changed");
  38. Class2 child = new Class2();
  39. child.PropertyChanged += (s, e) =>
  40. raised = s == child &&
  41. e.Property == AttachedOwner.AttachedProperty &&
  42. (string)e.OldValue == null &&
  43. (string)e.NewValue == "changed";
  44. child.Parent = parent;
  45. Assert.True(raised);
  46. }
  47. [Fact]
  48. public void Setting_InheritanceParent_Doesnt_Raise_PropertyChanged_When_Local_Value_Set()
  49. {
  50. bool raised = false;
  51. Class1 parent = new Class1();
  52. parent.SetValue(Class1.BazProperty, "changed");
  53. Class2 child = new Class2();
  54. child.SetValue(Class1.BazProperty, "localvalue");
  55. child.PropertyChanged += (s, e) => raised = true;
  56. child.Parent = parent;
  57. Assert.False(raised);
  58. }
  59. [Fact]
  60. public void Setting_Value_In_InheritanceParent_Raises_PropertyChanged()
  61. {
  62. bool raised = false;
  63. Class1 parent = new Class1();
  64. Class2 child = new Class2();
  65. child.PropertyChanged += (s, e) =>
  66. raised = s == child &&
  67. e.Property == Class1.BazProperty &&
  68. (string)e.OldValue == "bazdefault" &&
  69. (string)e.NewValue == "changed";
  70. child.Parent = parent;
  71. parent.SetValue(Class1.BazProperty, "changed");
  72. Assert.True(raised);
  73. }
  74. [Fact]
  75. public void Setting_Value_Of_Attached_Property_In_InheritanceParent_Raises_PropertyChanged()
  76. {
  77. bool raised = false;
  78. Class1 parent = new Class1();
  79. Class2 child = new Class2();
  80. child.PropertyChanged += (s, e) =>
  81. raised = s == child &&
  82. e.Property == AttachedOwner.AttachedProperty &&
  83. (string)e.OldValue == null &&
  84. (string)e.NewValue == "changed";
  85. child.Parent = parent;
  86. parent.SetValue(AttachedOwner.AttachedProperty, "changed");
  87. Assert.True(raised);
  88. }
  89. [Fact]
  90. public void PropertyChanged_Is_Raised_In_Parent_Before_Child()
  91. {
  92. var parent = new Class1();
  93. var child = new Class2 { Parent = parent };
  94. var result = new List<object>();
  95. parent.PropertyChanged += (s, e) => result.Add(parent);
  96. child.PropertyChanged += (s, e) => result.Add(child);
  97. parent.SetValue(Class1.BazProperty, "changed");
  98. Assert.Equal(new[] { parent, child }, result);
  99. }
  100. private class Class1 : AvaloniaObject
  101. {
  102. public static readonly StyledProperty<string> FooProperty =
  103. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  104. public static readonly StyledProperty<string> BazProperty =
  105. AvaloniaProperty.Register<Class1, string>("Baz", "bazdefault", true);
  106. }
  107. private class Class2 : Class1
  108. {
  109. static Class2()
  110. {
  111. FooProperty.OverrideDefaultValue(typeof(Class2), "foooverride");
  112. }
  113. public Class1 Parent
  114. {
  115. get { return (Class1)InheritanceParent; }
  116. set { InheritanceParent = value; }
  117. }
  118. }
  119. private class AttachedOwner : AvaloniaObject
  120. {
  121. public static readonly AttachedProperty<string> AttachedProperty =
  122. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached", inherits: true);
  123. }
  124. }
  125. }