From 9b44c9a26d7dc2de3b75b36853cd689338e33e84 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Sat, 6 Jul 2024 17:23:54 -0500 Subject: [PATCH] Move PopLifeCycle to device tests (#23468) --- .../NavigationPageLifecycleTests.cs | 1 - .../NavigationPage/NavigationPageTests.cs | 49 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs b/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs index 03df2d513051..36ca78d4a4de 100644 --- a/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs +++ b/src/Controls/tests/Core.UnitTests/NavigationPageLifecycleTests.cs @@ -57,7 +57,6 @@ public async Task PushLifeCycle(bool useMaui) [Theory] [InlineData(false)] - [InlineData(true)] public async Task PopLifeCycle(bool useMaui) { bool appearingShouldFireOnInitialPage = false; diff --git a/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs b/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs index 43bccb1d2ad7..3b5cb5727529 100644 --- a/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs @@ -102,6 +102,55 @@ await CreateHandlerAndAddToWindow(new Window(navPage), async Assert.False(pageFiredNavigated); } + [Fact] + public async Task PopLifeCycle() + { + SetupBuilder(); + + bool appearingShouldFireOnInitialPage = false; + ContentPage initialPage = new ContentPage(); + ContentPage pushedPage = new ContentPage(); + + ContentPage rootPageFiresAppearingAfterPop = null; + ContentPage pageDisappeared = null; + + NavigationPage nav = new NavigationPage(initialPage); + + // Because of queued change propagation on BPs + // sometimes the appearing will fire a bit later than we expect. + // This ensures the first one fires before we move on + TaskCompletionSource waitForFirstAppearing = new TaskCompletionSource(); + initialPage.Appearing += OnInitialPageAppearing; + void OnInitialPageAppearing(object sender, EventArgs e) + { + waitForFirstAppearing.SetResult(); + initialPage.Appearing -= OnInitialPageAppearing; + } + + await CreateHandlerAndAddToWindow(nav, async () => + { + await waitForFirstAppearing.Task.WaitAsync(TimeSpan.FromSeconds(2)); + initialPage.Appearing += (sender, _) => + { + Assert.True(appearingShouldFireOnInitialPage); + rootPageFiresAppearingAfterPop = (ContentPage)sender; + }; + + pushedPage.Disappearing += (sender, _) + => pageDisappeared = (ContentPage)sender; + + await nav.PushAsync(pushedPage).WaitAsync(TimeSpan.FromSeconds(2)); + Assert.Null(rootPageFiresAppearingAfterPop); + appearingShouldFireOnInitialPage = true; + Assert.Null(pageDisappeared); + + await nav.PopAsync().WaitAsync(TimeSpan.FromSeconds(2)); + + Assert.Equal(initialPage, rootPageFiresAppearingAfterPop); + Assert.Equal(pushedPage, pageDisappeared); + }); + } + #if !IOS && !MACCATALYST [Fact(DisplayName = "Swapping Navigation Toggles BackButton Correctly")]