|
| 1 | +using PerfView; |
| 2 | +using System; |
| 3 | +using System.Linq; |
| 4 | +using System.Windows; |
| 5 | +using System.Windows.Controls; |
| 6 | +using System.Windows.Documents; |
| 7 | +using System.Windows.Media; |
| 8 | +using System.Windows.Shapes; |
| 9 | +using REghZyFramework.Themes; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace PerfViewTests.Accessibility |
| 13 | +{ |
| 14 | + public class FocusVisualTests |
| 15 | + { |
| 16 | + private const double MinimumContrastRatio = 3.0; |
| 17 | + |
| 18 | + [WpfFact] |
| 19 | + public void AccessibleFocusVisualMeetsContrastRequirements() |
| 20 | + { |
| 21 | + AssertThemeFocusVisual( |
| 22 | + "LightTheme.xaml", |
| 23 | + Colors.White, |
| 24 | + GetThemeColor("LightTheme.xaml", "ContainerBackground"), |
| 25 | + GetThemeColor("LightTheme.xaml", "ControlDefaultBackground")); |
| 26 | + |
| 27 | + AssertThemeFocusVisual( |
| 28 | + "DarkTheme.xaml", |
| 29 | + GetThemeColor("DarkTheme.xaml", "ContainerBackground"), |
| 30 | + GetThemeColor("DarkTheme.xaml", "ControlDefaultBackground")); |
| 31 | + } |
| 32 | + |
| 33 | + [WpfFact] |
| 34 | + public void ReportedControlsUseAccessibleFocusVisual() |
| 35 | + { |
| 36 | + ResourceDictionary theme = LoadTheme("LightTheme.xaml"); |
| 37 | + |
| 38 | + MainWindow mainWindow = null; |
| 39 | + try |
| 40 | + { |
| 41 | + App.CommandLineArgs = new CommandLineArgs(); |
| 42 | + App.CommandProcessor = new CommandProcessor(); |
| 43 | + mainWindow = new MainWindow(true); |
| 44 | + mainWindow.Resources.MergedDictionaries.Add(theme); |
| 45 | + |
| 46 | + Style expectedStyle = (Style)theme["AccessibleFocusVisual"]; |
| 47 | + Assert.Same(expectedStyle, mainWindow.Body.FocusVisualStyle); |
| 48 | + |
| 49 | + StatusBar statusBar = mainWindow.StatusBar; |
| 50 | + Assert.Same(expectedStyle, ((TextBox)statusBar.FindName("m_StatusMessage")).FocusVisualStyle); |
| 51 | + Assert.Same(expectedStyle, ((Button)statusBar.FindName("m_LogButton")).FocusVisualStyle); |
| 52 | + Assert.Same(expectedStyle, ((Button)statusBar.FindName("m_CancelButton")).FocusVisualStyle); |
| 53 | + |
| 54 | + Hyperlink welcomeLink = mainWindow.Body.Document.Blocks |
| 55 | + .OfType<Paragraph>() |
| 56 | + .SelectMany(paragraph => paragraph.Inlines) |
| 57 | + .OfType<Hyperlink>() |
| 58 | + .First(); |
| 59 | + Assert.Same(expectedStyle, welcomeLink.FocusVisualStyle); |
| 60 | + } |
| 61 | + finally |
| 62 | + { |
| 63 | + mainWindow?.Close(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #region private |
| 68 | + |
| 69 | + private static void AssertThemeFocusVisual(string themeName, params Color[] adjacentColors) |
| 70 | + { |
| 71 | + ResourceDictionary theme = LoadTheme(themeName); |
| 72 | + SolidColorBrush focusBrush = (SolidColorBrush)theme["AccessibleFocusVisualBrush"]; |
| 73 | + Style focusStyle = (Style)theme["AccessibleFocusVisual"]; |
| 74 | + Setter templateSetter = focusStyle.Setters |
| 75 | + .OfType<Setter>() |
| 76 | + .Single(setter => setter.Property == Control.TemplateProperty); |
| 77 | + Rectangle focusRectangle = (Rectangle)((ControlTemplate)templateSetter.Value).LoadContent(); |
| 78 | + |
| 79 | + Assert.True(focusRectangle.StrokeThickness >= 2); |
| 80 | + Assert.Equal(focusBrush.Color, ((SolidColorBrush)focusRectangle.Stroke).Color); |
| 81 | + |
| 82 | + foreach (Color adjacentColor in adjacentColors) |
| 83 | + { |
| 84 | + double contrastRatio = GetContrastRatio(focusBrush.Color, adjacentColor); |
| 85 | + Assert.True( |
| 86 | + contrastRatio >= MinimumContrastRatio, |
| 87 | + $"{themeName} focus color {focusBrush.Color} has a contrast ratio of only {contrastRatio:F3}:1 against {adjacentColor}."); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static Color GetThemeColor(string themeName, string resourceKey) |
| 92 | + { |
| 93 | + return ((SolidColorBrush)LoadTheme(themeName)[resourceKey]).Color; |
| 94 | + } |
| 95 | + |
| 96 | + private static ResourceDictionary LoadTheme(string themeName) |
| 97 | + { |
| 98 | + if (themeName == "LightTheme.xaml") |
| 99 | + { |
| 100 | + var theme = new LightTheme(); |
| 101 | + theme.InitializeComponent(); |
| 102 | + return theme; |
| 103 | + } |
| 104 | + |
| 105 | + if (themeName == "DarkTheme.xaml") |
| 106 | + { |
| 107 | + var theme = new DarkTheme(); |
| 108 | + theme.InitializeComponent(); |
| 109 | + return theme; |
| 110 | + } |
| 111 | + |
| 112 | + throw new ArgumentException($"Unknown theme '{themeName}'.", nameof(themeName)); |
| 113 | + } |
| 114 | + |
| 115 | + private static double GetContrastRatio(Color first, Color second) |
| 116 | + { |
| 117 | + double firstLuminance = GetRelativeLuminance(first); |
| 118 | + double secondLuminance = GetRelativeLuminance(second); |
| 119 | + return (Math.Max(firstLuminance, secondLuminance) + 0.05) / |
| 120 | + (Math.Min(firstLuminance, secondLuminance) + 0.05); |
| 121 | + } |
| 122 | + |
| 123 | + private static double GetRelativeLuminance(Color color) |
| 124 | + { |
| 125 | + return (0.2126 * GetLinearChannel(color.R)) + |
| 126 | + (0.7152 * GetLinearChannel(color.G)) + |
| 127 | + (0.0722 * GetLinearChannel(color.B)); |
| 128 | + } |
| 129 | + |
| 130 | + private static double GetLinearChannel(byte channel) |
| 131 | + { |
| 132 | + double value = channel / 255.0; |
| 133 | + return value <= 0.04045 |
| 134 | + ? value / 12.92 |
| 135 | + : Math.Pow((value + 0.055) / 1.055, 2.4); |
| 136 | + } |
| 137 | + |
| 138 | + #endregion |
| 139 | + } |
| 140 | +} |
0 commit comments