Beispiel #1
0
 public void VerifyCtrlHCmd()
 {
     using (RegistryHelper reg = new RegistryHelper())
     {
         reg.BackupRegistry();
         using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
         {
             using (ViewportArea area = new ViewportArea(app))
             {
                 string testText = "1234blah5678";
                 IntPtr hConsole = app.GetStdOutHandle();
                 Verify.IsNotNull(hConsole, "Ensure the handle is valid.");
                 // get cursor location
                 WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX screenBufferInfo = new WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX();
                 WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref screenBufferInfo);
                 // send some text and a ^H to remove the last character
                 app.UIRoot.SendKeys(testText + Keys.Control + "h" + Keys.Control);
                 Globals.WaitForTimeout();
                 // test that we're missing the last character of testText on the line
                 Rectangle            rect = new Rectangle(0, 0, 200, 20);
                 IEnumerable <string> text = area.GetLinesInRectangle(hConsole, rect);
                 bool foundCtrlH           = false;
                 foreach (string line in text)
                 {
                     if (line.Contains(testText.Substring(0, testText.Length - 1)) && !line.Contains(testText))
                     {
                         foundCtrlH = true;
                         break;
                     }
                 }
                 Verify.IsTrue(foundCtrlH);
             }
         }
     }
 }
        public void CanGetVisibleRange()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                // get the ranges from uia api
                AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange[] ranges             = textPattern.GetVisibleRanges();

                // get the ranges from the console api
                WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX screenInfo = app.GetScreenBufferInfo();
                int viewportHeight = screenInfo.srWindow.Bottom - screenInfo.srWindow.Top + 1;

                // we should have one range per line in the viewport
                Verify.AreEqual(ranges.GetLength(0), viewportHeight);

                // each line should have the same text
                ViewportArea viewport = new ViewportArea(app);
                IntPtr       hConsole = app.GetStdOutHandle();
                for (int i = 0; i < viewportHeight; ++i)
                {
                    Rectangle            rect = new Rectangle(0, i, screenInfo.dwSize.X, 1);
                    IEnumerable <string> text = viewport.GetLinesInRectangle(hConsole, rect);
                    Verify.AreEqual(text.ElementAt(0).Trim(), ranges[i].GetText(-1).Trim());
                }
            }
        }
Beispiel #3
0
 public void VerifyCtrlZCmd()
 {
     using (RegistryHelper reg = new RegistryHelper())
     {
         reg.BackupRegistry();
         using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
         {
             using (ViewportArea area = new ViewportArea(app))
             {
                 IntPtr hConsole = app.GetStdOutHandle();
                 Verify.IsNotNull(hConsole, "Ensure the handle is valid.");
                 // get cursor location
                 WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX screenBufferInfo = new WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX();
                 WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref screenBufferInfo);
                 // send ^Z
                 app.UIRoot.SendKeys(Keys.Control + "z" + Keys.Control);
                 Globals.WaitForTimeout();
                 // test that "^Z" exists on the screen
                 Rectangle            rect = new Rectangle(0, 0, 200, 20);
                 IEnumerable <string> text = area.GetLinesInRectangle(hConsole, rect);
                 bool foundCtrlZ           = false;
                 foreach (string line in text)
                 {
                     if (line.Contains("^Z"))
                     {
                         foundCtrlZ = true;
                         break;
                     }
                 }
                 Verify.IsTrue(foundCtrlZ);
             }
         }
     }
 }
        public void CanGetDocumentRangeText()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                // get the text from uia api
                AutomationElement textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern       textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange  documentRange      = textPattern.DocumentRange;
                string            allText            = documentRange.GetText(-1);
                // get text from console api
                IntPtr hConsole = app.GetStdOutHandle();
                using (ViewportArea area = new ViewportArea(app))
                {
                    WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX screenInfo = app.GetScreenBufferInfo();
                    Rectangle            rect         = new Rectangle(0, 0, screenInfo.dwSize.X, screenInfo.dwSize.Y);
                    IEnumerable <string> viewportText = area.GetLinesInRectangle(hConsole, rect);

                    // the uia api does not return spaces beyond the last
                    // non -whitespace character so we need to trim those from
                    // the viewportText. The uia api also inserts \r\n to indicate
                    // a new linen so we need to add those back in after trimming.
                    string consoleText = "";
                    for (int i = 0; i < viewportText.Count(); ++i)
                    {
                        consoleText += viewportText.ElementAt(i).Trim() + "\r\n";
                    }
                    consoleText = consoleText.Trim();
                    allText     = allText.Trim();
                    // compare
                    Verify.IsTrue(consoleText.Equals(allText));
                }
            }
        }
        private static void BufferVerificationHelper(CmdApp app, ViewportArea area, IntPtr hConsole, GetExpectedChar expectedCharAlgorithm)
        {
            WinCon.SMALL_RECT    viewport    = app.GetViewport(hConsole);
            Rectangle            selectRect  = new Rectangle(viewport.Left, viewport.Top, viewport.Width, viewport.Height);
            IEnumerable <string> scrapedText = area.GetLinesInRectangle(hConsole, selectRect);

            Verify.AreEqual(viewport.Height, scrapedText.Count(), "Verify the rows scraped is equal to the entire viewport height.");

            bool isValidState = true;

            string[] rows = scrapedText.ToArray();
            for (int i = 0; i < rows.Length; i++)
            {
                for (int j = 0; j < viewport.Width; j++)
                {
                    char actual   = rows[i][j];
                    char expected = expectedCharAlgorithm(i, j, rows.Length, viewport.Width);

                    isValidState = actual == expected;

                    if (!isValidState)
                    {
                        Verify.Fail(string.Format("Text buffer verification failed at Row: {0} Col: {1} Expected: '{2}' Actual: '{3}'", i, j, expected, actual));
                        break;
                    }
                }

                if (!isValidState)
                {
                    break;
                }
            }
        }
        public void RunVtAppTester()
        {
            using (RegistryHelper reg = new RegistryHelper())
            {
                reg.BackupRegistry(); // we're going to modify the virtual terminal state for this, so back it up first.
                VersionSelector.SetConsoleVersion(reg, ConsoleVersion.V2);
                reg.SetDefaultValue(VIRTUAL_TERMINAL_KEY_NAME, VIRTUAL_TERMINAL_ON_VALUE);

                bool haveVtAppPath = !string.IsNullOrEmpty(vtAppLocation);

                Verify.IsTrue(haveVtAppPath, "Ensure that we passed in the location to VtApp.exe");

                if (haveVtAppPath)
                {
                    using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext, vtAppLocation))
                    {
                        using (ViewportArea area = new ViewportArea(app))
                        {
                            // Get console handle.
                            IntPtr hConsole = app.GetStdOutHandle();
                            Verify.IsNotNull(hConsole, "Ensure the STDOUT handle is valid.");

                            Log.Comment("Check that the VT test app loaded up properly with its output line and the cursor down one line.");
                            Rectangle            selectRect  = new Rectangle(0, 0, 9, 1);
                            IEnumerable <string> scrapedText = area.GetLinesInRectangle(hConsole, selectRect);

                            Verify.AreEqual(scrapedText.Count(), 1, "We should have retrieved one line.");
                            string testerWelcome = scrapedText.Single();

                            Verify.AreEqual(testerWelcome, "VT Tester");

                            WinCon.COORD cursorPos      = app.GetCursorPosition(hConsole);
                            WinCon.COORD cursorExpected = new WinCon.COORD();
                            cursorExpected.X = 0;
                            cursorExpected.Y = 1;
                            Verify.AreEqual(cursorExpected, cursorPos, "Check cursor has moved to expected starting position.");

                            TestCursorPositioningCommands(app, hConsole, cursorExpected);

                            TestCursorVisibilityCommands(app, hConsole);

                            TestAreaEraseCommands(app, area, hConsole);

                            TestGraphicsCommands(app, area, hConsole);

                            TestQueryResponses(app, hConsole);

                            TestVtToggle(app, hConsole);

                            TestInsertDelete(app, area, hConsole);
                        }
                    }
                }
            }
        }
Beispiel #7
0
 public void VerifyCtrlCCmd()
 {
     using (RegistryHelper reg = new RegistryHelper())
     {
         reg.BackupRegistry();
         using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
         {
             using (ViewportArea area = new ViewportArea(app))
             {
                 IntPtr hConsole = app.GetStdOutHandle();
                 Verify.IsNotNull(hConsole, "Ensure the handle is valid.");
                 Globals.WaitForTimeout();
                 // send ctrl-c sequence
                 const int keypressCount = 10;
                 for (int i = 0; i < keypressCount; ++i)
                 {
                     app.UIRoot.SendKeys(Keys.Control + "c" + Keys.Control);
                 }
                 Globals.WaitForTimeout();
                 // fetch the text
                 Rectangle            rect = new Rectangle(0, 0, 50, 50);
                 IEnumerable <string> text = area.GetLinesInRectangle(hConsole, rect);
                 // filter out the blank lines
                 List <string> possiblePromptLines = new List <string>();
                 for (int i = 0; i < text.Count(); ++i)
                 {
                     string line = text.ElementAt(i);
                     line.Trim(' ');
                     if (!line.Equals(""))
                     {
                         possiblePromptLines.Add(line);
                     }
                 }
                 // make sure that the prompt line shows up for each ^C key press
                 Verify.IsTrue(possiblePromptLines.Count() >= keypressCount);
                 possiblePromptLines.Reverse();
                 for (int i = 0; i < keypressCount; ++i)
                 {
                     Verify.AreEqual(possiblePromptLines[0], possiblePromptLines[1]);
                     possiblePromptLines.RemoveAt(0);
                 }
             }
         }
     }
 }
 public void VerifyVimInput()
 {
     if (!IsProgramInPath("vim.exe"))
     {
         Log.Comment("vim can't be found in path, skipping test.");
         return;
     }
     using (RegistryHelper reg = new RegistryHelper())
     {
         reg.BackupRegistry();
         using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
         {
             using (ViewportArea area = new ViewportArea(app))
             {
                 IntPtr hConsole = app.GetStdOutHandle();
                 string testText = "hello world";
                 Verify.IsNotNull(hConsole, "ensure the stdout handle is valid.");
                 // start up vim
                 app.UIRoot.SendKeys("vim");
                 app.UIRoot.SendKeys(Keys.Enter);
                 Globals.WaitForTimeout();
                 app.UIRoot.SendKeys(Keys.Enter);
                 // go to insert mode
                 app.UIRoot.SendKeys("i");
                 // write some text
                 app.UIRoot.SendKeys(testText);
                 Globals.WaitForTimeout();
                 // make sure text showed up in the output
                 Rectangle            rect = new Rectangle(0, 0, 20, 20);
                 IEnumerable <string> text = area.GetLinesInRectangle(hConsole, rect);
                 bool foundText            = false;
                 foreach (string line in text)
                 {
                     if (line.Contains(testText))
                     {
                         foundText = true;
                         break;
                     }
                 }
                 Verify.IsTrue(foundText);
             }
         }
     }
 }
Beispiel #9
0
 public void VerifyCtrlCBash()
 {
     using (RegistryHelper reg = new RegistryHelper())
     {
         reg.BackupRegistry();
         using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
         {
             using (ViewportArea area = new ViewportArea(app))
             {
                 IntPtr hConsole = app.GetStdOutHandle();
                 Verify.IsNotNull(hConsole, "Ensure the STDOUT handle is valid.");
                 // start up bash, run cat, type ctrl+c
                 app.UIRoot.SendKeys("bash");
                 app.UIRoot.SendKeys(Keys.Enter);
                 Globals.WaitForTimeout();
                 app.UIRoot.SendKeys("cat");
                 app.UIRoot.SendKeys(Keys.Enter);
                 Globals.WaitForTimeout();
                 app.UIRoot.SendKeys(Keys.Control + "c" + Keys.Control);
                 Globals.WaitForTimeout();
                 // make sure "^C" showed up in the output
                 Rectangle            rect = new Rectangle(0, 0, 10, 10);
                 IEnumerable <string> text = area.GetLinesInRectangle(hConsole, rect);
                 bool foundCtrlC           = false;
                 foreach (string line in text)
                 {
                     if (line.Contains("^C"))
                     {
                         foundCtrlC = true;
                         break;
                     }
                 }
                 Verify.IsTrue(foundCtrlC);
             }
         }
     }
 }
Beispiel #10
0
        public void TestCtrlHomeEnd()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                using (ViewportArea area = new ViewportArea(app))
                {
                    // Get console handle.
                    IntPtr hConsole = app.GetStdOutHandle();
                    Verify.IsNotNull(hConsole, "Ensure the STDOUT handle is valid.");

                    // Get us to an expected initial state.
                    app.UIRoot.SendKeys("C:" + Keys.Enter);
                    app.UIRoot.SendKeys(@"cd C:\" + Keys.Enter);
                    app.UIRoot.SendKeys("cls" + Keys.Enter);

                    // Get initial screen buffer position
                    WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX sbiexOriginal = new WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX();
                    sbiexOriginal.cbSize = (uint)Marshal.SizeOf(sbiexOriginal);
                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexOriginal), "Get initial viewport position.");

                    // Prep comparison structure
                    WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX sbiexCompare = new WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX();
                    sbiexCompare.cbSize = (uint)Marshal.SizeOf(sbiexCompare);

                    // Ctrl-End shouldn't move anything yet.
                    Log.Comment("Attempt Ctrl-End. Nothing should move yet.");
                    app.UIRoot.SendKeys(Keys.Control + Keys.End + Keys.Control);

                    Globals.WaitForTimeout();

                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexCompare), "Get comparison position.");
                    Verify.AreEqual <WinCon.SMALL_RECT>(sbiexOriginal.srWindow, sbiexCompare.srWindow, "Compare viewport positions before and after.");

                    // Ctrl-Home shouldn't move anything yet.
                    Log.Comment("Attempt Ctrl-Home. Nothing should move yet.");
                    app.UIRoot.SendKeys(Keys.Control + Keys.Home + Keys.Control);

                    Globals.WaitForTimeout();

                    Log.Comment("Now test the line with some text in it.");
                    // Retrieve original position (including cursor)
                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexOriginal), "Get position of viewport with nothing on edit line.");

                    // Put some text onto the edit line now
                    Log.Comment("Place some text onto the edit line to ensure behavior will change with edit line full.");
                    const string testText = "SomeTestText";
                    app.UIRoot.SendKeys(testText);

                    Globals.WaitForTimeout();

                    // Get the position of the cursor after the text is entered
                    WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX sbiexWithText = new WinCon.CONSOLE_SCREEN_BUFFER_INFO_EX();
                    sbiexWithText.cbSize = (uint)Marshal.SizeOf(sbiexWithText);
                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexWithText), "Get position of viewport with edit line text.");

                    // The cursor can't have moved down a line. We're going to verify the text by reading its "rectangle" out of the screen buffer.
                    // If it moved down a line, the calculation of what to select is more complicated than the simple rectangle assignment below.
                    Verify.AreEqual(sbiexOriginal.dwCursorPosition.Y, sbiexWithText.dwCursorPosition.Y, "There's an assumption here that the cursor stayed on the same line when we added our bit of text.");

                    // Prepare the read rectangle for what we want to get out of the buffer.
                    Rectangle readRectangle = new Rectangle(sbiexOriginal.dwCursorPosition.X,
                                                            sbiexOriginal.dwCursorPosition.Y,
                                                            (sbiexWithText.dwCursorPosition.X - sbiexOriginal.dwCursorPosition.X),
                                                            1);

                    Log.Comment("Verify that the text we keyed matches what's in the buffer.");
                    IEnumerable <string> text = area.GetLinesInRectangle(hConsole, readRectangle);
                    Verify.AreEqual(text.Count(), 1, "We should only have retrieved one line.");
                    Verify.AreEqual(text.First(), testText, "Verify text matches keyed input.");

                    // Move cursor into the middle of the text.
                    Log.Comment("Move cursor into the middle of the string.");

                    const int lefts = 4;
                    for (int i = 0; i < lefts; i++)
                    {
                        app.UIRoot.SendKeys(Keys.Left);
                    }

                    Globals.WaitForTimeout();

                    // Get cursor position now that it's moved.
                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexWithText), "Get position of viewport with cursor moved into the middle of the edit line text.");

                    Log.Comment("Ctrl-End should trim the end of the input line from the cursor (and not move the cursor.)");
                    app.UIRoot.SendKeys(Keys.Control + Keys.End + Keys.Control);

                    Globals.WaitForTimeout();

                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexCompare), "Get comparison position.");
                    Verify.AreEqual <WinCon.SMALL_RECT>(sbiexWithText.srWindow, sbiexCompare.srWindow, "Compare viewport positions before and after.");
                    Verify.AreEqual <WinCon.COORD>(sbiexWithText.dwCursorPosition, sbiexCompare.dwCursorPosition, "Compare cursor positions before and after.");

                    Log.Comment("Compare actual text visible on screen.");
                    text = area.GetLinesInRectangle(hConsole, readRectangle);
                    Verify.AreEqual(text.Count(), 1, "We should only have retrieved one line.");

                    // the substring length is the original length of the string minus the number of lefts
                    int substringCtrlEnd = testText.Length - lefts;
                    Verify.AreEqual(text.First().Trim(), testText.Substring(0, substringCtrlEnd), "Verify text matches keyed input without the last characters removed by Ctrl+End.");

                    Log.Comment("Ctrl-Home should trim the remainder of the edit line from the cursor to the beginning (restoring cursor to position before we entered anything.)");
                    app.UIRoot.SendKeys(Keys.Control + Keys.Home + Keys.Control);

                    Globals.WaitForTimeout();

                    NativeMethods.Win32BoolHelper(WinCon.GetConsoleScreenBufferInfoEx(hConsole, ref sbiexCompare), "Get comparison position.");
                    Verify.AreEqual <WinCon.SMALL_RECT>(sbiexOriginal.srWindow, sbiexCompare.srWindow, "Compare viewport positions before and after.");
                    Verify.AreEqual <WinCon.COORD>(sbiexOriginal.dwCursorPosition, sbiexCompare.dwCursorPosition, "Compare cursor positions before and after.");

                    Log.Comment("Compare actual text visible on screen.");
                    text = area.GetLinesInRectangle(hConsole, readRectangle);
                    Verify.AreEqual(text.Count(), 1, "We should only have retrieved one line.");

                    Verify.AreEqual(text.First().Trim(), string.Empty, "Verify text is now empty after Ctrl+Home from the end of it.");
                }
            }
        }