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 NavigatedFromEventArgsExtensionsTests : BaseViewTest
9+ {
10+ [ Fact ]
11+ public async Task NavigatedFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnTrue ( )
12+ {
13+ // Arrange
14+ TaskCompletionSource < bool ? > isDestinationPageACommunityToolkitPopupPageTCS = new ( ) ;
15+ var application = ( MockApplication ) ServiceProvider . GetRequiredService < IApplication > ( ) ;
16+ var popupService = ServiceProvider . GetRequiredService < IPopupService > ( ) ;
17+
18+ var shell = ( Shell ) ( application . Windows [ 0 ] . Page ?? throw new InvalidOperationException ( "Unable to retrieve Shell" ) ) ;
19+ var mainPage = shell . CurrentPage ;
20+ var shellContentPage = new ShellContentPage ( ) ;
21+ shellContentPage . NavigatedFromEventArgsReceived += HandleNavigatedFromEventArgsReceived ;
22+
23+ var shellParameters = new Dictionary < string , object >
24+ {
25+ { nameof ( ContentPage . BackgroundColor ) , Colors . Orange }
26+ } ;
27+
28+
29+ // Act
30+ await mainPage . Navigation . PushAsync ( shellContentPage ) ;
31+ await popupService . ShowPopupAsync < ShortLivedMockPageViewModel > ( shell , null , shellParameters , TestContext . Current . CancellationToken ) ;
32+ var isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS . Task ;
33+
34+ // Assert
35+ Assert . True ( isDestinationPageACommunityToolkitPopupPage ) ;
36+
37+ void HandleNavigatedFromEventArgsReceived ( object ? sender , NavigatedFromEventArgs e )
38+ {
39+ isDestinationPageACommunityToolkitPopupPageTCS . SetResult ( e . IsDestinationPageACommunityToolkitPopupPage ( ) ) ;
40+ }
41+ }
42+
43+ [ Fact ]
44+ public async Task NavigatedFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnFalse ( )
45+ {
46+ // Arrange
47+ TaskCompletionSource < bool ? > isDestinationPageACommunityToolkitPopupPageTCS = new ( ) ;
48+ var application = ( MockApplication ) ServiceProvider . GetRequiredService < IApplication > ( ) ;
49+
50+ var shell = ( Shell ) ( application . Windows [ 0 ] . Page ?? throw new InvalidOperationException ( "Unable to retrieve Shell" ) ) ;
51+ var mainPage = shell . CurrentPage ;
52+ var shellContentPage = new ShellContentPage ( ) ;
53+ shellContentPage . NavigatedFromEventArgsReceived += HandleNavigatedFromEventArgsReceived ;
54+ var newShellContentPage = new ShellContentPage ( ) ;
55+
56+
57+ // Act
58+ await mainPage . Navigation . PushAsync ( shellContentPage ) ;
59+ //push a new content page on top to make sure the navigation handler doesn't think we're navigating to a popup page
60+ await mainPage . Navigation . PushAsync ( newShellContentPage ) ;
61+ var isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS . Task ;
62+
63+ // Assert
64+ Assert . False ( isDestinationPageACommunityToolkitPopupPage ) ;
65+
66+ void HandleNavigatedFromEventArgsReceived ( object ? sender , NavigatedFromEventArgs e )
67+ {
68+ isDestinationPageACommunityToolkitPopupPageTCS . SetResult ( e . IsDestinationPageACommunityToolkitPopupPage ( ) ) ;
69+ }
70+ }
71+
72+ sealed class ShellContentPage : ContentPage
73+ {
74+ public event EventHandler < NavigatedFromEventArgs > ? NavigatedFromEventArgsReceived ;
75+
76+ protected override void OnNavigatedFrom ( NavigatedFromEventArgs args )
77+ {
78+ base . OnNavigatedFrom ( args ) ;
79+ NavigatedFromEventArgsReceived ? . Invoke ( this , args ) ;
80+ }
81+ }
82+ }
0 commit comments