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 String REQUIRE_RUN_ON_SECONDARY_USER_NAME = 35 "com.android.bedstead.harrier.annotations.RequireRunOnSecondaryUser"; 36 37 @Override getParameterIdentifier()38 public String getParameterIdentifier() { 39 return "run-on-secondary-user"; 40 } 41 42 /** {@inheritDoc} */ 43 @Override addParameterSpecificConfig(IConfiguration moduleConfiguration)44 public void addParameterSpecificConfig(IConfiguration moduleConfiguration) { 45 for (IDeviceConfiguration deviceConfig : moduleConfiguration.getDeviceConfig()) { 46 List<ITargetPreparer> preparers = deviceConfig.getTargetPreparers(); 47 // The first thing the module will do is run on a work profile 48 preparers.add(0, new RunOnSecondaryUserTargetPreparer()); 49 50 // Remove the target preparer which forces onto system user 51 preparers.removeIf(preparer -> preparer instanceof RunOnSystemUserTargetPreparer); 52 } 53 } 54 55 @Override applySetup(IConfiguration moduleConfiguration)56 public void applySetup(IConfiguration moduleConfiguration) { 57 // Add filter to include @RequireRunOnSecondaryUser 58 for (IRemoteTest test : moduleConfiguration.getTests()) { 59 if (test instanceof ITestAnnotationFilterReceiver) { 60 ITestAnnotationFilterReceiver filterTest = (ITestAnnotationFilterReceiver) test; 61 filterTest.clearIncludeAnnotations(); 62 filterTest.addIncludeAnnotation(REQUIRE_RUN_ON_SECONDARY_USER_NAME); 63 64 Set<String> excludeAnnotations = new HashSet<>(filterTest.getExcludeAnnotations()); 65 excludeAnnotations.remove(REQUIRE_RUN_ON_SECONDARY_USER_NAME); 66 filterTest.clearExcludeAnnotations(); 67 filterTest.addAllExcludeAnnotation(excludeAnnotations); 68 } 69 } 70 } 71 } 72