Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ static void UpdateMenuItem(AToolbar toolbar,

if (item.Order != ToolbarItemOrder.Secondary)
menuitem.SetShowAsAction(ShowAsAction.Always);
else
menuitem.SetShowAsAction(ShowAsAction.Never);

menuitem.SetOnMenuItemClickListener(new GenericMenuClickListener(((IMenuItemController)item).Activate));

Expand Down
60 changes: 54 additions & 6 deletions src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using Android.OS;
using AndroidX.AppCompat.App;
using AndroidX.AppCompat.Graphics.Drawable;
using AndroidX.AppCompat.View.Menu;
using AndroidX.AppCompat.Widget;
using AndroidX.CoordinatorLayout.Widget;
using AndroidX.Fragment.App;
using Google.Android.Material.AppBar;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Xunit;
Expand Down Expand Up @@ -90,6 +92,22 @@ public bool ToolbarItemsMatch(
ToolbarItem toolbarItem = toolbarItems[i];
var primaryCommand = menu.GetItem(i);
Assert.Equal(toolbarItem.Text, $"{primaryCommand.TitleFormatted}");

if (primaryCommand is MenuItemImpl menuItemImpl)
{
if (toolbarItem.Order != ToolbarItemOrder.Secondary)
{
Assert.True(menuItemImpl.RequiresActionButton(), "Secondary Menu Item `SetShowAsAction` not set correctly");
}
else
{
Assert.False(menuItemImpl.RequiresActionButton(), "Primary Menu Item `SetShowAsAction` not set correctly");
}
}
else
{
throw new Exception($"MenuItem type is not MenuItemImpl. Please rework test to work with {primaryCommand}");
}
}

return true;
Expand Down Expand Up @@ -117,29 +135,52 @@ protected MaterialToolbar GetPlatformToolbar(IElementHandler handler)
{
var shell = handler.VirtualView as Shell;
var currentPage = shell.CurrentPage;
var pagePlatformView = currentPage.Handler.PlatformView as AView;
var parentContainer = pagePlatformView.GetParentOfType<CoordinatorLayout>();
var toolbar = parentContainer.GetFirstChildOfType<MaterialToolbar>();
return toolbar;

if (currentPage?.Handler?.PlatformView is AView pagePlatformView)
{
var parentContainer = pagePlatformView.GetParentOfType<CoordinatorLayout>();
var toolbar = parentContainer?.GetFirstChildOfType<MaterialToolbar>();
return toolbar;
}

return null;
}
else
{
return GetPlatformToolbar(handler.MauiContext);
}
}

protected string GetToolbarTitle(IElementHandler handler) =>
GetPlatformToolbar(handler).Title;

protected MaterialToolbar GetPlatformToolbar(IMauiContext mauiContext)
{
var navManager = mauiContext.GetNavigationRootManager();
if (navManager?.RootView is null)
return null;

var appbarLayout =
navManager?.RootView?.FindViewById<AViewGroup>(Resource.Id.navigationlayout_appbar);
navManager.RootView.FindViewById<AViewGroup>(Resource.Id.navigationlayout_appbar);

if (appbarLayout is null &&
navManager.RootView is ContainerView cv &&
cv.CurrentView is Shell shell)
{
if (shell.Handler is Controls.Platform.Compatibility.IShellContext sr)
{
var layout = sr.CurrentDrawerLayout;
var content = layout?.GetFirstChildOfType<Controls.Platform.Compatibility.CustomFrameLayout>();
appbarLayout = content?.GetFirstChildOfType<AppBarLayout>();
}
}

var toolBar = appbarLayout?.GetFirstChildOfType<MaterialToolbar>();

toolBar = toolBar ?? navManager.ToolbarElement?.Toolbar?.Handler?.PlatformView as
MaterialToolbar;

if (toolBar == null)
if (toolBar is null)
{
appbarLayout =
(navManager?.RootView as AViewGroup)?.GetFirstChildOfType<AppBarLayout>();
Expand All @@ -150,6 +191,13 @@ protected MaterialToolbar GetPlatformToolbar(IMauiContext mauiContext)
return toolBar;
}

protected Size GetTitleViewExpectedSize(IElementHandler handler)
{
var context = handler.MauiContext.Context;
var toolbar = GetPlatformToolbar(handler.MauiContext).GetFirstChildOfType<Microsoft.Maui.Controls.Toolbar.Container>();
return new Size(context.FromPixels(toolbar.MeasuredWidth), context.FromPixels(toolbar.MeasuredHeight));
}

public bool IsNavigationBarVisible(IElementHandler handler) =>
IsNavigationBarVisible(handler.MauiContext);

Expand Down
33 changes: 27 additions & 6 deletions src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
Expand All @@ -9,6 +11,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Controls;
using Windows.Foundation.Collections;
using Xunit;
using NativeAutomationProperties = Microsoft.UI.Xaml.Automation.AutomationProperties;
using WAppBarButton = Microsoft.UI.Xaml.Controls.AppBarButton;
Expand Down Expand Up @@ -128,16 +131,31 @@ public bool ToolbarItemsMatch(
IElementHandler handler,
params ToolbarItem[] toolbarItems)
{
var primaryToolbarItems = toolbarItems.Where(x => x.Order != ToolbarItemOrder.Secondary).ToArray();
var secondaryToolbarItems = toolbarItems.Where(x => x.Order == ToolbarItemOrder.Secondary).ToArray();

var navView = (RootNavigationView)GetMauiNavigationView(handler.MauiContext);
MauiToolbar windowHeader = (MauiToolbar)navView.Header;
Assert.NotNull(windowHeader?.CommandBar?.PrimaryCommands);

Assert.Equal(toolbarItems.Length, windowHeader.CommandBar.PrimaryCommands.Count);
for (var i = 0; i < toolbarItems.Length; i++)
ValidateCommandBarCommands(windowHeader?.CommandBar?.PrimaryCommands, primaryToolbarItems);
ValidateCommandBarCommands(windowHeader?.CommandBar?.SecondaryCommands, secondaryToolbarItems);

void ValidateCommandBarCommands(IObservableVector<ICommandBarElement> commands, ToolbarItem[] orderToolbarItems)
{
ToolbarItem toolbarItem = toolbarItems[i];
var primaryCommand = ((WAppBarButton)windowHeader.CommandBar.PrimaryCommands[i]);
Assert.Equal(toolbarItem, primaryCommand.DataContext);
if (orderToolbarItems.Length == 0)
{
Assert.True(commands is null || commands.Count == 0);
return;
}

Assert.NotNull(commands);
Assert.Equal(orderToolbarItems.Length, commands.Count);
for (var i = 0; i < toolbarItems.Length; i++)
{
ToolbarItem toolbarItem = orderToolbarItems[i];
var command = ((WAppBarButton)commands[i]);
Assert.Equal(toolbarItem, command.DataContext);
}
}

return true;
Expand All @@ -148,5 +166,8 @@ protected object GetTitleView(IElementHandler handler)
var toolbar = GetPlatformToolbar(handler);
return toolbar.TitleView;
}

protected string GetToolbarTitle(IElementHandler handler) =>
GetPlatformToolbar(handler).Title;
}
}
54 changes: 50 additions & 4 deletions src/Controls/tests/DeviceTests/ControlsHandlerTestBase.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using UIKit;
using Xunit;
Expand Down Expand Up @@ -109,14 +111,16 @@ protected object GetTitleView(IElementHandler handler)

UIViewController[] GetActiveChildViewControllers(IElementHandler handler)
{
if (handler is IWindowHandler wh)
{
handler = wh.VirtualView.Content.Handler;
}

if (handler is ShellRenderer renderer)
{
if (renderer.ChildViewControllers[0] is ShellItemRenderer sir)
{
if (sir.ChildViewControllers[0] is ShellSectionRenderer ssr)
{
return ssr.ChildViewControllers;
}
return sir.SelectedViewController.ChildViewControllers;
}
}

Expand All @@ -140,5 +144,47 @@ UIViewController GetVisibleViewController(IElementHandler handler)
var vcs = GetActiveChildViewControllers(handler);
return vcs[vcs.Length - 1];
}

protected UINavigationBar GetPlatformToolbar(IElementHandler handler)
{
var visibleController = GetVisibleViewController(handler);
if (visibleController is UINavigationController nc)
return nc.NavigationBar;

var navController = visibleController.NavigationController;
return navController?.NavigationBar;
}

protected Size GetTitleViewExpectedSize(IElementHandler handler)
{
var titleContainer = GetPlatformToolbar(handler).FindDescendantView<UIView>(result =>
{
return result.Class.Name?.Contains("UINavigationBarTitleControl", StringComparison.OrdinalIgnoreCase) == true;
});

if (!OperatingSystem.IsIOSVersionAtLeast(16))
{
titleContainer = titleContainer ?? GetPlatformToolbar(handler).FindDescendantView<UIView>(result =>
{
return result.Class.Name?.Contains("TitleViewContainer", StringComparison.OrdinalIgnoreCase) == true;
});
}

_ = titleContainer ?? throw new Exception("Unable to Locate TitleView Container");

return new Size(titleContainer.Frame.Width, titleContainer.Frame.Height);
}

protected string GetToolbarTitle(IElementHandler handler)
{
var toolbar = GetPlatformToolbar(handler);
return AssertionExtensions.GetToolbarTitle(toolbar);
}

protected string GetBackButtonText(IElementHandler handler)
{
var toolbar = GetPlatformToolbar(handler);
return AssertionExtensions.GetBackButtonText(toolbar);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,5 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async
Assert.False(failed);
});
}

string GetToolbarTitle(IElementHandler handler) =>
GetPlatformToolbar(handler).Title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ namespace Microsoft.Maui.DeviceTests
[Category(TestCategory.NavigationPage)]
public partial class NavigationPageTests : ControlsHandlerTestBase
{

string GetToolbarTitle(IElementHandler handler) =>
GetPlatformToolbar(handler).Title;


[Fact(DisplayName = "Back Button Enabled Changes with push/pop")]
public async Task BackButtonEnabledChangesWithPushPop()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,6 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async (handler) =>
});
}

[Fact(DisplayName = "Toolbar Items Map Correctly")]
public async Task ToolbarItemsMapCorrectly()
{
SetupBuilder();
var toolbarItem = new ToolbarItem() { Text = "Toolbar Item 1" };
var navPage = new NavigationPage(new ContentPage()
{
ToolbarItems =
{
toolbarItem
}
});

await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
{
ToolbarItemsMatch(handler, toolbarItem);
return Task.CompletedTask;
});
}

[Fact(DisplayName = "Toolbar Title")]
public async Task ToolbarTitle()
{
SetupBuilder();
var navPage = new NavigationPage(new ContentPage()
{
Title = "Page Title"
});

await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handler) =>
{
string title = GetToolbarTitle(handler);
Assert.Equal("Page Title", title);
return Task.CompletedTask;
});
}

[Fact(DisplayName = "Insert Page Before Root Page and then PopToRoot")]
public async Task InsertPageBeforeRootPageAndThenPopToRoot()
{
Expand Down
Loading