Create ProtoUtils class to aid in using protos in tests.

The proto dump will be more reliable in the long run than the text dump,
so we're switching CTS tests to use JobScheduler's proto dump instead.

The ProtoUtils class has been created to make it easier to use other
service dumps as well.

Bug: 137550055
Test: atest BatterySavingTestBase
Test: atest CtsIncidentHostTestCases
Test: atest LocationAccessCheckTest
Test: atest ProfileScheduledJobHostSideTest
Change-Id: I1e7f64dd93a08cf30fa72b90bd825dd4f700e4e8
diff --git a/common/host-side/util/src/com/android/compatibility/common/util/ProtoUtils.java b/common/host-side/util/src/com/android/compatibility/common/util/ProtoUtils.java
new file mode 100644
index 0000000..f08eb45
--- /dev/null
+++ b/common/host-side/util/src/com/android/compatibility/common/util/ProtoUtils.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2019 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 com.android.compatibility.common.util;
+
+import com.android.tradefed.device.CollectingByteOutputReceiver;
+import com.android.tradefed.device.ITestDevice;
+
+import com.google.protobuf.MessageLite;
+import com.google.protobuf.Parser;
+
+/** Utility class for proto dumps. */
+public class ProtoUtils {
+    /**
+     * Command to get a JobScheduler proto dump. Can be used as input to {@link
+     * #getProto(ITestDevice, Parser, String)}.
+     */
+    public static final String DUMPSYS_JOB_SCHEDULER = "dumpsys jobscheduler --proto";
+
+    /**
+     * Call onto the device with an adb shell command and get the results of that as a proto of the
+     * given type.
+     *
+     * @param device The test device to run a command on.
+     * @param parser A protobuf parser object. e.g. MyProto.parser()
+     * @param command The adb shell command to run. e.g. "dumpsys fingerprint --proto"
+     */
+    public static <T extends MessageLite> T getProto(
+            ITestDevice device, Parser<T> parser, String command) throws Exception {
+        final CollectingByteOutputReceiver receiver = new CollectingByteOutputReceiver();
+        device.executeShellCommand(command, receiver);
+        return parser.parseFrom(receiver.getOutput());
+    }
+}