1 /* 2 * Copyright (C) 2021 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.tradefed.testtype.suite.params.multiuser; 18 19 import com.android.tradefed.config.IConfiguration; 20 import com.android.tradefed.config.IDeviceConfiguration; 21 import com.android.tradefed.targetprep.ITargetPreparer; 22 import com.android.tradefed.targetprep.RunOnSecondaryUserTargetPreparer; 23 import com.android.tradefed.targetprep.RunOnSystemUserTargetPreparer; 24 import com.android.tradefed.testtype.IRemoteTest; 25 import com.android.tradefed.testtype.ITestAnnotationFilterReceiver; 26 import com.android.tradefed.testtype.suite.params.IModuleParameterHandler; 27 28 import java.util.HashSet; 29 import java.util.List; 30 import java.util.Set; 31 32 public class RunOnSecondaryUserParameterHandler implements IModuleParameterHandler { 33 34 private static final List<String> REQUIRE_RUN_ON_SECONDARY_USER_NAMES = List.of( 35 "com.android.bedstead.multiuser.annotations.RequireRunOnSecondaryUser", 36 "com.android.bedstead.harrier.annotations.RequireRunOnSecondaryUser" 37 ); 38 39 @Override getParameterIdentifier()40 public String getParameterIdentifier() { 41 return "run-on-secondary-user"; 42 } 43 44 /** {@inheritDoc} */ 45 @Override addParameterSpecificConfig(IConfiguration moduleConfiguration)46 public void addParameterSpecificConfig(IConfiguration moduleConfiguration) { 47 for (IDeviceConfiguration deviceConfig : moduleConfiguration.getDeviceConfig()) { 48 List<ITargetPreparer> preparers = deviceConfig.getTargetPreparers(); 49 // The first thing the module will do is run on a work profile 50 preparers.add(0, new RunOnSecondaryUserTargetPreparer()); 51 52 // Remove the target preparer which forces onto system user 53 preparers.removeIf(preparer -> preparer instanceof RunOnSystemUserTargetPreparer); 54 } 55 } 56 57 @Override applySetup(IConfiguration moduleConfiguration)58 public void applySetup(IConfiguration moduleConfiguration) { 59 // Add filter to include @RequireRunOnSecondaryUser 60 for (IRemoteTest test : moduleConfiguration.getTests()) { 61 if (test instanceof ITestAnnotationFilterReceiver) { 62 ITestAnnotationFilterReceiver filterTest = (ITestAnnotationFilterReceiver) test; 63 filterTest.clearIncludeAnnotations(); 64 Set<String> excludeAnnotations = new HashSet<>(filterTest.getExcludeAnnotations()); 65 for (String requireRunOnSecondaryUserName : REQUIRE_RUN_ON_SECONDARY_USER_NAMES) { 66 filterTest.addIncludeAnnotation(requireRunOnSecondaryUserName); 67 excludeAnnotations.remove(requireRunOnSecondaryUserName); 68 } 69 filterTest.clearExcludeAnnotations(); 70 filterTest.addAllExcludeAnnotation(excludeAnnotations); 71 } 72 } 73 } 74 } 75