UiAutomationElement.java revision f9c2a591497874769b87bf492a0666cf853e0ae5
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.graphics.Rect; 22import android.view.accessibility.AccessibilityNodeInfo; 23 24import com.google.android.droiddriver.InputInjector; 25import com.google.android.droiddriver.base.AbstractUiElement; 26import com.google.common.base.Objects; 27import com.google.common.base.Preconditions; 28 29/** 30 * A UiElement that is backed by the UiAutomation object. 31 */ 32public class UiAutomationElement extends AbstractUiElement { 33 34 private final UiAutomationContext context; 35 private final AccessibilityNodeInfo node; 36 37 public UiAutomationElement(UiAutomationContext context, AccessibilityNodeInfo node) { 38 this.context = Preconditions.checkNotNull(context); 39 this.node = Preconditions.checkNotNull(node); 40 } 41 42 @Override 43 public String getText() { 44 return charSequenceToString(node.getText()); 45 } 46 47 @Override 48 public String getContentDescription() { 49 return charSequenceToString(node.getContentDescription()); 50 } 51 52 @Override 53 public String getClassName() { 54 return charSequenceToString(node.getClassName()); 55 } 56 57 @Override 58 public String getResourceId() { 59 return charSequenceToString(node.getViewIdResourceName()); 60 } 61 62 @Override 63 public String getPackageName() { 64 return charSequenceToString(node.getPackageName()); 65 } 66 67 @Override 68 public InputInjector getInjector() { 69 return context.getInjector(); 70 } 71 72 @Override 73 public boolean isVisible() { 74 return node.isVisibleToUser(); 75 } 76 77 @Override 78 public boolean isCheckable() { 79 return node.isCheckable(); 80 } 81 82 @Override 83 public boolean isChecked() { 84 return node.isChecked(); 85 } 86 87 @Override 88 public boolean isClickable() { 89 return node.isClickable(); 90 } 91 92 @Override 93 public boolean isEnabled() { 94 return node.isEnabled(); 95 } 96 97 @Override 98 public boolean isFocusable() { 99 return node.isFocusable(); 100 } 101 102 @Override 103 public boolean isFocused() { 104 return node.isFocused(); 105 } 106 107 @Override 108 public boolean isScrollable() { 109 return node.isScrollable(); 110 } 111 112 @Override 113 public boolean isLongClickable() { 114 return node.isLongClickable(); 115 } 116 117 @Override 118 public boolean isPassword() { 119 return node.isPassword(); 120 } 121 122 @Override 123 public boolean isSelected() { 124 return node.isSelected(); 125 } 126 127 @Override 128 public Rect getRect() { 129 Rect rect = new Rect(); 130 node.getBoundsInScreen(rect); 131 return rect; 132 } 133 134 @Override 135 public String toString() { 136 return Objects.toStringHelper(this).add("node", node).toString(); 137 } 138 139 @Override 140 protected int getChildCount() { 141 return node.getChildCount(); 142 } 143 144 @Override 145 protected UiAutomationElement getChild(int index) { 146 AccessibilityNodeInfo child = node.getChild(index); 147 return child == null ? null : context.getUiElement(child); 148 } 149} 150