public void ContextMenu()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();

                // 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 a mock UIShell.
                BaseMock uiShellMock = MockFactories.UIShellFactory.GetInstance();
                provider.AddService(typeof(SVsUIShell), uiShellMock, 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 handlers to the console window.
                    windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance();

                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);

                    // Verify that the "ShowContextMenu" command handler calls the
                    // ShowContextMenu method of IVsUIShell.
                    uiShellMock.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IVsUIShell).FullName, "ShowContextMenu"));
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU);
                    Assert.IsTrue(1 == uiShellMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsUIShell).FullName, "ShowContextMenu")));
                }
            }
        }
        public void ReadOnlyRegionAfterWrite()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();

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

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

                // Create the console window.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    // Get the stream from the window pane.
                    System.IO.Stream consoleStream = CommandWindowHelper.ConsoleStream(windowPane);
                    Assert.IsNotNull(consoleStream);

                    // Set a return value for GetLastLineIndex
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                        new object[] { 0, 12, 35 });

                    // Write some text on the stream.
                    System.IO.StreamWriter writer = new System.IO.StreamWriter(consoleStream);
                    writer.Write("");
                    writer.Flush();

                    // Verify that the ResetSpan method for the text marker was called and that
                    // the span is set to cover all the current buffer.
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    Assert.IsTrue(1 == markerMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLineMarker).FullName, "ResetSpan")));
                    TextSpan span = (TextSpan)markerMock["Span"];
                    Assert.IsTrue(0 == span.iStartLine);
                    Assert.IsTrue(0 == span.iStartIndex);
                    Assert.IsTrue(12 == span.iEndLine);
                    Assert.IsTrue(35 == span.iEndIndex);

                    // Change the end point of the buffer and try again.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                        new object[] { 0, 15, 3 });
                    writer.Write("abc");
                    writer.Flush();
                    Assert.IsTrue(2 == markerMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLineMarker).FullName, "ResetSpan")));
                    span = (TextSpan)markerMock["Span"];
                    Assert.IsTrue(0 == span.iStartLine);
                    Assert.IsTrue(0 == span.iStartIndex);
                    Assert.IsTrue(15 == span.iEndLine);
                    Assert.IsTrue(3 == span.iEndIndex);
                }
            }
        }
        public void EngineStreams()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                textLinesMock["Text"] = "";
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create a mock engine provider.
                BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance();
                // Create a mock engine.
                BaseMock mockEngine = MockFactories.CreateStandardEngine();
                // Add the callbacks for the setter methods of stderr and stdout
                mockEngine.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IEngine).FullName, "set_StdErr"),
                    new EventHandler<CallbackArgs>(SetEngineStdErr));
                mockEngine.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IEngine).FullName, "set_StdOut"),
                    new EventHandler<CallbackArgs>(SetEngineStdOut));
                // Set this engine as the one returned from the GetSharedEngine of the engine provider.
                mockEngineProvider.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"),
                    new object[] { (IEngine)mockEngine });
                // Add the engine provider to the list of the services.
                provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false);

                // Create the console window.
                using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    IVsWindowPane windowPane = disposableObject as IVsWindowPane;
                    Assert.IsNotNull(windowPane);
                    Assert.IsNotNull(mockEngine["StdErr"]);
                    Assert.IsNotNull(mockEngine["StdOut"]);

                    // Set the callback for the text buffer.
                    textLinesMock.AddMethodCallback(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"),
                        new EventHandler<CallbackArgs>(ReplaceLinesCallback));

                    // Verify that the standard error stream is associated with the text buffer.
                    System.IO.Stream stream = (System.IO.Stream)mockEngine["StdErr"];
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
                    {
                        writer.Write("Test String");
                        writer.Flush();
                        Assert.IsTrue((string)textLinesMock["Text"] == "Test String");
                        textLinesMock["Text"] = "";
                    }

                    // Verify the standard output.
                    stream = (System.IO.Stream)mockEngine["StdOut"];
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
                    {
                        writer.Write("Test String");
                        writer.Flush();
                        Assert.IsTrue((string)textLinesMock["Text"] == "Test String");
                        textLinesMock["Text"] = "";
                    }
                }
            }
        }
        public void EngineInitialization()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create a mock engine provider.
                BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance();
                // Create a mock engine.
                BaseMock mockEngine = MockFactories.CreateStandardEngine();
                // Set this engine as the one returned from the GetSharedEngine of the engine provider.
                mockEngineProvider.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"),
                    new object[] { (IEngine)mockEngine });
                // Add the engine provider to the list of the services.
                provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false);

                // Create the console window
                using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    IVsWindowPane windowPane = disposableObject as IVsWindowPane;
                    Assert.IsNotNull(windowPane);

                    // Verify that the shared engine was get.
                    Assert.IsTrue(1 == mockEngineProvider.FunctionCalls(string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine")));
                    Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine), "set_StdErr")));
                    Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine), "set_StdOut")));
                }
            }
        }
        public void OnClearPaneOnlyOneLine()
        {
            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)
                {
                    // 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 = 0;
                    markerSpan.iEndIndex = 3;
                    lineMarkerMock["Span"] = markerSpan;

                    // 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();

                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);

                    // Set the last index of the buffer before the end of the line marker.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                        new object[] { 0, 0, 2 });
                    // Reset the counters of function calls for the text buffer.
                    textLinesMock.ResetAllFunctionCalls();
                    // Execute the "Clear" command.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd97CmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.ClearPane);
                    // Verify that ReplaceLines wan never called.
                    Assert.IsTrue(0 == textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines")));

                    // Set the last index of the buffer after the end of the line marker.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                        new object[] { 0, 2, 1 });
                    textLinesMock.AddMethodCallback(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"),
                        new EventHandler<CallbackArgs>(ReplaceLinesCallback_ClearPane));
                    textLinesMock["ReplaceRegion"] = new int[] { 0, 3, 2, 1 };
                    textLinesMock["CallCount"] = 0;
                    // Reset the counters of function calls for the text buffer.
                    textLinesMock.ResetAllFunctionCalls();
                    // Execute the "Clear" command.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd97CmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.ClearPane);
                    // Verify that ReplaceLines wan called only once.
                    Assert.IsTrue(1 == textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines")));
                }
            }
        }
        public void ConsoleTextOfLineNoMarker()
        {
            string testString = "Test";
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();
                textLinesMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"),
                    new EventHandler<CallbackArgs>(GetLineTextCallbackForConsoleTextOfLine));
                textLinesMock["LineText"] = testString;
                textLinesMock["ExpectedLine"] = 1;
                textLinesMock["ExpectedStart"] = 0;
                textLinesMock["ExpectedEnd"] = 10;

                // Create a new local registry class.
                LocalRegistryMock mockRegistry = new LocalRegistryMock();
                // Add the text buffer to the list of the classes that local registry can create.
                mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Add the local registry to the service provider.
                provider.AddService(typeof(SLocalRegistry), mockRegistry, false);

                // Create the console.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    IConsoleText consoleText = windowPane as IConsoleText;
                    Assert.IsNull(consoleText.TextOfLine(1, -1, true));
                    Assert.IsNull(consoleText.TextOfLine(1, -1, false));
                    string text = consoleText.TextOfLine(1, 10, false);
                    Assert.IsTrue(testString == text);
                }
            }
        }
        public void WindowConstructor()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock object for the text buffer.
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();
                // Create a new local registry class.
                LocalRegistryMock mockRegistry = new LocalRegistryMock();
                // Add the text buffer to the list of the classes that local registry can create.
                mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);
                provider.AddService(typeof(SLocalRegistry), mockRegistry, false);

                // Now create the object and verify that the constructor sets the site for the text buffer.
                using (IDisposable consoleObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    Assert.IsNotNull(consoleObject);
                    Assert.IsTrue(0 < textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite")));
                }
            }
        }
        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 VerifyOnBeforeMoveLeft()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();

                // 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);
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];

                    // Create a OleMenuCommand to use to call OnBeforeHistory.
                    OleMenuCommand cmd = new OleMenuCommand(new EventHandler(EmptyMenuCallback), new CommandID(Guid.Empty, 0));

                    // Simulate the fact that the cursor is on the last line of the buffer and after the
                    // end of the prompt.
                    cmd.Supported = true;
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"),
                        new object[] { 0, 5 });
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 7 });
                    TextSpan span = new TextSpan();
                    span.iEndIndex = 4;
                    span.iEndLine = 3;
                    markerMock["Span"] = span;
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsFalse(cmd.Supported);

                    // Simulate the cursor over the prompt.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 3 });
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsTrue(cmd.Supported);

                    // Simulate the cursor right after the prompt.
                    cmd.Supported = false;
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 4 });
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsTrue(cmd.Supported);

                    // Simulate the cursor on a line before the last.
                    cmd.Supported = true;
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 3, 7 });
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsFalse(cmd.Supported);

                    // Simulate the cursor on a line before the last but over the prompt.
                    cmd.Supported = true;
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 3, 2 });
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsFalse(cmd.Supported);

                    // Simulate the cursor on a line after the last.
                    cmd.Supported = true;
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 5, 7 });
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsFalse(cmd.Supported);

                    // Simulate the cursor on a line after the last, but over the prompt.
                    cmd.Supported = true;
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 5, 0 });
                    CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd);
                    Assert.IsFalse(cmd.Supported);
                }
            }
        }
        public void VerifyHistoryOneElement()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();

                // 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
                    // commands handling functions.
                    windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance();

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

                    // Create the command target helper.
                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);

                    // Add an element to the history executing a command.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"),
                        new object[] { 0, 3 });
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 2, 4 });
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLengthOfLine"),
                        new object[] { 0, 3, 10 });
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    TextSpan span = new TextSpan();
                    span.iEndLine = 2;
                    span.iEndIndex = 4;
                    markerMock["Span"] = span;
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"),
                        new object[] { 0, 2, 4, 2, 10, "Line 1" });
                    // Execute the OnReturn handler.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN);

                    // Now there should be one element in the history buffer.
                    // Verify that DOWN key does nothing.
                    markerMock.ResetAllFunctionCalls();
                    textLinesMock.ResetAllFunctionCalls();
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN);
                    Assert.IsTrue(0 == markerMock.TotalCallsAllFunctions());
                    Assert.IsTrue(0 == textLinesMock.TotalCallsAllFunctions());

                    // The UP key should force the "Line 1" text in the last line of the text buffer.
                    textLinesMock.AddMethodCallback(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"),
                        new EventHandler<CallbackArgs>(ReplaceLinesCallback));
                    textLinesMock["Text"] = "";
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP);
                    Assert.IsTrue(1 == textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines")));
                    Assert.IsTrue("Line 1" == (string)textLinesMock["Text"]);
                }
            }
        }
        public void VerifyHistoryEmpty()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();

                // 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
                    // commands handling functions.
                    windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance();

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

                    // The history should be empty, so no function on the text buffer or text marker
                    // should be called. Reset all the function calls on these objects to verify.
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    markerMock.ResetAllFunctionCalls();
                    textLinesMock.ResetAllFunctionCalls();

                    // Create the command target helper.
                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);

                    // Call the command handler for the UP arrow.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP);
                    Assert.IsTrue(0 == markerMock.TotalCallsAllFunctions());
                    Assert.IsTrue(0 == textLinesMock.TotalCallsAllFunctions());

                    // Call the command handler for the DOWN arrow.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN);
                    Assert.IsTrue(0 == markerMock.TotalCallsAllFunctions());
                    Assert.IsTrue(0 == textLinesMock.TotalCallsAllFunctions());
                }
            }
        }
        public void SupportCommandOnInputPositionVerifySender()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();

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

                // 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)
                {
                    // Verify OnBeforeHistory can handle a null sender
                    CommandWindowHelper.ExecuteSupportCommandOnInputPosition(windowPane, null);
                    // Verify OnBeforeHistory can handle a sender of unexpected type.
                    CommandWindowHelper.ExecuteSupportCommandOnInputPosition(windowPane, "");
                }
            }
        }
        public void OnShiftHomeTest()
        {
            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();
                textViewMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextView).FullName, "SetSelection"),
                    new EventHandler<CallbackArgs>(SetSelectionCallback));
                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)
                {
                    // 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;

                    // 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();

                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);

                    // Set the cursor after the end of the marker, but on the same line.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 7 });
                    helper.ExecCommand(
                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                        (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT);
                    Assert.IsTrue(4 == (int)textViewMock["StartLine"]);
                    Assert.IsTrue(4 == (int)textViewMock["EndLine"]);
                    Assert.IsTrue(7 == (int)textViewMock["StartColumn"]);
                    Assert.IsTrue(3 == (int)textViewMock["EndColumn"]);

                    // Set the cursor before the end of the marker, but on the same line.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 4, 2 });
                    helper.ExecCommand(
                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                        (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT);
                    Assert.IsTrue(4 == (int)textViewMock["StartLine"]);
                    Assert.IsTrue(4 == (int)textViewMock["EndLine"]);
                    Assert.IsTrue(2 == (int)textViewMock["StartColumn"]);
                    Assert.IsTrue(0 == (int)textViewMock["EndColumn"]);

                    // Set the cursor before the end of the marker, on a different line.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 2, 8 });
                    helper.ExecCommand(
                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                        (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT);
                    Assert.IsTrue(2 == (int)textViewMock["StartLine"]);
                    Assert.IsTrue(2 == (int)textViewMock["EndLine"]);
                    Assert.IsTrue(8 == (int)textViewMock["StartColumn"]);
                    Assert.IsTrue(0 == (int)textViewMock["EndColumn"]);

                    // Set the cursor after the end of the marker, on a different line.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 9, 12 });
                    helper.ExecCommand(
                        typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                        (int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL_EXT);
                    Assert.IsTrue(9 == (int)textViewMock["StartLine"]);
                    Assert.IsTrue(9 == (int)textViewMock["EndLine"]);
                    Assert.IsTrue(12 == (int)textViewMock["StartColumn"]);
                    Assert.IsTrue(0 == (int)textViewMock["EndColumn"]);
                }
            }
        }
        public void StandardConstructor()
        {
            using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices())
            {
                IVsPackage package = null;
                try
                {
                    // Create a mock object for the text buffer.
                    BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();
                    // Create a new local registry class.
                    LocalRegistryMock mockRegistry = new LocalRegistryMock();
                    // Add the text buffer to the list of the classes that local registry can create.
                    mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);
                    provider.AddService(typeof(SLocalRegistry), mockRegistry, false);

                    // Now create a package object and site it.
                    package = new PythonConsolePackage() as IVsPackage;
                    package.SetSite(provider);

                    // Create a console window using the standard constructor and verify that the
                    // text buffer is created and sited.
                    using (IDisposable consoleObject = CommandWindowHelper.CreateConsoleWindow() as IDisposable)
                    {
                        Assert.IsTrue(0 < textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite")));
                    }
                }
                finally
                {
                    if (null != package)
                    {
                        package.SetSite(null);
                        package.Close();
                    }
                }
            }
        }
        public void VerifyOnHome()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();

                // 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();
                textViewMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextView).FullName, "SetCaretPos"),
                    new EventHandler<CallbackArgs>(SetCaretPosCallback));
                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)
                {
                    // Make sure that the text marker is created.
                    CommandWindowHelper.EnsureConsoleTextMarker(windowPane);

                    // 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();

                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);

                    // Simulate the fact that the cursor is on the last line of the buffer.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"),
                        new object[] { 0, 6 });
                    // Simulate a cursor 3 chars long.
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    TextSpan span = new TextSpan();
                    span.iEndLine = 5;
                    span.iEndIndex = 3;
                    markerMock["Span"] = span;
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 5, 7 });
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL);
                    Assert.IsTrue(5 == (int)textViewMock["CaretLine"]);
                    Assert.IsTrue(3 == (int)textViewMock["CaretColumn"]);

                    // Simulate the fact that the cursor is before last line of the buffer.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"),
                        new object[] { 0, 6 });
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 3, 7 });
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL);
                    Assert.IsTrue(3 == (int)textViewMock["CaretLine"]);
                    Assert.IsTrue(0 == (int)textViewMock["CaretColumn"]);

                    // Simulate the fact that the cursor is after last line of the buffer.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"),
                        new object[] { 0, 6 });
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 8, 7 });
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL);
                    Assert.IsTrue(8 == (int)textViewMock["CaretLine"]);
                    Assert.IsTrue(0 == (int)textViewMock["CaretColumn"]);
                }
            }
        }
        public void TextViewCreation()
        {
            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(IObjectWithSite).FullName, "SetSite"),
                                               new EventHandler<CallbackArgs>(TextViewSetSiteCallback));
                textViewMock.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsTextView).FullName, "Initialize"),
                                               new EventHandler<CallbackArgs>(TextViewInitializeCallback));
                mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock);

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

                // Create the tool window.
                using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    IVsWindowPane windowPane = disposableObject as IVsWindowPane;
                    Assert.IsNotNull(windowPane);

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

                    // Verify that the text view was used as expected.
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite), "SetSite")));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextView), "Initialize")));
                }
            }
        }
        public void VerifyOnReturn()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                textLinesMock.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"),
                    new object[] { 0, 0, 4 });
                textLinesMock.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLengthOfLine"),
                    new object[] { 0, 0, 13 });
                textLinesMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"),
                    new EventHandler<CallbackArgs>(GetLineTextCallback));
                textLinesMock.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"),
                    new object[] { 0, 13 });

                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Define the mock object for the text view.
                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 a mock engine provider.
                BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance();
                // Create a mock engine.
                BaseMock mockEngine = MockFactories.CreateStandardEngine();
                // Set this engine as the one returned from the GetSharedEngine of the engine provider.
                mockEngineProvider.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"),
                    new object[] { (IEngine)mockEngine });
                // Set the callback function for the ExecuteToConsole method of the engine.
                mockEngine.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole"),
                    new EventHandler<CallbackArgs>(ExecuteToConsoleCallback));
                // Add the engine provider to the list of the services.
                provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false);

                // Create the console window.
                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)));

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

                    // 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();

                    // Simulate the cursor on a line different from the last one.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 5, 8 });

                    // Execute the command handler for the RETURN key.
                    CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget);
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN);

                    // In this case nothing should happen because we are not on the input line.
                    Assert.IsTrue(0 == CommandWindowHelper.LinesInInputBuffer(windowPane));

                    // Now simulate the cursor on the input line.
                    textViewMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"),
                        new object[] { 0, 12, 1 });

                    // Make sure that the mock engine can execute the command.
                    mockEngine.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput"),
                        new object[] { true });
                    // Execute the command.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN);

                    // The input buffer should not contain any text because the engine should have
                    // executed it.
                    Assert.AreEqual<int>(0, CommandWindowHelper.LinesInInputBuffer(windowPane));
                    Assert.AreEqual<string>("Test Line", (string)mockEngine["ExecutedCommand"]);

                    // Now change the length of the line so that it is shorter than the
                    // console's prompt.
                    textLinesMock.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLengthOfLine"),
                        new object[] { 0, 0, 3 });

                    // Reset the count of the calls to GetLineText so that we can verify
                    // if it is called.
                    textLinesMock.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"));
                    // Do the same for the ParseInteractiveInput method of the engine.
                    mockEngine.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput"));
                    // Simulate a partial statment.
                    mockEngine.AddMethodReturnValues(
                        string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput"),
                        new object[] { false });

                    // Execute again the command handler.
                    helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID,
                                       (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN);

                    // Verify that GetLineText was not called.
                    Assert.IsTrue(0 == textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText")));
                    // Verify that the engine was not called to run an interactive command.
                    Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput")));
                    // Verify that the console's buffer contains an empty string.
                    Assert.IsTrue(0 == CommandWindowHelper.LinesInInputBuffer(windowPane));
                }
            }
        }
        public void ViewCreationWithLanguage()
        {
            using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices())
            {
                // Create a mock text buffer for the console.
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();
                // The buffer have to handle a few of connection points in order to enable the
                // creation of a Source object from the language service.
                ConnectionPointHelper.AddConnectionPointsToContainer(
                    textLinesMock,
                    new Type[] { typeof(IVsFinalTextChangeCommitEvents), typeof(IVsTextLinesEvents), typeof(IVsUserDataEvents) });

                // Create the local registry mock and add the text buffer to it.
                LocalRegistryMock mockLocalRegistry = new LocalRegistryMock();
                mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Define the mock object for the text view.
                BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance();
                // Create the connection point for IVsTextViewEvents (needed for the language service).
                ConnectionPointHelper.AddConnectionPointsToContainer(textViewMock, new Type[] { typeof(IVsTextViewEvents) });

                // Add the text view to the local registry.
                mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock);

                MockPackage package = new MockPackage();
                ((IVsPackage)package).SetSite(provider);
                provider.AddService(typeof(Microsoft.VisualStudio.Shell.Package), package, true);

                // Create the language service and add it to the list of services.
                PythonLanguage language = new MockLanguage();
                provider.AddService(typeof(PythonLanguage), language, true);
                language.SetSite(provider);

                // We need to add a method tip window to the local registry in order to create
                // a Source object.
                IVsMethodTipWindow methodTip = MockFactories.MethodTipFactory.GetInstance() as IVsMethodTipWindow;
                mockLocalRegistry.AddClass(typeof(VsMethodTipWindowClass), methodTip);

                // Create a mock expansion manager that is needed for the language service.
                BaseMock expansionManager = MockFactories.ExpansionManagerFactory.GetInstance();
                ConnectionPointHelper.AddConnectionPointsToContainer(expansionManager, new Type[] { typeof(IVsExpansionEvents) });
                Assembly asm = typeof(Microsoft.VisualStudio.Package.LanguageService).Assembly;
                Type expMgrType = asm.GetType("Microsoft.VisualStudio.Package.SVsExpansionManager");
                provider.AddService(expMgrType, expansionManager, false);

                // Add the local registry to the list of services.
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    Assert.IsNotNull(windowPane);

                    // 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)));

                    // Verify that the language service contains a special view for this text view.
                    FieldInfo specialSourcesField = typeof(PythonLanguage).GetField("specialSources", BindingFlags.Instance | BindingFlags.NonPublic);
                    Assert.IsNotNull(specialSourcesField);
                    Dictionary<IVsTextView, PythonSource> specialSources =
                        (Dictionary<IVsTextView, PythonSource>)specialSourcesField.GetValue(language);
                    PythonSource source;
                    Assert.IsTrue(specialSources.TryGetValue(textViewMock as IVsTextView, out source));
                    Assert.IsNotNull(source);
                    // Set ColorState to null so that Dispose will not call Marshal.ReleaseComObject on it.
                    source.ColorState = null;
                }
            }
        }
        public void ConsoleCreation()
        {
            using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices())
            {
                // In order to create a console window we have to add the text buffer to the
                // local registry.

                // Create a mock object for the text buffer.
                BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance();
                // Create a new local registry class.
                LocalRegistryMock mockRegistry = new LocalRegistryMock();
                // Add the text buffer to the list of the classes that local registry can create.
                mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Define the mock object for the text view.
                BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance();
                mockRegistry.AddClass(typeof(VsTextViewClass), textViewMock);

                // Add the local registry mock to the service provider.
                provider.AddService(typeof(SLocalRegistry), mockRegistry, false);

                // Create a mock UIShell to be able to create the tool window.
                BaseMock uiShell = MockFactories.UIShellFactory.GetInstance();
                uiShell["Frame"] = MockFactories.WindowFrameFactory.GetInstance() as IVsWindowFrame;
                uiShell.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsUIShell), "CreateToolWindow"),
                    new EventHandler<CallbackArgs>(CreateToolwindowCallback));
                provider.AddService(typeof(SVsUIShell), uiShell, false);

                IVsPackage package = null;
                try
                {
                    // Create the package.
                    package = new PythonConsolePackage() as IVsPackage;
                    Assert.IsNotNull(package);

                    // Make sure that the static variable about the global service provider is null;
                    FieldInfo globalProvider = typeof(Microsoft.VisualStudio.Shell.Package).GetField("_globalProvider", BindingFlags.Static | BindingFlags.NonPublic);
                    globalProvider.SetValue(null, null);

                    // Site it.
                    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);
                    helper.ExecCommand(GuidList.guidIronPythonConsoleCmdSet, PkgCmdIDList.cmdidIronPythonConsole);
                }
                finally
                {
                    if (null != package)
                    {
                        package.SetSite(null);
                        package.Close();
                    }
                }
            }
        }
        public void WindowPaneImplementation()
        {
            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);
                BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance();
                mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock);
                provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false);

                // Create the tool window.
                using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable)
                {
                    IVsWindowPane windowPane = disposableObject as IVsWindowPane;
                    Assert.IsNotNull(windowPane);

                    // Now call the IVsWindowPane's methods and check that they are redirect to
                    // the implementation provided by the text view.
                    IntPtr newHwnd;
                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd)));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "CreatePaneWindow")));

                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.GetDefaultSize(null)));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "GetDefaultSize")));

                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.LoadViewState(null)));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "LoadViewState")));

                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.SaveViewState(null)));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "SaveViewState")));

                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.SetSite(null)));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "SetSite")));

                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.TranslateAccelerator(null)));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "TranslateAccelerator")));

                    Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(
                        windowPane.ClosePane()));
                    Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "ClosePane")));
                }
                // Verify that the text view is closed after Dispose is called on the window pane.
                Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextView).FullName, "CloseView")));
            }
        }
        public void ConsoleTextOfLineWithMarker()
        {
            string testString1 = "Test 1";
            string testString2 = "Test 2";
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                BaseMock textLinesMock = MockFactories.CreateBufferWithMarker();
                textLinesMock.AddMethodCallback(
                    string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"),
                    new EventHandler<CallbackArgs>(GetLineTextCallbackForConsoleTextOfLine));

                // Create a new local registry class.
                LocalRegistryMock mockRegistry = new LocalRegistryMock();
                // Add the text buffer to the list of the classes that local registry can create.
                mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock);

                // Add the local registry to the service provider.
                provider.AddService(typeof(SLocalRegistry), mockRegistry, false);

                // Create the console.
                using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane)
                {
                    // Make sure that the text marker is created.
                    CommandWindowHelper.EnsureConsoleTextMarker(windowPane);

                    // Set the span of the marker.
                    TextSpan span = new TextSpan();
                    span.iStartLine = 0;
                    span.iStartIndex = 0;
                    span.iEndLine = 3;
                    span.iEndIndex = 5;
                    BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"];
                    markerMock["Span"] = span;

                    IConsoleText consoleText = windowPane as IConsoleText;

                    // Verify the case that the requested line is all inside the
                    // read only region.
                    textLinesMock["LineText"] = testString1;
                    textLinesMock["ExpectedLine"] = 1;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"] = 10;
                    Assert.IsNull(consoleText.TextOfLine(1, 10, true));
                    string text = consoleText.TextOfLine(1, 10, false);
                    Assert.IsTrue(text == testString1);

                    // Now ask for some text inside the read-only region, but on its last line.
                    textLinesMock["LineText"] = testString2;
                    textLinesMock["ExpectedLine"] = 3;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"] = 4;
                    Assert.IsNull(consoleText.TextOfLine(3, 4, true));
                    text = consoleText.TextOfLine(3, 4, false);
                    Assert.IsTrue(text == testString2);

                    // Now the text is part inside and part outside the read-only region.
                    textLinesMock["LineText"] = testString1;
                    textLinesMock["ExpectedLine"] = 3;
                    textLinesMock["ExpectedStart"] = 5;
                    textLinesMock["ExpectedEnd"] = 10;
                    text = consoleText.TextOfLine(3, 10, true);
                    Assert.IsTrue(testString1 == text);
                    textLinesMock["LineText"] = testString2;
                    textLinesMock["ExpectedLine"] = 3;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"] = 10;
                    text = consoleText.TextOfLine(3, 10, false);
                    Assert.IsTrue(text == testString2);

                    // Now the line has no intersection with the read-only region.
                    textLinesMock["LineText"] = testString1;
                    textLinesMock["ExpectedLine"] = 4;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"] = 10;
                    text = consoleText.TextOfLine(4, 10, true);
                    Assert.IsTrue(testString1 == text);
                    textLinesMock["LineText"] = testString2;
                    textLinesMock["ExpectedLine"] = 4;
                    textLinesMock["ExpectedStart"] = 0;
                    textLinesMock["ExpectedEnd"] = 10;
                    text = consoleText.TextOfLine(4, 10, false);
                    Assert.IsTrue(text == testString2);
                }
            }
        }
Пример #22
0
            public PackageTestEnvironment()
            {
                // Create the project
                project = new ProjectTestClass(new ProjectTestPackage());

                // Site the project
                services = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices();
                LocalRegistryMock localRegistry = new LocalRegistryMock();
                localRegistry.RegistryRoot = @"Software\Microsoft\VisualStudio\9.0";
                services.AddService(typeof(SLocalRegistry), localRegistry, true);

                BaseMock mockConfiguration = new GenericMockFactory("MockConfiguration", new[] { typeof(Configuration) }).GetInstance();
                mockConfiguration.AddMethodReturnValues(string.Format("{0}.{1}", typeof(Configuration).FullName, "ConfigurationName"), new[] { "Debug" });
                mockConfiguration.AddMethodReturnValues(string.Format("{0}.{1}", typeof(Configuration).FullName, "PlatformName"), new[] { "AnyCPU" });

                BaseMock mockConfigMgr = ConfigurationManagerFactory.GetInstance();
                mockConfigMgr.AddMethodReturnValues(string.Format("{0}.{1}", typeof(ConfigurationManager).FullName, ""), new[] { mockConfiguration });

                BaseMock extensibility = ExtensibilityFactory.GetInstance();
                extensibility.AddMethodReturnValues(
                    string.Format("{0}.{1}", typeof(IVsExtensibility3).FullName, "GetConfigMgr"),
                    new object[] { 0, null, null, mockConfigMgr });
                services.AddService(typeof(IVsExtensibility), extensibility, false);

                project.SetSite(services);

                // Init the msbuild engine
                Microsoft.Build.Evaluation.ProjectCollection engine = VisualStudio.Project.Utilities.InitializeMsBuildEngine(null, services);
                Assert.IsNotNull(engine, "MSBuild Engine could not be initialized");

                // Retrieve the project file content, load it and save it
                string fullpath = Path.Combine(new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName, "TestProject.proj");
                if(string.IsNullOrEmpty(projectXml))
                {
                    projectXml = Properties.Resources.TestProject;
                    using(TextWriter writer = new StreamWriter(fullpath))
                    {
                        writer.Write(projectXml);
                    }
                }

                // Init the msbuild project
                Microsoft.Build.Evaluation.Project buildProject = VisualStudio.Project.Utilities.InitializeMsBuildProject(engine, fullpath);
                Assert.IsNotNull(buildProject, "MSBuild project not initialized correctly in InitializeMsBuildProject");

                //Verify that we can set the build project on the projectnode
                project.BuildProject = buildProject;

                // Now the project is opened, so we can update its internal variable.
                if(null == projectOpened)
                {
                    projectOpened = typeof(VisualStudio.Project.ProjectNode).GetField("projectOpened", BindingFlags.Instance | BindingFlags.NonPublic);
                }
                projectOpened.SetValue(project, true);
            }
        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));
                }
            }
        }