AvaloniaObjectTests_GetObservable.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_GetObservable
  9. {
  10. [Fact]
  11. public void GetObservable_Returns_Initial_Value()
  12. {
  13. Class1 target = new Class1();
  14. int raised = 0;
  15. target.GetObservable(Class1.FooProperty).Subscribe(x =>
  16. {
  17. if (x == "foodefault")
  18. {
  19. ++raised;
  20. }
  21. });
  22. Assert.Equal(1, raised);
  23. }
  24. [Fact]
  25. public void GetObservable_Returns_Property_Change()
  26. {
  27. Class1 target = new Class1();
  28. bool raised = false;
  29. target.GetObservable(Class1.FooProperty).Subscribe(x => raised = x == "newvalue");
  30. raised = false;
  31. target.SetValue(Class1.FooProperty, "newvalue");
  32. Assert.True(raised);
  33. }
  34. [Fact]
  35. public void GetObservable_Returns_Property_Change_Only_For_Correct_Property()
  36. {
  37. Class2 target = new Class2();
  38. bool raised = false;
  39. target.GetObservable(Class1.FooProperty).Subscribe(x => raised = true);
  40. raised = false;
  41. target.SetValue(Class2.BarProperty, "newvalue");
  42. Assert.False(raised);
  43. }
  44. [Fact]
  45. public void GetObservable_Dispose_Stops_Property_Changes()
  46. {
  47. Class1 target = new Class1();
  48. bool raised = false;
  49. target.GetObservable(Class1.FooProperty)
  50. .Subscribe(x => raised = true)
  51. .Dispose();
  52. raised = false;
  53. target.SetValue(Class1.FooProperty, "newvalue");
  54. Assert.False(raised);
  55. }
  56. private class Class1 : AvaloniaObject
  57. {
  58. public static readonly StyledProperty<string> FooProperty =
  59. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  60. }
  61. private class Class2 : Class1
  62. {
  63. public static readonly StyledProperty<string> BarProperty =
  64. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  65. }
  66. }
  67. }