Skip to content

Commit

Permalink
Add leak test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Corsaro committed Jul 2, 2024
1 parent adad1da commit 8ec09aa
Showing 1 changed file with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.ObjectModel;
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
Expand Down Expand Up @@ -202,6 +204,45 @@ await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async (handler) =>
Assert.True(data.Count == 30);
});
}

[Fact]
public async Task VerifyGroupCollectionDoesntLeak()
{
var groupHeaderTemplate = new Controls.DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Name"));
return label;
});
var footerTemplate = new Controls.DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Count"));
return label;
});
var itemTemplate = new Controls.DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("Name"));
return label;
});

WeakReference reference;
var itemSource = new ObservableCollection<string>() { "Hello", "World" };
{
var collection = new GroupedItemTemplateCollection(itemSource,
itemTemplate, groupHeaderTemplate, footerTemplate, null);

reference = new WeakReference(collection);
collection.Dispose();
}

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();

Assert.False(reference.IsAlive, "Subscriber should not be alive!");
}

[Fact]
public async Task CollectionViewContentHeightChanged()
Expand Down Expand Up @@ -260,5 +301,34 @@ Rect GetCollectionViewCellBounds(IView cellContent)

return cellContent.ToPlatform().GetParentOfType<ItemContentControl>().GetBoundingBox();
}

class Subscriber
{
public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { }
}

private interface IItem { }

private class AnimalGroup : ObservableCollection<IItem>, IItem
{
internal string Name { get; }

internal AnimalGroup(string name, ObservableCollection<IItem> animals) : base(animals)
{
Name = name;
}
}

private class Animal : IItem
{
internal string Name { get; }
internal string Location { get; }

internal Animal(string name, string location)
{
Name = name;
Location = location;
}
}
}
}

0 comments on commit 8ec09aa

Please sign in to comment.