DirectPropertyTests.cs 2.6 KB

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