Add() public method

public Add ( AutomationPattern pattern ) : void
pattern AutomationPattern
return void
		public IEnumerable<ITab> GetTabsOfWindow(IntPtr hWnd)
		{
			var notepadPlusPlusWindow = AutomationElement.FromHandle(hWnd);

			var cacheRequest = new CacheRequest();
			cacheRequest.Add(AutomationElement.NameProperty);
			cacheRequest.Add(AutomationElement.LocalizedControlTypeProperty);
			cacheRequest.Add(SelectionItemPattern.Pattern);
			cacheRequest.Add(SelectionItemPattern.SelectionContainerProperty);
			cacheRequest.TreeScope = TreeScope.Element;

			AutomationElement tabBarElement;

			using (cacheRequest.Activate())
			{
				tabBarElement = notepadPlusPlusWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab"));
			}

			if(tabBarElement == null)
				yield break;

			var tabElements = tabBarElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tab item"));

			for (var tabIndex = 0; tabIndex < tabElements.Count; tabIndex++)
			{
				yield return new NotepadPlusPlusTab(tabElements[tabIndex]);
			}
		}
 public void MyTestInitialize()
 {
     // Get all children of the desktop for our target collection
     CacheRequest cacheReq = new CacheRequest();
     cacheReq.Add(AutomationElement.NameProperty);
     cacheReq.Add(AutomationElement.NativeWindowHandleProperty);
     using (cacheReq.Activate())
     {
         this.testColl = AutomationElement.RootElement.FindAll(
             TreeScope.Children,
             Condition.TrueCondition);
         Assert.IsNotNull(this.testColl);
         Assert.IsTrue(this.testColl.Count > 0);
     }
 }
 public static CacheRequest BuildCacheRequest(TreeScope treeScope, params AutomationProperty[] properties)
 {
     var cr = new CacheRequest { TreeScope = treeScope };
      foreach (var property in properties)
     cr.Add(property);
      return cr;
 }
示例#4
0
        public static UIA.CacheRequest ToNative(this CacheRequest cacheRequest)
        {
            var nativeCacheRequest = new UIA.CacheRequest();

            nativeCacheRequest.AutomationElementMode = (UIA.AutomationElementMode)cacheRequest.AutomationElementMode;
            nativeCacheRequest.TreeFilter            = ConditionConverter.ToNative(cacheRequest.TreeFilter);
            nativeCacheRequest.TreeScope             = (UIA.TreeScope)cacheRequest.TreeScope;
            foreach (var pattern in cacheRequest.Patterns)
            {
                nativeCacheRequest.Add(UIA.AutomationPattern.LookupById(pattern.Id));
            }
            foreach (var property in cacheRequest.Properties)
            {
                nativeCacheRequest.Add(UIA.AutomationProperty.LookupById(property.Id));
            }
            return(nativeCacheRequest);
        }
        public void GridPatternCachedTest()
        {
            CacheRequest req = new CacheRequest();
            req.Add(GridItemPattern.Pattern);
            req.Add(GridPattern.Pattern);
            req.Add(GridPattern.RowCountProperty);
            req.Add(GridPattern.ColumnCountProperty);
            req.Add(GridItemPattern.RowProperty);
            req.Add(GridItemPattern.ColumnProperty);
            req.Add(GridItemPattern.ContainingGridProperty);

            using (req.Activate())
            {
                AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.Subtree,
                    new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView"));
                Assert.IsNotNull(itemsView);

                // Try out the Grid Pattern
                GridPattern grid = (GridPattern)itemsView.GetCachedPattern(GridPattern.Pattern);
                Assert.IsTrue(grid.Cached.ColumnCount > 0);
                Assert.IsTrue(grid.Cached.RowCount > 0);

                // GridItem
                AutomationElement gridItemElement = grid.GetItem(0, 0);
                Assert.IsNotNull(gridItemElement);
                GridItemPattern gridItem = (GridItemPattern)gridItemElement.GetCachedPattern(GridItemPattern.Pattern);
                Assert.AreEqual(gridItem.Cached.Row, 0);
                Assert.AreEqual(gridItem.Cached.Column, 0);
                Assert.AreEqual(gridItem.Cached.ContainingGrid, itemsView);
            }
        }
示例#6
0
        public void ExpandCollapsePatternCachedTest()
        {
            using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL intl.cpl"))
            {
                CacheRequest req = new CacheRequest();
                req.Add(ExpandCollapsePattern.Pattern);
                req.Add(ExpandCollapsePattern.ExpandCollapseStateProperty);
                using (req.Activate())
                {
                    // Find a well-known combo box
                    AutomationElement combo = host.Element.FindFirst(TreeScope.Subtree,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, "1021"));
                    Assert.IsNotNull(combo);

                    ExpandCollapsePattern expando = (ExpandCollapsePattern)combo.GetCachedPattern(ExpandCollapsePattern.Pattern);
                    Assert.AreEqual(expando.Cached.ExpandCollapseState, ExpandCollapseState.Collapsed);
                }
            }
        }
        public void TablePatternCachedTest()
        {
            CacheRequest req = new CacheRequest();
            req.Add(TablePattern.Pattern);
            req.Add(TableItemPattern.Pattern);
            req.Add(GridPattern.Pattern);
            req.Add(GridItemPattern.Pattern);
            req.Add(GridPattern.RowCountProperty);
            req.Add(GridPattern.ColumnCountProperty);
            req.Add(GridItemPattern.RowProperty);
            req.Add(GridItemPattern.ColumnProperty);
            req.Add(GridItemPattern.ContainingGridProperty);
            req.Add(TablePattern.RowHeadersProperty);
            req.Add(TablePattern.ColumnHeadersProperty);
            req.Add(TableItemPattern.RowHeaderItemsProperty);
            req.Add(TableItemPattern.ColumnHeaderItemsProperty);
            using (req.Activate())
            {
                AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.Subtree,
                    new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView"));
                Assert.IsNotNull(itemsView);

                // TablePattern test
                TablePattern table = (TablePattern)itemsView.GetCachedPattern(TablePattern.Pattern);
                Assert.IsTrue(table.Cached.ColumnCount > 0);
                Assert.IsTrue(table.Cached.RowCount > 0);
                Assert.IsTrue(table.Cached.GetRowHeaders().Length == 0);
                Assert.IsTrue(table.Cached.GetColumnHeaders().Length > 0);

                AutomationElement tableItemElement = table.GetItem(0, 0);
                TableItemPattern tableItem = (TableItemPattern)tableItemElement.GetCachedPattern(TableItemPattern.Pattern);
                Assert.AreEqual(tableItem.Cached.Row, 0);
                Assert.AreEqual(tableItem.Cached.Column, 0);
                Assert.AreEqual(tableItem.Cached.ContainingGrid, itemsView);
                Assert.IsTrue(tableItem.Cached.GetColumnHeaderItems().Length == 1);
                Assert.IsTrue(tableItem.Cached.GetRowHeaderItems().Length == 0);
            }
        }
        protected internal void SubscribeToEvents(HasControlInputCmdletBase cmdlet,
                                                  AutomationElement inputObject,
                                                  AutomationEvent eventType,
                                                  AutomationProperty prop)
        {
            AutomationEventHandler uiaEventHandler;
            AutomationPropertyChangedEventHandler uiaPropertyChangedEventHandler;
            StructureChangedEventHandler uiaStructureChangedEventHandler;
            AutomationFocusChangedEventHandler uiaFocusChangedEventHandler;

            // 20130109
            if (null == CurrentData.Events) {
                CurrentData.InitializeEventCollection();
            }

            try {

                CacheRequest cacheRequest = new CacheRequest();
                cacheRequest.AutomationElementMode = AutomationElementMode.Full; //.None;
                cacheRequest.TreeFilter = Automation.RawViewCondition;
                cacheRequest.Add(AutomationElement.NameProperty);
                cacheRequest.Add(AutomationElement.AutomationIdProperty);
                cacheRequest.Add(AutomationElement.ClassNameProperty);
                cacheRequest.Add(AutomationElement.ControlTypeProperty);
                //cacheRequest.Add(AutomationElement.ProcessIdProperty);
                // cache patterns?

                // cacheRequest.Activate();
                cacheRequest.Push();

                switch (eventType.ProgrammaticName) {
                    case "InvokePatternIdentifiers.InvokedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the InvokedEvent handler");
                        Automation.AddAutomationEventHandler(
                            InvokePattern.InvokedEvent,
                            inputObject,
                            TreeScope.Element, // TreeScope.Subtree, // TreeScope.Element,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "TextPatternIdentifiers.TextChangedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the TextChangedEvent handler");
                        Automation.AddAutomationEventHandler(
                            TextPattern.TextChangedEvent,
                            inputObject,
                            TreeScope.Element,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "TextPatternIdentifiers.TextSelectionChangedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the TextSelectionChangedEvent handler");
                        Automation.AddAutomationEventHandler(
                            TextPattern.TextSelectionChangedEvent,
                            inputObject,
                            TreeScope.Element,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "WindowPatternIdentifiers.WindowOpenedProperty":
                        this.WriteVerbose(cmdlet, "subscribing to the WindowOpenedEvent handler");
                        Automation.AddAutomationEventHandler(
                            WindowPattern.WindowOpenedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.AutomationPropertyChangedEvent":
                        if (prop != null) {
                            this.WriteVerbose(cmdlet, "subscribing to the AutomationPropertyChangedEvent handler");
                            Automation.AddAutomationPropertyChangedEventHandler(
                                inputObject,
                                TreeScope.Subtree,
                                uiaPropertyChangedEventHandler =
                                    // new AutomationPropertyChangedEventHandler(OnUIAutomationPropertyChangedEvent),
                                    //new AutomationPropertyChangedEventHandler(handler),
                                    //new AutomationPropertyChangedEventHandler(((EventCmdletBase)cmdlet).AutomationPropertyChangedEventHandler),
                                    new AutomationPropertyChangedEventHandler(cmdlet.AutomationPropertyChangedEventHandler),
                                prop);
                            UIAHelper.WriteEventToCollection(cmdlet, uiaPropertyChangedEventHandler);
                            // 20130327
                            //this.WriteObject(cmdlet, uiaPropertyChangedEventHandler);
                            if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaPropertyChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        }
                        break;
                    case "AutomationElementIdentifiers.StructureChangedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the StructureChangedEvent handler");
                        Automation.AddStructureChangedEventHandler(
                            inputObject,
                            TreeScope.Subtree,
                            uiaStructureChangedEventHandler =
                            // new StructureChangedEventHandler(OnUIStructureChangedEvent));
                            //new StructureChangedEventHandler(handler));
                            //new StructureChangedEventHandler(((EventCmdletBase)cmdlet).StructureChangedEventHandler));
                            new StructureChangedEventHandler(cmdlet.StructureChangedEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaStructureChangedEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaStructureChangedEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaStructureChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "WindowPatternIdentifiers.WindowClosedProperty":
                        this.WriteVerbose(cmdlet, "subscribing to the WindowClosedEvent handler");
                        Automation.AddAutomationEventHandler(
                            WindowPattern.WindowClosedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            // uiaEventHandler = new AutomationEventHandler(OnUIAutomationEvent));
                            //uiaEventHandler = new AutomationEventHandler(handler));
                            //uiaEventHandler = new AutomationEventHandler(((EventCmdletBase)cmdlet).AutomationEventHandler));
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.MenuClosedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the MenuClosedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.MenuClosedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.MenuOpenedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the MenuOpenedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.MenuOpenedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.ToolTipClosedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the ToolTipClosedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.ToolTipClosedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.ToolTipOpenedEvent":
                        this.WriteVerbose(cmdlet, "subscribing to the ToolTipOpenedEvent handler");
                        Automation.AddAutomationEventHandler(
                            AutomationElement.ToolTipOpenedEvent,
                            inputObject,
                            TreeScope.Subtree,
                            uiaEventHandler = new AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.AutomationFocusChangedEvent":
                        WriteVerbose(cmdlet, "subscribing to the AutomationFocusChangedEvent handler");
                        Automation.AddAutomationFocusChangedEventHandler(
                            //AutomationElement.AutomationFocusChangedEvent,
                            //inputObject,
                            //System.Windows.Automation.AutomationElement.RootElement,
                            //TreeScope.Subtree,
                            uiaFocusChangedEventHandler = new AutomationFocusChangedEventHandler(cmdlet.AutomationEventHandler));
                        UIAHelper.WriteEventToCollection(cmdlet, uiaFocusChangedEventHandler);
                        // 20130327
                        //this.WriteObject(cmdlet, uiaFocusChangedEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaFocusChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    default:
                        this.WriteVerbose(cmdlet,
                                     "the following event has not been subscribed to: " +
                                     eventType.ProgrammaticName);
                        break;
                }
                this.WriteVerbose(cmdlet, "on the object " + inputObject.Current.Name);
                cacheRequest.Pop();

            }
            catch (Exception e) {
            // try {
            // ErrorRecord err = new ErrorRecord(
            // e,
            // "RegisteringEvent",
            // ErrorCategory.OperationStopped,
            // inputObject);
            // err.ErrorDetails =
            // new ErrorDetails("Unable to register event handler " +
            //  // handler.ToString());
            // eventType.ProgrammaticName +
            // " for " +
            // inputObject.Current.Name);
            //  // this.OnSuccessAction.ToString());
            // WriteError(this, err, false);
            // }
            // catch {
            // ErrorRecord err = new ErrorRecord(
            // e,
            // "RegisteringEvent",
            // ErrorCategory.OperationStopped,
            // inputObject);
            // err.ErrorDetails =
            // new ErrorDetails("Unable to register event handler " +
            // eventType.ProgrammaticName);;
            // WriteError(this, err, false);
            // }

                WriteVerbose(cmdlet,
                              "Unable to register event handler " +
                              eventType.ProgrammaticName +
                              " for " +
                              inputObject.Current.Name);
                 WriteVerbose(cmdlet,
                              e.Message);
            }
        }
 public void GetCachedPatternTest()
 {
     CacheRequest req = new CacheRequest();
     req.Add(InvokePatternIdentifiers.Pattern);
     using (req.Activate())
     {
         InvokePattern pattern = (InvokePattern)GetStartButton().GetCachedPattern(InvokePatternIdentifiers.Pattern);
         Assert.IsNotNull(pattern);
     }
 }
示例#10
0
 public void GetCachedPatternTest()
 {
     CacheRequest req = new CacheRequest();
     req.Add(LegacyIAccessiblePattern.Pattern);
     using (req.Activate())
     {
         LegacyIAccessiblePattern pattern = (LegacyIAccessiblePattern)GetTaskbar().GetCachedPattern(LegacyIAccessiblePattern.Pattern);
         Assert.IsNotNull(pattern);
     }
 }
示例#11
0
        public void GetUpdatedCacheTest()
        {
            AutomationElement elem = AutomationElement.RootElement;
            try
            {
                string name = elem.Cached.Name;
                Assert.Fail("expected exception");
            }
            catch (ArgumentException)
            {
            }

            CacheRequest req = new CacheRequest();
            req.Add(AutomationElement.NameProperty);
            AutomationElement refreshed = elem.GetUpdatedCache(req);
            string name2 = refreshed.Cached.Name;
        }
示例#12
0
        public void FindAllTest()
        {
            // Find all children
            CacheRequest cacheReq = new CacheRequest();
            cacheReq.Add(AutomationElement.NameProperty);
            cacheReq.Add(AutomationElement.NativeWindowHandleProperty);
            using (cacheReq.Activate())
            {
                AutomationElementCollection actual = AutomationElement.RootElement.FindAll(
                    TreeScope.Children,
                    Condition.TrueCondition);
                Assert.IsNotNull(actual);
                Assert.IsTrue(actual.Count > 0);

                foreach (AutomationElement elem in actual)
                {
                    Assert.IsNotNull(elem);
                    int nativeHwnd = (int)elem.GetCachedPropertyValue(AutomationElement.NativeWindowHandleProperty);
                    Assert.IsTrue(nativeHwnd != 0);
                }
            }
        }
示例#13
0
 public void FindFirstTest()
 {
     // Find a child
     CacheRequest cacheReq = new CacheRequest();
     cacheReq.Add(AutomationElement.NativeWindowHandleProperty);
     using (cacheReq.Activate())
     {
         Assert.AreSame(CacheRequest.Current, cacheReq);
         AutomationElement actualCached = AutomationElement.RootElement.FindFirst(
             TreeScope.Children,
             Condition.TrueCondition);
         Assert.IsNotNull(actualCached);
         int nativeHwnd = (int)actualCached.GetCachedPropertyValue(AutomationElement.NativeWindowHandleProperty);
         Assert.IsTrue(nativeHwnd != 0);
     }
 }
示例#14
0
 public void AddTest()
 {
     CacheRequest target = new CacheRequest();
     target.Add(AutomationElement.HelpTextProperty);
     target.Add(ExpandCollapsePatternIdentifiers.Pattern);
 }
示例#15
0
        public void DoWork()
        {
            long timeToGetUncached = 0;
            long timeToGetCached = 0;

            // Create a System.Diagnostics.Stopwatch.
            var stopWatchTimer = new Stopwatch();


            // TEST 1: Get the target element without caching, and retrieve
            //  current properties.

            stopWatchTimer.Start();
            AutomationElement targetNoCache = null;
            try
            {
                targetNoCache = AutomationElement.FromPoint(_targetPoint);
            }
            catch (ElementNotAvailableException)
            {
                OutputLine("Could not retrieve element.");
                return;
            }

            // Get current properties.
            _currentPropCount = 0;
            GetCurrentProperties(targetNoCache, 0);
            stopWatchTimer.Stop();
            timeToGetUncached = stopWatchTimer.Elapsed.Ticks;


            // TEST 2: Get the target element with caching, and retrieve
            //   cached properties.

            // Create CacheRequest.
            var fetchRequest = new CacheRequest();

            // Add properties to fetch.
            fetchRequest.Add(AutomationElement.NameProperty);
            fetchRequest.Add(AutomationElement.AutomationIdProperty);
            fetchRequest.Add(AutomationElement.ControlTypeProperty);
            fetchRequest.Add(AutomationElement.FrameworkIdProperty);
            fetchRequest.Add(AutomationElement.IsContentElementProperty);

            // Set options.
            fetchRequest.AutomationElementMode = _mode;
            fetchRequest.TreeScope = _treeScope;
            fetchRequest.TreeFilter = Automation.RawViewCondition;

            // Activate the CacheRequest and fetch the target.
            AutomationElement targetCached = null;
            using (fetchRequest.Activate())
            {
                stopWatchTimer.Reset();
                stopWatchTimer.Start();
                try
                {
                    targetCached = AutomationElement.FromPoint(_targetPoint);
                }
                catch (InvalidOperationException)
                {
                    OutputLine("InvalidOperationException. Could not retrieve element.");
                    return;
                }
                catch (ElementNotAvailableException)
                {
                    OutputLine("ElementNotAvailableException. Could not retrieve element.");
                    return;
                }
            } // CacheRequest is now inactive.

            // Get cached properties.
            GetCachedProperties(targetCached, true);
            stopWatchTimer.Stop();
            timeToGetCached = stopWatchTimer.Elapsed.Ticks;

            // TEST 3: Get updated cache.

            stopWatchTimer.Reset();
            stopWatchTimer.Start();
            var cacheUpdated = false;
            if (_mode == AutomationElementMode.Full)
            {
                var updatedTargetCached = targetCached.GetUpdatedCache(fetchRequest);
                GetCachedProperties(updatedTargetCached, false);
                // Fetches were counted again, so divide count by 2.
                _cachedPropCount /= 2;
                cacheUpdated = true;
                stopWatchTimer.Stop();
            }
            var updateTicks = stopWatchTimer.Elapsed.Ticks;

            // END OF TESTS. 

            // Display results

            var nameProperty = targetNoCache.Current.Name;
            var framework = targetNoCache.Current.FrameworkId;
            OutputLine("Name: " + nameProperty);
            OutputLine("Framework: " + framework);
            OutputLine(_elementCount + " cached element(s).");

            OutputLine(timeToGetUncached.ToString("N0") + " Ticks to retrieve element(s) and get "
                       + _currentPropCount + " current properties.");
            OutputLine(timeToGetCached.ToString("N0") + " Ticks to retrieve element(s) and get "
                       + _cachedPropCount + " cached properties.");

            // Show ratio between current and cached performance.
            var ratio = timeToGetUncached/(float) timeToGetCached;
            if (ratio > 2)
            {
                OutputLine("Current:Cached = " + ratio.ToString("N0") + ":1");
            }
            else
            {
                // Print with decimal.
                OutputLine("Current:Cached = " + ratio.ToString("N1") + ":1");
            }
            if (cacheUpdated)
            {
                OutputLine(updateTicks.ToString("N0") + " Ticks to update cache and get properties.");
            }
            else
            {
                OutputLine("Cannot update cache in None mode.");
            }
            OutputLine("");
        }
示例#16
0
        public void RangeValuePatternCachedTest()
        {
            using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL main.cpl ,2"))
            {
                CacheRequest req = new CacheRequest();
                req.Add(RangeValuePattern.Pattern);
                req.Add(RangeValuePattern.IsReadOnlyProperty);
                req.Add(RangeValuePattern.MaximumProperty);
                req.Add(RangeValuePattern.MinimumProperty);
                req.Add(RangeValuePattern.SmallChangeProperty);
                req.Add(RangeValuePattern.LargeChangeProperty);
                using (req.Activate())
                {
                    // Find a well-known slider
                    AutomationElement slider = host.Element.FindFirst(TreeScope.Subtree,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, "101"));
                    Assert.IsNotNull(slider);

                    RangeValuePattern range = (RangeValuePattern)slider.GetCachedPattern(RangeValuePattern.Pattern);
                    double originalValue = range.Cached.Value;
                    Assert.IsTrue(range.Cached.SmallChange >= 0);
                    Assert.IsTrue(range.Cached.LargeChange >= 0);
                    Assert.IsTrue(originalValue >= range.Cached.Minimum);
                    Assert.IsTrue(originalValue <= range.Cached.Maximum);
                    Assert.IsFalse(range.Cached.IsReadOnly);
                }
            }
        }
示例#17
0
        public void WindowPatternCachedTest()
        {
            using (AppHost host = new AppHost("notepad.exe", ""))
            {
                CacheRequest req = new CacheRequest();
                req.Add(WindowPattern.Pattern);
                req.Add(WindowPattern.CanMaximizeProperty);
                req.Add(WindowPattern.CanMinimizeProperty);
                req.Add(WindowPattern.IsTopmostProperty);
                req.Add(WindowPattern.WindowInteractionStateProperty);
                req.Add(WindowPattern.WindowVisualStateProperty);
                using (req.Activate())
                {
                    AutomationElement cachedEl = host.Element.GetUpdatedCache(req);

                    // Window Pattern
                    WindowPattern windowPattern = (WindowPattern)cachedEl.GetCachedPattern(WindowPattern.Pattern);
                    Assert.IsTrue(windowPattern.Cached.CanMaximize);
                    Assert.IsTrue(windowPattern.Cached.CanMinimize);
                    Assert.IsFalse(windowPattern.Cached.IsTopmost);
                    Assert.AreNotEqual(windowPattern.Cached.WindowVisualState, WindowVisualState.Minimized);
                    Assert.AreNotEqual(windowPattern.Cached.WindowInteractionState, WindowInteractionState.Closing);
                }
            }
        }
示例#18
0
        public void ValuePatternCachedTest()
        {
            using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL intl.cpl"))
            {
                CacheRequest req = new CacheRequest();
                req.Add(WindowPattern.Pattern);
                req.Add(WindowPattern.CanMaximizeProperty);
                req.Add(WindowPattern.CanMinimizeProperty);
                req.Add(WindowPattern.IsTopmostProperty);
                req.Add(WindowPattern.WindowInteractionStateProperty);
                req.Add(WindowPattern.WindowVisualStateProperty);
                using (req.Activate())
                {
                    // Find a well-known combo box
                    AutomationElement combo = host.Element.FindFirst(TreeScope.Subtree,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, "1021"));
                    Assert.IsNotNull(combo);

                    ValuePattern value = (ValuePattern)combo.GetCurrentPattern(ValuePattern.Pattern);
                    Assert.IsFalse(value.Current.IsReadOnly);
                    Assert.IsTrue(value.Current.Value.Length > 0);
                }
            }
        }
示例#19
0
        public void TransformPatternCachedTest()
        {
            using (AppHost host = new AppHost("notepad.exe", ""))
            {
                CacheRequest req = new CacheRequest();
                req.Add(TransformPattern.Pattern);
                req.Add(TransformPattern.CanMoveProperty);
                req.Add(TransformPattern.CanResizeProperty);
                req.Add(TransformPattern.CanRotateProperty);
                using (req.Activate())
                {
                    AutomationElement cachedEl = host.Element.GetUpdatedCache(req);

                    TransformPattern transformPattern = (TransformPattern)cachedEl.GetCachedPattern(TransformPattern.Pattern);
                    // Coded to expectations for an explorer window
                    Assert.IsTrue(transformPattern.Cached.CanMove);
                    Assert.IsTrue(transformPattern.Cached.CanResize);
                    Assert.IsFalse(transformPattern.Cached.CanRotate);

                    // Little move
                    transformPattern.Move(10, 10);

                    // Little resize
                    transformPattern.Resize(200, 200);
                }
            }
        }
示例#20
0
        public void TogglePatternCachedTest()
        {
            using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL main.cpl"))
            {
                CacheRequest req = new CacheRequest();
                req.Add(TogglePattern.Pattern);
                req.Add(TogglePattern.ToggleStateProperty);
                using (req.Activate())
                {
                    // Find a well-known checkbox
                    AutomationElement checkbox = host.Element.FindFirst(TreeScope.Subtree,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, "114"));
                    Assert.IsNotNull(checkbox);

                    TogglePattern toggle = (TogglePattern)checkbox.GetCachedPattern(TogglePattern.Pattern);
                    ToggleState originalState = toggle.Cached.ToggleState;
                    Assert.IsTrue(originalState == ToggleState.On || originalState == ToggleState.Off);
                }
            }
        }
        public void MultipleViewPatternTest()
        {
            CacheRequest req = new CacheRequest();
            req.Add(MultipleViewPattern.Pattern);
            req.Add(MultipleViewPattern.CurrentViewProperty);
            req.Add(MultipleViewPattern.SupportedViewsProperty);

            using (req.Activate())
            {
                AutomationElement itemsView = ExplorerTargetTests.explorerHost.Element.FindFirst(TreeScope.Subtree,
                    new PropertyCondition(AutomationElement.ClassNameProperty, "UIItemsView"));
                Assert.IsNotNull(itemsView);

                MultipleViewPattern multiView = (MultipleViewPattern)itemsView.GetCachedPattern(MultipleViewPattern.Pattern);
                int[] supportedViews = multiView.Cached.GetSupportedViews();
                Assert.IsNotNull(supportedViews.Length > 0);
                bool inSupportedViews = false;
                foreach (int view in supportedViews)
                {
                    if (view == multiView.Cached.CurrentView)
                    {
                        inSupportedViews = true;
                        break;
                    }
                    string viewName = multiView.GetViewName(view);
                    Assert.IsTrue(viewName.Length > 0);
                }
                Assert.IsTrue(inSupportedViews);
            }
        }
示例#22
0
        public void LegacyIAccessiblePatternCachedTest()
        {
            using (AppHost host = new AppHost("rundll32.exe", "shell32.dll,Control_RunDLL intl.cpl"))
            {
                CacheRequest req = new CacheRequest();
                req.Add(LegacyIAccessiblePattern.Pattern);
                req.Add(LegacyIAccessiblePattern.NameProperty);
                req.Add(LegacyIAccessiblePattern.ChildIdProperty);
                req.Add(LegacyIAccessiblePattern.KeyboardShortcutProperty);
                req.Add(LegacyIAccessiblePattern.RoleProperty);
                req.Add(LegacyIAccessiblePattern.ValueProperty);
                req.Add(LegacyIAccessiblePattern.StateProperty);
                req.Add(LegacyIAccessiblePattern.SelectionProperty);
                using (req.Activate())
                {
                    // Find a well-known combo box
                    AutomationElement combo = host.Element.FindFirst(TreeScope.Subtree,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, "1021"));
                    Assert.IsNotNull(combo);

                    // BLOCK
                    {
                        LegacyIAccessiblePattern acc = (LegacyIAccessiblePattern)combo.GetCachedPattern(LegacyIAccessiblePattern.Pattern);
                        Assert.AreEqual(0, acc.Cached.ChildId);
                        Assert.IsTrue(acc.Cached.Name.Length > 0);
                        Assert.IsTrue(acc.Cached.Value.Length > 0);
                        Assert.AreEqual("Alt+f", acc.Cached.KeyboardShortcut);
                        Assert.AreEqual((uint)0x2E /* combo box */, acc.Cached.Role);
                        Assert.AreEqual((uint)0x100400 /* collapsed|focusable */, acc.Cached.State & 0x100400);
                    }

                    // Get the tab controls
                    AutomationElement tabCtrl = host.Element.FindFirst(TreeScope.Subtree,
                        new PropertyCondition(AutomationElement.AutomationIdProperty, "12320"));
                    Assert.IsNotNull(tabCtrl);

                    // BLOCK
                    {
                        LegacyIAccessiblePattern acc = (LegacyIAccessiblePattern)tabCtrl.GetCachedPattern(LegacyIAccessiblePattern.Pattern);
                        AutomationElement[] selection = acc.Cached.GetSelection();
                        Assert.IsTrue(selection.Length > 0);
                        Assert.IsInstanceOf<AutomationElement>(selection[0]);
                        Assert.AreEqual(ControlType.TabItem, selection[0].Current.ControlType);
                    }

                }
            }
        }
示例#23
0
        /// -------------------------------------------------------------------
        /// <summary>HELPER: Generates a random cache</summary>
        /// -------------------------------------------------------------------
        static CacheRequest RandomCache()
        {
            Random rnd = new Random((int)DateTime.Now.Ticks);
            StringBuilder sb = new StringBuilder();
            CacheRequest cache = new CacheRequest();
            AutomationPattern p1;
            AutomationProperty p2;
            ArrayList propertyList = null;
            ArrayList patternList = null;

            PopulateAutomationElementProperties(ref propertyList, ref patternList);

            try
            {
                // Decide whether to add patterns
                if (rnd.Next(2) == 0)
                {
                    // Add up to two patterns
                    int maxIndex = rnd.Next(2);
                    for (int index = 0; index < maxIndex; index++)
                    {
                        try
                        {

                            if (null != (p1 = (AutomationPattern)patternList[rnd.Next(patternList.Count)]))
                            {
                                sb.Append(p1.ProgrammaticName + ":");
                                cache.Add(p1);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                // Decide whether to add properties
                if (rnd.Next(2) == 0)
                {
                    // Add up to three AutomationProperty
                    int maxIndex = rnd.Next(3);
                    for (int index = 0; index < maxIndex; index++)
                    {
                        try
                        {
                            if (null != (p2 = (AutomationProperty)propertyList[(rnd.Next(propertyList.Count))]))
                            {
                                sb.Append(p2.ProgrammaticName + ":");
                                cache.Add(p2);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

                switch (rnd.Next(2))
                {
                    case 0:
                        cache.AutomationElementMode = AutomationElementMode.Full;
                        sb.Append(AutomationElementMode.Full + ":");
                        break;
                    case 1:
                        cache.AutomationElementMode = AutomationElementMode.None;
                        sb.Append(AutomationElementMode.None + ":");
                        break;
                    default:
                        throw new Exception("Bad test code(AutomationElementMode)");
                }

                switch (rnd.Next(6))
                {
                    case 0:
                        cache.TreeScope = TreeScope.Ancestors;
                        sb.Append(TreeScope.Ancestors + ":");
                        break;
                    case 1:
                        cache.TreeScope = TreeScope.Children;
                        sb.Append(TreeScope.Children + ":");
                        break;
                    case 2:
                        cache.TreeScope = TreeScope.Descendants;
                        sb.Append(TreeScope.Descendants + ":");
                        break;
                    case 3:
                        cache.TreeScope = TreeScope.Element;
                        sb.Append(TreeScope.Element + ":");
                        break;
                    case 4:
                        cache.TreeScope = TreeScope.Parent;
                        sb.Append(TreeScope.Parent + ":");
                        break;
                    case 5:
                        cache.TreeScope = TreeScope.Subtree;
                        sb.Append(TreeScope.Subtree + ":");
                        break;
                    default:
                        throw new Exception("Bad test code(treescope)");
                }
                //switch (rnd.Next(3))
                //{
                //    case 0:
                //        cache.TreeFilter = NotCondition.;
                //        break;
                //    case 1:
                //        cache.TreeFilter = AndCondition;
                //        break;
                //    case 2:
                //        cache.TreeFilter = OrCondition;
                //        break;

                //}
            }
            catch (ArgumentException)
            {
                // Some TreeScopes are invalid
            }

            Dump(sb.ToString(), false, null);
            return cache;
        }
示例#24
0
 public AutomationElement GetUpdatedAEWhenStructureChanged(AutomationElement mainWidow, AutomationPattern patternToCache = null, AutomationProperty propertyToCache = null)
 {
     CacheRequest cacheRequest = new CacheRequest();
     cacheRequest.TreeScope = TreeScope.Element | TreeScope.Descendants | TreeScope.Children;
     cacheRequest.AutomationElementMode = AutomationElementMode.Full;
     cacheRequest.TreeFilter = System.Windows.Automation.Automation.RawViewCondition;
     if (patternToCache != null)
     {
         cacheRequest.Add(patternToCache);
     }
     else
     {
         cacheRequest.Add(SelectionPattern.Pattern);
         cacheRequest.Add(WindowPattern.Pattern);
         cacheRequest.Add(InvokePattern.Pattern);
         cacheRequest.Add(TogglePattern.Pattern);
         cacheRequest.Add(ExpandCollapsePattern.Pattern);
         cacheRequest.Add(ValuePattern.Pattern);
         cacheRequest.Add(SelectionItemPattern.Pattern);
     }
     if (propertyToCache != null)
     {
         cacheRequest.Add(propertyToCache);
     }
     else
     {
         cacheRequest.Add(AutomationElement.NameProperty);
         cacheRequest.Add(AutomationElement.AutomationIdProperty);
         cacheRequest.Add(AutomationElement.ClassNameProperty);
         cacheRequest.Add(AutomationElement.ControlTypeProperty);
     }
     AutomationElement updatedElement;
     using (cacheRequest.Activate())
     {
         updatedElement = mainWidow.GetUpdatedCache(cacheRequest);
     }
     return updatedElement;
 }