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 android.adservices.test.scenario.adservices.utils; 18 19 import android.Manifest; 20 21 import androidx.test.core.app.ApplicationProvider; 22 import androidx.test.platform.app.InstrumentationRegistry; 23 24 import com.android.adservices.common.AdservicesTestHelper; 25 import com.android.adservices.common.CompatAdServicesTestUtils; 26 import com.android.compatibility.common.util.ShellUtils; 27 import com.android.modules.utils.build.SdkLevel; 28 29 import org.junit.rules.TestRule; 30 import org.junit.runner.Description; 31 import org.junit.runners.model.Statement; 32 33 public class SelectAdsFlagRule implements TestRule { 34 @Override apply(Statement base, Description description)35 public Statement apply(Statement base, Description description) { 36 return new Statement() { 37 @Override 38 public void evaluate() throws Throwable { 39 setupFlags(); 40 base.evaluate(); 41 } 42 }; 43 } 44 45 private void setupFlags() { 46 InstrumentationRegistry.getInstrumentation() 47 .getUiAutomation() 48 .adoptShellPermissionIdentity(Manifest.permission.WRITE_DEVICE_CONFIG); 49 enableAdservicesApi(); 50 disableApiThrottling(); 51 disablePhenotypeFlagUpdates(); 52 extendAuctionTimeouts(); 53 // Disable backoff since we will be killing the process between tests 54 disableBackoff(); 55 } 56 57 private static void disableBackoff() { 58 String packageName = 59 AdservicesTestHelper.getAdServicesPackageName( 60 ApplicationProvider.getApplicationContext()); 61 ShellUtils.runShellCommand("am service-restart-backoff disable " + packageName); 62 } 63 64 private static void extendAuctionTimeouts() { 65 ShellUtils.runShellCommand( 66 "device_config put adservices fledge_ad_selection_bidding_timeout_per_ca_ms " 67 + "120000"); 68 ShellUtils.runShellCommand( 69 "device_config put adservices fledge_ad_selection_scoring_timeout_ms 120000"); 70 ShellUtils.runShellCommand( 71 "device_config put adservices fledge_ad_selection_overall_timeout_ms 120000"); 72 ShellUtils.runShellCommand( 73 "device_config put adservices fledge_ad_selection_bidding_timeout_per_buyer_ms " 74 + "120000"); 75 } 76 77 private static void disableApiThrottling() { 78 ShellUtils.runShellCommand( 79 "device_config put adservices sdk_request_permits_per_second 1000"); 80 } 81 82 private static void disablePhenotypeFlagUpdates() { 83 ShellUtils.runShellCommand("device_config set_sync_disabled_for_tests persistent"); 84 } 85 86 private static void enableAdservicesApi() { 87 ShellUtils.runShellCommand("setprop debug.adservices.disable_fledge_enrollment_check true"); 88 ShellUtils.runShellCommand("setprop debug.adservices.consent_manager_debug_mode true"); 89 ShellUtils.runShellCommand("device_config put adservices global_kill_switch false"); 90 ShellUtils.runShellCommand( 91 "device_config put adservices adservice_system_service_enabled true"); 92 // Extra flags to set for enabling AdServices on Android S- 93 if (!SdkLevel.isAtLeastT()) { 94 CompatAdServicesTestUtils.setFlags(); 95 } 96 } 97 } 98