StyleTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Linq;
  4. using System.Reactive.Linq;
  5. using Avalonia.Controls;
  6. using Avalonia.Data;
  7. using Avalonia.Markup.Xaml.Data;
  8. using Avalonia.Styling;
  9. using Avalonia.UnitTests;
  10. using Xunit;
  11. namespace Avalonia.Markup.Xaml.UnitTests
  12. {
  13. public class StyleTests
  14. {
  15. [Fact]
  16. public void Binding_Should_Be_Assigned_To_Setter_Value_Instead_Of_Bound()
  17. {
  18. using (UnitTestApplication.Start(TestServices.MockPlatformWrapper))
  19. {
  20. var xaml = "<Style xmlns='https://github.com/avaloniaui'><Setter Value='{Binding}'/></Style>";
  21. var loader = new AvaloniaXamlLoader();
  22. var style = (Style)loader.Load(xaml);
  23. var setter = (Setter)(style.Setters.First());
  24. Assert.IsType<Binding>(setter.Value);
  25. }
  26. }
  27. [Fact]
  28. public void Setter_With_TwoWay_Binding_Should_Update_Source()
  29. {
  30. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  31. {
  32. var data = new Data
  33. {
  34. Foo = "foo",
  35. };
  36. var control = new TextBox
  37. {
  38. DataContext = data,
  39. };
  40. var setter = new Setter
  41. {
  42. Property = TextBox.TextProperty,
  43. Value = new Binding
  44. {
  45. Path = "Foo",
  46. Mode = BindingMode.TwoWay
  47. }
  48. };
  49. setter.Apply(null, control, null);
  50. Assert.Equal("foo", control.Text);
  51. control.Text = "bar";
  52. Assert.Equal("bar", data.Foo);
  53. }
  54. }
  55. [Fact]
  56. public void Setter_With_TwoWay_Binding_And_Activator_Should_Update_Source()
  57. {
  58. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  59. {
  60. var data = new Data
  61. {
  62. Foo = "foo",
  63. };
  64. var control = new TextBox
  65. {
  66. DataContext = data,
  67. };
  68. var setter = new Setter
  69. {
  70. Property = TextBox.TextProperty,
  71. Value = new Binding
  72. {
  73. Path = "Foo",
  74. Mode = BindingMode.TwoWay
  75. }
  76. };
  77. var activator = Observable.Never<bool>().StartWith(true);
  78. setter.Apply(null, control, activator);
  79. Assert.Equal("foo", control.Text);
  80. control.Text = "bar";
  81. Assert.Equal("bar", data.Foo);
  82. }
  83. }
  84. private class Data
  85. {
  86. public string Foo { get; set; }
  87. }
  88. }
  89. }