PopupTests.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using Moq;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Primitives.PopupPositioning;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.Layout;
  11. using Avalonia.LogicalTree;
  12. using Avalonia.Platform;
  13. using Avalonia.Styling;
  14. using Avalonia.UnitTests;
  15. using Avalonia.VisualTree;
  16. using Xunit;
  17. using Avalonia.Input;
  18. using Avalonia.Rendering;
  19. using System.Threading.Tasks;
  20. using Avalonia.Threading;
  21. using Avalonia.Interactivity;
  22. namespace Avalonia.Controls.UnitTests.Primitives
  23. {
  24. public class PopupTests
  25. {
  26. protected bool UsePopupHost;
  27. [Fact]
  28. public void Popup_Open_Without_Target_Should_Attach_Itself_Later()
  29. {
  30. using (CreateServices())
  31. {
  32. int openedEvent = 0;
  33. var target = new Popup();
  34. target.Opened += (s, a) => openedEvent++;
  35. target.IsOpen = true;
  36. var window = PreparedWindow(target);
  37. window.Show();
  38. Assert.Equal(1, openedEvent);
  39. }
  40. }
  41. [Fact]
  42. public void Popup_Without_TopLevel_Shouldnt_Call_Open()
  43. {
  44. int openedEvent = 0;
  45. var target = new Popup();
  46. target.Opened += (s, a) => openedEvent++;
  47. target.IsOpen = true;
  48. Assert.Equal(0, openedEvent);
  49. }
  50. [Fact]
  51. public void Opening_Popup_Shouldnt_Throw_When_Not_In_Visual_Tree()
  52. {
  53. var target = new Popup();
  54. target.IsOpen = true;
  55. }
  56. [Fact]
  57. public void Opening_Popup_Shouldnt_Throw_When_In_Tree_Without_TopLevel()
  58. {
  59. Control c = new Control();
  60. var target = new Popup();
  61. ((ISetLogicalParent)target).SetParent(c);
  62. target.IsOpen = true;
  63. }
  64. [Fact]
  65. public void Setting_Child_Should_Set_Child_Controls_LogicalParent()
  66. {
  67. var target = new Popup();
  68. var child = new Control();
  69. target.Child = child;
  70. Assert.Equal(child.Parent, target);
  71. Assert.Equal(((ILogical)child).LogicalParent, target);
  72. }
  73. [Fact]
  74. public void Clearing_Child_Should_Clear_Child_Controls_Parent()
  75. {
  76. var target = new Popup();
  77. var child = new Control();
  78. target.Child = child;
  79. target.Child = null;
  80. Assert.Null(child.Parent);
  81. Assert.Null(((ILogical)child).LogicalParent);
  82. }
  83. [Fact]
  84. public void Child_Control_Should_Appear_In_LogicalChildren()
  85. {
  86. var target = new Popup();
  87. var child = new Control();
  88. target.Child = child;
  89. Assert.Equal(new[] { child }, target.GetLogicalChildren());
  90. }
  91. [Fact]
  92. public void Clearing_Child_Should_Remove_From_LogicalChildren()
  93. {
  94. var target = new Popup();
  95. var child = new Control();
  96. target.Child = child;
  97. target.Child = null;
  98. Assert.Equal(new ILogical[0], ((ILogical)target).LogicalChildren.ToList());
  99. }
  100. [Fact]
  101. public void Setting_Child_Should_Fire_LogicalChildren_CollectionChanged()
  102. {
  103. var target = new Popup();
  104. var child = new Control();
  105. var called = false;
  106. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  107. called = e.Action == NotifyCollectionChangedAction.Add;
  108. target.Child = child;
  109. Assert.True(called);
  110. }
  111. [Fact]
  112. public void Clearing_Child_Should_Fire_LogicalChildren_CollectionChanged()
  113. {
  114. var target = new Popup();
  115. var child = new Control();
  116. var called = false;
  117. target.Child = child;
  118. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
  119. called = e.Action == NotifyCollectionChangedAction.Remove;
  120. target.Child = null;
  121. Assert.True(called);
  122. }
  123. [Fact]
  124. public void Changing_Child_Should_Fire_LogicalChildren_CollectionChanged()
  125. {
  126. var target = new Popup();
  127. var child1 = new Control();
  128. var child2 = new Control();
  129. var called = false;
  130. target.Child = child1;
  131. ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
  132. target.Child = child2;
  133. Assert.True(called);
  134. }
  135. [Fact]
  136. public void Setting_Child_Should_Not_Set_Childs_VisualParent()
  137. {
  138. var target = new Popup();
  139. var child = new Control();
  140. target.Child = child;
  141. Assert.Null(((Visual)child).VisualParent);
  142. }
  143. [Fact]
  144. public void PopupRoot_Should_Initially_Be_Null()
  145. {
  146. using (CreateServices())
  147. {
  148. var target = new Popup();
  149. Assert.Null(((Visual)target.Host));
  150. }
  151. }
  152. [Fact]
  153. public void PopupRoot_Should_Have_Popup_As_LogicalParent()
  154. {
  155. using (CreateServices())
  156. {
  157. var target = new Popup() {PlacementTarget = PreparedWindow()};
  158. target.Open();
  159. Assert.Equal(target, ((Visual)target.Host).Parent);
  160. Assert.Equal(target, ((Visual)target.Host).GetLogicalParent());
  161. }
  162. }
  163. [Fact]
  164. public void PopupRoot_Should_Be_Detached_From_Logical_Tree_When_Popup_Is_Detached()
  165. {
  166. using (CreateServices())
  167. {
  168. var target = new Popup() {Placement = PlacementMode.Pointer};
  169. var root = PreparedWindow(target);
  170. target.Open();
  171. var popupRoot = (ILogical)((Visual)target.Host);
  172. Assert.True(popupRoot.IsAttachedToLogicalTree);
  173. root.Content = null;
  174. Assert.False(((ILogical)target).IsAttachedToLogicalTree);
  175. }
  176. }
  177. [Fact]
  178. public void Popup_Open_Should_Raise_Single_Opened_Event()
  179. {
  180. using (CreateServices())
  181. {
  182. var window = PreparedWindow();
  183. var target = new Popup() {Placement = PlacementMode.Pointer};
  184. window.Content = target;
  185. int openedCount = 0;
  186. target.Opened += (sender, args) =>
  187. {
  188. openedCount++;
  189. };
  190. target.Open();
  191. Assert.Equal(1, openedCount);
  192. }
  193. }
  194. [Fact]
  195. public void Popup_Close_Should_Raise_Single_Closed_Event()
  196. {
  197. using (CreateServices())
  198. {
  199. var window = PreparedWindow();
  200. var target = new Popup() {Placement = PlacementMode.Pointer};
  201. window.Content = target;
  202. window.ApplyTemplate();
  203. target.Open();
  204. int closedCount = 0;
  205. target.Closed += (sender, args) =>
  206. {
  207. closedCount++;
  208. };
  209. target.Close();
  210. Assert.Equal(1, closedCount);
  211. }
  212. }
  213. [Fact]
  214. public void Popup_Close_On_Closed_Popup_Should_Not_Raise_Closed_Event()
  215. {
  216. using (CreateServices())
  217. {
  218. var window = PreparedWindow();
  219. var target = new Popup() { Placement = PlacementMode.Pointer };
  220. window.Content = target;
  221. window.ApplyTemplate();
  222. int closedCount = 0;
  223. target.Closed += (sender, args) =>
  224. {
  225. closedCount++;
  226. };
  227. target.Close();
  228. target.Close();
  229. target.Close();
  230. target.Close();
  231. Assert.Equal(0, closedCount);
  232. }
  233. }
  234. [Fact]
  235. public void ContentControl_With_Popup_In_Template_Should_Set_TemplatedParent()
  236. {
  237. // Test uses OverlayPopupHost default template
  238. using (CreateServices())
  239. {
  240. PopupContentControl target;
  241. var root = PreparedWindow(target = new PopupContentControl
  242. {
  243. Content = new Border(),
  244. Template = new FuncControlTemplate<PopupContentControl>(PopupContentControlTemplate),
  245. });
  246. root.Show();
  247. target.ApplyTemplate();
  248. var popup = (Popup)target.GetTemplateChildren().First(x => x.Name == "popup");
  249. popup.Open();
  250. var popupRoot = (Control)popup.Host;
  251. popupRoot.Measure(Size.Infinity);
  252. popupRoot.Arrange(new Rect(popupRoot.DesiredSize));
  253. var children = popupRoot.GetVisualDescendants().ToList();
  254. var types = children.Select(x => x.GetType().Name).ToList();
  255. if (UsePopupHost)
  256. {
  257. Assert.Equal(
  258. new[]
  259. {
  260. "LayoutTransformControl",
  261. "VisualLayerManager",
  262. "ContentPresenter",
  263. "ContentPresenter",
  264. "Border",
  265. },
  266. types);
  267. }
  268. else
  269. {
  270. Assert.Equal(
  271. new[]
  272. {
  273. "LayoutTransformControl",
  274. "Panel",
  275. "Border",
  276. "VisualLayerManager",
  277. "ContentPresenter",
  278. "ContentPresenter",
  279. "Border",
  280. },
  281. types);
  282. }
  283. var templatedParents = children
  284. .OfType<Control>()
  285. .Select(x => x.TemplatedParent).ToList();
  286. if (UsePopupHost)
  287. {
  288. Assert.Equal(
  289. new object[]
  290. {
  291. popupRoot,
  292. popupRoot,
  293. popupRoot,
  294. target,
  295. null,
  296. },
  297. templatedParents);
  298. }
  299. else
  300. {
  301. Assert.Equal(
  302. new object[]
  303. {
  304. popupRoot,
  305. popupRoot,
  306. popupRoot,
  307. popupRoot,
  308. popupRoot,
  309. target,
  310. null,
  311. },
  312. templatedParents);
  313. }
  314. }
  315. }
  316. [Fact]
  317. public void ItemsControl_With_Popup_In_Template_Should_Set_TemplatedParent()
  318. {
  319. // Test uses OverlayPopupHost default template
  320. using (CreateServices())
  321. {
  322. PopupItemsControl target;
  323. var item = new Border();
  324. var root = PreparedWindow(target = new PopupItemsControl
  325. {
  326. Items = { item },
  327. Template = new FuncControlTemplate<PopupItemsControl>(PopupItemsControlTemplate),
  328. }); ;
  329. root.Show();
  330. target.ApplyTemplate();
  331. var popup = (Popup)target.GetTemplateChildren().First(x => x.Name == "popup");
  332. popup.Open();
  333. var popupRoot = (Control)popup.Host;
  334. popupRoot.Measure(Size.Infinity);
  335. popupRoot.Arrange(new Rect(popupRoot.DesiredSize));
  336. var children = popupRoot.GetVisualDescendants().ToList();
  337. var types = children.Select(x => x.GetType().Name).ToList();
  338. if (UsePopupHost)
  339. {
  340. Assert.Equal(
  341. new[]
  342. {
  343. "LayoutTransformControl",
  344. "VisualLayerManager",
  345. "ContentPresenter",
  346. "ItemsPresenter",
  347. "StackPanel",
  348. "Border",
  349. },
  350. types);
  351. }
  352. else
  353. {
  354. Assert.Equal(
  355. new[]
  356. {
  357. "LayoutTransformControl",
  358. "Panel",
  359. "Border",
  360. "VisualLayerManager",
  361. "ContentPresenter",
  362. "ItemsPresenter",
  363. "StackPanel",
  364. "Border",
  365. },
  366. types);
  367. }
  368. var templatedParents = children
  369. .OfType<Control>()
  370. .Select(x => x.TemplatedParent).ToList();
  371. if (UsePopupHost)
  372. {
  373. Assert.Equal(
  374. new object[]
  375. {
  376. popupRoot,
  377. popupRoot,
  378. popupRoot,
  379. target,
  380. target,
  381. null,
  382. },
  383. templatedParents);
  384. }
  385. else
  386. {
  387. Assert.Equal(
  388. new object[]
  389. {
  390. popupRoot,
  391. popupRoot,
  392. popupRoot,
  393. popupRoot,
  394. popupRoot,
  395. target,
  396. target,
  397. null,
  398. },
  399. templatedParents);
  400. }
  401. }
  402. }
  403. [Fact]
  404. public void Should_Not_Overwrite_TemplatedParent_Of_Item_In_ItemsControl_With_Popup_On_Second_Open()
  405. {
  406. // Test uses OverlayPopupHost default template
  407. using (CreateServices())
  408. {
  409. PopupItemsControl target;
  410. var item = new Border();
  411. var root = PreparedWindow(target = new PopupItemsControl
  412. {
  413. Items = { item },
  414. Template = new FuncControlTemplate<PopupItemsControl>(PopupItemsControlTemplate),
  415. });
  416. root.Show();
  417. target.ApplyTemplate();
  418. var popup = (Popup)target.GetTemplateChildren().First(x => x.Name == "popup");
  419. popup.Open();
  420. var popupRoot = (Control)popup.Host;
  421. popupRoot.Measure(Size.Infinity);
  422. popupRoot.Arrange(new Rect(popupRoot.DesiredSize));
  423. Assert.Null(item.TemplatedParent);
  424. popup.Close();
  425. popup.Open();
  426. Assert.Null(item.TemplatedParent);
  427. }
  428. }
  429. [Fact]
  430. public void DataContextBeginUpdate_Should_Not_Be_Called_For_Controls_That_Dont_Inherit()
  431. {
  432. using (CreateServices())
  433. {
  434. TestControl child;
  435. var popup = new Popup
  436. {
  437. Child = child = new TestControl(),
  438. DataContext = "foo",
  439. PlacementTarget = PreparedWindow()
  440. };
  441. var beginCalled = false;
  442. child.DataContextBeginUpdate += (s, e) => beginCalled = true;
  443. // Test for #1245. Here, the child's logical parent is the popup but it's not yet
  444. // attached to a visual tree because the popup hasn't been opened.
  445. Assert.Same(popup, ((ILogical)child).LogicalParent);
  446. Assert.Same(popup, child.InheritanceParent);
  447. Assert.Null(child.GetVisualRoot());
  448. popup.Open();
  449. // #1245 was caused by the fact that DataContextBeginUpdate was called on `target`
  450. // when the PopupRoot was created, even though PopupRoot isn't the
  451. // InheritanceParent of child.
  452. Assert.False(beginCalled);
  453. }
  454. }
  455. [Fact]
  456. public void Popup_Host_Type_Should_Match_Platform_Preference()
  457. {
  458. using (CreateServices())
  459. {
  460. var target = new Popup() {PlacementTarget = PreparedWindow()};
  461. target.Open();
  462. if (UsePopupHost)
  463. Assert.IsType<OverlayPopupHost>(target.Host);
  464. else
  465. Assert.IsType<PopupRoot>(target.Host);
  466. }
  467. }
  468. [Fact]
  469. public void OverlayDismissEventPassThrough_Should_Pass_Event_To_Window_Contents()
  470. {
  471. using (CreateServices())
  472. {
  473. var renderer = RendererMocks.CreateRenderer();
  474. var platform = AvaloniaLocator.Current.GetRequiredService<IWindowingPlatform>();
  475. var windowImpl = Mock.Get(platform.CreateWindow());
  476. windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer.Object);
  477. var window = new Window(windowImpl.Object);
  478. window.ApplyStyling();
  479. window.ApplyTemplate();
  480. var target = new Popup()
  481. {
  482. PlacementTarget = window ,
  483. IsLightDismissEnabled = true,
  484. OverlayDismissEventPassThrough = true,
  485. };
  486. var raised = 0;
  487. var border = new Border();
  488. window.Content = border;
  489. renderer.Setup(x =>
  490. x.HitTestFirst(new Point(10, 15), window, It.IsAny<Func<Visual, bool>>()))
  491. .Returns(border);
  492. border.PointerPressed += (s, e) =>
  493. {
  494. Assert.Same(border, e.Source);
  495. ++raised;
  496. };
  497. target.Open();
  498. Assert.True(target.IsOpen);
  499. var e = CreatePointerPressedEventArgs(window, new Point(10, 15));
  500. var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
  501. overlay.RaiseEvent(e);
  502. Assert.Equal(1, raised);
  503. Assert.False(target.IsOpen);
  504. }
  505. }
  506. [Fact]
  507. public void Focusable_Controls_In_Popup_Should_Get_Focus()
  508. {
  509. using (CreateServicesWithFocus())
  510. {
  511. var window = PreparedWindow();
  512. var tb = new TextBox();
  513. var b = new Button();
  514. var p = new Popup
  515. {
  516. PlacementTarget = window,
  517. Child = new StackPanel
  518. {
  519. Children =
  520. {
  521. tb,
  522. b
  523. }
  524. }
  525. };
  526. ((ISetLogicalParent)p).SetParent(p.PlacementTarget);
  527. window.Show();
  528. p.Open();
  529. if(p.Host is OverlayPopupHost host)
  530. {
  531. //Need to measure/arrange for visual children to show up
  532. //in OverlayPopupHost
  533. host.Measure(Size.Infinity);
  534. host.Arrange(new Rect(host.DesiredSize));
  535. }
  536. tb.Focus();
  537. Assert.True(FocusManager.Instance?.Current == tb);
  538. //Ensure focus remains in the popup
  539. var nextFocus = KeyboardNavigationHandler.GetNext(FocusManager.Instance.Current, NavigationDirection.Next);
  540. Assert.True(nextFocus == b);
  541. p.Close();
  542. }
  543. }
  544. [Fact]
  545. public void Closing_Popup_Sets_Focus_On_PlacementTarget()
  546. {
  547. using (CreateServicesWithFocus())
  548. {
  549. var window = PreparedWindow();
  550. window.Focusable = true;
  551. var tb = new TextBox();
  552. var p = new Popup
  553. {
  554. PlacementTarget = window,
  555. Child = tb
  556. };
  557. ((ISetLogicalParent)p).SetParent(p.PlacementTarget);
  558. window.Show();
  559. p.Open();
  560. if (p.Host is OverlayPopupHost host)
  561. {
  562. //Need to measure/arrange for visual children to show up
  563. //in OverlayPopupHost
  564. host.Measure(Size.Infinity);
  565. host.Arrange(new Rect(host.DesiredSize));
  566. }
  567. tb.Focus();
  568. p.Close();
  569. var focus = FocusManager.Instance?.Current;
  570. Assert.True(focus == window);
  571. }
  572. }
  573. [Fact]
  574. public void Prog_Close_Popup_NoLightDismiss_Doesnt_Move_Focus_To_PlacementTarget()
  575. {
  576. using (CreateServicesWithFocus())
  577. {
  578. var window = PreparedWindow();
  579. var windowTB = new TextBox();
  580. window.Content = windowTB;
  581. var popupTB = new TextBox();
  582. var p = new Popup
  583. {
  584. PlacementTarget = window,
  585. IsLightDismissEnabled = false,
  586. Child = popupTB
  587. };
  588. ((ISetLogicalParent)p).SetParent(p.PlacementTarget);
  589. window.Show();
  590. p.Open();
  591. if (p.Host is OverlayPopupHost host)
  592. {
  593. //Need to measure/arrange for visual children to show up
  594. //in OverlayPopupHost
  595. host.Measure(Size.Infinity);
  596. host.Arrange(new Rect(host.DesiredSize));
  597. }
  598. popupTB.Focus();
  599. windowTB.Focus();
  600. var focus = FocusManager.Instance?.Current;
  601. Assert.True(focus == windowTB);
  602. p.Close();
  603. Assert.True(focus == windowTB);
  604. }
  605. }
  606. [Fact]
  607. public void Popup_Should_Not_Follow_Placement_Target_On_Window_Move_If_Pointer()
  608. {
  609. using (CreateServices())
  610. {
  611. var popup = new Popup
  612. {
  613. Width = 400,
  614. Height = 200,
  615. Placement = PlacementMode.Pointer
  616. };
  617. var window = PreparedWindow(popup);
  618. window.Show();
  619. popup.Open();
  620. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  621. var raised = false;
  622. if (popup.Host is PopupRoot popupRoot)
  623. {
  624. popupRoot.PositionChanged += (_, args) =>
  625. {
  626. raised = true;
  627. };
  628. }
  629. else if (popup.Host is OverlayPopupHost overlayPopupHost)
  630. {
  631. overlayPopupHost.PropertyChanged += (_, args) =>
  632. {
  633. if (args.Property == Canvas.TopProperty
  634. || args.Property == Canvas.LeftProperty)
  635. {
  636. raised = true;
  637. }
  638. };
  639. }
  640. window.Position = new PixelPoint(10, 10);
  641. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  642. Assert.False(raised);
  643. }
  644. }
  645. [Fact]
  646. public void Popup_Should_Follow_Placement_Target_On_Window_Resize()
  647. {
  648. using (CreateServices())
  649. {
  650. var placementTarget = new Panel()
  651. {
  652. Width = 10,
  653. Height = 10,
  654. HorizontalAlignment = HorizontalAlignment.Center,
  655. VerticalAlignment = VerticalAlignment.Center
  656. };
  657. var popup = new Popup()
  658. {
  659. PlacementTarget = placementTarget,
  660. Placement = PlacementMode.Bottom,
  661. Width = 10,
  662. Height = 10
  663. };
  664. ((ISetLogicalParent)popup).SetParent(popup.PlacementTarget);
  665. var window = PreparedWindow(placementTarget);
  666. window.Show();
  667. popup.Open();
  668. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  669. // The target's initial placement is (395,295) which is a 10x10 panel centered in a 800x600 window
  670. Assert.Equal(placementTarget.Bounds, new Rect(395D, 295D, 10, 10));
  671. var raised = false;
  672. // Resizing the window to 700x500 must move the popup to (345,255) as this is the new
  673. // location of the placement target
  674. if (popup.Host is PopupRoot popupRoot)
  675. {
  676. popupRoot.PositionChanged += (_, args) =>
  677. {
  678. Assert.Equal(new PixelPoint(345, 255), args.Point);
  679. raised = true;
  680. };
  681. }
  682. else if (popup.Host is OverlayPopupHost overlayPopupHost)
  683. {
  684. overlayPopupHost.PropertyChanged += (_, args) =>
  685. {
  686. if ((args.Property == Canvas.TopProperty
  687. || args.Property == Canvas.LeftProperty)
  688. && Canvas.GetLeft(overlayPopupHost) == 345
  689. && Canvas.GetTop(overlayPopupHost) == 255)
  690. {
  691. raised = true;
  692. }
  693. };
  694. }
  695. window.PlatformImpl?.Resize(new Size(700D, 500D), PlatformResizeReason.Unspecified);
  696. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  697. Assert.True(raised);
  698. }
  699. }
  700. [Fact]
  701. public void Popup_Should_Not_Follow_Placement_Target_On_Window_Resize_If_Pointer_If_Pointer()
  702. {
  703. using (CreateServices())
  704. {
  705. var placementTarget = new Panel()
  706. {
  707. Width = 10,
  708. Height = 10,
  709. HorizontalAlignment = HorizontalAlignment.Center,
  710. VerticalAlignment = VerticalAlignment.Center
  711. };
  712. var popup = new Popup()
  713. {
  714. PlacementTarget = placementTarget,
  715. Placement = PlacementMode.Pointer,
  716. Width = 10,
  717. Height = 10
  718. };
  719. ((ISetLogicalParent)popup).SetParent(popup.PlacementTarget);
  720. var window = PreparedWindow(placementTarget);
  721. window.Show();
  722. popup.Open();
  723. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  724. // The target's initial placement is (395,295) which is a 10x10 panel centered in a 800x600 window
  725. Assert.Equal(placementTarget.Bounds, new Rect(395D, 295D, 10, 10));
  726. var raised = false;
  727. if (popup.Host is PopupRoot popupRoot)
  728. {
  729. popupRoot.PositionChanged += (_, args) =>
  730. {
  731. raised = true;
  732. };
  733. }
  734. else if (popup.Host is OverlayPopupHost overlayPopupHost)
  735. {
  736. overlayPopupHost.PropertyChanged += (_, args) =>
  737. {
  738. if (args.Property == Canvas.TopProperty
  739. || args.Property == Canvas.LeftProperty)
  740. {
  741. raised = true;
  742. }
  743. };
  744. }
  745. window.PlatformImpl?.Resize(new Size(700D, 500D), PlatformResizeReason.Unspecified);
  746. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  747. Assert.False(raised);
  748. }
  749. }
  750. [Fact]
  751. public void Popup_Should_Follow_Placement_Target_On_Target_Moved()
  752. {
  753. using (CreateServices())
  754. {
  755. var placementTarget = new Panel()
  756. {
  757. Width = 10,
  758. Height = 10,
  759. HorizontalAlignment = HorizontalAlignment.Center,
  760. VerticalAlignment = VerticalAlignment.Center
  761. };
  762. var popup = new Popup()
  763. {
  764. PlacementTarget = placementTarget,
  765. Placement = PlacementMode.Bottom,
  766. Width = 10,
  767. Height = 10
  768. };
  769. ((ISetLogicalParent)popup).SetParent(popup.PlacementTarget);
  770. var window = PreparedWindow(placementTarget);
  771. window.Show();
  772. popup.Open();
  773. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  774. // The target's initial placement is (395,295) which is a 10x10 panel centered in a 800x600 window
  775. Assert.Equal(placementTarget.Bounds, new Rect(395D, 295D, 10, 10));
  776. var raised = false;
  777. // Margin will move placement target
  778. if (popup.Host is PopupRoot popupRoot)
  779. {
  780. popupRoot.PositionChanged += (_, args) =>
  781. {
  782. Assert.Equal(new PixelPoint(400, 305), args.Point);
  783. raised = true;
  784. };
  785. }
  786. else if (popup.Host is OverlayPopupHost overlayPopupHost)
  787. {
  788. overlayPopupHost.PropertyChanged += (_, args) =>
  789. {
  790. if ((args.Property == Canvas.TopProperty
  791. || args.Property == Canvas.LeftProperty)
  792. && Canvas.GetLeft(overlayPopupHost) == 400
  793. && Canvas.GetTop(overlayPopupHost) == 305)
  794. {
  795. raised = true;
  796. }
  797. };
  798. }
  799. placementTarget.Margin = new Thickness(10, 0, 0, 0);
  800. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  801. Assert.True(raised);
  802. }
  803. }
  804. [Fact]
  805. public void Popup_Should_Not_Follow_Placement_Target_On_Target_Moved_If_Pointer()
  806. {
  807. using (CreateServices())
  808. {
  809. var placementTarget = new Panel()
  810. {
  811. Width = 10,
  812. Height = 10,
  813. HorizontalAlignment = HorizontalAlignment.Center,
  814. VerticalAlignment = VerticalAlignment.Center
  815. };
  816. var popup = new Popup()
  817. {
  818. PlacementTarget = placementTarget,
  819. Placement = PlacementMode.Pointer,
  820. Width = 10,
  821. Height = 10
  822. };
  823. ((ISetLogicalParent)popup).SetParent(popup.PlacementTarget);
  824. var window = PreparedWindow(placementTarget);
  825. window.Show();
  826. popup.Open();
  827. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  828. // The target's initial placement is (395,295) which is a 10x10 panel centered in a 800x600 window
  829. Assert.Equal(placementTarget.Bounds, new Rect(395D, 295D, 10, 10));
  830. var raised = false;
  831. if (popup.Host is PopupRoot popupRoot)
  832. {
  833. popupRoot.PositionChanged += (_, args) =>
  834. {
  835. raised = true;
  836. };
  837. }
  838. else if (popup.Host is OverlayPopupHost overlayPopupHost)
  839. {
  840. overlayPopupHost.PropertyChanged += (_, args) =>
  841. {
  842. if (args.Property == Canvas.TopProperty
  843. || args.Property == Canvas.LeftProperty)
  844. {
  845. raised = true;
  846. }
  847. };
  848. }
  849. placementTarget.Margin = new Thickness(10, 0, 0, 0);
  850. Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout);
  851. Assert.False(raised);
  852. }
  853. }
  854. [Fact]
  855. public void Popup_Should_Follow_Popup_Root_Placement_Target()
  856. {
  857. // When the placement target of a popup is another popup (e.g. nested menu items), the child popup must
  858. // follow the parent popup if it moves (due to root window movement or resize)
  859. using (CreateServices())
  860. {
  861. // The child popup is placed directly over the parent popup for position testing
  862. var parentPopup = new Popup() { Width = 10, Height = 10 };
  863. var childPopup = new Popup() {
  864. Width = 20,
  865. Height = 20,
  866. PlacementTarget = parentPopup,
  867. Placement = PlacementMode.AnchorAndGravity,
  868. PlacementAnchor = PopupAnchor.TopLeft,
  869. PlacementGravity = PopupGravity.BottomRight
  870. };
  871. ((ISetLogicalParent)childPopup).SetParent(childPopup.PlacementTarget);
  872. var window = PreparedWindow(parentPopup);
  873. window.Show();
  874. parentPopup.Open();
  875. childPopup.Open();
  876. if (childPopup.Host is PopupRoot popupRoot)
  877. {
  878. var raised = false;
  879. popupRoot.PositionChanged += (_, args) =>
  880. {
  881. // The parent's initial placement is (395,295) which is a 10x10 popup centered
  882. // in a 800x600 window. When the window is moved, the child's final placement is (405, 305)
  883. // which is the parent's placement moved 10 pixels left and down.
  884. Assert.Equal(new PixelPoint(405, 305), args.Point);
  885. raised = true;
  886. };
  887. window.Position = new PixelPoint(10, 10);
  888. Assert.True(raised);
  889. }
  890. }
  891. }
  892. [Fact]
  893. public void Events_Should_Be_Routed_To_Popup_Parent()
  894. {
  895. using (CreateServices())
  896. {
  897. var popupContent = new Border();
  898. var popup = new Popup { Child = popupContent };
  899. var popupParent = new Border { Child = popup };
  900. var root = PreparedWindow(popupParent);
  901. var raised = 0;
  902. root.LayoutManager.ExecuteInitialLayoutPass();
  903. popup.Open();
  904. root.LayoutManager.ExecuteLayoutPass();
  905. var ev = new RoutedEventArgs(Button.ClickEvent);
  906. popupParent.AddHandler(Button.ClickEvent, (s, e) => ++raised);
  907. popupContent.RaiseEvent(ev);
  908. Assert.Equal(1, raised);
  909. }
  910. }
  911. private IDisposable CreateServices()
  912. {
  913. return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform:
  914. new MockWindowingPlatform(null,
  915. x =>
  916. {
  917. if(UsePopupHost)
  918. return null;
  919. return MockWindowingPlatform.CreatePopupMock(x).Object;
  920. })));
  921. }
  922. private IDisposable CreateServicesWithFocus()
  923. {
  924. return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform:
  925. new MockWindowingPlatform(null,
  926. x =>
  927. {
  928. if (UsePopupHost)
  929. return null;
  930. return MockWindowingPlatform.CreatePopupMock(x).Object;
  931. }),
  932. focusManager: new FocusManager(),
  933. keyboardDevice: () => new KeyboardDevice()));
  934. }
  935. private static PointerPressedEventArgs CreatePointerPressedEventArgs(Window source, Point p)
  936. {
  937. var pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
  938. return new PointerPressedEventArgs(
  939. source,
  940. pointer,
  941. source,
  942. p,
  943. 0,
  944. new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonPressed),
  945. KeyModifiers.None);
  946. }
  947. private static Window PreparedWindow(object content = null)
  948. {
  949. var w = new Window { Content = content };
  950. w.ApplyStyling();
  951. w.ApplyTemplate();
  952. return w;
  953. }
  954. private static Control PopupContentControlTemplate(PopupContentControl control, INameScope scope)
  955. {
  956. return new Popup
  957. {
  958. Name = "popup",
  959. PlacementTarget = control,
  960. Child = new ContentPresenter
  961. {
  962. [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
  963. }
  964. }.RegisterInNameScope(scope);
  965. }
  966. private static Control PopupItemsControlTemplate(PopupItemsControl control, INameScope scope)
  967. {
  968. return new Popup
  969. {
  970. Name = "popup",
  971. PlacementTarget = control,
  972. Child = new ItemsPresenter(),
  973. }.RegisterInNameScope(scope);
  974. }
  975. private class PopupContentControl : ContentControl
  976. {
  977. }
  978. private class PopupItemsControl : ItemsControl
  979. {
  980. }
  981. private class TestControl : Decorator
  982. {
  983. public event EventHandler DataContextBeginUpdate;
  984. public new AvaloniaObject InheritanceParent => base.InheritanceParent;
  985. protected override void OnDataContextBeginUpdate()
  986. {
  987. DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
  988. base.OnDataContextBeginUpdate();
  989. }
  990. }
  991. }
  992. public class PopupTestsWithPopupRoot : PopupTests
  993. {
  994. public PopupTestsWithPopupRoot()
  995. {
  996. UsePopupHost = true;
  997. }
  998. }
  999. }