Merge "Better diagnosing leaked activities." into sc-dev

This commit is contained in:
TreeHugger Robot authored and Android (Google) Code Review committed 2021-03-03 08:26:34 +00:00
commit 161b27d5c5
2 files changed
+28 -15

No files matched your search

@@ -151,23 +151,30 @@ public abstract class AbstractLauncherUiTest {
}
public static String dumpHprofData() {
if (sDumpWasGenerated) return "dump has already been generated by another test";
try {
final String fileName = getInstrumentation().getTargetContext().getFilesDir().getPath()
+ "/ActivityLeakHeapDump.hprof";
if (TestHelpers.isInLauncherProcess()) {
Debug.dumpHprofData(fileName);
} else {
final UiDevice device = UiDevice.getInstance(getInstrumentation());
device.executeShellCommand(
"am dumpheap " + device.getLauncherPackageName() + " " + fileName);
String result;
if (sDumpWasGenerated) {
result = "dump has already been generated by another test";
} else {
try {
final String fileName =
getInstrumentation().getTargetContext().getFilesDir().getPath()
+ "/ActivityLeakHeapDump.hprof";
if (TestHelpers.isInLauncherProcess()) {
Debug.dumpHprofData(fileName);
} else {
final UiDevice device = UiDevice.getInstance(getInstrumentation());
device.executeShellCommand(
"am dumpheap " + device.getLauncherPackageName() + " " + fileName);
}
sDumpWasGenerated = true;
result = "memory dump filename: " + fileName;
} catch (Throwable e) {
Log.e(TAG, "dumpHprofData failed", e);
result = "failed to save memory dump";
}
sDumpWasGenerated = true;
return "memory dump filename: " + fileName;
} catch (Throwable e) {
Log.e(TAG, "dumpHprofData failed", e);
return "failed to save memory dump";
}
return result
+ ". Full list of activities: " + ACTIVITY_LEAK_TRACKER.getActivitiesList();
}
protected AbstractLauncherUiTest() {
@@ -25,6 +25,7 @@ import androidx.test.InstrumentationRegistry;
import com.android.launcher3.tapl.TestHelpers;
import java.util.WeakHashMap;
import java.util.stream.Collectors;
public class ActivityLeakTracker implements Application.ActivityLifecycleCallbacks {
private final WeakHashMap<Activity, Boolean> mActivities = new WeakHashMap<>();
@@ -81,4 +82,9 @@ public class ActivityLeakTracker implements Application.ActivityLifecycleCallbac
return mActivities.size() <= 2;
}
public String getActivitiesList() {
return mActivities.keySet().stream().map(a -> a.getClass().getSimpleName())
.collect(Collectors.joining(","));
}
}