ControlTests_Resources.cs 5.7 KB

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