ExpressionObserverTests_Lifetime.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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;
  6. using System.Reactive.Linq;
  7. using System.Reactive.Subjects;
  8. using Microsoft.Reactive.Testing;
  9. using Avalonia.Data.Core;
  10. using Xunit;
  11. using Avalonia.Markup.Parsers;
  12. namespace Avalonia.Base.UnitTests.Data.Core
  13. {
  14. public class ExpressionObserverTests_Lifetime
  15. {
  16. [Fact]
  17. public void Should_Complete_When_Source_Observable_Completes()
  18. {
  19. var source = new BehaviorSubject<object>(1);
  20. var target = ExpressionObserver.Create<object, object>(source, o => o);
  21. var completed = false;
  22. target.Subscribe(_ => { }, () => completed = true);
  23. source.OnCompleted();
  24. Assert.True(completed);
  25. }
  26. [Fact]
  27. public void Should_Complete_When_Source_Observable_Errors()
  28. {
  29. var source = new BehaviorSubject<object>(1);
  30. var target = ExpressionObserver.Create<object, object>(source, o => o);
  31. var completed = false;
  32. target.Subscribe(_ => { }, () => completed = true);
  33. source.OnError(new Exception());
  34. Assert.True(completed);
  35. }
  36. [Fact]
  37. public void Should_Complete_When_Update_Observable_Completes()
  38. {
  39. var update = new Subject<Unit>();
  40. var target = ExpressionObserver.Create(() => 1, o => o, update);
  41. var completed = false;
  42. target.Subscribe(_ => { }, () => completed = true);
  43. update.OnCompleted();
  44. Assert.True(completed);
  45. }
  46. [Fact]
  47. public void Should_Complete_When_Update_Observable_Errors()
  48. {
  49. var update = new Subject<Unit>();
  50. var target = ExpressionObserver.Create(() => 1, o => o, update);
  51. var completed = false;
  52. target.Subscribe(_ => { }, () => completed = true);
  53. update.OnError(new Exception());
  54. Assert.True(completed);
  55. }
  56. [Fact]
  57. public void Should_Unsubscribe_From_Source_Observable()
  58. {
  59. var scheduler = new TestScheduler();
  60. var source = scheduler.CreateColdObservable(
  61. OnNext(1, new { Foo = "foo" }));
  62. var target = ExpressionObserver.Create(source, o => o.Foo);
  63. var result = new List<object>();
  64. using (target.Subscribe(x => result.Add(x)))
  65. using (target.Subscribe(_ => { }))
  66. {
  67. scheduler.Start();
  68. }
  69. Assert.Equal(new[] { "foo" }, result);
  70. Assert.All(source.Subscriptions, x => Assert.NotEqual(Subscription.Infinite, x.Unsubscribe));
  71. }
  72. [Fact]
  73. public void Should_Unsubscribe_From_Update_Observable()
  74. {
  75. var scheduler = new TestScheduler();
  76. var update = scheduler.CreateColdObservable<Unit>();
  77. var data = new { Foo = "foo" };
  78. var target = ExpressionObserver.Create(() => data, o => o.Foo, update);
  79. var result = new List<object>();
  80. using (target.Subscribe(x => result.Add(x)))
  81. using (target.Subscribe(_ => { }))
  82. {
  83. scheduler.Start();
  84. }
  85. Assert.Equal(new[] { "foo" }, result);
  86. Assert.All(update.Subscriptions, x => Assert.NotEqual(Subscription.Infinite, x.Unsubscribe));
  87. GC.KeepAlive(data);
  88. }
  89. private Recorded<Notification<T>> OnNext<T>(long time, T value)
  90. {
  91. return new Recorded<Notification<T>>(time, Notification.CreateOnNext<T>(value));
  92. }
  93. }
  94. }