summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla56896.cs
blob: 1bc60418f7477bb036370b90dd09bc0191c71b8d (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System;
using System.Diagnostics;

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

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 56896, "ListViews for lists with many elements regressed in performance on iOS", PlatformAffected.iOS)]
	public class Bugzilla56896 : TestContentPage
	{
		const string Instructions = "The number in blue is the number of constructor calls. The number in red is the initial load time in milliseconds.";
		const string ConstructorCountId = "constructorCount";
		const string TimeId = "time";

		[Preserve(AllMembers = true)]
		class MyViewModel : INotifyPropertyChanged
		{
			int _constructorCallCount;

			public int ConstructorCallCount
			{
				get { return _constructorCallCount; }
				set
				{
					_constructorCallCount = value;
					OnPropertyChanged();
				}
			}

			public event PropertyChangedEventHandler PropertyChanged;

			protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
			{
				PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		[Preserve(AllMembers = true)]
		class Fizz : ViewCell
		{
			readonly MyViewModel _vm;

			Label myLabel;
			public Fizz(MyViewModel vm)
			{
				_vm = vm;

				vm.ConstructorCallCount++;

				Height = 30;

				myLabel = new Label { Text = "fizz" };
				View = myLabel;
			}
			~Fizz()
			{
				_vm.ConstructorCallCount--;
			}
		}

		[Preserve(AllMembers = true)]
		class Buzz : ViewCell
		{
			readonly MyViewModel _vm;

			Label myLabel;
			public Buzz(MyViewModel vm)
			{
				_vm = vm;

				vm.ConstructorCallCount++;

				Height = 50;

				myLabel = new Label { Text = "buzz" };
				View = myLabel;
			}
			~Buzz()
			{
				_vm.ConstructorCallCount--;
			}
		}

		[Preserve(AllMembers = true)]
		class Fizzbuzz : ViewCell
		{
			readonly MyViewModel _vm;

			Label myLabel;
			public Fizzbuzz(MyViewModel vm)
			{
				_vm = vm;

				vm.ConstructorCallCount++;

				Height = 150;

				myLabel = new Label { Text = "fizzbuzz" };
				View = myLabel;
			}
			~Fizzbuzz()
			{
				_vm.ConstructorCallCount--;
			}
		}

		[Preserve(AllMembers = true)]
		class Number : ViewCell
		{
			readonly MyViewModel _vm;

			Label myLabel;
			public Number(MyViewModel vm)
			{
				_vm = vm;

				vm.ConstructorCallCount++;

				Height = 44;

				myLabel = new Label();
				myLabel.SetBinding(Label.TextProperty, ".");
				View = myLabel;
			}
			~Number()
			{
				_vm.ConstructorCallCount--;
			}
		}

		class MyDataTemplateSelector : DataTemplateSelector
		{
			DataTemplate _fizzbuzz;
			DataTemplate _fizz;
			DataTemplate _buzz;
			DataTemplate _number;

			public MyDataTemplateSelector(MyViewModel vm)
			{
				_fizzbuzz = new DataTemplate(() => new Fizzbuzz(vm));
				_fizz = new DataTemplate(() => new Fizz(vm));
				_buzz = new DataTemplate(() => new Buzz(vm));
				_number = new DataTemplate(() => new Number(vm));
			}

			protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
			{
				int number = (int)item;

				if (number % 15 == 0)
					return _fizzbuzz;
				else if (number % 5 == 0)
					return _buzz;
				else if (number % 3 == 0)
					return _fizz;
				else
					return _number;
			}
		}


		Label _timeLabel = new Label { TextColor = Color.Purple, AutomationId = TimeId };
		Stopwatch _timer = new Stopwatch();
		ListView _listView;
		protected override void Init()
		{
			_timer.Start();
			var vm = new MyViewModel();

			BindingContext = vm;

			var label = new Label { TextColor = Color.Blue, AutomationId = ConstructorCountId };
			label.SetBinding(Label.TextProperty, nameof(vm.ConstructorCallCount));

			_listView = new ListView(ListViewCachingStrategy.RecycleElement)
			{
				HasUnevenRows = true,
				// Set the RowHeight to enable optimal performance and minimal constructor calls.
				// It will still use the specified Cell heights on final measure.
				// Note, however, that doing this negates the fix for Bugzilla 43313, so if user expects
				// to add items to the bottom of this list and scroll smoothly, user should omit the RowHeight
				// and rely solely on the Cell heights. This will cause each row to be constructed at least once,
				// but it will allow the ListView to estimate the height properly for smooth scrolling.
				// Also note that performance will degrade if the first cell does not have a specified height or
				// if most of the cells do not have a specified height. It is recommended to specify a height on all
				// or none of the cells when possible.
				RowHeight = 50, 
				ItemsSource = Enumerable.Range(1, 5001),
				ItemTemplate = new MyDataTemplateSelector(vm)
			};
			Content = new StackLayout { Children = { new Label { Text = Instructions }, label, _timeLabel, _listView } };
		}

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

			_timer.Stop();
			_timeLabel.Text = _timer.ElapsedMilliseconds.ToString();
			_timer.Reset();
		}

#if UITEST
		[Test]
		public void Bugzilla56896Test()
		{
			RunningApp.WaitForElement(q => q.Marked(Instructions));
			var count = int.Parse(RunningApp.Query(q => q.Marked(ConstructorCountId))[0].Text);
			Assert.IsTrue(count < 100); // Failing test makes ~15000 constructor calls
			var time = int.Parse(RunningApp.Query(q => q.Marked(TimeId))[0].Text);
			Assert.IsTrue(count < 2000); // Failing test takes ~4000ms
		}
#endif
	}
}