Skip to content

Commit 3bca58d

Browse files
SLVS-2570 Adapt converter to be able to search for any DarawingImage (#6419)
1 parent 064b487 commit 3bca58d

File tree

5 files changed

+77
-67
lines changed

5 files changed

+77
-67
lines changed
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
using System.Windows;
2323
using System.Windows.Controls;
2424
using SonarLint.VisualStudio.Core.Analysis;
25-
using SonarLint.VisualStudio.IssueVisualization.Security.DependencyRisks;
25+
using SonarLint.VisualStudio.IssueVisualization.Security.ReportView;
2626

27-
namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.DependencyRisks;
27+
namespace SonarLint.VisualStudio.IssueVisualization.Security.UnitTests.ReportView;
2828

2929
[TestClass]
30-
public class DependencyRiskImpactSeverityToImageSourceConverterTest
30+
public class EnumToImageSourceConverterTest
3131
{
32-
private DependencyRiskImpactSeverityToImageSourceConverter testSubject;
32+
private EnumToImageSourceConverter testSubject;
3333
private IResourceFinder resourceFinder;
3434
private Button uiElement;
3535

@@ -38,7 +38,7 @@ public void Initialize()
3838
{
3939
uiElement = new Button();
4040
resourceFinder = Substitute.For<IResourceFinder>();
41-
testSubject = new DependencyRiskImpactSeverityToImageSourceConverter();
41+
testSubject = new EnumToImageSourceConverter();
4242
}
4343

4444
[TestMethod]
@@ -80,12 +80,20 @@ public void Convert_ReturnsResourceForSeverity(DependencyRiskImpactSeverity seve
8080
var expectedResource = new Style();
8181
resourceFinder.TryFindResource(uiElement, $"{severity}SeverityDrawingImage").Returns(expectedResource);
8282

83-
var result = testSubject.Convert([severity, uiElement, resourceFinder], null, null, CultureInfo.InvariantCulture);
83+
var result = testSubject.Convert([severity, uiElement, resourceFinder], null, "Severity", CultureInfo.InvariantCulture);
8484

8585
resourceFinder.Received(1).TryFindResource(uiElement, $"{severity}SeverityDrawingImage");
8686
result.Should().Be(expectedResource);
8787
}
8888

89+
[TestMethod]
90+
public void Convert_ParameterNotProvided_SearchesForResource()
91+
{
92+
testSubject.Convert([DependencyRiskImpactSeverity.Blocker, uiElement, resourceFinder], null, null, CultureInfo.InvariantCulture);
93+
94+
resourceFinder.Received(1).TryFindResource(uiElement, $"{DependencyRiskImpactSeverity.Blocker}DrawingImage");
95+
}
96+
8997
[TestMethod]
9098
public void ConvertBack_ThrowsException()
9199
{

src/IssueViz.Security/DependencyRisks/DependencyRiskImpactSeverityToImageSourceConverter.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SonarLint for Visual Studio
3+
* Copyright (C) 2016-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
using System.Globalization;
22+
using System.Windows;
23+
using System.Windows.Data;
24+
using System.Windows.Media;
25+
26+
namespace SonarLint.VisualStudio.IssueVisualization.Security.ReportView;
27+
28+
/// <summary>
29+
/// Tries to find a DrawingImage resource based on the pattern [EnumValue][CustomSuffix]DrawingImage.
30+
/// The enum value should be provided as the first binding value.
31+
/// The FrameworkElement used to find the resource should be provided as the second binding value.
32+
/// The IResourceFinder used to find the resource should be provided as the third binding value.
33+
/// A custom suffix can be provided as a converter parameter.
34+
/// </summary>
35+
[ValueConversion(typeof(Enum), typeof(ImageSource))]
36+
public class EnumToImageSourceConverter : IMultiValueConverter
37+
{
38+
private const string Suffix = "DrawingImage";
39+
40+
public object Convert(
41+
object[] values,
42+
Type targetType,
43+
object parameter,
44+
CultureInfo culture)
45+
{
46+
if (values.Length < 3 || values[0] is not Enum enumValue || values[1] is not FrameworkElement element || values[2] is not IResourceFinder resourceFinder)
47+
{
48+
return null;
49+
}
50+
51+
return resourceFinder.TryFindResource(element, $"{enumValue}{parameter}{Suffix}");
52+
}
53+
54+
public object[] ConvertBack(
55+
object value,
56+
Type[] targetTypes,
57+
object parameter,
58+
CultureInfo culture) =>
59+
throw new NotImplementedException();
60+
}

src/IssueViz.Security/DependencyRisks/IResourceFinder.cs renamed to src/IssueViz.Security/ReportView/IResourceFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
using System.Diagnostics.CodeAnalysis;
2222
using System.Windows;
2323

24-
namespace SonarLint.VisualStudio.IssueVisualization.Security.DependencyRisks;
24+
namespace SonarLint.VisualStudio.IssueVisualization.Security.ReportView;
2525

2626
/// <summary>
2727
/// Abstraction for finding resources introduced to allow UI testability.

src/IssueViz.Security/ReportView/ReportViewControl.xaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
<ResourceDictionary Source="../SharedUI/SharedResources.xaml" />
1919
</ResourceDictionary.MergedDictionaries>
2020

21-
<dependencyRisks:DependencyRiskImpactSeverityToImageSourceConverter
22-
x:Key="DependencyRiskImpactSeverityToImageSourceConverter" />
23-
24-
21+
<reportView:EnumToImageSourceConverter x:Key="EnumToImageSourceConverter" />
2522
<wpf:BoolToVisibilityConverter x:Key="TrueToVisibleConverter" FalseValue="Collapsed" TrueValue="Visible" />
2623
<wpf:BoolToVisibilityConverter x:Key="TrueToCollapsedConverter" FalseValue="Visible" TrueValue="Collapsed" />
2724
<wpf:EnglishPluralizationConverter x:Key="EnglishPluralizationConverter" />
@@ -265,7 +262,7 @@
265262
<Image Grid.Column="0" Height="17" VerticalAlignment="Center">
266263
<Image.Source>
267264
<MultiBinding
268-
Converter="{StaticResource DependencyRiskImpactSeverityToImageSourceConverter}">
265+
Converter="{StaticResource EnumToImageSourceConverter}" ConverterParameter="Severity">
269266
<Binding Path="DependencyRisk.Severity" />
270267
<Binding RelativeSource="{RelativeSource Self}" />
271268
<Binding Path="ResourceFinder"

0 commit comments

Comments
 (0)