AvaloniaPropertyConverterTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Avalonia.Markup.Xaml.XamlIl.Runtime;
  11. namespace Avalonia.Markup.Xaml.UnitTests.Converters
  12. {
  13. public class AvaloniaPropertyConverterTest
  14. {
  15. public AvaloniaPropertyConverterTest()
  16. {
  17. // Ensure properties are registered.
  18. var foo = Class1.FooProperty;
  19. var attached = AttachedOwner.AttachedProperty;
  20. }
  21. [Fact]
  22. public void ConvertFrom_Finds_Fully_Qualified_Property()
  23. {
  24. var target = new AvaloniaPropertyTypeConverter();
  25. var style = new Style(x => x.OfType<Class1>());
  26. var context = CreateContext(style);
  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 style = new Style(x => x.OfType<Class1>());
  44. var context = CreateContext(style);
  45. var result = target.ConvertFrom(context, null, "AttachedOwner.Attached");
  46. Assert.Equal(AttachedOwner.AttachedProperty, result);
  47. }
  48. [Fact]
  49. public void ConvertFrom_Finds_Attached_Property_With_Parentheses()
  50. {
  51. var target = new AvaloniaPropertyTypeConverter();
  52. var style = new Style(x => x.OfType<Class1>());
  53. var context = CreateContext(style);
  54. var result = target.ConvertFrom(context, null, "(AttachedOwner.Attached)");
  55. Assert.Equal(AttachedOwner.AttachedProperty, result);
  56. }
  57. [Fact]
  58. public void ConvertFrom_Throws_For_Nonexistent_Property()
  59. {
  60. var target = new AvaloniaPropertyTypeConverter();
  61. var style = new Style(x => x.OfType<Class1>());
  62. var context = CreateContext(style);
  63. var ex = Assert.Throws<XamlLoadException>(() => target.ConvertFrom(context, null, "Nonexistent"));
  64. Assert.Equal("Could not find property 'Class1.Nonexistent'.", ex.Message);
  65. }
  66. [Fact]
  67. public void ConvertFrom_Throws_For_Nonexistent_Attached_Property()
  68. {
  69. var target = new AvaloniaPropertyTypeConverter();
  70. var style = new Style(x => x.OfType<Class1>());
  71. var context = CreateContext(style);
  72. var ex = Assert.Throws<XamlLoadException>(() => target.ConvertFrom(context, null, "AttachedOwner.NonExistent"));
  73. Assert.Equal("Could not find property 'AttachedOwner.NonExistent'.", ex.Message);
  74. }
  75. private ITypeDescriptorContext CreateContext(Style style = null)
  76. {
  77. var tdMock = new Mock<ITypeDescriptorContext>();
  78. var tr = new Mock<IXamlTypeResolver>();
  79. var ps = new Mock<IAvaloniaXamlIlParentStackProvider>();
  80. tdMock.Setup(d => d.GetService(typeof(IXamlTypeResolver)))
  81. .Returns(tr.Object);
  82. tdMock.Setup(d => d.GetService(typeof(IAvaloniaXamlIlParentStackProvider)))
  83. .Returns(ps.Object);
  84. ps.SetupGet(v => v.Parents)
  85. .Returns(new object[] {style});
  86. tr.Setup(v => v.Resolve(nameof(Class1)))
  87. .Returns(typeof(Class1));
  88. tr.Setup(v => v.Resolve(nameof(AttachedOwner)))
  89. .Returns(typeof(AttachedOwner));
  90. return tdMock.Object;
  91. }
  92. private class Class1 : AvaloniaObject, IStyleable
  93. {
  94. public static readonly StyledProperty<string> FooProperty =
  95. AvaloniaProperty.Register<Class1, string>("Foo");
  96. public IAvaloniaReadOnlyList<string> Classes
  97. {
  98. get { throw new NotImplementedException(); }
  99. }
  100. public string Name
  101. {
  102. get { throw new NotImplementedException(); }
  103. }
  104. public Type StyleKey
  105. {
  106. get { throw new NotImplementedException(); }
  107. }
  108. public ITemplatedControl TemplatedParent
  109. {
  110. get { throw new NotImplementedException(); }
  111. }
  112. public void DetachStyles()
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public void StyleApplied(IStyleInstance instance)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. }
  121. private class AttachedOwner
  122. {
  123. public static readonly AttachedProperty<string> AttachedProperty =
  124. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  125. }
  126. }
  127. }