AvaloniaPropertyConverterTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.ComponentModel;
  2. using Avalonia.Markup.Xaml.Converters;
  3. using Avalonia.Markup.Xaml.XamlIl.Runtime;
  4. using Avalonia.Styling;
  5. using Moq;
  6. using Xunit;
  7. namespace Avalonia.Markup.Xaml.UnitTests.Converters
  8. {
  9. public class AvaloniaPropertyConverterTest : XamlTestBase
  10. {
  11. public AvaloniaPropertyConverterTest()
  12. {
  13. // Ensure properties are registered.
  14. _ = Class1.FooProperty;
  15. _ = AttachedOwner.AttachedProperty;
  16. }
  17. [Fact]
  18. public void ConvertFrom_Finds_Fully_Qualified_Property()
  19. {
  20. var target = new AvaloniaPropertyTypeConverter();
  21. var style = new Style(x => x.OfType<Class1>());
  22. var context = CreateContext(style);
  23. var result = target.ConvertFrom(context, null, "Class1.Foo");
  24. Assert.Equal(Class1.FooProperty, result);
  25. }
  26. [Fact]
  27. public void ConvertFrom_Uses_Selector_TargetType()
  28. {
  29. var target = new AvaloniaPropertyTypeConverter();
  30. var style = new Style(x => x.OfType<Class1>());
  31. var context = CreateContext(style);
  32. var result = target.ConvertFrom(context, null, "Foo");
  33. Assert.Equal(Class1.FooProperty, result);
  34. }
  35. [Fact]
  36. public void ConvertFrom_Finds_Attached_Property()
  37. {
  38. var target = new AvaloniaPropertyTypeConverter();
  39. var style = new Style(x => x.OfType<Class1>());
  40. var context = CreateContext(style);
  41. var result = target.ConvertFrom(context, null, "AttachedOwner.Attached");
  42. Assert.Equal(AttachedOwner.AttachedProperty, result);
  43. }
  44. [Fact]
  45. public void ConvertFrom_Finds_Attached_Property_With_Parentheses()
  46. {
  47. var target = new AvaloniaPropertyTypeConverter();
  48. var style = new Style(x => x.OfType<Class1>());
  49. var context = CreateContext(style);
  50. var result = target.ConvertFrom(context, null, "(AttachedOwner.Attached)");
  51. Assert.Equal(AttachedOwner.AttachedProperty, result);
  52. }
  53. [Fact]
  54. public void ConvertFrom_Throws_For_Nonexistent_Property()
  55. {
  56. var target = new AvaloniaPropertyTypeConverter();
  57. var style = new Style(x => x.OfType<Class1>());
  58. var context = CreateContext(style);
  59. var ex = Assert.Throws<XamlLoadException>(() => target.ConvertFrom(context, null, "Nonexistent"));
  60. Assert.Equal("Could not find property 'Class1.Nonexistent'.", ex.Message);
  61. }
  62. [Fact]
  63. public void ConvertFrom_Throws_For_Nonexistent_Attached_Property()
  64. {
  65. var target = new AvaloniaPropertyTypeConverter();
  66. var style = new Style(x => x.OfType<Class1>());
  67. var context = CreateContext(style);
  68. var ex = Assert.Throws<XamlLoadException>(() => target.ConvertFrom(context, null, "AttachedOwner.NonExistent"));
  69. Assert.Equal("Could not find property 'AttachedOwner.NonExistent'.", ex.Message);
  70. }
  71. private ITypeDescriptorContext CreateContext(Style style = null)
  72. {
  73. var tdMock = new Mock<ITypeDescriptorContext>();
  74. var tr = new Mock<IXamlTypeResolver>();
  75. var ps = new Mock<IAvaloniaXamlIlParentStackProvider>();
  76. tdMock.Setup(d => d.GetService(typeof(IXamlTypeResolver)))
  77. .Returns(tr.Object);
  78. tdMock.Setup(d => d.GetService(typeof(IAvaloniaXamlIlParentStackProvider)))
  79. .Returns(ps.Object);
  80. ps.SetupGet(v => v.Parents)
  81. .Returns(new object[] {style});
  82. tr.Setup(v => v.Resolve(nameof(Class1)))
  83. .Returns(typeof(Class1));
  84. tr.Setup(v => v.Resolve(nameof(AttachedOwner)))
  85. .Returns(typeof(AttachedOwner));
  86. return tdMock.Object;
  87. }
  88. private class Class1 : StyledElement
  89. {
  90. public static readonly StyledProperty<string> FooProperty =
  91. AvaloniaProperty.Register<Class1, string>("Foo");
  92. }
  93. private class AttachedOwner
  94. {
  95. public static readonly AttachedProperty<string> AttachedProperty =
  96. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  97. }
  98. }
  99. }