SetterTests.cs 5.4 KB

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