UiAutomationElement.java revision acde362144222864329ad64d7e746b717103c806
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 android.graphics.Rect;
20import android.view.accessibility.AccessibilityNodeInfo;
21
22import com.google.android.droiddriver.UiElement;
23import com.google.android.droiddriver.actions.Action;
24import com.google.android.droiddriver.base.AbstractUiElement;
25import com.google.android.droiddriver.util.TextUtils;
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 TextUtils.charSequenceToString(node.getText());
45  }
46
47  @Override
48  public String getContentDescription() {
49    return TextUtils.charSequenceToString(node.getContentDescription());
50  }
51
52  @Override
53  public String getClassName() {
54    return TextUtils.charSequenceToString(node.getClassName());
55  }
56
57  @Override
58  public boolean perform(Action action) {
59    return action.perform(context.getInjector(), this);
60  }
61
62  @Override
63  public boolean isVisible() {
64    return node.isVisibleToUser();
65  }
66
67  @Override
68  public Rect getRect() {
69    Rect rect = new Rect();
70    node.getBoundsInScreen(rect);
71    return rect;
72  }
73
74  @Override
75  public String toString() {
76    return Objects.toStringHelper(this).add("node", node).toString();
77  }
78
79  @Override
80  protected int getChildCount() {
81    return node.getChildCount();
82  }
83
84  @Override
85  protected UiElement getChild(int index) {
86    AccessibilityNodeInfo child = node.getChild(index);
87    return child == null ? null : context.getUiElement(child);
88  }
89}
90