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

Update BlazorWebView Device Tests to have longer timesouts and more retries #21318

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/BlazorWebView/tests/MauiDeviceTests/ApplicationStub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Maui.Controls;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file and the next few files (AppStub, ControlHandlerTestBase, etc.) are copied from the MAUI Controls DeviceTests, but I then removed anything that wasn't needed by the BlazorWebView tests (it was too hard to get it to compile, and too hard to directly reuse the types).


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

public ApplicationStub() : base()
{
}

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

protected override Window CreateWindow(IActivationState activationState)
{
return _window ?? base.CreateWindow(activationState);
}
}
}
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
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Threading.Tasks;
using Android.Content;
using Android.OS;
using Android.Views;
using AndroidX.AppCompat.App;
using AndroidX.AppCompat.Widget;
using AndroidX.Fragment.App;
using Microsoft.Maui.Platform;
using ALayoutInflater = Android.Views.LayoutInflater;
using AView = Android.Views.View;
using AViewGroup = Android.Views.ViewGroup;

namespace Microsoft.Maui.DeviceTests
{
public partial class ControlsHandlerTestBase
{
Task SetupWindowForTests<THandler>(IWindow window, Func<Task> runTests, IMauiContext mauiContext = null)
where THandler : class, IElementHandler
{
mauiContext ??= MauiContext;
return InvokeOnMainThreadAsync(async () =>
{
AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup;
var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context);
var viewFragment = new WindowTestFragment(MauiContext, window);

try
{
linearLayoutCompat.Id = AView.GenerateViewId();
rootView.AddView(linearLayoutCompat);

await viewFragment.FinishedLoading;
await runTests.Invoke();
}
finally
{
window.Handler?.DisconnectHandler();

rootView.RemoveView(linearLayoutCompat);

await viewFragment.FinishedDestroying;

// Unset the Support Action bar if the calling code has set the support action bar
if (MauiContext.Context.GetActivity() is AppCompatActivity aca)
{
aca.SetSupportActionBar(null);
}
}
});
}

class WindowTestFragment : Fragment
{
TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
readonly IMauiContext _mauiContext;
readonly IWindow _window;

public IMauiContext ScopedMauiContext { get; set; }

public Task FinishedLoading => _taskCompletionSource.Task;

public Task FinishedDestroying => _taskCompletionSource.Task;

public FakeActivityRootView FakeActivityRootView { get; set; }

public WindowTestFragment(IMauiContext mauiContext, IWindow window)
{
_mauiContext = mauiContext;
_window = window;
}

public static IMauiContext MakeScoped(IMauiContext mauiContext,
LayoutInflater layoutInflater = null,
FragmentManager fragmentManager = null,
Android.Content.Context context = null,
bool registerNewNavigationRoot = false)
{
var scopedContext = new MauiContext(mauiContext.Services);

if (registerNewNavigationRoot)
{
if (fragmentManager == null)
throw new InvalidOperationException("If you're creating a new Navigation Root you need to use a new Fragment Manager");
}

return scopedContext;
}

public override AView OnCreateView(ALayoutInflater inflater, AViewGroup container, Bundle savedInstanceState)
{
ScopedMauiContext = MakeScoped(_mauiContext, layoutInflater: inflater, fragmentManager: ChildFragmentManager, registerNewNavigationRoot: true);
var handler = _window.ToHandler(ScopedMauiContext);

FakeActivityRootView = new FakeActivityRootView(ScopedMauiContext.Context);
FakeActivityRootView.LayoutParameters = new LinearLayoutCompat.LayoutParams(AViewGroup.LayoutParams.MatchParent, AViewGroup.LayoutParams.MatchParent);

return FakeActivityRootView;
}
}

public class FakeActivityRootView : FitWindowsFrameLayout
{
public FakeActivityRootView(Context context) : base(context)
{
Id = AView.GenerateViewId();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Platform;
using WWindow = Microsoft.UI.Xaml.Window;

namespace Microsoft.Maui.DeviceTests
{
public partial class ControlsHandlerTestBase
{
Task SetupWindowForTests<THandler>(IWindow window, Func<Task> runTests, IMauiContext mauiContext = null)
where THandler : class, IElementHandler
{
mauiContext ??= MauiContext;
return InvokeOnMainThreadAsync(async () =>
{
var appStub = new MauiAppNewWindowStub(window);

WWindow newWindow = null;
try
{
ApplicationExtensions.CreatePlatformWindow(UI.Xaml.Application.Current, appStub, new Handlers.OpenWindowRequest());
newWindow = window.Handler.PlatformView as WWindow;
await runTests.Invoke();
}
finally
{
window.Handler?.DisconnectHandler();
await Task.Delay(10);
newWindow?.Close();
appStub.Handler?.DisconnectHandler();
}
});
}
}
}