From 6c416c699d7bc9aafa24a98aacc00ab06b504ef3 Mon Sep 17 00:00:00 2001 From: vadimt Date: Thu, 26 Aug 2021 16:05:12 -0700 Subject: [PATCH] Adding a test artifact for logcat with only interesting tags This is based on the brainstorm "diagnostic scripts running on ATP". Please propose additional (to "TestRunner") tags. Also adding class name to all artifact filenames because sometimes different classes have failing tests with the same name. Bug: 197991109 Test: local runs Change-Id: Ic2022ca526d833fe99450e90fcfd8fb7be1f79d5 --- .../launcher3/util/rule/FailureWatcher.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java index fae55cc0cc..f9a9997e99 100644 --- a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java +++ b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java @@ -15,6 +15,7 @@ import org.junit.rules.TestWatcher; import org.junit.runner.Description; import org.junit.runners.model.Statement; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -66,15 +67,18 @@ public class FailureWatcher extends TestWatcher { onError(mDevice, description, e); } + static File diagFile(Description description, String prefix, String ext) { + return new File(getInstrumentation().getTargetContext().getFilesDir(), + prefix + "-" + description.getTestClass().getSimpleName() + "." + + description.getMethodName() + "." + ext); + } + public static void onError(UiDevice device, Description description, Throwable e) { Log.d("b/196820244", "onError 1"); if (device == null) return; Log.d("b/196820244", "onError 2"); - final File parentFile = getInstrumentation().getTargetContext().getFilesDir(); - final File sceenshot = new File(parentFile, - "TestScreenshot-" + description.getMethodName() + ".png"); - final File hierarchy = new File(parentFile, - "Hierarchy-" + description.getMethodName() + ".zip"); + final File sceenshot = diagFile(description, "TestScreenshot", "png"); + final File hierarchy = diagFile(description, "Hierarchy", "zip"); // Dump window hierarchy try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(hierarchy))) { @@ -97,13 +101,13 @@ public class FailureWatcher extends TestWatcher { device.takeScreenshot(sceenshot); // Dump accessibility hierarchy - final File accessibilityHierarchyFile = new File(parentFile, - "AccessibilityHierarchy-" + description.getMethodName() + ".uix"); try { - device.dumpWindowHierarchy(accessibilityHierarchyFile); + device.dumpWindowHierarchy(diagFile(description, "AccessibilityHierarchy", "uix")); } catch (IOException ex) { Log.e(TAG, "Failed to save accessibility hierarchy", ex); } + + dumpCommand("logcat -d -s TestRunner", diagFile(description, "FilteredLogcat", "txt")); } private static void dumpStringCommand(String cmd, OutputStream out) throws IOException { @@ -111,6 +115,14 @@ public class FailureWatcher extends TestWatcher { dumpCommand(cmd, out); } + private static void dumpCommand(String cmd, File out) { + try (BufferedOutputStream buffered = new BufferedOutputStream( + new FileOutputStream(out))) { + dumpCommand(cmd, buffered); + } catch (IOException ex) { + } + } + private static void dumpCommand(String cmd, OutputStream out) throws IOException { try (AutoCloseInputStream in = new AutoCloseInputStream(getInstrumentation() .getUiAutomation().executeShellCommand(cmd))) {