This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathShellTests.cs
1319 lines (1005 loc) · 39.6 KB
/
ShellTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class ShellTests : ShellTestBase
{
[Test]
public void DefaultState()
{
var shell = new Shell();
Assert.IsEmpty(shell.Items);
}
[Test]
public void CurrentItemAutoSets()
{
var shell = new Shell();
var shellItem = new ShellItem();
var shellSection = new ShellSection();
var shellContent = new ShellContent { Content = new ContentPage() };
shellSection.Items.Add(shellContent);
shellItem.Items.Add(shellSection);
shell.Items.Add(shellItem);
Assert.That(shell.CurrentItem, Is.EqualTo(shellItem));
}
[TestCase(true)]
[TestCase(false)]
public void SetCurrentItemWithImplicitlyWrappedShellContent(bool useShellContent)
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
BaseShellItem shellElement = null;
if (useShellContent)
shellElement = CreateShellContent(shellContentRoute: "TestMe");
else
shellElement = CreateShellSection(shellSectionRoute: "TestMe");
if (useShellContent)
shell.Items.Add((ShellContent)shellElement);
else
shell.Items.Add((ShellSection)shellElement);
var item2 = shell.Items[1];
Assert.AreEqual(FindParentOfType<ShellItem>(shellElement), item2);
if (useShellContent)
shell.CurrentItem = (ShellContent)shellElement;
else
shell.CurrentItem = (ShellSection)shellElement;
Assert.AreEqual(2, shell.Items.Count);
Assert.AreEqual(FindParentOfType<ShellItem>(shellElement), item2);
Assert.AreEqual(item2, shell.CurrentItem);
}
[Test]
public void SetCurrentItemAddsToShellCollection()
{
var shell = new Shell();
var shellItem = CreateShellItem();
var shellSection = CreateShellSection();
var shellContent = CreateShellContent();
shell.CurrentItem = shellItem;
Assert.IsTrue(shell.Items.Contains(shellItem));
Assert.AreEqual(shell.CurrentItem, shellItem);
shell.CurrentItem = shellSection;
Assert.AreEqual(shell.CurrentItem, shellSection.Parent);
shell.CurrentItem = shellContent;
Assert.AreEqual(shell.CurrentItem, shellContent.Parent.Parent);
}
[Test]
public void ShellChildrenBindingContext()
{
var shell = new Shell();
var shellItem = CreateShellItem();
shell.Items.Add(shellItem);
object viewModel = new object();
shell.BindingContext = viewModel;
Assert.AreSame(shell.BindingContext, viewModel);
Assert.AreSame(shellItem.BindingContext, viewModel);
Assert.AreSame(shellItem.Items[0].BindingContext, viewModel);
Assert.AreSame(shellItem.Items[0].Items[0].BindingContext, viewModel);
Assert.AreSame((shellItem.Items[0].Items[0].Content as BindableObject).BindingContext, viewModel);
}
[Test]
public void ShellPropagateBindingContextWhenAddingNewShellItem()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
object viewModel = new object();
shell.BindingContext = viewModel;
var shellItem = CreateShellItem();
shell.Items.Add(shellItem);
Assert.AreSame(shellItem.BindingContext, viewModel);
Assert.AreSame(shellItem.Items[0].BindingContext, viewModel);
Assert.AreSame(shellItem.Items[0].Items[0].BindingContext, viewModel);
Assert.AreSame((shellItem.Items[0].Items[0].Content as BindableObject).BindingContext, viewModel);
}
[Test]
public void ShellPropagateBindingContextWhenAddingNewShellSection()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
object viewModel = new object();
shell.BindingContext = viewModel;
var shellSection = CreateShellSection();
shell.Items[0].Items.Add(shellSection);
Assert.AreSame(shellSection.BindingContext, viewModel);
Assert.AreSame(GetItems(shellSection)[0].BindingContext, viewModel);
Assert.AreSame((GetItems(shellSection)[0].Content as BindableObject).BindingContext, viewModel);
}
[Test]
public void ShellPropagateBindingContextWhenAddingNewShellContent()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
object viewModel = new object();
shell.BindingContext = viewModel;
var shellContent = CreateShellContent();
shell.Items[0].Items[0].Items.Add(shellContent);
Assert.AreSame(shellContent.BindingContext, viewModel);
Assert.AreSame((shellContent.Content as BindableObject).BindingContext, viewModel);
}
[Test]
public void ShellPropagateBindingContextWhenChangingContent()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
object viewModel = new object();
shell.BindingContext = viewModel;
var contentPage = new ContentPage();
shell.Items[0].Items[0].Items[0].Content = contentPage;
Assert.AreSame(contentPage.BindingContext, viewModel);
}
[Test]
public async Task ShellPropagateBindingContextWhenPushingContent()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
object viewModel = new object();
shell.BindingContext = viewModel;
var contentPage = new ContentPage();
await shell.Navigation.PushAsync(contentPage);
Assert.AreSame(contentPage.BindingContext, viewModel);
}
[Test]
public void NavigationProxyWireUpTest()
{
var page = new ContentPage();
var shell = new Shell();
var shellItem = new ShellItem();
var shellSection = new ShellSection();
var shellContent = new ShellContent { Content = page };
shellSection.Items.Add(shellContent);
shellItem.Items.Add(shellSection);
shell.Items.Add(shellItem);
NavigationProxy proxy = page.NavigationProxy.Inner as NavigationProxy;
Assert.IsNotNull(proxy);
var shellProxy = proxy.Inner;
Assert.IsNotNull(shellProxy);
}
[Test]
public void CurrentItemDoesNotChangeOnSecondAdd()
{
var shell = new Shell();
var shellItem = new ShellItem();
var shellSection = new ShellSection();
var shellContent = new ShellContent { Content = new ContentPage() };
shellSection.Items.Add(shellContent);
shellItem.Items.Add(shellSection);
shell.Items.Add(shellItem);
Assume.That(shell.CurrentItem, Is.EqualTo(shellItem));
shell.Items.Add(new ShellItem());
Assert.AreEqual(shellItem, shell.CurrentItem);
}
[Test]
public void SimpleGoTo()
{
var shell = new Shell();
var one = new ShellItem { Route = "one" };
var two = new ShellItem { Route = "two" };
var tabone = MakeSimpleShellSection("tabone", "content");
var tabtwo = MakeSimpleShellSection("tabtwo", "content");
var tabthree = MakeSimpleShellSection("tabthree", "content");
var tabfour = MakeSimpleShellSection("tabfour", "content");
one.Items.Add(tabone);
one.Items.Add(tabtwo);
two.Items.Add(tabthree);
two.Items.Add(tabfour);
shell.Items.Add(one);
shell.Items.Add(two);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//one/tabone/content"));
shell.GoToAsync(new ShellNavigationState("//two/tabfour/"));
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tabfour/content"));
}
[Test]
public async Task CaseIgnoreRouting()
{
var routes = new[] { "Tab1", "TAB2", "@-_-@", "+:~", "=%", "Super_Simple+-Route.doc", "1/2", @"1\2/3", "app://tab" };
foreach (var route in routes)
{
var formattedRoute = Routing.FormatRoute(route);
Routing.RegisterRoute(formattedRoute, typeof(ShellItem));
var content1 = Routing.GetOrCreateContent(formattedRoute);
Assert.IsNotNull(content1);
Assert.AreEqual(Routing.GetRoute(content1), formattedRoute);
}
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("app://IMPL_tab21", typeof(ShellItem)));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute(@"app:\\IMPL_tab21", typeof(ShellItem)));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute(string.Empty, typeof(ShellItem)));
Assert.Catch(typeof(ArgumentNullException), () => Routing.RegisterRoute(null, typeof(ShellItem)));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("tab1/IMPL_tab11", typeof(ShellItem)));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("IMPL_shell", typeof(ShellItem)));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("app://tab2/IMPL_tab21", typeof(ShellItem)));
}
[Test]
public async Task FailWhenAddingDuplicatedRouting()
{
var route = "dogs";
Routing.RegisterRoute(route, typeof(ShellItem));
Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ContentPage)));
}
[Test]
public async Task SucceedWhenAddingDuplicateRouteOfSameType()
{
var route = "dogs";
Routing.RegisterRoute(route, typeof(ShellItem));
Routing.RegisterRoute(route, typeof(ShellItem));
}
[Test]
public async Task RelativeGoTo()
{
Routing.RegisterRoute("RelativeGoTo_Page1", typeof(ContentPage));
Routing.RegisterRoute("RelativeGoTo_Page2", typeof(ContentPage));
var shell = new Shell
{
};
var one = new ShellItem { Route = "one" };
var two = new ShellItem { Route = "two" };
var tab11 = MakeSimpleShellSection("tab11", "content");
var tab12 = MakeSimpleShellSection("tab12", "content");
var tab21 = MakeSimpleShellSection("tab21", "content");
var tab22 = MakeSimpleShellSection("tab22", "content");
var tab23 = MakeSimpleShellSection("tab23", "content");
one.Items.Add(tab11);
one.Items.Add(tab12);
two.Items.Add(tab21);
two.Items.Add(tab22);
two.Items.Add(tab23);
shell.Items.Add(one);
shell.Items.Add(two);
await shell.GoToAsync("//two/tab21/");
await shell.NavigationManager.GoToAsync("/tab22", false, true);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tab22/content"));
await shell.NavigationManager.GoToAsync("tab21", false, true);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tab21/content"));
await shell.NavigationManager.GoToAsync("/tab23", false, true);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tab23/content"));
await shell.GoToAsync("RelativeGoTo_Page1", false);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tab23/content/RelativeGoTo_Page1"));
await shell.GoToAsync("../RelativeGoTo_Page2", false);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tab23/content/RelativeGoTo_Page2"));
await shell.GoToAsync("..", false);
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//two/tab23/content"));
/*
* removing support for .. notation for now
await shell.GoToAsync("../one/tab11");
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/one/tab11/content/"));
await shell.GoToAsync("/eee/hm../../../../two/../one/../two/tab21");
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/two/tab21/content/"));
await shell.GoToAsync(new ShellNavigationState("../one/tab11"));
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/one/tab11/content/"));
await shell.GoToAsync(new ShellNavigationState($"../two/tab23/content?{nameof(ShellTestPage.SomeQueryParameter)}=1234"));
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/two/tab23/content/"));
Assert.AreEqual("1234", (two.CurrentItem.CurrentItem.Content as ShellTestPage).SomeQueryParameter);
await shell.GoToAsync(new ShellNavigationState($"../one/tab11#fragment"));
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/one/tab11/content/"));
*/
}
[Test]
public async Task DotDotAdheresToAnimationParameter()
{
Routing.RegisterRoute(nameof(DotDotAdheresToAnimationParameter), typeof(ContentPage));
var shellContent = new ShellContent();
var shell = new TestShell(new TestFlyoutItem(new TestShellSection(shellContent)));
await shell.GoToAsync(nameof(DotDotAdheresToAnimationParameter));
await shell.GoToAsync("..", true);
Assert.IsTrue(shell.LastPopWasAnimated);
}
[Test]
public async Task DefaultRoutesMaintainedIfThatsAllThereIs()
{
Routing.RegisterRoute(nameof(DefaultRoutesMaintainedIfThatsAllThereIs), typeof(ContentPage));
var shell = new Shell();
var shellContent = new ShellContent();
FlyoutItem flyoutItem = new FlyoutItem()
{
Items =
{
shellContent
}
};
shell.Items.Add(flyoutItem);
await shell.GoToAsync(nameof(DefaultRoutesMaintainedIfThatsAllThereIs));
Assume.That(shell.CurrentState.Location.ToString(), Is.EqualTo($"//{Routing.GetRoute(shellContent)}/{nameof(DefaultRoutesMaintainedIfThatsAllThereIs)}"));
await shell.GoToAsync("..");
}
[Test]
public async Task RoutePathDefaultRemovalWithGlobalRoutesKeepsOneDefaultRoute()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem());
Routing.RegisterRoute(nameof(RoutePathDefaultRemovalWithGlobalRoutesKeepsOneDefaultRoute), typeof(ContentPage));
await shell.GoToAsync(nameof(RoutePathDefaultRemovalWithGlobalRoutesKeepsOneDefaultRoute));
// If all routes on the shell are default we still need to make sure it appends something that represents where you are in the
// shell structure
Assert.AreNotEqual($"//{nameof(RoutePathDefaultRemovalWithGlobalRoutesKeepsOneDefaultRoute)}", shell.CurrentState.Location.ToString());
}
[Test]
public async Task RoutePathDefaultRemovalWithGlobalRoutesKeepsOneNamedRoute()
{
var shell = new Shell();
shell.Items.Add(CreateShellItem(shellContentRoute: "content"));
Routing.RegisterRoute(nameof(RoutePathDefaultRemovalWithGlobalRoutesKeepsOneNamedRoute), typeof(ContentPage));
await shell.GoToAsync(nameof(RoutePathDefaultRemovalWithGlobalRoutesKeepsOneNamedRoute));
Assert.AreEqual($"//content/{nameof(RoutePathDefaultRemovalWithGlobalRoutesKeepsOneNamedRoute)}", shell.CurrentState.Location.ToString());
}
[Test]
public async Task OnBackbuttonPressedPageReturnsTrue()
{
TestShell shell = new TestShell();
Routing.RegisterRoute("OnBackbuttonPressedFiresOnPage", typeof(ShellTestPage));
shell.Items.Add(CreateShellItem());
await shell.GoToAsync($"OnBackbuttonPressedFiresOnPage?CancelNavigationOnBackButtonPressed=true");
shell.SendBackButtonPressed();
Assert.AreEqual(2, shell.Navigation.NavigationStack.Count);
}
[Test]
public async Task OnBackbuttonPressedPageReturnsFalse()
{
TestShell shell = new TestShell();
Routing.RegisterRoute("OnBackbuttonPressedFiresOnPage", typeof(ShellTestPage));
shell.Items.Add(CreateShellItem());
await shell.GoToAsync($"OnBackbuttonPressedFiresOnPage?CancelNavigationOnBackButtonPressed=false");
shell.SendBackButtonPressed();
Assert.AreEqual(1, shell.Navigation.NavigationStack.Count);
}
[Test]
public async Task OnBackbuttonPressedShellReturnsTrue()
{
TestShell shell = new TestShell();
Routing.RegisterRoute("OnBackbuttonPressedShellReturnsTrue", typeof(ShellTestPage));
shell.Items.Add(CreateShellItem());
await shell.GoToAsync($"OnBackbuttonPressedShellReturnsTrue");
shell.OnBackButtonPressedFunc = () => true;
shell.SendBackButtonPressed();
Assert.AreEqual(2, shell.Navigation.NavigationStack.Count);
}
[Test]
public void BackButtonBehaviorSet()
{
var page = new ContentPage();
Assert.IsNull(Shell.GetBackButtonBehavior(page));
var backButtonBehavior = new BackButtonBehavior();
Shell.SetBackButtonBehavior(page, backButtonBehavior);
Assert.AreEqual(backButtonBehavior, Shell.GetBackButtonBehavior(page));
}
[Test]
public void ModalSetters()
{
var page = new ContentPage();
Assert.IsFalse(IsModal(page));
Assert.IsTrue(IsAnimated(page));
Shell.SetPresentationMode(page, PresentationMode.Modal | PresentationMode.NotAnimated);
Assert.IsTrue(IsModal(page));
Assert.IsFalse(IsAnimated(page));
}
[Test]
public void BackButtonBehaviorBindingContextPropagation()
{
object bindingContext = new object();
var page = new ContentPage();
var backButtonBehavior = new BackButtonBehavior();
Shell.SetBackButtonBehavior(page, backButtonBehavior);
page.BindingContext = bindingContext;
Assert.AreEqual(page.BindingContext, backButtonBehavior.BindingContext);
}
[Test]
public void BackButtonBehaviorBindingContextPropagationWithExistingBindingContext()
{
object bindingContext = new object();
var page = new ContentPage();
var backButtonBehavior = new BackButtonBehavior();
page.BindingContext = bindingContext;
Shell.SetBackButtonBehavior(page, backButtonBehavior);
Assert.AreEqual(page.BindingContext, backButtonBehavior.BindingContext);
}
[Test]
public void FlyoutHeaderProjection()
{
var shell = new Shell();
var label = new Label();
var viewModel = new Object();
shell.BindingContext = viewModel;
shell.FlyoutHeader = label;
Assert.AreEqual(((IShellController)shell).FlyoutHeader, label);
Label label2 = null;
shell.FlyoutHeaderTemplate = new DataTemplate(() =>
{
return label2 = new Label();
});
Assert.AreEqual(((IShellController)shell).FlyoutHeader, label2);
Assert.AreEqual(((IShellController)shell).FlyoutHeader.BindingContext, viewModel);
shell.FlyoutHeaderTemplate = null;
Assert.AreEqual(((IShellController)shell).FlyoutHeader, label);
}
[Test]
public async Task FlyoutNavigateToImplicitContentPage()
{
var shell = new Shell();
var shellITem = new ShellItem() { FlyoutDisplayOptions = FlyoutDisplayOptions.AsMultipleItems, };
var shellSection = new ShellSection() { Title = "can navigate to" };
shellSection.Items.Add(new ContentPage());
var shellSection2 = new ShellSection() { Title = "can navigate to" };
shellSection2.Items.Add(new ContentPage());
var implicitSection = CreateShellSection(new ContentPage(), asImplicit: true);
shellITem.Items.Add(shellSection);
shellITem.Items.Add(shellSection2);
shellITem.Items.Add(implicitSection);
shell.Items.Add(shellITem);
IShellController shellController = (IShellController)shell;
await shellController.OnFlyoutItemSelectedAsync(shellSection2);
Assert.AreEqual(shellSection2, shell.CurrentItem.CurrentItem);
await shellController.OnFlyoutItemSelectedAsync(shellSection);
Assert.AreEqual(shellSection, shell.CurrentItem.CurrentItem);
await shellController.OnFlyoutItemSelectedAsync(implicitSection);
Assert.AreEqual(implicitSection, shell.CurrentItem.CurrentItem);
}
[Test]
public async Task UriNavigationTests()
{
var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1");
var item2 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent2");
shell.Items.Add(item1);
shell.Items.Add(item2);
shell.GoToAsync("//rootlevelcontent2");
Assert.AreEqual(shell.CurrentItem, item2);
shell.GoToAsync("//rootlevelcontent1");
Assert.AreEqual(shell.CurrentItem, item1);
}
[Test]
public async Task TitleViewBindingContext()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
page.BindingContext = new { Text = "Binding" };
// setup title view
StackLayout layout = new StackLayout() { BackgroundColor = Color.White };
Label label = new Label();
label.SetBinding(Label.TextProperty, "Text");
layout.Children.Add(label);
Shell.SetTitleView(page, layout);
Assert.AreEqual("Binding", label.Text);
page.BindingContext = new { Text = "Binding Changed" };
Assert.AreEqual("Binding Changed", label.Text);
}
[Test]
public async Task VisualPropagationPageLevel()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
// setup title view
StackLayout titleView = new StackLayout() { BackgroundColor = Color.White };
Button button = new Button();
titleView.Children.Add(button);
Shell.SetTitleView(page, titleView);
IVisualController visualController = button as IVisualController;
Assert.AreEqual(page, titleView.Parent);
Assert.AreEqual(VisualMarker.Default, ((IVisualController)button).EffectiveVisual);
page.Visual = VisualMarker.Material;
Assert.AreEqual(VisualMarker.Material, ((IVisualController)button).EffectiveVisual);
}
[Test]
public async Task VisualPropagationShellLevel()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
// setup title view
StackLayout titleView = new StackLayout() { BackgroundColor = Color.White };
Button button = new Button();
titleView.Children.Add(button);
Shell.SetTitleView(page, titleView);
IVisualController visualController = button as IVisualController;
Assert.AreEqual(page, titleView.Parent);
Assert.AreEqual(VisualMarker.Default, ((IVisualController)button).EffectiveVisual);
shell.Visual = VisualMarker.Material;
Assert.AreEqual(VisualMarker.Material, ((IVisualController)button).EffectiveVisual);
}
[Test]
public async Task FlyoutViewVisualPropagation()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
// setup title view
StackLayout flyoutView = new StackLayout() { BackgroundColor = Color.White };
Button button = new Button();
flyoutView.Children.Add(button);
shell.SetValue(Shell.FlyoutHeaderProperty, flyoutView);
IVisualController visualController = button as IVisualController;
Assert.AreEqual(VisualMarker.Default, visualController.EffectiveVisual);
shell.Visual = VisualMarker.Material;
Assert.AreEqual(VisualMarker.Material, visualController.EffectiveVisual);
}
[Test]
public async Task FlyoutViewBindingContext()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
shell.BindingContext = new { Text = "Binding" };
// setup title view
StackLayout flyoutView = new StackLayout() { BackgroundColor = Color.White };
Label label = new Label();
label.SetBinding(Label.TextProperty, "Text");
flyoutView.Children.Add(label);
shell.SetValue(Shell.FlyoutHeaderProperty, flyoutView);
Assert.AreEqual("Binding", label.Text);
shell.BindingContext = new { Text = "Binding Changed" };
Assert.AreEqual("Binding Changed", label.Text);
shell.SetValue(Shell.FlyoutHeaderProperty, new ContentView());
Assert.AreEqual(null, flyoutView.BindingContext);
}
[Test]
public void MenuItemBindingContext()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
shell.BindingContext = new { Text = "Binding" };
object bindingContext = new object();
var menuItem = new MenuItem();
shell.Items.Add(new MenuShellItem(menuItem));
shell.BindingContext = bindingContext;
var menuItem2 = new MenuItem();
shell.Items.Add(new MenuShellItem(menuItem2));
Assert.AreEqual(bindingContext, menuItem.BindingContext);
Assert.AreEqual(bindingContext, menuItem2.BindingContext);
}
[Test]
public async Task TitleViewLogicalChild()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
page.BindingContext = new { Text = "Binding" };
// setup title view
StackLayout layout = new StackLayout() { BackgroundColor = Color.White };
Label label = new Label();
label.SetBinding(Label.TextProperty, "Text");
layout.Children.Add(label);
Shell.SetTitleView(page, layout);
Assert.True(page.ChildrenNotDrawnByThisElement.Contains(layout));
}
[Test]
public async Task FlyoutHeaderLogicalChild()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page));
// setup title view
StackLayout layout = new StackLayout() { BackgroundColor = Color.White };
Label label = new Label();
label.SetBinding(Label.TextProperty, "Text");
layout.Children.Add(label);
shell.FlyoutHeader = null;
shell.FlyoutHeader = layout;
Assert.True(shell.LogicalChildren.Contains(layout));
shell.FlyoutHeader = null;
Assert.False(shell.LogicalChildren.Contains(layout));
}
[Test]
public async Task ShellFlyoutChangeableOnShellWithFlyoutItem()
{
Shell shell = new Shell();
var flyoutItem = CreateShellItem<FlyoutItem>();
shell.Items.Add(flyoutItem);
Assert.AreEqual(FlyoutBehavior.Flyout, shell.GetEffectiveFlyoutBehavior());
shell.FlyoutBehavior = FlyoutBehavior.Locked;
Assert.AreEqual(FlyoutBehavior.Locked, shell.GetEffectiveFlyoutBehavior());
shell.FlyoutBehavior = FlyoutBehavior.Disabled;
Assert.AreEqual(FlyoutBehavior.Disabled, shell.GetEffectiveFlyoutBehavior());
}
[Test]
public async Task ShellFlyoutChangeableOnShellWithTabBar()
{
Shell shell = new Shell();
var tabBarItem = CreateShellItem<TabBar>();
shell.Items.Add(tabBarItem);
Assert.AreEqual(FlyoutBehavior.Disabled, shell.GetEffectiveFlyoutBehavior());
shell.FlyoutBehavior = FlyoutBehavior.Flyout;
Assert.AreEqual(FlyoutBehavior.Flyout, shell.GetEffectiveFlyoutBehavior());
shell.FlyoutBehavior = FlyoutBehavior.Locked;
Assert.AreEqual(FlyoutBehavior.Locked, shell.GetEffectiveFlyoutBehavior());
}
[Test]
public async Task ShellFlyoutBehaviorCalculation()
{
Shell shell = new Shell();
ContentPage page = new ContentPage();
shell.Items.Add(CreateShellItem(page: page));
Assert.AreEqual(FlyoutBehavior.Flyout, shell.GetEffectiveFlyoutBehavior());
Shell.SetFlyoutBehavior(page, FlyoutBehavior.Disabled);
Shell.SetFlyoutBehavior(shell.Items[0].Items[0].Items[0], FlyoutBehavior.Flyout);
Shell.SetFlyoutBehavior(shell.Items[0].Items[0], FlyoutBehavior.Disabled);
Shell.SetFlyoutBehavior(shell.Items[0], FlyoutBehavior.Locked);
Assert.AreEqual(FlyoutBehavior.Disabled, shell.GetEffectiveFlyoutBehavior());
page.ClearValue(Shell.FlyoutBehaviorProperty);
Assert.AreEqual(FlyoutBehavior.Flyout, shell.GetEffectiveFlyoutBehavior());
shell.Items[0].Items[0].Items[0].ClearValue(Shell.FlyoutBehaviorProperty);
Assert.AreEqual(FlyoutBehavior.Disabled, shell.GetEffectiveFlyoutBehavior());
shell.Items[0].Items[0].ClearValue(Shell.FlyoutBehaviorProperty);
Assert.AreEqual(FlyoutBehavior.Locked, shell.GetEffectiveFlyoutBehavior());
shell.Items[0].ClearValue(Shell.FlyoutBehaviorProperty);
Assert.AreEqual(FlyoutBehavior.Flyout, shell.GetEffectiveFlyoutBehavior());
}
[Test]
public async Task TabBarAutoCreation()
{
Shell shell = new Shell();
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
Assert.AreEqual(1, shell.Items.Count);
Assert.AreEqual(3, shell.Items[0].Items.Count);
Assert.AreEqual(FlyoutBehavior.Disabled, shell.GetEffectiveFlyoutBehavior());
shell = new Shell();
shell.Items.Add(new TabBar());
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
Assert.AreEqual(2, shell.Items.Count);
Assert.AreEqual(0, shell.Items[0].Items.Count);
Assert.AreEqual(3, shell.Items[1].Items.Count);
shell = new Shell();
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(new TabBar());
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
shell.Items.Add(ShellItem.CreateFromShellSection(CreateShellSection<Tab>()));
Assert.AreEqual(3, shell.Items.Count);
Assert.AreEqual(3, shell.Items[0].Items.Count);
Assert.AreEqual(0, shell.Items[1].Items.Count);
Assert.AreEqual(3, shell.Items[0].Items.Count);
}
[Test]
public async Task NavigatedFiresAfterContentIsCreatedWhenUsingTemplate()
{
var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1");
shell.Items.Add(item1);
Routing.RegisterRoute("cat", typeof(ContentPage));
Routing.RegisterRoute("details", typeof(ContentPage));
await shell.GoToAsync("cat");
await shell.GoToAsync("details");
Assert.AreEqual("//rootlevelcontent1/cat/details", shell.CurrentState.Location.ToString());
await shell.GoToAsync("//rootlevelcontent1/details");
Assert.AreEqual("//rootlevelcontent1/details", shell.CurrentState.Location.ToString());
}
[Test]
public async Task ShellItemNotVisibleWhenContentPageNotVisible()
{
var shell = new Shell();
var item1 = CreateShellItem(new ContentPage() { IsVisible = false });
var item2 = CreateShellItem();
shell.Items.Add(item1);
shell.Items.Add(item2);
Assert.IsFalse(GetItems(shell).Contains(item1));
Assert.IsTrue(GetItems(shell).Contains(item2));
}
[Test]
public async Task BaseShellItemNotVisible()
{
var shell = new Shell();
var item1 = CreateShellItem();
var item2 = CreateShellItem();
shell.Items.Add(item1);
shell.Items.Add(item2);
item1.IsVisible = false;
Assert.IsFalse(GetItems(shell).Contains(item1));
Assert.IsTrue(GetItems(shell).Contains(item2));
item1.IsVisible = true;
Assert.IsTrue(GetItems(shell).Contains(item1));
item1.Items[0].IsVisible = false;
Assert.IsFalse(GetItems(shell).Contains(item1));
item1.Items[0].IsVisible = true;
Assert.IsTrue(GetItems(shell).Contains(item1));
item1.Items[0].Items[0].IsVisible = false;
Assert.IsFalse(GetItems(shell).Contains(item1));
item1.Items[0].Items[0].IsVisible = true;
Assert.IsTrue(GetItems(shell).Contains(item1));
}
[Test]
public async Task CantNavigateToNotVisibleShellItem()
{
var shell = new Shell();
var item1 = CreateShellItem(shellItemRoute: "NotVisible");
var item2 = CreateShellItem();
shell.Items.Add(item1);
shell.Items.Add(item2);
item1.IsVisible = false;
Assert.That(async () => await shell.GoToAsync($"//NotVisible"), Throws.Exception);
Assert.AreEqual(shell.CurrentItem, item2);
}
[Test]
public async Task FlyoutItemVisible()
{
var shell = new Shell();
var item1 = CreateShellItem<FlyoutItem>(shellItemRoute: "NotVisible");
var item2 = CreateShellItem();
shell.Items.Add(item1);
shell.Items.Add(item2);
Shell.SetFlyoutItemIsVisible(item1, false);
Assert.IsTrue(GetItems(shell).Contains(item1));
bool hasFlyoutItem =
(shell as IShellController)
.GenerateFlyoutGrouping()
.SelectMany(i => i)
.Contains(item1);
Assert.IsFalse(hasFlyoutItem);
}
[Test]
public async Task ShellContentCollectionClear()
{
var shell = new Shell();
var item1 = CreateShellItem();
var section2 = CreateShellSection();
shell.Items.Add(item1);
item1.Items.Add(section2);
var mainTab = item1.Items[0];
var content1 = CreateShellContent();
var clearedContent = mainTab.Items[0];
mainTab.Items.Clear();
mainTab.Items.Add(content1);
mainTab.Items.Add(CreateShellContent());
Assert.IsNull(clearedContent.Parent);
Assert.AreEqual(2, mainTab.Items.Count);
Assert.AreEqual(content1, mainTab.CurrentItem);
}
[Test]
public async Task ShellItemCollectionClear()
{
var shell = new Shell();