Merge tag 'android-13.0.0_r52' into int/13/fp3

Android 13.0.0 Release 52 (TQ3A.230605.012)

* tag 'android-13.0.0_r52':
  DO NOT MERGE ANYWHERE: Temporarily disable tests broken by prebuilts
  Fix extension keys test for 2022-11 on Go
  Fix SdkExt e2e test to pass on R/S
  DO NOT MERGE Allow extension version 5
  DO NOT MERGE Allow extension version 5
  DO NOT MERGE Allow extension version 5
  DO NOT MERGE: Backport test changes
  DO NOT MERGE Allow extension version 4
  DO NOT MERGE Allow extension version 4
  DO NOT MERGE Allow extension version 4
  DO NOT MERGE Update S CTS to allow extension version 3
  DO NOT MERGE Update S-v2 CTS to allow extension version 3

Change-Id: I69e73690caeddfa794c5598b1b5dc1cf66ebd8bc
diff --git a/derive_sdk/derive_sdk_test.cpp b/derive_sdk/derive_sdk_test.cpp
index b04b5fd..ecd8033 100644
--- a/derive_sdk/derive_sdk_test.cpp
+++ b/derive_sdk/derive_sdk_test.cpp
@@ -102,6 +102,7 @@
 };
 
 TEST_F(DeriveSdkTest, CurrentSystemImageValue) {
+  GTEST_SKIP() << "Ignoring b/274987782";
   EXPECT_ALL(3);
 }
 
diff --git a/java/com/android/os/ext/testing/CurrentVersion.java b/java/com/android/os/ext/testing/CurrentVersion.java
index 8b5a7ed..0998e76 100644
--- a/java/com/android/os/ext/testing/CurrentVersion.java
+++ b/java/com/android/os/ext/testing/CurrentVersion.java
@@ -15,7 +15,6 @@
  */
 package com.android.os.ext.testing;
 
-import java.util.Collections;
 import java.util.Set;
 
 /**
@@ -24,8 +23,20 @@
  */
 public class CurrentVersion {
 
-    /** The current extension version */
-    public static final int V = 3;
+    /** The latest train's version */
+    public static final int CURRENT_TRAIN_VERSION = 5;
+
+    /** The version R shipped with (0) */
+    public static final int R_BASE_VERSION = 0;
+
+    /** The version S shipped with (1) */
+    public static final int S_BASE_VERSION = 1;
+
+    /** The version T shipped with (3) */
+    public static final int T_BASE_VERSION = 3;
+
+    /** The current platform's version */
+    public static final int CURRENT_BASE_VERSION = T_BASE_VERSION;
 
     /**
      * The current SDK Extension versions to expect / allow in CTS.
@@ -36,8 +47,8 @@
      * When a new extension version is defined, this Set must therefore be extended to include the
      * new version.
      */
-    public static final Set<Integer> ALLOWED_VERSIONS = Collections.unmodifiableSet(Set.of(
-        V
-    ));
+    public static final Set<Integer> ALLOWED_VERSIONS_CTS =
+        CURRENT_BASE_VERSION == CURRENT_TRAIN_VERSION ? Set.of(CURRENT_BASE_VERSION)
+            : Set.of(CURRENT_BASE_VERSION, 4, CURRENT_TRAIN_VERSION);
 
 }
diff --git a/javatests/com/android/os/ext/Android.bp b/javatests/com/android/os/ext/Android.bp
index 89e22af..27d065c 100644
--- a/javatests/com/android/os/ext/Android.bp
+++ b/javatests/com/android/os/ext/Android.bp
@@ -24,6 +24,7 @@
         "ctstestrunner-axt",
         "modules-utils-build",
         "test_util_current_version",
+        "truth-prebuilt",
     ],
     srcs: ["*.java"],
     test_config: "CtsSdkExtensionsTestCases.xml",
diff --git a/javatests/com/android/os/ext/SdkExtensionsTest.java b/javatests/com/android/os/ext/SdkExtensionsTest.java
index f8cbacf..88d6aac 100644
--- a/javatests/com/android/os/ext/SdkExtensionsTest.java
+++ b/javatests/com/android/os/ext/SdkExtensionsTest.java
@@ -17,22 +17,36 @@
 package com.android.os.ext;
 
 import static android.os.Build.VERSION_CODES;
-import static com.android.os.ext.testing.CurrentVersion.ALLOWED_VERSIONS;
+import static android.os.Build.VERSION_CODES.R;
+import static android.os.Build.VERSION_CODES.S;
+import static android.os.Build.VERSION_CODES.TIRAMISU;
+import static com.android.os.ext.testing.CurrentVersion.ALLOWED_VERSIONS_CTS;
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assume.assumeTrue;
 
 import android.os.SystemProperties;
 import android.os.ext.SdkExtensions;
+import androidx.test.runner.AndroidJUnit4;
 import com.android.modules.utils.build.SdkLevel;
-import junit.framework.TestCase;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
-public class SdkExtensionsTest extends TestCase {
+@RunWith(AndroidJUnit4.class)
+public class SdkExtensionsTest {
 
-    private static void assertCorrectVersion(int version) throws Exception {
-        assertTrue(ALLOWED_VERSIONS.contains(version));
+    private static final int AD_SERVICES = 1000000;
+
+    private static void assertCorrectVersion(int version) {
+        assertThat(version).isIn(ALLOWED_VERSIONS_CTS);
     }
 
-    private static void assertCorrectVersion(boolean expected, int version) throws Exception {
+    private static void assertCorrectVersion(boolean expected, int version) {
         if (expected) {
             assertCorrectVersion(version);
         } else {
@@ -40,7 +54,7 @@
         }
     }
 
-    private static void assertCorrectVersion(boolean expected, String propValue) throws Exception {
+    private static void assertCorrectVersion(boolean expected, String propValue) {
         if (expected) {
             int version = Integer.parseInt(propValue);
             assertCorrectVersion(version);
@@ -49,55 +63,80 @@
         }
     }
 
-    /** Verify that getExtensionVersion only accepts valid extension SDKs */
-    public void testBadArgument() throws Exception {
-        // R is the first SDK version with extensions.
-        for (int sdk = -1_000_000; sdk < VERSION_CODES.R; sdk++) {
-            try {
-                SdkExtensions.getExtensionVersion(sdk);
-                fail("expected IllegalArgumentException");
-            } catch (IllegalArgumentException expected) { }
+    private static final void assertCorrectVersion(boolean expected, int extension, String propId) {
+        String prop = "build.version.extensions." + propId;
+        assertCorrectVersion(expected, SystemProperties.get(prop));
+        assertCorrectVersion(expected, SdkExtensions.getExtensionVersion(extension));
+        if (expected) {
+            assertCorrectVersion(true, SdkExtensions.getAllExtensionVersions().get(extension));
         }
     }
 
-    /** Verifies that getExtensionVersion only return existing versions */
-    public void testValidValues() throws Exception {
-        assertCorrectVersion(true, SdkExtensions.getExtensionVersion(VERSION_CODES.R));
-        assertCorrectVersion(
-            SdkLevel.isAtLeastS(), SdkExtensions.getExtensionVersion(VERSION_CODES.S));
-        assertCorrectVersion(
-            SdkLevel.isAtLeastT(), SdkExtensions.getExtensionVersion(VERSION_CODES.TIRAMISU));
+    /** Verify that getExtensionVersion only accepts valid extension SDKs */
+    @Test
+    public void testBadArgument() throws Exception {
+        // R is the first SDK version with extensions.
+        for (int sdk = -1_000_000; sdk < VERSION_CODES.R; sdk++) {
+            final int finalSdk = sdk;
+            assertThrows(IllegalArgumentException.class,
+                    () -> SdkExtensions.getExtensionVersion(finalSdk));
+        }
+    }
 
-        int firstUnassigned = VERSION_CODES.TIRAMISU + 1;
-        for (int sdk = firstUnassigned; sdk <= 1_000_000; sdk++) {
+    /** Verifies that getExtensionVersion returns zero value for non-existing extensions */
+    @Test
+    public void testZeroValues() throws Exception {
+        Set<Integer> assignedCodes = Set.of(R, S, TIRAMISU, AD_SERVICES);
+        for (int sdk = VERSION_CODES.R; sdk <= 1_000_000; sdk++) {
+            if (assignedCodes.contains(sdk)) {
+                continue;
+            }
             // No extension SDKs yet.
             assertEquals(0, SdkExtensions.getExtensionVersion(sdk));
         }
     }
 
-    /** Verifies that the public sysprops are set as expected */
-    public void testSystemProperties() throws Exception {
-        assertCorrectVersion(true, SystemProperties.get("build.version.extensions.r"));
-        assertCorrectVersion(
-            SdkLevel.isAtLeastS(), SystemProperties.get("build.version.extensions.s"));
-        assertCorrectVersion(
-            SdkLevel.isAtLeastT(), SystemProperties.get("build.version.extensions.t"));
-    }
-
-    public void testExtensionVersions() throws Exception {
-        Map<Integer, Integer> versions = SdkExtensions.getAllExtensionVersions();
-        int expectedSize = 1;
-        assertCorrectVersion(versions.get(VERSION_CODES.R));
-
+    @Test
+    public void testGetAllExtensionVersionsKeys() throws Exception {
+        Set<Integer> expectedKeys = new HashSet<>();
+        expectedKeys.add(VERSION_CODES.R);
         if (SdkLevel.isAtLeastS()) {
-            assertCorrectVersion(versions.get(VERSION_CODES.S));
-            expectedSize++;
+            expectedKeys.add(VERSION_CODES.S);
         }
         if (SdkLevel.isAtLeastT()) {
-            assertCorrectVersion(versions.get(VERSION_CODES.TIRAMISU));
-            expectedSize++;
+            expectedKeys.add(VERSION_CODES.TIRAMISU);
+            if (SdkExtensions.getExtensionVersion(VERSION_CODES.S) >= 4) {
+                // The AdServices extension was added in v4 (2022-11). Note that we check the
+                // S extension version in the conditional above because the 22-11-Go train did
+                // not include AdServices, so the T extension version remains 3 (S is 4 though).
+                expectedKeys.add(AD_SERVICES);
+            }
         }
-        assertEquals(expectedSize, versions.size());
+        Set<Integer> actualKeys = SdkExtensions.getAllExtensionVersions().keySet();
+        assertThat(actualKeys).containsExactlyElementsIn(expectedKeys);
+    }
+
+    @Test
+    public void testExtensionR() {
+        assertCorrectVersion(true, R, "r");
+    }
+
+    @Test
+    public void testExtensionS() {
+        assertCorrectVersion(SdkLevel.isAtLeastS(), S, "s");
+    }
+
+    @Test
+    public void testExtensionT() {
+        assertCorrectVersion(SdkLevel.isAtLeastT(), TIRAMISU, "t");
+    }
+
+    @Test
+    public void testExtensionAdServices() {
+        // The AdServices extension was added in v4.
+        assumeTrue(SdkExtensions.getExtensionVersion(TIRAMISU) >= 4);
+
+        assertCorrectVersion(SdkLevel.isAtLeastT(), AD_SERVICES, "ad_services");
     }
 
 }
diff --git a/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java b/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java
index 707e6d3..8abab8c 100644
--- a/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java
+++ b/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java
@@ -16,8 +16,6 @@
 
 package com.android.sdkext.extensions;
 
-import static com.android.os.ext.testing.CurrentVersion.V;
-
 import static com.google.common.truth.Truth.assertWithMessage;
 
 import static org.junit.Assert.assertEquals;
@@ -25,9 +23,11 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeTrue;
+import org.junit.Ignore;
 
 import android.cts.install.lib.host.InstallUtilsHost;
 
+import com.android.os.ext.testing.CurrentVersion;
 import com.android.tests.rollback.host.AbandonSessionsRule;
 import com.android.tradefed.device.ITestDevice.ApexInfo;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
@@ -67,6 +67,7 @@
     private final InstallUtilsHost mInstallUtils = new InstallUtilsHost(this);
 
     private Boolean mIsAtLeastS = null;
+    private Boolean mIsAtLeastT = null;
 
     @Rule
     public AbandonSessionsRule mHostTestRule = new AbandonSessionsRule(this);
@@ -91,11 +92,13 @@
     }
 
     @Test
+    @Ignore("b/274764792")
     public void testDefault() throws Exception {
         assertVersionDefault();
     }
 
     @Test
+    @Ignore("b/274764792")
     public void upgradeOneApexWithBump()  throws Exception {
         assertVersionDefault();
         mInstallUtils.installApexes(SDKEXTENSIONS_FILENAME);
@@ -105,10 +108,11 @@
         // Version 45 requires sdkext + media, which isn't fulfilled
         assertRVersionEquals(12);
         assertSVersionEquals(12);
-        assertTrue(broadcastForBoolean("MAKE_CALLS_45", null)); // 45 APIs are available on 12 too.
+        assertTestMethodsPresent(); // 45 APIs are available on 12 too.
     }
 
     @Test
+    @Ignore("b/274764792")
     public void upgradeOneApex() throws Exception {
         // Version 45 requires updated sdkext and media, so updating just media changes nothing.
         assertVersionDefault();
@@ -118,6 +122,7 @@
     }
 
     @Test
+    @Ignore("b/274764792")
     public void upgradeTwoApexes() throws Exception {
         // Updating sdkext and media bumps the version to 45.
         assertVersionDefault();
@@ -175,15 +180,33 @@
     }
 
     private void assertVersionDefault() throws Exception {
-        assertRVersionEquals(V);
-        assertSVersionEquals(V);
-        assertTrue(broadcastForBoolean("MAKE_CALLS_DEFAULT", null));
+        int expected = isAtLeastT() ? CurrentVersion.T_BASE_VERSION
+            : isAtLeastS() ? CurrentVersion.S_BASE_VERSION
+            : CurrentVersion.R_BASE_VERSION;
+        assertRVersionEquals(expected);
+        assertSVersionEquals(expected);
+        assertTestMethodsNotPresent();
     }
 
     private void assertVersion45() throws Exception {
         assertRVersionEquals(45);
         assertSVersionEquals(45);
-        assertTrue(broadcastForBoolean("MAKE_CALLS_45", null));
+        assertTestMethodsPresent();
+    }
+
+    private void assertTestMethodsNotPresent() throws Exception {
+        assertTrue(broadcastForBoolean("MAKE_CALLS_DEFAULT", null));
+    }
+
+    private void assertTestMethodsPresent() throws Exception {
+        if (isAtLeastS()) {
+            assertTrue(broadcastForBoolean("MAKE_CALLS_45", null));
+        } else {
+            // The APIs in the test apex are not currently getting installed correctly
+            // on Android R devices because they rely on the dynamic classpath feature.
+            // TODO(b/234361913): fix this
+            assertTestMethodsNotPresent();
+        }
     }
 
     private void assertRVersionEquals(int version) throws Exception {
@@ -231,6 +254,13 @@
         return mIsAtLeastS;
     }
 
+    private boolean isAtLeastT() throws Exception {
+        if (mIsAtLeastT == null) {
+            mIsAtLeastT = broadcastForBoolean("IS_AT_LEAST", "t");
+        }
+        return mIsAtLeastT;
+    }
+
     private boolean uninstallApexes(String... filenames) throws Exception {
         boolean reboot = false;
         for (String filename : filenames) {
diff --git a/javatests/com/android/sdkext/extensions/apps/Receiver.java b/javatests/com/android/sdkext/extensions/apps/Receiver.java
index 4c81363..5f47b9c 100644
--- a/javatests/com/android/sdkext/extensions/apps/Receiver.java
+++ b/javatests/com/android/sdkext/extensions/apps/Receiver.java
@@ -41,6 +41,7 @@
         switch (letter) {
             case 'r': return Build.VERSION_CODES.R;
             case 's': return Build.VERSION_CODES.S;
+            case 't': return Build.VERSION_CODES.TIRAMISU;
         }
         throw new IllegalArgumentException(String.valueOf(letter));
     }
@@ -49,6 +50,7 @@
         switch (letter) {
             case 'r': return true; // our min sdk version is 30
             case 's': return SdkLevel.isAtLeastS();
+            case 't': return SdkLevel.isAtLeastT();
         }
         return false;
     }