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 
17 package com.android.adservices.tests.ui.u18ux.debugchannel;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.adservices.common.AdServicesCommonManager;
22 import android.adservices.common.AdServicesStates;
23 import android.content.Context;
24 import android.os.OutcomeReceiver;
25 import android.platform.test.rule.ScreenRecordRule;
26 
27 import androidx.test.platform.app.InstrumentationRegistry;
28 import androidx.test.runner.AndroidJUnit4;
29 import androidx.test.uiautomator.UiDevice;
30 
31 import com.android.adservices.common.AdservicesTestHelper;
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 
37 import org.junit.After;
38 import org.junit.Assert;
39 import org.junit.Assume;
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 import java.util.concurrent.Executor;
46 import java.util.concurrent.Executors;
47 
48 /** CTS test for U18 users */
49 @RunWith(AndroidJUnit4.class)
50 @ScreenRecordRule.ScreenRecord
51 public class U18UxDebugChannelTest {
52 
53     private static AdServicesCommonManager sCommonManager;
54     private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
55     private UiDevice mDevice;
56     private String mTestName;
57     private OutcomeReceiver<Boolean, Exception> mCallback;
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         UiUtils.setBinderTimeout();
68         AdservicesTestHelper.killAdservicesProcess(sContext);
69 
70         UiUtils.resetAdServicesConsentData(sContext);
71 
72         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
73 
74         UiUtils.enableNotificationPermission();
75         UiUtils.disableNotificationFlowV2();
76         UiUtils.disableOtaStrings();
77 
78         sCommonManager = AdServicesCommonManager.get(sContext);
79 
80         UiUtils.enableConsentDebugMode();
81         mCallback =
82                 new OutcomeReceiver<>() {
83                     @Override
84                     public void onResult(Boolean result) {
85                         assertThat(result).isTrue();
86                     }
87 
88                     @Override
89                     public void onError(Exception exception) {
90                         Assert.fail();
91                     }
92                 };
93 
94         mDevice.pressHome();
95     }
96 
97     @After
tearDown()98     public void tearDown() throws Exception {
99         if (!AdservicesTestHelper.isDeviceSupported()) return;
100 
101         UiUtils.takeScreenshot(mDevice, getClass().getSimpleName() + "_" + mTestName + "_");
102 
103         mDevice.pressHome();
104 
105         AdservicesTestHelper.killAdservicesProcess(sContext);
106     }
107 
108     @Test
testEntrypointDisabled()109     public void testEntrypointDisabled() throws Exception {
110         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
111 
112         UiUtils.enableU18();
113         UiUtils.enableGa();
114 
115         AdservicesTestHelper.killAdservicesProcess(sContext);
116 
117         AdServicesStates adServicesStates =
118                 new AdServicesStates.Builder()
119                         .setAdIdEnabled(false)
120                         .setAdultAccount(true)
121                         .setU18Account(true)
122                         .setPrivacySandboxUiEnabled(false)
123                         .setPrivacySandboxUiRequest(false)
124                         .build();
125 
126         sCommonManager.enableAdServices(adServicesStates, CALLBACK_EXECUTOR, mCallback);
127 
128         AdservicesWorkflows.verifyNotification(
129                 sContext,
130                 mDevice, /* isDisplayed */
131                 false, /* isEuTest */
132                 false,
133                 UiConstants.UX.U18_UX);
134     }
135 
136     @Test
testU18AdultBothTrueAdIdEnabled()137     public void testU18AdultBothTrueAdIdEnabled() throws Exception {
138         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
139 
140         UiUtils.enableU18();
141         UiUtils.enableGa();
142 
143         AdservicesTestHelper.killAdservicesProcess(sContext);
144 
145         AdServicesStates adServicesStates =
146                 new AdServicesStates.Builder()
147                         .setAdIdEnabled(true)
148                         .setAdultAccount(true)
149                         .setU18Account(true)
150                         .setPrivacySandboxUiEnabled(true)
151                         .setPrivacySandboxUiRequest(false)
152                         .build();
153 
154         sCommonManager.enableAdServices(adServicesStates, CALLBACK_EXECUTOR, mCallback);
155 
156         AdservicesWorkflows.verifyNotification(
157                 sContext,
158                 mDevice, /* isDisplayed */
159                 true, /* isEuTest */
160                 false,
161                 UiConstants.UX.U18_UX);
162     }
163 
164     @Test
testU18TrueAdultFalseAdIdEnabled()165     public void testU18TrueAdultFalseAdIdEnabled() throws Exception {
166         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
167 
168         UiUtils.enableU18();
169         UiUtils.enableGa();
170 
171         AdservicesTestHelper.killAdservicesProcess(sContext);
172 
173         AdServicesStates adServicesStates =
174                 new AdServicesStates.Builder()
175                         .setAdIdEnabled(true)
176                         .setAdultAccount(false)
177                         .setU18Account(true)
178                         .setPrivacySandboxUiEnabled(true)
179                         .setPrivacySandboxUiRequest(false)
180                         .build();
181 
182         sCommonManager.enableAdServices(adServicesStates, CALLBACK_EXECUTOR, mCallback);
183 
184         AdservicesWorkflows.verifyNotification(
185                 sContext,
186                 mDevice, /* isDisplayed */
187                 true, /* isEuTest */
188                 false,
189                 UiConstants.UX.U18_UX);
190     }
191 
192     @Test
testU18AdultBothTrueAdIdDisabled()193     public void testU18AdultBothTrueAdIdDisabled() throws Exception {
194         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
195 
196         UiUtils.enableU18();
197         UiUtils.enableGa();
198 
199         AdservicesTestHelper.killAdservicesProcess(sContext);
200 
201         AdServicesStates adServicesStates =
202                 new AdServicesStates.Builder()
203                         .setAdIdEnabled(false)
204                         .setAdultAccount(true)
205                         .setU18Account(true)
206                         .setPrivacySandboxUiEnabled(true)
207                         .setPrivacySandboxUiRequest(false)
208                         .build();
209 
210         sCommonManager.enableAdServices(adServicesStates, CALLBACK_EXECUTOR, mCallback);
211 
212         AdservicesWorkflows.verifyNotification(
213                 sContext,
214                 mDevice, /* isDisplayed */
215                 true, /* isEuTest */
216                 false,
217                 UiConstants.UX.U18_UX);
218     }
219 
220     @Test
testU18TrueAdultFalseAdIdDisabled()221     public void testU18TrueAdultFalseAdIdDisabled() throws Exception {
222         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
223 
224         UiUtils.enableU18();
225         UiUtils.enableGa();
226 
227         AdservicesTestHelper.killAdservicesProcess(sContext);
228 
229         AdServicesStates adServicesStates =
230                 new AdServicesStates.Builder()
231                         .setAdIdEnabled(false)
232                         .setAdultAccount(false)
233                         .setU18Account(true)
234                         .setPrivacySandboxUiEnabled(true)
235                         .setPrivacySandboxUiRequest(false)
236                         .build();
237 
238         sCommonManager.enableAdServices(adServicesStates, CALLBACK_EXECUTOR, mCallback);
239 
240         AdservicesWorkflows.verifyNotification(
241                 sContext,
242                 mDevice, /* isDisplayed */
243                 true, /* isEuTest */
244                 false,
245                 UiConstants.UX.U18_UX);
246     }
247 
248     @Test
testU18AdultBothFalseAdIdDisabled()249     public void testU18AdultBothFalseAdIdDisabled() throws Exception {
250         mTestName = new Object() {}.getClass().getEnclosingMethod().getName();
251 
252         UiUtils.enableU18();
253         UiUtils.enableGa();
254 
255         AdservicesTestHelper.killAdservicesProcess(sContext);
256 
257         AdServicesStates adServicesStates =
258                 new AdServicesStates.Builder()
259                         .setAdIdEnabled(false)
260                         .setAdultAccount(false)
261                         .setU18Account(false)
262                         .setPrivacySandboxUiEnabled(true)
263                         .setPrivacySandboxUiRequest(false)
264                         .build();
265 
266         sCommonManager.enableAdServices(adServicesStates, CALLBACK_EXECUTOR, mCallback);
267 
268         AdservicesWorkflows.verifyNotification(
269                 sContext, mDevice, false, false, UiConstants.UX.U18_UX);
270     }
271 }
272