Merge "[DO NOT MERGE] Create isYouTubePipModeOnLauncher() for youtubehelperimpl interface." into sc-v2-dev
diff --git a/libraries/compatibility-common-util/Android.bp b/libraries/compatibility-common-util/Android.bp
index 850a8bb..485a5f4 100644
--- a/libraries/compatibility-common-util/Android.bp
+++ b/libraries/compatibility-common-util/Android.bp
@@ -26,7 +26,6 @@
     static_libs: [
         "guava",
         "junit",
-        "platform-test-annotations",
     ],
 }
 
@@ -35,6 +34,7 @@
     visibility: [
         "//test/suite_harness/common/util",
         "//platform_testing/libraries/compatibility-common-util/tests",
+        "//platform_testing/libraries/sts-common-util/util",
     ],
     srcs: ["src/**/*.java"],
     host_supported: true,
diff --git a/libraries/compatibility-common-util/src/com/android/sts/OWNERS b/libraries/sts-common-util/OWNERS
similarity index 64%
rename from libraries/compatibility-common-util/src/com/android/sts/OWNERS
rename to libraries/sts-common-util/OWNERS
index d029d20..69d2081 100644
--- a/libraries/compatibility-common-util/src/com/android/sts/OWNERS
+++ b/libraries/sts-common-util/OWNERS
@@ -1,2 +1,3 @@
 # STS Owners
 cdombroski@google.com
+musashi@google.com
diff --git a/libraries/sts-common-util/device-side/Android.bp b/libraries/sts-common-util/device-side/Android.bp
new file mode 100644
index 0000000..1692a7c
--- /dev/null
+++ b/libraries/sts-common-util/device-side/Android.bp
@@ -0,0 +1,32 @@
+// Copyright (C) 2021 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+java_library_static {
+    name: "sts-device-util",
+    sdk_version: "test_current",
+
+    srcs: ["src/**/*.java"],
+
+    static_libs: [
+        "sts-common-util-devicesidelib",
+    ],
+
+    libs: [
+        "compatibility-device-util-axt",
+    ],
+}
diff --git a/libraries/sts-common-util/device-side/src/com/android/sts/common/util/StsExtraBusinessLogicTestCase.java b/libraries/sts-common-util/device-side/src/com/android/sts/common/util/StsExtraBusinessLogicTestCase.java
new file mode 100644
index 0000000..0eede16
--- /dev/null
+++ b/libraries/sts-common-util/device-side/src/com/android/sts/common/util/StsExtraBusinessLogicTestCase.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2021 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.sts.common.util;
+
+import com.android.compatibility.common.util.ExtraBusinessLogicTestCase;
+
+import android.os.Build;
+import android.util.Log;
+
+import androidx.test.InstrumentationRegistry;
+
+import org.junit.Rule;
+import org.junit.runner.Description;
+
+import java.time.LocalDate;
+import java.util.List;
+
+/** The device-side implementation of StsLogic. */
+public class StsExtraBusinessLogicTestCase extends ExtraBusinessLogicTestCase implements StsLogic {
+
+    private LocalDate deviceSpl = null;
+    @Rule public DescriptionProvider descriptionProvider = new DescriptionProvider();
+
+    protected StsExtraBusinessLogicTestCase() {
+        mDependentOnBusinessLogic = false;
+    }
+
+    @Override
+    public List<String> getExtraBusinessLogics() {
+        String stsDynamicPlan =
+                InstrumentationRegistry.getArguments().getString("sts-dynamic-plan");
+        switch (stsDynamicPlan) {
+            case "incremental":
+                return StsLogic.STS_EXTRA_BUSINESS_LOGIC_INCREMENTAL;
+            case "full":
+                return StsLogic.STS_EXTRA_BUSINESS_LOGIC_FULL;
+            default:
+                throw new RuntimeException(
+                        "Could not find Dynamic STS plan in InstrumentationRegistry arguments");
+        }
+    }
+
+    @Override
+    public Description getTestDescription() {
+        return descriptionProvider.getDescription();
+    }
+
+    @Override
+    public LocalDate getDeviceSpl() {
+        if (deviceSpl == null) {
+            deviceSpl = SplUtils.localDateFromSplString(Build.VERSION.SECURITY_PATCH);
+        }
+        return deviceSpl;
+    }
+
+    /**
+     * Specify the latest release bulletin. Control this from the command-line with the following:
+     * --test-arg
+     * com.android.tradefed.testtype.AndroidJUnitTest:instrumentation-arg:release-bulletin-spl:=2020-06
+     */
+    @Override
+    public LocalDate getReleaseBulletinSpl() {
+        String releaseBulletinSpl =
+                InstrumentationRegistry.getArguments().getString("release-bulletin-spl");
+        if (releaseBulletinSpl == null) {
+            return null;
+        }
+        // bulletin is released by month; add any day - only the year and month are compared.
+        releaseBulletinSpl =
+                String.format("%s-%02d", releaseBulletinSpl, SplUtils.Type.PARTIAL.day);
+        return SplUtils.localDateFromSplString(releaseBulletinSpl);
+    }
+
+    @Override
+    public void logInfo(String logTag, String format, Object... args) {
+        Log.i(logTag, String.format(format, args));
+    }
+
+    @Override
+    public void logDebug(String logTag, String format, Object... args) {
+        Log.d(logTag, String.format(format, args));
+    }
+
+    @Override
+    public void logWarn(String logTag, String format, Object... args) {
+        Log.w(logTag, String.format(format, args));
+    }
+
+    @Override
+    public void logError(String logTag, String format, Object... args) {
+        Log.e(logTag, String.format(format, args));
+    }
+}
diff --git a/libraries/sts-common-util/host-side/Android.bp b/libraries/sts-common-util/host-side/Android.bp
new file mode 100644
index 0000000..49f51f4
--- /dev/null
+++ b/libraries/sts-common-util/host-side/Android.bp
@@ -0,0 +1,33 @@
+// Copyright (C) 2021 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+java_library_host {
+    name: "sts-host-util",
+    defaults: ["cts_error_prone_rules"],
+
+    srcs: ["src/**/*.java"],
+
+    static_libs: [
+        "sts-common-util-lib",
+    ],
+
+    libs: [
+        "compatibility-tradefed",
+        "tradefed",
+    ],
+}
diff --git a/libraries/sts-common-util/host-side/src/com/android/sts/common/tradefed/testtype/StsExtraBusinessLogicHostTestBase.java b/libraries/sts-common-util/host-side/src/com/android/sts/common/tradefed/testtype/StsExtraBusinessLogicHostTestBase.java
new file mode 100644
index 0000000..c7fad5b
--- /dev/null
+++ b/libraries/sts-common-util/host-side/src/com/android/sts/common/tradefed/testtype/StsExtraBusinessLogicHostTestBase.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2021 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.sts.common.tradefed.testtype;
+
+import com.android.compatibility.common.tradefed.testtype.ExtraBusinessLogicHostTestBase;
+import com.android.ddmlib.Log;
+import com.android.sts.common.util.DescriptionProvider;
+import com.android.sts.common.util.SplUtils;
+import com.android.sts.common.util.StsLogic;
+import com.android.tradefed.device.DeviceNotAvailableException;
+
+import org.junit.Rule;
+import org.junit.runner.Description;
+
+import java.time.LocalDate;
+import java.util.List;
+
+/** The host-side implementation of StsLogic. */
+public class StsExtraBusinessLogicHostTestBase extends ExtraBusinessLogicHostTestBase
+        implements StsLogic {
+
+    private LocalDate deviceSpl = null;
+    @Rule public DescriptionProvider descriptionProvider = new DescriptionProvider();
+
+    public StsExtraBusinessLogicHostTestBase() {
+        super();
+        mDependentOnBusinessLogic = false;
+    }
+
+    @Override
+    public List<String> getExtraBusinessLogics() {
+        String stsDynamicPlan = getBuild().getBuildAttributes().get("sts-dynamic-plan");
+        switch (stsDynamicPlan) {
+            case "incremental":
+                return StsLogic.STS_EXTRA_BUSINESS_LOGIC_INCREMENTAL;
+            case "full":
+                return StsLogic.STS_EXTRA_BUSINESS_LOGIC_FULL;
+            default:
+                throw new RuntimeException("Could not find Dynamic STS plan in build attributes");
+        }
+    }
+
+    @Override
+    public Description getTestDescription() {
+        return descriptionProvider.getDescription();
+    }
+
+    @Override
+    public LocalDate getDeviceSpl() {
+        if (deviceSpl == null) {
+            try {
+                String splString = getDevice().getProperty("ro.build.version.security_patch");
+                deviceSpl = SplUtils.localDateFromSplString(splString);
+            } catch (DeviceNotAvailableException e) {
+                throw new RuntimeException("couldn't get the security patch level", e);
+            }
+        }
+        return deviceSpl;
+    }
+
+    /**
+     * Specify the latest release bulletin. Control this from the command-line with the following
+     * command line argument: --build-attribute "release-bulletin-spl=2021-06"
+     */
+    @Override
+    public LocalDate getReleaseBulletinSpl() {
+        String releaseBulletinSpl = getBuild().getBuildAttributes().get("release-bulletin-spl");
+        if (releaseBulletinSpl == null) {
+            return null;
+        }
+        // bulletin is released by month; add any day - only the year and month are compared.
+        releaseBulletinSpl =
+                String.format("%s-%02d", releaseBulletinSpl, SplUtils.Type.PARTIAL.day);
+        return SplUtils.localDateFromSplString(releaseBulletinSpl);
+    }
+
+    @Override
+    public void logInfo(String logTag, String format, Object... args) {
+        Log.i(logTag, String.format(format, args));
+    }
+
+    @Override
+    public void logDebug(String logTag, String format, Object... args) {
+        Log.d(logTag, String.format(format, args));
+    }
+
+    @Override
+    public void logWarn(String logTag, String format, Object... args) {
+        Log.w(logTag, String.format(format, args));
+    }
+
+    @Override
+    public void logError(String logTag, String format, Object... args) {
+        Log.e(logTag, String.format(format, args));
+    }
+}
diff --git a/libraries/sts-common-util/util/Android.bp b/libraries/sts-common-util/util/Android.bp
new file mode 100644
index 0000000..403dcc2
--- /dev/null
+++ b/libraries/sts-common-util/util/Android.bp
@@ -0,0 +1,48 @@
+// Copyright (C) 2020 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+// Build the common utility library for use device-side
+java_library_static {
+    name: "sts-common-util-devicesidelib",
+    visibility: [
+        "//platform_testing/libraries/sts-common-util/device-side",
+    ],
+    sdk_version: "current",
+    srcs: ["src/**/*.java"],
+    static_libs: [
+        "junit",
+        "platform-test-annotations",
+        "compatibility-common-util-devicesidelib",
+    ],
+}
+
+java_library {
+    name: "sts-common-util-lib",
+    visibility: [
+        "//platform_testing/libraries/sts-common-util/host-side",
+    ],
+    host_supported: true,
+    srcs: ["src/**/*.java"],
+    static_libs: [
+        "compatibility-common-util-lib",
+    ],
+    libs: [
+        "junit",
+        "platform-test-annotations",
+    ],
+}
diff --git a/libraries/sts-common-util/util/src/com/android/sts/common/util/DescriptionProvider.java b/libraries/sts-common-util/util/src/com/android/sts/common/util/DescriptionProvider.java
new file mode 100644
index 0000000..60d46ea
--- /dev/null
+++ b/libraries/sts-common-util/util/src/com/android/sts/common/util/DescriptionProvider.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2021 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.sts.common.util;
+
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/** Provide a way for tests to get their description while running. */
+public class DescriptionProvider extends TestWatcher {
+    private volatile Description description;
+
+    @Override
+    protected void starting(Description description) {
+        this.description = description;
+    }
+
+    public Description getDescription() {
+        return description;
+    }
+}
diff --git a/libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java b/libraries/sts-common-util/util/src/com/android/sts/common/util/SplUtils.java
similarity index 86%
rename from libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java
rename to libraries/sts-common-util/util/src/com/android/sts/common/util/SplUtils.java
index 4144fff..afb7b12 100644
--- a/libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java
+++ b/libraries/sts-common-util/util/src/com/android/sts/common/util/SplUtils.java
@@ -26,6 +26,17 @@
     private static final ZoneId UTC_ZONE_ID = ZoneId.of("UTC");
     private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
+    public enum Type {
+        PARTIAL(1), // platform
+        COMPLETE(5); // device-specific (kernel, soc, etc)
+
+        public final int day;
+
+        Type(int day) {
+            this.day = day;
+        }
+    }
+
     public static LocalDate localDateFromMillis(long millis) {
         return Instant.ofEpochMilli(millis).atZone(UTC_ZONE_ID).toLocalDate();
     }
diff --git a/libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java b/libraries/sts-common-util/util/src/com/android/sts/common/util/StsLogic.java
similarity index 72%
rename from libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java
rename to libraries/sts-common-util/util/src/com/android/sts/common/util/StsLogic.java
index 1dec855..648199c 100644
--- a/libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java
+++ b/libraries/sts-common-util/util/src/com/android/sts/common/util/StsLogic.java
@@ -17,11 +17,11 @@
 package com.android.sts.common.util;
 
 import static org.junit.Assume.*;
+import static org.junit.Assert.*;
 
 import android.platform.test.annotations.AsbSecurityTest;
 
 import com.android.compatibility.common.util.BusinessLogicMapStore;
-import com.android.compatibility.common.util.MultiLog;
 
 import org.junit.runner.Description;
 
@@ -31,7 +31,7 @@
 import java.util.Map;
 
 /** Common STS extra business logic for host-side and device-side to implement. */
-public interface StsLogic extends MultiLog {
+public interface StsLogic {
 
     static final String LOG_TAG = StsLogic.class.getSimpleName();
 
@@ -40,10 +40,12 @@
     List<String> STS_EXTRA_BUSINESS_LOGIC_FULL = Arrays.asList(new String[]{
         "uploadSpl",
         "uploadModificationTime",
+        "declaredSpl",
     });
     List<String> STS_EXTRA_BUSINESS_LOGIC_INCREMENTAL = Arrays.asList(new String[]{
         "uploadSpl",
         "uploadModificationTime",
+        "declaredSpl",
         "incremental",
     });
 
@@ -51,6 +53,8 @@
 
     LocalDate getDeviceSpl();
 
+    LocalDate getReleaseBulletinSpl();
+
     default long[] getCveBugIds() {
         AsbSecurityTest annotation = getTestDescription().getAnnotation(AsbSecurityTest.class);
         if (annotation == null) {
@@ -59,6 +63,16 @@
         return annotation.cveBugId();
     }
 
+    default boolean isBugSplDataKnownMissing() {
+        long[] bugIds = getCveBugIds();
+        if (bugIds == null) {
+            // no spl data, don't complain
+            return true;
+        }
+        // true if the bug id is older than ~ June 2020
+        return Arrays.stream(bugIds).min().getAsLong() < 157905780;
+    }
+
     default LocalDate getMinTestSpl() {
         Map<String, String> map = BusinessLogicMapStore.getMap("security_bulletins");
         if (map == null) {
@@ -73,7 +87,7 @@
                 // binary. Neither is a critical issue and the test will run in these cases.
                 // New test: developer should be able to write the test without getting blocked.
                 // Removed bug + old binary: test will run.
-                logInfo(LOG_TAG, "could not find the CVE bug %d in the spl map", cveBugId);
+                logWarn(LOG_TAG, "could not find the CVE bug %d in the spl map", cveBugId);
                 continue;
             }
             LocalDate spl = SplUtils.localDateFromSplString(splString);
@@ -127,13 +141,11 @@
         LocalDate minTestModifiedDate = getMinModificationDate();
         if (minTestModifiedDate == null) {
             // could not get the modification date - run the test
-            if (Arrays.stream(bugIds).min().getAsLong() < 157905780) {
-                // skip if the bug id is older than ~ June 2020
-                // otherwise the test will run due to missing data
+            if (isBugSplDataKnownMissing()) {
                 logDebug(LOG_TAG, "no data for this old test");
                 return true;
             }
-          return false;
+            return false;
         }
         if (minTestModifiedDate.isAfter(incrementalCutoffSpl)) {
             logDebug(LOG_TAG, "the test was recently modified");
@@ -155,11 +167,43 @@
         return true;
     }
 
-    default boolean shouldSkipSpl() {
-        return true;
+    default boolean shouldSkipDeclaredSpl() {
+        if (getCveBugIds() == null) {
+            // There were no @AsbSecurityTest annotations
+            logInfo(LOG_TAG, "not an ASB test");
+            return false;
+        }
+
+        LocalDate releaseBulletinSpl = getReleaseBulletinSpl();
+        LocalDate minTestSpl = getMinTestSpl();
+        if (releaseBulletinSpl != null && !isBugSplDataKnownMissing()) {
+            // assert that the test has a known SPL when we expect the data to be fresh
+            assertNotNull("Unknown SPL for new CVE", minTestSpl);
+
+            // set the days to be the same so we only compare year-month
+            releaseBulletinSpl = releaseBulletinSpl.withDayOfMonth(minTestSpl.getDayOfMonth());
+            // the test SPL can't be equal to or after the release bulletin SPL
+            assertFalse("Newer SPL than release bulletin", releaseBulletinSpl.isBefore(minTestSpl));
+        }
+        if (minTestSpl == null) {
+            // no SPL for this test; run normally
+            return false;
+        }
+
+        // skip if the test is newer than the device SPL
+        LocalDate deviceSpl = getDeviceSpl();
+        return minTestSpl.isAfter(deviceSpl);
     }
 
     default void skip(String message) {
         assumeTrue(message, false);
     }
+
+    public void logInfo(String logTag, String format, Object... args);
+
+    public void logDebug(String logTag, String format, Object... args);
+
+    public void logWarn(String logTag, String format, Object... args);
+
+    public void logError(String logTag, String format, Object... args);
 }
diff --git a/tests/functional/devicehealthchecks/assets/bug_map b/tests/functional/devicehealthchecks/assets/bug_map
index 62898e2..9cbe057 100644
--- a/tests/functional/devicehealthchecks/assets/bug_map
+++ b/tests/functional/devicehealthchecks/assets/bug_map
@@ -8,6 +8,7 @@
 system_app_anr executing\sservice\scom.google.android.as/com.google.android.apps.miphone.aiai.echo.scheduler.EchoJobService 192300119
 system_app_anr executing\sservice\scom.google.android.as/com.google.android.apps.miphone.aiai.actions.service.ActionRankingDataTtlService 192300119
 system_app_anr executing\sservice\scom.android.se/.SecureElementService 199457346
+system_app_anr act=android.telephony.action.CARRIER_CONFIG_CHANGED\sflg=0x15000010\scmp=com.android.phone/.otasp.OtaspSimStateReceiver 205896452
 system_app_crash -1\|android\|26\|null\|1000 155073214
 system_app_crash -1\|android\|32\|null\|1000 155073214
 system_app_crash android.database.sqlite.SQLiteCloseable.acquireReference 159658068