SetterTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. private class TestConverter : IValueConverter
  74. {
  75. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  76. {
  77. return value.ToString() + "bar";
  78. }
  79. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. }
  84. }
  85. }