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 ExtGaUxDebugChannelEuTest {
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                 true,
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, EU devices with non zeroed-out AdId, the GA EU notification is displayed.
179      */
180     @Test
testGaEuAdIdEnabled()181     public void testGaEuAdIdEnabled() throws Exception {
182         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
183 
184         mCommonManager.enableAdServices(
185                 new AdServicesStates.Builder()
186                         .setAdIdEnabled(true)
187                         .setAdultAccount(true)
188                         .setPrivacySandboxUiEnabled(true)
189                         .build(),
190                 Executors.newCachedThreadPool(),
191                 mCallback);
192 
193         AdservicesWorkflows.verifyNotification(
194                 sContext,
195                 mDevice, /* isDisplayed */
196                 true, /* isEuTest */
197                 true,
198                 UiConstants.UX.GA_UX,
199                 true);
200     }
201 
202     /** Verify that for GA, EU devices with zeroed-out AdId, the EU notification is displayed. */
203     @Test
testGaEuAdIdDisabled()204     public void testGaEuAdIdDisabled() throws Exception {
205         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
206 
207         mCommonManager.enableAdServices(
208                 new AdServicesStates.Builder()
209                         .setAdIdEnabled(false)
210                         .setAdultAccount(true)
211                         .setPrivacySandboxUiEnabled(true)
212                         .build(),
213                 Executors.newCachedThreadPool(),
214                 mCallback);
215 
216         AdservicesWorkflows.verifyNotification(
217                 sContext,
218                 mDevice, /* isDisplayed */
219                 true, /* isEuTest */
220                 true,
221                 UiConstants.UX.GA_UX,
222                 true);
223     }
224 }
225