Add GnssPsdsDownloadStatsTest

Test: atest GnssPsdsDownloadStatsTest
Bug: 220386318

Change-Id: Ib0345678f264c88d387d97c9d10501366ad25ccd
diff --git a/hostsidetests/statsdatom/src/android/cts/statsdatom/gnss/GnssPsdsDownloadStatsTest.java b/hostsidetests/statsdatom/src/android/cts/statsdatom/gnss/GnssPsdsDownloadStatsTest.java
new file mode 100644
index 0000000..1b43aa2
--- /dev/null
+++ b/hostsidetests/statsdatom/src/android/cts/statsdatom/gnss/GnssPsdsDownloadStatsTest.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2022 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 android.cts.statsdatom.gnss;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import android.cts.statsdatom.lib.AtomTestUtils;
+import android.cts.statsdatom.lib.ConfigUtils;
+import android.cts.statsdatom.lib.DeviceUtils;
+import android.cts.statsdatom.lib.ReportUtils;
+
+import com.android.os.AtomsProto;
+import com.android.os.StatsLog;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.log.LogUtil.CLog;
+import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.testtype.IBuildReceiver;
+
+import java.util.List;
+
+/**
+ * Test for GnssPsdsDownload stats
+ */
+public class GnssPsdsDownloadStatsTest extends DeviceTestCase implements IBuildReceiver {
+    private static final String LOCATION_DUMPSYS_COMMAND = "dumpsys location -a";
+    private static final String FORCE_PSDS_DOWNLOAD_COMMAND =
+            "cmd location providers send-extra-command gps force_psds_injection";
+    private static final String PSDS_SUPPORTED = "mSupportsPsds=true";
+    private static final long PSDS_DOWNLOAD_TIMEOUT_MILLIS = 5000;
+    private IBuildInfo mCtsBuild;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        assertThat(mCtsBuild).isNotNull();
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        DeviceUtils.installStatsdTestApp(getDevice(), mCtsBuild);
+        Thread.sleep(AtomTestUtils.WAIT_TIME_LONG);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        DeviceUtils.uninstallStatsdTestApp(getDevice());
+        super.tearDown();
+    }
+
+    @Override
+    public void setBuild(IBuildInfo buildInfo) {
+        mCtsBuild = buildInfo;
+    }
+
+    public void testGnssPsdsDownload() throws Exception {
+        String dumpsysOut = getDevice().executeShellCommand(LOCATION_DUMPSYS_COMMAND);
+        if (!dumpsysOut.contains(PSDS_SUPPORTED)) {
+            CLog.i("Skipping the test since GNSS PSDS is not supported.");
+            return;
+        }
+        ConfigUtils.uploadConfigForPushedAtom(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
+                AtomsProto.Atom.GNSS_PSDS_DOWNLOAD_REPORTED_FIELD_NUMBER);
+
+        getDevice().executeShellCommand(FORCE_PSDS_DOWNLOAD_COMMAND);
+        Thread.sleep(PSDS_DOWNLOAD_TIMEOUT_MILLIS);
+        List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice());
+        assertWithMessage("GNSS PSDS must be downloaded").that(data.size()).isAtLeast(1);
+    }
+}