Skip to content

Commit

Permalink
NavigationViewItem: Empty tooltip no longer shown (#3430)
Browse files Browse the repository at this point in the history
* Fix empty tooltip being shown even though it shouldn't.

* Add API test.

* Improve API testing.

* Update API test.
  • Loading branch information
Felix-Dev authored Feb 13, 2021
1 parent 6bdf738 commit 6ab8ecc
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dev/NavigationView/NavigationViewItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@ void NavigationViewItem::UpdateNavigationViewItemToolTip()
void NavigationViewItem::SuggestedToolTipChanged(winrt::IInspectable const& newContent)
{
auto potentialString = newContent.try_as<winrt::IPropertyValue>();
const bool stringableToolTip = (potentialString && potentialString.Type() == winrt::PropertyType::String);
const bool validStringableToolTip = potentialString
&& potentialString.Type() == winrt::PropertyType::String
&& !potentialString.GetString().empty();

winrt::IInspectable newToolTipContent{ nullptr };
if (stringableToolTip)
if (validStringableToolTip)
{
newToolTipContent = newContent;
}
Expand Down
108 changes: 108 additions & 0 deletions dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,5 +1091,113 @@ public void VerifyHierarchicalNavigationTopModeMenuItemsSourceDoesNotCrash()
Content.UpdateLayout();
});
}

[TestMethod]
public void VerifyNavigationViewItemToolTipCreation()
{
if (!PlatformConfiguration.IsOsVersionGreaterThanOrEqual(OSVersion.Redstone5))
{
Log.Warning("On RS4 and earlier the test needs to be modified slightly.");
return;
}

RunOnUIThread.Execute(() =>
{
var navView = new NavigationView();

// Item with null content
var menuItem1 = new NavigationViewItem();

// Item with empty string as content
var menuItem2 = new NavigationViewItem();
menuItem2.Content = "";

// Item with null content and custom tooltip
var menuItem3 = new NavigationViewItem();
ToolTipService.SetToolTip(menuItem3, "Custom tooltip");

// Item with non-empty string content
var menuItem4 = new NavigationViewItem();
menuItem4.Content = "Item 4";

navView.MenuItems.Add(menuItem1);
navView.MenuItems.Add(menuItem2);
navView.MenuItems.Add(menuItem3);
navView.MenuItems.Add(menuItem4);

// Use a pane configuration where tooltips are shown.
navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Left;
navView.IsPaneOpen = false;

Content = navView;
navView.UpdateLayout();

Verify.AreEqual(null, ToolTipService.GetToolTip(menuItem1), "Item 1's tooltip should have been [null].");
Verify.AreEqual(null, ToolTipService.GetToolTip(menuItem2), "Item 2's tooltip should have been [null].");
Verify.AreEqual("Custom tooltip", ToolTipService.GetToolTip(menuItem3), "Item 3's tooltip should have been \"Custom tooltip\".");
Verify.AreEqual("Item 4", ToolTipService.GetToolTip(menuItem4), "Item 4's tooltip should have been \"Item 4\".");
});
}

[TestMethod]
public void VerifyNavigationViewItemToolTipPaneDisplayMode()
{
if (!PlatformConfiguration.IsOsVersionGreaterThanOrEqual(OSVersion.Redstone5))
{
Log.Warning("On RS4 and earlier the test needs to be modified slightly.");
return;
}

RunOnUIThread.Execute(() =>
{
var navView = new NavigationView();

// Item with non-empty string content using in-built tooltip
var menuItem1 = new NavigationViewItem();
menuItem1.Content = "Item 1";

// Item with custom tooltip
var menuItem2 = new NavigationViewItem();
menuItem2.Content = "Item 2";
ToolTipService.SetToolTip(menuItem2, "Custom tooltip");

navView.MenuItems.Add(menuItem1);
navView.MenuItems.Add(menuItem2);

Content = navView;

SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Left, false, "Item 1", "Custom tooltip");
SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Left, true, null, "Custom tooltip");

SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.LeftCompact, false, "Item 1", "Custom tooltip");
SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.LeftCompact, true, null, "Custom tooltip");

// Show tooltips again
SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Left, false, "Item 1", "Custom tooltip");

SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.LeftMinimal, true, null, "Custom tooltip");

// Show tooltips again
SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Left, false, "Item 1", "Custom tooltip");

SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Top, false, null, "Custom tooltip");

// Show tooltips again
SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Left, false, "Item 1", "Custom tooltip");

SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode.Top, true, null, "Custom tooltip");

void SetPaneConfigAndVerifyToolTips(NavigationViewPaneDisplayMode paneDisplayMode, bool isPaneOpen, string expectedDefaultToolTip, string expectedCustomToolTip)
{
Log.Comment($"Verifying tooltips with PaneDisplayMode=[{paneDisplayMode}] and IsPaneOpen=[{isPaneOpen}]");
navView.PaneDisplayMode = paneDisplayMode;
navView.IsPaneOpen = isPaneOpen;
Content.UpdateLayout();

Verify.AreEqual(expectedDefaultToolTip, ToolTipService.GetToolTip(menuItem1), $"Item 1's tooltip should have been \"{expectedDefaultToolTip ?? "null"}\".");
Verify.AreEqual(expectedCustomToolTip, ToolTipService.GetToolTip(menuItem2), $"Item 2's tooltip should have been {expectedCustomToolTip}.");
}
});
}
}
}

0 comments on commit 6ab8ecc

Please sign in to comment.