summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44166.cs
blob: e38da0237f113bd4a7bcea2249fd49625cf335e2 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.ObjectModel;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Threading;
using System.Diagnostics;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 44166, "MasterDetailPage instances do not get disposed upon GC")]
	public class Bugzilla44166 : TestContentPage
	{
		protected override void Init()
		{
			var label = new Label() { Text = "Testing..." };

			var goButton = new Button { Text = "Go", AutomationId = "Go" };
			goButton.Clicked += (sender, args) => Application.Current.MainPage = new _44166MDP();

			var gcButton = new Button { Text = "GC", AutomationId = "GC" };
			gcButton.Clicked += (sender, args) =>
			{
				GC.Collect();
				GC.WaitForPendingFinalizers();

				if (_44166MDP.Counter > 0)
				{
					Debug.WriteLine($">>>>>>>> Post-GC, {_44166MDP.Counter} {nameof(_44166MDP)} allocated");
				}

				if (_44166Master.Counter > 0)
				{
					Debug.WriteLine($">>>>>>>> Post-GC, {_44166Master.Counter} {nameof(_44166Master)} allocated");
				}

				if (_44166Detail.Counter > 0)
				{
					Debug.WriteLine($">>>>>>>> Post-GC, {_44166Detail.Counter} {nameof(_44166Detail)} allocated");
				}

				if (_44166NavContent.Counter > 0)
				{
					Debug.WriteLine($">>>>>>>> Post-GC, {_44166NavContent.Counter} {nameof(_44166NavContent)} allocated");
				}

				if (_44166NavContent.Counter + _44166Detail.Counter + _44166Master.Counter + _44166MDP.Counter == 0)
				{
					label.Text = "Success";
				}
			};

			Content = new StackLayout
			{
				Children = { label, goButton, gcButton }
			};
		}

#if UITEST
		[Test]
		public void Bugzilla44166Test()
		{
			RunningApp.WaitForElement(q => q.Marked("Go"));
			RunningApp.Tap(q => q.Marked("Go"));

			RunningApp.WaitForElement(q => q.Marked("Previous"));
			RunningApp.Tap(q => q.Marked("Previous"));

			RunningApp.WaitForElement(q => q.Marked("GC"));

			for (var n = 0; n < 10; n++)
			{
				RunningApp.Tap(q => q.Marked("GC"));

				if (RunningApp.Query(q => q.Marked("Success")).Length > 0)
				{
					return;
				}
			}

			string pageStats = string.Empty;

			if (_44166MDP.Counter > 0)
			{
				pageStats += $"{_44166MDP.Counter} {nameof(_44166MDP)} allocated; ";
			}

			if (_44166Master.Counter > 0)
			{
				pageStats += $"{_44166Master.Counter} {nameof(_44166Master)} allocated; ";
			}

			if (_44166Detail.Counter > 0)
			{
				pageStats += $"{_44166Detail.Counter} {nameof(_44166Detail)} allocated; ";
			}

			if (_44166NavContent.Counter > 0)
			{
				pageStats += $"{_44166NavContent.Counter} {nameof(_44166NavContent)} allocated; ";
			}

			Assert.Fail($"At least one of the pages was not collected: {pageStats}");
		}
#endif
	}

	[Preserve(AllMembers = true)]
	public class _44166MDP : MasterDetailPage
	{
		public static int Counter;

		public _44166MDP()
		{
			Interlocked.Increment(ref Counter);
			Debug.WriteLine($"++++++++ {nameof(_44166MDP)} constructor, {Counter} allocated");

			Master = new _44166Master();
			Detail = new _44166Detail();
        }

		~_44166MDP()
		{
			Interlocked.Decrement(ref Counter);
			Debug.WriteLine($"-------- {nameof(_44166MDP)} destructor, {Counter} allocated");
		}
	}

	[Preserve(AllMembers = true)]
	public class _44166Master : ContentPage
	{
		public static int Counter;

		public _44166Master()
		{
			Interlocked.Increment(ref Counter);
			Debug.WriteLine($"++++++++ {nameof(_44166Master)} constructor, {Counter} allocated");

			Title = "Master";
			var goButton = new Button { Text = "Return", AutomationId = "Return"};
			goButton.Clicked += (sender, args) => Application.Current.MainPage = new Bugzilla44166();

			Content = new StackLayout
			{
				Children = { goButton }
			};
		}

		~_44166Master()
		{
			Interlocked.Decrement(ref Counter);
			Debug.WriteLine($"-------- {nameof(_44166Master)} destructor, {Counter} allocated");
		}
	}

	[Preserve(AllMembers = true)]
	public class _44166Detail : NavigationPage
	{
		public static int Counter;

		public _44166Detail()
		{
			Interlocked.Increment(ref Counter);
			Debug.WriteLine($"++++++++ {nameof(_44166Detail)} constructor, {Counter} allocated");

			Title = "Detail";
			PushAsync(new _44166NavContent());
		}

		~_44166Detail()
		{
			Interlocked.Decrement(ref Counter);
			Debug.WriteLine($"-------- {nameof(_44166Detail)} destructor, {Counter} allocated");
		}
	}

	[Preserve(AllMembers = true)]
	public class _44166NavContent : ContentPage
	{
		public static int Counter;

		public _44166NavContent ()
		{
			Interlocked.Increment(ref Counter);
			Debug.WriteLine($"++++++++ {nameof(_44166NavContent)} constructor, {Counter} allocated");

			var goButton = new Button { Text = "Previous", AutomationId = "Previous" };
			goButton.Clicked += (sender, args) => Application.Current.MainPage = new Bugzilla44166();

			Content = new StackLayout
			{
				Children = { goButton }
			};
		}

		~_44166NavContent ()
		{
			Interlocked.Decrement(ref Counter);
			Debug.WriteLine($"-------- {nameof(_44166NavContent)} destructor, {Counter} allocated");
		}
	}
}