Skip to content

Commit 3280af0

Browse files
committed
add maxDepth for dumpWindowHierarchy
1 parent 09bec32 commit 3280af0

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

app/src/androidTest/java/com/github/uiautomator/stub/AccessibilityNodeInfoDumper.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AccessibilityNodeInfoDumper {
3434

3535
private AccessibilityNodeInfoDumper() { }
3636

37-
public static void dumpWindowHierarchy(UiDevice device, OutputStream out) throws IOException {
37+
public static void dumpWindowHierarchy(UiDevice device, OutputStream out, int maxDepth) throws IOException {
3838
try (Section ignored = Traces.trace("AccessibilityNodeInfoDumper.dumpWindowHierarchy")) {
3939
XmlSerializer serializer = Xml.newSerializer();
4040
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
@@ -45,13 +45,8 @@ public static void dumpWindowHierarchy(UiDevice device, OutputStream out) throws
4545
serializer.attribute("", "rotation", Integer.toString(device.getDisplayRotation()));
4646

4747
for (AccessibilityNodeInfo root : getWindowRoots(device)) {
48-
try {
49-
dumpNodeRec(root, serializer, 0, device.getDisplayWidth(),
50-
device.getDisplayHeight());
51-
} catch (IllegalArgumentException e) {
52-
// java.lang.IllegalArgumentException: Illegal character (U+0)
53-
e.printStackTrace();
54-
}
48+
dumpNodeRec(root, serializer, 0, device.getDisplayWidth(),
49+
device.getDisplayHeight(), maxDepth);
5550
}
5651

5752
serializer.endTag("", "hierarchy");
@@ -104,16 +99,22 @@ private static List<AccessibilityWindowInfo> getWindows(UiAutomation uiAutomatio
10499
}
105100

106101
private static void dumpNodeRec(AccessibilityNodeInfo node, XmlSerializer serializer,int index,
107-
int width, int height) throws IOException {
102+
int width, int height, int maxDepth) throws IOException {
108103
serializer.startTag("", "node");
109104
if (!nafExcludedClass(node) && !nafCheck(node))
110105
serializer.attribute("", "NAF", Boolean.toString(true));
111106
serializer.attribute("", "index", Integer.toString(index));
112-
serializer.attribute("", "text", safeCharSeqToString(node.getText()));
113-
serializer.attribute("", "resource-id", safeCharSeqToString(node.getViewIdResourceName()));
114-
serializer.attribute("", "class", safeCharSeqToString(node.getClassName()));
115-
serializer.attribute("", "package", safeCharSeqToString(node.getPackageName()));
116-
serializer.attribute("", "content-desc", safeCharSeqToString(node.getContentDescription()));
107+
try {
108+
serializer.attribute("", "text", safeCharSeqToString(node.getText()));
109+
serializer.attribute("", "resource-id", safeCharSeqToString(node.getViewIdResourceName()));
110+
serializer.attribute("", "class", safeCharSeqToString(node.getClassName()));
111+
serializer.attribute("", "package", safeCharSeqToString(node.getPackageName()));
112+
serializer.attribute("", "content-desc", safeCharSeqToString(node.getContentDescription()));
113+
} catch (IllegalArgumentException e) {
114+
// java.lang.IllegalArgumentException: Illegal character (U+0)
115+
// TODO: maybe the best way is to update safeCharSeqToString
116+
e.printStackTrace();
117+
}
117118
serializer.attribute("", "checkable", Boolean.toString(node.isCheckable()));
118119
serializer.attribute("", "checked", Boolean.toString(node.isChecked()));
119120
serializer.attribute("", "clickable", Boolean.toString(node.isClickable()));
@@ -138,18 +139,20 @@ private static void dumpNodeRec(AccessibilityNodeInfo node, XmlSerializer serial
138139
serializer.attribute("", "display-id",
139140
Integer.toString(Api30Impl.getDisplayId(node)));
140141
}
141-
int count = node.getChildCount();
142-
for (int i = 0; i < count; i++) {
143-
AccessibilityNodeInfo child = node.getChild(i);
144-
if (child != null) {
145-
if (child.isVisibleToUser()) {
146-
dumpNodeRec(child, serializer, i, width, height);
147-
child.recycle();
142+
if (maxDepth > 0) {
143+
int count = node.getChildCount();
144+
for (int i = 0; i < count; i++) {
145+
AccessibilityNodeInfo child = node.getChild(i);
146+
if (child != null) {
147+
if (child.isVisibleToUser()) {
148+
dumpNodeRec(child, serializer, i, width, height, maxDepth-1);
149+
child.recycle();
150+
} else {
151+
Log.i(TAG, String.format("Skipping invisible child: %s", child));
152+
}
148153
} else {
149-
Log.i(TAG, String.format("Skipping invisible child: %s", child));
154+
Log.i(TAG, String.format("Null child %d/%d, parent: %s", i, count, node));
150155
}
151-
} else {
152-
Log.i(TAG, String.format("Null child %d/%d, parent: %s", i, count, node));
153156
}
154157
}
155158
serializer.endTag("", "node");

app/src/androidTest/java/com/github/uiautomator/stub/AutomatorService.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,21 @@ public interface AutomatorService {
161161
boolean injectInputEvent(int action, float x, float y, int metaState);
162162

163163
/**
164-
* Helper method used for debugging to dump the current window's layout hierarchy. The file root location is /data/local/tmp
164+
* Helper method used for debugging to dump the current window's layout hierarchy.
165165
*
166166
* @param compressed use compressed layout hierarchy or not using setCompressedLayoutHeirarchy method. Ignore the parameter in case the API level lt 18.
167-
* @param filename the filename to be stored.
168-
* @return the absolute path name of dumped file.
167+
* @return xml content
169168
*/
170-
@Deprecated
171-
String dumpWindowHierarchy(boolean compressed, String filename);
169+
String dumpWindowHierarchy(boolean compressed);
172170

173171
/**
174-
* Helper method used for debugging to dump the current window's layout hierarchy.
172+
* Helper method used for debugging to dump the current window's layout hierarchy
175173
*
176-
* @param compressed use compressed layout hierarchy or not using setCompressedLayoutHeirarchy method. Ignore the parameter in case the API level lt 18.
177-
* @return the absolute path name of dumped file.
174+
* @param compressed
175+
* @param maxDepth
176+
* @return xml content
178177
*/
179-
String dumpWindowHierarchy(boolean compressed);
178+
String dumpWindowHierarchy(boolean compressed, int maxDepth);
180179

181180
/**
182181
* Take a screenshot of current window and store it as PNG The screenshot is adjusted per screen rotation

app/src/androidTest/java/com/github/uiautomator/stub/AutomatorServiceImpl.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,30 +285,22 @@ public boolean injectInputEvent(int action, float x, float y, int metaState) {
285285
}
286286

287287
/**
288-
* Helper method used for debugging to dump the current window's layout hierarchy. The file root location is /data/local/tmp
288+
* Helper method used for debugging to dump the current window's layout hierarchy.
289289
*
290290
* @param compressed use compressed layout hierarchy or not using setCompressedLayoutHeirarchy method. Ignore the parameter in case the API level lt 18.
291-
* @param filename the filename to be stored. @deprecated
292291
* @return the absolute path name of dumped file.
293292
*/
294-
@Deprecated
295293
@Override
296-
public String dumpWindowHierarchy(boolean compressed, String filename) {
297-
return dumpWindowHierarchy(compressed);
294+
public String dumpWindowHierarchy(boolean compressed) {
295+
return dumpWindowHierarchy(compressed, 50);
298296
}
299297

300-
/**
301-
* Helper method used for debugging to dump the current window's layout hierarchy.
302-
*
303-
* @param compressed use compressed layout hierarchy or not using setCompressedLayoutHeirarchy method. Ignore the parameter in case the API level lt 18.
304-
* @return the absolute path name of dumped file.
305-
*/
306298
@Override
307-
public String dumpWindowHierarchy(boolean compressed) {
299+
public String dumpWindowHierarchy(boolean compressed, int maxDepth) {
308300
device.setCompressedLayoutHierarchy(compressed);
309301
ByteArrayOutputStream os = new ByteArrayOutputStream();
310302
try {
311-
AccessibilityNodeInfoDumper.dumpWindowHierarchy(device, os);
303+
AccessibilityNodeInfoDumper.dumpWindowHierarchy(device, os, maxDepth);
312304
// device.dumpWindowHierarchy(os);
313305
return os.toString("UTF-8");
314306
} catch (IOException e) {

app/src/androidTest/java/com/github/uiautomator/stub/ConfiguratorInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public ConfiguratorInfo() {
3737
this._scrollAcknowledgmentTimeout = config.getScrollAcknowledgmentTimeout();
3838
this._waitForIdleTimeout = config.getWaitForIdleTimeout();
3939
this._waitForSelectorTimeout = config.getWaitForSelectorTimeout();
40+
this._uiAutomationFlags = config.getUiAutomationFlags();
4041
}
4142

4243
public long getActionAcknowledgmentTimeout() {
@@ -79,18 +80,22 @@ public void setWaitForSelectorTimeout(long _waitForSelectorTimeout) {
7980
this._waitForSelectorTimeout = _waitForSelectorTimeout;
8081
}
8182

83+
public int getUiAutomationFlags() { return _uiAutomationFlags; }
84+
8285
public static void setConfigurator(ConfiguratorInfo info) {
8386
Configurator config = Configurator.getInstance();
8487
config.setActionAcknowledgmentTimeout(info.getActionAcknowledgmentTimeout());
8588
config.setKeyInjectionDelay(info.getKeyInjectionDelay());
8689
config.setScrollAcknowledgmentTimeout(info.getScrollAcknowledgmentTimeout());
8790
config.setWaitForIdleTimeout(info.getWaitForIdleTimeout());
8891
config.setWaitForSelectorTimeout(info.getWaitForSelectorTimeout());
92+
config.setUiAutomationFlags(info.getUiAutomationFlags());
8993
}
9094

9195
private long _actionAcknowledgmentTimeout;
9296
private long _keyInjectionDelay;
9397
private long _scrollAcknowledgmentTimeout;
9498
private long _waitForIdleTimeout;
9599
private long _waitForSelectorTimeout;
100+
private int _uiAutomationFlags;
96101
}

app/src/androidTest/java/com/github/uiautomator/stub/DeviceInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
package com.github.uiautomator.stub;
2525

2626
import android.os.RemoteException;
27-
import androidx.test.InstrumentationRegistry;
27+
28+
import androidx.test.platform.app.InstrumentationRegistry;
2829
import androidx.test.uiautomator.UiDevice;
2930

3031
public class DeviceInfo {

0 commit comments

Comments
 (0)