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 RunWindowTest <THandler>(IWindow window, Func <THandler, Task> action) where THandler : class, IElementHandler { 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; if (window.Content is Shell shell) { await OnLoadedAsync(shell.CurrentPage); } 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); } else if (window.Content is ContentPage cp && typeof(THandler).IsAssignableFrom(cp.Content.Handler.GetType())) { await action((THandler)cp.Content.Handler); } else { throw new Exception($"I can't work with {typeof(THandler)}"); } } 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); } // Ideally this wouldn't be needed but I haven't found the right platform // component I can key into for knowing when the world can move on await Task.Delay(1000); fragmentManager.ExecutePendingTransactions(); } }));
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 decorBackground = 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; 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); } else if (window.Content is ContentPage cp && typeof(THandler).IsAssignableFrom(cp.Content.Handler.GetType())) { await action((THandler)cp.Content.Handler); } else { throw new Exception($"I can't work with {typeof(THandler)}"); } } 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(); } // This is mainly to remove changes to the decor view that shell imposes if (decorBackground != rootView.Background) { rootView.Background = decorBackground; } // 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); } } }));
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); } } })); }