AddStructureChangedEventHandler() public static method

public static AddStructureChangedEventHandler ( AutomationElement element, TreeScope scope, StructureChangedEventHandler eventHandler ) : void
element AutomationElement
scope TreeScope
eventHandler StructureChangedEventHandler
return void
Beispiel #1
0
        public void StructureEventTest()
        //this test also tested WindowPattern.WindowOpenedEvent (i.e. AddAutomationEventHandler)
        {
            int                    automationEventCount = 0;
            int                    structureEventCount  = 0;
            AutomationEvent        eventId = null;
            AutomationEventHandler automationEventHandler = (o, e) =>
            {
                automationEventCount++;
                eventId = e.EventId;
            };

            At.AddAutomationEventHandler(
                WindowPattern.WindowOpenedEvent,
                AutomationElement.RootElement, TreeScope.Children,
                automationEventHandler);
            StructureChangedEventHandler structureEventHandler = (o, e) =>
            {
                if (e.StructureChangeType == StructureChangeType.ChildAdded)
                {
                    structureEventCount++;
                }
            };

            At.AddStructureChangedEventHandler(
                AutomationElement.RootElement, TreeScope.Children, structureEventHandler);
            int pid = OpenForm();

            Thread.Sleep(3000);
            Assert.AreEqual(1, structureEventCount, "[OpenForm] count of StructureChangedEvent");
            Assert.AreEqual(1, automationEventCount, "[OpenForm] count of WindowOpenedEvent");
            Assert.AreEqual(WindowPattern.WindowOpenedEvent, eventId);

            automationEventCount = 0;
            structureEventCount  = 0;
            At.RemoveAllEventHandlers();
            int pid2 = OpenForm();

            Thread.Sleep(3000);
            Assert.AreEqual(0, structureEventCount);
            Assert.AreEqual(0, automationEventCount);

            structureEventHandler = (o, e) =>
            {
                structureEventCount++;
            };
            At.AddStructureChangedEventHandler(
                AutomationElement.RootElement, TreeScope.Children, structureEventHandler);
            CloseForm(pid);
            CloseForm(pid2);
            Thread.Sleep(3000);
            // Note: I expect 2 events here (whose StructureChangeType are both ChildRemoved)
            // But as tested on Win 7, we'll actually get no event,
            // And with our current implementation, we'll get 4 events (i.e. besides the 2 expected events, we
            // get other 2 ChildRemoved events, whose sender is the "testFormElement")
            Assert.AreEqual(0, structureEventCount, "[CloseForm] count of StructureChangedEvent");
        }
        public void StructureEventTest()
        {
            List <AutomationElement>     elementEventSenders      = new List <AutomationElement> ();
            List <StructureChangeType>   elementEventChangeTypes  = new List <StructureChangeType> ();
            List <AutomationElement>     childrenEventSenders     = new List <AutomationElement> ();
            List <StructureChangeType>   childrenEventChangeTypes = new List <StructureChangeType> ();
            StructureChangedEventHandler elementHandler           = delegate(object sender, StructureChangedEventArgs args) {
                elementEventSenders.Add(sender as AutomationElement);
                elementEventChangeTypes.Add(args.StructureChangeType);
            };

            At.AddStructureChangedEventHandler(panel1Element, TreeScope.Element, elementHandler);
            StructureChangedEventHandler childrenHandler = delegate(object sender, StructureChangedEventArgs args) {
                childrenEventSenders.Add(sender as AutomationElement);
                childrenEventChangeTypes.Add(args.StructureChangeType);
            };

            At.AddStructureChangedEventHandler(panel1Element, TreeScope.Children, childrenHandler);
            InvokePattern addAction    = (InvokePattern)btnAddTextboxElement.GetCurrentPattern(InvokePattern.Pattern);
            InvokePattern removeAction = (InvokePattern)btnRemoveTextboxElement.GetCurrentPattern(InvokePattern.Pattern);

            addAction.Invoke();
            Thread.Sleep(1000);
            Assert.AreEqual(1, elementEventSenders.Count, "Check event count");
            Assert.AreEqual(panel1Element, elementEventSenders [0], "Check ChildrenInvalidated event sender");
            Assert.AreEqual(StructureChangeType.ChildrenInvalidated,
                            elementEventChangeTypes [0], "Check ChildrenInvalidated event type");
            Assert.AreEqual(1, childrenEventSenders.Count, "Check event count");
            Assert.AreEqual(StructureChangeType.ChildAdded,
                            childrenEventChangeTypes [0], "Check ChildAdded event type");
            removeAction.Invoke();
            Thread.Sleep(1000);
            Assert.AreEqual(3, elementEventSenders.Count, "Check event count");
            Assert.AreEqual(panel1Element, elementEventSenders [1], "Check event sender");
            Assert.AreEqual(panel1Element, elementEventSenders [2], "Check event sender");
            Assert.IsTrue((elementEventChangeTypes [1] == StructureChangeType.ChildRemoved &&
                           elementEventChangeTypes [2] == StructureChangeType.ChildrenInvalidated) ||
                          (elementEventChangeTypes [1] == StructureChangeType.ChildrenInvalidated &&
                           elementEventChangeTypes [2] == StructureChangeType.ChildRemoved),
                          "Check event type");
            addAction.Invoke();
            Thread.Sleep(1000);
            Assert.AreEqual(4, elementEventSenders.Count, "Check event count");
            Assert.AreEqual(panel1Element, elementEventSenders [3], "Check ChildrenInvalidated event sender");
            Assert.AreEqual(StructureChangeType.ChildrenInvalidated,
                            elementEventChangeTypes [3], "Check ChildrenInvalidated event type");
            Assert.AreEqual(2, childrenEventSenders.Count, "Check event count");
            Assert.AreEqual(StructureChangeType.ChildAdded,
                            childrenEventChangeTypes [1], "Check ChildAdded event type");

            At.RemoveStructureChangedEventHandler(panel1Element, elementHandler);
            At.RemoveStructureChangedEventHandler(panel1Element, childrenHandler);
            addAction.Invoke();
            Thread.Sleep(1000);
            Assert.AreEqual(4, elementEventSenders.Count, "Element event count didn't change");
            Assert.AreEqual(2, childrenEventSenders.Count, "Children event count didn't change");
        }
        public void ArgumentExceptionTest()
        {
            Action action = () => {
                At.AddAutomationEventHandler(InvokePattern.InvokedEvent,
                                             null, TreeScope.Element, (o, e) => {});
            };

            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as element to AddAutomationEventHandler");

            action = () => {
                At.AddAutomationPropertyChangedEventHandler(
                    null, TreeScope.Element, (o, e) => {}, AEIds.NameProperty);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as element to AddAutomationPropertyChangedEventHandler");

            action = () => {
                At.AddStructureChangedEventHandler(
                    null, TreeScope.Element, (o, e) => {});
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as element to AddStructureChangedEventHandler");

            action = () => {
                At.AddAutomationEventHandler(InvokePattern.InvokedEvent,
                                             button1Element, TreeScope.Element, null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to AddAutomationEventHandler");

            action = () => {
                At.AddAutomationPropertyChangedEventHandler(
                    button1Element, TreeScope.Element, null, AEIds.NameProperty);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to AddAutomationPropertyChangedEventHandler");

            action = () => {
                At.AddStructureChangedEventHandler(
                    button1Element, TreeScope.Element, null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to AddStructureChangedEventHandler");

            action = () => {
                At.AddAutomationFocusChangedEventHandler(null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to AddAutomationFocusChangedEventHandler");

            action = () => {
                At.RemoveAutomationEventHandler(InvokePattern.InvokedEvent,
                                                null, (o, e) => {});
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as element to RemoveAutomationEventHandler");

            action = () => {
                At.RemoveAutomationPropertyChangedEventHandler(
                    null, (o, e) => {});
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as element to RemoveAutomationPropertyChangedEventHandler");

            action = () => {
                At.RemoveStructureChangedEventHandler(
                    null, (o, e) => {});
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as element to RemoveStructureChangedEventHandler");

            action = () => {
                At.RemoveAutomationEventHandler(InvokePattern.InvokedEvent,
                                                button1Element, null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to RemoveAutomationEventHandler");

            action = () => {
                At.RemoveAutomationPropertyChangedEventHandler(
                    button1Element, null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to RemoveAutomationPropertyChangedEventHandler");

            action = () => {
                At.RemoveStructureChangedEventHandler(
                    button1Element, null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to RemoveStructureChangedEventHandler");

            action = () => {
                At.RemoveAutomationFocusChangedEventHandler(null);
            };
            AssertRaises <ArgumentNullException>(action,
                                                 "Pass null as handler to RemoveAutomationFocusChangedEventHandler");

            //Assert removing a non-existent handler won't fire any exception
            At.RemoveAutomationEventHandler(InvokePattern.InvokedEvent,
                                            button1Element, (o, e) => { Console.Write("nop"); });
            At.RemoveAutomationPropertyChangedEventHandler(
                button1Element, (o, e) => { Console.Write("nop"); });
            At.RemoveStructureChangedEventHandler(
                button1Element, (o, e) => { Console.Write("nop"); });
            At.RemoveAutomationFocusChangedEventHandler(
                (o, e) => { Console.Write("nop"); });
        }