AvaloniaObjectTests_Attached.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_Attached
  8. {
  9. [Fact]
  10. public void AddOwnered_Property_Retains_Default_Value()
  11. {
  12. var target = new Class2();
  13. Assert.Equal("foodefault", target.GetValue(Class2.FooProperty));
  14. }
  15. [Fact]
  16. public void AddOwnered_Property_Retains_Validation()
  17. {
  18. var target = new Class2();
  19. Assert.Throws<IndexOutOfRangeException>(() => target.SetValue(Class2.FooProperty, "throw"));
  20. }
  21. [Fact]
  22. public void AvaloniaProperty_Initialized_Is_Called_For_Attached_Property()
  23. {
  24. bool raised = false;
  25. using (Class1.FooProperty.Initialized.Subscribe(x => raised = true))
  26. {
  27. new Class3();
  28. }
  29. Assert.True(raised);
  30. }
  31. private class Base : AvaloniaObject
  32. {
  33. }
  34. private class Class1 : Base
  35. {
  36. public static readonly AttachedProperty<string> FooProperty =
  37. AvaloniaProperty.RegisterAttached<Class1, Base, string>(
  38. "Foo",
  39. "foodefault",
  40. validate: ValidateFoo);
  41. private static string ValidateFoo(AvaloniaObject arg1, string arg2)
  42. {
  43. if (arg2 == "throw")
  44. {
  45. throw new IndexOutOfRangeException();
  46. }
  47. return arg2;
  48. }
  49. }
  50. private class Class2 : Base
  51. {
  52. public static readonly AttachedProperty<string> FooProperty =
  53. Class1.FooProperty.AddOwner<Class2>();
  54. }
  55. private class Class3 : Base
  56. {
  57. }
  58. }
  59. }