Task RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action) where THandler : class, IElementHandler { return(InvokeOnMainThreadAsync(async() => { AViewGroup rootView = MauiContext.Context.GetActivity().Window.DecorView as AViewGroup; var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context); var fragmentManager = MauiContext.GetFragmentManager(); var viewFragment = new WindowTestFragment(MauiContext, window); try { linearLayoutCompat.Id = AView.GenerateViewId(); fragmentManager .BeginTransaction() .Add(linearLayoutCompat.Id, viewFragment) .Commit(); rootView.AddView(linearLayoutCompat); await viewFragment.FinishedLoading; if (typeof(THandler).IsAssignableFrom(window.Handler.GetType())) { await action((THandler)window.Handler); } else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType())) { await action((THandler)window.Content.Handler); } } finally { if (window.Handler != null) { window.Handler.DisconnectHandler(); } rootView.RemoveView(linearLayoutCompat); fragmentManager .BeginTransaction() .Remove(viewFragment) .Commit(); await linearLayoutCompat.OnUnloadedAsync(); if (viewFragment.View != null) { await viewFragment.View.OnUnloadedAsync(); } } })); }
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; _decorDrawable ??= rootView.Background; var linearLayoutCompat = new LinearLayoutCompat(MauiContext.Context); var fragmentManager = MauiContext.GetFragmentManager(); var viewFragment = new WindowTestFragment(MauiContext, window); try { linearLayoutCompat.Id = AView.GenerateViewId(); rootView.AddView(linearLayoutCompat); fragmentManager .BeginTransaction() .Add(linearLayoutCompat.Id, viewFragment) .Commit(); await viewFragment.FinishedLoading; await runTests.Invoke(); } finally { if (window.Handler != null) { window.Handler.DisconnectHandler(); } fragmentManager .BeginTransaction() .Remove(viewFragment) .Commit(); rootView.RemoveView(linearLayoutCompat); await linearLayoutCompat.OnUnloadedAsync(); if (viewFragment.View != null) { await viewFragment.View.OnUnloadedAsync(); } await viewFragment.FinishedDestroying; // This is mainly to remove changes to the decor view that shell imposes if (_decorDrawable != rootView.Background) { rootView.Background = _decorDrawable; } // 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); } } })); }