StyleTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Linq;
  2. using Avalonia.Controls;
  3. using Avalonia.Data;
  4. using Avalonia.Layout;
  5. using Avalonia.Styling;
  6. using Avalonia.UnitTests;
  7. using Xunit;
  8. namespace Avalonia.Markup.Xaml.UnitTests
  9. {
  10. public class StyleTests : XamlTestBase
  11. {
  12. [Fact]
  13. public void Binding_As_Attribute_Should_Be_Assigned_To_Setter_Value_Instead_Of_Bound()
  14. {
  15. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  16. {
  17. var xaml = "<Style Selector='Button' xmlns='https://github.com/avaloniaui'><Setter Property='Content' Value='{Binding}'/></Style>";
  18. var style = (Style)AvaloniaRuntimeXamlLoader.Load(xaml);
  19. var setter = (Setter)(style.Setters.First());
  20. Assert.IsType<Binding>(setter.Value);
  21. }
  22. }
  23. [Theory]
  24. [InlineData(nameof(ContentControl.Content))] // standard property
  25. [InlineData(nameof(Layoutable.Margin))] // primitive property which can be directly parsed
  26. public void Binding_As_Element_Should_Be_Assigned_To_Setter_Value(string propertyName)
  27. {
  28. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  29. {
  30. var style = (Style)AvaloniaRuntimeXamlLoader.Load(
  31. $"""
  32. <Style Selector="Button" xmlns="https://github.com/avaloniaui">
  33. <Setter Property="{propertyName}">
  34. <Binding />
  35. </Setter>
  36. </Style>
  37. """);
  38. var setter = (Setter)style.Setters.First();
  39. Assert.IsType<Binding>(setter.Value);
  40. }
  41. }
  42. [Fact]
  43. public void Xml_Value_Should_Be_Assigned_To_Setter_Value()
  44. {
  45. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  46. {
  47. var style = (Style)AvaloniaRuntimeXamlLoader.Load(@"
  48. <Style Selector='Button' xmlns='https://github.com/avaloniaui'>
  49. <Setter Property='Margin'>
  50. 10, 4, 0, 4
  51. </Setter>
  52. </Style>");
  53. var setter = (Setter)(style.Setters.First());
  54. var thickness = Assert.IsType<Thickness>(setter.Value);
  55. Assert.Equal(new Thickness(10, 4, 0, 4), thickness);
  56. }
  57. }
  58. [Fact]
  59. public void Setter_With_TwoWay_Binding_Should_Update_Source()
  60. {
  61. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  62. {
  63. var data = new Data
  64. {
  65. Foo = "foo",
  66. };
  67. var control = new TextBox
  68. {
  69. DataContext = data,
  70. };
  71. var style = new Style()
  72. {
  73. Setters =
  74. {
  75. new Setter
  76. {
  77. Property = TextBox.TextProperty,
  78. Value = new Binding
  79. {
  80. Path = "Foo",
  81. Mode = BindingMode.TwoWay
  82. }
  83. }
  84. }
  85. };
  86. StyleHelpers.TryAttach(style, control);
  87. Assert.Equal("foo", control.Text);
  88. control.Text = "bar";
  89. Assert.Equal("bar", data.Foo);
  90. }
  91. }
  92. private class Data
  93. {
  94. public string Foo { get; set; }
  95. }
  96. }
  97. }