BindingExpressionTests.AttachedProperty.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using Avalonia.Data.Core;
  3. using Avalonia.Diagnostics;
  4. using Xunit;
  5. #nullable enable
  6. namespace Avalonia.Base.UnitTests.Data.Core;
  7. public partial class BindingExpressionTests
  8. {
  9. [Fact]
  10. public void Should_Get_Attached_Property_Value()
  11. {
  12. var data = new SourceControl { [AttachedProperties.AttachedStringProperty] = "foo" };
  13. var target = CreateTargetWithSource(
  14. data,
  15. o => o[AttachedProperties.AttachedStringProperty],
  16. targetProperty: TargetClass.StringProperty);
  17. Assert.Equal("foo", target.String);
  18. }
  19. [Fact]
  20. public void Should_Get_Chained_Attached_Property_Value()
  21. {
  22. var data = new SourceControl
  23. {
  24. Next = new() { [AttachedProperties.AttachedStringProperty] = "foo" }
  25. };
  26. var target = CreateTargetWithSource(
  27. data,
  28. o => o.Next![AttachedProperties.AttachedStringProperty],
  29. targetProperty: TargetClass.StringProperty);
  30. Assert.Equal("foo", target.String);
  31. }
  32. [Fact]
  33. public void Should_Track_Simple_Attached_Value()
  34. {
  35. var data = new SourceControl { [AttachedProperties.AttachedStringProperty] = "foo" };
  36. var target = CreateTargetWithSource(
  37. data,
  38. o => o[AttachedProperties.AttachedStringProperty],
  39. targetProperty: TargetClass.StringProperty);
  40. Assert.Equal("foo", target.String);
  41. data.SetValue(AttachedProperties.AttachedStringProperty, "bar");
  42. Assert.Equal("bar", target.String);
  43. }
  44. [Fact]
  45. public void Should_Track_Chained_Attached_Value()
  46. {
  47. var data = new SourceControl
  48. {
  49. Next = new() { [AttachedProperties.AttachedStringProperty] = "foo" }
  50. };
  51. var target = CreateTargetWithSource(
  52. data,
  53. o => o.Next![AttachedProperties.AttachedStringProperty],
  54. targetProperty: TargetClass.StringProperty);
  55. Assert.Equal("foo", target.String);
  56. data.Next!.SetValue(AttachedProperties.AttachedStringProperty, "bar");
  57. Assert.Equal("bar", target.String);
  58. }
  59. [Fact]
  60. public void Should_Unsubscribe_From_AttachedProperty_Source()
  61. {
  62. var data = new SourceControl { [AttachedProperties.AttachedStringProperty] = "foo" };
  63. var (target, expression) = CreateTargetAndExpression<SourceControl, object?>(
  64. o => o[AttachedProperties.AttachedStringProperty],
  65. source: data,
  66. targetProperty: TargetClass.StringProperty);
  67. Assert.NotNull(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  68. expression.Dispose();
  69. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  70. }
  71. [Fact]
  72. public void Should_Unsubscribe_From_Chained_Source()
  73. {
  74. var data = new SourceControl
  75. {
  76. Next = new() { [AttachedProperties.AttachedStringProperty] = "foo" }
  77. };
  78. var (target, expression) = CreateTargetAndExpression<SourceControl, object?>(
  79. o => o.Next![AttachedProperties.AttachedStringProperty],
  80. source: data,
  81. targetProperty: TargetClass.StringProperty);
  82. Assert.NotNull(((IAvaloniaObjectDebug)data.Next).GetPropertyChangedSubscribers());
  83. expression.Dispose();
  84. Assert.Null(((IAvaloniaObjectDebug)data.Next).GetPropertyChangedSubscribers());
  85. }
  86. [Fact]
  87. public void Should_Not_Keep_Attached_Property_Source_Alive()
  88. {
  89. Func<(TargetClass, WeakReference<SourceControl>)> run = () =>
  90. {
  91. var source = new SourceControl();
  92. var target = CreateTargetWithSource(
  93. source,
  94. o => o.Next![AttachedProperties.AttachedStringProperty],
  95. targetProperty: TargetClass.StringProperty);
  96. return (target, new WeakReference<SourceControl>(source));
  97. };
  98. var result = run();
  99. GC.Collect();
  100. Assert.False(result.Item2.TryGetTarget(out _));
  101. GC.KeepAlive(result.Item1);
  102. }
  103. }