Skip to content

Commit

Permalink
NotificationService: Show notifications on different ToolWindow locat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
vnaskos-sonar committed Oct 16, 2024
1 parent 6993e8b commit 95fc9e1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
43 changes: 43 additions & 0 deletions src/Core.UnitTests/Notifications/NotificationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ public void ShowNotification_HasPreviousNotification_PreviousNotificationRemoved
VerifyInfoBarCreatedCorrectly(infoBarManager, notification2);
VerifySubscribedToInfoBarEvents(infoBar2);
}

[TestMethod]
public void ShowNotification_WithToolWindowGuid_ShowOnToolWindow()
{
var aToolWindowId = Guid.NewGuid();
var notification = CreateNotification();
var infoBar = CreateInfoBar();
var infoBarManager = new Mock<IInfoBarManager>();
SetupInfoBarManager(infoBarManager, notification, infoBar, aToolWindowId);
var testSubject = CreateTestSubject(infoBarManager.Object);

testSubject.ShowNotification(notification, aToolWindowId);

VerifyInfoBarCreatedCorrectly(infoBarManager, notification, aToolWindowId);
VerifySubscribedToInfoBarEvents(infoBar);
}

[TestMethod]
public void Dispose_NoExistingNotification_NoException()
Expand Down Expand Up @@ -628,6 +644,18 @@ private static void SetupInfoBarManager(Mock<IInfoBarManager> infoBarManager, IN
.Returns(infoBar.Object);
infoBarManager.Setup(x => x.CloseInfoBar(infoBar.Object)).Callback(() => infoBar.Raise(x => x.Closed += null, EventArgs.Empty));
}

private static void SetupInfoBarManager(Mock<IInfoBarManager> infoBarManager, INotification notification, Mock<IInfoBar> infoBar, Guid toolWindowId)
{
infoBarManager
.Setup(x => x.AttachInfoBarWithButtons(
toolWindowId,
notification.Message,
It.IsAny<string[]>(),
SonarLintImageMoniker.OfficialSonarLintMoniker))
.Returns(infoBar.Object);
infoBarManager.Setup(x => x.CloseInfoBar(infoBar.Object)).Callback(() => infoBar.Raise(x => x.Closed += null, EventArgs.Empty));
}

private static void VerifyInfoBarCreatedCorrectly(Mock<IInfoBarManager> infoBarManager, INotification notification)
{
Expand All @@ -642,6 +670,21 @@ private static void VerifyInfoBarCreatedCorrectly(Mock<IInfoBarManager> infoBarM

actualButtonTexts.Should().BeEquivalentTo(expectedButtonTexts);
}

private static void VerifyInfoBarCreatedCorrectly(Mock<IInfoBarManager> infoBarManager, INotification notification, Guid toolWindowId)
{
infoBarManager.Verify(x => x.AttachInfoBarWithButtons(
toolWindowId,
notification.Message,
It.IsAny<string[]>(),
SonarLintImageMoniker.OfficialSonarLintMoniker),
Times.Once);

var expectedButtonTexts = notification.Actions.Select(x => x.CommandText).ToArray();
var actualButtonTexts = infoBarManager.Invocations.First().Arguments[2] as IEnumerable<string>;

actualButtonTexts.Should().BeEquivalentTo(expectedButtonTexts);
}

private static void VerifyInfoBarRemoved(Mock<IInfoBarManager> infoBarManager, Mock<IInfoBar> infoBar)
{
Expand Down
38 changes: 30 additions & 8 deletions src/Core/Notifications/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace SonarLint.VisualStudio.Core.Notifications
public interface INotificationService : IDisposable
{
void ShowNotification(INotification notification);

void ShowNotification(INotification notification, Guid toolWindowId);

void CloseNotification();
}
Expand All @@ -42,6 +44,8 @@ public interface INotificationService : IDisposable
[PartCreationPolicy(CreationPolicy.NonShared)]
internal sealed class NotificationService : INotificationService
{
private static readonly Guid MainWindowId = Guid.Empty;

private readonly IInfoBarManager infoBarManager;
private readonly IDisabledNotificationsStorage notificationsStorage;
private readonly IThreadHandling threadHandling;
Expand All @@ -66,6 +70,11 @@ public NotificationService(IInfoBarManager infoBarManager,
}

public void ShowNotification(INotification notification)
{
ShowNotification(notification, MainWindowId);
}

public void ShowNotification(INotification notification, Guid toolWindowId)
{
if (notification == null)
{
Expand All @@ -87,7 +96,7 @@ public void ShowNotification(INotification notification)
threadHandling.RunOnUIThreadAsync(() =>
{
CloseNotification();
ShowInfoBar(notification);
ShowInfoBar(notification, toolWindowId);
});
}

Expand Down Expand Up @@ -143,16 +152,11 @@ private void CurrentInfoBar_Closed(object sender, EventArgs e)
activeNotification = null;
}

private void ShowInfoBar(INotification notification)
private void ShowInfoBar(INotification notification, Guid toolWindowId)
{
try
{
var buttonTexts = notification.Actions.Select(x => x.CommandText).ToArray();

var infoBar = infoBarManager.AttachInfoBarToMainWindow(notification.Message,
SonarLintImageMoniker.OfficialSonarLintMoniker,
buttonTexts);

var infoBar = AttachInfoBar(notification, toolWindowId);
activeNotification = new Tuple<IInfoBar, INotification>(infoBar, notification);
activeNotification.Item1.ButtonClick += CurrentInfoBar_ButtonClick;
activeNotification.Item1.Closed += CurrentInfoBar_Closed;
Expand All @@ -167,6 +171,24 @@ private void ShowInfoBar(INotification notification)
}
}

private IInfoBar AttachInfoBar(INotification notification, Guid toolWindowId)
{
var buttonTexts = notification.Actions.Select(x => x.CommandText).ToArray();

if (toolWindowId == MainWindowId)
{
return infoBarManager.AttachInfoBarToMainWindow(notification.Message,
SonarLintImageMoniker.OfficialSonarLintMoniker,
buttonTexts);
}

return infoBarManager.AttachInfoBarWithButtons(
toolWindowId,
notification.Message,
buttonTexts,
SonarLintImageMoniker.OfficialSonarLintMoniker);
}

public void Dispose()
{
CloseNotification();
Expand Down

0 comments on commit 95fc9e1

Please sign in to comment.