AddAutomationEventHandler() public static method

public static AddAutomationEventHandler ( AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler ) : void
eventId AutomationEvent
element AutomationElement
scope TreeScope
eventHandler AutomationEventHandler
return void
Beispiel #1
0
        public void Z_AutomationEventTest()
        {
            var automationEventsArray = new [] {
                new { Sender = (object)null, Args = (AutomationEventArgs)null }
            };
            var automationEvents = automationEventsArray.ToList();

            automationEvents.Clear();

            AutomationEventHandler handler =
                (o, e) => automationEvents.Add(new { Sender = o, Args = e });

            SelectionItemPattern item1 = (SelectionItemPattern)child1Element.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern);

            item1.Select();

            AutomationEvent eventId = SelectionItemPattern.ElementSelectedEvent;

            At.AddAutomationEventHandler(eventId,
                                         treeView1Element, TreeScope.Descendants, handler);

            SelectionItemPattern item2 = (SelectionItemPattern)child2Element.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern);

            item2.Select();
            Thread.Sleep(500);
            At.RemoveAutomationEventHandler(eventId, treeView1Element, handler);
            Assert.AreEqual(1, automationEvents.Count, "event count");
            Assert.AreEqual(child2Element, automationEvents [0].Sender, "event sender");
            Assert.AreEqual(SelectionItemPattern.ElementSelectedEvent, automationEvents [0].Args.EventId, "EventId");
            automationEvents.Clear();

            item1.Select();
            Thread.Sleep(500);
            Assert.AreEqual(0, automationEvents.Count, "event count");
        }
Beispiel #2
0
        public void InvokeEventTest()
        {
            int eventCount = 0;
            AutomationEventHandler handler = (o, e) => eventCount++;

            At.AddAutomationEventHandler(InvokePattern.InvokedEvent, button1Element,
                                         TreeScope.Element, handler);

            AssertRaises <ArgumentException> (
                () => At.RemoveAutomationEventHandler(AutomationElementIdentifiers.AutomationPropertyChangedEvent, button1Element, handler),
                "AutomationPropertyChangedEvent is not valid");

            //Shall have no effect.
            At.RemoveAutomationEventHandler(InvokePattern.InvokedEvent, testFormElement, handler);
            RunCommand("click button1");
            Assert.AreEqual(1, eventCount, "Invoke event fired");

            eventCount = 0;
            At.RemoveAutomationEventHandler(InvokePattern.InvokedEvent, button1Element, handler);
            RunCommand("click button1");
            Assert.AreEqual(0, eventCount, "Invoke event not fired");
            eventCount = 0;
            //Test for add the same handler again.
            At.AddAutomationEventHandler(InvokePattern.InvokedEvent, button1Element,
                                         TreeScope.Element, handler);
            At.AddAutomationEventHandler(InvokePattern.InvokedEvent, button1Element,
                                         TreeScope.Element, handler);
            RunCommand("click button1");
            Assert.AreEqual(2, eventCount, "Invoke event fired");

            eventCount = 0;
            At.RemoveAllEventHandlers();
            RunCommand("click button1");
            Assert.AreEqual(0, eventCount, "Invoke event not fired");
        }
Beispiel #3
0
        public void InvokeTest()
        {
            int eventCount = 0;
            AutomationEventHandler handler = (o, e) => eventCount++;

            At.AddAutomationEventHandler(InvokePattern.InvokedEvent, button1Element,
                                         TreeScope.Element, handler);

            InvokePattern pattern = (InvokePattern)button1Element.GetCurrentPattern(InvokePattern.Pattern);

            pattern.Invoke();
            if (Atspi)
            {
                Thread.Sleep(200);
            }
            Assert.AreEqual("button1_click",
                            textbox1Element.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty),
                            "textBox1's text is modified after button1 is clicked");
            Assert.AreEqual("button1_click",
                            label1Element.GetCurrentPropertyValue(AutomationElementIdentifiers.NameProperty),
                            "label1's text is modified after button1 is clicked");
            if (Atspi)
            {
                Assert.AreEqual(1, eventCount, "Invoke event fired");
            }
        }
Beispiel #4
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");
        }
Beispiel #5
0
        public void EventTest()
        {
            int eventCount = 0;
            AutomationEventHandler handler = (o, e) => eventCount++;

            At.AddAutomationEventHandler(InvokePattern.InvokedEvent,
                                         child, TreeScope.Element, handler);
            childProvider.PerformInvoke();
            Thread.Sleep(500);
            Assert.AreEqual(1, eventCount);
        }
Beispiel #6
0
        public void TextSelectionChangedEvent()
        {
            int eventCount = 0;
            AutomationEventHandler handler = (o, e) => eventCount++;

            At.AddAutomationEventHandler(TextPattern.TextSelectionChangedEvent, textbox3Element,
                                         TreeScope.Element, handler);
            RunCommand("select textbox3");
            Assert.AreEqual(1, eventCount, "TextSelectionChangedEvent fired");

            At.RemoveAutomationEventHandler(TextPattern.TextSelectionChangedEvent, textbox3Element, handler);
        }
Beispiel #7
0
        //todo this test will fail since we won't even fire the TextChangedEvent at
        //provider/bridge side
        public void TextChangedEvent()
        {
            int eventCount = 0;
            AutomationEventHandler handler = (o, e) => eventCount++;

            At.AddAutomationEventHandler(TextPattern.TextChangedEvent, textbox3Element,
                                         TreeScope.Element, handler);
            RunCommand("set textbox3 text");
            Thread.Sleep(500);
            // Ideally we should only receive one event, but at-spi
            // generates a text-changed::delete followed by a
            // text-changed::insert.
            int expectedEventCount = (Atspi? 2: 1);

            Assert.AreEqual(expectedEventCount, eventCount, "TextChangedEvent fired");

            At.RemoveAutomationEventHandler(TextPattern.TextChangedEvent, textbox3Element, handler);
        }
        public void SelectionPropertyChangedEventTest()
        {
            int propertyEventCount  = 0;
            int selectionEventCount = 0;
            AutomationPropertyChangedEventHandler propertyHandler =
                (o, e) => { propertyEventCount++; };
            AutomationEventHandler selectionEventHandler =
                (o, e) => { selectionEventCount++; };

            var childElement = treeView1Element.FindFirst(TreeScope.Children,
                                                          new PropertyCondition(AEIds.ControlTypeProperty,
                                                                                ControlType.TreeItem));

            At.AddAutomationPropertyChangedEventHandler(treeView1Element, TreeScope.Subtree,
                                                        propertyHandler, SelectionPattern.SelectionProperty);
            At.AddAutomationEventHandler(SelectionItemPattern.ElementSelectedEvent,
                                         childElement, TreeScope.Element, selectionEventHandler);

            var pattern = (SelectionPattern)
                          treeView1Element.GetCurrentPattern(SelectionPatternIdentifiers.Pattern);

            Assert.IsNotNull(pattern, "selectionPattern should not be null");
            var current = pattern.Current;

            AutomationElement [] selection = current.GetSelection();
            Assert.AreEqual(0, selection.Length, "Selection length");

            var selectionItemPattern = (SelectionItemPattern)
                                       childElement.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern);

            selectionItemPattern.Select();
            Thread.Sleep(1000);

            selection = current.GetSelection();
            Assert.AreEqual(1, selection.Length, "Selection length");
            Assert.AreEqual(0, propertyEventCount, "# of SelectionProperty changed event");
            Assert.AreEqual(1, selectionEventCount, "# of selection event");
        }
        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"); });
        }