DirectPropertyTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Subjects;
  5. using Avalonia.Data;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests
  8. {
  9. public class DirectPropertyTests
  10. {
  11. [Fact]
  12. public void Initialized_Observable_Fired()
  13. {
  14. bool invoked = false;
  15. Class1.FooProperty.Initialized.Subscribe(x =>
  16. {
  17. Assert.Equal(AvaloniaProperty.UnsetValue, x.OldValue);
  18. Assert.Equal("foo", x.NewValue);
  19. Assert.Equal(BindingPriority.Unset, x.Priority);
  20. invoked = true;
  21. });
  22. var target = new Class1();
  23. Assert.True(invoked);
  24. }
  25. [Fact]
  26. public void IsDirect_Property_Returns_True()
  27. {
  28. var target = new DirectProperty<Class1, string>(
  29. "test",
  30. o => null,
  31. null,
  32. new DirectPropertyMetadata<string>());
  33. Assert.True(target.IsDirect);
  34. }
  35. [Fact]
  36. public void AddOwnered_Property_Should_Equal_Original()
  37. {
  38. var p1 = Class1.FooProperty;
  39. var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
  40. Assert.NotSame(p1, p2);
  41. Assert.True(p1.Equals(p2));
  42. Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
  43. Assert.True(p1 == p2);
  44. }
  45. [Fact]
  46. public void AddOwnered_Property_Should_Have_OwnerType_Set()
  47. {
  48. var p1 = Class1.FooProperty;
  49. var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
  50. Assert.Equal(typeof(Class2), p2.OwnerType);
  51. }
  52. [Fact]
  53. public void AddOwnered_Properties_Should_Share_Observables()
  54. {
  55. var p1 = Class1.FooProperty;
  56. var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
  57. Assert.Same(p1.Changed, p2.Changed);
  58. Assert.Same(p1.Initialized, p2.Initialized);
  59. }
  60. [Fact]
  61. public void IsAnimating_On_DirectProperty_With_Binding_Returns_False()
  62. {
  63. var target = new Class1();
  64. var source = new BehaviorSubject<string>("foo");
  65. target.Bind(Class1.FooProperty, source, BindingPriority.Animation);
  66. Assert.False(target.IsAnimating(Class1.FooProperty));
  67. }
  68. private class Class1 : AvaloniaObject
  69. {
  70. public static readonly DirectProperty<Class1, string> FooProperty =
  71. AvaloniaProperty.RegisterDirect<Class1, string>(nameof(Foo), o => o.Foo, (o, v) => o.Foo = v);
  72. private string _foo = "foo";
  73. public string Foo
  74. {
  75. get { return _foo; }
  76. set { SetAndRaise(FooProperty, ref _foo, value); }
  77. }
  78. }
  79. private class Class2 : AvaloniaObject
  80. {
  81. }
  82. }
  83. }