internal AutomationElementTreeNode FindOrInsertChildElement(AutomationElement automationElement)
{
Debug.WriteLine("InsertChildElement ENTER");
// this variable will hold the currentTestTypeRootNode for inserted automationElement
AutomationElementTreeNode node = null;
// if there is some currentTestTypeRootNode inserted before then try to find if it is not currentTestTypeRootNode for this automationElement
if (this._newChildElementInserted || this._childrenStatus == ChildrenElementsStatus.Populated)
{
Debug.WriteLine("finding currentTestTypeRootNode for the element");
try
{
node = FindChildNodeForAutomationElementInternal(automationElement);
}
catch { /* it does not matter */ }
}
//if we didn't find child currentTestTypeRootNode for automationElement then create it
if (node == null)
{
Debug.WriteLine("creating new currentTestTypeRootNode for the element");
TreeNode newNode = CreateTreeNodeForAutomationElement(automationElement);
this._newChildElementInserted = true;
//add this new children to collection; we can call clear when there is no element inserted before
//because this method does not have to be synchronized, we call AddChildrenNodesToTree directly
//without using SyncManager
if(this.AutomationElementTreeControl.InvokeRequired)
{
this.AutomationElementTreeControl.Invoke(
new AddChildrenNodesToTreeNodeDelegate(AddChildrenNodesToTreeNodeInternal),
new TreeNode[] { newNode }, false, true);
}
else
{
AddChildrenNodesToTreeNodeInternal(new TreeNode[] { newNode }, false, true);
}
// AddChildrenNodesToTreeNode(new TreeNode[] { newNode }, false, true);
node = (AutomationElementTreeNode)newNode.Tag;
}
Debug.WriteLine("InsertChildElement EXIT");
return node;
}