Test for ION heap size atom

Bug: 140788538
Test: atest UidAtomTests#testIonHeapSize
Change-Id: Iefb62e2b7fe87a714de48ca704557c763db0e6e8
diff --git a/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java b/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java
index 1dfdd00..5e9e2ba 100644
--- a/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java
+++ b/hostsidetests/statsd/src/android/cts/statsd/atom/UidAtomTests.java
@@ -43,6 +43,7 @@
 import com.android.os.AtomsProto.ForegroundServiceStateChanged;
 import com.android.os.AtomsProto.GpsScanStateChanged;
 import com.android.os.AtomsProto.HiddenApiUsed;
+import com.android.os.AtomsProto.IonHeapSize;
 import com.android.os.AtomsProto.LooperStats;
 import com.android.os.AtomsProto.LmkKillOccurred;
 import com.android.os.AtomsProto.MediaCodecStateChanged;
@@ -1189,6 +1190,36 @@
             .that(foundSystemServer).isTrue();
     }
 
+    public void testIonHeapSize() throws Exception {
+        if (statsdDisabled() || !fileExists("/sys/kernel/ion/total_heaps_kb")) {
+            return;
+        }
+
+        // Get IonHeapSize as a simple gauge metric.
+        StatsdConfig.Builder config = getPulledConfig();
+        addGaugeAtomWithDimensions(config, Atom.ION_HEAP_SIZE_FIELD_NUMBER, null);
+        uploadConfig(config);
+        Thread.sleep(WAIT_TIME_SHORT);
+
+        // Start test app and trigger a pull while it is running.
+        try (AutoCloseable a = withActivity("StatsdCtsForegroundActivity", "action",
+                "action.show_notification")) {
+            setAppBreadcrumbPredicate();
+            Thread.sleep(WAIT_TIME_LONG);
+        }
+
+        List<Atom> atoms = getGaugeMetricDataList();
+        assertThat(atoms).hasSize(1);
+        IonHeapSize ionHeapSize = atoms.get(0).getIonHeapSize();
+        assertThat(ionHeapSize.getTotalSizeKb()).isGreaterThan(0);
+    }
+
+    private boolean fileExists(String path) throws Exception {
+        String commandFormat = "test -f %s && echo \"yes\" || echo \"no\"";
+        String result = getDevice().executeShellCommand(String.format(commandFormat, path));
+        return result.trim().equals("yes");
+    }
+
     /**
      * The the app id from a uid.
      *