From d12e886ebefc3d6193832e8c1165b5c85b8c5ae4 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Wed, 20 Dec 2023 12:54:27 +0200 Subject: [PATCH 1/3] fix: Fix WaitForIdle in UWP to really wait for "Idle" --- .../Engine/UI/UnitTestsUIContentHelper.cs | 6 ++++++ .../Engine/UI/_Private/UnitTestDispatcherCompat.cs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs index 1790eb0..510b9f3 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs @@ -111,8 +111,14 @@ internal set /// public static async Task WaitForIdle() { +#if WINDOWS_WINUI || HAS_UNO_WINUI + // This is a wrong implementation. It doesn't really wait for "Idle". await RootElementDispatcher.RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { }); await RootElementDispatcher.RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { }); +#else + 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 */ }); +#endif } /// diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs index 76434a0..5411558 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs @@ -53,6 +53,11 @@ public UnitTestDispatcherCompat(_Impl impl) this._impl = impl; } +#if !WINDOWS_WINUI && !HAS_UNO_WINUI + public Windows.Foundation.IAsyncAction RunIdleAsync(Windows.UI.Core.IdleDispatchedHandler agileCallback) + => _impl.RunIdleAsync(agileCallback); +#endif + public static UnitTestDispatcherCompat From(UIElement x) => #if HAS_UNO_WINUI || WINDOWS_WINUI new UnitTestDispatcherCompat(x.DispatcherQueue); From a9bfb90eb86f495d8d5b0c4e97b6469503c4fdc2 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Wed, 20 Dec 2023 16:21:55 +0200 Subject: [PATCH 2/3] chore: Adjust for Uno WinUI --- .../Engine/UI/UnitTestsUIContentHelper.cs | 6 ---- .../UI/_Private/UnitTestDispatcherCompat.cs | 31 ++++++++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs index 510b9f3..8a43207 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsUIContentHelper.cs @@ -111,14 +111,8 @@ internal set /// public static async Task WaitForIdle() { -#if WINDOWS_WINUI || HAS_UNO_WINUI - // This is a wrong implementation. It doesn't really wait for "Idle". - await RootElementDispatcher.RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { }); - await RootElementDispatcher.RunAsync(UnitTestDispatcherCompat.Priority.Low, () => { }); -#else 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 */ }); -#endif } /// diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs index 5411558..2e35457 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs @@ -47,27 +47,50 @@ public enum Priority { Low = -1, Normal = 0, High = 1 } public partial class UnitTestDispatcherCompat { private readonly _Impl _impl; + private readonly Windows.UI.Core.CoreDispatcher? _dispatcher; +#if WINDOWS_WINUI || HAS_UNO_WINUI + 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 -#if !WINDOWS_WINUI && !HAS_UNO_WINUI public Windows.Foundation.IAsyncAction RunIdleAsync(Windows.UI.Core.IdleDispatchedHandler agileCallback) - => _impl.RunIdleAsync(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 From 21a36cc1e6c56f1e8a2fb257aff5082feeeb06db Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Wed, 20 Dec 2023 16:43:40 +0200 Subject: [PATCH 3/3] chore: Small fix --- .../Engine/UI/_Private/UnitTestDispatcherCompat.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs index 2e35457..091cb9f 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/_Private/UnitTestDispatcherCompat.cs @@ -47,10 +47,11 @@ public enum Priority { Low = -1, Normal = 0, High = 1 } public partial class UnitTestDispatcherCompat { private readonly _Impl _impl; - private readonly Windows.UI.Core.CoreDispatcher? _dispatcher; #if WINDOWS_WINUI || HAS_UNO_WINUI - public UnitTestDispatcherCompat(_Impl impl, Windows.UI.Core.CoreDispatcher? dispatcher = null) + + private readonly Windows.UI.Core.CoreDispatcher? _dispatcher; + public UnitTestDispatcherCompat(_Impl impl, Windows.UI.Core.CoreDispatcher? dispatcher = null) { this._impl = impl; this._dispatcher = dispatcher;