AvaloniaObjectTests_GetValue.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Reactive.Subjects;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests
  5. {
  6. public class AvaloniaObjectTests_GetValue
  7. {
  8. [Fact]
  9. public void GetValue_Returns_Default_Value()
  10. {
  11. Class1 target = new Class1();
  12. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  13. }
  14. [Fact]
  15. public void GetValue_Returns_Overridden_Default_Value()
  16. {
  17. Class2 target = new Class2();
  18. Assert.Equal("foooverride", target.GetValue(Class1.FooProperty));
  19. }
  20. [Fact]
  21. public void GetValue_Returns_Set_Value()
  22. {
  23. var target = new Class1();
  24. var property = Class1.FooProperty;
  25. target.SetValue(property, "newvalue");
  26. Assert.Equal("newvalue", target.GetValue(property));
  27. }
  28. [Fact]
  29. public void GetValue_Returns_Bound_Value()
  30. {
  31. var target = new Class1();
  32. var property = Class1.FooProperty;
  33. target.Bind(property, new BehaviorSubject<string>("newvalue"));
  34. Assert.Equal("newvalue", target.GetValue(property));
  35. }
  36. [Fact]
  37. public void GetValue_Returns_Inherited_Value()
  38. {
  39. Class1 parent = new Class1();
  40. Class2 child = new Class2 { Parent = parent };
  41. parent.SetValue(Class1.BazProperty, "changed");
  42. Assert.Equal("changed", child.GetValue(Class1.BazProperty));
  43. }
  44. [Fact]
  45. public void GetValue_Doesnt_Throw_Exception_For_Unregistered_Property()
  46. {
  47. var target = new Class3();
  48. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  49. }
  50. private class Class1 : AvaloniaObject
  51. {
  52. public static readonly StyledProperty<string> FooProperty =
  53. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  54. public static readonly StyledProperty<string> BazProperty =
  55. AvaloniaProperty.Register<Class1, string>("Baz", "bazdefault", true);
  56. }
  57. private class Class2 : Class1
  58. {
  59. static Class2()
  60. {
  61. FooProperty.OverrideDefaultValue(typeof(Class2), "foooverride");
  62. }
  63. public Class1 Parent
  64. {
  65. get { return (Class1)InheritanceParent; }
  66. set { InheritanceParent = value; }
  67. }
  68. }
  69. private class Class3 : AvaloniaObject
  70. {
  71. }
  72. }
  73. }