1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.adservices.tests.ui.gaux.debugchannel; 17 18 import static com.android.adservices.service.FlagsConstants.KEY_ENABLE_AD_SERVICES_SYSTEM_API; 19 import static com.android.adservices.service.FlagsConstants.KEY_IS_EEA_DEVICE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.adservices.common.AdServicesCommonManager; 24 import android.adservices.common.AdServicesStates; 25 import android.os.OutcomeReceiver; 26 import android.platform.test.rule.ScreenRecordRule; 27 28 import androidx.test.platform.app.InstrumentationRegistry; 29 import androidx.test.uiautomator.UiDevice; 30 31 import com.android.adservices.shared.testing.annotations.SetFlagEnabled; 32 import com.android.adservices.tests.ui.libs.AdservicesWorkflows; 33 import com.android.adservices.tests.ui.libs.UiConstants; 34 import com.android.adservices.tests.ui.libs.UiUtils; 35 36 import org.junit.After; 37 import org.junit.Assert; 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 42 import java.util.concurrent.Executors; 43 44 /** Test for verifying user consent notification trigger behaviors. */ 45 @ScreenRecordRule.ScreenRecord 46 @SetFlagEnabled(KEY_ENABLE_AD_SERVICES_SYSTEM_API) 47 @SetFlagEnabled(KEY_IS_EEA_DEVICE) 48 public final class ExtGaUxDebugChannelEuTest extends AdExtServicesGaUxDebugChannelTestCase { 49 50 private AdServicesCommonManager mCommonManager; 51 52 private UiDevice mDevice; 53 54 private String mTestName; 55 56 private OutcomeReceiver<Boolean, Exception> mCallback; 57 58 @Rule public final ScreenRecordRule sScreenRecordRule = new ScreenRecordRule(); 59 60 @Before setUp()61 public void setUp() throws Exception { 62 mTestName = getTestName(); 63 64 UiUtils.resetAdServicesConsentData(sContext, flags); 65 66 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 67 68 UiUtils.enableNotificationPermission(); 69 70 mCommonManager = AdServicesCommonManager.get(sContext); 71 72 // consent debug mode is turned on for this test class as we only care about the 73 // first trigger (API call). 74 UiUtils.enableConsentDebugMode(flags); 75 UiUtils.disableNotificationFlowV2(flags); 76 UiUtils.disableOtaStrings(flags); 77 78 mCallback = 79 new OutcomeReceiver<>() { 80 @Override 81 public void onResult(Boolean result) { 82 assertThat(result).isTrue(); 83 } 84 85 @Override 86 public void onError(Exception exception) { 87 Assert.fail(); 88 } 89 }; 90 91 mDevice.pressHome(); 92 } 93 94 @After tearDown()95 public void tearDown() throws Exception { 96 UiUtils.takeScreenshot(mDevice, getClass().getSimpleName() + "_" + mTestName + "_"); 97 98 mDevice.pressHome(); 99 } 100 101 /** Verify that entry point disabled can not trigger consent notification. */ 102 @Test testEntryPointDisabled()103 public void testEntryPointDisabled() throws Exception { 104 mCommonManager.enableAdServices( 105 new AdServicesStates.Builder() 106 .setAdIdEnabled(true) 107 .setAdultAccount(true) 108 .setPrivacySandboxUiEnabled(false) 109 .build(), 110 Executors.newCachedThreadPool(), 111 mCallback); 112 113 AdservicesWorkflows.verifyNotification( 114 sContext, 115 mDevice, /* isDisplayed */ 116 false, /* isEuTest */ 117 false, /* isGa */ 118 UiConstants.UX.GA_UX, 119 true); 120 } 121 122 /** Verify that when request sent from entry point, we won't trigger notification. */ 123 @Test testFromEntryPointRequest()124 public void testFromEntryPointRequest() throws Exception { 125 mCommonManager.enableAdServices( 126 new AdServicesStates.Builder() 127 .setAdIdEnabled(false) 128 .setAdultAccount(true) 129 .setPrivacySandboxUiEnabled(true) 130 .setPrivacySandboxUiRequest(true) 131 .build(), 132 Executors.newCachedThreadPool(), 133 mCallback); 134 135 AdservicesWorkflows.verifyNotification( 136 sContext, 137 mDevice, /* isDisplayed */ 138 false, /* isEuTest */ 139 true, 140 UiConstants.UX.GA_UX, 141 true); 142 } 143 144 /** Verify that non-adult account can not trigger consent notification. */ 145 @Test testNonAdultAccount()146 public void testNonAdultAccount() throws Exception { 147 mCommonManager.enableAdServices( 148 new AdServicesStates.Builder() 149 .setAdIdEnabled(true) 150 .setAdultAccount(false) 151 .setPrivacySandboxUiEnabled(true) 152 .build(), 153 Executors.newCachedThreadPool(), 154 mCallback); 155 156 AdservicesWorkflows.verifyNotification( 157 sContext, 158 mDevice, /* isDisplayed */ 159 false, /* isEuTest */ 160 false, 161 UiConstants.UX.GA_UX, 162 true); 163 } 164 165 /** 166 * Verify that for GA, EU devices with non zeroed-out AdId, the GA EU notification is displayed. 167 */ 168 @Test testGaEuAdIdEnabled()169 public void testGaEuAdIdEnabled() throws Exception { 170 mCommonManager.enableAdServices( 171 new AdServicesStates.Builder() 172 .setAdIdEnabled(true) 173 .setAdultAccount(true) 174 .setPrivacySandboxUiEnabled(true) 175 .build(), 176 Executors.newCachedThreadPool(), 177 mCallback); 178 179 AdservicesWorkflows.verifyNotification( 180 sContext, 181 mDevice, /* isDisplayed */ 182 true, /* isEuTest */ 183 true, 184 UiConstants.UX.GA_UX, 185 true); 186 } 187 188 /** Verify that for GA, EU devices with zeroed-out AdId, the EU notification is displayed. */ 189 @Test testGaEuAdIdDisabled()190 public void testGaEuAdIdDisabled() throws Exception { 191 mCommonManager.enableAdServices( 192 new AdServicesStates.Builder() 193 .setAdIdEnabled(false) 194 .setAdultAccount(true) 195 .setPrivacySandboxUiEnabled(true) 196 .build(), 197 Executors.newCachedThreadPool(), 198 mCallback); 199 200 AdservicesWorkflows.verifyNotification( 201 sContext, 202 mDevice, /* isDisplayed */ 203 true, /* isEuTest */ 204 true, 205 UiConstants.UX.GA_UX, 206 true); 207 } 208 } 209