Skip to content

Commit

Permalink
Merge pull request #18983 from unoplatform/dev/mazi/exception-on-navi…
Browse files Browse the repository at this point in the history
…gate
  • Loading branch information
MartinZikmund authored Dec 8, 2024
2 parents 952e47f + 5249fba commit 49ad730
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,58 @@ public void When_NavigatingBetweenPages()
Assert.IsTrue(_navigateOrderTracker.FrameNavigated);
}

[TestMethod]
public async Task When_Exception_In_Page_Ctor()
{
var SUT = new Frame()
{
Width = 200,
Height = 200
};

#if HAS_UNO
bool navigationFailed = false;
SUT.NavigationFailed += (s, e) => navigationFailed = true;
#endif

TestServices.WindowHelper.WindowContent = SUT;
await TestServices.WindowHelper.WaitForLoaded(SUT);

var exception = Assert.ThrowsException<NotSupportedException>(() => SUT.Navigate(typeof(ExceptionInCtorPage)));
Assert.AreEqual("Crashed", exception.Message);
#if HAS_UNO
if (FeatureConfiguration.Frame.UseWinUIBehavior)
{
// This is only valid with WinUI Frame behavior
Assert.IsFalse(navigationFailed);
}
#endif
}

[TestMethod]
public async Task When_Exception_In_OnNavigatedTo()
{
var SUT = new Frame()
{
Width = 200,
Height = 200
};

bool navigationFailed = false;
SUT.NavigationFailed += (s, e) =>
{
navigationFailed = true;
e.Handled = true;
};

TestServices.WindowHelper.WindowContent = SUT;
await TestServices.WindowHelper.WaitForLoaded(SUT);

var exception = Assert.ThrowsException<NotSupportedException>(() => SUT.Navigate(typeof(ExceptionInOnNavigatedToPage)));
Assert.AreEqual("Crashed", exception.Message);
Assert.IsTrue(navigationFailed);
}

[TestCleanup]
public void Cleanup()
{
Expand Down Expand Up @@ -613,3 +665,28 @@ private async void FrameNavigateFirstPage_Loaded(object sender, Microsoft.UI.Xam
public partial class FrameNavigateSecondPage : Page
{
}

public partial class ExceptionInCtorPage : Page
{
public ExceptionInCtorPage()
{
throw new NotSupportedException("Crashed");
}
}

public partial class ExceptionInOnNavigatedToPage : Page
{
public ExceptionInOnNavigatedToPage()
{
}

protected
#if HAS_UNO
internal
#endif
override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
throw new NotSupportedException("Crashed");
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Frame/Frame.legacy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private bool InnerNavigate(PageStackEntry entry, NavigationMode mode)
Application.Current.RaiseRecoverableUnhandledException(new InvalidOperationException("Navigation failed", exception));
}

return false;
throw;
}
finally
{
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Frame/Frame.partial.mux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ private bool NavigateWithTransitionInfoImpl(Type sourcePageType, object paramete
catch
{
pCanNavigate = false;
throw;
}
Cleanup:
;
Expand Down Expand Up @@ -585,6 +586,8 @@ private void ChangeContent(object oldObject, object newObject, object parameter,
{
Content = oldObject;
}

throw;
}
}

Expand Down

0 comments on commit 49ad730

Please sign in to comment.