Skip to content

Commit ead4489

Browse files
Copilotrmarinho
authored andcommitted
Add CarouselView memory leak test with default ItemsLayout
Co-authored-by: rmarinho <1235097+rmarinho@users.noreply.github.com>
1 parent 187b02f commit ead4489

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
1-
namespace Microsoft.Maui.DeviceTests
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.Maui.Controls;
5+
using Microsoft.Maui.Controls.Handlers.Items2;
6+
using Microsoft.Maui.Handlers;
7+
using Xunit;
8+
9+
namespace Microsoft.Maui.DeviceTests
210
{
311
public partial class CarouselViewTests
412
{
13+
[Fact(DisplayName = "CarouselView Does Not Leak With Default ItemsLayout")]
14+
public async Task CarouselViewDoesNotLeakWithDefaultItemsLayout()
15+
{
16+
SetupBuilder();
17+
18+
WeakReference weakCarouselView = null;
19+
WeakReference weakHandler = null;
20+
21+
await InvokeOnMainThreadAsync(async () =>
22+
{
23+
var carouselView = new CarouselView
24+
{
25+
ItemsSource = new List<string> { "Item 1", "Item 2", "Item 3" },
26+
ItemTemplate = new DataTemplate(() => new Label())
27+
// Note: Not setting ItemsLayout - using the default
28+
};
29+
30+
weakCarouselView = new WeakReference(carouselView);
31+
32+
var handler = await CreateHandlerAsync<CarouselViewHandler2>(carouselView);
33+
34+
// Verify handler is created
35+
Assert.NotNull(handler);
36+
37+
// Store weak reference to the handler
38+
weakHandler = new WeakReference(handler);
39+
40+
// Disconnect the handler
41+
((IElementHandler)handler).DisconnectHandler();
42+
});
43+
44+
// Force garbage collection
45+
await AssertionExtensions.WaitForGC(weakCarouselView, weakHandler);
546

47+
// Verify the CarouselView was collected
48+
Assert.False(weakCarouselView.IsAlive, "CarouselView should have been garbage collected");
49+
50+
// Verify the handler was collected
51+
Assert.False(weakHandler.IsAlive, "CarouselViewHandler2 should have been garbage collected");
52+
}
653
}
754
}

0 commit comments

Comments
 (0)