StyleTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // -----------------------------------------------------------------------
  2. // <copyright file="StyleTests.cs" company="Steven Kirk">
  3. // Copyright 2014 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Styling.UnitTests
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Reactive.Subjects;
  11. using Perspex.Controls;
  12. using Perspex.Styling;
  13. using Xunit;
  14. public class StyleTests
  15. {
  16. [Fact]
  17. public void Style_With_Only_Type_Selector_Should_Update_Value()
  18. {
  19. Style style = new Style(x => x.OfType<Class1>())
  20. {
  21. Setters = new[]
  22. {
  23. new Setter(Class1.FooProperty, "Foo"),
  24. },
  25. };
  26. var target = new Class1();
  27. style.Attach(target);
  28. Assert.Equal("Foo", target.Foo);
  29. }
  30. [Fact]
  31. public void Style_With_Class_Selector_Should_Update_And_Restore_Value()
  32. {
  33. Style style = new Style(x => x.OfType<Class1>().Class("foo"))
  34. {
  35. Setters = new[]
  36. {
  37. new Setter(Class1.FooProperty, "Foo"),
  38. },
  39. };
  40. var target = new Class1();
  41. style.Attach(target);
  42. Assert.Equal("foodefault", target.Foo);
  43. target.Classes.Add("foo");
  44. Assert.Equal("Foo", target.Foo);
  45. target.Classes.Remove("foo");
  46. Assert.Equal("foodefault", target.Foo);
  47. }
  48. [Fact]
  49. public void LocalValue_Should_Override_Style()
  50. {
  51. Style style = new Style(x => x.OfType<Class1>())
  52. {
  53. Setters = new[]
  54. {
  55. new Setter(Class1.FooProperty, "Foo"),
  56. },
  57. };
  58. var target = new Class1
  59. {
  60. Foo = "Original",
  61. };
  62. style.Attach(target);
  63. Assert.Equal("Original", target.Foo);
  64. }
  65. [Fact]
  66. public void Later_Styles_Should_Override_Earlier()
  67. {
  68. Styles styles = new Styles
  69. {
  70. new Style(x => x.OfType<Class1>().Class("foo"))
  71. {
  72. Setters = new[]
  73. {
  74. new Setter(Class1.FooProperty, "Foo"),
  75. },
  76. },
  77. new Style(x => x.OfType<Class1>().Class("foo"))
  78. {
  79. Setters = new[]
  80. {
  81. new Setter(Class1.FooProperty, "Bar"),
  82. },
  83. }
  84. };
  85. var target = new Class1();
  86. List<string> values = new List<string>();
  87. target.GetObservable(Class1.FooProperty).Subscribe(x => values.Add(x));
  88. styles.Attach(target);
  89. target.Classes.Add("foo");
  90. target.Classes.Remove("foo");
  91. Assert.Equal(new[] { "foodefault", "Foo", "Bar", "foodefault" }, values);
  92. }
  93. [Fact]
  94. public void Style_With_Value_And_Source_Should_Throw_Exception()
  95. {
  96. var source = new BehaviorSubject<string>("Foo");
  97. Style style = new Style(x => x.OfType<Class1>())
  98. {
  99. Setters = new[]
  100. {
  101. new Setter
  102. {
  103. Property = Class1.FooProperty,
  104. Source = source,
  105. Value = "Foo",
  106. },
  107. },
  108. };
  109. var target = new Class1();
  110. Assert.Throws<InvalidOperationException>(() => style.Attach(target));
  111. }
  112. [Fact]
  113. public void Style_With_Source_Should_Update_Value()
  114. {
  115. var source = new BehaviorSubject<string>("Foo");
  116. Style style = new Style(x => x.OfType<Class1>())
  117. {
  118. Setters = new[]
  119. {
  120. new Setter(Class1.FooProperty, source),
  121. },
  122. };
  123. var target = new Class1();
  124. style.Attach(target);
  125. Assert.Equal("Foo", target.Foo);
  126. source.OnNext("Bar");
  127. Assert.Equal("Bar", target.Foo);
  128. }
  129. [Fact]
  130. public void Style_With_Source_Should_Update_And_Restore_Value()
  131. {
  132. var source = new BehaviorSubject<string>("Foo");
  133. Style style = new Style(x => x.OfType<Class1>().Class("foo"))
  134. {
  135. Setters = new[]
  136. {
  137. new Setter(Class1.FooProperty, source),
  138. },
  139. };
  140. var target = new Class1();
  141. style.Attach(target);
  142. Assert.Equal("foodefault", target.Foo);
  143. target.Classes.Add("foo");
  144. Assert.Equal("Foo", target.Foo);
  145. source.OnNext("Bar");
  146. Assert.Equal("Bar", target.Foo);
  147. target.Classes.Remove("foo");
  148. Assert.Equal("foodefault", target.Foo);
  149. }
  150. private class Class1 : Control
  151. {
  152. public static readonly PerspexProperty<string> FooProperty =
  153. PerspexProperty.Register<Class1, string>("Foo", "foodefault");
  154. public string Foo
  155. {
  156. get { return this.GetValue(FooProperty); }
  157. set { this.SetValue(FooProperty, value); }
  158. }
  159. protected override Size MeasureOverride(Size availableSize)
  160. {
  161. throw new NotImplementedException();
  162. }
  163. }
  164. }
  165. }