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

fix: Fix WaitForIdle to really wait for "Idle" #157

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ internal set
/// <returns></returns>
public static async Task WaitForIdle()
{
await RootElementDispatcher.RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { });
await RootElementDispatcher.RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { });
await RootElementDispatcher.RunIdleAsync(_ => { /* Empty to wait for the idle queue to be reached */ });
await RootElementDispatcher.RunIdleAsync(_ => { /* Empty to wait for the idle queue to be reached */ });
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,50 @@ public partial class UnitTestDispatcherCompat
{
private readonly _Impl _impl;

#if WINDOWS_WINUI || HAS_UNO_WINUI

private readonly Windows.UI.Core.CoreDispatcher? _dispatcher;
public UnitTestDispatcherCompat(_Impl impl, Windows.UI.Core.CoreDispatcher? dispatcher = null)
{
this._impl = impl;
this._dispatcher = dispatcher;
}
#else
public UnitTestDispatcherCompat(_Impl impl)
{
this._impl = impl;
}
#endif

public Windows.Foundation.IAsyncAction RunIdleAsync(Windows.UI.Core.IdleDispatchedHandler agileCallback)
{
#if !WINDOWS_WINUI && !HAS_UNO_WINUI
// Windows UWP and Uno UWP can use CoreDispatcher.RunIdleAsync without issues.
return _impl.RunIdleAsync(agileCallback);
#else
// For now, Uno WinUI Dispatcher is non-null.
// In the future, this will break and it will be null.
if (_dispatcher is not null)
{
return _dispatcher.RunIdleAsync(agileCallback);
}

// This code path is for Windows WinUI, and "potentially" Uno WinUI in future when the breaking change is taken.
// This is a wrong implementation. It doesn't really wait for "Idle".
return RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { }).AsAsyncAction();
#endif
}

public static UnitTestDispatcherCompat From(UIElement x) =>
#if HAS_UNO_WINUI || WINDOWS_WINUI
new UnitTestDispatcherCompat(x.DispatcherQueue);
new UnitTestDispatcherCompat(x.DispatcherQueue, x.Dispatcher);
#else
new UnitTestDispatcherCompat(x.Dispatcher);
#endif

public static UnitTestDispatcherCompat From(Window x) =>
#if HAS_UNO_WINUI || WINDOWS_WINUI
new UnitTestDispatcherCompat(x.DispatcherQueue);
new UnitTestDispatcherCompat(x.DispatcherQueue, x.Dispatcher);
#else
new UnitTestDispatcherCompat(x.Dispatcher);
#endif
Expand Down
Loading