UiAutomationElement.java revision eb4e1921c193bb90eb0122ea7b0fd37cef60e8e1
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.util.Log;
23import android.view.accessibility.AccessibilityNodeInfo;
24
25import com.google.android.droiddriver.InputInjector;
26import com.google.android.droiddriver.base.AbstractUiElement;
27import com.google.android.droiddriver.util.Logs;
28import com.google.common.base.Preconditions;
29
30/**
31 * A UiElement that is backed by the UiAutomation object.
32 */
33public class UiAutomationElement extends AbstractUiElement {
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 getBounds() {
129    Rect rect = new Rect();
130    node.getBoundsInScreen(rect);
131    return rect;
132  }
133
134  @Override
135  public Rect getVisibleBounds() {
136    if (!isVisible()) {
137      Logs.log(Log.INFO, "Node is invisible: " + node);
138      return new Rect();
139    }
140    Rect visibleBounds = getBounds();
141    UiAutomationElement parent = getParent();
142    Rect parentBounds;
143    while (parent != null) {
144      parentBounds = parent.getBounds();
145      visibleBounds.intersect(parentBounds);
146      parent = parent.getParent();
147    }
148    return visibleBounds;
149  }
150
151  @Override
152  public int getChildCount() {
153    return node.getChildCount();
154  }
155
156  @Override
157  public UiAutomationElement getChild(int index) {
158    AccessibilityNodeInfo child = node.getChild(index);
159    return child == null ? null : context.getUiElement(child);
160  }
161
162  @Override
163  public UiAutomationElement getParent() {
164    AccessibilityNodeInfo parent = node.getParent();
165    return parent == null ? null : context.getUiElement(parent);
166  }
167}
168