StyleTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  4. using System.Collections.Generic;
  5. using System.Reactive.Subjects;
  6. using Perspex.Controls;
  7. using Xunit;
  8. namespace Perspex.Styling.UnitTests
  9. {
  10. public class StyleTests
  11. {
  12. [Fact]
  13. public void Style_With_Only_Type_Selector_Should_Update_Value()
  14. {
  15. Style style = new Style(x => x.OfType<Class1>())
  16. {
  17. Setters = new[]
  18. {
  19. new Setter(Class1.FooProperty, "Foo"),
  20. },
  21. };
  22. var target = new Class1();
  23. style.Attach(target, null);
  24. Assert.Equal("Foo", target.Foo);
  25. }
  26. [Fact]
  27. public void Style_With_Class_Selector_Should_Update_And_Restore_Value()
  28. {
  29. Style style = new Style(x => x.OfType<Class1>().Class("foo"))
  30. {
  31. Setters = new[]
  32. {
  33. new Setter(Class1.FooProperty, "Foo"),
  34. },
  35. };
  36. var target = new Class1();
  37. style.Attach(target, null);
  38. Assert.Equal("foodefault", target.Foo);
  39. target.Classes.Add("foo");
  40. Assert.Equal("Foo", target.Foo);
  41. target.Classes.Remove("foo");
  42. Assert.Equal("foodefault", target.Foo);
  43. }
  44. [Fact]
  45. public void Style_With_No_Selector_Should_Apply_To_Containing_Control()
  46. {
  47. Style style = new Style
  48. {
  49. Setters = new[]
  50. {
  51. new Setter(Class1.FooProperty, "Foo"),
  52. },
  53. };
  54. var target = new Class1();
  55. style.Attach(target, target);
  56. Assert.Equal("Foo", target.Foo);
  57. }
  58. [Fact]
  59. public void Style_With_No_Selector_Should_Not_Apply_To_Other_Control()
  60. {
  61. Style style = new Style
  62. {
  63. Setters = new[]
  64. {
  65. new Setter(Class1.FooProperty, "Foo"),
  66. },
  67. };
  68. var target = new Class1();
  69. var other = new Class1();
  70. style.Attach(target, other);
  71. Assert.Equal("foodefault", target.Foo);
  72. }
  73. [Fact]
  74. public void LocalValue_Should_Override_Style()
  75. {
  76. Style style = new Style(x => x.OfType<Class1>())
  77. {
  78. Setters = new[]
  79. {
  80. new Setter(Class1.FooProperty, "Foo"),
  81. },
  82. };
  83. var target = new Class1
  84. {
  85. Foo = "Original",
  86. };
  87. style.Attach(target, null);
  88. Assert.Equal("Original", target.Foo);
  89. }
  90. [Fact]
  91. public void Later_Styles_Should_Override_Earlier()
  92. {
  93. Styles styles = new Styles
  94. {
  95. new Style(x => x.OfType<Class1>().Class("foo"))
  96. {
  97. Setters = new[]
  98. {
  99. new Setter(Class1.FooProperty, "Foo"),
  100. },
  101. },
  102. new Style(x => x.OfType<Class1>().Class("foo"))
  103. {
  104. Setters = new[]
  105. {
  106. new Setter(Class1.FooProperty, "Bar"),
  107. },
  108. }
  109. };
  110. var target = new Class1();
  111. List<string> values = new List<string>();
  112. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  113. styles.Attach(target, null);
  114. target.Classes.Add("foo");
  115. target.Classes.Remove("foo");
  116. Assert.Equal(new[] { "foodefault", "Foo", "Bar", "foodefault" }, values);
  117. }
  118. [Fact]
  119. public void Style_Should_Detach_When_Removed_From_Logical_Tree()
  120. {
  121. Border border;
  122. var style = new Style(x => x.OfType<Border>())
  123. {
  124. Setters = new[]
  125. {
  126. new Setter(Border.BorderThicknessProperty, 4),
  127. }
  128. };
  129. var root = new TestRoot
  130. {
  131. Child = border = new Border(),
  132. };
  133. style.Attach(border, null);
  134. Assert.Equal(4, border.BorderThickness);
  135. root.Child = null;
  136. Assert.Equal(0, border.BorderThickness);
  137. }
  138. private class Class1 : Control
  139. {
  140. public static readonly StyledProperty<string> FooProperty =
  141. PerspexProperty.Register<Class1, string>("Foo", "foodefault");
  142. public string Foo
  143. {
  144. get { return GetValue(FooProperty); }
  145. set { SetValue(FooProperty, value); }
  146. }
  147. protected override Size MeasureOverride(Size availableSize)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. }
  152. }
  153. }