Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +38,7 @@ public void Initialize()
{
uiElement = new Button();
resourceFinder = Substitute.For<IResourceFinder>();
testSubject = new DependencyRiskImpactSeverityToImageSourceConverter();
testSubject = new EnumToImageSourceConverter();
}

[TestMethod]
Expand Down Expand Up @@ -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()
{
Expand Down

This file was deleted.

60 changes: 60 additions & 0 deletions src/IssueViz.Security/ReportView/EnumToImageSourceConverter.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// 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.
/// </summary>
[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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Windows;

namespace SonarLint.VisualStudio.IssueVisualization.Security.DependencyRisks;
namespace SonarLint.VisualStudio.IssueVisualization.Security.ReportView;

/// <summary>
/// Abstraction for finding resources introduced to allow UI testability.
Expand Down
7 changes: 2 additions & 5 deletions src/IssueViz.Security/ReportView/ReportViewControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
<ResourceDictionary Source="../SharedUI/SharedResources.xaml" />
</ResourceDictionary.MergedDictionaries>

<dependencyRisks:DependencyRiskImpactSeverityToImageSourceConverter
x:Key="DependencyRiskImpactSeverityToImageSourceConverter" />


<reportView:EnumToImageSourceConverter x:Key="EnumToImageSourceConverter" />
<wpf:BoolToVisibilityConverter x:Key="TrueToVisibleConverter" FalseValue="Collapsed" TrueValue="Visible" />
<wpf:BoolToVisibilityConverter x:Key="TrueToCollapsedConverter" FalseValue="Visible" TrueValue="Collapsed" />
<wpf:EnglishPluralizationConverter x:Key="EnglishPluralizationConverter" />
Expand Down Expand Up @@ -265,7 +262,7 @@
<Image Grid.Column="0" Height="17" VerticalAlignment="Center">
<Image.Source>
<MultiBinding
Converter="{StaticResource DependencyRiskImpactSeverityToImageSourceConverter}">
Converter="{StaticResource EnumToImageSourceConverter}" ConverterParameter="Severity">
<Binding Path="DependencyRisk.Severity" />
<Binding RelativeSource="{RelativeSource Self}" />
<Binding Path="ResourceFinder"
Expand Down