AvaloniaObjectTests_GetValue.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Reactive.Subjects;
  3. using Avalonia.Data;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests
  6. {
  7. public class AvaloniaObjectTests_GetValue
  8. {
  9. [Fact]
  10. public void GetValue_Returns_Default_Value()
  11. {
  12. Class1 target = new Class1();
  13. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  14. }
  15. [Fact]
  16. public void GetValue_Returns_Overridden_Default_Value()
  17. {
  18. Class2 target = new Class2();
  19. Assert.Equal("foooverride", target.GetValue(Class1.FooProperty));
  20. }
  21. [Fact]
  22. public void GetValue_Returns_Set_Value()
  23. {
  24. var target = new Class1();
  25. var property = Class1.FooProperty;
  26. target.SetValue(property, "newvalue");
  27. Assert.Equal("newvalue", target.GetValue(property));
  28. }
  29. [Fact]
  30. public void GetValue_Returns_Bound_Value()
  31. {
  32. var target = new Class1();
  33. var property = Class1.FooProperty;
  34. target.Bind(property, new BehaviorSubject<string>("newvalue"));
  35. Assert.Equal("newvalue", target.GetValue(property));
  36. }
  37. [Fact]
  38. public void GetValue_Returns_Inherited_Value()
  39. {
  40. Class1 parent = new Class1();
  41. Class2 child = new Class2 { Parent = parent };
  42. parent.SetValue(Class1.BazProperty, "changed");
  43. Assert.Equal("changed", child.GetValue(Class1.BazProperty));
  44. }
  45. [Fact]
  46. public void GetValue_Doesnt_Throw_Exception_For_Unregistered_Property()
  47. {
  48. var target = new Class3();
  49. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  50. }
  51. [Fact]
  52. public void GetBaseValue_Ignores_Default_Value()
  53. {
  54. var target = new Class3();
  55. target.SetValue(Class1.FooProperty, "animated", BindingPriority.Animation);
  56. Assert.False(target.GetBaseValue(Class1.FooProperty).HasValue);
  57. }
  58. [Fact]
  59. public void GetBaseValue_Returns_Local_Value()
  60. {
  61. var target = new Class3();
  62. target.SetValue(Class1.FooProperty, "local");
  63. target.SetValue(Class1.FooProperty, "animated", BindingPriority.Animation);
  64. Assert.Equal("local", target.GetBaseValue(Class1.FooProperty).Value);
  65. }
  66. [Fact]
  67. public void GetBaseValue_Returns_Style_Value()
  68. {
  69. var target = new Class3();
  70. target.SetValue(Class1.FooProperty, "style", BindingPriority.Style);
  71. target.SetValue(Class1.FooProperty, "animated", BindingPriority.Animation);
  72. Assert.Equal("style", target.GetBaseValue(Class1.FooProperty).Value);
  73. }
  74. [Fact]
  75. public void GetBaseValue_Returns_Style_Value_Set_Via_Untyped_Setters()
  76. {
  77. var target = new Class3();
  78. target.SetValue(Class1.FooProperty, (object)"style", BindingPriority.Style);
  79. target.SetValue(Class1.FooProperty, (object)"animated", BindingPriority.Animation);
  80. Assert.Equal("style", target.GetBaseValue(Class1.FooProperty).Value);
  81. }
  82. private class Class1 : AvaloniaObject
  83. {
  84. public static readonly StyledProperty<string> FooProperty =
  85. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  86. public static readonly StyledProperty<string> BazProperty =
  87. AvaloniaProperty.Register<Class1, string>("Baz", "bazdefault", true);
  88. }
  89. private class Class2 : Class1
  90. {
  91. static Class2()
  92. {
  93. FooProperty.OverrideDefaultValue(typeof(Class2), "foooverride");
  94. }
  95. public Class1 Parent
  96. {
  97. get { return (Class1)InheritanceParent; }
  98. set { InheritanceParent = value; }
  99. }
  100. }
  101. private class Class3 : AvaloniaObject
  102. {
  103. }
  104. }
  105. }