Skip to content

Commit

Permalink
Clear window from ContextStub after it's closed (#15495)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored and StephaneDelcroix committed Jun 19, 2023
1 parent b97865c commit 7cdb37c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Core/tests/DeviceTests.Shared/Stubs/ContextStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,35 @@ public object GetService(Type serviceType)
return _windowManager ??= new NavigationRootManager((UI.Xaml.Window)this.GetService(typeof(UI.Xaml.Window)));

if (serviceType == typeof(UI.Xaml.Window))
return _window ??= (_services.GetService<UI.Xaml.Window>() ?? new UI.Xaml.Window());
{
if (_window is not null)
return _window;

_window = (_services.GetService<UI.Xaml.Window>() ?? new UI.Xaml.Window());

// If a single test needs to open multiple windows then we need to clear the window
// once it's closed. This way, the next request will get a new window instead of
// trying to reuse a closed window and crashing.
_window.Closed += OnWindowClosed;
return _window;
}
#endif
if (serviceType == typeof(IDispatcher))
return _services.GetService(serviceType) ?? TestDispatcher.Current;

return _services.GetService(serviceType);
}

#if WINDOWS
void OnWindowClosed(object sender, UI.Xaml.WindowEventArgs args)
{
if (sender is UI.Xaml.Window w)
w.Closed -= OnWindowClosed;

_window = null;
}
#endif

public IMauiHandlersFactory Handlers =>
Services.GetRequiredService<IMauiHandlersFactory>();

Expand Down

0 comments on commit 7cdb37c

Please sign in to comment.