AvaloniaObjectTests_GetValue.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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;
  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. Class1 target = new Class1();
  25. target.SetValue(Class1.FooProperty, "newvalue");
  26. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  27. }
  28. [Fact]
  29. public void GetValue_Returns_Inherited_Value()
  30. {
  31. Class1 parent = new Class1();
  32. Class2 child = new Class2 { Parent = parent };
  33. parent.SetValue(Class1.BazProperty, "changed");
  34. Assert.Equal("changed", child.GetValue(Class1.BazProperty));
  35. }
  36. [Fact]
  37. public void GetValue_Doesnt_Throw_Exception_For_Unregistered_Property()
  38. {
  39. var target = new Class3();
  40. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  41. }
  42. private class Class1 : AvaloniaObject
  43. {
  44. public static readonly StyledProperty<string> FooProperty =
  45. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  46. public static readonly StyledProperty<string> BazProperty =
  47. AvaloniaProperty.Register<Class1, string>("Baz", "bazdefault", true);
  48. }
  49. private class Class2 : Class1
  50. {
  51. static Class2()
  52. {
  53. FooProperty.OverrideDefaultValue(typeof(Class2), "foooverride");
  54. }
  55. public Class1 Parent
  56. {
  57. get { return (Class1)InheritanceParent; }
  58. set { InheritanceParent = value; }
  59. }
  60. }
  61. private class Class3 : AvaloniaObject
  62. {
  63. }
  64. }
  65. }