summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45926.cs
blob: 152d94ad2fb17a361cd44d494e9cddc12d7a9f53 (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
using System;
using System.Threading;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{

	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 45926, "MessagingCenter prevents subscriber from being collected", PlatformAffected.All)]
	public class Bugzilla45926 : TestNavigationPage
	{
		protected override void Init()
		{
			Button createPage, sendMessage, doGC;

			Label instanceCount = new Label();
			Label messageCount = new Label();

			instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}";
			messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}";

			var content = new ContentPage {
				Title = "Test",
				Content = new StackLayout {
					VerticalOptions = LayoutOptions.Center,
					Children = {
						(createPage = new Button { Text = "New Page" }),
						(sendMessage = new Button { Text = "Send Message" }),
						(doGC = new Button { Text = "Do GC" }),
						instanceCount, messageCount
					}
				}
			};

			createPage.Clicked += (s, e) =>
			{
				PushAsync(new _45926IntermediatePage());
				PushAsync(new _45926SecondPage());
			};
			
			sendMessage.Clicked += (s, e) =>
			{
				MessagingCenter.Send (this, "Test");
			};

			doGC.Clicked += (sender, e) => {
				GC.Collect ();
				GC.WaitForPendingFinalizers();
				instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}";
				messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}";
			};

			PushAsync(content);
		}

#if UITEST
		[Test]
		public void Issue45926Test ()
		{
			RunningApp.WaitForElement (q => q.Marked ("New Page"));

			RunningApp.Tap (q => q.Marked ("New Page"));
			RunningApp.Back();
			RunningApp.Back();
			RunningApp.Tap(q => q.Marked("Do GC"));
			RunningApp.Tap(q => q.Marked("Send Message"));
			RunningApp.Tap(q => q.Marked("Do GC"));

			RunningApp.WaitForElement (q => q.Marked ("Instances: 0"));
			RunningApp.WaitForElement (q => q.Marked ("Messages: 0"));
		}
#endif
	}

	[Preserve(AllMembers = true)]
	public class _45926IntermediatePage : ContentPage
	{
	}

	[Preserve(AllMembers = true)]
	public class _45926SecondPage : ContentPage
	{
		public static int InstanceCounter = 0;
		public static int MessageCounter = 0;

		public _45926SecondPage ()
		{
			Interlocked.Increment(ref InstanceCounter);

			Content = new Label {
				HorizontalOptions = LayoutOptions.Center,
				VerticalOptions = LayoutOptions.Center,
				Text = "Second Page #" + (InstanceCounter)
			};

			MessagingCenter.Subscribe<Bugzilla45926> (this, "Test", OnMessage);
		}

		protected override void OnDisappearing ()
		{
			base.OnDisappearing ();
		}

		void OnMessage (Bugzilla45926 app)
		{
			System.Diagnostics.Debug.WriteLine ("Got Test message!");
			Interlocked.Increment(ref MessageCounter);
		}

		~_45926SecondPage ()
		{
			Interlocked.Decrement(ref InstanceCounter);
			System.Diagnostics.Debug.WriteLine ("~SecondPage: {0}", GetHashCode ());
		}
	}
}