1 /* 2 * Copyright (C) 2024 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.ui.util; 18 19 import static com.android.adservices.ui.util.AdServicesUiTestCase.LAUNCH_TIMEOUT; 20 import static com.android.adservices.ui.util.ApkTestUtil.getConsentSwitch; 21 import static com.android.compatibility.common.util.ShellUtils.runShellCommand; 22 23 import static com.google.common.truth.Truth.assertThat; 24 import static com.google.common.truth.Truth.assertWithMessage; 25 26 import android.os.Build; 27 import android.os.RemoteException; 28 import android.util.Log; 29 30 import androidx.test.uiautomator.UiDevice; 31 import androidx.test.uiautomator.UiObject2; 32 import androidx.test.uiautomator.Until; 33 34 import com.android.adservices.api.R; 35 import com.android.compatibility.common.util.ShellUtils; 36 37 /** Util class for Settings tests. */ 38 public final class SettingsTestUtil { 39 40 private static final String ANDROID_WIDGET_SWITCH = "android.widget.Switch"; 41 42 private static final String TAG = SettingsTestUtil.class.getSimpleName(); 43 private static final int WINDOW_LAUNCH_TIMEOUT = 2_000; 44 private static final int PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS = 2_000; 45 settingsRemoveMainToggleAndMeasurementEntryTestUtil(UiDevice device)46 public static void settingsRemoveMainToggleAndMeasurementEntryTestUtil(UiDevice device) { 47 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 48 49 // make sure we are on the main settings page 50 UiObject2 appButton = ApkTestUtil.scrollTo(device, R.string.settingsUI_apps_ga_title); 51 assertNotNull(appButton, R.string.settingsUI_apps_ga_title); 52 53 UiObject2 topicsButton = ApkTestUtil.scrollTo(device, R.string.settingsUI_topics_ga_title); 54 assertNotNull(topicsButton, R.string.settingsUI_topics_ga_title); 55 56 // click measurement page 57 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_title); 58 59 // verify have entered to measurement page 60 UiObject2 measurementSwitch = 61 ApkTestUtil.getElement(device, R.string.settingsUI_measurement_switch_title); 62 assertNotNull(measurementSwitch, R.string.settingsUI_measurement_switch_title); 63 64 pressBack(device); 65 // verify back to the main page 66 assertNotNull(appButton, R.string.settingsUI_apps_ga_title); 67 } 68 settingsRemoveMainToggleAndMeasurementEntryTestRvcUxUtil(UiDevice device)69 public static void settingsRemoveMainToggleAndMeasurementEntryTestRvcUxUtil(UiDevice device) { 70 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 71 72 UiObject2 appButton = 73 ApkTestUtil.scrollTo(device, R.string.settingsUI_measurement_view_title); 74 assertNotNull(appButton, R.string.settingsUI_measurement_view_title); 75 76 // make sure we are on the main settings page 77 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_title); 78 79 // verify have entered to measurement page 80 UiObject2 measurementSwitch = 81 ApkTestUtil.getElement(device, R.string.settingsUI_measurement_switch_title); 82 assertNotNull(measurementSwitch, R.string.settingsUI_measurement_switch_title); 83 84 pressBack(device); 85 // verify back to the main page 86 assertNotNull(appButton, R.string.settingsUI_measurement_view_title); 87 } 88 measurementDialogTestUtil(UiDevice device)89 public static void measurementDialogTestUtil(UiDevice device) throws RemoteException { 90 runShellCommand("device_config put adservices ui_dialogs_feature_enabled true"); 91 device.setOrientationNatural(); 92 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 93 // open measurement view 94 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_title); 95 96 // click reset 97 SettingsTestUtil.clickResetButton(device); 98 UiObject2 resetButton = 99 ApkTestUtil.getElement(device, R.string.settingsUI_measurement_view_reset_title); 100 assertNotNull(resetButton, R.string.settingsUI_measurement_view_reset_title); 101 102 // click reset again 103 SettingsTestUtil.clickResetButton(device); 104 resetButton = 105 ApkTestUtil.getElement(device, R.string.settingsUI_measurement_view_reset_title); 106 assertNotNull(resetButton, R.string.settingsUI_measurement_view_reset_title); 107 } 108 topicsToggleTestUtil(UiDevice device)109 public static void topicsToggleTestUtil(UiDevice device) throws RemoteException { 110 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled false"); 111 112 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 113 // 1) disable Topics API is enabled 114 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_topics_ga_title); 115 116 UiObject2 topicsToggle = getConsentSwitch(device); 117 if (topicsToggle.isChecked()) { 118 topicsToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 119 } 120 assertToggleState(topicsToggle, /* checked= */ false); 121 pressBack(device); 122 123 // 2) enable Topics API 124 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_topics_ga_title); 125 126 topicsToggle = getConsentSwitch(device); 127 assertToggleState(topicsToggle, /* checked= */ false); 128 topicsToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 129 assertToggleState(topicsToggle, /* checked= */ true); 130 pressBack(device); 131 132 // 3) check if Topics API is enabled 133 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_topics_ga_title); 134 // rotate device to test rotating as well 135 device.setOrientationLeft(); 136 device.setOrientationNatural(); 137 topicsToggle = getConsentSwitch(device); 138 assertToggleState(topicsToggle, /* checked= */ true); 139 pressBack(device); 140 } 141 fledgeToggleTestUtil(UiDevice device)142 public static void fledgeToggleTestUtil(UiDevice device) throws RemoteException { 143 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled false"); 144 145 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 146 // 1) disable Fledge API is enabled 147 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 148 149 UiObject2 fledgeToggle = getConsentSwitch(device); 150 if (fledgeToggle.isChecked()) { 151 fledgeToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 152 } 153 assertToggleState(fledgeToggle, /* checked= */ false); 154 pressBack(device); 155 156 // 2) enable Fledge API 157 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 158 159 fledgeToggle = getConsentSwitch(device); 160 assertToggleState(fledgeToggle, /* checked= */ false); 161 fledgeToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 162 assertToggleState(fledgeToggle, /* checked= */ true); 163 pressBack(device); 164 165 // 3) check if Fledge API is enabled 166 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 167 // rotate device to test rotating as well 168 device.setOrientationLeft(); 169 device.setOrientationNatural(); 170 fledgeToggle = getConsentSwitch(device); 171 assertToggleState(fledgeToggle, /* checked= */ true); 172 pressBack(device); 173 } 174 measurementToggleTestUtil(UiDevice device)175 public static void measurementToggleTestUtil(UiDevice device) throws RemoteException { 176 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled false"); 177 178 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 179 // 1) disable Measurement API is enabled 180 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_title); 181 182 UiObject2 measurementToggle = getConsentSwitch(device); 183 if (measurementToggle.isChecked()) { 184 measurementToggle.clickAndWait( 185 Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 186 } 187 assertToggleState(measurementToggle, /* checked= */ false); 188 pressBack(device); 189 190 // 2) enable Measurement API 191 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_title); 192 193 measurementToggle = getConsentSwitch(device); 194 assertToggleState(measurementToggle, /* checked= */ false); 195 measurementToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 196 assertToggleState(measurementToggle, /* checked= */ true); 197 pressBack(device); 198 199 // 3) check if Measurement API is enabled 200 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_title); 201 // rotate device to test rotating as well 202 device.setOrientationLeft(); 203 device.setOrientationNatural(); 204 measurementToggle = getConsentSwitch(device); 205 assertToggleState(measurementToggle, /* checked= */ true); 206 pressBack(device); 207 } 208 topicsSubtitleTestUtil(UiDevice device)209 public static void topicsSubtitleTestUtil(UiDevice device) { 210 runShellCommand("device_config put adservices ui_dialogs_feature_enabled false"); 211 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled false"); 212 213 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 214 SettingsTestUtil.checkSubtitleMatchesToggle( 215 device, 216 ".*:id/topics_preference_subtitle", 217 R.string.settingsUI_topics_ga_title); 218 } 219 appsSubtitleTestUtil(UiDevice device)220 public static void appsSubtitleTestUtil(UiDevice device) { 221 runShellCommand("device_config put adservices ui_dialogs_feature_enabled false"); 222 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled false"); 223 224 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 225 SettingsTestUtil.checkSubtitleMatchesToggle( 226 device, 227 ".*:id/apps_preference_subtitle", 228 R.string.settingsUI_apps_ga_title); 229 } 230 measurementSubtitleTestUtil(UiDevice device)231 public static void measurementSubtitleTestUtil(UiDevice device) { 232 runShellCommand("device_config put adservices ui_dialogs_feature_enabled false"); 233 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled false"); 234 235 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 236 SettingsTestUtil.checkSubtitleMatchesToggle( 237 device, 238 ".*:id/measurement_preference_subtitle", 239 R.string.settingsUI_measurement_view_title); 240 } 241 topicsToggleDialogTestUtil(UiDevice device)242 public static void topicsToggleDialogTestUtil(UiDevice device) { 243 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled true"); 244 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 245 246 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_topics_ga_title); 247 248 UiObject2 topicsToggle = getConsentSwitch(device); 249 if (topicsToggle.isChecked()) { 250 // turn it off 251 topicsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 252 UiObject2 dialogOptOutTitle = 253 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_topics_opt_out_title); 254 UiObject2 positiveButton = 255 ApkTestUtil.getElement( 256 device, R.string.settingsUI_dialog_opt_out_positive_text); 257 assertNotNull(dialogOptOutTitle, R.string.settingsUI_dialog_topics_opt_out_title); 258 positiveButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 259 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 260 topicsToggle = getConsentSwitch(device); 261 assertToggleState(topicsToggle, /* checked= */ false); 262 // then turn it on again 263 topicsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 264 UiObject2 dialogOptInTitle = 265 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_topics_opt_in_title); 266 UiObject2 okButton = 267 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_acknowledge); 268 assertNotNull(dialogOptInTitle, R.string.settingsUI_dialog_topics_opt_in_title); 269 okButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 270 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 271 topicsToggle = getConsentSwitch(device); 272 assertToggleState(topicsToggle, /* checked= */ true); 273 } else { 274 // turn it on 275 topicsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 276 UiObject2 dialogOptInTitle = 277 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_topics_opt_in_title); 278 UiObject2 okButton = 279 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_acknowledge); 280 assertNotNull(dialogOptInTitle, R.string.settingsUI_dialog_topics_opt_in_title); 281 okButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 282 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 283 topicsToggle = getConsentSwitch(device); 284 assertToggleState(topicsToggle, /* checked= */ true); 285 // then turn it off 286 topicsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 287 UiObject2 dialogOptOutTitle = 288 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_topics_opt_out_title); 289 UiObject2 positiveButton = 290 ApkTestUtil.getElement( 291 device, R.string.settingsUI_dialog_opt_out_positive_text); 292 assertNotNull(dialogOptOutTitle, R.string.settingsUI_dialog_topics_opt_out_title); 293 positiveButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 294 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 295 topicsToggle = getConsentSwitch(device); 296 assertToggleState(topicsToggle, /* checked= */ false); 297 } 298 } 299 appsToggleDialogTestUtil(UiDevice device)300 public static void appsToggleDialogTestUtil(UiDevice device) { 301 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled true"); 302 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 303 304 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 305 306 UiObject2 appsToggle = getConsentSwitch(device); 307 if (appsToggle.isChecked()) { 308 // turn it off 309 appsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 310 UiObject2 dialogOptOutTitle = 311 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_apps_opt_out_title); 312 UiObject2 positiveButton = 313 ApkTestUtil.getElement( 314 device, R.string.settingsUI_dialog_opt_out_positive_text); 315 assertNotNull(dialogOptOutTitle, R.string.settingsUI_dialog_apps_opt_out_title); 316 positiveButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 317 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 318 appsToggle = getConsentSwitch(device); 319 assertToggleState(appsToggle, /* checked= */ false); 320 // then turn it on again 321 appsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 322 UiObject2 dialogOptInTitle = 323 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_apps_opt_in_title); 324 UiObject2 okButton = 325 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_acknowledge); 326 assertNotNull(dialogOptInTitle, R.string.settingsUI_dialog_apps_opt_in_title); 327 okButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 328 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 329 appsToggle = getConsentSwitch(device); 330 assertToggleState(appsToggle, /* checked= */ true); 331 } else { 332 // turn it on 333 appsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 334 UiObject2 dialogOptInTitle = 335 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_apps_opt_in_title); 336 UiObject2 okButton = 337 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_acknowledge); 338 assertNotNull(dialogOptInTitle, R.string.settingsUI_dialog_apps_opt_in_title); 339 okButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 340 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 341 appsToggle = getConsentSwitch(device); 342 assertToggleState(appsToggle, /* checked= */ true); 343 // then turn it off 344 appsToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 345 UiObject2 dialogOptOutTitle = 346 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_apps_opt_out_title); 347 UiObject2 positiveButton = 348 ApkTestUtil.getElement( 349 device, R.string.settingsUI_dialog_opt_out_positive_text); 350 assertNotNull(dialogOptOutTitle, R.string.settingsUI_dialog_apps_opt_out_title); 351 positiveButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 352 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 353 appsToggle = getConsentSwitch(device); 354 assertToggleState(appsToggle, /* checked= */ false); 355 } 356 } 357 measurementToggleDialogTestUtil(UiDevice device)358 public static void measurementToggleDialogTestUtil(UiDevice device) { 359 runShellCommand("device_config put adservices ui_toggle_speed_bump_enabled true"); 360 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 361 362 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_ga_title); 363 364 UiObject2 measurementToggle = getConsentSwitch(device); 365 366 if (measurementToggle.isChecked()) { 367 // turn it off 368 measurementToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 369 UiObject2 dialogOptOutTitle = 370 ApkTestUtil.getElement( 371 device, R.string.settingsUI_dialog_measurement_opt_out_title); 372 UiObject2 positiveButton = 373 ApkTestUtil.getElement( 374 device, R.string.settingsUI_dialog_opt_out_positive_text); 375 assertNotNull( 376 dialogOptOutTitle, 377 R.string.settingsUI_dialog_measurement_opt_out_title); 378 positiveButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 379 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 380 measurementToggle = getConsentSwitch(device); 381 assertToggleState(measurementToggle, /* checked= */ false); 382 // then turn it on again 383 measurementToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 384 UiObject2 dialogOptInTitle = 385 ApkTestUtil.getElement( 386 device, R.string.settingsUI_dialog_measurement_opt_in_title); 387 UiObject2 okButton = 388 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_acknowledge); 389 assertNotNull(dialogOptInTitle, R.string.settingsUI_dialog_measurement_opt_in_title); 390 okButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 391 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 392 measurementToggle = getConsentSwitch(device); 393 assertToggleState(measurementToggle, /* checked= */ true); 394 } else { 395 // turn it on 396 measurementToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 397 UiObject2 dialogOptInTitle = 398 ApkTestUtil.getElement( 399 device, R.string.settingsUI_dialog_measurement_opt_in_title); 400 UiObject2 okButton = 401 ApkTestUtil.getElement(device, R.string.settingsUI_dialog_acknowledge); 402 assertNotNull(dialogOptInTitle, R.string.settingsUI_dialog_measurement_opt_in_title); 403 okButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 404 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 405 measurementToggle = getConsentSwitch(device); 406 assertToggleState(measurementToggle, /* checked= */ true); 407 // then turn it off 408 measurementToggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 409 UiObject2 dialogOptOutTitle = 410 ApkTestUtil.getElement( 411 device, R.string.settingsUI_dialog_measurement_opt_out_title); 412 UiObject2 positiveButton = 413 ApkTestUtil.getElement( 414 device, R.string.settingsUI_dialog_opt_out_positive_text); 415 assertNotNull( 416 dialogOptOutTitle, 417 R.string.settingsUI_dialog_measurement_opt_out_title); 418 positiveButton.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 419 // Retrieve new instance to avoid android.support.test.uiautomator.StaleObjectException. 420 measurementToggle = getConsentSwitch(device); 421 assertToggleState(measurementToggle, /* checked= */ false); 422 } 423 } 424 425 /** 426 * Tests whether the new PAS Fledge view has updated PAS text. 427 * 428 * @param context Android context 429 * @param device UiDevice 430 * @throws RemoteException during screen rotation 431 */ fledgeViewTextPasEnabledTest(UiDevice device)432 public static void fledgeViewTextPasEnabledTest(UiDevice device) throws RemoteException { 433 ShellUtils.runShellCommand("device_config put adservices ga_ux_enabled true"); 434 ShellUtils.runShellCommand("device_config put adservices pas_ux_enabled true"); 435 ShellUtils.runShellCommand("setprop debug.adservices.consent_notification_debug_mode true"); 436 ShellUtils.runShellCommand( 437 "device_config put adservices is_eea_device_feature_enabled true"); 438 ShellUtils.runShellCommand("device_config put adservices is_eea_device false"); 439 ShellUtils.runShellCommand( 440 "device_config put adservices ui_toggle_speed_bump_enabled false"); 441 442 ApkTestUtil.launchSettingView(device, LAUNCH_TIMEOUT); 443 // 1) disable Fledge API is enabled 444 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 445 device.waitForIdle(); 446 447 UiObject2 fledgeToggle = getConsentSwitch(device); 448 if (fledgeToggle.isChecked()) { 449 fledgeToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 450 } 451 assertThat(fledgeToggle.isChecked()).isFalse(); 452 device.pressBack(); 453 454 // 2) enable Fledge API 455 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 456 457 fledgeToggle = getConsentSwitch(device); 458 assertThat(fledgeToggle.isChecked()).isFalse(); 459 fledgeToggle.clickAndWait(Until.newWindow(), PRIMITIVE_UI_OBJECTS_LAUNCH_TIMEOUT_MS); 460 fledgeToggle = getConsentSwitch(device); 461 assertThat(fledgeToggle.isChecked()).isTrue(); 462 device.pressBack(); 463 464 // 3) check if Fledge API is enabled 465 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_apps_ga_title); 466 // rotate device to test rotating as well 467 device.setOrientationLeft(); 468 device.setOrientationNatural(); 469 fledgeToggle = getConsentSwitch(device); 470 assertThat(fledgeToggle.isChecked()).isTrue(); 471 472 // 4) check text is PAS text 473 UiObject2 bodyText = 474 ApkTestUtil.getElement(device, R.string.settingsUI_pas_apps_view_body_text); 475 assertNotNull(bodyText, R.string.settingsUI_pas_apps_view_body_text); 476 device.pressBack(); 477 } 478 checkSubtitleMatchesToggle( UiDevice device, String regexResId, int stringIdOfTitle)479 public static void checkSubtitleMatchesToggle( 480 UiDevice device, String regexResId, int stringIdOfTitle) { 481 UiObject2 subtitle = ApkTestUtil.scrollTo(device, regexResId); 482 if (subtitle.getText() 483 .equals(ApkTestUtil.getString(R.string.settingsUI_subtitle_consent_off))) { 484 ApkTestUtil.scrollToAndClick(device, stringIdOfTitle); 485 UiObject2 toggle = getConsentSwitch(device); 486 assertToggleState(toggle, /* checked= */ false); 487 toggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 488 pressBack(device); 489 subtitle = ApkTestUtil.scrollTo(device, regexResId); 490 assertThat( 491 subtitle.getText() 492 .equals( 493 ApkTestUtil.getString( 494 R.string.settingsUI_subtitle_consent_off))) 495 .isFalse(); 496 } else { 497 ApkTestUtil.scrollToAndClick(device, stringIdOfTitle); 498 UiObject2 toggle = getConsentSwitch(device); 499 assertToggleState(toggle, /* checked= */ true); 500 toggle.clickAndWait(Until.newWindow(), WINDOW_LAUNCH_TIMEOUT); 501 pressBack(device); 502 subtitle = ApkTestUtil.scrollTo(device, regexResId); 503 assertThat( 504 subtitle.getText() 505 .equals( 506 ApkTestUtil.getString( 507 R.string.settingsUI_subtitle_consent_off))) 508 .isTrue(); 509 } 510 } 511 clickResetButton(UiDevice device)512 public static void clickResetButton(UiDevice device) { 513 // R Msmt UI is not scrollable 514 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) { 515 ApkTestUtil.click(device, R.string.settingsUI_measurement_view_reset_title); 516 } else { 517 ApkTestUtil.scrollToAndClick(device, R.string.settingsUI_measurement_view_reset_title); 518 } 519 } 520 assertNotNull(UiObject2 object, int resId)521 private static void assertNotNull(UiObject2 object, int resId) { 522 assertWithMessage("Button with text %s ", ApkTestUtil.getString(resId)) 523 .that(object) 524 .isNotNull(); 525 } 526 assertToggleState(UiObject2 toggleSwitch, boolean checked)527 private static void assertToggleState(UiObject2 toggleSwitch, boolean checked) { 528 if (checked) { 529 assertWithMessage("Toggle switch checked").that(toggleSwitch.isChecked()).isTrue(); 530 } else { 531 assertWithMessage("Toggle switch checked").that(toggleSwitch.isChecked()).isFalse(); 532 } 533 } 534 535 /** Presses the Back button. */ pressBack(UiDevice device)536 public static void pressBack(UiDevice device) { 537 Log.d(TAG, "pressBack()"); 538 device.pressBack(); 539 } 540 } 541