Skip to content

Commit

Permalink
Update BlazorWebView Device Tests to have longer timesouts and more r…
Browse files Browse the repository at this point in the history
…etries (#21318)

* WIP: Update BlazorWebView Device Tests to use newer handle test base classes

* Remove unused IDisposable interface

* Increase timeouts, and add more retries

* Remove unused test base class code

* Remove a lot of unused stuff
  • Loading branch information
Eilon committed Mar 22, 2024
1 parent 0cff6bd commit 8482028
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 220 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;

namespace Microsoft.Maui.DeviceTests
{
public static class ControlsDeviceTestExtensions
{
public static MauiAppBuilder ConfigureTestBuilder(this MauiAppBuilder mauiAppBuilder)
{
return
mauiAppBuilder
.ConfigureLifecycleEvents(lifecycle =>
{
#if IOS || MACCATALYST
lifecycle
.AddiOS(iOS => iOS
.OpenUrl((app, url, options) =>
ApplicationModel.Platform.OpenUrl(app, url, options))
.ContinueUserActivity((application, userActivity, completionHandler) =>
ApplicationModel.Platform.ContinueUserActivity(application, userActivity, completionHandler))
.PerformActionForShortcutItem((application, shortcutItem, completionHandler) =>
ApplicationModel.Platform.PerformActionForShortcutItem(application, shortcutItem, completionHandler)));
#elif WINDOWS
lifecycle
.AddWindows(windows =>
{
windows
.OnLaunched((app, e) =>
ApplicationModel.Platform.OnLaunched(e));
windows
.OnActivated((window, e) =>
ApplicationModel.Platform.OnActivated(window, e));
});
#endif
})
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Editor), typeof(EditorHandler));
handlers.AddHandler(typeof(VerticalStackLayout), typeof(LayoutHandler));
handlers.AddHandler(typeof(Controls.ContentPage), typeof(PageHandler));
#if WINDOWS
handlers.AddHandler(typeof(MauiAppNewWindowStub), typeof(ApplicationHandler));
#endif
});
}
}
}
21 changes: 21 additions & 0 deletions src/BlazorWebView/tests/MauiDeviceTests/ControlsHandlerTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Hosting;

namespace Microsoft.Maui.DeviceTests
{
public partial class ControlsHandlerTestBase : HandlerTestBase
{
protected override MauiAppBuilder ConfigureBuilder(MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddSingleton<IApplication>((_) => new Application());
return mauiAppBuilder.ConfigureTestBuilder();
}

protected THandler CreateHandler<THandler>(IElement view)
where THandler : IElementHandler, new()
{
return CreateHandler<THandler>(view, MauiContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests.Elements
{
[Category(TestCategory.BlazorWebView)]
public class BlazorWebViewTests : HandlerTestBase
public class BlazorWebViewTests : Microsoft.Maui.DeviceTests.ControlsHandlerTestBase
{
[Fact]
public async Task BasicRazorComponentClick()
Expand Down
20 changes: 0 additions & 20 deletions src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.Android.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.Windows.cs

This file was deleted.

90 changes: 0 additions & 90 deletions src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.iOS.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui.DeviceTests.Stubs
{
class MauiAppNewWindowStub : IApplication
{
readonly IWindow _window;
Window Window => _window as Window;

MauiWinUIWindow _plaformWindow;
IElementHandler _handler;

public MauiAppNewWindowStub(IWindow window)
{
_window = window;
Window.HandlerChanged += OnWindowHandlerChanged;
}

void OnWindowHandlerChanged(object sender, EventArgs e)
{
if (_window.Handler?.PlatformView is not MauiWinUIWindow platformWindow)
return;

if (_plaformWindow is null)
{
_plaformWindow = platformWindow;
InvokeWindowCreated();
_plaformWindow.Activated += OnActivated;
_plaformWindow.Closed += OnClosed;
}
}

public IReadOnlyList<IWindow> Windows => new List<IWindow>() { _window };

public IElementHandler Handler
{
get => _handler;
set
{
_handler = value;

if (value is not null)
{
InvokeWindowCreated();
}
}
}

void InvokeWindowCreated()
{
if (Window is null && _window is not null)
{
_window.Created();
}
}

void InvokeWindowDestroying()
{
if (Window is null)
{
_window.Destroying();
}
}

void OnClosed(object sender, UI.Xaml.WindowEventArgs args)
{
if (_plaformWindow is not null)
{
_plaformWindow.Activated -= OnActivated;
_plaformWindow.Closed -= OnClosed;
_plaformWindow = null;
InvokeWindowDestroying();
}
}

void OnActivated(object sender, UI.Xaml.WindowActivatedEventArgs args)
{
if (args.WindowActivationState != UI.Xaml.WindowActivationState.Deactivated)
{
if (Window is null)
{
_window.Activated();
}
}
}

public IElement Parent => null;

public AppTheme UserAppTheme { get; set; }

public void CloseWindow(IWindow window)
{
Handler?.Invoke(nameof(IApplication.CloseWindow), window);
}

public IWindow CreateWindow(IActivationState activationState)
{
return _window;
}

public void OpenWindow(IWindow window)
{
throw new NotImplementedException();
}

public void ThemeChanged()
{
throw new NotImplementedException();
}
}
}
8 changes: 6 additions & 2 deletions src/BlazorWebView/tests/MauiDeviceTests/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
Expand All @@ -12,12 +13,15 @@ public static class MauiProgram
#if ANDROID
public static Android.Content.Context CurrentContext => MauiProgramDefaults.DefaultContext;
#elif WINDOWS
public static UI.Xaml.Window DefaultWindow => MauiProgramDefaults.DefaultWindow;
public static Microsoft.UI.Xaml.Window CurrentWindow => MauiProgramDefaults.DefaultWindow;
#endif

public static IApplication DefaultTestApp => MauiProgramDefaults.DefaultTestApp;

public static MauiApp CreateMauiApp() =>
MauiProgramDefaults.CreateMauiApp(new List<Assembly>()
{
typeof(MauiProgram).Assembly
});
}
}
}

0 comments on commit 8482028

Please sign in to comment.