locked
Can use IVsTextView.ReplaceTextOnLine to edit the "read-only" Code Definition window RRS feed

  • General discussion

  • I am writing some sign-off unit tests for my package as per VSIT Tests.xls.

    For this this test:
    Editor->Code Definition Window Cut/Copy/Paste/Delete
    3) Paste text at the BOF in the window and verify previous cursor  position and selection preserved.

    I wrote the following code:

    IVsUIShell uiShellService = VsIdeTestHostContext.ServiceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell; Assert.IsNotNull(uiShellService); IVsWindowFrame windowFrame; Guid persistenceGuid = new Guid(ToolWindowGuids80.CodedefinitionWindow); int hr = uiShellService.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref persistenceGuid, out windowFrame); Assert.IsTrue(hr == VSConstants.S_OK); IVsTextView textView = VsShellUtilities.GetTextView(windowFrame); Assert.IsNotNull(textView); hr = textView.SetCaretPos(0, 0); Assert.IsTrue(hr == VSConstants.S_OK); // Actually edits text in the "read-only" code definition window! hr = textView.ReplaceTextOnLine(0, 0, 5, "12345", 5); Assert.IsTrue(hr == VSConstants.S_OK); int line; int column; hr = textView.GetCaretPos(out line, out column); Assert.IsTrue(hr == VSConstants.S_OK);
    Assert.IsTrue(line == 0);
    Assert.IsTrue(column == 0);

    The test failed, and I saw the code actually being edited in the Code Definition window, which is otherwise "read-only" to the user.

    Is this by design or a bug?





    Wednesday, July 25, 2012 10:40 AM

All replies

  • Hi Priyadarshini,

    Where I can try these codes? Can you debug this codes? Will any exception be reported?

    Best regards,

    Disley

    Friday, July 27, 2012 4:55 AM
  • The package creation wizard allows you to create an integration tests project. I have added this test to the same project. Here's the complete code:

    using System;
    using System.Windows;
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TextManager.Interop;
    using Microsoft.VsSDK.IntegrationTestLibrary;
    using Microsoft.VSSDK.Tools.VsIdeTesting;
    namespace IntegrationTests
    {
        [TestClass]
        public class CodeDefinitionTests
        {
            #region fields
            private delegate void ThreadInvoker();
            private TestContext _testContext;
            #endregion
            #region properties
            /// <summary>
            ///Gets or sets the test context which provides
            ///information about and functionality for the current test run.
            ///</summary>
            public TestContext TestContext
            {
                get { return _testContext; }
                set { _testContext = value; }
            }
            #endregion
            #region ctors
            public CodeDefinitionTests()
            {
            }
            #endregion
            #region Additional test attributes
            //
            // You can use the following additional attributes as you write your tests:
            //
            // Use ClassInitialize to run code before running the first test in the class
            // [ClassInitialize()]
            // public static void MyClassInitialize(TestContext testContext) { }
            //
            // Use ClassCleanup to run code after all tests in a class have run
            // [ClassCleanup()]
            // public static void MyClassCleanup() { }
            //
            // Use TestInitialize to run code before running each test 
            // [TestInitialize()]
            // public void MyTestInitialize() { }
            //
            // Use TestCleanup to run code after each test has run
            // [TestCleanup()]
            // public void MyTestCleanup() { }
            //
            #endregion
            [TestMethod]
            [HostType("VS IDE")]
            public void CodePasteTest()
            {
                PrepareProject();
                UIThreadInvoker.Invoke((ThreadInvoker)delegate()
                {
                    IVsUIShell uiShellService = VsIdeTestHostContext.ServiceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell;
                    Assert.IsNotNull(uiShellService);
                    IVsWindowFrame windowFrame;
                    Guid persistenceGuid = new Guid(ToolWindowGuids80.CodedefinitionWindow);
                    int hr = uiShellService.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref persistenceGuid, out windowFrame);
                    Assert.IsTrue(hr == VSConstants.S_OK);
                    IVsTextView textView = VsShellUtilities.GetTextView(windowFrame);
                    Assert.IsNotNull(textView);
                    hr = textView.SetSelection(0, 0, 0, 5);
                    Assert.IsTrue(hr == VSConstants.S_OK);
                    IOleCommandTarget dispatcher = VsIdeTestHostContext.ServiceProvider.GetService(typeof(SUIHostCommandDispatcher)) as IOleCommandTarget;
                    Guid pguidCmdGroup = VSConstants.GUID_VSStandardCommandSet97;
                    hr = dispatcher.Exec(ref pguidCmdGroup, (uint)VSConstants.VSStd97CmdID.Copy, 0, IntPtr.Zero, IntPtr.Zero);
                    Assert.IsTrue(hr == VSConstants.S_OK);
                    hr = textView.SetCaretPos(0, 0);
                    Assert.IsTrue(hr == VSConstants.S_OK);
                    // Actually edits text in the "read-only" code definition window!
                    hr = textView.ReplaceTextOnLine(0, 0, 5, "12345", 5);
                    Assert.IsTrue(hr == VSConstants.S_OK);
                    int line;
                    int column;
                    hr = textView.GetCaretPos(out line, out column);
                    Assert.IsTrue(hr == VSConstants.S_OK);
                    Assert.IsTrue(line == 0);
                    Assert.IsTrue(column == 0);
                });
            }
            void PrepareProject()
            {
                UIThreadInvoker.Invoke((ThreadInvoker)delegate()
                {
                    DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeof(DTE));
                    //Solution and project creation parameters
                    string solutionName = "CSConsoleApp";
                    string projectName = "CSConsoleApp";
                    //Template parameters
                    string language = "CSharp";
                    string projectTemplateName = "ConsoleApplication.Zip";
                    TestUtils testUtils = new TestUtils();
                    testUtils.CreateEmptySolution(TestContext.TestDir, solutionName);
                    Assert.AreEqual<int>(0, testUtils.ProjectCount());
                    //Add new Console application project to existing solution
                    testUtils.CreateProjectFromTemplate(projectName, projectTemplateName, language, false);
                    //Verify that the new project has been added to the solution
                    Assert.AreEqual<int>(1, testUtils.ProjectCount());
                    //Get the project
                    Project project = dte.Solution.Item(1);
                    Assert.IsNotNull(project);
                    Assert.IsTrue(string.Compare(project.Name, projectName, StringComparison.InvariantCultureIgnoreCase) == 0);
                    Document program = dte.ActiveDocument;
                    string programName = "Program.cs";
                    Assert.IsTrue(string.Compare(program.Name, programName, StringComparison.InvariantCultureIgnoreCase) == 0);
                    TextSelection selection = program.Selection as TextSelection;
                    Assert.IsNotNull(selection);
                    selection.LineDown(false, 5);
                    selection.WordRight();
                });
                System.Threading.Thread.Sleep(5000);
                UIThreadInvoker.Invoke((ThreadInvoker)delegate()
                {
                    DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeof(DTE));
                    Window codedef = dte.Windows.Item(WindowKinds.vsWindowKindCodeDefinition);
                    codedef.Activate();
                });
                System.Threading.Thread.Sleep(5000);
            }
        }
    }

    Friday, July 27, 2012 5:09 AM