1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* Copyright (C) 2013 DroidDriver committers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.droiddriver.base;
import android.graphics.Rect;
import com.google.android.droiddriver.UiElement;
import com.google.android.droiddriver.actions.Action;
import com.google.android.droiddriver.actions.ClickAction;
import com.google.android.droiddriver.actions.InputInjector;
import com.google.android.droiddriver.actions.SwipeAction;
import com.google.android.droiddriver.actions.TextAction;
import com.google.android.droiddriver.exceptions.ElementNotVisibleException;
import com.google.android.droiddriver.finders.Attribute;
import com.google.android.droiddriver.finders.ByXPath;
import com.google.android.droiddriver.scroll.Direction.PhysicalDirection;
import com.google.android.droiddriver.util.Logs;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import org.w3c.dom.Element;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* Base UiElement that implements the common operations.
*/
public abstract class BaseUiElement implements UiElement {
private WeakReference<Element> domNode;
@SuppressWarnings("unchecked")
@Override
public <T> T get(Attribute attribute) {
return (T) getAttributes().get(attribute);
}
@Override
public String getText() {
return get(Attribute.TEXT);
}
@Override
public String getContentDescription() {
return get(Attribute.CONTENT_DESC);
}
@Override
public String getClassName() {
return get(Attribute.CLASS);
}
@Override
public String getResourceId() {
return get(Attribute.RESOURCE_ID);
}
@Override
public String getPackageName() {
return get(Attribute.PACKAGE);
}
@Override
public boolean isCheckable() {
return (Boolean) get(Attribute.CHECKABLE);
}
@Override
public boolean isChecked() {
return (Boolean) get(Attribute.CHECKED);
}
@Override
public boolean isClickable() {
return (Boolean) get(Attribute.CLICKABLE);
}
@Override
public boolean isEnabled() {
return (Boolean) get(Attribute.ENABLED);
}
@Override
public boolean isFocusable() {
return (Boolean) get(Attribute.FOCUSABLE);
}
@Override
public boolean isFocused() {
return (Boolean) get(Attribute.FOCUSED);
}
@Override
public boolean isScrollable() {
return (Boolean) get(Attribute.SCROLLABLE);
}
@Override
public boolean isLongClickable() {
return (Boolean) get(Attribute.LONG_CLICKABLE);
}
@Override
public boolean isPassword() {
return (Boolean) get(Attribute.PASSWORD);
}
@Override
public boolean isSelected() {
return (Boolean) get(Attribute.SELECTED);
}
@Override
public Rect getBounds() {
return get(Attribute.BOUNDS);
}
@Override
public boolean perform(Action action) {
Logs.call(this, "perform", action);
checkVisible();
return performAndWait(action);
}
protected boolean doPerform(Action action) {
return action.perform(getInjector(), this);
}
protected void doPerformAndWait(FutureTask<Boolean> futureTask, long timeoutMillis) {
// ignores timeoutMillis; subclasses can override this behavior
futureTask.run();
}
private boolean performAndWait(final Action action) {
// timeoutMillis <= 0 means no need to wait
if (action.getTimeoutMillis() <= 0) {
return doPerform(action);
}
FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new Callable<Boolean>() {
@Override
public Boolean call() {
return doPerform(action);
}
});
doPerformAndWait(futureTask, action.getTimeoutMillis());
try {
return futureTask.get();
} catch (Exception e) {
// should not reach here b/c futureTask has run
return false;
}
}
@Override
public void setText(String text) {
perform(new TextAction(text));
// TextAction may not be effective immediately and reflected by getText(),
// so the following will fail.
// if (Logs.DEBUG) {
// String actual = getText();
// if (!text.equals(actual)) {
// throw new DroidDriverException(String.format(
// "setText failed: expected=\"%s\", actual=\"%s\"", text, actual));
// }
// }
}
@Override
public void click() {
perform(ClickAction.SINGLE);
}
@Override
public void longClick() {
perform(ClickAction.LONG);
}
@Override
public void doubleClick() {
perform(ClickAction.DOUBLE);
}
@Override
public void scroll(PhysicalDirection direction) {
perform(SwipeAction.toScroll(direction));
}
protected abstract Map<Attribute, Object> getAttributes();
protected abstract List<? extends BaseUiElement> getChildren();
private void checkVisible() {
if (!isVisible()) {
throw new ElementNotVisibleException(this);
}
}
@Override
public List<? extends BaseUiElement> getChildren(Predicate<? super UiElement> predicate) {
List<? extends BaseUiElement> children = getChildren();
if (children == null) {
return ImmutableList.of();
}
if (predicate == null || predicate.equals(Predicates.alwaysTrue())) {
return children;
}
return ImmutableList.copyOf(Collections2.filter(children, predicate));
}
@Override
public String toString() {
ToStringHelper toStringHelper = Objects.toStringHelper(this);
for (Attribute attr : Attribute.values()) {
addAttribute(toStringHelper, attr, get(attr));
}
return toStringHelper.toString();
}
private static void addAttribute(ToStringHelper toStringHelper, Attribute attr, Object value) {
if (value != null) {
if (value instanceof Boolean) {
if ((Boolean) value) {
toStringHelper.addValue(attr.getName());
}
} else {
toStringHelper.add(attr.getName(), value);
}
}
}
/**
* Used internally in {@link ByXPath}. Returns the DOM node representing this
* UiElement. The DOM is constructed from the UiElement tree.
* <p>
* TODO: move this to {@link ByXPath}. This requires a BiMap using
* WeakReference for both keys and values, which is error-prone. This will be
* deferred until we decide whether to clear cache upon getRootElement.
*/
public Element getDomNode() {
if (domNode == null || domNode.get() == null) {
domNode = new WeakReference<Element>(ByXPath.buildDomNode(this));
}
return domNode.get();
}
}
|