ExpressionObserverTests_AttachedProperty.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. public ExpressionObserverTests_AttachedProperty()
  15. {
  16. var foo = Owner.FooProperty;
  17. }
  18. [Fact]
  19. public async Task Should_Get_Attached_Property_Value()
  20. {
  21. var data = new Class1();
  22. var target = new ExpressionObserver(data, "(Owner.Foo)");
  23. var result = await target.Take(1);
  24. Assert.Equal("foo", result);
  25. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  26. }
  27. [Fact]
  28. public async Task Should_Get_Chained_Attached_Property_Value()
  29. {
  30. var data = new Class1
  31. {
  32. Next = new Class1
  33. {
  34. [Owner.FooProperty] = "bar",
  35. }
  36. };
  37. var target = new ExpressionObserver(data, "Next.(Owner.Foo)");
  38. var result = await target.Take(1);
  39. Assert.Equal("bar", result);
  40. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  41. }
  42. [Fact]
  43. public void Should_Track_Simple_Attached_Value()
  44. {
  45. var data = new Class1();
  46. var target = new ExpressionObserver(data, "(Owner.Foo)");
  47. var result = new List<object>();
  48. var sub = target.Subscribe(x => result.Add(x));
  49. data.SetValue(Owner.FooProperty, "bar");
  50. Assert.Equal(new[] { "foo", "bar" }, result);
  51. sub.Dispose();
  52. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  53. }
  54. [Fact]
  55. public void Should_Track_Chained_Attached_Value()
  56. {
  57. var data = new Class1
  58. {
  59. Next = new Class1
  60. {
  61. [Owner.FooProperty] = "foo",
  62. }
  63. };
  64. var target = new ExpressionObserver(data, "Next.(Owner.Foo)");
  65. var result = new List<object>();
  66. var sub = target.Subscribe(x => result.Add(x));
  67. data.Next.SetValue(Owner.FooProperty, "bar");
  68. Assert.Equal(new[] { "foo", "bar" }, result);
  69. sub.Dispose();
  70. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  71. }
  72. [Fact]
  73. public void Should_Not_Keep_Source_Alive()
  74. {
  75. Func<Tuple<ExpressionObserver, WeakReference>> run = () =>
  76. {
  77. var source = new Class1();
  78. var target = new ExpressionObserver(source, "(Owner.Foo)");
  79. return Tuple.Create(target, new WeakReference(source));
  80. };
  81. var result = run();
  82. result.Item1.Subscribe(x => { });
  83. GC.Collect();
  84. Assert.Null(result.Item2.Target);
  85. }
  86. [Fact]
  87. public void Should_Fail_With_Attached_Property_With_Only_1_Part()
  88. {
  89. var data = new Class1();
  90. Assert.Throws<ExpressionParseException>(() => new ExpressionObserver(data, "(Owner)"));
  91. }
  92. [Fact]
  93. public void Should_Fail_With_Attached_Property_With_More_Than_2_Parts()
  94. {
  95. var data = new Class1();
  96. Assert.Throws<ExpressionParseException>(() => new ExpressionObserver(data, "(Owner.Foo.Bar)"));
  97. }
  98. private static class Owner
  99. {
  100. public static readonly AttachedProperty<string> FooProperty =
  101. AvaloniaProperty.RegisterAttached<AvaloniaObject, string>(
  102. "Foo",
  103. typeof(Owner),
  104. defaultValue: "foo");
  105. }
  106. private class Class1 : AvaloniaObject
  107. {
  108. public static readonly StyledProperty<Class1> NextProperty =
  109. AvaloniaProperty.Register<Class1, Class1>(nameof(Next));
  110. public Class1 Next
  111. {
  112. get { return GetValue(NextProperty); }
  113. set { SetValue(NextProperty, value); }
  114. }
  115. }
  116. }
  117. }