Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear window from ContextStub after it's closed #15495

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// 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