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

Commit 105de2b8 authored by vadimt's avatar vadimt Committed by android-build-merger
Browse files

Adding system health diags for inporoc tests

am: 092b6f8c

Change-Id: I256a72c531f14f14f7167dfbb8e73f36bac7e522
parents 726578e7 092b6f8c
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -32,13 +32,14 @@ import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Base64;

import androidx.test.InstrumentationRegistry;

import com.android.launcher3.tapl.TestHelpers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import androidx.test.InstrumentationRegistry;

/**
 * Content provider to receive commands from tests
 */
@@ -47,6 +48,7 @@ public class TestCommandReceiver extends ContentProvider {
    public static final String ENABLE_TEST_LAUNCHER = "enable-test-launcher";
    public static final String DISABLE_TEST_LAUNCHER = "disable-test-launcher";
    public static final String KILL_PROCESS = "kill-process";
    public static final String GET_SYSTEM_HEALTH_MESSAGE = "get-system-health-message";

    @Override
    public boolean onCreate() {
@@ -99,6 +101,12 @@ public class TestCommandReceiver extends ContentProvider {
                        killBackgroundProcesses(arg);
                return null;
            }

            case GET_SYSTEM_HEALTH_MESSAGE: {
                final Bundle response = new Bundle();
                response.putString("result", TestHelpers.getSystemHealthMessage(getContext()));
                return response;
            }
        }
        return super.call(method, arg, extras);
    }
@@ -122,7 +130,8 @@ public class TestCommandReceiver extends ContentProvider {
            // Create an empty file so that we can pass its descriptor
            try {
                file.createNewFile();
            } catch (IOException e) { }
            } catch (IOException e) {
            }
        }

        return ParcelFileDescriptor.open(file, MODE_READ_WRITE);
+6 −1
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import com.android.launcher3.compat.LauncherAppsCompat;
import com.android.launcher3.model.AppLaunchTracker;
import com.android.launcher3.tapl.LauncherInstrumentation;
import com.android.launcher3.tapl.TestHelpers;
import com.android.launcher3.testcomponent.TestCommandReceiver;
import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.Wait;
import com.android.launcher3.util.rule.FailureWatcher;
@@ -98,7 +99,11 @@ public abstract class AbstractLauncherUiTest {
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
        if (TestHelpers.isInLauncherProcess()) Utilities.enableRunningInTestHarnessForTests();
        if (TestHelpers.isInLauncherProcess()) {
            Utilities.enableRunningInTestHarnessForTests();
            mLauncher.setSystemHealthSupplier(() -> TestCommandReceiver.callCommand(
                    TestCommandReceiver.GET_SYSTEM_HEALTH_MESSAGE).getString("result"));
        }
    }

    protected final LauncherActivityRule mActivityMonitor = new LauncherActivityRule();
+15 −69
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.DropBoxManager;
import android.os.Parcelable;
import android.os.SystemClock;
import android.text.TextUtils;
@@ -73,6 +72,7 @@ import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

/**
 * The main tapl object. The only object that can be explicitly constructed by the using code. It
@@ -133,6 +133,7 @@ public final class LauncherInstrumentation {
    private int mExpectedRotation = Surface.ROTATION_0;
    private final Uri mTestProviderUri;
    private final Deque<String> mDiagnosticContext = new LinkedList<>();
    private Supplier<String> mSystemHealthSupplier;

    /**
     * Constructs the root of TAPL hierarchy. You get all other objects from it.
@@ -285,54 +286,11 @@ public final class LauncherInstrumentation {
        return "Background";
    }

    private static String truncateCrash(String text, int maxLines) {
        String[] lines = text.split("\\r?\\n");
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < maxLines && i < lines.length; i++) {
            ret.append(lines[i]);
            ret.append('\n');
        }
        if (lines.length > maxLines) {
            ret.append("... ");
            ret.append(lines.length - maxLines);
            ret.append(" more lines truncated ...\n");
        }
        return ret.toString();
    }

    private String checkCrash(String label) {
        DropBoxManager dropbox = (DropBoxManager) getContext().getSystemService(
                Context.DROPBOX_SERVICE);
        Assert.assertNotNull("Unable access the DropBoxManager service", dropbox);

        long timestamp = 0;
        DropBoxManager.Entry entry;
        int crashCount = 0;
        StringBuilder errorDetails = new StringBuilder();
        while (null != (entry = dropbox.getNextEntry(label, timestamp))) {
            String dropboxSnippet;
            try {
                dropboxSnippet = entry.getText(4096);
            } finally {
                entry.close();
            }

            crashCount++;
            errorDetails.append(label);
            errorDetails.append(": ");
            errorDetails.append(truncateCrash(dropboxSnippet, 40));
            errorDetails.append("    ...\n");

            timestamp = entry.getTimeMillis();
        }
        Assert.assertEquals(errorDetails.toString(), 0, crashCount);
        return crashCount > 0 ? errorDetails.toString() : null;
    public void setSystemHealthSupplier(Supplier<String> supplier) {
        this.mSystemHealthSupplier = supplier;
    }

    private String getSystemHealthMessage() {
        try {
            StringBuilder errors = new StringBuilder();

        final String testPackage = getContext().getPackageName();
        try {
            mDevice.executeShellCommand("pm grant " + testPackage +
@@ -343,21 +301,9 @@ public final class LauncherInstrumentation {
            e.printStackTrace();
        }

            final String[] labels = {
                    "system_server_crash",
                    "system_server_native_crash",
                    "system_server_anr",
            };

            for (String label : labels) {
                final String crash = checkCrash(label);
                if (crash != null) errors.append(crash);
            }

            return errors.length() != 0 ? errors.toString() : null;
        } catch (Exception e) {
            return null;
        }
        return mSystemHealthSupplier != null
                ? mSystemHealthSupplier.get()
                : TestHelpers.getSystemHealthMessage(getContext());
    }

    private void fail(String message) {
+68 −0
Original line number Diff line number Diff line
@@ -26,6 +26,9 @@ import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.DropBoxManager;

import org.junit.Assert;

import java.util.List;

@@ -81,4 +84,69 @@ public class TestHelpers {
        }
        return "com.android.systemui";
    }

    private static String truncateCrash(String text, int maxLines) {
        String[] lines = text.split("\\r?\\n");
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < maxLines && i < lines.length; i++) {
            ret.append(lines[i]);
            ret.append('\n');
        }
        if (lines.length > maxLines) {
            ret.append("... ");
            ret.append(lines.length - maxLines);
            ret.append(" more lines truncated ...\n");
        }
        return ret.toString();
    }

    private static String checkCrash(Context context, String label) {
        DropBoxManager dropbox = (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
        Assert.assertNotNull("Unable access the DropBoxManager service", dropbox);

        long timestamp = 0;
        DropBoxManager.Entry entry;
        int crashCount = 0;
        StringBuilder errorDetails = new StringBuilder();
        while (null != (entry = dropbox.getNextEntry(label, timestamp))) {
            String dropboxSnippet;
            try {
                dropboxSnippet = entry.getText(4096);
            } finally {
                entry.close();
            }

            crashCount++;
            errorDetails.append(label);
            errorDetails.append(": ");
            errorDetails.append(truncateCrash(dropboxSnippet, 40));
            errorDetails.append("    ...\n");

            timestamp = entry.getTimeMillis();
        }
        Assert.assertEquals(errorDetails.toString(), 0, crashCount);
        return crashCount > 0 ? errorDetails.toString() : null;
    }

    public static String getSystemHealthMessage(Context context) {
        try {
            StringBuilder errors = new StringBuilder();

            final String[] labels = {
                    "system_server_crash",
                    "system_server_native_crash",
                    "system_server_anr",
            };

            for (String label : labels) {
                final String crash = checkCrash(context, label);
                if (crash != null) errors.append(crash);
            }

            return errors.length() != 0 ? errors.toString() : null;
        } catch (Exception e) {
            return "Failed to get system health diags, maybe build your test via .bp instead of "
                    + ".mk? " + android.util.Log.getStackTraceString(e);
        }
    }
}