-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathTreeListViewTests.cs
More file actions
1064 lines (819 loc) · 46.3 KB
/
TreeListViewTests.cs
File metadata and controls
1064 lines (819 loc) · 46.3 KB
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Windows.Media;
namespace MaterialDesignThemes.UITests.WPF.TreeListViews;
public class TreeListViewTests : TestBase
{
public TreeListViewTests(ITestOutputHelper output)
: base(output)
{
AttachedDebuggerToRemoteProcess = true;
}
public static IEnumerable<object[]> GetTestControls()
{
yield return new object[] { typeof(TreeListViewDataBinding) };
yield return new object[] { typeof(TreeListViewImplicitTemplate) };
}
[Theory]
[MemberData(nameof(GetTestControls))]
public async Task CanResetNestedElements(Type userControlType)
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl(userControlType)).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> resetButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Reset"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item and select second item
await secondItem.LeftClickExpander();
await secondItem.LeftClick();
await Wait.For(() => secondItem.GetIsSelected());
//Reset children
await resetButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0_x");
await AssertTreeItemContent(treeListView, 3, "1_1_x");
await AssertTreeItemContent(treeListView, 4, "1_2_x");
await AssertTreeItemContent(treeListView, 5, "2");
recorder.Success();
}
[Fact]
public async Task CanMoveNestedElementDown()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> moveDownButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Move child item
IVisualElement<TreeListViewItem> childElement;
await Wait.For(async () =>
{
childElement = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await childElement.LeftClick();
bool isSelected = await childElement.GetIsSelected();
if (!isSelected)
{
await Task.Delay(MouseInput.GetDoubleClickTime);
}
return isSelected;
});
await moveDownButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0");
await AssertTreeItemContent(treeListView, 3, "1_2");
await AssertTreeItemContent(treeListView, 4, "1_1");
await AssertTreeItemContent(treeListView, 5, "2");
recorder.Success();
}
[Fact]
public async Task CanMoveNestedElementUp()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> moveUpButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Up"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Move child item
IVisualElement<TreeListViewItem> childElement;
await Wait.For(async () =>
{
childElement = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await childElement.LeftClick();
bool isSelected = await childElement.GetIsSelected();
if (!isSelected)
{
await Task.Delay(MouseInput.GetDoubleClickTime);
}
return isSelected;
});
await moveUpButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_1");
await AssertTreeItemContent(treeListView, 3, "1_0");
await AssertTreeItemContent(treeListView, 4, "1_2");
await AssertTreeItemContent(treeListView, 5, "2");
recorder.Success();
}
[Fact]
public async Task CanMoveNestedElementWithExpandedChildrenDown()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> moveDownButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
// Select child item and add three children and expand it
IVisualElement<TreeListViewItem>? childElement = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await AddChildren(childElement!, 3, addButton);
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
await childElement!.LeftClickExpander();
//Move child item
await moveDownButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0");
await AssertTreeItemContent(treeListView, 3, "1_2");
await AssertTreeItemContent(treeListView, 4, "1_1", true);
await AssertTreeItemContent(treeListView, 5, "1_1_0"); // NOTE: If expansion state is lost when moving, this child and the next two should not be present.
await AssertTreeItemContent(treeListView, 6, "1_1_1");
await AssertTreeItemContent(treeListView, 7, "1_1_2");
await AssertTreeItemContent(treeListView, 8, "2");
recorder.Success();
}
[Fact]
public async Task CanMoveNestedElementWithExpandedChildrenUp()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> moveUpButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Up"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
// Select child item and add three children and expand it
IVisualElement<TreeListViewItem>? childElement = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await AddChildren(childElement!, 3, addButton);
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
await childElement!.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
//Move child item
await moveUpButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_1", true);
await AssertTreeItemContent(treeListView, 3, "1_1_0"); // NOTE: If expansion state is lost when moving, this child and the next two should not be present.
await AssertTreeItemContent(treeListView, 4, "1_1_1");
await AssertTreeItemContent(treeListView, 5, "1_1_2");
await AssertTreeItemContent(treeListView, 6, "1_0");
await AssertTreeItemContent(treeListView, 7, "1_2");
await AssertTreeItemContent(treeListView, 8, "2");
recorder.Success();
}
[Fact]
public async Task CanMoveTopLevelElementDown()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> moveDownButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
await secondItem.LeftClick();
await Wait.For(async () =>
{
return await secondItem.GetIsSelected();
});
//Move item
await moveDownButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "2");
await AssertTreeItemContent(treeListView, 2, "1");
recorder.Success();
}
[Fact]
public async Task CanMoveTopLevelElementUp()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> moveUpButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Up"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Move item
await Wait.For(async () =>
{
await secondItem.LeftClick();
return await secondItem.GetIsSelected();
});
await moveUpButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "1");
await AssertTreeItemContent(treeListView, 1, "0");
await AssertTreeItemContent(treeListView, 2, "2");
recorder.Success();
}
[Fact]
public async Task CanMoveTopLevelElementWithExpandedChildrenDown()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> moveDownButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Replace item
await Wait.For(async () =>
{
await secondItem.LeftClick();
return await secondItem.GetIsSelected();
});
await moveDownButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "2");
await AssertTreeItemContent(treeListView, 2, "1", true);
await AssertTreeItemContent(treeListView, 3, "1_0"); // NOTE: If expansion state is lost when moving, these 3 children should not be present.
await AssertTreeItemContent(treeListView, 4, "1_1");
await AssertTreeItemContent(treeListView, 5, "1_2");
recorder.Success();
}
[Fact]
public async Task CanMoveTopLevelElementWithExpandedChildrenUp()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> moveUpButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Up"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Replace item
await Wait.For(async () =>
{
await secondItem.LeftClick();
return await secondItem.GetIsSelected();
});
await moveUpButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "1", true);
await AssertTreeItemContent(treeListView, 1, "1_0"); // NOTE: If expansion state is lost when moving, these 3 children should not be present.
await AssertTreeItemContent(treeListView, 2, "1_1");
await AssertTreeItemContent(treeListView, 3, "1_2");
await AssertTreeItemContent(treeListView, 4, "0");
await AssertTreeItemContent(treeListView, 5, "2");
recorder.Success();
}
[Fact]
public async Task CanReplaceTopLevelElement()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> replaceButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Replace"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Replace item
await Wait.For(async () =>
{
await secondItem.LeftClick();
return await secondItem.GetIsSelected();
});
await replaceButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1_r");
await AssertTreeItemContent(treeListView, 2, "2");
recorder.Success();
}
[Fact]
public async Task CanReplaceTopLevelElementWithExpandedChildren()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> replaceButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Replace"));
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Replace item
await replaceButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1_r"); // NOTE: The three children should have been dropped by the replace call.
await AssertTreeItemContent(treeListView, 2, "2");
recorder.Success();
}
[Fact]
public async Task CanReplaceNestedChildElement()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> replaceButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Replace"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Replace child item
IVisualElement<TreeListViewItem> childElement;
await Wait.For(async () =>
{
childElement = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await childElement.LeftClick();
return await childElement.GetIsSelected();
});
await replaceButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0");
await AssertTreeItemContent(treeListView, 3, "1_1_r");
await AssertTreeItemContent(treeListView, 4, "1_2");
await AssertTreeItemContent(treeListView, 5, "2");
recorder.Success();
}
[Fact]
public async Task CanReplaceNestedChildElementWithExpandedChildren()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> replaceButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Replace"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
// Select child item and add three children and expand it
IVisualElement<TreeListViewItem>? childElement = null;
await Wait.For(async () =>
{
childElement = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await childElement.LeftClick();
return await childElement.GetIsSelected();
});
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
await AddChildren(childElement!, 3, addButton);
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
await Wait.For(async () =>
{
await childElement!.LeftClickExpander();
bool rv = await childElement!.GetIsExpanded();
if (!rv)
{
await Task.Delay(500);
}
});
//Replace child item
await replaceButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0");
await AssertTreeItemContent(treeListView, 3, "1_1_r");
await AssertTreeItemContent(treeListView, 4, "1_2");
await AssertTreeItemContent(treeListView, 5, "2");
recorder.Success();
}
[Theory]
[MemberData(nameof(GetTestControls))]
public async Task WithHierarchicalDataTemplate_CanRemoveTopLevelElement(Type userControlType)
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl(userControlType)).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> removeButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Remove"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select second item and add three children
await AddChildren(secondItem, 3, addButton);
//Expand item
await secondItem.LeftClickExpander();
//Remove second item
await removeButton.LeftClick();
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "2");
recorder.Success();
}
[Theory]
[MemberData(nameof(GetTestControls))]
public async Task WithHierarchicalDataTemplate_CanRemoveNestedElement(Type userControlType)
{
await using var recorder = new TestRecorder(App);
//Arrange
IVisualElement<Grid> root = (await LoadUserControl(userControlType)).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> removeButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Remove"));
//Act
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select and expand second item
await AddChildren(secondItem, 3, addButton);
//Expand it
await secondItem.LeftClickExpander();
//Add two children to each of the existing children
IVisualElement<TreeListViewItem> child1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
await AddChildren(child1, 2, addButton);
await child1.LeftClickExpander();
IVisualElement<TreeListViewItem> child2 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[5]");
await AddChildren(child2, 2, addButton);
await child2.LeftClickExpander();
//Remove second item
await child1.LeftClick();
await Wait.For(child1.GetIsSelected);
await removeButton.LeftClick();
//Assert
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_1", true);
await AssertTreeItemContent(treeListView, 3, "1_1_0");
await AssertTreeItemContent(treeListView, 4, "1_1_1");
await AssertTreeItemContent(treeListView, 5, "1_2");
await AssertTreeItemContent(treeListView, 6, "2");
recorder.Success();
}
[Fact]
public async Task DoubleClickOnTreeListViewItem_TogglesExpansion()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> removeButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Remove"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select and expand second item
await AddChildren(secondItem, 3, addButton);
//NB: Needs to be long enough delay so the first click below does not register as a double click (and the third click as another which effectively collapses the item again)
await Task.Delay(500);
//Double click to expand
await secondItem.LeftClick();
await secondItem.LeftClick();
await Wait.For(() => secondItem.GetIsExpanded());
//NB: Needs to be long enough delay so the next clicks register as a new double click
await Task.Delay(500);
//Double click to collapse
await secondItem.LeftClick();
await secondItem.LeftClick();
await Wait.For(async () => await secondItem.GetIsExpanded() == false);
recorder.Success();
}
[Fact]
public async Task LeftAndRightArrowKeys_CollapseAndExpand()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> removeButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Remove"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Select and expand second item
await AddChildren(secondItem, 3, addButton);
//Right arrow to expand
await secondItem.LeftClick();
await Wait.For(() => secondItem.GetIsSelected());
await Task.Delay(500);
await secondItem.SendKeyboardInput($"{Key.Right}");
await Wait.For(() => secondItem.GetIsExpanded());
//Add child items to test double left arrow
IVisualElement<TreeListViewItem> nestedItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await AddChildren(nestedItem, 2, addButton);
await nestedItem.LeftClick();
await Wait.For(() => nestedItem.GetIsSelected());
//Left arrow to collapse
await nestedItem.SendKeyboardInput($"{Key.Left}");
await Wait.For(async () => await nestedItem.GetIsExpanded() == false);
//Allow for collapsed animation to complete
await Task.Delay(500);
//Left arrow to jump to parent
await nestedItem.SendKeyboardInput($"{Key.Left}");
await Wait.For(() => secondItem.GetIsSelected(), message: "Parent item is not selected");
recorder.Success();
}
[Theory]
[MemberData(nameof(GetTestControls))]
public async Task AddingChildrenToItemWithAlreadyExpandedChildren_InsertsNewChildAtCorrectIndex(Type userControlType)
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl(userControlType)).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Add child to second item and expand
await AddChildren(secondItem, 1, addButton);
await secondItem.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
//Add child to newly added child and expand
IVisualElement<TreeListViewItem> newChild1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
await AddChildren(newChild1, 1, addButton);
await newChild1.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
//Select secondItem again, and add another child
await AddChildren(secondItem, 1, addButton);
//Assert the child added last is below the child added first (and its children)
await AssertTreeItemContent(treeListView, 4, "1_1");
recorder.Success();
}
[Theory]
[MemberData(nameof(GetTestControls))]
public async Task RemovingChildrenFromItemWithAlreadyExpandedChildren_ShouldDeleteSelectedChild(Type userControlType)
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl(userControlType)).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> removeButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Remove"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Add child to second item and expand
await AddChildren(secondItem, 3, addButton);
await secondItem.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
//Add child to newly added child and expand
IVisualElement<TreeListViewItem> newChild1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
await AddChildren(newChild1, 1, addButton);
await newChild1.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
// Remove item "1_2"
IVisualElement<TreeListViewItem> item1_2 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[5]");
await item1_2.LeftClick();
await Wait.For(() => item1_2.GetIsSelected());
await removeButton.LeftClick();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
// Remove item "1_1" (this fails)
IVisualElement<TreeListViewItem> item1_1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[4]");
await item1_1.LeftClick();
await Wait.For(() => item1_1.GetIsSelected());
await removeButton.LeftClick();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
//Assert the 2 children were successfully removed
await AssertTreeItemContent(treeListView, 4, "2");
recorder.Success();
}
[Fact]
public async Task MovingChildItemAfterHavingMovedRootLevelParentItem_ShouldMoveChild()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> downButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> secondItem = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Add children to item "1" item and expand
await AddChildren(secondItem, 3, addButton);
await secondItem.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(500);
//Move parent item down
await downButton.LeftClick();
//NB: Wait for the move to take effect
await Task.Delay(1000);
//Move child item
IVisualElement<TreeListViewItem> secondChild = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[4]");
await secondChild.LeftClick();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
await downButton.LeftClick();
//NB: Wait for the move to take effect
await Task.Delay(1000);
//Assert the child was successfully moved
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "2");
await AssertTreeItemContent(treeListView, 2, "1", true);
await AssertTreeItemContent(treeListView, 3, "1_0");
await AssertTreeItemContent(treeListView, 4, "1_2");
await AssertTreeItemContent(treeListView, 5, "1_1");
recorder.Success();
}
[Fact]
public async Task MovingChildItemOfNestedItemAfterHavingMovedNestedItem_ShouldMoveChild()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> downButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> item1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Add children to item "1" and expand
await AddChildren(item1, 3, addButton);
await item1.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
// Add children to item "1_1" and expand
IVisualElement<TreeListViewItem> item11 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await AddChildren(item11, 3, addButton);
await item11.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
//Move parent item down
await downButton.LeftClick();
await Task.Delay(1000);
//Move child item
IVisualElement<TreeListViewItem> item111 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[6]");
await item111.LeftClick();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
await downButton.LeftClick();
//NB: Wait for the move to take effect
await Task.Delay(1000);
//Assert the child was successfully moved
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0");
await AssertTreeItemContent(treeListView, 3, "1_2");
await AssertTreeItemContent(treeListView, 4, "1_1", true);
await AssertTreeItemContent(treeListView, 5, "1_1_0");
await AssertTreeItemContent(treeListView, 6, "1_1_2");
await AssertTreeItemContent(treeListView, 7, "1_1_1");
await AssertTreeItemContent(treeListView, 8, "2");
recorder.Success();
}
[Fact]
public async Task TopLevelItemWithNestedExpandedChild_MovesChildrenMaintainingExpansion()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> upButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Up"));
IVisualElement<Button> downButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> item1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Add children to item "1" and expand
await AddChildren(item1, 3, addButton);
await item1.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000, TestContext.Current.CancellationToken);
// Add children to item "1_1" and expand
IVisualElement<TreeListViewItem> item11 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await AddChildren(item11, 3, addButton);
await item11.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000, TestContext.Current.CancellationToken);
//Move parent item down
await item1.LeftClick();
await upButton.LeftClick();
await Task.Delay(1000, TestContext.Current.CancellationToken);
await downButton.LeftClick();
await Task.Delay(1000, TestContext.Current.CancellationToken);
//Assert the child was successfully moved
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "1", true);
await AssertTreeItemContent(treeListView, 2, "1_0");
await AssertTreeItemContent(treeListView, 3, "1_1", true);
await AssertTreeItemContent(treeListView, 4, "1_1_0");
await AssertTreeItemContent(treeListView, 5, "1_1_1");
await AssertTreeItemContent(treeListView, 6, "1_1_2");
await AssertTreeItemContent(treeListView, 7, "1_2");
await AssertTreeItemContent(treeListView, 8, "2");
recorder.Success();
}
[Fact]
public async Task TopLevelItemWhichHasBeenExpandedAndCollapsed_MovesAndMaintainsCollapsedState()
{
await using var recorder = new TestRecorder(App);
IVisualElement<Grid> root = (await LoadUserControl<TreeListViewDataBinding>()).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));
IVisualElement<Button> downButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Down"));
IVisualElement<TreeListViewItem> item1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
//Add children to item "1" and expand
await AddChildren(item1, 3, addButton);
await item1.LeftClickExpander();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
// Collapse item "1" again
await item1.LeftClickExpander(false);
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
// Move down
await downButton.LeftClick();
//NB: Needs to be long enough delay so the next click does not register as a double click
await Task.Delay(1000);
// FIXME: At this point, the expansion state is wrong. The chevron indicates expanded, but in fact it is collapsed. Expanding it then crashes with an ArgumentOutOfRangeException
// NOTE: This may not be needed, I am not entirely sure.
item1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
// Expand item "1"
await item1.LeftClickExpander();
//Assert the child was successfully moved
await AssertTreeItemContent(treeListView, 0, "0");
await AssertTreeItemContent(treeListView, 1, "2");
await AssertTreeItemContent(treeListView, 2, "1", true);
await AssertTreeItemContent(treeListView, 3, "1_0");
await AssertTreeItemContent(treeListView, 4, "1_1");
await AssertTreeItemContent(treeListView, 5, "1_2");
recorder.Success();
}
[Fact]
public async Task TreeListView_WithTemplateSelector_UsesSelectorTemplates()
{
await using var recorder = new TestRecorder(App);
IVisualElement<TreeListView> treeListView = (await LoadUserControl<TreeListViewTemplateSelector>()).As<TreeListView>();
IVisualElement<TreeListViewItem> item3 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
IVisualElement<TreeListViewItem> item4 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await item3.LeftClickExpander();
await Task.Delay(500);
await item4.LeftClickExpander();
await Task.Delay(500);
await AssertTreeItemContent(treeListView, 0, "Foo", Colors.Blue);
await AssertTreeItemContent(treeListView, 1, "42", Colors.Red);
await AssertTreeItemContent(treeListView, 2, "24", Colors.Red, true);
await AssertTreeItemContent(treeListView, 3, "a", Colors.Blue);
await AssertTreeItemContent(treeListView, 4, "b", Colors.Blue);
await AssertTreeItemContent(treeListView, 5, "c", Colors.Blue);
await AssertTreeItemContent(treeListView, 6, "Bar", Colors.Blue, true);
await AssertTreeItemContent(treeListView, 7, "1", Colors.Red);
await AssertTreeItemContent(treeListView, 8, "2", Colors.Red);
await AssertTreeItemContent(treeListView, 9, "3", Colors.Red);
recorder.Success();
}
[Fact]
public async Task TreeListView_WithCollectionView_RendersItems()
{
await using var recorder = new TestRecorder(App);
IVisualElement<TreeListView> treeListView = (await LoadUserControl<TreeListViewWithCollectionView>()).As<TreeListView>();
IVisualElement<TreeListViewItem> item3 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
IVisualElement<TreeListViewItem> item4 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");
await item3.LeftClickExpander();
await Task.Delay(500);
await item4.LeftClickExpander();
await Task.Delay(500);
await AssertTreeItemContent(treeListView, 0, "Foo");
await AssertTreeItemContent(treeListView, 1, "42");
await AssertTreeItemContent(treeListView, 2, "24", true);
await AssertTreeItemContent(treeListView, 3, "a");
await AssertTreeItemContent(treeListView, 4, "b");
await AssertTreeItemContent(treeListView, 5, "c");
await AssertTreeItemContent(treeListView, 6, "Bar", true);
await AssertTreeItemContent(treeListView, 7, "1");
await AssertTreeItemContent(treeListView, 8, "2");