Skip to content

Commit

Permalink
add IGNORE_BATTERY_OPTIMIZATIONS, fix dumpHierarchy, fix UiAutomation…
Browse files Browse the repository at this point in the history
… quit but server still alive bug
  • Loading branch information
codeskyblue committed May 11, 2024
1 parent d099871 commit 5fe3557
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,11 @@ public String dumpWindowHierarchy(boolean compressed) {
device.setCompressedLayoutHierarchy(compressed);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
uiAutomation.clearCache();
try {
uiAutomation.clearCache();
} catch (NoSuchMethodError e) {
// ignore
}
device.dumpWindowHierarchy(os);
return os.toString("UTF-8");
} catch (IOException e) {
Expand Down
20 changes: 17 additions & 3 deletions app/src/androidTest/java/com/github/uiautomator/stub/Stub.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@

package com.github.uiautomator.stub;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.test.filters.LargeTest;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
Expand Down Expand Up @@ -68,7 +71,7 @@ public class Stub {

@Before
public void setUp() throws Exception {
launchService();
//launchService();
JsonRpcServer jrs = new JsonRpcServer(new ObjectMapper(), new AutomatorServiceImpl(), AutomatorService.class);
jrs.setShouldLogInvocationErrors(true);
jrs.setErrorResolver(new ErrorResolver() {
Expand Down Expand Up @@ -103,7 +106,6 @@ private void launchPackage(String packageName) {

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

device.wakeUp();

// Wait for launcher
Expand Down Expand Up @@ -142,8 +144,20 @@ private void stopMonitorService(Context context) {
@LargeTest
public void testUIAutomatorStub() throws InterruptedException {
Log.i("server started");
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
int nullCount = 0;
while (server.isAlive()) {
Thread.sleep(100);
AccessibilityNodeInfo nodeInfo = instrumentation.getUiAutomation().getRootInActiveWindow();
if (nodeInfo == null) {
nullCount += 1;
} else {
nullCount = 0;
}
if (nullCount >= 3) {
Log.e("uiAutomation.getRootInActiveWindow() always return null, okhttpd server quit");
return;
}
Thread.sleep(500);
}
}
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<!-- <uses-permission android:name="android.permission.READ_PHONE_STATE" />-->

</manifest>
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@
android:layout_weight="1" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/LeftTitle"
android:layout_width="0dp"
android:layout_weight="1"
android:text="version: todo"
android:textStyle="bold"/>
</LinearLayout>

</LinearLayout>

</ScrollView>

0 comments on commit 5fe3557

Please sign in to comment.