summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla40704.cs
blob: 1298110fd5048fcd3c49c3bea2c4a53e60cdfcb5 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Text;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
	[Category(UITestCategories.ListView)]
#endif

	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 40704, "Strange duplication of listview headers when collapsing/expanding sections")]
	public class Bugzilla40704 : TestContentPage // or TestMasterDetailPage, etc ...
	{
		ListView listview;
		int count = 2;

		protected override void Init()
		{
			listview = new ListView(ListViewCachingStrategy.RecycleElement)
			{
				AutomationId = "lstMain",
				IsGroupingEnabled = true,
				HasUnevenRows = true,
				GroupHeaderTemplate = new DataTemplate(typeof(GroupHeaderViewCell)),
				ItemTemplate = new DataTemplate(typeof(ItemTestViewCell))
			};

			FillPatientsList();

			var button = new Button()
			{
				Text = "Collapse",
				AutomationId = "btnCollappse"
			};
			listview.Footer = button;
			button.Clicked += Button_Clicked;
			Content = listview;
		}

		void Button_Clicked(object sender, EventArgs e)
		{
			var source = listview.ItemsSource as List<PatientsGroupViewModel>;
			source[count].Toggle();
			count--;
			if (count < 0)
				count = 2;
		}

		private void FillPatientsList()
		{
			const int groupsNumber = 3;
			const int patientsNumber = 10;

			var patientGroups = new List<PatientsGroupViewModel>();
			var random = new Random();

			for (var i = 0; i < groupsNumber; i++)
			{
				var patients = new List<PatientViewModel>();
				for (var j = 0; j < patientsNumber; j++)
				{
					var code = string.Format("{0}-{1}", i, j);
					var length = random.Next(5, 100);
					var strBuilder = new StringBuilder();
					for (int z = 0; z < length; z++)
					{
						strBuilder.Append(code);
						if (z % 7 == 0)
						{
							strBuilder.Append(' ');
						}
					}

					patients.Add(new PatientViewModel(code) { Description = strBuilder.ToString() });
				}

				patientGroups.Add(new PatientsGroupViewModel(patients)
				{
					Title = "Menu - " + i.ToString()
				});

			}

			listview.ItemsSource = patientGroups;
		}

		[Preserve(AllMembers = true)]
		public class GroupHeaderViewCell : ViewCell
		{
			TapGestureRecognizer tapGesture;

			public GroupHeaderViewCell()
			{
				Height = 40;
				var grd = new Grid { BackgroundColor = Color.Aqua, Padding = new Thickness(5, 10) };
				tapGesture = new TapGestureRecognizer();
				tapGesture.Tapped += HeaderCell_OnTapped;
				grd.GestureRecognizers.Add(tapGesture);
				var lbl = new Label { VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.FillAndExpand, TextColor = Color.Black, FontSize = 16 };
				lbl.SetBinding(Label.TextProperty, new Binding("Title"));

				grd.Children.Add(lbl);
				View = grd;
			}

			void HeaderCell_OnTapped(object sender, EventArgs e)
			{
				var cell = (Layout)sender;
				var vm = cell.BindingContext as PatientsGroupViewModel;

				if (vm != null)
				{
					vm.Toggle();
				}
			}
		}

		[Preserve(AllMembers = true)]
		public class ItemTestViewCell : ViewCell
		{
			public ItemTestViewCell()
			{

				var grd = new Grid { BackgroundColor = Color.Yellow };
				var lbl = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, TextColor = Color.Black, FontSize = 16, LineBreakMode = LineBreakMode.WordWrap };
				lbl.SetBinding(Label.TextProperty, new Binding("Description"));
				grd.Children.Add(lbl);
				View = grd;
			}
		}

		[Preserve(AllMembers = true)]
		public class RangeObservableCollection<T> : ObservableCollection<T>
		{
			private bool _suppressNotification = false;

			protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
			{
				if (!_suppressNotification)
					base.OnCollectionChanged(e);
			}

			public void AddRange(IEnumerable<T> list)
			{
				if (list == null)
					throw new ArgumentNullException("list");

				_suppressNotification = true;

				foreach (var item in list)
				{
					Add(item);
				}
				_suppressNotification = false;
				OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
			}
		}

		[Preserve(AllMembers = true)]
		public class PatientsGroupViewModel : RangeObservableCollection<PatientViewModel>
		{
			public bool IsCollapsed { get; private set; }

			public string Title { get; set; }

			private readonly List<PatientViewModel> _patients;

			public PatientsGroupViewModel(List<PatientViewModel> patients)
			{
				_patients = patients;

				UpdateCollection();
			}

			public void Toggle()
			{
				IsCollapsed = !IsCollapsed;

				UpdateCollection();
			}

			private void UpdateCollection()
			{
				if (!IsCollapsed)
				{
					AddRange(_patients);
				}
				else
				{
					Clear();
				}
			}
		}

		[Preserve(AllMembers = true)]
		public class PatientViewModel
		{
			public PatientViewModel(string code)
			{
				Code = code;
			}

			public string Code { get; set; }

			public string Description { get; set; }
		}


#if UITEST
		[Test]
		public void Bugzilla40704HeaderPresentTest()
		{
			RunningApp.WaitForElement("Menu - 0");
		}
		[Test]
		public void Bugzilla40704Test()
		{
			RunningApp.ScrollDownTo("btnCollappse", "lstMain", ScrollStrategy.Gesture, 0.8, timeout: TimeSpan.FromMinutes(2));
			RunningApp.Tap("btnCollappse");
			RunningApp.ScrollDownTo("btnCollappse", "lstMain", ScrollStrategy.Gesture, 0.8, timeout: TimeSpan.FromMinutes(2));
			RunningApp.Tap("btnCollappse");
			RunningApp.ScrollDownTo("btnCollappse", "lstMain", ScrollStrategy.Gesture, 0.8, timeout: TimeSpan.FromMinutes(2));
			RunningApp.Tap("btnCollappse");
			RunningApp.WaitForElement("Menu - 2");
			RunningApp.WaitForElement("Menu - 1");
			RunningApp.WaitForElement("Menu - 0");
		}
#endif
	}
}