DirectPropertyTests.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) The Perspex 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 Perspex.Data;
  5. using Xunit;
  6. namespace Perspex.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(PerspexProperty.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>("test", o => null);
  28. Assert.True(target.IsDirect);
  29. }
  30. [Fact]
  31. public void AddOwnered_Property_Should_Equal_Original()
  32. {
  33. var p1 = Class1.FooProperty;
  34. var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
  35. Assert.NotSame(p1, p2);
  36. Assert.True(p1.Equals(p2));
  37. Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
  38. Assert.True(p1 == p2);
  39. }
  40. [Fact]
  41. public void AddOwnered_Property_Should_Have_OwnerType_Set()
  42. {
  43. var p1 = Class1.FooProperty;
  44. var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
  45. Assert.Equal(typeof(Class2), p2.OwnerType);
  46. }
  47. [Fact]
  48. public void AddOwnered_Properties_Should_Share_Observables()
  49. {
  50. var p1 = Class1.FooProperty;
  51. var p2 = p1.AddOwner<Class2>(o => null, (o, v) => { });
  52. Assert.Same(p1.Changed, p2.Changed);
  53. Assert.Same(p1.Initialized, p2.Initialized);
  54. }
  55. private class Class1 : PerspexObject
  56. {
  57. public static readonly DirectProperty<Class1, string> FooProperty =
  58. PerspexProperty.RegisterDirect<Class1, string>("Foo", o => o.Foo, (o, v) => o.Foo = v);
  59. private string _foo = "foo";
  60. public string Foo
  61. {
  62. get { return _foo; }
  63. set { SetAndRaise(FooProperty, ref _foo, value); }
  64. }
  65. }
  66. private class Class2 : PerspexObject
  67. {
  68. }
  69. }
  70. }