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.libs.pages;
18 
19 import static com.android.adservices.tests.ui.libs.UiUtils.PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT;
20 import static com.android.adservices.tests.ui.libs.UiUtils.scrollToAndClick;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import android.content.Context;
25 
26 import androidx.test.uiautomator.UiDevice;
27 import androidx.test.uiautomator.UiObject;
28 import androidx.test.uiautomator.UiObjectNotFoundException;
29 import androidx.test.uiautomator.UiSelector;
30 
31 import com.android.adservices.api.R;
32 import com.android.adservices.tests.ui.libs.UiConstants;
33 import com.android.adservices.tests.ui.libs.UiUtils;
34 
35 public class SettingsPages {
testSettingsPageConsents( Context context, UiDevice device, UiConstants.UX ux, boolean isOptin, boolean flipConsent, boolean assertOptIn)36     public static void testSettingsPageConsents(
37             Context context,
38             UiDevice device,
39             UiConstants.UX ux,
40             boolean isOptin,
41             boolean flipConsent,
42             boolean assertOptIn)
43             throws UiObjectNotFoundException, InterruptedException {
44         switch (ux) {
45             case GA_UX:
46                 enterTopicsConsentPage(context, device);
47                 flipConsent(device, isOptin, flipConsent, assertOptIn);
48                 device.pressBack();
49                 enterFledgeConsentPage(context, device);
50                 flipConsent(device, isOptin, flipConsent, assertOptIn);
51                 device.pressBack();
52                 enterMsmtConsentPage(context, device);
53                 flipConsent(device, isOptin, flipConsent, assertOptIn);
54                 device.pressBack();
55                 break;
56             case BETA_UX:
57                 flipConsent(device, isOptin, flipConsent, assertOptIn);
58                 break;
59             case U18_UX:
60                 enterU18ConsentPage(context, device);
61                 flipConsent(device, isOptin, flipConsent, assertOptIn);
62                 break;
63         }
64     }
65 
enterTopicsConsentPage(Context context, UiDevice device)66     public static void enterTopicsConsentPage(Context context, UiDevice device)
67             throws UiObjectNotFoundException, InterruptedException {
68         scrollToAndClick(context, device, R.string.settingsUI_topics_ga_title);
69     }
70 
enterMsmtConsentPage(Context context, UiDevice device)71     public static void enterMsmtConsentPage(Context context, UiDevice device)
72             throws UiObjectNotFoundException, InterruptedException {
73         scrollToAndClick(context, device, R.string.settingsUI_measurement_view_title);
74     }
75 
enterU18ConsentPage(Context context, UiDevice device)76     public static void enterU18ConsentPage(Context context, UiDevice device)
77             throws UiObjectNotFoundException, InterruptedException {
78         scrollToAndClick(context, device, R.string.settingsUI_u18_measurement_view_title);
79     }
80 
enterFledgeConsentPage(Context context, UiDevice device)81     public static void enterFledgeConsentPage(Context context, UiDevice device)
82             throws UiObjectNotFoundException, InterruptedException {
83         scrollToAndClick(context, device, R.string.settingsUI_apps_ga_title);
84     }
85 
86     /** Flips the consent toggle. */
flipConsent( UiDevice device, boolean isOptin, boolean flipConsent, boolean assertOptIn)87     public static void flipConsent(
88             UiDevice device, boolean isOptin, boolean flipConsent, boolean assertOptIn)
89             throws UiObjectNotFoundException, InterruptedException {
90         UiObject consentSwitch =
91                 device.findObject(new UiSelector().className("android.widget.Switch"));
92         consentSwitch.waitForExists(PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT);
93         assertThat(consentSwitch.exists()).isTrue();
94 
95         // The consent toggle can be blocked by the navigation bar on some devices.
96         UiUtils.gentleSwipe(device);
97 
98         boolean consentStatus = consentSwitch.isChecked();
99         if (flipConsent) {
100             consentSwitch.clickTopLeft();
101             Thread.sleep(PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT);
102             assertThat(consentSwitch.isChecked()).isEqualTo(!consentStatus);
103         } else if (isOptin) {
104             if (!consentStatus) {
105                 consentSwitch.clickTopLeft();
106             }
107             Thread.sleep(PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT);
108             assertThat(consentSwitch.isChecked()).isTrue();
109         } else if (assertOptIn) {
110             assertThat(consentSwitch.isChecked()).isTrue();
111         } else {
112             if (consentStatus) {
113                 consentSwitch.clickTopLeft();
114             }
115             Thread.sleep(PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT);
116             assertThat(consentSwitch.isChecked()).isFalse();
117         }
118     }
119 }
120