/// <summary>
/// Check to see if the specified automation element meets the requirements of the specified condition.
/// </summary>
/// <param name="condition">The condition to check.</param>
/// <param name="element">The automation element to check.</param>
/// <returns>True if the automation element meets the condition's requirements. False otherwise.</returns>
public static bool IsMeetsRequirements(Condition condition, AutomationElement element)
{
var type = condition.GetType();
if (condition == Condition.TrueCondition)
{
// Always return true.
return(true);
}
if (condition == Condition.FalseCondition)
{
// Always returns false.
return(false);
}
if (type == typeof(NotCondition))
{
// Return the negation of the inner condition.
return(!IsMeetsRequirements(((NotCondition)condition).Condition, element));
}
if (type == typeof(OrCondition))
{
// Return true if any of the inner conditions are true.
return(((OrCondition)condition).GetConditions().Any(inner => IsMeetsRequirements(inner, element)));
}
if (type == typeof(AndCondition))
{
// Return true if all of the inner conditions are true.
return(((AndCondition)condition).GetConditions().All(inner => IsMeetsRequirements(inner, element)));
}
if (type == typeof(StringPropertyCondition))
{
// Return true if the string property condition matches.
return(((StringPropertyCondition)condition).IsMatch(element));
}
if (type == typeof(PropertyCondition))
{
// Return true if the property condition matches.
var propertyCondition = (PropertyCondition)condition;
var actual = element.GetCurrentPropertyValue(propertyCondition.Property);
var expected = propertyCondition.Value;
Trace.WriteLine("Checking '" + AutomationPropertyHelper.ToString(actual) + "'='" + AutomationPropertyHelper.ToString(expected) + "'", "UIAutomation-ConditionHelper");
return(AutomationPropertyHelper.Equals(actual, expected));
}
// Don't know how to match any other conditions.
throw new NotSupportedException("Condition '" + type + "' is not supported");
}