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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.Reflection;
using Xamarin.PropertyEditing.Tests.MockControls;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Tests
{
[TestFixture]
internal class CreateBindingViewModelTests
{
[SetUp]
public void Setup ()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
this.syncContext = new AsyncSynchronizationContext ();
SynchronizationContext.SetSynchronizationContext (this.syncContext);
}
private Exception unhandled;
private void CurrentDomainOnUnhandledException (object sender, UnhandledExceptionEventArgs e)
{
this.unhandled = e.ExceptionObject as Exception;
}
[TearDown]
public void TearDown ()
{
this.syncContext.WaitForPendingOperationsToComplete ();
SynchronizationContext.SetSynchronizationContext (null);
AppDomain.CurrentDomain.UnhandledException -= CurrentDomainOnUnhandledException;
if (this.unhandled != null) {
var ex = this.unhandled;
this.unhandled = null;
Assert.Fail ("Unhandled exception: {0}", ex);
}
}
[Test]
public void PropertyDisplayType ()
{
var target = new object();
const string propName = "propertyName";
var property = GetBasicProperty (propName);
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new[] { property.Object });
editor.SetupGet (e => e.Target).Returns (target);
editor.SetupGet (e => e.TargetType).Returns (typeof(object).ToTypeInfo ());
var editorProvider = new MockEditorProvider (editor.Object);
var bpmock = new Mock<IBindingProvider> ();
var vm = new CreateBindingViewModel (new TargetPlatform (editorProvider, bpmock.Object), editor.Object, property.Object);
Assert.That (vm.PropertyDisplay, Contains.Substring ("Object"));
Assert.That (vm.PropertyDisplay, Contains.Substring (propName));
}
[Test]
public void PropertyDisplayNameable ()
{
var target = new object ();
const string propName = "propertyName";
var property = GetBasicProperty (propName);
var editor = GetBasicEditor (target, property.Object);
const string objName = "objName";
var nameable = editor.As<INameableObject> ();
nameable.Setup (n => n.GetNameAsync ()).ReturnsAsync (objName);
var editorProvider = new MockEditorProvider (editor.Object);
var bpmock = new Mock<IBindingProvider> ();
var vm = new CreateBindingViewModel (new TargetPlatform (editorProvider, bpmock.Object), editor.Object, property.Object);
Assert.That (vm.PropertyDisplay, Does.Not.Contains ("Object"));
Assert.That (vm.PropertyDisplay, Contains.Substring (objName));
Assert.That (vm.PropertyDisplay, Contains.Substring (propName));
}
[Test]
public async Task BindingSources ()
{
var target = new object ();
var property = GetBasicProperty ();
var editor = GetBasicEditor (target, property.Object);
var editorProvider = new MockEditorProvider (editor.Object);
var sources = new[] {
new BindingSource ("Short Description", BindingSourceType.Object, "Short Description"),
new BindingSource ("Long Description", BindingSourceType.Object, "Long Description"),
};
var bpmock = new Mock<IBindingProvider> ();
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (sources);
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[0], target)).ReturnsAsync (new[] { new object (), new object () });
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[1], target)).ReturnsAsync (new[] { new object () });
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new Resource[0]);
var vm = new CreateBindingViewModel (new TargetPlatform (editorProvider, bpmock.Object), editor.Object, property.Object);
Assert.That (vm.BindingSources, Is.Not.Null);
var requested = await vm.BindingSources.Task;
CollectionAssert.AreEqual (sources, requested);
Assert.That (vm.SelectedBindingSource, Is.EqualTo (sources[0]));
}
[Test]
public async Task BindingSourceObjectRoots ()
{
var target = new object ();
var property = GetBasicProperty ();
var editor = GetBasicEditor (target, property.Object);
var editorProvider = new MockEditorProvider (editor.Object);
var sources = new[] {
new BindingSource ("Short Description", BindingSourceType.Object, "Short Description"),
new BindingSource ("Long Description", BindingSourceType.Object, "Long Description"),
};
var shortRoots = new[] { new object (), new object () };
var bpmock = new Mock<IBindingProvider> ();
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (sources);
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[0], target)).ReturnsAsync (shortRoots);
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[1], target)).ReturnsAsync (new[] { new object () });
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new Resource[0]);
var vm = new CreateBindingViewModel (new TargetPlatform (editorProvider, bpmock.Object), editor.Object, property.Object);
await vm.BindingSources.Task;
Assume.That (vm.SelectedBindingSource, Is.EqualTo (sources[0]));
bpmock.Verify (bp => bp.GetRootElementsAsync (sources[0], target));
IReadOnlyList<ObjectTreeElement> roots = await vm.ObjectElementRoots.Task;
Assert.That (roots.Count, Is.EqualTo (2), "Unexpected number of roots");
CollectionAssert.AreEqual (roots.Select (r => r.Editor.Target), shortRoots);
}
[Test]
[Description ("If the source has a description, we're on object type source and it's the only one, use a long description")]
public async Task ShowLongDescription ()
{
var sources = new[] {
new BindingSource ("Short Description", BindingSourceType.Object, "Short Description"),
new BindingSource ("Long Description", BindingSourceType.SingleObject, "Long Description"),
};
var vm = CreateBasicViewModel (sources: sources);
await vm.BindingSources.Task;
Assume.That (vm.SelectedBindingSource, Is.EqualTo (sources[0]));
Assume.That (vm.ShowObjectSelector, Is.True, "Object selector should be showing");
Assert.That (vm.ShowLongDescription, Is.False);
vm.SelectedBindingSource = sources[1];
await vm.ObjectElementRoots.Task;
Assert.That (vm.ShowLongDescription, Is.True);
Assert.That (vm.ShowObjectSelector, Is.False);
}
[Test]
public async Task ValueConverters ()
{
var target = new object ();
var property = GetBasicProperty ();
var editor = GetBasicEditor (target, property.Object);
const string objName = "objName";
var nameable = editor.As<INameableObject> ();
nameable.Setup (n => n.GetNameAsync ()).ReturnsAsync (objName);
var editorProvider = new MockEditorProvider (editor.Object);
var sources = new[] {
new BindingSource ("Short Description", BindingSourceType.Object, "Short Description"),
new BindingSource ("Long Description", BindingSourceType.Object, "Long Description"),
};
var visi = new Resource ("BooleanToVisibilityConverter");
var bpmock = new Mock<IBindingProvider> ();
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (sources);
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[0], target)).ReturnsAsync (new[] { new object (), new object () });
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[1], target)).ReturnsAsync (new[] { new object () });
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new [] { visi });
var vm = new CreateBindingViewModel (new TargetPlatform (editorProvider, bpmock.Object), editor.Object, property.Object);
Assert.That (vm.ValueConverters, Is.Not.Null);
await vm.ValueConverters.Task;
Assert.That (vm.ValueConverters.Value, Contains.Item (visi));
Assert.That (vm.ValueConverters.Value.Count, Is.EqualTo (3)); // visi, No Converter, Request Converter
}
[Test]
public async Task RequestValueConverter ()
{
var target = new object();
var vm = CreateBasicViewModel (target: target);
Assume.That (vm.ValueConverters, Is.Not.Null);
await vm.ValueConverters.Task;
Assume.That (vm.SelectedValueConverter, Is.EqualTo (vm.ValueConverters.Value[0]));
Assume.That (vm.ValueConverters.Value.Count, Is.EqualTo (2));
const string name = "NewConverter";
bool requested = false;
vm.CreateValueConverterRequested += (o, e) => {
e.ConverterType = typeof(object).ToTypeInfo ();
e.Name = name;
e.Source = MockResourceProvider.ApplicationResourcesSource;
requested = true;
};
Assume.That (vm.ValueConverters.Value, Is.InstanceOf (typeof(INotifyCollectionChanged)));
object newItem = null;
((INotifyCollectionChanged) vm.ValueConverters.Value).CollectionChanged += (o, e) => {
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Count == 1) {
newItem = e.NewItems[0];
Assert.That (e.NewStartingIndex, Is.EqualTo (1), "New converter was not added below none and above request");
}
};
int selectedChanged = 0;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof(CreateBindingViewModel.SelectedValueConverter))
selectedChanged++;
};
vm.SelectedValueConverter = vm.ValueConverters.Value[1];
Assert.That (requested, Is.True);
Assert.That (selectedChanged, Is.EqualTo (2), "SelectedValueConverter did not fire INPC for request and result changes");
Assert.That (newItem, Is.Not.Null);
Assert.That (vm.SelectedValueConverter, Is.EqualTo (newItem));
}
[Test]
public async Task RequestValueConverterCanceled ()
{
var vm = CreateBasicViewModel ();
Assume.That (vm.ValueConverters, Is.Not.Null);
await vm.ValueConverters.Task;
Assume.That (vm.SelectedValueConverter, Is.EqualTo (vm.ValueConverters.Value[0]));
Assume.That (vm.ValueConverters.Value.Count, Is.EqualTo (2));
bool requested = false;
vm.CreateValueConverterRequested += (o, e) => {
requested = true;
};
vm.SelectedValueConverter = vm.ValueConverters.Value[1];
Assert.That (requested, Is.True);
Assert.That (vm.SelectedValueConverter, Is.EqualTo (vm.ValueConverters.Value[0]),
"SelectedValueConverter wasn't set back to No Converter after canceled request");
}
[Test]
public async Task PropertyRoot ()
{
var target = new object ();
var property = new Mock<IPropertyInfo> ();
property.SetupGet (p => p.ValueSources).Returns (ValueSources.Local | ValueSources.Binding);
property.SetupGet (p => p.Type).Returns (typeof (object));
property.SetupGet (p => p.Name).Returns ("name");
property.SetupGet (p => p.RealType).Returns (typeof (object).ToTypeInfo ());
property.SetupGet (p => p.CanWrite).Returns (true);
var editor = GetBasicEditor (target, property.Object);
var controlTarget = new MockWpfControl ();
var controlEditor = new MockObjectEditor (controlTarget);
var provider = new MockEditorProvider (controlEditor);
var source = new BindingSource ("Control", BindingSourceType.Object);
var bpmock = new Mock<IBindingProvider> ();
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (new[] { source });
bpmock.Setup (bp => bp.GetRootElementsAsync (source, target)).ReturnsAsync (new[] { controlTarget });
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new Resource[0]);
var vm = new CreateBindingViewModel (new TargetPlatform (provider, bpmock.Object), editor.Object, property.Object);
Assume.That (vm.SelectedBindingSource, Is.EqualTo (source));
Assert.That (vm.PropertyRoot, Is.Not.Null);
await vm.PropertyRoot.Task;
var targetType = typeof(MockWpfControl).ToTypeInfo ();
Assert.That (vm.PropertyRoot.Value.TargetType, Is.EqualTo (targetType));
CollectionAssert.AreEqual (controlEditor.Properties, vm.PropertyRoot.Value.Children.Select (te => te.Property));
}
[Test]
public async Task PropertyRootChildren ()
{
var target = new object();
var property = new Mock<IPropertyInfo> ();
property.SetupGet (p => p.ValueSources).Returns (ValueSources.Local | ValueSources.Binding);
property.SetupGet (p => p.Type).Returns (typeof (object));
property.SetupGet (p => p.Name).Returns ("name");
property.SetupGet (p => p.RealType).Returns (typeof (object).ToTypeInfo ());
property.SetupGet (p => p.CanWrite).Returns (true);
var editor = GetBasicEditor (target, property.Object);
var controlTarget = new MockWpfControl();
var controlEditor = new MockObjectEditor (controlTarget);
var provider = new MockEditorProvider (controlEditor);
var source = new BindingSource ("Control", BindingSourceType.Object);
var bpmock = new Mock<IBindingProvider> ();
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (new[] { source });
bpmock.Setup (bp => bp.GetRootElementsAsync (source, target)).ReturnsAsync (new[] { controlTarget });
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new Resource[0]);
var vm = new CreateBindingViewModel (new TargetPlatform (provider, bpmock.Object), editor.Object, property.Object);
Assume.That (vm.SelectedBindingSource, Is.EqualTo (source));
Assume.That (vm.PropertyRoot, Is.Not.Null);
await vm.PropertyRoot.Task;
var childrenProperty = controlEditor.Properties.First (p => p.Type == typeof(CommonThickness));
var element = vm.PropertyRoot.Value.Children.First (p => Equals (p.Property, childrenProperty));
Assert.That (element.Children, Is.Not.Null);
await element.Children.Task;
var expected = await provider.GetPropertiesForTypeAsync (typeof(CommonThickness).ToTypeInfo ());
CollectionAssert.AreEqual (expected, element.Children.Value.Select (te => te.Property));
}
[Test]
public async Task Path ()
{
var target = new object ();
var property = new Mock<IPropertyInfo> ();
property.SetupGet (p => p.ValueSources).Returns (ValueSources.Local | ValueSources.Binding);
property.SetupGet (p => p.Type).Returns (typeof (object));
property.SetupGet (p => p.Name).Returns ("name");
property.SetupGet (p => p.RealType).Returns (typeof (object).ToTypeInfo ());
property.SetupGet (p => p.CanWrite).Returns (true);
var editor = GetBasicEditor (target, property.Object);
var controlTarget = new MockWpfControl ();
var controlEditor = new MockObjectEditor (controlTarget);
var provider = new MockEditorProvider (controlEditor);
var source = new BindingSource ("Control", BindingSourceType.Object);
var bpmock = new Mock<IBindingProvider> ();
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (new[] { source });
bpmock.Setup (bp => bp.GetRootElementsAsync (source, target)).ReturnsAsync (new[] { controlTarget });
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new Resource[0]);
var vm = new CreateBindingViewModel (new TargetPlatform (provider, bpmock.Object), editor.Object, property.Object);
Assume.That (vm.SelectedBindingSource, Is.EqualTo (source));
Assume.That (vm.PropertyRoot, Is.Not.Null);
await vm.PropertyRoot.Task;
var element = vm.PropertyRoot.Value.Children.First (p => p.Property.Type == typeof(CommonThickness));
await element.Children.Task;
var sub = element.Children.Value.First ();
vm.SelectedPropertyElement = sub;
Assert.That (vm.Path, Is.EqualTo ($"{element.Property.Name}.{sub.Property.Name}"));
}
[Test]
public async Task SelectedBindingSource ()
{
BindingSource[] sources = new[] {
new BindingSource ("First", BindingSourceType.Object),
new BindingSource ("Second", BindingSourceType.Resource),
new BindingSource ("Third", BindingSourceType.Type),
};
var vm = CreateBasicViewModel (sources);
while (vm.SelectedObjects.Count == 0) {
await Task.Delay (1);
}
var binding = (MockBinding)vm.SelectedObjects.First ();
Assert.That (vm.SelectedBindingSource, Is.EqualTo (sources[0]));
Assert.That (binding.Source, Is.EqualTo (sources[0]), "Backing binding object property didn't update");
bool propertyChanged = false, objectRootsChanged = false, resourceRootsChanged = false, typeRootsChanged = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof(CreateBindingViewModel.SelectedBindingSource))
propertyChanged = true;
else if (e.PropertyName == nameof(CreateBindingViewModel.ObjectElementRoots))
objectRootsChanged = true;
else if (e.PropertyName == nameof(CreateBindingViewModel.SourceResources))
resourceRootsChanged = true;
else if (e.PropertyName == nameof(CreateBindingViewModel.TypeSelector))
typeRootsChanged = true;
};
vm.SelectedBindingSource = sources[1];
Assert.That (propertyChanged, Is.True, "INPC did not fire for SelectedBindingSource");
Assert.That (binding.Source, Is.EqualTo (sources[1]), "Backing binding object property didn't update");
Assert.That (resourceRootsChanged, Is.True, "SourceResources did not update when selected");
Assert.That (vm.SourceResources, Is.Not.Null);
Assert.That (objectRootsChanged, Is.False);
Assert.That (typeRootsChanged, Is.False);
propertyChanged = objectRootsChanged = resourceRootsChanged = typeRootsChanged = false;
vm.SelectedBindingSource = sources[2];
Assert.That (propertyChanged, Is.True, "INPC did not fire for SelectedBindingSource");
Assert.That (binding.Source, Is.EqualTo (sources[2]), "Backing binding object property didn't update");
Assert.That (resourceRootsChanged, Is.False);
Assert.That (objectRootsChanged, Is.False);
Assert.That (typeRootsChanged, Is.True, "TypeSelector didn't update when selected");
Assert.That (vm.TypeSelector, Is.Not.Null);
propertyChanged = objectRootsChanged = resourceRootsChanged = typeRootsChanged = false;
vm.SelectedBindingSource = sources[0];
Assert.That (propertyChanged, Is.True, "INPC did not fire for SelectedBindingSource");
Assert.That (binding.Source, Is.EqualTo (sources[0]), "Backing binding object property didn't update");
Assert.That (resourceRootsChanged, Is.False);
Assert.That (objectRootsChanged, Is.True, "ObjectElementRoots didn't update when selected");
Assert.That (vm.ObjectElementRoots, Is.Not.Null);
Assert.That (typeRootsChanged, Is.False);
}
[Test]
public async Task ShowSourceParameterSelectors ()
{
BindingSource[] sources = new[] {
new BindingSource ("First", BindingSourceType.Object),
new BindingSource ("Second", BindingSourceType.Resource),
new BindingSource ("Third", BindingSourceType.Type),
};
var vm = CreateBasicViewModel (sources);
while (vm.SelectedObjects.Count == 0) {
await Task.Delay (1);
}
Assume.That (vm.SelectedBindingSource, Is.EqualTo (sources[0]));
bool objectChanged = false, resourceChanged = false, typeChanged = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof (CreateBindingViewModel.ShowObjectSelector))
objectChanged = true;
else if (e.PropertyName == nameof (CreateBindingViewModel.ShowResourceSelector))
resourceChanged = true;
else if (e.PropertyName == nameof (CreateBindingViewModel.ShowTypeSelector))
typeChanged = true;
};
vm.SelectedBindingSource = sources[1];
Assert.That (resourceChanged, Is.True);
Assert.That (vm.ShowResourceSelector, Is.True);
Assert.That (objectChanged, Is.True, "Did not signal old selector changed");
Assert.That (vm.ShowObjectSelector, Is.False);
Assert.That (vm.ShowTypeSelector, Is.False);
objectChanged = resourceChanged = typeChanged = false;
vm.SelectedBindingSource = sources[2];
Assert.That (resourceChanged, Is.True);
Assert.That (vm.ShowResourceSelector, Is.False);
Assert.That (objectChanged, Is.True, "Did not signal old selector changed");
Assert.That (vm.ShowObjectSelector, Is.False);
Assert.That (vm.ShowTypeSelector, Is.True);
objectChanged = resourceChanged = typeChanged = false;
vm.SelectedBindingSource = sources[0];
Assert.That (vm.ShowResourceSelector, Is.False);
Assert.That (objectChanged, Is.True);
Assert.That (vm.ShowObjectSelector, Is.True);
Assert.That (typeChanged, Is.True, "Did not signal old selector changed");
Assert.That (vm.ShowTypeSelector, Is.False);
}
[Test]
public async Task ResourceRoots ()
{
object target = new object();
var property = GetBasicProperty ();
var editor = GetBasicEditor (target, property.Object);
var resources = new MockResourceProvider ();
var bindings = GetBasicBindingProvider (target, property.Object);
var source = new BindingSource ("Resources", BindingSourceType.Resource);
bindings.Setup (bp => bp.GetBindingSourcesAsync (target, property.Object)).ReturnsAsync (new[] { source });
bindings.Setup (bp => bp.GetResourcesAsync (source, target))
.Returns<BindingSource,object> (async (bs, t) => {
var rs = await resources.GetResourcesAsync (target, CancellationToken.None);
return rs.ToLookup (r => r.Source);
});
var vm = new CreateBindingViewModel (
new TargetPlatform (new MockEditorProvider (editor.Object), resources, bindings.Object), editor.Object,
property.Object);
Assume.That (vm.SelectedBindingSource, Is.EqualTo (source));
Assert.That (vm.SourceResources, Is.Not.Null);
await vm.SourceResources.Task;
Assert.That (vm.SourceResources.Value.First().Key, Is.EqualTo (DefaultResourceSources[0]));
}
[Test]
public async Task ResourceProperties ()
{
object target = new object ();
var property = GetBasicProperty ();
var editor = GetBasicEditor (target, property.Object);
var resources = new MockResourceProvider ();
var source = new BindingSource ("Resources", BindingSourceType.Resource);
var bindings = GetBasicBindingProvider (target, property.Object, sources: new [] { source });
bindings.Setup (bp => bp.GetResourcesAsync (source, target))
.Returns<BindingSource, object> (async (bs, t) => {
var rs = await resources.GetResourcesAsync (target, CancellationToken.None);
return rs.ToLookup (r => r.Source);
});
var vm = new CreateBindingViewModel (
new TargetPlatform (new MockEditorProvider (editor.Object), resources, bindings.Object), editor.Object,
property.Object);
Assume.That (vm.SelectedBindingSource, Is.EqualTo (source));
Assume.That (vm.SourceResources, Is.Not.Null);
await vm.SourceResources.Task;
while (vm.SelectedObjects.Count == 0) {
await Task.Delay (1);
}
var binding = (MockBinding)vm.SelectedObjects.First();
vm.SelectedResource = vm.SourceResources.Value.First ().OfType<Resource<CommonSolidBrush>>().First ();
Assert.That (binding.SourceParameter, Is.EqualTo (vm.SelectedResource));
Assume.That (vm.PropertyRoot, Is.Not.Null);
await vm.PropertyRoot.Task;
Assert.That (vm.PropertyRoot.Value.TargetType, Is.EqualTo (typeof(CommonSolidBrush).ToTypeInfo ()));
CollectionAssert.AreEqual (ReflectionEditorProvider.GetPropertiesForType (typeof(CommonSolidBrush)),
vm.PropertyRoot.Value.Children.Select (pe => pe.Property));
}
[Test]
public async Task Types ()
{
object target = new object ();
var property = GetBasicProperty ();
var editor = GetBasicEditor (target, property.Object);
var resources = new MockResourceProvider ();
var source = new BindingSource ("Resources", BindingSourceType.Type);
var type = typeof(MockSampleControl).ToTypeInfo ();
var bindings = GetBasicBindingProvider (target, property.Object, sources: new[] { source });
bindings.Setup (bp => bp.GetSourceTypesAsync (source, target))
.ReturnsAsync (new AssignableTypesResult (new[] { type }));
var provider = new MockEditorProvider (editor.Object);
var vm = new CreateBindingViewModel (new TargetPlatform (provider, resources, bindings.Object), editor.Object, property.Object);
Assume.That (vm.SelectedBindingSource, Is.EqualTo (source));
Assume.That (vm.TypeSelector, Is.Not.Null);
while (vm.SelectedObjects.Count == 0 && vm.TypeSelector.IsLoading) {
await Task.Delay (1);
}
var binding = (MockBinding) vm.SelectedObjects.First ();
Assert.That (vm.TypeSelector.SelectedType, Is.Null);
bool propertyChanged = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof(CreateBindingViewModel.PropertyRoot))
propertyChanged = true;
};
vm.TypeSelector.SelectedType = type;
Assert.That (propertyChanged, Is.True, "INPC didn't change for PropertyRoot on selected source param");
Assert.That (vm.PropertyRoot, Is.Not.Null);
Assert.That (binding.SourceParameter, Is.EqualTo (type));
await vm.PropertyRoot.Task;
CollectionAssert.AreEqual (provider.GetPropertiesForTypeAsync (type).Result,
vm.PropertyRoot.Value.Children.Select (pe => pe.Property));
}
[Test]
public async Task ExtraProperties ()
{
var provider = new MockEditorProvider();
var vm = CreateBasicViewModel ();
var editors = (IList<IObjectEditor>)typeof(CreateBindingViewModel).GetProperty ("ObjectEditors", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue (vm); // Shortcut
var editor = editors[0];
IEnumerable<IPropertyInfo> properties = (await provider.GetPropertiesForTypeAsync (typeof(MockBinding).ToTypeInfo ()));
CollectionAssert.AreEqual (properties, vm.Properties.Cast<PropertyViewModel> ().Select (pvm => pvm.Property));
properties = properties.Where (p => !editor.KnownProperties.ContainsKey (p));
CollectionAssert.AreEqual (properties.Where (p => p.Type == typeof(bool)),
vm.FlagsProperties.Cast<PropertyViewModel> ().Select (pvm => pvm.Property));
CollectionAssert.AreEqual (properties.Where (p => p.Type != typeof (bool)),
vm.BindingProperties.Cast<PropertyViewModel> ().Select (pvm => pvm.Property));
}
private AsyncSynchronizationContext syncContext;
private static readonly ResourceSource[] DefaultResourceSources = new[] { MockResourceProvider.SystemResourcesSource, MockResourceProvider.ApplicationResourcesSource };
private Mock<IPropertyInfo> GetBasicProperty (string name = "propertyName")
{
var property = new Mock<IPropertyInfo> ();
property.SetupGet (p => p.ValueSources).Returns (ValueSources.Local | ValueSources.Binding);
property.SetupGet (p => p.Type).Returns (typeof (string));
property.SetupGet (p => p.Name).Returns (name);
property.SetupGet (p => p.RealType).Returns (typeof (string).ToTypeInfo ());
property.SetupGet (p => p.CanWrite).Returns (true);
return property;
}
private Mock<IObjectEditor> GetBasicEditor (object target, IPropertyInfo property)
{
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new[] { property });
editor.SetupGet (e => e.Target).Returns (target);
editor.SetupGet (e => e.TargetType).Returns (typeof (object).ToTypeInfo ());
return editor;
}
private Mock<IResourceProvider> GetBasicResourceProvider (object target, ResourceSource[] sources = null)
{
sources = sources ?? DefaultResourceSources;
var resources = new Mock<IResourceProvider> ();
resources.Setup (r => r.GetResourceSourcesAsync (target)).ReturnsAsync (sources);
resources.Setup (r => r.CreateResourceAsync (It.IsAny<ResourceSource> (), It.IsAny<string> (), It.IsAny<object> ()))
.ReturnsAsync ((Func<ResourceSource, string, object, Resource>) ((s, n, v) => new Resource (s, n)));
return resources;
}
private Mock<IBindingProvider> GetBasicBindingProvider (object target, IPropertyInfo property, BindingSource[] sources = null)
{
var bpmock = new Mock<IBindingProvider> ();
if (sources == null) {
sources = new[] {
new BindingSource ("Short Description", BindingSourceType.Object, "Short Description"),
new BindingSource ("Long Description", BindingSourceType.SingleObject, "Long Description"),
};
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[0], target)).ReturnsAsync (new[] { new object (), new object () });
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[1], target)).ReturnsAsync (new[] { new object () });
} else {
for (int i = 0; i < sources.Length; i++) {
int index = i;
if (sources[i].Type == BindingSourceType.SingleObject)
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[index], target)).ReturnsAsync (new[] { new object () });
else
bpmock.Setup (bp => bp.GetRootElementsAsync (sources[index], target)).ReturnsAsync (new[] { new object (), new object() });
}
}
bpmock.Setup (bp => bp.GetBindingSourcesAsync (target, property)).ReturnsAsync (sources);
bpmock.Setup (bp => bp.GetValueConverterResourcesAsync (It.IsAny<object> ())).ReturnsAsync (new Resource[0]);
return bpmock;
}
private CreateBindingViewModel CreateBasicViewModel (BindingSource[] sources = null, object target = null)
{
target = target ?? new object ();
Mock<IPropertyInfo> property = GetBasicProperty ();
Mock<IObjectEditor> editor = GetBasicEditor (target, property.Object);
var editorProvider = new MockEditorProvider (editor.Object);
Mock<IResourceProvider> resourceProvider = GetBasicResourceProvider (target);
Mock<IBindingProvider> bpmock = GetBasicBindingProvider (target, property.Object, sources);
return new CreateBindingViewModel (new TargetPlatform (editorProvider, resourceProvider.Object, bpmock.Object), editor.Object, property.Object);
}
}
}
|