AvaloniaObjectTests_GetSubject.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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.Linq;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests
  8. {
  9. public class AvaloniaObjectTests_GetSubject
  10. {
  11. [Fact]
  12. public void GetSubject_Returns_Values()
  13. {
  14. var source = new Class1 { Foo = "foo" };
  15. var target = source.GetSubject(Class1.FooProperty);
  16. var result = new List<string>();
  17. target.Subscribe(x => result.Add(x));
  18. source.Foo = "bar";
  19. source.Foo = "baz";
  20. Assert.Equal(new[] { "foo", "bar", "baz" }, result);
  21. }
  22. [Fact]
  23. public void GetSubject_Sets_Values()
  24. {
  25. var source = new Class1 { Foo = "foo" };
  26. var target = source.GetSubject(Class1.FooProperty);
  27. target.OnNext("bar");
  28. Assert.Equal("bar", source.Foo);
  29. }
  30. private class Class1 : AvaloniaObject
  31. {
  32. public static readonly StyledProperty<string> FooProperty =
  33. AvaloniaProperty.Register<Class1, string>(nameof(Foo), "foodefault");
  34. public string Foo
  35. {
  36. get { return GetValue(FooProperty); }
  37. set { SetValue(FooProperty, value); }
  38. }
  39. }
  40. }
  41. }