Skip to content

Commit

Permalink
fix: Ensure each test ends with non-keyboard input device
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Oct 5, 2024
1 parent 04920af commit c60e172
Showing 1 changed file with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ async Task InvokeTestMethod(TestCase testCase)
canRetry = false;
var cleanupActions = new List<Func<Task>>
{
CloseRemainingPopupsAsync
GenericCleanupAsync
};

try
Expand Down Expand Up @@ -976,18 +976,14 @@ await TestServices.WindowHelper.RootElementDispatcher.RunAsync(() =>
}
}

async Task CloseRemainingPopupsAsync()
async Task GenericCleanupAsync()
{
await TestServices.WindowHelper.RootElementDispatcher.RunAsync(() =>
{
var popups = VisualTreeHelper.GetOpenPopupsForXamlRoot(TestServices.WindowHelper.XamlRoot);
if (popups.Count > 0)
{
foreach (var popup in popups)
{
popup.IsOpen = false;
}
}
CloseRemainingPopups();
#if HAS_UNO
ResetLastInputDeviceType();
#endif
});
}

Expand Down Expand Up @@ -1035,6 +1031,30 @@ async ValueTask WaitResult(object returnValue, string step)
}
}

private static void CloseRemainingPopups()
{
var popups = VisualTreeHelper.GetOpenPopupsForXamlRoot(TestServices.WindowHelper.XamlRoot);
if (popups.Count > 0)
{
foreach (var popup in popups)
{
popup.IsOpen = false;
}
}
}

#if HAS_UNO
private static void ResetLastInputDeviceType()
{
// Some tests inject keyboard input, which can then mean that subsequent tests will display
// system focus visuals which are unexpected. This resets the last input device type to touch.
if (TestServices.WindowHelper.XamlRoot?.VisualTree?.ContentRoot?.InputManager is { } inputManager)
{
inputManager.LastInputDeviceType = Xaml.Input.InputDeviceType.Touch;
}
}
#endif

private async ValueTask ExecuteOnDispatcher(Func<Task> asyncAction, CancellationToken ct = default)
{
var tcs = new TaskCompletionSource<object>();
Expand Down

0 comments on commit c60e172

Please sign in to comment.