-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Issue3652.cs
152 lines (128 loc) · 3.63 KB
/
Issue3652.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
using System.Collections.ObjectModel;
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
{
#if UITEST
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github5000)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 3652, "Loses the correct reference to the cell after adding and removing items to a ListView", PlatformAffected.UWP)]
public class Issue3652 : TestContentPage
{
MainPageViewModel model = new MainPageViewModel();
protected override void Init()
{
BindingContext = model;
Content = new StackLayout
{
Children =
{
new Label
{
Text = "Remove the items using the context menu. Then add 3 more items and try to delete them as well. " +
"If all items are deleted successfully, then the test is passed.",
BackgroundColor = Color.Aqua
},
new Button {
Command = model.AddListItemCommand,
Text = "Add an item"
},
new StackLayout
{
Children = {
new ListView
{
ItemsSource = model.ItemCollection,
ItemTemplate = new DataTemplate (typeof(ICell))
}
}
}
}
};
}
[Preserve(AllMembers = true)]
public class ICell : ViewCell
{
public ICell()
{
var label = new Label();
label.SetBinding(Label.TextProperty, "Description");
label.AutomationId = "pandabear";
var menu = new MenuItem { Text = "Remove" };
menu.Command = new Command(() => ((ListItemViewModel)BindingContext).Remove.Execute((this, BindingContext)));
ContextActions.Add(menu);
var stack = new StackLayout
{
Children =
{
label
}
};
View = stack;
}
}
[Preserve(AllMembers = true)]
public class MainPageViewModel
{
int newItemNumber = 1;
public MainPageViewModel()
{
MessagingCenter.Subscribe<ListItemViewModel, ListItemViewModel>(this, "Remove", (sender, arg) => RemoveAnItem(arg));
AddListItemCommand = new Command(AddListItem);
for (int i = 0; i < 3; i++)
AddListItem();
}
public ObservableCollection<ListItemViewModel> ItemCollection { get; set; } = new ObservableCollection<ListItemViewModel>();
public Command AddListItemCommand { get; set; }
void RemoveAnItem(ListItemViewModel item) => ItemCollection.Remove(item);
void AddListItem()
{
ItemCollection.Add(new ListItemViewModel()
{
Number = newItemNumber++
});
}
}
[Preserve(AllMembers = true)]
public class ListItemViewModel
{
public int Number { get; set; }
public string Description => $"Remove me using the context menu. #{Number}";
public Command Remove =>
new Command(() => MessagingCenter.Send(this, "Remove", this));
}
#if UITEST && !__WINDOWS__
[Test]
public void TestRemovingContextMenuItems()
{
for (int i = 1; i <= 3; i++)
{
string searchFor = $"Remove me using the context menu. #{i}";
RunningApp.WaitForElement(searchFor);
RunningApp.ActivateContextMenu(searchFor);
RunningApp.WaitForElement(c => c.Marked("Remove"));
RunningApp.Tap(c => c.Marked("Remove"));
}
for (int i = 4; i <= 6; i++)
{
RunningApp.Tap("Add an item");
string searchFor = $"Remove me using the context menu. #{i}";
RunningApp.ActivateContextMenu(searchFor);
RunningApp.WaitForElement(c => c.Marked("Remove"));
RunningApp.Tap(c => c.Marked("Remove"));
}
for (int i = 1; i <= 6; i++)
{
string searchFor = $"Remove me using the context menu. #{i}";
RunningApp.WaitForNoElement(c => c.Marked("Remove"));
}
}
#endif
}
}