Skip to content
Closed
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
34 changes: 34 additions & 0 deletions tests/UI/Logic/AccessibleNamesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
if (!AccessibleLabels.HasAccessibleName(control))
{
unnamed.AppendLine($"{type.Name}: {control.GetType().Name} {Describe(control)}");
continue;
}

if (!HasUsableAccessibleName(control))
{
unnamed.AppendLine($"{type.Name}: {control.GetType().Name} has an empty/unusable accessible label {Describe(control)}");
}
}
}
Expand Down Expand Up @@ -122,6 +128,34 @@
}
}

private static bool HasUsableAccessibleName(Control control)
{
if (!string.IsNullOrWhiteSpace(AutomationProperties.GetName(control)))
{
return true;
}

var labeledBy = AutomationProperties.GetLabeledBy(control);
if (labeledBy == null)
{
return false;
}

if (!string.IsNullOrWhiteSpace(AutomationProperties.GetName(labeledBy)))
{
return true;
}

return labeledBy switch
{
TextBlock textBlock => !string.IsNullOrWhiteSpace(textBlock.Text),
Label label => !string.IsNullOrWhiteSpace(label.Content?.ToString()),
CheckBox checkBox => !string.IsNullOrWhiteSpace(checkBox.Content?.ToString()),
RadioButton radioButton => !string.IsNullOrWhiteSpace(radioButton.Content?.ToString()),
_ => false,
};
}

private static string Describe(Control control)
{
var parts = new List<string>();
Expand All @@ -130,9 +164,9 @@
parts.Add($"name={control.Name}");
}

if (control is TextBox textBox && !string.IsNullOrEmpty(textBox.Watermark))

Check warning on line 167 in tests/UI/Logic/AccessibleNamesTests.cs

View workflow job for this annotation

GitHub Actions / test

'TextBox.Watermark' is obsolete: 'Use PlaceholderText instead.'
{
parts.Add($"watermark={textBox.Watermark}");

Check warning on line 169 in tests/UI/Logic/AccessibleNamesTests.cs

View workflow job for this annotation

GitHub Actions / test

'TextBox.Watermark' is obsolete: 'Use PlaceholderText instead.'
}

var parent = control.Parent;
Expand Down
Loading