-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Issue1342.cs
173 lines (150 loc) · 4.87 KB
/
Issue1342.cs
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 1342, "[iOS] ListView throws Exception on ObservableCollection.Add/Remove for non visible list view",
PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.ListView)]
[NUnit.Framework.Category(UITestCategories.UwpIgnore)]
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github5000)]
#endif
public class Issue1342 : TestNavigationPage
{
const string add2 = "add2";
const string add3 = "add3";
const string success = "No crash means success";
protected override void Init()
{
PushAsync(new MainPageCode
{
BindingContext = new MainViewModel
{
ViewModel1 = new ListViewModel
{
Items = new ObservableCollection<string>(new[] { $"Click {add2}", $"Click {add3}", success })
},
ViewModel2 = new ListViewModel
{
Items = new ObservableCollection<string>(new[] { "item2.1", "item2.2", "item2.3" })
},
ViewModel3 = new ListViewModel
{
Items = new ObservableCollection<string>()
}
}
});
}
[Preserve(AllMembers = true)]
public partial class MainPageCode : TabbedPage
{
public MainPageCode()
{
ToolbarItems.Add(new Xamarin.Forms.ToolbarItem() { Text = "add1" });
ToolbarItems.Add(new Xamarin.Forms.ToolbarItem() { Text = $"{add2}" });
ToolbarItems.Add(new Xamarin.Forms.ToolbarItem() { Text = $"{add3}" });
ToolbarItems.Add(new Xamarin.Forms.ToolbarItem() { Text = "reload" });
ToolbarItems.Add(new Xamarin.Forms.ToolbarItem() { Text = "visible" });
ToolbarItems[0].SetBinding(ToolbarItem.CommandProperty, "Add1Command");
ToolbarItems[1].SetBinding(ToolbarItem.CommandProperty, "Add2Command");
ToolbarItems[2].SetBinding(ToolbarItem.CommandProperty, "Add3Command");
ToolbarItems[3].SetBinding(ToolbarItem.CommandProperty, "Add4Command");
ToolbarItems[4].SetBinding(ToolbarItem.CommandProperty, "Add5Command");
ListPageCode page = new ListPageCode();
page.SetBinding(ListPageCode.BindingContextProperty, "ViewModel1");
Children.Add(page);
page = new ListPageCode();
page.SetBinding(ListPageCode.BindingContextProperty, "ViewModel2");
Children.Add(page);
page = new ListPageCode();
page.SetBinding(ListPageCode.BindingContextProperty, "ViewModel3");
Children.Add(page);
}
}
[Preserve(AllMembers = true)]
public class MainViewModel
{
void AddItems(ObservableCollection<string> list)
{
list.Add("new item");
}
public MainViewModel()
{
Add1Command = new Command(() => AddItems(ViewModel1.Items));
Add2Command = new Command(() => AddItems(ViewModel2.Items));
Add3Command = new Command(() => AddItems(ViewModel3.Items));
Add4Command = new Command(() =>
{
ViewModel1.ReloadData();
ViewModel2.ReloadData();
ViewModel3.ReloadData();
});
Add5Command = new Command(() =>
{
ViewModel1.ChangeListViewVisability();
ViewModel2.ChangeListViewVisability();
ViewModel3.ReloadData();
});
}
public ListViewModel ViewModel1 { get; set; }
public ListViewModel ViewModel2 { get; set; }
public ListViewModel ViewModel3 { get; set; }
public ICommand Add1Command { get; }
public ICommand Add2Command { get; }
public ICommand Add3Command { get; }
public ICommand Add4Command { get; }
public ICommand Add5Command { get; }
}
[Preserve(AllMembers = true)]
public class ListViewModel : INotifyPropertyChanged
{
public ObservableCollection<string> Items { get; set; }
public bool IsVisible { get; set; } = true;
public event PropertyChangedEventHandler PropertyChanged;
public void ReloadData()
{
Items = new ObservableCollection<string>();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Items)));
}
public void ChangeListViewVisability()
{
IsVisible = !IsVisible;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsVisible)));
}
}
[Preserve(AllMembers = true)]
public partial class ListPageCode : ContentPage
{
public ListPageCode()
{
IconImageSource = "coffee.png";
ListView view = new ListView(ListViewCachingStrategy.RecycleElement);
Content = view;
view.SetBinding(ListView.ItemsSourceProperty, "Items");
view.SetBinding(ListView.IsVisibleProperty, "IsVisible");
}
}
#if UITEST
[Test]
[Ignore("Fails sometimes - needs a better test")]
public void AddingItemsToNonVisibleListViewDoesntCrash()
{
RunningApp.Tap(add2);
RunningApp.Tap(add3);
RunningApp.WaitForElement(success);
}
#endif
}
}