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.experimental; 18 19 import static com.android.adservices.service.FlagsConstants.KEY_GLOBAL_KILL_SWITCH; 20 21 import android.os.SystemProperties; 22 import android.provider.DeviceConfig; 23 24 import com.android.adservices.shared.testing.AndroidLogger; 25 import com.android.adservices.shared.testing.Logger; 26 27 import org.junit.runners.model.FrameworkMethod; 28 import org.junit.runners.model.InitializationError; 29 import org.junit.runners.model.TestClass; 30 31 import java.util.List; 32 33 /** 34 * See {@link GlobalKillSwitchFlipper} - this class contains its main logic, while subclasses offers 35 * customizations. 36 */ 37 abstract class AbstractGlobalKillSwitchFlipper extends AbstractFlagsRouletteRunner { 38 39 public static final String FLAG = KEY_GLOBAL_KILL_SWITCH; 40 41 private static final String[] FLAGS = {FLAG}; 42 private static final String PROP_DISABLED = "debug.adservices.GlobalKillSwitchFlipper.disabled"; 43 44 private static final Logger sLogger = 45 new Logger(AndroidLogger.getInstance(), GlobalKillSwitchFlipper.class); 46 AbstractGlobalKillSwitchFlipper(Class<?> testClass)47 protected AbstractGlobalKillSwitchFlipper(Class<?> testClass) throws InitializationError { 48 super(testClass, new DeviceConfigFlagsManager(DeviceConfig.NAMESPACE_ADSERVICES)); 49 } 50 51 @Override getFlagsRoulette(TestClass testClass, List<Throwable> errors)52 protected String[] getFlagsRoulette(TestClass testClass, List<Throwable> errors) { 53 return FLAGS; 54 } 55 56 @Override log()57 protected Logger log() { 58 return sLogger; 59 } 60 61 @Override getRequiredFlagStates(FrameworkMethod method)62 protected FlagState[] getRequiredFlagStates(FrameworkMethod method) { 63 FlagState requiredFlag = null; 64 RequiresGlobalKillSwitchEnabled enabledAnnotation = 65 method.getAnnotation(RequiresGlobalKillSwitchEnabled.class); 66 if (enabledAnnotation != null) { 67 requiredFlag = new FlagState(FLAG, true); 68 } else { 69 RequiresGlobalKillSwitchDisabled disabledAnnotation = 70 method.getAnnotation(RequiresGlobalKillSwitchDisabled.class); 71 if (disabledAnnotation != null) { 72 requiredFlag = new FlagState(FLAG, false); 73 } 74 } 75 if (requiredFlag == null) { 76 return null; 77 } 78 sLogger.v("getRequiredFlags(): returning [%s] for %s", requiredFlag, method.getName()); 79 return new FlagState[] {requiredFlag}; 80 } 81 82 @Override isDisabled()83 protected boolean isDisabled() { 84 String propValue = SystemProperties.get(PROP_DISABLED); 85 if ("true".equalsIgnoreCase(propValue)) { 86 log().v("isDisabled(): returning true because of system property %s", PROP_DISABLED); 87 return true; 88 } 89 return false; 90 } 91 } 92