SetterTests.cs 5.9 KB

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