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

Commit 3c79aa57 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Adding a test artifact for logcat with only interesting tags" into sc-v2-dev

parents aa28886d 6c416c69
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))) {