diff --git a/src/IssueViz.Security.UnitTests/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverterTest.cs b/src/IssueViz.Security.UnitTests/ReportView/EnumToImageSourceConverterTest.cs similarity index 83% rename from src/IssueViz.Security.UnitTests/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverterTest.cs rename to src/IssueViz.Security.UnitTests/ReportView/EnumToImageSourceConverterTest.cs index 7c856315c1..24cbbc41ac 100644 --- a/src/IssueViz.Security.UnitTests/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverterTest.cs +++ b/src/IssueViz.Security.UnitTests/ReportView/EnumToImageSourceConverterTest.cs @@ -22,14 +22,14 @@ using System.Windows; using System.Windows.Controls; using SonarLint.VisualStudio.Core.Analysis; -using SonarLint.VisualStudio.IssueVisualization.Security.DependencyRisks; +using SonarLint.VisualStudio.IssueVisualization.Security.ReportView; -namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.DependencyRisks; +namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.ReportView; [TestClass] -public class DependencyRiskImpactSeverityToImageSourceConverterTest +public class EnumToImageSourceConverterTest { - private DependencyRiskImpactSeverityToImageSourceConverter testSubject; + private EnumToImageSourceConverter testSubject; private IResourceFinder resourceFinder; private Button uiElement; @@ -38,7 +38,7 @@ public void Initialize() { uiElement = new Button(); resourceFinder = Substitute.For(); - testSubject = new DependencyRiskImpactSeverityToImageSourceConverter(); + testSubject = new EnumToImageSourceConverter(); } [TestMethod] @@ -80,12 +80,20 @@ public void Convert_ReturnsResourceForSeverity(DependencyRiskImpactSeverity seve var expectedResource = new Style(); resourceFinder.TryFindResource(uiElement, $"{severity}SeverityDrawingImage").Returns(expectedResource); - var result = testSubject.Convert([severity, uiElement, resourceFinder], null, null, CultureInfo.InvariantCulture); + var result = testSubject.Convert([severity, uiElement, resourceFinder], null, "Severity", CultureInfo.InvariantCulture); resourceFinder.Received(1).TryFindResource(uiElement, $"{severity}SeverityDrawingImage"); result.Should().Be(expectedResource); } + [TestMethod] + public void Convert_ParameterNotProvided_SearchesForResource() + { + testSubject.Convert([DependencyRiskImpactSeverity.Blocker, uiElement, resourceFinder], null, null, CultureInfo.InvariantCulture); + + resourceFinder.Received(1).TryFindResource(uiElement, $"{DependencyRiskImpactSeverity.Blocker}DrawingImage"); + } + [TestMethod] public void ConvertBack_ThrowsException() { diff --git a/src/IssueViz.Security/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverter.cs b/src/IssueViz.Security/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverter.cs deleted file mode 100644 index 53634f82ee..0000000000 --- a/src/IssueViz.Security/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverter.cs +++ /dev/null @@ -1,55 +0,0 @@ -/* - * SonarLint for Visual Studio - * Copyright (C) 2016-2025 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -using System.Globalization; -using System.Windows; -using System.Windows.Data; -using System.Windows.Media; -using SonarLint.VisualStudio.Core.Analysis; - -namespace SonarLint.VisualStudio.IssueVisualization.Security.DependencyRisks -{ - [ValueConversion(typeof(DependencyRiskImpactSeverity), typeof(ImageSource))] - public class DependencyRiskImpactSeverityToImageSourceConverter : IMultiValueConverter - { - private const string Suffix = "SeverityDrawingImage"; - - public object Convert( - object[] values, - Type targetType, - object parameter, - CultureInfo culture) - { - if (values.Length < 3 || values[0] is not DependencyRiskImpactSeverity severity || values[1] is not FrameworkElement element || values[2] is not IResourceFinder resourceFinder) - { - return null; - } - - return resourceFinder.TryFindResource(element, $"{severity}{Suffix}"); - } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - CultureInfo culture) => - throw new NotImplementedException(); - } -} diff --git a/src/IssueViz.Security/ReportView/EnumToImageSourceConverter.cs b/src/IssueViz.Security/ReportView/EnumToImageSourceConverter.cs new file mode 100644 index 0000000000..177185e7f7 --- /dev/null +++ b/src/IssueViz.Security/ReportView/EnumToImageSourceConverter.cs @@ -0,0 +1,60 @@ +/* + * SonarLint for Visual Studio + * Copyright (C) 2016-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System.Globalization; +using System.Windows; +using System.Windows.Data; +using System.Windows.Media; + +namespace SonarLint.VisualStudio.IssueVisualization.Security.ReportView; + +/// +/// Tries to find a DrawingImage resource based on the pattern [EnumValue][CustomSuffix]DrawingImage. +/// The enum value should be provided as the first binding value. +/// The FrameworkElement used to find the resource should be provided as the second binding value. +/// The IResourceFinder used to find the resource should be provided as the third binding value. +/// A custom suffix can be provided as a converter parameter. +/// +[ValueConversion(typeof(Enum), typeof(ImageSource))] +public class EnumToImageSourceConverter : IMultiValueConverter +{ + private const string Suffix = "DrawingImage"; + + public object Convert( + object[] values, + Type targetType, + object parameter, + CultureInfo culture) + { + if (values.Length < 3 || values[0] is not Enum enumValue || values[1] is not FrameworkElement element || values[2] is not IResourceFinder resourceFinder) + { + return null; + } + + return resourceFinder.TryFindResource(element, $"{enumValue}{parameter}{Suffix}"); + } + + public object[] ConvertBack( + object value, + Type[] targetTypes, + object parameter, + CultureInfo culture) => + throw new NotImplementedException(); +} diff --git a/src/IssueViz.Security/DependencyRisks/IResourceFinder.cs b/src/IssueViz.Security/ReportView/IResourceFinder.cs similarity index 95% rename from src/IssueViz.Security/DependencyRisks/IResourceFinder.cs rename to src/IssueViz.Security/ReportView/IResourceFinder.cs index f847e9ba2b..a96060f71d 100644 --- a/src/IssueViz.Security/DependencyRisks/IResourceFinder.cs +++ b/src/IssueViz.Security/ReportView/IResourceFinder.cs @@ -21,7 +21,7 @@ using System.Diagnostics.CodeAnalysis; using System.Windows; -namespace SonarLint.VisualStudio.IssueVisualization.Security.DependencyRisks; +namespace SonarLint.VisualStudio.IssueVisualization.Security.ReportView; /// /// Abstraction for finding resources introduced to allow UI testability. diff --git a/src/IssueViz.Security/ReportView/ReportViewControl.xaml b/src/IssueViz.Security/ReportView/ReportViewControl.xaml index 2b57c04fb6..1424970c7a 100644 --- a/src/IssueViz.Security/ReportView/ReportViewControl.xaml +++ b/src/IssueViz.Security/ReportView/ReportViewControl.xaml @@ -18,10 +18,7 @@ - - - + @@ -265,7 +262,7 @@ + Converter="{StaticResource EnumToImageSourceConverter}" ConverterParameter="Severity">