From 1612ccbe0af77d85fdc060b2ee11419f2c2e2366 Mon Sep 17 00:00:00 2001 From: kubaflo Date: Thu, 25 Jan 2024 10:28:04 +0100 Subject: [PATCH 1/4] Throw exception when pushing outside NavigationPage (#2013) --- src/Controls/src/Core/NavigationProxy.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Controls/src/Core/NavigationProxy.cs b/src/Controls/src/Core/NavigationProxy.cs index e6fd40bee8a9..e65cd09c90ab 100644 --- a/src/Controls/src/Core/NavigationProxy.cs +++ b/src/Controls/src/Core/NavigationProxy.cs @@ -212,8 +212,7 @@ protected virtual Task OnPushAsync(Page page, bool animated) INavigation currentInner = Inner; if (currentInner is null) { - _pushStack.Value.Add(page); - return Task.FromResult(page); + throw new InvalidOperationException("Page must be wrapped into a navigation page to perform navigation"); } return currentInner.PushAsync(page, animated); } From cc770c838185ebf900455265ff81f0359f237219 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Fri, 8 Mar 2024 01:55:29 +0100 Subject: [PATCH 2/4] Added & modified unit tests (#2013) --- src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs b/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs index 83d528bbfc23..cfc2e46f1f19 100644 --- a/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs +++ b/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs @@ -839,6 +839,15 @@ public async Task PopModalWithEmptyStackThrows() Assert.ThrowsAsync(() => window.Navigation.PopModalAsync()); } + [Fact] + public async Task PushWithoutWrappingIntoNavigationPage() + { + var window = new TestWindow(new ContentPage()); + var contentPage1 = new ContentPage(); + var navigationPage = new TestNavigationPage(true, contentPage1); + Assert.ThrowsAsync(() => window.Navigation.PushAsync(contentPage1)); + } + [Fact] public async Task TabBarSetsOnFlyoutPageInsideModalPage() { From 0b19c46ad4d49ec6a59a418254add980512fe1f9 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Fri, 8 Mar 2024 01:55:14 +0100 Subject: [PATCH 3/4] Improvements after live (#2013) --- src/Controls/src/Core/NavigationProxy.cs | 3 ++- src/Controls/src/Core/Window/Window.cs | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Controls/src/Core/NavigationProxy.cs b/src/Controls/src/Core/NavigationProxy.cs index e65cd09c90ab..e6fd40bee8a9 100644 --- a/src/Controls/src/Core/NavigationProxy.cs +++ b/src/Controls/src/Core/NavigationProxy.cs @@ -212,7 +212,8 @@ protected virtual Task OnPushAsync(Page page, bool animated) INavigation currentInner = Inner; if (currentInner is null) { - throw new InvalidOperationException("Page must be wrapped into a navigation page to perform navigation"); + _pushStack.Value.Add(page); + return Task.FromResult(page); } return currentInner.PushAsync(page, animated); } diff --git a/src/Controls/src/Core/Window/Window.cs b/src/Controls/src/Core/Window/Window.cs index 392fa4be5507..08fe87339311 100644 --- a/src/Controls/src/Core/Window/Window.cs +++ b/src/Controls/src/Core/Window/Window.cs @@ -679,6 +679,11 @@ public NavigationImpl(Window owner) _owner = owner; } + protected override Task OnPushAsync(Page page, bool animated) + { + throw new InvalidOperationException("Page must be wrapped into a navigation page to perform navigation"); + } + protected override IReadOnlyList GetModalStack() { return _owner.ModalNavigationManager.ModalStack; From 45b614b4b3af3953bd104c19882dfe25d49b6095 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Mon, 18 Mar 2024 00:46:34 +0100 Subject: [PATCH 4/4] Added more overrides (#2013) --- src/Controls/src/Core/Window/Window.cs | 22 ++++++++++++++++++- .../Core.UnitTests/NavigationUnitTest.cs | 16 +++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Controls/src/Core/Window/Window.cs b/src/Controls/src/Core/Window/Window.cs index 08fe87339311..be74cc503588 100644 --- a/src/Controls/src/Core/Window/Window.cs +++ b/src/Controls/src/Core/Window/Window.cs @@ -679,9 +679,29 @@ public NavigationImpl(Window owner) _owner = owner; } + protected override void OnInsertPageBefore(Page page, Page before) + { + throw new InvalidOperationException("InsertPageBefore is not supported, please use a NavigationPage."); + } + protected override Task OnPushAsync(Page page, bool animated) { - throw new InvalidOperationException("Page must be wrapped into a navigation page to perform navigation"); + throw new InvalidOperationException("PushAsync is not supported, please use a NavigationPage."); + } + + protected override Task OnPopAsync(bool animated) + { + throw new InvalidOperationException("PopAsync is not supported, please use a NavigationPage."); + } + + protected override Task OnPopToRootAsync(bool animated) + { + throw new InvalidOperationException("PopToRootAsync is not supported, please use a NavigationPage."); + } + + protected override void OnRemovePage(Page page) + { + throw new InvalidOperationException("RemovePage is not supported, please use a NavigationPage."); } protected override IReadOnlyList GetModalStack() diff --git a/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs b/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs index cfc2e46f1f19..d5b35cba4a53 100644 --- a/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs +++ b/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs @@ -840,7 +840,21 @@ public async Task PopModalWithEmptyStackThrows() } [Fact] - public async Task PushWithoutWrappingIntoNavigationPage() + public async Task InvalidOperationExceptionIsThrownWhenNavigatingOutsideNavigationPage() + { + var window = new TestWindow(new ContentPage()); + var contentPage1 = new ContentPage(); + var contentPage2 = new ContentPage(); + + Assert.ThrowsAsync(() => window.Navigation.PushAsync(contentPage1)); + Assert.ThrowsAsync(() => window.Navigation.PopAsync()); + Assert.ThrowsAsync(() => window.Navigation.PopToRootAsync()); + Assert.Throws(() => window.Navigation.InsertPageBefore(contentPage1, contentPage2)); + Assert.Throws(() => window.Navigation.RemovePage(contentPage1)); + } + + [Fact] + public async Task RemoveWrappingIntoNavigationPage() { var window = new TestWindow(new ContentPage()); var contentPage1 = new ContentPage();