Dump AIDL TemperatureThreshold in custom format

In the same format as other public Thermal object like
Temperature or CoolingDevice

Bug: b/268508448
Test: atest ThermalManagerServiceTest
Change-Id: I45bc1df2db2c2d43939bf03d150e34c3b6e36e5f
diff --git a/services/core/java/com/android/server/power/ThermalManagerService.java b/services/core/java/com/android/server/power/ThermalManagerService.java
index e3d2189f..43f96e7 100644
--- a/services/core/java/com/android/server/power/ThermalManagerService.java
+++ b/services/core/java/com/android/server/power/ThermalManagerService.java
@@ -505,47 +505,9 @@
             return mTemperatureWatcher.getForecast(forecastSeconds);
         }
 
-        private void dumpItemsLocked(PrintWriter pw, String prefix,
-                Collection<?> items) {
-            for (Iterator iterator = items.iterator(); iterator.hasNext();) {
-                pw.println(prefix + iterator.next().toString());
-            }
-        }
-
         @Override
-        public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
-            if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
-                return;
-            }
-            final long token = Binder.clearCallingIdentity();
-            try {
-                synchronized (mLock) {
-                    pw.println("IsStatusOverride: " + mIsStatusOverride);
-                    pw.println("ThermalEventListeners:");
-                    mThermalEventListeners.dump(pw, "\t");
-                    pw.println("ThermalStatusListeners:");
-                    mThermalStatusListeners.dump(pw, "\t");
-                    pw.println("Thermal Status: " + mStatus);
-                    pw.println("Cached temperatures:");
-                    dumpItemsLocked(pw, "\t", mTemperatureMap.values());
-                    pw.println("HAL Ready: " + mHalReady.get());
-                    if (mHalReady.get()) {
-                        pw.println("HAL connection:");
-                        mHalWrapper.dump(pw, "\t");
-                        pw.println("Current temperatures from HAL:");
-                        dumpItemsLocked(pw, "\t",
-                                mHalWrapper.getCurrentTemperatures(false, 0));
-                        pw.println("Current cooling devices from HAL:");
-                        dumpItemsLocked(pw, "\t",
-                                mHalWrapper.getCurrentCoolingDevices(false, 0));
-                        pw.println("Temperature static thresholds from HAL:");
-                        dumpItemsLocked(pw, "\t",
-                                mHalWrapper.getTemperatureThresholds(false, 0));
-                    }
-                }
-            } finally {
-                Binder.restoreCallingIdentity(token);
-            }
+        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+            dumpInternal(fd, pw, args);
         }
 
         private boolean isCallerShell() {
@@ -567,6 +529,62 @@
 
     };
 
+    private static void dumpItemsLocked(PrintWriter pw, String prefix,
+            Collection<?> items) {
+        for (Iterator iterator = items.iterator(); iterator.hasNext();) {
+            pw.println(prefix + iterator.next().toString());
+        }
+    }
+
+    private static void dumpTemperatureThresholds(PrintWriter pw, String prefix,
+            List<TemperatureThreshold> thresholds) {
+        for (TemperatureThreshold threshold : thresholds) {
+            pw.println(prefix + "TemperatureThreshold{mType=" + threshold.type
+                    + ", mName=" + threshold.name
+                    + ", mHotThrottlingThresholds=" + Arrays.toString(
+                    threshold.hotThrottlingThresholds)
+                    + ", mColdThrottlingThresholds=" + Arrays.toString(
+                    threshold.coldThrottlingThresholds)
+                    + "}");
+        }
+    }
+
+    @VisibleForTesting
+    void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {
+        if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
+            return;
+        }
+        final long token = Binder.clearCallingIdentity();
+        try {
+            synchronized (mLock) {
+                pw.println("IsStatusOverride: " + mIsStatusOverride);
+                pw.println("ThermalEventListeners:");
+                mThermalEventListeners.dump(pw, "\t");
+                pw.println("ThermalStatusListeners:");
+                mThermalStatusListeners.dump(pw, "\t");
+                pw.println("Thermal Status: " + mStatus);
+                pw.println("Cached temperatures:");
+                dumpItemsLocked(pw, "\t", mTemperatureMap.values());
+                pw.println("HAL Ready: " + mHalReady.get());
+                if (mHalReady.get()) {
+                    pw.println("HAL connection:");
+                    mHalWrapper.dump(pw, "\t");
+                    pw.println("Current temperatures from HAL:");
+                    dumpItemsLocked(pw, "\t",
+                            mHalWrapper.getCurrentTemperatures(false, 0));
+                    pw.println("Current cooling devices from HAL:");
+                    dumpItemsLocked(pw, "\t",
+                            mHalWrapper.getCurrentCoolingDevices(false, 0));
+                    pw.println("Temperature static thresholds from HAL:");
+                    dumpTemperatureThresholds(pw, "\t",
+                            mHalWrapper.getTemperatureThresholds(false, 0));
+                }
+            }
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
+    }
+
     class ThermalShellCommand extends ShellCommand {
         @Override
         public int onCommand(String cmd) {
diff --git a/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
index 57f9f18..13c011a 100644
--- a/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
@@ -16,6 +16,8 @@
 
 package com.android.server.power;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -30,6 +32,7 @@
 import static org.mockito.Mockito.when;
 
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.hardware.thermal.TemperatureThreshold;
 import android.hardware.thermal.ThrottlingSeverity;
 import android.os.CoolingDevice;
@@ -55,7 +58,9 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
+import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -114,6 +119,7 @@
             skinThreshold.type = Temperature.TYPE_SKIN;
             skinThreshold.name = "skin1";
             skinThreshold.hotThrottlingThresholds = new float[7 /*ThrottlingSeverity#len*/];
+            skinThreshold.coldThrottlingThresholds = new float[7 /*ThrottlingSeverity#len*/];
             for (int i = 0; i < skinThreshold.hotThrottlingThresholds.length; ++i) {
                 // Sets NONE to 25.0f, SEVERE to 40.0f, and SHUTDOWN to 55.0f
                 skinThreshold.hotThrottlingThresholds[i] = 25.0f + 5.0f * i;
@@ -124,6 +130,7 @@
             cpuThreshold.type = Temperature.TYPE_CPU;
             cpuThreshold.name = "cpu";
             cpuThreshold.hotThrottlingThresholds = new float[7 /*ThrottlingSeverity#len*/];
+            cpuThreshold.coldThrottlingThresholds = new float[7 /*ThrottlingSeverity#len*/];
             for (int i = 0; i < cpuThreshold.hotThrottlingThresholds.length; ++i) {
                 if (i == ThrottlingSeverity.SEVERE) {
                     cpuThreshold.hotThrottlingThresholds[i] = 95.0f;
@@ -189,7 +196,8 @@
 
         @Override
         protected void dump(PrintWriter pw, String prefix) {
-            return;
+            pw.print(prefix);
+            pw.println("ThermalHAL AIDL 1  connected: yes");
         }
     }
 
@@ -496,4 +504,51 @@
             return watcher.mSamples.isEmpty();
         }
     }
+
+    @Test
+    public void testDump() {
+        when(mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP))
+                .thenReturn(PackageManager.PERMISSION_GRANTED);
+        final StringWriter out = new StringWriter();
+        PrintWriter pw = new PrintWriter(out);
+        mService.dumpInternal(new FileDescriptor(), pw, null);
+        final String dumpStr = out.toString();
+        assertThat(dumpStr).contains("IsStatusOverride: false");
+        assertThat(dumpStr).contains(
+                "ThermalEventListeners:\n"
+                        + "\tcallbacks: 2\n"
+                        + "\tkilled: false\n"
+                        + "\tbroadcasts count: -1");
+        assertThat(dumpStr).contains(
+                "ThermalStatusListeners:\n"
+                        + "\tcallbacks: 2\n"
+                        + "\tkilled: false\n"
+                        + "\tbroadcasts count: -1");
+        assertThat(dumpStr).contains("Thermal Status: 0");
+        assertThat(dumpStr).contains(
+                "Cached temperatures:\n"
+                + "\tTemperature{mValue=0.0, mType=4, mName=usbport, mStatus=0}\n"
+                + "\tTemperature{mValue=0.0, mType=2, mName=batt, mStatus=0}\n"
+                + "\tTemperature{mValue=0.0, mType=3, mName=skin1, mStatus=0}\n"
+                + "\tTemperature{mValue=0.0, mType=3, mName=skin2, mStatus=0}"
+        );
+        assertThat(dumpStr).contains("HAL Ready: true\n"
+                + "HAL connection:\n"
+                + "\tThermalHAL AIDL 1  connected: yes");
+        assertThat(dumpStr).contains("Current temperatures from HAL:\n"
+                + "\tTemperature{mValue=0.0, mType=3, mName=skin1, mStatus=0}\n"
+                + "\tTemperature{mValue=0.0, mType=3, mName=skin2, mStatus=0}\n"
+                + "\tTemperature{mValue=0.0, mType=2, mName=batt, mStatus=0}\n"
+                + "\tTemperature{mValue=0.0, mType=4, mName=usbport, mStatus=0}\n");
+        assertThat(dumpStr).contains("Current cooling devices from HAL:\n"
+                + "\tCoolingDevice{mValue=0, mType=1, mName=cpu}\n"
+                + "\tCoolingDevice{mValue=0, mType=1, mName=gpu}\n");
+        assertThat(dumpStr).contains("Temperature static thresholds from HAL:\n"
+                + "\tTemperatureThreshold{mType=3, mName=skin1, mHotThrottlingThresholds=[25.0, "
+                + "30.0, 35.0, 40.0, 45.0, 50.0, 55.0], mColdThrottlingThresholds=[0.0, 0.0, 0.0,"
+                + " 0.0, 0.0, 0.0, 0.0]}\n"
+                + "\tTemperatureThreshold{mType=0, mName=cpu, mHotThrottlingThresholds=[NaN, NaN,"
+                + " NaN, 95.0, NaN, NaN, NaN], mColdThrottlingThresholds=[0.0, 0.0, 0.0, 0.0, 0"
+                + ".0, 0.0, 0.0]}");
+    }
 }