StyledElementTests_Resources.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.Presenters;
  3. using Avalonia.Controls.Templates;
  4. using Avalonia.Styling;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Styling
  8. {
  9. public class StyledElementTests_Resources
  10. {
  11. [Fact]
  12. public void FindResource_Should_Find_Control_Resource()
  13. {
  14. var target = new StyledElement
  15. {
  16. Resources =
  17. {
  18. { "foo", "foo-value" },
  19. }
  20. };
  21. Assert.Equal("foo-value", target.FindResource("foo"));
  22. }
  23. [Fact]
  24. public void FindResource_Should_Find_Control_Resource_In_Parent()
  25. {
  26. Control target;
  27. var root = new Decorator
  28. {
  29. Resources =
  30. {
  31. { "foo", "foo-value" },
  32. },
  33. Child = target = new Control(),
  34. };
  35. Assert.Equal("foo-value", target.FindResource("foo"));
  36. }
  37. [Fact]
  38. public void FindResource_Should_Find_Application_Resource()
  39. {
  40. Control target;
  41. var app = new Application
  42. {
  43. Resources =
  44. {
  45. { "foo", "foo-value" },
  46. },
  47. };
  48. var root = new TestRoot
  49. {
  50. Child = target = new Control(),
  51. StylingParent = app,
  52. };
  53. Assert.Equal("foo-value", target.FindResource("foo"));
  54. }
  55. [Fact]
  56. public void FindResource_Should_Find_Style_Resource()
  57. {
  58. var target = new StyledElement
  59. {
  60. Styles =
  61. {
  62. new Style
  63. {
  64. Resources =
  65. {
  66. { "foo", "foo-value" },
  67. }
  68. }
  69. },
  70. Resources =
  71. {
  72. { "bar", "bar-value" },
  73. },
  74. };
  75. Assert.Equal("foo-value", target.FindResource("foo"));
  76. }
  77. [Fact]
  78. public void FindResource_Should_Find_Styles_Resource()
  79. {
  80. var target = new StyledElement
  81. {
  82. Styles =
  83. {
  84. new Styles
  85. {
  86. Resources =
  87. {
  88. { "foo", "foo-value" },
  89. }
  90. }
  91. },
  92. Resources =
  93. {
  94. { "bar", "bar-value" },
  95. },
  96. };
  97. Assert.Equal("foo-value", target.FindResource("foo"));
  98. }
  99. [Fact]
  100. public void FindResource_Should_Find_Application_Style_Resource()
  101. {
  102. Control target;
  103. var app = new Application
  104. {
  105. Styles =
  106. {
  107. new Style
  108. {
  109. Resources =
  110. {
  111. { "foo", "foo-value" },
  112. },
  113. }
  114. },
  115. Resources =
  116. {
  117. { "bar", "bar-value" },
  118. },
  119. };
  120. var root = new TestRoot
  121. {
  122. Child = target = new Control(),
  123. StylingParent = app,
  124. };
  125. Assert.Equal("foo-value", target.FindResource("foo"));
  126. }
  127. [Fact]
  128. public void Adding_Resource_Should_Call_Raise_ResourceChanged_On_Logical_Children()
  129. {
  130. Border child;
  131. var target = new ContentControl
  132. {
  133. Content = child = new Border(),
  134. Template = ContentControlTemplate(),
  135. };
  136. var raisedOnTarget = false;
  137. var raisedOnChild = false;
  138. target.Measure(Size.Infinity);
  139. target.ResourcesChanged += (_, __) => raisedOnTarget = true;
  140. child.ResourcesChanged += (_, __) => raisedOnChild = true;
  141. target.Resources.Add("foo", "bar");
  142. Assert.True(raisedOnTarget);
  143. Assert.True(raisedOnChild);
  144. }
  145. [Fact]
  146. public void Adding_Resource_To_Styles_Should_Raise_ResourceChanged()
  147. {
  148. var target = new Decorator();
  149. var raised = false;
  150. target.ResourcesChanged += (_, __) => raised = true;
  151. target.Styles.Resources.Add("foo", "bar");
  152. Assert.True(raised);
  153. }
  154. [Fact]
  155. public void Adding_Resource_To_Nested_Style_Should_Raise_ResourceChanged()
  156. {
  157. Style style;
  158. var target = new StyledElement
  159. {
  160. Styles =
  161. {
  162. (style = new Style()),
  163. }
  164. };
  165. var raised = false;
  166. target.ResourcesChanged += (_, __) => raised = true;
  167. style.Resources.Add("foo", "bar");
  168. Assert.True(raised);
  169. }
  170. private IControlTemplate ContentControlTemplate()
  171. {
  172. return new FuncControlTemplate<ContentControl>((x, scope) =>
  173. new ContentPresenter
  174. {
  175. Name = "PART_ContentPresenter",
  176. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  177. }.RegisterInNameScope(scope));
  178. }
  179. }
  180. }