summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2411.cs
blob: f32379739d0a2ba55342c612442badb137f78d44 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
´╗┐using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Github, 2411, "ListView.ScrollTo not working in TabbedPage", PlatformAffected.Android)]
	public class Issue2411 : TestTabbedPage
	{
		protected override void Init()
		{
			Children.Add(new XamarinListViewScrollToBugPage1());
			Children.Add(new XamarinListViewScrollToBugPage2());
			Children.Add(new XamarinListViewScrollToBugPage3());
		}

#if UITEST
		[Test]
#if __ANDROID__
		[Ignore("Appearing event is tied to virtualization in TabbedPage for Material")]
#endif
		[Issue(IssueTracker.Github, 2411, "ScrollToPosition.MakeVisible not called every time TabbedPage", PlatformAffected.Android)]
		public void Issue2411ScrollToPositionMakeVisible()
		{
			RunningApp.WaitForElement(q => q.Marked("99 99 99 99 99 99"));
			RunningApp.Screenshot("ScrollTo working correctly");
			RunningApp.Tap(q => q.Marked("Crash in ScrollToPosition.End"));
			RunningApp.Screenshot("On Second Tab");
			RunningApp.WaitForElement(q => q.Marked("2 0 0 0 0 0 0"));
			RunningApp.Tap(q => q.Marked("Scroll To in OnAppearing"));
			RunningApp.Screenshot("On First Tab");
			RunningApp.WaitForElement(q => q.Marked("99 99 99 99 99 99"));

			var listViewBound = RunningApp.Query(q => q.Marked("listView"))[0].Rect;
			Xamarin.Forms.Core.UITests.Gestures.ScrollForElement(RunningApp, "* marked:'0 0 0 0 0 0'", new Xamarin.Forms.Core.UITests.Drag(listViewBound, Xamarin.Forms.Core.UITests.Drag.Direction.TopToBottom, Xamarin.Forms.Core.UITests.Drag.DragLength.Long));
			RunningApp.Screenshot("Scrolled to Top");

			RunningApp.Tap(q => q.Marked("Crash in ScrollToPosition.End"));
			RunningApp.Screenshot("On Second Tab");
			RunningApp.WaitForElement(q => q.Marked("2 0 0 0 0 0 0"));
			RunningApp.Tap(q => q.Marked("Scroll To in OnAppearing"));
			RunningApp.Screenshot("On First Tab");
			RunningApp.WaitForElement(q => q.Marked("99 99 99 99 99 99"));
		}

		[Test]
		[Issue(IssueTracker.Github, 2411, "ScrollToPosition.End crashing in TabbedPage", PlatformAffected.Android)]
		public void Issue2411ScrollToPositionEndCrash()
		{
			RunningApp.Tap(q => q.Marked("Crash in ScrollToPosition.End"));
			RunningApp.Screenshot("On Second Tab");
			RunningApp.Tap(q => q.Marked("Scroll To in OnAppearing"));
			RunningApp.Screenshot("On First Tab");
			RunningApp.Tap(q => q.Marked("Crash in ScrollToPosition.End"));
			RunningApp.Screenshot("On Second Tab Again");
			RunningApp.Tap(q => q.Marked("ScrollToPosition.End End - Not animated"));
			RunningApp.WaitForElement(q => q.Marked("2 99 99 99 99 99 99"));
		}

		[Test]
		[Issue(IssueTracker.Github, 2411, "ScrollToPositon.End crashing in TabbedPage", PlatformAffected.Android)]
		public void Issue2411ScrollToPositionWrongOnUneven()
		{
			RunningApp.Tap(q => q.Marked("Crash in ScrollToPosition.End"));
			RunningApp.Tap(q => q.Marked("Scroll To in OnAppearing Uneven"));

			var dontRun = RunningApp.Query(q => q.Marked(XamarinListViewScrollToBugPage3.DontRun));
			if (dontRun.Length > 0)
				Assert.Inconclusive("Ignored on iOS < 9 until Bugzilla 28277 is resolved.");

			RunningApp.Screenshot("On Third Tab");
			RunningApp.WaitForElement(q => q.Marked("99 99 99 99 99 99"));
		}
#endif
	}

	[Preserve(AllMembers = true)]
	internal class ListObj
	{
		public string Name { get; set; }
	}

	[Preserve(AllMembers = true)]
	public class CellTemplateScrollTo : ViewCell
	{
		public CellTemplateScrollTo()
		{
			Label cellLabel = new Label()
			{
				HorizontalOptions = LayoutOptions.FillAndExpand,
			};

			cellLabel.SetBinding(Label.TextProperty, new Binding("Name", BindingMode.OneWay));

			StackLayout root = new StackLayout()
			{
				Children = {
					cellLabel
				}
			};

			View = root;
		}
	}

	[Preserve(AllMembers = true)]
	public class CellTemplateScrollToUneven : CellTemplateScrollTo
	{
		public CellTemplateScrollToUneven()
		{

			Height = 60 + new Random().Next(10, 100);
		}
	}

	[Preserve(AllMembers = true)]
	public class XamarinListViewScrollToBugPage1 : ContentPage
	{
		ListView _listView;
		ObservableCollection<ListObj> _collection = new ObservableCollection<ListObj>();

		public XamarinListViewScrollToBugPage1()
		{
			Title = "Scroll To in OnAppearing";

			for (int i = 0; i < 100; i++)
			{
				var item = new ListObj { Name = string.Format("{0} {0} {0} {0} {0} {0}", i) };
				_collection.Add(item);
			}

			_listView = new ListView
			{
				ItemsSource = _collection,
				ItemTemplate = new DataTemplate(typeof(CellTemplateScrollTo))
			};

			_listView.AutomationId = "listView";

			Content = new StackLayout
			{
				Children = {
					_listView
				}
			};
		}

		protected override void OnAppearing()
		{
			base.OnAppearing();
			_listView.ScrollTo(_collection.Last(), ScrollToPosition.MakeVisible, false);
		}
	}

	[Preserve(AllMembers = true)]
	public class XamarinListViewScrollToBugPage2 : ContentPage
	{
		ListView _listView;
		ObservableCollection<ListObj> _collection = new ObservableCollection<ListObj>();

		public XamarinListViewScrollToBugPage2()
		{
			Title = "Crash in ScrollToPosition.End";

			for (int i = 0; i < 100; i++)
			{
				var item = new ListObj { Name = string.Format("2 {0} {0} {0} {0} {0} {0}", i) };
				_collection.Add(item);
			}

			_listView = new ListView
			{
				ItemsSource = _collection,
				ItemTemplate = new DataTemplate(typeof(CellTemplateScrollTo))
			};

			var endButton = new Button
			{
				Text = "ScrollToPosition.End End - Not animated",
				Command = new Command(() =>
				{
					_listView.ScrollTo(_collection.Last(), ScrollToPosition.End, false);
				})
			};

			var endButtonAnimated = new Button
			{
				Text = "ScrollToPosition.MakeVisible End - Animated",
				Command = new Command(() =>
				{
					_listView.ScrollTo(_collection.Last(), ScrollToPosition.MakeVisible, true);
				})
			};

			Content = new StackLayout
			{
				Children = {
					endButton,
					endButtonAnimated,
					_listView
				}
			};
		}
	}

	public class XamarinListViewScrollToBugPage3 : ContentPage
	{
		ListView _listView;
		ObservableCollection<ListObj> _collection = new ObservableCollection<ListObj>();
		int _i = 0;
		public const string DontRun = "Don't run";
		public XamarinListViewScrollToBugPage3()
		{
			Title = "Scroll To in OnAppearing Uneven";

			bool runTest = true;
			// This test will fail in iOS < 9 because using ScrollTo with UnevenRows with estimation is currently not working.
			// It did not previously fail because this test used `TakePerformanceHit` to turn off row estimation. However, as
			// that was never a public feature, it was never a valid fix for the test.
			// https://bugzilla.xamarin.com/show_bug.cgi?id=28277
#if !UITEST
				if (App.IOSVersion < 9)
					runTest = false;
#endif

			if (!runTest)
				_collection.Add(new ListObj { Name = DontRun });
			else
			{
				for (_i = 0; _i < 100; _i++)
				{
					var item = new ListObj { Name = string.Format("{0} {0} {0} {0} {0} {0}", _i) };
					_collection.Add(item);
				}
			}

			var btnAdd = new Button
			{
				Text = "Add item",
				WidthRequest = 100
			};
			btnAdd.Clicked += BtnAddOnClicked;

			var btnBottom = new Button
			{
				Text = "Scroll to end",
				WidthRequest = 100
			};
			btnBottom.Clicked += BtnBottomOnClicked;

			var btnPanel = new StackLayout
			{
				Orientation = StackOrientation.Horizontal,
				HorizontalOptions = LayoutOptions.Center,
				Children = {
					btnAdd,
					btnBottom
				}
			};

			_listView = new ListView
			{
				ItemsSource = _collection,
				BackgroundColor = Color.Transparent,
				VerticalOptions = LayoutOptions.FillAndExpand,
				HasUnevenRows = true,
				ItemTemplate = new DataTemplate(typeof(CellTemplateScrollToUneven))
			};
			_listView.ItemTapped += (sender, e) => ((ListView)sender).SelectedItem = null;

			_listView.AutomationId = "listView";

			Content = new StackLayout
			{
				Children = {
					btnPanel,
					_listView
				}
			};
		}

		protected override void OnAppearing()
		{
			base.OnAppearing();
			_listView.ScrollTo(_collection.Last(), ScrollToPosition.MakeVisible, false);
		}

		void BtnBottomOnClicked(object sender, EventArgs e)
		{
			var item = _collection.Last();
			_listView.ScrollTo(item, ScrollToPosition.End, true);
		}

		void BtnAddOnClicked(object sender, EventArgs eventArgs)
		{
			var str = string.Format("Item {0}", _i++);
			var item = new ListObj { Name = string.Format("{0} {0} {0} {0} {0} {0}", _i) };
			_collection.Add(item);

			_listView.ScrollTo(item, ScrollToPosition.End, true);
		}
	}
}