Skip to content

Commit

Permalink
Fix iOS Shell Tests (still no CI <3) (#7110)
Browse files Browse the repository at this point in the history
* Switch to Modal for window tests

* - add iOS tests to collection

* - fix exception

* - fix VS dismiss

* - enable iOS Tests

* - comment out iOS tests for shell
  • Loading branch information
PureWeen committed May 13, 2022
1 parent 25b4b6f commit 36eed28
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 27 deletions.
Expand Up @@ -141,7 +141,14 @@ protected virtual void UpdateTabBarVisible()

void OnToolbarPropertyChanged(object sender, PropertyChangedEventArgs e)
{
UpdateTitle();
if (e.PropertyName == Shell.TitleViewProperty.PropertyName)
{
UpdateTitleView();
}
else if (e.PropertyName == Page.TitleProperty.PropertyName)
{
UpdateTitle();
}
}

protected virtual void UpdateTitle()
Expand Down Expand Up @@ -207,7 +214,13 @@ protected virtual void OnRendererSet()

protected virtual void UpdateTitleView()
{
var titleView = _context.Shell.GetEffectiveValue<View>(Shell.TitleViewProperty, () => Shell.GetTitleView(_context.Shell), null, Page);
var titleView = _context.Shell.Toolbar.TitleView as View;

if (NavigationItem.TitleView is TitleViewContainer tvc &&
tvc.View == titleView)
{
return;
}

if (titleView == null)
{
Expand Down
@@ -1,9 +1,11 @@
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Collection(HandlerTestBase.RunInNewWindowCollection)]
public partial class EditorTests
{
MauiTextView GetPlatformControl(EditorHandler handler) =>
Expand Down
@@ -0,0 +1,9 @@
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Collection(HandlerTestBase.RunInNewWindowCollection)]
public partial class FlyoutPageTests
{
}
}
Expand Up @@ -8,6 +8,7 @@

namespace Microsoft.Maui.DeviceTests
{
[Collection(HandlerTestBase.RunInNewWindowCollection)]
[Category(TestCategory.ListView)]
public partial class ListViewTests : HandlerTestBase
{
Expand Down
Expand Up @@ -17,7 +17,7 @@
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Modal)]
#if ANDROID
#if ANDROID || IOS
[Collection(HandlerTestBase.RunInNewWindowCollection)]
#endif
public partial class ModalTests : HandlerTestBase
Expand Down
@@ -0,0 +1,9 @@
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Collection(HandlerTestBase.RunInNewWindowCollection)]
public partial class NavigationPageTests : HandlerTestBase
{
}
}
Expand Up @@ -21,7 +21,7 @@
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Behavior)]
#if ANDROID
#if ANDROID || IOS
[Collection(HandlerTestBase.RunInNewWindowCollection)]
#endif
public partial class PlatformBehaviorTests : HandlerTestBase
Expand Down
10 changes: 4 additions & 6 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs
Expand Up @@ -71,6 +71,7 @@ public async Task FlyoutContentRenderersWhenFlyoutBehaviorStartsAsLocked()
});
}

#if !IOS
[Fact(DisplayName = "Flyout Starts as Open correctly")]
public async Task FlyoutIsPresented()
{
Expand All @@ -88,7 +89,7 @@ public async Task FlyoutIsPresented()
await CheckFlyoutState(handler, false);
});
}

#endif

[Fact(DisplayName = "Back Button Visibility Changes with push/pop")]
public async Task BackButtonVisibilityChangesWithPushPop()
Expand Down Expand Up @@ -147,12 +148,9 @@ public async Task SetHasBackButton()
{
SetupBuilder();

var shell = await InvokeOnMainThreadAsync<Shell>(() =>
var shell = await CreateShellAsync(shell =>
{
return new Shell()
{
Items = { new ContentPage() }
};
shell.CurrentItem = new ContentPage();
});

await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
Expand Down
Expand Up @@ -10,7 +10,7 @@
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.VisualElement)]
#if ANDROID
#if ANDROID || IOS
[Collection(HandlerTestBase.RunInNewWindowCollection)]
#endif
public partial class VisualElementTests : HandlerTestBase
Expand Down
45 changes: 34 additions & 11 deletions src/Controls/tests/DeviceTests/HandlerTestBase.iOS.cs
Expand Up @@ -59,7 +59,14 @@ Task RunWindowTest<THandler>(IWindow window, Func<THandler, Task> action)
await controlsWindow.Navigation.PopModalAsync();
}
window.Handler.DisconnectHandler();
if (window.Handler is WindowHandlerStub whs)
{
window.Handler.DisconnectHandler();
await whs.FinishedDisconnecting;
}
else
window.Handler.DisconnectHandler();
}
}
});
Expand All @@ -72,31 +79,47 @@ internal ModalWrapper GetModalWrapper(Page modalPage)
}

protected bool IsBackButtonVisible(IElementHandler handler)
{
var vcs = GetActiveChildViewControllers(handler);

if (vcs.Length <= 1)
return false;

return !vcs[vcs.Length - 1].NavigationItem.HidesBackButton;
}

protected object GetTitleView(IElementHandler handler)
{
var activeVC = GetVisibleViewController(handler);
if ( activeVC.NavigationItem.TitleView is
ShellPageRendererTracker.TitleViewContainer tvc)
{
return tvc.View.Handler.PlatformView;
}

return null;
}

UIViewController[] GetActiveChildViewControllers(IElementHandler handler)
{
if (handler is ShellRenderer renderer)
{
if (renderer.ChildViewControllers[0] is ShellItemRenderer sir)
{
if (sir.ChildViewControllers[0] is ShellSectionRenderer ssr)
{
// Nothing has been pushed to the stack
if (ssr.ChildViewControllers.Length == 1)
return false;

var activeVC =
ssr.ChildViewControllers[ssr.ChildViewControllers.Length - 1];

return !activeVC.NavigationItem.HidesBackButton;
return ssr.ChildViewControllers;
}
}
}

throw new NotImplementedException();
}

protected object GetTitleView(IElementHandler handler)
UIViewController GetVisibleViewController(IElementHandler handler)
{
throw new NotImplementedException();
var vcs = GetActiveChildViewControllers(handler);
return vcs[vcs.Length - 1];
}
}
}
45 changes: 40 additions & 5 deletions src/Controls/tests/DeviceTests/Stubs/WindowHandlerStub.iOS.cs
@@ -1,5 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using UIKit;
Expand All @@ -8,6 +10,9 @@ namespace Microsoft.Maui.DeviceTests
{
public class WindowHandlerStub : ElementHandler<IWindow, UIWindow>, IWindowHandler
{
TaskCompletionSource<bool> _finishedDisconnecting = new TaskCompletionSource<bool>();
public Task FinishedDisconnecting => _finishedDisconnecting.Task;

public static IPropertyMapper<IWindow, WindowHandlerStub> WindowMapper = new PropertyMapper<IWindow, WindowHandlerStub>(WindowHandler.Mapper)
{
[nameof(IWindow.Content)] = MapContent
Expand All @@ -16,15 +21,45 @@ public class WindowHandlerStub : ElementHandler<IWindow, UIWindow>, IWindowHandl
private static void MapContent(WindowHandlerStub handler, IWindow window)
{
var view = window.Content.ToPlatform(handler.MauiContext);
handler.PlatformView.RootViewController.View.AddSubview(view);

if (window.Content is Shell)
{
var vc =
(window.Content.Handler as IPlatformViewHandler)
.ViewController;

handler.PlatformView.RootViewController.PresentViewController(vc, false, null);
}
else
{
handler.PlatformView.RootViewController.View.AddSubview(view);
}
}

protected override void DisconnectHandler(UIWindow platformView)
{
VirtualView
.Content
.ToPlatform()
.RemoveFromSuperview();
var vc = (VirtualView.Content.Handler as IPlatformViewHandler)
.ViewController;

if (VirtualView.Content is Shell)
{
platformView.RootViewController
.PresentedViewController.
DismissViewController(false,
() =>
{
_finishedDisconnecting.SetResult(true);
});
}
else
{
VirtualView
.Content
.ToPlatform()
.RemoveFromSuperview();

_finishedDisconnecting.SetResult(true);
}

base.DisconnectHandler(platformView);
}
Expand Down

0 comments on commit 36eed28

Please sign in to comment.