ActivatedSubjectTests.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) The Perspex 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.Disposables;
  5. using System.Reactive.Subjects;
  6. using Xunit;
  7. namespace Perspex.Styling.UnitTests
  8. {
  9. public class ActivatedSubjectTests
  10. {
  11. [Fact]
  12. public void Should_Set_Values()
  13. {
  14. var activator = new BehaviorSubject<bool>(false);
  15. var source = new TestSubject();
  16. var target = new ActivatedSubject(activator, source, string.Empty);
  17. target.OnNext("bar");
  18. Assert.Equal(PerspexProperty.UnsetValue, source.Value);
  19. activator.OnNext(true);
  20. target.OnNext("baz");
  21. Assert.Equal("baz", source.Value);
  22. activator.OnNext(false);
  23. Assert.Equal(PerspexProperty.UnsetValue, source.Value);
  24. target.OnNext("bax");
  25. activator.OnNext(true);
  26. Assert.Equal("bax", source.Value);
  27. }
  28. [Fact]
  29. public void Should_Invoke_OnCompleted_On_Activator_Completed()
  30. {
  31. var activator = new BehaviorSubject<bool>(false);
  32. var source = new TestSubject();
  33. var target = new ActivatedSubject(activator, source, string.Empty);
  34. activator.OnCompleted();
  35. Assert.True(source.Completed);
  36. }
  37. [Fact]
  38. public void Should_Invoke_OnError_On_Activator_Error()
  39. {
  40. var activator = new BehaviorSubject<bool>(false);
  41. var source = new TestSubject();
  42. var target = new ActivatedSubject(activator, source, string.Empty);
  43. activator.OnError(new Exception());
  44. Assert.NotNull(source.Error);
  45. }
  46. private class Class1 : PerspexObject
  47. {
  48. public static readonly StyledProperty<string> FooProperty =
  49. PerspexProperty.Register<Class1, string>("Foo", "foodefault");
  50. public string Foo
  51. {
  52. get { return GetValue(FooProperty); }
  53. set { SetValue(FooProperty, value); }
  54. }
  55. }
  56. private class TestSubject : ISubject<object>
  57. {
  58. private IObserver<object> _observer;
  59. public bool Completed { get; set; }
  60. public Exception Error { get; set; }
  61. public object Value { get; set; } = PerspexProperty.UnsetValue;
  62. public void OnCompleted()
  63. {
  64. Completed = true;
  65. }
  66. public void OnError(Exception error)
  67. {
  68. Error = error;
  69. }
  70. public void OnNext(object value)
  71. {
  72. Value = value;
  73. }
  74. public IDisposable Subscribe(IObserver<object> observer)
  75. {
  76. _observer = observer;
  77. return Disposable.Empty;
  78. }
  79. }
  80. }
  81. }