Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Windows CTD on Application Close (#2067)
* Add null checks for MediaElement in update methods Improved robustness by adding null checks for `MediaElement` in `PlatformUpdatePosition` and `PlatformUpdateShouldKeepScreenOn` methods to prevent potential null reference exceptions. Removed redundant null checks in `PlatformUpdateShouldKeepScreenOn` to streamline the logic and enhance code readability. * Update src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs Co-authored-by: Shaun Lawrence <shaunrlawrence@gmail.com> * Add ParentWindow struct and refactor null checks Introduced a new `ParentWindow` record struct in the `MediaManager` class to handle null checks for the parent window and its handler. Added a static property `Exists` within `ParentWindow` to perform these checks and log errors if necessary. Updated the `PlatformUpdatePosition` method to return early if the parent window does not exist, preventing position updates when the window is closed. Simplified the `PlatformUpdateShouldKeepScreenOn` method by removing a redundant check for `MediaElement.ShouldKeepScreenOn`. * Fix accidental change * Add MediaManagerWindowsTests and ParentWindow struct Introduce MediaManagerWindowsTests in MediaManager.windows.Tests.cs. Add ParentWindow record struct with static properties CurrentPage and Exists. Implement four Xunit tests to verify ParentWindow.Exists behavior under various conditions. * Refactor ParentWindow tests into separate file Moved ParentWindow tests from MediaManager.windows.Tests.cs to a new file, ParentWindowTests.cs. Updated and reorganized test cases for ParentWindow.Exists, including scenarios for null parent window, null handler, and null platform view. Introduced MockWindowHandler class for simulating handler behavior. Adjusted using directives and namespace declarations to reflect the new file structure. * Refactor ParentWindow struct into PageExtensions class Refactored the `ParentWindow` record struct to consolidate its definition within the `PageExtensions` class. This change involved: - Adding a new `ParentWindow` record struct in `PageExtensions.cs` with static properties `CurrentPage` and `Exists`. - Removing the `ParentWindow` record struct from `MediaManager.windows.cs` and adding a `using` directive to import it from `CommunityToolkit.Maui.Extensions.PageExtensions`. - Removing the `ParentWindow` record struct from `ParentWindowTests.cs` and adding a `using` directive to import it from `CommunityToolkit.Maui.Extensions.PageExtensions`. - Ensuring the `ParentWindowTests` class retains its test method `Exists_WhenParentWindowIsNull_ReturnsFalse` to verify the `Exists` property behavior. * Refactor tests and add MockWindowHandler for unit testing Simplified GetCurrentPage call in PageExtensions.cs. Changed ParentWindow struct visibility to internal. Refactored ParentWindowTests.cs for better readability. Added MockWindowHandler class for IElementHandler mocking. --------- Co-authored-by: Shaun Lawrence <shaunrlawrence@gmail.com>
- Loading branch information