Resolve Access issue and Package Name issue in AOSP

Bug: 236865426
Test: atest in virtual and physical devices.
Change-Id: I5965b62b13f1bbd576f4410dd7330e9d1f9af92a
diff --git a/adservices/tests/cts/hosttests/src/com/android/adservices/cts/TopicsApiLoggingHostTest.java b/adservices/tests/cts/hosttests/src/com/android/adservices/cts/TopicsApiLoggingHostTest.java
index 336ced9..92e55da 100644
--- a/adservices/tests/cts/hosttests/src/com/android/adservices/cts/TopicsApiLoggingHostTest.java
+++ b/adservices/tests/cts/hosttests/src/com/android/adservices/cts/TopicsApiLoggingHostTest.java
@@ -29,6 +29,7 @@
 import com.android.os.AtomsProto.AdServicesApiCalled;
 import com.android.os.AtomsProto.Atom;
 import com.android.os.StatsLog.EventMetricData;
+import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics;
@@ -43,7 +44,7 @@
 import java.util.List;
 
 /**
- * Test to check that Topics API log to StatsD
+ * Test to check that Topics API logging to StatsD
  *
  * <p>When this test builds, it also builds {@link com.android.adservices.cts.TopicsApiLogActivity}
  * into an APK which it then installed at runtime and started. The activity simply called getTopics
@@ -58,6 +59,7 @@
     private static final String PACKAGE = "com.android.adservices.cts";
     private static final String CLASS = "TopicsApiLogActivity";
     private static final String TARGET_PACKAGE = "com.google.android.adservices.api";
+    private static final String TARGET_PACKAGE_AOSP = "com.android.adservices.api";
     private static final String SDK_NAME = "AdservicesCtsSdk";
 
     @Rule public TestMetrics mMetrics = new TestMetrics();
@@ -78,6 +80,11 @@
     public void setUp() throws Exception {
         ConfigUtils.removeConfig(getDevice());
         ReportUtils.clearReports(getDevice());
+
+        // We need to turn the Consent Manager into debug mode
+        overrideConsentManagerDebugMode();
+        disableMddBackgroundTasks(true);
+        overrideDisableTopicsEnrollmentCheck("1");
     }
 
     @After
@@ -86,30 +93,26 @@
         ReportUtils.clearReports(getDevice());
     }
 
+    // TODO(b/245400146): Get package Name for Topics API instead of running the test twice.
     @Test
     public void testGetTopicsLog() throws Exception {
         ITestDevice device = getDevice();
         assertNotNull("Device not set", device);
 
-        // Upload the config.
-        final StatsdConfig.Builder config = ConfigUtils.createConfigBuilder(TARGET_PACKAGE);
-
-        ConfigUtils.addEventMetric(config, Atom.AD_SERVICES_API_CALLED_FIELD_NUMBER);
-        ConfigUtils.uploadConfig(device, config);
-
-        // Run the get topic activity that has logging event on the devices
-        // 4th argument is actionKey and 5th is actionValue, which is the extra data that passed
-        // to the activity via an Intent, we don't need provide extra values, thus passing
-        // in null here
-        DeviceUtils.runActivity(
-                device, PACKAGE, CLASS, /* actionKey */ null, /* actionValue */ null);
-
-        // Wait for activity to finish and logging event to happen
-        Thread.sleep(AtomTestUtils.WAIT_TIME_SHORT);
+        callTopicsAPI(TARGET_PACKAGE, device);
 
         // Fetch a list of happened log events and their data
         List<EventMetricData> data = ReportUtils.getEventMetricDataList(device);
 
+        // Topics API Name is different in aosp and non-aosp devices. Attempt again with the other
+        // package Name if it fails at the first time;
+        if (data.isEmpty()) {
+            ConfigUtils.removeConfig(getDevice());
+            ReportUtils.clearReports(getDevice());
+            callTopicsAPI(TARGET_PACKAGE_AOSP, device);
+            data = ReportUtils.getEventMetricDataList(device);
+        }
+
         // We trigger only one event from activity, should only see one event in the list
         assertThat(data).hasSize(1);
 
@@ -122,4 +125,45 @@
         assertThat(adServicesApiCalled.getApiName())
                 .isEqualTo(AdServicesApiCalled.AdServicesApiName.GET_TOPICS);
     }
+
+    private void callTopicsAPI(String apiName, ITestDevice device) throws Exception {
+        // Upload the config.
+        final StatsdConfig.Builder config = ConfigUtils.createConfigBuilder(apiName);
+
+        ConfigUtils.addEventMetric(config, Atom.AD_SERVICES_API_CALLED_FIELD_NUMBER);
+        ConfigUtils.uploadConfig(device, config);
+
+        // Run the get topic activity that has logging event on the devices
+        // 4th argument is actionKey and 5th is actionValue, which is the extra data that passed
+        // to the activity via an Intent, we don't need to provide extra values, thus passing
+        // in null here
+        DeviceUtils.runActivity(
+                device, PACKAGE, CLASS, /* actionKey */ null, /* actionValue */ null);
+
+        // Wait for activity to finish and logging event to happen
+        Thread.sleep(AtomTestUtils.WAIT_TIME_SHORT);
+    }
+
+    // Override the Consent Manager behaviour - Consent Given
+    private void overrideConsentManagerDebugMode() throws DeviceNotAvailableException {
+        getDevice().executeShellCommand("setprop debug.adservices.consent_manager_debug_mode true");
+    }
+
+    // Switch on/off for MDD service. Default value is false, which means MDD is enabled.
+    private void disableMddBackgroundTasks(boolean isSwitchedOff)
+            throws DeviceNotAvailableException {
+        getDevice()
+                .executeShellCommand(
+                        "setprop debug.adservices.mdd_background_task_kill_switch "
+                                + isSwitchedOff);
+    }
+
+    // Override the flag to disable Topics enrollment check.
+    private void overrideDisableTopicsEnrollmentCheck(String val)
+            throws DeviceNotAvailableException {
+        // Setting it to 1 here disables the Topics' enrollment check.
+        getDevice()
+                .executeShellCommand(
+                        "setprop debug.adservices.disable_topics_enrollment_check " + val);
+    }
 }