Decouple sdkextension tests from their baseclass am: cd8df6cd38

Original change: https://android-review.googlesource.com/c/platform/packages/modules/SdkExtensions/+/1505636

Change-Id: Iad53ba8d0d58b87c423e03b4974f7bcb71a84749
diff --git a/tests/Android.bp b/tests/Android.bp
index e156d6b..e1238fa 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -16,7 +16,10 @@
     name: "sdkextensions_e2e_tests",
     srcs:  ["test-src/**/*.java"],
     libs: ["tradefed"],
-    static_libs: ["apex_e2e_base_test"],
+    static_libs: [
+        "cts-install-lib-host",
+        "frameworks-base-hostutils",
+    ],
     data: [
         ":sdkextensions_e2e_test_app",
         ":test_com.android.media",
diff --git a/tests/sdkextensions-e2e-tests.xml b/tests/sdkextensions-e2e-tests.xml
index 7787773..50be2c8 100644
--- a/tests/sdkextensions-e2e-tests.xml
+++ b/tests/sdkextensions-e2e-tests.xml
@@ -20,6 +20,5 @@
     <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer"/>
     <test class="com.android.tradefed.testtype.HostTest" >
         <option name="jar" value="sdkextensions_e2e_tests.jar" />
-        <option name="set-option" value="apex_file_name:test_com.android.sdkext.apex" />
     </test>
 </configuration>
diff --git a/tests/test-src/com/android/tests/apex/sdkextensions/SdkExtensionsHostTest.java b/tests/test-src/com/android/tests/apex/sdkextensions/SdkExtensionsHostTest.java
index 3ee8913..b0922a2 100644
--- a/tests/test-src/com/android/tests/apex/sdkextensions/SdkExtensionsHostTest.java
+++ b/tests/test-src/com/android/tests/apex/sdkextensions/SdkExtensionsHostTest.java
@@ -16,35 +16,64 @@
 
 package com.android.tests.apex.sdkextensions;
 
+import static com.google.common.truth.Truth.assertWithMessage;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
 
-import com.android.tests.apex.ApexE2EBaseHostTest;
+import android.cts.install.lib.host.InstallUtilsHost;
+
+import com.android.tests.rollback.host.AbandonSessionsRule;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.util.CommandResult;
 
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import java.io.File;
-import java.util.List;
+import java.time.Duration;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 @RunWith(DeviceJUnit4ClassRunner.class)
-public class SdkExtensionsHostTest extends ApexE2EBaseHostTest {
+public class SdkExtensionsHostTest extends BaseHostJUnit4Test {
 
     private static final String APP_FILENAME = "sdkextensions_e2e_test_app.apk";
     private static final String APP_PACKAGE = "com.android.tests.apex.sdkextensions";
     private static final String MEDIA_FILENAME = "test_com.android.media.apex";
     private static final String SDKEXTENSIONS_FILENAME = "test_com.android.sdkext.apex";
 
+    private static final Duration BOOT_COMPLETE_TIMEOUT = Duration.ofMinutes(2);
+
+    private final InstallUtilsHost mInstallUtils = new InstallUtilsHost(this);
+
+    @Rule
+    public AbandonSessionsRule mHostTestRule = new AbandonSessionsRule(this);
+
+    @Before
+    public void setUp() throws Exception {
+        assumeTrue("Updating APEX is not supported", mInstallUtils.isApexUpdateSupported());
+        uninstallApex(MEDIA_FILENAME);
+        uninstallApex(SDKEXTENSIONS_FILENAME);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        uninstallApex(MEDIA_FILENAME);
+        uninstallApex(SDKEXTENSIONS_FILENAME);
+    }
+
     @Before
     public void installTestApp() throws Exception {
-        File testAppFile = mUtils.getTestFile(APP_FILENAME);
+        File testAppFile = mInstallUtils.getTestFile(APP_FILENAME);
         String installResult = getDevice().installPackage(testAppFile, true);
         assertNull(installResult);
     }
@@ -55,23 +84,28 @@
         assertNull(uninstallResult);
     }
 
-    @Override
-    protected List<String> getAllApexFilenames() {
-        return List.of(SDKEXTENSIONS_FILENAME, MEDIA_FILENAME);
-    }
-
     @Test
     public void testDefault() throws Exception {
         assertVersion0();
     }
 
     @Test
+    public void upgradeOneApexWithBump()  throws Exception {
+        // On the system image, sdkextensions is the only apex with sdkinfo, and it's version 0.
+        // Verify that installing a new version of it with sdk version 45 bumps the version.
+        assertVersion0();
+        installApex(SDKEXTENSIONS_FILENAME);
+        reboot();
+        assertVersion45();
+    }
+
+    @Test
     public void upgradeOneApex() throws Exception {
         // On the system image, sdkextensions is the only apex with sdkinfo, and it's version 0.
         // This test verifies that installing media with sdk version 45 doesn't bump the version.
         assertVersion0();
         installApex(MEDIA_FILENAME);
-        reboot(false);
+        reboot();
         assertVersion0();
     }
 
@@ -81,14 +115,8 @@
         // This test verifies that installing media with sdk version 45 *and* a new sdkext does bump
         // the version.
         assertVersion0();
-        installApexes(MEDIA_FILENAME, SDKEXTENSIONS_FILENAME);
-        reboot(false);
-        assertVersion45();
-    }
-
-    @Override
-    public void additionalCheck() throws Exception {
-        // This method is run after the default test in the base class, which just installs sdkext.
+        mInstallUtils.installApexes(MEDIA_FILENAME, SDKEXTENSIONS_FILENAME);
+        reboot();
         assertVersion45();
     }
 
@@ -131,4 +159,26 @@
         cmd += " -n com.android.tests.apex.sdkextensions/.Receiver";
         return cmd;
     }
+
+    private void installApex(String filename) throws Exception {
+        String res = mInstallUtils.installStagedPackage(mInstallUtils.getTestFile(filename));
+        assertWithMessage("Failed to install %s. Reason: %s", filename, res).that(res).isNull();
+    }
+
+    private void uninstallApex(String filename) throws Exception {
+        ITestDevice.ApexInfo apex = mInstallUtils.getApexInfo(mInstallUtils.getTestFile(filename));
+        String res = getDevice().uninstallPackage(apex.name);
+        if (res != null) {
+            CLog.i("Uninstall of %s failed: %s, likely already on factory version", apex.name, res);
+        } else {
+            // Uninstall succeeded. Need to reboot for the uninstall to take affect.
+            reboot();
+        }
+    }
+
+    private void reboot() throws Exception {
+        getDevice().reboot();
+        boolean success = getDevice().waitForBootComplete(BOOT_COMPLETE_TIMEOUT.toMillis());
+        assertWithMessage("Device didn't boot in %s", BOOT_COMPLETE_TIMEOUT).that(success).isTrue();
+    }
 }