Skip to content

Commit

Permalink
Fix issue with NavigationViewItem announcing collapsed when not havin…
Browse files Browse the repository at this point in the history
…g children (#2770)

* Fix issue with collapse being announced on NavViewItem with no children

* Add test
  • Loading branch information
marcelwgn authored and ranjeshj committed Jul 7, 2020
1 parent e975aef commit 24197cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dev/NavigationView/NavigationViewItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class NavigationViewItem :
void RotateExpandCollapseChevron(bool isExpanded);
bool IsRepeaterVisible();
void PropagateDepthToChildren(int depth);
bool HasChildren();

private:
void UpdateNavigationViewItemToolTip();
Expand Down Expand Up @@ -97,7 +98,6 @@ class NavigationViewItem :
bool IsOnLeftNav();
bool IsOnTopPrimary();
bool HasChildren();

void UpdateRepeaterItemsSource();
void ReparentRepeater();
void ReparentContent();
Expand Down
14 changes: 12 additions & 2 deletions dev/NavigationView/NavigationViewItemAutomationPeer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#include "pch.h"
Expand Down Expand Up @@ -48,7 +48,8 @@ winrt::IInspectable NavigationViewItemAutomationPeer::GetPatternCore(winrt::Patt
{
if (pattern == winrt::PatternInterface::SelectionItem ||
pattern == winrt::PatternInterface::Invoke ||
pattern == winrt::PatternInterface::ExpandCollapse)
// Only provide expand collapse pattern if we have children!
(pattern == winrt::PatternInterface::ExpandCollapse && HasChildren()))
{
return *this;
}
Expand Down Expand Up @@ -457,3 +458,12 @@ void NavigationViewItemAutomationPeer::ChangeSelection(bool isSelected)
nvi.IsSelected(isSelected);
}
}

bool NavigationViewItemAutomationPeer::HasChildren()
{
if (const auto& navigationViewItem = Owner().try_as<NavigationViewItem>())
{
return navigationViewItem->HasChildren();
}
return false;
}
1 change: 1 addition & 0 deletions dev/NavigationView/NavigationViewItemAutomationPeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ class NavigationViewItemAutomationPeer :
int32_t GetPositionOrSetCountInLeftNavHelper(AutomationOutput automationOutput);
int32_t GetPositionOrSetCountInTopNavHelper(AutomationOutput automationOutput);
void ChangeSelection(bool isSelected);
bool HasChildren();
};
31 changes: 31 additions & 0 deletions dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,37 @@ public void VerifyNavigationItemUIAType()
});
}

[TestMethod]
public void VerifyAutomationPeerExpandCollapsePatternBehavior()
{
RunOnUIThread.Execute(() =>
{

var menuItem1 = new NavigationViewItem();
var menuItem2 = new NavigationViewItem();
var menuItem3 = new NavigationViewItem();
var menuItem4 = new NavigationViewItem();
menuItem1.Content = "Item 1";
menuItem2.Content = "Item 2";
menuItem3.Content = "Item 3";
menuItem4.Content = "Item 4";

menuItem2.MenuItems.Add(menuItem3);
menuItem4.HasUnrealizedChildren = true;

var expandPeer = NavigationViewItemAutomationPeer.CreatePeerForElement(menuItem1).GetPattern(PatternInterface.ExpandCollapse);

Verify.IsNull(expandPeer,"Verify NavigationViewItem with no children has no ExpandCollapse pattern");

expandPeer = NavigationViewItemAutomationPeer.CreatePeerForElement(menuItem2).GetPattern(PatternInterface.ExpandCollapse);
Verify.IsNotNull(expandPeer,"Verify NavigationViewItem with children has an ExpandCollapse pattern provided");

expandPeer = NavigationViewItemAutomationPeer.CreatePeerForElement(menuItem4).GetPattern(PatternInterface.ExpandCollapse);
Verify.IsNotNull(expandPeer,"Verify NavigationViewItem without children but with UnrealizedChildren set to true has an ExpandCollapse pattern provided");
});
}


[TestMethod]
public void VerifySettingsItemToolTip()
{
Expand Down

0 comments on commit 24197cc

Please sign in to comment.