| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System;
- using System.Collections.Specialized;
- using System.Diagnostics;
- using System.Linq;
- using Moq;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Templates;
- using Avalonia.Layout;
- using Avalonia.LogicalTree;
- using Avalonia.Platform;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Xunit;
- using Avalonia.Input;
- namespace Avalonia.Controls.UnitTests.Primitives
- {
- public class PopupTests
- {
- protected bool UsePopupHost;
-
- [Fact]
- public void Setting_Child_Should_Set_Child_Controls_LogicalParent()
- {
- var target = new Popup();
- var child = new Control();
- target.Child = child;
- Assert.Equal(child.Parent, target);
- Assert.Equal(((ILogical)child).LogicalParent, target);
- }
- [Fact]
- public void Clearing_Child_Should_Clear_Child_Controls_Parent()
- {
- var target = new Popup();
- var child = new Control();
- target.Child = child;
- target.Child = null;
- Assert.Null(child.Parent);
- Assert.Null(((ILogical)child).LogicalParent);
- }
- [Fact]
- public void Child_Control_Should_Appear_In_LogicalChildren()
- {
- var target = new Popup();
- var child = new Control();
- target.Child = child;
- Assert.Equal(new[] { child }, target.GetLogicalChildren());
- }
- [Fact]
- public void Clearing_Child_Should_Remove_From_LogicalChildren()
- {
- var target = new Popup();
- var child = new Control();
- target.Child = child;
- target.Child = null;
- Assert.Equal(new ILogical[0], ((ILogical)target).LogicalChildren.ToList());
- }
- [Fact]
- public void Setting_Child_Should_Fire_LogicalChildren_CollectionChanged()
- {
- var target = new Popup();
- var child = new Control();
- var called = false;
- ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
- called = e.Action == NotifyCollectionChangedAction.Add;
- target.Child = child;
- Assert.True(called);
- }
- [Fact]
- public void Clearing_Child_Should_Fire_LogicalChildren_CollectionChanged()
- {
- var target = new Popup();
- var child = new Control();
- var called = false;
- target.Child = child;
- ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
- called = e.Action == NotifyCollectionChangedAction.Remove;
- target.Child = null;
- Assert.True(called);
- }
- [Fact]
- public void Changing_Child_Should_Fire_LogicalChildren_CollectionChanged()
- {
- var target = new Popup();
- var child1 = new Control();
- var child2 = new Control();
- var called = false;
- target.Child = child1;
- ((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
- target.Child = child2;
- Assert.True(called);
- }
- [Fact]
- public void Setting_Child_Should_Not_Set_Childs_VisualParent()
- {
- var target = new Popup();
- var child = new Control();
- target.Child = child;
- Assert.Null(((IVisual)child).VisualParent);
- }
- [Fact]
- public void PopupRoot_Should_Initially_Be_Null()
- {
- using (CreateServices())
- {
- var target = new Popup();
- Assert.Null(((Visual)target.Host));
- }
- }
- [Fact]
- public void PopupRoot_Should_Have_Popup_As_LogicalParent()
- {
- using (CreateServices())
- {
- var target = new Popup() {PlacementTarget = PreparedWindow()};
- target.Open();
- Assert.Equal(target, ((Visual)target.Host).Parent);
- Assert.Equal(target, ((Visual)target.Host).GetLogicalParent());
- }
- }
- [Fact]
- public void PopupRoot_Should_Be_Detached_From_Logical_Tree_When_Popup_Is_Detached()
- {
- using (CreateServices())
- {
- var target = new Popup() {PlacementMode = PlacementMode.Pointer};
- var root = PreparedWindow(target);
- target.Open();
- var popupRoot = (ILogical)((Visual)target.Host);
- Assert.True(popupRoot.IsAttachedToLogicalTree);
- root.Content = null;
- Assert.False(((ILogical)target).IsAttachedToLogicalTree);
- }
- }
- [Fact]
- public void Popup_Open_Should_Raise_Single_Opened_Event()
- {
- using (CreateServices())
- {
- var window = PreparedWindow();
- var target = new Popup() {PlacementMode = PlacementMode.Pointer};
- window.Content = target;
- int openedCount = 0;
- target.Opened += (sender, args) =>
- {
- openedCount++;
- };
- target.Open();
- Assert.Equal(1, openedCount);
- }
- }
- [Fact]
- public void Popup_Close_Should_Raise_Single_Closed_Event()
- {
- using (CreateServices())
- {
- var window = PreparedWindow();
- var target = new Popup() {PlacementMode = PlacementMode.Pointer};
- window.Content = target;
- window.ApplyTemplate();
- target.Open();
- int closedCount = 0;
- target.Closed += (sender, args) =>
- {
- closedCount++;
- };
- target.Close();
- Assert.Equal(1, closedCount);
- }
- }
- [Fact]
- public void Popup_Close_On_Closed_Popup_Should_Not_Raise_Closed_Event()
- {
- using (CreateServices())
- {
- var window = PreparedWindow();
- var target = new Popup() { PlacementMode = PlacementMode.Pointer };
- window.Content = target;
- window.ApplyTemplate();
-
- int closedCount = 0;
- target.Closed += (sender, args) =>
- {
- closedCount++;
- };
- target.Close();
- target.Close();
- target.Close();
- target.Close();
- Assert.Equal(0, closedCount);
- }
- }
- [Fact]
- public void Templated_Control_With_Popup_In_Template_Should_Set_TemplatedParent()
- {
- using (CreateServices())
- {
- PopupContentControl target;
- var root = PreparedWindow(target = new PopupContentControl
- {
- Content = new Border(),
- Template = new FuncControlTemplate<PopupContentControl>(PopupContentControlTemplate),
- });
- root.Show();
- target.ApplyTemplate();
- var popup = (Popup)target.GetTemplateChildren().First(x => x.Name == "popup");
- popup.Open();
- var popupRoot = (Control)popup.Host;
- popupRoot.Measure(Size.Infinity);
- popupRoot.Arrange(new Rect(popupRoot.DesiredSize));
- var children = popupRoot.GetVisualDescendants().ToList();
- var types = children.Select(x => x.GetType().Name).ToList();
- Assert.Equal(
- new[]
- {
- "VisualLayerManager",
- "ContentPresenter",
- "ContentPresenter",
- "Border",
- },
- types);
- var templatedParents = children
- .OfType<IControl>()
- .Select(x => x.TemplatedParent).ToList();
- Assert.Equal(
- new object[]
- {
- popupRoot,
- popupRoot,
- target,
- null,
- },
- templatedParents);
- }
- }
- Window PreparedWindow(object content = null)
- {
- var w = new Window {Content = content};
- w.ApplyTemplate();
- return w;
- }
- [Fact]
- public void DataContextBeginUpdate_Should_Not_Be_Called_For_Controls_That_Dont_Inherit()
- {
- using (CreateServices())
- {
- TestControl child;
- var popup = new Popup
- {
- Child = child = new TestControl(),
- DataContext = "foo",
- PlacementTarget = PreparedWindow()
- };
- var beginCalled = false;
- child.DataContextBeginUpdate += (s, e) => beginCalled = true;
- // Test for #1245. Here, the child's logical parent is the popup but it's not yet
- // attached to a visual tree because the popup hasn't been opened.
- Assert.Same(popup, ((ILogical)child).LogicalParent);
- Assert.Same(popup, child.InheritanceParent);
- Assert.Null(child.GetVisualRoot());
- popup.Open();
- // #1245 was caused by the fact that DataContextBeginUpdate was called on `target`
- // when the PopupRoot was created, even though PopupRoot isn't the
- // InheritanceParent of child.
- Assert.False(beginCalled);
- }
- }
-
- [Fact]
- public void Popup_Host_Type_Should_Match_Platform_Preference()
- {
- using (CreateServices())
- {
- var target = new Popup() {PlacementTarget = PreparedWindow()};
- target.Open();
- if (UsePopupHost)
- Assert.IsType<OverlayPopupHost>(target.Host);
- else
- Assert.IsType<PopupRoot>(target.Host);
- }
- }
- private IDisposable CreateServices()
- {
- return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform:
- new MockWindowingPlatform(null,
- () =>
- {
- if(UsePopupHost)
- return null;
- return MockWindowingPlatform.CreatePopupMock().Object;
- })));
- }
- private static IControl PopupContentControlTemplate(PopupContentControl control, INameScope scope)
- {
- return new Popup
- {
- Name = "popup",
- PlacementTarget = control,
- Child = new ContentPresenter
- {
- [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
- }
- }.RegisterInNameScope(scope);
- }
- private class PopupContentControl : ContentControl
- {
- }
- private class TestControl : Decorator
- {
- public event EventHandler DataContextBeginUpdate;
- public new IAvaloniaObject InheritanceParent => base.InheritanceParent;
- protected override void OnDataContextBeginUpdate()
- {
- DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
- base.OnDataContextBeginUpdate();
- }
- }
- }
- public class PopupTestsWithPopupRoot : PopupTests
- {
- public PopupTestsWithPopupRoot()
- {
- UsePopupHost = true;
- }
- }
- }
|