summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2259.cs
blob: d59ef24ff1c712fde277716d330e7c1f1dc8759c (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
using System;
using System.Collections.ObjectModel;

using Xamarin.Forms;
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, 2259, "ListView.ScrollTo crashes app", PlatformAffected.iOS)]
	public class Issue2259 : TestContentPage
	{
		[Preserve (AllMembers = true)]
		public class Person
		{
			public string Name { private set; get; }

			public DateTime Birthday { private set; get; }

			public Color FavoriteColor { private set; get; }

			public Person (string name, DateTime birthday, Color favoriteColor)
			{
				Name = name;
				Birthday = birthday;
				FavoriteColor = favoriteColor;
			}
		};

		int _count = 1;

		protected override void Init ()
		{
			var people = new ObservableCollection<Person> {
				new Person ("Abigail", new DateTime (1975, 1, 15), Color.Aqua),
				new Person ("Bob", new DateTime (1976, 2, 20), Color.Black),
				new Person ("Cathy", new DateTime (1977, 3, 10), Color.Blue),
#pragma warning disable 618
				new Person ("David", new DateTime (1978, 4, 25), Color.Fuschia),
#pragma warning restore 618
			};

			var buttonAdd = new Button {
				Text = "Add",
				HorizontalOptions = LayoutOptions.Start,
				VerticalOptions = LayoutOptions.Center,
			};

			var buttonRemove = new Button {
				Text = "Remove",
				HorizontalOptions = LayoutOptions.Center,
				VerticalOptions = LayoutOptions.Center,
			};

			var buttonScrollToBottom = new Button {
				Text = "Bottom",
				HorizontalOptions = LayoutOptions.Start,
				VerticalOptions = LayoutOptions.Center,
			};

			var buttonStack = new StackLayout {
				Orientation = StackOrientation.Horizontal,
				Children = {
					buttonAdd,
					buttonRemove,
					buttonScrollToBottom,
				}
			};

			var listView = new ListView {
				HorizontalOptions = LayoutOptions.FillAndExpand,
				VerticalOptions = LayoutOptions.FillAndExpand,
				ItemsSource = people,
				ItemTemplate = new DataTemplate (() =>
				{
					var nameLabel = new Label ();
					var birthdayLabel = new Label ();
					var boxView = new BoxView ();

					var stack = new StackLayout {
						Padding = new Thickness (0, 5),
						Orientation = StackOrientation.Horizontal,
						BackgroundColor = Color.Black,
						Children = {
							boxView,
							new StackLayout {
								VerticalOptions = LayoutOptions.Center,
								Spacing = 0,
								Children = {
									nameLabel,
									birthdayLabel
								}
							}
						}
					};

					nameLabel.SetBinding (Label.TextProperty, "Name");
					birthdayLabel.SetBinding (Label.TextProperty, new Binding ("Birthday", BindingMode.OneWay, null, null, "Born {0:d}"));
					boxView.SetBinding (BoxView.ColorProperty, "FavoriteColor");
					stack.SetBinding (BackgroundColorProperty, "BackgroundColor");

					return new ViewCell {
						View = stack
					};
				})
			};

			buttonAdd.Clicked += (sender, e) =>
			{
				var person = new Person (string.Format ("Name {0}", _count++), DateTime.Today, Color.Blue);

				people.Add (person);

				listView.ScrollTo (person, ScrollToPosition.End, true);

			};

			buttonRemove.Clicked += (sender, e) => people.RemoveAt (people.Count - 1);

			buttonScrollToBottom.Clicked += (sender, e) =>
			{
				var person = people[people.Count - 1];

				listView.ScrollTo (person, ScrollToPosition.End, true);
			};

			Padding = new Thickness (10, Device.OnPlatform (20, 0, 0), 10, 5);

			Content = new StackLayout {
				Orientation = StackOrientation.Vertical,
				Children = {
					buttonStack,
					listView,
				}
			};
		}
	
#if UITEST
		[Test]
		[UiTest (typeof(ListView), "ScrollTo")]
		public void Issue2259Tests ()
		{
			for (int i = 0; i < 20; i++) {
				RunningApp.Tap (q => q.Button ("Add"));
				RunningApp.WaitForElement (q => q.Marked ("Name " + (i + 1).ToString ()));
				RunningApp.Screenshot ("Added Cell");
			}
		}
#endif
	}
}