Merge "VolumeShaperTest: Use AudioManager to get new session id" into pi-dev
diff --git a/hostsidetests/incident/src/com/android/server/cts/MemInfoIncidentTest.java b/hostsidetests/incident/src/com/android/server/cts/MemInfoIncidentTest.java
index 4ab421b..20a6cab 100644
--- a/hostsidetests/incident/src/com/android/server/cts/MemInfoIncidentTest.java
+++ b/hostsidetests/incident/src/com/android/server/cts/MemInfoIncidentTest.java
@@ -26,7 +26,7 @@
 
     public void testMemInfoDump() throws Exception {
         final MemInfoDumpProto dump =
-                getDump(MemInfoDumpProto.parser(), "dumpsys meminfo -a --proto");
+                getDump(MemInfoDumpProto.parser(), "dumpsys -t 30000 meminfo -a --proto");
 
         verifyMemInfoDumpProto(dump, PRIVACY_NONE);
     }
diff --git a/hostsidetests/seccomp/app/src/android/seccomp/cts/app/SeccompDeviceTest.java b/hostsidetests/seccomp/app/src/android/seccomp/cts/app/SeccompDeviceTest.java
index 724d235..42ea6c2 100644
--- a/hostsidetests/seccomp/app/src/android/seccomp/cts/app/SeccompDeviceTest.java
+++ b/hostsidetests/seccomp/app/src/android/seccomp/cts/app/SeccompDeviceTest.java
@@ -85,9 +85,9 @@
         } else if (CpuFeatures.isArmCpu()) {
             return "arm";
         } else if (CpuFeatures.isX86_64Cpu()) {
-            return "x86";
-        } else if (CpuFeatures.isX86Cpu()) {
             return "x86_64";
+        } else if (CpuFeatures.isX86Cpu()) {
+            return "x86";
         } else if (CpuFeatures.isMips64Cpu()) {
             return "mips64";
         } else if (CpuFeatures.isMipsCpu()) {
diff --git a/hostsidetests/statsd/src/android/cts/statsd/metadata/MetadataTestCase.java b/hostsidetests/statsd/src/android/cts/statsd/metadata/MetadataTestCase.java
new file mode 100644
index 0000000..6e9a462
--- /dev/null
+++ b/hostsidetests/statsd/src/android/cts/statsd/metadata/MetadataTestCase.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2018 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.statsd.metadata;
+
+import android.cts.statsd.atom.AtomTestCase;
+import com.android.internal.os.StatsdConfigProto;
+import com.android.internal.os.StatsdConfigProto.StatsdConfig;
+import com.android.os.AtomsProto.Atom;
+import com.android.os.StatsLog.StatsdStatsReport;
+import com.android.tradefed.log.LogUtil;
+
+public class MetadataTestCase extends AtomTestCase {
+    public static final String DUMP_METADATA_CMD = "cmd stats print-stats";
+
+    protected StatsdStatsReport getStatsdStatsReport() throws Exception {
+        try {
+            StatsdStatsReport report = getDump(StatsdStatsReport.parser(),
+                    String.join(" ", DUMP_METADATA_CMD, "--proto"));
+            return report;
+        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
+            LogUtil.CLog.e("Failed to fetch and parse the statsdstats output report.");
+            throw (e);
+        }
+    }
+
+    protected final StatsdConfig.Builder getBaseConfig() throws Exception {
+        StatsdConfig.Builder builder =  StatsdConfig.newBuilder().setId(CONFIG_ID)
+                .addAllowedLogSource("AID_SYSTEM");
+        addAtomEvent(builder, Atom.SCREEN_STATE_CHANGED_FIELD_NUMBER);
+        return builder;
+    }
+}
diff --git a/hostsidetests/statsd/src/android/cts/statsd/metadata/MetadataTests.java b/hostsidetests/statsd/src/android/cts/statsd/metadata/MetadataTests.java
new file mode 100644
index 0000000..5cb37d3
--- /dev/null
+++ b/hostsidetests/statsd/src/android/cts/statsd/metadata/MetadataTests.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2018 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.statsd.metadata;
+
+import android.cts.statsd.atom.AtomTestCase;
+
+import com.android.internal.os.StatsdConfigProto;
+import com.android.internal.os.StatsdConfigProto.StatsdConfig;
+import com.android.internal.os.StatsdConfigProto.Subscription;
+import com.android.internal.os.StatsdConfigProto.TimeUnit;
+import com.android.internal.os.StatsdConfigProto.ValueMetric;
+import com.android.os.AtomsProto.AnomalyDetected;
+import com.android.os.AtomsProto.AppBreadcrumbReported;
+import com.android.os.AtomsProto.Atom;
+import com.android.os.StatsLog.EventMetricData;
+import com.android.os.StatsLog.StatsdStatsReport;
+import com.android.os.StatsLog.StatsdStatsReport.ConfigStats;
+import com.android.tradefed.log.LogUtil;
+
+
+import java.util.List;
+
+/**
+ * Statsd Anomaly Detection tests.
+ */
+public class MetadataTests extends MetadataTestCase {
+
+    private static final String TAG = "Statsd.MetadataTests";
+
+    // Tests that anomaly detection for value works.
+    public void testConfigTtl() throws Exception {
+        final int TTL_TIME_SEC = 3;
+        StatsdConfig.Builder config = getBaseConfig();
+        config.setTtlInSeconds(TTL_TIME_SEC); // should reset in 3 seconds.
+        turnScreenOff();
+
+        uploadConfig(config);
+        Thread.sleep(WAIT_TIME_SHORT);
+        turnScreenOn();
+        Thread.sleep(WAIT_TIME_SHORT);
+        StatsdStatsReport report = getStatsdStatsReport(); // Has only been 1 second
+        LogUtil.CLog.d("got following statsdstats report: " + report.toString());
+        boolean foundActiveConfig = false;
+        int creationTime = 0;
+        for (ConfigStats stats: report.getConfigStatsList()) {
+            if (stats.getId() == CONFIG_ID) {
+                if(!stats.hasDeletionTimeSec()) {
+                    assertTrue("Found multiple active CTS configs!", foundActiveConfig == false);
+                    foundActiveConfig = true;
+                    creationTime = stats.getCreationTimeSec();
+                }
+            }
+        }
+        assertTrue("Did not find an active CTS config", foundActiveConfig);
+
+        turnScreenOff();
+        Thread.sleep(WAIT_TIME_LONG); // Has been 3 seconds, config should TTL
+        turnScreenOn(); // Force events to make sure the config TTLs.
+        report = getStatsdStatsReport();
+        LogUtil.CLog.d("got following statsdstats report: " + report.toString());
+        foundActiveConfig = false;
+        int expectedTime = creationTime + TTL_TIME_SEC;
+        for (ConfigStats stats: report.getConfigStatsList()) {
+            if (stats.getId() == CONFIG_ID) {
+                // Original config should be TTL'd
+                if (stats.getCreationTimeSec() == creationTime) {
+                    assertTrue("Config should have TTL'd but is still active",
+                            stats.hasDeletionTimeSec());
+                    assertTrue("Config deletion time should be about " + TTL_TIME_SEC +
+                            " after creation",
+                            Math.abs(stats.getDeletionTimeSec() - expectedTime) <= 1);
+                }
+                // There should still be one active config, that is marked as reset.
+                if(!stats.hasDeletionTimeSec()) {
+                    assertTrue("Found multiple active CTS configs!", foundActiveConfig == false);
+                    foundActiveConfig = true;
+                    creationTime = stats.getCreationTimeSec();
+                    assertTrue("Active config after TTL should be marked as reset",
+                            stats.hasResetTimeSec());
+                    assertEquals("Reset time and creation time should be equal for TTl'd configs",
+                            stats.getResetTimeSec(), stats.getCreationTimeSec());
+                    assertTrue("Reset config should be created when the original config TTL'd",
+                            Math.abs(stats.getCreationTimeSec() - expectedTime) <= 1);
+                }
+            }
+        }
+        assertTrue("Did not find an active CTS config after the TTL", foundActiveConfig);
+        turnScreenOff();
+    }
+}
diff --git a/tests/tests/telephony/Android.mk b/tests/tests/telephony/Android.mk
index f19dbb5..6f3d71a 100644
--- a/tests/tests/telephony/Android.mk
+++ b/tests/tests/telephony/Android.mk
@@ -26,7 +26,8 @@
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
     ctstestrunner \
-    compatibility-device-util
+    compatibility-device-util \
+    truth-prebuilt
 
 LOCAL_HOST_SHARED_LIBRARIES := compatibility-device-telephony-preconditions
 
diff --git a/tests/tests/telephony/src/android/telephony/cts/RadioAccessSpecifierTest.java b/tests/tests/telephony/src/android/telephony/cts/RadioAccessSpecifierTest.java
new file mode 100644
index 0000000..693472f
--- /dev/null
+++ b/tests/tests/telephony/src/android/telephony/cts/RadioAccessSpecifierTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2018 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.telephony.cts;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Parcel;
+import android.telephony.RadioAccessSpecifier;
+import android.test.AndroidTestCase;
+
+public class RadioAccessSpecifierTest extends AndroidTestCase {
+    public void testConstructorAndGetters() {
+        // Constructor and getters.
+        int ran = 1;
+        int[] bands = {1, 2, 3, 4};
+        int[] channels = {5, 6, 7};
+        RadioAccessSpecifier radioAccessSpecifier = new RadioAccessSpecifier(ran, bands, channels);
+        assertThat(radioAccessSpecifier.describeContents()).isEqualTo(0);
+        assertThat(radioAccessSpecifier.getRadioAccessNetwork()).isEqualTo(ran);
+        assertThat(radioAccessSpecifier.getBands()).isEqualTo(bands);
+        assertThat(radioAccessSpecifier.getChannels()).isEqualTo(channels);
+
+        // Comparision method.
+        RadioAccessSpecifier toCompare1 = new RadioAccessSpecifier(ran, bands, channels);
+        RadioAccessSpecifier toCompare2 = new RadioAccessSpecifier(ran, new int[] {1, 2, 3, 4},
+                new int[] {5, 6, 7});
+        RadioAccessSpecifier toCompare3 = new RadioAccessSpecifier(ran+1, bands, channels);
+        assertThat(radioAccessSpecifier).isEqualTo(toCompare1);
+        assertThat(radioAccessSpecifier).isEqualTo(toCompare2);
+        assertThat(radioAccessSpecifier).isNotEqualTo(toCompare3);
+
+        // Parcel read and write.
+        Parcel stateParcel = Parcel.obtain();
+        radioAccessSpecifier.writeToParcel(stateParcel, 0);
+        stateParcel.setDataPosition(0);
+        toCompare1 = RadioAccessSpecifier.CREATOR.createFromParcel(stateParcel);
+        assertThat(radioAccessSpecifier).isEqualTo(toCompare1);
+
+        // Other methods.
+        assertThat(radioAccessSpecifier.hashCode()).isGreaterThan(0);
+        assertThat(radioAccessSpecifier.toString()).isNotNull();
+    }
+}
diff --git a/tests/tests/telephony/src/android/telephony/cts/ServiceStateTest.java b/tests/tests/telephony/src/android/telephony/cts/ServiceStateTest.java
index ce9d72e..47f1429 100644
--- a/tests/tests/telephony/src/android/telephony/cts/ServiceStateTest.java
+++ b/tests/tests/telephony/src/android/telephony/cts/ServiceStateTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2018 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.
@@ -15,7 +15,12 @@
  */
 package android.telephony.cts;
 
+import static android.telephony.ServiceState.DUPLEX_MODE_FDD;
+import static android.telephony.ServiceState.DUPLEX_MODE_TDD;
+import static android.telephony.ServiceState.DUPLEX_MODE_UNKNOWN;
+
 import android.os.Parcel;
+import android.telephony.AccessNetworkConstants;
 import android.telephony.ServiceState;
 import android.test.AndroidTestCase;
 
@@ -25,6 +30,9 @@
     private static final String OPERATOR_NUMERIC = "02871";
     private static final int SYSTEM_ID = 123;
     private static final int NETWORK_ID = 456;
+    private static final int CHANNEL_NUMBER_BAND_66 = 66436;
+    private static final int CHANNEL_NUMBER_BAND_33 = 36000;
+    private static final int[] CELL_BANDWIDTH = {1, 2, 3};
 
     public void testServiceState() {
         ServiceState serviceState = new ServiceState();
@@ -59,6 +67,21 @@
         assertEquals(SYSTEM_ID, serviceState.getCdmaSystemId());
         assertEquals(NETWORK_ID, serviceState.getCdmaNetworkId());
 
+        serviceState.setChannelNumber(CHANNEL_NUMBER_BAND_66);
+        assertEquals(CHANNEL_NUMBER_BAND_66, serviceState.getChannelNumber());
+
+        serviceState.setCellBandwidths(CELL_BANDWIDTH);
+        assertEquals(CELL_BANDWIDTH, serviceState.getCellBandwidths());
+
+        serviceState.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
+        assertEquals(DUPLEX_MODE_UNKNOWN, serviceState.getDuplexMode());
+
+        serviceState.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
+        assertEquals(DUPLEX_MODE_FDD, serviceState.getDuplexMode());
+
+        serviceState.setChannelNumber(CHANNEL_NUMBER_BAND_33);
+        assertEquals(DUPLEX_MODE_TDD, serviceState.getDuplexMode());
+
         assertTrue(serviceState.hashCode() > 0);
         assertNotNull(serviceState.toString());