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

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

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.None, 1024, "Entry and Editor are leaking when used in ViewCell", PlatformAffected.iOS)]
	public class Bugzilla1024 : TestNavigationPage
	{
		protected override void Init()
		{
			PushAsync(new LandingPage1024());
		}

#if UITEST && __IOS__
		[Test]
		public void Bugzilla1024Test()
		{
			for (var n = 0; n < 10; n++)
			{
				RunningApp.WaitForElement(q => q.Marked("Push"));
				RunningApp.Tap(q => q.Marked("Push"));

				RunningApp.WaitForElement(q => q.Marked("ListView"));
				RunningApp.Back();
			}

			// At this point, the counter can be any value, but it's most likely not zero.
			// Invoking GC once is enough to clean up all garbage data and set counter to zero
			RunningApp.WaitForElement(q => q.Marked("GC"));
			RunningApp.Tap(q => q.Marked("GC"));

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

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

		public LandingPage1024()
		{
			Label = new Label
			{
				Text = "Counter: " + Counter,
				HorizontalTextAlignment = TextAlignment.Center,
				VerticalTextAlignment = TextAlignment.Center
			};

			Content = new StackLayout
			{
				Orientation = StackOrientation.Vertical,
				HorizontalOptions = LayoutOptions.Center,
				VerticalOptions = LayoutOptions.Center,
				Spacing = 15,
				Children =
				{
					new Label
					{
						Text = "Click Push to show a ListView. When you hit the Back button, Counter will show the number of pages that have not been finalized yet."
						+ " If you click GC, the counter should be 0."
					},
					Label,
					new Button
					{
						Text = "GC",
						AutomationId = "GC",
						Command = new Command(o =>
						{
							GC.Collect();
							GC.WaitForPendingFinalizers();
							GC.Collect();
							Label.Text = "Counter: " + Counter;
						})
					},
					new Button
					{
						Text = "Push",
						AutomationId = "Push",
						Command = new Command(async o =>
						{
							await Navigation.PushAsync(new ContentPage1024());
						})
					}
				}
			};
		}

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

			if (Label != null)
				Label.Text = "Counter: " + Counter;
		}
	}

	[Preserve(AllMembers = true)]
	public class ContentPage1024 : ContentPage
	{
		public ContentPage1024()
		{
			Interlocked.Increment(ref LandingPage1024.Counter);
			System.Diagnostics.Debug.WriteLine("Page: " + LandingPage1024.Counter);

			Content = new ListView
			{
				HasUnevenRows = true,
				ItemsSource = new List<string> { "Entry", "Editor"},
				ItemTemplate = new InputViewDataTemplateSelector(),
				AutomationId = "ListView"
			};
		}

		~ContentPage1024()
		{
			Interlocked.Decrement(ref LandingPage1024.Counter);
			System.Diagnostics.Debug.WriteLine("Page: " + LandingPage1024.Counter);
		}
	}

	[Preserve(AllMembers = true)]
	public class InputViewDataTemplateSelector : DataTemplateSelector
	{
		public DataTemplate EntryTemplate { get; set; }
		public DataTemplate EditorTemplate { get; set; }

		public InputViewDataTemplateSelector()
		{
			EntryTemplate = new DataTemplate(() => new ViewCell { View = new Entry { BackgroundColor = Color.DarkGoldenrod, Text = "Entry" } });
			EditorTemplate = new DataTemplate(() => new ViewCell { View = new Editor { BackgroundColor = Color.Bisque, Text = "Editor" } });
		}

		protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
		{
			switch (item as string)
			{
				case "Entry":
					return EntryTemplate;
				case "Editor":
					return EditorTemplate;
			}

			return null;
		}
	}
}