Treat non-running packages as cleared in GfxInfo

Bug: 359237606
Test: JankCollectionHelperTest
Test: JetsnackScrollBenchmark (with FrameTimingGfxInfoMetric added back locally)
Relnote: "Fixed an issue in FrameTimingGfxInfoMetric where starting
the metric would crash if the process wasn't already running."

Change-Id: I6e4126f2b3be7fdea22811141a68299c41296abe
diff --git a/benchmark/benchmark-macro/src/androidTest/java/androidx/benchmark/macro/JankCollectionHelperTest.kt b/benchmark/benchmark-macro/src/androidTest/java/androidx/benchmark/macro/JankCollectionHelperTest.kt
new file mode 100644
index 0000000..65f90419
--- /dev/null
+++ b/benchmark/benchmark-macro/src/androidTest/java/androidx/benchmark/macro/JankCollectionHelperTest.kt
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.benchmark.macro
+
+import androidx.test.filters.MediumTest
+import org.junit.Test
+
+@MediumTest
+class JankCollectionHelperTest {
+    @Test
+    fun clearGfxInfo_thisProcess() {
+        JankCollectionHelper().clearGfxInfo(Packages.TEST)
+    }
+
+    @Test
+    fun clearGfxInfo_notRunningPackage() {
+        // shouldn't fail, clearing a package that isn't running
+        // (or in this case, installed) is a noop
+        JankCollectionHelper().clearGfxInfo(Packages.MISSING)
+    }
+}
diff --git a/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java b/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java
index d0b11e8..fff2766 100644
--- a/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java
+++ b/benchmark/benchmark-macro/src/main/java/androidx/benchmark/macro/JankCollectionHelper.java
@@ -282,9 +282,13 @@
             } else {
                 String command = String.format(GFXINFO_COMMAND_RESET, pkg);
                 String output = getDevice().executeShellCommand(command);
-                // Success if the specified package header exists in the output.
-                verifyMatches(output, getHeaderMatcher(pkg), "No package header in output.");
-                Log.v(LOG_TAG, String.format("Cleared %s gfxinfo.", pkg));
+                if (output.trim().equals("No process found for: " + pkg)) {
+                    Log.v(LOG_TAG, "Skipped clearing " + pkg + " gfxinfo, not running.");
+                } else {
+                    // Success if the specified package header exists in the output.
+                    verifyMatches(output, getHeaderMatcher(pkg), "No package header in output.");
+                    Log.v(LOG_TAG, String.format("Cleared %s gfxinfo.", pkg));
+                }
             }
         } catch (IOException e) {
             throw new RuntimeException("Failed to clear gfxinfo.", e);