Add new set of UI CTS root tests for the enableAdServices API.

Test: atest
Bug: b/
Change-Id: I733b2a52557f4a0b75690a45c92c4e5e7f3eb200
diff --git a/adservices/tests/cts/ui/gaux/legacy/AndroidManifest.xml b/adservices/tests/cts/ui/gaux/legacy/AndroidManifest.xml
index f7ccd16..d25f3bb 100644
--- a/adservices/tests/cts/ui/gaux/legacy/AndroidManifest.xml
+++ b/adservices/tests/cts/ui/gaux/legacy/AndroidManifest.xml
@@ -24,7 +24,6 @@
     <!-- instrument into the system server for permission -->
     <instrumentation
         android:name="androidx.test.runner.AndroidJUnitRunner"
-        android:targetPackage="android"
-        android:label="CTS tests of AdServices UI">
+        android:targetPackage="android">
     </instrumentation>
 </manifest>
diff --git a/adservices/tests/cts/ui/gaux/notificationtrigger/Android.bp b/adservices/tests/cts/ui/gaux/notificationtrigger/Android.bp
new file mode 100644
index 0000000..174f4dd
--- /dev/null
+++ b/adservices/tests/cts/ui/gaux/notificationtrigger/Android.bp
@@ -0,0 +1,43 @@
+// Copyright (C) 2023 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"],
+}
+
+android_test {
+    name: "AdServicesGaUxNotificationTriggerCtsRootTest",
+    srcs: [
+        "src/**/*.java",
+    ],
+    static_libs: [
+        "adservices-assets",
+        "adservices-service-core",
+        "compatibility-device-util-axt",
+        "truth-prebuilt",
+        "adservices-test-utility",
+        "adservices-ui-cts-root-test-lib",
+    ],
+    libs: [
+        "android.test.base",
+        "framework-adservices.impl",
+    ],
+    test_suites: [
+        "cts_root",
+        "general-tests",
+        "mts-adservices"
+    ],
+    sdk_version: "module_current",
+    min_sdk_version: "Tiramisu",
+}
diff --git a/adservices/tests/cts/ui/gaux/notificationtrigger/AndroidManifest.xml b/adservices/tests/cts/ui/gaux/notificationtrigger/AndroidManifest.xml
new file mode 100644
index 0000000..7a3dc33
--- /dev/null
+++ b/adservices/tests/cts/ui/gaux/notificationtrigger/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2023 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.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.android.adservices.tests.ui.gaux.notificationtrigger">
+
+    <application>
+    </application>
+
+    <!-- instrument into the system server for permission -->
+    <instrumentation
+        android:name="androidx.test.runner.AndroidJUnitRunner"
+        android:targetPackage="android">
+    </instrumentation>
+</manifest>
diff --git a/adservices/tests/cts/ui/gaux/notificationtrigger/AndroidTest.xml b/adservices/tests/cts/ui/gaux/notificationtrigger/AndroidTest.xml
new file mode 100644
index 0000000..8137904
--- /dev/null
+++ b/adservices/tests/cts/ui/gaux/notificationtrigger/AndroidTest.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2023 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.
+  -->
+<configuration description="TF Config for AdServices Ui Cts test">
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer"/>
+
+    <target_preparer class="com.android.tradefed.targetprep.DeviceCleaner">
+        <option name="cleanup-action" value="REBOOT"/>
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="run-command" value="device_config set_sync_disabled_for_tests persistent"/>
+        <option name="teardown-command" value="device_config set_sync_disabled_for_tests none"/>
+
+        <option name="run-command" value="device_config put adservices global_kill_switch false"/>
+        <option
+            name="run-command"
+            value="device_config put adservices adservice_enable_status true"/>
+        <option
+            name="run-command"
+            value="device_config put adservices adservice_enabled true"/>
+        <option
+            name="run-command"
+            value="device_config put adservices enable_ad_services_system_api true"/>
+        <option
+            name="run-command"
+            value="device_config put adservices consent_notification_debug_mode false"/>
+        <!-- override scheduling params so the test is unaffected by time of the day -->
+        <option
+            name="run-command"
+            value="device_config put adservices consent_notification_interval_begin_ms 0"/>
+        <!-- end of day (24 hours) -->
+        <option
+            name="run-command"
+            value="device_config put adservices consent_notification_interval_end_ms 86400000"/>
+        <option
+            name="run-command"
+            value="device_config put adservices consent_notification_minimal_delay_before_interval_ends 0"/>
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+        <option name="cleanup-apks" value="true"/>
+        <option name="test-file-name" value="AdServicesGaUxNotificationTriggerCtsRootTest.apk"/>
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest">
+        <option name="package" value="com.android.adservices.tests.ui.gaux.notificationtrigger"/>
+        <option name="restart" value="false"/>
+    </test>
+</configuration>
diff --git a/adservices/tests/cts/ui/gaux/notificationtrigger/src/com.android.adservices.tests.ui.gaux.notificationtrigger/GaUxNotificationTriggerTest.java b/adservices/tests/cts/ui/gaux/notificationtrigger/src/com.android.adservices.tests.ui.gaux.notificationtrigger/GaUxNotificationTriggerTest.java
new file mode 100644
index 0000000..0dd87b5
--- /dev/null
+++ b/adservices/tests/cts/ui/gaux/notificationtrigger/src/com.android.adservices.tests.ui.gaux.notificationtrigger/GaUxNotificationTriggerTest.java
@@ -0,0 +1,235 @@
+/*
+ * Copyright (C) 2023 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.adservices.tests.ui.gaux;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.adservices.common.AdServicesCommonManager;
+import android.adservices.common.AdServicesStates;
+import android.content.Context;
+import android.os.OutcomeReceiver;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+import androidx.test.uiautomator.UiDevice;
+
+import com.android.adservices.common.AdservicesTestHelper;
+import com.android.adservices.tests.ui.libs.UiUtils;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.concurrent.Executors;
+
+/** Test for verifying user consent notification trigger behaviors. */
+@RunWith(AndroidJUnit4.class)
+public class GaUxNotificationTriggerTest {
+
+    private AdServicesCommonManager mCommonManager;
+
+    private UiDevice mDevice;
+
+    private OutcomeReceiver<Boolean, Exception> mCallback;
+
+    private static final Context sContext =
+            InstrumentationRegistry.getInstrumentation().getContext();
+
+    @Before
+    public void setUp() throws Exception {
+        // Skip the test if it runs on unsupported platforms.
+        Assume.assumeTrue(AdservicesTestHelper.isDeviceSupported());
+
+        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+
+        // TO-DO (b/271567864): grant the permission in our apk code and remove this in the future.
+        // Grant runtime permission to the AOSP adservices app.
+        UiUtils.enableNotificationPermission();
+
+        mCommonManager = AdServicesCommonManager.get(sContext);
+
+        // consent debug mode is turned on for this test class as we only care about the
+        // first trigger (API call).
+        UiUtils.enableConsentDebugMode();
+
+        mCallback =
+                new OutcomeReceiver<Boolean, Exception>() {
+                    @Override
+                    public void onResult(Boolean result) {
+                        assertThat(result).isTrue();
+                    }
+
+                    @Override
+                    public void onError(Exception exception) {
+                        Assert.fail();
+                    }
+                };
+
+        mDevice.pressHome();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (!AdservicesTestHelper.isDeviceSupported()) return;
+
+        mDevice.pressHome();
+        AdservicesTestHelper.killAdservicesProcess(sContext);
+    }
+
+    /** Verify that the API returns false when API is disabled. */
+    @Test
+    public void testApiDisabled() throws Exception {
+        UiUtils.turnOffEnableAdsServicesAPI();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(true)
+                        .setAdultAccount(true)
+                        .setPrivacySandboxUiEnabled(true)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ false, /* isEuTest */ false, /* isGa */ true);
+
+        UiUtils.turnOnEnableAdsServicesAPI();
+    }
+
+    /** Verify that entry point disabled can not trigger consent notification. */
+    @Test
+    public void testEntryPointDisabled() throws Exception {
+        UiUtils.setAsRowDevice();
+        UiUtils.enableGa();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(true)
+                        .setAdultAccount(true)
+                        .setPrivacySandboxUiEnabled(false)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ false, /* isEuTest */ false, /* isGa */ true);
+    }
+
+    /** Verify that non-adult account can not trigger consent notification. */
+    @Test
+    public void testNonAdultAccount() throws Exception {
+        UiUtils.setAsRowDevice();
+        UiUtils.enableGa();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(true)
+                        .setAdultAccount(false)
+                        .setPrivacySandboxUiEnabled(true)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ false, /* isEuTest */ false, /* isGa */ true);
+    }
+
+    /**
+     * Verify that for GA, ROW devices with non zeroed-out AdId, the GA ROW notification is
+     * displayed.
+     */
+    @Test
+    public void testGaRowAdIdEnabled() throws Exception {
+        UiUtils.setAsRowDevice();
+        UiUtils.enableGa();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(true)
+                        .setAdultAccount(true)
+                        .setPrivacySandboxUiEnabled(true)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ true, /* isEuTest */ false, /* isGa */ true);
+    }
+
+    /**
+     * Verify that for GA, ROW devices with zeroed-out AdId, the GA EU notification is displayed.
+     */
+    @Test
+    public void testGaRowAdIdDisabled() throws Exception {
+        UiUtils.setAsRowDevice();
+        UiUtils.enableGa();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(false)
+                        .setAdultAccount(true)
+                        .setPrivacySandboxUiEnabled(true)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ true, /* isEuTest */ true, /* isGa */ true);
+    }
+
+    /**
+     * Verify that for GA, EU devices with non zeroed-out AdId, the GA EU notification is displayed.
+     */
+    @Test
+    public void testGaEuAdIdEnabled() throws Exception {
+        UiUtils.setAsEuDevice();
+        UiUtils.enableGa();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(true)
+                        .setAdultAccount(true)
+                        .setPrivacySandboxUiEnabled(true)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ true, /* isEuTest */ true, /* isGa */ true);
+    }
+
+    /** Verify that for GA, EU devices with zeroed-out AdId, the EU notification is displayed. */
+    @Test
+    public void testGaEuAdIdDisabled() throws Exception {
+        UiUtils.setAsEuDevice();
+        UiUtils.enableGa();
+
+        mCommonManager.enableAdServices(
+                new AdServicesStates.Builder()
+                        .setAdIdEnabled(false)
+                        .setAdultAccount(true)
+                        .setPrivacySandboxUiEnabled(true)
+                        .build(),
+                Executors.newCachedThreadPool(),
+                mCallback);
+
+        UiUtils.verifyNotification(
+                sContext, mDevice, /* isDisplayed */ true, /* isEuTest */ true, /* isGa */ true);
+    }
+}
diff --git a/adservices/tests/cts/ui/libs/UiUtils.java b/adservices/tests/cts/ui/libs/UiUtils.java
index 2e74495..acd04d9 100644
--- a/adservices/tests/cts/ui/libs/UiUtils.java
+++ b/adservices/tests/cts/ui/libs/UiUtils.java
@@ -37,6 +37,16 @@
 
 public class UiUtils {
 
+    public static void turnOffEnableAdsServicesAPI() {
+        ShellUtils.runShellCommand(
+                "device_config put adservices enable_ad_services_system_api false");
+    }
+
+    public static void turnOnEnableAdsServicesAPI() {
+        ShellUtils.runShellCommand(
+                "device_config put adservices enable_ad_services_system_api true");
+    }
+
     public static void setAsNonWorkingHours() {
         // set the notification interval start time to 9:00 AM
         ShellUtils.runShellCommand(