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");
}