public void PackageCommands()
        {
            using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices())
            {
                IVsPackage package = null;
                try
                {
                    // Create and site the package.
                    package = new PythonConsolePackage() as IVsPackage;
                    Assert.IsNotNull(package);
                    int hr = package.SetSite(provider);
                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(hr));

                    // Get the command target from the package.
                    IOleCommandTarget target = package as IOleCommandTarget;
                    Assert.IsNotNull(target);

                    CommandTargetHelper helper = new CommandTargetHelper(target);
                    uint flags;
                    Assert.IsTrue(helper.IsCommandSupported(GuidList.guidIronPythonConsoleCmdSet, PkgCmdIDList.cmdidIronPythonConsole, out flags));
                    Assert.IsTrue(0 != ((uint)OLECMDF.OLECMDF_SUPPORTED & flags));
                    Assert.IsTrue(0 != ((uint)OLECMDF.OLECMDF_ENABLED & flags));
                    Assert.IsTrue(0 == ((uint)OLECMDF.OLECMDF_INVISIBLE & flags));
                }
                finally
                {
                    if (null != package)
                    {
                        package.SetSite(null);
                        package.Close();
                    }
                }
            }
        }
        public void VerifyCommandFilter()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Define the mock object for the text view.
                BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance();
                textViewMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextView).FullName, "AddCommandFilter"),
                    new EventHandler<CallbackArgs>(AddCommandFilterCallback));
                mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock);

                // Create a command target that handles some random command
                OleMenuCommandService commandService = new OleMenuCommandService(provider);
                Guid newCommandGroup = Guid.NewGuid();
                uint newCommandId = 42;
                CommandID id = new CommandID(newCommandGroup, (int)newCommandId);
                OleMenuCommand cmd = new OleMenuCommand(null, id);
                commandService.AddCommand(cmd);
                textViewMock["OriginalFilter"] = (IOleCommandTarget)commandService;

                // Add the local registry to the list of services.
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create the window.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    Assert.IsNotNull(windowPane);

                    // Verify that the command specific to the text view are not handled yet.
                    CommandTargetHelper commandHelper = new CommandTargetHelper((IOleCommandTarget)windowPane);
                    uint flags;
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN,
                                        out flags));
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP,
                                        out flags));
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN,
                                        out flags));
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU,
                                        out flags));
                    // Verify that also the command that we have defined here is not supported.
                    Assert.IsFalse(commandHelper.IsCommandSupported(newCommandGroup, newCommandId, out flags));

                    // Call the CreatePaneWindow method that will force the creation of the text view.
                    IntPtr newHwnd;
                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd)));

                    // Now we have to set the frame property on the ToolWindowFrame because
                    // this will cause the execution of OnToolWindowCreated.
                    windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance();

                    // Now the command filter should be set for the text view
                    Assert.IsNotNull(textViewMock["CommandFilter"]);
                    // The command target for the window pane should also be able to support
                    // the text view specific command that we have installed.
                    // Verify only two commands that are always supported
                    Assert.IsTrue(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN,
                                        out flags));
                    Assert.IsTrue(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL,
                                        out flags));
                    Assert.IsTrue(commandHelper.IsCommandSupported(
                                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                        (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU,
                                        out flags));
                    // Verify that also the commands supported by the original command target are
                    // supported by the new one.
                    Assert.IsTrue(commandHelper.IsCommandSupported(newCommandGroup, newCommandId, out flags));
                }
            }
        }
        public void PackageCommands()
        {
            using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices())
            {
                IVsPackage package = null;
                try
                {
                    // Create and site the package.
                    package = new PythonConsolePackage() as IVsPackage;
                    Assert.IsNotNull(package);
                    int hr = package.SetSite(provider);
                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(hr));

                    // Get the command target from the package.
                    IOleCommandTarget target = package as IOleCommandTarget;
                    Assert.IsNotNull(target);

                    CommandTargetHelper helper = new CommandTargetHelper(target);
                    uint flags;
                    Assert.IsTrue(helper.IsCommandSupported(GuidList.guidIronPythonConsoleCmdSet, PkgCmdIDList.cmdidIronPythonConsole, out flags));
                    Assert.IsTrue(0 != ((uint)OLECMDF.OLECMDF_SUPPORTED & flags));
                    Assert.IsTrue(0 != ((uint)OLECMDF.OLECMDF_ENABLED & flags));
                    Assert.IsTrue(0 == ((uint)OLECMDF.OLECMDF_INVISIBLE & flags));
                }
                finally
                {
                    if (null != package)
                    {
                        package.SetSite(null);
                        package.Close();
                    }
                }
            }
        }
Esempio n. 4
0
        public void VerifyCommandFilter()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock          textLinesMock     = MockFactories.TextBufferFactory.GetInstance();
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Define the mock object for the text view.
                BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance();
                textViewMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextView).FullName, "AddCommandFilter"),
                    new EventHandler <CallbackArgs>(AddCommandFilterCallback));
                mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock);

                // Create a command target that handles some random command
                OleMenuCommandService commandService = new OleMenuCommandService(provider);
                Guid           newCommandGroup       = Guid.NewGuid();
                uint           newCommandId          = 42;
                CommandID      id  = new CommandID(newCommandGroup, (int)newCommandId);
                OleMenuCommand cmd = new OleMenuCommand(null, id);
                commandService.AddCommand(cmd);
                textViewMock["OriginalFilter"] = (IOleCommandTarget)commandService;

                // Add the local registry to the list of services.
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create the window.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    Assert.IsNotNull(windowPane);

                    // Verify that the command specific to the text view are not handled yet.
                    CommandTargetHelper commandHelper = new CommandTargetHelper((IOleCommandTarget)windowPane);
                    uint flags;
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                       typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN,
                                       out flags));
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                       typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP,
                                       out flags));
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                       typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN,
                                       out flags));
                    Assert.IsFalse(commandHelper.IsCommandSupported(
                                       typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU,
                                       out flags));
                    // Verify that also the command that we have defined here is not supported.
                    Assert.IsFalse(commandHelper.IsCommandSupported(newCommandGroup, newCommandId, out flags));

                    // Call the CreatePaneWindow method that will force the creation of the text view.
                    IntPtr newHwnd;
                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                                      ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd)));

                    // Now we have to set the frame property on the ToolWindowFrame because
                    // this will cause the execution of OnToolWindowCreated.
                    windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance();

                    // Now the command filter should be set for the text view
                    Assert.IsNotNull(textViewMock["CommandFilter"]);
                    // The command target for the window pane should also be able to support
                    // the text view specific command that we have installed.
                    // Verify only two commands that are always supported
                    Assert.IsTrue(commandHelper.IsCommandSupported(
                                      typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                      (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN,
                                      out flags));
                    Assert.IsTrue(commandHelper.IsCommandSupported(
                                      typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                      (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL,
                                      out flags));
                    Assert.IsTrue(commandHelper.IsCommandSupported(
                                      typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                      (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU,
                                      out flags));
                    // Verify that also the commands supported by the original command target are
                    // supported by the new one.
                    Assert.IsTrue(commandHelper.IsCommandSupported(newCommandGroup, newCommandId, out flags));
                }
            }
        }
        public void InputPositionCommand()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                BaseMock lineMarkerMock = (BaseMock)textLinesMock["LineMarker"];

                // Add the text buffer to the local registry
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Define the mock object for the text view and add it to the local registry.
                BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance();
                mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock);

                // Add the local registry to the list of services.
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create the console.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    // Call the CreatePaneWindow method that will force the creation of the text view.
                    IntPtr newHwnd;
                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd)));

                    // Now we have to set the frame property on the ToolWindowFrame because
                    // this will cause the execution of OnToolWindowCreated and this will add the
                    // command handling for the return key.
                    windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance();

                    // Make sure that the text marker is created.
                    CommandWindowHelper.EnsureConsoleTextMarker(windowPane);

                    // Reset the span of the marker.
                    TextSpan markerSpan = new TextSpan();
                    markerSpan.iStartLine = 0;
                    markerSpan.iStartIndex = 0;
                    markerSpan.iEndLine = 4;
                    markerSpan.iEndIndex = 3;
                    lineMarkerMock["Span"] = markerSpan;

                    // Create the helper class to handle the command target implemented
                    // by the console.
                    CommandTargetHelper helper = new CommandTargetHelper((IOleCommandTarget)windowPane);

                    // Simulate the fact that the cursor is after the end of the marker.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 7 });
                    // Verify that the commands are supported.
                    uint flags;
                    Assert.IsTrue(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP,
                            out flags));
                    Assert.IsTrue(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN,
                            out flags));
                    Assert.IsTrue(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT,
                            out flags));

                    // Simulate the cursor on the last line, but before the end of the marker.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 2 });
                    Assert.IsFalse(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP,
                            out flags));
                    Assert.IsFalse(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN,
                            out flags));
                    Assert.IsFalse(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT,
                            out flags));

                    // Simulate the cursor on a line before the end of the marker.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 1, 7 });
                    Assert.IsFalse(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP,
                            out flags));
                    Assert.IsFalse(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN,
                            out flags));
                    Assert.IsFalse(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT,
                            out flags));

                    // Simulate the cursor on a line after the last.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 5, 7 });
                    Assert.IsTrue(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP,
                            out flags));
                    Assert.IsTrue(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN,
                            out flags));
                    Assert.IsTrue(
                        helper.IsCommandSupported(
                            typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                            (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT,
                            out flags));
                }
            }
        }