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

Commit 3192f86a authored by Kean Mariotti's avatar Kean Mariotti Committed by Android (Google) Code Review
Browse files

Merge "Adapt BugreportManagerTest to perfetto-based SurfaceFlinger tracing" into main

parents 57235fe2 b9b4ed5f
Loading
Loading
Loading
Loading
+40 −16
Original line number Diff line number Diff line
@@ -104,19 +104,15 @@ public class BugreportManagerTest {
    private static final String EXTRA_SCREENSHOT = "android.intent.extra.SCREENSHOT";

    private static final Path[] UI_TRACES_PREDUMPED = {
            Paths.get("/data/misc/perfetto-traces/bugreport/systrace.pftrace"),
            Paths.get("/data/misc/wmtrace/ime_trace_clients.winscope"),
            Paths.get("/data/misc/wmtrace/ime_trace_managerservice.winscope"),
            Paths.get("/data/misc/wmtrace/ime_trace_service.winscope"),
            Paths.get("/data/misc/wmtrace/wm_trace.winscope"),
            Paths.get("/data/misc/wmtrace/wm_log.winscope"),
            Paths.get("/data/misc/wmtrace/layers_trace.winscope"),
            Paths.get("/data/misc/wmtrace/transactions_trace.winscope"),
            Paths.get("/data/misc/wmtrace/transition_trace.winscope"),
            Paths.get("/data/misc/wmtrace/wm_transition_trace.winscope"),
            Paths.get("/data/misc/wmtrace/shell_transition_trace.winscope"),
    };
    private static final Path[] UI_TRACES_GENERATED_DURING_BUGREPORT = {
            Paths.get("/data/misc/wmtrace/layers_trace_from_transactions.winscope"),
    };

    private Handler mHandler;
    private Executor mExecutor;
@@ -210,7 +206,7 @@ public class BugreportManagerTest {

        mBrm.preDumpUiData();
        waitTillDumpstateExitedOrTimeout();
        List<File> expectedPreDumpedTraceFiles = copyFilesAsRoot(UI_TRACES_PREDUMPED);
        List<File> expectedPreDumpedTraceFiles = copyFiles(UI_TRACES_PREDUMPED);

        BugreportCallbackImpl callback = new BugreportCallbackImpl();
        mBrm.startBugreport(mBugreportFd, null, fullWithUsePreDumpFlag(), mExecutor,
@@ -225,7 +221,6 @@ public class BugreportManagerTest {
        assertFdsAreClosed(mBugreportFd);

        assertThatBugreportContainsFiles(UI_TRACES_PREDUMPED);
        assertThatBugreportContainsFiles(UI_TRACES_GENERATED_DURING_BUGREPORT);

        List<File> actualPreDumpedTraceFiles = extractFilesFromBugreport(UI_TRACES_PREDUMPED);
        assertThatAllFileContentsAreEqual(actualPreDumpedTraceFiles, expectedPreDumpedTraceFiles);
@@ -240,9 +235,9 @@ public class BugreportManagerTest {
        // In some corner cases, data dumped as part of the full bugreport could be the same as the
        // pre-dumped data and this test would fail. Hence, here we create fake/artificial
        // pre-dumped data that we know it won't match with the full bugreport data.
        createFilesWithFakeDataAsRoot(UI_TRACES_PREDUMPED, "system");
        createFakeTraceFiles(UI_TRACES_PREDUMPED);

        List<File> preDumpedTraceFiles = copyFilesAsRoot(UI_TRACES_PREDUMPED);
        List<File> preDumpedTraceFiles = copyFiles(UI_TRACES_PREDUMPED);

        BugreportCallbackImpl callback = new BugreportCallbackImpl();
        mBrm.startBugreport(mBugreportFd, null, full(), mExecutor,
@@ -257,7 +252,6 @@ public class BugreportManagerTest {
        assertFdsAreClosed(mBugreportFd);

        assertThatBugreportContainsFiles(UI_TRACES_PREDUMPED);
        assertThatBugreportContainsFiles(UI_TRACES_GENERATED_DURING_BUGREPORT);

        List<File> actualTraceFiles = extractFilesFromBugreport(UI_TRACES_PREDUMPED);
        assertThatAllFileContentsAreDifferent(preDumpedTraceFiles, actualTraceFiles);
@@ -480,7 +474,32 @@ public class BugreportManagerTest {
        return f;
    }

    private static void startPreDumpedUiTraces() {
    private static void startPreDumpedUiTraces() throws Exception {
        // Perfetto traces
        String perfettoConfig =
                "buffers: {\n"
                + "    size_kb: 2048\n"
                + "    fill_policy: RING_BUFFER\n"
                + "}\n"
                + "data_sources: {\n"
                + "    config {\n"
                + "        name: \"android.surfaceflinger.transactions\"\n"
                + "    }\n"
                + "}\n"
                + "bugreport_score: 10\n";
        File tmp = createTempFile("tmp", ".cfg");
        Files.write(perfettoConfig.getBytes(StandardCharsets.UTF_8), tmp);
        InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
                "install -m 644 -o root -g root "
                + tmp.getAbsolutePath() + " /data/misc/perfetto-configs/bugreport-manager-test.cfg"
        );
        InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
                "perfetto --background-wait"
                + " --config /data/misc/perfetto-configs/bugreport-manager-test.cfg --txt"
                + " --out /data/misc/perfetto-traces/not-used.perfetto-trace"
        );

        // Legacy traces
        InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
                "cmd input_method tracing start"
        );
@@ -563,19 +582,24 @@ public class BugreportManagerTest {
        return extractedFile;
    }

    private static void createFilesWithFakeDataAsRoot(Path[] paths, String owner) throws Exception {
    private static void createFakeTraceFiles(Path[] paths) throws Exception {
        File src = createTempFile("fake", ".data");
        Files.write("fake data".getBytes(StandardCharsets.UTF_8), src);

        for (Path path : paths) {
            InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
                    "install -m 611 -o " + owner + " -g " + owner
                    + " " + src.getAbsolutePath() + " " + path.toString()
                    "install -m 644 -o system -g system "
                    + src.getAbsolutePath() + " " + path.toString()
            );
        }

        // Dumpstate executes "perfetto --save-for-bugreport" as shell
        InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
                "chown shell:shell /data/misc/perfetto-traces/bugreport/systrace.pftrace"
        );
    }

    private static List<File> copyFilesAsRoot(Path[] paths) throws Exception {
    private static List<File> copyFiles(Path[] paths) throws Exception {
        ArrayList<File> files = new ArrayList<File>();
        for (Path src : paths) {
            File dst = createTempFile(src.getFileName().toString(), ".copy");