ExpressionObserverTests_SetValue.cs 3.1 KB

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