3535import android .os .Looper ;
3636import android .os .RemoteException ;
3737import android .os .SystemClock ;
38- import androidx .test .InstrumentationRegistry ;
38+
39+ import androidx .core .accessibilityservice .AccessibilityServiceInfoCompat ;
40+ import androidx .test .platform .app .InstrumentationRegistry ;
3941import androidx .test .uiautomator .Configurator ;
4042import androidx .test .uiautomator .Direction ;
4143import androidx .test .uiautomator .StaleObjectException ;
6971
7072public class AutomatorServiceImpl implements AutomatorService {
7173
72- private final HashSet <String > watchers = new HashSet <String >();
73- private final ConcurrentHashMap <String , UiObject > uiObjects = new ConcurrentHashMap <String , UiObject >();
74+ private final HashSet <String > watchers = new HashSet <>();
75+ private final ConcurrentHashMap <String , UiObject > uiObjects = new ConcurrentHashMap <>();
7476 private SoundPool soundPool = new SoundPool (100 , AudioManager .STREAM_MUSIC , 0 );
7577
7678 Handler handler = new Handler (Looper .getMainLooper ());
@@ -84,13 +86,19 @@ public class AutomatorServiceImpl implements AutomatorService {
8486 public AutomatorServiceImpl () {
8587 mInstrumentation = InstrumentationRegistry .getInstrumentation ();
8688 uiAutomation = mInstrumentation .getUiAutomation ();
89+
90+ // https://developer.android.com/reference/androidx/core/accessibilityservice/AccessibilityServiceInfoCompat#FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY()
91+ // https://www.jianshu.com/p/a8ccd607e172
92+ // improve accessibility support for "android.webkit.WebView"
93+ uiAutomation .getServiceInfo ().flags |= AccessibilityServiceInfoCompat .FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY ;
94+
8795 device = UiDevice .getInstance (mInstrumentation );
8896 touchController = new TouchController (mInstrumentation );
8997
9098 handler .post (new Runnable () {
9199 @ Override
92100 public void run () {
93- AutomatorServiceImpl .this .clipboard = (ClipboardManager ) InstrumentationRegistry .getTargetContext ().getSystemService (Context .CLIPBOARD_SERVICE );
101+ AutomatorServiceImpl .this .clipboard = (ClipboardManager ) mInstrumentation .getTargetContext ().getSystemService (Context .CLIPBOARD_SERVICE );
94102 }
95103 });
96104 // play music when loaded
@@ -119,7 +127,7 @@ private UiAutomation getUiAutomation() {
119127 /**
120128 * It's to play a section music to test
121129 *
122- * @return
130+ * @return bool
123131 */
124132 @ Override
125133 public boolean playSound (String path ) {
@@ -167,7 +175,7 @@ public boolean makeToast(final String text, final int duration) {
167175 handler .post (new Runnable () {
168176 @ Override
169177 public void run () {
170- ToastHelper .makeText (InstrumentationRegistry .getTargetContext (), text , duration ).show ();
178+ ToastHelper .makeText (mInstrumentation .getTargetContext (), text , duration ).show ();
171179 }
172180 });
173181 return true ;
@@ -292,12 +300,16 @@ public String dumpWindowHierarchy(boolean compressed, String filename) {
292300 */
293301 @ Override
294302 public String dumpWindowHierarchy (boolean compressed ) {
295- device .setCompressedLayoutHeirarchy (compressed );
303+ device .setCompressedLayoutHierarchy (compressed );
296304 ByteArrayOutputStream os = new ByteArrayOutputStream ();
297305 try {
298306 // Original code: device.dumpWindowHierarchy(os);
299- // The bellow code fix xml encode error
300- AccessibilityNodeInfoDumper .dumpWindowHierarchy (device , os );
307+ // The old code have xml encode error
308+ // It seems the new androidx.uiautomator dump looks fine.
309+ device .dumpWindowHierarchy (os );
310+ // alternative
311+ // sometimes java.lang.NullPointerException raises
312+ // AccessibilityNodeInfoDumper.dumpWindowHierarchy(device, os);
301313 return os .toString ("UTF-8" );
302314 } catch (IOException e ) {
303315 Log .d ("dumpWindowHierarchy got IOException: " + e );
@@ -323,7 +335,7 @@ public String dumpWindowHierarchy(boolean compressed) {
323335 */
324336 @ Override
325337 public String takeScreenshot (String filename , float scale , int quality ) throws NotImplementedException {
326- File f = new File (InstrumentationRegistry .getTargetContext ().getFilesDir (), filename );
338+ File f = new File (mInstrumentation .getTargetContext ().getFilesDir (), filename );
327339 device .takeScreenshot (f , scale , quality );
328340 if (f .exists ()) return f .getAbsolutePath ();
329341 return null ;
0 commit comments