Fixed CtsAdServicesTopicsAppUpdateTests to properly suppport multi-user.

Bug: 282019921
Test: atest --user-type secondary_user CtsAdServicesTopicsAppUpdateTests
Flag: TEST_ONLY

Change-Id: I5b5956f1b6c1362180ab663c3ec3c11ff1f5c201
diff --git a/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.ExtServices.xml b/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.ExtServices.xml
index 72a17c8..ce0becb 100644
--- a/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.ExtServices.xml
+++ b/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.ExtServices.xml
@@ -45,6 +45,7 @@
     </test>
 
     <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="test-user-token" value="%TEST_USER%"/>
         <!-- Disable global/topics kill_switch to ignore the effect of actual PH values. -->
         <option name="run-command" value="device_config put adservices global_kill_switch false"/>
         <option name="run-command" value="device_config put adservices topics_kill_switch false"/>
@@ -78,7 +79,7 @@
         <option name="teardown-command" value="rm -rf /data/local/tmp/cts"/>
         <!-- Uninstall the test app anyway in case failed test skips the uninstallation -->
         <option name="teardown-command"
-                value="pm uninstall --user 0 com.android.adservices.tests.cts.topics.testapp1"/>
+                value="pm uninstall --user %TEST_USER% com.android.adservices.tests.cts.topics.testapp1"/>
         <option name="teardown-command"
                 value="device_config put adservices adservice_system_service_enabled false"/>
         <option name="teardown-command" value="device_config set_sync_disabled_for_tests none"/>
@@ -86,11 +87,12 @@
 
     <!-- Create place to store tests apks that will be installed/uninstalled in the test. -->
     <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="test-user-token" value="%TEST_USER%"/>
         <option name="run-command" value="mkdir -p /data/local/tmp/cts/install"/>
         <option name="teardown-command" value="rm -rf /data/local/tmp/cts"/>
         <!-- Uninstall the test app anyway in case failed test skips the uninstallation -->
         <option name="teardown-command"
-                value="pm uninstall --user 0 com.android.adextservices.tests.cts.topics.testapp1"/>
+                value="pm uninstall --user %TEST_USER% com.android.adextservices.tests.cts.topics.testapp1"/>
     </target_preparer>
 
     <!-- Push compiled APK file of test apps to a local place for installation -->
diff --git a/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.xml b/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.xml
index bfce756..ac7716c 100644
--- a/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.xml
+++ b/adservices/tests/cts/endtoends/topics/appupdate/AndroidTest.xml
@@ -34,6 +34,7 @@
     </test>
 
     <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="test-user-token" value="%TEST_USER%"/>
         <!-- Disable global/topics kill_switch to ignore the effect of actual PH values. -->
         <option name="run-command" value="device_config put adservices global_kill_switch false" />
         <option name="run-command" value="device_config put adservices topics_kill_switch false" />
@@ -59,18 +60,19 @@
         <option name="teardown-command" value="rm -rf /data/local/tmp/cts"/>
         <!-- Uninstall the test app anyway in case failed test skips the uninstallation -->
         <option name="teardown-command"
-                value="pm uninstall --user 0 com.android.adservices.tests.cts.topics.testapp1" />
+                value="pm uninstall -user %TEST_USER% com.android.adservices.tests.cts.topics.testapp1" />
         <option name="teardown-command" value="device_config put adservices adservice_system_service_enabled false" />
         <option name="teardown-command" value="device_config set_sync_disabled_for_tests none" />
     </target_preparer>
 
     <!-- Create place to store tests apks that will be installed/uninstalled in the test. -->
     <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="test-user-token" value="%TEST_USER%"/>
         <option name="run-command" value="mkdir -p /data/local/tmp/cts/install" />
         <option name="teardown-command" value="rm -rf /data/local/tmp/cts"/>
         <!-- Uninstall the test app anyway in case failed test skips the uninstallation -->
         <option name="teardown-command"
-                value="pm uninstall --user 0 com.android.adservices.tests.cts.topics.testapp1" />
+                value="pm uninstall -user %TEST_USER% com.android.adservices.tests.cts.topics.testapp1" />
     </target_preparer>
 
     <!-- Push compiled APK file of test apps to a local place for installation -->
diff --git a/adservices/tests/cts/endtoends/topics/appupdate/src/com/android/adservices/tests/cts/topics/appupdate/AppUpdateTest.java b/adservices/tests/cts/endtoends/topics/appupdate/src/com/android/adservices/tests/cts/topics/appupdate/AppUpdateTest.java
index 9359c90..78e681b 100644
--- a/adservices/tests/cts/endtoends/topics/appupdate/src/com/android/adservices/tests/cts/topics/appupdate/AppUpdateTest.java
+++ b/adservices/tests/cts/endtoends/topics/appupdate/src/com/android/adservices/tests/cts/topics/appupdate/AppUpdateTest.java
@@ -24,6 +24,7 @@
 import static com.android.adservices.service.FlagsConstants.KEY_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC;
 import static com.android.adservices.tests.cts.topics.appupdate.CtsAdServicesTopicsAppUpdateTestCase.TEST_EPOCH_JOB_PERIOD_MS;
 import static com.android.adservices.tests.cts.topics.appupdate.CtsAdServicesTopicsAppUpdateTestCase.TEST_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC;
+import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -34,12 +35,12 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.util.Log;
 
 import androidx.test.filters.FlakyTest;
 
 import com.android.adservices.common.AdservicesTestHelper;
 import com.android.adservices.shared.testing.annotations.RequiresSdkLevelAtLeastT;
-import com.android.compatibility.common.util.ShellUtils;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -128,9 +129,13 @@
 
     private int mExpectedTopicResponseBroadCastIndex;
     private BroadcastReceiver mTopicsResponseReceiver;
+    private int mUserId;
 
     @Before
     public void setup() throws Exception {
+        mUserId = mContext.getUser().getIdentifier();
+        Log.v(mTag, "Running as user " + mUserId);
+
         // Kill AdServices process so that background jobs don't get skipped due to starting
         // with same params.
         AdservicesTestHelper.killAdservicesProcess(mAdServicesPackageName);
@@ -268,28 +273,27 @@
 
     // Install test sample app 1 and verify the installation.
     private void installTestSampleApp() {
-        String installMessage = ShellUtils.runShellCommand("pm install -r " + TEST_APK_PATH);
+        String installMessage =
+                runShellCommand("pm install --user %d -r %s", mUserId, TEST_APK_PATH);
         assertThat(installMessage).contains("Success");
     }
 
     // Note aosp_x86 requires --user 0 to uninstall though arm doesn't.
     private void uninstallTestSampleApp() {
-        ShellUtils.runShellCommand("pm uninstall --user 0 " + TEST_PKG_NAME);
+        runShellCommand("pm uninstall --user %d %s", mUserId, TEST_PKG_NAME);
     }
 
     /** Forces JobScheduler to run the Epoch Computation job */
     private void forceEpochComputationJob() {
-        ShellUtils.runShellCommand(
-                "cmd jobscheduler run -f" + " " + mAdServicesPackageName + " " + EPOCH_JOB_ID);
+        runShellCommand(
+                "cmd jobscheduler run --user %d -f %s %s",
+                mUserId, mAdServicesPackageName, EPOCH_JOB_ID);
     }
 
     // Forces JobScheduler to run the Maintenance job.
     private void forceMaintenanceJob() {
-        ShellUtils.runShellCommand(
-                "cmd jobscheduler run -f"
-                        + " "
-                        + mAdServicesPackageName
-                        + " "
-                        + MAINTENANCE_JOB_ID);
+        runShellCommand(
+                "cmd jobscheduler run --user %d -f %s %s",
+                mUserId, mAdServicesPackageName, MAINTENANCE_JOB_ID);
     }
 }