PerspexPropertyConverterTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 System.Reactive;
  5. using Moq;
  6. using OmniXaml;
  7. using OmniXaml.ObjectAssembler.Commands;
  8. using OmniXaml.TypeConversion;
  9. using OmniXaml.Typing;
  10. using Perspex.Collections;
  11. using Perspex.Controls;
  12. using Perspex.Markup.Xaml.Converters;
  13. using Perspex.Styling;
  14. using Xunit;
  15. namespace Perspex.Markup.Xaml.UnitTests.Converters
  16. {
  17. public class PerspexPropertyConverterTest
  18. {
  19. public PerspexPropertyConverterTest()
  20. {
  21. // Ensure properties are registered.
  22. var foo = Class1.FooProperty;
  23. var attached = AttachedOwner.AttachedProperty;
  24. }
  25. [Fact]
  26. public void ConvertFrom_Finds_Fully_Qualified_Property()
  27. {
  28. var target = new PerspexPropertyTypeConverter();
  29. var context = CreateContext();
  30. var result = target.ConvertFrom(context, null, "Class1.Foo");
  31. Assert.Equal(Class1.FooProperty, result);
  32. }
  33. [Fact]
  34. public void ConvertFrom_Uses_Selector_TargetType()
  35. {
  36. var target = new PerspexPropertyTypeConverter();
  37. var style = new Style(x => x.OfType<Class1>());
  38. var context = CreateContext(style);
  39. var result = target.ConvertFrom(context, null, "Foo");
  40. Assert.Equal(Class1.FooProperty, result);
  41. }
  42. [Fact]
  43. public void ConvertFrom_Finds_Attached_Property()
  44. {
  45. var target = new PerspexPropertyTypeConverter();
  46. var context = CreateContext();
  47. var result = target.ConvertFrom(context, null, "AttachedOwner.Attached");
  48. Assert.Equal(AttachedOwner.AttachedProperty, result);
  49. }
  50. private IXamlTypeConverterContext CreateContext(Style style = null)
  51. {
  52. var context = new Mock<IXamlTypeConverterContext>();
  53. var topDownValueContext = new Mock<ITopDownValueContext>();
  54. var typeRepository = new Mock<IXamlTypeRepository>();
  55. var featureProvider = new Mock<ITypeFeatureProvider>();
  56. var class1XamlType = new XamlType(typeof(Class1), typeRepository.Object, null, featureProvider.Object);
  57. var attachedOwnerXamlType = new XamlType(typeof(AttachedOwner), typeRepository.Object, null, featureProvider.Object);
  58. context.Setup(x => x.TopDownValueContext).Returns(topDownValueContext.Object);
  59. context.Setup(x => x.TypeRepository).Returns(typeRepository.Object);
  60. topDownValueContext.Setup(x => x.GetLastInstance(It.IsAny<XamlType>())).Returns(style);
  61. typeRepository.Setup(x => x.GetByQualifiedName("Class1")).Returns(class1XamlType);
  62. typeRepository.Setup(x => x.GetByQualifiedName("AttachedOwner")).Returns(attachedOwnerXamlType);
  63. return context.Object;
  64. }
  65. private class Class1 : PerspexObject, IStyleable
  66. {
  67. public static readonly StyledProperty<string> FooProperty =
  68. PerspexProperty.Register<Class1, string>("Foo");
  69. public IPerspexReadOnlyList<string> Classes
  70. {
  71. get { throw new NotImplementedException(); }
  72. }
  73. public string Name
  74. {
  75. get { throw new NotImplementedException(); }
  76. }
  77. public Type StyleKey
  78. {
  79. get { throw new NotImplementedException(); }
  80. }
  81. public ITemplatedControl TemplatedParent
  82. {
  83. get { throw new NotImplementedException(); }
  84. }
  85. IObservable<Unit> IStyleable.StyleDetach { get; }
  86. }
  87. private class AttachedOwner
  88. {
  89. public static readonly AttachedProperty<string> AttachedProperty =
  90. PerspexProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  91. }
  92. }
  93. }