PopupRootTests.cs 14 KB

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