PopupRootTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Primitives.PopupPositioning;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.LogicalTree;
  9. using Avalonia.Platform;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  13. using Moq;
  14. using Xunit;
  15. namespace Avalonia.Controls.UnitTests.Primitives
  16. {
  17. public class PopupRootTests
  18. {
  19. [Fact]
  20. public void PopupRoot_IsAttachedToLogicalTree_Is_True()
  21. {
  22. using (UnitTestApplication.Start(TestServices.StyledWindow))
  23. {
  24. var target = CreateTarget(new Window());
  25. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  26. }
  27. }
  28. [Fact]
  29. public void Templated_Child_IsAttachedToLogicalTree_Is_True()
  30. {
  31. using (UnitTestApplication.Start(TestServices.StyledWindow))
  32. {
  33. var target = CreateTarget(new Window());
  34. Assert.True(target.Presenter.IsAttachedToLogicalTree);
  35. }
  36. }
  37. [Fact]
  38. public void PopupRoot_StylingParent_Is_Popup()
  39. {
  40. using (UnitTestApplication.Start(TestServices.StyledWindow))
  41. {
  42. var window = new Window();
  43. var target = new TemplatedControlWithPopup
  44. {
  45. PopupContent = new Canvas(),
  46. };
  47. window.Content = target;
  48. window.ApplyTemplate();
  49. window.Presenter.ApplyTemplate();
  50. target.ApplyTemplate();
  51. target.Popup.Open();
  52. Assert.Equal(target.Popup, ((IStyleHost)target.Popup.Host).StylingParent);
  53. }
  54. }
  55. [Fact]
  56. public void PopupRoot_Should_Have_Template_Applied()
  57. {
  58. using (UnitTestApplication.Start(TestServices.StyledWindow))
  59. {
  60. var window = new Window();
  61. var target = new Popup {PlacementMode = PlacementMode.Pointer};
  62. var child = new Control();
  63. window.Content = target;
  64. window.ApplyTemplate();
  65. target.Open();
  66. Assert.Single(((Visual)target.Host).GetVisualChildren());
  67. var templatedChild = ((Visual)target.Host).GetVisualChildren().Single();
  68. Assert.IsType<LayoutTransformControl>(templatedChild);
  69. var panel = templatedChild.GetVisualChildren().Single();
  70. Assert.IsType<Panel>(panel);
  71. var visualLayerManager = panel.GetVisualChildren().Skip(1).Single();
  72. Assert.IsType<VisualLayerManager>(visualLayerManager);
  73. var contentPresenter = visualLayerManager.VisualChildren.Single();
  74. Assert.IsType<ContentPresenter>(contentPresenter);
  75. Assert.Equal((PopupRoot)target.Host, ((IControl)templatedChild).TemplatedParent);
  76. Assert.Equal((PopupRoot)target.Host, ((IControl)contentPresenter).TemplatedParent);
  77. }
  78. }
  79. [Fact]
  80. public void PopupRoot_Should_Have_Null_VisualParent()
  81. {
  82. using (UnitTestApplication.Start(TestServices.StyledWindow))
  83. {
  84. var target = new Popup() {PlacementTarget = new Window()};
  85. target.Open();
  86. Assert.Null(((Visual)target.Host).GetVisualParent());
  87. }
  88. }
  89. [Fact]
  90. public void Attaching_PopupRoot_To_Parent_Logical_Tree_Raises_DetachedFromLogicalTree_And_AttachedToLogicalTree()
  91. {
  92. using (UnitTestApplication.Start(TestServices.StyledWindow))
  93. {
  94. var child = new Decorator();
  95. var window = new Window();
  96. var target = CreateTarget(window);
  97. var detachedCount = 0;
  98. var attachedCount = 0;
  99. target.Content = child;
  100. target.DetachedFromLogicalTree += (s, e) => ++detachedCount;
  101. child.DetachedFromLogicalTree += (s, e) => ++detachedCount;
  102. target.AttachedToLogicalTree += (s, e) => ++attachedCount;
  103. child.AttachedToLogicalTree += (s, e) => ++attachedCount;
  104. ((ISetLogicalParent)target).SetParent(window);
  105. Assert.Equal(2, detachedCount);
  106. Assert.Equal(2, attachedCount);
  107. }
  108. }
  109. [Fact]
  110. public void Detaching_PopupRoot_From_Parent_Logical_Tree_Raises_DetachedFromLogicalTree_And_AttachedToLogicalTree()
  111. {
  112. using (UnitTestApplication.Start(TestServices.StyledWindow))
  113. {
  114. var child = new Decorator();
  115. var window = new Window();
  116. var target = CreateTarget(window);
  117. var detachedCount = 0;
  118. var attachedCount = 0;
  119. target.Content = child;
  120. ((ISetLogicalParent)target).SetParent(window);
  121. target.DetachedFromLogicalTree += (s, e) => ++detachedCount;
  122. child.DetachedFromLogicalTree += (s, e) => ++detachedCount;
  123. target.AttachedToLogicalTree += (s, e) => ++attachedCount;
  124. child.AttachedToLogicalTree += (s, e) => ++attachedCount;
  125. ((ISetLogicalParent)target).SetParent(null);
  126. // Despite being detached from the parent logical tree, we're still attached to a
  127. // logical tree as PopupRoot itself is a logical tree root.
  128. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  129. Assert.True(((ILogical)child).IsAttachedToLogicalTree);
  130. Assert.Equal(2, detachedCount);
  131. Assert.Equal(2, attachedCount);
  132. }
  133. }
  134. [Fact]
  135. public void Clearing_Content_Of_Popup_In_ControlTemplate_Doesnt_Crash()
  136. {
  137. using (UnitTestApplication.Start(TestServices.StyledWindow))
  138. {
  139. var window = new Window();
  140. var target = new TemplatedControlWithPopup
  141. {
  142. PopupContent = new Canvas(),
  143. };
  144. window.Content = target;
  145. window.ApplyTemplate();
  146. window.Presenter.ApplyTemplate();
  147. target.ApplyTemplate();
  148. target.Popup.Open();
  149. target.PopupContent = null;
  150. }
  151. }
  152. [Fact]
  153. public void Child_Should_Be_Measured_With_MaxAutoSizeHint()
  154. {
  155. using (UnitTestApplication.Start(TestServices.StyledWindow))
  156. {
  157. var child = new ChildControl();
  158. var window = new Window();
  159. var popupImpl = MockWindowingPlatform.CreatePopupMock(window.PlatformImpl);
  160. popupImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1200, 1000));
  161. var target = CreateTarget(window, popupImpl.Object);
  162. target.Content = child;
  163. target.Show();
  164. Assert.Equal(1, child.MeasureSizes.Count);
  165. Assert.Equal(new Size(1200, 1000), child.MeasureSizes[0]);
  166. }
  167. }
  168. [Fact]
  169. public void Child_Should_Be_Measured_With_Width_Height_When_Set()
  170. {
  171. using (UnitTestApplication.Start(TestServices.StyledWindow))
  172. {
  173. var child = new ChildControl();
  174. var window = new Window();
  175. var target = CreateTarget(window);
  176. target.Width = 500;
  177. target.Height = 600;
  178. target.Content = child;
  179. target.Show();
  180. Assert.Equal(1, child.MeasureSizes.Count);
  181. Assert.Equal(new Size(500, 600), child.MeasureSizes[0]);
  182. }
  183. }
  184. [Fact]
  185. public void Child_Should_Be_Measured_With_MaxWidth_MaxHeight_When_Set()
  186. {
  187. using (UnitTestApplication.Start(TestServices.StyledWindow))
  188. {
  189. var child = new ChildControl();
  190. var window = new Window();
  191. var target = CreateTarget(window);
  192. target.MaxWidth = 500;
  193. target.MaxHeight = 600;
  194. target.Content = child;
  195. target.Show();
  196. Assert.Equal(1, child.MeasureSizes.Count);
  197. Assert.Equal(new Size(500, 600), child.MeasureSizes[0]);
  198. }
  199. }
  200. [Fact]
  201. public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window_Size()
  202. {
  203. // Issue #3784.
  204. using (UnitTestApplication.Start(TestServices.StyledWindow))
  205. {
  206. var window = new Window();
  207. var popupImpl = MockWindowingPlatform.CreatePopupMock(window.PlatformImpl);
  208. var child = new Canvas
  209. {
  210. Width = 400,
  211. Height = 1344,
  212. };
  213. var target = CreateTarget(window, popupImpl.Object);
  214. target.Content = child;
  215. target.Show();
  216. Assert.Equal(new Size(400, 1024), target.Bounds.Size);
  217. // Issue #3784 causes this to be (0, 160) which makes no sense as Window has no
  218. // parent control to be offset against.
  219. Assert.Equal(new Point(0, 0), target.Bounds.Position);
  220. }
  221. }
  222. [Fact]
  223. public void MinWidth_MinHeight_Should_Be_Respected()
  224. {
  225. // Issue #3796
  226. using (UnitTestApplication.Start(TestServices.StyledWindow))
  227. {
  228. var window = new Window();
  229. var popupImpl = MockWindowingPlatform.CreatePopupMock(window.PlatformImpl);
  230. var target = CreateTarget(window, popupImpl.Object);
  231. target.MinWidth = 400;
  232. target.MinHeight = 800;
  233. target.Content = new Border
  234. {
  235. Width = 100,
  236. Height = 100,
  237. };
  238. target.Show();
  239. Assert.Equal(new Rect(0, 0, 400, 800), target.Bounds);
  240. Assert.Equal(new Size(400, 800), target.ClientSize);
  241. Assert.Equal(new Size(400, 800), target.PlatformImpl.ClientSize);
  242. }
  243. }
  244. [Fact]
  245. public void Setting_Width_Should_Resize_WindowImpl()
  246. {
  247. // Issue #3796
  248. using (UnitTestApplication.Start(TestServices.StyledWindow))
  249. {
  250. var window = new Window();
  251. var popupImpl = MockWindowingPlatform.CreatePopupMock(window.PlatformImpl);
  252. var positioner = new Mock<IPopupPositioner>();
  253. popupImpl.Setup(x => x.PopupPositioner).Returns(positioner.Object);
  254. var target = CreateTarget(window, popupImpl.Object);
  255. target.Width = 400;
  256. target.Height = 800;
  257. target.Show();
  258. Assert.Equal(400, target.Width);
  259. Assert.Equal(800, target.Height);
  260. target.Width = 410;
  261. target.LayoutManager.ExecuteLayoutPass();
  262. positioner.Verify(x =>
  263. x.Update(It.Is<PopupPositionerParameters>(x => x.Size.Width == 410)));
  264. Assert.Equal(410, target.Width);
  265. }
  266. }
  267. private static PopupRoot CreateTarget(TopLevel popupParent, IPopupImpl impl = null)
  268. {
  269. impl ??= popupParent.PlatformImpl.CreatePopup();
  270. var result = new PopupRoot(popupParent, impl)
  271. {
  272. Template = new FuncControlTemplate<PopupRoot>((parent, scope) =>
  273. new ContentPresenter
  274. {
  275. Name = "PART_ContentPresenter",
  276. [!ContentPresenter.ContentProperty] = parent[!PopupRoot.ContentProperty],
  277. }.RegisterInNameScope(scope)),
  278. };
  279. result.ApplyTemplate();
  280. return result;
  281. }
  282. private class TemplatedControlWithPopup : TemplatedControl
  283. {
  284. public static readonly StyledProperty<Control> PopupContentProperty =
  285. AvaloniaProperty.Register<TemplatedControlWithPopup, Control>(nameof(PopupContent));
  286. public TemplatedControlWithPopup()
  287. {
  288. Template = new FuncControlTemplate<TemplatedControlWithPopup>((parent, _) =>
  289. new Popup
  290. {
  291. [!Popup.ChildProperty] = parent[!TemplatedControlWithPopup.PopupContentProperty],
  292. PlacementTarget = parent
  293. });
  294. }
  295. public Popup Popup { get; private set; }
  296. public Control PopupContent
  297. {
  298. get => GetValue(PopupContentProperty);
  299. set => SetValue(PopupContentProperty, value);
  300. }
  301. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  302. {
  303. Popup = (Popup)this.GetVisualChildren().Single();
  304. }
  305. }
  306. private class ChildControl : Control
  307. {
  308. public List<Size> MeasureSizes { get; } = new List<Size>();
  309. protected override Size MeasureOverride(Size availableSize)
  310. {
  311. MeasureSizes.Add(availableSize);
  312. return base.MeasureOverride(availableSize);
  313. }
  314. }
  315. }
  316. }