| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reactive.Disposables;
- using System.Threading.Tasks;
- using Avalonia.Collections;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Shapes;
- using Avalonia.Controls.Templates;
- using Avalonia.Data;
- using Avalonia.Diagnostics;
- using Avalonia.Input;
- using Avalonia.Media;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.Rendering.Composition;
- using Avalonia.Styling;
- using Avalonia.Threading;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using JetBrains.dotMemoryUnit;
- using Moq;
- using Xunit;
- using Xunit.Abstractions;
- namespace Avalonia.LeakTests
- {
- [DotMemoryUnit(FailIfRunWithoutSupport = false)]
- public class ControlTests
- {
- // Need to have the collection as field, so GC will not free it
- private readonly ObservableCollection<string> _observableCollection = new();
-
- public ControlTests(ITestOutputHelper atr)
- {
- DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine);
- }
-
- [Fact]
- public void DataGrid_Is_Freed()
- {
- using (Start())
- {
- // When attached to INotifyCollectionChanged, DataGrid will subscribe to it's events, potentially causing leak
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new DataGrid
- {
- ItemsSource = _observableCollection
- }
- };
- window.Show();
- // Do a layout and make sure that DataGrid gets added to visual tree.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<DataGrid>(window.Presenter.Child);
- // Clear the content and ensure the DataGrid is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<DataGrid>()).ObjectsCount));
- }
- }
- [Fact]
- public void Canvas_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new Canvas()
- };
- window.Show();
- // Do a layout and make sure that Canvas gets added to visual tree.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<Canvas>(window.Presenter.Child);
- // Clear the content and ensure the Canvas is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
- }
- }
- [Fact]
- public void Named_Canvas_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var scope = new NameScope();
- var window = new Window
- {
- Content = new Canvas
- {
- Name = "foo"
- }.RegisterInNameScope(scope)
- };
- NameScope.SetNameScope(window, scope);
- window.Show();
- // Do a layout and make sure that Canvas gets added to visual tree.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<Canvas>(window.Find<Canvas>("foo"));
- Assert.IsType<Canvas>(window.Presenter.Child);
- // Clear the content and ensure the Canvas is removed.
- window.Content = null;
- NameScope.SetNameScope(window, null);
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
- }
- }
- [Fact]
- public void ScrollViewer_With_Content_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new ScrollViewer
- {
- Content = new Canvas()
- }
- };
- window.Show();
- // Do a layout and make sure that ScrollViewer gets added to visual tree and its
- // template applied.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<ScrollViewer>(window.Presenter.Child);
- Assert.IsType<Canvas>(((ScrollViewer)window.Presenter.Child).Presenter.Child);
- // Clear the content and ensure the ScrollViewer is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TextBox>()).ObjectsCount));
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
- }
- }
- [Fact]
- public void TextBox_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new TextBox()
- };
- window.Show();
- // Do a layout and make sure that TextBox gets added to visual tree and its
- // template applied.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<TextBox>(window.Presenter.Child);
- Assert.NotEmpty(window.Presenter.Child.GetVisualChildren());
- // Clear the content and ensure the TextBox is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TextBox>()).ObjectsCount));
- }
- }
- [Fact]
- public void TextBox_With_Xaml_Binding_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- DataContext = new Node { Name = "foo" },
- Content = new TextBox()
- };
- var binding = new Avalonia.Data.Binding
- {
- Path = "Name"
- };
- var textBox = (TextBox)window.Content;
- textBox.Bind(TextBox.TextProperty, binding);
- window.Show();
- // Do a layout and make sure that TextBox gets added to visual tree and its
- // Text property set.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<TextBox>(window.Presenter.Child);
- Assert.Equal("foo", ((TextBox)window.Presenter.Child).Text);
- // Clear the content and DataContext and ensure the TextBox is removed.
- window.Content = null;
- window.DataContext = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TextBox>()).ObjectsCount));
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Node>()).ObjectsCount));
- }
- }
- [Fact]
- public void TextBox_Class_Listeners_Are_Freed()
- {
- using (Start())
- {
- TextBox textBox;
- var window = new Window
- {
- Content = textBox = new TextBox()
- };
- window.Show();
- // Do a layout and make sure that TextBox gets added to visual tree and its
- // template applied.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.Same(textBox, window.Presenter.Child);
- // Get the border from the TextBox template.
- var border = textBox.GetTemplateChildren().FirstOrDefault(x => x.Name == "border");
- // The TextBox should have subscriptions to its Classes collection from the
- // default theme.
- Assert.NotEqual(0, textBox.Classes.ListenerCount);
- // Clear the content and ensure the TextBox is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- // Check that the TextBox has no subscriptions to its Classes collection.
- Assert.Null(((INotifyCollectionChangedDebug)textBox.Classes).GetCollectionChangedSubscribers());
- }
- }
- [Fact]
- public void TreeView_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var nodes = new[]
- {
- new Node
- {
- Children = new[] { new Node() },
- }
- };
- TreeView target;
- var window = new Window
- {
- Content = target = new TreeView
- {
- DataTemplates =
- {
- new FuncTreeDataTemplate<Node>(
- (x, _) => new TextBlock { Text = x.Name },
- x => x.Children)
- },
- ItemsSource = nodes
- }
- };
- window.Show();
- // Do a layout and make sure that TreeViewItems get realized.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.Single(target.GetRealizedContainers());
- // Clear the content and ensure the TreeView is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TreeView>()).ObjectsCount));
- }
- }
- [Fact]
- public void Slider_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new Slider()
- };
- window.Show();
- // Do a layout and make sure that Slider gets added to visual tree.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<Slider>(window.Presenter.Child);
- // Clear the content and ensure the Slider is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Slider>()).ObjectsCount));
- }
- }
- [Fact]
- public void TabItem_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new TabControl
- {
- ItemsSource = new[] { new TabItem() }
- }
- };
- window.Show();
- // Do a layout and make sure that TabControl and TabItem gets added to visual tree.
- window.LayoutManager.ExecuteInitialLayoutPass();
- var tabControl = Assert.IsType<TabControl>(window.Presenter.Child);
- Assert.IsType<TabItem>(tabControl.Presenter.Panel.Children[0]);
- // Clear the items and ensure the TabItem is removed.
- tabControl.ItemsSource = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Empty(tabControl.Presenter.Panel.Children);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TabItem>()).ObjectsCount));
- }
- }
- [Fact]
- public void RendererIsDisposed()
- {
- using (Start())
- {
- var impl = new Mock<IWindowImpl>();
- impl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null);
- impl.SetupGet(x => x.RenderScaling).Returns(1);
- impl.SetupProperty(x => x.Closed);
- impl.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
- impl.Setup(x => x.Dispose()).Callback(() => impl.Object.Closed());
- AvaloniaLocator.CurrentMutable.Bind<IWindowingPlatform>()
- .ToConstant(new MockWindowingPlatform(() => impl.Object));
- var window = new Window()
- {
- Content = new Button()
- };
- window.Show();
- window.Close();
- Assert.True(((CompositingRenderer)window.Renderer).IsDisposed);
- }
- }
- [Fact]
- public void Control_With_Style_RenderTransform_Is_Freed()
- {
- // # Issue #3545
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Styles =
- {
- new Style(x => x.OfType<Canvas>())
- {
- Setters =
- {
- new Setter
- {
- Property = Visual.RenderTransformProperty,
- Value = new RotateTransform(45),
- }
- }
- }
- },
- Content = new Canvas()
- };
- window.Show();
- // Do a layout and make sure that Canvas gets added to visual tree with
- // its render transform.
- window.LayoutManager.ExecuteInitialLayoutPass();
- var canvas = Assert.IsType<Canvas>(window.Presenter.Child);
- Assert.IsType<RotateTransform>(canvas.RenderTransform);
- // Clear the content and ensure the Canvas is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
- }
- }
- [Fact]
- public void Attached_ContextMenu_Is_Freed()
- {
- using (Start())
- {
- void AttachShowAndDetachContextMenu(Control control)
- {
- var contextMenu = new ContextMenu
- {
- Items =
- {
- new MenuItem { Header = "Foo" },
- new MenuItem { Header = "Foo" },
- }
- };
- control.ContextMenu = contextMenu;
- contextMenu.Open(control);
- contextMenu.Close();
- control.ContextMenu = null;
- }
- var window = new Window { Focusable = true };
- window.Show();
- Assert.Same(window, window.FocusManager.GetFocusedElement());
- // Context menu in resources means the baseline may not be 0.
- var initialMenuCount = 0;
- var initialMenuItemCount = 0;
- dotMemory.Check(memory =>
- {
- initialMenuCount = memory.GetObjects(where => where.Type.Is<ContextMenu>()).ObjectsCount;
- initialMenuItemCount = memory.GetObjects(where => where.Type.Is<MenuItem>()).ObjectsCount;
- });
- AttachShowAndDetachContextMenu(window);
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- Mock.Get(window.PlatformImpl).Invocations.Clear();
- dotMemory.Check(memory =>
- Assert.Equal(initialMenuCount, memory.GetObjects(where => where.Type.Is<ContextMenu>()).ObjectsCount));
- dotMemory.Check(memory =>
- Assert.Equal(initialMenuItemCount, memory.GetObjects(where => where.Type.Is<MenuItem>()).ObjectsCount));
- }
- }
-
- [Fact]
- public void Attached_Control_From_ContextMenu_Is_Freed()
- {
- using (Start())
- {
- var contextMenu = new ContextMenu();
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new TextBlock
- {
- ContextMenu = contextMenu
- }
- };
- window.Show();
- // Do a layout and make sure that TextBlock gets added to visual tree.
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<TextBlock>(window.Presenter.Child);
- // Clear the content and ensure the TextBlock is removed.
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TextBlock>()).ObjectsCount));
- }
- }
- [Fact]
- public void Standalone_ContextMenu_Is_Freed()
- {
- using (Start())
- {
- void BuildAndShowContextMenu(Control control)
- {
- var contextMenu = new ContextMenu
- {
- Items =
- {
- new MenuItem { Header = "Foo" },
- new MenuItem { Header = "Foo" },
- }
- };
- contextMenu.Open(control);
- contextMenu.Close();
- }
- var window = new Window { Focusable = true };
- window.Show();
- Assert.Same(window, window.FocusManager.GetFocusedElement());
- // Context menu in resources means the baseline may not be 0.
- var initialMenuCount = 0;
- var initialMenuItemCount = 0;
- dotMemory.Check(memory =>
- {
- initialMenuCount = memory.GetObjects(where => where.Type.Is<ContextMenu>()).ObjectsCount;
- initialMenuItemCount = memory.GetObjects(where => where.Type.Is<MenuItem>()).ObjectsCount;
- });
- BuildAndShowContextMenu(window);
- BuildAndShowContextMenu(window);
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- Mock.Get(window.PlatformImpl).Invocations.Clear();
- dotMemory.Check(memory =>
- Assert.Equal(initialMenuCount, memory.GetObjects(where => where.Type.Is<ContextMenu>()).ObjectsCount));
- dotMemory.Check(memory =>
- Assert.Equal(initialMenuItemCount, memory.GetObjects(where => where.Type.Is<MenuItem>()).ObjectsCount));
- }
- }
- [Fact]
- public void Path_Is_Freed()
- {
- using (Start())
- {
- var geometry = new EllipseGeometry { Rect = new Rect(0, 0, 10, 10) };
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new Path
- {
- Data = geometry
- }
- };
- window.Show();
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<Path>(window.Presenter.Child);
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Path>()).ObjectsCount));
- // We are keeping geometry alive to simulate a resource that outlives the control.
- GC.KeepAlive(geometry);
- }
- }
- [Fact]
- public void ItemsRepeater_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window
- {
- Content = new ItemsRepeater(),
- };
- window.Show();
- window.LayoutManager.ExecuteInitialLayoutPass();
- Assert.IsType<ItemsRepeater>(window.Presenter.Child);
- window.Content = null;
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Null(window.Presenter.Child);
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<ItemsRepeater>()).ObjectsCount));
- }
- }
- [Fact]
- public void ElementName_Binding_In_DataTemplate_Is_Freed()
- {
- using (Start())
- {
- // Height of ListBoxItem content + padding.
- const double ListBoxItemHeight = 12;
- const double TextBoxHeight = 25;
- var items = new ObservableCollection<int>(Enumerable.Range(0, 10));
- NameScope ns;
- TextBox tb;
- ListBox lb;
- var window = new Window
- {
- [NameScope.NameScopeProperty] = ns = new NameScope(),
- Width = 100,
- Height = (items.Count * ListBoxItemHeight) + TextBoxHeight,
- Content = new DockPanel
- {
- Children =
- {
- (tb = new TextBox
- {
- Name = "tb",
- Text = "foo",
- Height = TextBoxHeight,
- [DockPanel.DockProperty] = Dock.Top,
- }),
- (lb = new ListBox
- {
- ItemsSource = items,
- ItemTemplate = new FuncDataTemplate<int>((_, _) =>
- new Canvas
- {
- Width = 10,
- Height = 10,
- [!Control.TagProperty] = new Binding
- {
- ElementName = "tb",
- Path = "Text",
- NameScope = new WeakReference<INameScope>(ns),
- }
- }),
- Padding = new Thickness(0),
- }),
- }
- }
- };
- tb.RegisterInNameScope(ns);
- window.Show();
- window.LayoutManager.ExecuteInitialLayoutPass();
- void AssertInitialItemState()
- {
- var item0 = (ListBoxItem)lb.GetRealizedContainers().First();
- var canvas0 = (Canvas)item0.Presenter.Child;
- Assert.Equal("foo", canvas0.Tag);
- }
- Assert.Equal(10, lb.GetRealizedContainers().Count());
- AssertInitialItemState();
- items.Clear();
- window.LayoutManager.ExecuteLayoutPass();
- Assert.Empty(lb.GetRealizedContainers());
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
- }
- }
- [Fact]
- public void HotKeyManager_Should_Release_Reference_When_Control_Detached()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var gesture1 = new KeyGesture(Key.A, KeyModifiers.Control);
- var tl = new Window
- {
- Content = new ItemsRepeater(),
- };
- tl.Show();
- var button = new Button();
- tl.Content = button;
- tl.Template = CreateWindowTemplate();
- tl.ApplyTemplate();
- tl.Presenter.ApplyTemplate();
- HotKeyManager.SetHotKey(button, gesture1);
- // Detach the button from the logical tree, so there is no reference to it
- tl.Content = null;
- tl.ApplyTemplate();
- return tl;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Button>()).ObjectsCount));
- }
- }
- [Fact]
- public void HotKeyManager_Should_Release_Reference_When_Control_In_Item_Template_Detached()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var gesture1 = new KeyGesture(Key.A, KeyModifiers.Control);
- var tl = new Window { SizeToContent = SizeToContent.WidthAndHeight, IsVisible = true };
- var lm = tl.LayoutManager;
- tl.Show();
- var keyGestures = new AvaloniaList<KeyGesture> { gesture1 };
- var listBox = new ListBox
- {
- Width = 100,
- Height = 100,
- // Create a button with binding to the KeyGesture in the template and add it to references list
- ItemTemplate = new FuncDataTemplate(typeof(KeyGesture), (o, scope) =>
- {
- var keyGesture = o as KeyGesture;
- return new Button
- {
- DataContext = keyGesture,
- [!Button.HotKeyProperty] = new Binding("")
- };
- })
- };
- // Add the listbox and render it
- tl.Content = listBox;
- lm.ExecuteInitialLayoutPass();
- listBox.ItemsSource = keyGestures;
- lm.ExecuteLayoutPass();
- // Let the button detach when clearing the source items
- keyGestures.Clear();
- lm.ExecuteLayoutPass();
- // Add it again to double check,and render
- keyGestures.Add(gesture1);
- lm.ExecuteLayoutPass();
- keyGestures.Clear();
- lm.ExecuteLayoutPass();
- return tl;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Button>()).ObjectsCount));
- }
- }
- [Fact]
- public void ToolTip_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window();
- var source = new Button
- {
- Template = new FuncControlTemplate<Button>((parent, _) =>
- new Decorator
- {
- [ToolTip.TipProperty] = new TextBlock
- {
- [~TextBlock.TextProperty] = new TemplateBinding(ContentControl.ContentProperty)
- }
- }),
- };
- window.Content = source;
- window.Show();
- var templateChild = (Decorator)source.GetVisualChildren().Single();
- ToolTip.SetIsOpen(templateChild, true);
- ToolTip.SetIsOpen(templateChild, false);
- // Detach the button from the logical tree, so there is no reference to it
- window.Content = null;
-
- // Mock keep reference on a Popup via InvocationsCollection. So let's clear it before.
- Mock.Get(window.PlatformImpl).Invocations.Clear();
-
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- {
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TextBlock>()).ObjectsCount);
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<ToolTip>()).ObjectsCount);
- });
- }
- }
-
- [Fact]
- public void Flyout_Is_Freed()
- {
- using (Start())
- {
- Func<Window> run = () =>
- {
- var window = new Window();
- var source = new Button
- {
- Template = new FuncControlTemplate<Button>((parent, _) =>
- new Button
- {
- Flyout = new Flyout
- {
- Content = new TextBlock
- {
- [~TextBlock.TextProperty] = new TemplateBinding(ContentControl.ContentProperty)
- }
- }
- }),
- };
- window.Content = source;
- window.Show();
- var templateChild = (Button)source.GetVisualChildren().Single();
- templateChild.Flyout!.ShowAt(templateChild);
-
- templateChild.Flyout!.Hide();
- // Detach the button from the logical tree, so there is no reference to it
- window.Content = null;
- // Mock keep reference on a Popup via InvocationsCollection. So let's clear it before.
- Mock.Get(window.PlatformImpl).Invocations.Clear();
-
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- {
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TextBlock>()).ObjectsCount);
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Flyout>()).ObjectsCount);
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Popup>()).ObjectsCount);
- });
- }
- }
-
- private FuncControlTemplate CreateWindowTemplate()
- {
- return new FuncControlTemplate<Window>((parent, scope) =>
- {
- return new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
- }.RegisterInNameScope(scope);
- });
- }
- private IDisposable Start()
- {
- static void Cleanup()
- {
- // KeyboardDevice holds a reference to the focused item.
- KeyboardDevice.Instance.SetFocusedElement(null, NavigationMethod.Unspecified, KeyModifiers.None);
-
- // Empty the dispatcher queue.
- Dispatcher.UIThread.RunJobs();
- }
- return new CompositeDisposable
- {
- Disposable.Create(Cleanup),
- UnitTestApplication.Start(TestServices.StyledWindow.With(
- focusManager: new FocusManager(),
- keyboardDevice: () => new KeyboardDevice(),
- inputManager: new InputManager()))
- };
- }
- private class Node
- {
- public string Name { get; set; }
- public IEnumerable<Node> Children { get; set; }
- }
- }
- }
|