1 /* 2 * Copyright (C) 2018 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 package com.android.tradefed.testtype.suite.params; 17 18 import static com.android.tradefed.testtype.suite.params.ModuleParameters.ALL_FOLDABLE_STATES; 19 import static com.android.tradefed.testtype.suite.params.ModuleParameters.INSTANT_APP; 20 import static com.android.tradefed.testtype.suite.params.ModuleParameters.MULTIUSER; 21 import static com.android.tradefed.testtype.suite.params.ModuleParameters.MULTI_ABI; 22 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NOT_INSTANT_APP; 23 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NOT_MULTI_ABI; 24 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NOT_RUN_ON_SDK_SANDBOX; 25 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NOT_SECONDARY_USER; 26 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NOT_SECONDARY_USER_ON_DEFAULT_DISPLAY; 27 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NOT_SECONDARY_USER_ON_SECONDARY_DISPLAY; 28 import static com.android.tradefed.testtype.suite.params.ModuleParameters.NO_FOLDABLE_STATES; 29 import static com.android.tradefed.testtype.suite.params.ModuleParameters.RUN_ON_CLONE_PROFILE; 30 import static com.android.tradefed.testtype.suite.params.ModuleParameters.RUN_ON_PRIVATE_PROFILE; 31 import static com.android.tradefed.testtype.suite.params.ModuleParameters.RUN_ON_SDK_SANDBOX; 32 import static com.android.tradefed.testtype.suite.params.ModuleParameters.RUN_ON_SECONDARY_USER; 33 import static com.android.tradefed.testtype.suite.params.ModuleParameters.RUN_ON_WORK_PROFILE; 34 import static com.android.tradefed.testtype.suite.params.ModuleParameters.SECONDARY_USER; 35 import static com.android.tradefed.testtype.suite.params.ModuleParameters.SECONDARY_USER_ON_DEFAULT_DISPLAY; 36 import static com.android.tradefed.testtype.suite.params.ModuleParameters.SECONDARY_USER_ON_SECONDARY_DISPLAY; 37 38 import com.android.tradefed.testtype.suite.params.multiuser.RunOnCloneProfileParameterHandler; 39 import com.android.tradefed.testtype.suite.params.multiuser.RunOnPrivateProfileParameterHandler; 40 import com.android.tradefed.testtype.suite.params.multiuser.RunOnSecondaryUserParameterHandler; 41 import com.android.tradefed.testtype.suite.params.multiuser.RunOnWorkProfileParameterHandler; 42 43 import java.util.Collections; 44 import java.util.HashMap; 45 import java.util.Map; 46 import java.util.Set; 47 48 /** Helper to get the {@link IModuleParameterHandler} associated with the parameter. */ 49 public final class ModuleParametersHelper { 50 51 private static final Map<ModuleParameters, IModuleParameterHandler> sHandlerMap = 52 Map.of( 53 INSTANT_APP, new InstantAppHandler(), 54 NOT_INSTANT_APP, new NegativeHandler(), 55 // line separator 56 MULTI_ABI, new NegativeHandler(), 57 NOT_MULTI_ABI, new NotMultiAbiHandler(), 58 // line separator 59 RUN_ON_WORK_PROFILE, new RunOnWorkProfileParameterHandler(), 60 RUN_ON_SECONDARY_USER, new RunOnSecondaryUserParameterHandler(), 61 // line separator 62 NO_FOLDABLE_STATES, new NegativeHandler(), 63 ALL_FOLDABLE_STATES, new FoldableExpandingHandler(), 64 RUN_ON_CLONE_PROFILE, new RunOnCloneProfileParameterHandler(), 65 RUN_ON_PRIVATE_PROFILE, new RunOnPrivateProfileParameterHandler()); 66 67 private static final Map<ModuleParameters, Set<ModuleParameters>> sGroupMap = 68 Map.of(MULTIUSER, Set.of(RUN_ON_WORK_PROFILE, RUN_ON_SECONDARY_USER, RUN_ON_CLONE_PROFILE, RUN_ON_PRIVATE_PROFILE)); 69 70 /** 71 * Optional parameters are params that will not automatically be created when the module 72 * parameterization is enabled. They will need to be explicitly enabled. They represent a second 73 * set of parameterization that is less commonly requested to run. They could be upgraded to 74 * main parameters in the future by moving them above. 75 */ 76 private static final Map<ModuleParameters, IModuleParameterHandler> sOptionalHandlerMap = 77 Map.of( 78 SECONDARY_USER, 79 new SecondaryUserHandler(), 80 NOT_SECONDARY_USER, 81 new NegativeHandler(), 82 SECONDARY_USER_ON_SECONDARY_DISPLAY, 83 new SecondaryUserOnSecondaryDisplayHandler(), 84 NOT_SECONDARY_USER_ON_SECONDARY_DISPLAY, 85 new NegativeHandler(), 86 SECONDARY_USER_ON_DEFAULT_DISPLAY, 87 new SecondaryUserOnDefaultDisplayHandler(), 88 NOT_SECONDARY_USER_ON_DEFAULT_DISPLAY, 89 new NegativeHandler(), 90 RUN_ON_SDK_SANDBOX, 91 new RunOnSdkSandboxHandler(), 92 NOT_RUN_ON_SDK_SANDBOX, 93 new NegativeHandler()); 94 95 // NOTE: sOptionalGroupMap is currently empty, but used on resolveParam(), so don't remove it 96 private static Map<ModuleParameters, Set<ModuleParameters>> sOptionalGroupMap = 97 Collections.emptyMap(); 98 99 /** 100 * Get the all {@link ModuleParameters} which are sub-params of a given {@link 101 * ModuleParameters}. 102 * 103 * <p>This will recursively resolve sub-groups and will only return {@link ModuleParameters} 104 * which are not groups. 105 * 106 * <p>If {@code param} is not a group then a singleton set containing {@code param} will be 107 * returned itself, regardless of {@code withOptional}. 108 * 109 * @param withOptional Whether or not to also check optional param groups. 110 */ resolveParam( ModuleParameters param, boolean withOptional)111 public static Map<ModuleParameters, IModuleParameterHandler> resolveParam( 112 ModuleParameters param, boolean withOptional) { 113 Set<ModuleParameters> mappedParams = sGroupMap.get(param); 114 if (mappedParams == null && withOptional) { 115 mappedParams = sOptionalGroupMap.get(param); 116 } 117 if (mappedParams == null) { 118 IModuleParameterHandler handler = getParameterHandler(param, withOptional); 119 if (handler == null) { 120 // If the handler is not supported yet (for example, optional params) skip the 121 // param. 122 return new HashMap<>(); 123 } 124 return Map.of(param, getParameterHandler(param, withOptional)); 125 } 126 // If the parameter is a group, expand it. 127 Map<ModuleParameters, IModuleParameterHandler> resolvedParams = new HashMap<>(); 128 for (ModuleParameters moduleParameters : mappedParams) { 129 resolvedParams.put(moduleParameters, sHandlerMap.get(moduleParameters)); 130 } 131 return resolvedParams; 132 } 133 134 /** 135 * Returns the {@link IModuleParameterHandler} associated with the requested parameter. 136 * 137 * @param withOptional Whether or not to also check optional params. 138 */ getParameterHandler( ModuleParameters param, boolean withOptional)139 private static IModuleParameterHandler getParameterHandler( 140 ModuleParameters param, boolean withOptional) { 141 IModuleParameterHandler value = sHandlerMap.get(param); 142 if (value == null && withOptional) { 143 return sOptionalHandlerMap.get(param); 144 } 145 return value; 146 } 147 } 148