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 17package com.google.android.droiddriver.uiautomation; 18 19import static com.google.android.droiddriver.util.TextUtils.charSequenceToString; 20 21import android.app.UiAutomation; 22import android.app.UiAutomation.AccessibilityEventFilter; 23import android.graphics.Rect; 24import android.util.Log; 25import android.view.accessibility.AccessibilityEvent; 26import android.view.accessibility.AccessibilityNodeInfo; 27 28import com.google.android.droiddriver.InputInjector; 29import com.google.android.droiddriver.base.AbstractUiElement; 30import com.google.android.droiddriver.util.Logs; 31import com.google.common.base.Preconditions; 32 33import java.util.concurrent.FutureTask; 34import java.util.concurrent.TimeoutException; 35 36/** 37 * A UiElement that is backed by the UiAutomation object. 38 */ 39public class UiAutomationElement extends AbstractUiElement { 40 private static final AccessibilityEventFilter ANY_EVENT_FILTER = new AccessibilityEventFilter() { 41 @Override 42 public boolean accept(AccessibilityEvent arg0) { 43 return true; 44 } 45 }; 46 47 private final UiAutomationContext context; 48 private final AccessibilityNodeInfo node; 49 private final UiAutomation uiAutomation; 50 51 public UiAutomationElement(UiAutomationContext context, AccessibilityNodeInfo node) { 52 this.context = Preconditions.checkNotNull(context); 53 this.node = Preconditions.checkNotNull(node); 54 this.uiAutomation = context.getUiAutomation(); 55 } 56 57 @Override 58 public String getText() { 59 return charSequenceToString(node.getText()); 60 } 61 62 @Override 63 public String getContentDescription() { 64 return charSequenceToString(node.getContentDescription()); 65 } 66 67 @Override 68 public String getClassName() { 69 return charSequenceToString(node.getClassName()); 70 } 71 72 @Override 73 public String getResourceId() { 74 return charSequenceToString(node.getViewIdResourceName()); 75 } 76 77 @Override 78 public String getPackageName() { 79 return charSequenceToString(node.getPackageName()); 80 } 81 82 @Override 83 public InputInjector getInjector() { 84 return context.getInjector(); 85 } 86 87 @Override 88 public boolean isVisible() { 89 return node.isVisibleToUser(); 90 } 91 92 @Override 93 public boolean isCheckable() { 94 return node.isCheckable(); 95 } 96 97 @Override 98 public boolean isChecked() { 99 return node.isChecked(); 100 } 101 102 @Override 103 public boolean isClickable() { 104 return node.isClickable(); 105 } 106 107 @Override 108 public boolean isEnabled() { 109 return node.isEnabled(); 110 } 111 112 @Override 113 public boolean isFocusable() { 114 return node.isFocusable(); 115 } 116 117 @Override 118 public boolean isFocused() { 119 return node.isFocused(); 120 } 121 122 @Override 123 public boolean isScrollable() { 124 return node.isScrollable(); 125 } 126 127 @Override 128 public boolean isLongClickable() { 129 return node.isLongClickable(); 130 } 131 132 @Override 133 public boolean isPassword() { 134 return node.isPassword(); 135 } 136 137 @Override 138 public boolean isSelected() { 139 return node.isSelected(); 140 } 141 142 @Override 143 public Rect getBounds() { 144 Rect rect = new Rect(); 145 node.getBoundsInScreen(rect); 146 return rect; 147 } 148 149 @Override 150 public Rect getVisibleBounds() { 151 if (!isVisible()) { 152 Logs.log(Log.DEBUG, "Node is invisible: " + node); 153 return new Rect(); 154 } 155 Rect visibleBounds = getBounds(); 156 UiAutomationElement parent = getParent(); 157 Rect parentBounds; 158 while (parent != null) { 159 parentBounds = parent.getBounds(); 160 visibleBounds.intersect(parentBounds); 161 parent = parent.getParent(); 162 } 163 return visibleBounds; 164 } 165 166 @Override 167 public int getChildCount() { 168 return node.getChildCount(); 169 } 170 171 @Override 172 public UiAutomationElement getChild(int index) { 173 AccessibilityNodeInfo child = node.getChild(index); 174 return child == null ? null : context.getUiElement(child); 175 } 176 177 @Override 178 public UiAutomationElement getParent() { 179 AccessibilityNodeInfo parent = node.getParent(); 180 return parent == null ? null : context.getUiElement(parent); 181 } 182 183 @Override 184 protected void doPerformAndWait(FutureTask<Boolean> futureTask, long timeoutMillis) { 185 try { 186 uiAutomation.executeAndWaitForEvent(futureTask, ANY_EVENT_FILTER, timeoutMillis); 187 } catch (TimeoutException e) { 188 // This is for sync'ing with Accessibility API on best-effort because 189 // it is not reliable. 190 // Exception is ignored here. Tests will fail anyways if this is 191 // critical. 192 } 193 } 194} 195