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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Xamarin.PropertyEditing.Reflection;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Tests
{
internal abstract class PropertiesViewModelTests<TViewModel>
where TViewModel : PropertiesViewModel
{
protected class TestClass
{
public string Property
{
get;
set;
}
}
protected class TestClassSub
: TestClass
{
[System.ComponentModel.Category ("Sub")]
public int SubProperty
{
get;
set;
}
}
private Exception unhandled;
[SetUp]
public virtual void Setup ()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
}
[TearDown]
public void TearDown ()
{
AppDomain.CurrentDomain.UnhandledException -= CurrentDomainOnUnhandledException;
if (this.unhandled != null) {
var ex = this.unhandled;
this.unhandled = null;
Assert.Fail ("Unhandled exception: {0}", ex);
}
}
private void CurrentDomainOnUnhandledException (object sender, UnhandledExceptionEventArgs e)
{
this.unhandled = e.ExceptionObject as Exception;
}
[Test]
public void TypeName ()
{
var provider = new ReflectionEditorProvider ();
var obj = new TestClassSub ();
var vm = CreateVm (provider);
Assume.That (vm.TypeName, Is.Null);
vm.SelectedObjects.Add (obj);
Assert.That (vm.TypeName, Is.EqualTo (nameof (TestClassSub)));
}
[Test]
public void TypeNameNoneSelected ()
{
var provider = new ReflectionEditorProvider ();
var obj = new TestClassSub ();
var vm = CreateVm (provider);
Assume.That (vm.TypeName, Is.Null);
vm.SelectedObjects.Add (obj);
Assume.That (vm.TypeName, Is.EqualTo (nameof (TestClassSub)));
vm.SelectedObjects.Remove (obj);
Assert.That (vm.TypeName, Is.Null);
}
[Test]
public void TypeNameMultipleSameSelected ()
{
var provider = new ReflectionEditorProvider ();
var obj = new TestClassSub ();
var obj2 = new TestClassSub ();
var vm = CreateVm (provider);
Assume.That (vm.TypeName, Is.Null);
vm.SelectedObjects.Add (obj);
vm.SelectedObjects.Add (obj2);
Assert.That (vm.TypeName, Is.EqualTo (nameof (TestClassSub)));
}
[Test]
public void TypeNameMultipleNotSameSelected ()
{
var provider = new ReflectionEditorProvider ();
var obj = new TestClassSub ();
var obj2 = new TestClass ();
var vm = CreateVm (provider);
Assume.That (vm.TypeName, Is.Null);
vm.SelectedObjects.Add (obj);
vm.SelectedObjects.Add (obj2);
Assert.That (vm.TypeName, Is.Not.EqualTo (nameof (TestClassSub)));
Assert.That (vm.TypeName, Is.Not.EqualTo (nameof (TestClass)));
}
[Test]
public void ObjectName ()
{
var obj = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.ObjectName, Is.Null);
bool changed = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof (PropertiesViewModel.ObjectName))
changed = true;
};
vm.SelectedObjects.Add (obj);
Assert.That (vm.ObjectName, Is.EqualTo (name));
Assert.That (changed, Is.True);
}
[Test]
public void IsObjectNameable ()
{
var obj = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.IsObjectNameable, Is.False);
bool changed = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof (PropertiesViewModel.IsObjectNameable))
changed = true;
};
vm.SelectedObjects.Add (obj);
Assert.That (vm.IsObjectNameable, Is.True);
Assert.That (changed, Is.True);
}
[Test]
public void ObjectNameNoneSelected ()
{
var obj = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.ObjectName, Is.Null);
vm.SelectedObjects.Add (obj);
Assume.That (vm.ObjectName, Is.EqualTo (name));
vm.SelectedObjects.Remove (obj);
Assert.That (vm.ObjectName, Is.Null);
}
[Test]
public void IsObjectNameableNoneSelected ()
{
var obj = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.IsObjectNameable, Is.False);
vm.SelectedObjects.Add (obj);
Assume.That (vm.IsObjectNameable, Is.True);
vm.SelectedObjects.Remove (obj);
Assert.That (vm.IsObjectNameable, Is.False);
}
[Test]
public void IsObjectNameableNotNameable ()
{
var obj = new object ();
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.IsObjectNameable, Is.False);
vm.SelectedObjects.Add (obj);
Assume.That (vm.IsObjectNameable, Is.False);
vm.SelectedObjects.Remove (obj);
Assert.That (vm.IsObjectNameable, Is.False);
}
[Test]
public void ObjectNameNoName ()
{
var obj = new object ();
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetTarget (obj);
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync ((string)null);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.ObjectName, Is.Null);
vm.SelectedObjects.Add (obj);
Assert.That (vm.ObjectName, Is.EqualTo (Properties.Resources.NoName));
}
[Test]
public void ObjectNameMultipleSelected ()
{
var obj = new object ();
var obj2 = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var editor2 = new Mock<IObjectEditor> ();
editor2.SetupGet (e => e.Target).Returns (obj2);
editor2.SetupGet (oe => oe.TargetType).Returns (obj2.GetType ().ToTypeInfo ());
editor2.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
provider.Setup (p => p.GetObjectEditorAsync (obj2)).ReturnsAsync (editor2.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.ObjectName, Is.Null);
vm.SelectedObjects.Add (obj);
vm.SelectedObjects.Add (obj2);
// Properties.Resources.MultipleObjectsSelected
Assert.That (vm.ObjectName, Is.Not.Null.And.Not.EqualTo (name));
vm.SelectedObjects.Remove (obj2);
Assert.That (vm.ObjectName, Is.EqualTo (name));
}
[Test]
public void IsObjectNameableMultipleSelected ()
{
var obj = new object ();
var obj2 = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var editor2 = new Mock<IObjectEditor> ();
editor2.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor2.SetupGet (e => e.Target).Returns (obj2);
editor2.As<INameableObject> ();
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
provider.Setup (p => p.GetObjectEditorAsync (obj2)).ReturnsAsync (editor2.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.IsObjectNameable, Is.False);
vm.SelectedObjects.Add (obj);
Assume.That (vm.IsObjectNameable, Is.True);
vm.SelectedObjects.Add (obj2);
Assert.That (vm.IsObjectNameable, Is.True);
vm.SelectedObjects.Remove (obj2);
Assert.That (vm.IsObjectNameable, Is.True);
}
[Test]
public void IsObjectReadonlyMultipleSelected ()
{
var obj = new object ();
var obj2 = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (e => e.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.As<INameableObject> ().Setup (n => n.GetNameAsync ()).ReturnsAsync (name);
var editor2 = new Mock<IObjectEditor> ();
editor2.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor2.SetupGet (e => e.Target).Returns (obj2);
editor2.SetupGet (oe => oe.TargetType).Returns (obj2.GetType ().ToTypeInfo ());
editor2.As<INameableObject> ();
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
provider.Setup (p => p.GetObjectEditorAsync (obj2)).ReturnsAsync (editor2.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.IsObjectNameReadOnly, Is.False);
vm.SelectedObjects.Add (obj);
Assume.That (vm.IsObjectNameReadOnly, Is.False);
vm.SelectedObjects.Add (obj2);
Assert.That (vm.IsObjectNameReadOnly, Is.True);
vm.SelectedObjects.Remove (obj2);
Assert.That (vm.IsObjectNameReadOnly, Is.False);
}
[Test]
public void SetObjectName()
{
var obj = new object ();
const string name = "name";
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetTarget (obj);
var nameable = editor.As<INameableObject> ();
nameable.Setup (n => n.GetNameAsync ()).ReturnsAsync ((string)null);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.IsObjectNameable, Is.False);
vm.SelectedObjects.Add (obj);
Assume.That (vm.IsObjectNameable, Is.True);
bool changed = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof (PropertiesViewModel.ObjectName))
changed = true;
};
vm.ObjectName = name;
nameable.Verify (n => n.SetNameAsync (name));
Assert.That (changed, Is.True);
Assert.That (vm.ObjectName, Is.EqualTo (name));
}
[Test]
public void EventsNotEnabled()
{
var obj = new object ();
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetTarget (obj);
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assert.That (vm.EventsEnabled, Is.False);
vm.SelectedObjects.Add (obj);
Assert.That (vm.EventsEnabled, Is.False);
}
[Test]
public void Events()
{
var obj = new object ();
var ev = new Mock<IEventInfo> ();
ev.SetupGet (e => e.Name).Returns ("name");
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
editor.SetupGet (oe => oe.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
var eeditor = editor.As<IObjectEventEditor> ();
eeditor.SetupGet (e => e.Events).Returns (new[] { ev.Object });
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
Assume.That (vm.EventsEnabled, Is.False);
bool changed = false;
vm.PropertyChanged += (o, e) => {
if (e.PropertyName == nameof (PropertiesViewModel.EventsEnabled))
changed = true;
};
vm.SelectedObjects.Add (obj);
Assert.That (vm.EventsEnabled, Is.True);
Assert.That (changed, Is.True);
Assert.That (vm.Events.Count, Is.EqualTo (1));
Assert.That (vm.Events[0].Event, Is.SameAs (ev.Object));
}
[Test]
[Description ("Currently we just clear the events list if multiple objects are selected regardless of typ")]
public void MultipleObjectEvents ()
{
var obj = new object();
var obj2 = new object();
var ev = new Mock<IEventInfo> ();
ev.SetupGet (e => e.Name).Returns ("name");
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (oe => oe.Target).Returns (obj);
editor.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editor.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
var eeditor = editor.As<IObjectEventEditor> ();
eeditor.SetupGet (e => e.Events).Returns (new[] { ev.Object });
var editor2 = new Mock<IObjectEditor> ();
editor2.SetupGet (oe => oe.Target).Returns (obj2);
editor2.SetupGet (oe => oe.TargetType).Returns (obj2.GetType ().ToTypeInfo ());
editor2.SetupGet (e => e.Properties).Returns (new IPropertyInfo[0]);
var eeditor2 = editor.As<IObjectEventEditor> ();
eeditor2.SetupGet (e => e.Events).Returns (new[] { ev.Object });
var provider = new Mock<IEditorProvider> ();
provider.Setup (p => p.GetObjectEditorAsync (obj)).ReturnsAsync (editor.Object);
provider.Setup (p => p.GetObjectEditorAsync (obj2)).ReturnsAsync (editor2.Object);
var vm = CreateVm (provider.Object);
vm.SelectedObjects.Add (obj);
Assume.That (vm.EventsEnabled, Is.True);
Assume.That (vm.Events.Count, Is.EqualTo (1));
vm.SelectedObjects.Add (obj2);
Assert.That (vm.Events.Count, Is.EqualTo (0));
}
[Test]
[Description ("The IEditorProvider should be able to return null editors")]
public void NullEditorAdded ()
{
var providerMock = new Mock<IEditorProvider> ();
var vm = CreateVm (providerMock.Object);
vm.SelectedObjects.Add (null);
vm.SelectedObjects.Add (null);
}
[Test]
public void BasicPredefinedGetsPredefined ()
{
var property = new Mock<IPropertyInfo> ();
property.SetupGet (pi => pi.Type).Returns (typeof(string));
var p = property.As<IHavePredefinedValues<string>> ();
p.SetupGet (pv => pv.PredefinedValues).Returns (new Dictionary<string, string> { { "key", "value" } });
var target = new object ();
var editor = new Mock<IObjectEditor> ();
editor.SetupGet (oe => oe.Target).Returns (target);
editor.SetupGet (oe => oe.TargetType).Returns (target.GetType ().ToTypeInfo ());
editor.SetupGet (oe => oe.Properties).Returns (new[] { property.Object });
var provider = new Mock<IEditorProvider> ();
provider.Setup (ep => ep.GetObjectEditorAsync (target)).ReturnsAsync (editor.Object);
var vm = CreateVm (provider.Object);
vm.SelectedObjects.Add (target);
Assume.That (vm.Properties, Has.Count.EqualTo (1));
Assert.That (vm.Properties.First(), Is.TypeOf<PredefinedValuesViewModel<string>> ());
}
[Test]
public async Task PropertiesAddedFromEditor ()
{
var provider = new ReflectionEditorProvider ();
var obj = new TestClass ();
var editor = await provider.GetObjectEditorAsync (obj);
Assume.That (editor.Properties.Count, Is.EqualTo (1));
var vm = CreateVm (new TargetPlatform (provider));
vm.SelectedObjects.Add (obj);
Assert.That (vm.Properties, Is.Not.Empty);
Assert.That (((PropertyViewModel)vm.Properties[0]).Property, Is.EqualTo (editor.Properties.Single ()));
}
[Test]
[Description ("When editors of two different types are selected, the properties that are common should be listed")]
public void PropertiesFromCommonSubset ()
{
var obj1 = new TestClass ();
var obj2 = new TestClassSub ();
var sharedPropertyMock = new Mock<IPropertyInfo> ();
sharedPropertyMock.SetupGet (pi => pi.Type).Returns (typeof (string));
var subPropertyMock = new Mock<IPropertyInfo> ();
subPropertyMock.SetupGet (pi => pi.Type).Returns (typeof (int));
var editor1Mock = new Mock<IObjectEditor> ();
editor1Mock.SetupGet (oe => oe.Properties).Returns (new[] { sharedPropertyMock.Object });
editor1Mock.SetupGet (oe => oe.Target).Returns (obj1);
editor1Mock.SetupGet (oe => oe.TargetType).Returns (obj1.GetType ().ToTypeInfo ());
var editor2Mock = new Mock<IObjectEditor> ();
editor2Mock.SetupGet (oe => oe.Properties).Returns (new[] { sharedPropertyMock.Object, subPropertyMock.Object });
editor2Mock.SetupGet (oe => oe.Target).Returns (obj2);
editor2Mock.SetupGet (oe => oe.TargetType).Returns (obj2.GetType ().ToTypeInfo ());
var providerMock = new Mock<IEditorProvider> ();
providerMock.Setup (ep => ep.GetObjectEditorAsync (obj1)).ReturnsAsync (editor1Mock.Object);
providerMock.Setup (ep => ep.GetObjectEditorAsync (obj2)).ReturnsAsync (editor2Mock.Object);
var vm = CreateVm (new TargetPlatform (providerMock.Object));
vm.SelectedObjects.Add (obj1);
Assume.That (vm.Properties.Count, Is.EqualTo (1));
Assume.That (((PropertyViewModel)vm.Properties[0]).Property, Is.EqualTo (sharedPropertyMock.Object));
// Reflection property info equate actually fails on the same property across class/subclass
vm.SelectedObjects.Add (obj2);
Assert.That (vm.Properties.Count, Is.EqualTo (1));
Assert.That (((PropertyViewModel)vm.Properties.Single ()).Property, Is.EqualTo (sharedPropertyMock.Object));
}
[Test]
[Description ("When editors of two different types are selected, the properties that are common should be listed")]
public void PropertiesReducesToCommonSubset ()
{
var obj1 = new TestClass ();
var obj2 = new TestClassSub ();
var sharedPropertyMock = new Mock<IPropertyInfo> ();
sharedPropertyMock.SetupGet (pi => pi.Type).Returns (typeof (string));
var subPropertyMock = new Mock<IPropertyInfo> ();
subPropertyMock.SetupGet (pi => pi.Type).Returns (typeof (int));
var editor1Mock = new Mock<IObjectEditor> ();
editor1Mock.SetupGet (oe => oe.Properties).Returns (new[] { sharedPropertyMock.Object });
editor1Mock.SetTarget (obj1);
var editor2Mock = new Mock<IObjectEditor> ();
editor2Mock.SetupGet (oe => oe.Properties).Returns (new[] { sharedPropertyMock.Object, subPropertyMock.Object });
editor2Mock.SetTarget (obj2);
var providerMock = new Mock<IEditorProvider> ();
providerMock.Setup (ep => ep.GetObjectEditorAsync (obj1)).ReturnsAsync (editor1Mock.Object);
providerMock.Setup (ep => ep.GetObjectEditorAsync (obj2)).ReturnsAsync (editor2Mock.Object);
var vm = CreateVm (new TargetPlatform (providerMock.Object));
vm.SelectedObjects.Add (obj2);
Assume.That (vm.Properties.Count, Is.EqualTo (2));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (sharedPropertyMock.Object));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (subPropertyMock.Object));
// Reflection property info equate actually fails on the same property across class/subclass
vm.SelectedObjects.Add (obj1);
Assert.That (vm.Properties.Count, Is.EqualTo (1));
Assert.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (sharedPropertyMock.Object));
}
[Test]
[Description ("Adding or removing editors shouldn't remake other editors or duplicate")]
public void EditorsShouldBeConsistent ()
{
var provider = new ReflectionEditorProvider ();
var obj1 = new TestClass ();
var obj2 = new TestClass ();
var vm = CreateVm (new TargetPlatform (provider));
vm.SelectedObjects.Add (obj1);
var property = vm.Properties[0];
Assume.That (property.Editors.Count, Is.EqualTo (1));
var editor = property.Editors.Single ();
vm.SelectedObjects.Add (obj2);
Assume.That (property, Is.SameAs (vm.Properties[0]));
Assert.That (property.Editors, Contains.Item (editor));
Assert.That (property.Editors.Count, Is.EqualTo (2));
}
[Test]
public void EditorRemovedWithSelectedObject ()
{
var provider = new ReflectionEditorProvider ();
var obj1 = new TestClass ();
var obj2 = new TestClass ();
var vm = CreateVm (new TargetPlatform (provider));
vm.SelectedObjects.Add (obj1);
vm.SelectedObjects.Add (obj2);
var property = vm.Properties[0];
var editor = property.Editors.Single (oe => oe.Target == obj1);
Assume.That (property.Editors.Count, Is.EqualTo (2));
Assume.That (vm.SelectedObjects.Remove (obj2));
Assume.That (property, Is.SameAs (vm.Properties[0]));
Assert.That (property.Editors, Contains.Item (editor));
Assert.That (property.Editors.Count, Is.EqualTo (1));
}
[Test]
public void PropertiesListItemRemoved ()
{
var mockProperty1 = new Mock<IPropertyInfo> ();
mockProperty1.SetupGet (pi => pi.Type).Returns (typeof (string));
var mockProperty2 = new Mock<IPropertyInfo> ();
mockProperty2.SetupGet (pi => pi.Type).Returns (typeof (string));
var obj = new object ();
var properties = new ObservableCollection<IPropertyInfo> { mockProperty1.Object, mockProperty2.Object };
var editorMock = new Mock<IObjectEditor> ();
editorMock.SetupGet (e => e.Target).Returns (obj);
editorMock.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editorMock.SetupGet (e => e.Properties).Returns (properties);
var provider = new Mock<IEditorProvider> ();
provider.Setup (ep => ep.GetObjectEditorAsync (obj)).ReturnsAsync (editorMock.Object);
var vm = CreateVm (new TargetPlatform (provider.Object));
vm.SelectedObjects.Add (obj);
Assume.That (vm.Properties.Count, Is.EqualTo (2));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty1.Object));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty2.Object));
properties.Remove (mockProperty2.Object);
Assert.That (vm.Properties.Count, Is.EqualTo (1));
Assert.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty1.Object));
}
[Test]
public void PropertiesListItemAdded ()
{
var mockProperty1 = new Mock<IPropertyInfo> ();
mockProperty1.SetupGet (pi => pi.Type).Returns (typeof (string));
var mockProperty2 = new Mock<IPropertyInfo> ();
mockProperty2.SetupGet (pi => pi.Type).Returns (typeof (string));
var obj = new object ();
var properties = new ObservableCollection<IPropertyInfo> { mockProperty1.Object };
var editorMock = new Mock<IObjectEditor> ();
editorMock.SetupGet (oe => oe.Target).Returns (obj);
editorMock.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editorMock.SetupGet (e => e.Properties).Returns (properties);
var provider = new Mock<IEditorProvider> ();
provider.Setup (ep => ep.GetObjectEditorAsync (obj)).ReturnsAsync (editorMock.Object);
var vm = CreateVm (new TargetPlatform (provider.Object));
vm.SelectedObjects.Add (obj);
Assume.That (vm.Properties.Count, Is.EqualTo (1));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty1.Object));
properties.Add (mockProperty2.Object);
Assert.That (vm.Properties.Count, Is.EqualTo (2));
Assert.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty1.Object));
Assert.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty2.Object));
}
[Test]
public void PropertiesListItemAddedWithReset ()
{
var mockProperty1 = new Mock<IPropertyInfo> ();
mockProperty1.SetupGet (pi => pi.Type).Returns (typeof (string));
var mockProperty2 = new Mock<IPropertyInfo> ();
mockProperty2.SetupGet (pi => pi.Type).Returns (typeof (string));
var obj = new object ();
var properties = new ObservableCollection<IPropertyInfo> { mockProperty1.Object };
var editorMock = new Mock<IObjectEditor> ();
editorMock.SetupGet (e => e.Target).Returns (obj);
editorMock.SetupGet (oe => oe.TargetType).Returns (obj.GetType ().ToTypeInfo ());
editorMock.SetupGet (e => e.Properties).Returns (properties);
var provider = new Mock<IEditorProvider> ();
provider.Setup (ep => ep.GetObjectEditorAsync (obj)).ReturnsAsync (editorMock.Object);
var vm = CreateVm (new TargetPlatform (provider.Object));
// We need access to the custom reset method here to ensure compliance
// It's a bit hacky but this is unlikely to change. If it does, this test
// will ensure the new notifier works as it should when resetting.
Assume.That (vm.SelectedObjects, Is.TypeOf<ObservableCollectionEx<object>> ());
((ObservableCollectionEx<object>)vm.SelectedObjects).Reset (new[] { obj });
Assume.That (vm.Properties.Count, Is.EqualTo (1));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty1.Object));
properties.Add (mockProperty2.Object);
Assert.That (vm.Properties.Count, Is.EqualTo (2));
Assert.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty1.Object));
Assert.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (mockProperty2.Object));
}
[Test]
public void PropertiesListItemRemovedJointList ()
{
var baseObj = new object ();
var derivedObj = new object ();
var baseProperty = new Mock<IPropertyInfo> ();
baseProperty.SetupGet (pi => pi.Type).Returns (typeof (string));
var baseProperties = new ObservableCollectionEx<IPropertyInfo> { baseProperty.Object };
var derivedProperties = new ObservableCollectionEx<IPropertyInfo> { baseProperty.Object };
var baseEditorMock = new Mock<IObjectEditor> ();
baseEditorMock.SetupGet (e => e.Properties).Returns (baseProperties);
baseEditorMock.SetTarget (baseObj);
var derivedEditorMock = new Mock<IObjectEditor> ();
derivedEditorMock.SetupGet (e => e.Properties).Returns (derivedProperties);
derivedEditorMock.SetTarget (derivedObj);
var providerMock = new Mock<IEditorProvider> ();
providerMock.Setup (ep => ep.GetObjectEditorAsync (baseObj)).ReturnsAsync (baseEditorMock.Object);
providerMock.Setup (ep => ep.GetObjectEditorAsync (derivedObj)).ReturnsAsync (derivedEditorMock.Object);
var vm = CreateVm (new TargetPlatform (providerMock.Object));
vm.SelectedObjects.AddItems (new[] { baseObj, derivedObj });
Assume.That (vm.Properties.Count, Is.EqualTo (1));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (baseProperty.Object));
derivedProperties.Remove (baseProperty.Object);
Assert.That (vm.Properties, Is.Empty);
}
[Test]
public void PropertiesListSelectedItemRemovedStopsListening ()
{
var baseObj = new object ();
var derivedObj = new object ();
var baseProperty = new Mock<IPropertyInfo> ();
baseProperty.SetupGet (pi => pi.Type).Returns (typeof (string));
var baseProperties = new ObservableCollectionEx<IPropertyInfo> { baseProperty.Object };
var derivedProperties = new ObservableCollectionEx<IPropertyInfo> { baseProperty.Object };
var baseEditorMock = new Mock<IObjectEditor> ();
baseEditorMock.SetupGet (e => e.Properties).Returns (baseProperties);
baseEditorMock.SetTarget (baseObj);
var derivedEditorMock = new Mock<IObjectEditor> ();
derivedEditorMock.SetupGet (e => e.Properties).Returns (derivedProperties);
derivedEditorMock.SetTarget (derivedObj);
var providerMock = new Mock<IEditorProvider> ();
providerMock.Setup (ep => ep.GetObjectEditorAsync (baseObj)).ReturnsAsync (baseEditorMock.Object);
providerMock.Setup (ep => ep.GetObjectEditorAsync (derivedObj)).ReturnsAsync (derivedEditorMock.Object);
var vm = CreateVm (new TargetPlatform (providerMock.Object));
vm.SelectedObjects.AddItems (new[] { baseObj, derivedObj });
Assume.That (vm.Properties.Count, Is.EqualTo (1));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (baseProperty.Object));
vm.SelectedObjects.Remove (derivedObj);
Assume.That (vm.Properties, Is.Not.Empty);
var changedField = typeof (ObservableCollection<IPropertyInfo>).GetField (nameof (INotifyCollectionChanged.CollectionChanged), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
MulticastDelegate d = (MulticastDelegate)changedField.GetValue (derivedProperties);
Assert.That (d, Is.Null);
}
[Test]
public void PropertiesListSelectedItemResetStopsListening ()
{
var baseObj = new object ();
var derivedObj = new object ();
var baseProperty = new Mock<IPropertyInfo> ();
baseProperty.SetupGet (pi => pi.Type).Returns (typeof (string));
var baseProperties = new ObservableCollectionEx<IPropertyInfo> { baseProperty.Object };
var derivedProperties = new ObservableCollectionEx<IPropertyInfo> { baseProperty.Object };
var baseEditorMock = new Mock<IObjectEditor> ();
baseEditorMock.SetupGet (e => e.Properties).Returns (baseProperties);
baseEditorMock.SetTarget (baseObj);
var derivedEditorMock = new Mock<IObjectEditor> ();
derivedEditorMock.SetupGet (e => e.Properties).Returns (derivedProperties);
derivedEditorMock.SetTarget (derivedObj);
var providerMock = new Mock<IEditorProvider> ();
providerMock.Setup (ep => ep.GetObjectEditorAsync (baseObj)).ReturnsAsync (baseEditorMock.Object);
providerMock.Setup (ep => ep.GetObjectEditorAsync (derivedObj)).ReturnsAsync (derivedEditorMock.Object);
var vm = CreateVm (new TargetPlatform (providerMock.Object));
vm.SelectedObjects.AddItems (new[] { baseObj, derivedObj });
Assume.That (vm.Properties.Count, Is.EqualTo (1));
Assume.That (vm.Properties.Cast<PropertyViewModel> ().Select (v => v.Property), Contains.Item (baseProperty.Object));
Assume.That (vm.SelectedObjects, Is.TypeOf<ObservableCollectionEx<object>> ());
((ObservableCollectionEx<object>)vm.SelectedObjects).Reset (new[] { baseObj });
Assume.That (vm.Properties, Is.Not.Empty);
var changedField = typeof (ObservableCollection<IPropertyInfo>).GetField (nameof (INotifyCollectionChanged.CollectionChanged), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
MulticastDelegate d = (MulticastDelegate)changedField.GetValue (derivedProperties);
Assert.That (d, Is.Null);
}
protected TViewModel CreateVm (IEditorProvider provider)
{
return CreateVm (new TargetPlatform (provider));
}
internal abstract TViewModel CreateVm (TargetPlatform platform);
}
}
|