private void OnBindingFinished(ProjectInformation projectInformation, bool isFinishedSuccessfully)
        {
            this.IsBindingInProgress = false;
            this.host.VisualStateManager.ClearBoundProject();

            if (isFinishedSuccessfully)
            {
                this.host.VisualStateManager.SetBoundProject(projectInformation);

                var conflictsController = this.host.GetService <IRuleSetConflictsController>();
                conflictsController.AssertLocalServiceIsNotNull();

                if (conflictsController.CheckForConflicts())
                {
                    // In some cases we will end up navigating to the solution explorer, this will make sure that
                    // we're back in team explorer to view the conflicts
                    this.ServiceProvider.GetMefService <ITeamExplorerController>()?.ShowSonarQubePage();
                }
                else
                {
                    VsShellUtils.ActivateSolutionExplorer(this.ServiceProvider);
                }
            }
            else
            {
                IUserNotification notifications = this.host.ActiveSection?.UserNotifications;
                if (notifications != null)
                {
                    // Create a command with a fixed argument with the help of ContextualCommandViewModel that creates proxy command for the contextual (fixed) instance and the passed in ICommand that expects it
                    ICommand rebindCommand = new ContextualCommandViewModel(projectInformation, new RelayCommand <ProjectInformation>(this.OnBind, this.OnBindStatus)).Command;
                    notifications.ShowNotificationError(Strings.FailedToToBindSolution, NotificationIds.FailedToBindId, rebindCommand);
                }
            }
        }
        public void StateManager_BindCommand_DynamicText()
        {
            // Arrange
            var section                  = ConfigurableSectionController.CreateDefault();
            ConfigurableHost host        = new ConfigurableHost();
            StateManager     testSubject = this.CreateTestSubject(host, section);
            var connection1              = new ConnectionInformation(new Uri("http://127.0.0.1"));
            var projects                 = new SonarQubeProject[] { new SonarQubeProject("", "") };

            testSubject.SetProjects(connection1, projects);
            ProjectViewModel projectVM = testSubject.ManagedState.ConnectedServers.Single().Projects.Single();

            host.SetActiveSection(section);
            testSubject.SyncCommandFromActiveSection();
            ContextualCommandViewModel bindCmd = projectVM.Commands.First(x => x.InternalRealCommand.Equals(section.BindCommand));

            // Case 1: Bound
            projectVM.IsBound = true;
            // Act + Assert
            bindCmd.DisplayText.Should().Be(Strings.SyncButtonText, "Unexpected disabled context command text");

            // Case 2: Not bound
            projectVM.IsBound = false;

            // Act + Assert
            bindCmd.DisplayText.Should().Be(Strings.BindButtonText, "Unexpected context command text");
        }
예제 #3
0
        public void StateManager_ToggleShowAllProjectsCommand_DynamicText()
        {
            // Setup
            var section                  = ConfigurableSectionController.CreateDefault();
            ConfigurableHost host        = new ConfigurableHost();
            StateManager     testSubject = this.CreateTestSubject(host, section);
            var connection1              = new ConnectionInformation(new Uri("http://127.0.0.1"));
            var projects                 = new ProjectInformation[] { new ProjectInformation(), new ProjectInformation() };

            testSubject.SetProjects(connection1, projects);
            ServerViewModel serverVM = testSubject.ManagedState.ConnectedServers.Single();

            host.SetActiveSection(section);
            testSubject.SyncCommandFromActiveSection();
            ContextualCommandViewModel toggleContextCmd = serverVM.Commands.First(x => x.InternalRealCommand.Equals(section.ToggleShowAllProjectsCommand));

            // Case 1: No bound projects
            serverVM.ShowAllProjects = true;
            // Act + Verify
            Assert.AreEqual(Strings.HideUnboundProjectsCommandText, toggleContextCmd.DisplayText, "Unexpected disabled context command text");

            // Case 2: has bound projects
            serverVM.ShowAllProjects = false;

            // Act + Verify
            Assert.AreEqual(Strings.ShowAllProjectsCommandText, toggleContextCmd.DisplayText, "Unexpected context command text");
        }
        public void ContextualCommandViewModel_Ctor_NullArgChecks()
        {
            var command = new RelayCommand(() => { });
            ContextualCommandViewModel suppressAnalysisWarning;

            Exceptions.Expect <ArgumentNullException>(() =>
            {
                suppressAnalysisWarning = new ContextualCommandViewModel(null, command);
            });
        }
        private void SetServerVMCommands(ServerViewModel serverVM)
        {
            serverVM.Commands.Clear();
            if (this.Host.ActiveSection == null)
            {
                // Don't add command (which will be disabled).
                return;
            }


            var refreshContextualCommand = new ContextualCommandViewModel(serverVM, this.Host.ActiveSection.RefreshCommand)
            {
                DisplayText = Strings.RefreshCommandDisplayText,
                Tooltip     = Strings.RefreshCommandTooltip,
                Icon        = new IconViewModel(KnownMonikers.Refresh)
            };

            var browseServerContextualCommand = new ContextualCommandViewModel(serverVM.Url.ToString(), this.Host.ActiveSection.BrowseToUrlCommand)
            {
                DisplayText = Strings.BrowseServerMenuItemDisplayText,
                Tooltip     = Strings.BrowserServerMenuItemTooltip,
                Icon        = new IconViewModel(KnownMonikers.OpenWebSite)
            };

            var toggleShowAllProjectsCommand = new ContextualCommandViewModel(serverVM, this.Host.ActiveSection.ToggleShowAllProjectsCommand)
            {
                Tooltip = Strings.ToggleShowAllProjectsCommandTooltip
            };

            toggleShowAllProjectsCommand.SetDynamicDisplayText(x =>
            {
                ServerViewModel ctx = x as ServerViewModel;
                Debug.Assert(ctx != null, "Unexpected fixed context for ToggleShowAllProjects context command");
                return(ctx?.ShowAllProjects ?? false ? Strings.HideUnboundProjectsCommandText : Strings.ShowAllProjectsCommandText);
            });

            var unbindCommand = new ContextualCommandViewModel(serverVM, this.Host.ActiveSection.UnbindCommand);

            unbindCommand.DisplayText = Strings.Unbind_Command_DisplayText;
            unbindCommand.Tooltip     = Strings.Unbind_Command_Tooltip;
            unbindCommand.Icon        = new IconViewModel(KnownMonikers.Disconnect);

            // Note: the Disconnect command is not on the context menu, although it is
            // called directly from code e.g. when the solution unloads
            serverVM.Commands.Add(refreshContextualCommand);
            serverVM.Commands.Add(browseServerContextualCommand);
            serverVM.Commands.Add(toggleShowAllProjectsCommand);
            serverVM.Commands.Add(unbindCommand);
        }
        private void SetServerVMCommands(ServerViewModel serverVM)
        {
            serverVM.Commands.Clear();
            if (this.Host.ActiveSection == null)
            {
                // Don't add command (which will be disabled).
                return;
            }


            var refreshContextualCommand = new ContextualCommandViewModel(serverVM, this.Host.ActiveSection.RefreshCommand)
            {
                DisplayText = Strings.RefreshCommandDisplayText,
                Tooltip     = Strings.RefreshCommandTooltip,
                Icon        = new IconViewModel(KnownMonikers.Refresh)
            };

            var disconnectContextualCommand = new ContextualCommandViewModel(serverVM, this.Host.ActiveSection.DisconnectCommand)
            {
                DisplayText = Strings.DisconnectCommandDisplayText,
                Tooltip     = Strings.DisconnectCommandTooltip,
                Icon        = new IconViewModel(KnownMonikers.Disconnect)
            };

            var browseServerContextualCommand = new ContextualCommandViewModel(serverVM.Url.ToString(), this.Host.ActiveSection.BrowseToUrlCommand)
            {
                DisplayText = Strings.BrowseServerMenuItemDisplayText,
                Tooltip     = Strings.BrowserServerMenuItemTooltip,
                Icon        = new IconViewModel(KnownMonikers.OpenWebSite)
            };

            var toggleShowAllProjectsCommand = new ContextualCommandViewModel(serverVM, this.Host.ActiveSection.ToggleShowAllProjectsCommand)
            {
                Tooltip = Strings.ToggleShowAllProjectsCommandTooltip
            };

            toggleShowAllProjectsCommand.SetDynamicDisplayText(x =>
            {
                ServerViewModel ctx = x as ServerViewModel;
                Debug.Assert(ctx != null, "Unexpected fixed context for ToggleShowAllProjects context command");
                return(ctx?.ShowAllProjects ?? false ? Strings.HideUnboundProjectsCommandText : Strings.ShowAllProjectsCommandText);
            });

            serverVM.Commands.Add(refreshContextualCommand);
            serverVM.Commands.Add(disconnectContextualCommand);
            serverVM.Commands.Add(browseServerContextualCommand);
            serverVM.Commands.Add(toggleShowAllProjectsCommand);
        }
        private static void VerifyServerViewModelCommand(ServerViewModel serverVM, ICommand internalCommand, object fixedContext, bool hasIcon)
        {
            ContextualCommandViewModel commandVM = AssertCommandExists(serverVM.Commands, internalCommand);

            commandVM.DisplayText.Should().NotBeNull("DisplayText expected");
            commandVM.InternalFixedContext.Should().Be(fixedContext, "The fixed context is incorrect");
            if (hasIcon)
            {
                commandVM.Icon.Should().NotBeNull("Icon expected");
                commandVM.Icon.Moniker.Should().NotBeNull("Icon moniker expected");
            }
            else
            {
                commandVM.Icon.Should().BeNull("Icon not expected");
            }
        }
예제 #8
0
        private static void VerifyServerViewModelCommand(ServerViewModel serverVM, ICommand internalCommand, object fixedContext, bool hasIcon)
        {
            ContextualCommandViewModel commandVM = AssertCommandExists(serverVM.Commands, internalCommand);

            Assert.IsNotNull(commandVM.DisplayText, "DisplayText expected");
            Assert.AreEqual(fixedContext, commandVM.InternalFixedContext, "The fixed context is incorrect");
            if (hasIcon)
            {
                Assert.IsNotNull(commandVM.Icon, "Icon expected");
                Assert.IsNotNull(commandVM.Icon.Moniker, "Icon moniker expected");
            }
            else
            {
                Assert.IsNull(commandVM.Icon, "Icon not expected");
            }
        }
예제 #9
0
        private void OnBindingFinished(BindCommandArgs bindingArgs, bool isFinishedSuccessfully)
        {
            this.IsBindingInProgress = false;
            this.host.VisualStateManager.ClearBoundProject();

            if (isFinishedSuccessfully)
            {
                this.host.VisualStateManager.SetBoundProject(bindingArgs.Connection.ServerUri, bindingArgs.Connection.Organization?.Key, bindingArgs.ProjectKey);

                // The conflicts controller is only applicable in legacy connected mode
                // However, it is safe to call it regardless - in new connected mode it will
                // not return any conflicts.
                var conflictsController = this.host.GetService <IRuleSetConflictsController>();
                conflictsController.AssertLocalServiceIsNotNull();

                if (conflictsController.CheckForConflicts())
                {
                    // In some cases we will end up navigating to the solution explorer, this will make sure that
                    // we're back in team explorer to view the conflicts
                    this.host.GetMefService <ITeamExplorerController>()?.ShowSonarQubePage();
                }
                else
                {
                    VsShellUtils.ActivateSolutionExplorer(this.host);
                }
            }
            else
            {
                IUserNotification notifications = this.host.ActiveSection?.UserNotifications;
                if (notifications != null)
                {
                    // Create a command with a fixed argument with the help of ContextualCommandViewModel that creates proxy command for the contextual (fixed) instance and the passed in ICommand that expects it
                    var rebindCommandVM = new ContextualCommandViewModel(
                        bindingArgs,
                        new RelayCommand <BindCommandArgs>(this.OnBind, this.OnBindStatus));
                    notifications.ShowNotificationError(Strings.FailedToToBindSolution, NotificationIds.FailedToBindId, rebindCommandVM.Command);
                }
            }
        }
        private void SetServerProjectsVMCommands(ServerViewModel serverVM)
        {
            foreach (ProjectViewModel projectVM in serverVM.Projects)
            {
                projectVM.Commands.Clear();

                if (this.Host.ActiveSection == null)
                {
                    // Don't add command (which will be disabled).
                    continue;
                }

                var bindContextCommand = new ContextualCommandViewModel(projectVM,
                                                                        this.Host.ActiveSection.BindCommand,
                                                                        new BindCommandArgs(projectVM.Key, projectVM.ProjectName, serverVM.ConnectionInformation));
                bindContextCommand.SetDynamicDisplayText(x =>
                {
                    var ctx = x as ProjectViewModel;
                    Debug.Assert(ctx != null, "Unexpected fixed context for bind context command");
                    return(ctx?.IsBound ?? false ? Strings.SyncButtonText : Strings.BindButtonText);
                });
                bindContextCommand.SetDynamicIcon(x =>
                {
                    var ctx = x as ProjectViewModel;
                    Debug.Assert(ctx != null, "Unexpected fixed context for bind context command");
                    return(new IconViewModel(ctx?.IsBound ?? false ? KnownMonikers.Sync : KnownMonikers.Link));
                });

                var openProjectDashboardCommand = new ContextualCommandViewModel(projectVM, this.Host.ActiveSection.BrowseToProjectDashboardCommand)
                {
                    DisplayText = Strings.ViewInSonarQubeMenuItemDisplayText,
                    Tooltip     = Strings.ViewInSonarQubeMenuItemTooltip,
                    Icon        = new IconViewModel(KnownMonikers.OpenWebSite)
                };

                projectVM.Commands.Add(bindContextCommand);
                projectVM.Commands.Add(openProjectDashboardCommand);
            }
        }
        public void ContextualCommandViewModel_CommandInvocation()
        {
            // Arrange
            bool canExecute  = false;
            bool executed    = false;
            var  realCommand = new RelayCommand <object>(
                (state) =>
            {
                state.Should().Be(this);
                executed = true;
            },
                (state) =>
            {
                state.Should().Be(this);
                return(canExecute);
            });
            var testSubject = new ContextualCommandViewModel(this, realCommand);

            // Sanity
            testSubject.Command.Should().NotBeNull();
            testSubject.InternalRealCommand.Should().Be(realCommand);

            // Case 1: Can't execute
            canExecute = false;
            // Act
            testSubject.Command.CanExecute(null).Should().BeFalse("CanExecute wasn't called as expected");

            // Case 2: Can execute
            canExecute = true;

            // Act
            testSubject.Command.CanExecute(null).Should().BeTrue("CanExecute wasn't called as expected");

            // Case 3: Execute
            // Act
            testSubject.Command.Execute(null);
            executed.Should().BeTrue("Execute wasn't called as expected");
        }
        public void ContextualCommandViewModel_Icon()
        {
            // Arrange
            var context     = new object();
            var command     = new RelayCommand(() => { });
            var testSubject = new ContextualCommandViewModel(context, command);

            using (var tracker = new PropertyChangedTracker(testSubject))
            {
                // Case 1: null
                // Act + Assert
                testSubject.Icon.Should().BeNull("Expected icon to return null when not set");

                // Case 2: static
                var staticIcon = new IconViewModel(null);
                testSubject.Icon = staticIcon;
                // Act + Assert
                testSubject.Icon.Should().Be(staticIcon, "Unexpected static icon");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.Icon), 1);

                // Case 3: dynamic
                var dynamicIcon = new IconViewModel(null);
                var funcInvoked = false;
                Func <object, IconViewModel> func = x =>
                {
                    funcInvoked = true;
                    return(dynamicIcon);
                };
                testSubject.SetDynamicIcon(func);
                // Act + Assert
                testSubject.Icon.Should().Be(dynamicIcon, "Unexpected dynamic icon");
                funcInvoked.Should().BeTrue("Dynamic icon function  was not invoked");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.Icon), 2);
            }

            // Case 4: dynamic - null exception
            Exceptions.Expect <ArgumentNullException>(() => testSubject.SetDynamicIcon(null));
        }
        public void ContextualCommandViewModel_CommandInvocation()
        {
            // Setup
            bool canExecute  = false;
            bool executed    = false;
            var  realCommand = new RelayCommand <object>(
                (state) =>
            {
                Assert.AreEqual(this, state);
                executed = true;
            },
                (state) =>
            {
                Assert.AreEqual(this, state);
                return(canExecute);
            });
            var testSubject = new ContextualCommandViewModel(this, realCommand);

            // Sanity
            Assert.IsNotNull(testSubject.Command);
            Assert.AreSame(realCommand, testSubject.InternalRealCommand);

            // Case 1: Can't execute
            canExecute = false;
            // Act
            Assert.IsFalse(testSubject.Command.CanExecute(null), "CanExecute wasn't called as expected");

            // Case 2: Can execute
            canExecute = true;

            // Act
            Assert.IsTrue(testSubject.Command.CanExecute(null), "CanExecute wasn't called as expected");

            // Case 3: Execute
            // Act
            testSubject.Command.Execute(null);
            Assert.IsTrue(executed, "Execute wasn't called as expected");
        }
예제 #14
0
        public void ContextualCommandsCollection_HasCommands_ChangedOnCollectionChange()
        {
            // Arrange
            var testSubject = new ContextualCommandsCollection();
            int hasCommandsChangedCounter = 0;

            ((INotifyPropertyChanged)testSubject).PropertyChanged += (o, e) =>
            {
                if (e.PropertyName == "HasCommands")
                {
                    hasCommandsChangedCounter++;
                }
            };

            // Case 1: Add command
            var cmd1 = new ContextualCommandViewModel(this, new RelayCommand(() => { }));
            var cmd2 = new ContextualCommandViewModel(this, new RelayCommand(() => { }));

            // Act
            testSubject.Add(cmd1);
            testSubject.Add(cmd2);

            // Assert
            hasCommandsChangedCounter.Should().Be(2, "Adding a command should update HasCommands");

            // Case 2: Remove command
            // Act
            testSubject.Remove(cmd1);
            // Assert
            hasCommandsChangedCounter.Should().Be(3, "Adding a command should update HasCommands");

            // Case 3: Update command
            // Act
            testSubject[0] = cmd1;
            // Assert
            hasCommandsChangedCounter.Should().Be(4, "Adding a command should update HasCommands");
        }
        public void ContextualCommandViewModel_DisplayText()
        {
            // Arrange
            var context     = new object();
            var command     = new RelayCommand(() => { });
            var testSubject = new ContextualCommandViewModel(context, command);

            using (var tracker = new PropertyChangedTracker(testSubject))
            {
                // Case 1: null
                // Act + Assert
                testSubject.DisplayText.Should().BeNull("Expected display text to return null when not set");

                // Case 2: static
                testSubject.DisplayText = "foobar9000";
                // Act + Assert
                testSubject.DisplayText.Should().Be("foobar9000", "Unexpected static display text");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.DisplayText), 1);

                // Case 3: dynamic
                var funcInvoked            = false;
                Func <object, string> func = x =>
                {
                    funcInvoked = true;
                    return("1234");
                };
                testSubject.SetDynamicDisplayText(func);
                // Act + Assert
                testSubject.DisplayText.Should().Be("1234", "Unexpected dynamic display text");
                funcInvoked.Should().BeTrue("Dynamic display text function was not invoked");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.DisplayText), 2);
            }

            // Case 4: dynamic - null exception
            Exceptions.Expect <ArgumentNullException>(() => testSubject.SetDynamicDisplayText(null));
        }