Home | History | Annotate | Download | only in validators
      1 /*
      2  * Copyright (C) 2013 DroidDriver committers
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.android.droiddriver.validators;
     18 
     19 import android.text.TextUtils;
     20 import android.view.accessibility.AccessibilityNodeInfo;
     21 
     22 import com.google.android.droiddriver.UiElement;
     23 import com.google.android.droiddriver.actions.Action;
     24 import com.google.android.droiddriver.uiautomation.UiAutomationElement;
     25 
     26 /**
     27  * Fall-back Validator for accessibility.
     28  */
     29 public class DefaultAccessibilityValidator implements Validator {
     30   @Override
     31   public boolean isApplicable(UiElement element, Action action) {
     32     return true;
     33   }
     34 
     35   @Override
     36   public String validate(UiElement element, Action action) {
     37     return isSpeakingNode(element) ? null : "TalkBack cannot speak about it";
     38   }
     39 
     40   // Logic from TalkBack
     41   private static boolean isAccessibilityFocusable(UiElement element) {
     42     if (isActionableForAccessibility(element)) {
     43       return true;
     44     }
     45 
     46     if (isTopLevelScrollItem(element) && (isSpeakingNode(element))) {
     47       return true;
     48     }
     49     return false;
     50   }
     51 
     52   private static boolean isTopLevelScrollItem(UiElement element) {
     53     UiElement parent = element.getParent();
     54     return parent != null && parent.isScrollable();
     55   }
     56 
     57   private static boolean isActionableForAccessibility(UiElement element) {
     58     if (element.isFocusable() || element.isClickable() || element.isLongClickable()) {
     59       return true;
     60     }
     61 
     62     if (element instanceof UiAutomationElement) {
     63       AccessibilityNodeInfo node = ((UiAutomationElement) element).getRawElement();
     64       return (node.getActions() & AccessibilityNodeInfo.ACTION_FOCUS) == AccessibilityNodeInfo.ACTION_FOCUS;
     65     }
     66     return false;
     67   }
     68 
     69   private static boolean isSpeakingNode(UiElement element) {
     70     return hasContentDescriptionOrText(element) || element.isCheckable()
     71         || hasNonActionableSpeakingChildren(element);
     72   }
     73 
     74   private static boolean hasNonActionableSpeakingChildren(UiElement element) {
     75     // Recursively check visible and non-focusable descendant nodes.
     76     for (UiElement child : element.getChildren(UiElement.VISIBLE)) {
     77       if (!isAccessibilityFocusable(child) && isSpeakingNode(child)) {
     78         return true;
     79       }
     80     }
     81     return false;
     82   }
     83 
     84   private static boolean hasContentDescriptionOrText(UiElement element) {
     85     return !TextUtils.isEmpty(element.getContentDescription())
     86         || !TextUtils.isEmpty(element.getText());
     87   }
     88 }
     89