| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reactive.Disposables;
- 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.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
- {
- Items = _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)
- },
- Items = 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
- {
- Items = 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.Items = 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 renderer = new Mock<IRenderer>();
- renderer.Setup(x => x.Dispose());
- var impl = new Mock<IWindowImpl>();
- impl.SetupGet(x => x.RenderScaling).Returns(1);
- impl.SetupProperty(x => x.Closed);
- impl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer.Object);
- 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();
- renderer.Verify(r => r.Dispose());
- }
- }
- [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[]
- {
- new MenuItem { Header = "Foo" },
- new MenuItem { Header = "Foo" },
- }
- };
- control.ContextMenu = contextMenu;
- contextMenu.Open(control);
- contextMenu.Close();
- control.ContextMenu = null;
- }
- var window = new Window();
- window.Show();
- Assert.Same(window, FocusManager.Instance.Current);
- // 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 Standalone_ContextMenu_Is_Freed()
- {
- using (Start())
- {
- void BuildAndShowContextMenu(Control control)
- {
- var contextMenu = new ContextMenu
- {
- Items = new[]
- {
- new MenuItem { Header = "Foo" },
- new MenuItem { Header = "Foo" },
- }
- };
- contextMenu.Open(control);
- contextMenu.Close();
- }
- var window = new Window();
- window.Show();
- Assert.Same(window, FocusManager.Instance.Current);
- // 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())
- {
- 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 = 100,
- Content = new StackPanel
- {
- Children =
- {
- (tb = new TextBox
- {
- Name = "tb",
- Text = "foo",
- }),
- (lb = new ListBox
- {
- Items = items,
- ItemTemplate = new FuncDataTemplate<int>((_, _) =>
- new Canvas
- {
- Width = 10,
- Height = 10,
- [!Control.TagProperty] = new Binding
- {
- ElementName = "tb",
- Path = "Text",
- NameScope = new WeakReference<INameScope>(ns),
- }
- })
- }),
- }
- }
- };
- 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.Items = 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; }
- }
- private class NullRenderer : IRenderer
- {
- public bool DrawFps { get; set; }
- public bool DrawDirtyRects { get; set; }
- #pragma warning disable CS0067
- public event EventHandler<SceneInvalidatedEventArgs> SceneInvalidated;
- #pragma warning restore CS0067
- public void AddDirty(Visual visual)
- {
- }
- public void Dispose()
- {
- }
- public IEnumerable<Visual> HitTest(Point p, Visual root, Func<Visual, bool> filter) => null;
- public Visual HitTestFirst(Point p, Visual root, Func<Visual, bool> filter) => null;
- public void Paint(Rect rect)
- {
- }
- public void RecalculateChildren(Visual visual)
- {
- }
- public void Resized(Size size)
- {
- }
- public void Start()
- {
- }
- public void Stop()
- {
- }
- }
- }
- }
|