AvaloniaObjectTests_Inheritance.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Xunit;
  4. namespace Avalonia.Base.UnitTests
  5. {
  6. public class AvaloniaObjectTests_Inheritance
  7. {
  8. [Fact]
  9. public void GetValue_Returns_Inherited_Value()
  10. {
  11. Class1 parent = new Class1();
  12. Class2 child = new Class2 { Parent = parent };
  13. parent.SetValue(Class1.BazProperty, "changed");
  14. Assert.Equal("changed", child.GetValue(Class1.BazProperty));
  15. }
  16. [Fact]
  17. public void Setting_InheritanceParent_Raises_PropertyChanged_When_Value_Changed_In_Parent()
  18. {
  19. bool raised = false;
  20. Class1 parent = new Class1();
  21. parent.SetValue(Class1.BazProperty, "changed");
  22. Class2 child = new Class2();
  23. child.PropertyChanged += (s, e) =>
  24. raised = s == child &&
  25. e.Property == Class1.BazProperty &&
  26. (string)e.OldValue == "bazdefault" &&
  27. (string)e.NewValue == "changed";
  28. child.Parent = parent;
  29. Assert.True(raised);
  30. }
  31. [Fact]
  32. public void Setting_InheritanceParent_Doesnt_Raise_PropertyChanged_When_Local_Value_Set()
  33. {
  34. bool raised = false;
  35. Class1 parent = new Class1();
  36. parent.SetValue(Class1.BazProperty, "changed");
  37. Class2 child = new Class2();
  38. child.SetValue(Class1.BazProperty, "localvalue");
  39. child.PropertyChanged += (s, e) => raised = true;
  40. child.Parent = parent;
  41. Assert.False(raised);
  42. }
  43. [Fact]
  44. public void Setting_Value_In_InheritanceParent_Raises_PropertyChanged()
  45. {
  46. bool raised = false;
  47. Class1 parent = new Class1();
  48. Class2 child = new Class2();
  49. child.PropertyChanged += (s, e) =>
  50. raised = s == child &&
  51. e.Property == Class1.BazProperty &&
  52. (string)e.OldValue == "bazdefault" &&
  53. (string)e.NewValue == "changed";
  54. child.Parent = parent;
  55. parent.SetValue(Class1.BazProperty, "changed");
  56. Assert.True(raised);
  57. }
  58. private class Class1 : AvaloniaObject
  59. {
  60. public static readonly StyledProperty<string> FooProperty =
  61. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  62. public static readonly StyledProperty<string> BazProperty =
  63. AvaloniaProperty.Register<Class1, string>("Baz", "bazdefault", true);
  64. }
  65. private class Class2 : Class1
  66. {
  67. static Class2()
  68. {
  69. FooProperty.OverrideDefaultValue(typeof(Class2), "foooverride");
  70. }
  71. public Class1 Parent
  72. {
  73. get { return (Class1)InheritanceParent; }
  74. set { InheritanceParent = value; }
  75. }
  76. }
  77. }
  78. }