summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32898.cs
blob: effcd41a02e7334b54e8a51d12242efd80a941a4 (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
´╗┐using System;

using Xamarin.Forms.CustomAttributes;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 32898, "Memory leak when TabbedPage is popped out ")]
	public class Bugzilla32898 : TestContentPage
	{
		WeakReference _page2Tracker;
		WeakReference _tabTracker;

		protected override void Init ()
		{
			var stack = new StackLayout () { VerticalOptions = LayoutOptions.Center };

			stack.Children.Add (new Label () {
				VerticalOptions = LayoutOptions.Center,
#pragma warning disable 618
				XAlign = TextAlignment.Center,
#pragma warning restore 618
				Text = "Page 1"
			});

			Content = stack;
		}

		protected override async void OnAppearing ()
		{
			base.OnAppearing ();

			if (_page2Tracker == null) {
				var page2 = new TabbedPage () { Children = { new ContentPage () { Title = "tab" } } };
				page2.Appearing += async delegate {
					await Task.Delay (1000);
					await page2.Navigation.PopModalAsync ();
				};

				_page2Tracker = new WeakReference (page2, false);
				_tabTracker = new WeakReference (page2.Children [0], false);

				await Task.Delay (1000);
				await Navigation.PushModalAsync (page2);

				StartTrackPage2 ();
			}
		}

		async void StartTrackPage2 ()
		{
			while (true) {
				((Label)((StackLayout)Content).Children [0]).Text =
						$"Page1. But Page2 IsAlive = {_page2Tracker.IsAlive}, tab IsAlive = {_tabTracker.IsAlive}";

				await Task.Delay (1000);
				GC.Collect ();
			}
		}

		#if UITEST
		[Test]
		public async void Issu32898Test ()
		{
			await Task.Delay (5000);
			RunningApp.WaitForElement (q => q.Marked ("Page1. But Page2 IsAlive = False, tab IsAlive = False"));
		}
		#endif
	}
}