PopupTests.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // -----------------------------------------------------------------------
  2. // <copyright file="PopupTests.cs" company="Steven Kirk">
  3. // Copyright 2013 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Controls.UnitTests.Primitives
  7. {
  8. using System;
  9. using System.Collections.Specialized;
  10. using System.Linq;
  11. using Layout;
  12. using Moq;
  13. using Perspex.Controls.Presenters;
  14. using Perspex.Controls.Primitives;
  15. using Perspex.LogicalTree;
  16. using Perspex.Platform;
  17. using Perspex.Styling;
  18. using Perspex.VisualTree;
  19. using Splat;
  20. using Templates;
  21. using Xunit;
  22. public class PopupTests
  23. {
  24. [Fact]
  25. public void Setting_Child_Should_Set_Child_Controls_LogicalParent()
  26. {
  27. var target = new Popup();
  28. var child = new Control();
  29. target.Child = child;
  30. Assert.Equal(child.Parent, target);
  31. Assert.Equal(((ILogical)child).LogicalParent, target);
  32. }
  33. [Fact]
  34. public void Clearing_Child_Should_Clear_Child_Controls_Parent()
  35. {
  36. var target = new Popup();
  37. var child = new Control();
  38. target.Child = child;
  39. target.Child = null;
  40. Assert.Null(child.Parent);
  41. Assert.Null(((ILogical)child).LogicalParent);
  42. }
  43. [Fact]
  44. public void Child_Control_Should_Appear_In_LogicalChildren()
  45. {
  46. var target = new Popup();
  47. var child = new Control();
  48. target.Child = child;
  49. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  50. }
  51. [Fact]
  52. public void Clearing_Child_Should_Remove_From_LogicalChildren()
  53. {
  54. var target = new Popup();
  55. var child = new Control();
  56. target.Child = child;
  57. target.Child = null;
  58. Assert.Equal(new ILogical[0], ((ILogical)target).LogicalChildren.ToList());
  59. }
  60. [Fact]
  61. public void Setting_Child_Should_Fire_LogicalChildren_CollectionChanged()
  62. {
  63. var target = new Popup();
  64. var child = new Control();
  65. var called = false;
  66. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  67. called = e.Action == NotifyCollectionChangedAction.Add;
  68. target.Child = child;
  69. Assert.True(called);
  70. }
  71. [Fact]
  72. public void Clearing_Child_Should_Fire_LogicalChildren_CollectionChanged()
  73. {
  74. var target = new Popup();
  75. var child = new Control();
  76. var called = false;
  77. target.Child = child;
  78. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  79. called = e.Action == NotifyCollectionChangedAction.Remove;
  80. target.Child = null;
  81. Assert.True(called);
  82. }
  83. [Fact]
  84. public void Changing_Child_Should_Fire_LogicalChildren_CollectionChanged()
  85. {
  86. var target = new Popup();
  87. var child1 = new Control();
  88. var child2 = new Control();
  89. var called = false;
  90. target.Child = child1;
  91. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  92. target.Child = child2;
  93. Assert.True(called);
  94. }
  95. [Fact]
  96. public void Setting_Child_Should_Not_Set_Childs_VisualParent()
  97. {
  98. var target = new Popup();
  99. var child = new Control();
  100. target.Child = child;
  101. Assert.Null(((IVisual)child).VisualParent);
  102. }
  103. [Fact]
  104. public void PopupRoot_Should_Initially_Be_Null()
  105. {
  106. using (CreateServices())
  107. {
  108. var target = new Popup();
  109. Assert.Null(target.PopupRoot);
  110. }
  111. }
  112. [Fact]
  113. public void PopupRoot_Should_Have_Null_VisualParent()
  114. {
  115. using (CreateServices())
  116. {
  117. var target = new Popup();
  118. target.Open();
  119. Assert.Null(target.PopupRoot.GetVisualParent());
  120. }
  121. }
  122. [Fact]
  123. public void PopupRoot_Should_Have_Popup_As_LogicalParent()
  124. {
  125. using (CreateServices())
  126. {
  127. var target = new Popup();
  128. target.Open();
  129. Assert.Equal(target, target.PopupRoot.Parent);
  130. Assert.Equal(target, target.PopupRoot.GetLogicalParent());
  131. }
  132. }
  133. [Fact]
  134. public void PopupRoot_Should_Have_Template_Applied()
  135. {
  136. using (CreateServices())
  137. {
  138. var target = new Popup();
  139. var child = new Control();
  140. target.Open();
  141. Assert.Equal(1, target.PopupRoot.GetVisualChildren().Count());
  142. var templatedChild = target.PopupRoot.GetVisualChildren().Single();
  143. Assert.IsType<ContentPresenter>(templatedChild);
  144. Assert.Equal(target.PopupRoot, ((IControl)templatedChild).TemplatedParent);
  145. }
  146. }
  147. [Fact]
  148. public void PopupRoot_Should_Have_Child_As_LogicalChild()
  149. {
  150. using (CreateServices())
  151. {
  152. var target = new Popup();
  153. var child = new Control();
  154. target.Child = child;
  155. target.Open();
  156. Assert.Equal(new[] { child }, target.PopupRoot.GetLogicalChildren());
  157. }
  158. }
  159. [Fact]
  160. public void Templated_Control_With_Popup_In_Template_Should_Set_TemplatedParent()
  161. {
  162. using (CreateServices())
  163. {
  164. PopupContentControl target;
  165. var root = new TestRoot
  166. {
  167. Child = target = new PopupContentControl
  168. {
  169. Content = new Border(),
  170. Template = new ControlTemplate<PopupContentControl>(PopupContentControlTemplate),
  171. }
  172. };
  173. target.ApplyTemplate();
  174. var popup = target.GetTemplateChild<Popup>("popup");
  175. popup.Open();
  176. var popupRoot = popup.PopupRoot;
  177. var children = popupRoot.GetVisualDescendents().ToList();
  178. var types = children.Select(x => x.GetType().Name).ToList();
  179. Assert.Equal(
  180. new[]
  181. {
  182. "ContentPresenter",
  183. "ContentPresenter",
  184. "Border",
  185. },
  186. types);
  187. var templatedParents = children
  188. .OfType<IControl>()
  189. .Select(x => x.TemplatedParent).ToList();
  190. Assert.Equal(
  191. new object[]
  192. {
  193. popupRoot,
  194. target,
  195. null,
  196. },
  197. templatedParents);
  198. }
  199. }
  200. private static IDisposable CreateServices()
  201. {
  202. var result = Locator.Current.WithResolver();
  203. var styles = new Styles
  204. {
  205. new Style(x => x.OfType<PopupRoot>())
  206. {
  207. Setters = new[]
  208. {
  209. new Setter(TemplatedControl.TemplateProperty, new ControlTemplate<PopupRoot>(PopupRootTemplate)),
  210. }
  211. },
  212. };
  213. var globalStyles = new Mock<IGlobalStyles>();
  214. globalStyles.Setup(x => x.Styles).Returns(styles);
  215. Locator.CurrentMutable.Register(() => globalStyles.Object, typeof(IGlobalStyles));
  216. Locator.CurrentMutable.Register(() => new LayoutManager(), typeof(ILayoutManager));
  217. Locator.CurrentMutable.Register(() => new Mock<IPlatformThreadingInterface>().Object, typeof(IPlatformThreadingInterface));
  218. Locator.CurrentMutable.Register(() => new Mock<IPopupImpl>().Object, typeof(IPopupImpl));
  219. Locator.CurrentMutable.Register(() => new Styler(), typeof(IStyler));
  220. return result;
  221. }
  222. private static IControl PopupRootTemplate(PopupRoot control)
  223. {
  224. return new ContentPresenter
  225. {
  226. Name = "contentPresenter",
  227. [~ContentPresenter.ContentProperty] = control[~PopupRoot.ContentProperty],
  228. };
  229. }
  230. private static IControl PopupContentControlTemplate(PopupContentControl control)
  231. {
  232. return new Popup
  233. {
  234. Name = "popup",
  235. Child = new ContentPresenter
  236. {
  237. [~ContentPresenter.ContentProperty] = control[~PopupRoot.ContentProperty],
  238. }
  239. };
  240. }
  241. private class PopupContentControl : ContentControl
  242. {
  243. }
  244. }
  245. }