Inheritance: ViewModelBase
        public void ProjectViewModel_ToolTipProjectName_RespectsIsBound()
        {
            // Setup
            var projectInfo = new ProjectInformation
            {
                Key = "P1",
                Name = "Project1"
            };
            var viewModel = new ProjectViewModel(CreateServerViewModel(), projectInfo);

            // Test Case 1: When project is bound, should show message with 'bound' marker
            // Act
            viewModel.IsBound = true;

            // Verify
            StringAssert.Contains(viewModel.ToolTipProjectName, viewModel.ProjectName, "ToolTip message should include the project name");
            Assert.AreNotEqual(viewModel.ProjectName, viewModel.ToolTipProjectName, "ToolTip message should also indicate that the project is 'bound'");

            // Test Case 2: When project is NOT bound, should show project name only
            // Act
            viewModel.IsBound = false;

            // Verify
            Assert.AreEqual(viewModel.ProjectName, viewModel.ToolTipProjectName, "ToolTip message should be exactly the same as the project name");
        }
        public void TransferableVisualState_BoundProjectManagement()
        {
            // Setup
            var testSubject = new TransferableVisualState();
            var server = new ServerViewModel(new Integration.Service.ConnectionInformation(new System.Uri("http://server")));
            var project1 = new ProjectViewModel(server, new Integration.Service.ProjectInformation());
            var project2 = new ProjectViewModel(server, new Integration.Service.ProjectInformation());

            // Act (bind to something)
            testSubject.SetBoundProject(project1);

            // Verify
            Assert.IsTrue(testSubject.HasBoundProject);
            Assert.IsTrue(project1.IsBound);
            Assert.IsFalse(project2.IsBound);
            Assert.IsFalse(server.ShowAllProjects);

            // Act (bind to something else)
            testSubject.SetBoundProject(project2);

            // Verify
            Assert.IsTrue(testSubject.HasBoundProject);
            Assert.IsFalse(project1.IsBound);
            Assert.IsTrue(project2.IsBound);
            Assert.IsFalse(server.ShowAllProjects);

            // Act(clear binding)
            testSubject.ClearBoundProject();

            // Verify
            Assert.IsFalse(testSubject.HasBoundProject);
            Assert.IsFalse(project1.IsBound);
            Assert.IsFalse(project2.IsBound);
            Assert.IsTrue(server.ShowAllProjects);
        }
        public void SetBoundProject(ProjectViewModel project)
        {
            Debug.Assert(ThreadHelper.CheckAccess(), $"{nameof(SetBoundProject)} should only be accessed from the UI thread");
            this.ClearBoundProject();

            this.boundProject = project;
            this.boundProject.IsBound = true;
            this.boundProject.Owner.ShowAllProjects = false;

            this.OnHasBoundProjectChanged();
        }
        public void ClearBoundProject()
        {
            Debug.Assert(ThreadHelper.CheckAccess(), $"{nameof(ClearBoundProject)} should only be accessed from the UI thread");
            if (this.boundProject != null)
            {
                this.boundProject.IsBound = false;
                this.boundProject.Owner.ShowAllProjects = true;
                this.boundProject = null;

                this.OnHasBoundProjectChanged();
            }
        }
        public void ProjectViewModel_Ctor()
        {
            // Setup
            var projectInfo = new ProjectInformation
            {
                Key = "P1",
                Name = "Project1"
            };
            var serverVM = CreateServerViewModel();

            // Act
            var viewModel = new ProjectViewModel(serverVM, projectInfo);

            // Verify
            Assert.IsFalse(viewModel.IsBound);
            Assert.AreEqual(projectInfo.Key, viewModel.Key);
            Assert.AreEqual(projectInfo.Name, viewModel.ProjectName);
            Assert.AreSame(projectInfo, viewModel.ProjectInformation);
            Assert.AreSame(serverVM, viewModel.Owner);
        }
        private ProjectViewModel ConfigureProjectViewModel(ConfigurableSectionController section, Uri serverUri, string projectKey)
        {
            if (serverUri == null)
            {
                Assert.Inconclusive("Test setup: the server uri is not valid");
            }

            if (string.IsNullOrWhiteSpace(projectKey))
            {
                Assert.Inconclusive("Test setup: the project key is not valid");
            }

            section.ViewModel.State.ConnectedServers.Clear();
            var serverVM = new ServerViewModel(new ConnectionInformation(serverUri));
            section.ViewModel.State.ConnectedServers.Add(serverVM);
            var projectVM = new ProjectViewModel(serverVM, new ProjectInformation { Key = projectKey });
            serverVM.Projects.Add(projectVM);

            return projectVM;
        }
 private void OnBind(ProjectViewModel projectVM)
 {
     this.OnBind(projectVM?.ProjectInformation);
 }
 private bool OnBindStatus(ProjectViewModel projectVM)
 {
     return this.OnBindStatus(projectVM?.ProjectInformation);
 }
        private void ExecBrowseToProjectDashboard(ProjectViewModel project)
        {
            Debug.Assert(this.CanExecBrowseToProjectDashboard(project), $"Shouldn't be able to execute {nameof(this.BrowseToProjectDashboardCommand)}");

            TelemetryLoggerAccessor.GetLogger(this.ServiceProvider)?.ReportEvent(TelemetryEvent.BrowseToProjectDashboardCommandCommandCalled);

            var url = this.Host.SonarQubeService.CreateProjectDashboardUrl(project.Owner.ConnectionInformation, project.ProjectInformation);
            this.webBrowser.NavigateTo(url.ToString());
        }
        private bool CanExecBrowseToProjectDashboard(ProjectViewModel project)
        {
            if (project != null)
            {
                var url = this.Host.SonarQubeService.CreateProjectDashboardUrl(project.Owner.ConnectionInformation, project.ProjectInformation);
                return this.CanExecBrowseToUrl(url.ToString());
            }

            return false;
        }
        public void ProjectViewModel_AutomationName()
        {
            // Setup
            var projectInfo = new ProjectInformation
            {
                Key = "P1",
                Name = "Project1"
            };
            var testSubject = new ProjectViewModel(CreateServerViewModel(), projectInfo);

            var expectedNotBound = projectInfo.Name;
            var expectedBound = string.Format(CultureInfo.CurrentCulture, Strings.AutomationProjectBoundDescription, projectInfo.Name);

            // Test case 1: bound
            // Act
            testSubject.IsBound = true;
            var actualBound = testSubject.AutomationName;

            // Verify
            Assert.AreEqual(expectedBound, actualBound, "Unexpected bound SonarQube project description");


            // Test case 2: not bound
            // Act
            testSubject.IsBound = false;
            var actualNotBound = testSubject.AutomationName;

            // Verify
            Assert.AreEqual(expectedNotBound, actualNotBound, "Unexpected unbound SonarQube project description");
        }
        public void SectionController_BrowseToProjectDashboardCommand()
        {
            // Setup
            var webBrowser = new ConfigurableWebBrowser();
            var testSubject = this.CreateTestSubject(webBrowser);
            var serverUrl = new Uri("http://my-sonar-server:5555");
            var connectionInfo = new ConnectionInformation(serverUrl);
            var projectInfo = new ProjectInformation { Key = "p1" };

            Uri expectedUrl = new Uri(serverUrl, string.Format(SonarQubeServiceWrapper.ProjectDashboardRelativeUrl, projectInfo.Key));
            this.sonarQubeService.RegisterProjectDashboardUrl(connectionInfo, projectInfo, expectedUrl);

            // Case 1: Null parameter
            // Act + Verify CanExecute
            Assert.IsFalse(testSubject.BrowseToProjectDashboardCommand.CanExecute(null));

            // Case 2: Project VM
            var serverViewModel = new ServerViewModel(connectionInfo);
            var projectViewModel = new ProjectViewModel(serverViewModel, projectInfo);

            // Act + Verify CanExecute
            Assert.IsTrue(testSubject.BrowseToProjectDashboardCommand.CanExecute(projectViewModel));

            // Act + Verify Execute
            testSubject.BrowseToProjectDashboardCommand.Execute(projectViewModel);
            webBrowser.AssertNavigateToCalls(1);
            webBrowser.AssertRequestToNavigateTo(expectedUrl.ToString());
        }
        public void SectionController_ToggleShowAllProjectsCommand()
        {
            // Setup
            var testSubject = this.CreateTestSubject();
            var connInfo = new ConnectionInformation(new Uri("http://localhost"));
            var projectInfo = new ProjectInformation { Key = "p1", Name = "proj1" };
            var server = new ServerViewModel(connInfo);
            var project = new ProjectViewModel(server, projectInfo);
            server.Projects.Add(project);

            // Case 1: No bound projects
            project.IsBound = false;

            // Act + Verify CanExecute
            Assert.IsFalse(testSubject.ToggleShowAllProjectsCommand.CanExecute(server));

            // Case 2: Bound
            project.IsBound = true;

            // Act + Verify
            Assert.IsTrue(testSubject.ToggleShowAllProjectsCommand.CanExecute(server));

            // Verify execution
            bool original = server.ShowAllProjects;

            // Act
            testSubject.ToggleShowAllProjectsCommand.Execute(server);

            // Verify
            Assert.AreEqual(!original, server.ShowAllProjects);

            // Act
            testSubject.ToggleShowAllProjectsCommand.Execute(server);

            // Verify
            Assert.AreEqual(original, server.ShowAllProjects);
        }
 private static void VerifyProjectViewModelCommand(ProjectViewModel projectVM, ICommand internalCommand, object fixedContext, bool hasIcon)
 {
     ContextualCommandViewModel commandVM = AssertCommandExists(projectVM.Commands, internalCommand);
     Assert.IsNotNull(commandVM.DisplayText, "DisplayText expected");
     Assert.AreEqual(fixedContext, commandVM.InternalFixedContext, "The fixed context is incorrect");
     Assert.AreEqual(internalCommand, commandVM.InternalRealCommand, "Unexpected command");
     if (hasIcon)
     {
         Assert.IsNotNull(commandVM.Icon, "Icon expected");
         Assert.IsNotNull(commandVM.Icon.Moniker, "Icon moniker expected");
     }
     else
     {
         Assert.IsNull(commandVM.Icon, "Icon not expected");
     }
 }