public void CanMoveEndpointByUnitNearTopBoundary()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange[] visibleRanges      = textPattern.GetVisibleRanges();
                TextPatternRange   testRange          = visibleRanges.First().Clone();

                // assumes that range is a line range at the top of the screen buffer
                Action <TextPatternRange> testTopBoundary = delegate(TextPatternRange range)
                {
                    // the first visible range is at the top of the screen
                    // buffer, we shouldn't be able to move the starting endpoint up
                    int moveAmount = range.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Line, -1);
                    Verify.AreEqual(0, moveAmount);

                    // we should be able to move the ending endpoint back, creating a degenerate range
                    moveAmount = range.MoveEndpointByUnit(TextPatternRangeEndpoint.End, TextUnit.Line, -1);
                    Verify.AreEqual(-1, moveAmount);

                    // the range should now be degenerate and the ending
                    // endpoint should not be able to be moved back again
                    string rangeText = range.GetText(-1);
                    Verify.AreEqual("", rangeText);
                    moveAmount = range.MoveEndpointByUnit(TextPatternRangeEndpoint.End, TextUnit.Line, -1);
                    Verify.AreEqual(-1, moveAmount);
                };

                testTopBoundary(testRange);

                // we want to test that the boundaries are still observed
                // when the screen buffer index and text buffer index don't align.
                // write a bunch of text to the screen to fill up the text
                // buffer and make it start to reuse its buffer
                _FillOutputBufferWithData(app);
                Globals.WaitForTimeout();

                // move all the way to the bottom
                visibleRanges = textPattern.GetVisibleRanges();
                testRange     = visibleRanges.Last().Clone();
                while (true)
                {
                    int moved = testRange.Move(TextUnit.Line, 1);
                    if (moved == 0)
                    {
                        break;
                    }
                }
                // we're at the bottom of the screen buffer, so move back to the top
                // so we can test
                int rowsToMove = -1 * (_GetTotalRows(app) - 1);
                int moveCount  = testRange.Move(TextUnit.Line, rowsToMove);
                Verify.AreEqual(rowsToMove, moveCount);
                testRange.ScrollIntoView(true);

                testTopBoundary(testRange);
            }
        }
        public void CanExpandToEnclosingUnitTextRangeProvider()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange[] visibleRanges      = textPattern.GetVisibleRanges();
                TextPatternRange   testRange          = visibleRanges.First().Clone();

                // change testRange to a degenerate range and then expand to a line
                testRange.MoveEndpointByRange(TextPatternRangeEndpoint.End, testRange, TextPatternRangeEndpoint.Start);
                Verify.AreEqual(0, testRange.CompareEndpoints(TextPatternRangeEndpoint.Start,
                                                              testRange,
                                                              TextPatternRangeEndpoint.End));
                testRange.ExpandToEnclosingUnit(TextUnit.Line);
                Verify.IsTrue(testRange.Compare(visibleRanges[0]));

                // expand to document size
                testRange.ExpandToEnclosingUnit(TextUnit.Document);
                Verify.IsTrue(testRange.Compare(textPattern.DocumentRange));

                // shrink back to a line
                testRange.ExpandToEnclosingUnit(TextUnit.Line);
                Verify.IsTrue(testRange.Compare(visibleRanges[0]));

                // make the text buffer start to cycle its buffer
                _FillOutputBufferWithData(app);

                // expand to document range again
                testRange.ExpandToEnclosingUnit(TextUnit.Document);
                Verify.IsTrue(testRange.Compare(textPattern.DocumentRange));
            }
        }
        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());
                }
            }
        }
Esempio n. 4
0
        public TextPatternRange [] GetVisibleRanges(bool log)
        {
            if (log)
            {
                procedureLogger.Action(string.Format("Get visible ranges from {0}.", this.NameAndType));
            }

            TextPattern tp = (TextPattern)element.GetCurrentPattern(TextPattern.Pattern);

            return((TextPatternRange [])tp.GetVisibleRanges());
        }
Esempio n. 5
0
        public override string GetText()
        {
            AutomationElement edit = GetNotepadEdit(node);

            TextPattern pattern = edit.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
            string      ret     = string.Empty;

            if (pattern != null)
            {
                TextPatternRange[] ranges = pattern.GetVisibleRanges();
                foreach (TextPatternRange range in ranges)
                {
                    ret += range.GetText(-1);
                }
            }
            return(ret);
        }
 public void CanGetBoundingRectangles()
 {
     using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
     {
         AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
         TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
         TextPatternRange[] visibleRanges      = textPattern.GetVisibleRanges();
         // copy the first range
         TextPatternRange firstRange = visibleRanges[0].Clone();
         // only one bounding rect should be returned for the one line
         Rect[] boundingRects = firstRange.GetBoundingRectangles();
         Verify.AreEqual(1, boundingRects.GetLength(0));
         // expand to two lines, verify we get a bounding rect per line
         firstRange.MoveEndpointByRange(TextPatternRangeEndpoint.End, visibleRanges[1], TextPatternRangeEndpoint.End);
         boundingRects = firstRange.GetBoundingRectangles();
         Verify.AreEqual(2, boundingRects.GetLength(0));
     }
 }
        public void CanGetTextAtCharacterLevel()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                const int    noMaxLength = -1;
                const string row1        = "1234567890";
                const string row2        = "   abcdefghijk";
                _ClearScreenBuffer(app);
                _WriteCharTestText(app);
                AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange[] ranges             = textPattern.GetVisibleRanges();
                TextPatternRange   range = ranges[0].Clone();

                // should be able to get each char in row1
                range.ExpandToEnclosingUnit(TextUnit.Character);
                foreach (char ch in row1)
                {
                    string text = range.GetText(noMaxLength);
                    Verify.AreEqual(ch.ToString(), text);
                    range.Move(TextUnit.Character, 1);
                }

                // should be able to get each char in row2, including starting spaces
                range = ranges[1].Clone();
                range.ExpandToEnclosingUnit(TextUnit.Character);
                foreach (char ch in row2)
                {
                    string text = range.GetText(noMaxLength);
                    Verify.AreEqual(ch.ToString(), text);
                    range.Move(TextUnit.Character, 1);
                }

                // taking half of each row should return correct text with
                // spaces if they appear before the last non-whitespace char
                range = ranges[0].Clone();
                range.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Character, 8);
                range.MoveEndpointByUnit(TextPatternRangeEndpoint.End, TextUnit.Character, 8);
                Verify.AreEqual("90\r\n   abcde", range.GetText(noMaxLength));
            }
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            try
            {
                AutomationElement rootElement = AutomationElement.RootElement;

                if (rootElement != null)
                {
                    AutomationElement appElement = null;

                    appElement = rootElement.FindFirst(TreeScope.Children,
                                                       new PropertyCondition(AutomationElement.NameProperty
                                                                             , args[1].ToLower().Trim('"')
                                                                             , PropertyConditionFlags.IgnoreCase));


                    string function = args[0].ToLower().Trim('"');

                    if (function.Equals("settext"))
                    {
                        AutomationElement txt = GetElementByName(appElement, args[2].Trim('"'));

                        if (txt != null)
                        {
                            ValuePattern pattern = txt.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

                            pattern.SetValue(args[3].Trim('"'));

                            TextPattern textPattern = txt.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

                            textPattern.GetVisibleRanges()[0].Select();
                        }
                    }
                }
            }
            catch
            {}
        }
        public void CanMoveRange()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange[] visibleRanges      = textPattern.GetVisibleRanges();
                TextPatternRange   testRange          = visibleRanges.First().Clone();


                // assumes range is at the top of the screen buffer
                Action <TextPatternRange> testMovement = delegate(TextPatternRange range)
                {
                    // the range is at the top of the screen
                    // buffer, we shouldn't be able to move up.
                    int moveAmount = range.Move(TextUnit.Line, -1);
                    Verify.AreEqual(0, moveAmount);

                    // move to the bottom of the screen
                    // - 1 because we're already on the 0th row
                    int rowsToMove = _GetTotalRows(app) - 1;
                    moveAmount = range.Move(TextUnit.Line, rowsToMove);
                    Verify.AreEqual(rowsToMove, moveAmount);

                    // try to move one more row down, we should not be able to
                    moveAmount = range.Move(TextUnit.Line, 1);
                    Verify.AreEqual(0, moveAmount);

                    // move the range up to the top again, one row at a time,
                    // making sure that we have only one line being encompassed
                    // by the range. We check this by counting the number of
                    // bounding rectangles that represent the range.
                    for (int i = 0; i < rowsToMove; ++i)
                    {
                        moveAmount = range.Move(TextUnit.Line, -1);
                        // we need to scroll into view or getting the boundary
                        // rectangles might return 0
                        Verify.AreEqual(-1, moveAmount);
                        range.ScrollIntoView(true);
                        Rect[] boundingRects = range.GetBoundingRectangles();
                        Verify.AreEqual(1, boundingRects.GetLength(0));
                    }

                    // and back down to the bottom, one row at a time
                    for (int i = 0; i < rowsToMove; ++i)
                    {
                        moveAmount = range.Move(TextUnit.Line, 1);
                        // we need to scroll into view or getting the boundary
                        // rectangles might return 0
                        Verify.AreEqual(1, moveAmount);
                        range.ScrollIntoView(true);
                        Rect[] boundingRects = range.GetBoundingRectangles();
                        Verify.AreEqual(1, boundingRects.GetLength(0));
                    }
                };

                testMovement(testRange);

                // test again with unaligned text buffer and screen buffer
                _FillOutputBufferWithData(app);
                Globals.WaitForTimeout();

                visibleRanges = textPattern.GetVisibleRanges();
                testRange     = visibleRanges.First().Clone();
                // move range back to the top
                while (true)
                {
                    int moveCount = testRange.Move(TextUnit.Line, -1);
                    if (moveCount == 0)
                    {
                        break;
                    }
                }

                testMovement(testRange);
            }
        }
Esempio n. 10
0
        [TestProperty("Ignore", "True")] // GH#7282 - investigate and reenable
        public void CanMoveEndpointByUnitNearBottomBoundary()
        {
            using (CmdApp app = new CmdApp(CreateType.ProcessOnly, TestContext))
            {
                var sbiex = app.GetScreenBufferInfo();
                sbiex.dwSize.Y = (short)(2 * sbiex.srWindow.Height);
                app.SetScreenBufferInfo(sbiex);

                AutomationElement  textAreaUiaElement = GetTextAreaUiaElement(app);
                TextPattern        textPattern        = textAreaUiaElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
                TextPatternRange[] visibleRanges      = textPattern.GetVisibleRanges();
                TextPatternRange   testRange          = visibleRanges.First().Clone();

                // assumes that range is a line range at the bottom of the screen buffer
                Action <TextPatternRange> testBottomBoundary = delegate(TextPatternRange range)
                {
                    // the range is at the bottom of the screen buffer, we
                    // shouldn't be able to move the endpoint endpoint down
                    int moveAmount = range.MoveEndpointByUnit(TextPatternRangeEndpoint.End, TextUnit.Line, 1);
                    Verify.AreEqual(0, moveAmount);

                    // we shouldn't be able to move the starting endpoint down either
                    moveAmount = range.MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Line, 1);
                    Verify.AreEqual(0, moveAmount);
                };

                // move the range to the bottom of the screen
                int rowsToMove = _GetTotalRows(app) - 1;
                int moveCount  = testRange.Move(TextUnit.Line, rowsToMove);
                Verify.AreEqual(rowsToMove, moveCount);

                testBottomBoundary(testRange);

                // we want to test that the boundaries are still observed
                // when the screen buffer index and text buffer index don't align.
                // write a bunch of text to the screen to fill up the text
                // buffer and make it start to reuse its buffer
                _FillOutputBufferWithData(app);
                Globals.WaitForTimeout();

                // move all the way to the top
                visibleRanges = textPattern.GetVisibleRanges();
                testRange     = visibleRanges.First().Clone();
                while (true)
                {
                    int moved = testRange.Move(TextUnit.Line, -1);
                    if (moved == 0)
                    {
                        break;
                    }
                }

                // we're at the top of the screen buffer, so move back to the bottom
                // so we can test
                rowsToMove = _GetTotalRows(app) - 1;
                moveCount  = testRange.Move(TextUnit.Line, rowsToMove);
                Verify.AreEqual(rowsToMove, moveCount);
                testRange.ScrollIntoView(true);

                testBottomBoundary(testRange);
            }
        }
        /// <summary>
        /// Retrieves an array of disjoint text ranges from a text container where each text range begins with the first
        /// partially visible line through to the end of the last partially visible line
        /// </summary>
        /// <param name="control">The UI Automation element</param>
        /// <returns>
        /// The collection of visible text ranges within the container or an empty array
        /// </returns>
        internal static TextPatternRange[] GetVisibleRanges(AutomationElement control)
        {
            TextPattern pat = (TextPattern)CommonUIAPatternHelpers.CheckPatternSupport(TextPattern.Pattern, control);

            return(pat.GetVisibleRanges());
        }