SetterTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Globalization;
  3. using System.Reactive.Subjects;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Data;
  7. using Avalonia.Data.Converters;
  8. using Moq;
  9. using Xunit;
  10. namespace Avalonia.Styling.UnitTests
  11. {
  12. public class SetterTests
  13. {
  14. [Fact]
  15. public void Cannot_Assign_Control_To_Value()
  16. {
  17. var target = new Setter();
  18. Assert.Throws<InvalidOperationException>(() => target.Value = new Border());
  19. }
  20. [Fact]
  21. public void Setter_Should_Apply_Binding_To_Property()
  22. {
  23. var control = new TextBlock();
  24. var subject = new BehaviorSubject<object>("foo");
  25. var descriptor = InstancedBinding.OneWay(subject);
  26. var binding = Mock.Of<IBinding>(x => x.Initiate(control, TextBlock.TextProperty, null, false) == descriptor);
  27. var style = Mock.Of<IStyle>();
  28. var setter = new Setter(TextBlock.TextProperty, binding);
  29. setter.Instance(control).Start(false);
  30. Assert.Equal("foo", control.Text);
  31. }
  32. [Fact]
  33. public void Setter_Should_Materialize_Template_To_Property()
  34. {
  35. var control = new Decorator();
  36. var template = new FuncTemplate<Canvas>(() => new Canvas());
  37. var style = Mock.Of<IStyle>();
  38. var setter = new Setter(Decorator.ChildProperty, template);
  39. setter.Instance(control).Start(false);
  40. Assert.IsType<Canvas>(control.Child);
  41. }
  42. [Fact]
  43. public void Does_Not_Call_Converter_ConvertBack_On_OneWay_Binding()
  44. {
  45. var control = new Decorator { Name = "foo" };
  46. var style = Mock.Of<IStyle>();
  47. var binding = new Binding("Name", BindingMode.OneWay)
  48. {
  49. Converter = new TestConverter(),
  50. RelativeSource = new RelativeSource(RelativeSourceMode.Self),
  51. };
  52. var setter = new Setter(Decorator.TagProperty, binding);
  53. var instance = setter.Instance(control);
  54. instance.Start(true);
  55. instance.Activate();
  56. Assert.Equal("foobar", control.Tag);
  57. // Issue #1218 caused TestConverter.ConvertBack to throw here.
  58. instance.Deactivate();
  59. Assert.Null(control.Tag);
  60. }
  61. [Fact]
  62. public void Setter_Should_Apply_Value_Without_Activator_With_Style_Priority()
  63. {
  64. var control = new Mock<IStyleable>();
  65. var style = Mock.Of<Style>();
  66. var setter = new Setter(TextBlock.TagProperty, "foo");
  67. setter.Instance(control.Object).Start(false);
  68. control.Verify(x => x.SetValue(
  69. TextBlock.TagProperty,
  70. "foo",
  71. BindingPriority.Style));
  72. }
  73. [Fact]
  74. public void Setter_Should_Apply_Value_With_Activator_As_Binding_With_StyleTrigger_Priority()
  75. {
  76. var control = new Mock<IStyleable>();
  77. var style = Mock.Of<Style>();
  78. var setter = new Setter(TextBlock.TagProperty, "foo");
  79. var activator = new Subject<bool>();
  80. var instance = setter.Instance(control.Object);
  81. instance.Start(true);
  82. instance.Activate();
  83. control.Verify(x => x.Bind(
  84. TextBlock.TagProperty,
  85. It.IsAny<IObservable<BindingValue<object>>>(),
  86. BindingPriority.StyleTrigger));
  87. }
  88. [Fact]
  89. public void Setter_Should_Apply_Binding_Without_Activator_With_Style_Priority()
  90. {
  91. var control = new Mock<IStyleable>();
  92. var style = Mock.Of<Style>();
  93. var setter = new Setter(TextBlock.TagProperty, CreateMockBinding(TextBlock.TagProperty));
  94. setter.Instance(control.Object).Start(false);
  95. control.Verify(x => x.Bind(
  96. TextBlock.TagProperty,
  97. It.IsAny<PropertySetterBindingInstance<object>>(),
  98. BindingPriority.Style));
  99. }
  100. [Fact]
  101. public void Setter_Should_Apply_Binding_With_Activator_With_StyleTrigger_Priority()
  102. {
  103. var control = new Mock<IStyleable>();
  104. var style = Mock.Of<Style>();
  105. var setter = new Setter(TextBlock.TagProperty, CreateMockBinding(TextBlock.TagProperty));
  106. var instance = setter.Instance(control.Object);
  107. instance.Start(true);
  108. instance.Activate();
  109. control.Verify(x => x.Bind(
  110. TextBlock.TagProperty,
  111. It.IsAny<IObservable<BindingValue<object>>>(),
  112. BindingPriority.StyleTrigger));
  113. }
  114. private IBinding CreateMockBinding(AvaloniaProperty property)
  115. {
  116. var subject = new Subject<object>();
  117. var descriptor = InstancedBinding.OneWay(subject);
  118. var binding = Mock.Of<IBinding>(x =>
  119. x.Initiate(It.IsAny<IAvaloniaObject>(), property, null, false) == descriptor);
  120. return binding;
  121. }
  122. private class TestConverter : IValueConverter
  123. {
  124. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  125. {
  126. return value.ToString() + "bar";
  127. }
  128. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  129. {
  130. throw new NotImplementedException();
  131. }
  132. }
  133. }
  134. }