ExpressionObserverTests_AttachedProperty.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 System.Collections.Generic;
  5. using System.Reactive.Linq;
  6. using System.Threading.Tasks;
  7. using Avalonia.Diagnostics;
  8. using Avalonia.Data.Core;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.Data.Core
  11. {
  12. public class ExpressionObserverTests_AttachedProperty
  13. {
  14. [Fact]
  15. public async Task Should_Get_Attached_Property_Value()
  16. {
  17. var data = new Class1();
  18. var target = ExpressionObserver.Create(data, o => o[Owner.FooProperty]);
  19. var result = await target.Take(1);
  20. Assert.Equal("foo", result);
  21. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  22. }
  23. [Fact]
  24. public async Task Should_Get_Chained_Attached_Property_Value()
  25. {
  26. var data = new Class1
  27. {
  28. Next = new Class1
  29. {
  30. [Owner.FooProperty] = "bar",
  31. }
  32. };
  33. var target = ExpressionObserver.Create(data, o => o.Next[Owner.FooProperty]);
  34. var result = await target.Take(1);
  35. Assert.Equal("bar", result);
  36. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  37. }
  38. [Fact]
  39. public void Should_Track_Simple_Attached_Value()
  40. {
  41. var data = new Class1();
  42. var target = ExpressionObserver.Create(data, o => o[Owner.FooProperty]);
  43. var result = new List<object>();
  44. var sub = target.Subscribe(x => result.Add(x));
  45. data.SetValue(Owner.FooProperty, "bar");
  46. Assert.Equal(new[] { "foo", "bar" }, result);
  47. sub.Dispose();
  48. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  49. }
  50. [Fact]
  51. public void Should_Track_Chained_Attached_Value()
  52. {
  53. var data = new Class1
  54. {
  55. Next = new Class1
  56. {
  57. [Owner.FooProperty] = "foo",
  58. }
  59. };
  60. var target = ExpressionObserver.Create(data, o => o.Next[Owner.FooProperty]);
  61. var result = new List<object>();
  62. var sub = target.Subscribe(x => result.Add(x));
  63. data.Next.SetValue(Owner.FooProperty, "bar");
  64. Assert.Equal(new[] { "foo", "bar" }, result);
  65. sub.Dispose();
  66. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  67. }
  68. [Fact]
  69. public void Should_Not_Keep_Source_Alive()
  70. {
  71. Func<Tuple<ExpressionObserver, WeakReference>> run = () =>
  72. {
  73. var source = new Class1();
  74. var target = ExpressionObserver.Create(source, o => o.Next[Owner.FooProperty]);
  75. return Tuple.Create(target, new WeakReference(source));
  76. };
  77. var result = run();
  78. result.Item1.Subscribe(x => { });
  79. GC.Collect();
  80. Assert.Null(result.Item2.Target);
  81. }
  82. private static class Owner
  83. {
  84. public static readonly AttachedProperty<string> FooProperty =
  85. AvaloniaProperty.RegisterAttached<Class1, string>(
  86. "Foo",
  87. typeof(Owner),
  88. defaultValue: "foo");
  89. }
  90. private class Class1 : AvaloniaObject
  91. {
  92. public static readonly StyledProperty<Class1> NextProperty =
  93. AvaloniaProperty.Register<Class1, Class1>(nameof(Next));
  94. public Class1 Next
  95. {
  96. get { return GetValue(NextProperty); }
  97. set { SetValue(NextProperty, value); }
  98. }
  99. }
  100. }
  101. }