Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLVS-1490 Refactor test class #5729

Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 41 additions & 0 deletions src/ConnectedMode.UnitTests/UI/ConnectedModeManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2024 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 SonarLint.VisualStudio.ConnectedMode.UI;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.UI
{
[TestClass]
public class ConnectedModeManagerTests
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<ConnectedModeManager, IConnectedModeManager>(
MefTestHelpers.CreateExport<IConnectedModeServices>(),
MefTestHelpers.CreateExport<IConnectedModeBindingServices>());
}

[TestMethod]
public void MefCtor_CheckIsSingleton()
=> MefTestHelpers.CheckIsNonSharedMefComponent<ConnectedModeManager>();
}
}
53 changes: 53 additions & 0 deletions src/ConnectedMode/UI/ConnectedModeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SonarLint for Visual Studio
* Copyright (C) 2016-2024 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.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using SonarLint.VisualStudio.ConnectedMode.UI.ManageBinding;

namespace SonarLint.VisualStudio.ConnectedMode.UI;

public interface IConnectedModeManager
{
void ShowManageBindingDialog(bool useSharedBindingOnInitialization = false);
}

[Export(typeof(IConnectedModeManager))]
[PartCreationPolicy(CreationPolicy.NonShared)]
internal sealed class ConnectedModeManager : IConnectedModeManager
{
private readonly IConnectedModeServices connectedModeServices;
private readonly IConnectedModeBindingServices connectedModeBindingServices;

[ImportingConstructor]
public ConnectedModeManager(IConnectedModeServices connectedModeServices, IConnectedModeBindingServices connectedModeBindingServices)
{
this.connectedModeServices = connectedModeServices;
this.connectedModeBindingServices = connectedModeBindingServices;
}

[ExcludeFromCodeCoverage] // UI, not really unit-testable
public void ShowManageBindingDialog(bool useSharedBindingOnInitialization = false)
{
var manageBindingDialog = new ManageBindingDialog(connectedModeServices, connectedModeBindingServices, useSharedBindingOnInitialization);
manageBindingDialog.ShowDialog(Application.Current.MainWindow);
}
}
65 changes: 34 additions & 31 deletions src/Integration.UnitTests/Binding/BindingSuggestionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,44 @@
*/

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.Integration.TeamExplorer;
using SonarLint.VisualStudio.Integration.Binding;

namespace SonarLint.VisualStudio.Integration.UnitTests.Binding;

[TestClass]
public class BindingSuggestionHandlerTests
{
private BindingSuggestionHandler testSubject;
private INotificationService notificationService;
private IActiveSolutionBoundTracker activeSolutionBoundTracker;
private IIDEWindowService ideWindowService;
private IConnectedModeManager connectedModeManager;
private IBrowserService browserService;

[TestInitialize]
public void TestInitialize()
{
notificationService = Substitute.For<INotificationService>();
activeSolutionBoundTracker = Substitute.For<IActiveSolutionBoundTracker>();
ideWindowService = Substitute.For<IIDEWindowService>();
connectedModeManager = Substitute.For<IConnectedModeManager>();
browserService = Substitute.For<IBrowserService>();
testSubject = new BindingSuggestionHandler(notificationService, activeSolutionBoundTracker, ideWindowService, connectedModeManager, browserService);
}

[TestMethod]
public void MefCtor_CheckExports()
{
MefTestHelpers.CheckTypeCanBeImported<BindingSuggestionHandler, IBindingSuggestionHandler>(
MefTestHelpers.CreateExport<INotificationService>(),
MefTestHelpers.CreateExport<IActiveSolutionBoundTracker>(),
MefTestHelpers.CreateExport<IIDEWindowService>(),
MefTestHelpers.CreateExport<ITeamExplorerController>(),
MefTestHelpers.CreateExport<IConnectedModeManager>(),
MefTestHelpers.CreateExport<IBrowserService>());
}

Expand All @@ -47,9 +65,8 @@ public void MefCtor_CheckExports()
[DataRow(SonarLintMode.Connected)]
public void Notify_BringsWindowToFront(SonarLintMode sonarLintMode)
{
var ideWindowService = Substitute.For<IIDEWindowService>();
MockCurrentConfiguration(sonarLintMode);

var testSubject = CreateTestSubject(sonarLintMode: sonarLintMode, ideWindowService: ideWindowService);
testSubject.Notify();

ideWindowService.Received().BringToFront();
Expand All @@ -58,9 +75,8 @@ public void Notify_BringsWindowToFront(SonarLintMode sonarLintMode)
[TestMethod]
public void Notify_WithStandaloneProject_PromptsToConnect()
{
var notificationService = Substitute.For<INotificationService>();
MockCurrentConfiguration(SonarLintMode.Standalone);

var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Standalone, notificationService: notificationService);
testSubject.Notify();

notificationService.Received().ShowNotification(Arg.Is<INotification>(
Expand All @@ -73,9 +89,8 @@ public void Notify_WithStandaloneProject_PromptsToConnect()
[TestMethod]
public void Notify_WithBoundProject_ShowsConflictMessage()
{
var notificationService = Substitute.For<INotificationService>();
MockCurrentConfiguration(SonarLintMode.Connected);

var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Connected, notificationService: notificationService);
testSubject.Notify();

notificationService.Received().ShowNotification(Arg.Is<INotification>(
Expand All @@ -86,30 +101,28 @@ public void Notify_WithBoundProject_ShowsConflictMessage()
}

[TestMethod]
public void Notify_ConnectAction_OpensSonarQubePage()
public void Notify_ConnectAction_ShowsManageBindingDialog()
{
var notificationService = Substitute.For<INotificationService>();
var teamExplorerController = Substitute.For<ITeamExplorerController>();
MockCurrentConfiguration(SonarLintMode.Standalone);

var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Standalone, notificationService: notificationService, teamExplorerController: teamExplorerController);
testSubject.Notify();

var notification = (Notification)notificationService.ReceivedCalls().Single().GetArguments().Single();
var connectAction = notification.Actions.First(x => x.CommandText.Equals(BindingStrings.BindingSuggestionConnect));

teamExplorerController.DidNotReceive().ShowSonarQubePage();
connectedModeManager.DidNotReceive().ShowManageBindingDialog();
connectAction.Action(notification);

teamExplorerController.Received().ShowSonarQubePage();
connectedModeManager.Received().ShowManageBindingDialog();
}

[TestMethod]
public void Notify_LearnMoreAction_OpensDocumentationInBrowser()
{
var notificationService = Substitute.For<INotificationService>();
var browserService = Substitute.For<IBrowserService>();
MockCurrentConfiguration(SonarLintMode.Standalone);

var testSubject = CreateTestSubject(sonarLintMode: SonarLintMode.Standalone, notificationService: notificationService, browserService: browserService);
testSubject.Notify();

var notification = (Notification)notificationService.ReceivedCalls().Single().GetArguments().Single();
var connectAction = notification.Actions.First(x => x.CommandText.Equals(BindingStrings.BindingSuggestionLearnMore));

Expand All @@ -119,20 +132,10 @@ public void Notify_LearnMoreAction_OpensDocumentationInBrowser()
browserService.Received().Navigate(DocumentationLinks.OpenInIdeBindingSetup);
}

private BindingSuggestionHandler CreateTestSubject(SonarLintMode sonarLintMode,
INotificationService notificationService = null,
IIDEWindowService ideWindowService = null,
ITeamExplorerController teamExplorerController = null,
IBrowserService browserService = null)
private void MockCurrentConfiguration(SonarLintMode sonarLintMode)
{
notificationService ??= Substitute.For<INotificationService>();
var activeSolutionBoundTracker = Substitute.For<IActiveSolutionBoundTracker>();
ideWindowService ??= Substitute.For<IIDEWindowService>();
teamExplorerController ??= Substitute.For<ITeamExplorerController>();
browserService ??= Substitute.For<IBrowserService>();

activeSolutionBoundTracker.CurrentConfiguration.Returns(new BindingConfiguration(new BoundServerProject("solution", "server project", new ServerConnection.SonarCloud("org")), sonarLintMode, "a-directory"));

return new BindingSuggestionHandler(notificationService, activeSolutionBoundTracker, ideWindowService, teamExplorerController, browserService);
activeSolutionBoundTracker.CurrentConfiguration.Returns(new BindingConfiguration(
new BoundServerProject("solution", "server project", new ServerConnection.SonarCloud("org")), sonarLintMode,
"a-directory"));
}
}
10 changes: 5 additions & 5 deletions src/Integration/Binding/BindingSuggestionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

using System.ComponentModel.Composition;
using SonarLint.VisualStudio.ConnectedMode.Binding.Suggestion;
using SonarLint.VisualStudio.ConnectedMode.UI;
using SonarLint.VisualStudio.Core.Binding;
using SonarLint.VisualStudio.Core.Notifications;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Integration.TeamExplorer;

namespace SonarLint.VisualStudio.Integration.Binding
{
Expand All @@ -34,20 +34,20 @@ internal class BindingSuggestionHandler : IBindingSuggestionHandler
private readonly INotificationService notificationService;
private readonly IActiveSolutionBoundTracker activeSolutionBoundTracker;
private readonly IIDEWindowService ideWindowService;
private readonly ITeamExplorerController teamExplorerController;
private readonly IConnectedModeManager connectedModeManager;
private readonly IBrowserService browserService;

[ImportingConstructor]
public BindingSuggestionHandler(INotificationService notificationService,
IActiveSolutionBoundTracker activeSolutionBoundTracker,
IIDEWindowService ideWindowService,
ITeamExplorerController teamExplorerController,
IConnectedModeManager connectedModeManager,
IBrowserService browserService)
{
this.notificationService = notificationService;
this.activeSolutionBoundTracker = activeSolutionBoundTracker;
this.ideWindowService = ideWindowService;
this.teamExplorerController = teamExplorerController;
this.connectedModeManager = connectedModeManager;
this.browserService = browserService;
}

Expand All @@ -62,7 +62,7 @@ public void Notify()
: BindingStrings.BindingSuggetsionBindingConflict;

var connectAction = new NotificationAction(BindingStrings.BindingSuggestionConnect,
_ => teamExplorerController.ShowSonarQubePage(),
_ => connectedModeManager.ShowManageBindingDialog(),
true);
var learnMoreAction = new NotificationAction(BindingStrings.BindingSuggestionLearnMore,
_ => browserService.Navigate(DocumentationLinks.OpenInIdeBindingSetup),
Expand Down