1+ using CommunityToolkit . Maui . Extensions ;
2+ using CommunityToolkit . Maui . UnitTests . Mocks ;
3+ using CommunityToolkit . Maui . UnitTests . Services ;
4+ using Xunit ;
5+
6+ namespace CommunityToolkit . Maui . UnitTests . Extensions ;
7+
8+ public class NavigatedToEventArgsExtensionsTests : BaseViewTest
9+ {
10+ [ Fact ]
11+ public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnTrue ( )
12+ {
13+ // Arrange
14+ TaskCompletionSource < bool ? > wasPreviousPageACommunityToolkitPopupPageTCS = new ( ) ;
15+ var application = ( MockApplication ) ServiceProvider . GetRequiredService < IApplication > ( ) ;
16+ var selfClosingPopup = ServiceProvider . GetRequiredService < ShortLivedSelfClosingPopup > ( ) ;
17+ var popupService = ServiceProvider . GetRequiredService < IPopupService > ( ) ;
18+
19+ var shell = ( Shell ) ( application . Windows [ 0 ] . Page ?? throw new InvalidOperationException ( "Unable to retrieve Shell" ) ) ;
20+ var mainPage = shell . CurrentPage ;
21+ var shellContentPage = new ShellContentPage ( ) ;
22+ shellContentPage . NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived ;
23+
24+ var shellParameters = new Dictionary < string , object >
25+ {
26+ { nameof ( ContentPage . BackgroundColor ) , Colors . Orange }
27+ } ;
28+
29+
30+ // Act
31+ await mainPage . Navigation . PushAsync ( shellContentPage ) ;
32+ await popupService . ShowPopupAsync < ShortLivedMockPageViewModel > ( shell , null , shellParameters , TestContext . Current . CancellationToken ) ;
33+ var wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS . Task ;
34+
35+ // Assert
36+ Assert . True ( wasPreviousPageACommunityToolkitPopupPage ) ;
37+
38+ void HandleNavigatedToEventArgsReceived ( object ? sender , NavigatedToEventArgs e )
39+ {
40+ if ( e . PreviousPage != mainPage )
41+ {
42+ wasPreviousPageACommunityToolkitPopupPageTCS . SetResult ( e . WasPreviousPageACommunityToolkitPopupPage ( ) ) ;
43+ }
44+ }
45+ }
46+
47+ [ Fact ]
48+ public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnFalse ( )
49+ {
50+ // Arrange
51+ TaskCompletionSource < bool ? > wasPreviousPageACommunityToolkitPopupPageTCS = new ( ) ;
52+ var application = ( MockApplication ) ServiceProvider . GetRequiredService < IApplication > ( ) ;
53+ var selfClosingPopup = ServiceProvider . GetRequiredService < ShortLivedSelfClosingPopup > ( ) ;
54+ var popupService = ServiceProvider . GetRequiredService < IPopupService > ( ) ;
55+
56+ var shell = ( Shell ) ( application . Windows [ 0 ] . Page ?? throw new InvalidOperationException ( "Unable to retrieve Shell" ) ) ;
57+ var mainPage = shell . CurrentPage ;
58+ var shellContentPage = new ShellContentPage ( ) ;
59+ shellContentPage . NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived ;
60+
61+ var shellParameters = new Dictionary < string , object >
62+ {
63+ { nameof ( ContentPage . BackgroundColor ) , Colors . Orange }
64+ } ;
65+
66+
67+ // Act
68+ await mainPage . Navigation . PushAsync ( shellContentPage ) ;
69+ var wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS . Task ;
70+
71+ // Assert
72+ Assert . False ( wasPreviousPageACommunityToolkitPopupPage ) ;
73+
74+ void HandleNavigatedToEventArgsReceived ( object ? sender , NavigatedToEventArgs e )
75+ {
76+ wasPreviousPageACommunityToolkitPopupPageTCS . SetResult ( e . WasPreviousPageACommunityToolkitPopupPage ( ) ) ;
77+ }
78+ }
79+
80+ sealed class ShellContentPage : ContentPage
81+ {
82+ public event EventHandler < NavigatedToEventArgs > ? NavigatedToEventArgsReceived ;
83+
84+ protected override void OnNavigatedTo ( NavigatedToEventArgs args )
85+ {
86+ base . OnNavigatedTo ( args ) ;
87+ NavigatedToEventArgsReceived ? . Invoke ( this , args ) ;
88+ }
89+ }
90+ }
0 commit comments