diff --git a/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs b/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs index 31a6a12a29d7..0a5d471618d7 100644 --- a/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs @@ -36,7 +36,7 @@ public partial class ControlsHandlerTestBase : HandlerTestBase, IDisposable protected override MauiAppBuilder ConfigureBuilder(MauiAppBuilder mauiAppBuilder) { - mauiAppBuilder.Services.TryAddSingleton((_) => new ApplicationStub()); + mauiAppBuilder.Services.AddSingleton((_) => new ApplicationStub()); return mauiAppBuilder.ConfigureTestBuilder(); } @@ -67,7 +67,7 @@ protected THandler CreateHandler(IElement view) return CreateHandler(view, MauiContext); } - protected async Task CreateHandlerAsync(IElement view) + protected async Task CreateHandlerAsync(IElement view) where THandler : IElementHandler, new() => await InvokeOnMainThreadAsync(() => CreateHandler(view)); diff --git a/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs index de3492ffef0d..9d31c2e51c14 100644 --- a/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs @@ -448,10 +448,11 @@ public async Task EmptyShellHasNoTopMargin() var mainPage = new ContentPage(); var shell = new Shell() { CurrentItem = mainPage }; - await CreateHandlerAndAddToWindow(shell, (handler) => + await CreateHandlerAndAddToWindow(shell, async (handler) => { - var position = mainPage.ToPlatform().GetLocationOnScreen(); + Assert.True(await AssertionExtensions.Wait(() => mainPage.ToPlatform().GetLocationOnScreen().Value.Y > 0)); var appTitleBarHeight = GetWindowRootView(handler).AppTitleBarActualHeight; + var position = mainPage.ToPlatform().GetLocationOnScreen(); Assert.True(Math.Abs(position.Value.Y - appTitleBarHeight) < 1); }); @@ -555,7 +556,7 @@ public async Task SelectingTabUpdatesSelectedFlyoutItem() shell.FlyoutBehavior = FlyoutBehavior.Locked; }); - await CreateHandlerAndAddToWindow(shell, (handler) => + await CreateHandlerAndAddToWindow(shell, async (handler) => { var flyoutItems = shell.FlyoutItems.Cast>().ToList(); var rootView = handler.PlatformView as MauiNavigationView; @@ -572,6 +573,9 @@ await CreateHandlerAndAddToWindow(shell, (handler) => tabbedView.SelectedItem = platformTabItems[1].MenuItemsSource[1]; + // Wait for the selected item to propagate to the rootview + await AssertionExtensions.Wait(() => (rootView.SelectedItem as NavigationViewItemViewModel).Data == flyoutItems[0][1]); + // Verify that the flyout item updates Assert.Equal((rootView.SelectedItem as NavigationViewItemViewModel).Data, flyoutItems[0][1]); }); diff --git a/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs index 96b9f96401d2..d5bcc280f193 100644 --- a/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/Window/WindowTests.Windows.cs @@ -80,7 +80,10 @@ public async Task HeaderCorrectlyOffsetFromAppTitleBar() await CreateHandlerAndAddToWindow(mainPage, async (handler) => { var mauiToolBar = GetPlatformToolbar(handler); - await AssertionExtensions.Wait(() => mauiToolBar.GetLocationOnScreen().Value.Y > 0); + + Assert.NotNull(mauiToolBar); + Assert.True(await AssertionExtensions.Wait(() => mauiToolBar.GetLocationOnScreen().Value.Y > 0)); + var position = mauiToolBar.GetLocationOnScreen(); var appTitleBarHeight = GetWindowRootView(handler).AppTitleBarActualHeight; @@ -113,7 +116,10 @@ await CreateHandlerAndAddToWindow(window, async (handler) => if (nextRootPage is NavigationPage || nextRootPage is Shell) { var mauiToolBar = GetPlatformToolbar(handler); - await AssertionExtensions.Wait(() => mauiToolBar.GetLocationOnScreen().Value.Y > 0); + + Assert.NotNull(mauiToolBar); + Assert.True(await AssertionExtensions.Wait(() => mauiToolBar.GetLocationOnScreen().Value.Y > 0)); + var position = mauiToolBar.GetLocationOnScreen(); var appTitleBarHeight = GetWindowRootView(handler).AppTitleBarActualHeight; diff --git a/src/Core/tests/DeviceTests.Shared/HandlerTests/HandlerTestBase.cs b/src/Core/tests/DeviceTests.Shared/HandlerTests/HandlerTestBase.cs index adc8dab2005c..4cce557c4769 100644 --- a/src/Core/tests/DeviceTests.Shared/HandlerTests/HandlerTestBase.cs +++ b/src/Core/tests/DeviceTests.Shared/HandlerTests/HandlerTestBase.cs @@ -30,13 +30,14 @@ public void EnsureHandlerCreated(Action additionalCreationAction _isCreated = true; - var appBuilder = ConfigureBuilder(MauiApp.CreateBuilder()); + var appBuilder = MauiApp.CreateBuilder(); - additionalCreationActions?.Invoke(appBuilder); + appBuilder.Services.AddSingleton(svc => TestDispatcher.Provider); + appBuilder.Services.AddScoped(svc => TestDispatcher.Current); + appBuilder.Services.AddSingleton((_) => new CoreApplicationStub()); - appBuilder.Services.TryAddSingleton(svc => TestDispatcher.Provider); - appBuilder.Services.TryAddScoped(svc => TestDispatcher.Current); - appBuilder.Services.TryAddSingleton((_) => new ApplicationStub()); + appBuilder = ConfigureBuilder(appBuilder); + additionalCreationActions?.Invoke(appBuilder); _mauiApp = appBuilder.Build(); _servicesProvider = _mauiApp.Services; diff --git a/src/Core/tests/DeviceTests.Shared/Stubs/ApplicationStub.cs b/src/Core/tests/DeviceTests.Shared/Stubs/CoreApplicationStub.cs similarity index 93% rename from src/Core/tests/DeviceTests.Shared/Stubs/ApplicationStub.cs rename to src/Core/tests/DeviceTests.Shared/Stubs/CoreApplicationStub.cs index 14bf22301315..955cf3ba55b9 100644 --- a/src/Core/tests/DeviceTests.Shared/Stubs/ApplicationStub.cs +++ b/src/Core/tests/DeviceTests.Shared/Stubs/CoreApplicationStub.cs @@ -2,7 +2,7 @@ namespace Microsoft.Maui.DeviceTests.Stubs { - class ApplicationStub : IApplication + class CoreApplicationStub : IApplication { readonly List _windows = new List();