public void PushPopTest() { CacheRequest defaultCR = CacheRequest.Current; CacheRequest target = new CacheRequest(); target.TreeScope = TreeScope.Children; target.Push(); CacheRequest target2 = new CacheRequest(); target2.TreeScope = TreeScope.Subtree; target2.Push(); // Try to change target2 - this should fail try { target2.TreeScope = TreeScope.Descendants; Assert.Fail("exception expected"); } catch (System.InvalidOperationException) { } target2.Pop(); target.Pop(); Assert.AreEqual(CacheRequest.Current, defaultCR); }
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); } }