AvaloniaPropertyConverterTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Moq;
  5. using Avalonia.Collections;
  6. using Avalonia.Markup.Xaml.Converters;
  7. using Avalonia.Styling;
  8. using Xunit;
  9. using System.ComponentModel;
  10. using Portable.Xaml;
  11. using Portable.Xaml.Markup;
  12. namespace Avalonia.Markup.Xaml.UnitTests.Converters
  13. {
  14. public class AvaloniaPropertyConverterTest
  15. {
  16. public AvaloniaPropertyConverterTest()
  17. {
  18. // Ensure properties are registered.
  19. var foo = Class1.FooProperty;
  20. var attached = AttachedOwner.AttachedProperty;
  21. }
  22. [Fact]
  23. public void ConvertFrom_Finds_Fully_Qualified_Property()
  24. {
  25. var target = new AvaloniaPropertyTypeConverter();
  26. var context = CreateContext();
  27. var result = target.ConvertFrom(context, null, "Class1.Foo");
  28. Assert.Equal(Class1.FooProperty, result);
  29. }
  30. [Fact]
  31. public void ConvertFrom_Uses_Selector_TargetType()
  32. {
  33. var target = new AvaloniaPropertyTypeConverter();
  34. var style = new Style(x => x.OfType<Class1>());
  35. var context = CreateContext(style);
  36. var result = target.ConvertFrom(context, null, "Foo");
  37. Assert.Equal(Class1.FooProperty, result);
  38. }
  39. [Fact]
  40. public void ConvertFrom_Finds_Attached_Property()
  41. {
  42. var target = new AvaloniaPropertyTypeConverter();
  43. var context = CreateContext();
  44. var result = target.ConvertFrom(context, null, "AttachedOwner.Attached");
  45. Assert.Equal(AttachedOwner.AttachedProperty, result);
  46. }
  47. [Fact]
  48. public void ConvertFrom_Finds_Attached_Property_With_Parentheses()
  49. {
  50. var target = new AvaloniaPropertyTypeConverter();
  51. var context = CreateContext();
  52. var result = target.ConvertFrom(context, null, "(AttachedOwner.Attached)");
  53. Assert.Equal(AttachedOwner.AttachedProperty, result);
  54. }
  55. private ITypeDescriptorContext CreateContext(Style style = null)
  56. {
  57. var tdMock = new Mock<ITypeDescriptorContext>();
  58. var xsc = new Mock<IXamlSchemaContextProvider>();
  59. var sc = Mock.Of<XamlSchemaContext>();
  60. var amb = new Mock<IAmbientProvider>();
  61. var tr = new Mock<IXamlTypeResolver>();
  62. tdMock.Setup(d => d.GetService(typeof(IAmbientProvider)))
  63. .Returns(amb.Object);
  64. tdMock.Setup(d => d.GetService(typeof(IXamlSchemaContextProvider)))
  65. .Returns(xsc.Object);
  66. tdMock.Setup(d => d.GetService(typeof(IXamlTypeResolver)))
  67. .Returns(tr.Object);
  68. xsc.SetupGet(v => v.SchemaContext)
  69. .Returns(sc);
  70. amb.Setup(v => v.GetFirstAmbientValue(It.IsAny<Portable.Xaml.XamlType>()))
  71. .Returns(style);
  72. amb.Setup(v => v.GetAllAmbientValues(It.IsAny<Portable.Xaml.XamlType>()))
  73. .Returns(new object[] { style });
  74. tr.Setup(v => v.Resolve(nameof(Class1)))
  75. .Returns(typeof(Class1));
  76. tr.Setup(v => v.Resolve(nameof(AttachedOwner)))
  77. .Returns(typeof(AttachedOwner));
  78. return tdMock.Object;
  79. }
  80. private class Class1 : AvaloniaObject, IStyleable
  81. {
  82. public static readonly StyledProperty<string> FooProperty =
  83. AvaloniaProperty.Register<Class1, string>("Foo");
  84. public IAvaloniaReadOnlyList<string> Classes
  85. {
  86. get { throw new NotImplementedException(); }
  87. }
  88. public string Name
  89. {
  90. get { throw new NotImplementedException(); }
  91. }
  92. public Type StyleKey
  93. {
  94. get { throw new NotImplementedException(); }
  95. }
  96. public ITemplatedControl TemplatedParent
  97. {
  98. get { throw new NotImplementedException(); }
  99. }
  100. IObservable<IStyleable> IStyleable.StyleDetach { get; }
  101. }
  102. private class AttachedOwner
  103. {
  104. public static readonly AttachedProperty<string> AttachedProperty =
  105. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  106. }
  107. }
  108. }