Skip to content

Commit

Permalink
Fix text toolbar item icon not removed when navigating on Android (#1…
Browse files Browse the repository at this point in the history
…6796)

* Fix text toolbar item icon not removed when navigating on Android

Fixes an issue on Android when there are multiple pages with toolbar
items and on one page the toolbar item has an icon and on another page
it doesn't, the icon is not removed when navigating to the page without
the icon.

When updating the Android Menuitem icon and the base drawable is null,
the icon is set to null. On Android, this clears out the previous icon.

Fixes #7823

* Added UITest

* Improve test

* - update screenshots

* - add windows

---------

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
  • Loading branch information
4 people committed May 7, 2024
1 parent 396f7f7 commit a93e88c
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Controls/samples/Controls.Sample.UITests/Issues/Issue7823.xaml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue7823">
<ContentPage.ToolbarItems>
<ToolbarItem
IconImageSource="dotnet_bot.png"
Clicked="OnToolbarItemClicked"/>
</ContentPage.ToolbarItems>
<StackLayout
Padding="12">
<Button
AutomationId="WaitForStubControl"
Text="Navigate"
Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage>
@@ -0,0 +1,35 @@
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Platform;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 7823, "In a ToolbarItems, if an item has no icon but just text, MAUI uses the icon from the previous page in the Navigation", PlatformAffected.Android)]
public class Issue7823NavigationPage : NavigationPage
{
public Issue7823NavigationPage() : base(new Issue7823())
{

}
}

public partial class Issue7823 : ContentPage
{
public Issue7823()
{
InitializeComponent();
}

async void OnToolbarItemClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Issue7823Page2());
}

async void OnButtonClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Issue7823Page2());
}
}
}
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue7823Page2">
<ContentPage.ToolbarItems>
<ToolbarItem
Text="Test"
Clicked="OnToolbarItemClicked"/>
</ContentPage.ToolbarItems>
<Grid>
<Label
AutomationId="SecondPageLoaded"
HorizontalOptions="Center"
VerticalOptions="Center"
Text="Issue 7823" />
</Grid>
</ContentPage>
@@ -0,0 +1,21 @@
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Platform;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Issue7823Page2 : ContentPage
{
public Issue7823Page2()
{
InitializeComponent();
}

async void OnToolbarItemClicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
}
}
Expand Up @@ -377,6 +377,10 @@ internal static void UpdateMenuItemIcon(this IMauiContext mauiContext, IMenuItem
menuItem.SetIcon(iconDrawable);
}
}
else
{
menuItem.SetIcon(null);
}
});
}

Expand Down
29 changes: 29 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue7823.cs
@@ -0,0 +1,29 @@
using NUnit.Framework;
using OpenQA.Selenium;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue7823 : _IssuesUITest
{
public Issue7823(TestDevice device)
: base(device)
{ }

public override string Issue => "In a ToolbarItems, if an item has no icon but just text, MAUI uses the icon from the previous page in the Navigation";

[Test]
public void UpdateToolbarItemAfterNavigate()
{
// 1. Navigate from Page with a ToolbarItem using an Icon.
App.WaitForElement("WaitForStubControl");
App.Tap("WaitForStubControl");

App.WaitForElement("SecondPageLoaded");

// 2. Verify that the second page with a ToolbarItem without an icon does not show the icon of the previous page.
VerifyScreenshot();
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a93e88c

Please sign in to comment.