Skip to content

Commit

Permalink
fix FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY not property set error, c…
Browse files Browse the repository at this point in the history
…all clearCache before dumpWindowHierarchy
  • Loading branch information
codeskyblue committed May 11, 2024
1 parent 5a81f04 commit d099871
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ dependencies {
androidTestImplementation 'com.fasterxml.jackson.core:jackson-databind:2.5.3'

implementation project(':permission')
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'junit:junit:4.12'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.github.uiautomator.stub;
package com.github.uiautomator.exceptions;

import android.os.Build;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.os.RemoteException;
import androidx.test.uiautomator.UiObjectNotFoundException;

import com.github.uiautomator.exceptions.NotImplementedException;
import com.googlecode.jsonrpc4j.JsonRpcError;
import com.googlecode.jsonrpc4j.JsonRpcErrors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package com.github.uiautomator.stub;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.content.ClipData;
Expand Down Expand Up @@ -54,6 +55,7 @@


import com.github.uiautomator.ToastHelper;
import com.github.uiautomator.exceptions.NotImplementedException;
import com.github.uiautomator.stub.watcher.ClickUiObjectWatcher;
import com.github.uiautomator.stub.watcher.PressKeysWatcher;

Expand Down Expand Up @@ -89,8 +91,12 @@ public AutomatorServiceImpl() {

// https://developer.android.com/reference/androidx/core/accessibilityservice/AccessibilityServiceInfoCompat#FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY()
// https://www.jianshu.com/p/a8ccd607e172
// improve accessibility support for "android.webkit.WebView"
uiAutomation.getServiceInfo().flags |= AccessibilityServiceInfoCompat.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY;
// https://developer.android.com/reference/android/app/UiAutomation

AccessibilityServiceInfo info = uiAutomation.getServiceInfo();
// improve accessibility support for "android.webkit.WebView" q
info.flags |= AccessibilityServiceInfoCompat.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY;
uiAutomation.setServiceInfo(info);

device = UiDevice.getInstance(mInstrumentation);
touchController = new TouchController(mInstrumentation);
Expand All @@ -117,7 +123,7 @@ public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
configurator.setScrollAcknowledgmentTimeout(200); // Default 200
configurator.setKeyInjectionDelay(0); // Default 0

uiAutomation.setOnAccessibilityEventListener(new AccessibilityEventListener(device, watchers));
//uiAutomation.setOnAccessibilityEventListener(new AccessibilityEventListener(device, watchers));
}

private UiAutomation getUiAutomation() {
Expand Down Expand Up @@ -303,13 +309,8 @@ public String dumpWindowHierarchy(boolean compressed) {
device.setCompressedLayoutHierarchy(compressed);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
// Original code: device.dumpWindowHierarchy(os);
// The old code have xml encode error
// It seems the new androidx.uiautomator dump looks fine.
uiAutomation.clearCache();
device.dumpWindowHierarchy(os);
// alternative
// sometimes java.lang.NullPointerException raises
// AccessibilityNodeInfoDumper.dumpWindowHierarchy(device, os);
return os.toString("UTF-8");
} catch (IOException e) {
Log.d("dumpWindowHierarchy got IOException: " + e);
Expand Down
5 changes: 5 additions & 0 deletions app/src/androidTest/java/com/github/uiautomator/stub/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public static void i(String msg) {
public static void e(String msg) {
android.util.Log.e(TAG, msg);
}

public static void e(String msg, Exception e) {
android.util.Log.e(TAG, msg);
e.printStackTrace();
}
}
13 changes: 7 additions & 6 deletions app/src/androidTest/java/com/github/uiautomator/stub/Stub.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
Expand Down Expand Up @@ -91,7 +91,7 @@ public JsonError resolveError(Throwable throwable, Method method, List<JsonNode>
private void launchPackage(String packageName) {
Log.i("Launch " + packageName);
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Context context = InstrumentationRegistry.getContext();
Context context = InstrumentationRegistry.getInstrumentation().getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(packageName);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Expand All @@ -103,7 +103,7 @@ private void launchPackage(String packageName) {

private void launchService() throws RemoteException {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Context context = InstrumentationRegistry.getContext();

device.wakeUp();

// Wait for launcher
Expand All @@ -115,7 +115,8 @@ private void launchService() throws RemoteException {
}

Log.d("Launch service");
startMonitorService(context);
// Context context = InstrumentationRegistry.getInstrumentation().getContext();
//startMonitorService(context);
}

private void startMonitorService(Context context) {
Expand All @@ -127,8 +128,8 @@ private void startMonitorService(Context context) {
@After
public void tearDown() {
server.stop();
Context context = InstrumentationRegistry.getContext();
stopMonitorService(context);
//Context context = InstrumentationRegistry.getContext();
//stopMonitorService(context);
}

private void stopMonitorService(Context context) {
Expand Down

0 comments on commit d099871

Please sign in to comment.