Skip to content

Commit

Permalink
- fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Jul 18, 2022
1 parent 29ca2bb commit 5847b9b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public async Task AppearingFiresBeforeNavigatedTo()
int contentPageAppearingFired = 0;
int navigatedToFired = 0;
int shellNavigatedToFired = 0;

// If you fail these from the events then
// an exception is raised in the middle of the platform
// doing platform things which often leads to a crash
bool navigatedPartPassed = false;
bool navigatedToPartPassed = false;

contentPage.Appearing += (_, _) =>
{
Expand All @@ -68,15 +74,15 @@ public async Task AppearingFiresBeforeNavigatedTo()
contentPage.NavigatedTo += (_, _) =>
{
navigatedToFired++;
Assert.Equal(1, contentPageAppearingFired);
navigatedToPartPassed = (contentPageAppearingFired == 1);
};

Shell shell = await CreateShellAsync(shell =>
{
shell.Navigated += (_, _) =>
{
Assert.Equal(1, contentPageAppearingFired);
shellNavigatedToFired++;
navigatedPartPassed = (contentPageAppearingFired == 1);

};

Expand All @@ -95,10 +101,13 @@ public async Task AppearingFiresBeforeNavigatedTo()
await CreateHandlerAndAddToWindow<IWindowHandler>(shell, async (handler) =>
{
await OnFrameSetToNotEmpty(contentPage);
Assert.Equal(1, contentPageAppearingFired);
Assert.Equal(1, shellNavigatedToFired);
Assert.Equal(1, navigatedToFired);
});

Assert.True(navigatedPartPassed, "Appearing fired too many times before Navigated fired");
Assert.True(navigatedToPartPassed, "Appearing fired too many times before NavigatedTo fired");
Assert.Equal(1, contentPageAppearingFired);
Assert.Equal(1, shellNavigatedToFired);
Assert.Equal(1, navigatedToFired);
}

[Fact(DisplayName = "Navigating During Navigated Doesnt ReFire Appearing")]
Expand Down
14 changes: 13 additions & 1 deletion src/Controls/tests/DeviceTests/HandlerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void EnsureHandlerCreated(Action<MauiAppBuilder> additionalCreationAction

appBuilder.Services.AddSingleton<IDispatcherProvider>(svc => TestDispatcher.Provider);
appBuilder.Services.AddScoped<IDispatcher>(svc => TestDispatcher.Current);
appBuilder.Services.TryAddSingleton<IApplication>((_) => new Application());
appBuilder.Services.TryAddSingleton<IApplication>((_) => new ApplicationStub());

additionalCreationActions?.Invoke(appBuilder);

Expand Down Expand Up @@ -196,10 +196,14 @@ protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Action<THand
protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Func<THandler, Task> action, IMauiContext mauiContext = null)
where THandler : class, IElementHandler
{
mauiContext ??= MauiContext;

return InvokeOnMainThreadAsync(async () =>
{
IWindow window = null;

var application = mauiContext.Services.GetService<IApplication>();

if (view is IWindow w)
{
window = w;
Expand All @@ -213,6 +217,14 @@ protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Func<THandle
window = new Controls.Window(new ContentPage() { Content = (View)view });
}

if (application is ApplicationStub appStub)
{
appStub.SetWindow((Window)window);

// Trigger the work flow of creating a window
_ = application.CreateWindow(null);
}

try
{
await _takeOverMainContentSempahore.WaitAsync();
Expand Down
25 changes: 25 additions & 0 deletions src/Controls/tests/DeviceTests/Stubs/ApplicationStub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui.DeviceTests.Stubs
{
public class ApplicationStub : Application
{
Window _window;

public ApplicationStub()
{
}

public void SetWindow(Window window) => _window = window;

protected override Window CreateWindow(IActivationState activationState)
{
return _window;
}
}
}

0 comments on commit 5847b9b

Please sign in to comment.