ExpressionObserverBuilderTests_AvaloniaProperty.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 System.Threading.Tasks;
  7. using Avalonia.Diagnostics;
  8. using Avalonia.Data.Core;
  9. using Xunit;
  10. using Avalonia.Markup.Parsers;
  11. namespace Avalonia.Markup.UnitTests.Parsers
  12. {
  13. public class ExpressionObserverBuilderTests_AvaloniaProperty
  14. {
  15. public ExpressionObserverBuilderTests_AvaloniaProperty()
  16. {
  17. var foo = Class1.FooProperty;
  18. }
  19. [Fact]
  20. public async Task Should_Get_AvaloniaProperty_By_Name()
  21. {
  22. var data = new Class1();
  23. var target = ExpressionObserverBuilder.Build(data, "Foo");
  24. var result = await target.Take(1);
  25. Assert.Equal("foo", result);
  26. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  27. }
  28. [Fact]
  29. public void Should_Track_AvaloniaProperty_By_Name()
  30. {
  31. var data = new Class1();
  32. var target = ExpressionObserverBuilder.Build(data, "Foo");
  33. var result = new List<object>();
  34. var sub = target.Subscribe(x => result.Add(x));
  35. data.SetValue(Class1.FooProperty, "bar");
  36. Assert.Equal(new[] { "foo", "bar" }, result);
  37. sub.Dispose();
  38. Assert.Null(((IAvaloniaObjectDebug)data).GetPropertyChangedSubscribers());
  39. }
  40. private class Class1 : AvaloniaObject
  41. {
  42. public static readonly StyledProperty<string> FooProperty =
  43. AvaloniaProperty.Register<Class1, string>("Foo", defaultValue: "foo");
  44. public string ClrProperty { get; } = "clr-property";
  45. }
  46. }
  47. }