summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla58645.cs
blob: 87f7d1ba3045a23b70bb93a35f7268034745dded (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
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;

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

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 58645, "[iOS] NRE Thrown When ListView Items Are Replaced By Items With a Different Template", PlatformAffected.iOS)]
	public class Bugzilla58645 : TestContentPage
	{
		const string ButtonId = "button";
		ObservableCollection<string> Items { get; set; }

		protected override void Init()
		{
			Items = new ObservableCollection<string> { "Item 1A", "Item 2A", "Item 3A" };

			var myListView = new ListView
			{
				HasUnevenRows = true,
				ItemsSource = Items,
				ItemTemplate = new LayoutTemplateSelector
				{
					LayoutA = new DataTemplate(typeof(LayoutA)),
					LayoutB = new DataTemplate(typeof(LayoutB))
				}
			};

			var switchBtn = new Button
			{
				Text = "Switch Items",
				AutomationId = ButtonId,
				Command = new Command(() =>
				{
					Items.Clear();
					Items.Add("Item 1B");
				})
			};

			Content = new StackLayout
			{
				Children =
				{
					new Label { Text = "Tap the 'Switch Items' button. If the app does not crash, this test has passed." },
					switchBtn,
					myListView
				}
			};
		}

		public class LayoutA : ViewCell
		{
			public LayoutA()
			{
				var layout = new Grid
				{
					Padding = new Thickness(14),
					ColumnDefinitions =
				{
					new ColumnDefinition { Width = GridLength.Auto },
					new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
				},
					RowDefinitions =
				{
					new RowDefinition { Height = GridLength.Auto }
				}
				};

				var mainLabel = new Label();
				mainLabel.SetBinding(Label.TextProperty, ".");

				var sw = new Switch();
				layout.Children.Add(mainLabel, 0, 0);
				layout.Children.Add(sw, 2, 0);
				View = layout;
			}
		}

		public class LayoutB : ViewCell
		{
			public LayoutB()
			{
				var layout = new Grid
				{
					Padding = new Thickness(14),
					ColumnDefinitions =
				{
					new ColumnDefinition { Width = GridLength.Auto },
					new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
				},
					RowDefinitions =
				{
					new RowDefinition { Height = GridLength.Auto }
				}
				};

				var mainLabel = new Label();
				mainLabel.SetBinding(Label.TextProperty, ".");

				var secondLabel = new Label
				{
					Text = "B"
				};

				layout.Children.Add(mainLabel, 0, 0);
				layout.Children.Add(secondLabel, 2, 0);
				View = layout;
			}
		}

		public class LayoutTemplateSelector : DataTemplateSelector
		{
			public DataTemplate LayoutA { get; set; }
			public DataTemplate LayoutB { get; set; }

			protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
			{
				if (item == null)
					return LayoutA;

				var text = (string)item;
				return text.Contains("A") ? LayoutA : LayoutB;
			}
		}

#if UITEST
		[Test]
		public void Bugzilla58645Test()
		{
			RunningApp.WaitForElement(q => q.Marked(ButtonId));
			RunningApp.Tap(q => q.Marked(ButtonId));
		}
#endif
	}
}