/
MemoryTests.cs
159 lines (146 loc) · 5.34 KB
/
MemoryTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
namespace Microsoft.Maui.DeviceTests.Memory;
[Category(TestCategory.Memory)]
public class MemoryTests : ControlsHandlerTestBase
{
void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<ActivityIndicator, ActivityIndicatorHandler>();
handlers.AddHandler<Border, BorderHandler>();
handlers.AddHandler<BoxView, BoxViewHandler>();
handlers.AddHandler<CarouselView, CarouselViewHandler>();
handlers.AddHandler<CheckBox, CheckBoxHandler>();
handlers.AddHandler<DatePicker, DatePickerHandler>();
handlers.AddHandler<Entry, EntryHandler>();
handlers.AddHandler<Editor, EditorHandler>();
handlers.AddHandler<Frame, FrameRenderer>();
handlers.AddHandler<GraphicsView, GraphicsViewHandler>();
handlers.AddHandler<Label, LabelHandler>();
handlers.AddHandler<ListView, ListViewRenderer>();
handlers.AddHandler<Picker, PickerHandler>();
handlers.AddHandler<Polygon, PolygonHandler>();
handlers.AddHandler<Polyline, PolylineHandler>();
handlers.AddHandler<IContentView, ContentViewHandler>();
handlers.AddHandler<Image, ImageHandler>();
handlers.AddHandler<ImageButton, ImageButtonHandler>();
handlers.AddHandler<IndicatorView, IndicatorViewHandler>();
handlers.AddHandler<RefreshView, RefreshViewHandler>();
handlers.AddHandler<IScrollView, ScrollViewHandler>();
handlers.AddHandler<Slider, SliderHandler>();
handlers.AddHandler<Stepper, StepperHandler>();
handlers.AddHandler<SwipeView, SwipeViewHandler>();
handlers.AddHandler<Switch, SwitchHandler>();
handlers.AddHandler<TableView, TableViewRenderer>();
handlers.AddHandler<TimePicker, TimePickerHandler>();
handlers.AddHandler<WebView, WebViewHandler>();
});
});
}
[Theory("Handler Does Not Leak")]
[InlineData(typeof(ActivityIndicator))]
[InlineData(typeof(Border))]
[InlineData(typeof(BoxView))]
[InlineData(typeof(CarouselView))]
[InlineData(typeof(ContentView))]
[InlineData(typeof(CheckBox))]
[InlineData(typeof(DatePicker))]
[InlineData(typeof(Entry))]
[InlineData(typeof(Editor))]
[InlineData(typeof(Frame))]
[InlineData(typeof(GraphicsView))]
[InlineData(typeof(Image))]
[InlineData(typeof(ImageButton))]
[InlineData(typeof(IndicatorView))]
[InlineData(typeof(Label))]
[InlineData(typeof(Picker))]
[InlineData(typeof(Polygon))]
[InlineData(typeof(Polyline))]
[InlineData(typeof(RefreshView))]
[InlineData(typeof(ScrollView))]
[InlineData(typeof(Slider))]
[InlineData(typeof(Stepper))]
[InlineData(typeof(SwipeView))]
[InlineData(typeof(Switch))]
[InlineData(typeof(TimePicker))]
[InlineData(typeof(TableView))]
[InlineData(typeof(WebView))]
public async Task HandlerDoesNotLeak(Type type)
{
SetupBuilder();
#if ANDROID
// NOTE: skip certain controls on older Android devices
if (type == typeof (DatePicker) && !OperatingSystem.IsAndroidVersionAtLeast(30))
return;
#endif
#if IOS
// NOTE: skip certain controls on older iOS devices
if (type == typeof(WebView) && !OperatingSystem.IsIOSVersionAtLeast(16))
return;
#endif
WeakReference viewReference = null;
WeakReference platformViewReference = null;
WeakReference handlerReference = null;
var observable = new ObservableCollection<int> { 1, 2, 3 };
await InvokeOnMainThreadAsync(async () =>
{
var layout = new Grid();
var view = (View)Activator.CreateInstance(type);
layout.Add(view);
if (view is ContentView content)
{
content.Content = new Label();
}
else if (view is ItemsView items)
{
items.ItemTemplate = new DataTemplate(() => new Label());
items.ItemsSource = observable;
}
else if (view is WebView webView)
{
webView.Source = new HtmlWebViewSource { Html = "<p>hi</p>" };
await Task.Delay(1000);
}
var handler = CreateHandler<LayoutHandler>(layout);
viewReference = new WeakReference(view);
handlerReference = new WeakReference(view.Handler);
platformViewReference = new WeakReference(view.Handler.PlatformView);
});
await AssertionExtensions.WaitForGC(viewReference, handlerReference, platformViewReference);
Assert.False(viewReference.IsAlive, $"{type} should not be alive!");
Assert.False(handlerReference.IsAlive, "Handler should not be alive!");
Assert.False(platformViewReference.IsAlive, "PlatformView should not be alive!");
}
#if IOS
[Fact]
public async Task ResignFirstResponderTouchGestureRecognizer()
{
WeakReference viewReference = null;
WeakReference recognizerReference = null;
await InvokeOnMainThreadAsync(() =>
{
var view = new UIKit.UIView();
var recognizer = new Platform.ResignFirstResponderTouchGestureRecognizer(view);
view.AddGestureRecognizer(recognizer);
viewReference = new(view);
recognizerReference = new(recognizer);
});
await AssertionExtensions.WaitForGC(viewReference, recognizerReference);
Assert.False(viewReference.IsAlive, "UIView should not be alive!");
Assert.False(recognizerReference.IsAlive, "ResignFirstResponderTouchGestureRecognizer should not be alive!");
}
#endif
}