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.google.common.truth.Truth.assertThat; 19 20 import android.adservices.common.AdServicesCommonManager; 21 import android.adservices.common.AdServicesStates; 22 import android.content.Context; 23 import android.os.OutcomeReceiver; 24 import android.platform.test.rule.ScreenRecordRule; 25 26 import androidx.test.platform.app.InstrumentationRegistry; 27 import androidx.test.runner.AndroidJUnit4; 28 import androidx.test.uiautomator.UiDevice; 29 30 import com.android.adservices.common.AdservicesTestHelper; 31 import com.android.adservices.tests.ui.libs.AdservicesWorkflows; 32 import com.android.adservices.tests.ui.libs.UiConstants; 33 import com.android.adservices.tests.ui.libs.UiUtils; 34 35 import org.junit.After; 36 import org.junit.Assert; 37 import org.junit.Assume; 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 import java.util.concurrent.Executors; 44 45 /** Test for verifying user consent notification trigger behaviors. */ 46 @RunWith(AndroidJUnit4.class) 47 @ScreenRecordRule.ScreenRecord 48 public class ExtGaUxDebugChannelRowTest { 49 50 private AdServicesCommonManager mCommonManager; 51 52 private UiDevice mDevice; 53 54 private String mTestName; 55 56 private OutcomeReceiver<Boolean, Exception> mCallback; 57 58 private static final Context sContext = 59 InstrumentationRegistry.getInstrumentation().getContext(); 60 61 @Rule public final ScreenRecordRule sScreenRecordRule = new ScreenRecordRule(); 62 63 @Before setUp()64 public void setUp() throws Exception { 65 // Skip the test if it runs on unsupported platforms. 66 Assume.assumeTrue(AdservicesTestHelper.isDeviceSupported()); 67 68 UiUtils.resetAdServicesConsentData(sContext); 69 70 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 71 72 UiUtils.enableNotificationPermission(); 73 74 mCommonManager = AdServicesCommonManager.get(sContext); 75 76 // consent debug mode is turned on for this test class as we only care about the 77 // first trigger (API call). 78 UiUtils.enableConsentDebugMode(); 79 UiUtils.disableNotificationFlowV2(); 80 UiUtils.disableOtaStrings(); 81 82 mCallback = 83 new OutcomeReceiver<>() { 84 @Override 85 public void onResult(Boolean result) { 86 assertThat(result).isTrue(); 87 } 88 89 @Override 90 public void onError(Exception exception) { 91 Assert.fail(); 92 } 93 }; 94 95 mDevice.pressHome(); 96 } 97 98 @After tearDown()99 public void tearDown() throws Exception { 100 if (!AdservicesTestHelper.isDeviceSupported()) return; 101 102 UiUtils.takeScreenshot(mDevice, getClass().getSimpleName() + "_" + mTestName + "_"); 103 104 mDevice.pressHome(); 105 } 106 107 /** Verify that entry point disabled can not trigger consent notification. */ 108 @Test testEntryPointDisabled()109 public void testEntryPointDisabled() throws Exception { 110 mTestName = new Object() {}.getClass().getEnclosingMethod().getName(); 111 112 mCommonManager.enableAdServices( 113 new AdServicesStates.Builder() 114 .setAdIdEnabled(true) 115 .setAdultAccount(true) 116 .setPrivacySandboxUiEnabled(false) 117 .build(), 118 Executors.newCachedThreadPool(), 119 mCallback); 120 121 AdservicesWorkflows.verifyNotification( 122 sContext, 123 mDevice, /* isDisplayed */ 124 false, /* isEuTest */ 125 false, /* isGa */ 126 UiConstants.UX.GA_UX, 127 true); 128 } 129 130 /** Verify that when request sent from entry point, we won't trigger notification. */ 131 @Test testFromEntryPointRequest()132 public void testFromEntryPointRequest() throws Exception { 133 mTestName = new Object() {}.getClass().getEnclosingMethod().getName(); 134 135 mCommonManager.enableAdServices( 136 new AdServicesStates.Builder() 137 .setAdIdEnabled(false) 138 .setAdultAccount(true) 139 .setPrivacySandboxUiEnabled(true) 140 .setPrivacySandboxUiRequest(true) 141 .build(), 142 Executors.newCachedThreadPool(), 143 mCallback); 144 145 AdservicesWorkflows.verifyNotification( 146 sContext, 147 mDevice, /* isDisplayed */ 148 false, /* isEuTest */ 149 false, 150 UiConstants.UX.GA_UX, 151 true); 152 } 153 154 /** Verify that non-adult account can not trigger consent notification. */ 155 @Test testNonAdultAccount()156 public void testNonAdultAccount() throws Exception { 157 mTestName = new Object() {}.getClass().getEnclosingMethod().getName(); 158 159 mCommonManager.enableAdServices( 160 new AdServicesStates.Builder() 161 .setAdIdEnabled(true) 162 .setAdultAccount(false) 163 .setPrivacySandboxUiEnabled(true) 164 .build(), 165 Executors.newCachedThreadPool(), 166 mCallback); 167 168 AdservicesWorkflows.verifyNotification( 169 sContext, 170 mDevice, /* isDisplayed */ 171 false, /* isEuTest */ 172 false, 173 UiConstants.UX.GA_UX, 174 true); 175 } 176 177 /** 178 * Verify that for GA, ROW devices with non zeroed-out AdId, the GA ROW notification is 179 * displayed. 180 */ 181 @Test testGaRowAdIdEnabled()182 public void testGaRowAdIdEnabled() throws Exception { 183 mTestName = new Object() {}.getClass().getEnclosingMethod().getName(); 184 185 mCommonManager.enableAdServices( 186 new AdServicesStates.Builder() 187 .setAdIdEnabled(true) 188 .setAdultAccount(true) 189 .setPrivacySandboxUiEnabled(true) 190 .build(), 191 Executors.newCachedThreadPool(), 192 mCallback); 193 194 AdservicesWorkflows.verifyNotification( 195 sContext, 196 mDevice, /* isDisplayed */ 197 true, /* isEuTest */ 198 false, 199 UiConstants.UX.GA_UX, 200 true); 201 } 202 203 /** 204 * Verify that for GA, ROW devices with zeroed-out AdId, the GA EU notification is displayed. 205 */ 206 @Test testGaRowAdIdDisabled()207 public void testGaRowAdIdDisabled() throws Exception { 208 mTestName = new Object() {}.getClass().getEnclosingMethod().getName(); 209 210 mCommonManager.enableAdServices( 211 new AdServicesStates.Builder() 212 .setAdIdEnabled(false) 213 .setAdultAccount(true) 214 .setPrivacySandboxUiEnabled(true) 215 .build(), 216 Executors.newCachedThreadPool(), 217 mCallback); 218 219 AdservicesWorkflows.verifyNotification( 220 sContext, 221 mDevice, /* isDisplayed */ 222 true, /* isEuTest */ 223 true, 224 UiConstants.UX.GA_UX, 225 true); 226 } 227 } 228