Skip to content

Commit

Permalink
SLVS-1183 SLVS goldbars are not reset on solution/binding change (#5777)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaskos-sonar authored and georgii-borovinskikh-sonarsource committed Nov 5, 2024
1 parent 39a6438 commit cba74c7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
27 changes: 25 additions & 2 deletions src/Integration.UnitTests/Binding/BindingSuggestionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

using SonarLint.VisualStudio.ConnectedMode.Binding.Suggestion;
using SonarLint.VisualStudio.ConnectedMode.UI;
using SonarLint.VisualStudio.Core.Notifications;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.TestInfrastructure;
using SonarLint.VisualStudio.Core.Binding;
using SonarLint.VisualStudio.Core.Notifications;
using SonarLint.VisualStudio.Integration.Binding;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.Integration.UnitTests.Binding;

Expand Down Expand Up @@ -131,11 +131,34 @@ public void Notify_LearnMoreAction_OpensDocumentationInBrowser()

browserService.Received().Navigate(DocumentationLinks.OpenInIdeBindingSetup);
}

[TestMethod]
[DataRow(SonarLintMode.Connected, true)]
[DataRow(SonarLintMode.Standalone, false)]
public void SolutionBindingChanged_WhenConnectedMode_ClosesAnyOpenGoldBar(SonarLintMode mode, bool expectedToClose)
{
RaiseSolutionBindingChanged(mode);

notificationService.Received(expectedToClose ? 1 : 0).CloseNotification();
}

[TestMethod]
public void Dispose_UnsubscribesFromAllEvents()
{
testSubject.Dispose();

activeSolutionBoundTracker.Received(1).SolutionBindingChanged -= Arg.Any<EventHandler<ActiveSolutionBindingEventArgs>>();
}

private void MockCurrentConfiguration(SonarLintMode sonarLintMode)
{
activeSolutionBoundTracker.CurrentConfiguration.Returns(new BindingConfiguration(
new BoundServerProject("solution", "server project", new ServerConnection.SonarCloud("org")), sonarLintMode,
"a-directory"));
}

private void RaiseSolutionBindingChanged(SonarLintMode mode)
{
activeSolutionBoundTracker.SolutionBindingChanged += Raise.EventWith(new ActiveSolutionBindingEventArgs(new BindingConfiguration(null, mode, string.Empty)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SharedBindingSuggestionServiceTests
private IConnectedModeServices connectedModeServices;
private IConnectedModeBindingServices connectedModeBindingServices;
private IActiveSolutionTracker activeSolutionTracker;
private IActiveSolutionBoundTracker activeSolutionBoundTracker;
private IConnectedModeUIManager connectedModeManager;

[TestInitialize]
Expand All @@ -46,9 +47,10 @@ public void TestInitialize()
connectedModeServices = Substitute.For<IConnectedModeServices>();
connectedModeBindingServices = Substitute.For<IConnectedModeBindingServices>();
activeSolutionTracker = Substitute.For<IActiveSolutionTracker>();
activeSolutionBoundTracker = Substitute.For<IActiveSolutionBoundTracker>();
connectedModeManager = Substitute.For<IConnectedModeUIManager>();

testSubject = new SharedBindingSuggestionService(suggestSharedBindingGoldBar, connectedModeServices, connectedModeBindingServices, connectedModeManager, activeSolutionTracker);
testSubject = new SharedBindingSuggestionService(suggestSharedBindingGoldBar, connectedModeServices, connectedModeBindingServices, connectedModeManager, activeSolutionTracker, activeSolutionBoundTracker);
}

[TestMethod]
Expand All @@ -59,7 +61,8 @@ public void MefCtor_CheckExports()
MefTestHelpers.CreateExport<IConnectedModeServices>(),
MefTestHelpers.CreateExport<IConnectedModeBindingServices>(),
MefTestHelpers.CreateExport<IConnectedModeUIManager>(),
MefTestHelpers.CreateExport<IActiveSolutionTracker>());
MefTestHelpers.CreateExport<IActiveSolutionTracker>(),
MefTestHelpers.CreateExport<IActiveSolutionBoundTracker>());
}

[TestMethod]
Expand Down Expand Up @@ -112,22 +115,30 @@ public void ActiveSolutionChanged_SolutionIsOpened_ShowsGoldBar()
}

[TestMethod]
public void ActiveSolutionChanged_SolutionIsOpened_DoesNotShowGoldBar()
public void ActiveSolutionChanged_SolutionIsClosed_DoesNotShowGoldBar()
{
MockSharedBindingConfigExists();
MockSolutionMode(SonarLintMode.Standalone);

RaiseActiveSolutionChanged(false);

suggestSharedBindingGoldBar.DidNotReceive().Show(ServerType.SonarQube, Arg.Any<Action>());
suggestSharedBindingGoldBar.DidNotReceive().Show(Arg.Any<ServerType>(), Arg.Any<Action>());
}

[TestMethod]
[DataRow(SonarLintMode.Connected, true)]
[DataRow(SonarLintMode.Standalone, false)]
public void SolutionBindingChanged_WhenConnectedMode_ClosesAnyOpenGoldBar(SonarLintMode mode, bool expectedToClose)
{
RaiseSolutionBindingChanged(mode);

suggestSharedBindingGoldBar.Received(expectedToClose ? 1 : 0).Close();
}

[TestMethod]
public void Dispose_UnsubscribesFromActiveSolutionChanged()
public void Dispose_UnsubscribesFromAllEvents()
{
testSubject.Dispose();

activeSolutionTracker.Received(1).ActiveSolutionChanged -= Arg.Any<EventHandler<ActiveSolutionChangedEventArgs>>();
activeSolutionBoundTracker.Received(1).SolutionBindingChanged -= Arg.Any<EventHandler<ActiveSolutionBindingEventArgs>>();
}

[TestMethod]
Expand Down Expand Up @@ -163,6 +174,9 @@ private void MockSharedBindingConfigExists()
connectedModeBindingServices.SharedBindingConfigProvider.GetSharedBinding().Returns(new SharedBindingConfigModel());
}


private void RaiseSolutionBindingChanged(SonarLintMode mode)
{
activeSolutionBoundTracker.SolutionBindingChanged += Raise.EventWith(new ActiveSolutionBindingEventArgs(new BindingConfiguration(null, mode, string.Empty)));
}

}
19 changes: 17 additions & 2 deletions src/Integration/Binding/BindingSuggestionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
using System.ComponentModel.Composition;
using SonarLint.VisualStudio.ConnectedMode.Binding.Suggestion;
using SonarLint.VisualStudio.ConnectedMode.UI;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Binding;
using SonarLint.VisualStudio.Core.Notifications;
using SonarLint.VisualStudio.Core;

namespace SonarLint.VisualStudio.Integration.Binding
{
[Export(typeof(IBindingSuggestionHandler))]
[PartCreationPolicy(CreationPolicy.Shared)]
internal class BindingSuggestionHandler : IBindingSuggestionHandler
internal sealed class BindingSuggestionHandler : IBindingSuggestionHandler, IDisposable
{
private readonly INotificationService notificationService;
private readonly IActiveSolutionBoundTracker activeSolutionBoundTracker;
Expand All @@ -49,6 +49,8 @@ public BindingSuggestionHandler(INotificationService notificationService,
this.ideWindowService = ideWindowService;
this.connectedModeUiManager = connectedModeUiManager;
this.browserService = browserService;

this.activeSolutionBoundTracker.SolutionBindingChanged += OnActiveSolutionBindingChanged;
}

public void Notify()
Expand Down Expand Up @@ -78,5 +80,18 @@ public void Notify()

ideWindowService.BringToFront();
}

public void Dispose()
{
activeSolutionBoundTracker.SolutionBindingChanged -= OnActiveSolutionBindingChanged;
}

private void OnActiveSolutionBindingChanged(object sender, ActiveSolutionBindingEventArgs e)
{
if (e.Configuration.Mode == SonarLintMode.Connected)
{
notificationService.CloseNotification();
}
}
}
}
15 changes: 14 additions & 1 deletion src/Integration/MefServices/SharedBindingSuggestionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,26 @@ internal sealed class SharedBindingSuggestionService : ISharedBindingSuggestionS
private readonly IConnectedModeBindingServices connectedModeBindingServices;
private readonly IConnectedModeUIManager connectedModeUiManager;
private readonly IActiveSolutionTracker activeSolutionTracker;
private readonly IActiveSolutionBoundTracker activeSolutionBoundTracker;

[ImportingConstructor]
public SharedBindingSuggestionService(
ISuggestSharedBindingGoldBar suggestSharedBindingGoldBar,
IConnectedModeServices connectedModeServices,
IConnectedModeBindingServices connectedModeBindingServices,
IConnectedModeUIManager connectedModeUiManager,
IActiveSolutionTracker activeSolutionTracker)
IActiveSolutionTracker activeSolutionTracker,
IActiveSolutionBoundTracker activeSolutionBoundTracker)
{
this.suggestSharedBindingGoldBar = suggestSharedBindingGoldBar;
this.connectedModeServices = connectedModeServices;
this.connectedModeBindingServices = connectedModeBindingServices;
this.connectedModeUiManager = connectedModeUiManager;
this.activeSolutionTracker = activeSolutionTracker;
this.activeSolutionBoundTracker = activeSolutionBoundTracker;

this.activeSolutionTracker.ActiveSolutionChanged += OnActiveSolutionChanged;
this.activeSolutionBoundTracker.SolutionBindingChanged += OnActiveSolutionBindingChanged;
}

public void Suggest()
Expand All @@ -73,6 +77,7 @@ public void Suggest()
public void Dispose()
{
activeSolutionTracker.ActiveSolutionChanged -= OnActiveSolutionChanged;
activeSolutionBoundTracker.SolutionBindingChanged -= OnActiveSolutionBindingChanged;
}

private void ShowManageBindingDialog()
Expand All @@ -87,5 +92,13 @@ private void OnActiveSolutionChanged(object sender, ActiveSolutionChangedEventAr
Suggest();
}
}

private void OnActiveSolutionBindingChanged(object sender, ActiveSolutionBindingEventArgs e)
{
if (e.Configuration.Mode == SonarLintMode.Connected)
{
suggestSharedBindingGoldBar.Close();
}
}
}
}

0 comments on commit cba74c7

Please sign in to comment.