Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c416c69 authored by vadimt's avatar vadimt
Browse files

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
parent 2d5a8dc8
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -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))) {