StyleTests.cs 5.1 KB

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