ExpressionObserverTests_SetValue.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Reactive.Linq;
  5. using System.Reactive.Subjects;
  6. using Avalonia.Data.Core;
  7. using Avalonia.Markup.Parsers;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.Data.Core
  11. {
  12. public class ExpressionObserverTests_SetValue
  13. {
  14. [Fact]
  15. public void Should_Set_Simple_Property_Value()
  16. {
  17. var data = new { Foo = "foo" };
  18. var target = ExpressionObserver.Create(data, o => o.Foo);
  19. using (target.Subscribe(_ => { }))
  20. {
  21. target.SetValue("bar");
  22. }
  23. Assert.Equal("foo", data.Foo);
  24. }
  25. [Fact]
  26. public void Should_Set_Value_On_Simple_Property_Chain()
  27. {
  28. var data = new Class1 { Foo = new Class2 { Bar = "bar" } };
  29. var target = ExpressionObserver.Create(data, o => o.Foo.Bar);
  30. using (target.Subscribe(_ => { }))
  31. {
  32. target.SetValue("foo");
  33. }
  34. Assert.Equal("foo", data.Foo.Bar);
  35. }
  36. [Fact]
  37. public void Should_Not_Try_To_Set_Value_On_Broken_Chain()
  38. {
  39. var data = new Class1 { Foo = new Class2 { Bar = "bar" } };
  40. var target = ExpressionObserver.Create(data, o => o.Foo.Bar);
  41. // Ensure the ExpressionObserver's subscriptions are kept active.
  42. using (target.OfType<string>().Subscribe(x => { }))
  43. {
  44. data.Foo = null;
  45. Assert.False(target.SetValue("foo"));
  46. }
  47. }
  48. /// <summary>
  49. /// Test for #831 - Bound properties are incorrectly updated when changing tab items.
  50. /// </summary>
  51. /// <remarks>
  52. /// There was a bug whereby pushing a null as the ExpressionObserver root didn't update
  53. /// the leaf node, cauing a subsequent SetValue to update an object that should have become
  54. /// unbound.
  55. /// </remarks>
  56. [Fact]
  57. public void Pushing_Null_To_RootObservable_Updates_Leaf_Node()
  58. {
  59. var data = new Class1 { Foo = new Class2 { Bar = "bar" } };
  60. var rootObservable = new BehaviorSubject<Class1>(data);
  61. var target = ExpressionObserver.Create(rootObservable, o => o.Foo.Bar);
  62. using (target.Subscribe(_ => { }))
  63. {
  64. rootObservable.OnNext(null);
  65. target.SetValue("baz");
  66. Assert.Equal("bar", data.Foo.Bar);
  67. }
  68. }
  69. private class Class1 : NotifyingBase
  70. {
  71. private Class2 _foo;
  72. public Class2 Foo
  73. {
  74. get { return _foo; }
  75. set
  76. {
  77. _foo = value;
  78. RaisePropertyChanged(nameof(Foo));
  79. }
  80. }
  81. }
  82. private class Class2 : NotifyingBase
  83. {
  84. private string _bar;
  85. public string Bar
  86. {
  87. get { return _bar; }
  88. set
  89. {
  90. _bar = value;
  91. RaisePropertyChanged(nameof(Bar));
  92. }
  93. }
  94. }
  95. }
  96. }