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
|
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Xamarin.PropertyEditing.Reflection;
using Xamarin.PropertyEditing.Tests.MockControls;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Tests
{
[TestFixture]
internal class CollectionPropertyViewModelTests
{
[Test]
public async Task AddType ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
var buttonType = GetTypeInfo (typeof(MockWpfButton));
vm.TypeRequested += (o, e) => {
e.SelectedType = buttonType;
};
await vm.AssignableTypes.Task;
vm.SelectedType = vm.SuggestedTypes.First ();
Assert.That (vm.SuggestedTypes, Contains.Item (buttonType));
}
[Test]
public async Task AddTarget ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
Assert.That (vm.Targets, Is.Not.Empty, "Adding a target failed");
Assume.That (vm.Targets.Single ().Item, Is.InstanceOf (typeof(MockWpfButton)));
}
[Test]
public async Task RemoveTarget ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets, Is.Not.Empty);
vm.SelectedTarget = vm.Targets.First ();
Assert.That (vm.RemoveTargetCommand.CanExecute (null), Is.True);
vm.RemoveTargetCommand.Execute (null);
Assert.That (vm.Targets, Is.Empty);
}
[Test]
public async Task MoveUpCommand ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets.Count, Is.EqualTo (2));
Assume.That (vm.Targets, Is.Not.Empty, "Adding a target failed");
var target = vm.Targets.Skip (1).First ();
vm.SelectedTarget = target;
Assume.That (vm.Targets[0], Is.Not.SameAs (target));
Assert.That (vm.MoveUpCommand.CanExecute (null), Is.True);
vm.MoveUpCommand.Execute (null);
Assert.That (vm.MoveUpCommand.CanExecute (null), Is.False);
Assume.That (vm.SelectedTarget, Is.SameAs (target));
Assert.That (vm.Targets[0], Is.SameAs (target));
}
[Test]
public async Task MoveDownCommand ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets.Count, Is.EqualTo (2));
Assume.That (vm.Targets, Is.Not.Empty, "Adding a target failed");
var target = vm.Targets.First ();
vm.SelectedTarget = target;
Assume.That (vm.Targets[1], Is.Not.SameAs (target));
Assert.That (vm.MoveDownCommand.CanExecute (null), Is.True);
vm.MoveDownCommand.Execute (null);
Assert.That (vm.MoveDownCommand.CanExecute (null), Is.False);
Assume.That (vm.SelectedTarget, Is.SameAs (target));
Assert.That (vm.Targets[1], Is.SameAs (target));
}
[Test]
public async Task ReorderEnablesOnItemAdd ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets.Count, Is.EqualTo (1));
Assume.That (vm.Targets, Is.Not.Empty, "Adding a target failed");
vm.SelectedTarget = vm.Targets.First ();
Assume.That (vm.MoveUpCommand.CanExecute (null), Is.False);
Assume.That (vm.MoveDownCommand.CanExecute (null), Is.False);
bool upChanged = false;
vm.MoveUpCommand.CanExecuteChanged += (sender, args) => upChanged = true;
vm.AddTargetCommand.Execute (null);
Assume.That (vm.SelectedTarget, Is.SameAs (vm.Targets[vm.Targets.Count-1]), "Selected target not expected");
Assert.That (upChanged, Is.True);
Assert.That (vm.MoveUpCommand.CanExecute (null), Is.True);
Assert.That (vm.MoveDownCommand.CanExecute (null), Is.False);
}
[Test]
public async Task SelectOnAdd ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets, Is.Not.Empty, "Adding a target failed");
Assume.That (vm.Targets.Single ().Item, Is.InstanceOf (typeof(MockWpfButton)));
Assert.That (vm.SelectedTarget, Is.SameAs (vm.Targets[0]), "Didn't auto-select first added");
}
[Test]
[Description ("The added item should be inserted after a selected item")]
public async Task AddAfterSelected ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
vm.AddTargetCommand.Execute (null);
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets.Count, Is.EqualTo (3), "Dummy items were not added");
bool changed = false;
var incc = (INotifyCollectionChanged) vm.Targets;
incc.CollectionChanged += (sender, args) => {
changed = true;
Assert.That (args.Action, Is.EqualTo (NotifyCollectionChangedAction.Add));
Assert.That (args.NewStartingIndex, Is.EqualTo (2));
};
vm.SelectedTarget = vm.Targets[1];
vm.AddTargetCommand.Execute (null);
Assert.That (changed, Is.True);
Assert.That (vm.Targets[1].Row, Is.EqualTo (1));
Assert.That (vm.Targets[2].Row, Is.EqualTo (2));
Assert.That (vm.Targets[3].Row, Is.EqualTo (3));
}
[Test]
public async Task SelectPreviousOnRemove ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var obj = new {
Collection = new ArrayList ()
};
var editor = new ReflectionObjectEditor (obj);
var vm = new CollectionPropertyViewModel (platform, editor.Properties.First(), new[] { editor });
await vm.AssignableTypes.Task;
vm.SelectedType = GetTypeInfo (typeof(MockWpfButton));
vm.AddTargetCommand.Execute (null);
vm.AddTargetCommand.Execute (null);
vm.AddTargetCommand.Execute (null);
Assume.That (vm.Targets, Is.Not.Empty);
var newTarget = vm.Targets[1];
var target = vm.Targets[2];
vm.SelectedTarget = target;
Assume.That (vm.RemoveTargetCommand.CanExecute (null), Is.True);
vm.RemoveTargetCommand.Execute (null);
Assume.That (vm.Targets, Does.Not.Contain (target));
Assert.That (vm.SelectedTarget, Is.SameAs (newTarget));
vm.RemoveTargetCommand.Execute (null);
vm.RemoveTargetCommand.Execute (null);
Assert.That (vm.SelectedTarget, Is.Null);
}
[Test]
[Description ("If there's a suggested type (not other type..), it should be auto-selected to start")]
public async Task SuggestedSelected ()
{
TargetPlatform platform = new TargetPlatform (new MockEditorProvider());
var collectionProperty = new Mock<IPropertyInfo> ();
collectionProperty.SetupGet (pi => pi.Type).Returns (typeof(IList));
ITypeInfo objType = GetTypeInfo (typeof(object));
var editor = new Mock<IObjectEditor> ();
editor.Setup (e => e.GetAssignableTypesAsync (collectionProperty.Object, true)).ReturnsAsync (new AssignableTypesResult (new[] { objType }, new[] { objType }));
editor.Setup (e => e.Properties).Returns (new[] { collectionProperty.Object });
var vm = new CollectionPropertyViewModel (platform, collectionProperty.Object, new[] { editor.Object });
await vm.AssignableTypes.Task;
Assert.That (vm.SelectedType, Is.EqualTo (objType));
}
private ITypeInfo GetTypeInfo (Type type)
{
var asm = new AssemblyInfo (type.Assembly.FullName, true);
return new TypeInfo (asm, type.Namespace, type.Name);
}
}
}
|