StyleTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) The Perspex 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 Moq;
  6. using Perspex.Controls;
  7. using Perspex.Controls.Primitives;
  8. using Perspex.Data;
  9. using Perspex.Markup.Xaml.Data;
  10. using Perspex.Platform;
  11. using Perspex.Styling;
  12. using Xunit;
  13. namespace Perspex.Markup.Xaml.UnitTests
  14. {
  15. public class StyleTests
  16. {
  17. [Fact]
  18. public void Binding_Should_Be_Assigned_To_Setter_Value_Instead_Of_Bound()
  19. {
  20. using (PerspexLocator.EnterScope())
  21. {
  22. PerspexLocator.CurrentMutable
  23. .Bind<IPclPlatformWrapper>()
  24. .ToConstant(Mock.Of<IPclPlatformWrapper>());
  25. var xaml = "<Style xmlns='https://github.com/perspex'><Setter Value='{Binding}'/></Style>";
  26. var loader = new PerspexXamlLoader();
  27. var style = (Style)loader.Load(xaml);
  28. var setter = (Setter)(style.Setters.First());
  29. Assert.IsType<Binding>(setter.Value);
  30. }
  31. }
  32. [Fact]
  33. public void Setter_With_TwoWay_Binding_Should_Update_Source()
  34. {
  35. using (PerspexLocator.EnterScope())
  36. {
  37. PerspexLocator.CurrentMutable
  38. .Bind<IPlatformThreadingInterface>()
  39. .ToConstant(Mock.Of<IPlatformThreadingInterface>(x =>
  40. x.CurrentThreadIsLoopThread == true));
  41. var data = new Data
  42. {
  43. Foo = "foo",
  44. };
  45. var control = new TextBox
  46. {
  47. DataContext = data,
  48. };
  49. var setter = new Setter
  50. {
  51. Property = TextBox.TextProperty,
  52. Value = new Binding
  53. {
  54. Path = "Foo",
  55. Mode = BindingMode.TwoWay
  56. }
  57. };
  58. setter.Apply(null, control, null);
  59. Assert.Equal("foo", control.Text);
  60. control.Text = "bar";
  61. Assert.Equal("bar", data.Foo);
  62. }
  63. }
  64. [Fact]
  65. public void Setter_With_TwoWay_Binding_And_Activator_Should_Update_Source()
  66. {
  67. using (PerspexLocator.EnterScope())
  68. {
  69. PerspexLocator.CurrentMutable
  70. .Bind<IPlatformThreadingInterface>()
  71. .ToConstant(Mock.Of<IPlatformThreadingInterface>(x =>
  72. x.CurrentThreadIsLoopThread == true));
  73. var data = new Data
  74. {
  75. Foo = "foo",
  76. };
  77. var control = new TextBox
  78. {
  79. DataContext = data,
  80. };
  81. var setter = new Setter
  82. {
  83. Property = TextBox.TextProperty,
  84. Value = new Binding
  85. {
  86. Path = "Foo",
  87. Mode = BindingMode.TwoWay
  88. }
  89. };
  90. var activator = Observable.Never<bool>().StartWith(true);
  91. setter.Apply(null, control, activator);
  92. Assert.Equal("foo", control.Text);
  93. control.Text = "bar";
  94. Assert.Equal("bar", data.Foo);
  95. }
  96. }
  97. private class Data
  98. {
  99. public string Foo { get; set; }
  100. }
  101. }
  102. }